5 // Created by hyei on 11/09/07.
6 // Copyright 2007 __MyCompanyName__. All rights reserved.
9 #import "VLCMediaLayer.h"
11 @implementation VLCMediaLayer
26 - (void)setUrl:(NSURL*)url
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 url: %@", url);
61 VLCMedia * media = [VLCMedia mediaWithURL:url];
62 [_player setMedia:media];
71 return [[_player media] url];
75 - (void)setPlaying:(BOOL)playing
85 return [_player isPlaying];
88 - (VLCMediaPlayer*)player
94 static float rates[NUM_RATES] = {-8.0, -4.0, -2.0, 1.0, 2.0, 4.0, 8.0};
96 - (void)_slideRate:(int)delta
98 float rate = _player.rate;
102 for(index=0; index<NUM_RATES; index++) {
103 if(rate == rates[index]) {
104 int newIndex = index + delta;
105 if(newIndex >= 0 && newIndex < NUM_RATES) {
106 rate = rates[newIndex];
119 [self _slideRate:+1];
124 [self _slideRate:-1];
129 if(_player.rate != 1.0) {
133 if([_player isPlaying]) {
143 #define CHECK_ERR(func) \
145 CGLError err = func;\
146 if(err != kCGLNoError) NSLog(@"error: %s", CGLErrorString(err)); \
149 #define CHECK_GL_ERROR() \
151 GLenum err = glGetError(); \
152 if(err != GL_NO_ERROR) NSLog(@"glError: %d", err); \
155 - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
158 CGRect bounds = self.bounds;
162 if(i<0 && _pBuffer == NULL) {
163 NSOpenGLView * openglView = nil;
164 for(NSView * subview in [_videoView subviews]) {
165 if([subview isKindOfClass:[NSOpenGLView class]]) {
166 openglView = (NSOpenGLView*)subview;
171 if(openglView != nil) {
172 NSLog(@"Create pbuffer %@", NSStringFromRect([_videoView bounds]));
173 CHECK_ERR(CGLCreatePBuffer(CGRectGetWidth(bounds), CGRectGetHeight(bounds), GL_TEXTURE_RECTANGLE_ARB, GL_RGB, 0, &_pBuffer));
175 CGLContextObj vlcContext = (CGLContextObj)[[openglView openGLContext] CGLContextObj];
177 CHECK_ERR(CGLLockContext(vlcContext));
178 CHECK_ERR(CGLSetCurrentContext(vlcContext));
181 CHECK_ERR(CGLGetVirtualScreen(vlcContext, &screen));
183 CHECK_ERR(CGLSetPBuffer(vlcContext, _pBuffer, 0, 0, screen));
185 CHECK_ERR(CGLUnlockContext(vlcContext));
186 CHECK_ERR(CGLSetCurrentContext(ctx));
190 if(_pBuffer != NULL) {
191 glColor3f(1.0, 1.0, 1.0);
194 glGenTextures(1, &texture);
196 glEnable(GL_TEXTURE_RECTANGLE_ARB);
198 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
200 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
202 CHECK_ERR(CGLTexImagePBuffer(ctx, _pBuffer, GL_FRONT));
205 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMinY(bounds));
206 glVertex2f(-1.0, -1.0);
207 glTexCoord2f(CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
208 glVertex2f(-1.0, 1.0);
209 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
210 glVertex2f(1.0, 1.0);
211 glTexCoord2f(CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
212 glVertex2f(1.0, -1.0);
216 glDisable(GL_TEXTURE_RECTANGLE_ARB);
219 glColor3f(0.0, 0.0, 0.0);
222 glVertex2f(-1.0, 1.0);
223 glVertex2f(1.0, 1.0);
224 glVertex2f(1.0, -1.0);
225 glVertex2f(-1.0, -1.0);
229 [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];