VLCMediaPlayer.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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, VLCDeinterlaceOff, NULL);
  482. else
  483. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOn, [name UTF8String]);
  484. }
  485. - (void)setDeinterlace:(VLCDeinterlace)deinterlace withFilter:(NSString *)name
  486. {
  487. libvlc_video_set_deinterlace(_playerInstance, deinterlace, [name UTF8String]);
  488. }
  489. - (BOOL)adjustFilterEnabled
  490. {
  491. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  492. }
  493. - (void)setAdjustFilterEnabled:(BOOL)b_value
  494. {
  495. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  496. }
  497. - (float)contrast
  498. {
  499. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  500. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  501. }
  502. - (void)setContrast:(float)f_value
  503. {
  504. if (f_value <= 2. && f_value >= 0.) {
  505. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  506. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  507. }
  508. }
  509. - (float)brightness
  510. {
  511. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  512. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  513. }
  514. - (void)setBrightness:(float)f_value
  515. {
  516. if (f_value <= 2. && f_value >= 0.) {
  517. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  518. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  519. }
  520. }
  521. - (float)hue
  522. {
  523. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  524. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  525. }
  526. - (void)setHue:(float)f_value
  527. {
  528. if (f_value <= 180. && f_value >= -180.) {
  529. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  530. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  531. }
  532. }
  533. - (float)saturation
  534. {
  535. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  536. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  537. }
  538. - (void)setSaturation:(float)f_value
  539. {
  540. if (f_value <= 3. && f_value >= 0.) {
  541. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  542. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  543. }
  544. }
  545. - (float)gamma
  546. {
  547. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  548. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  549. }
  550. - (void)setGamma:(float)f_value
  551. {
  552. if (f_value <= 10. && f_value >= 0.) {
  553. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  554. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  555. }
  556. }
  557. - (void)setRate:(float)value
  558. {
  559. libvlc_media_player_set_rate(_playerInstance, value);
  560. }
  561. - (float)rate
  562. {
  563. return libvlc_media_player_get_rate(_playerInstance);
  564. }
  565. - (CGSize)videoSize
  566. {
  567. unsigned height = 0, width = 0;
  568. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  569. if (failure)
  570. return CGSizeZero;
  571. return CGSizeMake(width, height);
  572. }
  573. - (BOOL)hasVideoOut
  574. {
  575. return libvlc_media_player_has_vout(_playerInstance);
  576. }
  577. - (float)framesPerSecond
  578. {
  579. return .0;
  580. }
  581. - (void)setTime:(VLCTime *)value
  582. {
  583. // Time is managed in seconds, while duration is managed in microseconds
  584. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  585. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0);
  586. }
  587. - (VLCTime *)time
  588. {
  589. return _cachedTime;
  590. }
  591. - (VLCTime *)remainingTime
  592. {
  593. return _cachedRemainingTime;
  594. }
  595. #pragma mark -
  596. #pragma mark Chapters
  597. - (void)setCurrentChapterIndex:(int)value;
  598. {
  599. libvlc_media_player_set_chapter(_playerInstance, value);
  600. }
  601. - (int)currentChapterIndex
  602. {
  603. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  604. if (count <= 0)
  605. return -1;
  606. int result = libvlc_media_player_get_chapter(_playerInstance);
  607. return result;
  608. }
  609. - (void)nextChapter
  610. {
  611. libvlc_media_player_next_chapter(_playerInstance);
  612. }
  613. - (void)previousChapter
  614. {
  615. libvlc_media_player_previous_chapter(_playerInstance);
  616. }
  617. - (NSArray *)chaptersForTitleIndex:(int)title
  618. {
  619. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  620. if (count <= 0)
  621. return @[];
  622. #pragma clang diagnostic push
  623. #pragma clang diagnostic ignored "-Wdeprecated"
  624. libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
  625. libvlc_track_description_t *currentTrack = firstTrack;
  626. #pragma clang diagnostic push
  627. NSMutableArray *tempArray = [NSMutableArray array];
  628. for (NSInteger i = 0; i < count ; i++) {
  629. [tempArray addObject:@(currentTrack->psz_name)];
  630. currentTrack = currentTrack->p_next;
  631. }
  632. libvlc_track_description_list_release(firstTrack);
  633. return [NSArray arrayWithArray:tempArray];
  634. }
  635. #pragma mark -
  636. #pragma mark Titles
  637. - (void)setCurrentTitleIndex:(int)value
  638. {
  639. libvlc_media_player_set_title(_playerInstance, value);
  640. }
  641. - (int)currentTitleIndex
  642. {
  643. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  644. if (count <= 0)
  645. return -1;
  646. return libvlc_media_player_get_title(_playerInstance);
  647. }
  648. - (int)numberOfTitles
  649. {
  650. return libvlc_media_player_get_title_count(_playerInstance);
  651. }
  652. - (NSUInteger)countOfTitles
  653. {
  654. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  655. return result;
  656. }
  657. - (NSArray *)titles
  658. {
  659. NSUInteger count = [self countOfTitles];
  660. if (count == 0)
  661. return [NSArray array];
  662. #pragma clang diagnostic push
  663. #pragma clang diagnostic ignored "-Wdeprecated"
  664. libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
  665. libvlc_track_description_t *currentTrack = firstTrack;
  666. #pragma clang diagnostic pop
  667. if (!currentTrack)
  668. return [NSArray array];
  669. NSMutableArray *tempArray = [NSMutableArray array];
  670. while (1) {
  671. if (currentTrack->psz_name != nil)
  672. [tempArray addObject:@(currentTrack->psz_name)];
  673. if (currentTrack->p_next)
  674. currentTrack = currentTrack->p_next;
  675. else
  676. break;
  677. }
  678. libvlc_track_description_list_release(firstTrack);
  679. return [NSArray arrayWithArray: tempArray];
  680. }
  681. - (NSArray *)titleDescriptions
  682. {
  683. libvlc_title_description_t **titleInfo;
  684. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  685. if (numberOfTitleDescriptions < 0)
  686. return [NSArray array];
  687. if (numberOfTitleDescriptions == 0) {
  688. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  689. return [NSArray array];
  690. }
  691. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  692. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  693. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  694. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  695. VLCTitleDescriptionDuration,
  696. @(titleInfo[i]->i_flags & libvlc_title_menu),
  697. VLCTitleDescriptionIsMenu,
  698. nil];
  699. if (titleInfo[i]->psz_name != NULL)
  700. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  701. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  702. }
  703. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  704. return [NSArray arrayWithArray:array];
  705. }
  706. - (int)indexOfLongestTitle
  707. {
  708. NSArray *titles = [self titleDescriptions];
  709. NSUInteger titleCount = titles.count;
  710. int currentlyFoundTitle = 0;
  711. int64_t currentlySelectedDuration = 0;
  712. int64_t randomTitleDuration = 0;
  713. for (int x = 0; x < titleCount; x++) {
  714. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  715. if (randomTitleDuration > currentlySelectedDuration) {
  716. currentlySelectedDuration = randomTitleDuration;
  717. currentlyFoundTitle = x;
  718. }
  719. }
  720. return currentlyFoundTitle;
  721. }
  722. - (int)numberOfChaptersForTitle:(int)titleIndex
  723. {
  724. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  725. }
  726. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  727. {
  728. libvlc_chapter_description_t **chapterDescriptions;
  729. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  730. titleIndex,
  731. &chapterDescriptions);
  732. if (numberOfChapterDescriptions < 0)
  733. return [NSArray array];
  734. if (numberOfChapterDescriptions == 0) {
  735. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  736. return [NSArray array];
  737. }
  738. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  739. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  740. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  741. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  742. VLCChapterDescriptionDuration,
  743. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  744. VLCChapterDescriptionTimeOffset,
  745. nil];
  746. if (chapterDescriptions[i]->psz_name != NULL)
  747. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  748. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  749. }
  750. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  751. return [NSArray arrayWithArray:array];
  752. }
  753. #pragma mark -
  754. #pragma mark Audio tracks
  755. - (void)setCurrentAudioTrackIndex:(int)value
  756. {
  757. libvlc_audio_set_track(_playerInstance, value);
  758. }
  759. - (int)currentAudioTrackIndex
  760. {
  761. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  762. if (count <= 0)
  763. return -1;
  764. return libvlc_audio_get_track(_playerInstance);
  765. }
  766. - (NSArray *)audioTrackNames
  767. {
  768. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  769. if (count <= 0)
  770. return @[];
  771. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  772. libvlc_track_description_t *currentTrack = firstTrack;
  773. NSMutableArray *tempArray = [NSMutableArray array];
  774. while (currentTrack) {
  775. NSString *track = @(currentTrack->psz_name);
  776. [tempArray addObject:track != nil ? track : @""];
  777. currentTrack = currentTrack->p_next;
  778. }
  779. libvlc_track_description_list_release(firstTrack);
  780. return [NSArray arrayWithArray: tempArray];
  781. }
  782. - (NSArray *)audioTrackIndexes
  783. {
  784. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  785. if (count <= 0)
  786. return @[];
  787. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  788. libvlc_track_description_t *currentTrack = firstTrack;
  789. NSMutableArray *tempArray = [NSMutableArray array];
  790. while (currentTrack) {
  791. [tempArray addObject:@(currentTrack->i_id)];
  792. currentTrack = currentTrack->p_next;
  793. }
  794. libvlc_track_description_list_release(firstTrack);
  795. return [NSArray arrayWithArray: tempArray];
  796. }
  797. - (int)numberOfAudioTracks
  798. {
  799. return libvlc_audio_get_track_count(_playerInstance);
  800. }
  801. - (void)setAudioChannel:(int)value
  802. {
  803. libvlc_audio_set_channel(_playerInstance, value);
  804. }
  805. - (int)audioChannel
  806. {
  807. return libvlc_audio_get_channel(_playerInstance);
  808. }
  809. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  810. {
  811. libvlc_audio_set_delay(_playerInstance, index);
  812. }
  813. - (NSInteger)currentAudioPlaybackDelay
  814. {
  815. return libvlc_audio_get_delay(_playerInstance);
  816. }
  817. #pragma mark -
  818. #pragma mark equalizer
  819. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  820. {
  821. if (!_equalizerInstance) {
  822. if (!(_equalizerInstance = libvlc_audio_equalizer_new())) {
  823. NSAssert(_equalizerInstance, @"equalizer failed to initialize");
  824. return;
  825. }
  826. }
  827. _equalizerEnabled = equalizerEnabled;
  828. libvlc_media_player_set_equalizer(_playerInstance,
  829. equalizerEnabled ? _equalizerInstance : NULL);
  830. }
  831. - (BOOL)equalizerEnabled
  832. {
  833. return _equalizerEnabled;
  834. }
  835. - (NSArray *)equalizerProfiles
  836. {
  837. unsigned count = libvlc_audio_equalizer_get_preset_count();
  838. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  839. for (unsigned x = 0; x < count; x++)
  840. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  841. return [NSArray arrayWithArray:array];
  842. }
  843. - (void)resetEqualizerFromProfile:(unsigned)profile
  844. {
  845. BOOL wasactive = NO;
  846. if (_equalizerInstance) {
  847. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  848. libvlc_audio_equalizer_release(_equalizerInstance);
  849. wasactive = YES;
  850. }
  851. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  852. if (wasactive)
  853. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  854. }
  855. - (CGFloat)preAmplification
  856. {
  857. if (!_equalizerInstance)
  858. return 0.;
  859. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  860. }
  861. - (void)setPreAmplification:(CGFloat)preAmplification
  862. {
  863. if (!_equalizerInstance)
  864. _equalizerInstance = libvlc_audio_equalizer_new();
  865. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  866. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  867. }
  868. - (unsigned)numberOfBands
  869. {
  870. return libvlc_audio_equalizer_get_band_count();
  871. }
  872. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  873. {
  874. return libvlc_audio_equalizer_get_band_frequency(index);
  875. }
  876. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  877. {
  878. if (!_equalizerInstance)
  879. _equalizerInstance = libvlc_audio_equalizer_new();
  880. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  881. }
  882. - (CGFloat)amplificationOfBand:(unsigned int)index
  883. {
  884. if (!_equalizerInstance)
  885. return 0.;
  886. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  887. }
  888. #pragma mark -
  889. #pragma mark set/get media
  890. - (void)setMedia:(VLCMedia *)value
  891. {
  892. if (_media != value) {
  893. if (_media && [_media compare:value] == NSOrderedSame)
  894. return;
  895. _media = value;
  896. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  897. }
  898. }
  899. - (VLCMedia *)media
  900. {
  901. return _media;
  902. }
  903. #pragma mark -
  904. #pragma mark playback
  905. - (void)play
  906. {
  907. dispatch_async(_libVLCBackgroundQueue, ^{
  908. libvlc_media_player_play(_playerInstance);
  909. });
  910. }
  911. - (void)pause
  912. {
  913. // Pause the stream
  914. dispatch_async(_libVLCBackgroundQueue, ^{
  915. libvlc_media_player_set_pause(_playerInstance, 1);
  916. });
  917. }
  918. - (void)stop
  919. {
  920. dispatch_async(_libVLCBackgroundQueue, ^{
  921. libvlc_media_player_stop(_playerInstance);
  922. });
  923. }
  924. - (libvlc_video_viewpoint_t *)viewPoint
  925. {
  926. if (_viewpoint == NULL) {
  927. _viewpoint = libvlc_video_new_viewpoint();
  928. }
  929. return _viewpoint;
  930. }
  931. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  932. {
  933. if ([self viewPoint]) {
  934. [self viewPoint]->f_yaw = yaw;
  935. [self viewPoint]->f_pitch = pitch;
  936. [self viewPoint]->f_roll = roll;
  937. [self viewPoint]->f_field_of_view = fov;
  938. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  939. }
  940. return NO;
  941. }
  942. - (float)yaw
  943. {
  944. if ([self viewPoint]) {
  945. return [self viewPoint]->f_yaw;
  946. }
  947. return 0;
  948. }
  949. - (float)pitch
  950. {
  951. if ([self viewPoint]) {
  952. return [self viewPoint]->f_pitch;
  953. }
  954. return 0;
  955. }
  956. - (float)roll
  957. {
  958. if ([self viewPoint]) {
  959. return [self viewPoint]->f_roll;
  960. }
  961. return 0;
  962. }
  963. - (float)fov
  964. {
  965. if ([self viewPoint]) {
  966. return [self viewPoint]->f_field_of_view;
  967. }
  968. return 0;
  969. }
  970. - (void)gotoNextFrame
  971. {
  972. libvlc_media_player_next_frame(_playerInstance);
  973. }
  974. - (void)fastForward
  975. {
  976. [self fastForwardAtRate: 2.0];
  977. }
  978. - (void)fastForwardAtRate:(float)rate
  979. {
  980. [self setRate:rate];
  981. }
  982. - (void)rewind
  983. {
  984. [self rewindAtRate: 2.0];
  985. }
  986. - (void)rewindAtRate:(float)rate
  987. {
  988. [self setRate: -rate];
  989. }
  990. - (void)jumpBackward:(int)interval
  991. {
  992. if ([self isSeekable]) {
  993. interval = interval * 1000;
  994. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  995. }
  996. }
  997. - (void)jumpForward:(int)interval
  998. {
  999. if ([self isSeekable]) {
  1000. interval = interval * 1000;
  1001. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  1002. }
  1003. }
  1004. - (void)extraShortJumpBackward
  1005. {
  1006. [self jumpBackward:3];
  1007. }
  1008. - (void)extraShortJumpForward
  1009. {
  1010. [self jumpForward:3];
  1011. }
  1012. - (void)shortJumpBackward
  1013. {
  1014. [self jumpBackward:10];
  1015. }
  1016. - (void)shortJumpForward
  1017. {
  1018. [self jumpForward:10];
  1019. }
  1020. - (void)mediumJumpBackward
  1021. {
  1022. [self jumpBackward:60];
  1023. }
  1024. - (void)mediumJumpForward
  1025. {
  1026. [self jumpForward:60];
  1027. }
  1028. - (void)longJumpBackward
  1029. {
  1030. [self jumpBackward:300];
  1031. }
  1032. - (void)longJumpForward
  1033. {
  1034. [self jumpForward:300];
  1035. }
  1036. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  1037. {
  1038. libvlc_media_player_navigate(_playerInstance, action);
  1039. }
  1040. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  1041. {
  1042. return [NSSet setWithObjects:@"state", nil];
  1043. }
  1044. - (BOOL)isPlaying
  1045. {
  1046. return libvlc_media_player_is_playing(_playerInstance);
  1047. }
  1048. - (BOOL)willPlay
  1049. {
  1050. return libvlc_media_player_will_play(_playerInstance);
  1051. }
  1052. - (VLCMediaPlayerState)state
  1053. {
  1054. return _cachedState;
  1055. }
  1056. - (float)position
  1057. {
  1058. return _position;
  1059. }
  1060. - (void)setPosition:(float)newPosition
  1061. {
  1062. libvlc_media_player_set_position(_playerInstance, newPosition);
  1063. }
  1064. - (BOOL)isSeekable
  1065. {
  1066. return libvlc_media_player_is_seekable(_playerInstance);
  1067. }
  1068. - (BOOL)canPause
  1069. {
  1070. return libvlc_media_player_can_pause(_playerInstance);
  1071. }
  1072. - (NSArray *)snapshots
  1073. {
  1074. return [_snapshots copy];
  1075. }
  1076. #if TARGET_OS_IPHONE
  1077. - (UIImage *)lastSnapshot {
  1078. if (_snapshots == nil) {
  1079. return nil;
  1080. }
  1081. @synchronized(_snapshots) {
  1082. if (_snapshots.count == 0)
  1083. return nil;
  1084. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1085. }
  1086. }
  1087. #else
  1088. - (NSImage *)lastSnapshot {
  1089. if (_snapshots == nil) {
  1090. return nil;
  1091. }
  1092. @synchronized(_snapshots) {
  1093. if (_snapshots.count == 0)
  1094. return nil;
  1095. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1096. }
  1097. }
  1098. #endif
  1099. - (void *)libVLCMediaPlayer
  1100. {
  1101. return _playerInstance;
  1102. }
  1103. #pragma mark -
  1104. #pragma mark - Renderer
  1105. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1106. {
  1107. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1108. }
  1109. @end
  1110. @implementation VLCMediaPlayer (Private)
  1111. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1112. {
  1113. if (self = [super init]) {
  1114. _cachedTime = [VLCTime nullTime];
  1115. _cachedRemainingTime = [VLCTime nullTime];
  1116. _position = 0.0f;
  1117. _cachedState = VLCMediaPlayerStateStopped;
  1118. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1119. // Create a media instance, it doesn't matter what library we start off with
  1120. // it will change depending on the media descriptor provided to the media
  1121. // instance
  1122. if (options && options.count > 0) {
  1123. VKLog(@"creating player instance with private library as options were given");
  1124. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1125. } else {
  1126. VKLog(@"creating player instance using shared library");
  1127. _privateLibrary = [VLCLibrary sharedLibrary];
  1128. }
  1129. libvlc_retain([_privateLibrary instance]);
  1130. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1131. [self registerObservers];
  1132. [self setDrawable:aDrawable];
  1133. }
  1134. return self;
  1135. }
  1136. - (void)registerObservers
  1137. {
  1138. // Attach event observers into the media instance
  1139. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1140. if (!p_em)
  1141. return;
  1142. /* We need the caller to wait until this block is done.
  1143. * The initialized object shall not be returned until the event attachments are done. */
  1144. dispatch_sync(_libVLCBackgroundQueue,^{
  1145. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1146. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1147. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1148. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1149. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1150. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1151. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1152. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1153. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1154. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1155. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1156. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1157. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1158. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1159. });
  1160. }
  1161. - (void)unregisterObservers
  1162. {
  1163. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1164. if (!p_em)
  1165. return;
  1166. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1167. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1168. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1169. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1170. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1171. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1172. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1173. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1174. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1175. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1176. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1177. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1178. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1179. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1180. }
  1181. - (dispatch_queue_t)libVLCBackgroundQueue
  1182. {
  1183. if (!_libVLCBackgroundQueue) {
  1184. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1185. }
  1186. return _libVLCBackgroundQueue;
  1187. }
  1188. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1189. {
  1190. [self willChangeValueForKey:@"time"];
  1191. [self willChangeValueForKey:@"remainingTime"];
  1192. _cachedTime = [VLCTime timeWithNumber:newTime];
  1193. double currentTime = [[_cachedTime numberValue] doubleValue];
  1194. if (currentTime > 0 && _position > 0.) {
  1195. double remaining = currentTime / _position * (1 - _position);
  1196. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1197. } else
  1198. _cachedRemainingTime = [VLCTime nullTime];
  1199. [self didChangeValueForKey:@"remainingTime"];
  1200. [self didChangeValueForKey:@"time"];
  1201. }
  1202. #if !TARGET_OS_IPHONE
  1203. - (void)delaySleep
  1204. {
  1205. UpdateSystemActivity(UsrActivity);
  1206. }
  1207. #endif
  1208. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1209. {
  1210. #if !TARGET_OS_IPHONE
  1211. // This seems to be the most relevant place to delay sleeping and screen saver.
  1212. [self delaySleep];
  1213. #endif
  1214. [self willChangeValueForKey:@"position"];
  1215. _position = [newPosition floatValue];
  1216. [self didChangeValueForKey:@"position"];
  1217. }
  1218. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1219. {
  1220. [self willChangeValueForKey:@"state"];
  1221. _cachedState = [newState intValue];
  1222. #if TARGET_OS_IPHONE
  1223. // Disable idle timer if player is playing media
  1224. // Exclusion can be made for audio only media
  1225. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1226. #endif
  1227. [self didChangeValueForKey:@"state"];
  1228. }
  1229. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1230. {
  1231. [self willChangeValueForKey:@"media"];
  1232. if (_media != newMedia) {
  1233. _media = newMedia;
  1234. [self willChangeValueForKey:@"time"];
  1235. [self willChangeValueForKey:@"remainingTime"];
  1236. [self willChangeValueForKey:@"position"];
  1237. _cachedTime = [VLCTime nullTime];
  1238. _cachedRemainingTime = [VLCTime nullTime];
  1239. _position = 0.0f;
  1240. [self didChangeValueForKey:@"position"];
  1241. [self didChangeValueForKey:@"remainingTime"];
  1242. [self didChangeValueForKey:@"time"];
  1243. }
  1244. [self didChangeValueForKey:@"media"];
  1245. }
  1246. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1247. {
  1248. [self willChangeValueForKey:@"currentTitleIndex"];
  1249. [self didChangeValueForKey:@"currentTitleIndex"];
  1250. }
  1251. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1252. {
  1253. [self willChangeValueForKey:@"currentChapterIndex"];
  1254. [self didChangeValueForKey:@"currentChapterIndex"];
  1255. }
  1256. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1257. {
  1258. @synchronized(_snapshots) {
  1259. if (!_snapshots) {
  1260. _snapshots = [NSMutableArray array];
  1261. }
  1262. [_snapshots addObject:fileName];
  1263. }
  1264. }
  1265. @end