VLCMediaPlayer.m 46 KB

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