根因(级联死锁): 内核驱动 wedge(hbc3000/cs43198 时钟域族, write/PREPARE/amixer ioctl 卡死在内核) → 播放线程永不返回 → ring 填满 → IO 线程在 ring_write 无超时 cond_wait 永久睡眠 → accept 停摆(backlog 满后 CLI ECONNREFUSED) → ncm 堵 socket 写 → 全链冻结。 同效路径: popen_read_tmo 无界 waitpid 随挂死的 amixer 子进程拖死持锁线程。 修法: - ring 写有界等待(MEOW_AUDIO_RING_WAIT_MS 缺省 5s)超时丢弃+计数(dropped=), IO 永生; 等待期 sid 代际守卫(抢占中等待的旧流 PCM 不再注入新流 ring) - 全部 cond 等待 timedwait 化(播放 1s 周期自愈, g_cond 改 CLOCK_MONOTONIC); 信号处理器只置 g_stop(不再调非 async-signal-safe 的 cond_signal) - popen 收割有界: WNOHANG 300ms → SIGKILL 300ms → D 态放弃收割(泄漏换永生); 路由线程 amixer 调用不持 g_mu; QUERY 改报 g_route_cur 缓存(IO 热路径不再 popen) - ALSA 改 O_NONBLOCK + poll(POLLOUT,1s): 连续 3 次停摆判 wedge → PREPARE 自救无效即 关卡进丢弃模式(stall=), 下一路 OPEN 重开重试 - server→client 下行帧单发 write 写不全即整帧丢弃(txdrop=), 杜绝半帧错位/慢客户端堵 IO - 主线程 watchdog: IO/播放心跳 10s 静默告警; io 线程 poll 出错不退出(listener 不丢); backlog 4→8; RESUME/PAUSE 补 cond 唤醒 - SDK meow_ui_audio.c + CLI: connect 非阻塞+poll 2s, SO_SNDTIMEO/RCVTIMEO 2s (audiod 真死时 App 报错重开, 不再永久阻塞) 验证: 宿主 38/38 PASS(旧 19 回归 + 新增 T5 长流 3min 等效 31.7MB 持续灌 played==written underrun/dropped=0 喂流中 listener 探活 15/15; T6 wedge 故障注入生存 — 旧代码同款注入 挂死(90s timeout 强制 kill), 新代码 IO/accept/preempt/SIGTERM 全活; T7 慢客户端 tx 丢弃); qemu-mipsel 静态二进制 status/tone 全通(written=played=13230 underrun=0); -Wall -Wextra 0 warning(host+mipsel+SDK 双端编译)。 meow_audiod md5 86a8b6793d516c90bbd12c2997b30d8f / meow_audio c23b96490842f54324b855fd9c2467a5 (fw_content.conf 已更)。暂不部署真机(主会话统一); meow_ui.a/App 重链留主会话。
485 lines
20 KiB
C
485 lines
20 KiB
C
#define _GNU_SOURCE
|
||
|
||
/* test_audiod_host.c — meow_audiod 宿主集成测试(FILE sink,无 /dev/snd 环境)。
|
||
*
|
||
* 验证 AUDIOD_DESIGN.md 里程碑 A1:
|
||
* - 全 IPC 链路:OPEN→WRITE→QUERY→PAUSE/RESUME→STOP→EOF 回收
|
||
* - 两个客户端抢占:旧流收 MSG_AUDIO_PREEMPTED(PCM 丢弃)
|
||
* - tone 生成 WAV 可核验正弦(440Hz,帧数/过零率粗检)
|
||
*
|
||
* 启动被测试 audiod(宿主二进制,env MEOW_AUDIO_SINK=file / MEOW_AUDIO_OUT=<wav> /
|
||
* MEOW_AUDIO_SOCK=<sock> / MEOW_AUDIO_FORCE=1 跳过门控)。
|
||
*
|
||
* 编译(宿主):cc -I../meow_launcherd -Os -Wall -Wextra test_audiod_host.c -o test_audiod_host
|
||
*/
|
||
#include "meow_ipc.h"
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
#include <unistd.h>
|
||
#include <errno.h>
|
||
#include <fcntl.h>
|
||
#include <math.h>
|
||
#include <poll.h>
|
||
#include <signal.h>
|
||
#include <time.h>
|
||
#include <sys/time.h>
|
||
#include <sys/socket.h>
|
||
#include <sys/un.h>
|
||
#include <sys/wait.h>
|
||
|
||
#define TSOCK "/tmp/meow_audio_test.sock"
|
||
#define TWAV "/tmp/meow_audio_test.wav"
|
||
|
||
static int g_audiod_pid = -1;
|
||
static int g_pass = 0, g_fail = 0;
|
||
|
||
static void check(int cond, const char *name)
|
||
{
|
||
if (cond) { printf(" PASS %s\n", name); g_pass++; }
|
||
else { printf(" FAIL %s\n", name); g_fail++; }
|
||
}
|
||
|
||
/* ---- 原始 IPC 客户端(全有界:connect 非阻塞 2s + SO_SNDTIMEO 2s——回归卡死时
|
||
* 测试 FAIL 而不是跟着挂死) ---- */
|
||
struct cli { int fd; };
|
||
static int cli_connect(struct cli *c)
|
||
{
|
||
c->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||
if (c->fd < 0) return -1;
|
||
int fl = fcntl(c->fd, F_GETFL, 0);
|
||
if (fl >= 0) fcntl(c->fd, F_SETFL, fl | O_NONBLOCK);
|
||
struct sockaddr_un a;
|
||
memset(&a, 0, sizeof a);
|
||
a.sun_family = AF_UNIX;
|
||
snprintf(a.sun_path, sizeof a.sun_path, "%s", TSOCK);
|
||
int r = connect(c->fd, (struct sockaddr *)&a, sizeof a);
|
||
if (r < 0 && errno == EINPROGRESS) {
|
||
struct pollfd p;
|
||
p.fd = c->fd; p.events = POLLOUT; p.revents = 0;
|
||
if (poll(&p, 1, 2000) > 0) {
|
||
int err = 0;
|
||
socklen_t el = sizeof err;
|
||
if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &el) == 0 && err != 0) { errno = err; r = -1; }
|
||
} else {
|
||
r = -1;
|
||
}
|
||
}
|
||
if (r < 0) { close(c->fd); return -1; }
|
||
if (fl >= 0) fcntl(c->fd, F_SETFL, fl);
|
||
struct timeval tv;
|
||
tv.tv_sec = 2; tv.tv_usec = 0;
|
||
setsockopt(c->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
|
||
return 0;
|
||
}
|
||
static int w_all(int fd, const void *p, size_t n)
|
||
{
|
||
const uint8_t *b = p; size_t o = 0;
|
||
while (o < n) { ssize_t r = write(fd, b + o, n - o); if (r < 0) { if (errno == EINTR) continue; return -1; } o += (size_t)r; }
|
||
return 0;
|
||
}
|
||
static int r_all(int fd, void *p, size_t n)
|
||
{
|
||
uint8_t *b = p; size_t o = 0;
|
||
while (o < n) { ssize_t r = read(fd, b + o, n - o); if (r <= 0) return -1; o += (size_t)r; }
|
||
return 0;
|
||
}
|
||
static int sf(int fd, uint16_t type, uint32_t req, const void *pl, uint32_t len)
|
||
{
|
||
uint8_t hdr[MEOW_IPC_HDR_LEN];
|
||
uint32_t magic = MEOW_IPC_MAGIC; uint16_t ver = MEOW_IPC_VERSION;
|
||
memcpy(hdr, &magic, 4); memcpy(hdr + 4, &ver, 2); memcpy(hdr + 6, &type, 2); memcpy(hdr + 8, &req, 4); memcpy(hdr + 12, &len, 4);
|
||
if (w_all(fd, hdr, sizeof hdr)) return -1;
|
||
if (len && pl && w_all(fd, pl, len)) return -1;
|
||
return 0;
|
||
}
|
||
static int rf(int fd, uint16_t *type, uint8_t *rbuf, uint32_t rcap)
|
||
{
|
||
uint8_t hdr[MEOW_IPC_HDR_LEN];
|
||
if (r_all(fd, hdr, sizeof hdr)) return -1;
|
||
uint32_t magic, len; uint16_t ver;
|
||
memcpy(&magic, hdr, 4); memcpy(&ver, hdr + 4, 2); memcpy(type, hdr + 6, 2); memcpy(&len, hdr + 12, 4);
|
||
if (magic != MEOW_IPC_MAGIC || ver != MEOW_IPC_VERSION) return -1;
|
||
if (len > rcap) len = rcap;
|
||
if (len && r_all(fd, rbuf, len)) return -1;
|
||
return (int)len;
|
||
}
|
||
/* 带超时读一帧(poll) */
|
||
static int rf_tmo(int fd, int tmo_ms, uint16_t *type, uint8_t *rbuf, uint32_t rcap)
|
||
{
|
||
struct pollfd p; p.fd = fd; p.events = POLLIN; p.revents = 0;
|
||
if (poll(&p, 1, tmo_ms) <= 0) return -2;
|
||
return rf(fd, type, rbuf, rcap);
|
||
}
|
||
|
||
static void stop_audiod(void)
|
||
{
|
||
if (g_audiod_pid > 0) { kill(g_audiod_pid, SIGTERM); waitpid(g_audiod_pid, NULL, 0); g_audiod_pid = -1; }
|
||
}
|
||
|
||
/* 带超时的停止:SIGTERM 后最多等 tmo_ms;不退 → SIGKILL 并返回 0(卡死回归显形)。 */
|
||
static int stop_audiod_tmo(int tmo_ms)
|
||
{
|
||
if (g_audiod_pid <= 0) return 1;
|
||
kill(g_audiod_pid, SIGTERM);
|
||
int ok = 0;
|
||
for (int i = 0; i < tmo_ms / 50; i++) {
|
||
if (waitpid(g_audiod_pid, NULL, WNOHANG) == g_audiod_pid) { ok = 1; break; }
|
||
usleep(50000);
|
||
}
|
||
if (!ok) { kill(g_audiod_pid, SIGKILL); waitpid(g_audiod_pid, NULL, 0); }
|
||
g_audiod_pid = -1;
|
||
return ok;
|
||
}
|
||
|
||
static int start_audiod_ex(const char *sink, const char *wedge_after, const char *ring_wait_ms)
|
||
{
|
||
pid_t pid = fork();
|
||
if (pid == 0) {
|
||
setenv("MEOW_AUDIO_SINK", sink, 1);
|
||
setenv("MEOW_AUDIO_OUT", TWAV, 1);
|
||
setenv("MEOW_AUDIO_SOCK", TSOCK, 1);
|
||
setenv("MEOW_AUDIO_FORCE", "1", 1);
|
||
if (wedge_after) setenv("MEOW_AUDIO_TEST_WEDGE_AFTER_BYTES", wedge_after, 1);
|
||
if (ring_wait_ms) setenv("MEOW_AUDIO_RING_WAIT_MS", ring_wait_ms, 1);
|
||
execl("./meow_audiod_host", "meow_audiod_host", (char *)0);
|
||
_exit(127);
|
||
}
|
||
return pid;
|
||
}
|
||
|
||
static int start_audiod(void)
|
||
{
|
||
return start_audiod_ex("file", NULL, NULL);
|
||
}
|
||
|
||
/* 直接客户端 open */
|
||
static int do_open(struct cli *c, int rate, int ch, int fmt, m_audio_open_ack_t *ack)
|
||
{
|
||
m_audio_open_t o; memset(&o, 0, sizeof o);
|
||
snprintf(o.app_id, sizeof o.app_id, "test");
|
||
o.rate = (uint32_t)rate; o.ch = (uint16_t)ch; o.fmt = (uint16_t)fmt; o.flags = 0;
|
||
if (sf(c->fd, MSG_AUDIO_OPEN, 1, &o, sizeof o)) return -1;
|
||
uint8_t rbuf[256]; uint16_t rt;
|
||
if (rf(c->fd, &rt, rbuf, sizeof rbuf) < 0) return -1;
|
||
if (rt != MSG_AUDIO_OPEN_ACK) return -2;
|
||
memcpy(ack, rbuf, sizeof *ack);
|
||
return 0;
|
||
}
|
||
|
||
static int do_write(struct cli *c, const void *pcm, uint32_t len)
|
||
{
|
||
return sf(c->fd, MSG_AUDIO_WRITE, 0, pcm, len);
|
||
}
|
||
static int do_ctrl(struct cli *c, uint16_t type, uint32_t sid)
|
||
{
|
||
m_audio_stream_t s; s.stream_id = sid;
|
||
if (sf(c->fd, type, 2, &s, sizeof s)) return -1;
|
||
uint8_t rbuf[64]; uint16_t rt;
|
||
if (rf(c->fd, &rt, rbuf, sizeof rbuf) < 0) return -1;
|
||
return (rt == MSG_ACK) ? 0 : -2;
|
||
}
|
||
static int do_query(struct cli *c, char *out, size_t n)
|
||
{
|
||
if (sf(c->fd, MSG_AUDIO_QUERY, 3, NULL, 0)) return -1;
|
||
uint8_t rbuf[MEOW_IPC_MAX]; uint16_t rt;
|
||
int r = rf(c->fd, &rt, rbuf, sizeof rbuf);
|
||
if (r < 0 || rt != MSG_AUDIO_STATE) return -1;
|
||
size_t m = ((size_t)r < n - 1) ? (size_t)r : (n - 1);
|
||
memcpy(out, rbuf, m);
|
||
out[m] = 0;
|
||
return 0;
|
||
}
|
||
|
||
/* 生成正弦 PCM(S16 or S32,stereo 交织),返回字节数 */
|
||
static size_t gen_sine(uint8_t *buf, size_t cap, int fmt, int frames, double freq, int rate)
|
||
{
|
||
int bps = (fmt == AUDIO_FMT_S16) ? 2 : 4;
|
||
size_t fs = 2; /* 双声道 */
|
||
size_t n = (frames > (int)(cap / (fs * bps))) ? (cap / (fs * bps)) : (size_t)frames;
|
||
size_t o = 0;
|
||
for (size_t i = 0; i < n; i++) {
|
||
double ph = 2.0 * M_PI * freq * (double)i / (double)rate;
|
||
int16_t s = (int16_t)(sin(ph) * 0.3 * 32767);
|
||
for (int c = 0; c < 2; c++) {
|
||
int32_t v = (fmt == AUDIO_FMT_S16) ? (int32_t)s : ((int32_t)s << 16);
|
||
if (fmt == AUDIO_FMT_S16) { memcpy(buf + o, &s, 2); o += 2; }
|
||
else { memcpy(buf + o, &v, 4); o += 4; }
|
||
}
|
||
}
|
||
return o;
|
||
}
|
||
|
||
/* ---- WAV 校验:合法 + 近似正弦(逐帧过零/幅度粗检) ---- */
|
||
static int verify_wav(const char *path, int rate_expect, int ch_expect, int bits_expect)
|
||
{
|
||
FILE *f = fopen(path, "rb");
|
||
if (!f) return -1;
|
||
unsigned char h[44];
|
||
if (fread(h, 1, 44, f) != 44) { fclose(f); return -1; }
|
||
if (memcmp(h, "RIFF", 4) || memcmp(h + 8, "WAVE", 4) || memcmp(h + 12, "fmt ", 4)) { fclose(f); return -1; }
|
||
uint16_t ch, bits; uint32_t rate, byterate;
|
||
memcpy(&ch, h + 22, 2); memcpy(&rate, h + 24, 4); memcpy(&byterate, h + 28, 4); memcpy(&bits, h + 34, 2);
|
||
if (ch != ch_expect || rate != (uint32_t)rate_expect || bits != bits_expect) { fclose(f); return -1; }
|
||
if (byterate != rate * ch * bits / 8) { fclose(f); return -1; }
|
||
uint32_t data_len; memcpy(&data_len, h + 40, 4);
|
||
if (data_len < 44) { fclose(f); return -1; }
|
||
/* 读「左声道」样本(跳右声道)统计过零比例:正弦 freq@rate 单声道期望过零比例 ≈ 2*freq/rate。
|
||
* 仅对「近似正弦」做粗检(40% 容差),确认不是静音/噪声/满幅方波。 */
|
||
int bytes_samp = bits / 8;
|
||
int frame_bytes = ch * bytes_samp;
|
||
uint8_t framebuf[8];
|
||
long zero_cross = 0, total = 0, prev_neg = 0;
|
||
for (long i = 0; i < (long)(data_len / frame_bytes); i++) {
|
||
if (fread(framebuf, 1, (size_t)frame_bytes, f) != (size_t)frame_bytes) break;
|
||
int32_t val = 0;
|
||
if (bytes_samp == 2) { int16_t v; memcpy(&v, framebuf, 2); val = v; }
|
||
else { int32_t v; memcpy(&v, framebuf, 4); val = v >> 16; }
|
||
if (val != 0) {
|
||
int neg = val < 0;
|
||
if (total > 0 && neg != prev_neg) zero_cross++;
|
||
prev_neg = neg;
|
||
}
|
||
total++;
|
||
}
|
||
fclose(f);
|
||
if (total < 100) return -3;
|
||
double obs = (double)zero_cross / (double)total;
|
||
double exp = 2.0 * 440.0 / (double)rate_expect;
|
||
if (obs < exp * 0.6 || obs > exp * 1.4) return -2;
|
||
return 0;
|
||
}
|
||
|
||
int main(void)
|
||
{
|
||
unlink(TSOCK); unlink(TWAV);
|
||
g_audiod_pid = start_audiod();
|
||
if (g_audiod_pid < 0) { printf("cannot fork audiod\n"); return 1; }
|
||
usleep(300000);
|
||
printf("=== meow_audiod host test (FILE sink) ===\n");
|
||
|
||
/* ---- T1:全链路 OPEN→WRITE→QUERY→PAUSE/RESUME→STOP ---- */
|
||
printf("[T1 full chain]\n");
|
||
struct cli a;
|
||
check(cli_connect(&a) == 0, "connect A");
|
||
m_audio_open_ack_t ack;
|
||
check(do_open(&a, 44100, 2, AUDIO_FMT_S16, &ack) == 0 && ack.err == MEOW_OK && ack.stream_id > 0, "open A");
|
||
printf(" grant=%u/%u/%s sid=%u\n", (unsigned)ack.grant_rate, (unsigned)ack.grant_ch, (ack.grant_fmt == AUDIO_FMT_S16 ? "S16" : "S32"), (unsigned)ack.stream_id);
|
||
uint8_t pcm[4096];
|
||
size_t wb = gen_sine(pcm, sizeof pcm, (int)(ack.grant_fmt ? ack.grant_fmt : AUDIO_FMT_S16), 100, 440, 44100);
|
||
check(wb > 0 && do_write(&a, pcm, (uint32_t)wb) == 0, "write A");
|
||
usleep(150000);
|
||
char q[1024];
|
||
check(do_query(&a, q, sizeof q) == 0 && strstr(q, "active=1"), "query active");
|
||
printf(" query: %s\n", q);
|
||
check(do_ctrl(&a, MSG_AUDIO_PAUSE, ack.stream_id) == 0, "pause A");
|
||
usleep(50000);
|
||
do_query(&a, q, sizeof q);
|
||
check(strstr(q, "paused=1") != NULL, "query paused");
|
||
check(do_ctrl(&a, MSG_AUDIO_RESUME, ack.stream_id) == 0, "resume A");
|
||
usleep(50000);
|
||
check(do_ctrl(&a, MSG_AUDIO_STOP, ack.stream_id) == 0, "stop A");
|
||
usleep(100000);
|
||
do_query(&a, q, sizeof q);
|
||
check(strstr(q, "active=0") != NULL, "stop -> inactive");
|
||
close(a.fd);
|
||
|
||
/* ---- T2:两个客户端抢占(旧收 PREEMPTED,PCM 丢弃) ---- */
|
||
printf("[T2 preemption]\n");
|
||
struct cli a2, b2;
|
||
check(cli_connect(&a2) == 0, "connect A2");
|
||
m_audio_open_ack_t ackA2;
|
||
check(do_open(&a2, 48000, 2, AUDIO_FMT_S16, &ackA2) == 0 && ackA2.err == MEOW_OK, "open A2");
|
||
size_t wb2 = gen_sine(pcm, sizeof pcm, (int)(ackA2.grant_fmt ? ackA2.grant_fmt : AUDIO_FMT_S16), 100, 440, 48000);
|
||
do_write(&a2, pcm, (uint32_t)wb2);
|
||
check(cli_connect(&b2) == 0, "connect B2");
|
||
m_audio_open_ack_t ackB2;
|
||
check(do_open(&b2, 96000, 2, AUDIO_FMT_S16, &ackB2) == 0 && ackB2.err == MEOW_OK, "open B2 (preempt)");
|
||
/* A2 应收到 PREEMPTED */
|
||
uint8_t rbuf[128]; uint16_t rt;
|
||
int npr = rf_tmo(a2.fd, 1000, &rt, rbuf, sizeof rbuf);
|
||
check(npr >= (int)sizeof(m_audio_preempted_t) && rt == MSG_AUDIO_PREEMPTED, "A2 got PREEMPTED");
|
||
if (npr >= (int)sizeof(m_audio_preempted_t)) {
|
||
m_audio_preempted_t pm; memcpy(&pm, rbuf, sizeof pm);
|
||
printf(" preempted by %s (stream %u)\n", pm.by_app, (unsigned)pm.stream_id);
|
||
}
|
||
/* A2 写被丢弃;B2 写生效 */
|
||
do_write(&a2, pcm, (uint32_t)wb2); /* 应丢弃(A2 已 draining) */
|
||
usleep(100000);
|
||
do_query(&a2, q, sizeof q);
|
||
printf(" after preempt, A2 query: %s\n", q);
|
||
do_write(&b2, pcm, (uint32_t)wb2);
|
||
usleep(100000);
|
||
do_query(&b2, q, sizeof q);
|
||
check(strstr(q, "active=1") && strstr(q, "rate=96000"), "B2 active after preempt");
|
||
do_ctrl(&b2, MSG_AUDIO_STOP, ackB2.stream_id);
|
||
do_ctrl(&a2, MSG_AUDIO_STOP, ackA2.stream_id);
|
||
close(a2.fd); close(b2.fd);
|
||
|
||
/* ---- T3:EOF 回收(客户端直接断链) ---- */
|
||
printf("[T3 EOF recycle]\n");
|
||
struct cli c3;
|
||
check(cli_connect(&c3) == 0, "connect C3");
|
||
m_audio_open_ack_t ackC3;
|
||
check(do_open(&c3, 44100, 2, AUDIO_FMT_S16, &ackC3) == 0, "open C3");
|
||
/* 不 STOP,直接断链(close fd)→ audiod poll 读到 EOF → 回收 */
|
||
do_write(&c3, pcm, (uint32_t)gen_sine(pcm, sizeof pcm, AUDIO_FMT_S16, 100, 440, 44100));
|
||
close(c3.fd);
|
||
usleep(200000);
|
||
struct cli c3q; check(cli_connect(&c3q) == 0, "connect C3q");
|
||
do_query(&c3q, q, sizeof q);
|
||
printf(" after EOF: %s\n", q);
|
||
check(strstr(q, "active=0") != NULL, "EOF recycle -> inactive");
|
||
close(c3q.fd);
|
||
|
||
/* ---- T5:长流回归(3 分钟等效 PCM 持续灌;FILE sink 全速排空 → 加速时钟等效)。
|
||
* 回归目标:真机「长流卡死」——喂流期间 listener/IO 循环必须持续可服务,
|
||
* 帧数零丢失、零 underrun、零丢弃,STOP 后排空落盘完整。 ---- */
|
||
printf("[T5 long stream (3min equivalent)]\n");
|
||
struct cli l5;
|
||
check(cli_connect(&l5) == 0, "connect L5");
|
||
m_audio_open_ack_t ackL5;
|
||
check(do_open(&l5, 44100, 2, AUDIO_FMT_S16, &ackL5) == 0 && ackL5.err == MEOW_OK, "open L5");
|
||
uint32_t fs5 = (uint32_t)ackL5.grant_ch * (ackL5.grant_fmt == AUDIO_FMT_S16 ? 2u : 4u);
|
||
if (fs5 == 0) fs5 = 4;
|
||
uint64_t total_frames = 44100ULL * 180; /* 3 分钟等效流 */
|
||
uint64_t total_bytes = total_frames * fs5;
|
||
uint8_t chunk[4096];
|
||
memset(chunk, 0x5A, sizeof chunk); /* 内容无关:测完整性/不卡死 */
|
||
size_t per = (sizeof chunk / fs5) * fs5;
|
||
uint64_t sent = 0;
|
||
int werr = 0, probe_n = 0, probe_ok = 0;
|
||
while (sent < total_bytes) {
|
||
uint64_t w = total_bytes - sent;
|
||
if (w > per) w = per;
|
||
if (do_write(&l5, chunk, (uint32_t)w) != 0) { werr = 1; break; }
|
||
sent += w;
|
||
if (sent % (512 * per) == 0) { /* 每 2MB 探一次 listener 活性 */
|
||
struct cli s5;
|
||
s5.fd = -1;
|
||
probe_n++;
|
||
if (cli_connect(&s5) == 0) {
|
||
char q5[1024];
|
||
if (do_query(&s5, q5, sizeof q5) == 0 && strstr(q5, "active=1")) probe_ok++;
|
||
close(s5.fd);
|
||
}
|
||
}
|
||
}
|
||
check(!werr && sent == total_bytes, "long feed completed (io never stalled)");
|
||
printf(" fed %llu bytes (%llu frames), mid-feed status probes %d/%d alive\n",
|
||
(unsigned long long)sent, (unsigned long long)(sent / fs5), probe_ok, probe_n);
|
||
check(probe_n >= 10 && probe_ok == probe_n, "listener/io alive during long feed");
|
||
do_ctrl(&l5, MSG_AUDIO_STOP, ackL5.stream_id);
|
||
char expect[64];
|
||
snprintf(expect, sizeof expect, "played=%llu", (unsigned long long)total_frames);
|
||
int drained = 0;
|
||
char q5[1024];
|
||
for (int i = 0; i < 300; i++) { /* 最多 30s 等排空 */
|
||
if (do_query(&l5, q5, sizeof q5) == 0 && strstr(q5, expect)) { drained = 1; break; }
|
||
usleep(100000);
|
||
}
|
||
check(drained, "long stream fully played (played == fed frames)");
|
||
if (drained)
|
||
check(strstr(q5, "underrun=0") != NULL && strstr(q5, "dropped=0") != NULL,
|
||
"no underrun/drop on long stream");
|
||
usleep(300000); /* 等 dev_close 回填 WAV 头 */
|
||
close(l5.fd);
|
||
{
|
||
FILE *f = fopen(TWAV, "rb");
|
||
long sz = -1;
|
||
if (f) { fseek(f, 0, SEEK_END); sz = ftell(f); fclose(f); }
|
||
check(sz == (long)(44 + total_bytes), "long WAV size == 44 + fed bytes");
|
||
}
|
||
|
||
/* ---- T4:tone 写 WAV(由 CLI 走完整 IPC 链路;mipsel/host CLI 共用) ---- */
|
||
printf("[T4 tone -> WAV]\n");
|
||
unlink(TWAV);
|
||
pid_t tone_pid = fork();
|
||
if (tone_pid == 0) {
|
||
setenv("MEOW_AUDIO_SOCK", TSOCK, 1);
|
||
execl("./meow_audio_host", "meow_audio_host", "tone", "440", "500", "44100", (char *)0);
|
||
_exit(127);
|
||
}
|
||
waitpid(tone_pid, NULL, 0);
|
||
usleep(300000);
|
||
check(verify_wav(TWAV, 44100, 2, 16) == 0, "WAV valid sine");
|
||
printf(" wav size: ");
|
||
{ FILE *f = fopen(TWAV, "rb"); if (f) { fseek(f, 0, SEEK_END); printf("%ld bytes\n", ftell(f)); fclose(f); } }
|
||
|
||
stop_audiod();
|
||
|
||
/* ---- T6:设备 wedge 生存(故障注入复现真机长流卡死场景)。
|
||
* MEOW_AUDIO_TEST_WEDGE_AFTER_BYTES=128KB → 播放线程永久堵在 dev_write(睡到 g_stop)。
|
||
* 旧代码:ring 满 → IO 线程 ring_write cond_wait 永不醒 → accept 停摆 → 全链冻结。
|
||
* 护栏后:ring 写有界(RING_WAIT_MS=50ms)超时丢弃,IO/accept/preempt 全部存活。 ---- */
|
||
printf("[T6 device wedge survival]\n");
|
||
unlink(TSOCK);
|
||
g_audiod_pid = start_audiod_ex("null", "131072", "50");
|
||
usleep(400000);
|
||
struct cli w6;
|
||
check(cli_connect(&w6) == 0, "connect W6");
|
||
m_audio_open_ack_t ackW6;
|
||
check(do_open(&w6, 44100, 2, AUDIO_FMT_S16, &ackW6) == 0 && ackW6.err == MEOW_OK, "open W6");
|
||
int werr6 = 0;
|
||
uint64_t sent6 = 0;
|
||
for (int i = 0; i < 100; i++) { /* 400KB:128KB 后 wedge,其余全靠丢弃 */
|
||
if (do_write(&w6, chunk, (uint32_t)per) != 0) { werr6 = 1; break; }
|
||
sent6 += per;
|
||
}
|
||
check(!werr6 && sent6 == 100 * (uint64_t)per, "feed continues across wedge (no deadlock)");
|
||
struct cli s6;
|
||
s6.fd = -1;
|
||
char q6[1024];
|
||
int io_alive = 0;
|
||
if (cli_connect(&s6) == 0) {
|
||
if (do_query(&s6, q6, sizeof q6) == 0 && strstr(q6, "route_mode=")) io_alive = 1;
|
||
close(s6.fd);
|
||
}
|
||
check(io_alive, "io loop alive after wedge (connect+query served)");
|
||
check(io_alive && strstr(q6, "dropped=") != NULL && atoi(strstr(q6, "dropped=") + 8) > 0,
|
||
"drop policy engaged (dropped>0)");
|
||
struct cli p6;
|
||
p6.fd = -1;
|
||
int pre6 = 0;
|
||
if (cli_connect(&p6) == 0) {
|
||
m_audio_open_ack_t ak6;
|
||
if (do_open(&p6, 48000, 2, AUDIO_FMT_S16, &ak6) == 0 && ak6.err == MEOW_OK) pre6 = 1;
|
||
close(p6.fd);
|
||
}
|
||
check(pre6, "preempt OPEN granted while wedged");
|
||
close(w6.fd);
|
||
check(stop_audiod_tmo(5000) != 0, "wedged audiod exits on SIGTERM within 5s");
|
||
|
||
/* ---- T7:慢/不读客户端下行丢弃(server→client 写绝不堵 IO 循环)。
|
||
* Z 狂发 QUERY 从不读响应 → server 下行缓冲灌满 → send_frame 单发即丢(txdrop++)。
|
||
* 护栏断言:server 不卡死、新连接照常服务、txdrop 有计数。 ---- */
|
||
printf("[T7 slow-client tx drop]\n");
|
||
unlink(TSOCK);
|
||
g_audiod_pid = start_audiod_ex("null", NULL, NULL);
|
||
usleep(400000);
|
||
struct cli z7;
|
||
check(cli_connect(&z7) == 0, "connect Z7");
|
||
int serr7 = 0, sent7 = 0;
|
||
for (int i = 0; i < 2000; i++) {
|
||
if (sf(z7.fd, MSG_AUDIO_QUERY, 9, NULL, 0) != 0) { serr7 = 1; break; }
|
||
sent7++;
|
||
}
|
||
check(!serr7 && sent7 == 2000, "query spam sent (client never reads)");
|
||
struct cli f7;
|
||
f7.fd = -1;
|
||
char q7[1024];
|
||
int alive7 = 0;
|
||
if (cli_connect(&f7) == 0) {
|
||
if (do_query(&f7, q7, sizeof q7) == 0 && strstr(q7, "route_mode=")) alive7 = 1;
|
||
close(f7.fd);
|
||
}
|
||
check(alive7, "server responsive after tx drops");
|
||
check(alive7 && strstr(q7, "txdrop=") != NULL && atoi(strstr(q7, "txdrop=") + 7) > 0,
|
||
"txdrop counted");
|
||
close(z7.fd);
|
||
stop_audiod();
|
||
|
||
printf("=== SUMMARY: pass=%d fail=%d ===\n", g_pass, g_fail);
|
||
return g_fail ? 1 : 0;
|
||
}
|