Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
75cc3481
Commit
75cc3481
authored
Apr 26, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
保证不丢帧
parent
b2fe3cd0
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
40 行增加
和
36 行删除
+40
-36
src/Common/MediaSink.cpp
+32
-29
src/Common/MediaSink.h
+3
-2
src/Extension/AAC.h
+4
-4
src/Extension/AACRtmp.cpp
+1
-1
没有找到文件。
src/Common/MediaSink.cpp
查看文件 @
75cc3481
...
@@ -25,36 +25,30 @@ void MediaSink::addTrack(const Track::Ptr &track_in) {
...
@@ -25,36 +25,30 @@ void MediaSink::addTrack(const Track::Ptr &track_in) {
auto
track
=
track_in
->
clone
();
auto
track
=
track_in
->
clone
();
auto
codec_id
=
track
->
getCodecId
();
auto
codec_id
=
track
->
getCodecId
();
_track_map
[
codec_id
]
=
track
;
_track_map
[
codec_id
]
=
track
;
_all
TrackR
eady
=
false
;
_all
_track_r
eady
=
false
;
_track
ReadyC
allback
[
codec_id
]
=
[
this
,
track
]()
{
_track
_ready_c
allback
[
codec_id
]
=
[
this
,
track
]()
{
onTrackReady
(
track
);
onTrackReady
(
track
);
};
};
_ticker
.
resetTime
();
_ticker
.
resetTime
();
track
->
addDelegate
(
std
::
make_shared
<
FrameWriterInterfaceHelper
>
([
this
](
const
Frame
::
Ptr
&
frame
)
{
track
->
addDelegate
(
std
::
make_shared
<
FrameWriterInterfaceHelper
>
([
this
](
const
Frame
::
Ptr
&
frame
)
{
if
(
_all
TrackR
eady
)
{
if
(
_all
_track_r
eady
)
{
onTrackFrame
(
frame
);
onTrackFrame
(
frame
);
return
;
}
else
{
}
//还有Track未就绪,先缓存之
_frame_unread
[
frame
->
getCodecId
()].
emplace_back
(
Frame
::
getCacheAbleFrame
(
frame
));
//还有track未准备好,如果是视频的话,如果直接丢帧可能导致丢失I帧
checkTrackIfReady
(
nullptr
);
if
(
_allTrackReady
)
{
//运行至这里说明Track状态由未就绪切换为已就绪状态,那么这帧就不应该丢弃
onTrackFrame
(
frame
);
}
else
if
(
frame
->
keyFrame
()){
WarnL
<<
"some track is unready,drop key frame of: "
<<
frame
->
getCodecName
();
}
}
}));
}));
}
}
void
MediaSink
::
resetTracks
()
{
void
MediaSink
::
resetTracks
()
{
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
_all
TrackR
eady
=
false
;
_all
_track_r
eady
=
false
;
_track_map
.
clear
();
_track_map
.
clear
();
_track
ReadyC
allback
.
clear
();
_track
_ready_c
allback
.
clear
();
_ticker
.
resetTime
();
_ticker
.
resetTime
();
_max_track_size
=
2
;
_max_track_size
=
2
;
_frame_unread
.
clear
();
}
}
void
MediaSink
::
inputFrame
(
const
Frame
::
Ptr
&
frame
)
{
void
MediaSink
::
inputFrame
(
const
Frame
::
Ptr
&
frame
)
{
...
@@ -63,22 +57,21 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
...
@@ -63,22 +57,21 @@ void MediaSink::inputFrame(const Frame::Ptr &frame) {
if
(
it
==
_track_map
.
end
())
{
if
(
it
==
_track_map
.
end
())
{
return
;
return
;
}
}
checkTrackIfReady
(
it
->
second
);
it
->
second
->
inputFrame
(
frame
);
it
->
second
->
inputFrame
(
frame
);
checkTrackIfReady
(
nullptr
);
}
}
void
MediaSink
::
checkTrackIfReady_l
(
const
Track
::
Ptr
&
track
){
void
MediaSink
::
checkTrackIfReady_l
(
const
Track
::
Ptr
&
track
){
//Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
//Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
auto
it_callback
=
_track
ReadyC
allback
.
find
(
track
->
getCodecId
());
auto
it_callback
=
_track
_ready_c
allback
.
find
(
track
->
getCodecId
());
if
(
it_callback
!=
_track
ReadyC
allback
.
end
()
&&
track
->
ready
())
{
if
(
it_callback
!=
_track
_ready_c
allback
.
end
()
&&
track
->
ready
())
{
it_callback
->
second
();
it_callback
->
second
();
_track
ReadyC
allback
.
erase
(
it_callback
);
_track
_ready_c
allback
.
erase
(
it_callback
);
}
}
}
}
void
MediaSink
::
checkTrackIfReady
(
const
Track
::
Ptr
&
track
){
void
MediaSink
::
checkTrackIfReady
(
const
Track
::
Ptr
&
track
){
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
if
(
!
_all_track_ready
&&
!
_track_ready_callback
.
empty
())
{
if
(
!
_allTrackReady
&&
!
_trackReadyCallback
.
empty
())
{
if
(
track
)
{
if
(
track
)
{
checkTrackIfReady_l
(
track
);
checkTrackIfReady_l
(
track
);
}
else
{
}
else
{
...
@@ -88,14 +81,14 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
...
@@ -88,14 +81,14 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
}
}
}
}
if
(
!
_all
TrackR
eady
){
if
(
!
_all
_track_r
eady
){
if
(
_ticker
.
elapsedTime
()
>
MAX_WAIT_MS_READY
){
if
(
_ticker
.
elapsedTime
()
>
MAX_WAIT_MS_READY
){
//如果超过规定时间,那么不再等待并忽略未准备好的Track
//如果超过规定时间,那么不再等待并忽略未准备好的Track
emitAllTrackReady
();
emitAllTrackReady
();
return
;
return
;
}
}
if
(
!
_track
ReadyC
allback
.
empty
()){
if
(
!
_track
_ready_c
allback
.
empty
()){
//在超时时间内,如果存在未准备好的Track,那么继续等待
//在超时时间内,如果存在未准备好的Track,那么继续等待
return
;
return
;
}
}
...
@@ -115,22 +108,20 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
...
@@ -115,22 +108,20 @@ void MediaSink::checkTrackIfReady(const Track::Ptr &track){
}
}
void
MediaSink
::
addTrackCompleted
(){
void
MediaSink
::
addTrackCompleted
(){
{
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
lock_guard
<
recursive_mutex
>
lck
(
_mtx
);
_max_track_size
=
_track_map
.
size
();
_max_track_size
=
_track_map
.
size
();
}
checkTrackIfReady
(
nullptr
);
checkTrackIfReady
(
nullptr
);
}
}
void
MediaSink
::
emitAllTrackReady
()
{
void
MediaSink
::
emitAllTrackReady
()
{
if
(
_all
TrackR
eady
)
{
if
(
_all
_track_r
eady
)
{
return
;
return
;
}
}
DebugL
<<
"all track ready use "
<<
_ticker
.
elapsedTime
()
<<
"ms"
;
DebugL
<<
"all track ready use "
<<
_ticker
.
elapsedTime
()
<<
"ms"
;
if
(
!
_track
ReadyC
allback
.
empty
())
{
if
(
!
_track
_ready_c
allback
.
empty
())
{
//这是超时强制忽略未准备好的Track
//这是超时强制忽略未准备好的Track
_track
ReadyC
allback
.
clear
();
_track
_ready_c
allback
.
clear
();
//移除未准备好的Track
//移除未准备好的Track
for
(
auto
it
=
_track_map
.
begin
();
it
!=
_track_map
.
end
();)
{
for
(
auto
it
=
_track_map
.
begin
();
it
!=
_track_map
.
end
();)
{
if
(
!
it
->
second
->
ready
())
{
if
(
!
it
->
second
->
ready
())
{
...
@@ -144,8 +135,20 @@ void MediaSink::emitAllTrackReady() {
...
@@ -144,8 +135,20 @@ void MediaSink::emitAllTrackReady() {
if
(
!
_track_map
.
empty
())
{
if
(
!
_track_map
.
empty
())
{
//最少有一个有效的Track
//最少有一个有效的Track
_all
TrackR
eady
=
true
;
_all
_track_r
eady
=
true
;
onAllTrackReady
();
onAllTrackReady
();
//全部Track就绪,我们一次性把之前的帧输出
for
(
auto
&
pr
:
_frame_unread
){
if
(
_track_map
.
find
(
pr
.
first
)
==
_track_map
.
end
())
{
//该Track已经被移除
continue
;
}
pr
.
second
.
for_each
([
&
](
const
Frame
::
Ptr
&
frame
)
{
onTrackFrame
(
frame
);
});
}
_frame_unread
.
clear
();
}
}
}
}
...
...
src/Common/MediaSink.h
查看文件 @
75cc3481
...
@@ -114,8 +114,9 @@ private:
...
@@ -114,8 +114,9 @@ private:
private
:
private
:
mutable
recursive_mutex
_mtx
;
mutable
recursive_mutex
_mtx
;
unordered_map
<
int
,
Track
::
Ptr
>
_track_map
;
unordered_map
<
int
,
Track
::
Ptr
>
_track_map
;
unordered_map
<
int
,
function
<
void
()
>
>
_trackReadyCallback
;
unordered_map
<
int
,
List
<
Frame
::
Ptr
>
>
_frame_unread
;
bool
_allTrackReady
=
false
;
unordered_map
<
int
,
function
<
void
()
>
>
_track_ready_callback
;
bool
_all_track_ready
=
false
;
Ticker
_ticker
;
Ticker
_ticker
;
int
_max_track_size
=
2
;
int
_max_track_size
=
2
;
};
};
...
...
src/Extension/AAC.h
查看文件 @
75cc3481
...
@@ -223,13 +223,13 @@ public:
...
@@ -223,13 +223,13 @@ public:
* @param frame 数据帧
* @param frame 数据帧
*/
*/
void
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
{
void
inputFrame
(
const
Frame
::
Ptr
&
frame
)
override
{
if
(
_cfg
.
empty
())
{
if
(
_cfg
.
empty
())
{
//未获取到aac_cfg信息
//未获取到aac_cfg信息
if
(
frame
->
prefixSize
()
>=
7
)
{
if
(
frame
->
prefixSize
()
>=
7
)
{
//7个字节的adts头
//7个字节的adts头
_cfg
=
makeAdtsConfig
(
reinterpret_cast
<
const
uint8_t
*>
(
frame
->
data
()));
_cfg
=
makeAdtsConfig
(
(
uint8_t
*
)
(
frame
->
data
()));
onReady
();
onReady
();
}
else
{
}
else
{
WarnL
<<
"无法获取adts头!"
;
WarnL
<<
"无法获取adts头!"
;
}
}
}
}
...
...
src/Extension/AACRtmp.cpp
查看文件 @
75cc3481
...
@@ -93,7 +93,7 @@ void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
...
@@ -93,7 +93,7 @@ void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
if
(
_aac_cfg
.
empty
())
{
if
(
_aac_cfg
.
empty
())
{
if
(
frame
->
prefixSize
()
>=
7
)
{
if
(
frame
->
prefixSize
()
>=
7
)
{
//包含adts头,从adts头获取aac配置信息
//包含adts头,从adts头获取aac配置信息
_aac_cfg
=
makeAdtsConfig
(
reinterpret_cast
<
const
uint8_t
*>
(
frame
->
data
()));
_aac_cfg
=
makeAdtsConfig
(
(
uint8_t
*
)
(
frame
->
data
()));
}
}
makeConfigPacket
();
makeConfigPacket
();
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论