1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/* GStreamer
*
* Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
* Copyright (C) 2007-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gstgiobasesink.h"
GST_DEBUG_CATEGORY_STATIC (gst_gio_base_sink_debug);
#define GST_CAT_DEFAULT gst_gio_base_sink_debug
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
#define gst_gio_base_sink_parent_class parent_class
G_DEFINE_TYPE (GstGioBaseSink, gst_gio_base_sink, GST_TYPE_BASE_SINK);
static void gst_gio_base_sink_finalize (GObject * object);
static gboolean gst_gio_base_sink_start (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_stop (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_unlock (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_unlock_stop (GstBaseSink * base_sink);
static gboolean gst_gio_base_sink_event (GstBaseSink * base_sink,
GstEvent * event);
static GstFlowReturn gst_gio_base_sink_render (GstBaseSink * base_sink,
GstBuffer * buffer);
static gboolean gst_gio_base_sink_query (GstBaseSink * bsink, GstQuery * query);
static void
gst_gio_base_sink_class_init (GstGioBaseSinkClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstElementClass *gstelement_class = (GstElementClass *) klass;
GstBaseSinkClass *gstbasesink_class = (GstBaseSinkClass *) klass;
GST_DEBUG_CATEGORY_INIT (gst_gio_base_sink_debug, "gio_base_sink", 0,
"GIO base sink");
gobject_class->finalize = gst_gio_base_sink_finalize;
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&sink_factory));
gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_gio_base_sink_start);
gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_gio_base_sink_stop);
gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_gio_base_sink_unlock);
gstbasesink_class->unlock_stop =
GST_DEBUG_FUNCPTR (gst_gio_base_sink_unlock_stop);
gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_gio_base_sink_query);
gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_gio_base_sink_event);
gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_gio_base_sink_render);
}
static void
gst_gio_base_sink_init (GstGioBaseSink * sink)
{
gst_base_sink_set_sync (GST_BASE_SINK (sink), FALSE);
sink->cancel = g_cancellable_new ();
}
static void
gst_gio_base_sink_finalize (GObject * object)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (object);
if (sink->cancel) {
g_object_unref (sink->cancel);
sink->cancel = NULL;
}
if (sink->stream) {
g_object_unref (sink->stream);
sink->stream = NULL;
}
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}
static gboolean
gst_gio_base_sink_start (GstBaseSink * base_sink)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GstGioBaseSinkClass *gbsink_class = GST_GIO_BASE_SINK_GET_CLASS (sink);
sink->position = 0;
/* FIXME: This will likely block */
sink->stream = gbsink_class->get_stream (sink);
if (G_UNLIKELY (!G_IS_OUTPUT_STREAM (sink->stream))) {
GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
("No output stream provided by subclass"));
return FALSE;
} else if (G_UNLIKELY (g_output_stream_is_closed (sink->stream))) {
GST_ELEMENT_ERROR (sink, LIBRARY, FAILED, (NULL),
("Output stream is already closed"));
return FALSE;
}
GST_DEBUG_OBJECT (sink, "started sink");
return TRUE;
}
static gboolean
gst_gio_base_sink_stop (GstBaseSink * base_sink)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GstGioBaseSinkClass *klass = GST_GIO_BASE_SINK_GET_CLASS (sink);
gboolean success;
GError *err = NULL;
if (klass->close_on_stop && G_IS_OUTPUT_STREAM (sink->stream)) {
GST_DEBUG_OBJECT (sink, "closing stream");
/* FIXME: can block but unfortunately we can't use async operations
* here because they require a running main loop */
success = g_output_stream_close (sink->stream, sink->cancel, &err);
if (!success && !gst_gio_error (sink, "g_output_stream_close", &err, NULL)) {
GST_ELEMENT_WARNING (sink, RESOURCE, CLOSE, (NULL),
("gio_output_stream_close failed: %s", err->message));
g_clear_error (&err);
} else if (!success) {
GST_ELEMENT_WARNING (sink, RESOURCE, CLOSE, (NULL),
("g_output_stream_close failed"));
} else {
GST_DEBUG_OBJECT (sink, "g_outut_stream_close succeeded");
}
g_object_unref (sink->stream);
sink->stream = NULL;
} else {
success = g_output_stream_flush (sink->stream, sink->cancel, &err);
if (!success && !gst_gio_error (sink, "g_output_stream_flush", &err, NULL)) {
GST_ELEMENT_WARNING (sink, RESOURCE, CLOSE, (NULL),
("gio_output_stream_flush failed: %s", err->message));
g_clear_error (&err);
} else if (!success) {
GST_ELEMENT_WARNING (sink, RESOURCE, CLOSE, (NULL),
("g_output_stream_flush failed"));
} else {
GST_DEBUG_OBJECT (sink, "g_outut_stream_flush succeeded");
}
g_object_unref (sink->stream);
sink->stream = NULL;
}
return TRUE;
}
static gboolean
gst_gio_base_sink_unlock (GstBaseSink * base_sink)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GST_LOG_OBJECT (sink, "triggering cancellation");
g_cancellable_cancel (sink->cancel);
return TRUE;
}
static gboolean
gst_gio_base_sink_unlock_stop (GstBaseSink * base_sink)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GST_LOG_OBJECT (sink, "resetting cancellable");
g_cancellable_reset (sink->cancel);
return TRUE;
}
static gboolean
gst_gio_base_sink_event (GstBaseSink * base_sink, GstEvent * event)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
GstFlowReturn ret = GST_FLOW_OK;
if (sink->stream == NULL)
return TRUE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEGMENT:
if (G_IS_OUTPUT_STREAM (sink->stream)) {
const GstSegment *segment;
gst_event_parse_segment (event, &segment);
if (segment->format != GST_FORMAT_BYTES) {
GST_WARNING_OBJECT (sink, "ignored SEGMENT event in %s format",
gst_format_get_name (segment->format));
break;
}
if (GST_GIO_STREAM_IS_SEEKABLE (sink->stream)) {
ret = gst_gio_seek (sink, G_SEEKABLE (sink->stream), segment->start,
sink->cancel);
if (ret == GST_FLOW_OK)
sink->position = segment->start;
} else {
ret = GST_FLOW_NOT_SUPPORTED;
}
}
break;
case GST_EVENT_EOS:
case GST_EVENT_FLUSH_START:
if (G_IS_OUTPUT_STREAM (sink->stream)) {
gboolean success;
GError *err = NULL;
success = g_output_stream_flush (sink->stream, sink->cancel, &err);
if (!success && !gst_gio_error (sink, "g_output_stream_flush", &err,
&ret)) {
GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
("flush failed: %s", err->message));
g_clear_error (&err);
}
}
break;
default:
break;
}
if (ret == GST_FLOW_OK)
return GST_BASE_SINK_CLASS (parent_class)->event (base_sink, event);
else {
gst_event_unref (event);
return FALSE;
}
}
static GstFlowReturn
gst_gio_base_sink_render (GstBaseSink * base_sink, GstBuffer * buffer)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (base_sink);
gssize written;
guint8 *data;
gsize size;
gboolean success;
GError *err = NULL;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (sink->stream), GST_FLOW_ERROR);
data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
GST_LOG_OBJECT (sink,
"writing %" G_GSIZE_FORMAT " bytes to offset %" G_GUINT64_FORMAT, size,
sink->position);
written =
g_output_stream_write (sink->stream, data, size, sink->cancel, &err);
gst_buffer_unmap (buffer, data, size);
success = (written >= 0);
if (G_UNLIKELY (success && written < size)) {
/* FIXME: Can this happen? Should we handle it gracefully? gnomevfssink
* doesn't... */
GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
("Could not write to stream: (short write, only %"
G_GSSIZE_FORMAT " bytes of %" G_GSIZE_FORMAT " bytes written)",
written, size));
return GST_FLOW_ERROR;
}
if (success) {
sink->position += written;
return GST_FLOW_OK;
} else {
GstFlowReturn ret;
if (!gst_gio_error (sink, "g_output_stream_write", &err, &ret)) {
if (GST_GIO_ERROR_MATCHES (err, NO_SPACE)) {
GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
("Could not write to stream: %s", err->message));
} else {
GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
("Could not write to stream: %s", err->message));
}
g_clear_error (&err);
}
return ret;
}
}
static gboolean
gst_gio_base_sink_query (GstBaseSink * bsink, GstQuery * query)
{
GstGioBaseSink *sink = GST_GIO_BASE_SINK (bsink);
GstFormat format;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_POSITION:
gst_query_parse_position (query, &format, NULL);
switch (format) {
case GST_FORMAT_BYTES:
case GST_FORMAT_DEFAULT:
gst_query_set_position (query, GST_FORMAT_BYTES, sink->position);
return TRUE;
default:
return FALSE;
}
case GST_QUERY_FORMATS:
gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
return TRUE;
case GST_QUERY_URI:
if (GST_IS_URI_HANDLER (sink)) {
gchar *uri;
uri = gst_uri_handler_get_uri (GST_URI_HANDLER (sink));
gst_query_set_uri (query, uri);
g_free (uri);
return TRUE;
}
return FALSE;
default:
return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
}
}
|