debug: add some log

This commit is contained in:
langhuihui
2023-10-30 10:11:48 +08:00
parent 8ffe3d3d55
commit 4a9dd2c921
4 changed files with 44 additions and 38 deletions

46
io.go
View File

@@ -38,8 +38,8 @@ type IO struct {
ID string ID string
Type string Type string
RemoteAddr string RemoteAddr string
context.Context `json:"-" yaml:"-"` //不要直接设置,应当通过OnEvent传入父级Context context.Context `json:"-" yaml:"-"` //不要直接设置,应当通过SetParentCtx传入父级Context
context.CancelFunc `json:"-" yaml:"-"` //流关闭是关闭发布者或者订阅者 context.CancelCauseFunc `json:"-" yaml:"-"` //流关闭是关闭发布者或者订阅者
*log.Logger `json:"-" yaml:"-"` *log.Logger `json:"-" yaml:"-"`
StartTime time.Time //创建时间 StartTime time.Time //创建时间
Stream *Stream `json:"-" yaml:"-"` Stream *Stream `json:"-" yaml:"-"`
@@ -69,7 +69,7 @@ func (i *IO) SetIO(conn any) {
// SetParentCtx可选 // SetParentCtx可选
func (i *IO) SetParentCtx(parent context.Context) { func (i *IO) SetParentCtx(parent context.Context) {
i.Context, i.CancelFunc = context.WithCancel(parent) i.Context, i.CancelCauseFunc = context.WithCancelCause(parent)
} }
func (i *IO) SetLogger(logger *log.Logger) { func (i *IO) SetLogger(logger *log.Logger) {
@@ -78,8 +78,10 @@ func (i *IO) SetLogger(logger *log.Logger) {
func (i *IO) OnEvent(event any) { func (i *IO) OnEvent(event any) {
switch event.(type) { switch event.(type) {
case SEclose, SEKick: case SEclose:
i.close() i.close(StopError{zap.String("event", "close")})
case SEKick:
i.close(StopError{zap.String("event", "kick")})
} }
} }
@@ -102,26 +104,30 @@ type IIO interface {
log.Zap log.Zap
} }
func (i *IO) close() bool { func (i *IO) close(err StopError) bool {
if i.IsClosed() { if i.IsClosed() {
i.Warn("already closed", err...)
return false return false
} }
i.Info("close", err...)
if i.Closer != nil { if i.Closer != nil {
i.Closer.Close() i.Closer.Close()
} }
if i.CancelFunc != nil { if i.CancelCauseFunc != nil {
i.CancelFunc() i.CancelCauseFunc(err)
} }
return true return true
} }
// Stop 停止订阅或者发布,由订阅者或者发布者调用 // Stop 停止订阅或者发布,由订阅者或者发布者调用
func (io *IO) Stop(reason ...zapcore.Field) { func (io *IO) Stop(reason ...zapcore.Field) {
if io.close() { io.close(StopError(reason))
io.Info("stop", reason...) }
} else {
io.Warn("already stopped", reason...) type StopError []zapcore.Field
}
func (s StopError) Error() string {
return "stop"
} }
var ( var (
@@ -171,9 +177,11 @@ func (io *IO) receive(streamPath string, specific IIO) error {
if s == nil { if s == nil {
return ErrBadStreamName return ErrBadStreamName
} }
if Engine.Err() != nil {
return Engine.Err()
}
io.SetParentCtx(util.Conditoinal(io.Context == nil || io.Err() != nil, Engine.Context, io.Context))
if io.Stream == nil { //初次 if io.Stream == nil { //初次
io.Context, io.CancelFunc = context.WithCancel(util.Conditoinal[context.Context](io.Context == nil, Engine, io.Context))
if io.Type == "" { if io.Type == "" {
io.Type = reflect.TypeOf(specific).Elem().Name() io.Type = reflect.TypeOf(specific).Elem().Name()
} }
@@ -182,9 +190,9 @@ func (io *IO) receive(streamPath string, specific IIO) error {
logFeilds = append(logFeilds, zap.String("ID", io.ID)) logFeilds = append(logFeilds, zap.String("ID", io.ID))
} }
if io.Logger == nil { if io.Logger == nil {
logFeilds = append(logFeilds, zap.String("stream", s.Path))
io.Logger = s.With(logFeilds...) io.Logger = s.With(logFeilds...)
} else { } else {
logFeilds = append(logFeilds, zap.String("streamPath", s.Path))
io.Logger = io.Logger.With(logFeilds...) io.Logger = io.Logger.With(logFeilds...)
} }
} }
@@ -192,7 +200,8 @@ func (io *IO) receive(streamPath string, specific IIO) error {
io.Spesific = specific io.Spesific = specific
io.StartTime = time.Now() io.StartTime = time.Now()
if v, ok := specific.(IPublisher); ok { if v, ok := specific.(IPublisher); ok {
conf := v.GetPublisher().Config puber := v.GetPublisher()
conf := puber.Config
io.Info("publish") io.Info("publish")
s.pubLocker.Lock() s.pubLocker.Lock()
defer s.pubLocker.Unlock() defer s.pubLocker.Unlock()
@@ -208,6 +217,9 @@ func (io *IO) receive(streamPath string, specific IIO) error {
s.Warn("duplicate publish", zot) s.Warn("duplicate publish", zot)
return ErrDuplicatePublish return ErrDuplicatePublish
} }
} else {
puber.AudioTrack = nil
puber.VideoTrack = nil
} }
s.PublishTimeout = conf.PublishTimeout s.PublishTimeout = conf.PublishTimeout
s.DelayCloseTimeout = conf.DelayCloseTimeout s.DelayCloseTimeout = conf.DelayCloseTimeout

View File

@@ -325,7 +325,7 @@ func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save int)
pullConf := conf.GetPullConfig() pullConf := conf.GetPullConfig()
if save < 2 { if save < 2 {
zurl := zap.String("url", url) zurl := zap.String("url", url)
zpath := zap.String("path", streamPath) zpath := zap.String("stream", streamPath)
opt.Info("pull", zpath, zurl) opt.Info("pull", zpath, zurl)
puller.init(streamPath, url, pullConf) puller.init(streamPath, url, pullConf)
opt.AssignPubConfig(puller.GetPublisher()) opt.AssignPubConfig(puller.GetPublisher())

View File

@@ -1,7 +1,6 @@
package engine package engine
import ( import (
"context"
"io" "io"
"strings" "strings"
"time" "time"
@@ -86,13 +85,7 @@ func (pub *Puller) startPull(puller IPuller) {
puller.Error("pull publish", zap.Error(err)) puller.Error("pull publish", zap.Error(err))
return return
} }
s := puber.Stream stream = puber.Stream
if stream != s && stream != nil { // 这段代码说明老流已经中断创建了新流需要把track置空从而避免复用
puber.AudioTrack = nil
puber.VideoTrack = nil
puber.Context, puber.CancelFunc = context.WithCancel(Engine) // 老流的上下文已经取消,需要重新创建
}
stream = s
badPuller = false badPuller = false
if err = puller.Pull(); err != nil && !puller.IsShutdown() { if err = puller.Pull(); err != nil && !puller.IsShutdown() {
puller.Error("pull interrupt", zap.Error(err)) puller.Error("pull interrupt", zap.Error(err))

View File

@@ -1,6 +1,7 @@
package engine package engine
import ( import (
"context"
"encoding/json" "encoding/json"
"strings" "strings"
"sync" "sync"
@@ -479,7 +480,7 @@ func (s *Stream) run() {
s.Warn("no tracks") s.Warn("no tracks")
lost = true lost = true
} else if s.Publisher != nil && s.Publisher.IsClosed() { } else if s.Publisher != nil && s.Publisher.IsClosed() {
s.Warn("publish is closed") s.Warn("publish is closed", zap.Error(context.Cause(s.Publisher.GetPublisher())), zap.Error(Engine.Err()))
lost = true lost = true
} }
} }