sak1b

vdo

Jul 15th, 2021
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useEffect, useRef, useState} from 'react';
  2. import {Button} from "@material-ui/core";
  3. import Janus from './../../../plugins/videoroom-plugin';
  4.  
  5. interface JoinedInfo {
  6.   videoroom: string;
  7.   room: number;
  8.   description: string;
  9.   id: number;
  10.   private_id: number;
  11.   publishers: any[];
  12. }
  13.  
  14. export interface joinOption {
  15.   room: number;
  16.   ptype: string;
  17.   display: string;
  18.   feed?: number;
  19.   pin?: any;
  20. }
  21.  
  22. interface Publisher {
  23.   id: number;
  24.   display: string;
  25.   muted: boolean;
  26.   talking?: boolean;
  27.   audio_code?: string;
  28.   published?: boolean;
  29. }
  30.  
  31. const {REACT_APP_JANUS_SERVER_URL} = process.env;
  32. console.log('Janus server url: ',REACT_APP_JANUS_SERVER_URL);
  33.  
  34. export default function VideoRoom() {
  35.   const [publishersList, setPublishersList] = useState<Publisher[]>([])
  36.   const outGoingStream = useRef<any>();
  37.  
  38.  
  39.  
  40.   const serverUrl = `${REACT_APP_JANUS_SERVER_URL}`;
  41.   const serverToken = "";
  42.  
  43.   useEffect(() => { }, []);
  44.  
  45.   function addPublishers(publishers: Publisher[]){
  46.     setPublishersList((currentList : Publisher[])=> {
  47.         // console.log(currentList);
  48.         return currentList.concat(publishers)
  49.       }
  50.     );
  51.   }
  52.  
  53.   function removePublisher(leavingId: number){
  54.     setPublishersList((currentList : Publisher[])=> {
  55.         return currentList.filter((publisher : Publisher) => publisher.id !== leavingId);
  56.       }
  57.     );
  58.   }
  59.  
  60.   // function togglePublished(unpublishedId: number){ // toDo needs discussion
  61.   //   setPublishersList((currentList : Publisher[])=> {
  62.   //     return currentList.map((publisher : Publisher) => {
  63.   //       if(publisher.id === unpublishedId)
  64.   //       {
  65.   //         publisher.published = !publisher.published;
  66.   //       }
  67.   //       return publisher;
  68.   //     });
  69.   //    // return updatedList;
  70.   //     }
  71.   //   );
  72.   // }
  73.  
  74.  
  75.  
  76.   const subPluginTest = async (connection: any) =>
  77.   {
  78.     const session2  = await connection.createSession();
  79.     const plugin2 = await session2.attachPlugin(Janus.VideoroomPlugin.NAME);
  80.  
  81.  
  82.   }
  83.  
  84.   const createRoom = async () =>{
  85.  
  86.     try{
  87.  
  88.       const janus: any = new Janus.Client(serverUrl, {
  89.         token: serverToken,
  90.         apisecret: serverToken,
  91.         keepalive: "true",
  92.       });
  93.  
  94.       const connection = await janus.createConnection();
  95.       const session = await connection.createSession();
  96.       const plugin = await session.attachPlugin(Janus.VideoroomPlugin.NAME);
  97.       const stream = await plugin.getUserMedia({ audio: false, video: true });
  98.       const ts = document.getElementById("myVideo");
  99.       plugin.attachMediaStream(ts, stream);
  100.       // toDo .... GOOD SO FAR
  101.       plugin.on("message", function (message: any) {
  102.         console.log(message);
  103.         //plugin.processIncomeMessage(message);
  104.         let _plainMessage = message._plainMessage;
  105.         let janusType = _plainMessage['janus'];
  106.         switch (janusType) {
  107.           case 'event':
  108.             const plugindata = _plainMessage['plugindata']['data'];
  109.             if(plugindata['videoroom']){
  110.               let videoroom: string = plugindata['videoroom'];
  111.               switch(videoroom){
  112.                 // self joined event
  113.                 case 'joined':
  114.                   let publishers = plugindata['publishers'];
  115.                   const myAllocatedId = plugindata['id'];
  116.                   const myProfile: Publisher = {
  117.                     id: myAllocatedId,
  118.                     display: "myName(toDo)", //toDo change it please
  119.                     muted: false,
  120.                     talking: false,
  121.                     published: true,
  122.                   }
  123.                   addPublishers([myProfile,...publishers]);
  124.                   break;
  125.                 case 'event':
  126.                   // someone left event
  127.                   if (plugindata['leaving']) {
  128.                     let leavingId = plugindata['leaving'];
  129.                     removePublisher(leavingId);
  130.                   }
  131.                   //someone joined event
  132.                   if (plugindata['publishers']) {
  133.                     let publishers = plugindata['publishers'];
  134.                     addPublishers(publishers);
  135.                   }
  136.                   // unpublished toDo
  137.                   // if (plugindata['unpublished']) {
  138.                   //   let unpublishedId = plugindata['unpublished'];
  139.                   //   togglePublished(unpublishedId);
  140.                   // }
  141.                   break;
  142.               }
  143.             }
  144.             break;
  145.         }
  146.         // console.log(message);
  147.         // plugin.processIncomeMessagePublishers();
  148.  
  149.       });
  150.       // plugin.on("videoroom-remote-feed:publishers", function (message: any) {
  151.       //   console.log("in videoroom-remote-feed:publishers");
  152.       //   console.log(message);
  153.       // });
  154.  
  155.       const joinRoomDataOption: joinOption ={
  156.         room: 1234,
  157.         ptype: "publisher",
  158.         display: "sakib-desktop",
  159.  
  160.       }
  161.       let data = await plugin.joinRoom(joinRoomDataOption);
  162.       const joinedInfo: JoinedInfo = data._plainMessage.plugindata.data;
  163.       await plugin.processLocalVideo({ audio: true, video: true }, joinedInfo);
  164.  
  165.       await subPluginTest(connection);
  166.  
  167.  
  168.     }
  169.     catch (err)
  170.     {
  171.       console.log("createRoom did not work");
  172.       console.log(err);
  173.     }
  174.  
  175.   }
  176.  
  177.   const joinRoom = async () => {
  178.     await createRoom();
  179.   }
  180.  
  181.   return (
  182.     <div>
  183.       <h1>Janus Video Room</h1>
  184.       <h1>publishers: {publishersList.length}</h1>
  185.       <Button
  186.         variant="contained"
  187.         color="primary"
  188.         className="joinNow"
  189.         onClick={joinRoom}
  190.         // endIcon={<ChevronRightIcon />}
  191.       >
  192.         Join Now
  193.       </Button>
  194.       <br/>
  195.       {publishersList.map((publisher, idx) => {
  196.         return (
  197.           <div key={publisher.id}>
  198.             <h1>{publisher.display}: {(publisher.published)? "true":"false"}</h1>
  199.           </div>
  200.         );
  201.       })}
  202.       <video
  203.         id="myVideo"
  204.         ref={outGoingStream}
  205.         autoPlay
  206.         style={{ width: "600px", height: "400px" }}
  207.       />
  208.     </div>
  209.     )
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment