Program will restart if client gets TEARDOWN request; Domain changes if a camera was flipped to another domain.

This commit is contained in:
Сергей Петров 2025-03-11 17:13:11 +05:00
parent ff2b71b935
commit 66ba1f11ef
2 changed files with 58 additions and 4 deletions

View File

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

View File

@ -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 {
}
}()
panic(c.Wait())
// 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
}