VLCMediaPlayer.m 49 KB

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