VLCMediaPlayer.m 34 KB

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