From 772e99cc0f497719634a3be8aa54aafcfe0053b8 Mon Sep 17 00:00:00 2001 From: "Kasiewicz, Marek" Date: Fri, 13 Dec 2024 08:07:38 +0000 Subject: [PATCH] Add: support for saving audio to file in sample Signed-off-by: Kasiewicz, Marek --- app/src/app_base.h | 2 ++ app/src/rx_st20p_app.c | 5 +++++ app/src/rx_st30p_app.c | 28 ++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/src/app_base.h b/app/src/app_base.h index dac784ce9..0aa47e05f 100644 --- a/app/src/app_base.h +++ b/app/src/app_base.h @@ -593,6 +593,8 @@ struct st_app_rx_st30p_session { uint8_t num_port; uint64_t last_stat_time_ns; + char st30p_destination_url[ST_APP_URL_MAX_LEN]; + FILE* st30p_destination_file; /* stat */ int stat_frame_received; diff --git a/app/src/rx_st20p_app.c b/app/src/rx_st20p_app.c index f206da039..a2cb356e0 100644 --- a/app/src/rx_st20p_app.c +++ b/app/src/rx_st20p_app.c @@ -145,6 +145,11 @@ static int app_rx_st20p_uinit(struct st_app_rx_st20p_session* s) { s->handle = NULL; } + if (s->st20p_destination_file) { + fclose(s->st20p_destination_file); + s->st20p_destination_file = NULL; + } + return 0; } diff --git a/app/src/rx_st30p_app.c b/app/src/rx_st30p_app.c index b7204fe67..e15aca44d 100644 --- a/app/src/rx_st30p_app.c +++ b/app/src/rx_st30p_app.c @@ -6,10 +6,14 @@ static void app_rx_st30p_consume_frame(struct st_app_rx_st30p_session* s, struct st30_frame* frame) { - // int idx = s->idx; - // todo - MTL_MAY_UNUSED(s); - MTL_MAY_UNUSED(frame); + int idx = s->idx; + + if (s->st30p_destination_file) { + if (!fwrite(frame->addr, 1, s->st30p_frame_size, s->st30p_destination_file)) { + err("%s(%d), failed to write frame to file %s\n", __func__, idx, + s->st30p_destination_url); + } + } } static void* app_rx_st30p_frame_thread(void* arg) { @@ -69,6 +73,11 @@ static int app_rx_st30p_uinit(struct st_app_rx_st30p_session* s) { s->handle = NULL; } + if (s->st30p_destination_file) { + fclose(s->st30p_destination_file); + s->st30p_destination_file = NULL; + } + return 0; } @@ -114,6 +123,17 @@ static int app_rx_st30p_init(struct st_app_context* ctx, ops.port.udp_port[MTL_SESSION_PORT_R] = st30p ? st30p->base.udp_port : (10000 + s->idx); } + if (st30p && st30p->info.audio_url[0] != '\0') { + memcpy(s->st30p_destination_url, st30p->info.audio_url, ST_APP_URL_MAX_LEN); + s->st30p_destination_file = fopen(s->st30p_destination_url, "wb"); + + if (!s->st30p_destination_file) { + err("%s(%d), failed to open destination file %s\n", __func__, idx, + s->st30p_destination_url); + app_rx_st30p_uinit(s); + return -EIO; + } + } ops.port.payload_type = st30p ? st30p->base.payload_type : ST_APP_PAYLOAD_TYPE_AUDIO; ops.fmt = st30p ? st30p->info.audio_format : ST30_FMT_PCM24;