VLCMediaPlayer.m 39 KB

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