VLCMediaPlayer.m 27 KB

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