@@ -38,9 +38,9 @@ static struct bt_codec lc3_codec =
3838
3939static struct bt_conn * default_conn ;
4040static struct k_work_delayable audio_send_work ;
41- static struct bt_audio_stream streams [CONFIG_BT_ASCS_ASE_SNK_COUNT + CONFIG_BT_ASCS_ASE_SRC_COUNT ];
41+ static struct bt_audio_stream sink_streams [CONFIG_BT_ASCS_ASE_SNK_COUNT ];
4242static struct bt_audio_source {
43- struct bt_audio_stream * stream ;
43+ struct bt_audio_stream stream ;
4444 uint16_t seq_num ;
4545 uint16_t max_sdu ;
4646 size_t len_to_send ;
@@ -72,7 +72,7 @@ static const struct bt_data ad[] = {
7272static uint16_t get_and_incr_seq_num (const struct bt_audio_stream * stream )
7373{
7474 for (size_t i = 0U ; i < configured_source_stream_count ; i ++ ) {
75- if (stream == source_streams [i ].stream ) {
75+ if (stream == & source_streams [i ].stream ) {
7676 return source_streams [i ].seq_num ++ ;
7777 }
7878 }
@@ -190,7 +190,7 @@ static void audio_timer_timeout(struct k_work *work)
190190 * data going to the server)
191191 */
192192 for (size_t i = 0 ; i < configured_source_stream_count ; i ++ ) {
193- struct bt_audio_stream * stream = source_streams [i ].stream ;
193+ struct bt_audio_stream * stream = & source_streams [i ].stream ;
194194
195195 buf = net_buf_alloc (& tx_pool , K_FOREVER );
196196 net_buf_reserve (buf , BT_ISO_CHAN_SEND_RESERVE );
@@ -217,13 +217,41 @@ static void audio_timer_timeout(struct k_work *work)
217217 k_work_schedule (& audio_send_work , K_MSEC (1000U ));
218218}
219219
220- static struct bt_audio_stream * stream_alloc ( void )
220+ static enum bt_audio_dir stream_dir ( const struct bt_audio_stream * stream )
221221{
222- for (size_t i = 0 ; i < ARRAY_SIZE (streams ); i ++ ) {
223- struct bt_audio_stream * stream = & streams [i ];
222+ for (size_t i = 0U ; i < ARRAY_SIZE (source_streams ); i ++ ) {
223+ if (stream == & source_streams [i ].stream ) {
224+ return BT_AUDIO_DIR_SOURCE ;
225+ }
226+ }
224227
225- if (!stream -> conn ) {
226- return stream ;
228+ for (size_t i = 0U ; i < ARRAY_SIZE (sink_streams ); i ++ ) {
229+ if (stream == & sink_streams [i ]) {
230+ return BT_AUDIO_DIR_SINK ;
231+ }
232+ }
233+
234+ __ASSERT (false, "Invalid stream %p" , stream );
235+ return 0 ;
236+ }
237+
238+ static struct bt_audio_stream * stream_alloc (enum bt_audio_dir dir )
239+ {
240+ if (dir == BT_AUDIO_DIR_SOURCE ) {
241+ for (size_t i = 0 ; i < ARRAY_SIZE (source_streams ); i ++ ) {
242+ struct bt_audio_stream * stream = & source_streams [i ].stream ;
243+
244+ if (!stream -> conn ) {
245+ return stream ;
246+ }
247+ }
248+ } else {
249+ for (size_t i = 0 ; i < ARRAY_SIZE (sink_streams ); i ++ ) {
250+ struct bt_audio_stream * stream = & sink_streams [i ];
251+
252+ if (!stream -> conn ) {
253+ return stream ;
254+ }
227255 }
228256 }
229257
@@ -238,7 +266,7 @@ static int lc3_config(struct bt_conn *conn, const struct bt_audio_ep *ep, enum b
238266
239267 print_codec (codec );
240268
241- * stream = stream_alloc ();
269+ * stream = stream_alloc (dir );
242270 if (* stream == NULL ) {
243271 printk ("No streams available\n" );
244272
@@ -248,7 +276,7 @@ static int lc3_config(struct bt_conn *conn, const struct bt_audio_ep *ep, enum b
248276 printk ("ASE Codec Config stream %p\n" , * stream );
249277
250278 if (dir == BT_AUDIO_DIR_SOURCE ) {
251- source_streams [ configured_source_stream_count ++ ]. stream = * stream ;
279+ configured_source_stream_count ++ ;
252280 }
253281
254282 * pref = qos_pref ;
@@ -284,7 +312,7 @@ static int lc3_qos(struct bt_audio_stream *stream, const struct bt_codec_qos *qo
284312 print_qos (qos );
285313
286314 for (size_t i = 0U ; i < configured_source_stream_count ; i ++ ) {
287- if (source_streams [ i ]. stream == stream ) {
315+ if (stream == & source_streams [ i ]. stream ) {
288316 source_streams [i ].max_sdu = qos -> sdu ;
289317 break ;
290318 }
@@ -335,8 +363,9 @@ static int lc3_start(struct bt_audio_stream *stream)
335363 printk ("Start: stream %p\n" , stream );
336364
337365 for (size_t i = 0U ; i < configured_source_stream_count ; i ++ ) {
338- if (source_streams [ i ]. stream == stream ) {
366+ if (stream == & source_streams [ i ]. stream ) {
339367 source_streams [i ].seq_num = 0U ;
368+ source_streams [i ].len_to_send = 0U ;
340369 break ;
341370 }
342371 }
@@ -514,13 +543,29 @@ static void stream_stopped(struct bt_audio_stream *stream)
514543 k_work_cancel_delayable (& audio_send_work );
515544}
516545
546+
547+ static void stream_enabled_cb (struct bt_audio_stream * stream )
548+ {
549+ /* The unicast server is responsible for starting sink ASEs after the
550+ * client has enabled them.
551+ */
552+ if (stream_dir (stream ) == BT_AUDIO_DIR_SINK ) {
553+ const int err = bt_audio_stream_start (stream );
554+
555+ if (err != 0 ) {
556+ printk ("Failed to start stream %p: %d" , stream , err );
557+ }
558+ }
559+ }
560+
517561static struct bt_audio_stream_ops stream_ops = {
518562#if defined(CONFIG_LIBLC3 )
519563 .recv = stream_recv_lc3_codec ,
520564#else
521565 .recv = stream_recv ,
522566#endif
523567 .stopped = stream_stopped ,
568+ .enabled = stream_enabled_cb ,
524569};
525570
526571static void connected (struct bt_conn * conn , uint8_t err )
@@ -674,8 +719,13 @@ void main(void)
674719 bt_pacs_cap_register (BT_AUDIO_DIR_SINK , & cap_sink );
675720 bt_pacs_cap_register (BT_AUDIO_DIR_SOURCE , & cap_source );
676721
677- for (size_t i = 0 ; i < ARRAY_SIZE (streams ); i ++ ) {
678- bt_audio_stream_cb_register (& streams [i ], & stream_ops );
722+ for (size_t i = 0 ; i < ARRAY_SIZE (sink_streams ); i ++ ) {
723+ bt_audio_stream_cb_register (& sink_streams [i ], & stream_ops );
724+ }
725+
726+ for (size_t i = 0 ; i < ARRAY_SIZE (source_streams ); i ++ ) {
727+ bt_audio_stream_cb_register (& source_streams [i ].stream ,
728+ & stream_ops );
679729 }
680730
681731 err = set_location ();
@@ -726,7 +776,6 @@ void main(void)
726776 }
727777
728778 /* reset data */
729- (void )memset (source_streams , 0 , sizeof (source_streams ));
730779 configured_source_stream_count = 0U ;
731780 k_work_cancel_delayable_sync (& audio_send_work , & sync );
732781
0 commit comments