VLCMediaPlayer.m 32 KB

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