3 * Copyright (C) 2015 Centricular Ltd
4 * @author: Edward Hervey <edward@centricular.com>
5 * @author: Jan Schmidt <jan@centricular.com>
7 * gststreams.c: 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.
28 * SECTION:gststreamcollection
29 * @title: GstStreamCollection
30 * @short_description: Base class for collection of streams
35 #include "gst_private.h"
37 #include "gstenumtypes.h"
39 #include "gststreamcollection.h"
41 GST_DEBUG_CATEGORY_STATIC (stream_collection_debug);
42 #define GST_CAT_DEFAULT stream_collection_debug
44 #define GST_STREAM_COLLECTION_GET_PRIVATE(obj) \
45 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_STREAM_COLLECTION, GstStreamCollectionPrivate))
47 struct _GstStreamCollectionPrivate
49 /* Maybe switch this to a GArray if performance is
54 /* stream signals and properties */
68 static guint gst_stream_collection_signals[LAST_SIGNAL] = { 0 };
70 static void gst_stream_collection_dispose (GObject * object);
72 static void gst_stream_collection_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_stream_collection_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
78 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
79 GstStreamCollection * collection);
83 GST_DEBUG_CATEGORY_INIT (stream_collection_debug, "streamcollection", GST_DEBUG_BOLD, \
84 "debugging info for the stream collection objects"); \
88 #define gst_stream_collection_parent_class parent_class
89 G_DEFINE_TYPE_WITH_CODE (GstStreamCollection, gst_stream_collection,
90 GST_TYPE_OBJECT, _do_init);
93 gst_stream_collection_class_init (GstStreamCollectionClass * klass)
95 GObjectClass *gobject_class;
97 gobject_class = (GObjectClass *) klass;
99 g_type_class_add_private (klass, sizeof (GstStreamCollectionPrivate));
101 gobject_class->set_property = gst_stream_collection_set_property;
102 gobject_class->get_property = gst_stream_collection_get_property;
105 * GstStream:upstream-id:
109 g_object_class_install_property (gobject_class, PROP_UPSTREAM_ID,
110 g_param_spec_string ("upstream-id", "Upstream ID",
111 "The stream ID of the parent stream",
113 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
116 * GstStream::stream-notify:
117 * @collection: a #GstStreamCollection
118 * @prop_stream: the #GstStream that originated the signal
119 * @prop: the property that changed
121 * The stream notify signal is used to be notified of property changes to
122 * streams within the collection.
124 gst_stream_collection_signals[SIG_STREAM_NOTIFY] =
125 g_signal_new ("stream-notify", G_TYPE_FROM_CLASS (klass),
126 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
127 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstStreamCollectionClass,
128 stream_notify), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
129 2, GST_TYPE_STREAM, G_TYPE_PARAM);
131 gobject_class->dispose = gst_stream_collection_dispose;
135 gst_stream_collection_init (GstStreamCollection * collection)
137 collection->priv = GST_STREAM_COLLECTION_GET_PRIVATE (collection);
138 collection->priv->streams = g_queue_new ();
142 release_gst_stream (GstStream * stream, GstStreamCollection * collection)
144 g_signal_handlers_disconnect_by_func (stream,
145 proxy_stream_notify_cb, collection);
146 gst_object_unref (stream);
150 gst_stream_collection_dispose (GObject * object)
152 GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
154 if (collection->upstream_id) {
155 g_free (collection->upstream_id);
156 collection->upstream_id = NULL;
159 if (collection->priv->streams) {
160 g_queue_foreach (collection->priv->streams,
161 (GFunc) release_gst_stream, collection);
162 g_queue_free (collection->priv->streams);
163 collection->priv->streams = NULL;
166 G_OBJECT_CLASS (parent_class)->dispose (object);
170 * gst_stream_collection_new:
171 * @upstream_id: (allow-none): The stream id of the parent stream
173 * Create a new #GstStreamCollection.
175 * Returns: The new #GstStreamCollection.
179 GstStreamCollection *
180 gst_stream_collection_new (const gchar * upstream_id)
182 return g_object_new (GST_TYPE_STREAM_COLLECTION, "upstream-id", upstream_id,
187 gst_stream_collection_set_upstream_id (GstStreamCollection * collection,
188 const gchar * upstream_id)
190 g_return_if_fail (collection->upstream_id == NULL);
192 /* Upstream ID should only be set once on construction, but let's
193 * not leak in case someone does something silly */
194 if (collection->upstream_id)
195 g_free (collection->upstream_id);
198 collection->upstream_id = g_strdup (upstream_id);
202 * gst_stream_collection_get_upstream_id:
203 * @collection: a #GstStreamCollection
205 * Returns the upstream id of the @collection.
207 * Returns: (transfer none): The upstream id
212 gst_stream_collection_get_upstream_id (GstStreamCollection * collection)
216 res = collection->upstream_id;
222 gst_stream_collection_set_property (GObject * object, guint prop_id,
223 const GValue * value, GParamSpec * pspec)
225 GstStreamCollection *collection;
227 collection = GST_STREAM_COLLECTION_CAST (object);
230 case PROP_UPSTREAM_ID:
231 gst_stream_collection_set_upstream_id (collection,
232 g_value_get_string (value));
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gst_stream_collection_get_property (GObject * object, guint prop_id,
242 GValue * value, GParamSpec * pspec)
244 GstStreamCollection *collection;
246 collection = GST_STREAM_COLLECTION_CAST (object);
249 case PROP_UPSTREAM_ID:
250 g_value_set_string (value,
251 gst_stream_collection_get_upstream_id (collection));
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
261 GstStreamCollection * collection)
263 GST_DEBUG_OBJECT (collection, "Stream %" GST_PTR_FORMAT " updated %s",
264 stream, pspec->name);
265 g_signal_emit (collection, gst_stream_collection_signals[SIG_STREAM_NOTIFY],
266 g_quark_from_string (pspec->name), stream, pspec);
270 * gst_stream_collection_add_stream:
271 * @collection: a #GstStreamCollection
272 * @stream: (transfer full): the #GstStream to add
274 * Add the given @stream to the @collection.
276 * Returns: %TRUE if the @stream was properly added, else %FALSE
281 gst_stream_collection_add_stream (GstStreamCollection * collection,
284 g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), FALSE);
285 g_return_val_if_fail (GST_IS_STREAM (stream), FALSE);
286 g_return_val_if_fail (collection->priv->streams, FALSE);
288 GST_DEBUG_OBJECT (collection, "Adding stream %" GST_PTR_FORMAT, stream);
290 g_queue_push_tail (collection->priv->streams, stream);
291 g_signal_connect (stream, "notify", (GCallback) proxy_stream_notify_cb,
298 * gst_stream_collection_get_size:
299 * @collection: a #GstStreamCollection
301 * Get the number of streams this collection contains
303 * Returns: The number of streams that @collection contains
308 gst_stream_collection_get_size (GstStreamCollection * collection)
310 g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), 0);
311 g_return_val_if_fail (collection->priv->streams, 0);
313 return g_queue_get_length (collection->priv->streams);
317 * gst_stream_collection_get_stream:
318 * @collection: a #GstStreamCollection
319 * @index: Index of the stream to retrieve
321 * Retrieve the #GstStream with index @index from the collection.
323 * The caller should not modify the returned #GstStream
325 * Returns: (transfer none): A #GstStream
330 gst_stream_collection_get_stream (GstStreamCollection * collection, guint index)
332 g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
333 g_return_val_if_fail (collection->priv->streams, NULL);
335 return g_queue_peek_nth (collection->priv->streams, index);