Skip to content

Commit

Permalink
Add WPT for applyConstraints() for remote video tracks.
Browse files Browse the repository at this point in the history
Bug: 899789
Change-Id: If7e01be191d8b01f630b69a5f25c2f0929b617fd
Reviewed-on: https://chromium-review.googlesource.com/c/1409199
Reviewed-by: Henrik Boström <[email protected]>
Commit-Queue: Marina Ciocea <[email protected]>
Cr-Commit-Position: refs/heads/master@{#622462}
  • Loading branch information
cdmarina authored and Commit Bot committed Jan 14, 2019
1 parent 0e9488a commit 4fa0871
Showing 1 changed file with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html>
<head>
<title>RTCPeerConnection-ontrack</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>

const FRAME_RATE_ERROR = 0.5;
const SETTINGS_CHANGE_RATE = 0.8;

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

const localStream = await navigator.mediaDevices.getUserMedia({video:true});
const [localTrack] = localStream.getTracks();
t.add_cleanup(() => localTrack.stop());
let remoteTrack = null;
let remoteStream = null;
localPc.addTrack(localTrack, localStream);
remotePc.ontrack = e => {
remoteTrack = e.track;
remoteStream = e.stream;
};

exchangeIceCandidates(localPc, remotePc);
const offer = await localPc.createOffer();
await localPc.setLocalDescription(offer);
await remotePc.setRemoteDescription(offer);
const answer = await remotePc.createAnswer();
await remotePc.setLocalDescription(answer);
await localPc.setRemoteDescription(answer);

// Get initial settings once remote side starts receiving frames.
const settings = await new Promise((resolve) => {
const checkFirstFrameSettings = settings => 'frameRate' in settings;
listenForFrame(remoteTrack, resolve, checkFirstFrameSettings);
});

const constraints = {
frameRate: settings.frameRate * SETTINGS_CHANGE_RATE,
width: settings.width * SETTINGS_CHANGE_RATE,
height: settings.height * SETTINGS_CHANGE_RATE
};
remoteTrack.applyConstraints(constraints);

// Wait for remote side to receive frames with constraints applied.
await new Promise((resolve) => {
const checkConstrainedSettings = settings =>
settings.frameRate - constraints.frameRate <= FRAME_RATE_ERROR &&
settings.width == constraints.width &&
settings.height == constraints.height;
listenForFrame(remoteTrack, resolve, checkConstrainedSettings);
});
}, 'applyConstraints for remote video track.');

function listenForFrame(track, resolve, checkSettings) {
const settings = track.getSettings();
if (checkSettings(settings))
resolve(settings);
setTimeout(() => listenForFrame(track, resolve, checkSettings), 0);
}

function exchangeIceCandidates(pc1, pc2) {
function doExchange(localPc, remotePc) {
localPc.addEventListener('icecandidate', event => {
const { candidate } = event;
if(candidate && remotePc.signalingState !== 'closed') {
remotePc.addIceCandidate(candidate);
}
});
}
doExchange(pc1, pc2);
doExchange(pc2, pc1);
}

</script>
</body>
</html>

0 comments on commit 4fa0871

Please sign in to comment.