VLCMediaPlayer.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2014 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2014 Felix Paul Kühne
  7. * $Id$
  8. *
  9. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  10. * Faustion Osuna <enrique.osuna # gmail.com>
  11. * Felix Paul Kühne <fkuehne # videolan.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU Lesser General Public License as published by
  15. * the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  26. *****************************************************************************/
  27. #import "VLCLibrary.h"
  28. #import "VLCMediaPlayer.h"
  29. #import "VLCEventManager.h"
  30. #import "VLCLibVLCBridging.h"
  31. #if !TARGET_OS_IPHONE
  32. # import "VLCVideoView.h"
  33. #endif
  34. #ifdef HAVE_CONFIG_H
  35. # include "config.h"
  36. #endif
  37. #if !TARGET_OS_IPHONE
  38. /* prevent system sleep */
  39. # import <CoreServices/CoreServices.h>
  40. /* FIXME: Ugly hack! */
  41. # ifdef __x86_64__
  42. # import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  43. # endif
  44. #endif
  45. #include <vlc/vlc.h>
  46. /* Notification Messages */
  47. NSString *const VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  48. NSString *const VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  49. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  50. {
  51. static NSString * stateToStrings[] = {
  52. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  53. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  54. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  55. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  56. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  57. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  58. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  59. };
  60. return stateToStrings[state];
  61. }
  62. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  63. {
  64. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  65. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  66. withMethod:@selector(mediaPlayerTimeChanged:)
  67. withArgumentAsObject:@(event->u.media_player_time_changed.new_time)];
  68. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  69. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  70. withNotificationName:VLCMediaPlayerTimeChanged];
  71. [pool release];
  72. }
  73. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  74. {
  75. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  76. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  77. withMethod:@selector(mediaPlayerPositionChanged:)
  78. withArgumentAsObject:@(event->u.media_player_position_changed.new_position)];
  79. [pool release];
  80. }
  81. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  82. {
  83. VLCMediaPlayerState newState;
  84. if (event->type == libvlc_MediaPlayerPlaying)
  85. newState = VLCMediaPlayerStatePlaying;
  86. else if (event->type == libvlc_MediaPlayerPaused)
  87. newState = VLCMediaPlayerStatePaused;
  88. else if (event->type == libvlc_MediaPlayerEndReached || event->type == libvlc_MediaPlayerStopped)
  89. newState = VLCMediaPlayerStateStopped;
  90. else if (event->type == libvlc_MediaPlayerEncounteredError)
  91. newState = VLCMediaPlayerStateError;
  92. else if (event->type == libvlc_MediaPlayerBuffering)
  93. newState = VLCMediaPlayerStateBuffering;
  94. else if (event->type == libvlc_MediaPlayerOpening)
  95. newState = VLCMediaPlayerStateOpening;
  96. else {
  97. VKLog(@"%s: Unknown event", __FUNCTION__);
  98. return;
  99. }
  100. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  101. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  102. withMethod:@selector(mediaPlayerStateChanged:)
  103. withArgumentAsObject:@(newState)];
  104. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  105. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  106. withNotificationName:VLCMediaPlayerStateChanged];
  107. [pool release];
  108. }
  109. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  110. {
  111. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  112. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  113. withMethod:@selector(mediaPlayerMediaChanged:)
  114. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  115. [pool release];
  116. }
  117. // TODO: Documentation
  118. @interface VLCMediaPlayer (Private)
  119. - (id)initWithDrawable:(id)aDrawable options:(NSArray *)options;
  120. - (void)registerObservers;
  121. - (void)unregisterObservers;
  122. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  123. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  124. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  125. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  126. @end
  127. @interface VLCMediaPlayer ()
  128. {
  129. VLCLibrary *_privateLibrary;
  130. id _delegate; //< Object delegate
  131. void * _playerInstance; // Internal
  132. VLCMedia * _media; //< Current media being played
  133. VLCTime * _cachedTime; //< Cached time of the media being played
  134. VLCTime * _cachedRemainingTime; //< Cached remaining time of the media being played
  135. VLCMediaPlayerState _cachedState; //< Cached state of the media being played
  136. float _position; //< The position of the media being played
  137. id _drawable; //< The drawable associated to this media player
  138. VLCAudio *_audio;
  139. libvlc_equalizer_t *_equalizerInstance;
  140. BOOL _equalizerEnabled;
  141. }
  142. @end
  143. @implementation VLCMediaPlayer
  144. @synthesize libraryInstance = _privateLibrary;
  145. /* Bindings */
  146. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  147. {
  148. static NSDictionary * dict = nil;
  149. NSSet * superKeyPaths;
  150. if (!dict) {
  151. dict = [@{@"playing": [NSSet setWithObject:@"state"],
  152. @"seekable": [NSSet setWithObjects:@"state", @"media", nil],
  153. @"canPause": [NSSet setWithObjects:@"state", @"media", nil],
  154. @"description": [NSSet setWithObjects:@"state", @"media", nil]} retain];
  155. }
  156. if ((superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key])) {
  157. NSMutableSet * ret = [NSMutableSet setWithSet:dict[key]];
  158. [ret unionSet:superKeyPaths];
  159. return ret;
  160. }
  161. return dict[key];
  162. }
  163. /* Constructor */
  164. - (id)init
  165. {
  166. return [self initWithDrawable:nil options:nil];
  167. }
  168. #if !TARGET_OS_IPHONE
  169. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  170. {
  171. return [self initWithDrawable: aVideoView options:nil];
  172. }
  173. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  174. {
  175. return [self initWithDrawable: aVideoLayer options:nil];
  176. }
  177. - (id)initWithVideoView:(VLCVideoView *)aVideoView options:(NSArray *)options
  178. {
  179. return [self initWithDrawable: aVideoView options:options];
  180. }
  181. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer options:(NSArray *)options
  182. {
  183. return [self initWithDrawable: aVideoLayer options:options];
  184. }
  185. #endif
  186. - (id)initWithOptions:(NSArray *)options
  187. {
  188. return [self initWithDrawable:nil options:options];
  189. }
  190. - (void)dealloc
  191. {
  192. if (libvlc_media_player_get_state(_playerInstance) != libvlc_Stopped)
  193. [self stop];
  194. NSAssert(libvlc_media_player_get_state(_playerInstance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
  195. [self unregisterObservers];
  196. [[VLCEventManager sharedManager] cancelCallToObject:self];
  197. // Always get rid of the delegate first so we can stop sending messages to it
  198. // TODO: Should we tell the delegate that we're shutting down?
  199. _delegate = nil;
  200. // Clear our drawable as we are going to release it, we don't
  201. // want the core to use it from this point. This won't happen as
  202. // the media player must be stopped.
  203. libvlc_media_player_set_nsobject(_playerInstance, nil);
  204. if (_equalizerInstance) {
  205. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  206. libvlc_audio_equalizer_release(_equalizerInstance);
  207. }
  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. if (!name || name.length < 1)
  411. libvlc_video_set_deinterlace(_playerInstance, NULL);
  412. else
  413. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  414. }
  415. - (BOOL)adjustFilterEnabled
  416. {
  417. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  418. }
  419. - (void)setAdjustFilterEnabled:(BOOL)b_value
  420. {
  421. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  422. }
  423. - (float)contrast
  424. {
  425. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  426. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  427. }
  428. - (void)setContrast:(float)f_value
  429. {
  430. if (f_value <= 2. && f_value >= 0.) {
  431. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  432. libvlc_video_set_adjust_float(_playerInstance,libvlc_adjust_Contrast, f_value);
  433. }
  434. }
  435. - (float)brightness
  436. {
  437. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  438. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  439. }
  440. - (void)setBrightness:(float)f_value
  441. {
  442. if (f_value <= 2. && f_value >= 0.) {
  443. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  444. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  445. }
  446. }
  447. - (NSInteger)hue
  448. {
  449. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  450. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Hue);
  451. }
  452. - (void)setHue:(NSInteger)i_value
  453. {
  454. if (i_value <= 360 && i_value >= 0) {
  455. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  456. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Hue, i_value);
  457. }
  458. }
  459. - (float)saturation
  460. {
  461. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  462. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  463. }
  464. - (void)setSaturation:(float)f_value
  465. {
  466. if (f_value <= 3. && f_value >= 0.) {
  467. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  468. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  469. }
  470. }
  471. - (float)gamma
  472. {
  473. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  474. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  475. }
  476. - (void)setGamma:(float)f_value
  477. {
  478. if (f_value <= 10. && f_value >= 0.) {
  479. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  480. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  481. }
  482. }
  483. - (void)setRate:(float)value
  484. {
  485. libvlc_media_player_set_rate(_playerInstance, value);
  486. }
  487. - (float)rate
  488. {
  489. return libvlc_media_player_get_rate(_playerInstance);
  490. }
  491. - (CGSize)videoSize
  492. {
  493. unsigned height = 0, width = 0;
  494. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  495. if (failure)
  496. [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
  497. return CGSizeMake(width, height);
  498. }
  499. - (BOOL)hasVideoOut
  500. {
  501. return libvlc_media_player_has_vout(_playerInstance);
  502. }
  503. - (float)framesPerSecond
  504. {
  505. return libvlc_media_player_get_fps(_playerInstance);
  506. }
  507. - (void)setTime:(VLCTime *)value
  508. {
  509. // Time is managed in seconds, while duration is managed in microseconds
  510. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  511. libvlc_media_player_set_time(_playerInstance, value ? [[value numberValue] longLongValue] : 0);
  512. }
  513. - (VLCTime *)time
  514. {
  515. return _cachedTime;
  516. }
  517. - (VLCTime *)remainingTime
  518. {
  519. return _cachedRemainingTime;
  520. }
  521. - (NSUInteger)fps
  522. {
  523. return libvlc_media_player_get_fps(_playerInstance);
  524. }
  525. #pragma mark -
  526. #pragma mark Chapters
  527. - (void)setCurrentChapterIndex:(NSUInteger)value;
  528. {
  529. libvlc_media_player_set_chapter(_playerInstance, value);
  530. }
  531. - (NSUInteger)currentChapterIndex
  532. {
  533. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  534. if (count <= 0)
  535. return NSNotFound;
  536. NSUInteger result = libvlc_media_player_get_chapter(_playerInstance);
  537. return result;
  538. }
  539. - (void)nextChapter
  540. {
  541. libvlc_media_player_next_chapter(_playerInstance);
  542. }
  543. - (void)previousChapter
  544. {
  545. libvlc_media_player_previous_chapter(_playerInstance);
  546. }
  547. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  548. {
  549. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  550. if (count <= 0)
  551. return @[];
  552. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(_playerInstance, title);
  553. NSMutableArray *tempArray = [NSMutableArray array];
  554. for (NSInteger i = 0; i < count ; i++) {
  555. [tempArray addObject:@(tracks->psz_name)];
  556. tracks = tracks->p_next;
  557. }
  558. libvlc_track_description_list_release(tracks);
  559. return [NSArray arrayWithArray:tempArray];
  560. }
  561. #pragma mark -
  562. #pragma mark Titles
  563. - (void)setCurrentTitleIndex:(NSUInteger)value
  564. {
  565. libvlc_media_player_set_title(_playerInstance, value);
  566. }
  567. - (NSUInteger)currentTitleIndex
  568. {
  569. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  570. if (count <= 0)
  571. return NSNotFound;
  572. return libvlc_media_player_get_title(_playerInstance);
  573. }
  574. - (NSUInteger)countOfTitles
  575. {
  576. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  577. return result;
  578. }
  579. - (NSArray *)titles
  580. {
  581. libvlc_track_description_t *tracks = libvlc_video_get_title_description(_playerInstance);
  582. NSMutableArray *tempArray = [NSMutableArray array];
  583. for (NSInteger i = 0; i < [self countOfTitles] ; i++) {
  584. [tempArray addObject:@(tracks->psz_name)];
  585. tracks = tracks->p_next;
  586. }
  587. libvlc_track_description_list_release(tracks);
  588. return [NSArray arrayWithArray: tempArray];
  589. }
  590. #pragma mark -
  591. #pragma mark Audio tracks
  592. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  593. {
  594. libvlc_audio_set_track(_playerInstance, (int)value);
  595. }
  596. - (NSUInteger)currentAudioTrackIndex
  597. {
  598. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  599. if (count <= 0)
  600. return NSNotFound;
  601. NSUInteger result = libvlc_audio_get_track(_playerInstance);
  602. return result;
  603. }
  604. - (NSArray *)audioTrackNames
  605. {
  606. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  607. if (count <= 0)
  608. return @[];
  609. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  610. NSMutableArray *tempArray = [NSMutableArray array];
  611. while (currentTrack) {
  612. [tempArray addObject:@(currentTrack->psz_name)];
  613. currentTrack = currentTrack->p_next;
  614. }
  615. libvlc_track_description_list_release(currentTrack);
  616. return [NSArray arrayWithArray: tempArray];
  617. }
  618. - (NSArray *)audioTrackIndexes
  619. {
  620. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  621. if (count <= 0)
  622. return @[];
  623. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  624. NSMutableArray *tempArray = [NSMutableArray array];
  625. while (currentTrack) {
  626. [tempArray addObject:@(currentTrack->i_id)];
  627. currentTrack = currentTrack->p_next;
  628. }
  629. libvlc_track_description_list_release(currentTrack);
  630. return [NSArray arrayWithArray: tempArray];
  631. }
  632. - (NSArray *)audioTracks
  633. {
  634. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  635. if (count <= 0)
  636. return @[];
  637. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(_playerInstance);
  638. NSMutableArray *tempArray = [NSMutableArray array];
  639. for (NSUInteger i = 0; i < count ; i++) {
  640. [tempArray addObject:@(tracks->psz_name)];
  641. tracks = tracks->p_next;
  642. }
  643. libvlc_track_description_list_release(tracks);
  644. return [NSArray arrayWithArray: tempArray];
  645. }
  646. - (void)setAudioChannel:(NSInteger)value
  647. {
  648. libvlc_audio_set_channel(_playerInstance, value);
  649. }
  650. - (NSInteger)audioChannel
  651. {
  652. return libvlc_audio_get_channel(_playerInstance);
  653. }
  654. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  655. {
  656. libvlc_audio_set_delay(_playerInstance, index);
  657. }
  658. - (NSInteger)currentAudioPlaybackDelay
  659. {
  660. return libvlc_audio_get_delay(_playerInstance);
  661. }
  662. #pragma mark -
  663. #pragma mark equalizer
  664. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  665. {
  666. _equalizerEnabled = equalizerEnabled;
  667. if (!_equalizerEnabled) {
  668. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  669. if (_equalizerInstance)
  670. libvlc_audio_equalizer_release(_equalizerInstance);
  671. return;
  672. }
  673. if (!_equalizerInstance)
  674. _equalizerInstance = libvlc_audio_equalizer_new();
  675. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  676. }
  677. - (BOOL)equalizerEnabled
  678. {
  679. return _equalizerEnabled;
  680. }
  681. - (NSArray *)equalizerProfiles
  682. {
  683. unsigned count = libvlc_audio_equalizer_get_preset_count();
  684. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  685. for (unsigned x = 0; x < count; x++)
  686. [array addObject:[NSString stringWithUTF8String:libvlc_audio_equalizer_get_preset_name(x)]];
  687. return [NSArray arrayWithArray:array];
  688. }
  689. - (void)resetEqualizerFromProfile:(unsigned)profile
  690. {
  691. BOOL wasactive = NO;
  692. if (_equalizerInstance) {
  693. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  694. libvlc_audio_equalizer_release(_equalizerInstance);
  695. wasactive = YES;
  696. }
  697. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  698. if (wasactive)
  699. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  700. }
  701. - (CGFloat)preAmplification
  702. {
  703. if (!_equalizerInstance)
  704. return 0.;
  705. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  706. }
  707. - (void)setPreAmplification:(CGFloat)preAmplification
  708. {
  709. if (!_equalizerInstance)
  710. _equalizerInstance = libvlc_audio_equalizer_new();
  711. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  712. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  713. }
  714. - (unsigned)numberOfBands
  715. {
  716. return libvlc_audio_equalizer_get_band_count();
  717. }
  718. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  719. {
  720. return libvlc_audio_equalizer_get_band_frequency(index);
  721. }
  722. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  723. {
  724. if (!_equalizerInstance)
  725. _equalizerInstance = libvlc_audio_equalizer_new();
  726. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  727. }
  728. - (CGFloat)amplificationOfBand:(unsigned int)index
  729. {
  730. if (!_equalizerInstance)
  731. return 0.;
  732. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  733. }
  734. #pragma mark -
  735. #pragma mark set/get media
  736. - (void)setMedia:(VLCMedia *)value
  737. {
  738. if (_media != value) {
  739. if (_media && [_media compare:value] == NSOrderedSame)
  740. return;
  741. [_media release];
  742. _media = [value retain];
  743. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  744. }
  745. }
  746. - (VLCMedia *)media
  747. {
  748. return _media;
  749. }
  750. #pragma mark -
  751. #pragma mark playback
  752. - (BOOL)play
  753. {
  754. libvlc_media_player_play(_playerInstance);
  755. return YES;
  756. }
  757. - (void)pause
  758. {
  759. if ([NSThread isMainThread]) {
  760. /* Hack because we create a dead lock here, when the vout is stopped
  761. * and tries to recontact us on the main thread */
  762. /* FIXME: to do this properly we need to do some locking. We may want
  763. * to move that to libvlc */
  764. [self performSelectorInBackground:@selector(pause) withObject:nil];
  765. return;
  766. }
  767. // Pause the stream
  768. libvlc_media_player_pause(_playerInstance);
  769. }
  770. - (void)stop
  771. {
  772. if ([NSThread isMainThread]) {
  773. /* Hack because we create a dead lock here, when the vout is stopped
  774. * and tries to recontact us on the main thread */
  775. /* FIXME: to do this properly we need to do some locking. We may want
  776. * to move that to libvlc */
  777. [self performSelectorInBackground:@selector(stop) withObject:nil];
  778. return;
  779. }
  780. libvlc_media_player_stop(_playerInstance);
  781. }
  782. - (void)gotoNextFrame
  783. {
  784. libvlc_media_player_next_frame(_playerInstance);
  785. }
  786. - (void)fastForward
  787. {
  788. [self fastForwardAtRate: 2.0];
  789. }
  790. - (void)fastForwardAtRate:(float)rate
  791. {
  792. [self setRate:rate];
  793. }
  794. - (void)rewind
  795. {
  796. [self rewindAtRate: 2.0];
  797. }
  798. - (void)rewindAtRate:(float)rate
  799. {
  800. [self setRate: -rate];
  801. }
  802. - (void)jumpBackward:(NSInteger)interval
  803. {
  804. if ([self isSeekable]) {
  805. interval = interval * 1000;
  806. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  807. }
  808. }
  809. - (void)jumpForward:(NSInteger)interval
  810. {
  811. if ([self isSeekable]) {
  812. interval = interval * 1000;
  813. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  814. }
  815. }
  816. - (void)extraShortJumpBackward
  817. {
  818. [self jumpBackward:3];
  819. }
  820. - (void)extraShortJumpForward
  821. {
  822. [self jumpForward:3];
  823. }
  824. - (void)shortJumpBackward
  825. {
  826. [self jumpBackward:10];
  827. }
  828. - (void)shortJumpForward
  829. {
  830. [self jumpForward:10];
  831. }
  832. - (void)mediumJumpBackward
  833. {
  834. [self jumpBackward:60];
  835. }
  836. - (void)mediumJumpForward
  837. {
  838. [self jumpForward:60];
  839. }
  840. - (void)longJumpBackward
  841. {
  842. [self jumpBackward:300];
  843. }
  844. - (void)longJumpForward
  845. {
  846. [self jumpForward:300];
  847. }
  848. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  849. {
  850. return [NSSet setWithObjects:@"state", nil];
  851. }
  852. - (BOOL)isPlaying
  853. {
  854. return libvlc_media_player_is_playing(_playerInstance);
  855. }
  856. - (BOOL)willPlay
  857. {
  858. return libvlc_media_player_will_play(_playerInstance);
  859. }
  860. static const VLCMediaPlayerState libvlc_to_local_state[] =
  861. {
  862. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  863. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  864. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  865. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  866. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  867. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  868. [libvlc_Error] = VLCMediaPlayerStateError
  869. };
  870. - (VLCMediaPlayerState)state
  871. {
  872. return _cachedState;
  873. }
  874. - (float)position
  875. {
  876. return _position;
  877. }
  878. - (void)setPosition:(float)newPosition
  879. {
  880. libvlc_media_player_set_position(_playerInstance, newPosition);
  881. }
  882. - (BOOL)isSeekable
  883. {
  884. return libvlc_media_player_is_seekable(_playerInstance);
  885. }
  886. - (BOOL)canPause
  887. {
  888. return libvlc_media_player_can_pause(_playerInstance);
  889. }
  890. - (void *)libVLCMediaPlayer
  891. {
  892. return _playerInstance;
  893. }
  894. @end
  895. @implementation VLCMediaPlayer (Private)
  896. - (id)initWithDrawable:(id)aDrawable options:(NSArray *)options
  897. {
  898. if (self = [super init]) {
  899. _delegate = nil;
  900. _media = nil;
  901. _cachedTime = [[VLCTime nullTime] retain];
  902. _cachedRemainingTime = [[VLCTime nullTime] retain];
  903. _position = 0.0f;
  904. _cachedState = VLCMediaPlayerStateStopped;
  905. // Create a media instance, it doesn't matter what library we start off with
  906. // it will change depending on the media descriptor provided to the media
  907. // instance
  908. if (options && options.count > 0) {
  909. VKLog(@"creating player instance with private library as options were given");
  910. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  911. } else {
  912. VKLog(@"creating player instance using shared library");
  913. _privateLibrary = [[VLCLibrary sharedLibrary] retain];
  914. }
  915. libvlc_retain([_privateLibrary instance]);
  916. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  917. libvlc_media_player_retain(_playerInstance);
  918. [self registerObservers];
  919. [self setDrawable:aDrawable];
  920. }
  921. return self;
  922. }
  923. - (void)registerObservers
  924. {
  925. // Attach event observers into the media instance
  926. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  927. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  928. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  929. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  930. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  931. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, self);
  932. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  933. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  934. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  935. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  936. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  937. }
  938. - (void)unregisterObservers
  939. {
  940. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  941. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  942. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  943. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  944. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  945. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, self);
  946. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  947. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  948. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  949. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  950. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  951. }
  952. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  953. {
  954. [self willChangeValueForKey:@"time"];
  955. [self willChangeValueForKey:@"remainingTime"];
  956. [_cachedTime release];
  957. _cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  958. [_cachedRemainingTime release];
  959. double currentTime = [[_cachedTime numberValue] doubleValue];
  960. if (currentTime > 0) {
  961. double remaining = currentTime / _position * (1 - _position);
  962. _cachedRemainingTime = [[VLCTime timeWithNumber:@(-remaining)] retain];
  963. } else
  964. _cachedRemainingTime = [[VLCTime nullTime] retain];
  965. [self didChangeValueForKey:@"remainingTime"];
  966. [self didChangeValueForKey:@"time"];
  967. }
  968. #if !TARGET_OS_IPHONE
  969. - (void)delaySleep
  970. {
  971. UpdateSystemActivity(UsrActivity);
  972. }
  973. #endif
  974. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  975. {
  976. #if !TARGET_OS_IPHONE
  977. // This seems to be the most relevant place to delay sleeping and screen saver.
  978. [self delaySleep];
  979. #endif
  980. [self willChangeValueForKey:@"position"];
  981. _position = [newPosition floatValue];
  982. [self didChangeValueForKey:@"position"];
  983. }
  984. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  985. {
  986. [self willChangeValueForKey:@"state"];
  987. _cachedState = [newState intValue];
  988. #if TARGET_OS_IPHONE
  989. // Disable idle timer if player is playing media
  990. // Exclusion can be made for audio only media
  991. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  992. #endif
  993. [self didChangeValueForKey:@"state"];
  994. }
  995. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  996. {
  997. [self willChangeValueForKey:@"media"];
  998. if (_media != newMedia)
  999. {
  1000. [_media release];
  1001. _media = [newMedia retain];
  1002. }
  1003. [self didChangeValueForKey:@"media"];
  1004. }
  1005. @end