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