Commit ec7fe5cc by ziyue

整理frame相关代码,完善FrameStamp重载函数

parent 7b3968d9
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "Network/Socket.h" #include "Network/Socket.h"
#include "Common/Stamp.h" #include "Common/Stamp.h"
namespace mediakit{ namespace mediakit {
typedef enum { typedef enum {
TrackInvalid = -1, TrackInvalid = -1,
...@@ -82,8 +82,8 @@ class CodecInfo { ...@@ -82,8 +82,8 @@ class CodecInfo {
public: public:
typedef std::shared_ptr<CodecInfo> Ptr; typedef std::shared_ptr<CodecInfo> Ptr;
CodecInfo(){} CodecInfo() = default;
virtual ~CodecInfo(){} virtual ~CodecInfo() = default;
/** /**
* 获取编解码器类型 * 获取编解码器类型
...@@ -106,8 +106,8 @@ public: ...@@ -106,8 +106,8 @@ public:
*/ */
class Frame : public toolkit::Buffer, public CodecInfo { class Frame : public toolkit::Buffer, public CodecInfo {
public: public:
typedef std::shared_ptr<Frame> Ptr; using Ptr = std::shared_ptr<Frame>;
virtual ~Frame(){} virtual ~Frame() = default;
/** /**
* 返回解码时间戳,单位毫秒 * 返回解码时间戳,单位毫秒
...@@ -117,9 +117,7 @@ public: ...@@ -117,9 +117,7 @@ public:
/** /**
* 返回显示时间戳,单位毫秒 * 返回显示时间戳,单位毫秒
*/ */
virtual uint32_t pts() const { virtual uint32_t pts() const { return dts(); }
return dts();
}
/** /**
* 前缀长度,譬如264前缀为0x00 00 00 01,那么前缀长度就是4 * 前缀长度,譬如264前缀为0x00 00 00 01,那么前缀长度就是4
...@@ -176,7 +174,7 @@ class FrameImp : public Frame { ...@@ -176,7 +174,7 @@ class FrameImp : public Frame {
public: public:
using Ptr = std::shared_ptr<FrameImp>; using Ptr = std::shared_ptr<FrameImp>;
template<typename C=FrameImp> template <typename C = FrameImp>
static std::shared_ptr<C> create() { static std::shared_ptr<C> create() {
#if 0 #if 0
static ResourcePool<C> packet_pool; static ResourcePool<C> packet_pool;
...@@ -194,37 +192,14 @@ public: ...@@ -194,37 +192,14 @@ public:
#endif #endif
} }
char *data() const override{ char *data() const override { return (char *)_buffer.data(); }
return (char *)_buffer.data(); size_t size() const override { return _buffer.size(); }
} uint32_t dts() const override { return _dts; }
uint32_t pts() const override { return _pts ? _pts : _dts; }
size_t size() const override { size_t prefixSize() const override { return _prefix_size; }
return _buffer.size(); CodecId getCodecId() const override { return _codec_id; }
} bool keyFrame() const override { return false; }
bool configFrame() const override { return false; }
uint32_t dts() const override {
return _dts;
}
uint32_t pts() const override{
return _pts ? _pts : _dts;
}
size_t prefixSize() const override{
return _prefix_size;
}
CodecId getCodecId() const override{
return _codec_id;
}
bool keyFrame() const override {
return false;
}
bool configFrame() const override{
return false;
}
public: public:
CodecId _codec_id = CodecInvalid; CodecId _codec_id = CodecInvalid;
...@@ -248,17 +223,16 @@ protected: ...@@ -248,17 +223,16 @@ protected:
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame * 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能 * 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
*/ */
template<typename Parent> template <typename Parent>
class FrameInternal : public Parent{ class FrameInternal : public Parent {
public: public:
typedef std::shared_ptr<FrameInternal> Ptr; typedef std::shared_ptr<FrameInternal> Ptr;
FrameInternal(const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size) 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(ptr, size, parent_frame->dts(), parent_frame->pts(), prefix_size) {
_parent_frame = parent_frame; _parent_frame = parent_frame;
} }
bool cacheAble() const override { bool cacheAble() const override { return _parent_frame->cacheAble(); }
return _parent_frame->cacheAble();
}
private: private:
Frame::Ptr _parent_frame; Frame::Ptr _parent_frame;
}; };
...@@ -269,17 +243,17 @@ private: ...@@ -269,17 +243,17 @@ private:
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame * 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能 * 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
*/ */
template<typename Parent> template <typename Parent>
class FrameTSInternal : public Parent{ class FrameTSInternal : public Parent {
public: public:
typedef std::shared_ptr<FrameTSInternal> Ptr; typedef std::shared_ptr<FrameTSInternal> Ptr;
FrameTSInternal(const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size,uint32_t dts,uint32_t pts) FrameTSInternal(
: Parent(ptr, size, dts, pts, prefix_size) { const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size, uint32_t dts, uint32_t pts)
: Parent(ptr, size, dts, pts, prefix_size) {
_parent_frame = parent_frame; _parent_frame = parent_frame;
} }
bool cacheAble() const override { bool cacheAble() const override { return _parent_frame->cacheAble(); }
return _parent_frame->cacheAble();
}
private: private:
Frame::Ptr _parent_frame; Frame::Ptr _parent_frame;
}; };
...@@ -290,8 +264,8 @@ private: ...@@ -290,8 +264,8 @@ private:
class FrameWriterInterface { class FrameWriterInterface {
public: public:
typedef std::shared_ptr<FrameWriterInterface> Ptr; typedef std::shared_ptr<FrameWriterInterface> Ptr;
FrameWriterInterface(){} FrameWriterInterface() = default;
virtual ~FrameWriterInterface(){} virtual ~FrameWriterInterface() = default;
/** /**
* 写入帧数据 * 写入帧数据
...@@ -310,18 +284,13 @@ public: ...@@ -310,18 +284,13 @@ public:
/** /**
* inputFrame后触发onWriteFrame回调 * inputFrame后触发onWriteFrame回调
*/ */
FrameWriterInterfaceHelper(const onWriteFrame& cb){ FrameWriterInterfaceHelper(const onWriteFrame &cb) { _writeCallback = cb; }
_writeCallback = cb; virtual ~FrameWriterInterfaceHelper() = default;
}
virtual ~FrameWriterInterfaceHelper(){}
/** /**
* 写入帧数据 * 写入帧数据
*/ */
bool inputFrame(const Frame::Ptr &frame) override { bool inputFrame(const Frame::Ptr &frame) override { return _writeCallback(frame); }
return _writeCallback(frame);
}
private: private:
onWriteFrame _writeCallback; onWriteFrame _writeCallback;
...@@ -387,16 +356,18 @@ private: ...@@ -387,16 +356,18 @@ private:
/** /**
* 通过Frame接口包装指针,方便使用者把自己的数据快速接入ZLMediaKit * 通过Frame接口包装指针,方便使用者把自己的数据快速接入ZLMediaKit
*/ */
class FrameFromPtr : public Frame{ class FrameFromPtr : public Frame {
public: public:
typedef std::shared_ptr<FrameFromPtr> Ptr; typedef std::shared_ptr<FrameFromPtr> Ptr;
FrameFromPtr(CodecId codec_id, char *ptr, size_t size, uint32_t dts, uint32_t pts = 0, size_t prefix_size = 0,bool is_key = false ) FrameFromPtr(
: FrameFromPtr(ptr, size, dts, pts, prefix_size,is_key) { CodecId codec_id, char *ptr, size_t size, uint32_t dts, uint32_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; _codec_id = codec_id;
} }
FrameFromPtr(char *ptr, size_t size, uint32_t dts, uint32_t pts = 0, size_t prefix_size = 0,bool is_key = false){ FrameFromPtr(char *ptr, size_t size, uint32_t dts, uint32_t pts = 0, size_t prefix_size = 0, bool is_key = false) {
_ptr = ptr; _ptr = ptr;
_size = size; _size = size;
_dts = dts; _dts = dts;
...@@ -405,29 +376,15 @@ public: ...@@ -405,29 +376,15 @@ public:
_is_key = is_key; _is_key = is_key;
} }
char *data() const override{ char *data() const override { return _ptr; }
return _ptr; size_t size() const override { return _size; }
} uint32_t dts() const override { return _dts; }
uint32_t pts() const override { return _pts ? _pts : dts(); }
size_t size() const override { size_t prefixSize() const override { return _prefix_size; }
return _size; bool cacheAble() const override { return false; }
} bool keyFrame() const override { return _is_key; }
bool configFrame() const override { return false; }
uint32_t dts() const override { void setCodecId(CodecId codec_id) { _codec_id = codec_id; }
return _dts;
}
uint32_t pts() const override{
return _pts ? _pts : dts();
}
size_t prefixSize() const override{
return _prefix_size;
}
bool cacheAble() const override {
return false;
}
CodecId getCodecId() const override { CodecId getCodecId() const override {
if (_codec_id == CodecInvalid) { if (_codec_id == CodecInvalid) {
...@@ -436,29 +393,17 @@ public: ...@@ -436,29 +393,17 @@ public:
return _codec_id; return _codec_id;
} }
void setCodecId(CodecId codec_id) {
_codec_id = codec_id;
}
bool keyFrame() const override {
return _is_key;
}
bool configFrame() const override{
return false;
}
protected: protected:
FrameFromPtr() {} FrameFromPtr() = default;
protected: protected:
bool _is_key;
char *_ptr; char *_ptr;
uint32_t _dts; uint32_t _dts;
uint32_t _pts = 0; uint32_t _pts = 0;
size_t _size; size_t _size;
size_t _prefix_size; size_t _prefix_size;
CodecId _codec_id = CodecInvalid; CodecId _codec_id = CodecInvalid;
bool _is_key;
}; };
/** /**
...@@ -493,25 +438,11 @@ public: ...@@ -493,25 +438,11 @@ public:
/** /**
* 可以被缓存 * 可以被缓存
*/ */
bool cacheAble() const override { bool cacheAble() const override { return true; }
return true; bool keyFrame() const override { return _key; }
} bool configFrame() const override { return _config; }
bool dropAble() const override { return _drop_able; }
bool keyFrame() const override{ bool decodeAble() const override { return _decode_able; }
return _key;
}
bool configFrame() const override{
return _config;
}
bool dropAble() const override {
return _drop_able;
}
bool decodeAble() const override {
return _decode_able;
}
private: private:
bool _key; bool _key;
...@@ -523,51 +454,28 @@ private: ...@@ -523,51 +454,28 @@ private:
}; };
//该类实现frame级别的时间戳覆盖 //该类实现frame级别的时间戳覆盖
class FrameStamp : public Frame{ class FrameStamp : public Frame {
public: public:
typedef std::shared_ptr<FrameStamp> Ptr; using Ptr = std::shared_ptr<FrameStamp>;
FrameStamp(const Frame::Ptr &frame, Stamp &stamp,bool modify_stamp){ FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp) {
_frame = frame; _frame = std::move(frame);
//覆盖时间戳 //覆盖时间戳
stamp.revise(frame->dts(), frame->pts(), _dts, _pts, modify_stamp); stamp.revise(frame->dts(), frame->pts(), _dts, _pts, modify_stamp);
} }
~FrameStamp() override {} ~FrameStamp() override {}
uint32_t dts() const override{ uint32_t dts() const override { return (uint32_t)_dts; }
return (uint32_t)_dts; uint32_t pts() const override { return (uint32_t)_pts; }
} size_t prefixSize() const override { return _frame->prefixSize(); }
bool keyFrame() const override { return _frame->keyFrame(); }
uint32_t pts() const override{ bool configFrame() const override { return _frame->configFrame(); }
return (uint32_t)_pts; bool cacheAble() const override { return _frame->cacheAble(); }
} bool dropAble() const override { return _frame->dropAble(); }
bool decodeAble() const override { return _frame->decodeAble(); }
size_t prefixSize() const override { char *data() const override { return _frame->data(); }
return _frame->prefixSize(); size_t size() const override { return _frame->size(); }
} CodecId getCodecId() const override { return _frame->getCodecId(); }
bool keyFrame() const override {
return _frame->keyFrame();
}
bool configFrame() const override {
return _frame->configFrame();
}
bool cacheAble() const override {
return _frame->cacheAble();
}
char *data() const override {
return _frame->data();
}
size_t size() const override {
return _frame->size();
}
CodecId getCodecId() const override {
return _frame->getCodecId();
}
private: private:
int64_t _dts; int64_t _dts;
int64_t _pts; int64_t _pts;
...@@ -578,7 +486,7 @@ private: ...@@ -578,7 +486,7 @@ private:
* 该对象可以把Buffer对象转换成可缓存的Frame对象 * 该对象可以把Buffer对象转换成可缓存的Frame对象
*/ */
template <typename Parent> template <typename Parent>
class FrameWrapper : public Parent{ class FrameWrapper : public Parent {
public: public:
~FrameWrapper() = default; ~FrameWrapper() = default;
...@@ -590,8 +498,9 @@ public: ...@@ -590,8 +498,9 @@ public:
* @param prefix 帧前缀长度 * @param prefix 帧前缀长度
* @param offset buffer有效数据偏移量 * @param offset buffer有效数据偏移量
*/ */
FrameWrapper(const toolkit::Buffer::Ptr &buf, uint32_t dts, uint32_t pts, size_t prefix, size_t offset) : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix){ FrameWrapper(toolkit::Buffer::Ptr buf, uint32_t dts, uint32_t pts, size_t prefix, size_t offset)
_buf = buf; : Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
_buf = std::move(buf);
} }
/** /**
...@@ -603,16 +512,15 @@ public: ...@@ -603,16 +512,15 @@ public:
* @param offset buffer有效数据偏移量 * @param offset buffer有效数据偏移量
* @param codec 帧类型 * @param codec 帧类型
*/ */
FrameWrapper(const toolkit::Buffer::Ptr &buf, uint32_t dts, uint32_t pts, size_t prefix, size_t offset, CodecId codec) : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix){ FrameWrapper(toolkit::Buffer::Ptr buf, uint32_t dts, uint32_t pts, size_t prefix, size_t offset, CodecId codec)
_buf = buf; : Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
_buf = std::move(buf);
} }
/** /**
* 该帧可缓存 * 该帧可缓存
*/ */
bool cacheAble() const override { bool cacheAble() const override { return true; }
return true;
}
private: private:
toolkit::Buffer::Ptr _buf; toolkit::Buffer::Ptr _buf;
...@@ -647,5 +555,5 @@ private: ...@@ -647,5 +555,5 @@ private:
toolkit::List<Frame::Ptr> _frame_cache; toolkit::List<Frame::Ptr> _frame_cache;
}; };
}//namespace mediakit } // namespace mediakit
#endif //ZLMEDIAKIT_FRAME_H #endif // ZLMEDIAKIT_FRAME_H
\ No newline at end of file \ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论