VLCMediaPlayer.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. [media release];
  364. media = [value retain];
  365. libvlc_exception_t ex;
  366. libvlc_exception_init( &ex );
  367. libvlc_media_instance_set_media_descriptor( instance, [media libVLCMediaDescriptor], &ex );
  368. quit_on_exception( &ex );
  369. }
  370. }
  371. - (VLCMedia *)media
  372. {
  373. return media;
  374. }
  375. - (BOOL)play
  376. {
  377. libvlc_exception_t ex;
  378. libvlc_exception_init( &ex );
  379. libvlc_media_instance_play( (libvlc_media_instance_t *)instance, &ex );
  380. quit_on_exception( &ex );
  381. return YES;
  382. }
  383. - (void)pause
  384. {
  385. // Return if there is no media available or if the stream is not paused or
  386. // playing something else
  387. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  388. return;
  389. // Should never get here.
  390. if (!instance)
  391. return;
  392. // Pause the stream
  393. libvlc_exception_t ex;
  394. libvlc_exception_init( &ex );
  395. libvlc_media_instance_pause( (libvlc_media_instance_t *)instance, &ex );
  396. quit_on_exception( &ex );
  397. // TODO: Should we record the time in case the media instance is destroyed
  398. // then rebuilt?
  399. }
  400. - (void)stop
  401. {
  402. // Return if there is no media available or if the system is not in play status
  403. // or pause status.
  404. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  405. return;
  406. // The following is not implemented in the core, should I fix it or just
  407. // compensate?
  408. // libvlc_exception_t ex;
  409. // libvlc_exception_init( &ex );
  410. // libvlc_media_instance_stop((libvlc_media_instance_t *)instance, &ex);
  411. // quit_on_exception( &ex );
  412. // Pause and reposition to the begining of the stream.
  413. [self pause];
  414. [self setTime:0];
  415. // TODO: Should we pause this or destroy the media instance so that it appears as being "stopped"?
  416. }
  417. //- (void)fastForward;
  418. //- (void)fastForwardAtRate:(int)rate;
  419. //- (void)rewind;
  420. //- (void)rewindAtRate:(int)rate;
  421. - (BOOL)isPlaying
  422. {
  423. VLCMediaPlayerState state = [self state];
  424. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  425. (state == VLCMediaPlayerStatePlaying));
  426. }
  427. - (BOOL)willPlay
  428. {
  429. libvlc_exception_t ex;
  430. libvlc_exception_init( &ex );
  431. BOOL ret = libvlc_media_instance_will_play( (libvlc_media_instance_t *)instance, &ex );
  432. if (libvlc_exception_raised(&ex))
  433. {
  434. libvlc_exception_clear(&ex);
  435. return NO;
  436. }
  437. else
  438. return ret;
  439. }
  440. static const VLCMediaPlayerState libvlc_to_local_state[] =
  441. {
  442. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  443. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  444. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  445. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  446. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  447. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  448. [libvlc_Error] = VLCMediaPlayerStateError
  449. };
  450. - (VLCMediaPlayerState)state
  451. {
  452. return cachedState;
  453. }
  454. - (float)position
  455. {
  456. return position;
  457. }
  458. - (void)setPosition:(float)newPosition
  459. {
  460. libvlc_exception_t ex;
  461. libvlc_exception_init( &ex );
  462. libvlc_media_instance_set_position( instance, newPosition, &ex );
  463. quit_on_exception( &ex );
  464. }
  465. - (BOOL)isSeekable
  466. {
  467. libvlc_exception_t ex;
  468. libvlc_exception_init( &ex );
  469. BOOL ret = libvlc_media_instance_is_seekable( instance, &ex );
  470. quit_on_exception( &ex );
  471. return ret;
  472. }
  473. @end
  474. @implementation VLCMediaPlayer (Private)
  475. - (void)registerObservers
  476. {
  477. libvlc_exception_t ex;
  478. libvlc_exception_init( &ex );
  479. // Attach event observers into the media instance
  480. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager( instance, &ex );
  481. libvlc_event_attach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex );
  482. libvlc_event_attach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex );
  483. libvlc_event_attach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex );
  484. /* FIXME: We may want to turn that off when none is interested by that */
  485. libvlc_event_attach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaPositionChanged, self, &ex );
  486. libvlc_event_attach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, &ex );
  487. quit_on_exception( &ex );
  488. }
  489. - (void)unregisterObservers
  490. {
  491. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager( instance, NULL );
  492. libvlc_event_detach( p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL );
  493. libvlc_event_detach( p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL );
  494. libvlc_event_detach( p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL );
  495. libvlc_event_detach( p_em, libvlc_MediaInstancePositionChanged, HandleMediaTimeChanged, self, NULL );
  496. libvlc_event_detach( p_em, libvlc_MediaInstanceTimeChanged, HandleMediaTimeChanged, self, NULL );
  497. }
  498. - (void)mediaPlayerTimeChanged:(NSNumber *)newTime
  499. {
  500. [self willChangeValueForKey:@"time"];
  501. [cachedTime release];
  502. cachedTime = [[VLCTime timeWithNumber:newTime] retain];
  503. [self didChangeValueForKey:@"time"];
  504. }
  505. - (void)mediaPlayerPositionChanged:(NSNumber *)newPosition
  506. {
  507. if( [newPosition floatValue] - position < 0.005 && position - [newPosition floatValue] < 0.005 )
  508. return; /* Forget that, this is too much precision for our uses */
  509. [self willChangeValueForKey:@"position"];
  510. position = ((float)((int)([newPosition floatValue]*1000)))/1000.;
  511. [self didChangeValueForKey:@"position"];
  512. }
  513. - (void)mediaPlayerStateChanged:(NSNumber *)newState
  514. {
  515. [self willChangeValueForKey:@"state"];
  516. cachedState = [newState intValue];
  517. [self didChangeValueForKey:@"state"];
  518. }
  519. @end