From f8fe900207cd315c700b7035b221408a76fc73dc Mon Sep 17 00:00:00 2001 From: Saxon Nelson-Milton Date: Sun, 5 Mar 2023 08:25:14 +1030 Subject: [PATCH 1/2] revid: accept multiple RTMP outputs This change modifies the revid config RTMPURL field to be a slice instead of a single string. This allows use to have multiple RTMP outputs. --- revid/config/config.go | 20 ++++++++++---------- revid/config/config_test.go | 2 +- revid/config/variables.go | 12 +++++++++--- revid/pipeline.go | 8 +++++++- revid/pipeline_test.go | 2 +- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/revid/config/config.go b/revid/config/config.go index bc73e21b..b104032d 100644 --- a/revid/config/config.go +++ b/revid/config/config.go @@ -226,16 +226,16 @@ type Config struct { // localhost:6970. MPEGT-TS packetization is used. Outputs []uint8 - PSITime uint // Sets the time between a packet being sent. - Quantization uint // Quantization defines the quantization level, which will determine variable bitrate quality in the case of input from the Pi Camera. - PoolCapacity uint // The number of bytes the pool buffer will occupy. - PoolStartElementSize uint // The starting element size of the pool buffer from which element size will increase to accomodate frames. - PoolWriteTimeout uint // The pool buffer write timeout in seconds. - RecPeriod float64 // How many seconds to record at a time. - Rotation uint // Rotation defines the video rotation angle in degrees Raspivid input. - RTMPURL string // RTMPURL specifies the Rtmp output destination URL. This must be defined if RTMP is to be used as an output. - RTPAddress string // RTPAddress defines the RTP output destination. - SampleRate uint // Samples a second (Hz). + PSITime uint // Sets the time between a packet being sent. + Quantization uint // Quantization defines the quantization level, which will determine variable bitrate quality in the case of input from the Pi Camera. + PoolCapacity uint // The number of bytes the pool buffer will occupy. + PoolStartElementSize uint // The starting element size of the pool buffer from which element size will increase to accomodate frames. + PoolWriteTimeout uint // The pool buffer write timeout in seconds. + RecPeriod float64 // How many seconds to record at a time. + Rotation uint // Rotation defines the video rotation angle in degrees Raspivid input. + RTMPURL []string // RTMPURL specifies the Rtmp output destination URL. This must be defined if RTMP is to be used as an output. + RTPAddress string // RTPAddress defines the RTP output destination. + SampleRate uint // Samples a second (Hz). Saturation int // Sharpness is the sharpness of capture image/video from a capture device. diff --git a/revid/config/config_test.go b/revid/config/config_test.go index e30bba7a..1ea66a77 100644 --- a/revid/config/config_test.go +++ b/revid/config/config_test.go @@ -166,7 +166,7 @@ func TestUpdate(t *testing.T) { PoolCapacity: 100000, PoolWriteTimeout: 50, Rotation: 180, - RTMPURL: "rtmp://url", + RTMPURL: []string{"rtmp://url"}, RTPAddress: "ip:port", Saturation: -10, VBRBitrate: 300000, diff --git a/revid/config/variables.go b/revid/config/variables.go index c0922c0a..84d15631 100644 --- a/revid/config/variables.go +++ b/revid/config/variables.go @@ -582,9 +582,15 @@ var Variables = []struct { Update: func(c *Config, v string) { c.Rotation = parseUint(KeyRotation, v, c) }, }, { - Name: KeyRTMPURL, - Type: typeString, - Update: func(c *Config, v string) { c.RTMPURL = v }, + Name: KeyRTMPURL, + Type: typeString, + Update: func(c *Config, v string) { + v = strings.ReplaceAll(v, " ", "") + split := strings.Split(v, ",") + for _, s := range split { + c.RTMPURL = append(c.RTMPURL, s) + } + }, }, { Name: KeyRTPAddress, diff --git a/revid/pipeline.go b/revid/pipeline.go index 2edd116f..f959674e 100644 --- a/revid/pipeline.go +++ b/revid/pipeline.go @@ -178,6 +178,7 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io. // to mtsSenders if the output requires MPEGTS encoding, or flvSenders if the // output requires FLV encoding. var w io.WriteCloser + rtmpUrlIdx := 0 for _, out := range r.cfg.Outputs { switch out { case config.OutputHTTP: @@ -215,11 +216,16 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io. mtsSenders = append(mtsSenders, w) case config.OutputRTMP: r.cfg.Logger.Debug("using RTMP output") + if rtmpUrlIdx > len(r.cfg.RTMPURL)-1 { + r.cfg.Logger.Warning("rtmp outputs exceed available rtmp urls") + break + } pb := pool.NewBuffer(int(r.cfg.PoolStartElementSize), int(nElements), writeTimeout) - w, err := newRtmpSender(r.cfg.RTMPURL, rtmpConnectionMaxTries, pb, r.cfg.Logger, r.bitrate.Report) + w, err := newRtmpSender(r.cfg.RTMPURL[rtmpUrlIdx], rtmpConnectionMaxTries, pb, r.cfg.Logger, r.bitrate.Report) if err != nil { r.cfg.Logger.Warning("rtmp connect error", "error", err.Error()) } + rtmpUrlIdx++ flvSenders = append(flvSenders, w) } } diff --git a/revid/pipeline_test.go b/revid/pipeline_test.go index a4162603..9600ef4e 100644 --- a/revid/pipeline_test.go +++ b/revid/pipeline_test.go @@ -203,7 +203,7 @@ func TestResetEncoderSenderSetup(t *testing.T) { for testNum, test := range tests { // Create a new config and reset revid with it. const dummyURL = "rtmp://dummy" - c := config.Config{Logger: &testLogger{}, Outputs: test.outputs, RTMPURL: dummyURL} + c := config.Config{Logger: &testLogger{}, Outputs: test.outputs, RTMPURL: []string{dummyURL}} err := rv.setConfig(c) if err != nil { t.Fatalf("unexpected error: %v for test %v", err, testNum) From 0059ec1017c4accde9ac8fc93a26b2b8f7513d05 Mon Sep 17 00:00:00 2001 From: Saxon Nelson-Milton Date: Sun, 5 Mar 2023 08:39:39 +1030 Subject: [PATCH 2/2] vidforward: modify viforward to accept multiple RTMP URLs This change modifies vidforward to accept multiple RTMP URLs in its control request handler. This allows for the creation of a revid pipeline for a mac with multiple RTMP outputs. --- cmd/vidforward/file.go | 8 ++++---- cmd/vidforward/file_test.go | 20 ++++++++++---------- cmd/vidforward/main.go | 11 ++++++++--- cmd/vidforward/utils.go | 4 ++-- go.mod | 1 - go.sum | 11 ----------- 6 files changed, 24 insertions(+), 31 deletions(-) diff --git a/cmd/vidforward/file.go b/cmd/vidforward/file.go index 9470bb21..2081da77 100644 --- a/cmd/vidforward/file.go +++ b/cmd/vidforward/file.go @@ -44,7 +44,7 @@ const fileName = "state.json" // marshal/unmarshal overriding. type BroadcastBasic struct { MAC - URL string + URLs []string Status string } @@ -60,7 +60,7 @@ type ManagerBasic struct { func (b Broadcast) MarshalJSON() ([]byte, error) { return json.Marshal(BroadcastBasic{ MAC: b.mac, - URL: b.url, + URLs: b.urls, Status: b.status, }) } @@ -75,10 +75,10 @@ func (b *Broadcast) UnmarshalJSON(data []byte) error { } b.mac = bm.MAC - b.url = bm.URL + b.urls = bm.URLs b.status = bm.Status - b.rv, err = newRevid(global.GetLogger(), b.url) + b.rv, err = newRevid(global.GetLogger(), b.urls) if err != nil { return fmt.Errorf("could not populate RV field: %w", err) } diff --git a/cmd/vidforward/file_test.go b/cmd/vidforward/file_test.go index 891f241b..ee283cb5 100644 --- a/cmd/vidforward/file_test.go +++ b/cmd/vidforward/file_test.go @@ -58,11 +58,11 @@ func TestBroadcastMarshal(t *testing.T) { { in: Broadcast{ mac: testMAC, - url: testURL, + urls: []string{testURL}, status: statusActive, rv: newRevidForTest((*logging.TestLogger)(t), testURL, t), }, - expect: []byte("{\"MAC\":\"" + testMAC + "\",\"URL\":\"" + testURL + "\",\"Status\":\"" + statusActive + "\"}"), + expect: []byte("{\"MAC\":\"" + testMAC + "\",\"URLs\":[\"" + testURL + "\"],\"Status\":\"" + statusActive + "\"}"), }, } @@ -91,11 +91,11 @@ func TestBroadcastUnmarshal(t *testing.T) { { expect: Broadcast{ mac: testMAC, - url: testURL, + urls: []string{testURL}, status: statusActive, rv: newRevidForTest(logger, testURL, t), }, - in: []byte("{\"MAC\":\"" + testMAC + "\",\"URL\":\"" + testURL + "\",\"Status\":\"" + statusActive + "\"}"), + in: []byte("{\"MAC\":\"" + testMAC + "\",\"URLs\":[\"" + testURL + "\"],\"Status\":\"" + statusActive + "\"}"), }, } @@ -127,7 +127,7 @@ func TestBroadcastManagerMarshal(t *testing.T) { broadcasts: map[MAC]Broadcast{ testMAC: Broadcast{ testMAC, - testURL, + []string{testURL}, statusSlate, newRevidForTest((*logging.TestLogger)(t), testURL, t), }, @@ -136,7 +136,7 @@ func TestBroadcastManagerMarshal(t *testing.T) { log: logger, dogNotifier: newWatchdogNotifierForTest(t, logger), }, - expect: []byte("{\"Broadcasts\":{\"" + testMAC + "\":{\"MAC\":\"" + testMAC + "\",\"URL\":\"" + testURL + "\",\"Status\":\"" + statusSlate + "\"}},\"SlateExitSignals\":[\"" + testMAC + "\"]}"), + expect: []byte("{\"Broadcasts\":{\"" + testMAC + "\":{\"MAC\":\"" + testMAC + "\",\"URLs\":[\"" + testURL + "\"],\"Status\":\"" + statusSlate + "\"}},\"SlateExitSignals\":[\"" + testMAC + "\"]}"), }, } @@ -163,12 +163,12 @@ func TestBroadcastManagerUnmarshal(t *testing.T) { expect broadcastManager }{ { - in: []byte("{\"Broadcasts\":{\"" + testMAC + "\":{\"MAC\":\"" + testMAC + "\",\"URL\":\"" + testURL + "\",\"Status\":\"" + statusSlate + "\"}},\"SlateExitSignals\":[\"" + testMAC + "\"]}"), + in: []byte("{\"Broadcasts\":{\"" + testMAC + "\":{\"MAC\":\"" + testMAC + "\",\"URLs\":[\"" + testURL + "\"],\"Status\":\"" + statusSlate + "\"}},\"SlateExitSignals\":[\"" + testMAC + "\"]}"), expect: broadcastManager{ broadcasts: map[MAC]Broadcast{ testMAC: Broadcast{ testMAC, - testURL, + []string{testURL}, statusSlate, newRevidForTest((*logging.TestLogger)(t), testURL, t), }, @@ -239,7 +239,7 @@ func watchdogNotifiersEqual(w1, w2 watchdogNotifier) bool { } func broadcastsEqual(b1, b2 Broadcast) bool { - if b1.mac != b2.mac || b1.url != b2.url || b1.status != b2.status || + if b1.mac != b2.mac || !reflect.DeepEqual(b1.urls, b2.urls) || b1.status != b2.status || ((b1.rv == nil || b2.rv == nil) && b1.rv != b2.rv) { return false } @@ -268,7 +268,7 @@ func configsEqual(cfg1, cfg2 config.Config) bool { // newRevidForTest allows us to create revid in table driven test entry. func newRevidForTest(log logging.Logger, url string, t *testing.T) *revid.Revid { - r, err := newRevid(log, url) + r, err := newRevid(log, []string{url}) if err != nil { t.Fatalf("could not create revid pipeline: %v", err) return nil diff --git a/cmd/vidforward/main.go b/cmd/vidforward/main.go index c7aa8b57..7375fb0c 100644 --- a/cmd/vidforward/main.go +++ b/cmd/vidforward/main.go @@ -80,7 +80,7 @@ const ( // Broadcast is representative of a broadcast to be forwarded. type Broadcast struct { mac MAC // MAC address of the device from which the video is being received. - url string // The destination youtube RTMP URL. + urls []string // The destination youtube RTMP URLs. status string // The broadcast status i.e. active or slate. rv *revid.Revid // The revid pipeline which will handle forwarding to youtube. } @@ -281,12 +281,17 @@ func (m *broadcastManager) createOrUpdate(broadcast Broadcast) error { m.mu.Lock() defer m.mu.Unlock() + var outputs []uint8 + for _ = range broadcast.urls { + outputs = append(outputs, config.OutputRTMP) + } + cfg := config.Config{ Logger: m.log, Input: config.InputManual, InputCodec: codecutil.H264_AU, - Outputs: []uint8{config.OutputRTMP}, - RTMPURL: broadcast.url, + Outputs: outputs, + RTMPURL: broadcast.urls, LogLevel: logging.Debug, } diff --git a/cmd/vidforward/utils.go b/cmd/vidforward/utils.go index 4867cf04..4b5887f2 100644 --- a/cmd/vidforward/utils.go +++ b/cmd/vidforward/utils.go @@ -37,14 +37,14 @@ import ( var loggingLevel = logging.Info -func newRevid(log logging.Logger, url string) (*revid.Revid, error) { +func newRevid(log logging.Logger, urls []string) (*revid.Revid, error) { return revid.New( config.Config{ Logger: log, Input: config.InputManual, InputCodec: codecutil.H264_AU, Outputs: []uint8{config.OutputRTMP}, - RTMPURL: url, + RTMPURL: urls, LogLevel: loggingLevel, }, nil) } diff --git a/go.mod b/go.mod index 765778e3..83ed2ab3 100644 --- a/go.mod +++ b/go.mod @@ -35,5 +35,4 @@ require ( go.uber.org/multierr v1.1.0 // indirect go.uber.org/zap v1.10.0 // indirect golang.org/x/image v0.0.0-20210216034530-4410531fe030 // indirect - golang.org/x/text v0.3.5 // indirect ) diff --git a/go.sum b/go.sum index 0e75575d..63c6f33f 100644 --- a/go.sum +++ b/go.sum @@ -96,7 +96,6 @@ github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cb github.com/yobert/alsa v0.0.0-20180630182551-d38d89fa843e h1:3NIzz7weXhh3NToPgbtlQtKiVgerEaG4/nY2skGoGG0= github.com/yobert/alsa v0.0.0-20180630182551-d38d89fa843e/go.mod h1:CaowXBWOiSGWEpBBV8LoVnQTVPV4ycyviC9IBLj8dRw= github.com/yryz/ds18b20 v0.0.0-20180211073435-3cf383a40624/go.mod h1:MqFju5qeLDFh+S9PqxYT7TEla8xeW7bgGr/69q3oki0= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI= @@ -110,7 +109,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -129,14 +127,11 @@ golang.org/x/image v0.0.0-20210216034530-4410531fe030 h1:lP9pYkih3DUSC641giIXa2X golang.org/x/image v0.0.0-20210216034530-4410531fe030/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -146,22 +141,16 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210304124612-50617c2ba197/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210909193231-528a39cd75f3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=