From 00ecd3469f79eb0f1c27d43caabbb7e308ccd62b Mon Sep 17 00:00:00 2001 From: dexter <178529795@qq.com> Date: Tue, 23 Nov 2021 12:40:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F=E6=AC=A1=E9=87=8D=E8=BF=9E=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E8=BF=9E=E6=8E=A5=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 11 +++++++++-- publisher.go | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 4d6b39e..e874d46 100644 --- a/client.go +++ b/client.go @@ -34,6 +34,11 @@ func (rtsp *RTSPClient) PullStream(streamPath string, rtspUrl string) (err error go func() { for rtsp.startStream(); rtsp.Err() == nil; rtsp.startStream() { Printf("reconnecting:%s in 5 seconds", rtspUrl) + if rtsp.Transport == gortsplib.TransportTCP { + rtsp.Transport = gortsplib.TransportUDP + } else { + rtsp.Transport = gortsplib.TransportTCP + } time.Sleep(time.Second * 5) } if rtsp.IsTimeout { @@ -166,11 +171,13 @@ func (client *RTSPClient) startStream() { // find published tracks tracks, baseURL, res, err := client.Describe(u) if err != nil { - Printf("Describe:%s error:%v", baseURL.String(), err) + Printf("Describe:%s error:%v", client.URL, err) return } Println(res) - client.setTracks(tracks) + if client.processFunc == nil { + client.setTracks(tracks) + } for _, track := range tracks { if res, err = client.Setup(true, baseURL, track, 0, 0); err != nil { Printf("Setup:%s error:%v", baseURL.String(), err) diff --git a/publisher.go b/publisher.go index de32e00..a3cae81 100644 --- a/publisher.go +++ b/publisher.go @@ -20,14 +20,17 @@ type RTSPublisher struct { func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { if p.processFunc != nil { - p.processFunc = p.processFunc[:0] + p.processFunc = p.processFunc[:len(tracks)] + return + } else { + p.processFunc = make([]func([]byte), len(tracks)) } for i, track := range tracks { v, ok := track.Media.Attribute("rtpmap") if !ok { continue } - + fmtp := make(map[string]string) if v, ok := track.Media.Attribute("fmtp"); ok { if tmp := strings.SplitN(v, " ", 2); len(tmp) == 2 { @@ -56,14 +59,14 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { timeScale = i } if len(keyval) >= 2 { - Printf("track %d is %s",i,keyval[0]) + Printf("track %d is %s", i, keyval[0]) switch strings.ToLower(keyval[0]) { case "h264": vt := p.NewRTPVideo(7) if conf, err := track.ExtractConfigH264(); err == nil { vt.PushNalu(0, 0, conf.SPS, conf.PPS) } - p.processFunc = append(p.processFunc, vt.Push) + p.processFunc[i] = vt.Push case "h265", "hevc": vt := p.NewRTPVideo(12) if v, ok := fmtp["sprop-vps"]; ok { @@ -78,7 +81,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { pps, _ := base64.StdEncoding.DecodeString(v) vt.PushNalu(0, 0, pps) } - p.processFunc = append(p.processFunc, vt.Push) + p.processFunc[i] = vt.Push case "pcma": at := p.NewRTPAudio(7) at.SoundRate = timeScale @@ -90,7 +93,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { at.Channels = 1 } at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)} - p.processFunc = append(p.processFunc, at.Push) + p.processFunc[i] = at.Push case "pcmu": at := p.NewRTPAudio(8) at.SoundRate = timeScale @@ -102,7 +105,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { at.Channels = 1 } at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)} - p.processFunc = append(p.processFunc, at.Push) + p.processFunc[i] = at.Push case "mpeg4-generic": at := p.NewRTPAudio(0) if config, ok := fmtp["config"]; ok { @@ -112,7 +115,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { Println("aac no config") } at.SoundSize = 16 - p.processFunc = append(p.processFunc, at.Push) + p.processFunc[i] = at.Push } } }