VLCVideoView.m 9.2 KB

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