Skip to content

Commit 95b135a

Browse files
alexsvenkoffes
authored andcommitted
applications: nrf5340_audio: Use net_buf directly for audio
- Remove all calls to data_fifo - Use net_buf throughout the system for audio - Insert metadata into the net_buf as user data - OCT-3379 Signed-off-by: Alexander Svensen <[email protected]>
1 parent e260850 commit 95b135a

File tree

38 files changed

+441
-517
lines changed

38 files changed

+441
-517
lines changed

applications/nrf5340_audio/Kconfig.defaults

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ config PCM_MIX
5757
config PSCM
5858
default y
5959

60-
config DATA_FIFO
61-
default y
62-
6360
# Enable NRFX_CLOCK for ACLK control
6461
config NRFX_CLOCK
6562
default y

applications/nrf5340_audio/broadcast_sink/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ uint8_t stream_state_get(void)
568568
return strm_state;
569569
}
570570

571-
void streamctrl_send(struct audio_data const *const audio_frame)
571+
void streamctrl_send(struct net_buf const *const audio_frame)
572572
{
573573
ARG_UNUSED(audio_frame);
574574

applications/nrf5340_audio/broadcast_source/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,13 @@ uint8_t stream_state_get(void)
494494
return strm_state;
495495
}
496496

497-
void streamctrl_send(struct audio_data const *const audio_frame)
497+
void streamctrl_send(struct net_buf const *const audio_frame)
498498
{
499499
int ret;
500500
static int prev_ret;
501501

502502
if (strm_state == STATE_STREAMING) {
503-
ret = broadcast_source_send(0, 0, audio_frame);
503+
ret = broadcast_source_send(audio_frame, 0, 0);
504504

505505
if (ret != 0 && ret != prev_ret) {
506506
if (ret == -ECANCELED) {

applications/nrf5340_audio/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CONFIG_THREAD_RUNTIME_STATS=y
1818
CONFIG_STACK_SENTINEL=y
1919
CONFIG_INIT_STACKS=y
2020
CONFIG_BT=y
21+
CONFIG_NET_BUF_POOL_USAGE=y
2122

2223
# Uart driver
2324
CONFIG_SERIAL=y

applications/nrf5340_audio/prj_fota.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CONFIG_STACK_USAGE=y
1818
CONFIG_THREAD_RUNTIME_STATS=y
1919
CONFIG_STACK_SENTINEL=y
2020
CONFIG_INIT_STACKS=y
21+
CONFIG_NET_BUF_POOL_USAGE=y
2122

2223
# Uart driver
2324
CONFIG_SERIAL=y

applications/nrf5340_audio/prj_release.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CONFIG_CONSOLE=n
2020
CONFIG_PRINTK=n
2121
CONFIG_UART_CONSOLE=n
2222
CONFIG_BOOT_BANNER=n
23+
CONFIG_NET_BUF_POOL_USAGE=y
2324

2425
# Bluetooth
2526
# Default value from src/bluetooth/Kconfig.defaults.

applications/nrf5340_audio/src/audio/audio_datapath.c

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <zephyr/zbus/zbus.h>
1313
#include <zephyr/kernel.h>
1414
#include <zephyr/shell/shell.h>
15-
#include <zephyr/net_buf.h>
1615
#include <nrfx_clock.h>
1716
#include <contin_array.h>
1817
#include <tone.h>
@@ -92,7 +91,8 @@ LOG_MODULE_REGISTER(audio_datapath, CONFIG_AUDIO_DATAPATH_LOG_LEVEL);
9291
/* How often to print under-run warning */
9392
#define UNDERRUN_LOG_INTERVAL_BLKS 5000
9493

95-
NET_BUF_POOL_FIXED_DEFINE(pool_i2s_rx, FIFO_NUM_BLKS, BLK_STEREO_SIZE_OCTETS, 8, net_buf_destroy);
94+
NET_BUF_POOL_FIXED_DEFINE(pool_i2s_rx, FIFO_NUM_BLKS, BLK_STEREO_SIZE_OCTETS,
95+
sizeof(struct audio_metadata), NULL);
9696

9797
enum drift_comp_state {
9898
DRIFT_STATE_INIT, /* Waiting for data to be received */
@@ -128,7 +128,7 @@ static struct {
128128
void *decoded_data;
129129

130130
struct {
131-
struct data_fifo *fifo;
131+
struct k_msgq *audio_q;
132132
} in;
133133

134134
struct {
@@ -693,69 +693,60 @@ static void audio_datapath_i2s_blk_complete(uint32_t frame_start_ts_us, uint32_t
693693

694694
/********** I2S RX **********/
695695
static int prev_ret;
696-
struct audio_data *rx_audio_block = NULL;
697-
struct net_buf *buf_in;
696+
struct net_buf *rx_audio_block = NULL;
698697

699698
if (IS_ENABLED(CONFIG_STREAM_BIDIRECTIONAL) || (CONFIG_AUDIO_DEV == GATEWAY)) {
700699
if (rx_buf_released == NULL) {
701700
/* No RX data available */
702701
ERR_CHK_MSG(-ENOMEM, "No RX data available");
703702
}
704703

705-
buf_in = net_buf_alloc(&pool_i2s_rx, K_NO_WAIT);
706-
if (buf_in == NULL) {
707-
ERR_CHK_MSG(-ENOMEM, "Out of RX buffers for I2S");
704+
if (k_msgq_num_free_get(ctrl_blk.in.audio_q) == 0 || pool_i2s_rx.avail_count == 0) {
705+
ret = -ENOMEM;
706+
} else {
707+
ret = 0;
708708
}
709709

710-
/* Store RX buffer in net_buf */
711-
net_buf_add_mem(buf_in, rx_buf_released, BLK_STEREO_SIZE_OCTETS);
712-
713-
/* Get first available audio block */
714-
ret = data_fifo_pointer_first_vacant_get(ctrl_blk.in.fifo, (void *)&rx_audio_block,
715-
K_NO_WAIT);
716710
/* If RX FIFO is filled up */
717711
if (ret == -ENOMEM) {
718-
struct audio_data *stale_i2s_data;
719-
size_t stale_i2s_data_size;
712+
struct net_buf *stale_i2s_data;
720713

721714
if (ret != prev_ret) {
722715
LOG_WRN("I2S RX overrun. Single msg");
723716
prev_ret = ret;
724717
}
725718

726-
ret = data_fifo_pointer_last_filled_get(ctrl_blk.in.fifo,
727-
(void *)&stale_i2s_data,
728-
&stale_i2s_data_size, K_NO_WAIT);
719+
ret = k_msgq_get(ctrl_blk.in.audio_q, (void *)&stale_i2s_data, K_NO_WAIT);
729720
ERR_CHK(ret);
730721

731-
struct net_buf *buf = stale_i2s_data->data;
722+
net_buf_unref(stale_i2s_data);
732723

733-
net_buf_unref(buf);
734-
data_fifo_block_free(ctrl_blk.in.fifo, stale_i2s_data);
735-
736-
ret = data_fifo_pointer_first_vacant_get(
737-
ctrl_blk.in.fifo, (void *)&rx_audio_block, K_NO_WAIT);
738724
} else if (ret == 0 && prev_ret == -ENOMEM) {
739725
LOG_WRN("I2S RX continuing stream");
740726
prev_ret = ret;
741727
}
742728

743-
rx_audio_block->data = buf_in;
744-
rx_audio_block->data_size = BLK_STEREO_SIZE_OCTETS;
729+
rx_audio_block = net_buf_alloc(&pool_i2s_rx, K_NO_WAIT);
730+
if (rx_audio_block == NULL) {
731+
ERR_CHK_MSG(-ENOMEM, "Out of RX buffers for I2S");
732+
}
733+
734+
/* Store RX buffer in net_buf */
735+
net_buf_add_mem(rx_audio_block, rx_buf_released, BLK_STEREO_SIZE_OCTETS);
736+
737+
struct audio_metadata *meta = net_buf_user_data(rx_audio_block);
745738

746739
/* Add meta data */
747-
rx_audio_block->meta.data_coding = PCM;
748-
rx_audio_block->meta.data_len_us = 1000;
749-
rx_audio_block->meta.sample_rate_hz = CONFIG_AUDIO_SAMPLE_RATE_HZ;
750-
rx_audio_block->meta.bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS;
751-
rx_audio_block->meta.carried_bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS;
752-
rx_audio_block->meta.locations =
753-
BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT;
754-
rx_audio_block->meta.bad_data = false;
755-
756-
ret = data_fifo_block_lock(ctrl_blk.in.fifo, (void *)&rx_audio_block,
757-
sizeof(struct audio_data));
758-
ERR_CHK_MSG(ret, "Unable to lock block RX");
740+
meta->data_coding = PCM;
741+
meta->data_len_us = 1000;
742+
meta->sample_rate_hz = CONFIG_AUDIO_SAMPLE_RATE_HZ;
743+
meta->bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS;
744+
meta->carried_bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS;
745+
meta->locations = BT_AUDIO_LOCATION_FRONT_LEFT | BT_AUDIO_LOCATION_FRONT_RIGHT;
746+
meta->bad_data = false;
747+
748+
ret = k_msgq_put(ctrl_blk.in.audio_q, (void *)&rx_audio_block, K_NO_WAIT);
749+
ERR_CHK_MSG(ret, "Unable to put RX audio block into queue");
759750
}
760751

761752
/*** Data exchange ***/
@@ -907,32 +898,29 @@ void audio_datapath_pres_delay_us_get(uint32_t *delay_us)
907898
*delay_us = ctrl_blk.pres_comp.pres_delay_us;
908899
}
909900

910-
void audio_datapath_stream_out(struct audio_data *audio_frame)
901+
void audio_datapath_stream_out(struct net_buf *audio_frame)
911902
{
912903
if (!ctrl_blk.stream_started) {
913904
LOG_WRN("Stream not started");
914905
return;
915906
}
916907

917-
/*** Check incoming data ***/
918-
struct net_buf *audio_buf = audio_frame->data;
919-
920-
if (!audio_buf->data) {
908+
if (audio_frame == NULL) {
921909
LOG_ERR("Buffer pointer is NULL");
922910
}
923911

924-
if (audio_frame->meta.reference_ts_us == ctrl_blk.prev_pres_sdu_ref_us &&
925-
audio_frame->meta.reference_ts_us != 0) {
926-
LOG_WRN("Duplicate sdu_ref_us (%d) - Dropping audio frame",
927-
audio_frame->meta.reference_ts_us);
912+
/*** Check incoming data ***/
913+
struct audio_metadata *meta = net_buf_user_data(audio_frame);
914+
915+
if (meta->reference_ts_us == ctrl_blk.prev_pres_sdu_ref_us && meta->reference_ts_us != 0) {
916+
LOG_WRN("Duplicate sdu_ref_us (%d) - Dropping audio frame", meta->reference_ts_us);
928917
return;
929918
}
930919

931920
bool sdu_ref_not_consecutive = false;
932921

933922
if (ctrl_blk.prev_pres_sdu_ref_us) {
934-
uint32_t sdu_ref_delta_us =
935-
audio_frame->meta.reference_ts_us - ctrl_blk.prev_pres_sdu_ref_us;
923+
uint32_t sdu_ref_delta_us = meta->reference_ts_us - ctrl_blk.prev_pres_sdu_ref_us;
936924

937925
/* Check if the delta is from two consecutive frames */
938926
if (sdu_ref_delta_us <
@@ -946,8 +934,8 @@ void audio_datapath_stream_out(struct audio_data *audio_frame)
946934
sdu_ref_delta_us);
947935

948936
/* Estimate sdu_ref_us */
949-
audio_frame->meta.reference_ts_us = ctrl_blk.prev_pres_sdu_ref_us +
950-
CONFIG_AUDIO_FRAME_DURATION_US;
937+
meta->reference_ts_us = ctrl_blk.prev_pres_sdu_ref_us +
938+
CONFIG_AUDIO_FRAME_DURATION_US;
951939
}
952940
} else {
953941
LOG_INF("sdu_ref_us not from consecutive frames (diff: %d us)",
@@ -956,12 +944,11 @@ void audio_datapath_stream_out(struct audio_data *audio_frame)
956944
}
957945
}
958946

959-
ctrl_blk.prev_pres_sdu_ref_us = audio_frame->meta.reference_ts_us;
947+
ctrl_blk.prev_pres_sdu_ref_us = meta->reference_ts_us;
960948

961949
/*** Presentation compensation ***/
962950
if (ctrl_blk.pres_comp.enabled) {
963-
audio_datapath_presentation_compensation(audio_frame->meta.data_rx_ts_us,
964-
audio_frame->meta.reference_ts_us,
951+
audio_datapath_presentation_compensation(meta->data_rx_ts_us, meta->reference_ts_us,
965952
sdu_ref_not_consecutive);
966953
}
967954

@@ -1012,26 +999,25 @@ void audio_datapath_stream_out(struct audio_data *audio_frame)
1012999
}
10131000

10141001
/* Record producer block start reference */
1015-
ctrl_blk.out.prod_blk_ts[out_blk_idx] =
1016-
audio_frame->meta.data_rx_ts_us + (i * BLK_PERIOD_US);
1002+
ctrl_blk.out.prod_blk_ts[out_blk_idx] = meta->data_rx_ts_us + (i * BLK_PERIOD_US);
10171003

10181004
out_blk_idx = NEXT_IDX(out_blk_idx);
10191005
}
10201006

10211007
ctrl_blk.out.prod_blk_idx = out_blk_idx;
10221008
}
10231009

1024-
int audio_datapath_start(struct data_fifo *fifo_rx)
1010+
int audio_datapath_start(struct k_msgq *audio_q_rx)
10251011
{
1026-
__ASSERT_NO_MSG(fifo_rx != NULL);
1012+
__ASSERT_NO_MSG(audio_q_rx != NULL);
10271013

10281014
if (!ctrl_blk.datapath_initialized) {
10291015
LOG_WRN("Audio datapath not initialized");
10301016
return -ECANCELED;
10311017
}
10321018

10331019
if (!ctrl_blk.stream_started) {
1034-
ctrl_blk.in.fifo = fifo_rx;
1020+
ctrl_blk.in.audio_q = audio_q_rx;
10351021

10361022
/* Clear counters and mute initial audio */
10371023
memset(&ctrl_blk.out, 0, sizeof(ctrl_blk.out));

applications/nrf5340_audio/src/audio/audio_datapath.h

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,76 @@
1010
#include <zephyr/kernel.h>
1111
#include <stdint.h>
1212
#include <stdbool.h>
13-
#include <data_fifo.h>
13+
#include <zephyr/net_buf.h>
1414

1515
#include "sw_codec_select.h"
1616
#include "audio_defines.h"
1717

1818
/**
19-
* @brief Mixes a tone into the I2S TX stream
19+
* @brief Mixes a tone into the I2S TX stream.
2020
*
21-
* @param freq Tone frequency [Hz]
22-
* @param dur_ms Tone duration [ms]. 0 = forever
23-
* @param amplitude Tone amplitude [0, 1]
21+
* @param freq Tone frequency [Hz].
22+
* @param dur_ms Tone duration [ms]. (0 == forever)
23+
* @param amplitude Tone amplitude [0, 1].
2424
*
25-
* @return 0 if successful, error otherwise
25+
* @return 0 if successful, error otherwise.
2626
*/
2727
int audio_datapath_tone_play(uint16_t freq, uint16_t dur_ms, float amplitude);
2828

2929
/**
30-
* @brief Stops tone playback
30+
* @brief Stops tone playback.
3131
*/
3232
void audio_datapath_tone_stop(void);
3333

3434
/**
35-
* @brief Set the presentation delay
35+
* @brief Set the presentation delay.
3636
*
37-
* @param delay_us The presentation delay in µs
37+
* @param delay_us The presentation delay in µs.
3838
*
39-
* @return 0 if successful, error otherwise
39+
* @return 0 if successful, error otherwise.
4040
*/
4141
int audio_datapath_pres_delay_us_set(uint32_t delay_us);
4242

4343
/**
44-
* @brief Get the current presentation delay
44+
* @brief Get the current presentation delay.
4545
*
46-
* @param delay_us The presentation delay in µs
46+
* @param delay_us The presentation delay in µs.
4747
*/
4848
void audio_datapath_pres_delay_us_get(uint32_t *delay_us);
4949

5050
/**
51-
* @brief Input an audio data frame which is processed and outputted over I2S
51+
* @brief Input an audio data frame which is processed and outputted over I2S.
5252
*
53-
* @note A frame of raw encoded audio data is inputted, and this data then is decoded
54-
* and processed before being outputted over I2S. The audio is synchronized
55-
* using sdu_ref_us
53+
* @note A frame of raw encoded audio data is inputted, and this data then is decoded
54+
* and processed before being outputted over I2S. The audio is synchronized
55+
* using sdu_ref_us.
5656
*
57-
* @param audio_frame Pointer to the audio data frame to be processed
57+
* @param audio_frame Pointer to the audio buffer.
5858
*/
59-
void audio_datapath_stream_out(struct audio_data *audio_frame);
59+
void audio_datapath_stream_out(struct net_buf *audio_frame);
6060

6161
/**
62-
* @brief Start the audio datapath module
62+
* @brief Start the audio datapath module.
6363
*
64-
* @note The continuously running I2S is started
64+
* @note The continuously running I2S is started.
6565
*
66-
* @param fifo_rx Pointer to FIFO structure where I2S RX data is put
66+
* @param queue_rx Pointer to the queue structure where I2S RX data is put.
6767
*
68-
* @return 0 if successful, error otherwise
68+
* @return 0 if successful, error otherwise.
6969
*/
70-
int audio_datapath_start(struct data_fifo *fifo_rx);
70+
int audio_datapath_start(struct k_msgq *queue_rx);
7171

7272
/**
73-
* @brief Stop the audio datapath module
73+
* @brief Stop the audio datapath module.
7474
*
75-
* @return 0 if successful, error otherwise
75+
* @return 0 if successful, error otherwise.
7676
*/
7777
int audio_datapath_stop(void);
7878

7979
/**
80-
* @brief Initialize the audio datapath module
80+
* @brief Initialize the audio datapath module.
8181
*
82-
* @return 0 if successful, error otherwise
82+
* @return 0 if successful, error otherwise.
8383
*/
8484
int audio_datapath_init(void);
8585

0 commit comments

Comments
 (0)