ZLMediaKit/src/Extension/Frame.h

339 lines
7.1 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/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.
2018-10-25 10:00:17 +08:00
*/
2018-10-18 23:48:00 +08:00
#ifndef ZLMEDIAKIT_FRAME_H
#define ZLMEDIAKIT_FRAME_H
2018-10-26 16:09:48 +08:00
#include <mutex>
#include <functional>
2018-10-23 11:09:21 +08:00
#include "Util/RingBuffer.h"
2018-10-18 23:48:00 +08:00
#include "Network/Socket.h"
2018-10-26 16:09:48 +08:00
using namespace std;
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2018-10-18 23:48:00 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit{
2018-10-18 23:48:00 +08:00
2018-10-23 11:09:21 +08:00
typedef enum {
CodecInvalid = -1,
CodecH264 = 0,
2018-10-30 14:59:42 +08:00
CodecH265,
CodecAAC,
CodecMax = 0x7FFF
2018-10-23 11:09:21 +08:00
} CodecId;
typedef enum {
TrackInvalid = -1,
TrackVideo = 0,
TrackAudio,
2018-10-25 22:57:59 +08:00
TrackTitle,
2019-12-03 12:32:57 +08:00
TrackMax = 3
2018-10-23 11:09:21 +08:00
} TrackType;
2018-10-27 22:54:16 +08:00
/**
*
*/
2018-10-23 11:09:21 +08:00
class CodecInfo {
public:
2018-10-23 18:39:17 +08:00
typedef std::shared_ptr<CodecInfo> Ptr;
2018-10-23 11:09:21 +08:00
CodecInfo(){}
virtual ~CodecInfo(){}
/**
*
*/
virtual TrackType getTrackType() const = 0;
/**
*
*/
virtual CodecId getCodecId() const = 0;
2020-03-08 21:19:20 +08:00
/**
*
* @return
*/
2020-03-09 16:04:34 +08:00
const char *getCodecName();
2018-10-23 11:09:21 +08:00
};
2018-10-27 22:54:16 +08:00
/**
*
*/
class Frame : public Buffer, public CodecInfo {
2018-10-18 23:48:00 +08:00
public:
typedef std::shared_ptr<Frame> Ptr;
virtual ~Frame(){}
2018-11-17 17:26:38 +08:00
/**
*
* @return
*/
virtual uint32_t dts() const = 0;
/**
*
* @return
*/
virtual uint32_t pts() const {
return dts();
}
2018-10-21 22:24:24 +08:00
/**
* 2640x00 00 00 01,4
* aac前缀则为7个字节
*/
2018-10-23 21:41:45 +08:00
virtual uint32_t prefixSize() const = 0;
/**
*
* @return
*/
virtual bool keyFrame() const = 0;
2019-07-24 18:02:55 +08:00
2019-08-01 18:49:04 +08:00
/**
* sps pps vps
* @return
*/
virtual bool configFrame() const = 0;
2019-07-24 18:02:55 +08:00
/**
*
*/
virtual bool cacheAble() const { return true; }
/**
* frame
* @return
*/
static Ptr getCacheAbleFrame(const Ptr &frame);
2018-10-18 23:48:00 +08:00
};
2018-10-27 22:54:16 +08:00
/**
*
* @tparam T
*/
2018-10-24 12:01:40 +08:00
template <typename T>
class ResourcePoolHelper{
public:
ResourcePoolHelper(int size = 8){
_pool.setSize(size);
}
virtual ~ResourcePoolHelper(){}
std::shared_ptr<T> obtainObj(){
return _pool.obtain();
}
private:
ResourcePool<T> _pool;
};
2018-10-27 22:54:16 +08:00
/**
*
*/
class FrameWriterInterface {
2018-10-26 16:09:48 +08:00
public:
typedef std::shared_ptr<FrameWriterInterface> Ptr;
2018-10-26 16:09:48 +08:00
FrameWriterInterface(){}
virtual ~FrameWriterInterface(){}
2018-10-26 16:09:48 +08:00
/**
*
* @param frame
*/
virtual void inputFrame(const Frame::Ptr &frame) = 0;
};
2018-10-27 22:54:16 +08:00
/**
* function
*/
class FrameWriterInterfaceHelper : public FrameWriterInterface {
public:
typedef std::shared_ptr<FrameWriterInterfaceHelper> Ptr;
typedef std::function<void(const Frame::Ptr &frame)> onWriteFrame;
2018-10-27 22:54:16 +08:00
/**
* inputFrame后触发onWriteFrame回调
* @param cb
*/
FrameWriterInterfaceHelper(const onWriteFrame& cb){
_writeCallback = cb;
}
virtual ~FrameWriterInterfaceHelper(){}
/**
*
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override {
_writeCallback(frame);
}
private:
onWriteFrame _writeCallback;
};
2018-10-27 22:54:16 +08:00
/**
*
*/
2019-12-25 20:07:42 +08:00
class FrameDispatcher : public FrameWriterInterface {
2018-10-23 22:16:54 +08:00
public:
2019-12-25 20:07:42 +08:00
typedef std::shared_ptr<FrameDispatcher> Ptr;
2018-10-23 22:16:54 +08:00
2019-12-25 20:07:42 +08:00
FrameDispatcher(){}
virtual ~FrameDispatcher(){}
2018-10-23 22:16:54 +08:00
void addDelegate(const FrameWriterInterface::Ptr &delegate){
2018-10-26 16:09:48 +08:00
lock_guard<mutex> lck(_mtx);
_delegateMap.emplace(delegate.get(),delegate);
2018-10-23 22:16:54 +08:00
}
2018-10-26 16:09:48 +08:00
void delDelegate(void *ptr){
lock_guard<mutex> lck(_mtx);
_delegateMap.erase(ptr);
2018-10-23 22:16:54 +08:00
}
/**
*
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override{
2018-10-26 16:09:48 +08:00
lock_guard<mutex> lck(_mtx);
for(auto &pr : _delegateMap){
pr.second->inputFrame(frame);
2018-10-23 22:16:54 +08:00
}
}
private:
2018-10-26 16:09:48 +08:00
mutex _mtx;
map<void *,FrameWriterInterface::Ptr> _delegateMap;
2018-10-23 22:16:54 +08:00
};
2019-07-25 09:38:16 +08:00
/**
* Frame接口包装指针便使ZLMediaKit
*/
class FrameFromPtr : public Frame{
2018-10-26 16:09:48 +08:00
public:
2019-07-25 09:38:16 +08:00
typedef std::shared_ptr<FrameFromPtr> Ptr;
2018-10-26 16:09:48 +08:00
char *data() const override{
2019-07-03 16:22:12 +08:00
return _ptr;
2018-10-26 16:09:48 +08:00
}
uint32_t size() const override {
2019-07-03 16:22:12 +08:00
return _size;
2018-10-26 16:09:48 +08:00
}
2019-07-03 16:22:12 +08:00
2018-11-17 17:26:38 +08:00
uint32_t dts() const override {
2019-07-03 16:22:12 +08:00
return _dts;
}
uint32_t pts() const override{
if(_pts){
return _pts;
}
return dts();
2018-10-26 16:09:48 +08:00
}
2019-07-03 16:22:12 +08:00
2018-10-26 16:09:48 +08:00
uint32_t prefixSize() const override{
2019-07-03 16:22:12 +08:00
return _prefixSize;
2018-10-26 16:09:48 +08:00
}
2019-07-25 09:38:16 +08:00
protected:
char *_ptr;
uint32_t _size;
uint32_t _dts;
uint32_t _pts = 0;
uint32_t _prefixSize;
};
/**
* DevChannel类中有用到
* 使
* ZLMediaKit是同步对帧数据进行使用和处理的
*
* Frame::getCacheAbleFrame方法拷贝一个可缓存的帧
*/
class FrameNoCacheAble : public FrameFromPtr{
public:
typedef std::shared_ptr<FrameNoCacheAble> Ptr;
2019-07-03 16:22:12 +08:00
2019-07-24 18:02:55 +08:00
/**
*
* @return
*/
bool cacheAble() const override {
return false;
}
2018-10-26 16:09:48 +08:00
};
2019-07-25 09:38:16 +08:00
/**
*
* @see FrameNoCacheAble
*/
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{
_buffer = std::make_shared<BufferRaw>();
_buffer->assign(frame->data(),frame->size());
_ptr = _buffer->data();
}
_size = frame->size();
_dts = frame->dts();
_pts = frame->pts();
_prefixSize = frame->prefixSize();
_trackType = frame->getTrackType();
_codec = frame->getCodecId();
_key = frame->keyFrame();
2019-08-01 18:49:04 +08:00
_config = frame->configFrame();
}
virtual ~FrameCacheAble() = default;
/**
*
* @return
*/
bool cacheAble() const override {
return true;
}
TrackType getTrackType() const override{
return _trackType;
}
CodecId getCodecId() const override{
return _codec;
}
bool keyFrame() const override{
return _key;
}
2019-08-01 18:49:04 +08:00
bool configFrame() const override{
return _config;
}
private:
Frame::Ptr _frame;
BufferRaw::Ptr _buffer;
TrackType _trackType;
CodecId _codec;
bool _key;
2019-08-01 18:49:04 +08:00
bool _config;
};
2018-10-26 16:09:48 +08:00
2018-10-24 17:17:55 +08:00
}//namespace mediakit
2018-10-18 23:48:00 +08:00
#endif //ZLMEDIAKIT_FRAME_H