VLCMediaPlayer.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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)saveVideoSnapshotAt: (NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  262. {
  263. libvlc_exception_t ex;
  264. libvlc_exception_init( &ex );
  265. libvlc_video_take_snapshot( instance, [path UTF8String], width, height, &ex );
  266. catch_exception( &ex );
  267. }
  268. - (void)setDeinterlaceFilter: (NSString *)name enabled: (BOOL)enabled
  269. {
  270. libvlc_exception_t ex;
  271. libvlc_exception_init( &ex );
  272. libvlc_video_set_deinterlace( instance, (int)enabled , [name UTF8String], &ex );
  273. catch_exception( &ex );
  274. }
  275. - (void)setRate:(float)value
  276. {
  277. libvlc_media_player_set_rate( instance, value, NULL );
  278. }
  279. - (float)rate
  280. {
  281. libvlc_exception_t ex;
  282. libvlc_exception_init( &ex );
  283. float result = libvlc_media_player_get_rate( instance, &ex );
  284. catch_exception( &ex );
  285. return result;
  286. }
  287. - (NSSize)videoSize
  288. {
  289. libvlc_exception_t ex;
  290. libvlc_exception_init( &ex );
  291. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_player_t *)instance, &ex),
  292. libvlc_video_get_width((libvlc_media_player_t *)instance, &ex));
  293. catch_exception( &ex );
  294. return result;
  295. }
  296. - (BOOL)hasVideoOut
  297. {
  298. libvlc_exception_t ex;
  299. libvlc_exception_init( &ex );
  300. BOOL result = libvlc_media_player_has_vout((libvlc_media_player_t *)instance, &ex);
  301. if (libvlc_exception_raised( &ex ))
  302. {
  303. libvlc_exception_clear( &ex );
  304. return NO;
  305. }
  306. else
  307. return result;
  308. }
  309. - (float)framesPerSecond
  310. {
  311. libvlc_exception_t ex;
  312. libvlc_exception_init( &ex );
  313. float result = libvlc_media_player_get_fps( (libvlc_media_player_t *)instance, &ex );
  314. catch_exception( &ex );
  315. return result;
  316. }
  317. - (void)setTime:(VLCTime *)value
  318. {
  319. libvlc_exception_t ex;
  320. libvlc_exception_init( &ex );
  321. // Time is managed in seconds, while duration is managed in microseconds
  322. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  323. libvlc_media_player_set_time( (libvlc_media_player_t *)instance,
  324. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  325. &ex );
  326. catch_exception( &ex );
  327. }
  328. - (VLCTime *)time
  329. {
  330. return cachedTime;
  331. }
  332. - (void)setChapter:(int)value;
  333. {
  334. libvlc_media_player_set_chapter( instance, value, NULL );
  335. }
  336. - (int)chapter
  337. {
  338. libvlc_exception_t ex;
  339. libvlc_exception_init( &ex );
  340. int result = libvlc_media_player_get_chapter( instance, &ex );
  341. catch_exception( &ex );
  342. return result;
  343. }
  344. - (int)countOfChapters
  345. {
  346. libvlc_exception_t ex;
  347. libvlc_exception_init( &ex );
  348. int result = libvlc_media_player_get_chapter_count( instance, &ex );
  349. catch_exception( &ex );
  350. return result;
  351. }
  352. - (void)setAudioTrack:(int)value
  353. {
  354. libvlc_audio_set_track( instance, value, NULL );
  355. }
  356. - (int)audioTrack
  357. {
  358. libvlc_exception_t ex;
  359. libvlc_exception_init( &ex );
  360. int result = libvlc_audio_get_track( instance, &ex );
  361. catch_exception( &ex );
  362. return result;
  363. }
  364. - (int)countOfAudioTracks
  365. {
  366. libvlc_exception_t ex;
  367. libvlc_exception_init( &ex );
  368. int result = libvlc_audio_get_track_count( instance, &ex );
  369. catch_exception( &ex );
  370. return result;
  371. }
  372. - (void)setAudioChannel:(int)value
  373. {
  374. libvlc_audio_set_channel( instance, value, NULL );
  375. }
  376. - (int)audioChannel
  377. {
  378. libvlc_exception_t ex;
  379. libvlc_exception_init( &ex );
  380. int result = libvlc_audio_get_channel( instance, &ex );
  381. catch_exception( &ex );
  382. return result;
  383. }
  384. - (void)setMedia:(VLCMedia *)value
  385. {
  386. if (media != value)
  387. {
  388. if (media && [media compare:value] == NSOrderedSame)
  389. return;
  390. [media release];
  391. media = [value retain];
  392. libvlc_exception_t ex;
  393. libvlc_exception_init( &ex );
  394. libvlc_media_player_set_media( instance, [media libVLCMediaDescriptor], &ex );
  395. catch_exception( &ex );
  396. }
  397. }
  398. - (VLCMedia *)media
  399. {
  400. return media;
  401. }
  402. - (BOOL)play
  403. {
  404. libvlc_exception_t ex;
  405. libvlc_exception_init( &ex );
  406. libvlc_media_player_play( (libvlc_media_player_t *)instance, &ex );
  407. catch_exception( &ex );
  408. return YES;
  409. }
  410. - (void)pause
  411. {
  412. if( [NSThread isMainThread] )
  413. {
  414. /* Hack because we create a dead lock here, when the vout is stopped
  415. * and tries to recontact us on the main thread */
  416. /* FIXME: to do this properly we need to do some locking. We may want
  417. * to move that to libvlc */
  418. [self performSelectorInBackground:@selector(pause) withObject:nil];
  419. return;
  420. }
  421. // Pause the stream
  422. libvlc_exception_t ex;
  423. libvlc_exception_init( &ex );
  424. libvlc_media_player_pause( (libvlc_media_player_t *)instance, &ex );
  425. catch_exception( &ex );
  426. }
  427. - (void)stop
  428. {
  429. if( 0 && [NSThread isMainThread] )
  430. {
  431. /* Hack because we create a dead lock here, when the vout is stopped
  432. * and tries to recontact us on the main thread */
  433. /* FIXME: to do this properly we need to do some locking. We may want
  434. * to move that to libvlc */
  435. [self performSelectorInBackground:@selector(stop) withObject:nil];
  436. return;
  437. }
  438. libvlc_exception_t ex;
  439. libvlc_exception_init( &ex );
  440. libvlc_media_player_stop((libvlc_media_player_t *)instance, &ex);
  441. catch_exception( &ex );
  442. }
  443. - (void)fastForward
  444. {
  445. [self fastForwardAtRate: 2.0];
  446. }
  447. - (void)fastForwardAtRate:(float)rate
  448. {
  449. [self setRate:rate];
  450. }
  451. - (void)rewind
  452. {
  453. [self rewindAtRate: 2.0];
  454. }
  455. - (void)rewindAtRate:(float)rate
  456. {
  457. [self setRate: -rate];
  458. }
  459. - (void)jumpBackward:(NSInteger)interval
  460. {
  461. if( [self isSeekable] )
  462. {
  463. interval = interval * 1000000;
  464. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  465. }
  466. }
  467. - (void)jumpForward:(NSInteger)interval
  468. {
  469. if( [self isSeekable] )
  470. {
  471. interval = interval * 1000000;
  472. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  473. }
  474. }
  475. - (void)extraShortJumpBackward
  476. {
  477. [self jumpBackward:3];
  478. }
  479. - (void)extraShortJumpForward
  480. {
  481. [self jumpForward:3];
  482. }
  483. - (void)shortJumpBackward
  484. {
  485. [self jumpBackward:10];
  486. }
  487. - (void)shortJumpForward
  488. {
  489. [self jumpForward:10];
  490. }
  491. - (void)mediumJumpBackward
  492. {
  493. [self jumpBackward:60];
  494. }
  495. - (void)mediumJumpForward
  496. {
  497. [self jumpForward:60];
  498. }
  499. - (void)longJumpBackward
  500. {
  501. [self jumpBackward:300];
  502. }
  503. - (void)longJumpForward
  504. {
  505. [self jumpForward:300];
  506. }
  507. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  508. {
  509. return [NSSet setWithObjects:@"state", nil];
  510. }
  511. - (BOOL)isPlaying
  512. {
  513. VLCMediaPlayerState state = [self state];
  514. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  515. (state == VLCMediaPlayerStatePlaying));
  516. }
  517. - (BOOL)willPlay
  518. {
  519. libvlc_exception_t ex;
  520. libvlc_exception_init( &ex );
  521. BOOL ret = libvlc_media_player_will_play( (libvlc_media_player_t *)instance, &ex );
  522. if (libvlc_exception_raised(&ex))
  523. {
  524. libvlc_exception_clear(&ex);
  525. return NO;
  526. }
  527. else
  528. return ret;
  529. }
  530. static const VLCMediaPlayerState libvlc_to_local_state[] =
  531. {
  532. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  533. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  534. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  535. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  536. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  537. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  538. [libvlc_Error] = VLCMediaPlayerStateError
  539. };
  540. - (VLCMediaPlayerState)state
  541. {
  542. return cachedState;
  543. }
  544. - (float)position
  545. {
  546. return position;
  547. }
  548. - (void)setPosition:(float)newPosition
  549. {
  550. libvlc_exception_t ex;
  551. libvlc_exception_init( &ex );
  552. libvlc_media_player_set_position( instance, newPosition, &ex );
  553. catch_exception( &ex );
  554. }
  555. - (BOOL)isSeekable
  556. {
  557. libvlc_exception_t ex;
  558. libvlc_exception_init( &ex );
  559. BOOL ret = libvlc_media_player_is_seekable( instance, &ex );
  560. catch_exception( &ex );
  561. return ret;
  562. }
  563. - (BOOL)canPause
  564. {
  565. libvlc_exception_t ex;
  566. libvlc_exception_init( &ex );
  567. BOOL ret = libvlc_media_player_can_pause( instance, &ex );
  568. catch_exception( &ex );
  569. return ret;
  570. }
  571. - (void *)libVLCMediaPlayer
  572. {
  573. return instance;
  574. }
  575. @end
  576. @implementation VLCMediaPlayer (Private)
  577. - (id)initWithDrawable:(id)aDrawable
  578. {
  579. if (self = [super init])
  580. {
  581. delegate = nil;
  582. media = nil;
  583. cachedTime = [[VLCTime nullTime] retain];
  584. position = 0.0f;
  585. cachedState = VLCMediaPlayerStateStopped;
  586. // Create a media instance, it doesn't matter what library we start off with
  587. // it will change depending on the media descriptor provided to the media
  588. // instance
  589. libvlc_exception_t ex;
  590. libvlc_exception_init( &ex );
  591. instance = (void *)libvlc_media_player_new([VLCLibrary sharedInstance], &ex);
  592. catch_exception( &ex );
  593. [self registerObservers];
  594. [self setDrawable:aDrawable];
  595. }
  596. return self;
  597. }
  598. - (void)registerObservers
  599. {
  600. libvlc_exception_t ex;
  601. libvlc_exception_init( &ex );
  602. // Attach event observers into the media instance
  603. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, &ex );
  604. libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, &ex );
  605. libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, &ex );
  606. libvlc_event_attach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, &ex );
  607. libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, &ex );
  608. /* FIXME: We may want to turn that off when none is interested by that */
  609. libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, &ex );
  610. libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, &ex );
  611. catch_exception( &ex );
  612. }
  613. - (void)unregisterObservers
  614. {
  615. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager( instance, NULL );
  616. libvlc_event_detach( p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self, NULL );
  617. libvlc_event_detach( p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self, NULL );
  618. libvlc_event_detach( p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self, NULL );
  619. libvlc_event_detach( p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self, NULL );
  620. libvlc_event_detach( p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self, NULL );
  621. libvlc_event_detach( p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self, NULL );
  622. }
  623. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  624. {
  625. [self willChangeValueForKey:@"time"];
  626. [cachedTime release];
  627. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  628. [self didChangeValueForKey:@"time"];
  629. }
  630. - (void)delaySleep
  631. {
  632. UpdateSystemActivity(UsrActivity);
  633. }
  634. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  635. {
  636. // This seems to be the most relevant place to delay sleeping and screen saver.
  637. [self delaySleep];
  638. [self willChangeValueForKey:@"position"];
  639. position = [newPosition floatValue];
  640. [self didChangeValueForKey:@"position"];
  641. }
  642. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  643. {
  644. [self willChangeValueForKey:@"state"];
  645. cachedState = [newState intValue];
  646. [self didChangeValueForKey:@"state"];
  647. }
  648. @end