diff --git a/src/a2dp-aac.c b/src/a2dp-aac.c index ec24c9a31..5b10914a8 100644 --- a/src/a2dp-aac.c +++ b/src/a2dp-aac.c @@ -230,8 +230,7 @@ void *a2dp_aac_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { in_args.numInSamples = -1; @@ -248,7 +247,6 @@ void *a2dp_aac_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); while ((in_args.numInSamples = ffb_len_out(&pcm)) > 0) { if ((err = aacEncEncode(handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) @@ -386,8 +384,9 @@ void *a2dp_aac_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-aptx-hd.c b/src/a2dp-aptx-hd.c index 76f5fbeea..d21f0d7db 100644 --- a/src/a2dp-aptx-hd.c +++ b/src/a2dp-aptx-hd.c @@ -78,8 +78,7 @@ void *a2dp_aptx_hd_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { ffb_rewind(&pcm); @@ -92,10 +91,8 @@ void *a2dp_aptx_hd_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - - int32_t *input = pcm.data; + const int32_t *input = pcm.data; + const size_t samples = ffb_len_out(&pcm); size_t input_samples = samples; /* encode and transfer obtained data */ @@ -210,8 +207,9 @@ void *a2dp_aptx_hd_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-aptx.c b/src/a2dp-aptx.c index 1b7774eb0..2f6f5c332 100644 --- a/src/a2dp-aptx.c +++ b/src/a2dp-aptx.c @@ -67,8 +67,7 @@ void *a2dp_aptx_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { ffb_rewind(&pcm); @@ -81,10 +80,8 @@ void *a2dp_aptx_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - - int16_t *input = pcm.data; + const int16_t *input = pcm.data; + const size_t samples = ffb_len_out(&pcm); size_t input_samples = samples; /* encode and transfer obtained data */ @@ -184,8 +181,9 @@ void *a2dp_aptx_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-faststream.c b/src/a2dp-faststream.c index 4113a0037..9ab8b0ff6 100644 --- a/src/a2dp-faststream.c +++ b/src/a2dp-faststream.c @@ -70,8 +70,7 @@ void *a2dp_faststream_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { sbc_reinit_a2dp_faststream(&sbc, 0, configuration, @@ -86,11 +85,8 @@ void *a2dp_faststream_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - const int16_t *input = pcm.data; - size_t input_len = samples; + size_t input_len = ffb_len_out(&pcm); size_t output_len = ffb_len_in(&bt); size_t pcm_frames = 0; size_t sbc_frames = 0; @@ -140,7 +136,7 @@ void *a2dp_faststream_enc_thread(struct ba_transport_pcm *t_pcm) { * have to append new data to the existing one. Since we do not use * ring buffer, we will simply move unprocessed data to the front * of our linear buffer. */ - ffb_shift(&pcm, samples - input_len); + ffb_shift(&pcm, pcm_frames * channels); } @@ -194,8 +190,9 @@ void *a2dp_faststream_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.tail, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-lc3plus.c b/src/a2dp-lc3plus.c index cfa25a59b..cf15d2bab 100644 --- a/src/a2dp-lc3plus.c +++ b/src/a2dp-lc3plus.c @@ -187,8 +187,7 @@ void *a2dp_lc3plus_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { int encoded = 0; @@ -207,14 +206,11 @@ void *a2dp_lc3plus_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - /* anchor for RTP payload */ bt.tail = rtp_payload; const int32_t *input = pcm.data; - size_t input_samples = samples; + size_t input_samples = ffb_len_out(&pcm); size_t output_len = ffb_len_in(&bt); size_t pcm_frames = 0; size_t lc3plus_frames = 0; @@ -306,7 +302,7 @@ void *a2dp_lc3plus_enc_thread(struct ba_transport_pcm *t_pcm) { * have to append new data to the existing one. Since we do not use * ring buffer, we will simply move unprocessed data to the front * of our linear buffer. */ - ffb_shift(&pcm, samples - input_samples); + ffb_shift(&pcm, pcm_frames * channels); } @@ -397,8 +393,9 @@ void *a2dp_lc3plus_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-ldac.c b/src/a2dp-ldac.c index 1a98ba06a..4c6befbe4 100644 --- a/src/a2dp-ldac.c +++ b/src/a2dp-ldac.c @@ -105,8 +105,7 @@ void *a2dp_ldac_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { int tmp; @@ -122,10 +121,8 @@ void *a2dp_ldac_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - int16_t *input = pcm.data; + size_t samples = ffb_len_out(&pcm); size_t input_len = samples; /* encode and transfer obtained data */ @@ -259,8 +256,9 @@ void *a2dp_ldac_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-mpeg.c b/src/a2dp-mpeg.c index c63197ab3..f51ae1891 100644 --- a/src/a2dp-mpeg.c +++ b/src/a2dp-mpeg.c @@ -164,8 +164,7 @@ void *a2dp_mp3_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { lame_encode_flush(handle, rtp_payload, mpeg_frame_len); @@ -179,13 +178,10 @@ void *a2dp_mp3_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - /* anchor for RTP payload */ bt.tail = rtp_payload; - size_t pcm_frames = samples / channels; + size_t pcm_frames = ffb_len_out(&pcm) / channels; ssize_t len; if ((len = channels == 1 ? @@ -347,8 +343,9 @@ void *a2dp_mpeg_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/a2dp-sbc.c b/src/a2dp-sbc.c index d74b3179f..7044813f6 100644 --- a/src/a2dp-sbc.c +++ b/src/a2dp-sbc.c @@ -108,8 +108,7 @@ void *a2dp_sbc_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &pcm)) { case -1: if (errno == ESTALE) { sbc_reinit_a2dp(&sbc, 0, configuration, sizeof(*configuration)); @@ -125,14 +124,11 @@ void *a2dp_sbc_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&pcm, samples); - samples = ffb_len_out(&pcm); - /* anchor for RTP payload */ bt.tail = rtp_payload; const int16_t *input = pcm.data; - size_t input_samples = samples; + size_t input_samples = ffb_len_out(&pcm); size_t output_len = ffb_len_in(&bt); size_t pcm_frames = 0; size_t sbc_frames = 0; @@ -188,7 +184,7 @@ void *a2dp_sbc_enc_thread(struct ba_transport_pcm *t_pcm) { * have to append new data to the existing one. Since we do not use * ring buffer, we will simply move unprocessed data to the front * of our linear buffer. */ - ffb_shift(&pcm, samples - input_samples); + ffb_shift(&pcm, pcm_frames * channels); } @@ -252,8 +248,9 @@ void *a2dp_sbc_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail; diff --git a/src/io.c b/src/io.c index 51753ff36..3cd2e0391 100644 --- a/src/io.c +++ b/src/io.c @@ -24,6 +24,7 @@ #include "audio.h" #include "ba-config.h" #include "shared/defs.h" +#include "shared/ffb.h" #include "shared/log.h" /** @@ -265,13 +266,6 @@ ssize_t io_pcm_write( return ret; } -static enum ba_transport_pcm_signal io_poll_signal_filter_none( - enum ba_transport_pcm_signal signal, - void *userdata) { - (void)userdata; - return signal; -} - /** * Poll and read data from the BT transport socket. * @@ -280,8 +274,7 @@ static enum ba_transport_pcm_signal io_poll_signal_filter_none( ssize_t io_poll_and_read_bt( struct io_poll *io, struct ba_transport_pcm *pcm, - void *buffer, - size_t count) { + ffb_t *buffer) { struct pollfd fds[] = { { pcm->pipe[0], POLLIN, 0 }, @@ -299,17 +292,16 @@ ssize_t io_poll_and_read_bt( return -1; } - if (fds[0].revents & POLLIN) { - /* dispatch incoming event */ - io_poll_signal_filter *filter = io->signal.filter != NULL ? - io->signal.filter : io_poll_signal_filter_none; - switch (filter(ba_transport_pcm_signal_recv(pcm), io->signal.userdata)) { + if (fds[0].revents & POLLIN) + switch (ba_transport_pcm_signal_recv(pcm)) { default: goto repoll; } - } - return io_bt_read(pcm, buffer, count); + ssize_t len; + if ((len = io_bt_read(pcm, buffer->tail, ffb_blen_in(buffer))) > 0) + ffb_seek(buffer, len); + return len; } /** @@ -320,8 +312,7 @@ ssize_t io_poll_and_read_bt( ssize_t io_poll_and_read_pcm( struct io_poll *io, struct ba_transport_pcm *pcm, - void *buffer, - size_t samples) { + ffb_t *buffer) { struct pollfd fds[] = { { pcm->pipe[0], POLLIN, 0 }, @@ -353,11 +344,8 @@ ssize_t io_poll_and_read_pcm( return -1; } - if (fds[0].revents & POLLIN) { - /* dispatch incoming event */ - io_poll_signal_filter *filter = io->signal.filter != NULL ? - io->signal.filter : io_poll_signal_filter_none; - switch (filter(ba_transport_pcm_signal_recv(pcm), io->signal.userdata)) { + if (fds[0].revents & POLLIN) + switch (ba_transport_pcm_signal_recv(pcm)) { case BA_TRANSPORT_PCM_SIGNAL_OPEN: case BA_TRANSPORT_PCM_SIGNAL_RESUME: io->asrs.frames = 0; @@ -377,21 +365,20 @@ ssize_t io_poll_and_read_pcm( default: goto repoll; } - } if (fds[1].revents == 0) return 0; - ssize_t samples_read; - if ((samples_read = io_pcm_read(pcm, buffer, samples)) == -1) { + ssize_t samples; + if ((samples = io_pcm_read(pcm, buffer->tail, ffb_len_in(buffer))) == -1) { if (errno == EAGAIN) goto repoll; if (errno != EBADF) return -1; - samples_read = 0; + samples = 0; } - if (samples_read == 0) + if (samples == 0) return 0; /* When the thread is created, there might be no data in the FIFO. In fact @@ -401,5 +388,6 @@ ssize_t io_poll_and_read_pcm( if (io->asrs.frames == 0) asrsync_init(&io->asrs, pcm->sampling); - return samples_read; + ffb_seek(buffer, samples); + return samples; } diff --git a/src/io.h b/src/io.h index 716da7775..d619e3976 100644 --- a/src/io.h +++ b/src/io.h @@ -1,6 +1,6 @@ /* * BlueALSA - io.h - * Copyright (c) 2016-2023 Arkadiusz Bokowy + * Copyright (c) 2016-2024 Arkadiusz Bokowy * * This file is a part of bluez-alsa. * @@ -19,25 +19,15 @@ #include #include "ba-transport-pcm.h" +#include "shared/ffb.h" #include "shared/rt.h" -/** - * Callback function for thread signal filtering. */ -typedef enum ba_transport_pcm_signal io_poll_signal_filter( - enum ba_transport_pcm_signal signal, - void *userdata); - /** * Data associated with IO polling. * * Note: * The timeout field shall be initialized to -1. */ struct io_poll { - struct { - /* signal filtering callback */ - io_poll_signal_filter *filter; - void *userdata; - } signal; /* transfer bit rate synchronization */ struct asrsync asrs; /* keep-alive and sync timeout */ @@ -75,13 +65,11 @@ ssize_t io_pcm_write( ssize_t io_poll_and_read_bt( struct io_poll *io, struct ba_transport_pcm *pcm, - void *buffer, - size_t count); + ffb_t *buffer); ssize_t io_poll_and_read_pcm( struct io_poll *io, struct ba_transport_pcm *pcm, - void *buffer, - size_t samples); + ffb_t *buffer); #endif diff --git a/src/sco-cvsd.c b/src/sco-cvsd.c index 20cf5549e..a61d24b1f 100644 --- a/src/sco-cvsd.c +++ b/src/sco-cvsd.c @@ -47,8 +47,7 @@ void *sco_cvsd_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&buffer); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, buffer.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &buffer)) { case -1: if (errno == ESTALE) { ffb_rewind(&buffer); @@ -61,10 +60,8 @@ void *sco_cvsd_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&buffer, samples); - samples = ffb_len_out(&buffer); - const int16_t *input = buffer.data; + const size_t samples = ffb_len_out(&buffer); size_t input_samples = samples; while (input_samples >= mtu_samples) { @@ -106,13 +103,11 @@ void *sco_cvsd_dec_thread(struct ba_transport_pcm *t_pcm) { struct ba_transport *t = t_pcm->t; struct io_poll io = { .timeout = -1 }; - const size_t mtu_samples = t->mtu_read / sizeof(int16_t); - const size_t mtu_samples_multiplier = 2; - ffb_t buffer = { 0 }; pthread_cleanup_push(PTHREAD_CLEANUP(ffb_free), &buffer); - if (ffb_init_int16_t(&buffer, mtu_samples * mtu_samples_multiplier) == -1) { + const size_t mtu_read_multiplier = 3; + if (ffb_init_uint8_t(&buffer, t->mtu_read * mtu_read_multiplier) == -1) { error("Couldn't create data buffers: %s", strerror(errno)); goto fail_ffb; } @@ -120,27 +115,19 @@ void *sco_cvsd_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&buffer); - if ((len = io_poll_and_read_bt(&io, t_pcm, buffer.tail, len)) == -1) + ssize_t len; + if ((len = io_poll_and_read_bt(&io, t_pcm, &buffer)) == -1) error("BT poll and read error: %s", strerror(errno)); else if (len == 0) goto exit; - if ((size_t)len == buffer.nmemb * buffer.size) { - debug("Resizing CVSD read buffer: %zd -> %zd", - buffer.nmemb * buffer.size, buffer.nmemb * 2 * buffer.size); - if (ffb_init_int16_t(&buffer, buffer.nmemb * 2) == -1) - error("Couldn't resize CVSD read buffer: %s", strerror(errno)); - } - - if (!ba_transport_pcm_is_active(t_pcm)) + if (!ba_transport_pcm_is_active(t_pcm)) { + ffb_rewind(&buffer); continue; - - if (len > 0) - ffb_seek(&buffer, len / buffer.size); + } ssize_t samples; - if ((samples = ffb_len_out(&buffer)) <= 0) + if ((samples = ffb_blen_out(&buffer) / sizeof(int16_t)) <= 0) continue; io_pcm_scale(t_pcm, buffer.data, samples); @@ -149,7 +136,7 @@ void *sco_cvsd_dec_thread(struct ba_transport_pcm *t_pcm) { else if (samples == 0) ba_transport_stop_if_no_clients(t); - ffb_shift(&buffer, samples); + ffb_shift(&buffer, samples * sizeof(int16_t)); } diff --git a/src/sco-lc3-swb.c b/src/sco-lc3-swb.c index 17d61bcc4..507f6ef29 100644 --- a/src/sco-lc3-swb.c +++ b/src/sco-lc3-swb.c @@ -44,8 +44,7 @@ void *sco_lc3_swb_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&codec.pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, codec.pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &codec.pcm)) { case -1: if (errno == ESTALE) { /* reinitialize LC3-SWB encoder */ @@ -59,8 +58,6 @@ void *sco_lc3_swb_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&codec.pcm, samples); - /* encode as much PCM data as possible */ while (lc3_swb_encode(&codec) > 0) { @@ -115,17 +112,16 @@ void *sco_lc3_swb_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&codec.data); - if ((len = io_poll_and_read_bt(&io, t_pcm, codec.data.tail, len)) == -1) + ssize_t len; + if ((len = io_poll_and_read_bt(&io, t_pcm, &codec.data)) == -1) error("BT poll and read error: %s", strerror(errno)); else if (len == 0) goto exit; - if (!ba_transport_pcm_is_active(t_pcm)) + if (!ba_transport_pcm_is_active(t_pcm)) { + ffb_rewind(&codec.data); continue; - - if (len > 0) - ffb_seek(&codec.data, len); + } int err; /* Process data until there is no more LC3-SWB frames to decode. This loop diff --git a/src/sco-msbc.c b/src/sco-msbc.c index 4533b2a74..c83eb8045 100644 --- a/src/sco-msbc.c +++ b/src/sco-msbc.c @@ -46,8 +46,7 @@ void *sco_msbc_enc_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t samples = ffb_len_in(&msbc.pcm); - switch (samples = io_poll_and_read_pcm(&io, t_pcm, msbc.pcm.tail, samples)) { + switch (io_poll_and_read_pcm(&io, t_pcm, &msbc.pcm)) { case -1: if (errno == ESTALE) { /* reinitialize mSBC encoder */ @@ -61,8 +60,6 @@ void *sco_msbc_enc_thread(struct ba_transport_pcm *t_pcm) { continue; } - ffb_seek(&msbc.pcm, samples); - while (ffb_len_out(&msbc.pcm) >= MSBC_CODESAMPLES) { int err; @@ -129,17 +126,16 @@ void *sco_msbc_dec_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&msbc.data); - if ((len = io_poll_and_read_bt(&io, t_pcm, msbc.data.tail, len)) == -1) + ssize_t len; + if ((len = io_poll_and_read_bt(&io, t_pcm, &msbc.data)) == -1) error("BT poll and read error: %s", strerror(errno)); else if (len == 0) goto exit; - if (!ba_transport_pcm_is_active(t_pcm)) + if (!ba_transport_pcm_is_active(t_pcm)) { + ffb_rewind(&msbc.data); continue; - - if (len > 0) - ffb_seek(&msbc.data, len); + } int err; /* Process data until there is no more mSBC frames to decode. This loop diff --git a/test/inc/btd.inc b/test/inc/btd.inc index 0d99b6fe4..fa0fb78d4 100644 --- a/test/inc/btd.inc +++ b/test/inc/btd.inc @@ -279,8 +279,9 @@ void *bt_dump_io_thread(struct ba_transport_pcm *t_pcm) { debug_transport_pcm_thread_loop(t_pcm, "START"); for (ba_transport_pcm_state_set_running(t_pcm);;) { - ssize_t len = ffb_blen_in(&bt); - if ((len = io_poll_and_read_bt(&io, t_pcm, bt.data, len)) <= 0) { + ssize_t len; + ffb_rewind(&bt); + if ((len = io_poll_and_read_bt(&io, t_pcm, &bt)) <= 0) { if (len == -1) error("BT poll and read error: %s", strerror(errno)); goto fail;