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>
32 //#define DEBUG_ENABLED
33 #include "gstpipefilter.h"
36 GstElementDetails gst_pipefilter_details = {
39 "Pass data without modification",
41 "Erik Walthinsen <omega@cse.ogi.edu>",
46 /* Pipefilter signals and args */
58 static void gst_pipefilter_class_init(GstPipefilterClass *klass);
59 static void gst_pipefilter_init(GstPipefilter *pipefilter);
60 static void gst_pipefilter_set_arg(GtkObject *object,GtkArg *arg,guint id);
61 static void gst_pipefilter_get_arg(GtkObject *object,GtkArg *arg,guint id);
63 void gst_pipefilter_chain(GstPad *pad,GstBuffer *buf);
65 static GstElementStateReturn gst_pipefilter_change_state(GstElement *element);
67 static GstElementClass *parent_class = NULL;
68 //static guint gst_pipefilter_signals[LAST_SIGNAL] = { 0 };
71 gst_pipefilter_get_type(void) {
72 static GtkType pipefilter_type = 0;
74 if (!pipefilter_type) {
75 static const GtkTypeInfo pipefilter_info = {
77 sizeof(GstPipefilter),
78 sizeof(GstPipefilterClass),
79 (GtkClassInitFunc)gst_pipefilter_class_init,
80 (GtkObjectInitFunc)gst_pipefilter_init,
81 (GtkArgSetFunc)gst_pipefilter_set_arg,
82 (GtkArgGetFunc)gst_pipefilter_get_arg,
83 (GtkClassInitFunc)NULL,
85 pipefilter_type = gtk_type_unique(GST_TYPE_ELEMENT,&pipefilter_info);
87 return pipefilter_type;
90 static void gst_pipefilter_class_init(GstPipefilterClass *klass) {
91 GtkObjectClass *gtkobject_class;
92 GstElementClass *gstelement_class;
94 gtkobject_class = (GtkObjectClass*)klass;
95 gstelement_class = (GstElementClass*)klass;
97 parent_class = gtk_type_class(GST_TYPE_ELEMENT);
99 gstelement_class->change_state = gst_pipefilter_change_state;
101 gtk_object_add_arg_type("GstPipefilter::command", GTK_TYPE_STRING,
102 GTK_ARG_READWRITE, ARG_COMMAND);
104 gtkobject_class->set_arg = gst_pipefilter_set_arg;
105 gtkobject_class->get_arg = gst_pipefilter_get_arg;
108 static void gst_pipefilter_init(GstPipefilter *pipefilter) {
109 pipefilter->sinkpad = gst_pad_new("sink",GST_PAD_SINK);
110 gst_element_add_pad(GST_ELEMENT(pipefilter),pipefilter->sinkpad);
111 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);
115 pipefilter->command = NULL;
116 pipefilter->curoffset = 0;
117 pipefilter->bytes_per_read = 4096;
121 static gboolean gst_pipefilter_read_and_push(GstPipefilter *pipefilter) {
125 /* create the buffer */
126 // FIXME: should eventually use a bufferpool for this
127 newbuf = gst_buffer_new();
128 g_return_val_if_fail(newbuf, FALSE);
130 /* allocate the space for the buffer data */
131 GST_BUFFER_DATA(newbuf) = g_malloc(pipefilter->bytes_per_read);
132 g_return_val_if_fail(GST_BUFFER_DATA(newbuf) != NULL, FALSE);
134 /* read it in from the file */
135 GST_DEBUG (0,"attemting to read %ld bytes\n", pipefilter->bytes_per_read);
136 readbytes = read(pipefilter->fdout[0],GST_BUFFER_DATA(newbuf),pipefilter->bytes_per_read);
137 GST_DEBUG (0,"read %ld bytes\n", readbytes);
139 if (errno == EAGAIN) {
140 GST_DEBUG (0,"no input yet\n");
141 gst_buffer_unref(newbuf);
146 gst_element_error(GST_ELEMENT(pipefilter),"reading");
150 if (readbytes == 0) {
151 gst_buffer_unref(newbuf);
154 /* if we didn't get as many bytes as we asked for, we're at EOF */
155 if (readbytes < pipefilter->bytes_per_read)
156 GST_BUFFER_FLAG_SET(newbuf,GST_BUFFER_EOS);
157 GST_BUFFER_OFFSET(newbuf) = pipefilter->curoffset;
158 GST_BUFFER_SIZE(newbuf) = readbytes;
159 pipefilter->curoffset += readbytes;
161 /* we're done, push the buffer off now */
162 gst_pad_push(pipefilter->srcpad,newbuf);
165 void gst_pipefilter_chain(GstPad *pad,GstBuffer *buf) {
166 GstPipefilter *pipefilter;
171 g_return_if_fail(pad != NULL);
172 g_return_if_fail(GST_IS_PAD(pad));
173 g_return_if_fail(buf != NULL);
175 pipefilter = GST_PIPEFILTER (gst_pad_get_parent (pad));
177 while (gst_pipefilter_read_and_push(pipefilter));
179 data = GST_BUFFER_DATA(buf);
180 size = GST_BUFFER_SIZE(buf);
182 GST_DEBUG (0,"attemting to write %ld bytes\n", size);
183 writebytes = write(pipefilter->fdin[1],data,size);
184 GST_DEBUG (0,"written %ld bytes\n", writebytes);
185 if (writebytes < 0) {
187 gst_element_error(GST_ELEMENT(pipefilter),"writing");
190 gst_buffer_unref(buf);
192 while (gst_pipefilter_read_and_push(pipefilter));
195 static void gst_pipefilter_set_arg(GtkObject *object,GtkArg *arg,guint id) {
196 GstPipefilter *pipefilter;
198 /* it's not null if we got it, but it might not be ours */
199 g_return_if_fail(GST_IS_PIPEFILTER(object));
200 pipefilter = GST_PIPEFILTER(object);
204 pipefilter->orig_command = g_strdup(GTK_VALUE_STRING(*arg));
205 pipefilter->command = g_strsplit(GTK_VALUE_STRING(*arg), " ", 0);
212 static void gst_pipefilter_get_arg(GtkObject *object,GtkArg *arg,guint id) {
213 GstPipefilter *pipefilter;
215 /* it's not null if we got it, but it might not be ours */
216 g_return_if_fail(GST_IS_PIPEFILTER(object));
217 pipefilter = GST_PIPEFILTER(object);
221 GTK_VALUE_STRING(*arg) = pipefilter->orig_command;
224 arg->type = GTK_TYPE_INVALID;
229 /* open the file, necessary to go to RUNNING state */
230 static gboolean gst_pipefilter_open_file(GstPipefilter *src) {
231 g_return_val_if_fail(!GST_FLAG_IS_SET(src,GST_PIPEFILTER_OPEN), FALSE);
236 if (fcntl(src->fdout[0], F_SETFL, O_NONBLOCK) < 0) {
238 gst_element_error(GST_ELEMENT(src),"fcntl");
242 if((src->childpid = fork()) == -1)
245 gst_element_error(GST_ELEMENT(src),"forking");
249 if(src->childpid == 0)
252 dup2(src->fdin[0], STDIN_FILENO); /* set the childs input stream */
253 dup2(src->fdout[1], STDOUT_FILENO); /* set the childs output stream */
254 execvp(src->command[0], &src->command[0]);
255 // will only reach if error
257 gst_element_error(GST_ELEMENT(src),"starting child process");
262 GST_FLAG_SET(src,GST_PIPEFILTER_OPEN);
267 static void gst_pipefilter_close_file(GstPipefilter *src) {
268 g_return_if_fail(GST_FLAG_IS_SET(src,GST_PIPEFILTER_OPEN));
271 close(src->fdout[0]);
272 close(src->fdout[1]);
276 /* zero out a lot of our state */
280 GST_FLAG_UNSET(src,GST_PIPEFILTER_OPEN);
283 static GstElementStateReturn gst_pipefilter_change_state(GstElement *element) {
284 g_return_val_if_fail(GST_IS_PIPEFILTER(element), FALSE);
286 /* if going down into NULL state, close the file if it's open */
287 if (GST_STATE_PENDING(element) == GST_STATE_NULL) {
288 if (GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN))
289 gst_pipefilter_close_file(GST_PIPEFILTER(element));
290 /* otherwise (READY or higher) we need to open the file */
292 if (!GST_FLAG_IS_SET(element,GST_PIPEFILTER_OPEN)) {
293 if (!gst_pipefilter_open_file(GST_PIPEFILTER(element)))
294 return GST_STATE_FAILURE;
298 if (GST_ELEMENT_CLASS(parent_class)->change_state)
299 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
300 return GST_STATE_SUCCESS;