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