Move FFmpeg callback initialization

Configure FFmpeg log redirection on start from a log helper.
This commit is contained in:
Romain Vimont 2023-02-23 12:49:42 +01:00
parent f30fd963a1
commit 10e8295aea
4 changed files with 49 additions and 39 deletions

View file

@ -75,6 +75,8 @@ main_scrcpy(int argc, char *argv[]) {
return SCRCPY_EXIT_FAILURE; return SCRCPY_EXIT_FAILURE;
} }
sc_log_configure();
#ifdef HAVE_USB #ifdef HAVE_USB
enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts) enum scrcpy_exit_code ret = args.opts.otg ? scrcpy_otg(&args.opts)
: scrcpy(&args.opts); : scrcpy(&args.opts);

View file

@ -198,43 +198,6 @@ await_for_server(bool *connected) {
return false; 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 static void
sc_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos, void *userdata) { sc_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos, void *userdata) {
(void) demuxer; (void) demuxer;
@ -426,8 +389,6 @@ scrcpy(struct scrcpy_options *options) {
recorder_initialized = true; recorder_initialized = true;
} }
av_log_set_callback(av_log_callback);
static const struct sc_demuxer_callbacks demuxer_cbs = { static const struct sc_demuxer_callbacks demuxer_cbs = {
.on_ended = sc_demuxer_on_ended, .on_ended = sc_demuxer_on_ended,
}; };

View file

@ -4,6 +4,7 @@
# include <windows.h> # include <windows.h>
#endif #endif
#include <assert.h> #include <assert.h>
#include <libavformat/avformat.h>
static SDL_LogPriority static SDL_LogPriority
log_level_sc_to_sdl(enum sc_log_level level) { 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; return true;
} }
#endif #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);
}

View file

@ -35,4 +35,7 @@ bool
sc_log_windows_error(const char *prefix, int error); sc_log_windows_error(const char *prefix, int error);
#endif #endif
void
sc_log_configure();
#endif #endif