VLCMediaPlayer.m 41 KB

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