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         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
122     );
123
124 static GstElementClass *parent_class = NULL;
125
126 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
127
128 GType
129 gst_oss_sink_get_type (void)
130 {
131   static GType osssink_type = 0;
132
133   if (!osssink_type) {
134     static const GTypeInfo osssink_info = {
135       sizeof (GstOssSinkClass),
136       gst_oss_sink_base_init,
137       NULL,
138       (GClassInitFunc) gst_oss_sink_class_init,
139       NULL,
140       NULL,
141       sizeof (GstOssSink),
142       0,
143       (GInstanceInitFunc) gst_oss_sink_init,
144     };
145
146     osssink_type =
147         g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
148         &osssink_info, 0);
149   }
150
151   return osssink_type;
152 }
153
154 static void
155 gst_oss_sink_dispose (GObject * object)
156 {
157   GstOssSink *osssink = GST_OSSSINK (object);
158
159   if (osssink->probed_caps) {
160     gst_caps_unref (osssink->probed_caps);
161     osssink->probed_caps = NULL;
162   }
163
164   G_OBJECT_CLASS (parent_class)->dispose (object);
165 }
166
167 static void
168 gst_oss_sink_base_init (gpointer g_class)
169 {
170   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
171
172   gst_element_class_set_details_simple (element_class, "Audio Sink (OSS)",
173       "Sink/Audio",
174       "Output to a sound card via OSS",
175       "Erik Walthinsen <omega@cse.ogi.edu>, "
176       "Wim Taymans <wim.taymans@chello.be>");
177
178   gst_element_class_add_pad_template (element_class,
179       gst_static_pad_template_get (&osssink_sink_factory));
180 }
181
182 static void
183 gst_oss_sink_class_init (GstOssSinkClass * klass)
184 {
185   GObjectClass *gobject_class;
186   GstBaseSinkClass *gstbasesink_class;
187   GstAudioSinkClass *gstaudiosink_class;
188
189   gobject_class = (GObjectClass *) klass;
190   gstbasesink_class = (GstBaseSinkClass *) klass;
191   gstaudiosink_class = (GstAudioSinkClass *) klass;
192
193   parent_class = g_type_class_peek_parent (klass);
194
195   gobject_class->dispose = gst_oss_sink_dispose;
196   gobject_class->finalize = gst_oss_sink_finalise;
197   gobject_class->get_property = gst_oss_sink_get_property;
198   gobject_class->set_property = gst_oss_sink_set_property;
199
200   g_object_class_install_property (gobject_class, PROP_DEVICE,
201       g_param_spec_string ("device", "Device",
202           "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
203           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
206
207   gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
208   gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
209   gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
210   gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
211   gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
212   gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
213   gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
214 }
215
216 static void
217 gst_oss_sink_init (GstOssSink * osssink)
218 {
219   const gchar *device;
220
221   GST_DEBUG_OBJECT (osssink, "initializing osssink");
222
223   device = g_getenv ("AUDIODEV");
224   if (device == NULL)
225     device = DEFAULT_DEVICE;
226   osssink->device = g_strdup (device);
227   osssink->fd = -1;
228 }
229
230 static void
231 gst_oss_sink_finalise (GObject * object)
232 {
233   GstOssSink *osssink = GST_OSSSINK (object);
234
235   g_free (osssink->device);
236
237   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
238 }
239
240 static void
241 gst_oss_sink_set_property (GObject * object, guint prop_id,
242     const GValue * value, GParamSpec * pspec)
243 {
244   GstOssSink *sink;
245
246   sink = GST_OSSSINK (object);
247
248   switch (prop_id) {
249     case PROP_DEVICE:
250       g_free (sink->device);
251       sink->device = g_value_dup_string (value);
252       if (sink->probed_caps) {
253         gst_caps_unref (sink->probed_caps);
254         sink->probed_caps = NULL;
255       }
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262
263 static void
264 gst_oss_sink_get_property (GObject * object, guint prop_id,
265     GValue * value, GParamSpec * pspec)
266 {
267   GstOssSink *sink;
268
269   sink = GST_OSSSINK (object);
270
271   switch (prop_id) {
272     case PROP_DEVICE:
273       g_value_set_string (value, sink->device);
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278   }
279 }
280
281 static GstCaps *
282 gst_oss_sink_getcaps (GstBaseSink * bsink)
283 {
284   GstOssSink *osssink;
285   GstCaps *caps;
286
287   osssink = GST_OSSSINK (bsink);
288
289   if (osssink->fd == -1) {
290     caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
291             (bsink)));
292   } else if (osssink->probed_caps) {
293     caps = gst_caps_copy (osssink->probed_caps);
294   } else {
295     caps = gst_oss_helper_probe_caps (osssink->fd);
296     if (caps && !gst_caps_is_empty (caps)) {
297       osssink->probed_caps = gst_caps_copy (caps);
298     }
299   }
300
301   return caps;
302 }
303
304 static gint
305 ilog2 (gint x)
306 {
307   /* well... hacker's delight explains... */
308   x = x | (x >> 1);
309   x = x | (x >> 2);
310   x = x | (x >> 4);
311   x = x | (x >> 8);
312   x = x | (x >> 16);
313   x = x - ((x >> 1) & 0x55555555);
314   x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
315   x = (x + (x >> 4)) & 0x0f0f0f0f;
316   x = x + (x >> 8);
317   x = x + (x >> 16);
318   return (x & 0x0000003f) - 1;
319 }
320
321 static gint
322 gst_oss_sink_get_format (GstBufferFormat fmt)
323 {
324   gint result;
325
326   switch (fmt) {
327     case GST_MU_LAW:
328       result = AFMT_MU_LAW;
329       break;
330     case GST_A_LAW:
331       result = AFMT_A_LAW;
332       break;
333     case GST_IMA_ADPCM:
334       result = AFMT_IMA_ADPCM;
335       break;
336     case GST_U8:
337       result = AFMT_U8;
338       break;
339     case GST_S16_LE:
340       result = AFMT_S16_LE;
341       break;
342     case GST_S16_BE:
343       result = AFMT_S16_BE;
344       break;
345     case GST_S8:
346       result = AFMT_S8;
347       break;
348     case GST_U16_LE:
349       result = AFMT_U16_LE;
350       break;
351     case GST_U16_BE:
352       result = AFMT_U16_BE;
353       break;
354     case GST_MPEG:
355       result = AFMT_MPEG;
356       break;
357     default:
358       result = 0;
359       break;
360   }
361   return result;
362 }
363
364 static gboolean
365 gst_oss_sink_open (GstAudioSink * asink)
366 {
367   GstOssSink *oss;
368   int mode;
369
370   oss = GST_OSSSINK (asink);
371
372   mode = O_WRONLY;
373   mode |= O_NONBLOCK;
374
375   oss->fd = open (oss->device, mode, 0);
376   if (oss->fd == -1) {
377     switch (errno) {
378       case EBUSY:
379         goto busy;
380       case EACCES:
381         goto no_permission;
382       default:
383         goto open_failed;
384     }
385   }
386
387   return TRUE;
388
389   /* ERRORS */
390 busy:
391   {
392     GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
393         (_("Could not open audio device for playback. "
394                 "Device is being used by another application.")), (NULL));
395     return FALSE;
396   }
397 no_permission:
398   {
399     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
400         (_("Could not open audio device for playback. "
401                 "You don't have permission to open the device.")),
402         GST_ERROR_SYSTEM);
403     return FALSE;
404   }
405 open_failed:
406   {
407     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
408         (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
409     return FALSE;
410   }
411 }
412
413 static gboolean
414 gst_oss_sink_close (GstAudioSink * asink)
415 {
416   close (GST_OSSSINK (asink)->fd);
417   GST_OSSSINK (asink)->fd = -1;
418   return TRUE;
419 }
420
421 static gboolean
422 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
423 {
424   GstOssSink *oss;
425   struct audio_buf_info info;
426   int mode;
427   int tmp;
428
429   oss = GST_OSSSINK (asink);
430
431   /* we opened non-blocking so that we can detect if the device is available
432    * without hanging forever. We now want to remove the non-blocking flag. */
433   mode = fcntl (oss->fd, F_GETFL);
434   mode &= ~O_NONBLOCK;
435   if (fcntl (oss->fd, F_SETFL, mode) == -1) {
436     /* some drivers do no support unsetting the non-blocking flag, try to
437      * close/open the device then. This is racy but we error out properly. */
438     gst_oss_sink_close (asink);
439     if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
440       goto non_block;
441   }
442
443   tmp = gst_oss_sink_get_format (spec->format);
444   if (tmp == 0)
445     goto wrong_format;
446
447   if (spec->width != 16 && spec->width != 8)
448     goto dodgy_width;
449
450   SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
451   if (spec->channels == 2)
452     SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
453   SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
454   SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
455
456   tmp = ilog2 (spec->segsize);
457   tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
458   GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
459       spec->segsize, spec->segtotal, tmp);
460
461   SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
462   GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
463
464   spec->segsize = info.fragsize;
465   spec->segtotal = info.fragstotal;
466
467   spec->bytes_per_sample = (spec->width / 8) * spec->channels;
468   oss->bytes_per_sample = (spec->width / 8) * spec->channels;
469
470   GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
471       spec->segsize, spec->segtotal, tmp);
472
473   return TRUE;
474
475   /* ERRORS */
476 non_block:
477   {
478     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
479         ("Unable to set device %s in non blocking mode: %s",
480             oss->device, g_strerror (errno)));
481     return FALSE;
482   }
483 wrong_format:
484   {
485     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
486         ("Unable to get format %d", spec->format));
487     return FALSE;
488   }
489 dodgy_width:
490   {
491     GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
492         ("unexpected width %d", spec->width));
493     return FALSE;
494   }
495 }
496
497 static gboolean
498 gst_oss_sink_unprepare (GstAudioSink * asink)
499 {
500   /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
501
502   if (!gst_oss_sink_close (asink))
503     goto couldnt_close;
504
505   if (!gst_oss_sink_open (asink))
506     goto couldnt_reopen;
507
508   return TRUE;
509
510   /* ERRORS */
511 couldnt_close:
512   {
513     GST_DEBUG_OBJECT (asink, "Could not close the audio device");
514     return FALSE;
515   }
516 couldnt_reopen:
517   {
518     GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
519     return FALSE;
520   }
521 }
522
523 static guint
524 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
525 {
526   return write (GST_OSSSINK (asink)->fd, data, length);
527 }
528
529 static guint
530 gst_oss_sink_delay (GstAudioSink * asink)
531 {
532   GstOssSink *oss;
533   gint delay = 0;
534   gint ret;
535
536   oss = GST_OSSSINK (asink);
537
538 #ifdef SNDCTL_DSP_GETODELAY
539   ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
540 #else
541   ret = -1;
542 #endif
543   if (ret < 0) {
544     audio_buf_info info;
545
546     ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
547
548     delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
549   }
550   return delay / oss->bytes_per_sample;
551 }
552
553 static void
554 gst_oss_sink_reset (GstAudioSink * asink)
555 {
556   /* There's nothing we can do here really: OSS can't handle access to the
557    * same device/fd from multiple threads and might deadlock or blow up in
558    * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */
559 }