VLCMediaPlayer.m 38 KB

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