VLCMediaPlayer.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2019 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2019 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. [VLCMediaPlayerStateESAdded] = @"VLCMediaPlayerStateESAdded"
  72. };
  73. return stateToStrings[state];
  74. }
  75. // TODO: Documentation
  76. @interface VLCMediaPlayer (Private)
  77. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  78. - (void)registerObservers;
  79. - (void)unregisterObservers;
  80. - (dispatch_queue_t)libVLCBackgroundQueue;
  81. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  82. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  83. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  84. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  85. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle;
  86. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter;
  87. - (void)mediaPlayerSnapshot:(NSString *)fileName;
  88. - (void)mediaPlayerRecordChanged:(NSArray *)arguments;
  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. static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
  166. {
  167. @autoreleasepool {
  168. if (event->u.media_player_snapshot_taken.psz_filename != NULL) {
  169. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  170. withMethod:@selector(mediaPlayerSnapshot:)
  171. withArgumentAsObject:[NSString stringWithUTF8String:event->u.media_player_snapshot_taken.psz_filename]];
  172. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  173. withDelegateMethod:@selector(mediaPlayerSnapshot:)
  174. withNotificationName:VLCMediaPlayerSnapshotTaken];
  175. }
  176. }
  177. }
  178. static void HandleMediaPlayerRecord(const libvlc_event_t * event, void * self)
  179. {
  180. @autoreleasepool {
  181. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  182. withMethod:@selector(mediaPlayerRecordChanged:)
  183. withArgumentAsObject:@[@{@"filePath": [NSString stringWithFormat:@"%s", event->u.media_player_record_changed.file_path],
  184. @"isRecording": @(event->u.media_player_record_changed.recording)
  185. }]];
  186. }
  187. }
  188. @interface VLCMediaPlayer ()
  189. {
  190. VLCLibrary *_privateLibrary; ///< Internal
  191. void * _playerInstance; ///< Internal
  192. VLCMedia * _media; ///< Current media being played
  193. VLCTime * _cachedTime; ///< Cached time of the media being played
  194. VLCTime * _cachedRemainingTime; ///< Cached remaining time of the media being played
  195. VLCMediaPlayerState _cachedState; ///< Cached state of the media being played
  196. float _position; ///< The position of the media being played
  197. id _drawable; ///< The drawable associated to this media player
  198. NSMutableArray *_snapshots; ///< Array with snapshot file names
  199. VLCAudio *_audio; ///< The audio controller
  200. libvlc_equalizer_t *_equalizerInstance; ///< The equalizer controller
  201. BOOL _equalizerEnabled; ///< Equalizer state
  202. libvlc_video_viewpoint_t *_viewpoint; ///< Current viewpoint of the media
  203. dispatch_queue_t _libVLCBackgroundQueue; ///< Background dispatch queue to call libvlc
  204. }
  205. @end
  206. @implementation VLCMediaPlayer
  207. @synthesize libraryInstance = _privateLibrary;
  208. /* Bindings */
  209. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  210. {
  211. static NSDictionary * dict = nil;
  212. NSSet * superKeyPaths;
  213. if (!dict) {
  214. dict = @{@"playing": [NSSet setWithObject:@"state"],
  215. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  216. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  217. @"description": [NSSet setWithObjects:@"state", @"media", nil]};
  218. }
  219. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  220. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  221. [ret unionSet:superKeyPaths];
  222. return ret;
  223. }
  224. return dict[key];
  225. }
  226. /* Constructor */
  227. - (instancetype)init
  228. {
  229. return [self initWithDrawable:nil options:nil];
  230. }
  231. - (instancetype)initWithLibVLCInstance:(void *)playerInstance andLibrary:(VLCLibrary *)library
  232. {
  233. if (self = [super init]) {
  234. _cachedTime = [VLCTime nullTime];
  235. _cachedRemainingTime = [VLCTime nullTime];
  236. _position = 0.0f;
  237. _cachedState = VLCMediaPlayerStateStopped;
  238. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  239. _privateLibrary = library;
  240. libvlc_retain([_privateLibrary instance]);
  241. _playerInstance = playerInstance;
  242. [self registerObservers];
  243. }
  244. return self;
  245. }
  246. #if !TARGET_OS_IPHONE
  247. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView
  248. {
  249. return [self initWithDrawable: aVideoView options:nil];
  250. }
  251. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  252. {
  253. return [self initWithDrawable: aVideoLayer options:nil];
  254. }
  255. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
  256. {
  257. return [self initWithDrawable: aVideoView options:options];
  258. }
  259. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
  260. {
  261. return [self initWithDrawable: aVideoLayer options:options];
  262. }
  263. #endif
  264. - (instancetype)initWithOptions:(NSArray *)options
  265. {
  266. return [self initWithDrawable:nil options:options];
  267. }
  268. - (void)dealloc
  269. {
  270. NSAssert(libvlc_media_player_get_state(_playerInstance) == libvlc_Stopped ||
  271. libvlc_media_player_get_state(_playerInstance) == libvlc_NothingSpecial,
  272. @"You released the media player before ensuring that it is stopped");
  273. [self unregisterObservers];
  274. [[VLCEventManager sharedManager] cancelCallToObject:self];
  275. // Always get rid of the delegate first so we can stop sending messages to it
  276. // TODO: Should we tell the delegate that we're shutting down?
  277. _delegate = nil;
  278. // Clear our drawable as we are going to release it, we don't
  279. // want the core to use it from this point. This won't happen as
  280. // the media player must be stopped.
  281. libvlc_media_player_set_nsobject(_playerInstance, nil);
  282. if (_equalizerInstance) {
  283. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  284. libvlc_audio_equalizer_release(_equalizerInstance);
  285. _equalizerInstance = nil;
  286. }
  287. if (_viewpoint)
  288. libvlc_free(_viewpoint);
  289. libvlc_media_player_release(_playerInstance);
  290. if (_privateLibrary != [VLCLibrary sharedLibrary])
  291. libvlc_release(_privateLibrary.instance);
  292. }
  293. #if !TARGET_OS_IPHONE
  294. - (void)setVideoView:(VLCVideoView *)aVideoView
  295. {
  296. [self setDrawable: aVideoView];
  297. }
  298. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  299. {
  300. [self setDrawable: aVideoLayer];
  301. }
  302. #endif
  303. - (void)setDrawable:(id)aDrawable
  304. {
  305. // Make sure that this instance has been associated with the drawing canvas.
  306. _drawable = aDrawable;
  307. /* Note that ee need the caller to wait until the setter succeeded.
  308. * Otherwise, s/he might want to deploy the drawable while it isn’t ready yet. */
  309. dispatch_sync(_libVLCBackgroundQueue, ^{
  310. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  311. });
  312. }
  313. - (id)drawable
  314. {
  315. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  316. }
  317. - (VLCAudio *)audio
  318. {
  319. if (!_audio)
  320. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  321. return _audio;
  322. }
  323. #pragma mark -
  324. #pragma mark Video Tracks
  325. - (void)setCurrentVideoTrackIndex:(int)value
  326. {
  327. libvlc_video_set_track(_playerInstance, value);
  328. }
  329. - (int)currentVideoTrackIndex
  330. {
  331. int count = libvlc_video_get_track_count(_playerInstance);
  332. if (count <= 0)
  333. return -1;
  334. return libvlc_video_get_track(_playerInstance);
  335. }
  336. - (NSArray *)videoTrackNames
  337. {
  338. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  339. if (count <= 0)
  340. return @[];
  341. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  342. libvlc_track_description_t *currentTrack = firstTrack;
  343. NSMutableArray *tempArray = [NSMutableArray array];
  344. while (currentTrack) {
  345. [tempArray addObject:@(currentTrack->psz_name)];
  346. currentTrack = currentTrack->p_next;
  347. }
  348. libvlc_track_description_list_release(firstTrack);
  349. return [NSArray arrayWithArray: tempArray];
  350. }
  351. - (NSArray *)videoTrackIndexes
  352. {
  353. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  354. if (count <= 0)
  355. return @[];
  356. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  357. libvlc_track_description_t *currentTrack = firstTrack;
  358. NSMutableArray *tempArray = [NSMutableArray array];
  359. while (currentTrack) {
  360. [tempArray addObject:@(currentTrack->i_id)];
  361. currentTrack = currentTrack->p_next;
  362. }
  363. libvlc_track_description_list_release(firstTrack);
  364. return [NSArray arrayWithArray: tempArray];
  365. }
  366. - (int)numberOfVideoTracks
  367. {
  368. return libvlc_video_get_track_count(_playerInstance);
  369. }
  370. #pragma mark -
  371. #pragma mark Subtitles
  372. - (void)setCurrentVideoSubTitleIndex:(int)index
  373. {
  374. libvlc_video_set_spu(_playerInstance, index);
  375. }
  376. - (int)currentVideoSubTitleIndex
  377. {
  378. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  379. if (count <= 0)
  380. return -1;
  381. return libvlc_video_get_spu(_playerInstance);
  382. }
  383. - (NSArray *)videoSubTitlesNames
  384. {
  385. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  386. if (count <= 0)
  387. return @[];
  388. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  389. libvlc_track_description_t *currentTrack = firstTrack;
  390. NSMutableArray *tempArray = [NSMutableArray array];
  391. while (currentTrack) {
  392. NSString *track = @(currentTrack->psz_name);
  393. [tempArray addObject:track != nil ? track : @""];
  394. currentTrack = currentTrack->p_next;
  395. }
  396. libvlc_track_description_list_release(firstTrack);
  397. return [NSArray arrayWithArray: tempArray];
  398. }
  399. - (NSArray *)videoSubTitlesIndexes
  400. {
  401. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  402. if (count <= 0)
  403. return @[];
  404. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  405. libvlc_track_description_t *currentTrack = firstTrack;
  406. NSMutableArray *tempArray = [NSMutableArray array];
  407. while (currentTrack) {
  408. [tempArray addObject:@(currentTrack->i_id)];
  409. currentTrack = currentTrack->p_next;
  410. }
  411. libvlc_track_description_list_release(firstTrack);
  412. return [NSArray arrayWithArray: tempArray];
  413. }
  414. - (int)numberOfSubtitlesTracks
  415. {
  416. return libvlc_video_get_spu_count(_playerInstance);
  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. (libvlc_media_slave_type_t)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)setCropRatioWithNumerator:(unsigned int)numerator denominator:(unsigned int)denominator
  456. {
  457. libvlc_video_set_crop_ratio(_playerInstance, numerator, denominator);
  458. }
  459. - (void)setVideoAspectRatio:(char *)value
  460. {
  461. libvlc_video_set_aspect_ratio(_playerInstance, value);
  462. }
  463. - (char *)videoAspectRatio
  464. {
  465. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  466. return result;
  467. }
  468. - (void)setScaleFactor:(float)value
  469. {
  470. libvlc_video_set_scale(_playerInstance, value);
  471. }
  472. - (float)scaleFactor
  473. {
  474. return libvlc_video_get_scale(_playerInstance);
  475. }
  476. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  477. {
  478. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  479. if (failure)
  480. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  481. }
  482. - (void)setDeinterlaceFilter:(NSString *)name
  483. {
  484. if (!name || name.length < 1)
  485. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOff, NULL);
  486. else
  487. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOn, [name UTF8String]);
  488. }
  489. - (void)setDeinterlace:(VLCDeinterlace)deinterlace withFilter:(NSString *)name
  490. {
  491. libvlc_video_set_deinterlace(_playerInstance, (int)deinterlace, [name UTF8String]);
  492. }
  493. - (BOOL)adjustFilterEnabled
  494. {
  495. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  496. }
  497. - (void)setAdjustFilterEnabled:(BOOL)b_value
  498. {
  499. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  500. }
  501. - (float)contrast
  502. {
  503. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  504. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  505. }
  506. - (void)setContrast:(float)f_value
  507. {
  508. if (f_value <= 2. && f_value >= 0.) {
  509. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  510. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  511. }
  512. }
  513. - (float)brightness
  514. {
  515. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  516. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  517. }
  518. - (void)setBrightness:(float)f_value
  519. {
  520. if (f_value <= 2. && f_value >= 0.) {
  521. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  522. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  523. }
  524. }
  525. - (float)hue
  526. {
  527. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  528. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  529. }
  530. - (void)setHue:(float)f_value
  531. {
  532. if (f_value <= 180. && f_value >= -180.) {
  533. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  534. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  535. }
  536. }
  537. - (float)saturation
  538. {
  539. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  540. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  541. }
  542. - (void)setSaturation:(float)f_value
  543. {
  544. if (f_value <= 3. && f_value >= 0.) {
  545. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  546. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  547. }
  548. }
  549. - (float)gamma
  550. {
  551. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  552. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  553. }
  554. - (void)setGamma:(float)f_value
  555. {
  556. if (f_value <= 10. && f_value >= 0.) {
  557. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  558. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  559. }
  560. }
  561. - (void)setRate:(float)value
  562. {
  563. libvlc_media_player_set_rate(_playerInstance, value);
  564. }
  565. - (float)rate
  566. {
  567. return libvlc_media_player_get_rate(_playerInstance);
  568. }
  569. - (CGSize)videoSize
  570. {
  571. unsigned height = 0, width = 0;
  572. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  573. if (failure)
  574. return CGSizeZero;
  575. return CGSizeMake(width, height);
  576. }
  577. - (BOOL)hasVideoOut
  578. {
  579. return libvlc_media_player_has_vout(_playerInstance);
  580. }
  581. - (void)setTime:(VLCTime *)value
  582. {
  583. // Time is managed in seconds, while duration is managed in microseconds
  584. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  585. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0, NO);
  586. }
  587. - (VLCTime *)time
  588. {
  589. return _cachedTime;
  590. }
  591. - (VLCTime *)remainingTime
  592. {
  593. return _cachedRemainingTime;
  594. }
  595. #pragma mark -
  596. #pragma mark Chapters
  597. - (void)setCurrentChapterIndex:(int)value;
  598. {
  599. libvlc_media_player_set_chapter(_playerInstance, value);
  600. }
  601. - (int)currentChapterIndex
  602. {
  603. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  604. if (count <= 0)
  605. return -1;
  606. int result = libvlc_media_player_get_chapter(_playerInstance);
  607. return result;
  608. }
  609. - (void)nextChapter
  610. {
  611. libvlc_media_player_next_chapter(_playerInstance);
  612. }
  613. - (void)previousChapter
  614. {
  615. libvlc_media_player_previous_chapter(_playerInstance);
  616. }
  617. #pragma mark -
  618. #pragma mark Titles
  619. - (void)setCurrentTitleIndex:(int)value
  620. {
  621. libvlc_media_player_set_title(_playerInstance, value);
  622. }
  623. - (int)currentTitleIndex
  624. {
  625. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  626. if (count <= 0)
  627. return -1;
  628. return libvlc_media_player_get_title(_playerInstance);
  629. }
  630. - (int)numberOfTitles
  631. {
  632. return libvlc_media_player_get_title_count(_playerInstance);
  633. }
  634. - (NSArray *)titleDescriptions
  635. {
  636. libvlc_title_description_t **titleInfo;
  637. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  638. if (numberOfTitleDescriptions < 0)
  639. return [NSArray array];
  640. if (numberOfTitleDescriptions == 0) {
  641. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  642. return [NSArray array];
  643. }
  644. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  645. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  646. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  647. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  648. VLCTitleDescriptionDuration,
  649. @(titleInfo[i]->i_flags & libvlc_title_menu),
  650. VLCTitleDescriptionIsMenu,
  651. nil];
  652. if (titleInfo[i]->psz_name != NULL)
  653. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  654. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  655. }
  656. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  657. return [NSArray arrayWithArray:array];
  658. }
  659. - (int)indexOfLongestTitle
  660. {
  661. NSArray *titles = [self titleDescriptions];
  662. NSUInteger titleCount = titles.count;
  663. int currentlyFoundTitle = 0;
  664. int64_t currentlySelectedDuration = 0;
  665. int64_t randomTitleDuration = 0;
  666. for (int x = 0; x < titleCount; x++) {
  667. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  668. if (randomTitleDuration > currentlySelectedDuration) {
  669. currentlySelectedDuration = randomTitleDuration;
  670. currentlyFoundTitle = x;
  671. }
  672. }
  673. return currentlyFoundTitle;
  674. }
  675. - (int)numberOfChaptersForTitle:(int)titleIndex
  676. {
  677. if (titleIndex >= 0) {
  678. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  679. }
  680. return 0;
  681. }
  682. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  683. {
  684. libvlc_chapter_description_t **chapterDescriptions;
  685. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  686. titleIndex,
  687. &chapterDescriptions);
  688. if (numberOfChapterDescriptions < 0)
  689. return [NSArray array];
  690. if (numberOfChapterDescriptions == 0) {
  691. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  692. return [NSArray array];
  693. }
  694. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  695. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  696. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  697. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  698. VLCChapterDescriptionDuration,
  699. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  700. VLCChapterDescriptionTimeOffset,
  701. nil];
  702. if (chapterDescriptions[i]->psz_name != NULL)
  703. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  704. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  705. }
  706. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  707. return [NSArray arrayWithArray:array];
  708. }
  709. #pragma mark -
  710. #pragma mark Audio tracks
  711. - (void)setCurrentAudioTrackIndex:(int)value
  712. {
  713. libvlc_audio_set_track(_playerInstance, value);
  714. }
  715. - (int)currentAudioTrackIndex
  716. {
  717. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  718. if (count <= 0)
  719. return -1;
  720. return libvlc_audio_get_track(_playerInstance);
  721. }
  722. - (NSArray *)audioTrackNames
  723. {
  724. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  725. if (count <= 0)
  726. return @[];
  727. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  728. libvlc_track_description_t *currentTrack = firstTrack;
  729. NSMutableArray *tempArray = [NSMutableArray array];
  730. while (currentTrack) {
  731. NSString *track = @(currentTrack->psz_name);
  732. [tempArray addObject:track != nil ? track : @""];
  733. currentTrack = currentTrack->p_next;
  734. }
  735. libvlc_track_description_list_release(firstTrack);
  736. return [NSArray arrayWithArray: tempArray];
  737. }
  738. - (NSArray *)audioTrackIndexes
  739. {
  740. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  741. if (count <= 0)
  742. return @[];
  743. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  744. libvlc_track_description_t *currentTrack = firstTrack;
  745. NSMutableArray *tempArray = [NSMutableArray array];
  746. while (currentTrack) {
  747. [tempArray addObject:@(currentTrack->i_id)];
  748. currentTrack = currentTrack->p_next;
  749. }
  750. libvlc_track_description_list_release(firstTrack);
  751. return [NSArray arrayWithArray: tempArray];
  752. }
  753. - (int)numberOfAudioTracks
  754. {
  755. return libvlc_audio_get_track_count(_playerInstance);
  756. }
  757. - (void)setAudioChannel:(int)value
  758. {
  759. libvlc_audio_set_channel(_playerInstance, value);
  760. }
  761. - (int)audioChannel
  762. {
  763. return libvlc_audio_get_channel(_playerInstance);
  764. }
  765. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  766. {
  767. libvlc_audio_set_delay(_playerInstance, index);
  768. }
  769. - (NSInteger)currentAudioPlaybackDelay
  770. {
  771. return libvlc_audio_get_delay(_playerInstance);
  772. }
  773. #pragma mark -
  774. #pragma mark equalizer
  775. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  776. {
  777. if (!_equalizerInstance && equalizerEnabled) {
  778. if (!(_equalizerInstance = libvlc_audio_equalizer_new())) {
  779. NSAssert(_equalizerInstance, @"equalizer failed to initialize");
  780. return;
  781. }
  782. }
  783. _equalizerEnabled = equalizerEnabled;
  784. libvlc_media_player_set_equalizer(_playerInstance,
  785. equalizerEnabled ? _equalizerInstance : NULL);
  786. }
  787. - (BOOL)equalizerEnabled
  788. {
  789. return _equalizerEnabled;
  790. }
  791. - (NSArray *)equalizerProfiles
  792. {
  793. unsigned count = libvlc_audio_equalizer_get_preset_count();
  794. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  795. for (unsigned x = 0; x < count; x++)
  796. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  797. return [NSArray arrayWithArray:array];
  798. }
  799. - (void)resetEqualizerFromProfile:(unsigned)profile
  800. {
  801. BOOL wasactive = NO;
  802. if (_equalizerInstance) {
  803. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  804. libvlc_audio_equalizer_release(_equalizerInstance);
  805. _equalizerInstance = nil;
  806. wasactive = YES;
  807. }
  808. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  809. if (wasactive)
  810. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  811. }
  812. - (CGFloat)preAmplification
  813. {
  814. if (!_equalizerInstance)
  815. return 0.;
  816. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  817. }
  818. - (void)setPreAmplification:(CGFloat)preAmplification
  819. {
  820. if (!_equalizerInstance)
  821. _equalizerInstance = libvlc_audio_equalizer_new();
  822. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  823. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  824. }
  825. - (unsigned)numberOfBands
  826. {
  827. return libvlc_audio_equalizer_get_band_count();
  828. }
  829. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  830. {
  831. return libvlc_audio_equalizer_get_band_frequency(index);
  832. }
  833. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  834. {
  835. if (!_equalizerInstance)
  836. _equalizerInstance = libvlc_audio_equalizer_new();
  837. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  838. }
  839. - (CGFloat)amplificationOfBand:(unsigned int)index
  840. {
  841. if (!_equalizerInstance)
  842. return 0.;
  843. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  844. }
  845. #pragma mark -
  846. #pragma mark set/get media
  847. - (void)setMedia:(VLCMedia *)value
  848. {
  849. if (_media != value) {
  850. if (_media && [_media compare:value] == NSOrderedSame)
  851. return;
  852. _media = value;
  853. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  854. }
  855. }
  856. - (VLCMedia *)media
  857. {
  858. return _media;
  859. }
  860. #pragma mark -
  861. #pragma mark playback
  862. - (void)play
  863. {
  864. dispatch_async(_libVLCBackgroundQueue, ^{
  865. libvlc_media_player_play(_playerInstance);
  866. });
  867. }
  868. - (void)pause
  869. {
  870. // Pause the stream
  871. dispatch_async(_libVLCBackgroundQueue, ^{
  872. libvlc_media_player_set_pause(_playerInstance, 1);
  873. });
  874. }
  875. - (void)stop
  876. {
  877. dispatch_async(_libVLCBackgroundQueue, ^{
  878. libvlc_media_player_stop_async(_playerInstance);
  879. });
  880. }
  881. - (libvlc_video_viewpoint_t *)viewPoint
  882. {
  883. if (_viewpoint == NULL) {
  884. _viewpoint = libvlc_video_new_viewpoint();
  885. }
  886. return _viewpoint;
  887. }
  888. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  889. {
  890. if ([self viewPoint]) {
  891. [self viewPoint]->f_yaw = yaw;
  892. [self viewPoint]->f_pitch = pitch;
  893. [self viewPoint]->f_roll = roll;
  894. [self viewPoint]->f_field_of_view = fov;
  895. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  896. }
  897. return NO;
  898. }
  899. - (float)yaw
  900. {
  901. if ([self viewPoint]) {
  902. return [self viewPoint]->f_yaw;
  903. }
  904. return 0;
  905. }
  906. - (float)pitch
  907. {
  908. if ([self viewPoint]) {
  909. return [self viewPoint]->f_pitch;
  910. }
  911. return 0;
  912. }
  913. - (float)roll
  914. {
  915. if ([self viewPoint]) {
  916. return [self viewPoint]->f_roll;
  917. }
  918. return 0;
  919. }
  920. - (float)fov
  921. {
  922. if ([self viewPoint]) {
  923. return [self viewPoint]->f_field_of_view;
  924. }
  925. return 0;
  926. }
  927. - (void)gotoNextFrame
  928. {
  929. libvlc_media_player_next_frame(_playerInstance);
  930. }
  931. - (void)fastForward
  932. {
  933. [self fastForwardAtRate: 2.0];
  934. }
  935. - (void)fastForwardAtRate:(float)rate
  936. {
  937. [self setRate:rate];
  938. }
  939. - (void)rewind
  940. {
  941. [self rewindAtRate: 2.0];
  942. }
  943. - (void)rewindAtRate:(float)rate
  944. {
  945. [self setRate: -rate];
  946. }
  947. - (void)jumpBackward:(int)interval
  948. {
  949. if ([self isSeekable]) {
  950. interval = interval * 1000;
  951. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  952. }
  953. }
  954. - (void)jumpForward:(int)interval
  955. {
  956. if ([self isSeekable]) {
  957. interval = interval * 1000;
  958. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  959. }
  960. }
  961. - (void)extraShortJumpBackward
  962. {
  963. [self jumpBackward:3];
  964. }
  965. - (void)extraShortJumpForward
  966. {
  967. [self jumpForward:3];
  968. }
  969. - (void)shortJumpBackward
  970. {
  971. [self jumpBackward:10];
  972. }
  973. - (void)shortJumpForward
  974. {
  975. [self jumpForward:10];
  976. }
  977. - (void)mediumJumpBackward
  978. {
  979. [self jumpBackward:60];
  980. }
  981. - (void)mediumJumpForward
  982. {
  983. [self jumpForward:60];
  984. }
  985. - (void)longJumpBackward
  986. {
  987. [self jumpBackward:300];
  988. }
  989. - (void)longJumpForward
  990. {
  991. [self jumpForward:300];
  992. }
  993. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  994. {
  995. libvlc_media_player_navigate(_playerInstance, action);
  996. }
  997. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  998. {
  999. return [NSSet setWithObjects:@"state", nil];
  1000. }
  1001. - (BOOL)isPlaying
  1002. {
  1003. return libvlc_media_player_is_playing(_playerInstance);
  1004. }
  1005. - (VLCMediaPlayerState)state
  1006. {
  1007. return _cachedState;
  1008. }
  1009. - (float)position
  1010. {
  1011. return _position;
  1012. }
  1013. - (void)setPosition:(float)newPosition
  1014. {
  1015. libvlc_media_player_set_position(_playerInstance, newPosition, NO);
  1016. }
  1017. - (BOOL)isSeekable
  1018. {
  1019. return libvlc_media_player_is_seekable(_playerInstance);
  1020. }
  1021. - (BOOL)canPause
  1022. {
  1023. return libvlc_media_player_can_pause(_playerInstance);
  1024. }
  1025. - (NSArray *)snapshots
  1026. {
  1027. return [_snapshots copy];
  1028. }
  1029. #if TARGET_OS_IPHONE
  1030. - (UIImage *)lastSnapshot {
  1031. if (_snapshots == nil) {
  1032. return nil;
  1033. }
  1034. @synchronized(_snapshots) {
  1035. if (_snapshots.count == 0)
  1036. return nil;
  1037. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1038. }
  1039. }
  1040. #else
  1041. - (NSImage *)lastSnapshot {
  1042. if (_snapshots == nil) {
  1043. return nil;
  1044. }
  1045. @synchronized(_snapshots) {
  1046. if (_snapshots.count == 0)
  1047. return nil;
  1048. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1049. }
  1050. }
  1051. #endif
  1052. - (void *)libVLCMediaPlayer
  1053. {
  1054. return _playerInstance;
  1055. }
  1056. - (BOOL)startRecordingAtPath:(NSString *)path
  1057. {
  1058. return libvlc_media_player_record(_playerInstance, YES, [path UTF8String]);
  1059. }
  1060. - (BOOL)stopRecording
  1061. {
  1062. return libvlc_media_player_record(_playerInstance, NO, nil);
  1063. }
  1064. #pragma mark -
  1065. #pragma mark - Renderer
  1066. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1067. {
  1068. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1069. }
  1070. @end
  1071. @implementation VLCMediaPlayer (Private)
  1072. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1073. {
  1074. if (self = [super init]) {
  1075. _cachedTime = [VLCTime nullTime];
  1076. _cachedRemainingTime = [VLCTime nullTime];
  1077. _position = 0.0f;
  1078. _cachedState = VLCMediaPlayerStateStopped;
  1079. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1080. // Create a media instance, it doesn't matter what library we start off with
  1081. // it will change depending on the media descriptor provided to the media
  1082. // instance
  1083. if (options && options.count > 0) {
  1084. VKLog(@"creating player instance with private library as options were given");
  1085. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1086. } else {
  1087. VKLog(@"creating player instance using shared library");
  1088. _privateLibrary = [VLCLibrary sharedLibrary];
  1089. }
  1090. libvlc_retain([_privateLibrary instance]);
  1091. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1092. if (_playerInstance == NULL) {
  1093. NSAssert(0, @"%s: player initialization failed", __PRETTY_FUNCTION__);
  1094. libvlc_release([_privateLibrary instance]);
  1095. return nil;
  1096. }
  1097. [self registerObservers];
  1098. [self setDrawable:aDrawable];
  1099. }
  1100. return self;
  1101. }
  1102. - (void)registerObservers
  1103. {
  1104. // Attach event observers into the media instance
  1105. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1106. if (!p_em)
  1107. return;
  1108. /* We need the caller to wait until this block is done.
  1109. * The initialized object shall not be returned until the event attachments are done. */
  1110. dispatch_sync(_libVLCBackgroundQueue,^{
  1111. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1112. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1113. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1114. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1115. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1116. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1117. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1118. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1119. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1120. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1121. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1122. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1123. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1124. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1125. libvlc_event_attach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1126. });
  1127. }
  1128. - (void)unregisterObservers
  1129. {
  1130. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1131. if (!p_em)
  1132. return;
  1133. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1134. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1135. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1136. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1137. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1138. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1139. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1140. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1141. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1142. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1143. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1144. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1145. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1146. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1147. libvlc_event_detach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1148. }
  1149. - (dispatch_queue_t)libVLCBackgroundQueue
  1150. {
  1151. if (!_libVLCBackgroundQueue) {
  1152. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1153. }
  1154. return _libVLCBackgroundQueue;
  1155. }
  1156. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1157. {
  1158. [self willChangeValueForKey:@"time"];
  1159. [self willChangeValueForKey:@"remainingTime"];
  1160. _cachedTime = [VLCTime timeWithNumber:newTime];
  1161. double currentTime = [[_cachedTime value] doubleValue];
  1162. if (currentTime > 0 && _position > 0.) {
  1163. double remaining = currentTime / _position * (1 - _position);
  1164. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1165. } else
  1166. _cachedRemainingTime = [VLCTime nullTime];
  1167. [self didChangeValueForKey:@"remainingTime"];
  1168. [self didChangeValueForKey:@"time"];
  1169. }
  1170. #if !TARGET_OS_IPHONE
  1171. - (void)delaySleep
  1172. {
  1173. UpdateSystemActivity(UsrActivity);
  1174. }
  1175. #endif
  1176. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1177. {
  1178. #if !TARGET_OS_IPHONE
  1179. // This seems to be the most relevant place to delay sleeping and screen saver.
  1180. [self delaySleep];
  1181. #endif
  1182. [self willChangeValueForKey:@"position"];
  1183. _position = [newPosition floatValue];
  1184. [self didChangeValueForKey:@"position"];
  1185. }
  1186. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1187. {
  1188. [self willChangeValueForKey:@"state"];
  1189. _cachedState = [newState intValue];
  1190. #if TARGET_OS_IPHONE
  1191. // Disable idle timer if player is playing media
  1192. // Exclusion can be made for audio only media
  1193. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1194. #endif
  1195. [self didChangeValueForKey:@"state"];
  1196. }
  1197. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1198. {
  1199. [self willChangeValueForKey:@"media"];
  1200. if (_media != newMedia) {
  1201. _media = newMedia;
  1202. [self willChangeValueForKey:@"time"];
  1203. [self willChangeValueForKey:@"remainingTime"];
  1204. [self willChangeValueForKey:@"position"];
  1205. _cachedTime = [VLCTime nullTime];
  1206. _cachedRemainingTime = [VLCTime nullTime];
  1207. _position = 0.0f;
  1208. [self didChangeValueForKey:@"position"];
  1209. [self didChangeValueForKey:@"remainingTime"];
  1210. [self didChangeValueForKey:@"time"];
  1211. }
  1212. [self didChangeValueForKey:@"media"];
  1213. }
  1214. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1215. {
  1216. [self willChangeValueForKey:@"currentTitleIndex"];
  1217. [self didChangeValueForKey:@"currentTitleIndex"];
  1218. }
  1219. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1220. {
  1221. [self willChangeValueForKey:@"currentChapterIndex"];
  1222. [self didChangeValueForKey:@"currentChapterIndex"];
  1223. }
  1224. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1225. {
  1226. @synchronized(_snapshots) {
  1227. if (!_snapshots) {
  1228. _snapshots = [NSMutableArray array];
  1229. }
  1230. [_snapshots addObject:fileName];
  1231. }
  1232. }
  1233. - (void)mediaPlayerRecordChanged:(NSArray *)arguments
  1234. {
  1235. NSString *filePath = arguments.firstObject[@"filePath"];
  1236. BOOL isRecording = [arguments.firstObject[@"isRecording"] boolValue];
  1237. if (isRecording) {
  1238. if ([_delegate respondsToSelector:@selector(mediaPlayerStartedRecording:)]) {
  1239. [_delegate mediaPlayerStartedRecording:self];
  1240. }
  1241. } else {
  1242. if ([_delegate respondsToSelector:@selector(mediaPlayer:recordingStoppedAtPath:)]) {
  1243. [_delegate mediaPlayer:self recordingStoppedAtPath:filePath];
  1244. }
  1245. }
  1246. }
  1247. @end