28 #import <AVFoundation/AVFoundation.h>
74 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
143 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
144 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
145 fromConnection:(AVCaptureConnection *)connection;
153 if (
self = [super
init]) {
159 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
160 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
161 fromConnection:(AVCaptureConnection *)connection
189 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
190 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
191 fromConnection:(AVCaptureConnection *)connection;
199 if (
self = [super
init]) {
205 - (
void) captureOutput:(AVCaptureOutput *)captureOutput
206 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
207 fromConnection:(AVCaptureConnection *)connection
228 [ctx->capture_session stopRunning];
230 [ctx->capture_session release];
231 [ctx->video_output release];
232 [ctx->audio_output release];
233 [ctx->avf_delegate release];
234 [ctx->avf_audio_delegate release];
269 NSError *error = nil;
270 AVCaptureInput* capture_input = nil;
273 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
275 capture_input = (AVCaptureInput*) video_device;
278 if (!capture_input) {
280 [[error localizedDescription] UTF8String]);
285 [ctx->capture_session addInput:capture_input];
292 ctx->
video_output = [[AVCaptureVideoDataOutput alloc] init];
305 pxl_fmt_spec = avf_pixel_formats[i];
312 av_log(s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by AVFoundation.\n",
318 if ([[ctx->
video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.
avf_id]] == NSNotFound) {
319 av_log(s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
325 for (NSNumber *pxl_fmt
in [ctx->
video_output availableVideoCVPixelFormatTypes]) {
329 if ([pxl_fmt intValue] == avf_pixel_formats[i].
avf_id) {
330 pxl_fmt_dummy = avf_pixel_formats[i];
340 pxl_fmt_spec = pxl_fmt_dummy;
355 NSNumber *pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
356 NSDictionary *capture_dict = [NSDictionary dictionaryWithObject:pixel_format
357 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
359 [ctx->video_output setVideoSettings:capture_dict];
360 [ctx->video_output setAlwaysDiscardsLateVideoFrames:YES];
364 dispatch_queue_t queue = dispatch_queue_create(
"avf_queue",
NULL);
365 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
366 dispatch_release(queue);
369 [ctx->capture_session addOutput:ctx->video_output];
381 NSError *error = nil;
382 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device error:&error] autorelease];
384 if (!audio_dev_input) {
386 [[error localizedDescription] UTF8String]);
391 [ctx->capture_session addInput:audio_dev_input];
398 ctx->
audio_output = [[AVCaptureAudioDataOutput alloc] init];
407 dispatch_queue_t queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
408 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
409 dispatch_release(queue);
412 [ctx->capture_session addOutput:ctx->audio_output];
427 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
442 CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->
current_frame);
443 CGSize image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
447 stream->
codec->
width = (int)image_buffer_size.width;
448 stream->
codec->
height = (
int)image_buffer_size.height;
465 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
480 CMFormatDescriptionRef format_desc = CMSampleBufferGetFormatDescription(ctx->
current_audio_frame);
481 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
495 ctx->
audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
496 ctx->
audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
498 ctx->
audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
501 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
506 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
511 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
516 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
546 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
550 uint32_t num_screens = 0;
555 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
556 CGGetActiveDisplayList(0,
NULL, &num_screens);
562 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
564 for (AVCaptureDevice *device
in devices) {
565 const char *
name = [[device localizedName] UTF8String];
566 index = [devices indexOfObject:device];
570 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
571 if (num_screens > 0) {
572 CGDirectDisplayID screens[num_screens];
573 CGGetActiveDisplayList(num_screens, screens, &num_screens);
574 for (
int i = 0; i < num_screens; i++) {
581 devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
582 for (AVCaptureDevice *device
in devices) {
583 const char *
name = [[device localizedName] UTF8String];
584 int index = [devices indexOfObject:device];
591 AVCaptureDevice *video_device = nil;
592 AVCaptureDevice *audio_device = nil;
594 NSArray *video_devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
610 video_device = [video_devices objectAtIndex:ctx->video_device_index];
612 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
613 CGDirectDisplayID screens[num_screens];
614 CGGetActiveDisplayList(num_screens, screens, &num_screens);
615 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
616 video_device = (AVCaptureDevice*) capture_screen_input;
625 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
628 for (AVCaptureDevice *device
in video_devices) {
630 video_device = device;
635 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
639 if(sscanf(ctx->
video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
640 CGDirectDisplayID screens[num_screens];
641 CGGetActiveDisplayList(num_screens, screens, &num_screens);
642 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
643 video_device = (AVCaptureDevice*) capture_screen_input;
658 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
665 audio_device = [devices objectAtIndex:ctx->audio_device_index];
669 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
671 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
673 for (AVCaptureDevice *device
in devices) {
675 audio_device = device;
688 if (!video_device && !audio_device) {
701 av_log(s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
713 [ctx->capture_session startRunning];
740 CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(ctx->
current_frame);
743 if (
av_new_packet(pkt, (
int)CVPixelBufferGetDataSize(image_buffer)) < 0) {
753 CVPixelBufferLockBaseAddress(image_buffer, 0);
755 void*
data = CVPixelBufferGetBaseAddress(image_buffer);
758 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
763 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
765 if (!block_buffer || !block_buffer_size) {
788 if (ret != kCMBlockBufferNoErr) {
795 #define INTERLEAVE_OUTPUT(bps) \
797 int##bps##_t **src; \
798 int##bps##_t *dest; \
799 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
800 if (!src) return AVERROR(EIO); \
801 for (c = 0; c < ctx->audio_channels; c++) { \
802 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
804 dest = (int##bps##_t*)pkt->data; \
805 shift = bps - ctx->audio_bits_per_sample; \
806 for (sample = 0; sample < num_samples; sample++) \
807 for (c = 0; c < ctx->audio_channels; c++) \
808 *dest++ = src[c][sample] << shift; \
818 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->
size, pkt->
data);
819 if (ret != kCMBlockBufferNoErr) {
845 {
"list_devices",
"list available devices", offsetof(
AVFContext, list_devices),
AV_OPT_TYPE_INT, {.i64=0}, 0, 1,
AV_OPT_FLAG_DECODING_PARAM,
"list_devices" },
863 .
name =
"avfoundation",
870 .priv_class = &avf_class,
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
static const struct AVFPixelFormatSpec avf_pixel_formats[]
static void lock_frames(AVFContext *ctx)
static int shift(int a, int b)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
AVCaptureSession * capture_session
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
ptrdiff_t const GLvoid * data
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
packed RGB 8:8:8, 24bpp, RGBRGB...
static av_cold int init(AVCodecContext *avctx)
int audio_frames_captured
static const AVOption options[]
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
int index
stream index in AVFormatContext
static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
CMSampleBufferRef current_audio_frame
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int get_audio_config(AVFormatContext *s)
int audio_non_interleaved
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
AudioReciever class - delegate for AVCaptureSession.
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
static av_cold int read_close(AVFormatContext *ctx)
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
static void parse_device_name(AVFormatContext *s)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
AVInputFormat ff_avfoundation_demuxer
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Main libavdevice API header.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
static int avf_read_header(AVFormatContext *s)
#define AV_PIX_FMT_YUV444P10
int flags
A combination of AV_PKT_FLAG values.
uint64_t channel_layout
Audio channel layout.
AVCodecContext * codec
Codec context associated with this stream.
common internal API header
char filename[1024]
input or output filename
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
int width
picture width / height.
static const int avf_time_base
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
packed RGB 8:8:8, 24bpp, BGRBGR...
int audio_bits_per_sample
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
static int read_header(FFV1Context *f)
int64_t av_gettime(void)
Get the current time in microseconds.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
#define AV_LOG_INFO
Standard information.
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
static const AVRational avf_time_base_q
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
AVCaptureVideoDataOutput * video_output
pthread_mutex_t frame_lock
static int get_video_config(AVFormatContext *s)
static const AVClass avf_class
Describe the class of an AVClass context structure.
planar YUV 4:4:4 64bpp, (1 Cr & Cb sample per 1x1 Y & A samples, little-endian)
#define INTERLEAVE_OUTPUT(bps)
rational number numerator/denominator
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
static void unlock_frames(AVFContext *ctx)
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
FrameReciever class - delegate for AVCaptureSession.
CMSampleBufferRef current_frame
#define AV_PIX_FMT_YUV422P10
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
enum AVPixelFormat pixel_format
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
AVCaptureAudioDataOutput * audio_output
static void destroy_context(AVFContext *ctx)
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
int channels
number of audio channels
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
void * priv_data
Format private data.
static int avf_close(AVFormatContext *s)
pthread_cond_t frame_wait_cond
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define AV_PIX_FMT_YUV422P16
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...