From 10e8295aea0163f80d88eab234be5b918a7571eb Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 23 Feb 2023 12:49:42 +0100 Subject: [PATCH] Move FFmpeg callback initialization Configure FFmpeg log redirection on start from a log helper. --- app/src/main.c | 2 ++ app/src/scrcpy.c | 39 --------------------------------------- app/src/util/log.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ app/src/util/log.h | 3 +++ 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/app/src/main.c b/app/src/main.c index 185f1d8f..cc3a85a7 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -75,6 +75,8 @@ main_scrcpy(int argc, char *argv[]) { return SCRCPY_EXIT_FAILURE; } + sc_log_configure(); + #ifdef HAVE_USB enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts) : scrcpy(&args.opts); diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 8932dd1d..afd04d6d 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -198,43 +198,6 @@ await_for_server(bool *connected) { return false; } -static SDL_LogPriority -sdl_priority_from_av_level(int level) { - switch (level) { - case AV_LOG_PANIC: - case AV_LOG_FATAL: - return SDL_LOG_PRIORITY_CRITICAL; - case AV_LOG_ERROR: - return SDL_LOG_PRIORITY_ERROR; - case AV_LOG_WARNING: - return SDL_LOG_PRIORITY_WARN; - case AV_LOG_INFO: - return SDL_LOG_PRIORITY_INFO; - } - // do not forward others, which are too verbose - return 0; -} - -static void -av_log_callback(void *avcl, int level, const char *fmt, va_list vl) { - (void) avcl; - SDL_LogPriority priority = sdl_priority_from_av_level(level); - if (priority == 0) { - return; - } - - size_t fmt_len = strlen(fmt); - char *local_fmt = malloc(fmt_len + 10); - if (!local_fmt) { - LOG_OOM(); - return; - } - memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0' - memcpy(local_fmt + 9, fmt, fmt_len + 1); // include '\0' - SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl); - free(local_fmt); -} - static void sc_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos, void *userdata) { (void) demuxer; @@ -426,8 +389,6 @@ scrcpy(struct scrcpy_options *options) { recorder_initialized = true; } - av_log_set_callback(av_log_callback); - static const struct sc_demuxer_callbacks demuxer_cbs = { .on_ended = sc_demuxer_on_ended, }; diff --git a/app/src/util/log.c b/app/src/util/log.c index 72cd2877..ef11d2d1 100644 --- a/app/src/util/log.c +++ b/app/src/util/log.c @@ -4,6 +4,7 @@ # include #endif #include +#include static SDL_LogPriority log_level_sc_to_sdl(enum sc_log_level level) { @@ -85,3 +86,46 @@ sc_log_windows_error(const char *prefix, int error) { return true; } #endif + +static SDL_LogPriority +sdl_priority_from_av_level(int level) { + switch (level) { + case AV_LOG_PANIC: + case AV_LOG_FATAL: + return SDL_LOG_PRIORITY_CRITICAL; + case AV_LOG_ERROR: + return SDL_LOG_PRIORITY_ERROR; + case AV_LOG_WARNING: + return SDL_LOG_PRIORITY_WARN; + case AV_LOG_INFO: + return SDL_LOG_PRIORITY_INFO; + } + // do not forward others, which are too verbose + return 0; +} + +static void +sc_av_log_callback(void *avcl, int level, const char *fmt, va_list vl) { + (void) avcl; + SDL_LogPriority priority = sdl_priority_from_av_level(level); + if (priority == 0) { + return; + } + + size_t fmt_len = strlen(fmt); + char *local_fmt = malloc(fmt_len + 10); + if (!local_fmt) { + LOG_OOM(); + return; + } + memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0' + memcpy(local_fmt + 9, fmt, fmt_len + 1); // include '\0' + SDL_LogMessageV(SDL_LOG_CATEGORY_VIDEO, priority, local_fmt, vl); + free(local_fmt); +} + +void +sc_log_configure() { + // Redirect FFmpeg logs to SDL logs + av_log_set_callback(sc_av_log_callback); +} diff --git a/app/src/util/log.h b/app/src/util/log.h index 6bd8506c..8e1b73a2 100644 --- a/app/src/util/log.h +++ b/app/src/util/log.h @@ -35,4 +35,7 @@ bool sc_log_windows_error(const char *prefix, int error); #endif +void +sc_log_configure(); + #endif