1From ef206e6b67526e9fc91f9515a1a1b65a5e755f14 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Fri, 26 Nov 2021 17:41:47 +0800
4Subject: [PATCH 04/14] playbin2: Fix deadlock when hooking about-to-finish
5 signal
6
7The playbin2 will deactivate old group in drain_cb() for that, which
8would cause deadlock when other thread tries to notify pad-change at
9the same time.
10
11Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
12---
13 gst/playback/gstplaybin2.c | 21 +++++++++++++++++++++
14 1 file changed, 21 insertions(+)
15
16diff --git a/gst/playback/gstplaybin2.c b/gst/playback/gstplaybin2.c
17index 4098fb9..cbd8e08 100644
18--- a/gst/playback/gstplaybin2.c
19+++ b/gst/playback/gstplaybin2.c
20@@ -389,6 +389,11 @@ G_STMT_START {                                          \
21 #define GST_PLAY_BIN_SHUTDOWN_UNLOCK(bin)         \
22   GST_PLAY_BIN_DYN_UNLOCK (bin);                  \
23
24+/* lock to protect drain callbacks */
25+#define GST_PLAY_BIN_DRAIN_LOCK(bin)    g_mutex_lock (&(bin)->drain_lock)
26+#define GST_PLAY_BIN_DRAIN_TRYLOCK(bin)    g_mutex_trylock (&(bin)->drain_lock)
27+#define GST_PLAY_BIN_DRAIN_UNLOCK(bin)  g_mutex_unlock (&(bin)->drain_lock)
28+
29 /**
30  * GstPlayBin:
31  *
32@@ -431,6 +436,9 @@ struct _GstPlayBin
33   gint shutdown;
34   gboolean async_pending;       /* async-start has been emitted */
35
36+  /* lock protecting draining */
37+  GMutex drain_lock;
38+
39   GMutex elements_lock;
40   guint32 elements_cookie;
41   GList *elements;              /* factories we can use for selecting elements */
42@@ -1537,6 +1545,7 @@ gst_play_bin_init (GstPlayBin * playbin)
43 {
44   g_rec_mutex_init (&playbin->lock);
45   g_mutex_init (&playbin->dyn_lock);
46+  g_mutex_init (&playbin->drain_lock);
47
48   /* assume we can create an input-selector */
49   playbin->have_selector = TRUE;
50@@ -1633,6 +1642,7 @@ gst_play_bin_finalize (GObject * object)
51   g_list_free_full (playbin->contexts, (GDestroyNotify) gst_context_unref);
52
53   g_rec_mutex_clear (&playbin->lock);
54+  g_mutex_clear (&playbin->drain_lock);
55   g_mutex_clear (&playbin->dyn_lock);
56   g_mutex_clear (&playbin->elements_lock);
57
58@@ -3112,7 +3122,14 @@ combiner_active_pad_changed (GObject * combiner, GParamSpec * pspec,
59   GstSourceCombine *combine = NULL;
60   int i;
61
62+  /* We got a pad-change after draining; no need to notify */
63+  if (!GST_PLAY_BIN_DRAIN_TRYLOCK (playbin))
64+    return;
65+
66   GST_PLAY_BIN_LOCK (playbin);
67+
68+  GST_PLAY_BIN_DRAIN_UNLOCK (playbin);
69+
70   group = get_group (playbin);
71
72   for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
73@@ -3929,7 +3946,11 @@ drained_cb (GstElement * decodebin, GstSourceGroup * group)
74
75   /* now activate the next group. If the app did not set a uri, this will
76    * fail and we can do EOS */
77+
78+  GST_PLAY_BIN_DRAIN_LOCK (playbin);
79   setup_next_source (playbin, GST_STATE_PAUSED);
80+  GST_PLAY_BIN_DRAIN_UNLOCK (playbin);
81+
82   group->pending_about_to_finish = TRUE;
83 }
84
85--
862.20.1
87
88