| 120 | } |
| 121 | |
| 122 | void AlsaWorkerThread::run(){ |
| 123 | snd_pcm_sframes_t workingSizeSample = (1 << SOUNDCAPLINUX_BUF_LENP); |
| 124 | snd_pcm_uframes_t periodSize = workingSizeSample; |
| 125 | snd_pcm_uframes_t bufferSizeInBytes = periodSize * 2; |
| 126 | bool initialMessage = false; |
| 127 | snd_pcm_t* handle; |
| 128 | int status = 0; |
| 129 | bool error = false; |
| 130 | unsigned int exactRate = 22050; |
| 131 | |
| 132 | if (!initAlsaLib()) |
| 133 | return; |
| 134 | |
| 135 | if ((status = this->snd_pcm_open(&handle, QSTRING_CSTR(_device), SND_PCM_STREAM_CAPTURE, 0)) < 0) { |
| 136 | Error(_logger, "Cannot open input sound device '%s'. Error: '%s'", QSTRING_CSTR(_device), this->snd_strerror(status)); |
| 137 | |
| 138 | // clean-up |
| 139 | closeAlsaLib(); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | snd_pcm_hw_params_t* selected_hw_params = nullptr; |
| 144 | |
| 145 | if ((status = this->snd_pcm_hw_params_malloc(&selected_hw_params)) < 0) { |
| 146 | Error(_logger, "Cannot allocate hardware parameter buffer: '%s'", this->snd_strerror(status)); |
| 147 | this->snd_pcm_close(handle); |
| 148 | |
| 149 | // clean-up |
| 150 | closeAlsaLib(); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | try |
| 155 | { |
| 156 | if ((status = this->snd_pcm_hw_params_any(handle, selected_hw_params)) < 0) { |
| 157 | Error(_logger, "Cannot set snd_pcm_hw_params_any: '%s'", this->snd_strerror(status)); |
| 158 | throw 1; |
| 159 | } |
| 160 | |
| 161 | if ((status = this->snd_pcm_hw_params_set_access(handle, selected_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { |
| 162 | Error(_logger, "Cannot set snd_pcm_hw_params_set_access: '%s'", this->snd_strerror(status)); |
| 163 | throw 2; |
| 164 | } |
| 165 | |
| 166 | if ((status = this->snd_pcm_hw_params_set_format(handle, selected_hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { |
| 167 | Error(_logger, "Cannot set snd_pcm_hw_params_set_format: '%s'", this->snd_strerror(status)); |
| 168 | throw 3; |
| 169 | } |
| 170 | |
| 171 | if ((status = this->snd_pcm_hw_params_set_rate_near(handle, selected_hw_params, &exactRate, 0)) < 0) { |
| 172 | Error(_logger, "Cannot set snd_pcm_hw_params_set_rate_near: '%s'", this->snd_strerror(status)); |
| 173 | throw 4; |
| 174 | } |
| 175 | else if (exactRate != 22050) |
| 176 | { |
| 177 | Error(_logger, "Cannot set rate to 22050"); |
| 178 | throw 5; |
| 179 | } |
nothing calls this directly
no test coverage detected