2010-11-11 13:07:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* maintained by Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 or
|
|
|
|
* (at your option) version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 18:33:52 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-03-15 15:36:13 +01:00
|
|
|
#include "qemu/host-utils.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2015-03-17 18:29:20 +01:00
|
|
|
#include "qemu/error-report.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2023-09-22 19:13:44 +02:00
|
|
|
#include "qapi/error.h"
|
2010-11-09 17:29:46 +01:00
|
|
|
#include "ui/qemu-spice.h"
|
|
|
|
|
|
|
|
#define AUDIO_CAP "spice"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "audio_int.h"
|
|
|
|
|
2014-01-02 16:25:56 +01:00
|
|
|
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
|
|
|
#define LINE_OUT_SAMPLES (480 * 4)
|
|
|
|
#else
|
|
|
|
#define LINE_OUT_SAMPLES (256 * 4)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
|
|
#define LINE_IN_SAMPLES (480 * 4)
|
|
|
|
#else
|
|
|
|
#define LINE_IN_SAMPLES (256 * 4)
|
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
|
|
|
|
typedef struct SpiceVoiceOut {
|
|
|
|
HWVoiceOut hw;
|
|
|
|
SpicePlaybackInstance sin;
|
2019-09-19 23:24:21 +02:00
|
|
|
RateCtl rate;
|
2010-11-09 17:29:46 +01:00
|
|
|
int active;
|
|
|
|
uint32_t *frame;
|
2019-09-19 23:24:17 +02:00
|
|
|
uint32_t fpos;
|
2010-11-09 17:29:46 +01:00
|
|
|
uint32_t fsize;
|
|
|
|
} SpiceVoiceOut;
|
|
|
|
|
|
|
|
typedef struct SpiceVoiceIn {
|
|
|
|
HWVoiceIn hw;
|
|
|
|
SpiceRecordInstance sin;
|
2019-09-19 23:24:21 +02:00
|
|
|
RateCtl rate;
|
2010-11-09 17:29:46 +01:00
|
|
|
int active;
|
|
|
|
} SpiceVoiceIn;
|
|
|
|
|
|
|
|
static const SpicePlaybackInterface playback_sif = {
|
|
|
|
.base.type = SPICE_INTERFACE_PLAYBACK,
|
|
|
|
.base.description = "playback",
|
|
|
|
.base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
|
|
|
|
.base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SpiceRecordInterface record_sif = {
|
|
|
|
.base.type = SPICE_INTERFACE_RECORD,
|
|
|
|
.base.description = "record",
|
|
|
|
.base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
|
|
|
|
.base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
|
|
|
|
};
|
|
|
|
|
2023-09-22 19:13:44 +02:00
|
|
|
static void *spice_audio_init(Audiodev *dev, Error **errp)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
if (!using_spice) {
|
2023-09-22 19:13:44 +02:00
|
|
|
error_setg(errp, "Cannot use spice audio without -spice");
|
2010-11-09 17:29:46 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-09-22 19:13:44 +02:00
|
|
|
|
2010-11-09 17:29:46 +01:00
|
|
|
return &spice_audio_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void spice_audio_fini (void *opaque)
|
|
|
|
{
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* playback */
|
|
|
|
|
2015-06-03 23:03:47 +02:00
|
|
|
static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
|
|
|
|
void *drv_opaque)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
|
|
|
struct audsettings settings;
|
|
|
|
|
2014-01-02 16:25:56 +01:00
|
|
|
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
|
|
|
settings.freq = spice_server_get_best_playback_rate(NULL);
|
|
|
|
#else
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
|
2014-01-02 16:25:56 +01:00
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
|
2019-03-08 23:34:13 +01:00
|
|
|
settings.fmt = AUDIO_FORMAT_S16;
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
|
|
|
|
|
|
|
audio_pcm_init_info (&hw->info, &settings);
|
|
|
|
hw->samples = LINE_OUT_SAMPLES;
|
|
|
|
out->active = 0;
|
|
|
|
|
|
|
|
out->sin.base.sif = &playback_sif.base;
|
2020-10-19 09:52:15 +02:00
|
|
|
qemu_spice.add_interface(&out->sin.base);
|
2014-01-02 16:25:56 +01:00
|
|
|
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
|
|
|
spice_server_set_playback_rate(&out->sin, settings.freq);
|
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void line_out_fini (HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
|
|
|
|
|
|
|
spice_server_remove_interface (&out->sin.base);
|
|
|
|
}
|
|
|
|
|
2022-09-23 20:36:34 +02:00
|
|
|
static size_t line_out_get_free(HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
|
|
|
|
|
|
|
|
return audio_rate_peek_bytes(&out->rate, &hw->info);
|
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
2019-09-19 23:24:17 +02:00
|
|
|
SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
|
2010-11-09 17:29:46 +01:00
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
if (!out->frame) {
|
|
|
|
spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
|
|
|
|
out->fpos = 0;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
if (out->frame) {
|
2020-09-20 19:17:23 +02:00
|
|
|
*size = MIN((out->fsize - out->fpos) << 2, *size);
|
2019-09-19 23:24:17 +02:00
|
|
|
}
|
2020-09-20 19:17:23 +02:00
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
return out->frame + out->fpos;
|
|
|
|
}
|
2010-11-09 17:29:46 +01:00
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
|
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
|
|
|
|
|
2022-09-23 20:36:34 +02:00
|
|
|
audio_rate_add_bytes(&out->rate, size);
|
|
|
|
|
2020-09-20 19:17:21 +02:00
|
|
|
if (buf) {
|
|
|
|
assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
|
|
|
|
out->fpos += size >> 2;
|
2019-09-19 23:24:17 +02:00
|
|
|
|
2020-09-20 19:17:21 +02:00
|
|
|
if (out->fpos == out->fsize) { /* buffer full */
|
|
|
|
spice_server_playback_put_samples(&out->sin, out->frame);
|
|
|
|
out->frame = NULL;
|
|
|
|
}
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
2019-09-19 23:24:17 +02:00
|
|
|
|
|
|
|
return size;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
static void line_out_enable(HWVoiceOut *hw, bool enable)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
if (enable) {
|
2010-11-09 17:29:46 +01:00
|
|
|
if (out->active) {
|
2019-09-19 23:24:22 +02:00
|
|
|
return;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
out->active = 1;
|
2019-09-19 23:24:21 +02:00
|
|
|
audio_rate_start(&out->rate);
|
2010-11-09 17:29:46 +01:00
|
|
|
spice_server_playback_start (&out->sin);
|
2019-09-19 23:24:22 +02:00
|
|
|
} else {
|
2010-11-09 17:29:46 +01:00
|
|
|
if (!out->active) {
|
2019-09-19 23:24:22 +02:00
|
|
|
return;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
out->active = 0;
|
|
|
|
if (out->frame) {
|
2019-09-19 23:24:17 +02:00
|
|
|
memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
|
2010-11-09 17:29:46 +01:00
|
|
|
spice_server_playback_put_samples (&out->sin, out->frame);
|
2019-09-19 23:24:17 +02:00
|
|
|
out->frame = NULL;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
spice_server_playback_stop (&out->sin);
|
|
|
|
}
|
2019-09-19 23:24:22 +02:00
|
|
|
}
|
2012-04-17 14:32:40 +02:00
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
|
2019-10-13 21:58:01 +02:00
|
|
|
static void line_out_volume(HWVoiceOut *hw, Volume *vol)
|
2019-09-19 23:24:22 +02:00
|
|
|
{
|
|
|
|
SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
|
|
|
|
uint16_t svol[2];
|
|
|
|
|
2019-10-13 21:58:01 +02:00
|
|
|
assert(vol->channels == 2);
|
|
|
|
svol[0] = vol->vol[0] * 257;
|
|
|
|
svol[1] = vol->vol[1] * 257;
|
2019-09-19 23:24:22 +02:00
|
|
|
spice_server_playback_set_volume(&out->sin, 2, svol);
|
|
|
|
spice_server_playback_set_mute(&out->sin, vol->mute);
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
2019-09-19 23:24:22 +02:00
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
|
|
|
|
/* record */
|
|
|
|
|
2015-06-03 23:03:47 +02:00
|
|
|
static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
|
|
|
struct audsettings settings;
|
|
|
|
|
2014-01-02 16:25:56 +01:00
|
|
|
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
|
|
settings.freq = spice_server_get_best_record_rate(NULL);
|
|
|
|
#else
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.freq = SPICE_INTERFACE_RECORD_FREQ;
|
2014-01-02 16:25:56 +01:00
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
|
2019-03-08 23:34:13 +01:00
|
|
|
settings.fmt = AUDIO_FORMAT_S16;
|
2010-11-09 17:29:46 +01:00
|
|
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
|
|
|
|
|
|
|
audio_pcm_init_info (&hw->info, &settings);
|
|
|
|
hw->samples = LINE_IN_SAMPLES;
|
|
|
|
in->active = 0;
|
|
|
|
|
|
|
|
in->sin.base.sif = &record_sif.base;
|
2020-10-19 09:52:15 +02:00
|
|
|
qemu_spice.add_interface(&in->sin.base);
|
2014-01-02 16:25:56 +01:00
|
|
|
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
|
|
|
|
spice_server_set_record_rate(&in->sin, settings.freq);
|
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void line_in_fini (HWVoiceIn *hw)
|
|
|
|
{
|
|
|
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
|
|
|
|
|
|
|
spice_server_remove_interface (&in->sin.base);
|
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
2022-09-23 20:36:36 +02:00
|
|
|
uint64_t to_read = audio_rate_get_bytes(&in->rate, &hw->info, len) >> 2;
|
2019-09-19 23:24:17 +02:00
|
|
|
size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
|
2010-11-09 17:29:46 +01:00
|
|
|
|
2022-09-23 20:36:35 +02:00
|
|
|
/*
|
|
|
|
* If the client didn't send new frames, it most likely disconnected.
|
|
|
|
* Generate silence in this case to avoid a stalled audio stream.
|
|
|
|
*/
|
2010-11-09 17:29:46 +01:00
|
|
|
if (ready == 0) {
|
2019-09-19 23:24:17 +02:00
|
|
|
memset(buf, 0, to_read << 2);
|
|
|
|
ready = to_read;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:17 +02:00
|
|
|
return ready << 2;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
static void line_in_enable(HWVoiceIn *hw, bool enable)
|
2010-11-09 17:29:46 +01:00
|
|
|
{
|
|
|
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
if (enable) {
|
2010-11-09 17:29:46 +01:00
|
|
|
if (in->active) {
|
2019-09-19 23:24:22 +02:00
|
|
|
return;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
in->active = 1;
|
2019-09-19 23:24:21 +02:00
|
|
|
audio_rate_start(&in->rate);
|
2010-11-09 17:29:46 +01:00
|
|
|
spice_server_record_start (&in->sin);
|
2019-09-19 23:24:22 +02:00
|
|
|
} else {
|
2010-11-09 17:29:46 +01:00
|
|
|
if (!in->active) {
|
2019-09-19 23:24:22 +02:00
|
|
|
return;
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
|
|
|
in->active = 0;
|
|
|
|
spice_server_record_stop (&in->sin);
|
|
|
|
}
|
2019-09-19 23:24:22 +02:00
|
|
|
}
|
2012-04-17 14:32:40 +02:00
|
|
|
|
2019-09-19 23:24:22 +02:00
|
|
|
#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
|
2019-10-13 21:58:01 +02:00
|
|
|
static void line_in_volume(HWVoiceIn *hw, Volume *vol)
|
2019-09-19 23:24:22 +02:00
|
|
|
{
|
|
|
|
SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
|
|
|
|
uint16_t svol[2];
|
|
|
|
|
2019-10-13 21:58:01 +02:00
|
|
|
assert(vol->channels == 2);
|
|
|
|
svol[0] = vol->vol[0] * 257;
|
|
|
|
svol[1] = vol->vol[1] * 257;
|
2019-09-19 23:24:22 +02:00
|
|
|
spice_server_record_set_volume(&in->sin, 2, svol);
|
|
|
|
spice_server_record_set_mute(&in->sin, vol->mute);
|
2010-11-09 17:29:46 +01:00
|
|
|
}
|
2019-09-19 23:24:22 +02:00
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
|
|
|
|
static struct audio_pcm_ops audio_callbacks = {
|
|
|
|
.init_out = line_out_init,
|
|
|
|
.fini_out = line_out_fini,
|
2019-09-19 23:24:17 +02:00
|
|
|
.write = audio_generic_write,
|
2022-09-23 20:36:34 +02:00
|
|
|
.buffer_get_free = line_out_get_free,
|
2019-09-19 23:24:17 +02:00
|
|
|
.get_buffer_out = line_out_get_buffer,
|
|
|
|
.put_buffer_out = line_out_put_buffer,
|
2019-09-19 23:24:22 +02:00
|
|
|
.enable_out = line_out_enable,
|
|
|
|
#if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
|
|
|
|
(SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
|
|
|
|
.volume_out = line_out_volume,
|
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
|
|
|
|
.init_in = line_in_init,
|
|
|
|
.fini_in = line_in_fini,
|
2019-09-19 23:24:17 +02:00
|
|
|
.read = line_in_read,
|
2021-01-10 11:02:24 +01:00
|
|
|
.run_buffer_in = audio_generic_run_buffer_in,
|
2019-09-19 23:24:22 +02:00
|
|
|
.enable_in = line_in_enable,
|
|
|
|
#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
|
|
|
|
.volume_in = line_in_volume,
|
|
|
|
#endif
|
2010-11-09 17:29:46 +01:00
|
|
|
};
|
|
|
|
|
2018-03-06 08:40:47 +01:00
|
|
|
static struct audio_driver spice_audio_driver = {
|
2010-11-09 17:29:46 +01:00
|
|
|
.name = "spice",
|
|
|
|
.descr = "spice audio driver",
|
|
|
|
.init = spice_audio_init,
|
|
|
|
.fini = spice_audio_fini,
|
|
|
|
.pcm_ops = &audio_callbacks,
|
|
|
|
.max_voices_out = 1,
|
|
|
|
.max_voices_in = 1,
|
|
|
|
.voice_size_out = sizeof (SpiceVoiceOut),
|
|
|
|
.voice_size_in = sizeof (SpiceVoiceIn),
|
|
|
|
};
|
|
|
|
|
2018-03-06 08:40:47 +01:00
|
|
|
static void register_audio_spice(void)
|
|
|
|
{
|
|
|
|
audio_driver_register(&spice_audio_driver);
|
|
|
|
}
|
|
|
|
type_init(register_audio_spice);
|
2021-06-24 12:38:10 +02:00
|
|
|
|
|
|
|
module_dep("ui-spice-core");
|