Skip to content

Commit

Permalink
Fix loop request, ex. camera1: ffmpeg:camera1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Nov 9, 2024
1 parent 2c34a17 commit 340fd81
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func parseArgs(s string) *ffmpeg.Args {
Version: verAV,
}

var source = s
var query url.Values
if i := strings.IndexByte(s, '#'); i >= 0 {
query = streams.ParseQuery(s[i+1:])
Expand Down Expand Up @@ -221,6 +222,7 @@ func parseArgs(s string) *ffmpeg.Args {
default:
s += "?video&audio"
}
s += "&source=ffmpeg:" + url.QueryEscape(source)
args.Input = inputTemplate("rtsp", s, query)
} else if i = strings.Index(s, "?"); i > 0 {
switch s[:i] {
Expand Down
3 changes: 3 additions & 0 deletions internal/rtsp/rtsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func tcpHandler(conn *rtsp.Conn) {
conn.PacketSize = uint16(core.Atoi(s))
}

// will help to protect looping requests to same source
conn.Connection.Source = query.Get("source")

if err := stream.AddConsumer(conn); err != nil {
log.Warn().Err(err).Str("stream", name).Msg("[rtsp]")
return
Expand Down
6 changes: 6 additions & 0 deletions internal/streams/add_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func (s *Stream) AddConsumer(cons core.Consumer) (err error) {

producers:
for prodN, prod := range s.producers {
// check for loop request, ex. `camera1: ffmpeg:camera1`
if info, ok := cons.(core.Info); ok && prod.url == info.GetSource() {
log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN)
continue
}

if prodErrors[prodN] != nil {
log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN)
continue
Expand Down
5 changes: 5 additions & 0 deletions pkg/core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Info interface {
SetSource(string)
SetURL(string)
WithRequest(*http.Request)
GetSource() string
}

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

func (c *Connection) GetSource() string {
return c.Source
}

// Create like os.Create, init Consumer with existing Transport
func Create(w io.Writer) (*Connection, error) {
return &Connection{Transport: w}, nil
Expand Down

0 comments on commit 340fd81

Please sign in to comment.