VLCMediaPlayer.m 28 KB

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