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