From 4a08c4ee93a32e8787fb2a020f47121332c5333e Mon Sep 17 00:00:00 2001 From: Sergey Petrov Date: Fri, 14 Mar 2025 12:41:46 +0500 Subject: [PATCH] Refactoring. --- writer/cmd/main.go | 8 ++++++-- writer/internal/config/config.go | 8 ++++---- writer/internal/procRTSP/client.go | 24 +++++++++--------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/writer/cmd/main.go b/writer/cmd/main.go index d4dda29..d6cf724 100644 --- a/writer/cmd/main.go +++ b/writer/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "log" "net/http" _ "net/http/pprof" @@ -14,8 +15,11 @@ func main() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() + directory := flag.String("dir", "/home/psa/GoRepository", "directory") + flag.Parse() + // Parse camera links from YAML file into struct Cameras. - c, err := config.ParseCamerasYAML() + c, err := config.ParseCamerasYAML(*directory) if err != nil { panic(err) } @@ -25,7 +29,7 @@ func main() { log.Printf("start recording on camera: %s\n", link) go func() { - err = procRTSP.ProcRTSP(1, link) + err = procRTSP.ProcRTSP(*directory, 1, link) if err != nil { panic(err) } diff --git a/writer/internal/config/config.go b/writer/internal/config/config.go index ec514e8..845fef2 100644 --- a/writer/internal/config/config.go +++ b/writer/internal/config/config.go @@ -10,10 +10,10 @@ import ( ) // ParseCamerasYAML parses camera links from YAML file into struct Cameras. -func ParseCamerasYAML() (map[string]string, error) { +func ParseCamerasYAML(dir string) (map[string]string, error) { var CamerasYAML map[string]string - data, err := os.ReadFile("/home/psa/GoRepository/rtsp_reader-writer/writer/internal/config/source.yaml") + data, err := os.ReadFile(dir + "/rtsp_reader-writer/writer/internal/config/source.yaml") if err != nil { return CamerasYAML, err } @@ -33,9 +33,9 @@ func CutURI(URI string) (CutterURI string) { } // CreateFileName creates FileName structure. -func CreateFileName(resolutions []string, URI string, period int) *protos.FileName { +func CreateFileName(dir string, resolutions []string, URI string, period int) *protos.FileName { fn := protos.FileName{ - Path: "/home/psa/GoRepository/data/" + URI + "/" + resolutions[0], + Path: dir + "/data/" + URI + "/" + resolutions[0], TimeNow: time.Now().Format("15-04-05_02-01-2006"), Name: "videoFragment", Number: -1, diff --git a/writer/internal/procRTSP/client.go b/writer/internal/procRTSP/client.go index e56ce17..f5a0480 100644 --- a/writer/internal/procRTSP/client.go +++ b/writer/internal/procRTSP/client.go @@ -28,14 +28,14 @@ var ( ) // StartWriter starts the program. -func StartWriter(period int, URI string) error { - err := ProcRTSP(period, URI) +func StartWriter(dir string, period int, URI string) error { + err := ProcRTSP(dir, period, URI) if err != nil { // Temporary solution for inner cameras. // // Change domain if a camera was flipped to another domain. - err = changeDomain(period, URI, err) + err = changeDomain(dir, period, URI, err) if err != nil { return fmt.Errorf("change domain error: %w", err) } @@ -45,18 +45,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) - } - }() - +func ProcRTSP(dir string, period int, URI string) error { // Return the last part of the URI after "/". cuttedURI := config.CutURI(URI) // Create FileName structure - fn := config.CreateFileName(resolutions, cuttedURI, period) + fn := config.CreateFileName(dir, resolutions, cuttedURI, period) //////////////////////////////////////////////////////////////////////////////////////// @@ -67,7 +61,7 @@ func ProcRTSP(period int, URI string) error { } // Create M3U8 playlist. - go gens.MediaPlaylistGenerator("/home/psa/GoRepository/data/"+cuttedURI, "", fn.Duration, resolutions) + go gens.MediaPlaylistGenerator(dir+"/data/"+cuttedURI, "", fn.Duration, resolutions) //////////////////////////////////////////////////////////////////////////////////////// @@ -238,16 +232,16 @@ func ProcRTSP(period int, URI string) error { } // changeDomain changes domain if a camera was flipped to another domain. -func changeDomain(period int, URI string, err error) error { +func changeDomain(directory string, period int, URI string, err error) error { err2 := errors.New("404 (Not found)") if errors.As(err, &err2) { if strings.Contains(URI, "video-1") { - err = ProcRTSP(period, strings.Replace(URI, "video-1", "video-2", 1)) + err = ProcRTSP(directory, period, strings.Replace(URI, "video-1", "video-2", 1)) if err != nil { return err } } else { - err = ProcRTSP(period, strings.Replace(URI, "video-2", "video-1", 1)) + err = ProcRTSP(directory, period, strings.Replace(URI, "video-2", "video-1", 1)) if err != nil { return err }