VLCVideoView.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /******************************************************************************
  32. * Soon deprecated stuff
  33. */
  34. /* This is a forward reference to VLCOpenGLVoutView specified in minimal_macosx
  35. library. We could get rid of this, but it prevents warnings from the
  36. compiler. (Scheduled to deletion) */
  37. @interface VLCOpenGLVoutView : NSView
  38. - (void)detachFromVout;
  39. @end
  40. /* Depreacted methods */
  41. @interface VLCVideoView (Deprecated)
  42. - (void)setStretchesVideo:(BOOL)value;
  43. - (BOOL)stretchesVideo;
  44. - (void)addVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
  45. - (void)removeVoutSubview:(NSView *)aView; /* (Scheduled to deletion) */
  46. @end
  47. /******************************************************************************
  48. * VLCVideoView (Private)
  49. */
  50. @interface VLCVideoView (Private)
  51. /* Method */
  52. - (void)addVoutLayer:(CALayer *)aLayer;
  53. @end
  54. /******************************************************************************
  55. * Interface & Implementation VLCConstraintLayoutManager
  56. *
  57. * Manage the size of the video layer
  58. */
  59. @interface VLCConstraintLayoutManager : CAConstraintLayoutManager
  60. {
  61. CGSize originalVideoSize;
  62. BOOL fillScreenEntirely;
  63. }
  64. @property BOOL fillScreenEntirely;
  65. @property CGSize originalVideoSize;
  66. @end
  67. @implementation VLCConstraintLayoutManager
  68. @synthesize fillScreenEntirely;
  69. @synthesize originalVideoSize;
  70. - (id)init
  71. {
  72. if( self = [super init] )
  73. {
  74. self.originalVideoSize = CGSizeMake(0., 0.);
  75. self.fillScreenEntirely = NO;
  76. }
  77. return self;
  78. }
  79. + (id)layoutManager
  80. {
  81. return [[[VLCConstraintLayoutManager alloc] init] autorelease];
  82. }
  83. - (void)layoutSublayersOfLayer:(CALayer *)layer
  84. {
  85. /* Called by CA, when our rootLayer needs layout */
  86. [super layoutSublayersOfLayer:layer];
  87. /* After having done everything normally resize the vlcopengllayer */
  88. if( [[layer sublayers] count] > 0 && [[[[layer sublayers] objectAtIndex:0] name] isEqualToString:@"vlcopengllayer"])
  89. {
  90. CALayer * videolayer = [[layer sublayers] objectAtIndex:0];
  91. CGRect bounds = layer.bounds;
  92. float new_height = (bounds.size.width * originalVideoSize.height) / originalVideoSize.width;
  93. if( fillScreenEntirely )
  94. {
  95. if( bounds.size.height > new_height )
  96. bounds.size.height = new_height;
  97. else
  98. bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
  99. }
  100. else
  101. {
  102. if( bounds.size.height > new_height )
  103. bounds.size.width = (bounds.size.height * originalVideoSize.width) / originalVideoSize.height;
  104. else
  105. bounds.size.height = new_height;
  106. }
  107. bounds.origin = CGPointMake( 0.0, 0.0 );
  108. videolayer.bounds = bounds;
  109. videolayer.position = CGPointMake((layer.bounds.size.width - layer.bounds.origin.x)/2, (layer.bounds.size.height - layer.bounds.origin.y)/2);
  110. }
  111. }
  112. @end
  113. /******************************************************************************
  114. * Implementation VLCVideoView
  115. */
  116. @implementation VLCVideoView
  117. - (BOOL)fillScreen
  118. {
  119. return [layoutManager fillScreenEntirely];
  120. }
  121. - (void)setFillScreen:(BOOL)fillScreen
  122. {
  123. [layoutManager setFillScreenEntirely:fillScreen];
  124. [[self layer] setNeedsLayout];
  125. }
  126. - (id)initWithFrame:(NSRect)rect
  127. {
  128. if (self = [super initWithFrame:rect])
  129. {
  130. delegate = nil;
  131. [self setBackColor:[NSColor blackColor]];
  132. [self setStretchesVideo:NO];
  133. [self setAutoresizesSubviews:YES];
  134. [self setFillScreen: NO];
  135. layoutManager = [[VLCConstraintLayoutManager layoutManager] retain];
  136. }
  137. return self;
  138. }
  139. - (void)dealloc
  140. {
  141. [layoutManager release];
  142. delegate = nil;
  143. [backColor release];
  144. [super dealloc];
  145. }
  146. - (void)setDelegate:(id)value
  147. {
  148. delegate = value;
  149. }
  150. - (id)delegate
  151. {
  152. return delegate;
  153. }
  154. - (void)setBackColor:(NSColor *)value
  155. {
  156. if (backColor != value)
  157. {
  158. [backColor release];
  159. backColor = [value retain];
  160. }
  161. }
  162. - (NSColor *)backColor
  163. {
  164. return backColor;
  165. }
  166. - (void)drawRect:(NSRect)aRect
  167. {
  168. [self lockFocus];
  169. [backColor set];
  170. NSRectFill(aRect);
  171. [self unlockFocus];
  172. }
  173. - (BOOL)isOpaque
  174. {
  175. return YES;
  176. }
  177. @end
  178. /******************************************************************************
  179. * Implementation VLCVideoView (Private)
  180. */
  181. @implementation VLCVideoView (Private)
  182. /* This is called by the libvlc module 'opengllayer' as soon as there is one
  183. * vout available
  184. */
  185. - (void)addVoutLayer:(CALayer *)aLayer
  186. {
  187. [CATransaction begin];
  188. [self setWantsLayer: YES];
  189. CALayer * rootLayer = [self layer];
  190. aLayer.name = @"vlcopengllayer";
  191. [layoutManager setOriginalVideoSize:aLayer.bounds.size];
  192. [rootLayer setLayoutManager:layoutManager];
  193. [rootLayer insertSublayer:aLayer atIndex:0];
  194. [aLayer setNeedsLayout];
  195. [aLayer setNeedsDisplay];
  196. [rootLayer setNeedsDisplay];
  197. [rootLayer layoutIfNeeded];
  198. [CATransaction commit];
  199. }
  200. @end
  201. /******************************************************************************
  202. * Implementation VLCVideoView (Deprecated)
  203. */
  204. @implementation VLCVideoView (Deprecated)
  205. - (void)setStretchesVideo:(BOOL)value
  206. {
  207. stretchesVideo = value;
  208. }
  209. - (BOOL)stretchesVideo
  210. {
  211. return stretchesVideo;
  212. }
  213. /* This is called by the libvlc module 'minimal_macosx' as soon as there is one
  214. * vout available
  215. */
  216. - (void)addVoutSubview:(NSView *)aView /* (Scheduled to deletion) */
  217. {
  218. /* This is where the real video comes from */
  219. if( [[self subviews] count] )
  220. {
  221. /* XXX: This is a hack until core gets fixed */
  222. int i;
  223. for( i = 0; i < [[self subviews] count]; i++ )
  224. {
  225. [[[self subviews] objectAtIndex:i] detachFromVout];
  226. [[[self subviews] objectAtIndex:i] retain];
  227. [[[self subviews] objectAtIndex:i] removeFromSuperview];
  228. }
  229. }
  230. [aView setFrame:[self bounds]];
  231. [self addSubview:aView];
  232. // TODO: Should we let the media player specify these values?
  233. [aView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
  234. }
  235. - (void)removeVoutSubview:(NSView *)view /* (Scheduled to deletion) */
  236. {
  237. // Should we do something? I don't know, however the protocol requires
  238. // this to be implemented
  239. }
  240. @end