mirror of
https://github.com/cuteLittleDevil/m7s-jt1078.git
synced 2025-12-24 13:29:23 +08:00
fix: 修复保存的debug文件可能丢失部分 #6
This commit is contained in:
@@ -32,8 +32,11 @@ type connection struct {
|
||||
audioWriter *m7s.PublishAudioWriter[*format.Mpeg2Audio]
|
||||
videoWriter *m7s.PublishVideoWriter[*format.AnnexB]
|
||||
debug struct {
|
||||
file *os.File
|
||||
closeTime time.Time
|
||||
hasRecord bool
|
||||
file *os.File
|
||||
closeTime time.Time
|
||||
hasTemporaryStorage bool
|
||||
temporaryStorage []byte
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +64,7 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati
|
||||
ticker.Stop()
|
||||
clear(data)
|
||||
c.stop()
|
||||
if c.debug.file != nil {
|
||||
if c.debug.hasRecord {
|
||||
_ = c.debug.file.Close()
|
||||
}
|
||||
}()
|
||||
@@ -89,6 +92,9 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati
|
||||
}
|
||||
return err
|
||||
} else if n > 0 {
|
||||
if c.debug.hasTemporaryStorage {
|
||||
c.debug.temporaryStorage = append(c.debug.temporaryStorage, data[:n]...)
|
||||
}
|
||||
for pack, err := range packParse.parse(data[:n]) {
|
||||
if err == nil {
|
||||
once.Do(func() {
|
||||
@@ -111,14 +117,12 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati
|
||||
if handleErr != nil {
|
||||
return handleErr
|
||||
}
|
||||
if c.debug.file != nil {
|
||||
if _, err := c.debug.file.WriteString(fmt.Sprintf("%x", data[:n])); err != nil {
|
||||
c.Warn("write debug file",
|
||||
slog.String("err", err.Error()))
|
||||
}
|
||||
if time.Since(c.debug.closeTime) > 0 {
|
||||
_ = c.debug.file.Close()
|
||||
c.debug.file = nil
|
||||
if c.debug.hasRecord {
|
||||
if c.debug.hasTemporaryStorage {
|
||||
c.debug.hasTemporaryStorage = false
|
||||
c.saveDebugFile(c.debug.temporaryStorage)
|
||||
} else {
|
||||
c.saveDebugFile(data[:n])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,6 +130,18 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati
|
||||
}
|
||||
}
|
||||
|
||||
func (c *connection) saveDebugFile(data []byte) {
|
||||
if _, err := c.debug.file.WriteString(fmt.Sprintf("%x", data)); err != nil {
|
||||
c.Warn("write debug file",
|
||||
slog.String("err", err.Error()))
|
||||
}
|
||||
_ = c.debug.file.Sync()
|
||||
if time.Since(c.debug.closeTime) > 0 {
|
||||
_ = c.debug.file.Close()
|
||||
c.debug.hasRecord = false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *connection) stop() {
|
||||
c.stopOnce.Do(func() {
|
||||
close(c.stopChan)
|
||||
|
||||
@@ -55,6 +55,10 @@ func (s *Service) Run() {
|
||||
return
|
||||
}
|
||||
client := newConnection(conn, s.Logger, s.opts.timestampFunc)
|
||||
if debug := s.opts.Debug; debug.enable {
|
||||
client.debug.hasTemporaryStorage = true
|
||||
client.debug.temporaryStorage = make([]byte, 0, 10*1024)
|
||||
}
|
||||
httpBody := map[string]any{}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
client.onJoinEvent = func(c *connection, pack *jt1078.Packet) error {
|
||||
@@ -69,11 +73,11 @@ func (s *Service) Run() {
|
||||
"channel": pack.LogicChannel,
|
||||
"startTime": time.Now().Format(time.DateTime),
|
||||
}
|
||||
|
||||
if debug := s.opts.Debug; debug.enable {
|
||||
_ = os.MkdirAll(debug.dir, 0o755)
|
||||
name := filepath.Join(debug.dir, strings.ReplaceAll(c.publisher.StreamPath, string(os.PathSeparator), "-")+"-debug.txt")
|
||||
if file, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC|os.O_APPEND, 0o666); err == nil {
|
||||
name := filepath.Join(debug.dir, strings.ReplaceAll(c.publisher.StreamPath, string(os.PathSeparator), "-"))
|
||||
if file, fileErr := os.OpenFile(name+"-debug.txt", os.O_CREATE|os.O_RDWR|os.O_TRUNC|os.O_APPEND, 0o666); fileErr == nil {
|
||||
client.debug.hasRecord = true
|
||||
client.debug.file = file
|
||||
client.debug.closeTime = time.Now().Add(debug.time)
|
||||
httpBody["debugFile"] = name
|
||||
@@ -84,7 +88,6 @@ func (s *Service) Run() {
|
||||
slog.String("err", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
onNoticeEvent(s.opts.onJoinURL, httpBody)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user