streams: sprinkle some Since: markers for docs
[platform/upstream/gstreamer.git] / gst / gststreamcollection.c
1 /* GStreamer
2  *
3  * Copyright (C) 2015 Centricular Ltd
4  *  @author: Edward Hervey <edward@centricular.com>
5  *  @author: Jan Schmidt <jan@centricular.com>
6  *
7  * gststreams.c: GstStreamCollection object and methods
8  *
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.
13  *
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.
18  *
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.
23  *
24  * MT safe.
25  */
26
27 /**
28  * SECTION:gststreamcollection
29  * @short_description: Base class for collection of streams
30  *
31  * Since: 1.10
32  */
33
34 #include "gst_private.h"
35
36 #include "gstenumtypes.h"
37 #include "gstevent.h"
38 #include "gststreamcollection.h"
39
40 GST_DEBUG_CATEGORY_STATIC (stream_collection_debug);
41 #define GST_CAT_DEFAULT stream_collection_debug
42
43 #define GST_STREAM_COLLECTION_GET_PRIVATE(obj)  \
44    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_STREAM_COLLECTION, GstStreamCollectionPrivate))
45
46 struct _GstStreamCollectionPrivate
47 {
48   /* Maybe switch this to a GArray if performance is
49    * ever an issue? */
50   GQueue *streams;
51 };
52
53 /* stream signals and properties */
54 enum
55 {
56   SIG_STREAM_NOTIFY,
57   LAST_SIGNAL
58 };
59
60 enum
61 {
62   PROP_0,
63   PROP_UPSTREAM_ID,
64   PROP_LAST
65 };
66
67 static guint gst_stream_collection_signals[LAST_SIGNAL] = { 0 };
68
69 static void gst_stream_collection_dispose (GObject * object);
70
71 static void gst_stream_collection_set_property (GObject * object, guint prop_id,
72     const GValue * value, GParamSpec * pspec);
73 static void gst_stream_collection_get_property (GObject * object, guint prop_id,
74     GValue * value, GParamSpec * pspec);
75
76 static void
77 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
78     GstStreamCollection * collection);
79
80 #define _do_init                                \
81 { \
82   GST_DEBUG_CATEGORY_INIT (stream_collection_debug, "streamcollection", GST_DEBUG_BOLD, \
83       "debugging info for the stream collection objects"); \
84   \
85 }
86
87 #define gst_stream_collection_parent_class parent_class
88 G_DEFINE_TYPE_WITH_CODE (GstStreamCollection, gst_stream_collection,
89     GST_TYPE_OBJECT, _do_init);
90
91 static void
92 gst_stream_collection_class_init (GstStreamCollectionClass * klass)
93 {
94   GObjectClass *gobject_class;
95
96   gobject_class = (GObjectClass *) klass;
97
98   g_type_class_add_private (klass, sizeof (GstStreamCollectionPrivate));
99
100   gobject_class->set_property = gst_stream_collection_set_property;
101   gobject_class->get_property = gst_stream_collection_get_property;
102
103   /**
104    * GstStream:upstream-id:
105    *
106    * stream-id
107    */
108   g_object_class_install_property (gobject_class, PROP_UPSTREAM_ID,
109       g_param_spec_string ("upstream-id", "Upstream ID",
110           "The stream ID of the parent stream",
111           NULL,
112           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
113
114   /**
115    * GstStream::stream-notify:
116    * @collection: a #GstStreamCollection
117    * @prop_stream: the #GstStream that originated the signal
118    * @prop: the property that changed
119    *
120    * The stream notify signal is used to be notified of property changes to
121    * streams within the collection.
122    */
123   gst_stream_collection_signals[SIG_STREAM_NOTIFY] =
124       g_signal_new ("stream-notify", G_TYPE_FROM_CLASS (klass),
125       G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
126       G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstStreamCollectionClass,
127           stream_notify), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
128       2, GST_TYPE_STREAM, G_TYPE_PARAM);
129
130   gobject_class->dispose = gst_stream_collection_dispose;
131 }
132
133 static void
134 gst_stream_collection_init (GstStreamCollection * collection)
135 {
136   collection->priv = GST_STREAM_COLLECTION_GET_PRIVATE (collection);
137   collection->priv->streams = g_queue_new ();
138 }
139
140 static void
141 release_gst_stream (GstStream * stream, GstStreamCollection * collection)
142 {
143   g_signal_handlers_disconnect_by_func (stream,
144       proxy_stream_notify_cb, collection);
145   gst_object_unref (stream);
146 }
147
148 static void
149 gst_stream_collection_dispose (GObject * object)
150 {
151   GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
152
153   if (collection->upstream_id) {
154     g_free (collection->upstream_id);
155     collection->upstream_id = NULL;
156   }
157
158   if (collection->priv->streams) {
159     g_queue_foreach (collection->priv->streams,
160         (GFunc) release_gst_stream, collection);
161     g_queue_free (collection->priv->streams);
162     collection->priv->streams = NULL;
163   }
164
165   G_OBJECT_CLASS (parent_class)->dispose (object);
166 }
167
168 /**
169  * gst_stream_collection_new:
170  * @upstream_id: (allow-none): The stream id of the parent stream
171  *
172  * Create a new #GstStreamCollection.
173  *
174  * Returns: The new #GstStreamCollection.
175  *
176  * Since: 1.10
177  */
178 GstStreamCollection *
179 gst_stream_collection_new (const gchar * upstream_id)
180 {
181   return g_object_new (GST_TYPE_STREAM_COLLECTION, "upstream-id", upstream_id,
182       NULL);
183 }
184
185 static void
186 gst_stream_collection_set_upstream_id (GstStreamCollection * collection,
187     const gchar * upstream_id)
188 {
189   g_return_if_fail (collection->upstream_id == NULL);
190
191   /* Upstream ID should only be set once on construction, but let's
192    * not leak in case someone does something silly */
193   if (collection->upstream_id)
194     g_free (collection->upstream_id);
195
196   if (upstream_id)
197     collection->upstream_id = g_strdup (upstream_id);
198 }
199
200 /**
201  * gst_stream_collection_get_upstream_id:
202  * @collection: a #GstStreamCollection
203  *
204  * Returns the upstream id of the @collection.
205  *
206  * Returns: (transfer none): The upstream id
207  *
208  * Since: 1.10
209  */
210 const gchar *
211 gst_stream_collection_get_upstream_id (GstStreamCollection * collection)
212 {
213   const gchar *res;
214
215   res = collection->upstream_id;
216
217   return res;
218 }
219
220 static void
221 gst_stream_collection_set_property (GObject * object, guint prop_id,
222     const GValue * value, GParamSpec * pspec)
223 {
224   GstStreamCollection *collection;
225
226   collection = GST_STREAM_COLLECTION_CAST (object);
227
228   switch (prop_id) {
229     case PROP_UPSTREAM_ID:
230       gst_stream_collection_set_upstream_id (collection,
231           g_value_get_string (value));
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235       break;
236   }
237 }
238
239 static void
240 gst_stream_collection_get_property (GObject * object, guint prop_id,
241     GValue * value, GParamSpec * pspec)
242 {
243   GstStreamCollection *collection;
244
245   collection = GST_STREAM_COLLECTION_CAST (object);
246
247   switch (prop_id) {
248     case PROP_UPSTREAM_ID:
249       g_value_set_string (value,
250           gst_stream_collection_get_upstream_id (collection));
251       break;
252     default:
253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254       break;
255   }
256 }
257
258 static void
259 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
260     GstStreamCollection * collection)
261 {
262   GST_DEBUG_OBJECT (collection, "Stream %" GST_PTR_FORMAT " updated %s",
263       stream, pspec->name);
264   g_signal_emit (collection, gst_stream_collection_signals[SIG_STREAM_NOTIFY],
265       g_quark_from_string (pspec->name), stream, pspec);
266 }
267
268 /**
269  * gst_stream_collection_add_stream:
270  * @collection: a #GstStreamCollection
271  * @stream: (transfer full): the #GstStream to add
272  *
273  * Add the given @stream to the @collection.
274  *
275  * Returns: %TRUE if the @stream was properly added, else %FALSE
276  *
277  * Since: 1.10
278  */
279 gboolean
280 gst_stream_collection_add_stream (GstStreamCollection * collection,
281     GstStream * stream)
282 {
283   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), FALSE);
284   g_return_val_if_fail (GST_IS_STREAM (stream), FALSE);
285   g_return_val_if_fail (collection->priv->streams, FALSE);
286
287   GST_DEBUG_OBJECT (collection, "Adding stream %" GST_PTR_FORMAT, stream);
288
289   g_queue_push_tail (collection->priv->streams, stream);
290   g_signal_connect (stream, "notify", (GCallback) proxy_stream_notify_cb,
291       collection);
292
293   return TRUE;
294 }
295
296 /**
297  * gst_stream_collection_get_size:
298  * @collection: a #GstStreamCollection
299  *
300  * Get the number of streams this collection contains
301  *
302  * Returns: The number of streams that @collection contains
303  *
304  * Since: 1.10
305  */
306 guint
307 gst_stream_collection_get_size (GstStreamCollection * collection)
308 {
309   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), 0);
310   g_return_val_if_fail (collection->priv->streams, 0);
311
312   return g_queue_get_length (collection->priv->streams);
313 }
314
315 /**
316  * gst_stream_collection_get_stream:
317  * @collection: a #GstStreamCollection
318  * @index: Index of the stream to retrieve
319  *
320  * Retrieve the #GstStream with index @index from the collection.
321  *
322  * The caller should not modify the returned #GstStream
323  *
324  * Returns: (transfer none): A #GstStream
325  *
326  * Since: 1.10
327  */
328 GstStream *
329 gst_stream_collection_get_stream (GstStreamCollection * collection, guint index)
330 {
331   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
332   g_return_val_if_fail (collection->priv->streams, NULL);
333
334   return g_queue_peek_nth (collection->priv->streams, index);
335 }