VLCMediaPlayer.m 19 KB

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