hls m3u8文件直接通过内存读写

This commit is contained in:
ziyue
2022-02-11 16:21:19 +08:00
parent 642c9c075a
commit 49fc17d7e0
9 changed files with 96 additions and 69 deletions

View File

@@ -11,11 +11,11 @@
#ifndef ZLMEDIAKIT_HLSMEDIASOURCE_H
#define ZLMEDIAKIT_HLSMEDIASOURCE_H
#include <atomic>
#include "Util/TimeTicker.h"
#include "Common/MediaSource.h"
#include "Util/TimeTicker.h"
#include <atomic>
namespace mediakit{
namespace mediakit {
class HlsMediaSource : public MediaSource {
public:
@@ -24,28 +24,25 @@ public:
using RingType = toolkit::RingBuffer<std::string>;
using Ptr = std::shared_ptr<HlsMediaSource>;
HlsMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){}
HlsMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id)
: MediaSource(HLS_SCHEMA, vhost, app, stream_id) {}
~HlsMediaSource() override = default;
/**
* 获取媒体源的环形缓冲
*/
const RingType::Ptr &getRing() const {
return _ring;
}
const RingType::Ptr &getRing() const { return _ring; }
/**
* 获取播放器个数
*/
int readerCount() override {
return _ring ? _ring->readerCount() : 0;
}
int readerCount() override { return _ring ? _ring->readerCount() : 0; }
/**
* 生成m3u8文件时触发
* @param file_created 是否产生了hls文件
* @param index_file m3u8文件内容
*/
void registHls(bool file_created){
void registHls(std::string index_file) {
if (!_is_regist) {
_is_regist = true;
std::weak_ptr<HlsMediaSource> weakSelf = std::dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
@@ -61,56 +58,68 @@ public:
regist();
}
if (!file_created) {
//没产生文件
if (index_file.empty()) {
//没产生索引文件, 只是为了触发媒体注册
return;
}
//m3u8文件生成发送给播放器
decltype(_list_cb) copy;
{
std::lock_guard<std::mutex> lck(_mtx_cb);
copy.swap(_list_cb);
}
copy.for_each([](const std::function<void()> &cb) {
cb();
});
//赋值m3u8索引文件内容
std::lock_guard<std::mutex> lck(_mtx_index);
_index_file = std::move(index_file);
_list_cb.for_each([&](const std::function<void(const std::string &str)> &cb) { cb(_index_file); });
_list_cb.clear();
}
void waitForFile(std::function<void()> cb) {
void getIndexFile(std::function<void(const std::string &str)> cb) {
std::lock_guard<std::mutex> lck(_mtx_index);
if (!_index_file.empty()) {
cb(_index_file);
return;
}
//等待生成m3u8文件
std::lock_guard<std::mutex> lck(_mtx_cb);
_list_cb.emplace_back(std::move(cb));
}
void onSegmentSize(size_t bytes) {
_speed[TrackVideo] += bytes;
std::string getIndexFile() const {
std::lock_guard<std::mutex> lck(_mtx_index);
return _index_file;
}
void onSegmentSize(size_t bytes) { _speed[TrackVideo] += bytes; }
private:
bool _is_regist = false;
RingType::Ptr _ring;
std::mutex _mtx_cb;
toolkit::List<std::function<void()> > _list_cb;
std::string _index_file;
mutable std::mutex _mtx_index;
toolkit::List<std::function<void(const std::string &)> > _list_cb;
};
class HlsCookieData{
class HlsCookieData {
public:
typedef std::shared_ptr<HlsCookieData> Ptr;
using Ptr = std::shared_ptr<HlsCookieData>;
HlsCookieData(const MediaInfo &info, const std::shared_ptr<toolkit::SockInfo> &sock_info);
~HlsCookieData();
void addByteUsage(size_t bytes);
void setMediaSource(const HlsMediaSource::Ptr &src);
HlsMediaSource::Ptr getMediaSource() const;
private:
void addReaderCount();
private:
std::atomic<uint64_t> _bytes {0};
std::atomic<uint64_t> _bytes { 0 };
MediaInfo _info;
std::shared_ptr<bool> _added;
toolkit::Ticker _ticker;
std::weak_ptr<HlsMediaSource> _src;
std::shared_ptr<toolkit::SockInfo> _sock_info;
HlsMediaSource::RingType::RingReader::Ptr _ring_reader;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_HLSMEDIASOURCE_H
} // namespace mediakit
#endif // ZLMEDIAKIT_HLSMEDIASOURCE_H