VLCMediaPlayer.m 47 KB

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