VLCMediaPlayer.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2009 the VideoLAN team
  6. * Partial Copyright (C) 2009 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
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 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 General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, 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. #import "VLCVideoView.h"
  32. #ifdef HAVE_CONFIG_H
  33. # include "config.h"
  34. #endif
  35. /* prevent system sleep */
  36. #import <CoreServices/CoreServices.h>
  37. /* FIXME: Ugly hack! */
  38. #ifdef __x86_64__
  39. #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  40. #endif
  41. #include <vlc/vlc.h>
  42. /* Notification Messages */
  43. NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  44. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  45. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  46. {
  47. static NSString * stateToStrings[] = {
  48. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  49. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  50. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  51. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  52. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  53. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  54. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  55. };
  56. return stateToStrings[state];
  57. }
  58. static inline libvlc_track_description_t *freeAndGetNextTrack(libvlc_track_description_t *track)
  59. {
  60. libvlc_track_description_t *next = track->p_next;
  61. libvlc_track_description_release(track);
  62. return next;
  63. }
  64. /* libvlc event callback */
  65. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  66. {
  67. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  68. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  69. withNotificationName:VLCMediaPlayerVolumeChanged];
  70. }
  71. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  72. {
  73. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  74. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  75. withMethod:@selector(mediaPlayerTimeChanged:)
  76. withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
  77. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  78. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  79. withNotificationName:VLCMediaPlayerTimeChanged];
  80. [pool release];
  81. }
  82. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  83. {
  84. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  85. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  86. withMethod:@selector(mediaPlayerPositionChanged:)
  87. withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
  88. [pool release];
  89. }
  90. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  91. {
  92. VLCMediaPlayerState newState;
  93. if( event->type == libvlc_MediaPlayerPlaying )
  94. newState = VLCMediaPlayerStatePlaying;
  95. else if( event->type == libvlc_MediaPlayerPaused )
  96. newState = VLCMediaPlayerStatePaused;
  97. else if( event->type == libvlc_MediaPlayerEndReached )
  98. newState = VLCMediaPlayerStateStopped;
  99. else if( event->type == libvlc_MediaPlayerEncounteredError )
  100. newState = VLCMediaPlayerStateError;
  101. else
  102. {
  103. NSLog(@"%s: Unknown event", __FUNCTION__);
  104. return;
  105. }
  106. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  107. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  108. withMethod:@selector(mediaPlayerStateChanged:)
  109. withArgumentAsObject:[NSNumber numberWithInt:newState]];
  110. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  111. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  112. withNotificationName:VLCMediaPlayerStateChanged];
  113. [pool release];
  114. }
  115. static void HandleMediaPlayerMediaChanged(const libvlc_event_t * event, void * self)
  116. {
  117. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  118. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  119. withMethod:@selector(mediaPlayerMediaChanged:)
  120. withArgumentAsObject:[VLCMedia mediaWithLibVLCMediaDescriptor:event->u.media_player_media_changed.new_media]];
  121. [pool release];
  122. }
  123. // TODO: Documentation
  124. @interface VLCMediaPlayer (Private)
  125. - (id)initWithDrawable:(id)aDrawable;
  126. - (void)registerObservers;
  127. - (void)unregisterObservers;
  128. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  129. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  130. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  131. - (void)mediaPlayerMediaChanged:(VLCMedia *)media;
  132. @end
  133. @implementation VLCMediaPlayer
  134. /* Bindings */
  135. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  136. {
  137. static NSDictionary * dict = nil;
  138. NSSet * superKeyPaths;
  139. if( !dict )
  140. {
  141. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  142. [NSSet setWithObject:@"state"], @"playing",
  143. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  144. [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
  145. [NSSet setWithObjects:@"state", @"media", nil], @"description",
  146. nil] retain];
  147. }
  148. if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
  149. {
  150. NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
  151. [ret unionSet:superKeyPaths];
  152. return ret;
  153. }
  154. return [dict objectForKey: key];
  155. }
  156. /* Contructor */
  157. - (id)init
  158. {
  159. return [self initWithDrawable:nil];
  160. }
  161. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  162. {
  163. return [self initWithDrawable: aVideoView];
  164. }
  165. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  166. {
  167. return [self initWithDrawable: aVideoLayer];
  168. }
  169. - (void)release
  170. {
  171. @synchronized(self)
  172. {
  173. if([self retainCount] <= 1)
  174. {
  175. /* We must make sure we won't receive new event after an upcoming dealloc
  176. * We also may receive a -retain in some event callback that may occcur
  177. * Before libvlc_event_detach. So this can't happen in dealloc */
  178. [self unregisterObservers];
  179. }
  180. [super release];
  181. }
  182. }
  183. - (void)dealloc
  184. {
  185. NSAssert(libvlc_media_player_get_state(instance) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
  186. // Always get rid of the delegate first so we can stop sending messages to it
  187. // TODO: Should we tell the delegate that we're shutting down?
  188. delegate = nil;
  189. // Clear our drawable as we are going to release it, we don't
  190. // want the core to use it from this point. This won't happen as
  191. // the media player must be stopped.
  192. libvlc_media_player_set_nsobject(instance, nil);
  193. libvlc_media_player_release(instance);
  194. // Get rid of everything else
  195. [media release];
  196. [cachedTime release];
  197. [cachedRemainingTime release];
  198. [drawable release];
  199. [super dealloc];
  200. }
  201. - (void)setDelegate:(id)value
  202. {
  203. delegate = value;
  204. }
  205. - (id)delegate
  206. {
  207. return delegate;
  208. }
  209. - (void)setVideoView:(VLCVideoView *)aVideoView
  210. {
  211. [self setDrawable: aVideoView];
  212. }
  213. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  214. {
  215. [self setDrawable: aVideoLayer];
  216. }
  217. - (void)setDrawable:(id)aDrawable
  218. {
  219. // Make sure that this instance has been associated with the drawing canvas.
  220. libvlc_media_player_set_nsobject(instance, aDrawable);
  221. }
  222. - (id)drawable
  223. {
  224. libvlc_exception_t ex;
  225. libvlc_exception_init( &ex );
  226. id ret = libvlc_media_player_get_nsobject(instance);
  227. catch_exception( &ex );
  228. return ret;
  229. }
  230. - (VLCAudio *)audio
  231. {
  232. return [[VLCLibrary sharedLibrary] audio];
  233. }
  234. #pragma mark -
  235. #pragma mark Subtitles
  236. - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
  237. {
  238. libvlc_exception_t ex;
  239. libvlc_exception_init( &ex );
  240. libvlc_video_set_spu( instance, (int)index, &ex );
  241. catch_exception( &ex );
  242. }
  243. - (NSUInteger)currentVideoSubTitleIndex
  244. {
  245. libvlc_exception_t ex;
  246. libvlc_exception_init( &ex );
  247. NSInteger count = libvlc_video_get_spu_count( instance, &ex );
  248. if (libvlc_exception_raised( &ex ))
  249. {
  250. libvlc_exception_clear( &ex );
  251. return NSNotFound;
  252. }
  253. if (count <= 0)
  254. return NSNotFound;
  255. NSUInteger result = libvlc_video_get_spu( instance, &ex );
  256. if (libvlc_exception_raised( &ex ))
  257. {
  258. libvlc_exception_clear( &ex );
  259. return NSNotFound;
  260. }
  261. else
  262. return result;
  263. }
  264. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  265. {
  266. libvlc_exception_t ex;
  267. libvlc_exception_init( &ex );
  268. BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
  269. catch_exception( &ex );
  270. return result;
  271. }
  272. - (NSArray *)videoSubTitles
  273. {
  274. libvlc_exception_t ex;
  275. libvlc_exception_init( &ex );
  276. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description( instance, &ex );
  277. catch_exception( &ex );
  278. NSMutableArray *tempArray = [NSMutableArray array];
  279. while (currentTrack) {
  280. [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
  281. currentTrack = freeAndGetNextTrack(currentTrack);
  282. }
  283. return [NSArray arrayWithArray: tempArray];
  284. }
  285. #pragma mark -
  286. #pragma mark Video Crop geometry
  287. - (void)setVideoCropGeometry:(char *)value
  288. {
  289. libvlc_exception_t ex;
  290. libvlc_exception_init( &ex );
  291. libvlc_video_set_crop_geometry( instance, value, &ex );
  292. catch_exception( &ex );
  293. }
  294. - (char *)videoCropGeometry
  295. {
  296. libvlc_exception_t ex;
  297. libvlc_exception_init( &ex );
  298. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  299. catch_exception( &ex );
  300. return result;
  301. }
  302. - (void)setVideoAspectRatio:(char *)value
  303. {
  304. libvlc_exception_t ex;
  305. libvlc_exception_init( &ex );
  306. libvlc_video_set_aspect_ratio( instance, value, &ex );
  307. catch_exception( &ex );
  308. }
  309. - (char *)videoAspectRatio
  310. {
  311. libvlc_exception_t ex;
  312. libvlc_exception_init( &ex );
  313. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  314. catch_exception( &ex );
  315. return result;
  316. }
  317. - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  318. {
  319. libvlc_exception_t ex;
  320. libvlc_exception_init( &ex );
  321. libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
  322. catch_exception( &ex );
  323. }
  324. - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
  325. {
  326. libvlc_exception_t ex;
  327. libvlc_exception_init( &ex );
  328. libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
  329. catch_exception( &ex );
  330. }
  331. - (void)setRate:(float)value
  332. {
  333. libvlc_exception_t ex;
  334. libvlc_exception_init( &ex );
  335. libvlc_media_player_set_rate( instance, value, &ex );
  336. catch_exception( &ex );
  337. }
  338. - (float)rate
  339. {
  340. libvlc_exception_t ex;
  341. libvlc_exception_init( &ex );
  342. float result = libvlc_media_player_get_rate( instance, &ex );
  343. if (libvlc_exception_raised(&ex))
  344. {
  345. result = 1;
  346. libvlc_exception_clear(&ex);
  347. }
  348. return result;
  349. }
  350. - (NSSize)videoSize
  351. {
  352. libvlc_exception_t ex;
  353. libvlc_exception_init( &ex );
  354. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  355. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  356. catch_exception( &ex );
  357. return result;
  358. }
  359. - (BOOL)hasVideoOut
  360. {
  361. libvlc_exception_t ex;
  362. libvlc_exception_init( &ex );
  363. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  364. if (libvlc_exception_raised( &ex ))
  365. {
  366. libvlc_exception_clear( &ex );
  367. return NO;
  368. }
  369. else
  370. return result;
  371. }
  372. - (float)framesPerSecond
  373. {
  374. libvlc_exception_t ex;
  375. libvlc_exception_init( &ex );
  376. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  377. catch_exception( &ex );
  378. return result;
  379. }
  380. - (void)setTime:(VLCTime *)value
  381. {
  382. libvlc_exception_t ex;
  383. libvlc_exception_init( &ex );
  384. // Time is managed in seconds, while duration is managed in microseconds
  385. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  386. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  387. (value ? [[value numberValue] longLongValue] : 0),
  388. &ex );
  389. catch_exception( &ex );
  390. }
  391. - (VLCTime *)time
  392. {
  393. return cachedTime;
  394. }
  395. - (VLCTime *)remainingTime
  396. {
  397. return cachedRemainingTime;
  398. }
  399. - (NSUInteger)fps
  400. {
  401. libvlc_exception_t ex;
  402. libvlc_exception_init( &ex );
  403. NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
  404. catch_exception( &ex );
  405. return result;
  406. }
  407. #pragma mark -
  408. #pragma mark Chapters
  409. - (void)setCurrentChapterIndex:(NSUInteger)value;
  410. {
  411. libvlc_exception_t ex;
  412. libvlc_exception_init( &ex );
  413. libvlc_media_player_set_chapter( instance, value, &ex );
  414. catch_exception( &ex );
  415. }
  416. - (NSUInteger)currentChapterIndex
  417. {
  418. libvlc_exception_t ex;
  419. libvlc_exception_init( &ex );
  420. NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
  421. catch_exception( &ex );
  422. if (count <= 0)
  423. return NSNotFound;
  424. NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
  425. catch_exception( &ex );
  426. return result;
  427. }
  428. - (void)nextChapter
  429. {
  430. libvlc_exception_t ex;
  431. libvlc_exception_init( &ex );
  432. libvlc_media_player_next_chapter( instance, &ex );
  433. catch_exception( &ex );
  434. }
  435. - (void)previousChapter
  436. {
  437. libvlc_exception_t ex;
  438. libvlc_exception_init( &ex );
  439. libvlc_media_player_previous_chapter( instance, &ex );
  440. catch_exception( &ex );
  441. }
  442. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  443. {
  444. libvlc_exception_t ex;
  445. libvlc_exception_init( &ex );
  446. NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
  447. if (count <= 0)
  448. return [NSArray array];
  449. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
  450. NSMutableArray *tempArray = [NSMutableArray array];
  451. NSInteger i;
  452. for (i = 0; i < count ; i++)
  453. {
  454. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  455. tracks = freeAndGetNextTrack(tracks);
  456. }
  457. return [NSArray arrayWithArray: tempArray];
  458. }
  459. #pragma mark -
  460. #pragma mark Titles
  461. - (void)setCurrentTitleIndex:(NSUInteger)value
  462. {
  463. libvlc_exception_t ex;
  464. libvlc_exception_init( &ex );
  465. libvlc_media_player_set_title( instance, value, &ex );
  466. catch_exception( &ex );
  467. }
  468. - (NSUInteger)currentTitleIndex
  469. {
  470. libvlc_exception_t ex;
  471. libvlc_exception_init( &ex );
  472. NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
  473. catch_exception( &ex );
  474. if (count <= 0)
  475. return NSNotFound;
  476. NSUInteger result = libvlc_media_player_get_title( instance, &ex );
  477. catch_exception( &ex );
  478. return result;
  479. }
  480. - (NSUInteger)countOfTitles
  481. {
  482. libvlc_exception_t ex;
  483. libvlc_exception_init( &ex );
  484. NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
  485. catch_exception( &ex );
  486. return result;
  487. }
  488. - (NSArray *)titles
  489. {
  490. libvlc_exception_t ex;
  491. libvlc_exception_init( &ex );
  492. libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
  493. NSMutableArray *tempArray = [NSMutableArray array];
  494. NSInteger i;
  495. for (i = 0; i < [self countOfTitles] ; i++)
  496. {
  497. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  498. tracks = freeAndGetNextTrack(tracks);
  499. }
  500. return [NSArray arrayWithArray: tempArray];
  501. }
  502. #pragma mark -
  503. #pragma mark Audio tracks
  504. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  505. {
  506. libvlc_exception_t ex;
  507. libvlc_exception_init( &ex );
  508. libvlc_audio_set_track( instance, (int)value, &ex );
  509. catch_exception( &ex );
  510. }
  511. - (NSUInteger)currentAudioTrackIndex
  512. {
  513. libvlc_exception_t ex;
  514. libvlc_exception_init( &ex );
  515. NSInteger count = libvlc_audio_get_track_count( instance, &ex );
  516. catch_exception( &ex );
  517. if (count <= 0)
  518. return NSNotFound;
  519. NSUInteger result = libvlc_audio_get_track( instance, &ex );
  520. catch_exception( &ex );
  521. return result;
  522. }
  523. - (NSArray *)audioTracks
  524. {
  525. libvlc_exception_t ex;
  526. libvlc_exception_init( &ex );
  527. NSInteger count = libvlc_audio_get_track_count( instance, &ex );
  528. catch_exception( &ex );
  529. if (count <= 0)
  530. return [NSArray array];
  531. libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
  532. NSMutableArray *tempArray = [NSMutableArray array];
  533. NSUInteger i;
  534. for (i = 0; i < count ; i++)
  535. {
  536. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  537. tracks = freeAndGetNextTrack(tracks);
  538. }
  539. return [NSArray arrayWithArray: tempArray];
  540. }
  541. - (void)setAudioChannel:(NSInteger)value
  542. {
  543. libvlc_exception_t ex;
  544. libvlc_exception_init( &ex );
  545. libvlc_audio_set_channel( instance, value, &ex );
  546. catch_exception( &ex );
  547. }
  548. - (NSInteger)audioChannel
  549. {
  550. libvlc_exception_t ex;
  551. libvlc_exception_init( &ex );
  552. NSInteger result = libvlc_audio_get_channel( instance, &ex );
  553. catch_exception( &ex );
  554. return result;
  555. }
  556. - (void)setMedia:(VLCMedia *)value
  557. {
  558. if (media != value)
  559. {
  560. if (media && [media compare:value] == NSOrderedSame)
  561. return;
  562. [media release];
  563. media = [value retain];
  564. libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
  565. }
  566. }
  567. - (VLCMedia *)media
  568. {
  569. return media;
  570. }
  571. - (BOOL)play
  572. {
  573. libvlc_exception_t ex;
  574. libvlc_exception_init( &ex );
  575. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  576. catch_exception( &ex );
  577. return YES;
  578. }
  579. - (void)pause
  580. {
  581. if( [NSThread isMainThread] )
  582. {
  583. /* Hack because we create a dead lock here, when the vout is stopped
  584. * and tries to recontact us on the main thread */
  585. /* FIXME: to do this properly we need to do some locking. We may want
  586. * to move that to libvlc */
  587. [self performSelectorInBackground:@selector(pause) withObject:nil];
  588. return;
  589. }
  590. // Pause the stream
  591. libvlc_exception_t ex;
  592. libvlc_exception_init(&ex);
  593. libvlc_media_player_pause(instance, &ex);
  594. // fail gracefully
  595. // in most cases, it's just EOF so let's stop
  596. if (libvlc_exception_raised(&ex))
  597. [self stop];
  598. libvlc_exception_clear(&ex);
  599. }
  600. - (void)stop
  601. {
  602. libvlc_media_player_stop(instance);
  603. }
  604. - (void)gotoNextFrame
  605. {
  606. libvlc_exception_t e;
  607. libvlc_exception_init(&e);
  608. libvlc_media_player_next_frame(instance, &e);
  609. catch_exception(&e);
  610. }
  611. - (void)fastForward
  612. {
  613. [self fastForwardAtRate: 2.0];
  614. }
  615. - (void)fastForwardAtRate:(float)rate
  616. {
  617. [self setRate:rate];
  618. }
  619. - (void)rewind
  620. {
  621. [self rewindAtRate: 2.0];
  622. }
  623. - (void)rewindAtRate:(float)rate
  624. {
  625. [self setRate: -rate];
  626. }
  627. - (void)jumpBackward:(NSInteger)interval
  628. {
  629. if( [self isSeekable] )
  630. {
  631. interval = interval * 1000;
  632. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  633. }
  634. }
  635. - (void)jumpForward:(NSInteger)interval
  636. {
  637. if( [self isSeekable] )
  638. {
  639. interval = interval * 1000;
  640. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  641. }
  642. }
  643. - (void)extraShortJumpBackward
  644. {
  645. [self jumpBackward:3];
  646. }
  647. - (void)extraShortJumpForward
  648. {
  649. [self jumpForward:3];
  650. }
  651. - (void)shortJumpBackward
  652. {
  653. [self jumpBackward:10];
  654. }
  655. - (void)shortJumpForward
  656. {
  657. [self jumpForward:10];
  658. }
  659. - (void)mediumJumpBackward
  660. {
  661. [self jumpBackward:60];
  662. }
  663. - (void)mediumJumpForward
  664. {
  665. [self jumpForward:60];
  666. }
  667. - (void)longJumpBackward
  668. {
  669. [self jumpBackward:300];
  670. }
  671. - (void)longJumpForward
  672. {
  673. [self jumpForward:300];
  674. }
  675. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  676. {
  677. return [NSSet setWithObjects:@"state", nil];
  678. }
  679. - (BOOL)isPlaying
  680. {
  681. VLCMediaPlayerState state = [self state];
  682. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  683. (state == VLCMediaPlayerStatePlaying));
  684. }
  685. - (BOOL)willPlay
  686. {
  687. libvlc_exception_t ex;
  688. libvlc_exception_init( &ex );
  689. BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
  690. if (libvlc_exception_raised(&ex))
  691. {
  692. libvlc_exception_clear(&ex);
  693. return NO;
  694. }
  695. else
  696. return ret;
  697. }
  698. static const VLCMediaPlayerState libvlc_to_local_state[] =
  699. {
  700. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  701. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  702. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  703. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  704. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  705. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  706. [libvlc_Error] = VLCMediaPlayerStateError
  707. };
  708. - (VLCMediaPlayerState)state
  709. {
  710. return cachedState;
  711. }
  712. - (float)position
  713. {
  714. return position;
  715. }
  716. - (void)setPosition:(float)newPosition
  717. {
  718. libvlc_exception_t ex;
  719. libvlc_exception_init( &ex );
  720. libvlc_media_player_set_position( instance, newPosition, &ex );
  721. catch_exception( &ex );
  722. }
  723. - (BOOL)isSeekable
  724. {
  725. libvlc_exception_t ex;
  726. libvlc_exception_init( &ex );
  727. BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
  728. catch_exception( &ex );
  729. return ret;
  730. }
  731. - (BOOL)canPause
  732. {
  733. libvlc_exception_t ex;
  734. libvlc_exception_init( &ex );
  735. BOOL ret = libvlc_media_player_can_pause( instance, &ex );
  736. catch_exception( &ex );
  737. return ret;
  738. }
  739. - (void *)libVLCMediaPlayer
  740. {
  741. return instance;
  742. }
  743. @end
  744. @implementation VLCMediaPlayer (Private)
  745. - (id)initWithDrawable:(id)aDrawable
  746. {
  747. if (self = [super init])
  748. {
  749. delegate = nil;
  750. media = nil;
  751. cachedTime = [[VLCTime nullTime] retain];
  752. cachedRemainingTime = [[VLCTime nullTime] retain];
  753. position = 0.0f;
  754. cachedState = VLCMediaPlayerStateStopped;
  755. // Create a media instance, it doesn't matter what library we start off with
  756. // it will change depending on the media descriptor provided to the media
  757. // instance
  758. libvlc_exception_t ex;
  759. libvlc_exception_init( &ex );
  760. instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
  761. catch_exception( &ex );
  762. [self registerObservers];
  763. [self setDrawable:aDrawable];
  764. }
  765. return self;
  766. }
  767. - (void)registerObservers
  768. {
  769. // Attach event observers into the media instance
  770. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  771. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  772. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  773. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  774. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  775. /* FIXME: We may want to turn that off when none is interested by that */
  776. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  777. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  778. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  779. }
  780. - (void)unregisterObservers
  781. {
  782. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  783. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  784. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  785. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  786. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  787. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  788. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  789. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  790. }
  791. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  792. {
  793. [self willChangeValueForKey:@"time"];
  794. [self willChangeValueForKey:@"remainingTime"];
  795. [cachedTime release];
  796. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  797. [cachedRemainingTime release];
  798. double currentTime = [[cachedTime numberValue] doubleValue];
  799. double remaining = currentTime / position * (1 - position);
  800. cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
  801. [self didChangeValueForKey:@"remainingTime"];
  802. [self didChangeValueForKey:@"time"];
  803. }
  804. - (void)delaySleep
  805. {
  806. UpdateSystemActivity(UsrActivity);
  807. }
  808. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  809. {
  810. // This seems to be the most relevant place to delay sleeping and screen saver.
  811. [self delaySleep];
  812. [self willChangeValueForKey:@"position"];
  813. position = [newPosition floatValue];
  814. [self didChangeValueForKey:@"position"];
  815. }
  816. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  817. {
  818. [self willChangeValueForKey:@"state"];
  819. cachedState = [newState intValue];
  820. [self didChangeValueForKey:@"state"];
  821. }
  822. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  823. {
  824. [self willChangeValueForKey:@"media"];
  825. if (media != newMedia)
  826. {
  827. [media release];
  828. media = [newMedia retain];
  829. }
  830. [self didChangeValueForKey:@"media"];
  831. }
  832. @end