spiceaudio.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include "qemu/osdep.h"
  20. #include "qemu/host-utils.h"
  21. #include "qemu/module.h"
  22. #include "qemu/error-report.h"
  23. #include "qemu/timer.h"
  24. #include "qapi/error.h"
  25. #include "ui/qemu-spice.h"
  26. #define AUDIO_CAP "spice"
  27. #include "audio.h"
  28. #include "audio_int.h"
  29. #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
  30. #define LINE_OUT_SAMPLES (480 * 4)
  31. #else
  32. #define LINE_OUT_SAMPLES (256 * 4)
  33. #endif
  34. #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
  35. #define LINE_IN_SAMPLES (480 * 4)
  36. #else
  37. #define LINE_IN_SAMPLES (256 * 4)
  38. #endif
  39. typedef struct SpiceVoiceOut {
  40. HWVoiceOut hw;
  41. SpicePlaybackInstance sin;
  42. RateCtl rate;
  43. int active;
  44. uint32_t *frame;
  45. uint32_t fpos;
  46. uint32_t fsize;
  47. } SpiceVoiceOut;
  48. typedef struct SpiceVoiceIn {
  49. HWVoiceIn hw;
  50. SpiceRecordInstance sin;
  51. RateCtl rate;
  52. int active;
  53. } SpiceVoiceIn;
  54. static const SpicePlaybackInterface playback_sif = {
  55. .base.type = SPICE_INTERFACE_PLAYBACK,
  56. .base.description = "playback",
  57. .base.major_version = SPICE_INTERFACE_PLAYBACK_MAJOR,
  58. .base.minor_version = SPICE_INTERFACE_PLAYBACK_MINOR,
  59. };
  60. static const SpiceRecordInterface record_sif = {
  61. .base.type = SPICE_INTERFACE_RECORD,
  62. .base.description = "record",
  63. .base.major_version = SPICE_INTERFACE_RECORD_MAJOR,
  64. .base.minor_version = SPICE_INTERFACE_RECORD_MINOR,
  65. };
  66. static void *spice_audio_init(Audiodev *dev, Error **errp)
  67. {
  68. if (!using_spice) {
  69. error_setg(errp, "Cannot use spice audio without -spice");
  70. return NULL;
  71. }
  72. return &spice_audio_init;
  73. }
  74. static void spice_audio_fini (void *opaque)
  75. {
  76. /* nothing */
  77. }
  78. /* playback */
  79. static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
  80. void *drv_opaque)
  81. {
  82. SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
  83. struct audsettings settings;
  84. #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
  85. settings.freq = spice_server_get_best_playback_rate(NULL);
  86. #else
  87. settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
  88. #endif
  89. settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
  90. settings.fmt = AUDIO_FORMAT_S16;
  91. settings.endianness = AUDIO_HOST_ENDIANNESS;
  92. audio_pcm_init_info (&hw->info, &settings);
  93. hw->samples = LINE_OUT_SAMPLES;
  94. out->active = 0;
  95. out->sin.base.sif = &playback_sif.base;
  96. qemu_spice.add_interface(&out->sin.base);
  97. #if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
  98. spice_server_set_playback_rate(&out->sin, settings.freq);
  99. #endif
  100. return 0;
  101. }
  102. static void line_out_fini (HWVoiceOut *hw)
  103. {
  104. SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
  105. spice_server_remove_interface (&out->sin.base);
  106. }
  107. static size_t line_out_get_free(HWVoiceOut *hw)
  108. {
  109. SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
  110. return audio_rate_peek_bytes(&out->rate, &hw->info);
  111. }
  112. static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)
  113. {
  114. SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
  115. if (!out->frame) {
  116. spice_server_playback_get_buffer(&out->sin, &out->frame, &out->fsize);
  117. out->fpos = 0;
  118. }
  119. if (out->frame) {
  120. *size = MIN((out->fsize - out->fpos) << 2, *size);
  121. }
  122. return out->frame + out->fpos;
  123. }
  124. static size_t line_out_put_buffer(HWVoiceOut *hw, void *buf, size_t size)
  125. {
  126. SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
  127. audio_rate_add_bytes(&out->rate, size);
  128. if (buf) {
  129. assert(buf == out->frame + out->fpos && out->fpos <= out->fsize);
  130. out->fpos += size >> 2;
  131. if (out->fpos == out->fsize) { /* buffer full */
  132. spice_server_playback_put_samples(&out->sin, out->frame);
  133. out->frame = NULL;
  134. }
  135. }
  136. return size;
  137. }
  138. static void line_out_enable(HWVoiceOut *hw, bool enable)
  139. {
  140. SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
  141. if (enable) {
  142. if (out->active) {
  143. return;
  144. }
  145. out->active = 1;
  146. audio_rate_start(&out->rate);
  147. spice_server_playback_start (&out->sin);
  148. } else {
  149. if (!out->active) {
  150. return;
  151. }
  152. out->active = 0;
  153. if (out->frame) {
  154. memset(out->frame + out->fpos, 0, (out->fsize - out->fpos) << 2);
  155. spice_server_playback_put_samples (&out->sin, out->frame);
  156. out->frame = NULL;
  157. }
  158. spice_server_playback_stop (&out->sin);
  159. }
  160. }
  161. #if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
  162. static void line_out_volume(HWVoiceOut *hw, Volume *vol)
  163. {
  164. SpiceVoiceOut *out = container_of(hw, SpiceVoiceOut, hw);
  165. uint16_t svol[2];
  166. assert(vol->channels == 2);
  167. svol[0] = vol->vol[0] * 257;
  168. svol[1] = vol->vol[1] * 257;
  169. spice_server_playback_set_volume(&out->sin, 2, svol);
  170. spice_server_playback_set_mute(&out->sin, vol->mute);
  171. }
  172. #endif
  173. /* record */
  174. static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
  175. {
  176. SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
  177. struct audsettings settings;
  178. #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
  179. settings.freq = spice_server_get_best_record_rate(NULL);
  180. #else
  181. settings.freq = SPICE_INTERFACE_RECORD_FREQ;
  182. #endif
  183. settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
  184. settings.fmt = AUDIO_FORMAT_S16;
  185. settings.endianness = AUDIO_HOST_ENDIANNESS;
  186. audio_pcm_init_info (&hw->info, &settings);
  187. hw->samples = LINE_IN_SAMPLES;
  188. in->active = 0;
  189. in->sin.base.sif = &record_sif.base;
  190. qemu_spice.add_interface(&in->sin.base);
  191. #if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
  192. spice_server_set_record_rate(&in->sin, settings.freq);
  193. #endif
  194. return 0;
  195. }
  196. static void line_in_fini (HWVoiceIn *hw)
  197. {
  198. SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
  199. spice_server_remove_interface (&in->sin.base);
  200. }
  201. static size_t line_in_read(HWVoiceIn *hw, void *buf, size_t len)
  202. {
  203. SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
  204. uint64_t to_read = audio_rate_get_bytes(&in->rate, &hw->info, len) >> 2;
  205. size_t ready = spice_server_record_get_samples(&in->sin, buf, to_read);
  206. /*
  207. * If the client didn't send new frames, it most likely disconnected.
  208. * Generate silence in this case to avoid a stalled audio stream.
  209. */
  210. if (ready == 0) {
  211. memset(buf, 0, to_read << 2);
  212. ready = to_read;
  213. }
  214. return ready << 2;
  215. }
  216. static void line_in_enable(HWVoiceIn *hw, bool enable)
  217. {
  218. SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
  219. if (enable) {
  220. if (in->active) {
  221. return;
  222. }
  223. in->active = 1;
  224. audio_rate_start(&in->rate);
  225. spice_server_record_start (&in->sin);
  226. } else {
  227. if (!in->active) {
  228. return;
  229. }
  230. in->active = 0;
  231. spice_server_record_stop (&in->sin);
  232. }
  233. }
  234. #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
  235. static void line_in_volume(HWVoiceIn *hw, Volume *vol)
  236. {
  237. SpiceVoiceIn *in = container_of(hw, SpiceVoiceIn, hw);
  238. uint16_t svol[2];
  239. assert(vol->channels == 2);
  240. svol[0] = vol->vol[0] * 257;
  241. svol[1] = vol->vol[1] * 257;
  242. spice_server_record_set_volume(&in->sin, 2, svol);
  243. spice_server_record_set_mute(&in->sin, vol->mute);
  244. }
  245. #endif
  246. static struct audio_pcm_ops audio_callbacks = {
  247. .init_out = line_out_init,
  248. .fini_out = line_out_fini,
  249. .write = audio_generic_write,
  250. .buffer_get_free = line_out_get_free,
  251. .get_buffer_out = line_out_get_buffer,
  252. .put_buffer_out = line_out_put_buffer,
  253. .enable_out = line_out_enable,
  254. #if (SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && \
  255. (SPICE_INTERFACE_PLAYBACK_MINOR >= 2)
  256. .volume_out = line_out_volume,
  257. #endif
  258. .init_in = line_in_init,
  259. .fini_in = line_in_fini,
  260. .read = line_in_read,
  261. .run_buffer_in = audio_generic_run_buffer_in,
  262. .enable_in = line_in_enable,
  263. #if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
  264. .volume_in = line_in_volume,
  265. #endif
  266. };
  267. static struct audio_driver spice_audio_driver = {
  268. .name = "spice",
  269. .descr = "spice audio driver",
  270. .init = spice_audio_init,
  271. .fini = spice_audio_fini,
  272. .pcm_ops = &audio_callbacks,
  273. .max_voices_out = 1,
  274. .max_voices_in = 1,
  275. .voice_size_out = sizeof (SpiceVoiceOut),
  276. .voice_size_in = sizeof (SpiceVoiceIn),
  277. };
  278. static void register_audio_spice(void)
  279. {
  280. audio_driver_register(&spice_audio_driver);
  281. }
  282. type_init(register_audio_spice);
  283. module_dep("ui-spice-core");