gst-indent run on core
[platform/upstream/gstreamer.git] / gst / elements / gstfilesink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfilesink.c: 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "../gst-i18n-lib.h"
29
30 #include <gst/gst.h>
31 #include <errno.h>
32 #include "gstfilesink.h"
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37
38
39 GST_DEBUG_CATEGORY_STATIC (gst_filesink_debug);
40 #define GST_CAT_DEFAULT gst_filesink_debug
41
42 GstElementDetails gst_filesink_details = GST_ELEMENT_DETAILS ("File Sink",
43     "Sink/File",
44     "Write stream to a file",
45     "Thomas <thomas@apestaart.org>");
46
47
48 /* FileSink signals and args */
49 enum
50 {
51   /* FILL ME */
52   SIGNAL_HANDOFF,
53   LAST_SIGNAL
54 };
55
56 enum
57 {
58   ARG_0,
59   ARG_LOCATION
60 };
61
62 static const GstFormat *
63 gst_filesink_get_formats (GstPad * pad)
64 {
65   static const GstFormat formats[] = {
66     GST_FORMAT_BYTES,
67     0,
68   };
69   return formats;
70 }
71
72 static const GstQueryType *
73 gst_filesink_get_query_types (GstPad * pad)
74 {
75   static const GstQueryType types[] = {
76     GST_QUERY_TOTAL,
77     GST_QUERY_POSITION,
78     0
79   };
80   return types;
81 }
82
83 static void gst_filesink_dispose (GObject * object);
84
85 static void gst_filesink_set_property (GObject * object, guint prop_id,
86     const GValue * value, GParamSpec * pspec);
87 static void gst_filesink_get_property (GObject * object, guint prop_id,
88     GValue * value, GParamSpec * pspec);
89
90 static gboolean gst_filesink_open_file (GstFileSink * sink);
91 static void gst_filesink_close_file (GstFileSink * sink);
92
93 static gboolean gst_filesink_handle_event (GstPad * pad, GstEvent * event);
94 static gboolean gst_filesink_pad_query (GstPad * pad, GstQueryType type,
95     GstFormat * format, gint64 * value);
96 static void gst_filesink_chain (GstPad * pad, GstData * _data);
97
98 static void gst_filesink_uri_handler_init (gpointer g_iface,
99     gpointer iface_data);
100
101 static GstElementStateReturn gst_filesink_change_state (GstElement * element);
102
103 static guint gst_filesink_signals[LAST_SIGNAL] = { 0 };
104
105 static void
106 _do_init (GType filesink_type)
107 {
108   static const GInterfaceInfo urihandler_info = {
109     gst_filesink_uri_handler_init,
110     NULL,
111     NULL
112   };
113   g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
114       &urihandler_info);
115   GST_DEBUG_CATEGORY_INIT (gst_filesink_debug, "filesink", 0,
116       "filesink element");
117 }
118
119 GST_BOILERPLATE_FULL (GstFileSink, gst_filesink, GstElement, GST_TYPE_ELEMENT,
120     _do_init);
121
122
123 static void
124 gst_filesink_base_init (gpointer g_class)
125 {
126   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
127
128   gstelement_class->change_state = gst_filesink_change_state;
129   gst_element_class_set_details (gstelement_class, &gst_filesink_details);
130 }
131 static void
132 gst_filesink_class_init (GstFileSinkClass * klass)
133 {
134   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
135
136
137   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
138       g_param_spec_string ("location", "File Location",
139           "Location of the file to write", NULL, G_PARAM_READWRITE));
140
141   gst_filesink_signals[SIGNAL_HANDOFF] =
142       g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
143       G_STRUCT_OFFSET (GstFileSinkClass, handoff), NULL, NULL,
144       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
145
146   gobject_class->set_property = gst_filesink_set_property;
147   gobject_class->get_property = gst_filesink_get_property;
148   gobject_class->dispose = gst_filesink_dispose;
149 }
150 static void
151 gst_filesink_init (GstFileSink * filesink)
152 {
153   GstPad *pad;
154
155   pad = gst_pad_new ("sink", GST_PAD_SINK);
156   gst_element_add_pad (GST_ELEMENT (filesink), pad);
157   gst_pad_set_chain_function (pad, gst_filesink_chain);
158
159   GST_FLAG_SET (GST_ELEMENT (filesink), GST_ELEMENT_EVENT_AWARE);
160
161   gst_pad_set_query_function (pad, gst_filesink_pad_query);
162   gst_pad_set_query_type_function (pad, gst_filesink_get_query_types);
163   gst_pad_set_formats_function (pad, gst_filesink_get_formats);
164
165   filesink->filename = NULL;
166   filesink->file = NULL;
167 }
168 static void
169 gst_filesink_dispose (GObject * object)
170 {
171   GstFileSink *sink = GST_FILESINK (object);
172
173   G_OBJECT_CLASS (parent_class)->dispose (object);
174
175   g_free (sink->uri);
176   sink->uri = NULL;
177   g_free (sink->filename);
178   sink->filename = NULL;
179 }
180
181 static gboolean
182 gst_filesink_set_location (GstFileSink * sink, const gchar * location)
183 {
184   /* the element must be stopped or paused in order to do this */
185   if (GST_STATE (sink) > GST_STATE_PAUSED)
186     return FALSE;
187   if (GST_STATE (sink) == GST_STATE_PAUSED &&
188       GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN))
189     return FALSE;
190
191   g_free (sink->filename);
192   g_free (sink->uri);
193   if (location != NULL) {
194     sink->filename = g_strdup (location);
195     sink->uri = gst_uri_construct ("file", location);
196   } else {
197     sink->filename = NULL;
198     sink->uri = NULL;
199   }
200
201   if (GST_STATE (sink) == GST_STATE_PAUSED)
202     gst_filesink_open_file (sink);
203
204   return TRUE;
205 }
206 static void
207 gst_filesink_set_property (GObject * object, guint prop_id,
208     const GValue * value, GParamSpec * pspec)
209 {
210   GstFileSink *sink;
211
212   /* it's not null if we got it, but it might not be ours */
213   sink = GST_FILESINK (object);
214
215   switch (prop_id) {
216     case ARG_LOCATION:
217       gst_filesink_set_location (sink, g_value_get_string (value));
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224
225 static void
226 gst_filesink_get_property (GObject * object, guint prop_id, GValue * value,
227     GParamSpec * pspec)
228 {
229   GstFileSink *sink;
230
231   /* it's not null if we got it, but it might not be ours */
232   g_return_if_fail (GST_IS_FILESINK (object));
233
234   sink = GST_FILESINK (object);
235
236   switch (prop_id) {
237     case ARG_LOCATION:
238       g_value_set_string (value, sink->filename);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static gboolean
247 gst_filesink_open_file (GstFileSink * sink)
248 {
249   g_return_val_if_fail (!GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN), FALSE);
250
251   /* open the file */
252   if (sink->filename == NULL || sink->filename[0] == '\0') {
253     GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
254         (_("No file name specified for writing.")), (NULL));
255     return FALSE;
256   }
257
258   sink->file = fopen (sink->filename, "w");
259   if (sink->file == NULL) {
260     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
261         (_("Could not open file \"%s\" for writing."), sink->filename),
262         GST_ERROR_SYSTEM);
263     return FALSE;
264   }
265
266   GST_FLAG_SET (sink, GST_FILESINK_OPEN);
267
268   sink->data_written = 0;
269
270   return TRUE;
271 }
272
273 static void
274 gst_filesink_close_file (GstFileSink * sink)
275 {
276   g_return_if_fail (GST_FLAG_IS_SET (sink, GST_FILESINK_OPEN));
277
278   if (fclose (sink->file) != 0) {
279     GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
280         (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
281   } else {
282     GST_FLAG_UNSET (sink, GST_FILESINK_OPEN);
283   }
284 }
285
286 static gboolean
287 gst_filesink_pad_query (GstPad * pad, GstQueryType type,
288     GstFormat * format, gint64 * value)
289 {
290   GstFileSink *sink = GST_FILESINK (GST_PAD_PARENT (pad));
291
292   switch (type) {
293     case GST_QUERY_TOTAL:
294       switch (*format) {
295         case GST_FORMAT_BYTES:
296           if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
297             *value = sink->data_written;        /* FIXME - doesn't the kernel provide
298                                                    such a function? */
299             break;
300           }
301         default:
302           return FALSE;
303       }
304       break;
305     case GST_QUERY_POSITION:
306       switch (*format) {
307         case GST_FORMAT_BYTES:
308           if (GST_FLAG_IS_SET (GST_ELEMENT (sink), GST_FILESINK_OPEN)) {
309             *value = ftell (sink->file);
310             break;
311           }
312         default:
313           return FALSE;
314       }
315       break;
316     default:
317       return FALSE;
318   }
319
320   return TRUE;
321 }
322
323 /* handle events (search) */
324 static gboolean
325 gst_filesink_handle_event (GstPad * pad, GstEvent * event)
326 {
327   GstEventType type;
328   GstFileSink *filesink;
329
330   filesink = GST_FILESINK (gst_pad_get_parent (pad));
331
332   g_return_val_if_fail (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN), FALSE);
333
334   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
335
336   switch (type) {
337     case GST_EVENT_SEEK:
338       g_return_val_if_fail (GST_EVENT_SEEK_FORMAT (event) == GST_FORMAT_BYTES,
339           FALSE);
340
341       if (GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_FLUSH)
342         if (fflush (filesink->file))
343           GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
344               (_("Error while writing to file \"%s\"."), filesink->filename),
345               GST_ERROR_SYSTEM);
346
347       switch (GST_EVENT_SEEK_METHOD (event)) {
348         case GST_SEEK_METHOD_SET:
349           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_SET);
350           break;
351         case GST_SEEK_METHOD_CUR:
352           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_CUR);
353           break;
354         case GST_SEEK_METHOD_END:
355           fseek (filesink->file, GST_EVENT_SEEK_OFFSET (event), SEEK_END);
356           break;
357         default:
358           g_warning ("unknown seek method!");
359           break;
360       }
361       break;
362     case GST_EVENT_DISCONTINUOUS:
363     {
364       gint64 offset;
365
366       if (gst_event_discont_get_value (event, GST_FORMAT_BYTES, &offset))
367         fseek (filesink->file, offset, SEEK_SET);
368
369       gst_event_unref (event);
370       break;
371     }
372     case GST_EVENT_FLUSH:
373       if (fflush (filesink->file)) {
374         GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
375             (_("Error while writing to file \"%s\"."), filesink->filename),
376             GST_ERROR_SYSTEM);
377       }
378       break;
379     case GST_EVENT_EOS:
380       gst_filesink_close_file (filesink);
381       gst_element_set_eos (GST_ELEMENT (filesink));
382       break;
383     default:
384       gst_pad_event_default (pad, event);
385       break;
386   }
387
388   return TRUE;
389 }
390
391 /**
392  * gst_filesink_chain:
393  * @pad: the pad this filesink is connected to
394  * @buf: the buffer that has to be absorbed
395  *
396  * take the buffer from the pad and write to file if it's open
397  */
398 static void
399 gst_filesink_chain (GstPad * pad, GstData * _data)
400 {
401   GstBuffer *buf = GST_BUFFER (_data);
402   GstFileSink *filesink;
403
404   g_return_if_fail (pad != NULL);
405   g_return_if_fail (GST_IS_PAD (pad));
406   g_return_if_fail (buf != NULL);
407
408   filesink = GST_FILESINK (gst_pad_get_parent (pad));
409
410   if (GST_IS_EVENT (buf)) {
411     gst_filesink_handle_event (pad, GST_EVENT (buf));
412     return;
413   }
414
415   if (GST_FLAG_IS_SET (filesink, GST_FILESINK_OPEN)) {
416     guint bytes_written = 0, back_pending = 0;
417
418     if (ftell (filesink->file) < filesink->data_written)
419       back_pending = filesink->data_written - ftell (filesink->file);
420     while (bytes_written < GST_BUFFER_SIZE (buf)) {
421       size_t wrote = fwrite (GST_BUFFER_DATA (buf) + bytes_written, 1,
422           GST_BUFFER_SIZE (buf) - bytes_written,
423           filesink->file);
424
425       if (wrote <= 0) {
426         GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
427             (_("Error while writing to file \"%s\"."), filesink->filename),
428             ("Only %d of %d bytes written: %s",
429                 bytes_written, GST_BUFFER_SIZE (buf), strerror (errno)));
430         break;
431       }
432       bytes_written += wrote;
433     }
434
435     filesink->data_written += bytes_written - back_pending;
436   }
437
438   gst_buffer_unref (buf);
439
440   g_signal_emit (G_OBJECT (filesink),
441       gst_filesink_signals[SIGNAL_HANDOFF], 0, filesink);
442 }
443
444 static GstElementStateReturn
445 gst_filesink_change_state (GstElement * element)
446 {
447   g_return_val_if_fail (GST_IS_FILESINK (element), GST_STATE_FAILURE);
448
449   switch (GST_STATE_TRANSITION (element)) {
450     case GST_STATE_PAUSED_TO_READY:
451       if (GST_FLAG_IS_SET (element, GST_FILESINK_OPEN))
452         gst_filesink_close_file (GST_FILESINK (element));
453       break;
454
455     case GST_STATE_READY_TO_PAUSED:
456       if (!GST_FLAG_IS_SET (element, GST_FILESINK_OPEN)) {
457         if (!gst_filesink_open_file (GST_FILESINK (element)))
458           return GST_STATE_FAILURE;
459       }
460       break;
461   }
462
463   if (GST_ELEMENT_CLASS (parent_class)->change_state)
464     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
465
466   return GST_STATE_SUCCESS;
467 }
468
469 /*** GSTURIHANDLER INTERFACE *************************************************/
470
471 static guint
472 gst_filesink_uri_get_type (void)
473 {
474   return GST_URI_SINK;
475 }
476 static gchar **
477 gst_filesink_uri_get_protocols (void)
478 {
479   static gchar *protocols[] = { "file", NULL };
480   return protocols;
481 }
482 static const gchar *
483 gst_filesink_uri_get_uri (GstURIHandler * handler)
484 {
485   GstFileSink *sink = GST_FILESINK (handler);
486
487   return sink->uri;
488 }
489
490 static gboolean
491 gst_filesink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
492 {
493   gchar *protocol, *location;
494   gboolean ret;
495   GstFileSink *sink = GST_FILESINK (handler);
496
497   protocol = gst_uri_get_protocol (uri);
498   if (strcmp (protocol, "file") != 0) {
499     g_free (protocol);
500     return FALSE;
501   }
502   g_free (protocol);
503   location = gst_uri_get_location (uri);
504   ret = gst_filesink_set_location (sink, location);
505   g_free (location);
506
507   return ret;
508 }
509
510 static void
511 gst_filesink_uri_handler_init (gpointer g_iface, gpointer iface_data)
512 {
513   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
514
515   iface->get_type = gst_filesink_uri_get_type;
516   iface->get_protocols = gst_filesink_uri_get_protocols;
517   iface->get_uri = gst_filesink_uri_get_uri;
518   iface->set_uri = gst_filesink_uri_set_uri;
519 }