Files
utun2/tests/test_call_audio_batch.c
T

53 lines
2.9 KiB
C

/* Wire: границы размера пачек, полный разбор и атомарный отказ. */
#include "../src/call/call_audio_batch.h"
#include "../lib/debug_config.h"
#include <stdlib.h>
#include <string.h>
#define CHECK(x) do { if (!(x)) { DEBUG_ERROR(DEBUG_CATEGORY_CALL, "audio batch test line=%d: %s", __LINE__, #x); exit(1); } } while (0)
static void wire_test(void) {
uint8_t opus[57], wire[CALL_MEDIA_MAX_BYTES], saved[CALL_MEDIA_MAX_BYTES];
memset(opus, 0xa5, sizeof(opus));
struct call_audio_batch_view b = {.header = {.subcmd = CALL_SUBCMD_MEDIA, .call_id = 1, .track_id = CALL_TRACK_AUDIO,
.capture_epoch = 17, .codec_delay_samples = 312, .sent_ms = UINT32_MAX}};
size_t length = 0;
for (unsigned n = 1; n <= 10; n++) {
b.header.count = n;
b.frames[n - 1] = (struct call_audio_frame_view){.header = {(uint16_t)(65532 + n), 1000000 + n * 20000, 37}, .opus = opus};
CHECK(call_audio_batch_encode(&b, wire, sizeof(wire), &length) == 0 && length == 27 + n * 49);
struct call_audio_batch_view out;
CHECK(call_audio_batch_parse(wire, length, &out) == 0 && out.header.count == n && out.header.sent_ms == UINT32_MAX);
for (unsigned i = 0; i < n; i++) CHECK(out.frames[i].header.seq == b.frames[i].header.seq
&& out.frames[i].header.capture_pts_us == b.frames[i].header.capture_pts_us
&& out.frames[i].header.length == 37 && !memcmp(out.frames[i].opus, opus, 37));
}
struct call_audio_batch_view out, before;
memset(&out, 0x77, sizeof(out)); before = out;
for (size_t n = 0; n < length; n++) {
CHECK(call_audio_batch_parse(wire, n, &out) == -1 && !memcmp(&out, &before, sizeof(out)));
}
CHECK(call_audio_batch_parse(wire, length + 1, &out) == -1);
memcpy(saved, wire, sizeof(wire));
wire[CALL_MEDIA_HDR_SIZE - 1] = 11;
CHECK(call_audio_batch_parse(wire, length, &out) == -1);
memcpy(wire, saved, sizeof(wire));
struct call_pkt_audio_frame invalid = {1, 0, UINT16_MAX};
memcpy(wire + CALL_MEDIA_HDR_SIZE, &invalid, sizeof(invalid));
CHECK(call_audio_batch_parse(wire, length, &out) == -1);
memset(wire, 0x55, sizeof(wire)); memcpy(saved, wire, sizeof(wire));
CHECK(call_audio_batch_encode(&b, wire, length - 1, &length) == -1 && !memcmp(wire, saved, sizeof(wire)));
b.header.count = 1; b.header.capture_epoch = 0;
CHECK(call_audio_batch_encode(&b, wire, sizeof(wire), &length) == -1);
b.frames[0].header.capture_pts_us = INT64_MIN;
CHECK(call_audio_batch_encode(&b, wire, sizeof(wire), &length) == 0);
CHECK(call_audio_batch_parse(wire, length, &out) == 0 && out.frames[0].header.capture_pts_us == INT64_MIN);
}
int main(void) {
debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR);
wire_test();
debug_set_level(DEBUG_LEVEL_INFO);
DEBUG_INFO(DEBUG_CATEGORY_CALL, "PASS audio batch: 1..10, partial/trailing/invalid wire, atomic output");
return 0;
}