#include "audio_player.h" #include // windows.h 之后:waveOut* 系列与 WAVEHDR 都在 winmm。 #include #include #include #include #include #include #include namespace { // 与被控端 AudioPlaybackCapture 的格式保持一致。 constexpr int kSampleRate = 48000; constexpr int kChannels = 2; constexpr int kBitsPerSample = 16; constexpr int kBytesPerSecond = kSampleRate * kChannels * (kBitsPerSample / 8); // 每块 20ms,既够小(延迟低)又能吸收 HTTP 抖动。 constexpr int kBufferBytes = kBytesPerSecond / 50; constexpr int kBufferCount = 8; // 这么久没新数据就关掉设备:投屏停了不该继续占着音频端点。 constexpr DWORD kIdleCloseMs = 3000; constexpr DWORD kPollIntervalMs = 5; std::atomic g_running{false}; std::thread g_thread; // Rust Core 暴露的音频队列读取接口。 class RustAudioApi { public: using NextFn = int32_t (*)(uint8_t*, size_t); using PendingFn = uint32_t (*)(); bool EnsureLoaded() { if (loaded_) { return true; } HMODULE module = GetModuleHandleW(L"ttstdconnect_core.dll"); if (module == nullptr) { wchar_t exe_path[MAX_PATH] = {}; const DWORD length = GetModuleFileNameW(nullptr, exe_path, MAX_PATH); if (length > 0 && length < MAX_PATH) { std::wstring directory(exe_path, length); const size_t slash = directory.find_last_of(L"\\/"); if (slash != std::wstring::npos) { directory.resize(slash + 1); module = LoadLibraryW( (directory + L"ttstdconnect_core.dll").c_str()); } } } if (module == nullptr) { return false; } next_ = reinterpret_cast( GetProcAddress(module, "ttstd_audio_next")); pending_ = reinterpret_cast( GetProcAddress(module, "ttstd_audio_pending")); // 老版本 Core 没有音频导出:静默降级成「没有音频」,不影响视频。 loaded_ = next_ != nullptr && pending_ != nullptr; return loaded_; } uint32_t Pending() const { return (loaded_ && pending_ != nullptr) ? pending_() : 0; } int32_t Next(uint8_t* buffer, size_t capacity) const { if (!loaded_ || next_ == nullptr) { return 0; } return next_(buffer, capacity); } private: bool loaded_ = false; NextFn next_ = nullptr; PendingFn pending_ = nullptr; }; struct Buffer { std::vector data; WAVEHDR header{}; bool queued = false; bool prepared = false; }; void CloseDevice(HWAVEOUT* device, Buffer* buffers) { if (*device == nullptr) { return; } waveOutReset(*device); for (int index = 0; index < kBufferCount; ++index) { if (buffers[index].prepared) { waveOutUnprepareHeader(*device, &buffers[index].header, sizeof(WAVEHDR)); buffers[index].prepared = false; } buffers[index].queued = false; } waveOutClose(*device); *device = nullptr; } bool OpenDevice(HWAVEOUT* device, Buffer* buffers) { WAVEFORMATEX format{}; format.wFormatTag = WAVE_FORMAT_PCM; format.nChannels = static_cast(kChannels); format.nSamplesPerSec = static_cast(kSampleRate); format.wBitsPerSample = static_cast(kBitsPerSample); format.nBlockAlign = static_cast(kChannels * (kBitsPerSample / 8)); format.nAvgBytesPerSec = static_cast(kBytesPerSecond); format.cbSize = 0; HWAVEOUT opened = nullptr; if (waveOutOpen(&opened, WAVE_MAPPER, &format, 0, 0, CALLBACK_NULL) != MMSYSERR_NOERROR) { return false; } for (int index = 0; index < kBufferCount; ++index) { Buffer& buffer = buffers[index]; buffer.data.assign(kBufferBytes, 0); buffer.header = WAVEHDR{}; buffer.header.lpData = reinterpret_cast(buffer.data.data()); buffer.header.dwBufferLength = static_cast(kBufferBytes); if (waveOutPrepareHeader(opened, &buffer.header, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) { CloseDevice(&opened, buffers); return false; } buffer.prepared = true; buffer.queued = false; } *device = opened; return true; } void RunLoop() { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); RustAudioApi api; bool api_checked = false; HWAVEOUT device = nullptr; std::unique_ptr buffers(new Buffer[kBufferCount]); ULONGLONG last_data_at = GetTickCount64(); while (g_running.load()) { if (!api_checked) { if (!api.EnsureLoaded()) { // Core 还没加载好(Dart 可能尚未调用 FFI),稍后再试。 Sleep(500); continue; } api_checked = true; } if (device == nullptr) { if (api.Pending() > 0 && OpenDevice(&device, buffers.get())) { last_data_at = GetTickCount64(); } else { Sleep(kPollIntervalMs); continue; } } bool fed = false; for (int index = 0; index < kBufferCount; ++index) { Buffer& buffer = buffers[index]; if (buffer.queued && (buffer.header.dwFlags & WHDR_DONE) != 0) { buffer.queued = false; } if (buffer.queued) { continue; } const int32_t read = api.Next(buffer.data.data(), buffer.data.size()); if (read <= 0) { continue; } buffer.header.dwBufferLength = static_cast(read); buffer.header.dwFlags = 0; buffer.header.dwLoops = 0; if (waveOutWrite(device, &buffer.header, sizeof(WAVEHDR)) == MMSYSERR_NOERROR) { buffer.queued = true; fed = true; last_data_at = GetTickCount64(); } } if (!fed && api.Pending() == 0 && GetTickCount64() - last_data_at > kIdleCloseMs) { CloseDevice(&device, buffers.get()); } Sleep(kPollIntervalMs); } CloseDevice(&device, buffers.get()); } } // namespace void StartAudioPlayer() { if (g_running.exchange(true)) { return; } g_thread = std::thread(RunLoop); } void StopAudioPlayer() { if (!g_running.exchange(false)) { return; } if (g_thread.joinable()) { g_thread.join(); } }