VLCMediaPlayer.m 46 KB

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