Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
90fff361
Unverified
提交
90fff361
authored
12月 30, 2023
作者:
Iñaki Baz Castillo
提交者:
GitHub
12月 30, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid modification of user input data (#1285)
上级
2b0f4692
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
1032 行增加
和
1092 行删除
+1032
-1092
Transport.ts
node/src/Transport.ts
+41
-23
Worker.ts
node/src/Worker.ts
+13
-8
index.ts
node/src/index.ts
+1
-1
ortc.ts
node/src/ortc.ts
+974
-1058
test-ortc.ts
node/src/tests/test-ortc.ts
+3
-2
没有找到文件。
node/src/Transport.ts
浏览文件 @
90fff361
...
...
@@ -714,17 +714,20 @@ export class Transport
throw
new
TypeError
(
'if given, appData must be an object'
);
}
// Clone given RTP parameters to not modify input data.
const
clonedRtpParameters
=
utils
.
clone
<
RtpParameters
>
(
rtpParameters
);
// This may throw.
ortc
.
validateRtpParameters
(
r
tpParameters
);
ortc
.
validateRtpParameters
(
clonedR
tpParameters
);
// If missing or empty encodings, add one.
if
(
!
r
tpParameters
.
encodings
||
!
Array
.
isArray
(
r
tpParameters
.
encodings
)
||
r
tpParameters
.
encodings
.
length
===
0
!
clonedR
tpParameters
.
encodings
||
!
Array
.
isArray
(
clonedR
tpParameters
.
encodings
)
||
clonedR
tpParameters
.
encodings
.
length
===
0
)
{
r
tpParameters
.
encodings
=
[
{}
];
clonedR
tpParameters
.
encodings
=
[
{}
];
}
// Don't do this in PipeTransports since there we must keep CNAME value in
...
...
@@ -733,9 +736,13 @@ export class Transport
{
// If CNAME is given and we don't have yet a CNAME for Producers in this
// Transport, take it.
if
(
!
this
.
#
cnameForProducers
&&
rtpParameters
.
rtcp
&&
rtpParameters
.
rtcp
.
cname
)
if
(
!
this
.
#
cnameForProducers
&&
clonedRtpParameters
.
rtcp
&&
clonedRtpParameters
.
rtcp
.
cname
)
{
this
.
#
cnameForProducers
=
r
tpParameters
.
rtcp
.
cname
;
this
.
#
cnameForProducers
=
clonedR
tpParameters
.
rtcp
.
cname
;
}
// Otherwise if we don't have yet a CNAME for Producers and the RTP
// parameters do not include CNAME, create a random one.
...
...
@@ -745,26 +752,26 @@ export class Transport
}
// Override Producer's CNAME.
rtpParameters
.
rtcp
=
rtpParameters
.
rtcp
||
{};
r
tpParameters
.
rtcp
.
cname
=
this
.
#
cnameForProducers
;
clonedRtpParameters
.
rtcp
=
clonedRtpParameters
.
rtcp
??
{};
clonedR
tpParameters
.
rtcp
.
cname
=
this
.
#
cnameForProducers
;
}
const
routerRtpCapabilities
=
this
.
#
getRouterRtpCapabilities
();
// This may throw.
const
rtpMapping
=
ortc
.
getProducerRtpParametersMapping
(
r
tpParameters
,
routerRtpCapabilities
);
clonedR
tpParameters
,
routerRtpCapabilities
);
// This may throw.
const
consumableRtpParameters
=
ortc
.
getConsumableRtpParameters
(
kind
,
r
tpParameters
,
routerRtpCapabilities
,
rtpMapping
);
kind
,
clonedR
tpParameters
,
routerRtpCapabilities
,
rtpMapping
);
const
producerId
=
id
||
utils
.
generateUUIDv4
();
const
requestOffset
=
createProduceRequest
({
builder
:
this
.
channel
.
bufferBuilder
,
builder
:
this
.
channel
.
bufferBuilder
,
producerId
,
kind
,
rtpParameters
,
rtpParameters
:
clonedRtpParameters
,
rtpMapping
,
keyFrameRequestDelay
,
paused
...
...
@@ -787,8 +794,8 @@ export class Transport
const
data
=
{
kind
,
rtpParameters
,
type
:
producerTypeFromFbs
(
status
.
type
),
rtpParameters
:
clonedRtpParameters
,
type
:
producerTypeFromFbs
(
status
.
type
),
consumableRtpParameters
};
...
...
@@ -800,7 +807,7 @@ export class Transport
producerId
},
data
,
channel
:
this
.
channel
,
channel
:
this
.
channel
,
appData
,
paused
});
...
...
@@ -854,8 +861,11 @@ export class Transport
throw
new
TypeError
(
'if given, mid must be non empty string'
);
}
// Clone given RTP capabilities to not modify input data.
const
clonedRtpCapabilities
=
utils
.
clone
<
RtpCapabilities
>
(
rtpCapabilities
);
// This may throw.
ortc
.
validateRtpCapabilities
(
rtpCapabilities
!
);
ortc
.
validateRtpCapabilities
(
clonedRtpCapabilities
);
const
producer
=
this
.
getProducerById
(
producerId
);
...
...
@@ -874,7 +884,7 @@ export class Transport
const
rtpParameters
=
ortc
.
getConsumerRtpParameters
(
{
consumableRtpParameters
:
producer
.
consumableRtpParameters
,
remoteRtpCapabilities
:
rtpCapabilities
!
,
remoteRtpCapabilities
:
clonedRtpCapabilities
,
pipe
,
enableRtx
}
...
...
@@ -996,13 +1006,17 @@ export class Transport
let
type
:
DataProducerType
;
// Clone given SCTP stream parameters to not modify input data.
let
clonedSctpStreamParameters
=
utils
.
clone
<
SctpStreamParameters
|
undefined
>
(
sctpStreamParameters
);
// If this is not a DirectTransport, sctpStreamParameters are required.
if
(
this
.
constructor
.
name
!==
'DirectTransport'
)
{
type
=
'sctp'
;
// This may throw.
ortc
.
validateSctpStreamParameters
(
s
ctpStreamParameters
!
);
ortc
.
validateSctpStreamParameters
(
clonedS
ctpStreamParameters
!
);
}
// If this is a DirectTransport, sctpStreamParameters must not be given.
else
...
...
@@ -1013,15 +1027,17 @@ export class Transport
{
logger
.
warn
(
'produceData() | sctpStreamParameters are ignored when producing data on a DirectTransport'
);
clonedSctpStreamParameters
=
undefined
;
}
}
const
dataProducerId
=
id
||
utils
.
generateUUIDv4
();
const
requestOffset
=
createProduceDataRequest
({
builder
:
this
.
channel
.
bufferBuilder
,
builder
:
this
.
channel
.
bufferBuilder
,
dataProducerId
,
type
,
sctpStreamParameters
,
sctpStreamParameters
:
clonedSctpStreamParameters
,
label
,
protocol
,
paused
...
...
@@ -1117,8 +1133,10 @@ export class Transport
if
(
this
.
constructor
.
name
!==
'DirectTransport'
)
{
type
=
'sctp'
;
sctpStreamParameters
=
(
utils
.
clone
(
dataProducer
.
sctpStreamParameters
)
??
{})
as
SctpStreamParameters
;
sctpStreamParameters
=
utils
.
clone
<
SctpStreamParameters
|
undefined
>
(
dataProducer
.
sctpStreamParameters
)
??
{}
as
SctpStreamParameters
;
// Override if given.
if
(
ordered
!==
undefined
)
...
...
node/src/Worker.ts
浏览文件 @
90fff361
...
...
@@ -8,8 +8,9 @@ import * as ortc from './ortc';
import
{
Channel
}
from
'./Channel'
;
import
{
Router
,
RouterOptions
}
from
'./Router'
;
import
{
WebRtcServer
,
WebRtcServerOptions
}
from
'./WebRtcServer'
;
import
{
RtpCodecCapability
}
from
'./RtpParameters'
;
import
{
AppData
}
from
'./types'
;
import
{
generateUUIDv4
,
parseVector
}
from
'./utils'
;
import
*
as
utils
from
'./utils'
;
import
{
Event
}
from
'./fbs/notification'
;
import
*
as
FbsRequest
from
'./fbs/request'
;
import
*
as
FbsWorker
from
'./fbs/worker'
;
...
...
@@ -703,7 +704,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
);
}
const
webRtcServerId
=
generateUUIDv4
();
const
webRtcServerId
=
utils
.
generateUUIDv4
();
const
createWebRtcServerRequestOffset
=
new
FbsWorker
.
CreateWebRtcServerRequestT
(
webRtcServerId
,
fbsListenInfos
...
...
@@ -747,10 +748,14 @@ export class Worker<WorkerAppData extends AppData = AppData>
throw
new
TypeError
(
'if given, appData must be an object'
);
}
// Clone given media codecs to not modify input data.
const
clonedMediaCodecs
=
utils
.
clone
<
RtpCodecCapability
[]
|
undefined
>
(
mediaCodecs
);
// This may throw.
const
rtpCapabilities
=
ortc
.
generateRouterRtpCapabilities
(
m
ediaCodecs
);
const
rtpCapabilities
=
ortc
.
generateRouterRtpCapabilities
(
clonedM
ediaCodecs
);
const
routerId
=
generateUUIDv4
();
const
routerId
=
utils
.
generateUUIDv4
();
// Get flatbuffer builder.
const
createRouterRequestOffset
=
...
...
@@ -822,12 +827,12 @@ export function parseWorkerDumpResponse(
{
const
dump
:
WorkerDump
=
{
pid
:
binary
.
pid
()
!
,
webRtcServerIds
:
parseVector
(
binary
,
'webRtcServerIds'
),
routerIds
:
parseVector
(
binary
,
'routerIds'
),
webRtcServerIds
:
utils
.
parseVector
(
binary
,
'webRtcServerIds'
),
routerIds
:
utils
.
parseVector
(
binary
,
'routerIds'
),
channelMessageHandlers
:
{
channelRequestHandlers
:
parseVector
(
binary
.
channelMessageHandlers
()
!
,
'channelRequestHandlers'
),
channelNotificationHandlers
:
parseVector
(
binary
.
channelMessageHandlers
()
!
,
'channelNotificationHandlers'
)
channelRequestHandlers
:
utils
.
parseVector
(
binary
.
channelMessageHandlers
()
!
,
'channelRequestHandlers'
),
channelNotificationHandlers
:
utils
.
parseVector
(
binary
.
channelMessageHandlers
()
!
,
'channelNotificationHandlers'
)
}
};
...
...
node/src/index.ts
浏览文件 @
90fff361
...
...
@@ -95,5 +95,5 @@ export async function createWorker<WorkerAppData extends types.AppData = types.A
*/
export
function
getSupportedRtpCapabilities
():
RtpCapabilities
{
return
utils
.
clone
(
supportedRtpCapabilities
)
as
RtpCapabilities
;
return
utils
.
clone
<
RtpCapabilities
>
(
supportedRtpCapabilities
)
;
}
node/src/ortc.ts
浏览文件 @
90fff361
...
...
@@ -14,12 +14,7 @@ import {
RtpHeaderExtensionParameters
,
RtcpParameters
}
from
'./RtpParameters'
;
import
{
SctpCapabilities
,
NumSctpStreams
,
SctpParameters
,
SctpStreamParameters
}
from
'./SctpParameters'
;
import
{
SctpStreamParameters
}
from
'./SctpParameters'
;
import
*
as
utils
from
'./utils'
;
import
{
UnsupportedError
}
from
'./errors'
;
import
*
as
FbsRtpParameters
from
'./fbs/rtp-parameters'
;
...
...
@@ -91,181 +86,6 @@ export function validateRtpCapabilities(caps: RtpCapabilities): void
}
}
/**
* Validates RtpCodecCapability. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateRtpCodecCapability
(
codec
:
RtpCodecCapability
):
void
{
const
MimeTypeRegex
=
new
RegExp
(
'^(audio|video)/(.+)'
,
'i'
);
if
(
typeof
codec
!==
'object'
)
{
throw
new
TypeError
(
'codec is not an object'
);
}
// mimeType is mandatory.
if
(
!
codec
.
mimeType
||
typeof
codec
.
mimeType
!==
'string'
)
{
throw
new
TypeError
(
'missing codec.mimeType'
);
}
const
mimeTypeMatch
=
MimeTypeRegex
.
exec
(
codec
.
mimeType
);
if
(
!
mimeTypeMatch
)
{
throw
new
TypeError
(
'invalid codec.mimeType'
);
}
// Just override kind with media component of mimeType.
codec
.
kind
=
mimeTypeMatch
[
1
].
toLowerCase
()
as
MediaKind
;
// preferredPayloadType is optional.
if
(
codec
.
preferredPayloadType
&&
typeof
codec
.
preferredPayloadType
!==
'number'
)
{
throw
new
TypeError
(
'invalid codec.preferredPayloadType'
);
}
// clockRate is mandatory.
if
(
typeof
codec
.
clockRate
!==
'number'
)
{
throw
new
TypeError
(
'missing codec.clockRate'
);
}
// channels is optional. If unset, set it to 1 (just if audio).
if
(
codec
.
kind
===
'audio'
)
{
if
(
typeof
codec
.
channels
!==
'number'
)
{
codec
.
channels
=
1
;
}
}
else
{
delete
codec
.
channels
;
}
// parameters is optional. If unset, set it to an empty object.
if
(
!
codec
.
parameters
||
typeof
codec
.
parameters
!==
'object'
)
{
codec
.
parameters
=
{};
}
for
(
const
key
of
Object
.
keys
(
codec
.
parameters
))
{
let
value
=
codec
.
parameters
[
key
];
if
(
value
===
undefined
)
{
codec
.
parameters
[
key
]
=
''
;
value
=
''
;
}
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
{
throw
new
TypeError
(
`invalid codec parameter [key:
${
key
}
s, value:
${
value
}
]`
);
}
// Specific parameters validation.
if
(
key
===
'apt'
)
{
if
(
typeof
value
!==
'number'
)
{
throw
new
TypeError
(
'invalid codec apt parameter'
);
}
}
}
// rtcpFeedback is optional. If unset, set it to an empty array.
if
(
!
codec
.
rtcpFeedback
||
!
Array
.
isArray
(
codec
.
rtcpFeedback
))
{
codec
.
rtcpFeedback
=
[];
}
for
(
const
fb
of
codec
.
rtcpFeedback
)
{
validateRtcpFeedback
(
fb
);
}
}
/**
* Validates RtcpFeedback. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateRtcpFeedback
(
fb
:
RtcpFeedback
):
void
{
if
(
typeof
fb
!==
'object'
)
{
throw
new
TypeError
(
'fb is not an object'
);
}
// type is mandatory.
if
(
!
fb
.
type
||
typeof
fb
.
type
!==
'string'
)
{
throw
new
TypeError
(
'missing fb.type'
);
}
// parameter is optional. If unset set it to an empty string.
if
(
!
fb
.
parameter
||
typeof
fb
.
parameter
!==
'string'
)
{
fb
.
parameter
=
''
;
}
}
/**
* Validates RtpHeaderExtension. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateRtpHeaderExtension
(
ext
:
RtpHeaderExtension
):
void
{
if
(
typeof
ext
!==
'object'
)
{
throw
new
TypeError
(
'ext is not an object'
);
}
if
(
ext
.
kind
!==
'audio'
&&
ext
.
kind
!==
'video'
)
{
throw
new
TypeError
(
'invalid ext.kind'
);
}
// uri is mandatory.
if
(
!
ext
.
uri
||
typeof
ext
.
uri
!==
'string'
)
{
throw
new
TypeError
(
'missing ext.uri'
);
}
// preferredId is mandatory.
if
(
typeof
ext
.
preferredId
!==
'number'
)
{
throw
new
TypeError
(
'missing ext.preferredId'
);
}
// preferredEncrypt is optional. If unset set it to false.
if
(
ext
.
preferredEncrypt
&&
typeof
ext
.
preferredEncrypt
!==
'boolean'
)
{
throw
new
TypeError
(
'invalid ext.preferredEncrypt'
);
}
else
if
(
!
ext
.
preferredEncrypt
)
{
ext
.
preferredEncrypt
=
false
;
}
// direction is optional. If unset set it to sendrecv.
if
(
ext
.
direction
&&
typeof
ext
.
direction
!==
'string'
)
{
throw
new
TypeError
(
'invalid ext.direction'
);
}
else
if
(
!
ext
.
direction
)
{
ext
.
direction
=
'sendrecv'
;
}
}
/**
* Validates RtpParameters. It may modify given data by adding missing
* fields with default values.
...
...
@@ -339,1221 +159,1317 @@ export function validateRtpParameters(params: RtpParameters): void
}
/**
* Validates
RtpCodec
Parameters. It may modify given data by adding missing
* Validates
SctpStream
Parameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validate
RtpCodecParameters
(
codec
:
RtpCodec
Parameters
):
void
export
function
validate
SctpStreamParameters
(
params
:
SctpStream
Parameters
):
void
{
const
MimeTypeRegex
=
new
RegExp
(
'^(audio|video)/(.+)'
,
'i'
);
if
(
typeof
codec
!==
'object'
)
if
(
typeof
params
!==
'object'
)
{
throw
new
TypeError
(
'
codec
is not an object'
);
throw
new
TypeError
(
'
params
is not an object'
);
}
//
mimeType
is mandatory.
if
(
!
codec
.
mimeType
||
typeof
codec
.
mimeType
!==
'string
'
)
//
streamId
is mandatory.
if
(
typeof
params
.
streamId
!==
'number
'
)
{
throw
new
TypeError
(
'missing
codec.mimeType
'
);
throw
new
TypeError
(
'missing
params.streamId
'
);
}
const
mimeTypeMatch
=
MimeTypeRegex
.
exec
(
codec
.
mimeType
);
// ordered is optional.
let
orderedGiven
=
false
;
if
(
!
mimeTypeMatch
)
if
(
typeof
params
.
ordered
===
'boolean'
)
{
throw
new
TypeError
(
'invalid codec.mimeType'
);
orderedGiven
=
true
;
}
else
{
params
.
ordered
=
true
;
}
//
payloadType is mandatory
.
if
(
typeof
codec
.
payloadTyp
e
!==
'number'
)
//
maxPacketLifeTime is optional
.
if
(
params
.
maxPacketLifeTime
&&
typeof
params
.
maxPacketLifeTim
e
!==
'number'
)
{
throw
new
TypeError
(
'
missing codec.payloadTyp
e'
);
throw
new
TypeError
(
'
invalid params.maxPacketLifeTim
e'
);
}
//
clockRate is mandatory
.
if
(
typeof
codec
.
clockRate
!==
'number'
)
//
maxRetransmits is optional
.
if
(
params
.
maxRetransmits
&&
typeof
params
.
maxRetransmits
!==
'number'
)
{
throw
new
TypeError
(
'
missing codec.clockRate
'
);
throw
new
TypeError
(
'
invalid params.maxRetransmits
'
);
}
const
kind
=
mimeTypeMatch
[
1
].
toLowerCase
()
as
MediaKind
;
if
(
params
.
maxPacketLifeTime
&&
params
.
maxRetransmits
)
{
throw
new
TypeError
(
'cannot provide both maxPacketLifeTime and maxRetransmits'
);
}
// channels is optional. If unset, set it to 1 (just if audio).
if
(
kind
===
'audio'
)
if
(
orderedGiven
&&
params
.
ordered
&&
(
params
.
maxPacketLifeTime
||
params
.
maxRetransmits
)
)
{
if
(
typeof
codec
.
channels
!==
'number'
)
{
codec
.
channels
=
1
;
}
throw
new
TypeError
(
'cannot be ordered with maxPacketLifeTime or maxRetransmits'
);
}
else
else
if
(
!
orderedGiven
&&
(
params
.
maxPacketLifeTime
||
params
.
maxRetransmits
))
{
delete
codec
.
channels
;
params
.
ordered
=
false
;
}
}
// parameters is optional. If unset, set it to an empty object.
if
(
!
codec
.
parameters
||
typeof
codec
.
parameters
!==
'object'
)
/**
* Generate RTP capabilities for the Router based on the given media codecs and
* mediasoup supported RTP capabilities.
*/
export
function
generateRouterRtpCapabilities
(
mediaCodecs
:
RtpCodecCapability
[]
=
[]
):
RtpCapabilities
{
// Normalize supported RTP capabilities.
validateRtpCapabilities
(
supportedRtpCapabilities
);
if
(
!
Array
.
isArray
(
mediaCodecs
))
{
codec
.
parameters
=
{}
;
throw
new
TypeError
(
'mediaCodecs must be an Array'
)
;
}
for
(
const
key
of
Object
.
keys
(
codec
.
parameters
))
const
clonedSupportedRtpCapabilities
=
utils
.
clone
<
RtpCapabilities
>
(
supportedRtpCapabilities
);
const
dynamicPayloadTypes
=
utils
.
clone
<
number
[]
>
(
DynamicPayloadTypes
);
const
caps
:
RtpCapabilities
=
{
let
value
=
codec
.
parameters
[
key
];
codecs
:
[],
headerExtensions
:
clonedSupportedRtpCapabilities
.
headerExtensions
};
if
(
value
===
undefined
)
for
(
const
mediaCodec
of
mediaCodecs
)
{
// This may throw.
validateRtpCodecCapability
(
mediaCodec
);
const
matchedSupportedCodec
=
clonedSupportedRtpCapabilities
.
codecs
!
.
find
((
supportedCodec
)
=>
(
matchCodecs
(
mediaCodec
,
supportedCodec
,
{
strict
:
false
}))
);
if
(
!
matchedSupportedCodec
)
{
codec
.
parameters
[
key
]
=
''
;
value
=
''
;
throw
new
UnsupportedError
(
`media codec not supported [mimeType:
${
mediaCodec
.
mimeType
}
]`
)
;
}
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
// Clone the supported codec.
const
codec
=
utils
.
clone
<
RtpCodecCapability
>
(
matchedSupportedCodec
);
// If the given media codec has preferredPayloadType, keep it.
if
(
typeof
mediaCodec
.
preferredPayloadType
===
'number'
)
{
throw
new
TypeError
(
`invalid codec parameter [key:
${
key
}
s, value:
${
value
}
]`
);
}
codec
.
preferredPayloadType
=
mediaCodec
.
preferredPayloadType
;
// Specific parameters validation.
if
(
key
===
'apt'
)
// Also remove the pt from the list of available dynamic values.
const
idx
=
dynamicPayloadTypes
.
indexOf
(
codec
.
preferredPayloadType
);
if
(
idx
>
-
1
)
{
dynamicPayloadTypes
.
splice
(
idx
,
1
);
}
}
// Otherwise if the supported codec has preferredPayloadType, use it.
else
if
(
typeof
codec
.
preferredPayloadType
===
'number'
)
{
if
(
typeof
value
!==
'number'
)
// No need to remove it from the list since it's not a dynamic value.
}
// Otherwise choose a dynamic one.
else
{
// Take the first available pt and remove it from the list.
const
pt
=
dynamicPayloadTypes
.
shift
();
if
(
!
pt
)
{
throw
new
TypeError
(
'invalid codec apt parameter
'
);
throw
new
Error
(
'cannot allocate more dynamic codec payload types
'
);
}
codec
.
preferredPayloadType
=
pt
;
}
}
// rtcpFeedback is optional. If unset, set it to an empty array
.
if
(
!
codec
.
rtcpFeedback
||
!
Array
.
isArray
(
codec
.
rtcpFeedback
))
{
codec
.
rtcpFeedback
=
[]
;
}
// Ensure there is not duplicated preferredPayloadType values
.
if
(
caps
.
codecs
!
.
some
((
c
)
=>
c
.
preferredPayloadType
===
codec
.
preferredPayloadType
))
{
throw
new
TypeError
(
'duplicated codec.preferredPayloadType'
)
;
}
for
(
const
fb
of
codec
.
rtcpFeedback
)
{
validateRtcpFeedback
(
fb
);
// Merge the media codec parameters.
codec
.
parameters
=
{
...
codec
.
parameters
,
...
mediaCodec
.
parameters
};
// Append to the codec list.
caps
.
codecs
!
.
push
(
codec
);
// Add a RTX video codec if video.
if
(
codec
.
kind
===
'video'
)
{
// Take the first available pt and remove it from the list.
const
pt
=
dynamicPayloadTypes
.
shift
();
if
(
!
pt
)
{
throw
new
Error
(
'cannot allocate more dynamic codec payload types'
);
}
const
rtxCodec
:
RtpCodecCapability
=
{
kind
:
codec
.
kind
,
mimeType
:
`
${
codec
.
kind
}
/rtx`
,
preferredPayloadType
:
pt
,
clockRate
:
codec
.
clockRate
,
parameters
:
{
apt
:
codec
.
preferredPayloadType
},
rtcpFeedback
:
[]
};
// Append to the codec list.
caps
.
codecs
!
.
push
(
rtxCodec
);
}
}
return
caps
;
}
/**
* Validates RtpHeaderExtensionParameteters. It may modify given data by adding
* missing fields with default values. It throws if invalid.
* Get a mapping of codec payloads and encodings of the given Producer RTP
* parameters as values expected by the Router.
*
* It may throw if invalid or non supported RTP parameters are given.
*/
export
function
validateRtpHeaderExtensionParameters
(
ext
:
RtpHeaderExtensionParameters
):
void
export
function
getProducerRtpParametersMapping
(
params
:
RtpParameters
,
caps
:
RtpCapabilities
):
RtpMapping
{
if
(
typeof
ext
!==
'object'
)
const
rtpMapping
:
RtpMapping
=
{
throw
new
TypeError
(
'ext is not an object'
);
}
codecs
:
[],
encodings
:
[]
};
// uri is mandatory.
if
(
!
ext
.
uri
||
typeof
ext
.
uri
!==
'string'
)
{
throw
new
TypeError
(
'missing ext.uri'
);
}
// Match parameters media codecs to capabilities media codecs.
const
codecToCapCodec
:
Map
<
RtpCodecParameters
,
RtpCodecCapability
>
=
new
Map
();
// id is mandatory.
if
(
typeof
ext
.
id
!==
'number'
)
for
(
const
codec
of
params
.
codecs
)
{
throw
new
TypeError
(
'missing ext.id'
);
}
if
(
isRtxCodec
(
codec
))
{
continue
;
}
// encrypt is optional. If unset set it to false.
if
(
ext
.
encrypt
&&
typeof
ext
.
encrypt
!==
'boolean'
)
{
throw
new
TypeError
(
'invalid ext.encrypt'
);
}
else
if
(
!
ext
.
encrypt
)
{
ext
.
encrypt
=
false
;
}
// Search for the same media codec in capabilities.
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
(
matchCodecs
(
codec
,
capCodec
,
{
strict
:
true
,
modify
:
true
}))
);
// parameters is optional. If unset, set it to an empty object.
if
(
!
ext
.
parameters
||
typeof
ext
.
parameters
!==
'object'
)
{
ext
.
parameters
=
{};
if
(
!
matchedCapCodec
)
{
throw
new
UnsupportedError
(
`unsupported codec [mimeType:
${
codec
.
mimeType
}
, payloadType:
${
codec
.
payloadType
}
]`
);
}
codecToCapCodec
.
set
(
codec
,
matchedCapCodec
);
}
for
(
const
key
of
Object
.
keys
(
ext
.
parameters
))
// Match parameters RTX codecs to capabilities RTX codecs.
for
(
const
codec
of
params
.
codecs
)
{
let
value
=
ext
.
parameters
[
key
];
if
(
!
isRtxCodec
(
codec
))
{
continue
;
}
if
(
value
===
undefined
)
// Search for the associated media codec.
const
associatedMediaCodec
=
params
.
codecs
.
find
((
mediaCodec
)
=>
mediaCodec
.
payloadType
===
codec
.
parameters
.
apt
);
if
(
!
associatedMediaCodec
)
{
ext
.
parameters
[
key
]
=
''
;
value
=
''
;
throw
new
TypeError
(
`missing media codec found for RTX PT
${
codec
.
payloadType
}
`
)
;
}
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
const
capMediaCodec
=
codecToCapCodec
.
get
(
associatedMediaCodec
);
// Ensure that the capabilities media codec has a RTX codec.
const
associatedCapRtxCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
(
isRtxCodec
(
capCodec
)
&&
capCodec
.
parameters
.
apt
===
capMediaCodec
!
.
preferredPayloadType
));
if
(
!
associatedCapRtxCodec
)
{
throw
new
TypeError
(
'invalid header extension parameter'
);
throw
new
UnsupportedError
(
`no RTX codec for capability codec PT
${
capMediaCodec
!
.
preferredPayloadType
}
`
);
}
}
}
/**
* Validates RtpEncodingParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateRtpEncodingParameters
(
encoding
:
RtpEncodingParameters
):
void
{
if
(
typeof
encoding
!==
'object'
)
{
throw
new
TypeError
(
'encoding is not an object'
);
codecToCapCodec
.
set
(
codec
,
associatedCapRtxCodec
);
}
//
ssrc is optional
.
if
(
encoding
.
ssrc
&&
typeof
encoding
.
ssrc
!==
'number'
)
//
Generate codecs mapping
.
for
(
const
[
codec
,
capCodec
]
of
codecToCapCodec
)
{
throw
new
TypeError
(
'invalid encoding.ssrc'
);
rtpMapping
.
codecs
.
push
(
{
payloadType
:
codec
.
payloadType
,
mappedPayloadType
:
capCodec
.
preferredPayloadType
!
});
}
// rid is optional.
if
(
encoding
.
rid
&&
typeof
encoding
.
rid
!==
'string'
)
{
throw
new
TypeError
(
'invalid encoding.rid'
);
}
// Generate encodings mapping.
let
mappedSsrc
=
utils
.
generateRandomNumber
();
// rtx is optional.
if
(
encoding
.
rtx
&&
typeof
encoding
.
rtx
!==
'object'
)
{
throw
new
TypeError
(
'invalid encoding.rtx'
);
}
else
if
(
encoding
.
rtx
)
for
(
const
encoding
of
params
.
encodings
!
)
{
// RTX ssrc is mandatory if rtx is present.
if
(
typeof
encoding
.
rtx
.
ssrc
!==
'number'
)
const
mappedEncoding
:
any
=
{};
mappedEncoding
.
mappedSsrc
=
mappedSsrc
++
;
if
(
encoding
.
rid
)
{
throw
new
TypeError
(
'missing encoding.rtx.ssrc'
);
mappedEncoding
.
rid
=
encoding
.
rid
;
}
if
(
encoding
.
ssrc
)
{
mappedEncoding
.
ssrc
=
encoding
.
ssrc
;
}
if
(
encoding
.
scalabilityMode
)
{
mappedEncoding
.
scalabilityMode
=
encoding
.
scalabilityMode
;
}
}
// dtx is optional. If unset set it to false.
if
(
!
encoding
.
dtx
||
typeof
encoding
.
dtx
!==
'boolean'
)
{
encoding
.
dtx
=
false
;
rtpMapping
.
encodings
.
push
(
mappedEncoding
);
}
// scalabilityMode is optional.
if
(
encoding
.
scalabilityMode
&&
typeof
encoding
.
scalabilityMode
!==
'string'
)
{
throw
new
TypeError
(
'invalid encoding.scalabilityMode'
);
}
return
rtpMapping
;
}
/**
* Validates RtcpParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
* Generate RTP parameters to be internally used by Consumers given the RTP
* parameters of a Producer and the RTP capabilities of the Router.
*/
export
function
validateRtcpParameters
(
rtcp
:
RtcpParameters
):
void
export
function
getConsumableRtpParameters
(
kind
:
string
,
params
:
RtpParameters
,
caps
:
RtpCapabilities
,
rtpMapping
:
RtpMapping
):
RtpParameters
{
if
(
typeof
rtcp
!==
'object'
)
const
consumableParams
:
RtpParameters
=
{
throw
new
TypeError
(
'rtcp is not an object'
);
}
codecs
:
[],
headerExtensions
:
[],
encodings
:
[],
rtcp
:
{}
};
// cname is optional.
if
(
rtcp
.
cname
&&
typeof
rtcp
.
cname
!==
'string'
)
for
(
const
codec
of
params
.
codecs
)
{
throw
new
TypeError
(
'invalid rtcp.cname'
);
}
if
(
isRtxCodec
(
codec
))
{
continue
;
}
// reducedSize is optional. If unset set it to true.
if
(
!
rtcp
.
reducedSize
||
typeof
rtcp
.
reducedSize
!==
'boolean'
)
{
rtcp
.
reducedSize
=
true
;
}
}
const
consumableCodecPt
=
rtpMapping
.
codecs
.
find
((
entry
)
=>
entry
.
payloadType
===
codec
.
payloadType
)
!
.
mappedPayloadType
;
/**
* Validates SctpCapabilities. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateSctpCapabilities
(
caps
:
SctpCapabilities
):
void
{
if
(
typeof
caps
!==
'object'
)
{
throw
new
TypeError
(
'caps is not an object'
);
}
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
capCodec
.
preferredPayloadType
===
consumableCodecPt
)
!
;
// numStreams is mandatory.
if
(
!
caps
.
numStreams
||
typeof
caps
.
numStreams
!==
'object'
)
{
throw
new
TypeError
(
'missing caps.numStreams'
);
}
const
consumableCodec
:
RtpCodecParameters
=
{
mimeType
:
matchedCapCodec
.
mimeType
,
payloadType
:
matchedCapCodec
.
preferredPayloadType
!
,
clockRate
:
matchedCapCodec
.
clockRate
,
channels
:
matchedCapCodec
.
channels
,
parameters
:
codec
.
parameters
,
// Keep the Producer codec parameters.
rtcpFeedback
:
matchedCapCodec
.
rtcpFeedback
};
validateNumSctpStreams
(
caps
.
numStreams
);
}
consumableParams
.
codecs
.
push
(
consumableCodec
);
/**
* Validates NumSctpStreams. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateNumSctpStreams
(
numStreams
:
NumSctpStreams
):
void
{
if
(
typeof
numStreams
!==
'object'
)
{
throw
new
TypeError
(
'numStreams is not an object'
);
}
const
consumableCapRtxCodec
=
caps
.
codecs
!
.
find
((
capRtxCodec
)
=>
(
isRtxCodec
(
capRtxCodec
)
&&
capRtxCodec
.
parameters
.
apt
===
consumableCodec
.
payloadType
));
// OS is mandatory.
if
(
typeof
numStreams
.
OS
!==
'number'
)
{
throw
new
TypeError
(
'missing numStreams.OS'
);
}
if
(
consumableCapRtxCodec
)
{
const
consumableRtxCodec
:
RtpCodecParameters
=
{
mimeType
:
consumableCapRtxCodec
.
mimeType
,
payloadType
:
consumableCapRtxCodec
.
preferredPayloadType
!
,
clockRate
:
consumableCapRtxCodec
.
clockRate
,
parameters
:
consumableCapRtxCodec
.
parameters
,
rtcpFeedback
:
consumableCapRtxCodec
.
rtcpFeedback
};
// MIS is mandatory.
if
(
typeof
numStreams
.
MIS
!==
'number'
)
{
throw
new
TypeError
(
'missing numStreams.MIS'
);
consumableParams
.
codecs
.
push
(
consumableRtxCodec
);
}
}
}
/**
* Validates SctpParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
validateSctpParameters
(
params
:
SctpParameters
):
void
{
if
(
typeof
params
!==
'object'
)
for
(
const
capExt
of
caps
.
headerExtensions
!
)
{
throw
new
TypeError
(
'params is not an object'
);
}
// port is mandatory.
if
(
typeof
params
.
port
!==
'number'
)
{
throw
new
TypeError
(
'missing params.port'
);
}
// Just take RTP header extension that can be used in Consumers.
if
(
capExt
.
kind
!==
kind
||
(
capExt
.
direction
!==
'sendrecv'
&&
capExt
.
direction
!==
'sendonly'
)
)
{
continue
;
}
// OS is mandatory.
if
(
typeof
params
.
OS
!==
'number'
)
{
throw
new
TypeError
(
'missing params.OS'
);
const
consumableExt
=
{
uri
:
capExt
.
uri
,
id
:
capExt
.
preferredId
,
encrypt
:
capExt
.
preferredEncrypt
,
parameters
:
{}
};
consumableParams
.
headerExtensions
!
.
push
(
consumableExt
);
}
// MIS is mandatory.
if
(
typeof
params
.
MIS
!==
'number'
)
// Clone Producer encodings since we'll mangle them.
const
consumableEncodings
=
utils
.
clone
<
RtpEncodingParameters
[]
|
undefined
>
(
params
.
encodings
)
??
[];
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
{
throw
new
TypeError
(
'missing params.MIS'
);
const
consumableEncoding
=
consumableEncodings
[
i
];
const
{
mappedSsrc
}
=
rtpMapping
.
encodings
[
i
];
// Remove useless fields.
delete
consumableEncoding
.
rid
;
delete
consumableEncoding
.
rtx
;
delete
consumableEncoding
.
codecPayloadType
;
// Set the mapped ssrc.
consumableEncoding
.
ssrc
=
mappedSsrc
;
consumableParams
.
encodings
!
.
push
(
consumableEncoding
);
}
// maxMessageSize is mandatory.
if
(
typeof
params
.
maxMessageSize
!==
'number'
)
consumableParams
.
rtcp
=
{
throw
new
TypeError
(
'missing params.maxMessageSize'
);
}
cname
:
params
.
rtcp
!
.
cname
,
reducedSize
:
true
};
return
consumableParams
;
}
/**
* Validates SctpStreamParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
* Check whether the given RTP capabilities can consume the given Producer.
*/
export
function
validateSctpStreamParameters
(
params
:
SctpStreamParameters
):
void
export
function
canConsume
(
consumableParams
:
RtpParameters
,
caps
:
RtpCapabilities
):
boolean
{
if
(
typeof
params
!==
'object'
)
{
throw
new
TypeError
(
'params is not an object'
);
}
// streamId is mandatory.
if
(
typeof
params
.
streamId
!==
'number'
)
{
throw
new
TypeError
(
'missing params.streamId'
);
}
// This may throw.
validateRtpCapabilities
(
caps
);
// ordered is optional.
let
orderedGiven
=
false
;
const
matchingCodecs
:
RtpCodecParameters
[]
=
[];
if
(
typeof
params
.
ordered
===
'boolean'
)
{
orderedGiven
=
true
;
}
else
for
(
const
codec
of
consumableParams
.
codecs
)
{
params
.
ordered
=
true
;
}
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
matchCodecs
(
capCodec
,
codec
,
{
strict
:
true
}));
// maxPacketLifeTime is optional.
if
(
params
.
maxPacketLifeTime
&&
typeof
params
.
maxPacketLifeTime
!==
'number'
)
{
throw
new
TypeError
(
'invalid params.maxPacketLifeTime'
);
}
if
(
!
matchedCapCodec
)
{
continue
;
}
// maxRetransmits is optional.
if
(
params
.
maxRetransmits
&&
typeof
params
.
maxRetransmits
!==
'number'
)
{
throw
new
TypeError
(
'invalid params.maxRetransmits'
);
matchingCodecs
.
push
(
codec
);
}
if
(
params
.
maxPacketLifeTime
&&
params
.
maxRetransmits
)
// Ensure there is at least one media codec.
if
(
matchingCodecs
.
length
===
0
||
isRtxCodec
(
matchingCodecs
[
0
]))
{
throw
new
TypeError
(
'cannot provide both maxPacketLifeTime and maxRetransmits'
)
;
return
false
;
}
if
(
orderedGiven
&&
params
.
ordered
&&
(
params
.
maxPacketLifeTime
||
params
.
maxRetransmits
)
)
{
throw
new
TypeError
(
'cannot be ordered with maxPacketLifeTime or maxRetransmits'
);
}
else
if
(
!
orderedGiven
&&
(
params
.
maxPacketLifeTime
||
params
.
maxRetransmits
))
{
params
.
ordered
=
false
;
}
return
true
;
}
/**
* Generate RTP capabilities for the Router based on the given media codecs and
* mediasoup supported RTP capabilities.
* Generate RTP parameters for a specific Consumer.
*
* It reduces encodings to just one and takes into account given RTP
* capabilities to reduce codecs, codecs' RTCP feedback and header extensions,
* and also enables or disables RTX.
*/
export
function
generateRouterRtpCapabilities
(
mediaCodecs
:
RtpCodecCapability
[]
=
[]
):
RtpCapabilities
{
// Normalize supported RTP capabilities.
validateRtpCapabilities
(
supportedRtpCapabilities
);
if
(
!
Array
.
isArray
(
mediaCodecs
))
export
function
getConsumerRtpParameters
(
{
throw
new
TypeError
(
'mediaCodecs must be an Array'
);
consumableRtpParameters
,
remoteRtpCapabilities
,
pipe
,
enableRtx
}:
{
consumableRtpParameters
:
RtpParameters
;
remoteRtpCapabilities
:
RtpCapabilities
;
pipe
:
boolean
;
enableRtx
:
boolean
;
}
const
clonedSupportedRtpCapabilities
=
utils
.
clone
(
supportedRtpCapabilities
)
as
RtpCapabilities
;
const
dynamicPayloadTypes
=
utils
.
clone
(
DynamicPayloadTypes
)
as
number
[];
const
caps
:
RtpCapabilities
=
):
RtpParameters
{
const
consumerParams
:
RtpParameters
=
{
codecs
:
[],
headerExtensions
:
clonedSupportedRtpCapabilities
.
headerExtensions
headerExtensions
:
[],
encodings
:
[],
rtcp
:
consumableRtpParameters
.
rtcp
};
for
(
const
mediaCodec
of
mediaCodecs
)
for
(
const
capCodec
of
remoteRtpCapabilities
.
codecs
!
)
{
// This may throw.
validateRtpCodecCapability
(
mediaCodec
);
validateRtpCodecCapability
(
capCodec
);
}
const
matchedSupportedCodec
=
clonedSupportedRtpCapabilities
.
codecs
!
.
find
((
supportedCodec
)
=>
(
matchCodecs
(
mediaCodec
,
supportedCodec
,
{
strict
:
false
}))
);
const
consumableCodecs
=
utils
.
clone
<
RtpCodecParameters
[]
|
undefined
>
(
consumableRtpParameters
.
codecs
)
??
[];
if
(
!
matchedSupportedCodec
)
let
rtxSupported
=
false
;
for
(
const
codec
of
consumableCodecs
)
{
if
(
!
enableRtx
&&
isRtxCodec
(
codec
))
{
throw
new
UnsupportedError
(
`media codec not supported [mimeType:
${
mediaCodec
.
mimeType
}
]`
);
continue
;
}
// Clone the supported codec.
const
codec
=
utils
.
clone
(
matchedSupportedCodec
)
as
RtpCodecCapability
;
const
matchedCapCodec
=
remoteRtpCapabilities
.
codecs
!
.
find
((
capCodec
)
=>
matchCodecs
(
capCodec
,
codec
,
{
strict
:
true
}))
;
// If the given media codec has preferredPayloadType, keep it.
if
(
typeof
mediaCodec
.
preferredPayloadType
===
'number'
)
if
(
!
matchedCapCodec
)
{
codec
.
preferredPayloadType
=
mediaCodec
.
preferredPayloadType
;
continue
;
}
// Also remove the pt from the list of available dynamic values.
const
idx
=
dynamicPayloadTypes
.
indexOf
(
codec
.
preferredPayloadType
);
codec
.
rtcpFeedback
=
matchedCapCodec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
(
enableRtx
||
fb
.
type
!==
'nack'
||
fb
.
parameter
)
));
if
(
idx
>
-
1
)
consumerParams
.
codecs
.
push
(
codec
);
}
// Must sanitize the list of matched codecs by removing useless RTX codecs.
for
(
let
idx
=
consumerParams
.
codecs
.
length
-
1
;
idx
>=
0
;
--
idx
)
{
const
codec
=
consumerParams
.
codecs
[
idx
];
if
(
isRtxCodec
(
codec
))
{
// Search for the associated media codec.
const
associatedMediaCodec
=
consumerParams
.
codecs
.
find
((
mediaCodec
)
=>
mediaCodec
.
payloadType
===
codec
.
parameters
.
apt
);
if
(
associatedMediaCodec
)
{
dynamicPayloadTypes
.
splice
(
idx
,
1
);
rtxSupported
=
true
;
}
else
{
consumerParams
.
codecs
.
splice
(
idx
,
1
);
}
}
// Otherwise if the supported codec has preferredPayloadType, use it.
else
if
(
typeof
codec
.
preferredPayloadType
===
'number'
)
}
// Ensure there is at least one media codec.
if
(
consumerParams
.
codecs
.
length
===
0
||
isRtxCodec
(
consumerParams
.
codecs
[
0
]))
{
throw
new
UnsupportedError
(
'no compatible media codecs'
);
}
consumerParams
.
headerExtensions
=
consumableRtpParameters
.
headerExtensions
!
.
filter
((
ext
)
=>
(
remoteRtpCapabilities
.
headerExtensions
!
.
some
((
capExt
)
=>
(
capExt
.
preferredId
===
ext
.
id
&&
capExt
.
uri
===
ext
.
uri
))
));
// Reduce codecs' RTCP feedback. Use Transport-CC if available, REMB otherwise.
if
(
consumerParams
.
headerExtensions
.
some
((
ext
)
=>
(
ext
.
uri
===
'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01'
))
)
{
for
(
const
codec
of
consumerParams
.
codecs
)
{
// No need to remove it from the list since it's not a dynamic value.
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
fb
.
type
!==
'goog-remb'
);
}
// Otherwise choose a dynamic one.
else
}
else
if
(
consumerParams
.
headerExtensions
.
some
((
ext
)
=>
(
ext
.
uri
===
'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time'
))
)
{
for
(
const
codec
of
consumerParams
.
codecs
)
{
// Take the first available pt and remove it from the list.
const
pt
=
dynamicPayloadTypes
.
shift
();
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
fb
.
type
!==
'transport-cc'
);
}
}
else
{
for
(
const
codec
of
consumerParams
.
codecs
)
{
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
fb
.
type
!==
'transport-cc'
&&
fb
.
type
!==
'goog-remb'
));
}
}
if
(
!
pt
)
{
throw
new
Error
(
'cannot allocate more dynamic codec payload types'
);
}
if
(
!
pipe
)
{
const
consumerEncoding
:
RtpEncodingParameters
=
{
ssrc
:
utils
.
generateRandomNumber
()
};
codec
.
preferredPayloadType
=
pt
;
if
(
rtxSupported
)
{
consumerEncoding
.
rtx
=
{
ssrc
:
consumerEncoding
.
ssrc
!
+
1
};
}
// Ensure there is not duplicated preferredPayloadType values.
if
(
caps
.
codecs
!
.
some
((
c
)
=>
c
.
preferredPayloadType
===
codec
.
preferredPayloadType
))
// If any of the consumableRtpParameters.encodings has scalabilityMode,
// process it (assume all encodings have the same value).
const
encodingWithScalabilityMode
=
consumableRtpParameters
.
encodings
!
.
find
((
encoding
)
=>
encoding
.
scalabilityMode
);
let
scalabilityMode
=
encodingWithScalabilityMode
?
encodingWithScalabilityMode
.
scalabilityMode
:
undefined
;
// If there is simulast, mangle spatial layers in scalabilityMode.
if
(
consumableRtpParameters
.
encodings
!
.
length
>
1
)
{
throw
new
TypeError
(
'duplicated codec.preferredPayloadType'
);
const
{
temporalLayers
}
=
parseScalabilityMode
(
scalabilityMode
);
scalabilityMode
=
`L
${
consumableRtpParameters
.
encodings
!
.
length
}
T
${
temporalLayers
}
`
;
}
// Merge the media codec parameters.
codec
.
parameters
=
{
...
codec
.
parameters
,
...
mediaCodec
.
parameters
};
if
(
scalabilityMode
)
{
consumerEncoding
.
scalabilityMode
=
scalabilityMode
;
}
// Append to the codec list.
caps
.
codecs
!
.
push
(
codec
);
// Use the maximum maxBitrate in any encoding and honor it in the Consumer's
// encoding.
const
maxEncodingMaxBitrate
=
consumableRtpParameters
.
encodings
!
.
reduce
((
maxBitrate
,
encoding
)
=>
(
encoding
.
maxBitrate
&&
encoding
.
maxBitrate
>
maxBitrate
?
encoding
.
maxBitrate
:
maxBitrate
),
0
);
// Add a RTX video codec if video.
if
(
codec
.
kind
===
'video'
)
if
(
maxEncodingMaxBitrate
)
{
// Take the first available pt and remove it from the list.
const
pt
=
dynamicPayloadTypes
.
shift
();
consumerEncoding
.
maxBitrate
=
maxEncodingMaxBitrate
;
}
if
(
!
pt
)
// Set a single encoding for the Consumer.
consumerParams
.
encodings
!
.
push
(
consumerEncoding
);
}
else
{
const
consumableEncodings
=
utils
.
clone
<
RtpEncodingParameters
[]
|
undefined
>
(
consumableRtpParameters
.
encodings
)
??
[];
const
baseSsrc
=
utils
.
generateRandomNumber
();
const
baseRtxSsrc
=
utils
.
generateRandomNumber
();
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
{
const
encoding
=
consumableEncodings
[
i
];
encoding
.
ssrc
=
baseSsrc
+
i
;
if
(
rtxSupported
)
{
throw
new
Error
(
'cannot allocate more dynamic codec payload types'
)
;
encoding
.
rtx
=
{
ssrc
:
baseRtxSsrc
+
i
}
;
}
const
rtxCodec
:
RtpCodecCapability
=
else
{
kind
:
codec
.
kind
,
mimeType
:
`
${
codec
.
kind
}
/rtx`
,
preferredPayloadType
:
pt
,
clockRate
:
codec
.
clockRate
,
parameters
:
{
apt
:
codec
.
preferredPayloadType
},
rtcpFeedback
:
[]
};
delete
encoding
.
rtx
;
}
// Append to the codec list.
caps
.
codecs
!
.
push
(
rtxCodec
);
consumerParams
.
encodings
!
.
push
(
encoding
);
}
}
return
c
ap
s
;
return
c
onsumerParam
s
;
}
/**
* Get a mapping of codec payloads and encodings of the given Producer RTP
* parameters as values expected by the Router.
* Generate RTP parameters for a pipe Consumer.
*
* It may throw if invalid or non supported RTP parameters are given.
* It keeps all original consumable encodings and removes support for BWE. If
* enableRtx is false, it also removes RTX and NACK support.
*/
export
function
getProducerRtpParametersMapping
(
params
:
RtpParameters
,
caps
:
RtpCapabilities
):
RtpMapping
export
function
getPipeConsumerRtpParameters
(
{
consumableRtpParameters
,
enableRtx
}:
{
consumableRtpParameters
:
RtpParameters
;
enableRtx
:
boolean
;
}
):
RtpParameters
{
const
rtpMapping
:
RtpMapping
=
const
consumerParams
:
RtpParameters
=
{
codecs
:
[],
encodings
:
[]
codecs
:
[],
headerExtensions
:
[],
encodings
:
[],
rtcp
:
consumableRtpParameters
.
rtcp
};
// Match parameters media codecs to capabilities media codecs.
const
codecToCapCodec
:
Map
<
RtpCodecParameters
,
RtpCodecCapability
>
=
new
Map
()
;
const
consumableCodecs
=
utils
.
clone
<
RtpCodecParameters
[]
|
undefined
>
(
consumableRtpParameters
.
codecs
)
??
[]
;
for
(
const
codec
of
params
.
c
odecs
)
for
(
const
codec
of
consumableC
odecs
)
{
if
(
isRtxCodec
(
codec
))
if
(
!
enableRtx
&&
isRtxCodec
(
codec
))
{
continue
;
}
// Search for the same media codec in capabilities.
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
(
matchCodecs
(
codec
,
capCodec
,
{
strict
:
true
,
modify
:
true
}))
);
if
(
!
matchedCapCodec
)
{
throw
new
UnsupportedError
(
`unsupported codec [mimeType:
${
codec
.
mimeType
}
, payloadType:
${
codec
.
payloadType
}
]`
);
}
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
(
fb
.
type
===
'nack'
&&
fb
.
parameter
===
'pli'
)
||
(
fb
.
type
===
'ccm'
&&
fb
.
parameter
===
'fir'
)
||
(
enableRtx
&&
fb
.
type
===
'nack'
&&
!
fb
.
parameter
)
));
co
decToCapCodec
.
set
(
codec
,
matchedCapC
odec
);
co
nsumerParams
.
codecs
.
push
(
c
odec
);
}
// Match parameters RTX codecs to capabilities RTX codecs.
for
(
const
codec
of
params
.
codecs
)
{
if
(
!
isRtxCodec
(
codec
))
{
continue
;
}
// Search for the associated media codec.
const
associatedMediaCodec
=
params
.
codecs
.
find
((
mediaCodec
)
=>
mediaCodec
.
payloadType
===
codec
.
parameters
.
apt
);
if
(
!
associatedMediaCodec
)
{
throw
new
TypeError
(
`missing media codec found for RTX PT
${
codec
.
payloadType
}
`
);
}
const
capMediaCodec
=
codecToCapCodec
.
get
(
associatedMediaCodec
);
// Ensure that the capabilities media codec has a RTX codec.
const
associatedCapRtxCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
(
isRtxCodec
(
capCodec
)
&&
capCodec
.
parameters
.
apt
===
capMediaCodec
!
.
preferredPayloadType
));
if
(
!
associatedCapRtxCodec
)
{
throw
new
UnsupportedError
(
`no RTX codec for capability codec PT
${
capMediaCodec
!
.
preferredPayloadType
}
`
);
}
codecToCapCodec
.
set
(
codec
,
associatedCapRtxCodec
);
}
// Generate codecs mapping.
for
(
const
[
codec
,
capCodec
]
of
codecToCapCodec
)
{
rtpMapping
.
codecs
.
push
(
{
payloadType
:
codec
.
payloadType
,
mappedPayloadType
:
capCodec
.
preferredPayloadType
!
});
}
// Reduce RTP extensions by disabling transport MID and BWE related ones.
consumerParams
.
headerExtensions
=
consumableRtpParameters
.
headerExtensions
!
.
filter
((
ext
)
=>
(
ext
.
uri
!==
'urn:ietf:params:rtp-hdrext:sdes:mid'
&&
ext
.
uri
!==
'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time'
&&
ext
.
uri
!==
'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01'
));
// Generate encodings mapping.
let
mappedSsrc
=
utils
.
generateRandomNumber
();
const
consumableEncodings
=
utils
.
clone
<
RtpEncodingParameters
[]
|
undefined
>
(
consumableRtpParameters
.
encodings
)
??
[];
const
baseSsrc
=
utils
.
generateRandomNumber
();
const
baseRtxSsrc
=
utils
.
generateRandomNumber
();
for
(
const
encoding
of
params
.
encodings
!
)
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
{
const
mappedEncoding
:
any
=
{}
;
const
encoding
=
consumableEncodings
[
i
]
;
mappedEncoding
.
mappedSsrc
=
mappedSsrc
++
;
encoding
.
ssrc
=
baseSsrc
+
i
;
if
(
encoding
.
rid
)
{
mappedEncoding
.
rid
=
encoding
.
rid
;
}
if
(
encoding
.
ssrc
)
if
(
enableRtx
)
{
mappedEncoding
.
ssrc
=
encoding
.
ssrc
;
encoding
.
rtx
=
{
ssrc
:
baseRtxSsrc
+
i
}
;
}
if
(
encoding
.
scalabilityMode
)
else
{
mappedEncoding
.
scalabilityMode
=
encoding
.
scalabilityMode
;
delete
encoding
.
rtx
;
}
rtpMapping
.
encodings
.
push
(
mappedE
ncoding
);
consumerParams
.
encodings
!
.
push
(
e
ncoding
);
}
return
rtpMapping
;
return
consumerParams
;
}
/**
* Generate RTP parameters to be internally used by Consumers given the RTP
* parameters of a Producer and the RTP capabilities of the Router.
*/
export
function
getConsumableRtpParameters
(
kind
:
string
,
params
:
RtpParameters
,
caps
:
RtpCapabilities
,
rtpMapping
:
RtpMapping
):
RtpParameters
function
isRtxCodec
(
codec
:
RtpCodecCapability
|
RtpCodecParameters
):
boolean
{
const
consumableParams
:
RtpParameters
=
{
codecs
:
[],
headerExtensions
:
[],
encodings
:
[],
rtcp
:
{}
};
return
/.+
\/
rtx$/i
.
test
(
codec
.
mimeType
);
}
for
(
const
codec
of
params
.
codecs
)
function
matchCodecs
(
aCodec
:
RtpCodecCapability
|
RtpCodecParameters
,
bCodec
:
RtpCodecCapability
|
RtpCodecParameters
,
{
strict
=
false
,
modify
=
false
}
=
{}
):
boolean
{
const
aMimeType
=
aCodec
.
mimeType
.
toLowerCase
();
const
bMimeType
=
bCodec
.
mimeType
.
toLowerCase
();
if
(
aMimeType
!==
bMimeType
)
{
if
(
isRtxCodec
(
codec
))
{
continue
;
}
return
false
;
}
const
consumableCodecPt
=
rtpMapping
.
codecs
.
find
((
entry
)
=>
entry
.
payloadType
===
codec
.
payloadType
)
!
.
mappedPayloadType
;
if
(
aCodec
.
clockRate
!==
bCodec
.
clockRate
)
{
return
false
;
}
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
capCodec
.
preferredPayloadType
===
consumableCodecPt
)
!
;
if
(
aCodec
.
channels
!==
bCodec
.
channels
)
{
return
false
;
}
const
consumableCodec
:
RtpCodecParameters
=
// Per codec special checks.
switch
(
aMimeType
)
{
case
'audio/multiopus'
:
{
mimeType
:
matchedCapCodec
.
mimeType
,
payloadType
:
matchedCapCodec
.
preferredPayloadType
!
,
clockRate
:
matchedCapCodec
.
clockRate
,
channels
:
matchedCapCodec
.
channels
,
parameters
:
codec
.
parameters
,
// Keep the Producer codec parameters.
rtcpFeedback
:
matchedCapCodec
.
rtcpFeedback
};
const
aNumStreams
=
aCodec
.
parameters
[
'num_streams'
];
const
bNumStreams
=
bCodec
.
parameters
[
'num_streams'
];
consumableParams
.
codecs
.
push
(
consumableCodec
);
if
(
aNumStreams
!==
bNumStreams
)
{
return
false
;
}
const
consumableCapRtxCodec
=
caps
.
codecs
!
.
find
((
capRtxCodec
)
=>
(
isRtxCodec
(
capRtxCodec
)
&&
capRtxCodec
.
parameters
.
apt
===
consumableCodec
.
payloadType
));
const
aCoupledStreams
=
aCodec
.
parameters
[
'coupled_streams'
];
const
bCoupledStreams
=
bCodec
.
parameters
[
'coupled_streams'
];
if
(
consumableCapRtxCodec
)
{
const
consumableRtxCodec
:
RtpCodecParameters
=
if
(
aCoupledStreams
!==
bCoupledStreams
)
{
mimeType
:
consumableCapRtxCodec
.
mimeType
,
payloadType
:
consumableCapRtxCodec
.
preferredPayloadType
!
,
clockRate
:
consumableCapRtxCodec
.
clockRate
,
parameters
:
consumableCapRtxCodec
.
parameters
,
rtcpFeedback
:
consumableCapRtxCodec
.
rtcpFeedback
};
return
false
;
}
consumableParams
.
codecs
.
push
(
consumableRtxCodec
)
;
break
;
}
}
for
(
const
capExt
of
caps
.
headerExtensions
!
)
{
// Just take RTP header extension that can be used in Consumers.
if
(
capExt
.
kind
!==
kind
||
(
capExt
.
direction
!==
'sendrecv'
&&
capExt
.
direction
!==
'sendonly'
)
)
case
'video/h264'
:
case
'video/h264-svc'
:
{
continue
;
}
if
(
strict
)
{
const
aPacketizationMode
=
aCodec
.
parameters
[
'packetization-mode'
]
||
0
;
const
bPacketizationMode
=
bCodec
.
parameters
[
'packetization-mode'
]
||
0
;
const
consumableExt
=
{
uri
:
capExt
.
uri
,
id
:
capExt
.
preferredId
,
encrypt
:
capExt
.
preferredEncrypt
,
parameters
:
{}
};
if
(
aPacketizationMode
!==
bPacketizationMode
)
{
return
false
;
}
consumableParams
.
headerExtensions
!
.
push
(
consumableExt
);
}
if
(
!
h264
.
isSameProfile
(
aCodec
.
parameters
,
bCodec
.
parameters
))
{
return
false
;
}
// Clone Producer encodings since we'll mangle them.
const
consumableEncodings
=
(
utils
.
clone
(
params
.
encodings
)
??
[])
as
RtpEncodingParameters
[];
let
selectedProfileLevelId
;
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
{
const
consumableEncoding
=
consumableEncodings
[
i
];
const
{
mappedSsrc
}
=
rtpMapping
.
encodings
[
i
];
try
{
selectedProfileLevelId
=
h264
.
generateProfileLevelIdStringForAnswer
(
aCodec
.
parameters
,
bCodec
.
parameters
);
}
catch
(
error
)
{
return
false
;
}
// Remove useless fields.
delete
consumableEncoding
.
rid
;
delete
consumableEncoding
.
rtx
;
delete
consumableEncoding
.
codecPayloadType
;
if
(
modify
)
{
if
(
selectedProfileLevelId
)
{
aCodec
.
parameters
[
'profile-level-id'
]
=
selectedProfileLevelId
;
}
else
{
delete
aCodec
.
parameters
[
'profile-level-id'
];
}
}
}
// Set the mapped ssrc.
consumableEncoding
.
ssrc
=
mappedSsrc
;
break
;
}
consumableParams
.
encodings
!
.
push
(
consumableEncoding
);
}
case
'video/vp9'
:
{
if
(
strict
)
{
const
aProfileId
=
aCodec
.
parameters
[
'profile-id'
]
||
0
;
const
bProfileId
=
bCodec
.
parameters
[
'profile-id'
]
||
0
;
consumableParams
.
rtcp
=
{
cname
:
params
.
rtcp
!
.
cname
,
reducedSize
:
true
};
if
(
aProfileId
!==
bProfileId
)
{
return
false
;
}
}
return
consumableParams
;
break
;
}
}
return
true
;
}
/**
* Check whether the given RTP capabilities can consume the given Producer.
*/
export
function
canConsume
(
consumableParams
:
RtpParameters
,
caps
:
RtpCapabilities
):
boolean
export
function
serializeRtpMapping
(
builder
:
flatbuffers
.
Builder
,
rtpMapping
:
RtpMapping
):
number
{
// This may throw.
validateRtpCapabilities
(
caps
);
const
matchingCodecs
:
RtpCodecParameters
[]
=
[];
const
codecs
:
number
[]
=
[];
for
(
const
codec
of
consumableParams
.
codecs
)
for
(
const
codec
of
rtpMapping
.
codecs
)
{
const
matchedCapCodec
=
caps
.
codecs
!
.
find
((
capCodec
)
=>
matchCodecs
(
capCodec
,
codec
,
{
strict
:
true
}));
if
(
!
matchedCapCodec
)
{
continue
;
}
matchingCodecs
.
push
(
codec
);
codecs
.
push
(
FbsRtpParameters
.
CodecMapping
.
createCodecMapping
(
builder
,
codec
.
payloadType
,
codec
.
mappedPayloadType
)
);
}
const
codecsOffset
=
FbsRtpParameters
.
RtpMapping
.
createCodecsVector
(
builder
,
codecs
);
// Ensure there is at least one media codec.
if
(
matchingCodecs
.
length
===
0
||
isRtxCodec
(
matchingCodecs
[
0
]))
const
encodings
:
number
[]
=
[];
for
(
const
encoding
of
rtpMapping
.
encodings
)
{
return
false
;
encodings
.
push
(
FbsRtpParameters
.
EncodingMapping
.
createEncodingMapping
(
builder
,
builder
.
createString
(
encoding
.
rid
),
encoding
.
ssrc
??
null
,
builder
.
createString
(
encoding
.
scalabilityMode
),
encoding
.
mappedSsrc
)
);
}
return
true
;
const
encodingsOffset
=
FbsRtpParameters
.
RtpMapping
.
createEncodingsVector
(
builder
,
encodings
);
return
FbsRtpParameters
.
RtpMapping
.
createRtpMapping
(
builder
,
codecsOffset
,
encodingsOffset
);
}
/**
* Generate RTP parameters for a specific Consumer.
*
* It reduces encodings to just one and takes into account given RTP capabilities
* to reduce codecs, codecs' RTCP feedback and header extensions, and also enables
* or disables RTX.
* Validates RtpCodecCapability. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
getConsumerRtpParameters
(
{
consumableRtpParameters
,
remoteRtpCapabilities
,
pipe
,
enableRtx
}:
{
consumableRtpParameters
:
RtpParameters
;
remoteRtpCapabilities
:
RtpCapabilities
;
pipe
:
boolean
;
enableRtx
:
boolean
;
}
):
RtpParameters
function
validateRtpCodecCapability
(
codec
:
RtpCodecCapability
):
void
{
const
consumerParams
:
RtpParameters
=
{
codecs
:
[],
headerExtensions
:
[],
encodings
:
[],
rtcp
:
consumableRtpParameters
.
rtcp
};
const
MimeTypeRegex
=
new
RegExp
(
'^(audio|video)/(.+)'
,
'i'
);
for
(
const
capCodec
of
remoteRtpCapabilities
.
codecs
!
)
if
(
typeof
codec
!==
'object'
)
{
validateRtpCodecCapability
(
capCodec
);
throw
new
TypeError
(
'codec is not an object'
);
}
const
consumableCodecs
=
(
utils
.
clone
(
consumableRtpParameters
.
codecs
)
??
[])
as
RtpCodecParameters
[];
let
rtxSupported
=
false
;
for
(
const
codec
of
consumableCodecs
)
// mimeType is mandatory.
if
(
!
codec
.
mimeType
||
typeof
codec
.
mimeType
!==
'string'
)
{
if
(
!
enableRtx
&&
isRtxCodec
(
codec
))
{
continue
;
}
const
matchedCapCodec
=
remoteRtpCapabilities
.
codecs
!
.
find
((
capCodec
)
=>
matchCodecs
(
capCodec
,
codec
,
{
strict
:
true
}));
if
(
!
matchedCapCodec
)
{
continue
;
}
codec
.
rtcpFeedback
=
matchedCapCodec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
(
enableRtx
||
fb
.
type
!==
'nack'
||
fb
.
parameter
)
));
consumerParams
.
codecs
.
push
(
codec
);
throw
new
TypeError
(
'missing codec.mimeType'
);
}
// Must sanitize the list of matched codecs by removing useless RTX codecs.
for
(
let
idx
=
consumerParams
.
codecs
.
length
-
1
;
idx
>=
0
;
--
idx
)
{
const
codec
=
consumerParams
.
codecs
[
idx
];
if
(
isRtxCodec
(
codec
))
{
// Search for the associated media codec.
const
associatedMediaCodec
=
consumerParams
.
codecs
.
find
((
mediaCodec
)
=>
mediaCodec
.
payloadType
===
codec
.
parameters
.
apt
);
const
mimeTypeMatch
=
MimeTypeRegex
.
exec
(
codec
.
mimeType
);
if
(
associatedMediaCodec
)
{
rtxSupported
=
true
;
}
else
{
consumerParams
.
codecs
.
splice
(
idx
,
1
);
}
}
if
(
!
mimeTypeMatch
)
{
throw
new
TypeError
(
'invalid codec.mimeType'
);
}
// Ensure there is at least one media codec.
if
(
consumerParams
.
codecs
.
length
===
0
||
isRtxCodec
(
consumerParams
.
codecs
[
0
]))
// Just override kind with media component of mimeType.
codec
.
kind
=
mimeTypeMatch
[
1
].
toLowerCase
()
as
MediaKind
;
// preferredPayloadType is optional.
if
(
codec
.
preferredPayloadType
&&
typeof
codec
.
preferredPayloadType
!==
'number'
)
{
throw
new
UnsupportedError
(
'no compatible media codecs
'
);
throw
new
TypeError
(
'invalid codec.preferredPayloadType
'
);
}
consumerParams
.
headerExtensions
=
consumableRtpParameters
.
headerExtensions
!
.
filter
((
ext
)
=>
(
remoteRtpCapabilities
.
headerExtensions
!
.
some
((
capExt
)
=>
(
capExt
.
preferredId
===
ext
.
id
&&
capExt
.
uri
===
ext
.
uri
))
));
// Reduce codecs' RTCP feedback. Use Transport-CC if available, REMB otherwise.
if
(
consumerParams
.
headerExtensions
.
some
((
ext
)
=>
(
ext
.
uri
===
'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01'
))
)
// clockRate is mandatory.
if
(
typeof
codec
.
clockRate
!==
'number'
)
{
for
(
const
codec
of
consumerParams
.
codecs
)
{
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
fb
.
type
!==
'goog-remb'
);
}
throw
new
TypeError
(
'missing codec.clockRate'
);
}
else
if
(
consumerParams
.
headerExtensions
.
some
((
ext
)
=>
(
ext
.
uri
===
'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time'
))
)
// channels is optional. If unset, set it to 1 (just if audio).
if
(
codec
.
kind
===
'audio'
)
{
for
(
const
codec
of
consumerParams
.
codecs
)
if
(
typeof
codec
.
channels
!==
'number'
)
{
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
fb
.
type
!==
'transport-cc'
);
codec
.
channels
=
1
;
}
}
else
{
for
(
const
codec
of
consumerParams
.
codecs
)
{
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
fb
.
type
!==
'transport-cc'
&&
fb
.
type
!==
'goog-remb'
));
}
delete
codec
.
channels
;
}
if
(
!
pipe
)
// parameters is optional. If unset, set it to an empty object.
if
(
!
codec
.
parameters
||
typeof
codec
.
parameters
!==
'object'
)
{
const
consumerEncoding
:
RtpEncodingParameters
=
{
ssrc
:
utils
.
generateRandomNumber
()
};
codec
.
parameters
=
{};
}
if
(
rtxSupported
)
for
(
const
key
of
Object
.
keys
(
codec
.
parameters
))
{
let
value
=
codec
.
parameters
[
key
];
if
(
value
===
undefined
)
{
consumerEncoding
.
rtx
=
{
ssrc
:
consumerEncoding
.
ssrc
!
+
1
};
codec
.
parameters
[
key
]
=
''
;
value
=
''
;
}
// If any of the consumableRtpParameters.encodings has scalabilityMode,
// process it (assume all encodings have the same value).
const
encodingWithScalabilityMode
=
consumableRtpParameters
.
encodings
!
.
find
((
encoding
)
=>
encoding
.
scalabilityMode
);
let
scalabilityMode
=
encodingWithScalabilityMode
?
encodingWithScalabilityMode
.
scalabilityMode
:
undefined
;
// If there is simulast, mangle spatial layers in scalabilityMode.
if
(
consumableRtpParameters
.
encodings
!
.
length
>
1
)
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
{
const
{
temporalLayers
}
=
parseScalabilityMode
(
scalabilityMode
);
scalabilityMode
=
`L
${
consumableRtpParameters
.
encodings
!
.
length
}
T
${
temporalLayers
}
`
;
throw
new
TypeError
(
`invalid codec parameter [key:
${
key
}
s, value:
${
value
}
]`
);
}
if
(
scalabilityMode
)
// Specific parameters validation.
if
(
key
===
'apt'
)
{
consumerEncoding
.
scalabilityMode
=
scalabilityMode
;
if
(
typeof
value
!==
'number'
)
{
throw
new
TypeError
(
'invalid codec apt parameter'
);
}
}
}
// Use the maximum maxBitrate in any encoding and honor it in the Consumer's
// encoding.
const
maxEncodingMaxBitrate
=
consumableRtpParameters
.
encodings
!
.
reduce
((
maxBitrate
,
encoding
)
=>
(
encoding
.
maxBitrate
&&
encoding
.
maxBitrate
>
maxBitrate
?
encoding
.
maxBitrate
:
maxBitrate
),
0
);
// rtcpFeedback is optional. If unset, set it to an empty array.
if
(
!
codec
.
rtcpFeedback
||
!
Array
.
isArray
(
codec
.
rtcpFeedback
))
{
codec
.
rtcpFeedback
=
[];
}
if
(
maxEncodingMaxBitrate
)
{
consumerEncoding
.
maxBitrate
=
maxEncodingMaxBitrate
;
}
for
(
const
fb
of
codec
.
rtcpFeedback
)
{
validateRtcpFeedback
(
fb
);
}
}
// Set a single encoding for the Consumer.
consumerParams
.
encodings
!
.
push
(
consumerEncoding
);
/**
* Validates RtcpFeedback. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
function
validateRtcpFeedback
(
fb
:
RtcpFeedback
):
void
{
if
(
typeof
fb
!==
'object'
)
{
throw
new
TypeError
(
'fb is not an object'
);
}
else
// type is mandatory.
if
(
!
fb
.
type
||
typeof
fb
.
type
!==
'string'
)
{
const
consumableEncodings
=
(
utils
.
clone
(
consumableRtpParameters
.
encodings
)
??
[])
as
RtpEncodingParameters
[];
const
baseSsrc
=
utils
.
generateRandomNumber
();
const
baseRtxSsrc
=
utils
.
generateRandomNumber
();
throw
new
TypeError
(
'missing fb.type'
);
}
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
{
const
encoding
=
consumableEncodings
[
i
];
// parameter is optional. If unset set it to an empty string.
if
(
!
fb
.
parameter
||
typeof
fb
.
parameter
!==
'string'
)
{
fb
.
parameter
=
''
;
}
}
encoding
.
ssrc
=
baseSsrc
+
i
;
/**
* Validates RtpHeaderExtension. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
function
validateRtpHeaderExtension
(
ext
:
RtpHeaderExtension
):
void
{
if
(
rtxSupported
)
{
encoding
.
rtx
=
{
ssrc
:
baseRtxSsrc
+
i
};
}
else
{
delete
encoding
.
rtx
;
}
if
(
typeof
ext
!==
'object'
)
{
throw
new
TypeError
(
'ext is not an object'
);
}
consumerParams
.
encodings
!
.
push
(
encoding
);
}
if
(
ext
.
kind
!==
'audio'
&&
ext
.
kind
!==
'video'
)
{
throw
new
TypeError
(
'invalid ext.kind'
);
}
return
consumerParams
;
// uri is mandatory.
if
(
!
ext
.
uri
||
typeof
ext
.
uri
!==
'string'
)
{
throw
new
TypeError
(
'missing ext.uri'
);
}
// preferredId is mandatory.
if
(
typeof
ext
.
preferredId
!==
'number'
)
{
throw
new
TypeError
(
'missing ext.preferredId'
);
}
// preferredEncrypt is optional. If unset set it to false.
if
(
ext
.
preferredEncrypt
&&
typeof
ext
.
preferredEncrypt
!==
'boolean'
)
{
throw
new
TypeError
(
'invalid ext.preferredEncrypt'
);
}
else
if
(
!
ext
.
preferredEncrypt
)
{
ext
.
preferredEncrypt
=
false
;
}
// direction is optional. If unset set it to sendrecv.
if
(
ext
.
direction
&&
typeof
ext
.
direction
!==
'string'
)
{
throw
new
TypeError
(
'invalid ext.direction'
);
}
else
if
(
!
ext
.
direction
)
{
ext
.
direction
=
'sendrecv'
;
}
}
/**
* Generate RTP parameters for a pipe Consumer.
*
* It keeps all original consumable encodings and removes support for BWE. If
* enableRtx is false, it also removes RTX and NACK support.
* Validates RtpCodecParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
export
function
getPipeConsumerRtpParameters
(
{
consumableRtpParameters
,
enableRtx
}:
function
validateRtpCodecParameters
(
codec
:
RtpCodecParameters
):
void
{
const
MimeTypeRegex
=
new
RegExp
(
'^(audio|video)/(.+)'
,
'i'
);
if
(
typeof
codec
!==
'object'
)
{
consumableRtpParameters
:
RtpParameters
;
enableRtx
:
boolean
;
throw
new
TypeError
(
'codec is not an object'
);
}
):
RtpParameters
{
const
consumerParams
:
RtpParameters
=
// mimeType is mandatory.
if
(
!
codec
.
mimeType
||
typeof
codec
.
mimeType
!==
'string'
)
{
codecs
:
[],
headerExtensions
:
[],
encodings
:
[],
rtcp
:
consumableRtpParameters
.
rtcp
};
throw
new
TypeError
(
'missing codec.mimeType'
);
}
const
consumableCodecs
=
(
utils
.
clone
(
consumableRtpParameters
.
codecs
)
??
[])
as
RtpCodecParameters
[];
const
mimeTypeMatch
=
MimeTypeRegex
.
exec
(
codec
.
mimeType
);
for
(
const
codec
of
consumableCodecs
)
if
(
!
mimeTypeMatch
)
{
if
(
!
enableRtx
&&
isRtxCodec
(
codec
))
{
continue
;
}
throw
new
TypeError
(
'invalid codec.mimeType'
);
}
codec
.
rtcpFeedback
=
codec
.
rtcpFeedback
!
.
filter
((
fb
)
=>
(
(
fb
.
type
===
'nack'
&&
fb
.
parameter
===
'pli'
)
||
(
fb
.
type
===
'ccm'
&&
fb
.
parameter
===
'fir'
)
||
(
enableRtx
&&
fb
.
type
===
'nack'
&&
!
fb
.
parameter
)
));
// payloadType is mandatory.
if
(
typeof
codec
.
payloadType
!==
'number'
)
{
throw
new
TypeError
(
'missing codec.payloadType'
);
}
consumerParams
.
codecs
.
push
(
codec
);
// clockRate is mandatory.
if
(
typeof
codec
.
clockRate
!==
'number'
)
{
throw
new
TypeError
(
'missing codec.clockRate'
);
}
// Reduce RTP extensions by disabling transport MID and BWE related ones.
consumerParams
.
headerExtensions
=
consumableRtpParameters
.
headerExtensions
!
.
filter
((
ext
)
=>
(
ext
.
uri
!==
'urn:ietf:params:rtp-hdrext:sdes:mid'
&&
ext
.
uri
!==
'http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time'
&&
ext
.
uri
!==
'http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01'
));
const
kind
=
mimeTypeMatch
[
1
].
toLowerCase
()
as
MediaKind
;
const
consumableEncodings
=
(
utils
.
clone
(
consumableRtpParameters
.
encodings
)
??
[])
as
RtpEncodingParameters
[];
const
baseSsrc
=
utils
.
generateRandomNumber
();
const
baseRtxSsrc
=
utils
.
generateRandomNumber
();
// channels is optional. If unset, set it to 1 (just if audio).
if
(
kind
===
'audio'
)
{
if
(
typeof
codec
.
channels
!==
'number'
)
{
codec
.
channels
=
1
;
}
}
else
{
delete
codec
.
channels
;
}
for
(
let
i
=
0
;
i
<
consumableEncodings
.
length
;
++
i
)
// parameters is optional. If unset, set it to an empty object.
if
(
!
codec
.
parameters
||
typeof
codec
.
parameters
!==
'object'
)
{
const
encoding
=
consumableEncodings
[
i
];
codec
.
parameters
=
{};
}
encoding
.
ssrc
=
baseSsrc
+
i
;
for
(
const
key
of
Object
.
keys
(
codec
.
parameters
))
{
let
value
=
codec
.
parameters
[
key
];
if
(
enableRtx
)
if
(
value
===
undefined
)
{
encoding
.
rtx
=
{
ssrc
:
baseRtxSsrc
+
i
};
codec
.
parameters
[
key
]
=
''
;
value
=
''
;
}
else
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
{
delete
encoding
.
rtx
;
throw
new
TypeError
(
`invalid codec parameter [key:
${
key
}
s, value:
${
value
}
]`
);
}
consumerParams
.
encodings
!
.
push
(
encoding
);
// Specific parameters validation.
if
(
key
===
'apt'
)
{
if
(
typeof
value
!==
'number'
)
{
throw
new
TypeError
(
'invalid codec apt parameter'
);
}
}
}
return
consumerParams
;
}
// rtcpFeedback is optional. If unset, set it to an empty array.
if
(
!
codec
.
rtcpFeedback
||
!
Array
.
isArray
(
codec
.
rtcpFeedback
))
{
codec
.
rtcpFeedback
=
[];
}
function
isRtxCodec
(
codec
:
RtpCodecCapability
|
RtpCodecParameters
):
boolean
{
return
/.+
\/
rtx$/i
.
test
(
codec
.
mimeType
);
for
(
const
fb
of
codec
.
rtcpFeedback
)
{
validateRtcpFeedback
(
fb
);
}
}
function
matchCodecs
(
aCodec
:
RtpCodecCapability
|
RtpCodecParameters
,
bCodec
:
RtpCodecCapability
|
RtpCodecParameters
,
{
strict
=
false
,
modify
=
false
}
=
{}
):
boolean
/**
* Validates RtpHeaderExtensionParameteters. It may modify given data by adding
* missing fields with default values. It throws if invalid.
*/
function
validateRtpHeaderExtensionParameters
(
ext
:
RtpHeaderExtensionParameters
):
void
{
const
aMimeType
=
aCodec
.
mimeType
.
toLowerCase
();
const
bMimeType
=
bCodec
.
mimeType
.
toLowerCase
();
if
(
aMimeType
!==
bMimeType
)
if
(
typeof
ext
!==
'object'
)
{
return
false
;
throw
new
TypeError
(
'ext is not an object'
)
;
}
if
(
aCodec
.
clockRate
!==
bCodec
.
clockRate
)
// uri is mandatory.
if
(
!
ext
.
uri
||
typeof
ext
.
uri
!==
'string'
)
{
return
false
;
throw
new
TypeError
(
'missing ext.uri'
)
;
}
if
(
aCodec
.
channels
!==
bCodec
.
channels
)
// id is mandatory.
if
(
typeof
ext
.
id
!==
'number'
)
{
return
false
;
throw
new
TypeError
(
'missing ext.id'
)
;
}
//
Per codec special checks
.
switch
(
aMimeType
)
//
encrypt is optional. If unset set it to false
.
if
(
ext
.
encrypt
&&
typeof
ext
.
encrypt
!==
'boolean'
)
{
case
'audio/multiopus'
:
{
const
aNumStreams
=
aCodec
.
parameters
[
'num_streams'
];
const
bNumStreams
=
bCodec
.
parameters
[
'num_streams'
];
if
(
aNumStreams
!==
bNumStreams
)
{
return
false
;
}
throw
new
TypeError
(
'invalid ext.encrypt'
);
}
else
if
(
!
ext
.
encrypt
)
{
ext
.
encrypt
=
false
;
}
const
aCoupledStreams
=
aCodec
.
parameters
[
'coupled_streams'
];
const
bCoupledStreams
=
bCodec
.
parameters
[
'coupled_streams'
];
// parameters is optional. If unset, set it to an empty object.
if
(
!
ext
.
parameters
||
typeof
ext
.
parameters
!==
'object'
)
{
ext
.
parameters
=
{};
}
if
(
aCoupledStreams
!==
bCoupledStreams
)
{
return
false
;
}
for
(
const
key
of
Object
.
keys
(
ext
.
parameters
))
{
let
value
=
ext
.
parameters
[
key
];
break
;
if
(
value
===
undefined
)
{
ext
.
parameters
[
key
]
=
''
;
value
=
''
;
}
case
'video/h264'
:
case
'video/h264-svc'
:
if
(
typeof
value
!==
'string'
&&
typeof
value
!==
'number'
)
{
if
(
strict
)
{
const
aPacketizationMode
=
aCodec
.
parameters
[
'packetization-mode'
]
||
0
;
const
bPacketizationMode
=
bCodec
.
parameters
[
'packetization-mode'
]
||
0
;
if
(
aPacketizationMode
!==
bPacketizationMode
)
{
return
false
;
}
if
(
!
h264
.
isSameProfile
(
aCodec
.
parameters
,
bCodec
.
parameters
))
{
return
false
;
}
let
selectedProfileLevelId
;
throw
new
TypeError
(
'invalid header extension parameter'
);
}
}
}
try
{
selectedProfileLevelId
=
h264
.
generateProfileLevelIdStringForAnswer
(
aCodec
.
parameters
,
bCodec
.
parameters
);
}
catch
(
error
)
{
return
false
;
}
/**
* Validates RtpEncodingParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
function
validateRtpEncodingParameters
(
encoding
:
RtpEncodingParameters
):
void
{
if
(
typeof
encoding
!==
'object'
)
{
throw
new
TypeError
(
'encoding is not an object'
)
;
}
if
(
modify
)
{
if
(
selectedProfileLevelId
)
{
aCodec
.
parameters
[
'profile-level-id'
]
=
selectedProfileLevelId
;
}
else
{
delete
aCodec
.
parameters
[
'profile-level-id'
];
}
}
}
// ssrc is optional.
if
(
encoding
.
ssrc
&&
typeof
encoding
.
ssrc
!==
'number'
)
{
throw
new
TypeError
(
'invalid encoding.ssrc'
);
}
break
;
}
// rid is optional.
if
(
encoding
.
rid
&&
typeof
encoding
.
rid
!==
'string'
)
{
throw
new
TypeError
(
'invalid encoding.rid'
);
}
case
'video/vp9'
:
// rtx is optional.
if
(
encoding
.
rtx
&&
typeof
encoding
.
rtx
!==
'object'
)
{
throw
new
TypeError
(
'invalid encoding.rtx'
);
}
else
if
(
encoding
.
rtx
)
{
// RTX ssrc is mandatory if rtx is present.
if
(
typeof
encoding
.
rtx
.
ssrc
!==
'number'
)
{
if
(
strict
)
{
const
aProfileId
=
aCodec
.
parameters
[
'profile-id'
]
||
0
;
const
bProfileId
=
bCodec
.
parameters
[
'profile-id'
]
||
0
;
if
(
aProfileId
!==
bProfileId
)
{
return
false
;
}
}
break
;
throw
new
TypeError
(
'missing encoding.rtx.ssrc'
);
}
}
return
true
;
// dtx is optional. If unset set it to false.
if
(
!
encoding
.
dtx
||
typeof
encoding
.
dtx
!==
'boolean'
)
{
encoding
.
dtx
=
false
;
}
// scalabilityMode is optional.
if
(
encoding
.
scalabilityMode
&&
typeof
encoding
.
scalabilityMode
!==
'string'
)
{
throw
new
TypeError
(
'invalid encoding.scalabilityMode'
);
}
}
export
function
serializeRtpMapping
(
builder
:
flatbuffers
.
Builder
,
rtpMapping
:
RtpMapping
):
number
/**
* Validates RtcpParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
function
validateRtcpParameters
(
rtcp
:
RtcpParameters
):
void
{
const
codecs
:
number
[]
=
[];
for
(
const
codec
of
rtpMapping
.
codecs
)
if
(
typeof
rtcp
!==
'object'
)
{
codecs
.
push
(
FbsRtpParameters
.
CodecMapping
.
createCodecMapping
(
builder
,
codec
.
payloadType
,
codec
.
mappedPayloadType
)
);
throw
new
TypeError
(
'rtcp is not an object'
);
}
const
codecsOffset
=
FbsRtpParameters
.
RtpMapping
.
createCodecsVector
(
builder
,
codecs
);
const
encodings
:
number
[]
=
[];
for
(
const
encoding
of
rtpMapping
.
encodings
)
// cname is optional.
if
(
rtcp
.
cname
&&
typeof
rtcp
.
cname
!==
'string'
)
{
encodings
.
push
(
FbsRtpParameters
.
EncodingMapping
.
createEncodingMapping
(
builder
,
builder
.
createString
(
encoding
.
rid
),
encoding
.
ssrc
??
null
,
builder
.
createString
(
encoding
.
scalabilityMode
),
encoding
.
mappedSsrc
)
);
throw
new
TypeError
(
'invalid rtcp.cname'
);
}
const
encodingsOffset
=
FbsRtpParameters
.
RtpMapping
.
createEncodingsVector
(
builder
,
encodings
);
return
FbsRtpParameters
.
RtpMapping
.
createRtpMapping
(
builder
,
codecsOffset
,
encodingsOffset
);
// reducedSize is optional. If unset set it to true.
if
(
!
rtcp
.
reducedSize
||
typeof
rtcp
.
reducedSize
!==
'boolean'
)
{
rtcp
.
reducedSize
=
true
;
}
}
node/src/tests/test-ortc.ts
浏览文件 @
90fff361
...
...
@@ -604,6 +604,7 @@ test('getProducerRtpParametersMapping() with incompatible params throws Unsuppor
};
expect
(
()
=>
ortc
.
getProducerRtpParametersMapping
(
rtpParameters
,
routerRtpCapabilities
))
.
toThrow
(
UnsupportedError
);
()
=>
ortc
.
getProducerRtpParametersMapping
(
rtpParameters
,
routerRtpCapabilities
)).
toThrow
(
UnsupportedError
);
});
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论