| 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_H |
| 26 | #define QEMU_AUDIO_H |
| 27 | |
| 28 | #include "qemu/queue.h" |
| 29 | #include "qapi/qapi-types-audio.h" |
| 30 | #include "hw/core/qdev-properties-system.h" |
| 31 | #ifdef CONFIG_GIO |
| 32 | #include "gio/gio.h" |
| 33 | #endif |
| 34 | |
| 35 | typedef void (*audio_callback_fn) (void *opaque, int avail); |
| 36 | |
| 37 | typedef struct audsettings { |
| 38 | int freq; |
| 39 | int nchannels; |
| 40 | AudioFormat fmt; |
| 41 | bool big_endian; |
| 42 | } audsettings; |
| 43 | |
| 44 | typedef struct SWVoiceOut SWVoiceOut; |
| 45 | typedef struct SWVoiceIn SWVoiceIn; |
| 46 | typedef struct CaptureVoiceOut CaptureVoiceOut; |
| 47 | |
| 48 | typedef enum { |
| 49 | AUD_CNOTIFY_ENABLE, |
| 50 | AUD_CNOTIFY_DISABLE |
| 51 | } audcnotification_e; |
| 52 | |
| 53 | struct audio_capture_ops { |
| 54 | void (*notify) (void *opaque, audcnotification_e cmd); |
| 55 | void (*capture) (void *opaque, const void *buf, int size); |
| 56 | void (*destroy) (void *opaque); |
| 57 | }; |
| 58 | |
| 59 | #define AUDIO_MAX_CHANNELS 16 |
| 60 | typedef struct Volume { |
| 61 | bool mute; |
| 62 | int channels; |
| 63 | uint8_t vol[AUDIO_MAX_CHANNELS]; |
| 64 | } Volume; |
| 65 | |
| 66 | typedef struct AudioBackend { |
| 67 | Object parent_obj; |
| 68 | } AudioBackend; |
| 69 | |
| 70 | typedef struct AudioBackendClass { |
| 71 | ObjectClass parent_class; |
| 72 | |
| 73 | bool (*realize)(AudioBackend *be, Audiodev *dev, Error **errp); |
| 74 | const char *(*get_id)(AudioBackend *be); |
| 75 | SWVoiceOut *(*open_out)(AudioBackend *be, |
| 76 | SWVoiceOut *sw, |
| 77 | const char *name, |
| 78 | void *callback_opaque, |
| 79 | audio_callback_fn callback_fn, |
| 80 | const struct audsettings *as); |
| 81 | SWVoiceIn *(*open_in)(AudioBackend *be, |
| 82 | SWVoiceIn *sw, |
| 83 | const char *name, |
| 84 | void *callback_opaque, |
| 85 | audio_callback_fn callback_fn, |
| 86 | const struct audsettings *as); |
| 87 | void (*close_out)(AudioBackend *be, SWVoiceOut *sw); |
| 88 | void (*close_in)(AudioBackend *be, SWVoiceIn *sw); |
| 89 | bool (*is_active_out)(AudioBackend *be, SWVoiceOut *sw); |
| 90 | bool (*is_active_in)(AudioBackend *be, SWVoiceIn *sw); |
| 91 | void (*set_active_out)(AudioBackend *be, SWVoiceOut *sw, bool on); |
| 92 | void (*set_active_in)(AudioBackend *be, SWVoiceIn *sw, bool on); |
| 93 | void (*set_volume_out)(AudioBackend *be, SWVoiceOut *sw, Volume *vol); |
| 94 | void (*set_volume_in)(AudioBackend *be, SWVoiceIn *sw, Volume *vol); |
| 95 | size_t (*write)(AudioBackend *be, SWVoiceOut *sw, void *buf, size_t size); |
| 96 | size_t (*read)(AudioBackend *be, SWVoiceIn *sw, void *buf, size_t size); |
| 97 | int (*get_buffer_size_out)(AudioBackend *be, SWVoiceOut *sw); |
| 98 | CaptureVoiceOut *(*add_capture)(AudioBackend *be, |
| 99 | const struct audsettings *as, |
| 100 | const struct audio_capture_ops *ops, |
| 101 | void *cb_opaque); |
| 102 | void (*del_capture)(AudioBackend *be, CaptureVoiceOut *cap, void *cb_opaque); |
| 103 | |
| 104 | #ifdef CONFIG_GIO |
| 105 | bool (*set_dbus_server)(AudioBackend *be, |
| 106 | GDBusObjectManagerServer *manager, |
| 107 | bool p2p, |
| 108 | Error **errp); |
| 109 | #endif |
| 110 | } AudioBackendClass; |
| 111 | |
| 112 | bool audio_be_check(AudioBackend **be, Error **errp); |
| 113 | |
| 114 | AudioBackend *audio_be_new(Audiodev *dev, Error **errp); |
| 115 | |
| 116 | SWVoiceOut *audio_be_open_out( |
| 117 | AudioBackend *be, |
| 118 | SWVoiceOut *sw, |
| 119 | const char *name, |
| 120 | void *callback_opaque, |
| 121 | audio_callback_fn callback_fn, |
| 122 | const struct audsettings *settings); |
| 123 | |
| 124 | void audio_be_close_out(AudioBackend *be, SWVoiceOut *sw); |
| 125 | size_t audio_be_write(AudioBackend *be, SWVoiceOut *sw, void *pcm_buf, size_t size); |
| 126 | int audio_be_get_buffer_size_out(AudioBackend *be, SWVoiceOut *sw); |
| 127 | void audio_be_set_active_out(AudioBackend *be, SWVoiceOut *sw, bool on); |
| 128 | bool audio_be_is_active_out(AudioBackend *be, SWVoiceOut *sw); |
| 129 | |
| 130 | |
| 131 | void audio_be_set_volume_out(AudioBackend *be, SWVoiceOut *sw, Volume *vol); |
| 132 | void audio_be_set_volume_in(AudioBackend *be, SWVoiceIn *sw, Volume *vol); |
| 133 | |
| 134 | static inline void |
| 135 | audio_be_set_volume_out_lr(AudioBackend *be, SWVoiceOut *sw, |
| 136 | bool mut, uint8_t lvol, uint8_t rvol) { |
| 137 | audio_be_set_volume_out(be, sw, &(Volume) { |
| 138 | .mute = mut, .channels = 2, .vol = { lvol, rvol } |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | static inline void |
| 143 | audio_be_set_volume_in_lr(AudioBackend *be, SWVoiceIn *sw, |
| 144 | bool mut, uint8_t lvol, uint8_t rvol) { |
| 145 | audio_be_set_volume_in(be, sw, &(Volume) { |
| 146 | .mute = mut, .channels = 2, .vol = { lvol, rvol } |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | SWVoiceIn *audio_be_open_in( |
| 151 | AudioBackend *be, |
| 152 | SWVoiceIn *sw, |
| 153 | const char *name, |
| 154 | void *callback_opaque, |
| 155 | audio_callback_fn callback_fn, |
| 156 | const struct audsettings *settings |
| 157 | ); |
| 158 | |
| 159 | void audio_be_close_in(AudioBackend *be, SWVoiceIn *sw); |
| 160 | size_t audio_be_read(AudioBackend *be, SWVoiceIn *sw, void *pcm_buf, size_t size); |
| 161 | void audio_be_set_active_in(AudioBackend *be, SWVoiceIn *sw, bool on); |
| 162 | bool audio_be_is_active_in(AudioBackend *be, SWVoiceIn *sw); |
| 163 | |
| 164 | void audio_cleanup(void); |
| 165 | |
| 166 | typedef struct st_sample st_sample; |
| 167 | |
| 168 | void audio_add_audiodev(Audiodev *audio); |
| 169 | void audio_add_default_audiodev(Audiodev *dev, Error **errp); |
| 170 | void audio_parse_option(const char *opt); |
| 171 | void audio_create_default_audiodevs(void); |
| 172 | void audio_init_audiodevs(void); |
| 173 | void audio_help(void); |
| 174 | |
| 175 | AudioBackend *audio_be_by_name(const char *name, Error **errp); |
| 176 | AudioBackend *audio_get_default_audio_be(Error **errp); |
| 177 | const char *audio_be_get_id(AudioBackend *be); |
| 178 | #ifdef CONFIG_GIO |
| 179 | bool audio_be_can_set_dbus_server(AudioBackend *be); |
| 180 | bool audio_be_set_dbus_server(AudioBackend *be, |
| 181 | GDBusObjectManagerServer *server, |
| 182 | bool p2p, |
| 183 | Error **errp); |
| 184 | #endif |
| 185 | |
| 186 | const char *audio_application_name(void); |
| 187 | |
| 188 | static inline int audio_format_bits(AudioFormat fmt) |
| 189 | { |
| 190 | switch (fmt) { |
| 191 | case AUDIO_FORMAT_S8: |
| 192 | case AUDIO_FORMAT_U8: |
| 193 | return 8; |
| 194 | |
| 195 | case AUDIO_FORMAT_S16: |
| 196 | case AUDIO_FORMAT_U16: |
| 197 | return 16; |
| 198 | |
| 199 | case AUDIO_FORMAT_F32: |
| 200 | case AUDIO_FORMAT_S32: |
| 201 | case AUDIO_FORMAT_U32: |
| 202 | return 32; |
| 203 | |
| 204 | case AUDIO_FORMAT__MAX: |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | g_assert_not_reached(); |
| 209 | } |
| 210 | |
| 211 | static inline bool audio_format_is_float(AudioFormat fmt) |
| 212 | { |
| 213 | return fmt == AUDIO_FORMAT_F32; |
| 214 | } |
| 215 | |
| 216 | static inline bool audio_format_is_signed(AudioFormat fmt) |
| 217 | { |
| 218 | switch (fmt) { |
| 219 | case AUDIO_FORMAT_S8: |
| 220 | case AUDIO_FORMAT_S16: |
| 221 | case AUDIO_FORMAT_S32: |
| 222 | case AUDIO_FORMAT_F32: |
| 223 | return true; |
| 224 | |
| 225 | case AUDIO_FORMAT_U8: |
| 226 | case AUDIO_FORMAT_U16: |
| 227 | case AUDIO_FORMAT_U32: |
| 228 | return false; |
| 229 | |
| 230 | case AUDIO_FORMAT__MAX: |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | g_assert_not_reached(); |
| 235 | } |
| 236 | |
| 237 | #define DEFINE_AUDIO_PROPERTIES(_s, _f) \ |
| 238 | DEFINE_PROP_AUDIODEV("audiodev", _s, _f) |
| 239 | |
| 240 | #define TYPE_AUDIO_BACKEND "audio-backend" |
| 241 | OBJECT_DECLARE_TYPE(AudioBackend, AudioBackendClass, AUDIO_BACKEND) |
| 242 | |
| 243 | #endif /* QEMU_AUDIO_H */ |