VLCMediaPlayer.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*****************************************************************************
  2. * VLCMediaPlayer.m: VLC.framework VLCMediaPlayer implementation
  3. *****************************************************************************
  4. * Copyright (C) 2007 Pierre d'Herbemont
  5. * Copyright (C) 2007 the VideoLAN team
  6. * $Id$
  7. *
  8. * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
  9. * Faustion Osuna <enrique.osuna # gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  24. *****************************************************************************/
  25. #import "VLCLibrary.h"
  26. #import "VLCMediaPlayer.h"
  27. #import "VLCEventManager.h"
  28. #import "VLCLibVLCBridging.h"
  29. #import "VLCVideoView.h"
  30. #include <vlc/vlc.h>
  31. /* Notification Messages */
  32. NSString * VLCMediaPlayerTimeChanged = @"VLCMediaPlayerTimeChanged";
  33. NSString * VLCMediaPlayerStateChanged = @"VLCMediaPlayerStateChanged";
  34. NSString * VLCMediaPlayerStateToString(VLCMediaPlayerState state)
  35. {
  36. static NSString * stateToStrings[] = {
  37. [VLCMediaPlayerStateStopped] = @"VLCMediaPlayerStateStopped",
  38. [VLCMediaPlayerStateOpening] = @"VLCMediaPlayerStateOpening",
  39. [VLCMediaPlayerStateBuffering] = @"VLCMediaPlayerStateBuffering",
  40. [VLCMediaPlayerStateEnded] = @"VLCMediaPlayerStateEnded",
  41. [VLCMediaPlayerStateError] = @"VLCMediaPlayerStateError",
  42. [VLCMediaPlayerStatePlaying] = @"VLCMediaPlayerStatePlaying",
  43. [VLCMediaPlayerStatePaused] = @"VLCMediaPlayerStatePaused"
  44. };
  45. return stateToStrings[state];
  46. }
  47. /* libvlc event callback */
  48. static void HandleMediaInstanceVolumeChanged(const libvlc_event_t * event, void * self)
  49. {
  50. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  51. withDelegateMethod:@selector(mediaPlayerVolumeChanged:)
  52. withNotificationName:VLCMediaPlayerVolumeChanged];
  53. }
  54. static void HandleMediaTimeChanged(const libvlc_event_t * event, void * self)
  55. {
  56. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  57. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  58. withMethod:@selector(mediaPlayerTimeChanged:)
  59. withArgumentAsObject:[NSNumber numberWithLongLong:event->u.media_instance_time_changed.new_time]];
  60. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  61. withDelegateMethod:@selector(mediaPlayerTimeChanged:)
  62. withNotificationName:VLCMediaPlayerTimeChanged];
  63. [pool release];
  64. }
  65. static void HandleMediaPositionChanged(const libvlc_event_t * event, void * self)
  66. {
  67. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  68. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  69. withMethod:@selector(mediaPlayerPositionChanged:)
  70. withArgumentAsObject:[NSNumber numberWithFloat:event->u.media_instance_position_changed.new_position]];
  71. [pool release];
  72. }
  73. static void HandleMediaInstanceStateChanged(const libvlc_event_t * event, void * self)
  74. {
  75. VLCMediaPlayerState newState;
  76. if( event->type == libvlc_MediaInstancePlayed )
  77. newState = VLCMediaPlayerStatePlaying;
  78. else if( event->type == libvlc_MediaInstancePaused )
  79. newState = VLCMediaPlayerStatePaused;
  80. else if( event->type == libvlc_MediaInstanceReachedEnd )
  81. newState = VLCMediaPlayerStateStopped;
  82. else
  83. {
  84. NSLog(@"%s: Unknown event", __FUNCTION__);
  85. return;
  86. }
  87. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  88. [[VLCEventManager sharedManager] callOnMainThreadObject:self
  89. withMethod:@selector(mediaPlayerStateChanged:)
  90. withArgumentAsObject:[NSNumber numberWithInt:newState]];
  91. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  92. withDelegateMethod:@selector(mediaPlayerStateChanged:)
  93. withNotificationName:VLCMediaPlayerStateChanged];
  94. [pool release];
  95. }
  96. // TODO: Documentation
  97. @interface VLCMediaPlayer (Private)
  98. - (id)initWithDrawable:(id)aDrawable;
  99. - (void)setDrawable:(id)aDrawable;
  100. - (void)registerObservers;
  101. - (void)unregisterObservers;
  102. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  103. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  104. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  105. @end
  106. @implementation VLCMediaPlayer
  107. /* Bindings */
  108. + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
  109. {
  110. static NSDictionary * dict = nil;
  111. if( !dict )
  112. {
  113. dict = [[NSDictionary dictionaryWithObjectsAndKeys:
  114. [NSSet setWithObject:@"state"], @"playing",
  115. [NSSet setWithObjects:@"state", @"media", nil], @"seekable",
  116. nil] retain];
  117. }
  118. return [dict objectForKey: key];
  119. }
  120. /* Contructor */
  121. - (id)init
  122. {
  123. return [self initWithDrawable:nil];
  124. }
  125. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  126. {
  127. return [self initWithDrawable: aVideoView];
  128. }
  129. - (id)initWithVideoLayer:(VLCVideoLayer *)aVideoLayer
  130. {
  131. return [self initWithDrawable: aVideoLayer];
  132. }
  133. - (void)release
  134. {
  135. @synchronized(self)
  136. {
  137. if([self retainCount] <= 1)
  138. {
  139. /* We must make sure we won't receive new event after an upcoming dealloc
  140. * We also may receive a -retain in some event callback that may occcur
  141. * Before libvlc_event_detach. So this can't happen in dealloc */
  142. [self unregisterObservers];
  143. }
  144. [super release];
  145. }
  146. }
  147. - (void)dealloc
  148. {
  149. // Always get rid of the delegate first so we can stop sending messages to it
  150. // TODO: Should we tell the delegate that we're shutting down?
  151. delegate = nil;
  152. libvlc_media_instance_release((libvlc_media_instance_t *)instance);
  153. // Get rid of everything else
  154. [media release];
  155. [cachedTime release];
  156. [super dealloc];
  157. }
  158. - (void)setDelegate:(id)value
  159. {
  160. delegate = value;
  161. }
  162. - (id)delegate
  163. {
  164. return delegate;
  165. }
  166. - (void)setVideoView:(VLCVideoView *)aVideoView
  167. {
  168. [self setDrawable: aVideoView];
  169. }
  170. - (void)setVideoLayer:(VLCVideoLayer *)aVideoLayer
  171. {
  172. [self setDrawable: aVideoLayer];
  173. }
  174. - (void)setFullscreen:(BOOL)value
  175. {
  176. libvlc_set_fullscreen(instance, value, NULL);
  177. }
  178. - (BOOL)fullscreen
  179. {
  180. libvlc_exception_t ex;
  181. libvlc_exception_init( &ex );
  182. int result = libvlc_get_fullscreen( instance, &ex );
  183. catch_exception( &ex );
  184. return result;
  185. }
  186. - (void)setVideoAspectRatio:(char *)value
  187. {
  188. libvlc_video_set_aspect_ratio( instance, value, NULL );
  189. }
  190. - (char *)videoAspectRatio
  191. {
  192. libvlc_exception_t ex;
  193. libvlc_exception_init( &ex );
  194. char * result = libvlc_video_get_aspect_ratio( instance, &ex );
  195. catch_exception( &ex );
  196. return result;
  197. }
  198. - (void)setVideoSubTitles:(int)value
  199. {
  200. libvlc_video_set_spu( instance, value, NULL );
  201. }
  202. - (int)videoSubTitles
  203. {
  204. libvlc_exception_t ex;
  205. libvlc_exception_init( &ex );
  206. int result = libvlc_video_get_spu( instance, &ex );
  207. catch_exception( &ex );
  208. return result;
  209. }
  210. - (void)setVideoCropGeometry:(char *)value
  211. {
  212. libvlc_video_set_crop_geometry( instance, value, NULL );
  213. }
  214. - (char *)videoCropGeometry
  215. {
  216. libvlc_exception_t ex;
  217. libvlc_exception_init( &ex );
  218. char * result = libvlc_video_get_crop_geometry( instance, &ex );
  219. catch_exception( &ex );
  220. return result;
  221. }
  222. - (void)setVideoTeleText:(int)value
  223. {
  224. libvlc_video_set_teletext( instance, value, NULL );
  225. }
  226. - (int)videoTeleText
  227. {
  228. libvlc_exception_t ex;
  229. libvlc_exception_init( &ex );
  230. int result = libvlc_video_get_teletext( instance, &ex );
  231. catch_exception( &ex );
  232. return result;
  233. }
  234. - (void)setRate:(int)value
  235. {
  236. libvlc_media_instance_set_rate( instance, value, NULL );
  237. }
  238. - (int)rate
  239. {
  240. libvlc_exception_t ex;
  241. libvlc_exception_init( &ex );
  242. float result = libvlc_media_instance_get_rate( instance, &ex );
  243. catch_exception( &ex );
  244. return result;
  245. }
  246. - (NSSize)videoSize
  247. {
  248. libvlc_exception_t ex;
  249. libvlc_exception_init( &ex );
  250. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_instance_t *)instance, &ex),
  251. libvlc_video_get_width((libvlc_media_instance_t *)instance, &ex));
  252. catch_exception( &ex );
  253. return result;
  254. }
  255. - (BOOL)hasVideoOut
  256. {
  257. libvlc_exception_t ex;
  258. libvlc_exception_init( &ex );
  259. BOOL result = libvlc_media_instance_has_vout((libvlc_media_instance_t *)instance, &ex);
  260. if (libvlc_exception_raised( &ex ))
  261. {
  262. libvlc_exception_clear( &ex );
  263. return NO;
  264. }
  265. else
  266. return result;
  267. }
  268. - (float)framesPerSecond
  269. {
  270. libvlc_exception_t ex;
  271. libvlc_exception_init( &ex );
  272. float result = libvlc_media_instance_get_fps( (libvlc_media_instance_t *)instance, &ex );
  273. catch_exception( &ex );
  274. return result;
  275. }
  276. - (void)setTime:(VLCTime *)value
  277. {
  278. libvlc_exception_t ex;
  279. libvlc_exception_init( &ex );
  280. // Time is managed in seconds, while duration is managed in microseconds
  281. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  282. libvlc_media_instance_set_time( (libvlc_media_instance_t *)instance,
  283. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  284. &ex );
  285. catch_exception( &ex );
  286. }
  287. - (VLCTime *)time
  288. {
  289. return cachedTime;
  290. }
  291. - (void)setChapter:(int)value;
  292. {
  293. libvlc_media_instance_set_chapter( instance, value, NULL );
  294. }
  295. - (int)chapter
  296. {
  297. libvlc_exception_t ex;
  298. libvlc_exception_init( &ex );
  299. int result = libvlc_media_instance_get_chapter( instance, &ex );
  300. catch_exception( &ex );
  301. return result;
  302. }
  303. - (int)countOfChapters
  304. {
  305. libvlc_exception_t ex;
  306. libvlc_exception_init( &ex );
  307. int result = libvlc_media_instance_get_chapter_count( instance, &ex );
  308. catch_exception( &ex );
  309. return result;
  310. }
  311. - (void)setAudioTrack:(int)value
  312. {
  313. libvlc_audio_set_track( instance, value, NULL );
  314. }
  315. - (int)audioTrack
  316. {
  317. libvlc_exception_t ex;
  318. libvlc_exception_init( &ex );
  319. int result = libvlc_audio_get_track( instance, &ex );
  320. catch_exception( &ex );
  321. return result;
  322. }
  323. - (int)countOfAudioTracks
  324. {
  325. libvlc_exception_t ex;
  326. libvlc_exception_init( &ex );
  327. int result = libvlc_audio_get_track_count( instance, &ex );
  328. catch_exception( &ex );
  329. return result;
  330. }
  331. - (void)setAudioChannel:(int)value
  332. {
  333. libvlc_audio_set_channel( instance, value, NULL );
  334. }
  335. - (int)audioChannel
  336. {
  337. libvlc_exception_t ex;
  338. libvlc_exception_init( &ex );
  339. int result = libvlc_audio_get_channel( instance, &ex );
  340. catch_exception( &ex );
  341. return result;
  342. }
  343. - (void)setMedia:(VLCMedia *)value
  344. {
  345. // We only know how to play media files...not media resources with subitems
  346. if (media != value && [media subitems] == nil)
  347. {
  348. if (media && [media compare:value] == NSOrderedSame)
  349. return;
  350. BOOL wasPlaying;
  351. if (wasPlaying = [self isPlaying])
  352. {
  353. [self pause];
  354. // // TODO: Should we wait until it stops playing?
  355. // while ([self isPlaying])
  356. // usleep(1000);
  357. }
  358. [media release];
  359. media = [value retain];
  360. libvlc_exception_t ex;
  361. libvlc_exception_init( &ex );
  362. libvlc_media_instance_set_media_descriptor( instance, [media libVLCMediaDescriptor], &ex );
  363. catch_exception( &ex );
  364. }
  365. }
  366. - (VLCMedia *)media
  367. {
  368. return media;
  369. }
  370. - (BOOL)play
  371. {
  372. libvlc_exception_t ex;
  373. libvlc_exception_init( &ex );
  374. libvlc_media_instance_play( (libvlc_media_instance_t *)instance, &ex );
  375. catch_exception( &ex );
  376. return YES;
  377. }
  378. - (void)pause
  379. {
  380. // Return if there is no media available or if the stream is not paused or
  381. // playing something else
  382. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  383. return;
  384. // Should never get here.
  385. if (!instance)
  386. return;
  387. // Pause the stream
  388. libvlc_exception_t ex;
  389. libvlc_exception_init( &ex );
  390. libvlc_media_instance_pause( (libvlc_media_instance_t *)instance, &ex );
  391. catch_exception( &ex );
  392. // TODO: Should we record the time in case the media instance is destroyed
  393. // then rebuilt?
  394. }
  395. - (void)stop
  396. {
  397. // Return if there is no media available or if the system is not in play status
  398. // or pause status.
  399. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  400. return;
  401. // The following is not implemented in the core, should I fix it or just
  402. // compensate?
  403. // libvlc_exception_t ex;
  404. // libvlc_exception_init( &ex );
  405. // libvlc_media_instance_stop((libvlc_media_instance_t *)instance, &ex);
  406. // catch_exception( &ex );
  407. // Pause and reposition to the begining of the stream.
  408. [self pause];
  409. [self setTime:0];
  410. // TODO: Should we pause this or destroy the media instance so that it appears as being "stopped"?
  411. }
  412. //- (void)fastForward;
  413. //- (void)fastForwardAtRate:(int)rate;
  414. //- (void)rewind;
  415. //- (void)rewindAtRate:(int)rate;
  416. - (BOOL)isPlaying
  417. {
  418. VLCMediaPlayerState state = [self state];
  419. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  420. (state == VLCMediaPlayerStatePlaying));
  421. }
  422. - (BOOL)willPlay
  423. {
  424. libvlc_exception_t ex;
  425. libvlc_exception_init( &ex );
  426. BOOL ret = libvlc_media_instance_will_play( (libvlc_media_instance_t *)instance, &ex );
  427. if (libvlc_exception_raised(&ex))
  428. {
  429. libvlc_exception_clear(&ex);
  430. return NO;
  431. }
  432. else
  433. return ret;
  434. }
  435. static const VLCMediaPlayerState libvlc_to_local_state[] =
  436. {
  437. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  438. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  439. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  440. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  441. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  442. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  443. [libvlc_Error] = VLCMediaPlayerStateError
  444. };
  445. - (VLCMediaPlayerState)state
  446. {
  447. return cachedState;
  448. }
  449. - (float)position
  450. {
  451. return position;
  452. }
  453. - (void)setPosition:(float)newPosition
  454. {
  455. libvlc_exception_t ex;
  456. libvlc_exception_init( &ex );
  457. libvlc_media_instance_set_position( instance, newPosition, &ex );
  458. catch_exception( &ex );
  459. }
  460. - (BOOL)isSeekable
  461. {
  462. libvlc_exception_t ex;
  463. libvlc_exception_init( &ex );
  464. BOOL ret = libvlc_media_instance_is_seekable( instance, &ex );
  465. catch_exception( &ex );
  466. return ret;
  467. }
  468. @end
  469. @implementation VLCMediaPlayer (Private)
  470. - (id)initWithDrawable:(id)aDrawable
  471. {
  472. if (self = [super init])
  473. {
  474. delegate = nil;
  475. media = nil;
  476. cachedTime = [[VLCTime nullTime] retain];
  477. position = 0.0f;
  478. cachedState = VLCMediaPlayerStateStopped;
  479. // Create a media instance, it doesn't matter what library we start off with
  480. // it will change depending on the media descriptor provided to the media
  481. // instance
  482. libvlc_exception_t ex;
  483. libvlc_exception_init( &ex );
  484. instance = (void *)libvlc_media_instance_new([VLCLibrary sharedInstance], &ex);
  485. catch_exception( &ex );
  486. [self registerObservers];
  487. [self setDrawable:aDrawable];
  488. }
  489. return self;
  490. }
  491. - (void)setDrawable:(id)aDrawable
  492. {
  493. // Make sure that this instance has been associated with the drawing canvas.
  494. libvlc_exception_t ex;
  495. libvlc_exception_init( &ex );
  496. libvlc_media_instance_set_drawable ((libvlc_media_instance_t *)instance,
  497. (libvlc_drawable_t)aDrawable,
  498. &ex);
  499. catch_exception( &ex );
  500. }
  501. - (void)registerObservers
  502. {
  503. libvlc_exception_t ex;
  504. libvlc_exception_init( &ex );
  505. // Attach event observers into the media instance
  506. libvlc_event_manager_t * p_em = libvlc_media_instance_event_manager( instance, &ex );
  507. libvlc_event_attach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex );
  508. libvlc_event_attach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex );
  509. libvlc_event_attach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex );
  510. /* FIXME: We may want to turn that off when none is interested by that */
  511. libvlc_event_attach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, &ex );
  512. libvlc_event_attach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, &ex );
  513. catch_exception( &ex );
  514. }
  515. - (void)unregisterObservers
  516. {
  517. libvlc_event_manager_t * p_em = libvlc_media_instance_event_manager( instance, NULL );
  518. libvlc_event_detach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL );
  519. libvlc_event_detach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL );
  520. libvlc_event_detach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL );
  521. //libvlc_event_detach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaTimeChanged, self, NULL );
  522. libvlc_event_detach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, NULL );
  523. }
  524. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  525. {
  526. [self willChangeValueForKey:@"time"];
  527. [cachedTime release];
  528. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  529. [self didChangeValueForKey:@"time"];
  530. }
  531. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  532. {
  533. if( [newPosition floatValue] - position < 0.005 && position - [newPosition floatValue] < 0.005 )
  534. return; /* Forget that, this is too much precision for our uses */
  535. [self willChangeValueForKey:@"position"];
  536. position = ((float)((int)([newPosition floatValue]*1000)))/1000.;
  537. [self didChangeValueForKey:@"position"];
  538. }
  539. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  540. {
  541. [self willChangeValueForKey:@"state"];
  542. cachedState = [newState intValue];
  543. [self didChangeValueForKey:@"state"];
  544. }
  545. @end