VLCMediaPlayer.m 23 KB

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