gst-inspect: fix unused-const-variable error in windows
[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  * @title: GstStreamCollection
30  * @short_description: Base class for collection of streams
31  *
32  * Since: 1.10
33  */
34
35 #include "gst_private.h"
36
37 #include "gstenumtypes.h"
38 #include "gstevent.h"
39 #include "gststreamcollection.h"
40
41 GST_DEBUG_CATEGORY_STATIC (stream_collection_debug);
42 #define GST_CAT_DEFAULT stream_collection_debug
43
44 struct _GstStreamCollectionPrivate
45 {
46   /* Maybe switch this to a GArray if performance is
47    * ever an issue? */
48   GQueue streams;
49 };
50
51 /* stream signals and properties */
52 enum
53 {
54   SIG_STREAM_NOTIFY,
55   LAST_SIGNAL
56 };
57
58 enum
59 {
60   PROP_0,
61   PROP_UPSTREAM_ID,
62   PROP_LAST
63 };
64
65 static guint gst_stream_collection_signals[LAST_SIGNAL] = { 0 };
66
67 static void gst_stream_collection_dispose (GObject * object);
68
69 static void gst_stream_collection_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_stream_collection_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73
74 static void
75 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
76     GstStreamCollection * collection);
77
78 #define _do_init                                \
79 { \
80   GST_DEBUG_CATEGORY_INIT (stream_collection_debug, "streamcollection", GST_DEBUG_BOLD, \
81       "debugging info for the stream collection objects"); \
82   \
83 }
84
85 #define gst_stream_collection_parent_class parent_class
86 G_DEFINE_TYPE_WITH_CODE (GstStreamCollection, gst_stream_collection,
87     GST_TYPE_OBJECT, G_ADD_PRIVATE (GstStreamCollection) _do_init);
88
89 static void
90 gst_stream_collection_class_init (GstStreamCollectionClass * klass)
91 {
92   GObjectClass *gobject_class;
93
94   gobject_class = (GObjectClass *) klass;
95
96   gobject_class->set_property = gst_stream_collection_set_property;
97   gobject_class->get_property = gst_stream_collection_get_property;
98
99   /**
100    * GstStream:upstream-id:
101    *
102    * stream-id
103    */
104   g_object_class_install_property (gobject_class, PROP_UPSTREAM_ID,
105       g_param_spec_string ("upstream-id", "Upstream ID",
106           "The stream ID of the parent stream",
107           NULL,
108           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
109
110   /**
111    * GstStream::stream-notify:
112    * @collection: a #GstStreamCollection
113    * @prop_stream: the #GstStream that originated the signal
114    * @prop: the property that changed
115    *
116    * The stream notify signal is used to be notified of property changes to
117    * streams within the collection.
118    */
119   gst_stream_collection_signals[SIG_STREAM_NOTIFY] =
120       g_signal_new ("stream-notify", G_TYPE_FROM_CLASS (klass),
121       G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
122       G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstStreamCollectionClass,
123           stream_notify), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
124       2, GST_TYPE_STREAM, G_TYPE_PARAM);
125
126   gobject_class->dispose = gst_stream_collection_dispose;
127 }
128
129 static void
130 gst_stream_collection_init (GstStreamCollection * collection)
131 {
132   collection->priv = gst_stream_collection_get_instance_private (collection);
133   g_queue_init (&collection->priv->streams);
134 }
135
136 static void
137 release_gst_stream (GstStream * stream, GstStreamCollection * collection)
138 {
139   g_signal_handlers_disconnect_by_func (stream,
140       proxy_stream_notify_cb, collection);
141   gst_object_unref (stream);
142 }
143
144 static void
145 gst_stream_collection_dispose (GObject * object)
146 {
147   GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
148
149   if (collection->upstream_id) {
150     g_free (collection->upstream_id);
151     collection->upstream_id = NULL;
152   }
153
154   g_queue_foreach (&collection->priv->streams,
155       (GFunc) release_gst_stream, collection);
156   g_queue_clear (&collection->priv->streams);
157
158   G_OBJECT_CLASS (parent_class)->dispose (object);
159 }
160
161 /**
162  * gst_stream_collection_new:
163  * @upstream_id: (allow-none): The stream id of the parent stream
164  *
165  * Create a new #GstStreamCollection.
166  *
167  * Returns: (transfer full): The new #GstStreamCollection.
168  *
169  * Since: 1.10
170  */
171 GstStreamCollection *
172 gst_stream_collection_new (const gchar * upstream_id)
173 {
174   GstStreamCollection *collection;
175
176   collection =
177       g_object_new (GST_TYPE_STREAM_COLLECTION, "upstream-id", upstream_id,
178       NULL);
179
180   /* Clear floating flag */
181   g_object_ref_sink (collection);
182
183   return collection;
184 }
185
186 static void
187 gst_stream_collection_set_upstream_id (GstStreamCollection * collection,
188     const gchar * upstream_id)
189 {
190   g_return_if_fail (collection->upstream_id == NULL);
191
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);
196
197   if (upstream_id)
198     collection->upstream_id = g_strdup (upstream_id);
199 }
200
201 /**
202  * gst_stream_collection_get_upstream_id:
203  * @collection: a #GstStreamCollection
204  *
205  * Returns the upstream id of the @collection.
206  *
207  * Returns: (transfer none): The upstream id
208  *
209  * Since: 1.10
210  */
211 const gchar *
212 gst_stream_collection_get_upstream_id (GstStreamCollection * collection)
213 {
214   const gchar *res;
215
216   res = collection->upstream_id;
217
218   return res;
219 }
220
221 static void
222 gst_stream_collection_set_property (GObject * object, guint prop_id,
223     const GValue * value, GParamSpec * pspec)
224 {
225   GstStreamCollection *collection;
226
227   collection = GST_STREAM_COLLECTION_CAST (object);
228
229   switch (prop_id) {
230     case PROP_UPSTREAM_ID:
231       gst_stream_collection_set_upstream_id (collection,
232           g_value_get_string (value));
233       break;
234     default:
235       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236       break;
237   }
238 }
239
240 static void
241 gst_stream_collection_get_property (GObject * object, guint prop_id,
242     GValue * value, GParamSpec * pspec)
243 {
244   GstStreamCollection *collection;
245
246   collection = GST_STREAM_COLLECTION_CAST (object);
247
248   switch (prop_id) {
249     case PROP_UPSTREAM_ID:
250       g_value_set_string (value,
251           gst_stream_collection_get_upstream_id (collection));
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256   }
257 }
258
259 static void
260 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
261     GstStreamCollection * collection)
262 {
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);
267 }
268
269 /**
270  * gst_stream_collection_add_stream:
271  * @collection: a #GstStreamCollection
272  * @stream: (transfer full): the #GstStream to add
273  *
274  * Add the given @stream to the @collection.
275  *
276  * Returns: %TRUE if the @stream was properly added, else %FALSE
277  *
278  * Since: 1.10
279  */
280 gboolean
281 gst_stream_collection_add_stream (GstStreamCollection * collection,
282     GstStream * stream)
283 {
284   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), FALSE);
285   g_return_val_if_fail (GST_IS_STREAM (stream), 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
311   return g_queue_get_length (&collection->priv->streams);
312 }
313
314 /**
315  * gst_stream_collection_get_stream:
316  * @collection: a #GstStreamCollection
317  * @index: Index of the stream to retrieve
318  *
319  * Retrieve the #GstStream with index @index from the collection.
320  *
321  * The caller should not modify the returned #GstStream
322  *
323  * Returns: (transfer none) (nullable): A #GstStream
324  *
325  * Since: 1.10
326  */
327 GstStream *
328 gst_stream_collection_get_stream (GstStreamCollection * collection, guint index)
329 {
330   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
331
332   return g_queue_peek_nth (&collection->priv->streams, index);
333 }