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;
71 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
72 - (BOOL)copyCurrentFrameToBuffer:(void *)buffer;
75 /* Apple sample code */
76 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
79 if( self = [super init] )
81 currentImageBuffer = nil;
87 @synchronized (self) {
88 CVBufferRelease(currentImageBuffer);
89 currentImageBuffer = nil;
94 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
96 // Store the latest frame
97 // This must be done in a @synchronized block because this delegate method is not called on the main thread
98 CVImageBufferRef imageBufferToRelease;
100 CVBufferRetain(videoFrame);
102 @synchronized (self) {
103 imageBufferToRelease = currentImageBuffer;
104 currentImageBuffer = videoFrame;
106 CVBufferRelease(imageBufferToRelease);
109 - (BOOL)copyCurrentFrameToBuffer:(void *)buffer
111 CVImageBufferRef imageBuffer;
113 @synchronized (self) {
114 if(!currentImageBuffer) return NO;
115 imageBuffer = CVBufferRetain(currentImageBuffer);
118 CVPixelBufferLockBaseAddress(imageBuffer, 0);
119 void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
120 memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
121 CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
123 CVBufferRelease(imageBuffer);
131 QTCaptureSession * session;
132 VLCDecompressedVideoOutput * output;
134 es_out_id_t * p_es_video;
138 int qtchroma_to_fourcc( int i_qt )
144 } qtchroma_to_fourcc[] =
147 { k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') },
151 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
153 if( qtchroma_to_fourcc[i].i_qt == i_qt )
154 return qtchroma_to_fourcc[i].i_fourcc;
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 demux_t *p_demux = (demux_t*)p_this;
165 demux_sys_t *p_sys = NULL;
174 p_demux->pf_demux = Demux;
175 p_demux->pf_control = Control;
176 p_demux->info.i_update = 0;
177 p_demux->info.i_title = 0;
178 p_demux->info.i_seekpoint = 0;
180 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
182 msg_Dbg( p_demux, "QTCapture Probed" );
184 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
185 if( !p_sys ) return VLC_ENOMEM;
187 memset( p_sys, 0, sizeof( demux_sys_t ) );
188 memset( &fmt, 0, sizeof( es_format_t ) );
190 QTCaptureDeviceInput * input = nil;
191 QTCaptureSession * session = nil;
192 VLCDecompressedVideoOutput * output = nil;
194 QTCaptureDevice * device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
197 msg_Err( p_demux, "Can't open any Video device" );
201 if( ![device open: nil /* FIXME */] )
203 msg_Err( p_demux, "Can't open any Video device" );
207 input = [[QTCaptureDeviceInput alloc] initWithDevice: device];
210 msg_Err( p_demux, "Can't create a capture session" );
214 output = [[VLCDecompressedVideoOutput alloc] init];
216 session = [[QTCaptureSession alloc] init];
218 bool ret = [session addInput:input error:nil /* FIXME */];
221 msg_Err( p_demux, "Can't add the video device as input" );
225 ret = [session addOutput:output error:nil /* FIXME */];
228 msg_Err( p_demux, "Can't get any output output" );
232 [session startRunning];
234 int qtchroma = [[[device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
235 int chroma = qtchroma_to_fourcc( qtchroma );
238 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
242 es_format_Init( &fmt, VIDEO_ES, chroma );
244 NSSize size = [[device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
245 p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
246 p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
248 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
249 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
251 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
253 p_sys->output = [output retain];
254 p_sys->session = [session retain];
261 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
276 /*****************************************************************************
278 *****************************************************************************/
279 static void Close( vlc_object_t *p_this )
281 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
283 demux_t *p_demux = (demux_t*)p_this;
284 demux_sys_t *p_sys = p_demux->p_sys;
285 [p_sys->output release];
286 [p_sys->session release];
293 /*****************************************************************************
295 *****************************************************************************/
296 static int Demux( demux_t *p_demux )
298 demux_sys_t *p_sys = p_demux->p_sys;
301 p_block = block_New( p_demux, p_sys->width *
302 p_sys->height * 2 /* FIXME */ );
305 msg_Err( p_demux, "cannot get block" );
309 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
311 if( ![p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer] )
313 /* Nothing to display yet, just forget */
314 block_Release( p_block );
319 p_block->i_pts = mdate(); /* FIXME */
321 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
322 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
328 /*****************************************************************************
330 *****************************************************************************/
331 static int Control( demux_t *p_demux, int i_query, va_list args )
338 /* Special for access_demux */
339 case DEMUX_CAN_PAUSE:
341 case DEMUX_SET_PAUSE_STATE:
342 case DEMUX_CAN_CONTROL_PACE:
343 pb = (bool*)va_arg( args, bool * );
347 case DEMUX_GET_PTS_DELAY:
348 pi64 = (int64_t*)va_arg( args, int64_t * );
349 *pi64 = (int64_t)DEFAULT_PTS_DELAY;
353 pi64 = (int64_t*)va_arg( args, int64_t * );
357 /* TODO implement others */