Refactoring.

This commit is contained in:
Сергей Петров 2025-03-18 10:20:12 +05:00
parent 6eea6cec1f
commit 230f2ffc7b
13 changed files with 74 additions and 44 deletions

View File

@ -2,13 +2,12 @@ package main
import ( import (
"flag" "flag"
"git.insit.tech/psa/rtsp_reader-writer/writer/internal/config"
"git.insit.tech/psa/rtsp_reader-writer/writer/internal/ingest/rtsp"
"log" "log"
"writer/internal/config"
"writer/internal/procRTSP"
) )
func main() { func main() {
directory := flag.String("dir", "/home/psa/GoRepository", "directory") directory := flag.String("dir", "/home/psa/GoRepository", "directory")
flag.Parse() flag.Parse()
@ -23,9 +22,9 @@ 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(*directory, 1, link) err = rtsp.RTSP(*directory, 60, link)
if err != nil { if err != nil {
panic(err) log.Printf("procRTSP function error for camera %s: %s", link, err.Error())
} }
}() }()
} }

Binary file not shown.

View File

@ -1,4 +1,4 @@
module writer module git.insit.tech/psa/rtsp_reader-writer
go 1.23.6 go 1.23.6

View File

@ -0,0 +1 @@
package auth

View File

@ -0,0 +1,23 @@
package config
import (
"gopkg.in/yaml.v3"
"os"
)
// ParseCamerasYAML parses camera links from YAML file into struct Cameras.
func ParseCamerasYAML(dir string) (map[string]string, error) {
var CamerasYAML map[string]string
data, err := os.ReadFile(dir + "/rtsp_reader-writer/writer/internal/config/source.yaml")
if err != nil {
return CamerasYAML, err
}
err = yaml.Unmarshal(data, &CamerasYAML)
if err != nil {
return CamerasYAML, err
}
return CamerasYAML, nil
}

View File

@ -32,5 +32,5 @@
#camera_32: rtsp://intercom-video-1.insit.ru/dp-woxvkbynctgfbuztsalttgburbpvjf #camera_32: rtsp://intercom-video-1.insit.ru/dp-woxvkbynctgfbuztsalttgburbpvjf
#camera_33: rtsp://intercom-video-1.insit.ru/dp-fdzbasqehtptsuhxnjeqqnlrixfahcgvlcr #camera_33: rtsp://intercom-video-1.insit.ru/dp-fdzbasqehtptsuhxnjeqqnlrixfahcgvlcr
#camera_34: rtsp://intercom-video-1.insit.ru/dp-exyeqscyamrbkwkjifagouyprtsdoe #camera_34: rtsp://intercom-video-1.insit.ru/dp-exyeqscyamrbkwkjifagouyprtsdoe
camera_35: rtsp://intercom-video-2.insit.ru/dp-sutyagina3a-iv-uujtwbsjekv #camera_35: rtsp://intercom-video-2.insit.ru/dp-sutyagina3a-iv-uujtwbsjekv
camera_36: rtsp://intercom-video-1.insit.ru/dp-wyshispseamhqmnhkqwkbarshnrvni camera_36: rtsp://intercom-video-1.insit.ru/dp-wyshispseamhqmnhkqwkbarshnrvni

View File

@ -1,4 +1,4 @@
package media package rtsp
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package media package rtsp
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package converter package rtsp
import ( import (
"bytes" "bytes"

View File

@ -1,8 +1,10 @@
package procRTSP package rtsp
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.insit.tech/psa/rtsp_reader-writer/writer/internal/storage"
"git.insit.tech/psa/rtsp_reader-writer/writer/pkg/converter"
"git.insit.tech/sas/rtsp_proxy/protos/gens" "git.insit.tech/sas/rtsp_proxy/protos/gens"
"github.com/bluenviron/gortsplib/v4" "github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base" "github.com/bluenviron/gortsplib/v4/pkg/base"
@ -14,14 +16,11 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
"writer/internal/config"
"writer/internal/media"
"writer/pkg/converter"
) )
// StartWriter starts the program. // StartWriter starts the program.
func StartWriter(dir string, period int, URI string) error { func StartWriter(dir string, period int, URI string) error {
err := ProcRTSP(dir, period, URI) err := RTSP(dir, period, URI)
if err != nil { if err != nil {
// Temporary solution for inner cameras. // Temporary solution for inner cameras.
@ -36,19 +35,14 @@ func StartWriter(dir string, period int, URI string) error {
return nil return nil
} }
// ProcRTSP process RTSP protocol and writes H264 and PCM flows into TS container. // RTSP processes RTSP protocol.
func ProcRTSP(dir string, period int, link string) error { func RTSP(dir string, period int, link string) error {
resolutions := []string{"1280x720"} resolutions := []string{"1280x720"}
// Return the last part of the URI after "/". // Create file name structure and directory for files.
cuttedURI := config.CutURI(link) cuttedURI := storage.CutURI(link)
fn := storage.CreateFileName(dir, resolutions, cuttedURI, period)
// Create FileName structure
fn := config.CreateFileName(dir, resolutions, cuttedURI, period)
////////////////////////////////////////////////////////////////////////////////////////
// Create directory for files according to resolutions.
err := os.MkdirAll(fmt.Sprintf("%s", fn.Path), 0755) err := os.MkdirAll(fmt.Sprintf("%s", fn.Path), 0755)
if err != nil { if err != nil {
return fmt.Errorf("mkdirall error: %w", err) return fmt.Errorf("mkdirall error: %w", err)
@ -57,48 +51,42 @@ func ProcRTSP(dir string, period int, link string) error {
// Create M3U8 playlist. // Create M3U8 playlist.
go gens.MediaPlaylistGenerator(dir+"/data/"+cuttedURI, "", fn.Duration, resolutions) go gens.MediaPlaylistGenerator(dir+"/data/"+cuttedURI, "", fn.Duration, resolutions)
//////////////////////////////////////////////////////////////////////////////////////// // Connect to the server.
// Initialise client.
c := gortsplib.Client{ c := gortsplib.Client{
UserAgent: "PSA", UserAgent: "PSA",
} }
// Parse URL.
u, err := base.ParseURL(link) u, err := base.ParseURL(link)
if err != nil { if err != nil {
return fmt.Errorf("parse URL error: %w", err) return fmt.Errorf("parse URL error: %w", err)
} }
// Connect to the server.
err = c.Start(u.Scheme, u.Host) err = c.Start(u.Scheme, u.Host)
if err != nil { if err != nil {
return fmt.Errorf("connect to the server error: %w", err) return fmt.Errorf("connect to the server error: %w", err)
} }
defer c.Close() defer c.Close()
// Find available medias. desc, r, err := c.Describe(u)
desc, _, err := c.Describe(u)
if err != nil || desc == nil { if err != nil || desc == nil {
log.Printf("medias not found for camera [%s]: %v", link, err) log.Printf("medias not found for camera [%s]: %v", link, err)
return nil return nil
} }
//////////////////////////////////////////////////////////////////////////////////////// ///////////////
// Find the H264 media and format. // Find formats.
//var run bool //var run bool
h264Format, h264Media, err := media.CheckH264Format(desc) h264Format, h264Media, err := CheckH264Format(desc)
if err != nil { if err != nil {
log.Printf("H264 format not found: %v", err) log.Printf("H264 format not found: %v", err)
//run = true //run = true
} }
//// Find the G711 media and format. //g711Format, g711Media, err := CheckG711Format(desc)
//g711Format, g711Media, err := media.CheckG711Format(desc)
//if err != nil { //if err != nil {
// log.Printf("G711 format not found: %v", err) // log.Printf("G711 format not found: %v", err)
// run = true // // run = true
//} //}
// Initialising variable for AAC. // Initialising variable for AAC.
@ -120,7 +108,7 @@ func ProcRTSP(dir string, period int, link string) error {
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
// Wait for the next period. // Wait for the next period.
config.Waiter(period) storage.WaitPeriod(period)
log.Println("Start recording") log.Println("Start recording")
//////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////
@ -153,7 +141,7 @@ func ProcRTSP(dir string, period int, link string) error {
switch forma.(type) { switch forma.(type) {
case *format.H264: case *format.H264:
// Process H264 flow and return PTS and AU. // Process H264 flow and return PTS and AU.
pts, au, err := media.ProcH264(&c, h264Media, h264RTPDec, pkt) pts, au, err := ProcH264(&c, h264Media, h264RTPDec, pkt)
if err != nil { if err != nil {
//log.Printf("%s: process H264 error: %s\n", cuttedURI, err) //log.Printf("%s: process H264 error: %s\n", cuttedURI, err)
} }
@ -197,7 +185,7 @@ func ProcRTSP(dir string, period int, link string) error {
} }
// Create ticker for rotation files. // Create ticker for rotation files.
ticker := time.NewTicker(time.Duration(period) * time.Minute) ticker := time.NewTicker(time.Duration(period) * time.Second)
defer ticker.Stop() defer ticker.Stop()
// Rotate files. // Rotate files.
@ -234,12 +222,12 @@ 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(directory, period, strings.Replace(URI, "video-1", "video-2", 1)) err = RTSP(directory, period, strings.Replace(URI, "video-1", "video-2", 1))
if err != nil { if err != nil {
return err return err
} }
} else { } else {
err = ProcRTSP(directory, period, strings.Replace(URI, "video-2", "video-1", 1)) err = RTSP(directory, period, strings.Replace(URI, "video-2", "video-1", 1))
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,19 @@
package storage
import "time"
type Segment struct {
Start time.Time
Duration time.Duration
H264 byte
G711 byte
SPS []byte
PPS []byte
}
type StreamRecord struct {
ID string
Segments []Segment
}

View File

@ -0,0 +1 @@
package storage

View File

@ -1 +0,0 @@
package parser