Lines Matching defs:lhandle
41 static int set_rate_shift(struct loopback_handle *lhandle, double pitch);
42 static int get_rate(struct loopback_handle *lhandle);
111 static int setparams_stream(struct loopback_handle *lhandle,
114 snd_pcm_t *handle = lhandle->handle;
120 logit(LOG_CRIT, "Broken configuration for %s PCM: no configurations available: %s\n", lhandle->id, snd_strerror(err));
123 err = snd_pcm_hw_params_set_rate_resample(handle, params, lhandle->resample);
125 logit(LOG_CRIT, "Resample setup failed for %s (val %u): %s\n", lhandle->id, lhandle->resample, snd_strerror(err));
128 err = snd_pcm_hw_params_set_access(handle, params, lhandle->access);
130 logit(LOG_CRIT, "Access type not available for %s: %s\n", lhandle->id, snd_strerror(err));
133 err = snd_pcm_hw_params_set_format(handle, params, lhandle->format);
135 logit(LOG_CRIT, "Sample format not available for %s: %s\n", lhandle->id, snd_strerror(err));
138 err = snd_pcm_hw_params_set_channels(handle, params, lhandle->channels);
140 logit(LOG_CRIT, "Channels count (%u) not available for %s: %s\n", lhandle->channels, lhandle->id, snd_strerror(err));
143 rrate = lhandle->rate_req;
146 logit(LOG_CRIT, "Rate %uHz not available for %s: %s\n", lhandle->rate_req, lhandle->id, snd_strerror(err));
151 lhandle->rate = rrate;
154 !lhandle->loopback->src_enable &&
156 rrate != lhandle->rate) {
157 logit(LOG_CRIT, "Rate does not match (requested %uHz, got %uHz, resample %u)\n", lhandle->rate, rrate, lhandle->resample);
160 lhandle->pitch = (double)lhandle->rate_req / (double)lhandle->rate;
164 static int setparams_bufsize(struct loopback_handle *lhandle,
169 snd_pcm_t *handle = lhandle->handle;
175 if (lhandle->buffer_size_req > 0) {
176 bufsize = lhandle->buffer_size_req;
181 if (lhandle->buffer_size_req > 0) {
182 logit(LOG_CRIT, "Unable to set buffer size %li for %s\n", (long)lhandle->buffer_size, lhandle->id);
197 logit(LOG_CRIT, "Unable to set buffer size %li for %s: %s\n", periodsize, lhandle->id, snd_strerror(err));
202 snd_output_printf(lhandle->loopback->output, "%s: buffer_size=%li\n", lhandle->id, periodsize);
203 if (lhandle->period_size_req > 0)
204 periodsize = lhandle->period_size_req;
209 logit(LOG_CRIT, "Unable to set period size %li for %s: %s\n", periodsize, lhandle->id, snd_strerror(err));
214 snd_output_printf(lhandle->loopback->output, "%s: period_size=%li\n", lhandle->id, periodsize);
220 lhandle->period_size = periodsize;
221 lhandle->buffer_size = buffersize;
225 static int setparams_set(struct loopback_handle *lhandle,
230 snd_pcm_t *handle = lhandle->handle;
236 logit(LOG_CRIT, "Unable to set hw params for %s: %s\n", lhandle->id, snd_strerror(err));
241 logit(LOG_CRIT, "Unable to determine current swparams for %s: %s\n", lhandle->id, snd_strerror(err));
246 logit(LOG_CRIT, "Unable to set start threshold mode for %s: %s\n", lhandle->id, snd_strerror(err));
251 if (lhandle->nblock) {
252 if (lhandle == lhandle->loopback->play) {
258 snd_output_printf(lhandle->loopback->output, "%s: avail_min1=%li\n", lhandle->id, val);
260 if (lhandle == lhandle->loopback->play) {
271 snd_output_printf(lhandle->loopback->output, "%s: avail_min2=%li\n", lhandle->id, val);
275 logit(LOG_CRIT, "Unable to set avail min for %s: %s\n", lhandle->id, snd_strerror(err));
278 snd_pcm_sw_params_get_avail_min(swparams, &lhandle->avail_min);
281 logit(LOG_CRIT, "Unable to set sw params for %s: %s\n", lhandle->id, snd_strerror(err));
287 static int increase_playback_avail_min(struct loopback_handle *lhandle)
289 snd_pcm_t *handle = lhandle->handle;
294 if (lhandle->avail_min + (lhandle->period_size / 2) > lhandle->buffer_size) {
304 logit(LOG_CRIT, "Unable to determine current swparams for %s: %s\n", lhandle->id, snd_strerror(err));
307 err = snd_pcm_sw_params_set_avail_min(handle, swparams, lhandle->avail_min + 4);
309 logit(LOG_CRIT, "Unable to set avail min for %s: %s\n", lhandle->id, snd_strerror(err));
312 snd_pcm_sw_params_get_avail_min(swparams, &lhandle->avail_min);
314 snd_output_printf(lhandle->loopback->output, "%s: change avail_min=%li\n", lhandle->id, lhandle->avail_min);
317 logit(LOG_CRIT, "Unable to set sw params for %s: %s\n", lhandle->id, snd_strerror(err));
488 static inline snd_pcm_uframes_t buf_avail(struct loopback_handle *lhandle)
490 return lhandle->buf_size - lhandle->buf_count;
645 static int xrun(struct loopback_handle *lhandle)
649 if (lhandle == lhandle->loopback->play) {
650 logit(LOG_DEBUG, "underrun for %s\n", lhandle->id);
651 xrun_stats(lhandle->loopback);
652 if ((err = snd_pcm_prepare(lhandle->handle)) < 0)
654 lhandle->xrun_pending = 1;
656 logit(LOG_DEBUG, "overrun for %s\n", lhandle->id);
657 xrun_stats(lhandle->loopback);
658 if ((err = snd_pcm_prepare(lhandle->handle)) < 0)
660 lhandle->xrun_pending = 1;
665 static int suspend(struct loopback_handle *lhandle)
669 while ((err = snd_pcm_resume(lhandle->handle)) == -EAGAIN)
672 return xrun(lhandle);
676 static int readit(struct loopback_handle *lhandle)
682 avail = snd_pcm_avail_update(lhandle->handle);
684 return xrun(lhandle);
686 if ((err = suspend(lhandle)) < 0)
689 if ((snd_pcm_uframes_t)avail > buf_avail(lhandle)) {
690 lhandle->buf_over += avail - buf_avail(lhandle);
691 avail = buf_avail(lhandle);
693 if (snd_pcm_state(lhandle->handle) == SND_PCM_STATE_DRAINING) {
694 lhandle->loopback->reinit = 1;
699 r = buf_avail(lhandle);
700 if (r + lhandle->buf_pos > lhandle->buf_size)
701 r = lhandle->buf_size - lhandle->buf_pos;
704 r = snd_pcm_readi(lhandle->handle,
705 lhandle->buf +
706 lhandle->buf_pos *
707 lhandle->frame_size, r);
712 err = xrun(lhandle);
715 if ((err = suspend(lhandle)) < 0)
723 if (lhandle->loopback->cfile)
724 fwrite(lhandle->buf + lhandle->buf_pos * lhandle->frame_size,
725 r, lhandle->frame_size, lhandle->loopback->cfile);
728 if (lhandle->max < (snd_pcm_uframes_t)res)
729 lhandle->max = res;
730 lhandle->counter += r;
731 lhandle->buf_count += r;
732 lhandle->buf_pos += r;
733 lhandle->buf_pos %= lhandle->buf_size;
739 static int writeit(struct loopback_handle *lhandle)
746 avail = snd_pcm_avail_update(lhandle->handle);
748 if ((err = xrun(lhandle)) < 0)
752 if ((err = suspend(lhandle)) < 0)
756 while (avail > 0 && lhandle->buf_count > 0) {
757 r = lhandle->buf_count;
758 if (r + lhandle->buf_pos > lhandle->buf_size)
759 r = lhandle->buf_size - lhandle->buf_pos;
762 r = snd_pcm_writei(lhandle->handle,
763 lhandle->buf +
764 lhandle->buf_pos *
765 lhandle->frame_size, r);
768 if ((err = xrun(lhandle)) < 0)
776 if (lhandle->loopback->pfile)
777 fwrite(lhandle->buf + lhandle->buf_pos * lhandle->frame_size,
778 r, lhandle->frame_size, lhandle->loopback->pfile);
781 lhandle->counter += r;
782 lhandle->buf_count -= r;
783 lhandle->buf_pos += r;
784 lhandle->buf_pos %= lhandle->buf_size;
785 xrun_profile(lhandle->loopback);
786 if (lhandle->loopback->stop_pending) {
787 lhandle->loopback->stop_count += r;
788 if (lhandle->loopback->stop_count * lhandle->pitch >
789 lhandle->loopback->latency * 3) {
790 lhandle->loopback->stop_pending = 0;
791 lhandle->loopback->reinit = 1;
1041 static int set_notify(struct loopback_handle *lhandle, int enable)
1045 if (lhandle->ctl_notify == NULL)
1047 snd_ctl_elem_value_set_boolean(lhandle->ctl_notify, 0, enable);
1048 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_notify);
1050 logit(LOG_CRIT, "Cannot set PCM Notify element for %s: %s\n", lhandle->id, snd_strerror(err));
1053 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_notify);
1055 logit(LOG_CRIT, "Cannot get PCM Notify element for %s: %s\n", lhandle->id, snd_strerror(err));
1061 static int set_rate_shift(struct loopback_handle *lhandle, double pitch)
1065 if (lhandle->ctl_rate_shift) {
1066 snd_ctl_elem_value_set_integer(lhandle->ctl_rate_shift, 0, pitch * 100000);
1067 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_rate_shift);
1068 } else if (lhandle->ctl_pitch) {
1070 snd_ctl_elem_value_set_integer(lhandle->ctl_pitch, 0, (1 / pitch) * 1000000);
1071 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_pitch);
1076 logit(LOG_CRIT, "Cannot set PCM Rate Shift element for %s: %s\n", lhandle->id, snd_strerror(err));
1123 static int get_active(struct loopback_handle *lhandle)
1127 if (lhandle->ctl_active == NULL)
1129 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_active);
1131 logit(LOG_CRIT, "Cannot get PCM Slave Active element for %s: %s\n", lhandle->id, snd_strerror(err));
1134 return snd_ctl_elem_value_get_boolean(lhandle->ctl_active, 0);
1137 static int get_format(struct loopback_handle *lhandle)
1141 if (lhandle->ctl_format == NULL)
1143 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_format);
1145 logit(LOG_CRIT, "Cannot get PCM Format element for %s: %s\n", lhandle->id, snd_strerror(err));
1148 return snd_ctl_elem_value_get_integer(lhandle->ctl_format, 0);
1151 static int get_rate(struct loopback_handle *lhandle)
1155 if (lhandle->ctl_rate == NULL)
1157 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_rate);
1159 logit(LOG_CRIT, "Cannot get PCM Rate element for %s: %s\n", lhandle->id, snd_strerror(err));
1162 return snd_ctl_elem_value_get_integer(lhandle->ctl_rate, 0);
1165 static int get_channels(struct loopback_handle *lhandle)
1169 if (lhandle->ctl_channels == NULL)
1171 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_channels);
1173 logit(LOG_CRIT, "Cannot get PCM Channels element for %s: %s\n", lhandle->id, snd_strerror(err));
1176 return snd_ctl_elem_value_get_integer(lhandle->ctl_channels, 0);
1179 static int openctl_elem_id(struct loopback_handle *lhandle, snd_ctl_elem_id_t *id,
1190 err = snd_ctl_elem_read(lhandle->ctl, *elem);
1196 snd_output_printf(lhandle->loopback->output,
1198 snd_ctl_elem_id_get_name(id), snd_ctl_name(lhandle->ctl),
1205 static int openctl_elem(struct loopback_handle *lhandle,
1216 return openctl_elem_id(lhandle, id, elem);
1219 static int openctl_elem_ascii(struct loopback_handle *lhandle, char *ascii_name,
1229 return openctl_elem_id(lhandle, id, elem);
1232 static int openctl(struct loopback_handle *lhandle, int device, int subdevice)
1236 lhandle->ctl_rate_shift = NULL;
1237 if (lhandle->loopback->play == lhandle) {
1239 if (lhandle->prateshift_name) {
1240 err = openctl_elem_ascii(lhandle, lhandle->prateshift_name,
1241 &lhandle->ctl_rate_shift);
1244 lhandle->prateshift_name);
1248 openctl_elem(lhandle, device, subdevice, "Playback Pitch 1000000",
1249 &lhandle->ctl_pitch);
1250 set_rate_shift(lhandle, 1);
1251 if (lhandle->loopback->controls)
1256 openctl_elem(lhandle, device, subdevice, "PCM Notify",
1257 &lhandle->ctl_notify);
1258 openctl_elem(lhandle, device, subdevice, "PCM Rate Shift 100000",
1259 &lhandle->ctl_rate_shift);
1260 openctl_elem(lhandle, device, subdevice, "Capture Pitch 1000000",
1261 &lhandle->ctl_pitch);
1262 set_rate_shift(lhandle, 1);
1263 openctl_elem(lhandle, device, subdevice, "PCM Slave Active",
1264 &lhandle->ctl_active);
1265 openctl_elem(lhandle, device, subdevice, "PCM Slave Format",
1266 &lhandle->ctl_format);
1267 openctl_elem(lhandle, device, subdevice, "PCM Slave Rate",
1268 &lhandle->ctl_rate);
1269 openctl_elem(lhandle, device, subdevice, "PCM Slave Channels",
1270 &lhandle->ctl_channels);
1271 if ((lhandle->ctl_active &&
1272 lhandle->ctl_format &&
1273 lhandle->ctl_rate &&
1274 lhandle->ctl_channels) ||
1275 lhandle->loopback->controls) {
1277 if ((err = snd_ctl_poll_descriptors_count(lhandle->ctl)) < 0)
1278 lhandle->ctl_pollfd_count = 0;
1280 lhandle->ctl_pollfd_count = err;
1281 if (snd_ctl_subscribe_events(lhandle->ctl, 1) < 0)
1282 lhandle->ctl_pollfd_count = 0;
1287 static int openit(struct loopback_handle *lhandle)
1290 int stream = lhandle == lhandle->loopback->play ?
1295 err = snd_pcm_open(&lhandle->handle, lhandle->device, stream, SND_PCM_NONBLOCK);
1298 logit(LOG_CRIT, "%s open error: %s\n", lhandle->id, snd_strerror(err));
1303 if ((err = snd_pcm_info(lhandle->handle, info)) < 0) {
1311 lhandle->card_number = card;
1312 lhandle->ctl = NULL;
1313 if (card >= 0 || lhandle->ctldev) {
1314 char name[16], *dev = lhandle->ctldev;
1320 err = snd_ctl_open(&lhandle->ctl, dev, SND_CTL_NONBLOCK);
1323 logit(LOG_CRIT, "%s [%s] ctl open error: %s\n", lhandle->id, dev, snd_strerror(err));
1324 lhandle->ctl = NULL;
1326 if (lhandle->ctl)
1327 openctl(lhandle, device, subdevice);
1332 static int freeit(struct loopback_handle *lhandle)
1334 free(lhandle->buf);
1335 lhandle->buf = NULL;
1339 static int closeit(struct loopback_handle *lhandle)
1343 set_rate_shift(lhandle, 1);
1344 if (lhandle->ctl_rate_shift)
1345 snd_ctl_elem_value_free(lhandle->ctl_rate_shift);
1346 lhandle->ctl_rate_shift = NULL;
1347 if (lhandle->ctl_pitch)
1348 snd_ctl_elem_value_free(lhandle->ctl_pitch);
1349 lhandle->ctl_pitch = NULL;
1350 if (lhandle->ctl)
1351 err = snd_ctl_close(lhandle->ctl);
1352 lhandle->ctl = NULL;
1353 if (lhandle->handle)
1354 err = snd_pcm_close(lhandle->handle);
1355 lhandle->handle = NULL;
1359 static int init_handle(struct loopback_handle *lhandle, int alloc)
1362 lhandle->frame_size = (snd_pcm_format_physical_width(lhandle->format)
1363 / 8) * lhandle->channels;
1364 lhandle->sync_point = lhandle->rate * 15; /* every 15 seconds */
1365 lat = lhandle->loopback->latency;
1366 if (lhandle->buffer_size > lat)
1367 lat = lhandle->buffer_size;
1368 lhandle->buf_size = lat * 2;
1370 lhandle->buf = calloc(1, lhandle->buf_size * lhandle->frame_size);
1371 if (lhandle->buf == NULL)
1474 static void lhandle_start(struct loopback_handle *lhandle)
1476 lhandle->buf_pos = 0;
1477 lhandle->buf_count = 0;
1478 lhandle->counter = 0;
1479 lhandle->total_queued = 0;
1796 static int handle_ctl_events(struct loopback_handle *lhandle,
1799 struct loopback *loop = lhandle->loopback;
1804 while ((err = snd_ctl_read(lhandle->ctl, ev)) != 0 && err != -EAGAIN) {
1809 if (lhandle == loop->play)
1812 snd_output_printf(loop->output, "%s: ctl event!!!! %s\n", lhandle->id, snd_ctl_event_elem_get_name(ev));
1813 if (ctl_event_check(lhandle->ctl_active, ev)) {
1815 } else if (ctl_event_check(lhandle->ctl_format, ev)) {
1816 err = get_format(lhandle);
1817 if (lhandle->format != err)
1820 } else if (ctl_event_check(lhandle->ctl_rate, ev)) {
1821 err = get_rate(lhandle);
1822 if ((int)lhandle->rate != err)
1825 } else if (ctl_event_check(lhandle->ctl_channels, ev)) {
1826 err = get_channels(lhandle);
1827 if ((int)lhandle->channels != err)
1832 control_event(lhandle, ev);
1834 err = get_active(lhandle);
1836 snd_output_printf(loop->output, "%s: ctl event active %i\n", lhandle->id, err);
1838 if (lhandle->loopback->running) {
2062 static void show_handle(struct loopback_handle *lhandle, const char *id)
2064 struct loopback *loop = lhandle->loopback;
2066 OUT(" %s: %s:\n", id, lhandle->id);
2067 OUT(" device = '%s', ctldev '%s'\n", lhandle->device, lhandle->ctldev);
2068 OUT(" card_number = %i\n", lhandle->card_number);
2071 OUT(" access = %s, format = %s, rate = %u, channels = %u\n", snd_pcm_access_name(lhandle->access), snd_pcm_format_name(lhandle->format), lhandle->rate, lhandle->channels);
2072 OUT(" buffer_size = %u, period_size = %u, avail_min = %li\n", lhandle->buffer_size, lhandle->period_size, lhandle->avail_min);
2073 OUT(" xrun_pending = %i\n", lhandle->xrun_pending);
2074 OUT(" buf_size = %li, buf_pos = %li, buf_count = %li, buf_over = %li\n", lhandle->buf_size, lhandle->buf_pos, lhandle->buf_count, lhandle->buf_over);
2075 OUT(" pitch = %.8f\n", lhandle->pitch);