Merge remote-tracking branch 'remotes/kraxel/tags/ui-and-input-20170623-pull-request...
[qemu/ar7.git] / audio / spiceaudio.c
blob5580e76307f910c27c3a1af77504e18a8bfba682
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/error-report.h"
24 #include "qemu/timer.h"
25 #include "ui/qemu-spice.h"
27 #define AUDIO_CAP "spice"
28 #include "audio.h"
29 #include "audio_int.h"
31 #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
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
43 typedef struct SpiceRateCtl {
44 int64_t start_ticks;
45 int64_t bytes_sent;
46 } SpiceRateCtl;
48 typedef struct SpiceVoiceOut {
49 HWVoiceOut hw;
50 SpicePlaybackInstance sin;
51 SpiceRateCtl rate;
52 int active;
53 uint32_t *frame;
54 uint32_t *fpos;
55 uint32_t fsize;
56 } SpiceVoiceOut;
58 typedef struct SpiceVoiceIn {
59 HWVoiceIn hw;
60 SpiceRecordInstance sin;
61 SpiceRateCtl rate;
62 int active;
63 uint32_t samples[LINE_IN_SAMPLES];
64 } SpiceVoiceIn;
66 static const SpicePlaybackInterface playback_sif = {
67 .base.type = SPICE_INTERFACE_PLAYBACK,
68 .base.description = "playback",
69 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
70 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
73 static const SpiceRecordInterface record_sif = {
74 .base.type = SPICE_INTERFACE_RECORD,
75 .base.description = "record",
76 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
77 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
80 static void *spice_audio_init (void)
82 if (!using_spice) {
83 return NULL;
85 return &spice_audio_init;
88 static void spice_audio_fini (void *opaque)
90 /* nothing */
93 static void rate_start (SpiceRateCtl *rate)
95 memset (rate, 0, sizeof (*rate));
96 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
99 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
101 int64_t now;
102 int64_t ticks;
103 int64_t bytes;
104 int64_t samples;
106 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
107 ticks = now - rate->start_ticks;
108 bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
109 samples = (bytes - rate->bytes_sent) >> info->shift;
110 if (samples < 0 || samples > 65536) {
111 error_report("Resetting rate control (%" PRId64 " samples)", samples);
112 rate_start(rate);
113 samples = 0;
115 rate->bytes_sent += samples << info->shift;
116 return samples;
119 /* playback */
121 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
122 void *drv_opaque)
124 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
125 struct audsettings settings;
127 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
128 settings.freq = spice_server_get_best_playback_rate(NULL);
129 #else
130 settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
131 #endif
132 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
133 settings.fmt = AUD_FMT_S16;
134 settings.endianness = AUDIO_HOST_ENDIANNESS;
136 audio_pcm_init_info (&hw->info, &settings);
137 hw->samples = LINE_OUT_SAMPLES;
138 out->active = 0;
140 out->sin.base.sif = &playback_sif.base;
141 qemu_spice_add_interface (&out->sin.base);
142 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
143 spice_server_set_playback_rate(&out->sin, settings.freq);
144 #endif
145 return 0;
148 static void line_out_fini (HWVoiceOut *hw)
150 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
152 spice_server_remove_interface (&out->sin.base);
155 static int line_out_run (HWVoiceOut *hw, int live)
157 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
158 int rpos, decr;
159 int samples;
161 if (!live) {
162 return 0;
165 decr = rate_get_samples (&hw->info, &out->rate);
166 decr = audio_MIN (live, decr);
168 samples = decr;
169 rpos = hw->rpos;
170 while (samples) {
171 int left_till_end_samples = hw->samples - rpos;
172 int len = audio_MIN (samples, left_till_end_samples);
174 if (!out->frame) {
175 spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
176 out->fpos = out->frame;
178 if (out->frame) {
179 len = audio_MIN (len, out->fsize);
180 hw->clip (out->fpos, hw->mix_buf + rpos, len);
181 out->fsize -= len;
182 out->fpos += len;
183 if (out->fsize == 0) {
184 spice_server_playback_put_samples (&out->sin, out->frame);
185 out->frame = out->fpos = NULL;
188 rpos = (rpos + len) % hw->samples;
189 samples -= len;
191 hw->rpos = rpos;
192 return decr;
195 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
197 return audio_pcm_sw_write (sw, buf, len);
200 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
202 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
204 switch (cmd) {
205 case VOICE_ENABLE:
206 if (out->active) {
207 break;
209 out->active = 1;
210 rate_start (&out->rate);
211 spice_server_playback_start (&out->sin);
212 break;
213 case VOICE_DISABLE:
214 if (!out->active) {
215 break;
217 out->active = 0;
218 if (out->frame) {
219 memset (out->fpos, 0, out->fsize << 2);
220 spice_server_playback_put_samples (&out->sin, out->frame);
221 out->frame = out->fpos = NULL;
223 spice_server_playback_stop (&out->sin);
224 break;
225 case VOICE_VOLUME:
227 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
228 SWVoiceOut *sw;
229 va_list ap;
230 uint16_t vol[2];
232 va_start (ap, cmd);
233 sw = va_arg (ap, SWVoiceOut *);
234 va_end (ap);
236 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
237 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
238 spice_server_playback_set_volume (&out->sin, 2, vol);
239 spice_server_playback_set_mute (&out->sin, sw->vol.mute);
240 #endif
241 break;
245 return 0;
248 /* record */
250 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
252 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
253 struct audsettings settings;
255 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
256 settings.freq = spice_server_get_best_record_rate(NULL);
257 #else
258 settings.freq = SPICE_INTERFACE_RECORD_FREQ;
259 #endif
260 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
261 settings.fmt = AUD_FMT_S16;
262 settings.endianness = AUDIO_HOST_ENDIANNESS;
264 audio_pcm_init_info (&hw->info, &settings);
265 hw->samples = LINE_IN_SAMPLES;
266 in->active = 0;
268 in->sin.base.sif = &record_sif.base;
269 qemu_spice_add_interface (&in->sin.base);
270 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
271 spice_server_set_record_rate(&in->sin, settings.freq);
272 #endif
273 return 0;
276 static void line_in_fini (HWVoiceIn *hw)
278 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
280 spice_server_remove_interface (&in->sin.base);
283 static int line_in_run (HWVoiceIn *hw)
285 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
286 int num_samples;
287 int ready;
288 int len[2];
289 uint64_t delta_samp;
290 const uint32_t *samples;
292 if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
293 return 0;
296 delta_samp = rate_get_samples (&hw->info, &in->rate);
297 num_samples = audio_MIN (num_samples, delta_samp);
299 ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
300 samples = in->samples;
301 if (ready == 0) {
302 static const uint32_t silence[LINE_IN_SAMPLES];
303 samples = silence;
304 ready = LINE_IN_SAMPLES;
307 num_samples = audio_MIN (ready, num_samples);
309 if (hw->wpos + num_samples > hw->samples) {
310 len[0] = hw->samples - hw->wpos;
311 len[1] = num_samples - len[0];
312 } else {
313 len[0] = num_samples;
314 len[1] = 0;
317 hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
319 if (len[1]) {
320 hw->conv (hw->conv_buf, samples + len[0], len[1]);
323 hw->wpos = (hw->wpos + num_samples) % hw->samples;
325 return num_samples;
328 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
330 return audio_pcm_sw_read (sw, buf, size);
333 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
335 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
337 switch (cmd) {
338 case VOICE_ENABLE:
339 if (in->active) {
340 break;
342 in->active = 1;
343 rate_start (&in->rate);
344 spice_server_record_start (&in->sin);
345 break;
346 case VOICE_DISABLE:
347 if (!in->active) {
348 break;
350 in->active = 0;
351 spice_server_record_stop (&in->sin);
352 break;
353 case VOICE_VOLUME:
355 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
356 SWVoiceIn *sw;
357 va_list ap;
358 uint16_t vol[2];
360 va_start (ap, cmd);
361 sw = va_arg (ap, SWVoiceIn *);
362 va_end (ap);
364 vol[0] = sw->vol.l / ((1ULL << 16) + 1);
365 vol[1] = sw->vol.r / ((1ULL << 16) + 1);
366 spice_server_record_set_volume (&in->sin, 2, vol);
367 spice_server_record_set_mute (&in->sin, sw->vol.mute);
368 #endif
369 break;
373 return 0;
376 static struct audio_option audio_options[] = {
377 { /* end of list */ },
380 static struct audio_pcm_ops audio_callbacks = {
381 .init_out = line_out_init,
382 .fini_out = line_out_fini,
383 .run_out = line_out_run,
384 .write = line_out_write,
385 .ctl_out = line_out_ctl,
387 .init_in = line_in_init,
388 .fini_in = line_in_fini,
389 .run_in = line_in_run,
390 .read = line_in_read,
391 .ctl_in = line_in_ctl,
394 struct audio_driver spice_audio_driver = {
395 .name = "spice",
396 .descr = "spice audio driver",
397 .options = audio_options,
398 .init = spice_audio_init,
399 .fini = spice_audio_fini,
400 .pcm_ops = &audio_callbacks,
401 .max_voices_out = 1,
402 .max_voices_in = 1,
403 .voice_size_out = sizeof (SpiceVoiceOut),
404 .voice_size_in = sizeof (SpiceVoiceIn),
405 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
406 .ctl_caps = VOICE_VOLUME_CAP
407 #endif
410 void qemu_spice_audio_init (void)
412 spice_audio_driver.can_be_default = 1;