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