Repo created
This commit is contained in:
parent
81b91f4139
commit
f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions
175
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputALSA.cpp
Normal file
175
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputALSA.cpp
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
//
|
||||
// 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
#include "AudioInputALSA.h"
|
||||
#include "../../logging.h"
|
||||
#include "../../VoIPController.h"
|
||||
|
||||
using namespace tgvoip::audio;
|
||||
|
||||
#define BUFFER_SIZE 960
|
||||
#define CHECK_ERROR(res, msg) if(res<0){LOGE(msg ": %s", _snd_strerror(res)); failed=true; return;}
|
||||
#define CHECK_DL_ERROR(res, msg) if(!res){LOGE(msg ": %s", dlerror()); failed=true; return;}
|
||||
#define LOAD_FUNCTION(lib, name, ref) {ref=(typeof(ref))dlsym(lib, name); CHECK_DL_ERROR(ref, "Error getting entry point for " name);}
|
||||
|
||||
AudioInputALSA::AudioInputALSA(std::string devID){
|
||||
isRecording=false;
|
||||
handle=NULL;
|
||||
|
||||
lib=dlopen("libasound.so.2", RTLD_LAZY);
|
||||
if(!lib)
|
||||
lib=dlopen("libasound.so", RTLD_LAZY);
|
||||
if(!lib){
|
||||
LOGE("Error loading libasound: %s", dlerror());
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
|
||||
LOAD_FUNCTION(lib, "snd_pcm_open", _snd_pcm_open);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_set_params", _snd_pcm_set_params);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_close", _snd_pcm_close);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_readi", _snd_pcm_readi);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_recover", _snd_pcm_recover);
|
||||
LOAD_FUNCTION(lib, "snd_strerror", _snd_strerror);
|
||||
|
||||
SetCurrentDevice(devID);
|
||||
}
|
||||
|
||||
AudioInputALSA::~AudioInputALSA(){
|
||||
if(handle)
|
||||
_snd_pcm_close(handle);
|
||||
if(lib)
|
||||
dlclose(lib);
|
||||
}
|
||||
|
||||
void AudioInputALSA::Start(){
|
||||
if(failed || isRecording)
|
||||
return;
|
||||
|
||||
isRecording=true;
|
||||
thread=new Thread(std::bind(&AudioInputALSA::RunThread, this));
|
||||
thread->SetName("AudioInputALSA");
|
||||
thread->Start();
|
||||
}
|
||||
|
||||
void AudioInputALSA::Stop(){
|
||||
if(!isRecording)
|
||||
return;
|
||||
|
||||
isRecording=false;
|
||||
thread->Join();
|
||||
delete thread;
|
||||
thread=NULL;
|
||||
}
|
||||
|
||||
void AudioInputALSA::RunThread(){
|
||||
unsigned char buffer[BUFFER_SIZE*2];
|
||||
snd_pcm_sframes_t frames;
|
||||
while(isRecording){
|
||||
frames=_snd_pcm_readi(handle, buffer, BUFFER_SIZE);
|
||||
if (frames < 0){
|
||||
frames = _snd_pcm_recover(handle, frames, 0);
|
||||
}
|
||||
if (frames < 0) {
|
||||
LOGE("snd_pcm_readi failed: %s\n", _snd_strerror(frames));
|
||||
break;
|
||||
}
|
||||
InvokeCallback(buffer, sizeof(buffer));
|
||||
}
|
||||
}
|
||||
|
||||
void AudioInputALSA::SetCurrentDevice(std::string devID){
|
||||
bool wasRecording=isRecording;
|
||||
isRecording=false;
|
||||
if(handle){
|
||||
thread->Join();
|
||||
_snd_pcm_close(handle);
|
||||
}
|
||||
currentDevice=devID;
|
||||
|
||||
int res=_snd_pcm_open(&handle, devID.c_str(), SND_PCM_STREAM_CAPTURE, 0);
|
||||
if(res<0)
|
||||
res=_snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0);
|
||||
CHECK_ERROR(res, "snd_pcm_open failed");
|
||||
|
||||
res=_snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000);
|
||||
CHECK_ERROR(res, "snd_pcm_set_params failed");
|
||||
|
||||
if(wasRecording){
|
||||
isRecording=true;
|
||||
thread->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void AudioInputALSA::EnumerateDevices(std::vector<AudioInputDevice>& devs){
|
||||
int (*_snd_device_name_hint)(int card, const char* iface, void*** hints);
|
||||
char* (*_snd_device_name_get_hint)(const void* hint, const char* id);
|
||||
int (*_snd_device_name_free_hint)(void** hinst);
|
||||
void* lib=dlopen("libasound.so.2", RTLD_LAZY);
|
||||
if(!lib)
|
||||
dlopen("libasound.so", RTLD_LAZY);
|
||||
if(!lib)
|
||||
return;
|
||||
|
||||
_snd_device_name_hint=(typeof(_snd_device_name_hint))dlsym(lib, "snd_device_name_hint");
|
||||
_snd_device_name_get_hint=(typeof(_snd_device_name_get_hint))dlsym(lib, "snd_device_name_get_hint");
|
||||
_snd_device_name_free_hint=(typeof(_snd_device_name_free_hint))dlsym(lib, "snd_device_name_free_hint");
|
||||
|
||||
if(!_snd_device_name_hint || !_snd_device_name_get_hint || !_snd_device_name_free_hint){
|
||||
dlclose(lib);
|
||||
return;
|
||||
}
|
||||
|
||||
char** hints;
|
||||
int err=_snd_device_name_hint(-1, "pcm", (void***)&hints);
|
||||
if(err!=0){
|
||||
dlclose(lib);
|
||||
return;
|
||||
}
|
||||
|
||||
char** n=hints;
|
||||
while(*n){
|
||||
char* name=_snd_device_name_get_hint(*n, "NAME");
|
||||
if(strncmp(name, "surround", 8)==0 || strcmp(name, "null")==0){
|
||||
free(name);
|
||||
n++;
|
||||
continue;
|
||||
}
|
||||
char* desc=_snd_device_name_get_hint(*n, "DESC");
|
||||
char* ioid=_snd_device_name_get_hint(*n, "IOID");
|
||||
if(!ioid || strcmp(ioid, "Input")==0){
|
||||
char* l1=strtok(desc, "\n");
|
||||
char* l2=strtok(NULL, "\n");
|
||||
char* tmp=strtok(l1, ",");
|
||||
char* actualName=tmp;
|
||||
while((tmp=strtok(NULL, ","))){
|
||||
actualName=tmp;
|
||||
}
|
||||
if(actualName[0]==' ')
|
||||
actualName++;
|
||||
AudioInputDevice dev;
|
||||
dev.id=std::string(name);
|
||||
if(l2){
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "%s (%s)", actualName, l2);
|
||||
dev.displayName=std::string(buf);
|
||||
}else{
|
||||
dev.displayName=std::string(actualName);
|
||||
}
|
||||
devs.push_back(dev);
|
||||
}
|
||||
free(name);
|
||||
free(desc);
|
||||
free(ioid);
|
||||
n++;
|
||||
}
|
||||
|
||||
dlclose(lib);
|
||||
}
|
||||
46
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputALSA.h
Normal file
46
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputALSA.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_AUDIOINPUTALSA_H
|
||||
#define LIBTGVOIP_AUDIOINPUTALSA_H
|
||||
|
||||
#include "../../audio/AudioInput.h"
|
||||
#include "../../threading.h"
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
namespace tgvoip{
|
||||
namespace audio{
|
||||
|
||||
class AudioInputALSA : public AudioInput{
|
||||
|
||||
public:
|
||||
AudioInputALSA(std::string devID);
|
||||
virtual ~AudioInputALSA();
|
||||
virtual void Start();
|
||||
virtual void Stop();
|
||||
virtual void SetCurrentDevice(std::string devID);
|
||||
static void EnumerateDevices(std::vector<AudioInputDevice>& devs);
|
||||
|
||||
private:
|
||||
void RunThread();
|
||||
|
||||
int (*_snd_pcm_open)(snd_pcm_t** pcm, const char* name, snd_pcm_stream_t stream, int mode);
|
||||
int (*_snd_pcm_set_params)(snd_pcm_t* pcm, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int channels, unsigned int rate, int soft_resample, unsigned int latency);
|
||||
int (*_snd_pcm_close)(snd_pcm_t* pcm);
|
||||
snd_pcm_sframes_t (*_snd_pcm_readi)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
|
||||
int (*_snd_pcm_recover)(snd_pcm_t* pcm, int err, int silent);
|
||||
const char* (*_snd_strerror)(int errnum);
|
||||
void* lib;
|
||||
|
||||
snd_pcm_t* handle;
|
||||
Thread* thread;
|
||||
bool isRecording;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LIBTGVOIP_AUDIOINPUTALSA_H
|
||||
204
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputPulse.cpp
Normal file
204
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputPulse.cpp
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
//
|
||||
// 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 "AudioInputPulse.h"
|
||||
#include "../../logging.h"
|
||||
#include "../../VoIPController.h"
|
||||
#include "AudioPulse.h"
|
||||
#include "PulseFunctions.h"
|
||||
#if !defined(__GLIBC__)
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
#define BUFFER_SIZE 960
|
||||
#define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
|
||||
|
||||
using namespace tgvoip::audio;
|
||||
|
||||
AudioInputPulse::AudioInputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID){
|
||||
isRecording=false;
|
||||
isConnected=false;
|
||||
didStart=false;
|
||||
|
||||
this->mainloop=mainloop;
|
||||
this->context=context;
|
||||
stream=NULL;
|
||||
remainingDataSize=0;
|
||||
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
|
||||
stream=CreateAndInitStream();
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
isLocked=false;
|
||||
if(!stream){
|
||||
return;
|
||||
}
|
||||
|
||||
SetCurrentDevice(devID);
|
||||
}
|
||||
|
||||
AudioInputPulse::~AudioInputPulse(){
|
||||
if(stream){
|
||||
pa_stream_disconnect(stream);
|
||||
pa_stream_unref(stream);
|
||||
}
|
||||
}
|
||||
|
||||
pa_stream* AudioInputPulse::CreateAndInitStream(){
|
||||
pa_sample_spec sampleSpec{
|
||||
.format=PA_SAMPLE_S16LE,
|
||||
.rate=48000,
|
||||
.channels=1
|
||||
};
|
||||
pa_proplist* proplist=pa_proplist_new();
|
||||
pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters
|
||||
pa_stream* stream=pa_stream_new_with_proplist(context, "libtgvoip capture", &sampleSpec, NULL, proplist);
|
||||
pa_proplist_free(proplist);
|
||||
if(!stream){
|
||||
LOGE("Error initializing PulseAudio (pa_stream_new)");
|
||||
failed=true;
|
||||
return NULL;
|
||||
}
|
||||
pa_stream_set_state_callback(stream, AudioInputPulse::StreamStateCallback, this);
|
||||
pa_stream_set_read_callback(stream, AudioInputPulse::StreamReadCallback, this);
|
||||
return stream;
|
||||
}
|
||||
|
||||
void AudioInputPulse::Start(){
|
||||
if(failed || isRecording)
|
||||
return;
|
||||
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
isRecording=true;
|
||||
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
void AudioInputPulse::Stop(){
|
||||
if(!isRecording)
|
||||
return;
|
||||
|
||||
isRecording=false;
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
pa_operation_unref(pa_stream_cork(stream, 1, NULL, NULL));
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
bool AudioInputPulse::IsRecording(){
|
||||
return isRecording;
|
||||
}
|
||||
|
||||
void AudioInputPulse::SetCurrentDevice(std::string devID){
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
currentDevice=devID;
|
||||
if(isRecording && isConnected){
|
||||
pa_stream_disconnect(stream);
|
||||
pa_stream_unref(stream);
|
||||
isConnected=false;
|
||||
stream=CreateAndInitStream();
|
||||
}
|
||||
|
||||
pa_buffer_attr bufferAttr={
|
||||
.maxlength=(uint32_t)-1,
|
||||
.tlength=(uint32_t)-1,
|
||||
.prebuf=(uint32_t)-1,
|
||||
.minreq=(uint32_t)-1,
|
||||
.fragsize=960*2
|
||||
};
|
||||
int streamFlags=PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY;
|
||||
|
||||
int err=pa_stream_connect_record(stream, devID=="default" ? NULL : devID.c_str(), &bufferAttr, (pa_stream_flags_t)streamFlags);
|
||||
if(err!=0){
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
/*if(devID!="default"){
|
||||
SetCurrentDevice("default");
|
||||
return;
|
||||
}*/
|
||||
}
|
||||
CHECK_ERROR(err, "pa_stream_connect_record");
|
||||
|
||||
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());
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
if(streamState==PA_STREAM_READY)
|
||||
break;
|
||||
pa_threaded_mainloop_wait(mainloop);
|
||||
}
|
||||
|
||||
isConnected=true;
|
||||
|
||||
if(isRecording){
|
||||
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
||||
}
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
bool AudioInputPulse::EnumerateDevices(std::vector<AudioInputDevice>& devs){
|
||||
return AudioPulse::DoOneOperation([&](pa_context* ctx){
|
||||
return pa_context_get_source_info_list(ctx, [](pa_context* ctx, const pa_source_info* info, int eol, void* userdata){
|
||||
if(eol>0)
|
||||
return;
|
||||
std::vector<AudioInputDevice>* devs=(std::vector<AudioInputDevice>*)userdata;
|
||||
AudioInputDevice dev;
|
||||
dev.id=std::string(info->name);
|
||||
dev.displayName=std::string(info->description);
|
||||
devs->push_back(dev);
|
||||
}, &devs);
|
||||
});
|
||||
}
|
||||
|
||||
void AudioInputPulse::StreamStateCallback(pa_stream *s, void* arg) {
|
||||
AudioInputPulse* self=(AudioInputPulse*) arg;
|
||||
pa_threaded_mainloop_signal(self->mainloop, 0);
|
||||
}
|
||||
|
||||
void AudioInputPulse::StreamReadCallback(pa_stream *stream, size_t requestedBytes, void *userdata){
|
||||
((AudioInputPulse*)userdata)->StreamReadCallback(stream, requestedBytes);
|
||||
}
|
||||
|
||||
void AudioInputPulse::StreamReadCallback(pa_stream *stream, size_t requestedBytes) {
|
||||
size_t bytesRemaining = requestedBytes;
|
||||
uint8_t *buffer = NULL;
|
||||
pa_usec_t latency;
|
||||
if(pa_stream_get_latency(stream, &latency, NULL)==0){
|
||||
estimatedDelay=(int32_t)(latency/100);
|
||||
}
|
||||
while (bytesRemaining > 0) {
|
||||
size_t bytesToFill = 102400;
|
||||
|
||||
if (bytesToFill > bytesRemaining) bytesToFill = bytesRemaining;
|
||||
|
||||
int err=pa_stream_peek(stream, (const void**) &buffer, &bytesToFill);
|
||||
CHECK_ERROR(err, "pa_stream_peek");
|
||||
|
||||
if(isRecording){
|
||||
if(remainingDataSize+bytesToFill>sizeof(remainingData)){
|
||||
LOGE("Capture buffer is too big (%d)", (int)bytesToFill);
|
||||
}
|
||||
memcpy(remainingData+remainingDataSize, buffer, bytesToFill);
|
||||
remainingDataSize+=bytesToFill;
|
||||
while(remainingDataSize>=960*2){
|
||||
InvokeCallback(remainingData, 960*2);
|
||||
memmove(remainingData, remainingData+960*2, remainingDataSize-960*2);
|
||||
remainingDataSize-=960*2;
|
||||
}
|
||||
}
|
||||
|
||||
err=pa_stream_drop(stream);
|
||||
CHECK_ERROR(err, "pa_stream_drop");
|
||||
|
||||
bytesRemaining -= bytesToFill;
|
||||
}
|
||||
}
|
||||
52
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputPulse.h
Normal file
52
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioInputPulse.h
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_AUDIOINPUTPULSE_H
|
||||
#define LIBTGVOIP_AUDIOINPUTPULSE_H
|
||||
|
||||
#include "../../audio/AudioInput.h"
|
||||
#include "../../threading.h"
|
||||
#include <pulse/pulseaudio.h>
|
||||
|
||||
#define DECLARE_DL_FUNCTION(name) typeof(name)* _import_##name
|
||||
|
||||
namespace tgvoip{
|
||||
namespace audio{
|
||||
|
||||
class AudioInputPulse : public AudioInput{
|
||||
public:
|
||||
AudioInputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID);
|
||||
virtual ~AudioInputPulse();
|
||||
virtual void Start();
|
||||
virtual void Stop();
|
||||
virtual bool IsRecording();
|
||||
virtual void SetCurrentDevice(std::string devID);
|
||||
static bool EnumerateDevices(std::vector<AudioInputDevice>& devs);
|
||||
|
||||
private:
|
||||
static void StreamStateCallback(pa_stream* s, void* arg);
|
||||
static void StreamReadCallback(pa_stream* stream, size_t requested_bytes, void* userdata);
|
||||
void StreamReadCallback(pa_stream* stream, size_t requestedBytes);
|
||||
pa_stream* CreateAndInitStream();
|
||||
|
||||
pa_threaded_mainloop* mainloop;
|
||||
pa_context* context;
|
||||
pa_stream* stream;
|
||||
|
||||
bool isRecording;
|
||||
bool isConnected;
|
||||
bool didStart;
|
||||
bool isLocked;
|
||||
unsigned char remainingData[960*8*2];
|
||||
size_t remainingDataSize;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#undef DECLARE_DL_FUNCTION
|
||||
|
||||
#endif //LIBTGVOIP_AUDIOINPUTPULSE_H
|
||||
177
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputALSA.cpp
Normal file
177
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputALSA.cpp
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
//
|
||||
// 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 "AudioOutputALSA.h"
|
||||
#include "../../logging.h"
|
||||
#include "../../VoIPController.h"
|
||||
|
||||
#define BUFFER_SIZE 960
|
||||
#define CHECK_ERROR(res, msg) if(res<0){LOGE(msg ": %s", _snd_strerror(res)); failed=true; return;}
|
||||
#define CHECK_DL_ERROR(res, msg) if(!res){LOGE(msg ": %s", dlerror()); failed=true; return;}
|
||||
#define LOAD_FUNCTION(lib, name, ref) {ref=(typeof(ref))dlsym(lib, name); CHECK_DL_ERROR(ref, "Error getting entry point for " name);}
|
||||
|
||||
using namespace tgvoip::audio;
|
||||
|
||||
AudioOutputALSA::AudioOutputALSA(std::string devID){
|
||||
isPlaying=false;
|
||||
handle=NULL;
|
||||
|
||||
lib=dlopen("libasound.so.2", RTLD_LAZY);
|
||||
if(!lib)
|
||||
lib=dlopen("libasound.so", RTLD_LAZY);
|
||||
if(!lib){
|
||||
LOGE("Error loading libasound: %s", dlerror());
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
|
||||
LOAD_FUNCTION(lib, "snd_pcm_open", _snd_pcm_open);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_set_params", _snd_pcm_set_params);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_close", _snd_pcm_close);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_writei", _snd_pcm_writei);
|
||||
LOAD_FUNCTION(lib, "snd_pcm_recover", _snd_pcm_recover);
|
||||
LOAD_FUNCTION(lib, "snd_strerror", _snd_strerror);
|
||||
|
||||
SetCurrentDevice(devID);
|
||||
}
|
||||
|
||||
AudioOutputALSA::~AudioOutputALSA(){
|
||||
if(handle)
|
||||
_snd_pcm_close(handle);
|
||||
if(lib)
|
||||
dlclose(lib);
|
||||
}
|
||||
|
||||
void AudioOutputALSA::Start(){
|
||||
if(failed || isPlaying)
|
||||
return;
|
||||
|
||||
isPlaying=true;
|
||||
thread=new Thread(std::bind(&AudioOutputALSA::RunThread, this));
|
||||
thread->SetName("AudioOutputALSA");
|
||||
thread->Start();
|
||||
}
|
||||
|
||||
void AudioOutputALSA::Stop(){
|
||||
if(!isPlaying)
|
||||
return;
|
||||
|
||||
isPlaying=false;
|
||||
thread->Join();
|
||||
delete thread;
|
||||
thread=NULL;
|
||||
}
|
||||
|
||||
bool AudioOutputALSA::IsPlaying(){
|
||||
return isPlaying;
|
||||
}
|
||||
void AudioOutputALSA::RunThread(){
|
||||
unsigned char buffer[BUFFER_SIZE*2];
|
||||
snd_pcm_sframes_t frames;
|
||||
while(isPlaying){
|
||||
InvokeCallback(buffer, sizeof(buffer));
|
||||
frames=_snd_pcm_writei(handle, buffer, BUFFER_SIZE);
|
||||
if (frames < 0){
|
||||
frames = _snd_pcm_recover(handle, frames, 0);
|
||||
}
|
||||
if (frames < 0) {
|
||||
LOGE("snd_pcm_writei failed: %s\n", _snd_strerror(frames));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AudioOutputALSA::SetCurrentDevice(std::string devID){
|
||||
bool wasPlaying=isPlaying;
|
||||
isPlaying=false;
|
||||
if(handle){
|
||||
thread->Join();
|
||||
_snd_pcm_close(handle);
|
||||
}
|
||||
currentDevice=devID;
|
||||
|
||||
int res=_snd_pcm_open(&handle, devID.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
|
||||
if(res<0)
|
||||
res=_snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
|
||||
CHECK_ERROR(res, "snd_pcm_open failed");
|
||||
|
||||
res=_snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000);
|
||||
CHECK_ERROR(res, "snd_pcm_set_params failed");
|
||||
|
||||
if(wasPlaying){
|
||||
isPlaying=true;
|
||||
thread->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void AudioOutputALSA::EnumerateDevices(std::vector<AudioOutputDevice>& devs){
|
||||
int (*_snd_device_name_hint)(int card, const char* iface, void*** hints);
|
||||
char* (*_snd_device_name_get_hint)(const void* hint, const char* id);
|
||||
int (*_snd_device_name_free_hint)(void** hinst);
|
||||
void* lib=dlopen("libasound.so.2", RTLD_LAZY);
|
||||
if(!lib)
|
||||
dlopen("libasound.so", RTLD_LAZY);
|
||||
if(!lib)
|
||||
return;
|
||||
|
||||
_snd_device_name_hint=(typeof(_snd_device_name_hint))dlsym(lib, "snd_device_name_hint");
|
||||
_snd_device_name_get_hint=(typeof(_snd_device_name_get_hint))dlsym(lib, "snd_device_name_get_hint");
|
||||
_snd_device_name_free_hint=(typeof(_snd_device_name_free_hint))dlsym(lib, "snd_device_name_free_hint");
|
||||
|
||||
if(!_snd_device_name_hint || !_snd_device_name_get_hint || !_snd_device_name_free_hint){
|
||||
dlclose(lib);
|
||||
return;
|
||||
}
|
||||
|
||||
char** hints;
|
||||
int err=_snd_device_name_hint(-1, "pcm", (void***)&hints);
|
||||
if(err!=0){
|
||||
dlclose(lib);
|
||||
return;
|
||||
}
|
||||
|
||||
char** n=hints;
|
||||
while(*n){
|
||||
char* name=_snd_device_name_get_hint(*n, "NAME");
|
||||
if(strncmp(name, "surround", 8)==0 || strcmp(name, "null")==0){
|
||||
free(name);
|
||||
n++;
|
||||
continue;
|
||||
}
|
||||
char* desc=_snd_device_name_get_hint(*n, "DESC");
|
||||
char* ioid=_snd_device_name_get_hint(*n, "IOID");
|
||||
if(!ioid || strcmp(ioid, "Output")==0){
|
||||
char* l1=strtok(desc, "\n");
|
||||
char* l2=strtok(NULL, "\n");
|
||||
char* tmp=strtok(l1, ",");
|
||||
char* actualName=tmp;
|
||||
while((tmp=strtok(NULL, ","))){
|
||||
actualName=tmp;
|
||||
}
|
||||
if(actualName[0]==' ')
|
||||
actualName++;
|
||||
AudioOutputDevice dev;
|
||||
dev.id=std::string(name);
|
||||
if(l2){
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "%s (%s)", actualName, l2);
|
||||
dev.displayName=std::string(buf);
|
||||
}else{
|
||||
dev.displayName=std::string(actualName);
|
||||
}
|
||||
devs.push_back(dev);
|
||||
}
|
||||
free(name);
|
||||
free(desc);
|
||||
free(ioid);
|
||||
n++;
|
||||
}
|
||||
|
||||
dlclose(lib);
|
||||
}
|
||||
46
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputALSA.h
Normal file
46
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputALSA.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_AUDIOOUTPUTALSA_H
|
||||
#define LIBTGVOIP_AUDIOOUTPUTALSA_H
|
||||
|
||||
#include "../../audio/AudioOutput.h"
|
||||
#include "../../threading.h"
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
namespace tgvoip{
|
||||
namespace audio{
|
||||
|
||||
class AudioOutputALSA : public AudioOutput{
|
||||
public:
|
||||
AudioOutputALSA(std::string devID);
|
||||
virtual ~AudioOutputALSA();
|
||||
virtual void Start();
|
||||
virtual void Stop();
|
||||
virtual bool IsPlaying();
|
||||
virtual void SetCurrentDevice(std::string devID);
|
||||
static void EnumerateDevices(std::vector<AudioOutputDevice>& devs);
|
||||
|
||||
private:
|
||||
void RunThread();
|
||||
|
||||
int (*_snd_pcm_open)(snd_pcm_t** pcm, const char* name, snd_pcm_stream_t stream, int mode);
|
||||
int (*_snd_pcm_set_params)(snd_pcm_t* pcm, snd_pcm_format_t format, snd_pcm_access_t access, unsigned int channels, unsigned int rate, int soft_resample, unsigned int latency);
|
||||
int (*_snd_pcm_close)(snd_pcm_t* pcm);
|
||||
snd_pcm_sframes_t (*_snd_pcm_writei)(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
|
||||
int (*_snd_pcm_recover)(snd_pcm_t* pcm, int err, int silent);
|
||||
const char* (*_snd_strerror)(int errnum);
|
||||
void* lib;
|
||||
|
||||
snd_pcm_t* handle;
|
||||
Thread* thread;
|
||||
bool isPlaying;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LIBTGVOIP_AUDIOOUTPUTALSA_H
|
||||
187
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputPulse.cpp
Normal file
187
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputPulse.cpp
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
//
|
||||
// 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"
|
||||
#include "AudioPulse.h"
|
||||
#include "PulseFunctions.h"
|
||||
#if !defined(__GLIBC__)
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
#define BUFFER_SIZE 960
|
||||
#define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
|
||||
|
||||
using namespace tgvoip;
|
||||
using namespace tgvoip::audio;
|
||||
|
||||
AudioOutputPulse::AudioOutputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID){
|
||||
isPlaying=false;
|
||||
isConnected=false;
|
||||
didStart=false;
|
||||
isLocked=false;
|
||||
|
||||
this->mainloop=mainloop;
|
||||
this->context=context;
|
||||
stream=NULL;
|
||||
remainingDataSize=0;
|
||||
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
stream=CreateAndInitStream();
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
|
||||
SetCurrentDevice(devID);
|
||||
}
|
||||
|
||||
AudioOutputPulse::~AudioOutputPulse(){
|
||||
if(stream){
|
||||
pa_stream_disconnect(stream);
|
||||
pa_stream_unref(stream);
|
||||
}
|
||||
}
|
||||
|
||||
pa_stream* AudioOutputPulse::CreateAndInitStream(){
|
||||
pa_sample_spec sampleSpec{
|
||||
.format=PA_SAMPLE_S16LE,
|
||||
.rate=48000,
|
||||
.channels=1
|
||||
};
|
||||
pa_proplist* proplist=pa_proplist_new();
|
||||
pa_proplist_sets(proplist, PA_PROP_FILTER_APPLY, ""); // according to PA sources, this disables any possible filters
|
||||
pa_stream* stream=pa_stream_new_with_proplist(context, "libtgvoip playback", &sampleSpec, NULL, proplist);
|
||||
pa_proplist_free(proplist);
|
||||
if(!stream){
|
||||
LOGE("Error initializing PulseAudio (pa_stream_new)");
|
||||
failed=true;
|
||||
return NULL;
|
||||
}
|
||||
pa_stream_set_state_callback(stream, AudioOutputPulse::StreamStateCallback, this);
|
||||
pa_stream_set_write_callback(stream, AudioOutputPulse::StreamWriteCallback, this);
|
||||
return stream;
|
||||
}
|
||||
|
||||
void AudioOutputPulse::Start(){
|
||||
if(failed || isPlaying)
|
||||
return;
|
||||
|
||||
isPlaying=true;
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
void AudioOutputPulse::Stop(){
|
||||
if(!isPlaying)
|
||||
return;
|
||||
|
||||
isPlaying=false;
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
pa_operation_unref(pa_stream_cork(stream, 1, NULL, NULL));
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
bool AudioOutputPulse::IsPlaying(){
|
||||
return isPlaying;
|
||||
}
|
||||
|
||||
void AudioOutputPulse::SetCurrentDevice(std::string devID){
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
currentDevice=devID;
|
||||
if(isPlaying && isConnected){
|
||||
pa_stream_disconnect(stream);
|
||||
pa_stream_unref(stream);
|
||||
isConnected=false;
|
||||
stream=CreateAndInitStream();
|
||||
}
|
||||
|
||||
pa_buffer_attr bufferAttr={
|
||||
.maxlength=(uint32_t)-1,
|
||||
.tlength=960*2,
|
||||
.prebuf=(uint32_t)-1,
|
||||
.minreq=(uint32_t)-1,
|
||||
.fragsize=(uint32_t)-1
|
||||
};
|
||||
int streamFlags=PA_STREAM_START_CORKED | PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY;
|
||||
|
||||
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){
|
||||
pa_operation_unref(pa_stream_cork(stream, 0, NULL, NULL));
|
||||
}
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
}
|
||||
|
||||
bool AudioOutputPulse::EnumerateDevices(std::vector<AudioOutputDevice>& devs){
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
void AudioOutputPulse::StreamStateCallback(pa_stream *s, void* arg) {
|
||||
AudioOutputPulse* self=(AudioOutputPulse*) arg;
|
||||
pa_threaded_mainloop_signal(self->mainloop, 0);
|
||||
}
|
||||
|
||||
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) {
|
||||
//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
|
||||
}
|
||||
pa_usec_t latency;
|
||||
if(pa_stream_get_latency(stream, &latency, NULL)==0){
|
||||
estimatedDelay=(int32_t)(latency/100);
|
||||
}
|
||||
while(requestedBytes>remainingDataSize){
|
||||
if(isPlaying){
|
||||
InvokeCallback(remainingData+remainingDataSize, 960*2);
|
||||
remainingDataSize+=960*2;
|
||||
}else{
|
||||
memset(remainingData+remainingDataSize, 0, requestedBytes-remainingDataSize);
|
||||
remainingDataSize=requestedBytes;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
48
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputPulse.h
Normal file
48
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioOutputPulse.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_AUDIOOUTPUTPULSE_H
|
||||
#define LIBTGVOIP_AUDIOOUTPUTPULSE_H
|
||||
|
||||
#include "../../audio/AudioOutput.h"
|
||||
#include "../../threading.h"
|
||||
#include <pulse/pulseaudio.h>
|
||||
|
||||
namespace tgvoip{
|
||||
namespace audio{
|
||||
|
||||
class AudioOutputPulse : public AudioOutput{
|
||||
public:
|
||||
AudioOutputPulse(pa_context* context, pa_threaded_mainloop* mainloop, std::string devID);
|
||||
virtual ~AudioOutputPulse();
|
||||
virtual void Start();
|
||||
virtual void Stop();
|
||||
virtual bool IsPlaying();
|
||||
virtual void SetCurrentDevice(std::string devID);
|
||||
static bool EnumerateDevices(std::vector<AudioOutputDevice>& devs);
|
||||
|
||||
private:
|
||||
static void StreamStateCallback(pa_stream* s, void* arg);
|
||||
static void StreamWriteCallback(pa_stream* stream, size_t requested_bytes, void* userdata);
|
||||
void StreamWriteCallback(pa_stream* stream, size_t requestedBytes);
|
||||
pa_stream* CreateAndInitStream();
|
||||
|
||||
pa_threaded_mainloop* mainloop;
|
||||
pa_context* context;
|
||||
pa_stream* stream;
|
||||
|
||||
bool isPlaying;
|
||||
bool isConnected;
|
||||
bool didStart;
|
||||
bool isLocked;
|
||||
unsigned char remainingData[960*8*2];
|
||||
size_t remainingDataSize;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LIBTGVOIP_AUDIOOUTPUTPULSE_H
|
||||
288
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioPulse.cpp
Normal file
288
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioPulse.cpp
Normal file
|
|
@ -0,0 +1,288 @@
|
|||
//
|
||||
// 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 "AudioPulse.h"
|
||||
#include <dlfcn.h>
|
||||
#include "../../logging.h"
|
||||
|
||||
#define DECLARE_DL_FUNCTION(name) typeof(name)* AudioPulse::_import_##name=NULL
|
||||
#define CHECK_DL_ERROR(res, msg) if(!res){LOGE(msg ": %s", dlerror()); return false;}
|
||||
#define LOAD_DL_FUNCTION(name) {_import_##name=(typeof(_import_##name))dlsym(lib, #name); CHECK_DL_ERROR(_import_##name, "Error getting entry point for " #name);}
|
||||
#define CHECK_ERROR(res, msg) if(res!=0){LOGE(msg " failed: %s", pa_strerror(res)); failed=true; return;}
|
||||
|
||||
using namespace tgvoip;
|
||||
using namespace tgvoip::audio;
|
||||
|
||||
bool AudioPulse::loaded=false;
|
||||
void* AudioPulse::lib=NULL;
|
||||
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_new);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_get_api);
|
||||
DECLARE_DL_FUNCTION(pa_context_new);
|
||||
DECLARE_DL_FUNCTION(pa_context_new_with_proplist);
|
||||
DECLARE_DL_FUNCTION(pa_context_set_state_callback);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_lock);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_unlock);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_start);
|
||||
DECLARE_DL_FUNCTION(pa_context_connect);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_state);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_wait);
|
||||
DECLARE_DL_FUNCTION(pa_stream_new_with_proplist);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_state_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_write_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_connect_playback);
|
||||
DECLARE_DL_FUNCTION(pa_operation_unref);
|
||||
DECLARE_DL_FUNCTION(pa_stream_cork);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_stop);
|
||||
DECLARE_DL_FUNCTION(pa_stream_disconnect);
|
||||
DECLARE_DL_FUNCTION(pa_stream_unref);
|
||||
DECLARE_DL_FUNCTION(pa_context_disconnect);
|
||||
DECLARE_DL_FUNCTION(pa_context_unref);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_free);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_signal);
|
||||
DECLARE_DL_FUNCTION(pa_stream_begin_write);
|
||||
DECLARE_DL_FUNCTION(pa_stream_write);
|
||||
DECLARE_DL_FUNCTION(pa_stream_get_state);
|
||||
DECLARE_DL_FUNCTION(pa_strerror);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_read_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_connect_record);
|
||||
DECLARE_DL_FUNCTION(pa_stream_peek);
|
||||
DECLARE_DL_FUNCTION(pa_stream_drop);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_new);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_get_api);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_iterate);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_free);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_sink_info_list);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_source_info_list);
|
||||
DECLARE_DL_FUNCTION(pa_operation_get_state);
|
||||
DECLARE_DL_FUNCTION(pa_proplist_new);
|
||||
DECLARE_DL_FUNCTION(pa_proplist_sets);
|
||||
DECLARE_DL_FUNCTION(pa_proplist_free);
|
||||
DECLARE_DL_FUNCTION(pa_stream_get_latency);
|
||||
|
||||
#include "PulseFunctions.h"
|
||||
|
||||
bool AudioPulse::Load(){
|
||||
if(loaded)
|
||||
return true;
|
||||
|
||||
lib=dlopen("libpulse.so.0", RTLD_LAZY);
|
||||
if(!lib)
|
||||
lib=dlopen("libpulse.so", RTLD_LAZY);
|
||||
if(!lib){
|
||||
LOGE("Error loading libpulse: %s", dlerror());
|
||||
return false;
|
||||
}
|
||||
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_new);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_get_api);
|
||||
LOAD_DL_FUNCTION(pa_context_new);
|
||||
LOAD_DL_FUNCTION(pa_context_new_with_proplist);
|
||||
LOAD_DL_FUNCTION(pa_context_set_state_callback);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_lock);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_unlock);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_start);
|
||||
LOAD_DL_FUNCTION(pa_context_connect);
|
||||
LOAD_DL_FUNCTION(pa_context_get_state);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_wait);
|
||||
LOAD_DL_FUNCTION(pa_stream_new_with_proplist);
|
||||
LOAD_DL_FUNCTION(pa_stream_set_state_callback);
|
||||
LOAD_DL_FUNCTION(pa_stream_set_write_callback);
|
||||
LOAD_DL_FUNCTION(pa_stream_connect_playback);
|
||||
LOAD_DL_FUNCTION(pa_operation_unref);
|
||||
LOAD_DL_FUNCTION(pa_stream_cork);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_stop);
|
||||
LOAD_DL_FUNCTION(pa_stream_disconnect);
|
||||
LOAD_DL_FUNCTION(pa_stream_unref);
|
||||
LOAD_DL_FUNCTION(pa_context_disconnect);
|
||||
LOAD_DL_FUNCTION(pa_context_unref);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_free);
|
||||
LOAD_DL_FUNCTION(pa_threaded_mainloop_signal);
|
||||
LOAD_DL_FUNCTION(pa_stream_begin_write);
|
||||
LOAD_DL_FUNCTION(pa_stream_write);
|
||||
LOAD_DL_FUNCTION(pa_stream_get_state);
|
||||
LOAD_DL_FUNCTION(pa_strerror);
|
||||
LOAD_DL_FUNCTION(pa_stream_set_read_callback);
|
||||
LOAD_DL_FUNCTION(pa_stream_connect_record);
|
||||
LOAD_DL_FUNCTION(pa_stream_peek);
|
||||
LOAD_DL_FUNCTION(pa_stream_drop);
|
||||
LOAD_DL_FUNCTION(pa_mainloop_new);
|
||||
LOAD_DL_FUNCTION(pa_mainloop_get_api);
|
||||
LOAD_DL_FUNCTION(pa_mainloop_iterate);
|
||||
LOAD_DL_FUNCTION(pa_mainloop_free);
|
||||
LOAD_DL_FUNCTION(pa_context_get_sink_info_list);
|
||||
LOAD_DL_FUNCTION(pa_context_get_source_info_list);
|
||||
LOAD_DL_FUNCTION(pa_operation_get_state);
|
||||
LOAD_DL_FUNCTION(pa_proplist_new);
|
||||
LOAD_DL_FUNCTION(pa_proplist_sets);
|
||||
LOAD_DL_FUNCTION(pa_proplist_free);
|
||||
LOAD_DL_FUNCTION(pa_stream_get_latency);
|
||||
|
||||
loaded=true;
|
||||
return true;
|
||||
}
|
||||
|
||||
AudioPulse::AudioPulse(std::string inputDevice, std::string outputDevice){
|
||||
if(!Load()){
|
||||
failed=true;
|
||||
LOGE("Failed to load libpulse");
|
||||
return;
|
||||
}
|
||||
|
||||
mainloop=pa_threaded_mainloop_new();
|
||||
if(!mainloop){
|
||||
LOGE("Error initializing PulseAudio (pa_threaded_mainloop_new)");
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
mainloopApi=pa_threaded_mainloop_get_api(mainloop);
|
||||
#ifndef MAXPATHLEN
|
||||
char exeName[20];
|
||||
#else
|
||||
char exePath[MAXPATHLEN];
|
||||
char exeName[MAXPATHLEN];
|
||||
ssize_t lres=readlink("/proc/self/exe", exePath, sizeof(exePath));
|
||||
if(lres==-1)
|
||||
lres=readlink("/proc/curproc/file", exePath, sizeof(exePath));
|
||||
if(lres==-1)
|
||||
lres=readlink("/proc/curproc/exe", exePath, sizeof(exePath));
|
||||
if(lres>0){
|
||||
strcpy(exeName, basename(exePath));
|
||||
}else
|
||||
#endif
|
||||
{
|
||||
snprintf(exeName, sizeof(exeName), "Process %d", getpid());
|
||||
}
|
||||
pa_proplist* proplist=pa_proplist_new();
|
||||
pa_proplist_sets(proplist, PA_PROP_MEDIA_ROLE, "phone");
|
||||
context=pa_context_new_with_proplist(mainloopApi, exeName, proplist);
|
||||
pa_proplist_free(proplist);
|
||||
if(!context){
|
||||
LOGE("Error initializing PulseAudio (pa_context_new)");
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
pa_context_set_state_callback(context, [](pa_context* context, void* arg){
|
||||
AudioPulse* self=reinterpret_cast<AudioPulse*>(arg);
|
||||
pa_threaded_mainloop_signal(self->mainloop, 0);
|
||||
}, this);
|
||||
pa_threaded_mainloop_lock(mainloop);
|
||||
isLocked=true;
|
||||
int err=pa_threaded_mainloop_start(mainloop);
|
||||
CHECK_ERROR(err, "pa_threaded_mainloop_start");
|
||||
didStart=true;
|
||||
|
||||
err=pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
|
||||
CHECK_ERROR(err, "pa_context_connect");
|
||||
|
||||
while(true){
|
||||
pa_context_state_t contextState=pa_context_get_state(context);
|
||||
if(!PA_CONTEXT_IS_GOOD(contextState)){
|
||||
LOGE("Error initializing PulseAudio (PA_CONTEXT_IS_GOOD)");
|
||||
failed=true;
|
||||
return;
|
||||
}
|
||||
if(contextState==PA_CONTEXT_READY)
|
||||
break;
|
||||
pa_threaded_mainloop_wait(mainloop);
|
||||
}
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
isLocked=false;
|
||||
|
||||
output=new AudioOutputPulse(context, mainloop, outputDevice);
|
||||
input=new AudioInputPulse(context, mainloop, inputDevice);
|
||||
}
|
||||
|
||||
AudioPulse::~AudioPulse(){
|
||||
if(mainloop && didStart){
|
||||
if(isLocked)
|
||||
pa_threaded_mainloop_unlock(mainloop);
|
||||
pa_threaded_mainloop_stop(mainloop);
|
||||
}
|
||||
|
||||
if(input)
|
||||
delete input;
|
||||
if(output)
|
||||
delete output;
|
||||
|
||||
if(context){
|
||||
pa_context_disconnect(context);
|
||||
pa_context_unref(context);
|
||||
}
|
||||
if(mainloop)
|
||||
pa_threaded_mainloop_free(mainloop);
|
||||
}
|
||||
|
||||
AudioOutput* AudioPulse::GetOutput(){
|
||||
return output;
|
||||
}
|
||||
|
||||
AudioInput* AudioPulse::GetInput(){
|
||||
return input;
|
||||
}
|
||||
|
||||
bool AudioPulse::DoOneOperation(std::function<pa_operation*(pa_context*)> f){
|
||||
if(!Load())
|
||||
return false;
|
||||
|
||||
pa_mainloop* ml;
|
||||
pa_mainloop_api* mlAPI;
|
||||
pa_context* ctx;
|
||||
pa_operation* op=NULL;
|
||||
int paReady=0;
|
||||
|
||||
ml=pa_mainloop_new();
|
||||
mlAPI=pa_mainloop_get_api(ml);
|
||||
ctx=pa_context_new(mlAPI, "libtgvoip");
|
||||
|
||||
pa_context_connect(ctx, NULL, PA_CONTEXT_NOFLAGS, NULL);
|
||||
pa_context_set_state_callback(ctx, [](pa_context* context, void* arg){
|
||||
pa_context_state_t state;
|
||||
int* pa_ready=(int*)arg;
|
||||
|
||||
state=pa_context_get_state(context);
|
||||
switch(state){
|
||||
case PA_CONTEXT_UNCONNECTED:
|
||||
case PA_CONTEXT_CONNECTING:
|
||||
case PA_CONTEXT_AUTHORIZING:
|
||||
case PA_CONTEXT_SETTING_NAME:
|
||||
default:
|
||||
break;
|
||||
case PA_CONTEXT_FAILED:
|
||||
case PA_CONTEXT_TERMINATED:
|
||||
*pa_ready=2;
|
||||
break;
|
||||
case PA_CONTEXT_READY:
|
||||
*pa_ready=1;
|
||||
break;
|
||||
}
|
||||
}, &paReady);
|
||||
|
||||
while(true){
|
||||
if(paReady==0){
|
||||
pa_mainloop_iterate(ml, 1, NULL);
|
||||
continue;
|
||||
}
|
||||
if(paReady==2){
|
||||
pa_context_disconnect(ctx);
|
||||
pa_context_unref(ctx);
|
||||
pa_mainloop_free(ml);
|
||||
return false;
|
||||
}
|
||||
if(!op){
|
||||
op=f(ctx);
|
||||
continue;
|
||||
}
|
||||
if(pa_operation_get_state(op)==PA_OPERATION_DONE){
|
||||
pa_operation_unref(op);
|
||||
pa_context_disconnect(ctx);
|
||||
pa_context_unref(ctx);
|
||||
pa_mainloop_free(ml);
|
||||
return true;
|
||||
}
|
||||
pa_mainloop_iterate(ml, 1, NULL);
|
||||
}
|
||||
}
|
||||
95
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioPulse.h
Normal file
95
TMessagesProj/jni/voip/libtgvoip/os/linux/AudioPulse.h
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
#ifndef LIBTGVOIP_PULSEAUDIOLOADER_H
|
||||
#define LIBTGVOIP_PULSEAUDIOLOADER_H
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include "../../audio/AudioIO.h"
|
||||
#include "AudioInputPulse.h"
|
||||
#include "AudioOutputPulse.h"
|
||||
|
||||
#define DECLARE_DL_FUNCTION(name) static typeof(name)* _import_##name
|
||||
|
||||
namespace tgvoip{
|
||||
namespace audio{
|
||||
class AudioPulse : public AudioIO{
|
||||
public:
|
||||
AudioPulse(std::string inputDevice, std::string outputDevice);
|
||||
virtual ~AudioPulse();
|
||||
virtual AudioInput* GetInput();
|
||||
virtual AudioOutput* GetOutput();
|
||||
|
||||
static bool Load();
|
||||
static bool DoOneOperation(std::function<pa_operation*(pa_context*)> f);
|
||||
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_new);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_get_api);
|
||||
DECLARE_DL_FUNCTION(pa_context_new);
|
||||
DECLARE_DL_FUNCTION(pa_context_new_with_proplist);
|
||||
DECLARE_DL_FUNCTION(pa_context_set_state_callback);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_lock);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_unlock);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_start);
|
||||
DECLARE_DL_FUNCTION(pa_context_connect);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_state);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_wait);
|
||||
DECLARE_DL_FUNCTION(pa_stream_new_with_proplist);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_state_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_write_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_connect_playback);
|
||||
DECLARE_DL_FUNCTION(pa_operation_unref);
|
||||
DECLARE_DL_FUNCTION(pa_stream_cork);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_stop);
|
||||
DECLARE_DL_FUNCTION(pa_stream_disconnect);
|
||||
DECLARE_DL_FUNCTION(pa_stream_unref);
|
||||
DECLARE_DL_FUNCTION(pa_context_disconnect);
|
||||
DECLARE_DL_FUNCTION(pa_context_unref);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_free);
|
||||
DECLARE_DL_FUNCTION(pa_threaded_mainloop_signal);
|
||||
DECLARE_DL_FUNCTION(pa_stream_begin_write);
|
||||
DECLARE_DL_FUNCTION(pa_stream_write);
|
||||
DECLARE_DL_FUNCTION(pa_stream_get_state);
|
||||
DECLARE_DL_FUNCTION(pa_strerror);
|
||||
DECLARE_DL_FUNCTION(pa_stream_set_read_callback);
|
||||
DECLARE_DL_FUNCTION(pa_stream_connect_record);
|
||||
DECLARE_DL_FUNCTION(pa_stream_peek);
|
||||
DECLARE_DL_FUNCTION(pa_stream_drop);
|
||||
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_new);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_get_api);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_iterate);
|
||||
DECLARE_DL_FUNCTION(pa_mainloop_free);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_sink_info_list);
|
||||
DECLARE_DL_FUNCTION(pa_context_get_source_info_list);
|
||||
DECLARE_DL_FUNCTION(pa_operation_get_state);
|
||||
|
||||
DECLARE_DL_FUNCTION(pa_proplist_new);
|
||||
DECLARE_DL_FUNCTION(pa_proplist_sets);
|
||||
DECLARE_DL_FUNCTION(pa_proplist_free);
|
||||
|
||||
DECLARE_DL_FUNCTION(pa_stream_get_latency);
|
||||
|
||||
private:
|
||||
static void* lib;
|
||||
static bool loaded;
|
||||
AudioInputPulse* input=NULL;
|
||||
AudioOutputPulse* output=NULL;
|
||||
|
||||
pa_threaded_mainloop* mainloop;
|
||||
pa_mainloop_api* mainloopApi;
|
||||
pa_context* context;
|
||||
bool isLocked=false;
|
||||
bool didStart=false;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#undef DECLARE_DL_FUNCTION
|
||||
|
||||
#endif // LIBTGVOIP_PULSEAUDIOLOADER_H
|
||||
48
TMessagesProj/jni/voip/libtgvoip/os/linux/PulseFunctions.h
Normal file
48
TMessagesProj/jni/voip/libtgvoip/os/linux/PulseFunctions.h
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#ifndef LIBTGVOIP_PULSE_FUNCTIONS_H
|
||||
#define LIBTGVOIP_PULSE_FUNCTIONS_H
|
||||
|
||||
#define pa_threaded_mainloop_new AudioPulse::_import_pa_threaded_mainloop_new
|
||||
#define pa_threaded_mainloop_get_api AudioPulse::_import_pa_threaded_mainloop_get_api
|
||||
#define pa_context_new AudioPulse::_import_pa_context_new
|
||||
#define pa_context_new_with_proplist AudioPulse::_import_pa_context_new_with_proplist
|
||||
#define pa_context_set_state_callback AudioPulse::_import_pa_context_set_state_callback
|
||||
#define pa_threaded_mainloop_lock AudioPulse::_import_pa_threaded_mainloop_lock
|
||||
#define pa_threaded_mainloop_unlock AudioPulse::_import_pa_threaded_mainloop_unlock
|
||||
#define pa_threaded_mainloop_start AudioPulse::_import_pa_threaded_mainloop_start
|
||||
#define pa_context_connect AudioPulse::_import_pa_context_connect
|
||||
#define pa_context_get_state AudioPulse::_import_pa_context_get_state
|
||||
#define pa_threaded_mainloop_wait AudioPulse::_import_pa_threaded_mainloop_wait
|
||||
#define pa_stream_new_with_proplist AudioPulse::_import_pa_stream_new_with_proplist
|
||||
#define pa_stream_set_state_callback AudioPulse::_import_pa_stream_set_state_callback
|
||||
#define pa_stream_set_write_callback AudioPulse::_import_pa_stream_set_write_callback
|
||||
#define pa_stream_connect_playback AudioPulse::_import_pa_stream_connect_playback
|
||||
#define pa_operation_unref AudioPulse::_import_pa_operation_unref
|
||||
#define pa_stream_cork AudioPulse::_import_pa_stream_cork
|
||||
#define pa_threaded_mainloop_stop AudioPulse::_import_pa_threaded_mainloop_stop
|
||||
#define pa_stream_disconnect AudioPulse::_import_pa_stream_disconnect
|
||||
#define pa_stream_unref AudioPulse::_import_pa_stream_unref
|
||||
#define pa_context_disconnect AudioPulse::_import_pa_context_disconnect
|
||||
#define pa_context_unref AudioPulse::_import_pa_context_unref
|
||||
#define pa_threaded_mainloop_free AudioPulse::_import_pa_threaded_mainloop_free
|
||||
#define pa_threaded_mainloop_signal AudioPulse::_import_pa_threaded_mainloop_signal
|
||||
#define pa_stream_begin_write AudioPulse::_import_pa_stream_begin_write
|
||||
#define pa_stream_write AudioPulse::_import_pa_stream_write
|
||||
#define pa_strerror AudioPulse::_import_pa_strerror
|
||||
#define pa_stream_get_state AudioPulse::_import_pa_stream_get_state
|
||||
#define pa_stream_set_read_callback AudioPulse::_import_pa_stream_set_read_callback
|
||||
#define pa_stream_connect_record AudioPulse::_import_pa_stream_connect_record
|
||||
#define pa_stream_peek AudioPulse::_import_pa_stream_peek
|
||||
#define pa_stream_drop AudioPulse::_import_pa_stream_drop
|
||||
#define pa_mainloop_new AudioPulse::_import_pa_mainloop_new
|
||||
#define pa_mainloop_get_api AudioPulse::_import_pa_mainloop_get_api
|
||||
#define pa_mainloop_iterate AudioPulse::_import_pa_mainloop_iterate
|
||||
#define pa_mainloop_free AudioPulse::_import_pa_mainloop_free
|
||||
#define pa_context_get_sink_info_list AudioPulse::_import_pa_context_get_sink_info_list
|
||||
#define pa_context_get_source_info_list AudioPulse::_import_pa_context_get_source_info_list
|
||||
#define pa_operation_get_state AudioPulse::_import_pa_operation_get_state
|
||||
#define pa_proplist_new AudioPulse::_import_pa_proplist_new
|
||||
#define pa_proplist_sets AudioPulse::_import_pa_proplist_sets
|
||||
#define pa_proplist_free AudioPulse::_import_pa_proplist_free
|
||||
#define pa_stream_get_latency AudioPulse::_import_pa_stream_get_latency
|
||||
|
||||
#endif //LIBTGVOIP_PULSE_FUNCTIONS_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue