Files
lotherk 76ccabd127 dtchat: synchronise video to the audio clock (T7.5)
media_sync.c: audio is the clock (20 ms per decoded frame); a video frame whose timestamp is more than DC_SYNC_LATE_MS (80 ms) behind it is dropped, anything else is rendered and its video-audio offset recorded. media_call.c advances the clock on every decoded audio frame, timestamps video at 1000/15 ms per frame, drops late frames in dc_call_video_out (-1) and counts them; the reassembler now carries the media packet's timestamp with the frame. test_media_sync: on-time, 20 ms late, exactly 80 ms (rendered), 81 ms (dropped), early, a one-minute call and NULL arguments. test_video_packet gains a call-level sequence: 200 ms of decoded audio, then four video frames at 0/66/132/198 ms; the first two are dropped, the third renders with offset -68 ms and the dropped counter is 2. Standalone and ASan/UBSan clean. In-tree build pending: the next make mcde-host carries it and the harness reaches 41 entries.
2026-11-17 08:27:58 +00:00

63 lines
1.4 KiB
C

/*
* media_sync.c -- A/V synchronisation for a call (T7.5)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* The audio is the clock: every decoded audio frame is 20 ms of playout.
* A video frame carries a timestamp in the same milliseconds; if it arrives
* more than DC_SYNC_LATE_MS behind the audio clock it is dropped (late lips
* are worse than a skipped frame), otherwise it is rendered and its offset
* (video - audio) becomes the lip-sync error of that frame. A frame that is
* early is rendered too; there is no video queue in v1, the sender paces at
* the frame rate.
*/
#include "dc_internal.h"
void
dc_sync_init(DcSync *sync)
{
if (sync == NULL)
return;
sync->audio_ms = 0;
sync->offset_ms = 0;
sync->rendered = 0;
sync->dropped = 0;
}
void
dc_sync_audio(DcSync *sync, int frames)
{
if (sync == NULL)
return;
sync->audio_ms += (long long)frames * DC_AUDIO_FRAME_MS;
}
int
dc_sync_video(DcSync *sync, long long video_ms)
{
long long late;
if (sync == NULL)
return 0;
late = sync->audio_ms - video_ms;
if (late > DC_SYNC_LATE_MS) {
sync->dropped++;
return 0;
}
sync->offset_ms = video_ms - sync->audio_ms;
sync->rendered++;
return 1;
}
long long
dc_sync_offset(const DcSync *sync)
{
return sync != NULL ? sync->offset_ms : 0;
}
int
dc_sync_dropped(const DcSync *sync)
{
return sync != NULL ? sync->dropped : 0;
}