VLCMediaPlayer.m 46 KB

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