audio_driver_alsa.cpp 9.4 KB

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