VLCMediaPlayer.m 41 KB

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