25 #define LOC QString("AudioInALSA: ")
26 #define LOC_DEV QString("AudioInALSA(%1): ").arg(m_alsaDevice.constData())
32 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"invalid alsa device name, %1")
36 (void)
AlsaBad(snd_config_update_free_global(),
"failed to update snd config");
41 SND_PCM_STREAM_CAPTURE, 0),
74 LOG(VB_AUDIO, LOG_INFO,
LOC_DEV +
"capture stopped");
87 case SND_PCM_STATE_XRUN:
88 case SND_PCM_STATE_SUSPENDED:
91 LOG(VB_AUDIO, LOG_INFO,
LOC_DEV +
"xrun recovery " +
92 (recov ?
"good" :
"not good"));
97 case SND_PCM_STATE_PREPARED:
101 case SND_PCM_STATE_RUNNING:
102 bytes_read =
PcmRead(buf, nbytes);
106 QString(
"weird pcm state through GetSamples, %1")
118 snd_pcm_sframes_t frames_avail = 0;
122 case SND_PCM_STATE_PREPARED:
123 case SND_PCM_STATE_RUNNING:
126 "GetNumReadyBytes, available update failed"))
136 snd_pcm_hw_params_t* hwparams =
nullptr;
137 snd_pcm_hw_params_alloca(&hwparams);
139 "failed to init hw params"))
141 snd_pcm_access_t axs = SND_PCM_ACCESS_RW_INTERLEAVED;
143 "failed to set interleaved rw io"))
145 snd_pcm_format_t format =
148 QString(
"failed to set sample format %1")
149 .arg(snd_pcm_format_description(format))))
155 if(
AlsaBad(snd_pcm_hw_params_get_channels_min(hwparams, &min_chans),
156 QString(
"unable to get min channel count")))
158 if(
AlsaBad(snd_pcm_hw_params_get_channels_max(hwparams, &max_chans),
159 QString(
"unable to get max channel count")))
162 QString(
"min channels %1, max channels %2, myth requests %3")
177 if (!
AlsaBad(snd_pcm_hw_params_get_rate_numden(hwparams, &rate_num,
178 &rate_den),
"snd_pcm_hw_params_get_rate_numden failed"))
183 QString(
"device reports sample rate as %1")
184 .arg(rate_num / rate_den));
189 uint buffer_time = 64000;
190 uint period_time = buffer_time / 4;
191 if (
AlsaBad(snd_pcm_hw_params_set_period_time_near(
m_pcmHandle, hwparams, &period_time,
nullptr),
192 "failed to set period time"))
194 if (
AlsaBad(snd_pcm_hw_params_set_buffer_time_near(
m_pcmHandle, hwparams, &buffer_time,
nullptr),
195 "failed to set buffer time"))
198 "failed to get period size"))
202 "failed to set hwparams"))
207 QString(
"channels %1, sample rate %2, buffer_time %3 msec, period "
211 LOG(VB_AUDIO, LOG_DEBUG,
LOC_DEV + QString(
"myth block size %1")
218 snd_pcm_sw_params_t* swparams =
nullptr;
219 snd_pcm_sw_params_alloca(&swparams);
220 snd_pcm_uframes_t boundary = 0;
222 "failed to get swparams"))
224 if (
AlsaBad(snd_pcm_sw_params_get_boundary(swparams, &boundary),
225 "failed to get boundary"))
229 boundary),
"failed to set start threshold"))
232 boundary),
"failed to set stop threshold"))
235 "failed to set software parameters"))
243 auto* bufptr = (
unsigned char*)buf;
244 snd_pcm_uframes_t to_read = snd_pcm_bytes_to_frames(
m_pcmHandle, nbytes);
245 snd_pcm_uframes_t nframes = to_read;
246 snd_pcm_sframes_t nread = 0;
247 snd_pcm_sframes_t avail = 0;
249 while (nframes > 0 && retries < 3)
252 if (
AlsaBad(avail,
"available update failed"))
260 nread = snd_pcm_readi(
m_pcmHandle, bufptr, nframes);
269 QString(
"in a state unfit to read (%1): %2")
270 .arg(nread).arg(snd_strerror(nread)));
274 #if ESTRPIPE != EPIPE
281 QString(
"weird return from snd_pcm_readi: %1")
282 .arg(snd_strerror(nread)));
289 bufptr += snd_pcm_frames_to_bytes(
m_pcmHandle, nread);
296 QString(
"short pcm read, %1 of %2 frames, retries %3")
297 .arg(to_read - nframes).arg(to_read).arg(retries));
299 return snd_pcm_frames_to_bytes(
m_pcmHandle, to_read - nframes);
307 bool suspense =
false;
313 #if ESTRPIPE != EPIPE
324 QString(
"failed to recover from %1. %2")
325 .arg(suspense ?
"suspend" :
"underrun",
340 bool bad = (op_result < 0);
343 errmsg +
": " + snd_strerror(op_result));