VLCMediaPlayer.m 30 KB

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