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 *****************************************************************************/
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
37 #include <vlc_demux.h>
39 #import <QTKit/QTKit.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open( vlc_object_t *p_this );
45 static void Close( vlc_object_t *p_this );
46 static int Demux( demux_t *p_demux );
47 static int Control( demux_t *, int, va_list );
49 /*****************************************************************************
51 *****************************************************************************/
53 set_shortname( N_("Quicktime Capture") );
54 set_description( N_("Quicktime Capture") );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_ACCESS );
57 add_shortcut( "qtcapture" );
58 set_capability( "access_demux", 10 );
59 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
68 CVImageBufferRef currentImageBuffer;
72 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
73 - (BOOL)copyCurrentFrameToBuffer:(void *)buffer;
76 /* Apple sample code */
77 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
80 if( self = [super init] )
82 currentImageBuffer = nil;
89 @synchronized (self) {
90 CVBufferRelease(currentImageBuffer);
91 currentImageBuffer = nil;
96 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
98 // Store the latest frame
99 // This must be done in a @synchronized block because this delegate method is not called on the main thread
100 CVImageBufferRef imageBufferToRelease;
102 CVBufferRetain(videoFrame);
104 @synchronized (self) {
105 imageBufferToRelease = currentImageBuffer;
106 currentImageBuffer = videoFrame;
107 /* FIXME: is it the right PTS? */
108 currentPts = [sampleBuffer presentationTime].timeValue * 1000;
110 CVBufferRelease(imageBufferToRelease);
113 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
115 CVImageBufferRef imageBuffer;
117 @synchronized (self) {
118 if(!currentImageBuffer) return 0;
119 imageBuffer = CVBufferRetain(currentImageBuffer);
123 CVPixelBufferLockBaseAddress(imageBuffer, 0);
124 void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
125 memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
126 CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
128 CVBufferRelease(imageBuffer);
135 /*****************************************************************************
137 *****************************************************************************/
140 QTCaptureSession * session;
141 VLCDecompressedVideoOutput * output;
143 es_out_id_t * p_es_video;
147 /*****************************************************************************
149 *****************************************************************************/
150 static int qtchroma_to_fourcc( int i_qt )
156 } qtchroma_to_fourcc[] =
159 { k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') },
163 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
165 if( qtchroma_to_fourcc[i].i_qt == i_qt )
166 return qtchroma_to_fourcc[i].i_fourcc;
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
177 demux_sys_t *p_sys = NULL;
186 p_demux->pf_demux = Demux;
187 p_demux->pf_control = Control;
188 p_demux->info.i_update = 0;
189 p_demux->info.i_title = 0;
190 p_demux->info.i_seekpoint = 0;
192 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
194 msg_Dbg( p_demux, "QTCapture Probed" );
196 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
197 if( !p_sys ) return VLC_ENOMEM;
199 memset( p_sys, 0, sizeof( demux_sys_t ) );
200 memset( &fmt, 0, sizeof( es_format_t ) );
202 QTCaptureDeviceInput * input = nil;
203 QTCaptureSession * session = nil;
204 VLCDecompressedVideoOutput * output = nil;
206 QTCaptureDevice * device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
209 msg_Err( p_demux, "Can't open any Video device" );
213 if( ![device open: nil /* FIXME */] )
215 msg_Err( p_demux, "Can't open any Video device" );
219 input = [[QTCaptureDeviceInput alloc] initWithDevice: device];
222 msg_Err( p_demux, "Can't create a capture session" );
226 output = [[VLCDecompressedVideoOutput alloc] init];
228 session = [[QTCaptureSession alloc] init];
230 bool ret = [session addInput:input error:nil /* FIXME */];
233 msg_Err( p_demux, "Can't add the video device as input" );
237 ret = [session addOutput:output error:nil /* FIXME */];
240 msg_Err( p_demux, "Can't get any output output" );
244 [session startRunning];
246 int qtchroma = [[[device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
247 int chroma = qtchroma_to_fourcc( qtchroma );
250 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
254 es_format_Init( &fmt, VIDEO_ES, chroma );
256 NSSize size = [[device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
257 p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
258 p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
260 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
261 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
263 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
265 p_sys->output = [output retain];
266 p_sys->session = [session retain];
273 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
288 /*****************************************************************************
290 *****************************************************************************/
291 static void Close( vlc_object_t *p_this )
293 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
295 demux_t *p_demux = (demux_t*)p_this;
296 demux_sys_t *p_sys = p_demux->p_sys;
297 [p_sys->output release];
298 [p_sys->session release];
305 /*****************************************************************************
307 *****************************************************************************/
308 static int Demux( demux_t *p_demux )
310 demux_sys_t *p_sys = p_demux->p_sys;
313 p_block = block_New( p_demux, p_sys->width *
314 p_sys->height * 2 /* FIXME */ );
317 msg_Err( p_demux, "cannot get block" );
321 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
323 p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
325 if( !p_block->i_pts )
327 /* Nothing to display yet, just forget */
328 block_Release( p_block );
333 p_block->i_pts += mdate();
335 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
336 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
342 /*****************************************************************************
344 *****************************************************************************/
345 static int Control( demux_t *p_demux, int i_query, va_list args )
352 /* Special for access_demux */
353 case DEMUX_CAN_PAUSE:
355 case DEMUX_SET_PAUSE_STATE:
356 case DEMUX_CAN_CONTROL_PACE:
357 pb = (bool*)va_arg( args, bool * );
361 case DEMUX_GET_PTS_DELAY:
362 pi64 = (int64_t*)va_arg( args, int64_t * );
363 *pi64 = (int64_t)DEFAULT_PTS_DELAY;
367 pi64 = (int64_t*)va_arg( args, int64_t * );
371 /* TODO implement others */