Skip to content

Commit

Permalink
Add sweeping and changed encode defaults
Browse files Browse the repository at this point in the history
SciLor committed Mar 30, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent fe7c91d commit 5493d1c
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion include/settings.h
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
#define TONIESV2_CUSTOM_JSON_FILE "tonies.custom.json"
#define CONFIG_FILE "config.ini"
#define CONFIG_OVERLAY_FILE "config.overlay.ini"
#define CONFIG_VERSION 8
#define CONFIG_VERSION 9
#define MAX_OVERLAYS 16 + 1

typedef enum
@@ -78,6 +78,8 @@ typedef struct
uint32_t bitrate;
uint32_t ffmpeg_stream_buffer_ms;
bool ffmpeg_stream_restart;
bool ffmpeg_sweep_startup_buffer;
uint32_t ffmpeg_sweep_delay_ms;

} settings_encode_t;

3 changes: 2 additions & 1 deletion include/toniefile.h
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ typedef struct
size_t skip_seconds;
char *targetFile;
bool_t append;
bool_t sweep;
OsTaskId taskId;
error_t error;
bool_t quit;
@@ -51,6 +52,6 @@ FILE *ffmpeg_decode_audio_start(const char *input_source);
FILE *ffmpeg_decode_audio_start_skip(const char *input_source, size_t skip_seconds);
error_t ffmpeg_decode_audio_end(FILE *ffmpeg_pipe, error_t error);
error_t ffmpeg_decode_audio(FILE *ffmpeg_pipe, int16_t *buffer, size_t size, size_t *bytes_read);
error_t ffmpeg_stream(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds, bool_t *active, bool_t append);
error_t ffmpeg_stream(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds, bool_t *active, bool_t *sweep, bool_t append);
error_t ffmpeg_convert(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds);
void ffmpeg_stream_task(void *param);
7 changes: 7 additions & 0 deletions src/handler_cloud.c
Original file line number Diff line number Diff line change
@@ -450,6 +450,7 @@ error_t handleCloudContent(HttpConnection *connection, const char_t *uri, const
ffmpeg_ctx->active = false;
ffmpeg_ctx->quit = false;
ffmpeg_ctx->append = (connection->request.Range.start != 0);
ffmpeg_ctx->sweep = client_ctx->settings->encode.ffmpeg_sweep_startup_buffer;
ffmpeg_ctx->source = tonieInfo->json.source;
ffmpeg_ctx->skip_seconds = tonieInfo->json.skip_seconds;
ffmpeg_ctx->targetFile = tonieInfo->json._streamFile;
@@ -462,8 +463,14 @@ error_t handleCloudContent(HttpConnection *connection, const char_t *uri, const
}
if (ffmpeg_ctx->error == NO_ERROR)
{
if (client_ctx->settings->encode.ffmpeg_sweep_startup_buffer)
{
osDelayTask(client_ctx->settings->encode.ffmpeg_sweep_delay_ms);
}

uint32_t delay = client_ctx->settings->encode.ffmpeg_stream_buffer_ms;
TRACE_INFO("Serve streaming content from %s, delay %" PRIu32 "ms\r\n", tonieInfo->json.source, delay);
ffmpeg_ctx->sweep = false;
if (!ffmpeg_ctx->append)
{
osDelayTask(delay);
4 changes: 3 additions & 1 deletion src/settings.c
Original file line number Diff line number Diff line change
@@ -187,7 +187,9 @@ static void option_map_init(uint8_t settingsId)
OPTION_TREE_DESC("encode", "TAF encoding")
OPTION_UNSIGNED("encode.bitrate", &settings->encode.bitrate, 96, 0, 256, "Opus bitrate", "Opus bitrate, tested 64, 96(default), 128, 192, 256 - be aware that this increases the TAF size!")
OPTION_UNSIGNED("encode.ffmpeg_stream_buffer_ms", &settings->encode.ffmpeg_stream_buffer_ms, 1000, 0, 60000, "Stream buffer ms", "Stream buffer for ffmpeg based streaming.")
OPTION_BOOL("encode.ffmpeg_stream_restart", &settings->encode.ffmpeg_stream_restart, TRUE, "Stream force restart", "If a stream is continued by the box, a new file is forced. This has the cost of a slower restart, but does not play the old buffered content and deletes the previous stream data on the box.")
OPTION_BOOL("encode.ffmpeg_stream_restart", &settings->encode.ffmpeg_stream_restart, FALSE, "Stream force restart", "If a stream is continued by the box, a new file is forced. This has the cost of a slower restart, but does not play the old buffered content and deletes the previous stream data on the box.")
OPTION_BOOL("encode.ffmpeg_sweep_startup_buffer", &settings->encode.ffmpeg_sweep_startup_buffer, TRUE, "Sweep stream prebuffer", "Webradio streams often send several seconds as a buffer immediately. This may contain ads and will add up if you disalbe 'Stream force restart'.")
OPTION_UNSIGNED("encode.ffmpeg_sweep_delay_ms", &settings->encode.ffmpeg_sweep_delay_ms, 2000, 0, 10000, "Sweep delay ms", "Wait x ms until sweeping is stopped and stream is started. Delays stream start, but may increase success.")

OPTION_TREE_DESC("toniebox", "Toniebox")
OPTION_BOOL("toniebox.overrideCloud", &settings->toniebox.overrideCloud, TRUE, "Override cloud settings", "Override tonies cloud settings for the toniebox with those set here")
12 changes: 8 additions & 4 deletions src/toniefile.c
Original file line number Diff line number Diff line change
@@ -634,10 +634,11 @@ error_t ffmpeg_decode_audio(FILE *ffmpeg_pipe, int16_t *buffer, size_t size, siz
error_t ffmpeg_convert(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds)
{
bool_t active = true;
return ffmpeg_stream(source, source_len, target_taf, skip_seconds, &active, false);
bool_t sweep = false;
return ffmpeg_stream(source, source_len, target_taf, skip_seconds, &active, &sweep, false);
}

error_t ffmpeg_stream(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds, bool_t *active, bool_t append)
error_t ffmpeg_stream(char source[99][PATH_LEN], size_t source_len, const char *target_taf, size_t skip_seconds, bool_t *active, bool_t *sweep, bool_t append)
{
TRACE_INFO("Encode %" PRIuSIZE " sources: \r\n", source_len);
for (size_t i = 0; i < source_len; i++)
@@ -702,7 +703,10 @@ error_t ffmpeg_stream(char source[99][PATH_LEN], size_t source_len, const char *
}
break;
}
error = toniefile_encode(taf, sample_buffer, blocks_read / OPUS_CHANNELS);
if (*sweep == false)
{
error = toniefile_encode(taf, sample_buffer, blocks_read / OPUS_CHANNELS);
}
if (error != NO_ERROR && error != ERROR_END_OF_STREAM)
{
TRACE_ERROR("Could not encode toniesample error=%s\r\n", error2text(error));
@@ -731,7 +735,7 @@ void ffmpeg_stream_task(void *param)
ffmpeg_stream_ctx_t *ctx = (ffmpeg_stream_ctx_t *)param;
char source[99][PATH_LEN]; // waste memory, but warning otherwise
strncpy(source[0], ctx->source, PATH_LEN - 1);
ctx->error = ffmpeg_stream(source, 1, ctx->targetFile, ctx->skip_seconds, &ctx->active, ctx->append);
ctx->error = ffmpeg_stream(source, 1, ctx->targetFile, ctx->skip_seconds, &ctx->active, &ctx->sweep, ctx->append);
ctx->quit = true;
osDeleteTask(OS_SELF_TASK_ID);
}

0 comments on commit 5493d1c

Please sign in to comment.