2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@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.
24 #include <sys/types.h>
26 #include <sys/ioctl.h>
28 #include <sys/soundcard.h>
33 #include "gstossgst.h"
35 #include "gstosshelper.h"
37 static GstElementDetails gst_ossgst_details = {
38 "Audio Wrapper (OSS)",
40 "Hijacks /dev/dsp to get the output of OSS apps into GStreamer",
42 "Wim Taymans <wim.taymans@chello.be>",
46 static void gst_ossgst_class_init (GstOssGstClass *klass);
47 static void gst_ossgst_init (GstOssGst *ossgst);
49 static GstElementStateReturn gst_ossgst_change_state (GstElement *element);
51 static void gst_ossgst_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
52 static void gst_ossgst_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
54 static GstBuffer* gst_ossgst_get (GstPad *pad);
56 /* OssGst signals and args */
68 static GstPadTemplate*
69 ossgst_src_factory (void)
80 "format", GST_PROPS_STRING ("int"),
81 "law", GST_PROPS_INT (0),
82 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
83 "signed", GST_PROPS_LIST (
84 GST_PROPS_BOOLEAN (FALSE),
85 GST_PROPS_BOOLEAN (TRUE)
87 "width", GST_PROPS_LIST (
91 "depth", GST_PROPS_LIST (
95 "rate", GST_PROPS_INT_RANGE (8000, 48000),
96 "channels", GST_PROPS_INT_RANGE (1, 2),
102 static GstElementClass *parent_class = NULL;
103 static GstPadTemplate *gst_ossgst_src_template;
105 static gchar *plugin_dir = NULL;
108 gst_ossgst_get_type (void)
110 static GType ossgst_type = 0;
113 static const GTypeInfo ossgst_info = {
114 sizeof(GstOssGstClass),
117 (GClassInitFunc)gst_ossgst_class_init,
122 (GInstanceInitFunc)gst_ossgst_init,
124 ossgst_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssGst", &ossgst_info, 0);
131 gst_ossgst_class_init (GstOssGstClass *klass)
133 GObjectClass *gobject_class;
134 GstElementClass *gstelement_class;
136 gobject_class = (GObjectClass*)klass;
137 gstelement_class = (GstElementClass*)klass;
139 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
141 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
142 g_param_spec_boolean("mute","mute","mute",
143 TRUE,G_PARAM_READWRITE)); /* CHECKME */
144 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PROGRAM,
145 g_param_spec_string("command","command","command",
146 NULL, G_PARAM_READWRITE)); /* CHECKME */
148 gobject_class->set_property = gst_ossgst_set_property;
149 gobject_class->get_property = gst_ossgst_get_property;
151 gstelement_class->change_state = gst_ossgst_change_state;
155 gst_ossgst_init (GstOssGst *ossgst)
157 ossgst->srcpad = gst_pad_new_from_template (gst_ossgst_src_template, "src");
158 gst_element_add_pad (GST_ELEMENT (ossgst), ossgst->srcpad);
160 gst_pad_set_get_function (ossgst->srcpad, gst_ossgst_get);
162 ossgst->command = NULL;
166 gst_ossgst_format_to_caps (gint format, gint stereo, gint rate)
168 GstCaps *caps = NULL;
170 gulong endianness = G_BYTE_ORDER;
171 gboolean is_signed = TRUE;
173 gboolean supported = TRUE;
175 GST_DEBUG (0, "have format 0x%08x %d %d\n", format, stereo, rate);
190 endianness = G_LITTLE_ENDIAN;
194 endianness = G_BIG_ENDIAN;
204 endianness = G_LITTLE_ENDIAN;
209 endianness = G_BIG_ENDIAN;
223 caps = gst_caps_new (
227 "format", GST_PROPS_STRING ("int"),
228 "law", GST_PROPS_INT (law),
229 "endianness", GST_PROPS_INT (endianness),
230 "signed", GST_PROPS_BOOLEAN (is_signed),
231 "width", GST_PROPS_INT (width),
232 "depth", GST_PROPS_INT (width),
233 "rate", GST_PROPS_INT (rate),
234 "channels", GST_PROPS_INT (stereo?2:1),
238 g_warning ("gstossgst: program tried to use unsupported format %x\n", format);
245 gst_ossgst_get (GstPad *pad)
248 GstBuffer *buf = NULL;
250 gboolean have_data = FALSE;
252 g_return_val_if_fail (pad != NULL, NULL);
253 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
255 /* this has to be an audio buffer */
256 ossgst = GST_OSSGST (gst_pad_get_parent (pad));
259 /* read the command */
260 read (ossgst->fdout[0], &cmd, sizeof (command));
264 buf = gst_buffer_new ();
265 GST_BUFFER_SIZE (buf) = cmd.cmd.length;
266 GST_BUFFER_DATA (buf) = g_malloc (GST_BUFFER_SIZE (buf));
268 GST_BUFFER_SIZE (buf) = read (ossgst->fdout[0], GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
275 caps = gst_ossgst_format_to_caps (cmd.cmd.format.format,
276 cmd.cmd.format.stereo,
277 cmd.cmd.format.rate);
279 gst_pad_try_set_caps (ossgst->srcpad, caps);
291 gst_ossgst_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
295 /* it's not null if we got it, but it might not be ours */
296 g_return_if_fail (GST_IS_OSSGST (object));
298 ossgst = GST_OSSGST (object);
302 ossgst->mute = g_value_get_boolean (value);
306 g_free (ossgst->command);
307 ossgst->command = g_strdup (g_value_get_string (value));
315 gst_ossgst_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
319 /* it's not null if we got it, but it might not be ours */
320 g_return_if_fail (GST_IS_OSSGST (object));
322 ossgst = GST_OSSGST (object);
326 g_value_set_boolean (value, ossgst->mute);
329 g_value_set_string (value, ossgst->command);
337 gst_ossgst_spawn_process (GstOssGst *ossgst)
339 static gchar *ld_preload;
344 GST_DEBUG (0, "about to fork\n");
346 if((ossgst->childpid = fork()) == -1)
349 gst_element_error(GST_ELEMENT(ossgst),"forking");
352 GST_DEBUG (0,"forked %d\n", ossgst->childpid);
354 if(ossgst->childpid == 0)
358 GST_DEBUG (0, "fork command %d\n", ossgst->childpid);
360 ld_preload = getenv ("LD_PRELOAD");
362 if (ld_preload == NULL) {
366 ld_preload = g_strconcat (ld_preload, " ", plugin_dir, G_DIR_SEPARATOR_S,
367 "libgstosshelper.so", NULL);
369 setenv ("LD_PRELOAD", ld_preload, TRUE);
372 dup2(ossgst->fdin[0], HELPER_MAGIC_IN); /* set the childs input stream */
373 dup2(ossgst->fdout[1], HELPER_MAGIC_OUT); /* set the childs output stream */
375 /* split the arguments */
376 args = g_strsplit (ossgst->command, " ", 0);
378 execvp(args[0], args);
380 /* will only reach if error */
382 gst_element_error(GST_ELEMENT(ossgst),"starting child process");
386 GST_FLAG_SET(ossgst,GST_OSSGST_OPEN);
392 gst_ossgst_kill_process (GstOssGst *ossgst)
397 static GstElementStateReturn
398 gst_ossgst_change_state (GstElement *element)
400 g_return_val_if_fail (GST_IS_OSSGST (element), FALSE);
402 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
403 if (GST_FLAG_IS_SET (element, GST_OSSGST_OPEN))
404 gst_ossgst_kill_process (GST_OSSGST (element));
406 if (!GST_FLAG_IS_SET (element, GST_OSSGST_OPEN)) {
407 if (!gst_ossgst_spawn_process (GST_OSSGST (element))) {
408 return GST_STATE_FAILURE;
413 if (GST_ELEMENT_CLASS (parent_class)->change_state)
414 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
415 return GST_STATE_SUCCESS;
419 gst_ossgst_factory_init (GstPlugin *plugin)
421 GstElementFactory *factory;
425 /* get the path of this plugin, we assume the helper progam lives in the */
426 /* same directory. */
427 path = g_strsplit (plugin->filename, G_DIR_SEPARATOR_S, 0);
430 if (path[i] == NULL) {
435 plugin_dir = g_strjoinv (G_DIR_SEPARATOR_S, path);
438 factory = gst_elementfactory_new ("ossgst", GST_TYPE_OSSGST, &gst_ossgst_details);
439 g_return_val_if_fail (factory != NULL, FALSE);
441 gst_ossgst_src_template = ossgst_src_factory ();
442 gst_elementfactory_add_padtemplate (factory, gst_ossgst_src_template);
444 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));