| 6 |
| 7 | #include <stdlib.h> |
| 8 | #include <stdio.h> |
| 9 | #include <assert.h> |
| 10 | #include <dlfcn.h> |
| 11 | #include "AudioInputALSA.h" |
| 12 | #include "../../logging.h" |
| 13 | #include "../../VoIPController.h" |
| 14 |
| 15 | using namespace tgvoip::audio; |
| 16 |
| 17 | #define BUFFER_SIZE 960 |
| 18 | #define CHECK_ERROR(res, msg) if(res<0){LOGE(msg ": %s", _snd_strerror(res)); failed=true; return;} |
| 19 | #define CHECK_DL_ERROR(res, msg) if(!res){LOGE(msg ": %s", dlerror()); failed=true; return;} |
| 20 | #define LOAD_FUNCTION(lib, name, ref) {ref=(typeof(ref))dlsym(lib, name); CHECK_DL_ERROR(ref, "Error getting entry point for " name);} |
| 21 |
| 22 | AudioInputALSA::AudioInputALSA(std::string devID){ |
| 23 | isRecording=false; |
| 24 | handle=NULL; |
| 25 |
| 26 | lib=dlopen("libasound.so.2", RTLD_LAZY); |
| 27 | if(!lib) |
| 28 | lib=dlopen("libasound.so", RTLD_LAZY); |
| 29 | if(!lib){ |
| 30 | LOGE("Error loading libasound: %s", dlerror()); |
| 31 | failed=true; |
| 32 | return; |
| 33 | } |
| 34 |
| 35 | LOAD_FUNCTION(lib, "snd_pcm_open", _snd_pcm_open); |
| 36 | LOAD_FUNCTION(lib, "snd_pcm_set_params", _snd_pcm_set_params); |
| 37 | LOAD_FUNCTION(lib, "snd_pcm_close", _snd_pcm_close); |
| 38 | LOAD_FUNCTION(lib, "snd_pcm_readi", _snd_pcm_readi); |
| 39 | LOAD_FUNCTION(lib, "snd_pcm_recover", _snd_pcm_recover); |
| 40 | LOAD_FUNCTION(lib, "snd_strerror", _snd_strerror); |
| 41 |
| 42 | SetCurrentDevice(devID); |
| 43 | } |
| 44 |
| 45 | AudioInputALSA::~AudioInputALSA(){ |
| 46 | if(handle) |
| 47 | _snd_pcm_close(handle); |
| 48 | if(lib) |
| 49 | dlclose(lib); |
| 50 | } |
| 51 |
| 52 | void AudioInputALSA::Start(){ |
| 53 | if(failed || isRecording) |
| 54 | return; |
| 55 |
| 56 | isRecording=true; |
| 57 | thread=new Thread(std::bind(&AudioInputALSA::RunThread, this)); |
| 58 | thread->SetName("AudioInputALSA"); |
| 59 | thread->Start(); |
| 60 | } |
| 61 |
| 62 | void AudioInputALSA::Stop(){ |
| 63 | if(!isRecording) |
| 64 | return; |
| 65 |
| 66 | isRecording=false; |
| 67 | thread->Join(); |
| 68 | delete thread; |
| 69 | thread=NULL; |
| 70 | } |
| 71 |
| 72 | void AudioInputALSA::RunThread(){ |
| 73 | unsigned char buffer[BUFFER_SIZE*2]; |
| 74 | snd_pcm_sframes_t frames; |
| 75 | while(isRecording){ |
| 76 | frames=_snd_pcm_readi(handle, buffer, BUFFER_SIZE); |
| 77 | if (frames < 0){ |
| 78 | frames = _snd_pcm_recover(handle, frames, 0); |
| 79 | } |
| 80 | if (frames < 0) { |
| 81 | LOGE("snd_pcm_readi failed: %s\n", _snd_strerror(frames)); |
| 82 | break; |
| 83 | } |
| 84 | InvokeCallback(buffer, sizeof(buffer)); |
| 85 | } |
| 86 | } |
| 87 |
| 88 | void AudioInputALSA::SetCurrentDevice(std::string devID){ |
| 89 | bool wasRecording=isRecording; |
| 90 | isRecording=false; |
| 91 | if(handle){ |
| 92 | thread->Join(); |
| 93 | _snd_pcm_close(handle); |
| 94 | } |
| 95 | currentDevice=devID; |
| 96 |
| 97 | int res=_snd_pcm_open(&handle, devID.c_str(), SND_PCM_STREAM_CAPTURE, 0); |
| 98 | if(res<0) |
| 99 | res=_snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0); |
| 100 | CHECK_ERROR(res, "snd_pcm_open failed"); |
| 101 |
| 102 | res=_snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000); |
| 103 | CHECK_ERROR(res, "snd_pcm_set_params failed"); |
| 104 |
| 105 | if(wasRecording){ |
| 106 | isRecording=true; |
| 107 | thread->Start(); |
| 108 | } |
| 109 | } |
| 110 |
| 111 | void AudioInputALSA::EnumerateDevices(std::vector<AudioInputDevice>& devs){ |
| 112 | int (*_snd_device_name_hint)(int card, const char* iface, void*** hints); |
| 113 | char* (*_snd_device_name_get_hint)(const void* hint, const char* id); |
| 114 | int (*_snd_device_name_free_hint)(void** hinst); |
| 115 | void* lib=dlopen("libasound.so.2", RTLD_LAZY); |
| 116 | if(!lib) |
| 117 | dlopen("libasound.so", RTLD_LAZY); |
| 118 | if(!lib) |
| 119 | return; |
| 120 |
| 121 | _snd_device_name_hint=(typeof(_snd_device_name_hint))dlsym(lib, "snd_device_name_hint"); |
| 122 | _snd_device_name_get_hint=(typeof(_snd_device_name_get_hint))dlsym(lib, "snd_device_name_get_hint"); |
| 123 | _snd_device_name_free_hint=(typeof(_snd_device_name_free_hint))dlsym(lib, "snd_device_name_free_hint"); |
| 124 |
| 125 | if(!_snd_device_name_hint || !_snd_device_name_get_hint || !_snd_device_name_free_hint){ |
| 126 | dlclose(lib); |
| 127 | return; |
| 128 | } |
| 129 |
| 130 | char** hints; |
| 131 | int err=_snd_device_name_hint(-1, "pcm", (void***)&hints); |
| 132 | if(err!=0){ |
| 133 | dlclose(lib); |
| 134 | return; |
| 135 | } |
| 136 |
| 137 | char** n=hints; |
| 138 | while(*n){ |
| 139 | char* name=_snd_device_name_get_hint(*n, "NAME"); |
| 140 | if(strncmp(name, "surround", 8)==0 || strcmp(name, "null")==0){ |
| 141 | free(name); |
| 142 | n++; |
| 143 | continue; |
| 144 | } |
| 145 | char* desc=_snd_device_name_get_hint(*n, "DESC"); |
| 146 | char* ioid=_snd_device_name_get_hint(*n, "IOID"); |
| 147 | if(!ioid || strcmp(ioid, "Input")==0){ |
| 148 | char* l1=strtok(desc, "\n"); |
| 149 | char* l2=strtok(NULL, "\n"); |
| 150 | char* tmp=strtok(l1, ","); |
| 151 | char* actualName=tmp; |
| 152 | while((tmp=strtok(NULL, ","))){ |
| 153 | actualName=tmp; |
| 154 | } |
| 155 | if(actualName[0]==' ') |
| 156 | actualName++; |
| 157 | AudioInputDevice dev; |
| 158 | dev.id=std::string(name); |
| 159 | if(l2){ |
| 160 | char buf[256]; |
| 161 | snprintf(buf, sizeof(buf), "%s (%s)", actualName, l2); |
| 162 | dev.displayName=std::string(buf); |
| 163 | }else{ |
| 164 | dev.displayName=std::string(actualName); |
| 165 | } |
| 166 | devs.push_back(dev); |
| 167 | } |
| 168 | free(name); |
| 169 | free(desc); |
| 170 | free(ioid); |
| 171 | n++; |
| 172 | } |
| 173 |
| 174 | dlclose(lib); |
| 175 | } |
| 176 |