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 #define GST_STREAM_GET_PRIVATE(obj) \
59 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_STREAM, GstStreamPrivate))
61 struct _GstStreamPrivate
69 /* stream signals and properties */
86 static GParamSpec *gst_stream_pspecs[PROP_LAST] = { 0 };
89 static guint gst_stream_signals[LAST_SIGNAL] = { 0 };
92 static void gst_stream_finalize (GObject * object);
94 static void gst_stream_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_stream_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
101 GST_DEBUG_CATEGORY_INIT (streams_debug, "streams", GST_DEBUG_BOLD, \
102 "debugging info for the stream and stream collection objects"); \
106 #define gst_stream_parent_class parent_class
107 G_DEFINE_TYPE_WITH_CODE (GstStream, gst_stream, GST_TYPE_OBJECT, _do_init);
110 gst_stream_class_init (GstStreamClass * klass)
112 GObjectClass *gobject_class;
114 gobject_class = (GObjectClass *) klass;
116 g_type_class_add_private (klass, sizeof (GstStreamPrivate));
118 gobject_class->set_property = gst_stream_set_property;
119 gobject_class->get_property = gst_stream_get_property;
122 * GstStream:stream-id:
124 * The unique identifier of the #GstStream. Can only be set at construction
127 g_object_class_install_property (gobject_class, PROP_STREAM_ID,
128 g_param_spec_string ("stream-id", "Stream ID",
129 "The stream ID of the stream",
131 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
136 * The #GstStreamFlags of the #GstStream. Can only be set at construction time.
138 gst_stream_pspecs[PROP_STREAM_FLAGS] =
139 g_param_spec_flags ("stream-flags", "Stream Flags", "The stream flags",
140 GST_TYPE_STREAM_FLAGS, GST_STREAM_FLAG_NONE,
141 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
142 g_object_class_install_property (gobject_class, PROP_STREAM_FLAGS,
143 gst_stream_pspecs[PROP_STREAM_FLAGS]);
146 * GstStream:stream-type:
148 * The #GstStreamType of the #GstStream. Can only be set at construction time.
150 gst_stream_pspecs[PROP_STREAM_TYPE] =
151 g_param_spec_flags ("stream-type", "Stream Type", "The type of stream",
152 GST_TYPE_STREAM_TYPE, GST_STREAM_TYPE_UNKNOWN,
153 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
154 g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
155 gst_stream_pspecs[PROP_STREAM_TYPE]);
160 * The #GstCaps of the #GstStream.
162 gst_stream_pspecs[PROP_CAPS] =
163 g_param_spec_boxed ("caps", "Caps", "The caps of the stream",
164 GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
165 g_object_class_install_property (gobject_class, PROP_CAPS,
166 gst_stream_pspecs[PROP_CAPS]);
171 * The #GstTagList of the #GstStream.
173 gst_stream_pspecs[PROP_TAGS] =
174 g_param_spec_boxed ("tags", "Tags", "The tags of the stream",
175 GST_TYPE_TAG_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
176 g_object_class_install_property (gobject_class, PROP_TAGS,
177 gst_stream_pspecs[PROP_TAGS]);
179 gobject_class->finalize = gst_stream_finalize;
183 gst_stream_init (GstStream * stream)
185 stream->priv = GST_STREAM_GET_PRIVATE (stream);
186 stream->priv->type = GST_STREAM_TYPE_UNKNOWN;
190 gst_stream_finalize (GObject * object)
192 GstStream *stream = GST_STREAM_CAST (object);
194 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
195 (GstMiniObject *) NULL);
196 gst_caps_replace (&stream->priv->caps, NULL);
197 g_free ((gchar *) stream->stream_id);
199 G_OBJECT_CLASS (parent_class)->finalize (object);
204 * @stream_id: (allow-none): the id for the new stream. If %NULL,
205 * a new one will be automatically generated
206 * @caps: (allow-none) (transfer none): the #GstCaps of the stream
207 * @type: the #GstStreamType of the stream
208 * @flags: the #GstStreamFlags of the stream
210 * Create a new #GstStream for the given @stream_id, @caps, @type
213 * Returns: (transfer full): The new #GstStream
218 gst_stream_new (const gchar * stream_id, GstCaps * caps, GstStreamType type,
219 GstStreamFlags flags)
223 stream = g_object_new (GST_TYPE_STREAM, "stream-id", stream_id, "caps", caps,
224 "stream-type", type, "stream-flags", flags, NULL);
226 /* Clear floating flag */
227 gst_object_ref_sink (stream);
233 gst_stream_set_stream_id (GstStream * stream, const gchar * stream_id)
235 GST_OBJECT_LOCK (stream);
236 g_assert (stream->stream_id == NULL);
238 stream->stream_id = g_strdup (stream_id);
240 /* Create a randoom stream_id if NULL */
241 GST_FIXME_OBJECT (stream, "Creating random stream-id, consider "
242 "implementing a deterministic way of creating a stream-id");
244 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
245 g_random_int (), g_random_int ());
248 GST_OBJECT_UNLOCK (stream);
252 * gst_stream_get_stream_id:
253 * @stream: a #GstStream
255 * Returns the stream ID of @stream.
257 * Returns: (transfer none) (nullable): the stream ID of @stream. Only valid
258 * during the lifetime of @stream.
263 gst_stream_get_stream_id (GstStream * stream)
265 return stream->stream_id;
269 * gst_stream_set_stream_flags:
270 * @stream: a #GstStream
271 * @flags: the flags to set on @stream
273 * Set the @flags for the @stream.
278 gst_stream_set_stream_flags (GstStream * stream, GstStreamFlags flags)
280 GST_OBJECT_LOCK (stream);
281 stream->priv->flags = flags;
282 GST_OBJECT_UNLOCK (stream);
284 g_object_notify_by_pspec (G_OBJECT (stream),
285 gst_stream_pspecs[PROP_STREAM_FLAGS]);
289 * gst_stream_get_stream_flags:
290 * @stream: a #GstStream
292 * Retrieve the current stream flags for @stream
294 * Returns: The #GstStreamFlags for @stream
299 gst_stream_get_stream_flags (GstStream * stream)
303 GST_OBJECT_LOCK (stream);
304 res = stream->priv->flags;
305 GST_OBJECT_UNLOCK (stream);
311 * gst_stream_set_stream_type:
312 * @stream: a #GstStream
313 * @stream_type: the type to set on @stream
315 * Set the stream type of @stream
320 gst_stream_set_stream_type (GstStream * stream, GstStreamType stream_type)
322 GST_OBJECT_LOCK (stream);
323 stream->priv->type = stream_type;
324 GST_OBJECT_UNLOCK (stream);
326 g_object_notify_by_pspec (G_OBJECT (stream),
327 gst_stream_pspecs[PROP_STREAM_TYPE]);
331 * gst_stream_get_stream_type:
332 * @stream: a #GstStream
334 * Retrieve the stream type for @stream
336 * Returns: The #GstStreamType for @stream
341 gst_stream_get_stream_type (GstStream * stream)
345 GST_OBJECT_LOCK (stream);
346 res = stream->priv->type;
347 GST_OBJECT_UNLOCK (stream);
353 * gst_stream_set_tags:
354 * @stream: a #GstStream
355 * @tags: (transfer none) (allow-none): a #GstTagList
357 * Set the tags for the #GstStream
362 gst_stream_set_tags (GstStream * stream, GstTagList * tags)
364 gboolean notify = FALSE;
366 GST_OBJECT_LOCK (stream);
367 if (stream->priv->tags == NULL || tags == NULL
368 || !gst_tag_list_is_equal (stream->priv->tags, tags)) {
369 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
370 (GstMiniObject *) tags);
373 GST_OBJECT_UNLOCK (stream);
376 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_TAGS]);
380 * gst_stream_get_tags:
381 * @stream: a #GstStream
383 * Retrieve the tags for @stream, if any
385 * Returns: (transfer full) (nullable): The #GstTagList for @stream
390 gst_stream_get_tags (GstStream * stream)
392 GstTagList *res = NULL;
394 GST_OBJECT_LOCK (stream);
395 if (stream->priv->tags)
396 res = gst_tag_list_ref (stream->priv->tags);
397 GST_OBJECT_UNLOCK (stream);
403 * gst_stream_set_caps:
404 * @stream: a #GstStream
405 * @caps: (transfer none) (allow-none): a #GstCaps
407 * Set the caps for the #GstStream
412 gst_stream_set_caps (GstStream * stream, GstCaps * caps)
414 gboolean notify = FALSE;
416 GST_OBJECT_LOCK (stream);
417 if (stream->priv->caps == NULL || (caps
418 && !gst_caps_is_equal (stream->priv->caps, caps))) {
419 gst_caps_replace (&stream->priv->caps, caps);
422 GST_OBJECT_UNLOCK (stream);
425 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_CAPS]);
430 * gst_stream_get_caps:
431 * @stream: a #GstStream
433 * Retrieve the caps for @stream, if any
435 * Returns: (transfer full) (nullable): The #GstCaps for @stream
440 gst_stream_get_caps (GstStream * stream)
444 GST_OBJECT_LOCK (stream);
445 if (stream->priv->caps)
446 res = gst_caps_ref (stream->priv->caps);
447 GST_OBJECT_UNLOCK (stream);
453 gst_stream_set_property (GObject * object, guint prop_id,
454 const GValue * value, GParamSpec * pspec)
458 stream = GST_STREAM_CAST (object);
462 gst_stream_set_stream_id (stream, g_value_get_string (value));
464 case PROP_STREAM_FLAGS:
465 GST_OBJECT_LOCK (stream);
466 stream->priv->flags = g_value_get_flags (value);
467 GST_OBJECT_UNLOCK (stream);
469 case PROP_STREAM_TYPE:
470 GST_OBJECT_LOCK (stream);
471 stream->priv->type = g_value_get_flags (value);
472 GST_OBJECT_UNLOCK (stream);
475 GST_OBJECT_LOCK (stream);
476 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
477 (GstMiniObject *) g_value_get_boxed (value));
478 GST_OBJECT_UNLOCK (stream);
481 GST_OBJECT_LOCK (stream);
482 gst_mini_object_replace ((GstMiniObject **) & stream->priv->caps,
483 (GstMiniObject *) g_value_get_boxed (value));
484 GST_OBJECT_UNLOCK (stream);
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
493 gst_stream_get_property (GObject * object, guint prop_id,
494 GValue * value, GParamSpec * pspec)
498 stream = GST_STREAM_CAST (object);
502 g_value_set_string (value, gst_stream_get_stream_id (stream));
504 case PROP_STREAM_FLAGS:
505 g_value_set_flags (value, gst_stream_get_stream_flags (stream));
507 case PROP_STREAM_TYPE:
508 g_value_set_flags (value, gst_stream_get_stream_type (stream));
511 g_value_take_boxed (value, gst_stream_get_tags (stream));
514 g_value_take_boxed (value, gst_stream_get_caps (stream));
517 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
523 * gst_stream_type_get_name:
524 * @stype: a #GstStreamType
526 * Get a descriptive string for a given #GstStreamType
528 * Returns: A string describing the stream type
533 gst_stream_type_get_name (GstStreamType stype)
535 /* FIXME : Make this more flexible */
537 case GST_STREAM_TYPE_UNKNOWN:
539 case GST_STREAM_TYPE_AUDIO:
541 case GST_STREAM_TYPE_VIDEO:
543 case GST_STREAM_TYPE_CONTAINER:
545 case GST_STREAM_TYPE_TEXT: