xref: /openbmc/qemu/audio/spiceaudio.c (revision 4a09d0bb)
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 
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"
26 
27 #define AUDIO_CAP "spice"
28 #include "audio.h"
29 #include "audio_int.h"
30 
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
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
42 
43 typedef struct SpiceRateCtl {
44     int64_t               start_ticks;
45     int64_t               bytes_sent;
46 } SpiceRateCtl;
47 
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;
57 
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;
65 
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,
71 };
72 
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,
78 };
79 
80 static void *spice_audio_init (void)
81 {
82     if (!using_spice) {
83         return NULL;
84     }
85     return &spice_audio_init;
86 }
87 
88 static void spice_audio_fini (void *opaque)
89 {
90     /* nothing */
91 }
92 
93 static void rate_start (SpiceRateCtl *rate)
94 {
95     memset (rate, 0, sizeof (*rate));
96     rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
97 }
98 
99 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
100 {
101     int64_t now;
102     int64_t ticks;
103     int64_t bytes;
104     int64_t samples;
105 
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;
114     }
115     rate->bytes_sent += samples << info->shift;
116     return samples;
117 }
118 
119 /* playback */
120 
121 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
122                          void *drv_opaque)
123 {
124     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
125     struct audsettings settings;
126 
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;
135 
136     audio_pcm_init_info (&hw->info, &settings);
137     hw->samples = LINE_OUT_SAMPLES;
138     out->active = 0;
139 
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;
146 }
147 
148 static void line_out_fini (HWVoiceOut *hw)
149 {
150     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
151 
152     spice_server_remove_interface (&out->sin.base);
153 }
154 
155 static int line_out_run (HWVoiceOut *hw, int live)
156 {
157     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
158     int rpos, decr;
159     int samples;
160 
161     if (!live) {
162         return 0;
163     }
164 
165     decr = rate_get_samples (&hw->info, &out->rate);
166     decr = audio_MIN (live, decr);
167 
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);
173 
174         if (!out->frame) {
175             spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
176             out->fpos = out->frame;
177         }
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;
186             }
187         }
188         rpos = (rpos + len) % hw->samples;
189         samples -= len;
190     }
191     hw->rpos = rpos;
192     return decr;
193 }
194 
195 static int line_out_write (SWVoiceOut *sw, void *buf, int len)
196 {
197     return audio_pcm_sw_write (sw, buf, len);
198 }
199 
200 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
201 {
202     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
203 
204     switch (cmd) {
205     case VOICE_ENABLE:
206         if (out->active) {
207             break;
208         }
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;
216         }
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;
222         }
223         spice_server_playback_stop (&out->sin);
224         break;
225     case VOICE_VOLUME:
226         {
227 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
228             SWVoiceOut *sw;
229             va_list ap;
230             uint16_t vol[2];
231 
232             va_start (ap, cmd);
233             sw = va_arg (ap, SWVoiceOut *);
234             va_end (ap);
235 
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;
242         }
243     }
244 
245     return 0;
246 }
247 
248 /* record */
249 
250 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
251 {
252     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
253     struct audsettings settings;
254 
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;
263 
264     audio_pcm_init_info (&hw->info, &settings);
265     hw->samples = LINE_IN_SAMPLES;
266     in->active = 0;
267 
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;
274 }
275 
276 static void line_in_fini (HWVoiceIn *hw)
277 {
278     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
279 
280     spice_server_remove_interface (&in->sin.base);
281 }
282 
283 static int line_in_run (HWVoiceIn *hw)
284 {
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;
291 
292     if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
293         return 0;
294     }
295 
296     delta_samp = rate_get_samples (&hw->info, &in->rate);
297     num_samples = audio_MIN (num_samples, delta_samp);
298 
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;
305     }
306 
307     num_samples = audio_MIN (ready, num_samples);
308 
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;
315     }
316 
317     hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
318 
319     if (len[1]) {
320         hw->conv (hw->conv_buf, samples + len[0], len[1]);
321     }
322 
323     hw->wpos = (hw->wpos + num_samples) % hw->samples;
324 
325     return num_samples;
326 }
327 
328 static int line_in_read (SWVoiceIn *sw, void *buf, int size)
329 {
330     return audio_pcm_sw_read (sw, buf, size);
331 }
332 
333 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
334 {
335     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
336 
337     switch (cmd) {
338     case VOICE_ENABLE:
339         if (in->active) {
340             break;
341         }
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;
349         }
350         in->active = 0;
351         spice_server_record_stop (&in->sin);
352         break;
353     case VOICE_VOLUME:
354         {
355 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
356             SWVoiceIn *sw;
357             va_list ap;
358             uint16_t vol[2];
359 
360             va_start (ap, cmd);
361             sw = va_arg (ap, SWVoiceIn *);
362             va_end (ap);
363 
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;
370         }
371     }
372 
373     return 0;
374 }
375 
376 static struct audio_option audio_options[] = {
377     { /* end of list */ },
378 };
379 
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,
386 
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,
392 };
393 
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
408 };
409 
410 void qemu_spice_audio_init (void)
411 {
412     spice_audio_driver.can_be_default = 1;
413 }
414