Browse Source

support proxing servers that redirect to other urls

pull/169/head
aler9 5 years ago
parent
commit
42fa504014
  1. 2
      go.mod
  2. 4
      go.sum
  3. 76
      sourcertsp/source.go

2
go.mod

@ -5,7 +5,7 @@ go 1.15
require ( require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/aler9/gortsplib v0.0.0-20201101183936-812545b7765e github.com/aler9/gortsplib v0.0.0-20201101211017-ece3dd7077f8
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 github.com/fsnotify/fsnotify v1.4.9
github.com/notedit/rtmp v0.0.2 github.com/notedit/rtmp v0.0.2

4
go.sum

@ -2,8 +2,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/aler9/gortsplib v0.0.0-20201101183936-812545b7765e h1:miz7AbbS9DzhPnOt6EeoK3xqDh99nkZrfvQr/8pN3+k= github.com/aler9/gortsplib v0.0.0-20201101211017-ece3dd7077f8 h1:YpHqqbw/0gxh/l4QWA13kALiHBf7wyAqtHBsOt4AcrM=
github.com/aler9/gortsplib v0.0.0-20201101183936-812545b7765e/go.mod h1:8mpBfMEJIZn2C5fMM6vRYHgGH49WX0EH8gP1SDxv0Uw= github.com/aler9/gortsplib v0.0.0-20201101211017-ece3dd7077f8/go.mod h1:8mpBfMEJIZn2C5fMM6vRYHgGH49WX0EH8gP1SDxv0Uw=
github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625 h1:A3upkpYzceQTuBPvVleu1zd6R8jInhg5ifimSO7ku/o= github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625 h1:A3upkpYzceQTuBPvVleu1zd6R8jInhg5ifimSO7ku/o=
github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625/go.mod h1:5bO/aUQr9m3OasDatNNcVqKAgs7r5hgGXmszWHaC6mI= github.com/aler9/sdp-dirty/v3 v3.0.0-20200919115950-f1abc664f625/go.mod h1:5bO/aUQr9m3OasDatNNcVqKAgs7r5hgGXmszWHaC6mI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

76
sourcertsp/source.go

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/aler9/gortsplib" "github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/base"
"github.com/aler9/rtsp-simple-server/stats" "github.com/aler9/rtsp-simple-server/stats"
) )
@ -103,19 +102,16 @@ func (s *Source) run() {
func (s *Source) runInner() bool { func (s *Source) runInner() bool {
s.parent.Log("connecting to rtsp source") s.parent.Log("connecting to rtsp source")
u, _ := base.ParseURL(s.ur)
var conn *gortsplib.ConnClient var conn *gortsplib.ConnClient
var err error var err error
dialDone := make(chan struct{}, 1) dialDone := make(chan struct{}, 1)
go func() { go func() {
defer close(dialDone) defer close(dialDone)
conn, err = gortsplib.NewConnClient(gortsplib.ConnClientConf{ conn, err = gortsplib.Dialer{
Host: u.Host(),
ReadTimeout: s.readTimeout, ReadTimeout: s.readTimeout,
WriteTimeout: s.writeTimeout, WriteTimeout: s.writeTimeout,
ReadBufferCount: 2, ReadBufferCount: 2,
}) }.DialRead(s.ur, s.proto)
}() }()
select { select {
@ -129,47 +125,23 @@ func (s *Source) runInner() bool {
return true return true
} }
_, err = conn.Options(u) tracks := conn.Tracks()
if err != nil {
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
}
tracks, _, err := conn.Describe(u) s.parent.Log("rtsp source ready")
if err != nil { s.parent.OnSourceSetReady(tracks)
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
}
var ret bool
if s.proto == gortsplib.StreamProtocolUDP { if s.proto == gortsplib.StreamProtocolUDP {
return s.runUDP(u, conn, tracks) ret = s.runUDP(conn, tracks)
} else { } else {
return s.runTCP(u, conn, tracks) ret = s.runTCP(conn, tracks)
}
} }
func (s *Source) runUDP(u *base.URL, conn *gortsplib.ConnClient, tracks gortsplib.Tracks) bool { s.parent.OnSourceSetNotReady()
for _, track := range tracks { return ret
_, err := conn.SetupUDP(u, gortsplib.TransportModePlay, track, 0, 0)
if err != nil {
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
}
}
_, err := conn.Play(u)
if err != nil {
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
} }
s.parent.Log("rtsp source ready") func (s *Source) runUDP(conn *gortsplib.ConnClient, tracks gortsplib.Tracks) bool {
s.parent.OnSourceSetReady(tracks)
var wg sync.WaitGroup var wg sync.WaitGroup
// receive RTP packets // receive RTP packets
@ -231,32 +203,10 @@ outer:
} }
wg.Wait() wg.Wait()
s.parent.OnSourceSetNotReady()
return ret return ret
} }
func (s *Source) runTCP(u *base.URL, conn *gortsplib.ConnClient, tracks gortsplib.Tracks) bool { func (s *Source) runTCP(conn *gortsplib.ConnClient, tracks gortsplib.Tracks) bool {
for _, track := range tracks {
_, err := conn.SetupTCP(u, gortsplib.TransportModePlay, track)
if err != nil {
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
}
}
_, err := conn.Play(u)
if err != nil {
conn.Close()
s.parent.Log("rtsp source ERR: %s", err)
return true
}
s.parent.Log("rtsp source ready")
s.parent.OnSourceSetReady(tracks)
tcpConnDone := make(chan error) tcpConnDone := make(chan error)
go func() { go func() {
for { for {
@ -289,7 +239,5 @@ outer:
} }
} }
s.parent.OnSourceSetNotReady()
return ret return ret
} }

Loading…
Cancel
Save