VLCMediaPlayer.m 47 KB

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