Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 11 additions & 33 deletions packages/media-signaling/src/definition/call/IClientMediaCall.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import type { Emitter } from '@rocket.chat/emitter';

import type { CallEvents } from './CallEvents';
import type { IMediaStreamWrapper } from '../media/IMediaStreamWrapper';

export type CallActorType = 'user' | 'sip';

export type CallContact = {
type?: CallActorType;
id?: string;
contractId?: string;

displayName?: string;
username?: string;
sipExtension?: string;
};

export type CallRole = 'caller' | 'callee';
import type {
AnyClientMediaCallParticipant,
IClientMediaCallLocalParticipant,
IClientMediaCallRemoteParticipant,
} from './IClientMediaCallParticipant';
import type { CallActorType } from './common';

export type CallService = 'webrtc';

Expand Down Expand Up @@ -77,26 +68,13 @@ export type CallFlag = 'internal' | 'create-data-channel';

export interface IClientMediaCall {
callId: string;
role: CallRole;
service: CallService | null;
flags: readonly CallFlag[];
features: readonly CallFeature[];

state: CallState;
ignored: boolean;
signed: boolean;
hidden: boolean;
muted: boolean;
/* if the call was put on hold */
held: boolean;
/* busy = state >= 'accepted' && state < 'hangup' */
busy: boolean;
/* if the other side has put the call on hold */
remoteHeld: boolean;
remoteMute: boolean;

contact: CallContact;
transferredBy: CallContact | null;

/** The timestamp of the moment the call was marked as active for the first time */
activeTimestamp?: Date;
Expand All @@ -108,14 +86,9 @@ export interface IClientMediaCall {

emitter: Emitter<CallEvents>;

getLocalMediaStream(tag?: string): IMediaStreamWrapper | null;
getRemoteMediaStream(tag?: string): IMediaStreamWrapper | null;

accept(): void;
reject(): void;
hangup(): void;
setMuted(muted: boolean): void;
setHeld(onHold: boolean): void;
requestScreenShare(requested: boolean): void;
setScreenVideoTrack(videoTrack: MediaStreamTrack | null): Promise<void>;
hasScreenVideoTrack(): boolean;
Expand All @@ -126,4 +99,9 @@ export interface IClientMediaCall {

getStats(selector?: MediaStreamTrack | null): Promise<RTCStatsReport | null>;
isFeatureAvailable(feature: CallFeature): boolean;
hasFlag(flag: CallFlag): boolean;

readonly localParticipant: IClientMediaCallLocalParticipant;
readonly remoteParticipants: IClientMediaCallRemoteParticipant[];
readonly participants: AnyClientMediaCallParticipant[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { IMediaStreamWrapper } from '../media';
import type { CallActorType, CallContact, CallRole } from './common';

export interface IClientMediaCallParticipant {
readonly local: boolean;

readonly participantId: string;

readonly actorType: CallActorType;

readonly actorId: string;

readonly role: CallRole;

readonly muted: boolean;

readonly held: boolean;

readonly contact: CallContact;

getMediaStream(tag?: string): IMediaStreamWrapper | null;
}

export interface IClientMediaCallLocalParticipant extends IClientMediaCallParticipant {
readonly local: true;

setMuted(muted: boolean): void;
setHeld(onHold: boolean): void;
}

export interface IClientMediaCallRemoteParticipant extends IClientMediaCallParticipant {
readonly local: false;
}

export type AnyClientMediaCallParticipant = IClientMediaCallLocalParticipant | IClientMediaCallRemoteParticipant;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { IDirectMediaCallData } from './IDirectMediaCallData';
import type { ITempMediaCallData } from './ITempMediaCallData';

export type AnyMediaCallData = ITempMediaCallData | IDirectMediaCallData;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { CallFeature, CallFlag, CallService, CallState } from '../IClientMediaCall';
import type { IClientMediaCallLocalParticipant, IClientMediaCallRemoteParticipant } from '../IClientMediaCallParticipant';
import type { CallContact } from '../common';

export interface IDirectMediaCallData {
readonly confirmed: true;

readonly callId: string;
readonly service: CallService | null;
readonly flags: readonly CallFlag[];
readonly features: readonly CallFeature[];
readonly state: CallState;
readonly hidden: boolean;

readonly transferredBy: CallContact | null;

readonly activeTimestamp?: Date;

readonly tempCallId: string;

readonly localParticipant: IClientMediaCallLocalParticipant;
readonly remoteParticipant: IClientMediaCallRemoteParticipant;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CallState } from '../IClientMediaCall';
import type { IClientMediaCallLocalParticipant } from '../IClientMediaCallParticipant';

export interface ITempMediaCallData {
readonly confirmed: false;
readonly tempCallId: string;

readonly state: CallState;

readonly title: string;

readonly localParticipant: IClientMediaCallLocalParticipant;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type * from './AnyMediaCallData';
export type * from './IDirectMediaCallData';
export type * from './ITempMediaCallData';
13 changes: 13 additions & 0 deletions packages/media-signaling/src/definition/call/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type CallActorType = 'user' | 'sip';

export type CallContact = {
type?: CallActorType;
id?: string;
contractId?: string;

displayName?: string;
username?: string;
sipExtension?: string;
};

export type CallRole = 'caller' | 'callee';
3 changes: 3 additions & 0 deletions packages/media-signaling/src/definition/call/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export type * from './CallEvents';
export type * from './callStates';
export type * from './common';
export * from './IClientMediaCall';
export type * from './IClientMediaCallParticipant';
142 changes: 142 additions & 0 deletions packages/media-signaling/src/lib/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import type {
CallActorType,
CallFlag,
CallFeature,
IClientMediaCallLocalParticipant,
IClientMediaCallRemoteParticipant,
AnyClientMediaCallParticipant,
} from '../definition/call';
import type { AnyMediaCallData } from '../definition/call/callStates';
import type { ClientContractState, ClientState } from '../definition/client';
import type { IMediaSignalLogger } from '../definition/logger';
import type { MediaStreamIdentification, IMediaStreamWrapper } from '../definition/media';
Expand All @@ -30,6 +34,7 @@ import type {
} from '../definition/signals/server';

export interface IClientMediaCallConfig {
userId: string;
logger?: IMediaSignalLogger;
transporter: MediaSignalTransportWrapper;
processorFactories: IServiceProcessorFactoryList;
Expand Down Expand Up @@ -216,6 +221,54 @@ export class ClientMediaCall implements IClientMediaCall {
return [...(this.enabledFeatures || [])];
}

public readonly localParticipant: IClientMediaCallLocalParticipant;

private selfContact: CallContact | null;

private remoteParticipant: IClientMediaCallRemoteParticipant | null;

public get remoteParticipants(): IClientMediaCallRemoteParticipant[] {
if (!this.remoteParticipant) {
return [];
}

return [this.remoteParticipant];
}

public get participants(): AnyClientMediaCallParticipant[] {
return [this.localParticipant, ...this.remoteParticipants];
}

public get callStateData(): AnyMediaCallData {
if (!this.confirmed || !this.remoteParticipant) {
const number = this.contact.type === 'sip' ? this.contact.id : '';

return {
confirmed: false,
tempCallId: this.tempCallId,
state: this.state,
title: this.contact.displayName || number || 'unknown',
localParticipant: this.localParticipant,
};
}

return {
confirmed: this.confirmed,
callId: this.callId,
service: this.service,
flags: this.flags,
features: this.features,
state: this.state,
transferredBy: this.transferredBy,
activeTimestamp: this.activeTimestamp,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
tempCallId: this.tempCallId,
hidden: this.hidden,

localParticipant: this.localParticipant,
remoteParticipant: this.remoteParticipant,
};
}

constructor(
private readonly config: IClientMediaCallConfig,
callId: string,
Expand Down Expand Up @@ -256,6 +309,9 @@ export class ClientMediaCall implements IClientMediaCall {
this._remoteHeld = false;
this._remoteMute = false;
this._flags = [];
this.selfContact = null;
this.localParticipant = this.createLocalParticipantProxy();
this.remoteParticipant = null;

this.negotiationManager = new NegotiationManager(this, { logger: config.logger });
}
Expand Down Expand Up @@ -325,6 +381,7 @@ export class ClientMediaCall implements IClientMediaCall {
this._service = signal.service;
this._role = signal.role;
this._flags = signal.flags || [];
this.selfContact = signal.self || { type: 'user', id: this.config.userId };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

this._transferredBy = signal.transferredBy || null;

Expand All @@ -343,6 +400,8 @@ export class ClientMediaCall implements IClientMediaCall {

this.changeContact(signal.contact);

this.remoteParticipant = this.createRemoteParticipantProxy();

// If the call is already flagged as over before the initialization, do not process anything other than filling in the basic information
if (this.isOver()) {
return;
Expand Down Expand Up @@ -820,6 +879,14 @@ export class ClientMediaCall implements IClientMediaCall {
return this.enabledFeatures.includes(feature);
}

public hasFlag(flag: CallFlag): boolean {
if (!this.flags) {
return false;
}

return this.flags.includes(flag);
}

private changeState(newState: CallState): void {
if (newState === this._state) {
return;
Expand Down Expand Up @@ -1316,6 +1383,81 @@ export class ClientMediaCall implements IClientMediaCall {
return this.signed;
}

private createLocalParticipantProxy(): IClientMediaCallLocalParticipant {
const localParticipant: IClientMediaCallLocalParticipant = {
local: true,
participantId: this.config.userId,
actorType: 'user',
actorId: this.config.userId,
role: this._role,
muted: this.muted,
held: this.held,
contact: this.selfContact || { type: 'user', id: this.config.userId },
getMediaStream: (tag?: string) => this.getLocalMediaStream(tag),
setMuted: (muted: boolean) => this.setMuted(muted),
setHeld: (held: boolean) => this.setHeld(held),
};

return new Proxy<IClientMediaCallLocalParticipant>(localParticipant, {
get: (target: typeof localParticipant, prop: keyof typeof localParticipant, receiver): any => {
switch (prop) {
case 'role':
return this._role;
case 'contact':
return this.selfContact || { type: 'user', id: this.config.userId };
case 'muted':
return this.muted;
case 'held':
return this.held;
default:
return Reflect.get(target, prop, receiver);
}
},
});
}

private createRemoteParticipantProxy(): IClientMediaCallRemoteParticipant {
if (!this.hasRemoteData) {
throw new Error('Unable to initialize remote participant without remote data');
}

const { type: actorType, id: actorId } = this.contact;

if (!actorType || !actorId) {
throw new Error('Unable to initialize remote participant without actor identification');
}

const participantId = actorType === 'user' ? actorId : `${actorType}/${actorId}`;
const role = this._role === 'callee' ? 'caller' : 'callee';

const remote: IClientMediaCallRemoteParticipant = {
local: false,
participantId,
actorType,
actorId,
role,
muted: this.remoteMute,
held: this.remoteHeld,
contact: this.contact,
getMediaStream: (tag?: string) => this.getRemoteMediaStream(tag),
};

return new Proxy<IClientMediaCallRemoteParticipant>(remote, {
get: (target: typeof remote, prop: keyof typeof remote, receiver): any => {
switch (prop) {
case 'contact':
return this.contact;
case 'muted':
return this.remoteMute;
case 'held':
return this.remoteHeld;
default:
return Reflect.get(target, prop, receiver);
}
},
});
}

private mayUseStreams(): this is ClientMediaCallWebRTC {
if (this.hidden || !this.signed) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/media-signaling/src/lib/NegotiationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class NegotiationManager {
}

protected isPoliteClient(): boolean {
return this.call.role === 'callee';
return this.call.localParticipant.role === 'callee';
}

protected addToQueue(negotiation: Negotiation): void {
Expand Down
Loading
Loading