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