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-giostreamsink
24 * @title: giostreamsink
25 *
26 * This plugin writes incoming data to a custom GIO #GOutputStream.
27 *
28 * It can, for example, be used to write a stream to memory with a
29 * #GMemoryOutputStream or to write to a file with a #GFileOutputStream.
30 *
31 * ## Example code
32 *
33 * The following example writes the received data to a #GMemoryOutputStream.
34 * |[
35
36 #include <gst/gst.h>
37 #include <gio/gio.h>
38
39 ...
40
41 GstElement *sink;
42 GMemoryOuputStream *stream;
43 // out_data will contain the received data
44 guint8 *out_data;
45
46 ...
47
48 stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (NULL, 0,
49 (GReallocFunc) g_realloc, (GDestroyNotify) g_free));
50 sink = gst_element_factory_make ("giostreamsink", "sink");
51 g_object_set (G_OBJECT (sink), "stream", stream, NULL);
52
53 ...
54
55 // after processing get the written data
56 out_data = g_memory_ouput_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream));
57
58 ...
59
60 * ]|
61 *
62 */
63 #ifdef HAVE_CONFIG_H
64 #include <config.h>
65 #endif
66
67 #include "gstgiostreamsink.h"
68 #include "gstgioelements.h"
69
70 GST_DEBUG_CATEGORY_STATIC (gst_gio_stream_sink_debug);
71 #define GST_CAT_DEFAULT gst_gio_stream_sink_debug
72
73 /* Filter signals and args */
74 enum
75 {
76 LAST_SIGNAL
77 };
78
79 enum
80 {
81 PROP_0,
82 PROP_STREAM
83 };
84
85 #define gst_gio_stream_sink_parent_class parent_class
86 G_DEFINE_TYPE (GstGioStreamSink, gst_gio_stream_sink, GST_TYPE_GIO_BASE_SINK);
87 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (giostreamsink, "giostreamsink",
88 GST_RANK_NONE, GST_TYPE_GIO_STREAM_SINK, gio_element_init (plugin));
89
90 static void gst_gio_stream_sink_finalize (GObject * object);
91 static void gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
95 static GOutputStream *gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink);
96
97 static void
gst_gio_stream_sink_class_init(GstGioStreamSinkClass * klass)98 gst_gio_stream_sink_class_init (GstGioStreamSinkClass * klass)
99 {
100 GObjectClass *gobject_class = (GObjectClass *) klass;
101 GstElementClass *gstelement_class = (GstElementClass *) klass;
102 GstGioBaseSinkClass *ggbsink_class = (GstGioBaseSinkClass *) klass;
103
104 GST_DEBUG_CATEGORY_INIT (gst_gio_stream_sink_debug, "gio_stream_sink", 0,
105 "GIO stream sink");
106
107 gobject_class->finalize = gst_gio_stream_sink_finalize;
108 gobject_class->set_property = gst_gio_stream_sink_set_property;
109 gobject_class->get_property = gst_gio_stream_sink_get_property;
110
111 g_object_class_install_property (gobject_class, PROP_STREAM,
112 g_param_spec_object ("stream", "Stream", "Stream to write to",
113 G_TYPE_OUTPUT_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114
115 gst_element_class_set_static_metadata (gstelement_class, "GIO stream sink",
116 "Sink",
117 "Write to any GIO stream",
118 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
119
120 ggbsink_class->get_stream =
121 GST_DEBUG_FUNCPTR (gst_gio_stream_sink_get_stream);
122 }
123
124 static void
gst_gio_stream_sink_init(GstGioStreamSink * sink)125 gst_gio_stream_sink_init (GstGioStreamSink * sink)
126 {
127 }
128
129 static void
gst_gio_stream_sink_finalize(GObject * object)130 gst_gio_stream_sink_finalize (GObject * object)
131 {
132 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
133
134 if (sink->stream) {
135 g_object_unref (sink->stream);
136 sink->stream = NULL;
137 }
138
139 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
140 }
141
142 static void
gst_gio_stream_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)143 gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
144 const GValue * value, GParamSpec * pspec)
145 {
146 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
147
148 switch (prop_id) {
149 case PROP_STREAM:{
150 GObject *stream;
151
152 if (GST_STATE (sink) == GST_STATE_PLAYING ||
153 GST_STATE (sink) == GST_STATE_PAUSED) {
154 GST_WARNING
155 ("Setting a new stream not supported in PLAYING or PAUSED state");
156 break;
157 }
158
159 stream = g_value_dup_object (value);
160 if (sink->stream)
161 g_object_unref (sink->stream);
162 sink->stream = G_OUTPUT_STREAM (stream);
163 break;
164 }
165 default:
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 break;
168 }
169 }
170
171 static void
gst_gio_stream_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)172 gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
173 GValue * value, GParamSpec * pspec)
174 {
175 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
176
177 switch (prop_id) {
178 case PROP_STREAM:
179 g_value_set_object (value, GST_GIO_BASE_SINK (sink)->stream);
180 break;
181 default:
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 break;
184 }
185 }
186
187 static GOutputStream *
gst_gio_stream_sink_get_stream(GstGioBaseSink * bsink)188 gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink)
189 {
190 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (bsink);
191
192 return (sink->stream) ? g_object_ref (sink->stream) : NULL;
193 }
194