VLCMediaPlayer.m 45 KB

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