summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_nonDefaultRate.html
blob: ad9414cef24ce9051aa728176e854b0688316e59 (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({ title: "PeerConnection feed to a graph with non default rate", bug: "1387454" });
  /**
   * Run a test to verify that when we use the streem with nonDefault rate to/from a PC
   * the connection fails. (PC is always on default rate).
   */

  let pc1;
  let pc2;

  const offerOptions = {
    offerToReceiveAudio: 1,
  };

  function getName(pc) {
    return (pc === pc1) ? 'pc1' : 'pc2';
  }

  function getOtherPc(pc) {
    return (pc === pc1) ? pc2 : pc1;
  }

  function onAddIceCandidateSuccess(pc) {
    ok(true, getName(pc) + ' addIceCandidate success');
  }

  function onAddIceCandidateError(pc, error) {
    ok(false, getName(pc) + ' failed to add ICE Candidate: ' + error.toString());
  }

  function onIceCandidate(pc, event, done) {
    if (!event.candidate) {
      ok(pc.iceGatheringState === 'complete', getName(pc) + " ICE gathering state has reached complete");
      done();
      return;
    }
    getOtherPc(pc).addIceCandidate(event.candidate)
    .then(() => {
        onAddIceCandidateSuccess(pc);
      },
      (err) => {
        onAddIceCandidateError(pc, err);
      });
    info(getName(pc) + ' ICE candidate: ' + event.candidate.candidate);
  }

  function onIceStateChange(pc, event) {
    if (pc) {
      info(getName(pc) + ' ICE state: ' + pc.iceConnectionState);
      info('ICE state change event: ', event);
    }
  }

  function onCreateOfferSuccess(desc) {
    info('Offer from pc1\n' + desc.sdp);
    info('pc1 setLocalDescription start');

    pc1.setLocalDescription(desc)
    .then(() => {
      onSetLocalSuccess(pc1);
    },
    onSetSessionDescriptionError);

    info('pc2 setRemoteDescription start');
    pc2.setRemoteDescription(desc).then(() => {
      onSetRemoteSuccess(pc2);
    },
    onSetSessionDescriptionError);

    info('pc2 createAnswer start');

    // Since the 'remote' side has no media stream we need
    // to pass in the right constraints in order for it to
    // accept the incoming offer of audio and video.
    pc2.createAnswer()
    .then(onCreateAnswerSuccess, onCreateSessionDescriptionError);
  }

  function onSetSessionDescriptionError(error) {
    ok(false, 'Failed to set session description: ' + error.toString());
  }

  function onSetLocalSuccess(pc) {
    ok(true, getName(pc) + ' setLocalDescription complete');
  }

  function onCreateSessionDescriptionError(error) {
    ok(false, 'Failed to create session description: ' + error.toString());
  }

  function onSetRemoteSuccess(pc) {
    ok(true, getName(pc) + ' setRemoteDescription complete');
  }

  function onCreateAnswerSuccess(desc) {
    info('Answer from pc2:\n' + desc.sdp);
    info('pc2 setLocalDescription start');
    pc2.setLocalDescription(desc).then(() => {
      onSetLocalSuccess(pc2);
    },
    onSetSessionDescriptionError);
    info('pc1 setRemoteDescription start');
    pc1.setRemoteDescription(desc).then(() => {
        onSetRemoteSuccess(pc1);
    },
    onSetSessionDescriptionError);
  }

  async function getRemoteStream(localStream) {
    info("got local stream")
    const audioTracks = localStream.getAudioTracks();

    const servers = null;

    pc1 = new RTCPeerConnection(servers);
    info('Created local peer connection object pc1');
    const iceComplete1 = new Promise((resolve, reject) => {
      pc1.onicecandidate = (e) => {
        onIceCandidate(pc1, e, resolve);
      };
    });

    pc2 = new RTCPeerConnection(servers);
    info('Created remote peer connection object pc2');
    const iceComplete2 = new Promise((resolve, reject) => {
      pc2.onicecandidate = (e) => {
        onIceCandidate(pc2, e, resolve);
      };
    });

    pc1.oniceconnectionstatechange = (e) => {
      onIceStateChange(pc1, e);
    };
    pc2.oniceconnectionstatechange = (e) => {
      onIceStateChange(pc2, e);
    };

    const remoteStreamPromise = new Promise((resolve, reject) => {
      pc2.ontrack = (e) => {
        info('pc2 received remote stream ' + e.streams[0]);
        resolve(e.streams[0]);
      };
    });

    localStream.getTracks().forEach((track) => {
      pc1.addTrack(track, localStream);
    });
    info('Added local stream to pc1');

    info('pc1 createOffer start');
    pc1.createOffer(offerOptions)
      .then(onCreateOfferSuccess,onCreateSessionDescriptionError);

    let promise_arr = await Promise.all([remoteStreamPromise, iceComplete1, iceComplete2]);
    return promise_arr[0];
  }

  runTest(async () => {
    // Local stream operates at non default rate (32000)
    const nonDefaultRate = 32000;
    const nonDefault_ctx = new AudioContext({sampleRate: nonDefaultRate});
    oscillator = nonDefault_ctx.createOscillator();
    const dest = nonDefault_ctx.createMediaStreamDestination();
    oscillator.connect(dest);
    oscillator.start();

    // Wait for remote stream
    const remoteStream = await getRemoteStream(dest.stream)
    ok(true, 'Got remote stream ' + remoteStream);

    // remoteStream now comes from PC so operates at default
    // rates. Verify that by adding to a default context
    const ac = new AudioContext;
    const source_default_rate = ac.createMediaStreamSource(remoteStream);

    // Now try to add the remoteStream on a non default context
    mustThrowWith(
      "Connect stream with graph of different sample rate",
      "NotSupportedError", () => {
        nonDefault_ctx.createMediaStreamSource(remoteStream);
      }
    );

    // Close peer connections to make sure we don't get error:
    // "logged result after SimpleTest.finish(): pc1 addIceCandidate success"
    // See Bug 1626814.
    pc1.close();
    pc2.close();
  });
</script>
</pre>
</body>
</html>