VLCMediaPlayer.m 16 KB

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