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