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
|
<!doctype html>
<meta charset=utf-8>
<title>RTCRtpTransceiver.prototype.setCodecPreferences</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./third_party/sdp/sdp.js"></script>
<script>
'use strict';
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
transceiver.setCodecPreferences(capabilities.codecs);
}, `setCodecPreferences() on audio transceiver with codecs returned from RTCRtpReceiver.getCapabilities('audio') should succeed`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('video');
const capabilities = RTCRtpReceiver.getCapabilities('video');
transceiver.setCodecPreferences(capabilities.codecs);
}, `setCodecPreferences() on video transceiver with codecs returned from RTCRtpReceiver.getCapabilities('video') should succeed`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
transceiver.setCodecPreferences([]);
}, `setCodecPreferences([]) should succeed`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const { codecs } = capabilities;
if(codecs.length >= 2) {
const tmp = codecs[0];
codecs[0] = codecs[1];
codecs[1] = tmp;
}
transceiver.setCodecPreferences(codecs);
}, `setCodecPreferences() with reordered codecs should succeed`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('video');
const capabilities = RTCRtpReceiver.getCapabilities('video');
const { codecs } = capabilities;
// This test verifies that the mandatory VP8 codec is present
// and can be preferred.
const codec = codecs.find(c => c.mimeType === 'video/VP8');
assert_true(!!codec, 'VP8 video codec was found');
transceiver.setCodecPreferences([codec]);
}, `setCodecPreferences() with only VP8 should succeed`);
test(() => {
const pc = new RTCPeerConnection();
const transceiver = pc.addTransceiver('video');
const capabilities = RTCRtpReceiver.getCapabilities('video');
const { codecs } = capabilities;
// This test verifies that the mandatory H264 codec is present
// and can be preferred.
const codec = codecs.find(c => c.mimeType === 'video/H264');
assert_true(!!codec, 'H264 video codec was found');
transceiver.setCodecPreferences([codec]);
}, `setCodecPreferences() with only H264 should succeed`);
async function getRTPMapLinesWithCodecAsFirst(firstCodec)
{
const codecs = RTCRtpReceiver.getCapabilities('video').codecs;
codecs.forEach((codec, idx) => {
if (codec.mimeType === firstCodec) {
codecs.splice(idx, 1);
codecs.unshift(codec);
}
});
const pc = new RTCPeerConnection();
const transceiver = pc.addTransceiver('video');
transceiver.setCodecPreferences(codecs);
const offer = await pc.createOffer();
return offer.sdp.split('\r\n').filter(line => line.startsWith('a=rtpmap:'));
}
promise_test(async () => {
const lines = await getRTPMapLinesWithCodecAsFirst('video/H264');
assert_greater_than(lines.length, 1);
assert_true(lines[0].indexOf('H264') !== -1, 'H264 should be the first codec');
}, `setCodecPreferences() should allow setting H264 as first codec`);
promise_test(async () => {
const lines = await getRTPMapLinesWithCodecAsFirst('video/VP8');
assert_greater_than(lines.length, 1);
assert_true(lines[0].indexOf('VP8') !== -1, 'VP8 should be the first codec');
}, `setCodecPreferences() should allow setting VP8 as first codec`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('video');
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(capabilities.codecs));
}, `setCodecPreferences() on audio transceiver with codecs returned from getCapabilities('video') should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const codecs = [{
mimeType: 'data',
clockRate: 2000,
channels: 2,
sdpFmtpLine: '0-15'
}];
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with user defined codec with invalid mimeType should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const codecs = [{
mimeType: 'audio/piepiper',
clockRate: 2000,
channels: 2,
sdpFmtpLine: '0-15'
}];
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with user defined codec should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const codecs = [
...capabilities.codecs,
{
mimeType: 'audio/piepiper',
clockRate: 2000,
channels: 2,
sdpFmtpLine: '0-15'
}];
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with user defined codec together with codecs returned from getCapabilities() should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const codecs = [capabilities.codecs[0]];
codecs[0].clockRate = codecs[0].clockRate / 2;
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with modified codec clock rate should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const codecs = [capabilities.codecs[0]];
codecs[0].channels = codecs[0].channels + 11;
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with modified codec channel count should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const codecs = [capabilities.codecs[0]];
codecs[0].sdpFmtpLine = "modifiedparameter=1";
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with modified codec parameters should throw InvalidModificationError`);
test((t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const capabilities = RTCRtpReceiver.getCapabilities('audio');
const { codecs } = capabilities;
assert_greater_than(codecs.length, 0,
'Expect at least one codec available');
const [ codec ] = codecs;
const { channels=2 } = codec;
codec.channels = channels+1;
assert_throws_dom('InvalidModificationError', () => transceiver.setCodecPreferences(codecs));
}, `setCodecPreferences() with modified codecs returned from getCapabilities() should throw InvalidModificationError`);
promise_test(async (t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('audio');
const {codecs} = RTCRtpReceiver.getCapabilities('audio');
// Reorder codecs, put PCMU/PCMA first.
let firstCodec;
let i;
for (i = 0; i < codecs.length; i++) {
const codec = codecs[i];
if (codec.mimeType === 'audio/PCMU' || codec.mimeType === 'audio/PCMA') {
codecs.splice(i, 1);
codecs.unshift(codec);
firstCodec = codec.mimeType.substr(6);
break;
}
}
assert_not_equals(firstCodec, undefined, 'PCMU or PCMA codec not found');
transceiver.setCodecPreferences(codecs);
const offer = await pc.createOffer();
const mediaSection = SDPUtils.getMediaSections(offer.sdp)[0];
const rtpParameters = SDPUtils.parseRtpParameters(mediaSection);
assert_equals(rtpParameters.codecs[0].name, firstCodec);
}, `setCodecPreferences() modifies the order of audio codecs in createOffer`);
promise_test(async (t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('video');
const {codecs} = RTCRtpReceiver.getCapabilities('video');
// Reorder codecs, swap H264 and VP8.
let vp8 = -1;
let h264 = -1;
let firstCodec;
let i;
for (i = 0; i < codecs.length; i++) {
const codec = codecs[i];
if (codec.mimeType === 'video/VP8' && vp8 === -1) {
vp8 = i;
if (h264 !== -1) {
codecs[vp8] = codecs[h264];
codecs[h264] = codec;
firstCodec = 'VP8';
break;
}
}
if (codec.mimeType === 'video/H264' && h264 === -1) {
h264 = i;
if (vp8 !== -1) {
codecs[h264] = codecs[vp8];
codecs[vp8] = codec;
firstCodec = 'H264';
break;
}
}
}
assert_not_equals(firstCodec, undefined, 'VP8 and H264 codecs not found');
transceiver.setCodecPreferences(codecs);
const offer = await pc.createOffer();
const mediaSection = SDPUtils.getMediaSections(offer.sdp)[0];
const rtpParameters = SDPUtils.parseRtpParameters(mediaSection);
assert_equals(rtpParameters.codecs[0].name, firstCodec);
}, `setCodecPreferences() modifies the order of video codecs in createOffer`);
['rtx', 'red', 'ulpfec'].forEach(resiliencyMechanism => {
promise_test(async (t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const transceiver = pc.addTransceiver('video');
const {codecs} = RTCRtpReceiver.getCapabilities('video');
const filteredCodecs = codecs.
filter(codec => codec.mimeType !== 'video/' + resiliencyMechanism);
assert_not_equals(codecs.length, filteredCodecs.length);
transceiver.setCodecPreferences(filteredCodecs);
const offer = await pc.createOffer();
const mediaSection = SDPUtils.getMediaSections(offer.sdp)[0];
const rtpParameters = SDPUtils.parseRtpParameters(mediaSection);
assert_equals(rtpParameters.codecs.find(codec => codec.name === resiliencyMechanism),
undefined);
}, `setCodecPreferences() can remove ${resiliencyMechanism}`);
});
// Tests the note removed as result of discussion in
// https://github.com/w3c/webrtc-pc/issues/2933
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('video');
const {codecs} = RTCRtpReceiver.getCapabilities('video');
const vp8 = codecs.find(codec => codec.mimeType === 'video/VP8');
const h264 = codecs.find(codec => codec.mimeType === 'video/H264');
const thirdCodec = codecs.find(codec => ['video/VP9', 'video/AV1'].includes(codec.mimeType));
assert_true(!!vp8);
assert_true(!!h264);
assert_true(!!thirdCodec);
transceiver.setCodecPreferences([vp8, thirdCodec]);
await pc1.setLocalDescription();
await pc2.setRemoteDescription(pc1.localDescription);
const transceiver2 = pc2.getTransceivers()[0];
transceiver2.setCodecPreferences([h264, thirdCodec, vp8]);
await pc2.setLocalDescription();
await pc1.setRemoteDescription(pc2.localDescription);
const mediaSection = SDPUtils.getMediaSections(pc2.localDescription.sdp)[0];
const rtpParameters = SDPUtils.parseRtpParameters(mediaSection);
// Order is determined by pc2 but H264 is not present.
assert_equals(rtpParameters.codecs.length, 2);
assert_equals(rtpParameters.codecs[0].name, thirdCodec.mimeType.substring(6));
assert_equals(rtpParameters.codecs[1].name, 'VP8');
}, `setCodecPreferences() filters on receiver and prefers receiver order`);
["audio", "video"].forEach(kind => promise_test(async (t) => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
const [codec] = RTCRtpReceiver.getCapabilities(kind).codecs;
codec.mimeType = codec.mimeType.toUpperCase();
const transceiver = pc.addTransceiver(kind);
transceiver.setCodecPreferences([codec]);
codec.mimeType = codec.mimeType.toLowerCase();
transceiver.setCodecPreferences([codec]);
}, `setCodecPreferences should accept ${kind} codecs regardless of mimeType case`));
</script>
|