diff --git a/windows/runner/mpv/mpv_player.cpp b/windows/runner/mpv/mpv_player.cpp index 7c91f4da..f9572b2b 100644 --- a/windows/runner/mpv/mpv_player.cpp +++ b/windows/runner/mpv/mpv_player.cpp @@ -72,6 +72,7 @@ bool MpvPlayer::Initialize(HWND container, HWND flutter_window) { // Observe video-params/sig-peak for HDR detection mpv_observe_property(mpv_, 0, "video-params/sig-peak", MPV_FORMAT_DOUBLE); + mpv_observe_property(mpv_, 0, "current-ao", MPV_FORMAT_STRING); // Start event loop. StartEventLoop(); @@ -295,6 +296,12 @@ void MpvPlayer::SetEventCallback(EventCallback callback) { event_callback_ = std::move(callback); } +void MpvPlayer::ReloadAudioOutput() { + if (audio_reload_pending_) return; + audio_reload_pending_ = true; + CommandAsync({"ao-reload"}, [this](int) { audio_reload_pending_ = false; }); +} + void MpvPlayer::StartEventLoop() { running_ = true; event_thread_ = std::thread(&MpvPlayer::EventLoop, this); @@ -399,18 +406,18 @@ void MpvPlayer::HandleMpvEvent(mpv_event* event) { UpdateHDRMode(sigPeak); } - // Audio recovery: when the device list changes and audio has fallen back - // to null output (e.g. after sleep/wake or device unplug), re-set - // audio-device to switch back to the real output. + if (strcmp(prop->name, "current-ao") == 0) { + const char* current_ao = nullptr; + if (prop->format == MPV_FORMAT_STRING && prop->data) { + current_ao = *static_cast(prop->data); + } + current_ao_is_null_ = current_ao && strcmp(current_ao, "null") == 0; + } + + // Audio recovery for sleep/wake or unplugged devices. // Mirrors mpv's TOOLS/lua/ao-null-reload.lua for embedded libmpv. - if (strcmp(prop->name, "audio-device-list") == 0) { - GetPropertyAsync("current-ao", [this](int ao_error, const std::string& current_ao) { - if (ao_error < 0 || current_ao != "null") return; - GetPropertyAsync("audio-device", [this](int device_error, const std::string& device) { - if (device_error < 0 || device.empty()) return; - SetProperty("audio-device", device); - }); - }); + if (strcmp(prop->name, "audio-device-list") == 0 && current_ao_is_null_) { + ReloadAudioOutput(); } SendPropertyChange(prop->name, &node); diff --git a/windows/runner/mpv/mpv_player.h b/windows/runner/mpv/mpv_player.h index f8d20d94..54d28e6a 100644 --- a/windows/runner/mpv/mpv_player.h +++ b/windows/runner/mpv/mpv_player.h @@ -79,6 +79,7 @@ class MpvPlayer { void HandleMpvEvent(mpv_event* event); void SendPropertyChange(const char* name, mpv_node* data); void SendEvent(const std::string& name, const flutter::EncodableMap& data = {}); + void ReloadAudioOutput(); uint64_t RegisterStatusRequest(StatusCallback callback); StatusCallback TakeStatusRequest(uint64_t request_id); uint64_t RegisterGetPropertyRequest(GetPropertyCallback callback); @@ -95,6 +96,8 @@ class MpvPlayer { std::atomic running_{false}; EventCallback event_callback_; std::mutex callback_mutex_; + bool current_ao_is_null_ = false; + bool audio_reload_pending_ = false; uint64_t next_reply_userdata_ = 1; std::map observed_properties_;