VLCMediaPlayer.m 46 KB

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