Blame SOURCES/0015-audio-add-functions-to-set-volume-mute-with-alsa.patch

a547b4
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a547b4
From: Victor Toso <victortoso@redhat.com>
a547b4
Date: Fri, 6 Mar 2015 18:14:21 +0100
a547b4
Subject: [PATCH] audio: add functions to set volume/mute with alsa
a547b4
a547b4
This patch includes the vdagent-audio.[ch] files in order to
a547b4
communicate with backend audio server.
a547b4
a547b4
The two functions provide a way to set volume and mute in the guest
a547b4
by connecting to default mixer control in alsa which is 'Master' for
a547b4
playback and 'Capture' for record.
a547b4
---
a547b4
 Makefile.am         |  12 +++-
a547b4
 configure.ac        |   1 +
a547b4
 src/vdagent-audio.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++
a547b4
 src/vdagent-audio.h |  27 +++++++++
a547b4
 4 files changed, 205 insertions(+), 3 deletions(-)
a547b4
 create mode 100644 src/vdagent-audio.c
a547b4
 create mode 100644 src/vdagent-audio.h
a547b4
a547b4
diff --git a/Makefile.am b/Makefile.am
a547b4
index a06a259..2effe4e 100644
a547b4
--- a/Makefile.am
a547b4
+++ b/Makefile.am
a547b4
@@ -4,9 +4,14 @@ NULL =
a547b4
 bin_PROGRAMS = src/spice-vdagent
a547b4
 sbin_PROGRAMS = src/spice-vdagentd
a547b4
 
a547b4
-src_spice_vdagent_CFLAGS = $(X_CFLAGS) $(SPICE_CFLAGS) $(GLIB2_CFLAGS)
a547b4
-src_spice_vdagent_LDADD = $(X_LIBS) $(SPICE_LIBS) $(GLIB2_LIBS)
a547b4
-src_spice_vdagent_SOURCES = src/vdagent.c src/vdagent-x11.c src/vdagent-x11-randr.c src/vdagent-file-xfers.c src/udscs.c
a547b4
+src_spice_vdagent_CFLAGS = $(X_CFLAGS) $(SPICE_CFLAGS) $(GLIB2_CFLAGS) $(ALSA_CFLAGS)
a547b4
+src_spice_vdagent_LDADD = $(X_LIBS) $(SPICE_LIBS) $(GLIB2_LIBS) $(ALSA_LIBS)
a547b4
+src_spice_vdagent_SOURCES = src/vdagent.c \
a547b4
+                            src/vdagent-x11.c \
a547b4
+                            src/vdagent-x11-randr.c \
a547b4
+                            src/vdagent-file-xfers.c \
a547b4
+                            src/vdagent-audio.c \
a547b4
+                            src/udscs.c
a547b4
 
a547b4
 src_spice_vdagentd_CFLAGS = $(DBUS_CFLAGS) $(LIBSYSTEMD_LOGIN_CFLAGS) \
a547b4
   $(PCIACCESS_CFLAGS) $(SPICE_CFLAGS) $(GLIB2_CFLAGS) $(PIE_CFLAGS)
a547b4
@@ -30,6 +35,7 @@ endif
a547b4
 noinst_HEADERS = src/glib-compat.h \
a547b4
                  src/session-info.h \
a547b4
                  src/udscs.h \
a547b4
+                 src/vdagent-audio.h \
a547b4
                  src/vdagent-file-xfers.h \
a547b4
                  src/vdagent-virtio-port.h \
a547b4
                  src/vdagent-x11.h \
a547b4
diff --git a/configure.ac b/configure.ac
a547b4
index a1ce6c0..377fc74 100644
a547b4
--- a/configure.ac
a547b4
+++ b/configure.ac
a547b4
@@ -79,6 +79,7 @@ AC_ARG_ENABLE([static-uinput],
a547b4
 PKG_CHECK_MODULES([GLIB2], [glib-2.0 >= 2.12])
a547b4
 PKG_CHECK_MODULES(X, [xfixes xrandr >= 1.3 xinerama x11])
a547b4
 PKG_CHECK_MODULES(SPICE, [spice-protocol >= 0.12.5])
a547b4
+PKG_CHECK_MODULES(ALSA, [alsa >= 1.0.22])
a547b4
 
a547b4
 if test "$with_session_info" = "auto" || test "$with_session_info" = "systemd"; then
a547b4
     PKG_CHECK_MODULES([LIBSYSTEMD_LOGIN],
a547b4
diff --git a/src/vdagent-audio.c b/src/vdagent-audio.c
a547b4
new file mode 100644
a547b4
index 0000000..6b11cd8
a547b4
--- /dev/null
a547b4
+++ b/src/vdagent-audio.c
a547b4
@@ -0,0 +1,168 @@
a547b4
+/*  vdagent-audio.c vdagentd audio handling code
a547b4
+
a547b4
+    Copyright 2015 Red Hat, Inc.
a547b4
+
a547b4
+    This program is free software: you can redistribute it and/or modify
a547b4
+    it under the terms of the GNU General Public License as published by
a547b4
+    the Free Software Foundation, either version 3 of the License, or
a547b4
+    (at your option) any later version.
a547b4
+
a547b4
+    This program is distributed in the hope that it will be useful,
a547b4
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
a547b4
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a547b4
+    GNU General Public License for more details.
a547b4
+
a547b4
+    You should have received a copy of the GNU General Public License
a547b4
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
a547b4
+*/
a547b4
+
a547b4
+#ifdef HAVE_CONFIG_H
a547b4
+#include <config.h>
a547b4
+#endif
a547b4
+
a547b4
+#include <glib.h>
a547b4
+#include <syslog.h>
a547b4
+#include <stdbool.h>
a547b4
+#include <alsa/asoundlib.h>
a547b4
+#include <alsa/mixer.h>
a547b4
+#include <alsa/error.h>
a547b4
+#include "vdagent-audio.h"
a547b4
+
a547b4
+#define ALSA_MUTE   0
a547b4
+#define ALSA_UNMUTE 1
a547b4
+
a547b4
+static snd_mixer_elem_t *
a547b4
+get_alsa_default_mixer_by_name(snd_mixer_t **handle, const char *name)
a547b4
+{
a547b4
+    snd_mixer_selem_id_t *sid;
a547b4
+    int err = 0;
a547b4
+
a547b4
+    if ((err = snd_mixer_open(handle, 0)) < 0)
a547b4
+        goto fail;
a547b4
+
a547b4
+    if ((err = snd_mixer_attach(*handle, "default")) < 0)
a547b4
+        goto fail;
a547b4
+
a547b4
+    if ((err = snd_mixer_selem_register(*handle, NULL, NULL)) < 0)
a547b4
+        goto fail;
a547b4
+
a547b4
+    if ((err = snd_mixer_load(*handle)) < 0)
a547b4
+        goto fail;
a547b4
+
a547b4
+    snd_mixer_selem_id_alloca(&sid;;
a547b4
+    snd_mixer_selem_id_set_index(sid, 0);
a547b4
+    snd_mixer_selem_id_set_name(sid, name);
a547b4
+    return snd_mixer_find_selem(*handle, sid);
a547b4
+
a547b4
+fail:
a547b4
+    syslog(LOG_WARNING, "%s fail: %s", __func__, snd_strerror(err));
a547b4
+    return NULL;
a547b4
+}
a547b4
+
a547b4
+static bool set_alsa_capture(uint8_t mute, uint8_t nchannels, uint16_t *volume)
a547b4
+{
a547b4
+    snd_mixer_t *handle = NULL;
a547b4
+    snd_mixer_elem_t *e;
a547b4
+    long min, max, vol;
a547b4
+    bool ret = true;
a547b4
+    int alsa_mute;
a547b4
+
a547b4
+    e = get_alsa_default_mixer_by_name (&handle, "Capture");
a547b4
+    if (e == NULL) {
a547b4
+        syslog(LOG_WARNING, "vdagent-audio: can't get default alsa mixer");
a547b4
+        ret = false;
a547b4
+        goto end;
a547b4
+    }
a547b4
+
a547b4
+    alsa_mute = (mute) ? ALSA_MUTE : ALSA_UNMUTE;
a547b4
+    snd_mixer_selem_set_capture_switch_all(e, alsa_mute);
a547b4
+
a547b4
+    snd_mixer_selem_get_capture_volume_range(e, &min, &max;;
a547b4
+    switch (nchannels) {
a547b4
+    case 1: /* MONO */
a547b4
+        vol = CLAMP(volume[0], min, max);
a547b4
+        snd_mixer_selem_set_capture_volume(e, SND_MIXER_SCHN_MONO, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (capture-mono) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        break;
a547b4
+    case 2: /* LEFT-RIGHT */
a547b4
+        vol = CLAMP(volume[0], min, max);
a547b4
+        snd_mixer_selem_set_capture_volume(e, SND_MIXER_SCHN_FRONT_LEFT, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (capture-left) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        vol = CLAMP(volume[1], min, max);
a547b4
+        snd_mixer_selem_set_capture_volume(e, SND_MIXER_SCHN_FRONT_RIGHT, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (capture-right) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        break;
a547b4
+    default:
a547b4
+        syslog(LOG_WARNING, "vdagent-audio: number of channels not supported");
a547b4
+        ret = false;
a547b4
+    }
a547b4
+end:
a547b4
+    if (handle != NULL)
a547b4
+        snd_mixer_close(handle);
a547b4
+    return ret;
a547b4
+}
a547b4
+
a547b4
+static bool set_alsa_playback (uint8_t mute, uint8_t nchannels, uint16_t *volume)
a547b4
+{
a547b4
+    snd_mixer_t *handle = NULL;
a547b4
+    snd_mixer_elem_t* e;
a547b4
+    long min, max, vol;
a547b4
+    bool ret = true;
a547b4
+    int alsa_mute;
a547b4
+
a547b4
+    e = get_alsa_default_mixer_by_name (&handle, "Master");
a547b4
+    if (e == NULL) {
a547b4
+        syslog(LOG_WARNING, "vdagent-audio: can't get default alsa mixer");
a547b4
+        ret = false;
a547b4
+        goto end;
a547b4
+    }
a547b4
+
a547b4
+    alsa_mute = (mute) ? ALSA_MUTE : ALSA_UNMUTE;
a547b4
+    snd_mixer_selem_set_playback_switch_all(e, alsa_mute);
a547b4
+
a547b4
+    snd_mixer_selem_get_playback_volume_range(e, &min, &max;;
a547b4
+    switch (nchannels) {
a547b4
+    case 1: /* MONO */
a547b4
+        vol = CLAMP(volume[0], min, max);
a547b4
+        snd_mixer_selem_set_playback_volume(e, SND_MIXER_SCHN_MONO, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (playback-mono) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        break;
a547b4
+    case 2: /* LEFT-RIGHT */
a547b4
+        vol = CLAMP(volume[0], min, max);
a547b4
+        snd_mixer_selem_set_playback_volume(e, SND_MIXER_SCHN_FRONT_LEFT, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (playback-left) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        vol = CLAMP(volume[1], min, max);
a547b4
+        snd_mixer_selem_set_playback_volume(e, SND_MIXER_SCHN_FRONT_RIGHT, vol);
a547b4
+        syslog(LOG_DEBUG, "vdagent-audio: (playback-right) %lu (%%%0.2f)",
a547b4
+               vol, (float) (100*vol/max));
a547b4
+        break;
a547b4
+    default:
a547b4
+        syslog(LOG_WARNING, "vdagent-audio: number of channels not supported");
a547b4
+        ret = false;
a547b4
+    }
a547b4
+end:
a547b4
+    if (handle != NULL)
a547b4
+        snd_mixer_close(handle);
a547b4
+    return ret;
a547b4
+}
a547b4
+
a547b4
+void vdagent_audio_playback_sync(uint8_t mute, uint8_t nchannels, uint16_t *volume)
a547b4
+{
a547b4
+    syslog(LOG_DEBUG, "%s mute=%s nchannels=%u",
a547b4
+           __func__, (mute) ? "yes" : "no", nchannels);
a547b4
+    if (set_alsa_playback (mute, nchannels, volume) == false)
a547b4
+        syslog(LOG_WARNING, "Fail to sync playback volume");
a547b4
+}
a547b4
+
a547b4
+void vdagent_audio_record_sync(uint8_t mute, uint8_t nchannels, uint16_t *volume)
a547b4
+{
a547b4
+    syslog(LOG_DEBUG, "%s mute=%s nchannels=%u",
a547b4
+           __func__, (mute) ? "yes" : "no", nchannels);
a547b4
+    if (set_alsa_capture (mute, nchannels, volume) == false)
a547b4
+        syslog(LOG_WARNING, "Fail to sync record volume");
a547b4
+}
a547b4
diff --git a/src/vdagent-audio.h b/src/vdagent-audio.h
a547b4
new file mode 100644
a547b4
index 0000000..6f29d4b
a547b4
--- /dev/null
a547b4
+++ b/src/vdagent-audio.h
a547b4
@@ -0,0 +1,27 @@
a547b4
+/*  vdagent-audio.h vdagentd audio handling header
a547b4
+
a547b4
+    Copyright 2015 Red Hat, Inc.
a547b4
+
a547b4
+    This program is free software: you can redistribute it and/or modify
a547b4
+    it under the terms of the GNU General Public License as published by
a547b4
+    the Free Software Foundation, either version 3 of the License, or
a547b4
+    (at your option) any later version.
a547b4
+
a547b4
+    This program is distributed in the hope that it will be useful,
a547b4
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
a547b4
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a547b4
+    GNU General Public License for more details.
a547b4
+
a547b4
+    You should have received a copy of the GNU General Public License
a547b4
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
a547b4
+*/
a547b4
+#ifndef __VDAGENT_AUDIO_H
a547b4
+#define __VDAGENT_AUDIO_H
a547b4
+
a547b4
+#include <stdio.h>
a547b4
+#include <stdint.h>
a547b4
+
a547b4
+void vdagent_audio_playback_sync(uint8_t mute, uint8_t nchannels, uint16_t *volume);
a547b4
+void vdagent_audio_record_sync(uint8_t mute, uint8_t nchannels, uint16_t *volume);
a547b4
+
a547b4
+#endif