@@ -18,24 +18,35 @@ VideoPlayer::VideoPlayer(flutter::BinaryMessenger *messenger,
1818 : ecore_wl2_window_proxy_(std::make_unique<EcoreWl2WindowProxy>()),
1919 binary_messenger_ (messenger),
2020 flutter_view_(flutter_view) {
21- sink_event_pipe_ = ecore_pipe_add (
22- [](void *data, void *buffer, unsigned int nbyte) -> void {
23- auto *self = static_cast <VideoPlayer *>(data);
24- self->ExecuteSinkEvents ();
25- },
26- this );
21+ // Initialize GMainContext and event dispatch state
22+ main_context_ = std::unique_ptr<GMainContext, GMainContextDeleter>(
23+ g_main_context_ref_thread_default ());
24+ event_dispatch_state_ = std::make_shared<VideoPlayer::EventDispatchState>();
25+ event_dispatch_state_->player = this ;
2726}
2827
2928VideoPlayer::~VideoPlayer () {
30- if (sink_event_pipe_) {
31- ecore_pipe_del (sink_event_pipe_);
32- sink_event_pipe_ = nullptr ;
29+ // Mark event dispatch state as disposed and cancel pending event source
30+ if (event_dispatch_state_) {
31+ std::lock_guard<std::mutex> lock (event_dispatch_state_->mutex );
32+ event_dispatch_state_->disposed = true ;
33+ event_dispatch_state_->player = nullptr ;
34+
35+ if (event_dispatch_state_->pending_source_id != 0 ) {
36+ g_source_remove (event_dispatch_state_->pending_source_id );
37+ event_dispatch_state_->pending_source_id = 0 ;
38+ }
3339 }
40+
41+ main_context_.reset ();
3442}
3543
3644void VideoPlayer::ClearUpEventChannel () {
3745 is_initialized_ = false ;
38- event_sink_ = nullptr ;
46+ {
47+ std::lock_guard<std::mutex> lock (queue_mutex_);
48+ event_sink_ = nullptr ;
49+ }
3950 if (event_channel_) {
4051 event_channel_->SetStreamHandler (nullptr );
4152 }
@@ -90,14 +101,56 @@ void VideoPlayer::ExecuteSinkEvents() {
90101 }
91102}
92103
93- void VideoPlayer::PushEvent (flutter::EncodableValue encodable_value) {
94- std::lock_guard<std::mutex> lock (queue_mutex_);
95- if (event_sink_ == nullptr ) {
96- LOG_ERROR (" [VideoPlayer] event sink is nullptr." );
104+ void VideoPlayer::ScheduleSendPendingEvents () {
105+ std::lock_guard<std::mutex> lock (event_dispatch_state_->mutex );
106+
107+ // Check conditions and deduplicate
108+ if (!main_context_ || !event_dispatch_state_ ||
109+ event_dispatch_state_->disposed ||
110+ event_dispatch_state_->pending_source_id != 0 ) {
97111 return ;
98112 }
99- encodable_event_queue_.push (encodable_value);
100- ecore_pipe_write (sink_event_pipe_, nullptr , 0 );
113+
114+ auto *state = new std::shared_ptr<EventDispatchState>(event_dispatch_state_);
115+
116+ GSource *source = g_idle_source_new ();
117+ g_source_set_callback (
118+ source,
119+ [](gpointer data) -> gboolean {
120+ auto state = static_cast <std::shared_ptr<EventDispatchState> *>(data);
121+ VideoPlayer *player = nullptr ;
122+ {
123+ std::lock_guard<std::mutex> lock ((*state)->mutex );
124+ if (!(*state)->disposed && (*state)->player ) {
125+ (*state)->pending_source_id = 0 ;
126+ player = (*state)->player ;
127+ }
128+ }
129+ if (player) {
130+ player->ExecuteSinkEvents ();
131+ }
132+ return G_SOURCE_REMOVE ;
133+ },
134+ state,
135+ [](gpointer data) {
136+ delete static_cast <std::shared_ptr<EventDispatchState> *>(data);
137+ });
138+
139+ event_dispatch_state_->pending_source_id =
140+ g_source_attach (source, main_context_.get ());
141+ g_source_unref (source);
142+ }
143+
144+ void VideoPlayer::PushEvent (flutter::EncodableValue encodable_value) {
145+ {
146+ std::lock_guard<std::mutex> lock (queue_mutex_);
147+ if (!event_sink_) {
148+ LOG_ERROR (" [VideoPlayer] event sink is nullptr." );
149+ return ;
150+ }
151+ encodable_event_queue_.push (encodable_value);
152+ }
153+ ScheduleSendPendingEvents ();
101154}
102155
103156void VideoPlayer::SendInitialized () {
@@ -197,11 +250,15 @@ void VideoPlayer::SendRestored() {
197250
198251void VideoPlayer::SendError (const std::string &error_code,
199252 const std::string &error_message) {
200- if (event_sink_) {
253+ {
201254 std::lock_guard<std::mutex> lock (queue_mutex_);
255+ if (!event_sink_) {
256+ LOG_ERROR (" [VideoPlayer] event sink is nullptr." );
257+ return ;
258+ }
202259 error_event_queue_.push (std::make_pair (error_code, error_message));
203- ecore_pipe_write (sink_event_pipe_, nullptr , 0 );
204260 }
261+ ScheduleSendPendingEvents ();
205262}
206263
207264void *VideoPlayer::GetWindowHandle () {
0 commit comments