audio_int.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * QEMU Audio subsystem header
  3. *
  4. * Copyright (c) 2003-2005 Vassili Karpov (malc)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef QEMU_AUDIO_INT_H
  25. #define QEMU_AUDIO_INT_H
  26. #ifdef CONFIG_AUDIO_COREAUDIO
  27. #define FLOAT_MIXENG
  28. /* #define RECIPROCAL */
  29. #endif
  30. #include "mixeng.h"
  31. #ifdef CONFIG_GIO
  32. #include <gio/gio.h>
  33. #endif
  34. struct audio_pcm_ops;
  35. struct audio_callback {
  36. void *opaque;
  37. audio_callback_fn fn;
  38. };
  39. struct audio_pcm_info {
  40. int bits;
  41. bool is_signed;
  42. bool is_float;
  43. int freq;
  44. int nchannels;
  45. int bytes_per_frame;
  46. int bytes_per_second;
  47. int swap_endianness;
  48. };
  49. typedef struct AudioState AudioState;
  50. typedef struct SWVoiceCap SWVoiceCap;
  51. typedef struct STSampleBuffer {
  52. size_t pos, size;
  53. st_sample *buffer;
  54. } STSampleBuffer;
  55. typedef struct HWVoiceOut {
  56. AudioState *s;
  57. int enabled;
  58. int poll_mode;
  59. int pending_disable;
  60. struct audio_pcm_info info;
  61. f_sample *clip;
  62. uint64_t ts_helper;
  63. STSampleBuffer mix_buf;
  64. void *buf_emul;
  65. size_t pos_emul, pending_emul, size_emul;
  66. size_t samples;
  67. QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
  68. QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
  69. struct audio_pcm_ops *pcm_ops;
  70. QLIST_ENTRY (HWVoiceOut) entries;
  71. } HWVoiceOut;
  72. typedef struct HWVoiceIn {
  73. AudioState *s;
  74. int enabled;
  75. int poll_mode;
  76. struct audio_pcm_info info;
  77. t_sample *conv;
  78. size_t total_samples_captured;
  79. uint64_t ts_helper;
  80. STSampleBuffer conv_buf;
  81. void *buf_emul;
  82. size_t pos_emul, pending_emul, size_emul;
  83. size_t samples;
  84. QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
  85. struct audio_pcm_ops *pcm_ops;
  86. QLIST_ENTRY (HWVoiceIn) entries;
  87. } HWVoiceIn;
  88. struct SWVoiceOut {
  89. QEMUSoundCard *card;
  90. AudioState *s;
  91. struct audio_pcm_info info;
  92. t_sample *conv;
  93. STSampleBuffer resample_buf;
  94. void *rate;
  95. size_t total_hw_samples_mixed;
  96. int active;
  97. int empty;
  98. HWVoiceOut *hw;
  99. char *name;
  100. struct mixeng_volume vol;
  101. struct audio_callback callback;
  102. QLIST_ENTRY (SWVoiceOut) entries;
  103. };
  104. struct SWVoiceIn {
  105. QEMUSoundCard *card;
  106. AudioState *s;
  107. int active;
  108. struct audio_pcm_info info;
  109. void *rate;
  110. size_t total_hw_samples_acquired;
  111. STSampleBuffer resample_buf;
  112. f_sample *clip;
  113. HWVoiceIn *hw;
  114. char *name;
  115. struct mixeng_volume vol;
  116. struct audio_callback callback;
  117. QLIST_ENTRY (SWVoiceIn) entries;
  118. };
  119. typedef struct audio_driver audio_driver;
  120. struct audio_driver {
  121. const char *name;
  122. const char *descr;
  123. void *(*init) (Audiodev *);
  124. void (*fini) (void *);
  125. #ifdef CONFIG_GIO
  126. void (*set_dbus_server) (AudioState *s, GDBusObjectManagerServer *manager, bool p2p);
  127. #endif
  128. struct audio_pcm_ops *pcm_ops;
  129. int can_be_default;
  130. int max_voices_out;
  131. int max_voices_in;
  132. size_t voice_size_out;
  133. size_t voice_size_in;
  134. QLIST_ENTRY(audio_driver) next;
  135. };
  136. struct audio_pcm_ops {
  137. int (*init_out)(HWVoiceOut *hw, audsettings *as, void *drv_opaque);
  138. void (*fini_out)(HWVoiceOut *hw);
  139. size_t (*write) (HWVoiceOut *hw, void *buf, size_t size);
  140. void (*run_buffer_out)(HWVoiceOut *hw);
  141. /*
  142. * Get the free output buffer size. This is an upper limit. The size
  143. * returned by function get_buffer_out may be smaller.
  144. */
  145. size_t (*buffer_get_free)(HWVoiceOut *hw);
  146. /*
  147. * get a buffer that after later can be passed to put_buffer_out; optional
  148. * returns the buffer, and writes it's size to size (in bytes)
  149. */
  150. void *(*get_buffer_out)(HWVoiceOut *hw, size_t *size);
  151. /*
  152. * put back the buffer returned by get_buffer_out; optional
  153. * buf must be equal the pointer returned by get_buffer_out,
  154. * size may be smaller
  155. */
  156. size_t (*put_buffer_out)(HWVoiceOut *hw, void *buf, size_t size);
  157. void (*enable_out)(HWVoiceOut *hw, bool enable);
  158. void (*volume_out)(HWVoiceOut *hw, Volume *vol);
  159. int (*init_in) (HWVoiceIn *hw, audsettings *as, void *drv_opaque);
  160. void (*fini_in) (HWVoiceIn *hw);
  161. size_t (*read) (HWVoiceIn *hw, void *buf, size_t size);
  162. void (*run_buffer_in)(HWVoiceIn *hw);
  163. void *(*get_buffer_in)(HWVoiceIn *hw, size_t *size);
  164. void (*put_buffer_in)(HWVoiceIn *hw, void *buf, size_t size);
  165. void (*enable_in)(HWVoiceIn *hw, bool enable);
  166. void (*volume_in)(HWVoiceIn *hw, Volume *vol);
  167. };
  168. void audio_generic_run_buffer_in(HWVoiceIn *hw);
  169. void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
  170. void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
  171. void audio_generic_run_buffer_out(HWVoiceOut *hw);
  172. size_t audio_generic_buffer_get_free(HWVoiceOut *hw);
  173. void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
  174. size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size);
  175. size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size);
  176. size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size);
  177. struct capture_callback {
  178. struct audio_capture_ops ops;
  179. void *opaque;
  180. QLIST_ENTRY (capture_callback) entries;
  181. };
  182. struct CaptureVoiceOut {
  183. HWVoiceOut hw;
  184. void *buf;
  185. QLIST_HEAD (cb_listhead, capture_callback) cb_head;
  186. QLIST_ENTRY (CaptureVoiceOut) entries;
  187. };
  188. struct SWVoiceCap {
  189. SWVoiceOut sw;
  190. CaptureVoiceOut *cap;
  191. QLIST_ENTRY (SWVoiceCap) entries;
  192. };
  193. typedef struct AudioState {
  194. struct audio_driver *drv;
  195. Audiodev *dev;
  196. void *drv_opaque;
  197. QEMUTimer *ts;
  198. QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
  199. QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
  200. QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
  201. QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
  202. int nb_hw_voices_out;
  203. int nb_hw_voices_in;
  204. int vm_running;
  205. int64_t period_ticks;
  206. bool timer_running;
  207. uint64_t timer_last;
  208. QTAILQ_ENTRY(AudioState) list;
  209. } AudioState;
  210. extern const struct mixeng_volume nominal_volume;
  211. extern const char *audio_prio_list[];
  212. void audio_driver_register(audio_driver *drv);
  213. audio_driver *audio_driver_lookup(const char *name);
  214. void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
  215. void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
  216. int audio_bug (const char *funcname, int cond);
  217. void audio_run(AudioState *s, const char *msg);
  218. const char *audio_application_name(void);
  219. typedef struct RateCtl {
  220. int64_t start_ticks;
  221. int64_t bytes_sent;
  222. } RateCtl;
  223. void audio_rate_start(RateCtl *rate);
  224. size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info);
  225. void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used);
  226. size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
  227. size_t bytes_avail);
  228. static inline size_t audio_ring_dist(size_t dst, size_t src, size_t len)
  229. {
  230. return (dst >= src) ? (dst - src) : (len - src + dst);
  231. }
  232. /**
  233. * audio_ring_posb() - returns new position in ringbuffer in backward
  234. * direction at given distance
  235. *
  236. * @pos: current position in ringbuffer
  237. * @dist: distance in ringbuffer to walk in reverse direction
  238. * @len: size of ringbuffer
  239. */
  240. static inline size_t audio_ring_posb(size_t pos, size_t dist, size_t len)
  241. {
  242. return pos >= dist ? pos - dist : len - dist + pos;
  243. }
  244. #define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
  245. #ifdef DEBUG
  246. #define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
  247. #else
  248. #define ldebug(fmt, ...) (void)0
  249. #endif
  250. typedef struct AudiodevListEntry {
  251. Audiodev *dev;
  252. QSIMPLEQ_ENTRY(AudiodevListEntry) next;
  253. } AudiodevListEntry;
  254. typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead;
  255. AudiodevListHead audio_handle_legacy_opts(void);
  256. void audio_free_audiodev_list(AudiodevListHead *head);
  257. void audio_create_pdos(Audiodev *dev);
  258. AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev);
  259. AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev);
  260. #endif /* QEMU_AUDIO_INT_H */