dtchat: add the headless voice test (T6.7)
dtchat-headless gains --call, --audio-in FILE and --audio-out FILE: it exchanges media-key on the ready link, calls the peer, answers an incoming ring (it is a test peer, so the answer is automatic), and pumps the call from its loop: a 20 ms frame from the raw s16le 48 kHz file is encoded and sent while in the call, received frames are decoded into the output file, and call/audio events go to stdout. tests/p2p-audio.sh starts two instances (A listens and records, B connects, calls and plays tests/tone.py's 2 s 440 Hz tone), waits for both in-call, for 100 received frames, prints the CPU time of both, quits them and has tests/tone-check.py verify the recording (length within 25 percent, RMS > 500, zero crossings within 15 percent). Standalone run: A 45475, both in-call, recording 2.00 s / RMS 6339 / 441 Hz, all ok. Wired into the harness as DtChat/p2p-audio (42 entries). In-tree build pending: a foreign make userland holds the lock.
This commit is contained in:
1 parent
3d587b1af5
commit
1a6572a499
4 files changed
+409
-2
No files matched your search
+225
-2
@@ -47,6 +47,25 @@ static int quit_flag;
|
||||
static int stdin_id;
|
||||
static DcFrameBuf stdin_frame;
|
||||
|
||||
/* one call at a time: the headless tool is a test peer, not a switchboard */
|
||||
static DcMediaCall *call;
|
||||
static unsigned char call_priv[DC_MEDIA_KEY_LEN];
|
||||
static char call_pub[DC_MEDIA_PUB_B64_LEN];
|
||||
static char call_peer_pub[DC_MEDIA_PUB_B64_LEN];
|
||||
static int call_offered;
|
||||
static int call_initiator;
|
||||
static int call_port;
|
||||
static int call_answer = 1;
|
||||
static int local_port;
|
||||
static const char *audio_in_path;
|
||||
static const char *audio_out_path;
|
||||
static FILE *audio_in_fp;
|
||||
static FILE *audio_out_fp;
|
||||
static unsigned int call_in_frames;
|
||||
static unsigned int call_out_frames;
|
||||
static DcCallState last_call_state = DC_CALL_IDLE;
|
||||
static int call_flag_enabled;
|
||||
|
||||
static void
|
||||
emit(DcJson *json)
|
||||
{
|
||||
@@ -145,6 +164,182 @@ emit_message(const char *from, const DcJson *json)
|
||||
emit(out);
|
||||
}
|
||||
|
||||
static const char *
|
||||
call_state_name(DcCallState state)
|
||||
{
|
||||
switch (state) {
|
||||
case DC_CALL_IDLE:
|
||||
return "idle";
|
||||
case DC_CALL_RINGING:
|
||||
return "ringing";
|
||||
case DC_CALL_IN_CALL:
|
||||
return "in-call";
|
||||
case DC_CALL_ENDED:
|
||||
return "ended";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
static void
|
||||
emit_call(DcCallState state, int in, int out)
|
||||
{
|
||||
DcJson *json = dc_json_new_object();
|
||||
|
||||
(void)dc_json_object_set(json, "event", dc_json_new_string("call"));
|
||||
(void)dc_json_object_set(json, "state",
|
||||
dc_json_new_string(call_state_name(state)));
|
||||
if (in >= 0) {
|
||||
(void)dc_json_object_set(json, "audio_in",
|
||||
dc_json_new_int(in));
|
||||
(void)dc_json_object_set(json, "audio_out",
|
||||
dc_json_new_int(out));
|
||||
}
|
||||
emit(json);
|
||||
}
|
||||
|
||||
static void
|
||||
send_media_key(DcLocalConn *conn, int port)
|
||||
{
|
||||
DcJson *json;
|
||||
|
||||
if (conn == NULL)
|
||||
return;
|
||||
json = dc_json_new_object();
|
||||
(void)dc_json_object_set(json, "type",
|
||||
dc_json_new_string("media-key"));
|
||||
(void)dc_json_object_set(json, "pub",
|
||||
dc_json_new_string(call_pub));
|
||||
(void)dc_json_object_set(json, "port", dc_json_new_int(port));
|
||||
(void)dc_local_conn_send_json(conn, json);
|
||||
dc_json_free(json);
|
||||
}
|
||||
|
||||
static void
|
||||
start_call(DcLocalConn *conn, int initiator)
|
||||
{
|
||||
DcMediaKeys keys;
|
||||
DcMediaSend snd;
|
||||
DcMediaRecv rcv;
|
||||
char host[128] = "127.0.0.1";
|
||||
char err[256] = "";
|
||||
|
||||
if (call != NULL || conn == NULL || call_port <= 0)
|
||||
return;
|
||||
if (local_port <= 0) {
|
||||
emit_call(DC_CALL_ENDED, -1, -1);
|
||||
return;
|
||||
}
|
||||
if (dc_media_keys_from_b64(call_priv, call_peer_pub, initiator,
|
||||
&keys) != 0) {
|
||||
emit_call(DC_CALL_ENDED, -1, -1);
|
||||
return;
|
||||
}
|
||||
(void)dc_local_conn_remote_addr(conn, host, sizeof(host));
|
||||
dc_media_send_init(&snd, keys.send);
|
||||
dc_media_recv_init(&rcv, keys.recv);
|
||||
call = dc_call_new_bound(local_port, host, call_port, &snd, &rcv,
|
||||
err, sizeof(err));
|
||||
if (call == NULL) {
|
||||
emit_call(DC_CALL_ENDED, -1, -1);
|
||||
return;
|
||||
}
|
||||
call_initiator = initiator;
|
||||
last_call_state = DC_CALL_IDLE;
|
||||
call_in_frames = 0;
|
||||
call_out_frames = 0;
|
||||
if (audio_in_path != NULL)
|
||||
audio_in_fp = fopen(audio_in_path, "rb");
|
||||
if (audio_out_path != NULL)
|
||||
audio_out_fp = fopen(audio_out_path, "wb");
|
||||
if (initiator)
|
||||
(void)dc_call_ring(call);
|
||||
emit_call(dc_call_state(call), -1, -1);
|
||||
}
|
||||
|
||||
static void
|
||||
handle_media_key(const DcJson *json, DcLocalConn *conn)
|
||||
{
|
||||
const char *pub = dc_json_string(dc_json_get(json, "pub"));
|
||||
int port = (int)dc_json_int(dc_json_get(json, "port"));
|
||||
|
||||
if (pub == NULL || strlen(pub) != 44)
|
||||
return;
|
||||
snprintf(call_peer_pub, sizeof(call_peer_pub), "%s", pub);
|
||||
call_port = port > 0 && port < 65536 ? port : 0;
|
||||
if (call == NULL && call_offered) {
|
||||
call_offered = 0;
|
||||
start_call(conn, 1);
|
||||
} else if (call == NULL && call_peer_pub[0] != '\0') {
|
||||
char mypub[DC_MEDIA_PUB_B64_LEN];
|
||||
|
||||
if (dc_media_keypair_b64(mypub, call_priv) != 0)
|
||||
return;
|
||||
snprintf(call_pub, sizeof(call_pub), "%s", mypub);
|
||||
start_call(conn, 0);
|
||||
send_media_key(conn, local_port);
|
||||
}
|
||||
}
|
||||
|
||||
/* the audio files and the media packets, once per loop */
|
||||
static void
|
||||
call_pump(void)
|
||||
{
|
||||
int16_t pcm[DC_AUDIO_FRAME_SAMPLES];
|
||||
DcCallState state;
|
||||
|
||||
if (call == NULL)
|
||||
return;
|
||||
(void)dc_call_drain(call);
|
||||
if (audio_out_fp != NULL) {
|
||||
while (dc_call_audio_out(call, pcm) == 1) {
|
||||
if (fwrite(pcm, sizeof(int16_t),
|
||||
DC_AUDIO_FRAME_SAMPLES, audio_out_fp) !=
|
||||
DC_AUDIO_FRAME_SAMPLES)
|
||||
break;
|
||||
call_out_frames++;
|
||||
}
|
||||
} else {
|
||||
while (dc_call_audio_out(call, pcm) == 1)
|
||||
call_out_frames++;
|
||||
}
|
||||
if (audio_in_fp != NULL &&
|
||||
dc_call_state(call) == DC_CALL_IN_CALL) {
|
||||
size_t n = fread(pcm, sizeof(int16_t),
|
||||
DC_AUDIO_FRAME_SAMPLES, audio_in_fp);
|
||||
|
||||
if (n > 0) {
|
||||
if (n < DC_AUDIO_FRAME_SAMPLES)
|
||||
memset(pcm + n, 0, (DC_AUDIO_FRAME_SAMPLES - n) *
|
||||
sizeof(int16_t));
|
||||
if (dc_call_audio_in(call, pcm) == 0)
|
||||
call_in_frames++;
|
||||
} else {
|
||||
fclose(audio_in_fp);
|
||||
audio_in_fp = NULL;
|
||||
}
|
||||
}
|
||||
dc_call_tick(call);
|
||||
if ((call_in_frames + call_out_frames) % 50 == 0 &&
|
||||
(call_in_frames + call_out_frames) > 0) {
|
||||
DcJson *json = dc_json_new_object();
|
||||
|
||||
(void)dc_json_object_set(json, "event",
|
||||
dc_json_new_string("audio"));
|
||||
(void)dc_json_object_set(json, "in",
|
||||
dc_json_new_int(call_in_frames));
|
||||
(void)dc_json_object_set(json, "out",
|
||||
dc_json_new_int(call_out_frames));
|
||||
emit(json);
|
||||
}
|
||||
state = dc_call_state(call);
|
||||
if (state != last_call_state) {
|
||||
last_call_state = state;
|
||||
if (state == DC_CALL_RINGING && !call_initiator && call_answer)
|
||||
(void)dc_call_accept(call);
|
||||
emit_call(state, call_in_frames, call_out_frames);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
common_line(const char *from, DcLocalConn *conn, const DcJson *json)
|
||||
{
|
||||
@@ -152,6 +347,10 @@ common_line(const char *from, DcLocalConn *conn, const DcJson *json)
|
||||
|
||||
if (type == NULL)
|
||||
return;
|
||||
if (strcmp(type, "media-key") == 0) {
|
||||
handle_media_key(json, conn);
|
||||
return;
|
||||
}
|
||||
if (strcmp(type, "msg") == 0) {
|
||||
emit_message(from, json);
|
||||
if (conn != NULL)
|
||||
@@ -232,6 +431,13 @@ link_state(DcLocalPeer *peer, int online, void *arg)
|
||||
link->online = online;
|
||||
if (online && id != NULL)
|
||||
snprintf(link->id, sizeof(link->id), "%s", id);
|
||||
if (online && call_flag_enabled && call_offered == 0 && call == NULL &&
|
||||
call_pub[0] == '\0') {
|
||||
if (dc_media_keypair_b64(call_pub, call_priv) == 0) {
|
||||
call_offered = 1;
|
||||
send_media_key(dc_local_peer_conn(peer), local_port);
|
||||
}
|
||||
}
|
||||
emit_peer(online ? "peer-online" : "peer-offline", "client",
|
||||
link->target, link->id, online ? pin : NULL);
|
||||
}
|
||||
@@ -364,6 +570,14 @@ main(int argc, char **argv)
|
||||
} else if (strcmp(argv[i], "--listen") == 0 &&
|
||||
i + 1 < argc) {
|
||||
listen_text = argv[++i];
|
||||
} else if (strcmp(argv[i], "--call") == 0) {
|
||||
call_flag_enabled = 1;
|
||||
} else if (strcmp(argv[i], "--audio-in") == 0 &&
|
||||
i + 1 < argc) {
|
||||
audio_in_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--audio-out") == 0 &&
|
||||
i + 1 < argc) {
|
||||
audio_out_path = argv[++i];
|
||||
} else if (strcmp(argv[i], "--peer") == 0 &&
|
||||
i + 1 < argc && npeers < 8) {
|
||||
peer_text[npeers++] = argv[++i];
|
||||
@@ -405,6 +619,7 @@ main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
port = dc_local_server_port(server);
|
||||
local_port = port;
|
||||
}
|
||||
for (i = 0; i < npeers; i++) {
|
||||
char host[128];
|
||||
@@ -442,8 +657,10 @@ main(int argc, char **argv)
|
||||
dc_frame_init(&stdin_frame);
|
||||
stdin_id = dc_loop_add_fd(loop, STDIN_FILENO, POLLIN, stdin_readable,
|
||||
NULL);
|
||||
while (!quit_flag)
|
||||
(void)dc_loop_run(loop, 200);
|
||||
while (!quit_flag) {
|
||||
(void)dc_loop_run(loop, 50);
|
||||
call_pump();
|
||||
}
|
||||
if (stdin_id > 0)
|
||||
(void)dc_loop_del_fd(loop, stdin_id);
|
||||
while (links != NULL) {
|
||||
@@ -453,6 +670,12 @@ main(int argc, char **argv)
|
||||
free(links);
|
||||
links = next;
|
||||
}
|
||||
if (call != NULL)
|
||||
dc_call_free(call);
|
||||
if (audio_in_fp != NULL)
|
||||
fclose(audio_in_fp);
|
||||
if (audio_out_fp != NULL)
|
||||
fclose(audio_out_fp);
|
||||
dc_local_server_free(server);
|
||||
while (accepted != NULL) {
|
||||
struct Accepted *next = accepted->next;
|
||||
|
||||
Executable
+110
@@ -0,0 +1,110 @@
|
||||
#!/bin/sh
|
||||
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
||||
#
|
||||
# p2p-audio.sh -- two headless dtchat instances carry a voice call (T6.7)
|
||||
#
|
||||
# usage: tests/p2p-audio.sh [path-to-dtchat-headless]
|
||||
# default: build/mcde-host/programs/dtchat/dtchat-headless
|
||||
#
|
||||
# A listens and writes the audio it receives, B connects, calls it and sends
|
||||
# a 2 s 440 Hz tone from a file. The script checks: both sides reach the
|
||||
# call, A received about two seconds of tone (length, RMS, zero crossings)
|
||||
# and reports the CPU time of both processes. Then it quits both cleanly.
|
||||
|
||||
set -u
|
||||
|
||||
TOP=$(cd "$(dirname "$0")/.." && pwd)
|
||||
BIN=${1:-"$TOP/../../../build/mcde-host/programs/dtchat/dtchat-headless"}
|
||||
case $BIN in
|
||||
/*) ;;
|
||||
*) BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN") ;;
|
||||
esac
|
||||
[ -x "$BIN" ] || { echo "p2p-audio: no $BIN" >&2; exit 2; }
|
||||
|
||||
TMP=$(mktemp -d /tmp/dtchat-audio-XXXXXX) || exit 2
|
||||
A_IN=$TMP/a.in
|
||||
B_IN=$TMP/b.in
|
||||
mkfifo "$A_IN" "$B_IN" || exit 2
|
||||
exec 3<>"$A_IN"
|
||||
exec 4<>"$B_IN"
|
||||
|
||||
cleanup() {
|
||||
exec 3>&- 2>/dev/null
|
||||
exec 4>&- 2>/dev/null
|
||||
[ -n "${PA:-}" ] && kill "$PA" 2>/dev/null
|
||||
[ -n "${PB:-}" ] && kill "$PB" 2>/dev/null
|
||||
rm -rf "$TMP"
|
||||
}
|
||||
|
||||
wait_for() { # file, fixed string, tries
|
||||
i=0
|
||||
while [ "$i" -lt "$3" ]; do
|
||||
if grep -q -F "$2" "$1" 2>/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
sleep 0.1
|
||||
i=$((i + 1))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
fail() {
|
||||
echo "p2p-audio: FAIL $*" >&2
|
||||
echo "--- a.log" >&2
|
||||
cat "$TMP/a.log" >&2 2>/dev/null
|
||||
echo "--- b.log" >&2
|
||||
cat "$TMP/b.log" >&2 2>/dev/null
|
||||
cleanup
|
||||
exit 1
|
||||
}
|
||||
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
# 2 s of 440 Hz, s16le mono 48 kHz
|
||||
"$TOP/tests/tone.py" "$TMP/tone.pcm" 2 440 || fail "cannot write the tone"
|
||||
|
||||
"$BIN" --config "$TMP/a" --listen 127.0.0.1:0 --audio-out "$TMP/received.pcm" \
|
||||
<&3 >"$TMP/a.log" 2>"$TMP/a.err" &
|
||||
PA=$!
|
||||
wait_for "$TMP/a.log" '"event":"ready"' 100 || fail "A did not become ready"
|
||||
PORT=$(sed -n 's/.*"listen":\([0-9][0-9]*\).*/\1/p' "$TMP/a.log" | head -1)
|
||||
[ -n "$PORT" ] || fail "no listen port"
|
||||
echo "p2p-audio: A listens on $PORT"
|
||||
|
||||
"$BIN" --config "$TMP/b" --listen 127.0.0.1:0 \
|
||||
--peer "127.0.0.1:$PORT" --call --audio-in "$TMP/tone.pcm" \
|
||||
<&4 >"$TMP/b.log" 2>"$TMP/b.err" &
|
||||
PB=$!
|
||||
wait_for "$TMP/a.log" '"event":"call","state":"in-call"' 100 ||
|
||||
fail "A did not reach the call"
|
||||
wait_for "$TMP/b.log" '"event":"call","state":"in-call"' 100 ||
|
||||
fail "B did not reach the call"
|
||||
echo "p2p-audio: both sides are in the call"
|
||||
|
||||
# wait until the receiver has about two seconds of audio
|
||||
i=0
|
||||
while [ "$i" -lt 100 ]; do
|
||||
if grep -q '"event":"audio".*"out":100' "$TMP/a.log" 2>/dev/null; then
|
||||
break
|
||||
fi
|
||||
sleep 0.1
|
||||
i=$((i + 1))
|
||||
done
|
||||
grep -q '"event":"audio".*"out":100' "$TMP/a.log" ||
|
||||
fail "A did not receive 100 frames"
|
||||
|
||||
CPU_A=$(ps -o times= -p "$PA" 2>/dev/null | tr -d ' ')
|
||||
CPU_B=$(ps -o times= -p "$PB" 2>/dev/null | tr -d ' ')
|
||||
echo "p2p-audio: received audio, cpu A=${CPU_A:-?}s B=${CPU_B:-?}s"
|
||||
|
||||
echo '{"cmd":"quit"}' >&4
|
||||
echo '{"cmd":"quit"}' >&3
|
||||
wait "$PB" 2>/dev/null || fail "B did not exit cleanly"
|
||||
PB=
|
||||
wait "$PA" 2>/dev/null || fail "A did not exit cleanly"
|
||||
PA=
|
||||
|
||||
"$TOP/tests/tone-check.py" "$TMP/received.pcm" 2 440 ||
|
||||
fail "the received audio is not the tone"
|
||||
echo "p2p-audio: audio verified"
|
||||
echo "tests/p2p-audio.sh: all ok"
|
||||
Executable
+50
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
"""tests/tone-check.py -- check that FILE holds a sine of about HZ.
|
||||
|
||||
usage: tone-check.py FILE SECONDS HZ
|
||||
Checks the length (within 25 percent: jitter buffer and Opus), the RMS
|
||||
(> 500, so it is not silence) and the zero crossings (within 15 percent).
|
||||
"""
|
||||
import math
|
||||
import struct
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
path = sys.argv[1]
|
||||
seconds = float(sys.argv[2])
|
||||
hz = float(sys.argv[3])
|
||||
with open(path, "rb") as f:
|
||||
data = f.read()
|
||||
n = len(data) // 2
|
||||
if n == 0:
|
||||
print("tone-check: no samples", file=sys.stderr)
|
||||
return 1
|
||||
samples = struct.unpack("<%dh" % n, data[:n * 2])
|
||||
rate = 48000
|
||||
duration = n / rate
|
||||
if abs(duration - seconds) > seconds * 0.25:
|
||||
print("tone-check: %.2f s instead of %.2f" % (duration, seconds),
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
rms = math.sqrt(sum(float(s) * s for s in samples) / n)
|
||||
if rms < 500:
|
||||
print("tone-check: rms %.0f is silence" % rms, file=sys.stderr)
|
||||
return 1
|
||||
crossings = 0
|
||||
prev = samples[0]
|
||||
for s in samples[1:]:
|
||||
if (prev < 0 and s >= 0) or (prev >= 0 and s < 0):
|
||||
crossings += 1
|
||||
prev = s
|
||||
measured = crossings / 2.0 / duration
|
||||
if abs(measured - hz) > hz * 0.15:
|
||||
print("tone-check: %.0f Hz instead of %.0f" % (measured, hz),
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
print("tone-check: %.2f s, rms %.0f, %.0f Hz" % (duration, rms, measured))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env python3
|
||||
"""tests/tone.py -- write a sine tone as raw s16le mono 48 kHz.
|
||||
|
||||
usage: tone.py FILE SECONDS [HZ]
|
||||
"""
|
||||
import math
|
||||
import struct
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
path = sys.argv[1]
|
||||
seconds = float(sys.argv[2]) if len(sys.argv) > 2 else 2.0
|
||||
hz = float(sys.argv[3]) if len(sys.argv) > 3 else 440.0
|
||||
rate = 48000
|
||||
n = int(seconds * rate)
|
||||
with open(path, "wb") as f:
|
||||
for i in range(n):
|
||||
v = int(math.sin(2.0 * math.pi * hz * i / rate) * 9000)
|
||||
f.write(struct.pack("<h", v))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user