From 67e090a50640e1d72d000b63c44c98c083894c70 Mon Sep 17 00:00:00 2001 From: David Thorpe Date: Wed, 31 Jul 2024 10:50:20 +0200 Subject: [PATCH] Updated logging --- pkg/ffmpeg/logging.go | 33 +++++++++++++++++++++++++++++++++ pkg/ffmpeg/logging_test.go | 31 +++++++++++++++++++++++++++++++ pkg/ffmpeg/resampler.go | 11 ++++++++--- sys/ffmpeg61/avutil_log.go | 27 +++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 pkg/ffmpeg/logging.go create mode 100644 pkg/ffmpeg/logging_test.go diff --git a/pkg/ffmpeg/logging.go b/pkg/ffmpeg/logging.go new file mode 100644 index 0000000..4a5c42a --- /dev/null +++ b/pkg/ffmpeg/logging.go @@ -0,0 +1,33 @@ +package ffmpeg + +import ( + "fmt" + "strings" + + // Packages + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" +) + +/////////////////////////////////////////////////////////////////////////////// +// TYPES + +// Logging function +type LogFn func(text string) + +/////////////////////////////////////////////////////////////////////////////// +// PUBLIC METHODS + +// Set logging options, including a callback function +func SetLogging(verbose bool, fn LogFn) { + ff.AVUtil_log_set_level(ff.AV_LOG_INFO) + if !verbose { + ff.AVUtil_log_set_level(ff.AV_LOG_ERROR) + } + if fn != nil { + ff.AVUtil_log_set_callback(func(level ff.AVLog, message string, userInfo any) { + fn(fmt.Sprintf("[%v] %v", level, strings.TrimSpace(message))) + }) + } else { + ff.AVUtil_log_set_callback(nil) + } +} diff --git a/pkg/ffmpeg/logging_test.go b/pkg/ffmpeg/logging_test.go new file mode 100644 index 0000000..3cec925 --- /dev/null +++ b/pkg/ffmpeg/logging_test.go @@ -0,0 +1,31 @@ +package ffmpeg_test + +import ( + "testing" + + // Packages + ffmpeg "github.com/mutablelogic/go-media/pkg/ffmpeg" + ff "github.com/mutablelogic/go-media/sys/ffmpeg61" +) + +func Test_logging_001(t *testing.T) { + // Set logging + ffmpeg.SetLogging(true, func(v string) { + t.Log(v) + }) + + ff.AVUtil_log(nil, ff.AV_LOG_INFO, "INFO test") + ff.AVUtil_log(nil, ff.AV_LOG_WARNING, "WARN test") + ff.AVUtil_log(nil, ff.AV_LOG_ERROR, "ERROR test") +} + +func Test_logging_002(t *testing.T) { + // Set logging + ffmpeg.SetLogging(false, func(v string) { + t.Log(v) + }) + + ff.AVUtil_log(nil, ff.AV_LOG_INFO, "INFO test") + ff.AVUtil_log(nil, ff.AV_LOG_WARNING, "WARN test") + ff.AVUtil_log(nil, ff.AV_LOG_ERROR, "ERROR test") +} diff --git a/pkg/ffmpeg/resampler.go b/pkg/ffmpeg/resampler.go index 9beb134..8035258 100644 --- a/pkg/ffmpeg/resampler.go +++ b/pkg/ffmpeg/resampler.go @@ -146,17 +146,22 @@ func (r *resampler) Frame(src *Frame) (*Frame, error) { //////////////////////////////////////////////////////////////////////////////// // PRIVATE METHODS -//func swrConvertFrame_(ctx *ff.SWRContext, src, dest *ff.AVFrame) error { -// return ff.SWResample_convert_frame(ctx, src, dest) -//} +func swrConvertFrame(ctx *ff.SWRContext, src, dest *ff.AVFrame) error { + return ff.SWResample_convert_frame(ctx, src, dest) +} +/* func swrConvertFrame(ctx *ff.SWRContext, src, dest *ff.AVFrame) error { + fmt.Println("swrConvertFrame", src, "=>", dest) + fmt.Println("swrConvertFrame", ff.AVUtil_samples_frame(src), "=>", ff.AVUtil_samples_frame(dest)) + _, err := ff.SWResample_convert(ctx, ff.AVUtil_samples_frame(dest), ff.AVUtil_samples_frame(src)) if err != nil { return err } return nil } +*/ func newResampler(dest, src *Frame) (*ff.SWRContext, error) { // Create a new resampler diff --git a/sys/ffmpeg61/avutil_log.go b/sys/ffmpeg61/avutil_log.go index 71b7ce7..b43e6c4 100644 --- a/sys/ffmpeg61/avutil_log.go +++ b/sys/ffmpeg61/avutil_log.go @@ -60,6 +60,33 @@ const ( var cbLog AVLogFunc +//////////////////////////////////////////////////////////////////////////////// +// STRINGIFY + +func (v AVLog) String() string { + switch v { + case AV_LOG_QUIET: + return "QUIET" + case AV_LOG_PANIC: + return "PANIC" + case AV_LOG_FATAL: + return "FATAL" + case AV_LOG_ERROR: + return "ERROR" + case AV_LOG_WARNING: + return "WARN" + case AV_LOG_INFO: + return "INFO" + case AV_LOG_VERBOSE: + return "VERBOSE" + case AV_LOG_DEBUG: + return "DEBUG" + case AV_LOG_TRACE: + return "TRACE" + } + return "[?? Invalid AVLog value]" +} + //////////////////////////////////////////////////////////////////////////////// // BINDINGS