Skip to content

Commit 36c1aea

Browse files
Thalleyhenrikbrixandersen
authored andcommitted
Bluetooth: BAP: Shell: Add support for USB audio in
Add support for receiving audio data from e.g. a PC over USB and LC3 encode it before sending it on BAP audio streams. This refactores the entire TX path, as it has moved from only support the sine wave generator, to also supporting USB. The encoding and sending of data is now in it's own thread, instead of relying on the system workqueue thread and k_work items. Several other refactors have taken place to reduce lines of codec (such as the introduction of the bap_foreach_stream function. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent 92145a2 commit 36c1aea

File tree

4 files changed

+597
-448
lines changed

4 files changed

+597
-448
lines changed

subsys/bluetooth/audio/shell/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ zephyr_library_sources_ifdef(
8181
CONFIG_BT_BAP_STREAM
8282
bap.c
8383
)
84-
if (CONFIG_BT_AUDIO_RX AND CONFIG_LIBLC3 AND CONFIG_USB_DEVICE_AUDIO)
84+
if (CONFIG_LIBLC3 AND CONFIG_USB_DEVICE_AUDIO)
8585
zephyr_library_sources(bap_usb.c)
8686
endif()
8787
zephyr_library_sources_ifdef(

subsys/bluetooth/audio/shell/audio.h

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ssize_t cap_initiator_pa_data_add(struct bt_data *data_array, const size_t data_
4848
#include <zephyr/bluetooth/audio/bap_lc3_preset.h>
4949
#include <zephyr/bluetooth/audio/cap.h>
5050

51-
unsigned long bap_get_recv_stats_interval(void);
51+
unsigned long bap_get_stats_interval(void);
5252

5353
#if defined(CONFIG_LIBLC3)
5454
#include "lc3.h"
@@ -61,7 +61,7 @@ unsigned long bap_get_recv_stats_interval(void);
6161
#define LC3_MAX_NUM_SAMPLES_STEREO (LC3_MAX_NUM_SAMPLES_MONO * 2U)
6262
#endif /* CONFIG_LIBLC3 */
6363

64-
#define LOCATION BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT
64+
#define LOCATION BT_AUDIO_LOCATION_FRONT_LEFT
6565
#define CONTEXT \
6666
(BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | \
6767
BT_AUDIO_CONTEXT_TYPE_MEDIA | \
@@ -75,15 +75,6 @@ struct named_lc3_preset {
7575
struct bt_bap_lc3_preset preset;
7676
};
7777

78-
const struct named_lc3_preset *bap_get_named_preset(bool is_unicast, enum bt_audio_dir dir,
79-
const char *preset_arg);
80-
81-
size_t bap_get_rx_streaming_cnt(void);
82-
int bap_usb_init(void);
83-
int bap_usb_add_frame_to_usb(enum bt_audio_location lc3_chan_allocation, const int16_t *frame,
84-
size_t frame_size, uint32_t ts);
85-
void bap_usb_clear_frames_to_usb(void);
86-
8778
struct shell_stream {
8879
struct bt_cap_stream stream;
8980
struct bt_audio_codec_cfg codec_cfg;
@@ -106,11 +97,20 @@ struct shell_stream {
10697
/* The uptime tick measured when stream was connected */
10798
int64_t connected_at_ticks;
10899
uint16_t seq_num;
109-
struct k_work_delayable audio_send_work;
110-
bool active;
111100
#if defined(CONFIG_LIBLC3)
112101
atomic_t lc3_enqueue_cnt;
102+
bool active;
103+
size_t encoded_cnt;
113104
size_t lc3_sdu_cnt;
105+
lc3_encoder_mem_48k_t lc3_encoder_mem;
106+
lc3_encoder_t lc3_encoder;
107+
#if defined(CONFIG_USB_DEVICE_AUDIO)
108+
/* Indicates where to read left USB data in the ring buffer */
109+
size_t left_read_idx;
110+
/* Indicates where to read right USB data in the ring buffer */
111+
size_t right_read_idx;
112+
size_t right_ring_buf_fail_cnt;
113+
#endif /* CONFIG_USB_DEVICE_AUDIO */
114114
#endif /* CONFIG_LIBLC3 */
115115
} tx;
116116
#endif /* CONFIG_BT_AUDIO_TX */
@@ -119,6 +119,7 @@ struct shell_stream {
119119
struct {
120120
struct bt_iso_recv_info last_info;
121121
size_t empty_sdu_pkts;
122+
size_t valid_sdu_pkts;
122123
size_t lost_pkts;
123124
size_t err_pkts;
124125
size_t dup_psn;
@@ -134,6 +135,26 @@ struct shell_stream {
134135
};
135136
};
136137

138+
const struct named_lc3_preset *bap_get_named_preset(bool is_unicast, enum bt_audio_dir dir,
139+
const char *preset_arg);
140+
141+
size_t bap_get_rx_streaming_cnt(void);
142+
size_t bap_get_tx_streaming_cnt(void);
143+
void bap_foreach_stream(void (*func)(struct shell_stream *sh_stream, void *data), void *data);
144+
145+
int bap_usb_init(void);
146+
147+
int bap_usb_add_frame_to_usb(enum bt_audio_location lc3_chan_allocation, const int16_t *frame,
148+
size_t frame_size, uint32_t ts);
149+
void bap_usb_clear_frames_to_usb(void);
150+
uint16_t get_next_seq_num(struct bt_bap_stream *bap_stream);
151+
struct shell_stream *shell_stream_from_bap_stream(struct bt_bap_stream *bap_stream);
152+
struct bt_bap_stream *bap_stream_from_shell_stream(struct shell_stream *sh_stream);
153+
bool bap_usb_can_get_full_sdu(struct shell_stream *sh_stream);
154+
void bap_usb_get_frame(struct shell_stream *sh_stream, enum bt_audio_location chan_alloc,
155+
int16_t buffer[]);
156+
size_t bap_usb_get_frame_size(const struct shell_stream *sh_stream);
157+
137158
struct broadcast_source {
138159
bool is_cap;
139160
union {

0 commit comments

Comments
 (0)