43fc167a9c2cf9c3a3b73d3becaeb073215b70ab
[platform/upstream/gst-plugins-good.git] / sys / oss / gstosssink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstosssink.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  * SECTION:element-osssink
25  * @short_description: output sound using OSS
26  *
27  * <refsect2>
28  * <para>
29  * This element lets you output sound using the Open Sound System (OSS).
30  * </para>
31  * <para>
32  * Note that you should almost always use generic audio conversion elements
33  * like audioconvert and audioresample in front of an audiosink to make sure
34  * your pipeline works under all circumstances (those conversion elements will
35  * act in passthrough-mode if no conversion is necessary).
36  * </para>
37  * <title>Example pipelines</title>
38  * <para>
39  * <programlisting>
40  * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! osssink
41  * </programlisting>
42  * will output a sine wave (continuous beep sound) to your sound card (with
43  * a very low volume as precaution).
44  * </para>
45  * <para>
46  * <programlisting>
47  * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! osssink
48  * </programlisting>
49  * will play an Ogg/Vorbis audio file and output it using the Open Sound System.
50  * </para>
51  * </refsect2>
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57 #include <sys/ioctl.h>
58 #include <fcntl.h>
59 #include <errno.h>
60 #include <unistd.h>
61 #include <string.h>
62
63 #ifdef HAVE_OSS_INCLUDE_IN_SYS
64 # include <sys/soundcard.h>
65 #else
66 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
67 #  include <soundcard.h>
68 # else
69 #  ifdef HAVE_OSS_INCLUDE_IN_MACHINE
70 #   include <machine/soundcard.h>
71 #  else
72 #   error "What to include?"
73 #  endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
74 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
75 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
76
77 #include "common.h"
78 #include "gstosssink.h"
79
80 #include <gst/gst-i18n-plugin.h>
81
82 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
83 #define GST_CAT_DEFAULT oss_debug
84
85 /* elementfactory information */
86 static const GstElementDetails gst_oss_sink_details =
87 GST_ELEMENT_DETAILS ("Audio Sink (OSS)",
88     "Sink/Audio",
89     "Output to a sound card via OSS",
90     "Erik Walthinsen <omega@cse.ogi.edu>, "
91     "Wim Taymans <wim.taymans@chello.be>");
92
93 static void gst_oss_sink_base_init (gpointer g_class);
94 static void gst_oss_sink_class_init (GstOssSinkClass * klass);
95 static void gst_oss_sink_init (GstOssSink * osssink);
96
97 static void gst_oss_sink_dispose (GObject * object);
98 static void gst_oss_sink_finalise (GObject * object);
99
100 static void gst_oss_sink_get_property (GObject * object, guint prop_id,
101     GValue * value, GParamSpec * pspec);
102 static void gst_oss_sink_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104
105 static GstCaps *gst_oss_sink_getcaps (GstBaseSink * bsink);
106
107 static gboolean gst_oss_sink_open (GstAudioSink * asink);
108 static gboolean gst_oss_sink_close (GstAudioSink * asink);
109 static gboolean gst_oss_sink_prepare (GstAudioSink * asink,
110     GstRingBufferSpec * spec);
111 static gboolean gst_oss_sink_unprepare (GstAudioSink * asink);
112 static guint gst_oss_sink_write (GstAudioSink * asink, gpointer data,
113     guint length);
114 static guint gst_oss_sink_delay (GstAudioSink * asink);
115 static void gst_oss_sink_reset (GstAudioSink * asink);
116
117 /* OssSink signals and args */
118 enum
119 {
120   LAST_SIGNAL
121 };
122
123 #define DEFAULT_DEVICE  "/dev/dsp"
124 enum
125 {
126   PROP_0,
127   PROP_DEVICE,
128 };
129
130 static GstStaticPadTemplate osssink_sink_factory =
131     GST_STATIC_PAD_TEMPLATE ("sink",
132     GST_PAD_SINK,
133     GST_PAD_ALWAYS,
134     GST_STATIC_CAPS ("audio/x-raw-int, "
135         "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
136         "signed = (boolean) { TRUE, FALSE }, "
137         "width = (int) 16, "
138         "depth = (int) 16, "
139         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
140         "audio/x-raw-int, "
141         "signed = (boolean) { TRUE, FALSE }, "
142         "width = (int) 8, "
143         "depth = (int) 8, "
144         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
145     );
146
147 static GstElementClass *parent_class = NULL;
148
149 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
150
151 GType
152 gst_oss_sink_get_type (void)
153 {
154   static GType osssink_type = 0;
155
156   if (!osssink_type) {
157     static const GTypeInfo osssink_info = {
158       sizeof (GstOssSinkClass),
159       gst_oss_sink_base_init,
160       NULL,
161       (GClassInitFunc) gst_oss_sink_class_init,
162       NULL,
163       NULL,
164       sizeof (GstOssSink),
165       0,
166       (GInstanceInitFunc) gst_oss_sink_init,
167     };
168
169     osssink_type =
170         g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
171         &osssink_info, 0);
172   }
173
174   return osssink_type;
175 }
176
177 static void
178 gst_oss_sink_dispose (GObject * object)
179 {
180   GstOssSink *osssink = GST_OSSSINK (object);
181
182   if (osssink->probed_caps) {
183     gst_caps_unref (osssink->probed_caps);
184     osssink->probed_caps = NULL;
185   }
186
187   G_OBJECT_CLASS (parent_class)->dispose (object);
188 }
189
190 static void
191 gst_oss_sink_base_init (gpointer g_class)
192 {
193   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
194
195   gst_element_class_set_details (element_class, &gst_oss_sink_details);
196
197   gst_element_class_add_pad_template (element_class,
198       gst_static_pad_template_get (&osssink_sink_factory));
199 }
200 static void
201 gst_oss_sink_class_init (GstOssSinkClass * klass)
202 {
203   GObjectClass *gobject_class;
204   GstElementClass *gstelement_class;
205   GstBaseSinkClass *gstbasesink_class;
206   GstBaseAudioSinkClass *gstbaseaudiosink_class;
207   GstAudioSinkClass *gstaudiosink_class;
208
209   gobject_class = (GObjectClass *) klass;
210   gstelement_class = (GstElementClass *) klass;
211   gstbasesink_class = (GstBaseSinkClass *) klass;
212   gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
213   gstaudiosink_class = (GstAudioSinkClass *) klass;
214
215   parent_class = g_type_class_peek_parent (klass);
216
217   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_sink_dispose);
218   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss_sink_finalise);
219   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_sink_get_property);
220   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_sink_set_property);
221
222   g_object_class_install_property (gobject_class, PROP_DEVICE,
223       g_param_spec_string ("device", "Device",
224           "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
225
226   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
227
228   gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
229   gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
230   gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
231   gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
232   gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
233   gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
234   gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
235 }
236
237 static void
238 gst_oss_sink_init (GstOssSink * osssink)
239 {
240   const gchar *device;
241
242   GST_DEBUG_OBJECT (osssink, "initializing osssink");
243
244   device = g_getenv ("AUDIODEV");
245   if (device == NULL)
246     device = DEFAULT_DEVICE;
247   osssink->device = g_strdup (device);
248   osssink->fd = -1;
249 }
250
251 static void
252 gst_oss_sink_finalise (GObject * object)
253 {
254   GstOssSink *osssink = GST_OSSSINK (object);
255
256   g_free (osssink->device);
257
258   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
259 }
260
261 static void
262 gst_oss_sink_set_property (GObject * object, guint prop_id,
263     const GValue * value, GParamSpec * pspec)
264 {
265   GstOssSink *sink;
266
267   sink = GST_OSSSINK (object);
268
269   switch (prop_id) {
270     case PROP_DEVICE:
271       g_free (sink->device);
272       sink->device = g_value_dup_string (value);
273       if (sink->probed_caps) {
274         gst_caps_unref (sink->probed_caps);
275         sink->probed_caps = NULL;
276       }
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281   }
282 }
283
284 static void
285 gst_oss_sink_get_property (GObject * object, guint prop_id,
286     GValue * value, GParamSpec * pspec)
287 {
288   GstOssSink *sink;
289
290   sink = GST_OSSSINK (object);
291
292   switch (prop_id) {
293     case PROP_DEVICE:
294       g_value_set_string (value, sink->device);
295       break;
296     default:
297       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298       break;
299   }
300 }
301
302 static GstCaps *
303 gst_oss_sink_getcaps (GstBaseSink * bsink)
304 {
305   GstOssSink *osssink;
306   GstCaps *caps;
307
308   osssink = GST_OSSSINK (bsink);
309
310   if (osssink->fd == -1) {
311     caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
312             (bsink)));
313   } else if (osssink->probed_caps) {
314     caps = gst_caps_copy (osssink->probed_caps);
315   } else {
316     caps = gst_oss_helper_probe_caps (osssink->fd);
317     if (caps && !gst_caps_is_empty (caps)) {
318       osssink->probed_caps = gst_caps_copy (caps);
319     }
320   }
321
322   return caps;
323 }
324
325 static gint
326 ilog2 (gint x)
327 {
328   /* well... hacker's delight explains... */
329   x = x | (x >> 1);
330   x = x | (x >> 2);
331   x = x | (x >> 4);
332   x = x | (x >> 8);
333   x = x | (x >> 16);
334   x = x - ((x >> 1) & 0x55555555);
335   x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
336   x = (x + (x >> 4)) & 0x0f0f0f0f;
337   x = x + (x >> 8);
338   x = x + (x >> 16);
339   return (x & 0x0000003f) - 1;
340 }
341
342 static gint
343 gst_oss_sink_get_format (GstBufferFormat fmt)
344 {
345   gint result;
346
347   switch (fmt) {
348     case GST_MU_LAW:
349       result = AFMT_MU_LAW;
350       break;
351     case GST_A_LAW:
352       result = AFMT_A_LAW;
353       break;
354     case GST_IMA_ADPCM:
355       result = AFMT_IMA_ADPCM;
356       break;
357     case GST_U8:
358       result = AFMT_U8;
359       break;
360     case GST_S16_LE:
361       result = AFMT_S16_LE;
362       break;
363     case GST_S16_BE:
364       result = AFMT_S16_BE;
365       break;
366     case GST_S8:
367       result = AFMT_S8;
368       break;
369     case GST_U16_LE:
370       result = AFMT_U16_LE;
371       break;
372     case GST_U16_BE:
373       result = AFMT_U16_BE;
374       break;
375     case GST_MPEG:
376       result = AFMT_MPEG;
377       break;
378     default:
379       result = 0;
380       break;
381   }
382   return result;
383 }
384
385 static gboolean
386 gst_oss_sink_open (GstAudioSink * asink)
387 {
388   GstOssSink *oss;
389   int mode;
390
391   oss = GST_OSSSINK (asink);
392
393   mode = O_WRONLY;
394   mode |= O_NONBLOCK;
395
396   oss->fd = open (oss->device, mode, 0);
397   if (oss->fd == -1) {
398     switch (errno) {
399       case EBUSY:
400         goto busy;
401       case EACCES:
402         goto no_permission;
403       default:
404         goto open_failed;
405     }
406   }
407
408   return TRUE;
409
410   /* ERRORS */
411 busy:
412   {
413     GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
414         (_("Could not open audio device for playback. "
415                 "Device is being used by another application.")), (NULL));
416     return FALSE;
417   }
418 no_permission:
419   {
420     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
421         (_("Could not open audio device for playback. "
422                 "You don't have permission to open the device.")),
423         GST_ERROR_SYSTEM);
424     return FALSE;
425   }
426 open_failed:
427   {
428     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
429         (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
430     return FALSE;
431   }
432 }
433
434 static gboolean
435 gst_oss_sink_close (GstAudioSink * asink)
436 {
437   close (GST_OSSSINK (asink)->fd);
438   GST_OSSSINK (asink)->fd = -1;
439   return TRUE;
440 }
441
442 static gboolean
443 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
444 {
445   GstOssSink *oss;
446   struct audio_buf_info info;
447   int mode;
448   int tmp;
449
450   oss = GST_OSSSINK (asink);
451
452   /* we opened non-blocking so that we can detect if the device is available
453    * without hanging forever. We now want to remove the non-blocking flag. */
454   mode = fcntl (oss->fd, F_GETFL);
455   mode &= ~O_NONBLOCK;
456   if (fcntl (oss->fd, F_SETFL, mode) == -1) {
457     /* some drivers do no support unsetting the non-blocking flag, try to
458      * close/open the device then. This is racy but we error out properly. */
459     gst_oss_sink_close (asink);
460     if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
461       goto non_block;
462   }
463
464   tmp = gst_oss_sink_get_format (spec->format);
465   if (tmp == 0)
466     goto wrong_format;
467
468   if (spec->width != 16 && spec->width != 8)
469     goto dodgy_width;
470
471   SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
472   if (spec->channels == 2)
473     SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
474   SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
475   SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
476
477   tmp = ilog2 (spec->segsize);
478   tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
479   GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
480       spec->segsize, spec->segtotal, tmp);
481
482   SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
483   GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
484
485   spec->segsize = info.fragsize;
486   spec->segtotal = info.fragstotal;
487
488   spec->bytes_per_sample = (spec->width / 8) * spec->channels;
489   oss->bytes_per_sample = (spec->width / 8) * spec->channels;
490
491   GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
492       spec->segsize, spec->segtotal, tmp);
493
494   return TRUE;
495
496   /* ERRORS */
497 non_block:
498   {
499     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
500         ("Unable to set device %s in non blocking mode: %s",
501             oss->device, g_strerror (errno)));
502     return FALSE;
503   }
504 wrong_format:
505   {
506     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
507         ("Unable to get format %d", spec->format));
508     return FALSE;
509   }
510 dodgy_width:
511   {
512     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
513         ("unexpected width %d", spec->width));
514     return FALSE;
515   }
516 }
517
518 static gboolean
519 gst_oss_sink_unprepare (GstAudioSink * asink)
520 {
521   /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
522
523   if (!gst_oss_sink_close (asink))
524     goto couldnt_close;
525
526   if (!gst_oss_sink_open (asink))
527     goto couldnt_reopen;
528
529   return TRUE;
530
531   /* ERRORS */
532 couldnt_close:
533   {
534     GST_DEBUG_OBJECT (asink, "Could not close the audio device");
535     return FALSE;
536   }
537 couldnt_reopen:
538   {
539     GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
540     return FALSE;
541   }
542 }
543
544 static guint
545 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
546 {
547   return write (GST_OSSSINK (asink)->fd, data, length);
548 }
549
550 static guint
551 gst_oss_sink_delay (GstAudioSink * asink)
552 {
553   GstOssSink *oss;
554   gint delay = 0;
555   gint ret;
556
557   oss = GST_OSSSINK (asink);
558
559 #ifdef SNDCTL_DSP_GETODELAY
560   ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
561 #else
562   ret = -1;
563 #endif
564   if (ret < 0) {
565     audio_buf_info info;
566
567     ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
568
569     delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
570   }
571   return delay / oss->bytes_per_sample;
572 }
573
574 static void
575 gst_oss_sink_reset (GstAudioSink * asink)
576 {
577   /* There's nothing we can do here really: OSS can't handle access to the
578    * same device/fd from multiple threads and might deadlock or blow up in
579    * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */
580 }