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
|
|
|
local: boolean;
|
|
|
|
connected: boolean;
|
|
|
|
nickname: string;
|
|
|
|
type: VideoType;
|
|
|
|
streamManager: StreamManager;
|
|
|
|
videoEnlarged: boolean;
|
|
|
|
connectionId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class ParticipantAbstractModel {
|
2022-01-26 11:30:30 +01:00
|
|
|
connections: Map<VideoType, StreamModel> = new Map();
|
2022-01-19 17:24:11 +01:00
|
|
|
id: string;
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
addConnection(streamModel: StreamModel) {
|
|
|
|
this.connections.set(streamModel.type, streamModel);
|
2022-01-19 17:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public isCameraAudioActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
return cameraConnection.connected && cameraConnection.streamManager.stream.audioActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isCameraVideoActive(): boolean {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
return cameraConnection?.connected && cameraConnection?.streamManager?.stream?.videoActive;
|
|
|
|
}
|
|
|
|
isScreenAudioActive(): boolean {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
return screenConnection?.connected && screenConnection?.streamManager?.stream?.audioActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasConnectionType(type: VideoType): boolean {
|
|
|
|
return this.connections.has(type);
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
public getCameraConnection(): StreamModel {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.connections.get(VideoType.CAMERA);
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
public getScreenConnection(): StreamModel {
|
2022-01-19 17:24:11 +01:00
|
|
|
return this.connections.get(VideoType.SCREEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
getConnectionTypesEnabled(): VideoType[] {
|
|
|
|
let connType = [];
|
|
|
|
if (this.isCameraEnabled()) connType.push(VideoType.CAMERA);
|
|
|
|
if (this.isScreenEnabled()) connType.push(VideoType.SCREEN);
|
|
|
|
|
|
|
|
return connType;
|
|
|
|
}
|
|
|
|
|
|
|
|
setCameraConnectionId(connectionId: string) {
|
|
|
|
this.getCameraConnection().connectionId = connectionId;
|
|
|
|
}
|
|
|
|
setScreenConnectionId(connectionId: string) {
|
|
|
|
this.getScreenConnection().connectionId = connectionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeConnection(connectionId: string) {
|
|
|
|
this.connections.delete(this.getConnectionById(connectionId).type);
|
|
|
|
}
|
|
|
|
|
|
|
|
hasConnectionId(connectionId: string): boolean {
|
|
|
|
return Array.from(this.connections.values()).some((conn) => conn.connectionId === connectionId);
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
getConnectionById(connectionId: string): StreamModel {
|
2022-01-19 17:24:11 +01:00
|
|
|
return Array.from(this.connections.values()).find((conn) => conn.connectionId === connectionId);
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
getAvailableConnections(): StreamModel[] {
|
2022-01-19 17:24:11 +01:00
|
|
|
return Array.from(this.connections.values()).filter((conn) => conn.connected);
|
|
|
|
}
|
|
|
|
|
|
|
|
isLocal(): boolean {
|
|
|
|
return Array.from(this.connections.values()).every((conn) => conn.local);
|
|
|
|
}
|
|
|
|
|
|
|
|
setNickname(nickname: string) {
|
|
|
|
this.connections.forEach((conn) => {
|
|
|
|
if (conn.type === VideoType.CAMERA) {
|
|
|
|
conn.nickname = nickname;
|
|
|
|
} else {
|
|
|
|
conn.nickname = `${nickname}_${conn.type}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCameraNickname(): string {
|
|
|
|
return this.getCameraConnection()?.nickname;
|
|
|
|
}
|
|
|
|
|
|
|
|
getScreenNickname(): string {
|
|
|
|
return this.getScreenConnection()?.nickname;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const connection = this.connections.get(connType);
|
|
|
|
if(connection) {
|
|
|
|
connection.streamManager = publisher;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isCameraEnabled(): boolean {
|
|
|
|
return this.getCameraConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
enableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableCamera() {
|
|
|
|
const cameraConnection = this.getCameraConnection();
|
|
|
|
if (cameraConnection) cameraConnection.connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
isScreenEnabled(): boolean {
|
|
|
|
return this.getScreenConnection()?.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
enablescreen() {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.connected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disableScreen() {
|
|
|
|
const screenConnection = this.getScreenConnection();
|
|
|
|
if (screenConnection) screenConnection.connected = false;
|
|
|
|
}
|
|
|
|
setAllVideoEnlarged(enlarged: boolean) {
|
|
|
|
this.connections.forEach((conn) => (conn.videoEnlarged = enlarged));
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleVideoEnlarged(connectionId: string) {
|
|
|
|
this.connections.forEach((conn) => {
|
|
|
|
if (conn.connectionId === connectionId) {
|
|
|
|
conn.videoEnlarged = !conn.videoEnlarged;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
someHasVideoEnlarged(): boolean {
|
|
|
|
return Array.from(this.connections.values()).some((conn) => conn.videoEnlarged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ParticipantModel extends ParticipantAbstractModel {
|
|
|
|
|
2022-01-26 11:30:30 +01:00
|
|
|
constructor(model?: StreamModel, id?: string) {
|
2022-01-19 17:24:11 +01:00
|
|
|
super();
|
2022-01-26 11:30:30 +01:00
|
|
|
let streamModel: StreamModel = {
|
|
|
|
local: model ? model.local : true,
|
2022-01-19 17:24:11 +01:00
|
|
|
connected: true,
|
2022-01-26 11:30:30 +01:00
|
|
|
nickname: model ? model.nickname : 'OpenVidu_User',
|
|
|
|
type: model ? model.type : VideoType.CAMERA,
|
|
|
|
streamManager: model ? model.streamManager : null,
|
|
|
|
videoEnlarged: model ? model.videoEnlarged : false,
|
|
|
|
connectionId: model ? model.connectionId : null
|
2022-01-19 17:24:11 +01:00
|
|
|
};
|
2022-01-26 11:30:30 +01:00
|
|
|
this.connections.set(streamModel.type, streamModel);
|
2022-01-19 17:24:11 +01:00
|
|
|
this.id = id ? id : new Date().getTime().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|