Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
3cde2cb8
Unverified
提交
3cde2cb8
authored
4月 29, 2021
作者:
Vittorio Palmisano
提交者:
GitHub
4月 29, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize rate calculator (v2) (#547)
上级
6f8953a3
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
104 行增加
和
66 行删除
+104
-66
RateCalculator.hpp
worker/include/RTC/RateCalculator.hpp
+20
-16
RateCalculator.cpp
worker/src/RTC/RateCalculator.cpp
+51
-40
TestRateCalculator.cpp
worker/test/src/RTC/TestRateCalculator.cpp
+33
-10
没有找到文件。
worker/include/RTC/RateCalculator.hpp
浏览文件 @
3cde2cb8
...
@@ -14,15 +14,17 @@ namespace RTC
...
@@ -14,15 +14,17 @@ namespace RTC
public
:
public
:
static
constexpr
size_t
DefaultWindowSize
{
1000u
};
static
constexpr
size_t
DefaultWindowSize
{
1000u
};
static
constexpr
float
DefaultBpsScale
{
8000.0
f
};
static
constexpr
float
DefaultBpsScale
{
8000.0
f
};
static
constexpr
uint16_t
DefaultWindowItems
{
100
0
u
};
static
constexpr
uint16_t
DefaultWindowItems
{
100u
};
public
:
public
:
RateCalculator
(
RateCalculator
(
size_t
windowSize
=
DefaultWindowSize
,
size_t
windowSize
Ms
=
DefaultWindowSize
,
float
scale
=
DefaultBpsScale
,
float
scale
=
DefaultBpsScale
,
uint16_t
windowItems
=
DefaultWindowItems
)
uint16_t
windowItems
=
DefaultWindowItems
)
:
windowSize
(
windowSize
),
scale
(
scale
),
windowItems
(
windowItems
)
:
windowSize
Ms
(
windowSizeMs
),
scale
(
scale
),
windowItems
(
windowItems
)
{
{
this
->
itemSizeMs
=
std
::
max
(
windowSizeMs
/
windowItems
,
static_cast
<
size_t
>
(
1
));
Reset
();
Reset
();
}
}
void
Update
(
size_t
size
,
uint64_t
nowMs
);
void
Update
(
size_t
size
,
uint64_t
nowMs
);
...
@@ -43,13 +45,13 @@ namespace RTC
...
@@ -43,13 +45,13 @@ namespace RTC
void
Reset
(
uint64_t
nowMs
)
void
Reset
(
uint64_t
nowMs
)
{
{
this
->
buffer
.
reset
(
new
BufferItem
[
this
->
windowItems
]);
this
->
buffer
.
reset
(
new
BufferItem
[
this
->
windowItems
]);
this
->
newest
Time
=
0u
;
this
->
newest
ItemStartTime
=
0u
;
this
->
newest
TimeIndex
=
-
1
;
this
->
newest
ItemIndex
=
-
1
;
this
->
oldest
TimeIndex
=
0u
;
this
->
oldest
ItemStartTime
=
0u
;
this
->
oldestIndex
=
-
1
;
this
->
oldestI
temI
ndex
=
-
1
;
this
->
totalCount
=
0u
;
this
->
totalCount
=
0u
;
this
->
lastRate
=
0u
;
this
->
lastRate
=
0u
;
this
->
lastTime
=
0u
;
this
->
lastTime
=
0u
;
}
}
private
:
private
:
...
@@ -61,21 +63,23 @@ namespace RTC
...
@@ -61,21 +63,23 @@ namespace RTC
private
:
private
:
// Window Size (in milliseconds).
// Window Size (in milliseconds).
size_t
windowSize
{
DefaultWindowSize
};
size_t
windowSize
Ms
{
DefaultWindowSize
};
// Scale in which the rate is represented.
// Scale in which the rate is represented.
float
scale
{
DefaultBpsScale
};
float
scale
{
DefaultBpsScale
};
// Window Size (number of items).
// Window Size (number of items).
uint16_t
windowItems
{
DefaultWindowItems
};
uint16_t
windowItems
{
DefaultWindowItems
};
// Item Size (in milliseconds), calculated as: windowSizeMs / windowItems.
size_t
itemSizeMs
{
0u
};
// Buffer to keep data.
// Buffer to keep data.
std
::
unique_ptr
<
BufferItem
[]
>
buffer
;
std
::
unique_ptr
<
BufferItem
[]
>
buffer
;
// Time (in milliseconds) for last item in the time window.
// Time (in milliseconds) for last item in the time window.
uint64_t
newestTime
{
0u
};
uint64_t
newest
ItemStart
Time
{
0u
};
// Index for the last item in the time window.
// Index for the last item in the time window.
int32_t
newest
Time
Index
{
-
1
};
int32_t
newest
Item
Index
{
-
1
};
// Time (in milliseconds) for oldest item in the time window.
// Time (in milliseconds) for oldest item in the time window.
uint64_t
oldest
TimeIndex
{
0u
};
uint64_t
oldest
ItemStartTime
{
0u
};
// Index for the oldest item in the time window.
// Index for the oldest item in the time window.
int32_t
oldestIndex
{
-
1
};
int32_t
oldestI
temI
ndex
{
-
1
};
// Total count in the time window.
// Total count in the time window.
size_t
totalCount
{
0u
};
size_t
totalCount
{
0u
};
// Total bytes transmitted.
// Total bytes transmitted.
...
@@ -89,7 +93,7 @@ namespace RTC
...
@@ -89,7 +93,7 @@ namespace RTC
class
RtpDataCounter
class
RtpDataCounter
{
{
public
:
public
:
explicit
RtpDataCounter
(
size_t
windowSize
=
2500
)
:
rate
(
windowSize
)
explicit
RtpDataCounter
(
size_t
windowSize
Ms
=
2500
)
:
rate
(
windowSizeMs
)
{
{
}
}
...
...
worker/src/RTC/RateCalculator.cpp
浏览文件 @
3cde2cb8
...
@@ -12,7 +12,7 @@ namespace RTC
...
@@ -12,7 +12,7 @@ namespace RTC
MS_TRACE
();
MS_TRACE
();
// Ignore too old data. Should never happen.
// Ignore too old data. Should never happen.
if
(
nowMs
<
this
->
oldest
TimeIndex
)
if
(
nowMs
<
this
->
oldest
ItemStartTime
)
return
;
return
;
// Increase bytes.
// Increase bytes.
...
@@ -20,38 +20,49 @@ namespace RTC
...
@@ -20,38 +20,49 @@ namespace RTC
RemoveOldData
(
nowMs
);
RemoveOldData
(
nowMs
);
// Increase the index.
// If the elapsed time from the newest item start time is greater than the
if
(
++
this
->
newestTimeIndex
>=
this
->
windowItems
)
// item size (in milliseconds), increase the item index.
this
->
newestTimeIndex
=
0
;
if
(
this
->
newestItemIndex
<
0
||
nowMs
-
this
->
newestItemStartTime
>=
this
->
itemSizeMs
)
// Newest index overlaps with the oldest one, remove it.
if
(
this
->
newestTimeIndex
==
this
->
oldestIndex
&&
this
->
oldestIndex
!=
-
1
)
{
{
MS_WARN_TAG
(
this
->
newestItemIndex
++
;
info
,
this
->
newestItemStartTime
=
nowMs
;
"calculation buffer full, windowSize:%zu ms windowItems:%"
PRIu16
,
if
(
this
->
newestItemIndex
>=
this
->
windowItems
)
this
->
windowSize
,
this
->
newestItemIndex
=
0
;
this
->
windowItems
);
// Newest index overlaps with the oldest one, remove it.
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestIndex
];
if
(
this
->
newestItemIndex
==
this
->
oldestItemIndex
&&
this
->
oldestItemIndex
!=
-
1
)
this
->
totalCount
-=
oldestItem
.
count
;
{
oldestItem
.
count
=
0u
;
MS_WARN_TAG
(
oldestItem
.
time
=
0u
;
info
,
if
(
++
this
->
oldestIndex
>=
this
->
windowItems
)
"calculation buffer full, windowSizeMs:%zu ms windowItems:%"
PRIu16
,
this
->
oldestIndex
=
0
;
this
->
windowSizeMs
,
this
->
windowItems
);
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestItemIndex
];
this
->
totalCount
-=
oldestItem
.
count
;
oldestItem
.
count
=
0u
;
oldestItem
.
time
=
0u
;
if
(
++
this
->
oldestItemIndex
>=
this
->
windowItems
)
this
->
oldestItemIndex
=
0
;
}
// Set the newest item.
BufferItem
&
item
=
buffer
[
this
->
newestItemIndex
];
item
.
count
=
size
;
item
.
time
=
nowMs
;
}
else
{
// Update the newest item.
BufferItem
&
item
=
buffer
[
this
->
newestItemIndex
];
item
.
count
+=
size
;
}
}
// Update the newest item.
BufferItem
&
item
=
buffer
[
this
->
newestTimeIndex
];
item
.
count
+=
size
;
item
.
time
=
nowMs
;
this
->
newestTime
=
nowMs
;
// Set the oldest item index and time, if not set.
// Set the oldest item index and time, if not set.
if
(
this
->
oldestIndex
<
0
)
if
(
this
->
oldestI
temI
ndex
<
0
)
{
{
this
->
oldestI
ndex
=
this
->
newestTime
Index
;
this
->
oldestI
temIndex
=
this
->
newestItem
Index
;
this
->
oldest
TimeIndex
=
nowMs
;
this
->
oldest
ItemStartTime
=
nowMs
;
}
}
this
->
totalCount
+=
size
;
this
->
totalCount
+=
size
;
...
@@ -71,7 +82,7 @@ namespace RTC
...
@@ -71,7 +82,7 @@ namespace RTC
RemoveOldData
(
nowMs
);
RemoveOldData
(
nowMs
);
float
scale
=
this
->
scale
/
this
->
windowSize
;
float
scale
=
this
->
scale
/
this
->
windowSize
Ms
;
this
->
lastTime
=
nowMs
;
this
->
lastTime
=
nowMs
;
this
->
lastRate
=
static_cast
<
uint32_t
>
(
std
::
trunc
(
this
->
totalCount
*
scale
+
0.5
f
));
this
->
lastRate
=
static_cast
<
uint32_t
>
(
std
::
trunc
(
this
->
totalCount
*
scale
+
0.5
f
));
...
@@ -83,36 +94,36 @@ namespace RTC
...
@@ -83,36 +94,36 @@ namespace RTC
{
{
MS_TRACE
();
MS_TRACE
();
// No item set
// No item set
.
if
(
this
->
newest
TimeIndex
<
0
||
this
->
oldest
Index
<
0
)
if
(
this
->
newest
ItemIndex
<
0
||
this
->
oldestItem
Index
<
0
)
return
;
return
;
uint64_t
newoldestTime
Index
=
nowMs
-
this
->
windowSize
;
uint64_t
newoldestTime
=
nowMs
-
this
->
windowSizeMs
;
// Oldest item already removed.
// Oldest item already removed.
if
(
newoldestTime
Index
<=
this
->
oldestTimeIndex
)
if
(
newoldestTime
<=
this
->
oldestItemStartTime
)
return
;
return
;
// A whole window size time has elapsed since last entry. Reset the buffer.
// A whole window size time has elapsed since last entry. Reset the buffer.
if
(
newoldestTime
Index
>
this
->
newes
tTime
)
if
(
newoldestTime
>
this
->
newestItemStar
tTime
)
{
{
Reset
(
nowMs
);
Reset
(
nowMs
);
return
;
return
;
}
}
while
(
this
->
oldest
TimeIndex
<
newoldestTimeIndex
)
while
(
this
->
oldest
ItemStartTime
<
newoldestTime
)
{
{
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestIndex
];
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestI
temI
ndex
];
this
->
totalCount
-=
oldestItem
.
count
;
this
->
totalCount
-=
oldestItem
.
count
;
oldestItem
.
count
=
0u
;
oldestItem
.
count
=
0u
;
oldestItem
.
time
=
0u
;
oldestItem
.
time
=
0u
;
if
(
++
this
->
oldestIndex
>=
this
->
windowItems
)
if
(
++
this
->
oldestI
temI
ndex
>=
this
->
windowItems
)
this
->
oldestIndex
=
0
;
this
->
oldestI
temI
ndex
=
0
;
const
BufferItem
&
newOldestItem
=
buffer
[
this
->
oldestIndex
];
const
BufferItem
&
newOldestItem
=
buffer
[
this
->
oldestI
temI
ndex
];
this
->
oldest
TimeIndex
=
newOldestItem
.
time
;
this
->
oldest
ItemStartTime
=
newOldestItem
.
time
;
}
}
}
}
...
...
worker/test/src/RTC/TestRateCalculator.cpp
浏览文件 @
3cde2cb8
...
@@ -61,7 +61,7 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
...
@@ -61,7 +61,7 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
SECTION
(
"slide"
)
SECTION
(
"slide"
)
{
{
RateCalculator
rate
;
RateCalculator
rate
(
1000
,
8000
,
1000
)
;
// clang-format off
// clang-format off
std
::
vector
<
data
>
input
=
std
::
vector
<
data
>
input
=
...
@@ -79,23 +79,46 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
...
@@ -79,23 +79,46 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
REQUIRE
(
rate
.
GetRate
(
nowMs
+
3001
)
==
0
);
REQUIRE
(
rate
.
GetRate
(
nowMs
+
3001
)
==
0
);
}
}
SECTION
(
"slide with 100 items"
)
{
RateCalculator
rate
(
1000
,
8000
,
100
);
// clang-format off
std
::
vector
<
data
>
input
=
{
{
0
,
5
,
40
},
{
999
,
2
,
56
},
{
1001
,
1
,
24
},
// merged inside 999
{
1001
,
1
,
32
},
// merged inside 999
{
2000
,
1
,
8
}
// it will erase the item with timestamp=999,
// removing also the next two samples.
// The end estimation will include only the last sample.
};
// clang-format on
validate
(
rate
,
nowMs
,
input
);
REQUIRE
(
rate
.
GetRate
(
nowMs
+
3001
)
==
0
);
}
SECTION
(
"wrap"
)
SECTION
(
"wrap"
)
{
{
// window: 1000ms, items: 5 (granularity: 200ms)
RateCalculator
rate
(
1000
,
8000
,
5
);
RateCalculator
rate
(
1000
,
8000
,
5
);
// clang-format off
// clang-format off
std
::
vector
<
data
>
input
=
std
::
vector
<
data
>
input
=
{
{
{
1000
,
1
,
1
*
8
},
{
1000
,
1
,
1
*
8
},
{
1
001
,
1
,
1
*
8
+
1
*
8
},
{
1
200
,
1
,
1
*
8
+
1
*
8
},
{
1
002
,
1
,
1
*
8
+
2
*
8
},
{
1
400
,
1
,
1
*
8
+
2
*
8
},
{
1
003
,
1
,
1
*
8
+
3
*
8
},
{
1
600
,
1
,
1
*
8
+
3
*
8
},
{
1
004
,
1
,
1
*
8
+
4
*
8
},
{
1
800
,
1
,
1
*
8
+
4
*
8
},
{
1005
,
1
,
1
*
8
+
(
5
-
1
)
*
8
},
// starts wrap here
{
2000
,
1
,
1
*
8
+
(
5
-
1
)
*
8
},
// starts wrap here
{
1006
,
1
,
1
*
8
+
(
6
-
2
)
*
8
},
{
2200
,
1
,
1
*
8
+
(
6
-
2
)
*
8
},
{
1007
,
1
,
1
*
8
+
(
7
-
3
)
*
8
},
{
2400
,
1
,
1
*
8
+
(
7
-
3
)
*
8
},
{
1008
,
1
,
1
*
8
+
(
8
-
4
)
*
8
},
{
2600
,
1
,
1
*
8
+
(
8
-
4
)
*
8
},
{
1009
,
1
,
1
*
8
+
(
9
-
5
)
*
8
},
{
2800
,
1
,
1
*
8
+
(
9
-
5
)
*
8
},
};
};
// clang-format on
// clang-format on
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论