1 /* GStreamer
2 *
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <ts.santos@sisa.samsung.com>
5 *
6 * gstflowcombiner.c: utility to combine multiple flow returns into a single one
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24 /**
25 * SECTION:gstflowcombiner
26 * @title: GstFlowCombiner
27 * @short_description: Utility to combine multiple flow returns into one
28 *
29 * Utility struct to help handling #GstFlowReturn combination. Useful for
30 * #GstElement<!-- -->s that have multiple source pads and need to combine
31 * the different #GstFlowReturn for those pads.
32 *
33 * #GstFlowCombiner works by using the last #GstFlowReturn for all #GstPad
34 * it has in its list and computes the combined return value and provides
35 * it to the caller.
36 *
37 * To add a new pad to the #GstFlowCombiner use gst_flow_combiner_add_pad().
38 * The new #GstPad is stored with a default value of %GST_FLOW_OK.
39 *
40 * In case you want a #GstPad to be removed, use gst_flow_combiner_remove_pad().
41 *
42 * Please be aware that this struct isn't thread safe as its designed to be
43 * used by demuxers, those usually will have a single thread operating it.
44 *
45 * These functions will take refs on the passed #GstPad<!-- -->s.
46 *
47 * Aside from reducing the user's code size, the main advantage of using this
48 * helper struct is to follow the standard rules for #GstFlowReturn combination.
49 * These rules are:
50 *
51 * * %GST_FLOW_EOS: only if all returns are EOS too
52 * * %GST_FLOW_NOT_LINKED: only if all returns are NOT_LINKED too
53 * * %GST_FLOW_ERROR or below: if at least one returns an error return
54 * * %GST_FLOW_NOT_NEGOTIATED: if at least one returns a not-negotiated return
55 * * %GST_FLOW_FLUSHING: if at least one returns flushing
56 * * %GST_FLOW_OK: otherwise
57 *
58 * %GST_FLOW_ERROR or below, GST_FLOW_NOT_NEGOTIATED and GST_FLOW_FLUSHING are
59 * returned immediately from the gst_flow_combiner_update_flow() function.
60 *
61 * Since: 1.4
62 */
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
66
67 #include <gst/gst.h>
68 #include "gstflowcombiner.h"
69
70 struct _GstFlowCombiner
71 {
72 GQueue pads;
73
74 GstFlowReturn last_ret;
75 gint ref_count;
76 };
77
78 GST_DEBUG_CATEGORY_STATIC (flowcombiner_dbg);
79 #define GST_CAT_DEFAULT flowcombiner_dbg
80
81 G_DEFINE_BOXED_TYPE_WITH_CODE (GstFlowCombiner, gst_flow_combiner,
82 (GBoxedCopyFunc) gst_flow_combiner_ref,
83 (GBoxedFreeFunc) gst_flow_combiner_unref,
84 GST_DEBUG_CATEGORY_INIT (flowcombiner_dbg, "flowcombiner", 0,
85 "Flow Combiner"));
86
87 /**
88 * gst_flow_combiner_new:
89 *
90 * Creates a new #GstFlowCombiner, use gst_flow_combiner_free() to free it.
91 *
92 * Returns: A new #GstFlowCombiner
93 * Since: 1.4
94 */
95 GstFlowCombiner *
gst_flow_combiner_new(void)96 gst_flow_combiner_new (void)
97 {
98 GstFlowCombiner *combiner = g_slice_new (GstFlowCombiner);
99
100 g_queue_init (&combiner->pads);
101 combiner->last_ret = GST_FLOW_OK;
102 g_atomic_int_set (&combiner->ref_count, 1);
103
104 /* Make sure debug category is initialised */
105 gst_flow_combiner_get_type ();
106
107 return combiner;
108 }
109
110 /**
111 * gst_flow_combiner_free:
112 * @combiner: the #GstFlowCombiner to free
113 *
114 * Frees a #GstFlowCombiner struct and all its internal data.
115 *
116 * Since: 1.4
117 */
118 void
gst_flow_combiner_free(GstFlowCombiner * combiner)119 gst_flow_combiner_free (GstFlowCombiner * combiner)
120 {
121 gst_flow_combiner_unref (combiner);
122 }
123
124 /**
125 * gst_flow_combiner_ref:
126 * @combiner: the #GstFlowCombiner to add a reference to.
127 *
128 * Increments the reference count on the #GstFlowCombiner.
129 *
130 * Returns: the #GstFlowCombiner.
131 *
132 * Since: 1.12.1
133 */
134 GstFlowCombiner *
gst_flow_combiner_ref(GstFlowCombiner * combiner)135 gst_flow_combiner_ref (GstFlowCombiner * combiner)
136 {
137 g_return_val_if_fail (combiner != NULL, NULL);
138
139 g_atomic_int_inc (&combiner->ref_count);
140
141 return combiner;
142 }
143
144 /**
145 * gst_flow_combiner_unref:
146 * @combiner: the #GstFlowCombiner to unreference.
147 *
148 * Decrements the reference count on the #GstFlowCombiner.
149 *
150 * Since: 1.12.1
151 */
152 void
gst_flow_combiner_unref(GstFlowCombiner * combiner)153 gst_flow_combiner_unref (GstFlowCombiner * combiner)
154 {
155 g_return_if_fail (combiner != NULL);
156 g_return_if_fail (combiner->ref_count > 0);
157
158 if (g_atomic_int_dec_and_test (&combiner->ref_count)) {
159 GstPad *pad;
160
161 while ((pad = g_queue_pop_head (&combiner->pads)))
162 gst_object_unref (pad);
163
164 g_slice_free (GstFlowCombiner, combiner);
165 }
166 }
167
168 /**
169 * gst_flow_combiner_clear:
170 * @combiner: the #GstFlowCombiner to clear
171 *
172 * Removes all pads from a #GstFlowCombiner and resets it to its initial state.
173 *
174 * Since: 1.6
175 */
176 void
gst_flow_combiner_clear(GstFlowCombiner * combiner)177 gst_flow_combiner_clear (GstFlowCombiner * combiner)
178 {
179 GstPad *pad;
180
181 g_return_if_fail (combiner != NULL);
182
183 GST_DEBUG ("%p clearing", combiner);
184
185 while ((pad = g_queue_pop_head (&combiner->pads)))
186 gst_object_unref (pad);
187 combiner->last_ret = GST_FLOW_OK;
188 }
189
190 /**
191 * gst_flow_combiner_reset:
192 * @combiner: the #GstFlowCombiner to clear
193 *
194 * Reset flow combiner and all pads to their initial state without removing pads.
195 *
196 * Since: 1.6
197 */
198 void
gst_flow_combiner_reset(GstFlowCombiner * combiner)199 gst_flow_combiner_reset (GstFlowCombiner * combiner)
200 {
201 GList *iter;
202
203 g_return_if_fail (combiner != NULL);
204
205 GST_DEBUG ("%p reset flow returns", combiner);
206
207 for (iter = combiner->pads.head; iter; iter = iter->next) {
208 GST_PAD_LAST_FLOW_RETURN (iter->data) = GST_FLOW_OK;
209 }
210
211 combiner->last_ret = GST_FLOW_OK;
212 }
213
214 static GstFlowReturn
gst_flow_combiner_get_flow(GstFlowCombiner * combiner)215 gst_flow_combiner_get_flow (GstFlowCombiner * combiner)
216 {
217 GstFlowReturn cret = GST_FLOW_OK;
218 gboolean all_eos = TRUE;
219 gboolean all_notlinked = TRUE;
220 GList *iter;
221
222 GST_DEBUG ("%p Combining flow returns", combiner);
223
224 for (iter = combiner->pads.head; iter; iter = iter->next) {
225 GstFlowReturn fret = GST_PAD_LAST_FLOW_RETURN (iter->data);
226
227 GST_TRACE ("%p pad %" GST_PTR_FORMAT " has flow return of %s (%d)",
228 combiner, iter->data, gst_flow_get_name (fret), fret);
229
230 if (fret <= GST_FLOW_NOT_NEGOTIATED || fret == GST_FLOW_FLUSHING) {
231 GST_DEBUG ("%p Error flow return found, returning", combiner);
232 cret = fret;
233 goto done;
234 }
235
236 if (fret != GST_FLOW_NOT_LINKED) {
237 all_notlinked = FALSE;
238 if (fret != GST_FLOW_EOS)
239 all_eos = FALSE;
240 }
241 }
242 if (all_notlinked)
243 cret = GST_FLOW_NOT_LINKED;
244 else if (all_eos)
245 cret = GST_FLOW_EOS;
246
247 done:
248 GST_DEBUG ("%p Combined flow return: %s (%d)", combiner,
249 gst_flow_get_name (cret), cret);
250 return cret;
251 }
252
253 /**
254 * gst_flow_combiner_update_flow:
255 * @combiner: the #GstFlowCombiner
256 * @fret: the latest #GstFlowReturn received for a pad in this #GstFlowCombiner
257 *
258 * Computes the combined flow return for the pads in it.
259 *
260 * The #GstFlowReturn parameter should be the last flow return update for a pad
261 * in this #GstFlowCombiner. It will use this value to be able to shortcut some
262 * combinations and avoid looking over all pads again. e.g. The last combined
263 * return is the same as the latest obtained #GstFlowReturn.
264 *
265 * Returns: The combined #GstFlowReturn
266 * Since: 1.4
267 */
268 GstFlowReturn
gst_flow_combiner_update_flow(GstFlowCombiner * combiner,GstFlowReturn fret)269 gst_flow_combiner_update_flow (GstFlowCombiner * combiner, GstFlowReturn fret)
270 {
271 GstFlowReturn ret;
272
273 g_return_val_if_fail (combiner != NULL, GST_FLOW_ERROR);
274
275 GST_DEBUG ("%p updating combiner with flow %s (%d)", combiner,
276 gst_flow_get_name (fret), fret);
277
278 if (combiner->last_ret == fret) {
279 return fret;
280 }
281
282 if (fret <= GST_FLOW_NOT_NEGOTIATED || fret == GST_FLOW_FLUSHING
283 || !combiner->pads.head) {
284 ret = fret;
285 } else {
286 ret = gst_flow_combiner_get_flow (combiner);
287 }
288 combiner->last_ret = ret;
289 return ret;
290 }
291
292 /**
293 * gst_flow_combiner_update_pad_flow:
294 * @combiner: the #GstFlowCombiner
295 * @pad: the #GstPad whose #GstFlowReturn to update
296 * @fret: the latest #GstFlowReturn received for a pad in this #GstFlowCombiner
297 *
298 * Sets the provided pad's last flow return to provided value and computes
299 * the combined flow return for the pads in it.
300 *
301 * The #GstFlowReturn parameter should be the last flow return update for a pad
302 * in this #GstFlowCombiner. It will use this value to be able to shortcut some
303 * combinations and avoid looking over all pads again. e.g. The last combined
304 * return is the same as the latest obtained #GstFlowReturn.
305 *
306 * Returns: The combined #GstFlowReturn
307 * Since: 1.6
308 */
309 GstFlowReturn
gst_flow_combiner_update_pad_flow(GstFlowCombiner * combiner,GstPad * pad,GstFlowReturn fret)310 gst_flow_combiner_update_pad_flow (GstFlowCombiner * combiner, GstPad * pad,
311 GstFlowReturn fret)
312 {
313 g_return_val_if_fail (pad != NULL, GST_FLOW_ERROR);
314
315 GST_PAD_LAST_FLOW_RETURN (pad) = fret;
316
317 return gst_flow_combiner_update_flow (combiner, fret);
318 }
319
320 /**
321 * gst_flow_combiner_add_pad:
322 * @combiner: the #GstFlowCombiner
323 * @pad: (transfer none): the #GstPad that is being added
324 *
325 * Adds a new #GstPad to the #GstFlowCombiner.
326 *
327 * Since: 1.4
328 */
329 void
gst_flow_combiner_add_pad(GstFlowCombiner * combiner,GstPad * pad)330 gst_flow_combiner_add_pad (GstFlowCombiner * combiner, GstPad * pad)
331 {
332 g_return_if_fail (combiner != NULL);
333 g_return_if_fail (pad != NULL);
334
335 g_queue_push_head (&combiner->pads, gst_object_ref (pad));
336 }
337
338 /**
339 * gst_flow_combiner_remove_pad:
340 * @combiner: the #GstFlowCombiner
341 * @pad: (transfer none): the #GstPad to remove
342 *
343 * Removes a #GstPad from the #GstFlowCombiner.
344 *
345 * Since: 1.4
346 */
347 void
gst_flow_combiner_remove_pad(GstFlowCombiner * combiner,GstPad * pad)348 gst_flow_combiner_remove_pad (GstFlowCombiner * combiner, GstPad * pad)
349 {
350 g_return_if_fail (combiner != NULL);
351 g_return_if_fail (pad != NULL);
352
353 if (g_queue_remove (&combiner->pads, pad))
354 gst_object_unref (pad);
355 }
356