5 // Created by hyei on 11/09/07.
6 // Copyright 2007 __MyCompanyName__. All rights reserved.
9 #import "VLCMediaLayer.h"
11 @implementation VLCMediaLayer
26 - (void)setMedia:(VLCMedia*)media
29 if(_videoLayer != nil) {
30 [_videoLayer removeFromSuperlayer];
33 _videoLayer = [VLCVideoLayer layer];
34 _videoLayer.frame = self.bounds;
35 _videoLayer.autoresizingMask = kCALayerWidthSizable|kCALayerHeightSizable;
36 [self addSublayer:_videoLayer];
42 _player = [[VLCMediaPlayer alloc] initWithVideoLayer:_videoLayer];
44 if(_videoLayer == nil) {
45 _videoLayer = [VLCVideoLayer layer];
46 _videoLayer.frame = self.bounds;
47 _videoLayer.autoresizingMask = kCALayerWidthSizable|kCALayerHeightSizable;
48 [self addSublayer:_videoLayer];
52 _player = [[VLCMediaPlayer alloc] initWithVideoLayer:_videoLayer];
59 NSLog(@"playing media: %@", media);
61 [_player setMedia:media];
66 return [_player media];
75 return [_player media];
79 - (void)setPlaying:(BOOL)playing
89 return [_player isPlaying];
92 - (VLCMediaPlayer*)player
98 static float rates[NUM_RATES] = {-8.0, -4.0, -2.0, 1.0, 2.0, 4.0, 8.0};
100 - (void)_slideRate:(int)delta
102 float rate = _player.rate;
106 for(index=0; index<NUM_RATES; index++) {
107 if(rate == rates[index]) {
108 int newIndex = index + delta;
109 if(newIndex >= 0 && newIndex < NUM_RATES) {
110 rate = rates[newIndex];
123 [self _slideRate:+1];
128 [self _slideRate:-1];
133 if(_player.rate != 1.0) {
137 if([_player isPlaying]) {
147 #define CHECK_ERR(func) \
149 CGLError err = func;\
150 if(err != kCGLNoError) NSLog(@"error: %s", CGLErrorString(err)); \
153 #define CHECK_GL_ERROR() \
155 GLenum err = glGetError(); \
156 if(err != GL_NO_ERROR) NSLog(@"glError: %d", err); \
159 - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
162 CGRect bounds = self.bounds;
166 if(i<0 && _pBuffer == NULL) {
167 NSOpenGLView * openglView = nil;
168 for(NSView * subview in [_videoView subviews]) {
169 if([subview isKindOfClass:[NSOpenGLView class]]) {
170 openglView = (NSOpenGLView*)subview;
175 if(openglView != nil) {
176 NSLog(@"Create pbuffer %@", NSStringFromRect([_videoView bounds]));
177 CHECK_ERR(CGLCreatePBuffer(CGRectGetWidth(bounds), CGRectGetHeight(bounds), GL_TEXTURE_RECTANGLE_ARB, GL_RGB, 0, &_pBuffer));
179 CGLContextObj vlcContext = (CGLContextObj)[[openglView openGLContext] CGLContextObj];
181 CHECK_ERR(CGLLockContext(vlcContext));
182 CHECK_ERR(CGLSetCurrentContext(vlcContext));
185 CHECK_ERR(CGLGetVirtualScreen(vlcContext, &screen));
187 CHECK_ERR(CGLSetPBuffer(vlcContext, _pBuffer, 0, 0, screen));
189 CHECK_ERR(CGLUnlockContext(vlcContext));
190 CHECK_ERR(CGLSetCurrentContext(ctx));
194 if(_pBuffer != NULL) {
195 glColor3f(1.0, 1.0, 1.0);
198 glGenTextures(1, &texture);
200 glEnable(GL_TEXTURE_RECTANGLE_ARB);
202 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
204 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
206 CHECK_ERR(CGLTexImagePBuffer(ctx, _pBuffer, GL_FRONT));
209 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMinY(bounds));
210 glVertex2f(-1.0, -1.0);
211 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
212 glVertex2f(-1.0, 1.0);
213 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
214 glVertex2f(1.0, 1.0);
215 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
216 glVertex2f(1.0, -1.0);
220 glDisable(GL_TEXTURE_RECTANGLE_ARB);
223 glColor3f(0.0, 0.0, 0.0);
226 glVertex2f(-1.0, 1.0);
227 glVertex2f(1.0, 1.0);
228 glVertex2f(1.0, -1.0);
229 glVertex2f(-1.0, -1.0);
233 [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];