| 1 | /**************************************************************************/ |
| 2 | /* audio_driver_alsa.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "audio_driver_alsa.h" |
| 32 | |
| 33 | #ifdef ALSA_ENABLED |
| 34 | |
| 35 | #include "core/config/project_settings.h" |
| 36 | #include "core/os/os.h" |
| 37 | |
| 38 | #include <errno.h> |
| 39 | |
| 40 | #if defined(PULSEAUDIO_ENABLED) && defined(SOWRAP_ENABLED) |
| 41 | extern "C" { |
| 42 | extern int initialize_pulse(int verbose); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | Error AudioDriverALSA::init_output_device() { |
| 47 | mix_rate = _get_configured_mix_rate(); |
| 48 | |
| 49 | speaker_mode = SPEAKER_MODE_STEREO; |
| 50 | channels = 2; |
| 51 | |
| 52 | // If there is a specified output device check that it is really present |
| 53 | if (output_device_name != "Default" ) { |
| 54 | PackedStringArray list = get_output_device_list(); |
| 55 | if (list.find(output_device_name) == -1) { |
| 56 | output_device_name = "Default" ; |
| 57 | new_output_device = "Default" ; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int status; |
| 62 | snd_pcm_hw_params_t *hwparams; |
| 63 | snd_pcm_sw_params_t *swparams; |
| 64 | |
| 65 | #define CHECK_FAIL(m_cond) \ |
| 66 | if (m_cond) { \ |
| 67 | fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \ |
| 68 | if (pcm_handle) { \ |
| 69 | snd_pcm_close(pcm_handle); \ |
| 70 | pcm_handle = nullptr; \ |
| 71 | } \ |
| 72 | ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \ |
| 73 | } |
| 74 | |
| 75 | //todo, add |
| 76 | //6 chans - "plug:surround51" |
| 77 | //4 chans - "plug:surround40"; |
| 78 | |
| 79 | if (output_device_name == "Default" ) { |
| 80 | status = snd_pcm_open(&pcm_handle, "default" , SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); |
| 81 | } else { |
| 82 | String device = output_device_name; |
| 83 | int pos = device.find(";" ); |
| 84 | if (pos != -1) { |
| 85 | device = device.substr(0, pos); |
| 86 | } |
| 87 | status = snd_pcm_open(&pcm_handle, device.utf8().get_data(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); |
| 88 | } |
| 89 | |
| 90 | ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN); |
| 91 | |
| 92 | snd_pcm_hw_params_alloca(&hwparams); |
| 93 | |
| 94 | status = snd_pcm_hw_params_any(pcm_handle, hwparams); |
| 95 | CHECK_FAIL(status < 0); |
| 96 | |
| 97 | status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 98 | CHECK_FAIL(status < 0); |
| 99 | |
| 100 | //not interested in anything else |
| 101 | status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE); |
| 102 | CHECK_FAIL(status < 0); |
| 103 | |
| 104 | //todo: support 4 and 6 |
| 105 | status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2); |
| 106 | CHECK_FAIL(status < 0); |
| 107 | |
| 108 | status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, nullptr); |
| 109 | CHECK_FAIL(status < 0); |
| 110 | |
| 111 | // In ALSA the period size seems to be the one that will determine the actual latency |
| 112 | // Ref: https://www.alsa-project.org/main/index.php/FramesPeriods |
| 113 | unsigned int periods = 2; |
| 114 | int latency = Engine::get_singleton()->get_audio_output_latency(); |
| 115 | buffer_frames = closest_power_of_2(latency * mix_rate / 1000); |
| 116 | buffer_size = buffer_frames * periods; |
| 117 | period_size = buffer_frames; |
| 118 | |
| 119 | // set buffer size from project settings |
| 120 | status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size); |
| 121 | CHECK_FAIL(status < 0); |
| 122 | |
| 123 | status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, nullptr); |
| 124 | CHECK_FAIL(status < 0); |
| 125 | |
| 126 | print_verbose("Audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms" ); |
| 127 | |
| 128 | status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, nullptr); |
| 129 | CHECK_FAIL(status < 0); |
| 130 | |
| 131 | status = snd_pcm_hw_params(pcm_handle, hwparams); |
| 132 | CHECK_FAIL(status < 0); |
| 133 | |
| 134 | //snd_pcm_hw_params_free(&hwparams); |
| 135 | |
| 136 | snd_pcm_sw_params_alloca(&swparams); |
| 137 | |
| 138 | status = snd_pcm_sw_params_current(pcm_handle, swparams); |
| 139 | CHECK_FAIL(status < 0); |
| 140 | |
| 141 | status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size); |
| 142 | CHECK_FAIL(status < 0); |
| 143 | |
| 144 | status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1); |
| 145 | CHECK_FAIL(status < 0); |
| 146 | |
| 147 | status = snd_pcm_sw_params(pcm_handle, swparams); |
| 148 | CHECK_FAIL(status < 0); |
| 149 | |
| 150 | samples_in.resize(period_size * channels); |
| 151 | samples_out.resize(period_size * channels); |
| 152 | |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | Error AudioDriverALSA::init() { |
| 157 | #ifdef SOWRAP_ENABLED |
| 158 | #ifdef DEBUG_ENABLED |
| 159 | int dylibloader_verbose = 1; |
| 160 | #else |
| 161 | int dylibloader_verbose = 0; |
| 162 | #endif |
| 163 | #ifdef PULSEAUDIO_ENABLED |
| 164 | // On pulse enabled systems Alsa will silently use pulse. |
| 165 | // It doesn't matter if this fails as that likely means there is no pulse |
| 166 | initialize_pulse(dylibloader_verbose); |
| 167 | #endif |
| 168 | |
| 169 | if (initialize_asound(dylibloader_verbose)) { |
| 170 | return ERR_CANT_OPEN; |
| 171 | } |
| 172 | #endif |
| 173 | bool ver_ok = false; |
| 174 | String version = String::utf8(snd_asoundlib_version()); |
| 175 | Vector<String> ver_parts = version.split("." ); |
| 176 | if (ver_parts.size() >= 2) { |
| 177 | ver_ok = ((ver_parts[0].to_int() == 1 && ver_parts[1].to_int() >= 1)) || (ver_parts[0].to_int() > 1); // 1.1.0 |
| 178 | } |
| 179 | print_verbose(vformat("ALSA %s detected." , version)); |
| 180 | if (!ver_ok) { |
| 181 | print_verbose("Unsupported ALSA library version!" ); |
| 182 | return ERR_CANT_OPEN; |
| 183 | } |
| 184 | |
| 185 | active.clear(); |
| 186 | exit_thread.clear(); |
| 187 | |
| 188 | Error err = init_output_device(); |
| 189 | if (err == OK) { |
| 190 | thread.start(AudioDriverALSA::thread_func, this); |
| 191 | } |
| 192 | |
| 193 | return err; |
| 194 | } |
| 195 | |
| 196 | void AudioDriverALSA::thread_func(void *p_udata) { |
| 197 | AudioDriverALSA *ad = static_cast<AudioDriverALSA *>(p_udata); |
| 198 | |
| 199 | while (!ad->exit_thread.is_set()) { |
| 200 | ad->lock(); |
| 201 | ad->start_counting_ticks(); |
| 202 | |
| 203 | if (!ad->active.is_set()) { |
| 204 | for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) { |
| 205 | ad->samples_out.write[i] = 0; |
| 206 | } |
| 207 | |
| 208 | } else { |
| 209 | ad->audio_server_process(ad->period_size, ad->samples_in.ptrw()); |
| 210 | |
| 211 | for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) { |
| 212 | ad->samples_out.write[i] = ad->samples_in[i] >> 16; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int todo = ad->period_size; |
| 217 | int total = 0; |
| 218 | |
| 219 | while (todo && !ad->exit_thread.is_set()) { |
| 220 | int16_t *src = (int16_t *)ad->samples_out.ptr(); |
| 221 | int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo); |
| 222 | |
| 223 | if (wrote > 0) { |
| 224 | total += wrote; |
| 225 | todo -= wrote; |
| 226 | } else if (wrote == -EAGAIN) { |
| 227 | ad->stop_counting_ticks(); |
| 228 | ad->unlock(); |
| 229 | |
| 230 | OS::get_singleton()->delay_usec(1000); |
| 231 | |
| 232 | ad->lock(); |
| 233 | ad->start_counting_ticks(); |
| 234 | } else { |
| 235 | wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0); |
| 236 | if (wrote < 0) { |
| 237 | ERR_PRINT("ALSA: Failed and can't recover: " + String(snd_strerror(wrote))); |
| 238 | ad->active.clear(); |
| 239 | ad->exit_thread.set(); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // User selected a new output device, finish the current one so we'll init the new device. |
| 245 | if (ad->output_device_name != ad->new_output_device) { |
| 246 | ad->output_device_name = ad->new_output_device; |
| 247 | ad->finish_output_device(); |
| 248 | |
| 249 | Error err = ad->init_output_device(); |
| 250 | if (err != OK) { |
| 251 | ERR_PRINT("ALSA: init_output_device error" ); |
| 252 | ad->output_device_name = "Default" ; |
| 253 | ad->new_output_device = "Default" ; |
| 254 | |
| 255 | err = ad->init_output_device(); |
| 256 | if (err != OK) { |
| 257 | ad->active.clear(); |
| 258 | ad->exit_thread.set(); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | ad->stop_counting_ticks(); |
| 264 | ad->unlock(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void AudioDriverALSA::start() { |
| 269 | active.set(); |
| 270 | } |
| 271 | |
| 272 | int AudioDriverALSA::get_mix_rate() const { |
| 273 | return mix_rate; |
| 274 | } |
| 275 | |
| 276 | AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const { |
| 277 | return speaker_mode; |
| 278 | } |
| 279 | |
| 280 | PackedStringArray AudioDriverALSA::get_output_device_list() { |
| 281 | PackedStringArray list; |
| 282 | |
| 283 | list.push_back("Default" ); |
| 284 | |
| 285 | void **hints; |
| 286 | |
| 287 | if (snd_device_name_hint(-1, "pcm" , &hints) < 0) { |
| 288 | return list; |
| 289 | } |
| 290 | |
| 291 | for (void **n = hints; *n != nullptr; n++) { |
| 292 | char *name = snd_device_name_get_hint(*n, "NAME" ); |
| 293 | char *desc = snd_device_name_get_hint(*n, "DESC" ); |
| 294 | |
| 295 | if (name != nullptr && !strncmp(name, "plughw" , 6)) { |
| 296 | if (desc) { |
| 297 | list.push_back(String::utf8(name) + ";" + String::utf8(desc)); |
| 298 | } else { |
| 299 | list.push_back(String::utf8(name)); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (desc != nullptr) { |
| 304 | free(desc); |
| 305 | } |
| 306 | if (name != nullptr) { |
| 307 | free(name); |
| 308 | } |
| 309 | } |
| 310 | snd_device_name_free_hint(hints); |
| 311 | |
| 312 | return list; |
| 313 | } |
| 314 | |
| 315 | String AudioDriverALSA::get_output_device() { |
| 316 | return output_device_name; |
| 317 | } |
| 318 | |
| 319 | void AudioDriverALSA::set_output_device(const String &p_name) { |
| 320 | lock(); |
| 321 | new_output_device = p_name; |
| 322 | unlock(); |
| 323 | } |
| 324 | |
| 325 | void AudioDriverALSA::lock() { |
| 326 | mutex.lock(); |
| 327 | } |
| 328 | |
| 329 | void AudioDriverALSA::unlock() { |
| 330 | mutex.unlock(); |
| 331 | } |
| 332 | |
| 333 | void AudioDriverALSA::finish_output_device() { |
| 334 | if (pcm_handle) { |
| 335 | snd_pcm_close(pcm_handle); |
| 336 | pcm_handle = nullptr; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void AudioDriverALSA::finish() { |
| 341 | exit_thread.set(); |
| 342 | if (thread.is_started()) { |
| 343 | thread.wait_to_finish(); |
| 344 | } |
| 345 | |
| 346 | finish_output_device(); |
| 347 | } |
| 348 | |
| 349 | #endif // ALSA_ENABLED |
| 350 | |