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 * #GMemoryOuputStream or to write to a file with a #GFileOuputStream.
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
64 #ifdef HAVE_CONFIG_H
65 #include <config.h>
66 #endif
67
68 #include "gstgiostreamsink.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
88 static void gst_gio_stream_sink_finalize (GObject * object);
89 static void gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
93 static GOutputStream *gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink);
94
95 static void
gst_gio_stream_sink_class_init(GstGioStreamSinkClass * klass)96 gst_gio_stream_sink_class_init (GstGioStreamSinkClass * klass)
97 {
98 GObjectClass *gobject_class = (GObjectClass *) klass;
99 GstElementClass *gstelement_class = (GstElementClass *) klass;
100 GstGioBaseSinkClass *ggbsink_class = (GstGioBaseSinkClass *) klass;
101
102 GST_DEBUG_CATEGORY_INIT (gst_gio_stream_sink_debug, "gio_stream_sink", 0,
103 "GIO stream sink");
104
105 gobject_class->finalize = gst_gio_stream_sink_finalize;
106 gobject_class->set_property = gst_gio_stream_sink_set_property;
107 gobject_class->get_property = gst_gio_stream_sink_get_property;
108
109 g_object_class_install_property (gobject_class, PROP_STREAM,
110 g_param_spec_object ("stream", "Stream", "Stream to write to",
111 G_TYPE_OUTPUT_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112
113 gst_element_class_set_static_metadata (gstelement_class, "GIO stream sink",
114 "Sink",
115 "Write to any GIO stream",
116 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
117
118 ggbsink_class->get_stream =
119 GST_DEBUG_FUNCPTR (gst_gio_stream_sink_get_stream);
120 }
121
122 static void
gst_gio_stream_sink_init(GstGioStreamSink * sink)123 gst_gio_stream_sink_init (GstGioStreamSink * sink)
124 {
125 }
126
127 static void
gst_gio_stream_sink_finalize(GObject * object)128 gst_gio_stream_sink_finalize (GObject * object)
129 {
130 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
131
132 if (sink->stream) {
133 g_object_unref (sink->stream);
134 sink->stream = NULL;
135 }
136
137 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
138 }
139
140 static void
gst_gio_stream_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)141 gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
142 const GValue * value, GParamSpec * pspec)
143 {
144 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
145
146 switch (prop_id) {
147 case PROP_STREAM:{
148 GObject *stream;
149
150 if (GST_STATE (sink) == GST_STATE_PLAYING ||
151 GST_STATE (sink) == GST_STATE_PAUSED) {
152 GST_WARNING
153 ("Setting a new stream not supported in PLAYING or PAUSED state");
154 break;
155 }
156
157 stream = g_value_dup_object (value);
158 if (sink->stream)
159 g_object_unref (sink->stream);
160 sink->stream = G_OUTPUT_STREAM (stream);
161 break;
162 }
163 default:
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 break;
166 }
167 }
168
169 static void
gst_gio_stream_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)170 gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
171 GValue * value, GParamSpec * pspec)
172 {
173 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
174
175 switch (prop_id) {
176 case PROP_STREAM:
177 g_value_set_object (value, GST_GIO_BASE_SINK (sink)->stream);
178 break;
179 default:
180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 break;
182 }
183 }
184
185 static GOutputStream *
gst_gio_stream_sink_get_stream(GstGioBaseSink * bsink)186 gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink)
187 {
188 GstGioStreamSink *sink = GST_GIO_STREAM_SINK (bsink);
189
190 return (sink->stream) ? g_object_ref (sink->stream) : NULL;
191 }
192