2022-01-19 17:24:11 +01:00
|
|
|
import { Publisher, StreamManager } from 'openvidu-browser';
|
|
|
|
import { VideoType } from './video-type.model';
|
|
|
|
|
2022-06-02 10:57:47 +02:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export enum OpenViduRole {
|
|
|
|
MODERATOR = 'MODERATOR',
|
|
|
|
PUBLISHER = 'PUBLISHER'
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
export interface StreamModel {
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Whether the stream is available or not
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
connected: boolean;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* The stream type.{@link VideoType}
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
type: VideoType;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* The streamManager object from openvidu-browser library.{@link https://docs.openvidu.io/en/stable/api/openvidu-browser/classes/StreamManager.html}
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
streamManager: StreamManager;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Whether the stream is enlarged or not
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
videoEnlarged: boolean;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Unique identifier of the stream
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
connectionId: string;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* The participant object
|
|
|
|
*/
|
|
|
|
participant?: ParticipantAbstractModel;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:35:14 +01:00
|
|
|
export interface ParticipantProperties {
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Whether the participant is local or not
|
|
|
|
*/
|
2022-03-02 17:35:14 +01:00
|
|
|
local: boolean;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* The participant nickname
|
|
|
|
*/
|
2022-03-02 17:35:14 +01:00
|
|
|
nickname: string;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Unique identifier of the participant
|
|
|
|
*/
|
2022-03-02 17:35:14 +01:00
|
|
|
id?: string;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* The participant color profile
|
|
|
|
*/
|
2022-03-02 17:35:14 +01:00
|
|
|
colorProfile?: string;
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* Whether the participant is muted forcibly or not
|
|
|
|
*/
|
2022-03-02 17:35:14 +01:00
|
|
|
isMutedForcibly?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-01-19 17:24:11 +01:00
|
|
|
export abstract class ParticipantAbstractModel {
|
2022-02-15 13:24:08 +01:00
|
|
|
streams: Map<VideoType, StreamModel> = new Map();
|
2022-01-19 17:24:11 +01:00
|
|
|
id: string;
|
2022-02-28 13:48:58 +01:00
|
|
|
local: boolean;
|
|
|
|
nickname: string;
|
2022-03-02 11:02:06 +01:00
|
|
|
colorProfile: string;
|
2022-02-28 13:06:39 +01:00
|
|
|
isMutedForcibly: boolean;
|
2022-01-19 17:24:11 +01:00
|
|
|
|
2022-03-02 17:35:14 +01:00
|
|
|
constructor(props: ParticipantProperties, model?: StreamModel) {
|
2023-02-10 15:50:20 +01:00
|
|
|
this.id = props.id ? props.id : Math.random().toString(32).replace('.','_');
|
2022-03-02 17:35:14 +01:00
|
|
|
this.local = props.local;
|
|
|
|
this.nickname = props.nickname;
|
2022-03-23 13:48:17 +01:00
|
|
|
this.colorProfile = !!props.colorProfile ? props.colorProfile : `hsl(${Math.random() * 360}, 100%, 80%)`;
|
2022-03-02 17:35:14 +01:00
|
|
|
this.isMutedForcibly = typeof props.isMutedForcibly === 'boolean' ? props.isMutedForcibly : false;
|
2022-01-26 16:08:04 +01:00
|
|
|
let streamModel: StreamModel = {
|
2022-03-03 10:21:33 +01:00
|
|
|
connected: model ? model.connected : true,
|
2022-01-26 16:08:04 +01:00
|
|
|
type: model ? model.type : VideoType.CAMERA,
|
|
|
|
streamManager: model ? model.streamManager : null,
|
|
|
|
videoEnlarged: model ? model.videoEnlarged : false,
|
2022-02-15 16:44:09 +01:00
|
|
|
connectionId: model ? model.connectionId : null,
|
|
|
|
participant: this
|
2022-01-26 16:08:04 +01:00
|
|
|
};
|
2022-02-15 13:24:08 +01:00
|
|
|
this.streams.set(streamModel.type, streamModel);
|
2022-01-26 16:08:04 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-26 11:30:30 +01:00
|
|
|
addConnection(streamModel: StreamModel) {
|
2022-02-28 13:48:58 +01:00
|
|
|
streamModel.participant = this;
|
2022-02-15 13:24:08 +01:00
|
|
|
this.streams.set(streamModel.type, streamModel);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-06-16 14:01:07 +02:00
|
|
|
hasAudioActive(): boolean {
|
2022-01-19 17:24:11 +01:00
|
|
|
const cameraConnection = this.getCameraConnection();
|
2022-06-16 14:01:07 +02:00
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
|
|
|
|
if (cameraConnection.connected) {
|
|
|
|
return this.isCameraAudioActive();
|
|
|
|
} else if (screenConnection.connected) {
|
|
|
|
return this.isScreenAudioActive();
|
2022-02-25 11:19:21 +01:00
|
|
|
}
|
2022-06-16 14:01:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
private isCameraAudioActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection?.connected) {
|
|
|
|
return cameraConnection.streamManager?.stream?.audioActive;
|
|
|
|
}
|
|
|
|
return false;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
public isCameraVideoActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
return cameraConnection?.connected && cameraConnection?.streamManager?.stream?.videoActive;
|
|
|
|
}
|
2022-03-23 13:48:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
isScreenAudioActive(): boolean {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
2022-06-16 14:01:07 +02:00
|
|
|
if (screenConnection?.connected) {
|
|
|
|
return screenConnection?.streamManager?.stream?.audioActive;
|
2022-02-25 11:19:21 +01:00
|
|
|
}
|
|
|
|
return false;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
hasConnectionType(type: VideoType): boolean {
|
2022-02-15 13:24:08 +01:00
|
|
|
return this.streams.has(type);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-26 11:30:30 +01:00
|
|
|
public getCameraConnection(): StreamModel {
|
2022-02-15 13:24:08 +01:00
|
|
|
return this.streams.get(VideoType.CAMERA);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-26 11:30:30 +01:00
|
|
|
public getScreenConnection(): StreamModel {
|
2022-02-15 13:24:08 +01:00
|
|
|
return this.streams.get(VideoType.SCREEN);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 11:19:21 +01:00
|
|
|
getConnectionTypesActive(): VideoType[] {
|
2022-01-19 17:24:11 +01:00
|
|
|
let connType = [];
|
2022-02-25 11:19:21 +01:00
|
|
|
if (this.isCameraActive()) connType.push(VideoType.CAMERA);
|
|
|
|
if (this.isScreenActive()) connType.push(VideoType.SCREEN);
|
2022-01-19 17:24:11 +01:00
|
|
|
|
|
|
|
return connType;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setCameraConnectionId(connectionId: string) {
|
|
|
|
this.getCameraConnection().connectionId = connectionId;
|
|
|
|
}
|
2022-03-23 13:48:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setScreenConnectionId(connectionId: string) {
|
|
|
|
this.getScreenConnection().connectionId = connectionId;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 13:53:49 +01:00
|
|
|
removeConnection(connectionId: string): StreamModel {
|
|
|
|
const removeStream = this.getConnectionById(connectionId);
|
|
|
|
this.streams.delete(removeStream.type);
|
|
|
|
return removeStream;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
hasConnectionId(connectionId: string): boolean {
|
2022-02-15 13:24:08 +01:00
|
|
|
return Array.from(this.streams.values()).some((conn) => conn.connectionId === connectionId);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-26 11:30:30 +01:00
|
|
|
getConnectionById(connectionId: string): StreamModel {
|
2022-02-15 13:24:08 +01:00
|
|
|
return Array.from(this.streams.values()).find((conn) => conn.connectionId === connectionId);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-26 11:30:30 +01:00
|
|
|
getAvailableConnections(): StreamModel[] {
|
2022-02-15 13:24:08 +01:00
|
|
|
return Array.from(this.streams.values()).filter((conn) => conn.connected);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
isLocal(): boolean {
|
2022-02-28 13:48:58 +01:00
|
|
|
return this.local;
|
|
|
|
// return Array.from(this.streams.values()).every((conn) => conn.local);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setNickname(nickname: string) {
|
2022-02-28 13:48:58 +01:00
|
|
|
this.nickname = nickname;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-28 13:48:58 +01:00
|
|
|
getNickname() {
|
|
|
|
return this.nickname;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-02-27 16:41:53 +01:00
|
|
|
setCameraPublisher(publisher: Publisher | undefined) {
|
2022-01-19 17:24:11 +01:00
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setScreenPublisher(publisher: Publisher) {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setPublisher(connType: VideoType, publisher: StreamManager) {
|
2022-02-15 13:24:08 +01:00
|
|
|
const connection = this.streams.get(connType);
|
2022-03-23 13:48:17 +01:00
|
|
|
if (connection) {
|
2022-01-19 17:24:11 +01:00
|
|
|
connection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 11:19:21 +01:00
|
|
|
isCameraActive(): boolean {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.getCameraConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
enableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = true;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
disableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = false;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 11:19:21 +01:00
|
|
|
isScreenActive(): boolean {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.getScreenConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 11:19:21 +01:00
|
|
|
enableScreen() {
|
2022-01-19 17:24:11 +01:00
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.connected = true;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
disableScreen() {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.connected = false;
|
|
|
|
}
|
2022-02-25 13:53:49 +01:00
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
setAllVideoEnlarged(enlarged: boolean) {
|
2022-02-15 13:24:08 +01:00
|
|
|
this.streams.forEach((conn) => (conn.videoEnlarged = enlarged));
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 13:53:49 +01:00
|
|
|
setCameraEnlarged(enlarged: boolean) {
|
|
|
|
this.streams.get(VideoType.CAMERA).videoEnlarged = enlarged;
|
|
|
|
}
|
2022-03-23 13:48:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-02-25 13:53:49 +01:00
|
|
|
setScreenEnlarged(enlarged: boolean) {
|
|
|
|
this.streams.get(VideoType.SCREEN).videoEnlarged = enlarged;
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
toggleVideoEnlarged(connectionId: string) {
|
2022-02-15 13:24:08 +01:00
|
|
|
this.streams.forEach((conn) => {
|
2022-01-19 17:24:11 +01:00
|
|
|
if (conn.connectionId === connectionId) {
|
|
|
|
conn.videoEnlarged = !conn.videoEnlarged;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2022-01-19 17:24:11 +01:00
|
|
|
someHasVideoEnlarged(): boolean {
|
2022-02-15 13:24:08 +01:00
|
|
|
return Array.from(this.streams.values()).some((conn) => conn.videoEnlarged);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
2022-02-28 13:06:39 +01:00
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
setMutedForcibly(muted: boolean) {
|
2022-02-28 13:06:39 +01:00
|
|
|
this.isMutedForcibly = muted;
|
|
|
|
}
|
2022-06-02 10:57:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
getRole(): OpenViduRole {
|
|
|
|
return <OpenViduRole>this.streams.get(VideoType.CAMERA)?.streamManager?.stream?.connection?.role;
|
|
|
|
}
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:48:17 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class ParticipantModel extends ParticipantAbstractModel {}
|