tried setting up Webrtc RTP (not working)
This commit is contained in:
BIN
board-vision/frontend/.gitignore
(Stored with Git LFS)
vendored
BIN
board-vision/frontend/.gitignore
(Stored with Git LFS)
vendored
Binary file not shown.
@@ -1,14 +1,53 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
|
||||
let peerConnection: RTCPeerConnection;
|
||||
let videoElement: HTMLVideoElement;
|
||||
let mediaSource: MediaSource;
|
||||
|
||||
onMount(() => {
|
||||
mediaSource = new MediaSource();
|
||||
mediaSource.addSourceBuffer('video/mpeg');
|
||||
videoElement.src = URL.createObjectURL(mediaSource);
|
||||
})
|
||||
async function negotiateWebRTC(peerConn: RTCPeerConnection) {
|
||||
peerConn.addTransceiver('video', { direction: 'recvonly' });
|
||||
|
||||
const offer = await peerConn.createOffer();
|
||||
await peerConn.setLocalDescription(offer);
|
||||
|
||||
await new Promise((resolve) => {
|
||||
if(peerConn.iceGatheringState === 'complete') {
|
||||
resolve(null);
|
||||
} else {
|
||||
peerConn.addEventListener('icegatheringstatechange', () => {
|
||||
console.log(peerConn.iceGatheringState);
|
||||
if(peerConn.iceGatheringState === 'complete')
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const remoteOffer = await (await fetch('/api/webrtc-offer', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ offer })
|
||||
})).json();
|
||||
|
||||
await peerConn.setRemoteDescription(remoteOffer);
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
peerConnection = new RTCPeerConnection({
|
||||
iceServers: []
|
||||
});
|
||||
|
||||
peerConnection.addEventListener('track', (event) => {
|
||||
console.log('track', event);
|
||||
videoElement.srcObject = event.streams[0];
|
||||
});
|
||||
|
||||
await negotiateWebRTC(peerConnection);
|
||||
peerConnection.onconnectionstatechange = () => {
|
||||
console.log("PC State:", peerConnection.connectionState);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<main>
|
||||
|
||||
@@ -2,5 +2,13 @@ import { sveltekit } from '@sveltejs/kit/vite';
|
||||
import { defineConfig } from 'vite';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [sveltekit()]
|
||||
plugins: [sveltekit()],
|
||||
server: {
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:3000',
|
||||
changeOrigin: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user