Skip to content

Commit

Permalink
Merge pull request #12 from jaygno/master
Browse files Browse the repository at this point in the history
支持采样率解析
  • Loading branch information
notedit authored Aug 18, 2022
2 parents 059e378 + 9f4cfe0 commit 674ccca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
23 changes: 20 additions & 3 deletions codecinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdp

import (
"strconv"
"strings"
)

Expand All @@ -10,16 +11,18 @@ type CodecInfo struct {
rtx int
params map[string]string
rtcpfbs []*RTCPFeedbackInfo
rate int
}

func NewCodecInfo(codec string, payload int) *CodecInfo {
func NewCodecInfo(codec string, payload int, rate int) *CodecInfo {

codecInfo := &CodecInfo{
codec: codec,
payload: payload,
rtx: 0,
params: map[string]string{},
rtcpfbs: []*RTCPFeedbackInfo{},
rate: rate,
}

return codecInfo
Expand All @@ -33,6 +36,7 @@ func (c *CodecInfo) Clone() *CodecInfo {
rtx: c.rtx,
params: make(map[string]string),
rtcpfbs: []*RTCPFeedbackInfo{},
rate: c.rate,
}

for key, param := range c.params {
Expand Down Expand Up @@ -72,6 +76,14 @@ func (c *CodecInfo) GetCodec() string {
return c.codec
}

func (c *CodecInfo) GetRate() int {
return c.rate
}

func (c *CodecInfo) SetRate(rate int) {
c.rate = rate
}

func (c *CodecInfo) GetParam(key string) string {
if c.HasParam(key) {
return c.params[key]
Expand Down Expand Up @@ -121,8 +133,13 @@ func CodecMapFromNames(names []string, rtx bool, rtcpfbs []*RTCPFeedbackInfo) ma
for _, name := range names {

var pt int
var rate int
params := strings.Split(name, ";")
codecName := strings.TrimSpace(strings.ToLower(params[0]))
encoding := strings.Split(params[0], "/")
codecName := strings.TrimSpace(strings.ToLower(encoding[0]))
if len(encoding) == 2 {
rate, _ = strconv.Atoi(encoding[1])
}
if codecName == "pcmu" {
pt = 0
} else if codecName == "pcma" {
Expand All @@ -132,7 +149,7 @@ func CodecMapFromNames(names []string, rtx bool, rtcpfbs []*RTCPFeedbackInfo) ma
pt = basePt
}

codec := NewCodecInfo(codecName, pt)
codec := NewCodecInfo(codecName, pt, rate)

if rtx && codecName != "ulpfec" && codecName != "flexfec-03" && codecName != "red" {
basePt++
Expand Down
1 change: 1 addition & 0 deletions mediainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func (m *MediaInfo) AnswerCapability(cap *Capability) *MediaInfo {
if cloned.HasRTX() {
cloned.SetRTX(codec.GetRTX())
}
cloned.SetRate(codec.GetRate())
cloned.AddParams(codec.GetParams())
answer.AddCodec(cloned)
}
Expand Down
7 changes: 4 additions & 3 deletions sdpinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,14 @@ func (s *SDPInfo) String() string {
mediaMap.Rtp = append(mediaMap.Rtp, &transform.RtpStruct{
Payload: codec.GetPayload(),
Codec: codec.GetCodec(),
Rate: 48000,
Rate: codec.GetRate(),
Encoding: 2,
})
} else {
mediaMap.Rtp = append(mediaMap.Rtp, &transform.RtpStruct{
Payload: codec.GetPayload(),
Codec: codec.GetCodec(),
Rate: 8000,
Rate: codec.GetRate(),
})
}
}
Expand Down Expand Up @@ -865,6 +865,7 @@ func Parse(sdp string) (*SDPInfo, error) {

payload := fmt.Payload
codec := fmt.Codec
rate := fmt.Rate

if "RED" == strings.ToUpper(codec) || "ULPFEC" == strings.ToUpper(codec) {
continue
Expand Down Expand Up @@ -893,7 +894,7 @@ func Parse(sdp string) (*SDPInfo, error) {
apts[aptint] = payload
}
} else {
codecInfo := NewCodecInfo(codec, payload)
codecInfo := NewCodecInfo(codec, payload, rate)
codecInfo.AddParams(params)
mediaInfo.AddCodec(codecInfo)
}
Expand Down
4 changes: 2 additions & 2 deletions sdpinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ const sdpstr = "v=1\r\n" +

var Capabilities = map[string]*Capability{
"audio": &Capability{
Codecs: []string{"opus"},
Codecs: []string{"opus/48000"},
},
"video": &Capability{
Codecs: []string{"vp8"},
Codecs: []string{"vp8/90000"},
Rtx: true,
Rtcpfbs: []*RtcpFeedback{
&RtcpFeedback{
Expand Down

0 comments on commit 674ccca

Please sign in to comment.