VLCMediaPlayer.m 35 KB

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