Skip to content

Commit

Permalink
Properly update metadata when PayloadType is 0
Browse files Browse the repository at this point in the history
In the case where a remote track is sending PCMU with payload type 0
checkAndUpdateTrack will fail to update the track codec and params
(because t.PayloadType() is already 0). Add an extra check to handle
this case.
  • Loading branch information
mklingb authored and Sean-Der committed Jun 5, 2024
1 parent 1778f7f commit 6981562
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
45 changes: 45 additions & 0 deletions peerconnection_media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1729,3 +1729,48 @@ func TestPeerConnection_Simulcast_NoDataChannel(t *testing.T) {

closePairNow(t, pcSender, pcReceiver)
}

// Check that PayloadType of 0 is handled correctly. At one point
// we incorrectly assumed 0 meant an invalid stream and wouldn't update things
// properly
func TestPeerConnection_Zero_PayloadType(t *testing.T) {
lim := test.TimeOut(time.Second * 5)
defer lim.Stop()

report := test.CheckRoutines(t)
defer report()

pcOffer, pcAnswer, err := newPair()
require.NoError(t, err)

audioTrack, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypePCMU}, "audio", "audio")
require.NoError(t, err)

_, err = pcOffer.AddTrack(audioTrack)
require.NoError(t, err)

assert.NoError(t, signalPair(pcOffer, pcAnswer))

onTrackFired, onTrackFiredCancel := context.WithCancel(context.Background())
pcAnswer.OnTrack(func(track *TrackRemote, _ *RTPReceiver) {
require.Equal(t, track.Codec().MimeType, MimeTypePCMU)
onTrackFiredCancel()
})

go func() {
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()

select {
case <-onTrackFired.Done():
return
case <-ticker.C:
if routineErr := audioTrack.WriteSample(media.Sample{Data: []byte{0x00}, Duration: time.Second}); routineErr != nil {
fmt.Println(routineErr)
}
}
}()

<-onTrackFired.Done()
closePairNow(t, pcOffer, pcAnswer)
}
3 changes: 2 additions & 1 deletion track_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func (t *TrackRemote) checkAndUpdateTrack(b []byte) error {
return errRTPTooShort
}

if payloadType := PayloadType(b[1] & rtpPayloadTypeBitmask); payloadType != t.PayloadType() {
payloadType := PayloadType(b[1] & rtpPayloadTypeBitmask)
if payloadType != t.PayloadType() || len(t.params.Codecs) == 0 {
t.mu.Lock()
defer t.mu.Unlock()

Expand Down

0 comments on commit 6981562

Please sign in to comment.