Refactoring.

This commit is contained in:
Сергей Петров 2025-03-14 12:21:51 +05:00
parent f640611620
commit 82521c8142
3 changed files with 33 additions and 20 deletions

View File

@ -2,11 +2,18 @@ package main
import ( import (
"log" "log"
"net/http"
_ "net/http/pprof"
"writer/internal/config" "writer/internal/config"
"writer/internal/procRTSP" "writer/internal/procRTSP"
) )
func main() { func main() {
// Profile.
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// Parse camera links from YAML file into struct Cameras. // Parse camera links from YAML file into struct Cameras.
c, err := config.ParseCamerasYAML() c, err := config.ParseCamerasYAML()
if err != nil { if err != nil {

View File

@ -46,6 +46,12 @@ func StartWriter(period int, URI string) error {
// ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container. // ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container.
func ProcRTSP(period int, URI string) error { 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 "/". // Return the last part of the URI after "/".
cuttedURI := config.CutURI(URI) cuttedURI := config.CutURI(URI)
@ -133,8 +139,6 @@ func ProcRTSP(period int, URI string) error {
// Mpeg4AudioFormat: mpeg4AudioFormat, // Mpeg4AudioFormat: mpeg4AudioFormat,
} }
fmt.Println(currentMpegtsMuxer.FileName)
err = currentMpegtsMuxer.Initialize() err = currentMpegtsMuxer.Initialize()
if err != nil { if err != nil {
panic(err) panic(err)
@ -158,13 +162,13 @@ func ProcRTSP(period int, URI string) error {
// Process H264 flow and return PTS and AU. // Process H264 flow and return PTS and AU.
pts, au, err := media.ProcH264(&c, h264Media, h264RTPDec, pkt) pts, au, err := media.ProcH264(&c, h264Media, h264RTPDec, pkt)
if err != nil { 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. // Encode the access unit into MPEG-TS.
err = currentMpegtsMuxer.WriteH264(au, pts) err = currentMpegtsMuxer.WriteH264(au, pts)
if err != nil { 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: case *format.G711:

View File

@ -79,27 +79,29 @@ func (e *MpegtsMuxer) WriteH264(au [][]byte, pts int64) error {
idrPresent := false idrPresent := false
for _, nalu := range au { for _, nalu := range au {
typ := h264.NALUType(nalu[0] & 0x1F) if len(nalu) != 0 {
switch typ { typ := h264.NALUType(nalu[0] & 0x1F)
case h264.NALUTypeSPS: switch typ {
e.H264Format.SPS = nalu case h264.NALUTypeSPS:
continue e.H264Format.SPS = nalu
continue
case h264.NALUTypePPS: case h264.NALUTypePPS:
e.H264Format.PPS = nalu e.H264Format.PPS = nalu
continue continue
case h264.NALUTypeAccessUnitDelimiter: case h264.NALUTypeAccessUnitDelimiter:
continue continue
case h264.NALUTypeIDR: case h264.NALUTypeIDR:
idrPresent = true idrPresent = true
case h264.NALUTypeNonIDR: case h264.NALUTypeNonIDR:
nonIDRPresent = true nonIDRPresent = true
}
filteredAU = append(filteredAU, nalu)
} }
filteredAU = append(filteredAU, nalu)
} }
au = filteredAU au = filteredAU