2019-07-24 18:40:18 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-07-24 18:40:18 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-07-24 18:40:18 +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-07-24 18:40:18 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Frame.h"
|
2021-02-05 11:51:16 +08:00
|
|
|
|
#include "H264.h"
|
|
|
|
|
#include "H265.h"
|
2021-03-30 10:59:15 +08:00
|
|
|
|
#include "Common/Parser.h"
|
2019-07-24 18:40:18 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2021-01-23 09:44:37 +08:00
|
|
|
|
namespace toolkit {
|
|
|
|
|
StatisticImp(mediakit::Frame);
|
|
|
|
|
StatisticImp(mediakit::FrameImp);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:18 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
template<typename C>
|
|
|
|
|
std::shared_ptr<C> FrameImp::create_l() {
|
|
|
|
|
#if 0
|
|
|
|
|
static ResourcePool<C> packet_pool;
|
|
|
|
|
static onceToken token([]() {
|
|
|
|
|
packet_pool.setSize(1024);
|
|
|
|
|
});
|
|
|
|
|
auto ret = packet_pool.obtain();
|
|
|
|
|
ret->_buffer.clear();
|
|
|
|
|
ret->_prefix_size = 0;
|
|
|
|
|
ret->_dts = 0;
|
|
|
|
|
ret->_pts = 0;
|
|
|
|
|
return ret;
|
|
|
|
|
#else
|
|
|
|
|
return std::shared_ptr<C>(new C());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CREATE_FRAME_IMP(C) \
|
|
|
|
|
template<> \
|
|
|
|
|
std::shared_ptr<C> FrameImp::create<C>() { \
|
|
|
|
|
return create_l<C>(); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CREATE_FRAME_IMP(FrameImp);
|
|
|
|
|
CREATE_FRAME_IMP(H264Frame);
|
|
|
|
|
CREATE_FRAME_IMP(H265Frame);
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
|
|
|
|
|
*/
|
|
|
|
|
class FrameCacheAble : public FrameFromPtr {
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<FrameCacheAble> Ptr;
|
|
|
|
|
|
|
|
|
|
FrameCacheAble(const Frame::Ptr &frame){
|
|
|
|
|
if(frame->cacheAble()){
|
|
|
|
|
_frame = frame;
|
|
|
|
|
_ptr = frame->data();
|
|
|
|
|
}else{
|
2021-02-05 11:51:16 +08:00
|
|
|
|
_buffer = FrameImp::create();
|
|
|
|
|
_buffer->_buffer.assign(frame->data(),frame->size());
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_ptr = _buffer->data();
|
|
|
|
|
}
|
|
|
|
|
_size = frame->size();
|
|
|
|
|
_dts = frame->dts();
|
|
|
|
|
_pts = frame->pts();
|
|
|
|
|
_prefix_size = frame->prefixSize();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_codec_id = frame->getCodecId();
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_key = frame->keyFrame();
|
|
|
|
|
_config = frame->configFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 10:22:12 +08:00
|
|
|
|
~FrameCacheAble() override = default;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 可以被缓存
|
|
|
|
|
*/
|
|
|
|
|
bool cacheAble() const override {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keyFrame() const override{
|
|
|
|
|
return _key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool configFrame() const override{
|
|
|
|
|
return _config;
|
|
|
|
|
}
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
private:
|
|
|
|
|
bool _key;
|
|
|
|
|
bool _config;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Frame::Ptr _frame;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
FrameImp::Ptr _buffer;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-24 18:40:18 +08:00
|
|
|
|
Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
|
|
|
|
|
if(frame->cacheAble()){
|
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
return std::make_shared<FrameCacheAble>(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 11:14:10 +08:00
|
|
|
|
TrackType getTrackType(CodecId codecId) {
|
|
|
|
|
switch (codecId) {
|
|
|
|
|
#define XX(name, type, value, str) case name : return type;
|
|
|
|
|
CODEC_MAP(XX)
|
|
|
|
|
#undef XX
|
|
|
|
|
default : return TrackInvalid;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-24 18:40:18 +08:00
|
|
|
|
|
2021-06-16 11:14:10 +08:00
|
|
|
|
const char *getCodecName(CodecId codec) {
|
2021-03-30 10:59:15 +08:00
|
|
|
|
switch (codec) {
|
2021-06-16 11:14:10 +08:00
|
|
|
|
#define XX(name, type, value, str) case name : return str;
|
|
|
|
|
CODEC_MAP(XX)
|
|
|
|
|
#undef XX
|
|
|
|
|
default : return "invalid";
|
2021-03-30 10:59:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 11:14:10 +08:00
|
|
|
|
#define XX(name, type, value, str) {str, name},
|
|
|
|
|
static map<string, CodecId, StrCaseCompare> codec_map = {CODEC_MAP(XX)};
|
|
|
|
|
#undef XX
|
2021-03-30 10:59:15 +08:00
|
|
|
|
|
|
|
|
|
CodecId getCodecId(const string &str){
|
|
|
|
|
auto it = codec_map.find(str);
|
|
|
|
|
return it == codec_map.end() ? CodecInvalid : it->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static map<string, TrackType, StrCaseCompare> track_str_map = {
|
|
|
|
|
{"video", TrackVideo},
|
|
|
|
|
{"audio", TrackAudio},
|
|
|
|
|
{"application", TrackApplication}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TrackType getTrackType(const string &str) {
|
|
|
|
|
auto it = track_str_map.find(str);
|
|
|
|
|
return it == track_str_map.end() ? TrackInvalid : it->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* getTrackString(TrackType type){
|
|
|
|
|
switch (type) {
|
|
|
|
|
case TrackVideo : return "video";
|
|
|
|
|
case TrackAudio : return "audio";
|
|
|
|
|
case TrackApplication : return "application";
|
|
|
|
|
default: return "invalid";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
const char *CodecInfo::getCodecName() {
|
|
|
|
|
return mediakit::getCodecName(getCodecId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TrackType CodecInfo::getTrackType() {
|
|
|
|
|
return mediakit::getTrackType(getCodecId());
|
|
|
|
|
}
|
2021-04-26 18:26:07 +08:00
|
|
|
|
|
|
|
|
|
static size_t constexpr kMaxFrameCacheSize = 100;
|
|
|
|
|
|
|
|
|
|
bool FrameMerger::willFlush(const Frame::Ptr &frame) const{
|
|
|
|
|
if (_frameCached.empty()) {
|
2021-07-07 11:02:05 +08:00
|
|
|
|
//缓存为空
|
2021-04-26 18:26:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case none : {
|
|
|
|
|
//frame不是完整的帧,我们合并为一帧
|
|
|
|
|
bool new_frame = false;
|
|
|
|
|
switch (frame->getCodecId()) {
|
|
|
|
|
case CodecH264:
|
|
|
|
|
case CodecH265: {
|
|
|
|
|
//如果是新的一帧,前面的缓存需要输出
|
|
|
|
|
new_frame = frame->prefixSize();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
2021-07-06 23:37:04 +08:00
|
|
|
|
//遇到新帧、或时间戳变化或缓存太多,防止内存溢出,则flush输出
|
2021-04-26 18:26:07 +08:00
|
|
|
|
return new_frame || _frameCached.back()->dts() != frame->dts() || _frameCached.size() > kMaxFrameCacheSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case mp4_nal_size:
|
|
|
|
|
case h264_prefix: {
|
2021-07-07 11:02:05 +08:00
|
|
|
|
if (!frameCacheHasVCL()) {
|
|
|
|
|
//缓存中没有有效的能解码的帧,所以这次不flush
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-26 18:26:07 +08:00
|
|
|
|
if (_frameCached.back()->dts() != frame->dts()) {
|
2021-07-07 11:02:05 +08:00
|
|
|
|
//时间戳变化了,立即flush
|
2021-04-26 18:26:07 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-06-15 12:55:55 +08:00
|
|
|
|
switch (frame->getCodecId()) {
|
|
|
|
|
case CodecH264 : {
|
2021-06-17 19:26:58 +08:00
|
|
|
|
auto type = H264_TYPE(frame->data()[frame->prefixSize()]);
|
2021-07-06 23:37:04 +08:00
|
|
|
|
// sei aud pps sps 不判断;264 新一帧的开始,刷新输出
|
|
|
|
|
return (frame->data()[frame->prefixSize() + 1] & 0x80) != 0 && type >= H264Frame::NAL_B_P &&
|
|
|
|
|
type <= H264Frame::NAL_IDR;
|
2021-06-15 12:55:55 +08:00
|
|
|
|
}
|
|
|
|
|
case CodecH265 : {
|
2021-06-18 12:40:42 +08:00
|
|
|
|
auto type = H265_TYPE(frame->data()[frame->prefixSize()]);
|
2021-07-06 23:37:04 +08:00
|
|
|
|
//first_slice_segment_in_pic_flag is frame start
|
|
|
|
|
return (type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23) &&
|
|
|
|
|
((frame->data()[frame->prefixSize() + 2] >> 7 & 0x01) != 0);
|
2021-06-15 12:55:55 +08:00
|
|
|
|
}
|
2021-07-06 23:37:04 +08:00
|
|
|
|
default :
|
|
|
|
|
//缓存太多,防止内存溢出
|
|
|
|
|
return _frameCached.size() > kMaxFrameCacheSize;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default: /*不可达*/ assert(0); return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameMerger::doMerge(BufferLikeString &merged, const Frame::Ptr &frame) const{
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case none : {
|
2021-07-06 23:37:04 +08:00
|
|
|
|
//此处是合并ps解析输出的流,解析出的流可能是半帧或多帧,不能简单的根据nal type过滤
|
|
|
|
|
//此流程只用于合并ps解析输出为H264/H265,后面流程有split和忽略无效帧操作
|
2021-04-26 18:26:07 +08:00
|
|
|
|
merged.append(frame->data(), frame->size());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case h264_prefix: {
|
|
|
|
|
if (frame->prefixSize()) {
|
|
|
|
|
merged.append(frame->data(), frame->size());
|
|
|
|
|
} else {
|
|
|
|
|
merged.append("\x00\x00\x00\x01", 4);
|
|
|
|
|
merged.append(frame->data(), frame->size());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case mp4_nal_size: {
|
|
|
|
|
uint32_t nalu_size = (uint32_t) (frame->size() - frame->prefixSize());
|
|
|
|
|
nalu_size = htonl(nalu_size);
|
|
|
|
|
merged.append((char *) &nalu_size, 4);
|
|
|
|
|
merged.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: /*不可达*/ assert(0); break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-28 10:35:08 +08:00
|
|
|
|
|
2021-06-18 15:19:21 +08:00
|
|
|
|
bool FrameMerger::shouldDrop(const Frame::Ptr &frame) const{
|
|
|
|
|
switch (frame->getCodecId()) {
|
2021-06-28 10:35:08 +08:00
|
|
|
|
case CodecH264: {
|
2021-07-06 23:37:04 +08:00
|
|
|
|
switch (H264_TYPE(frame->data()[frame->prefixSize()])) {
|
|
|
|
|
// 防止把AUD或者SEI当成一帧
|
|
|
|
|
case H264Frame::NAL_SEI:
|
|
|
|
|
case H264Frame::NAL_AUD: return true;
|
|
|
|
|
default: return false;
|
2021-06-28 10:35:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-06 23:37:04 +08:00
|
|
|
|
|
2021-06-28 10:35:08 +08:00
|
|
|
|
case CodecH265: {
|
2021-07-06 23:37:04 +08:00
|
|
|
|
switch (H265_TYPE(frame->data()[frame->prefixSize()])) {
|
|
|
|
|
// 防止把AUD或者SEI当成一帧
|
|
|
|
|
case H265Frame::NAL_AUD:
|
|
|
|
|
case H265Frame::NAL_SEI_SUFFIX:
|
|
|
|
|
case H265Frame::NAL_SEI_PREFIX: return true;
|
|
|
|
|
default: return false;
|
2021-06-18 15:19:21 +08:00
|
|
|
|
}
|
2021-06-28 10:35:08 +08:00
|
|
|
|
}
|
2021-07-06 23:37:04 +08:00
|
|
|
|
|
|
|
|
|
default: return false;
|
2021-06-28 10:35:08 +08:00
|
|
|
|
}
|
2021-06-18 15:19:21 +08:00
|
|
|
|
}
|
2021-06-28 10:35:08 +08:00
|
|
|
|
|
2021-07-07 11:02:05 +08:00
|
|
|
|
bool FrameMerger::frameCacheHasVCL() const {
|
|
|
|
|
bool has_vcl = false;
|
|
|
|
|
bool is_h264_or_h265 = false;
|
|
|
|
|
_frameCached.for_each([&](const Frame::Ptr &frame) {
|
2021-06-19 01:27:13 +08:00
|
|
|
|
switch (frame->getCodecId()) {
|
2021-06-28 10:35:08 +08:00
|
|
|
|
case CodecH264: {
|
|
|
|
|
auto type = H264_TYPE(frame->data()[frame->prefixSize()]);
|
2021-07-07 11:02:05 +08:00
|
|
|
|
//有编码数据
|
|
|
|
|
has_vcl = type >= H264Frame::NAL_B_P && type <= H264Frame::NAL_IDR;
|
|
|
|
|
is_h264_or_h265 = true;
|
2021-06-28 10:35:08 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case CodecH265: {
|
|
|
|
|
auto type = H265_TYPE(frame->data()[frame->prefixSize()]);
|
2021-07-07 11:02:05 +08:00
|
|
|
|
//有编码数据
|
|
|
|
|
has_vcl = type >= H265Frame::NAL_TRAIL_R && type <= H265Frame::NAL_RSV_IRAP_VCL23;
|
|
|
|
|
is_h264_or_h265 = true;
|
2021-06-28 10:35:08 +08:00
|
|
|
|
break;
|
2021-06-19 01:27:13 +08:00
|
|
|
|
}
|
2021-07-07 11:02:05 +08:00
|
|
|
|
default: break;
|
2021-06-28 10:35:08 +08:00
|
|
|
|
}
|
2021-06-19 01:27:13 +08:00
|
|
|
|
});
|
2021-07-07 11:02:05 +08:00
|
|
|
|
if (is_h264_or_h265) {
|
|
|
|
|
return has_vcl;
|
2021-06-19 01:27:13 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-06-28 10:35:08 +08:00
|
|
|
|
|
2021-04-26 18:26:07 +08:00
|
|
|
|
void FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb) {
|
2021-07-07 11:02:05 +08:00
|
|
|
|
if (willFlush(frame)) {
|
2021-04-26 18:26:07 +08:00
|
|
|
|
Frame::Ptr back = _frameCached.back();
|
|
|
|
|
Buffer::Ptr merged_frame = back;
|
|
|
|
|
bool have_idr = back->keyFrame();
|
|
|
|
|
|
|
|
|
|
if (_frameCached.size() != 1 || _type == mp4_nal_size) {
|
|
|
|
|
//在MP4模式下,一帧数据也需要在前添加nalu_size
|
|
|
|
|
BufferLikeString merged;
|
|
|
|
|
merged.reserve(back->size() + 1024);
|
|
|
|
|
_frameCached.for_each([&](const Frame::Ptr &frame) {
|
|
|
|
|
doMerge(merged, frame);
|
|
|
|
|
if (frame->keyFrame()) {
|
|
|
|
|
have_idr = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
merged_frame = std::make_shared<BufferOffset<BufferLikeString> >(std::move(merged));
|
|
|
|
|
}
|
|
|
|
|
cb(back->dts(), back->pts(), merged_frame, have_idr);
|
|
|
|
|
_frameCached.clear();
|
|
|
|
|
}
|
2021-07-07 11:02:05 +08:00
|
|
|
|
|
|
|
|
|
switch (_type) {
|
|
|
|
|
case h264_prefix:
|
|
|
|
|
case mp4_nal_size: {
|
|
|
|
|
//h264头和mp4头模式过滤无效的帧
|
|
|
|
|
if (shouldDrop(frame)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 18:26:07 +08:00
|
|
|
|
_frameCached.emplace_back(Frame::getCacheAbleFrame(frame));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FrameMerger::FrameMerger(int type) {
|
|
|
|
|
_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameMerger::clear() {
|
|
|
|
|
_frameCached.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
}//namespace mediakit
|