VLCMediaPlayer.m 32 KB

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