2019-07-24 18:40:18 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2019-07-24 18:40:18 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-07-24 18:40:18 +08:00
|
|
|
|
*
|
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.
|
2019-07-24 18:40:18 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Frame.h"
|
2021-02-05 11:51:16 +08:00
|
|
|
|
#include "H264.h"
|
|
|
|
|
#include "H265.h"
|
2019-07-24 18:40:18 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2021-01-23 09:44:37 +08:00
|
|
|
|
namespace toolkit {
|
|
|
|
|
StatisticImp(mediakit::Frame);
|
|
|
|
|
StatisticImp(mediakit::FrameImp);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:18 +08:00
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
2021-02-05 11:51:16 +08:00
|
|
|
|
template<typename C>
|
|
|
|
|
std::shared_ptr<C> FrameImp::create_l() {
|
|
|
|
|
#if 0
|
|
|
|
|
static ResourcePool<C> packet_pool;
|
|
|
|
|
static onceToken token([]() {
|
|
|
|
|
packet_pool.setSize(1024);
|
|
|
|
|
});
|
|
|
|
|
auto ret = packet_pool.obtain();
|
|
|
|
|
ret->_buffer.clear();
|
|
|
|
|
ret->_prefix_size = 0;
|
|
|
|
|
ret->_dts = 0;
|
|
|
|
|
ret->_pts = 0;
|
|
|
|
|
return ret;
|
|
|
|
|
#else
|
|
|
|
|
return std::shared_ptr<C>(new C());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define CREATE_FRAME_IMP(C) \
|
|
|
|
|
template<> \
|
|
|
|
|
std::shared_ptr<C> FrameImp::create<C>() { \
|
|
|
|
|
return create_l<C>(); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CREATE_FRAME_IMP(FrameImp);
|
|
|
|
|
CREATE_FRAME_IMP(H264Frame);
|
|
|
|
|
CREATE_FRAME_IMP(H265Frame);
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
|
|
|
|
|
*/
|
|
|
|
|
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{
|
2021-02-05 11:51:16 +08:00
|
|
|
|
_buffer = FrameImp::create();
|
|
|
|
|
_buffer->_buffer.assign(frame->data(),frame->size());
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_ptr = _buffer->data();
|
|
|
|
|
}
|
|
|
|
|
_size = frame->size();
|
|
|
|
|
_dts = frame->dts();
|
|
|
|
|
_pts = frame->pts();
|
|
|
|
|
_prefix_size = frame->prefixSize();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_codec_id = frame->getCodecId();
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_key = frame->keyFrame();
|
|
|
|
|
_config = frame->configFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 10:22:12 +08:00
|
|
|
|
~FrameCacheAble() override = default;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 可以被缓存
|
|
|
|
|
*/
|
|
|
|
|
bool cacheAble() const override {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keyFrame() const override{
|
|
|
|
|
return _key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool configFrame() const override{
|
|
|
|
|
return _config;
|
|
|
|
|
}
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
private:
|
|
|
|
|
bool _key;
|
|
|
|
|
bool _config;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
Frame::Ptr _frame;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
FrameImp::Ptr _buffer;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-24 18:40:18 +08:00
|
|
|
|
Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
|
|
|
|
|
if(frame->cacheAble()){
|
|
|
|
|
return frame;
|
|
|
|
|
}
|
|
|
|
|
return std::make_shared<FrameCacheAble>(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 21:19:20 +08:00
|
|
|
|
#define SWITCH_CASE(codec_id) case codec_id : return #codec_id
|
2020-05-15 18:08:54 +08:00
|
|
|
|
const char *getCodecName(CodecId codecId) {
|
|
|
|
|
switch (codecId) {
|
2020-03-08 21:19:20 +08:00
|
|
|
|
SWITCH_CASE(CodecH264);
|
|
|
|
|
SWITCH_CASE(CodecH265);
|
|
|
|
|
SWITCH_CASE(CodecAAC);
|
2020-04-17 17:47:10 +08:00
|
|
|
|
SWITCH_CASE(CodecG711A);
|
|
|
|
|
SWITCH_CASE(CodecG711U);
|
2020-05-11 22:33:10 +08:00
|
|
|
|
SWITCH_CASE(CodecOpus);
|
2020-12-26 16:01:08 +08:00
|
|
|
|
SWITCH_CASE(CodecL16);
|
2020-05-11 22:33:10 +08:00
|
|
|
|
default : return "unknown codec";
|
2020-03-08 21:19:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
TrackType getTrackType(CodecId codecId){
|
|
|
|
|
switch (codecId){
|
2020-05-11 22:33:10 +08:00
|
|
|
|
case CodecH264:
|
|
|
|
|
case CodecH265: return TrackVideo;
|
|
|
|
|
case CodecAAC:
|
|
|
|
|
case CodecG711A:
|
|
|
|
|
case CodecG711U:
|
2020-12-26 16:01:08 +08:00
|
|
|
|
case CodecOpus:
|
|
|
|
|
case CodecL16: return TrackAudio;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
default: return TrackInvalid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-24 18:40:18 +08:00
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
const char *CodecInfo::getCodecName() {
|
|
|
|
|
return mediakit::getCodecName(getCodecId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TrackType CodecInfo::getTrackType() {
|
|
|
|
|
return mediakit::getTrackType(getCodecId());
|
|
|
|
|
}
|
2020-05-11 22:33:10 +08:00
|
|
|
|
}//namespace mediakit
|