Refactoring.
This commit is contained in:
parent
82521c8142
commit
4a08c4ee93
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
@ -14,8 +15,11 @@ func main() {
|
|||||||
log.Println(http.ListenAndServe("localhost:6060", nil))
|
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.
|
// Parse camera links from YAML file into struct Cameras.
|
||||||
c, err := config.ParseCamerasYAML()
|
c, err := config.ParseCamerasYAML(*directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -25,7 +29,7 @@ func main() {
|
|||||||
log.Printf("start recording on camera: %s\n", link)
|
log.Printf("start recording on camera: %s\n", link)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err = procRTSP.ProcRTSP(1, link)
|
err = procRTSP.ProcRTSP(*directory, 1, link)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ParseCamerasYAML parses camera links from YAML file into struct Cameras.
|
// 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
|
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 {
|
if err != nil {
|
||||||
return CamerasYAML, err
|
return CamerasYAML, err
|
||||||
}
|
}
|
||||||
@ -33,9 +33,9 @@ func CutURI(URI string) (CutterURI string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateFileName creates FileName structure.
|
// 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{
|
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"),
|
TimeNow: time.Now().Format("15-04-05_02-01-2006"),
|
||||||
Name: "videoFragment",
|
Name: "videoFragment",
|
||||||
Number: -1,
|
Number: -1,
|
||||||
|
@ -28,14 +28,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// StartWriter starts the program.
|
// StartWriter starts the program.
|
||||||
func StartWriter(period int, URI string) error {
|
func StartWriter(dir string, period int, URI string) error {
|
||||||
err := ProcRTSP(period, URI)
|
err := ProcRTSP(dir, period, URI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
// Temporary solution for inner cameras.
|
// Temporary solution for inner cameras.
|
||||||
//
|
//
|
||||||
// Change domain if a camera was flipped to another domain.
|
// Change domain if a camera was flipped to another domain.
|
||||||
err = changeDomain(period, URI, err)
|
err = changeDomain(dir, period, URI, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("change domain error: %w", err)
|
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.
|
// ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container.
|
||||||
func ProcRTSP(period int, URI string) error {
|
func ProcRTSP(dir string, 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)
|
||||||
|
|
||||||
// Create FileName structure
|
// 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.
|
// 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.
|
// 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)")
|
err2 := errors.New("404 (Not found)")
|
||||||
if errors.As(err, &err2) {
|
if errors.As(err, &err2) {
|
||||||
if strings.Contains(URI, "video-1") {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user