mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-28 03:50:11 +08:00
Viewer refactor with state sync (#212)
* initial commit for viewer refactor * updating name change to nerfactory * adding headers * making the rendering fix which broke from a merge * things are starting to work. removing old commands now * removed prints * cleanup * updating state works now even for nested dictionaries * updating state in redux. listening for state changes. trying to draw three.js now * added drawing functionality. need to fix translation of frustums and image planes * images and frustums drawn correctly * fixing lint * github actions * back to npm * fixing eslint * remove package.json from root * new favicon * some viewer cleanup * notebook * fixed camera drawings speed to avoid redraw * trying to get Leva to update * quick fix * synced config panel * adding notebook * fixing output print error
This commit is contained in:
1 parent
966374474d
commit
73fedc7593
67 files changed
+2510
-14057
No files matched your search
@@ -0,0 +1,147 @@
|
||||
import React, { createContext, useContext, useEffect, useRef } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
|
||||
import { WebSocketContext } from '../WebSocket/WebSocket';
|
||||
|
||||
const WebRtcContext = createContext(null);
|
||||
const msgpack = require('msgpack-lite');
|
||||
|
||||
export { WebRtcContext };
|
||||
|
||||
export default function WebRtcWindow() {
|
||||
const websocket = useContext(WebSocketContext).socket;
|
||||
const pcRef = useRef(null);
|
||||
const localVideoRef = useRef(null);
|
||||
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const getRTCPeerConnection = () => {
|
||||
const pc = new RTCPeerConnection({
|
||||
iceServers: [
|
||||
{
|
||||
urls: 'stun:openrelay.metered.ca:80',
|
||||
},
|
||||
{
|
||||
urls: 'turn:openrelay.metered.ca:80',
|
||||
username: 'openrelayproject',
|
||||
credential: 'openrelayproject',
|
||||
},
|
||||
{
|
||||
urls: 'turn:openrelay.metered.ca:443',
|
||||
username: 'openrelayproject',
|
||||
credential: 'openrelayproject',
|
||||
},
|
||||
{
|
||||
urls: 'turn:openrelay.metered.ca:443?transport=tcp',
|
||||
username: 'openrelayproject',
|
||||
credential: 'openrelayproject',
|
||||
},
|
||||
],
|
||||
});
|
||||
// connect video
|
||||
pc.addEventListener('track', (evt) => {
|
||||
dispatch({
|
||||
type: "write",
|
||||
path: 'webrtcState/isConnected',
|
||||
data: true,
|
||||
});
|
||||
if (evt.track.kind === 'video') {
|
||||
[localVideoRef.current.srcObject] = evt.streams; // uses array destructuring
|
||||
}
|
||||
});
|
||||
pc.addTransceiver('video', { direction: 'recvonly' });
|
||||
|
||||
// for updating the status of the peer connection
|
||||
pc.oniceconnectionstatechange = () => {
|
||||
if (pc.iceConnectionState === 'connected') {
|
||||
dispatch({
|
||||
type: "write",
|
||||
path: 'webrtcState/isConnected',
|
||||
data: true,
|
||||
});
|
||||
} else if (pc.iceConnectionState === 'disconnected') {
|
||||
dispatch({
|
||||
type: "write",
|
||||
path: 'webrtcState/isConnected',
|
||||
data: false,
|
||||
});
|
||||
}
|
||||
};
|
||||
return pc;
|
||||
};
|
||||
|
||||
const sendOffer = () => {
|
||||
pcRef.current
|
||||
.createOffer()
|
||||
.then((offer) => {
|
||||
return pcRef.current.setLocalDescription(offer);
|
||||
})
|
||||
.then(() => {
|
||||
// wait for ICE gathering to complete
|
||||
return new Promise((resolve) => {
|
||||
if (pcRef.current.iceGatheringState === 'complete') {
|
||||
resolve();
|
||||
} else {
|
||||
const checkState = () => {
|
||||
if (pcRef.current.iceGatheringState === 'complete') {
|
||||
pcRef.current.removeEventListener(
|
||||
'icegatheringstatechange',
|
||||
checkState,
|
||||
);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
pcRef.current.addEventListener(
|
||||
'icegatheringstatechange',
|
||||
checkState,
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
// send the offer
|
||||
const offer = pcRef.current.localDescription;
|
||||
const cmd = 'offer';
|
||||
const path = '';
|
||||
const data = {
|
||||
type: cmd,
|
||||
path,
|
||||
data: {
|
||||
sdp: offer.sdp,
|
||||
type: offer.type,
|
||||
},
|
||||
};
|
||||
const message = msgpack.encode(data);
|
||||
websocket.send(message);
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
websocket.addEventListener('message', (originalCmd) => {
|
||||
// set the remote description when the offer is received
|
||||
const cmd = msgpack.decode(new Uint8Array(originalCmd.data));
|
||||
if (cmd.type === 'answer') {
|
||||
const answer = cmd.data;
|
||||
pcRef.current.setRemoteDescription(answer);
|
||||
}
|
||||
});
|
||||
|
||||
websocket.addEventListener('open', () => {
|
||||
// connect webrtc when the websocket is connected
|
||||
pcRef.current = getRTCPeerConnection();
|
||||
sendOffer();
|
||||
});
|
||||
}, []); // empty dependency array means only run once
|
||||
|
||||
return (
|
||||
<div className="WebRTCVideo">
|
||||
<video
|
||||
className="WebRTCVideo-video"
|
||||
autoPlay
|
||||
playsInline
|
||||
muted
|
||||
ref={localVideoRef}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user