VLCMediaPlayer.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. - (void)setTextRendererFontSize:(NSNumber *)fontSize
  375. {
  376. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontsize, [fontSize intValue]);
  377. }
  378. - (void)setTextRendererFont:(NSString *)fontname
  379. {
  380. libvlc_video_set_textrenderer_string(_playerInstance, libvlc_textrender_font, [fontname UTF8String]);
  381. }
  382. - (void)setTextRendererFontColor:(NSNumber *)fontColor
  383. {
  384. libvlc_video_set_textrenderer_int(_playerInstance, libvlc_textrender_fontcolor, [fontColor intValue]);
  385. }
  386. #pragma mark -
  387. #pragma mark Video Crop geometry
  388. - (void)setVideoCropGeometry:(char *)value
  389. {
  390. libvlc_video_set_crop_geometry(_playerInstance, value);
  391. }
  392. - (char *)videoCropGeometry
  393. {
  394. char * result = libvlc_video_get_crop_geometry(_playerInstance);
  395. return result;
  396. }
  397. - (void)setVideoAspectRatio:(char *)value
  398. {
  399. libvlc_video_set_aspect_ratio(_playerInstance, value);
  400. }
  401. - (char *)videoAspectRatio
  402. {
  403. char * result = libvlc_video_get_aspect_ratio(_playerInstance);
  404. return result;
  405. }
  406. - (void)setScaleFactor:(float)value
  407. {
  408. libvlc_video_set_scale(_playerInstance, value);
  409. }
  410. - (float)scaleFactor
  411. {
  412. return libvlc_video_get_scale(_playerInstance);
  413. }
  414. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  415. {
  416. int failure = libvlc_video_take_snapshot(_playerInstance, 0, [path UTF8String], width, height);
  417. if (failure)
  418. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  419. }
  420. - (void)setDeinterlaceFilter:(NSString *)name
  421. {
  422. if (!name || name.length < 1)
  423. libvlc_video_set_deinterlace(_playerInstance, NULL);
  424. else
  425. libvlc_video_set_deinterlace(_playerInstance, [name UTF8String]);
  426. }
  427. - (BOOL)adjustFilterEnabled
  428. {
  429. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Enable);
  430. }
  431. - (void)setAdjustFilterEnabled:(BOOL)b_value
  432. {
  433. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, b_value);
  434. }
  435. - (float)contrast
  436. {
  437. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  438. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Contrast);
  439. }
  440. - (void)setContrast:(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_Contrast, f_value);
  445. }
  446. }
  447. - (float)brightness
  448. {
  449. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  450. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Brightness);
  451. }
  452. - (void)setBrightness:(float)f_value
  453. {
  454. if (f_value <= 2. && f_value >= 0.) {
  455. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  456. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Brightness, f_value);
  457. }
  458. }
  459. - (NSInteger)hue
  460. {
  461. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  462. return libvlc_video_get_adjust_int(_playerInstance, libvlc_adjust_Hue);
  463. }
  464. - (void)setHue:(NSInteger)i_value
  465. {
  466. if (i_value <= 360 && i_value >= 0) {
  467. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  468. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Hue, i_value);
  469. }
  470. }
  471. - (float)saturation
  472. {
  473. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  474. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Saturation);
  475. }
  476. - (void)setSaturation:(float)f_value
  477. {
  478. if (f_value <= 3. && f_value >= 0.) {
  479. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  480. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Saturation, f_value);
  481. }
  482. }
  483. - (float)gamma
  484. {
  485. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  486. return libvlc_video_get_adjust_float(_playerInstance, libvlc_adjust_Gamma);
  487. }
  488. - (void)setGamma:(float)f_value
  489. {
  490. if (f_value <= 10. && f_value >= 0.) {
  491. libvlc_video_set_adjust_int(_playerInstance, libvlc_adjust_Enable, 1);
  492. libvlc_video_set_adjust_float(_playerInstance, libvlc_adjust_Gamma, f_value);
  493. }
  494. }
  495. - (void)setRate:(float)value
  496. {
  497. libvlc_media_player_set_rate(_playerInstance, value);
  498. }
  499. - (float)rate
  500. {
  501. return libvlc_media_player_get_rate(_playerInstance);
  502. }
  503. - (CGSize)videoSize
  504. {
  505. unsigned height = 0, width = 0;
  506. int failure = libvlc_video_get_size(_playerInstance, 0, &width, &height);
  507. if (failure)
  508. [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
  509. return CGSizeMake(width, height);
  510. }
  511. - (BOOL)hasVideoOut
  512. {
  513. return libvlc_media_player_has_vout(_playerInstance);
  514. }
  515. - (float)framesPerSecond
  516. {
  517. return libvlc_media_player_get_fps(_playerInstance);
  518. }
  519. - (void)setTime:(VLCTime *)value
  520. {
  521. // Time is managed in seconds, while duration is managed in microseconds
  522. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  523. libvlc_media_player_set_time(_playerInstance, value ? [[value numberValue] longLongValue] : 0);
  524. }
  525. - (VLCTime *)time
  526. {
  527. return _cachedTime;
  528. }
  529. - (VLCTime *)remainingTime
  530. {
  531. return _cachedRemainingTime;
  532. }
  533. - (NSUInteger)fps
  534. {
  535. return libvlc_media_player_get_fps(_playerInstance);
  536. }
  537. #pragma mark -
  538. #pragma mark Chapters
  539. - (void)setCurrentChapterIndex:(NSUInteger)value;
  540. {
  541. libvlc_media_player_set_chapter(_playerInstance, value);
  542. }
  543. - (NSUInteger)currentChapterIndex
  544. {
  545. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  546. if (count <= 0)
  547. return NSNotFound;
  548. NSUInteger result = libvlc_media_player_get_chapter(_playerInstance);
  549. return result;
  550. }
  551. - (void)nextChapter
  552. {
  553. libvlc_media_player_next_chapter(_playerInstance);
  554. }
  555. - (void)previousChapter
  556. {
  557. libvlc_media_player_previous_chapter(_playerInstance);
  558. }
  559. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  560. {
  561. NSInteger count = libvlc_media_player_get_chapter_count(_playerInstance);
  562. if (count <= 0)
  563. return @[];
  564. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(_playerInstance, title);
  565. NSMutableArray *tempArray = [NSMutableArray array];
  566. for (NSInteger i = 0; i < count ; i++) {
  567. [tempArray addObject:@(tracks->psz_name)];
  568. tracks = tracks->p_next;
  569. }
  570. libvlc_track_description_list_release(tracks);
  571. return [NSArray arrayWithArray:tempArray];
  572. }
  573. #pragma mark -
  574. #pragma mark Titles
  575. - (void)setCurrentTitleIndex:(NSUInteger)value
  576. {
  577. libvlc_media_player_set_title(_playerInstance, value);
  578. }
  579. - (NSUInteger)currentTitleIndex
  580. {
  581. NSInteger count = libvlc_media_player_get_title_count(_playerInstance);
  582. if (count <= 0)
  583. return NSNotFound;
  584. return libvlc_media_player_get_title(_playerInstance);
  585. }
  586. - (NSUInteger)countOfTitles
  587. {
  588. NSUInteger result = libvlc_media_player_get_title_count(_playerInstance);
  589. return result;
  590. }
  591. - (NSArray *)titles
  592. {
  593. libvlc_track_description_t *tracks = libvlc_video_get_title_description(_playerInstance);
  594. NSMutableArray *tempArray = [NSMutableArray array];
  595. for (NSInteger i = 0; i < [self countOfTitles] ; i++) {
  596. [tempArray addObject:@(tracks->psz_name)];
  597. tracks = tracks->p_next;
  598. }
  599. libvlc_track_description_list_release(tracks);
  600. return [NSArray arrayWithArray: tempArray];
  601. }
  602. #pragma mark -
  603. #pragma mark Audio tracks
  604. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  605. {
  606. libvlc_audio_set_track(_playerInstance, (int)value);
  607. }
  608. - (NSUInteger)currentAudioTrackIndex
  609. {
  610. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  611. if (count <= 0)
  612. return NSNotFound;
  613. NSUInteger result = libvlc_audio_get_track(_playerInstance);
  614. return result;
  615. }
  616. - (NSArray *)audioTrackNames
  617. {
  618. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  619. if (count <= 0)
  620. return @[];
  621. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  622. NSMutableArray *tempArray = [NSMutableArray array];
  623. while (currentTrack) {
  624. [tempArray addObject:@(currentTrack->psz_name)];
  625. currentTrack = currentTrack->p_next;
  626. }
  627. libvlc_track_description_list_release(currentTrack);
  628. return [NSArray arrayWithArray: tempArray];
  629. }
  630. - (NSArray *)audioTrackIndexes
  631. {
  632. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  633. if (count <= 0)
  634. return @[];
  635. libvlc_track_description_t *currentTrack = libvlc_audio_get_track_description(_playerInstance);
  636. NSMutableArray *tempArray = [NSMutableArray array];
  637. while (currentTrack) {
  638. [tempArray addObject:@(currentTrack->i_id)];
  639. currentTrack = currentTrack->p_next;
  640. }
  641. libvlc_track_description_list_release(currentTrack);
  642. return [NSArray arrayWithArray: tempArray];
  643. }
  644. - (NSArray *)audioTracks
  645. {
  646. NSInteger count = libvlc_audio_get_track_count(_playerInstance);
  647. if (count <= 0)
  648. return @[];
  649. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(_playerInstance);
  650. NSMutableArray *tempArray = [NSMutableArray array];
  651. for (NSUInteger i = 0; i < count ; i++) {
  652. [tempArray addObject:@(tracks->psz_name)];
  653. tracks = tracks->p_next;
  654. }
  655. libvlc_track_description_list_release(tracks);
  656. return [NSArray arrayWithArray: tempArray];
  657. }
  658. - (void)setAudioChannel:(NSInteger)value
  659. {
  660. libvlc_audio_set_channel(_playerInstance, value);
  661. }
  662. - (NSInteger)audioChannel
  663. {
  664. return libvlc_audio_get_channel(_playerInstance);
  665. }
  666. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  667. {
  668. libvlc_audio_set_delay(_playerInstance, index);
  669. }
  670. - (NSInteger)currentAudioPlaybackDelay
  671. {
  672. return libvlc_audio_get_delay(_playerInstance);
  673. }
  674. #pragma mark -
  675. #pragma mark equalizer
  676. - (void)setEqualizerEnabled:(BOOL)equalizerEnabled
  677. {
  678. _equalizerEnabled = equalizerEnabled;
  679. if (!_equalizerEnabled) {
  680. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  681. if (_equalizerInstance)
  682. libvlc_audio_equalizer_release(_equalizerInstance);
  683. return;
  684. }
  685. if (!_equalizerInstance)
  686. _equalizerInstance = libvlc_audio_equalizer_new();
  687. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  688. }
  689. - (BOOL)equalizerEnabled
  690. {
  691. return _equalizerEnabled;
  692. }
  693. - (NSArray *)equalizerProfiles
  694. {
  695. unsigned count = libvlc_audio_equalizer_get_preset_count();
  696. NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:count];
  697. for (unsigned x = 0; x < count; x++)
  698. [array addObject:[NSString stringWithUTF8String:libvlc_audio_equalizer_get_preset_name(x)]];
  699. return [NSArray arrayWithArray:array];
  700. }
  701. - (void)resetEqualizerFromProfile:(unsigned)profile
  702. {
  703. BOOL wasactive = NO;
  704. if (_equalizerInstance) {
  705. libvlc_media_player_set_equalizer(_playerInstance, NULL);
  706. libvlc_audio_equalizer_release(_equalizerInstance);
  707. wasactive = YES;
  708. }
  709. _equalizerInstance = libvlc_audio_equalizer_new_from_preset(profile);
  710. if (wasactive)
  711. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  712. }
  713. - (CGFloat)preAmplification
  714. {
  715. if (!_equalizerInstance)
  716. return 0.;
  717. return libvlc_audio_equalizer_get_preamp(_equalizerInstance);
  718. }
  719. - (void)setPreAmplification:(CGFloat)preAmplification
  720. {
  721. if (!_equalizerInstance)
  722. _equalizerInstance = libvlc_audio_equalizer_new();
  723. libvlc_audio_equalizer_set_preamp(_equalizerInstance, preAmplification);
  724. libvlc_media_player_set_equalizer(_playerInstance, _equalizerInstance);
  725. }
  726. - (unsigned)numberOfBands
  727. {
  728. return libvlc_audio_equalizer_get_band_count();
  729. }
  730. - (CGFloat)frequencyOfBandAtIndex:(unsigned int)index
  731. {
  732. return libvlc_audio_equalizer_get_band_frequency(index);
  733. }
  734. - (void)setAmplification:(CGFloat)amplification forBand:(unsigned int)index
  735. {
  736. if (!_equalizerInstance)
  737. _equalizerInstance = libvlc_audio_equalizer_new();
  738. libvlc_audio_equalizer_set_amp_at_index(_equalizerInstance, amplification, index);
  739. }
  740. - (CGFloat)amplificationOfBand:(unsigned int)index
  741. {
  742. if (!_equalizerInstance)
  743. return 0.;
  744. return libvlc_audio_equalizer_get_amp_at_index(_equalizerInstance, index);
  745. }
  746. #pragma mark -
  747. #pragma mark set/get media
  748. - (void)setMedia:(VLCMedia *)value
  749. {
  750. if (_media != value) {
  751. if (_media && [_media compare:value] == NSOrderedSame)
  752. return;
  753. [_media release];
  754. _media = [value retain];
  755. libvlc_media_player_set_media(_playerInstance, [_media libVLCMediaDescriptor]);
  756. }
  757. }
  758. - (VLCMedia *)media
  759. {
  760. return _media;
  761. }
  762. #pragma mark -
  763. #pragma mark playback
  764. - (BOOL)play
  765. {
  766. libvlc_media_player_play(_playerInstance);
  767. return YES;
  768. }
  769. - (void)pause
  770. {
  771. if ([NSThread isMainThread]) {
  772. /* Hack because we create a dead lock here, when the vout is stopped
  773. * and tries to recontact us on the main thread */
  774. /* FIXME: to do this properly we need to do some locking. We may want
  775. * to move that to libvlc */
  776. [self performSelectorInBackground:@selector(pause) withObject:nil];
  777. return;
  778. }
  779. // Pause the stream
  780. libvlc_media_player_pause(_playerInstance);
  781. }
  782. - (void)stop
  783. {
  784. if ([NSThread isMainThread]) {
  785. /* Hack because we create a dead lock here, when the vout is stopped
  786. * and tries to recontact us on the main thread */
  787. /* FIXME: to do this properly we need to do some locking. We may want
  788. * to move that to libvlc */
  789. [self performSelectorInBackground:@selector(stop) withObject:nil];
  790. return;
  791. }
  792. libvlc_media_player_stop(_playerInstance);
  793. }
  794. - (void)gotoNextFrame
  795. {
  796. libvlc_media_player_next_frame(_playerInstance);
  797. }
  798. - (void)fastForward
  799. {
  800. [self fastForwardAtRate: 2.0];
  801. }
  802. - (void)fastForwardAtRate:(float)rate
  803. {
  804. [self setRate:rate];
  805. }
  806. - (void)rewind
  807. {
  808. [self rewindAtRate: 2.0];
  809. }
  810. - (void)rewindAtRate:(float)rate
  811. {
  812. [self setRate: -rate];
  813. }
  814. - (void)jumpBackward:(NSInteger)interval
  815. {
  816. if ([self isSeekable]) {
  817. interval = interval * 1000;
  818. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  819. }
  820. }
  821. - (void)jumpForward:(NSInteger)interval
  822. {
  823. if ([self isSeekable]) {
  824. interval = interval * 1000;
  825. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  826. }
  827. }
  828. - (void)extraShortJumpBackward
  829. {
  830. [self jumpBackward:3];
  831. }
  832. - (void)extraShortJumpForward
  833. {
  834. [self jumpForward:3];
  835. }
  836. - (void)shortJumpBackward
  837. {
  838. [self jumpBackward:10];
  839. }
  840. - (void)shortJumpForward
  841. {
  842. [self jumpForward:10];
  843. }
  844. - (void)mediumJumpBackward
  845. {
  846. [self jumpBackward:60];
  847. }
  848. - (void)mediumJumpForward
  849. {
  850. [self jumpForward:60];
  851. }
  852. - (void)longJumpBackward
  853. {
  854. [self jumpBackward:300];
  855. }
  856. - (void)longJumpForward
  857. {
  858. [self jumpForward:300];
  859. }
  860. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  861. {
  862. return [NSSet setWithObjects:@"state", nil];
  863. }
  864. - (BOOL)isPlaying
  865. {
  866. return libvlc_media_player_is_playing(_playerInstance);
  867. }
  868. - (BOOL)willPlay
  869. {
  870. return libvlc_media_player_will_play(_playerInstance);
  871. }
  872. static const VLCMediaPlayerState libvlc_to_local_state[] =
  873. {
  874. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  875. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  876. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  877. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  878. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  879. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  880. [libvlc_Error] = VLCMediaPlayerStateError
  881. };
  882. - (VLCMediaPlayerState)state
  883. {
  884. return _cachedState;
  885. }
  886. - (float)position
  887. {
  888. return _position;
  889. }
  890. - (void)setPosition:(float)newPosition
  891. {
  892. libvlc_media_player_set_position(_playerInstance, newPosition);
  893. }
  894. - (BOOL)isSeekable
  895. {
  896. return libvlc_media_player_is_seekable(_playerInstance);
  897. }
  898. - (BOOL)canPause
  899. {
  900. return libvlc_media_player_can_pause(_playerInstance);
  901. }
  902. - (void *)libVLCMediaPlayer
  903. {
  904. return _playerInstance;
  905. }
  906. @end
  907. @implementation VLCMediaPlayer (Private)
  908. - (id)initWithDrawable:(id)aDrawable options:(NSArray *)options
  909. {
  910. if (self = [super init]) {
  911. _delegate = nil;
  912. _media = nil;
  913. _cachedTime = [[VLCTime nullTime] retain];
  914. _cachedRemainingTime = [[VLCTime nullTime] retain];
  915. _position = 0.0f;
  916. _cachedState = VLCMediaPlayerStateStopped;
  917. // Create a media instance, it doesn't matter what library we start off with
  918. // it will change depending on the media descriptor provided to the media
  919. // instance
  920. if (options && options.count > 0) {
  921. VKLog(@"creating player instance with private library as options were given");
  922. _privateLibrary = [[VLCLibrary alloc] initWithOptions:options];
  923. } else {
  924. VKLog(@"creating player instance using shared library");
  925. _privateLibrary = [[VLCLibrary sharedLibrary] retain];
  926. }
  927. libvlc_retain([_privateLibrary instance]);
  928. _playerInstance = libvlc_media_player_new([_privateLibrary instance]);
  929. libvlc_media_player_retain(_playerInstance);
  930. [self registerObservers];
  931. [self setDrawable:aDrawable];
  932. }
  933. return self;
  934. }
  935. - (void)registerObservers
  936. {
  937. // Attach event observers into the media instance
  938. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  939. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  940. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  941. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  942. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  943. libvlc_event_attach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, self);
  944. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  945. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  946. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  947. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  948. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  949. }
  950. - (void)unregisterObservers
  951. {
  952. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(_playerInstance);
  953. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  954. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  955. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  956. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  957. libvlc_event_detach(p_em, libvlc_MediaPlayerStopped, HandleMediaInstanceStateChanged, self);
  958. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  959. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  960. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  961. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  962. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  963. }
  964. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  965. {
  966. [self willChangeValueForKey:@"time"];
  967. [self willChangeValueForKey:@"remainingTime"];
  968. [_cachedTime release];
  969. _cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  970. [_cachedRemainingTime release];
  971. double currentTime = [[_cachedTime numberValue] doubleValue];
  972. if (currentTime > 0) {
  973. double remaining = currentTime / _position * (1 - _position);
  974. _cachedRemainingTime = [[VLCTime timeWithNumber:@(-remaining)] retain];
  975. } else
  976. _cachedRemainingTime = [[VLCTime nullTime] retain];
  977. [self didChangeValueForKey:@"remainingTime"];
  978. [self didChangeValueForKey:@"time"];
  979. }
  980. #if !TARGET_OS_IPHONE
  981. - (void)delaySleep
  982. {
  983. UpdateSystemActivity(UsrActivity);
  984. }
  985. #endif
  986. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  987. {
  988. #if !TARGET_OS_IPHONE
  989. // This seems to be the most relevant place to delay sleeping and screen saver.
  990. [self delaySleep];
  991. #endif
  992. [self willChangeValueForKey:@"position"];
  993. _position = [newPosition floatValue];
  994. [self didChangeValueForKey:@"position"];
  995. }
  996. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  997. {
  998. [self willChangeValueForKey:@"state"];
  999. _cachedState = [newState intValue];
  1000. #if TARGET_OS_IPHONE
  1001. // Disable idle timer if player is playing media
  1002. // Exclusion can be made for audio only media
  1003. [UIApplication sharedApplication].idleTimerDisabled = [self isPlaying];
  1004. #endif
  1005. [self didChangeValueForKey:@"state"];
  1006. }
  1007. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  1008. {
  1009. [self willChangeValueForKey:@"media"];
  1010. if (_media != newMedia)
  1011. {
  1012. [_media release];
  1013. _media = [newMedia retain];
  1014. }
  1015. [self didChangeValueForKey:@"media"];
  1016. }
  1017. @end