pc-bios: refresh edk2 build artifacts for edk2-stable201905
[qemu/ar7.git] / audio / spiceaudio.c
blob0ead5ae43adecb881f2e9d01b60b0a87745fae7e
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
4 * maintained by Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "hw/hw.h"
22 #include "qemu/host-utils.h"
23 #include "qemu/module.h"
24 #include "qemu/error-report.h"
25 #include "qemu/timer.h"
26 #include "ui/qemu-spice.h"
28 #define AUDIO_CAP "spice"
29 #include "audio.h"
30 #include "audio_int.h"
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
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
44 typedef struct SpiceRateCtl {
45 int64_t start_ticks;
46 int64_t bytes_sent;
47 } SpiceRateCtl;
49 typedef struct SpiceVoiceOut {
50 HWVoiceOut hw;
51 SpicePlaybackInstance sin;
52 SpiceRateCtl rate;
53 int active;
54 uint32_t *frame;
55 uint32_t *fpos;
56 uint32_t fsize;
57 } SpiceVoiceOut;
59 typedef struct SpiceVoiceIn {
60 HWVoiceIn hw;
61 SpiceRecordInstance sin;
62 SpiceRateCtl rate;
63 int active;
64 uint32_t samples[LINE_IN_SAMPLES];
65 } SpiceVoiceIn;
67 static const SpicePlaybackInterface playback_sif = {
68 .base.type = SPICE_INTERFACE_PLAYBACK,
69 .base.description = "playback",
70 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
71 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
74 static const SpiceRecordInterface record_sif = {
75 .base.type = SPICE_INTERFACE_RECORD,
76 .base.description = "record",
77 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
78 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
81 static void *spice_audio_init(Audiodev *dev)
83 if (!using_spice) {
84 return NULL;
86 return &spice_audio_init;
89 static void spice_audio_fini (void *opaque)
91 /* nothing */
94 static void rate_start (SpiceRateCtl *rate)
96 memset (rate, 0, sizeof (*rate));
97 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
100 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
102 int64_t now;
103 int64_t ticks;
104 int64_t bytes;
105 int64_t samples;
107 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
108 ticks = now - rate->start_ticks;
109 bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
110 samples = (bytes - rate->bytes_sent) >> info->shift;
111 if (samples < 0 || samples > 65536) {
112 error_report("Resetting rate control (%" PRId64 " samples)", samples);
113 rate_start(rate);
114 samples = 0;
116 rate->bytes_sent += samples << info->shift;
117 return samples;
120 /* playback */
122 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
123 void *drv_opaque)
125 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
126 struct audsettings settings;
128 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
129 settings.freq = spice_server_get_best_playback_rate(NULL);
130 #else
131 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
132 #endif
133 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
134 settings.fmt = AUDIO_FORMAT_S16;
135 settings.endianness = AUDIO_HOST_ENDIANNESS;
137 audio_pcm_init_info (&hw->info, &settings);
138 hw->samples = LINE_OUT_SAMPLES;
139 out->active = 0;
141 out->sin.base.sif = &playback_sif.base;
142 qemu_spice_add_interface (&out->sin.base);
143 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
144 spice_server_set_playback_rate(&out->sin, settings.freq);
145 #endif
146 return 0;
149 static void line_out_fini (HWVoiceOut *hw)
151 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
153 spice_server_remove_interface (&out->sin.base);
156 static int line_out_run (HWVoiceOut *hw, int live)
158 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
159 int rpos, decr;
160 int samples;
162 if (!live) {
163 return 0;
166 decr = rate_get_samples (&hw->info, &out->rate);
167 decr = audio_MIN (live, decr);
169 samples = decr;
170 rpos = hw->rpos;
171 while (samples) {
172 int left_till_end_samples = hw->samples - rpos;
173 int len = audio_MIN (samples, left_till_end_samples);
175 if (!out->frame) {
176 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
177 out->fpos = out->frame;
179 if (out->frame) {
180 len = audio_MIN (len, out->fsize);
181 hw->clip (out->fpos, hw->mix_buf + rpos, len);
182 out->fsize -= len;
183 out->fpos += len;
184 if (out->fsize == 0) {
185 spice_server_playback_put_samples (&out->sin, out->frame);
186 out->frame = out->fpos = NULL;
189 rpos = (rpos + len) % hw->samples;
190 samples -= len;
192 hw->rpos = rpos;
193 return decr;
196 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
198 return audio_pcm_sw_write (sw, buf, len);
201 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
203 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
205 switch (cmd) {
206 case VOICE_ENABLE:
207 if (out->active) {
208 break;
210 out->active = 1;
211 rate_start (&out->rate);
212 spice_server_playback_start (&out->sin);
213 break;
214 case VOICE_DISABLE:
215 if (!out->active) {
216 break;
218 out->active = 0;
219 if (out->frame) {
220 memset (out->fpos, 0, out->fsize << 2);
221 spice_server_playback_put_samples (&out->sin, out->frame);
222 out->frame = out->fpos = NULL;
224 spice_server_playback_stop (&out->sin);
225 break;
226 case VOICE_VOLUME:
228 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
229 SWVoiceOut *sw;
230 va_list ap;
231 uint16_t vol[2];
233 va_start (ap, cmd);
234 sw = va_arg (ap, SWVoiceOut *);
235 va_end (ap);
237 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
238 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
239 spice_server_playback_set_volume (&out->sin, 2, vol);
240 spice_server_playback_set_mute (&out->sin, sw->vol.mute);
241 #endif
242 break;
246 return 0;
249 /* record */
251 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
253 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
254 struct audsettings settings;
256 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
257 settings.freq = spice_server_get_best_record_rate(NULL);
258 #else
259 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
260 #endif
261 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
262 settings.fmt = AUDIO_FORMAT_S16;
263 settings.endianness = AUDIO_HOST_ENDIANNESS;
265 audio_pcm_init_info (&hw->info, &settings);
266 hw->samples = LINE_IN_SAMPLES;
267 in->active = 0;
269 in->sin.base.sif = &record_sif.base;
270 qemu_spice_add_interface (&in->sin.base);
271 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
272 spice_server_set_record_rate(&in->sin, settings.freq);
273 #endif
274 return 0;
277 static void line_in_fini (HWVoiceIn *hw)
279 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
281 spice_server_remove_interface (&in->sin.base);
284 static int line_in_run (HWVoiceIn *hw)
286 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
287 int num_samples;
288 int ready;
289 int len[2];
290 uint64_t delta_samp;
291 const uint32_t *samples;
293 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
294 return 0;
297 delta_samp = rate_get_samples (&hw->info, &in->rate);
298 num_samples = audio_MIN (num_samples, delta_samp);
300 ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
301 samples = in->samples;
302 if (ready == 0) {
303 static const uint32_t silence[LINE_IN_SAMPLES];
304 samples = silence;
305 ready = LINE_IN_SAMPLES;
308 num_samples = audio_MIN (ready, num_samples);
310 if (hw->wpos + num_samples > hw->samples) {
311 len[0] = hw->samples - hw->wpos;
312 len[1] = num_samples - len[0];
313 } else {
314 len[0] = num_samples;
315 len[1] = 0;
318 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
320 if (len[1]) {
321 hw->conv (hw->conv_buf, samples + len[0], len[1]);
324 hw->wpos = (hw->wpos + num_samples) % hw->samples;
326 return num_samples;
329 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
331 return audio_pcm_sw_read (sw, buf, size);
334 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
336 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
338 switch (cmd) {
339 case VOICE_ENABLE:
340 if (in->active) {
341 break;
343 in->active = 1;
344 rate_start (&in->rate);
345 spice_server_record_start (&in->sin);
346 break;
347 case VOICE_DISABLE:
348 if (!in->active) {
349 break;
351 in->active = 0;
352 spice_server_record_stop (&in->sin);
353 break;
354 case VOICE_VOLUME:
356 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
357 SWVoiceIn *sw;
358 va_list ap;
359 uint16_t vol[2];
361 va_start (ap, cmd);
362 sw = va_arg (ap, SWVoiceIn *);
363 va_end (ap);
365 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
366 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
367 spice_server_record_set_volume (&in->sin, 2, vol);
368 spice_server_record_set_mute (&in->sin, sw->vol.mute);
369 #endif
370 break;
374 return 0;
377 static struct audio_pcm_ops audio_callbacks = {
378 .init_out = line_out_init,
379 .fini_out = line_out_fini,
380 .run_out = line_out_run,
381 .write = line_out_write,
382 .ctl_out = line_out_ctl,
384 .init_in = line_in_init,
385 .fini_in = line_in_fini,
386 .run_in = line_in_run,
387 .read = line_in_read,
388 .ctl_in = line_in_ctl,
391 static struct audio_driver spice_audio_driver = {
392 .name = "spice",
393 .descr = "spice audio driver",
394 .init = spice_audio_init,
395 .fini = spice_audio_fini,
396 .pcm_ops = &audio_callbacks,
397 .max_voices_out = 1,
398 .max_voices_in = 1,
399 .voice_size_out = sizeof (SpiceVoiceOut),
400 .voice_size_in = sizeof (SpiceVoiceIn),
401 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
402 .ctl_caps = VOICE_VOLUME_CAP
403 #endif
406 void qemu_spice_audio_init (void)
408 spice_audio_driver.can_be_default = 1;
411 static void register_audio_spice(void)
413 audio_driver_register(&spice_audio_driver);
415 type_init(register_audio_spice);