PulseAudioStream.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <cstring>
  4. #include "AudioCommon/PulseAudioStream.h"
  5. #include "Common/Assert.h"
  6. #include "Common/CommonTypes.h"
  7. #include "Common/Logging/Log.h"
  8. #include "Common/Thread.h"
  9. #include "Core/Config/MainSettings.h"
  10. namespace
  11. {
  12. const size_t BUFFER_SAMPLES = 512; // ~10 ms - needs to be at least 240 for surround
  13. }
  14. PulseAudio::PulseAudio() = default;
  15. bool PulseAudio::Init()
  16. {
  17. m_stereo = !Config::ShouldUseDPL2Decoder();
  18. m_channels = m_stereo ? 2 : 6; // will tell PA we use a Stereo or 5.0 channel setup
  19. NOTICE_LOG_FMT(AUDIO, "PulseAudio backend using {} channels", m_channels);
  20. m_run_thread.Set();
  21. m_thread = std::thread(&PulseAudio::SoundLoop, this);
  22. return true;
  23. }
  24. PulseAudio::~PulseAudio()
  25. {
  26. m_run_thread.Clear();
  27. m_thread.join();
  28. }
  29. // Called on audio thread.
  30. void PulseAudio::SoundLoop()
  31. {
  32. Common::SetCurrentThreadName("Audio thread - pulse");
  33. if (PulseInit())
  34. {
  35. while (m_run_thread.IsSet() && m_pa_connected == 1 && m_pa_error >= 0)
  36. m_pa_error = pa_mainloop_iterate(m_pa_ml, 1, nullptr);
  37. if (m_pa_error < 0)
  38. ERROR_LOG_FMT(AUDIO, "PulseAudio error: {}", pa_strerror(m_pa_error));
  39. PulseShutdown();
  40. }
  41. }
  42. bool PulseAudio::PulseInit()
  43. {
  44. m_pa_error = 0;
  45. m_pa_connected = 0;
  46. // create pulseaudio main loop and context
  47. // also register the async state callback which is called when the connection to the pa server has
  48. // changed
  49. m_pa_ml = pa_mainloop_new();
  50. m_pa_mlapi = pa_mainloop_get_api(m_pa_ml);
  51. m_pa_ctx = pa_context_new(m_pa_mlapi, "dolphin-emu");
  52. m_pa_error = pa_context_connect(m_pa_ctx, nullptr, PA_CONTEXT_NOFLAGS, nullptr);
  53. pa_context_set_state_callback(m_pa_ctx, StateCallback, this);
  54. // wait until we're connected to the pulseaudio server
  55. while (m_pa_connected == 0 && m_pa_error >= 0)
  56. m_pa_error = pa_mainloop_iterate(m_pa_ml, 1, nullptr);
  57. if (m_pa_connected == 2 || m_pa_error < 0)
  58. {
  59. ERROR_LOG_FMT(AUDIO, "PulseAudio failed to initialize: {}", pa_strerror(m_pa_error));
  60. return false;
  61. }
  62. // create a new audio stream with our sample format
  63. // also connect the callbacks for this stream
  64. pa_sample_spec ss;
  65. pa_channel_map channel_map;
  66. pa_channel_map* channel_map_p = nullptr; // auto channel map
  67. if (m_stereo)
  68. {
  69. ss.format = PA_SAMPLE_S16LE;
  70. m_bytespersample = sizeof(s16);
  71. }
  72. else
  73. {
  74. // surround is remixed in floats, use a float PA buffer to save another conversion
  75. ss.format = PA_SAMPLE_FLOAT32NE;
  76. m_bytespersample = sizeof(float);
  77. channel_map_p = &channel_map; // explicit channel map:
  78. channel_map.channels = 6;
  79. channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
  80. channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
  81. channel_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
  82. channel_map.map[3] = PA_CHANNEL_POSITION_LFE;
  83. channel_map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
  84. channel_map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
  85. }
  86. ss.channels = m_channels;
  87. ss.rate = m_mixer->GetSampleRate();
  88. ASSERT(pa_sample_spec_valid(&ss));
  89. m_pa_s = pa_stream_new(m_pa_ctx, "Playback", &ss, channel_map_p);
  90. pa_stream_set_write_callback(m_pa_s, WriteCallback, this);
  91. pa_stream_set_underflow_callback(m_pa_s, UnderflowCallback, this);
  92. // connect this audio stream to the default audio playback
  93. // limit buffersize to reduce latency
  94. m_pa_ba.fragsize = -1;
  95. m_pa_ba.maxlength = -1; // max buffer, so also max latency
  96. m_pa_ba.minreq = -1; // don't read every byte, try to group them _a bit_
  97. m_pa_ba.prebuf = -1; // start as early as possible
  98. m_pa_ba.tlength =
  99. BUFFER_SAMPLES * m_channels *
  100. m_bytespersample; // designed latency, only change this flag for low latency output
  101. pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY |
  102. PA_STREAM_AUTO_TIMING_UPDATE);
  103. m_pa_error = pa_stream_connect_playback(m_pa_s, nullptr, &m_pa_ba, flags, nullptr, nullptr);
  104. if (m_pa_error < 0)
  105. {
  106. ERROR_LOG_FMT(AUDIO, "PulseAudio failed to initialize: {}", pa_strerror(m_pa_error));
  107. return false;
  108. }
  109. INFO_LOG_FMT(AUDIO, "Pulse successfully initialized");
  110. return true;
  111. }
  112. void PulseAudio::PulseShutdown()
  113. {
  114. pa_context_disconnect(m_pa_ctx);
  115. pa_context_unref(m_pa_ctx);
  116. pa_mainloop_free(m_pa_ml);
  117. }
  118. void PulseAudio::StateCallback(pa_context* c)
  119. {
  120. pa_context_state_t state = pa_context_get_state(c);
  121. switch (state)
  122. {
  123. case PA_CONTEXT_FAILED:
  124. case PA_CONTEXT_TERMINATED:
  125. m_pa_connected = 2;
  126. break;
  127. case PA_CONTEXT_READY:
  128. m_pa_connected = 1;
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. // on underflow, increase pulseaudio latency in ~10ms steps
  135. void PulseAudio::UnderflowCallback(pa_stream* s)
  136. {
  137. m_pa_ba.tlength += BUFFER_SAMPLES * m_channels * m_bytespersample;
  138. pa_operation* op = pa_stream_set_buffer_attr(s, &m_pa_ba, nullptr, nullptr);
  139. pa_operation_unref(op);
  140. WARN_LOG_FMT(AUDIO, "pulseaudio underflow, new latency: {} bytes", m_pa_ba.tlength);
  141. }
  142. void PulseAudio::WriteCallback(pa_stream* s, size_t length)
  143. {
  144. int bytes_per_frame = m_channels * m_bytespersample;
  145. int frames = (length / bytes_per_frame);
  146. size_t trunc_length = frames * bytes_per_frame;
  147. // fetch dst buffer directly from pulseaudio, so no memcpy is needed
  148. void* buffer;
  149. m_pa_error = pa_stream_begin_write(s, &buffer, &trunc_length);
  150. if (!buffer || m_pa_error < 0)
  151. return; // error will be printed from main loop
  152. if (m_stereo)
  153. {
  154. // use the raw s16 stereo mix
  155. m_mixer->Mix((s16*)buffer, frames);
  156. }
  157. else
  158. {
  159. if (m_channels == 6) // Extract dpl2/5.1 Surround
  160. {
  161. m_mixer->MixSurround((float*)buffer, frames);
  162. }
  163. else
  164. {
  165. ERROR_LOG_FMT(AUDIO, "Unsupported number of PA channels requested: {}", m_channels);
  166. return;
  167. }
  168. }
  169. m_pa_error = pa_stream_write(s, buffer, trunc_length, nullptr, 0, PA_SEEK_RELATIVE);
  170. }
  171. // Callbacks that forward to internal methods (required because PulseAudio is a C API).
  172. void PulseAudio::StateCallback(pa_context* c, void* userdata)
  173. {
  174. PulseAudio* p = (PulseAudio*)userdata;
  175. p->StateCallback(c);
  176. }
  177. void PulseAudio::UnderflowCallback(pa_stream* s, void* userdata)
  178. {
  179. PulseAudio* p = (PulseAudio*)userdata;
  180. p->UnderflowCallback(s);
  181. }
  182. void PulseAudio::WriteCallback(pa_stream* s, size_t length, void* userdata)
  183. {
  184. PulseAudio* p = (PulseAudio*)userdata;
  185. p->WriteCallback(s, length);
  186. }