Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-base.git] / ext / gio / gstgiostreamsink.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-giostreamsink
24  *
25  * This plugin writes incoming data to a custom GIO #GOutputStream.
26  *
27  * It can, for example, be used to write a stream to memory with a
28  * #GMemoryOuputStream or to write to a file with a #GFileOuputStream.
29  *
30  * <refsect2>
31  * <title>Example code</title>
32  * <para>
33  * The following example writes the received data to a #GMemoryOutputStream.
34  * |[
35
36 #include &lt;gst/gst.h&gt;
37 #include &lt;gio/gio.h&gt;
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  * </para>
62  * </refsect2>
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include <config.h>
67 #endif
68
69 #include "gstgiostreamsink.h"
70
71 GST_DEBUG_CATEGORY_STATIC (gst_gio_stream_sink_debug);
72 #define GST_CAT_DEFAULT gst_gio_stream_sink_debug
73
74 /* Filter signals and args */
75 enum
76 {
77   LAST_SIGNAL
78 };
79
80 enum
81 {
82   PROP_0,
83   PROP_STREAM
84 };
85
86 #define gst_gio_stream_sink_parent_class parent_class
87 G_DEFINE_TYPE (GstGioStreamSink, gst_gio_stream_sink, GST_TYPE_GIO_BASE_SINK);
88
89 static void gst_gio_stream_sink_finalize (GObject * object);
90 static void gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94 static GOutputStream *gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink);
95
96 static void
97 gst_gio_stream_sink_class_init (GstGioStreamSinkClass * klass)
98 {
99   GObjectClass *gobject_class = (GObjectClass *) klass;
100   GstElementClass *gstelement_class = (GstElementClass *) klass;
101   GstGioBaseSinkClass *ggbsink_class = (GstGioBaseSinkClass *) klass;
102
103   GST_DEBUG_CATEGORY_INIT (gst_gio_stream_sink_debug, "gio_stream_sink", 0,
104       "GIO stream sink");
105
106   gobject_class->finalize = gst_gio_stream_sink_finalize;
107   gobject_class->set_property = gst_gio_stream_sink_set_property;
108   gobject_class->get_property = gst_gio_stream_sink_get_property;
109
110   g_object_class_install_property (gobject_class, PROP_STREAM,
111       g_param_spec_object ("stream", "Stream", "Stream to write to",
112           G_TYPE_OUTPUT_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113
114   gst_element_class_set_details_simple (gstelement_class, "GIO stream sink",
115       "Sink",
116       "Write to any GIO stream",
117       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
118
119   ggbsink_class->get_stream =
120       GST_DEBUG_FUNCPTR (gst_gio_stream_sink_get_stream);
121 }
122
123 static void
124 gst_gio_stream_sink_init (GstGioStreamSink * sink)
125 {
126 }
127
128 static void
129 gst_gio_stream_sink_finalize (GObject * object)
130 {
131   GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
132
133   if (sink->stream) {
134     g_object_unref (sink->stream);
135     sink->stream = NULL;
136   }
137
138   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
139 }
140
141 static void
142 gst_gio_stream_sink_set_property (GObject * object, guint prop_id,
143     const GValue * value, GParamSpec * pspec)
144 {
145   GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
146
147   switch (prop_id) {
148     case PROP_STREAM:{
149       GObject *stream;
150
151       if (GST_STATE (sink) == GST_STATE_PLAYING ||
152           GST_STATE (sink) == GST_STATE_PAUSED) {
153         GST_WARNING
154             ("Setting a new stream not supported in PLAYING or PAUSED state");
155         break;
156       }
157
158       stream = g_value_dup_object (value);
159       if (sink->stream)
160         g_object_unref (sink->stream);
161       sink->stream = G_OUTPUT_STREAM (stream);
162       break;
163     }
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167   }
168 }
169
170 static void
171 gst_gio_stream_sink_get_property (GObject * object, guint prop_id,
172     GValue * value, GParamSpec * pspec)
173 {
174   GstGioStreamSink *sink = GST_GIO_STREAM_SINK (object);
175
176   switch (prop_id) {
177     case PROP_STREAM:
178       g_value_set_object (value, GST_GIO_BASE_SINK (sink)->stream);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183   }
184 }
185
186 static GOutputStream *
187 gst_gio_stream_sink_get_stream (GstGioBaseSink * bsink)
188 {
189   GstGioStreamSink *sink = GST_GIO_STREAM_SINK (bsink);
190
191   return (sink->stream) ? g_object_ref (sink->stream) : NULL;
192 }