]> git.proxmox.com Git - mirror_qemu.git/blame - audio/spiceaudio.c
Merge tag 'pull-request-2023-12-20' of https://gitlab.com/thuth/qemu into staging
[mirror_qemu.git] / audio / spiceaudio.c
CommitLineData
cf2c1839
GH
1/*
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
6086a565 20#include "qemu/osdep.h"
87776ab7 21#include "qemu/host-utils.h"
0b8fa32f 22#include "qemu/module.h"
d49b6836 23#include "qemu/error-report.h"
1de7afc9 24#include "qemu/timer.h"
f6061733 25#include "qapi/error.h"
3e313753
GH
26#include "ui/qemu-spice.h"
27
28#define AUDIO_CAP "spice"
29#include "audio.h"
30#include "audio_int.h"
31
795ca114
JW
32#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
33#define LINE_OUT_SAMPLES (480 * 4)
34#else
35#define LINE_OUT_SAMPLES (256 * 4)
36#endif
37
38#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
39#define LINE_IN_SAMPLES (480 * 4)
40#else
41#define LINE_IN_SAMPLES (256 * 4)
42#endif
3e313753 43
3e313753
GH
44typedef struct SpiceVoiceOut {
45 HWVoiceOut hw;
46 SpicePlaybackInstance sin;
857271a2 47 RateCtl rate;
3e313753
GH
48 int active;
49 uint32_t *frame;
8c198ff0 50 uint32_t fpos;
3e313753
GH
51 uint32_t fsize;
52} SpiceVoiceOut;
53
54typedef struct SpiceVoiceIn {
55 HWVoiceIn hw;
56 SpiceRecordInstance sin;
857271a2 57 RateCtl rate;
3e313753 58 int active;
3e313753
GH
59} SpiceVoiceIn;
60
61static const SpicePlaybackInterface playback_sif = {
62 .base.type = SPICE_INTERFACE_PLAYBACK,
63 .base.description = "playback",
64 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
65 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
66};
67
68static const SpiceRecordInterface record_sif = {
69 .base.type = SPICE_INTERFACE_RECORD,
70 .base.description = "record",
71 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
72 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
73};
74
f6061733 75static void *spice_audio_init(Audiodev *dev, Error **errp)
3e313753
GH
76{
77 if (!using_spice) {
f6061733 78 error_setg(errp, "Cannot use spice audio without -spice");
3e313753
GH
79 return NULL;
80 }
f6061733 81
3e313753
GH
82 return &spice_audio_init;
83}
84
85static void spice_audio_fini (void *opaque)
86{
87 /* nothing */
88}
89
3e313753
GH
90/* playback */
91
5706db1d
KZ
92static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
93 void *drv_opaque)
3e313753
GH
94{
95 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
96 struct audsettings settings;
97
795ca114
JW
98#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
99 settings.freq = spice_server_get_best_playback_rate(NULL);
100#else
3e313753 101 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
795ca114 102#endif
3e313753 103 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
85bc5852 104 settings.fmt = AUDIO_FORMAT_S16;
3e313753
GH
105 settings.endianness = AUDIO_HOST_ENDIANNESS;
106
107 audio_pcm_init_info (&hw->info, &settings);
108 hw->samples = LINE_OUT_SAMPLES;
109 out->active = 0;
110
111 out->sin.base.sif = &playback_sif.base;
05b53636 112 qemu_spice.add_interface(&out->sin.base);
795ca114
JW
113#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
114 spice_server_set_playback_rate(&out->sin, settings.freq);
115#endif
3e313753
GH
116 return 0;
117}
118
119static void line_out_fini (HWVoiceOut *hw)
120{
121 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
122
123 spice_server_remove_interface (&out->sin.base);
124}
125
90320051
VR
126static size_t line_out_get_free(HWVoiceOut *hw)
127{
128 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
129
130 return audio_rate_peek_bytes(&out->rate, &hw->info);
131}
132
8c198ff0 133static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
3e313753 134{
8c198ff0 135 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
3e313753 136
8c198ff0
KZ
137 if (!out->frame) {
138 spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
139 out->fpos = 0;
3e313753
GH
140 }
141
8c198ff0 142 if (out->frame) {
aec6d0dc 143 *size = MIN((out->fsize - out->fpos) << 2, *size);
8c198ff0 144 }
aec6d0dc 145
8c198ff0
KZ
146 return out->frame + out->fpos;
147}
3e313753 148
8c198ff0
KZ
149static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
150{
151 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
152
90320051
VR
153 audio_rate_add_bytes(&out->rate, size);
154
d4b70fa4
VR
155 if (buf) {
156 assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
157 out->fpos += size >> 2;
8c198ff0 158
d4b70fa4
VR
159 if (out->fpos == out->fsize) { /* buffer full */
160 spice_server_playback_put_samples(&out->sin, out->frame);
161 out->frame = NULL;
162 }
3e313753 163 }
8c198ff0
KZ
164
165 return size;
3e313753
GH
166}
167
571a8c52 168static void line_out_enable(HWVoiceOut *hw, bool enable)
3e313753
GH
169{
170 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
171
571a8c52 172 if (enable) {
3e313753 173 if (out->active) {
571a8c52 174 return;
3e313753
GH
175 }
176 out->active = 1;
857271a2 177 audio_rate_start(&out->rate);
3e313753 178 spice_server_playback_start (&out->sin);
571a8c52 179 } else {
3e313753 180 if (!out->active) {
571a8c52 181 return;
3e313753
GH
182 }
183 out->active = 0;
184 if (out->frame) {
8c198ff0 185 memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
3e313753 186 spice_server_playback_put_samples (&out->sin, out->frame);
8c198ff0 187 out->frame = NULL;
3e313753
GH
188 }
189 spice_server_playback_stop (&out->sin);
3e313753 190 }
571a8c52 191}
a70c99c6 192
571a8c52 193#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
cecc1e79 194static void line_out_volume(HWVoiceOut *hw, Volume *vol)
571a8c52
KZ
195{
196 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
197 uint16_t svol[2];
198
cecc1e79
KZ
199 assert(vol->channels == 2);
200 svol[0] = vol->vol[0] * 257;
201 svol[1] = vol->vol[1] * 257;
571a8c52
KZ
202 spice_server_playback_set_volume(&out->sin, 2, svol);
203 spice_server_playback_set_mute(&out->sin, vol->mute);
3e313753 204}
571a8c52 205#endif
3e313753
GH
206
207/* record */
208
5706db1d 209static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
3e313753
GH
210{
211 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
212 struct audsettings settings;
213
795ca114
JW
214#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
215 settings.freq = spice_server_get_best_record_rate(NULL);
216#else
3e313753 217 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
795ca114 218#endif
3e313753 219 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
85bc5852 220 settings.fmt = AUDIO_FORMAT_S16;
3e313753
GH
221 settings.endianness = AUDIO_HOST_ENDIANNESS;
222
223 audio_pcm_init_info (&hw->info, &settings);
224 hw->samples = LINE_IN_SAMPLES;
225 in->active = 0;
226
227 in->sin.base.sif = &record_sif.base;
05b53636 228 qemu_spice.add_interface(&in->sin.base);
795ca114
JW
229#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
230 spice_server_set_record_rate(&in->sin, settings.freq);
231#endif
3e313753
GH
232 return 0;
233}
234
235static void line_in_fini (HWVoiceIn *hw)
236{
237 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
238
239 spice_server_remove_interface (&in->sin.base);
240}
241
8c198ff0 242static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
3e313753
GH
243{
244 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
613fe02b 245 uint64_t to_read = audio_rate_get_bytes(&in->rate, &hw->info, len) >> 2;
8c198ff0 246 size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
3e313753 247
70ded68b
VR
248 /*
249 * If the client didn't send new frames, it most likely disconnected.
250 * Generate silence in this case to avoid a stalled audio stream.
251 */
3e313753 252 if (ready == 0) {
8c198ff0
KZ
253 memset(buf, 0, to_read << 2);
254 ready = to_read;
3e313753
GH
255 }
256
8c198ff0 257 return ready << 2;
3e313753
GH
258}
259
571a8c52 260static void line_in_enable(HWVoiceIn *hw, bool enable)
3e313753
GH
261{
262 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
263
571a8c52 264 if (enable) {
3e313753 265 if (in->active) {
571a8c52 266 return;
3e313753
GH
267 }
268 in->active = 1;
857271a2 269 audio_rate_start(&in->rate);
3e313753 270 spice_server_record_start (&in->sin);
571a8c52 271 } else {
3e313753 272 if (!in->active) {
571a8c52 273 return;
3e313753
GH
274 }
275 in->active = 0;
276 spice_server_record_stop (&in->sin);
3e313753 277 }
571a8c52 278}
a70c99c6 279
571a8c52 280#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
cecc1e79 281static void line_in_volume(HWVoiceIn *hw, Volume *vol)
571a8c52
KZ
282{
283 SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
284 uint16_t svol[2];
285
cecc1e79
KZ
286 assert(vol->channels == 2);
287 svol[0] = vol->vol[0] * 257;
288 svol[1] = vol->vol[1] * 257;
571a8c52
KZ
289 spice_server_record_set_volume(&in->sin, 2, svol);
290 spice_server_record_set_mute(&in->sin, vol->mute);
3e313753 291}
571a8c52 292#endif
3e313753 293
3e313753
GH
294static struct audio_pcm_ops audio_callbacks = {
295 .init_out = line_out_init,
296 .fini_out = line_out_fini,
8c198ff0 297 .write = audio_generic_write,
90320051 298 .buffer_get_free = line_out_get_free,
8c198ff0
KZ
299 .get_buffer_out = line_out_get_buffer,
300 .put_buffer_out = line_out_put_buffer,
571a8c52
KZ
301 .enable_out = line_out_enable,
302#if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
303 (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
304 .volume_out = line_out_volume,
305#endif
3e313753
GH
306
307 .init_in = line_in_init,
308 .fini_in = line_in_fini,
8c198ff0 309 .read = line_in_read,
a2893c83 310 .run_buffer_in = audio_generic_run_buffer_in,
571a8c52
KZ
311 .enable_in = line_in_enable,
312#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
313 .volume_in = line_in_volume,
314#endif
3e313753
GH
315};
316
d3893a39 317static struct audio_driver spice_audio_driver = {
3e313753
GH
318 .name = "spice",
319 .descr = "spice audio driver",
3e313753
GH
320 .init = spice_audio_init,
321 .fini = spice_audio_fini,
322 .pcm_ops = &audio_callbacks,
323 .max_voices_out = 1,
324 .max_voices_in = 1,
325 .voice_size_out = sizeof (SpiceVoiceOut),
326 .voice_size_in = sizeof (SpiceVoiceIn),
327};
328
d3893a39
GH
329static void register_audio_spice(void)
330{
331 audio_driver_register(&spice_audio_driver);
332}
333type_init(register_audio_spice);
f6b12dfd
GH
334
335module_dep("ui-spice-core");