VLCMediaPlayer.m 16 KB

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