Files
mcde/lib/libDtChat/test_media_sync.c
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

82 lines
2.0 KiB
C

/*
* test_media_sync.c -- tests for the A/V clock (T7.5)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
int
main(void)
{
DcSync sync;
dc_sync_init(&sync);
CHECK(sync.audio_ms == 0);
/* the first frame at zero is exactly on time */
CHECK(dc_sync_video(&sync, 0) == 1);
CHECK(dc_sync_offset(&sync) == 0);
CHECK(dc_sync_dropped(&sync) == 0);
/* 20 ms of audio: 20 ms late is fine */
dc_sync_audio(&sync, 1);
CHECK(sync.audio_ms == DC_AUDIO_FRAME_MS);
CHECK(dc_sync_video(&sync, 0) == 1);
CHECK(dc_sync_offset(&sync) == -20);
/* 100 ms of audio: video at 0 is 100 ms late, more than the budget */
dc_sync_audio(&sync, 4);
CHECK(sync.audio_ms == 100);
CHECK(dc_sync_video(&sync, 0) == 0);
CHECK(dc_sync_dropped(&sync) == 1);
/* exactly the budget is still rendered */
CHECK(dc_sync_video(&sync, 20) == 1);
CHECK(dc_sync_offset(&sync) == -80);
/* early frames are rendered too */
CHECK(dc_sync_video(&sync, 120) == 1);
CHECK(dc_sync_offset(&sync) == 20);
/* one millisecond past the budget is dropped */
CHECK(dc_sync_video(&sync, 19) == 0);
CHECK(dc_sync_dropped(&sync) == 2);
/* a long call stays correct */
dc_sync_audio(&sync, 3000); /* one minute */
CHECK(sync.audio_ms == 60100);
CHECK(dc_sync_video(&sync, 60050) == 1);
CHECK(dc_sync_offset(&sync) == -50);
CHECK(dc_sync_video(&sync, 60100) == 1);
CHECK(dc_sync_offset(&sync) == 0);
/* a frame far behind is dropped, a far ahead one rendered */
CHECK(dc_sync_video(&sync, 1000) == 0);
CHECK(dc_sync_video(&sync, 70000) == 1);
/* NULL is not a crash */
dc_sync_init(NULL);
dc_sync_audio(NULL, 1);
CHECK(dc_sync_video(NULL, 0) == 0);
CHECK(dc_sync_offset(NULL) == 0);
CHECK(dc_sync_dropped(NULL) == 0);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_media_sync: all ok\n");
return 0;
}