]> git.sesse.net Git - vlc/blob - modules/access/qtcapture.m
Don't double free objects on error. Pointed by Pierre
[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_common.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     mtime_t currentPts;
70 }
71 - (id)init;
72 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
73 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer;
74 @end
75
76 /* Apple sample code */
77 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
78 - (id)init
79 {
80     if( self = [super init] )
81     {
82         currentImageBuffer = nil;
83         currentPts = 0;
84     }
85     return self;
86 }
87 - (void)dealloc
88 {
89     @synchronized (self)
90     {
91         CVBufferRelease(currentImageBuffer);
92         currentImageBuffer = nil;
93     }
94     [super dealloc];
95 }
96
97 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
98 {
99     // Store the latest frame
100     // This must be done in a @synchronized block because this delegate method is not called on the main thread
101     CVImageBufferRef imageBufferToRelease;
102
103     CVBufferRetain(videoFrame);
104
105     @synchronized (self)
106     {
107         imageBufferToRelease = currentImageBuffer;
108         currentImageBuffer = videoFrame;
109         currentPts = [sampleBuffer presentationTime].timeValue;
110     }
111     CVBufferRelease(imageBufferToRelease);
112 }
113
114 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
115 {
116     CVImageBufferRef imageBuffer;
117     mtime_t pts;
118
119     if(!currentImageBuffer)
120         return 0;
121
122     @synchronized (self)
123     {
124         imageBuffer = CVBufferRetain(currentImageBuffer);
125         pts = currentPts;
126
127         CVPixelBufferLockBaseAddress(imageBuffer, 0);
128         void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
129         memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
130         CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
131     }
132
133     CVBufferRelease(imageBuffer);
134
135     return currentPts;
136 }
137
138 @end
139
140 /*****************************************************************************
141 * Struct
142 *****************************************************************************/
143
144 struct demux_sys_t {
145     QTCaptureSession * session;
146     QTCaptureDevice * device;
147     VLCDecompressedVideoOutput * output;
148     int height, width;
149     es_out_id_t * p_es_video;
150 };
151
152
153 /*****************************************************************************
154 * qtchroma_to_fourcc
155 *****************************************************************************/
156 static int qtchroma_to_fourcc( int i_qt )
157 {
158     static struct
159     {
160         unsigned int i_qt;
161         int i_fourcc;
162     } qtchroma_to_fourcc[] =
163     {
164         /* Raw data types */
165         { k422YpCbCr8CodecType,    VLC_FOURCC('U','Y','V','Y') },
166         { 0, 0 }
167     };
168     int i;
169     for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
170     {
171         if( qtchroma_to_fourcc[i].i_qt == i_qt )
172             return qtchroma_to_fourcc[i].i_fourcc;
173     }
174     return 0;
175 }
176
177 /*****************************************************************************
178 * Open:
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
181 {
182     demux_t     *p_demux = (demux_t*)p_this;
183     demux_sys_t *p_sys = NULL;
184     es_format_t fmt;
185     int i;
186     int i_width;
187     int i_height;
188     int i_aspect;
189     int result = 0;
190
191     /* Only when selected */
192     if( *p_demux->psz_access == '\0' )
193         return VLC_EGENERIC;
194     
195     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
196
197     /* Set up p_demux */
198     p_demux->pf_demux = Demux;
199     p_demux->pf_control = Control;
200     p_demux->info.i_update = 0;
201     p_demux->info.i_title = 0;
202     p_demux->info.i_seekpoint = 0;
203     
204     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
205     if( !p_sys ) return VLC_ENOMEM;
206     
207     memset( p_sys, 0, sizeof( demux_sys_t ) );
208     memset( &fmt, 0, sizeof( es_format_t ) );    
209     
210     msg_Dbg( p_demux, "QTCapture Probed" );
211
212     QTCaptureDeviceInput * input = nil;
213
214     p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
215     if( !p_sys->device )
216     {
217         msg_Err( p_demux, "Can't find any Video device" );
218         goto error;
219     }
220
221     if( ![p_sys->device open: nil  /* FIXME */] )
222     {
223         msg_Err( p_demux, "Can't open any Video device" );
224         goto error;
225     }
226
227     input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
228     if( !p_sys->device )
229     {
230         msg_Err( p_demux, "Can't create a capture session" );
231         goto error;
232     }
233
234     p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
235
236     /* Hack - This will lower CPU consumption for some reason */
237     [p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
238         [NSNumber numberWithInt:480], kCVPixelBufferHeightKey,
239         [NSNumber numberWithInt:640], kCVPixelBufferWidthKey, nil]];
240
241     p_sys->session = [[QTCaptureSession alloc] init];
242
243     bool ret = [p_sys->session addInput:input error:nil  /* FIXME */];
244     if( !ret )
245     {
246         msg_Err( p_demux, "Can't add the video device as input" );
247         goto error;
248     }
249
250     ret = [p_sys->session addOutput:p_sys->output error:nil  /* FIXME */];
251     if( !ret )
252     {
253         msg_Err( p_demux, "Can't get any output output" );
254         goto error;
255     }
256
257     [p_sys->session startRunning];
258
259
260     int qtchroma = [[[p_sys->device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
261     int chroma = qtchroma_to_fourcc( qtchroma );
262     if( !chroma )
263     {
264         msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
265         goto error;
266     }
267
268     /* Now we can init */
269
270     es_format_Init( &fmt, VIDEO_ES, chroma );
271
272     NSSize size = [[p_sys->device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
273     p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
274     p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
275
276     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
277             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
278
279     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
280
281     [input release];
282     [pool release];
283
284     msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
285
286     return VLC_SUCCESS;
287 error:
288     [input release];
289     [pool release];
290
291     free( p_sys );
292
293     return VLC_EGENERIC;
294 }
295
296 /*****************************************************************************
297 * Close:
298 *****************************************************************************/
299 static void Close( vlc_object_t *p_this )
300 {
301     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
302
303     demux_t     *p_demux = (demux_t*)p_this;
304     demux_sys_t *p_sys = p_demux->p_sys;
305
306     [p_sys->session stopRunning];
307     [p_sys->output release];
308     [p_sys->session release];
309     [p_sys->device release];
310     free( p_sys );
311
312     [pool release];
313 }
314
315
316 /*****************************************************************************
317 * Demux:
318 *****************************************************************************/
319 static int Demux( demux_t *p_demux )
320 {
321     demux_sys_t *p_sys = p_demux->p_sys;
322     block_t *p_block;
323
324     p_block = block_New( p_demux, p_sys->width *
325                             p_sys->height * 2 /* FIXME */ );
326     if( !p_block )
327     {
328         msg_Err( p_demux, "cannot get block" );
329         return 0;
330     }
331
332     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
333
334     @synchronized (p_sys->output)
335     {
336     p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
337     }
338
339     if( !p_block->i_pts )
340     {
341         /* Nothing to display yet, just forget */
342         block_Release( p_block );
343         [pool release];
344         return 1;
345     }
346
347     /* FIXME */
348     p_block->i_pts = mdate();
349
350     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
351     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
352
353     [pool release];
354     return 1;
355 }
356
357 /*****************************************************************************
358 * Control:
359 *****************************************************************************/
360 static int Control( demux_t *p_demux, int i_query, va_list args )
361 {
362     bool *pb;
363     int64_t    *pi64;
364
365     switch( i_query )
366     {
367         /* Special for access_demux */
368         case DEMUX_CAN_PAUSE:
369         case DEMUX_CAN_SEEK:
370         case DEMUX_SET_PAUSE_STATE:
371         case DEMUX_CAN_CONTROL_PACE:
372            pb = (bool*)va_arg( args, bool * );
373            *pb = false;
374            return VLC_SUCCESS;
375
376         case DEMUX_GET_PTS_DELAY:
377            pi64 = (int64_t*)va_arg( args, int64_t * );
378            *pi64 = (int64_t)DEFAULT_PTS_DELAY;
379            return VLC_SUCCESS;
380
381         case DEMUX_GET_TIME:
382            pi64 = (int64_t*)va_arg( args, int64_t * );
383            *pi64 = mdate();
384            return VLC_SUCCESS;
385
386         /* TODO implement others */
387         default:
388            return VLC_EGENERIC;
389     }
390     return VLC_EGENERIC;
391 }