diff --git a/writer/cmd/main.go b/writer/cmd/main.go index 616b8ce..9670935 100644 --- a/writer/cmd/main.go +++ b/writer/cmd/main.go @@ -1,9 +1,11 @@ package main -import "writer/internal/procRTSP" +import ( + "writer/internal/procRTSP" +) func main() { - err := procRTSP.ProcRTSP(1, "rtsp://intercom-video-2.insit.ru/dp-ohusuxzcvzsnpzzvkpyhddnwxuyeyc") + err := procRTSP.StartWriter(1, "rtsp://intercom-video-2.insit.ru/dp-ohusuxzcvzsnpzzvkpyhddnwxuyeyc") if err != nil { panic(err) } diff --git a/writer/internal/procRTSP/client.go b/writer/internal/procRTSP/client.go index bbe344d..05e3c8c 100644 --- a/writer/internal/procRTSP/client.go +++ b/writer/internal/procRTSP/client.go @@ -1,6 +1,7 @@ package procRTSP import ( + "errors" "fmt" "git.insit.tech/sas/rtsp_proxy/protos/gens" "github.com/bluenviron/gortsplib/v4" @@ -12,6 +13,7 @@ import ( "github.com/pion/rtp" _ "github.com/zaf/g711" "log" + "strings" "time" "writer/internal/config" "writer/internal/media" @@ -24,8 +26,21 @@ var ( g711RTPDec *rtplpcm.Decoder ) -// ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container. -func ProcRTSP(period int, URI string) error { +// StartWriter starts the program. +func StartWriter(period int, URI string) error { + err := procRTSP(period, URI) + if err != nil { + // Change domain if a camera was flipped to another domain. + err = changeDomain(period, URI, err) + if err != nil { + return fmt.Errorf("change domain error: %w", err) + } + } + return nil +} + +// procRTSP process RTSP protocol and writes H264 and PCM flows into TS container. +func procRTSP(period int, URI string) error { // Create FileName structure fn := config.CreateFileName(resolutions, period) @@ -186,5 +201,42 @@ func ProcRTSP(period int, URI string) error { } }() + // Restart program if client gets TEARDOWN request. + c.OnRequest = func(req *base.Request) { + if req.Method == base.Teardown { + log.Printf("got TEARDOWN request from server: %v", req) + } + + go func() { + err = procRTSP(period, URI) + if err != nil { + log.Fatalf("restart RTSP error: %s\n", err) + } + }() + } + panic(c.Wait()) + +} + +// changeDomain changes domain if a camera was flipped to another domain. +func changeDomain(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)) + if err != nil { + return err + } + } else { + err = procRTSP(period, strings.Replace(URI, "video-2", "video-1", 1)) + if err != nil { + return err + } + } + } else { + panic(err) + } + + return nil }