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