import * as React from "react"; import "./ClassroomMediaManager.less"; import {NetlessStream} from "./ClassroomMedia"; import * as microphone_open from "../../assets/image/microphone_open.svg"; import * as microphone_close from "../../assets/image/microphone_close.svg"; import Identicon from "../../tools/identicon/Identicon"; import {ClassModeType} from "../../pages/NetlessRoomTypes"; import {roomStore} from "../../models/RoomStore"; export type ClassroomManagerCellProps = { stream: NetlessStream; userId: number; rtcClient: any; streamsLength: number; streamIndex: number userAvatarUrl?: string; setMemberToStageById: (userId: number) => void; classMode: ClassModeType; setLocalStreamState: (state: boolean) => void; isLocalStreamPublish: boolean; mediaLayerDownRef: HTMLDivElement; }; export default class ClassroomMediaCell extends React.Component { public constructor(props: ClassroomManagerCellProps) { super(props); } public videoEl: HTMLVideoElement; public async componentDidMount(): Promise { const {stream} = this.props; await this.startStream(stream); roomStore.releaseMedia = this.release; } private startStream = (stream: NetlessStream): void => { const streamId = stream.getId(); this.publishLocalStream(stream); stream.play(`netless-${streamId}`); } private release = (): void => { const {stream} = this.props; this.stopStream(stream); } public componentWillUnmount(): void { this.release(); } private stopStream = (stream: NetlessStream): void => { if (stream.isPlaying()) { stream.stop(); } } private renderStageStyle = (): any => { const {streamsLength, mediaLayerDownRef} = this.props; const {clientWidth, clientHeight} = mediaLayerDownRef; if (streamsLength <= 2) { return {width: clientWidth, height: clientHeight}; } else if (streamsLength > 4 && streamsLength < 6) { return {width: clientWidth, height: (0.75 * clientHeight)}; } else { return {width: clientWidth, height: (0.7 * clientHeight)}; } } private renderStyle = (): any => { const {streamsLength, streamIndex, mediaLayerDownRef} = this.props; const {clientWidth, clientHeight} = mediaLayerDownRef; const index = streamIndex - 1; if (streamsLength === 2) { return {width: (0.3 * clientWidth), height: (0.3 * clientHeight)}; } else if (streamsLength === 3) { return {width: (0.5 * clientWidth), height: (0.3 * clientHeight), left: (index * (0.5 * clientWidth))}; } else if (streamsLength === 4) { return {width: (clientWidth / 3), height: (0.3 * clientHeight), left: (index * (clientWidth / 3))}; } else if (streamsLength === 5) { return {width: (0.25 * clientWidth), height: (0.25 * clientHeight), left: (index * (0.25 * clientWidth))}; } else { if (streamIndex >= 4) { return {width: (0.25 * clientWidth), height: (0.25 * clientHeight), left: ((index - 4) * (0.25 * clientWidth)), bottom: (0.25 * clientHeight)}; } else { return {width: (0.25 * clientWidth), height: (0.25 * clientHeight), left: (index * (0.25 * clientWidth))}; } } } private publishLocalStream = (stream: NetlessStream): void => { const {userId, rtcClient} = this.props; const streamId = stream.getId(); if (streamId === userId && !this.props.isLocalStreamPublish && rtcClient !== undefined) { rtcClient.publish(stream, (err: any) => { console.log("publish failed"); console.error(err); }); this.props.setLocalStreamState(true); } } private handleClickVideo = (userId: number): void => { this.props.setMemberToStageById(userId); } private renderStageAvatar = (): React.ReactNode => { const {userAvatarUrl, userId, stream} = this.props; const size = this.renderStageStyle(); if (userAvatarUrl) { return
; } else { return
; } } private renderAvatar = (): React.ReactNode => { const {userAvatarUrl, stream} = this.props; const size = this.renderStyle(); if (userAvatarUrl) { return
; } else { return
; } } public render(): React.ReactNode { const {stream} = this.props; const streamId = stream.getId(); if (stream.state.isInStage) { return (
{stream.state.isAudioOpen ? : }
{!stream.state.isVideoOpen && this.renderStageAvatar()}
); } else { return (
{stream.state.isAudioOpen ? : }
{!stream.state.isVideoOpen && this.renderAvatar()}
this.handleClickVideo(streamId)} className="rtc-media-cell-box" />
); } } }