blob: a8d370fe6f31882b5f6b38fb87deea256a6100a6 [file] [log] [blame]
Gerd Hoffmanncf2c1832010-11-11 13:07:52 +01001/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
4 * maintained by Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydell6086a562016-01-18 17:33:52 +000020#include "qemu/osdep.h"
Paolo Bonzini87776ab2016-03-15 15:36:13 +010021#include "qemu/host-utils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020022#include "qemu/module.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010023#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/timer.h"
Gerd Hoffmann3e313752010-11-09 17:29:46 +010025#include "ui/qemu-spice.h"
26
27#define AUDIO_CAP "spice"
28#include "audio.h"
29#include "audio_int.h"
30
Jeremy White795ca112014-01-02 09:25:56 -060031#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
32#define LINE_OUT_SAMPLES (480 * 4)
33#else
34#define LINE_OUT_SAMPLES (256 * 4)
35#endif
36
37#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
38#define LINE_IN_SAMPLES (480 * 4)
39#else
40#define LINE_IN_SAMPLES (256 * 4)
41#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +010042
Gerd Hoffmann3e313752010-11-09 17:29:46 +010043typedef struct SpiceVoiceOut {
44 HWVoiceOut hw;
45 SpicePlaybackInstance sin;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +020046 RateCtl rate;
Gerd Hoffmann3e313752010-11-09 17:29:46 +010047 int active;
48 uint32_t *frame;
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +020049 uint32_t fpos;
Gerd Hoffmann3e313752010-11-09 17:29:46 +010050 uint32_t fsize;
51} SpiceVoiceOut;
52
53typedef struct SpiceVoiceIn {
54 HWVoiceIn hw;
55 SpiceRecordInstance sin;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +020056 RateCtl rate;
Gerd Hoffmann3e313752010-11-09 17:29:46 +010057 int active;
Gerd Hoffmann3e313752010-11-09 17:29:46 +010058} SpiceVoiceIn;
59
60static const SpicePlaybackInterface playback_sif = {
61 .base.type = SPICE_INTERFACE_PLAYBACK,
62 .base.description = "playback",
63 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
64 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
65};
66
67static const SpiceRecordInterface record_sif = {
68 .base.type = SPICE_INTERFACE_RECORD,
69 .base.description = "record",
70 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
71 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
72};
73
Kővágó, Zoltán71830222019-03-08 23:34:15 +010074static void *spice_audio_init(Audiodev *dev)
Gerd Hoffmann3e313752010-11-09 17:29:46 +010075{
76 if (!using_spice) {
77 return NULL;
78 }
79 return &spice_audio_init;
80}
81
82static void spice_audio_fini (void *opaque)
83{
84 /* nothing */
85}
86
Gerd Hoffmann3e313752010-11-09 17:29:46 +010087/* playback */
88
Kővágó, Zoltán5706db12015-06-03 23:03:47 +020089static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
90 void *drv_opaque)
Gerd Hoffmann3e313752010-11-09 17:29:46 +010091{
92 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
93 struct audsettings settings;
94
Jeremy White795ca112014-01-02 09:25:56 -060095#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
96 settings.freq = spice_server_get_best_playback_rate(NULL);
97#else
Gerd Hoffmann3e313752010-11-09 17:29:46 +010098 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
Jeremy White795ca112014-01-02 09:25:56 -060099#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100100 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100101 settings.fmt = AUDIO_FORMAT_S16;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100102 settings.endianness = AUDIO_HOST_ENDIANNESS;
103
104 audio_pcm_init_info (&hw->info, &settings);
105 hw->samples = LINE_OUT_SAMPLES;
106 out->active = 0;
107
108 out->sin.base.sif = &playback_sif.base;
Gerd Hoffmann05b53632020-10-19 09:52:15 +0200109 qemu_spice.add_interface(&out->sin.base);
Jeremy White795ca112014-01-02 09:25:56 -0600110#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
111 spice_server_set_playback_rate(&out->sin, settings.freq);
112#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100113 return 0;
114}
115
116static void line_out_fini (HWVoiceOut *hw)
117{
118 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
119
120 spice_server_remove_interface (&out->sin.base);
121}
122
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200123static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100124{
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200125 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100126
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200127 if (!out->frame) {
128 spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
129 out->fpos = 0;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100130 }
131
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200132 if (out->frame) {
Volker Rümelinaec6d0d2020-09-20 19:17:23 +0200133 *size = MIN((out->fsize - out->fpos) << 2, *size);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100134 }
Volker Rümelinaec6d0d2020-09-20 19:17:23 +0200135
136 *size = audio_rate_get_bytes(&hw->info, &out->rate, *size);
137
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200138 return out->frame + out->fpos;
139}
140
141static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
142{
143 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
144
Volker Rümelind4b70fa2020-09-20 19:17:21 +0200145 if (buf) {
146 assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
147 out->fpos += size >> 2;
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200148
Volker Rümelind4b70fa2020-09-20 19:17:21 +0200149 if (out->fpos == out->fsize) { /* buffer full */
150 spice_server_playback_put_samples(&out->sin, out->frame);
151 out->frame = NULL;
152 }
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200153 }
154
155 return size;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100156}
157
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200158static void line_out_enable(HWVoiceOut *hw, bool enable)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100159{
160 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
161
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200162 if (enable) {
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100163 if (out->active) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200164 return;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100165 }
166 out->active = 1;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +0200167 audio_rate_start(&out->rate);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100168 spice_server_playback_start (&out->sin);
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200169 } else {
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100170 if (!out->active) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200171 return;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100172 }
173 out->active = 0;
174 if (out->frame) {
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200175 memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100176 spice_server_playback_put_samples (&out->sin, out->frame);
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200177 out->frame = NULL;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100178 }
179 spice_server_playback_stop (&out->sin);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100180 }
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100181}
182
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200183#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200184static void line_out_volume(HWVoiceOut *hw, Volume *vol)
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200185{
186 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
187 uint16_t svol[2];
188
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200189 assert(vol->channels == 2);
190 svol[0] = vol->vol[0] * 257;
191 svol[1] = vol->vol[1] * 257;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200192 spice_server_playback_set_volume(&out->sin, 2, svol);
193 spice_server_playback_set_mute(&out->sin, vol->mute);
194}
195#endif
196
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100197/* record */
198
Kővágó, Zoltán5706db12015-06-03 23:03:47 +0200199static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100200{
201 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
202 struct audsettings settings;
203
Jeremy White795ca112014-01-02 09:25:56 -0600204#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
205 settings.freq = spice_server_get_best_record_rate(NULL);
206#else
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100207 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
Jeremy White795ca112014-01-02 09:25:56 -0600208#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100209 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100210 settings.fmt = AUDIO_FORMAT_S16;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100211 settings.endianness = AUDIO_HOST_ENDIANNESS;
212
213 audio_pcm_init_info (&hw->info, &settings);
214 hw->samples = LINE_IN_SAMPLES;
215 in->active = 0;
216
217 in->sin.base.sif = &record_sif.base;
Gerd Hoffmann05b53632020-10-19 09:52:15 +0200218 qemu_spice.add_interface(&in->sin.base);
Jeremy White795ca112014-01-02 09:25:56 -0600219#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
220 spice_server_set_record_rate(&in->sin, settings.freq);
221#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100222 return 0;
223}
224
225static void line_in_fini (HWVoiceIn *hw)
226{
227 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
228
229 spice_server_remove_interface (&in->sin.base);
230}
231
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200232static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100233{
234 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
Kővágó, Zoltán857271a2019-09-19 23:24:21 +0200235 uint64_t to_read = audio_rate_get_bytes(&hw->info, &in->rate, len) >> 2;
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200236 size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100237
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200238 /* XXX: do we need this? */
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100239 if (ready == 0) {
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200240 memset(buf, 0, to_read << 2);
241 ready = to_read;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100242 }
243
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200244 return ready << 2;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100245}
246
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200247static void line_in_enable(HWVoiceIn *hw, bool enable)
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100248{
249 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
250
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200251 if (enable) {
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100252 if (in->active) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200253 return;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100254 }
255 in->active = 1;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +0200256 audio_rate_start(&in->rate);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100257 spice_server_record_start (&in->sin);
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200258 } else {
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100259 if (!in->active) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200260 return;
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100261 }
262 in->active = 0;
263 spice_server_record_stop (&in->sin);
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100264 }
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100265}
266
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200267#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200268static void line_in_volume(HWVoiceIn *hw, Volume *vol)
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200269{
270 SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
271 uint16_t svol[2];
272
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +0200273 assert(vol->channels == 2);
274 svol[0] = vol->vol[0] * 257;
275 svol[1] = vol->vol[1] * 257;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200276 spice_server_record_set_volume(&in->sin, 2, svol);
277 spice_server_record_set_mute(&in->sin, vol->mute);
278}
279#endif
280
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100281static struct audio_pcm_ops audio_callbacks = {
282 .init_out = line_out_init,
283 .fini_out = line_out_fini,
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200284 .write = audio_generic_write,
285 .get_buffer_out = line_out_get_buffer,
286 .put_buffer_out = line_out_put_buffer,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200287 .enable_out = line_out_enable,
288#if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
289 (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
290 .volume_out = line_out_volume,
291#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100292
293 .init_in = line_in_init,
294 .fini_in = line_in_fini,
Kővágó, Zoltán8c198ff2019-09-19 23:24:17 +0200295 .read = line_in_read,
Volker Rümelina2893c82021-01-10 11:02:24 +0100296 .run_buffer_in = audio_generic_run_buffer_in,
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200297 .enable_in = line_in_enable,
298#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
299 .volume_in = line_in_volume,
300#endif
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100301};
302
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100303static struct audio_driver spice_audio_driver = {
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100304 .name = "spice",
305 .descr = "spice audio driver",
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100306 .init = spice_audio_init,
307 .fini = spice_audio_fini,
308 .pcm_ops = &audio_callbacks,
309 .max_voices_out = 1,
310 .max_voices_in = 1,
311 .voice_size_out = sizeof (SpiceVoiceOut),
312 .voice_size_in = sizeof (SpiceVoiceIn),
Gerd Hoffmann3e313752010-11-09 17:29:46 +0100313};
314
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100315static void register_audio_spice(void)
316{
317 audio_driver_register(&spice_audio_driver);
318}
319type_init(register_audio_spice);
Gerd Hoffmannf6b12df2021-06-24 12:38:10 +0200320
321module_dep("ui-spice-core");