1 /* GStreamer OSS4 audio source
2 * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
21 * SECTION:element-oss4src
25 * This element lets you record sound using the Open Sound System (OSS)
28 * <title>Example pipelines</title>
31 * gst-launch -v oss4src ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
33 * will record sound from your sound card using OSS4 and encode it to an
34 * Ogg/Vorbis file (this will only work if your mixer settings are right
35 * and the right inputs areenabled etc.)
42 /* FIXME: make sure we're not doing ioctls from the app thread (e.g. via the
43 * mixer interface) while recording */
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
57 #include <gst/interfaces/mixer.h>
58 #include <gst/gst-i18n-plugin.h>
60 #define NO_LEGACY_MIXER
61 #include "oss4-audio.h"
62 #include "oss4-source.h"
63 #include "oss4-property-probe.h"
64 #include "oss4-soundcard.h"
66 #define GST_OSS4_SOURCE_IS_OPEN(src) (GST_OSS4_SOURCE(src)->fd != -1)
68 GST_DEBUG_CATEGORY_EXTERN (oss4src_debug);
69 #define GST_CAT_DEFAULT oss4src_debug
71 #define DEFAULT_DEVICE NULL
72 #define DEFAULT_DEVICE_NAME NULL
81 static void gst_oss4_source_init_interfaces (GType type);
83 GST_BOILERPLATE_FULL (GstOss4Source, gst_oss4_source, GstAudioSrc,
84 GST_TYPE_AUDIO_SRC, gst_oss4_source_init_interfaces);
86 static void gst_oss4_source_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
88 static void gst_oss4_source_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
91 static void gst_oss4_source_dispose (GObject * object);
92 static void gst_oss4_source_finalize (GstOss4Source * osssrc);
94 static GstCaps *gst_oss4_source_getcaps (GstBaseSrc * bsrc);
96 static gboolean gst_oss4_source_open (GstAudioSrc * asrc,
97 gboolean silent_errors);
98 static gboolean gst_oss4_source_open_func (GstAudioSrc * asrc);
99 static gboolean gst_oss4_source_close (GstAudioSrc * asrc);
100 static gboolean gst_oss4_source_prepare (GstAudioSrc * asrc,
101 GstRingBufferSpec * spec);
102 static gboolean gst_oss4_source_unprepare (GstAudioSrc * asrc);
103 static guint gst_oss4_source_read (GstAudioSrc * asrc, gpointer data,
105 static guint gst_oss4_source_delay (GstAudioSrc * asrc);
106 static void gst_oss4_source_reset (GstAudioSrc * asrc);
109 gst_oss4_source_base_init (gpointer g_class)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112 GstPadTemplate *templ;
114 gst_element_class_set_details_simple (element_class,
115 "OSS v4 Audio Source", "Source/Audio",
116 "Capture from a sound card via OSS version 4",
117 "Tim-Philipp Müller <tim centricular net>");
119 templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
120 gst_oss4_audio_get_template_caps ());
121 gst_element_class_add_pad_template (element_class, templ);
124 gst_oss4_source_class_init (GstOss4SourceClass * klass)
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
128 GstBaseSrcClass *gstbasesrc_class;
129 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
130 GstAudioSrcClass *gstaudiosrc_class;
132 gobject_class = (GObjectClass *) klass;
133 gstelement_class = (GstElementClass *) klass;
134 gstbasesrc_class = (GstBaseSrcClass *) klass;
135 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
136 gstaudiosrc_class = (GstAudioSrcClass *) klass;
138 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss4_source_dispose);
139 gobject_class->finalize =
140 (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_oss4_source_finalize);
141 gobject_class->get_property =
142 GST_DEBUG_FUNCPTR (gst_oss4_source_get_property);
143 gobject_class->set_property =
144 GST_DEBUG_FUNCPTR (gst_oss4_source_set_property);
146 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_source_getcaps);
148 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss4_source_open_func);
149 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_source_prepare);
150 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_source_unprepare);
151 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss4_source_close);
152 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss4_source_read);
153 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_source_delay);
154 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_source_reset);
156 g_object_class_install_property (gobject_class, PROP_DEVICE,
157 g_param_spec_string ("device", "Device",
158 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
159 "(NULL = use first available device)",
160 DEFAULT_DEVICE, G_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
163 g_param_spec_string ("device-name", "Device name",
164 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
169 gst_oss4_source_init (GstOss4Source * osssrc, GstOss4SourceClass * g_class)
173 device = g_getenv ("AUDIODEV");
175 device = DEFAULT_DEVICE;
178 osssrc->device = g_strdup (device);
179 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
180 osssrc->device_name = NULL;
184 gst_oss4_source_finalize (GstOss4Source * oss)
186 g_free (oss->device);
189 g_list_free (oss->property_probe_list);
190 oss->property_probe_list = NULL;
192 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (oss));
196 gst_oss4_source_dispose (GObject * object)
198 GstOss4Source *oss = GST_OSS4_SOURCE (object);
200 if (oss->probed_caps) {
201 gst_caps_unref (oss->probed_caps);
202 oss->probed_caps = NULL;
205 G_OBJECT_CLASS (parent_class)->dispose (object);
209 gst_oss4_source_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
214 oss = GST_OSS4_SOURCE (object);
218 GST_OBJECT_LOCK (oss);
220 g_free (oss->device);
221 oss->device = g_value_dup_string (value);
222 g_free (oss->device_name);
223 oss->device_name = NULL;
225 g_warning ("%s: can't change \"device\" property while audio source "
226 "is open", GST_OBJECT_NAME (oss));
228 GST_OBJECT_UNLOCK (oss);
231 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 gst_oss4_source_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec)
242 oss = GST_OSS4_SOURCE (object);
246 GST_OBJECT_LOCK (oss);
247 g_value_set_string (value, oss->device);
248 GST_OBJECT_UNLOCK (oss);
250 case PROP_DEVICE_NAME:
251 GST_OBJECT_LOCK (oss);
252 /* If device is set, try to retrieve the name even if we're not open */
253 if (oss->fd == -1 && oss->device != NULL) {
254 if (gst_oss4_source_open (GST_AUDIO_SRC (oss), TRUE)) {
255 g_value_set_string (value, oss->device_name);
256 gst_oss4_source_close (GST_AUDIO_SRC (oss));
260 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
262 g_value_set_string (value, name);
266 g_value_set_string (value, oss->device_name);
269 GST_OBJECT_UNLOCK (oss);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_oss4_source_getcaps (GstBaseSrc * bsrc)
283 oss = GST_OSS4_SOURCE (bsrc);
286 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
287 } else if (oss->probed_caps) {
288 caps = gst_caps_copy (oss->probed_caps);
290 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
291 if (caps != NULL && !gst_caps_is_empty (caps)) {
292 oss->probed_caps = gst_caps_copy (caps);
299 /* note: we must not take the object lock here unless we fix up get_property */
301 gst_oss4_source_open (GstAudioSrc * asrc, gboolean silent_errors)
307 oss = GST_OSS4_SOURCE (asrc);
310 device = g_strdup (oss->device);
312 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
314 /* desperate times, desperate measures */
316 device = g_strdup ("/dev/dsp0");
318 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
320 /* we open in non-blocking mode even if we don't really want to do writes
321 * non-blocking because we can't be sure that this is really a genuine
322 * OSS4 device with well-behaved drivers etc. We really don't want to
323 * hang forever under any circumstances. */
324 oss->fd = open (device, O_RDONLY | O_NONBLOCK, 0);
336 GST_INFO_OBJECT (oss, "Opened device");
338 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
339 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
342 /* now remove the non-blocking flag. */
343 mode = fcntl (oss->fd, F_GETFL);
345 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
346 /* some drivers do no support unsetting the non-blocking flag, try to
347 * close/open the device then. This is racy but we error out properly. */
348 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
349 "will try to re-open device now");
350 gst_oss4_source_close (asrc);
351 if ((oss->fd = open (device, O_RDONLY, 0)) == -1)
355 oss->open_device = device;
357 /* not using ENGINEINFO here because it sometimes returns a different and
358 * less useful name than AUDIOINFO for the same device */
359 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
360 oss->open_device, &oss->device_name)) {
361 oss->device_name = NULL;
369 if (!silent_errors) {
370 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
371 (_("Could not open audio device for playback. "
372 "Device is being used by another application.")), (NULL));
379 if (!silent_errors) {
380 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
381 (_("Could not open audio device for playback."
382 "You don't have permission to open the device.")),
390 if (!silent_errors) {
391 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
392 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
399 gst_oss4_source_close (asrc);
400 if (!silent_errors) {
401 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
402 (_("Could not open audio device for playback."
403 "This version of the Open Sound System is not supported by this "
404 "element.")), ("Try the 'osssink' element instead"));
411 if (!silent_errors) {
412 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
413 ("Unable to set device %s into non-blocking mode: %s",
414 oss->device, g_strerror (errno)));
422 gst_oss4_source_open_func (GstAudioSrc * asrc)
424 return gst_oss4_source_open (asrc, FALSE);
428 gst_oss4_source_free_mixer_tracks (GstOss4Source * oss)
430 g_list_foreach (oss->tracks, (GFunc) g_object_unref, NULL);
431 g_list_free (oss->tracks);
436 gst_oss4_source_close (GstAudioSrc * asrc)
440 oss = GST_OSS4_SOURCE (asrc);
443 GST_DEBUG_OBJECT (oss, "closing device");
448 oss->bytes_per_sample = 0;
450 gst_caps_replace (&oss->probed_caps, NULL);
452 g_free (oss->open_device);
453 oss->open_device = NULL;
455 g_free (oss->device_name);
456 oss->device_name = NULL;
458 gst_oss4_source_free_mixer_tracks (oss);
464 gst_oss4_source_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
468 oss = GST_OSS4_SOURCE (asrc);
470 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
471 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
476 oss->bytes_per_sample = spec->bytes_per_sample;
481 gst_oss4_source_unprepare (GstAudioSrc * asrc)
483 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
484 * since HALT won't properly reset some devices, apparently */
486 if (!gst_oss4_source_close (asrc))
489 if (!gst_oss4_source_open_func (asrc))
497 GST_DEBUG_OBJECT (asrc, "Couldn't close the audio device");
502 GST_DEBUG_OBJECT (asrc, "Couldn't reopen the audio device");
508 gst_oss4_source_read (GstAudioSrc * asrc, gpointer data, guint length)
513 oss = GST_OSS4_SOURCE_CAST (asrc);
515 n = read (oss->fd, data, length);
516 GST_LOG_OBJECT (asrc, "%u bytes, %u samples", n, n / oss->bytes_per_sample);
518 if (G_UNLIKELY (n < 0)) {
522 /* This is the most likely cause, I think */
523 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
524 (_("Recording is not supported by this audio device.")),
525 ("read: %s (device: %s) (maybe this is an output-only device?)",
526 g_strerror (errno), oss->open_device));
530 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
531 (_("Error recording from audio device.")),
532 ("read: %s (device: %s)", g_strerror (errno), oss->open_device));
542 gst_oss4_source_delay (GstAudioSrc * asrc)
544 audio_buf_info info = { 0, };
548 oss = GST_OSS4_SOURCE_CAST (asrc);
550 if (ioctl (oss->fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
551 GST_LOG_OBJECT (oss, "GETISPACE failed: %s", g_strerror (errno));
555 delay = (info.fragstotal * info.fragsize) - info.bytes;
556 GST_LOG_OBJECT (oss, "fragstotal:%d, fragsize:%d, bytes:%d, delay:%d",
557 info.fragstotal, info.fragsize, info.bytes, delay);
562 gst_oss4_source_reset (GstAudioSrc * asrc)
564 /* There's nothing we can do here really: OSS can't handle access to the
565 * same device/fd from multiple threads and might deadlock or blow up in
566 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
569 /* GstMixer interface, which we abuse here for input selection, because we
570 * don't have a proper interface for that and because that's what
571 * gnome-sound-recorder does. */
573 /* GstMixerTrack is a plain GObject, so let's just use the GLib macro here */
574 G_DEFINE_TYPE (GstOss4SourceInput, gst_oss4_source_input, GST_TYPE_MIXER_TRACK);
577 gst_oss4_source_input_class_init (GstOss4SourceInputClass * klass)
579 /* nothing to do here */
583 gst_oss4_source_input_init (GstOss4SourceInput * i)
585 /* nothing to do here */
591 gst_ossmixer_ensure_track_list (GstOssMixer * mixer)
595 g_return_if_fail (mixer->fd != -1);
597 if (mixer->tracklist)
600 /* find master volume */
601 if (mixer->devmask & SOUND_MASK_VOLUME)
602 master = SOUND_MIXER_VOLUME;
603 else if (mixer->devmask & SOUND_MASK_PCM)
604 master = SOUND_MIXER_PCM;
605 else if (mixer->devmask & SOUND_MASK_SPEAKER)
606 master = SOUND_MIXER_SPEAKER; /* doubtful... */
607 /* else: no master, so we won't set any */
609 /* build track list */
610 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
611 if (mixer->devmask & (1 << i)) {
612 GstMixerTrack *track;
613 gboolean input = FALSE, stereo = FALSE, record = FALSE;
615 /* track exists, make up capabilities */
616 if (MASK_BIT_IS_SET (mixer->stereomask, i))
618 if (MASK_BIT_IS_SET (mixer->recmask, i))
620 if (MASK_BIT_IS_SET (mixer->recdevs, i))
623 /* do we want mixer in our list? */
624 if (!((mixer->dir & GST_OSS_MIXER_CAPTURE && input == TRUE) ||
625 (mixer->dir & GST_OSS_MIXER_PLAYBACK && i != SOUND_MIXER_PCM)))
626 /* the PLAYBACK case seems hacky, but that's how 0.8 had it */
629 /* add track to list */
630 track = gst_ossmixer_track_new (mixer->fd, i, stereo ? 2 : 1,
631 (record ? GST_MIXER_TRACK_RECORD : 0) |
632 (input ? GST_MIXER_TRACK_INPUT :
633 GST_MIXER_TRACK_OUTPUT) |
634 ((master != i) ? 0 : GST_MIXER_TRACK_MASTER));
635 mixer->tracklist = g_list_append (mixer->tracklist, track);
640 /* unused with G_DISABLE_* */
641 static G_GNUC_UNUSED gboolean
642 gst_ossmixer_contains_track (GstOssMixer * mixer, GstOssMixerTrack * osstrack)
646 for (item = mixer->tracklist; item != NULL; item = item->next)
647 if (item->data == osstrack)
654 gst_ossmixer_list_tracks (GstOssMixer * mixer)
656 gst_ossmixer_ensure_track_list (mixer);
658 return (const GList *) mixer->tracklist;
662 gst_ossmixer_get_volume (GstOssMixer * mixer,
663 GstMixerTrack * track, gint * volumes)
666 GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);
668 g_return_if_fail (mixer->fd != -1);
669 g_return_if_fail (gst_ossmixer_contains_track (mixer, osstrack));
671 if (track->flags & GST_MIXER_TRACK_MUTE) {
672 volumes[0] = osstrack->lvol;
673 if (track->num_channels == 2) {
674 volumes[1] = osstrack->rvol;
678 if (ioctl (mixer->fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
679 g_warning ("Error getting recording device (%d) volume: %s",
680 osstrack->track_num, g_strerror (errno));
684 osstrack->lvol = volumes[0] = (volume & 0xff);
685 if (track->num_channels == 2) {
686 osstrack->rvol = volumes[1] = ((volume >> 8) & 0xff);
692 gst_ossmixer_set_mute (GstOssMixer * mixer, GstMixerTrack * track,
696 GstOssMixerTrack *osstrack = GST_OSSMIXER_TRACK (track);
698 g_return_if_fail (mixer->fd != -1);
699 g_return_if_fail (gst_ossmixer_contains_track (mixer, osstrack));
704 volume = (osstrack->lvol & 0xff);
705 if (MASK_BIT_IS_SET (mixer->stereomask, osstrack->track_num)) {
706 volume |= ((osstrack->rvol & 0xff) << 8);
710 if (ioctl (mixer->fd, MIXER_WRITE (osstrack->track_num), &volume) < 0) {
711 g_warning ("Error setting mixer recording device volume (0x%x): %s",
712 volume, g_strerror (errno));
717 track->flags |= GST_MIXER_TRACK_MUTE;
719 track->flags &= ~GST_MIXER_TRACK_MUTE;
725 gst_oss4_source_mixer_get_current_input (GstOss4Source * oss)
729 if (ioctl (oss->fd, SNDCTL_DSP_GET_RECSRC, &cur) == -1 || cur < 0)
736 gst_oss4_source_mixer_update_record_flags (GstOss4Source * oss, gint cur_route)
738 const gchar *cur_name = "";
741 for (t = oss->tracks; t != NULL; t = t->next) {
742 GstMixerTrack *track = t->data;
744 if (GST_OSS4_SOURCE_INPUT (track)->route == cur_route) {
745 if (!GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) {
746 track->flags |= GST_MIXER_TRACK_RECORD;
747 /* no point in sending a mixer-record-changes message here */
749 cur_name = track->label;
751 if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) {
752 track->flags &= ~GST_MIXER_TRACK_RECORD;
753 /* no point in sending a mixer-record-changes message here */
762 gst_oss4_source_mixer_list_tracks (GstMixer * mixer)
764 oss_mixer_enuminfo names = { 0, };
766 const gchar *cur_name;
767 GList *tracks = NULL;
770 g_return_val_if_fail (mixer != NULL, NULL);
771 g_return_val_if_fail (GST_IS_OSS4_SOURCE (mixer), NULL);
772 g_return_val_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer), NULL);
774 oss = GST_OSS4_SOURCE (mixer);
776 if (oss->tracks != NULL && oss->tracks_static)
779 if (ioctl (oss->fd, SNDCTL_DSP_GET_RECSRC_NAMES, &names) == -1)
780 goto get_recsrc_names_error;
782 oss->tracks_static = (names.version == 0);
784 GST_INFO_OBJECT (oss, "%d inputs (list is static: %s):", names.nvalues,
785 (oss->tracks_static) ? "yes" : "no");
787 for (i = 0; i < MIN (names.nvalues, OSS_ENUM_MAXVALUE + 1); ++i) {
788 GstMixerTrack *track;
790 track = g_object_new (GST_TYPE_OSS4_SOURCE_INPUT, NULL);
791 track->label = g_strdup (&names.strings[names.strindex[i]]);
792 track->flags = GST_MIXER_TRACK_INPUT;
793 track->num_channels = 2;
794 track->min_volume = 0;
795 track->max_volume = 100;
796 GST_OSS4_SOURCE_INPUT (track)->route = i;
798 GST_INFO_OBJECT (oss, " [%d] %s", i, track->label);
799 tracks = g_list_append (tracks, track);
802 gst_oss4_source_free_mixer_tracks (oss);
803 oss->tracks = tracks;
807 /* update RECORD flags */
808 cur = gst_oss4_source_mixer_get_current_input (oss);
809 cur_name = gst_oss4_source_mixer_update_record_flags (oss, cur);
810 GST_DEBUG_OBJECT (oss, "current input route: %d (%s)", cur, cur_name);
812 return (const GList *) oss->tracks;
815 get_recsrc_names_error:
817 GST_WARNING_OBJECT (oss, "GET_RECSRC_NAMES failed: %s", g_strerror (errno));
823 gst_oss4_source_mixer_set_volume (GstMixer * mixer, GstMixerTrack * track,
829 g_return_if_fail (mixer != NULL);
830 g_return_if_fail (track != NULL);
831 g_return_if_fail (GST_IS_MIXER_TRACK (track));
832 g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
833 g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
835 oss = GST_OSS4_SOURCE (mixer);
837 cur = gst_oss4_source_mixer_get_current_input (oss);
838 if (cur != GST_OSS4_SOURCE_INPUT (track)->route) {
839 GST_DEBUG_OBJECT (oss, "track not selected input route, ignoring request");
843 new_vol = (volumes[1] << 8) | volumes[0];
844 if (ioctl (oss->fd, SNDCTL_DSP_SETRECVOL, &new_vol) == -1) {
845 GST_WARNING_OBJECT (oss, "SETRECVOL failed: %s", g_strerror (errno));
850 gst_oss4_source_mixer_get_volume (GstMixer * mixer, GstMixerTrack * track,
856 g_return_if_fail (mixer != NULL);
857 g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
858 g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
860 oss = GST_OSS4_SOURCE (mixer);
862 cur = gst_oss4_source_mixer_get_current_input (oss);
863 if (cur != GST_OSS4_SOURCE_INPUT (track)->route) {
869 if (ioctl (oss->fd, SNDCTL_DSP_GETRECVOL, &vol) == -1 || vol < 0) {
870 GST_WARNING_OBJECT (oss, "GETRECVOL failed: %s", g_strerror (errno));
874 volumes[0] = MIN (100, vol & 0xff);
875 volumes[1] = MIN (100, (vol >> 8) & 0xff);
881 gst_oss4_source_mixer_set_record (GstMixer * mixer, GstMixerTrack * track,
885 const gchar *cur_name;
888 g_return_if_fail (mixer != NULL);
889 g_return_if_fail (track != NULL);
890 g_return_if_fail (GST_IS_MIXER_TRACK (track));
891 g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
892 g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
894 oss = GST_OSS4_SOURCE (mixer);
896 cur = gst_oss4_source_mixer_get_current_input (oss);
898 /* stop recording for an input that's not selected anyway => nothing to do */
899 if (!record && cur != GST_OSS4_SOURCE_INPUT (track)->route)
902 /* select recording for an input that's already selected => nothing to do
903 * (or should we mess with the recording volume in this case maybe?) */
904 if (record && cur == GST_OSS4_SOURCE_INPUT (track)->route)
907 /* make current input stop recording: we can't really make an input stop
908 * recording, we can only select an input FOR recording, so we'll just ignore
909 * all requests to stop for now */
911 GST_WARNING_OBJECT (oss, "Can't un-select an input as such, only switch "
912 "to a different input source");
913 /* FIXME: set recording volume to 0 maybe? */
915 int new_route = GST_OSS4_SOURCE_INPUT (track)->route;
917 /* select this input for recording */
919 if (ioctl (oss->fd, SNDCTL_DSP_SET_RECSRC, &new_route) == -1) {
920 GST_WARNING_OBJECT (oss, "Could not select input %d for recording: %s",
921 new_route, g_strerror (errno));
929 cur_name = gst_oss4_source_mixer_update_record_flags (oss, cur);
930 GST_DEBUG_OBJECT (oss, "active input route: %d (%s)", cur, cur_name);
934 gst_oss4_source_mixer_set_mute (GstMixer * mixer, GstMixerTrack * track,
939 g_return_if_fail (mixer != NULL);
940 g_return_if_fail (track != NULL);
941 g_return_if_fail (GST_IS_MIXER_TRACK (track));
942 g_return_if_fail (GST_IS_OSS4_SOURCE (mixer));
943 g_return_if_fail (GST_OSS4_SOURCE_IS_OPEN (mixer));
945 oss = GST_OSS4_SOURCE (mixer);
947 /* FIXME: implement gst_oss4_source_mixer_set_mute() - what to do here? */
948 /* oss4_mixer_set_mute (mixer->mixer, track, mute); */
952 gst_oss4_source_mixer_interface_init (GstMixerClass * klass)
954 GST_MIXER_TYPE (klass) = GST_MIXER_HARDWARE;
956 klass->list_tracks = gst_oss4_source_mixer_list_tracks;
957 klass->set_volume = gst_oss4_source_mixer_set_volume;
958 klass->get_volume = gst_oss4_source_mixer_get_volume;
959 klass->set_mute = gst_oss4_source_mixer_set_mute;
960 klass->set_record = gst_oss4_source_mixer_set_record;
963 /* Implement the horror that is GstImplementsInterface */
966 gst_oss4_source_mixer_supported (GstImplementsInterface * iface,
972 g_return_val_if_fail (GST_IS_OSS4_SOURCE (iface), FALSE);
973 g_return_val_if_fail (iface_type == GST_TYPE_MIXER, FALSE);
975 oss = GST_OSS4_SOURCE (iface);
977 GST_OBJECT_LOCK (oss);
978 is_open = GST_OSS4_SOURCE_IS_OPEN (iface);
979 GST_OBJECT_UNLOCK (oss);
985 gst_oss4_source_mixer_implements_interface_init (GstImplementsInterfaceClass *
988 klass->supported = gst_oss4_source_mixer_supported;
992 gst_oss4_source_init_interfaces (GType type)
994 static const GInterfaceInfo implements_iface_info = {
995 (GInterfaceInitFunc) gst_oss4_source_mixer_implements_interface_init,
999 static const GInterfaceInfo mixer_iface_info = {
1000 (GInterfaceInitFunc) gst_oss4_source_mixer_interface_init,
1005 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
1006 &implements_iface_info);
1007 g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
1009 gst_oss4_add_property_probe_interface (type);