VLCMediaPlayer.m 27 KB

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