github.com/pion/webrtc/v4@v4.0.1/rtptransceiver_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !js
     5  // +build !js
     6  
     7  package webrtc
     8  
     9  import (
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func Test_RTPTransceiver_SetCodecPreferences(t *testing.T) {
    17  	me := &MediaEngine{}
    18  	api := NewAPI(WithMediaEngine(me))
    19  	assert.NoError(t, me.RegisterDefaultCodecs())
    20  
    21  	me.pushCodecs(me.videoCodecs, RTPCodecTypeVideo)
    22  	me.pushCodecs(me.audioCodecs, RTPCodecTypeAudio)
    23  
    24  	tr := RTPTransceiver{kind: RTPCodecTypeVideo, api: api, codecs: me.videoCodecs}
    25  	assert.EqualValues(t, me.videoCodecs, tr.getCodecs())
    26  
    27  	failTestCases := [][]RTPCodecParameters{
    28  		{
    29  			{
    30  				RTPCodecCapability: RTPCodecCapability{MimeTypeOpus, 48000, 2, "minptime=10;useinbandfec=1", nil},
    31  				PayloadType:        111,
    32  			},
    33  		},
    34  		{
    35  			{
    36  				RTPCodecCapability: RTPCodecCapability{MimeTypeVP8, 90000, 0, "", nil},
    37  				PayloadType:        96,
    38  			},
    39  			{
    40  				RTPCodecCapability: RTPCodecCapability{MimeTypeOpus, 48000, 2, "minptime=10;useinbandfec=1", nil},
    41  				PayloadType:        111,
    42  			},
    43  		},
    44  	}
    45  
    46  	for _, testCase := range failTestCases {
    47  		assert.ErrorIs(t, tr.SetCodecPreferences(testCase), errRTPTransceiverCodecUnsupported)
    48  	}
    49  
    50  	successTestCases := [][]RTPCodecParameters{
    51  		{
    52  			{
    53  				RTPCodecCapability: RTPCodecCapability{MimeTypeVP8, 90000, 0, "", nil},
    54  				PayloadType:        96,
    55  			},
    56  		},
    57  		{
    58  			{
    59  				RTPCodecCapability: RTPCodecCapability{MimeTypeVP8, 90000, 0, "", nil},
    60  				PayloadType:        96,
    61  			},
    62  			{
    63  				RTPCodecCapability: RTPCodecCapability{MimeTypeRTX, 90000, 0, "apt=96", nil},
    64  				PayloadType:        97,
    65  			},
    66  
    67  			{
    68  				RTPCodecCapability: RTPCodecCapability{MimeTypeVP9, 90000, 0, "profile-id=0", nil},
    69  				PayloadType:        98,
    70  			},
    71  			{
    72  				RTPCodecCapability: RTPCodecCapability{MimeTypeRTX, 90000, 0, "apt=98", nil},
    73  				PayloadType:        99,
    74  			},
    75  		},
    76  	}
    77  
    78  	for _, testCase := range successTestCases {
    79  		assert.NoError(t, tr.SetCodecPreferences(testCase))
    80  	}
    81  
    82  	assert.NoError(t, tr.SetCodecPreferences(nil))
    83  	assert.NotEqual(t, 0, len(tr.getCodecs()))
    84  
    85  	assert.NoError(t, tr.SetCodecPreferences([]RTPCodecParameters{}))
    86  	assert.NotEqual(t, 0, len(tr.getCodecs()))
    87  }
    88  
    89  // Assert that SetCodecPreferences properly filters codecs and PayloadTypes are respected
    90  func Test_RTPTransceiver_SetCodecPreferences_PayloadType(t *testing.T) {
    91  	testCodec := RTPCodecParameters{
    92  		RTPCodecCapability: RTPCodecCapability{"video/testCodec", 90000, 0, "", nil},
    93  		PayloadType:        50,
    94  	}
    95  
    96  	m := &MediaEngine{}
    97  	assert.NoError(t, m.RegisterDefaultCodecs())
    98  
    99  	offerPC, err := NewAPI(WithMediaEngine(m)).NewPeerConnection(Configuration{})
   100  	assert.NoError(t, err)
   101  
   102  	assert.NoError(t, m.RegisterCodec(testCodec, RTPCodecTypeVideo))
   103  
   104  	answerPC, err := NewAPI(WithMediaEngine(m)).NewPeerConnection(Configuration{})
   105  	assert.NoError(t, err)
   106  
   107  	_, err = offerPC.AddTransceiverFromKind(RTPCodecTypeVideo)
   108  	assert.NoError(t, err)
   109  
   110  	answerTransceiver, err := answerPC.AddTransceiverFromKind(RTPCodecTypeVideo)
   111  	assert.NoError(t, err)
   112  
   113  	assert.NoError(t, answerTransceiver.SetCodecPreferences([]RTPCodecParameters{
   114  		testCodec,
   115  		{
   116  			RTPCodecCapability: RTPCodecCapability{MimeTypeVP8, 90000, 0, "", nil},
   117  			PayloadType:        51,
   118  		},
   119  	}))
   120  
   121  	offer, err := offerPC.CreateOffer(nil)
   122  	assert.NoError(t, err)
   123  
   124  	assert.NoError(t, offerPC.SetLocalDescription(offer))
   125  	assert.NoError(t, answerPC.SetRemoteDescription(offer))
   126  
   127  	answer, err := answerPC.CreateAnswer(nil)
   128  	assert.NoError(t, err)
   129  
   130  	// VP8 with proper PayloadType
   131  	assert.NotEqual(t, -1, strings.Index(answer.SDP, "a=rtpmap:51 VP8/90000"))
   132  
   133  	// testCodec is ignored since offerer doesn't support
   134  	assert.Equal(t, -1, strings.Index(answer.SDP, "testCodec"))
   135  
   136  	closePairNow(t, offerPC, answerPC)
   137  }