VLCMediaPlayer.m 32 KB

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