1 /*
2 * Copyright (c) 2022-2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/bluetooth/bluetooth.h>
8 #include <zephyr/bluetooth/audio/audio.h>
9 #include <zephyr/bluetooth/audio/bap.h>
10 #include <zephyr/bluetooth/audio/bap_lc3_preset.h>
11
12 BUILD_ASSERT(strlen(CONFIG_BROADCAST_CODE) <= BT_AUDIO_BROADCAST_CODE_SIZE,
13 "Invalid broadcast code");
14
15 /* Zephyr Controller works best while Extended Advertising interval to be a multiple
16 * of the ISO Interval minus 10 ms (max. advertising random delay). This is
17 * required to place the AUX_ADV_IND PDUs in a non-overlapping interval with the
18 * Broadcast ISO radio events.
19 *
20 * I.e. for a 7.5 ms ISO interval use 90 ms minus 10 ms ==> 80 ms advertising
21 * interval.
22 * And, for 10 ms ISO interval, can use 90 ms minus 10 ms ==> 80 ms advertising
23 * interval.
24 */
25 #define BT_LE_EXT_ADV_CUSTOM BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, 0x0080, 0x0080, NULL)
26
27 /* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that
28 * the controller is never idle
29 */
30 #define BROADCAST_ENQUEUE_COUNT 3U
31 #define TOTAL_BUF_NEEDED (BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT)
32
33 BUILD_ASSERT(CONFIG_BT_ISO_TX_BUF_COUNT >= TOTAL_BUF_NEEDED,
34 "CONFIG_BT_ISO_TX_BUF_COUNT should be at least "
35 "BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT");
36
37 #if defined(CONFIG_BAP_BROADCAST_16_2_1)
38
39 static struct bt_bap_lc3_preset preset_active = BT_BAP_LC3_BROADCAST_PRESET_16_2_1(
40 BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT,
41 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
42
43 #define BROADCAST_SAMPLE_RATE 16000
44
45 #elif defined(CONFIG_BAP_BROADCAST_24_2_1)
46
47 static struct bt_bap_lc3_preset preset_active = BT_BAP_LC3_BROADCAST_PRESET_24_2_1(
48 BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT,
49 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
50
51 #define BROADCAST_SAMPLE_RATE 24000
52
53 #endif
54
55 #if defined(CONFIG_BAP_BROADCAST_16_2_1)
56 #define MAX_SAMPLE_RATE 16000
57 #elif defined(CONFIG_BAP_BROADCAST_24_2_1)
58 #define MAX_SAMPLE_RATE 24000
59 #endif
60 #define MAX_FRAME_DURATION_US 10000
61 #define MAX_NUM_SAMPLES ((MAX_FRAME_DURATION_US * MAX_SAMPLE_RATE) / USEC_PER_SEC)
62
63 #if defined(CONFIG_LIBLC3)
64 #include "lc3.h"
65
66 #if defined(CONFIG_USB_DEVICE_AUDIO)
67 #include <zephyr/usb/usb_device.h>
68 #include <zephyr/usb/class/usb_audio.h>
69 #include <zephyr/sys/ring_buffer.h>
70
71 /* USB Audio Data is downsampled from 48kHz to match broadcast preset when receiving data */
72 #define USB_SAMPLE_RATE 48000
73 #define USB_DOWNSAMPLE_RATE BROADCAST_SAMPLE_RATE
74 #define USB_FRAME_DURATION_US 1000
75 #define USB_NUM_SAMPLES ((USB_FRAME_DURATION_US * USB_DOWNSAMPLE_RATE) / USEC_PER_SEC)
76 #define USB_BYTES_PER_SAMPLE 2
77 #define USB_CHANNELS 2
78
79 #define RING_BUF_USB_FRAMES 20
80 #define AUDIO_RING_BUF_BYTES (USB_NUM_SAMPLES * USB_BYTES_PER_SAMPLE * RING_BUF_USB_FRAMES)
81 #else /* !defined(CONFIG_USB_DEVICE_AUDIO) */
82
83 #include <math.h>
84
85 #define AUDIO_VOLUME (INT16_MAX - 3000) /* codec does clipping above INT16_MAX - 3000 */
86 #define AUDIO_TONE_FREQUENCY_HZ 400
87
88 /**
89 * Use the math lib to generate a sine-wave using 16 bit samples into a buffer.
90 *
91 * @param buf Destination buffer
92 * @param length_us Length of the buffer in microseconds
93 * @param frequency_hz frequency in Hz
94 * @param sample_rate_hz sample-rate in Hz.
95 */
fill_audio_buf_sin(int16_t * buf,int length_us,int frequency_hz,int sample_rate_hz)96 static void fill_audio_buf_sin(int16_t *buf, int length_us, int frequency_hz, int sample_rate_hz)
97 {
98 const int sine_period_samples = sample_rate_hz / frequency_hz;
99 const unsigned int num_samples = (length_us * sample_rate_hz) / USEC_PER_SEC;
100 const float step = 2 * 3.1415f / sine_period_samples;
101
102 for (unsigned int i = 0; i < num_samples; i++) {
103 const float sample = sinf(i * step);
104
105 buf[i] = (int16_t)(AUDIO_VOLUME * sample);
106 }
107 }
108 #endif /* defined(CONFIG_USB_DEVICE_AUDIO) */
109 #endif /* defined(CONFIG_LIBLC3) */
110
111 static struct broadcast_source_stream {
112 struct bt_bap_stream stream;
113 uint16_t seq_num;
114 size_t sent_cnt;
115 #if defined(CONFIG_LIBLC3)
116 lc3_encoder_t lc3_encoder;
117 #if defined(CONFIG_BAP_BROADCAST_16_2_1)
118 lc3_encoder_mem_16k_t lc3_encoder_mem;
119 #elif defined(CONFIG_BAP_BROADCAST_24_2_1)
120 lc3_encoder_mem_48k_t lc3_encoder_mem;
121 #endif
122 #if defined(CONFIG_USB_DEVICE_AUDIO)
123 struct ring_buf audio_ring_buf;
124 uint8_t _ring_buffer_memory[AUDIO_RING_BUF_BYTES];
125 #endif /* defined(CONFIG_USB_DEVICE_AUDIO) */
126 #endif /* defined(CONFIG_LIBLC3) */
127 } streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
128 static struct bt_bap_broadcast_source *broadcast_source;
129
130 NET_BUF_POOL_FIXED_DEFINE(tx_pool, TOTAL_BUF_NEEDED, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
131 CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
132
133 static int16_t send_pcm_data[MAX_NUM_SAMPLES];
134 static uint16_t seq_num;
135 static bool stopping;
136
137 static K_SEM_DEFINE(sem_started, 0U, ARRAY_SIZE(streams));
138 static K_SEM_DEFINE(sem_stopped, 0U, ARRAY_SIZE(streams));
139
140 #define BROADCAST_SOURCE_LIFETIME 120U /* seconds */
141
142 #if defined(CONFIG_LIBLC3)
143 static int freq_hz;
144 static int frame_duration_us;
145 static int frames_per_sdu;
146 static int octets_per_frame;
147
148 static K_SEM_DEFINE(lc3_encoder_sem, 0U, TOTAL_BUF_NEEDED);
149 #endif
150
send_data(struct broadcast_source_stream * source_stream)151 static void send_data(struct broadcast_source_stream *source_stream)
152 {
153 struct bt_bap_stream *stream = &source_stream->stream;
154 struct net_buf *buf;
155 int ret;
156
157 if (stopping) {
158 return;
159 }
160
161 buf = net_buf_alloc(&tx_pool, K_FOREVER);
162 if (buf == NULL) {
163 printk("Could not allocate buffer when sending on %p\n", stream);
164 return;
165 }
166
167 net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
168 #if defined(CONFIG_LIBLC3)
169 uint8_t lc3_encoded_buffer[preset_active.qos.sdu];
170
171 if (source_stream->lc3_encoder == NULL) {
172 printk("LC3 encoder not setup, cannot encode data.\n");
173 net_buf_unref(buf);
174 return;
175 }
176
177 #if defined(CONFIG_USB_DEVICE_AUDIO)
178 uint32_t size = ring_buf_get(&source_stream->audio_ring_buf, (uint8_t *)send_pcm_data,
179 sizeof(send_pcm_data));
180
181 if (size < sizeof(send_pcm_data)) {
182 const size_t padding_size = sizeof(send_pcm_data) - size;
183
184 printk("Not enough bytes ready, padding %d!\n", padding_size);
185 memset(&((uint8_t *)send_pcm_data)[size], 0, padding_size);
186 }
187 #endif
188
189 ret = lc3_encode(source_stream->lc3_encoder, LC3_PCM_FORMAT_S16, send_pcm_data, 1,
190 octets_per_frame, lc3_encoded_buffer);
191 if (ret == -1) {
192 printk("LC3 encoder failed - wrong parameters?: %d", ret);
193 net_buf_unref(buf);
194 return;
195 }
196
197 net_buf_add_mem(buf, lc3_encoded_buffer, preset_active.qos.sdu);
198 #else
199 net_buf_add_mem(buf, send_pcm_data, preset_active.qos.sdu);
200 #endif /* defined(CONFIG_LIBLC3) */
201
202 ret = bt_bap_stream_send(stream, buf, source_stream->seq_num++);
203 if (ret < 0) {
204 /* This will end broadcasting on this stream. */
205 printk("Unable to broadcast data on %p: %d\n", stream, ret);
206 net_buf_unref(buf);
207 return;
208 }
209
210 source_stream->sent_cnt++;
211 if ((source_stream->sent_cnt % 1000U) == 0U) {
212 printk("Stream %p: Sent %u total ISO packets\n", stream, source_stream->sent_cnt);
213 }
214 }
215
216 #if defined(CONFIG_LIBLC3)
init_lc3_thread(void * arg1,void * arg2,void * arg3)217 static void init_lc3_thread(void *arg1, void *arg2, void *arg3)
218 {
219 const struct bt_audio_codec_cfg *codec_cfg = &preset_active.codec_cfg;
220 int ret;
221
222 ret = bt_audio_codec_cfg_get_freq(codec_cfg);
223 if (ret > 0) {
224 freq_hz = bt_audio_codec_cfg_freq_to_freq_hz(ret);
225 } else {
226 return;
227 }
228
229 ret = bt_audio_codec_cfg_get_frame_dur(codec_cfg);
230 if (ret > 0) {
231 frame_duration_us = bt_audio_codec_cfg_frame_dur_to_frame_dur_us(ret);
232 } else {
233 printk("Error: Frame duration not set, cannot start codec.");
234 return;
235 }
236
237 octets_per_frame = bt_audio_codec_cfg_get_octets_per_frame(codec_cfg);
238 frames_per_sdu = bt_audio_codec_cfg_get_frame_blocks_per_sdu(codec_cfg, true);
239
240 if (freq_hz < 0) {
241 printk("Error: Codec frequency not set, cannot start codec.");
242 return;
243 }
244
245 if (frame_duration_us < 0) {
246 printk("Error: Frame duration not set, cannot start codec.");
247 return;
248 }
249
250 if (octets_per_frame < 0) {
251 printk("Error: Octets per frame not set, cannot start codec.");
252 return;
253 }
254
255 #if !defined(CONFIG_USB_DEVICE_AUDIO)
256 /* If USB is not used as a sound source, generate a sine wave */
257 fill_audio_buf_sin(send_pcm_data, frame_duration_us, AUDIO_TONE_FREQUENCY_HZ, freq_hz);
258 #endif
259
260 /* Create the encoder instance. This shall complete before stream_started() is called. */
261 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
262 printk("Initializing lc3 encoder for stream %zu\n", i);
263 streams[i].lc3_encoder = lc3_setup_encoder(frame_duration_us, freq_hz, 0,
264 &streams[i].lc3_encoder_mem);
265
266 if (streams[i].lc3_encoder == NULL) {
267 printk("ERROR: Failed to setup LC3 encoder - wrong parameters?\n");
268 }
269 }
270
271 while (true) {
272 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
273 k_sem_take(&lc3_encoder_sem, K_FOREVER);
274 }
275 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
276 send_data(&streams[i]);
277 }
278 }
279 }
280
281 #define LC3_ENCODER_STACK_SIZE 4 * 4096
282 #define LC3_ENCODER_PRIORITY 5
283
284 K_THREAD_DEFINE(encoder, LC3_ENCODER_STACK_SIZE, init_lc3_thread, NULL, NULL, NULL,
285 LC3_ENCODER_PRIORITY, 0, -1);
286
287 #if defined(CONFIG_USB_DEVICE_AUDIO)
data_received(const struct device * dev,struct net_buf * buffer,size_t size)288 static void data_received(const struct device *dev, struct net_buf *buffer, size_t size)
289 {
290 static int count;
291 int16_t *pcm;
292 int nsamples, ratio;
293 int16_t usb_pcm_data[USB_CHANNELS][USB_NUM_SAMPLES];
294
295 if (!buffer) {
296 return;
297 }
298
299 if (!size) {
300 net_buf_unref(buffer);
301 return;
302 }
303
304 pcm = (int16_t *)net_buf_pull_mem(buffer, size);
305
306 /* 'size' is in bytes, containing 1ms, 48kHz, stereo, 2 bytes per sample.
307 * Take left channel and do a simple downsample to 16kHz/24Khz
308 * matching the broadcast preset.
309 */
310
311 ratio = USB_SAMPLE_RATE / USB_DOWNSAMPLE_RATE;
312 nsamples = size / (sizeof(int16_t) * USB_CHANNELS * ratio);
313 for (size_t i = 0, j = 0; i < nsamples; i++, j += USB_CHANNELS * ratio) {
314 usb_pcm_data[0][i] = pcm[j];
315 usb_pcm_data[1][i] = pcm[j + 1];
316 }
317
318 for (size_t i = 0U; i < MIN(ARRAY_SIZE(streams), 2); i++) {
319 const uint32_t size_put =
320 ring_buf_put(&(streams[i].audio_ring_buf), (uint8_t *)(usb_pcm_data[i]),
321 nsamples * USB_BYTES_PER_SAMPLE);
322 if (size_put < nsamples * USB_BYTES_PER_SAMPLE) {
323 printk("Not enough room for samples in %s buffer: %u < %u, total capacity: "
324 "%u\n",
325 i == 0 ? "left" : "right", size_put, nsamples * USB_BYTES_PER_SAMPLE,
326 ring_buf_capacity_get(&(streams[i].audio_ring_buf)));
327 }
328 }
329
330 count++;
331 if ((count % 1000) == 0) {
332 printk("USB Data received (count = %d)\n", count);
333 }
334
335 net_buf_unref(buffer);
336 }
337
338 static const struct usb_audio_ops ops = {.data_received_cb = data_received};
339 #endif /* defined(CONFIG_USB_DEVICE_AUDIO) */
340 #endif /* defined(CONFIG_LIBLC3) */
341
stream_started_cb(struct bt_bap_stream * stream)342 static void stream_started_cb(struct bt_bap_stream *stream)
343 {
344 struct broadcast_source_stream *source_stream =
345 CONTAINER_OF(stream, struct broadcast_source_stream, stream);
346
347 source_stream->seq_num = 0U;
348 source_stream->sent_cnt = 0U;
349 k_sem_give(&sem_started);
350 }
351
stream_stopped_cb(struct bt_bap_stream * stream,uint8_t reason)352 static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
353 {
354 k_sem_give(&sem_stopped);
355 }
356
stream_sent_cb(struct bt_bap_stream * stream)357 static void stream_sent_cb(struct bt_bap_stream *stream)
358 {
359 #if defined(CONFIG_LIBLC3)
360 k_sem_give(&lc3_encoder_sem);
361 #else
362 /* If no LC3 encoder is used, just send mock data directly */
363 struct broadcast_source_stream *source_stream =
364 CONTAINER_OF(stream, struct broadcast_source_stream, stream);
365
366 send_data(source_stream);
367 #endif
368 }
369
370 static struct bt_bap_stream_ops stream_ops = {
371 .started = stream_started_cb, .stopped = stream_stopped_cb, .sent = stream_sent_cb};
372
setup_broadcast_source(struct bt_bap_broadcast_source ** source)373 static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
374 {
375 struct bt_bap_broadcast_source_stream_param
376 stream_params[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
377 struct bt_bap_broadcast_source_subgroup_param
378 subgroup_param[CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT];
379 struct bt_bap_broadcast_source_param create_param = {0};
380 const size_t streams_per_subgroup = ARRAY_SIZE(stream_params) / ARRAY_SIZE(subgroup_param);
381 uint8_t left[] = {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_CHAN_ALLOC,
382 BT_BYTES_LIST_LE32(BT_AUDIO_LOCATION_FRONT_LEFT))};
383 uint8_t right[] = {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_CHAN_ALLOC,
384 BT_BYTES_LIST_LE32(BT_AUDIO_LOCATION_FRONT_RIGHT))};
385 int err;
386
387 for (size_t i = 0U; i < ARRAY_SIZE(subgroup_param); i++) {
388 subgroup_param[i].params_count = streams_per_subgroup;
389 subgroup_param[i].params = stream_params + i * streams_per_subgroup;
390 subgroup_param[i].codec_cfg = &preset_active.codec_cfg;
391 }
392
393 for (size_t j = 0U; j < ARRAY_SIZE(stream_params); j++) {
394 stream_params[j].stream = &streams[j].stream;
395 stream_params[j].data = j == 0 ? left : right;
396 stream_params[j].data_len = j == 0 ? sizeof(left) : sizeof(right);
397 bt_bap_stream_cb_register(stream_params[j].stream, &stream_ops);
398 }
399
400 create_param.params_count = ARRAY_SIZE(subgroup_param);
401 create_param.params = subgroup_param;
402 create_param.qos = &preset_active.qos;
403 create_param.encryption = strlen(CONFIG_BROADCAST_CODE) > 0;
404 create_param.packing = BT_ISO_PACKING_SEQUENTIAL;
405
406 if (create_param.encryption) {
407 memcpy(create_param.broadcast_code, CONFIG_BROADCAST_CODE,
408 strlen(CONFIG_BROADCAST_CODE));
409 }
410
411 printk("Creating broadcast source with %zu subgroups with %zu streams\n",
412 ARRAY_SIZE(subgroup_param), ARRAY_SIZE(subgroup_param) * streams_per_subgroup);
413
414 err = bt_bap_broadcast_source_create(&create_param, source);
415 if (err != 0) {
416 printk("Unable to create broadcast source: %d\n", err);
417 return err;
418 }
419
420 return 0;
421 }
422
main(void)423 int main(void)
424 {
425 struct bt_le_ext_adv *adv;
426 int err;
427
428 err = bt_enable(NULL);
429 if (err) {
430 printk("Bluetooth init failed (err %d)\n", err);
431 return 0;
432 }
433 printk("Bluetooth initialized\n");
434
435 for (size_t i = 0U; i < ARRAY_SIZE(send_pcm_data); i++) {
436 /* Initialize mock data */
437 send_pcm_data[i] = i;
438 }
439
440 #if defined(CONFIG_LIBLC3)
441 #if defined(CONFIG_USB_DEVICE_AUDIO)
442 const struct device *hs_dev;
443
444 hs_dev = DEVICE_DT_GET(DT_NODELABEL(hs_0));
445
446 if (!device_is_ready(hs_dev)) {
447 printk("Device USB Headset is not ready\n");
448 return 0;
449 }
450
451 printk("Found USB Headset Device\n");
452
453 (void)memset(streams, 0, sizeof(streams));
454
455 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
456 ring_buf_init(&(streams[i].audio_ring_buf), sizeof(streams[i]._ring_buffer_memory),
457 streams[i]._ring_buffer_memory);
458 printk("Initialized ring buf %zu: capacity: %u\n", i,
459 ring_buf_capacity_get(&(streams[i].audio_ring_buf)));
460 }
461
462 usb_audio_register(hs_dev, &ops);
463
464 err = usb_enable(NULL);
465 if (err && err != -EALREADY) {
466 printk("Failed to enable USB (%d)", err);
467 return 0;
468 }
469
470 #endif /* defined(CONFIG_USB_DEVICE_AUDIO) */
471 k_thread_start(encoder);
472 #endif /* defined(CONFIG_LIBLC3) */
473
474 while (true) {
475 /* Broadcast Audio Streaming Endpoint advertising data */
476 NET_BUF_SIMPLE_DEFINE(ad_buf, BT_UUID_SIZE_16 + BT_AUDIO_BROADCAST_ID_SIZE);
477 NET_BUF_SIMPLE_DEFINE(base_buf, 128);
478 struct bt_data ext_ad[2];
479 struct bt_data per_ad;
480 uint32_t broadcast_id;
481
482 /* Create a connectable advertising set */
483 err = bt_le_ext_adv_create(BT_LE_EXT_ADV_CUSTOM, NULL, &adv);
484 if (err != 0) {
485 printk("Unable to create extended advertising set: %d\n", err);
486 return 0;
487 }
488
489 /* Set periodic advertising parameters */
490 err = bt_le_per_adv_set_param(adv, BT_LE_PER_ADV_DEFAULT);
491 if (err) {
492 printk("Failed to set periodic advertising parameters (err %d)\n", err);
493 return 0;
494 }
495
496 printk("Creating broadcast source\n");
497 err = setup_broadcast_source(&broadcast_source);
498 if (err != 0) {
499 printk("Unable to setup broadcast source: %d\n", err);
500 return 0;
501 }
502
503 err = bt_bap_broadcast_source_get_id(broadcast_source, &broadcast_id);
504 if (err != 0) {
505 printk("Unable to get broadcast ID: %d\n", err);
506 return 0;
507 }
508
509 /* Setup extended advertising data */
510 net_buf_simple_add_le16(&ad_buf, BT_UUID_BROADCAST_AUDIO_VAL);
511 net_buf_simple_add_le24(&ad_buf, broadcast_id);
512 ext_ad[0].type = BT_DATA_SVC_DATA16;
513 ext_ad[0].data_len = ad_buf.len;
514 ext_ad[0].data = ad_buf.data;
515 ext_ad[1] = (struct bt_data)BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME,
516 sizeof(CONFIG_BT_DEVICE_NAME) - 1);
517 err = bt_le_ext_adv_set_data(adv, ext_ad, 2, NULL, 0);
518 if (err != 0) {
519 printk("Failed to set extended advertising data: %d\n", err);
520 return 0;
521 }
522
523 /* Setup periodic advertising data */
524 err = bt_bap_broadcast_source_get_base(broadcast_source, &base_buf);
525 if (err != 0) {
526 printk("Failed to get encoded BASE: %d\n", err);
527 return 0;
528 }
529
530 per_ad.type = BT_DATA_SVC_DATA16;
531 per_ad.data_len = base_buf.len;
532 per_ad.data = base_buf.data;
533 err = bt_le_per_adv_set_data(adv, &per_ad, 1);
534 if (err != 0) {
535 printk("Failed to set periodic advertising data: %d\n", err);
536 return 0;
537 }
538
539 /* Start extended advertising */
540 err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT);
541 if (err) {
542 printk("Failed to start extended advertising: %d\n", err);
543 return 0;
544 }
545
546 /* Enable Periodic Advertising */
547 err = bt_le_per_adv_start(adv);
548 if (err) {
549 printk("Failed to enable periodic advertising: %d\n", err);
550 return 0;
551 }
552
553 printk("Starting broadcast source\n");
554 stopping = false;
555 err = bt_bap_broadcast_source_start(broadcast_source, adv);
556 if (err != 0) {
557 printk("Unable to start broadcast source: %d\n", err);
558 return 0;
559 }
560
561 /* Wait for all to be started */
562 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
563 k_sem_take(&sem_started, K_FOREVER);
564 }
565 printk("Broadcast source started\n");
566
567 /* Initialize sending */
568 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
569 for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
570 stream_sent_cb(&streams[i].stream);
571 }
572 }
573
574 #if defined(CONFIG_LIBLC3) && defined(CONFIG_USB_DEVICE_AUDIO)
575 /* Never stop streaming when using USB Audio as input */
576 k_sleep(K_FOREVER);
577 #endif /* defined(CONFIG_LIBLC3) && defined(CONFIG_USB_DEVICE_AUDIO) */
578 printk("Waiting %u seconds before stopped\n", BROADCAST_SOURCE_LIFETIME);
579 k_sleep(K_SECONDS(BROADCAST_SOURCE_LIFETIME));
580 printk("Stopping broadcast source\n");
581 stopping = true;
582 err = bt_bap_broadcast_source_stop(broadcast_source);
583 if (err != 0) {
584 printk("Unable to stop broadcast source: %d\n", err);
585 return 0;
586 }
587
588 /* Wait for all to be stopped */
589 for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
590 k_sem_take(&sem_stopped, K_FOREVER);
591 }
592 printk("Broadcast source stopped\n");
593
594 printk("Deleting broadcast source\n");
595 err = bt_bap_broadcast_source_delete(broadcast_source);
596 if (err != 0) {
597 printk("Unable to delete broadcast source: %d\n", err);
598 return 0;
599 }
600 printk("Broadcast source deleted\n");
601 broadcast_source = NULL;
602 seq_num = 0;
603
604 err = bt_le_per_adv_stop(adv);
605 if (err) {
606 printk("Failed to stop periodic advertising (err %d)\n", err);
607 return 0;
608 }
609
610 err = bt_le_ext_adv_stop(adv);
611 if (err) {
612 printk("Failed to stop extended advertising (err %d)\n", err);
613 return 0;
614 }
615
616 err = bt_le_ext_adv_delete(adv);
617 if (err) {
618 printk("Failed to delete extended advertising (err %d)\n", err);
619 return 0;
620 }
621 }
622 return 0;
623 }
624