Skip to content

Commit 8db7be7

Browse files
Thalleyjhedberg
authored andcommitted
samples: Bluetooth: Fix seq_num in broadcast audio source
When using multiple streams the seq_num provided to the controller would be incorrect as we used a single global seq_num rather than a proper seq_num per stream. Signed-off-by: Emil Gydesen <[email protected]>
1 parent f977457 commit 8db7be7

File tree

1 file changed

+18
-9
lines changed
  • samples/bluetooth/broadcast_audio_source/src

1 file changed

+18
-9
lines changed

samples/bluetooth/broadcast_audio_source/src/main.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ BUILD_ASSERT(CONFIG_BT_ISO_TX_BUF_COUNT >= TOTAL_BUF_NEEDED,
2121

2222
static struct bt_bap_lc3_preset preset_16_2_1 = BT_BAP_LC3_BROADCAST_PRESET_16_2_1(
2323
BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
24-
static struct bt_bap_stream streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
24+
static struct broadcast_source_stream {
25+
struct bt_bap_stream stream;
26+
uint16_t seq_num;
27+
size_t sent_cnt;
28+
} streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
2529
static struct bt_bap_broadcast_source *broadcast_source;
2630

2731
NET_BUF_POOL_FIXED_DEFINE(tx_pool,
@@ -39,6 +43,11 @@ static K_SEM_DEFINE(sem_stopped, 0U, ARRAY_SIZE(streams));
3943

4044
static void stream_started_cb(struct bt_bap_stream *stream)
4145
{
46+
struct broadcast_source_stream *source_stream =
47+
CONTAINER_OF(stream, struct broadcast_source_stream, stream);
48+
49+
source_stream->seq_num = 0U;
50+
source_stream->sent_cnt = 0U;
4251
k_sem_give(&sem_started);
4352
}
4453

@@ -49,7 +58,8 @@ static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
4958

5059
static void stream_sent_cb(struct bt_bap_stream *stream)
5160
{
52-
static uint32_t sent_cnt;
61+
struct broadcast_source_stream *source_stream =
62+
CONTAINER_OF(stream, struct broadcast_source_stream, stream);
5363
struct net_buf *buf;
5464
int ret;
5565

@@ -66,18 +76,17 @@ static void stream_sent_cb(struct bt_bap_stream *stream)
6676

6777
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
6878
net_buf_add_mem(buf, mock_data, preset_16_2_1.qos.sdu);
69-
ret = bt_bap_stream_send(stream, buf, seq_num++,
70-
BT_ISO_TIMESTAMP_NONE);
79+
ret = bt_bap_stream_send(stream, buf, source_stream->seq_num++, BT_ISO_TIMESTAMP_NONE);
7180
if (ret < 0) {
7281
/* This will end broadcasting on this stream. */
7382
printk("Unable to broadcast data on %p: %d\n", stream, ret);
7483
net_buf_unref(buf);
7584
return;
7685
}
7786

78-
sent_cnt++;
79-
if ((sent_cnt % 1000U) == 0U) {
80-
printk("Sent %u total ISO packets\n", sent_cnt);
87+
source_stream->sent_cnt++;
88+
if ((source_stream->sent_cnt % 1000U) == 0U) {
89+
printk("Stream %p: Sent %u total ISO packets\n", stream, source_stream->sent_cnt);
8190
}
8291
}
8392

@@ -106,7 +115,7 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
106115
}
107116

108117
for (size_t j = 0U; j < ARRAY_SIZE(stream_params); j++) {
109-
stream_params[j].stream = &streams[j];
118+
stream_params[j].stream = &streams[j].stream;
110119
stream_params[j].data = NULL;
111120
stream_params[j].data_len = 0U;
112121
bt_bap_stream_cb_register(stream_params[j].stream, &stream_ops);
@@ -249,7 +258,7 @@ int main(void)
249258
/* Initialize sending */
250259
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
251260
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
252-
stream_sent_cb(&streams[i]);
261+
stream_sent_cb(&streams[i].stream);
253262
}
254263
}
255264

0 commit comments

Comments
 (0)