VLCMediaPlayer.m 44 KB

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