VLCMediaPlayer.m 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2014 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2014 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 * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  50. {
  51. static NSString * stateToStrings[] = {
  52. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  53. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  54. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  55. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  56. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  57. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  58. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  59. };
  60. return stateToStrings[state];
  61. }
  62. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  63. {
  64. @autoreleasepool {
  65. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  66. withMethod:@selector(mediaPlayerTimeChanged:)
  67. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  68. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  69. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  70. withNotificationName:VLCMediaPlayerTimeChanged];
  71. }
  72. }
  73. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  74. {
  75. @autoreleasepool {
  76. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  77. withMethod:@selector(mediaPlayerPositionChanged:)
  78. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  79. }
  80. }
  81. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  82. {
  83. VLCMediaPlayerState newState;
  84. if (event->type == libvlc_MediaPlayerPlaying)
  85. newState = VLCMediaPlayerStatePlaying;
  86. else if (event->type == libvlc_MediaPlayerPaused)
  87. newState = VLCMediaPlayerStatePaused;
  88. else if (event->type == libvlc_MediaPlayerEndReached || event->type == libvlc_MediaPlayerStopped)
  89. newState = VLCMediaPlayerStateStopped;
  90. else if (event->type == libvlc_MediaPlayerEncounteredError)
  91. newState = VLCMediaPlayerStateError;
  92. else if (event->type == libvlc_MediaPlayerBuffering)
  93. newState = VLCMediaPlayerStateBuffering;
  94. else if (event->type == libvlc_MediaPlayerOpening)
  95. newState = VLCMediaPlayerStateOpening;
  96. else {
  97. VKLog(@"%s: Unknown event", __FUNCTION__);
  98. return;
  99. }
  100. @autoreleasepool {
  101. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  102. withMethod:@selector(mediaPlayerStateChanged:)
  103. withArgumentAsObject:@(newState)];
  104. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:(__bridge id)(self)
  105. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  106. withNotificationName:VLCMediaPlayerStateChanged];
  107. }
  108. }
  109. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  110. {
  111. @autoreleasepool {
  112. [[VLCEventManager sharedManager] callOnMainThreadObject:(__bridge id)(self)
  113. withMethod:@selector(mediaPlayerMediaChanged:)
  114. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  115. }
  116. }
  117. // TODO: Documentation
  118. @interface VLCMediaPlayer (Private)
  119. - (id)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  120. - (void)registerObservers;
  121. - (void)unregisterObservers;
  122. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  123. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  124. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  125. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  126. @end
  127. @interface VLCMediaPlayer ()
  128. {
  129. VLCLibrary *_privateLibrary;
  130. void * _playerInstance; // Internal
  131. VLCMedia * _media; //< Current media being played
  132. VLCTime * _cachedTime; //< Cached time of the media being played
  133. VLCTime * _cachedRemainingTime; //< Cached remaining time of the media being played
  134. VLCMediaPlayerState _cachedState; //< Cached state of the media being played
  135. float _position; //< The position of the media being played
  136. id _drawable; //< The drawable associated to this media player
  137. VLCAudio *_audio;
  138. libvlc_equalizer_t *_equalizerInstance;
  139. BOOL _equalizerEnabled;
  140. }
  141. @end
  142. @implementation VLCMediaPlayer
  143. @synthesize libraryInstance = _privateLibrary;
  144. /* Bindings */
  145. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  146. {
  147. static NSDictionary * dict = nil;
  148. NSSet * superKeyPaths;
  149. if (!dict) {
  150. dict = @{@"playing": [NSSet setWithObject:@"state"],
  151. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  152. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  153. @"description": [NSSet setWithObjects:@"state", @"media", nil]};
  154. }
  155. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  156. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  157. [ret unionSet:superKeyPaths];
  158. return ret;
  159. }
  160. return dict[key];
  161. }
  162. /* Constructor */
  163. - (instancetype)init
  164. {
  165. return [self initWithDrawable:nil options:nil];
  166. }
  167. #if !TARGET_OS_IPHONE
  168. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView
  169. {
  170. return [self initWithDrawable: aVideoView options:nil];
  171. }
  172. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  173. {
  174. return [self initWithDrawable: aVideoLayer options:nil];
  175. }
  176. - (instancetype)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
  177. {
  178. return [self initWithDrawable: aVideoView options:options];
  179. }
  180. - (instancetype)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
  181. {
  182. return [self initWithDrawable: aVideoLayer options:options];
  183. }
  184. #endif
  185. - (instancetype)initWithOptions:(NSArray *)options
  186. {
  187. return [self initWithDrawable:nil options:options];
  188. }
  189. - (void)dealloc
  190. {
  191. 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");
  192. [self unregisterObservers];
  193. [[VLCEventManager sharedManager] cancelCallToObject:self];
  194. // Always get rid of the delegate first so we can stop sending messages to it
  195. // TODO: Should we tell the delegate that we're shutting down?
  196. _delegate = nil;
  197. // Clear our drawable as we are going to release it, we don't
  198. // want the core to use it from this point. This won't happen as
  199. // the media player must be stopped.
  200. libvlc_media_player_set_nsobject(_playerInstance, nil);
  201. if (_equalizerInstance) {
  202. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  203. libvlc_audio_equalizer_release(_equalizerInstance);
  204. }
  205. libvlc_media_player_release(_playerInstance);
  206. if (_privateLibrary != [VLCLibrary sharedLibrary])
  207. libvlc_release(_privateLibrary.instance);
  208. }
  209. #if !TARGET_OS_IPHONE
  210. - (void)setVideoView:(VLCVideoView *)aVideoView
  211. {
  212. [self setDrawable: aVideoView];
  213. }
  214. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  215. {
  216. [self setDrawable: aVideoLayer];
  217. }
  218. #endif
  219. - (void)setDrawable:(id)aDrawable
  220. {
  221. // Make sure that this instance has been associated with the drawing canvas.
  222. libvlc_media_player_set_nsobject(_playerInstance, (__bridge void *)(aDrawable));
  223. }
  224. - (id)drawable
  225. {
  226. return (__bridge id)(libvlc_media_player_get_nsobject(_playerInstance));
  227. }
  228. - (VLCAudio *)audio
  229. {
  230. if (!_audio)
  231. _audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  232. return _audio;
  233. }
  234. #pragma mark -
  235. #pragma mark Video Tracks
  236. - (void)setCurrentVideoTrackIndex:(NSUInteger)value
  237. {
  238. libvlc_video_set_track(_playerInstance, (int)value);
  239. }
  240. - (NSUInteger)currentVideoTrackIndex
  241. {
  242. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  243. if (count <= 0)
  244. return NSNotFound;
  245. NSUInteger result = libvlc_video_get_track(_playerInstance);
  246. return result;
  247. }
  248. - (NSArray *)videoTrackNames
  249. {
  250. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  251. if (count <= 0)
  252. return @[];
  253. libvlc_track_description_t *currentTrack = libvlc_video_get_track_description(_playerInstance);
  254. NSMutableArray *tempArray = [NSMutableArray array];
  255. while (currentTrack) {
  256. [tempArray addObject:@(currentTrack->psz_name)];
  257. currentTrack = currentTrack->p_next;
  258. }
  259. libvlc_track_description_list_release(currentTrack);
  260. return [NSArray arrayWithArray: tempArray];
  261. }
  262. - (NSArray *)videoTrackIndexes
  263. {
  264. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  265. if (count <= 0)
  266. return @[];
  267. libvlc_track_description_t *currentTrack = libvlc_video_get_track_description(_playerInstance);
  268. NSMutableArray *tempArray = [NSMutableArray array];
  269. while (currentTrack) {
  270. [tempArray addObject:@(currentTrack->i_id)];
  271. currentTrack = currentTrack->p_next;
  272. }
  273. libvlc_track_description_list_release(currentTrack);
  274. return [NSArray arrayWithArray: tempArray];
  275. }
  276. - (NSArray *)videoTracks
  277. {
  278. NSInteger count = libvlc_video_get_track_count(_playerInstance);
  279. if (count <= 0)
  280. return @[];
  281. libvlc_track_description_t *tracks = libvlc_video_get_track_description(_playerInstance);
  282. NSMutableArray *tempArray = [NSMutableArray array];
  283. for (NSUInteger i = 0; i < count ; i++) {
  284. [tempArray addObject:@(tracks->psz_name)];
  285. tracks = tracks->p_next;
  286. }
  287. libvlc_track_description_list_release(tracks);
  288. return [NSArray arrayWithArray: tempArray];
  289. }
  290. #pragma mark -
  291. #pragma mark Subtitles
  292. - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
  293. {
  294. libvlc_video_set_spu(_playerInstance, (int)index);
  295. }
  296. - (NSUInteger)currentVideoSubTitleIndex
  297. {
  298. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  299. if (count <= 0)
  300. return NSNotFound;
  301. return libvlc_video_get_spu(_playerInstance);
  302. }
  303. - (NSArray *)videoSubTitlesNames
  304. {
  305. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  306. if (count <= 0)
  307. return @[];
  308. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(_playerInstance);
  309. NSMutableArray *tempArray = [NSMutableArray array];
  310. while (currentTrack) {
  311. [tempArray addObject:@(currentTrack->psz_name)];
  312. currentTrack = currentTrack->p_next;
  313. }
  314. libvlc_track_description_list_release(currentTrack);
  315. return [NSArray arrayWithArray: tempArray];
  316. }
  317. - (NSArray *)videoSubTitlesIndexes
  318. {
  319. NSInteger count = libvlc_video_get_spu_count(_playerInstance);
  320. if (count <= 0)
  321. return @[];
  322. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(_playerInstance);
  323. NSMutableArray *tempArray = [NSMutableArray array];
  324. while (currentTrack) {
  325. [tempArray addObject:@(currentTrack->i_id)];
  326. currentTrack = currentTrack->p_next;
  327. }
  328. libvlc_track_description_list_release(currentTrack);
  329. return [NSArray arrayWithArray: tempArray];
  330. }
  331. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  332. {
  333. return libvlc_video_set_subtitle_file(_playerInstance, [path UTF8String]);
  334. }
  335. - (NSArray *)videoSubTitles
  336. {
  337. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(_playerInstance);
  338. NSMutableArray *tempArray = [NSMutableArray array];
  339. while (currentTrack) {
  340. [tempArray addObject:@(currentTrack->psz_name)];
  341. currentTrack = currentTrack->p_next;
  342. }
  343. libvlc_track_description_list_release(currentTrack);
  344. return [NSArray arrayWithArray: tempArray];
  345. }
  346. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  347. {
  348. libvlc_video_set_spu_delay(_playerInstance, index);
  349. }
  350. - (NSInteger)currentVideoSubTitleDelay
  351. {
  352. return libvlc_video_get_spu_delay(_playerInstance);
  353. }
  354. #if TARGET_OS_IPHONE
  355. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  356. {
  357. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  358. }
  359. #endif
  360. #if TARGET_OS_IPHONE
  361. - (void)setTextRendererFont:(NSString *)fontname
  362. {
  363. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  364. }
  365. #endif
  366. #if TARGET_OS_IPHONE
  367. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  368. {
  369. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  370. }
  371. #endif
  372. #pragma mark -
  373. #pragma mark Video Crop geometry
  374. - (void)setVideoCropGeometry:(char *)value
  375. {
  376. libvlc_video_set_crop_geometry(_playerInstance, value);
  377. }
  378. - (char *)videoCropGeometry
  379. {
  380. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  381. return result;
  382. }
  383. - (void)setVideoAspectRatio:(char *)value
  384. {
  385. libvlc_video_set_aspect_ratio(_playerInstance, value);
  386. }
  387. - (char *)videoAspectRatio
  388. {
  389. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  390. return result;
  391. }
  392. - (void)setScaleFactor:(float)value
  393. {
  394. libvlc_video_set_scale(_playerInstance, value);
  395. }
  396. - (float)scaleFactor
  397. {
  398. return libvlc_video_get_scale(_playerInstance);
  399. }
  400. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(int)width andHeight:(int)height
  401. {
  402. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  403. if (failure)
  404. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  405. }
  406. - (void)setDeinterlaceFilter:(NSString *)name
  407. {
  408. if (!name || name.length < 1)
  409. libvlc_video_set_deinterlace(_playerInstance, NULL);
  410. else
  411. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  412. }
  413. - (BOOL)adjustFilterEnabled
  414. {
  415. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  416. }
  417. - (void)setAdjustFilterEnabled:(BOOL)b_value
  418. {
  419. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  420. }
  421. - (float)contrast
  422. {
  423. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  424. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  425. }
  426. - (void)setContrast:(float)f_value
  427. {
  428. if (f_value <= 2. && f_value >= 0.) {
  429. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  430. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  431. }
  432. }
  433. - (float)brightness
  434. {
  435. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  436. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  437. }
  438. - (void)setBrightness:(float)f_value
  439. {
  440. if (f_value <= 2. && f_value >= 0.) {
  441. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  442. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  443. }
  444. }
  445. - (int)hue
  446. {
  447. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  448. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Hue);
  449. }
  450. - (void)setHue:(int)i_value
  451. {
  452. if (i_value <= 360 && i_value >= 0) {
  453. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  454. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Hue, i_value);
  455. }
  456. }
  457. - (float)saturation
  458. {
  459. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  460. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  461. }
  462. - (void)setSaturation:(float)f_value
  463. {
  464. if (f_value <= 3. && f_value >= 0.) {
  465. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  466. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  467. }
  468. }
  469. - (float)gamma
  470. {
  471. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  472. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  473. }
  474. - (void)setGamma:(float)f_value
  475. {
  476. if (f_value <= 10. && f_value >= 0.) {
  477. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  478. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  479. }
  480. }
  481. - (void)setRate:(float)value
  482. {
  483. libvlc_media_player_set_rate(_playerInstance, value);
  484. }
  485. - (float)rate
  486. {
  487. return libvlc_media_player_get_rate(_playerInstance);
  488. }
  489. - (CGSize)videoSize
  490. {
  491. unsigned height = 0, width = 0;
  492. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  493. if (failure)
  494. return CGSizeZero;
  495. return CGSizeMake(width, height);
  496. }
  497. - (BOOL)hasVideoOut
  498. {
  499. return libvlc_media_player_has_vout(_playerInstance);
  500. }
  501. - (float)framesPerSecond
  502. {
  503. return libvlc_media_player_get_fps(_playerInstance);
  504. }
  505. - (void)setTime:(VLCTime *)value
  506. {
  507. // Time is managed in seconds, while duration is managed in microseconds
  508. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  509. libvlc_media_player_set_time(_playerInstance, value ? [[value numberValue] longLongValue] : 0);
  510. }
  511. - (VLCTime *)time
  512. {
  513. return _cachedTime;
  514. }
  515. - (VLCTime *)remainingTime
  516. {
  517. return _cachedRemainingTime;
  518. }
  519. - (NSUInteger)fps
  520. {
  521. return libvlc_media_player_get_fps(_playerInstance);
  522. }
  523. #pragma mark -
  524. #pragma mark Chapters
  525. - (void)setCurrentChapterIndex:(int)value;
  526. {
  527. libvlc_media_player_set_chapter(_playerInstance, value);
  528. }
  529. - (int)currentChapterIndex
  530. {
  531. int count = libvlc_media_player_get_chapter_count(_playerInstance);
  532. if (count <= 0)
  533. return NSNotFound;
  534. int result = libvlc_media_player_get_chapter(_playerInstance);
  535. return result;
  536. }
  537. - (void)nextChapter
  538. {
  539. libvlc_media_player_next_chapter(_playerInstance);
  540. }
  541. - (void)previousChapter
  542. {
  543. libvlc_media_player_previous_chapter(_playerInstance);
  544. }
  545. - (NSArray *)chaptersForTitleIndex:(int)title
  546. {
  547. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  548. if (count <= 0)
  549. return @[];
  550. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(_playerInstance, title);
  551. NSMutableArray *tempArray = [NSMutableArray array];
  552. for (NSInteger i = 0; i < count ; i++) {
  553. [tempArray addObject:@(tracks->psz_name)];
  554. tracks = tracks->p_next;
  555. }
  556. libvlc_track_description_list_release(tracks);
  557. return [NSArray arrayWithArray:tempArray];
  558. }
  559. #pragma mark -
  560. #pragma mark Titles
  561. - (void)setCurrentTitleIndex:(int)value
  562. {
  563. libvlc_media_player_set_title(_playerInstance, value);
  564. }
  565. - (int)currentTitleIndex
  566. {
  567. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  568. if (count <= 0)
  569. return NSNotFound;
  570. return libvlc_media_player_get_title(_playerInstance);
  571. }
  572. - (NSUInteger)countOfTitles
  573. {
  574. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  575. return result;
  576. }
  577. - (NSArray *)titles
  578. {
  579. libvlc_track_description_t *tracks = libvlc_video_get_title_description(_playerInstance);
  580. NSMutableArray *tempArray = [NSMutableArray array];
  581. for (NSInteger i = 0; i < [self countOfTitles] ; i++) {
  582. [tempArray addObject:@(tracks->psz_name)];
  583. tracks = tracks->p_next;
  584. }
  585. libvlc_track_description_list_release(tracks);
  586. return [NSArray arrayWithArray: tempArray];
  587. }
  588. #pragma mark -
  589. #pragma mark Audio tracks
  590. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  591. {
  592. libvlc_audio_set_track(_playerInstance, (int)value);
  593. }
  594. - (NSUInteger)currentAudioTrackIndex
  595. {
  596. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  597. if (count <= 0)
  598. return NSNotFound;
  599. NSUInteger result = libvlc_audio_get_track(_playerInstance);
  600. return result;
  601. }
  602. - (NSArray *)audioTrackNames
  603. {
  604. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  605. if (count <= 0)
  606. return @[];
  607. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  608. NSMutableArray *tempArray = [NSMutableArray array];
  609. while (currentTrack) {
  610. [tempArray addObject:@(currentTrack->psz_name)];
  611. currentTrack = currentTrack->p_next;
  612. }
  613. libvlc_track_description_list_release(currentTrack);
  614. return [NSArray arrayWithArray: tempArray];
  615. }
  616. - (NSArray *)audioTrackIndexes
  617. {
  618. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  619. if (count <= 0)
  620. return @[];
  621. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  622. NSMutableArray *tempArray = [NSMutableArray array];
  623. while (currentTrack) {
  624. [tempArray addObject:@(currentTrack->i_id)];
  625. currentTrack = currentTrack->p_next;
  626. }
  627. libvlc_track_description_list_release(currentTrack);
  628. return [NSArray arrayWithArray: tempArray];
  629. }
  630. - (NSArray *)audioTracks
  631. {
  632. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  633. if (count <= 0)
  634. return @[];
  635. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(_playerInstance);
  636. NSMutableArray *tempArray = [NSMutableArray array];
  637. for (NSUInteger i = 0; i < count ; i++) {
  638. [tempArray addObject:@(tracks->psz_name)];
  639. tracks = tracks->p_next;
  640. }
  641. libvlc_track_description_list_release(tracks);
  642. return [NSArray arrayWithArray: tempArray];
  643. }
  644. - (void)setAudioChannel:(int)value
  645. {
  646. libvlc_audio_set_channel(_playerInstance, value);
  647. }
  648. - (int)audioChannel
  649. {
  650. return libvlc_audio_get_channel(_playerInstance);
  651. }
  652. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  653. {
  654. libvlc_audio_set_delay(_playerInstance, index);
  655. }
  656. - (NSInteger)currentAudioPlaybackDelay
  657. {
  658. return libvlc_audio_get_delay(_playerInstance);
  659. }
  660. #pragma mark -
  661. #pragma mark equalizer
  662. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  663. {
  664. _equalizerEnabled = equalizerEnabled;
  665. if (!_equalizerEnabled) {
  666. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  667. if (_equalizerInstance)
  668. libvlc_audio_equalizer_release(_equalizerInstance);
  669. return;
  670. }
  671. if (!_equalizerInstance)
  672. _equalizerInstance = libvlc_audio_equalizer_new();
  673. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  674. }
  675. - (BOOL)equalizerEnabled
  676. {
  677. return _equalizerEnabled;
  678. }
  679. - (NSArray *)equalizerProfiles
  680. {
  681. unsigned count = libvlc_audio_equalizer_get_preset_count();
  682. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  683. for (unsigned x = 0; x < count; x++)
  684. [array addObject:@(libvlc_audio_equalizer_get_preset_name(x))];
  685. return [NSArray arrayWithArray:array];
  686. }
  687. - (void)resetEqualizerFromProfile:(unsigned)profile
  688. {
  689. BOOL wasactive = NO;
  690. if (_equalizerInstance) {
  691. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  692. libvlc_audio_equalizer_release(_equalizerInstance);
  693. wasactive = YES;
  694. }
  695. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  696. if (wasactive)
  697. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  698. }
  699. - (CGFloat)preAmplification
  700. {
  701. if (!_equalizerInstance)
  702. return 0.;
  703. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  704. }
  705. - (void)setPreAmplification:(CGFloat)preAmplification
  706. {
  707. if (!_equalizerInstance)
  708. _equalizerInstance = libvlc_audio_equalizer_new();
  709. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  710. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  711. }
  712. - (unsigned)numberOfBands
  713. {
  714. return libvlc_audio_equalizer_get_band_count();
  715. }
  716. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  717. {
  718. return libvlc_audio_equalizer_get_band_frequency(index);
  719. }
  720. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  721. {
  722. if (!_equalizerInstance)
  723. _equalizerInstance = libvlc_audio_equalizer_new();
  724. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  725. }
  726. - (CGFloat)amplificationOfBand:(unsigned int)index
  727. {
  728. if (!_equalizerInstance)
  729. return 0.;
  730. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  731. }
  732. #pragma mark -
  733. #pragma mark set/get media
  734. - (void)setMedia:(VLCMedia *)value
  735. {
  736. if (_media != value) {
  737. if (_media && [_media compare:value] == NSOrderedSame)
  738. return;
  739. _media = value;
  740. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  741. }
  742. }
  743. - (VLCMedia *)media
  744. {
  745. return _media;
  746. }
  747. #pragma mark -
  748. #pragma mark playback
  749. - (BOOL)play
  750. {
  751. libvlc_media_player_play(_playerInstance);
  752. return YES;
  753. }
  754. - (void)pause
  755. {
  756. if ([NSThread isMainThread]) {
  757. /* Hack because we create a dead lock here, when the vout is stopped
  758. * and tries to recontact us on the main thread */
  759. /* FIXME: to do this properly we need to do some locking. We may want
  760. * to move that to libvlc */
  761. [self performSelectorInBackground:@selector(pause) withObject:nil];
  762. return;
  763. }
  764. // Pause the stream
  765. libvlc_media_player_pause(_playerInstance);
  766. }
  767. - (void)stop
  768. {
  769. if ([NSThread isMainThread]) {
  770. /* Hack because we create a dead lock here, when the vout is stopped
  771. * and tries to recontact us on the main thread */
  772. /* FIXME: to do this properly we need to do some locking. We may want
  773. * to move that to libvlc */
  774. [self performSelectorInBackground:@selector(stop) withObject:nil];
  775. return;
  776. }
  777. libvlc_media_player_stop(_playerInstance);
  778. }
  779. - (void)gotoNextFrame
  780. {
  781. libvlc_media_player_next_frame(_playerInstance);
  782. }
  783. - (void)fastForward
  784. {
  785. [self fastForwardAtRate: 2.0];
  786. }
  787. - (void)fastForwardAtRate:(float)rate
  788. {
  789. [self setRate:rate];
  790. }
  791. - (void)rewind
  792. {
  793. [self rewindAtRate: 2.0];
  794. }
  795. - (void)rewindAtRate:(float)rate
  796. {
  797. [self setRate: -rate];
  798. }
  799. - (void)jumpBackward:(int)interval
  800. {
  801. if ([self isSeekable]) {
  802. interval = interval * 1000;
  803. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  804. }
  805. }
  806. - (void)jumpForward:(int)interval
  807. {
  808. if ([self isSeekable]) {
  809. interval = interval * 1000;
  810. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  811. }
  812. }
  813. - (void)extraShortJumpBackward
  814. {
  815. [self jumpBackward:3];
  816. }
  817. - (void)extraShortJumpForward
  818. {
  819. [self jumpForward:3];
  820. }
  821. - (void)shortJumpBackward
  822. {
  823. [self jumpBackward:10];
  824. }
  825. - (void)shortJumpForward
  826. {
  827. [self jumpForward:10];
  828. }
  829. - (void)mediumJumpBackward
  830. {
  831. [self jumpBackward:60];
  832. }
  833. - (void)mediumJumpForward
  834. {
  835. [self jumpForward:60];
  836. }
  837. - (void)longJumpBackward
  838. {
  839. [self jumpBackward:300];
  840. }
  841. - (void)longJumpForward
  842. {
  843. [self jumpForward:300];
  844. }
  845. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  846. {
  847. return [NSSet setWithObjects:@"state", nil];
  848. }
  849. - (BOOL)isPlaying
  850. {
  851. return libvlc_media_player_is_playing(_playerInstance);
  852. }
  853. - (BOOL)willPlay
  854. {
  855. return libvlc_media_player_will_play(_playerInstance);
  856. }
  857. - (VLCMediaPlayerState)state
  858. {
  859. return _cachedState;
  860. }
  861. - (float)position
  862. {
  863. return _position;
  864. }
  865. - (void)setPosition:(float)newPosition
  866. {
  867. libvlc_media_player_set_position(_playerInstance, newPosition);
  868. }
  869. - (BOOL)isSeekable
  870. {
  871. return libvlc_media_player_is_seekable(_playerInstance);
  872. }
  873. - (BOOL)canPause
  874. {
  875. return libvlc_media_player_can_pause(_playerInstance);
  876. }
  877. - (void *)libVLCMediaPlayer
  878. {
  879. return _playerInstance;
  880. }
  881. @end
  882. @implementation VLCMediaPlayer (Private)
  883. - (id)initWithDrawable:(id)aDrawable options:(NSArray *)options
  884. {
  885. if (self = [super init]) {
  886. _cachedTime = [VLCTime nullTime];
  887. _cachedRemainingTime = [VLCTime nullTime];
  888. _position = 0.0f;
  889. _cachedState = VLCMediaPlayerStateStopped;
  890. // Create a media instance, it doesn't matter what library we start off with
  891. // it will change depending on the media descriptor provided to the media
  892. // instance
  893. if (options && options.count > 0) {
  894. VKLog(@"creating player instance with private library as options were given");
  895. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  896. } else {
  897. VKLog(@"creating player instance using shared library");
  898. _privateLibrary = [VLCLibrary sharedLibrary];
  899. }
  900. libvlc_retain([_privateLibrary instance]);
  901. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  902. libvlc_media_player_retain(_playerInstance);
  903. [self registerObservers];
  904. [self setDrawable:aDrawable];
  905. }
  906. return self;
  907. }
  908. - (void)registerObservers
  909. {
  910. // Attach event observers into the media instance
  911. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  912. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  913. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  914. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  915. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  916. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  917. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  918. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  919. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  920. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  921. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  922. }
  923. - (void)unregisterObservers
  924. {
  925. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  926. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  927. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  928. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  929. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  930. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  931. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  932. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, (__bridge void *)(self));
  933. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, (__bridge void *)(self));
  934. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, (__bridge void *)(self));
  935. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, (__bridge void *)(self));
  936. }
  937. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  938. {
  939. [self willChangeValueForKey:@"time"];
  940. [self willChangeValueForKey:@"remainingTime"];
  941. _cachedTime = [VLCTime timeWithNumber:newTime];
  942. double currentTime = [[_cachedTime numberValue] doubleValue];
  943. if (currentTime > 0) {
  944. double remaining = currentTime / _position * (1 - _position);
  945. _cachedRemainingTime = [VLCTime timeWithNumber:@(-remaining)];
  946. } else
  947. _cachedRemainingTime = [VLCTime nullTime];
  948. [self didChangeValueForKey:@"remainingTime"];
  949. [self didChangeValueForKey:@"time"];
  950. }
  951. #if !TARGET_OS_IPHONE
  952. - (void)delaySleep
  953. {
  954. UpdateSystemActivity(UsrActivity);
  955. }
  956. #endif
  957. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  958. {
  959. #if !TARGET_OS_IPHONE
  960. // This seems to be the most relevant place to delay sleeping and screen saver.
  961. [self delaySleep];
  962. #endif
  963. [self willChangeValueForKey:@"position"];
  964. _position = [newPosition floatValue];
  965. [self didChangeValueForKey:@"position"];
  966. }
  967. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  968. {
  969. [self willChangeValueForKey:@"state"];
  970. _cachedState = [newState intValue];
  971. #if TARGET_OS_IPHONE
  972. // Disable idle timer if player is playing media
  973. // Exclusion can be made for audio only media
  974. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  975. #endif
  976. [self didChangeValueForKey:@"state"];
  977. }
  978. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  979. {
  980. [self willChangeValueForKey:@"media"];
  981. if (_media != newMedia)
  982. _media = newMedia;
  983. [self didChangeValueForKey:@"media"];
  984. }
  985. @end