Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ENGDESK-26634): add custom headers to invite and answer messages #338

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/js/src/Modules/Verto/messages/verto/BaseRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const tmpMap = {
remoteCallerNumber: 'remote_caller_id_number',
callerName: 'caller_id_name',
callerNumber: 'caller_id_number',
customHeaders: 'custom_headers',
};

export default abstract class BaseRequest extends BaseMessage {
Expand All @@ -24,12 +25,14 @@ export default abstract class BaseRequest extends BaseMessage {
speakerId,
...dialogParams
} = params.dialogParams;

for (const key in tmpMap) {
if (key && dialogParams.hasOwnProperty(key)) {
dialogParams[tmpMap[key]] = dialogParams[key];
delete dialogParams[key];
}
}

params.dialogParams = dialogParams;
}

Expand Down
63 changes: 40 additions & 23 deletions packages/js/src/Modules/Verto/webrtc/BaseCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ import {
stopAudio,
} from './helpers';
import { objEmpty, mutateLiveArrayData, isFunction } from '../util/helpers';
import { IVertoCallOptions, IWebRTCCall, IAudio, IStatsBinding } from './interfaces';
import {
IVertoCallOptions,
IWebRTCCall,
IAudio,
IStatsBinding,
AnswerParams,
} from './interfaces';
import {
attachMediaStream,
detachMediaStream,
Expand Down Expand Up @@ -130,8 +136,8 @@ export default abstract class BaseCall implements IWebRTCCall {
private _ringback: IAudio;

private _statsBindings: IStatsBinding[] = [];
private _statsIntervalId: any = null;

private _statsIntervalId: any = null;

constructor(protected session: BrowserSession, opts?: IVertoCallOptions) {
const {
Expand Down Expand Up @@ -255,10 +261,20 @@ export default abstract class BaseCall implements IWebRTCCall {
* call.answer()
* ```
*/
answer() {
answer(params: AnswerParams = {}) {
this.stopRingtone();

this.direction = Direction.Inbound;

this.options = {
...this.options,
customHeaders: [
...(params.customHeaders
? params.customHeaders
: this.options.customHeaders),
],
};

this.peer = new Peer(PeerType.Answer, this.options);
this._registerPeerEvents();
}
Expand Down Expand Up @@ -326,7 +342,7 @@ export default abstract class BaseCall implements IWebRTCCall {
});
this._execute(bye)
.catch((error) => {
logger.error('telnyl_rtc.bye failed!', error)
logger.error('telnyl_rtc.bye failed!', error);
trigger(SwEvent.Error, error, this.session.uuid);
})
.then(_close.bind(this));
Expand Down Expand Up @@ -783,18 +799,21 @@ export default abstract class BaseCall implements IWebRTCCall {

/**
* Registers callback for stats.
*
* @param callback
* @param constraints
* @returns
*
* @param callback
* @param constraints
* @returns
*/
getStats(callback: Function, constraints: any) {
if (!callback) {
return;
}
const binding: IStatsBinding = { callback: callback, constraints: constraints };
const binding: IStatsBinding = {
callback: callback,
constraints: constraints,
};
this._statsBindings.push(binding);

if (!this._statsIntervalId) {
const STATS_INTERVAL = 2000;
this._startStats(STATS_INTERVAL);
Expand Down Expand Up @@ -1354,6 +1373,7 @@ export default abstract class BaseCall implements IWebRTCCall {
this.peer.instance.removeEventListener('icecandidate', this._onIce);

let msg = null;

const tmpParams = {
sessid: this.session.sessionid,
sdp,
Expand Down Expand Up @@ -1476,12 +1496,8 @@ export default abstract class BaseCall implements IWebRTCCall {
}

private _init() {
const {
id,
userVariables,
remoteCallerNumber,
onNotification,
} = this.options;
const { id, userVariables, remoteCallerNumber, onNotification } =
this.options;
if (!id) {
this.options.id = uuidv4();
}
Expand Down Expand Up @@ -1528,7 +1544,7 @@ export default abstract class BaseCall implements IWebRTCCall {
clearInterval(this._statsIntervalId);
this._statsIntervalId = null;
}
logger.info('Stats stopped')
logger.info('Stats stopped');
}

private _doStats = () => {
Expand All @@ -1543,16 +1559,17 @@ export default abstract class BaseCall implements IWebRTCCall {
}

this.peer.instance.getStats().then((res) => {

res.forEach((report) => {

this._statsBindings.forEach((binding) => {
this._statsBindings.forEach((binding) => {
if (!binding.callback) {
return;
}
if (binding.constraints) {
for (var key in binding.constraints) {
if (binding.constraints.hasOwnProperty(key) && (binding.constraints[key] !== report[key])) {
if (
binding.constraints.hasOwnProperty(key) &&
binding.constraints[key] !== report[key]
) {
return;
}
}
Expand All @@ -1561,7 +1578,7 @@ export default abstract class BaseCall implements IWebRTCCall {
});
});
});
}
};

static setStateTelnyx = (call: Call) => {
if (!call) {
Expand Down
8 changes: 8 additions & 0 deletions packages/js/src/Modules/Verto/webrtc/VertoHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class VertoHandler {
callOptions.clientState = params.client_state;
}

if (
params.dialogParams &&
params.dialogParams.custom_headers &&
params.dialogParams.custom_headers.length
) {
callOptions.customHeaders = params.dialogParams.custom_headers;
}

const call = new Call(session, callOptions);
call.nodeId = this.nodeId;
return call;
Expand Down
7 changes: 6 additions & 1 deletion packages/js/src/Modules/Verto/webrtc/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ export interface IVertoCallOptions {
negotiateAudio?: boolean;
negotiateVideo?: boolean;
mediaSettings?: IMediaSettings;
customHeaders?: Array<{ name: string; value: string }>;
}

export interface IStatsBinding {
constraints: any;
callback: Function;
}

export interface AnswerParams {
customHeaders?: Array<{ name: string; value: string }>;
}

export interface IWebRTCCall {
id: string;
state: string;
Expand All @@ -65,7 +70,7 @@ export interface IWebRTCCall {
localStream: MediaStream;
remoteStream: MediaStream;
invite: () => void;
answer: () => void;
answer: (params: AnswerParams) => void;
hangup: (params: any, execute: boolean) => void;
transfer: (destination: string) => void;
replace: (replaceCallID: string) => void;
Expand Down
5 changes: 5 additions & 0 deletions packages/js/src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export interface ICallOptions {
useSdpASBandwidthKbps?: boolean;
sdpASBandwidthKbps?: number;
};

/**
* Add custom headers to the INVITE and ANSWER request.
*/
customHeaders?: { name: string; value: string }[];
}

/**
Expand Down
Loading