VLCMediaPlayer.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. NSLog(@"time!");
  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. - (void)registerObservers;
  99. - (void)unregisterObservers;
  100. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime;
  101. - (void)mediaPlayerPositionChanged:(NSNumber *)newTime;
  102. - (void)mediaPlayerStateChanged:(NSNumber *)newState;
  103. @end
  104. @implementation VLCMediaPlayer
  105. + (void)initialize {
  106. [self setKeys:[NSArray arrayWithObject:@"state"] triggerChangeNotificationsForDependentKey:@"playing"];
  107. }
  108. - (id)init
  109. {
  110. return [self initWithVideoView:nil];
  111. }
  112. - (id)initWithVideoView:(VLCVideoView *)aVideoView
  113. {
  114. if (self = [super init])
  115. {
  116. delegate = nil;
  117. media = nil;
  118. cachedTime = [[VLCTime nullTime] retain];
  119. position = 0.0f;
  120. cachedState = VLCMediaPlayerStateStopped;
  121. // Create a media instance, it doesn't matter what library we start off with
  122. // it will change depending on the media descriptor provided to the media
  123. // instance
  124. libvlc_exception_t ex;
  125. libvlc_exception_init( &ex );
  126. instance = (void *)libvlc_media_instance_new([VLCLibrary sharedInstance], &ex);
  127. quit_on_exception( &ex );
  128. [self registerObservers];
  129. [self setVideoView:aVideoView];
  130. }
  131. return self;
  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 *)value
  167. {
  168. videoView = value;
  169. // Make sure that this instance has been associated with the drawing canvas.
  170. libvlc_exception_t ex;
  171. libvlc_exception_init( &ex );
  172. libvlc_media_instance_set_drawable ((libvlc_media_instance_t *)instance,
  173. (libvlc_drawable_t)videoView,
  174. &ex);
  175. quit_on_exception( &ex );
  176. }
  177. - (VLCVideoView *)videoView
  178. {
  179. return videoView;
  180. }
  181. - (void)setFullscreen:(BOOL)value
  182. {
  183. libvlc_set_fullscreen(instance, value, NULL);
  184. }
  185. - (BOOL)fullscreen
  186. {
  187. libvlc_exception_t ex;
  188. libvlc_exception_init( &ex );
  189. int result = libvlc_get_fullscreen( instance, &ex );
  190. quit_on_exception( &ex );
  191. return result;
  192. }
  193. - (void)setVideoAspectRatio:(char *)value
  194. {
  195. libvlc_video_set_aspect_ratio( instance, value, NULL );
  196. }
  197. - (char *)videoAspectRatio
  198. {
  199. libvlc_exception_t ex;
  200. libvlc_exception_init( &ex );
  201. char *result = libvlc_video_get_aspect_ratio( instance, &ex );
  202. quit_on_exception( &ex );
  203. return result;
  204. }
  205. - (void)setVideoSubTitles:(int)value
  206. {
  207. libvlc_video_set_spu( instance, value, NULL );
  208. }
  209. - (int)videoSubTitles
  210. {
  211. libvlc_exception_t ex;
  212. libvlc_exception_init( &ex );
  213. int result = libvlc_video_get_spu( instance, &ex );
  214. quit_on_exception( &ex );
  215. return result;
  216. }
  217. - (void)setVideoCropGeometry:(char *)value
  218. {
  219. libvlc_video_set_crop_geometry( instance, value, NULL );
  220. }
  221. - (char *)videoCropGeometry
  222. {
  223. libvlc_exception_t ex;
  224. libvlc_exception_init( &ex );
  225. char *result = libvlc_video_get_crop_geometry( instance, &ex );
  226. quit_on_exception( &ex );
  227. return result;
  228. }
  229. - (void)setVideoTeleText:(int)value
  230. {
  231. libvlc_video_set_teletext( instance, value, NULL );
  232. }
  233. - (int)videoTeleText
  234. {
  235. libvlc_exception_t ex;
  236. libvlc_exception_init( &ex );
  237. int result = libvlc_video_get_teletext( instance, &ex );
  238. quit_on_exception( &ex );
  239. return result;
  240. }
  241. - (void)setRate:(int)value
  242. {
  243. libvlc_media_instance_set_rate( instance, value, NULL );
  244. }
  245. - (int)rate
  246. {
  247. libvlc_exception_t ex;
  248. libvlc_exception_init( &ex );
  249. float result = libvlc_media_instance_get_rate( instance, &ex );
  250. quit_on_exception( &ex );
  251. return result;
  252. }
  253. - (NSSize)videoSize
  254. {
  255. libvlc_exception_t ex;
  256. libvlc_exception_init( &ex );
  257. NSSize result = NSMakeSize(libvlc_video_get_height((libvlc_media_instance_t *)instance, &ex),
  258. libvlc_video_get_width((libvlc_media_instance_t *)instance, &ex));
  259. quit_on_exception( &ex );
  260. return result;
  261. }
  262. - (BOOL)hasVideoOut
  263. {
  264. libvlc_exception_t ex;
  265. libvlc_exception_init( &ex );
  266. BOOL result = libvlc_media_instance_has_vout((libvlc_media_instance_t *)instance, &ex);
  267. if (libvlc_exception_raised( &ex ))
  268. {
  269. libvlc_exception_clear( &ex );
  270. return NO;
  271. }
  272. else
  273. return result;
  274. }
  275. - (float)framesPerSecond
  276. {
  277. libvlc_exception_t ex;
  278. libvlc_exception_init( &ex );
  279. float result = libvlc_media_instance_get_fps( (libvlc_media_instance_t *)instance, &ex );
  280. quit_on_exception( &ex );
  281. return result;
  282. }
  283. - (void)setTime:(VLCTime *)value
  284. {
  285. libvlc_exception_t ex;
  286. libvlc_exception_init( &ex );
  287. // Time is managed in seconds, while duration is managed in microseconds
  288. // TODO: Redo VLCTime to provide value numberAsMilliseconds, numberAsMicroseconds, numberAsSeconds, numberAsMinutes, numberAsHours
  289. libvlc_media_instance_set_time( (libvlc_media_instance_t *)instance,
  290. (value ? [[value numberValue] longLongValue] / 1000 : 0),
  291. &ex );
  292. quit_on_exception( &ex );
  293. }
  294. - (VLCTime *)time
  295. {
  296. return cachedTime;
  297. }
  298. - (void)setChapter:(int)value;
  299. {
  300. libvlc_media_instance_set_chapter( instance, value, NULL );
  301. }
  302. - (int)chapter
  303. {
  304. libvlc_exception_t ex;
  305. libvlc_exception_init( &ex );
  306. int result = libvlc_media_instance_get_chapter( instance, &ex );
  307. quit_on_exception( &ex );
  308. return result;
  309. }
  310. - (int)countOfChapters
  311. {
  312. libvlc_exception_t ex;
  313. libvlc_exception_init( &ex );
  314. int result = libvlc_media_instance_get_chapter_count( instance, &ex );
  315. quit_on_exception( &ex );
  316. return result;
  317. }
  318. - (void)setAudioTrack:(int)value
  319. {
  320. libvlc_audio_set_track( instance, value, NULL );
  321. }
  322. - (int)audioTrack
  323. {
  324. libvlc_exception_t ex;
  325. libvlc_exception_init( &ex );
  326. int result = libvlc_audio_get_track( instance, &ex );
  327. quit_on_exception( &ex );
  328. return result;
  329. }
  330. - (int)countOfAudioTracks
  331. {
  332. libvlc_exception_t ex;
  333. libvlc_exception_init( &ex );
  334. int result = libvlc_audio_get_track_count( instance, &ex );
  335. quit_on_exception( &ex );
  336. return result;
  337. }
  338. - (void)setAudioChannel:(int)value
  339. {
  340. libvlc_audio_set_channel( instance, value, NULL );
  341. }
  342. - (int)audioChannel
  343. {
  344. libvlc_exception_t ex;
  345. libvlc_exception_init( &ex );
  346. int result = libvlc_audio_get_channel( instance, &ex );
  347. quit_on_exception( &ex );
  348. return result;
  349. }
  350. - (void)setMedia:(VLCMedia *)value
  351. {
  352. // We only know how to play media files...not media resources with subitems
  353. if (media != value && [media subitems] == nil)
  354. {
  355. if (media && [media compare:value] == NSOrderedSame)
  356. return;
  357. BOOL wasPlaying;
  358. if (wasPlaying = [self isPlaying])
  359. {
  360. [self pause];
  361. // // TODO: Should we wait until it stops playing?
  362. // while ([self isPlaying])
  363. // usleep(1000);
  364. }
  365. [self willChangeValueForKey:@"media"];
  366. [media release];
  367. media = [value retain];
  368. [self didChangeValueForKey:@"media"];
  369. libvlc_exception_t ex;
  370. libvlc_exception_init( &ex );
  371. libvlc_media_instance_set_media_descriptor( instance, [media libVLCMediaDescriptor], &ex );
  372. quit_on_exception( &ex );
  373. if (media) {
  374. if (wasPlaying)
  375. [self play];
  376. }
  377. }
  378. }
  379. - (VLCMedia *)media
  380. {
  381. return media;
  382. }
  383. - (BOOL)play
  384. {
  385. // Return if there is no media available or if the stream is already playing something
  386. if (!media || [self isPlaying])
  387. return [self isPlaying];
  388. libvlc_exception_t ex;
  389. libvlc_exception_init( &ex );
  390. libvlc_media_instance_play( (libvlc_media_instance_t *)instance, &ex );
  391. quit_on_exception( &ex );
  392. return YES;
  393. }
  394. - (void)pause
  395. {
  396. // Return if there is no media available or if the stream is not paused or
  397. // playing something else
  398. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  399. return;
  400. // Should never get here.
  401. if (!instance)
  402. return;
  403. // Pause the stream
  404. libvlc_exception_t ex;
  405. libvlc_exception_init( &ex );
  406. libvlc_media_instance_pause( (libvlc_media_instance_t *)instance, &ex );
  407. quit_on_exception( &ex );
  408. // TODO: Should we record the time in case the media instance is destroyed
  409. // then rebuilt?
  410. }
  411. - (void)stop
  412. {
  413. // Return if there is no media available or if the system is not in play status
  414. // or pause status.
  415. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  416. return;
  417. // The following is not implemented in the core, should I fix it or just
  418. // compensate?
  419. // libvlc_exception_t ex;
  420. // libvlc_exception_init( &ex );
  421. // libvlc_media_instance_stop((libvlc_media_instance_t *)instance, &ex);
  422. // quit_on_exception( &ex );
  423. // Pause and reposition to the begining of the stream.
  424. [self pause];
  425. [self setTime:0];
  426. // TODO: Should we pause this or destroy the media instance so that it appears as being "stopped"?
  427. }
  428. //- (void)fastForward;
  429. //- (void)fastForwardAtRate:(int)rate;
  430. //- (void)rewind;
  431. //- (void)rewindAtRate:(int)rate;
  432. - (BOOL)isPlaying
  433. {
  434. VLCMediaPlayerState state = [self state];
  435. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  436. (state == VLCMediaPlayerStatePlaying));
  437. }
  438. - (BOOL)willPlay
  439. {
  440. libvlc_exception_t ex;
  441. libvlc_exception_init( &ex );
  442. BOOL ret = libvlc_media_instance_will_play( (libvlc_media_instance_t *)instance, &ex );
  443. if (libvlc_exception_raised(&ex))
  444. {
  445. libvlc_exception_clear(&ex);
  446. return NO;
  447. }
  448. else
  449. return ret;
  450. }
  451. static const VLCMediaPlayerState libvlc_to_local_state[] =
  452. {
  453. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  454. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  455. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  456. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  457. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  458. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  459. [libvlc_Error] = VLCMediaPlayerStateError
  460. };
  461. - (VLCMediaPlayerState)state
  462. {
  463. return cachedState;
  464. }
  465. - (float)position
  466. {
  467. return position;
  468. }
  469. - (void)setPosition:(float)newPosition
  470. {
  471. libvlc_exception_t ex;
  472. libvlc_exception_init( &ex );
  473. libvlc_media_instance_set_position( instance, newPosition, &ex );
  474. quit_on_exception( &ex );
  475. }
  476. @end
  477. @implementation VLCMediaPlayer (Private)
  478. - (void)registerObservers
  479. {
  480. libvlc_exception_t ex;
  481. libvlc_exception_init( &ex );
  482. // Attach event observers into the media instance
  483. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager( instance, &ex );
  484. libvlc_event_attach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex );
  485. libvlc_event_attach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex );
  486. libvlc_event_attach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex );
  487. /* FIXME: We may want to turn that off when none is interested by that */
  488. libvlc_event_attach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, &ex );
  489. libvlc_event_attach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, &ex );
  490. quit_on_exception( &ex );
  491. }
  492. - (void)unregisterObservers
  493. {
  494. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager( instance, NULL );
  495. libvlc_event_detach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL );
  496. libvlc_event_detach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL );
  497. libvlc_event_detach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL );
  498. libvlc_event_detach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaTimeChanged, self, NULL );
  499. libvlc_event_detach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, NULL );
  500. }
  501. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  502. {
  503. [self willChangeValueForKey:@"time"];
  504. [cachedTime release];
  505. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  506. [self didChangeValueForKey:@"time"];
  507. }
  508. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  509. {
  510. if( [newPosition floatValue] - position < 0.005 && position - [newPosition floatValue] < 0.005 )
  511. return; /* Forget that, this is too much precision for our uses */
  512. [self willChangeValueForKey:@"position"];
  513. position = ((float)((int)([newPosition floatValue]*1000)))/1000.;
  514. [self didChangeValueForKey:@"position"];
  515. }
  516. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  517. {
  518. [self willChangeValueForKey:@"state"];
  519. cachedState = [newState intValue];
  520. [self didChangeValueForKey:@"state"];
  521. }
  522. @end