Files
plugin-erwscascade/ui/subscribe.html
2024-03-17 22:55:02 +08:00

113 lines
3.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试WebRTC拉流</title>
<style>
</style>
</head>
<body>
<video id="video" width="640" height="480" autoplay muted controls>
</video>
<!-- <button id="sw" onclick="action()" type="button" style="width:100px;height:30px;display: block;">unpublish</button> -->
<pre>
<div style="display: flex;" style="width: 100vw;">
<div style="width: 50vw; padding-left: 20px; border-left: 1px solid red;">
<text>local</text>
<code id="localSdp">
</code>
</div>
<div style="width: 50vw; padding-left: 20px; border-left: 1px solid red;">
<text>remote</text>
<code id="remoteSdp" >
</code>
</div>
</div>
</pre>
</body>
<script>
let action = () => { console.log('action not set'); };
(async () => {
const pc = new RTCPeerConnection({
bundlePolicy: "max-bundle" ,
iceServers: [
// {
// //urls: 'stun:stun.l.google.com:19302'
// urls: "stun:erp.jshwx.com.cn:3478",
// },
// {
// urls: "turn:erp.jshwx.com.cn:3478",
// credential: "ert123",
// username: "ert"
// }
{
urls: "stun:stream.jshwx.com.cn:3478",
},
{
urls: "turn:stream.jshwx.com.cn:3478",
credential: "hwx",
username: "jshwx123"
}
]
});
pc.ontrack = (e) => {
console.log('ontrack', e);
if (e.streams.length === 0) return;
document.getElementById('video').srcObject = e.streams[0];
document.getElementById('video').play();
};
pc.oniceconnectionstatechange = () => {
console.log('oniceconnectionstatechange', pc.iceConnectionState);
};
pc.onicecandidate = (e) => {
console.log('onicecandidate', e.candidate);
};
pc.addTransceiver('video', { direction: 'recvonly' });
pc.addTransceiver('audio', { direction: 'recvonly' });
// const dc = pc.createDataChannel('sdp');
const offer = await pc.createOffer();
const localSdp = offer.sdp;
document.getElementById('localSdp').innerText = localSdp;
await pc.setLocalDescription(offer);
const searchParams = new URLSearchParams(location.search);
const streamPath = searchParams.get('streamPath') || 'live/webrtc';
searchParams.delete('streamPath')
if (searchParams.has("cid")){
searchParams.delete("cid")
}
const result = await fetch(
`/erwscascade/proxy/webrtc/play/${streamPath}${searchParams.lenght?`?${searchParams.toString()}`:''}`,
{
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'include',
redirect: 'follow',
referrerPolicy: 'no-referrer',
headers: { 'Content-Type': 'application/sdp' },
body: offer.sdp,
},
);
const remoteSdp = await result.text();
document.getElementById('remoteSdp').innerText = remoteSdp;
await pc.setRemoteDescription(
new RTCSessionDescription({ type: 'answer', sdp: remoteSdp }),
);
})()
</script>
</html>