VLCMediaPlayer.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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)saveVideoSnapshotAt:(NSString *)path withWidth:(NSUInteger)width andHeight:(NSUInteger)height
  317. {
  318. int failure = libvlc_video_take_snapshot(instance, 0, [path UTF8String], width, height);
  319. if (failure)
  320. [[NSException exceptionWithName:@"Can't take a video snapshot" reason:@"No video output" userInfo:nil] raise];
  321. }
  322. - (void)setDeinterlaceFilter:(NSString *)name
  323. {
  324. libvlc_video_set_deinterlace(instance, [name UTF8String]);
  325. }
  326. - (void)setRate:(float)value
  327. {
  328. libvlc_media_player_set_rate(instance, value);
  329. }
  330. - (float)rate
  331. {
  332. return libvlc_media_player_get_rate(instance);
  333. }
  334. - (CGSize)videoSize
  335. {
  336. unsigned height = 0, width = 0;
  337. int failure = libvlc_video_get_size(instance, 0, &width, &height);
  338. if (failure)
  339. [[NSException exceptionWithName:@"Can't get video size" reason:@"No video output" userInfo:nil] raise];
  340. return CGSizeMake(width, height);
  341. }
  342. - (BOOL)hasVideoOut
  343. {
  344. return libvlc_media_player_has_vout(instance);
  345. }
  346. - (float)framesPerSecond
  347. {
  348. return libvlc_media_player_get_fps(instance);
  349. }
  350. - (void)setTime:(VLCTime *)value
  351. {
  352. // Time is managed in seconds, while duration is managed in microseconds
  353. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  354. libvlc_media_player_set_time(instance, value ? [[value numberValue] longLongValue] : 0);
  355. }
  356. - (VLCTime *)time
  357. {
  358. return cachedTime;
  359. }
  360. - (VLCTime *)remainingTime
  361. {
  362. return cachedRemainingTime;
  363. }
  364. - (NSUInteger)fps
  365. {
  366. return libvlc_media_player_get_fps(instance);
  367. }
  368. #pragma mark -
  369. #pragma mark Chapters
  370. - (void)setCurrentChapterIndex:(NSUInteger)value;
  371. {
  372. libvlc_media_player_set_chapter(instance, value);
  373. }
  374. - (NSUInteger)currentChapterIndex
  375. {
  376. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  377. if (count <= 0)
  378. return NSNotFound;
  379. NSUInteger result = libvlc_media_player_get_chapter(instance);
  380. return result;
  381. }
  382. - (void)nextChapter
  383. {
  384. libvlc_media_player_next_chapter(instance);
  385. }
  386. - (void)previousChapter
  387. {
  388. libvlc_media_player_previous_chapter(instance);
  389. }
  390. - (NSArray *)chaptersForTitleIndex:(NSUInteger)title
  391. {
  392. NSInteger count = libvlc_media_player_get_chapter_count(instance);
  393. if (count <= 0)
  394. return [NSArray array];
  395. libvlc_track_description_t *tracks = libvlc_video_get_chapter_description(instance, title);
  396. NSMutableArray *tempArray = [NSMutableArray array];
  397. NSInteger i;
  398. for (i = 0; i < count ; i++)
  399. {
  400. [tempArray addObject:[NSString stringWithUTF8String:tracks->psz_name]];
  401. tracks = tracks->p_next;
  402. }
  403. libvlc_track_description_list_release(tracks);
  404. return [NSArray arrayWithArray:tempArray];
  405. }
  406. #pragma mark -
  407. #pragma mark Titles
  408. - (void)setCurrentTitleIndex:(NSUInteger)value
  409. {
  410. libvlc_media_player_set_title(instance, value);
  411. }
  412. - (NSUInteger)currentTitleIndex
  413. {
  414. NSInteger count = libvlc_media_player_get_title_count(instance);
  415. if (count <= 0)
  416. return NSNotFound;
  417. return libvlc_media_player_get_title(instance);
  418. }
  419. - (NSUInteger)countOfTitles
  420. {
  421. NSUInteger result = libvlc_media_player_get_title_count(instance);
  422. return result;
  423. }
  424. - (NSArray *)titles
  425. {
  426. libvlc_track_description_t *tracks = libvlc_video_get_title_description(instance);
  427. NSMutableArray *tempArray = [NSMutableArray array];
  428. NSInteger i;
  429. for (i = 0; i < [self countOfTitles] ; i++)
  430. {
  431. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  432. tracks = tracks->p_next;
  433. }
  434. libvlc_track_description_list_release(tracks);
  435. return [NSArray arrayWithArray: tempArray];
  436. }
  437. #pragma mark -
  438. #pragma mark Audio tracks
  439. - (void)setCurrentAudioTrackIndex:(NSUInteger)value
  440. {
  441. libvlc_audio_set_track(instance, (int)value);
  442. }
  443. - (NSUInteger)currentAudioTrackIndex
  444. {
  445. NSInteger count = libvlc_audio_get_track_count(instance);
  446. if (count <= 0)
  447. return NSNotFound;
  448. NSUInteger result = libvlc_audio_get_track(instance);
  449. return result;
  450. }
  451. - (NSArray *)audioTracks
  452. {
  453. NSInteger count = libvlc_audio_get_track_count(instance);
  454. if (count <= 0)
  455. return [NSArray array];
  456. libvlc_track_description_t *tracks = libvlc_audio_get_track_description(instance);
  457. NSMutableArray *tempArray = [NSMutableArray array];
  458. NSUInteger i;
  459. for (i = 0; i < count ; i++)
  460. {
  461. [tempArray addObject:[NSString stringWithUTF8String: tracks->psz_name]];
  462. tracks = tracks->p_next;
  463. }
  464. libvlc_track_description_list_release(tracks);
  465. return [NSArray arrayWithArray: tempArray];
  466. }
  467. - (void)setAudioChannel:(NSInteger)value
  468. {
  469. libvlc_audio_set_channel(instance, value);
  470. }
  471. - (NSInteger)audioChannel
  472. {
  473. return libvlc_audio_get_channel(instance);
  474. }
  475. - (void)setMedia:(VLCMedia *)value
  476. {
  477. if (media != value)
  478. {
  479. if (media && [media compare:value] == NSOrderedSame)
  480. return;
  481. [media release];
  482. media = [value retain];
  483. libvlc_media_player_set_media(instance, [media libVLCMediaDescriptor]);
  484. }
  485. }
  486. - (VLCMedia *)media
  487. {
  488. return media;
  489. }
  490. - (BOOL)play
  491. {
  492. libvlc_media_player_play(instance);
  493. return YES;
  494. }
  495. - (void)pause
  496. {
  497. if( [NSThread isMainThread] )
  498. {
  499. /* Hack because we create a dead lock here, when the vout is stopped
  500. * and tries to recontact us on the main thread */
  501. /* FIXME: to do this properly we need to do some locking. We may want
  502. * to move that to libvlc */
  503. [self performSelectorInBackground:@selector(pause) withObject:nil];
  504. return;
  505. }
  506. // Pause the stream
  507. libvlc_media_player_pause(instance);
  508. }
  509. - (void)stop
  510. {
  511. libvlc_media_player_stop(instance);
  512. }
  513. - (void)gotoNextFrame
  514. {
  515. libvlc_media_player_next_frame(instance);
  516. }
  517. - (void)fastForward
  518. {
  519. [self fastForwardAtRate: 2.0];
  520. }
  521. - (void)fastForwardAtRate:(float)rate
  522. {
  523. [self setRate:rate];
  524. }
  525. - (void)rewind
  526. {
  527. [self rewindAtRate: 2.0];
  528. }
  529. - (void)rewindAtRate:(float)rate
  530. {
  531. [self setRate: -rate];
  532. }
  533. - (void)jumpBackward:(NSInteger)interval
  534. {
  535. if( [self isSeekable] )
  536. {
  537. interval = interval * 1000;
  538. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] - interval)]];
  539. }
  540. }
  541. - (void)jumpForward:(NSInteger)interval
  542. {
  543. if( [self isSeekable] )
  544. {
  545. interval = interval * 1000;
  546. [self setTime: [VLCTime timeWithInt: ([[self time] intValue] + interval)]];
  547. }
  548. }
  549. - (void)extraShortJumpBackward
  550. {
  551. [self jumpBackward:3];
  552. }
  553. - (void)extraShortJumpForward
  554. {
  555. [self jumpForward:3];
  556. }
  557. - (void)shortJumpBackward
  558. {
  559. [self jumpBackward:10];
  560. }
  561. - (void)shortJumpForward
  562. {
  563. [self jumpForward:10];
  564. }
  565. - (void)mediumJumpBackward
  566. {
  567. [self jumpBackward:60];
  568. }
  569. - (void)mediumJumpForward
  570. {
  571. [self jumpForward:60];
  572. }
  573. - (void)longJumpBackward
  574. {
  575. [self jumpBackward:300];
  576. }
  577. - (void)longJumpForward
  578. {
  579. [self jumpForward:300];
  580. }
  581. + (NSSet *)keyPathsForValuesAffectingIsPlaying
  582. {
  583. return [NSSet setWithObjects:@"state", nil];
  584. }
  585. - (BOOL)isPlaying
  586. {
  587. VLCMediaPlayerState state = [self state];
  588. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  589. (state == VLCMediaPlayerStatePlaying));
  590. }
  591. - (BOOL)willPlay
  592. {
  593. return libvlc_media_player_will_play(instance);
  594. }
  595. static const VLCMediaPlayerState libvlc_to_local_state[] =
  596. {
  597. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  598. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  599. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  600. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  601. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  602. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  603. [libvlc_Error] = VLCMediaPlayerStateError
  604. };
  605. - (VLCMediaPlayerState)state
  606. {
  607. return cachedState;
  608. }
  609. - (float)position
  610. {
  611. return position;
  612. }
  613. - (void)setPosition:(float)newPosition
  614. {
  615. libvlc_media_player_set_position(instance, newPosition);
  616. }
  617. - (BOOL)isSeekable
  618. {
  619. return libvlc_media_player_is_seekable(instance);
  620. }
  621. - (BOOL)canPause
  622. {
  623. return libvlc_media_player_can_pause(instance);
  624. }
  625. - (void *)libVLCMediaPlayer
  626. {
  627. return instance;
  628. }
  629. @end
  630. @implementation VLCMediaPlayer (Private)
  631. - (id)initWithDrawable:(id)aDrawable
  632. {
  633. if (self = [super init])
  634. {
  635. delegate = nil;
  636. media = nil;
  637. cachedTime = [[VLCTime nullTime] retain];
  638. cachedRemainingTime = [[VLCTime nullTime] retain];
  639. position = 0.0f;
  640. cachedState = VLCMediaPlayerStateStopped;
  641. // Create a media instance, it doesn't matter what library we start off with
  642. // it will change depending on the media descriptor provided to the media
  643. // instance
  644. instance = libvlc_media_player_new([VLCLibrary sharedInstance]);
  645. [self registerObservers];
  646. #if TARGET_OS_IPHONE
  647. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)
  648. name:UIApplicationWillResignActiveNotification object:nil];
  649. #endif
  650. [self setDrawable:aDrawable];
  651. }
  652. return self;
  653. }
  654. - (void)registerObservers
  655. {
  656. // Attach event observers into the media instance
  657. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  658. libvlc_event_attach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  659. libvlc_event_attach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  660. libvlc_event_attach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  661. libvlc_event_attach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  662. libvlc_event_attach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  663. libvlc_event_attach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  664. /* FIXME: We may want to turn that off when none is interested by that */
  665. libvlc_event_attach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  666. libvlc_event_attach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  667. libvlc_event_attach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  668. }
  669. - (void)unregisterObservers
  670. {
  671. libvlc_event_manager_t * p_em = libvlc_media_player_event_manager(instance);
  672. libvlc_event_detach(p_em, libvlc_MediaPlayerPlaying, HandleMediaInstanceStateChanged, self);
  673. libvlc_event_detach(p_em, libvlc_MediaPlayerPaused, HandleMediaInstanceStateChanged, self);
  674. libvlc_event_detach(p_em, libvlc_MediaPlayerEncounteredError, HandleMediaInstanceStateChanged, self);
  675. libvlc_event_detach(p_em, libvlc_MediaPlayerEndReached, HandleMediaInstanceStateChanged, self);
  676. libvlc_event_detach(p_em, libvlc_MediaPlayerOpening, HandleMediaInstanceStateChanged, self);
  677. libvlc_event_detach(p_em, libvlc_MediaPlayerBuffering, HandleMediaInstanceStateChanged, self);
  678. libvlc_event_detach(p_em, libvlc_MediaPlayerPositionChanged, HandleMediaPositionChanged, self);
  679. libvlc_event_detach(p_em, libvlc_MediaPlayerTimeChanged, HandleMediaTimeChanged, self);
  680. libvlc_event_detach(p_em, libvlc_MediaPlayerMediaChanged, HandleMediaPlayerMediaChanged, self);
  681. }
  682. #if TARGET_OS_IPHONE
  683. - (void)applicationWillResignActive:(NSNotification *)notification
  684. {
  685. [self pause];
  686. }
  687. #endif
  688. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  689. {
  690. [self willChangeValueForKey:@"time"];
  691. [self willChangeValueForKey:@"remainingTime"];
  692. [cachedTime release];
  693. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  694. [cachedRemainingTime release];
  695. double currentTime = [[cachedTime numberValue] doubleValue];
  696. double remaining = currentTime / position * (1 - position);
  697. cachedRemainingTime = [[VLCTime timeWithNumber:[NSNumber numberWithDouble:-remaining]] retain];
  698. [self didChangeValueForKey:@"remainingTime"];
  699. [self didChangeValueForKey:@"time"];
  700. }
  701. #if !TARGET_OS_IPHONE
  702. - (void)delaySleep
  703. {
  704. UpdateSystemActivity(UsrActivity);
  705. }
  706. #endif
  707. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  708. {
  709. #if !TARGET_OS_IPHONE
  710. // This seems to be the most relevant place to delay sleeping and screen saver.
  711. [self delaySleep];
  712. #endif
  713. [self willChangeValueForKey:@"position"];
  714. position = [newPosition floatValue];
  715. [self didChangeValueForKey:@"position"];
  716. }
  717. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  718. {
  719. [self willChangeValueForKey:@"state"];
  720. cachedState = [newState intValue];
  721. [self didChangeValueForKey:@"state"];
  722. }
  723. - (void)mediaPlayerMediaChanged:(VLCMedia *)newMedia
  724. {
  725. [self willChangeValueForKey:@"media"];
  726. if (media != newMedia)
  727. {
  728. [media release];
  729. media = [newMedia retain];
  730. }
  731. [self didChangeValueForKey:@"media"];
  732. }
  733. @end