3 * Copyright (C) 2015 Centricular Ltd
4 * @author: Edward Hervey <edward@centricular.com>
5 * @author: Jan Schmidt <jan@centricular.com>
7 * gststreams.c: GstStream and GstStreamCollection object and methods
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
30 * @short_description: Base class for stream objects
32 * A #GstStream is a high-level object defining a stream of data which is, or
33 * can be, present in a #GstPipeline.
35 * It is defined by a unique identifier, a "Stream ID". A #GstStream does not
36 * automatically imply the stream is present within a pipeline or element.
38 * Any element that can introduce new streams in a pipeline should create the
39 * appropriate #GstStream object, and can convey that object via the
40 * %GST_EVENT_STREAM_START event and/or the #GstStreamCollection.
42 * Elements that do not modify the nature of the stream can add extra information
43 * on it (such as enrich the #GstCaps, or #GstTagList). This is typically done
44 * by parsing elements.
49 #include "gst_private.h"
51 #include "gstenumtypes.h"
53 #include "gststreams.h"
55 GST_DEBUG_CATEGORY_STATIC (streams_debug);
56 #define GST_CAT_DEFAULT streams_debug
58 struct _GstStreamPrivate
66 /* stream signals and properties */
83 static GParamSpec *gst_stream_pspecs[PROP_LAST] = { 0 };
86 static guint gst_stream_signals[LAST_SIGNAL] = { 0 };
89 static void gst_stream_finalize (GObject * object);
91 static void gst_stream_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_stream_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
98 GST_DEBUG_CATEGORY_INIT (streams_debug, "streams", GST_DEBUG_BOLD, \
99 "debugging info for the stream and stream collection objects"); \
103 #define gst_stream_parent_class parent_class
104 G_DEFINE_TYPE_WITH_CODE (GstStream, gst_stream, GST_TYPE_OBJECT,
105 G_ADD_PRIVATE (GstStream) _do_init);
108 gst_stream_class_init (GstStreamClass * klass)
110 GObjectClass *gobject_class;
112 gobject_class = (GObjectClass *) klass;
114 gobject_class->set_property = gst_stream_set_property;
115 gobject_class->get_property = gst_stream_get_property;
118 * GstStream:stream-id:
120 * The unique identifier of the #GstStream. Can only be set at construction
123 g_object_class_install_property (gobject_class, PROP_STREAM_ID,
124 g_param_spec_string ("stream-id", "Stream ID",
125 "The stream ID of the stream",
127 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
132 * The #GstStreamFlags of the #GstStream. Can only be set at construction time.
134 gst_stream_pspecs[PROP_STREAM_FLAGS] =
135 g_param_spec_flags ("stream-flags", "Stream Flags", "The stream flags",
136 GST_TYPE_STREAM_FLAGS, GST_STREAM_FLAG_NONE,
137 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
138 g_object_class_install_property (gobject_class, PROP_STREAM_FLAGS,
139 gst_stream_pspecs[PROP_STREAM_FLAGS]);
142 * GstStream:stream-type:
144 * The #GstStreamType of the #GstStream. Can only be set at construction time.
146 gst_stream_pspecs[PROP_STREAM_TYPE] =
147 g_param_spec_flags ("stream-type", "Stream Type", "The type of stream",
148 GST_TYPE_STREAM_TYPE, GST_STREAM_TYPE_UNKNOWN,
149 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
150 g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
151 gst_stream_pspecs[PROP_STREAM_TYPE]);
156 * The #GstCaps of the #GstStream.
158 gst_stream_pspecs[PROP_CAPS] =
159 g_param_spec_boxed ("caps", "Caps", "The caps of the stream",
160 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
161 g_object_class_install_property (gobject_class, PROP_CAPS,
162 gst_stream_pspecs[PROP_CAPS]);
167 * The #GstTagList of the #GstStream.
169 gst_stream_pspecs[PROP_TAGS] =
170 g_param_spec_boxed ("tags", "Tags", "The tags of the stream",
171 GST_TYPE_TAG_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
172 g_object_class_install_property (gobject_class, PROP_TAGS,
173 gst_stream_pspecs[PROP_TAGS]);
175 gobject_class->finalize = gst_stream_finalize;
179 gst_stream_init (GstStream * stream)
181 stream->priv = gst_stream_get_instance_private (stream);
182 stream->priv->type = GST_STREAM_TYPE_UNKNOWN;
186 gst_stream_finalize (GObject * object)
188 GstStream *stream = GST_STREAM_CAST (object);
190 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
191 (GstMiniObject *) NULL);
192 gst_caps_replace (&stream->priv->caps, NULL);
193 g_free ((gchar *) stream->stream_id);
195 G_OBJECT_CLASS (parent_class)->finalize (object);
200 * @stream_id: (allow-none): the id for the new stream. If %NULL,
201 * a new one will be automatically generated
202 * @caps: (allow-none) (transfer none): the #GstCaps of the stream
203 * @type: the #GstStreamType of the stream
204 * @flags: the #GstStreamFlags of the stream
206 * Create a new #GstStream for the given @stream_id, @caps, @type
209 * Returns: (transfer full): The new #GstStream
214 gst_stream_new (const gchar * stream_id, GstCaps * caps, GstStreamType type,
215 GstStreamFlags flags)
219 stream = g_object_new (GST_TYPE_STREAM, "stream-id", stream_id, "caps", caps,
220 "stream-type", type, "stream-flags", flags, NULL);
222 /* Clear floating flag */
223 gst_object_ref_sink (stream);
229 gst_stream_set_stream_id (GstStream * stream, const gchar * stream_id)
231 g_return_if_fail (GST_IS_STREAM (stream));
233 GST_OBJECT_LOCK (stream);
234 g_assert (stream->stream_id == NULL);
236 stream->stream_id = g_strdup (stream_id);
238 /* Create a random stream_id if NULL */
240 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
241 g_random_int (), g_random_int ());
244 /* We hold the object lock, replace directly */
245 g_free (GST_OBJECT_NAME (stream));
246 GST_OBJECT_NAME (stream) = g_strdup (stream->stream_id);
248 GST_OBJECT_UNLOCK (stream);
251 GST_FIXME_OBJECT (stream, "Created random stream-id, consider "
252 "implementing a deterministic way of creating a stream-id");
256 * gst_stream_get_stream_id:
257 * @stream: a #GstStream
259 * Returns the stream ID of @stream.
261 * Returns: (transfer none) (nullable): the stream ID of @stream. Only valid
262 * during the lifetime of @stream.
267 gst_stream_get_stream_id (GstStream * stream)
269 g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
271 return stream->stream_id;
275 * gst_stream_set_stream_flags:
276 * @stream: a #GstStream
277 * @flags: the flags to set on @stream
279 * Set the @flags for the @stream.
284 gst_stream_set_stream_flags (GstStream * stream, GstStreamFlags flags)
286 g_return_if_fail (GST_IS_STREAM (stream));
288 GST_OBJECT_LOCK (stream);
289 stream->priv->flags = flags;
290 GST_OBJECT_UNLOCK (stream);
292 g_object_notify_by_pspec (G_OBJECT (stream),
293 gst_stream_pspecs[PROP_STREAM_FLAGS]);
297 * gst_stream_get_stream_flags:
298 * @stream: a #GstStream
300 * Retrieve the current stream flags for @stream
302 * Returns: The #GstStreamFlags for @stream
307 gst_stream_get_stream_flags (GstStream * stream)
311 g_return_val_if_fail (GST_IS_STREAM (stream), GST_STREAM_FLAG_NONE);
313 GST_OBJECT_LOCK (stream);
314 res = stream->priv->flags;
315 GST_OBJECT_UNLOCK (stream);
321 * gst_stream_set_stream_type:
322 * @stream: a #GstStream
323 * @stream_type: the type to set on @stream
325 * Set the stream type of @stream
330 gst_stream_set_stream_type (GstStream * stream, GstStreamType stream_type)
332 g_return_if_fail (GST_IS_STREAM (stream));
334 GST_OBJECT_LOCK (stream);
335 stream->priv->type = stream_type;
336 GST_OBJECT_UNLOCK (stream);
338 g_object_notify_by_pspec (G_OBJECT (stream),
339 gst_stream_pspecs[PROP_STREAM_TYPE]);
343 * gst_stream_get_stream_type:
344 * @stream: a #GstStream
346 * Retrieve the stream type for @stream
348 * Returns: The #GstStreamType for @stream
353 gst_stream_get_stream_type (GstStream * stream)
357 g_return_val_if_fail (GST_IS_STREAM (stream), GST_STREAM_TYPE_UNKNOWN);
359 GST_OBJECT_LOCK (stream);
360 res = stream->priv->type;
361 GST_OBJECT_UNLOCK (stream);
367 * gst_stream_set_tags:
368 * @stream: a #GstStream
369 * @tags: (transfer none) (allow-none): a #GstTagList
371 * Set the tags for the #GstStream
376 gst_stream_set_tags (GstStream * stream, GstTagList * tags)
378 gboolean notify = FALSE;
380 g_return_if_fail (GST_IS_STREAM (stream));
382 GST_OBJECT_LOCK (stream);
383 if (stream->priv->tags == NULL || tags == NULL
384 || !gst_tag_list_is_equal (stream->priv->tags, tags)) {
385 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
386 (GstMiniObject *) tags);
389 GST_OBJECT_UNLOCK (stream);
392 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_TAGS]);
396 * gst_stream_get_tags:
397 * @stream: a #GstStream
399 * Retrieve the tags for @stream, if any
401 * Returns: (transfer full) (nullable): The #GstTagList for @stream
406 gst_stream_get_tags (GstStream * stream)
408 GstTagList *res = NULL;
410 g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
412 GST_OBJECT_LOCK (stream);
413 if (stream->priv->tags)
414 res = gst_tag_list_ref (stream->priv->tags);
415 GST_OBJECT_UNLOCK (stream);
421 * gst_stream_set_caps:
422 * @stream: a #GstStream
423 * @caps: (transfer none) (allow-none): a #GstCaps
425 * Set the caps for the #GstStream
430 gst_stream_set_caps (GstStream * stream, GstCaps * caps)
432 gboolean notify = FALSE;
434 g_return_if_fail (GST_IS_STREAM (stream));
436 GST_OBJECT_LOCK (stream);
437 if (stream->priv->caps == NULL || (caps
438 && !gst_caps_is_equal (stream->priv->caps, caps))) {
439 gst_caps_replace (&stream->priv->caps, caps);
442 GST_OBJECT_UNLOCK (stream);
445 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_CAPS]);
450 * gst_stream_get_caps:
451 * @stream: a #GstStream
453 * Retrieve the caps for @stream, if any
455 * Returns: (transfer full) (nullable): The #GstCaps for @stream
460 gst_stream_get_caps (GstStream * stream)
464 g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
466 GST_OBJECT_LOCK (stream);
467 if (stream->priv->caps)
468 res = gst_caps_ref (stream->priv->caps);
469 GST_OBJECT_UNLOCK (stream);
475 gst_stream_set_property (GObject * object, guint prop_id,
476 const GValue * value, GParamSpec * pspec)
480 stream = GST_STREAM_CAST (object);
484 gst_stream_set_stream_id (stream, g_value_get_string (value));
486 case PROP_STREAM_FLAGS:
487 GST_OBJECT_LOCK (stream);
488 stream->priv->flags = g_value_get_flags (value);
489 GST_OBJECT_UNLOCK (stream);
491 case PROP_STREAM_TYPE:
492 GST_OBJECT_LOCK (stream);
493 stream->priv->type = g_value_get_flags (value);
494 GST_OBJECT_UNLOCK (stream);
497 GST_OBJECT_LOCK (stream);
498 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
499 (GstMiniObject *) g_value_get_boxed (value));
500 GST_OBJECT_UNLOCK (stream);
503 GST_OBJECT_LOCK (stream);
504 gst_mini_object_replace ((GstMiniObject **) & stream->priv->caps,
505 (GstMiniObject *) g_value_get_boxed (value));
506 GST_OBJECT_UNLOCK (stream);
509 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 gst_stream_get_property (GObject * object, guint prop_id,
516 GValue * value, GParamSpec * pspec)
520 stream = GST_STREAM_CAST (object);
524 g_value_set_string (value, gst_stream_get_stream_id (stream));
526 case PROP_STREAM_FLAGS:
527 g_value_set_flags (value, gst_stream_get_stream_flags (stream));
529 case PROP_STREAM_TYPE:
530 g_value_set_flags (value, gst_stream_get_stream_type (stream));
533 g_value_take_boxed (value, gst_stream_get_tags (stream));
536 g_value_take_boxed (value, gst_stream_get_caps (stream));
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
545 * gst_stream_type_get_name:
546 * @stype: a #GstStreamType
548 * Get a descriptive string for a given #GstStreamType
550 * Returns: A string describing the stream type
555 gst_stream_type_get_name (GstStreamType stype)
557 /* FIXME : Make this more flexible */
559 case GST_STREAM_TYPE_UNKNOWN:
561 case GST_STREAM_TYPE_AUDIO:
563 case GST_STREAM_TYPE_VIDEO:
565 case GST_STREAM_TYPE_CONTAINER:
567 case GST_STREAM_TYPE_TEXT:
570 g_return_val_if_reached ("invalid");