VLCMediaPlayer.m 35 KB

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