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 
70 GST_DEBUG_CATEGORY_STATIC (gst_gio_stream_src_debug);
71 #define GST_CAT_DEFAULT gst_gio_stream_src_debug
72 
73 enum
74 {
75   PROP_0,
76   PROP_STREAM
77 };
78 
79 #define gst_gio_stream_src_parent_class parent_class
80 G_DEFINE_TYPE (GstGioStreamSrc, gst_gio_stream_src, GST_TYPE_GIO_BASE_SRC);
81 
82 static void gst_gio_stream_src_finalize (GObject * object);
83 static void gst_gio_stream_src_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_gio_stream_src_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87 static GInputStream *gst_gio_stream_src_get_stream (GstGioBaseSrc * bsrc);
88 
89 static void
gst_gio_stream_src_class_init(GstGioStreamSrcClass * klass)90 gst_gio_stream_src_class_init (GstGioStreamSrcClass * klass)
91 {
92   GObjectClass *gobject_class = (GObjectClass *) klass;
93   GstElementClass *gstelement_class = (GstElementClass *) klass;
94   GstGioBaseSrcClass *gstgiobasesrc_class = (GstGioBaseSrcClass *) klass;
95 
96   GST_DEBUG_CATEGORY_INIT (gst_gio_stream_src_debug, "gio_stream_src", 0,
97       "GIO source");
98 
99   gobject_class->finalize = gst_gio_stream_src_finalize;
100   gobject_class->set_property = gst_gio_stream_src_set_property;
101   gobject_class->get_property = gst_gio_stream_src_get_property;
102 
103   g_object_class_install_property (gobject_class, PROP_STREAM,
104       g_param_spec_object ("stream", "Stream", "Stream to read from",
105           G_TYPE_INPUT_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 
107   gst_element_class_set_static_metadata (gstelement_class, "GIO stream source",
108       "Source",
109       "Read from any GIO stream",
110       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
111 
112   gstgiobasesrc_class->get_stream =
113       GST_DEBUG_FUNCPTR (gst_gio_stream_src_get_stream);
114 }
115 
116 static void
gst_gio_stream_src_init(GstGioStreamSrc * src)117 gst_gio_stream_src_init (GstGioStreamSrc * src)
118 {
119 }
120 
121 static void
gst_gio_stream_src_finalize(GObject * object)122 gst_gio_stream_src_finalize (GObject * object)
123 {
124   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
125 
126   if (src->stream) {
127     g_object_unref (src->stream);
128     src->stream = NULL;
129   }
130 
131   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
132 }
133 
134 static void
gst_gio_stream_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)135 gst_gio_stream_src_set_property (GObject * object, guint prop_id,
136     const GValue * value, GParamSpec * pspec)
137 {
138   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
139 
140   switch (prop_id) {
141     case PROP_STREAM:{
142       GObject *stream;
143 
144       if (GST_STATE (src) == GST_STATE_PLAYING ||
145           GST_STATE (src) == GST_STATE_PAUSED) {
146         GST_WARNING
147             ("Setting a new stream not supported in PLAYING or PAUSED state");
148         break;
149       }
150 
151       stream = g_value_dup_object (value);
152       if (src->stream)
153         g_object_unref (src->stream);
154       src->stream = G_INPUT_STREAM (stream);
155       break;
156     }
157     default:
158       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159       break;
160   }
161 }
162 
163 static void
gst_gio_stream_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)164 gst_gio_stream_src_get_property (GObject * object, guint prop_id,
165     GValue * value, GParamSpec * pspec)
166 {
167   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (object);
168 
169   switch (prop_id) {
170     case PROP_STREAM:
171       g_value_set_object (value, src->stream);
172       break;
173     default:
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175       break;
176   }
177 }
178 
179 static GInputStream *
gst_gio_stream_src_get_stream(GstGioBaseSrc * bsrc)180 gst_gio_stream_src_get_stream (GstGioBaseSrc * bsrc)
181 {
182   GstGioStreamSrc *src = GST_GIO_STREAM_SRC (bsrc);
183 
184   return (src->stream) ? g_object_ref (src->stream) : NULL;
185 }
186