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