2017-05-12 18:29:35 +02:00
|
|
|
//
|
|
|
|
// libtgvoip is free and unencumbered public domain software.
|
|
|
|
// For more information, see http://unlicense.org or the UNLICENSE file
|
|
|
|
// you should have received with this source code distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "AudioOutputPulse.h"
|
|
|
|
#include "../../logging.h"
|
|
|
|
#include "../../VoIPController.h"
|
2018-07-17 18:48:21 +02:00
|
|
|
#include "AudioPulse.h"
|
|
|
|
#include "PulseFunctions.h"
|
2018-02-19 17:49:30 +01:00
|
|
|
#if !defined(__GLIBC__)
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2017-05-12 18:29:35 +02:00
|
|
|
|
|
|
|
#define BUFFER_SIZE 960
|
2017-05-17 13:30:14 +02:00
|
|
|
#define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
|
2017-05-12 18:29:35 +02:00
|
|
|
|
2017-05-24 02:31:35 +02:00
|
|
|
using namespace tgvoip;
|
2017-05-12 18:29:35 +02:00
|
|
|
using namespace tgvoip::audio;
|
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
AudioOutputPulse::AudioOutputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID){
|
2017-05-12 18:29:35 +02:00
|
|
|
isPlaying=false;
|
|
|
|
isConnected=false;
|
2017-05-21 16:50:23 +02:00
|
|
|
didStart=false;
|
2017-05-21 20:15:42 +02:00
|
|
|
isLocked=false;
|
2017-05-12 18:29:35 +02:00
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
this->mainloop=mainloop;
|
|
|
|
this->context=context;
|
2017-05-12 18:29:35 +02:00
|
|
|
stream=NULL;
|
|
|
|
remainingDataSize=0;
|
|
|
|
|
|
|
|
pa_sample_spec sample_specifications{
|
|
|
|
.format=PA_SAMPLE_S16LE,
|
|
|
|
.rate=48000,
|
|
|
|
.channels=1
|
|
|
|
};
|
|
|
|
|
2018-07-17 18:48:21 +02:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2018-08-07 22:10:31 +02:00
|
|
|
pa_proplist* proplist=pa_proplist_new();
|
|
|
|
pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters
|
|
|
|
stream=pa_stream_new_with_proplist(context, "libtgvoip playback", &sample_specifications, NULL, proplist);
|
|
|
|
pa_proplist_free(proplist);
|
2017-05-12 18:29:35 +02:00
|
|
|
if(!stream){
|
|
|
|
LOGE("Error initializing PulseAudio (pa_stream_new)");
|
2018-07-17 18:48:21 +02:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
failed=true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pa_stream_set_state_callback(stream, AudioOutputPulse::StreamStateCallback, this);
|
|
|
|
pa_stream_set_write_callback(stream, AudioOutputPulse::StreamWriteCallback, this);
|
2017-05-21 20:15:42 +02:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
|
|
|
|
SetCurrentDevice(devID);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioOutputPulse::~AudioOutputPulse(){
|
|
|
|
if(stream){
|
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
pa_stream_unref(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::Start(){
|
|
|
|
if(failed || isPlaying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
isPlaying=true;
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2018-07-17 18:48:21 +02:00
|
|
|
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::Stop(){
|
|
|
|
if(!isPlaying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
isPlaying=false;
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2018-07-17 18:48:21 +02:00
|
|
|
pa_operation_unref(pa_stream_cork(stream, 1, NULL, NULL));
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioOutputPulse::IsPlaying(){
|
|
|
|
return isPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::SetCurrentDevice(std::string devID){
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
currentDevice=devID;
|
|
|
|
if(isPlaying && isConnected){
|
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
isConnected=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_buffer_attr bufferAttr={
|
2018-07-01 23:42:49 +02:00
|
|
|
.maxlength=(uint32_t)-1,
|
|
|
|
.tlength=960*2,
|
|
|
|
.prebuf=(uint32_t)-1,
|
|
|
|
.minreq=(uint32_t)-1,
|
|
|
|
.fragsize=(uint32_t)-1
|
2017-05-12 18:29:35 +02:00
|
|
|
};
|
2018-07-01 23:42:49 +02:00
|
|
|
int streamFlags=PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY;
|
2017-05-12 18:29:35 +02:00
|
|
|
|
|
|
|
int err=pa_stream_connect_playback(stream, devID=="default" ? NULL : devID.c_str(), &bufferAttr, (pa_stream_flags_t)streamFlags, NULL, NULL);
|
|
|
|
if(err!=0 && devID!="default"){
|
|
|
|
SetCurrentDevice("default");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK_ERROR(err, "pa_stream_connect_playback");
|
|
|
|
|
|
|
|
while(true){
|
|
|
|
pa_stream_state_t streamState=pa_stream_get_state(stream);
|
|
|
|
if(!PA_STREAM_IS_GOOD(streamState)){
|
|
|
|
LOGE("Error connecting to audio device '%s'", devID.c_str());
|
|
|
|
failed=true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(streamState==PA_STREAM_READY)
|
|
|
|
break;
|
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
isConnected=true;
|
|
|
|
|
|
|
|
if(isPlaying){
|
2018-07-17 18:48:21 +02:00
|
|
|
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
2018-07-01 23:42:49 +02:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 02:31:35 +02:00
|
|
|
bool AudioOutputPulse::EnumerateDevices(std::vector<AudioOutputDevice>& devs){
|
2018-07-17 18:48:21 +02:00
|
|
|
return AudioPulse::DoOneOperation([&](pa_context* ctx){
|
|
|
|
return pa_context_get_sink_info_list(ctx, [](pa_context* ctx, const pa_sink_info* info, int eol, void* userdata){
|
|
|
|
if(eol>0)
|
|
|
|
return;
|
|
|
|
std::vector<AudioOutputDevice>* devs=(std::vector<AudioOutputDevice>*)userdata;
|
|
|
|
AudioOutputDevice dev;
|
|
|
|
dev.id=std::string(info->name);
|
|
|
|
dev.displayName=std::string(info->description);
|
|
|
|
devs->push_back(dev);
|
|
|
|
}, &devs);
|
|
|
|
});
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::StreamStateCallback(pa_stream *s, void* arg) {
|
|
|
|
AudioOutputPulse* self=(AudioOutputPulse*) arg;
|
2017-05-24 02:31:35 +02:00
|
|
|
pa_threaded_mainloop_signal(self->mainloop, 0);
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::StreamWriteCallback(pa_stream *stream, size_t requestedBytes, void *userdata){
|
|
|
|
((AudioOutputPulse*)userdata)->StreamWriteCallback(stream, requestedBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioOutputPulse::StreamWriteCallback(pa_stream *stream, size_t requestedBytes) {
|
2018-08-07 22:10:31 +02:00
|
|
|
//assert(requestedBytes<=sizeof(remainingData));
|
|
|
|
if(requestedBytes>sizeof(remainingData)){
|
|
|
|
requestedBytes=960*2; // force buffer size to 20ms. This probably wrecks the jitter buffer, but still better than crashing
|
|
|
|
}
|
2018-11-21 19:22:31 +01:00
|
|
|
pa_usec_t latency;
|
|
|
|
if(pa_stream_get_latency(stream, &latency, NULL)==0){
|
|
|
|
estimatedDelay=(int32_t)(latency/100);
|
|
|
|
}
|
2018-07-01 23:42:49 +02:00
|
|
|
while(requestedBytes>remainingDataSize){
|
2017-05-12 18:29:35 +02:00
|
|
|
if(isPlaying){
|
2018-07-01 23:42:49 +02:00
|
|
|
InvokeCallback(remainingData+remainingDataSize, 960*2);
|
|
|
|
remainingDataSize+=960*2;
|
2017-05-12 18:29:35 +02:00
|
|
|
}else{
|
2018-07-01 23:42:49 +02:00
|
|
|
memset(remainingData+remainingDataSize, 0, requestedBytes-remainingDataSize);
|
|
|
|
remainingDataSize=requestedBytes;
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-01 23:42:49 +02:00
|
|
|
int err=pa_stream_write(stream, remainingData, requestedBytes, NULL, 0, PA_SEEK_RELATIVE);
|
|
|
|
CHECK_ERROR(err, "pa_stream_write");
|
|
|
|
remainingDataSize-=requestedBytes;
|
|
|
|
if(remainingDataSize>0)
|
|
|
|
memmove(remainingData, remainingData+requestedBytes, remainingDataSize);
|
2017-05-12 18:29:35 +02:00
|
|
|
}
|