|
| 1 | +package srtgo |
| 2 | + |
| 3 | +/* |
| 4 | +#cgo LDFLAGS: -lsrt |
| 5 | +#include <srt/srt.h> |
| 6 | +extern void srtLogCB(void* opaque, int level, const char* file, int line, const char* area, const char* message); |
| 7 | +*/ |
| 8 | +import "C" |
| 9 | + |
| 10 | +import ( |
| 11 | + "sync" |
| 12 | + "unsafe" |
| 13 | + |
| 14 | + gopointer "github.com/mattn/go-pointer" |
| 15 | +) |
| 16 | + |
| 17 | +type LogCallBackFunc func(level SrtLogLevel, file string, line int, area, message string) |
| 18 | + |
| 19 | +type SrtLogLevel int |
| 20 | + |
| 21 | +const ( |
| 22 | + // SrtLogLevelEmerg = int(C.LOG_EMERG) |
| 23 | + // SrtLogLevelAlert = int(C.LOG_ALERT) |
| 24 | + SrtLogLevelCrit SrtLogLevel = SrtLogLevel(C.LOG_CRIT) |
| 25 | + SrtLogLevelErr SrtLogLevel = SrtLogLevel(C.LOG_ERR) |
| 26 | + SrtLogLevelWarning SrtLogLevel = SrtLogLevel(C.LOG_WARNING) |
| 27 | + SrtLogLevelNotice SrtLogLevel = SrtLogLevel(C.LOG_NOTICE) |
| 28 | + SrtLogLevelInfo SrtLogLevel = SrtLogLevel(C.LOG_INFO) |
| 29 | + SrtLogLevelDebug SrtLogLevel = SrtLogLevel(C.LOG_DEBUG) |
| 30 | + SrtLogLevelTrace SrtLogLevel = SrtLogLevel(8) |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + logCBPtr unsafe.Pointer = nil |
| 35 | + logCBPtrLock sync.Mutex |
| 36 | +) |
| 37 | + |
| 38 | +//export srtLogCBWrapper |
| 39 | +func srtLogCBWrapper(arg unsafe.Pointer, level C.int, file *C.char, line C.int, area, message *C.char) { |
| 40 | + userCB := gopointer.Restore(arg).(LogCallBackFunc) |
| 41 | + go userCB(SrtLogLevel(level), C.GoString(file), int(line), C.GoString(area), C.GoString(message)) |
| 42 | +} |
| 43 | + |
| 44 | +func SrtSetLogLevel(level SrtLogLevel) { |
| 45 | + C.srt_setloglevel(C.int(level)) |
| 46 | +} |
| 47 | + |
| 48 | +func SrtSetLogHandler(cb LogCallBackFunc) { |
| 49 | + ptr := gopointer.Save(cb) |
| 50 | + C.srt_setloghandler(ptr, (*C.SRT_LOG_HANDLER_FN)(C.srtLogCB)) |
| 51 | + logCBPtrLock.Lock() |
| 52 | + storeLogCBPtr(ptr) |
| 53 | +} |
| 54 | + |
| 55 | +func SrtUnsetLogHandler() { |
| 56 | + C.srt_setloghandler(nil, nil) |
| 57 | + storeLogCBPtr(nil) |
| 58 | +} |
| 59 | + |
| 60 | +func storeLogCBPtr(ptr unsafe.Pointer) { |
| 61 | + logCBPtrLock.Lock() |
| 62 | + defer logCBPtrLock.Unlock() |
| 63 | + if logCBPtr != nil { |
| 64 | + gopointer.Unref(logCBPtr) |
| 65 | + } |
| 66 | + logCBPtr = nil |
| 67 | +} |
0 commit comments