VLCMediaPlayer.m 23 KB

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