VLCMediaPlayer.m 32 KB

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