From 20d9d29a8ceddced6798f500296f06c05d272fa2 Mon Sep 17 00:00:00 2001 From: evgeny Date: Fri, 30 Oct 2026 19:29:06 +0100 Subject: [PATCH] Verify adaptive batch targets and reject partial sends on a free transport --- tests/test_call.c | 34 ++++++++++++++++++----- tests/test_call_audio_batch.c | 52 +++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/tests/test_call.c b/tests/test_call.c index 7002913f7..e9eee4fe0 100644 --- a/tests/test_call.c +++ b/tests/test_call.c @@ -568,6 +568,11 @@ static void call_tick(void* arg) { if (ea->ended || eb->ended) { fprintf(stderr, "temporary path loss ended call\n"); t->result = 2; break; } /* тестовый «Opus-кадр» — релей произвольных байт, не валидируем кодек */ + for (int i = 0; i < 2; i++) { + struct call_session *cs = call_session_find((i ? B : A)->call, t->call_id); + if (cs->audio_batch_timer) uasync_cancel_timeout(t->ua, cs->audio_batch_timer); + cs->audio_batch_timer = NULL; cs->audio_batch.target = 1; + } static const uint8_t opus[16] = { 0xf8, 0xff, 0xfe, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d }; struct call_capture_time invalid = sent_timing; invalid.delay_samples = 48001; @@ -594,6 +599,7 @@ static void call_tick(void* arg) { break; case P_BATCH_SEND: { struct call_session *cs = call_session_find(A->call, t->call_id); + cs->audio_batch.target = 10; struct ETCP_CONN *transport = instance_find_conn(A, nb); if (!transport) { t->result = 2; break; } queue_set_threshold(transport->send_input_q, -1, 0); @@ -622,24 +628,38 @@ static void call_tick(void* arg) { fprintf(stderr, "batch frames/timing not preserved\n"); t->result = 2; break; } struct call_session *cs = call_session_find(A->call, t->call_id); + cs->audio_batch.target = 4; t->phase_ticks = 0; uint8_t opus[16] = {0}; for (unsigned i = 11; i <= 13; i++) { struct call_capture_time timing = sent_timing; timing.pts_us += i * 20000; if (call_send_media(A, t->call_id, opus, sizeof(opus), &timing)) { t->result = 2; break; } } - if (cs->tx_batches != 2 || !cs->audio_send.conn || cs->tx_q->count != 3) { - fprintf(stderr, "partial batch did not request transport readiness\n"); t->result = 2; + if (cs->tx_batches != 2 || cs->audio_send.conn || cs->tx_q->count != 3) { + fprintf(stderr, "partial batch requested transport before target\n"); t->result = 2; } t->phase = P_PARTIAL_WAIT; } break; case P_PARTIAL_WAIT: - if (g_media[IDX_B].frames >= 14) { + if (++t->phase_ticks <= 10) { struct call_session *cs = call_session_find(A->call, t->call_id); - if (g_media[IDX_B].frames != 14 || g_audio_packets != 3 || cs->tx_batches != 3 || cs->audio_send.conn - || g_capture_timing.pts_us != sent_timing.pts_us + 260000) { - fprintf(stderr, "partial batch ready delivery failed\n"); t->result = 2; break; + if (cs->tx_batches != 2 || cs->tx_q->count != 3 || cs->audio_send.conn || g_media[IDX_B].frames != 11) { + fprintf(stderr, "partial batch escaped while transport was free\n"); t->result = 2; break; } + if (t->phase_ticks == 10) { + uint8_t opus[16] = {0}; + struct call_capture_time timing = sent_timing; timing.pts_us += 280000; + if (call_send_media(A, t->call_id, opus, sizeof(opus), &timing) || !cs->audio_send.conn) t->result = 2; + } + break; + } + if (g_media[IDX_B].frames >= 15) { + struct call_session *cs = call_session_find(A->call, t->call_id); + if (g_media[IDX_B].frames != 15 || g_audio_packets != 3 || cs->tx_batches != 3 || cs->audio_send.conn + || g_capture_timing.pts_us != sent_timing.pts_us + 280000) { + fprintf(stderr, "full batch ready delivery failed\n"); t->result = 2; break; + } + cs->audio_batch.target = 1; t->phase = P_VIDEO_CONFIG; } break; @@ -839,7 +859,7 @@ static void call_tick(void* arg) { || !pending_audio->audio_send.conn) { t->result = 2; break; } call_hangup(B, t->call_id); struct call_session* ended = call_session_find(B->call, t->call_id); - if (!ended || ended->tx_q || ended->video || ended->audio_send.conn + if (!ended || ended->tx_q || ended->video || ended->audio_send.conn || ended->audio_batch_timer || !ended->end_subcmd || !ended->teardown_timer) { DEBUG_ERROR(DEBUG_CATEGORY_CALL, "ended call retained media or lost final command"); t->result = 2; break; } diff --git a/tests/test_call_audio_batch.c b/tests/test_call_audio_batch.c index a9abc7d00..4f408b6f1 100644 --- a/tests/test_call_audio_batch.c +++ b/tests/test_call_audio_batch.c @@ -1,5 +1,6 @@ /* Wire: границы размера пачек, полный разбор и атомарный отказ. */ #include "../src/call/call_audio_batch.h" +#include "../src/routing_layer/etcp_router.h" #include "../lib/debug_config.h" #include #include @@ -43,10 +44,57 @@ static void wire_test(void) { CHECK(call_audio_batch_parse(wire, length, &out) == 0 && out.frames[0].header.capture_pts_us == INT64_MIN); } +static void step(struct call_audio_batch_control *c, struct ETCP_ROUTER_TX_STATS *n, uint64_t t) { + n->last_ack_us = t; CHECK(call_audio_batch_step(c, t, n) == 0); +} +static void control_test(void) { + struct call_audio_batch_control c; + struct ETCP_ROUTER_TX_STATS n = {.local_epoch = 1, .peer_epoch = 2, .ready = 1, .acked_packets = 1, + .delivery_rtt_us = 20000, .window = {.valid = 1}}; + call_audio_batch_init(&c, 0); + CHECK(c.target == 4); + CHECK(call_audio_batch_step(&c, 0, NULL) == 0 && c.target == 4); + n.ready = 0; n.acked_packets = 0; n.window.valid = 0; + step(&c, &n, 50000); CHECK(c.target == 4); + n.ready = 1; n.acked_packets = 1; + step(&c, &n, 100000); CHECK(c.target == 4); // Первый ACK до полного окна статистики. + n.window.valid = 1; + for (unsigned t = 150000; t < 2000000; t += 50000) step(&c, &n, t); + CHECK(c.target == 4); + /* Изолированная задержка не включает упаковку. */ + n.delivery_rtt_us = 150000; step(&c, &n, 2000000); step(&c, &n, 2050000); + n.delivery_rtt_us = 20000; step(&c, &n, 2100000); CHECK(c.target == 4); + /* Даже без queued/inflight высокий обычный RTT включает пачки. */ + n.delivery_rtt_us = 150000; + for (unsigned t = 2150000; t <= 2350000; t += 50000) step(&c, &n, t); + CHECK(c.target == 5); + for (unsigned t = 2400000; t <= 2850000; t += 50000) step(&c, &n, t); + CHECK(c.target == 6); + n.delivery_rtt_us = 300000; + for (unsigned t = 2900000; t <= 9000000; t += 50000) step(&c, &n, t); + CHECK(c.target == 10); + n.delivery_rtt_us = 10000; step(&c, &n, 9050000); + CHECK(c.target == 10); + for (unsigned t = 9100000; t <= 11050000; t += 50000) step(&c, &n, t); + CHECK(c.target == 9); + n.peer_epoch = 3; step(&c, &n, 11100000); CHECK(c.target == 4); + n.inflight_age_us = 150000; + for (unsigned t = 11150000; t <= 11350000; t += 50000) step(&c, &n, t); + CHECK(c.target == 5); + n.receive_busy = 1; + for (unsigned t = 11400000; t <= 13000000; t += 50000) step(&c, &n, t); + CHECK(c.target == 5); + n.receive_busy = 0; CHECK(call_audio_batch_step(&c, 20000000, &n) == 0 && c.target == 5); + CHECK(call_audio_batch_step(&c, 19999999, &n) == -1); + CHECK(call_audio_batch_step(&c, 20000001, NULL) == 0 && c.target == 4); + n.inflight_age_us = 0; + for (unsigned t = 20100000; t <= 27000000; t += 50000) step(&c, &n, t); + CHECK(c.target == 1); +} int main(void) { debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR); - wire_test(); + wire_test(); control_test(); debug_set_level(DEBUG_LEVEL_INFO); - DEBUG_INFO(DEBUG_CATEGORY_CALL, "PASS audio batch: 1..10, partial/trailing/invalid wire, atomic output"); + DEBUG_INFO(DEBUG_CATEGORY_CALL, "PASS audio batch: 1..10, partial/trailing/invalid wire, atomic output, adaptive target/hysteresis/epochs"); return 0; }