From ba75922a273063af9793dd069f17d63c09a437fd Mon Sep 17 00:00:00 2001 From: Pedro Sanders Date: Tue, 5 Mar 2024 00:33:25 -0500 Subject: [PATCH] chore: wip add support for late sdp offer in rtprelay module --- mods/processor/src/helper.ts | 13 ++++++++++--- mods/rtprelay/src/client.ts | 20 +++++++++++++++----- mods/rtprelay/src/service.ts | 24 ++++++++++++++++++------ mods/rtprelay/src/types.ts | 3 ++- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/mods/processor/src/helper.ts b/mods/processor/src/helper.ts index bf4f1ae0b..19d29e1d6 100644 --- a/mods/processor/src/helper.ts +++ b/mods/processor/src/helper.ts @@ -74,10 +74,17 @@ export const isRinging = (request: MessageRequest) => export const isWebRTC = (transport: Transport) => transport === Transport.WS || transport === Transport.WSS +export const isInviteWithSDP = (request: MessageRequest) => + isTypeRequest(request) && hasSDP(request) && request.method === Method.INVITE + +export const isAckWithSDP = (request: MessageRequest) => + isTypeRequest(request) && hasSDP(request) && request.method === Method.ACK + export const isInviteOrAckWithSDP = (request: MessageRequest) => - isTypeRequest(request) && - hasSDP(request) && - (request.method === Method.INVITE || request.method === Method.ACK) + isInviteWithSDP(request) || isAckWithSDP(request) + +export const isResponseWithSDP = (request: MessageRequest) => + isTypeResponse(request) && hasSDP(request) export const isOkOrRingingWithSDP = (request: MessageRequest) => isTypeResponse(request) && diff --git a/mods/rtprelay/src/client.ts b/mods/rtprelay/src/client.ts index a60bd3c52..8f62872c2 100644 --- a/mods/rtprelay/src/client.ts +++ b/mods/rtprelay/src/client.ts @@ -29,7 +29,15 @@ import { const Client = require("rtpengine-client").Client const rtpe = new Client({ timeout: RTPENGINE_TIMEOUT }) -export function offer(config: RTPEConfig) { +export function query(config: RTPEConfig) { + return async (request: MessageRequest): Promise => { + return await rtpe.query(config, { + "call-id": request.message.callId.callId + }) + } +} + +export function offer(config: RTPEConfig, invertTags = false) { return async (request: MessageRequest): Promise => { const direction = getDirectionFromRequest(request) const sdpModifiers = getRTPEParamsByDirection(direction) @@ -37,12 +45,12 @@ export function offer(config: RTPEConfig) { ...sdpModifiers, sdp: request.message.body, "call-id": request.message.callId.callId, - "from-tag": request.message.from.tag + "from-tag": invertTags ? request.message.to.tag : request.message.from.tag }) } } -export function answer(config: RTPEConfig) { +export function answer(config: RTPEConfig, invertTags = false) { return async (request: MessageRequest): Promise => { const direction = getDirectionFromResponse(request) const sdpModifiers = getRTPEParamsByDirection(direction) @@ -50,8 +58,10 @@ export function answer(config: RTPEConfig) { ...sdpModifiers, sdp: request.message.body, "call-id": request.message.callId.callId, - "from-tag": request.message.from.tag, - "to-tag": request.message.to.tag + "from-tag": invertTags + ? request.message.to.tag + : request.message.from.tag, + "to-tag": invertTags ? request.message.from.tag : request.message.to.tag }) } } diff --git a/mods/rtprelay/src/service.ts b/mods/rtprelay/src/service.ts index e0be510ee..df422b649 100644 --- a/mods/rtprelay/src/service.ts +++ b/mods/rtprelay/src/service.ts @@ -24,7 +24,7 @@ import Processor, { MessageRequest, Response } from "@routr/processor" -import { offer, answer, del } from "./client" +import { offer, answer, del, query } from "./client" import { CommonErrors as CE } from "@routr/common" import { Direction, RTPEFunction } from "./types" import { getLogger } from "@fonoster/logger" @@ -59,8 +59,8 @@ export default function rtprelay( ? getDirectionFromResponse(req) : getDirectionFromRequest(req) - const sendRequest = async (f: RTPEFunction) => { - const r = await f(rtpeConfig)(req) + const sendRequest = async (f: RTPEFunction, invertTags = false) => { + const r = await f(rtpeConfig, invertTags)(req) if (r.result === "error") { throw new CE.BadRequestError(r["error-reason"]) @@ -71,14 +71,26 @@ export default function rtprelay( return res.send(req) } - if (H.isInviteOrAckWithSDP(req)) { + const queryResponse = await query(rtpeConfig)(req) + const isNewCall = queryResponse.result === "error" + + logger.verbose("request", { + direction, + isNewCall, + type: H.isTypeRequest(req) ? req.method : req.message.responseType + }) + + if (H.isInviteWithSDP(req)) { return await sendRequest(offer) + } else if (H.isAckWithSDP(req)) { + return await sendRequest(answer, true) } else if (H.isRinging(req) && direction === Direction.WEB_TO_PHONE) { // FIXME: This was added to prevent the ringing message from being sent to the // caller while the call is being established. This is a temporary fix req.message.body = "" - return res.send(req) - } else if (H.isOkOrRingingWithSDP(req)) { + } else if (H.isResponseWithSDP(req) && isNewCall) { + return await sendRequest(offer, true) + } else if (H.isResponseWithSDP(req)) { return await sendRequest(answer) } else if (H.isBye(req)) { return await sendRequest(del) diff --git a/mods/rtprelay/src/types.ts b/mods/rtprelay/src/types.ts index 7eacb15c8..eb9b17caa 100644 --- a/mods/rtprelay/src/types.ts +++ b/mods/rtprelay/src/types.ts @@ -54,5 +54,6 @@ export type RTPEFunctionResult = | { result: "error"; "error-reason": string } export type RTPEFunction = ( - config: RTPEConfig + config: RTPEConfig, + invertTags?: boolean ) => (req: MessageRequest) => Promise