poll: minor docs clarification
[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
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);
76
77 static void
78 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
79     GstStreamCollection * collection);
80
81 #define _do_init                                \
82 { \
83   GST_DEBUG_CATEGORY_INIT (stream_collection_debug, "streamcollection", GST_DEBUG_BOLD, \
84       "debugging info for the stream collection objects"); \
85   \
86 }
87
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);
91
92 static void
93 gst_stream_collection_class_init (GstStreamCollectionClass * klass)
94 {
95   GObjectClass *gobject_class;
96
97   gobject_class = (GObjectClass *) klass;
98
99   g_type_class_add_private (klass, sizeof (GstStreamCollectionPrivate));
100
101   gobject_class->set_property = gst_stream_collection_set_property;
102   gobject_class->get_property = gst_stream_collection_get_property;
103
104   /**
105    * GstStream:upstream-id:
106    *
107    * stream-id
108    */
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",
112           NULL,
113           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
114
115   /**
116    * GstStream::stream-notify:
117    * @collection: a #GstStreamCollection
118    * @prop_stream: the #GstStream that originated the signal
119    * @prop: the property that changed
120    *
121    * The stream notify signal is used to be notified of property changes to
122    * streams within the collection.
123    */
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);
130
131   gobject_class->dispose = gst_stream_collection_dispose;
132 }
133
134 static void
135 gst_stream_collection_init (GstStreamCollection * collection)
136 {
137   collection->priv = GST_STREAM_COLLECTION_GET_PRIVATE (collection);
138   g_queue_init (&collection->priv->streams);
139 }
140
141 static void
142 release_gst_stream (GstStream * stream, GstStreamCollection * collection)
143 {
144   g_signal_handlers_disconnect_by_func (stream,
145       proxy_stream_notify_cb, collection);
146   gst_object_unref (stream);
147 }
148
149 static void
150 gst_stream_collection_dispose (GObject * object)
151 {
152   GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
153
154   if (collection->upstream_id) {
155     g_free (collection->upstream_id);
156     collection->upstream_id = NULL;
157   }
158
159   g_queue_foreach (&collection->priv->streams,
160       (GFunc) release_gst_stream, collection);
161   g_queue_clear (&collection->priv->streams);
162
163   G_OBJECT_CLASS (parent_class)->dispose (object);
164 }
165
166 /**
167  * gst_stream_collection_new:
168  * @upstream_id: (allow-none): The stream id of the parent stream
169  *
170  * Create a new #GstStreamCollection.
171  *
172  * Returns: (transfer full): The new #GstStreamCollection.
173  *
174  * Since: 1.10
175  */
176 GstStreamCollection *
177 gst_stream_collection_new (const gchar * upstream_id)
178 {
179   GstStreamCollection *collection;
180
181   collection =
182       g_object_new (GST_TYPE_STREAM_COLLECTION, "upstream-id", upstream_id,
183       NULL);
184
185   /* Clear floating flag */
186   g_object_ref_sink (collection);
187
188   return collection;
189 }
190
191 static void
192 gst_stream_collection_set_upstream_id (GstStreamCollection * collection,
193     const gchar * upstream_id)
194 {
195   g_return_if_fail (collection->upstream_id == NULL);
196
197   /* Upstream ID should only be set once on construction, but let's
198    * not leak in case someone does something silly */
199   if (collection->upstream_id)
200     g_free (collection->upstream_id);
201
202   if (upstream_id)
203     collection->upstream_id = g_strdup (upstream_id);
204 }
205
206 /**
207  * gst_stream_collection_get_upstream_id:
208  * @collection: a #GstStreamCollection
209  *
210  * Returns the upstream id of the @collection.
211  *
212  * Returns: (transfer none): The upstream id
213  *
214  * Since: 1.10
215  */
216 const gchar *
217 gst_stream_collection_get_upstream_id (GstStreamCollection * collection)
218 {
219   const gchar *res;
220
221   res = collection->upstream_id;
222
223   return res;
224 }
225
226 static void
227 gst_stream_collection_set_property (GObject * object, guint prop_id,
228     const GValue * value, GParamSpec * pspec)
229 {
230   GstStreamCollection *collection;
231
232   collection = GST_STREAM_COLLECTION_CAST (object);
233
234   switch (prop_id) {
235     case PROP_UPSTREAM_ID:
236       gst_stream_collection_set_upstream_id (collection,
237           g_value_get_string (value));
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244
245 static void
246 gst_stream_collection_get_property (GObject * object, guint prop_id,
247     GValue * value, GParamSpec * pspec)
248 {
249   GstStreamCollection *collection;
250
251   collection = GST_STREAM_COLLECTION_CAST (object);
252
253   switch (prop_id) {
254     case PROP_UPSTREAM_ID:
255       g_value_set_string (value,
256           gst_stream_collection_get_upstream_id (collection));
257       break;
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261   }
262 }
263
264 static void
265 proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
266     GstStreamCollection * collection)
267 {
268   GST_DEBUG_OBJECT (collection, "Stream %" GST_PTR_FORMAT " updated %s",
269       stream, pspec->name);
270   g_signal_emit (collection, gst_stream_collection_signals[SIG_STREAM_NOTIFY],
271       g_quark_from_string (pspec->name), stream, pspec);
272 }
273
274 /**
275  * gst_stream_collection_add_stream:
276  * @collection: a #GstStreamCollection
277  * @stream: (transfer full): the #GstStream to add
278  *
279  * Add the given @stream to the @collection.
280  *
281  * Returns: %TRUE if the @stream was properly added, else %FALSE
282  *
283  * Since: 1.10
284  */
285 gboolean
286 gst_stream_collection_add_stream (GstStreamCollection * collection,
287     GstStream * stream)
288 {
289   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), FALSE);
290   g_return_val_if_fail (GST_IS_STREAM (stream), FALSE);
291
292   GST_DEBUG_OBJECT (collection, "Adding stream %" GST_PTR_FORMAT, stream);
293
294   g_queue_push_tail (&collection->priv->streams, stream);
295   g_signal_connect (stream, "notify", (GCallback) proxy_stream_notify_cb,
296       collection);
297
298   return TRUE;
299 }
300
301 /**
302  * gst_stream_collection_get_size:
303  * @collection: a #GstStreamCollection
304  *
305  * Get the number of streams this collection contains
306  *
307  * Returns: The number of streams that @collection contains
308  *
309  * Since: 1.10
310  */
311 guint
312 gst_stream_collection_get_size (GstStreamCollection * collection)
313 {
314   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), 0);
315
316   return g_queue_get_length (&collection->priv->streams);
317 }
318
319 /**
320  * gst_stream_collection_get_stream:
321  * @collection: a #GstStreamCollection
322  * @index: Index of the stream to retrieve
323  *
324  * Retrieve the #GstStream with index @index from the collection.
325  *
326  * The caller should not modify the returned #GstStream
327  *
328  * Returns: (transfer none) (nullable): A #GstStream
329  *
330  * Since: 1.10
331  */
332 GstStream *
333 gst_stream_collection_get_stream (GstStreamCollection * collection, guint index)
334 {
335   g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
336
337   return g_queue_peek_nth (&collection->priv->streams, index);
338 }