From 29afe5a688f8ce7c56bcc0b6295dee21fdee28cd Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 1 Jan 2021 15:07:25 -0500 Subject: [PATCH 1/2] video: Stop playing when appropriate When the widget gets unrealized or hidden, pause the mediastream. Otherwise, audio might keep playing unexpectedly. --- gtk/gtkvideo.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/gtk/gtkvideo.c b/gtk/gtkvideo.c index d2cb23321a..5f3293ba64 100644 --- a/gtk/gtkvideo.c +++ b/gtk/gtkvideo.c @@ -131,6 +131,9 @@ gtk_video_unrealize (GtkWidget *widget) { GtkVideo *self = GTK_VIDEO (widget); + if (self->autoplay && self->media_stream) + gtk_media_stream_pause (self->media_stream); + if (self->media_stream) { GdkSurface *surface; @@ -165,11 +168,20 @@ gtk_video_unmap (GtkWidget *widget) gtk_revealer_set_reveal_child (GTK_REVEALER (self->controls_revealer), FALSE); } - /* XXX: pause video here? */ - GTK_WIDGET_CLASS (gtk_video_parent_class)->unmap (widget); } +static void +gtk_video_hide (GtkWidget *widget) +{ + GtkVideo *self = GTK_VIDEO (widget); + + if (self->autoplay && self->media_stream) + gtk_media_stream_pause (self->media_stream); + + GTK_WIDGET_CLASS (gtk_video_parent_class)->hide (widget); +} + static void gtk_video_set_focus_child (GtkWidget *widget, GtkWidget *child) @@ -268,6 +280,7 @@ gtk_video_class_init (GtkVideoClass *klass) widget_class->unrealize = gtk_video_unrealize; widget_class->map = gtk_video_map; widget_class->unmap = gtk_video_unmap; + widget_class->hide = gtk_video_hide; widget_class->set_focus_child = gtk_video_set_focus_child; gobject_class->dispose = gtk_video_dispose; From 20f3d1608d15159f171d8c77859c06ef9c1c4e68 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 1 Jan 2021 16:25:54 -0500 Subject: [PATCH 2/2] video: Be consistent about play/pause Always check that the stream is prepared, and the widget is mapped, before calling gtk_media_stream_play(). --- gtk/gtkvideo.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/gtk/gtkvideo.c b/gtk/gtkvideo.c index 5f3293ba64..7eee5fd5ba 100644 --- a/gtk/gtkvideo.c +++ b/gtk/gtkvideo.c @@ -152,7 +152,9 @@ gtk_video_map (GtkWidget *widget) GTK_WIDGET_CLASS (gtk_video_parent_class)->map (widget); - if (self->autoplay && self->media_stream) + if (self->autoplay && + self->media_stream && + gtk_media_stream_is_prepared (self->media_stream)) gtk_media_stream_play (self->media_stream); } @@ -560,7 +562,9 @@ gtk_video_notify_cb (GtkMediaStream *stream, gtk_video_update_playing (self); if (g_str_equal (pspec->name, "prepared")) { - if (self->autoplay && gtk_media_stream_is_prepared (stream)) + if (self->autoplay && + gtk_media_stream_is_prepared (stream) && + gtk_widget_get_mapped (GTK_WIDGET (self))) gtk_media_stream_play (stream); } } @@ -618,7 +622,9 @@ gtk_video_set_media_stream (GtkVideo *self, "notify", G_CALLBACK (gtk_video_notify_cb), self); - if (self->autoplay) + if (self->autoplay && + gtk_media_stream_is_prepared (stream) && + gtk_widget_get_mapped (GTK_WIDGET (self))) gtk_media_stream_play (stream); }