1From d1f8653d907d5a6b96dbe7e287a35cbc595a24f2 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?St=C3=A9phane=20Cerveau?= <scerveau@collabora.com>
3Date: Mon, 9 Aug 2021 18:02:57 +0200
4Subject: [PATCH 03/14] playbin: do not drain on first EOS
5
6In a case of audio/video media, the decodebin
7should wait for each branch to be on EOS before
8draining the others.
9
10A new signal "wait-on-eos" has been implemented
11for (uri)decodebin to tell that an EOS has been
12received on a branch.
13
14In the case of playbin2, it will compare
15the nb of active pads with the nb of EOS received
16and decodebin will start to drain all the active pads when the nb
17of EOS is equal to active pads (active combiners).
18---
19 gst/playback/gstdecodebin2.c   | 37 ++++++++++++++++++++++++++++++++++
20 gst/playback/gstplaybin2.c     | 31 ++++++++++++++++++++++++++++
21 gst/playback/gsturidecodebin.c | 32 +++++++++++++++++++++++++++++
22 3 files changed, 100 insertions(+)
23
24diff --git a/gst/playback/gstdecodebin2.c b/gst/playback/gstdecodebin2.c
25index 1849714..b84a30d 100644
26--- a/gst/playback/gstdecodebin2.c
27+++ b/gst/playback/gstdecodebin2.c
28@@ -163,6 +163,9 @@ struct _GstDecodeBin
29   GMutex expose_lock;           /* Protects exposal and removal of groups */
30   GstDecodeChain *decode_chain; /* Top level decode chain */
31   guint nbpads;                 /* unique identifier for source pads */
32+  guint nbpads_eos;             /* number of pads in EOS */
33+  gboolean wait_on_eos;         /* wait EOS on other pads */
34+  GCond eos_cond;               /* condition to block the pad in EOS */
35
36   GMutex factories_lock;
37   guint32 factories_cookie;     /* Cookie from last time when factories was updated */
38@@ -224,6 +227,8 @@ struct _GstDecodeBinClass
39
40   /* fired when the last group is drained */
41   void (*drained) (GstElement * element);
42+  /* emitted when an EOS is received */
43+    gboolean (*wait_on_eos) (GstElement * element, guint eos_received);
44 };
45
46 /* signals */
47@@ -236,6 +241,7 @@ enum
48   SIGNAL_AUTOPLUG_SORT,
49   SIGNAL_AUTOPLUG_QUERY,
50   SIGNAL_DRAINED,
51+  SIGNAL_WAIT_ON_EOS,
52   LAST_SIGNAL
53 };
54
55@@ -870,6 +876,20 @@ gst_decode_bin_class_init (GstDecodeBinClass * klass)
56       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, drained),
57       NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
58
59+  /**
60+   * GstDecodeBin::wait-on-eos
61+   * @bin: The decodebin
62+   * @nb_eos: the number of EOS received
63+   *
64+   * This signal is emitted once decodebin has received an EOS.
65+   *
66+   * Since: 1.20
67+   */
68+  gst_decode_bin_signals[SIGNAL_WAIT_ON_EOS] =
69+      g_signal_new ("wait-on-eos", G_TYPE_FROM_CLASS (klass),
70+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstDecodeBinClass, wait_on_eos),
71+      NULL, NULL, NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
72+
73   g_object_class_install_property (gobject_klass, PROP_CAPS,
74       g_param_spec_boxed ("caps", "Caps", "The caps on which to stop decoding.",
75           GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
76@@ -1111,6 +1131,7 @@ gst_decode_bin_init (GstDecodeBin * decode_bin)
77     gst_object_unref (pad);
78   }
79
80+  g_cond_init (&decode_bin->eos_cond);
81   g_mutex_init (&decode_bin->expose_lock);
82   decode_bin->decode_chain = NULL;
83
84@@ -1177,6 +1198,7 @@ gst_decode_bin_finalize (GObject * object)
85
86   decode_bin = GST_DECODE_BIN (object);
87
88+  g_cond_clear (&decode_bin->eos_cond);
89   g_mutex_clear (&decode_bin->expose_lock);
90   g_mutex_clear (&decode_bin->dyn_lock);
91   g_mutex_clear (&decode_bin->subtitle_lock);
92@@ -4285,6 +4307,17 @@ gst_decode_pad_handle_eos (GstDecodePad * pad)
93   }
94
95   EXPOSE_LOCK (dbin);
96+  dbin->nbpads_eos++;
97+  g_signal_emit (G_OBJECT (dbin),
98+      gst_decode_bin_signals[SIGNAL_WAIT_ON_EOS], 0, dbin->nbpads_eos,
99+      &dbin->wait_on_eos);
100+  g_cond_broadcast (&dbin->eos_cond);
101+  GST_DEBUG_OBJECT (dbin, "dbin->nbpads_eos %u wait_on_eos %u",
102+      dbin->nbpads_eos, dbin->wait_on_eos);
103+
104+  while (dbin->wait_on_eos)
105+    g_cond_wait (&dbin->eos_cond, &dbin->expose_lock);
106+
107   if (dbin->decode_chain) {
108     drain_and_switch_chains (dbin->decode_chain, pad, &last_group, &drained,
109         &switched);
110@@ -5326,6 +5359,10 @@ unblock_pads (GstDecodeBin * dbin)
111     GST_DEBUG_OBJECT (dpad, "unblocked");
112     gst_object_unref (dpad);
113   }
114+  dbin->nbpads = 0;
115+  dbin->nbpads_eos = 0;
116+  dbin->wait_on_eos = FALSE;
117+  g_cond_broadcast (&dbin->eos_cond);
118 }
119
120 static void
121diff --git a/gst/playback/gstplaybin2.c b/gst/playback/gstplaybin2.c
122index 51751ff..4098fb9 100644
123--- a/gst/playback/gstplaybin2.c
124+++ b/gst/playback/gstplaybin2.c
125@@ -337,6 +337,7 @@ struct _GstSourceGroup
126   gulong notify_source_id;
127   gulong source_setup_id;
128   gulong drained_id;
129+  gulong wait_on_eos_id;
130   gulong autoplug_factories_id;
131   gulong autoplug_select_id;
132   gulong autoplug_continue_id;
133@@ -3932,6 +3933,30 @@ drained_cb (GstElement * decodebin, GstSourceGroup * group)
134   group->pending_about_to_finish = TRUE;
135 }
136
137+static gboolean
138+wait_on_eos_cb (GstElement * decodebin, guint eos_received,
139+    GstSourceGroup * group)
140+{
141+  GstPlayBin *playbin = group->playbin;
142+  int i;
143+  guint active_pads = 0;
144+
145+  for (i = 0; i < PLAYBIN_STREAM_LAST; i++) {
146+    GstSourceCombine *combine = &group->combiner[i];
147+    if (combine->has_active_pad)
148+      active_pads++;
149+  }
150+
151+  GST_DEBUG_OBJECT (playbin,
152+      "%d eos received in group with uri %s, active pads %d", eos_received,
153+      group->uri, active_pads);
154+
155+  if (eos_received < active_pads)
156+    return TRUE;
157+
158+  return FALSE;
159+}
160+
161 /* Like gst_element_factory_can_sink_any_caps() but doesn't
162  * allow ANY caps on the sinkpad template */
163 static gboolean
164@@ -5401,6 +5426,10 @@ activate_group (GstPlayBin * playbin, GstSourceGroup * group, GstState target)
165   group->drained_id =
166       g_signal_connect (uridecodebin, "drained", G_CALLBACK (drained_cb),
167       group);
168+  /* is called when the uridecodebin received an EOS */
169+  group->wait_on_eos_id =
170+      g_signal_connect (uridecodebin, "wait-on-eos",
171+      G_CALLBACK (wait_on_eos_cb), group);
172
173   /* will be called when a new media type is found. We return a list of decoders
174    * including sinks for decodebin to try */
175@@ -5574,6 +5603,7 @@ error_cleanup:
176       REMOVE_SIGNAL (group->uridecodebin, group->notify_source_id);
177       REMOVE_SIGNAL (group->uridecodebin, group->source_setup_id);
178       REMOVE_SIGNAL (group->uridecodebin, group->drained_id);
179+      REMOVE_SIGNAL (group->uridecodebin, group->wait_on_eos_id);
180       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_factories_id);
181       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_select_id);
182       REMOVE_SIGNAL (group->uridecodebin, group->autoplug_continue_id);
183@@ -5663,6 +5693,7 @@ deactivate_group (GstPlayBin * playbin, GstSourceGroup * group)
184     REMOVE_SIGNAL (group->uridecodebin, group->notify_source_id);
185     REMOVE_SIGNAL (group->uridecodebin, group->source_setup_id);
186     REMOVE_SIGNAL (group->uridecodebin, group->drained_id);
187+    REMOVE_SIGNAL (group->uridecodebin, group->wait_on_eos_id);
188     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_factories_id);
189     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_select_id);
190     REMOVE_SIGNAL (group->uridecodebin, group->autoplug_continue_id);
191diff --git a/gst/playback/gsturidecodebin.c b/gst/playback/gsturidecodebin.c
192index a128d92..7031600 100644
193--- a/gst/playback/gsturidecodebin.c
194+++ b/gst/playback/gsturidecodebin.c
195@@ -148,6 +148,8 @@ struct _GstURIDecodeBinClass
196
197   /* emitted when all data is decoded */
198   void (*drained) (GstElement * element);
199+  /* emitted when an EOS is received */
200+    gboolean (*wait_on_eos) (GstElement * element, guint eos_received);
201 };
202
203 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
204@@ -171,6 +173,7 @@ enum
205   SIGNAL_AUTOPLUG_QUERY,
206   SIGNAL_DRAINED,
207   SIGNAL_SOURCE_SETUP,
208+  SIGNAL_WAIT_ON_EOS,
209   LAST_SIGNAL
210 };
211
212@@ -713,6 +716,19 @@ gst_uri_decode_bin_class_init (GstURIDecodeBinClass * klass)
213       G_SIGNAL_RUN_LAST,
214       G_STRUCT_OFFSET (GstURIDecodeBinClass, drained), NULL, NULL, NULL,
215       G_TYPE_NONE, 0, G_TYPE_NONE);
216+  /**
217+   * GstURIDecodeBin::wait-on-eos
218+   * @bin: The decodebin
219+   * @nb_eos: the number of EOS received
220+   *
221+   * This signal is emitted once decodebin has received an EOS.
222+   *
223+   * Since: 1.20
224+   */
225+  gst_uri_decode_bin_signals[SIGNAL_WAIT_ON_EOS] =
226+      g_signal_new ("wait-on-eos", G_TYPE_FROM_CLASS (klass),
227+      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstURIDecodeBinClass, wait_on_eos),
228+      NULL, NULL, NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
229
230   /**
231    * GstURIDecodeBin::source-setup:
232@@ -1838,6 +1854,20 @@ proxy_drained_signal (GstElement * decodebin, GstURIDecodeBin * dec)
233   g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_DRAINED], 0, NULL);
234 }
235
236+
237+static gboolean
238+proxy_wait_on_eos_signal (GstElement * decodebin, guint eos_received,
239+    GstURIDecodeBin * dec)
240+{
241+  gboolean result;
242+
243+  g_signal_emit (dec, gst_uri_decode_bin_signals[SIGNAL_WAIT_ON_EOS], 0,
244+      eos_received, &result);
245+  GST_DEBUG_OBJECT (dec, "wait-on-eos returned %d", result);
246+
247+  return result;
248+}
249+
250 /* make a decodebin and connect to all the signals */
251 static GstElement *
252 make_decoder (GstURIDecodeBin * decoder)
253@@ -1881,6 +1911,8 @@ make_decoder (GstURIDecodeBin * decoder)
254         G_CALLBACK (proxy_autoplug_query_signal), decoder);
255     g_signal_connect (decodebin, "drained",
256         G_CALLBACK (proxy_drained_signal), decoder);
257+    g_signal_connect (decodebin, "wait-on-eos",
258+        G_CALLBACK (proxy_wait_on_eos_signal), decoder);
259
260     /* set up callbacks to create the links between decoded data
261      * and video/audio/subtitle rendering/output. */
262--
2632.20.1
264
265