| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include <vlc_plugin.h> |
|---|
| 35 |
#include <vlc_input.h> |
|---|
| 36 |
#include <vlc_vout.h> |
|---|
| 37 |
#include <vlc_demux.h> |
|---|
| 38 |
#include <vlc_interface.h> |
|---|
| 39 |
|
|---|
| 40 |
#import <QTKit/QTKit.h> |
|---|
| 41 |
#import <CoreAudio/CoreAudio.h> |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
static int Open( vlc_object_t *p_this ); |
|---|
| 47 |
static void Close( vlc_object_t *p_this ); |
|---|
| 48 |
static int Demux( demux_t *p_demux ); |
|---|
| 49 |
static int Control( demux_t *, int, va_list ); |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
vlc_module_begin(); |
|---|
| 55 |
set_shortname( N_("Quicktime Capture") ); |
|---|
| 56 |
set_description( N_("Quicktime Capture") ); |
|---|
| 57 |
set_category( CAT_INPUT ); |
|---|
| 58 |
set_subcategory( SUBCAT_INPUT_ACCESS ); |
|---|
| 59 |
add_shortcut( "qtcapture" ); |
|---|
| 60 |
set_capability( "access_demux", 10 ); |
|---|
| 61 |
set_callbacks( Open, Close ); |
|---|
| 62 |
vlc_module_end(); |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
@interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput |
|---|
| 69 |
{ |
|---|
| 70 |
CVImageBufferRef currentImageBuffer; |
|---|
| 71 |
mtime_t currentPts; |
|---|
| 72 |
mtime_t previousPts; |
|---|
| 73 |
} |
|---|
| 74 |
- (id)init; |
|---|
| 75 |
- (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection; |
|---|
| 76 |
- (mtime_t)copyCurrentFrameToBuffer:(void *)buffer; |
|---|
| 77 |
@end |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
@implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput |
|---|
| 81 |
- (id)init |
|---|
| 82 |
{ |
|---|
| 83 |
if( self = [super init] ) |
|---|
| 84 |
{ |
|---|
| 85 |
currentImageBuffer = nil; |
|---|
| 86 |
currentPts = 0; |
|---|
| 87 |
previousPts = 0; |
|---|
| 88 |
} |
|---|
| 89 |
return self; |
|---|
| 90 |
} |
|---|
| 91 |
- (void)dealloc |
|---|
| 92 |
{ |
|---|
| 93 |
@synchronized (self) |
|---|
| 94 |
{ |
|---|
| 95 |
CVBufferRelease(currentImageBuffer); |
|---|
| 96 |
currentImageBuffer = nil; |
|---|
| 97 |
} |
|---|
| 98 |
[super dealloc]; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
- (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection |
|---|
| 102 |
{ |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
CVImageBufferRef imageBufferToRelease; |
|---|
| 106 |
|
|---|
| 107 |
CVBufferRetain(videoFrame); |
|---|
| 108 |
|
|---|
| 109 |
@synchronized (self) |
|---|
| 110 |
{ |
|---|
| 111 |
imageBufferToRelease = currentImageBuffer; |
|---|
| 112 |
currentImageBuffer = videoFrame; |
|---|
| 113 |
currentPts = (mtime_t)(1000000L / [sampleBuffer presentationTime].timeScale * [sampleBuffer presentationTime].timeValue); |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
NSNumber *hosttime = (NSNumber *)[sampleBuffer attributeForKey:QTSampleBufferHostTimeAttribute]; |
|---|
| 117 |
if( hosttime ) currentPts = (mtime_t)AudioConvertHostTimeToNanos([hosttime unsignedLongLongValue])/1000; |
|---|
| 118 |
} |
|---|
| 119 |
CVBufferRelease(imageBufferToRelease); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
- (mtime_t)copyCurrentFrameToBuffer:(void *)buffer |
|---|
| 123 |
{ |
|---|
| 124 |
CVImageBufferRef imageBuffer; |
|---|
| 125 |
mtime_t pts; |
|---|
| 126 |
|
|---|
| 127 |
if(!currentImageBuffer || currentPts == previousPts ) |
|---|
| 128 |
return 0; |
|---|
| 129 |
|
|---|
| 130 |
@synchronized (self) |
|---|
| 131 |
{ |
|---|
| 132 |
imageBuffer = CVBufferRetain(currentImageBuffer); |
|---|
| 133 |
pts = previousPts = currentPts; |
|---|
| 134 |
|
|---|
| 135 |
CVPixelBufferLockBaseAddress(imageBuffer, 0); |
|---|
| 136 |
void * pixels = CVPixelBufferGetBaseAddress(imageBuffer); |
|---|
| 137 |
memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) ); |
|---|
| 138 |
CVPixelBufferUnlockBaseAddress(imageBuffer, 0); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
CVBufferRelease(imageBuffer); |
|---|
| 142 |
|
|---|
| 143 |
return currentPts; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
@end |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
struct demux_sys_t { |
|---|
| 153 |
QTCaptureSession * session; |
|---|
| 154 |
QTCaptureDevice * device; |
|---|
| 155 |
VLCDecompressedVideoOutput * output; |
|---|
| 156 |
int height, width; |
|---|
| 157 |
es_out_id_t * p_es_video; |
|---|
| 158 |
}; |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
static int qtchroma_to_fourcc( int i_qt ) |
|---|
| 165 |
{ |
|---|
| 166 |
static const struct |
|---|
| 167 |
{ |
|---|
| 168 |
unsigned int i_qt; |
|---|
| 169 |
int i_fourcc; |
|---|
| 170 |
} qtchroma_to_fourcc[] = |
|---|
| 171 |
{ |
|---|
| 172 |
|
|---|
| 173 |
{ k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') }, |
|---|
| 174 |
{ kComponentVideoCodecType,VLC_FOURCC('Y','U','Y','2') }, |
|---|
| 175 |
{ 0, 0 } |
|---|
| 176 |
}; |
|---|
| 177 |
int i; |
|---|
| 178 |
for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ ) |
|---|
| 179 |
{ |
|---|
| 180 |
if( qtchroma_to_fourcc[i].i_qt == i_qt ) |
|---|
| 181 |
return qtchroma_to_fourcc[i].i_fourcc; |
|---|
| 182 |
} |
|---|
| 183 |
return 0; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
static int Open( vlc_object_t *p_this ) |
|---|
| 190 |
{ |
|---|
| 191 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 192 |
demux_sys_t *p_sys = NULL; |
|---|
| 193 |
es_format_t fmt; |
|---|
| 194 |
int i; |
|---|
| 195 |
int i_width; |
|---|
| 196 |
int i_height; |
|---|
| 197 |
int i_aspect; |
|---|
| 198 |
int result = 0; |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
if( *p_demux->psz_access == '\0' ) |
|---|
| 202 |
return VLC_EGENERIC; |
|---|
| 203 |
|
|---|
| 204 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
p_demux->pf_demux = Demux; |
|---|
| 208 |
p_demux->pf_control = Control; |
|---|
| 209 |
p_demux->info.i_update = 0; |
|---|
| 210 |
p_demux->info.i_title = 0; |
|---|
| 211 |
p_demux->info.i_seekpoint = 0; |
|---|
| 212 |
|
|---|
| 213 |
p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) ); |
|---|
| 214 |
if( !p_sys ) return VLC_ENOMEM; |
|---|
| 215 |
|
|---|
| 216 |
memset( p_sys, 0, sizeof( demux_sys_t ) ); |
|---|
| 217 |
memset( &fmt, 0, sizeof( es_format_t ) ); |
|---|
| 218 |
|
|---|
| 219 |
QTCaptureDeviceInput * input = nil; |
|---|
| 220 |
NSError *o_returnedError; |
|---|
| 221 |
|
|---|
| 222 |
p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo]; |
|---|
| 223 |
if( !p_sys->device ) |
|---|
| 224 |
{ |
|---|
| 225 |
intf_UserFatal( p_demux, true, _("No Input device found"), |
|---|
| 226 |
_("Your Mac does not seem to be equipped with a suitable input device. " |
|---|
| 227 |
"Please check your connectors and drivers.") ); |
|---|
| 228 |
msg_Err( p_demux, "Can't find any Video device" ); |
|---|
| 229 |
|
|---|
| 230 |
goto error; |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
if( ![p_sys->device open: &o_returnedError] ) |
|---|
| 234 |
{ |
|---|
| 235 |
msg_Err( p_demux, "Unable to open the capture device (%i)", [o_returnedError code] ); |
|---|
| 236 |
goto error; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
if( [p_sys->device isInUseByAnotherApplication] == YES ) |
|---|
| 240 |
{ |
|---|
| 241 |
msg_Err( p_demux, "default capture device is exclusively in use by another application" ); |
|---|
| 242 |
goto error; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device]; |
|---|
| 246 |
if( !input ) |
|---|
| 247 |
{ |
|---|
| 248 |
msg_Err( p_demux, "can't create a valid capture input facility" ); |
|---|
| 249 |
goto error; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
p_sys->output = [[VLCDecompressedVideoOutput alloc] init]; |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
NSArray *format_array = [p_sys->device formatDescriptions]; |
|---|
| 256 |
QTFormatDescription* camera_format = NULL; |
|---|
| 257 |
for( int k=0; k < [format_array count]; k++ ) |
|---|
| 258 |
{ |
|---|
| 259 |
camera_format = [format_array objectAtIndex: k]; |
|---|
| 260 |
|
|---|
| 261 |
NSLog( [camera_format localizedFormatSummary] ); |
|---|
| 262 |
NSLog( [[camera_format formatDescriptionAttributes] description] ); |
|---|
| 263 |
} |
|---|
| 264 |
if( [format_array count] ) |
|---|
| 265 |
camera_format = [format_array objectAtIndex: 0]; |
|---|
| 266 |
else goto error; |
|---|
| 267 |
|
|---|
| 268 |
int qtchroma = [camera_format formatType]; |
|---|
| 269 |
int chroma = qtchroma_to_fourcc( qtchroma ); |
|---|
| 270 |
if( !chroma ) |
|---|
| 271 |
{ |
|---|
| 272 |
msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma ); |
|---|
| 273 |
goto error; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
es_format_Init( &fmt, VIDEO_ES, chroma ); |
|---|
| 278 |
|
|---|
| 279 |
NSSize encoded_size = [[camera_format attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue]; |
|---|
| 280 |
NSSize display_size = [[camera_format attributeForKey:QTFormatDescriptionVideoCleanApertureDisplaySizeAttribute] sizeValue]; |
|---|
| 281 |
NSSize par_size = [[camera_format attributeForKey:QTFormatDescriptionVideoProductionApertureDisplaySizeAttribute] sizeValue]; |
|---|
| 282 |
|
|---|
| 283 |
fmt.video.i_width = p_sys->width = encoded_size.width; |
|---|
| 284 |
fmt.video.i_height = p_sys->height = encoded_size.height; |
|---|
| 285 |
if( par_size.width != encoded_size.width ) |
|---|
| 286 |
{ |
|---|
| 287 |
fmt.video.i_aspect = par_size.width * VOUT_ASPECT_FACTOR / encoded_size.width ; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
NSLog( @"encoded_size %d %d", (int)encoded_size.width, (int)encoded_size.height ); |
|---|
| 291 |
NSLog( @"display_size %d %d", (int)display_size.width, (int)display_size.height ); |
|---|
| 292 |
NSLog( @"PAR size %d %d", (int)par_size.width, (int)par_size.height ); |
|---|
| 293 |
|
|---|
| 294 |
[p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys: |
|---|
| 295 |
[NSNumber numberWithInt: p_sys->height], kCVPixelBufferHeightKey, |
|---|
| 296 |
[NSNumber numberWithInt: p_sys->width], kCVPixelBufferWidthKey, |
|---|
| 297 |
[NSNumber numberWithBool:YES], (id)kCVPixelBufferOpenGLCompatibilityKey, |
|---|
| 298 |
nil]]; |
|---|
| 299 |
|
|---|
| 300 |
p_sys->session = [[QTCaptureSession alloc] init]; |
|---|
| 301 |
|
|---|
| 302 |
bool ret = [p_sys->session addInput:input error: &o_returnedError]; |
|---|
| 303 |
if( !ret ) |
|---|
| 304 |
{ |
|---|
| 305 |
msg_Err( p_demux, "default video capture device could not be added to capture session (%i)", [o_returnedError code] ); |
|---|
| 306 |
goto error; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
ret = [p_sys->session addOutput:p_sys->output error: &o_returnedError]; |
|---|
| 310 |
if( !ret ) |
|---|
| 311 |
{ |
|---|
| 312 |
msg_Err( p_demux, "output could not be added to capture session (%i)", [o_returnedError code] ); |
|---|
| 313 |
goto error; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
[p_sys->session startRunning]; |
|---|
| 317 |
|
|---|
| 318 |
msg_Dbg( p_demux, "added new video es %4.4s %dx%d", |
|---|
| 319 |
(char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height ); |
|---|
| 320 |
|
|---|
| 321 |
p_sys->p_es_video = es_out_Add( p_demux->out, &fmt ); |
|---|
| 322 |
|
|---|
| 323 |
[input release]; |
|---|
| 324 |
[pool release]; |
|---|
| 325 |
|
|---|
| 326 |
msg_Dbg( p_demux, "QTCapture: We have a video device ready!" ); |
|---|
| 327 |
|
|---|
| 328 |
return VLC_SUCCESS; |
|---|
| 329 |
error: |
|---|
| 330 |
[input release]; |
|---|
| 331 |
[pool release]; |
|---|
| 332 |
|
|---|
| 333 |
free( p_sys ); |
|---|
| 334 |
|
|---|
| 335 |
return VLC_EGENERIC; |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
static void Close( vlc_object_t *p_this ) |
|---|
| 342 |
{ |
|---|
| 343 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 344 |
|
|---|
| 345 |
demux_t *p_demux = (demux_t*)p_this; |
|---|
| 346 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
if( vlc_object_alive(p_this->p_libvlc)) |
|---|
| 352 |
{ |
|---|
| 353 |
[p_sys->session stopRunning]; |
|---|
| 354 |
[p_sys->output release]; |
|---|
| 355 |
[p_sys->session release]; |
|---|
| 356 |
} |
|---|
| 357 |
free( p_sys ); |
|---|
| 358 |
|
|---|
| 359 |
[pool release]; |
|---|
| 360 |
} |
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
static int Demux( demux_t *p_demux ) |
|---|
| 367 |
{ |
|---|
| 368 |
demux_sys_t *p_sys = p_demux->p_sys; |
|---|
| 369 |
block_t *p_block; |
|---|
| 370 |
|
|---|
| 371 |
p_block = block_New( p_demux, p_sys->width * |
|---|
| 372 |
p_sys->height * 2 ); |
|---|
| 373 |
if( !p_block ) |
|---|
| 374 |
{ |
|---|
| 375 |
msg_Err( p_demux, "cannot get block" ); |
|---|
| 376 |
return 0; |
|---|
| 377 |
} |
|---|
| 378 |
|
|---|
| 379 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 380 |
|
|---|
| 381 |
@synchronized (p_sys->output) |
|---|
| 382 |
{ |
|---|
| 383 |
p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer]; |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
if( !p_block->i_pts ) |
|---|
| 387 |
{ |
|---|
| 388 |
|
|---|
| 389 |
block_Release( p_block ); |
|---|
| 390 |
[pool release]; |
|---|
| 391 |
msleep( 10000 ); |
|---|
| 392 |
return 1; |
|---|
| 393 |
} |
|---|
| 394 |
|
|---|
| 395 |
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts ); |
|---|
| 396 |
es_out_Send( p_demux->out, p_sys->p_es_video, p_block ); |
|---|
| 397 |
|
|---|
| 398 |
[pool release]; |
|---|
| 399 |
return 1; |
|---|
| 400 |
} |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
static int Control( demux_t *p_demux, int i_query, va_list args ) |
|---|
| 406 |
{ |
|---|
| 407 |
bool *pb; |
|---|
| 408 |
int64_t *pi64; |
|---|
| 409 |
|
|---|
| 410 |
switch( i_query ) |
|---|
| 411 |
{ |
|---|
| 412 |
|
|---|
| 413 |
case DEMUX_CAN_PAUSE: |
|---|
| 414 |
case DEMUX_CAN_SEEK: |
|---|
| 415 |
case DEMUX_SET_PAUSE_STATE: |
|---|
| 416 |
case DEMUX_CAN_CONTROL_PACE: |
|---|
| 417 |
pb = (bool*)va_arg( args, bool * ); |
|---|
| 418 |
*pb = false; |
|---|
| 419 |
return VLC_SUCCESS; |
|---|
| 420 |
|
|---|
| 421 |
case DEMUX_GET_PTS_DELAY: |
|---|
| 422 |
pi64 = (int64_t*)va_arg( args, int64_t * ); |
|---|
| 423 |
*pi64 = (int64_t)DEFAULT_PTS_DELAY; |
|---|
| 424 |
return VLC_SUCCESS; |
|---|
| 425 |
|
|---|
| 426 |
default: |
|---|
| 427 |
return VLC_EGENERIC; |
|---|
| 428 |
} |
|---|
| 429 |
return VLC_EGENERIC; |
|---|
| 430 |
} |
|---|