VLCMediaPlayer.m 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  74. {
  75. @autoreleasepool {
  76. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  77. withMethod:@selector(mediaPlayerTimeChanged:)
  78. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  79. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  80. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  81. withNotificationName:VLCMediaPlayerTimeChanged];
  82. }
  83. }
  84. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  85. {
  86. @autoreleasepool {
  87. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  88. withMethod:@selector(mediaPlayerPositionChanged:)
  89. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  90. }
  91. }
  92. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  93. {
  94. VLCMediaPlayerState newState;
  95. if (event->type == libvlc_MediaPlayerPlaying)
  96. newState = VLCMediaPlayerStatePlaying;
  97. else if (event->type == libvlc_MediaPlayerPaused)
  98. newState = VLCMediaPlayerStatePaused;
  99. else if (event->type == libvlc_MediaPlayerEndReached || event->type == libvlc_MediaPlayerStopped)
  100. newState = VLCMediaPlayerStateStopped;
  101. else if (event->type == libvlc_MediaPlayerEncounteredError)
  102. newState = VLCMediaPlayerStateError;
  103. else if (event->type == libvlc_MediaPlayerBuffering)
  104. newState = VLCMediaPlayerStateBuffering;
  105. else if (event->type == libvlc_MediaPlayerOpening)
  106. newState = VLCMediaPlayerStateOpening;
  107. else {
  108. VKLog(@"%s: Unknown event", __FUNCTION__);
  109. return;
  110. }
  111. @autoreleasepool {
  112. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  113. withMethod:@selector(mediaPlayerStateChanged:)
  114. withArgumentAsObject:@(newState)];
  115. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  116. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  117. withNotificationName:VLCMediaPlayerStateChanged];
  118. }
  119. }
  120. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  121. {
  122. @autoreleasepool {
  123. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  124. withMethod:@selector(mediaPlayerMediaChanged:)
  125. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  126. }
  127. }
  128. static void HandleMediaTitleChanged(const libvlc_event_t * event, void * self)
  129. {
  130. @autoreleasepool {
  131. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  132. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  133. withNotificationName:VLCMediaPlayerTitleChanged];
  134. }
  135. }
  136. static void HandleMediaChapterChanged(const libvlc_event_t * event, void * self)
  137. {
  138. @autoreleasepool {
  139. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  140. withDelegateMethod:@selector(mediaPlayerChapterChanged:)
  141. withNotificationName:VLCMediaPlayerChapterChanged];
  142. }
  143. }
  144. #if TARGET_OS_IPHONE
  145. static void HandleMediaPlayerSnapshot(const libvlc_event_t * event, void * self)
  146. {
  147. @autoreleasepool {
  148. if (event->u.media_player_snapshot_taken.psz_filename != NULL) {
  149. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  150. withMethod:@selector(mediaPlayerSnapshot:)
  151. withArgumentAsObject:[NSString stringWithUTF8String:event->u.media_player_snapshot_taken.psz_filename]];
  152. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  153. withDelegateMethod:@selector(mediaPlayerSnapshot:)
  154. withNotificationName:VLCMediaPlayerSnapshotTaken];
  155. }
  156. }
  157. }
  158. #endif
  159. // TODO: Documentation
  160. @interface VLCMediaPlayer (Private)
  161. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  162. - (void)registerObservers;
  163. - (void)unregisterObservers;
  164. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  165. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  166. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  167. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  168. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle;
  169. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter;
  170. #if TARGET_OS_IPHONE
  171. - (void)mediaPlayerSnapshot:(NSString *)fileName;
  172. #endif
  173. @end
  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. [tempArray addObject:@(currentTrack->psz_name)];
  369. currentTrack = currentTrack->p_next;
  370. }
  371. libvlc_track_description_list_release(firstTrack);
  372. return [NSArray arrayWithArray: tempArray];
  373. }
  374. - (NSArray *)videoSubTitlesIndexes
  375. {
  376. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  377. if (count <= 0)
  378. return @[];
  379. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  380. libvlc_track_description_t *currentTrack = firstTrack;
  381. NSMutableArray *tempArray = [NSMutableArray array];
  382. while (currentTrack) {
  383. [tempArray addObject:@(currentTrack->i_id)];
  384. currentTrack = currentTrack->p_next;
  385. }
  386. libvlc_track_description_list_release(firstTrack);
  387. return [NSArray arrayWithArray: tempArray];
  388. }
  389. - (int)numberOfSubtitlesTracks
  390. {
  391. return libvlc_video_get_spu_count(_playerInstance);
  392. }
  393. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  394. {
  395. return libvlc_video_set_subtitle_file(_playerInstance, [path UTF8String]);
  396. }
  397. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  398. {
  399. libvlc_video_set_spu_delay(_playerInstance, index);
  400. }
  401. - (NSInteger)currentVideoSubTitleDelay
  402. {
  403. return libvlc_video_get_spu_delay(_playerInstance);
  404. }
  405. #if TARGET_OS_IPHONE
  406. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  407. {
  408. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  409. }
  410. - (void)setTextRendererFont:(NSString *)fontname
  411. {
  412. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  413. }
  414. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  415. {
  416. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  417. }
  418. - (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
  419. {
  420. libvlc_video_set_textrenderer_bool(_playerInstance, libvlc_textrender_fontforcebold, [fontForceBold boolValue]);
  421. }
  422. #endif
  423. #pragma mark -
  424. #pragma mark Video Crop geometry
  425. - (void)setVideoCropGeometry:(char *)value
  426. {
  427. libvlc_video_set_crop_geometry(_playerInstance, value);
  428. }
  429. - (char *)videoCropGeometry
  430. {
  431. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  432. return result;
  433. }
  434. - (void)setVideoAspectRatio:(char *)value
  435. {
  436. libvlc_video_set_aspect_ratio(_playerInstance, value);
  437. }
  438. - (char *)videoAspectRatio
  439. {
  440. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  441. return result;
  442. }
  443. - (void)setScaleFactor:(float)value
  444. {
  445. libvlc_video_set_scale(_playerInstance, value);
  446. }
  447. - (float)scaleFactor
  448. {
  449. return libvlc_video_get_scale(_playerInstance);
  450. }
  451. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  452. {
  453. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  454. if (failure)
  455. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  456. }
  457. - (void)setDeinterlaceFilter:(NSString *)name
  458. {
  459. if (!name || name.length < 1)
  460. libvlc_video_set_deinterlace(_playerInstance, NULL);
  461. else
  462. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  463. }
  464. - (BOOL)adjustFilterEnabled
  465. {
  466. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  467. }
  468. - (void)setAdjustFilterEnabled:(BOOL)b_value
  469. {
  470. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  471. }
  472. - (float)contrast
  473. {
  474. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  475. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  476. }
  477. - (void)setContrast:(float)f_value
  478. {
  479. if (f_value <= 2. && f_value >= 0.) {
  480. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  481. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  482. }
  483. }
  484. - (float)brightness
  485. {
  486. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  487. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  488. }
  489. - (void)setBrightness:(float)f_value
  490. {
  491. if (f_value <= 2. && f_value >= 0.) {
  492. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  493. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  494. }
  495. }
  496. - (int)hue
  497. {
  498. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  499. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Hue);
  500. }
  501. - (void)setHue:(int)i_value
  502. {
  503. if (i_value <= 360 && i_value >= 0) {
  504. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  505. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Hue, i_value);
  506. }
  507. }
  508. - (float)saturation
  509. {
  510. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  511. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  512. }
  513. - (void)setSaturation:(float)f_value
  514. {
  515. if (f_value <= 3. && f_value >= 0.) {
  516. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  517. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  518. }
  519. }
  520. - (float)gamma
  521. {
  522. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  523. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  524. }
  525. - (void)setGamma:(float)f_value
  526. {
  527. if (f_value <= 10. && f_value >= 0.) {
  528. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  529. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  530. }
  531. }
  532. - (void)setRate:(float)value
  533. {
  534. libvlc_media_player_set_rate(_playerInstance, value);
  535. }
  536. - (float)rate
  537. {
  538. return libvlc_media_player_get_rate(_playerInstance);
  539. }
  540. - (CGSize)videoSize
  541. {
  542. unsigned height = 0, width = 0;
  543. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  544. if (failure)
  545. return CGSizeZero;
  546. return CGSizeMake(width, height);
  547. }
  548. - (BOOL)hasVideoOut
  549. {
  550. return libvlc_media_player_has_vout(_playerInstance);
  551. }
  552. - (float)framesPerSecond
  553. {
  554. return .0;
  555. }
  556. - (void)setTime:(VLCTime *)value
  557. {
  558. // Time is managed in seconds, while duration is managed in microseconds
  559. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  560. libvlc_media_player_set_time(_playerInstance, value ? [[value numberValue] longLongValue] : 0);
  561. }
  562. - (VLCTime *)time
  563. {
  564. return _cachedTime;
  565. }
  566. - (VLCTime *)remainingTime
  567. {
  568. return _cachedRemainingTime;
  569. }
  570. #pragma mark -
  571. #pragma mark Chapters
  572. - (void)setCurrentChapterIndex:(int)value;
  573. {
  574. libvlc_media_player_set_chapter(_playerInstance, value);
  575. }
  576. - (int)currentChapterIndex
  577. {
  578. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  579. if (count <= 0)
  580. return INT_MAX;
  581. int result = libvlc_media_player_get_chapter(_playerInstance);
  582. return result;
  583. }
  584. - (void)nextChapter
  585. {
  586. libvlc_media_player_next_chapter(_playerInstance);
  587. }
  588. - (void)previousChapter
  589. {
  590. libvlc_media_player_previous_chapter(_playerInstance);
  591. }
  592. - (NSArray *)chaptersForTitleIndex:(int)title
  593. {
  594. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  595. if (count <= 0)
  596. return @[];
  597. #pragma clang diagnostic push
  598. #pragma clang diagnostic ignored "-Wdeprecated"
  599. libvlc_track_description_t *firstTrack = libvlc_video_get_chapter_description(_playerInstance, title);
  600. libvlc_track_description_t *currentTrack = firstTrack;
  601. #pragma clang diagnostic push
  602. NSMutableArray *tempArray = [NSMutableArray array];
  603. for (NSInteger i = 0; i < count ; i++) {
  604. [tempArray addObject:@(currentTrack->psz_name)];
  605. currentTrack = currentTrack->p_next;
  606. }
  607. libvlc_track_description_list_release(firstTrack);
  608. return [NSArray arrayWithArray:tempArray];
  609. }
  610. #pragma mark -
  611. #pragma mark Titles
  612. - (void)setCurrentTitleIndex:(int)value
  613. {
  614. libvlc_media_player_set_title(_playerInstance, value);
  615. }
  616. - (int)currentTitleIndex
  617. {
  618. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  619. if (count <= 0)
  620. return INT_MAX;
  621. return libvlc_media_player_get_title(_playerInstance);
  622. }
  623. - (int)numberOfTitles
  624. {
  625. return libvlc_media_player_get_title_count(_playerInstance);
  626. }
  627. - (NSUInteger)countOfTitles
  628. {
  629. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  630. return result;
  631. }
  632. - (NSArray *)titles
  633. {
  634. NSUInteger count = [self countOfTitles];
  635. if (count == 0)
  636. return [NSArray array];
  637. #pragma clang diagnostic push
  638. #pragma clang diagnostic ignored "-Wdeprecated"
  639. libvlc_track_description_t *firstTrack = libvlc_video_get_title_description(_playerInstance);
  640. libvlc_track_description_t *currentTrack = firstTrack;
  641. #pragma clang diagnostic pop
  642. if (!currentTrack)
  643. return [NSArray array];
  644. NSMutableArray *tempArray = [NSMutableArray array];
  645. while (1) {
  646. if (currentTrack->psz_name != nil)
  647. [tempArray addObject:@(currentTrack->psz_name)];
  648. if (currentTrack->p_next)
  649. currentTrack = currentTrack->p_next;
  650. else
  651. break;
  652. }
  653. libvlc_track_description_list_release(firstTrack);
  654. return [NSArray arrayWithArray: tempArray];
  655. }
  656. - (NSArray *)titleDescriptions
  657. {
  658. libvlc_title_description_t **titleInfo;
  659. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  660. if (numberOfTitleDescriptions < 0)
  661. return [NSArray array];
  662. if (numberOfTitleDescriptions == 0) {
  663. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  664. return [NSArray array];
  665. }
  666. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  667. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  668. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  669. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  670. VLCTitleDescriptionDuration,
  671. @(titleInfo[i]->b_menu),
  672. VLCTitleDescriptionIsMenu,
  673. nil];
  674. if (titleInfo[i]->psz_name != NULL)
  675. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  676. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  677. }
  678. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  679. return [NSArray arrayWithArray:array];
  680. }
  681. - (int)indexOfLongestTitle
  682. {
  683. NSArray *titles = [self titleDescriptions];
  684. NSUInteger titleCount = titles.count;
  685. int currentlyFoundTitle = 0;
  686. int64_t currentlySelectedDuration = 0;
  687. int64_t randomTitleDuration = 0;
  688. for (int x = 0; x < titleCount; x++) {
  689. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  690. if (randomTitleDuration > currentlySelectedDuration) {
  691. currentlySelectedDuration = randomTitleDuration;
  692. currentlyFoundTitle = x;
  693. }
  694. }
  695. return currentlyFoundTitle;
  696. }
  697. - (int)numberOfChaptersForTitle:(int)titleIndex
  698. {
  699. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  700. }
  701. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  702. {
  703. libvlc_chapter_description_t **chapterDescriptions;
  704. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  705. titleIndex,
  706. &chapterDescriptions);
  707. if (numberOfChapterDescriptions < 0)
  708. return [NSArray array];
  709. if (numberOfChapterDescriptions == 0) {
  710. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  711. return [NSArray array];
  712. }
  713. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  714. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  715. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  716. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  717. VLCChapterDescriptionDuration,
  718. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  719. VLCChapterDescriptionTimeOffset,
  720. nil];
  721. if (chapterDescriptions[i]->psz_name != NULL)
  722. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  723. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  724. }
  725. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  726. return [NSArray arrayWithArray:array];
  727. }
  728. #pragma mark -
  729. #pragma mark Audio tracks
  730. - (void)setCurrentAudioTrackIndex:(int)value
  731. {
  732. libvlc_audio_set_track(_playerInstance, value);
  733. }
  734. - (int)currentAudioTrackIndex
  735. {
  736. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  737. if (count <= 0)
  738. return INT_MAX;
  739. return libvlc_audio_get_track(_playerInstance);
  740. }
  741. - (NSArray *)audioTrackNames
  742. {
  743. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  744. if (count <= 0)
  745. return @[];
  746. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  747. libvlc_track_description_t *currentTrack = firstTrack;
  748. NSMutableArray *tempArray = [NSMutableArray array];
  749. while (currentTrack) {
  750. [tempArray addObject:@(currentTrack->psz_name)];
  751. currentTrack = currentTrack->p_next;
  752. }
  753. libvlc_track_description_list_release(firstTrack);
  754. return [NSArray arrayWithArray: tempArray];
  755. }
  756. - (NSArray *)audioTrackIndexes
  757. {
  758. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  759. if (count <= 0)
  760. return @[];
  761. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  762. libvlc_track_description_t *currentTrack = firstTrack;
  763. NSMutableArray *tempArray = [NSMutableArray array];
  764. while (currentTrack) {
  765. [tempArray addObject:@(currentTrack->i_id)];
  766. currentTrack = currentTrack->p_next;
  767. }
  768. libvlc_track_description_list_release(firstTrack);
  769. return [NSArray arrayWithArray: tempArray];
  770. }
  771. - (int)numberOfAudioTracks
  772. {
  773. return libvlc_audio_get_track_count(_playerInstance);
  774. }
  775. - (void)setAudioChannel:(int)value
  776. {
  777. libvlc_audio_set_channel(_playerInstance, value);
  778. }
  779. - (int)audioChannel
  780. {
  781. return libvlc_audio_get_channel(_playerInstance);
  782. }
  783. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  784. {
  785. libvlc_audio_set_delay(_playerInstance, index);
  786. }
  787. - (NSInteger)currentAudioPlaybackDelay
  788. {
  789. return libvlc_audio_get_delay(_playerInstance);
  790. }
  791. #pragma mark -
  792. #pragma mark equalizer
  793. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  794. {
  795. _equalizerEnabled = equalizerEnabled;
  796. if (!_equalizerEnabled) {
  797. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  798. if (_equalizerInstance)
  799. libvlc_audio_equalizer_release(_equalizerInstance);
  800. return;
  801. }
  802. if (!_equalizerInstance)
  803. _equalizerInstance = libvlc_audio_equalizer_new();
  804. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  805. }
  806. - (BOOL)equalizerEnabled
  807. {
  808. return _equalizerEnabled;
  809. }
  810. - (NSArray *)equalizerProfiles
  811. {
  812. unsigned count = libvlc_audio_equalizer_get_preset_count();
  813. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  814. for (unsigned x = 0; x < count; x++)
  815. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  816. return [NSArray arrayWithArray:array];
  817. }
  818. - (void)resetEqualizerFromProfile:(unsigned)profile
  819. {
  820. BOOL wasactive = NO;
  821. if (_equalizerInstance) {
  822. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  823. libvlc_audio_equalizer_release(_equalizerInstance);
  824. wasactive = YES;
  825. }
  826. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  827. if (wasactive)
  828. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  829. }
  830. - (CGFloat)preAmplification
  831. {
  832. if (!_equalizerInstance)
  833. return 0.;
  834. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  835. }
  836. - (void)setPreAmplification:(CGFloat)preAmplification
  837. {
  838. if (!_equalizerInstance)
  839. _equalizerInstance = libvlc_audio_equalizer_new();
  840. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  841. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  842. }
  843. - (unsigned)numberOfBands
  844. {
  845. return libvlc_audio_equalizer_get_band_count();
  846. }
  847. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  848. {
  849. return libvlc_audio_equalizer_get_band_frequency(index);
  850. }
  851. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  852. {
  853. if (!_equalizerInstance)
  854. _equalizerInstance = libvlc_audio_equalizer_new();
  855. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  856. }
  857. - (CGFloat)amplificationOfBand:(unsigned int)index
  858. {
  859. if (!_equalizerInstance)
  860. return 0.;
  861. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  862. }
  863. #pragma mark -
  864. #pragma mark set/get media
  865. - (void)setMedia:(VLCMedia *)value
  866. {
  867. if (_media != value) {
  868. if (_media && [_media compare:value] == NSOrderedSame)
  869. return;
  870. _media = value;
  871. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  872. }
  873. }
  874. - (VLCMedia *)media
  875. {
  876. return _media;
  877. }
  878. #pragma mark -
  879. #pragma mark playback
  880. - (BOOL)play
  881. {
  882. libvlc_media_player_play(_playerInstance);
  883. return YES;
  884. }
  885. - (void)pause
  886. {
  887. if ([NSThread isMainThread]) {
  888. /* Hack because we create a dead lock here, when the vout is stopped
  889. * and tries to recontact us on the main thread */
  890. /* FIXME: to do this properly we need to do some locking. We may want
  891. * to move that to libvlc */
  892. [self performSelectorInBackground:@selector(pause) withObject:nil];
  893. return;
  894. }
  895. // Pause the stream
  896. libvlc_media_player_pause(_playerInstance);
  897. }
  898. - (void)stop
  899. {
  900. if ([NSThread isMainThread]) {
  901. /* Hack because we create a dead lock here, when the vout is stopped
  902. * and tries to recontact us on the main thread */
  903. /* FIXME: to do this properly we need to do some locking. We may want
  904. * to move that to libvlc */
  905. [self performSelectorInBackground:@selector(stop) withObject:nil];
  906. return;
  907. }
  908. libvlc_media_player_stop(_playerInstance);
  909. }
  910. - (void)gotoNextFrame
  911. {
  912. libvlc_media_player_next_frame(_playerInstance);
  913. }
  914. - (void)fastForward
  915. {
  916. [self fastForwardAtRate: 2.0];
  917. }
  918. - (void)fastForwardAtRate:(float)rate
  919. {
  920. [self setRate:rate];
  921. }
  922. - (void)rewind
  923. {
  924. [self rewindAtRate: 2.0];
  925. }
  926. - (void)rewindAtRate:(float)rate
  927. {
  928. [self setRate: -rate];
  929. }
  930. - (void)jumpBackward:(int)interval
  931. {
  932. if ([self isSeekable]) {
  933. interval = interval * 1000;
  934. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  935. }
  936. }
  937. - (void)jumpForward:(int)interval
  938. {
  939. if ([self isSeekable]) {
  940. interval = interval * 1000;
  941. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  942. }
  943. }
  944. - (void)extraShortJumpBackward
  945. {
  946. [self jumpBackward:3];
  947. }
  948. - (void)extraShortJumpForward
  949. {
  950. [self jumpForward:3];
  951. }
  952. - (void)shortJumpBackward
  953. {
  954. [self jumpBackward:10];
  955. }
  956. - (void)shortJumpForward
  957. {
  958. [self jumpForward:10];
  959. }
  960. - (void)mediumJumpBackward
  961. {
  962. [self jumpBackward:60];
  963. }
  964. - (void)mediumJumpForward
  965. {
  966. [self jumpForward:60];
  967. }
  968. - (void)longJumpBackward
  969. {
  970. [self jumpBackward:300];
  971. }
  972. - (void)longJumpForward
  973. {
  974. [self jumpForward:300];
  975. }
  976. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  977. {
  978. return [NSSet setWithObjects:@"state", nil];
  979. }
  980. - (BOOL)isPlaying
  981. {
  982. return libvlc_media_player_is_playing(_playerInstance);
  983. }
  984. - (BOOL)willPlay
  985. {
  986. return libvlc_media_player_will_play(_playerInstance);
  987. }
  988. - (VLCMediaPlayerState)state
  989. {
  990. return _cachedState;
  991. }
  992. - (float)position
  993. {
  994. return _position;
  995. }
  996. - (void)setPosition:(float)newPosition
  997. {
  998. libvlc_media_player_set_position(_playerInstance, newPosition);
  999. }
  1000. - (BOOL)isSeekable
  1001. {
  1002. return libvlc_media_player_is_seekable(_playerInstance);
  1003. }
  1004. - (BOOL)canPause
  1005. {
  1006. return libvlc_media_player_can_pause(_playerInstance);
  1007. }
  1008. #if TARGET_OS_IPHONE
  1009. - (NSArray *)snapshots
  1010. {
  1011. return [_snapshots copy];
  1012. }
  1013. - (UIImage *)lastSnapshot {
  1014. if (_snapshots == nil) {
  1015. return nil;
  1016. }
  1017. @synchronized(_snapshots) {
  1018. if (_snapshots.count == 0)
  1019. return nil;
  1020. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1021. }
  1022. }
  1023. #endif
  1024. - (void *)libVLCMediaPlayer
  1025. {
  1026. return _playerInstance;
  1027. }
  1028. @end
  1029. @implementation VLCMediaPlayer (Private)
  1030. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1031. {
  1032. if (self = [super init]) {
  1033. _cachedTime = [VLCTime nullTime];
  1034. _cachedRemainingTime = [VLCTime nullTime];
  1035. _position = 0.0f;
  1036. _cachedState = VLCMediaPlayerStateStopped;
  1037. // Create a media instance, it doesn't matter what library we start off with
  1038. // it will change depending on the media descriptor provided to the media
  1039. // instance
  1040. if (options && options.count > 0) {
  1041. VKLog(@"creating player instance with private library as options were given");
  1042. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1043. } else {
  1044. VKLog(@"creating player instance using shared library");
  1045. _privateLibrary = [VLCLibrary sharedLibrary];
  1046. }
  1047. libvlc_retain([_privateLibrary instance]);
  1048. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1049. libvlc_media_player_retain(_playerInstance);
  1050. [self registerObservers];
  1051. [self setDrawable:aDrawable];
  1052. }
  1053. return self;
  1054. }
  1055. - (void)registerObservers
  1056. {
  1057. // Attach event observers into the media instance
  1058. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1059. if (!p_em)
  1060. return;
  1061. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1062. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1063. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1064. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1065. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1066. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1067. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1068. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1069. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1070. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1071. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1072. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1073. #if TARGET_OS_IPHONE
  1074. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1075. #endif
  1076. }
  1077. - (void)unregisterObservers
  1078. {
  1079. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1080. if (!p_em)
  1081. return;
  1082. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1083. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1084. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1085. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1086. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1087. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1088. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1089. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1090. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1091. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1092. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1093. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1094. #if TARGET_OS_IPHONE
  1095. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1096. #endif
  1097. }
  1098. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1099. {
  1100. [self willChangeValueForKey:@"time"];
  1101. [self willChangeValueForKey:@"remainingTime"];
  1102. _cachedTime = [VLCTime timeWithNumber:newTime];
  1103. double currentTime = [[_cachedTime numberValue] doubleValue];
  1104. if (currentTime > 0 && _position > 0.) {
  1105. double remaining = currentTime / _position * (1 - _position);
  1106. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1107. } else
  1108. _cachedRemainingTime = [VLCTime nullTime];
  1109. [self didChangeValueForKey:@"remainingTime"];
  1110. [self didChangeValueForKey:@"time"];
  1111. }
  1112. #if !TARGET_OS_IPHONE
  1113. - (void)delaySleep
  1114. {
  1115. UpdateSystemActivity(UsrActivity);
  1116. }
  1117. #endif
  1118. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1119. {
  1120. #if !TARGET_OS_IPHONE
  1121. // This seems to be the most relevant place to delay sleeping and screen saver.
  1122. [self delaySleep];
  1123. #endif
  1124. [self willChangeValueForKey:@"position"];
  1125. _position = [newPosition floatValue];
  1126. [self didChangeValueForKey:@"position"];
  1127. }
  1128. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1129. {
  1130. [self willChangeValueForKey:@"state"];
  1131. _cachedState = [newState intValue];
  1132. #if TARGET_OS_IPHONE
  1133. // Disable idle timer if player is playing media
  1134. // Exclusion can be made for audio only media
  1135. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1136. #endif
  1137. [self didChangeValueForKey:@"state"];
  1138. }
  1139. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1140. {
  1141. [self willChangeValueForKey:@"media"];
  1142. if (_media != newMedia)
  1143. _media = newMedia;
  1144. [self didChangeValueForKey:@"media"];
  1145. }
  1146. #if TARGET_OS_IPHONE
  1147. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1148. {
  1149. @synchronized(_snapshots) {
  1150. if (!_snapshots) {
  1151. _snapshots = [NSMutableArray array];
  1152. }
  1153. [_snapshots addObject:fileName];
  1154. }
  1155. }
  1156. #endif
  1157. @end