2019-08-08 19:01:45 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-04-01 12:57:33 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef HLSRECORDER_H
|
|
|
|
|
#define HLSRECORDER_H
|
|
|
|
|
|
|
|
|
|
#include "HlsMakerImp.h"
|
2021-12-28 21:04:53 +08:00
|
|
|
|
#include "MPEG.h"
|
|
|
|
|
|
2019-04-01 12:57:33 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2021-12-28 21:04:53 +08:00
|
|
|
|
class HlsRecorder : public MediaSourceEventInterceptor, public MpegMuxer, public std::enable_shared_from_this<HlsRecorder> {
|
2019-04-01 12:57:33 +08:00
|
|
|
|
public:
|
2021-12-28 21:04:53 +08:00
|
|
|
|
using Ptr = std::shared_ptr<HlsRecorder>;
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
HlsRecorder(const std::string &m3u8_file, const std::string ¶ms) : MpegMuxer(false) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
GET_CONFIG(uint32_t, hlsNum, Hls::kSegmentNum);
|
2022-05-10 17:32:50 +08:00
|
|
|
|
GET_CONFIG(bool, hlsKeep, Hls::kSegmentKeep);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
GET_CONFIG(uint32_t, hlsBufSize, Hls::kFileBufSize);
|
2021-01-17 18:31:50 +08:00
|
|
|
|
GET_CONFIG(float, hlsDuration, Hls::kSegmentDuration);
|
2022-05-10 17:32:50 +08:00
|
|
|
|
_hls = std::make_shared<HlsMakerImp>(m3u8_file, params, hlsBufSize, hlsDuration, hlsNum, hlsKeep);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
//清空上次的残余文件
|
|
|
|
|
_hls->clearCache();
|
2019-12-04 10:45:38 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2021-12-28 21:04:53 +08:00
|
|
|
|
~HlsRecorder() = default;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void setMediaSource(const std::string &vhost, const std::string &app, const std::string &stream_id) {
|
2019-12-29 10:49:04 +08:00
|
|
|
|
_hls->setMediaSource(vhost, app, stream_id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
void setListener(const std::weak_ptr<MediaSourceEvent> &listener) {
|
2020-10-24 23:31:58 +08:00
|
|
|
|
setDelegate(listener);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_hls->getMediaSource()->setListener(shared_from_this());
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 16:24:55 +08:00
|
|
|
|
int readerCount() { return _hls->getMediaSource()->readerCount(); }
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
|
|
|
|
void onReaderChanged(MediaSource &sender, int size) override {
|
2020-11-15 00:40:46 +08:00
|
|
|
|
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
|
2022-02-12 16:24:55 +08:00
|
|
|
|
// hls保留切片个数为0时代表为hls录制(不删除切片),那么不管有无观看者都一直生成hls
|
2020-11-15 00:40:46 +08:00
|
|
|
|
_enabled = hls_demand ? (_hls->isLive() ? size : true) : true;
|
|
|
|
|
if (!size && _hls->isLive() && hls_demand) {
|
2022-02-12 16:24:55 +08:00
|
|
|
|
// hls直播时,如果无人观看就删除视频缓存,目的是为了防止视频跳跃
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_clear_cache = true;
|
|
|
|
|
}
|
|
|
|
|
MediaSourceEventInterceptor::onReaderChanged(sender, size);
|
2019-12-28 18:50:56 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override {
|
2020-11-15 00:40:46 +08:00
|
|
|
|
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
|
|
|
|
|
if (_clear_cache && hls_demand) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_clear_cache = false;
|
2022-02-12 16:24:55 +08:00
|
|
|
|
//清空旧的m3u8索引文件于ts切片
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_hls->clearCache();
|
2022-02-12 16:24:55 +08:00
|
|
|
|
_hls->getMediaSource()->setIndexFile("");
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
2020-11-15 00:40:46 +08:00
|
|
|
|
if (_enabled || !hls_demand) {
|
2021-12-28 21:04:53 +08:00
|
|
|
|
return MpegMuxer::inputFrame(frame);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return false;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 16:24:55 +08:00
|
|
|
|
bool isEnabled() {
|
|
|
|
|
GET_CONFIG(bool, hls_demand, General::kHlsDemand);
|
|
|
|
|
//缓存尚未清空时,还允许触发inputFrame函数,以便及时清空缓存
|
|
|
|
|
return hls_demand ? (_clear_cache ? true : _enabled) : true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
private:
|
2022-08-08 17:13:39 +08:00
|
|
|
|
void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint64_t timestamp, bool key_pos) override {
|
2021-07-07 16:15:42 +08:00
|
|
|
|
if (!buffer) {
|
2021-12-28 21:04:53 +08:00
|
|
|
|
_hls->inputData(nullptr, 0, timestamp, key_pos);
|
2021-07-07 16:15:42 +08:00
|
|
|
|
} else {
|
2021-12-28 21:04:53 +08:00
|
|
|
|
_hls->inputData(buffer->data(), buffer->size(), timestamp, key_pos);
|
2021-07-07 16:15:42 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 10:45:38 +08:00
|
|
|
|
private:
|
2022-02-12 16:24:55 +08:00
|
|
|
|
bool _enabled = true;
|
2020-09-12 19:20:18 +08:00
|
|
|
|
bool _clear_cache = false;
|
|
|
|
|
std::shared_ptr<HlsMakerImp> _hls;
|
2019-04-01 12:57:33 +08:00
|
|
|
|
};
|
|
|
|
|
}//namespace mediakit
|
|
|
|
|
#endif //HLSRECORDER_H
|