1 /* ALSA mixer implementation.
2 * Copyright (C) 2003 Leif Johnson <leif@ambient.2y.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-alsamixer
22 * @see_also: alsasink, alsasrc
24 * This element controls various aspects such as the volume and balance
25 * of an audio device using the ALSA api.
27 * The application should query and use the interfaces provided by this
28 * element to control the device.
30 * Last reviewed on 2006-03-01 (0.10.4)
37 #include "gstalsamixer.h"
39 static void gst_alsa_mixer_update_option (GstAlsaMixer * mixer,
40 GstAlsaMixerOptions * alsa_opts);
41 static void gst_alsa_mixer_update_track (GstAlsaMixer * mixer,
42 GstAlsaMixerTrack * alsa_track);
43 static int gst_alsa_mixer_handle_callback (snd_mixer_t * handle,
44 unsigned int mask, snd_mixer_elem_t * elem);
46 /* First some utils, then the mixer implementation */
48 gst_alsa_mixer_open (GstAlsaMixer * mixer)
52 snd_ctl_card_info_t *card_info;
54 g_return_val_if_fail (mixer->handle == NULL, FALSE);
56 /* open and initialize the mixer device */
57 err = snd_mixer_open (&mixer->handle, 0);
58 if (err < 0 || mixer->handle == NULL)
61 if ((err = snd_mixer_attach (mixer->handle, mixer->device)) < 0) {
62 GST_WARNING ("Cannot open mixer for sound device '%s': %s", mixer->device,
67 if ((err = snd_mixer_selem_register (mixer->handle, NULL, NULL)) < 0) {
68 GST_WARNING ("Cannot register mixer elements: %s", snd_strerror (err));
72 if ((err = snd_mixer_load (mixer->handle)) < 0) {
73 GST_WARNING ("Cannot load mixer settings: %s", snd_strerror (err));
77 snd_mixer_set_callback_private (mixer->handle, mixer);
78 snd_mixer_set_callback (mixer->handle, gst_alsa_mixer_handle_callback);
80 /* now get the device name, any of this is not fatal */
81 g_free (mixer->cardname);
82 if ((err = snd_ctl_open (&ctl, mixer->device, 0)) < 0) {
83 GST_WARNING ("Cannot open CTL: %s", snd_strerror (err));
87 snd_ctl_card_info_malloc (&card_info);
88 if ((err = snd_ctl_card_info (ctl, card_info)) < 0) {
89 GST_WARNING ("Cannot get card info: %s", snd_strerror (err));
94 mixer->cardname = g_strdup (snd_ctl_card_info_get_name (card_info));
95 GST_DEBUG ("Card name = %s", GST_STR_NULL (mixer->cardname));
96 snd_ctl_card_info_free (card_info);
100 if (mixer->cardname == NULL) {
101 mixer->cardname = g_strdup ("Unknown");
102 GST_DEBUG ("Cannot find card name");
105 GST_INFO ("Successfully opened mixer for device '%s'.", mixer->device);
112 GST_WARNING ("Cannot open mixer: %s", snd_strerror (err));
113 mixer->handle = NULL;
118 snd_mixer_close (mixer->handle);
119 mixer->handle = NULL;
124 static snd_mixer_elem_t *
125 gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
127 snd_mixer_elem_t *element;
130 count = snd_mixer_get_count (handle);
132 g_static_rec_mutex_lock (mixer->rec_mutex);
134 /* Check if we have a playback mixer labelled as 'Master' */
135 element = snd_mixer_first_elem (handle);
136 for (i = 0; i < count; i++) {
137 if (snd_mixer_selem_has_playback_volume (element) &&
138 strcmp (snd_mixer_selem_get_name (element), "Master") == 0) {
139 g_static_rec_mutex_unlock (mixer->rec_mutex);
142 element = snd_mixer_elem_next (element);
145 /* If not, check if we have a playback mixer labelled as 'Front' */
146 element = snd_mixer_first_elem (handle);
147 for (i = 0; i < count; i++) {
148 if (snd_mixer_selem_has_playback_volume (element) &&
149 strcmp (snd_mixer_selem_get_name (element), "Front") == 0) {
150 g_static_rec_mutex_unlock (mixer->rec_mutex);
153 element = snd_mixer_elem_next (element);
156 /* If not, check if we have a playback mixer labelled as 'PCM' */
157 element = snd_mixer_first_elem (handle);
158 for (i = 0; i < count; i++) {
159 if (snd_mixer_selem_has_playback_volume (element) &&
160 strcmp (snd_mixer_selem_get_name (element), "PCM") == 0) {
161 g_static_rec_mutex_unlock (mixer->rec_mutex);
164 element = snd_mixer_elem_next (element);
167 /* If not, check if we have a playback mixer labelled as 'Speaker' */
168 element = snd_mixer_first_elem (handle);
169 for (i = 0; i < count; i++) {
170 if (snd_mixer_selem_has_playback_volume (element) &&
171 strcmp (snd_mixer_selem_get_name (element), "Speaker") == 0) {
174 element = snd_mixer_elem_next (element);
177 /* If not, check if we have a playback mixer with both volume and switch that
179 element = snd_mixer_first_elem (handle);
180 for (i = 0; i < count; i++) {
181 if (snd_mixer_selem_has_playback_volume (element) &&
182 snd_mixer_selem_has_playback_switch (element) &&
183 !snd_mixer_selem_is_playback_mono (element)) {
186 element = snd_mixer_elem_next (element);
189 /* If not, check if we have any playback mixer with both volume and switch */
190 element = snd_mixer_first_elem (handle);
191 for (i = 0; i < count; i++) {
192 if (snd_mixer_selem_has_playback_volume (element) &&
193 snd_mixer_selem_has_playback_switch (element)) {
194 g_static_rec_mutex_unlock (mixer->rec_mutex);
197 element = snd_mixer_elem_next (element);
200 /* If not, take any playback mixer with a volume control */
201 element = snd_mixer_first_elem (handle);
202 for (i = 0; i < count; i++) {
203 if (snd_mixer_selem_has_playback_volume (element)) {
204 g_static_rec_mutex_unlock (mixer->rec_mutex);
207 element = snd_mixer_elem_next (element);
210 g_static_rec_mutex_unlock (mixer->rec_mutex);
211 /* Looks like we're out of luck ... */
216 gst_alsa_mixer_update (GstAlsaMixer * mixer, snd_mixer_elem_t * elem)
220 g_return_if_fail (mixer != NULL);
222 g_static_rec_mutex_lock (mixer->rec_mutex);
224 for (item = mixer->tracklist; item != NULL; item = item->next) {
225 if (GST_IS_ALSA_MIXER_TRACK (item->data)) {
226 if (elem && (GST_ALSA_MIXER_TRACK (item->data)->element != elem))
229 gst_alsa_mixer_update_track (mixer, GST_ALSA_MIXER_TRACK (item->data));
230 } else if (GST_IS_ALSA_MIXER_OPTIONS (item->data)) {
231 if (elem && (GST_ALSA_MIXER_OPTIONS (item->data)->element != elem))
234 gst_alsa_mixer_update_option (mixer, GST_ALSA_MIXER_OPTIONS (item->data));
238 g_static_rec_mutex_unlock (mixer->rec_mutex);
242 gst_alsa_mixer_elem_handle_callback (snd_mixer_elem_t * elem, unsigned int mask)
244 GstAlsaMixer *mixer =
245 (GstAlsaMixer *) snd_mixer_elem_get_callback_private (elem);
247 GST_LOG ("ALSA elem cb");
249 g_return_val_if_fail (mixer != NULL, 1);
251 gst_alsa_mixer_update (mixer, elem);
257 gst_alsa_mixer_handle_callback (snd_mixer_t * handle, unsigned int mask,
258 snd_mixer_elem_t * elem)
260 GstAlsaMixer *mixer =
261 (GstAlsaMixer *) snd_mixer_get_callback_private (handle);
265 g_return_val_if_fail (mixer != NULL, 1);
267 /* Hopefully won't be call recursively and will handle pending elem events */
268 snd_mixer_handle_events (mixer->handle);
270 gst_alsa_mixer_update (mixer, elem);
276 gst_alsa_mixer_ensure_track_list (GstAlsaMixer * mixer)
279 snd_mixer_elem_t *element, *master;
282 g_return_if_fail (mixer->handle != NULL);
284 if (mixer->tracklist)
287 g_static_rec_mutex_lock (mixer->rec_mutex);
289 master = gst_alsa_mixer_find_master_mixer (mixer, mixer->handle);
291 count = snd_mixer_get_count (mixer->handle);
292 element = snd_mixer_first_elem (mixer->handle);
296 * Some ALSA tracks may have playback and capture capabilities.
297 * Here we model them as two separate GStreamer tracks.
300 for (i = 0; i < count; i++) {
301 GstMixerTrack *play_track = NULL;
302 GstMixerTrack *cap_track = NULL;
307 name = snd_mixer_selem_get_name (element);
309 /* prevent dup names */
310 for (item = mixer->tracklist; item != NULL; item = item->next) {
311 snd_mixer_elem_t *temp;
313 if (GST_IS_ALSA_MIXER_OPTIONS (item->data))
314 temp = GST_ALSA_MIXER_OPTIONS (item->data)->element;
316 temp = GST_ALSA_MIXER_TRACK (item->data)->element;
318 if (strcmp (name, snd_mixer_selem_get_name (temp)) == 0)
322 GST_LOG ("[%s] probing element #%u, mixer->dir=%u", name, i, mixer->dir);
324 if (mixer->dir & GST_ALSA_MIXER_PLAYBACK) {
325 gboolean has_playback_switch, has_playback_volume;
327 has_playback_switch = snd_mixer_selem_has_playback_switch (element);
328 has_playback_volume = snd_mixer_selem_has_playback_volume (element);
330 GST_LOG ("[%s] PLAYBACK: has_playback_volume=%d, has_playback_switch=%d"
331 "%s", name, has_playback_volume, has_playback_switch,
332 (element == master) ? " MASTER" : "");
334 if (has_playback_volume) {
335 gint flags = GST_MIXER_TRACK_OUTPUT;
337 if (element == master)
338 flags |= GST_MIXER_TRACK_MASTER;
340 play_track = gst_alsa_mixer_track_new (element, samename, i,
341 flags, FALSE, NULL, FALSE);
343 } else if (has_playback_switch) {
344 /* simple mute switch */
345 play_track = gst_alsa_mixer_track_new (element, samename, i,
346 GST_MIXER_TRACK_OUTPUT, TRUE, NULL, FALSE);
349 if (snd_mixer_selem_is_enumerated (element)) {
350 GstMixerOptions *opts = gst_alsa_mixer_options_new (element, i);
352 GST_LOG ("[%s] is enumerated (%d)", name, i);
353 mixer->tracklist = g_list_append (mixer->tracklist, opts);
357 if (mixer->dir & GST_ALSA_MIXER_CAPTURE) {
358 gboolean has_capture_switch, has_common_switch;
359 gboolean has_capture_volume, has_common_volume;
361 has_capture_switch = snd_mixer_selem_has_capture_switch (element);
362 has_common_switch = snd_mixer_selem_has_common_switch (element);
363 has_capture_volume = snd_mixer_selem_has_capture_volume (element);
364 has_common_volume = snd_mixer_selem_has_common_volume (element);
366 GST_LOG ("[%s] CAPTURE: has_capture_volume=%d, has_common_volume=%d, "
367 "has_capture_switch=%d, has_common_switch=%d, play_track=%p", name,
368 has_capture_volume, has_common_volume, has_capture_switch,
369 has_common_switch, play_track);
371 if (has_capture_volume && !(play_track && has_common_volume)) {
372 cap_track = gst_alsa_mixer_track_new (element, samename, i,
373 GST_MIXER_TRACK_INPUT, FALSE, NULL, play_track != NULL);
374 } else if (has_capture_switch && !(play_track && has_common_switch)) {
375 cap_track = gst_alsa_mixer_track_new (element, samename, i,
376 GST_MIXER_TRACK_INPUT, TRUE, NULL, play_track != NULL);
381 if (play_track && cap_track) {
382 GST_ALSA_MIXER_TRACK (play_track)->shared_mute =
383 GST_ALSA_MIXER_TRACK (cap_track);
384 GST_ALSA_MIXER_TRACK (cap_track)->shared_mute =
385 GST_ALSA_MIXER_TRACK (play_track);
389 mixer->tracklist = g_list_append (mixer->tracklist, play_track);
392 mixer->tracklist = g_list_append (mixer->tracklist, cap_track);
394 element = snd_mixer_elem_next (element);
397 for (item = mixer->tracklist; item != NULL; item = item->next) {
398 snd_mixer_elem_t *temp;
400 if (GST_IS_ALSA_MIXER_OPTIONS (item->data))
401 temp = GST_ALSA_MIXER_OPTIONS (item->data)->element;
403 temp = GST_ALSA_MIXER_TRACK (item->data)->element;
405 snd_mixer_elem_set_callback (temp, gst_alsa_mixer_elem_handle_callback);
406 snd_mixer_elem_set_callback_private (temp, mixer);
409 g_static_rec_mutex_unlock (mixer->rec_mutex);
413 task_monitor_alsa (gpointer data)
416 unsigned int nfds, rnfds;
417 unsigned short revents;
418 GstAlsaMixer *mixer = (GstAlsaMixer *) data;
420 g_static_rec_mutex_lock (mixer->rec_mutex);
422 nfds = snd_mixer_poll_descriptors_count (mixer->handle);
424 GST_ERROR ("snd_mixer_poll_descriptors_count <= 0: %d", nfds);
425 /* FIXME: sleep ? stop monitoring ? */
429 pfds = g_newa (struct pollfd, nfds + 1);
430 rnfds = snd_mixer_poll_descriptors (mixer->handle, pfds, nfds);
431 g_assert (rnfds <= nfds);
433 pfds[rnfds].fd = mixer->pfd[0];
434 pfds[rnfds].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
435 pfds[rnfds].revents = 0;
437 g_static_rec_mutex_unlock (mixer->rec_mutex);
439 GST_LOG ("task loop");
440 poll (pfds, rnfds + 1, -1);
442 g_static_rec_mutex_lock (mixer->rec_mutex);
444 snd_mixer_poll_descriptors_revents (mixer->handle, pfds, nfds, &revents);
445 if (revents & POLLIN || revents & POLLPRI) {
446 GST_DEBUG ("Handling events");
447 snd_mixer_handle_events (mixer->handle);
450 g_static_rec_mutex_unlock (mixer->rec_mutex);
456 gst_alsa_mixer_new (const char *device, GstAlsaMixerDirection dir)
458 GstAlsaMixer *ret = NULL;
460 g_return_val_if_fail (device != NULL, NULL);
462 ret = g_new0 (GstAlsaMixer, 1);
464 if (pipe (ret->pfd) == -1)
467 ret->rec_mutex = g_new (GStaticRecMutex, 1);
468 g_static_rec_mutex_init (ret->rec_mutex);
470 ret->task_mutex = g_new (GStaticRecMutex, 1);
471 g_static_rec_mutex_init (ret->task_mutex);
473 ret->task = gst_task_create (task_monitor_alsa, ret);
474 gst_task_set_lock (ret->task, ret->task_mutex);
476 ret->device = g_strdup (device);
479 if (!gst_alsa_mixer_open (ret))
482 if (gst_task_start (ret->task) == FALSE) {
483 GST_WARNING ("Could not start alsamixer task");
491 gst_alsa_mixer_free (ret);
497 gst_alsa_mixer_free (GstAlsaMixer * mixer)
499 g_return_if_fail (mixer != NULL);
502 if (write (mixer->pfd[1], "stop", 5) <= 0) {
503 GST_ERROR ("Cannot send " "stop" " to alsamixer task");
504 close (mixer->pfd[1]);
508 if (gst_task_join (mixer->task) == FALSE) {
509 GST_ERROR ("Cannot join alsamixer task");
512 gst_object_unref (mixer->task);
516 g_static_rec_mutex_free (mixer->task_mutex);
517 g_free (mixer->task_mutex);
518 mixer->task_mutex = NULL;
520 if (mixer->pfd[0] > 0) {
521 close (mixer->pfd[0]);
525 if (mixer->pfd[1] > 0) {
526 close (mixer->pfd[1]);
530 if (mixer->interface) {
531 g_object_unref (G_OBJECT (mixer->interface));
532 mixer->interface = NULL;
536 g_free (mixer->device);
537 mixer->device = NULL;
540 if (mixer->cardname) {
541 g_free (mixer->cardname);
542 mixer->cardname = NULL;
545 if (mixer->tracklist) {
546 g_list_foreach (mixer->tracklist, (GFunc) g_object_unref, NULL);
547 g_list_free (mixer->tracklist);
548 mixer->tracklist = NULL;
552 snd_mixer_close (mixer->handle);
553 mixer->handle = NULL;
556 g_static_rec_mutex_free (mixer->rec_mutex);
557 g_free (mixer->rec_mutex);
558 mixer->rec_mutex = NULL;
564 gst_alsa_mixer_list_tracks (GstAlsaMixer * mixer)
566 g_return_val_if_fail (mixer->handle != NULL, NULL);
568 gst_alsa_mixer_ensure_track_list (mixer);
570 return (const GList *) mixer->tracklist;
574 gst_alsa_mixer_get_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
578 GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
580 g_return_if_fail (mixer->handle != NULL);
582 g_static_rec_mutex_lock (mixer->rec_mutex);
584 gst_alsa_mixer_track_update (alsa_track);
586 if (track->flags & GST_MIXER_TRACK_OUTPUT) { /* return playback volume */
588 /* Is emulated mute flag activated? */
589 if (track->flags & GST_MIXER_TRACK_MUTE &&
590 !(alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH)) {
591 for (i = 0; i < track->num_channels; i++)
592 volumes[i] = alsa_track->volumes[i];
594 for (i = 0; i < track->num_channels; i++) {
597 snd_mixer_selem_get_playback_volume (alsa_track->element, i, &tmp);
598 alsa_track->volumes[i] = volumes[i] = (gint) tmp;
602 } else if (track->flags & GST_MIXER_TRACK_INPUT) { /* return capture volume */
604 /* Is emulated record flag activated? */
605 if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH ||
606 track->flags & GST_MIXER_TRACK_RECORD) {
607 for (i = 0; i < track->num_channels; i++) {
610 snd_mixer_selem_get_capture_volume (alsa_track->element, i, &tmp);
611 alsa_track->volumes[i] = volumes[i] = (gint) tmp;
614 for (i = 0; i < track->num_channels; i++)
615 volumes[i] = alsa_track->volumes[i];
618 g_static_rec_mutex_unlock (mixer->rec_mutex);
622 check_if_volumes_are_the_same (guint num_channels, gint * volumes)
626 if (num_channels <= 1)
629 for (i = 1; i < num_channels; i++) {
630 if (volumes[i] != volumes[0])
638 gst_alsa_mixer_set_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
641 GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
644 g_return_if_fail (mixer->handle != NULL);
646 g_static_rec_mutex_lock (mixer->rec_mutex);
648 gst_alsa_mixer_track_update (alsa_track);
650 if (track->flags & GST_MIXER_TRACK_OUTPUT) {
652 /* Is emulated mute flag activated? */
653 if (track->flags & GST_MIXER_TRACK_MUTE &&
654 !(alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH)) {
655 for (i = 0; i < track->num_channels; i++)
656 alsa_track->volumes[i] = volumes[i];
658 if (check_if_volumes_are_the_same (track->num_channels, volumes)) {
659 snd_mixer_selem_set_playback_volume_all (alsa_track->element,
661 for (i = 0; i < track->num_channels; i++)
662 alsa_track->volumes[i] = volumes[0];
664 for (i = 0; i < track->num_channels; i++) {
665 alsa_track->volumes[i] = volumes[i];
666 snd_mixer_selem_set_playback_volume (alsa_track->element, i,
672 } else if (track->flags & GST_MIXER_TRACK_INPUT) {
674 /* Is emulated record flag activated? */
675 if (track->flags & GST_MIXER_TRACK_RECORD ||
676 alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH) {
677 if (check_if_volumes_are_the_same (track->num_channels, volumes)) {
678 snd_mixer_selem_set_capture_volume_all (alsa_track->element,
680 for (i = 0; i < track->num_channels; i++)
681 alsa_track->volumes[i] = volumes[0];
683 for (i = 0; i < track->num_channels; i++) {
684 alsa_track->volumes[i] = volumes[i];
685 snd_mixer_selem_set_capture_volume (alsa_track->element, i,
690 for (i = 0; i < track->num_channels; i++)
691 alsa_track->volumes[i] = volumes[i];
694 g_static_rec_mutex_unlock (mixer->rec_mutex);
698 gst_alsa_mixer_set_mute (GstAlsaMixer * mixer, GstMixerTrack * track,
701 GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
703 g_return_if_fail (mixer->handle != NULL);
705 g_static_rec_mutex_lock (mixer->rec_mutex);
707 gst_alsa_mixer_track_update (alsa_track);
709 if (!!(mute) == !!(track->flags & GST_MIXER_TRACK_MUTE)) {
710 g_static_rec_mutex_unlock (mixer->rec_mutex);
714 track->flags |= GST_MIXER_TRACK_MUTE;
716 if (alsa_track->shared_mute)
717 ((GstMixerTrack *) (alsa_track->shared_mute))->flags |=
718 GST_MIXER_TRACK_MUTE;
720 track->flags &= ~GST_MIXER_TRACK_MUTE;
722 if (alsa_track->shared_mute)
723 ((GstMixerTrack *) (alsa_track->shared_mute))->flags &=
724 ~GST_MIXER_TRACK_MUTE;
727 if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH) {
728 snd_mixer_selem_set_playback_switch_all (alsa_track->element, mute ? 0 : 1);
731 GstAlsaMixerTrack *ctrl_track;
733 if ((track->flags & GST_MIXER_TRACK_INPUT)
734 && alsa_track->shared_mute != NULL)
735 ctrl_track = alsa_track->shared_mute;
737 ctrl_track = alsa_track;
739 for (i = 0; i < ((GstMixerTrack *) ctrl_track)->num_channels; i++) {
741 mute ? ((GstMixerTrack *) ctrl_track)->min_volume : ctrl_track->
743 snd_mixer_selem_set_playback_volume (ctrl_track->element, i, vol);
746 g_static_rec_mutex_unlock (mixer->rec_mutex);
750 gst_alsa_mixer_set_record (GstAlsaMixer * mixer,
751 GstMixerTrack * track, gboolean record)
753 GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
755 g_return_if_fail (mixer->handle != NULL);
757 g_static_rec_mutex_lock (mixer->rec_mutex);
759 gst_alsa_mixer_track_update (alsa_track);
761 if (!!(record) == !!(track->flags & GST_MIXER_TRACK_RECORD)) {
762 g_static_rec_mutex_unlock (mixer->rec_mutex);
767 track->flags |= GST_MIXER_TRACK_RECORD;
769 track->flags &= ~GST_MIXER_TRACK_RECORD;
772 if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH) {
773 snd_mixer_selem_set_capture_switch_all (alsa_track->element,
776 /* update all tracks in same exlusive cswitch group */
777 if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH_EXCL) {
780 for (item = mixer->tracklist; item != NULL; item = item->next) {
782 if (GST_IS_ALSA_MIXER_TRACK (item->data)) {
783 GstAlsaMixerTrack *item_alsa_track =
784 GST_ALSA_MIXER_TRACK (item->data);
786 if (item_alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH_EXCL &&
787 item_alsa_track->capture_group == alsa_track->capture_group) {
788 gst_alsa_mixer_track_update (item_alsa_track);
796 for (i = 0; i < track->num_channels; i++) {
797 long vol = record ? alsa_track->volumes[i] : track->min_volume;
799 snd_mixer_selem_set_capture_volume (alsa_track->element, i, vol);
802 g_static_rec_mutex_unlock (mixer->rec_mutex);
806 gst_alsa_mixer_set_option (GstAlsaMixer * mixer,
807 GstMixerOptions * opts, gchar * value)
809 gint idx = -1, n = 0;
811 GstAlsaMixerOptions *alsa_opts = GST_ALSA_MIXER_OPTIONS (opts);
813 g_return_if_fail (mixer->handle != NULL);
815 for (item = opts->values; item != NULL; item = item->next, n++) {
816 if (!strcmp (item->data, value)) {
824 g_static_rec_mutex_lock (mixer->rec_mutex);
825 snd_mixer_selem_set_enum_item (alsa_opts->element, 0, idx);
826 g_static_rec_mutex_unlock (mixer->rec_mutex);
830 gst_alsa_mixer_get_option (GstAlsaMixer * mixer, GstMixerOptions * opts)
834 GstAlsaMixerOptions *alsa_opts = GST_ALSA_MIXER_OPTIONS (opts);
836 g_return_val_if_fail (mixer->handle != NULL, NULL);
837 g_static_rec_mutex_lock (mixer->rec_mutex);
838 ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
839 g_static_rec_mutex_unlock (mixer->rec_mutex);
841 return g_list_nth_data (opts->values, idx);
843 return snd_strerror (ret); /* feeble attempt at error handling */
847 gst_alsa_mixer_get_mixer_flags (GstAlsaMixer * mixer)
849 g_return_val_if_fail (mixer != NULL, GST_MIXER_FLAG_NONE);
851 return GST_MIXER_FLAG_AUTO_NOTIFICATIONS;
855 gst_alsa_mixer_update_option (GstAlsaMixer * mixer,
856 GstAlsaMixerOptions * alsa_opts)
860 /* const */ gchar *option;
862 if (mixer->interface == NULL) {
863 GST_WARNING ("Cannot send update notifications, no GstMixer * given");
866 g_static_rec_mutex_lock (mixer->rec_mutex);
867 ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
868 g_static_rec_mutex_unlock (mixer->rec_mutex);
870 option = g_list_nth_data (GST_MIXER_OPTIONS (alsa_opts)->values, idx);
871 gst_mixer_option_changed (mixer->interface, GST_MIXER_OPTIONS (alsa_opts),
877 gst_alsa_mixer_update_track (GstAlsaMixer * mixer,
878 GstAlsaMixerTrack * alsa_track)
880 GstMixerTrack *track = (GstMixerTrack *) alsa_track;
886 GST_DEBUG ("Updating track %" GST_PTR_FORMAT, alsa_track);
888 if (mixer->interface == NULL) {
889 GST_WARNING ("Cannot send update notifications, no GstMixer * given");
893 old_mute = !!(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE));
894 old_record = !!(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD));
895 old_volumes = g_new (gint, track->num_channels);
896 n_channels = track->num_channels;
897 memcpy (old_volumes, alsa_track->volumes,
898 sizeof (gint) * track->num_channels);
900 gst_alsa_mixer_track_update (alsa_track);
903 !!(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD))) {
904 gst_mixer_record_toggled (mixer->interface, track,
905 !!GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD));
907 if (old_mute != !!(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE))) {
908 gst_mixer_mute_toggled (mixer->interface, track,
909 !!GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE));
912 n_channels = MIN (n_channels, track->num_channels);
913 for (i = 0; i < n_channels; i++) {
914 if (old_volumes[i] != alsa_track->volumes[i]) {
915 gst_mixer_volume_changed (mixer->interface, track, alsa_track->volumes);
919 g_free (old_volumes);
922 /* utility function for gstalsamixerelement to set the interface */
924 _gst_alsa_mixer_set_interface (GstAlsaMixer * mixer, GstMixer * interface)
926 g_return_if_fail (mixer != NULL && mixer->interface == NULL);
927 g_return_if_fail (interface != NULL);
929 mixer->interface = g_object_ref (G_OBJECT (interface));