1 /*****************************************************************************
2 * qtcapture.m: qtkit (Mac OS X) based capture module
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
6 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
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.
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.
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
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
37 #include <vlc_demux.h>
38 #include <vlc_interface.h>
40 #import <QTKit/QTKit.h>
42 /*****************************************************************************
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 );
50 /*****************************************************************************
52 *****************************************************************************/
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 );
64 /*****************************************************************************
66 *****************************************************************************/
67 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
69 CVImageBufferRef currentImageBuffer;
74 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
75 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer;
78 /* Apple sample code */
79 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
82 if( self = [super init] )
84 currentImageBuffer = nil;
94 CVBufferRelease(currentImageBuffer);
95 currentImageBuffer = nil;
100 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
102 // Store the latest frame
103 // This must be done in a @synchronized block because this delegate method is not called on the main thread
104 CVImageBufferRef imageBufferToRelease;
106 CVBufferRetain(videoFrame);
110 imageBufferToRelease = currentImageBuffer;
111 currentImageBuffer = videoFrame;
112 currentPts = 1000000L / [sampleBuffer presentationTime].timeScale * [sampleBuffer presentationTime].timeValue;
114 CVBufferRelease(imageBufferToRelease);
117 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
119 CVImageBufferRef imageBuffer;
122 if(!currentImageBuffer || currentPts == previousPts )
127 imageBuffer = CVBufferRetain(currentImageBuffer);
128 pts = previousPts = currentPts;
130 CVPixelBufferLockBaseAddress(imageBuffer, 0);
131 void * pixels = CVPixelBufferGetBaseAddress(imageBuffer);
132 memcpy( buffer, pixels, CVPixelBufferGetBytesPerRow(imageBuffer) * CVPixelBufferGetHeight(imageBuffer) );
133 CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
136 CVBufferRelease(imageBuffer);
143 /*****************************************************************************
145 *****************************************************************************/
148 QTCaptureSession * session;
149 QTCaptureDevice * device;
150 VLCDecompressedVideoOutput * output;
152 es_out_id_t * p_es_video;
156 /*****************************************************************************
158 *****************************************************************************/
159 static int qtchroma_to_fourcc( int i_qt )
165 } qtchroma_to_fourcc[] =
168 { k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') },
172 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
174 if( qtchroma_to_fourcc[i].i_qt == i_qt )
175 return qtchroma_to_fourcc[i].i_fourcc;
180 /*****************************************************************************
182 *****************************************************************************/
183 static int Open( vlc_object_t *p_this )
185 demux_t *p_demux = (demux_t*)p_this;
186 demux_sys_t *p_sys = NULL;
194 /* Only when selected */
195 if( *p_demux->psz_access == '\0' )
198 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
201 p_demux->pf_demux = Demux;
202 p_demux->pf_control = Control;
203 p_demux->info.i_update = 0;
204 p_demux->info.i_title = 0;
205 p_demux->info.i_seekpoint = 0;
207 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
208 if( !p_sys ) return VLC_ENOMEM;
210 memset( p_sys, 0, sizeof( demux_sys_t ) );
211 memset( &fmt, 0, sizeof( es_format_t ) );
213 msg_Dbg( p_demux, "QTCapture Probed" );
215 QTCaptureDeviceInput * input = nil;
216 NSError *o_returnedError;
218 p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
221 intf_UserFatal( p_demux, true, _("No Input device found"),
222 _("Your Mac does not seem to be equipped with a suitable input device. "
223 "Please check your connectors and drivers.") );
224 msg_Err( p_demux, "Can't find any Video device" );
229 if( ![p_sys->device open: &o_returnedError] )
231 msg_Err( p_demux, "Unable to open the capture device (%i)", [o_returnedError code] );
235 if( [p_sys->device isInUseByAnotherApplication] == YES )
237 msg_Err( p_demux, "default capture device is exclusively in use by another application" );
241 input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
244 msg_Err( p_demux, "can't create a valid capture input facility" );
248 p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
250 /* Hack - This will lower CPU consumption for some reason */
251 [p_sys->output setPixelBufferAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
252 [NSNumber numberWithInt:480], kCVPixelBufferHeightKey,
253 [NSNumber numberWithInt:640], kCVPixelBufferWidthKey, nil]];
255 p_sys->session = [[QTCaptureSession alloc] init];
257 bool ret = [p_sys->session addInput:input error: &o_returnedError];
260 msg_Err( p_demux, "default video capture device could not be added to capture session (%i)", [o_returnedError code] );
264 ret = [p_sys->session addOutput:p_sys->output error: &o_returnedError];
267 msg_Err( p_demux, "output could not be added to capture session (%i)", [o_returnedError code] );
271 [p_sys->session startRunning];
274 int qtchroma = [[[p_sys->device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
275 int chroma = qtchroma_to_fourcc( qtchroma );
278 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
282 /* Now we can init */
284 es_format_Init( &fmt, VIDEO_ES, chroma );
286 NSSize size = [[p_sys->device attributeForKey:QTFormatDescriptionVideoEncodedPixelsSizeAttribute] sizeValue];
287 p_sys->width = fmt.video.i_width = 640;/* size.width; FIXME */
288 p_sys->height = fmt.video.i_height = 480;/* size.height; FIXME */
290 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
291 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
293 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
298 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
310 /*****************************************************************************
312 *****************************************************************************/
313 static void Close( vlc_object_t *p_this )
315 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
317 demux_t *p_demux = (demux_t*)p_this;
318 demux_sys_t *p_sys = p_demux->p_sys;
320 /* Hack: if libvlc was killed, main interface thread was,
321 * and poor QTKit needs it, so don't tell him.
322 * Else we dead lock. */
323 if( vlc_object_alive(p_this->p_libvlc))
325 [p_sys->session stopRunning];
326 [p_sys->device release];
327 [p_sys->output release];
328 [p_sys->session release];
336 /*****************************************************************************
338 *****************************************************************************/
339 static int Demux( demux_t *p_demux )
341 demux_sys_t *p_sys = p_demux->p_sys;
344 p_block = block_New( p_demux, p_sys->width *
345 p_sys->height * 2 /* FIXME */ );
348 msg_Err( p_demux, "cannot get block" );
352 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
354 @synchronized (p_sys->output)
356 p_block->i_pts = [p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
359 if( !p_block->i_pts )
361 /* Nothing to display yet, just forget */
362 block_Release( p_block );
368 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
369 es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
375 /*****************************************************************************
377 *****************************************************************************/
378 static int Control( demux_t *p_demux, int i_query, va_list args )
385 /* Special for access_demux */
386 case DEMUX_CAN_PAUSE:
388 case DEMUX_SET_PAUSE_STATE:
389 case DEMUX_CAN_CONTROL_PACE:
390 pb = (bool*)va_arg( args, bool * );
394 case DEMUX_GET_PTS_DELAY:
395 pi64 = (int64_t*)va_arg( args, int64_t * );
396 *pi64 = (int64_t)DEFAULT_PTS_DELAY;