WHEP 拉取go2rtc demo

浏览: 13评论: 0
发布时间: 2025-12-08
<!DOCTYPE html>
<html>
<head><title>WHEP Player</title></head>
<body>
<video id="video" controls autoplay muted style="width: 800px;"></video>
<script>
   async function start() {
       // 1. 填入您的 go2rtc WHEP 地址
       const url = "http://100.96.222.23:1984/api/webrtc?src=m10";
 
       const video = document.getElementById('video');
       const pc = new RTCPeerConnection();
 
       // 2. 将 WebRTC 接收到的流绑定到 video 标签
       pc.ontrack = event => video.srcObject = event.streams[0];
       // 添加收发器,只接收视频
       pc.addTransceiver('video', { direction: 'recvonly' });
       pc.addTransceiver('audio', { direction: 'recvonly' });
 
       // 3. 创建 Offer 并发送 WHEP 请求
       const offer = await pc.createOffer();
       await pc.setLocalDescription(offer);
 
       const response = await fetch(url, {
           method: 'POST',
           body: offer.sdp,
           headers: { 'Content-Type': 'application/sdp' }
       });
 
       // 4. 接收 Server 返回的 Answer
       const answer = await response.text();
       await pc.setRemoteDescription({ type: 'answer', sdp: answer });
   }
   start();
</script>
</body>
</html>