Files
mcde/lib/libDtChat/test_media_call.c
lotherk ea31073ce0 dtchat: exchange media keys and wire live calls into the UI (T6.5)
The local protocol gets media-key: each side sends its ephemeral X25519 public key (base64) and UDP port over the ready text connection; both derive the direction keys with HKDF-SHA256, the caller is the initiator. localui sends the key on Conversation > Call, answers an offer with its own key, creates the call with dc_call_new_bound on the listener port (TLS and media share the number), rings, and opens the call window; an incoming ring enables Answer. The Xt loop pumps the call: drain, Opus decode to the playback stream, capture and encode only while our own PTT is held, video frames to the window, ring timeout. dc_local_conn_remote_addr gives the numeric peer address; dc_call_new_bound binds the announced port. dc_call_talking() is now only our own key, dc_call_peer_talking() the peer's (a received PTT used to start our microphone too). The call window has an Answer button and lays the buttons above the video so they stay visible. --call-first and DTCHAT_TEST_TRUST/DTCHAT_TEST_AUTOANSWER/DTCHAT_TEST_PTT drive the test: two UI instances auto-paired, exchanged media-key, rang, answered, reached IN_CALL and carried 200 Opus frames caller->callee with the callee silent (audio out 200 on the receiver, audio in 200 on the sender); screenshot build/mcde-shots/dtchat-t65-live.png. The smoke still passes; test_media_call uses peer_talking; test_media_packet covers the base64 key exchange. The test switch is documented in the security review.
2026-11-17 08:49:25 +00:00

292 lines
7.6 KiB
C

/*
* test_media_call.c -- tests for the jitter buffer and the UDP call (T6.4)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static void
test_jitter(void)
{
DcJitter *j = dc_jitter_new(3, 32);
unsigned char out[64];
size_t len = 0;
uint32_t ts = 0;
int i;
CHECK(j != NULL);
if (j == NULL)
return;
/* below the depth nothing comes out */
CHECK(dc_jitter_put(j, 0, 100, "aa", 2) == 1);
CHECK(dc_jitter_put(j, 1, 120, "bb", 2) == 1);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 0);
CHECK(dc_jitter_put(j, 2, 140, "cc", 2) == 1);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(ts == 100 && len == 2 && memcmp(out, "aa", 2) == 0);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(ts == 120 && memcmp(out, "bb", 2) == 0);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(ts == 140 && memcmp(out, "cc", 2) == 0);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 0);
/* reordering: the buffer starts at the lowest of the three */
dc_jitter_free(j);
j = dc_jitter_new(3, 32);
CHECK(j != NULL);
if (j == NULL)
return;
CHECK(dc_jitter_put(j, 10, 1000, "x", 1) == 1);
CHECK(dc_jitter_put(j, 12, 1040, "z", 1) == 1);
CHECK(dc_jitter_put(j, 11, 1020, "y", 1) == 1);
for (i = 0; i < 3; i++) {
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(len == 1);
CHECK(out[0] == (unsigned char)('x' + i));
CHECK(ts == (uint32_t)(1000 + 20 * i));
}
/* a loss becomes one frame of length 0 once a later frame proves it */
dc_jitter_free(j);
j = dc_jitter_new(3, 32);
CHECK(j != NULL);
if (j == NULL)
return;
CHECK(dc_jitter_put(j, 20, 2000, "a", 1) == 1);
CHECK(dc_jitter_put(j, 21, 2020, "b", 1) == 1);
CHECK(dc_jitter_put(j, 23, 2060, "d", 1) == 1);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(out[0] == 'a');
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(out[0] == 'b');
/* 22 is missing: with 23, 24, 25 buffered and depth 3, it is lost */
CHECK(dc_jitter_put(j, 24, 2080, "e", 1) == 1);
CHECK(dc_jitter_put(j, 25, 2100, "f", 1) == 1);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(len == 0);
CHECK(dc_jitter_get(j, &ts, out, sizeof(out), &len) == 1);
CHECK(out[0] == 'd');
/* duplicates and late frames are dropped */
CHECK(dc_jitter_put(j, 24, 2080, "e", 1) == 0); /* duplicate */
CHECK(dc_jitter_put(j, 21, 2020, "b", 1) == 0); /* late */
CHECK(dc_jitter_put(j, 25, 2100, "f", 1) == 0); /* duplicate */
/* too large a payload is refused */
{
static unsigned char big[DC_MEDIA_MAX_PAYLOAD + 1];
CHECK(dc_jitter_put(j, 30, 1, big, sizeof(big)) == -1);
}
dc_jitter_free(j);
/* a depth beyond the slots is clamped, not refused */
j = dc_jitter_new(999, 2);
CHECK(j != NULL);
dc_jitter_free(j);
j = dc_jitter_new(0, 0);
CHECK(j != NULL);
dc_jitter_free(j);
}
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) * 9000.0);
}
}
static double
rms(const int16_t *pcm, size_t n)
{
double sum = 0;
size_t i;
for (i = 0; i < n; i++)
sum += (double)pcm[i] * (double)pcm[i];
return sqrt(sum / (double)n);
}
static int
wait_frames(DcMediaCall *recv, int16_t *pcm, int *frames, int want)
{
int guard = 0;
while (*frames < want && guard++ < 50) {
if (dc_call_drain(recv) > 0) {
while (dc_call_audio_out(recv, pcm) == 1) {
(*frames)++;
if (*frames >= want)
break;
}
} else {
struct timespec pause;
pause.tv_sec = 0;
pause.tv_nsec = 10 * 1000 * 1000;
(void)nanosleep(&pause, NULL);
}
}
return *frames;
}
static void
test_call(void)
{
unsigned char pub_a[DC_MEDIA_KEY_LEN], priv_a[DC_MEDIA_KEY_LEN];
unsigned char pub_b[DC_MEDIA_KEY_LEN], priv_b[DC_MEDIA_KEY_LEN];
DcMediaKeys ka, kb;
DcMediaSend snd;
DcMediaRecv rcv;
DcMediaCall *a = NULL;
DcMediaCall *b = NULL;
int16_t pcm[DC_AUDIO_FRAME_SAMPLES];
char err[256] = "";
int port_b;
int sent;
int frames = 0;
CHECK(dc_media_keypair(pub_a, priv_a) == 0);
CHECK(dc_media_keypair(pub_b, priv_b) == 0);
CHECK(dc_media_keys_agree(priv_a, pub_b, 1, &ka) == 0);
CHECK(dc_media_keys_agree(priv_b, pub_a, 0, &kb) == 0);
dc_media_send_init(&snd, kb.send);
dc_media_recv_init(&rcv, kb.recv);
/* B first: it needs its port for A; its peer is set by the RING. */
b = dc_call_new("127.0.0.1", 9, NULL, NULL, err, sizeof(err));
CHECK(b == NULL); /* NULL keys are refused */
b = dc_call_new("127.0.0.1", 9, &snd, &rcv, err, sizeof(err));
CHECK(b != NULL);
if (b == NULL) {
fprintf(stderr, "call: %s\n", err);
return;
}
port_b = dc_call_local_port(b);
CHECK(port_b > 0);
dc_media_send_init(&snd, ka.send);
dc_media_recv_init(&rcv, ka.recv);
a = dc_call_new("127.0.0.1", port_b, &snd, &rcv, err, sizeof(err));
CHECK(a != NULL);
if (a == NULL) {
fprintf(stderr, "call: %s\n", err);
dc_call_free(b);
return;
}
CHECK(dc_call_fd(a) >= 0);
CHECK(dc_call_fd(b) >= 0);
CHECK(dc_call_fd(a) != dc_call_fd(b));
CHECK(dc_call_talking(a) == 0);
/* no audio before the call is answered */
fill_sine(pcm, 0);
CHECK(dc_call_audio_in(a, pcm) < 0);
/* ring, see it on the other side, accept, both in the call */
CHECK(dc_call_state(a) == DC_CALL_IDLE);
CHECK(dc_call_ring(a) == 0);
CHECK(dc_call_state(a) == DC_CALL_RINGING);
(void)dc_call_drain(b);
CHECK(dc_call_state(b) == DC_CALL_RINGING);
CHECK(dc_call_audio_in(a, pcm) < 0);
CHECK(dc_call_accept(b) == 0);
(void)dc_call_drain(a);
CHECK(dc_call_state(a) == DC_CALL_IN_CALL);
CHECK(dc_call_state(b) == DC_CALL_IN_CALL);
/* push-to-talk reaches the other side */
CHECK(dc_call_send_ptt(a, 1) == 0);
(void)dc_call_drain(b);
CHECK(dc_call_peer_talking(b) == 1);
CHECK(dc_call_talking(b) == 0);
CHECK(dc_call_send_ptt(a, 0) == 0);
(void)dc_call_drain(b);
CHECK(dc_call_peer_talking(b) == 0);
/* ten frames of audio, decoded in order */
for (sent = 0; sent < 10; sent++) {
fill_sine(pcm, sent);
CHECK(dc_call_audio_in(a, pcm) == 0);
}
frames = 0;
wait_frames(b, pcm, &frames, 10);
CHECK(frames == 10);
if (frames == 10) {
CHECK(rms(pcm, DC_AUDIO_FRAME_SAMPLES) > 1000.0);
}
/* a damaged datagram is dropped, nothing changes */
{
unsigned char junk[32];
int fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in addr;
CHECK(fd >= 0);
memset(junk, 0xa5, sizeof(junk));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons((uint16_t)port_b);
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
CHECK(sendto(fd, junk, sizeof(junk), 0,
(struct sockaddr *)&addr, sizeof(addr)) == 32);
(void)close(fd);
}
CHECK(dc_call_drain(b) == 0);
CHECK(dc_call_audio_out(b, pcm) == 0);
/* hanging up ends the call on both sides and stops the audio */
CHECK(dc_call_hangup(a) == 0);
CHECK(dc_call_state(a) == DC_CALL_ENDED);
(void)dc_call_drain(b);
CHECK(dc_call_state(b) == DC_CALL_ENDED);
fill_sine(pcm, 0);
CHECK(dc_call_audio_in(a, pcm) < 0);
dc_call_free(a);
dc_call_free(b);
}
int
main(void)
{
test_jitter();
test_call();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_media_call: all ok\n");
return 0;
}