Refactoring.

This commit is contained in:
Сергей Петров 2025-03-14 12:41:46 +05:00
parent 82521c8142
commit 4a08c4ee93
3 changed files with 19 additions and 21 deletions

View File

@ -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)
}

View File

@ -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,

View File

@ -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
}