VLCVideoView.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "VLCVideoView.h"
  25. #import "VLCLibrary.h"
  26. #import "VLCEventManager.h"
  27. /* Libvlc */
  28. #include <vlc/vlc.h>
  29. #include <vlc/libvlc.h>
  30. #import <QuartzCore/QuartzCore.h>
  31. /* Notifications */
  32. NSString * VLCVideoViewEnteredFullScreen = @"VLCVideoViewEnteredFullScreen";
  33. NSString * VLCVideoViewLeftFullScreen = @"VLCVideoViewLeftFullScreen";
  34. /******************************************************************************
  35. * Soon deprecated stuff
  36. */
  37. /* This is a forward reference to VLCOpenGLVoutView specified in minimal_macosx
  38. library. We could get rid of this, but it prevents warnings from the
  39. compiler. (Scheduled to deletion) */
  40. @interface VLCOpenGLVoutView : NSView
  41. - (void)detachFromVout;
  42. @end
  43. /* Depreacted methods */
  44. @interface VLCVideoView (Deprecated)
  45. - (void)setStretchesVideo:(BOOL)value;
  46. - (BOOL)stretchesVideo;
  47. - (void)addVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
  48. - (void)removeVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
  49. @end
  50. /******************************************************************************
  51. * VLCVideoView (Private)
  52. */
  53. @interface VLCVideoView (Private)
  54. /* Method */
  55. - (void)addVoutLayer:(CALayer *)aLayer;
  56. @end
  57. /******************************************************************************
  58. * Interface & Implementation VLCConstraintLayoutManager
  59. *
  60. * Manage the size of the video layer
  61. */
  62. @interface VLCConstraintLayoutManager : CAConstraintLayoutManager
  63. {
  64. CGSize originalVideoSize;
  65. BOOL fillScreenEntirely;
  66. }
  67. @property BOOL fillScreenEntirely;
  68. @property CGSize originalVideoSize;
  69. @end
  70. @implementation VLCConstraintLayoutManager
  71. @synthesize fillScreenEntirely;
  72. @synthesize originalVideoSize;
  73. - (id)init
  74. {
  75. if( self = [super init] )
  76. {
  77. self.originalVideoSize = CGSizeMake(0., 0.);
  78. self.fillScreenEntirely = NO;
  79. }
  80. return self;
  81. }
  82. + (id)layoutManager
  83. {
  84. return [[[VLCConstraintLayoutManager alloc] init] autorelease];
  85. }
  86. - (void)layoutSublayersOfLayer:(CALayer *)layer
  87. {
  88. /* Called by CA, when our rootLayer needs layout */
  89. [super layoutSublayersOfLayer:layer];
  90. /* After having done everything normally resize the vlcopengllayer */
  91. if( [[layer sublayers] count] > 0 && [[[[layer sublayers] objectAtIndex:0] name] isEqualToString:@"vlcopengllayer"])
  92. {
  93. CALayer * videolayer = [[layer sublayers] objectAtIndex:0];
  94. CGRect bounds = layer.bounds;
  95. float new_height = (bounds.size.width * originalVideoSize.height) / originalVideoSize.width;
  96. if( fillScreenEntirely )
  97. {
  98. if( bounds.size.height > new_height )
  99. bounds.size.height = new_height;
  100. else
  101. bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
  102. }
  103. else
  104. {
  105. if( bounds.size.height > new_height )
  106. bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
  107. else
  108. bounds.size.height = new_height;
  109. }
  110. bounds.origin = CGPointMake( 0.0, 0.0 );
  111. videolayer.bounds = bounds;
  112. videolayer.position = CGPointMake((layer.bounds.size.width - layer.bounds.origin.x)/2, (layer.bounds.size.height - layer.bounds.origin.y)/2);
  113. }
  114. }
  115. @end
  116. /******************************************************************************
  117. * Implementation VLCVideoView
  118. */
  119. @implementation VLCVideoView
  120. - (BOOL)fillScreen
  121. {
  122. return [layoutManager fillScreenEntirely];
  123. }
  124. - (void)setFillScreen:(BOOL)fillScreen
  125. {
  126. [layoutManager setFillScreenEntirely:fillScreen];
  127. [[self layer] setNeedsLayout];
  128. }
  129. - (BOOL)fullScreen
  130. {
  131. return fullScreen;
  132. }
  133. - (void)setFullScreen:(BOOL)newFullScreen
  134. {
  135. if( newFullScreen )
  136. {
  137. fullScreen = YES;
  138. [self enterFullscreen];
  139. }
  140. else
  141. {
  142. fullScreen = NO;
  143. [self leaveFullscreen];
  144. }
  145. }
  146. - (id)initWithFrame:(NSRect)rect
  147. {
  148. if (self = [super initWithFrame:rect])
  149. {
  150. delegate = nil;
  151. [self setBackColor:[NSColor blackColor]];
  152. [self setStretchesVideo:NO];
  153. [self setAutoresizesSubviews:YES];
  154. [self setFillScreen: NO];
  155. layoutManager = [[VLCConstraintLayoutManager layoutManager] retain];
  156. }
  157. return self;
  158. }
  159. - (void)dealloc
  160. {
  161. [layoutManager release];
  162. delegate = nil;
  163. [backColor release];
  164. [super dealloc];
  165. }
  166. - (void)setDelegate:(id)value
  167. {
  168. delegate = value;
  169. }
  170. - (id)delegate
  171. {
  172. return delegate;
  173. }
  174. - (void)setBackColor:(NSColor *)value
  175. {
  176. if (backColor != value)
  177. {
  178. [backColor release];
  179. backColor = [value retain];
  180. }
  181. }
  182. - (NSColor *)backColor
  183. {
  184. return backColor;
  185. }
  186. /* This is a LibVLC notification that we're about to enter into full screen,
  187. there is no other place where I can see where we can trap this event */
  188. - (void)enterFullscreen
  189. {
  190. // Go ahead and send a notification to the world we're going into full screen
  191. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  192. withDelegateMethod:nil
  193. withNotificationName:VLCVideoViewEnteredFullScreen];
  194. [super enterFullScreenMode:[[self window] screen] withOptions:nil];
  195. if( !self.fullScreen ) self.fullScreen = YES;
  196. }
  197. /* This is a LibVLC notification that we're about to enter leaving full screen,
  198. there is no other place where I can see where we can trap this event */
  199. - (void)leaveFullscreen
  200. {
  201. // Go ahead and send a notification to the world we're leaving full screen
  202. [[VLCEventManager sharedManager] callOnMainThreadDelegateOfObject:self
  203. withDelegateMethod:nil
  204. withNotificationName:VLCVideoViewLeftFullScreen];
  205. // There is nothing else to do, as this object strictly displays the video feed
  206. [super exitFullScreenModeWithOptions:nil];
  207. if( self.fullScreen ) self.fullScreen = NO;
  208. }
  209. - (void)drawRect:(NSRect)aRect
  210. {
  211. [self lockFocus];
  212. [backColor set];
  213. NSRectFill(aRect);
  214. [self unlockFocus];
  215. }
  216. - (BOOL)isOpaque
  217. {
  218. return YES;
  219. }
  220. - (void)mouseDown:(NSEvent *)theEvent
  221. {
  222. if([theEvent clickCount] != 2)
  223. return;
  224. if(self.fullScreen)
  225. [self leaveFullscreen];
  226. else
  227. [self enterFullscreen];
  228. }
  229. @end
  230. /******************************************************************************
  231. * Implementation VLCVideoView (Private)
  232. */
  233. @implementation VLCVideoView (Private)
  234. /* This is called by the libvlc module 'opengllayer' as soon as there is one
  235. * vout available
  236. */
  237. - (void)addVoutLayer:(CALayer *)aLayer
  238. {
  239. [CATransaction begin];
  240. [self setWantsLayer: YES];
  241. CALayer * rootLayer = [self layer];
  242. aLayer.name = @"vlcopengllayer";
  243. [layoutManager setOriginalVideoSize:aLayer.bounds.size];
  244. [rootLayer setLayoutManager:layoutManager];
  245. [rootLayer insertSublayer:aLayer atIndex:0];
  246. [aLayer setNeedsLayout];
  247. [aLayer setNeedsDisplay];
  248. [rootLayer setNeedsDisplay];
  249. [rootLayer layoutIfNeeded];
  250. [CATransaction commit];
  251. }
  252. @end
  253. /******************************************************************************
  254. * Implementation VLCVideoView (Deprecated)
  255. */
  256. @implementation VLCVideoView (Deprecated)
  257. - (void)setStretchesVideo:(BOOL)value
  258. {
  259. stretchesVideo = value;
  260. }
  261. - (BOOL)stretchesVideo
  262. {
  263. return stretchesVideo;
  264. }
  265. /* This is called by the libvlc module 'minimal_macosx' as soon as there is one
  266. * vout available
  267. */
  268. - (void)addVoutSubview:(NSView *)aView /* (Scheduled to deletion) */
  269. {
  270. /* This is where the real video comes from */
  271. if( [[self subviews] count] )
  272. {
  273. /* XXX: This is a hack until core gets fixed */
  274. int i;
  275. for( i = 0; i < [[self subviews] count]; i++ )
  276. {
  277. [[[self subviews] objectAtIndex:i] detachFromVout];
  278. [[[self subviews] objectAtIndex:i] retain];
  279. [[[self subviews] objectAtIndex:i] removeFromSuperview];
  280. }
  281. }
  282. [aView setFrame:[self bounds]];
  283. [self addSubview:aView];
  284. // TODO: Should we let the media player specify these values?
  285. [aView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
  286. }
  287. - (void)removeVoutSubview:(NSView *)view /* (Scheduled to deletion) */
  288. {
  289. // Should we do something? I don't know, however the protocol requires
  290. // this to be implemented
  291. }
  292. @end