audio_driver_alsa.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*************************************************************************/
  2. /* audio_driver_alsa.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "audio_driver_alsa.h"
  31. #ifdef ALSA_ENABLED
  32. #include "core/config/project_settings.h"
  33. #include "core/os/os.h"
  34. #include <errno.h>
  35. #ifdef PULSEAUDIO_ENABLED
  36. extern "C" {
  37. extern int initialize_pulse(int verbose);
  38. }
  39. #endif
  40. Error AudioDriverALSA::init_device() {
  41. mix_rate = GLOBAL_GET("audio/driver/mix_rate");
  42. speaker_mode = SPEAKER_MODE_STEREO;
  43. channels = 2;
  44. // If there is a specified device check that it is really present
  45. if (device_name != "Default") {
  46. PackedStringArray list = get_device_list();
  47. if (list.find(device_name) == -1) {
  48. device_name = "Default";
  49. new_device = "Default";
  50. }
  51. }
  52. int status;
  53. snd_pcm_hw_params_t *hwparams;
  54. snd_pcm_sw_params_t *swparams;
  55. #define CHECK_FAIL(m_cond) \
  56. if (m_cond) { \
  57. fprintf(stderr, "ALSA ERR: %s\n", snd_strerror(status)); \
  58. if (pcm_handle) { \
  59. snd_pcm_close(pcm_handle); \
  60. pcm_handle = nullptr; \
  61. } \
  62. ERR_FAIL_COND_V(m_cond, ERR_CANT_OPEN); \
  63. }
  64. //todo, add
  65. //6 chans - "plug:surround51"
  66. //4 chans - "plug:surround40";
  67. if (device_name == "Default") {
  68. status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
  69. } else {
  70. String device = device_name;
  71. int pos = device.find(";");
  72. if (pos != -1) {
  73. device = device.substr(0, pos);
  74. }
  75. status = snd_pcm_open(&pcm_handle, device.utf8().get_data(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
  76. }
  77. ERR_FAIL_COND_V(status < 0, ERR_CANT_OPEN);
  78. snd_pcm_hw_params_alloca(&hwparams);
  79. status = snd_pcm_hw_params_any(pcm_handle, hwparams);
  80. CHECK_FAIL(status < 0);
  81. status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  82. CHECK_FAIL(status < 0);
  83. //not interested in anything else
  84. status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
  85. CHECK_FAIL(status < 0);
  86. //todo: support 4 and 6
  87. status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
  88. CHECK_FAIL(status < 0);
  89. status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, nullptr);
  90. CHECK_FAIL(status < 0);
  91. // In ALSA the period size seems to be the one that will determine the actual latency
  92. // Ref: https://www.alsa-project.org/main/index.php/FramesPeriods
  93. unsigned int periods = 2;
  94. int latency = GLOBAL_GET("audio/driver/output_latency");
  95. buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
  96. buffer_size = buffer_frames * periods;
  97. period_size = buffer_frames;
  98. // set buffer size from project settings
  99. status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
  100. CHECK_FAIL(status < 0);
  101. status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, nullptr);
  102. CHECK_FAIL(status < 0);
  103. print_verbose("Audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms");
  104. status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, nullptr);
  105. CHECK_FAIL(status < 0);
  106. status = snd_pcm_hw_params(pcm_handle, hwparams);
  107. CHECK_FAIL(status < 0);
  108. //snd_pcm_hw_params_free(&hwparams);
  109. snd_pcm_sw_params_alloca(&swparams);
  110. status = snd_pcm_sw_params_current(pcm_handle, swparams);
  111. CHECK_FAIL(status < 0);
  112. status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
  113. CHECK_FAIL(status < 0);
  114. status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
  115. CHECK_FAIL(status < 0);
  116. status = snd_pcm_sw_params(pcm_handle, swparams);
  117. CHECK_FAIL(status < 0);
  118. samples_in.resize(period_size * channels);
  119. samples_out.resize(period_size * channels);
  120. return OK;
  121. }
  122. Error AudioDriverALSA::init() {
  123. #ifdef DEBUG_ENABLED
  124. int dylibloader_verbose = 1;
  125. #else
  126. int dylibloader_verbose = 0;
  127. #endif
  128. #ifdef PULSEAUDIO_ENABLED
  129. // On pulse enabled systems Alsa will silently use pulse.
  130. // It doesn't matter if this fails as that likely means there is no pulse
  131. initialize_pulse(dylibloader_verbose);
  132. #endif
  133. if (initialize_asound(dylibloader_verbose)) {
  134. return ERR_CANT_OPEN;
  135. }
  136. active.clear();
  137. exit_thread.clear();
  138. Error err = init_device();
  139. if (err == OK) {
  140. thread.start(AudioDriverALSA::thread_func, this);
  141. }
  142. return err;
  143. }
  144. void AudioDriverALSA::thread_func(void *p_udata) {
  145. AudioDriverALSA *ad = static_cast<AudioDriverALSA *>(p_udata);
  146. while (!ad->exit_thread.is_set()) {
  147. ad->lock();
  148. ad->start_counting_ticks();
  149. if (!ad->active.is_set()) {
  150. for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
  151. ad->samples_out.write[i] = 0;
  152. }
  153. } else {
  154. ad->audio_server_process(ad->period_size, ad->samples_in.ptrw());
  155. for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
  156. ad->samples_out.write[i] = ad->samples_in[i] >> 16;
  157. }
  158. }
  159. int todo = ad->period_size;
  160. int total = 0;
  161. while (todo && !ad->exit_thread.is_set()) {
  162. int16_t *src = (int16_t *)ad->samples_out.ptr();
  163. int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
  164. if (wrote > 0) {
  165. total += wrote;
  166. todo -= wrote;
  167. } else if (wrote == -EAGAIN) {
  168. ad->stop_counting_ticks();
  169. ad->unlock();
  170. OS::get_singleton()->delay_usec(1000);
  171. ad->lock();
  172. ad->start_counting_ticks();
  173. } else {
  174. wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
  175. if (wrote < 0) {
  176. ERR_PRINT("ALSA: Failed and can't recover: " + String(snd_strerror(wrote)));
  177. ad->active.clear();
  178. ad->exit_thread.set();
  179. }
  180. }
  181. }
  182. // User selected a new device, finish the current one so we'll init the new device
  183. if (ad->device_name != ad->new_device) {
  184. ad->device_name = ad->new_device;
  185. ad->finish_device();
  186. Error err = ad->init_device();
  187. if (err != OK) {
  188. ERR_PRINT("ALSA: init_device error");
  189. ad->device_name = "Default";
  190. ad->new_device = "Default";
  191. err = ad->init_device();
  192. if (err != OK) {
  193. ad->active.clear();
  194. ad->exit_thread.set();
  195. }
  196. }
  197. }
  198. ad->stop_counting_ticks();
  199. ad->unlock();
  200. }
  201. }
  202. void AudioDriverALSA::start() {
  203. active.set();
  204. }
  205. int AudioDriverALSA::get_mix_rate() const {
  206. return mix_rate;
  207. }
  208. AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const {
  209. return speaker_mode;
  210. }
  211. PackedStringArray AudioDriverALSA::get_device_list() {
  212. PackedStringArray list;
  213. list.push_back("Default");
  214. void **hints;
  215. if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
  216. return list;
  217. }
  218. for (void **n = hints; *n != nullptr; n++) {
  219. char *name = snd_device_name_get_hint(*n, "NAME");
  220. char *desc = snd_device_name_get_hint(*n, "DESC");
  221. if (name != nullptr && !strncmp(name, "plughw", 6)) {
  222. if (desc) {
  223. list.push_back(String::utf8(name) + ";" + String::utf8(desc));
  224. } else {
  225. list.push_back(String::utf8(name));
  226. }
  227. }
  228. if (desc != nullptr) {
  229. free(desc);
  230. }
  231. if (name != nullptr) {
  232. free(name);
  233. }
  234. }
  235. snd_device_name_free_hint(hints);
  236. return list;
  237. }
  238. String AudioDriverALSA::get_device() {
  239. return device_name;
  240. }
  241. void AudioDriverALSA::set_device(String device) {
  242. lock();
  243. new_device = device;
  244. unlock();
  245. }
  246. void AudioDriverALSA::lock() {
  247. mutex.lock();
  248. }
  249. void AudioDriverALSA::unlock() {
  250. mutex.unlock();
  251. }
  252. void AudioDriverALSA::finish_device() {
  253. if (pcm_handle) {
  254. snd_pcm_close(pcm_handle);
  255. pcm_handle = nullptr;
  256. }
  257. }
  258. void AudioDriverALSA::finish() {
  259. exit_thread.set();
  260. thread.wait_to_finish();
  261. finish_device();
  262. }
  263. #endif // ALSA_ENABLED