1 /*****************************************************************************
2 * qtcapture.m: qtkit (Mac OS X) based capture module
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
6 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
8 *****************************************************************************
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_demux.h>
37 #include <vlc_interface.h>
38 #include <vlc_dialog.h>
40 #import <QTKit/QTKit.h>
41 #import <CoreAudio/CoreAudio.h>
43 /*****************************************************************************
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 );
51 /*****************************************************************************
53 *****************************************************************************/
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 )
65 /*****************************************************************************
67 *****************************************************************************/
68 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
70 CVImageBufferRef currentImageBuffer;
75 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
76 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer;
79 /* Apple sample code */
80 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
83 if( self = [super init] )
85 currentImageBuffer = nil;
95 CVBufferRelease(currentImageBuffer);
96 currentImageBuffer = nil;
101 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
103 // Store the latest frame
104 // This must be done in a @synchronized block because this delegate method is not called on the main thread
105 CVImageBufferRef imageBufferToRelease;
107 CVBufferRetain(videoFrame);
111 imageBufferToRelease = currentImageBuffer;
112 currentImageBuffer = videoFrame;
113 currentPts = (mtime_t)(1000000L / [sampleBuffer presentationTime].timeScale * [sampleBuffer presentationTime].timeValue);
115 /* Try to use hosttime of the sample if available, because iSight Pts seems broken */
116 NSNumber *hosttime = (NSNumber *)[sampleBuffer attributeForKey:QTSampleBufferHostTimeAttribute];
117 if( hosttime ) currentPts = (mtime_t)AudioConvertHostTimeToNanos([hosttime unsignedLongLongValue])/1000;
119 CVBufferRelease(imageBufferToRelease);
122 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
124 CVImageBufferRef imageBuffer;
127 if(!currentImageBuffer || currentPts == previousPts )
132 imageBuffer = CVBufferRetain(currentImageBuffer);
133 pts = previousPts = currentPts;
135 CVPixelBufferLockBaseAddress(imageBuffer, 0);
136 void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
137 memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
138 CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
141 CVBufferRelease(imageBuffer);
148 /*****************************************************************************
150 *****************************************************************************/
153 QTCaptureSession * session;
154 QTCaptureDevice * device;
155 VLCDecompressedVideoOutput * output;
157 es_out_id_t * p_es_video;
161 /*****************************************************************************
163 *****************************************************************************/
164 static int qtchroma_to_fourcc( int i_qt )
170 } qtchroma_to_fourcc[] =
173 { '2vuy', VLC_CODEC_UYVY },
174 { 'yuv2',VLC_CODEC_YUYV },
175 { 'yuvs', VLC_CODEC_YUYV },
179 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
181 if( qtchroma_to_fourcc[i].i_qt == i_qt )
182 return qtchroma_to_fourcc[i].i_fourcc;
187 /*****************************************************************************
189 *****************************************************************************/
190 static int Open( vlc_object_t *p_this )
192 demux_t *p_demux = (demux_t*)p_this;
193 demux_sys_t *p_sys = NULL;
200 /* Only when selected */
201 if( *p_demux->psz_access == '\0' )
204 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
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;
213 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
217 memset( &fmt, 0, sizeof( es_format_t ) );
219 QTCaptureDeviceInput * input = nil;
220 NSError *o_returnedError;
222 p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
225 dialog_FatalWait( p_demux, _("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" );
233 if( ![p_sys->device open: &o_returnedError] )
235 msg_Err( p_demux, "Unable to open the capture device (%ld)", [o_returnedError code] );
239 if( [p_sys->device isInUseByAnotherApplication] == YES )
241 msg_Err( p_demux, "default capture device is exclusively in use by another application" );
245 input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
248 msg_Err( p_demux, "can't create a valid capture input facility" );
252 p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
254 /* Get the formats */
255 NSArray *format_array = [p_sys->device formatDescriptions];
256 QTFormatDescription* camera_format = NULL;
257 for( int k = 0; k < [format_array count]; k++ )
259 camera_format = [format_array objectAtIndex: k];
261 NSLog( @"%@", [camera_format localizedFormatSummary] );
262 NSLog( @"%@",[[camera_format formatDescriptionAttributes] description] );
264 if( [format_array count] )
265 camera_format = [format_array objectAtIndex: 0];
268 int qtchroma = [camera_format formatType];
269 int chroma = qtchroma_to_fourcc( qtchroma );
272 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
276 /* Now we can init */
277 es_format_Init( &fmt, VIDEO_ES, chroma );
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];
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 )
287 fmt.video.i_sar_num = (int64_t)encoded_size.height * par_size.width / encoded_size.width;
288 fmt.video.i_sar_den = encoded_size.width;
291 NSLog( @"encoded_size %d %d", (int)encoded_size.width, (int)encoded_size.height );
292 NSLog( @"display_size %d %d", (int)display_size.width, (int)display_size.height );
293 NSLog( @"PAR size %d %d", (int)par_size.width, (int)par_size.height );
295 [p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
296 [NSNumber numberWithInt: p_sys->height], kCVPixelBufferHeightKey,
297 [NSNumber numberWithInt: p_sys->width], kCVPixelBufferWidthKey,
298 [NSNumber numberWithBool:YES], (id)kCVPixelBufferOpenGLCompatibilityKey,
301 p_sys->session = [[QTCaptureSession alloc] init];
303 bool ret = [p_sys->session addInput:input error: &o_returnedError];
306 msg_Err( p_demux, "default video capture device could not be added to capture session (%ld)", [o_returnedError code] );
310 ret = [p_sys->session addOutput:p_sys->output error: &o_returnedError];
313 msg_Err( p_demux, "output could not be added to capture session (%ld)", [o_returnedError code] );
317 [p_sys->session startRunning];
319 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
320 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
322 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
327 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
339 /*****************************************************************************
341 *****************************************************************************/
342 static void Close( vlc_object_t *p_this )
344 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
346 demux_t *p_demux = (demux_t*)p_this;
347 demux_sys_t *p_sys = p_demux->p_sys;
349 /* Hack: if libvlc was killed, main interface thread was,
350 * and poor QTKit needs it, so don't tell him.
351 * Else we dead lock. */
352 if( vlc_object_alive(p_this->p_libvlc))
354 // Perform this on main thread, as the framework itself will sometimes try to synchronously
355 // work on main thread. And this will create a dead lock.
356 [p_sys->session performSelectorOnMainThread:@selector(stopRunning) withObject:nil waitUntilDone:NO];
357 [p_sys->output performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
358 [p_sys->session performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
366 /*****************************************************************************
368 *****************************************************************************/
369 static int Demux( demux_t *p_demux )
371 demux_sys_t *p_sys = p_demux->p_sys;
374 p_block = block_New( p_demux, p_sys->width *
375 p_sys->height * 2 /* FIXME */ );
378 msg_Err( p_demux, "cannot get block" );
382 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
384 @synchronized (p_sys->output)
386 p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
389 if( !p_block->i_pts )
391 /* Nothing to display yet, just forget */
392 block_Release( p_block );
398 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
399 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
405 /*****************************************************************************
407 *****************************************************************************/
408 static int Control( demux_t *p_demux, int i_query, va_list args )
415 /* Special for access_demux */
416 case DEMUX_CAN_PAUSE:
418 case DEMUX_SET_PAUSE_STATE:
419 case DEMUX_CAN_CONTROL_PACE:
420 pb = (bool*)va_arg( args, bool * );
424 case DEMUX_GET_PTS_DELAY:
425 pi64 = (int64_t*)va_arg( args, int64_t * );
426 *pi64 = (int64_t)DEFAULT_PTS_DELAY;