VLCMediaPlayer.m 21 KB

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