VLCMediaPlayer.m 23 KB

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