Files
webrtc/examples/whip-whep/index.html
Stephan Rotolante a8c02b0879 Add examples/whip-whep
Create WHIP/WHEP example works with OBS or browser

Resolves #2499
2024-03-24 20:24:32 -04:00

87 lines
2.4 KiB
HTML

<html>
<!--
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
SPDX-License-Identifier: MIT
-->
<head>
<title>whip-whep</title>
</head>
<body>
<button onclick="window.doWHIP()">Publish</button>
<button onclick="window.doWHEP()">Subscribe</button>
<h3> Video </h3>
<video id="videoPlayer" autoplay muted controls style="width: 500"> </video>
<h3> ICE Connection States </h3>
<div id="iceConnectionStates"></div> <br />
</body>
<script>
let peerConnection = new RTCPeerConnection()
peerConnection.oniceconnectionstatechange = () => {
let el = document.createElement('p')
el.appendChild(document.createTextNode(peerConnection.iceConnectionState))
document.getElementById('iceConnectionStates').appendChild(el);
}
window.doWHEP = () => {
peerConnection.addTransceiver('video', { direction: 'recvonly' })
peerConnection.ontrack = function (event) {
document.getElementById('videoPlayer').srcObject = event.streams[0]
}
peerConnection.createOffer().then(offer => {
peerConnection.setLocalDescription(offer)
fetch(`/whep`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer none`,
'Content-Type': 'application/sdp'
}
}).then(r => r.text())
.then(answer => {
peerConnection.setRemoteDescription({
sdp: answer,
type: 'answer'
})
})
})
}
window.doWHIP = () => {
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(stream => {
document.getElementById('videoPlayer').srcObject = stream
stream.getTracks().forEach(track => peerConnection.addTrack(track, stream))
peerConnection.createOffer().then(offer => {
peerConnection.setLocalDescription(offer)
fetch(`/whip`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer none`,
'Content-Type': 'application/sdp'
}
}).then(r => r.text())
.then(answer => {
peerConnection.setRemoteDescription({
sdp: answer,
type: 'answer'
})
})
})
})
}
</script>
</html>