VLCMediaPlayer.m 32 KB

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