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
c25ff640
Commit
c25ff640
authored
Dec 24, 2021
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HLS: hls播放解决播放下一个切片导致数据丢失的bug: #1294
parent
0bf75529
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
114 行增加
和
99 行删除
+114
-99
src/Http/HlsPlayer.cpp
+70
-68
src/Http/HlsPlayer.h
+1
-2
src/Http/HttpTSPlayer.cpp
+17
-16
src/Http/HttpTSPlayer.h
+26
-13
没有找到文件。
src/Http/HlsPlayer.cpp
查看文件 @
c25ff640
...
...
@@ -9,9 +9,10 @@
*/
#include "HlsPlayer.h"
namespace
mediakit
{
HlsPlayer
::
HlsPlayer
(
const
EventPoller
::
Ptr
&
poller
){
HlsPlayer
::
HlsPlayer
(
const
EventPoller
::
Ptr
&
poller
)
{
_segment
.
setOnSegment
([
this
](
const
char
*
data
,
size_t
len
)
{
onPacket
(
data
,
len
);
});
setPoller
(
poller
?
poller
:
EventPollerPool
::
Instance
().
getPoller
());
}
...
...
@@ -23,24 +24,23 @@ void HlsPlayer::play(const string &strUrl) {
play_l
();
}
void
HlsPlayer
::
play_l
(){
void
HlsPlayer
::
play_l
()
{
if
(
_m3u8_list
.
empty
())
{
teardown_l
(
SockException
(
Err_shutdown
,
"所有hls url都尝试播放失败!"
));
return
;
}
if
(
alive
()
&&
_waiting_response
)
{
if
(
waitResponse
()
)
{
return
;
}
float
playTimeOutSec
=
(
*
this
)[
Client
::
kTimeoutMS
].
as
<
int
>
()
/
1000.0
f
;
setMethod
(
"GET"
);
if
(
!
(
*
this
)[
kNetAdapter
].
empty
())
{
if
(
!
(
*
this
)[
kNetAdapter
].
empty
())
{
setNetAdapter
((
*
this
)[
kNetAdapter
]);
}
_waiting_response
=
true
;
sendRequest
(
_m3u8_list
.
back
(),
playTimeOutSec
);
}
void
HlsPlayer
::
teardown_l
(
const
SockException
&
ex
){
void
HlsPlayer
::
teardown_l
(
const
SockException
&
ex
)
{
_timer
.
reset
();
_timer_ts
.
reset
();
_http_ts_player
.
reset
();
...
...
@@ -48,75 +48,79 @@ void HlsPlayer::teardown_l(const SockException &ex){
}
void
HlsPlayer
::
teardown
()
{
teardown_l
(
SockException
(
Err_shutdown
,
"teardown"
));
teardown_l
(
SockException
(
Err_shutdown
,
"teardown"
));
}
void
HlsPlayer
::
playNextTs
(
bool
force
)
{
void
HlsPlayer
::
playNextTs
(
)
{
if
(
_ts_list
.
empty
())
{
//播放列表为空,那么立即重新下载m3u8文件
_timer
.
reset
();
play_l
();
return
;
}
if
(
!
force
&&
_http_ts_player
&&
_http_ts_player
->
aliv
e
())
{
if
(
_http_ts_player
&&
_http_ts_player
->
waitRespons
e
())
{
//播放器目前还存活,正在下载中
return
;
}
auto
ts_duration
=
_ts_list
.
front
().
duration
*
1000
;
weak_ptr
<
HlsPlayer
>
weakSelf
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
std
::
shared_ptr
<
Ticker
>
ticker
(
new
Ticker
);
weak_ptr
<
HlsPlayer
>
weak_self
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
if
(
!
_http_ts_player
)
{
_http_ts_player
=
std
::
make_shared
<
HttpTSPlayer
>
(
getPoller
(),
false
);
_http_ts_player
->
setOnCreateSocket
([
weakSelf
](
const
EventPoller
::
Ptr
&
poller
)
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
strongSelf
)
{
return
strongSelf
->
createSocket
();
_http_ts_player
->
setOnCreateSocket
([
weak_self
](
const
EventPoller
::
Ptr
&
poller
)
{
auto
strong_self
=
weak_self
.
lock
();
if
(
strong_self
)
{
return
strong_self
->
createSocket
();
}
return
Socket
::
createSocket
(
poller
,
true
);
});
_http_ts_player
->
setOnDisconnect
([
weakSelf
,
ticker
,
ts_duration
](
const
SockException
&
err
)
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
;
}
auto
delay
=
ts_duration
-
500
-
ticker
->
elapsedTime
();
if
(
delay
<=
0
)
{
//播放这个ts切片花费了太长时间,我们立即下一个切片的播放
strongSelf
->
playNextTs
(
true
);
}
else
{
//下一个切片慢点播放
strongSelf
->
_timer_ts
.
reset
(
new
Timer
(
delay
/
1000.0
f
,
[
weakSelf
]()
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
false
;
}
strongSelf
->
playNextTs
(
true
);
return
false
;
},
strongSelf
->
getPoller
()));
}
});
_http_ts_player
->
setOnPacket
([
weakSelf
](
const
char
*
data
,
size_t
len
)
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
_http_ts_player
->
setOnPacket
([
weak_self
](
const
char
*
data
,
size_t
len
)
{
auto
strong_self
=
weak_self
.
lock
();
if
(
!
strong_self
)
{
return
;
}
//收到ts包
strongS
elf
->
onPacket_l
(
data
,
len
);
strong_s
elf
->
onPacket_l
(
data
,
len
);
});
_http_ts_player
->
setMethod
(
"GET"
);
if
(
!
(
*
this
)[
kNetAdapter
].
empty
())
{
_http_ts_player
->
setNetAdapter
((
*
this
)[
Client
::
kNetAdapter
]);
}
}
_http_ts_player
->
sendRequest
(
_ts_list
.
front
().
url
,
2
*
_ts_list
.
front
().
duration
);
Ticker
ticker
;
auto
url
=
_ts_list
.
front
().
url
;
auto
duration
=
_ts_list
.
front
().
duration
;
_ts_list
.
pop_front
();
_http_ts_player
->
setOnComplete
([
weak_self
,
ticker
,
duration
,
url
](
const
SockException
&
err
)
{
auto
strong_self
=
weak_self
.
lock
();
if
(
!
strong_self
)
{
return
;
}
if
(
err
)
{
WarnL
<<
"download ts segment "
<<
url
<<
" failed:"
<<
err
.
what
();
}
//提前半秒下载好
auto
delay
=
duration
-
ticker
.
elapsedTime
()
/
1000.0
f
-
0.5
;
if
(
delay
<=
0
)
{
//延时最小10ms
delay
=
10
;
}
//延时下载下一个切片
strong_self
->
_timer_ts
.
reset
(
new
Timer
(
delay
/
1000.0
f
,
[
weak_self
]()
{
auto
strong_self
=
weak_self
.
lock
();
if
(
strong_self
)
{
strong_self
->
playNextTs
();
}
return
false
;
},
strong_self
->
getPoller
()));
});
_http_ts_player
->
setMethod
(
"GET"
);
_http_ts_player
->
sendRequest
(
url
,
2
*
duration
);
}
void
HlsPlayer
::
onParsed
(
bool
is_m3u8_inner
,
int64_t
sequence
,
const
map
<
int
,
ts_segment
>
&
ts_map
)
{
void
HlsPlayer
::
onParsed
(
bool
is_m3u8_inner
,
int64_t
sequence
,
const
map
<
int
,
ts_segment
>
&
ts_map
)
{
if
(
!
is_m3u8_inner
)
{
//这是ts播放列表
if
(
_last_sequence
==
sequence
)
{
...
...
@@ -146,12 +150,12 @@ void HlsPlayer::onParsed(bool is_m3u8_inner,int64_t sequence,const map<int,ts_se
}
_timer
.
reset
();
weak_ptr
<
HlsPlayer
>
weak
S
elf
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
weak_ptr
<
HlsPlayer
>
weak
_s
elf
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
auto
url
=
ts_map
.
rbegin
()
->
second
.
url
;
getPoller
()
->
async
([
weak
S
elf
,
url
]()
{
auto
strong
Self
=
weakS
elf
.
lock
();
if
(
strong
S
elf
)
{
strong
S
elf
->
play
(
url
);
getPoller
()
->
async
([
weak
_s
elf
,
url
]()
{
auto
strong
_self
=
weak_s
elf
.
lock
();
if
(
strong
_s
elf
)
{
strong
_s
elf
->
play
(
url
);
}
},
false
);
}
...
...
@@ -163,8 +167,8 @@ ssize_t HlsPlayer::onResponseHeader(const string &status, const HttpClient::Http
teardown_l
(
SockException
(
Err_shutdown
,
StrPrinter
<<
"bad http status code:"
+
status
));
return
0
;
}
auto
contet_type
=
const_cast
<
HttpClient
::
HttpHeader
&>
(
headers
)[
"Content-Type"
];
_is_m3u8
=
(
contet_type
.
find
(
"application/vnd.apple.mpegurl"
)
==
0
);
auto
conte
n
t_type
=
const_cast
<
HttpClient
::
HttpHeader
&>
(
headers
)[
"Content-Type"
];
_is_m3u8
=
(
conte
n
t_type
.
find
(
"application/vnd.apple.mpegurl"
)
==
0
);
return
-
1
;
}
...
...
@@ -177,13 +181,12 @@ void HlsPlayer::onResponseBody(const char *buf, size_t size, size_t recvedSize,
}
void
HlsPlayer
::
onResponseCompleted
()
{
_waiting_response
=
false
;
if
(
HlsParser
::
parse
(
getUrl
(),
_m3u8
))
{
playDelay
();
if
(
_first
)
{
_first
=
false
;
onPlayResult
(
SockException
(
Err_success
,
"play success"
));
}
playDelay
();
}
else
{
teardown_l
(
SockException
(
Err_shutdown
,
"解析m3u8文件失败"
));
}
...
...
@@ -213,7 +216,6 @@ float HlsPlayer::delaySecond() {
}
void
HlsPlayer
::
onDisconnect
(
const
SockException
&
ex
)
{
_waiting_response
=
false
;
if
(
_first
)
{
//第一次失败,则播放失败
_first
=
false
;
...
...
@@ -240,24 +242,24 @@ void HlsPlayer::onDisconnect(const SockException &ex) {
playDelay
();
}
bool
HlsPlayer
::
onRedirectUrl
(
const
string
&
url
,
bool
temporary
)
{
bool
HlsPlayer
::
onRedirectUrl
(
const
string
&
url
,
bool
temporary
)
{
_m3u8_list
.
emplace_back
(
url
);
return
true
;
}
void
HlsPlayer
::
playDelay
(){
weak_ptr
<
HlsPlayer
>
weak
S
elf
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
_timer
.
reset
(
new
Timer
(
delaySecond
(),
[
weak
S
elf
]()
{
auto
strong
Self
=
weakS
elf
.
lock
();
if
(
strong
S
elf
)
{
strong
S
elf
->
play_l
();
void
HlsPlayer
::
playDelay
()
{
weak_ptr
<
HlsPlayer
>
weak
_s
elf
=
dynamic_pointer_cast
<
HlsPlayer
>
(
shared_from_this
());
_timer
.
reset
(
new
Timer
(
delaySecond
(),
[
weak
_s
elf
]()
{
auto
strong
_self
=
weak_s
elf
.
lock
();
if
(
strong
_s
elf
)
{
strong
_s
elf
->
play_l
();
}
return
false
;
},
getPoller
()));
}
void
HlsPlayer
::
onPacket_l
(
const
char
*
data
,
size_t
len
){
_segment
.
input
(
data
,
len
);
void
HlsPlayer
::
onPacket_l
(
const
char
*
data
,
size_t
len
)
{
_segment
.
input
(
data
,
len
);
}
//////////////////////////////////////////////////////////////////////////
...
...
@@ -280,7 +282,7 @@ public:
}
void
resetTracks
()
override
{
((
MediaSink
&
)
_delegate
).
resetTracks
();
((
MediaSink
&
)
_delegate
).
resetTracks
();
}
vector
<
Track
::
Ptr
>
getTracks
(
bool
ready
=
true
)
const
override
{
...
...
@@ -388,7 +390,7 @@ void HlsDemuxer::onTick() {
HlsPlayerImp
::
HlsPlayerImp
(
const
EventPoller
::
Ptr
&
poller
)
:
PlayerImp
<
HlsPlayer
,
PlayerBase
>
(
poller
)
{}
void
HlsPlayerImp
::
onPacket
(
const
char
*
data
,
size_t
len
)
{
void
HlsPlayerImp
::
onPacket
(
const
char
*
data
,
size_t
len
)
{
if
(
!
_decoder
)
{
_decoder
=
DecoderImp
::
createDecoder
(
DecoderImp
::
decoder_ts
,
_demuxer
.
get
());
}
...
...
src/Http/HlsPlayer.h
查看文件 @
c25ff640
...
...
@@ -96,7 +96,7 @@ private:
private
:
void
playDelay
();
float
delaySecond
();
void
playNextTs
(
bool
force
=
false
);
void
playNextTs
();
void
teardown_l
(
const
SockException
&
ex
);
void
play_l
();
void
onPacket_l
(
const
char
*
data
,
size_t
len
);
...
...
@@ -112,7 +112,6 @@ private:
private
:
bool
_is_m3u8
=
false
;
bool
_first
=
true
;
bool
_waiting_response
=
false
;
int64_t
_last_sequence
=
-
1
;
string
_m3u8
;
Timer
::
Ptr
_timer
;
...
...
src/Http/HttpTSPlayer.cpp
查看文件 @
c25ff640
...
...
@@ -9,24 +9,23 @@
*/
#include "HttpTSPlayer.h"
namespace
mediakit
{
HttpTSPlayer
::
HttpTSPlayer
(
const
EventPoller
::
Ptr
&
poller
,
bool
split_ts
){
HttpTSPlayer
::
HttpTSPlayer
(
const
EventPoller
::
Ptr
&
poller
,
bool
split_ts
)
{
_split_ts
=
split_ts
;
_segment
.
setOnSegment
([
this
](
const
char
*
data
,
size_t
len
)
{
onPacket
(
data
,
len
);
});
setPoller
(
poller
?
poller
:
EventPollerPool
::
Instance
().
getPoller
());
}
HttpTSPlayer
::~
HttpTSPlayer
()
{}
ssize_t
HttpTSPlayer
::
onResponseHeader
(
const
string
&
status
,
const
HttpClient
::
HttpHeader
&
headers
)
{
ssize_t
HttpTSPlayer
::
onResponseHeader
(
const
string
&
status
,
const
HttpClient
::
HttpHeader
&
header
)
{
_status
=
status
;
if
(
status
!=
"200"
&&
status
!=
"206"
)
{
//http状态码不符合预期
shutdown
(
SockException
(
Err_other
,
StrPrinter
<<
"bad http status code:"
+
status
));
return
0
;
}
auto
content_type
=
const_cast
<
HttpClient
::
HttpHeader
&>
(
header
s
)[
"Content-Type"
];
auto
content_type
=
const_cast
<
HttpClient
::
HttpHeader
&>
(
header
)[
"Content-Type"
];
if
(
content_type
.
find
(
"video/mp2t"
)
==
0
||
content_type
.
find
(
"video/mpeg"
)
==
0
)
{
_is_ts_content
=
true
;
}
...
...
@@ -35,11 +34,11 @@ ssize_t HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::H
return
-
1
;
}
void
HttpTSPlayer
::
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recved
Size
,
size_t
totalS
ize
)
{
void
HttpTSPlayer
::
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recved
_size
,
size_t
total_s
ize
)
{
if
(
_status
!=
"200"
&&
_status
!=
"206"
)
{
return
;
}
if
(
recved
S
ize
==
size
)
{
if
(
recved
_s
ize
==
size
)
{
//开始接收数据
if
(
buf
[
0
]
==
TS_SYNC_BYTE
)
{
//这是ts头
...
...
@@ -57,14 +56,16 @@ void HttpTSPlayer::onResponseBody(const char *buf, size_t size, size_t recvedSiz
}
void
HttpTSPlayer
::
onResponseCompleted
()
{
//接收完毕
shutdown
(
SockException
(
Err_success
,
"play completed"
));
emitOnComplete
(
SockException
(
Err_success
,
"play completed"
));
}
void
HttpTSPlayer
::
onDisconnect
(
const
SockException
&
ex
)
{
if
(
_on_disconnect
)
{
_on_disconnect
(
ex
);
_on_disconnect
=
nullptr
;
emitOnComplete
(
ex
);
}
void
HttpTSPlayer
::
emitOnComplete
(
const
SockException
&
ex
)
{
if
(
_on_complete
)
{
_on_complete
(
ex
);
}
}
...
...
@@ -74,12 +75,12 @@ void HttpTSPlayer::onPacket(const char *data, size_t len) {
}
}
void
HttpTSPlayer
::
setOn
Disconnect
(
const
HttpTSPlayer
::
onShutdown
&
cb
)
{
_on_
disconnect
=
cb
;
void
HttpTSPlayer
::
setOn
Complete
(
onComplete
cb
)
{
_on_
complete
=
std
::
move
(
cb
)
;
}
void
HttpTSPlayer
::
setOnPacket
(
const
TSSegment
::
onSegment
&
cb
)
{
_on_segment
=
cb
;
void
HttpTSPlayer
::
setOnPacket
(
TSSegment
::
onSegment
cb
)
{
_on_segment
=
std
::
move
(
cb
)
;
}
}
//namespace mediakit
src/Http/HttpTSPlayer.h
查看文件 @
c25ff640
...
...
@@ -14,34 +14,47 @@
#include "Http/HttpDownloader.h"
#include "Player/MediaPlayer.h"
#include "Rtp/TSDecoder.h"
using
namespace
toolkit
;
namespace
mediakit
{
//http-ts播发器,未实现ts解复用
class
HttpTSPlayer
:
public
HttpClientImp
{
class
HttpTSPlayer
:
public
HttpClientImp
{
public
:
typedef
function
<
void
(
const
SockException
&
)
>
onShutdown
;
typedef
std
::
shared_ptr
<
HttpTSPlayer
>
Ptr
;
using
Ptr
=
std
::
shared_ptr
<
HttpTSPlayer
>
;
using
onComplete
=
std
::
function
<
void
(
const
SockException
&
)
>
;
HttpTSPlayer
(
const
EventPoller
::
Ptr
&
poller
=
nullptr
,
bool
split_ts
=
true
);
~
HttpTSPlayer
()
override
;
~
HttpTSPlayer
()
override
=
default
;
//设置异常断开回调
void
setOnDisconnect
(
const
onShutdown
&
cb
);
//设置接收ts包回调
void
setOnPacket
(
const
TSSegment
::
onSegment
&
cb
);
/**
* 设置下载完毕或异常断开回调
*/
void
setOnComplete
(
onComplete
cb
);
/**
* 设置接收ts包回调
*/
void
setOnPacket
(
TSSegment
::
onSegment
cb
);
protected
:
///HttpClient override///
ssize_t
onResponseHeader
(
const
string
&
status
,
const
HttpHeader
&
headers
)
override
;
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recvedSize
,
size_t
totalS
ize
)
override
;
ssize_t
onResponseHeader
(
const
string
&
status
,
const
HttpHeader
&
header
)
override
;
void
onResponseBody
(
const
char
*
buf
,
size_t
size
,
size_t
recved_size
,
size_t
total_s
ize
)
override
;
void
onResponseCompleted
()
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
void
onDisconnect
(
const
SockException
&
ex
)
override
;
//收到ts包
protected
:
/**
* 收到ts数据
*/
virtual
void
onPacket
(
const
char
*
data
,
size_t
len
);
private
:
void
emitOnComplete
(
const
SockException
&
ex
);
private
:
//是否为mpegts负载
bool
_is_ts_content
=
false
;
//第一个包是否为ts包
...
...
@@ -50,7 +63,7 @@ private:
bool
_split_ts
;
string
_status
;
TSSegment
_segment
;
on
Shutdown
_on_disconnect
;
on
Complete
_on_complete
;
TSSegment
::
onSegment
_on_segment
;
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论