ZLMediaKit/src/Player/Frame.h

393 lines
9.6 KiB
C
Raw Normal View History

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-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,
CodecAAC = 0x0100,
CodecMax
} CodecId;
typedef enum {
TrackInvalid = -1,
TrackVideo = 0,
TrackAudio,
2018-10-25 22:57:59 +08:00
TrackTitle,
2018-10-23 11:09:21 +08:00
TrackMax
} TrackType;
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;
};
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-10-23 21:41:45 +08:00
virtual uint32_t stamp() const = 0;
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;
2018-10-18 23:48:00 +08:00
};
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-26 16:09:48 +08:00
class FrameRingWriterInterface {
public:
typedef std::shared_ptr<FrameRingWriterInterface> Ptr;
FrameRingWriterInterface(){}
virtual ~FrameRingWriterInterface(){}
/**
*
* @param frame
*/
virtual void inputFrame(const Frame::Ptr &frame) = 0;
};
2018-10-23 11:09:21 +08:00
/**
*
*/
2018-10-26 16:09:48 +08:00
class FrameRingInterface : public FrameRingWriterInterface{
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;
};
class FrameRing : public FrameRingInterface{
public:
typedef std::shared_ptr<FrameRing> Ptr;
FrameRing(){
2018-10-23 16:41:25 +08:00
_frameRing = std::make_shared<RingType>();
2018-10-23 11:09:21 +08:00
}
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-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-26 16:09:48 +08:00
void addDelegate(const FrameRingWriterInterface::Ptr &delegate){
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;
map<void *,FrameRingWriterInterface::Ptr> _delegateMap;
FrameRing::Ptr _frameRing;
2018-10-23 22:16:54 +08:00
};
2018-10-23 11:09:21 +08:00
/**
* 264
*/
2018-10-18 23:48:00 +08:00
class H264Frame : public Frame {
public:
typedef std::shared_ptr<H264Frame> Ptr;
char *data() const override{
return (char *)buffer.data();
}
uint32_t size() const override {
return buffer.size();
}
2018-10-23 21:41:45 +08:00
uint32_t stamp() const override {
2018-10-18 23:48:00 +08:00
return timeStamp;
}
2018-10-23 21:41:45 +08:00
uint32_t prefixSize() const override{
2018-10-21 22:24:24 +08:00
return iPrefixSize;
}
2018-10-23 11:09:21 +08:00
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
}
2018-10-23 21:41:45 +08:00
bool keyFrame() const override {
return type == 5;
}
2018-10-18 23:48:00 +08:00
public:
uint16_t sequence;
uint32_t timeStamp;
unsigned char type;
string buffer;
2018-10-21 22:24:24 +08:00
uint32_t iPrefixSize = 4;
2018-10-18 23:48:00 +08:00
};
2018-10-26 16:09:48 +08:00
2018-10-23 11:09:21 +08:00
/**
* aac帧adts头
*/
class AACFrame : public Frame {
2018-10-18 23:48:00 +08:00
public:
2018-10-23 11:09:21 +08:00
typedef std::shared_ptr<AACFrame> Ptr;
2018-10-18 23:48:00 +08:00
char *data() const override{
return (char *)buffer;
}
uint32_t size() const override {
return aac_frame_length;
}
2018-10-23 21:41:45 +08:00
uint32_t stamp() const override {
2018-10-18 23:48:00 +08:00
return timeStamp;
}
2018-10-23 21:41:45 +08:00
uint32_t prefixSize() const override{
2018-10-21 22:24:24 +08:00
return iPrefixSize;
}
2018-10-23 11:09:21 +08:00
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return CodecAAC;
}
2018-10-23 21:41:45 +08:00
bool keyFrame() const override {
return false;
}
2018-10-18 23:48:00 +08:00
public:
unsigned int syncword; //12 bslbf 同步字The bit string 1111 1111 1111说明一个ADTS帧的开始
unsigned int id; //1 bslbf MPEG 标示符, 设置为1
unsigned int layer; //2 uimsbf Indicates which layer is used. Set to 00
unsigned int protection_absent; //1 bslbf 表示是否误码校验
unsigned int profile; //2 uimsbf 表示使用哪个级别的AAC如01 Low Complexity(LC)--- AACLC
unsigned int sf_index; //4 uimsbf 表示使用的采样率下标
unsigned int private_bit; //1 bslbf
unsigned int channel_configuration; //3 uimsbf 表示声道数
unsigned int original; //1 bslbf
unsigned int home; //1 bslbf
//下面的为改变的参数即每一帧都不同
unsigned int copyright_identification_bit; //1 bslbf
unsigned int copyright_identification_start; //1 bslbf
unsigned int aac_frame_length; // 13 bslbf 一个ADTS帧的长度包括ADTS头和raw data block
unsigned int adts_buffer_fullness; //11 bslbf 0x7FF 说明是码率可变的码流
//no_raw_data_blocks_in_frame 表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧.
//所以说number_of_raw_data_blocks_in_frame == 0
//表示说ADTS帧中有一个AAC数据块并不是说没有。(一个AAC原始帧包含一段时间内1024个采样及相关数据)
unsigned int no_raw_data_blocks_in_frame; //2 uimsfb
unsigned char buffer[2 * 1024 + 7];
uint16_t sequence;
uint32_t timeStamp;
2018-10-26 16:09:48 +08:00
uint32_t iPrefixSize = 7;
} ;
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;
}
uint32_t stamp() const override {
return timeStamp;
}
uint32_t prefixSize() const override{
return iPrefixSize;
}
public:
char *buffer_ptr;
uint32_t buffer_size;
uint32_t timeStamp;
uint32_t iPrefixSize;
};
class H264FrameNoCopyAble : public FrameNoCopyAble {
public:
typedef std::shared_ptr<H264FrameNoCopyAble> Ptr;
H264FrameNoCopyAble(char *ptr,uint32_t size,uint32_t stamp,int prefixeSize = 4){
buffer_ptr = ptr;
buffer_size = size;
timeStamp = stamp;
iPrefixSize = prefixeSize;
}
TrackType getTrackType() const override{
return TrackVideo;
}
CodecId getCodecId() const override{
return CodecH264;
}
bool keyFrame() const override {
return (buffer_ptr[iPrefixSize] & 0x1F) == 5;
}
};
class AACFrameNoCopyAble : public FrameNoCopyAble {
public:
typedef std::shared_ptr<AACFrameNoCopyAble> Ptr;
AACFrameNoCopyAble(char *ptr,uint32_t size,uint32_t stamp,int prefixeSize = 7){
buffer_ptr = ptr;
buffer_size = size;
timeStamp = stamp;
iPrefixSize = prefixeSize;
}
TrackType getTrackType() const override{
return TrackAudio;
}
CodecId getCodecId() const override{
return CodecAAC;
}
bool keyFrame() const override {
return false;
}
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