VLCMediaPlayer.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. [self unregisterObservers];
  271. [[VLCEventManager sharedManager] cancelCallToObject:self];
  272. // Always get rid of the delegate first so we can stop sending messages to it
  273. // TODO: Should we tell the delegate that we're shutting down?
  274. _delegate = nil;
  275. // Clear our drawable as we are going to release it, we don't
  276. // want the core to use it from this point.
  277. libvlc_media_player_set_nsobject(_playerInstance, nil);
  278. _drawable = nil;
  279. if (_equalizerInstance) {
  280. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  281. libvlc_audio_equalizer_release(_equalizerInstance);
  282. _equalizerInstance = nil;
  283. }
  284. if (_viewpoint)
  285. libvlc_free(_viewpoint);
  286. libvlc_media_player_release(_playerInstance);
  287. if (_privateLibrary != [VLCLibrary sharedLibrary])
  288. libvlc_release(_privateLibrary.instance);
  289. }
  290. #if !TARGET_OS_IPHONE
  291. - (void)setVideoView:(VLCVideoView *)aVideoView
  292. {
  293. [self setDrawable: aVideoView];
  294. }
  295. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  296. {
  297. [self setDrawable: aVideoLayer];
  298. }
  299. #endif
  300. - (void)setDrawable:(id)aDrawable
  301. {
  302. // Make sure that this instance has been associated with the drawing canvas.
  303. _drawable = aDrawable;
  304. /* Note that ee need the caller to wait until the setter succeeded.
  305. * Otherwise, s/he might want to deploy the drawable while it isn’t ready yet. */
  306. dispatch_sync(_libVLCBackgroundQueue, ^{
  307. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  308. });
  309. }
  310. - (id)drawable
  311. {
  312. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  313. }
  314. - (VLCAudio *)audio
  315. {
  316. if (!_audio)
  317. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  318. return _audio;
  319. }
  320. #pragma mark -
  321. #pragma mark Video Tracks
  322. - (void)setCurrentVideoTrackIndex:(int)value
  323. {
  324. libvlc_video_set_track(_playerInstance, value);
  325. }
  326. - (int)currentVideoTrackIndex
  327. {
  328. int count = libvlc_video_get_track_count(_playerInstance);
  329. if (count <= 0)
  330. return -1;
  331. return libvlc_video_get_track(_playerInstance);
  332. }
  333. - (NSArray *)videoTrackNames
  334. {
  335. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  336. if (count <= 0)
  337. return @[];
  338. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  339. libvlc_track_description_t *currentTrack = firstTrack;
  340. NSMutableArray *tempArray = [NSMutableArray array];
  341. while (currentTrack) {
  342. [tempArray addObject:@(currentTrack->psz_name)];
  343. currentTrack = currentTrack->p_next;
  344. }
  345. libvlc_track_description_list_release(firstTrack);
  346. return [NSArray arrayWithArray: tempArray];
  347. }
  348. - (NSArray *)videoTrackIndexes
  349. {
  350. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  351. if (count <= 0)
  352. return @[];
  353. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  354. libvlc_track_description_t *currentTrack = firstTrack;
  355. NSMutableArray *tempArray = [NSMutableArray array];
  356. while (currentTrack) {
  357. [tempArray addObject:@(currentTrack->i_id)];
  358. currentTrack = currentTrack->p_next;
  359. }
  360. libvlc_track_description_list_release(firstTrack);
  361. return [NSArray arrayWithArray: tempArray];
  362. }
  363. - (int)numberOfVideoTracks
  364. {
  365. return libvlc_video_get_track_count(_playerInstance);
  366. }
  367. #pragma mark -
  368. #pragma mark Subtitles
  369. - (void)setCurrentVideoSubTitleIndex:(int)index
  370. {
  371. libvlc_video_set_spu(_playerInstance, index);
  372. }
  373. - (int)currentVideoSubTitleIndex
  374. {
  375. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  376. if (count <= 0)
  377. return -1;
  378. return libvlc_video_get_spu(_playerInstance);
  379. }
  380. - (NSArray *)videoSubTitlesNames
  381. {
  382. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  383. if (count <= 0)
  384. return @[];
  385. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  386. libvlc_track_description_t *currentTrack = firstTrack;
  387. NSMutableArray *tempArray = [NSMutableArray array];
  388. while (currentTrack) {
  389. NSString *track = @(currentTrack->psz_name);
  390. [tempArray addObject:track != nil ? track : @""];
  391. currentTrack = currentTrack->p_next;
  392. }
  393. libvlc_track_description_list_release(firstTrack);
  394. return [NSArray arrayWithArray: tempArray];
  395. }
  396. - (NSArray *)videoSubTitlesIndexes
  397. {
  398. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  399. if (count <= 0)
  400. return @[];
  401. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  402. libvlc_track_description_t *currentTrack = firstTrack;
  403. NSMutableArray *tempArray = [NSMutableArray array];
  404. while (currentTrack) {
  405. [tempArray addObject:@(currentTrack->i_id)];
  406. currentTrack = currentTrack->p_next;
  407. }
  408. libvlc_track_description_list_release(firstTrack);
  409. return [NSArray arrayWithArray: tempArray];
  410. }
  411. - (int)numberOfSubtitlesTracks
  412. {
  413. return libvlc_video_get_spu_count(_playerInstance);
  414. }
  415. - (int)addPlaybackSlave:(NSURL *)slaveURL type:(VLCMediaPlaybackSlaveType)slaveType enforce:(BOOL)enforceSelection
  416. {
  417. if (!slaveURL)
  418. return -1;
  419. return libvlc_media_player_add_slave(_playerInstance,
  420. (libvlc_media_slave_type_t)slaveType,
  421. [[slaveURL absoluteString] UTF8String],
  422. enforceSelection);
  423. }
  424. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  425. {
  426. libvlc_video_set_spu_delay(_playerInstance, index);
  427. }
  428. - (NSInteger)currentVideoSubTitleDelay
  429. {
  430. return libvlc_video_get_spu_delay(_playerInstance);
  431. }
  432. #if TARGET_OS_IPHONE
  433. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  434. {
  435. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  436. }
  437. - (void)setTextRendererFont:(NSString *)fontname
  438. {
  439. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  440. }
  441. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  442. {
  443. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  444. }
  445. - (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
  446. {
  447. libvlc_video_set_textrenderer_bool(_playerInstance, libvlc_textrender_fontforcebold, [fontForceBold boolValue]);
  448. }
  449. #endif
  450. #pragma mark -
  451. #pragma mark Video Crop geometry
  452. - (void)setCropRatioWithNumerator:(unsigned int)numerator denominator:(unsigned int)denominator
  453. {
  454. libvlc_video_set_crop_ratio(_playerInstance, numerator, denominator);
  455. }
  456. - (void)setVideoAspectRatio:(char *)value
  457. {
  458. libvlc_video_set_aspect_ratio(_playerInstance, value);
  459. }
  460. - (char *)videoAspectRatio
  461. {
  462. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  463. return result;
  464. }
  465. - (void)setScaleFactor:(float)value
  466. {
  467. libvlc_video_set_scale(_playerInstance, value);
  468. }
  469. - (float)scaleFactor
  470. {
  471. return libvlc_video_get_scale(_playerInstance);
  472. }
  473. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  474. {
  475. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  476. if (failure)
  477. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  478. }
  479. - (void)setDeinterlaceFilter:(NSString *)name
  480. {
  481. if (!name || name.length < 1)
  482. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOff, NULL);
  483. else
  484. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOn, [name UTF8String]);
  485. }
  486. - (void)setDeinterlace:(VLCDeinterlace)deinterlace withFilter:(NSString *)name
  487. {
  488. libvlc_video_set_deinterlace(_playerInstance, (int)deinterlace, [name UTF8String]);
  489. }
  490. - (BOOL)adjustFilterEnabled
  491. {
  492. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  493. }
  494. - (void)setAdjustFilterEnabled:(BOOL)b_value
  495. {
  496. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  497. }
  498. - (float)contrast
  499. {
  500. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  501. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  502. }
  503. - (void)setContrast:(float)f_value
  504. {
  505. if (f_value <= 2. && f_value >= 0.) {
  506. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  507. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  508. }
  509. }
  510. - (float)brightness
  511. {
  512. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  513. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  514. }
  515. - (void)setBrightness:(float)f_value
  516. {
  517. if (f_value <= 2. && f_value >= 0.) {
  518. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  519. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  520. }
  521. }
  522. - (float)hue
  523. {
  524. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  525. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  526. }
  527. - (void)setHue:(float)f_value
  528. {
  529. if (f_value <= 180. && f_value >= -180.) {
  530. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  531. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  532. }
  533. }
  534. - (float)saturation
  535. {
  536. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  537. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  538. }
  539. - (void)setSaturation:(float)f_value
  540. {
  541. if (f_value <= 3. && f_value >= 0.) {
  542. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  543. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  544. }
  545. }
  546. - (float)gamma
  547. {
  548. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  549. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  550. }
  551. - (void)setGamma:(float)f_value
  552. {
  553. if (f_value <= 10. && f_value >= 0.) {
  554. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  555. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  556. }
  557. }
  558. - (void)setRate:(float)value
  559. {
  560. libvlc_media_player_set_rate(_playerInstance, value);
  561. }
  562. - (float)rate
  563. {
  564. return libvlc_media_player_get_rate(_playerInstance);
  565. }
  566. - (CGSize)videoSize
  567. {
  568. unsigned height = 0, width = 0;
  569. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  570. if (failure)
  571. return CGSizeZero;
  572. return CGSizeMake(width, height);
  573. }
  574. - (BOOL)hasVideoOut
  575. {
  576. return libvlc_media_player_has_vout(_playerInstance);
  577. }
  578. - (void)setTime:(VLCTime *)value
  579. {
  580. // Time is managed in seconds, while duration is managed in microseconds
  581. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  582. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0, NO);
  583. }
  584. - (VLCTime *)time
  585. {
  586. return _cachedTime;
  587. }
  588. - (VLCTime *)remainingTime
  589. {
  590. return _cachedRemainingTime;
  591. }
  592. #pragma mark -
  593. #pragma mark Chapters
  594. - (void)setCurrentChapterIndex:(int)value;
  595. {
  596. libvlc_media_player_set_chapter(_playerInstance, value);
  597. }
  598. - (int)currentChapterIndex
  599. {
  600. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  601. if (count <= 0)
  602. return -1;
  603. int result = libvlc_media_player_get_chapter(_playerInstance);
  604. return result;
  605. }
  606. - (void)nextChapter
  607. {
  608. libvlc_media_player_next_chapter(_playerInstance);
  609. }
  610. - (void)previousChapter
  611. {
  612. libvlc_media_player_previous_chapter(_playerInstance);
  613. }
  614. #pragma mark -
  615. #pragma mark Titles
  616. - (void)setCurrentTitleIndex:(int)value
  617. {
  618. libvlc_media_player_set_title(_playerInstance, value);
  619. }
  620. - (int)currentTitleIndex
  621. {
  622. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  623. if (count <= 0)
  624. return -1;
  625. return libvlc_media_player_get_title(_playerInstance);
  626. }
  627. - (int)numberOfTitles
  628. {
  629. return libvlc_media_player_get_title_count(_playerInstance);
  630. }
  631. - (NSArray *)titleDescriptions
  632. {
  633. libvlc_title_description_t **titleInfo;
  634. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  635. if (numberOfTitleDescriptions < 0)
  636. return [NSArray array];
  637. if (numberOfTitleDescriptions == 0) {
  638. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  639. return [NSArray array];
  640. }
  641. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  642. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  643. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  644. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  645. VLCTitleDescriptionDuration,
  646. @(titleInfo[i]->i_flags & libvlc_title_menu),
  647. VLCTitleDescriptionIsMenu,
  648. nil];
  649. if (titleInfo[i]->psz_name != NULL)
  650. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  651. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  652. }
  653. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  654. return [NSArray arrayWithArray:array];
  655. }
  656. - (int)indexOfLongestTitle
  657. {
  658. NSArray *titles = [self titleDescriptions];
  659. NSUInteger titleCount = titles.count;
  660. int currentlyFoundTitle = 0;
  661. int64_t currentlySelectedDuration = 0;
  662. int64_t randomTitleDuration = 0;
  663. for (int x = 0; x < titleCount; x++) {
  664. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  665. if (randomTitleDuration > currentlySelectedDuration) {
  666. currentlySelectedDuration = randomTitleDuration;
  667. currentlyFoundTitle = x;
  668. }
  669. }
  670. return currentlyFoundTitle;
  671. }
  672. - (int)numberOfChaptersForTitle:(int)titleIndex
  673. {
  674. if (titleIndex >= 0) {
  675. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  676. }
  677. return 0;
  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. libvlc_media_player_stop_async(_playerInstance);
  875. }
  876. - (libvlc_video_viewpoint_t *)viewPoint
  877. {
  878. if (_viewpoint == NULL) {
  879. _viewpoint = libvlc_video_new_viewpoint();
  880. }
  881. return _viewpoint;
  882. }
  883. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  884. {
  885. if ([self viewPoint]) {
  886. [self viewPoint]->f_yaw = yaw;
  887. [self viewPoint]->f_pitch = pitch;
  888. [self viewPoint]->f_roll = roll;
  889. [self viewPoint]->f_field_of_view = fov;
  890. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  891. }
  892. return NO;
  893. }
  894. - (float)yaw
  895. {
  896. if ([self viewPoint]) {
  897. return [self viewPoint]->f_yaw;
  898. }
  899. return 0;
  900. }
  901. - (float)pitch
  902. {
  903. if ([self viewPoint]) {
  904. return [self viewPoint]->f_pitch;
  905. }
  906. return 0;
  907. }
  908. - (float)roll
  909. {
  910. if ([self viewPoint]) {
  911. return [self viewPoint]->f_roll;
  912. }
  913. return 0;
  914. }
  915. - (float)fov
  916. {
  917. if ([self viewPoint]) {
  918. return [self viewPoint]->f_field_of_view;
  919. }
  920. return 0;
  921. }
  922. - (void)gotoNextFrame
  923. {
  924. libvlc_media_player_next_frame(_playerInstance);
  925. }
  926. - (void)fastForward
  927. {
  928. [self fastForwardAtRate: 2.0];
  929. }
  930. - (void)fastForwardAtRate:(float)rate
  931. {
  932. [self setRate:rate];
  933. }
  934. - (void)rewind
  935. {
  936. [self rewindAtRate: 2.0];
  937. }
  938. - (void)rewindAtRate:(float)rate
  939. {
  940. [self setRate: -rate];
  941. }
  942. - (void)jumpBackward:(int)interval
  943. {
  944. if ([self isSeekable]) {
  945. interval = interval * 1000;
  946. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  947. }
  948. }
  949. - (void)jumpForward:(int)interval
  950. {
  951. if ([self isSeekable]) {
  952. interval = interval * 1000;
  953. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  954. }
  955. }
  956. - (void)extraShortJumpBackward
  957. {
  958. [self jumpBackward:3];
  959. }
  960. - (void)extraShortJumpForward
  961. {
  962. [self jumpForward:3];
  963. }
  964. - (void)shortJumpBackward
  965. {
  966. [self jumpBackward:10];
  967. }
  968. - (void)shortJumpForward
  969. {
  970. [self jumpForward:10];
  971. }
  972. - (void)mediumJumpBackward
  973. {
  974. [self jumpBackward:60];
  975. }
  976. - (void)mediumJumpForward
  977. {
  978. [self jumpForward:60];
  979. }
  980. - (void)longJumpBackward
  981. {
  982. [self jumpBackward:300];
  983. }
  984. - (void)longJumpForward
  985. {
  986. [self jumpForward:300];
  987. }
  988. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  989. {
  990. libvlc_media_player_navigate(_playerInstance, action);
  991. }
  992. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  993. {
  994. return [NSSet setWithObjects:@"state", nil];
  995. }
  996. - (BOOL)isPlaying
  997. {
  998. return libvlc_media_player_is_playing(_playerInstance);
  999. }
  1000. - (VLCMediaPlayerState)state
  1001. {
  1002. return _cachedState;
  1003. }
  1004. - (float)position
  1005. {
  1006. return _position;
  1007. }
  1008. - (void)setPosition:(float)newPosition
  1009. {
  1010. libvlc_media_player_set_position(_playerInstance, newPosition, NO);
  1011. }
  1012. - (BOOL)isSeekable
  1013. {
  1014. return libvlc_media_player_is_seekable(_playerInstance);
  1015. }
  1016. - (BOOL)canPause
  1017. {
  1018. return libvlc_media_player_can_pause(_playerInstance);
  1019. }
  1020. - (NSArray *)snapshots
  1021. {
  1022. return [_snapshots copy];
  1023. }
  1024. #if TARGET_OS_IPHONE
  1025. - (UIImage *)lastSnapshot {
  1026. if (_snapshots == nil) {
  1027. return nil;
  1028. }
  1029. @synchronized(_snapshots) {
  1030. if (_snapshots.count == 0)
  1031. return nil;
  1032. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1033. }
  1034. }
  1035. #else
  1036. - (NSImage *)lastSnapshot {
  1037. if (_snapshots == nil) {
  1038. return nil;
  1039. }
  1040. @synchronized(_snapshots) {
  1041. if (_snapshots.count == 0)
  1042. return nil;
  1043. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1044. }
  1045. }
  1046. #endif
  1047. - (void *)libVLCMediaPlayer
  1048. {
  1049. return _playerInstance;
  1050. }
  1051. - (BOOL)startRecordingAtPath:(NSString *)path
  1052. {
  1053. return libvlc_media_player_record(_playerInstance, YES, [path UTF8String]);
  1054. }
  1055. - (BOOL)stopRecording
  1056. {
  1057. return libvlc_media_player_record(_playerInstance, NO, nil);
  1058. }
  1059. #pragma mark -
  1060. #pragma mark - Renderer
  1061. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1062. {
  1063. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1064. }
  1065. @end
  1066. @implementation VLCMediaPlayer (Private)
  1067. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1068. {
  1069. if (self = [super init]) {
  1070. _cachedTime = [VLCTime nullTime];
  1071. _cachedRemainingTime = [VLCTime nullTime];
  1072. _position = 0.0f;
  1073. _cachedState = VLCMediaPlayerStateStopped;
  1074. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1075. // Create a media instance, it doesn't matter what library we start off with
  1076. // it will change depending on the media descriptor provided to the media
  1077. // instance
  1078. if (options && options.count > 0) {
  1079. VKLog(@"creating player instance with private library as options were given");
  1080. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1081. } else {
  1082. VKLog(@"creating player instance using shared library");
  1083. _privateLibrary = [VLCLibrary sharedLibrary];
  1084. }
  1085. libvlc_retain([_privateLibrary instance]);
  1086. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1087. if (_playerInstance == NULL) {
  1088. NSAssert(0, @"%s: player initialization failed", __PRETTY_FUNCTION__);
  1089. libvlc_release([_privateLibrary instance]);
  1090. return nil;
  1091. }
  1092. [self registerObservers];
  1093. [self setDrawable:aDrawable];
  1094. }
  1095. return self;
  1096. }
  1097. - (void)registerObservers
  1098. {
  1099. // Attach event observers into the media instance
  1100. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1101. if (!p_em)
  1102. return;
  1103. /* We need the caller to wait until this block is done.
  1104. * The initialized object shall not be returned until the event attachments are done. */
  1105. dispatch_sync(_libVLCBackgroundQueue,^{
  1106. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1107. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1108. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1109. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1110. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1111. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1112. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1113. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1114. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1115. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1116. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1117. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1118. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1119. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1120. libvlc_event_attach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1121. });
  1122. }
  1123. - (void)unregisterObservers
  1124. {
  1125. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1126. if (!p_em)
  1127. return;
  1128. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1129. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1130. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1131. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1132. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1133. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1134. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1135. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1136. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1137. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1138. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1139. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1140. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1141. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1142. libvlc_event_detach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1143. }
  1144. - (dispatch_queue_t)libVLCBackgroundQueue
  1145. {
  1146. if (!_libVLCBackgroundQueue) {
  1147. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1148. }
  1149. return _libVLCBackgroundQueue;
  1150. }
  1151. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1152. {
  1153. [self willChangeValueForKey:@"time"];
  1154. [self willChangeValueForKey:@"remainingTime"];
  1155. _cachedTime = [VLCTime timeWithNumber:newTime];
  1156. double currentTime = [[_cachedTime value] doubleValue];
  1157. if (currentTime > 0 && _position > 0.) {
  1158. double remaining = currentTime / _position * (1 - _position);
  1159. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1160. } else
  1161. _cachedRemainingTime = [VLCTime nullTime];
  1162. [self didChangeValueForKey:@"remainingTime"];
  1163. [self didChangeValueForKey:@"time"];
  1164. }
  1165. #if !TARGET_OS_IPHONE
  1166. - (void)delaySleep
  1167. {
  1168. UpdateSystemActivity(UsrActivity);
  1169. }
  1170. #endif
  1171. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1172. {
  1173. #if !TARGET_OS_IPHONE
  1174. // This seems to be the most relevant place to delay sleeping and screen saver.
  1175. [self delaySleep];
  1176. #endif
  1177. [self willChangeValueForKey:@"position"];
  1178. _position = [newPosition floatValue];
  1179. [self didChangeValueForKey:@"position"];
  1180. }
  1181. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1182. {
  1183. [self willChangeValueForKey:@"state"];
  1184. _cachedState = [newState intValue];
  1185. #if TARGET_OS_IPHONE
  1186. // Disable idle timer if player is playing media
  1187. // Exclusion can be made for audio only media
  1188. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1189. #endif
  1190. [self didChangeValueForKey:@"state"];
  1191. }
  1192. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1193. {
  1194. [self willChangeValueForKey:@"media"];
  1195. if (_media != newMedia) {
  1196. _media = newMedia;
  1197. [self willChangeValueForKey:@"time"];
  1198. [self willChangeValueForKey:@"remainingTime"];
  1199. [self willChangeValueForKey:@"position"];
  1200. _cachedTime = [VLCTime nullTime];
  1201. _cachedRemainingTime = [VLCTime nullTime];
  1202. _position = 0.0f;
  1203. [self didChangeValueForKey:@"position"];
  1204. [self didChangeValueForKey:@"remainingTime"];
  1205. [self didChangeValueForKey:@"time"];
  1206. }
  1207. [self didChangeValueForKey:@"media"];
  1208. }
  1209. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1210. {
  1211. [self willChangeValueForKey:@"currentTitleIndex"];
  1212. [self didChangeValueForKey:@"currentTitleIndex"];
  1213. }
  1214. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1215. {
  1216. [self willChangeValueForKey:@"currentChapterIndex"];
  1217. [self didChangeValueForKey:@"currentChapterIndex"];
  1218. }
  1219. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1220. {
  1221. @synchronized(_snapshots) {
  1222. if (!_snapshots) {
  1223. _snapshots = [NSMutableArray array];
  1224. }
  1225. [_snapshots addObject:fileName];
  1226. }
  1227. }
  1228. - (void)mediaPlayerRecordChanged:(NSArray *)arguments
  1229. {
  1230. NSString *filePath = arguments.firstObject[@"filePath"];
  1231. BOOL isRecording = [arguments.firstObject[@"isRecording"] boolValue];
  1232. if (isRecording) {
  1233. if ([_delegate respondsToSelector:@selector(mediaPlayerStartedRecording:)]) {
  1234. [_delegate mediaPlayerStartedRecording:self];
  1235. }
  1236. } else {
  1237. if ([_delegate respondsToSelector:@selector(mediaPlayer:recordingStoppedAtPath:)]) {
  1238. [_delegate mediaPlayer:self recordingStoppedAtPath:filePath];
  1239. }
  1240. }
  1241. }
  1242. @end