Corrected writing with many cameras.

This commit is contained in:
Сергей Петров 2025-03-14 18:03:48 +05:00
parent 4b39fc3bf0
commit 9ff99e2654
2 changed files with 37 additions and 46 deletions

View File

@ -3,17 +3,11 @@ package main
import ( import (
"flag" "flag"
"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))
}()
directory := flag.String("dir", "/home/psa/GoRepository", "directory") directory := flag.String("dir", "/home/psa/GoRepository", "directory")
flag.Parse() flag.Parse()
@ -35,6 +29,5 @@ func main() {
} }
}() }()
} }
select {} select {}
} }

View File

@ -8,8 +8,6 @@ import (
"github.com/bluenviron/gortsplib/v4/pkg/base" "github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/description" "github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format" "github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtph264"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtplpcm"
"github.com/pion/rtp" "github.com/pion/rtp"
_ "github.com/zaf/g711" _ "github.com/zaf/g711"
"log" "log"
@ -21,12 +19,6 @@ import (
"writer/pkg/converter" "writer/pkg/converter"
) )
var (
resolutions = []string{"1280x720"}
h264RTPDec *rtph264.Decoder
g711RTPDec *rtplpcm.Decoder
)
// StartWriter starts the program. // StartWriter starts the program.
func StartWriter(dir string, period int, URI string) error { func StartWriter(dir string, period int, URI string) error {
err := ProcRTSP(dir, period, URI) err := ProcRTSP(dir, period, URI)
@ -45,9 +37,11 @@ func StartWriter(dir string, 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(dir string, period int, URI string) error { func ProcRTSP(dir string, period int, link string) error {
resolutions := []string{"1280x720"}
// Return the last part of the URI after "/". // Return the last part of the URI after "/".
cuttedURI := config.CutURI(URI) cuttedURI := config.CutURI(link)
// Create FileName structure // Create FileName structure
fn := config.CreateFileName(dir, resolutions, cuttedURI, period) fn := config.CreateFileName(dir, resolutions, cuttedURI, period)
@ -71,7 +65,7 @@ func ProcRTSP(dir string, period int, URI string) error {
} }
// Parse URL. // Parse URL.
u, err := base.ParseURL(URI) u, err := base.ParseURL(link)
if err != nil { if err != nil {
return fmt.Errorf("parse URL error: %w", err) return fmt.Errorf("parse URL error: %w", err)
} }
@ -86,37 +80,42 @@ func ProcRTSP(dir string, period int, URI string) error {
// Find available medias. // Find available medias.
desc, _, err := c.Describe(u) desc, _, err := c.Describe(u)
if err != nil || desc == nil { if err != nil || desc == nil {
return fmt.Errorf("medias not found: %w", err) log.Printf("medias not found for camera [%s]: %v", link, err)
return nil
} }
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// Find the H264 media and format. // Find the H264 media and format.
//var run bool
h264Format, h264Media, err := media.CheckH264Format(desc) h264Format, h264Media, err := media.CheckH264Format(desc)
if err != nil {
log.Printf("H264 format not found: %v", err)
//run = true
}
// Find the G711 media and format. //// Find the G711 media and format.
g711Format, g711Media, err := media.CheckG711Format(desc) //g711Format, g711Media, err := media.CheckG711Format(desc)
//if err != nil {
// log.Printf("G711 format not found: %v", err)
// run = true
//}
// Initialising variable for AAC. // Initialising variable for AAC.
// var mpeg4AudioFormat *format.MPEG4Audio // var mpeg4AudioFormat *format.MPEG4Audio
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// Create RTP -> H264 decoder. // Create RTP -> H264 decoder.
if h264Format != nil { h264RTPDec, err := h264Format.CreateDecoder()
h264RTPDec, err = h264Format.CreateDecoder() if err != nil {
if err != nil { log.Printf("create H264 decoder error: %v", err)
return fmt.Errorf("create H264 decoder error: %w", err)
}
} }
// Create RTP -> H264 decoder. // Create RTP -> H264 decoder.
if g711Format != nil { //g711RTPDec, err := g711Format.CreateDecoder()
g711RTPDec, err = g711Format.CreateDecoder() //if err != nil {
if err != nil { // log.Printf("create G711 decoder error: %v", err)
return fmt.Errorf("create G711 decoder error: %w", err) //}
}
}
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
@ -127,7 +126,7 @@ func ProcRTSP(dir string, period int, URI string) error {
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// Setup MPEG-TS muxer. // Setup MPEG-TS muxer.
currentMpegtsMuxer := &converter.MpegtsMuxer{ currentMpegtsMuxer := converter.MpegtsMuxer{
FileName: fn.SetNumNTime(), FileName: fn.SetNumNTime(),
H264Format: h264Format, H264Format: h264Format,
// Mpeg4AudioFormat: mpeg4AudioFormat, // Mpeg4AudioFormat: mpeg4AudioFormat,
@ -151,7 +150,7 @@ func ProcRTSP(dir string, period int, URI string) error {
// Called when a H264/RTP or G711/RTP packet arrives. // Called when a H264/RTP or G711/RTP packet arrives.
c.OnPacketRTPAny(func(medi *description.Media, forma format.Format, pkt *rtp.Packet) { c.OnPacketRTPAny(func(medi *description.Media, forma format.Format, pkt *rtp.Packet) {
switch f := forma.(type) { switch forma.(type) {
case *format.H264: case *format.H264:
// 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)
@ -166,17 +165,17 @@ func ProcRTSP(dir string, period int, URI string) error {
} }
case *format.G711: case *format.G711:
// Process G711 flow and returns PTS and AU. //// Process G711 flow and returns PTS and AU.
_, au, err := media.ProcG711(&c, g711Media, g711RTPDec, pkt) //_, _, err := media.ProcG711(&c, g711Media, g711RTPDec, pkt)
if err != nil { //if err != nil {
log.Printf("%s: process G711 error: %s\n", cuttedURI, err) // log.Printf("%s: process G711 error: %s\n", cuttedURI, err)
} //}
// Convert G711 to AAC. //// Convert G711 to AAC.
_, err = converter.ConvertG711ToAAC(au, f.MULaw) // take aacAu //_, err = converter.ConvertG711ToAAC(au, f.MULaw) // take aacAu
if err != nil { //if err != nil {
log.Printf("%s: converting G711 to AAC frame: %s\n", cuttedURI, err) // log.Printf("%s: converting G711 to AAC frame: %s\n", cuttedURI, err)
} //}
/* /*
// Encode the access unit into MPEG-TS. // Encode the access unit into MPEG-TS.
@ -227,7 +226,6 @@ func ProcRTSP(dir string, period int, URI string) error {
//} //}
} }
} }
panic(c.Wait()) panic(c.Wait())
} }