2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
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>
|
2018-10-27 22:40:44 +08:00
|
|
|
|
#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,
|
2018-10-26 17:46:31 +08:00
|
|
|
|
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,
|
2018-10-26 17:46:31 +08:00
|
|
|
|
TrackMax = 0x7FFF
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 帧类型的抽象接口
|
|
|
|
|
*/
|
2018-10-23 11:09:21 +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-10-21 22:24:24 +08:00
|
|
|
|
/**
|
2018-11-17 17:26:38 +08:00
|
|
|
|
* 时间戳,已经废弃,请使用dts() 、pts()接口
|
2018-10-21 22:24:24 +08:00
|
|
|
|
*/
|
2018-11-17 17:26:38 +08:00
|
|
|
|
inline uint32_t stamp() const {
|
|
|
|
|
return dts();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回解码时间戳,单位毫秒
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual uint32_t dts() const = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回显示时间戳,单位毫秒
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual uint32_t pts() const {
|
|
|
|
|
return dts();
|
|
|
|
|
}
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 前缀长度,譬如264前缀为0x00 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;
|
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
|
|
|
|
/**
|
|
|
|
|
* 写帧接口的抽闲接口
|
|
|
|
|
*/
|
2018-10-27 22:40:44 +08:00
|
|
|
|
class FrameWriterInterface {
|
2018-10-26 16:09:48 +08:00
|
|
|
|
public:
|
2018-10-27 22:40:44 +08:00
|
|
|
|
typedef std::shared_ptr<FrameWriterInterface> Ptr;
|
2018-10-26 16:09:48 +08:00
|
|
|
|
|
2018-10-27 22:40:44 +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,辅助类
|
|
|
|
|
*/
|
2018-10-27 22:40:44 +08:00
|
|
|
|
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
|
|
|
|
|
*/
|
2018-10-27 22:40:44 +08:00
|
|
|
|
FrameWriterInterfaceHelper(const onWriteFrame& cb){
|
|
|
|
|
_writeCallback = cb;
|
|
|
|
|
}
|
|
|
|
|
virtual ~FrameWriterInterfaceHelper(){}
|
|
|
|
|
/**
|
|
|
|
|
* 写入帧数据
|
|
|
|
|
* @param frame 帧
|
|
|
|
|
*/
|
|
|
|
|
void inputFrame(const Frame::Ptr &frame) override {
|
|
|
|
|
_writeCallback(frame);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
onWriteFrame _writeCallback;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2018-10-23 11:09:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* 帧环形缓存接口类
|
|
|
|
|
*/
|
2018-10-27 22:40:44 +08:00
|
|
|
|
class FrameRingInterface : public FrameWriterInterface{
|
2018-10-23 11:09:21 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef RingBuffer<Frame::Ptr> RingType;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
typedef std::shared_ptr<FrameRingInterface> Ptr;
|
2018-10-23 11:09:21 +08:00
|
|
|
|
|
|
|
|
|
FrameRingInterface(){}
|
|
|
|
|
virtual ~FrameRingInterface(){}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取帧环形缓存
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
virtual RingType::Ptr getFrameRing() const = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置帧环形缓存
|
|
|
|
|
* @param ring
|
|
|
|
|
*/
|
|
|
|
|
virtual void setFrameRing(const RingType::Ptr &ring) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 帧环形缓存
|
|
|
|
|
*/
|
2018-10-23 11:09:21 +08:00
|
|
|
|
class FrameRing : public FrameRingInterface{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<FrameRing> Ptr;
|
|
|
|
|
|
|
|
|
|
FrameRing(){
|
|
|
|
|
}
|
|
|
|
|
virtual ~FrameRing(){}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取帧环形缓存
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
RingType::Ptr getFrameRing() const override {
|
|
|
|
|
return _frameRing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置帧环形缓存
|
|
|
|
|
* @param ring
|
|
|
|
|
*/
|
|
|
|
|
void setFrameRing(const RingType::Ptr &ring) override {
|
|
|
|
|
_frameRing = ring;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入数据帧
|
|
|
|
|
* @param frame
|
|
|
|
|
*/
|
2018-10-23 21:41:45 +08:00
|
|
|
|
void inputFrame(const Frame::Ptr &frame) override{
|
2018-10-25 11:53:45 +08:00
|
|
|
|
if(_frameRing){
|
|
|
|
|
_frameRing->write(frame,frame->keyFrame());
|
|
|
|
|
}
|
2018-10-23 11:09:21 +08:00
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
RingType::Ptr _frameRing;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 支持代理转发的帧环形缓存
|
|
|
|
|
*/
|
2018-10-26 16:09:48 +08:00
|
|
|
|
class FrameRingInterfaceDelegate : public FrameRing {
|
2018-10-23 22:16:54 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<FrameRingInterfaceDelegate> Ptr;
|
|
|
|
|
|
2018-10-26 16:09:48 +08:00
|
|
|
|
FrameRingInterfaceDelegate(){}
|
2018-10-23 22:16:54 +08:00
|
|
|
|
virtual ~FrameRingInterfaceDelegate(){}
|
|
|
|
|
|
2018-10-27 22:40:44 +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
|
|
|
|
FrameRing::inputFrame(frame);
|
|
|
|
|
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;
|
2018-10-27 22:40:44 +08:00
|
|
|
|
map<void *,FrameWriterInterface::Ptr> _delegateMap;
|
2018-10-23 22:16:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-26 16:09:48 +08:00
|
|
|
|
class FrameNoCopyAble : public Frame{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<FrameNoCopyAble> Ptr;
|
|
|
|
|
char *data() const override{
|
|
|
|
|
return buffer_ptr;
|
|
|
|
|
}
|
|
|
|
|
uint32_t size() const override {
|
|
|
|
|
return buffer_size;
|
|
|
|
|
}
|
2018-11-17 17:26:38 +08:00
|
|
|
|
uint32_t dts() const override {
|
2018-10-26 16:09:48 +08:00
|
|
|
|
return timeStamp;
|
|
|
|
|
}
|
|
|
|
|
uint32_t prefixSize() const override{
|
|
|
|
|
return iPrefixSize;
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
char *buffer_ptr;
|
|
|
|
|
uint32_t buffer_size;
|
|
|
|
|
uint32_t timeStamp;
|
|
|
|
|
uint32_t iPrefixSize;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_FRAME_H
|