summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc/RTCRtpTransceiver-stopping.https.html
blob: e085be6d3cfff43a2366838bcf076c6b3bacf1d3 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!doctype html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';

['audio', 'video'].forEach((kind) => {
  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const transceiver = pc1.addTransceiver(kind);

    // Complete O/A exchange such that the transceiver gets associated.
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    await pc2.setLocalDescription();
    await pc1.setRemoteDescription(pc2.localDescription);
    assert_not_equals(transceiver.mid, null, 'mid before stop()');
    assert_not_equals(transceiver.direction, 'stopped',
                      'direction before stop()');
    assert_not_equals(transceiver.currentDirection, 'stopped',
                      'currentDirection before stop()');

    // Stop makes it stopping, but not stopped.
    transceiver.stop();
    assert_not_equals(transceiver.mid, null, 'mid after stop()');
    assert_equals(transceiver.direction, 'stopped', 'direction after stop()');
    assert_not_equals(transceiver.currentDirection, 'stopped',
                      'currentDirection after stop()');

    // Negotiating makes it stopped.
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    await pc2.setLocalDescription();
    await pc1.setRemoteDescription(pc2.localDescription);
    assert_equals(transceiver.mid, null, 'mid after negotiation');
    assert_equals(transceiver.direction, 'stopped',
                  'direction after negotiation');
    assert_equals(transceiver.currentDirection, 'stopped',
                  'currentDirection after negotiation');
  }, `[${kind}] Locally stopped transceiver goes from stopping to stopped`);

  promise_test(async t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());

    const transceiver = pc.addTransceiver(kind);
    const trackEnded = new Promise(r => transceiver.receiver.track.onended = r);
    assert_equals(transceiver.receiver.track.readyState, 'live');
    transceiver.stop();
    // Stopping triggers ending the track, but this happens asynchronously.
    assert_equals(transceiver.receiver.track.readyState, 'live');
    await trackEnded;
    assert_equals(transceiver.receiver.track.readyState, 'ended');
  }, `[${kind}] Locally stopping a transceiver ends the track`);

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const pc1Transceiver = pc1.addTransceiver(kind);
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    await pc2.setLocalDescription();
    await pc1.setRemoteDescription(pc2.localDescription);
    const [pc2Transceiver] = pc2.getTransceivers();

    pc1Transceiver.stop();

    await pc1.setLocalDescription();
    assert_equals(pc2Transceiver.receiver.track.readyState, 'live');
    // Applying the remote offer immediately ends the track, we don't need to
    // create or apply an answer.
    await pc2.setRemoteDescription(pc1.localDescription);
    // sRD just resolved, so we're in the success task for sRD. The transition
    // from live -> ended is queued right now.
    assert_equals(pc2Transceiver.receiver.track.readyState, 'live');
    await new Promise(r => pc2Transceiver.receiver.track.onended = r);
    assert_equals(pc2Transceiver.receiver.track.readyState, 'ended');
  }, `[${kind}] Remotely stopping a transceiver ends the track`);

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const pc1Transceiver = pc1.addTransceiver(kind);

    // Complete O/A exchange such that the transceiver gets associated.
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    await pc2.setLocalDescription();
    await pc1.setRemoteDescription(pc2.localDescription);
    const [pc2Transceiver] = pc2.getTransceivers();
    assert_not_equals(pc2Transceiver.mid, null, 'mid before stop()');
    assert_not_equals(pc2Transceiver.direction, 'stopped',
                      'direction before stop()');
    assert_not_equals(pc2Transceiver.currentDirection, 'stopped',
                      'currentDirection before stop()');

    // Make the remote transceiver stopped.
    pc1Transceiver.stop();

    // Negotiating makes it stopped.
    assert_equals(pc2.getTransceivers().length, 1);
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    // As soon as the remote offer is set, the transceiver is stopped but it is
    // not disassociated or removed until setting the local answer.
    assert_equals(pc2.getTransceivers().length, 1);
    assert_not_equals(pc2Transceiver.mid, null, 'mid during negotiation');
    assert_equals(pc2Transceiver.direction, 'stopped',
                  'direction during negotiation');
    assert_equals(pc2Transceiver.currentDirection, 'stopped',
                  'currentDirection during negotiation');
    await pc2.setLocalDescription();
    assert_equals(pc2.getTransceivers().length, 0);
    assert_equals(pc2Transceiver.mid, null, 'mid after negotiation');
    assert_equals(pc2Transceiver.direction, 'stopped',
                  'direction after negotiation');
    assert_equals(pc2Transceiver.currentDirection, 'stopped',
                  'currentDirection after negotiation');
  }, `[${kind}] Remotely stopped transceiver goes directly to stopped`);

  promise_test(async t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());

    const transceiver = pc.addTransceiver(kind);

    // Rollback does not end the track, because the transceiver is not removed.
    await pc.setLocalDescription();
    await pc.setLocalDescription({type:'rollback'});
    assert_equals(transceiver.receiver.track.readyState, 'live');
  }, `[${kind}] Rollback when transceiver is not removed does not end track`);

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const pc1Transceiver = pc1.addTransceiver(kind);

    // Start negotiation, causing a transceiver to be created.
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    const [pc2Transceiver] = pc2.getTransceivers();

    // Rollback such that the transceiver is removed.
    await pc2.setRemoteDescription({type:'rollback'});
    assert_equals(pc2.getTransceivers().length, 0);
    // sRD just resolved, so we're in the success task for sRD. The transition
    // from live -> ended is queued right now.
    assert_equals(pc2Transceiver.receiver.track.readyState, 'live');
    await new Promise(r => pc2Transceiver.receiver.track.onended = r);
    assert_equals(pc2Transceiver.receiver.track.readyState, 'ended');
  }, `[${kind}] Rollback when removing transceiver does end the track`);

  // Same test as above but looking at direction and currentDirection.
  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const pc1Transceiver = pc1.addTransceiver(kind);

    // Start negotiation, causing a transceiver to be created.
    await pc1.setLocalDescription();
    await pc2.setRemoteDescription(pc1.localDescription);
    const [pc2Transceiver] = pc2.getTransceivers();

    // Rollback such that the transceiver is removed.
    await pc2.setRemoteDescription({type:'rollback'});
    assert_equals(pc2.getTransceivers().length, 0);
    // The removed transceiver is stopped.
    assert_equals(pc2Transceiver.currentDirection, 'stopped',
                  'currentDirection indicate stopped');
    // A stopped transceiver is necessarily also stopping.
    assert_equals(pc2Transceiver.direction, 'stopped',
                  'direction indicate stopping');
    // A stopped transceiver has no mid.
    assert_equals(pc2Transceiver.mid, null, 'not associated');
  }, `[${kind}] Rollback when removing transceiver makes it stopped`);

  promise_test(async t => {
    const pc1 = new RTCPeerConnection();
    t.add_cleanup(() => pc1.close());
    const pc2 = new RTCPeerConnection();
    t.add_cleanup(() => pc2.close());

    const constraints = {};
    constraints[kind] = true;
    const stream = await navigator.mediaDevices.getUserMedia(constraints);
    const [track] = stream.getTracks();

    pc1.addTrack(track);
    pc2.addTrack(track);
    const transceiver = pc2.getTransceivers()[0];

    const ontrackEvent = new Promise(r => {
      pc2.ontrack = e => r(e.track);
    });

    // Simulate glare: both peer connections set local offers.
    await pc1.setLocalDescription();
    await pc2.setLocalDescription();
    // Set remote offer, which implicitly rolls back the local offer. Because
    // `transceiver` is an addTrack-transceiver, it should get repurposed.
    await pc2.setRemoteDescription(pc1.localDescription);
    assert_equals(transceiver.receiver.track.readyState, 'live');
    // Sanity check: the track should still be live when ontrack fires.
    assert_equals((await ontrackEvent).readyState, 'live');
  }, `[${kind}] Glare when transceiver is not removed does not end track`);
});
</script>