audio_driver_pulseaudio.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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-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_pulseaudio.h"
  31. #ifdef PULSEAUDIO_ENABLED
  32. #include <pulse/error.h>
  33. #include "globals.h"
  34. Error AudioDriverPulseAudio::init() {
  35. active = false;
  36. thread_exited = false;
  37. exit_thread = false;
  38. pcm_open = false;
  39. samples_in = NULL;
  40. samples_out = NULL;
  41. mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
  42. output_format = OUTPUT_STEREO;
  43. channels = 2;
  44. pa_sample_spec spec;
  45. spec.format = PA_SAMPLE_S16LE;
  46. spec.channels = channels;
  47. spec.rate = mix_rate;
  48. int latency = GLOBAL_DEF("audio/output_latency", 25);
  49. buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  50. pa_buffer_attr attr;
  51. // set to appropriate buffer size from global settings
  52. attr.tlength = buffer_size;
  53. // set them to be automatically chosen
  54. attr.prebuf = (uint32_t)-1;
  55. attr.maxlength = (uint32_t)-1;
  56. attr.minreq = (uint32_t)-1;
  57. int error_code;
  58. pulse = pa_simple_new(NULL, // default server
  59. "Godot", // application name
  60. PA_STREAM_PLAYBACK,
  61. NULL, // default device
  62. "Sound", // stream description
  63. &spec,
  64. NULL, // use default channel map
  65. &attr, // use buffering attributes from above
  66. &error_code);
  67. if (pulse == NULL) {
  68. fprintf(stderr, "PulseAudio ERR: %s\n", pa_strerror(error_code));
  69. ERR_FAIL_COND_V(pulse == NULL, ERR_CANT_OPEN);
  70. }
  71. samples_in = memnew_arr(int32_t, buffer_size * channels);
  72. samples_out = memnew_arr(int16_t, buffer_size * channels);
  73. mutex = Mutex::create();
  74. thread = Thread::create(AudioDriverPulseAudio::thread_func, this);
  75. return OK;
  76. }
  77. float AudioDriverPulseAudio::get_latency() {
  78. if (latency == 0) { //only do this once since it's approximate anyway
  79. int error_code;
  80. pa_usec_t palat = pa_simple_get_latency(pulse, &error_code);
  81. latency = double(palat) / 1000000.0;
  82. }
  83. return latency;
  84. }
  85. void AudioDriverPulseAudio::thread_func(void *p_udata) {
  86. AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)p_udata;
  87. while (!ad->exit_thread) {
  88. if (!ad->active) {
  89. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  90. ad->samples_out[i] = 0;
  91. }
  92. } else {
  93. ad->lock();
  94. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  95. ad->unlock();
  96. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  97. ad->samples_out[i] = ad->samples_in[i] >> 16;
  98. }
  99. }
  100. // pa_simple_write always consumes the entire buffer
  101. int error_code;
  102. int byte_size = ad->buffer_size * sizeof(int16_t) * ad->channels;
  103. if (pa_simple_write(ad->pulse, ad->samples_out, byte_size, &error_code) < 0) {
  104. // can't recover here
  105. fprintf(stderr, "PulseAudio failed and can't recover: %s\n", pa_strerror(error_code));
  106. ad->active = false;
  107. ad->exit_thread = true;
  108. break;
  109. }
  110. }
  111. ad->thread_exited = true;
  112. }
  113. void AudioDriverPulseAudio::start() {
  114. active = true;
  115. }
  116. int AudioDriverPulseAudio::get_mix_rate() const {
  117. return mix_rate;
  118. }
  119. AudioDriverSW::OutputFormat AudioDriverPulseAudio::get_output_format() const {
  120. return output_format;
  121. }
  122. void AudioDriverPulseAudio::lock() {
  123. if (!thread || !mutex)
  124. return;
  125. mutex->lock();
  126. }
  127. void AudioDriverPulseAudio::unlock() {
  128. if (!thread || !mutex)
  129. return;
  130. mutex->unlock();
  131. }
  132. void AudioDriverPulseAudio::finish() {
  133. if (!thread)
  134. return;
  135. exit_thread = true;
  136. Thread::wait_to_finish(thread);
  137. if (pulse)
  138. pa_simple_free(pulse);
  139. if (samples_in) {
  140. memdelete_arr(samples_in);
  141. memdelete_arr(samples_out);
  142. };
  143. memdelete(thread);
  144. if (mutex) {
  145. memdelete(mutex);
  146. mutex = NULL;
  147. }
  148. thread = NULL;
  149. }
  150. AudioDriverPulseAudio::AudioDriverPulseAudio() {
  151. mutex = NULL;
  152. thread = NULL;
  153. pulse = NULL;
  154. latency = 0;
  155. }
  156. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  157. }
  158. #endif