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