]> git.sesse.net Git - vlc/blob - modules/access/qtcapture.m
aad13264dec87d9bbc5eea4eb593465d083d94d5
[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 typedef struct qtcapture_block_t
51 {
52     block_t block;
53     CVImageBufferRef imageBuffer;
54     block_free_t pf_original_release;
55 } qtcapture_block_t;
56
57 static void qtcapture_block_release( block_t *p_block )
58 {
59     qtcapture_block_t * p_qtblock = (qtcapture_block_t *)p_block;
60     CVBufferRelease(p_qtblock->imageBuffer);
61     CVPixelBufferUnlockBaseAddress(p_qtblock->imageBuffer, 0);
62     p_qtblock->pf_original_release( &p_qtblock->block );
63 }
64
65 static block_t * qtcapture_block_new( void * p_buffer,
66     int i_buffer,
67     CVImageBufferRef imageBufferToRelease )
68 {
69     qtcapture_block_t * p_qtblock;
70
71     /* Build block */
72     p_qtblock = malloc( sizeof( qtcapture_block_t ) );
73     if(!p_qtblock) return NULL;
74
75     /* Fill all fields */
76     block_Init( &p_qtblock->block, p_buffer, i_buffer );
77     p_qtblock->block.pf_release = qtcapture_block_release;
78     p_qtblock->imageBuffer = imageBufferToRelease;
79
80     return (block_t *)p_qtblock;
81 }
82
83
84 /*****************************************************************************
85 * Module descriptor
86 *****************************************************************************/
87 vlc_module_begin();
88    set_shortname( N_("Quicktime Capture") );
89    set_description( N_("Quicktime Capture") );
90    set_category( CAT_INPUT );
91    set_subcategory( SUBCAT_INPUT_ACCESS );
92    add_shortcut( "qtcapture" );
93    set_capability( "access_demux", 10 );
94    set_callbacks( Open, Close );
95 vlc_module_end();
96
97
98 /*****************************************************************************
99 * QTKit Bridge
100 *****************************************************************************/
101 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
102 {
103     CVImageBufferRef currentImageBuffer;
104     mtime_t currentPts;
105     mtime_t previousPts;
106 }
107 - (id)init;
108 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
109 - (block_t *)blockWithCurrentFrame;
110 @end
111
112 /* Apple sample code */
113 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
114 - (id)init
115 {
116     if( self = [super init] )
117     {
118         currentImageBuffer = nil;
119         currentPts = 0;
120         previousPts = 0;
121     }
122     return self;
123 }
124 - (void)dealloc
125 {
126     @synchronized (self)
127     {
128         CVBufferRelease(currentImageBuffer);
129         currentImageBuffer = nil;
130     }
131     [super dealloc];
132 }
133
134 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
135 {
136     // Store the latest frame
137     // This must be done in a @synchronized block because this delegate method is not called on the main thread
138     CVImageBufferRef imageBufferToRelease;
139
140     CVBufferRetain(videoFrame);
141
142     @synchronized (self)
143     {
144         imageBufferToRelease = currentImageBuffer;
145         currentImageBuffer = videoFrame;
146         currentPts = 1000000L / [sampleBuffer presentationTime].timeScale * [sampleBuffer presentationTime].timeValue;
147     }
148
149     CVBufferRelease(imageBufferToRelease);
150 }
151
152 - (block_t *)blockWithCurrentFrame
153 {
154     CVImageBufferRef imageBuffer;
155     mtime_t pts;
156     block_t * p_block = NULL;
157
158     if(!currentImageBuffer || currentPts == previousPts )
159         return NULL;
160
161     @synchronized (self)
162     {
163         // Released in the p_block release method.
164         imageBuffer = CVBufferRetain(currentImageBuffer);
165         pts = previousPts = currentPts;
166
167
168         // Unlocked in the p_block release method.
169         CVPixelBufferLockBaseAddress(imageBuffer, 0);
170         void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
171         p_block = qtcapture_block_new( imageBuffer, CVPixelBufferGetDataSize( imageBuffer ),
172                 imageBuffer );
173         p_block->i_pts = currentPts;
174     }
175
176     return p_block;
177 }
178
179 @end
180
181 /*****************************************************************************
182 * Struct
183 *****************************************************************************/
184
185 struct demux_sys_t {
186     QTCaptureSession * session;
187     QTCaptureDevice * device;
188     VLCDecompressedVideoOutput * output;
189     int height, width;
190     es_out_id_t * p_es_video;
191 };
192
193
194 /*****************************************************************************
195 * qtchroma_to_fourcc
196 *****************************************************************************/
197 static int qtchroma_to_fourcc( int i_qt )
198 {
199     static struct
200     {
201         unsigned int i_qt;
202         int i_fourcc;
203     } qtchroma_to_fourcc[] =
204     {
205         /* Raw data types */
206         { k422YpCbCr8CodecType,    VLC_FOURCC('U','Y','V','Y') },
207         { 0, 0 }
208     };
209     int i;
210     for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
211     {
212         if( qtchroma_to_fourcc[i].i_qt == i_qt )
213             return qtchroma_to_fourcc[i].i_fourcc;
214     }
215     return 0;
216 }
217
218 /*****************************************************************************
219 * Open:
220 *****************************************************************************/
221 static int Open( vlc_object_t *p_this )
222 {
223     demux_t     *p_demux = (demux_t*)p_this;
224     demux_sys_t *p_sys = NULL;
225     es_format_t fmt;
226     int i;
227     int i_width;
228     int i_height;
229     int i_aspect;
230     int result = 0;
231
232     /* Only when selected */
233     if( *p_demux->psz_access == '\0' )
234         return VLC_EGENERIC;
235     
236     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
237
238     /* Set up p_demux */
239     p_demux->pf_demux = Demux;
240     p_demux->pf_control = Control;
241     p_demux->info.i_update = 0;
242     p_demux->info.i_title = 0;
243     p_demux->info.i_seekpoint = 0;
244     
245     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
246     if( !p_sys ) return VLC_ENOMEM;
247     
248     memset( p_sys, 0, sizeof( demux_sys_t ) );
249     memset( &fmt, 0, sizeof( es_format_t ) );    
250     
251     msg_Dbg( p_demux, "QTCapture Probed" );
252
253     QTCaptureDeviceInput * input = nil;
254     NSError *o_returnedError;
255
256     p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
257     if( !p_sys->device )
258     {
259         intf_UserFatal( p_demux, true, _("No Input device found"),
260                         _("Your Mac does not seem to be equipped with a suitable input device. "
261                           "Please check your connectors and drivers.") );
262         msg_Err( p_demux, "Can't find any Video device" );
263         
264         goto error;
265     }
266
267     if( ![p_sys->device open: &o_returnedError] )
268     {
269         msg_Err( p_demux, "Unable to open the capture device (%i)", [o_returnedError code] );
270         goto error;
271     }
272
273     if( [p_sys->device isInUseByAnotherApplication] == YES )
274     {
275         msg_Err( p_demux, "default capture device is exclusively in use by another application" );
276         goto error;
277     }
278
279     input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
280     if( !input )
281     {
282         msg_Err( p_demux, "can't create a valid capture input facility" );
283         goto error;
284     }
285
286     p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
287
288     /* Hack - This will lower CPU consumption for some reason */
289     [p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
290         [NSNumber numberWithInt:480], kCVPixelBufferHeightKey,
291         [NSNumber numberWithInt:640], kCVPixelBufferWidthKey, nil]];
292
293     p_sys->session = [[QTCaptureSession alloc] init];
294
295     bool ret = [p_sys->session addInput:input error: &o_returnedError];
296     if( !ret )
297     {
298         msg_Err( p_demux, "default video capture device could not be added to capture session (%i)", [o_returnedError code] );
299         goto error;
300     }
301
302     ret = [p_sys->session addOutput:p_sys->output error: &o_returnedError];
303     if( !ret )
304     {
305         msg_Err( p_demux, "output could not be added to capture session (%i)", [o_returnedError code] );
306         goto error;
307     }
308
309     [p_sys->session startRunning];
310
311
312     int qtchroma = [[[p_sys->device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
313     int chroma = qtchroma_to_fourcc( qtchroma );
314     if( !chroma )
315     {
316         msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
317         goto error;
318     }
319
320     /* Now we can init */
321
322     es_format_Init( &fmt, VIDEO_ES, chroma );
323
324     NSSize size = [[p_sys->device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
325     p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
326     p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
327
328     msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
329             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
330
331     p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
332
333     [input release];
334     [pool release];
335
336     msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
337
338     return VLC_SUCCESS;
339 error:
340     [input release];
341     [pool release];
342
343     free( p_sys );
344
345     return VLC_EGENERIC;
346 }
347
348 /*****************************************************************************
349 * Close:
350 *****************************************************************************/
351 static void Close( vlc_object_t *p_this )
352 {
353     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
354
355     demux_t     *p_demux = (demux_t*)p_this;
356     demux_sys_t *p_sys = p_demux->p_sys;
357
358     /* Hack: if libvlc was killed, main interface thread was,
359      * and poor QTKit needs it, so don't tell him.
360      * Else we dead lock. */
361     if( vlc_object_alive(p_this->p_libvlc))
362     {
363         [p_sys->session stopRunning];
364         [p_sys->device release];
365         [p_sys->output release];
366         [p_sys->session release];
367     }
368     free( p_sys );
369
370     [pool release];
371 }
372
373
374 /*****************************************************************************
375 * Demux:
376 *****************************************************************************/
377 static int Demux( demux_t *p_demux )
378 {
379     demux_sys_t *p_sys = p_demux->p_sys;
380     block_t *p_block;
381
382     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
383
384     @synchronized (p_sys->output)
385     {
386         p_block = [p_sys->output blockWithCurrentFrame];
387     }
388
389     if( !p_block )
390     {
391         /* Nothing to display yet, just forget */
392         [pool release];
393         msleep( 10000 );
394         return 1;
395     }
396
397     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
398     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
399
400     [pool release];
401     return 1;
402 }
403
404 /*****************************************************************************
405 * Control:
406 *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
408 {
409     bool *pb;
410     int64_t    *pi64;
411
412     switch( i_query )
413     {
414         /* Special for access_demux */
415         case DEMUX_CAN_PAUSE:
416         case DEMUX_CAN_SEEK:
417         case DEMUX_SET_PAUSE_STATE:
418         case DEMUX_CAN_CONTROL_PACE:
419            pb = (bool*)va_arg( args, bool * );
420            *pb = false;
421            return VLC_SUCCESS;
422
423         case DEMUX_GET_PTS_DELAY:
424            pi64 = (int64_t*)va_arg( args, int64_t * );
425            *pi64 = (int64_t)DEFAULT_PTS_DELAY;
426            return VLC_SUCCESS;
427
428         default:
429            return VLC_EGENERIC;
430     }
431     return VLC_EGENERIC;
432 }