Skip to content

Commit 340fd81

Browse files
committed
Fix loop request, ex. camera1: ffmpeg:camera1
1 parent 2c34a17 commit 340fd81

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

internal/ffmpeg/ffmpeg.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func parseArgs(s string) *ffmpeg.Args {
179179
Version: verAV,
180180
}
181181

182+
var source = s
182183
var query url.Values
183184
if i := strings.IndexByte(s, '#'); i >= 0 {
184185
query = streams.ParseQuery(s[i+1:])
@@ -221,6 +222,7 @@ func parseArgs(s string) *ffmpeg.Args {
221222
default:
222223
s += "?video&audio"
223224
}
225+
s += "&source=ffmpeg:" + url.QueryEscape(source)
224226
args.Input = inputTemplate("rtsp", s, query)
225227
} else if i = strings.Index(s, "?"); i > 0 {
226228
switch s[:i] {

internal/rtsp/rtsp.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ func tcpHandler(conn *rtsp.Conn) {
188188
conn.PacketSize = uint16(core.Atoi(s))
189189
}
190190

191+
// will help to protect looping requests to same source
192+
conn.Connection.Source = query.Get("source")
193+
191194
if err := stream.AddConsumer(conn); err != nil {
192195
log.Warn().Err(err).Str("stream", name).Msg("[rtsp]")
193196
return

internal/streams/add_consumer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func (s *Stream) AddConsumer(cons core.Consumer) (err error) {
2222

2323
producers:
2424
for prodN, prod := range s.producers {
25+
// check for loop request, ex. `camera1: ffmpeg:camera1`
26+
if info, ok := cons.(core.Info); ok && prod.url == info.GetSource() {
27+
log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN)
28+
continue
29+
}
30+
2531
if prodErrors[prodN] != nil {
2632
log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN)
2733
continue

pkg/core/connection.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Info interface {
2525
SetSource(string)
2626
SetURL(string)
2727
WithRequest(*http.Request)
28+
GetSource() string
2829
}
2930

3031
// Connection just like webrtc.PeerConnection
@@ -123,6 +124,10 @@ func (c *Connection) WithRequest(r *http.Request) {
123124
c.UserAgent = r.UserAgent()
124125
}
125126

127+
func (c *Connection) GetSource() string {
128+
return c.Source
129+
}
130+
126131
// Create like os.Create, init Consumer with existing Transport
127132
func Create(w io.Writer) (*Connection, error) {
128133
return &Connection{Transport: w}, nil

0 commit comments

Comments
 (0)