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