VLCMediaPlayer.m 35 KB

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