Files
plezy/windows/runner/mpv/mpv_plugin.h
T
edde746 0e3c592205 fix(windows): marshal mpv events onto the platform thread
close #1453

Events were delivered to the Flutter event sink directly on the mpv event
thread while method replies were already marshaled; concurrent messenger
use from two threads intermittently crashed guests during the event burst
of an in-place media switch. Also joins the event thread before draining
platform tasks in the plugin destructor (reverse member destruction order
would otherwise let the event thread enqueue into a destroyed queue) and
coalesces the wakeup PostMessage behind a retry-safe flag.
2026-07-02 12:43:10 +02:00

62 lines
1.8 KiB
C++

#ifndef MPV_PLUGIN_H_
#define MPV_PLUGIN_H_
#include <flutter/encodable_value.h>
#include <flutter/event_channel.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>
#include "display_mode_manager.h"
#include "mpv_player.h"
// C-style registration function for the plugin.
void MpvPlayerPluginRegisterWithRegistrar(FlutterDesktopPluginRegistrarRef registrar);
namespace mpv {
class MpvPlayerPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows* registrar);
MpvPlayerPlugin(flutter::PluginRegistrarWindows* registrar);
virtual ~MpvPlayerPlugin();
private:
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
void SendEvent(const flutter::EncodableValue& event);
void PostToPlatformThread(std::function<void()> task);
void DrainPlatformTasks();
HWND GetWindow();
HWND GetChildWindow();
flutter::PluginRegistrarWindows* registrar_;
DWORD platform_thread_id_;
HWND flutter_window_ = nullptr;
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> method_channel_;
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>> event_channel_;
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> event_sink_;
std::unique_ptr<MpvPlayer> player_;
DisplayModeManager display_mode_manager_;
std::optional<int32_t> proc_id_;
std::mutex platform_tasks_mutex_;
std::queue<std::function<void()>> platform_tasks_;
bool wakeup_posted_ = false; // guarded by platform_tasks_mutex_
};
} // namespace mpv
#endif // MPV_PLUGIN_H_