diff --git a/client.go b/client.go index b278c23..4d6b39e 100644 --- a/client.go +++ b/client.go @@ -17,7 +17,8 @@ import ( type RTSPClient struct { RTSPublisher - gortsplib.Client `json:"-"` + Transport gortsplib.Transport + *gortsplib.Client `json:"-"` } // PullStream 从外部拉流 @@ -27,9 +28,6 @@ func (rtsp *RTSPClient) PullStream(streamPath string, rtspUrl string) (err error Type: "RTSP Pull", ExtraProp: rtsp, } - rtsp.OnPacketRTP = func(trackID int, payload []byte) { - rtsp.processFunc[trackID](payload) - } if result := rtsp.Publish(); result { rtsp.URL = rtspUrl if config.Reconnect { @@ -38,16 +36,12 @@ func (rtsp *RTSPClient) PullStream(streamPath string, rtspUrl string) (err error Printf("reconnecting:%s in 5 seconds", rtspUrl) time.Sleep(time.Second * 5) } - rtsp.Client.Close() if rtsp.IsTimeout { go rtsp.PullStream(streamPath, rtspUrl) } }() } else { - go func() { - rtsp.startStream() - rtsp.Client.Close() - }() + go rtsp.startStream() } return } @@ -144,6 +138,14 @@ func (client *RTSPClient) startStream() { if client.Err() != nil { return } + client.Client = &gortsplib.Client{ + OnPacketRTP: func(trackID int, payload []byte) { + var clone []byte + client.processFunc[trackID](append(clone, payload...)) + }, + Transport: &client.Transport, + } + defer client.Client.Close() // parse URL u, err := base.ParseURL(client.URL) if err != nil { diff --git a/go.mod b/go.mod index be77118..fd21bfd 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.16 require ( github.com/Monibuca/engine/v3 v3.3.11 github.com/Monibuca/utils/v3 v3.0.4 - github.com/aler9/gortsplib v0.0.0-20211115164017-1411cb33f558 + github.com/aler9/gortsplib v0.0.0-20211118111704-d5bdc197beb5 github.com/pion/rtp v1.7.4 github.com/pion/sdp/v3 v3.0.4 - golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c // indirect + golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect ) diff --git a/go.sum b/go.sum index cfa229c..74d24c3 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/aler9/gortsplib v0.0.0-20211106122816-6e38851a096e h1:qSjVAaIvJukmEuL github.com/aler9/gortsplib v0.0.0-20211106122816-6e38851a096e/go.mod h1:fyQrQyHo8QvdR/h357tkv1g36VesZlzEPsdAu2VrHHc= github.com/aler9/gortsplib v0.0.0-20211115164017-1411cb33f558 h1:uyW1alIzoJCAmNZ2xuo5EOTbSbf9W7tVYOzb0UrzZ0U= github.com/aler9/gortsplib v0.0.0-20211115164017-1411cb33f558/go.mod h1:fyQrQyHo8QvdR/h357tkv1g36VesZlzEPsdAu2VrHHc= +github.com/aler9/gortsplib v0.0.0-20211118111704-d5bdc197beb5 h1:qcrLOrHxW0x9fIguacwD/2CIlswI6kVCUThtx/uuhSs= +github.com/aler9/gortsplib v0.0.0-20211118111704-d5bdc197beb5/go.mod h1:fyQrQyHo8QvdR/h357tkv1g36VesZlzEPsdAu2VrHHc= github.com/asticode/go-astikit v0.20.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0= github.com/asticode/go-astits v1.10.0/go.mod h1:DkOWmBNQpnr9mv24KfZjq4JawCFX1FCqjLVGvO0DygQ= github.com/cnotch/apirouter v0.0.0-20200731232942-89e243a791f3/go.mod h1:5deJPLON/x/s2dLOQfuKS0lenhOIT4xX0pvtN/OEIuY= @@ -94,6 +96,8 @@ golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 h1:7NCfEGl0sfUojmX78nK9pBJuU golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c h1:DHcbWVXeY+0Y8HHKR+rbLwnoh2F4tNCY7rTiHJ30RmA= golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 h1:kwrAHlwJ0DUBZwQ238v+Uod/3eZ8B2K5rYsUHBQvzmI= +golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go index a5d1e58..a646b59 100644 --- a/main.go +++ b/main.go @@ -58,14 +58,14 @@ func runPlugin() { CORS(w, r) targetURL := r.URL.Query().Get("target") streamPath := r.URL.Query().Get("streamPath") - if err := (&RTSPClient{}).PullStream(streamPath, targetURL); err == nil { + if err := (&RTSPClient{Transport: gortsplib.TransportTCP}).PullStream(streamPath, targetURL); err == nil { w.Write([]byte(`{"code":0}`)) } else { w.Write([]byte(fmt.Sprintf(`{"code":1,"msg":"%s"}`, err.Error()))) } }) for streamPath, url := range config.AutoPullList { - if err := (&RTSPClient{}).PullStream(streamPath, url); err != nil { + if err := (&RTSPClient{Transport: gortsplib.TransportTCP}).PullStream(streamPath, url); err != nil { Println(err) } }