xref: /openbmc/qemu/audio/spiceaudio.c (revision f363d039)
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 SpiceVoiceOut {
44     HWVoiceOut            hw;
45     SpicePlaybackInstance sin;
46     RateCtl               rate;
47     int                   active;
48     uint32_t              *frame;
49     uint32_t              fpos;
50     uint32_t              fsize;
51 } SpiceVoiceOut;
52 
53 typedef struct SpiceVoiceIn {
54     HWVoiceIn             hw;
55     SpiceRecordInstance   sin;
56     RateCtl               rate;
57     int                   active;
58 } SpiceVoiceIn;
59 
60 static const SpicePlaybackInterface playback_sif = {
61     .base.type          = SPICE_INTERFACE_PLAYBACK,
62     .base.description   = "playback",
63     .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
64     .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
65 };
66 
67 static const SpiceRecordInterface record_sif = {
68     .base.type          = SPICE_INTERFACE_RECORD,
69     .base.description   = "record",
70     .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
71     .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
72 };
73 
74 static void *spice_audio_init(Audiodev *dev)
75 {
76     if (!using_spice) {
77         return NULL;
78     }
79     return &spice_audio_init;
80 }
81 
82 static void spice_audio_fini (void *opaque)
83 {
84     /* nothing */
85 }
86 
87 /* playback */
88 
89 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
90                          void *drv_opaque)
91 {
92     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
93     struct audsettings settings;
94 
95 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
96     settings.freq       = spice_server_get_best_playback_rate(NULL);
97 #else
98     settings.freq       = SPICE_INTERFACE_PLAYBACK_FREQ;
99 #endif
100     settings.nchannels  = SPICE_INTERFACE_PLAYBACK_CHAN;
101     settings.fmt        = AUDIO_FORMAT_S16;
102     settings.endianness = AUDIO_HOST_ENDIANNESS;
103 
104     audio_pcm_init_info (&hw->info, &settings);
105     hw->samples = LINE_OUT_SAMPLES;
106     out->active = 0;
107 
108     out->sin.base.sif = &playback_sif.base;
109     qemu_spice_add_interface (&out->sin.base);
110 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
111     spice_server_set_playback_rate(&out->sin, settings.freq);
112 #endif
113     return 0;
114 }
115 
116 static void line_out_fini (HWVoiceOut *hw)
117 {
118     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
119 
120     spice_server_remove_interface (&out->sin.base);
121 }
122 
123 static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
124 {
125     SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
126 
127     if (!out->frame) {
128         spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
129         out->fpos = 0;
130     }
131 
132     if (out->frame) {
133         *size = audio_rate_get_bytes(
134             &hw->info, &out->rate, (out->fsize - out->fpos) << hw->info.shift);
135     } else {
136         audio_rate_start(&out->rate);
137     }
138     return out->frame + out->fpos;
139 }
140 
141 static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
142 {
143     SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
144 
145     assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
146     out->fpos += size >> 2;
147 
148     if (out->fpos == out->fsize) { /* buffer full */
149         spice_server_playback_put_samples(&out->sin, out->frame);
150         out->frame = NULL;
151     }
152 
153     return size;
154 }
155 
156 static void line_out_enable(HWVoiceOut *hw, bool enable)
157 {
158     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
159 
160     if (enable) {
161         if (out->active) {
162             return;
163         }
164         out->active = 1;
165         audio_rate_start(&out->rate);
166         spice_server_playback_start (&out->sin);
167     } else {
168         if (!out->active) {
169             return;
170         }
171         out->active = 0;
172         if (out->frame) {
173             memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
174             spice_server_playback_put_samples (&out->sin, out->frame);
175             out->frame = NULL;
176         }
177         spice_server_playback_stop (&out->sin);
178     }
179 }
180 
181 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
182 static void line_out_volume(HWVoiceOut *hw, struct mixeng_volume *vol)
183 {
184     SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
185     uint16_t svol[2];
186 
187     svol[0] = vol->l / ((1ULL << 16) + 1);
188     svol[1] = vol->r / ((1ULL << 16) + 1);
189     spice_server_playback_set_volume(&out->sin, 2, svol);
190     spice_server_playback_set_mute(&out->sin, vol->mute);
191 }
192 #endif
193 
194 /* record */
195 
196 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
197 {
198     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
199     struct audsettings settings;
200 
201 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
202     settings.freq       = spice_server_get_best_record_rate(NULL);
203 #else
204     settings.freq       = SPICE_INTERFACE_RECORD_FREQ;
205 #endif
206     settings.nchannels  = SPICE_INTERFACE_RECORD_CHAN;
207     settings.fmt        = AUDIO_FORMAT_S16;
208     settings.endianness = AUDIO_HOST_ENDIANNESS;
209 
210     audio_pcm_init_info (&hw->info, &settings);
211     hw->samples = LINE_IN_SAMPLES;
212     in->active = 0;
213 
214     in->sin.base.sif = &record_sif.base;
215     qemu_spice_add_interface (&in->sin.base);
216 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
217     spice_server_set_record_rate(&in->sin, settings.freq);
218 #endif
219     return 0;
220 }
221 
222 static void line_in_fini (HWVoiceIn *hw)
223 {
224     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
225 
226     spice_server_remove_interface (&in->sin.base);
227 }
228 
229 static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
230 {
231     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
232     uint64_t to_read = audio_rate_get_bytes(&hw->info, &in->rate, len) >> 2;
233     size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
234 
235     /* XXX: do we need this? */
236     if (ready == 0) {
237         memset(buf, 0, to_read << 2);
238         ready = to_read;
239     }
240 
241     return ready << 2;
242 }
243 
244 static void line_in_enable(HWVoiceIn *hw, bool enable)
245 {
246     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
247 
248     if (enable) {
249         if (in->active) {
250             return;
251         }
252         in->active = 1;
253         audio_rate_start(&in->rate);
254         spice_server_record_start (&in->sin);
255     } else {
256         if (!in->active) {
257             return;
258         }
259         in->active = 0;
260         spice_server_record_stop (&in->sin);
261     }
262 }
263 
264 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
265 static void line_in_volume(HWVoiceIn *hw, struct mixeng_volume *vol)
266 {
267     SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
268     uint16_t svol[2];
269 
270     svol[0] = vol->l / ((1ULL << 16) + 1);
271     svol[1] = vol->r / ((1ULL << 16) + 1);
272     spice_server_record_set_volume(&in->sin, 2, svol);
273     spice_server_record_set_mute(&in->sin, vol->mute);
274 }
275 #endif
276 
277 static struct audio_pcm_ops audio_callbacks = {
278     .init_out = line_out_init,
279     .fini_out = line_out_fini,
280     .write    = audio_generic_write,
281     .get_buffer_out = line_out_get_buffer,
282     .put_buffer_out = line_out_put_buffer,
283     .enable_out = line_out_enable,
284 #if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
285         (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
286     .volume_out = line_out_volume,
287 #endif
288 
289     .init_in  = line_in_init,
290     .fini_in  = line_in_fini,
291     .read     = line_in_read,
292     .enable_in = line_in_enable,
293 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
294     .volume_in = line_in_volume,
295 #endif
296 };
297 
298 static struct audio_driver spice_audio_driver = {
299     .name           = "spice",
300     .descr          = "spice audio driver",
301     .init           = spice_audio_init,
302     .fini           = spice_audio_fini,
303     .pcm_ops        = &audio_callbacks,
304     .max_voices_out = 1,
305     .max_voices_in  = 1,
306     .voice_size_out = sizeof (SpiceVoiceOut),
307     .voice_size_in  = sizeof (SpiceVoiceIn),
308 };
309 
310 void qemu_spice_audio_init (void)
311 {
312     spice_audio_driver.can_be_default = 1;
313 }
314 
315 static void register_audio_spice(void)
316 {
317     audio_driver_register(&spice_audio_driver);
318 }
319 type_init(register_audio_spice);
320