VLCMediaPlayer.m 45 KB

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