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