VLCMediaPlayer.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLCKit.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007-2009 Pierre d'Herbemont
  5. * Copyright (C) 2007-2013 VLC authors and VideoLAN
  6. * Partial Copyright (C) 2009-2013 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. #if TARGET_OS_IPHONE
  179. [[NSNotificationCenter defaultCenter] removeObserver:self];
  180. #endif
  181. // Always get rid of the delegate first so we can stop sending messages to it
  182. // TODO: Should we tell the delegate that we're shutting down?
  183. delegate = nil;
  184. // Clear our drawable as we are going to release it, we don't
  185. // want the core to use it from this point. This won't happen as
  186. // the media player must be stopped.
  187. libvlc_media_player_set_nsobject(instance, nil);
  188. libvlc_media_player_release(instance);
  189. // Get rid of everything else
  190. [media release];
  191. [cachedTime release];
  192. [cachedRemainingTime release];
  193. [drawable release];
  194. [audio release];
  195. [super dealloc];
  196. }
  197. - (void)setDelegate:(id)value
  198. {
  199. delegate = value;
  200. }
  201. - (id)delegate
  202. {
  203. return delegate;
  204. }
  205. #if !TARGET_OS_IPHONE
  206. - (void)setVideoView:(VLCVideoView *)aVideoView
  207. {
  208. [self setDrawable: aVideoView];
  209. }
  210. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  211. {
  212. [self setDrawable: aVideoLayer];
  213. }
  214. #endif
  215. - (void)setDrawable:(id)aDrawable
  216. {
  217. // Make sure that this instance has been associated with the drawing canvas.
  218. libvlc_media_player_set_nsobject(instance, aDrawable);
  219. }
  220. - (id)drawable
  221. {
  222. return libvlc_media_player_get_nsobject(instance);
  223. }
  224. - (VLCAudio *)audio
  225. {
  226. if (!audio)
  227. audio = [[VLCAudio alloc] initWithMediaPlayer:self];
  228. return audio;
  229. }
  230. #pragma mark -
  231. #pragma mark Video Tracks
  232. - (void)setCurrentVideoTrackIndex:(NSUInteger)value
  233. {
  234. libvlc_video_set_track(instance, (int)value);
  235. }
  236. - (NSUInteger)currentVideoTrackIndex
  237. {
  238. NSInteger count = libvlc_video_get_track_count(instance);
  239. if (count <= 0)
  240. return NSNotFound;
  241. NSUInteger result = libvlc_video_get_track(instance);
  242. return result;
  243. }
  244. - (NSArray *)videoTracks
  245. {
  246. NSInteger count = libvlc_video_get_track_count(instance);
  247. if (count <= 0)
  248. return [NSArray array];
  249. libvlc_track_description_t *tracks = libvlc_video_get_track_description(instance);
  250. NSMutableArray *tempArray = [NSMutableArray array];
  251. NSUInteger i;
  252. for (i = 0; i < count ; i++)
  253. {
  254. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  255. tracks = tracks->p_next;
  256. }
  257. libvlc_track_description_list_release(tracks);
  258. return [NSArray arrayWithArray: tempArray];
  259. }
  260. #pragma mark -
  261. #pragma mark Subtitles
  262. - (void)setCurrentVideoSubTitleIndex:(NSUInteger)index
  263. {
  264. libvlc_video_set_spu(instance, (int)index);
  265. }
  266. - (NSUInteger)currentVideoSubTitleIndex
  267. {
  268. NSInteger count = libvlc_video_get_spu_count(instance);
  269. if (count <= 0)
  270. return NSNotFound;
  271. return libvlc_video_get_spu(instance);
  272. }
  273. - (BOOL)openVideoSubTitlesFromFile:(NSString *)path
  274. {
  275. return libvlc_video_set_subtitle_file(instance, [path UTF8String]);
  276. }
  277. - (NSArray *)videoSubTitles
  278. {
  279. libvlc_track_description_t *currentTrack = libvlc_video_get_spu_description(instance);
  280. NSMutableArray *tempArray = [NSMutableArray array];
  281. while (currentTrack) {
  282. [tempArray addObject:[NSString stringWithUTF8String:currentTrack->psz_name]];
  283. currentTrack = currentTrack->p_next;
  284. }
  285. libvlc_track_description_list_release(currentTrack);
  286. return [NSArray arrayWithArray: tempArray];
  287. }
  288. - (void)setCurrentVideoSubTitleDelay:(NSInteger)index
  289. {
  290. libvlc_video_set_spu_delay(instance, index);
  291. }
  292. - (NSInteger)currentVideoSubTitleDelay
  293. {
  294. return libvlc_video_get_spu_delay(instance);
  295. }
  296. #pragma mark -
  297. #pragma mark Video Crop geometry
  298. - (void)setVideoCropGeometry:(char *)value
  299. {
  300. libvlc_video_set_crop_geometry(instance, value);
  301. }
  302. - (char *)videoCropGeometry
  303. {
  304. char * result = libvlc_video_get_crop_geometry(instance);
  305. return result;
  306. }
  307. - (void)setVideoAspectRatio:(char *)value
  308. {
  309. libvlc_video_set_aspect_ratio( instance, value );
  310. }
  311. - (char *)videoAspectRatio
  312. {
  313. char * result = libvlc_video_get_aspect_ratio( instance );
  314. return result;
  315. }
  316. - (void)setScaleFactor:(float)value
  317. {
  318. libvlc_video_set_scale(instance, value);
  319. }
  320. - (float)scaleFactor
  321. {
  322. return libvlc_video_get_scale(instance);
  323. }
  324. - (void)saveVideoSnapshotAt:(NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  325. {
  326. int failure = libvlc_video_take_snapshot(instance, 0, [path UTF8String], width, height);
  327. if (failure)
  328. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  329. }
  330. - (void)setDeinterlaceFilter:(NSString *)name
  331. {
  332. libvlc_video_set_deinterlace(instance, [name UTF8String]);
  333. }
  334. - (void)setRate:(float)value
  335. {
  336. libvlc_media_player_set_rate(instance, value);
  337. }
  338. - (float)rate
  339. {
  340. return libvlc_media_player_get_rate(instance);
  341. }
  342. - (CGSize)videoSize
  343. {
  344. unsigned height = 0, width = 0;
  345. int failure = libvlc_video_get_size(instance, 0, &width, &height);
  346. if (failure)
  347. [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
  348. return CGSizeMake(width, height);
  349. }
  350. - (BOOL)hasVideoOut
  351. {
  352. return libvlc_media_player_has_vout(instance);
  353. }
  354. - (float)framesPerSecond
  355. {
  356. return libvlc_media_player_get_fps(instance);
  357. }
  358. - (void)setTime:(VLCTime *)value
  359. {
  360. // Time is managed in seconds, while duration is managed in microseconds
  361. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  362. libvlc_media_player_set_time(instance, value ? [[value numberValue] longLongValue] : 0);
  363. }
  364. - (VLCTime *)time
  365. {
  366. return cachedTime;
  367. }
  368. - (VLCTime *)remainingTime
  369. {
  370. return cachedRemainingTime;
  371. }
  372. - (NSUInteger)fps
  373. {
  374. return libvlc_media_player_get_fps(instance);
  375. }
  376. #pragma mark -
  377. #pragma mark Chapters
  378. - (void)setCurrentChapterIndex:(NSUInteger)value;
  379. {
  380. libvlc_media_player_set_chapter(instance, value);
  381. }
  382. - (NSUInteger)currentChapterIndex
  383. {
  384. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  385. if (count <= 0)
  386. return NSNotFound;
  387. NSUInteger result = libvlc_media_player_get_chapter(instance);
  388. return result;
  389. }
  390. - (void)nextChapter
  391. {
  392. libvlc_media_player_next_chapter(instance);
  393. }
  394. - (void)previousChapter
  395. {
  396. libvlc_media_player_previous_chapter(instance);
  397. }
  398. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  399. {
  400. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  401. if (count <= 0)
  402. return [NSArray array];
  403. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(instance, title);
  404. NSMutableArray *tempArray = [NSMutableArray array];
  405. NSInteger i;
  406. for (i = 0; i < count ; i++)
  407. {
  408. [tempArray addObject:[NSString stringWithUTF8String:tracks->psz_name]];
  409. tracks = tracks->p_next;
  410. }
  411. libvlc_track_description_list_release(tracks);
  412. return [NSArray arrayWithArray:tempArray];
  413. }
  414. #pragma mark -
  415. #pragma mark Titles
  416. - (void)setCurrentTitleIndex:(NSUInteger)value
  417. {
  418. libvlc_media_player_set_title(instance, value);
  419. }
  420. - (NSUInteger)currentTitleIndex
  421. {
  422. NSInteger count = libvlc_media_player_get_title_count(instance);
  423. if (count <= 0)
  424. return NSNotFound;
  425. return libvlc_media_player_get_title(instance);
  426. }
  427. - (NSUInteger)countOfTitles
  428. {
  429. NSUInteger result = libvlc_media_player_get_title_count(instance);
  430. return result;
  431. }
  432. - (NSArray *)titles
  433. {
  434. libvlc_track_description_t *tracks = libvlc_video_get_title_description(instance);
  435. NSMutableArray *tempArray = [NSMutableArray array];
  436. NSInteger i;
  437. for (i = 0; i < [self countOfTitles] ; i++)
  438. {
  439. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  440. tracks = tracks->p_next;
  441. }
  442. libvlc_track_description_list_release(tracks);
  443. return [NSArray arrayWithArray: tempArray];
  444. }
  445. #pragma mark -
  446. #pragma mark Audio tracks
  447. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  448. {
  449. libvlc_audio_set_track(instance, (int)value);
  450. }
  451. - (NSUInteger)currentAudioTrackIndex
  452. {
  453. NSInteger count = libvlc_audio_get_track_count(instance);
  454. if (count <= 0)
  455. return NSNotFound;
  456. NSUInteger result = libvlc_audio_get_track(instance);
  457. return result;
  458. }
  459. - (NSArray *)audioTracks
  460. {
  461. NSInteger count = libvlc_audio_get_track_count(instance);
  462. if (count <= 0)
  463. return [NSArray array];
  464. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(instance);
  465. NSMutableArray *tempArray = [NSMutableArray array];
  466. NSUInteger i;
  467. for (i = 0; i < count ; i++)
  468. {
  469. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  470. tracks = tracks->p_next;
  471. }
  472. libvlc_track_description_list_release(tracks);
  473. return [NSArray arrayWithArray: tempArray];
  474. }
  475. - (void)setAudioChannel:(NSInteger)value
  476. {
  477. libvlc_audio_set_channel(instance, value);
  478. }
  479. - (NSInteger)audioChannel
  480. {
  481. return libvlc_audio_get_channel(instance);
  482. }
  483. - (void)setCurrentAudioPlaybackDelay:(NSInteger)index
  484. {
  485. libvlc_audio_set_delay(instance, index);
  486. }
  487. - (NSInteger)currentAudioPlaybackDelay
  488. {
  489. return libvlc_audio_get_delay(instance);
  490. }
  491. #pragma mark -
  492. #pragma mark set/get media
  493. - (void)setMedia:(VLCMedia *)value
  494. {
  495. if (media != value)
  496. {
  497. if (media && [media compare:value] == NSOrderedSame)
  498. return;
  499. [media release];
  500. media = [value retain];
  501. libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
  502. }
  503. }
  504. - (VLCMedia *)media
  505. {
  506. return media;
  507. }
  508. #pragma mark -
  509. #pragma mark playback
  510. - (BOOL)play
  511. {
  512. libvlc_media_player_play(instance);
  513. return YES;
  514. }
  515. - (void)pause
  516. {
  517. if( [NSThread isMainThread] )
  518. {
  519. /* Hack because we create a dead lock here, when the vout is stopped
  520. * and tries to recontact us on the main thread */
  521. /* FIXME: to do this properly we need to do some locking. We may want
  522. * to move that to libvlc */
  523. [self performSelectorInBackground:@selector(pause) withObject:nil];
  524. return;
  525. }
  526. // Pause the stream
  527. libvlc_media_player_pause(instance);
  528. }
  529. - (void)stop
  530. {
  531. libvlc_media_player_stop(instance);
  532. }
  533. - (void)gotoNextFrame
  534. {
  535. libvlc_media_player_next_frame(instance);
  536. }
  537. - (void)fastForward
  538. {
  539. [self fastForwardAtRate: 2.0];
  540. }
  541. - (void)fastForwardAtRate:(float)rate
  542. {
  543. [self setRate:rate];
  544. }
  545. - (void)rewind
  546. {
  547. [self rewindAtRate: 2.0];
  548. }
  549. - (void)rewindAtRate:(float)rate
  550. {
  551. [self setRate: -rate];
  552. }
  553. - (void)jumpBackward:(NSInteger)interval
  554. {
  555. if( [self isSeekable] )
  556. {
  557. interval = interval * 1000;
  558. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  559. }
  560. }
  561. - (void)jumpForward:(NSInteger)interval
  562. {
  563. if( [self isSeekable] )
  564. {
  565. interval = interval * 1000;
  566. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  567. }
  568. }
  569. - (void)extraShortJumpBackward
  570. {
  571. [self jumpBackward:3];
  572. }
  573. - (void)extraShortJumpForward
  574. {
  575. [self jumpForward:3];
  576. }
  577. - (void)shortJumpBackward
  578. {
  579. [self jumpBackward:10];
  580. }
  581. - (void)shortJumpForward
  582. {
  583. [self jumpForward:10];
  584. }
  585. - (void)mediumJumpBackward
  586. {
  587. [self jumpBackward:60];
  588. }
  589. - (void)mediumJumpForward
  590. {
  591. [self jumpForward:60];
  592. }
  593. - (void)longJumpBackward
  594. {
  595. [self jumpBackward:300];
  596. }
  597. - (void)longJumpForward
  598. {
  599. [self jumpForward:300];
  600. }
  601. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  602. {
  603. return [NSSet setWithObjects:@"state", nil];
  604. }
  605. - (BOOL)isPlaying
  606. {
  607. VLCMediaPlayerState state = [self state];
  608. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  609. (state == VLCMediaPlayerStatePlaying));
  610. }
  611. - (BOOL)willPlay
  612. {
  613. return libvlc_media_player_will_play(instance);
  614. }
  615. static const VLCMediaPlayerState libvlc_to_local_state[] =
  616. {
  617. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  618. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  619. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  620. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  621. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  622. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  623. [libvlc_Error] = VLCMediaPlayerStateError
  624. };
  625. - (VLCMediaPlayerState)state
  626. {
  627. return cachedState;
  628. }
  629. - (float)position
  630. {
  631. return position;
  632. }
  633. - (void)setPosition:(float)newPosition
  634. {
  635. libvlc_media_player_set_position(instance, newPosition);
  636. }
  637. - (BOOL)isSeekable
  638. {
  639. return libvlc_media_player_is_seekable(instance);
  640. }
  641. - (BOOL)canPause
  642. {
  643. return libvlc_media_player_can_pause(instance);
  644. }
  645. - (void *)libVLCMediaPlayer
  646. {
  647. return instance;
  648. }
  649. @end
  650. @implementation VLCMediaPlayer (Private)
  651. - (id)initWithDrawable:(id)aDrawable
  652. {
  653. if (self = [super init])
  654. {
  655. delegate = nil;
  656. media = nil;
  657. cachedTime = [[VLCTime nullTime] retain];
  658. cachedRemainingTime = [[VLCTime nullTime] retain];
  659. position = 0.0f;
  660. cachedState = VLCMediaPlayerStateStopped;
  661. // Create a media instance, it doesn't matter what library we start off with
  662. // it will change depending on the media descriptor provided to the media
  663. // instance
  664. instance = libvlc_media_player_new([VLCLibrary sharedInstance]);
  665. [self registerObservers];
  666. #if TARGET_OS_IPHONE
  667. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
  668. name:UIApplicationWillResignActiveNotification object:nil];
  669. #endif
  670. [self setDrawable:aDrawable];
  671. }
  672. return self;
  673. }
  674. - (void)registerObservers
  675. {
  676. // Attach event observers into the media instance
  677. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  678. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  679. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  680. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  681. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  682. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  683. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  684. /* FIXME: We may want to turn that off when none is interested by that */
  685. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  686. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  687. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  688. }
  689. - (void)unregisterObservers
  690. {
  691. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  692. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  693. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  694. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  695. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  696. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  697. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  698. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  699. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  700. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  701. }
  702. #if TARGET_OS_IPHONE
  703. - (void)applicationWillResignActive:(NSNotification *)notification
  704. {
  705. [self pause];
  706. }
  707. #endif
  708. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  709. {
  710. [self willChangeValueForKey:@"time"];
  711. [self willChangeValueForKey:@"remainingTime"];
  712. [cachedTime release];
  713. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  714. [cachedRemainingTime release];
  715. double currentTime = [[cachedTime numberValue] doubleValue];
  716. double remaining = currentTime / position * (1 - position);
  717. cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
  718. [self didChangeValueForKey:@"remainingTime"];
  719. [self didChangeValueForKey:@"time"];
  720. }
  721. #if !TARGET_OS_IPHONE
  722. - (void)delaySleep
  723. {
  724. UpdateSystemActivity(UsrActivity);
  725. }
  726. #endif
  727. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  728. {
  729. #if !TARGET_OS_IPHONE
  730. // This seems to be the most relevant place to delay sleeping and screen saver.
  731. [self delaySleep];
  732. #endif
  733. [self willChangeValueForKey:@"position"];
  734. position = [newPosition floatValue];
  735. [self didChangeValueForKey:@"position"];
  736. }
  737. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  738. {
  739. [self willChangeValueForKey:@"state"];
  740. cachedState = [newState intValue];
  741. [self didChangeValueForKey:@"state"];
  742. }
  743. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  744. {
  745. [self willChangeValueForKey:@"media"];
  746. if (media != newMedia)
  747. {
  748. [media release];
  749. media = [newMedia retain];
  750. }
  751. [self didChangeValueForKey:@"media"];
  752. }
  753. @end