• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: GstStream and 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:gststreams
29  * @title: GstStreams
30  * @short_description: Base class for stream objects
31  *
32  * A #GstStream is a high-level object defining a stream of data which is, or
33  * can be, present in a #GstPipeline.
34  *
35  * It is defined by a unique identifier, a "Stream ID". A #GstStream does not
36  * automatically imply the stream is present within a pipeline or element.
37  *
38  * Any element that can introduce new streams in a pipeline should create the
39  * appropriate #GstStream object, and can convey that object via the
40  * %GST_EVENT_STREAM_START event and/or the #GstStreamCollection.
41  *
42  * Elements that do not modify the nature of the stream can add extra information
43  * on it (such as enrich the #GstCaps, or #GstTagList). This is typically done
44  * by parsing elements.
45  *
46  * Since: 1.10
47  */
48 
49 #include "gst_private.h"
50 
51 #include "gstenumtypes.h"
52 #include "gstevent.h"
53 #include "gststreams.h"
54 
55 GST_DEBUG_CATEGORY_STATIC (streams_debug);
56 #define GST_CAT_DEFAULT streams_debug
57 
58 struct _GstStreamPrivate
59 {
60   GstStreamFlags flags;
61   GstStreamType type;
62   GstTagList *tags;
63   GstCaps *caps;
64 };
65 
66 /* stream signals and properties */
67 enum
68 {
69   LAST_SIGNAL
70 };
71 
72 enum
73 {
74   PROP_0,
75   PROP_STREAM_ID,
76   PROP_STREAM_FLAGS,
77   PROP_STREAM_TYPE,
78   PROP_TAGS,
79   PROP_CAPS,
80   PROP_LAST
81 };
82 
83 static GParamSpec *gst_stream_pspecs[PROP_LAST] = { 0 };
84 
85 #if 0
86 static guint gst_stream_signals[LAST_SIGNAL] = { 0 };
87 #endif
88 
89 static void gst_stream_finalize (GObject * object);
90 
91 static void gst_stream_set_property (GObject * object, guint prop_id,
92     const GValue * value, GParamSpec * pspec);
93 static void gst_stream_get_property (GObject * object, guint prop_id,
94     GValue * value, GParamSpec * pspec);
95 
96 #define _do_init				\
97 { \
98   GST_DEBUG_CATEGORY_INIT (streams_debug, "streams", GST_DEBUG_BOLD, \
99       "debugging info for the stream and stream collection objects"); \
100   \
101 }
102 
103 #define gst_stream_parent_class parent_class
104 G_DEFINE_TYPE_WITH_CODE (GstStream, gst_stream, GST_TYPE_OBJECT,
105     G_ADD_PRIVATE (GstStream) _do_init);
106 
107 static void
gst_stream_class_init(GstStreamClass * klass)108 gst_stream_class_init (GstStreamClass * klass)
109 {
110   GObjectClass *gobject_class;
111 
112   gobject_class = (GObjectClass *) klass;
113 
114   gobject_class->set_property = gst_stream_set_property;
115   gobject_class->get_property = gst_stream_get_property;
116 
117   /**
118    * GstStream:stream-id:
119    *
120    * The unique identifier of the #GstStream. Can only be set at construction
121    * time.
122    */
123   g_object_class_install_property (gobject_class, PROP_STREAM_ID,
124       g_param_spec_string ("stream-id", "Stream ID",
125           "The stream ID of the stream",
126           NULL,
127           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
128 
129   /**
130    * GstStream:flags:
131    *
132    * The #GstStreamFlags of the #GstStream. Can only be set at construction time.
133    **/
134   gst_stream_pspecs[PROP_STREAM_FLAGS] =
135       g_param_spec_flags ("stream-flags", "Stream Flags", "The stream flags",
136       GST_TYPE_STREAM_FLAGS, GST_STREAM_FLAG_NONE,
137       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
138   g_object_class_install_property (gobject_class, PROP_STREAM_FLAGS,
139       gst_stream_pspecs[PROP_STREAM_FLAGS]);
140 
141   /**
142    * GstStream:stream-type:
143    *
144    * The #GstStreamType of the #GstStream. Can only be set at construction time.
145    **/
146   gst_stream_pspecs[PROP_STREAM_TYPE] =
147       g_param_spec_flags ("stream-type", "Stream Type", "The type of stream",
148       GST_TYPE_STREAM_TYPE, GST_STREAM_TYPE_UNKNOWN,
149       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
150   g_object_class_install_property (gobject_class, PROP_STREAM_TYPE,
151       gst_stream_pspecs[PROP_STREAM_TYPE]);
152 
153   /**
154    * GstStream:caps:
155    *
156    * The #GstCaps of the #GstStream.
157    **/
158   gst_stream_pspecs[PROP_CAPS] =
159       g_param_spec_boxed ("caps", "Caps", "The caps of the stream",
160       GST_TYPE_CAPS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
161   g_object_class_install_property (gobject_class, PROP_CAPS,
162       gst_stream_pspecs[PROP_CAPS]);
163 
164   /**
165    * GstStream:tags:
166    *
167    * The #GstTagList of the #GstStream.
168    **/
169   gst_stream_pspecs[PROP_TAGS] =
170       g_param_spec_boxed ("tags", "Tags", "The tags of the stream",
171       GST_TYPE_TAG_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
172   g_object_class_install_property (gobject_class, PROP_TAGS,
173       gst_stream_pspecs[PROP_TAGS]);
174 
175   gobject_class->finalize = gst_stream_finalize;
176 }
177 
178 static void
gst_stream_init(GstStream * stream)179 gst_stream_init (GstStream * stream)
180 {
181   stream->priv = gst_stream_get_instance_private (stream);
182   stream->priv->type = GST_STREAM_TYPE_UNKNOWN;
183 }
184 
185 static void
gst_stream_finalize(GObject * object)186 gst_stream_finalize (GObject * object)
187 {
188   GstStream *stream = GST_STREAM_CAST (object);
189 
190   gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
191       (GstMiniObject *) NULL);
192   gst_caps_replace (&stream->priv->caps, NULL);
193   g_free ((gchar *) stream->stream_id);
194 
195   G_OBJECT_CLASS (parent_class)->finalize (object);
196 }
197 
198 /**
199  * gst_stream_new:
200  * @stream_id: (allow-none): the id for the new stream. If %NULL,
201  * a new one will be automatically generated
202  * @caps: (allow-none) (transfer none): the #GstCaps of the stream
203  * @type: the #GstStreamType of the stream
204  * @flags: the #GstStreamFlags of the stream
205  *
206  * Create a new #GstStream for the given @stream_id, @caps, @type
207  * and @flags
208  *
209  * Returns: (transfer full): The new #GstStream
210  *
211  * Since: 1.10
212  */
213 GstStream *
gst_stream_new(const gchar * stream_id,GstCaps * caps,GstStreamType type,GstStreamFlags flags)214 gst_stream_new (const gchar * stream_id, GstCaps * caps, GstStreamType type,
215     GstStreamFlags flags)
216 {
217   GstStream *stream;
218 
219   stream = g_object_new (GST_TYPE_STREAM, "stream-id", stream_id, "caps", caps,
220       "stream-type", type, "stream-flags", flags, NULL);
221 
222   /* Clear floating flag */
223   gst_object_ref_sink (stream);
224 
225   return stream;
226 }
227 
228 static void
gst_stream_set_stream_id(GstStream * stream,const gchar * stream_id)229 gst_stream_set_stream_id (GstStream * stream, const gchar * stream_id)
230 {
231   g_return_if_fail (GST_IS_STREAM (stream));
232 
233   GST_OBJECT_LOCK (stream);
234   g_assert (stream->stream_id == NULL);
235   if (stream_id)
236     stream->stream_id = g_strdup (stream_id);
237   else {
238     /* Create a random stream_id if NULL */
239     GST_FIXME_OBJECT (stream, "Creating random stream-id, consider "
240         "implementing a deterministic way of creating a stream-id");
241     stream->stream_id =
242         g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
243         g_random_int (), g_random_int ());
244   }
245 
246   GST_OBJECT_UNLOCK (stream);
247 }
248 
249 /**
250  * gst_stream_get_stream_id:
251  * @stream: a #GstStream
252  *
253  * Returns the stream ID of @stream.
254  *
255  * Returns: (transfer none) (nullable): the stream ID of @stream. Only valid
256  * during the lifetime of @stream.
257  *
258  * Since: 1.10
259  */
260 const gchar *
gst_stream_get_stream_id(GstStream * stream)261 gst_stream_get_stream_id (GstStream * stream)
262 {
263   g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
264 
265   return stream->stream_id;
266 }
267 
268 /**
269  * gst_stream_set_stream_flags:
270  * @stream: a #GstStream
271  * @flags: the flags to set on @stream
272  *
273  * Set the @flags for the @stream.
274  *
275  * Since: 1.10
276  */
277 void
gst_stream_set_stream_flags(GstStream * stream,GstStreamFlags flags)278 gst_stream_set_stream_flags (GstStream * stream, GstStreamFlags flags)
279 {
280   g_return_if_fail (GST_IS_STREAM (stream));
281 
282   GST_OBJECT_LOCK (stream);
283   stream->priv->flags = flags;
284   GST_OBJECT_UNLOCK (stream);
285 
286   g_object_notify_by_pspec (G_OBJECT (stream),
287       gst_stream_pspecs[PROP_STREAM_FLAGS]);
288 }
289 
290 /**
291  * gst_stream_get_stream_flags:
292  * @stream: a #GstStream
293  *
294  * Retrieve the current stream flags for @stream
295  *
296  * Returns: The #GstStreamFlags for @stream
297  *
298  * Since: 1.10
299  */
300 GstStreamFlags
gst_stream_get_stream_flags(GstStream * stream)301 gst_stream_get_stream_flags (GstStream * stream)
302 {
303   GstStreamFlags res;
304 
305   g_return_val_if_fail (GST_IS_STREAM (stream), GST_STREAM_FLAG_NONE);
306 
307   GST_OBJECT_LOCK (stream);
308   res = stream->priv->flags;
309   GST_OBJECT_UNLOCK (stream);
310 
311   return res;
312 }
313 
314 /**
315  * gst_stream_set_stream_type:
316  * @stream: a #GstStream
317  * @stream_type: the type to set on @stream
318  *
319  * Set the stream type of @stream
320  *
321  * Since: 1.10
322  */
323 void
gst_stream_set_stream_type(GstStream * stream,GstStreamType stream_type)324 gst_stream_set_stream_type (GstStream * stream, GstStreamType stream_type)
325 {
326   g_return_if_fail (GST_IS_STREAM (stream));
327 
328   GST_OBJECT_LOCK (stream);
329   stream->priv->type = stream_type;
330   GST_OBJECT_UNLOCK (stream);
331 
332   g_object_notify_by_pspec (G_OBJECT (stream),
333       gst_stream_pspecs[PROP_STREAM_TYPE]);
334 }
335 
336 /**
337  * gst_stream_get_stream_type:
338  * @stream: a #GstStream
339  *
340  * Retrieve the stream type for @stream
341  *
342  * Returns: The #GstStreamType for @stream
343  *
344  * Since: 1.10
345  */
346 GstStreamType
gst_stream_get_stream_type(GstStream * stream)347 gst_stream_get_stream_type (GstStream * stream)
348 {
349   GstStreamType res;
350 
351   g_return_val_if_fail (GST_IS_STREAM (stream), GST_STREAM_TYPE_UNKNOWN);
352 
353   GST_OBJECT_LOCK (stream);
354   res = stream->priv->type;
355   GST_OBJECT_UNLOCK (stream);
356 
357   return res;
358 }
359 
360 /**
361  * gst_stream_set_tags:
362  * @stream: a #GstStream
363  * @tags: (transfer none) (allow-none): a #GstTagList
364  *
365  * Set the tags for the #GstStream
366  *
367  * Since: 1.10
368  */
369 void
gst_stream_set_tags(GstStream * stream,GstTagList * tags)370 gst_stream_set_tags (GstStream * stream, GstTagList * tags)
371 {
372   gboolean notify = FALSE;
373 
374   g_return_if_fail (GST_IS_STREAM (stream));
375 
376   GST_OBJECT_LOCK (stream);
377   if (stream->priv->tags == NULL || tags == NULL
378       || !gst_tag_list_is_equal (stream->priv->tags, tags)) {
379     gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
380         (GstMiniObject *) tags);
381     notify = TRUE;
382   }
383   GST_OBJECT_UNLOCK (stream);
384 
385   if (notify)
386     g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_TAGS]);
387 }
388 
389 /**
390  * gst_stream_get_tags:
391  * @stream: a #GstStream
392  *
393  * Retrieve the tags for @stream, if any
394  *
395  * Returns: (transfer full) (nullable): The #GstTagList for @stream
396  *
397  * Since: 1.10
398  */
399 GstTagList *
gst_stream_get_tags(GstStream * stream)400 gst_stream_get_tags (GstStream * stream)
401 {
402   GstTagList *res = NULL;
403 
404   g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
405 
406   GST_OBJECT_LOCK (stream);
407   if (stream->priv->tags)
408     res = gst_tag_list_ref (stream->priv->tags);
409   GST_OBJECT_UNLOCK (stream);
410 
411   return res;
412 }
413 
414 /**
415  * gst_stream_set_caps:
416  * @stream: a #GstStream
417  * @caps: (transfer none) (allow-none): a #GstCaps
418  *
419  * Set the caps for the #GstStream
420  *
421  * Since: 1.10
422  */
423 void
gst_stream_set_caps(GstStream * stream,GstCaps * caps)424 gst_stream_set_caps (GstStream * stream, GstCaps * caps)
425 {
426   gboolean notify = FALSE;
427 
428   g_return_if_fail (GST_IS_STREAM (stream));
429 
430   GST_OBJECT_LOCK (stream);
431   if (stream->priv->caps == NULL || (caps
432           && !gst_caps_is_equal (stream->priv->caps, caps))) {
433     gst_caps_replace (&stream->priv->caps, caps);
434     notify = TRUE;
435   }
436   GST_OBJECT_UNLOCK (stream);
437 
438   if (notify)
439     g_object_notify_by_pspec (G_OBJECT (stream), gst_stream_pspecs[PROP_CAPS]);
440 }
441 
442 
443 /**
444  * gst_stream_get_caps:
445  * @stream: a #GstStream
446  *
447  * Retrieve the caps for @stream, if any
448  *
449  * Returns: (transfer full) (nullable): The #GstCaps for @stream
450  *
451  * Since: 1.10
452  */
453 GstCaps *
gst_stream_get_caps(GstStream * stream)454 gst_stream_get_caps (GstStream * stream)
455 {
456   GstCaps *res = NULL;
457 
458   g_return_val_if_fail (GST_IS_STREAM (stream), NULL);
459 
460   GST_OBJECT_LOCK (stream);
461   if (stream->priv->caps)
462     res = gst_caps_ref (stream->priv->caps);
463   GST_OBJECT_UNLOCK (stream);
464 
465   return res;
466 }
467 
468 static void
gst_stream_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)469 gst_stream_set_property (GObject * object, guint prop_id,
470     const GValue * value, GParamSpec * pspec)
471 {
472   GstStream *stream;
473 
474   stream = GST_STREAM_CAST (object);
475 
476   switch (prop_id) {
477     case PROP_STREAM_ID:
478       gst_stream_set_stream_id (stream, g_value_get_string (value));
479       break;
480     case PROP_STREAM_FLAGS:
481       GST_OBJECT_LOCK (stream);
482       stream->priv->flags = g_value_get_flags (value);
483       GST_OBJECT_UNLOCK (stream);
484       break;
485     case PROP_STREAM_TYPE:
486       GST_OBJECT_LOCK (stream);
487       stream->priv->type = g_value_get_flags (value);
488       GST_OBJECT_UNLOCK (stream);
489       break;
490     case PROP_TAGS:
491       GST_OBJECT_LOCK (stream);
492       gst_mini_object_replace ((GstMiniObject **) & stream->priv->tags,
493           (GstMiniObject *) g_value_get_boxed (value));
494       GST_OBJECT_UNLOCK (stream);
495       break;
496     case PROP_CAPS:
497       GST_OBJECT_LOCK (stream);
498       gst_mini_object_replace ((GstMiniObject **) & stream->priv->caps,
499           (GstMiniObject *) g_value_get_boxed (value));
500       GST_OBJECT_UNLOCK (stream);
501       break;
502     default:
503       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504       break;
505   }
506 }
507 
508 static void
gst_stream_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)509 gst_stream_get_property (GObject * object, guint prop_id,
510     GValue * value, GParamSpec * pspec)
511 {
512   GstStream *stream;
513 
514   stream = GST_STREAM_CAST (object);
515 
516   switch (prop_id) {
517     case PROP_STREAM_ID:
518       g_value_set_string (value, gst_stream_get_stream_id (stream));
519       break;
520     case PROP_STREAM_FLAGS:
521       g_value_set_flags (value, gst_stream_get_stream_flags (stream));
522       break;
523     case PROP_STREAM_TYPE:
524       g_value_set_flags (value, gst_stream_get_stream_type (stream));
525       break;
526     case PROP_TAGS:
527       g_value_take_boxed (value, gst_stream_get_tags (stream));
528       break;
529     case PROP_CAPS:
530       g_value_take_boxed (value, gst_stream_get_caps (stream));
531       break;
532     default:
533       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534       break;
535   }
536 }
537 
538 /**
539  * gst_stream_type_get_name:
540  * @stype: a #GstStreamType
541  *
542  * Get a descriptive string for a given #GstStreamType
543  *
544  * Returns: (nullable): A string describing the stream type
545  *
546  * Since: 1.10
547  */
548 const gchar *
gst_stream_type_get_name(GstStreamType stype)549 gst_stream_type_get_name (GstStreamType stype)
550 {
551   /* FIXME : Make this more flexible */
552   switch (stype) {
553     case GST_STREAM_TYPE_UNKNOWN:
554       return "unknown";
555     case GST_STREAM_TYPE_AUDIO:
556       return "audio";
557     case GST_STREAM_TYPE_VIDEO:
558       return "video";
559     case GST_STREAM_TYPE_CONTAINER:
560       return "container";
561     case GST_STREAM_TYPE_TEXT:
562       return "text";
563     default:
564       return NULL;
565   }
566 
567   return NULL;
568 }
569