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 _videoLayer.fillScreen = YES;
49 [self addSublayer:_videoLayer];
53 _player = [[VLCMediaPlayer alloc] initWithVideoLayer:_videoLayer];
57 NSLog(@"playing media: %@", media);
59 [_player setMedia:media];
64 return [_player media];
73 return [_player media];
77 - (void)setPlaying:(BOOL)playing
87 return [_player isPlaying];
90 - (VLCMediaPlayer*)player
96 static float rates[NUM_RATES] = {-8.0, -4.0, -2.0, 1.0, 2.0, 4.0, 8.0};
98 - (void)_slideRate:(int)delta
100 float rate = _player.rate;
104 for(index=0; index<NUM_RATES; index++) {
105 if(rate == rates[index]) {
106 int newIndex = index + delta;
107 if(newIndex >= 0 && newIndex < NUM_RATES) {
108 rate = rates[newIndex];
121 [self _slideRate:+1];
126 [self _slideRate:-1];
131 if(_player.rate != 1.0) {
135 if([_player isPlaying]) {
145 #define CHECK_ERR(func) \
147 CGLError err = func;\
148 if(err != kCGLNoError) NSLog(@"error: %s", CGLErrorString(err)); \
151 #define CHECK_GL_ERROR() \
153 GLenum err = glGetError(); \
154 if(err != GL_NO_ERROR) NSLog(@"glError: %d", err); \
157 - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
160 CGRect bounds = self.bounds;
164 if(i<0 && _pBuffer == NULL) {
165 NSOpenGLView * openglView = nil;
166 for(NSView * subview in [_videoView subviews]) {
167 if([subview isKindOfClass:[NSOpenGLView class]]) {
168 openglView = (NSOpenGLView*)subview;
173 if(openglView != nil) {
174 NSLog(@"Create pbuffer %@", NSStringFromRect([_videoView bounds]));
175 CHECK_ERR(CGLCreatePBuffer(CGRectGetWidth(bounds), CGRectGetHeight(bounds), GL_TEXTURE_RECTANGLE_ARB, GL_RGB, 0, &_pBuffer));
177 CGLContextObj vlcContext = (CGLContextObj)[[openglView openGLContext] CGLContextObj];
179 CHECK_ERR(CGLLockContext(vlcContext));
180 CHECK_ERR(CGLSetCurrentContext(vlcContext));
183 CHECK_ERR(CGLGetVirtualScreen(vlcContext, &screen));
185 CHECK_ERR(CGLSetPBuffer(vlcContext, _pBuffer, 0, 0, screen));
187 CHECK_ERR(CGLUnlockContext(vlcContext));
188 CHECK_ERR(CGLSetCurrentContext(ctx));
192 if(_pBuffer != NULL) {
193 glColor3f(1.0, 1.0, 1.0);
196 glGenTextures(1, &texture);
198 glEnable(GL_TEXTURE_RECTANGLE_ARB);
200 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
202 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
204 CHECK_ERR(CGLTexImagePBuffer(ctx, _pBuffer, GL_FRONT));
207 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMinY(bounds));
208 glVertex2f(-1.0, -1.0);
209 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
210 glVertex2f(-1.0, 1.0);
211 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
212 glVertex2f(1.0, 1.0);
213 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
214 glVertex2f(1.0, -1.0);
218 glDisable(GL_TEXTURE_RECTANGLE_ARB);
221 glColor3f(0.0, 0.0, 0.0);
224 glVertex2f(-1.0, 1.0);
225 glVertex2f(1.0, 1.0);
226 glVertex2f(1.0, -1.0);
227 glVertex2f(-1.0, -1.0);
231 [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];