ZLMediaKit/src/Extension/Frame.cpp

273 lines
7.9 KiB
C++
Raw Normal View History

/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
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.
*/
#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"
using namespace std;
using namespace toolkit;
2021-01-23 09:44:37 +08:00
namespace toolkit {
StatisticImp(mediakit::Frame);
StatisticImp(mediakit::FrameImp);
}
namespace mediakit{
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()) {
2020-05-11 22:33:10 +08:00
_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();
_drop_able = frame->dropAble();
_decode_able = frame->decodeAble();
2020-05-11 22:33:10 +08:00
}
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
bool dropAble() const override {
return _drop_able;
}
bool decodeAble() const override {
return _decode_able;
}
2020-05-11 22:33:10 +08:00
private:
bool _key;
bool _config;
bool _drop_able;
bool _decode_able;
Frame::Ptr _frame;
2021-02-05 11:51:16 +08:00
FrameImp::Ptr _buffer;
2020-05-11 22:33:10 +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) {
2021-12-28 21:04:53 +08:00
#define XX(name, type, value, str, mpeg_id) case name : return type;
2021-06-16 11:14:10 +08:00
CODEC_MAP(XX)
#undef XX
default : return TrackInvalid;
2020-05-11 22:33:10 +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-12-28 21:04:53 +08:00
#define XX(name, type, value, str, mpeg_id) case name : return str;
2021-06-16 11:14:10 +08:00
CODEC_MAP(XX)
#undef XX
default : return "invalid";
2021-03-30 10:59:15 +08:00
}
}
2021-12-28 21:04:53 +08:00
#define XX(name, type, value, str, mpeg_id) {str, name},
2021-06-16 11:14:10 +08:00
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";
}
}
const char *CodecInfo::getCodecName() const {
2020-05-15 18:08:54 +08:00
return mediakit::getCodecName(getCodecId());
}
TrackType CodecInfo::getTrackType() const {
2020-05-15 18:08:54 +08:00
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 (_frame_cache.empty()) {
//缓存为空
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;
}
//遇到新帧、或时间戳变化或缓存太多防止内存溢出则flush输出
return new_frame || _frame_cache.back()->dts() != frame->dts() || _frame_cache.size() > kMaxFrameCacheSize;
2021-04-26 18:26:07 +08:00
}
case mp4_nal_size:
case h264_prefix: {
if (!_have_decode_able_frame) {
//缓存中没有有效的能解码的帧所以这次不flush
return _frame_cache.size() > kMaxFrameCacheSize;
}
if (_frame_cache.back()->dts() != frame->dts() || frame->decodeAble() || frame->configFrame()) {
//时间戳变化了,或新的一帧或遇到config帧立即flush
2021-04-26 18:26:07 +08:00
return true;
}
return _frame_cache.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 : {
//此处是合并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
bool FrameMerger::inputFrame(const Frame::Ptr &frame, const onOutput &cb, BufferLikeString *buffer) {
if (willFlush(frame)) {
Frame::Ptr back = _frame_cache.back();
2021-04-26 18:26:07 +08:00
Buffer::Ptr merged_frame = back;
bool have_key_frame = back->keyFrame();
2021-04-26 18:26:07 +08:00
if (_frame_cache.size() != 1 || _type == mp4_nal_size || buffer) {
2021-04-26 18:26:07 +08:00
//在MP4模式下一帧数据也需要在前添加nalu_size
BufferLikeString tmp;
BufferLikeString &merged = buffer ? *buffer : tmp;
if (!buffer) {
tmp.reserve(back->size() + 1024);
}
_frame_cache.for_each([&](const Frame::Ptr &frame) {
2021-04-26 18:26:07 +08:00
doMerge(merged, frame);
if (frame->keyFrame()) {
have_key_frame = true;
2021-04-26 18:26:07 +08:00
}
});
merged_frame = std::make_shared<BufferOffset<BufferLikeString> >(buffer ? merged : std::move(merged));
2021-04-26 18:26:07 +08:00
}
cb(back->dts(), back->pts(), merged_frame, have_key_frame);
_frame_cache.clear();
_have_decode_able_frame = false;
2021-04-26 18:26:07 +08:00
}
switch (_type) {
case h264_prefix:
case mp4_nal_size: {
if (frame->dropAble()) {
//h264头和mp4头模式过滤无效的帧
return false;
}
break;
}
default: break;
}
if (frame->decodeAble()) {
_have_decode_able_frame = true;
}
_frame_cache.emplace_back(Frame::getCacheAbleFrame(frame));
return true;
2021-04-26 18:26:07 +08:00
}
FrameMerger::FrameMerger(int type) {
_type = type;
}
void FrameMerger::clear() {
_frame_cache.clear();
_have_decode_able_frame = false;
2021-04-26 18:26:07 +08:00
}
2020-05-11 22:33:10 +08:00
}//namespace mediakit