VLCMediaPlayer.m 18 KB

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