diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index 4119f05e..c53ca14d 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -13,7 +13,7 @@ libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include $(VPX_CPPFLAGS) libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c media_audio.c \ media_packet.c media_call.c media_video.c media_video_packet.c \ - media_render.c dc_internal.h + media_render.c media_sync.c dc_internal.h libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) $(OPUSLIB) $(VPXLIB) libDtChat_la_LDFLAGS = -version-info 1:0:0 @@ -22,8 +22,8 @@ libDtChat_la_LDFLAGS = -version-info 1:0:0 noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \ - test_video_packet test_video_render test_adapter test_instance \ - fake_adapter + test_video_packet test_video_render test_media_sync test_adapter \ + test_instance fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -94,7 +94,11 @@ test_video_packet_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(OPUSLIB) - test_video_render_SOURCES = test_video_render.c test_video_render_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_video_render_LDADD = libDtChat.la libDtChatIpc.la -lm +test_media_sync_SOURCES = test_media_sync.c +test_media_sync_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_media_sync_LDADD = libDtChat.la libDtChatIpc.la TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \ - test_video_packet test_video_render test_adapter test_instance + test_video_packet test_video_render test_media_sync test_adapter \ + test_instance diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index ad59f5e0..b631c360 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -471,8 +471,15 @@ int dc_call_audio_out(DcMediaCall *call, int16_t *pcm); /* one encoded frame out (fragmented) and the completed frames in */ int dc_call_send_video(DcMediaCall *call, uint16_t frame_id, const unsigned char *frame, size_t len, int keyframe); +/* 1: a frame to render, 0: nothing complete, -1: a frame was dropped + * because it was more than DC_SYNC_LATE_MS behind the audio (or OUT is too + * small) */ int dc_call_video_out(DcMediaCall *call, unsigned char *out, size_t outlen, size_t *len, int *keyframe, uint16_t *frame_id); +/* the last rendered frame's video - audio offset, and the number of frames + * dropped because they were more than DC_SYNC_LATE_MS late */ +long long dc_call_sync_offset(const DcMediaCall *call); +int dc_call_video_dropped(const DcMediaCall *call); int dc_call_wants_keyframe(DcMediaCall *call); DcJitter *dc_call_jitter(const DcMediaCall *call); void dc_call_free(DcMediaCall *call); @@ -520,13 +527,38 @@ int dc_video_fragment_make(uint16_t frame_id, int keyframe, int index, size_t outlen, size_t *outlen_out); DcVideoReasm *dc_video_reasm_new(size_t max_frame); void dc_video_reasm_free(DcVideoReasm *reasm); -int dc_video_reasm_put(DcVideoReasm *reasm, const unsigned char *payload, - size_t len); +int dc_video_reasm_put(DcVideoReasm *reasm, uint32_t ts, + const unsigned char *payload, size_t len); int dc_video_reasm_ready(const DcVideoReasm *reasm); int dc_video_reasm_frame(DcVideoReasm *reasm, unsigned char *out, - size_t outlen, size_t *len_out, int *keyframe, uint16_t *frame_id); + size_t outlen, size_t *len_out, int *keyframe, uint16_t *frame_id, + uint32_t *ts); int dc_video_reasm_wants_keyframe(DcVideoReasm *reasm); +/* + * A/V sync (T7.5): the audio is the clock. dc_sync_audio() advances it by + * one 20 ms frame per played audio frame; dc_sync_video() takes a video + * timestamp in the same milliseconds and answers 0 when the frame is more + * than DC_SYNC_LATE_MS behind the audio (the caller drops it) and 1 when it + * should be rendered. The offset of the last rendered frame is video - + * audio, so the lip-sync budget is |offset| <= DC_SYNC_LATE_MS for a frame + * that was on time. + */ +#define DC_SYNC_LATE_MS 80 + +typedef struct { + long long audio_ms; + long long offset_ms; + int rendered; + int dropped; +} DcSync; + +void dc_sync_init(DcSync *sync); +void dc_sync_audio(DcSync *sync, int frames); +int dc_sync_video(DcSync *sync, long long video_ms); +long long dc_sync_offset(const DcSync *sync); +int dc_sync_dropped(const DcSync *sync); + /* * Rendering (T7.2): bilinear I420 scaling and the BT.601 limited-range * conversion to 0x00RRGGBB pixels for an XImage. STRIDE_PX is in pixels diff --git a/lib/libDtChat/media_call.c b/lib/libDtChat/media_call.c index 50ee4bcd..d044265b 100644 --- a/lib/libDtChat/media_call.c +++ b/lib/libDtChat/media_call.c @@ -57,6 +57,9 @@ struct DcMediaCall { DcMediaRecv rcv; DcJitter *jitter; DcVideoReasm *video; + DcSync sync; + uint32_t video_ts; + int video_dropped; uint32_t frames; int talking; long long last_in; @@ -231,6 +234,7 @@ dc_call_new(const char *peer_host, int peer_port, const DcMediaSend *send, call->rcv = *recv; call->state = DC_CALL_IDLE; call->state_since = dc_loop_now_ms(); + dc_sync_init(&call->sync); if (call->fd < 0 || call->enc == NULL || call->dec == NULL || call->jitter == NULL) { if (err != NULL && errlen > 0 && err[0] == '\0') @@ -461,7 +465,7 @@ dc_call_drain(DcMediaCall *call) call->video = dc_video_reasm_new( DC_VIDEO_FRAME_MAX); if (call->video != NULL) - (void)dc_video_reasm_put(call->video, + (void)dc_video_reasm_put(call->video, ts, plain, len); continue; } @@ -487,9 +491,15 @@ dc_call_audio_out(DcMediaCall *call, int16_t *pcm) if (dc_jitter_get(call->jitter, &ts, payload, sizeof(payload), &len) != 1) return 0; - if (len == 0) - return dc_opus_dec_frame(call->dec, NULL, 0, pcm) > 0; + if (len == 0) { + n = dc_opus_dec_frame(call->dec, NULL, 0, pcm); + if (n > 0) + dc_sync_audio(&call->sync, 1); + return n > 0; + } n = dc_opus_dec_frame(call->dec, payload, len, pcm); + if (n > 0) + dc_sync_audio(&call->sync, 1); return n > 0; } @@ -514,8 +524,9 @@ dc_call_send_video(DcMediaCall *call, uint16_t frame_id, if (dc_video_fragment_make(frame_id, keyframe, i, count, frame, len, fragment, sizeof(fragment), &flen) != 0) return -1; - plen = dc_media_seal(&call->snd, DC_MEDIA_VIDEO, frame_id, - fragment, flen, packet, sizeof(packet)); + plen = dc_media_seal(&call->snd, DC_MEDIA_VIDEO, + call->video_ts, fragment, flen, packet, + sizeof(packet)); if (plen == 0) return -1; if (sendto(call->fd, packet, plen, 0, @@ -523,6 +534,7 @@ dc_call_send_video(DcMediaCall *call, uint16_t frame_id, sizeof(call->peer)) != (ssize_t)plen) return -1; } + call->video_ts += (uint32_t)(1000 / DC_VIDEO_FPS); return 0; } @@ -530,10 +542,32 @@ int dc_call_video_out(DcMediaCall *call, unsigned char *out, size_t outlen, size_t *len, int *keyframe, uint16_t *frame_id) { + uint32_t ts = 0; + int rc; + if (call == NULL || call->video == NULL) return 0; - return dc_video_reasm_frame(call->video, out, outlen, len, - keyframe, frame_id); + rc = dc_video_reasm_frame(call->video, out, outlen, len, keyframe, + frame_id, &ts); + if (rc != 1) + return rc; + if (!dc_sync_video(&call->sync, ts)) { + call->video_dropped++; + return -1; /* too late, the audio has moved on */ + } + return 1; +} + +long long +dc_call_sync_offset(const DcMediaCall *call) +{ + return call != NULL ? dc_sync_offset(&call->sync) : 0; +} + +int +dc_call_video_dropped(const DcMediaCall *call) +{ + return call != NULL ? call->video_dropped : 0; } int diff --git a/lib/libDtChat/media_sync.c b/lib/libDtChat/media_sync.c new file mode 100644 index 00000000..76b1be03 --- /dev/null +++ b/lib/libDtChat/media_sync.c @@ -0,0 +1,62 @@ +/* + * 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; +} diff --git a/lib/libDtChat/media_video_packet.c b/lib/libDtChat/media_video_packet.c index e73cc73c..e132aaa6 100644 --- a/lib/libDtChat/media_video_packet.c +++ b/lib/libDtChat/media_video_packet.c @@ -41,6 +41,8 @@ struct DcVideoReasm { int have_id; int completed; int wants_keyframe; + uint32_t ts; /* timestamp of the frame being built */ + uint32_t last_ts; /* timestamp of the last completed frame */ }; size_t @@ -141,7 +143,8 @@ reset_frame(DcVideoReasm *r, uint16_t frame_id, uint16_t count, int keyframe) } int -dc_video_reasm_put(DcVideoReasm *r, const unsigned char *payload, size_t len) +dc_video_reasm_put(DcVideoReasm *r, uint32_t ts, const unsigned char *payload, + size_t len) { uint16_t frame_id, count, index; size_t off, n; @@ -166,8 +169,10 @@ dc_video_reasm_put(DcVideoReasm *r, const unsigned char *payload, size_t len) r->wants_keyframe = 1; r->have_id = 0; } - if (!r->have_id) + if (!r->have_id) { reset_frame(r, frame_id, count, (payload[0] & 4) != 0); + r->ts = ts; + } if (r->count != count) return -1; if ((payload[0] & 4) != 0) @@ -193,7 +198,7 @@ dc_video_reasm_ready(const DcVideoReasm *r) int dc_video_reasm_frame(DcVideoReasm *r, unsigned char *out, size_t outlen, - size_t *len_out, int *keyframe, uint16_t *frame_id) + size_t *len_out, int *keyframe, uint16_t *frame_id, uint32_t *ts) { if (r == NULL || out == NULL || len_out == NULL || !dc_video_reasm_ready(r)) @@ -206,6 +211,9 @@ dc_video_reasm_frame(DcVideoReasm *r, unsigned char *out, size_t outlen, *keyframe = r->keyframe; if (frame_id != NULL) *frame_id = r->frame_id; + if (ts != NULL) + *ts = r->ts; + r->last_ts = r->ts; r->completed = 1; r->have_id = 0; return 1; diff --git a/lib/libDtChat/test_media_sync.c b/lib/libDtChat/test_media_sync.c new file mode 100644 index 00000000..a95442db --- /dev/null +++ b/lib/libDtChat/test_media_sync.c @@ -0,0 +1,81 @@ +/* + * 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 + +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; +} diff --git a/lib/libDtChat/test_video_packet.c b/lib/libDtChat/test_video_packet.c index 521ba493..07e96642 100644 --- a/lib/libDtChat/test_video_packet.c +++ b/lib/libDtChat/test_video_packet.c @@ -5,11 +5,16 @@ */ #include "dc_internal.h" +#include #include #include #include #include +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + static int fails; #define CHECK(cond) do { \ @@ -22,6 +27,19 @@ static int fails; #define FRAME_LEN 50000 +static void +fill_sine(int16_t *pcm, int frame) +{ + int i; + + for (i = 0; i < DC_AUDIO_FRAME_SAMPLES; i++) { + double t = (double)(frame * DC_AUDIO_FRAME_SAMPLES + i) / + (double)DC_AUDIO_RATE; + + pcm[i] = (int16_t)(sin(2.0 * M_PI * 440.0 * t) * 8000.0); + } +} + static unsigned char frame[DC_VIDEO_FRAME_MAX]; static unsigned char got[DC_VIDEO_FRAME_MAX]; static unsigned char fragment[DC_MEDIA_MAX_PAYLOAD]; @@ -43,6 +61,7 @@ test_unit(void) int i; int keyframe = 0; uint16_t frame_id = 0; + uint32_t ts = 0; size_t len = 0; size_t flen; @@ -65,11 +84,11 @@ test_unit(void) CHECK(flen <= DC_MEDIA_MAX_PAYLOAD); CHECK((fragment[0] & 1) == (i == 0 ? 1 : 0)); CHECK((fragment[0] & 2) == (i == count - 1 ? 2 : 0)); - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, (uint32_t)i, fragment, flen) == 1); } CHECK(dc_video_reasm_ready(r)); CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe, - &frame_id) == 1); + &frame_id, &ts) == 1); CHECK(len == FRAME_LEN); CHECK(keyframe == 1); CHECK(frame_id == 1); @@ -78,19 +97,19 @@ test_unit(void) /* out of order, with a duplicate */ CHECK(dc_video_fragment_make(2, 0, 0, count, frame, FRAME_LEN, fragment, sizeof(fragment), &flen) == 0); - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, 0, fragment, flen) == 1); for (i = count - 1; i > 0; i--) { CHECK(dc_video_fragment_make(2, 0, i, count, frame, FRAME_LEN, fragment, sizeof(fragment), &flen) == 0); - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, (uint32_t)i, fragment, flen) == 1); if (i == count - 1) { - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, (uint32_t)i, fragment, flen) == 1); CHECK(!dc_video_reasm_ready(r)); } } CHECK(dc_video_reasm_ready(r)); CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe, - &frame_id) == 1); + &frame_id, &ts) == 1); CHECK(len == FRAME_LEN && frame_id == 2 && keyframe == 0); /* a lost fragment: the frame never completes, the next frame id asks @@ -100,25 +119,26 @@ test_unit(void) continue; CHECK(dc_video_fragment_make(3, 0, i, count, frame, FRAME_LEN, fragment, sizeof(fragment), &flen) == 0); - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, (uint32_t)i, fragment, flen) == 1); } CHECK(!dc_video_reasm_ready(r)); CHECK(dc_video_reasm_wants_keyframe(r) == 0); CHECK(dc_video_fragment_make(4, 1, 0, 1, frame, 100, fragment, sizeof(fragment), &flen) == 0); - CHECK(dc_video_reasm_put(r, fragment, flen) == 1); + CHECK(dc_video_reasm_put(r, 3, fragment, flen) == 1); CHECK(dc_video_reasm_wants_keyframe(r) == 1); CHECK(dc_video_reasm_wants_keyframe(r) == 0); CHECK(dc_video_reasm_ready(r)); CHECK(dc_video_reasm_frame(r, got, sizeof(got), &len, &keyframe, - &frame_id) == 1); + &frame_id, &ts) == 1); CHECK(len == 100 && frame_id == 4 && keyframe == 1); + CHECK(ts == 3); /* bad fragments are refused */ - CHECK(dc_video_reasm_put(r, fragment, 4) == -1); + CHECK(dc_video_reasm_put(r, 4, fragment, 4) == -1); fragment[6] = 1; fragment[7] = 0; /* count 256 */ - CHECK(dc_video_reasm_put(r, fragment, flen) == -1); + CHECK(dc_video_reasm_put(r, 4, fragment, flen) == -1); dc_video_reasm_free(r); } @@ -209,6 +229,57 @@ main(void) dc_call_free(a); dc_call_free(b); + + /* T7.5: the audio clock decides whether a video frame is too late. + * A fresh call: 200 ms of audio, then video timestamps 0, 66, 132, + * 198; the first two are more than 80 ms behind and are dropped. */ + { + DcMediaCall *c = NULL; + DcMediaCall *d = NULL; + int port_d; + int i; + + dc_media_send_init(&snd, kb.send); + dc_media_recv_init(&rcv, kb.recv); + d = dc_call_new("127.0.0.1", 9, &snd, &rcv, err, + sizeof(err)); + CHECK(d != NULL); + port_d = dc_call_local_port(d); + dc_media_send_init(&snd, ka.send); + dc_media_recv_init(&rcv, ka.recv); + c = dc_call_new("127.0.0.1", port_d, &snd, &rcv, err, + sizeof(err)); + CHECK(c != NULL); + CHECK(dc_call_ring(c) == 0); + (void)dc_call_drain(d); + CHECK(dc_call_accept(d) == 0); + (void)dc_call_drain(c); + for (i = 0; i < 10; i++) { + fill_sine(pcm, i); + CHECK(dc_call_audio_in(c, pcm) == 0); + } + (void)dc_call_drain(d); + for (i = 0; i < 10; i++) { + CHECK(dc_call_audio_out(d, pcm) == 1); + } + CHECK(dc_call_send_video(c, 1, frame, 1000, 0) == 0); + (void)dc_call_drain(d); + CHECK(dc_call_video_out(d, got, sizeof(got), &len, &keyframe, + &frame_id) == -1); + CHECK(dc_call_send_video(c, 2, frame, 1000, 0) == 0); + (void)dc_call_drain(d); + CHECK(dc_call_video_out(d, got, sizeof(got), &len, &keyframe, + &frame_id) == -1); + CHECK(dc_call_send_video(c, 3, frame, 1000, 0) == 0); + (void)dc_call_drain(d); + CHECK(dc_call_video_out(d, got, sizeof(got), &len, &keyframe, + &frame_id) == 1); + CHECK(dc_call_sync_offset(d) == -68); + CHECK(dc_call_video_dropped(d) == 2); + dc_call_free(c); + dc_call_free(d); + } + if (fails != 0) { fprintf(stderr, "%d test(s) failed\n", fails); return 1;