filter newlines out of GST_DEBUG statements to reflect new core behavior fixes to...
[platform/upstream/gstreamer.git] / sys / oss / gstossgst.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstossgst.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 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <fcntl.h>
28 #include <sys/soundcard.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <stdlib.h>
32
33 #include "gstossgst.h"
34
35 #include "gstosshelper.h"
36
37 static GstElementDetails gst_ossgst_details = {  
38   "Audio Wrapper (OSS)",
39   "Src/Audio",
40   "Hijacks /dev/dsp to get the output of OSS apps into GStreamer",
41   VERSION,
42   "Wim Taymans <wim.taymans@chello.be>",
43   "(C) 2001",
44 };
45
46 static void                     gst_ossgst_class_init           (GstOssGstClass *klass);
47 static void                     gst_ossgst_init         (GstOssGst *ossgst);
48
49 static GstElementStateReturn    gst_ossgst_change_state (GstElement *element);
50
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);
53
54 static GstBuffer*               gst_ossgst_get                  (GstPad *pad);
55
56 /* OssGst signals and args */
57 enum {
58   LAST_SIGNAL
59 };
60
61 enum {
62   ARG_0,
63   ARG_MUTE,
64   ARG_PROGRAM,
65   /* FILL ME */
66 };
67
68 static GstPadTemplate*
69 ossgst_src_factory (void)
70 {
71   return
72     gst_padtemplate_new (
73         "src",
74         GST_PAD_SRC,
75         GST_PAD_ALWAYS,
76         gst_caps_new (
77           "ossgst_src",
78           "audio/raw",
79           gst_props_new (
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)
86                             ),
87               "width",      GST_PROPS_LIST (
88                               GST_PROPS_INT (8),
89                               GST_PROPS_INT (16)
90                             ),
91               "depth",      GST_PROPS_LIST (
92                               GST_PROPS_INT (8),
93                               GST_PROPS_INT (16)
94                             ),
95               "rate",       GST_PROPS_INT_RANGE (8000, 48000),
96               "channels",   GST_PROPS_INT_RANGE (1, 2),
97               NULL)),
98         NULL);
99 }
100
101
102 static GstElementClass *parent_class = NULL;
103 static GstPadTemplate *gst_ossgst_src_template;
104
105 static gchar *plugin_dir = NULL;
106
107 GType
108 gst_ossgst_get_type (void) 
109 {
110   static GType ossgst_type = 0;
111
112   if (!ossgst_type) {
113     static const GTypeInfo ossgst_info = {
114       sizeof(GstOssGstClass),
115       NULL,
116       NULL,
117       (GClassInitFunc)gst_ossgst_class_init,
118       NULL,
119       NULL,
120       sizeof(GstOssGst),
121       0,
122       (GInstanceInitFunc)gst_ossgst_init,
123     };
124     ossgst_type = g_type_register_static (GST_TYPE_ELEMENT, "GstOssGst", &ossgst_info, 0);
125   }
126
127   return ossgst_type;
128 }
129
130 static void
131 gst_ossgst_class_init (GstOssGstClass *klass) 
132 {
133   GObjectClass *gobject_class;
134   GstElementClass *gstelement_class;
135
136   gobject_class = (GObjectClass*)klass;
137   gstelement_class = (GstElementClass*)klass;
138
139   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
140
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 */
147
148   gobject_class->set_property = gst_ossgst_set_property;
149   gobject_class->get_property = gst_ossgst_get_property;
150
151   gstelement_class->change_state = gst_ossgst_change_state;
152 }
153
154 static void 
155 gst_ossgst_init (GstOssGst *ossgst) 
156 {
157   ossgst->srcpad = gst_pad_new_from_template (gst_ossgst_src_template, "src");
158   gst_element_add_pad (GST_ELEMENT (ossgst), ossgst->srcpad);
159
160   gst_pad_set_get_function (ossgst->srcpad, gst_ossgst_get);
161
162   ossgst->command = NULL;
163 }
164
165 static GstCaps* 
166 gst_ossgst_format_to_caps (gint format, gint stereo, gint rate) 
167 {
168   GstCaps *caps = NULL;
169   gint law = 0;
170   gulong endianness = G_BYTE_ORDER;
171   gboolean is_signed = TRUE;
172   gint width = 16;
173   gboolean supported = TRUE;
174
175   GST_DEBUG (0, "have format 0x%08x %d %d", format, stereo, rate); 
176
177   switch (format) {
178     case AFMT_MU_LAW:
179       law = 1;
180       break;
181     case AFMT_A_LAW:
182       law = 2;
183       break;
184     case AFMT_U8:
185       width = 8;
186       is_signed = FALSE;
187       break;
188     case AFMT_S16_LE:
189       width = 16;
190       endianness = G_LITTLE_ENDIAN;
191       is_signed = TRUE;
192       break;
193     case AFMT_S16_BE:
194       endianness = G_BIG_ENDIAN;
195       width = 16;
196       is_signed = TRUE;
197       break;
198     case AFMT_S8:
199       width = 8;
200       is_signed = TRUE;
201       break;
202     case AFMT_U16_LE:
203       width = 16;
204       endianness = G_LITTLE_ENDIAN;
205       is_signed = FALSE;
206       break;
207     case AFMT_U16_BE:
208       width = 16;
209       endianness = G_BIG_ENDIAN;
210       is_signed = FALSE;
211       break;
212     case AFMT_IMA_ADPCM:
213     case AFMT_MPEG:
214 #ifdef AFMT_AC3
215     case AFMT_AC3:
216 #endif
217     default:
218       supported = FALSE;
219       break;
220   }
221
222   if (supported) {
223     caps = gst_caps_new (
224                   "ossgst_caps",
225                   "audio/raw",
226                   gst_props_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),
235                             NULL));
236   }
237   else {
238     g_warning ("gstossgst: program tried to use unsupported format %x\n", format);
239   }
240
241   return caps;
242 }
243
244 static GstBuffer* 
245 gst_ossgst_get (GstPad *pad) 
246 {
247   GstOssGst *ossgst;
248   GstBuffer *buf = NULL;
249   command cmd;
250   gboolean have_data = FALSE;
251
252   g_return_val_if_fail (pad != NULL, NULL);
253   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
254
255   /* this has to be an audio buffer */
256   ossgst = GST_OSSGST (gst_pad_get_parent (pad));
257
258   while (!have_data) {
259     /* read the command */
260     read (ossgst->fdout[0], &cmd, sizeof (command));
261
262     switch (cmd.id) { 
263       case CMD_DATA:
264         buf = gst_buffer_new ();
265         GST_BUFFER_SIZE (buf) = cmd.cmd.length;
266         GST_BUFFER_DATA (buf) = g_malloc (GST_BUFFER_SIZE (buf));
267
268         GST_BUFFER_SIZE (buf) = read (ossgst->fdout[0], GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
269         have_data = TRUE;
270         break;
271       case CMD_FORMAT:
272         {
273           GstCaps *caps;
274
275           caps = gst_ossgst_format_to_caps (cmd.cmd.format.format, 
276                                             cmd.cmd.format.stereo, 
277                                             cmd.cmd.format.rate); 
278
279           gst_pad_try_set_caps (ossgst->srcpad, caps);
280         }
281         break;
282       default:
283         break;
284     }
285   }
286
287   return buf;
288 }
289
290 static void 
291 gst_ossgst_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
292 {
293   GstOssGst *ossgst;
294
295   /* it's not null if we got it, but it might not be ours */
296   g_return_if_fail (GST_IS_OSSGST (object));
297   
298   ossgst = GST_OSSGST (object);
299
300   switch (prop_id) {
301     case ARG_MUTE:
302       ossgst->mute = g_value_get_boolean (value);
303       break;
304     case ARG_PROGRAM:
305       if (ossgst->command)
306         g_free (ossgst->command);
307       ossgst->command = g_strdup (g_value_get_string (value));
308       break;
309     default:
310       break;
311   }
312 }
313
314 static void 
315 gst_ossgst_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
316 {
317   GstOssGst *ossgst;
318
319   /* it's not null if we got it, but it might not be ours */
320   g_return_if_fail (GST_IS_OSSGST (object));
321   
322   ossgst = GST_OSSGST (object);
323
324   switch (prop_id) {
325     case ARG_MUTE:
326       g_value_set_boolean (value, ossgst->mute);
327       break;
328     case ARG_PROGRAM:
329       g_value_set_string (value, ossgst->command);
330       break;
331     default:
332       break;
333   }
334 }
335
336 static gboolean 
337 gst_ossgst_spawn_process (GstOssGst *ossgst) 
338 {
339   static gchar *ld_preload;
340
341   pipe(ossgst->fdin);
342   pipe(ossgst->fdout);
343
344   GST_DEBUG (0, "about to fork");
345
346   if((ossgst->childpid = fork()) == -1)
347   {
348     perror("fork");
349     gst_element_error(GST_ELEMENT(ossgst),"forking");
350     return FALSE;
351   }
352   GST_DEBUG (0,"forked %d", ossgst->childpid);
353
354   if(ossgst->childpid == 0)
355   {
356     gchar **args;
357
358     GST_DEBUG (0, "fork command %d", ossgst->childpid);
359
360     ld_preload = getenv ("LD_PRELOAD");
361
362     if (ld_preload == NULL) {
363       ld_preload = "";
364     }
365
366     ld_preload = g_strconcat (ld_preload, " ", plugin_dir, G_DIR_SEPARATOR_S, 
367                     "libgstosshelper.so", NULL);
368
369     setenv ("LD_PRELOAD", ld_preload, TRUE);
370
371     /* child */
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 */
374     
375     /* split the arguments  */
376     args = g_strsplit (ossgst->command, " ", 0);
377
378     execvp(args[0], args);
379
380     /* will only reach if error */
381     perror("exec");
382     gst_element_error(GST_ELEMENT(ossgst),"starting child process");
383     return FALSE;
384
385   }
386   GST_FLAG_SET(ossgst,GST_OSSGST_OPEN);
387
388   return TRUE;
389 }
390
391 static gboolean 
392 gst_ossgst_kill_process (GstOssGst *ossgst) 
393 {
394   return TRUE;
395 }
396
397 static GstElementStateReturn 
398 gst_ossgst_change_state (GstElement *element) 
399 {
400   g_return_val_if_fail (GST_IS_OSSGST (element), FALSE);
401
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));
405   } else {
406     if (!GST_FLAG_IS_SET (element, GST_OSSGST_OPEN)) {
407       if (!gst_ossgst_spawn_process (GST_OSSGST (element))) {
408         return GST_STATE_FAILURE;
409       }
410     }
411   }
412       
413   if (GST_ELEMENT_CLASS (parent_class)->change_state)
414     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
415   return GST_STATE_SUCCESS;
416 }
417
418 gboolean 
419 gst_ossgst_factory_init (GstPlugin *plugin) 
420
421   GstElementFactory *factory;
422   gchar **path;
423   gint i =0;
424
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);
428   while (path[i]) {
429     i++;
430     if (path[i] == NULL) {
431       g_free (path[i-1]);
432       path[i-1] = NULL;
433     }
434   }
435   plugin_dir = g_strjoinv (G_DIR_SEPARATOR_S, path);
436   g_strfreev (path);
437
438   factory = gst_elementfactory_new ("ossgst", GST_TYPE_OSSGST, &gst_ossgst_details);
439   g_return_val_if_fail (factory != NULL, FALSE);
440
441   gst_ossgst_src_template = ossgst_src_factory ();
442   gst_elementfactory_add_padtemplate (factory, gst_ossgst_src_template);
443
444   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
445
446   return TRUE;
447 }
448