master
c 315 lines 8.72 KB
Raw
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 "qapi/error.h"
26 #include "ui/qemu-spice.h"
27 #include "qom/object.h"
28
29 #include "qemu/audio.h"
30 #include "audio_int.h"
31
32 #define TYPE_AUDIO_SPICE "audio-spice"
33 OBJECT_DECLARE_SIMPLE_TYPE(AudioSpice, AUDIO_SPICE)
34
35 static AudioBackendClass *audio_spice_parent_class;
36
37 struct AudioSpice {
38 AudioMixengBackend parent_obj;
39 };
40
41 static bool spice_audio_realize(AudioBackend *abe, Audiodev *dev, Error **errp)
42 {
43 if (!using_spice) {
44 error_setg(errp, "Cannot use spice audio without -spice");
45 qapi_free_Audiodev(dev);
46 return false;
47 }
48
49 return audio_spice_parent_class->realize(abe, dev, errp);
50 }
51
52 #define LINE_OUT_SAMPLES (480 * 4)
53 #define LINE_IN_SAMPLES (480 * 4)
54
55 typedef struct SpiceVoiceOut {
56 HWVoiceOut hw;
57 SpicePlaybackInstance sin;
58 RateCtl rate;
59 int active;
60 uint32_t *frame;
61 uint32_t fpos;
62 uint32_t fsize;
63 } SpiceVoiceOut;
64
65 typedef struct SpiceVoiceIn {
66 HWVoiceIn hw;
67 SpiceRecordInstance sin;
68 RateCtl rate;
69 int active;
70 } SpiceVoiceIn;
71
72 static const SpicePlaybackInterface playback_sif = {
73 .base.type = SPICE_INTERFACE_PLAYBACK,
74 .base.description = "playback",
75 .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
76 .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
77 };
78
79 static const SpiceRecordInterface record_sif = {
80 .base.type = SPICE_INTERFACE_RECORD,
81 .base.description = "record",
82 .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
83 .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
84 };
85
86 /* playback */
87
88 static int line_out_init(HWVoiceOut *hw, struct audsettings *as)
89 {
90 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
91 struct audsettings settings;
92
93 settings.freq = spice_server_get_best_playback_rate(NULL);
94 settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
95 settings.fmt = AUDIO_FORMAT_S16;
96 settings.big_endian = HOST_BIG_ENDIAN;
97
98 audio_pcm_init_info (&hw->info, &settings);
99 hw->samples = LINE_OUT_SAMPLES;
100 out->active = 0;
101
102 out->sin.base.sif = &playback_sif.base;
103 qemu_spice.add_interface(&out->sin.base);
104 spice_server_set_playback_rate(&out->sin, settings.freq);
105 return 0;
106 }
107
108 static void line_out_fini (HWVoiceOut *hw)
109 {
110 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
111
112 spice_server_remove_interface (&out->sin.base);
113 }
114
115 static size_t line_out_get_free(HWVoiceOut *hw)
116 {
117 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
118
119 return audio_rate_peek_bytes(&out->rate, &hw->info);
120 }
121
122 static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
123 {
124 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
125
126 if (!out->frame) {
127 spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
128 out->fpos = 0;
129 }
130
131 if (out->frame) {
132 *size = MIN((out->fsize - out->fpos) << 2, *size);
133 }
134
135 return out->frame + out->fpos;
136 }
137
138 static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
139 {
140 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
141
142 audio_rate_add_bytes(&out->rate, size);
143
144 if (buf) {
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
154 return size;
155 }
156
157 static void line_out_enable(HWVoiceOut *hw, bool enable)
158 {
159 SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
160
161 if (enable) {
162 if (out->active) {
163 return;
164 }
165 out->active = 1;
166 audio_rate_start(&out->rate);
167 spice_server_playback_start (&out->sin);
168 } else {
169 if (!out->active) {
170 return;
171 }
172 out->active = 0;
173 if (out->frame) {
174 memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
175 spice_server_playback_put_samples (&out->sin, out->frame);
176 out->frame = NULL;
177 }
178 spice_server_playback_stop (&out->sin);
179 }
180 }
181
182 static void line_out_volume(HWVoiceOut *hw, Volume *vol)
183 {
184 SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
185 uint16_t svol[2];
186
187 assert(vol->channels == 2);
188 svol[0] = vol->vol[0] * 257;
189 svol[1] = vol->vol[1] * 257;
190 spice_server_playback_set_volume(&out->sin, 2, svol);
191 spice_server_playback_set_mute(&out->sin, vol->mute);
192 }
193
194 /* record */
195
196 static int line_in_init(HWVoiceIn *hw, struct audsettings *as)
197 {
198 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
199 struct audsettings settings;
200
201 settings.freq = spice_server_get_best_record_rate(NULL);
202 settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
203 settings.fmt = AUDIO_FORMAT_S16;
204 settings.big_endian = HOST_BIG_ENDIAN;
205
206 audio_pcm_init_info (&hw->info, &settings);
207 hw->samples = LINE_IN_SAMPLES;
208 in->active = 0;
209
210 in->sin.base.sif = &record_sif.base;
211 qemu_spice.add_interface(&in->sin.base);
212 spice_server_set_record_rate(&in->sin, settings.freq);
213 return 0;
214 }
215
216 static void line_in_fini (HWVoiceIn *hw)
217 {
218 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
219
220 spice_server_remove_interface (&in->sin.base);
221 }
222
223 static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
224 {
225 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
226 uint64_t to_read = audio_rate_get_bytes(&in->rate, &hw->info, len) >> 2;
227 size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
228
229 /*
230 * If the client didn't send new frames, it most likely disconnected.
231 * Generate silence in this case to avoid a stalled audio stream.
232 */
233 if (ready == 0) {
234 memset(buf, 0, to_read << 2);
235 ready = to_read;
236 }
237
238 return ready << 2;
239 }
240
241 static void line_in_enable(HWVoiceIn *hw, bool enable)
242 {
243 SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
244
245 if (enable) {
246 if (in->active) {
247 return;
248 }
249 in->active = 1;
250 audio_rate_start(&in->rate);
251 spice_server_record_start (&in->sin);
252 } else {
253 if (!in->active) {
254 return;
255 }
256 in->active = 0;
257 spice_server_record_stop (&in->sin);
258 }
259 }
260
261 static void line_in_volume(HWVoiceIn *hw, Volume *vol)
262 {
263 SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
264 uint16_t svol[2];
265
266 assert(vol->channels == 2);
267 svol[0] = vol->vol[0] * 257;
268 svol[1] = vol->vol[1] * 257;
269 spice_server_record_set_volume(&in->sin, 2, svol);
270 spice_server_record_set_mute(&in->sin, vol->mute);
271 }
272
273 static void audio_spice_class_init(ObjectClass *klass, const void *data)
274 {
275 AudioBackendClass *b = AUDIO_BACKEND_CLASS(klass);
276 AudioMixengBackendClass *k = AUDIO_MIXENG_BACKEND_CLASS(klass);
277
278 audio_spice_parent_class = AUDIO_BACKEND_CLASS(object_class_get_parent(klass));
279
280 b->realize = spice_audio_realize;
281 k->max_voices_out = 1;
282 k->max_voices_in = 1;
283 k->voice_size_out = sizeof(SpiceVoiceOut);
284 k->voice_size_in = sizeof(SpiceVoiceIn);
285
286 k->init_out = line_out_init;
287 k->fini_out = line_out_fini;
288 k->write = audio_generic_write;
289 k->buffer_get_free = line_out_get_free;
290 k->get_buffer_out = line_out_get_buffer;
291 k->put_buffer_out = line_out_put_buffer;
292 k->enable_out = line_out_enable;
293 k->volume_out = line_out_volume;
294
295 k->init_in = line_in_init;
296 k->fini_in = line_in_fini;
297 k->read = line_in_read;
298 k->run_buffer_in = audio_generic_run_buffer_in;
299 k->enable_in = line_in_enable;
300 k->volume_in = line_in_volume;
301 }
302
303 static const TypeInfo audio_types[] = {
304 {
305 .name = TYPE_AUDIO_SPICE,
306 .parent = TYPE_AUDIO_MIXENG_BACKEND,
307 .instance_size = sizeof(AudioSpice),
308 .class_init = audio_spice_class_init,
309 },
310 };
311
312 DEFINE_TYPES(audio_types)
313 module_obj(TYPE_AUDIO_SPICE);
314
315 module_dep("ui-spice-core");