Skip to content

Commit

Permalink
chore: wip add support for late sdp offer in rtprelay module
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Mar 5, 2024
1 parent f1e1d36 commit ba75922
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
13 changes: 10 additions & 3 deletions mods/processor/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
20 changes: 15 additions & 5 deletions mods/rtprelay/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,39 @@ 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<RTPEFunctionResult> => {
return await rtpe.query(config, {
"call-id": request.message.callId.callId
})
}
}

export function offer(config: RTPEConfig, invertTags = false) {
return async (request: MessageRequest): Promise<RTPEFunctionResult> => {
const direction = getDirectionFromRequest(request)
const sdpModifiers = getRTPEParamsByDirection(direction)
return await rtpe.offer(config, {
...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<RTPEFunctionResult> => {
const direction = getDirectionFromResponse(request)
const sdpModifiers = getRTPEParamsByDirection(direction)
return await rtpe.answer(config, {
...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
})
}
}
Expand Down
24 changes: 18 additions & 6 deletions mods/rtprelay/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"])
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion mods/rtprelay/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ export type RTPEFunctionResult =
| { result: "error"; "error-reason": string }

export type RTPEFunction = (
config: RTPEConfig
config: RTPEConfig,
invertTags?: boolean
) => (req: MessageRequest) => Promise<RTPEFunctionResult>

0 comments on commit ba75922

Please sign in to comment.