xref: /openbmc/qemu/audio/spiceaudio.c (revision ebe15582)
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 "qemu/host-utils.h"
22 #include "qemu/module.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(Audiodev *dev)
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        = AUDIO_FORMAT_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 size_t line_out_run (HWVoiceOut *hw, size_t live)
156 {
157     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
158     size_t rpos, decr;
159     size_t samples;
160 
161     if (!live) {
162         return 0;
163     }
164 
165     decr = rate_get_samples (&hw->info, &out->rate);
166     decr = 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 = 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 = 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_ctl (HWVoiceOut *hw, int cmd, ...)
196 {
197     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
198 
199     switch (cmd) {
200     case VOICE_ENABLE:
201         if (out->active) {
202             break;
203         }
204         out->active = 1;
205         rate_start (&out->rate);
206         spice_server_playback_start (&out->sin);
207         break;
208     case VOICE_DISABLE:
209         if (!out->active) {
210             break;
211         }
212         out->active = 0;
213         if (out->frame) {
214             memset (out->fpos, 0, out->fsize << 2);
215             spice_server_playback_put_samples (&out->sin, out->frame);
216             out->frame = out->fpos = NULL;
217         }
218         spice_server_playback_stop (&out->sin);
219         break;
220     case VOICE_VOLUME:
221         {
222 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
223             SWVoiceOut *sw;
224             va_list ap;
225             uint16_t vol[2];
226 
227             va_start (ap, cmd);
228             sw = va_arg (ap, SWVoiceOut *);
229             va_end (ap);
230 
231             vol[0] = sw->vol.l / ((1ULL << 16) + 1);
232             vol[1] = sw->vol.r / ((1ULL << 16) + 1);
233             spice_server_playback_set_volume (&out->sin, 2, vol);
234             spice_server_playback_set_mute (&out->sin, sw->vol.mute);
235 #endif
236             break;
237         }
238     }
239 
240     return 0;
241 }
242 
243 /* record */
244 
245 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
246 {
247     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
248     struct audsettings settings;
249 
250 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
251     settings.freq       = spice_server_get_best_record_rate(NULL);
252 #else
253     settings.freq       = SPICE_INTERFACE_RECORD_FREQ;
254 #endif
255     settings.nchannels  = SPICE_INTERFACE_RECORD_CHAN;
256     settings.fmt        = AUDIO_FORMAT_S16;
257     settings.endianness = AUDIO_HOST_ENDIANNESS;
258 
259     audio_pcm_init_info (&hw->info, &settings);
260     hw->samples = LINE_IN_SAMPLES;
261     in->active = 0;
262 
263     in->sin.base.sif = &record_sif.base;
264     qemu_spice_add_interface (&in->sin.base);
265 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
266     spice_server_set_record_rate(&in->sin, settings.freq);
267 #endif
268     return 0;
269 }
270 
271 static void line_in_fini (HWVoiceIn *hw)
272 {
273     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
274 
275     spice_server_remove_interface (&in->sin.base);
276 }
277 
278 static size_t line_in_run(HWVoiceIn *hw)
279 {
280     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
281     size_t num_samples;
282     int ready;
283     size_t len[2];
284     uint64_t delta_samp;
285     const uint32_t *samples;
286 
287     if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
288         return 0;
289     }
290 
291     delta_samp = rate_get_samples (&hw->info, &in->rate);
292     num_samples = MIN (num_samples, delta_samp);
293 
294     ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
295     samples = in->samples;
296     if (ready == 0) {
297         static const uint32_t silence[LINE_IN_SAMPLES];
298         samples = silence;
299         ready = LINE_IN_SAMPLES;
300     }
301 
302     num_samples = MIN (ready, num_samples);
303 
304     if (hw->wpos + num_samples > hw->samples) {
305         len[0] = hw->samples - hw->wpos;
306         len[1] = num_samples - len[0];
307     } else {
308         len[0] = num_samples;
309         len[1] = 0;
310     }
311 
312     hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
313 
314     if (len[1]) {
315         hw->conv (hw->conv_buf, samples + len[0], len[1]);
316     }
317 
318     hw->wpos = (hw->wpos + num_samples) % hw->samples;
319 
320     return num_samples;
321 }
322 
323 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
324 {
325     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
326 
327     switch (cmd) {
328     case VOICE_ENABLE:
329         if (in->active) {
330             break;
331         }
332         in->active = 1;
333         rate_start (&in->rate);
334         spice_server_record_start (&in->sin);
335         break;
336     case VOICE_DISABLE:
337         if (!in->active) {
338             break;
339         }
340         in->active = 0;
341         spice_server_record_stop (&in->sin);
342         break;
343     case VOICE_VOLUME:
344         {
345 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
346             SWVoiceIn *sw;
347             va_list ap;
348             uint16_t vol[2];
349 
350             va_start (ap, cmd);
351             sw = va_arg (ap, SWVoiceIn *);
352             va_end (ap);
353 
354             vol[0] = sw->vol.l / ((1ULL << 16) + 1);
355             vol[1] = sw->vol.r / ((1ULL << 16) + 1);
356             spice_server_record_set_volume (&in->sin, 2, vol);
357             spice_server_record_set_mute (&in->sin, sw->vol.mute);
358 #endif
359             break;
360         }
361     }
362 
363     return 0;
364 }
365 
366 static struct audio_pcm_ops audio_callbacks = {
367     .init_out = line_out_init,
368     .fini_out = line_out_fini,
369     .run_out  = line_out_run,
370     .ctl_out  = line_out_ctl,
371 
372     .init_in  = line_in_init,
373     .fini_in  = line_in_fini,
374     .run_in   = line_in_run,
375     .ctl_in   = line_in_ctl,
376 };
377 
378 static struct audio_driver spice_audio_driver = {
379     .name           = "spice",
380     .descr          = "spice audio driver",
381     .init           = spice_audio_init,
382     .fini           = spice_audio_fini,
383     .pcm_ops        = &audio_callbacks,
384     .max_voices_out = 1,
385     .max_voices_in  = 1,
386     .voice_size_out = sizeof (SpiceVoiceOut),
387     .voice_size_in  = sizeof (SpiceVoiceIn),
388 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
389     .ctl_caps       = VOICE_VOLUME_CAP
390 #endif
391 };
392 
393 void qemu_spice_audio_init (void)
394 {
395     spice_audio_driver.can_be_default = 1;
396 }
397 
398 static void register_audio_spice(void)
399 {
400     audio_driver_register(&spice_audio_driver);
401 }
402 type_init(register_audio_spice);
403