Skip to content

Instantly share code, notes, and snippets.

@aliakseis
Last active November 4, 2022 13:28
Show Gist options
  • Save aliakseis/44ffe552175b3d2f6575f66407369580 to your computer and use it in GitHub Desktop.
Save aliakseis/44ffe552175b3d2f6575f66407369580 to your computer and use it in GitHub Desktop.
This is a copy/paste example streaming the webcam from the mobile browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>libdatachannel media receiver example</title>
</head>
<body>
<p>Please enter the offer provided to you by the receiver application: </p>
<textarea cols="80" rows="25"></textarea>
<button>Submit</button>
<p/>
<span id="iceconnectionstate"> none </span>
<script>
document.querySelector('button').addEventListener('click', async () => {
const offer = JSON.parse(document.querySelector('textarea').value);
const pc = new RTCPeerConnection({
// Recommended for libdatachannel
bundlePolicy: 'max-bundle',
});
pc.onicegatheringstatechange = (state) => {
if (pc.iceGatheringState === 'complete') {
// We only want to provide an answer once all of our candidates have been added to the SDP.
const answer = pc.localDescription;
document.querySelector('textarea').value = JSON.stringify({"type": answer.type, sdp: answer.sdp});
document.querySelector('p').value = 'Please paste the answer in the receiver application.';
alert('Please paste the answer in the receiver application.');
}
}
pc.addEventListener('iceconnectionstatechange', function(e) {
console.log('ice state change', pc.iceConnectionState);
document.getElementById("iceconnectionstate").textContent=pc.iceConnectionState + " " + new Date().toLocaleString();
});
await pc.setRemoteDescription(offer);
const media = await navigator.mediaDevices.getUserMedia({
video: {
width: 1280,
height: 720
}
});
media.getTracks().forEach(track => pc.addTrack(track, media));
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
try {
wakeLock = await navigator.wakeLock.request('screen');
wakeLock.addEventListener('release', () => {
console.log('Screen Wake Lock was released');
});
console.log('Screen Wake Lock is active');
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
})
</script>
</body>
</html>
@aliakseis
Copy link
Author

aliakseis commented Oct 19, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment