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