audio_driver_pulseaudio.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*************************************************************************/
  2. /* audio_driver_alsa.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_driver_pulseaudio.h"
  30. #ifdef PULSEAUDIO_ENABLED
  31. #include <pulse/error.h>
  32. #include "globals.h"
  33. Error AudioDriverPulseAudio::init() {
  34. active = false;
  35. thread_exited = false;
  36. exit_thread = false;
  37. pcm_open = false;
  38. samples_in = NULL;
  39. samples_out = NULL;
  40. mix_rate = 44100;
  41. output_format = OUTPUT_STEREO;
  42. channels = 2;
  43. pa_sample_spec spec;
  44. spec.format = PA_SAMPLE_S16LE;
  45. spec.channels = channels;
  46. spec.rate = mix_rate;
  47. int error_code;
  48. pulse = pa_simple_new(NULL, // default server
  49. "Godot", // application name
  50. PA_STREAM_PLAYBACK,
  51. NULL, // default device
  52. "Sound", // stream description
  53. &spec,
  54. NULL, // use default channel map
  55. NULL, // use default buffering attributes
  56. &error_code
  57. );
  58. if (pulse == NULL) {
  59. fprintf(stderr, "PulseAudio ERR: %s\n", pa_strerror(error_code));\
  60. ERR_FAIL_COND_V(pulse == NULL, ERR_CANT_OPEN);
  61. }
  62. int latency = GLOBAL_DEF("audio/output_latency", 25);
  63. buffer_size = nearest_power_of_2(latency * mix_rate / 1000);
  64. samples_in = memnew_arr(int32_t, buffer_size * channels);
  65. samples_out = memnew_arr(int16_t, buffer_size * channels);
  66. mutex = Mutex::create();
  67. thread = Thread::create(AudioDriverPulseAudio::thread_func, this);
  68. return OK;
  69. }
  70. void AudioDriverPulseAudio::thread_func(void* p_udata) {
  71. AudioDriverPulseAudio* ad = (AudioDriverPulseAudio*)p_udata;
  72. while (!ad->exit_thread) {
  73. if (!ad->active) {
  74. for (unsigned int i=0; i < ad->buffer_size * ad->channels; i++) {
  75. ad->samples_out[i] = 0;
  76. }
  77. } else {
  78. ad->lock();
  79. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  80. ad->unlock();
  81. for (unsigned int i=0; i < ad->buffer_size * ad->channels;i ++) {
  82. ad->samples_out[i] = ad->samples_in[i] >> 16;
  83. }
  84. }
  85. // pa_simple_write always consumes the entire buffer
  86. int error_code;
  87. int byte_size = ad->buffer_size * sizeof(int16_t) * ad->channels;
  88. if (pa_simple_write(ad->pulse, ad->samples_out, byte_size, &error_code) < 0) {
  89. // can't recover here
  90. fprintf(stderr, "PulseAudio failed and can't recover: %s\n", pa_strerror(error_code));
  91. ad->active = false;
  92. ad->exit_thread = true;
  93. break;
  94. }
  95. }
  96. ad->thread_exited = true;
  97. }
  98. void AudioDriverPulseAudio::start() {
  99. active = true;
  100. }
  101. int AudioDriverPulseAudio::get_mix_rate() const {
  102. return mix_rate;
  103. }
  104. AudioDriverSW::OutputFormat AudioDriverPulseAudio::get_output_format() const {
  105. return output_format;
  106. }
  107. void AudioDriverPulseAudio::lock() {
  108. if (!thread || !mutex)
  109. return;
  110. mutex->lock();
  111. }
  112. void AudioDriverPulseAudio::unlock() {
  113. if (!thread || !mutex)
  114. return;
  115. mutex->unlock();
  116. }
  117. void AudioDriverPulseAudio::finish() {
  118. if (!thread)
  119. return;
  120. exit_thread = true;
  121. Thread::wait_to_finish(thread);
  122. if (pulse)
  123. pa_simple_free(pulse);
  124. if (samples_in) {
  125. memdelete_arr(samples_in);
  126. memdelete_arr(samples_out);
  127. };
  128. memdelete(thread);
  129. if (mutex) {
  130. memdelete(mutex);
  131. mutex = NULL;
  132. }
  133. thread = NULL;
  134. }
  135. AudioDriverPulseAudio::AudioDriverPulseAudio() {
  136. mutex = NULL;
  137. thread = NULL;
  138. pulse = NULL;
  139. }
  140. AudioDriverPulseAudio::~AudioDriverPulseAudio() {
  141. }
  142. #endif