VLCVideoView.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*****************************************************************************
  2. * VLCVideoView.h: VLC.framework VLCVideoView 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. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #import <VLC/VLCVideoView.h>
  25. #import "VLCLibrary.h"
  26. #import "VLCEventManager.h"
  27. /* Libvlc */
  28. #include <vlc/vlc.h>
  29. #include <vlc/libvlc.h>
  30. /* Notification */
  31. NSString * VLCVideoDidChangeVolume = @"VLCVideoDidChangeVolume";
  32. NSString * VLCVideoDidChangeTime = @"VLCVideoDidChangeTime";
  33. NSString * VLCVideoDidChangeCurrentlyPlayingItem = @"VLCVideoDidChangeCurrentlyPlayingItem";
  34. NSString * VLCVideoDidPause = @"VLCVideoDidPause";
  35. NSString * VLCVideoDidPlay = @"VLCVideoDidPlay";
  36. NSString * VLCVideoDidStop = @"VLCVideoDidStop";
  37. static void HandleMediaListPlayerCurrentPlayingItemChanged( const libvlc_event_t * event, void * self )
  38. {
  39. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  40. withDelegateMethod: @selector(volumeDidChangeCurrentPlayingItem:)
  41. withNotificationName: VLCVideoDidChangeCurrentlyPlayingItem];
  42. }
  43. static void HandleMediaListPlayerPaused( const libvlc_event_t * event, void * self )
  44. {
  45. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  46. withDelegateMethod: @selector(videoDidPause:)
  47. withNotificationName: VLCVideoDidPause];
  48. }
  49. static void HandleMediaListPlayerPlayed( const libvlc_event_t * event, void * self )
  50. {
  51. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  52. withDelegateMethod: @selector(videoDidPlay:)
  53. withNotificationName: VLCVideoDidPlay];
  54. }
  55. static void HandleMediaListPlayerStopped( const libvlc_event_t * event, void * self )
  56. {
  57. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject: self
  58. withDelegateMethod: @selector(videoDidStop:)
  59. withNotificationName: VLCVideoDidStop];
  60. }
  61. @implementation VLCVideoView
  62. - (id)initWithFrame:(NSRect)frameRect
  63. {
  64. if (self = [super initWithFrame: frameRect])
  65. {
  66. libvlc_exception_t p_e;
  67. libvlc_exception_init( &p_e );
  68. p_mi = libvlc_media_instance_new( [VLCLibrary sharedInstance], &p_e );
  69. quit_on_exception( &p_e );
  70. p_mlp = libvlc_media_list_player_new( [VLCLibrary sharedInstance], &p_e );
  71. quit_on_exception( &p_e );
  72. libvlc_media_list_player_set_media_instance( p_mlp, p_mi, &p_e );
  73. quit_on_exception( &p_e );
  74. libvlc_media_instance_set_drawable( p_mi, (libvlc_drawable_t)self, &p_e );
  75. quit_on_exception( &p_e );
  76. libvlc_event_manager_t * p_em = libvlc_media_list_event_manager( p_mlp, &p_e );
  77. //libvlc_event_attach( p_em, libvlc_MediaListPlayerCurrentPlayingItemChanged, HandleMediaListPlayerCurrentPlayingItemChanged, self, &p_e );
  78. //libvlc_event_attach( p_em, libvlc_MediaListPlayerPaused, HandleMediaListPlayerPaused, self, &p_e );
  79. //libvlc_event_attach( p_em, libvlc_MediaListPlayerPlayed, HandleMediaListPlayerPlayed, self, &p_e );
  80. //libvlc_event_attach( p_em, libvlc_MediaListPlayerStopped, HandleMediaListPlayerStopped, self, &p_e );
  81. quit_on_exception( &p_e );
  82. delegate = nil;
  83. playlist = nil;
  84. [self setStretchesVideo: NO];
  85. [self setAutoresizesSubviews: YES];
  86. }
  87. return self;
  88. }
  89. - (void)dealloc
  90. {
  91. libvlc_media_instance_release( p_mi );
  92. if (delegate)
  93. {
  94. [delegate release];
  95. delegate = nil;
  96. }
  97. [super dealloc];
  98. }
  99. - (void)setPlaylist: (VLCPlaylist *)newPlaylist
  100. {
  101. libvlc_exception_t p_e;
  102. libvlc_exception_init( &p_e );
  103. if (playlist)
  104. [playlist release];
  105. playlist = [newPlaylist retain];
  106. libvlc_media_list_player_set_media_list( p_mlp, [playlist libVLCMediaList], &p_e );
  107. quit_on_exception( &p_e );
  108. }
  109. - (VLCPlaylist *)playlist
  110. {
  111. return playlist;
  112. }
  113. /* Play */
  114. - (void)play
  115. {
  116. libvlc_exception_t p_e;
  117. libvlc_exception_init( &p_e );
  118. libvlc_media_list_player_play( p_mlp, &p_e );
  119. quit_on_exception( &p_e );
  120. }
  121. - (void)playItemAtIndex:(int) i
  122. {
  123. libvlc_exception_t p_e;
  124. libvlc_exception_init( &p_e );
  125. libvlc_media_list_player_play_item_at_index( p_mlp, i, &p_e );
  126. quit_on_exception( &p_e );
  127. }
  128. - (void)playMedia:(VLCMedia *) media
  129. {
  130. libvlc_exception_t p_e;
  131. libvlc_exception_init( &p_e );
  132. libvlc_media_list_player_play_item( p_mlp, [media libVLCMediaDescriptor], &p_e );
  133. quit_on_exception( &p_e );
  134. }
  135. - (void)pause
  136. {
  137. libvlc_exception_t p_e;
  138. libvlc_exception_init( &p_e );
  139. libvlc_media_list_player_pause( p_mlp, &p_e );
  140. quit_on_exception( &p_e );
  141. }
  142. - (void)setCurrentTime:(VLCTime *)timeObj
  143. {
  144. }
  145. /* State */
  146. - (BOOL)isPlaying
  147. {
  148. libvlc_exception_t p_e;
  149. BOOL ret = libvlc_media_list_player_is_playing( p_mlp, &p_e );
  150. quit_on_exception( &p_e );
  151. return ret;
  152. }
  153. - (BOOL)isPaused
  154. {
  155. libvlc_exception_t p_e;
  156. libvlc_state_t state = libvlc_media_list_player_get_state( p_mlp, &p_e );
  157. quit_on_exception( &p_e );
  158. return (state == libvlc_Paused);
  159. }
  160. - (VLCTime *)currentTime
  161. {
  162. return NULL;
  163. }
  164. - (id)currentPlaylistItem
  165. {
  166. return NULL;
  167. }
  168. /* Video output property */
  169. - (void)setStretchesVideo:(BOOL)flag
  170. {
  171. stretchVideo = flag;
  172. }
  173. - (BOOL)stretchesVideo
  174. {
  175. return stretchVideo;
  176. }
  177. /* Fullscreen */
  178. - (void)enterFullscreen
  179. {
  180. }
  181. - (void)leaveFullscreen
  182. {
  183. }
  184. /* Delegate */
  185. - (void)setDelegate: (id)newDelegate
  186. {
  187. if (delegate)
  188. [delegate release];
  189. delegate = [newDelegate retain];
  190. }
  191. - (id)delegate
  192. {
  193. return delegate;
  194. }
  195. @end
  196. @implementation VLCVideoView (NSViewSubclass)
  197. - (void)drawRect:(NSRect)aRect
  198. {
  199. [self lockFocus];
  200. [[NSColor blackColor] set];
  201. NSRectFill(aRect);
  202. [self unlockFocus];
  203. }
  204. - (BOOL)isOpaque
  205. {
  206. return YES;
  207. }
  208. @end
  209. @interface VLCOpenGLVoutView : NSView
  210. /* This is part of the hack to avoid a warning in -[VLCVideoView addVoutSubview:] */
  211. - (void)detachFromVout;
  212. @end
  213. @implementation VLCVideoView (VLCOpenGLVoutEmbedding)
  214. /* This is called by the libvlc module 'macosx' as soon as there is one vout
  215. * available */
  216. - (void)addVoutSubview:(id)aView
  217. {
  218. if( [[self subviews] count] )
  219. {
  220. /* XXX: This is a hack until core gets fixed */
  221. int i;
  222. for( i = 0; i < [[self subviews] count]; i++ )
  223. {
  224. [[[self subviews] objectAtIndex: i] detachFromVout];
  225. [[[self subviews] objectAtIndex: i] retain];
  226. [[[self subviews] objectAtIndex: i] removeFromSuperview];
  227. }
  228. }
  229. [self addSubview: aView];
  230. [aView setFrame: [self bounds]];
  231. [aView setAutoresizingMask:NSViewHeightSizable|NSViewWidthSizable];
  232. }
  233. @end