优化track设定

This commit is contained in:
李宇翔
2021-06-23 15:57:44 +08:00
parent b0c3cdb21a
commit cc7b899922
5 changed files with 56 additions and 75 deletions

View File

@@ -218,14 +218,7 @@ func (client *RTSP) requestStream() (err error) {
} }
switch t { switch t {
case "video": case "video":
if len(sdpInfo.SpropParameterSets) > 1 { client.setVideoTrack()
client.RtpVideo = client.NewRTPVideo(7)
client.RtpVideo.PushNalu(VideoPack{NALUs: sdpInfo.SpropParameterSets})
} else if client.VSdp.Codec == "H264" {
client.RtpVideo = client.NewRTPVideo(7)
} else if client.VSdp.Codec == "H265" {
client.RtpVideo = client.NewRTPVideo(12)
}
if client.TransType == TRANS_TYPE_TCP { if client.TransType == TRANS_TYPE_TCP {
headers["Transport"] = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", client.vRTPChannel, client.vRTPControlChannel) headers["Transport"] = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", client.vRTPChannel, client.vRTPControlChannel)
} else { } else {
@@ -238,13 +231,7 @@ func (client *RTSP) requestStream() (err error) {
client.Conn.timeout = 0 // UDP ignore timeout client.Conn.timeout = 0 // UDP ignore timeout
} }
case "audio": case "audio":
client.RtpAudio = client.NewRTPAudio(0) client.setAudioTrack()
at := client.RtpAudio.AudioTrack
if len(client.ASdp.Control) > 0 {
at.SetASC(client.ASdp.Config)
} else {
client.setAudioFormat(at)
}
if client.TransType == TRANS_TYPE_TCP { if client.TransType == TRANS_TYPE_TCP {
headers["Transport"] = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", client.aRTPChannel, client.aRTPControlChannel) headers["Transport"] = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", client.aRTPChannel, client.aRTPControlChannel)
} else { } else {

11
go.sum
View File

@@ -1,14 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Monibuca/engine/v3 v3.0.0-alpha6 h1:USZa+JDVJd13rb9wmX06ZxjRCrqYTAPe5wVD+0IODEE=
github.com/Monibuca/engine/v3 v3.0.0-alpha6/go.mod h1:V0/kfen6K5O/RLXHPsZj4DF/LboDZ0OqfeCfn35bWMo=
github.com/Monibuca/engine/v3 v3.0.0-beta3 h1:/co+L2qCRZUq55S0LtYpY9xzOJiUUG3VGytYGFf1RD4=
github.com/Monibuca/engine/v3 v3.0.0-beta3/go.mod h1:SMgnlwih4pBA/HkTLjKXZFYkv3ukRzFjv65CARRLVIk=
github.com/Monibuca/engine/v3 v3.0.0-beta5 h1:b27ZQDfvf5dBMZbCSIUXItUwVIFs95fpkAV4xjN7BNE= github.com/Monibuca/engine/v3 v3.0.0-beta5 h1:b27ZQDfvf5dBMZbCSIUXItUwVIFs95fpkAV4xjN7BNE=
github.com/Monibuca/engine/v3 v3.0.0-beta5/go.mod h1:SMgnlwih4pBA/HkTLjKXZFYkv3ukRzFjv65CARRLVIk= github.com/Monibuca/engine/v3 v3.0.0-beta5/go.mod h1:SMgnlwih4pBA/HkTLjKXZFYkv3ukRzFjv65CARRLVIk=
github.com/Monibuca/utils/v3 v3.0.0-alpha4/go.mod h1:3xYmhQbgAZBHLyIMteUCd1va+1z/xnd72B585mCaT3c=
github.com/Monibuca/utils/v3 v3.0.0-alpha5 h1:IOyW/KJSRdRg+TPcgwkHLBynqfNQOV6p3iP7LgXEMFc=
github.com/Monibuca/utils/v3 v3.0.0-alpha5/go.mod h1:3xYmhQbgAZBHLyIMteUCd1va+1z/xnd72B585mCaT3c=
github.com/Monibuca/utils/v3 v3.0.0-beta h1:z4p/BSH5J9Ja/gwoDmj1RyN+b0q28Nmn/fqXiwq2hGY= github.com/Monibuca/utils/v3 v3.0.0-beta h1:z4p/BSH5J9Ja/gwoDmj1RyN+b0q28Nmn/fqXiwq2hGY=
github.com/Monibuca/utils/v3 v3.0.0-beta/go.mod h1:mQYP/OMox1tkWP6Qut7pBfARr1TXSRkK662dexQl6kI= github.com/Monibuca/utils/v3 v3.0.0-beta/go.mod h1:mQYP/OMox1tkWP6Qut7pBfARr1TXSRkK662dexQl6kI=
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0= github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0=
@@ -23,8 +16,6 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA= github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=
github.com/pion/rtp v1.6.2 h1:iGBerLX6JiDjB9NXuaPzHyxHFG9JsIEdgwTC0lp5n/U=
github.com/pion/rtp v1.6.2/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko=
github.com/pion/rtp v1.6.5 h1:o2cZf8OascA5HF/b0PAbTxRKvOWxTQxWYt7SlToxFGI= github.com/pion/rtp v1.6.5 h1:o2cZf8OascA5HF/b0PAbTxRKvOWxTQxWYt7SlToxFGI=
github.com/pion/rtp v1.6.5/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko= github.com/pion/rtp v1.6.5/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -37,7 +28,5 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVs
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 h1:SgQ6LNaYJU0JIuEHv9+s6EbhSCwYeAf5Yvj6lpYlqAE=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 h1:faDu4veV+8pcThn4fewv6TVlNCezafGoC1gM/mxQLbQ= golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 h1:faDu4veV+8pcThn4fewv6TVlNCezafGoC1gM/mxQLbQ=
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210611083646-a4fc73990273/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

37
main.go
View File

@@ -143,10 +143,8 @@ type RTSP struct {
SDPMap map[string]*SDPInfo SDPMap map[string]*SDPInfo
nonce string nonce string
closeOld bool
ASdp *SDPInfo ASdp *SDPInfo
VSdp *SDPInfo VSdp *SDPInfo
aacsent bool
Timeout int Timeout int
//tcp channels //tcp channels
aRTPChannel int aRTPChannel int
@@ -162,23 +160,42 @@ type RTSP struct {
RtpVideo *RTPVideo RtpVideo *RTPVideo
} }
func (rtsp *RTSP) setAudioFormat(at *AudioTrack) { func (rtsp *RTSP) setVideoTrack() {
if rtsp.VSdp.Codec == "H264" {
rtsp.RtpVideo = rtsp.NewRTPVideo(7)
if len(rtsp.VSdp.SpropParameterSets) > 1 {
rtsp.RtpVideo.PushNalu(VideoPack{NALUs: rtsp.VSdp.SpropParameterSets})
}
} else if rtsp.VSdp.Codec == "H265" {
rtsp.RtpVideo = rtsp.NewRTPVideo(12)
if len(rtsp.VSdp.VPS) > 0 {
rtsp.RtpVideo.PushNalu(VideoPack{NALUs: [][]byte{rtsp.VSdp.VPS, rtsp.ASdp.SPS, rtsp.ASdp.PPS}})
}
}
}
func (rtsp *RTSP) setAudioTrack() {
var at *RTPAudio
if len(rtsp.ASdp.Control) > 0 {
at = rtsp.NewRTPAudio(0)
at.SetASC(rtsp.ASdp.Config)
} else {
switch rtsp.ASdp.Codec { switch rtsp.ASdp.Codec {
case "aac": case "AAC":
at.CodecID = 10 at = rtsp.NewRTPAudio(10)
case "pcma": case "PCMA":
at.CodecID = 7 at = rtsp.NewRTPAudio(7)
at.SoundRate = rtsp.ASdp.TimeScale at.SoundRate = rtsp.ASdp.TimeScale
at.SoundSize = 16 at.SoundSize = 16
case "pcmu": case "PCMU":
at.CodecID = 8 at = rtsp.NewRTPAudio(8)
at.SoundRate = rtsp.ASdp.TimeScale at.SoundRate = rtsp.ASdp.TimeScale
at.SoundSize = 16 at.SoundSize = 16
default: default:
Printf("rtsp audio codec not support:%s", rtsp.ASdp.Codec) Printf("rtsp audio codec not support:%s", rtsp.ASdp.Codec)
return return
} }
rtsp.AudioTracks.AddTrack(rtsp.ASdp.Codec, at) }
rtsp.RtpAudio = at
} }
type RTSPClientInfo struct { type RTSPClientInfo struct {

View File

@@ -15,6 +15,9 @@ type SDPInfo struct {
Rtpmap int Rtpmap int
Config []byte Config []byte
SpropParameterSets [][]byte SpropParameterSets [][]byte
VPS []byte
PPS []byte
SPS []byte
PayloadType int PayloadType int
SizeLength int SizeLength int
IndexLength int IndexLength int
@@ -54,18 +57,10 @@ func ParseSDP(sdpRaw string) map[string]*SDPInfo {
} }
keyval = strings.Split(field, "/") keyval = strings.Split(field, "/")
if len(keyval) >= 2 { if len(keyval) >= 2 {
key := keyval[0] info.Codec = keyval[0]
switch key { switch info.Codec {
case "PCMA":
info.Codec = "pcma"
case "PCMU":
info.Codec = "pcmu"
case "MPEG4-GENERIC": case "MPEG4-GENERIC":
info.Codec = "aac" info.Codec = "AAC"
case "H264":
info.Codec = "h264"
case "H265":
info.Codec = "h265"
} }
if i, err := strconv.Atoi(keyval[1]); err == nil { if i, err := strconv.Atoi(keyval[1]); err == nil {
info.TimeScale = i info.TimeScale = i
@@ -85,6 +80,12 @@ func ParseSDP(sdpRaw string) map[string]*SDPInfo {
info.SizeLength, _ = strconv.Atoi(val) info.SizeLength, _ = strconv.Atoi(val)
case "indexlength": case "indexlength":
info.IndexLength, _ = strconv.Atoi(val) info.IndexLength, _ = strconv.Atoi(val)
case "sprop-vps":
info.VPS, _ = base64.StdEncoding.DecodeString(field)
case "sprop-sps":
info.SPS, _ = base64.StdEncoding.DecodeString(field)
case "sprop-pps":
info.PPS, _ = base64.StdEncoding.DecodeString(field)
case "sprop-parameter-sets": case "sprop-parameter-sets":
fields := strings.Split(val, ",") fields := strings.Split(val, ",")
for _, field := range fields { for _, field := range fields {

View File

@@ -341,24 +341,11 @@ func (session *RTSP) handleRequest(req *Request) {
session.Stream = stream session.Stream = stream
if session.Publish() { if session.Publish() {
if session.ASdp, session.HasAudio = session.SDPMap["audio"]; session.HasAudio { if session.ASdp, session.HasAudio = session.SDPMap["audio"]; session.HasAudio {
session.RtpAudio = stream.NewRTPAudio(0) session.setAudioTrack()
at := session.RtpAudio.AudioTrack
if len(session.ASdp.Control) > 0 {
at.SetASC(session.ASdp.Config)
} else {
session.setAudioFormat(at)
}
Printf("audio codec[%s]\n", session.ASdp.Codec) Printf("audio codec[%s]\n", session.ASdp.Codec)
} }
if session.VSdp, session.HasVideo = session.SDPMap["video"]; session.HasVideo { if session.VSdp, session.HasVideo = session.SDPMap["video"]; session.HasVideo {
if len(session.VSdp.SpropParameterSets) > 1 { session.setVideoTrack()
session.RtpVideo = stream.NewRTPVideo(7)
session.RtpVideo.PushNalu(VideoPack{NALUs: session.VSdp.SpropParameterSets})
} else if session.VSdp.Codec == "H264" {
session.RtpVideo = stream.NewRTPVideo(7)
} else if session.VSdp.Codec == "H265" {
session.RtpVideo = stream.NewRTPVideo(12)
}
Printf("video codec[%s]\n", session.VSdp.Codec) Printf("video codec[%s]\n", session.VSdp.Codec)
} }
session.Stream.Type = "RTSP" session.Stream.Type = "RTSP"