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 25 #ifndef QEMU_AUDIO_INT_H 26 #define QEMU_AUDIO_INT_H 27 28 #ifdef CONFIG_AUDIO_COREAUDIO 29 #define FLOAT_MIXENG 30 /* #define RECIPROCAL */ 31 #endif 32 #include "qemu/audio.h" 33 #include "qemu/audio-capture.h" 34 #include "mixeng.h" 35 36 #ifdef CONFIG_GIO 37 #include <gio/gio.h> 38 #endif 39 40 void G_GNUC_PRINTF(2, 0) 41 AUD_vlog(const char *cap, const char *fmt, va_list ap); 42 43 void G_GNUC_PRINTF(2, 3) 44 AUD_log(const char *cap, const char *fmt, ...); 45 46 struct audio_pcm_ops; 47 48 struct audio_callback { 49 void *opaque; 50 audio_callback_fn fn; 51 }; 52 53 struct audio_pcm_info { 54 int bits; 55 bool is_signed; 56 bool is_float; 57 int freq; 58 int nchannels; 59 int bytes_per_frame; 60 int bytes_per_second; 61 int swap_endianness; 62 }; 63 64 typedef struct AudioBackend AudioBackend; 65 typedef struct SWVoiceCap SWVoiceCap; 66 67 typedef struct STSampleBuffer { 68 size_t pos, size; 69 st_sample *buffer; 70 } STSampleBuffer; 71 72 typedef struct HWVoiceOut { 73 AudioBackend *s; 74 bool enabled; 75 int poll_mode; 76 bool pending_disable; 77 struct audio_pcm_info info; 78 79 f_sample *clip; 80 uint64_t ts_helper; 81 82 STSampleBuffer mix_buf; 83 void *buf_emul; 84 size_t pos_emul, pending_emul, size_emul; 85 86 size_t samples; 87 QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head; 88 QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head; 89 struct audio_pcm_ops *pcm_ops; 90 QLIST_ENTRY (HWVoiceOut) entries; 91 } HWVoiceOut; 92 93 typedef struct HWVoiceIn { 94 AudioBackend *s; 95 bool enabled; 96 int poll_mode; 97 struct audio_pcm_info info; 98 99 t_sample *conv; 100 101 size_t total_samples_captured; 102 uint64_t ts_helper; 103 104 STSampleBuffer conv_buf; 105 void *buf_emul; 106 size_t pos_emul, pending_emul, size_emul; 107 108 size_t samples; 109 QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head; 110 struct audio_pcm_ops *pcm_ops; 111 QLIST_ENTRY (HWVoiceIn) entries; 112 } HWVoiceIn; 113 114 struct SWVoiceOut { 115 AudioBackend *s; 116 struct audio_pcm_info info; 117 t_sample *conv; 118 STSampleBuffer resample_buf; 119 void *rate; 120 size_t total_hw_samples_mixed; 121 bool active; 122 bool empty; 123 HWVoiceOut *hw; 124 char *name; 125 struct mixeng_volume vol; 126 struct audio_callback callback; 127 QLIST_ENTRY (SWVoiceOut) entries; 128 }; 129 130 struct SWVoiceIn { 131 AudioBackend *s; 132 bool active; 133 struct audio_pcm_info info; 134 void *rate; 135 size_t total_hw_samples_acquired; 136 STSampleBuffer resample_buf; 137 f_sample *clip; 138 HWVoiceIn *hw; 139 char *name; 140 struct mixeng_volume vol; 141 struct audio_callback callback; 142 QLIST_ENTRY (SWVoiceIn) entries; 143 }; 144 145 typedef struct audio_driver audio_driver; 146 struct audio_driver { 147 const char *name; 148 void *(*init) (Audiodev *, Error **); 149 void (*fini) (void *); 150 #ifdef CONFIG_GIO 151 bool (*set_dbus_server)(AudioBackend *be, 152 GDBusObjectManagerServer *manager, 153 bool p2p, 154 Error **errp); 155 #endif 156 struct audio_pcm_ops *pcm_ops; 157 int max_voices_out; 158 int max_voices_in; 159 size_t voice_size_out; 160 size_t voice_size_in; 161 QLIST_ENTRY(audio_driver) next; 162 }; 163 164 struct audio_pcm_ops { 165 int (*init_out)(HWVoiceOut *hw, audsettings *as, void *drv_opaque); 166 void (*fini_out)(HWVoiceOut *hw); 167 size_t (*write) (HWVoiceOut *hw, void *buf, size_t size); 168 void (*run_buffer_out)(HWVoiceOut *hw); 169 /* 170 * Get the free output buffer size. This is an upper limit. The size 171 * returned by function get_buffer_out may be smaller. 172 */ 173 size_t (*buffer_get_free)(HWVoiceOut *hw); 174 /* 175 * get a buffer that after later can be passed to put_buffer_out; optional 176 * returns the buffer, and writes it's size to size (in bytes) 177 */ 178 void *(*get_buffer_out)(HWVoiceOut *hw, size_t *size); 179 /* 180 * put back the buffer returned by get_buffer_out; optional 181 * buf must be equal the pointer returned by get_buffer_out, 182 * size may be smaller 183 */ 184 size_t (*put_buffer_out)(HWVoiceOut *hw, void *buf, size_t size); 185 void (*enable_out)(HWVoiceOut *hw, bool enable); 186 void (*volume_out)(HWVoiceOut *hw, Volume *vol); 187 188 int (*init_in) (HWVoiceIn *hw, audsettings *as, void *drv_opaque); 189 void (*fini_in) (HWVoiceIn *hw); 190 size_t (*read) (HWVoiceIn *hw, void *buf, size_t size); 191 void (*run_buffer_in)(HWVoiceIn *hw); 192 void *(*get_buffer_in)(HWVoiceIn *hw, size_t *size); 193 void (*put_buffer_in)(HWVoiceIn *hw, void *buf, size_t size); 194 void (*enable_in)(HWVoiceIn *hw, bool enable); 195 void (*volume_in)(HWVoiceIn *hw, Volume *vol); 196 }; 197 198 audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo); 199 int audioformat_bytes_per_sample(AudioFormat fmt); 200 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo, 201 audsettings *as, int def_usecs); 202 int audio_buffer_samples(AudiodevPerDirectionOptions *pdo, 203 audsettings *as, int def_usecs); 204 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, 205 audsettings *as, int def_usecs); 206 207 static inline void *advance(void *p, size_t incr) 208 { 209 return (uint8_t *)p + incr; 210 } 211 212 int wav_start_capture(AudioBackend *state, CaptureState *s, const char *path, 213 int freq, int bits, int nchannels); 214 215 void audio_generic_run_buffer_in(HWVoiceIn *hw); 216 void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size); 217 void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size); 218 void audio_generic_run_buffer_out(HWVoiceOut *hw); 219 size_t audio_generic_buffer_get_free(HWVoiceOut *hw); 220 void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size); 221 size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size); 222 size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size); 223 size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size); 224 225 struct capture_callback { 226 struct audio_capture_ops ops; 227 void *opaque; 228 QLIST_ENTRY (capture_callback) entries; 229 }; 230 231 struct CaptureVoiceOut { 232 HWVoiceOut hw; 233 void *buf; 234 QLIST_HEAD (cb_listhead, capture_callback) cb_head; 235 QLIST_ENTRY (CaptureVoiceOut) entries; 236 }; 237 238 struct SWVoiceCap { 239 SWVoiceOut sw; 240 CaptureVoiceOut *cap; 241 QLIST_ENTRY (SWVoiceCap) entries; 242 }; 243 244 typedef struct AudioBackend { 245 Object parent; 246 247 struct audio_driver *drv; 248 Audiodev *dev; 249 void *drv_opaque; 250 251 QEMUTimer *ts; 252 QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in; 253 QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out; 254 QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head; 255 int nb_hw_voices_out; 256 int nb_hw_voices_in; 257 int vm_running; 258 int64_t period_ticks; 259 260 bool timer_running; 261 uint64_t timer_last; 262 VMChangeStateEntry *vmse; 263 } AudioBackend; 264 265 extern const struct mixeng_volume nominal_volume; 266 267 extern const char *audio_prio_list[]; 268 269 void audio_driver_register(audio_driver *drv); 270 271 void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as); 272 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len); 273 274 int audio_bug (const char *funcname, int cond); 275 276 void audio_run(AudioBackend *s, const char *msg); 277 278 const char *audio_application_name(void); 279 280 typedef struct RateCtl { 281 int64_t start_ticks; 282 int64_t bytes_sent; 283 int64_t peeked_frames; 284 } RateCtl; 285 286 void audio_rate_start(RateCtl *rate); 287 size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info); 288 void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used); 289 size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info, 290 size_t bytes_avail); 291 292 static inline size_t audio_ring_dist(size_t dst, size_t src, size_t len) 293 { 294 return (dst >= src) ? (dst - src) : (len - src + dst); 295 } 296 297 /** 298 * audio_ring_posb() - returns new position in ringbuffer in backward 299 * direction at given distance 300 * 301 * @pos: current position in ringbuffer 302 * @dist: distance in ringbuffer to walk in reverse direction 303 * @len: size of ringbuffer 304 */ 305 static inline size_t audio_ring_posb(size_t pos, size_t dist, size_t len) 306 { 307 return pos >= dist ? pos - dist : len - dist + pos; 308 } 309 310 #define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__) 311 312 #ifdef DEBUG 313 #define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__) 314 #else 315 #define ldebug(fmt, ...) (void)0 316 #endif 317 318 typedef struct AudiodevListEntry { 319 Audiodev *dev; 320 QSIMPLEQ_ENTRY(AudiodevListEntry) next; 321 } AudiodevListEntry; 322 323 typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead; 324 325 void audio_create_pdos(Audiodev *dev); 326 AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev); 327 AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev); 328 329 #endif /* QEMU_AUDIO_INT_H */ 330