/* * test_vad_ptt_devices — closes M3's "explicitly out of scope" gaps (PROGRESS.md): device * enumeration, the VAD/PTT send-side input gate, and true stereo playback mixing. * * Mirrors test_m3_multistream.cpp's approach (real vc_client instances against a real * in-process server, not raw sockets) for the ABI-level pieces, plus a white-box AudioEngine * test for the stereo mixer (no audio hardware needed — see AudioEngine::mix_for_test). * * 1. Device enumeration (vc_list_devices) works pre-connect, for both kinds, and tolerates * an empty list (headless CI build agents may have zero audio devices) — VC_OK is the * only thing asserted, never count > 0. * 2. VAD gate: under VC_INPUT_VOICE_ACTIVATION (the default), silent PCM never reaches the * peer (no talking edge); loud PCM does. * 3. PTT gate: under VC_INPUT_PUSH_TO_TALK, loud PCM is gated closed until * vc_set_push_to_talk(1); then it reaches the peer. * 4. Stereo playback mixer: white-box (AudioEngine directly) — a genuinely stereo decoded * stream survives into the mix without being downmixed to mono. */ #include #ifdef VOICECAT_HAS_NET #include #include #include #include #include #include #include #include #include #include #include "voicecat.h" #include "server.h" #include "db.h" #ifdef VOICECAT_HAS_AUDIO #include "audio/audio_engine.h" #endif #ifdef VOICECAT_HAS_OPUS #include "codec/opus_codec.h" #endif // ── Event tracking (same shape as test_m3_multistream.cpp) ────────────────────── struct TalkEvent { uint32_t user_id; uint32_t stream_id; bool talking; }; struct EventStore { std::mutex mu; std::condition_variable cv; bool auth_ok{false}; uint32_t self_user_id{0}; bool channel_list_received{false}; bool saw_stream_started{false}; std::vector talk_events; bool disconnected{false}; const char* label{nullptr}; // Set right after vc_client_create, before vc_connect — lets on_event auto-confirm the // M4 TOFU gate (VC_EVENT_SERVER_IDENTITY below) for this headless test. vc_client* client{nullptr}; }; static void on_event(void* user, const vc_event* ev) { auto* s = static_cast(user); std::lock_guard lk(s->mu); switch (ev->type) { case VC_EVENT_SERVER_IDENTITY: // No human to ask in a headless test — trust on first connect unconditionally. vc_confirm_server_identity(s->client, 1); break; case VC_EVENT_AUTH_RESULT: s->auth_ok = (ev->result == VC_OK); s->self_user_id = ev->user_id; break; case VC_EVENT_CHANNEL_LIST: s->channel_list_received = true; break; case VC_EVENT_STREAM_STARTED: s->saw_stream_started = true; break; case VC_EVENT_TALK_STATE: s->talk_events.push_back({ev->user_id, ev->stream_id, ev->u32a != 0}); break; case VC_EVENT_DISCONNECTED: s->disconnected = true; break; default: break; } s->cv.notify_all(); } template static bool wait_for(EventStore& s, Pred pred, int timeout_ms) { auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); std::unique_lock lk(s.mu); return s.cv.wait_until(lk, deadline, [&] { return pred(s); }); } static std::vector make_sine_frame(int frame_idx, float freq_hz, int frame_samples = 960) { std::vector pcm(frame_samples); for (int i = 0; i < frame_samples; ++i) { float t = static_cast(frame_idx * frame_samples + i) / 48000.0f; pcm[i] = static_cast(std::sin(2.0f * 3.14159265f * freq_hz * t) * 16000.0f); } return pcm; } static std::vector make_silence_frame(int frame_samples = 960) { return std::vector(frame_samples, 0); } // Did `talking==true` ever fire for (user_id, stream_id) at index >= `from`? static bool saw_talking_true(EventStore& s, uint32_t user_id, uint32_t stream_id, size_t from) { std::lock_guard lk(s.mu); for (size_t i = from; i < s.talk_events.size(); ++i) { auto& e = s.talk_events[i]; if (e.user_id == user_id && e.stream_id == stream_id && e.talking) return true; } return false; } static size_t talk_event_count(EventStore& s) { std::lock_guard lk(s.mu); return s.talk_events.size(); } // ── Test harness ────────────────────────────────────────────────────────────── static int g_failures = 0; #define CHECK(cond) \ do { \ if (!(cond)) { \ std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \ ++g_failures; \ } \ } while (0) // ── 1. Device enumeration (no server needed) ──────────────────────────────────── static void test_device_enumeration() { vc_config cfg{"test-devices", "0.1", VC_LOG_OFF}; vc_callbacks cb{}; vc_client* c = vc_client_create(&cfg, cb); CHECK(c != nullptr); for (vc_device_kind kind : {VC_DEVICE_INPUT, VC_DEVICE_OUTPUT}) { vc_device_list dl{}; vc_result r = vc_list_devices(c, kind, &dl); #ifdef VOICECAT_HAS_AUDIO CHECK(r == VC_OK); // Headless CI build agents may legitimately report zero devices — never assert // count > 0, only that the call itself succeeded and the list is well-formed. for (size_t i = 0; i < dl.count; ++i) { CHECK(dl.items[i].id != nullptr); CHECK(dl.items[i].name != nullptr); } #else CHECK(r == VC_ERR_NOT_IMPLEMENTED); #endif vc_free_device_list(&dl); vc_free_device_list(&dl); // idempotent — must not crash on a second call } vc_client_destroy(c); std::printf("test_device_enumeration: ok\n"); } // ── 4. Stereo playback mixer (white-box, no audio hardware needed) ────────────── #if defined(VOICECAT_HAS_AUDIO) && defined(VOICECAT_HAS_OPUS) static void test_stereo_mix() { voicecat::audio::AudioEngine engine; voicecat::audio::AudioParams p; p.sample_rate = 48000; p.capture_channels = 1; p.playback_channels = 2; p.frame_ms = 20; CHECK(engine.start(p)); // capture_cb intentionally omitted — not exercised here voicecat::codec::OpusParams stereo_params; stereo_params.stereo = true; int frame_samples = voicecat::codec::opus_frame_samples(stereo_params); voicecat::codec::OpusEncoder enc; CHECK(enc.init(stereo_params)); // Loud left channel, silent right channel — a real downmix would average them into a // single audible-but-quieter centered sample; true stereo should keep them distinct. std::vector interleaved(static_cast(frame_samples) * 2); for (int i = 0; i < frame_samples; ++i) { float t = static_cast(i) / 48000.0f; interleaved[i * 2] = static_cast(std::sin(2.0f * 3.14159265f * 440.0f * t) * 20000.0f); interleaved[i * 2 + 1] = 0; } uint8_t opus_buf[1500]; int opus_len = enc.encode(interleaved.data(), frame_samples, opus_buf, sizeof(opus_buf)); CHECK(opus_len > 0); engine.init_recv_stream(/*ssrc=*/1, stereo_params); voicecat::audio::JitterBuffer::Frame f; f.seq = 0; f.timestamp = 0; f.fec_present = false; f.payload.assign(opus_buf, opus_buf + opus_len); engine.push_recv_frame(1, std::move(f)); std::vector out(static_cast(frame_samples) * 2, 0); engine.mix_for_test(out.data(), static_cast(frame_samples)); // If the engine downmixed (old M3 behavior), every L/R pair would be identical (the // average of a loud sample and 0). True stereo should show a clear, consistent L != R // difference across the frame. int64_t total_diff = 0; for (int i = 0; i < frame_samples; ++i) total_diff += std::abs(static_cast(out[i * 2]) - static_cast(out[i * 2 + 1])); CHECK(total_diff > static_cast(frame_samples) * 1000); // well above decode noise engine.remove_stream(1); engine.stop(); std::printf("test_stereo_mix: ok (total_diff=%lld)\n", static_cast(total_diff)); } #endif // VOICECAT_HAS_AUDIO && VOICECAT_HAS_OPUS // ── 2/3. VAD + PTT gate, through the real ABI against a real server ───────────── static void test_vad_and_ptt_gate() { auto tmp = std::filesystem::temp_directory_path() / ("vctest_vadptt_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count())); std::filesystem::create_directories(tmp); std::string data_dir = tmp.string(); std::atomic bound_port{0}; std::mutex ready_mu; std::condition_variable ready_cv; bool ready{false}; voicecat::server::Config cfg; cfg.data_dir = data_dir; cfg.bind_port = 0; cfg.media_port = 0; cfg.server_name = "VoiceCat-VadPttTest"; cfg.allow_guests = true; cfg.on_ready = [&](uint16_t p) { bound_port.store(p); { std::lock_guard lk(ready_mu); ready = true; } ready_cv.notify_all(); }; voicecat::server::Server server(cfg); std::thread server_thread([&] { server.run(); }); { std::unique_lock lk(ready_mu); bool ok = ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; }); if (!ok) { std::printf("FAIL: server did not become ready within 10s\n"); ++g_failures; server.stop(); server_thread.join(); std::filesystem::remove_all(tmp); return; } } uint16_t port = bound_port.load(); std::printf("test_vad_and_ptt_gate: server ready on :%u\n", port); EventStore evA; evA.label = "A"; vc_callbacks cbA{on_event, nullptr, &evA}; vc_config cfgA{"test-A", "0.1", VC_LOG_OFF}; vc_client* clientA = vc_client_create(&cfgA, cbA); CHECK(clientA != nullptr); evA.client = clientA; CHECK(vc_connect(clientA, "127.0.0.1", port) == VC_OK); CHECK(vc_authenticate_guest(clientA, "VP-A") == VC_OK); CHECK(wait_for(evA, [](EventStore& s) { return s.auth_ok; }, 8000)); CHECK(wait_for(evA, [](EventStore& s) { return s.channel_list_received; }, 3000)); EventStore evB; evB.label = "B"; vc_callbacks cbB{on_event, nullptr, &evB}; vc_config cfgB{"test-B", "0.1", VC_LOG_OFF}; vc_client* clientB = vc_client_create(&cfgB, cbB); CHECK(clientB != nullptr); evB.client = clientB; CHECK(vc_connect(clientB, "127.0.0.1", port) == VC_OK); CHECK(vc_authenticate_guest(clientB, "VP-B") == VC_OK); CHECK(wait_for(evB, [](EventStore& s) { return s.auth_ok; }, 8000)); CHECK(wait_for(evB, [](EventStore& s) { return s.channel_list_received; }, 3000)); uint32_t a_uid = 0; { std::lock_guard lk(evA.mu); a_uid = evA.self_user_id; } std::this_thread::sleep_for(std::chrono::milliseconds(500)); vc_stream_desc mic_desc{}; mic_desc.kind = VC_STREAM_MIC; mic_desc.label = "mic"; uint32_t mic_sid = 0; CHECK(vc_stream_start(clientA, &mic_desc, &mic_sid) == VC_OK); CHECK(wait_for(evB, [](EventStore& s) { return s.saw_stream_started; }, 5000)); CHECK(wait_for(evA, [](EventStore& s) { return s.saw_stream_started; }, 5000)); // ── 2a. VAD mode (default), silent PCM: must NOT reach B as a talking edge ── CHECK(vc_set_input_mode(clientA, VC_INPUT_VOICE_ACTIVATION) == VC_OK); for (int i = 0; i < 15; ++i) { auto silence = make_silence_frame(); CHECK(vc_test_inject_capture(clientA, mic_sid, silence.data(), silence.size()) == VC_OK); std::this_thread::sleep_for(std::chrono::milliseconds(20)); } CHECK(!saw_talking_true(evB, a_uid, mic_sid, 0)); // ── 2b. VAD mode, loud PCM: must reach B as a talking edge ────────────────── size_t mark = talk_event_count(evB); for (int i = 0; i < 20; ++i) { auto loud = make_sine_frame(i, 440.0f); CHECK(vc_test_inject_capture(clientA, mic_sid, loud.data(), loud.size()) == VC_OK); std::this_thread::sleep_for(std::chrono::milliseconds(20)); } CHECK(wait_for(evB, [&](EventStore& s) { for (size_t i = mark; i < s.talk_events.size(); ++i) { auto& e = s.talk_events[i]; if (e.user_id == a_uid && e.stream_id == mic_sid && e.talking) return true; } return false; }, 3000)); // ── 3a. PTT mode, key up: loud PCM must NOT reach B as a new talking edge ─── CHECK(vc_set_input_mode(clientA, VC_INPUT_PUSH_TO_TALK) == VC_OK); CHECK(vc_set_push_to_talk(clientA, 0) == VC_OK); // Let any in-flight VAD-driven talking state lapse (hang-time ~300ms) before measuring. std::this_thread::sleep_for(std::chrono::milliseconds(500)); mark = talk_event_count(evB); for (int i = 0; i < 20; ++i) { auto loud = make_sine_frame(i, 440.0f); CHECK(vc_test_inject_capture(clientA, mic_sid, loud.data(), loud.size()) == VC_OK); std::this_thread::sleep_for(std::chrono::milliseconds(20)); } std::this_thread::sleep_for(std::chrono::milliseconds(200)); CHECK(!saw_talking_true(evB, a_uid, mic_sid, mark)); // ── 3b. PTT mode, key down: loud PCM must reach B as a talking edge ───────── CHECK(vc_set_push_to_talk(clientA, 1) == VC_OK); mark = talk_event_count(evB); for (int i = 0; i < 20; ++i) { auto loud = make_sine_frame(i, 440.0f); CHECK(vc_test_inject_capture(clientA, mic_sid, loud.data(), loud.size()) == VC_OK); std::this_thread::sleep_for(std::chrono::milliseconds(20)); } CHECK(wait_for(evB, [&](EventStore& s) { for (size_t i = mark; i < s.talk_events.size(); ++i) { auto& e = s.talk_events[i]; if (e.user_id == a_uid && e.stream_id == mic_sid && e.talking) return true; } return false; }, 3000)); { std::lock_guard lk(evA.mu); CHECK(!evA.disconnected); } { std::lock_guard lk(evB.mu); CHECK(!evB.disconnected); } vc_disconnect(clientA); vc_disconnect(clientB); vc_client_destroy(clientA); vc_client_destroy(clientB); server.stop(); server_thread.join(); std::filesystem::remove_all(tmp); std::printf("test_vad_and_ptt_gate: done\n"); } int main() { test_device_enumeration(); #if defined(VOICECAT_HAS_AUDIO) && defined(VOICECAT_HAS_OPUS) test_stereo_mix(); #endif test_vad_and_ptt_gate(); if (g_failures == 0) { std::printf("vad_ptt_devices: all checks passed\n"); return 0; } std::printf("vad_ptt_devices: %d failure(s)\n", g_failures); return 1; } #else // !VOICECAT_HAS_NET int main() { std::printf("vad_ptt_devices: SKIP (VOICECAT_HAS_NET not defined)\n"); return 0; } #endif // VOICECAT_HAS_NET