2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-21 00:17:03 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-09-09 15:26:33 -06:00
|
|
|
|
2015-07-07 07:30:27 -06:00
|
|
|
#include <mutex>
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "AudioCommon/AlsaSoundStream.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/Thread.h"
|
2009-09-09 15:26:33 -06:00
|
|
|
|
2015-05-24 02:13:02 -06:00
|
|
|
AlsaSound::AlsaSound()
|
|
|
|
: m_thread_status(ALSAThreadStatus::STOPPED), handle(nullptr),
|
2015-05-10 00:30:24 -06:00
|
|
|
frames_to_deliver(FRAME_COUNT_MIN)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
AlsaSound::~AlsaSound()
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2017-10-21 17:23:40 -06:00
|
|
|
m_thread_status.store(ALSAThreadStatus::STOPPING);
|
|
|
|
|
2019-04-06 16:39:25 -06:00
|
|
|
// Immediately lock and unlock mutex to prevent cv race.
|
|
|
|
std::unique_lock<std::mutex>{cv_m};
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
// Give the opportunity to the audio thread
|
|
|
|
// to realize we are stopping the emulation
|
|
|
|
cv.notify_one();
|
2019-10-09 08:07:25 -06:00
|
|
|
if (thread.joinable())
|
|
|
|
thread.join();
|
2017-10-21 17:23:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AlsaSound::Init()
|
|
|
|
{
|
|
|
|
m_thread_status.store(ALSAThreadStatus::PAUSED);
|
2015-08-08 16:37:48 -06:00
|
|
|
if (!AlsaInit())
|
|
|
|
{
|
|
|
|
m_thread_status.store(ALSAThreadStatus::STOPPED);
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-09-08 11:39:51 -06:00
|
|
|
thread = std::thread(&AlsaSound::SoundLoop, this);
|
2009-09-09 15:26:33 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlsaSound::Update()
|
|
|
|
{
|
|
|
|
// don't need to do anything here.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called on audio thread.
|
|
|
|
void AlsaSound::SoundLoop()
|
|
|
|
{
|
2011-12-30 21:22:48 -07:00
|
|
|
Common::SetCurrentThreadName("Audio thread - alsa");
|
2015-09-29 05:51:34 -06:00
|
|
|
while (m_thread_status.load() != ALSAThreadStatus::STOPPING)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2015-09-29 05:51:34 -06:00
|
|
|
while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2015-09-29 05:51:34 -06:00
|
|
|
m_mixer->Mix(mix_buffer, frames_to_deliver);
|
|
|
|
int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
|
|
|
|
if (rc == -EPIPE)
|
|
|
|
{
|
|
|
|
// Underrun
|
|
|
|
snd_pcm_prepare(handle);
|
|
|
|
}
|
|
|
|
else if (rc < 0)
|
|
|
|
{
|
|
|
|
ERROR_LOG(AUDIO, "writei fail: %s", snd_strerror(rc));
|
|
|
|
}
|
2009-09-09 15:26:33 -06:00
|
|
|
}
|
2015-09-29 05:51:34 -06:00
|
|
|
if (m_thread_status.load() == ALSAThreadStatus::PAUSED)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2015-09-29 05:51:34 -06:00
|
|
|
snd_pcm_drop(handle); // Stop sound output
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-29 05:51:34 -06:00
|
|
|
// Block until thread status changes.
|
|
|
|
std::unique_lock<std::mutex> lock(cv_m);
|
|
|
|
cv.wait(lock, [this] { return m_thread_status.load() != ALSAThreadStatus::PAUSED; });
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-29 05:51:34 -06:00
|
|
|
snd_pcm_prepare(handle); // resume sound output
|
2009-09-09 15:26:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AlsaShutdown();
|
2015-05-10 00:30:24 -06:00
|
|
|
m_thread_status.store(ALSAThreadStatus::STOPPED);
|
2009-09-09 15:26:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-21 17:23:40 -06:00
|
|
|
bool AlsaSound::SetRunning(bool running)
|
2015-07-07 07:30:27 -06:00
|
|
|
{
|
2017-10-21 13:37:39 -06:00
|
|
|
m_thread_status.store(running ? ALSAThreadStatus::RUNNING : ALSAThreadStatus::PAUSED);
|
2019-04-06 16:39:25 -06:00
|
|
|
|
|
|
|
// Immediately lock and unlock mutex to prevent cv race.
|
|
|
|
std::unique_lock<std::mutex>{cv_m};
|
|
|
|
|
|
|
|
// Notify thread that status has changed
|
|
|
|
cv.notify_one();
|
2017-10-21 17:23:40 -06:00
|
|
|
return true;
|
2015-07-07 07:30:27 -06:00
|
|
|
}
|
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
bool AlsaSound::AlsaInit()
|
|
|
|
{
|
|
|
|
unsigned int sample_rate = m_mixer->GetSampleRate();
|
|
|
|
int err;
|
|
|
|
int dir;
|
|
|
|
snd_pcm_sw_params_t* swparams;
|
|
|
|
snd_pcm_hw_params_t* hwparams;
|
2010-05-21 16:48:57 -06:00
|
|
|
snd_pcm_uframes_t buffer_size, buffer_size_max;
|
2010-05-07 17:14:40 -06:00
|
|
|
unsigned int periods;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Audio open error: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
snd_pcm_hw_params_alloca(&hwparams);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_hw_params_any(handle, hwparams);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Broken configuration for this PCM: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Access type not available: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-19 19:51:12 -06:00
|
|
|
err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16_LE);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Sample format not available: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-12-27 08:05:18 -07:00
|
|
|
dir = 0;
|
2009-10-14 20:04:27 -06:00
|
|
|
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &sample_rate, &dir);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Rate not available: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-30 02:41:33 -06:00
|
|
|
err = snd_pcm_hw_params_set_channels(handle, hwparams, CHANNEL_COUNT);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Channels count not available: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-05-21 16:48:57 -06:00
|
|
|
periods = BUFFER_SIZE_MAX / FRAME_COUNT_MIN;
|
|
|
|
err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &periods, &dir);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2010-05-07 17:14:40 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Cannot set maximum periods per buffer: %s", snd_strerror(err));
|
2010-05-07 17:14:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-05-21 16:48:57 -06:00
|
|
|
buffer_size_max = BUFFER_SIZE_MAX;
|
|
|
|
err = snd_pcm_hw_params_set_buffer_size_max(handle, hwparams, &buffer_size_max);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2010-05-07 17:14:40 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Cannot set maximum buffer size: %s", snd_strerror(err));
|
2010-05-07 17:14:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-05-21 16:48:57 -06:00
|
|
|
err = snd_pcm_hw_params(handle, hwparams);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2010-05-07 17:14:40 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Unable to install hw params: %s", snd_strerror(err));
|
2010-05-21 16:48:57 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-05-21 16:48:57 -06:00
|
|
|
err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2010-05-21 16:48:57 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Cannot get buffer size: %s", snd_strerror(err));
|
2010-05-07 17:14:40 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2010-05-21 16:48:57 -06:00
|
|
|
err = snd_pcm_hw_params_get_periods_max(hwparams, &periods, &dir);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Cannot get periods: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
// periods is the number of fragments alsa can wait for during one
|
2010-05-21 16:48:57 -06:00
|
|
|
// buffer_size
|
|
|
|
frames_to_deliver = buffer_size / periods;
|
|
|
|
// limit the minimum size. pulseaudio advertises a minimum of 32 samples.
|
|
|
|
if (frames_to_deliver < FRAME_COUNT_MIN)
|
2013-03-19 19:51:12 -06:00
|
|
|
frames_to_deliver = FRAME_COUNT_MIN;
|
2010-05-21 16:48:57 -06:00
|
|
|
// it is probably a bad idea to try to send more than one buffer of data
|
|
|
|
if ((unsigned int)frames_to_deliver > buffer_size)
|
2013-03-19 19:51:12 -06:00
|
|
|
frames_to_deliver = buffer_size;
|
2018-04-12 06:18:04 -06:00
|
|
|
NOTICE_LOG(AUDIO,
|
|
|
|
"ALSA gave us a %ld sample \"hardware\" buffer with %d periods. Will send %d "
|
|
|
|
"samples per fragments.",
|
2010-12-05 02:04:34 -07:00
|
|
|
buffer_size, periods, frames_to_deliver);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
snd_pcm_sw_params_alloca(&swparams);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_sw_params_current(handle, swparams);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "cannot init sw params: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, 0U);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "cannot set start thresh: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_sw_params(handle, swparams);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "cannot set sw params: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-09-09 15:26:33 -06:00
|
|
|
err = snd_pcm_prepare(handle);
|
2013-10-28 23:23:17 -06:00
|
|
|
if (err < 0)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
2016-11-01 19:19:00 -06:00
|
|
|
ERROR_LOG(AUDIO, "Unable to prepare: %s", snd_strerror(err));
|
2009-09-09 15:26:33 -06:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-01 19:19:00 -06:00
|
|
|
NOTICE_LOG(AUDIO, "ALSA successfully initialized.");
|
2009-09-09 15:26:33 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlsaSound::AlsaShutdown()
|
|
|
|
{
|
2014-03-09 14:14:26 -06:00
|
|
|
if (handle != nullptr)
|
2009-09-09 15:26:33 -06:00
|
|
|
{
|
|
|
|
snd_pcm_drop(handle);
|
|
|
|
snd_pcm_close(handle);
|
2014-03-09 14:14:26 -06:00
|
|
|
handle = nullptr;
|
2009-09-09 15:26:33 -06:00
|
|
|
}
|
|
|
|
}
|