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>
37 #include <vlc_demux.h>
38 #include <vlc_interface.h>
40 #import <QTKit/QTKit.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open( vlc_object_t *p_this );
46 static void Close( vlc_object_t *p_this );
47 static int Demux( demux_t *p_demux );
48 static int Control( demux_t *, int, va_list );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_shortname( N_("Quicktime Capture") );
55 set_description( N_("Quicktime Capture") );
56 set_category( CAT_INPUT );
57 set_subcategory( SUBCAT_INPUT_ACCESS );
58 add_shortcut( "qtcapture" );
59 set_capability( "access_demux", 10 );
60 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
67 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
69 CVImageBufferRef currentImageBuffer;
73 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
74 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer;
77 /* Apple sample code */
78 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
81 if( self = [super init] )
83 currentImageBuffer = nil;
92 CVBufferRelease(currentImageBuffer);
93 currentImageBuffer = nil;
98 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
100 // Store the latest frame
101 // This must be done in a @synchronized block because this delegate method is not called on the main thread
102 CVImageBufferRef imageBufferToRelease;
104 CVBufferRetain(videoFrame);
108 imageBufferToRelease = currentImageBuffer;
109 currentImageBuffer = videoFrame;
110 currentPts = [sampleBuffer presentationTime].timeValue;
112 CVBufferRelease(imageBufferToRelease);
115 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
117 CVImageBufferRef imageBuffer;
120 if(!currentImageBuffer)
125 imageBuffer = CVBufferRetain(currentImageBuffer);
128 CVPixelBufferLockBaseAddress(imageBuffer, 0);
129 void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
130 memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
131 CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
134 CVBufferRelease(imageBuffer);
141 /*****************************************************************************
143 *****************************************************************************/
146 QTCaptureSession * session;
147 QTCaptureDevice * device;
148 VLCDecompressedVideoOutput * output;
150 es_out_id_t * p_es_video;
154 /*****************************************************************************
156 *****************************************************************************/
157 static int qtchroma_to_fourcc( int i_qt )
163 } qtchroma_to_fourcc[] =
166 { k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') },
170 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
172 if( qtchroma_to_fourcc[i].i_qt == i_qt )
173 return qtchroma_to_fourcc[i].i_fourcc;
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 demux_t *p_demux = (demux_t*)p_this;
184 demux_sys_t *p_sys = NULL;
192 /* Only when selected */
193 if( *p_demux->psz_access == '\0' )
196 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
199 p_demux->pf_demux = Demux;
200 p_demux->pf_control = Control;
201 p_demux->info.i_update = 0;
202 p_demux->info.i_title = 0;
203 p_demux->info.i_seekpoint = 0;
205 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
206 if( !p_sys ) return VLC_ENOMEM;
208 memset( p_sys, 0, sizeof( demux_sys_t ) );
209 memset( &fmt, 0, sizeof( es_format_t ) );
211 msg_Dbg( p_demux, "QTCapture Probed" );
213 QTCaptureDeviceInput * input = nil;
215 p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
218 intf_UserFatal( p_demux, true, _("No Input device found"),
219 _("Your Mac does not seem to be equipped with a suitable input device. "
220 "Please check your connectors and drivers.") );
221 msg_Err( p_demux, "Can't find any Video device" );
226 if( ![p_sys->device open: nil /* FIXME */] )
228 msg_Err( p_demux, "Can't open any Video device" );
232 input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
235 msg_Err( p_demux, "Can't create a capture session" );
239 p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
241 /* Hack - This will lower CPU consumption for some reason */
242 [p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
243 [NSNumber numberWithInt:480], kCVPixelBufferHeightKey,
244 [NSNumber numberWithInt:640], kCVPixelBufferWidthKey, nil]];
246 p_sys->session = [[QTCaptureSession alloc] init];
248 bool ret = [p_sys->session addInput:input error:nil /* FIXME */];
251 msg_Err( p_demux, "Can't add the video device as input" );
255 ret = [p_sys->session addOutput:p_sys->output error:nil /* FIXME */];
258 msg_Err( p_demux, "Can't get any output output" );
262 [p_sys->session startRunning];
265 int qtchroma = [[[p_sys->device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
266 int chroma = qtchroma_to_fourcc( qtchroma );
269 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
273 /* Now we can init */
275 es_format_Init( &fmt, VIDEO_ES, chroma );
277 NSSize size = [[p_sys->device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
278 p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
279 p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
281 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
282 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
284 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
289 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
301 /*****************************************************************************
303 *****************************************************************************/
304 static void Close( vlc_object_t *p_this )
306 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
308 demux_t *p_demux = (demux_t*)p_this;
309 demux_sys_t *p_sys = p_demux->p_sys;
311 [p_sys->session stopRunning];
312 [p_sys->output release];
313 [p_sys->session release];
314 [p_sys->device release];
321 /*****************************************************************************
323 *****************************************************************************/
324 static int Demux( demux_t *p_demux )
326 demux_sys_t *p_sys = p_demux->p_sys;
329 p_block = block_New( p_demux, p_sys->width *
330 p_sys->height * 2 /* FIXME */ );
333 msg_Err( p_demux, "cannot get block" );
337 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
339 @synchronized (p_sys->output)
341 p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
344 if( !p_block->i_pts )
346 /* Nothing to display yet, just forget */
347 block_Release( p_block );
353 p_block->i_pts = mdate();
355 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
356 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
362 /*****************************************************************************
364 *****************************************************************************/
365 static int Control( demux_t *p_demux, int i_query, va_list args )
372 /* Special for access_demux */
373 case DEMUX_CAN_PAUSE:
375 case DEMUX_SET_PAUSE_STATE:
376 case DEMUX_CAN_CONTROL_PACE:
377 pb = (bool*)va_arg( args, bool * );
381 case DEMUX_GET_PTS_DELAY:
382 pi64 = (int64_t*)va_arg( args, int64_t * );
383 *pi64 = (int64_t)DEFAULT_PTS_DELAY;
387 pi64 = (int64_t*)va_arg( args, int64_t * );
391 /* TODO implement others */