VLCMediaPlayer.m 44 KB

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