Skip to content

Commit

Permalink
Merge branch 'feature/video' into 'master'
Browse files Browse the repository at this point in the history
Feature/video

See merge request jam-systems/jam!83
  • Loading branch information
DoubleMalt committed Aug 12, 2022
2 parents 8f038ff + 57a8ebb commit 0188acd
Show file tree
Hide file tree
Showing 22 changed files with 1,320 additions and 80 deletions.
2 changes: 2 additions & 0 deletions ui/examples/jam-video/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundle.js
.vercel
141 changes: 141 additions & 0 deletions ui/examples/jam-video/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import React, {useState, useEffect} from 'react';
import {render} from 'react-dom';
import {JamProvider, useJam, use} from 'jam-core-react';

const jamConfig = {
domain: 'beta.jam.systems',
development: true,
sfu: true,
};

render(
<JamProvider options={{jamConfig}}>
<App />
</JamProvider>,
document.querySelector('#root')
);

const Video = ({stream}) => {
const videoRef = React.createRef();

useEffect(() => {
if (videoRef.current) videoRef.current.srcObject = stream;
}, [stream, videoRef]);

return <video style={{height: 100, width: 100}} ref={videoRef} autoPlay />;
};

function App() {
const [
state,
{createRoom, setProps, enterRoom, leaveRoom, addPresenter},
] = useJam();
let [
roomId,
speaking,
myId,
inRoom,
iAmSpeaker,
peers,
peerState,
myVideo,
remoteVideoStreams,
] = use(state, [
'roomId',
'speaking',
'myId',
'inRoom',
'iAmSpeaker',
'peers',
'peerState',
'myVideo',
'remoteVideoStreams',
]);

const myVideoRef = React.createRef();
useEffect(() => {
// Let's update the srcObject only after the ref has been set
// and then every time the stream prop updates
if (myVideoRef.current) myVideoRef.current.srcObject = myVideo;
}, [myVideo, myVideoRef]);

let hash = location.hash.slice(1) || null;
let [potentialRoomId, setPotentialRoomId] = useState(hash);
let nJoinedPeers = peers.filter(id => peerState[id]?.inRoom).length;

function stream(e) {
setProps({userInteracted: true});
e.preventDefault();
addPresenter(roomId, myId);
}

function submit(e) {
setProps({userInteracted: true});
e.preventDefault();
if (state.inRoom) {
leaveRoom();
setProps('roomId', null);
} else {
createRoom(potentialRoomId, {stageOnly: true});
setProps('roomId', potentialRoomId);
enterRoom(potentialRoomId);
location.hash = potentialRoomId;
}
}

useEffect(() => {
let hashChange = () => {
let hash = location.hash.slice(1) || null;
if (hash !== state.roomId) {
setPotentialRoomId(hash);
setProps('roomId', null);
}
};
window.addEventListener('hashchange', hashChange);
return () => {
window.removeEventListener('hashchange', hashChange);
};
}, [setProps, state]);

const videoElements = remoteVideoStreams.map(stream => (
<Video stream={stream.stream} />
));

return (
<>
<form onSubmit={submit} style={{marginBottom: '4px'}}>
<input
type="text"
placeholder="Room ID"
style={{width: '145px'}}
value={potentialRoomId ?? ''}
onChange={e => setPotentialRoomId(e.target.value || null)}
disabled={!!roomId}
/>
<button onClick={submit} disabled={!(potentialRoomId?.length > 3)}>
{inRoom ? 'Leave' : 'Join'}
</button>
<button onClick={stream} disabled={!inRoom}>
Start Video
</button>
</form>
<div>
<b style={speaking.has(myId) ? {color: 'green'} : undefined}>
{iAmSpeaker ? 'Speaking' : 'Not speaking'}
</b>{' '}
with <b>{nJoinedPeers}</b> other peer{nJoinedPeers === 1 ? '' : 's'}.
</div>
<div>
<video
style={{height: 100, width: 100, border: '1px solid black'}}
ref={myVideoRef}
autoPlay
/>
</div>
<div>
<h3> Remote Videos </h3>
{videoElements}
</div>
</>
);
}
19 changes: 19 additions & 0 deletions ui/examples/jam-video/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "jam-video",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "npx serve . & esbuild --bundle ./App.jsx --outfile=./bundle.js --format=esm --watch",
"build": "esbuild --minify --bundle ./App.jsx --outfile=./public/bundle.js --format=esm"
},
"dependencies": {
"jam-core-react": "^0.5.0",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"devDependencies": {
"esbuild": "^0.12.5",
"serve": "^12.0.0"
}
}
10 changes: 10 additions & 0 deletions ui/examples/jam-video/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>jam video demo</title>
<link rel="icon" href="data:," />
</head>
<body>
<div id="root" />
<script type="module" src="/bundle.js"></script>
</body>
</html>
Loading

0 comments on commit 0188acd

Please sign in to comment.