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