2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
25 #include <sys/types.h>
36 #include "gstpipefilter.h"
38 GST_DEBUG_CATEGORY_STATIC (gst_pipefilter_debug);
39 #define GST_CAT_DEFAULT gst_pipefilter_debug
41 GstElementDetails gst_pipefilter_details = GST_ELEMENT_DETAILS (
44 "Interoperate with an external program using stdin and stdout",
45 "Erik Walthinsen <omega@cse.ogi.edu>, "
46 "Wim Taymans <wim.taymans@chello.be>"
50 /* Pipefilter signals and args */
62 #define _do_init(bla) \
63 GST_DEBUG_CATEGORY_INIT (gst_pipefilter_debug, "pipefilter", 0, "pipefilter element");
65 GST_BOILERPLATE_FULL (GstPipefilter, gst_pipefilter, GstElement, GST_TYPE_ELEMENT, _do_init);
67 static void gst_pipefilter_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
68 static void gst_pipefilter_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
70 static GstData* gst_pipefilter_get (GstPad *pad);
71 static void gst_pipefilter_chain (GstPad *pad, GstData *_data);
72 static gboolean gst_pipefilter_handle_event (GstPad *pad, GstEvent *event);
74 static GstElementStateReturn gst_pipefilter_change_state (GstElement *element);
77 gst_pipefilter_base_init (gpointer g_class)
79 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
81 gst_element_class_set_details (gstelement_class, &gst_pipefilter_details);
84 gst_pipefilter_class_init (GstPipefilterClass *klass)
86 GObjectClass *gobject_class;
87 GstElementClass *gstelement_class;
89 gobject_class = (GObjectClass*)klass;
90 gstelement_class = (GstElementClass*)klass;
93 gobject_class->set_property = gst_pipefilter_set_property;
94 gobject_class->get_property = gst_pipefilter_get_property;
96 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_COMMAND,
97 g_param_spec_string("command","command","command",
98 NULL, G_PARAM_READWRITE)); /* CHECKME */
100 gstelement_class->change_state = gst_pipefilter_change_state;
104 gst_pipefilter_init (GstPipefilter *pipefilter)
106 GST_FLAG_SET (pipefilter, GST_ELEMENT_DECOUPLED);
108 pipefilter->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
109 gst_element_add_pad (GST_ELEMENT (pipefilter), pipefilter->sinkpad);
110 gst_pad_set_chain_function (pipefilter->sinkpad, gst_pipefilter_chain);
112 pipefilter->srcpad = gst_pad_new ("src", GST_PAD_SRC);
113 gst_element_add_pad (GST_ELEMENT (pipefilter), pipefilter->srcpad);
114 gst_pad_set_get_function (pipefilter->srcpad, gst_pipefilter_get);
116 pipefilter->command = NULL;
117 pipefilter->curoffset = 0;
118 pipefilter->bytes_per_read = 4096;
123 gst_pipefilter_handle_event (GstPad *pad, GstEvent *event)
125 GstPipefilter *pipefilter;
127 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
129 GST_DEBUG ("pipefilter: %s received event", GST_ELEMENT_NAME (pipefilter));
131 switch (GST_EVENT_TYPE (event)) {
133 if (close (pipefilter->fdin[1]) < 0)
135 if (close (pipefilter->fdout[0]) < 0)
142 gst_pad_event_default (pad, event);
148 gst_pipefilter_get (GstPad *pad)
150 GstPipefilter *pipefilter;
154 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
156 /* create the buffer */
157 /* FIXME: should eventually use a bufferpool for this */
158 newbuf = gst_buffer_new();
159 g_return_val_if_fail(newbuf, NULL);
161 /* allocate the space for the buffer data */
162 GST_BUFFER_DATA(newbuf) = g_malloc(pipefilter->bytes_per_read);
163 g_return_val_if_fail(GST_BUFFER_DATA(newbuf) != NULL, NULL);
165 /* read it in from the file */
166 GST_DEBUG ("attemting to read %ld bytes", pipefilter->bytes_per_read);
167 readbytes = read(pipefilter->fdout[0], GST_BUFFER_DATA(newbuf), pipefilter->bytes_per_read);
168 GST_DEBUG ("read %ld bytes", readbytes);
171 gst_element_error(GST_ELEMENT(pipefilter),"reading");
174 /* if we didn't get as many bytes as we asked for, we're at EOF */
175 if (readbytes == 0) {
176 return GST_DATA (gst_event_new (GST_EVENT_EOS));
180 GST_BUFFER_OFFSET(newbuf) = pipefilter->curoffset;
181 GST_BUFFER_SIZE(newbuf) = readbytes;
182 pipefilter->curoffset += readbytes;
184 return GST_DATA (newbuf);
188 gst_pipefilter_chain (GstPad *pad,GstData *_data)
191 GstPipefilter *pipefilter;
196 g_return_if_fail(pad != NULL);
197 g_return_if_fail(GST_IS_PAD(pad));
199 if (GST_IS_EVENT (_data)) {
200 gst_pipefilter_handle_event (pad, GST_EVENT (_data));
204 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
206 buf = GST_BUFFER (_data);
207 data = GST_BUFFER_DATA(buf);
208 size = GST_BUFFER_SIZE(buf);
210 GST_DEBUG ("attemting to write %ld bytes", size);
211 writebytes = write(pipefilter->fdin[1],data,size);
212 GST_DEBUG ("written %ld bytes", writebytes);
213 if (writebytes < 0) {
215 gst_element_error(GST_ELEMENT(pipefilter),"writing");
218 gst_buffer_unref(buf);
222 gst_pipefilter_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
224 GstPipefilter *pipefilter;
226 /* it's not null if we got it, but it might not be ours */
227 g_return_if_fail(GST_IS_PIPEFILTER(object));
228 pipefilter = GST_PIPEFILTER(object);
232 pipefilter->orig_command = g_strdup(g_value_get_string (value));
233 pipefilter->command = g_strsplit(g_value_get_string (value), " ", 0);
241 gst_pipefilter_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
243 GstPipefilter *pipefilter;
245 /* it's not null if we got it, but it might not be ours */
246 g_return_if_fail(GST_IS_PIPEFILTER(object));
247 pipefilter = GST_PIPEFILTER(object);
251 g_value_set_string (value, pipefilter->orig_command);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 /* open the file, necessary to go to RUNNING state */
261 gst_pipefilter_open_file (GstPipefilter *src)
263 g_return_val_if_fail(!GST_FLAG_IS_SET(src,GST_PIPEFILTER_OPEN), FALSE);
268 if((src->childpid = fork()) == -1)
271 gst_element_error(GST_ELEMENT(src),"forking");
275 if(src->childpid == 0)
278 close(src->fdout[0]);
280 dup2(src->fdin[0], STDIN_FILENO); /* set the childs input stream */
281 dup2(src->fdout[1], STDOUT_FILENO); /* set the childs output stream */
282 execvp(src->command[0], &src->command[0]);
283 /* will only reach if error */
285 gst_element_error(GST_ELEMENT(src),"starting child process");
291 close(src->fdout[1]);
294 GST_FLAG_SET(src,GST_PIPEFILTER_OPEN);
300 gst_pipefilter_close_file (GstPipefilter *src)
302 g_return_if_fail(GST_FLAG_IS_SET(src,GST_PIPEFILTER_OPEN));
305 close(src->fdout[0]);
306 close(src->fdout[1]);
310 /* zero out a lot of our state */
314 GST_FLAG_UNSET(src,GST_PIPEFILTER_OPEN);
317 static GstElementStateReturn
318 gst_pipefilter_change_state (GstElement *element)
320 g_return_val_if_fail(GST_IS_PIPEFILTER(element), FALSE);
322 /* if going down into NULL state, close the file if it's open */
323 if (GST_STATE_PENDING(element) == GST_STATE_NULL) {
324 if (GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN))
325 gst_pipefilter_close_file(GST_PIPEFILTER(element));
326 /* otherwise (READY or higher) we need to open the file */
328 if (!GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN)) {
329 if (!gst_pipefilter_open_file(GST_PIPEFILTER(element)))
330 return GST_STATE_FAILURE;
334 if (GST_ELEMENT_CLASS(parent_class)->change_state)
335 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
336 return GST_STATE_SUCCESS;