VLCMediaPlayer.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 (PrivateAPI)
  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)setAudioTrack:(int)value
  259. {
  260. libvlc_audio_set_track(instance, value, NULL);
  261. }
  262. - (int)audioTrack
  263. {
  264. libvlc_exception_t ex;
  265. libvlc_exception_init(&ex);
  266. int result = libvlc_audio_get_track(instance, &ex);
  267. quit_on_exception(&ex);
  268. return result;
  269. }
  270. - (void)setAudioChannel:(int)value
  271. {
  272. libvlc_audio_set_channel(instance, value, NULL);
  273. }
  274. - (int)audioChannel
  275. {
  276. libvlc_exception_t ex;
  277. libvlc_exception_init(&ex);
  278. int result = libvlc_audio_get_channel(instance, &ex);
  279. quit_on_exception(&ex);
  280. return result;
  281. }
  282. - (void)setMedia:(VLCMedia *)value
  283. {
  284. // We only know how to play media files...not media resources with subitems
  285. if (media != value && [media subitems] == nil)
  286. {
  287. if (media && [media compare:value] == NSOrderedSame)
  288. return;
  289. BOOL wasPlaying;
  290. if (wasPlaying = [self isPlaying])
  291. {
  292. [self pause];
  293. // // TODO: Should we wait until it stops playing?
  294. // while ([self isPlaying])
  295. // usleep(1000);
  296. }
  297. [self willChangeValueForKey:@"media"];
  298. [media release];
  299. media = [value retain];
  300. [self didChangeValueForKey:@"media"];
  301. libvlc_exception_t ex;
  302. libvlc_exception_init(&ex);
  303. libvlc_media_instance_set_media_descriptor(instance, [media libVLCMediaDescriptor], &ex);
  304. quit_on_exception(&ex);
  305. if (media) {
  306. if (wasPlaying)
  307. [self play];
  308. }
  309. }
  310. }
  311. - (VLCMedia *)media
  312. {
  313. return media;
  314. }
  315. - (BOOL)play
  316. {
  317. // Return if there is no media available or if the stream is already playing something
  318. if (!media || [self isPlaying])
  319. return [self isPlaying];
  320. libvlc_exception_t ex;
  321. libvlc_exception_init(&ex);
  322. libvlc_media_instance_play((libvlc_media_instance_t *)instance, &ex);
  323. quit_on_exception(&ex);
  324. return YES;
  325. }
  326. - (void)pause
  327. {
  328. // Return if there is no media available or if the stream is not paused or
  329. // playing something else
  330. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  331. return;
  332. // Should never get here.
  333. if (!instance)
  334. return;
  335. // Pause the stream
  336. libvlc_exception_t ex;
  337. libvlc_exception_init(&ex);
  338. libvlc_media_instance_pause((libvlc_media_instance_t *)instance, &ex);
  339. quit_on_exception(&ex);
  340. // TODO: Should we record the time in case the media instance is destroyed
  341. // then rebuilt?
  342. }
  343. - (void)stop
  344. {
  345. // Return if there is no media available or if the system is not in play status
  346. // or pause status.
  347. if (!media || (![self isPlaying] && [self state] != VLCMediaPlayerStatePaused))
  348. return;
  349. // The following is not implemented in the core, should I fix it or just
  350. // compensate?
  351. // libvlc_exception_t ex;
  352. // libvlc_exception_init(&ex);
  353. // libvlc_media_instance_stop((libvlc_media_instance_t *)instance, &ex);
  354. // quit_on_exception(&ex);
  355. // Pause and reposition to the begining of the stream.
  356. [self pause];
  357. [self setTime:0];
  358. // TODO: Should we pause this or destroy the media instance so that it appears as being "stopped"?
  359. }
  360. //- (void)fastForward;
  361. //- (void)fastForwardAtRate:(int)rate;
  362. //- (void)rewind;
  363. //- (void)rewindAtRate:(int)rate;
  364. - (BOOL)isPlaying
  365. {
  366. VLCMediaPlayerState state = [self state];
  367. return ((state == VLCMediaPlayerStateOpening) || (state == VLCMediaPlayerStateBuffering) ||
  368. (state == VLCMediaPlayerStatePlaying));
  369. }
  370. - (BOOL)willPlay
  371. {
  372. libvlc_exception_t ex;
  373. libvlc_exception_init(&ex);
  374. BOOL ret = libvlc_media_instance_will_play((libvlc_media_instance_t *)instance, &ex);
  375. if (libvlc_exception_raised(&ex))
  376. {
  377. libvlc_exception_clear(&ex);
  378. return NO;
  379. }
  380. else
  381. return ret;
  382. }
  383. static VLCMediaPlayerState libvlc_to_local_state [] =
  384. {
  385. [libvlc_Stopped] = VLCMediaPlayerStateStopped,
  386. [libvlc_Opening] = VLCMediaPlayerStateOpening,
  387. [libvlc_Buffering] = VLCMediaPlayerStateBuffering,
  388. [libvlc_Playing] = VLCMediaPlayerStatePlaying,
  389. [libvlc_Paused] = VLCMediaPlayerStatePaused,
  390. [libvlc_Ended] = VLCMediaPlayerStateEnded,
  391. [libvlc_Error] = VLCMediaPlayerStateError
  392. };
  393. - (VLCMediaPlayerState)state
  394. {
  395. // If there is no instance, assume that we're in a stopped state
  396. if (!instance)
  397. return VLCMediaPlayerStateStopped;
  398. libvlc_exception_t ex;
  399. libvlc_exception_init(&ex);
  400. libvlc_state_t libvlc_state = libvlc_media_instance_get_state((libvlc_media_instance_t *)instance, &ex);
  401. if (libvlc_exception_raised(&ex))
  402. {
  403. libvlc_exception_clear(&ex);
  404. return VLCMediaPlayerStateError;
  405. }
  406. else
  407. return libvlc_to_local_state[libvlc_state];
  408. }
  409. @end
  410. @implementation VLCMediaPlayer (PrivateAPI)
  411. - (void)registerObservers
  412. {
  413. libvlc_exception_t ex;
  414. libvlc_exception_init(&ex);
  415. // Attach event observers into the media instance
  416. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager(instance, &ex);
  417. libvlc_event_attach(p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, &ex);
  418. libvlc_event_attach(p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, &ex);
  419. libvlc_event_attach(p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, &ex);
  420. libvlc_event_attach(p_em, libvlc_MediaInstancePositionChanged, HandleMediaTimeChanged, self, &ex);
  421. quit_on_exception(&ex);
  422. }
  423. - (void)unregisterObservers
  424. {
  425. libvlc_event_manager_t *p_em = libvlc_media_instance_event_manager(instance, NULL);
  426. libvlc_event_detach(p_em, libvlc_MediaInstancePlayed, HandleMediaInstanceStateChanged, self, NULL);
  427. libvlc_event_detach(p_em, libvlc_MediaInstancePaused, HandleMediaInstanceStateChanged, self, NULL);
  428. libvlc_event_detach(p_em, libvlc_MediaInstanceReachedEnd, HandleMediaInstanceStateChanged, self, NULL);
  429. libvlc_event_detach(p_em, libvlc_MediaInstancePositionChanged, HandleMediaTimeChanged, self, NULL);
  430. }
  431. @end