sak1b

AudioBridge.js (mute works)

Jun 27th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect, useRef } from "react";
  2. import { Button, TextField, Card, CardContent } from "@material-ui/core";
  3. import Avatar from "@material-ui/core/Avatar";
  4. import { deepOrange, deepPurple } from "@material-ui/core/colors";
  5. import { makeStyles } from "@material-ui/core/styles";
  6. import * as Janus from "janus-gateway-js";
  7.  
  8. const useStyles = makeStyles((theme) => ({
  9.   joinButton: {
  10.     [theme.breakpoints.down("sm")]: {
  11.       width: "50%",
  12.     },
  13.   },
  14.   card: {
  15.     display: "flex",
  16.     justifyContent: "center",
  17.     width: "200px",
  18.   },
  19.   participantList: {
  20.     display: "flex",
  21.     justifyContent: "center",
  22.   },
  23.   orange: {
  24.     color: theme.palette.getContrastText(deepOrange[500]),
  25.     backgroundColor: deepOrange[500],
  26.   },
  27.   purple: {
  28.     color: theme.palette.getContrastText(deepPurple[500]),
  29.     backgroundColor: deepPurple[500],
  30.   },
  31.   error:{
  32.     color: "red"
  33.   }
  34. }));
  35.  
  36. export default function AudioBridge() {
  37.   const classes = useStyles();
  38.   const [loggedIn, setLoggedIn] = useState(false);
  39.   const [username, setUsername] = useState("");
  40.   const [roomId, setRoomId] = useState("");
  41.   const [participantList, setparticipantList] = useState([]);
  42.   const [error, setError] = useState("");
  43.   const incomingStream = useRef();
  44.  
  45.   let userList = [];
  46.   let pluginHandle = null;
  47.  
  48.   const serverUrl = "ws://localhost:8188/janus"; //my local setup
  49.   // const serverUrl = "wss://janus.onudaan.com:8979/janus"; /// works 192.168.5.73
  50.   // const serverUrl = "wss://janus.onudaan.com:443/janus";  /// DOESN'T WORK janus cloud e audio bridge plugin nai
  51.  
  52.   const serverToken = "legpko4equi3aycpaex67rl7eo4xtpai";
  53.  
  54.   const janus = new Janus.Client(serverUrl, {
  55.     token: serverToken,
  56.     apisecret: serverToken,
  57.     keepalive: "true",
  58.   });
  59.  
  60.   useEffect(() => {
  61.     console.log("From useEffect");
  62.     console.log(participantList);
  63.    
  64.   }, [participantList]);
  65.  
  66.   const leave = (e) => {
  67.     e.preventDefault();
  68.     console.log('in leave');
  69.     console.log(pluginHandle);
  70.   }
  71.  
  72.   function init_room()
  73.   {
  74.     console.log("in init room");
  75.     let cleanRoomId = parseInt(roomId);
  76.  
  77.     if(isNaN(cleanRoomId))
  78.     {
  79.       setError("Room ID must be a number.");
  80.       setTimeout(()=>{setError("");},3000);
  81.       return;
  82.     }
  83.  
  84.     janus.createConnection("client").then(function (connection) {
  85.       connection.createSession().then(function (session) {
  86.         // console.log("session:", session);
  87.         session
  88.           .attachPlugin("janus.plugin.audiobridge")
  89.           .then(function (plugin) {
  90.  
  91.             plugin.create(cleanRoomId,{
  92.  
  93.             });
  94.  
  95.             plugin.on("error", function (message) {
  96.                 console.log(message);
  97.                 console.log(message.message);
  98.  
  99.             });
  100.  
  101.             plugin.join(cleanRoomId, {
  102.               display: username,
  103.               quality: 3,
  104.               token: "token",
  105.             });
  106.  
  107.             setLoggedIn(true);
  108.             pluginHandle = plugin;
  109.  
  110.             //let stream = plugin.getUserMedia({ audio: true, video: false });
  111.  
  112.             plugin.getUserMedia({ audio: true, video: false })
  113.             .then(function(stream){
  114.               plugin.offerStream(stream);
  115.             })
  116.  
  117.  
  118.            
  119.  
  120.             plugin.on("message", function (message) {
  121.               console.log(message);
  122.              
  123.               if (message._plainMessage.janus === "event") {
  124.                 // join
  125.                 if(message._plainMessage.plugindata.data.audiobridge === "joined"){
  126.                   console.log("in joined block===");
  127.                   if (userList.length === 0) {
  128.                     // setparticipantList(message._plainMessage.plugindata.data.participants);
  129.                     userList = message._plainMessage.plugindata.data.participants;
  130.                     setparticipantList(userList);
  131.                   } else {
  132.                     userList = [...userList, message._plainMessage.plugindata.data.participants[0]];
  133.                     setparticipantList(userList);
  134.                   }
  135.                 }
  136.                 // leaving
  137.                 if (message._plainMessage.plugindata.data.leaving) {
  138.                   console.log("in leaving block===");
  139.                   let leavingId = message._plainMessage.plugindata.data.leaving;
  140.                   userList = userList.filter((user) => user.id !== leavingId);
  141.                   setparticipantList(userList);
  142.                 }
  143.  
  144.                 //mute unmute event
  145.  
  146.                 if (message._plainMessage.plugindata.data.audiobridge ==="event" && message._plainMessage.plugindata.data.participants) {
  147.                   console.log("in event block (mute/unmute)");
  148.                   const actor = message._plainMessage.plugindata.data.participants[0];
  149.                   console.log(actor);
  150.  
  151.                   // for (let i = 0; i < userList.length; i++) {
  152.                   //   if (userList[i].id === actor.id) {
  153.                   //     userList[i].muted = actor.muted;
  154.                   //   }
  155.                   // }
  156.  
  157.                  // userList = userList.filter((user) => user.id !== actor.id);
  158.                  // userList = [...userList, actor];
  159.  
  160.                   userList = userList.map(user => {
  161.                     if(user.id === actor.id)
  162.                     {
  163.                       user.muted = actor.muted;
  164.                     }
  165.                     return user;
  166.                   });
  167.                  
  168.                   console.log(userList);
  169.                   setparticipantList(userList);
  170.                   console.log(participantList);
  171.  
  172.                 }
  173.  
  174.               }
  175.             });
  176.  
  177.             plugin.on("pc:track:remote", function (message) {
  178.               console.log("Receiving incoming stream.");
  179.               //console.log(message.streams[0]);
  180.               incomingStream.current.srcObject = message.streams[0];
  181.             });
  182.           });
  183.       });
  184.     });
  185.   }
  186.  
  187.   const join = (e) => {
  188.     e.preventDefault();
  189.     init_room();
  190.    
  191.   };
  192.  
  193.   return (
  194.     <div>
  195.       {loggedIn === false ? (
  196.         <div>
  197.           <h1>Audio Bridge Plugin Mounted!</h1>
  198.           <TextField
  199.             onChange={(e) => setUsername(e.target.value)}
  200.             value={username}
  201.             id="userName"
  202.             type="text"
  203.             label="Username"
  204.             variant="outlined"
  205.             margin="normal"
  206.           />
  207.           <br/>
  208.           <TextField
  209.             onChange={(e) => setRoomId(e.target.value)}
  210.             value={roomId}
  211.             id="roomId"
  212.             type="text"
  213.             label="Room ID"
  214.             variant="outlined"
  215.             margin="normal"
  216.           />
  217.           <br />
  218.           <br />
  219.          
  220.           <Button
  221.             type="submit"
  222.             variant="contained"
  223.             color="primary"
  224.             onClick={join}
  225.           //className={classes.joinButton}
  226.           >
  227.             Join
  228.           </Button>
  229.  
  230.           <br />
  231.  
  232.           {error.length !== 0 && <p className={classes.error}>{error}</p>}
  233.          
  234.           <br/>
  235.  
  236.         </div>
  237.       ) : (
  238.         <div>
  239.           <h2>
  240.             {username}, you have successfully joined room: {roomId}
  241.           </h2>
  242.           <h4>Participants</h4>
  243.           <div className={classes.participantList}>
  244.             <video
  245.               playsInline
  246.               ref={incomingStream}
  247.               autoPlay
  248.               style={{ width: "0px" }}
  249.             />
  250.             <br />
  251.             <div>
  252.               {participantList.map((participant, idx) => {
  253.                 return (
  254.                   <div key={participant.id}>
  255.                     <Card className={classes.card} variant="outlined">
  256.                       <CardContent>
  257.                         <Avatar className={classes.orange}>
  258.                           {participant.display[0]}
  259.                         </Avatar>
  260.                         <p>{participant.display}</p>
  261.                         {participant.muted === true && <p>muted</p>}
  262.                       </CardContent>
  263.                     </Card>
  264.                   </div>
  265.                 );
  266.               })}
  267.             </div>
  268.             <br/>
  269.             <div>
  270.               <Button
  271.               type="submit"
  272.               variant="contained"
  273.               color="secondary"
  274.               onClick={leave}
  275.               //className={classes.joinButton}
  276.               >
  277.                 Leave
  278.               </Button>
  279.             </div>
  280.            
  281.           </div>
  282.         </div>
  283.       )}
  284.     </div>
  285.   );
  286. }
  287.  
Add Comment
Please, Sign In to add comment