audio_driver_alsa.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #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. #if defined(PULSEAUDIO_ENABLED) && defined(SOWRAP_ENABLED)
  36. extern "C" {
  37. extern int initialize_pulse(int verbose);
  38. }
  39. #endif
  40. Error AudioDriverALSA::init_output_device() {
  41. mix_rate = _get_configured_mix_rate();
  42. speaker_mode = SPEAKER_MODE_STEREO;
  43. channels = 2;
  44. // If there is a specified output device check that it is really present
  45. if (output_device_name != "Default") {
  46. PackedStringArray list = get_output_device_list();
  47. if (list.find(output_device_name) == -1) {
  48. output_device_name = "Default";
  49. new_output_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 (output_device_name == "Default") {
  68. status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
  69. } else {
  70. String device = output_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 SOWRAP_ENABLED
  124. #ifdef DEBUG_ENABLED
  125. int dylibloader_verbose = 1;
  126. #else
  127. int dylibloader_verbose = 0;
  128. #endif
  129. #ifdef PULSEAUDIO_ENABLED
  130. // On pulse enabled systems Alsa will silently use pulse.
  131. // It doesn't matter if this fails as that likely means there is no pulse
  132. initialize_pulse(dylibloader_verbose);
  133. #endif
  134. if (initialize_asound(dylibloader_verbose)) {
  135. return ERR_CANT_OPEN;
  136. }
  137. #endif
  138. active.clear();
  139. exit_thread.clear();
  140. Error err = init_output_device();
  141. if (err == OK) {
  142. thread.start(AudioDriverALSA::thread_func, this);
  143. }
  144. return err;
  145. }
  146. void AudioDriverALSA::thread_func(void *p_udata) {
  147. AudioDriverALSA *ad = static_cast<AudioDriverALSA *>(p_udata);
  148. while (!ad->exit_thread.is_set()) {
  149. ad->lock();
  150. ad->start_counting_ticks();
  151. if (!ad->active.is_set()) {
  152. for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
  153. ad->samples_out.write[i] = 0;
  154. }
  155. } else {
  156. ad->audio_server_process(ad->period_size, ad->samples_in.ptrw());
  157. for (uint64_t i = 0; i < ad->period_size * ad->channels; i++) {
  158. ad->samples_out.write[i] = ad->samples_in[i] >> 16;
  159. }
  160. }
  161. int todo = ad->period_size;
  162. int total = 0;
  163. while (todo && !ad->exit_thread.is_set()) {
  164. int16_t *src = (int16_t *)ad->samples_out.ptr();
  165. int wrote = snd_pcm_writei(ad->pcm_handle, (void *)(src + (total * ad->channels)), todo);
  166. if (wrote > 0) {
  167. total += wrote;
  168. todo -= wrote;
  169. } else if (wrote == -EAGAIN) {
  170. ad->stop_counting_ticks();
  171. ad->unlock();
  172. OS::get_singleton()->delay_usec(1000);
  173. ad->lock();
  174. ad->start_counting_ticks();
  175. } else {
  176. wrote = snd_pcm_recover(ad->pcm_handle, wrote, 0);
  177. if (wrote < 0) {
  178. ERR_PRINT("ALSA: Failed and can't recover: " + String(snd_strerror(wrote)));
  179. ad->active.clear();
  180. ad->exit_thread.set();
  181. }
  182. }
  183. }
  184. // User selected a new output device, finish the current one so we'll init the new device.
  185. if (ad->output_device_name != ad->new_output_device) {
  186. ad->output_device_name = ad->new_output_device;
  187. ad->finish_output_device();
  188. Error err = ad->init_output_device();
  189. if (err != OK) {
  190. ERR_PRINT("ALSA: init_output_device error");
  191. ad->output_device_name = "Default";
  192. ad->new_output_device = "Default";
  193. err = ad->init_output_device();
  194. if (err != OK) {
  195. ad->active.clear();
  196. ad->exit_thread.set();
  197. }
  198. }
  199. }
  200. ad->stop_counting_ticks();
  201. ad->unlock();
  202. }
  203. }
  204. void AudioDriverALSA::start() {
  205. active.set();
  206. }
  207. int AudioDriverALSA::get_mix_rate() const {
  208. return mix_rate;
  209. }
  210. AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const {
  211. return speaker_mode;
  212. }
  213. PackedStringArray AudioDriverALSA::get_output_device_list() {
  214. PackedStringArray list;
  215. list.push_back("Default");
  216. void **hints;
  217. if (snd_device_name_hint(-1, "pcm", &hints) < 0) {
  218. return list;
  219. }
  220. for (void **n = hints; *n != nullptr; n++) {
  221. char *name = snd_device_name_get_hint(*n, "NAME");
  222. char *desc = snd_device_name_get_hint(*n, "DESC");
  223. if (name != nullptr && !strncmp(name, "plughw", 6)) {
  224. if (desc) {
  225. list.push_back(String::utf8(name) + ";" + String::utf8(desc));
  226. } else {
  227. list.push_back(String::utf8(name));
  228. }
  229. }
  230. if (desc != nullptr) {
  231. free(desc);
  232. }
  233. if (name != nullptr) {
  234. free(name);
  235. }
  236. }
  237. snd_device_name_free_hint(hints);
  238. return list;
  239. }
  240. String AudioDriverALSA::get_output_device() {
  241. return output_device_name;
  242. }
  243. void AudioDriverALSA::set_output_device(const String &p_name) {
  244. lock();
  245. new_output_device = p_name;
  246. unlock();
  247. }
  248. void AudioDriverALSA::lock() {
  249. mutex.lock();
  250. }
  251. void AudioDriverALSA::unlock() {
  252. mutex.unlock();
  253. }
  254. void AudioDriverALSA::finish_output_device() {
  255. if (pcm_handle) {
  256. snd_pcm_close(pcm_handle);
  257. pcm_handle = nullptr;
  258. }
  259. }
  260. void AudioDriverALSA::finish() {
  261. exit_thread.set();
  262. thread.wait_to_finish();
  263. finish_output_device();
  264. }
  265. #endif // ALSA_ENABLED