From 157c63114f32aefb02c22eb2d23010cbc8feceb5 Mon Sep 17 00:00:00 2001 From: Misaki Kasumi Date: Tue, 26 Mar 2024 07:13:16 +0800 Subject: [PATCH] ao_avfoundation: fix strange on partial data --- audio/out/ao_avfoundation.m | 42 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/audio/out/ao_avfoundation.m b/audio/out/ao_avfoundation.m index c6faf65a05896..4c3d0057102e2 100644 --- a/audio/out/ao_avfoundation.m +++ b/audio/out/ao_avfoundation.m @@ -70,38 +70,35 @@ static void feed(struct ao *ao) int request_sample_count = samplerate / 10; int buffer_size = request_sample_count * sstride; + void *data[] = {CFAllocatorAllocate(NULL, buffer_size, 0)}; + + int64_t cur_time_av = CMTimeGetNanoseconds([p->synchronizer currentTime]); + int64_t cur_time_mp = mp_time_ns(); + int64_t end_time_av = MPMAX(p->end_time_av, cur_time_av); + int64_t time_delta = CMTimeGetNanoseconds(CMTimeMake(request_sample_count, samplerate)); + int real_sample_count = ao_read_data_nonblocking(ao, data, request_sample_count, end_time_av - cur_time_av + cur_time_mp + time_delta); + if (real_sample_count == 0) { + // avoid spinning by blocking the thread. + mp_sleep_ns(1000000); + goto finish; + } + if ((err = CMBlockBufferCreateWithMemoryBlock( - kCFAllocatorDefault, NULL, + data[0], buffer_size, NULL, NULL, 0, - buffer_size, - kCMBlockBufferAssureMemoryNowFlag, + real_sample_count * sstride, + 0, &block_buffer )) != noErr) { MP_FATAL(ao, "failed to create block buffer\n"); MP_VERBOSE(ao, "CMBlockBufferCreateWithMemoryBlock returned %d\n", err); goto error; } - void* data[1]; - if ((err = CMBlockBufferGetDataPointer(block_buffer, 0, NULL, NULL, (char**)data)) != noErr) { - MP_FATAL(ao, "failed to get data pointer from block buffer\n"); - MP_VERBOSE(ao, "CMBlockBufferGetDataPointer returned %d\n", err); - goto error; - } - - int64_t cur_time_av = CMTimeGetNanoseconds([p->synchronizer currentTime]); - int64_t cur_time_mp = mp_time_ns(); - int64_t end_time_av = MPMAX(p->end_time_av, cur_time_av); - int64_t time_delta = CMTimeGetNanoseconds(CMTimeMake(request_sample_count, samplerate)); - int real_sample_count = ao_read_data_nonblocking(ao, data, request_sample_count, end_time_av - cur_time_av + cur_time_mp + time_delta); - if (real_sample_count == 0) { - // avoid spinning by blocking the thread. - mp_sleep_ns(1000000); - goto finish; - } + data[0] = NULL; CMSampleTimingInfo sample_timing_into[] = {(CMSampleTimingInfo) { .duration = CMTimeMake(1, samplerate), @@ -110,7 +107,7 @@ static void feed(struct ao *ao) }}; size_t sample_size_array[] = {sstride}; if ((err = CMSampleBufferCreateReady( - kCFAllocatorDefault, + NULL, block_buffer, p->format_description, real_sample_count, @@ -135,6 +132,7 @@ static void feed(struct ao *ao) error: ao_request_reload(ao); finish: + if (data[0]) CFAllocatorDeallocate(NULL, data[0]); if (block_buffer) CFRelease(block_buffer); if (sample_buffer) CFRelease(sample_buffer); } @@ -276,7 +274,7 @@ static int init(struct ao *ao) OSStatus err; if ((err = CMAudioFormatDescriptionCreate( - kCFAllocatorDefault, + NULL, &asbd, layout_size, layout,