VLCMediaPlayer.m 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2015 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2017 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Faustion Osuna <enrique.osuna # gmail.com>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  12. * Soomin Lee <TheHungryBu # gmail.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU Lesser General Public License as published by
  16. * the Free Software Foundation; either version 2.1 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  27. *****************************************************************************/
  28. #import "VLCLibrary.h"
  29. #import "VLCMediaPlayer.h"
  30. #import "VLCEventManager.h"
  31. #import "VLCLibVLCBridging.h"
  32. #if !TARGET_OS_IPHONE
  33. # import "VLCVideoView.h"
  34. #endif
  35. #ifdef HAVE_CONFIG_H
  36. # include "config.h"
  37. #endif
  38. #if !TARGET_OS_IPHONE
  39. /* prevent system sleep */
  40. # import <CoreServices/CoreServices.h>
  41. /* FIXME: Ugly hack! */
  42. # ifdef __x86_64__
  43. # import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  44. # endif
  45. #endif
  46. #include <vlc/vlc.h>
  47. /* Notification Messages */
  48. NSString *const VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  49. NSString *const VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  50. NSString *const VLCMediaPlayerTitleChanged = @"VLCMediaPlayerTitleChanged";
  51. NSString *const VLCMediaPlayerChapterChanged = @"VLCMediaPlayerChapterChanged";
  52. NSString *const VLCMediaPlayerSnapshotTaken = @"VLCMediaPlayerSnapshotTaken";
  53. /* title keys */
  54. NSString *const VLCTitleDescriptionName = @"VLCTitleDescriptionName";
  55. NSString *const VLCTitleDescriptionDuration = @"VLCTitleDescriptionDuration";
  56. NSString *const VLCTitleDescriptionIsMenu = @"VLCTitleDescriptionIsMenu";
  57. /* chapter keys */
  58. NSString *const VLCChapterDescriptionName = @"VLCChapterDescriptionName";
  59. NSString *const VLCChapterDescriptionTimeOffset = @"VLCChapterDescriptionTimeOffset";
  60. NSString *const VLCChapterDescriptionDuration = @"VLCChapterDescriptionDuration";
  61. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  62. {
  63. static NSString * stateToStrings[] = {
  64. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  65. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  66. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  67. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  68. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  69. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  70. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused",
  71. [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. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  419. {
  420. return [self addPlaybackSlave:[NSURL fileURLWithPath:path] type:VLCMediaPlaybackSlaveTypeSubtitle enforce:YES];
  421. }
  422. - (int)addPlaybackSlave:(NSURL *)slaveURL type:(VLCMediaPlaybackSlaveType)slaveType enforce:(BOOL)enforceSelection
  423. {
  424. if (!slaveURL)
  425. return -1;
  426. return libvlc_media_player_add_slave(_playerInstance,
  427. slaveType,
  428. [[slaveURL absoluteString] UTF8String],
  429. enforceSelection);
  430. }
  431. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  432. {
  433. libvlc_video_set_spu_delay(_playerInstance, index);
  434. }
  435. - (NSInteger)currentVideoSubTitleDelay
  436. {
  437. return libvlc_video_get_spu_delay(_playerInstance);
  438. }
  439. #if TARGET_OS_IPHONE
  440. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  441. {
  442. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  443. }
  444. - (void)setTextRendererFont:(NSString *)fontname
  445. {
  446. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  447. }
  448. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  449. {
  450. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  451. }
  452. - (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
  453. {
  454. libvlc_video_set_textrenderer_bool(_playerInstance, libvlc_textrender_fontforcebold, [fontForceBold boolValue]);
  455. }
  456. #endif
  457. #pragma mark -
  458. #pragma mark Video Crop geometry
  459. - (void)setVideoCropGeometry:(char *)value
  460. {
  461. libvlc_video_set_crop_geometry(_playerInstance, value);
  462. }
  463. - (char *)videoCropGeometry
  464. {
  465. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  466. return result;
  467. }
  468. - (void)setVideoAspectRatio:(char *)value
  469. {
  470. libvlc_video_set_aspect_ratio(_playerInstance, value);
  471. }
  472. - (char *)videoAspectRatio
  473. {
  474. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  475. return result;
  476. }
  477. - (void)setScaleFactor:(float)value
  478. {
  479. libvlc_video_set_scale(_playerInstance, value);
  480. }
  481. - (float)scaleFactor
  482. {
  483. return libvlc_video_get_scale(_playerInstance);
  484. }
  485. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  486. {
  487. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  488. if (failure)
  489. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  490. }
  491. - (void)setDeinterlaceFilter:(NSString *)name
  492. {
  493. if (!name || name.length < 1)
  494. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOff, NULL);
  495. else
  496. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOn, [name UTF8String]);
  497. }
  498. - (void)setDeinterlace:(VLCDeinterlace)deinterlace withFilter:(NSString *)name
  499. {
  500. libvlc_video_set_deinterlace(_playerInstance, deinterlace, [name UTF8String]);
  501. }
  502. - (BOOL)adjustFilterEnabled
  503. {
  504. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  505. }
  506. - (void)setAdjustFilterEnabled:(BOOL)b_value
  507. {
  508. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  509. }
  510. - (float)contrast
  511. {
  512. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  513. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  514. }
  515. - (void)setContrast:(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_Contrast, f_value);
  520. }
  521. }
  522. - (float)brightness
  523. {
  524. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  525. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  526. }
  527. - (void)setBrightness:(float)f_value
  528. {
  529. if (f_value <= 2. && f_value >= 0.) {
  530. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  531. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  532. }
  533. }
  534. - (float)hue
  535. {
  536. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  537. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  538. }
  539. - (void)setHue:(float)f_value
  540. {
  541. if (f_value <= 180. && f_value >= -180.) {
  542. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  543. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  544. }
  545. }
  546. - (float)saturation
  547. {
  548. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  549. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  550. }
  551. - (void)setSaturation:(float)f_value
  552. {
  553. if (f_value <= 3. && f_value >= 0.) {
  554. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  555. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  556. }
  557. }
  558. - (float)gamma
  559. {
  560. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  561. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  562. }
  563. - (void)setGamma:(float)f_value
  564. {
  565. if (f_value <= 10. && f_value >= 0.) {
  566. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  567. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  568. }
  569. }
  570. - (void)setRate:(float)value
  571. {
  572. libvlc_media_player_set_rate(_playerInstance, value);
  573. }
  574. - (float)rate
  575. {
  576. return libvlc_media_player_get_rate(_playerInstance);
  577. }
  578. - (CGSize)videoSize
  579. {
  580. unsigned height = 0, width = 0;
  581. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  582. if (failure)
  583. return CGSizeZero;
  584. return CGSizeMake(width, height);
  585. }
  586. - (BOOL)hasVideoOut
  587. {
  588. return libvlc_media_player_has_vout(_playerInstance);
  589. }
  590. - (float)framesPerSecond
  591. {
  592. return .0;
  593. }
  594. - (void)setTime:(VLCTime *)value
  595. {
  596. // Time is managed in seconds, while duration is managed in microseconds
  597. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  598. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0);
  599. }
  600. - (VLCTime *)time
  601. {
  602. return _cachedTime;
  603. }
  604. - (VLCTime *)remainingTime
  605. {
  606. return _cachedRemainingTime;
  607. }
  608. #pragma mark -
  609. #pragma mark Chapters
  610. - (void)setCurrentChapterIndex:(int)value;
  611. {
  612. libvlc_media_player_set_chapter(_playerInstance, value);
  613. }
  614. - (int)currentChapterIndex
  615. {
  616. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  617. if (count <= 0)
  618. return -1;
  619. int result = libvlc_media_player_get_chapter(_playerInstance);
  620. return result;
  621. }
  622. - (void)nextChapter
  623. {
  624. libvlc_media_player_next_chapter(_playerInstance);
  625. }
  626. - (void)previousChapter
  627. {
  628. libvlc_media_player_previous_chapter(_playerInstance);
  629. }
  630. - (NSArray *)chaptersForTitleIndex:(int)title
  631. {
  632. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  633. if (count <= 0)
  634. return @[];
  635. #pragma clang diagnostic push
  636. #pragma clang diagnostic ignored "-Wdeprecated"
  637. libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
  638. libvlc_track_description_t *currentTrack = firstTrack;
  639. #pragma clang diagnostic push
  640. NSMutableArray *tempArray = [NSMutableArray array];
  641. for (NSInteger i = 0; i < count ; i++) {
  642. [tempArray addObject:@(currentTrack->psz_name)];
  643. currentTrack = currentTrack->p_next;
  644. }
  645. libvlc_track_description_list_release(firstTrack);
  646. return [NSArray arrayWithArray:tempArray];
  647. }
  648. #pragma mark -
  649. #pragma mark Titles
  650. - (void)setCurrentTitleIndex:(int)value
  651. {
  652. libvlc_media_player_set_title(_playerInstance, value);
  653. }
  654. - (int)currentTitleIndex
  655. {
  656. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  657. if (count <= 0)
  658. return -1;
  659. return libvlc_media_player_get_title(_playerInstance);
  660. }
  661. - (int)numberOfTitles
  662. {
  663. return libvlc_media_player_get_title_count(_playerInstance);
  664. }
  665. - (NSUInteger)countOfTitles
  666. {
  667. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  668. return result;
  669. }
  670. - (NSArray *)titles
  671. {
  672. NSUInteger count = [self countOfTitles];
  673. if (count == 0)
  674. return [NSArray array];
  675. #pragma clang diagnostic push
  676. #pragma clang diagnostic ignored "-Wdeprecated"
  677. libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
  678. libvlc_track_description_t *currentTrack = firstTrack;
  679. #pragma clang diagnostic pop
  680. if (!currentTrack)
  681. return [NSArray array];
  682. NSMutableArray *tempArray = [NSMutableArray array];
  683. while (1) {
  684. if (currentTrack->psz_name != nil)
  685. [tempArray addObject:@(currentTrack->psz_name)];
  686. if (currentTrack->p_next)
  687. currentTrack = currentTrack->p_next;
  688. else
  689. break;
  690. }
  691. libvlc_track_description_list_release(firstTrack);
  692. return [NSArray arrayWithArray: tempArray];
  693. }
  694. - (NSArray *)titleDescriptions
  695. {
  696. libvlc_title_description_t **titleInfo;
  697. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  698. if (numberOfTitleDescriptions < 0)
  699. return [NSArray array];
  700. if (numberOfTitleDescriptions == 0) {
  701. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  702. return [NSArray array];
  703. }
  704. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  705. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  706. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  707. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  708. VLCTitleDescriptionDuration,
  709. @(titleInfo[i]->i_flags & libvlc_title_menu),
  710. VLCTitleDescriptionIsMenu,
  711. nil];
  712. if (titleInfo[i]->psz_name != NULL)
  713. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  714. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  715. }
  716. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  717. return [NSArray arrayWithArray:array];
  718. }
  719. - (int)indexOfLongestTitle
  720. {
  721. NSArray *titles = [self titleDescriptions];
  722. NSUInteger titleCount = titles.count;
  723. int currentlyFoundTitle = 0;
  724. int64_t currentlySelectedDuration = 0;
  725. int64_t randomTitleDuration = 0;
  726. for (int x = 0; x < titleCount; x++) {
  727. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  728. if (randomTitleDuration > currentlySelectedDuration) {
  729. currentlySelectedDuration = randomTitleDuration;
  730. currentlyFoundTitle = x;
  731. }
  732. }
  733. return currentlyFoundTitle;
  734. }
  735. - (int)numberOfChaptersForTitle:(int)titleIndex
  736. {
  737. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  738. }
  739. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  740. {
  741. libvlc_chapter_description_t **chapterDescriptions;
  742. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  743. titleIndex,
  744. &chapterDescriptions);
  745. if (numberOfChapterDescriptions < 0)
  746. return [NSArray array];
  747. if (numberOfChapterDescriptions == 0) {
  748. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  749. return [NSArray array];
  750. }
  751. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  752. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  753. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  754. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  755. VLCChapterDescriptionDuration,
  756. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  757. VLCChapterDescriptionTimeOffset,
  758. nil];
  759. if (chapterDescriptions[i]->psz_name != NULL)
  760. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  761. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  762. }
  763. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  764. return [NSArray arrayWithArray:array];
  765. }
  766. #pragma mark -
  767. #pragma mark Audio tracks
  768. - (void)setCurrentAudioTrackIndex:(int)value
  769. {
  770. libvlc_audio_set_track(_playerInstance, value);
  771. }
  772. - (int)currentAudioTrackIndex
  773. {
  774. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  775. if (count <= 0)
  776. return -1;
  777. return libvlc_audio_get_track(_playerInstance);
  778. }
  779. - (NSArray *)audioTrackNames
  780. {
  781. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  782. if (count <= 0)
  783. return @[];
  784. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  785. libvlc_track_description_t *currentTrack = firstTrack;
  786. NSMutableArray *tempArray = [NSMutableArray array];
  787. while (currentTrack) {
  788. NSString *track = @(currentTrack->psz_name);
  789. [tempArray addObject:track != nil ? track : @""];
  790. currentTrack = currentTrack->p_next;
  791. }
  792. libvlc_track_description_list_release(firstTrack);
  793. return [NSArray arrayWithArray: tempArray];
  794. }
  795. - (NSArray *)audioTrackIndexes
  796. {
  797. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  798. if (count <= 0)
  799. return @[];
  800. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  801. libvlc_track_description_t *currentTrack = firstTrack;
  802. NSMutableArray *tempArray = [NSMutableArray array];
  803. while (currentTrack) {
  804. [tempArray addObject:@(currentTrack->i_id)];
  805. currentTrack = currentTrack->p_next;
  806. }
  807. libvlc_track_description_list_release(firstTrack);
  808. return [NSArray arrayWithArray: tempArray];
  809. }
  810. - (int)numberOfAudioTracks
  811. {
  812. return libvlc_audio_get_track_count(_playerInstance);
  813. }
  814. - (void)setAudioChannel:(int)value
  815. {
  816. libvlc_audio_set_channel(_playerInstance, value);
  817. }
  818. - (int)audioChannel
  819. {
  820. return libvlc_audio_get_channel(_playerInstance);
  821. }
  822. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  823. {
  824. libvlc_audio_set_delay(_playerInstance, index);
  825. }
  826. - (NSInteger)currentAudioPlaybackDelay
  827. {
  828. return libvlc_audio_get_delay(_playerInstance);
  829. }
  830. #pragma mark -
  831. #pragma mark equalizer
  832. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  833. {
  834. if (!_equalizerInstance && equalizerEnabled) {
  835. if (!(_equalizerInstance = libvlc_audio_equalizer_new())) {
  836. NSAssert(_equalizerInstance, @"equalizer failed to initialize");
  837. return;
  838. }
  839. }
  840. _equalizerEnabled = equalizerEnabled;
  841. libvlc_media_player_set_equalizer(_playerInstance,
  842. equalizerEnabled ? _equalizerInstance : NULL);
  843. }
  844. - (BOOL)equalizerEnabled
  845. {
  846. return _equalizerEnabled;
  847. }
  848. - (NSArray *)equalizerProfiles
  849. {
  850. unsigned count = libvlc_audio_equalizer_get_preset_count();
  851. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  852. for (unsigned x = 0; x < count; x++)
  853. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  854. return [NSArray arrayWithArray:array];
  855. }
  856. - (void)resetEqualizerFromProfile:(unsigned)profile
  857. {
  858. BOOL wasactive = NO;
  859. if (_equalizerInstance) {
  860. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  861. libvlc_audio_equalizer_release(_equalizerInstance);
  862. _equalizerInstance = nil;
  863. wasactive = YES;
  864. }
  865. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  866. if (wasactive)
  867. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  868. }
  869. - (CGFloat)preAmplification
  870. {
  871. if (!_equalizerInstance)
  872. return 0.;
  873. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  874. }
  875. - (void)setPreAmplification:(CGFloat)preAmplification
  876. {
  877. if (!_equalizerInstance)
  878. _equalizerInstance = libvlc_audio_equalizer_new();
  879. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  880. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  881. }
  882. - (unsigned)numberOfBands
  883. {
  884. return libvlc_audio_equalizer_get_band_count();
  885. }
  886. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  887. {
  888. return libvlc_audio_equalizer_get_band_frequency(index);
  889. }
  890. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  891. {
  892. if (!_equalizerInstance)
  893. _equalizerInstance = libvlc_audio_equalizer_new();
  894. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  895. }
  896. - (CGFloat)amplificationOfBand:(unsigned int)index
  897. {
  898. if (!_equalizerInstance)
  899. return 0.;
  900. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  901. }
  902. #pragma mark -
  903. #pragma mark set/get media
  904. - (void)setMedia:(VLCMedia *)value
  905. {
  906. if (_media != value) {
  907. if (_media && [_media compare:value] == NSOrderedSame)
  908. return;
  909. _media = value;
  910. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  911. }
  912. }
  913. - (VLCMedia *)media
  914. {
  915. return _media;
  916. }
  917. #pragma mark -
  918. #pragma mark playback
  919. - (void)play
  920. {
  921. dispatch_async(_libVLCBackgroundQueue, ^{
  922. libvlc_media_player_play(_playerInstance);
  923. });
  924. }
  925. - (void)pause
  926. {
  927. // Pause the stream
  928. dispatch_async(_libVLCBackgroundQueue, ^{
  929. libvlc_media_player_set_pause(_playerInstance, 1);
  930. });
  931. }
  932. - (void)stop
  933. {
  934. dispatch_async(_libVLCBackgroundQueue, ^{
  935. libvlc_media_player_stop(_playerInstance);
  936. });
  937. }
  938. - (libvlc_video_viewpoint_t *)viewPoint
  939. {
  940. if (_viewpoint == NULL) {
  941. _viewpoint = libvlc_video_new_viewpoint();
  942. }
  943. return _viewpoint;
  944. }
  945. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  946. {
  947. if ([self viewPoint]) {
  948. [self viewPoint]->f_yaw = yaw;
  949. [self viewPoint]->f_pitch = pitch;
  950. [self viewPoint]->f_roll = roll;
  951. [self viewPoint]->f_field_of_view = fov;
  952. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  953. }
  954. return NO;
  955. }
  956. - (float)yaw
  957. {
  958. if ([self viewPoint]) {
  959. return [self viewPoint]->f_yaw;
  960. }
  961. return 0;
  962. }
  963. - (float)pitch
  964. {
  965. if ([self viewPoint]) {
  966. return [self viewPoint]->f_pitch;
  967. }
  968. return 0;
  969. }
  970. - (float)roll
  971. {
  972. if ([self viewPoint]) {
  973. return [self viewPoint]->f_roll;
  974. }
  975. return 0;
  976. }
  977. - (float)fov
  978. {
  979. if ([self viewPoint]) {
  980. return [self viewPoint]->f_field_of_view;
  981. }
  982. return 0;
  983. }
  984. - (void)gotoNextFrame
  985. {
  986. libvlc_media_player_next_frame(_playerInstance);
  987. }
  988. - (void)fastForward
  989. {
  990. [self fastForwardAtRate: 2.0];
  991. }
  992. - (void)fastForwardAtRate:(float)rate
  993. {
  994. [self setRate:rate];
  995. }
  996. - (void)rewind
  997. {
  998. [self rewindAtRate: 2.0];
  999. }
  1000. - (void)rewindAtRate:(float)rate
  1001. {
  1002. [self setRate: -rate];
  1003. }
  1004. - (void)jumpBackward:(int)interval
  1005. {
  1006. if ([self isSeekable]) {
  1007. interval = interval * 1000;
  1008. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  1009. }
  1010. }
  1011. - (void)jumpForward:(int)interval
  1012. {
  1013. if ([self isSeekable]) {
  1014. interval = interval * 1000;
  1015. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  1016. }
  1017. }
  1018. - (void)extraShortJumpBackward
  1019. {
  1020. [self jumpBackward:3];
  1021. }
  1022. - (void)extraShortJumpForward
  1023. {
  1024. [self jumpForward:3];
  1025. }
  1026. - (void)shortJumpBackward
  1027. {
  1028. [self jumpBackward:10];
  1029. }
  1030. - (void)shortJumpForward
  1031. {
  1032. [self jumpForward:10];
  1033. }
  1034. - (void)mediumJumpBackward
  1035. {
  1036. [self jumpBackward:60];
  1037. }
  1038. - (void)mediumJumpForward
  1039. {
  1040. [self jumpForward:60];
  1041. }
  1042. - (void)longJumpBackward
  1043. {
  1044. [self jumpBackward:300];
  1045. }
  1046. - (void)longJumpForward
  1047. {
  1048. [self jumpForward:300];
  1049. }
  1050. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  1051. {
  1052. libvlc_media_player_navigate(_playerInstance, action);
  1053. }
  1054. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  1055. {
  1056. return [NSSet setWithObjects:@"state", nil];
  1057. }
  1058. - (BOOL)isPlaying
  1059. {
  1060. return libvlc_media_player_is_playing(_playerInstance);
  1061. }
  1062. - (BOOL)willPlay
  1063. {
  1064. return libvlc_media_player_will_play(_playerInstance);
  1065. }
  1066. - (VLCMediaPlayerState)state
  1067. {
  1068. return _cachedState;
  1069. }
  1070. - (float)position
  1071. {
  1072. return _position;
  1073. }
  1074. - (void)setPosition:(float)newPosition
  1075. {
  1076. libvlc_media_player_set_position(_playerInstance, newPosition);
  1077. }
  1078. - (BOOL)isSeekable
  1079. {
  1080. return libvlc_media_player_is_seekable(_playerInstance);
  1081. }
  1082. - (BOOL)canPause
  1083. {
  1084. return libvlc_media_player_can_pause(_playerInstance);
  1085. }
  1086. - (NSArray *)snapshots
  1087. {
  1088. return [_snapshots copy];
  1089. }
  1090. #if TARGET_OS_IPHONE
  1091. - (UIImage *)lastSnapshot {
  1092. if (_snapshots == nil) {
  1093. return nil;
  1094. }
  1095. @synchronized(_snapshots) {
  1096. if (_snapshots.count == 0)
  1097. return nil;
  1098. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1099. }
  1100. }
  1101. #else
  1102. - (NSImage *)lastSnapshot {
  1103. if (_snapshots == nil) {
  1104. return nil;
  1105. }
  1106. @synchronized(_snapshots) {
  1107. if (_snapshots.count == 0)
  1108. return nil;
  1109. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1110. }
  1111. }
  1112. #endif
  1113. - (void *)libVLCMediaPlayer
  1114. {
  1115. return _playerInstance;
  1116. }
  1117. - (BOOL)startRecordingAtPath:(NSString *)path
  1118. {
  1119. return libvlc_media_player_record(_playerInstance, YES, [path UTF8String]);
  1120. }
  1121. - (BOOL)stopRecording
  1122. {
  1123. return libvlc_media_player_record(_playerInstance, NO, nil);
  1124. }
  1125. #pragma mark -
  1126. #pragma mark - Renderer
  1127. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1128. {
  1129. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1130. }
  1131. @end
  1132. @implementation VLCMediaPlayer (Private)
  1133. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1134. {
  1135. if (self = [super init]) {
  1136. _cachedTime = [VLCTime nullTime];
  1137. _cachedRemainingTime = [VLCTime nullTime];
  1138. _position = 0.0f;
  1139. _cachedState = VLCMediaPlayerStateStopped;
  1140. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1141. // Create a media instance, it doesn't matter what library we start off with
  1142. // it will change depending on the media descriptor provided to the media
  1143. // instance
  1144. if (options && options.count > 0) {
  1145. VKLog(@"creating player instance with private library as options were given");
  1146. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1147. } else {
  1148. VKLog(@"creating player instance using shared library");
  1149. _privateLibrary = [VLCLibrary sharedLibrary];
  1150. }
  1151. libvlc_retain([_privateLibrary instance]);
  1152. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1153. if (_playerInstance == NULL) {
  1154. NSAssert(0, @"%s: player initialization failed", __PRETTY_FUNCTION__);
  1155. libvlc_release([_privateLibrary instance]);
  1156. return nil;
  1157. }
  1158. [self registerObservers];
  1159. [self setDrawable:aDrawable];
  1160. }
  1161. return self;
  1162. }
  1163. - (void)registerObservers
  1164. {
  1165. // Attach event observers into the media instance
  1166. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1167. if (!p_em)
  1168. return;
  1169. /* We need the caller to wait until this block is done.
  1170. * The initialized object shall not be returned until the event attachments are done. */
  1171. dispatch_sync(_libVLCBackgroundQueue,^{
  1172. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1173. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1174. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1175. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1176. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1177. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1178. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1179. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1180. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1181. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1182. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1183. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1184. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1185. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1186. libvlc_event_attach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1187. });
  1188. }
  1189. - (void)unregisterObservers
  1190. {
  1191. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1192. if (!p_em)
  1193. return;
  1194. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1195. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1196. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1197. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1198. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1199. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1200. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1201. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1202. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1203. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1204. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1205. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1206. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1207. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1208. libvlc_event_detach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1209. }
  1210. - (dispatch_queue_t)libVLCBackgroundQueue
  1211. {
  1212. if (!_libVLCBackgroundQueue) {
  1213. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1214. }
  1215. return _libVLCBackgroundQueue;
  1216. }
  1217. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1218. {
  1219. [self willChangeValueForKey:@"time"];
  1220. [self willChangeValueForKey:@"remainingTime"];
  1221. _cachedTime = [VLCTime timeWithNumber:newTime];
  1222. double currentTime = [[_cachedTime numberValue] doubleValue];
  1223. if (currentTime > 0 && _position > 0.) {
  1224. double remaining = currentTime / _position * (1 - _position);
  1225. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1226. } else
  1227. _cachedRemainingTime = [VLCTime nullTime];
  1228. [self didChangeValueForKey:@"remainingTime"];
  1229. [self didChangeValueForKey:@"time"];
  1230. }
  1231. #if !TARGET_OS_IPHONE
  1232. - (void)delaySleep
  1233. {
  1234. UpdateSystemActivity(UsrActivity);
  1235. }
  1236. #endif
  1237. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1238. {
  1239. #if !TARGET_OS_IPHONE
  1240. // This seems to be the most relevant place to delay sleeping and screen saver.
  1241. [self delaySleep];
  1242. #endif
  1243. [self willChangeValueForKey:@"position"];
  1244. _position = [newPosition floatValue];
  1245. [self didChangeValueForKey:@"position"];
  1246. }
  1247. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1248. {
  1249. [self willChangeValueForKey:@"state"];
  1250. _cachedState = [newState intValue];
  1251. #if TARGET_OS_IPHONE
  1252. // Disable idle timer if player is playing media
  1253. // Exclusion can be made for audio only media
  1254. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1255. #endif
  1256. [self didChangeValueForKey:@"state"];
  1257. }
  1258. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1259. {
  1260. [self willChangeValueForKey:@"media"];
  1261. if (_media != newMedia) {
  1262. _media = newMedia;
  1263. [self willChangeValueForKey:@"time"];
  1264. [self willChangeValueForKey:@"remainingTime"];
  1265. [self willChangeValueForKey:@"position"];
  1266. _cachedTime = [VLCTime nullTime];
  1267. _cachedRemainingTime = [VLCTime nullTime];
  1268. _position = 0.0f;
  1269. [self didChangeValueForKey:@"position"];
  1270. [self didChangeValueForKey:@"remainingTime"];
  1271. [self didChangeValueForKey:@"time"];
  1272. }
  1273. [self didChangeValueForKey:@"media"];
  1274. }
  1275. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1276. {
  1277. [self willChangeValueForKey:@"currentTitleIndex"];
  1278. [self didChangeValueForKey:@"currentTitleIndex"];
  1279. }
  1280. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1281. {
  1282. [self willChangeValueForKey:@"currentChapterIndex"];
  1283. [self didChangeValueForKey:@"currentChapterIndex"];
  1284. }
  1285. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1286. {
  1287. @synchronized(_snapshots) {
  1288. if (!_snapshots) {
  1289. _snapshots = [NSMutableArray array];
  1290. }
  1291. [_snapshots addObject:fileName];
  1292. }
  1293. }
  1294. - (void)mediaPlayerRecordChanged:(NSArray *)arguments
  1295. {
  1296. NSString *filePath = arguments.firstObject[@"filePath"];
  1297. BOOL isRecording = [arguments.firstObject[@"isRecording"] boolValue];
  1298. if (isRecording) {
  1299. if ([_delegate respondsToSelector:@selector(mediaPlayerStartedRecording:)]) {
  1300. [_delegate mediaPlayerStartedRecording:self];
  1301. }
  1302. } else {
  1303. if ([_delegate respondsToSelector:@selector(mediaPlayer:recordingStoppedAtPath:)]) {
  1304. [_delegate mediaPlayer:self recordingStoppedAtPath:filePath];
  1305. }
  1306. }
  1307. }
  1308. @end