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

<!DOCTYPE html>
<html>
<head>
<title>RTCPeerConnection.createDTMFSender</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
promise_test(async t => {
  let pc1 = new RTCPeerConnection();
  t.add_cleanup(() => pc1.close());
  let streams = await createStreams({audio:true, video:false}, 1);
  let stream = streams[0];
  let track = stream.getAudioTracks()[0];
  let sender = pc1.addTrack(track);
  let dtmf = pc1.createDTMFSender(track);
  assert_false(dtmf.canInsertDTMF);
  let pc2 = new RTCPeerConnection();
  t.add_cleanup(() => pc2.close());
  exchangeIceCandidates(pc1, pc2);
  let offer = await pc1.createOffer();
  await pc1.setLocalDescription(offer);
  await pc2.setRemoteDescription(offer);
  let answer = await pc2.createAnswer();
  await pc2.setLocalDescription(answer);
  await pc1.setRemoteDescription(answer);
  await new Promise((resolve, reject) => {
    let counter = 0;
    let checkfunc = function() {
      if (dtmf.canInsertDTMF) {
        resolve();
      } else {
        // Note: Using a short timeout and rejected promise in order to
        // make test return a clear error message on failure.
        if (counter >= 100) {
          reject('Waited too long for canInsertDTMF');
          return;
        }
        ++counter;
        step_timeout(checkfunc, 10);
      }
    };
    checkfunc();
  });
  assert_true(dtmf.canInsertDTMF);
}, 'DTMF sender for an audio track can be created.');

/**
 * Helper functions.
 */

function createStreams(constraints, numStreams, streamsSoFar = []) {
  if (numStreams == 0) {
    return Promise.resolve(streamsSoFar);
  }
  return navigator.mediaDevices.getUserMedia(constraints)
    .then(function(stream) {
      return createStreams(constraints,
                           numStreams - 1,
                           streamsSoFar.concat([stream]));
    });
}

function exchangeIceCandidates(pc1, pc2) {
  // private function
  function doExchange(localPc, remotePc) {
    localPc.addEventListener('icecandidate', event => {
      const { candidate } = event;
      if(candidate) {
        remotePc.addIceCandidate(candidate);
      }
    });
  }

  doExchange(pc1, pc2);
  doExchange(pc2, pc1);
}

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