VLCMediaPlayer.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. // TODO: Documentation
  110. @interface VLCMediaPlayer (Private)
  111. - (id)initWithDrawable:(id)aDrawable;
  112. - (void)registerObservers;
  113. - (void)unregisterObservers;
  114. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  115. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  116. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  117. @end
  118. @implementation VLCMediaPlayer
  119. /* Bindings */
  120. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  121. {
  122. static NSDictionary * dict = nil;
  123. NSSet * superKeyPaths;
  124. if( !dict )
  125. {
  126. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  127. [NSSet setWithObject:@"state"], @"playing",
  128. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  129. [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
  130. [NSSet setWithObjects:@"state", @"media", nil], @"description",
  131. nil] retain];
  132. }
  133. if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
  134. {
  135. NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
  136. [ret unionSet:superKeyPaths];
  137. return ret;
  138. }
  139. return [dict objectForKey: key];
  140. }
  141. /* Contructor */
  142. - (id)init
  143. {
  144. return [self initWithDrawable:nil];
  145. }
  146. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  147. {
  148. return [self initWithDrawable: aVideoView];
  149. }
  150. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  151. {
  152. return [self initWithDrawable: aVideoLayer];
  153. }
  154. - (void)release
  155. {
  156. @synchronized(self)
  157. {
  158. if([self retainCount] <= 1)
  159. {
  160. /* We must make sure we won't receive new event after an upcoming dealloc
  161. * We also may receive a -retain in some event callback that may occcur
  162. * Before libvlc_event_detach. So this can't happen in dealloc */
  163. [self unregisterObservers];
  164. }
  165. [super release];
  166. }
  167. }
  168. - (void)dealloc
  169. {
  170. NSAssert(libvlc_media_player_get_state(instance, NULL) == libvlc_Stopped, @"You released the media player before ensuring that it is stopped");
  171. // Always get rid of the delegate first so we can stop sending messages to it
  172. // TODO: Should we tell the delegate that we're shutting down?
  173. delegate = nil;
  174. // Clear our drawable as we are going to release it, we don't
  175. // want the core to use it from this point. This won't happen as
  176. // the media player must be stopped.
  177. libvlc_media_player_set_nsobject(instance, nil, NULL);
  178. libvlc_media_player_release(instance);
  179. // Get rid of everything else
  180. [media release];
  181. [cachedTime release];
  182. [drawable release];
  183. [super dealloc];
  184. }
  185. - (void)setDelegate:(id)value
  186. {
  187. delegate = value;
  188. }
  189. - (id)delegate
  190. {
  191. return delegate;
  192. }
  193. - (void)setVideoView:(VLCVideoView *)aVideoView
  194. {
  195. [self setDrawable: aVideoView];
  196. }
  197. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  198. {
  199. [self setDrawable: aVideoLayer];
  200. }
  201. - (void)setDrawable:(id)aDrawable
  202. {
  203. // Make sure that this instance has been associated with the drawing canvas.
  204. libvlc_exception_t ex;
  205. libvlc_exception_init( &ex );
  206. libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
  207. catch_exception( &ex );
  208. }
  209. - (id)drawable
  210. {
  211. libvlc_exception_t ex;
  212. libvlc_exception_init( &ex );
  213. id ret = libvlc_media_player_get_nsobject(instance);
  214. catch_exception( &ex );
  215. return ret;
  216. }
  217. - (VLCAudio *)audio
  218. {
  219. return [[VLCLibrary sharedLibrary] audio];
  220. }
  221. - (void)setVideoAspectRatio:(char *)value
  222. {
  223. libvlc_video_set_aspect_ratio( instance, value, NULL );
  224. }
  225. - (char *)videoAspectRatio
  226. {
  227. libvlc_exception_t ex;
  228. libvlc_exception_init( &ex );
  229. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  230. catch_exception( &ex );
  231. return result;
  232. }
  233. - (void)setVideoSubTitles:(int)value
  234. {
  235. libvlc_video_set_spu( instance, value, NULL );
  236. }
  237. - (int)videoSubTitles
  238. {
  239. libvlc_exception_t ex;
  240. libvlc_exception_init( &ex );
  241. int result = libvlc_video_get_spu( instance, &ex );
  242. catch_exception( &ex );
  243. return result;
  244. }
  245. - (void)setVideoCropGeometry:(char *)value
  246. {
  247. libvlc_video_set_crop_geometry( instance, value, NULL );
  248. }
  249. - (char *)videoCropGeometry
  250. {
  251. libvlc_exception_t ex;
  252. libvlc_exception_init( &ex );
  253. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  254. catch_exception( &ex );
  255. return result;
  256. }
  257. - (void)setVideoTeleText:(int)value
  258. {
  259. libvlc_video_set_teletext( instance, value, NULL );
  260. }
  261. - (int)videoTeleText
  262. {
  263. libvlc_exception_t ex;
  264. libvlc_exception_init( &ex );
  265. int result = libvlc_video_get_teletext( instance, &ex );
  266. catch_exception( &ex );
  267. return result;
  268. }
  269. - (void)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  270. {
  271. libvlc_exception_t ex;
  272. libvlc_exception_init( &ex );
  273. libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
  274. catch_exception( &ex );
  275. }
  276. - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
  277. {
  278. libvlc_exception_t ex;
  279. libvlc_exception_init( &ex );
  280. libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
  281. catch_exception( &ex );
  282. }
  283. - (void)setRate:(float)value
  284. {
  285. libvlc_media_player_set_rate( instance, value, NULL );
  286. }
  287. - (float)rate
  288. {
  289. libvlc_exception_t ex;
  290. libvlc_exception_init( &ex );
  291. float result = libvlc_media_player_get_rate( instance, &ex );
  292. catch_exception( &ex );
  293. return result;
  294. }
  295. - (NSSize)videoSize
  296. {
  297. libvlc_exception_t ex;
  298. libvlc_exception_init( &ex );
  299. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  300. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  301. catch_exception( &ex );
  302. return result;
  303. }
  304. - (BOOL)hasVideoOut
  305. {
  306. libvlc_exception_t ex;
  307. libvlc_exception_init( &ex );
  308. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  309. if (libvlc_exception_raised( &ex ))
  310. {
  311. libvlc_exception_clear( &ex );
  312. return NO;
  313. }
  314. else
  315. return result;
  316. }
  317. - (float)framesPerSecond
  318. {
  319. libvlc_exception_t ex;
  320. libvlc_exception_init( &ex );
  321. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  322. catch_exception( &ex );
  323. return result;
  324. }
  325. - (void)setTime:(VLCTime *)value
  326. {
  327. libvlc_exception_t ex;
  328. libvlc_exception_init( &ex );
  329. // Time is managed in seconds, while duration is managed in microseconds
  330. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  331. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  332. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  333. &ex );
  334. catch_exception( &ex );
  335. }
  336. - (VLCTime *)time
  337. {
  338. return cachedTime;
  339. }
  340. - (VLCTime *)remainingTime
  341. {
  342. double currentTime = [[cachedTime numberValue] doubleValue];
  343. double remaining = currentTime / position * (1 - position);
  344. return [VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]];
  345. }
  346. - (int)fps
  347. {
  348. libvlc_exception_t ex;
  349. libvlc_exception_init( &ex );
  350. int result = libvlc_media_player_get_fps( instance, &ex );
  351. catch_exception( &ex );
  352. return result;
  353. }
  354. - (void)setChapter:(int)value;
  355. {
  356. libvlc_exception_t ex;
  357. libvlc_exception_init( &ex );
  358. libvlc_media_player_set_chapter( instance, value, &ex );
  359. catch_exception( &ex );
  360. }
  361. - (int)chapter
  362. {
  363. libvlc_exception_t ex;
  364. libvlc_exception_init( &ex );
  365. int result = libvlc_media_player_get_chapter( instance, &ex );
  366. catch_exception( &ex );
  367. return result;
  368. }
  369. - (int)countOfChapters
  370. {
  371. libvlc_exception_t ex;
  372. libvlc_exception_init( &ex );
  373. int result = libvlc_media_player_get_chapter_count( instance, &ex );
  374. catch_exception( &ex );
  375. return result;
  376. }
  377. - (void)nextChapter
  378. {
  379. libvlc_exception_t ex;
  380. libvlc_exception_init( &ex );
  381. libvlc_media_player_next_chapter( instance, &ex );
  382. catch_exception( &ex );
  383. }
  384. - (void)previousChapter
  385. {
  386. libvlc_exception_t ex;
  387. libvlc_exception_init( &ex );
  388. libvlc_media_player_previous_chapter( instance, &ex );
  389. catch_exception( &ex );
  390. }
  391. - (void)setTitle:(int)value
  392. {
  393. libvlc_exception_t ex;
  394. libvlc_exception_init( &ex );
  395. libvlc_media_player_set_title( instance, value, &ex );
  396. catch_exception( &ex );
  397. }
  398. - (int)title
  399. {
  400. libvlc_exception_t ex;
  401. libvlc_exception_init( &ex );
  402. int result = libvlc_media_player_get_title( instance, &ex );
  403. catch_exception( &ex );
  404. return result;
  405. }
  406. - (int)countOfTitles
  407. {
  408. libvlc_exception_t ex;
  409. libvlc_exception_init( &ex );
  410. int result = libvlc_media_player_get_title_count( instance, &ex );
  411. catch_exception( &ex );
  412. return result;
  413. }
  414. - (void)setAudioTrack:(int)value
  415. {
  416. libvlc_audio_set_track( instance, value, NULL );
  417. }
  418. - (int)audioTrack
  419. {
  420. libvlc_exception_t ex;
  421. libvlc_exception_init( &ex );
  422. int result = libvlc_audio_get_track( instance, &ex );
  423. catch_exception( &ex );
  424. return result;
  425. }
  426. - (int)countOfAudioTracks
  427. {
  428. libvlc_exception_t ex;
  429. libvlc_exception_init( &ex );
  430. int result = libvlc_audio_get_track_count( instance, &ex );
  431. catch_exception( &ex );
  432. return result;
  433. }
  434. - (void)setAudioChannel:(int)value
  435. {
  436. libvlc_audio_set_channel( instance, value, NULL );
  437. }
  438. - (int)audioChannel
  439. {
  440. libvlc_exception_t ex;
  441. libvlc_exception_init( &ex );
  442. int result = libvlc_audio_get_channel( instance, &ex );
  443. catch_exception( &ex );
  444. return result;
  445. }
  446. - (void)setMedia:(VLCMedia *)value
  447. {
  448. if (media != value)
  449. {
  450. if (media && [media compare:value] == NSOrderedSame)
  451. return;
  452. [media release];
  453. media = [value retain];
  454. libvlc_exception_t ex;
  455. libvlc_exception_init( &ex );
  456. libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
  457. catch_exception( &ex );
  458. }
  459. }
  460. - (VLCMedia *)media
  461. {
  462. return media;
  463. }
  464. - (BOOL)play
  465. {
  466. libvlc_exception_t ex;
  467. libvlc_exception_init( &ex );
  468. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  469. catch_exception( &ex );
  470. return YES;
  471. }
  472. - (void)pause
  473. {
  474. if( [NSThread isMainThread] )
  475. {
  476. /* Hack because we create a dead lock here, when the vout is stopped
  477. * and tries to recontact us on the main thread */
  478. /* FIXME: to do this properly we need to do some locking. We may want
  479. * to move that to libvlc */
  480. [self performSelectorInBackground:@selector(pause) withObject:nil];
  481. return;
  482. }
  483. // Pause the stream
  484. libvlc_exception_t ex;
  485. libvlc_exception_init( &ex );
  486. libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
  487. catch_exception( &ex );
  488. }
  489. - (void)stop
  490. {
  491. libvlc_exception_t ex;
  492. libvlc_exception_init( &ex );
  493. libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
  494. catch_exception( &ex );
  495. }
  496. - (void)fastForward
  497. {
  498. [self fastForwardAtRate: 2.0];
  499. }
  500. - (void)fastForwardAtRate:(float)rate
  501. {
  502. [self setRate:rate];
  503. }
  504. - (void)rewind
  505. {
  506. [self rewindAtRate: 2.0];
  507. }
  508. - (void)rewindAtRate:(float)rate
  509. {
  510. [self setRate: -rate];
  511. }
  512. - (void)jumpBackward:(NSInteger)interval
  513. {
  514. if( [self isSeekable] )
  515. {
  516. interval = interval * 1000000;
  517. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  518. }
  519. }
  520. - (void)jumpForward:(NSInteger)interval
  521. {
  522. if( [self isSeekable] )
  523. {
  524. interval = interval * 1000000;
  525. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  526. }
  527. }
  528. - (void)extraShortJumpBackward
  529. {
  530. [self jumpBackward:3];
  531. }
  532. - (void)extraShortJumpForward
  533. {
  534. [self jumpForward:3];
  535. }
  536. - (void)shortJumpBackward
  537. {
  538. [self jumpBackward:10];
  539. }
  540. - (void)shortJumpForward
  541. {
  542. [self jumpForward:10];
  543. }
  544. - (void)mediumJumpBackward
  545. {
  546. [self jumpBackward:60];
  547. }
  548. - (void)mediumJumpForward
  549. {
  550. [self jumpForward:60];
  551. }
  552. - (void)longJumpBackward
  553. {
  554. [self jumpBackward:300];
  555. }
  556. - (void)longJumpForward
  557. {
  558. [self jumpForward:300];
  559. }
  560. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  561. {
  562. return [NSSet setWithObjects:@"state", nil];
  563. }
  564. - (BOOL)isPlaying
  565. {
  566. VLCMediaPlayerState state = [self state];
  567. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  568. (state == VLCMediaPlayerStatePlaying));
  569. }
  570. - (BOOL)willPlay
  571. {
  572. libvlc_exception_t ex;
  573. libvlc_exception_init( &ex );
  574. BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
  575. if (libvlc_exception_raised(&ex))
  576. {
  577. libvlc_exception_clear(&ex);
  578. return NO;
  579. }
  580. else
  581. return ret;
  582. }
  583. static const VLCMediaPlayerState libvlc_to_local_state[] =
  584. {
  585. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  586. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  587. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  588. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  589. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  590. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  591. [libvlc_Error] = VLCMediaPlayerStateError
  592. };
  593. - (VLCMediaPlayerState)state
  594. {
  595. return cachedState;
  596. }
  597. - (float)position
  598. {
  599. return position;
  600. }
  601. - (void)setPosition:(float)newPosition
  602. {
  603. libvlc_exception_t ex;
  604. libvlc_exception_init( &ex );
  605. libvlc_media_player_set_position( instance, newPosition, &ex );
  606. catch_exception( &ex );
  607. }
  608. - (BOOL)isSeekable
  609. {
  610. libvlc_exception_t ex;
  611. libvlc_exception_init( &ex );
  612. BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
  613. catch_exception( &ex );
  614. return ret;
  615. }
  616. - (BOOL)canPause
  617. {
  618. libvlc_exception_t ex;
  619. libvlc_exception_init( &ex );
  620. BOOL ret = libvlc_media_player_can_pause( instance, &ex );
  621. catch_exception( &ex );
  622. return ret;
  623. }
  624. - (void *)libVLCMediaPlayer
  625. {
  626. return instance;
  627. }
  628. @end
  629. @implementation VLCMediaPlayer (Private)
  630. - (id)initWithDrawable:(id)aDrawable
  631. {
  632. if (self = [super init])
  633. {
  634. delegate = nil;
  635. media = nil;
  636. cachedTime = [[VLCTime nullTime] retain];
  637. position = 0.0f;
  638. cachedState = VLCMediaPlayerStateStopped;
  639. // Create a media instance, it doesn't matter what library we start off with
  640. // it will change depending on the media descriptor provided to the media
  641. // instance
  642. libvlc_exception_t ex;
  643. libvlc_exception_init( &ex );
  644. instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
  645. catch_exception( &ex );
  646. [self registerObservers];
  647. [self setDrawable:aDrawable];
  648. }
  649. return self;
  650. }
  651. - (void)registerObservers
  652. {
  653. libvlc_exception_t ex;
  654. libvlc_exception_init( &ex );
  655. // Attach event observers into the media instance
  656. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
  657. libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
  658. libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
  659. libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
  660. libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
  661. /* FIXME: We may want to turn that off when none is interested by that */
  662. libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
  663. libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
  664. catch_exception( &ex );
  665. }
  666. - (void)unregisterObservers
  667. {
  668. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
  669. libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
  670. libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
  671. libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
  672. libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
  673. libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
  674. libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
  675. }
  676. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  677. {
  678. [self willChangeValueForKey:@"time"];
  679. [self willChangeValueForKey:@"remainingTime"];
  680. [cachedTime release];
  681. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  682. [self didChangeValueForKey:@"remainingTime"];
  683. [self didChangeValueForKey:@"time"];
  684. }
  685. - (void)delaySleep
  686. {
  687. UpdateSystemActivity(UsrActivity);
  688. }
  689. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  690. {
  691. // This seems to be the most relevant place to delay sleeping and screen saver.
  692. [self delaySleep];
  693. [self willChangeValueForKey:@"position"];
  694. position = [newPosition floatValue];
  695. [self didChangeValueForKey:@"position"];
  696. }
  697. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  698. {
  699. [self willChangeValueForKey:@"state"];
  700. cachedState = [newState intValue];
  701. [self didChangeValueForKey:@"state"];
  702. }
  703. @end