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>
37 #include "../gst-i18n-lib.h"
38 #include "gstpipefilter.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_pipefilter_debug);
41 #define GST_CAT_DEFAULT gst_pipefilter_debug
43 GstElementDetails gst_pipefilter_details = GST_ELEMENT_DETAILS (
46 "Interoperate with an external program using stdin and stdout",
47 "Erik Walthinsen <omega@cse.ogi.edu>, "
48 "Wim Taymans <wim.taymans@chello.be>"
52 /* Pipefilter signals and args */
64 #define _do_init(bla) \
65 GST_DEBUG_CATEGORY_INIT (gst_pipefilter_debug, "pipefilter", 0, "pipefilter element");
67 GST_BOILERPLATE_FULL (GstPipefilter, gst_pipefilter, GstElement, GST_TYPE_ELEMENT, _do_init);
69 static void gst_pipefilter_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
70 static void gst_pipefilter_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
72 static GstData* gst_pipefilter_get (GstPad *pad);
73 static void gst_pipefilter_chain (GstPad *pad, GstData *_data);
74 static gboolean gst_pipefilter_handle_event (GstPad *pad, GstEvent *event);
76 static GstElementStateReturn gst_pipefilter_change_state (GstElement *element);
79 gst_pipefilter_base_init (gpointer g_class)
81 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
83 gst_element_class_set_details (gstelement_class, &gst_pipefilter_details);
86 gst_pipefilter_class_init (GstPipefilterClass *klass)
88 GObjectClass *gobject_class;
89 GstElementClass *gstelement_class;
91 gobject_class = (GObjectClass*)klass;
92 gstelement_class = (GstElementClass*)klass;
95 gobject_class->set_property = gst_pipefilter_set_property;
96 gobject_class->get_property = gst_pipefilter_get_property;
98 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_COMMAND,
99 g_param_spec_string("command","command","command",
100 NULL, G_PARAM_READWRITE)); /* CHECKME */
102 gstelement_class->change_state = gst_pipefilter_change_state;
106 gst_pipefilter_init (GstPipefilter *pipefilter)
108 GST_FLAG_SET (pipefilter, GST_ELEMENT_DECOUPLED);
110 pipefilter->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
111 gst_element_add_pad (GST_ELEMENT (pipefilter), pipefilter->sinkpad);
112 gst_pad_set_chain_function (pipefilter->sinkpad, gst_pipefilter_chain);
114 pipefilter->srcpad = gst_pad_new ("src", GST_PAD_SRC);
115 gst_element_add_pad (GST_ELEMENT (pipefilter), pipefilter->srcpad);
116 gst_pad_set_get_function (pipefilter->srcpad, gst_pipefilter_get);
118 pipefilter->command = NULL;
119 pipefilter->curoffset = 0;
120 pipefilter->bytes_per_read = 4096;
125 gst_pipefilter_handle_event (GstPad *pad, GstEvent *event)
127 GstPipefilter *pipefilter;
129 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
131 GST_DEBUG ("pipefilter: %s received event", GST_ELEMENT_NAME (pipefilter));
133 switch (GST_EVENT_TYPE (event)) {
135 if (close (pipefilter->fdin[1]) < 0)
137 if (close (pipefilter->fdout[0]) < 0)
144 gst_pad_event_default (pad, event);
150 gst_pipefilter_get (GstPad *pad)
152 GstPipefilter *pipefilter;
156 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
158 /* create the buffer */
159 /* FIXME: should eventually use a bufferpool for this */
160 newbuf = gst_buffer_new();
161 g_return_val_if_fail(newbuf, NULL);
163 /* allocate the space for the buffer data */
164 GST_BUFFER_DATA(newbuf) = g_malloc(pipefilter->bytes_per_read);
165 g_return_val_if_fail(GST_BUFFER_DATA(newbuf) != NULL, NULL);
167 /* read it in from the file */
168 GST_DEBUG ("attemting to read %ld bytes", pipefilter->bytes_per_read);
169 readbytes = read(pipefilter->fdout[0], GST_BUFFER_DATA(newbuf), pipefilter->bytes_per_read);
170 GST_DEBUG ("read %ld bytes", readbytes);
172 GST_ELEMENT_ERROR (pipefilter, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
175 /* if we didn't get as many bytes as we asked for, we're at EOF */
176 if (readbytes == 0) {
177 return GST_DATA (gst_event_new (GST_EVENT_EOS));
181 GST_BUFFER_OFFSET(newbuf) = pipefilter->curoffset;
182 GST_BUFFER_SIZE(newbuf) = readbytes;
183 pipefilter->curoffset += readbytes;
185 return GST_DATA (newbuf);
189 gst_pipefilter_chain (GstPad *pad,GstData *_data)
192 GstPipefilter *pipefilter;
197 g_return_if_fail(pad != NULL);
198 g_return_if_fail(GST_IS_PAD(pad));
200 if (GST_IS_EVENT (_data)) {
201 gst_pipefilter_handle_event (pad, GST_EVENT (_data));
205 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
207 buf = GST_BUFFER (_data);
208 data = GST_BUFFER_DATA(buf);
209 size = GST_BUFFER_SIZE(buf);
211 GST_DEBUG ("attemting to write %ld bytes", size);
212 writebytes = write(pipefilter->fdin[1],data,size);
213 GST_DEBUG ("written %ld bytes", writebytes);
214 if (writebytes < 0) {
215 GST_ELEMENT_ERROR (pipefilter, RESOURCE, WRITE, (NULL), GST_ERROR_SYSTEM);
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)
270 GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL), GST_ERROR_SYSTEM);
274 if(src->childpid == 0)
277 close(src->fdout[0]);
279 dup2(src->fdin[0], STDIN_FILENO); /* set the childs input stream */
280 dup2(src->fdout[1], STDOUT_FILENO); /* set the childs output stream */
281 execvp(src->command[0], &src->command[0]);
282 /* will only be reached if execvp has an error */
283 GST_ELEMENT_ERROR (src, RESOURCE, TOO_LAZY, (NULL), GST_ERROR_SYSTEM);
289 close(src->fdout[1]);
292 GST_FLAG_SET(src,GST_PIPEFILTER_OPEN);
298 gst_pipefilter_close_file (GstPipefilter *src)
300 g_return_if_fail(GST_FLAG_IS_SET(src,GST_PIPEFILTER_OPEN));
303 close(src->fdout[0]);
304 close(src->fdout[1]);
308 /* zero out a lot of our state */
312 GST_FLAG_UNSET(src,GST_PIPEFILTER_OPEN);
315 static GstElementStateReturn
316 gst_pipefilter_change_state (GstElement *element)
318 g_return_val_if_fail(GST_IS_PIPEFILTER(element), FALSE);
320 /* if going down into NULL state, close the file if it's open */
321 if (GST_STATE_PENDING(element) == GST_STATE_NULL) {
322 if (GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN))
323 gst_pipefilter_close_file(GST_PIPEFILTER(element));
324 /* otherwise (READY or higher) we need to open the file */
326 if (!GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN)) {
327 if (!gst_pipefilter_open_file(GST_PIPEFILTER(element)))
328 return GST_STATE_FAILURE;
332 if (GST_ELEMENT_CLASS(parent_class)->change_state)
333 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
334 return GST_STATE_SUCCESS;