1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256

content / test / data / media / peerconnection-call-audio.html [blame]

<html>
<head>
  <script type="text/javascript" src="webrtc_test_utilities.js"></script>
  <script type="text/javascript" src="webrtc_test_common.js"></script>
  <script type="text/javascript" src="webrtc_test_audio.js"></script>
  <script type="text/javascript">
  $ = function(id) {
    return document.getElementById(id);
  };

  var gFirstConnection = null;
  var gSecondConnection = null;
  var gLocalStream = null;

  var gRemoteStreams = {};

  // The second set of constraints should request audio (e.g. audio:true) since
  // we expect audio to be playing after the second renegotiation.
  function callAndRenegotiateToAudio(constraints, renegotiationConstraints) {
    createConnections(null);
    return Promise.all([
      navigator.mediaDevices.getUserMedia(constraints)
          .then(addStreamToBothConnectionsAndNegotiate),

      waitForConnectionToStabilize(gFirstConnection).then(() => {
        gFirstConnection.removeStream(gLocalStream);
        gSecondConnection.removeStream(gLocalStream);
        return navigator.mediaDevices.getUserMedia(renegotiationConstraints)
            .then(addStreamToTheFirstConnectionAndNegotiate);
        }).then(() => {
          return waitForConnectionToStabilize(gFirstConnection)
        }).then(() => {
          return ensureAudioPlaying(gSecondConnection);
        }),
    ])
    .then(logSuccess);
  }

  function setupCallAndPromiseAudioPlaying(constraints) {
    createConnections(null);

    // Add the local stream to gFirstConnection to play one-way audio.
    return Promise.all([
      navigator.mediaDevices.getUserMedia(constraints)
          .then(addStreamToTheFirstConnectionAndNegotiate),
      waitForConnectionToStabilize(gFirstConnection)
          .then(() => { return ensureAudioPlaying(gSecondConnection); }),
    ]);
  }

  function callAndEnsureAudioIsPlaying(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints)
        .then(logSuccess);
  }

  function enableRemoteVideo(peerConnection, enabled) {
    remoteStream = peerConnection.getRemoteStreams()[0];
    remoteStream.getVideoTracks()[0].enabled = enabled;
  }

  function enableRemoteAudio(peerConnection, enabled) {
    remoteStream = peerConnection.getRemoteStreams()[0];
    remoteStream.getAudioTracks()[0].enabled = enabled;
  }

  function enableLocalVideo(peerConnection, enabled) {
    localStream = peerConnection.getLocalStreams()[0];
    localStream.getVideoTracks()[0].enabled = enabled;
  }

  function enableLocalAudio(peerConnection, enabled) {
    localStream = peerConnection.getLocalStreams()[0];
    localStream.getAudioTracks()[0].enabled = enabled;
  }

  function callAndEnsureRemoteAudioTrackMutingWorks(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints).then(() => {
      // Call is up, now mute the remote track and check we stop playing out
      // audio (after a small delay, we don't expect it to happen instantly).
      enableRemoteAudio(gSecondConnection, false);

      return new Promise(resolve => {
        setTimeout(resolve, 250);
      })
      .then(() => ensureSilence(gSecondConnection));
    })
    .then(logSuccess);
  }

  function callAndEnsureLocalAudioTrackMutingWorks(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints).then(() => {
      // Call is up, now mute the local track of the sending side and ensure
      // the receiving side stops receiving audio.
      enableLocalAudio(gFirstConnection, false);

      return new Promise(resolve => {
        setTimeout(resolve, 250);
      })
      .then(() => ensureSilence(gSecondConnection));
    })
    .then(logSuccess);
  }

  function callAndEnsureAudioTrackUnmutingWorks(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints).then(() => {
      // Mute, wait a while, unmute, verify audio gets back up.
      // (Also, ensure video muting doesn't affect audio).
      enableRemoteAudio(gSecondConnection, false);
      enableRemoteVideo(gSecondConnection, false);

      setTimeout(function() {
        enableRemoteAudio(gSecondConnection, true);
      }, 500);

      return new Promise(resolve => {
        setTimeout(resolve, 1500);
      })
      .then(() => ensureAudioPlaying(gSecondConnection));
    })
    .then(logSuccess);
  }

  function callAndEnsureLocalVideoMutingDoesntMuteAudio(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints).then(() => {
      enableLocalVideo(gFirstConnection, false);
      return ensureAudioPlaying(gSecondConnection)
          .then(logSuccess);
    });
  }

  function callAndEnsureRemoteVideoMutingDoesntMuteAudio(constraints) {
    return setupCallAndPromiseAudioPlaying(constraints).then(() => {
      enableRemoteVideo(gSecondConnection, false);
      return ensureAudioPlaying(gSecondConnection)
          .then(logSuccess);
    });
  }

  // TODO(crbug.com/40637961): This test is a temporary replacement for:
  // external/wpt/webrtc/RTCRtpReceiver-getSynchronizationSources.https.html
  async function testEstablishAudioOnlyCallAndVerifyGetSynchronizationSourcesWorks() {
    const startTime = performance.timeOrigin + performance.now();

    await setupCallAndPromiseAudioPlaying({audio: true});

    const peerConnection = gSecondConnection;

    const receivers = peerConnection.getReceivers();
    assertEquals(receivers.length, 1);
    const receiver = receivers[0];
    assertEquals(receiver.track.kind, 'audio');

    const results = receiver.getSynchronizationSources();
    assertEquals(results.length, 1);
    const result = results[0];

    const endTime = performance.timeOrigin + performance.now();

    console.log('getSynchronizationSources() = ' + JSON.stringify(result));

    // timestamp
    assertEquals(typeof result.timestamp, 'number');
    assertTrue(result.timestamp >= startTime);
    assertTrue(result.timestamp <= endTime);

    // source
    assertEquals(typeof result.source, 'number');
    assertTrue(result.source >= 0);
    assertTrue(result.source <= 0xffffffff);

    // rtpTimestamp
    assertEquals(typeof result.rtpTimestamp, 'number');
    assertTrue(result.rtpTimestamp >= 0);
    assertTrue(result.rtpTimestamp <= 0xffffffff);

    // audioLevel
    assertEquals(typeof result.audioLevel, 'number');
    assertTrue(result.audioLevel >= 0);
    assertTrue(result.audioLevel <= 1);

    // voiceActivityFlag
    if (result.voiceActivityFlag != undefined) {
      assertEquals(typeof result.voiceActivityFlag, 'boolean');
    }

    return logSuccess();
  }

  function createConnections(constraints) {
    gFirstConnection = createConnection(constraints, 'remote-view-1');
    assertEquals('stable', gFirstConnection.signalingState);

    gSecondConnection = createConnection(constraints, 'remote-view-2');
    assertEquals('stable', gSecondConnection.signalingState);
  }

  function createConnection(constraints, remoteView) {
    var pc = new RTCPeerConnection(null, constraints);
    pc.onaddstream = function(event) {
      onRemoteStream(event, remoteView);
    }
    return pc;
  }

  function displayAndRemember(localStream) {
    $('local-view').srcObject = localStream;

    gLocalStream = localStream;
  }

  // Called if getUserMedia succeeds and we want to send from both connections.
  function addStreamToBothConnectionsAndNegotiate(localStream) {
    displayAndRemember(localStream);
    gFirstConnection.addStream(localStream);
    gSecondConnection.addStream(localStream);
    negotiate();
  }

  // Called if getUserMedia succeeds when we want to send from one connection.
  function addStreamToTheFirstConnectionAndNegotiate(localStream) {
    displayAndRemember(localStream);
    gFirstConnection.addStream(localStream);
    negotiate();
  }

  function negotiate() {
    negotiateBetween(gFirstConnection, gSecondConnection);
  }

  function onRemoteStream(e, target) {
    console.log("Receiving remote stream...");
    gRemoteStreams[target] = e.stream;
    var remoteVideo = $(target);
    remoteVideo.srcObject = e.stream;
  }

  </script>
</head>
<body>
  <table border="0">
    <tr>
      <td><video width="320" height="240" id="local-view" style="display:none"
          autoplay muted></video></td>
      <td><video width="320" height="240" id="remote-view-1"
          style="display:none" autoplay></video></td>
      <td><video width="320" height="240" id="remote-view-2"
          style="display:none" autoplay></video></td>
      <!-- Canvases are named after their corresponding video elements. -->
      <td><canvas width="320" height="240" id="remote-view-1-canvas"
          style="display:none"></canvas></td>
      <td><canvas width="320" height="240" id="remote-view-2-canvas"
          style="display:none"></canvas></td>
    </tr>
  </table>
</body>
</html>