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