| 6 |
| 7 | #include "../../logging.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "AudioOutputPulse.h" |
| 10 | #include "AudioPulse.h" |
| 11 | #include "PulseFunctions.h" |
| 12 |
| 13 | #include <dlfcn.h> |
| 14 | #include <unistd.h> |
| 15 | #if !defined(__GLIBC__) |
| 16 | #include <libgen.h> |
| 17 | #endif |
| 18 |
| 19 | #include <cassert> |
| 20 | #include <cstring> |
| 21 |
| 22 | #define BUFFER_SIZE 960 |
| 23 | #define CHECK_ERROR(res, msg) \ |
| 24 | if (res != 0) \ |
| 25 | { \ |
| 26 | LOGE(msg " failed: %s", pa_strerror(res)); \ |
| 27 | m_failed = true; \ |
| 28 | return; \ |
| 29 | } |
| 30 |
| 31 | using namespace tgvoip; |
| 32 | using namespace tgvoip::audio; |
| 33 |
| 34 | AudioOutputPulse::AudioOutputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID) |
| 35 | : m_mainloop(mainloop) |
| 36 | , m_context(context) |
| 37 | { |
| 38 | pa_threaded_mainloop_lock(mainloop); |
| 39 | m_stream = CreateAndInitStream(); |
| 40 | pa_threaded_mainloop_unlock(mainloop); |
| 41 |
| 42 | SetCurrentDevice(std::move(devID)); |
| 43 | } |
| 44 |
| 45 | AudioOutputPulse::~AudioOutputPulse() |
| 46 | { |
| 47 | if (m_stream) |
| 48 | { |
| 49 | pa_stream_disconnect(m_stream); |
| 50 | pa_stream_unref(m_stream); |
| 51 | } |
| 52 | } |
| 53 |
| 54 | pa_stream* AudioOutputPulse::CreateAndInitStream() |
| 55 | { |
| 56 | pa_sample_spec sampleSpec |
| 57 | { |
| 58 | .format = PA_SAMPLE_S16LE, |
| 59 | .rate = 48000, |
| 60 | .channels = 1 |
| 61 | }; |
| 62 | pa_proplist* proplist = pa_proplist_new(); |
| 63 | pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters |
| 64 | pa_stream* stream = pa_stream_new_with_proplist(m_context, "libtgvoip playback", &sampleSpec, nullptr, proplist); |
| 65 | pa_proplist_free(proplist); |
| 66 | if (stream == nullptr) |
| 67 | { |
| 68 | LOGE("Error initializing PulseAudio (pa_stream_new)"); |
| 69 | m_failed = true; |
| 70 | return nullptr; |
| 71 | } |
| 72 | pa_stream_set_state_callback(stream, AudioOutputPulse::StreamStateCallback, this); |
| 73 | pa_stream_set_write_callback(stream, AudioOutputPulse::StreamWriteCallback, this); |
| 74 | return stream; |
| 75 | } |
| 76 |
| 77 | void AudioOutputPulse::Start() |
| 78 | { |
| 79 | if (m_failed || m_isPlaying) |
| 80 | return; |
| 81 |
| 82 | m_isPlaying = true; |
| 83 | pa_threaded_mainloop_lock(m_mainloop); |
| 84 | pa_operation_unref(pa_stream_cork(m_stream, 0, nullptr, nullptr)); |
| 85 | pa_threaded_mainloop_unlock(m_mainloop); |
| 86 | } |
| 87 |
| 88 | void AudioOutputPulse::Stop() |
| 89 | { |
| 90 | if (!m_isPlaying) |
| 91 | return; |
| 92 |
| 93 | m_isPlaying = false; |
| 94 | pa_threaded_mainloop_lock(m_mainloop); |
| 95 | pa_operation_unref(pa_stream_cork(m_stream, 1, nullptr, nullptr)); |
| 96 | pa_threaded_mainloop_unlock(m_mainloop); |
| 97 | } |
| 98 |
| 99 | bool AudioOutputPulse::IsPlaying() |
| 100 | { |
| 101 | return m_isPlaying; |
| 102 | } |
| 103 |
| 104 | void AudioOutputPulse::SetCurrentDevice(std::string devID) |
| 105 | { |
| 106 | pa_threaded_mainloop_lock(m_mainloop); |
| 107 | m_currentDevice = std::move(devID); |
| 108 | if (m_isPlaying && m_isConnected) |
| 109 | { |
| 110 | pa_stream_disconnect(m_stream); |
| 111 | pa_stream_unref(m_stream); |
| 112 | m_isConnected = false; |
| 113 | m_stream = CreateAndInitStream(); |
| 114 | } |
| 115 |
| 116 | pa_buffer_attr bufferAttr = |
| 117 | { |
| 118 | .maxlength = std::numeric_limits<std::uint32_t>::max(), |
| 119 | .tlength = 960 * 2, |
| 120 | .prebuf = std::numeric_limits<std::uint32_t>::max(), |
| 121 | .minreq = std::numeric_limits<std::uint32_t>::max(), |
| 122 | .fragsize = std::numeric_limits<std::uint32_t>::max() |
| 123 | }; |
| 124 | int streamFlags = PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY; |
| 125 |
| 126 | int err = pa_stream_connect_playback(m_stream, devID == "default" ? nullptr : devID.c_str(), &bufferAttr, static_cast<pa_stream_flags_t>(streamFlags), nullptr, nullptr); |
| 127 | if (err != 0 && devID != "default") |
| 128 | { |
| 129 | SetCurrentDevice("default"); |
| 130 | return; |
| 131 | } |
| 132 | CHECK_ERROR(err, "pa_stream_connect_playback"); |
| 133 |
| 134 | while (true) |
| 135 | { |
| 136 | pa_stream_state_t streamState = pa_stream_get_state(m_stream); |
| 137 | if (!PA_STREAM_IS_GOOD(streamState)) |
| 138 | { |
| 139 | LOGE("Error connecting to audio device '%s'", devID.c_str()); |
| 140 | m_failed = true; |
| 141 | return; |
| 142 | } |
| 143 | if (streamState == PA_STREAM_READY) |
| 144 | break; |
| 145 | pa_threaded_mainloop_wait(m_mainloop); |
| 146 | } |
| 147 |
| 148 | m_isConnected = true; |
| 149 |
| 150 | if (m_isPlaying) |
| 151 | { |
| 152 | pa_operation_unref(pa_stream_cork(m_stream, 0, nullptr, nullptr)); |
| 153 | } |
| 154 | pa_threaded_mainloop_unlock(m_mainloop); |
| 155 | } |
| 156 |
| 157 | bool AudioOutputPulse::EnumerateDevices(std::vector<AudioOutputDevice>& devs) |
| 158 | { |
| 159 | return AudioPulse::DoOneOperation([&](pa_context* ctx) |
| 160 | { |
| 161 | return pa_context_get_sink_info_list( |
| 162 | ctx, [](pa_context* ctx, const pa_sink_info* info, int eol, void* userdata) |
| 163 | { |
| 164 | if (eol > 0) |
| 165 | return; |
| 166 | std::vector<AudioOutputDevice>* devs = reinterpret_cast<std::vector<AudioOutputDevice>*>(userdata); |
| 167 | AudioOutputDevice dev; |
| 168 | dev.id = std::string(info->name); |
| 169 | dev.displayName = std::string(info->description); |
| 170 | devs->emplace_back(dev); |
| 171 | }, |
| 172 | &devs); |
| 173 | }); |
| 174 | } |
| 175 |
| 176 | void AudioOutputPulse::StreamStateCallback(pa_stream* s, void* arg) |
| 177 | { |
| 178 | AudioOutputPulse* self = reinterpret_cast<AudioOutputPulse*>(arg); |
| 179 | pa_threaded_mainloop_signal(self->m_mainloop, 0); |
| 180 | } |
| 181 |
| 182 | void AudioOutputPulse::StreamWriteCallback(pa_stream* stream, std::size_t requestedBytes, void* userdata) |
| 183 | { |
| 184 | (reinterpret_cast<AudioOutputPulse*>(userdata))->StreamWriteCallback(stream, requestedBytes); |
| 185 | } |
| 186 |
| 187 | void AudioOutputPulse::StreamWriteCallback(pa_stream* stream, std::size_t requestedBytes) |
| 188 | { |
| 189 | if (requestedBytes > sizeof(m_remainingData)) |
| 190 | { |
| 191 | requestedBytes = 960 * 2; // force buffer size to 20ms. This probably wrecks the jitter buffer, but still better than crashing |
| 192 | } |
| 193 | pa_usec_t latency; |
| 194 | if (pa_stream_get_latency(stream, &latency, nullptr) == 0) |
| 195 | { |
| 196 | m_estimatedDelay = static_cast<std::int32_t>(latency / 100); |
| 197 | } |
| 198 | while (requestedBytes > m_remainingDataSize) |
| 199 | { |
| 200 | if (m_isPlaying) |
| 201 | { |
| 202 | InvokeCallback(m_remainingData + m_remainingDataSize, 960 * 2); |
| 203 | m_remainingDataSize += 960 * 2; |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | std::memset(m_remainingData + m_remainingDataSize, 0, requestedBytes - m_remainingDataSize); |
| 208 | m_remainingDataSize = requestedBytes; |
| 209 | } |
| 210 | } |
| 211 | int err = pa_stream_write(stream, m_remainingData, requestedBytes, nullptr, 0, PA_SEEK_RELATIVE); |
| 212 | CHECK_ERROR(err, "pa_stream_write"); |
| 213 | m_remainingDataSize -= requestedBytes; |
| 214 | if (m_remainingDataSize > 0) |
| 215 | memmove(m_remainingData, m_remainingData + requestedBytes, m_remainingDataSize); |
| 216 | } |
| 217 |