Files
lotherk 1a6572a499 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.
2026-11-17 08:54:05 +00:00

111 lines
3.2 KiB
Bash
Executable File

#!/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"