Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Mar 23, 2024
1 parent f57da51 commit 20fed18
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions audio/out/ao_avfoundation.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ static CMTime CMTimeFromNanoseconds(int64_t time)
static void feed(struct ao *ao)
{
struct priv *p = ao->priv;
int samplerate = ao->samplerate;
int sstride = ao->sstride;

CMBlockBufferRef block_buffer = NULL;
CMSampleBufferRef sample_buffer = NULL;

int64_t cur_time_av = CMTimeGetNanoseconds([p->synchronizer currentTime]);
int64_t cur_time_mp = mp_time_ns();
p->end_time_av = MPMAX(p->end_time_av, cur_time_av);

int request_sample_count = ao->samplerate / 10; // 1/10 samplerate may be a good number
int buffer_size = request_sample_count * ao->sstride;
int request_sample_count = samplerate / 10; // 1/10 samplerate may be a good number
int buffer_size = request_sample_count * sstride;
if (CMBlockBufferCreateWithMemoryBlock(
kCFAllocatorDefault,
NULL,
Expand All @@ -90,19 +89,22 @@ static void feed(struct ao *ao)
goto error;
}

int64_t delta = CMTimeGetNanoseconds(CMTimeMake(request_sample_count, ao->samplerate));
int real_sample_count = ao_read_data_nonblocking(ao, data, request_sample_count, p->end_time_av - cur_time_av + cur_time_mp + delta);
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) {
mp_sleep_ns(1000000); // avoid spinning
goto finish;
}

CMSampleTimingInfo sample_timing_into[] = {(CMSampleTimingInfo) {
.duration = CMTimeMake(1, ao->samplerate),
.presentationTimeStamp = CMTimeFromNanoseconds(p->end_time_av),
.duration = CMTimeMake(1, samplerate),
.presentationTimeStamp = CMTimeFromNanoseconds(end_time_av),
.decodeTimeStamp = kCMTimeInvalid
}};
size_t sample_size_array[] = {ao->sstride};
size_t sample_size_array[] = {sstride};
if (CMSampleBufferCreateReady(
kCFAllocatorDefault,
block_buffer,
Expand All @@ -120,8 +122,8 @@ static void feed(struct ao *ao)

[p->renderer enqueueSampleBuffer:sample_buffer];

delta = CMTimeGetNanoseconds(CMTimeMake(real_sample_count, ao->samplerate));
p->end_time_av += delta;
time_delta = CMTimeGetNanoseconds(CMTimeMake(real_sample_count, samplerate));
p->end_time_av = end_time_av + time_delta;

goto finish;

Expand Down

0 comments on commit 20fed18

Please sign in to comment.