2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-21 06:17:03 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-10-15 17:28:23 +00:00
|
|
|
|
2015-09-28 15:57:16 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "AudioCommon/PulseAudioStream.h"
|
2021-04-01 21:30:01 +00:00
|
|
|
|
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Thread.h"
|
2015-01-07 10:57:20 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2009-10-15 17:28:23 +00:00
|
|
|
|
2013-01-09 23:39:19 +00:00
|
|
|
namespace
|
2009-10-15 17:28:23 +00:00
|
|
|
{
|
2015-01-07 10:57:20 +00:00
|
|
|
const size_t BUFFER_SAMPLES = 512; // ~10 ms - needs to be at least 240 for surround
|
2009-10-15 17:28:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 18:42:25 +00:00
|
|
|
PulseAudio::PulseAudio() = default;
|
2009-10-15 17:28:23 +00:00
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
bool PulseAudio::Init()
|
2009-10-15 17:28:23 +00:00
|
|
|
{
|
2019-11-11 11:03:36 +00:00
|
|
|
m_stereo = !SConfig::GetInstance().ShouldUseDPL2Decoder();
|
2017-08-09 19:59:37 +00:00
|
|
|
m_channels = m_stereo ? 2 : 6; // will tell PA we use a Stereo or 5.0 channel setup
|
2015-01-07 10:57:20 +00:00
|
|
|
|
2020-10-21 17:32:25 +00:00
|
|
|
NOTICE_LOG_FMT(AUDIO, "PulseAudio backend using {} channels", m_channels);
|
2015-01-07 10:57:20 +00:00
|
|
|
|
2016-08-05 14:04:39 +00:00
|
|
|
m_run_thread.Set();
|
2014-09-08 17:39:51 +00:00
|
|
|
m_thread = std::thread(&PulseAudio::SoundLoop, this);
|
2015-01-07 10:57:20 +00:00
|
|
|
|
2009-10-15 17:28:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-21 23:23:40 +00:00
|
|
|
PulseAudio::~PulseAudio()
|
2009-10-15 17:28:23 +00:00
|
|
|
{
|
2016-08-05 14:04:39 +00:00
|
|
|
m_run_thread.Clear();
|
2014-01-31 21:00:33 +00:00
|
|
|
m_thread.join();
|
2009-10-15 17:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called on audio thread.
|
|
|
|
void PulseAudio::SoundLoop()
|
|
|
|
{
|
2011-12-31 04:22:48 +00:00
|
|
|
Common::SetCurrentThreadName("Audio thread - pulse");
|
|
|
|
|
2013-01-09 23:39:19 +00:00
|
|
|
if (PulseInit())
|
2010-08-17 02:14:04 +00:00
|
|
|
{
|
2016-08-05 14:04:39 +00:00
|
|
|
while (m_run_thread.IsSet() && m_pa_connected == 1 && m_pa_error >= 0)
|
2014-03-09 20:14:26 +00:00
|
|
|
m_pa_error = pa_mainloop_iterate(m_pa_ml, 1, nullptr);
|
2014-01-31 21:00:33 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if (m_pa_error < 0)
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "PulseAudio error: {}", pa_strerror(m_pa_error));
|
2010-08-17 02:14:04 +00:00
|
|
|
|
2013-01-09 23:39:19 +00:00
|
|
|
PulseShutdown();
|
2010-08-17 02:14:04 +00:00
|
|
|
}
|
2009-10-15 17:28:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 23:39:19 +00:00
|
|
|
bool PulseAudio::PulseInit()
|
2009-10-15 17:28:23 +00:00
|
|
|
{
|
2014-01-31 21:00:33 +00:00
|
|
|
m_pa_error = 0;
|
|
|
|
m_pa_connected = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
// create pulseaudio main loop and context
|
|
|
|
// also register the async state callback which is called when the connection to the pa server has
|
|
|
|
// changed
|
|
|
|
m_pa_ml = pa_mainloop_new();
|
|
|
|
m_pa_mlapi = pa_mainloop_get_api(m_pa_ml);
|
|
|
|
m_pa_ctx = pa_context_new(m_pa_mlapi, "dolphin-emu");
|
2014-03-09 20:14:26 +00:00
|
|
|
m_pa_error = pa_context_connect(m_pa_ctx, nullptr, PA_CONTEXT_NOFLAGS, nullptr);
|
2014-01-31 21:00:33 +00:00
|
|
|
pa_context_set_state_callback(m_pa_ctx, StateCallback, this);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
// wait until we're connected to the pulseaudio server
|
|
|
|
while (m_pa_connected == 0 && m_pa_error >= 0)
|
2014-03-09 20:14:26 +00:00
|
|
|
m_pa_error = pa_mainloop_iterate(m_pa_ml, 1, nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
if (m_pa_connected == 2 || m_pa_error < 0)
|
|
|
|
{
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "PulseAudio failed to initialize: {}", pa_strerror(m_pa_error));
|
2014-01-31 21:00:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
// create a new audio stream with our sample format
|
|
|
|
// also connect the callbacks for this stream
|
|
|
|
pa_sample_spec ss;
|
2015-01-07 10:57:20 +00:00
|
|
|
pa_channel_map channel_map;
|
|
|
|
pa_channel_map* channel_map_p = nullptr; // auto channel map
|
|
|
|
if (m_stereo)
|
|
|
|
{
|
|
|
|
ss.format = PA_SAMPLE_S16LE;
|
|
|
|
m_bytespersample = sizeof(s16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// surround is remixed in floats, use a float PA buffer to save another conversion
|
|
|
|
ss.format = PA_SAMPLE_FLOAT32NE;
|
|
|
|
m_bytespersample = sizeof(float);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-01-07 10:57:20 +00:00
|
|
|
channel_map_p = &channel_map; // explicit channel map:
|
2017-08-09 19:59:37 +00:00
|
|
|
channel_map.channels = 6;
|
2015-01-07 10:57:20 +00:00
|
|
|
channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
|
|
|
|
channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
|
|
|
|
channel_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
|
2017-08-09 19:59:37 +00:00
|
|
|
channel_map.map[3] = PA_CHANNEL_POSITION_LFE;
|
|
|
|
channel_map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
|
|
|
|
channel_map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
|
2015-01-07 10:57:20 +00:00
|
|
|
}
|
|
|
|
ss.channels = m_channels;
|
2013-01-14 04:20:33 +00:00
|
|
|
ss.rate = m_mixer->GetSampleRate();
|
2021-04-01 21:30:01 +00:00
|
|
|
ASSERT(pa_sample_spec_valid(&ss));
|
2015-01-07 10:57:20 +00:00
|
|
|
m_pa_s = pa_stream_new(m_pa_ctx, "Playback", &ss, channel_map_p);
|
2014-01-31 21:00:33 +00:00
|
|
|
pa_stream_set_write_callback(m_pa_s, WriteCallback, this);
|
|
|
|
pa_stream_set_underflow_callback(m_pa_s, UnderflowCallback, this);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
// connect this audio stream to the default audio playback
|
|
|
|
// limit buffersize to reduce latency
|
|
|
|
m_pa_ba.fragsize = -1;
|
|
|
|
m_pa_ba.maxlength = -1; // max buffer, so also max latency
|
|
|
|
m_pa_ba.minreq = -1; // don't read every byte, try to group them _a bit_
|
|
|
|
m_pa_ba.prebuf = -1; // start as early as possible
|
2015-01-07 10:57:20 +00:00
|
|
|
m_pa_ba.tlength =
|
|
|
|
BUFFER_SAMPLES * m_channels *
|
|
|
|
m_bytespersample; // designed latency, only change this flag for low latency output
|
2014-01-31 21:00:33 +00:00
|
|
|
pa_stream_flags flags = pa_stream_flags(PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY |
|
|
|
|
PA_STREAM_AUTO_TIMING_UPDATE);
|
2014-03-09 20:14:26 +00:00
|
|
|
m_pa_error = pa_stream_connect_playback(m_pa_s, nullptr, &m_pa_ba, flags, nullptr, nullptr);
|
2014-01-31 21:00:33 +00:00
|
|
|
if (m_pa_error < 0)
|
2009-10-15 17:28:23 +00:00
|
|
|
{
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "PulseAudio failed to initialize: {}", pa_strerror(m_pa_error));
|
2013-01-09 23:39:19 +00:00
|
|
|
return false;
|
2009-10-15 17:28:23 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 17:32:25 +00:00
|
|
|
INFO_LOG_FMT(AUDIO, "Pulse successfully initialized");
|
2014-01-31 21:00:33 +00:00
|
|
|
return true;
|
2010-08-17 02:14:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 23:39:19 +00:00
|
|
|
void PulseAudio::PulseShutdown()
|
2010-08-17 02:14:04 +00:00
|
|
|
{
|
2014-01-31 21:00:33 +00:00
|
|
|
pa_context_disconnect(m_pa_ctx);
|
|
|
|
pa_context_unref(m_pa_ctx);
|
|
|
|
pa_mainloop_free(m_pa_ml);
|
2010-08-17 02:14:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
void PulseAudio::StateCallback(pa_context* c)
|
2010-08-17 02:14:04 +00:00
|
|
|
{
|
2014-01-31 21:00:33 +00:00
|
|
|
pa_context_state_t state = pa_context_get_state(c);
|
|
|
|
switch (state)
|
2010-08-17 02:14:04 +00:00
|
|
|
{
|
2014-01-31 21:00:33 +00:00
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
m_pa_connected = 2;
|
|
|
|
break;
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
m_pa_connected = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-17 02:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 21:00:33 +00:00
|
|
|
// on underflow, increase pulseaudio latency in ~10ms steps
|
|
|
|
void PulseAudio::UnderflowCallback(pa_stream* s)
|
|
|
|
{
|
2015-01-07 10:57:20 +00:00
|
|
|
m_pa_ba.tlength += BUFFER_SAMPLES * m_channels * m_bytespersample;
|
2015-08-15 10:51:11 +00:00
|
|
|
pa_operation* op = pa_stream_set_buffer_attr(s, &m_pa_ba, nullptr, nullptr);
|
|
|
|
pa_operation_unref(op);
|
2014-01-31 21:00:33 +00:00
|
|
|
|
2020-10-21 17:32:25 +00:00
|
|
|
WARN_LOG_FMT(AUDIO, "pulseaudio underflow, new latency: {} bytes", m_pa_ba.tlength);
|
2014-01-31 21:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PulseAudio::WriteCallback(pa_stream* s, size_t length)
|
|
|
|
{
|
2015-01-07 10:57:20 +00:00
|
|
|
int bytes_per_frame = m_channels * m_bytespersample;
|
|
|
|
int frames = (length / bytes_per_frame);
|
|
|
|
size_t trunc_length = frames * bytes_per_frame;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
// fetch dst buffer directly from pulseaudio, so no memcpy is needed
|
|
|
|
void* buffer;
|
2015-01-07 10:57:20 +00:00
|
|
|
m_pa_error = pa_stream_begin_write(s, &buffer, &trunc_length);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-01-31 21:00:33 +00:00
|
|
|
if (!buffer || m_pa_error < 0)
|
|
|
|
return; // error will be printed from main loop
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-01-07 10:57:20 +00:00
|
|
|
if (m_stereo)
|
|
|
|
{
|
|
|
|
// use the raw s16 stereo mix
|
|
|
|
m_mixer->Mix((s16*)buffer, frames);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-09 19:59:37 +00:00
|
|
|
if (m_channels == 6) // Extract dpl2/5.1 Surround
|
2015-01-07 10:57:20 +00:00
|
|
|
{
|
2017-08-09 19:59:37 +00:00
|
|
|
m_mixer->MixSurround((float*)buffer, frames);
|
2015-01-07 10:57:20 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-21 17:32:25 +00:00
|
|
|
ERROR_LOG_FMT(AUDIO, "Unsupported number of PA channels requested: {}", m_channels);
|
2015-01-07 10:57:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-01-07 10:57:20 +00:00
|
|
|
m_pa_error = pa_stream_write(s, buffer, trunc_length, nullptr, 0, PA_SEEK_RELATIVE);
|
2014-01-31 21:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Callbacks that forward to internal methods (required because PulseAudio is a C API).
|
|
|
|
|
|
|
|
void PulseAudio::StateCallback(pa_context* c, void* userdata)
|
|
|
|
{
|
|
|
|
PulseAudio* p = (PulseAudio*)userdata;
|
|
|
|
p->StateCallback(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PulseAudio::UnderflowCallback(pa_stream* s, void* userdata)
|
|
|
|
{
|
|
|
|
PulseAudio* p = (PulseAudio*)userdata;
|
|
|
|
p->UnderflowCallback(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PulseAudio::WriteCallback(pa_stream* s, size_t length, void* userdata)
|
|
|
|
{
|
|
|
|
PulseAudio* p = (PulseAudio*)userdata;
|
|
|
|
p->WriteCallback(s, length);
|
|
|
|
}
|