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:
125 "GetNumReadyBytes, available update failed"))
135 snd_pcm_hw_params_t* hwparams =
nullptr;
136 snd_pcm_hw_params_alloca(&hwparams);
138 "failed to init hw params"))
140 snd_pcm_access_t axs = SND_PCM_ACCESS_RW_INTERLEAVED;
142 "failed to set interleaved rw io"))
144 snd_pcm_format_t format =
147 QString(
"failed to set sample format %1")
148 .arg(snd_pcm_format_description(format))))
154 if(
AlsaBad(snd_pcm_hw_params_get_channels_min(hwparams, &min_chans),
155 QString(
"unable to get min channel count")))
157 if(
AlsaBad(snd_pcm_hw_params_get_channels_max(hwparams, &max_chans),
158 QString(
"unable to get max channel count")))
161 QString(
"min channels %1, max channels %2, myth requests %3")
176 if (!
AlsaBad(snd_pcm_hw_params_get_rate_numden(hwparams, &rate_num,
177 &rate_den),
"snd_pcm_hw_params_get_rate_numden failed"))
182 QString(
"device reports sample rate as %1")
183 .arg(rate_num / rate_den));
188 uint buffer_time = 64000;
189 uint period_time = buffer_time / 4;
190 if (
AlsaBad(snd_pcm_hw_params_set_period_time_near(
m_pcmHandle, hwparams, &period_time,
nullptr),
191 "failed to set period time"))
193 if (
AlsaBad(snd_pcm_hw_params_set_buffer_time_near(
m_pcmHandle, hwparams, &buffer_time,
nullptr),
194 "failed to set buffer time"))
197 "failed to get period size"))
201 "failed to set hwparams"))
206 QString(
"channels %1, sample rate %2, buffer_time %3 msec, period "
210 LOG(VB_AUDIO, LOG_DEBUG,
LOC_DEV + QString(
"myth block size %1")
217 snd_pcm_sw_params_t* swparams =
nullptr;
218 snd_pcm_sw_params_alloca(&swparams);
219 snd_pcm_uframes_t boundary = 0;
221 "failed to get swparams"))
223 if (
AlsaBad(snd_pcm_sw_params_get_boundary(swparams, &boundary),
224 "failed to get boundary"))
228 boundary),
"failed to set start threshold"))
231 boundary),
"failed to set stop threshold"))
234 "failed to set software parameters"))
242 auto* bufptr = (
unsigned char*)buf;
243 snd_pcm_uframes_t to_read = snd_pcm_bytes_to_frames(
m_pcmHandle, nbytes);
244 snd_pcm_uframes_t nframes = to_read;
245 snd_pcm_sframes_t nread = 0;
246 snd_pcm_sframes_t avail = 0;
248 while (nframes > 0 && retries < 3)
251 "available update failed"))
259 if ((nread = snd_pcm_readi(
m_pcmHandle, bufptr, nframes)) < 0)
267 QString(
"in a state unfit to read (%1): %2")
268 .arg(nread).arg(snd_strerror(nread)));
272 #if ESTRPIPE != EPIPE
279 QString(
"weird return from snd_pcm_readi: %1")
280 .arg(snd_strerror(nread)));
287 bufptr += snd_pcm_frames_to_bytes(
m_pcmHandle, nread);
294 QString(
"short pcm read, %1 of %2 frames, retries %3")
295 .arg(to_read - nframes).arg(to_read).arg(retries));
297 return snd_pcm_frames_to_bytes(
m_pcmHandle, to_read - nframes);
305 bool suspense =
false;
311 #if ESTRPIPE != EPIPE
322 QString(
"failed to recover from %1. %2")
323 .arg(suspense ?
"suspend" :
"underrun",
338 bool bad = (op_result < 0);
341 errmsg +
": " + snd_strerror(op_result));