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