VLCMediaPlayer.m 46 KB

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