/* * 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; }