Skip to content

Commit

Permalink
ao_avfoundation: fix strange on partial data
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Mar 25, 2024
1 parent 477310e commit 157c631
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions audio/out/ao_avfoundation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -276,7 +274,7 @@ static int init(struct ao *ao)

OSStatus err;
if ((err = CMAudioFormatDescriptionCreate(
kCFAllocatorDefault,
NULL,
&asbd,
layout_size,
layout,
Expand Down

0 comments on commit 157c631

Please sign in to comment.