Skip to content

Commit

Permalink
applications: nrf5340_audio: Add helper functions to lc3_streamer
Browse files Browse the repository at this point in the history
Add helper functions to return the file path or the loop setting
for a lc3_stream.

OCT-3109

Signed-off-by: Andreas Vibeto <[email protected]>
  • Loading branch information
andvib authored and rlubos committed Sep 16, 2024
1 parent e4fcca8 commit 3af6fcc
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 1 deletion.
1 change: 0 additions & 1 deletion applications/nrf5340_audio/src/modules/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ menuconfig NRF5340_AUDIO_SD_CARD_LC3_STREAMER
bool "Audio SD Card LC3 Streamer"
depends on NRF5340_AUDIO_SD_CARD_LC3_FILE
select EXPERIMENTAL
default n

if NRF5340_AUDIO_SD_CARD_LC3_STREAMER

Expand Down
32 changes: 32 additions & 0 deletions applications/nrf5340_audio/src/modules/lc3_streamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static int stream_close(struct lc3_stream *stream)
}

stream->state = STREAM_IDLE;
memset(stream->filename, 0, sizeof(stream->filename));

return 0;
}
Expand Down Expand Up @@ -350,6 +351,37 @@ uint8_t lc3_streamer_num_active_streams(void)
return num_active;
}

int lc3_streamer_file_path_get(const uint8_t streamer_idx, char *const path, const size_t path_len)
{
if (streamer_idx >= ARRAY_SIZE(streams)) {
LOG_ERR("Invalid streamer index %d", streamer_idx);
return -EINVAL;
}

if (path == NULL) {
LOG_ERR("Nullptr received for path");
return -EINVAL;
}

if (path_len < strlen(streams[streamer_idx].filename)) {
LOG_WRN("Path buffer too small");
}

strncpy(path, streams[streamer_idx].filename, path_len);

return 0;
}

bool lc3_streamer_is_looping(const uint8_t streamer_idx)
{
if (streamer_idx >= ARRAY_SIZE(streams)) {
LOG_ERR("Invalid streamer index %d", streamer_idx);
return false;
}

return streams[streamer_idx].loop_stream;
}

int lc3_streamer_stream_close(const uint8_t streamer_idx)
{
int ret;
Expand Down
25 changes: 25 additions & 0 deletions applications/nrf5340_audio/src/modules/lc3_streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ int lc3_streamer_stream_register(const char *const filename, uint8_t *const stre
*/
uint8_t lc3_streamer_num_active_streams(void);

/**
* @brief Get the file path for a stream.
*
* @details If path buffer is smaller than the length of the actual path, the path will be
* truncated.
*
* @param[in] streamer_idx Index of the streamer.
* @param[out] path Pointer for string to store filepath in.
* @param[in] path_len Length of string buffer.
*
* @retval -EINVAL Nullpointers or invalid index given.
* @retval 0 Success.
*/
int lc3_streamer_file_path_get(const uint8_t streamer_idx, char *const path, const size_t path_len);

/**
* @brief Check if a stream is configured to loop.
*
* @param[in] streamer_idx Index of the streamer.
*
* @retval true Streamer is configured to loop.
* @retval false Streamer is not configured to loop, or the streamer index is too high.
*/
bool lc3_streamer_is_looping(const uint8_t streamer_idx);

/**
* @brief End a stream that's playing.
*
Expand Down
86 changes: 86 additions & 0 deletions tests/nrf5340_audio/lc3_streamer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,90 @@ ZTEST(lc3_streamer, test_lc3_streamer_stream_close_invalid_index)
zassert_equal(-EINVAL, ret, "lc3_streamer_stream_close should return -EINVAL");
}

ZTEST(lc3_streamer, test_lc3_streamer_file_path_get_valid)
{
int ret;
uint8_t streamer_idx;
char file_path[CONFIG_FS_FATFS_MAX_LFN];

char test_string_1[] = "test_string_1";

ret = lc3_streamer_stream_register(test_string_1, &streamer_idx, false);
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_equal(0, streamer_idx, "lc3_streamer_stream_register should return index 0");

ret = lc3_streamer_file_path_get(streamer_idx, file_path, sizeof(file_path));
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_mem_equal(test_string_1, file_path, sizeof(test_string_1),
"Strings for file path doesn't match");

ret = lc3_streamer_stream_close(streamer_idx);
zassert_equal(0, ret, "lc3_streamer_close_stream should return success");

ret = lc3_streamer_file_path_get(streamer_idx, file_path, sizeof(file_path));
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_equal(0, strlen(file_path), "File path should be empty");
}

ZTEST(lc3_streamer, test_lc3_streamer_file_path_get_invalid)
{
int ret;
uint8_t streamer_idx;
char file_path[CONFIG_FS_FATFS_MAX_LFN];

ret = lc3_streamer_file_path_get(CONFIG_SD_CARD_LC3_STREAMER_MAX_NUM_STREAMS, file_path,
sizeof(file_path));
zassert_equal(-EINVAL, ret,
"lc3_streamer_file_path_get should return -EINVAL on invalid index");

char test_string_1[] = "test_string_1";

ret = lc3_streamer_stream_register(test_string_1, &streamer_idx, false);
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_equal(0, streamer_idx, "lc3_streamer_stream_register should return index 0");

ret = lc3_streamer_file_path_get(streamer_idx, NULL, CONFIG_FS_FATFS_MAX_LFN);
zassert_equal(-EINVAL, ret, "lc3_streamer_file_path_get should return -EINVAL on nullptr");

size_t small_buf_size = 4;
uint8_t too_small_buffer[sizeof(test_string_1)];

ret = lc3_streamer_file_path_get(streamer_idx, too_small_buffer, small_buf_size);
zassert_equal(0, ret, "lc3_streamer_file_path_get should return success");
zassert_equal(small_buf_size, strlen(too_small_buffer),
"File path should be truncated to buffer size");
zassert_mem_equal(test_string_1, too_small_buffer, small_buf_size,
"Strings for file path doesn't match");
}

ZTEST(lc3_streamer, test_lc3_streamer_is_looping_valid)
{
int ret;
bool is_looping;
uint8_t streamer_idx;

ret = lc3_streamer_stream_register("test", &streamer_idx, false);
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_equal(0, streamer_idx, "lc3_streamer_stream_register should return index 0");

is_looping = lc3_streamer_is_looping(streamer_idx);
zassert_equal(false, is_looping, "Stream should not be looping");

ret = lc3_streamer_stream_register("test", &streamer_idx, true);
zassert_equal(0, ret, "lc3_streamer_stream_register should return success");
zassert_equal(1, streamer_idx, "lc3_streamer_stream_register should return index 1");

is_looping = lc3_streamer_is_looping(streamer_idx);
zassert_equal(true, is_looping, "Stream should be looping");
}

ZTEST(lc3_streamer, test_lc3_streamer_is_looping_invalid)
{
bool is_looping;

is_looping = lc3_streamer_is_looping(CONFIG_SD_CARD_LC3_STREAMER_MAX_NUM_STREAMS);
zassert_equal(false, is_looping,
"lc3_streamer_is_looping should return false on an invalid index");
}

ZTEST_SUITE(lc3_streamer, NULL, suite_setup, test_setup, test_teardown, NULL);

0 comments on commit 3af6fcc

Please sign in to comment.