VLCMediaPlayer.m 39 KB

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