VLCMediaPlayer.m 22 KB

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