Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {useEffect, useRef, useState} from 'react';
- import {Button} from "@material-ui/core";
- import Janus from './../../../plugins/videoroom-plugin';
- interface JoinedInfo {
- videoroom: string;
- room: number;
- description: string;
- id: number;
- private_id: number;
- publishers: any[];
- }
- export interface joinOption {
- room: number;
- ptype: string;
- display: string;
- feed?: number;
- pin?: any;
- }
- interface Publisher {
- id: number;
- display: string;
- muted: boolean;
- talking?: boolean;
- audio_code?: string;
- published?: boolean;
- }
- const {REACT_APP_JANUS_SERVER_URL} = process.env;
- console.log('Janus server url: ',REACT_APP_JANUS_SERVER_URL);
- export default function VideoRoom() {
- const [publishersList, setPublishersList] = useState<Publisher[]>([])
- const outGoingStream = useRef<any>();
- const serverUrl = `${REACT_APP_JANUS_SERVER_URL}`;
- const serverToken = "";
- useEffect(() => { }, []);
- function addPublishers(publishers: Publisher[]){
- setPublishersList((currentList : Publisher[])=> {
- // console.log(currentList);
- return currentList.concat(publishers)
- }
- );
- }
- function removePublisher(leavingId: number){
- setPublishersList((currentList : Publisher[])=> {
- return currentList.filter((publisher : Publisher) => publisher.id !== leavingId);
- }
- );
- }
- // function togglePublished(unpublishedId: number){ // toDo needs discussion
- // setPublishersList((currentList : Publisher[])=> {
- // return currentList.map((publisher : Publisher) => {
- // if(publisher.id === unpublishedId)
- // {
- // publisher.published = !publisher.published;
- // }
- // return publisher;
- // });
- // // return updatedList;
- // }
- // );
- // }
- const subPluginTest = async (connection: any) =>
- {
- const session2 = await connection.createSession();
- const plugin2 = await session2.attachPlugin(Janus.VideoroomPlugin.NAME);
- }
- const createRoom = async () =>{
- try{
- const janus: any = new Janus.Client(serverUrl, {
- token: serverToken,
- apisecret: serverToken,
- keepalive: "true",
- });
- const connection = await janus.createConnection();
- const session = await connection.createSession();
- const plugin = await session.attachPlugin(Janus.VideoroomPlugin.NAME);
- const stream = await plugin.getUserMedia({ audio: false, video: true });
- const ts = document.getElementById("myVideo");
- plugin.attachMediaStream(ts, stream);
- // toDo .... GOOD SO FAR
- plugin.on("message", function (message: any) {
- console.log(message);
- //plugin.processIncomeMessage(message);
- let _plainMessage = message._plainMessage;
- let janusType = _plainMessage['janus'];
- switch (janusType) {
- case 'event':
- const plugindata = _plainMessage['plugindata']['data'];
- if(plugindata['videoroom']){
- let videoroom: string = plugindata['videoroom'];
- switch(videoroom){
- // self joined event
- case 'joined':
- let publishers = plugindata['publishers'];
- const myAllocatedId = plugindata['id'];
- const myProfile: Publisher = {
- id: myAllocatedId,
- display: "myName(toDo)", //toDo change it please
- muted: false,
- talking: false,
- published: true,
- }
- addPublishers([myProfile,...publishers]);
- break;
- case 'event':
- // someone left event
- if (plugindata['leaving']) {
- let leavingId = plugindata['leaving'];
- removePublisher(leavingId);
- }
- //someone joined event
- if (plugindata['publishers']) {
- let publishers = plugindata['publishers'];
- addPublishers(publishers);
- }
- // unpublished toDo
- // if (plugindata['unpublished']) {
- // let unpublishedId = plugindata['unpublished'];
- // togglePublished(unpublishedId);
- // }
- break;
- }
- }
- break;
- }
- // console.log(message);
- // plugin.processIncomeMessagePublishers();
- });
- // plugin.on("videoroom-remote-feed:publishers", function (message: any) {
- // console.log("in videoroom-remote-feed:publishers");
- // console.log(message);
- // });
- const joinRoomDataOption: joinOption ={
- room: 1234,
- ptype: "publisher",
- display: "sakib-desktop",
- }
- let data = await plugin.joinRoom(joinRoomDataOption);
- const joinedInfo: JoinedInfo = data._plainMessage.plugindata.data;
- await plugin.processLocalVideo({ audio: true, video: true }, joinedInfo);
- await subPluginTest(connection);
- }
- catch (err)
- {
- console.log("createRoom did not work");
- console.log(err);
- }
- }
- const joinRoom = async () => {
- await createRoom();
- }
- return (
- <div>
- <h1>Janus Video Room</h1>
- <h1>publishers: {publishersList.length}</h1>
- <Button
- variant="contained"
- color="primary"
- className="joinNow"
- onClick={joinRoom}
- // endIcon={<ChevronRightIcon />}
- >
- Join Now
- </Button>
- <br/>
- {publishersList.map((publisher, idx) => {
- return (
- <div key={publisher.id}>
- <h1>{publisher.display}: {(publisher.published)? "true":"false"}</h1>
- </div>
- );
- })}
- <video
- id="myVideo"
- ref={outGoingStream}
- autoPlay
- style={{ width: "600px", height: "400px" }}
- />
- </div>
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment