Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
M
mediasoup
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
聂卓夫
mediasoup
Commits
56068f83
Unverified
提交
56068f83
authored
4月 20, 2021
作者:
Vittorio Palmisano
提交者:
GitHub
4月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rate calculator optimization (#538)
上级
75a0d637
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
91 行增加
和
37 行删除
+91
-37
RateCalculator.hpp
worker/include/RTC/RateCalculator.hpp
+20
-7
RateCalculator.cpp
worker/src/RTC/RateCalculator.cpp
+47
-29
TestRateCalculator.cpp
worker/test/src/RTC/TestRateCalculator.cpp
+24
-1
没有找到文件。
worker/include/RTC/RateCalculator.hpp
浏览文件 @
56068f83
...
@@ -14,10 +14,14 @@ namespace RTC
...
@@ -14,10 +14,14 @@ 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
{
1000u
};
public
:
public
:
RateCalculator
(
size_t
windowSize
=
DefaultWindowSize
,
float
scale
=
DefaultBpsScale
)
RateCalculator
(
:
windowSize
(
windowSize
),
scale
(
scale
)
size_t
windowSize
=
DefaultWindowSize
,
float
scale
=
DefaultBpsScale
,
uint16_t
windowItems
=
DefaultWindowItems
)
:
windowSize
(
windowSize
),
scale
(
scale
),
windowItems
(
windowItems
)
{
{
Reset
();
Reset
();
}
}
...
@@ -38,9 +42,11 @@ namespace RTC
...
@@ -38,9 +42,11 @@ namespace RTC
void
RemoveOldData
(
uint64_t
nowMs
);
void
RemoveOldData
(
uint64_t
nowMs
);
void
Reset
(
uint64_t
nowMs
)
void
Reset
(
uint64_t
nowMs
)
{
{
this
->
buffer
.
reset
(
new
BufferItem
[
this
->
windowSize
]);
this
->
buffer
.
reset
(
new
BufferItem
[
this
->
windowItems
]);
this
->
oldestTime
=
nowMs
-
this
->
windowSize
;
this
->
newestTime
=
0u
;
this
->
oldestIndex
=
0u
;
this
->
newestTimeIndex
=
-
1
;
this
->
oldestTimeIndex
=
0u
;
this
->
oldestIndex
=
-
1
;
this
->
totalCount
=
0u
;
this
->
totalCount
=
0u
;
this
->
lastRate
=
0u
;
this
->
lastRate
=
0u
;
this
->
lastTime
=
0u
;
this
->
lastTime
=
0u
;
...
@@ -50,6 +56,7 @@ namespace RTC
...
@@ -50,6 +56,7 @@ namespace RTC
struct
BufferItem
struct
BufferItem
{
{
size_t
count
{
0u
};
size_t
count
{
0u
};
uint64_t
time
{
0u
};
};
};
private
:
private
:
...
@@ -57,12 +64,18 @@ namespace RTC
...
@@ -57,12 +64,18 @@ namespace RTC
size_t
windowSize
{
DefaultWindowSize
};
size_t
windowSize
{
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).
uint16_t
windowItems
{
DefaultWindowItems
};
// 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.
uint64_t
newestTime
{
0u
};
// Index for the last item in the time window.
int32_t
newestTimeIndex
{
-
1
};
// Time (in milliseconds) for oldest item in the time window.
// Time (in milliseconds) for oldest item in the time window.
uint64_t
oldestTime
{
0u
};
uint64_t
oldestTime
Index
{
0u
};
// Index for the oldest item in the time window.
// Index for the oldest item in the time window.
uint32_t
oldestIndex
{
0u
};
int32_t
oldestIndex
{
-
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.
...
...
worker/src/RTC/RateCalculator.cpp
浏览文件 @
56068f83
...
@@ -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
->
oldestTime
)
if
(
nowMs
<
this
->
oldestTime
Index
)
return
;
return
;
// Increase bytes.
// Increase bytes.
...
@@ -20,14 +20,40 @@ namespace RTC
...
@@ -20,14 +20,40 @@ namespace RTC
RemoveOldData
(
nowMs
);
RemoveOldData
(
nowMs
);
//
Set data in the index before the oldest
index.
//
Increase the
index.
uint32_t
offset
=
this
->
windowSize
-
1
;
if
(
++
this
->
newestTimeIndex
>=
this
->
windowItems
)
uint32_t
index
=
this
->
oldestIndex
+
offset
;
this
->
newestTimeIndex
=
0
;
if
(
index
>=
this
->
windowSize
)
// Newest index overlaps with the oldest one, remove it.
index
-=
this
->
windowSize
;
if
(
this
->
newestTimeIndex
==
this
->
oldestIndex
&&
this
->
oldestIndex
!=
-
1
)
{
MS_WARN_TAG
(
info
,
"calculation buffer full, windowSize:%"
PRIu64
" ms windowItems:%"
PRIu16
,
this
->
windowSize
,
this
->
windowItems
);
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestIndex
];
this
->
totalCount
-=
oldestItem
.
count
;
oldestItem
.
count
=
0u
;
oldestItem
.
time
=
0u
;
if
(
++
this
->
oldestIndex
>=
this
->
windowItems
)
this
->
oldestIndex
=
0
;
}
// 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.
if
(
this
->
oldestIndex
<
0
)
{
this
->
oldestIndex
=
this
->
newestTimeIndex
;
this
->
oldestTimeIndex
=
nowMs
;
}
this
->
buffer
[
index
].
count
+=
size
;
this
->
totalCount
+=
size
;
this
->
totalCount
+=
size
;
// Reset lastRate and lastTime so GetRate() will calculate rate again even
// Reset lastRate and lastTime so GetRate() will calculate rate again even
...
@@ -57,45 +83,37 @@ namespace RTC
...
@@ -57,45 +83,37 @@ namespace RTC
{
{
MS_TRACE
();
MS_TRACE
();
uint64_t
newOldestTime
=
nowMs
-
this
->
windowSize
;
// No item set
if
(
this
->
newestTimeIndex
<
0
||
this
->
oldestIndex
<
0
)
// Should never happen.
if
(
newOldestTime
<
this
->
oldestTime
)
{
MS_ERROR
(
"current time [%"
PRIu64
"] is older than a previous [%"
PRIu64
"]"
,
newOldestTime
,
this
->
oldestTime
);
return
;
return
;
}
// We are in the same time unit (ms) as the last entry.
uint64_t
newoldestTimeIndex
=
nowMs
-
this
->
windowSize
;
if
(
newOldestTime
==
this
->
oldestTime
)
// Oldest item already removed.
if
(
newoldestTimeIndex
<=
this
->
oldestTimeIndex
)
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
(
new
OldestTime
>
this
->
oldestTime
+
this
->
windowSiz
e
)
if
(
new
oldestTimeIndex
>
this
->
newestTim
e
)
{
{
Reset
(
nowMs
);
Reset
(
nowMs
);
return
;
return
;
}
}
while
(
this
->
oldestTime
<
newOldestTime
)
while
(
this
->
oldestTime
Index
<
newoldestTimeIndex
)
{
{
const
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestIndex
];
BufferItem
&
oldestItem
=
buffer
[
this
->
oldestIndex
];
this
->
totalCount
-=
oldestItem
.
count
;
this
->
totalCount
-=
oldestItem
.
count
;
this
->
buffer
[
this
->
oldestIndex
]
=
BufferItem
();
oldestItem
.
count
=
0u
;
oldestItem
.
time
=
0u
;
if
(
++
this
->
oldestIndex
>=
this
->
window
Size
)
if
(
++
this
->
oldestIndex
>=
this
->
window
Items
)
this
->
oldestIndex
=
0
;
this
->
oldestIndex
=
0
;
++
this
->
oldestTime
;
const
BufferItem
&
newOldestItem
=
buffer
[
this
->
oldestIndex
];
this
->
oldestTimeIndex
=
newOldestItem
.
time
;
}
}
this
->
oldestTime
=
newOldestTime
;
}
}
void
RtpDataCounter
::
Update
(
RTC
::
RtpPacket
*
packet
)
void
RtpDataCounter
::
Update
(
RTC
::
RtpPacket
*
packet
)
...
...
worker/test/src/RTC/TestRateCalculator.cpp
浏览文件 @
56068f83
...
@@ -76,6 +76,29 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
...
@@ -76,6 +76,29 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
validate
(
rate
,
nowMs
,
input
);
validate
(
rate
,
nowMs
,
input
);
REQUIRE
(
rate
.
GetRate
(
nowMs
+
3000
)
==
0
);
REQUIRE
(
rate
.
GetRate
(
nowMs
+
3001
)
==
0
);
}
SECTION
(
"wrap"
)
{
RateCalculator
rate
(
1000
,
8000
,
5
);
// clang-format off
std
::
vector
<
data
>
input
=
{
{
1000
,
1
,
1
*
8
},
{
1001
,
1
,
1
*
8
+
1
*
8
},
{
1002
,
1
,
1
*
8
+
2
*
8
},
{
1003
,
1
,
1
*
8
+
3
*
8
},
{
1004
,
1
,
1
*
8
+
4
*
8
},
{
1005
,
1
,
1
*
8
+
(
5
-
1
)
*
8
},
// starts wrap here
{
1006
,
1
,
1
*
8
+
(
6
-
2
)
*
8
},
{
1007
,
1
,
1
*
8
+
(
7
-
3
)
*
8
},
{
1008
,
1
,
1
*
8
+
(
8
-
4
)
*
8
},
{
1009
,
1
,
1
*
8
+
(
9
-
5
)
*
8
},
};
// clang-format on
validate
(
rate
,
nowMs
,
input
);
}
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论