VLCMediaPlayer.m 27 KB

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