2022-01-19 17:24:11 +01:00
|
|
|
import { Publisher, StreamManager } from 'openvidu-browser';
|
|
|
|
import { VideoType } from './video-type.model';
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
export interface StreamModel {
|
2022-01-19 17:24:11 +01:00
|
|
|
connected: boolean;
|
|
|
|
type: VideoType;
|
|
|
|
streamManager: StreamManager;
|
|
|
|
videoEnlarged: boolean;
|
|
|
|
connectionId: string;
|
2022-02-15 16:44:09 +01:00
|
|
|
participant?: ParticipantAbstractModel
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-02 17:35:14 +01:00
|
|
|
export interface ParticipantProperties {
|
|
|
|
local: boolean;
|
|
|
|
nickname: string;
|
|
|
|
id?: string;
|
|
|
|
colorProfile?: string;
|
|
|
|
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) {
|
|
|
|
this.id = props.id ? props.id : new Date().getTime().toString();
|
|
|
|
this.local = props.local;
|
|
|
|
this.nickname = props.nickname;
|
|
|
|
this.colorProfile = !!props.colorProfile ? props.colorProfile : `hsl(${Math.random()*360}, 100%, 80%)`;
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
public isCameraAudioActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
2022-02-25 11:19:21 +01:00
|
|
|
if(cameraConnection) {
|
2022-03-10 14:12:43 +01:00
|
|
|
return cameraConnection.connected && cameraConnection.streamManager?.stream?.audioActive;
|
2022-02-25 11:19:21 +01:00
|
|
|
}
|
|
|
|
return this.isScreenAudioActive();;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public isCameraVideoActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
return cameraConnection?.connected && cameraConnection?.streamManager?.stream?.videoActive;
|
|
|
|
}
|
|
|
|
isScreenAudioActive(): boolean {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
2022-02-25 11:19:21 +01:00
|
|
|
if(screenConnection){
|
|
|
|
return screenConnection?.connected && screenConnection?.streamManager?.stream?.audioActive;
|
|
|
|
}
|
|
|
|
return false;
|
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-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-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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCameraConnectionId(connectionId: string) {
|
|
|
|
this.getCameraConnection().connectionId = connectionId;
|
|
|
|
}
|
|
|
|
setScreenConnectionId(connectionId: string) {
|
|
|
|
this.getScreenConnection().connectionId = connectionId;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
setNickname(nickname: string) {
|
2022-02-28 13:48:58 +01:00
|
|
|
this.nickname = nickname;
|
|
|
|
// this.streams.forEach((conn) => {
|
|
|
|
// if (conn.type === VideoType.CAMERA) {
|
|
|
|
// conn.nickname = nickname;
|
|
|
|
// } else {
|
|
|
|
// conn.nickname = `${nickname}_${conn.type}`;
|
|
|
|
// }
|
|
|
|
// });
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:48:58 +01:00
|
|
|
getNickname() {
|
|
|
|
return this.nickname;
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:48:58 +01:00
|
|
|
// getCameraNickname(): string {
|
|
|
|
// return this.getCameraConnection()?.nickname;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// getScreenNickname(): string {
|
|
|
|
// return this.getScreenConnection()?.nickname;
|
|
|
|
// }
|
2022-01-19 17:24:11 +01:00
|
|
|
|
|
|
|
setCameraPublisher(publisher: Publisher) {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
|
|
|
|
setScreenPublisher(publisher: Publisher) {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPublisher(connType: VideoType, publisher: StreamManager) {
|
2022-02-15 13:24:08 +01:00
|
|
|
const connection = this.streams.get(connType);
|
2022-01-19 17:24:11 +01:00
|
|
|
if(connection) {
|
|
|
|
connection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 11:19:21 +01:00
|
|
|
isCameraActive(): boolean {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.getCameraConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
enableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = false;
|
|
|
|
}
|
|
|
|
|
2022-02-25 11:19:21 +01:00
|
|
|
isScreenActive(): boolean {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.getScreenConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableScreen() {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.connected = false;
|
|
|
|
}
|
2022-02-25 13:53:49 +01:00
|
|
|
|
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-02-25 13:53:49 +01:00
|
|
|
setCameraEnlarged(enlarged: boolean) {
|
|
|
|
this.streams.get(VideoType.CAMERA).videoEnlarged = enlarged;
|
|
|
|
}
|
|
|
|
setScreenEnlarged(enlarged: boolean) {
|
|
|
|
this.streams.get(VideoType.SCREEN).videoEnlarged = enlarged;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
setMutedForcibly(muted: boolean){
|
|
|
|
this.isMutedForcibly = muted;
|
|
|
|
}
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ParticipantModel extends ParticipantAbstractModel {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|