VLCMediaPlayer.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. 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, 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. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  678. }
  679. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  680. {
  681. libvlc_chapter_description_t **chapterDescriptions;
  682. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  683. titleIndex,
  684. &chapterDescriptions);
  685. if (numberOfChapterDescriptions < 0)
  686. return [NSArray array];
  687. if (numberOfChapterDescriptions == 0) {
  688. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  689. return [NSArray array];
  690. }
  691. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  692. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  693. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  694. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  695. VLCChapterDescriptionDuration,
  696. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  697. VLCChapterDescriptionTimeOffset,
  698. nil];
  699. if (chapterDescriptions[i]->psz_name != NULL)
  700. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  701. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  702. }
  703. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  704. return [NSArray arrayWithArray:array];
  705. }
  706. #pragma mark -
  707. #pragma mark Audio tracks
  708. - (void)setCurrentAudioTrackIndex:(int)value
  709. {
  710. libvlc_audio_set_track(_playerInstance, value);
  711. }
  712. - (int)currentAudioTrackIndex
  713. {
  714. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  715. if (count <= 0)
  716. return -1;
  717. return libvlc_audio_get_track(_playerInstance);
  718. }
  719. - (NSArray *)audioTrackNames
  720. {
  721. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  722. if (count <= 0)
  723. return @[];
  724. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  725. libvlc_track_description_t *currentTrack = firstTrack;
  726. NSMutableArray *tempArray = [NSMutableArray array];
  727. while (currentTrack) {
  728. NSString *track = @(currentTrack->psz_name);
  729. [tempArray addObject:track != nil ? track : @""];
  730. currentTrack = currentTrack->p_next;
  731. }
  732. libvlc_track_description_list_release(firstTrack);
  733. return [NSArray arrayWithArray: tempArray];
  734. }
  735. - (NSArray *)audioTrackIndexes
  736. {
  737. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  738. if (count <= 0)
  739. return @[];
  740. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  741. libvlc_track_description_t *currentTrack = firstTrack;
  742. NSMutableArray *tempArray = [NSMutableArray array];
  743. while (currentTrack) {
  744. [tempArray addObject:@(currentTrack->i_id)];
  745. currentTrack = currentTrack->p_next;
  746. }
  747. libvlc_track_description_list_release(firstTrack);
  748. return [NSArray arrayWithArray: tempArray];
  749. }
  750. - (int)numberOfAudioTracks
  751. {
  752. return libvlc_audio_get_track_count(_playerInstance);
  753. }
  754. - (void)setAudioChannel:(int)value
  755. {
  756. libvlc_audio_set_channel(_playerInstance, value);
  757. }
  758. - (int)audioChannel
  759. {
  760. return libvlc_audio_get_channel(_playerInstance);
  761. }
  762. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  763. {
  764. libvlc_audio_set_delay(_playerInstance, index);
  765. }
  766. - (NSInteger)currentAudioPlaybackDelay
  767. {
  768. return libvlc_audio_get_delay(_playerInstance);
  769. }
  770. #pragma mark -
  771. #pragma mark equalizer
  772. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  773. {
  774. if (!_equalizerInstance && equalizerEnabled) {
  775. if (!(_equalizerInstance = libvlc_audio_equalizer_new())) {
  776. NSAssert(_equalizerInstance, @"equalizer failed to initialize");
  777. return;
  778. }
  779. }
  780. _equalizerEnabled = equalizerEnabled;
  781. libvlc_media_player_set_equalizer(_playerInstance,
  782. equalizerEnabled ? _equalizerInstance : NULL);
  783. }
  784. - (BOOL)equalizerEnabled
  785. {
  786. return _equalizerEnabled;
  787. }
  788. - (NSArray *)equalizerProfiles
  789. {
  790. unsigned count = libvlc_audio_equalizer_get_preset_count();
  791. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  792. for (unsigned x = 0; x < count; x++)
  793. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  794. return [NSArray arrayWithArray:array];
  795. }
  796. - (void)resetEqualizerFromProfile:(unsigned)profile
  797. {
  798. BOOL wasactive = NO;
  799. if (_equalizerInstance) {
  800. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  801. libvlc_audio_equalizer_release(_equalizerInstance);
  802. _equalizerInstance = nil;
  803. wasactive = YES;
  804. }
  805. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  806. if (wasactive)
  807. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  808. }
  809. - (CGFloat)preAmplification
  810. {
  811. if (!_equalizerInstance)
  812. return 0.;
  813. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  814. }
  815. - (void)setPreAmplification:(CGFloat)preAmplification
  816. {
  817. if (!_equalizerInstance)
  818. _equalizerInstance = libvlc_audio_equalizer_new();
  819. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  820. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  821. }
  822. - (unsigned)numberOfBands
  823. {
  824. return libvlc_audio_equalizer_get_band_count();
  825. }
  826. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  827. {
  828. return libvlc_audio_equalizer_get_band_frequency(index);
  829. }
  830. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  831. {
  832. if (!_equalizerInstance)
  833. _equalizerInstance = libvlc_audio_equalizer_new();
  834. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  835. }
  836. - (CGFloat)amplificationOfBand:(unsigned int)index
  837. {
  838. if (!_equalizerInstance)
  839. return 0.;
  840. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  841. }
  842. #pragma mark -
  843. #pragma mark set/get media
  844. - (void)setMedia:(VLCMedia *)value
  845. {
  846. if (_media != value) {
  847. if (_media && [_media compare:value] == NSOrderedSame)
  848. return;
  849. _media = value;
  850. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  851. }
  852. }
  853. - (VLCMedia *)media
  854. {
  855. return _media;
  856. }
  857. #pragma mark -
  858. #pragma mark playback
  859. - (void)play
  860. {
  861. dispatch_async(_libVLCBackgroundQueue, ^{
  862. libvlc_media_player_play(_playerInstance);
  863. });
  864. }
  865. - (void)pause
  866. {
  867. // Pause the stream
  868. dispatch_async(_libVLCBackgroundQueue, ^{
  869. libvlc_media_player_set_pause(_playerInstance, 1);
  870. });
  871. }
  872. - (void)stop
  873. {
  874. dispatch_async(_libVLCBackgroundQueue, ^{
  875. libvlc_media_player_stop_async(_playerInstance);
  876. });
  877. }
  878. - (libvlc_video_viewpoint_t *)viewPoint
  879. {
  880. if (_viewpoint == NULL) {
  881. _viewpoint = libvlc_video_new_viewpoint();
  882. }
  883. return _viewpoint;
  884. }
  885. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  886. {
  887. if ([self viewPoint]) {
  888. [self viewPoint]->f_yaw = yaw;
  889. [self viewPoint]->f_pitch = pitch;
  890. [self viewPoint]->f_roll = roll;
  891. [self viewPoint]->f_field_of_view = fov;
  892. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  893. }
  894. return NO;
  895. }
  896. - (float)yaw
  897. {
  898. if ([self viewPoint]) {
  899. return [self viewPoint]->f_yaw;
  900. }
  901. return 0;
  902. }
  903. - (float)pitch
  904. {
  905. if ([self viewPoint]) {
  906. return [self viewPoint]->f_pitch;
  907. }
  908. return 0;
  909. }
  910. - (float)roll
  911. {
  912. if ([self viewPoint]) {
  913. return [self viewPoint]->f_roll;
  914. }
  915. return 0;
  916. }
  917. - (float)fov
  918. {
  919. if ([self viewPoint]) {
  920. return [self viewPoint]->f_field_of_view;
  921. }
  922. return 0;
  923. }
  924. - (void)gotoNextFrame
  925. {
  926. libvlc_media_player_next_frame(_playerInstance);
  927. }
  928. - (void)fastForward
  929. {
  930. [self fastForwardAtRate: 2.0];
  931. }
  932. - (void)fastForwardAtRate:(float)rate
  933. {
  934. [self setRate:rate];
  935. }
  936. - (void)rewind
  937. {
  938. [self rewindAtRate: 2.0];
  939. }
  940. - (void)rewindAtRate:(float)rate
  941. {
  942. [self setRate: -rate];
  943. }
  944. - (void)jumpBackward:(int)interval
  945. {
  946. if ([self isSeekable]) {
  947. interval = interval * 1000;
  948. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  949. }
  950. }
  951. - (void)jumpForward:(int)interval
  952. {
  953. if ([self isSeekable]) {
  954. interval = interval * 1000;
  955. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  956. }
  957. }
  958. - (void)extraShortJumpBackward
  959. {
  960. [self jumpBackward:3];
  961. }
  962. - (void)extraShortJumpForward
  963. {
  964. [self jumpForward:3];
  965. }
  966. - (void)shortJumpBackward
  967. {
  968. [self jumpBackward:10];
  969. }
  970. - (void)shortJumpForward
  971. {
  972. [self jumpForward:10];
  973. }
  974. - (void)mediumJumpBackward
  975. {
  976. [self jumpBackward:60];
  977. }
  978. - (void)mediumJumpForward
  979. {
  980. [self jumpForward:60];
  981. }
  982. - (void)longJumpBackward
  983. {
  984. [self jumpBackward:300];
  985. }
  986. - (void)longJumpForward
  987. {
  988. [self jumpForward:300];
  989. }
  990. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  991. {
  992. libvlc_media_player_navigate(_playerInstance, action);
  993. }
  994. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  995. {
  996. return [NSSet setWithObjects:@"state", nil];
  997. }
  998. - (BOOL)isPlaying
  999. {
  1000. return libvlc_media_player_is_playing(_playerInstance);
  1001. }
  1002. - (VLCMediaPlayerState)state
  1003. {
  1004. return _cachedState;
  1005. }
  1006. - (float)position
  1007. {
  1008. return _position;
  1009. }
  1010. - (void)setPosition:(float)newPosition
  1011. {
  1012. libvlc_media_player_set_position(_playerInstance, newPosition, NO);
  1013. }
  1014. - (BOOL)isSeekable
  1015. {
  1016. return libvlc_media_player_is_seekable(_playerInstance);
  1017. }
  1018. - (BOOL)canPause
  1019. {
  1020. return libvlc_media_player_can_pause(_playerInstance);
  1021. }
  1022. - (NSArray *)snapshots
  1023. {
  1024. return [_snapshots copy];
  1025. }
  1026. #if TARGET_OS_IPHONE
  1027. - (UIImage *)lastSnapshot {
  1028. if (_snapshots == nil) {
  1029. return nil;
  1030. }
  1031. @synchronized(_snapshots) {
  1032. if (_snapshots.count == 0)
  1033. return nil;
  1034. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1035. }
  1036. }
  1037. #else
  1038. - (NSImage *)lastSnapshot {
  1039. if (_snapshots == nil) {
  1040. return nil;
  1041. }
  1042. @synchronized(_snapshots) {
  1043. if (_snapshots.count == 0)
  1044. return nil;
  1045. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1046. }
  1047. }
  1048. #endif
  1049. - (void *)libVLCMediaPlayer
  1050. {
  1051. return _playerInstance;
  1052. }
  1053. - (BOOL)startRecordingAtPath:(NSString *)path
  1054. {
  1055. return libvlc_media_player_record(_playerInstance, YES, [path UTF8String]);
  1056. }
  1057. - (BOOL)stopRecording
  1058. {
  1059. return libvlc_media_player_record(_playerInstance, NO, nil);
  1060. }
  1061. #pragma mark -
  1062. #pragma mark - Renderer
  1063. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1064. {
  1065. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1066. }
  1067. @end
  1068. @implementation VLCMediaPlayer (Private)
  1069. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1070. {
  1071. if (self = [super init]) {
  1072. _cachedTime = [VLCTime nullTime];
  1073. _cachedRemainingTime = [VLCTime nullTime];
  1074. _position = 0.0f;
  1075. _cachedState = VLCMediaPlayerStateStopped;
  1076. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1077. // Create a media instance, it doesn't matter what library we start off with
  1078. // it will change depending on the media descriptor provided to the media
  1079. // instance
  1080. if (options && options.count > 0) {
  1081. VKLog(@"creating player instance with private library as options were given");
  1082. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1083. } else {
  1084. VKLog(@"creating player instance using shared library");
  1085. _privateLibrary = [VLCLibrary sharedLibrary];
  1086. }
  1087. libvlc_retain([_privateLibrary instance]);
  1088. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1089. if (_playerInstance == NULL) {
  1090. NSAssert(0, @"%s: player initialization failed", __PRETTY_FUNCTION__);
  1091. libvlc_release([_privateLibrary instance]);
  1092. return nil;
  1093. }
  1094. [self registerObservers];
  1095. [self setDrawable:aDrawable];
  1096. }
  1097. return self;
  1098. }
  1099. - (void)registerObservers
  1100. {
  1101. // Attach event observers into the media instance
  1102. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1103. if (!p_em)
  1104. return;
  1105. /* We need the caller to wait until this block is done.
  1106. * The initialized object shall not be returned until the event attachments are done. */
  1107. dispatch_sync(_libVLCBackgroundQueue,^{
  1108. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1109. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1110. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1111. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1112. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1113. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1114. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1115. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1116. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1117. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1118. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1119. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1120. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1121. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1122. libvlc_event_attach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1123. });
  1124. }
  1125. - (void)unregisterObservers
  1126. {
  1127. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1128. if (!p_em)
  1129. return;
  1130. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1131. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1132. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1133. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1134. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1135. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1136. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1137. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1138. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1139. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1140. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1141. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1142. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1143. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1144. libvlc_event_detach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1145. }
  1146. - (dispatch_queue_t)libVLCBackgroundQueue
  1147. {
  1148. if (!_libVLCBackgroundQueue) {
  1149. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1150. }
  1151. return _libVLCBackgroundQueue;
  1152. }
  1153. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1154. {
  1155. [self willChangeValueForKey:@"time"];
  1156. [self willChangeValueForKey:@"remainingTime"];
  1157. _cachedTime = [VLCTime timeWithNumber:newTime];
  1158. double currentTime = [[_cachedTime numberValue] doubleValue];
  1159. if (currentTime > 0 && _position > 0.) {
  1160. double remaining = currentTime / _position * (1 - _position);
  1161. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1162. } else
  1163. _cachedRemainingTime = [VLCTime nullTime];
  1164. [self didChangeValueForKey:@"remainingTime"];
  1165. [self didChangeValueForKey:@"time"];
  1166. }
  1167. #if !TARGET_OS_IPHONE
  1168. - (void)delaySleep
  1169. {
  1170. UpdateSystemActivity(UsrActivity);
  1171. }
  1172. #endif
  1173. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1174. {
  1175. #if !TARGET_OS_IPHONE
  1176. // This seems to be the most relevant place to delay sleeping and screen saver.
  1177. [self delaySleep];
  1178. #endif
  1179. [self willChangeValueForKey:@"position"];
  1180. _position = [newPosition floatValue];
  1181. [self didChangeValueForKey:@"position"];
  1182. }
  1183. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1184. {
  1185. [self willChangeValueForKey:@"state"];
  1186. _cachedState = [newState intValue];
  1187. #if TARGET_OS_IPHONE
  1188. // Disable idle timer if player is playing media
  1189. // Exclusion can be made for audio only media
  1190. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1191. #endif
  1192. [self didChangeValueForKey:@"state"];
  1193. }
  1194. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1195. {
  1196. [self willChangeValueForKey:@"media"];
  1197. if (_media != newMedia) {
  1198. _media = newMedia;
  1199. [self willChangeValueForKey:@"time"];
  1200. [self willChangeValueForKey:@"remainingTime"];
  1201. [self willChangeValueForKey:@"position"];
  1202. _cachedTime = [VLCTime nullTime];
  1203. _cachedRemainingTime = [VLCTime nullTime];
  1204. _position = 0.0f;
  1205. [self didChangeValueForKey:@"position"];
  1206. [self didChangeValueForKey:@"remainingTime"];
  1207. [self didChangeValueForKey:@"time"];
  1208. }
  1209. [self didChangeValueForKey:@"media"];
  1210. }
  1211. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1212. {
  1213. [self willChangeValueForKey:@"currentTitleIndex"];
  1214. [self didChangeValueForKey:@"currentTitleIndex"];
  1215. }
  1216. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1217. {
  1218. [self willChangeValueForKey:@"currentChapterIndex"];
  1219. [self didChangeValueForKey:@"currentChapterIndex"];
  1220. }
  1221. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1222. {
  1223. @synchronized(_snapshots) {
  1224. if (!_snapshots) {
  1225. _snapshots = [NSMutableArray array];
  1226. }
  1227. [_snapshots addObject:fileName];
  1228. }
  1229. }
  1230. - (void)mediaPlayerRecordChanged:(NSArray *)arguments
  1231. {
  1232. NSString *filePath = arguments.firstObject[@"filePath"];
  1233. BOOL isRecording = [arguments.firstObject[@"isRecording"] boolValue];
  1234. if (isRecording) {
  1235. if ([_delegate respondsToSelector:@selector(mediaPlayerStartedRecording:)]) {
  1236. [_delegate mediaPlayerStartedRecording:self];
  1237. }
  1238. } else {
  1239. if ([_delegate respondsToSelector:@selector(mediaPlayer:recordingStoppedAtPath:)]) {
  1240. [_delegate mediaPlayer:self recordingStoppedAtPath:filePath];
  1241. }
  1242. }
  1243. }
  1244. @end