This commit is contained in:
xiongziliang 2022-04-07 20:39:34 +08:00
commit 6c0fea4c46
5 changed files with 34 additions and 16 deletions

View File

@ -42,7 +42,22 @@ void HlsPlayer::teardown_l(const SockException &ex) {
_play_result = true;
onPlayResult(ex);
} else {
onShutdown(ex);
//如果不是主动关闭的,则重新拉取索引文件
if (ex.getErrCode() != Err_shutdown) {
// 当切片列表已空, 且没有正在下载的切片并且重试次数已经达到最大次数时, 则认为失败关闭播放器
if (_ts_list.empty() && !(_http_ts_player && _http_ts_player->waitResponse())
&& _try_fetch_index_times >= MAX_TRY_FETCH_INDEX_TIMES) {
onShutdown(ex);
} else {
_try_fetch_index_times += 1;
shutdown(ex);
WarnL << "重新尝试拉取索引文件[" << _try_fetch_index_times << "]:" << _play_url;
fetchIndexFile();
return;
}
} else {
onShutdown(ex);
}
}
_timer.reset();
_timer_ts.reset();

View File

@ -19,6 +19,7 @@
#define MIN_TIMEOUT_MULTIPLE 2
#define MAX_TIMEOUT_MULTIPLE 5
#define MAX_TRY_FETCH_INDEX_TIMES 5
namespace mediakit {
@ -105,6 +106,7 @@ private:
std::set<std::string, UrlComp> _ts_url_cache;
HttpTSPlayer::Ptr _http_ts_player;
int _timeout_multiple = MIN_TIMEOUT_MULTIPLE;
int _try_fetch_index_times = 0;
};
class HlsPlayerImp : public PlayerImp<HlsPlayer, PlayerBase>, private TrackListener {

View File

@ -9,29 +9,32 @@
*/
#include <string.h>
#include "Common/macros.h"
#include "HttpChunkedSplitter.h"
using namespace std;
//[chunk size][\r\n][chunk data][\r\n][chunk size][\r\n][chunk data][\r\n][chunk size = 0][\r\n][\r\n]
namespace mediakit{
const char *HttpChunkedSplitter::onSearchPacketTail(const char *data, size_t len) {
auto pos = strstr(data,"\r\n");
if(!pos){
auto pos = strstr(data, "\r\n");
if (!pos) {
return nullptr;
}
return pos + 2;
}
void HttpChunkedSplitter::onRecvContent(const char *data, size_t len) {
onRecvChunk(data,len - 2);
onRecvChunk(data, len - 2);
}
ssize_t HttpChunkedSplitter::onRecvHeader(const char *data, size_t len) {
string str(data,len - 2);
int ret;
sscanf(str.data(),"%X",&ret);
return ret + 2;
int size;
CHECK(sscanf(data, "%X", &size) == 1 && size >= 0);
//包括后面\r\n两个字节
return size + 2;
}
}//namespace mediakit
}//namespace mediakit

View File

@ -21,12 +21,10 @@ public:
/**
* len == 0
*/
typedef std::function<void (const char *data,size_t len)> onChunkData;
using onChunkData = std::function<void(const char *data, size_t len)>;
HttpChunkedSplitter(const onChunkData &cb){
_onChunkData = cb;
};
~HttpChunkedSplitter() override {} ;
HttpChunkedSplitter(const onChunkData &cb) { _onChunkData = cb; };
~HttpChunkedSplitter() override { _onChunkData = nullptr; };
protected:
ssize_t onRecvHeader(const char *data,size_t len) override;

View File

@ -137,7 +137,7 @@ void HlsMaker::flushLastSegment(bool eof){
if (seg_dur <= 0) {
seg_dur = 100;
}
_seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name)));
_seg_dur_list.emplace_back(seg_dur, std::move(_last_file_name));
delOldSegment();
//先flush ts切片否则可能存在ts文件未写入完毕就被访问的情况
onFlushLastSegment(seg_dur);