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
|
<!DOCTYPE html>
<html>
<head>
<title>MediaStream Insertable Streams - VideoTrackGenerator</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webrtc/RTCPeerConnection-helper.js"></script>
</head>
<body>
<h1 class="instructions">Description</h1>
<p class="instructions">This test checks that generating video MediaStreamTracks from VideoTrackGenerator works as expected.</p>
<script id="scriptRoutines">
const pixelColour = [50, 100, 150, 255];
const height = 240;
const width = 320;
function makeVideoFrame(timestamp) {
const canvas = new OffscreenCanvas(width, height);
const ctx = canvas.getContext('2d', {alpha: false});
ctx.fillStyle = `rgba(${pixelColour.join()})`;
ctx.fillRect(0, 0, width, height);
return new VideoFrame(canvas, {timestamp, alpha: 'discard'});
}
async function getVideoFrame() {
const stream = await getNoiseStream({video: true});
const input_track = stream.getTracks()[0];
const processor = new MediaStreamTrackProcessor(input_track);
const reader = processor.readable.getReader();
const result = await reader.read();
input_track.stop();
return result.value;
}
function assertPixel(t, bytes, expected, epsilon = 5) {
for (let i = 0; i < bytes.length; i++) {
t.step(() => {
assert_less_than(Math.abs(bytes[i] - expected[i]), epsilon, "Mismatched pixel");
});
}
}
async function initiateSingleTrackCall(t, track, output) {
const caller = new RTCPeerConnection();
t.add_cleanup(() => caller.close());
const callee = new RTCPeerConnection();
t.add_cleanup(() => callee.close());
caller.addTrack(track);
t.add_cleanup(() => track.stop());
exchangeIceCandidates(caller, callee);
// Wait for the first track.
const e = await exchangeOfferAndListenToOntrack(t, caller, callee);
output.srcObject = new MediaStream([e.track]);
// Exchange answer.
await exchangeAnswer(caller, callee);
await waitForConnectionStateChange(callee, ['connected']);
}
</script>
<script>
async function createWorker(script) {
script = scriptRoutines.text + script + "self.postMessage('ready');";
const blob = new Blob([script], { type: 'text/javascript' });
const url = URL.createObjectURL(blob);
const worker = new Worker(url);
try {
await new Promise((resolve, reject) => {
worker.onmessage = resolve;
worker.onerror = (err) => reject(err.message);
});
return worker;
} finally {
URL.revokeObjectURL(url);
}
}
promise_test(async t => {
const worker = await createWorker(`
const generator = new VideoTrackGenerator();
const videoFrame = makeVideoFrame(1);
const originalWidth = videoFrame.displayWidth;
const originalHeight = videoFrame.displayHeight;
self.onmessage = async (event) => {
if (event.data == "transfer") {
self.postMessage({ track: generator.track, originalWidth, originalHeight }, [generator.track]);
return;
}
if (event.data == "write frame") {
generator.writable.getWriter().write(videoFrame)
return;
}
if (event.data == "cleanup") {
videoFrame.close();
return;
}
}
`);
t.add_cleanup(() => worker.postMessage("cleanup"));
worker.postMessage("transfer");
const { track, originalWidth, originalHeight } = await new Promise(resolve => worker.onmessage = e => resolve(e.data));
t.add_cleanup(() => track.stop());
const video = document.createElement("video");
video.autoplay = true;
video.width = 320;
video.height = 240;
video.srcObject = new MediaStream([track]);
video.play();
// Wait for the video element to be connected to the generator and
// generate the frame.
video.onloadstart = () => worker.postMessage("write frame");
return new Promise((resolve)=> {
video.ontimeupdate = t.step_func(() => {
const canvas = document.createElement("canvas");
canvas.width = originalWidth;
canvas.height = originalHeight;
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0);
// Pick a pixel in the centre of the video and check that it has the colour of the frame provided.
const pixel = context.getImageData(originalWidth/2, originalHeight/2, 1, 1);
assertPixel(t, pixel.data, pixelColour);
resolve();
});
});
}, 'Tests that frames are actually rendered correctly in a stream used for a video element.');
promise_test(async t => {
const worker = await createWorker(`
const generator = new VideoTrackGenerator();
const videoFrame = makeVideoFrame(1);
const originalWidth = videoFrame.displayWidth;
const originalHeight = videoFrame.displayHeight;
let intervalId;
self.onmessage = async (event) => {
if (event.data == "transfer") {
self.postMessage({ track: generator.track}, [generator.track]);
// Write frames for the duration of the test.
const writer = generator.writable.getWriter();
let timestamp = 0;
intervalId = setInterval(async () => {
timestamp++;
await writer.write(makeVideoFrame(timestamp));
}, 40);
return;
}
}
`);
worker.postMessage("transfer");
const { track } = await new Promise(resolve => worker.onmessage = e => resolve(e.data));
t.add_cleanup(() => track.stop());
const video = document.createElement('video');
video.autoplay = true;
video.width = width;
video.height = height;
video.muted = true;
await initiateSingleTrackCall(t, track, video);
return new Promise(resolve => {
video.requestVideoFrameCallback(t.step_func(() => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0);
// Pick a pixel in the centre of the video and check that it has the
// colour of the frame provided.
const pixel = context.getImageData(width / 2, height / 2, 1, 1);
// Encoding/decoding can add noise, so increase the threshhold to 8.
assertPixel(t, pixel.data, pixelColour, 8);
resolve();
}));
});
}, 'Tests that frames are actually rendered correctly in a stream sent over a peer connection.');
promise_test(async t => {
const colorUL = [255, 0, 0, 255];
const colorUR = [255, 255, 0, 255];
const colorLL = [0, 255, 0, 255];
const colorLR = [0, 255, 255, 255];
const worker = await createWorker(`
const generator = new VideoTrackGenerator();
const videoFrame = makeVideoFrame(1);
const originalWidth = videoFrame.displayWidth;
const originalHeight = videoFrame.displayHeight;
let intervalId;
self.onmessage = async (event) => {
if (event.data == "transfer") {
self.postMessage({ track: generator.track}, [generator.track]);
const inputCanvas = new OffscreenCanvas(width, height);
const inputContext = inputCanvas.getContext('2d', {alpha: false});
// draw four quadrants
inputContext.fillStyle = \`rgba(${colorUL.join()})\`;
inputContext.fillRect(0, 0, width / 2, height / 2);
inputContext.fillStyle = \`rgba(${colorUR.join()})\`;
inputContext.fillRect(width / 2, 0, width / 2, height / 2);
inputContext.fillStyle = \`rgba(${colorLL.join()})\`;
inputContext.fillRect(0, height / 2, width / 2, height / 2);
inputContext.fillStyle = \`rgba(${colorLR.join()})\`;
inputContext.fillRect(width / 2, height / 2, width / 2, height / 2);
// Write frames for the duration of the test.
const writer = generator.writable.getWriter();
let timestamp = 0;
const intervalId = setInterval(async () => {
timestamp++;
await writer.write(new VideoFrame(inputCanvas, {timestamp: timestamp, alpha: 'discard'}));
}, 40);
return;
}
if (event.data.type === "getVideoFrame") {
const processor = new MediaStreamTrackProcessor({ track: event.data.track });
const reader = processor.readable.getReader();
const frame = (await reader.read()).value;
self.postMessage({frame}, [frame])
event.data.track.stop();
}
}
`);
worker.postMessage("transfer");
const { track } = await new Promise(resolve => worker.onmessage = e => resolve(e.data));
t.add_cleanup(() => track.stop());
const caller = new RTCPeerConnection();
t.add_cleanup(() => caller.close());
const callee = new RTCPeerConnection();
t.add_cleanup(() => callee.close());
const sender = caller.addTrack(track);
exchangeIceCandidates(caller, callee);
// Wait for the first track.
const e = await exchangeOfferAndListenToOntrack(t, caller, callee);
// Exchange answer.
await exchangeAnswer(caller, callee);
await waitForConnectionStateChange(callee, ['connected']);
const params = sender.getParameters();
params.encodings.forEach(e => e.scaleResolutionDownBy = 2);
sender.setParameters(params);
// The first frame may not have had scaleResolutionDownBy applied
const numTries = 5;
for (let i = 1; i <= numTries; i++) {
const clone = e.track.clone();
worker.postMessage({type:"getVideoFrame", track: clone}, [clone]);
const {frame: outputFrame} = await new Promise(resolve => worker.onmessage = e => resolve(e.data));
if (outputFrame.displayWidth !== width / 2) {
assert_less_than(i, numTries, `First ${numTries} frames were the wrong size.`);
outputFrame.close();
continue;
}
assert_equals(outputFrame.displayWidth, width / 2);
assert_equals(outputFrame.displayHeight, height / 2);
const outputCanvas = new OffscreenCanvas(width / 2, height / 2);
const outputContext = outputCanvas.getContext('2d', {alpha: false});
outputContext.drawImage(outputFrame, 0, 0);
outputFrame.close();
// Check the four quadrants
const pixelUL = outputContext.getImageData(width / 8, height / 8, 1, 1);
assertPixel(t, pixelUL.data, colorUL);
const pixelUR =
outputContext.getImageData(width * 3 / 8, height / 8, 1, 1);
assertPixel(t, pixelUR.data, colorUR);
const pixelLL =
outputContext.getImageData(width / 8, height * 3 / 8, 1, 1);
assertPixel(t, pixelLL.data, colorLL);
const pixelLR =
outputContext.getImageData(width * 3 / 8, height * 3 / 8, 1, 1);
assertPixel(t, pixelLR.data, colorLR);
break;
}
}, 'Tests that frames are sent correctly with RTCRtpEncodingParameters.scaleResolutionDownBy.');
</script>
</body>
</html>
|