1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef SAMPLE_BAP_BROADCAST_SINK_STREAM_RX_H
7 #define SAMPLE_BAP_BROADCAST_SINK_STREAM_RX_H
8 
9 #include <stddef.h>
10 #include <stdint.h>
11 
12 #include <zephyr/autoconf.h>
13 #include <zephyr/bluetooth/audio/audio.h>
14 #include <zephyr/bluetooth/audio/bap.h>
15 #include <zephyr/bluetooth/iso.h>
16 #include <zephyr/kernel.h>
17 #include <zephyr/net_buf.h>
18 
19 #if defined(CONFIG_LIBLC3)
20 #include <lc3.h>
21 #endif /* defined(CONFIG_LIBLC3) */
22 
23 struct stream_rx {
24 	/* A BAP stream object */
25 	struct bt_bap_stream stream;
26 #if CONFIG_INFO_REPORTING_INTERVAL > 0
27 	/** Struct containing information useful for logging purposes */
28 	struct {
29 		/** Total Number of SDUs received */
30 		size_t recv_cnt;
31 		/** Number of lost SDUs */
32 		size_t loss_cnt;
33 		/** Number of SDUs containing errors */
34 		size_t error_cnt;
35 		/** Number of valid SDUs received */
36 		size_t valid_cnt;
37 		/** Number of empty SDUs received */
38 		size_t empty_sdu_cnt;
39 		/** Number of SDUs with duplicated packet sequence number received */
40 		size_t dup_psn_cnt;
41 		/** Number of SDUs with duplicated timestamps received */
42 		size_t dup_ts_cnt;
43 		/** The last received timestamp to track dup_ts_cnt */
44 		uint32_t last_ts;
45 		/** The last received sequence number to track dup_psn_cnt */
46 		uint16_t last_seq_num;
47 #if CONFIG_LIBLC3 > 0
48 		/** Number of SDUs decoded  */
49 		size_t lc3_decoded_cnt;
50 #endif /* CONFIG_LIBLC3 > 0 */
51 	} reporting_info;
52 #endif /* CONFIG_INFO_REPORTING_INTERVAL > 0 */
53 
54 #if defined(CONFIG_LIBLC3)
55 	/** Octets per frame - Used to validate that the incoming data is of correct size  */
56 	uint16_t lc3_octets_per_frame;
57 	/** Frame blocks per SDU - Used to split the SDU into frame blocks when decoding */
58 	uint8_t lc3_frame_blocks_per_sdu;
59 
60 	/** Number of channels - Used to split the SDU into frame blocks when decoding */
61 	uint8_t lc3_chan_cnt;
62 
63 	/**
64 	 * @brief The configured channels of the stream
65 	 *
66 	 * Used to determine whether to send data to USB and count number of channels
67 	 */
68 	enum bt_audio_location lc3_chan_allocation;
69 
70 	/** Memory use for the LC3 decoder - Supports any configuration */
71 	lc3_decoder_mem_48k_t lc3_decoder_mem;
72 	/** Reference to the LC3 decoder */
73 	lc3_decoder_t lc3_decoder;
74 #endif /* defined(CONFIG_LIBLC3) */
75 };
76 
77 /**
78  * @brief Function to call for each SDU received
79  *
80  * Will decode with LC3 and send to USB if enabled
81  */
82 void stream_rx_recv(struct bt_bap_stream *bap_stream, const struct bt_iso_recv_info *info,
83 		    struct net_buf *buf);
84 
85 size_t stream_rx_get_streaming_cnt(void);
86 int stream_rx_started(struct bt_bap_stream *bap_stream);
87 int stream_rx_stopped(struct bt_bap_stream *bap_stream);
88 void stream_rx_get_streams(
89 	struct bt_bap_stream *bap_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT]);
90 
91 #endif /* SAMPLE_BAP_BROADCAST_SINK_STREAM_RX_H */
92