mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
HLS: 修复hls直播ts/m3u8文件更新导致mmap失效,触发bus error的bug
This commit is contained in:
parent
fe575af0d8
commit
2e9ff3ed3c
@ -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) {
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
||||||
if (fp) {
|
if (fp) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
init(fp, 0, 0);
|
init(fp, 0, 0, use_mmap);
|
||||||
} else {
|
} 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) {
|
HttpFileBody::HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size, bool use_mmap) {
|
||||||
init(fp,offset,max_size);
|
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;
|
_fp = fp;
|
||||||
_max_size = max_size;
|
_max_size = max_size;
|
||||||
#ifdef ENABLE_MMAP
|
#ifdef ENABLE_MMAP
|
||||||
|
if (use_mmap) {
|
||||||
do {
|
do {
|
||||||
if (!_fp) {
|
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);
|
munmap(ptr, max_size);
|
||||||
});
|
});
|
||||||
} while (false);
|
} while (false);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (!_map_addr && offset && fp.get()) {
|
if (!_map_addr && offset && fp.get()) {
|
||||||
//未映射,那么fseek设置偏移量
|
//未映射,那么fseek设置偏移量
|
||||||
@ -92,7 +94,6 @@ void HttpFileBody::init(const std::shared_ptr<FILE> &fp, size_t offset, size_t m
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class BufferMmap : public Buffer{
|
class BufferMmap : public Buffer{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<BufferMmap> Ptr;
|
typedef std::shared_ptr<BufferMmap> Ptr;
|
||||||
|
@ -107,16 +107,17 @@ public:
|
|||||||
* @param fp 文件句柄,文件的偏移量必须为0
|
* @param fp 文件句柄,文件的偏移量必须为0
|
||||||
* @param offset 相对文件头的偏移量
|
* @param offset 相对文件头的偏移量
|
||||||
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
* @param max_size 最大读取字节数,未判断是否大于文件真实大小
|
||||||
|
* @param use_mmap 是否使用mmap方式访问文件
|
||||||
*/
|
*/
|
||||||
HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size);
|
HttpFileBody(const std::shared_ptr<FILE> &fp, size_t offset, size_t max_size, bool use_mmap = true);
|
||||||
HttpFileBody(const string &file_path);
|
HttpFileBody(const string &file_path, bool use_mmap = true);
|
||||||
~HttpFileBody() override = default;
|
~HttpFileBody() override = default;
|
||||||
|
|
||||||
ssize_t remainSize() override ;
|
ssize_t remainSize() override ;
|
||||||
Buffer::Ptr readData(size_t size) override;
|
Buffer::Ptr readData(size_t size) override;
|
||||||
|
|
||||||
private:
|
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:
|
private:
|
||||||
size_t _max_size;
|
size_t _max_size;
|
||||||
|
@ -405,7 +405,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
|
|||||||
return;
|
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;
|
StrCaseMap httpHeader;
|
||||||
if (cookie) {
|
if (cookie) {
|
||||||
auto lck = cookie->getLock();
|
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);
|
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) {
|
if (!is_hls) {
|
||||||
@ -576,7 +576,8 @@ HttpResponseInvokerImp::HttpResponseInvokerImp(const HttpResponseInvokerImp::Htt
|
|||||||
|
|
||||||
void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
|
void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
|
||||||
const StrCaseMap &responseHeader,
|
const StrCaseMap &responseHeader,
|
||||||
const string &filePath) const {
|
const string &filePath,
|
||||||
|
bool use_mmap) const {
|
||||||
StrCaseMap &httpHeader = const_cast<StrCaseMap &>(responseHeader);
|
StrCaseMap &httpHeader = const_cast<StrCaseMap &>(responseHeader);
|
||||||
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
std::shared_ptr<FILE> fp(fopen(filePath.data(), "rb"), [](FILE *fp) {
|
||||||
if (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);
|
(*this)(code, httpHeader, fileBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 HttpBody::Ptr &body) const;
|
||||||
void operator()(int code, const StrCaseMap &headerOut, const string &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();
|
operator bool();
|
||||||
private:
|
private:
|
||||||
HttpResponseInvokerLambda0 _lambad;
|
HttpResponseInvokerLambda0 _lambad;
|
||||||
|
@ -132,8 +132,10 @@ void HlsMaker::flushLastSegment(bool eof){
|
|||||||
_seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name)));
|
_seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name)));
|
||||||
_last_file_name.clear();
|
_last_file_name.clear();
|
||||||
delOldSegment();
|
delOldSegment();
|
||||||
makeIndexFile(eof);
|
//先flush ts切片,否则可能存在ts文件未写入完毕就被访问的情况
|
||||||
onFlushLastSegment(seg_dur);
|
onFlushLastSegment(seg_dur);
|
||||||
|
//然后写m3u8文件
|
||||||
|
makeIndexFile(eof);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HlsMaker::isLive() {
|
bool HlsMaker::isLive() {
|
||||||
|
@ -125,10 +125,11 @@ void HlsMakerImp::onWriteHls(const char *data, size_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HlsMakerImp::onFlushLastSegment(uint32_t duration_ms) {
|
void HlsMakerImp::onFlushLastSegment(uint32_t duration_ms) {
|
||||||
|
//关闭并flush文件到磁盘
|
||||||
|
_file = nullptr;
|
||||||
|
|
||||||
GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
|
GET_CONFIG(bool, broadcastRecordTs, Hls::kBroadcastRecordTs);
|
||||||
if (broadcastRecordTs) {
|
if (broadcastRecordTs) {
|
||||||
//关闭ts文件以便获取正确的文件大小
|
|
||||||
_file = nullptr;
|
|
||||||
_info.time_len = duration_ms / 1000.0f;
|
_info.time_len = duration_ms / 1000.0f;
|
||||||
struct stat fileData;
|
struct stat fileData;
|
||||||
stat(_info.file_path.data(), &fileData);
|
stat(_info.file_path.data(), &fileData);
|
||||||
|
Loading…
Reference in New Issue
Block a user