每次重连切换连接方式

This commit is contained in:
dexter
2021-11-23 12:40:21 +08:00
parent 4107d31c79
commit 00ecd3469f
2 changed files with 20 additions and 10 deletions

View File

@@ -34,6 +34,11 @@ func (rtsp *RTSPClient) PullStream(streamPath string, rtspUrl string) (err error
go func() { go func() {
for rtsp.startStream(); rtsp.Err() == nil; rtsp.startStream() { for rtsp.startStream(); rtsp.Err() == nil; rtsp.startStream() {
Printf("reconnecting:%s in 5 seconds", rtspUrl) 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) time.Sleep(time.Second * 5)
} }
if rtsp.IsTimeout { if rtsp.IsTimeout {
@@ -166,11 +171,13 @@ func (client *RTSPClient) startStream() {
// find published tracks // find published tracks
tracks, baseURL, res, err := client.Describe(u) tracks, baseURL, res, err := client.Describe(u)
if err != nil { if err != nil {
Printf("Describe:%s error:%v", baseURL.String(), err) Printf("Describe:%s error:%v", client.URL, err)
return return
} }
Println(res) Println(res)
client.setTracks(tracks) if client.processFunc == nil {
client.setTracks(tracks)
}
for _, track := range tracks { for _, track := range tracks {
if res, err = client.Setup(true, baseURL, track, 0, 0); err != nil { if res, err = client.Setup(true, baseURL, track, 0, 0); err != nil {
Printf("Setup:%s error:%v", baseURL.String(), err) Printf("Setup:%s error:%v", baseURL.String(), err)

View File

@@ -20,14 +20,17 @@ type RTSPublisher struct {
func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) { func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
if p.processFunc != nil { 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 { for i, track := range tracks {
v, ok := track.Media.Attribute("rtpmap") v, ok := track.Media.Attribute("rtpmap")
if !ok { if !ok {
continue continue
} }
fmtp := make(map[string]string) fmtp := make(map[string]string)
if v, ok := track.Media.Attribute("fmtp"); ok { if v, ok := track.Media.Attribute("fmtp"); ok {
if tmp := strings.SplitN(v, " ", 2); len(tmp) == 2 { if tmp := strings.SplitN(v, " ", 2); len(tmp) == 2 {
@@ -56,14 +59,14 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
timeScale = i timeScale = i
} }
if len(keyval) >= 2 { 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]) { switch strings.ToLower(keyval[0]) {
case "h264": case "h264":
vt := p.NewRTPVideo(7) vt := p.NewRTPVideo(7)
if conf, err := track.ExtractConfigH264(); err == nil { if conf, err := track.ExtractConfigH264(); err == nil {
vt.PushNalu(0, 0, conf.SPS, conf.PPS) vt.PushNalu(0, 0, conf.SPS, conf.PPS)
} }
p.processFunc = append(p.processFunc, vt.Push) p.processFunc[i] = vt.Push
case "h265", "hevc": case "h265", "hevc":
vt := p.NewRTPVideo(12) vt := p.NewRTPVideo(12)
if v, ok := fmtp["sprop-vps"]; ok { if v, ok := fmtp["sprop-vps"]; ok {
@@ -78,7 +81,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
pps, _ := base64.StdEncoding.DecodeString(v) pps, _ := base64.StdEncoding.DecodeString(v)
vt.PushNalu(0, 0, pps) vt.PushNalu(0, 0, pps)
} }
p.processFunc = append(p.processFunc, vt.Push) p.processFunc[i] = vt.Push
case "pcma": case "pcma":
at := p.NewRTPAudio(7) at := p.NewRTPAudio(7)
at.SoundRate = timeScale at.SoundRate = timeScale
@@ -90,7 +93,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
at.Channels = 1 at.Channels = 1
} }
at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)} at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)}
p.processFunc = append(p.processFunc, at.Push) p.processFunc[i] = at.Push
case "pcmu": case "pcmu":
at := p.NewRTPAudio(8) at := p.NewRTPAudio(8)
at.SoundRate = timeScale at.SoundRate = timeScale
@@ -102,7 +105,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
at.Channels = 1 at.Channels = 1
} }
at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)} at.ExtraData = []byte{(at.CodecID << 4) | (1 << 1)}
p.processFunc = append(p.processFunc, at.Push) p.processFunc[i] = at.Push
case "mpeg4-generic": case "mpeg4-generic":
at := p.NewRTPAudio(0) at := p.NewRTPAudio(0)
if config, ok := fmtp["config"]; ok { if config, ok := fmtp["config"]; ok {
@@ -112,7 +115,7 @@ func (p *RTSPublisher) setTracks(tracks gortsplib.Tracks) {
Println("aac no config") Println("aac no config")
} }
at.SoundSize = 16 at.SoundSize = 16
p.processFunc = append(p.processFunc, at.Push) p.processFunc[i] = at.Push
} }
} }
} }