VLCMediaPlayer.m 24 KB

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