VLCMediaPlayer.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2015 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2015 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Faustion Osuna <enrique.osuna # gmail.com>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCLibrary.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibVLCBridging.h"
  31. #if !TARGET_OS_IPHONE
  32. # import "VLCVideoView.h"
  33. #endif
  34. #ifdef HAVE_CONFIG_H
  35. # include "config.h"
  36. #endif
  37. #if !TARGET_OS_IPHONE
  38. /* prevent system sleep */
  39. # import <CoreServices/CoreServices.h>
  40. /* FIXME: Ugly hack! */
  41. # ifdef __x86_64__
  42. # import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  43. # endif
  44. #endif
  45. #include <vlc/vlc.h>
  46. /* Notification Messages */
  47. NSString *const VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  48. NSString *const VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  49. NSString *const VLCMediaPlayerTitleChanged = @"VLCMediaPlayerTitleChanged";
  50. NSString *const VLCMediaPlayerChapterChanged = @"VLCMediaPlayerChapterChanged";
  51. NSString *const VLCMediaPlayerSnapshotTaken = @"VLCMediaPlayerSnapshotTaken";
  52. /* title keys */
  53. NSString *const VLCTitleDescriptionName = @"VLCTitleDescriptionName";
  54. NSString *const VLCTitleDescriptionDuration = @"VLCTitleDescriptionDuration";
  55. NSString *const VLCTitleDescriptionIsMenu = @"VLCTitleDescriptionIsMenu";
  56. /* chapter keys */
  57. NSString *const VLCChapterDescriptionName = @"VLCChapterDescriptionName";
  58. NSString *const VLCChapterDescriptionTimeOffset = @"VLCChapterDescriptionTimeOffset";
  59. NSString *const VLCChapterDescriptionDuration = @"VLCChapterDescriptionDuration";
  60. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  61. {
  62. static NSString * stateToStrings[] = {
  63. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  64. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  65. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  66. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  67. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  68. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  69. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  70. };
  71. return stateToStrings[state];
  72. }
  73. // TODO: Documentation
  74. @interface VLCMediaPlayer (Private)
  75. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  76. - (void)registerObservers;
  77. - (void)unregisterObservers;
  78. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  79. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  80. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  81. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  82. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle;
  83. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter;
  84. #if TARGET_OS_IPHONE
  85. - (void)mediaPlayerSnapshot:(NSString *)fileName;
  86. #endif
  87. @end
  88. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  89. {
  90. @autoreleasepool {
  91. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  92. withMethod:@selector(mediaPlayerTimeChanged:)
  93. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  94. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  95. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  96. withNotificationName:VLCMediaPlayerTimeChanged];
  97. }
  98. }
  99. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  100. {
  101. @autoreleasepool {
  102. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  103. withMethod:@selector(mediaPlayerPositionChanged:)
  104. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  105. }
  106. }
  107. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  108. {
  109. VLCMediaPlayerState newState;
  110. if (event->type == libvlc_MediaPlayerPlaying)
  111. newState = VLCMediaPlayerStatePlaying;
  112. else if (event->type == libvlc_MediaPlayerPaused)
  113. newState = VLCMediaPlayerStatePaused;
  114. else if (event->type == libvlc_MediaPlayerStopped)
  115. newState = VLCMediaPlayerStateStopped;
  116. else if (event->type == libvlc_MediaPlayerEncounteredError)
  117. newState = VLCMediaPlayerStateError;
  118. else if (event->type == libvlc_MediaPlayerBuffering)
  119. newState = VLCMediaPlayerStateBuffering;
  120. else if (event->type == libvlc_MediaPlayerOpening)
  121. newState = VLCMediaPlayerStateOpening;
  122. else if (event->type == libvlc_MediaPlayerEndReached)
  123. newState = VLCMediaPlayerStateEnded;
  124. else {
  125. VKLog(@"%s: Unknown event", __FUNCTION__);
  126. return;
  127. }
  128. @autoreleasepool {
  129. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  130. withMethod:@selector(mediaPlayerStateChanged:)
  131. withArgumentAsObject:@(newState)];
  132. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  133. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  134. withNotificationName:VLCMediaPlayerStateChanged];
  135. }
  136. }
  137. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  138. {
  139. @autoreleasepool {
  140. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  141. withMethod:@selector(mediaPlayerMediaChanged:)
  142. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  143. }
  144. }
  145. static void HandleMediaTitleChanged(const libvlc_event_t * event, void * self)
  146. {
  147. @autoreleasepool {
  148. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  149. withDelegateMethod:@selector(mediaPlayerTitleChanged:)
  150. withNotificationName:VLCMediaPlayerTitleChanged];
  151. }
  152. }
  153. static void HandleMediaChapterChanged(const libvlc_event_t * event, void * self)
  154. {
  155. @autoreleasepool {
  156. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  157. withDelegateMethod:@selector(mediaPlayerChapterChanged:)
  158. withNotificationName:VLCMediaPlayerChapterChanged];
  159. }
  160. }
  161. #if TARGET_OS_IPHONE
  162. static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
  163. {
  164. @autoreleasepool {
  165. if (event->u.media_player_snapshot_taken.psz_filename != NULL) {
  166. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  167. withMethod:@selector(mediaPlayerSnapshot:)
  168. withArgumentAsObject:[NSString stringWithUTF8String:event->u.media_player_snapshot_taken.psz_filename]];
  169. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  170. withDelegateMethod:@selector(mediaPlayerSnapshot:)
  171. withNotificationName:VLCMediaPlayerSnapshotTaken];
  172. }
  173. }
  174. }
  175. #endif
  176. @interface VLCMediaPlayer ()
  177. {
  178. VLCLibrary *_privateLibrary; ///< Internal
  179. void * _playerInstance; ///< Internal
  180. VLCMedia * _media; ///< Current media being played
  181. VLCTime * _cachedTime; ///< Cached time of the media being played
  182. VLCTime * _cachedRemainingTime; ///< Cached remaining time of the media being played
  183. VLCMediaPlayerState _cachedState; ///< Cached state of the media being played
  184. float _position; ///< The position of the media being played
  185. id _drawable; ///< The drawable associated to this media player
  186. #if TARGET_OS_IPHONE
  187. NSMutableArray *_snapshots; ///< Array with snapshot file names
  188. #endif
  189. VLCAudio *_audio; ///< The audio controller
  190. libvlc_equalizer_t *_equalizerInstance; ///< The equalizer controller
  191. BOOL _equalizerEnabled; ///< Equalizer state
  192. }
  193. @end
  194. @implementation VLCMediaPlayer
  195. @synthesize libraryInstance = _privateLibrary;
  196. /* Bindings */
  197. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  198. {
  199. static NSDictionary * dict = nil;
  200. NSSet * superKeyPaths;
  201. if (!dict) {
  202. dict = @{@"playing": [NSSet setWithObject:@"state"],
  203. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  204. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  205. @"description": [NSSet setWithObjects:@"state", @"media", nil]};
  206. }
  207. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  208. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  209. [ret unionSet:superKeyPaths];
  210. return ret;
  211. }
  212. return dict[key];
  213. }
  214. /* Constructor */
  215. - (instancetype)init
  216. {
  217. return [self initWithDrawable:nil options:nil];
  218. }
  219. - (instancetype)initWithLibVLCInstance:(void *)playerInstance andLibrary:(VLCLibrary *)library
  220. {
  221. if (self = [super init]) {
  222. _cachedTime = [VLCTime nullTime];
  223. _cachedRemainingTime = [VLCTime nullTime];
  224. _position = 0.0f;
  225. _cachedState = VLCMediaPlayerStateStopped;
  226. _privateLibrary = library;
  227. libvlc_retain([_privateLibrary instance]);
  228. _playerInstance = playerInstance;
  229. libvlc_media_player_retain(_playerInstance);
  230. [self registerObservers];
  231. }
  232. return self;
  233. }
  234. #if !TARGET_OS_IPHONE
  235. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView
  236. {
  237. return [self initWithDrawable: aVideoView options:nil];
  238. }
  239. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  240. {
  241. return [self initWithDrawable: aVideoLayer options:nil];
  242. }
  243. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
  244. {
  245. return [self initWithDrawable: aVideoView options:options];
  246. }
  247. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
  248. {
  249. return [self initWithDrawable: aVideoLayer options:options];
  250. }
  251. #endif
  252. - (instancetype)initWithOptions:(NSArray *)options
  253. {
  254. return [self initWithDrawable:nil options:options];
  255. }
  256. - (void)dealloc
  257. {
  258. NSAssert(libvlc_media_player_get_state(_playerInstance) == libvlc_Stopped || libvlc_media_player_get_state(_playerInstance) == libvlc_NothingSpecial, @"You released the media player before ensuring that it is stopped");
  259. [self unregisterObservers];
  260. [[VLCEventManager sharedManager] cancelCallToObject:self];
  261. // Always get rid of the delegate first so we can stop sending messages to it
  262. // TODO: Should we tell the delegate that we're shutting down?
  263. _delegate = nil;
  264. // Clear our drawable as we are going to release it, we don't
  265. // want the core to use it from this point. This won't happen as
  266. // the media player must be stopped.
  267. libvlc_media_player_set_nsobject(_playerInstance, nil);
  268. if (_equalizerInstance) {
  269. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  270. libvlc_audio_equalizer_release(_equalizerInstance);
  271. }
  272. libvlc_media_player_release(_playerInstance);
  273. if (_privateLibrary != [VLCLibrary sharedLibrary])
  274. libvlc_release(_privateLibrary.instance);
  275. }
  276. #if !TARGET_OS_IPHONE
  277. - (void)setVideoView:(VLCVideoView *)aVideoView
  278. {
  279. [self setDrawable: aVideoView];
  280. }
  281. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  282. {
  283. [self setDrawable: aVideoLayer];
  284. }
  285. #endif
  286. - (void)setDrawable:(id)aDrawable
  287. {
  288. // Make sure that this instance has been associated with the drawing canvas.
  289. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  290. }
  291. - (id)drawable
  292. {
  293. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  294. }
  295. - (VLCAudio *)audio
  296. {
  297. if (!_audio)
  298. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  299. return _audio;
  300. }
  301. #pragma mark -
  302. #pragma mark Video Tracks
  303. - (void)setCurrentVideoTrackIndex:(int)value
  304. {
  305. libvlc_video_set_track(_playerInstance, value);
  306. }
  307. - (int)currentVideoTrackIndex
  308. {
  309. int count = libvlc_video_get_track_count(_playerInstance);
  310. if (count <= 0)
  311. return INT_MAX;
  312. return libvlc_video_get_track(_playerInstance);
  313. }
  314. - (NSArray *)videoTrackNames
  315. {
  316. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  317. if (count <= 0)
  318. return @[];
  319. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  320. libvlc_track_description_t *currentTrack = firstTrack;
  321. NSMutableArray *tempArray = [NSMutableArray array];
  322. while (currentTrack) {
  323. [tempArray addObject:@(currentTrack->psz_name)];
  324. currentTrack = currentTrack->p_next;
  325. }
  326. libvlc_track_description_list_release(firstTrack);
  327. return [NSArray arrayWithArray: tempArray];
  328. }
  329. - (NSArray *)videoTrackIndexes
  330. {
  331. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  332. if (count <= 0)
  333. return @[];
  334. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  335. libvlc_track_description_t *currentTrack = firstTrack;
  336. NSMutableArray *tempArray = [NSMutableArray array];
  337. while (currentTrack) {
  338. [tempArray addObject:@(currentTrack->i_id)];
  339. currentTrack = currentTrack->p_next;
  340. }
  341. libvlc_track_description_list_release(firstTrack);
  342. return [NSArray arrayWithArray: tempArray];
  343. }
  344. - (int)numberOfVideoTracks
  345. {
  346. return libvlc_video_get_track_count(_playerInstance);
  347. }
  348. #pragma mark -
  349. #pragma mark Subtitles
  350. - (void)setCurrentVideoSubTitleIndex:(int)index
  351. {
  352. libvlc_video_set_spu(_playerInstance, index);
  353. }
  354. - (int)currentVideoSubTitleIndex
  355. {
  356. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  357. if (count <= 0)
  358. return INT_MAX;
  359. return libvlc_video_get_spu(_playerInstance);
  360. }
  361. - (NSArray *)videoSubTitlesNames
  362. {
  363. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  364. if (count <= 0)
  365. return @[];
  366. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  367. libvlc_track_description_t *currentTrack = firstTrack;
  368. NSMutableArray *tempArray = [NSMutableArray array];
  369. while (currentTrack) {
  370. NSString *track = @(currentTrack->psz_name);
  371. [tempArray addObject:track != nil ? track : @""];
  372. currentTrack = currentTrack->p_next;
  373. }
  374. libvlc_track_description_list_release(firstTrack);
  375. return [NSArray arrayWithArray: tempArray];
  376. }
  377. - (NSArray *)videoSubTitlesIndexes
  378. {
  379. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  380. if (count <= 0)
  381. return @[];
  382. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  383. libvlc_track_description_t *currentTrack = firstTrack;
  384. NSMutableArray *tempArray = [NSMutableArray array];
  385. while (currentTrack) {
  386. [tempArray addObject:@(currentTrack->i_id)];
  387. currentTrack = currentTrack->p_next;
  388. }
  389. libvlc_track_description_list_release(firstTrack);
  390. return [NSArray arrayWithArray: tempArray];
  391. }
  392. - (int)numberOfSubtitlesTracks
  393. {
  394. return libvlc_video_get_spu_count(_playerInstance);
  395. }
  396. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  397. {
  398. return libvlc_media_player_add_slave(_playerInstance,
  399. libvlc_media_slave_type_subtitle,
  400. [path UTF8String],
  401. TRUE);
  402. }
  403. - (int)addPlaybackSlave:(NSURL *)slaveURL type:(VLCMediaPlaybackSlaveType)slaveType enforce:(BOOL)enforceSelection
  404. {
  405. if (!slaveURL)
  406. return -1;
  407. return libvlc_media_player_add_slave(_playerInstance,
  408. slaveType,
  409. [[slaveURL absoluteString] UTF8String],
  410. enforceSelection);
  411. }
  412. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  413. {
  414. libvlc_video_set_spu_delay(_playerInstance, index);
  415. }
  416. - (NSInteger)currentVideoSubTitleDelay
  417. {
  418. return libvlc_video_get_spu_delay(_playerInstance);
  419. }
  420. #if TARGET_OS_IPHONE
  421. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  422. {
  423. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  424. }
  425. - (void)setTextRendererFont:(NSString *)fontname
  426. {
  427. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  428. }
  429. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  430. {
  431. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  432. }
  433. - (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
  434. {
  435. libvlc_video_set_textrenderer_bool(_playerInstance, libvlc_textrender_fontforcebold, [fontForceBold boolValue]);
  436. }
  437. #endif
  438. #pragma mark -
  439. #pragma mark Video Crop geometry
  440. - (void)setVideoCropGeometry:(char *)value
  441. {
  442. libvlc_video_set_crop_geometry(_playerInstance, value);
  443. }
  444. - (char *)videoCropGeometry
  445. {
  446. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  447. return result;
  448. }
  449. - (void)setVideoAspectRatio:(char *)value
  450. {
  451. libvlc_video_set_aspect_ratio(_playerInstance, value);
  452. }
  453. - (char *)videoAspectRatio
  454. {
  455. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  456. return result;
  457. }
  458. - (void)setScaleFactor:(float)value
  459. {
  460. libvlc_video_set_scale(_playerInstance, value);
  461. }
  462. - (float)scaleFactor
  463. {
  464. return libvlc_video_get_scale(_playerInstance);
  465. }
  466. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  467. {
  468. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  469. if (failure)
  470. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  471. }
  472. - (void)setDeinterlaceFilter:(NSString *)name
  473. {
  474. if (!name || name.length < 1)
  475. libvlc_video_set_deinterlace(_playerInstance, NULL);
  476. else
  477. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  478. }
  479. - (BOOL)adjustFilterEnabled
  480. {
  481. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  482. }
  483. - (void)setAdjustFilterEnabled:(BOOL)b_value
  484. {
  485. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  486. }
  487. - (float)contrast
  488. {
  489. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  490. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  491. }
  492. - (void)setContrast:(float)f_value
  493. {
  494. if (f_value <= 2. && f_value >= 0.) {
  495. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  496. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  497. }
  498. }
  499. - (float)brightness
  500. {
  501. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  502. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  503. }
  504. - (void)setBrightness:(float)f_value
  505. {
  506. if (f_value <= 2. && f_value >= 0.) {
  507. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  508. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  509. }
  510. }
  511. - (float)hue
  512. {
  513. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  514. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  515. }
  516. - (void)setHue:(float)f_value
  517. {
  518. if (f_value <= 180. && f_value >= -180.) {
  519. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  520. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  521. }
  522. }
  523. - (float)saturation
  524. {
  525. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  526. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  527. }
  528. - (void)setSaturation:(float)f_value
  529. {
  530. if (f_value <= 3. && f_value >= 0.) {
  531. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  532. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  533. }
  534. }
  535. - (float)gamma
  536. {
  537. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  538. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  539. }
  540. - (void)setGamma:(float)f_value
  541. {
  542. if (f_value <= 10. && f_value >= 0.) {
  543. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  544. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  545. }
  546. }
  547. - (void)setRate:(float)value
  548. {
  549. libvlc_media_player_set_rate(_playerInstance, value);
  550. }
  551. - (float)rate
  552. {
  553. return libvlc_media_player_get_rate(_playerInstance);
  554. }
  555. - (CGSize)videoSize
  556. {
  557. unsigned height = 0, width = 0;
  558. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  559. if (failure)
  560. return CGSizeZero;
  561. return CGSizeMake(width, height);
  562. }
  563. - (BOOL)hasVideoOut
  564. {
  565. return libvlc_media_player_has_vout(_playerInstance);
  566. }
  567. - (float)framesPerSecond
  568. {
  569. return .0;
  570. }
  571. - (void)setTime:(VLCTime *)value
  572. {
  573. // Time is managed in seconds, while duration is managed in microseconds
  574. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  575. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0);
  576. }
  577. - (VLCTime *)time
  578. {
  579. return _cachedTime;
  580. }
  581. - (VLCTime *)remainingTime
  582. {
  583. return _cachedRemainingTime;
  584. }
  585. #pragma mark -
  586. #pragma mark Chapters
  587. - (void)setCurrentChapterIndex:(int)value;
  588. {
  589. libvlc_media_player_set_chapter(_playerInstance, value);
  590. }
  591. - (int)currentChapterIndex
  592. {
  593. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  594. if (count <= 0)
  595. return INT_MAX;
  596. int result = libvlc_media_player_get_chapter(_playerInstance);
  597. return result;
  598. }
  599. - (void)nextChapter
  600. {
  601. libvlc_media_player_next_chapter(_playerInstance);
  602. }
  603. - (void)previousChapter
  604. {
  605. libvlc_media_player_previous_chapter(_playerInstance);
  606. }
  607. - (NSArray *)chaptersForTitleIndex:(int)title
  608. {
  609. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  610. if (count <= 0)
  611. return @[];
  612. #pragma clang diagnostic push
  613. #pragma clang diagnostic ignored "-Wdeprecated"
  614. libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
  615. libvlc_track_description_t *currentTrack = firstTrack;
  616. #pragma clang diagnostic push
  617. NSMutableArray *tempArray = [NSMutableArray array];
  618. for (NSInteger i = 0; i < count ; i++) {
  619. [tempArray addObject:@(currentTrack->psz_name)];
  620. currentTrack = currentTrack->p_next;
  621. }
  622. libvlc_track_description_list_release(firstTrack);
  623. return [NSArray arrayWithArray:tempArray];
  624. }
  625. #pragma mark -
  626. #pragma mark Titles
  627. - (void)setCurrentTitleIndex:(int)value
  628. {
  629. libvlc_media_player_set_title(_playerInstance, value);
  630. }
  631. - (int)currentTitleIndex
  632. {
  633. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  634. if (count <= 0)
  635. return INT_MAX;
  636. return libvlc_media_player_get_title(_playerInstance);
  637. }
  638. - (int)numberOfTitles
  639. {
  640. return libvlc_media_player_get_title_count(_playerInstance);
  641. }
  642. - (NSUInteger)countOfTitles
  643. {
  644. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  645. return result;
  646. }
  647. - (NSArray *)titles
  648. {
  649. NSUInteger count = [self countOfTitles];
  650. if (count == 0)
  651. return [NSArray array];
  652. #pragma clang diagnostic push
  653. #pragma clang diagnostic ignored "-Wdeprecated"
  654. libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
  655. libvlc_track_description_t *currentTrack = firstTrack;
  656. #pragma clang diagnostic pop
  657. if (!currentTrack)
  658. return [NSArray array];
  659. NSMutableArray *tempArray = [NSMutableArray array];
  660. while (1) {
  661. if (currentTrack->psz_name != nil)
  662. [tempArray addObject:@(currentTrack->psz_name)];
  663. if (currentTrack->p_next)
  664. currentTrack = currentTrack->p_next;
  665. else
  666. break;
  667. }
  668. libvlc_track_description_list_release(firstTrack);
  669. return [NSArray arrayWithArray: tempArray];
  670. }
  671. - (NSArray *)titleDescriptions
  672. {
  673. libvlc_title_description_t **titleInfo;
  674. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  675. if (numberOfTitleDescriptions < 0)
  676. return [NSArray array];
  677. if (numberOfTitleDescriptions == 0) {
  678. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  679. return [NSArray array];
  680. }
  681. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  682. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  683. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  684. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  685. VLCTitleDescriptionDuration,
  686. @(titleInfo[i]->i_flags & libvlc_title_menu),
  687. VLCTitleDescriptionIsMenu,
  688. nil];
  689. if (titleInfo[i]->psz_name != NULL)
  690. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  691. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  692. }
  693. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  694. return [NSArray arrayWithArray:array];
  695. }
  696. - (int)indexOfLongestTitle
  697. {
  698. NSArray *titles = [self titleDescriptions];
  699. NSUInteger titleCount = titles.count;
  700. int currentlyFoundTitle = 0;
  701. int64_t currentlySelectedDuration = 0;
  702. int64_t randomTitleDuration = 0;
  703. for (int x = 0; x < titleCount; x++) {
  704. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  705. if (randomTitleDuration > currentlySelectedDuration) {
  706. currentlySelectedDuration = randomTitleDuration;
  707. currentlyFoundTitle = x;
  708. }
  709. }
  710. return currentlyFoundTitle;
  711. }
  712. - (int)numberOfChaptersForTitle:(int)titleIndex
  713. {
  714. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  715. }
  716. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  717. {
  718. libvlc_chapter_description_t **chapterDescriptions;
  719. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  720. titleIndex,
  721. &chapterDescriptions);
  722. if (numberOfChapterDescriptions < 0)
  723. return [NSArray array];
  724. if (numberOfChapterDescriptions == 0) {
  725. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  726. return [NSArray array];
  727. }
  728. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  729. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  730. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  731. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  732. VLCChapterDescriptionDuration,
  733. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  734. VLCChapterDescriptionTimeOffset,
  735. nil];
  736. if (chapterDescriptions[i]->psz_name != NULL)
  737. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  738. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  739. }
  740. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  741. return [NSArray arrayWithArray:array];
  742. }
  743. #pragma mark -
  744. #pragma mark Audio tracks
  745. - (void)setCurrentAudioTrackIndex:(int)value
  746. {
  747. libvlc_audio_set_track(_playerInstance, value);
  748. }
  749. - (int)currentAudioTrackIndex
  750. {
  751. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  752. if (count <= 0)
  753. return INT_MAX;
  754. return libvlc_audio_get_track(_playerInstance);
  755. }
  756. - (NSArray *)audioTrackNames
  757. {
  758. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  759. if (count <= 0)
  760. return @[];
  761. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  762. libvlc_track_description_t *currentTrack = firstTrack;
  763. NSMutableArray *tempArray = [NSMutableArray array];
  764. while (currentTrack) {
  765. NSString *track = @(currentTrack->psz_name);
  766. [tempArray addObject:track != nil ? track : @""];
  767. currentTrack = currentTrack->p_next;
  768. }
  769. libvlc_track_description_list_release(firstTrack);
  770. return [NSArray arrayWithArray: tempArray];
  771. }
  772. - (NSArray *)audioTrackIndexes
  773. {
  774. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  775. if (count <= 0)
  776. return @[];
  777. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  778. libvlc_track_description_t *currentTrack = firstTrack;
  779. NSMutableArray *tempArray = [NSMutableArray array];
  780. while (currentTrack) {
  781. [tempArray addObject:@(currentTrack->i_id)];
  782. currentTrack = currentTrack->p_next;
  783. }
  784. libvlc_track_description_list_release(firstTrack);
  785. return [NSArray arrayWithArray: tempArray];
  786. }
  787. - (int)numberOfAudioTracks
  788. {
  789. return libvlc_audio_get_track_count(_playerInstance);
  790. }
  791. - (void)setAudioChannel:(int)value
  792. {
  793. libvlc_audio_set_channel(_playerInstance, value);
  794. }
  795. - (int)audioChannel
  796. {
  797. return libvlc_audio_get_channel(_playerInstance);
  798. }
  799. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  800. {
  801. libvlc_audio_set_delay(_playerInstance, index);
  802. }
  803. - (NSInteger)currentAudioPlaybackDelay
  804. {
  805. return libvlc_audio_get_delay(_playerInstance);
  806. }
  807. #pragma mark -
  808. #pragma mark equalizer
  809. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  810. {
  811. _equalizerEnabled = equalizerEnabled;
  812. if (!_equalizerEnabled) {
  813. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  814. if (_equalizerInstance)
  815. libvlc_audio_equalizer_release(_equalizerInstance);
  816. return;
  817. }
  818. if (!_equalizerInstance)
  819. _equalizerInstance = libvlc_audio_equalizer_new();
  820. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  821. }
  822. - (BOOL)equalizerEnabled
  823. {
  824. return _equalizerEnabled;
  825. }
  826. - (NSArray *)equalizerProfiles
  827. {
  828. unsigned count = libvlc_audio_equalizer_get_preset_count();
  829. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  830. for (unsigned x = 0; x < count; x++)
  831. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  832. return [NSArray arrayWithArray:array];
  833. }
  834. - (void)resetEqualizerFromProfile:(unsigned)profile
  835. {
  836. BOOL wasactive = NO;
  837. if (_equalizerInstance) {
  838. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  839. libvlc_audio_equalizer_release(_equalizerInstance);
  840. wasactive = YES;
  841. }
  842. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  843. if (wasactive)
  844. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  845. }
  846. - (CGFloat)preAmplification
  847. {
  848. if (!_equalizerInstance)
  849. return 0.;
  850. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  851. }
  852. - (void)setPreAmplification:(CGFloat)preAmplification
  853. {
  854. if (!_equalizerInstance)
  855. _equalizerInstance = libvlc_audio_equalizer_new();
  856. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  857. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  858. }
  859. - (unsigned)numberOfBands
  860. {
  861. return libvlc_audio_equalizer_get_band_count();
  862. }
  863. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  864. {
  865. return libvlc_audio_equalizer_get_band_frequency(index);
  866. }
  867. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  868. {
  869. if (!_equalizerInstance)
  870. _equalizerInstance = libvlc_audio_equalizer_new();
  871. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  872. }
  873. - (CGFloat)amplificationOfBand:(unsigned int)index
  874. {
  875. if (!_equalizerInstance)
  876. return 0.;
  877. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  878. }
  879. #pragma mark -
  880. #pragma mark set/get media
  881. - (void)setMedia:(VLCMedia *)value
  882. {
  883. if (_media != value) {
  884. if (_media && [_media compare:value] == NSOrderedSame)
  885. return;
  886. _media = value;
  887. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  888. }
  889. }
  890. - (VLCMedia *)media
  891. {
  892. return _media;
  893. }
  894. #pragma mark -
  895. #pragma mark playback
  896. - (void)play
  897. {
  898. if ([NSThread isMainThread]) {
  899. /* Hack because we create a dead lock here, when the vout is created
  900. * and tries to recontact us on the main thread */
  901. /* FIXME: to do this properly we need to do some locking. We may want
  902. * to move that to libvlc */
  903. [self performSelectorInBackground:@selector(play) withObject:nil];
  904. return;
  905. }
  906. libvlc_media_player_play(_playerInstance);
  907. }
  908. - (void)pause
  909. {
  910. if ([NSThread isMainThread]) {
  911. /* Hack because we create a dead lock here, when the vout is stopped
  912. * and tries to recontact us on the main thread */
  913. /* FIXME: to do this properly we need to do some locking. We may want
  914. * to move that to libvlc */
  915. [self performSelectorInBackground:@selector(pause) withObject:nil];
  916. return;
  917. }
  918. // Pause the stream
  919. libvlc_media_player_pause(_playerInstance);
  920. }
  921. - (void)stop
  922. {
  923. if ([NSThread isMainThread]) {
  924. /* Hack because we create a dead lock here, when the vout is stopped
  925. * and tries to recontact us on the main thread */
  926. /* FIXME: to do this properly we need to do some locking. We may want
  927. * to move that to libvlc */
  928. [self performSelectorInBackground:@selector(stop) withObject:nil];
  929. return;
  930. }
  931. libvlc_media_player_stop(_playerInstance);
  932. }
  933. - (void)gotoNextFrame
  934. {
  935. libvlc_media_player_next_frame(_playerInstance);
  936. }
  937. - (void)fastForward
  938. {
  939. [self fastForwardAtRate: 2.0];
  940. }
  941. - (void)fastForwardAtRate:(float)rate
  942. {
  943. [self setRate:rate];
  944. }
  945. - (void)rewind
  946. {
  947. [self rewindAtRate: 2.0];
  948. }
  949. - (void)rewindAtRate:(float)rate
  950. {
  951. [self setRate: -rate];
  952. }
  953. - (void)jumpBackward:(int)interval
  954. {
  955. if ([self isSeekable]) {
  956. interval = interval * 1000;
  957. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  958. }
  959. }
  960. - (void)jumpForward:(int)interval
  961. {
  962. if ([self isSeekable]) {
  963. interval = interval * 1000;
  964. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  965. }
  966. }
  967. - (void)extraShortJumpBackward
  968. {
  969. [self jumpBackward:3];
  970. }
  971. - (void)extraShortJumpForward
  972. {
  973. [self jumpForward:3];
  974. }
  975. - (void)shortJumpBackward
  976. {
  977. [self jumpBackward:10];
  978. }
  979. - (void)shortJumpForward
  980. {
  981. [self jumpForward:10];
  982. }
  983. - (void)mediumJumpBackward
  984. {
  985. [self jumpBackward:60];
  986. }
  987. - (void)mediumJumpForward
  988. {
  989. [self jumpForward:60];
  990. }
  991. - (void)longJumpBackward
  992. {
  993. [self jumpBackward:300];
  994. }
  995. - (void)longJumpForward
  996. {
  997. [self jumpForward:300];
  998. }
  999. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  1000. {
  1001. libvlc_media_player_navigate(_playerInstance, action);
  1002. }
  1003. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  1004. {
  1005. return [NSSet setWithObjects:@"state", nil];
  1006. }
  1007. - (BOOL)isPlaying
  1008. {
  1009. return libvlc_media_player_is_playing(_playerInstance);
  1010. }
  1011. - (BOOL)willPlay
  1012. {
  1013. return libvlc_media_player_will_play(_playerInstance);
  1014. }
  1015. - (VLCMediaPlayerState)state
  1016. {
  1017. return _cachedState;
  1018. }
  1019. - (float)position
  1020. {
  1021. return _position;
  1022. }
  1023. - (void)setPosition:(float)newPosition
  1024. {
  1025. libvlc_media_player_set_position(_playerInstance, newPosition);
  1026. }
  1027. - (BOOL)isSeekable
  1028. {
  1029. return libvlc_media_player_is_seekable(_playerInstance);
  1030. }
  1031. - (BOOL)canPause
  1032. {
  1033. return libvlc_media_player_can_pause(_playerInstance);
  1034. }
  1035. #if TARGET_OS_IPHONE
  1036. - (NSArray *)snapshots
  1037. {
  1038. return [_snapshots copy];
  1039. }
  1040. - (UIImage *)lastSnapshot {
  1041. if (_snapshots == nil) {
  1042. return nil;
  1043. }
  1044. @synchronized(_snapshots) {
  1045. if (_snapshots.count == 0)
  1046. return nil;
  1047. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1048. }
  1049. }
  1050. #endif
  1051. - (void *)libVLCMediaPlayer
  1052. {
  1053. return _playerInstance;
  1054. }
  1055. @end
  1056. @implementation VLCMediaPlayer (Private)
  1057. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1058. {
  1059. if (self = [super init]) {
  1060. _cachedTime = [VLCTime nullTime];
  1061. _cachedRemainingTime = [VLCTime nullTime];
  1062. _position = 0.0f;
  1063. _cachedState = VLCMediaPlayerStateStopped;
  1064. // Create a media instance, it doesn't matter what library we start off with
  1065. // it will change depending on the media descriptor provided to the media
  1066. // instance
  1067. if (options && options.count > 0) {
  1068. VKLog(@"creating player instance with private library as options were given");
  1069. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1070. } else {
  1071. VKLog(@"creating player instance using shared library");
  1072. _privateLibrary = [VLCLibrary sharedLibrary];
  1073. }
  1074. libvlc_retain([_privateLibrary instance]);
  1075. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1076. libvlc_media_player_retain(_playerInstance);
  1077. [self registerObservers];
  1078. [self setDrawable:aDrawable];
  1079. }
  1080. return self;
  1081. }
  1082. - (void)registerObservers
  1083. {
  1084. // Attach event observers into the media instance
  1085. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1086. if (!p_em)
  1087. return;
  1088. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1089. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1090. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1091. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1092. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1093. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1094. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1095. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1096. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1097. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1098. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1099. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1100. #if TARGET_OS_IPHONE
  1101. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1102. #endif
  1103. }
  1104. - (void)unregisterObservers
  1105. {
  1106. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1107. if (!p_em)
  1108. return;
  1109. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1110. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1111. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1112. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1113. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1114. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1115. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1116. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1117. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1118. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1119. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1120. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1121. #if TARGET_OS_IPHONE
  1122. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1123. #endif
  1124. }
  1125. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1126. {
  1127. [self willChangeValueForKey:@"time"];
  1128. [self willChangeValueForKey:@"remainingTime"];
  1129. _cachedTime = [VLCTime timeWithNumber:newTime];
  1130. double currentTime = [[_cachedTime numberValue] doubleValue];
  1131. if (currentTime > 0 && _position > 0.) {
  1132. double remaining = currentTime / _position * (1 - _position);
  1133. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1134. } else
  1135. _cachedRemainingTime = [VLCTime nullTime];
  1136. [self didChangeValueForKey:@"remainingTime"];
  1137. [self didChangeValueForKey:@"time"];
  1138. }
  1139. #if !TARGET_OS_IPHONE
  1140. - (void)delaySleep
  1141. {
  1142. UpdateSystemActivity(UsrActivity);
  1143. }
  1144. #endif
  1145. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1146. {
  1147. #if !TARGET_OS_IPHONE
  1148. // This seems to be the most relevant place to delay sleeping and screen saver.
  1149. [self delaySleep];
  1150. #endif
  1151. [self willChangeValueForKey:@"position"];
  1152. _position = [newPosition floatValue];
  1153. [self didChangeValueForKey:@"position"];
  1154. }
  1155. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1156. {
  1157. [self willChangeValueForKey:@"state"];
  1158. _cachedState = [newState intValue];
  1159. #if TARGET_OS_IPHONE
  1160. // Disable idle timer if player is playing media
  1161. // Exclusion can be made for audio only media
  1162. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1163. #endif
  1164. [self didChangeValueForKey:@"state"];
  1165. }
  1166. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1167. {
  1168. [self willChangeValueForKey:@"media"];
  1169. if (_media != newMedia) {
  1170. _media = newMedia;
  1171. [self willChangeValueForKey:@"time"];
  1172. [self willChangeValueForKey:@"remainingTime"];
  1173. [self willChangeValueForKey:@"position"];
  1174. _cachedTime = [VLCTime nullTime];
  1175. _cachedRemainingTime = [VLCTime nullTime];
  1176. _position = 0.0f;
  1177. [self didChangeValueForKey:@"position"];
  1178. [self didChangeValueForKey:@"remainingTime"];
  1179. [self didChangeValueForKey:@"time"];
  1180. }
  1181. [self didChangeValueForKey:@"media"];
  1182. }
  1183. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1184. {
  1185. [self willChangeValueForKey:@"currentTitleIndex"];
  1186. [self didChangeValueForKey:@"currentTitleIndex"];
  1187. }
  1188. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1189. {
  1190. [self willChangeValueForKey:@"currentChapterIndex"];
  1191. [self didChangeValueForKey:@"currentChapterIndex"];
  1192. }
  1193. #if TARGET_OS_IPHONE
  1194. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1195. {
  1196. @synchronized(_snapshots) {
  1197. if (!_snapshots) {
  1198. _snapshots = [NSMutableArray array];
  1199. }
  1200. [_snapshots addObject:fileName];
  1201. }
  1202. }
  1203. #endif
  1204. @end