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