AlsaSoundStream.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "AudioCommon/AlsaSoundStream.h"
  4. #include <mutex>
  5. #include "Common/CommonTypes.h"
  6. #include "Common/Logging/Log.h"
  7. #include "Common/Thread.h"
  8. AlsaSound::AlsaSound()
  9. : m_thread_status(ALSAThreadStatus::STOPPED), handle(nullptr),
  10. frames_to_deliver(FRAME_COUNT_MIN)
  11. {
  12. }
  13. AlsaSound::~AlsaSound()
  14. {
  15. m_thread_status.store(ALSAThreadStatus::STOPPING);
  16. // Immediately lock and unlock mutex to prevent cv race.
  17. std::unique_lock<std::mutex>{cv_m}.unlock();
  18. // Give the opportunity to the audio thread
  19. // to realize we are stopping the emulation
  20. cv.notify_one();
  21. if (thread.joinable())
  22. thread.join();
  23. }
  24. bool AlsaSound::Init()
  25. {
  26. m_thread_status.store(ALSAThreadStatus::PAUSED);
  27. if (!AlsaInit())
  28. {
  29. m_thread_status.store(ALSAThreadStatus::STOPPED);
  30. return false;
  31. }
  32. thread = std::thread(&AlsaSound::SoundLoop, this);
  33. return true;
  34. }
  35. // Called on audio thread.
  36. void AlsaSound::SoundLoop()
  37. {
  38. Common::SetCurrentThreadName("Audio thread - alsa");
  39. while (m_thread_status.load() != ALSAThreadStatus::STOPPING)
  40. {
  41. while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
  42. {
  43. m_mixer->Mix(mix_buffer, frames_to_deliver);
  44. int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
  45. if (rc == -EPIPE)
  46. {
  47. // Underrun
  48. snd_pcm_prepare(handle);
  49. }
  50. else if (rc < 0)
  51. {
  52. ERROR_LOG_FMT(AUDIO, "writei fail: {}", snd_strerror(rc));
  53. }
  54. }
  55. if (m_thread_status.load() == ALSAThreadStatus::PAUSED)
  56. {
  57. snd_pcm_drop(handle); // Stop sound output
  58. // Block until thread status changes.
  59. std::unique_lock<std::mutex> lock(cv_m);
  60. cv.wait(lock, [this] { return m_thread_status.load() != ALSAThreadStatus::PAUSED; });
  61. snd_pcm_prepare(handle); // resume sound output
  62. }
  63. }
  64. AlsaShutdown();
  65. m_thread_status.store(ALSAThreadStatus::STOPPED);
  66. }
  67. bool AlsaSound::SetRunning(bool running)
  68. {
  69. m_thread_status.store(running ? ALSAThreadStatus::RUNNING : ALSAThreadStatus::PAUSED);
  70. // Immediately lock and unlock mutex to prevent cv race.
  71. std::unique_lock<std::mutex>{cv_m}.unlock();
  72. // Notify thread that status has changed
  73. cv.notify_one();
  74. return true;
  75. }
  76. bool AlsaSound::AlsaInit()
  77. {
  78. unsigned int sample_rate = m_mixer->GetSampleRate();
  79. int err;
  80. int dir;
  81. snd_pcm_sw_params_t* swparams;
  82. snd_pcm_hw_params_t* hwparams;
  83. snd_pcm_uframes_t buffer_size, buffer_size_max;
  84. unsigned int periods;
  85. err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
  86. if (err < 0)
  87. {
  88. ERROR_LOG_FMT(AUDIO, "Audio open error: {}", snd_strerror(err));
  89. return false;
  90. }
  91. snd_pcm_hw_params_alloca(&hwparams);
  92. err = snd_pcm_hw_params_any(handle, hwparams);
  93. if (err < 0)
  94. {
  95. ERROR_LOG_FMT(AUDIO, "Broken configuration for this PCM: {}", snd_strerror(err));
  96. return false;
  97. }
  98. err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
  99. if (err < 0)
  100. {
  101. ERROR_LOG_FMT(AUDIO, "Access type not available: {}", snd_strerror(err));
  102. return false;
  103. }
  104. err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
  105. if (err < 0)
  106. {
  107. ERROR_LOG_FMT(AUDIO, "Sample format not available: {}", snd_strerror(err));
  108. return false;
  109. }
  110. dir = 0;
  111. err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &sample_rate, &dir);
  112. if (err < 0)
  113. {
  114. ERROR_LOG_FMT(AUDIO, "Rate not available: {}", snd_strerror(err));
  115. return false;
  116. }
  117. err = snd_pcm_hw_params_set_channels(handle, hwparams, CHANNEL_COUNT);
  118. if (err < 0)
  119. {
  120. ERROR_LOG_FMT(AUDIO, "Channels count not available: {}", snd_strerror(err));
  121. return false;
  122. }
  123. periods = BUFFER_SIZE_MAX / FRAME_COUNT_MIN;
  124. err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &periods, &dir);
  125. if (err < 0)
  126. {
  127. ERROR_LOG_FMT(AUDIO, "Cannot set maximum periods per buffer: {}", snd_strerror(err));
  128. return false;
  129. }
  130. buffer_size_max = BUFFER_SIZE_MAX;
  131. err = snd_pcm_hw_params_set_buffer_size_max(handle, hwparams, &buffer_size_max);
  132. if (err < 0)
  133. {
  134. ERROR_LOG_FMT(AUDIO, "Cannot set maximum buffer size: {}", snd_strerror(err));
  135. return false;
  136. }
  137. err = snd_pcm_hw_params(handle, hwparams);
  138. if (err < 0)
  139. {
  140. ERROR_LOG_FMT(AUDIO, "Unable to install hw params: {}", snd_strerror(err));
  141. return false;
  142. }
  143. err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
  144. if (err < 0)
  145. {
  146. ERROR_LOG_FMT(AUDIO, "Cannot get buffer size: {}", snd_strerror(err));
  147. return false;
  148. }
  149. err = snd_pcm_hw_params_get_periods_max(hwparams, &periods, &dir);
  150. if (err < 0)
  151. {
  152. ERROR_LOG_FMT(AUDIO, "Cannot get periods: {}", snd_strerror(err));
  153. return false;
  154. }
  155. // periods is the number of fragments alsa can wait for during one
  156. // buffer_size
  157. frames_to_deliver = buffer_size / periods;
  158. // limit the minimum size. pulseaudio advertises a minimum of 32 samples.
  159. if (frames_to_deliver < FRAME_COUNT_MIN)
  160. frames_to_deliver = FRAME_COUNT_MIN;
  161. // it is probably a bad idea to try to send more than one buffer of data
  162. if ((unsigned int)frames_to_deliver > buffer_size)
  163. frames_to_deliver = buffer_size;
  164. NOTICE_LOG_FMT(AUDIO,
  165. "ALSA gave us a {} sample \"hardware\" buffer with {} periods. Will send {} "
  166. "samples per fragments.",
  167. buffer_size, periods, frames_to_deliver);
  168. snd_pcm_sw_params_alloca(&swparams);
  169. err = snd_pcm_sw_params_current(handle, swparams);
  170. if (err < 0)
  171. {
  172. ERROR_LOG_FMT(AUDIO, "cannot init sw params: {}", snd_strerror(err));
  173. return false;
  174. }
  175. err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0U);
  176. if (err < 0)
  177. {
  178. ERROR_LOG_FMT(AUDIO, "cannot set start thresh: {}", snd_strerror(err));
  179. return false;
  180. }
  181. err = snd_pcm_sw_params(handle, swparams);
  182. if (err < 0)
  183. {
  184. ERROR_LOG_FMT(AUDIO, "cannot set sw params: {}", snd_strerror(err));
  185. return false;
  186. }
  187. err = snd_pcm_prepare(handle);
  188. if (err < 0)
  189. {
  190. ERROR_LOG_FMT(AUDIO, "Unable to prepare: {}", snd_strerror(err));
  191. return false;
  192. }
  193. NOTICE_LOG_FMT(AUDIO, "ALSA successfully initialized.");
  194. return true;
  195. }
  196. void AlsaSound::AlsaShutdown()
  197. {
  198. if (handle != nullptr)
  199. {
  200. snd_pcm_drop(handle);
  201. snd_pcm_close(handle);
  202. handle = nullptr;
  203. }
  204. }