Skip to content

Commit

Permalink
ao_avfoundation: add jitter check
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihe774 committed Mar 21, 2024
1 parent 0624592 commit ac99bff
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions audio/out/ao_avfoundation.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ static void feed(struct ao *ao)
CMBlockBufferRef block_buffer = NULL;
CMSampleBufferRef sample_buffer = NULL;

if (p->end_time_mp == -1) {
p->end_time_mp = mp_time_ns();
}
if (p->end_time_av == -1) {
p->end_time_av = CMTimeGetNanoseconds([p->synchronizer currentTime]);
int64_t time_mp = mp_time_ns();
int64_t time_av = CMTimeGetNanoseconds([p->synchronizer currentTime]);
int64_t end_time_mp = p->end_time_mp == -1 ? time_mp : p->end_time_mp;
int64_t end_time_av = p->end_time_av == -1 ? time_av : p->end_time_av;
int64_t diff = (time_mp - time_av) - (end_time_mp - end_time_av);
if (diff > 1000000 || diff < -1000000) {
MP_WARN(ao, "timestamps mismatch; restarting");
goto error;
}

int request_sample_count = ao->samplerate / 10; // 1/10 samplerate may be a good number
Expand All @@ -94,7 +97,7 @@ static void feed(struct ao *ao)
goto error;
}

int real_sample_count = ao_read_data_nonblocking(ao, data, request_sample_count, p->end_time_mp);
int real_sample_count = ao_read_data_nonblocking(ao, data, request_sample_count, end_time_mp);
if (real_sample_count == 0) {
CFRelease(block_buffer);
block_buffer = NULL;
Expand All @@ -104,7 +107,7 @@ static void feed(struct ao *ao)

CMSampleTimingInfo sample_timing_into[] = {(CMSampleTimingInfo) {
.duration = CMTimeMake(1, ao->samplerate),
.presentationTimeStamp = CMTimeFromNanoseconds(p->end_time_av),
.presentationTimeStamp = CMTimeFromNanoseconds(end_time_av),
.decodeTimeStamp = kCMTimeInvalid
}};
size_t sample_size_array[] = {ao->sstride};
Expand All @@ -130,8 +133,8 @@ static void feed(struct ao *ao)
sample_buffer = NULL;

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

return;

Expand Down

0 comments on commit ac99bff

Please sign in to comment.