Skip to content

Commit

Permalink
Remove legacy A2DP IO poll and read functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jul 8, 2021
1 parent 1f82111 commit ad63d45
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ bluealsa_SOURCES = \
shared/rt.c \
a2dp.c \
a2dp-audio.c \
a2dp-rtp.c \
at.c \
audio.c \
ba-adapter.c \
Expand All @@ -29,6 +28,7 @@ bluealsa_SOURCES = \
dbus.c \
hci.c \
io.c \
rtp.c \
sco.c \
utils.c \
main.c
Expand Down
113 changes: 53 additions & 60 deletions src/a2dp-audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@

#include "a2dp.h"
#include "a2dp-codecs.h"
#include "a2dp-rtp.h"
#include "bluealsa.h"
#if ENABLE_APTX || ENABLE_APTX_HD
# include "codec-aptx.h"
#endif
#include "codec-sbc.h"
#include "io.h"
#include "rtp.h"
#include "utils.h"
#include "shared/defs.h"
#include "shared/ffb.h"
Expand All @@ -78,35 +78,6 @@ static enum ba_transport_thread_signal a2dp_io_poll_signal_filter_dec(
return signal;
}

/**
* Poll and read PCM signal from the transport PCM FIFO.
*
* Note:
* This function temporally re-enables thread cancellation! */
static ssize_t a2dp_poll_and_read_pcm(struct a2dp_io_poll *io,
struct ba_transport_pcm *pcm, ffb_t *buffer) {

ssize_t samples;
if ((samples = io_poll_and_read_pcm(&io->io, pcm,
buffer->tail, ffb_len_in(buffer))) <= 0)
return samples;

/* update PCM buffer */
ffb_seek(buffer, samples);

/* return overall number of samples */
return ffb_len_out(buffer);
}

/**
* Poll and read BT data from the SEQPACKET socket.
*
* Note:
* This function temporally re-enables thread cancellation! */
static ssize_t a2dp_poll_and_read_bt(struct a2dp_io_poll *io, ffb_t *buffer) {
return io_poll_and_read_bt(&io->io, io->th, buffer->tail, ffb_blen_in(buffer));
}

/**
* Write data to the BT SEQPACKET socket.
*
Expand Down Expand Up @@ -179,8 +150,8 @@ static void *a2dp_sink_sbc(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand All @@ -190,7 +161,7 @@ static void *a2dp_sink_sbc(struct ba_transport_thread *th) {
continue;

const rtp_media_header_t *rtp_media_header;
if ((rtp_media_header = a2dp_rtp_payload(bt.data, &io.rtp_seq_number)) == NULL)
if ((rtp_media_header = rtp_a2dp_payload(bt.data, &io.rtp_seq_number)) == NULL)
continue;

const uint8_t *rtp_payload = (uint8_t *)(rtp_media_header + 1);
Expand Down Expand Up @@ -298,7 +269,7 @@ static void *a2dp_source_sbc(struct ba_transport_thread *th) {
rtp_media_header_t *rtp_media_header;

/* initialize RTP headers and get anchor for payload */
uint8_t *rtp_payload = a2dp_rtp_init(bt.data, &rtp_header,
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header,
(void **)&rtp_media_header, sizeof(*rtp_media_header));
uint16_t seq_number = be16toh(rtp_header->seq_number);
uint32_t timestamp = be32toh(rtp_header->timestamp);
Expand All @@ -307,13 +278,17 @@ static void *a2dp_source_sbc(struct ba_transport_thread *th) {
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
continue;
}

ffb_seek(&pcm, samples);
samples = ffb_len_out(&pcm);

/* anchor for RTP payload */
bt.tail = rtp_payload;

Expand Down Expand Up @@ -472,8 +447,8 @@ static void *a2dp_sink_mpeg(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand All @@ -483,7 +458,7 @@ static void *a2dp_sink_mpeg(struct ba_transport_thread *th) {
continue;

const rtp_mpeg_audio_header_t *rtp_mpeg_header;
if ((rtp_mpeg_header = a2dp_rtp_payload(bt.data, &io.rtp_seq_number)) == NULL)
if ((rtp_mpeg_header = rtp_a2dp_payload(bt.data, &io.rtp_seq_number)) == NULL)
continue;

uint8_t *rtp_mpeg = (uint8_t *)(rtp_mpeg_header + 1);
Expand Down Expand Up @@ -685,7 +660,7 @@ static void *a2dp_source_mp3(struct ba_transport_thread *th) {
rtp_mpeg_audio_header_t *rtp_mpeg_audio_header;

/* initialize RTP headers and get anchor for payload */
uint8_t *rtp_payload = a2dp_rtp_init(bt.data, &rtp_header,
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header,
(void **)&rtp_mpeg_audio_header, sizeof(*rtp_mpeg_audio_header));
uint16_t seq_number = be16toh(rtp_header->seq_number);
uint32_t timestamp = be32toh(rtp_header->timestamp);
Expand All @@ -694,13 +669,17 @@ static void *a2dp_source_mp3(struct ba_transport_thread *th) {
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
continue;
}

ffb_seek(&pcm, samples);
samples = ffb_len_out(&pcm);

/* anchor for RTP payload */
bt.tail = rtp_payload;

Expand Down Expand Up @@ -846,8 +825,8 @@ static void *a2dp_sink_aac(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand All @@ -857,7 +836,7 @@ static void *a2dp_sink_aac(struct ba_transport_thread *th) {
continue;

const uint8_t *rtp_latm;
if ((rtp_latm = a2dp_rtp_payload(bt.data, &io.rtp_seq_number)) == NULL)
if ((rtp_latm = rtp_a2dp_payload(bt.data, &io.rtp_seq_number)) == NULL)
continue;

const rtp_header_t *rtp_header = (rtp_header_t *)bt.data;
Expand Down Expand Up @@ -1046,7 +1025,7 @@ static void *a2dp_source_aac(struct ba_transport_thread *th) {
rtp_header_t *rtp_header;

/* initialize RTP header and get anchor for payload */
uint8_t *rtp_payload = a2dp_rtp_init(bt.data, &rtp_header, NULL, 0);
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header, NULL, 0);
uint16_t seq_number = be16toh(rtp_header->seq_number);
uint32_t timestamp = be32toh(rtp_header->timestamp);

Expand Down Expand Up @@ -1078,13 +1057,15 @@ static void *a2dp_source_aac(struct ba_transport_thread *th) {
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
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 @@ -1201,8 +1182,8 @@ static void *a2dp_sink_aptx(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand Down Expand Up @@ -1291,13 +1272,17 @@ static void *a2dp_source_aptx(struct ba_transport_thread *th) {
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
continue;
}

ffb_seek(&pcm, samples);
samples = ffb_len_out(&pcm);

int16_t *input = pcm.data;
size_t input_samples = samples;

Expand Down Expand Up @@ -1405,8 +1390,8 @@ static void *a2dp_sink_aptx_hd(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand All @@ -1416,7 +1401,7 @@ static void *a2dp_sink_aptx_hd(struct ba_transport_thread *th) {
continue;

const uint8_t *rtp_payload;
if ((rtp_payload = a2dp_rtp_payload(bt.data, &io.rtp_seq_number)) == NULL)
if ((rtp_payload = rtp_a2dp_payload(bt.data, &io.rtp_seq_number)) == NULL)
continue;

size_t rtp_payload_len = len - (rtp_payload - (uint8_t *)bt.data);
Expand Down Expand Up @@ -1498,21 +1483,25 @@ static void *a2dp_source_aptx_hd(struct ba_transport_thread *th) {
rtp_header_t *rtp_header;

/* initialize RTP header and get anchor for payload */
uint8_t *rtp_payload = a2dp_rtp_init(bt.data, &rtp_header, NULL, 0);
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header, NULL, 0);
uint16_t seq_number = be16toh(rtp_header->seq_number);
uint32_t timestamp = be32toh(rtp_header->timestamp);

debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
continue;
}

ffb_seek(&pcm, samples);
samples = ffb_len_out(&pcm);

int32_t *input = pcm.data;
size_t input_samples = samples;

Expand Down Expand Up @@ -1637,8 +1626,8 @@ static void *a2dp_sink_ldac(struct ba_transport_thread *th) {
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {

ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand All @@ -1648,7 +1637,7 @@ static void *a2dp_sink_ldac(struct ba_transport_thread *th) {
continue;

const rtp_media_header_t *rtp_media_header;
if ((rtp_media_header = a2dp_rtp_payload(bt.data, &io.rtp_seq_number)) == NULL)
if ((rtp_media_header = rtp_a2dp_payload(bt.data, &io.rtp_seq_number)) == NULL)
continue;

const uint8_t *rtp_payload = (uint8_t *)(rtp_media_header + 1);
Expand Down Expand Up @@ -1757,7 +1746,7 @@ static void *a2dp_source_ldac(struct ba_transport_thread *th) {
rtp_media_header_t *rtp_media_header;

/* initialize RTP headers and get anchor for payload */
uint8_t *rtp_payload = a2dp_rtp_init(bt.data, &rtp_header,
uint8_t *rtp_payload = rtp_a2dp_init(bt.data, &rtp_header,
(void **)&rtp_media_header, sizeof(*rtp_media_header));
uint16_t seq_number = be16toh(rtp_header->seq_number);
uint32_t timestamp = be32toh(rtp_header->timestamp);
Expand All @@ -1767,13 +1756,17 @@ static void *a2dp_source_ldac(struct ba_transport_thread *th) {
for (ba_transport_thread_set_state_running(th);;) {

ssize_t samples;
if ((samples = a2dp_poll_and_read_pcm(&io, &t->a2dp.pcm, &pcm)) <= 0) {
if ((samples = io_poll_and_read_pcm(&io.io, &t->a2dp.pcm,
pcm.tail, ffb_len_in(&pcm))) <= 0) {
if (samples == -1)
error("PCM poll and read error: %s", strerror(errno));
ba_transport_stop_if_no_clients(t);
continue;
}

ffb_seek(&pcm, samples);
samples = ffb_len_out(&pcm);

int16_t *input = pcm.data;
size_t input_len = samples;

Expand Down Expand Up @@ -1890,8 +1883,8 @@ static void *a2dp_sink_dump(struct ba_transport_thread *th) {

debug_transport_thread_loop(th, "START");
for (ba_transport_thread_set_state_running(th);;) {
ssize_t len;
if ((len = a2dp_poll_and_read_bt(&io, &bt)) <= 0) {
ssize_t len = ffb_blen_in(&bt);
if ((len = io_poll_and_read_bt(&io.io, th, bt.data, len)) <= 0) {
if (len == -1)
error("BT poll and read error: %s", strerror(errno));
goto fail;
Expand Down
8 changes: 4 additions & 4 deletions src/a2dp-rtp.c → src/rtp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* BlueALSA - a2dp-rtp.c
* BlueALSA - rtp.c
* Copyright (c) 2016-2021 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
Expand All @@ -8,7 +8,7 @@
*
*/

#include "a2dp-rtp.h"
#include "rtp.h"

#include <stdlib.h>
#include <string.h>
Expand All @@ -24,7 +24,7 @@
* be stored. This parameter might be NULL.
* @param phdr_size The size of the RTP payload header.
* @return This function returns the address of the RTP payload region. */
void *a2dp_rtp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size) {
void *rtp_a2dp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size) {

rtp_header_t *header = *hdr = (rtp_header_t *)s;
memset(header, 0, RTP_HEADER_LEN + phdr_size);
Expand All @@ -48,7 +48,7 @@ void *a2dp_rtp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size)
* @param seq_number The pointer to a local RTP sequence number.
* @return On success, this function returns pointer to data just after
* the RTP header - RTP header payload. On failure, NULL is returned. */
void *a2dp_rtp_payload(const rtp_header_t *hdr, uint16_t *seq_number) {
void *rtp_a2dp_payload(const rtp_header_t *hdr, uint16_t *seq_number) {

#if ENABLE_PAYLOADCHECK
if (hdr->paytype < 96) {
Expand Down
Loading

0 comments on commit ad63d45

Please sign in to comment.