File: RTCRtpEncodingParameters-scaleResolutionDownTo.https.html

package info (click to toggle)
firefox 145.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,344 kB
  • sloc: cpp: 7,594,932; javascript: 6,459,612; ansic: 3,752,905; python: 1,403,433; xml: 629,811; asm: 438,677; java: 186,421; sh: 67,287; makefile: 19,169; objc: 13,086; perl: 12,982; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (392 lines) | stat: -rw-r--r-- 14,065 bytes parent folder | download | duplicates (12)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!DOCTYPE html>
<meta charset="utf-8">
<title>RTCRtpEncodingParameters scaleResolutionDownTo attribute</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../webrtc/simulcast/simulcast.js"></script>
<script src="../webrtc/third_party/sdp/sdp.js"></script>
<script>
'use strict';

// Creates a track that can be resized with `track.resize(w,h)`.
function createResizableTrack(width, height) {
  // Draw to a canvas with a 30 fps interval.
  const canvas = Object.assign(
      document.createElement('canvas'), {width, height});
  const ctx = canvas.getContext('2d');
  ctx.fillStyle = 'rgb(255,0,0)';
  const interval = setInterval(() => {
    ctx.fillRect(0, 0, canvas.width, canvas.height);
  }, 1000 / 30);
  // Capture the canvas and add/modify reize() and stop() methods.
  const stream = canvas.captureStream();
  const [track] = stream.getTracks();
  track.resize = (w, h) => {
    canvas.width = w;
    canvas.height = h;
  };
  const nativeStop = track.stop;
  track.stop = () => {
    clearInterval(interval);
    nativeStop.apply(track);
  };
  return track;
}

async function getLastEncodedResolution(pc, rid = null) {
  const report = await pc.getStats();
  for (const stats of report.values()) {
    if (stats.type != 'outbound-rtp') {
      continue;
    }
    if (rid && stats.rid != rid) {
      continue;
    }
    if (!stats.frameWidth || !stats.frameWidth) {
      // The resolution is missing until the first frame has been encoded.
      break;
    }
    return { width: stats.frameWidth, height: stats.frameHeight };
  }
  return { width: null, height: null };
}

async function waitForFrameWithResolution(t, pc, width, height, rid = null) {
  let resolution;
  do {
    resolution = await getLastEncodedResolution(pc, rid);
    await new Promise(r => t.step_timeout(r, 50));
  } while (resolution.width != width || resolution.height != height);
}

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  assert_throws_dom('OperationError', () => {
    pc.addTransceiver(track, {
          sendEncodings:[{
              scaleResolutionDownTo: { maxWidth: 0, maxHeight: 0 }
          }]
        });
  });
}, `addTransceiver: Invalid scaling throws`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc.addTransceiver(track, {sendEncodings:[{}]});

  const params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 0, maxHeight: 0 };
  const p = sender.setParameters(params);

  promise_rejects_dom(t, 'InvalidModificationError', p);
}, `setParameters: Invalid scaling throws`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  assert_throws_dom('OperationError', () => {
    pc.addTransceiver(track, {
          sendEncodings:[{
              scaleResolutionDownTo: undefined,
          }, {
              scaleResolutionDownTo: { maxWidth: 120, maxHeight: 60 }
          }]
        });
  });
}, `addTransceiver: Specifying scaling on a subset of active encodings throws`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  pc.addTransceiver(track, {
        sendEncodings:[{
            active: true,
            scaleResolutionDownTo: { maxWidth: 120, maxHeight: 60 },
        }, {
            active: false,
            scaleResolutionDownTo: undefined
        }]
      });
}, `addTransceiver: Specifying scaling on inactive encodings is optional`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc.addTransceiver(track, {sendEncodings:[{},{}]});

  const params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = undefined;
  params.encodings[1].scaleResolutionDownTo = { maxWidth: 120, maxHeight: 60 };
  const p = sender.setParameters(params);

  promise_rejects_dom(t, 'InvalidModificationError', p);
}, `setParameters: Specifying scaling on a subset of active encodings throws`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc.addTransceiver(track, {sendEncodings:[{},{}]});

  const params = sender.getParameters();
  params.encodings[0].active = true;
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 120, maxHeight: 60 };
  params.encodings[1].active = false;
  params.encodings[1].scaleResolutionDownTo = undefined;
  await sender.setParameters(params);
}, `setParameters: Specifying scaling on inactive encodings is optional`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());

  let {sender} = pc1.addTransceiver(track, {
        sendEncodings:[{
            scaleResolutionDownTo: { maxWidth: 120, maxHeight: 60 }
        }]
      });

  // Get the initial value set.
  let params = sender.getParameters();
  assert_equals(params.encodings[0].scaleResolutionDownTo?.maxWidth, 120);
  assert_equals(params.encodings[0].scaleResolutionDownTo?.maxHeight, 60);
  // Set parameters and get the result.
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 60, maxHeight: 30 };
  await sender.setParameters(params);
  params = sender.getParameters();
  assert_equals(params.encodings[0].scaleResolutionDownTo?.maxWidth, 60);
  assert_equals(params.encodings[0].scaleResolutionDownTo?.maxHeight, 30);
}, `getParameters reflect the current scaleResolutionDownTo`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());

  pc1.addTransceiver(track, {
        sendEncodings:[{
            scaleResolutionDownBy: 2.0,
            scaleResolutionDownTo: { maxWidth: 120, maxHeight: 60 }
        }]
      });

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  await waitForFrameWithResolution(t, pc1, 120, 60);
}, `addTransceiver: scaleResolutionDownBy is ignored when ` +
   `scaleResolutionDownTo is specified`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track);

  const params = sender.getParameters();
  params.encodings[0].scaleResolutionDownBy = 2.0;
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 120, maxHeight: 60 };
  const p = sender.setParameters(params);

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  await waitForFrameWithResolution(t, pc1, 120, 60);
}, `setParameters: scaleResolutionDownBy is ignored when ` +
   `scaleResolutionDownTo is specified`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track, {
      sendEncodings: [{
          scaleResolutionDownTo: { maxWidth: 60, maxHeight: 30 }
      }]
  });

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  await waitForFrameWithResolution(t, pc1, 60, 30);
}, `addTransceiver: scaleResolutionDownTo with half resolution`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track);

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  // Request full resolution.
  let params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 120, maxHeight: 60 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 120, 60);

  // Request half resolution.
  params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 60, maxHeight: 30 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 60, 30);

  // Request full resolution again.
  params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 120, maxHeight: 60 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 120, 60);
}, `setParameters: Modify scaleResolutionDownTo while sending`);

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

  const track = createResizableTrack(80, 40);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track);

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  // scaleTo is portrait, track is landscape, but no scaling should happen due
  // to orientation agnosticism.
  let params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 40, maxHeight: 80 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 80, 40);

  // Change orientation of the track: still no downscale, but encoder begins to
  // produce the new orientation.
  track.resize(40, 80);
  await waitForFrameWithResolution(t, pc1, 40, 80);

  // scaleTo in landscape, reducing to half size. Verify track, which is
  // portrait, is scaled down by 2.
  params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 40, maxHeight: 20 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 20, 40);
}, `scaleResolutionDownTo is orientation agnostic`);

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

  const track = createResizableTrack(120, 60);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track);

  await pc1.setLocalDescription();
  await pc2.setRemoteDescription(pc1.localDescription);
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);

  // Restrict to 60x60. This results in 60x30 due to maintaining aspect ratio.
  let params = sender.getParameters();
  params.encodings[0].scaleResolutionDownTo = { maxWidth: 60, maxHeight: 60 };
  await sender.setParameters(params);
  await waitForFrameWithResolution(t, pc1, 60, 30);
}, `scaleResolutionDownTo does not change aspect ratio`);

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

  const track = createResizableTrack(160, 80);
  t.add_cleanup(() => track.stop());
  const {sender} = pc1.addTransceiver(track, {
      sendEncodings: [{
          rid: '0',
          scaleResolutionDownTo: { maxWidth: 40, maxHeight: 20 }
      }, {
          rid: '1',
          scaleResolutionDownTo: { maxWidth: 80, maxHeight: 40 }
      }, {
          rid: '2',
          scaleResolutionDownTo: { maxWidth: 160, maxHeight: 80 }
      }]
  });

  // Negotiate with simulcast tweaks.
  await doOfferToSendSimulcastAndAnswer(pc1, pc2, ['0', '1', '2']);

  await waitForFrameWithResolution(t, pc1, 40, 20, '0');
  await waitForFrameWithResolution(t, pc1, 80, 40, '1');
  await waitForFrameWithResolution(t, pc1, 160, 80, '2');
}, `scaleResolutionDownTo with simulcast`);
</script>