Skip to content

Commit

Permalink
Simplify internal API for IO poll and read
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Feb 27, 2024
1 parent 16058dc commit 68893c2
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 154 deletions.
9 changes: 4 additions & 5 deletions src/a2dp-aac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions src/a2dp-aptx-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 6 additions & 8 deletions src/a2dp-aptx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/a2dp-faststream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/a2dp-lc3plus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/a2dp-ldac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 5 additions & 8 deletions src/a2dp-mpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ?
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 6 additions & 9 deletions src/a2dp-sbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 68893c2

Please sign in to comment.