VLCMediaPlayer.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. catch_exception( &ex );
  246. if (count <= 0)
  247. return NSNotFound;
  248. NSUInteger result = libvlc_video_get_spu( instance, &ex );
  249. catch_exception( &ex );
  250. return result;
  251. }
  252. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  253. {
  254. libvlc_exception_t ex;
  255. libvlc_exception_init( &ex );
  256. BOOL result = libvlc_video_set_subtitle_file( instance, [path UTF8String], &ex );
  257. catch_exception( &ex );
  258. return result;
  259. }
  260. - (NSArray *)videoSubTitles
  261. {
  262. libvlc_exception_t ex;
  263. libvlc_exception_init( &ex );
  264. NSInteger count = libvlc_video_get_spu_count( instance, &ex );
  265. catch_exception( &ex );
  266. libvlc_track_description_t *tracks = libvlc_video_get_spu_description( instance, &ex );
  267. NSMutableArray *tempArray = [NSMutableArray array];
  268. NSInteger i;
  269. for (i = 0; i < count; i++)
  270. {
  271. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  272. tracks = tracks->p_next;
  273. }
  274. return [NSArray arrayWithArray: tempArray];
  275. }
  276. #pragma mark -
  277. #pragma mark Video Crop geometry
  278. - (void)setVideoCropGeometry:(char *)value
  279. {
  280. libvlc_exception_t ex;
  281. libvlc_exception_init( &ex );
  282. libvlc_video_set_crop_geometry( instance, value, &ex );
  283. catch_exception( &ex );
  284. }
  285. - (char *)videoCropGeometry
  286. {
  287. libvlc_exception_t ex;
  288. libvlc_exception_init( &ex );
  289. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  290. catch_exception( &ex );
  291. return result;
  292. }
  293. - (void)setVideoAspectRatio:(char *)value
  294. {
  295. libvlc_exception_t ex;
  296. libvlc_exception_init( &ex );
  297. libvlc_video_set_aspect_ratio( instance, value, &ex );
  298. catch_exception( &ex );
  299. }
  300. - (char *)videoAspectRatio
  301. {
  302. libvlc_exception_t ex;
  303. libvlc_exception_init( &ex );
  304. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  305. catch_exception( &ex );
  306. return result;
  307. }
  308. - (void)setVideoTeleText:(NSUInteger)value
  309. {
  310. libvlc_exception_t ex;
  311. libvlc_exception_init( &ex );
  312. libvlc_video_set_teletext( instance, value, &ex );
  313. catch_exception( &ex );
  314. }
  315. - (NSUInteger)videoTeleText
  316. {
  317. libvlc_exception_t ex;
  318. libvlc_exception_init( &ex );
  319. NSInteger result = libvlc_video_get_teletext( instance, &ex );
  320. catch_exception( &ex );
  321. return result;
  322. }
  323. - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  324. {
  325. libvlc_exception_t ex;
  326. libvlc_exception_init( &ex );
  327. libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
  328. catch_exception( &ex );
  329. }
  330. - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
  331. {
  332. libvlc_exception_t ex;
  333. libvlc_exception_init( &ex );
  334. libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
  335. catch_exception( &ex );
  336. }
  337. - (void)setRate:(float)value
  338. {
  339. libvlc_exception_t ex;
  340. libvlc_exception_init( &ex );
  341. libvlc_media_player_set_rate( instance, value, &ex );
  342. catch_exception( &ex );
  343. }
  344. - (float)rate
  345. {
  346. libvlc_exception_t ex;
  347. libvlc_exception_init( &ex );
  348. float result = libvlc_media_player_get_rate( instance, &ex );
  349. catch_exception( &ex );
  350. return result;
  351. }
  352. - (NSSize)videoSize
  353. {
  354. libvlc_exception_t ex;
  355. libvlc_exception_init( &ex );
  356. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  357. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  358. catch_exception( &ex );
  359. return result;
  360. }
  361. - (BOOL)hasVideoOut
  362. {
  363. libvlc_exception_t ex;
  364. libvlc_exception_init( &ex );
  365. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  366. if (libvlc_exception_raised( &ex ))
  367. {
  368. libvlc_exception_clear( &ex );
  369. return NO;
  370. }
  371. else
  372. return result;
  373. }
  374. - (float)framesPerSecond
  375. {
  376. libvlc_exception_t ex;
  377. libvlc_exception_init( &ex );
  378. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  379. catch_exception( &ex );
  380. return result;
  381. }
  382. - (void)setTime:(VLCTime *)value
  383. {
  384. libvlc_exception_t ex;
  385. libvlc_exception_init( &ex );
  386. // Time is managed in seconds, while duration is managed in microseconds
  387. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  388. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  389. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  390. &ex );
  391. catch_exception( &ex );
  392. }
  393. - (VLCTime *)time
  394. {
  395. return cachedTime;
  396. }
  397. - (VLCTime *)remainingTime
  398. {
  399. return cachedRemainingTime;
  400. }
  401. - (NSUInteger)fps
  402. {
  403. libvlc_exception_t ex;
  404. libvlc_exception_init( &ex );
  405. NSUInteger result = libvlc_media_player_get_fps( instance, &ex );
  406. catch_exception( &ex );
  407. return result;
  408. }
  409. #pragma mark -
  410. #pragma mark Chapters
  411. - (void)setCurrentChapterIndex:(NSUInteger)value;
  412. {
  413. libvlc_exception_t ex;
  414. libvlc_exception_init( &ex );
  415. libvlc_media_player_set_chapter( instance, value, &ex );
  416. catch_exception( &ex );
  417. }
  418. - (NSUInteger)currentChapterIndex
  419. {
  420. libvlc_exception_t ex;
  421. libvlc_exception_init( &ex );
  422. NSInteger count = libvlc_media_player_get_chapter_count( instance, &ex );
  423. catch_exception( &ex );
  424. if (count <= 0)
  425. return NSNotFound;
  426. NSUInteger result = libvlc_media_player_get_chapter( instance, &ex );
  427. catch_exception( &ex );
  428. return result;
  429. }
  430. - (void)nextChapter
  431. {
  432. libvlc_exception_t ex;
  433. libvlc_exception_init( &ex );
  434. libvlc_media_player_next_chapter( instance, &ex );
  435. catch_exception( &ex );
  436. }
  437. - (void)previousChapter
  438. {
  439. libvlc_exception_t ex;
  440. libvlc_exception_init( &ex );
  441. libvlc_media_player_previous_chapter( instance, &ex );
  442. catch_exception( &ex );
  443. }
  444. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  445. {
  446. libvlc_exception_t ex;
  447. libvlc_exception_init( &ex );
  448. NSInteger count = libvlc_media_player_get_chapter_count(instance, &ex);
  449. if (count <= 0)
  450. return [NSArray array];
  451. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description( instance, title, &ex );
  452. NSMutableArray *tempArray = [NSMutableArray array];
  453. NSInteger i;
  454. for (i = 0; i < count ; i++)
  455. {
  456. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  457. tracks = tracks->p_next;
  458. }
  459. return [NSArray arrayWithArray: tempArray];
  460. }
  461. #pragma mark -
  462. #pragma mark Titles
  463. - (void)setCurrentTitleIndex:(NSUInteger)value
  464. {
  465. libvlc_exception_t ex;
  466. libvlc_exception_init( &ex );
  467. libvlc_media_player_set_title( instance, value, &ex );
  468. catch_exception( &ex );
  469. }
  470. - (NSUInteger)currentTitleIndex
  471. {
  472. libvlc_exception_t ex;
  473. libvlc_exception_init( &ex );
  474. NSInteger count = libvlc_media_player_get_title_count( instance, &ex );
  475. catch_exception( &ex );
  476. if (count <= 0)
  477. return NSNotFound;
  478. NSUInteger result = libvlc_media_player_get_title( instance, &ex );
  479. catch_exception( &ex );
  480. return result;
  481. }
  482. - (NSUInteger)countOfTitles
  483. {
  484. libvlc_exception_t ex;
  485. libvlc_exception_init( &ex );
  486. NSUInteger result = libvlc_media_player_get_title_count( instance, &ex );
  487. catch_exception( &ex );
  488. return result;
  489. }
  490. - (NSArray *)titles
  491. {
  492. libvlc_exception_t ex;
  493. libvlc_exception_init( &ex );
  494. libvlc_track_description_t *tracks = libvlc_video_get_title_description( instance, &ex );
  495. NSMutableArray *tempArray = [NSMutableArray array];
  496. NSInteger i;
  497. for (i = 0; i < [self countOfTitles] ; i++)
  498. {
  499. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  500. tracks = tracks->p_next;
  501. }
  502. return [NSArray arrayWithArray: tempArray];
  503. }
  504. #pragma mark -
  505. #pragma mark Audio tracks
  506. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  507. {
  508. libvlc_exception_t ex;
  509. libvlc_exception_init( &ex );
  510. libvlc_audio_set_track( instance, (int)value, &ex );
  511. catch_exception( &ex );
  512. }
  513. - (NSUInteger)currentAudioTrackIndex
  514. {
  515. libvlc_exception_t ex;
  516. libvlc_exception_init( &ex );
  517. NSInteger count = libvlc_audio_get_track_count( instance, &ex );
  518. catch_exception( &ex );
  519. if (count <= 0)
  520. return NSNotFound;
  521. NSUInteger result = libvlc_audio_get_track( instance, &ex );
  522. catch_exception( &ex );
  523. return result;
  524. }
  525. - (NSArray *)audioTracks
  526. {
  527. libvlc_exception_t ex;
  528. libvlc_exception_init( &ex );
  529. NSInteger count = libvlc_audio_get_track_count( instance, &ex );
  530. catch_exception( &ex );
  531. if (count <= 0)
  532. return [NSArray array];
  533. libvlc_track_description_t *tracks = libvlc_audio_get_track_description( instance, &ex );
  534. NSMutableArray *tempArray = [NSMutableArray array];
  535. NSUInteger i;
  536. for (i = 0; i < count ; i++)
  537. {
  538. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  539. tracks = tracks->p_next;
  540. }
  541. return [NSArray arrayWithArray: tempArray];
  542. }
  543. - (void)setAudioChannel:(NSInteger)value
  544. {
  545. libvlc_exception_t ex;
  546. libvlc_exception_init( &ex );
  547. libvlc_audio_set_channel( instance, value, &ex );
  548. catch_exception( &ex );
  549. }
  550. - (NSInteger)audioChannel
  551. {
  552. libvlc_exception_t ex;
  553. libvlc_exception_init( &ex );
  554. NSInteger result = libvlc_audio_get_channel( instance, &ex );
  555. catch_exception( &ex );
  556. return result;
  557. }
  558. - (void)setMedia:(VLCMedia *)value
  559. {
  560. if (media != value)
  561. {
  562. if (media && [media compare:value] == NSOrderedSame)
  563. return;
  564. [media release];
  565. media = [value retain];
  566. libvlc_exception_t ex;
  567. libvlc_exception_init( &ex );
  568. libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
  569. catch_exception( &ex );
  570. }
  571. }
  572. - (VLCMedia *)media
  573. {
  574. return media;
  575. }
  576. - (BOOL)play
  577. {
  578. libvlc_exception_t ex;
  579. libvlc_exception_init( &ex );
  580. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  581. catch_exception( &ex );
  582. return YES;
  583. }
  584. - (void)pause
  585. {
  586. if( [NSThread isMainThread] )
  587. {
  588. /* Hack because we create a dead lock here, when the vout is stopped
  589. * and tries to recontact us on the main thread */
  590. /* FIXME: to do this properly we need to do some locking. We may want
  591. * to move that to libvlc */
  592. [self performSelectorInBackground:@selector(pause) withObject:nil];
  593. return;
  594. }
  595. // Pause the stream
  596. libvlc_exception_t ex;
  597. libvlc_exception_init( &ex );
  598. libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
  599. // fail gracefully
  600. // in most cases, it's just EOF so let's stop
  601. if (libvlc_exception_raised(&ex))
  602. [self stop];
  603. libvlc_exception_clear(&ex);
  604. }
  605. - (void)stop
  606. {
  607. libvlc_exception_t ex;
  608. libvlc_exception_init( &ex );
  609. libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
  610. catch_exception( &ex );
  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 * 1000000;
  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 * 1000000;
  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. libvlc_exception_t ex;
  771. libvlc_exception_init( &ex );
  772. // Attach event observers into the media instance
  773. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
  774. libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
  775. libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
  776. libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
  777. libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
  778. /* FIXME: We may want to turn that off when none is interested by that */
  779. libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
  780. libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
  781. libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, &ex );
  782. catch_exception( &ex );
  783. }
  784. - (void)unregisterObservers
  785. {
  786. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
  787. libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
  788. libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
  789. libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
  790. libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
  791. libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
  792. libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
  793. libvlc_event_attach( p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self, NULL );
  794. }
  795. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  796. {
  797. [self willChangeValueForKey:@"time"];
  798. [self willChangeValueForKey:@"remainingTime"];
  799. [cachedTime release];
  800. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  801. [cachedRemainingTime release];
  802. double currentTime = [[cachedTime numberValue] doubleValue];
  803. double remaining = currentTime / position * (1 - position);
  804. cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
  805. [self didChangeValueForKey:@"remainingTime"];
  806. [self didChangeValueForKey:@"time"];
  807. }
  808. - (void)delaySleep
  809. {
  810. UpdateSystemActivity(UsrActivity);
  811. }
  812. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  813. {
  814. // This seems to be the most relevant place to delay sleeping and screen saver.
  815. [self delaySleep];
  816. [self willChangeValueForKey:@"position"];
  817. position = [newPosition floatValue];
  818. [self didChangeValueForKey:@"position"];
  819. }
  820. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  821. {
  822. [self willChangeValueForKey:@"state"];
  823. cachedState = [newState intValue];
  824. [self didChangeValueForKey:@"state"];
  825. }
  826. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  827. {
  828. [self willChangeValueForKey:@"media"];
  829. if (media != newMedia)
  830. {
  831. [media release];
  832. media = [newMedia retain];
  833. }
  834. [self didChangeValueForKey:@"media"];
  835. }
  836. @end