audio_driver_pulseaudio.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*************************************************************************/
  2. /* audio_driver_pulseaudio.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_pulseaudio.h"
  31. #ifdef PULSEAUDIO_ENABLED
  32. #include <pulse/pulseaudio.h>
  33. #include "os/os.h"
  34. #include "project_settings.h"
  35. void pa_state_cb(pa_context *c, void *userdata) {
  36. pa_context_state_t state;
  37. int *pa_ready = (int *)userdata;
  38. state = pa_context_get_state(c);
  39. switch (state) {
  40. case PA_CONTEXT_FAILED:
  41. case PA_CONTEXT_TERMINATED:
  42. *pa_ready = 2;
  43. break;
  44. case PA_CONTEXT_READY:
  45. *pa_ready = 1;
  46. break;
  47. }
  48. }
  49. void sink_info_cb(pa_context *c, const pa_sink_info *l, int eol, void *userdata) {
  50. unsigned int *channels = (unsigned int *)userdata;
  51. // If eol is set to a positive number, you're at the end of the list
  52. if (eol > 0) {
  53. return;
  54. }
  55. *channels = l->channel_map.channels;
  56. }
  57. void server_info_cb(pa_context *c, const pa_server_info *i, void *userdata) {
  58. char *default_output = (char *)userdata;
  59. strncpy(default_output, i->default_sink_name, 1024);
  60. }
  61. static unsigned int detect_channels() {
  62. pa_mainloop *pa_ml;
  63. pa_mainloop_api *pa_mlapi;
  64. pa_operation *pa_op;
  65. pa_context *pa_ctx;
  66. int state = 0;
  67. int pa_ready = 0;
  68. char default_output[1024];
  69. unsigned int channels = 2;
  70. pa_ml = pa_mainloop_new();
  71. pa_mlapi = pa_mainloop_get_api(pa_ml);
  72. pa_ctx = pa_context_new(pa_mlapi, "Godot");
  73. int ret = pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
  74. if (ret < 0) {
  75. pa_context_unref(pa_ctx);
  76. pa_mainloop_free(pa_ml);
  77. return 2;
  78. }
  79. pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
  80. // Wait until the pa server is ready
  81. while (pa_ready == 0) {
  82. pa_mainloop_iterate(pa_ml, 1, NULL);
  83. }
  84. // Check if there was an error connecting to the pa server
  85. if (pa_ready == 2) {
  86. pa_context_disconnect(pa_ctx);
  87. pa_context_unref(pa_ctx);
  88. pa_mainloop_free(pa_ml);
  89. return 2;
  90. }
  91. // Get the default output device name
  92. pa_op = pa_context_get_server_info(pa_ctx, &server_info_cb, (void *)default_output);
  93. if (pa_op) {
  94. while (pa_operation_get_state(pa_op) == PA_OPERATION_RUNNING) {
  95. ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  96. if (ret < 0) {
  97. ERR_PRINT("pa_mainloop_iterate error");
  98. }
  99. }
  100. pa_operation_unref(pa_op);
  101. // Now using the device name get the amount of channels
  102. pa_op = pa_context_get_sink_info_by_name(pa_ctx, default_output, &sink_info_cb, (void *)&channels);
  103. if (pa_op) {
  104. while (pa_operation_get_state(pa_op) == PA_OPERATION_RUNNING) {
  105. ret = pa_mainloop_iterate(pa_ml, 1, NULL);
  106. if (ret < 0) {
  107. ERR_PRINT("pa_mainloop_iterate error");
  108. }
  109. }
  110. pa_operation_unref(pa_op);
  111. } else {
  112. ERR_PRINT("pa_context_get_sink_info_by_name error");
  113. }
  114. } else {
  115. ERR_PRINT("pa_context_get_server_info error");
  116. }
  117. pa_context_disconnect(pa_ctx);
  118. pa_context_unref(pa_ctx);
  119. pa_mainloop_free(pa_ml);
  120. return channels;
  121. }
  122. Error AudioDriverPulseAudio::init() {
  123. active = false;
  124. thread_exited = false;
  125. exit_thread = false;
  126. mix_rate = GLOBAL_DEF("audio/mix_rate", DEFAULT_MIX_RATE);
  127. channels = detect_channels();
  128. switch (channels) {
  129. case 2: // Stereo
  130. case 4: // Surround 3.1
  131. case 6: // Surround 5.1
  132. case 8: // Surround 7.1
  133. break;
  134. default:
  135. ERR_PRINTS("PulseAudio: Unsupported number of channels: " + itos(channels));
  136. ERR_FAIL_V(ERR_CANT_OPEN);
  137. break;
  138. }
  139. pa_sample_spec spec;
  140. spec.format = PA_SAMPLE_S16LE;
  141. spec.channels = channels;
  142. spec.rate = mix_rate;
  143. int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY);
  144. buffer_frames = closest_power_of_2(latency * mix_rate / 1000);
  145. buffer_size = buffer_frames * channels;
  146. if (OS::get_singleton()->is_stdout_verbose()) {
  147. print_line("PulseAudio: detected " + itos(channels) + " channels");
  148. print_line("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  149. }
  150. pa_buffer_attr attr;
  151. // set to appropriate buffer length (in bytes) from global settings
  152. attr.tlength = buffer_size * sizeof(int16_t);
  153. // set them to be automatically chosen
  154. attr.prebuf = (uint32_t)-1;
  155. attr.maxlength = (uint32_t)-1;
  156. attr.minreq = (uint32_t)-1;
  157. int error_code;
  158. pulse = pa_simple_new(NULL, // default server
  159. "Godot", // application name
  160. PA_STREAM_PLAYBACK,
  161. NULL, // default device
  162. "Sound", // stream description
  163. &spec,
  164. NULL, // use default channel map
  165. &attr, // use buffering attributes from above
  166. &error_code);
  167. if (pulse == NULL) {
  168. fprintf(stderr, "PulseAudio ERR: %s\n", pa_strerror(error_code));
  169. ERR_FAIL_COND_V(pulse == NULL, ERR_CANT_OPEN);
  170. }
  171. samples_in.resize(buffer_size);
  172. samples_out.resize(buffer_size);
  173. mutex = Mutex::create();
  174. thread = Thread::create(AudioDriverPulseAudio::thread_func, this);
  175. return OK;
  176. }
  177. float AudioDriverPulseAudio::get_latency() {
  178. if (latency == 0) { //only do this once since it's approximate anyway
  179. int error_code;
  180. pa_usec_t palat = pa_simple_get_latency(pulse, &error_code);
  181. latency = double(palat) / 1000000.0;
  182. }
  183. return latency;
  184. }
  185. void AudioDriverPulseAudio::thread_func(void *p_udata) {
  186. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;
  187. while (!ad->exit_thread) {
  188. if (!ad->active) {
  189. for (unsigned int i = 0; i < ad->buffer_size; i++) {
  190. ad->samples_out[i] = 0;
  191. }
  192. } else {
  193. ad->lock();
  194. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptr());
  195. ad->unlock();
  196. for (unsigned int i = 0; i < ad->buffer_size; i++) {
  197. ad->samples_out[i] = ad->samples_in[i] >> 16;
  198. }
  199. }
  200. // pa_simple_write always consumes the entire buffer
  201. int error_code;
  202. int byte_size = ad->buffer_size * sizeof(int16_t);
  203. if (pa_simple_write(ad->pulse, ad->samples_out.ptr(), byte_size, &error_code) < 0) {
  204. // can't recover here
  205. fprintf(stderr, "PulseAudio failed and can't recover: %s\n", pa_strerror(error_code));
  206. ad->active = false;
  207. ad->exit_thread = true;
  208. break;
  209. }
  210. }
  211. ad->thread_exited = true;
  212. }
  213. void AudioDriverPulseAudio::start() {
  214. active = true;
  215. }
  216. int AudioDriverPulseAudio::get_mix_rate() const {
  217. return mix_rate;
  218. }
  219. AudioDriver::SpeakerMode AudioDriverPulseAudio::get_speaker_mode() const {
  220. return get_speaker_mode_by_total_channels(channels);
  221. }
  222. void AudioDriverPulseAudio::lock() {
  223. if (!thread || !mutex)
  224. return;
  225. mutex->lock();
  226. }
  227. void AudioDriverPulseAudio::unlock() {
  228. if (!thread || !mutex)
  229. return;
  230. mutex->unlock();
  231. }
  232. void AudioDriverPulseAudio::finish() {
  233. if (!thread)
  234. return;
  235. exit_thread = true;
  236. Thread::wait_to_finish(thread);
  237. if (pulse) {
  238. pa_simple_free(pulse);
  239. pulse = NULL;
  240. }
  241. memdelete(thread);
  242. if (mutex) {
  243. memdelete(mutex);
  244. mutex = NULL;
  245. }
  246. thread = NULL;
  247. }
  248. AudioDriverPulseAudio::AudioDriverPulseAudio() {
  249. mutex = NULL;
  250. thread = NULL;
  251. pulse = NULL;
  252. samples_in.clear();
  253. samples_out.clear();
  254. mix_rate = 0;
  255. buffer_size = 0;
  256. channels = 0;
  257. active = false;
  258. thread_exited = false;
  259. exit_thread = false;
  260. latency = 0;
  261. buffer_frames = 0;
  262. buffer_size = 0;
  263. channels = 0;
  264. }
  265. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  266. }
  267. #endif