VLCVideoView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. - (id)initWithFrame:(NSRect)rect
  118. {
  119. if (self = [super initWithFrame:rect])
  120. {
  121. delegate = nil;
  122. [self setBackColor:[NSColor blackColor]];
  123. [self setStretchesVideo:NO];
  124. [self setAutoresizesSubviews:YES];
  125. [self setFillScreen: NO];
  126. layoutManager = [[VLCConstraintLayoutManager layoutManager] retain];
  127. }
  128. return self;
  129. }
  130. - (void)dealloc
  131. {
  132. [layoutManager release];
  133. delegate = nil;
  134. [backColor release];
  135. [super dealloc];
  136. }
  137. - (void)drawRect:(NSRect)aRect
  138. {
  139. [self lockFocus];
  140. [backColor set];
  141. NSRectFill(aRect);
  142. [self unlockFocus];
  143. }
  144. - (BOOL)isOpaque
  145. {
  146. return YES;
  147. }
  148. @synthesize delegate;
  149. @synthesize backColor;
  150. - (BOOL)fillScreen
  151. {
  152. return [layoutManager fillScreenEntirely];
  153. }
  154. - (void)setFillScreen:(BOOL)fillScreen
  155. {
  156. [layoutManager setFillScreenEntirely:fillScreen];
  157. [[self layer] setNeedsLayout];
  158. }
  159. @end
  160. /******************************************************************************
  161. * Implementation VLCVideoView (Private)
  162. */
  163. @implementation VLCVideoView (Private)
  164. /* This is called by the libvlc module 'opengllayer' as soon as there is one
  165. * vout available
  166. */
  167. - (void)addVoutLayer:(CALayer *)aLayer
  168. {
  169. [CATransaction begin];
  170. [self setWantsLayer: YES];
  171. CALayer * rootLayer = [self layer];
  172. aLayer.name = @"vlcopengllayer";
  173. [layoutManager setOriginalVideoSize:aLayer.bounds.size];
  174. [rootLayer setLayoutManager:layoutManager];
  175. [rootLayer insertSublayer:aLayer atIndex:0];
  176. [aLayer setNeedsLayout];
  177. [aLayer setNeedsDisplay];
  178. [rootLayer setNeedsDisplay];
  179. [rootLayer layoutIfNeeded];
  180. [CATransaction commit];
  181. }
  182. @end
  183. /******************************************************************************
  184. * Implementation VLCVideoView (Deprecated)
  185. */
  186. @implementation VLCVideoView (Deprecated)
  187. - (void)setStretchesVideo:(BOOL)value
  188. {
  189. stretchesVideo = value;
  190. }
  191. - (BOOL)stretchesVideo
  192. {
  193. return stretchesVideo;
  194. }
  195. /* This is called by the libvlc module 'minimal_macosx' as soon as there is one
  196. * vout available
  197. */
  198. - (void)addVoutSubview:(NSView *)aView /* (Scheduled to deletion) */
  199. {
  200. /* This is where the real video comes from */
  201. if( [[self subviews] count] )
  202. {
  203. /* XXX: This is a hack until core gets fixed */
  204. int i;
  205. for( i = 0; i < [[self subviews] count]; i++ )
  206. {
  207. [[[self subviews] objectAtIndex:i] detachFromVout];
  208. [[[self subviews] objectAtIndex:i] retain];
  209. [[[self subviews] objectAtIndex:i] removeFromSuperview];
  210. }
  211. }
  212. [aView setFrame:[self bounds]];
  213. [self addSubview:aView];
  214. // TODO: Should we let the media player specify these values?
  215. [aView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
  216. }
  217. - (void)removeVoutSubview:(NSView *)view /* (Scheduled to deletion) */
  218. {
  219. // Should we do something? I don't know, however the protocol requires
  220. // this to be implemented
  221. }
  222. @end