VLCMediaPlayer.m 22 KB

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