---
libavdevice/avfoundation.m | 154 ++++++++++++++++++++++++++-------------------
1 file changed, 91 insertions(+), 63 deletions(-)
diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 763e675..1ed9cef 100644
|
a
|
b
|
|
| 37 | 37 | #include "libavutil/time.h" |
| 38 | 38 | #include "avdevice.h" |
| 39 | 39 | |
| | 40 | #define QUEUE_SIZE 4 |
| | 41 | |
| 40 | 42 | static const int avf_time_base = 1000000; |
| 41 | 43 | |
| 42 | 44 | static const AVRational avf_time_base_q = { |
| … |
… |
static const struct AVFPixelFormatSpec avf_pixel_formats[] = {
|
| 78 | 80 | { AV_PIX_FMT_NONE, 0 } |
| 79 | 81 | }; |
| 80 | 82 | |
| | 83 | enum { |
| | 84 | QUEUE_IS_EMPTY, |
| | 85 | QUEUE_HAS_BUFFERS, |
| | 86 | }; |
| | 87 | |
| 81 | 88 | typedef struct |
| 82 | 89 | { |
| 83 | 90 | AVClass* class; |
| … |
… |
typedef struct
|
| 86 | 93 | int audio_frames_captured; |
| 87 | 94 | int64_t first_pts; |
| 88 | 95 | int64_t first_audio_pts; |
| 89 | | pthread_mutex_t frame_lock; |
| 90 | | pthread_cond_t frame_wait_cond; |
| 91 | 96 | id avf_delegate; |
| 92 | 97 | id avf_audio_delegate; |
| 93 | 98 | |
| … |
… |
typedef struct
|
| 124 | 129 | AVCaptureSession *capture_session; |
| 125 | 130 | AVCaptureVideoDataOutput *video_output; |
| 126 | 131 | AVCaptureAudioDataOutput *audio_output; |
| 127 | | CMSampleBufferRef current_frame; |
| 128 | | CMSampleBufferRef current_audio_frame; |
| 129 | | } AVFContext; |
| 130 | 132 | |
| 131 | | static void lock_frames(AVFContext* ctx) |
| 132 | | { |
| 133 | | pthread_mutex_lock(&ctx->frame_lock); |
| 134 | | } |
| | 133 | NSConditionLock *lock; |
| | 134 | NSMutableArray *video_queue; |
| | 135 | NSMutableArray *audio_queue; |
| 135 | 136 | |
| 136 | | static void unlock_frames(AVFContext* ctx) |
| 137 | | { |
| 138 | | pthread_mutex_unlock(&ctx->frame_lock); |
| 139 | | } |
| | 137 | } AVFContext; |
| 140 | 138 | |
| 141 | 139 | /** FrameReciever class - delegate for AVCaptureSession |
| 142 | 140 | */ |
| … |
… |
static void unlock_frames(AVFContext* ctx)
|
| 167 | 165 | didOutputSampleBuffer:(CMSampleBufferRef)videoFrame |
| 168 | 166 | fromConnection:(AVCaptureConnection *)connection |
| 169 | 167 | { |
| 170 | | lock_frames(_context); |
| | 168 | NSMutableArray *queue = _context->video_queue; |
| | 169 | NSConditionLock *lock = _context->lock; |
| 171 | 170 | |
| 172 | | if (_context->current_frame != nil) { |
| 173 | | CFRelease(_context->current_frame); |
| 174 | | } |
| | 171 | [lock lock]; |
| 175 | 172 | |
| 176 | | _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame); |
| | 173 | if ([queue count] == QUEUE_SIZE) { |
| | 174 | av_log(_context, AV_LOG_WARNING, "video queue is full, the oldest frame has been dropped\n"); |
| | 175 | [queue removeLastObject]; |
| | 176 | } |
| 177 | 177 | |
| 178 | | pthread_cond_signal(&_context->frame_wait_cond); |
| | 178 | [queue insertObject:(id)videoFrame atIndex:0]; |
| 179 | 179 | |
| 180 | | unlock_frames(_context); |
| | 180 | [lock unlockWithCondition:QUEUE_HAS_BUFFERS]; |
| 181 | 181 | |
| 182 | 182 | ++_context->frames_captured; |
| 183 | 183 | } |
| … |
… |
static void unlock_frames(AVFContext* ctx)
|
| 213 | 213 | didOutputSampleBuffer:(CMSampleBufferRef)audioFrame |
| 214 | 214 | fromConnection:(AVCaptureConnection *)connection |
| 215 | 215 | { |
| 216 | | lock_frames(_context); |
| | 216 | NSMutableArray *queue = _context->audio_queue; |
| | 217 | NSConditionLock *lock = _context->lock; |
| 217 | 218 | |
| 218 | | if (_context->current_audio_frame != nil) { |
| 219 | | CFRelease(_context->current_audio_frame); |
| 220 | | } |
| | 219 | [lock lock]; |
| 221 | 220 | |
| 222 | | _context->current_audio_frame = (CMSampleBufferRef)CFRetain(audioFrame); |
| | 221 | if ([queue count] == QUEUE_SIZE) { |
| | 222 | av_log(_context, AV_LOG_WARNING, "audio queue is full, the oldest frame has been dropped\n"); |
| | 223 | [queue removeLastObject]; |
| | 224 | } |
| 223 | 225 | |
| 224 | | pthread_cond_signal(&_context->frame_wait_cond); |
| | 226 | [queue insertObject:(id)audioFrame atIndex:0]; |
| 225 | 227 | |
| 226 | | unlock_frames(_context); |
| | 228 | [lock unlockWithCondition:QUEUE_HAS_BUFFERS]; |
| 227 | 229 | |
| 228 | 230 | ++_context->audio_frames_captured; |
| 229 | 231 | } |
| … |
… |
static void destroy_context(AVFContext* ctx)
|
| 248 | 250 | |
| 249 | 251 | av_freep(&ctx->audio_buffer); |
| 250 | 252 | |
| 251 | | pthread_mutex_destroy(&ctx->frame_lock); |
| 252 | | pthread_cond_destroy(&ctx->frame_wait_cond); |
| | 253 | [ctx->audio_queue release]; |
| | 254 | [ctx->video_queue release]; |
| | 255 | [ctx->lock release]; |
| 253 | 256 | |
| 254 | | if (ctx->current_frame) { |
| 255 | | CFRelease(ctx->current_frame); |
| 256 | | } |
| | 257 | ctx->audio_queue = NULL; |
| | 258 | ctx->video_queue = NULL; |
| | 259 | ctx->lock = NULL; |
| 257 | 260 | } |
| 258 | 261 | |
| 259 | 262 | static void parse_device_name(AVFormatContext *s) |
| … |
… |
static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
|
| 538 | 541 | static int get_video_config(AVFormatContext *s) |
| 539 | 542 | { |
| 540 | 543 | AVFContext *ctx = (AVFContext*)s->priv_data; |
| | 544 | CMSampleBufferRef sample_buffer; |
| 541 | 545 | CVImageBufferRef image_buffer; |
| 542 | 546 | CGSize image_buffer_size; |
| 543 | 547 | AVStream* stream = avformat_new_stream(s, NULL); |
| … |
… |
static int get_video_config(AVFormatContext *s)
|
| 551 | 555 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES); |
| 552 | 556 | } |
| 553 | 557 | |
| 554 | | lock_frames(ctx); |
| | 558 | [ctx->lock lock]; |
| 555 | 559 | |
| 556 | 560 | ctx->video_stream_index = stream->index; |
| 557 | 561 | |
| 558 | 562 | avpriv_set_pts_info(stream, 64, 1, avf_time_base); |
| 559 | 563 | |
| 560 | | image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame); |
| | 564 | sample_buffer = (CMSampleBufferRef)[ctx->video_queue lastObject]; |
| | 565 | image_buffer = CMSampleBufferGetImageBuffer(sample_buffer); |
| 561 | 566 | image_buffer_size = CVImageBufferGetEncodedSize(image_buffer); |
| 562 | 567 | |
| 563 | 568 | stream->codec->codec_id = AV_CODEC_ID_RAWVIDEO; |
| … |
… |
static int get_video_config(AVFormatContext *s)
|
| 566 | 571 | stream->codec->height = (int)image_buffer_size.height; |
| 567 | 572 | stream->codec->pix_fmt = ctx->pixel_format; |
| 568 | 573 | |
| 569 | | CFRelease(ctx->current_frame); |
| 570 | | ctx->current_frame = nil; |
| | 574 | [ctx->lock unlockWithCondition:QUEUE_HAS_BUFFERS]; |
| 571 | 575 | |
| 572 | | unlock_frames(ctx); |
| | 576 | [ctx->video_queue removeLastObject]; |
| 573 | 577 | |
| 574 | 578 | return 0; |
| 575 | 579 | } |
| … |
… |
static int get_video_config(AVFormatContext *s)
|
| 577 | 581 | static int get_audio_config(AVFormatContext *s) |
| 578 | 582 | { |
| 579 | 583 | AVFContext *ctx = (AVFContext*)s->priv_data; |
| | 584 | CMSampleBufferRef sample_buffer; |
| 580 | 585 | CMFormatDescriptionRef format_desc; |
| 581 | 586 | AVStream* stream = avformat_new_stream(s, NULL); |
| 582 | 587 | |
| … |
… |
static int get_audio_config(AVFormatContext *s)
|
| 589 | 594 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES); |
| 590 | 595 | } |
| 591 | 596 | |
| 592 | | lock_frames(ctx); |
| | 597 | [ctx->lock lock]; |
| 593 | 598 | |
| 594 | 599 | ctx->audio_stream_index = stream->index; |
| 595 | 600 | |
| 596 | 601 | avpriv_set_pts_info(stream, 64, 1, avf_time_base); |
| 597 | 602 | |
| 598 | | format_desc = CMSampleBufferGetFormatDescription(ctx->current_audio_frame); |
| | 603 | sample_buffer = (CMSampleBufferRef)[ctx->audio_queue lastObject]; |
| | 604 | format_desc = CMSampleBufferGetFormatDescription(sample_buffer); |
| 599 | 605 | const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc); |
| 600 | 606 | |
| 601 | 607 | if (!basic_desc) { |
| … |
… |
static int get_audio_config(AVFormatContext *s)
|
| 642 | 648 | } |
| 643 | 649 | |
| 644 | 650 | if (ctx->audio_non_interleaved) { |
| 645 | | CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame); |
| | 651 | CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(sample_buffer); |
| 646 | 652 | ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer); |
| 647 | 653 | ctx->audio_buffer = av_malloc(ctx->audio_buffer_size); |
| 648 | 654 | if (!ctx->audio_buffer) { |
| … |
… |
static int get_audio_config(AVFormatContext *s)
|
| 651 | 657 | } |
| 652 | 658 | } |
| 653 | 659 | |
| 654 | | CFRelease(ctx->current_audio_frame); |
| 655 | | ctx->current_audio_frame = nil; |
| 656 | | |
| 657 | | unlock_frames(ctx); |
| | 660 | [ctx->lock unlockWithCondition:QUEUE_HAS_BUFFERS]; |
| 658 | 661 | |
| 659 | 662 | return 0; |
| 660 | 663 | } |
| … |
… |
static int avf_read_header(AVFormatContext *s)
|
| 674 | 677 | ctx->first_pts = av_gettime(); |
| 675 | 678 | ctx->first_audio_pts = av_gettime(); |
| 676 | 679 | |
| 677 | | pthread_mutex_init(&ctx->frame_lock, NULL); |
| 678 | | pthread_cond_init(&ctx->frame_wait_cond, NULL); |
| | 680 | ctx->lock = [[NSConditionLock alloc] initWithCondition:QUEUE_IS_EMPTY]; |
| | 681 | ctx->video_queue = [[NSMutableArray alloc] initWithCapacity:QUEUE_SIZE]; |
| | 682 | ctx->audio_queue = [[NSMutableArray alloc] initWithCapacity:QUEUE_SIZE]; |
| 679 | 683 | |
| 680 | 684 | #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
| 681 | 685 | CGGetActiveDisplayList(0, NULL, &num_screens); |
| … |
… |
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
| 897 | 901 | AVFContext* ctx = (AVFContext*)s->priv_data; |
| 898 | 902 | |
| 899 | 903 | do { |
| 900 | | CVImageBufferRef image_buffer; |
| 901 | | lock_frames(ctx); |
| | 904 | int got_buffer = 0; |
| | 905 | CMSampleBufferRef asample_buffer; |
| | 906 | CMSampleBufferRef vsample_buffer; |
| | 907 | |
| | 908 | [ctx->lock lockWhenCondition:QUEUE_HAS_BUFFERS]; |
| | 909 | vsample_buffer = (CMSampleBufferRef)[ctx->video_queue lastObject]; |
| | 910 | if (vsample_buffer) { |
| | 911 | vsample_buffer = (CMSampleBufferRef)CFRetain(vsample_buffer); |
| | 912 | [ctx->video_queue removeLastObject]; |
| | 913 | |
| | 914 | got_buffer |= 1; |
| | 915 | } |
| | 916 | |
| | 917 | asample_buffer = (CMSampleBufferRef)[ctx->audio_queue lastObject]; |
| | 918 | if (asample_buffer) { |
| | 919 | asample_buffer = (CMSampleBufferRef)CFRetain(asample_buffer); |
| | 920 | [ctx->audio_queue removeLastObject]; |
| | 921 | |
| | 922 | got_buffer |= 1; |
| | 923 | } |
| 902 | 924 | |
| 903 | | image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame); |
| | 925 | if (!got_buffer || ([ctx->video_queue count] == 0 && [ctx->audio_queue count] == 0)) { |
| | 926 | [ctx->lock unlockWithCondition:QUEUE_IS_EMPTY]; |
| | 927 | } else { |
| | 928 | [ctx->lock unlock]; |
| | 929 | } |
| 904 | 930 | |
| 905 | | if (ctx->current_frame != nil) { |
| | 931 | if (vsample_buffer != nil) { |
| 906 | 932 | void *data; |
| | 933 | CVImageBufferRef image_buffer; |
| | 934 | |
| | 935 | image_buffer = CMSampleBufferGetImageBuffer(vsample_buffer); |
| 907 | 936 | if (av_new_packet(pkt, (int)CVPixelBufferGetDataSize(image_buffer)) < 0) { |
| | 937 | CVPixelBufferUnlockBaseAddress(image_buffer, 0); |
| | 938 | CFRelease(vsample_buffer); |
| 908 | 939 | return AVERROR(EIO); |
| 909 | 940 | } |
| 910 | 941 | |
| 911 | 942 | CMItemCount count; |
| 912 | 943 | CMSampleTimingInfo timing_info; |
| 913 | 944 | |
| 914 | | if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_frame, 1, &timing_info, &count) == noErr) { |
| | 945 | if (CMSampleBufferGetOutputSampleTimingInfoArray(vsample_buffer, 1, &timing_info, &count) == noErr) { |
| 915 | 946 | AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale); |
| 916 | 947 | pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q); |
| 917 | 948 | } |
| … |
… |
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
| 925 | 956 | memcpy(pkt->data, data, pkt->size); |
| 926 | 957 | |
| 927 | 958 | CVPixelBufferUnlockBaseAddress(image_buffer, 0); |
| 928 | | CFRelease(ctx->current_frame); |
| 929 | | ctx->current_frame = nil; |
| 930 | | } else if (ctx->current_audio_frame != nil) { |
| 931 | | CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame); |
| | 959 | CFRelease(vsample_buffer); |
| | 960 | vsample_buffer = NULL; |
| | 961 | } |
| | 962 | |
| | 963 | if (asample_buffer != nil) { |
| | 964 | CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(asample_buffer); |
| 932 | 965 | int block_buffer_size = CMBlockBufferGetDataLength(block_buffer); |
| 933 | 966 | |
| 934 | 967 | if (!block_buffer || !block_buffer_size) { |
| … |
… |
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
| 946 | 979 | CMItemCount count; |
| 947 | 980 | CMSampleTimingInfo timing_info; |
| 948 | 981 | |
| 949 | | if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_audio_frame, 1, &timing_info, &count) == noErr) { |
| | 982 | if (CMSampleBufferGetOutputSampleTimingInfoArray(asample_buffer, 1, &timing_info, &count) == noErr) { |
| 950 | 983 | AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale); |
| 951 | 984 | pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q); |
| 952 | 985 | } |
| … |
… |
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
| 994 | 1027 | } |
| 995 | 1028 | } |
| 996 | 1029 | |
| 997 | | CFRelease(ctx->current_audio_frame); |
| 998 | | ctx->current_audio_frame = nil; |
| 999 | | } else { |
| 1000 | | pkt->data = NULL; |
| 1001 | | pthread_cond_wait(&ctx->frame_wait_cond, &ctx->frame_lock); |
| | 1030 | CFRelease(asample_buffer); |
| | 1031 | asample_buffer = NULL; |
| 1002 | 1032 | } |
| 1003 | | |
| 1004 | | unlock_frames(ctx); |
| 1005 | 1033 | } while (!pkt->data); |
| 1006 | 1034 | |
| 1007 | 1035 | return 0; |