| 6 |
| 7 | #include "../../logging.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "AudioInputPulse.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::audio; |
| 32 |
| 33 | AudioInputPulse::AudioInputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID) |
| 34 | : m_mainloop(mainloop) |
| 35 | , m_context(context) |
| 36 | { |
| 37 | pa_threaded_mainloop_lock(mainloop); |
| 38 |
| 39 | m_stream = CreateAndInitStream(); |
| 40 | pa_threaded_mainloop_unlock(mainloop); |
| 41 | if (m_stream == nullptr) |
| 42 | { |
| 43 | return; |
| 44 | } |
| 45 |
| 46 | SetCurrentDevice(std::move(devID)); |
| 47 | } |
| 48 |
| 49 | AudioInputPulse::~AudioInputPulse() |
| 50 | { |
| 51 | if (m_stream != nullptr) |
| 52 | { |
| 53 | pa_stream_disconnect(m_stream); |
| 54 | pa_stream_unref(m_stream); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | pa_stream* AudioInputPulse::CreateAndInitStream() |
| 59 | { |
| 60 | pa_sample_spec sampleSpec |
| 61 | { |
| 62 | .format = PA_SAMPLE_S16LE, |
| 63 | .rate = 48000, |
| 64 | .channels = 1 |
| 65 | }; |
| 66 | pa_proplist* proplist = pa_proplist_new(); |
| 67 | pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters |
| 68 | pa_stream* stream = pa_stream_new_with_proplist(m_context, "libtgvoip capture", &sampleSpec, nullptr, proplist); |
| 69 | pa_proplist_free(proplist); |
| 70 | if (stream == nullptr) |
| 71 | { |
| 72 | LOGE("Error initializing PulseAudio (pa_stream_new)"); |
| 73 | m_failed = true; |
| 74 | return nullptr; |
| 75 | } |
| 76 | pa_stream_set_state_callback(stream, AudioInputPulse::StreamStateCallback, this); |
| 77 | pa_stream_set_read_callback(stream, AudioInputPulse::StreamReadCallback, this); |
| 78 | return stream; |
| 79 | } |
| 80 |
| 81 | void AudioInputPulse::Start() |
| 82 | { |
| 83 | if (m_failed || m_isRecording) |
| 84 | return; |
| 85 |
| 86 | pa_threaded_mainloop_lock(m_mainloop); |
| 87 | m_isRecording = true; |
| 88 | pa_operation_unref(pa_stream_cork(m_stream, 0, nullptr, nullptr)); |
| 89 | pa_threaded_mainloop_unlock(m_mainloop); |
| 90 | } |
| 91 |
| 92 | void AudioInputPulse::Stop() |
| 93 | { |
| 94 | if (!m_isRecording) |
| 95 | return; |
| 96 |
| 97 | m_isRecording = false; |
| 98 | pa_threaded_mainloop_lock(m_mainloop); |
| 99 | pa_operation_unref(pa_stream_cork(m_stream, 1, nullptr, nullptr)); |
| 100 | pa_threaded_mainloop_unlock(m_mainloop); |
| 101 | } |
| 102 |
| 103 | bool AudioInputPulse::IsRecording() |
| 104 | { |
| 105 | return m_isRecording; |
| 106 | } |
| 107 |
| 108 | void AudioInputPulse::SetCurrentDevice(std::string devID) |
| 109 | { |
| 110 | pa_threaded_mainloop_lock(m_mainloop); |
| 111 | m_currentDevice = std::move(devID); |
| 112 | if (m_isRecording && m_isConnected) |
| 113 | { |
| 114 | pa_stream_disconnect(m_stream); |
| 115 | pa_stream_unref(m_stream); |
| 116 | m_isConnected = false; |
| 117 | m_stream = CreateAndInitStream(); |
| 118 | } |
| 119 |
| 120 | pa_buffer_attr bufferAttr = |
| 121 | { |
| 122 | .maxlength = std::numeric_limits<std::uint32_t>::max(), |
| 123 | .tlength = std::numeric_limits<std::uint32_t>::max(), |
| 124 | .prebuf = std::numeric_limits<std::uint32_t>::max(), |
| 125 | .minreq = std::numeric_limits<std::uint32_t>::max(), |
| 126 | .fragsize = 960 * 2 |
| 127 | }; |
| 128 | int streamFlags = PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY; |
| 129 |
| 130 | int err = pa_stream_connect_record(m_stream, m_currentDevice == "default" ? nullptr : m_currentDevice.c_str(), &bufferAttr, (pa_stream_flags_t)streamFlags); |
| 131 | if (err != 0) |
| 132 | { |
| 133 | pa_threaded_mainloop_unlock(m_mainloop); |
| 134 | } |
| 135 | CHECK_ERROR(err, "pa_stream_connect_record"); |
| 136 |
| 137 | while (true) |
| 138 | { |
| 139 | pa_stream_state_t streamState = pa_stream_get_state(m_stream); |
| 140 | if (!PA_STREAM_IS_GOOD(streamState)) |
| 141 | { |
| 142 | LOGE("Error connecting to audio device '%s'", m_currentDevice.c_str()); |
| 143 | pa_threaded_mainloop_unlock(m_mainloop); |
| 144 | m_failed = true; |
| 145 | return; |
| 146 | } |
| 147 | if (streamState == PA_STREAM_READY) |
| 148 | break; |
| 149 | pa_threaded_mainloop_wait(m_mainloop); |
| 150 | } |
| 151 |
| 152 | m_isConnected = true; |
| 153 |
| 154 | if (m_isRecording) |
| 155 | { |
| 156 | pa_operation_unref(pa_stream_cork(m_stream, 0, nullptr, nullptr)); |
| 157 | } |
| 158 | pa_threaded_mainloop_unlock(m_mainloop); |
| 159 | } |
| 160 |
| 161 | bool AudioInputPulse::EnumerateDevices(std::vector<AudioInputDevice>& devs) |
| 162 | { |
| 163 | return AudioPulse::DoOneOperation([&](pa_context* ctx) |
| 164 | { |
| 165 | return pa_context_get_source_info_list( |
| 166 | ctx, [](pa_context* ctx, const pa_source_info* info, int eol, void* userdata) |
| 167 | { |
| 168 | if (eol > 0) |
| 169 | return; |
| 170 | std::vector<AudioInputDevice>* devs = reinterpret_cast<std::vector<AudioInputDevice>*>(userdata); |
| 171 | AudioInputDevice dev; |
| 172 | dev.id = std::string(info->name); |
| 173 | dev.displayName = std::string(info->description); |
| 174 | devs->emplace_back(dev); |
| 175 | }, |
| 176 | &devs); |
| 177 | }); |
| 178 | } |
| 179 |
| 180 | void AudioInputPulse::StreamStateCallback(pa_stream* s, void* arg) |
| 181 | { |
| 182 | AudioInputPulse* self = reinterpret_cast<AudioInputPulse*>(arg); |
| 183 | pa_threaded_mainloop_signal(self->m_mainloop, 0); |
| 184 | } |
| 185 |
| 186 | void AudioInputPulse::StreamReadCallback(pa_stream* stream, std::size_t requestedBytes, void* userdata) |
| 187 | { |
| 188 | (reinterpret_cast<AudioInputPulse*>(userdata))->StreamReadCallback(stream, requestedBytes); |
| 189 | } |
| 190 |
| 191 | void AudioInputPulse::StreamReadCallback(pa_stream* stream, std::size_t requestedBytes) |
| 192 | { |
| 193 | std::size_t bytesRemaining = requestedBytes; |
| 194 | std::uint8_t* buffer = nullptr; |
| 195 | pa_usec_t latency; |
| 196 | if (pa_stream_get_latency(stream, &latency, nullptr) == 0) |
| 197 | { |
| 198 | m_estimatedDelay = static_cast<std::int32_t>(latency / 100); |
| 199 | } |
| 200 | while (bytesRemaining > 0) |
| 201 | { |
| 202 | std::size_t bytesToFill = 102400; |
| 203 |
| 204 | if (bytesToFill > bytesRemaining) |
| 205 | bytesToFill = bytesRemaining; |
| 206 |
| 207 | int err = pa_stream_peek(stream, reinterpret_cast<const void**>(&buffer), &bytesToFill); |
| 208 | CHECK_ERROR(err, "pa_stream_peek"); |
| 209 |
| 210 | if (m_isRecording) |
| 211 | { |
| 212 | if (m_remainingDataSize + bytesToFill > sizeof(m_remainingData)) |
| 213 | { |
| 214 | LOGE("Capture buffer is too big (%d)", static_cast<int>(bytesToFill)); |
| 215 | } |
| 216 | std::memcpy(m_remainingData + m_remainingDataSize, buffer, bytesToFill); |
| 217 | m_remainingDataSize += bytesToFill; |
| 218 | while (m_remainingDataSize >= 960 * 2) |
| 219 | { |
| 220 | InvokeCallback(m_remainingData, 960 * 2); |
| 221 | std::memmove(m_remainingData, m_remainingData + 960 * 2, m_remainingDataSize - 960 * 2); |
| 222 | m_remainingDataSize -= 960 * 2; |
| 223 | } |
| 224 | } |
| 225 |
| 226 | err = pa_stream_drop(stream); |
| 227 | CHECK_ERROR(err, "pa_stream_drop"); |
| 228 |
| 229 | bytesRemaining -= bytesToFill; |
| 230 | } |
| 231 | } |
| 232 |