VLCMediaPlayer.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. * Faustion Osuna <enrique.osuna # gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24. *****************************************************************************/
  25. #import "VLCLibrary.h"
  26. #import "VLCMediaPlayer.h"
  27. #import "VLCEventManager.h"
  28. #import "VLCLibVLCBridging.h"
  29. #import "VLCVideoView.h"
  30. #ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. #endif
  33. /* prevent system sleep */
  34. #import <CoreServices/CoreServices.h>
  35. /* FIXME: Ugly hack! */
  36. #ifdef __x86_64__
  37. #import <CoreServices/../Frameworks/OSServices.framework/Headers/Power.h>
  38. #endif
  39. #include <vlc/vlc.h>
  40. /* Notification Messages */
  41. NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  42. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  43. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  44. {
  45. static NSString * stateToStrings[] = {
  46. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  47. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  48. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  49. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  50. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  51. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  52. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  53. };
  54. return stateToStrings[state];
  55. }
  56. /* libvlc event callback */
  57. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  58. {
  59. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  60. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  61. withNotificationName:VLCMediaPlayerVolumeChanged];
  62. }
  63. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  64. {
  65. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  66. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  67. withMethod:@selector(mediaPlayerTimeChanged:)
  68. withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_player_time_changed.new_time]];
  69. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  70. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  71. withNotificationName:VLCMediaPlayerTimeChanged];
  72. [pool release];
  73. }
  74. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  75. {
  76. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  77. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  78. withMethod:@selector(mediaPlayerPositionChanged:)
  79. withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_player_position_changed.new_position]];
  80. [pool release];
  81. }
  82. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  83. {
  84. VLCMediaPlayerState newState;
  85. if( event->type == libvlc_MediaPlayerPlaying )
  86. newState = VLCMediaPlayerStatePlaying;
  87. else if( event->type == libvlc_MediaPlayerPaused )
  88. newState = VLCMediaPlayerStatePaused;
  89. else if( event->type == libvlc_MediaPlayerEndReached )
  90. newState = VLCMediaPlayerStateStopped;
  91. else if( event->type == libvlc_MediaPlayerEncounteredError )
  92. newState = VLCMediaPlayerStateError;
  93. else
  94. {
  95. NSLog(@"%s: Unknown event", __FUNCTION__);
  96. return;
  97. }
  98. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  99. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  100. withMethod:@selector(mediaPlayerStateChanged:)
  101. withArgumentAsObject:[NSNumber numberWithInt:newState]];
  102. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  103. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  104. withNotificationName:VLCMediaPlayerStateChanged];
  105. [pool release];
  106. }
  107. // TODO: Documentation
  108. @interface VLCMediaPlayer (Private)
  109. - (id)initWithDrawable:(id)aDrawable;
  110. - (void)registerObservers;
  111. - (void)unregisterObservers;
  112. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  113. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  114. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  115. @end
  116. @implementation VLCMediaPlayer
  117. /* Bindings */
  118. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  119. {
  120. static NSDictionary * dict = nil;
  121. NSSet * superKeyPaths;
  122. if( !dict )
  123. {
  124. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  125. [NSSet setWithObject:@"state"], @"playing",
  126. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  127. [NSSet setWithObjects:@"state", @"media", nil], @"canPause",
  128. [NSSet setWithObjects:@"state", @"media", nil], @"description",
  129. nil] retain];
  130. }
  131. if( (superKeyPaths = [super keyPathsForValuesAffectingValueForKey: key]) )
  132. {
  133. NSMutableSet * ret = [NSMutableSet setWithSet:[dict objectForKey: key]];
  134. [ret unionSet:superKeyPaths];
  135. return ret;
  136. }
  137. return [dict objectForKey: key];
  138. }
  139. /* Contructor */
  140. - (id)init
  141. {
  142. return [self initWithDrawable:nil];
  143. }
  144. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  145. {
  146. return [self initWithDrawable: aVideoView];
  147. }
  148. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  149. {
  150. return [self initWithDrawable: aVideoLayer];
  151. }
  152. - (void)release
  153. {
  154. @synchronized(self)
  155. {
  156. if([self retainCount] <= 1)
  157. {
  158. /* We must make sure we won't receive new event after an upcoming dealloc
  159. * We also may receive a -retain in some event callback that may occcur
  160. * Before libvlc_event_detach. So this can't happen in dealloc */
  161. [self unregisterObservers];
  162. }
  163. [super release];
  164. }
  165. }
  166. - (void)dealloc
  167. {
  168. // Always get rid of the delegate first so we can stop sending messages to it
  169. // TODO: Should we tell the delegate that we're shutting down?
  170. delegate = nil;
  171. libvlc_media_player_release((libvlc_media_player_t *)instance);
  172. // Get rid of everything else
  173. [media release];
  174. [cachedTime release];
  175. [super dealloc];
  176. }
  177. - (void)setDelegate:(id)value
  178. {
  179. delegate = value;
  180. }
  181. - (id)delegate
  182. {
  183. return delegate;
  184. }
  185. - (void)setVideoView:(VLCVideoView *)aVideoView
  186. {
  187. [self setDrawable: aVideoView];
  188. }
  189. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  190. {
  191. [self setDrawable: aVideoLayer];
  192. }
  193. - (void)setDrawable:(id)aDrawable
  194. {
  195. // Make sure that this instance has been associated with the drawing canvas.
  196. libvlc_exception_t ex;
  197. libvlc_exception_init( &ex );
  198. libvlc_media_player_set_nsobject(instance, aDrawable, &ex);
  199. catch_exception( &ex );
  200. }
  201. - (id)drawable
  202. {
  203. libvlc_exception_t ex;
  204. libvlc_exception_init( &ex );
  205. id ret = libvlc_media_player_get_nsobject(instance);
  206. catch_exception( &ex );
  207. return ret;
  208. }
  209. - (VLCAudio *)audio
  210. {
  211. return [[VLCLibrary sharedLibrary] audio];
  212. }
  213. - (void)setVideoAspectRatio:(char *)value
  214. {
  215. libvlc_video_set_aspect_ratio( instance, value, NULL );
  216. }
  217. - (char *)videoAspectRatio
  218. {
  219. libvlc_exception_t ex;
  220. libvlc_exception_init( &ex );
  221. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  222. catch_exception( &ex );
  223. return result;
  224. }
  225. - (void)setVideoSubTitles:(int)value
  226. {
  227. libvlc_video_set_spu( instance, value, NULL );
  228. }
  229. - (int)videoSubTitles
  230. {
  231. libvlc_exception_t ex;
  232. libvlc_exception_init( &ex );
  233. int result = libvlc_video_get_spu( instance, &ex );
  234. catch_exception( &ex );
  235. return result;
  236. }
  237. - (void)setVideoCropGeometry:(char *)value
  238. {
  239. libvlc_video_set_crop_geometry( instance, value, NULL );
  240. }
  241. - (char *)videoCropGeometry
  242. {
  243. libvlc_exception_t ex;
  244. libvlc_exception_init( &ex );
  245. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  246. catch_exception( &ex );
  247. return result;
  248. }
  249. - (void)setVideoTeleText:(int)value
  250. {
  251. libvlc_video_set_teletext( instance, value, NULL );
  252. }
  253. - (int)videoTeleText
  254. {
  255. libvlc_exception_t ex;
  256. libvlc_exception_init( &ex );
  257. int result = libvlc_video_get_teletext( instance, &ex );
  258. catch_exception( &ex );
  259. return result;
  260. }
  261. - (void)setRate:(float)value
  262. {
  263. libvlc_media_player_set_rate( instance, value, NULL );
  264. }
  265. - (float)rate
  266. {
  267. libvlc_exception_t ex;
  268. libvlc_exception_init( &ex );
  269. float result = libvlc_media_player_get_rate( instance, &ex );
  270. catch_exception( &ex );
  271. return result;
  272. }
  273. - (NSSize)videoSize
  274. {
  275. libvlc_exception_t ex;
  276. libvlc_exception_init( &ex );
  277. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  278. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  279. catch_exception( &ex );
  280. return result;
  281. }
  282. - (BOOL)hasVideoOut
  283. {
  284. libvlc_exception_t ex;
  285. libvlc_exception_init( &ex );
  286. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  287. if (libvlc_exception_raised( &ex ))
  288. {
  289. libvlc_exception_clear( &ex );
  290. return NO;
  291. }
  292. else
  293. return result;
  294. }
  295. - (float)framesPerSecond
  296. {
  297. libvlc_exception_t ex;
  298. libvlc_exception_init( &ex );
  299. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  300. catch_exception( &ex );
  301. return result;
  302. }
  303. - (void)setTime:(VLCTime *)value
  304. {
  305. libvlc_exception_t ex;
  306. libvlc_exception_init( &ex );
  307. // Time is managed in seconds, while duration is managed in microseconds
  308. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  309. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  310. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  311. &ex );
  312. catch_exception( &ex );
  313. }
  314. - (VLCTime *)time
  315. {
  316. return cachedTime;
  317. }
  318. - (void)setChapter:(int)value;
  319. {
  320. libvlc_media_player_set_chapter( instance, value, NULL );
  321. }
  322. - (int)chapter
  323. {
  324. libvlc_exception_t ex;
  325. libvlc_exception_init( &ex );
  326. int result = libvlc_media_player_get_chapter( instance, &ex );
  327. catch_exception( &ex );
  328. return result;
  329. }
  330. - (int)countOfChapters
  331. {
  332. libvlc_exception_t ex;
  333. libvlc_exception_init( &ex );
  334. int result = libvlc_media_player_get_chapter_count( instance, &ex );
  335. catch_exception( &ex );
  336. return result;
  337. }
  338. - (void)setAudioTrack:(int)value
  339. {
  340. libvlc_audio_set_track( instance, value, NULL );
  341. }
  342. - (int)audioTrack
  343. {
  344. libvlc_exception_t ex;
  345. libvlc_exception_init( &ex );
  346. int result = libvlc_audio_get_track( instance, &ex );
  347. catch_exception( &ex );
  348. return result;
  349. }
  350. - (int)countOfAudioTracks
  351. {
  352. libvlc_exception_t ex;
  353. libvlc_exception_init( &ex );
  354. int result = libvlc_audio_get_track_count( instance, &ex );
  355. catch_exception( &ex );
  356. return result;
  357. }
  358. - (void)setAudioChannel:(int)value
  359. {
  360. libvlc_audio_set_channel( instance, value, NULL );
  361. }
  362. - (int)audioChannel
  363. {
  364. libvlc_exception_t ex;
  365. libvlc_exception_init( &ex );
  366. int result = libvlc_audio_get_channel( instance, &ex );
  367. catch_exception( &ex );
  368. return result;
  369. }
  370. - (void)setMedia:(VLCMedia *)value
  371. {
  372. if (media != value)
  373. {
  374. if (media && [media compare:value] == NSOrderedSame)
  375. return;
  376. [media release];
  377. media = [value retain];
  378. libvlc_exception_t ex;
  379. libvlc_exception_init( &ex );
  380. libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
  381. catch_exception( &ex );
  382. }
  383. }
  384. - (VLCMedia *)media
  385. {
  386. return media;
  387. }
  388. - (BOOL)play
  389. {
  390. libvlc_exception_t ex;
  391. libvlc_exception_init( &ex );
  392. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  393. catch_exception( &ex );
  394. return YES;
  395. }
  396. - (void)pause
  397. {
  398. if( [NSThread isMainThread] )
  399. {
  400. /* Hack because we create a dead lock here, when the vout is stopped
  401. * and tries to recontact us on the main thread */
  402. /* FIXME: to do this properly we need to do some locking. We may want
  403. * to move that to libvlc */
  404. [self performSelectorInBackground:@selector(pause) withObject:nil];
  405. return;
  406. }
  407. // Pause the stream
  408. libvlc_exception_t ex;
  409. libvlc_exception_init( &ex );
  410. libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
  411. catch_exception( &ex );
  412. }
  413. - (void)stop
  414. {
  415. if( 0 && [NSThread isMainThread] )
  416. {
  417. /* Hack because we create a dead lock here, when the vout is stopped
  418. * and tries to recontact us on the main thread */
  419. /* FIXME: to do this properly we need to do some locking. We may want
  420. * to move that to libvlc */
  421. [self performSelectorInBackground:@selector(stop) withObject:nil];
  422. return;
  423. }
  424. libvlc_exception_t ex;
  425. libvlc_exception_init( &ex );
  426. libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
  427. catch_exception( &ex );
  428. }
  429. - (void)fastForward
  430. {
  431. [self fastForwardAtRate: 2.0];
  432. }
  433. - (void)fastForwardAtRate:(float)rate
  434. {
  435. [self setRate:rate];
  436. }
  437. - (void)rewind
  438. {
  439. [self rewindAtRate: 2.0];
  440. }
  441. - (void)rewindAtRate:(float)rate
  442. {
  443. [self setRate: -rate];
  444. }
  445. - (void)jumpBackward:(NSInteger)interval
  446. {
  447. if( [self isSeekable] )
  448. {
  449. interval = interval * 1000000;
  450. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  451. }
  452. }
  453. - (void)jumpForward:(NSInteger)interval
  454. {
  455. if( [self isSeekable] )
  456. {
  457. interval = interval * 1000000;
  458. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  459. }
  460. }
  461. - (void)extraShortJumpBackward
  462. {
  463. [self jumpBackward:3];
  464. }
  465. - (void)extraShortJumpForward
  466. {
  467. [self jumpForward:3];
  468. }
  469. - (void)shortJumpBackward
  470. {
  471. [self jumpBackward:10];
  472. }
  473. - (void)shortJumpForward
  474. {
  475. [self jumpForward:10];
  476. }
  477. - (void)mediumJumpBackward
  478. {
  479. [self jumpBackward:60];
  480. }
  481. - (void)mediumJumpForward
  482. {
  483. [self jumpForward:60];
  484. }
  485. - (void)longJumpBackward
  486. {
  487. [self jumpBackward:300];
  488. }
  489. - (void)longJumpForward
  490. {
  491. [self jumpForward:300];
  492. }
  493. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  494. {
  495. return [NSSet setWithObjects:@"state", nil];
  496. }
  497. - (BOOL)isPlaying
  498. {
  499. VLCMediaPlayerState state = [self state];
  500. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  501. (state == VLCMediaPlayerStatePlaying));
  502. }
  503. - (BOOL)willPlay
  504. {
  505. libvlc_exception_t ex;
  506. libvlc_exception_init( &ex );
  507. BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
  508. if (libvlc_exception_raised(&ex))
  509. {
  510. libvlc_exception_clear(&ex);
  511. return NO;
  512. }
  513. else
  514. return ret;
  515. }
  516. static const VLCMediaPlayerState libvlc_to_local_state[] =
  517. {
  518. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  519. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  520. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  521. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  522. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  523. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  524. [libvlc_Error] = VLCMediaPlayerStateError
  525. };
  526. - (VLCMediaPlayerState)state
  527. {
  528. return cachedState;
  529. }
  530. - (float)position
  531. {
  532. return position;
  533. }
  534. - (void)setPosition:(float)newPosition
  535. {
  536. libvlc_exception_t ex;
  537. libvlc_exception_init( &ex );
  538. libvlc_media_player_set_position( instance, newPosition, &ex );
  539. catch_exception( &ex );
  540. }
  541. - (BOOL)isSeekable
  542. {
  543. libvlc_exception_t ex;
  544. libvlc_exception_init( &ex );
  545. BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
  546. catch_exception( &ex );
  547. return ret;
  548. }
  549. - (BOOL)canPause
  550. {
  551. libvlc_exception_t ex;
  552. libvlc_exception_init( &ex );
  553. BOOL ret = libvlc_media_player_can_pause( instance, &ex );
  554. catch_exception( &ex );
  555. return ret;
  556. }
  557. - (void *)libVLCMediaPlayer
  558. {
  559. return instance;
  560. }
  561. @end
  562. @implementation VLCMediaPlayer (Private)
  563. - (id)initWithDrawable:(id)aDrawable
  564. {
  565. if (self = [super init])
  566. {
  567. delegate = nil;
  568. media = nil;
  569. cachedTime = [[VLCTime nullTime] retain];
  570. position = 0.0f;
  571. cachedState = VLCMediaPlayerStateStopped;
  572. // Create a media instance, it doesn't matter what library we start off with
  573. // it will change depending on the media descriptor provided to the media
  574. // instance
  575. libvlc_exception_t ex;
  576. libvlc_exception_init( &ex );
  577. instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
  578. catch_exception( &ex );
  579. [self registerObservers];
  580. [self setDrawable:aDrawable];
  581. }
  582. return self;
  583. }
  584. - (void)registerObservers
  585. {
  586. libvlc_exception_t ex;
  587. libvlc_exception_init( &ex );
  588. // Attach event observers into the media instance
  589. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
  590. libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
  591. libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
  592. libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
  593. libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
  594. /* FIXME: We may want to turn that off when none is interested by that */
  595. libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
  596. libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
  597. catch_exception( &ex );
  598. }
  599. - (void)unregisterObservers
  600. {
  601. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
  602. libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
  603. libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
  604. libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
  605. libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
  606. libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
  607. libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
  608. }
  609. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  610. {
  611. [self willChangeValueForKey:@"time"];
  612. [cachedTime release];
  613. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  614. [self didChangeValueForKey:@"time"];
  615. }
  616. - (void)delaySleep
  617. {
  618. UpdateSystemActivity(UsrActivity);
  619. }
  620. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  621. {
  622. // This seems to be the most relevant place to delay sleeping and screen saver.
  623. [self delaySleep];
  624. [self willChangeValueForKey:@"position"];
  625. position = [newPosition floatValue];
  626. [self didChangeValueForKey:@"position"];
  627. }
  628. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  629. {
  630. [self willChangeValueForKey:@"state"];
  631. cachedState = [newState intValue];
  632. [self didChangeValueForKey:@"state"];
  633. }
  634. @end