OSDN Git Service

spiceaudio: port to the new audio backend api
[qmiga/qemu.git] / audio / spiceaudio.c
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 } SpiceVoiceIn;
64
65 static const SpicePlaybackInterface playback_sif = {
66     .base.type          = SPICE_INTERFACE_PLAYBACK,
67     .base.description   = "playback",
68     .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
69     .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
70 };
71
72 static const SpiceRecordInterface record_sif = {
73     .base.type          = SPICE_INTERFACE_RECORD,
74     .base.description   = "record",
75     .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
76     .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
77 };
78
79 static void *spice_audio_init(Audiodev *dev)
80 {
81     if (!using_spice) {
82         return NULL;
83     }
84     return &spice_audio_init;
85 }
86
87 static void spice_audio_fini (void *opaque)
88 {
89     /* nothing */
90 }
91
92 static void rate_start (SpiceRateCtl *rate)
93 {
94     memset (rate, 0, sizeof (*rate));
95     rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
96 }
97
98 static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
99 {
100     int64_t now;
101     int64_t ticks;
102     int64_t bytes;
103     int64_t samples;
104
105     now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
106     ticks = now - rate->start_ticks;
107     bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
108     samples = (bytes - rate->bytes_sent) >> info->shift;
109     if (samples < 0 || samples > 65536) {
110         error_report("Resetting rate control (%" PRId64 " samples)", samples);
111         rate_start(rate);
112         samples = 0;
113     }
114     rate->bytes_sent += samples << info->shift;
115     return samples;
116 }
117
118 /* playback */
119
120 static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
121                          void *drv_opaque)
122 {
123     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
124     struct audsettings settings;
125
126 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
127     settings.freq       = spice_server_get_best_playback_rate(NULL);
128 #else
129     settings.freq       = SPICE_INTERFACE_PLAYBACK_FREQ;
130 #endif
131     settings.nchannels  = SPICE_INTERFACE_PLAYBACK_CHAN;
132     settings.fmt        = AUDIO_FORMAT_S16;
133     settings.endianness = AUDIO_HOST_ENDIANNESS;
134
135     audio_pcm_init_info (&hw->info, &settings);
136     hw->samples = LINE_OUT_SAMPLES;
137     out->active = 0;
138
139     out->sin.base.sif = &playback_sif.base;
140     qemu_spice_add_interface (&out->sin.base);
141 #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
142     spice_server_set_playback_rate(&out->sin, settings.freq);
143 #endif
144     return 0;
145 }
146
147 static void line_out_fini (HWVoiceOut *hw)
148 {
149     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
150
151     spice_server_remove_interface (&out->sin.base);
152 }
153
154 static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
155 {
156     SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
157     size_t decr;
158
159     if (!out->frame) {
160         spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
161         out->fpos = 0;
162     }
163
164     if (out->frame) {
165         decr = rate_get_samples(&hw->info, &out->rate);
166         decr = MIN(out->fsize - out->fpos, decr);
167
168         *size = decr << hw->info.shift;
169     } else {
170         rate_start(&out->rate);
171     }
172     return out->frame + out->fpos;
173 }
174
175 static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
176 {
177     SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
178
179     assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
180     out->fpos += size >> 2;
181
182     if (out->fpos == out->fsize) { /* buffer full */
183         spice_server_playback_put_samples(&out->sin, out->frame);
184         out->frame = NULL;
185     }
186
187     return size;
188 }
189
190 static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
191 {
192     SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
193
194     switch (cmd) {
195     case VOICE_ENABLE:
196         if (out->active) {
197             break;
198         }
199         out->active = 1;
200         rate_start (&out->rate);
201         spice_server_playback_start (&out->sin);
202         break;
203     case VOICE_DISABLE:
204         if (!out->active) {
205             break;
206         }
207         out->active = 0;
208         if (out->frame) {
209             memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
210             spice_server_playback_put_samples (&out->sin, out->frame);
211             out->frame = NULL;
212         }
213         spice_server_playback_stop (&out->sin);
214         break;
215     case VOICE_VOLUME:
216         {
217 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
218             SWVoiceOut *sw;
219             va_list ap;
220             uint16_t vol[2];
221
222             va_start (ap, cmd);
223             sw = va_arg (ap, SWVoiceOut *);
224             va_end (ap);
225
226             vol[0] = sw->vol.l / ((1ULL << 16) + 1);
227             vol[1] = sw->vol.r / ((1ULL << 16) + 1);
228             spice_server_playback_set_volume (&out->sin, 2, vol);
229             spice_server_playback_set_mute (&out->sin, sw->vol.mute);
230 #endif
231             break;
232         }
233     }
234
235     return 0;
236 }
237
238 /* record */
239
240 static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
241 {
242     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
243     struct audsettings settings;
244
245 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
246     settings.freq       = spice_server_get_best_record_rate(NULL);
247 #else
248     settings.freq       = SPICE_INTERFACE_RECORD_FREQ;
249 #endif
250     settings.nchannels  = SPICE_INTERFACE_RECORD_CHAN;
251     settings.fmt        = AUDIO_FORMAT_S16;
252     settings.endianness = AUDIO_HOST_ENDIANNESS;
253
254     audio_pcm_init_info (&hw->info, &settings);
255     hw->samples = LINE_IN_SAMPLES;
256     in->active = 0;
257
258     in->sin.base.sif = &record_sif.base;
259     qemu_spice_add_interface (&in->sin.base);
260 #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
261     spice_server_set_record_rate(&in->sin, settings.freq);
262 #endif
263     return 0;
264 }
265
266 static void line_in_fini (HWVoiceIn *hw)
267 {
268     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
269
270     spice_server_remove_interface (&in->sin.base);
271 }
272
273 static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
274 {
275     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
276     uint64_t delta_samp = rate_get_samples(&hw->info, &in->rate);
277     uint64_t to_read = MIN(len >> 2, delta_samp);
278     size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
279
280     /* XXX: do we need this? */
281     if (ready == 0) {
282         memset(buf, 0, to_read << 2);
283         ready = to_read;
284     }
285
286     return ready << 2;
287 }
288
289 static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
290 {
291     SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
292
293     switch (cmd) {
294     case VOICE_ENABLE:
295         if (in->active) {
296             break;
297         }
298         in->active = 1;
299         rate_start (&in->rate);
300         spice_server_record_start (&in->sin);
301         break;
302     case VOICE_DISABLE:
303         if (!in->active) {
304             break;
305         }
306         in->active = 0;
307         spice_server_record_stop (&in->sin);
308         break;
309     case VOICE_VOLUME:
310         {
311 #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
312             SWVoiceIn *sw;
313             va_list ap;
314             uint16_t vol[2];
315
316             va_start (ap, cmd);
317             sw = va_arg (ap, SWVoiceIn *);
318             va_end (ap);
319
320             vol[0] = sw->vol.l / ((1ULL << 16) + 1);
321             vol[1] = sw->vol.r / ((1ULL << 16) + 1);
322             spice_server_record_set_volume (&in->sin, 2, vol);
323             spice_server_record_set_mute (&in->sin, sw->vol.mute);
324 #endif
325             break;
326         }
327     }
328
329     return 0;
330 }
331
332 static struct audio_pcm_ops audio_callbacks = {
333     .init_out = line_out_init,
334     .fini_out = line_out_fini,
335     .write    = audio_generic_write,
336     .get_buffer_out = line_out_get_buffer,
337     .put_buffer_out = line_out_put_buffer,
338     .ctl_out  = line_out_ctl,
339
340     .init_in  = line_in_init,
341     .fini_in  = line_in_fini,
342     .read     = line_in_read,
343     .ctl_in   = line_in_ctl,
344 };
345
346 static struct audio_driver spice_audio_driver = {
347     .name           = "spice",
348     .descr          = "spice audio driver",
349     .init           = spice_audio_init,
350     .fini           = spice_audio_fini,
351     .pcm_ops        = &audio_callbacks,
352     .max_voices_out = 1,
353     .max_voices_in  = 1,
354     .voice_size_out = sizeof (SpiceVoiceOut),
355     .voice_size_in  = sizeof (SpiceVoiceIn),
356 #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
357     .ctl_caps       = VOICE_VOLUME_CAP
358 #endif
359 };
360
361 void qemu_spice_audio_init (void)
362 {
363     spice_audio_driver.can_be_default = 1;
364 }
365
366 static void register_audio_spice(void)
367 {
368     audio_driver_register(&spice_audio_driver);
369 }
370 type_init(register_audio_spice);