mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
bluez5: clean up SCO code a bit
Minor cleanup in variable naming etc.
This commit is contained in:
parent
45ff965f33
commit
ebde25fe58
2 changed files with 74 additions and 78 deletions
|
|
@ -141,13 +141,15 @@ struct impl {
|
|||
uint64_t prev_flush_time;
|
||||
uint64_t next_flush_time;
|
||||
|
||||
/* mSBC */
|
||||
sbc_t msbc;
|
||||
/* Codecs */
|
||||
uint8_t *buffer;
|
||||
uint8_t *buffer_head;
|
||||
uint8_t *buffer_next;
|
||||
int buffer_size;
|
||||
int msbc_seq;
|
||||
int h2_seq;
|
||||
|
||||
/* mSBC */
|
||||
sbc_t msbc;
|
||||
|
||||
/* LC3 */
|
||||
#ifdef HAVE_LC3
|
||||
|
|
@ -406,12 +408,9 @@ static int flush_data(struct impl *this)
|
|||
if (this->transport == NULL || this->transport->sco_io == NULL || !this->flush_timer_source.loop)
|
||||
return -EIO;
|
||||
|
||||
const bool transp_codec = (this->transport->codec == HFP_AUDIO_CODEC_MSBC) ||
|
||||
(this->transport->codec == HFP_AUDIO_CODEC_LC3);
|
||||
const uint32_t min_in_size = (this->transport->codec == HFP_AUDIO_CODEC_MSBC) ? MSBC_DECODED_SIZE :
|
||||
(this->transport->codec == HFP_AUDIO_CODEC_LC3) ? LC3_SWB_DECODED_SIZE :
|
||||
this->transport->write_mtu;
|
||||
uint8_t * const packet = transp_codec ? this->buffer_head : port->write_buffer;
|
||||
const uint32_t packet_samples = min_in_size / port->frame_size;
|
||||
const uint64_t packet_time = (uint64_t)packet_samples * SPA_NSEC_PER_SEC
|
||||
/ port->current_format.info.raw.rate;
|
||||
|
|
@ -466,7 +465,8 @@ static int flush_data(struct impl *this)
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (transp_codec) {
|
||||
if (this->transport->codec == HFP_AUDIO_CODEC_MSBC ||
|
||||
this->transport->codec == HFP_AUDIO_CODEC_LC3) {
|
||||
uint32_t encoded_size = (this->transport->codec == HFP_AUDIO_CODEC_MSBC) ? MSBC_ENCODED_SIZE :
|
||||
LC3_SWB_ENCODED_SIZE;
|
||||
ssize_t out_encoded;
|
||||
|
|
@ -478,10 +478,11 @@ static int flush_data(struct impl *this)
|
|||
spa_log_warn(this->log, "sco-sink: mSBC/LC3 buffer overrun, dropping data");
|
||||
}
|
||||
|
||||
/* H2 sync header */
|
||||
this->buffer_next[0] = 0x01;
|
||||
this->buffer_next[1] = sntable[this->msbc_seq % 4];
|
||||
this->buffer_next[1] = sntable[this->h2_seq % 4];
|
||||
this->buffer_next[59] = 0x00;
|
||||
this->msbc_seq = (this->msbc_seq + 1) % 4;
|
||||
this->h2_seq = (this->h2_seq + 1) % 4;
|
||||
|
||||
if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) {
|
||||
processed = sbc_encode(&this->msbc, port->write_buffer, port->write_buffer_size,
|
||||
|
|
@ -500,7 +501,7 @@ static int flush_data(struct impl *this)
|
|||
port->write_buffer_size = 0;
|
||||
|
||||
/* Write */
|
||||
written = spa_bt_sco_io_write(this->transport->sco_io, packet,
|
||||
written = spa_bt_sco_io_write(this->transport->sco_io, this->buffer_head,
|
||||
this->buffer_next - this->buffer_head);
|
||||
if (written < 0) {
|
||||
spa_log_warn(this->log, "failed to write data: %d (%s)",
|
||||
|
|
@ -522,7 +523,7 @@ static int flush_data(struct impl *this)
|
|||
this->buffer_head = this->buffer;
|
||||
}
|
||||
} else {
|
||||
written = spa_bt_sco_io_write(this->transport->sco_io, packet,
|
||||
written = spa_bt_sco_io_write(this->transport->sco_io, port->write_buffer,
|
||||
port->write_buffer_size);
|
||||
if (written < 0) {
|
||||
spa_log_warn(this->log, "sco-sink: write failure: %d (%s)",
|
||||
|
|
@ -692,7 +693,6 @@ static int lcm(int a, int b) {
|
|||
|
||||
static int transport_start(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->port;
|
||||
int res;
|
||||
|
||||
/* Don't do anything if the node has already started */
|
||||
|
|
@ -723,12 +723,6 @@ static int transport_start(struct impl *this)
|
|||
* is going to happen.
|
||||
*/
|
||||
this->buffer_size = lcm(24, lcm(60, lcm(this->transport->write_mtu, 2 * MSBC_ENCODED_SIZE)));
|
||||
this->buffer = calloc(this->buffer_size, sizeof(uint8_t));
|
||||
this->buffer_head = this->buffer_next = this->buffer;
|
||||
if (this->buffer == NULL) {
|
||||
res = -errno;
|
||||
goto fail;
|
||||
}
|
||||
} else if (this->transport->codec == HFP_AUDIO_CODEC_LC3) {
|
||||
#ifdef HAVE_LC3
|
||||
this->lc3 = lc3_setup_encoder(7500, 32000, 0,
|
||||
|
|
@ -736,19 +730,24 @@ static int transport_start(struct impl *this)
|
|||
if (!this->lc3)
|
||||
return -EINVAL;
|
||||
|
||||
spa_assert(lc3_frame_samples(7500, 32000) * port->frame_size == LC3_SWB_DECODED_SIZE);
|
||||
spa_assert(lc3_frame_samples(7500, 32000) * this->port.frame_size == LC3_SWB_DECODED_SIZE);
|
||||
|
||||
this->buffer_size = lcm(24, lcm(60, lcm(this->transport->write_mtu, 2 * LC3_SWB_ENCODED_SIZE)));
|
||||
#else
|
||||
res = -EOPNOTSUPP;
|
||||
goto fail;
|
||||
#endif
|
||||
} else {
|
||||
this->buffer_size = 0;
|
||||
}
|
||||
|
||||
if (this->buffer_size) {
|
||||
this->buffer = calloc(this->buffer_size, sizeof(uint8_t));
|
||||
this->buffer_head = this->buffer_next = this->buffer;
|
||||
if (this->buffer == NULL) {
|
||||
res = -errno;
|
||||
goto fail;
|
||||
}
|
||||
#else
|
||||
res = -EOPNOTSUPP;
|
||||
goto fail;
|
||||
#endif
|
||||
}
|
||||
|
||||
spa_return_val_if_fail(this->transport->write_mtu <= sizeof(this->port.write_buffer), -EINVAL);
|
||||
|
|
|
|||
|
|
@ -133,14 +133,16 @@ struct impl {
|
|||
uint64_t current_time;
|
||||
uint64_t next_time;
|
||||
|
||||
/* mSBC */
|
||||
sbc_t msbc;
|
||||
bool msbc_seq_initialized;
|
||||
uint8_t msbc_seq;
|
||||
/* Codecs */
|
||||
bool h2_seq_initialized;
|
||||
uint8_t h2_seq;
|
||||
|
||||
/* mSBC/LC3 frame parsing */
|
||||
uint8_t msbc_buffer[MSBC_ENCODED_SIZE + LC3_SWB_ENCODED_SIZE];
|
||||
uint8_t msbc_buffer_pos;
|
||||
uint8_t recv_buffer[MSBC_ENCODED_SIZE + LC3_SWB_ENCODED_SIZE];
|
||||
uint8_t recv_buffer_pos;
|
||||
|
||||
/* mSBC */
|
||||
sbc_t msbc;
|
||||
|
||||
/* LC3 */
|
||||
#ifdef HAVE_LC3
|
||||
|
|
@ -354,61 +356,56 @@ static void recycle_buffer(struct impl *this, struct port *port, uint32_t buffer
|
|||
}
|
||||
}
|
||||
|
||||
/* Append data to msbc buffer, syncing buffer start to frame headers */
|
||||
static void msbc_buffer_append_byte(struct impl *this, uint8_t byte)
|
||||
/* Append data to recv buffer, syncing buffer start to headers */
|
||||
static void recv_buffer_append_byte(struct impl *this, uint8_t byte)
|
||||
{
|
||||
/* Parse mSBC frame header */
|
||||
if (this->msbc_buffer_pos == 0) {
|
||||
unsigned int encoded_size = (this->transport->codec == HFP_AUDIO_CODEC_MSBC) ? MSBC_ENCODED_SIZE :
|
||||
LC3_SWB_ENCODED_SIZE;
|
||||
|
||||
/* Parse H2 sync header */
|
||||
if (this->recv_buffer_pos == 0) {
|
||||
if (byte != 0x01) {
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->msbc_buffer_pos == 1) {
|
||||
} else if (this->recv_buffer_pos == 1) {
|
||||
if (!((byte & 0x0F) == 0x08 &&
|
||||
((byte >> 4) & 1) == ((byte >> 5) & 1) &&
|
||||
((byte >> 6) & 1) == ((byte >> 7) & 1))) {
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) {
|
||||
if (this->msbc_buffer_pos == 2) {
|
||||
/* .. and beginning of MSBC frame: SYNCWORD + 2 nul bytes */
|
||||
} else if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) {
|
||||
/* Beginning of MSBC frame: SYNCWORD + 2 nul bytes */
|
||||
if (this->recv_buffer_pos == 2) {
|
||||
if (byte != 0xAD) {
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->msbc_buffer_pos == 3) {
|
||||
else if (this->recv_buffer_pos == 3) {
|
||||
if (byte != 0x00) {
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->msbc_buffer_pos == 4) {
|
||||
else if (this->recv_buffer_pos == 4) {
|
||||
if (byte != 0x00) {
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->msbc_buffer_pos >= MSBC_ENCODED_SIZE) {
|
||||
/* Packet completed. Reset. */
|
||||
this->msbc_buffer_pos = 0;
|
||||
msbc_buffer_append_byte(this, byte);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (this->transport->codec == HFP_AUDIO_CODEC_LC3) {
|
||||
if (this->msbc_buffer_pos >= LC3_SWB_ENCODED_SIZE) {
|
||||
/* Packet completed. Reset. */
|
||||
this->msbc_buffer_pos = 0;
|
||||
msbc_buffer_append_byte(this, byte);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->recv_buffer_pos >= encoded_size) {
|
||||
/* Packet completed. Reset. */
|
||||
this->recv_buffer_pos = 0;
|
||||
recv_buffer_append_byte(this, byte);
|
||||
return;
|
||||
}
|
||||
this->msbc_buffer[this->msbc_buffer_pos] = byte;
|
||||
++this->msbc_buffer_pos;
|
||||
|
||||
this->recv_buffer[this->recv_buffer_pos] = byte;
|
||||
++this->recv_buffer_pos;
|
||||
}
|
||||
|
||||
/* Helper function for debugging */
|
||||
|
|
@ -490,9 +487,9 @@ static uint32_t preprocess_and_decode_codec_data(void *userdata, uint8_t *read_d
|
|||
int seq, processed;
|
||||
size_t written;
|
||||
|
||||
msbc_buffer_append_byte(this, read_data[i]);
|
||||
recv_buffer_append_byte(this, read_data[i]);
|
||||
|
||||
if (this->msbc_buffer_pos != encoded_size)
|
||||
if (this->recv_buffer_pos != encoded_size)
|
||||
continue;
|
||||
|
||||
/*
|
||||
|
|
@ -502,21 +499,21 @@ static uint32_t preprocess_and_decode_codec_data(void *userdata, uint8_t *read_d
|
|||
buf = spa_bt_decode_buffer_get_write(&port->buffer, &avail);
|
||||
|
||||
/* Check sequence number */
|
||||
seq = ((this->msbc_buffer[1] >> 4) & 1) |
|
||||
((this->msbc_buffer[1] >> 6) & 2);
|
||||
seq = ((this->recv_buffer[1] >> 4) & 1) |
|
||||
((this->recv_buffer[1] >> 6) & 2);
|
||||
|
||||
spa_log_trace(this->log, "mSBC/LC3 packet seq=%u", seq);
|
||||
if (!this->msbc_seq_initialized) {
|
||||
this->msbc_seq_initialized = true;
|
||||
this->msbc_seq = seq;
|
||||
} else if (seq != this->msbc_seq) {
|
||||
if (!this->h2_seq_initialized) {
|
||||
this->h2_seq_initialized = true;
|
||||
this->h2_seq = seq;
|
||||
} else if (seq != this->h2_seq) {
|
||||
/* TODO: PLC (too late to insert data now) */
|
||||
spa_log_info(this->log,
|
||||
"missing mSBC/LC3 packet: %u != %u", seq, this->msbc_seq);
|
||||
this->msbc_seq = seq;
|
||||
"missing mSBC/LC3 packet: %u != %u", seq, this->h2_seq);
|
||||
this->h2_seq = seq;
|
||||
}
|
||||
|
||||
this->msbc_seq = (this->msbc_seq + 1) % 4;
|
||||
this->h2_seq = (this->h2_seq + 1) % 4;
|
||||
|
||||
if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) {
|
||||
if (avail < decoded_size)
|
||||
|
|
@ -524,10 +521,10 @@ static uint32_t preprocess_and_decode_codec_data(void *userdata, uint8_t *read_d
|
|||
|
||||
/* decode frame */
|
||||
processed = sbc_decode(
|
||||
&this->msbc, this->msbc_buffer + 2, encoded_size - 3,
|
||||
&this->msbc, this->recv_buffer + 2, encoded_size - 3,
|
||||
buf, avail, &written);
|
||||
} else {
|
||||
processed = lc3_decode_frame(this, this->msbc_buffer + 2, encoded_size - 2,
|
||||
processed = lc3_decode_frame(this, this->recv_buffer + 2, encoded_size - 2,
|
||||
buf, avail, &written);
|
||||
}
|
||||
|
||||
|
|
@ -746,9 +743,9 @@ static int transport_start(struct impl *this)
|
|||
|
||||
/* Libsbc expects audio samples by default in host endianness, mSBC requires little endian */
|
||||
this->msbc.endian = SBC_LE;
|
||||
this->msbc_seq_initialized = false;
|
||||
this->h2_seq_initialized = false;
|
||||
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->recv_buffer_pos = 0;
|
||||
} else if (this->transport->codec == HFP_AUDIO_CODEC_LC3) {
|
||||
#ifdef HAVE_LC3
|
||||
this->lc3 = lc3_setup_decoder(7500, 32000, 0,
|
||||
|
|
@ -758,8 +755,8 @@ static int transport_start(struct impl *this)
|
|||
|
||||
spa_assert(lc3_frame_samples(7500, 32000) * port->frame_size == LC3_SWB_DECODED_SIZE);
|
||||
|
||||
this->msbc_seq_initialized = false;
|
||||
this->msbc_buffer_pos = 0;
|
||||
this->h2_seq_initialized = false;
|
||||
this->recv_buffer_pos = 0;
|
||||
#else
|
||||
res = -EINVAL;
|
||||
goto fail;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue