Code refactoring for #1450

This commit is contained in:
Alex X
2024-11-11 17:45:55 +03:00
parent 831aa03c9f
commit 570b7d0d97
3 changed files with 17 additions and 37 deletions
+3 -2
View File
@@ -223,8 +223,9 @@ func parseArgs(s string) *ffmpeg.Args {
s += "?video&audio" s += "?video&audio"
} }
s += "&source=ffmpeg:" + url.QueryEscape(source) s += "&source=ffmpeg:" + url.QueryEscape(source)
// change codec not matched error level to debug for _, v := range query["query"] {
s += "&" + string(streams.CodecNotMatchedErrorCode) + "=" + zerolog.DebugLevel.String() s += "&" + v
}
args.Input = inputTemplate("rtsp", s, query) args.Input = inputTemplate("rtsp", s, query)
} else if i = strings.Index(s, "?"); i > 0 { } else if i = strings.Index(s, "?"); i > 0 {
switch s[:i] { switch s[:i] {
+10 -11
View File
@@ -147,6 +147,7 @@ func tcpHandler(conn *rtsp.Conn) {
var closer func() var closer func()
trace := log.Trace().Enabled() trace := log.Trace().Enabled()
level := zerolog.WarnLevel
conn.Listen(func(msg any) { conn.Listen(func(msg any) {
if trace { if trace {
@@ -188,20 +189,18 @@ func tcpHandler(conn *rtsp.Conn) {
conn.PacketSize = uint16(core.Atoi(s)) conn.PacketSize = uint16(core.Atoi(s))
} }
// param name like ffmpeg style https://ffmpeg.org/ffmpeg-protocols.html
if s := query.Get("log_level"); s != "" {
if lvl, err := zerolog.ParseLevel(s); err == nil {
level = lvl
}
}
// will help to protect looping requests to same source // will help to protect looping requests to same source
conn.Connection.Source = query.Get("source") conn.Connection.Source = query.Get("source")
if err := stream.AddConsumer(conn); err != nil { if err := stream.AddConsumer(conn); err != nil {
logEvent := log.Warn() log.WithLevel(level).Err(err).Str("stream", name).Msg("[rtsp]")
if err, ok := err.(*streams.ErrorWithErrorCode); ok {
level, parseErr := zerolog.ParseLevel(query.Get(err.Code()))
if parseErr == nil {
logEvent = log.WithLevel(level)
}
}
logEvent.Err(err).Str("stream", name).Msg("[rtsp]")
return return
} }
@@ -239,7 +238,7 @@ func tcpHandler(conn *rtsp.Conn) {
if err := conn.Accept(); err != nil { if err := conn.Accept(); err != nil {
if err != io.EOF { if err != io.EOF {
log.Warn().Err(err).Caller().Send() log.WithLevel(level).Err(err).Caller().Send()
} }
if closer != nil { if closer != nil {
closer() closer()
+4 -24
View File
@@ -1,6 +1,7 @@
package streams package streams
import ( import (
"errors"
"strings" "strings"
"github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/core"
@@ -124,7 +125,7 @@ func formatError(consMedias, prodMedias []*core.Media, prodErrors []error) error
} }
if len(text) != 0 { if len(text) != 0 {
return &ErrorWithErrorCode{MultipleErrorCode, "streams: " + text} return errors.New("streams: " + text)
} }
// 2. Return "codecs not matched" // 2. Return "codecs not matched"
@@ -147,11 +148,11 @@ func formatError(consMedias, prodMedias []*core.Media, prodErrors []error) error
} }
} }
return &ErrorWithErrorCode{CodecNotMatchedErrorCode, "streams: codecs not matched: " + prod + " => " + cons} return errors.New("streams: codecs not matched: " + prod + " => " + cons)
} }
// 3. Return unknown error // 3. Return unknown error
return &ErrorWithErrorCode{UnknownErrorCode, "streams: unknown error"} return errors.New("streams: unknown error")
} }
func appendString(s, elem string) string { func appendString(s, elem string) string {
@@ -163,24 +164,3 @@ func appendString(s, elem string) string {
} }
return s + ", " + elem return s + ", " + elem
} }
type ErrorCode string
const (
CodecNotMatchedErrorCode ErrorCode = "codecNotMatched"
MultipleErrorCode ErrorCode = "multiple"
UnknownErrorCode ErrorCode = "unknown"
)
type ErrorWithErrorCode struct {
code ErrorCode
message string
}
func (e *ErrorWithErrorCode) Error() string {
return e.message
}
func (e *ErrorWithErrorCode) Code() string {
return string(e.code)
}