From 82521c8142b573053e4e722e972a319bc1e57f6d Mon Sep 17 00:00:00 2001 From: Sergey Petrov Date: Fri, 14 Mar 2025 12:21:51 +0500 Subject: [PATCH] Refactoring. --- writer/cmd/main.go | 7 ++++++ writer/internal/procRTSP/client.go | 12 ++++++---- writer/pkg/converter/mpegts_muxer.go | 34 +++++++++++++++------------- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/writer/cmd/main.go b/writer/cmd/main.go index 59b228c..d4dda29 100644 --- a/writer/cmd/main.go +++ b/writer/cmd/main.go @@ -2,11 +2,18 @@ package main import ( "log" + "net/http" + _ "net/http/pprof" "writer/internal/config" "writer/internal/procRTSP" ) func main() { + // Profile. + go func() { + log.Println(http.ListenAndServe("localhost:6060", nil)) + }() + // Parse camera links from YAML file into struct Cameras. c, err := config.ParseCamerasYAML() if err != nil { diff --git a/writer/internal/procRTSP/client.go b/writer/internal/procRTSP/client.go index 319b388..e56ce17 100644 --- a/writer/internal/procRTSP/client.go +++ b/writer/internal/procRTSP/client.go @@ -46,6 +46,12 @@ func StartWriter(period int, URI string) error { // ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container. func ProcRTSP(period int, URI string) error { + defer func() { + if r := recover(); r != nil { + fmt.Println("Поймана паника:", r) + } + }() + // Return the last part of the URI after "/". cuttedURI := config.CutURI(URI) @@ -133,8 +139,6 @@ func ProcRTSP(period int, URI string) error { // Mpeg4AudioFormat: mpeg4AudioFormat, } - fmt.Println(currentMpegtsMuxer.FileName) - err = currentMpegtsMuxer.Initialize() if err != nil { panic(err) @@ -158,13 +162,13 @@ func ProcRTSP(period int, URI string) error { // Process H264 flow and return PTS and AU. pts, au, err := media.ProcH264(&c, h264Media, h264RTPDec, pkt) if err != nil { - log.Printf("%s: process H264 error: %s\n", cuttedURI, err) + //log.Printf("%s: process H264 error: %s\n", cuttedURI, err) } // Encode the access unit into MPEG-TS. err = currentMpegtsMuxer.WriteH264(au, pts) if err != nil { - log.Printf("%s: writing H264 packet: %s\n", cuttedURI, err) + //log.Printf("%s: writing H264 packet: %s\n", cuttedURI, err) } case *format.G711: diff --git a/writer/pkg/converter/mpegts_muxer.go b/writer/pkg/converter/mpegts_muxer.go index 679163a..9fba0dc 100644 --- a/writer/pkg/converter/mpegts_muxer.go +++ b/writer/pkg/converter/mpegts_muxer.go @@ -79,27 +79,29 @@ func (e *MpegtsMuxer) WriteH264(au [][]byte, pts int64) error { idrPresent := false for _, nalu := range au { - typ := h264.NALUType(nalu[0] & 0x1F) - switch typ { - case h264.NALUTypeSPS: - e.H264Format.SPS = nalu - continue + if len(nalu) != 0 { + typ := h264.NALUType(nalu[0] & 0x1F) + switch typ { + case h264.NALUTypeSPS: + e.H264Format.SPS = nalu + continue - case h264.NALUTypePPS: - e.H264Format.PPS = nalu - continue + case h264.NALUTypePPS: + e.H264Format.PPS = nalu + continue - case h264.NALUTypeAccessUnitDelimiter: - continue + case h264.NALUTypeAccessUnitDelimiter: + continue - case h264.NALUTypeIDR: - idrPresent = true + case h264.NALUTypeIDR: + idrPresent = true - case h264.NALUTypeNonIDR: - nonIDRPresent = true + case h264.NALUTypeNonIDR: + nonIDRPresent = true + } + + filteredAU = append(filteredAU, nalu) } - - filteredAU = append(filteredAU, nalu) } au = filteredAU