]> git.sesse.net Git - vlc/blob - modules/access/qtcapture.m
qtcapture: statification and comment.
[vlc] / modules / access / qtcapture.m
1 /*****************************************************************************
2 * qtcapture.m: qtkit (Mac OS X) based capture module
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
5 *
6 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
7 *
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.
13 *
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.
18 *
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
22 *
23 *****************************************************************************/
24
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_vout.h>
37 #include <vlc_demux.h>
38
39 #import <QTKit/QTKit.h>
40
41 /*****************************************************************************
42 * Local prototypes
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 );
48
49 /*****************************************************************************
50 * Module descriptor
51 *****************************************************************************/
52 vlc_module_begin();
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 );
60 vlc_module_end();
61
62
63 /*****************************************************************************
64 * QTKit Bridge
65 *****************************************************************************/
66 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
67 {
68    CVImageBufferRef currentImageBuffer;
69 }
70 - (id)init;
71 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
72 - (BOOL)copyCurrentFrameToBuffer:(void *)buffer;
73 @end
74
75 /* Apple sample code */
76 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
77 - (id)init
78 {
79    if( self = [super init] )
80    {
81        currentImageBuffer = nil;
82    }
83    return self;
84 }
85 - (void)dealloc
86 {
87    @synchronized (self) {
88        CVBufferRelease(currentImageBuffer);
89        currentImageBuffer = nil;
90    }
91    [super dealloc];
92 }
93
94 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
95 {
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;
99
100    CVBufferRetain(videoFrame);
101
102    @synchronized (self) {
103        imageBufferToRelease = currentImageBuffer;
104        currentImageBuffer = videoFrame;
105    }
106    CVBufferRelease(imageBufferToRelease);
107 }
108
109 - (BOOL)copyCurrentFrameToBuffer:(void *)buffer
110 {
111    CVImageBufferRef imageBuffer;
112
113    @synchronized (self) {
114        if(!currentImageBuffer) return NO;
115        imageBuffer = CVBufferRetain(currentImageBuffer);
116    }
117
118    CVPixelBufferLockBaseAddress(imageBuffer, 0);
119    void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
120    memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
121    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
122
123    CVBufferRelease(imageBuffer);
124
125    return YES;
126 }
127
128 @end
129
130 /*****************************************************************************
131 * Struct
132 *****************************************************************************/
133
134 struct demux_sys_t {
135    QTCaptureSession * session;
136    VLCDecompressedVideoOutput * output;
137    int height, width;
138    es_out_id_t * p_es_video;
139 };
140
141
142 /*****************************************************************************
143 * qtchroma_to_fourcc
144 *****************************************************************************/
145 static int qtchroma_to_fourcc( int i_qt )
146 {
147     static struct
148     {
149         unsigned int i_qt;
150         int i_fourcc;
151     } qtchroma_to_fourcc[] =
152     {
153         /* Raw data types */
154         { k422YpCbCr8CodecType,    VLC_FOURCC('U','Y','V','Y') },
155         { 0, 0 }
156     };
157     int i;
158     for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
159     {
160         if( qtchroma_to_fourcc[i].i_qt == i_qt )
161             return qtchroma_to_fourcc[i].i_fourcc;
162     }
163     return 0;
164 }
165
166 /*****************************************************************************
167 * Open:
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
170 {
171     demux_t     *p_demux = (demux_t*)p_this;
172     demux_sys_t *p_sys = NULL;
173     es_format_t fmt;
174     int i;
175     int i_width;
176     int i_height;
177     int i_aspect;
178     int result = 0;
179
180     /* Set up p_demux */
181     p_demux->pf_demux = Demux;
182     p_demux->pf_control = Control;
183     p_demux->info.i_update = 0;
184     p_demux->info.i_title = 0;
185     p_demux->info.i_seekpoint = 0;
186
187     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
188
189     msg_Dbg( p_demux, "QTCapture Probed" );
190
191     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
192     if( !p_sys ) return VLC_ENOMEM;
193
194     memset( p_sys, 0, sizeof( demux_sys_t ) );
195     memset( &fmt, 0, sizeof( es_format_t ) );
196
197     QTCaptureDeviceInput * input = nil;
198     QTCaptureSession * session = nil;
199     VLCDecompressedVideoOutput * output = nil;
200
201     QTCaptureDevice * device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
202     if( !device )
203     {
204         msg_Err( p_demux, "Can't open any Video device" );
205         goto error;
206     }
207
208     if( ![device open: nil  /* FIXME */] )
209     {
210         msg_Err( p_demux, "Can't open any Video device" );
211         goto error;
212     }
213
214     input = [[QTCaptureDeviceInput alloc] initWithDevice: device];
215     if( !device )
216     {
217         msg_Err( p_demux, "Can't create a capture session" );
218         goto error;
219     }
220
221     output = [[VLCDecompressedVideoOutput alloc] init];
222
223     session = [[QTCaptureSession alloc] init];
224
225     bool ret = [session addInput:input error:nil  /* FIXME */];
226     if( !ret )
227     {
228         msg_Err( p_demux, "Can't add the video device as input" );
229         goto error;
230     }
231
232     ret = [session addOutput:output error:nil  /* FIXME */];
233     if( !ret )
234     {
235         msg_Err( p_demux, "Can't get any output output" );
236         goto error;
237     }
238
239     [session startRunning];
240
241     int qtchroma = [[[device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
242     int chroma = qtchroma_to_fourcc( qtchroma );
243     if( !chroma )
244     {
245         msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
246         goto error;
247     }
248
249     es_format_Init( &fmt, VIDEO_ES, chroma );
250
251     NSSize size = [[device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
252     p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
253     p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
254
255     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
256             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
257
258     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
259
260     p_sys->output = [output retain];
261     p_sys->session = [session retain];
262
263     [input release];
264     [output release];
265     [session release];
266     [pool release];
267
268     msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
269
270     return VLC_SUCCESS;
271 error:
272     [input release];
273     [session release];
274     [input release];
275     [output release];
276     [pool release];
277
278     free( p_sys );
279
280     return VLC_EGENERIC;
281 }
282
283 /*****************************************************************************
284 * Close:
285 *****************************************************************************/
286 static void Close( vlc_object_t *p_this )
287 {
288     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
289
290     demux_t     *p_demux = (demux_t*)p_this;
291     demux_sys_t *p_sys = p_demux->p_sys;
292     [p_sys->output release];
293     [p_sys->session release];
294     free( p_sys );
295
296     [pool release];
297 }
298
299
300 /*****************************************************************************
301 * Demux:
302 *****************************************************************************/
303 static int Demux( demux_t *p_demux )
304 {
305     demux_sys_t *p_sys = p_demux->p_sys;
306     block_t *p_block;
307
308     p_block = block_New( p_demux, p_sys->width *
309                             p_sys->height * 2 /* FIXME */ );
310     if( !p_block )
311     {
312         msg_Err( p_demux, "cannot get block" );
313         return 0;
314     }
315
316     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
317
318     if( ![p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer] )
319     {
320         /* Nothing to display yet, just forget */
321         block_Release( p_block );
322         [pool release];
323         return 1;
324     }
325
326     p_block->i_pts = mdate(); /* FIXME */
327
328     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
329     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
330
331     [pool release];
332     return 1;
333 }
334
335 /*****************************************************************************
336 * Control:
337 *****************************************************************************/
338 static int Control( demux_t *p_demux, int i_query, va_list args )
339 {
340     bool *pb;
341     int64_t    *pi64;
342
343     switch( i_query )
344     {
345         /* Special for access_demux */
346         case DEMUX_CAN_PAUSE:
347         case DEMUX_CAN_SEEK:
348         case DEMUX_SET_PAUSE_STATE:
349         case DEMUX_CAN_CONTROL_PACE:
350            pb = (bool*)va_arg( args, bool * );
351            *pb = false;
352            return VLC_SUCCESS;
353
354         case DEMUX_GET_PTS_DELAY:
355            pi64 = (int64_t*)va_arg( args, int64_t * );
356            *pi64 = (int64_t)DEFAULT_PTS_DELAY;
357            return VLC_SUCCESS;
358
359         case DEMUX_GET_TIME:
360            pi64 = (int64_t*)va_arg( args, int64_t * );
361            *pi64 = mdate();
362            return VLC_SUCCESS;
363
364         /* TODO implement others */
365         default:
366            return VLC_EGENERIC;
367     }
368     return VLC_EGENERIC;
369 }