feat(ffmpeg): logging callback handler

This commit is contained in:
Adrian Shum 2022-09-11 11:04:08 +08:00 committed by GitHub
parent f11d2b351c
commit 57541418b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 1 deletions

View file

@ -52,3 +52,8 @@ func goInterrupt(opaque unsafe.Pointer) C.int {
}
return 0
}
//export goAVLoggingHandler
func goAVLoggingHandler(level C.int, cstr *C.char) {
log(AVLogLevel(level), C.GoString(cstr))
}

View file

@ -69,4 +69,4 @@ extern int goPacketRead(void *opaque, uint8_t *buf, int buf_size);
extern int64_t goPacketSeek(void *opaque, int64_t seek, int whence);
extern int goInterrupt(void *opaque);
extern int goInterrupt(void *opaque);

12
ffmpeg/logging.c Normal file
View file

@ -0,0 +1,12 @@
#include "logging.h"
void goavLogCallback(void *class_ptr, int level, const char *fmt, va_list vl) {
char line[LINE_SZ];
int print_prefix = 1;
av_log_format_line(class_ptr, level, fmt, vl, line, LINE_SZ, &print_prefix);
goAVLoggingHandler(level, line);
}
void goavLogSetup() {
av_log_set_callback(goavLogCallback);
}

View file

@ -1,7 +1,9 @@
package ffmpeg
// #include "ffmpeg.h"
// #include "logging.h"
import "C"
import "sync"
// AVLogLevel defines the ffmpeg threshold for dumping information to stderr.
type AVLogLevel int
@ -19,6 +21,12 @@ const (
AVLogTrace
)
var (
currentLoggingHandlerFunction = noopLoggingHandler
currentLoggingVerbosity AVLogLevel
onceLogging sync.Once
)
func logLevel() AVLogLevel {
return AVLogLevel(C.av_log_get_level())
}
@ -26,4 +34,25 @@ func logLevel() AVLogLevel {
// SetFFmpegLogLevel allows you to change the log level from the default (AVLogInfo).
func SetFFmpegLogLevel(logLevel AVLogLevel) {
C.av_log_set_level(C.int(logLevel))
currentLoggingVerbosity = logLevel
}
type LoggingHandlerFunction func(messageLevel AVLogLevel, message string)
func SetLogging(handler LoggingHandlerFunction) {
onceLogging.Do(func() {
C.goavLogSetup()
})
if handler != nil {
currentLoggingHandlerFunction = handler
}
}
func noopLoggingHandler(_ AVLogLevel, _ string) {
}
func log(level AVLogLevel, message string) {
if level <= currentLoggingVerbosity {
currentLoggingHandlerFunction(level, message)
}
}

11
ffmpeg/logging.h Normal file
View file

@ -0,0 +1,11 @@
#include <stdarg.h>
#define LINE_SZ 1024
extern void goAVLoggingHandler(int level, char *str);
extern void av_log_set_callback(void (*callback)(void *, int, const char *, va_list));
extern void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl, char *line, int line_size, int *print_prefix);
void goavLogCallback(void *class_ptr, int level, const char *fmt, va_list vl);
void goavLogSetup();

View file

@ -29,6 +29,19 @@ func NewProcessor(options ...Option) *Processor {
}
func (p *Processor) Startup(_ context.Context) error {
ffmpeg.SetLogging(func(level ffmpeg.AVLogLevel, message string) {
message = strings.TrimSuffix(message, "\n")
switch level {
case ffmpeg.AVLogTrace, ffmpeg.AVLogDebug, ffmpeg.AVLogVerbose:
p.Logger.Debug("ffmpeg", zap.String("msg", message))
case ffmpeg.AVLogInfo:
p.Logger.Info("ffmpeg", zap.String("msg", message))
case ffmpeg.AVLogWarning:
p.Logger.Warn("ffmpeg", zap.String("msg", message))
case ffmpeg.AVLogError, ffmpeg.AVLogFatal, ffmpeg.AVLogPanic:
p.Logger.Error("ffmpeg", zap.String("msg", message))
}
})
if p.Debug {
ffmpeg.SetFFmpegLogLevel(ffmpeg.AVLogDebug)
} else {