contrib: soxr: enable by default
[vlc.git] / modules / access / avcapture.m
blob30b9c199ef7f578d1d050e02c6139d9bd526b567
1 /*****************************************************************************
2  * avcapture.m: AVFoundation (Mac OS X) based video capture module
3  *****************************************************************************
4  * Copyright © 2008-2013 VLC authors and VideoLAN
5  *
6  * Authors: Michael Feurstein <michael.feurstein@gmail.com>
7  *
8  ****************************************************************************
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program 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
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
28 #define OS_OBJECT_USE_OBJC 0
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_input.h>
37 #include <vlc_demux.h>
38 #include <vlc_interface.h>
39 #include <vlc_dialog.h>
40 #include <vlc_access.h>
42 #import <AVFoundation/AVFoundation.h>
43 #import <CoreMedia/CoreMedia.h>
45 /*****************************************************************************
46 * Local prototypes
47 *****************************************************************************/
48 static int Open(vlc_object_t *p_this);
49 static void Close(vlc_object_t *p_this);
50 static int Demux(demux_t *p_demux);
51 static int Control(demux_t *, int, va_list);
53 /*****************************************************************************
54 * Module descriptor
55 *****************************************************************************/
56 vlc_module_begin ()
57    set_shortname(N_("AVFoundation Video Capture"))
58    set_description(N_("AVFoundation video capture module."))
59    set_category(CAT_INPUT)
60    set_subcategory(SUBCAT_INPUT_ACCESS)
61    add_shortcut("avcapture")
62    set_capability("access_demux", 0)
63    set_callbacks(Open, Close)
64 vlc_module_end ()
67 /*****************************************************************************
68 * AVFoundation Bridge
69 *****************************************************************************/
70 @interface VLCAVDecompressedVideoOutput : AVCaptureVideoDataOutput
72     demux_t             *p_avcapture;
74     CVImageBufferRef    currentImageBuffer;
76     mtime_t             currentPts;
77     mtime_t             previousPts;
78     size_t              bytesPerRow;
80     long                timeScale;
81     BOOL                videoDimensionsReady;
84 @property (readwrite) CMVideoDimensions videoDimensions;
86 - (id)initWithDemux:(demux_t *)p_demux;
87 - (int)width;
88 - (int)height;
89 - (void)getVideoDimensions:(CMSampleBufferRef)sampleBuffer;
90 - (mtime_t)currentPts;
91 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;
92 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer;
93 @end
95 @implementation VLCAVDecompressedVideoOutput : AVCaptureVideoDataOutput
97 - (id)initWithDemux:(demux_t *)p_demux
99     if (self = [super init])
100     {
101         p_avcapture = p_demux;
102         currentImageBuffer = nil;
103         currentPts = 0;
104         previousPts = 0;
105         bytesPerRow = 0;
106         timeScale = 0;
107         videoDimensionsReady = NO;
108     }
109     return self;
112 - (void)dealloc
114     @synchronized (self)
115     {
116         CVBufferRelease(currentImageBuffer);
117         currentImageBuffer = nil;
118         bytesPerRow = 0;
119         videoDimensionsReady = NO;
120     }
123 - (long)timeScale
125     return timeScale;
128 - (int)width
130     return self.videoDimensions.width;
133 - (int)height
135     return self.videoDimensions.height;
138 - (size_t)bytesPerRow
140     return bytesPerRow;
143 - (void)getVideoDimensions:(CMSampleBufferRef)sampleBuffer
145     if (!videoDimensionsReady)
146     {
147         CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
148         self.videoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
149         bytesPerRow = CVPixelBufferGetBytesPerRow(CMSampleBufferGetImageBuffer(sampleBuffer));
150         videoDimensionsReady = YES;
151         msg_Dbg(p_avcapture, "Dimensionns obtained height:%i width:%i bytesPerRow:%lu", [self height], [self width], bytesPerRow);
152     }
155 -(mtime_t)currentPts
157     mtime_t pts;
159     if ( !currentImageBuffer || currentPts == previousPts )
160         return 0;
162     @synchronized (self)
163     {
164         pts = previousPts = currentPts;
165     }
167     return currentPts;
170 - (void)captureOutput:(AVCaptureOutput *)captureOutput
171 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
172        fromConnection:(AVCaptureConnection *)connection
174     @autoreleasepool {
175         CVImageBufferRef imageBufferToRelease;
176         CMTime presentationtimestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
177         CVImageBufferRef videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
178         CVBufferRetain(videoFrame);
179         [self getVideoDimensions:sampleBuffer];
181         @synchronized (self) {
182             imageBufferToRelease = currentImageBuffer;
183             currentImageBuffer = videoFrame;
184             currentPts = (mtime_t)presentationtimestamp.value;
185             timeScale = (long)presentationtimestamp.timescale;
186         }
187         
188         CVBufferRelease(imageBufferToRelease);
189     }
192 - (mtime_t)copyCurrentFrameToBuffer:(void *)buffer
194     CVImageBufferRef imageBuffer;
195     mtime_t pts;
197     void *pixels;
199     if ( !currentImageBuffer || currentPts == previousPts )
200         return 0;
202     @synchronized (self)
203     {
204         imageBuffer = CVBufferRetain(currentImageBuffer);
205         if (imageBuffer)
206         {
207             pts = previousPts = currentPts;
208             CVPixelBufferLockBaseAddress(imageBuffer, 0);
209             pixels = CVPixelBufferGetBaseAddress(imageBuffer);
210             if (pixels)
211             {
212                 memcpy(buffer, pixels, CVPixelBufferGetHeight(imageBuffer) * CVPixelBufferGetBytesPerRow(imageBuffer));
213             }
214             CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
215         }
216     }
217     CVBufferRelease(imageBuffer);
219     if (pixels)
220         return currentPts;
221     else
222         return 0;
225 @end
227 /*****************************************************************************
228 * Struct
229 *****************************************************************************/
231 struct demux_sys_t
233     CFTypeRef _Nullable             session;       // AVCaptureSession
234     CFTypeRef _Nullable             device;        // AVCaptureDevice
235     CFTypeRef _Nullable             output;        // VLCAVDecompressedVideoOutput
236     es_out_id_t                     *p_es_video;
237     es_format_t                     fmt;
238     int                             height, width;
239     BOOL                            b_es_setup;
242 /*****************************************************************************
243 * Open:
244 *****************************************************************************/
245 static int Open(vlc_object_t *p_this)
247     demux_t                 *p_demux = (demux_t*)p_this;
248     demux_sys_t             *p_sys = NULL;
250     NSString                *avf_currdevice_uid;
251     NSArray                 *myVideoDevices;
252     NSError                 *o_returnedError;
254     AVCaptureDeviceInput    *input = nil;
256     int                     i, i_width, i_height, deviceCount, ivideo;
258     char                    *psz_uid = NULL;
260     @autoreleasepool {
261         if (p_demux->psz_location && *p_demux->psz_location)
262             psz_uid = strdup(p_demux->psz_location);
264         msg_Dbg(p_demux, "avcapture uid = %s", psz_uid);
265         avf_currdevice_uid = [[NSString alloc] initWithFormat:@"%s", psz_uid];
267         /* Set up p_demux */
268         p_demux->pf_demux = Demux;
269         p_demux->pf_control = Control;
271         p_demux->p_sys = p_sys = calloc(1, sizeof(demux_sys_t));
272         if ( !p_sys )
273             return VLC_ENOMEM;
275         myVideoDevices = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]
276                            arrayByAddingObjectsFromArray:[AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed]];
277         if ( [myVideoDevices count] == 0 )
278         {
279             vlc_dialog_display_error(p_demux, _("No video devices found"),
280                 _("Your Mac does not seem to be equipped with a suitable video input device. "
281                 "Please check your connectors and drivers."));
282             msg_Err(p_demux, "Can't find any suitable video device");
283             goto error;
284         }
286         deviceCount = [myVideoDevices count];
287         for ( ivideo = 0; ivideo < deviceCount; ivideo++ )
288         {
289             AVCaptureDevice *avf_device;
290             avf_device = [myVideoDevices objectAtIndex:ivideo];
291             msg_Dbg(p_demux, "avcapture %i/%i %s %s", ivideo, deviceCount, [[avf_device modelID] UTF8String], [[avf_device uniqueID] UTF8String]);
292             if ([[[avf_device uniqueID]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:avf_currdevice_uid]) {
293                 break;
294             }
295         }
297         if ( ivideo < [myVideoDevices count] )
298         {
299             p_sys->device = CFBridgingRetain([myVideoDevices objectAtIndex:ivideo]);
300         }
301         else
302         {
303             msg_Dbg(p_demux, "Cannot find designated device as %s, falling back to default.", [avf_currdevice_uid UTF8String]);
304             p_sys->device = CFBridgingRetain([AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]);
305         }
306         if ( !p_sys->device )
307         {
308             vlc_dialog_display_error(p_demux, _("No video devices found"),
309                 _("Your Mac does not seem to be equipped with a suitable input device. "
310                 "Please check your connectors and drivers."));
311             msg_Err(p_demux, "Can't find any suitable video device");
312             goto error;
313         }
315         if ( [(__bridge AVCaptureDevice *)p_sys->device isInUseByAnotherApplication] == YES )
316         {
317             msg_Err(p_demux, "default capture device is exclusively in use by another application");
318             goto error;
319         }
321         input = [AVCaptureDeviceInput deviceInputWithDevice:(__bridge AVCaptureDevice *)p_sys->device error:&o_returnedError];
323         if ( !input )
324         {
325             msg_Err(p_demux, "can't create a valid capture input facility (%ld)", [o_returnedError code]);
326             goto error;
327         }
329         int chroma = VLC_CODEC_RGB32;
331         memset(&p_sys->fmt, 0, sizeof(es_format_t));
332         es_format_Init(&p_sys->fmt, VIDEO_ES, chroma);
334         p_sys->session = CFBridgingRetain([[AVCaptureSession alloc] init]);
335         [(__bridge AVCaptureSession *)p_sys->session addInput:input];
337         p_sys->output = CFBridgingRetain([[VLCAVDecompressedVideoOutput alloc] initWithDemux:p_demux]);
338         [(__bridge AVCaptureSession *)p_sys->session addOutput:(__bridge VLCAVDecompressedVideoOutput *)p_sys->output];
340         dispatch_queue_t queue = dispatch_queue_create("avCaptureQueue", NULL);
341         [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output setSampleBufferDelegate:(__bridge id)p_sys->output queue:queue];
342         dispatch_release(queue);
344         [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
345         [(__bridge AVCaptureSession *)p_sys->session startRunning];
347         input = nil;
349         msg_Dbg(p_demux, "AVCapture: Video device ready!");
351         return VLC_SUCCESS;
352     error:
353         msg_Err(p_demux, "Error");
354         input = nil;
356         free(p_sys);
358         return VLC_EGENERIC;
359     }
362 /*****************************************************************************
363 * Close:
364 *****************************************************************************/
365 static void Close(vlc_object_t *p_this)
367     demux_t             *p_demux = (demux_t*)p_this;
368     demux_sys_t         *p_sys = p_demux->p_sys;
370     @autoreleasepool {
371         msg_Dbg(p_demux,"Close AVCapture");
373         // Perform this on main thread, as the framework itself will sometimes try to synchronously
374         // work on main thread. And this will create a dead lock.
375         [(__bridge AVCaptureSession *)p_sys->session performSelectorOnMainThread:@selector(stopRunning) withObject:nil waitUntilDone:NO];
376         CFBridgingRelease(p_sys->output);
377         CFBridgingRelease(p_sys->session);
379         free(p_sys);
380     }
383 /*****************************************************************************
384 * Demux:
385 *****************************************************************************/
386 static int Demux(demux_t *p_demux)
388     demux_sys_t *p_sys = p_demux->p_sys;
389     block_t     *p_block;
391     @autoreleasepool {
392         @synchronized ( p_sys->output )
393         {
394             p_block = block_Alloc([(__bridge VLCAVDecompressedVideoOutput *)p_sys->output width] * [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output bytesPerRow]);
396             if ( !p_block )
397             {
398                 msg_Err(p_demux, "cannot get block");
399                 return 0;
400             }
402             p_block->i_pts = [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output copyCurrentFrameToBuffer: p_block->p_buffer];
404             if ( !p_block->i_pts )
405             {
406                 /* Nothing to display yet, just forget */
407                 block_Release(p_block);
408                 msleep(10000);
409                 return 1;
410             }
411             else if ( !p_sys->b_es_setup )
412             {
413                 p_sys->fmt.video.i_frame_rate_base = [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output timeScale];
414                 msg_Dbg(p_demux, "using frame rate base: %i", p_sys->fmt.video.i_frame_rate_base);
415                 p_sys->width = p_sys->fmt.video.i_width = [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output width];
416                 p_sys->height = p_sys->fmt.video.i_height = [(__bridge VLCAVDecompressedVideoOutput *)p_sys->output height];
417                 p_sys->p_es_video = es_out_Add(p_demux->out, &p_sys->fmt);
418                 msg_Dbg(p_demux, "added new video es %4.4s %dx%d", (char*)&p_sys->fmt.i_codec, p_sys->width, p_sys->height);
419                 p_sys->b_es_setup = YES;
420             }
421         }
422         
423         es_out_SetPCR(p_demux->out, p_block->i_pts);
424         es_out_Send(p_demux->out, p_sys->p_es_video, p_block);
425         
426     }
427     return 1;
430 /*****************************************************************************
431 * Control:
432 *****************************************************************************/
433 static int Control(demux_t *p_demux, int i_query, va_list args)
435     bool        *pb;
436     int64_t     *pi64;
438     switch( i_query )
439     {
440         /* Special for access_demux */
441         case DEMUX_CAN_PAUSE:
442         case DEMUX_CAN_SEEK:
443         case DEMUX_SET_PAUSE_STATE:
444         case DEMUX_CAN_CONTROL_PACE:
445            pb = va_arg(args, bool *);
446            *pb = false;
447            return VLC_SUCCESS;
449         case DEMUX_GET_PTS_DELAY:
450            pi64 = va_arg(args, int64_t *);
451            *pi64 = INT64_C(1000) * var_InheritInteger(p_demux, "live-caching");
452            return VLC_SUCCESS;
454         case DEMUX_GET_TIME:
455             pi64 = va_arg(args, int64_t *);
456             *pi64 = mdate();
457             return VLC_SUCCESS;
459         default:
460            return VLC_EGENERIC;
461     }
462     return VLC_EGENERIC;