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 floating): The new #GstStream
218 gst_stream_new (const gchar * stream_id, GstCaps * caps, GstStreamType type,
219 GstStreamFlags flags)
221 return g_object_new (GST_TYPE_STREAM, "stream-id", stream_id, "caps", caps,
222 "stream-type", type, "stream-flags", flags, NULL);
226 gst_stream_set_stream_id (GstStream * stream, const gchar * stream_id)
228 GST_OBJECT_LOCK (stream);
229 g_assert (stream->stream_id == NULL);
231 stream->stream_id = g_strdup (stream_id);
233 /* Create a randoom stream_id if NULL */
234 GST_FIXME_OBJECT (stream, "Creating random stream-id, consider "
235 "implementing a deterministic way of creating a stream-id");
237 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
238 g_random_int (), g_random_int ());
241 GST_OBJECT_UNLOCK (stream);
245 * gst_stream_get_stream_id:
246 * @stream: a #GstStream
248 * Returns the stream ID of @stream.
250 * Returns: (transfer none) (nullable): the stream ID of @stream. Only valid
251 * during the lifetime of @stream.
256 gst_stream_get_stream_id (GstStream * stream)
258 return stream->stream_id;
262 * gst_stream_set_stream_flags:
263 * @stream: a #GstStream
264 * @flags: the flags to set on @stream
266 * Set the @flags for the @stream.
271 gst_stream_set_stream_flags (GstStream * stream, GstStreamFlags flags)
273 GST_OBJECT_LOCK (stream);
274 stream->priv->flags = flags;
275 GST_OBJECT_UNLOCK (stream);
277 g_object_notify_by_pspec (G_OBJECT (stream),
278 gst_stream_pspecs[PROP_STREAM_FLAGS]);
282 * gst_stream_get_stream_flags:
283 * @stream: a #GstStream
285 * Retrieve the current stream flags for @stream
287 * Returns: The #GstStreamFlags for @stream
292 gst_stream_get_stream_flags (GstStream * stream)
296 GST_OBJECT_LOCK (stream);
297 res = stream->priv->flags;
298 GST_OBJECT_UNLOCK (stream);
304 * gst_stream_set_stream_type:
305 * @stream: a #GstStream
306 * @stream_type: the type to set on @stream
308 * Set the stream type of @stream
313 gst_stream_set_stream_type (GstStream * stream, GstStreamType stream_type)
315 GST_OBJECT_LOCK (stream);
316 stream->priv->type = stream_type;
317 GST_OBJECT_UNLOCK (stream);
319 g_object_notify_by_pspec (G_OBJECT (stream),
320 gst_stream_pspecs[PROP_STREAM_TYPE]);
324 * gst_stream_get_stream_type:
325 * @stream: a #GstStream
327 * Retrieve the stream type for @stream
329 * Returns: The #GstStreamType for @stream
334 gst_stream_get_stream_type (GstStream * stream)
338 GST_OBJECT_LOCK (stream);
339 res = stream->priv->type;
340 GST_OBJECT_UNLOCK (stream);
346 * gst_stream_set_tags:
347 * @stream: a #GstStream
348 * @tags: (transfer none) (allow-none): a #GstTagList
350 * Set the tags for the #GstStream
355 gst_stream_set_tags (GstStream * stream, GstTagList * tags)
357 gboolean notify = FALSE;
359 GST_OBJECT_LOCK (stream);
360 if (stream->priv->tags == NULL || tags == NULL
361 || !gst_tag_list_is_equal (stream->priv->tags, tags)) {
362 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
363 (GstMiniObject *) tags);
366 GST_OBJECT_UNLOCK (stream);
369 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_TAGS]);
373 * gst_stream_get_tags:
374 * @stream: a #GstStream
376 * Retrieve the tags for @stream, if any
378 * Returns: (transfer full) (nullable): The #GstTagList for @stream
383 gst_stream_get_tags (GstStream * stream)
385 GstTagList *res = NULL;
387 GST_OBJECT_LOCK (stream);
388 if (stream->priv->tags)
389 res = gst_tag_list_ref (stream->priv->tags);
390 GST_OBJECT_UNLOCK (stream);
396 * gst_stream_set_caps:
397 * @stream: a #GstStream
398 * @caps: (transfer none) (allow-none): a #GstCaps
400 * Set the caps for the #GstStream
405 gst_stream_set_caps (GstStream * stream, GstCaps * caps)
407 gboolean notify = FALSE;
409 GST_OBJECT_LOCK (stream);
410 if (stream->priv->caps == NULL || (caps
411 && !gst_caps_is_equal (stream->priv->caps, caps))) {
412 gst_caps_replace (&stream->priv->caps, caps);
415 GST_OBJECT_UNLOCK (stream);
418 g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_CAPS]);
423 * gst_stream_get_caps:
424 * @stream: a #GstStream
426 * Retrieve the caps for @stream, if any
428 * Returns: (transfer full) (nullable): The #GstCaps for @stream
433 gst_stream_get_caps (GstStream * stream)
437 GST_OBJECT_LOCK (stream);
438 if (stream->priv->caps)
439 res = gst_caps_ref (stream->priv->caps);
440 GST_OBJECT_UNLOCK (stream);
446 gst_stream_set_property (GObject * object, guint prop_id,
447 const GValue * value, GParamSpec * pspec)
451 stream = GST_STREAM_CAST (object);
455 gst_stream_set_stream_id (stream, g_value_get_string (value));
457 case PROP_STREAM_FLAGS:
458 GST_OBJECT_LOCK (stream);
459 stream->priv->flags = g_value_get_flags (value);
460 GST_OBJECT_UNLOCK (stream);
462 case PROP_STREAM_TYPE:
463 GST_OBJECT_LOCK (stream);
464 stream->priv->type = g_value_get_flags (value);
465 GST_OBJECT_UNLOCK (stream);
468 GST_OBJECT_LOCK (stream);
469 gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
470 (GstMiniObject *) g_value_get_boxed (value));
471 GST_OBJECT_UNLOCK (stream);
474 GST_OBJECT_LOCK (stream);
475 gst_mini_object_replace ((GstMiniObject **) & stream->priv->caps,
476 (GstMiniObject *) g_value_get_boxed (value));
477 GST_OBJECT_UNLOCK (stream);
480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486 gst_stream_get_property (GObject * object, guint prop_id,
487 GValue * value, GParamSpec * pspec)
491 stream = GST_STREAM_CAST (object);
495 g_value_set_string (value, gst_stream_get_stream_id (stream));
497 case PROP_STREAM_FLAGS:
498 g_value_set_flags (value, gst_stream_get_stream_flags (stream));
500 case PROP_STREAM_TYPE:
501 g_value_set_flags (value, gst_stream_get_stream_type (stream));
504 g_value_take_boxed (value, gst_stream_get_tags (stream));
507 g_value_take_boxed (value, gst_stream_get_caps (stream));
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516 * gst_stream_type_get_name:
517 * @stype: a #GstStreamType
519 * Get a descriptive string for a given #GstStreamType
521 * Returns: A string describing the stream type
526 gst_stream_type_get_name (GstStreamType stype)
528 /* FIXME : Make this more flexible */
530 case GST_STREAM_TYPE_UNKNOWN:
532 case GST_STREAM_TYPE_AUDIO:
534 case GST_STREAM_TYPE_VIDEO:
536 case GST_STREAM_TYPE_CONTAINER:
538 case GST_STREAM_TYPE_TEXT: