HLS: 修复hls直播ts/m3u8文件更新导致mmap失效,触发bus error的bug

This commit is contained in:
ziyue 2021-12-22 15:42:03 +08:00
parent fe575af0d8
commit 2e9ff3ed3c
6 changed files with 44 additions and 38 deletions

View File

@ -45,27 +45,28 @@ Buffer::Ptr HttpStringBody::readData(size_t size) {
//////////////////////////////////////////////////////////////////
HttpFileBody::HttpFileBody(const string &filePath){
HttpFileBody::HttpFileBody(const string &filePath, bool use_mmap) {
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
if (fp) {
fclose(fp);
}
});
if (!fp) {
init(fp, 0, 0);
init(fp, 0, 0, use_mmap);
} else {
init(fp, 0, File::fileSize(fp.get()));
init(fp, 0, File::fileSize(fp.get()), use_mmap);
}
}
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size) {
init(fp,offset,max_size);
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size, bool use_mmap) {
init(fp, offset, max_size, use_mmap);
}
void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size) {
void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size, bool use_mmap) {
_fp = fp;
_max_size = max_size;
#ifdef ENABLE_MMAP
if (use_mmap) {
do {
if (!_fp) {
//文件不存在
@ -85,6 +86,7 @@ void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t m
munmap(ptr, max_size);
});
} while (false);
}
#endif
if (!_map_addr && offset && fp.get()) {
//未映射,那么fseek设置偏移量
@ -92,7 +94,6 @@ void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t m
}
}
class BufferMmap : public Buffer{
public:
typedef std::shared_ptr<BufferMmap> Ptr;

View File

@ -107,16 +107,17 @@ public:
* @param fp 0
* @param offset
* @param max_size
* @param use_mmap 使mmap方式访问文件
*/
HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size);
HttpFileBody(const string &file_path);
HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size, bool use_mmap = true);
HttpFileBody(const string &file_path, bool use_mmap = true);
~HttpFileBody() override = default;
ssize_t remainSize() override ;
Buffer::Ptr readData(size_t size) override;
private:
void init(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size);
void init(const std::shared_ptr<FILE> &fp,size_t offset,size_t max_size, bool use_mmap);
private:
size_t _max_size;

View File

@ -405,7 +405,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
return;
}
auto response_file = [file_exist](const HttpServerCookie::Ptr &cookie, const HttpFileManager::invoker &cb, const string &strFile, const Parser &parser) {
auto response_file = [file_exist, is_hls](const HttpServerCookie::Ptr &cookie, const HttpFileManager::invoker &cb, const string &strFile, const Parser &parser) {
StrCaseMap httpHeader;
if (cookie) {
auto lck = cookie->getLock();
@ -421,7 +421,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
}
cb(code, HttpFileManager::getContentType(strFile.data()), headerOut, body);
};
invoker.responseFile(parser.getHeader(), httpHeader, strFile);
invoker.responseFile(parser.getHeader(), httpHeader, strFile, !is_hls);
};
if (!is_hls) {
@ -576,7 +576,8 @@ HttpResponseInvokerImp::HttpResponseInvokerImp(const HttpResponseInvokerImp::Htt
void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
const StrCaseMap &responseHeader,
const string &filePath) const {
const string &filePath,
bool use_mmap) const {
StrCaseMap &httpHeader = const_cast<StrCaseMap &>(responseHeader);
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
if (fp) {
@ -618,7 +619,7 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
}
//回复文件
HttpBody::Ptr fileBody = std::make_shared<HttpFileBody>(fp, iRangeStart, iRangeEnd - iRangeStart + 1);
HttpBody::Ptr fileBody = std::make_shared<HttpFileBody>(fp, iRangeStart, iRangeEnd - iRangeStart + 1, use_mmap);
(*this)(code, httpHeader, fileBody);
}

View File

@ -35,7 +35,7 @@ public:
void operator()(int code, const StrCaseMap &headerOut, const HttpBody::Ptr &body) const;
void operator()(int code, const StrCaseMap &headerOut, const string &body) const;
void responseFile(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath) const;
void responseFile(const StrCaseMap &requestHeader,const StrCaseMap &responseHeader,const string &filePath, bool use_mmap = true) const;
operator bool();
private:
HttpResponseInvokerLambda0 _lambad;

View File

@ -132,8 +132,10 @@ void HlsMaker::flushLastSegment(bool eof){
_seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name)));
_last_file_name.clear();
delOldSegment();
makeIndexFile(eof);
//先flush ts切片否则可能存在ts文件未写入完毕就被访问的情况
onFlushLastSegment(seg_dur);
//然后写m3u8文件
makeIndexFile(eof);
}
bool HlsMaker::isLive() {

View File

@ -125,10 +125,11 @@ void HlsMakerImp::onWriteHls(const char *data, size_t len) {
}
void HlsMakerImp::onFlushLastSegment(uint32_t duration_ms) {
//关闭并flush文件到磁盘
_file = nullptr;
GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
if (broadcastRecordTs) {
//关闭ts文件以便获取正确的文件大小
_file = nullptr;
_info.time_len = duration_ms / 1000.0f;
struct stat fileData;
stat(_info.file_path.data(), &fileData);