]> git.sesse.net Git - vlc/blob - projects/macosx/frontrow_plugin/VLCMediaLayer.m
macosx/frontrow_plugin: VLCMediaListController, display a mediaListAspect in a FrontR...
[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)setMedia:(VLCMedia*)media
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 media: %@", media);
60     
61     [_player setMedia:media];
62 }
63
64 - (VLCMedia *)media
65 {
66     return [_player media];
67 }
68
69 - (VLCMedia *)url
70 {
71     if(_player == nil) {
72         return nil;
73     }
74     else {
75         return [_player media];
76     }
77 }
78
79 - (void)setPlaying:(BOOL)playing
80 {
81     if(playing)
82         [_player play];
83     else
84         [_player pause];
85 }
86
87 - (BOOL)playing
88 {
89     return [_player isPlaying];
90 }
91
92 - (VLCMediaPlayer*)player
93 {
94     return _player;
95 }
96
97 #define NUM_RATES 7
98 static float rates[NUM_RATES] = {-8.0, -4.0, -2.0, 1.0, 2.0, 4.0, 8.0};
99
100 - (void)_slideRate:(int)delta
101 {
102     float rate = _player.rate;
103     BOOL foundRate = NO;
104     
105     int index;
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];
111                 foundRate = YES;
112             }
113         }
114     }
115
116     if(foundRate) {
117         _player.rate = rate;
118     }
119 }
120
121 - (void)goFaster
122 {
123     [self _slideRate:+1];
124 }
125
126 - (void)goSlower
127 {
128     [self _slideRate:-1];
129 }
130
131 - (void)playPause
132 {
133     if(_player.rate != 1.0) {
134         _player.rate = 1.0;
135     }
136     else {
137         if([_player isPlaying]) {
138             [_player pause];
139         }
140         else {
141             [_player play];
142         }
143     }
144 }
145
146 #if 0
147 #define CHECK_ERR(func) \
148 { \
149         CGLError err = func;\
150         if(err != kCGLNoError) NSLog(@"error: %s", CGLErrorString(err)); \
151 }
152
153 #define CHECK_GL_ERROR() \
154 { \
155         GLenum err = glGetError(); \
156         if(err != GL_NO_ERROR) NSLog(@"glError: %d", err); \
157 }
158
159 - (void)drawInCGLContext:(CGLContextObj)ctx pixelFormat:(CGLPixelFormatObj)pf forLayerTime:(CFTimeInterval)t displayTime:(const CVTimeStamp *)ts
160 {
161     static int i = 0;
162     CGRect bounds = self.bounds;
163     //NSLog(@"draw");
164     --i;
165
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;
171                 break;
172             }
173         }
174
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));
178             
179             CGLContextObj vlcContext = (CGLContextObj)[[openglView openGLContext] CGLContextObj];
180             
181             CHECK_ERR(CGLLockContext(vlcContext));
182             CHECK_ERR(CGLSetCurrentContext(vlcContext));
183             
184             GLint screen;
185             CHECK_ERR(CGLGetVirtualScreen(vlcContext, &screen));
186             
187             CHECK_ERR(CGLSetPBuffer(vlcContext, _pBuffer, 0, 0, screen));
188             
189             CHECK_ERR(CGLUnlockContext(vlcContext));
190             CHECK_ERR(CGLSetCurrentContext(ctx));
191         }
192     }
193     
194     if(_pBuffer != NULL) {
195         glColor3f(1.0, 1.0, 1.0);
196         
197         GLuint texture;
198         glGenTextures(1, &texture);
199         CHECK_GL_ERROR();
200         glEnable(GL_TEXTURE_RECTANGLE_ARB);
201         CHECK_GL_ERROR();
202         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
203         CHECK_GL_ERROR();
204         glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
205         CHECK_GL_ERROR();
206         CHECK_ERR(CGLTexImagePBuffer(ctx, _pBuffer, GL_FRONT));
207         
208         glBegin(GL_QUADS);
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);
217         glEnd();
218         CHECK_GL_ERROR();
219         
220         glDisable(GL_TEXTURE_RECTANGLE_ARB);
221     }
222     else {
223         glColor3f(0.0, 0.0, 0.0);
224         
225         glBegin(GL_QUADS);
226         glVertex2f(-1.0, 1.0);
227         glVertex2f(1.0, 1.0);
228         glVertex2f(1.0, -1.0);
229         glVertex2f(-1.0, -1.0);
230         glEnd();
231     }
232
233     [super drawInCGLContext:ctx pixelFormat:pf forLayerTime:t displayTime:ts];
234 }
235 #endif
236 @end