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 typedef struct qtcapture_block_t
53 CVImageBufferRef imageBuffer;
54 block_free_t pf_original_release;
57 static void qtcapture_block_release( block_t *p_block )
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 );
65 static block_t * qtcapture_block_new( void * p_buffer,
67 CVImageBufferRef imageBufferToRelease )
69 qtcapture_block_t * p_qtblock;
72 p_qtblock = malloc( sizeof( qtcapture_block_t ) );
73 if(!p_qtblock) return NULL;
76 block_Init( &p_qtblock->block, p_buffer, i_buffer );
77 p_qtblock->block.pf_release = qtcapture_block_release;
78 p_qtblock->imageBuffer = imageBufferToRelease;
80 return (block_t *)p_qtblock;
84 /*****************************************************************************
86 *****************************************************************************/
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 );
98 /*****************************************************************************
100 *****************************************************************************/
101 @interface VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
103 CVImageBufferRef currentImageBuffer;
108 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection;
109 - (block_t *)blockWithCurrentFrame;
112 /* Apple sample code */
113 @implementation VLCDecompressedVideoOutput : QTCaptureDecompressedVideoOutput
116 if( self = [super init] )
118 currentImageBuffer = nil;
128 CVBufferRelease(currentImageBuffer);
129 currentImageBuffer = nil;
134 - (void)outputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
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;
140 CVBufferRetain(videoFrame);
144 imageBufferToRelease = currentImageBuffer;
145 currentImageBuffer = videoFrame;
146 currentPts = 1000000L / [sampleBuffer presentationTime].timeScale * [sampleBuffer presentationTime].timeValue;
149 CVBufferRelease(imageBufferToRelease);
152 - (block_t *)blockWithCurrentFrame
154 CVImageBufferRef imageBuffer;
156 block_t * p_block = NULL;
158 if(!currentImageBuffer || currentPts == previousPts )
163 // Released in the p_block release method.
164 imageBuffer = CVBufferRetain(currentImageBuffer);
165 pts = previousPts = currentPts;
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 ),
173 p_block->i_pts = currentPts;
181 /*****************************************************************************
183 *****************************************************************************/
186 QTCaptureSession * session;
187 QTCaptureDevice * device;
188 VLCDecompressedVideoOutput * output;
190 es_out_id_t * p_es_video;
194 /*****************************************************************************
196 *****************************************************************************/
197 static int qtchroma_to_fourcc( int i_qt )
203 } qtchroma_to_fourcc[] =
206 { k422YpCbCr8CodecType, VLC_FOURCC('U','Y','V','Y') },
210 for( i = 0; qtchroma_to_fourcc[i].i_qt; i++ )
212 if( qtchroma_to_fourcc[i].i_qt == i_qt )
213 return qtchroma_to_fourcc[i].i_fourcc;
218 /*****************************************************************************
220 *****************************************************************************/
221 static int Open( vlc_object_t *p_this )
223 demux_t *p_demux = (demux_t*)p_this;
224 demux_sys_t *p_sys = NULL;
232 /* Only when selected */
233 if( *p_demux->psz_access == '\0' )
236 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
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;
245 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
246 if( !p_sys ) return VLC_ENOMEM;
248 memset( p_sys, 0, sizeof( demux_sys_t ) );
249 memset( &fmt, 0, sizeof( es_format_t ) );
251 msg_Dbg( p_demux, "QTCapture Probed" );
253 QTCaptureDeviceInput * input = nil;
254 NSError *o_returnedError;
256 p_sys->device = [QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo];
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" );
267 if( ![p_sys->device open: &o_returnedError] )
269 msg_Err( p_demux, "Unable to open the capture device (%i)", [o_returnedError code] );
273 if( [p_sys->device isInUseByAnotherApplication] == YES )
275 msg_Err( p_demux, "default capture device is exclusively in use by another application" );
279 input = [[QTCaptureDeviceInput alloc] initWithDevice: p_sys->device];
282 msg_Err( p_demux, "can't create a valid capture input facility" );
286 p_sys->output = [[VLCDecompressedVideoOutput alloc] init];
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]];
293 p_sys->session = [[QTCaptureSession alloc] init];
295 bool ret = [p_sys->session addInput:input error: &o_returnedError];
298 msg_Err( p_demux, "default video capture device could not be added to capture session (%i)", [o_returnedError code] );
302 ret = [p_sys->session addOutput:p_sys->output error: &o_returnedError];
305 msg_Err( p_demux, "output could not be added to capture session (%i)", [o_returnedError code] );
309 [p_sys->session startRunning];
312 int qtchroma = [[[p_sys->device formatDescriptions] objectAtIndex: 0] formatType]; /* FIXME */
313 int chroma = qtchroma_to_fourcc( qtchroma );
316 msg_Err( p_demux, "Unknown qt chroma %4.4s provided by camera", (char*)&qtchroma );
320 /* Now we can init */
322 es_format_Init( &fmt, VIDEO_ES, chroma );
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 */
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 );
331 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
336 msg_Dbg( p_demux, "QTCapture: We have a video device ready!" );
348 /*****************************************************************************
350 *****************************************************************************/
351 static void Close( vlc_object_t *p_this )
353 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
355 demux_t *p_demux = (demux_t*)p_this;
356 demux_sys_t *p_sys = p_demux->p_sys;
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))
363 [p_sys->session stopRunning];
364 [p_sys->device release];
365 [p_sys->output release];
366 [p_sys->session release];
374 /*****************************************************************************
376 *****************************************************************************/
377 static int Demux( demux_t *p_demux )
379 demux_sys_t *p_sys = p_demux->p_sys;
382 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
384 @synchronized (p_sys->output)
386 p_block = [p_sys->output blockWithCurrentFrame];
391 /* Nothing to display yet, just forget */
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 );
404 /*****************************************************************************
406 *****************************************************************************/
407 static int Control( demux_t *p_demux, int i_query, va_list args )
414 /* Special for access_demux */
415 case DEMUX_CAN_PAUSE:
417 case DEMUX_SET_PAUSE_STATE:
418 case DEMUX_CAN_CONTROL_PACE:
419 pb = (bool*)va_arg( args, bool * );
423 case DEMUX_GET_PTS_DELAY:
424 pi64 = (int64_t*)va_arg( args, int64_t * );
425 *pi64 = (int64_t)DEFAULT_PTS_DELAY;