Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { useState, useEffect, useRef } from "react";
- import { Button, TextField, Card, CardContent } from "@material-ui/core";
- import Avatar from "@material-ui/core/Avatar";
- import { deepOrange, deepPurple } from "@material-ui/core/colors";
- import { makeStyles } from "@material-ui/core/styles";
- import * as Janus from "janus-gateway-js";
- const useStyles = makeStyles((theme) => ({
- joinButton: {
- [theme.breakpoints.down("sm")]: {
- width: "50%",
- },
- },
- card: {
- display: "flex",
- justifyContent: "center",
- width: "200px",
- },
- participantList: {
- display: "flex",
- justifyContent: "center",
- },
- orange: {
- color: theme.palette.getContrastText(deepOrange[500]),
- backgroundColor: deepOrange[500],
- },
- purple: {
- color: theme.palette.getContrastText(deepPurple[500]),
- backgroundColor: deepPurple[500],
- },
- error:{
- color: "red"
- }
- }));
- export default function AudioBridge() {
- const classes = useStyles();
- const [loggedIn, setLoggedIn] = useState(false);
- const [username, setUsername] = useState("");
- const [roomId, setRoomId] = useState("");
- const [participantList, setparticipantList] = useState([]);
- const [error, setError] = useState("");
- const incomingStream = useRef();
- let userList = [];
- let pluginHandle = null;
- const serverUrl = "ws://localhost:8188/janus"; //my local setup
- // const serverUrl = "wss://janus.onudaan.com:8979/janus"; /// works 192.168.5.73
- // const serverUrl = "wss://janus.onudaan.com:443/janus"; /// DOESN'T WORK janus cloud e audio bridge plugin nai
- const serverToken = "legpko4equi3aycpaex67rl7eo4xtpai";
- const janus = new Janus.Client(serverUrl, {
- token: serverToken,
- apisecret: serverToken,
- keepalive: "true",
- });
- useEffect(() => {
- console.log("From useEffect");
- console.log(participantList);
- }, [participantList]);
- const leave = (e) => {
- e.preventDefault();
- console.log('in leave');
- console.log(pluginHandle);
- }
- function init_room()
- {
- console.log("in init room");
- let cleanRoomId = parseInt(roomId);
- if(isNaN(cleanRoomId))
- {
- setError("Room ID must be a number.");
- setTimeout(()=>{setError("");},3000);
- return;
- }
- janus.createConnection("client").then(function (connection) {
- connection.createSession().then(function (session) {
- // console.log("session:", session);
- session
- .attachPlugin("janus.plugin.audiobridge")
- .then(function (plugin) {
- plugin.create(cleanRoomId,{
- });
- plugin.on("error", function (message) {
- console.log(message);
- console.log(message.message);
- });
- plugin.join(cleanRoomId, {
- display: username,
- quality: 3,
- token: "token",
- });
- setLoggedIn(true);
- pluginHandle = plugin;
- //let stream = plugin.getUserMedia({ audio: true, video: false });
- plugin.getUserMedia({ audio: true, video: false })
- .then(function(stream){
- plugin.offerStream(stream);
- })
- plugin.on("message", function (message) {
- console.log(message);
- if (message._plainMessage.janus === "event") {
- // join
- if(message._plainMessage.plugindata.data.audiobridge === "joined"){
- console.log("in joined block===");
- if (userList.length === 0) {
- // setparticipantList(message._plainMessage.plugindata.data.participants);
- userList = message._plainMessage.plugindata.data.participants;
- setparticipantList(userList);
- } else {
- userList = [...userList, message._plainMessage.plugindata.data.participants[0]];
- setparticipantList(userList);
- }
- }
- // leaving
- if (message._plainMessage.plugindata.data.leaving) {
- console.log("in leaving block===");
- let leavingId = message._plainMessage.plugindata.data.leaving;
- userList = userList.filter((user) => user.id !== leavingId);
- setparticipantList(userList);
- }
- //mute unmute event
- if (message._plainMessage.plugindata.data.audiobridge ==="event" && message._plainMessage.plugindata.data.participants) {
- console.log("in event block (mute/unmute)");
- const actor = message._plainMessage.plugindata.data.participants[0];
- console.log(actor);
- // for (let i = 0; i < userList.length; i++) {
- // if (userList[i].id === actor.id) {
- // userList[i].muted = actor.muted;
- // }
- // }
- // userList = userList.filter((user) => user.id !== actor.id);
- // userList = [...userList, actor];
- userList = userList.map(user => {
- if(user.id === actor.id)
- {
- user.muted = actor.muted;
- }
- return user;
- });
- console.log(userList);
- setparticipantList(userList);
- console.log(participantList);
- }
- }
- });
- plugin.on("pc:track:remote", function (message) {
- console.log("Receiving incoming stream.");
- //console.log(message.streams[0]);
- incomingStream.current.srcObject = message.streams[0];
- });
- });
- });
- });
- }
- const join = (e) => {
- e.preventDefault();
- init_room();
- };
- return (
- <div>
- {loggedIn === false ? (
- <div>
- <h1>Audio Bridge Plugin Mounted!</h1>
- <TextField
- onChange={(e) => setUsername(e.target.value)}
- value={username}
- id="userName"
- type="text"
- label="Username"
- variant="outlined"
- margin="normal"
- />
- <br/>
- <TextField
- onChange={(e) => setRoomId(e.target.value)}
- value={roomId}
- id="roomId"
- type="text"
- label="Room ID"
- variant="outlined"
- margin="normal"
- />
- <br />
- <br />
- <Button
- type="submit"
- variant="contained"
- color="primary"
- onClick={join}
- //className={classes.joinButton}
- >
- Join
- </Button>
- <br />
- {error.length !== 0 && <p className={classes.error}>{error}</p>}
- <br/>
- </div>
- ) : (
- <div>
- <h2>
- {username}, you have successfully joined room: {roomId}
- </h2>
- <h4>Participants</h4>
- <div className={classes.participantList}>
- <video
- playsInline
- ref={incomingStream}
- autoPlay
- style={{ width: "0px" }}
- />
- <br />
- <div>
- {participantList.map((participant, idx) => {
- return (
- <div key={participant.id}>
- <Card className={classes.card} variant="outlined">
- <CardContent>
- <Avatar className={classes.orange}>
- {participant.display[0]}
- </Avatar>
- <p>{participant.display}</p>
- {participant.muted === true && <p>muted</p>}
- </CardContent>
- </Card>
- </div>
- );
- })}
- </div>
- <br/>
- <div>
- <Button
- type="submit"
- variant="contained"
- color="secondary"
- onClick={leave}
- //className={classes.joinButton}
- >
- Leave
- </Button>
- </div>
- </div>
- </div>
- )}
- </div>
- );
- }
Add Comment
Please, Sign In to add comment