VLCMediaPlayer.m 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2019 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2019 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. _equalizerInstance = nil;
  286. }
  287. if (_viewpoint)
  288. libvlc_free(_viewpoint);
  289. libvlc_media_player_release(_playerInstance);
  290. if (_privateLibrary != [VLCLibrary sharedLibrary])
  291. libvlc_release(_privateLibrary.instance);
  292. }
  293. #if !TARGET_OS_IPHONE
  294. - (void)setVideoView:(VLCVideoView *)aVideoView
  295. {
  296. [self setDrawable: aVideoView];
  297. }
  298. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  299. {
  300. [self setDrawable: aVideoLayer];
  301. }
  302. #endif
  303. - (void)setDrawable:(id)aDrawable
  304. {
  305. // Make sure that this instance has been associated with the drawing canvas.
  306. _drawable = aDrawable;
  307. /* Note that ee need the caller to wait until the setter succeeded.
  308. * Otherwise, s/he might want to deploy the drawable while it isn’t ready yet. */
  309. dispatch_sync(_libVLCBackgroundQueue, ^{
  310. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  311. });
  312. }
  313. - (id)drawable
  314. {
  315. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  316. }
  317. - (VLCAudio *)audio
  318. {
  319. if (!_audio)
  320. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  321. return _audio;
  322. }
  323. #pragma mark -
  324. #pragma mark Video Tracks
  325. - (void)setCurrentVideoTrackIndex:(int)value
  326. {
  327. libvlc_video_set_track(_playerInstance, value);
  328. }
  329. - (int)currentVideoTrackIndex
  330. {
  331. int count = libvlc_video_get_track_count(_playerInstance);
  332. if (count <= 0)
  333. return -1;
  334. return libvlc_video_get_track(_playerInstance);
  335. }
  336. - (NSArray *)videoTrackNames
  337. {
  338. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  339. if (count <= 0)
  340. return @[];
  341. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  342. libvlc_track_description_t *currentTrack = firstTrack;
  343. NSMutableArray *tempArray = [NSMutableArray array];
  344. while (currentTrack) {
  345. [tempArray addObject:@(currentTrack->psz_name)];
  346. currentTrack = currentTrack->p_next;
  347. }
  348. libvlc_track_description_list_release(firstTrack);
  349. return [NSArray arrayWithArray: tempArray];
  350. }
  351. - (NSArray *)videoTrackIndexes
  352. {
  353. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  354. if (count <= 0)
  355. return @[];
  356. libvlc_track_description_t *firstTrack = libvlc_video_get_track_description(_playerInstance);
  357. libvlc_track_description_t *currentTrack = firstTrack;
  358. NSMutableArray *tempArray = [NSMutableArray array];
  359. while (currentTrack) {
  360. [tempArray addObject:@(currentTrack->i_id)];
  361. currentTrack = currentTrack->p_next;
  362. }
  363. libvlc_track_description_list_release(firstTrack);
  364. return [NSArray arrayWithArray: tempArray];
  365. }
  366. - (int)numberOfVideoTracks
  367. {
  368. return libvlc_video_get_track_count(_playerInstance);
  369. }
  370. #pragma mark -
  371. #pragma mark Subtitles
  372. - (void)setCurrentVideoSubTitleIndex:(int)index
  373. {
  374. libvlc_video_set_spu(_playerInstance, index);
  375. }
  376. - (int)currentVideoSubTitleIndex
  377. {
  378. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  379. if (count <= 0)
  380. return -1;
  381. return libvlc_video_get_spu(_playerInstance);
  382. }
  383. - (NSArray *)videoSubTitlesNames
  384. {
  385. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  386. if (count <= 0)
  387. return @[];
  388. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  389. libvlc_track_description_t *currentTrack = firstTrack;
  390. NSMutableArray *tempArray = [NSMutableArray array];
  391. while (currentTrack) {
  392. NSString *track = @(currentTrack->psz_name);
  393. [tempArray addObject:track != nil ? track : @""];
  394. currentTrack = currentTrack->p_next;
  395. }
  396. libvlc_track_description_list_release(firstTrack);
  397. return [NSArray arrayWithArray: tempArray];
  398. }
  399. - (NSArray *)videoSubTitlesIndexes
  400. {
  401. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  402. if (count <= 0)
  403. return @[];
  404. libvlc_track_description_t *firstTrack = libvlc_video_get_spu_description(_playerInstance);
  405. libvlc_track_description_t *currentTrack = firstTrack;
  406. NSMutableArray *tempArray = [NSMutableArray array];
  407. while (currentTrack) {
  408. [tempArray addObject:@(currentTrack->i_id)];
  409. currentTrack = currentTrack->p_next;
  410. }
  411. libvlc_track_description_list_release(firstTrack);
  412. return [NSArray arrayWithArray: tempArray];
  413. }
  414. - (int)numberOfSubtitlesTracks
  415. {
  416. return libvlc_video_get_spu_count(_playerInstance);
  417. }
  418. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  419. {
  420. return [self addPlaybackSlave:[NSURL fileURLWithPath:path] type:VLCMediaPlaybackSlaveTypeSubtitle enforce:YES];
  421. }
  422. - (int)addPlaybackSlave:(NSURL *)slaveURL type:(VLCMediaPlaybackSlaveType)slaveType enforce:(BOOL)enforceSelection
  423. {
  424. if (!slaveURL)
  425. return -1;
  426. return libvlc_media_player_add_slave(_playerInstance,
  427. slaveType,
  428. [[slaveURL absoluteString] UTF8String],
  429. enforceSelection);
  430. }
  431. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  432. {
  433. libvlc_video_set_spu_delay(_playerInstance, index);
  434. }
  435. - (NSInteger)currentVideoSubTitleDelay
  436. {
  437. return libvlc_video_get_spu_delay(_playerInstance);
  438. }
  439. #if TARGET_OS_IPHONE
  440. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  441. {
  442. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  443. }
  444. - (void)setTextRendererFont:(NSString *)fontname
  445. {
  446. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  447. }
  448. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  449. {
  450. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  451. }
  452. - (void)setTextRendererFontForceBold:(NSNumber *)fontForceBold
  453. {
  454. libvlc_video_set_textrenderer_bool(_playerInstance, libvlc_textrender_fontforcebold, [fontForceBold boolValue]);
  455. }
  456. #endif
  457. #pragma mark -
  458. #pragma mark Video Crop geometry
  459. - (void)setCropRatioWithNumerator:(unsigned int)numerator denominator:(unsigned int)denominator
  460. {
  461. libvlc_video_set_crop_ratio(_playerInstance, numerator, denominator);
  462. }
  463. - (void)setVideoAspectRatio:(char *)value
  464. {
  465. libvlc_video_set_aspect_ratio(_playerInstance, value);
  466. }
  467. - (char *)videoAspectRatio
  468. {
  469. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  470. return result;
  471. }
  472. - (void)setScaleFactor:(float)value
  473. {
  474. libvlc_video_set_scale(_playerInstance, value);
  475. }
  476. - (float)scaleFactor
  477. {
  478. return libvlc_video_get_scale(_playerInstance);
  479. }
  480. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  481. {
  482. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  483. if (failure)
  484. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  485. }
  486. - (void)setDeinterlaceFilter:(NSString *)name
  487. {
  488. if (!name || name.length < 1)
  489. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOff, NULL);
  490. else
  491. libvlc_video_set_deinterlace(_playerInstance, VLCDeinterlaceOn, [name UTF8String]);
  492. }
  493. - (void)setDeinterlace:(VLCDeinterlace)deinterlace withFilter:(NSString *)name
  494. {
  495. libvlc_video_set_deinterlace(_playerInstance, deinterlace, [name UTF8String]);
  496. }
  497. - (BOOL)adjustFilterEnabled
  498. {
  499. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  500. }
  501. - (void)setAdjustFilterEnabled:(BOOL)b_value
  502. {
  503. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  504. }
  505. - (float)contrast
  506. {
  507. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  508. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  509. }
  510. - (void)setContrast:(float)f_value
  511. {
  512. if (f_value <= 2. && f_value >= 0.) {
  513. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  514. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  515. }
  516. }
  517. - (float)brightness
  518. {
  519. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  520. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  521. }
  522. - (void)setBrightness:(float)f_value
  523. {
  524. if (f_value <= 2. && f_value >= 0.) {
  525. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  526. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  527. }
  528. }
  529. - (float)hue
  530. {
  531. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  532. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Hue);
  533. }
  534. - (void)setHue:(float)f_value
  535. {
  536. if (f_value <= 180. && f_value >= -180.) {
  537. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  538. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Hue, f_value);
  539. }
  540. }
  541. - (float)saturation
  542. {
  543. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  544. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  545. }
  546. - (void)setSaturation:(float)f_value
  547. {
  548. if (f_value <= 3. && f_value >= 0.) {
  549. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  550. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  551. }
  552. }
  553. - (float)gamma
  554. {
  555. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  556. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  557. }
  558. - (void)setGamma:(float)f_value
  559. {
  560. if (f_value <= 10. && f_value >= 0.) {
  561. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  562. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  563. }
  564. }
  565. - (void)setRate:(float)value
  566. {
  567. libvlc_media_player_set_rate(_playerInstance, value);
  568. }
  569. - (float)rate
  570. {
  571. return libvlc_media_player_get_rate(_playerInstance);
  572. }
  573. - (CGSize)videoSize
  574. {
  575. unsigned height = 0, width = 0;
  576. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  577. if (failure)
  578. return CGSizeZero;
  579. return CGSizeMake(width, height);
  580. }
  581. - (BOOL)hasVideoOut
  582. {
  583. return libvlc_media_player_has_vout(_playerInstance);
  584. }
  585. - (float)framesPerSecond
  586. {
  587. return .0;
  588. }
  589. - (void)setTime:(VLCTime *)value
  590. {
  591. // Time is managed in seconds, while duration is managed in microseconds
  592. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  593. libvlc_media_player_set_time(_playerInstance, value ? [[value value] longLongValue] : 0, NO);
  594. }
  595. - (VLCTime *)time
  596. {
  597. return _cachedTime;
  598. }
  599. - (VLCTime *)remainingTime
  600. {
  601. return _cachedRemainingTime;
  602. }
  603. #pragma mark -
  604. #pragma mark Chapters
  605. - (void)setCurrentChapterIndex:(int)value;
  606. {
  607. libvlc_media_player_set_chapter(_playerInstance, value);
  608. }
  609. - (int)currentChapterIndex
  610. {
  611. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  612. if (count <= 0)
  613. return -1;
  614. int result = libvlc_media_player_get_chapter(_playerInstance);
  615. return result;
  616. }
  617. - (void)nextChapter
  618. {
  619. libvlc_media_player_next_chapter(_playerInstance);
  620. }
  621. - (void)previousChapter
  622. {
  623. libvlc_media_player_previous_chapter(_playerInstance);
  624. }
  625. #pragma mark -
  626. #pragma mark Titles
  627. - (void)setCurrentTitleIndex:(int)value
  628. {
  629. libvlc_media_player_set_title(_playerInstance, value);
  630. }
  631. - (int)currentTitleIndex
  632. {
  633. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  634. if (count <= 0)
  635. return -1;
  636. return libvlc_media_player_get_title(_playerInstance);
  637. }
  638. - (int)numberOfTitles
  639. {
  640. return libvlc_media_player_get_title_count(_playerInstance);
  641. }
  642. - (NSArray *)titleDescriptions
  643. {
  644. libvlc_title_description_t **titleInfo;
  645. int numberOfTitleDescriptions = libvlc_media_player_get_full_title_descriptions(_playerInstance, &titleInfo);
  646. if (numberOfTitleDescriptions < 0)
  647. return [NSArray array];
  648. if (numberOfTitleDescriptions == 0) {
  649. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  650. return [NSArray array];
  651. }
  652. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfTitleDescriptions];
  653. for (int i = 0; i < numberOfTitleDescriptions; i++) {
  654. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  655. [NSNumber numberWithLongLong:titleInfo[i]->i_duration],
  656. VLCTitleDescriptionDuration,
  657. @(titleInfo[i]->i_flags & libvlc_title_menu),
  658. VLCTitleDescriptionIsMenu,
  659. nil];
  660. if (titleInfo[i]->psz_name != NULL)
  661. dictionary[VLCTitleDescriptionName] = [NSString stringWithUTF8String:titleInfo[i]->psz_name];
  662. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  663. }
  664. libvlc_title_descriptions_release(titleInfo, numberOfTitleDescriptions);
  665. return [NSArray arrayWithArray:array];
  666. }
  667. - (int)indexOfLongestTitle
  668. {
  669. NSArray *titles = [self titleDescriptions];
  670. NSUInteger titleCount = titles.count;
  671. int currentlyFoundTitle = 0;
  672. int64_t currentlySelectedDuration = 0;
  673. int64_t randomTitleDuration = 0;
  674. for (int x = 0; x < titleCount; x++) {
  675. randomTitleDuration = [[titles[x] valueForKey:VLCTitleDescriptionDuration] longLongValue];
  676. if (randomTitleDuration > currentlySelectedDuration) {
  677. currentlySelectedDuration = randomTitleDuration;
  678. currentlyFoundTitle = x;
  679. }
  680. }
  681. return currentlyFoundTitle;
  682. }
  683. - (int)numberOfChaptersForTitle:(int)titleIndex
  684. {
  685. return libvlc_media_player_get_chapter_count_for_title(_playerInstance, titleIndex);
  686. }
  687. - (NSArray *)chapterDescriptionsOfTitle:(int)titleIndex
  688. {
  689. libvlc_chapter_description_t **chapterDescriptions;
  690. int numberOfChapterDescriptions = libvlc_media_player_get_full_chapter_descriptions(_playerInstance,
  691. titleIndex,
  692. &chapterDescriptions);
  693. if (numberOfChapterDescriptions < 0)
  694. return [NSArray array];
  695. if (numberOfChapterDescriptions == 0) {
  696. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  697. return [NSArray array];
  698. }
  699. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:numberOfChapterDescriptions];
  700. for (int i = 0; i < numberOfChapterDescriptions; i++) {
  701. NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  702. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_duration],
  703. VLCChapterDescriptionDuration,
  704. [NSNumber numberWithLongLong:chapterDescriptions[i]->i_time_offset],
  705. VLCChapterDescriptionTimeOffset,
  706. nil];
  707. if (chapterDescriptions[i]->psz_name != NULL)
  708. dictionary[VLCChapterDescriptionName] = [NSString stringWithUTF8String:chapterDescriptions[i]->psz_name];
  709. [array addObject:[NSDictionary dictionaryWithDictionary:dictionary]];
  710. }
  711. libvlc_chapter_descriptions_release(chapterDescriptions, numberOfChapterDescriptions);
  712. return [NSArray arrayWithArray:array];
  713. }
  714. #pragma mark -
  715. #pragma mark Audio tracks
  716. - (void)setCurrentAudioTrackIndex:(int)value
  717. {
  718. libvlc_audio_set_track(_playerInstance, value);
  719. }
  720. - (int)currentAudioTrackIndex
  721. {
  722. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  723. if (count <= 0)
  724. return -1;
  725. return libvlc_audio_get_track(_playerInstance);
  726. }
  727. - (NSArray *)audioTrackNames
  728. {
  729. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  730. if (count <= 0)
  731. return @[];
  732. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  733. libvlc_track_description_t *currentTrack = firstTrack;
  734. NSMutableArray *tempArray = [NSMutableArray array];
  735. while (currentTrack) {
  736. NSString *track = @(currentTrack->psz_name);
  737. [tempArray addObject:track != nil ? track : @""];
  738. currentTrack = currentTrack->p_next;
  739. }
  740. libvlc_track_description_list_release(firstTrack);
  741. return [NSArray arrayWithArray: tempArray];
  742. }
  743. - (NSArray *)audioTrackIndexes
  744. {
  745. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  746. if (count <= 0)
  747. return @[];
  748. libvlc_track_description_t *firstTrack = libvlc_audio_get_track_description(_playerInstance);
  749. libvlc_track_description_t *currentTrack = firstTrack;
  750. NSMutableArray *tempArray = [NSMutableArray array];
  751. while (currentTrack) {
  752. [tempArray addObject:@(currentTrack->i_id)];
  753. currentTrack = currentTrack->p_next;
  754. }
  755. libvlc_track_description_list_release(firstTrack);
  756. return [NSArray arrayWithArray: tempArray];
  757. }
  758. - (int)numberOfAudioTracks
  759. {
  760. return libvlc_audio_get_track_count(_playerInstance);
  761. }
  762. - (void)setAudioChannel:(int)value
  763. {
  764. libvlc_audio_set_channel(_playerInstance, value);
  765. }
  766. - (int)audioChannel
  767. {
  768. return libvlc_audio_get_channel(_playerInstance);
  769. }
  770. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  771. {
  772. libvlc_audio_set_delay(_playerInstance, index);
  773. }
  774. - (NSInteger)currentAudioPlaybackDelay
  775. {
  776. return libvlc_audio_get_delay(_playerInstance);
  777. }
  778. #pragma mark -
  779. #pragma mark equalizer
  780. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  781. {
  782. if (!_equalizerInstance && equalizerEnabled) {
  783. if (!(_equalizerInstance = libvlc_audio_equalizer_new())) {
  784. NSAssert(_equalizerInstance, @"equalizer failed to initialize");
  785. return;
  786. }
  787. }
  788. _equalizerEnabled = equalizerEnabled;
  789. libvlc_media_player_set_equalizer(_playerInstance,
  790. equalizerEnabled ? _equalizerInstance : NULL);
  791. }
  792. - (BOOL)equalizerEnabled
  793. {
  794. return _equalizerEnabled;
  795. }
  796. - (NSArray *)equalizerProfiles
  797. {
  798. unsigned count = libvlc_audio_equalizer_get_preset_count();
  799. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  800. for (unsigned x = 0; x < count; x++)
  801. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  802. return [NSArray arrayWithArray:array];
  803. }
  804. - (void)resetEqualizerFromProfile:(unsigned)profile
  805. {
  806. BOOL wasactive = NO;
  807. if (_equalizerInstance) {
  808. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  809. libvlc_audio_equalizer_release(_equalizerInstance);
  810. _equalizerInstance = nil;
  811. wasactive = YES;
  812. }
  813. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  814. if (wasactive)
  815. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  816. }
  817. - (CGFloat)preAmplification
  818. {
  819. if (!_equalizerInstance)
  820. return 0.;
  821. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  822. }
  823. - (void)setPreAmplification:(CGFloat)preAmplification
  824. {
  825. if (!_equalizerInstance)
  826. _equalizerInstance = libvlc_audio_equalizer_new();
  827. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  828. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  829. }
  830. - (unsigned)numberOfBands
  831. {
  832. return libvlc_audio_equalizer_get_band_count();
  833. }
  834. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  835. {
  836. return libvlc_audio_equalizer_get_band_frequency(index);
  837. }
  838. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  839. {
  840. if (!_equalizerInstance)
  841. _equalizerInstance = libvlc_audio_equalizer_new();
  842. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  843. }
  844. - (CGFloat)amplificationOfBand:(unsigned int)index
  845. {
  846. if (!_equalizerInstance)
  847. return 0.;
  848. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  849. }
  850. #pragma mark -
  851. #pragma mark set/get media
  852. - (void)setMedia:(VLCMedia *)value
  853. {
  854. if (_media != value) {
  855. if (_media && [_media compare:value] == NSOrderedSame)
  856. return;
  857. _media = value;
  858. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  859. }
  860. }
  861. - (VLCMedia *)media
  862. {
  863. return _media;
  864. }
  865. #pragma mark -
  866. #pragma mark playback
  867. - (void)play
  868. {
  869. dispatch_async(_libVLCBackgroundQueue, ^{
  870. libvlc_media_player_play(_playerInstance);
  871. });
  872. }
  873. - (void)pause
  874. {
  875. // Pause the stream
  876. dispatch_async(_libVLCBackgroundQueue, ^{
  877. libvlc_media_player_set_pause(_playerInstance, 1);
  878. });
  879. }
  880. - (void)stop
  881. {
  882. dispatch_async(_libVLCBackgroundQueue, ^{
  883. libvlc_media_player_stop_async(_playerInstance);
  884. });
  885. }
  886. - (libvlc_video_viewpoint_t *)viewPoint
  887. {
  888. if (_viewpoint == NULL) {
  889. _viewpoint = libvlc_video_new_viewpoint();
  890. }
  891. return _viewpoint;
  892. }
  893. - (BOOL)updateViewpoint:(float)yaw pitch:(float)pitch roll:(float)roll fov:(float)fov absolute:(BOOL)absolute
  894. {
  895. if ([self viewPoint]) {
  896. [self viewPoint]->f_yaw = yaw;
  897. [self viewPoint]->f_pitch = pitch;
  898. [self viewPoint]->f_roll = roll;
  899. [self viewPoint]->f_field_of_view = fov;
  900. return libvlc_video_update_viewpoint(_playerInstance, _viewpoint, absolute) == 0;
  901. }
  902. return NO;
  903. }
  904. - (float)yaw
  905. {
  906. if ([self viewPoint]) {
  907. return [self viewPoint]->f_yaw;
  908. }
  909. return 0;
  910. }
  911. - (float)pitch
  912. {
  913. if ([self viewPoint]) {
  914. return [self viewPoint]->f_pitch;
  915. }
  916. return 0;
  917. }
  918. - (float)roll
  919. {
  920. if ([self viewPoint]) {
  921. return [self viewPoint]->f_roll;
  922. }
  923. return 0;
  924. }
  925. - (float)fov
  926. {
  927. if ([self viewPoint]) {
  928. return [self viewPoint]->f_field_of_view;
  929. }
  930. return 0;
  931. }
  932. - (void)gotoNextFrame
  933. {
  934. libvlc_media_player_next_frame(_playerInstance);
  935. }
  936. - (void)fastForward
  937. {
  938. [self fastForwardAtRate: 2.0];
  939. }
  940. - (void)fastForwardAtRate:(float)rate
  941. {
  942. [self setRate:rate];
  943. }
  944. - (void)rewind
  945. {
  946. [self rewindAtRate: 2.0];
  947. }
  948. - (void)rewindAtRate:(float)rate
  949. {
  950. [self setRate: -rate];
  951. }
  952. - (void)jumpBackward:(int)interval
  953. {
  954. if ([self isSeekable]) {
  955. interval = interval * 1000;
  956. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  957. }
  958. }
  959. - (void)jumpForward:(int)interval
  960. {
  961. if ([self isSeekable]) {
  962. interval = interval * 1000;
  963. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  964. }
  965. }
  966. - (void)extraShortJumpBackward
  967. {
  968. [self jumpBackward:3];
  969. }
  970. - (void)extraShortJumpForward
  971. {
  972. [self jumpForward:3];
  973. }
  974. - (void)shortJumpBackward
  975. {
  976. [self jumpBackward:10];
  977. }
  978. - (void)shortJumpForward
  979. {
  980. [self jumpForward:10];
  981. }
  982. - (void)mediumJumpBackward
  983. {
  984. [self jumpBackward:60];
  985. }
  986. - (void)mediumJumpForward
  987. {
  988. [self jumpForward:60];
  989. }
  990. - (void)longJumpBackward
  991. {
  992. [self jumpBackward:300];
  993. }
  994. - (void)longJumpForward
  995. {
  996. [self jumpForward:300];
  997. }
  998. - (void)performNavigationAction:(VLCMediaPlaybackNavigationAction)action
  999. {
  1000. libvlc_media_player_navigate(_playerInstance, action);
  1001. }
  1002. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  1003. {
  1004. return [NSSet setWithObjects:@"state", nil];
  1005. }
  1006. - (BOOL)isPlaying
  1007. {
  1008. return libvlc_media_player_is_playing(_playerInstance);
  1009. }
  1010. - (VLCMediaPlayerState)state
  1011. {
  1012. return _cachedState;
  1013. }
  1014. - (float)position
  1015. {
  1016. return _position;
  1017. }
  1018. - (void)setPosition:(float)newPosition
  1019. {
  1020. libvlc_media_player_set_position(_playerInstance, newPosition, NO);
  1021. }
  1022. - (BOOL)isSeekable
  1023. {
  1024. return libvlc_media_player_is_seekable(_playerInstance);
  1025. }
  1026. - (BOOL)canPause
  1027. {
  1028. return libvlc_media_player_can_pause(_playerInstance);
  1029. }
  1030. - (NSArray *)snapshots
  1031. {
  1032. return [_snapshots copy];
  1033. }
  1034. #if TARGET_OS_IPHONE
  1035. - (UIImage *)lastSnapshot {
  1036. if (_snapshots == nil) {
  1037. return nil;
  1038. }
  1039. @synchronized(_snapshots) {
  1040. if (_snapshots.count == 0)
  1041. return nil;
  1042. return [UIImage imageWithContentsOfFile:[_snapshots lastObject]];
  1043. }
  1044. }
  1045. #else
  1046. - (NSImage *)lastSnapshot {
  1047. if (_snapshots == nil) {
  1048. return nil;
  1049. }
  1050. @synchronized(_snapshots) {
  1051. if (_snapshots.count == 0)
  1052. return nil;
  1053. return [[NSImage alloc] initWithContentsOfFile:[_snapshots lastObject]];
  1054. }
  1055. }
  1056. #endif
  1057. - (void *)libVLCMediaPlayer
  1058. {
  1059. return _playerInstance;
  1060. }
  1061. - (BOOL)startRecordingAtPath:(NSString *)path
  1062. {
  1063. return libvlc_media_player_record(_playerInstance, YES, [path UTF8String]);
  1064. }
  1065. - (BOOL)stopRecording
  1066. {
  1067. return libvlc_media_player_record(_playerInstance, NO, nil);
  1068. }
  1069. #pragma mark -
  1070. #pragma mark - Renderer
  1071. - (BOOL)setRendererItem:(VLCRendererItem *)item
  1072. {
  1073. return libvlc_media_player_set_renderer(_playerInstance, item.libVLCRendererItem) == 0;
  1074. }
  1075. @end
  1076. @implementation VLCMediaPlayer (Private)
  1077. - (instancetype)initWithDrawable:(id)aDrawable options:(NSArray *)options
  1078. {
  1079. if (self = [super init]) {
  1080. _cachedTime = [VLCTime nullTime];
  1081. _cachedRemainingTime = [VLCTime nullTime];
  1082. _position = 0.0f;
  1083. _cachedState = VLCMediaPlayerStateStopped;
  1084. _libVLCBackgroundQueue = [self libVLCBackgroundQueue];
  1085. // Create a media instance, it doesn't matter what library we start off with
  1086. // it will change depending on the media descriptor provided to the media
  1087. // instance
  1088. if (options && options.count > 0) {
  1089. VKLog(@"creating player instance with private library as options were given");
  1090. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  1091. } else {
  1092. VKLog(@"creating player instance using shared library");
  1093. _privateLibrary = [VLCLibrary sharedLibrary];
  1094. }
  1095. libvlc_retain([_privateLibrary instance]);
  1096. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  1097. if (_playerInstance == NULL) {
  1098. NSAssert(0, @"%s: player initialization failed", __PRETTY_FUNCTION__);
  1099. libvlc_release([_privateLibrary instance]);
  1100. return nil;
  1101. }
  1102. [self registerObservers];
  1103. [self setDrawable:aDrawable];
  1104. }
  1105. return self;
  1106. }
  1107. - (void)registerObservers
  1108. {
  1109. // Attach event observers into the media instance
  1110. __block libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1111. if (!p_em)
  1112. return;
  1113. /* We need the caller to wait until this block is done.
  1114. * The initialized object shall not be returned until the event attachments are done. */
  1115. dispatch_sync(_libVLCBackgroundQueue,^{
  1116. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1117. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1118. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1119. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1120. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1121. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1122. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1123. libvlc_event_attach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1124. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1125. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1126. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1127. libvlc_event_attach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1128. libvlc_event_attach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1129. libvlc_event_attach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1130. libvlc_event_attach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1131. });
  1132. }
  1133. - (void)unregisterObservers
  1134. {
  1135. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  1136. if (!p_em)
  1137. return;
  1138. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1139. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1140. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1141. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1142. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1143. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1144. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1145. libvlc_event_detach(p_em, libvlc_MediaPlayerESAdded, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  1146. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  1147. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  1148. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  1149. libvlc_event_detach(p_em, libvlc_MediaPlayerTitleChanged, HandleMediaTitleChanged, (__bridge void *)(self));
  1150. libvlc_event_detach(p_em, libvlc_MediaPlayerChapterChanged, HandleMediaChapterChanged, (__bridge void *)(self));
  1151. libvlc_event_detach(p_em, libvlc_MediaPlayerSnapshotTaken, HandleMediaPlayerSnapshot, (__bridge void *)(self));
  1152. libvlc_event_detach(p_em, libvlc_MediaPlayerRecordChanged, HandleMediaPlayerRecord, (__bridge void *)(self));
  1153. }
  1154. - (dispatch_queue_t)libVLCBackgroundQueue
  1155. {
  1156. if (!_libVLCBackgroundQueue) {
  1157. _libVLCBackgroundQueue = dispatch_queue_create("libvlcQueue", DISPATCH_QUEUE_SERIAL);
  1158. }
  1159. return _libVLCBackgroundQueue;
  1160. }
  1161. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  1162. {
  1163. [self willChangeValueForKey:@"time"];
  1164. [self willChangeValueForKey:@"remainingTime"];
  1165. _cachedTime = [VLCTime timeWithNumber:newTime];
  1166. double currentTime = [[_cachedTime numberValue] doubleValue];
  1167. if (currentTime > 0 && _position > 0.) {
  1168. double remaining = currentTime / _position * (1 - _position);
  1169. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  1170. } else
  1171. _cachedRemainingTime = [VLCTime nullTime];
  1172. [self didChangeValueForKey:@"remainingTime"];
  1173. [self didChangeValueForKey:@"time"];
  1174. }
  1175. #if !TARGET_OS_IPHONE
  1176. - (void)delaySleep
  1177. {
  1178. UpdateSystemActivity(UsrActivity);
  1179. }
  1180. #endif
  1181. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  1182. {
  1183. #if !TARGET_OS_IPHONE
  1184. // This seems to be the most relevant place to delay sleeping and screen saver.
  1185. [self delaySleep];
  1186. #endif
  1187. [self willChangeValueForKey:@"position"];
  1188. _position = [newPosition floatValue];
  1189. [self didChangeValueForKey:@"position"];
  1190. }
  1191. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  1192. {
  1193. [self willChangeValueForKey:@"state"];
  1194. _cachedState = [newState intValue];
  1195. #if TARGET_OS_IPHONE
  1196. // Disable idle timer if player is playing media
  1197. // Exclusion can be made for audio only media
  1198. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1199. #endif
  1200. [self didChangeValueForKey:@"state"];
  1201. }
  1202. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1203. {
  1204. [self willChangeValueForKey:@"media"];
  1205. if (_media != newMedia) {
  1206. _media = newMedia;
  1207. [self willChangeValueForKey:@"time"];
  1208. [self willChangeValueForKey:@"remainingTime"];
  1209. [self willChangeValueForKey:@"position"];
  1210. _cachedTime = [VLCTime nullTime];
  1211. _cachedRemainingTime = [VLCTime nullTime];
  1212. _position = 0.0f;
  1213. [self didChangeValueForKey:@"position"];
  1214. [self didChangeValueForKey:@"remainingTime"];
  1215. [self didChangeValueForKey:@"time"];
  1216. }
  1217. [self didChangeValueForKey:@"media"];
  1218. }
  1219. - (void)mediaPlayerTitleChanged:(NSNumber *)newTitle
  1220. {
  1221. [self willChangeValueForKey:@"currentTitleIndex"];
  1222. [self didChangeValueForKey:@"currentTitleIndex"];
  1223. }
  1224. - (void)mediaPlayerChapterChanged:(NSNumber *)newChapter
  1225. {
  1226. [self willChangeValueForKey:@"currentChapterIndex"];
  1227. [self didChangeValueForKey:@"currentChapterIndex"];
  1228. }
  1229. - (void)mediaPlayerSnapshot:(NSString *)fileName
  1230. {
  1231. @synchronized(_snapshots) {
  1232. if (!_snapshots) {
  1233. _snapshots = [NSMutableArray array];
  1234. }
  1235. [_snapshots addObject:fileName];
  1236. }
  1237. }
  1238. - (void)mediaPlayerRecordChanged:(NSArray *)arguments
  1239. {
  1240. NSString *filePath = arguments.firstObject[@"filePath"];
  1241. BOOL isRecording = [arguments.firstObject[@"isRecording"] boolValue];
  1242. if (isRecording) {
  1243. if ([_delegate respondsToSelector:@selector(mediaPlayerStartedRecording:)]) {
  1244. [_delegate mediaPlayerStartedRecording:self];
  1245. }
  1246. } else {
  1247. if ([_delegate respondsToSelector:@selector(mediaPlayer:recordingStoppedAtPath:)]) {
  1248. [_delegate mediaPlayer:self recordingStoppedAtPath:filePath];
  1249. }
  1250. }
  1251. }
  1252. @end