3 * Copyright (C) 2018 Igalia S.L. All rights reserved.
4 * @author: Thibault Saunier <tsaunier@igalia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-testsrcbin
26 * This is a simple GstBin source that wraps audiotestsrc/videotestsrc following
27 * specification passed in the URI (it implements the #GstURIHandler interface)
28 * in the form of `testbin://audio+video` or setting the "stream-types" property
29 * with the same format.
31 * This element also provides GstStream and GstStreamCollection and thus the
32 * element is useful for testing the new playbin3 infrastructure.
36 * `testbin://<stream1 definition>[+<stream2 definition>]`
38 * With **<stream definition>**:
40 * `<media-type>,<element-properties>,[caps=<media caps>]`
44 * - `<media-type>`: Adds a new source of type `<media-type>`. Supported
46 * * `video`: A #videotestsrc element will be used
47 * * `audio`: An #audiotestsrc will be used
48 * you can use it as many time as wanted to expose new streams.
49 * - `<element-properties>`: `key=value` list of properties to be set on the
50 * source element. See #videotestsrc properties for the video case and
51 * #audiotestsrc properties for the audio case.
52 * - `<media caps>`: Caps to be set in the #capsfilter that follows source elements
53 * for example to force the video source to output a full HD stream, you can use
54 * `video/x-raw,width=1920,height=1080`.
56 * Note that stream definitions are interpreted as serialized #GstStructure.
58 * ## Examples pipeline:
60 * ### One audio stream with volume=0.5 and a white video stream in full HD at 30fps
63 * gst-launch-1.0 playbin3 uri="testbin://audio,volume=0.5+video,pattern=white,caps=[video/x-raw,width=1920,height=1080,framerate=30/1]"
66 * ### Single full HD stream
69 * gst-launch-1.0 playbin3 uri="testbin://video,pattern=green,caps=[video/x-raw,width=1920,height=1080,framerate=30/1]"
72 * ### Two audio streams
75 * gst-launch-1.0 playbin3 uri="testbin://audio+audio"
79 #include <gst/base/gstflowcombiner.h>
80 #include <gst/app/gstappsink.h>
81 #include "gstdebugutilsbadelements.h"
83 static GstStaticPadTemplate video_src_template =
84 GST_STATIC_PAD_TEMPLATE ("video_src_%u",
87 GST_STATIC_CAPS ("video/x-raw(ANY)"));
89 static GstStaticPadTemplate audio_src_template =
90 GST_STATIC_PAD_TEMPLATE ("audio_src_%u",
93 GST_STATIC_CAPS ("audio/x-raw(ANY);"));
95 #define GST_TYPE_TEST_SRC_BIN gst_test_src_bin_get_type()
96 #define GST_TEST_SRC_BIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GST_TYPE_TEST_SRC_BIN, GstTestSrcBin))
98 typedef struct _GstTestSrcBin GstTestSrcBin;
99 typedef struct _GstTestSrcBinClass GstTestSrcBinClass;
102 GType gst_test_src_bin_get_type (void) G_GNUC_CONST;
105 struct _GstTestSrcBinClass
107 GstBinClass parent_class;
110 struct _GstTestSrcBin
116 GstFlowCombiner *flow_combiner;
117 GstCaps *streams_def;
118 GstCaps *next_streams_def;
119 gboolean expose_sources_async;
126 PROP_EXPOSE_SOURCES_ASYNC,
130 #define DEFAULT_TYPES GST_STREAM_TYPE_AUDIO & GST_STREAM_TYPE_VIDEO
133 gst_test_src_bin_uri_handler_get_type (GType type)
138 static const gchar *const *
139 gst_test_src_bin_uri_handler_get_protocols (GType type)
141 static const gchar *protocols[] = { "testbin", NULL };
147 gst_test_src_bin_uri_handler_get_uri (GstURIHandler * handler)
149 GstTestSrcBin *self = GST_TEST_SRC_BIN (handler);
152 GST_OBJECT_LOCK (self);
153 uri = g_strdup (self->uri);
154 GST_OBJECT_UNLOCK (self);
161 GstEvent *stream_start;
162 GstStreamCollection *collection;
166 _probe_data_new (GstEvent * stream_start, GstStreamCollection * collection)
168 ProbeData *data = g_malloc0 (sizeof (ProbeData));
170 data->stream_start = stream_start;
171 data->collection = gst_object_ref (collection);
177 _probe_data_free (ProbeData * data)
179 gst_event_replace (&data->stream_start, NULL);
180 gst_object_replace ((GstObject **) & data->collection, NULL);
185 static GstPadProbeReturn
186 src_pad_probe_cb (GstPad * pad, GstPadProbeInfo * info, ProbeData * data)
188 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
190 switch (GST_EVENT_TYPE (event)) {
191 case GST_EVENT_STREAM_START:{
192 gst_event_unref (event);
193 info->data = gst_event_ref (data->stream_start);
194 return GST_PAD_PROBE_OK;
196 case GST_EVENT_CAPS:{
197 if (data->collection) {
198 GstStreamCollection *collection = data->collection;
199 /* Make sure the collection is NULL so that when caps get unstickied
200 * we let them pass through. */
201 data->collection = NULL;
202 gst_pad_push_event (pad, gst_event_new_stream_collection (collection));
203 gst_object_unref (collection);
205 return GST_PAD_PROBE_REMOVE;
211 return GST_PAD_PROBE_OK;
215 gst_test_src_bin_chain (GstPad * pad, GstObject * object, GstBuffer * buffer)
217 GstFlowReturn res, chain_res;
219 GstTestSrcBin *self = GST_TEST_SRC_BIN (gst_object_get_parent (object));
221 chain_res = gst_proxy_pad_chain_default (pad, GST_OBJECT (self), buffer);
222 GST_OBJECT_LOCK (self);
223 res = gst_flow_combiner_update_pad_flow (self->flow_combiner, pad, chain_res);
224 GST_OBJECT_UNLOCK (self);
225 gst_object_unref (self);
227 if (res == GST_FLOW_FLUSHING)
230 if (res == GST_FLOW_NOT_LINKED)
231 GST_WARNING_OBJECT (pad,
232 "all testsrcbin pads not linked, returning not-linked.");
238 gst_test_src_bin_set_element_property (GQuark property_id, const GValue * value,
241 if (property_id == g_quark_from_static_string ("__streamobj__"))
244 if (property_id == g_quark_from_static_string ("caps"))
247 if (G_VALUE_HOLDS_STRING (value))
248 gst_util_set_object_arg (element, g_quark_to_string (property_id),
249 g_value_get_string (value));
251 g_object_set_property (element, g_quark_to_string (property_id), value);
265 forward_seeks (GstElement * element, GstPad * pad, ForwardEventData * data)
268 gst_pad_event_default (pad, data->parent, gst_event_ref (data->event));
274 gst_test_src_event_function (GstPad * pad, GstObject * parent, GstEvent * event)
276 switch (GST_EVENT_TYPE (event)) {
277 case GST_EVENT_RECONFIGURE:{
278 GstTestSrcBin *self = GST_TEST_SRC_BIN (parent);
279 GST_OBJECT_LOCK (self);
280 gst_flow_combiner_reset (self->flow_combiner);
281 GST_OBJECT_UNLOCK (self);
284 case GST_EVENT_SEEK:{
285 ForwardEventData data = { event, TRUE, parent };
287 gst_element_foreach_src_pad (GST_ELEMENT (parent),
288 (GstElementForeachPadFunc) forward_seeks, &data);
294 return gst_pad_event_default (pad, parent, event);
298 gst_test_src_bin_setup_src (GstTestSrcBin * self, const gchar * srcfactory,
299 GstStaticPadTemplate * template, GstStreamType stype,
300 GstStreamCollection * collection, gint * n_stream, GstStructure * props,
304 GstElement *capsfilter;
305 GstPad *proxypad, *ghost, *pad;
308 GstCaps *caps = NULL;
310 GstEvent *stream_start;
311 GstPadTemplate *templ;
312 const GValue *caps_value = gst_structure_get_value (props, "caps");
315 if (GST_VALUE_HOLDS_CAPS (caps_value)) {
316 caps = gst_caps_copy (gst_value_get_caps (caps_value));
317 } else if (GST_VALUE_HOLDS_STRUCTURE (caps_value)) {
319 gst_caps_new_full (gst_structure_copy (gst_value_get_structure
320 (caps_value)), NULL);
321 } else if (G_VALUE_HOLDS_STRING (caps_value)) {
322 caps = gst_caps_from_string (g_value_get_string (caps_value));
326 g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
327 "Invalid caps string: %s", g_value_get_string (caps_value));
335 g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
336 "Invalid type %s for `caps`", G_VALUE_TYPE_NAME (caps_value));
343 capsfilter = gst_element_factory_make ("capsfilter", NULL);
345 g_object_set (capsfilter, "caps", caps, NULL);
346 gst_caps_unref (caps);
349 src = gst_element_factory_make (srcfactory, NULL);
350 pad = gst_element_get_static_pad (src, "src");
351 stream_id = g_strdup_printf ("%s_stream_%d", srcfactory, *n_stream);
352 stream = gst_stream_new (stream_id, caps, stype,
353 (*n_stream == 0) ? GST_STREAM_FLAG_SELECT : GST_STREAM_FLAG_UNSELECT);
354 stream_start = gst_event_new_stream_start (gst_stream_get_stream_id (stream));
356 gst_structure_foreach (props,
357 (GstStructureForeachFunc) gst_test_src_bin_set_element_property, src);
359 gst_event_set_stream (stream_start, stream);
360 gst_event_set_group_id (stream_start, self->group_id);
362 gst_structure_set (props, "__streamobj__", GST_TYPE_STREAM, stream, NULL);
363 gst_stream_collection_add_stream (collection, stream);
365 gst_pad_add_probe (pad, (GstPadProbeType) GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
366 (GstPadProbeCallback) src_pad_probe_cb, _probe_data_new (stream_start,
367 collection), (GDestroyNotify) _probe_data_free);
371 gst_bin_add_many (GST_BIN (self), src, capsfilter, NULL);
372 if (!gst_element_link (src, capsfilter)) {
373 g_error ("Could not link src with capsfilter?!");
376 gst_object_unref (pad);
378 pad = gst_element_get_static_pad (capsfilter, "src");
379 pad_name = g_strdup_printf (template->name_template, *n_stream);
380 templ = gst_static_pad_template_get (template);
381 ghost = gst_ghost_pad_new_from_template (pad_name, pad, templ);
382 gst_object_unref (templ);
384 gst_object_unref (pad);
386 proxypad = GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (ghost)));
387 gst_flow_combiner_add_pad (self->flow_combiner, ghost);
388 gst_pad_set_chain_function (proxypad,
389 (GstPadChainFunction) gst_test_src_bin_chain);
390 gst_pad_set_event_function (ghost,
391 (GstPadEventFunction) gst_test_src_event_function);
392 gst_object_unref (proxypad);
393 gst_pad_store_sticky_event (ghost, stream_start);
394 gst_element_add_pad (GST_ELEMENT (self), ghost);
395 gst_element_sync_state_with_parent (capsfilter);
396 gst_element_sync_state_with_parent (src);
399 gst_structure_set (props, "__src__", GST_TYPE_OBJECT, src, NULL);
401 gst_clear_caps (&caps);
407 gst_test_src_bin_remove_child (GstElement * self, GstElement * child)
409 GstPad *pad = gst_element_get_static_pad (child, "src");
411 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (gst_pad_get_peer
415 gst_element_set_locked_state (child, FALSE);
416 gst_element_set_state (child, GST_STATE_NULL);
417 gst_bin_remove (GST_BIN (self), child);
418 gst_element_remove_pad (self, ghost);
422 gst_test_check_prev_stream_def (GstTestSrcBin * self, GstCaps * prev_streams,
423 GstStructure * stream_def)
430 for (i = 0; i < gst_caps_get_size (prev_streams); i++) {
431 GstStructure *prev_stream = gst_caps_get_structure (prev_streams, i);
432 GstElement *e = NULL;
433 GstStream *stream = NULL;
435 gst_structure_get (prev_stream, "__src__", GST_TYPE_OBJECT, &e,
436 "__streamobj__", GST_TYPE_STREAM, &stream, NULL);
437 gst_structure_remove_fields (prev_stream, "__src__", "__streamobj__", NULL);
438 if (gst_structure_is_equal (prev_stream, stream_def)) {
441 gst_caps_remove_structure (prev_streams, i);
442 gst_structure_set (stream_def, "__src__", GST_TYPE_OBJECT, e,
443 "__streamobj__", GST_TYPE_STREAM, stream, NULL);
449 gst_structure_set (stream_def, "__src__", GST_TYPE_OBJECT, e,
450 "__streamobj__", GST_TYPE_STREAM, stream, NULL);
457 gst_test_src_bin_create_sources (GstTestSrcBin * self)
459 gint i, n_audio = 0, n_video = 0;
460 GError *error = NULL;
461 GstStreamCollection *collection = gst_stream_collection_new (NULL);
462 GstCaps *streams_def, *prev_streams_def;
464 GST_OBJECT_LOCK (self);
465 streams_def = self->next_streams_def;
466 prev_streams_def = self->streams_def;
467 self->next_streams_def = NULL;
468 self->streams_def = NULL;
469 GST_OBJECT_UNLOCK (self);
471 GST_INFO_OBJECT (self, "Create sources %" GST_PTR_FORMAT,
472 self->next_streams_def);
474 self->group_id = gst_util_group_id_next ();
475 for (i = 0; i < gst_caps_get_size (streams_def); i++) {
477 GstStructure *stream_def = gst_caps_get_structure (streams_def, i);
480 gst_test_check_prev_stream_def (self, prev_streams_def,
482 GST_INFO_OBJECT (self,
483 "Reusing already existing stream: %" GST_PTR_FORMAT, stream_def);
484 gst_stream_collection_add_stream (collection, stream);
485 if (gst_structure_has_name (stream_def, "video"))
492 if (gst_structure_has_name (stream_def, "video")) {
493 if (!gst_test_src_bin_setup_src (self, "videotestsrc",
494 &video_src_template, GST_STREAM_TYPE_VIDEO, collection, &n_video,
495 stream_def, &error)) {
498 } else if (gst_structure_has_name (stream_def, "audio")) {
499 if (!gst_test_src_bin_setup_src (self, "audiotestsrc",
500 &audio_src_template, GST_STREAM_TYPE_AUDIO, collection, &n_audio,
501 stream_def, &error)) {
505 GST_ERROR_OBJECT (self, "Unknown type %s",
506 gst_structure_get_name (stream_def));
510 if (prev_streams_def) {
511 for (i = 0; i < gst_caps_get_size (prev_streams_def); i++) {
512 GstStructure *prev_stream = gst_caps_get_structure (prev_streams_def, i);
515 gst_structure_get (prev_stream, "__src__", GST_TYPE_OBJECT, &child, NULL);
516 gst_test_src_bin_remove_child (GST_ELEMENT (self), child);
518 gst_clear_caps (&prev_streams_def);
521 if (!n_video && !n_audio) {
522 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
523 ("No audio or video stream defined."), (NULL));
527 GST_OBJECT_LOCK (self);
528 self->streams_def = streams_def;
529 GST_OBJECT_UNLOCK (self);
531 gst_element_post_message (GST_ELEMENT (self),
532 gst_message_new_stream_collection (GST_OBJECT (self), collection));
533 gst_object_unref (collection);
535 gst_element_no_more_pads (GST_ELEMENT (self));
540 gst_element_post_message (GST_ELEMENT (self),
541 gst_message_new_error (GST_OBJECT (self), error, NULL)
547 gst_test_src_bin_uri_handler_set_uri (GstURIHandler * handler,
548 const gchar * uri, GError ** error)
550 GstTestSrcBin *self = GST_TEST_SRC_BIN (handler);
551 gchar *tmp, *location = gst_uri_get_location (uri);
552 GstCaps *streams_def;
554 for (tmp = location; *tmp != '\0'; tmp++)
558 streams_def = gst_caps_from_string (location);
564 GST_OBJECT_LOCK (self);
565 gst_clear_caps (&self->next_streams_def);
566 self->next_streams_def = streams_def;
568 self->uri = g_strdup (uri);
570 if (GST_STATE (self) >= GST_STATE_PAUSED) {
572 if (self->expose_sources_async) {
573 GST_OBJECT_UNLOCK (self);
575 gst_element_call_async (GST_ELEMENT (self),
576 (GstElementCallAsyncFunc) gst_test_src_bin_create_sources,
579 GST_OBJECT_UNLOCK (self);
581 gst_test_src_bin_create_sources (self);
584 GST_OBJECT_UNLOCK (self);
595 gst_test_src_bin_uri_handler_init (gpointer g_iface, gpointer unused)
597 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
599 iface->get_type = gst_test_src_bin_uri_handler_get_type;
600 iface->get_protocols = gst_test_src_bin_uri_handler_get_protocols;
601 iface->get_uri = gst_test_src_bin_uri_handler_get_uri;
602 iface->set_uri = gst_test_src_bin_uri_handler_set_uri;
606 G_DEFINE_TYPE_WITH_CODE (GstTestSrcBin, gst_test_src_bin, GST_TYPE_BIN,
607 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_test_src_bin_uri_handler_init))
610 GST_ELEMENT_REGISTER_DEFINE (testsrcbin, "testsrcbin",
611 GST_RANK_NONE, gst_test_src_bin_get_type ());
614 gst_test_src_bin_set_property (GObject * object, guint prop_id,
615 const GValue * value, GParamSpec * pspec)
617 GstTestSrcBin *self = GST_TEST_SRC_BIN (object);
620 case PROP_STREAM_TYPES:
622 gchar *uri = g_strdup_printf ("testbin://%s", g_value_get_string (value));
624 g_assert (gst_uri_handler_set_uri (GST_URI_HANDLER (self), uri, NULL));
628 case PROP_EXPOSE_SOURCES_ASYNC:
630 GST_OBJECT_LOCK (self);
631 self->expose_sources_async = g_value_get_boolean (value);
632 GST_OBJECT_UNLOCK (self);
636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
642 gst_test_src_bin_get_property (GObject * object, guint prop_id, GValue * value,
645 GstTestSrcBin *self = GST_TEST_SRC_BIN (object);
648 case PROP_STREAM_TYPES:
650 gchar *uri = gst_uri_handler_get_uri (GST_URI_HANDLER (self));
652 gchar *types = gst_uri_get_location (uri);
653 g_value_set_string (value, types);
659 case PROP_EXPOSE_SOURCES_ASYNC:
661 GST_OBJECT_LOCK (self);
662 g_value_set_boolean (value, self->expose_sources_async);
663 GST_OBJECT_UNLOCK (self);
667 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
672 static GstStateChangeReturn
673 gst_test_src_bin_change_state (GstElement * element, GstStateChange transition)
675 GstTestSrcBin *self = GST_TEST_SRC_BIN (element);
676 GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
678 switch (transition) {
679 case GST_STATE_CHANGE_READY_TO_PAUSED:{
680 if (self->expose_sources_async) {
681 gst_element_call_async (element,
682 (GstElementCallAsyncFunc) gst_test_src_bin_create_sources,
685 gst_test_src_bin_create_sources (self);
694 GST_ELEMENT_CLASS (gst_test_src_bin_parent_class)->change_state (element,
697 switch (transition) {
698 case GST_STATE_CHANGE_PAUSED_TO_READY:{
699 gst_flow_combiner_reset (self->flow_combiner);
710 gst_test_src_bin_finalize (GObject * object)
712 GstTestSrcBin *self = GST_TEST_SRC_BIN (object);
714 G_OBJECT_CLASS (gst_test_src_bin_parent_class)->finalize (object);
717 gst_clear_caps (&self->streams_def);
718 gst_flow_combiner_free (self->flow_combiner);
723 gst_test_src_bin_init (GstTestSrcBin * self)
725 self->flow_combiner = gst_flow_combiner_new ();
729 gst_test_src_bin_class_init (GstTestSrcBinClass * klass)
731 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
732 GstElementClass *gstelement_klass = (GstElementClass *) klass;
734 gobject_class->finalize = gst_test_src_bin_finalize;
735 gobject_class->get_property = gst_test_src_bin_get_property;
736 gobject_class->set_property = gst_test_src_bin_set_property;
739 * GstTestSrcBin:stream-types:
741 * String describing the stream types to expose, eg. "video+audio".
743 g_object_class_install_property (gobject_class, PROP_STREAM_TYPES,
744 g_param_spec_string ("stream-types", "Stream types",
745 "String describing the stream types to expose, eg. \"video+audio\".",
746 NULL, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
749 * GstTestSrcBin:expose-sources-async:
751 * Whether to expose sources at random time to simulate a source that is
752 * reading a file and exposing the srcpads later.
756 g_object_class_install_property (gobject_class, PROP_EXPOSE_SOURCES_ASYNC,
757 g_param_spec_boolean ("expose-sources-async", "Expose Sources Async",
758 " Whether to expose sources at random time to simulate a source that is"
759 " reading a file and exposing the srcpads later.",
760 FALSE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
762 gstelement_klass->change_state =
763 GST_DEBUG_FUNCPTR (gst_test_src_bin_change_state);
764 gst_element_class_add_pad_template (gstelement_klass,
765 gst_static_pad_template_get (&video_src_template));
766 gst_element_class_add_pad_template (gstelement_klass,
767 gst_static_pad_template_get (&audio_src_template));