diff --git a/src/Makefile.am b/src/Makefile.am index a7bd8e2e2..8a5a73c81 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,6 @@ bluealsa_SOURCES = \ shared/rt.c \ a2dp.c \ a2dp-audio.c \ - a2dp-rtp.c \ at.c \ audio.c \ ba-adapter.c \ @@ -29,6 +28,7 @@ bluealsa_SOURCES = \ dbus.c \ hci.c \ io.c \ + rtp.c \ sco.c \ utils.c \ main.c diff --git a/src/a2dp-audio.c b/src/a2dp-audio.c index 16d637866..44bfbcd9a 100644 --- a/src/a2dp-audio.c +++ b/src/a2dp-audio.c @@ -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" @@ -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. * @@ -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; @@ -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); @@ -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); @@ -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; @@ -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; @@ -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); @@ -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); @@ -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; @@ -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; @@ -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; @@ -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); @@ -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) @@ -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; @@ -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; @@ -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; @@ -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); @@ -1498,7 +1483,7 @@ 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); @@ -1506,13 +1491,17 @@ static void *a2dp_source_aptx_hd(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); + int32_t *input = pcm.data; size_t input_samples = samples; @@ -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; @@ -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); @@ -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); @@ -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; @@ -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; diff --git a/src/a2dp-rtp.c b/src/rtp.c similarity index 91% rename from src/a2dp-rtp.c rename to src/rtp.c index ffc508775..6164297b4 100644 --- a/src/a2dp-rtp.c +++ b/src/rtp.c @@ -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. @@ -8,7 +8,7 @@ * */ -#include "a2dp-rtp.h" +#include "rtp.h" #include #include @@ -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); @@ -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) { diff --git a/src/a2dp-rtp.h b/src/rtp.h similarity index 87% rename from src/a2dp-rtp.h rename to src/rtp.h index 54b2ac7c1..aa6a0a059 100644 --- a/src/a2dp-rtp.h +++ b/src/rtp.h @@ -1,5 +1,5 @@ /* - * BlueALSA - a2dp-rtp.h + * BlueALSA - rtp.h * Copyright (c) 2016-2021 Arkadiusz Bokowy * * This file is a part of bluez-alsa. @@ -9,8 +9,8 @@ */ #pragma once -#ifndef BLUEALSA_A2DPRTP_H_ -#define BLUEALSA_A2DPRTP_H_ +#ifndef BLUEALSA_RTP_H_ +#define BLUEALSA_RTP_H_ #if HAVE_CONFIG_H # include @@ -72,7 +72,7 @@ typedef struct rtp_mpeg_audio_header { uint16_t offset; } __attribute__ ((packed)) rtp_mpeg_audio_header_t; -void *a2dp_rtp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size); -void *a2dp_rtp_payload(const rtp_header_t *hdr, uint16_t *seq_number); +void *rtp_a2dp_init(void *s, rtp_header_t **hdr, void **phdr, size_t phdr_size); +void *rtp_a2dp_payload(const rtp_header_t *hdr, uint16_t *seq_number); #endif diff --git a/test/Makefile.am b/test/Makefile.am index eaa48efc1..cd84c8d76 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -41,7 +41,6 @@ bluealsa_mock_SOURCES = \ ../src/shared/log.c \ ../src/shared/rt.c \ ../src/a2dp-audio.c \ - ../src/a2dp-rtp.c \ ../src/at.c \ ../src/audio.c \ ../src/ba-adapter.c \ @@ -55,6 +54,7 @@ bluealsa_mock_SOURCES = \ ../src/dbus.c \ ../src/hci.c \ ../src/io.c \ + ../src/rtp.c \ ../src/sco.c \ ../src/utils.c \ bluealsa-mock.c @@ -98,7 +98,6 @@ test_io_SOURCES = \ ../src/shared/ffb.c \ ../src/shared/log.c \ ../src/shared/rt.c \ - ../src/a2dp-rtp.c \ ../src/audio.c \ ../src/ba-adapter.c \ ../src/ba-device.c \ @@ -107,6 +106,7 @@ test_io_SOURCES = \ ../src/dbus.c \ ../src/hci.c \ ../src/io.c \ + ../src/rtp.c \ ../src/sco.c \ ../src/utils.c \ test-io.c diff --git a/test/test-io.c b/test/test-io.c index c410bd478..62707cba7 100644 --- a/test/test-io.c +++ b/test/test-io.c @@ -35,7 +35,6 @@ #endif #include "a2dp-codecs.h" -#include "a2dp-rtp.h" #include "a2dp.h" #include "ba-adapter.h" #include "ba-device.h" @@ -45,6 +44,7 @@ #include "bluealsa.h" #include "bluez.h" #include "hfp.h" +#include "rtp.h" #include "sco.h" #include "utils.h" #include "shared/defs.h"