Lines Matching refs:lhandle
39 static int set_rate_shift(struct loopback_handle *lhandle, double pitch);
40 static int get_rate(struct loopback_handle *lhandle);
109 static int setparams_stream(struct loopback_handle *lhandle, in setparams_stream() argument
112 snd_pcm_t *handle = lhandle->handle; in setparams_stream()
118 …logit(LOG_CRIT, "Broken configuration for %s PCM: no configurations available: %s\n", lhandle->id,… in setparams_stream()
121 err = snd_pcm_hw_params_set_rate_resample(handle, params, lhandle->resample); in setparams_stream()
123 …logit(LOG_CRIT, "Resample setup failed for %s (val %u): %s\n", lhandle->id, lhandle->resample, snd… in setparams_stream()
126 err = snd_pcm_hw_params_set_access(handle, params, lhandle->access); in setparams_stream()
128 logit(LOG_CRIT, "Access type not available for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_stream()
131 err = snd_pcm_hw_params_set_format(handle, params, lhandle->format); in setparams_stream()
133 logit(LOG_CRIT, "Sample format not available for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_stream()
136 err = snd_pcm_hw_params_set_channels(handle, params, lhandle->channels); in setparams_stream()
138 …logit(LOG_CRIT, "Channels count (%u) not available for %s: %s\n", lhandle->channels, lhandle->id, … in setparams_stream()
141 rrate = lhandle->rate_req; in setparams_stream()
144 …logit(LOG_CRIT, "Rate %uHz not available for %s: %s\n", lhandle->rate_req, lhandle->id, snd_strerr… in setparams_stream()
149 lhandle->rate = rrate; in setparams_stream()
152 !lhandle->loopback->src_enable && in setparams_stream()
154 (int)rrate != lhandle->rate) { in setparams_stream()
155 …te does not match (requested %uHz, got %uHz, resample %u)\n", lhandle->rate, rrate, lhandle->resam… in setparams_stream()
158 lhandle->pitch = (double)lhandle->rate_req / (double)lhandle->rate; in setparams_stream()
162 static int setparams_bufsize(struct loopback_handle *lhandle, in setparams_bufsize() argument
167 snd_pcm_t *handle = lhandle->handle; in setparams_bufsize()
173 if (lhandle->buffer_size_req > 0) { in setparams_bufsize()
174 bufsize = lhandle->buffer_size_req; in setparams_bufsize()
179 if (lhandle->buffer_size_req > 0) { in setparams_bufsize()
180 …logit(LOG_CRIT, "Unable to set buffer size %li for %s\n", (long)lhandle->buffer_size, lhandle->id); in setparams_bufsize()
195 …logit(LOG_CRIT, "Unable to set buffer size %li for %s: %s\n", periodsize, lhandle->id, snd_strerro… in setparams_bufsize()
200 snd_output_printf(lhandle->loopback->output, "%s: buffer_size=%li\n", lhandle->id, periodsize); in setparams_bufsize()
201 if (lhandle->period_size_req > 0) in setparams_bufsize()
202 periodsize = lhandle->period_size_req; in setparams_bufsize()
207 …logit(LOG_CRIT, "Unable to set period size %li for %s: %s\n", periodsize, lhandle->id, snd_strerro… in setparams_bufsize()
212 snd_output_printf(lhandle->loopback->output, "%s: period_size=%li\n", lhandle->id, periodsize); in setparams_bufsize()
218 lhandle->period_size = periodsize; in setparams_bufsize()
219 lhandle->buffer_size = buffersize; in setparams_bufsize()
223 static int setparams_set(struct loopback_handle *lhandle, in setparams_set() argument
228 snd_pcm_t *handle = lhandle->handle; in setparams_set()
234 logit(LOG_CRIT, "Unable to set hw params for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_set()
239 …logit(LOG_CRIT, "Unable to determine current swparams for %s: %s\n", lhandle->id, snd_strerror(err… in setparams_set()
244 …logit(LOG_CRIT, "Unable to set start threshold mode for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_set()
249 if (lhandle->nblock) { in setparams_set()
250 if (lhandle == lhandle->loopback->play) { in setparams_set()
256 snd_output_printf(lhandle->loopback->output, "%s: avail_min1=%li\n", lhandle->id, val); in setparams_set()
258 if (lhandle == lhandle->loopback->play) { in setparams_set()
269 snd_output_printf(lhandle->loopback->output, "%s: avail_min2=%li\n", lhandle->id, val); in setparams_set()
273 logit(LOG_CRIT, "Unable to set avail min for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_set()
276 snd_pcm_sw_params_get_avail_min(swparams, &lhandle->avail_min); in setparams_set()
279 logit(LOG_CRIT, "Unable to set sw params for %s: %s\n", lhandle->id, snd_strerror(err)); in setparams_set()
285 static int increase_playback_avail_min(struct loopback_handle *lhandle) in increase_playback_avail_min() argument
287 snd_pcm_t *handle = lhandle->handle; in increase_playback_avail_min()
292 if (lhandle->avail_min + (lhandle->period_size / 2) > lhandle->buffer_size) { in increase_playback_avail_min()
302 …logit(LOG_CRIT, "Unable to determine current swparams for %s: %s\n", lhandle->id, snd_strerror(err… in increase_playback_avail_min()
305 err = snd_pcm_sw_params_set_avail_min(handle, swparams, lhandle->avail_min + 4); in increase_playback_avail_min()
307 logit(LOG_CRIT, "Unable to set avail min for %s: %s\n", lhandle->id, snd_strerror(err)); in increase_playback_avail_min()
310 snd_pcm_sw_params_get_avail_min(swparams, &lhandle->avail_min); in increase_playback_avail_min()
312 …snd_output_printf(lhandle->loopback->output, "%s: change avail_min=%li\n", lhandle->id, lhandle->a… in increase_playback_avail_min()
315 logit(LOG_CRIT, "Unable to set sw params for %s: %s\n", lhandle->id, snd_strerror(err)); in increase_playback_avail_min()
486 static inline snd_pcm_uframes_t buf_avail(struct loopback_handle *lhandle) in buf_avail() argument
488 return lhandle->buf_size - lhandle->buf_count; in buf_avail()
643 static int xrun(struct loopback_handle *lhandle) in xrun() argument
647 if (lhandle == lhandle->loopback->play) { in xrun()
648 logit(LOG_DEBUG, "underrun for %s\n", lhandle->id); in xrun()
649 xrun_stats(lhandle->loopback); in xrun()
650 if ((err = snd_pcm_prepare(lhandle->handle)) < 0) in xrun()
652 lhandle->xrun_pending = 1; in xrun()
654 logit(LOG_DEBUG, "overrun for %s\n", lhandle->id); in xrun()
655 xrun_stats(lhandle->loopback); in xrun()
656 if ((err = snd_pcm_prepare(lhandle->handle)) < 0) in xrun()
658 lhandle->xrun_pending = 1; in xrun()
663 static int suspend(struct loopback_handle *lhandle) in suspend() argument
667 while ((err = snd_pcm_resume(lhandle->handle)) == -EAGAIN) in suspend()
670 return xrun(lhandle); in suspend()
674 static int readit(struct loopback_handle *lhandle) in readit() argument
680 avail = snd_pcm_avail_update(lhandle->handle); in readit()
682 return xrun(lhandle); in readit()
684 if ((err = suspend(lhandle)) < 0) in readit()
687 if (avail > buf_avail(lhandle)) { in readit()
688 lhandle->buf_over += avail - buf_avail(lhandle); in readit()
689 avail = buf_avail(lhandle); in readit()
691 if (snd_pcm_state(lhandle->handle) == SND_PCM_STATE_DRAINING) { in readit()
692 lhandle->loopback->reinit = 1; in readit()
697 r = buf_avail(lhandle); in readit()
698 if (r + lhandle->buf_pos > lhandle->buf_size) in readit()
699 r = lhandle->buf_size - lhandle->buf_pos; in readit()
702 r = snd_pcm_readi(lhandle->handle, in readit()
703 lhandle->buf + in readit()
704 lhandle->buf_pos * in readit()
705 lhandle->frame_size, r); in readit()
710 err = xrun(lhandle); in readit()
713 if ((err = suspend(lhandle)) < 0) in readit()
721 if (lhandle->loopback->cfile) in readit()
722 fwrite(lhandle->buf + lhandle->buf_pos * lhandle->frame_size, in readit()
723 r, lhandle->frame_size, lhandle->loopback->cfile); in readit()
726 if (lhandle->max < res) in readit()
727 lhandle->max = res; in readit()
728 lhandle->counter += r; in readit()
729 lhandle->buf_count += r; in readit()
730 lhandle->buf_pos += r; in readit()
731 lhandle->buf_pos %= lhandle->buf_size; in readit()
737 static int writeit(struct loopback_handle *lhandle) in writeit() argument
744 avail = snd_pcm_avail_update(lhandle->handle); in writeit()
746 if ((err = xrun(lhandle)) < 0) in writeit()
750 if ((err = suspend(lhandle)) < 0) in writeit()
754 while (avail > 0 && lhandle->buf_count > 0) { in writeit()
755 r = lhandle->buf_count; in writeit()
756 if (r + lhandle->buf_pos > lhandle->buf_size) in writeit()
757 r = lhandle->buf_size - lhandle->buf_pos; in writeit()
760 r = snd_pcm_writei(lhandle->handle, in writeit()
761 lhandle->buf + in writeit()
762 lhandle->buf_pos * in writeit()
763 lhandle->frame_size, r); in writeit()
766 if ((err = xrun(lhandle)) < 0) in writeit()
774 if (lhandle->loopback->pfile) in writeit()
775 fwrite(lhandle->buf + lhandle->buf_pos * lhandle->frame_size, in writeit()
776 r, lhandle->frame_size, lhandle->loopback->pfile); in writeit()
779 lhandle->counter += r; in writeit()
780 lhandle->buf_count -= r; in writeit()
781 lhandle->buf_pos += r; in writeit()
782 lhandle->buf_pos %= lhandle->buf_size; in writeit()
783 xrun_profile(lhandle->loopback); in writeit()
784 if (lhandle->loopback->stop_pending) { in writeit()
785 lhandle->loopback->stop_count += r; in writeit()
786 if (lhandle->loopback->stop_count * lhandle->pitch > in writeit()
787 lhandle->loopback->latency * 3) { in writeit()
788 lhandle->loopback->stop_pending = 0; in writeit()
789 lhandle->loopback->reinit = 1; in writeit()
1039 static int set_notify(struct loopback_handle *lhandle, int enable) in set_notify() argument
1043 if (lhandle->ctl_notify == NULL) in set_notify()
1045 snd_ctl_elem_value_set_boolean(lhandle->ctl_notify, 0, enable); in set_notify()
1046 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_notify); in set_notify()
1048 logit(LOG_CRIT, "Cannot set PCM Notify element for %s: %s\n", lhandle->id, snd_strerror(err)); in set_notify()
1051 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_notify); in set_notify()
1053 logit(LOG_CRIT, "Cannot get PCM Notify element for %s: %s\n", lhandle->id, snd_strerror(err)); in set_notify()
1059 static int set_rate_shift(struct loopback_handle *lhandle, double pitch) in set_rate_shift() argument
1063 if (lhandle->ctl_rate_shift) { in set_rate_shift()
1064 snd_ctl_elem_value_set_integer(lhandle->ctl_rate_shift, 0, pitch * 100000); in set_rate_shift()
1065 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_rate_shift); in set_rate_shift()
1066 } else if (lhandle->ctl_pitch) { in set_rate_shift()
1068 snd_ctl_elem_value_set_integer(lhandle->ctl_pitch, 0, (1 / pitch) * 1000000); in set_rate_shift()
1069 err = snd_ctl_elem_write(lhandle->ctl, lhandle->ctl_pitch); in set_rate_shift()
1074 logit(LOG_CRIT, "Cannot set PCM Rate Shift element for %s: %s\n", lhandle->id, snd_strerror(err)); in set_rate_shift()
1121 static int get_active(struct loopback_handle *lhandle) in get_active() argument
1125 if (lhandle->ctl_active == NULL) in get_active()
1127 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_active); in get_active()
1129 …logit(LOG_CRIT, "Cannot get PCM Slave Active element for %s: %s\n", lhandle->id, snd_strerror(err)… in get_active()
1132 return snd_ctl_elem_value_get_boolean(lhandle->ctl_active, 0); in get_active()
1135 static int get_format(struct loopback_handle *lhandle) in get_format() argument
1139 if (lhandle->ctl_format == NULL) in get_format()
1141 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_format); in get_format()
1143 logit(LOG_CRIT, "Cannot get PCM Format element for %s: %s\n", lhandle->id, snd_strerror(err)); in get_format()
1146 return snd_ctl_elem_value_get_integer(lhandle->ctl_format, 0); in get_format()
1149 static int get_rate(struct loopback_handle *lhandle) in get_rate() argument
1153 if (lhandle->ctl_rate == NULL) in get_rate()
1155 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_rate); in get_rate()
1157 logit(LOG_CRIT, "Cannot get PCM Rate element for %s: %s\n", lhandle->id, snd_strerror(err)); in get_rate()
1160 return snd_ctl_elem_value_get_integer(lhandle->ctl_rate, 0); in get_rate()
1163 static int get_channels(struct loopback_handle *lhandle) in get_channels() argument
1167 if (lhandle->ctl_channels == NULL) in get_channels()
1169 err = snd_ctl_elem_read(lhandle->ctl, lhandle->ctl_channels); in get_channels()
1171 logit(LOG_CRIT, "Cannot get PCM Channels element for %s: %s\n", lhandle->id, snd_strerror(err)); in get_channels()
1174 return snd_ctl_elem_value_get_integer(lhandle->ctl_channels, 0); in get_channels()
1177 static int openctl_elem_id(struct loopback_handle *lhandle, snd_ctl_elem_id_t *id, in openctl_elem_id() argument
1188 err = snd_ctl_elem_read(lhandle->ctl, *elem); in openctl_elem_id()
1194 snd_output_printf(lhandle->loopback->output, in openctl_elem_id()
1196 snd_ctl_elem_id_get_name(id), snd_ctl_name(lhandle->ctl), in openctl_elem_id()
1203 static int openctl_elem(struct loopback_handle *lhandle, in openctl_elem() argument
1214 return openctl_elem_id(lhandle, id, elem); in openctl_elem()
1217 static int openctl_elem_ascii(struct loopback_handle *lhandle, char *ascii_name, in openctl_elem_ascii() argument
1227 return openctl_elem_id(lhandle, id, elem); in openctl_elem_ascii()
1230 static int openctl(struct loopback_handle *lhandle, int device, int subdevice) in openctl() argument
1234 lhandle->ctl_rate_shift = NULL; in openctl()
1235 if (lhandle->loopback->play == lhandle) { in openctl()
1237 if (lhandle->prateshift_name) { in openctl()
1238 err = openctl_elem_ascii(lhandle, lhandle->prateshift_name, in openctl()
1239 &lhandle->ctl_rate_shift); in openctl()
1242 lhandle->prateshift_name); in openctl()
1246 openctl_elem(lhandle, device, subdevice, "Playback Pitch 1000000", in openctl()
1247 &lhandle->ctl_pitch); in openctl()
1248 set_rate_shift(lhandle, 1); in openctl()
1249 if (lhandle->loopback->controls) in openctl()
1254 openctl_elem(lhandle, device, subdevice, "PCM Notify", in openctl()
1255 &lhandle->ctl_notify); in openctl()
1256 openctl_elem(lhandle, device, subdevice, "PCM Rate Shift 100000", in openctl()
1257 &lhandle->ctl_rate_shift); in openctl()
1258 openctl_elem(lhandle, device, subdevice, "Capture Pitch 1000000", in openctl()
1259 &lhandle->ctl_pitch); in openctl()
1260 set_rate_shift(lhandle, 1); in openctl()
1261 openctl_elem(lhandle, device, subdevice, "PCM Slave Active", in openctl()
1262 &lhandle->ctl_active); in openctl()
1263 openctl_elem(lhandle, device, subdevice, "PCM Slave Format", in openctl()
1264 &lhandle->ctl_format); in openctl()
1265 openctl_elem(lhandle, device, subdevice, "PCM Slave Rate", in openctl()
1266 &lhandle->ctl_rate); in openctl()
1267 openctl_elem(lhandle, device, subdevice, "PCM Slave Channels", in openctl()
1268 &lhandle->ctl_channels); in openctl()
1269 if ((lhandle->ctl_active && in openctl()
1270 lhandle->ctl_format && in openctl()
1271 lhandle->ctl_rate && in openctl()
1272 lhandle->ctl_channels) || in openctl()
1273 lhandle->loopback->controls) { in openctl()
1275 if ((err = snd_ctl_poll_descriptors_count(lhandle->ctl)) < 0) in openctl()
1276 lhandle->ctl_pollfd_count = 0; in openctl()
1278 lhandle->ctl_pollfd_count = err; in openctl()
1279 if (snd_ctl_subscribe_events(lhandle->ctl, 1) < 0) in openctl()
1280 lhandle->ctl_pollfd_count = 0; in openctl()
1285 static int openit(struct loopback_handle *lhandle) in openit() argument
1288 int stream = lhandle == lhandle->loopback->play ? in openit()
1293 err = snd_pcm_open(&lhandle->handle, lhandle->device, stream, SND_PCM_NONBLOCK); in openit()
1296 logit(LOG_CRIT, "%s open error: %s\n", lhandle->id, snd_strerror(err)); in openit()
1301 if ((err = snd_pcm_info(lhandle->handle, info)) < 0) { in openit()
1309 lhandle->card_number = card; in openit()
1310 lhandle->ctl = NULL; in openit()
1311 if (card >= 0 || lhandle->ctldev) { in openit()
1312 char name[16], *dev = lhandle->ctldev; in openit()
1318 err = snd_ctl_open(&lhandle->ctl, dev, SND_CTL_NONBLOCK); in openit()
1321 logit(LOG_CRIT, "%s [%s] ctl open error: %s\n", lhandle->id, dev, snd_strerror(err)); in openit()
1322 lhandle->ctl = NULL; in openit()
1324 if (lhandle->ctl) in openit()
1325 openctl(lhandle, device, subdevice); in openit()
1330 static int freeit(struct loopback_handle *lhandle) in freeit() argument
1332 free(lhandle->buf); in freeit()
1333 lhandle->buf = NULL; in freeit()
1337 static int closeit(struct loopback_handle *lhandle) in closeit() argument
1341 set_rate_shift(lhandle, 1); in closeit()
1342 if (lhandle->ctl_rate_shift) in closeit()
1343 snd_ctl_elem_value_free(lhandle->ctl_rate_shift); in closeit()
1344 lhandle->ctl_rate_shift = NULL; in closeit()
1345 if (lhandle->ctl_pitch) in closeit()
1346 snd_ctl_elem_value_free(lhandle->ctl_pitch); in closeit()
1347 lhandle->ctl_pitch = NULL; in closeit()
1348 if (lhandle->ctl) in closeit()
1349 err = snd_ctl_close(lhandle->ctl); in closeit()
1350 lhandle->ctl = NULL; in closeit()
1351 if (lhandle->handle) in closeit()
1352 err = snd_pcm_close(lhandle->handle); in closeit()
1353 lhandle->handle = NULL; in closeit()
1357 static int init_handle(struct loopback_handle *lhandle, int alloc) in init_handle() argument
1360 lhandle->frame_size = (snd_pcm_format_physical_width(lhandle->format) in init_handle()
1361 / 8) * lhandle->channels; in init_handle()
1362 lhandle->sync_point = lhandle->rate * 15; /* every 15 seconds */ in init_handle()
1363 lat = lhandle->loopback->latency; in init_handle()
1364 if (lhandle->buffer_size > lat) in init_handle()
1365 lat = lhandle->buffer_size; in init_handle()
1366 lhandle->buf_size = lat * 2; in init_handle()
1368 lhandle->buf = calloc(1, lhandle->buf_size * lhandle->frame_size); in init_handle()
1369 if (lhandle->buf == NULL) in init_handle()
1472 static void lhandle_start(struct loopback_handle *lhandle) in lhandle_start() argument
1474 lhandle->buf_pos = 0; in lhandle_start()
1475 lhandle->buf_count = 0; in lhandle_start()
1476 lhandle->counter = 0; in lhandle_start()
1477 lhandle->total_queued = 0; in lhandle_start()
1794 static int handle_ctl_events(struct loopback_handle *lhandle, in handle_ctl_events() argument
1797 struct loopback *loop = lhandle->loopback; in handle_ctl_events()
1802 while ((err = snd_ctl_read(lhandle->ctl, ev)) != 0 && err != -EAGAIN) { in handle_ctl_events()
1807 if (lhandle == loop->play) in handle_ctl_events()
1810 …snd_output_printf(loop->output, "%s: ctl event!!!! %s\n", lhandle->id, snd_ctl_event_elem_get_name… in handle_ctl_events()
1811 if (ctl_event_check(lhandle->ctl_active, ev)) { in handle_ctl_events()
1813 } else if (ctl_event_check(lhandle->ctl_format, ev)) { in handle_ctl_events()
1814 err = get_format(lhandle); in handle_ctl_events()
1815 if (lhandle->format != err) in handle_ctl_events()
1818 } else if (ctl_event_check(lhandle->ctl_rate, ev)) { in handle_ctl_events()
1819 err = get_rate(lhandle); in handle_ctl_events()
1820 if (lhandle->rate != err) in handle_ctl_events()
1823 } else if (ctl_event_check(lhandle->ctl_channels, ev)) { in handle_ctl_events()
1824 err = get_channels(lhandle); in handle_ctl_events()
1825 if (lhandle->channels != err) in handle_ctl_events()
1830 control_event(lhandle, ev); in handle_ctl_events()
1832 err = get_active(lhandle); in handle_ctl_events()
1834 snd_output_printf(loop->output, "%s: ctl event active %i\n", lhandle->id, err); in handle_ctl_events()
1836 if (lhandle->loopback->running) { in handle_ctl_events()
2060 static void show_handle(struct loopback_handle *lhandle, const char *id) in show_handle() argument
2062 struct loopback *loop = lhandle->loopback; in show_handle()
2064 OUT(" %s: %s:\n", id, lhandle->id); in show_handle()
2065 OUT(" device = '%s', ctldev '%s'\n", lhandle->device, lhandle->ctldev); in show_handle()
2066 OUT(" card_number = %i\n", lhandle->card_number); in show_handle()
2069 …hannels = %u\n", snd_pcm_access_name(lhandle->access), snd_pcm_format_name(lhandle->format), lhand… in show_handle()
2070 …r_size = %u, period_size = %u, avail_min = %li\n", lhandle->buffer_size, lhandle->period_size, lha… in show_handle()
2071 OUT(" xrun_pending = %i\n", lhandle->xrun_pending); in show_handle()
2072 … = %li, buf_count = %li, buf_over = %li\n", lhandle->buf_size, lhandle->buf_pos, lhandle->buf_coun… in show_handle()
2073 OUT(" pitch = %.8f\n", lhandle->pitch); in show_handle()