VLCMediaPlayer.m 35 KB

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