VLCMediaPlayer.m 23 KB

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