HLS: 修复http客户端连接复用导致hls播放异常的bug: #1294

This commit is contained in:
ziyue 2021-12-22 11:33:40 +08:00
parent f27893ffa5
commit 6a044f0726
4 changed files with 32 additions and 13 deletions

View File

@ -28,11 +28,15 @@ void HlsPlayer::play_l(){
teardown_l(SockException(Err_shutdown, "所有hls url都尝试播放失败!")); teardown_l(SockException(Err_shutdown, "所有hls url都尝试播放失败!"));
return; return;
} }
if (alive() && _waiting_response) {
return;
}
float playTimeOutSec = (*this)[Client::kTimeoutMS].as<int>() / 1000.0f; float playTimeOutSec = (*this)[Client::kTimeoutMS].as<int>() / 1000.0f;
setMethod("GET"); setMethod("GET");
if(!(*this)[kNetAdapter].empty()) { if(!(*this)[kNetAdapter].empty()) {
setNetAdapter((*this)[kNetAdapter]); setNetAdapter((*this)[kNetAdapter]);
} }
_waiting_response = true;
sendRequest(_m3u8_list.back(), playTimeOutSec); sendRequest(_m3u8_list.back(), playTimeOutSec);
} }
@ -173,6 +177,7 @@ void HlsPlayer::onResponseBody(const char *buf, size_t size, size_t recvedSize,
} }
void HlsPlayer::onResponseCompleted() { void HlsPlayer::onResponseCompleted() {
_waiting_response = false;
if (HlsParser::parse(getUrl(), _m3u8)) { if (HlsParser::parse(getUrl(), _m3u8)) {
playDelay(); playDelay();
if (_first) { if (_first) {
@ -208,6 +213,7 @@ float HlsPlayer::delaySecond() {
} }
void HlsPlayer::onDisconnect(const SockException &ex) { void HlsPlayer::onDisconnect(const SockException &ex) {
_waiting_response = false;
if (_first) { if (_first) {
//第一次失败,则播放失败 //第一次失败,则播放失败
_first = false; _first = false;
@ -219,6 +225,8 @@ void HlsPlayer::onDisconnect(const SockException &ex) {
if (ex.getErrCode() == Err_shutdown) { if (ex.getErrCode() == Err_shutdown) {
if (_m3u8_list.size() <= 1) { if (_m3u8_list.size() <= 1) {
//全部url都播放失败 //全部url都播放失败
_timer = nullptr;
_timer_ts = nullptr;
onShutdown(ex); onShutdown(ex);
} else { } else {
_m3u8_list.pop_back(); _m3u8_list.pop_back();
@ -399,6 +407,7 @@ void HlsPlayerImp::onPlayResult(const SockException &ex) {
} }
void HlsPlayerImp::onShutdown(const SockException &ex) { void HlsPlayerImp::onShutdown(const SockException &ex) {
WarnL << ex.what();
PlayerImp<HlsPlayer, PlayerBase>::onShutdown(ex); PlayerImp<HlsPlayer, PlayerBase>::onShutdown(ex);
_demuxer = nullptr; _demuxer = nullptr;
} }

View File

@ -112,6 +112,7 @@ private:
private: private:
bool _is_m3u8 = false; bool _is_m3u8 = false;
bool _first = true; bool _first = true;
bool _waiting_response = false;
int64_t _last_sequence = -1; int64_t _last_sequence = -1;
string _m3u8; string _m3u8;
Timer::Ptr _timer; Timer::Ptr _timer;

View File

@ -80,20 +80,27 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
startConnect(host, port, fTimeOutSec); startConnect(host, port, fTimeOutSec);
} else { } else {
SockException ex; SockException ex;
onConnect(ex); onConnect_l(ex);
} }
} }
void HttpClient::clear() { void HttpClient::clear() {
_url.clear();
_header.clear(); _header.clear();
_body.reset(); _body.reset();
_method.clear(); _method.clear();
_path.clear(); _path.clear();
_parser.Clear();
_recvedBodySize = 0;
_totalBodySize = 0;
_aliveTicker.resetTime(); _aliveTicker.resetTime();
_chunkedSplitter.reset(); _chunkedSplitter.reset();
_fTimeOutSec = 0;
clearResponse();
}
void HttpClient::clearResponse() {
_recvedBodySize = 0;
_totalBodySize = 0;
_parser.Clear();
_chunkedSplitter = nullptr;
HttpRequestSplitter::reset(); HttpRequestSplitter::reset();
} }
@ -131,16 +138,17 @@ const string &HttpClient::getUrl() const {
} }
void HttpClient::onConnect(const SockException &ex) { void HttpClient::onConnect(const SockException &ex) {
onConnect_l(ex);
}
void HttpClient::onConnect_l(const SockException &ex) {
_aliveTicker.resetTime(); _aliveTicker.resetTime();
if (ex) { if (ex) {
onDisconnect(ex); onDisconnect(ex);
return; return;
} }
_totalBodySize = 0; clearResponse();
_recvedBodySize = 0;
HttpRequestSplitter::reset();
_chunkedSplitter.reset();
_StrPrinter printer; _StrPrinter printer;
printer << _method + " " << _path + " HTTP/1.1\r\n"; printer << _method + " " << _path + " HTTP/1.1\r\n";
@ -280,8 +288,6 @@ void HttpClient::onManager() {
} }
void HttpClient::onResponseCompleted_l() { void HttpClient::onResponseCompleted_l() {
_totalBodySize = 0;
_recvedBodySize = 0;
onResponseCompleted(); onResponseCompleted();
} }

View File

@ -165,7 +165,9 @@ protected:
private: private:
void onResponseCompleted_l(); void onResponseCompleted_l();
void onConnect_l(const SockException &ex);
void checkCookie(HttpHeader &headers); void checkCookie(HttpHeader &headers);
void clearResponse();
protected: protected:
bool _isHttps; bool _isHttps;
@ -176,13 +178,14 @@ private:
HttpBody::Ptr _body; HttpBody::Ptr _body;
string _method; string _method;
string _path; string _path;
string _lastHost;
Ticker _aliveTicker;
float _fTimeOutSec = 0;
//recv //recv
size_t _recvedBodySize; size_t _recvedBodySize;
ssize_t _totalBodySize; ssize_t _totalBodySize;
Parser _parser; Parser _parser;
string _lastHost;
Ticker _aliveTicker;
float _fTimeOutSec = 0;
std::shared_ptr<HttpChunkedSplitter> _chunkedSplitter; std::shared_ptr<HttpChunkedSplitter> _chunkedSplitter;
}; };