Skip to content

Commit

Permalink
refs #104802; override to correct payloadType into rtp headers;
Browse files Browse the repository at this point in the history
  • Loading branch information
r-novel committed Feb 17, 2020
1 parent 09a6722 commit ecd95cb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mediaengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/sdp/v2"
"github.com/pion/sdp"
)

// PayloadTypes for the default codecs
Expand Down Expand Up @@ -108,6 +108,16 @@ func (m *MediaEngine) PopulateFromSDP(sd SessionDescription) error {
return nil
}

func (m *MediaEngine) GetCodecsByName(codecName string) []*RTPCodec {
var codecs []*RTPCodec
for _, codec := range m.codecs {
if codec.Name == codecName {
codecs = append(codecs, codec)
}
}
return codecs
}

func (m *MediaEngine) getCodec(payloadType uint8) (*RTPCodec, error) {
for _, codec := range m.codecs {
if codec.PayloadType == payloadType {
Expand Down
44 changes: 44 additions & 0 deletions rtpsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type RTPSender struct {
// A reference to the associated api object
api *API

payloadType *uint8

mu sync.RWMutex
sendCalled, stopCalled chan interface{}
}
Expand Down Expand Up @@ -148,10 +150,52 @@ func (r *RTPSender) sendRTP(header *rtp.Header, payload []byte) (int, error) {
return 0, err
}

if r.payloadType == nil {
codecs := r.api.mediaEngine.GetCodecsByName(r.track.codec.Name)
if len(codecs) == 0 {
return 0, fmt.Errorf("no %s codecs in media engine", r.track.codec.Name)
}
for _, c := range codecs {
if equalCodecs(c, r.track.codec) {
r.payloadType = &c.PayloadType
break
}
}
if r.payloadType == nil {
return 0, fmt.Errorf("could not match %s codec from track to media engine", r.track.codec.Name)
}
}
// Overwrite the payload type in the RTP header.
if r.payloadType != nil {
header.PayloadType = *r.payloadType
}

return writeStream.WriteRTP(header, payload)
}
}

func equalCodecs(codecA, codecB *RTPCodec) bool {
if codecA.Name != codecB.Name {
return false
}
if codecA.Type != codecB.Type {
return false
}
if codecA.SDPFmtpLine != codecB.SDPFmtpLine {
return false
}
if codecA.Channels != codecB.Channels {
return false
}
if codecA.ClockRate != codecB.ClockRate {
return false
}
if codecA.MimeType != codecB.MimeType {
return false
}
return true
}

// hasSent tells if data has been ever sent for this instance
func (r *RTPSender) hasSent() bool {
select {
Expand Down

0 comments on commit ecd95cb

Please sign in to comment.