Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / sys / oss4 / oss4-source.c
1 /* GStreamer OSS4 audio source
2  * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-oss4src
22  *
23  * This element lets you record sound using the Open Sound System (OSS)
24  * version 4.
25  * 
26  * <refsect2>
27  * <title>Example pipelines</title>
28  * |[
29  * gst-launch -v oss4src ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
30  * ]| will record sound from your sound card using OSS4 and encode it to an
31  * Ogg/Vorbis file (this will only work if your mixer settings are right
32  * and the right inputs areenabled etc.)
33  * </refsect2>
34  *
35  * Since: 0.10.7
36  */
37
38 /* FIXME: make sure we're not doing ioctls from the app thread (e.g. via the
39  * mixer interface) while recording */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/ioctl.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <string.h>
52
53 #include <gst/interfaces/mixer.h>
54 #include <gst/gst-i18n-plugin.h>
55
56 #define NO_LEGACY_MIXER
57 #include "oss4-audio.h"
58 #include "oss4-source.h"
59 #include "oss4-property-probe.h"
60 #include "oss4-soundcard.h"
61
62 #define GST_OSS4_SOURCE_IS_OPEN(src)  (GST_OSS4_SOURCE(src)->fd != -1)
63
64 GST_DEBUG_CATEGORY_EXTERN (oss4src_debug);
65 #define GST_CAT_DEFAULT oss4src_debug
66
67 #define DEFAULT_DEVICE       NULL
68 #define DEFAULT_DEVICE_NAME  NULL
69
70 enum
71 {
72   PROP_0,
73   PROP_DEVICE,
74   PROP_DEVICE_NAME
75 };
76
77 static void gst_oss4_source_init_interfaces (GType type);
78
79 GST_BOILERPLATE_FULL (GstOss4Source, gst_oss4_source, GstAudioSrc,
80     GST_TYPE_AUDIO_SRC, gst_oss4_source_init_interfaces);
81
82 static void gst_oss4_source_get_property (GObject * object, guint prop_id,
83     GValue * value, GParamSpec * pspec);
84 static void gst_oss4_source_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86
87 static void gst_oss4_source_dispose (GObject * object);
88 static void gst_oss4_source_finalize (GstOss4Source * osssrc);
89
90 static GstCaps *gst_oss4_source_getcaps (GstBaseSrc * bsrc);
91
92 static gboolean gst_oss4_source_open (GstAudioSrc * asrc,
93     gboolean silent_errors);
94 static gboolean gst_oss4_source_open_func (GstAudioSrc * asrc);
95 static gboolean gst_oss4_source_close (GstAudioSrc * asrc);
96 static gboolean gst_oss4_source_prepare (GstAudioSrc * asrc,
97     GstRingBufferSpec * spec);
98 static gboolean gst_oss4_source_unprepare (GstAudioSrc * asrc);
99 static guint gst_oss4_source_read (GstAudioSrc * asrc, gpointer data,
100     guint length);
101 static guint gst_oss4_source_delay (GstAudioSrc * asrc);
102 static void gst_oss4_source_reset (GstAudioSrc * asrc);
103
104 static void
105 gst_oss4_source_base_init (gpointer g_class)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108   GstPadTemplate *templ;
109
110   gst_element_class_set_details_simple (element_class,
111       "OSS v4 Audio Source", "Source/Audio",
112       "Capture from a sound card via OSS version 4",
113       "Tim-Philipp Müller <tim centricular net>");
114
115   templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
116       gst_oss4_audio_get_template_caps ());
117   gst_element_class_add_pad_template (element_class, templ);
118   gst_object_unref (templ);
119 }
120
121 static void
122 gst_oss4_source_class_init (GstOss4SourceClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GstBaseSrcClass *gstbasesrc_class;
126   GstAudioSrcClass *gstaudiosrc_class;
127
128   gobject_class = (GObjectClass *) klass;
129   gstbasesrc_class = (GstBaseSrcClass *) klass;
130   gstaudiosrc_class = (GstAudioSrcClass *) klass;
131
132   gobject_class->dispose = gst_oss4_source_dispose;
133   gobject_class->finalize = (GObjectFinalizeFunc) gst_oss4_source_finalize;
134   gobject_class->get_property = gst_oss4_source_get_property;
135   gobject_class->set_property = gst_oss4_source_set_property;
136
137   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_source_getcaps);
138
139   gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss4_source_open_func);
140   gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_source_prepare);
141   gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_source_unprepare);
142   gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss4_source_close);
143   gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss4_source_read);
144   gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_source_delay);
145   gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_source_reset);
146
147   g_object_class_install_property (gobject_class, PROP_DEVICE,
148       g_param_spec_string ("device", "Device",
149           "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
150           "(NULL = use first available device)",
151           DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152
153   g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
154       g_param_spec_string ("device-name", "Device name",
155           "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
156           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
157 }
158
159 static void
160 gst_oss4_source_init (GstOss4Source * osssrc, GstOss4SourceClass * g_class)
161 {
162   const gchar *device;
163
164   device = g_getenv ("AUDIODEV");
165   if (device == NULL)
166     device = DEFAULT_DEVICE;
167
168   osssrc->fd = -1;
169   osssrc->device = g_strdup (device);
170   osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
171   osssrc->device_name = NULL;
172 }
173
174 static void
175 gst_oss4_source_finalize (GstOss4Source * oss)
176 {
177   g_free (oss->device);
178   oss->device = NULL;
179
180   g_list_free (oss->property_probe_list);
181   oss->property_probe_list = NULL;
182
183   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (oss));
184 }
185
186 static void
187 gst_oss4_source_dispose (GObject * object)
188 {
189   GstOss4Source *oss = GST_OSS4_SOURCE (object);
190
191   if (oss->probed_caps) {
192     gst_caps_unref (oss->probed_caps);
193     oss->probed_caps = NULL;
194   }
195
196   G_OBJECT_CLASS (parent_class)->dispose (object);
197 }
198
199 static void
200 gst_oss4_source_set_property (GObject * object, guint prop_id,
201     const GValue * value, GParamSpec * pspec)
202 {
203   GstOss4Source *oss;
204
205   oss = GST_OSS4_SOURCE (object);
206
207   switch (prop_id) {
208     case PROP_DEVICE:
209       GST_OBJECT_LOCK (oss);
210       if (oss->fd == -1) {
211         g_free (oss->device);
212         oss->device = g_value_dup_string (value);
213         g_free (oss->device_name);
214         oss->device_name = NULL;
215       } else {
216         g_warning ("%s: can't change \"device\" property while audio source "
217             "is open", GST_OBJECT_NAME (oss));
218       }
219       GST_OBJECT_UNLOCK (oss);
220       break;
221     default:
222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223       break;
224   }
225 }
226
227 static void
228 gst_oss4_source_get_property (GObject * object, guint prop_id,
229     GValue * value, GParamSpec * pspec)
230 {
231   GstOss4Source *oss;
232
233   oss = GST_OSS4_SOURCE (object);
234
235   switch (prop_id) {
236     case PROP_DEVICE:
237       GST_OBJECT_LOCK (oss);
238       g_value_set_string (value, oss->device);
239       GST_OBJECT_UNLOCK (oss);
240       break;
241     case PROP_DEVICE_NAME:
242       GST_OBJECT_LOCK (oss);
243       /* If device is set, try to retrieve the name even if we're not open */
244       if (oss->fd == -1 && oss->device != NULL) {
245         if (gst_oss4_source_open (GST_AUDIO_SRC (oss), TRUE)) {
246           g_value_set_string (value, oss->device_name);
247           gst_oss4_source_close (GST_AUDIO_SRC (oss));
248         } else {
249           gchar *name = NULL;
250
251           gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
252               oss->device, &name);
253           g_value_set_string (value, name);
254           g_free (name);
255         }
256       } else {
257         g_value_set_string (value, oss->device_name);
258       }
259
260       GST_OBJECT_UNLOCK (oss);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264       break;
265   }
266 }
267
268 static GstCaps *
269 gst_oss4_source_getcaps (GstBaseSrc * bsrc)
270 {
271   GstOss4Source *oss;
272   GstCaps *caps;
273
274   oss = GST_OSS4_SOURCE (bsrc);
275
276   if (oss->fd == -1) {
277     caps = gst_oss4_audio_get_template_caps ();
278   } else if (oss->probed_caps) {
279     caps = gst_caps_copy (oss->probed_caps);
280   } else {
281     caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
282     if (caps != NULL && !gst_caps_is_empty (caps)) {
283       oss->probed_caps = gst_caps_copy (caps);
284     }
285   }
286
287   return caps;
288 }
289
290 /* note: we must not take the object lock here unless we fix up get_property */
291 static gboolean
292 gst_oss4_source_open (GstAudioSrc * asrc, gboolean silent_errors)
293 {
294   GstOss4Source *oss;
295   gchar *device;
296   int mode;
297
298   oss = GST_OSS4_SOURCE (asrc);
299
300   if (oss->device)
301     device = g_strdup (oss->device);
302   else
303     device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
304
305   /* desperate times, desperate measures */
306   if (device == NULL)
307     device = g_strdup ("/dev/dsp0");
308
309   GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
310
311   /* we open in non-blocking mode even if we don't really want to do writes
312    * non-blocking because we can't be sure that this is really a genuine
313    * OSS4 device with well-behaved drivers etc. We really don't want to
314    * hang forever under any circumstances. */
315   oss->fd = open (device, O_RDONLY | O_NONBLOCK, 0);
316   if (oss->fd == -1) {
317     switch (errno) {
318       case EBUSY:
319         goto busy;
320       case EACCES:
321         goto no_permission;
322       default:
323         goto open_failed;
324     }
325   }
326
327   GST_INFO_OBJECT (oss, "Opened device");
328
329   /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
330   if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
331     goto legacy_oss;
332
333   /* now remove the non-blocking flag. */
334   mode = fcntl (oss->fd, F_GETFL);
335   mode &= ~O_NONBLOCK;
336   if (fcntl (oss->fd, F_SETFL, mode) < 0) {
337     /* some drivers do no support unsetting the non-blocking flag, try to
338      * close/open the device then. This is racy but we error out properly. */
339     GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
340         "will try to re-open device now");
341     gst_oss4_source_close (asrc);
342     if ((oss->fd = open (device, O_RDONLY, 0)) == -1)
343       goto non_block;
344   }
345
346   oss->open_device = device;
347
348   /* not using ENGINEINFO here because it sometimes returns a different and
349    * less useful name than AUDIOINFO for the same device */
350   if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
351           oss->open_device, &oss->device_name)) {
352     oss->device_name = NULL;
353   }
354
355   return TRUE;
356
357   /* ERRORS */
358 busy:
359   {
360     if (!silent_errors) {
361       GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
362           (_("Could not open audio device for playback. "
363                   "Device is being used by another application.")), (NULL));
364     }
365     g_free (device);
366     return FALSE;
367   }
368 no_permission:
369   {
370     if (!silent_errors) {
371       GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
372           (_("Could not open audio device for playback. "
373                   "You don't have permission to open the device.")),
374           GST_ERROR_SYSTEM);
375     }
376     g_free (device);
377     return FALSE;
378   }
379 open_failed:
380   {
381     if (!silent_errors) {
382       GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
383           (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
384     }
385     g_free (device);
386     return FALSE;
387   }
388 legacy_oss:
389   {
390     gst_oss4_source_close (asrc);
391     if (!silent_errors) {
392       GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
393           (_("Could not open audio device for playback. "
394                   "This version of the Open Sound System is not supported by this "
395                   "element.")), ("Try the 'osssink' element instead"));
396     }
397     g_free (device);
398     return FALSE;
399   }
400 non_block:
401   {
402     if (!silent_errors) {
403       GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
404           ("Unable to set device %s into non-blocking mode: %s",
405               oss->device, g_strerror (errno)));
406     }
407     g_free (device);
408     return FALSE;
409   }
410 }
411
412 static gboolean
413 gst_oss4_source_open_func (GstAudioSrc * asrc)
414 {
415   return gst_oss4_source_open (asrc, FALSE);
416 }
417
418 static void
419 gst_oss4_source_free_mixer_tracks (GstOss4Source * oss)
420 {
421   g_list_foreach (oss->tracks, (GFunc) g_object_unref, NULL);
422   g_list_free (oss->tracks);
423   oss->tracks = NULL;
424 }
425
426 static gboolean
427 gst_oss4_source_close (GstAudioSrc * asrc)
428 {
429   GstOss4Source *oss;
430
431   oss = GST_OSS4_SOURCE (asrc);
432
433   if (oss->fd != -1) {
434     GST_DEBUG_OBJECT (oss, "closing device");
435     close (oss->fd);
436     oss->fd = -1;
437   }
438
439   oss->bytes_per_sample = 0;
440
441   gst_caps_replace (&oss->probed_caps, NULL);
442
443   g_free (oss->open_device);
444   oss->open_device = NULL;
445
446   g_free (oss->device_name);
447   oss->device_name = NULL;
448
449   gst_oss4_source_free_mixer_tracks (oss);
450
451   return TRUE;
452 }
453
454 static gboolean
455 gst_oss4_source_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
456 {
457   GstOss4Source *oss;
458
459   oss = GST_OSS4_SOURCE (asrc);
460
461   if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
462     GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
463         spec->caps);
464     return FALSE;
465   }
466
467   oss->bytes_per_sample = spec->bytes_per_sample;
468   return TRUE;
469 }
470
471 static gboolean
472 gst_oss4_source_unprepare (GstAudioSrc * asrc)
473 {
474   /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
475    * since HALT won't properly reset some devices, apparently */
476
477   if (!gst_oss4_source_close (asrc))
478     goto couldnt_close;
479
480   if (!gst_oss4_source_open_func (asrc))
481     goto couldnt_reopen;
482
483   return TRUE;
484
485   /* ERRORS */
486 couldnt_close:
487   {
488     GST_DEBUG_OBJECT (asrc, "Couldn't close the audio device");
489     return FALSE;
490   }
491 couldnt_reopen:
492   {
493     GST_DEBUG_OBJECT (asrc, "Couldn't reopen the audio device");
494     return FALSE;
495   }
496 }
497
498 static guint
499 gst_oss4_source_read (GstAudioSrc * asrc, gpointer data, guint length)
500 {
501   GstOss4Source *oss;
502   int n;
503
504   oss = GST_OSS4_SOURCE_CAST (asrc);
505
506   n = read (oss->fd, data, length);
507   GST_LOG_OBJECT (asrc, "%u bytes, %u samples", n, n / oss->bytes_per_sample);
508
509   if (G_UNLIKELY (n < 0)) {
510     switch (errno) {
511       case ENOTSUP:
512       case EACCES:{
513         /* This is the most likely cause, I think */
514         GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
515             (_("Recording is not supported by this audio device.")),
516             ("read: %s (device: %s) (maybe this is an output-only device?)",
517                 g_strerror (errno), oss->open_device));
518         break;
519       }
520       default:{
521         GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
522             (_("Error recording from audio device.")),
523             ("read: %s (device: %s)", g_strerror (errno), oss->open_device));
524         break;
525       }
526     }
527   }
528
529   return (guint) n;
530 }
531
532 static guint
533 gst_oss4_source_delay (GstAudioSrc * asrc)
534 {
535   audio_buf_info info = { 0, };
536   GstOss4Source *oss;
537   guint delay;
538
539   oss = GST_OSS4_SOURCE_CAST (asrc);
540
541   if (ioctl (oss->fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
542     GST_LOG_OBJECT (oss, "GETISPACE failed: %s", g_strerror (errno));
543     return 0;
544   }
545
546   delay = (info.fragstotal * info.fragsize) - info.bytes;
547   GST_LOG_OBJECT (oss, "fragstotal:%d, fragsize:%d, bytes:%d, delay:%d",
548       info.fragstotal, info.fragsize, info.bytes, delay);
549   return delay;
550 }
551
552 static void
553 gst_oss4_source_reset (GstAudioSrc * asrc)
554 {
555   /* There's nothing we can do here really: OSS can't handle access to the
556    * same device/fd from multiple threads and might deadlock or blow up in
557    * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
558 }
559
560 /* GstMixer interface, which we abuse here for input selection, because we
561  * don't have a proper interface for that and because that's what
562  * gnome-sound-recorder does. */
563
564 /* GstMixerTrack is a plain GObject, so let's just use the GLib macro here */
565 G_DEFINE_TYPE (GstOss4SourceInput, gst_oss4_source_input, GST_TYPE_MIXER_TRACK);
566
567 static void
568 gst_oss4_source_input_class_init (GstOss4SourceInputClass * klass)
569 {
570   /* nothing to do here */
571 }
572
573 static void
574 gst_oss4_source_input_init (GstOss4SourceInput * i)
575 {
576   /* nothing to do here */
577 }
578
579 #if 0
580
581 static void
582 gst_ossmixer_ensure_track_list (GstOssMixer * mixer)
583 {
584   gint i, master = -1;
585
586   g_return_if_fail (mixer->fd != -1);
587
588   if (mixer->tracklist)
589     return;
590
591   /* find master volume */
592   if (mixer->devmask & SOUND_MASK_VOLUME)
593     master = SOUND_MIXER_VOLUME;
594   else if (mixer->devmask & SOUND_MASK_PCM)
595     master = SOUND_MIXER_PCM;
596   else if (mixer->devmask & SOUND_MASK_SPEAKER)
597     master = SOUND_MIXER_SPEAKER;       /* doubtful... */
598   /* else: no master, so we won't set any */
599
600   /* build track list */
601   for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
602     if (mixer->devmask & (1 << i)) {
603       GstMixerTrack *track;
604       gboolean input = FALSE, stereo = FALSE, record = FALSE;
605
606       /* track exists, make up capabilities */
607       if (MASK_BIT_IS_SET (mixer->stereomask, i))
608         stereo = TRUE;
609       if (MASK_BIT_IS_SET (mixer->recmask, i))
610         input = TRUE;
611       if (MASK_BIT_IS_SET (mixer->recdevs, i))
612         record = TRUE;
613
614       /* do we want mixer in our list? */
615       if (!((mixer->dir & GST_OSS_MIXER_CAPTURE && input == TRUE) ||
616               (mixer->dir & GST_OSS_MIXER_PLAYBACK && i != SOUND_MIXER_PCM)))
617         /* the PLAYBACK case seems hacky, but that's how 0.8 had it */
618         continue;
619
620       /* add track to list */
621       track = gst_ossmixer_track_new (mixer->fd, i, stereo ? 2 : 1,
622           (record ? GST_MIXER_TRACK_RECORD : 0) |
623           (input ? GST_MIXER_TRACK_INPUT :
624               GST_MIXER_TRACK_OUTPUT) |
625           ((master != i) ? 0 : GST_MIXER_TRACK_MASTER));
626       mixer->tracklist = g_list_append (mixer->tracklist, track);
627     }
628   }
629 }
630
631 /* unused with G_DISABLE_* */
632 static G_GNUC_UNUSED gboolean
633 gst_ossmixer_contains_track (GstOssMixer * mixer, GstOssMixerTrack * osstrack)
634 {
635   const GList *item;
636
637   for (item = mixer->tracklist; item != NULL; item = item->next)
638     if (item->data == osstrack)
639       return TRUE;
640
641   return FALSE;
642 }
643
644 const GList *
645 gst_ossmixer_list_tracks (GstOssMixer * mixer)
646 {
647   gst_ossmixer_ensure_track_list (mixer);
648
649   return (const GList *) mixer->tracklist;
650 }
651
652 void
653 gst_ossmixer_get_volume (GstOssMixer * mixer,
654     GstMixerTrack * track, gint * volumes)
655 {
656   gint volume;
657   GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);
658
659   g_return_if_fail (mixer->fd != -1);
660   g_return_if_fail (gst_ossmixer_contains_track (mixer, osstrack));
661
662   if (track->flags & GST_MIXER_TRACK_MUTE) {
663     volumes[0] = osstrack->lvol;
664     if (track->num_channels == 2) {
665       volumes[1] = osstrack->rvol;
666     }
667   } else {
668     /* get */
669     if (ioctl (mixer->fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
670       g_warning ("Error getting recording device (%d) volume: %s",
671           osstrack->track_num, g_strerror (errno));
672       volume = 0;
673     }
674
675     osstrack->lvol = volumes[0] = (volume & 0xff);
676     if (track->num_channels == 2) {
677       osstrack->rvol = volumes[1] = ((volume >> 8) & 0xff);
678     }
679   }
680 }
681
682 void
683 gst_ossmixer_set_mute (GstOssMixer * mixer, GstMixerTrack * track,
684     gboolean mute)
685 {
686   int volume;
687   GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);
688
689   g_return_if_fail (mixer->fd != -1);
690   g_return_if_fail (gst_ossmixer_contains_track (mixer, osstrack));
691
692   if (mute) {
693     volume = 0;
694   } else {
695     volume = (osstrack->lvol & 0xff);
696     if (MASK_BIT_IS_SET (mixer->stereomask, osstrack->track_num)) {
697       volume |= ((osstrack->rvol & 0xff) << 8);
698     }
699   }
700
701   if (ioctl (mixer->fd, MIXER_WRITE (osstrack->track_num), &volume) < 0) {
702     g_warning ("Error setting mixer recording device volume (0x%x): %s",
703         volume, g_strerror (errno));
704     return;
705   }
706
707   if (mute) {
708     track->flags |= GST_MIXER_TRACK_MUTE;
709   } else {
710     track->flags &= ~GST_MIXER_TRACK_MUTE;
711   }
712 }
713 #endif
714
715 static gint
716 gst_oss4_source_mixer_get_current_input (GstOss4Source * oss)
717 {
718   int cur = -1;
719
720   if (ioctl (oss->fd, SNDCTL_DSP_GET_RECSRC, &cur) == -1 || cur < 0)
721     return -1;
722
723   return cur;
724 }
725
726 static const gchar *
727 gst_oss4_source_mixer_update_record_flags (GstOss4Source * oss, gint cur_route)
728 {
729   const gchar *cur_name = "";
730   GList *t;
731
732   for (t = oss->tracks; t != NULL; t = t->next) {
733     GstMixerTrack *track = t->data;
734
735     if (GST_OSS4_SOURCE_INPUT (track)->route == cur_route) {
736       if (!GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) {
737         track->flags |= GST_MIXER_TRACK_RECORD;
738         /* no point in sending a mixer-record-changes message here */
739       }
740       cur_name = track->label;
741     } else {
742       if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) {
743         track->flags &= ~GST_MIXER_TRACK_RECORD;
744         /* no point in sending a mixer-record-changes message here */
745       }
746     }
747   }
748
749   return cur_name;
750 }
751
752 static const GList *
753 gst_oss4_source_mixer_list_tracks (GstMixer * mixer)
754 {
755   oss_mixer_enuminfo names = { 0, };
756   GstOss4Source *oss;
757   const gchar *cur_name;
758   GList *tracks = NULL;
759   gint i, cur;
760
761   g_return_val_if_fail (mixer != NULL, NULL);
762   g_return_val_if_fail (GST_IS_OSS4_SOURCE (mixer), NULL);
763   g_return_val_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer), NULL);
764
765   oss = GST_OSS4_SOURCE (mixer);
766
767   if (oss->tracks != NULL && oss->tracks_static)
768     goto done;
769
770   if (ioctl (oss->fd, SNDCTL_DSP_GET_RECSRC_NAMES, &names) == -1)
771     goto get_recsrc_names_error;
772
773   oss->tracks_static = (names.version == 0);
774
775   GST_INFO_OBJECT (oss, "%d inputs (list is static: %s):", names.nvalues,
776       (oss->tracks_static) ? "yes" : "no");
777
778   for (i = 0; i < MIN (names.nvalues, OSS_ENUM_MAXVALUE + 1); ++i) {
779     GstMixerTrack *track;
780
781     track = g_object_new (GST_TYPE_OSS4_SOURCE_INPUT, NULL);
782     track->label = g_strdup (&names.strings[names.strindex[i]]);
783     track->flags = GST_MIXER_TRACK_INPUT;
784     track->num_channels = 2;
785     track->min_volume = 0;
786     track->max_volume = 100;
787     GST_OSS4_SOURCE_INPUT (track)->route = i;
788
789     GST_INFO_OBJECT (oss, " [%d] %s", i, track->label);
790     tracks = g_list_append (tracks, track);
791   }
792
793   gst_oss4_source_free_mixer_tracks (oss);
794   oss->tracks = tracks;
795
796 done:
797
798   /* update RECORD flags */
799   cur = gst_oss4_source_mixer_get_current_input (oss);
800   cur_name = gst_oss4_source_mixer_update_record_flags (oss, cur);
801   GST_DEBUG_OBJECT (oss, "current input route: %d (%s)", cur, cur_name);
802
803   return (const GList *) oss->tracks;
804
805 /* ERRORS */
806 get_recsrc_names_error:
807   {
808     GST_WARNING_OBJECT (oss, "GET_RECSRC_NAMES failed: %s", g_strerror (errno));
809     return NULL;
810   }
811 }
812
813 static void
814 gst_oss4_source_mixer_set_volume (GstMixer * mixer, GstMixerTrack * track,
815     gint * volumes)
816 {
817   GstOss4Source *oss;
818   int new_vol, cur;
819
820   g_return_if_fail (mixer != NULL);
821   g_return_if_fail (track != NULL);
822   g_return_if_fail (GST_IS_MIXER_TRACK (track));
823   g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
824   g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
825
826   oss = GST_OSS4_SOURCE (mixer);
827
828   cur = gst_oss4_source_mixer_get_current_input (oss);
829   if (cur != GST_OSS4_SOURCE_INPUT (track)->route) {
830     GST_DEBUG_OBJECT (oss, "track not selected input route, ignoring request");
831     return;
832   }
833
834   new_vol = (volumes[1] << 8) | volumes[0];
835   if (ioctl (oss->fd, SNDCTL_DSP_SETRECVOL, &new_vol) == -1) {
836     GST_WARNING_OBJECT (oss, "SETRECVOL failed: %s", g_strerror (errno));
837   }
838 }
839
840 static void
841 gst_oss4_source_mixer_get_volume (GstMixer * mixer, GstMixerTrack * track,
842     gint * volumes)
843 {
844   GstOss4Source *oss;
845   int cur;
846
847   g_return_if_fail (mixer != NULL);
848   g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
849   g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
850
851   oss = GST_OSS4_SOURCE (mixer);
852
853   cur = gst_oss4_source_mixer_get_current_input (oss);
854   if (cur != GST_OSS4_SOURCE_INPUT (track)->route) {
855     volumes[0] = 0;
856     volumes[1] = 0;
857   } else {
858     int vol = -1;
859
860     if (ioctl (oss->fd, SNDCTL_DSP_GETRECVOL, &vol) == -1 || vol < 0) {
861       GST_WARNING_OBJECT (oss, "GETRECVOL failed: %s", g_strerror (errno));
862       volumes[0] = 100;
863       volumes[1] = 100;
864     } else {
865       volumes[0] = MIN (100, vol & 0xff);
866       volumes[1] = MIN (100, (vol >> 8) & 0xff);
867     }
868   }
869 }
870
871 static void
872 gst_oss4_source_mixer_set_record (GstMixer * mixer, GstMixerTrack * track,
873     gboolean record)
874 {
875   GstOss4Source *oss;
876   const gchar *cur_name;
877   gint cur;
878
879   g_return_if_fail (mixer != NULL);
880   g_return_if_fail (track != NULL);
881   g_return_if_fail (GST_IS_MIXER_TRACK (track));
882   g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
883   g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
884
885   oss = GST_OSS4_SOURCE (mixer);
886
887   cur = gst_oss4_source_mixer_get_current_input (oss);
888
889   /* stop recording for an input that's not selected anyway => nothing to do */
890   if (!record && cur != GST_OSS4_SOURCE_INPUT (track)->route)
891     goto done;
892
893   /* select recording for an input that's already selected => nothing to do
894    * (or should we mess with the recording volume in this case maybe?) */
895   if (record && cur == GST_OSS4_SOURCE_INPUT (track)->route)
896     goto done;
897
898   /* make current input stop recording: we can't really make an input stop
899    * recording, we can only select an input FOR recording, so we'll just ignore
900    * all requests to stop for now */
901   if (!record) {
902     GST_WARNING_OBJECT (oss, "Can't un-select an input as such, only switch "
903         "to a different input source");
904     /* FIXME: set recording volume to 0 maybe? */
905   } else {
906     int new_route = GST_OSS4_SOURCE_INPUT (track)->route;
907
908     /* select this input for recording */
909
910     if (ioctl (oss->fd, SNDCTL_DSP_SET_RECSRC, &new_route) == -1) {
911       GST_WARNING_OBJECT (oss, "Could not select input %d for recording: %s",
912           new_route, g_strerror (errno));
913     } else {
914       cur = new_route;
915     }
916   }
917
918 done:
919
920   cur_name = gst_oss4_source_mixer_update_record_flags (oss, cur);
921   GST_DEBUG_OBJECT (oss, "active input route: %d (%s)", cur, cur_name);
922 }
923
924 static void
925 gst_oss4_source_mixer_set_mute (GstMixer * mixer, GstMixerTrack * track,
926     gboolean mute)
927 {
928   g_return_if_fail (mixer != NULL);
929   g_return_if_fail (track != NULL);
930   g_return_if_fail (GST_IS_MIXER_TRACK (track));
931   g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
932   g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
933
934   /* FIXME: implement gst_oss4_source_mixer_set_mute() - what to do here? */
935   /* oss4_mixer_set_mute (mixer->mixer, track, mute); */
936 }
937
938 static void
939 gst_oss4_source_mixer_interface_init (GstMixerClass * klass)
940 {
941   GST_MIXER_TYPE (klass) = GST_MIXER_HARDWARE;
942
943   klass->list_tracks = gst_oss4_source_mixer_list_tracks;
944   klass->set_volume = gst_oss4_source_mixer_set_volume;
945   klass->get_volume = gst_oss4_source_mixer_get_volume;
946   klass->set_mute = gst_oss4_source_mixer_set_mute;
947   klass->set_record = gst_oss4_source_mixer_set_record;
948 }
949
950 /* Implement the horror that is GstImplementsInterface */
951
952 static gboolean
953 gst_oss4_source_mixer_supported (GstImplementsInterface * iface,
954     GType iface_type)
955 {
956   GstOss4Source *oss;
957   gboolean is_open;
958
959   g_return_val_if_fail (GST_IS_OSS4_SOURCE (iface), FALSE);
960   g_return_val_if_fail (iface_type == GST_TYPE_MIXER, FALSE);
961
962   oss = GST_OSS4_SOURCE (iface);
963
964   GST_OBJECT_LOCK (oss);
965   is_open = GST_OSS4_SOURCE_IS_OPEN (iface);
966   GST_OBJECT_UNLOCK (oss);
967
968   return is_open;
969 }
970
971 static void
972 gst_oss4_source_mixer_implements_interface_init (GstImplementsInterfaceClass *
973     klass)
974 {
975   klass->supported = gst_oss4_source_mixer_supported;
976 }
977
978 static void
979 gst_oss4_source_init_interfaces (GType type)
980 {
981   static const GInterfaceInfo implements_iface_info = {
982     (GInterfaceInitFunc) gst_oss4_source_mixer_implements_interface_init,
983     NULL,
984     NULL,
985   };
986   static const GInterfaceInfo mixer_iface_info = {
987     (GInterfaceInitFunc) gst_oss4_source_mixer_interface_init,
988     NULL,
989     NULL,
990   };
991
992   g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
993       &implements_iface_info);
994   g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
995
996   gst_oss4_add_property_probe_interface (type);
997 }