• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  * Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 /**
23  * SECTION:element-giostreamsrc
24  * @title: giostreamsrc
25  *
26  * This plugin reads data from a custom GIO #GInputStream.
27  *
28  * It can, for example, be used to read data from memory with a
29  * #GMemoryInputStream or to read from a file with a
30  * #GFileInputStream.
31  *
32  * ## Example code
33  *
34  * The following example reads data from a #GMemoryInputStream.
35  * |[
36 
37 #include <gst/gst.h>
38 #include <gio/gio.h>
39 
40 ...
41 
42 GstElement *src;
43 GMemoryInputStream *stream;
44 // in_data will contain the data to send
45 guint8 *in_data;
46 gint i;
47 
48 ...
49 in_data = g_new (guint8, 512);
50 for (i = 0; i < 512; i++)
51   in_data[i] = i % 256;
52 
53 stream = G_MEMORY_INPUT_STREAM (g_memory_input_stream_new_from_data (in_data, 512,
54           (GDestroyNotify) g_free));
55 src = gst_element_factory_make ("giostreamsrc", "src");
56 g_object_set (G_OBJECT (src), "stream", stream, NULL);
57 
58 ...
59 
60  * ]|
61  *
62  */
63 
64 #ifdef HAVE_CONFIG_H
65 #include <config.h>
66 #endif
67 
68 #include "gstgiostreamsrc.h"
69 #include "gstgioelements.h"
70 
71 GST_DEBUG_CATEGORY_STATIC (gst_gio_stream_src_debug);
72 #define GST_CAT_DEFAULT gst_gio_stream_src_debug
73 
74 enum
75 {
76   PROP_0,
77   PROP_STREAM
78 };
79 
80 #define gst_gio_stream_src_parent_class parent_class
81 G_DEFINE_TYPE (GstGioStreamSrc, gst_gio_stream_src, GST_TYPE_GIO_BASE_SRC);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (giostreamsrc, "giostreamsrc",
83     GST_RANK_NONE, GST_TYPE_GIO_STREAM_SRC, gio_element_init (plugin));
84 
85 static void gst_gio_stream_src_finalize (GObject * object);
86 static void gst_gio_stream_src_set_property (GObject * object, guint prop_id,
87     const GValue * value, GParamSpec * pspec);
88 static void gst_gio_stream_src_get_property (GObject * object, guint prop_id,
89     GValue * value, GParamSpec * pspec);
90 static GInputStream *gst_gio_stream_src_get_stream (GstGioBaseSrc * bsrc);
91 
92 static void
gst_gio_stream_src_class_init(GstGioStreamSrcClass * klass)93 gst_gio_stream_src_class_init (GstGioStreamSrcClass * klass)
94 {
95   GObjectClass *gobject_class = (GObjectClass *) klass;
96   GstElementClass *gstelement_class = (GstElementClass *) klass;
97   GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
98 
99   GST_DEBUG_CATEGORY_INIT (gst_gio_stream_src_debug, "gio_stream_src", 0,
100       "GIO source");
101 
102   gobject_class->finalize = gst_gio_stream_src_finalize;
103   gobject_class->set_property = gst_gio_stream_src_set_property;
104   gobject_class->get_property = gst_gio_stream_src_get_property;
105 
106   g_object_class_install_property (gobject_class, PROP_STREAM,
107       g_param_spec_object ("stream", "Stream", "Stream to read from",
108           G_TYPE_INPUT_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109 
110   gst_element_class_set_static_metadata (gstelement_class, "GIO stream source",
111       "Source",
112       "Read from any GIO stream",
113       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
114 
115   gstgiobasesrc_class->get_stream =
116       GST_DEBUG_FUNCPTR (gst_gio_stream_src_get_stream);
117 }
118 
119 static void
gst_gio_stream_src_init(GstGioStreamSrc * src)120 gst_gio_stream_src_init (GstGioStreamSrc * src)
121 {
122 }
123 
124 static void
gst_gio_stream_src_finalize(GObject * object)125 gst_gio_stream_src_finalize (GObject * object)
126 {
127   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
128 
129   if (src->stream) {
130     g_object_unref (src->stream);
131     src->stream = NULL;
132   }
133 
134   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
135 }
136 
137 static void
gst_gio_stream_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)138 gst_gio_stream_src_set_property (GObject * object, guint prop_id,
139     const GValue * value, GParamSpec * pspec)
140 {
141   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
142 
143   switch (prop_id) {
144     case PROP_STREAM:{
145       GObject *stream;
146 
147       if (GST_STATE (src) == GST_STATE_PLAYING ||
148           GST_STATE (src) == GST_STATE_PAUSED) {
149         GST_WARNING
150             ("Setting a new stream not supported in PLAYING or PAUSED state");
151         break;
152       }
153 
154       stream = g_value_dup_object (value);
155       if (src->stream)
156         g_object_unref (src->stream);
157       src->stream = G_INPUT_STREAM (stream);
158       break;
159     }
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162       break;
163   }
164 }
165 
166 static void
gst_gio_stream_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)167 gst_gio_stream_src_get_property (GObject * object, guint prop_id,
168     GValue * value, GParamSpec * pspec)
169 {
170   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
171 
172   switch (prop_id) {
173     case PROP_STREAM:
174       g_value_set_object (value, src->stream);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181 
182 static GInputStream *
gst_gio_stream_src_get_stream(GstGioBaseSrc * bsrc)183 gst_gio_stream_src_get_stream (GstGioBaseSrc * bsrc)
184 {
185   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (bsrc);
186 
187   return (src->stream) ? g_object_ref (src->stream) : NULL;
188 }
189