*/
#include "qemu/osdep.h"
-#include "hw/hw.h"
+#include "qemu/host-utils.h"
+#include "qemu/module.h"
#include "qemu/error-report.h"
#include "qemu/timer.h"
#include "ui/qemu-spice.h"
#define LINE_IN_SAMPLES (256 * 4)
#endif
-typedef struct SpiceRateCtl {
- int64_t start_ticks;
- int64_t bytes_sent;
-} SpiceRateCtl;
-
typedef struct SpiceVoiceOut {
HWVoiceOut hw;
SpicePlaybackInstance sin;
- SpiceRateCtl rate;
+ RateCtl rate;
int active;
uint32_t *frame;
- uint32_t *fpos;
+ uint32_t fpos;
uint32_t fsize;
} SpiceVoiceOut;
typedef struct SpiceVoiceIn {
HWVoiceIn hw;
SpiceRecordInstance sin;
- SpiceRateCtl rate;
+ RateCtl rate;
int active;
- uint32_t samples[LINE_IN_SAMPLES];
} SpiceVoiceIn;
static const SpicePlaybackInterface playback_sif = {
.base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
};
-static void *spice_audio_init (void)
+static void *spice_audio_init(Audiodev *dev)
{
if (!using_spice) {
return NULL;
/* nothing */
}
-static void rate_start (SpiceRateCtl *rate)
-{
- memset (rate, 0, sizeof (*rate));
- rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-}
-
-static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
-{
- int64_t now;
- int64_t ticks;
- int64_t bytes;
- int64_t samples;
-
- now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
- ticks = now - rate->start_ticks;
- bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
- samples = (bytes - rate->bytes_sent) >> info->shift;
- if (samples < 0 || samples > 65536) {
- error_report("Resetting rate control (%" PRId64 " samples)", samples);
- rate_start(rate);
- samples = 0;
- }
- rate->bytes_sent += samples << info->shift;
- return samples;
-}
-
/* playback */
static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
#endif
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
- settings.fmt = AUD_FMT_S16;
+ settings.fmt = AUDIO_FORMAT_S16;
settings.endianness = AUDIO_HOST_ENDIANNESS;
audio_pcm_init_info (&hw->info, &settings);
out->active = 0;
out->sin.base.sif = &playback_sif.base;
- qemu_spice_add_interface (&out->sin.base);
+ qemu_spice.add_interface(&out->sin.base);
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
spice_server_set_playback_rate(&out->sin, settings.freq);
#endif
spice_server_remove_interface (&out->sin.base);
}
-static int line_out_run (HWVoiceOut *hw, int live)
+static size_t line_out_get_free(HWVoiceOut *hw)
{
- SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
- int rpos, decr;
- int samples;
+ SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
- if (!live) {
- return 0;
- }
+ return audio_rate_peek_bytes(&out->rate, &hw->info);
+}
- decr = rate_get_samples (&hw->info, &out->rate);
- decr = audio_MIN (live, decr);
+static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
+{
+ SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
- samples = decr;
- rpos = hw->rpos;
- while (samples) {
- int left_till_end_samples = hw->samples - rpos;
- int len = audio_MIN (samples, left_till_end_samples);
+ if (!out->frame) {
+ spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
+ out->fpos = 0;
+ }
- if (!out->frame) {
- spice_server_playback_get_buffer (&out->sin, &out->frame, &out->fsize);
- out->fpos = out->frame;
- }
- if (out->frame) {
- len = audio_MIN (len, out->fsize);
- hw->clip (out->fpos, hw->mix_buf + rpos, len);
- out->fsize -= len;
- out->fpos += len;
- if (out->fsize == 0) {
- spice_server_playback_put_samples (&out->sin, out->frame);
- out->frame = out->fpos = NULL;
- }
- }
- rpos = (rpos + len) % hw->samples;
- samples -= len;
+ if (out->frame) {
+ *size = MIN((out->fsize - out->fpos) << 2, *size);
}
- hw->rpos = rpos;
- return decr;
+
+ return out->frame + out->fpos;
}
-static int line_out_write (SWVoiceOut *sw, void *buf, int len)
+static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
{
- return audio_pcm_sw_write (sw, buf, len);
+ SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
+
+ audio_rate_add_bytes(&out->rate, size);
+
+ if (buf) {
+ assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
+ out->fpos += size >> 2;
+
+ if (out->fpos == out->fsize) { /* buffer full */
+ spice_server_playback_put_samples(&out->sin, out->frame);
+ out->frame = NULL;
+ }
+ }
+
+ return size;
}
-static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
+static void line_out_enable(HWVoiceOut *hw, bool enable)
{
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
- switch (cmd) {
- case VOICE_ENABLE:
+ if (enable) {
if (out->active) {
- break;
+ return;
}
out->active = 1;
- rate_start (&out->rate);
+ audio_rate_start(&out->rate);
spice_server_playback_start (&out->sin);
- break;
- case VOICE_DISABLE:
+ } else {
if (!out->active) {
- break;
+ return;
}
out->active = 0;
if (out->frame) {
- memset (out->fpos, 0, out->fsize << 2);
+ memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
spice_server_playback_put_samples (&out->sin, out->frame);
- out->frame = out->fpos = NULL;
+ out->frame = NULL;
}
spice_server_playback_stop (&out->sin);
- break;
- case VOICE_VOLUME:
- {
-#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
- SWVoiceOut *sw;
- va_list ap;
- uint16_t vol[2];
-
- va_start (ap, cmd);
- sw = va_arg (ap, SWVoiceOut *);
- va_end (ap);
-
- vol[0] = sw->vol.l / ((1ULL << 16) + 1);
- vol[1] = sw->vol.r / ((1ULL << 16) + 1);
- spice_server_playback_set_volume (&out->sin, 2, vol);
- spice_server_playback_set_mute (&out->sin, sw->vol.mute);
-#endif
- break;
- }
}
+}
- return 0;
+#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
+static void line_out_volume(HWVoiceOut *hw, Volume *vol)
+{
+ SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
+ uint16_t svol[2];
+
+ assert(vol->channels == 2);
+ svol[0] = vol->vol[0] * 257;
+ svol[1] = vol->vol[1] * 257;
+ spice_server_playback_set_volume(&out->sin, 2, svol);
+ spice_server_playback_set_mute(&out->sin, vol->mute);
}
+#endif
/* record */
settings.freq = SPICE_INTERFACE_RECORD_FREQ;
#endif
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
- settings.fmt = AUD_FMT_S16;
+ settings.fmt = AUDIO_FORMAT_S16;
settings.endianness = AUDIO_HOST_ENDIANNESS;
audio_pcm_init_info (&hw->info, &settings);
in->active = 0;
in->sin.base.sif = &record_sif.base;
- qemu_spice_add_interface (&in->sin.base);
+ qemu_spice.add_interface(&in->sin.base);
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
spice_server_set_record_rate(&in->sin, settings.freq);
#endif
spice_server_remove_interface (&in->sin.base);
}
-static int line_in_run (HWVoiceIn *hw)
+static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
{
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
- int num_samples;
- int ready;
- int len[2];
- uint64_t delta_samp;
- const uint32_t *samples;
-
- if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in (hw))) {
- return 0;
- }
+ uint64_t to_read = audio_rate_get_bytes(&in->rate, &hw->info, len) >> 2;
+ size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
- delta_samp = rate_get_samples (&hw->info, &in->rate);
- num_samples = audio_MIN (num_samples, delta_samp);
-
- ready = spice_server_record_get_samples (&in->sin, in->samples, num_samples);
- samples = in->samples;
+ /*
+ * If the client didn't send new frames, it most likely disconnected.
+ * Generate silence in this case to avoid a stalled audio stream.
+ */
if (ready == 0) {
- static const uint32_t silence[LINE_IN_SAMPLES];
- samples = silence;
- ready = LINE_IN_SAMPLES;
- }
-
- num_samples = audio_MIN (ready, num_samples);
-
- if (hw->wpos + num_samples > hw->samples) {
- len[0] = hw->samples - hw->wpos;
- len[1] = num_samples - len[0];
- } else {
- len[0] = num_samples;
- len[1] = 0;
- }
-
- hw->conv (hw->conv_buf + hw->wpos, samples, len[0]);
-
- if (len[1]) {
- hw->conv (hw->conv_buf, samples + len[0], len[1]);
+ memset(buf, 0, to_read << 2);
+ ready = to_read;
}
- hw->wpos = (hw->wpos + num_samples) % hw->samples;
-
- return num_samples;
-}
-
-static int line_in_read (SWVoiceIn *sw, void *buf, int size)
-{
- return audio_pcm_sw_read (sw, buf, size);
+ return ready << 2;
}
-static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
+static void line_in_enable(HWVoiceIn *hw, bool enable)
{
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
- switch (cmd) {
- case VOICE_ENABLE:
+ if (enable) {
if (in->active) {
- break;
+ return;
}
in->active = 1;
- rate_start (&in->rate);
+ audio_rate_start(&in->rate);
spice_server_record_start (&in->sin);
- break;
- case VOICE_DISABLE:
+ } else {
if (!in->active) {
- break;
+ return;
}
in->active = 0;
spice_server_record_stop (&in->sin);
- break;
- case VOICE_VOLUME:
- {
-#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
- SWVoiceIn *sw;
- va_list ap;
- uint16_t vol[2];
-
- va_start (ap, cmd);
- sw = va_arg (ap, SWVoiceIn *);
- va_end (ap);
-
- vol[0] = sw->vol.l / ((1ULL << 16) + 1);
- vol[1] = sw->vol.r / ((1ULL << 16) + 1);
- spice_server_record_set_volume (&in->sin, 2, vol);
- spice_server_record_set_mute (&in->sin, sw->vol.mute);
-#endif
- break;
- }
}
-
- return 0;
}
-static struct audio_option audio_options[] = {
- { /* end of list */ },
-};
+#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
+static void line_in_volume(HWVoiceIn *hw, Volume *vol)
+{
+ SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
+ uint16_t svol[2];
+
+ assert(vol->channels == 2);
+ svol[0] = vol->vol[0] * 257;
+ svol[1] = vol->vol[1] * 257;
+ spice_server_record_set_volume(&in->sin, 2, svol);
+ spice_server_record_set_mute(&in->sin, vol->mute);
+}
+#endif
static struct audio_pcm_ops audio_callbacks = {
.init_out = line_out_init,
.fini_out = line_out_fini,
- .run_out = line_out_run,
- .write = line_out_write,
- .ctl_out = line_out_ctl,
+ .write = audio_generic_write,
+ .buffer_get_free = line_out_get_free,
+ .get_buffer_out = line_out_get_buffer,
+ .put_buffer_out = line_out_put_buffer,
+ .enable_out = line_out_enable,
+#if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
+ (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
+ .volume_out = line_out_volume,
+#endif
.init_in = line_in_init,
.fini_in = line_in_fini,
- .run_in = line_in_run,
.read = line_in_read,
- .ctl_in = line_in_ctl,
+ .run_buffer_in = audio_generic_run_buffer_in,
+ .enable_in = line_in_enable,
+#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
+ .volume_in = line_in_volume,
+#endif
};
-struct audio_driver spice_audio_driver = {
+static struct audio_driver spice_audio_driver = {
.name = "spice",
.descr = "spice audio driver",
- .options = audio_options,
.init = spice_audio_init,
.fini = spice_audio_fini,
.pcm_ops = &audio_callbacks,
.max_voices_in = 1,
.voice_size_out = sizeof (SpiceVoiceOut),
.voice_size_in = sizeof (SpiceVoiceIn),
-#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
- .ctl_caps = VOICE_VOLUME_CAP
-#endif
};
-void qemu_spice_audio_init (void)
+static void register_audio_spice(void)
{
- spice_audio_driver.can_be_default = 1;
+ audio_driver_register(&spice_audio_driver);
}
+type_init(register_audio_spice);
+
+module_dep("ui-spice-core");