Skip to content

Instantly share code, notes, and snippets.

@B-R-Bender
Created November 18, 2021 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save B-R-Bender/c1ca7fdae69cc69e0976e395d32a25c7 to your computer and use it in GitHub Desktop.
Save B-R-Bender/c1ca7fdae69cc69e0976e395d32a25c7 to your computer and use it in GitHub Desktop.
export default function TestScreen() {
const janus = React.useRef(null);
const videoRoom = React.useRef(null);
const [stream, setStream] = React.useState(null);
const [publishers, setPublishers] = React.useState([]);
React.useEffect(() => {
async function init() {
try {
/*
this.setState(state => ({
publishers: [
{
publisher: null,
stream: stream,
},
],
}));
*/
janus.current = new Janus("wss://j2.nextologies.com:8989");
await janus.current.init();
const stream = await getUserStream();
videoRoom.current = new JanusVideoRoomPlugin(janus.current);
videoRoom.current.setRoomID(200004);
videoRoom.current.setDisplayName("bender");
videoRoom.current.setOnPublishersListener((publishers) => {
for (let i = 0; i < publishers.length; i++) {
receivePublisher(publishers[i]);
}
});
videoRoom.current.setOnPublisherJoinedListener((publisher) => {
receivePublisher(publisher);
});
videoRoom.current.setOnPublisherLeftListener((publisherID) => {
removePublisher(publisherID);
});
videoRoom.current.setOnWebRTCUpListener(async () => {
});
await videoRoom.current.createPeer();
await videoRoom.current.connect();
await videoRoom.current.join();
await videoRoom.current.publish(stream);
} catch (e) {
console.error('main', JSON.stringify(e));
}
}
init();
}, []);
const getUserStream = React.useCallback(() => {
async function getStream() {
const isFront = true;
const devices = await mediaDevices.enumerateDevices();
let deviceId;
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
console.log(device);
if (device.kind === 'videoinput' && device.facing === (isFront ? 'front' : 'environment')) {
deviceId = device.deviceId;
}
}
const stream = await mediaDevices.getUserMedia({
audio: true,
video: {
facingMode: (isFront ? 'user' : 'environment'),
},
});
}
return getStream();
}, []);
const receivePublisher = async (publisher: any) => {
try {
let videoRoomParty = new JanusVideoRoomPlugin(janus.current);
videoRoomParty.setRoomID(200004);
videoRoomParty.setOnStreamListener((stream: any) => {
setPublishers(state => ([...state, {publisher, stream}]));
});
await videoRoomParty.createPeer();
await videoRoomParty.connect();
await videoRoomParty.receive(videoRoom.current.getUserPrivateID(), publisher);
} catch (e) {
console.error(e);
}
};
const removePublisher = async (publisherID: any) => {
try {
setPublishers(state => state.filter(item => item.publisher === null || item.publisher.id !== publisherID));
} catch (e) {
console.error(e);
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>View</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="/screens/TabOneScreen.tsx" />
</View>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment