VLCMediaPlayer.m 35 KB

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