mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-20 23:32:22 +08:00
release 8.0
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
/*
|
||||
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
||||
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
||||
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
||||
*
|
||||
* Use of this source code is governed by MIT license that can be found in the
|
||||
* Use of this source code is governed by MIT-like 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.
|
||||
*/
|
||||
@@ -20,7 +20,9 @@
|
||||
#include "Network/Buffer.h"
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
class Stamp;
|
||||
|
||||
typedef enum {
|
||||
TrackInvalid = -1,
|
||||
TrackVideo = 0,
|
||||
@@ -31,21 +33,21 @@ typedef enum {
|
||||
} TrackType;
|
||||
|
||||
#define CODEC_MAP(XX) \
|
||||
XX(CodecH264, TrackVideo, 0, "H264", PSI_STREAM_H264) \
|
||||
XX(CodecH265, TrackVideo, 1, "H265", PSI_STREAM_H265) \
|
||||
XX(CodecAAC, TrackAudio, 2, "mpeg4-generic", PSI_STREAM_AAC) \
|
||||
XX(CodecG711A, TrackAudio, 3, "PCMA", PSI_STREAM_AUDIO_G711A) \
|
||||
XX(CodecG711U, TrackAudio, 4, "PCMU", PSI_STREAM_AUDIO_G711U) \
|
||||
XX(CodecOpus, TrackAudio, 5, "opus", PSI_STREAM_AUDIO_OPUS) \
|
||||
XX(CodecL16, TrackAudio, 6, "L16", PSI_STREAM_RESERVED) \
|
||||
XX(CodecVP8, TrackVideo, 7, "VP8", PSI_STREAM_VP8) \
|
||||
XX(CodecVP9, TrackVideo, 8, "VP9", PSI_STREAM_VP9) \
|
||||
XX(CodecAV1, TrackVideo, 9, "AV1", PSI_STREAM_AV1) \
|
||||
XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_RESERVED)
|
||||
XX(CodecH264, TrackVideo, 0, "H264", PSI_STREAM_H264, MOV_OBJECT_H264) \
|
||||
XX(CodecH265, TrackVideo, 1, "H265", PSI_STREAM_H265, MOV_OBJECT_HEVC) \
|
||||
XX(CodecAAC, TrackAudio, 2, "mpeg4-generic", PSI_STREAM_AAC, MOV_OBJECT_AAC) \
|
||||
XX(CodecG711A, TrackAudio, 3, "PCMA", PSI_STREAM_AUDIO_G711A, MOV_OBJECT_G711a) \
|
||||
XX(CodecG711U, TrackAudio, 4, "PCMU", PSI_STREAM_AUDIO_G711U, MOV_OBJECT_G711u) \
|
||||
XX(CodecOpus, TrackAudio, 5, "opus", PSI_STREAM_AUDIO_OPUS, MOV_OBJECT_OPUS) \
|
||||
XX(CodecL16, TrackAudio, 6, "L16", PSI_STREAM_RESERVED, MOV_OBJECT_NONE) \
|
||||
XX(CodecVP8, TrackVideo, 7, "VP8", PSI_STREAM_VP8, MOV_OBJECT_VP8) \
|
||||
XX(CodecVP9, TrackVideo, 8, "VP9", PSI_STREAM_VP9, MOV_OBJECT_VP9) \
|
||||
XX(CodecAV1, TrackVideo, 9, "AV1", PSI_STREAM_AV1, MOV_OBJECT_AV1) \
|
||||
XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_JPEG_2000, MOV_OBJECT_JPEG)
|
||||
|
||||
typedef enum {
|
||||
CodecInvalid = -1,
|
||||
#define XX(name, type, value, str, mpeg_id) name = value,
|
||||
#define XX(name, type, value, str, mpeg_id, mp4_id) name = value,
|
||||
CODEC_MAP(XX)
|
||||
#undef XX
|
||||
CodecMax
|
||||
@@ -78,6 +80,26 @@ const char *getCodecName(CodecId codecId);
|
||||
*/
|
||||
TrackType getTrackType(CodecId codecId);
|
||||
|
||||
/**
|
||||
* 根据codecid获取mov object id
|
||||
*/
|
||||
int getMovIdByCodec(CodecId codecId);
|
||||
|
||||
/**
|
||||
* 根据mov object id获取CodecId
|
||||
*/
|
||||
CodecId getCodecByMovId(int object_id);
|
||||
|
||||
/**
|
||||
* 根据codecid获取mpeg id
|
||||
*/
|
||||
int getMpegIdByCodec(CodecId codec);
|
||||
|
||||
/**
|
||||
* 根据mpeg id获取CodecId
|
||||
*/
|
||||
CodecId getCodecByMpegId(int mpeg_id);
|
||||
|
||||
/**
|
||||
* 编码信息的抽象接口
|
||||
*/
|
||||
@@ -85,7 +107,6 @@ class CodecInfo {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<CodecInfo>;
|
||||
|
||||
CodecInfo() = default;
|
||||
virtual ~CodecInfo() = default;
|
||||
|
||||
/**
|
||||
@@ -102,6 +123,7 @@ public:
|
||||
* 获取音视频类型
|
||||
*/
|
||||
TrackType getTrackType() const;
|
||||
std::string getTrackTypeStr() const;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -110,7 +132,6 @@ public:
|
||||
class Frame : public toolkit::Buffer, public CodecInfo {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<Frame>;
|
||||
virtual ~Frame() = default;
|
||||
|
||||
/**
|
||||
* 返回解码时间戳,单位毫秒
|
||||
@@ -220,24 +241,52 @@ protected:
|
||||
FrameImp() = default;
|
||||
};
|
||||
|
||||
/**
|
||||
* 一个Frame类中可以有多个帧,他们通过 0x 00 00 01 分隔
|
||||
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
|
||||
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
|
||||
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
|
||||
*/
|
||||
template <typename Parent>
|
||||
class FrameInternal : public Parent {
|
||||
// 包装一个指针成不可缓存的frame
|
||||
class FrameFromPtr : public Frame {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameInternal>;
|
||||
FrameInternal(const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size)
|
||||
: Parent(ptr, size, parent_frame->dts(), parent_frame->pts(), prefix_size) {
|
||||
_parent_frame = parent_frame;
|
||||
}
|
||||
bool cacheAble() const override { return _parent_frame->cacheAble(); }
|
||||
using Ptr = std::shared_ptr<FrameFromPtr>;
|
||||
|
||||
private:
|
||||
Frame::Ptr _parent_frame;
|
||||
FrameFromPtr(CodecId codec_id, char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0, bool is_key = false)
|
||||
: FrameFromPtr(ptr, size, dts, pts, prefix_size, is_key) {
|
||||
_codec_id = codec_id;
|
||||
}
|
||||
|
||||
char *data() const override { return _ptr; }
|
||||
size_t size() const override { return _size; }
|
||||
uint64_t dts() const override { return _dts; }
|
||||
uint64_t pts() const override { return _pts ? _pts : dts(); }
|
||||
size_t prefixSize() const override { return _prefix_size; }
|
||||
bool cacheAble() const override { return false; }
|
||||
bool keyFrame() const override { return _is_key; }
|
||||
bool configFrame() const override { return false; }
|
||||
|
||||
CodecId getCodecId() const override {
|
||||
if (_codec_id == CodecInvalid) {
|
||||
throw std::invalid_argument("Invalid codec type of FrameFromPtr");
|
||||
}
|
||||
return _codec_id;
|
||||
}
|
||||
|
||||
protected:
|
||||
FrameFromPtr() = default;
|
||||
|
||||
FrameFromPtr(char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0, bool is_key = false) {
|
||||
_ptr = ptr;
|
||||
_size = size;
|
||||
_dts = dts;
|
||||
_pts = pts;
|
||||
_prefix_size = prefix_size;
|
||||
_is_key = is_key;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool _is_key;
|
||||
char *_ptr;
|
||||
uint64_t _dts;
|
||||
uint64_t _pts = 0;
|
||||
size_t _size;
|
||||
size_t _prefix_size;
|
||||
CodecId _codec_id = CodecInvalid;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -247,27 +296,210 @@ private:
|
||||
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
|
||||
*/
|
||||
template <typename Parent>
|
||||
class FrameTSInternal : public Parent {
|
||||
class FrameInternalBase : public Parent {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameTSInternal>;
|
||||
FrameTSInternal(
|
||||
const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size, uint64_t dts, uint64_t pts)
|
||||
: Parent(ptr, size, dts, pts, prefix_size) {
|
||||
_parent_frame = parent_frame;
|
||||
using Ptr = std::shared_ptr<FrameInternalBase>;
|
||||
FrameInternalBase(Frame::Ptr parent_frame, char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0)
|
||||
: Parent(parent_frame->getCodecId(), ptr, size, dts, pts, prefix_size) {
|
||||
_parent_frame = std::move(parent_frame);
|
||||
}
|
||||
|
||||
bool cacheAble() const override { return _parent_frame->cacheAble(); }
|
||||
|
||||
private:
|
||||
Frame::Ptr _parent_frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 一个Frame类中可以有多个帧,他们通过 0x 00 00 01 分隔
|
||||
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
|
||||
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
|
||||
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
|
||||
*/
|
||||
template <typename Parent>
|
||||
class FrameInternal : public FrameInternalBase<Parent> {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameInternal>;
|
||||
FrameInternal(const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size)
|
||||
: FrameInternalBase<Parent>(parent_frame, ptr, size, parent_frame->dts(), parent_frame->pts(), prefix_size) {}
|
||||
};
|
||||
|
||||
// 管理一个指针生命周期并生产一个frame
|
||||
class FrameAutoDelete : public FrameFromPtr {
|
||||
public:
|
||||
template <typename... ARGS>
|
||||
FrameAutoDelete(ARGS &&...args) : FrameFromPtr(std::forward<ARGS>(args)...) {}
|
||||
|
||||
~FrameAutoDelete() override { delete[] _ptr; };
|
||||
|
||||
bool cacheAble() const override { return true; }
|
||||
};
|
||||
|
||||
// 把一个不可缓存的frame声明为可缓存的
|
||||
template <typename Parent>
|
||||
class FrameToCache : public Parent {
|
||||
public:
|
||||
template<typename ... ARGS>
|
||||
FrameToCache(ARGS &&...args) : Parent(std::forward<ARGS>(args)...) {};
|
||||
|
||||
bool cacheAble() const override {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
|
||||
class FrameCacheAble : public FrameFromPtr {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameCacheAble>;
|
||||
|
||||
FrameCacheAble(const Frame::Ptr &frame, bool force_key_frame = false, toolkit::Buffer::Ptr buf = nullptr) {
|
||||
if (frame->cacheAble()) {
|
||||
_ptr = frame->data();
|
||||
_buffer = frame;
|
||||
} else if (buf) {
|
||||
_ptr = frame->data();
|
||||
_buffer = std::move(buf);
|
||||
} else {
|
||||
auto buffer = std::make_shared<toolkit::BufferLikeString>();
|
||||
buffer->assign(frame->data(), frame->size());
|
||||
_ptr = buffer->data();
|
||||
_buffer = std::move(buffer);
|
||||
}
|
||||
_size = frame->size();
|
||||
_dts = frame->dts();
|
||||
_pts = frame->pts();
|
||||
_prefix_size = frame->prefixSize();
|
||||
_codec_id = frame->getCodecId();
|
||||
_key = force_key_frame ? true : frame->keyFrame();
|
||||
_config = frame->configFrame();
|
||||
_drop_able = frame->dropAble();
|
||||
_decode_able = frame->decodeAble();
|
||||
}
|
||||
|
||||
/**
|
||||
* 可以被缓存
|
||||
*/
|
||||
bool cacheAble() const override { return true; }
|
||||
bool keyFrame() const override { return _key; }
|
||||
bool configFrame() const override { return _config; }
|
||||
bool dropAble() const override { return _drop_able; }
|
||||
bool decodeAble() const override { return _decode_able; }
|
||||
|
||||
private:
|
||||
bool _key;
|
||||
bool _config;
|
||||
bool _drop_able;
|
||||
bool _decode_able;
|
||||
toolkit::Buffer::Ptr _buffer;
|
||||
};
|
||||
|
||||
//该类实现frame级别的时间戳覆盖
|
||||
class FrameStamp : public Frame {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameStamp>;
|
||||
FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp);
|
||||
~FrameStamp() override {}
|
||||
|
||||
uint64_t dts() const override { return (uint64_t)_dts; }
|
||||
uint64_t pts() const override { return (uint64_t)_pts; }
|
||||
size_t prefixSize() const override { return _frame->prefixSize(); }
|
||||
bool keyFrame() const override { return _frame->keyFrame(); }
|
||||
bool configFrame() const override { return _frame->configFrame(); }
|
||||
bool cacheAble() const override { return _frame->cacheAble(); }
|
||||
bool dropAble() const override { return _frame->dropAble(); }
|
||||
bool decodeAble() const override { return _frame->decodeAble(); }
|
||||
char *data() const override { return _frame->data(); }
|
||||
size_t size() const override { return _frame->size(); }
|
||||
CodecId getCodecId() const override { return _frame->getCodecId(); }
|
||||
|
||||
private:
|
||||
int64_t _dts;
|
||||
int64_t _pts;
|
||||
Frame::Ptr _frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 该对象可以把Buffer对象转换成可缓存的Frame对象
|
||||
*/
|
||||
template <typename Parent>
|
||||
class FrameFromBuffer : public Parent {
|
||||
public:
|
||||
/**
|
||||
* 构造frame
|
||||
* @param buf 数据缓存
|
||||
* @param dts 解码时间戳
|
||||
* @param pts 显示时间戳
|
||||
* @param prefix 帧前缀长度
|
||||
* @param offset buffer有效数据偏移量
|
||||
*/
|
||||
FrameFromBuffer(toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix = 0, size_t offset = 0)
|
||||
: Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
||||
_buf = std::move(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造frame
|
||||
* @param buf 数据缓存
|
||||
* @param dts 解码时间戳
|
||||
* @param pts 显示时间戳
|
||||
* @param prefix 帧前缀长度
|
||||
* @param offset buffer有效数据偏移量
|
||||
* @param codec 帧类型
|
||||
*/
|
||||
FrameFromBuffer(CodecId codec, toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix = 0, size_t offset = 0)
|
||||
: Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
||||
_buf = std::move(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该帧可缓存
|
||||
*/
|
||||
bool cacheAble() const override { return true; }
|
||||
|
||||
private:
|
||||
toolkit::Buffer::Ptr _buf;
|
||||
};
|
||||
|
||||
/**
|
||||
* 合并一些时间戳相同的frame
|
||||
*/
|
||||
class FrameMerger {
|
||||
public:
|
||||
using onOutput = std::function<void(uint64_t dts, uint64_t pts, const toolkit::Buffer::Ptr &buffer, bool have_key_frame)>;
|
||||
using Ptr = std::shared_ptr<FrameMerger>;
|
||||
enum {
|
||||
none = 0,
|
||||
h264_prefix,
|
||||
mp4_nal_size,
|
||||
};
|
||||
|
||||
FrameMerger(int type);
|
||||
|
||||
/**
|
||||
* 刷新输出缓冲,注意此时会调用FrameMerger::inputFrame传入的onOutput回调
|
||||
* 请注意回调捕获参数此时是否有效
|
||||
*/
|
||||
void flush();
|
||||
void clear();
|
||||
bool inputFrame(const Frame::Ptr &frame, onOutput cb, toolkit::BufferLikeString *buffer = nullptr);
|
||||
|
||||
private:
|
||||
bool willFlush(const Frame::Ptr &frame) const;
|
||||
void doMerge(toolkit::BufferLikeString &buffer, const Frame::Ptr &frame) const;
|
||||
|
||||
private:
|
||||
int _type;
|
||||
bool _have_decode_able_frame = false;
|
||||
onOutput _cb;
|
||||
toolkit::List<Frame::Ptr> _frame_cache;
|
||||
};
|
||||
|
||||
/**
|
||||
* 写帧接口的抽象接口类
|
||||
*/
|
||||
class FrameWriterInterface {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameWriterInterface>;
|
||||
FrameWriterInterface() = default;
|
||||
virtual ~FrameWriterInterface() = default;
|
||||
|
||||
/**
|
||||
@@ -287,8 +519,6 @@ public:
|
||||
class FrameDispatcher : public FrameWriterInterface {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameDispatcher>;
|
||||
FrameDispatcher() = default;
|
||||
~FrameDispatcher() override = default;
|
||||
|
||||
/**
|
||||
* 添加代理
|
||||
@@ -397,209 +627,5 @@ private:
|
||||
std::map<void *, FrameWriterInterface::Ptr> _delegates;
|
||||
};
|
||||
|
||||
/**
|
||||
* 通过Frame接口包装指针,方便使用者把自己的数据快速接入ZLMediaKit
|
||||
*/
|
||||
class FrameFromPtr : public Frame {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameFromPtr>;
|
||||
|
||||
FrameFromPtr(
|
||||
CodecId codec_id, char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0,
|
||||
bool is_key = false)
|
||||
: FrameFromPtr(ptr, size, dts, pts, prefix_size, is_key) {
|
||||
_codec_id = codec_id;
|
||||
}
|
||||
|
||||
FrameFromPtr(char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0, bool is_key = false) {
|
||||
_ptr = ptr;
|
||||
_size = size;
|
||||
_dts = dts;
|
||||
_pts = pts;
|
||||
_prefix_size = prefix_size;
|
||||
_is_key = is_key;
|
||||
}
|
||||
|
||||
char *data() const override { return _ptr; }
|
||||
size_t size() const override { return _size; }
|
||||
uint64_t dts() const override { return _dts; }
|
||||
uint64_t pts() const override { return _pts ? _pts : dts(); }
|
||||
size_t prefixSize() const override { return _prefix_size; }
|
||||
bool cacheAble() const override { return false; }
|
||||
bool keyFrame() const override { return _is_key; }
|
||||
bool configFrame() const override { return false; }
|
||||
void setCodecId(CodecId codec_id) { _codec_id = codec_id; }
|
||||
|
||||
CodecId getCodecId() const override {
|
||||
if (_codec_id == CodecInvalid) {
|
||||
throw std::invalid_argument("FrameFromPtr对象未设置codec类型");
|
||||
}
|
||||
return _codec_id;
|
||||
}
|
||||
|
||||
protected:
|
||||
FrameFromPtr() = default;
|
||||
|
||||
protected:
|
||||
bool _is_key;
|
||||
char *_ptr;
|
||||
uint64_t _dts;
|
||||
uint64_t _pts = 0;
|
||||
size_t _size;
|
||||
size_t _prefix_size;
|
||||
CodecId _codec_id = CodecInvalid;
|
||||
};
|
||||
|
||||
/**
|
||||
* 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
|
||||
*/
|
||||
class FrameCacheAble : public FrameFromPtr {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameCacheAble>;
|
||||
|
||||
FrameCacheAble(const Frame::Ptr &frame, bool force_key_frame = false) {
|
||||
if (frame->cacheAble()) {
|
||||
_frame = frame;
|
||||
_ptr = frame->data();
|
||||
} else {
|
||||
_buffer = FrameImp::create();
|
||||
_buffer->_buffer.assign(frame->data(), frame->size());
|
||||
_ptr = _buffer->data();
|
||||
}
|
||||
_size = frame->size();
|
||||
_dts = frame->dts();
|
||||
_pts = frame->pts();
|
||||
_prefix_size = frame->prefixSize();
|
||||
_codec_id = frame->getCodecId();
|
||||
_key = force_key_frame ? true : frame->keyFrame();
|
||||
_config = frame->configFrame();
|
||||
_drop_able = frame->dropAble();
|
||||
_decode_able = frame->decodeAble();
|
||||
}
|
||||
|
||||
~FrameCacheAble() override = default;
|
||||
|
||||
/**
|
||||
* 可以被缓存
|
||||
*/
|
||||
bool cacheAble() const override { return true; }
|
||||
bool keyFrame() const override { return _key; }
|
||||
bool configFrame() const override { return _config; }
|
||||
bool dropAble() const override { return _drop_able; }
|
||||
bool decodeAble() const override { return _decode_able; }
|
||||
|
||||
private:
|
||||
bool _key;
|
||||
bool _config;
|
||||
bool _drop_able;
|
||||
bool _decode_able;
|
||||
Frame::Ptr _frame;
|
||||
FrameImp::Ptr _buffer;
|
||||
};
|
||||
|
||||
//该类实现frame级别的时间戳覆盖
|
||||
class FrameStamp : public Frame {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FrameStamp>;
|
||||
FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp);
|
||||
~FrameStamp() override {}
|
||||
|
||||
uint64_t dts() const override { return (uint64_t)_dts; }
|
||||
uint64_t pts() const override { return (uint64_t)_pts; }
|
||||
size_t prefixSize() const override { return _frame->prefixSize(); }
|
||||
bool keyFrame() const override { return _frame->keyFrame(); }
|
||||
bool configFrame() const override { return _frame->configFrame(); }
|
||||
bool cacheAble() const override { return _frame->cacheAble(); }
|
||||
bool dropAble() const override { return _frame->dropAble(); }
|
||||
bool decodeAble() const override { return _frame->decodeAble(); }
|
||||
char *data() const override { return _frame->data(); }
|
||||
size_t size() const override { return _frame->size(); }
|
||||
CodecId getCodecId() const override { return _frame->getCodecId(); }
|
||||
|
||||
private:
|
||||
int64_t _dts;
|
||||
int64_t _pts;
|
||||
Frame::Ptr _frame;
|
||||
};
|
||||
|
||||
/**
|
||||
* 该对象可以把Buffer对象转换成可缓存的Frame对象
|
||||
*/
|
||||
template <typename Parent>
|
||||
class FrameWrapper : public Parent {
|
||||
public:
|
||||
~FrameWrapper() = default;
|
||||
|
||||
/**
|
||||
* 构造frame
|
||||
* @param buf 数据缓存
|
||||
* @param dts 解码时间戳
|
||||
* @param pts 显示时间戳
|
||||
* @param prefix 帧前缀长度
|
||||
* @param offset buffer有效数据偏移量
|
||||
*/
|
||||
FrameWrapper(toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix, size_t offset)
|
||||
: Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
||||
_buf = std::move(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造frame
|
||||
* @param buf 数据缓存
|
||||
* @param dts 解码时间戳
|
||||
* @param pts 显示时间戳
|
||||
* @param prefix 帧前缀长度
|
||||
* @param offset buffer有效数据偏移量
|
||||
* @param codec 帧类型
|
||||
*/
|
||||
FrameWrapper(toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix, size_t offset, CodecId codec)
|
||||
: Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
||||
_buf = std::move(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该帧可缓存
|
||||
*/
|
||||
bool cacheAble() const override { return true; }
|
||||
|
||||
private:
|
||||
toolkit::Buffer::Ptr _buf;
|
||||
};
|
||||
|
||||
/**
|
||||
* 合并一些时间戳相同的frame
|
||||
*/
|
||||
class FrameMerger {
|
||||
public:
|
||||
using onOutput = std::function<void(uint64_t dts, uint64_t pts, const toolkit::Buffer::Ptr &buffer, bool have_key_frame)>;
|
||||
using Ptr = std::shared_ptr<FrameMerger>;
|
||||
enum {
|
||||
none = 0,
|
||||
h264_prefix,
|
||||
mp4_nal_size,
|
||||
};
|
||||
|
||||
FrameMerger(int type);
|
||||
~FrameMerger() = default;
|
||||
|
||||
/**
|
||||
* 刷新输出缓冲,注意此时会调用FrameMerger::inputFrame传入的onOutput回调
|
||||
* 请注意回调捕获参数此时是否有效
|
||||
*/
|
||||
void flush();
|
||||
void clear();
|
||||
bool inputFrame(const Frame::Ptr &frame, onOutput cb, toolkit::BufferLikeString *buffer = nullptr);
|
||||
|
||||
private:
|
||||
bool willFlush(const Frame::Ptr &frame) const;
|
||||
void doMerge(toolkit::BufferLikeString &buffer, const Frame::Ptr &frame) const;
|
||||
|
||||
private:
|
||||
int _type;
|
||||
bool _have_decode_able_frame = false;
|
||||
onOutput _cb;
|
||||
toolkit::List<Frame::Ptr> _frame_cache;
|
||||
};
|
||||
|
||||
} // namespace mediakit
|
||||
#endif // ZLMEDIAKIT_FRAME_H
|
||||
Reference in New Issue
Block a user