From d466cffe2360d60dcb8e0d5f867e4ed8dac9e6db Mon Sep 17 00:00:00 2001 From: Mauricio Collares Date: Tue, 22 Dec 2020 20:17:07 -0300 Subject: [PATCH] Revert "port a2dp-source.c changes to sco-source.c" This reverts commit 3ad39d83a1af45a24f60277d6dc7e506fe43a8a8. --- spa/plugins/bluez5/sco-source.c | 109 +++++++++++++------------------- 1 file changed, 43 insertions(+), 66 deletions(-) diff --git a/spa/plugins/bluez5/sco-source.c b/spa/plugins/bluez5/sco-source.c index 2dafc00f3..4c95342b0 100644 --- a/spa/plugins/bluez5/sco-source.c +++ b/spa/plugins/bluez5/sco-source.c @@ -85,8 +85,8 @@ struct port { struct spa_list free; struct spa_list ready; - uint32_t n_ready; - unsigned int buffering:1; + struct buffer *current_buffer; + uint32_t ready_offset; }; struct impl { @@ -275,8 +275,6 @@ static void reset_buffers(struct port *port) spa_list_init(&port->free); spa_list_init(&port->ready); - port->n_ready = 0; - port->buffering = true; for (i = 0; i < port->n_buffers; i++) { struct buffer *b = &port->buffers[i]; @@ -350,11 +348,8 @@ static void sco_on_ready_read(struct spa_source *source) struct spa_io_buffers *io = port->io; int size_read; struct spa_data *datas; - struct buffer *buffer; + uint32_t max_out_size; uint8_t *packet; - /* must be at least this->transport->read_mtu and at least MSBC_DECODED_SIZE (240) */ - uint8_t read_decoded[4096]; - size_t decoded; /* make sure the source has input data */ if ((source->rmask & SPA_IO_IN) == 0) { @@ -366,15 +361,29 @@ static void sco_on_ready_read(struct spa_source *source) goto stop; } - /* update the current pts */ - spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now); + /* get buffer */ + if (!port->current_buffer) { + if (spa_list_is_empty(&port->free)) { + spa_log_warn(this->log, "buffer not available"); + return; + } + port->current_buffer = spa_list_first(&port->free, struct buffer, link); + spa_list_remove(&port->current_buffer->link); + port->ready_offset = 0; + } + datas = port->current_buffer->buf->datas; if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) { + max_out_size = MSBC_DECODED_SIZE; packet = this->msbc_buffer_head; } else { - packet = read_decoded; + max_out_size = this->transport->read_mtu; + packet = (uint8_t *)datas[0].data + port->ready_offset; } + /* update the current pts */ + spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &this->now); + /* read */ size_read = read_data(this, packet, this->transport->read_mtu); if (size_read < 0) { @@ -386,6 +395,7 @@ static void sco_on_ready_read(struct spa_source *source) if (this->transport->codec == HFP_AUDIO_CODEC_MSBC) { uint8_t seq; uint8_t *next_header; + size_t written; this->msbc_buffer_head += size_read; if (this->msbc_buffer_head - this->msbc_buffer >= MSBC_BUFFER_SIZE) { @@ -433,7 +443,7 @@ static void sco_on_ready_read(struct spa_source *source) /* decode frame */ int processed = sbc_decode(&this->msbc, this->msbc_buffer_tail + 2, MSBC_ENCODED_SIZE - 3, - read_decoded, MSBC_DECODED_SIZE, &decoded); + (uint8_t *)datas[0].data + port->ready_offset, MSBC_DECODED_SIZE, &written); if (processed < 0) { spa_log_warn(this->log, "sbc_decode failed: %d", processed); @@ -444,51 +454,30 @@ static void sco_on_ready_read(struct spa_source *source) return; } + port->ready_offset += written; this->msbc_seq = (this->msbc_seq + 1) % 4; this->msbc_buffer_tail += MSBC_ENCODED_SIZE; compact_msbc_buffer(this, this->msbc_buffer_tail, this->msbc_buffer_head); - } else { - decoded = size_read; - } + } else + port->ready_offset += size_read; - /* discard when not started */ - if (!this->started) - return; + /* send buffer if full */ + if ((max_out_size + port->ready_offset) > (this->props.max_latency * port->frame_size)) { + datas[0].chunk->offset = 0; + datas[0].chunk->size = port->ready_offset; + datas[0].chunk->stride = port->frame_size; - /* get buffer */ - if (spa_list_is_empty(&port->free)) { - spa_log_warn(this->log, "buffer not available"); - return; - } - buffer = spa_list_first(&port->free, struct buffer, link); - spa_list_remove(&buffer->link); - spa_log_debug(this->log, "dequeue %d", buffer->id); + this->sample_count += datas[0].chunk->size / port->frame_size; + spa_list_append(&port->ready, &port->current_buffer->link); + port->current_buffer = NULL; - if (buffer->h) { - buffer->h->seq = this->sample_count; - buffer->h->pts = SPA_TIMESPEC_TO_NSEC(&this->now); - buffer->h->dts_offset = 0; - } - datas = buffer->buf->datas; - - memcpy ((uint8_t *)datas[0].data, read_decoded, decoded); - - datas[0].chunk->offset = 0; - datas[0].chunk->size = decoded; - datas[0].chunk->stride = port->frame_size; - - this->sample_count += datas[0].chunk->size / port->frame_size; - - spa_log_debug(this->log, "queue %d", buffer->id); - spa_list_append(&port->ready, &buffer->link); - port->n_ready++; - - if (this->clock) { - this->clock->nsec = SPA_TIMESPEC_TO_NSEC(&this->now); - this->clock->position = this->sample_count; - this->clock->delay = 0; - this->clock->rate_diff = 1.0f; - this->clock->next_nsec = this->clock->nsec; + if (this->clock) { + this->clock->nsec = SPA_TIMESPEC_TO_NSEC(&this->now); + this->clock->position = this->sample_count; + this->clock->delay = 0; + this->clock->rate_diff = 1.0f; + this->clock->next_nsec = this->clock->nsec; + } } /* done if there are no buffers ready */ @@ -504,8 +493,6 @@ static void sco_on_ready_read(struct spa_source *source) b = spa_list_first(&port->ready, struct buffer, link); spa_list_remove(&b->link); - if (--port->n_ready == 0) - port->buffering = true; b->outstanding = true; io->buffer_id = b->id; @@ -794,9 +781,9 @@ impl_node_port_enum_params(void *object, int seq, param = spa_pod_builder_add_object(&b, SPA_TYPE_OBJECT_ParamBuffers, id, - SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(8, 8, MAX_BUFFERS), + SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(2, 1, MAX_BUFFERS), SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1), - SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int( + SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int( this->props.max_latency * port->frame_size, this->props.min_latency * port->frame_size, INT32_MAX), @@ -857,8 +844,6 @@ static int clear_buffers(struct impl *this, struct port *port) if (port->n_buffers > 0) { spa_list_init(&port->free); spa_list_init(&port->ready); - port->n_ready = 0; - port->buffering = true; port->n_buffers = 0; } return 0; @@ -1040,8 +1025,6 @@ static int impl_node_process(void *object) io = port->io; spa_return_val_if_fail(io != NULL, -EIO); - spa_log_debug(this->log, "%p status:%d %d", this, io->status, port->n_ready); - /* Return if we already have a buffer */ if (io->status == SPA_STATUS_HAVE_DATA) return SPA_STATUS_HAVE_DATA; @@ -1055,17 +1038,11 @@ static int impl_node_process(void *object) /* Return if there are no buffers ready to be processed */ if (spa_list_is_empty(&port->ready)) return SPA_STATUS_OK; - if (port->buffering && port->n_ready < 4) - return SPA_STATUS_OK; - - port->buffering = false; /* Get the new buffer from the ready list */ buffer = spa_list_first(&port->ready, struct buffer, link); spa_list_remove(&buffer->link); - if (--port->n_ready == 0) - port->buffering = true; - buffer->outstanding = true; + buffer->outstanding = false; /* Set the new buffer in IO */ io->buffer_id = buffer->id;