chromium/third_party/blink/web_tests/fast/peerconnection/RTCPeerConnection-payloadTypes.html

<!DOCTYPE html>
<html>
<head>
<title>RTCPeerConnection RTP payload types</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="resources/RTCPeerConnection-helper.js"></script>
</head>
<body>
<script>

// Test that when creating an offer we do not run out of valid payload types.
promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());

  const offer = await pc1.createOffer({offerToReceiveAudio: true, offerToReceiveVideo: true});

  // Extract all payload types from the m= lines.
  const payloadTypes = offer.sdp.split('\n')
    .map(line => line.trim())
    .filter(line => line.startsWith('m='))
    .map(line => line.split(' ').slice(3).join(' '))
    .join(' ')
    .split(' ')
    .map(payloadType => parseInt(payloadType, 10));

  // The list of allowed payload types is taken from here
  // https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-1.
  const forbiddenPayloadTypes = payloadTypes
    .filter(payloadType => {
      if (payloadType >= 96 && payloadType <= 127) {
        return false;
      }
      if (payloadType >= 72 && payloadType < 96) {
        return true;
      }
      if (payloadType >= 35 && payloadType < 72) {
        return false;
      }
      // TODO: Check against static payload type list.
      return false;
    });
  assert_equals(forbiddenPayloadTypes.length, 0)
}, 'createOffer with the maximum set of codecs and RTP datachannels does not generate invalid payload types');

// Test local SDP munging of payload type.
// This is illegal according to spec, but has been supported in the past, so we verify
// that it is not yet broken.
promise_test(async t => {
  const pc1 = new RTCPeerConnection();
  const pc2 = new RTCPeerConnection();
  t.add_cleanup(() => {
    pc1.close();
    pc2.close();
  });
  pc1.addTransceiver('audio');
  const offer = await pc1.createOffer();
  // Replace all Opus (default PT: 111) codec references with "121"
  // This includes the m= line, the a=rtpmap line, and the fmtp parameter of RED.
  offer.sdp = offer.sdp.replace(/111/g, "121");
  console.log(offer.sdp);
  // Check that the codec has the assigned PT.
  // The codec list is populated only after offer/answer is exchanged.
  await pc1.setLocalDescription(offer);
  await pc2.setRemoteDescription(offer);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);
  const parameters = pc1.getSenders()[0].getParameters();
  // Note: fragile - this depends on the media codec being the first one.
  assert_equals(parameters.codecs[0].mimeType, 'audio/opus');
  assert_equals(parameters.codecs[0].payloadType, 121);

}, 'Modifying PT after createOffer() succeeds');

</script>
</body>
</html>