]> git.sesse.net Git - vlc/blob - projects/macosx/frontrow_plugin/VLCMediaLayer.m
1d51317932189e5ecbf05810927dba7f3a8dac27
[vlc] / projects / macosx / frontrow_plugin / VLCMediaLayer.m
1 //
2 //  VLCMediaLayer.m
3 //  FRVLC
4 //
5 //  Created by hyei on 11/09/07.
6 //  Copyright 2007 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "VLCMediaLayer.h"
10
11 @implementation VLCMediaLayer
12
13 - init
14 {
15     self = [super init];
16     
17     return self;
18 }
19
20 - (void)dealloc
21 {
22     [_player release];
23     [super dealloc];
24 }
25
26 - (void)setUrl:(NSURL*)url
27 {
28 #if 0
29     if(_videoLayer != nil) {
30         [_videoLayer removeFromSuperlayer];
31     }
32     
33     _videoLayer = [VLCVideoLayer layer];
34     _videoLayer.frame = self.bounds;
35     _videoLayer.autoresizingMask = kCALayerWidthSizable|kCALayerHeightSizable;
36     [self addSublayer:_videoLayer];
37     
38     if(_player != nil) {
39         [_player release];
40     }
41     
42     _player = [[VLCMediaPlayer alloc] initWithVideoLayer:_videoLayer];
43 #else
44     if(_videoLayer == nil) {
45         _videoLayer = [VLCVideoLayer layer];
46         _videoLayer.frame = self.bounds;
47         _videoLayer.autoresizingMask = kCALayerWidthSizable|kCALayerHeightSizable;
48         [self addSublayer:_videoLayer];
49     }
50     
51     if(_player == nil) {
52         _player = [[VLCMediaPlayer alloc] initWithVideoLayer:_videoLayer];
53     }
54     else {
55         [_player pause];
56     }
57 #endif
58     
59     NSLog(@"playing url: %@", url);
60     
61     VLCMedia * media = [VLCMedia mediaWithURL:url];
62     [_player setMedia:media];
63 }
64
65 - (NSURL*)url
66 {
67     if(_player == nil) {
68         return nil;
69     }
70     else {
71         return [[_player media] url];
72     }
73 }
74
75 - (void)setPlaying:(BOOL)playing
76 {
77     if(playing)
78         [_player play];
79     else
80         [_player pause];
81 }
82
83 - (BOOL)playing
84 {
85     return [_player isPlaying];
86 }
87
88 - (VLCMediaPlayer*)player
89 {
90     return _player;
91 }
92
93 #define NUM_RATES 7
94 static float rates[NUM_RATES] = {-8.0, -4.0, -2.0, 1.0, 2.0, 4.0, 8.0};
95
96 - (void)_slideRate:(int)delta
97 {
98     float rate = _player.rate;
99     BOOL foundRate = NO;
100     
101     int index;
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];
107                 foundRate = YES;
108             }
109         }
110     }
111
112     if(foundRate) {
113         _player.rate = rate;
114     }
115 }
116
117 - (void)goFaster
118 {
119     [self _slideRate:+1];
120 }
121
122 - (void)goSlower
123 {
124     [self _slideRate:-1];
125 }
126
127 - (void)playPause
128 {
129     if(_player.rate != 1.0) {
130         _player.rate = 1.0;
131     }
132     else {
133         if([_player isPlaying]) {
134             [_player pause];
135         }
136         else {
137             [_player play];
138         }
139     }
140 }
141
142 #if 0
143 #define CHECK_ERR(func) \
144 { \
145         CGLError err = func;\
146         if(err != kCGLNoError) NSLog(@"error: %s", CGLErrorString(err)); \
147 }
148
149 #define CHECK_GL_ERROR() \
150 { \
151         GLenum err = glGetError(); \
152         if(err != GL_NO_ERROR) NSLog(@"glError: %d", err); \
153 }
154
155 - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
156 {
157     static int i = 0;
158     CGRect bounds = self.bounds;
159     //NSLog(@"draw");
160     --i;
161
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;
167                 break;
168             }
169         }
170
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));
174             
175             CGLContextObj vlcContext = (CGLContextObj)[[openglView openGLContext] CGLContextObj];
176             
177             CHECK_ERR(CGLLockContext(vlcContext));
178             CHECK_ERR(CGLSetCurrentContext(vlcContext));
179             
180             GLint screen;
181             CHECK_ERR(CGLGetVirtualScreen(vlcContext, &screen));
182             
183             CHECK_ERR(CGLSetPBuffer(vlcContext, _pBuffer, 0, 0, screen));
184             
185             CHECK_ERR(CGLUnlockContext(vlcContext));
186             CHECK_ERR(CGLSetCurrentContext(ctx));
187         }
188     }
189     
190     if(_pBuffer != NULL) {
191         glColor3f(1.0, 1.0, 1.0);
192         
193         GLuint texture;
194         glGenTextures(1, &texture);
195         CHECK_GL_ERROR();
196         glEnable(GL_TEXTURE_RECTANGLE_ARB);
197         CHECK_GL_ERROR();
198         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
199         CHECK_GL_ERROR();
200         glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
201         CHECK_GL_ERROR();
202         CHECK_ERR(CGLTexImagePBuffer(ctx, _pBuffer, GL_FRONT));
203         
204         glBegin(GL_QUADS);
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);
213         glEnd();
214         CHECK_GL_ERROR();
215         
216         glDisable(GL_TEXTURE_RECTANGLE_ARB);
217     }
218     else {
219         glColor3f(0.0, 0.0, 0.0);
220         
221         glBegin(GL_QUADS);
222         glVertex2f(-1.0, 1.0);
223         glVertex2f(1.0, 1.0);
224         glVertex2f(1.0, -1.0);
225         glVertex2f(-1.0, -1.0);
226         glEnd();
227     }
228
229     [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];
230 }
231 #endif
232 @end