VLCMediaPlayer.m 39 KB

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