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