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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-10-25 10:00:17 +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.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*/
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_FRAME_H
|
|
|
|
|
#define ZLMEDIAKIT_FRAME_H
|
|
|
|
|
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include <map>
|
2018-10-26 16:09:48 +08:00
|
|
|
|
#include <mutex>
|
2018-10-27 22:40:44 +08:00
|
|
|
|
#include <functional>
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Util/List.h"
|
新增支持获取gop大小与间隔信息: #1570
getMediaList/getMediaInfo接口、on_media_changed hook新增支持字段如下:
{
"codec_id" : 0,
"codec_id_name" : "H264",
"codec_type" : 0,
"fps" : 0.0,
"frames" : 1119, #累计接收帧数,不包含sei/aud/sps/pps等不能解码的帧
"gop_interval_ms" : 1993, #gop间隔时间,单位毫秒
"gop_size" : 60, #gop大小,单位帧数
"height" : 556,
"key_frames" : 21, #累计接收关键帧数
"ready" : true,
"width" : 990
}
2023-02-20 16:10:22 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Network/Buffer.h"
|
2018-10-26 16:09:48 +08:00
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
namespace mediakit {
|
2022-11-29 11:07:13 +08:00
|
|
|
|
class Stamp;
|
2018-10-23 11:09:21 +08:00
|
|
|
|
typedef enum {
|
|
|
|
|
TrackInvalid = -1,
|
|
|
|
|
TrackVideo = 0,
|
|
|
|
|
TrackAudio,
|
2018-10-25 22:57:59 +08:00
|
|
|
|
TrackTitle,
|
2021-03-28 17:32:53 +08:00
|
|
|
|
TrackApplication,
|
2021-03-30 11:51:19 +08:00
|
|
|
|
TrackMax
|
2018-10-23 11:09:21 +08:00
|
|
|
|
} TrackType;
|
|
|
|
|
|
2021-06-16 11:14:10 +08:00
|
|
|
|
#define CODEC_MAP(XX) \
|
2021-12-28 21:04:53 +08:00
|
|
|
|
XX(CodecH264, TrackVideo, 0, "H264", PSI_STREAM_H264) \
|
|
|
|
|
XX(CodecH265, TrackVideo, 1, "H265", PSI_STREAM_H265) \
|
|
|
|
|
XX(CodecAAC, TrackAudio, 2, "mpeg4-generic", PSI_STREAM_AAC) \
|
|
|
|
|
XX(CodecG711A, TrackAudio, 3, "PCMA", PSI_STREAM_AUDIO_G711A) \
|
|
|
|
|
XX(CodecG711U, TrackAudio, 4, "PCMU", PSI_STREAM_AUDIO_G711U) \
|
|
|
|
|
XX(CodecOpus, TrackAudio, 5, "opus", PSI_STREAM_AUDIO_OPUS) \
|
|
|
|
|
XX(CodecL16, TrackAudio, 6, "L16", PSI_STREAM_RESERVED) \
|
|
|
|
|
XX(CodecVP8, TrackVideo, 7, "VP8", PSI_STREAM_VP8) \
|
|
|
|
|
XX(CodecVP9, TrackVideo, 8, "VP9", PSI_STREAM_VP9) \
|
2022-12-21 15:32:16 +08:00
|
|
|
|
XX(CodecAV1, TrackVideo, 9, "AV1", PSI_STREAM_AV1) \
|
|
|
|
|
XX(CodecJPEG, TrackVideo, 10, "JPEG", PSI_STREAM_JPEG_2000)
|
2021-06-16 11:14:10 +08:00
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
CodecInvalid = -1,
|
2021-12-28 21:04:53 +08:00
|
|
|
|
#define XX(name, type, value, str, mpeg_id) name = value,
|
2021-06-16 11:14:10 +08:00
|
|
|
|
CODEC_MAP(XX)
|
|
|
|
|
#undef XX
|
|
|
|
|
CodecMax
|
|
|
|
|
} CodecId;
|
|
|
|
|
|
2021-03-30 10:59:15 +08:00
|
|
|
|
/**
|
|
|
|
|
* 字符串转媒体类型转
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
TrackType getTrackType(const std::string &str);
|
2021-03-30 10:59:15 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 媒体类型转字符串
|
|
|
|
|
*/
|
|
|
|
|
const char* getTrackString(TrackType type);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据SDP中描述获取codec_id
|
|
|
|
|
* @param str
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
CodecId getCodecId(const std::string &str);
|
2021-03-30 10:59:15 +08:00
|
|
|
|
|
2020-05-15 18:08:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取编码器名称
|
|
|
|
|
*/
|
|
|
|
|
const char *getCodecName(CodecId codecId);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取音视频类型
|
|
|
|
|
*/
|
|
|
|
|
TrackType getTrackType(CodecId codecId);
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 编码信息的抽象接口
|
|
|
|
|
*/
|
2018-10-23 11:09:21 +08:00
|
|
|
|
class CodecInfo {
|
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<CodecInfo>;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
CodecInfo() = default;
|
|
|
|
|
virtual ~CodecInfo() = default;
|
2018-10-23 11:09:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取编解码器类型
|
|
|
|
|
*/
|
|
|
|
|
virtual CodecId getCodecId() const = 0;
|
2020-03-08 21:19:20 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取编码器名称
|
|
|
|
|
*/
|
2021-07-09 13:38:20 +08:00
|
|
|
|
const char *getCodecName() const;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取音视频类型
|
|
|
|
|
*/
|
2021-07-09 13:38:20 +08:00
|
|
|
|
TrackType getTrackType() const;
|
2018-10-23 11:09:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 帧类型的抽象接口
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class Frame : public toolkit::Buffer, public CodecInfo {
|
2018-10-18 23:48:00 +08:00
|
|
|
|
public:
|
2022-07-22 16:20:47 +08:00
|
|
|
|
using Ptr = std::shared_ptr<Frame>;
|
|
|
|
|
virtual ~Frame() = default;
|
2018-11-17 17:26:38 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回解码时间戳,单位毫秒
|
|
|
|
|
*/
|
2022-08-08 17:13:39 +08:00
|
|
|
|
virtual uint64_t dts() const = 0;
|
2018-11-17 17:26:38 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回显示时间戳,单位毫秒
|
|
|
|
|
*/
|
2022-08-08 17:13:39 +08:00
|
|
|
|
virtual uint64_t pts() const { return dts(); }
|
2018-10-21 22:24:24 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 前缀长度,譬如264前缀为0x00 00 00 01,那么前缀长度就是4
|
|
|
|
|
* aac前缀则为7个字节
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
virtual size_t prefixSize() const = 0;
|
2018-10-23 21:41:45 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回是否为关键帧
|
|
|
|
|
*/
|
|
|
|
|
virtual bool keyFrame() const = 0;
|
2019-07-24 18:02:55 +08:00
|
|
|
|
|
2019-08-01 18:49:04 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否为配置帧,譬如sps pps vps
|
|
|
|
|
*/
|
|
|
|
|
virtual bool configFrame() const = 0;
|
|
|
|
|
|
2019-07-24 18:02:55 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否可以缓存
|
|
|
|
|
*/
|
|
|
|
|
virtual bool cacheAble() const { return true; }
|
2019-07-24 18:40:18 +08:00
|
|
|
|
|
2021-07-09 13:38:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* 该帧是否可以丢弃
|
|
|
|
|
* SEI/AUD帧可以丢弃
|
|
|
|
|
* 默认都不能丢帧
|
|
|
|
|
*/
|
|
|
|
|
virtual bool dropAble() const { return false; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否为可解码帧
|
|
|
|
|
* sps pps等帧不能解码
|
|
|
|
|
*/
|
|
|
|
|
virtual bool decodeAble() const {
|
|
|
|
|
if (getTrackType() != TrackVideo) {
|
|
|
|
|
//非视频帧都可以解码
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//默认非sps pps帧都可以解码
|
|
|
|
|
return !configFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:18 +08:00
|
|
|
|
/**
|
|
|
|
|
* 返回可缓存的frame
|
|
|
|
|
*/
|
|
|
|
|
static Ptr getCacheAbleFrame(const Ptr &frame);
|
2021-01-23 09:44:37 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
//对象个数统计
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::ObjectStatistic<Frame> _statistic;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
class FrameImp : public Frame {
|
|
|
|
|
public:
|
2021-02-05 11:51:16 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameImp>;
|
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
template <typename C = FrameImp>
|
2021-09-02 10:56:21 +08:00
|
|
|
|
static std::shared_ptr<C> create() {
|
|
|
|
|
#if 0
|
|
|
|
|
static ResourcePool<C> packet_pool;
|
|
|
|
|
static onceToken token([]() {
|
|
|
|
|
packet_pool.setSize(1024);
|
|
|
|
|
});
|
2022-01-06 14:30:44 +08:00
|
|
|
|
auto ret = packet_pool.obtain2();
|
2021-09-02 10:56:21 +08:00
|
|
|
|
ret->_buffer.clear();
|
|
|
|
|
ret->_prefix_size = 0;
|
|
|
|
|
ret->_dts = 0;
|
|
|
|
|
ret->_pts = 0;
|
|
|
|
|
return ret;
|
|
|
|
|
#else
|
|
|
|
|
return std::shared_ptr<C>(new C());
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
char *data() const override { return (char *)_buffer.data(); }
|
|
|
|
|
size_t size() const override { return _buffer.size(); }
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t dts() const override { return _dts; }
|
|
|
|
|
uint64_t pts() const override { return _pts ? _pts : _dts; }
|
2022-07-22 16:20:47 +08:00
|
|
|
|
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; }
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
|
|
|
|
public:
|
2020-08-01 10:22:12 +08:00
|
|
|
|
CodecId _codec_id = CodecInvalid;
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t _dts = 0;
|
|
|
|
|
uint64_t _pts = 0;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _prefix_size = 0;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::BufferLikeString _buffer;
|
2021-02-05 16:49:11 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2021-01-23 09:44:37 +08:00
|
|
|
|
//对象个数统计
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::ObjectStatistic<FrameImp> _statistic;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
friend class toolkit::ResourcePool_l<FrameImp>;
|
2021-02-05 11:51:16 +08:00
|
|
|
|
FrameImp() = default;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 一个Frame类中可以有多个帧,他们通过 0x 00 00 01 分隔
|
|
|
|
|
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
|
|
|
|
|
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
|
|
|
|
|
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
|
|
|
|
|
*/
|
2022-07-22 16:20:47 +08:00
|
|
|
|
template <typename Parent>
|
|
|
|
|
class FrameInternal : public Parent {
|
2020-05-11 22:33:10 +08:00
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameInternal>;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
FrameInternal(const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size)
|
2022-07-22 16:20:47 +08:00
|
|
|
|
: Parent(ptr, size, parent_frame->dts(), parent_frame->pts(), prefix_size) {
|
2020-05-11 22:33:10 +08:00
|
|
|
|
_parent_frame = parent_frame;
|
|
|
|
|
}
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool cacheAble() const override { return _parent_frame->cacheAble(); }
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
private:
|
|
|
|
|
Frame::Ptr _parent_frame;
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-26 00:54:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 一个Frame类中可以有多个帧(AAC),时间戳会变化
|
|
|
|
|
* ZLMediaKit会先把这种复合帧split成单个帧然后再处理
|
|
|
|
|
* 一个复合帧可以通过无内存拷贝的方式切割成多个子Frame
|
|
|
|
|
* 提供该类的目的是切割复合帧时防止内存拷贝,提高性能
|
|
|
|
|
*/
|
2022-07-22 16:20:47 +08:00
|
|
|
|
template <typename Parent>
|
|
|
|
|
class FrameTSInternal : public Parent {
|
2022-06-26 00:54:31 +08:00
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameTSInternal>;
|
2022-07-22 16:20:47 +08:00
|
|
|
|
FrameTSInternal(
|
2022-08-08 17:13:39 +08:00
|
|
|
|
const Frame::Ptr &parent_frame, char *ptr, size_t size, size_t prefix_size, uint64_t dts, uint64_t pts)
|
2022-07-22 16:20:47 +08:00
|
|
|
|
: Parent(ptr, size, dts, pts, prefix_size) {
|
2022-06-26 00:54:31 +08:00
|
|
|
|
_parent_frame = parent_frame;
|
|
|
|
|
}
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool cacheAble() const override { return _parent_frame->cacheAble(); }
|
|
|
|
|
|
2022-06-26 00:54:31 +08:00
|
|
|
|
private:
|
|
|
|
|
Frame::Ptr _parent_frame;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-27 22:54:16 +08:00
|
|
|
|
/**
|
2020-05-11 22:33:10 +08:00
|
|
|
|
* 写帧接口的抽象接口类
|
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:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameWriterInterface>;
|
2022-07-22 16:20:47 +08:00
|
|
|
|
FrameWriterInterface() = default;
|
|
|
|
|
virtual ~FrameWriterInterface() = default;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
|
2018-10-26 16:09:48 +08:00
|
|
|
|
/**
|
2020-05-11 22:33:10 +08:00
|
|
|
|
* 写入帧数据
|
|
|
|
|
*/
|
2021-09-27 13:12:53 +08:00
|
|
|
|
virtual bool inputFrame(const Frame::Ptr &frame) = 0;
|
2022-10-16 19:49:56 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新输出所有frame缓存
|
|
|
|
|
*/
|
|
|
|
|
virtual void flush() {};
|
2018-10-26 16:09:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
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:
|
2022-05-25 15:38:32 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameDispatcher>;
|
|
|
|
|
FrameDispatcher() = default;
|
|
|
|
|
~FrameDispatcher() override = default;
|
2018-10-23 22:16:54 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加代理
|
|
|
|
|
*/
|
2022-11-01 16:52:52 +08:00
|
|
|
|
FrameWriterInterface* addDelegate(FrameWriterInterface::Ptr delegate) {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2022-11-01 16:52:52 +08:00
|
|
|
|
return _delegates.emplace(delegate.get(), std::move(delegate)).first->second.get();
|
2018-10-23 22:16:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 16:52:52 +08:00
|
|
|
|
FrameWriterInterface* addDelegate(std::function<bool(const Frame::Ptr &frame)> cb);
|
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 删除代理
|
|
|
|
|
*/
|
2022-05-25 15:38:32 +08:00
|
|
|
|
void delDelegate(FrameWriterInterface *ptr) {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2022-05-25 15:38:32 +08:00
|
|
|
|
_delegates.erase(ptr);
|
2018-10-23 22:16:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-11 22:33:10 +08:00
|
|
|
|
* 写入帧并派发
|
2018-10-23 22:16:54 +08:00
|
|
|
|
*/
|
2022-05-25 15:38:32 +08:00
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
新增支持获取gop大小与间隔信息: #1570
getMediaList/getMediaInfo接口、on_media_changed hook新增支持字段如下:
{
"codec_id" : 0,
"codec_id_name" : "H264",
"codec_type" : 0,
"fps" : 0.0,
"frames" : 1119, #累计接收帧数,不包含sei/aud/sps/pps等不能解码的帧
"gop_interval_ms" : 1993, #gop间隔时间,单位毫秒
"gop_size" : 60, #gop大小,单位帧数
"height" : 556,
"key_frames" : 21, #累计接收关键帧数
"ready" : true,
"width" : 990
}
2023-02-20 16:10:22 +08:00
|
|
|
|
doStatistics(frame);
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool ret = false;
|
2022-05-25 15:38:32 +08:00
|
|
|
|
for (auto &pr : _delegates) {
|
2021-09-27 13:12:53 +08:00
|
|
|
|
if (pr.second->inputFrame(frame)) {
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
2020-04-06 21:44:32 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return ret;
|
2020-05-11 22:33:10 +08:00
|
|
|
|
}
|
2020-04-06 21:44:32 +08:00
|
|
|
|
|
2020-05-11 22:33:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 返回代理个数
|
|
|
|
|
*/
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t size() const {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2022-05-25 15:38:32 +08:00
|
|
|
|
return _delegates.size();
|
2018-10-23 22:16:54 +08:00
|
|
|
|
}
|
2022-05-25 15:38:32 +08:00
|
|
|
|
|
|
|
|
|
void clear() {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2022-05-25 15:38:32 +08:00
|
|
|
|
_delegates.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-05 22:04:14 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取累计关键帧数
|
|
|
|
|
*/
|
|
|
|
|
uint64_t getVideoKeyFrames() const {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2023-02-05 22:04:14 +08:00
|
|
|
|
return _video_key_frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取帧数
|
|
|
|
|
*/
|
|
|
|
|
uint64_t getFrames() const {
|
2023-02-06 14:18:21 +08:00
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
2023-02-05 22:04:14 +08:00
|
|
|
|
return _frames;
|
|
|
|
|
}
|
|
|
|
|
|
新增支持获取gop大小与间隔信息: #1570
getMediaList/getMediaInfo接口、on_media_changed hook新增支持字段如下:
{
"codec_id" : 0,
"codec_id_name" : "H264",
"codec_type" : 0,
"fps" : 0.0,
"frames" : 1119, #累计接收帧数,不包含sei/aud/sps/pps等不能解码的帧
"gop_interval_ms" : 1993, #gop间隔时间,单位毫秒
"gop_size" : 60, #gop大小,单位帧数
"height" : 556,
"key_frames" : 21, #累计接收关键帧数
"ready" : true,
"width" : 990
}
2023-02-20 16:10:22 +08:00
|
|
|
|
size_t getVideoGopSize() const {
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
|
|
|
|
return _gop_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t getVideoGopInterval() const {
|
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
|
|
|
|
return _gop_interval_ms;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void doStatistics(const Frame::Ptr &frame) {
|
|
|
|
|
if (!frame->configFrame() && !frame->dropAble()) {
|
|
|
|
|
// 忽略配置帧与可丢弃的帧
|
|
|
|
|
++_frames;
|
|
|
|
|
if (frame->keyFrame() && frame->getTrackType() == TrackVideo) {
|
|
|
|
|
// 遇视频关键帧时统计
|
|
|
|
|
++_video_key_frames;
|
|
|
|
|
_gop_size = _frames - _last_frames;
|
|
|
|
|
_gop_interval_ms = _ticker.elapsedTime();
|
|
|
|
|
_last_frames = _frames;
|
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-23 22:16:54 +08:00
|
|
|
|
private:
|
新增支持获取gop大小与间隔信息: #1570
getMediaList/getMediaInfo接口、on_media_changed hook新增支持字段如下:
{
"codec_id" : 0,
"codec_id_name" : "H264",
"codec_type" : 0,
"fps" : 0.0,
"frames" : 1119, #累计接收帧数,不包含sei/aud/sps/pps等不能解码的帧
"gop_interval_ms" : 1993, #gop间隔时间,单位毫秒
"gop_size" : 60, #gop大小,单位帧数
"height" : 556,
"key_frames" : 21, #累计接收关键帧数
"ready" : true,
"width" : 990
}
2023-02-20 16:10:22 +08:00
|
|
|
|
toolkit::Ticker _ticker;
|
|
|
|
|
size_t _gop_interval_ms = 0;
|
|
|
|
|
size_t _gop_size = 0;
|
|
|
|
|
uint64_t _last_frames = 0;
|
2023-02-05 22:04:14 +08:00
|
|
|
|
uint64_t _frames = 0;
|
|
|
|
|
uint64_t _video_key_frames = 0;
|
2023-02-06 14:18:21 +08:00
|
|
|
|
mutable std::recursive_mutex _mtx;
|
2022-05-25 15:38:32 +08:00
|
|
|
|
std::map<void *, FrameWriterInterface::Ptr> _delegates;
|
2018-10-23 22:16:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-07-25 09:38:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通过Frame接口包装指针,方便使用者把自己的数据快速接入ZLMediaKit
|
|
|
|
|
*/
|
2022-07-22 16:20:47 +08:00
|
|
|
|
class FrameFromPtr : public Frame {
|
2018-10-26 16:09:48 +08:00
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameFromPtr>;
|
2020-08-08 12:19:04 +08:00
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
FrameFromPtr(
|
2022-08-08 17:13:39 +08:00
|
|
|
|
CodecId codec_id, char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0,
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool is_key = false)
|
|
|
|
|
: FrameFromPtr(ptr, size, dts, pts, prefix_size, is_key) {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_codec_id = codec_id;
|
2020-08-08 12:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
FrameFromPtr(char *ptr, size_t size, uint64_t dts, uint64_t pts = 0, size_t prefix_size = 0, bool is_key = false) {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_ptr = ptr;
|
|
|
|
|
_size = size;
|
|
|
|
|
_dts = dts;
|
|
|
|
|
_pts = pts;
|
|
|
|
|
_prefix_size = prefix_size;
|
2022-07-09 22:30:43 +08:00
|
|
|
|
_is_key = is_key;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
char *data() const override { return _ptr; }
|
|
|
|
|
size_t size() const override { return _size; }
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t dts() const override { return _dts; }
|
|
|
|
|
uint64_t pts() const override { return _pts ? _pts : dts(); }
|
2022-07-22 16:20:47 +08:00
|
|
|
|
size_t prefixSize() const override { return _prefix_size; }
|
|
|
|
|
bool cacheAble() const override { return false; }
|
|
|
|
|
bool keyFrame() const override { return _is_key; }
|
|
|
|
|
bool configFrame() const override { return false; }
|
|
|
|
|
void setCodecId(CodecId codec_id) { _codec_id = codec_id; }
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2020-08-08 12:19:04 +08:00
|
|
|
|
CodecId getCodecId() const override {
|
|
|
|
|
if (_codec_id == CodecInvalid) {
|
|
|
|
|
throw std::invalid_argument("FrameFromPtr对象未设置codec类型");
|
|
|
|
|
}
|
2020-08-01 10:22:12 +08:00
|
|
|
|
return _codec_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2022-07-22 16:20:47 +08:00
|
|
|
|
FrameFromPtr() = default;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2019-07-25 09:38:16 +08:00
|
|
|
|
protected:
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool _is_key;
|
2019-07-25 09:38:16 +08:00
|
|
|
|
char *_ptr;
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t _dts;
|
|
|
|
|
uint64_t _pts = 0;
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t _size;
|
|
|
|
|
size_t _prefix_size;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
CodecId _codec_id = CodecInvalid;
|
2019-07-25 09:38:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-04-09 10:22:41 +08:00
|
|
|
|
/**
|
|
|
|
|
* 该对象的功能是把一个不可缓存的帧转换成可缓存的帧
|
|
|
|
|
*/
|
|
|
|
|
class FrameCacheAble : public FrameFromPtr {
|
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameCacheAble>;
|
2022-04-09 10:22:41 +08:00
|
|
|
|
|
|
|
|
|
FrameCacheAble(const Frame::Ptr &frame, bool force_key_frame = false) {
|
|
|
|
|
if (frame->cacheAble()) {
|
|
|
|
|
_frame = frame;
|
|
|
|
|
_ptr = frame->data();
|
|
|
|
|
} else {
|
|
|
|
|
_buffer = FrameImp::create();
|
|
|
|
|
_buffer->_buffer.assign(frame->data(), frame->size());
|
|
|
|
|
_ptr = _buffer->data();
|
|
|
|
|
}
|
|
|
|
|
_size = frame->size();
|
|
|
|
|
_dts = frame->dts();
|
|
|
|
|
_pts = frame->pts();
|
|
|
|
|
_prefix_size = frame->prefixSize();
|
|
|
|
|
_codec_id = frame->getCodecId();
|
|
|
|
|
_key = force_key_frame ? true : frame->keyFrame();
|
|
|
|
|
_config = frame->configFrame();
|
|
|
|
|
_drop_able = frame->dropAble();
|
|
|
|
|
_decode_able = frame->decodeAble();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~FrameCacheAble() override = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 可以被缓存
|
|
|
|
|
*/
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool cacheAble() const override { return true; }
|
|
|
|
|
bool keyFrame() const override { return _key; }
|
|
|
|
|
bool configFrame() const override { return _config; }
|
|
|
|
|
bool dropAble() const override { return _drop_able; }
|
|
|
|
|
bool decodeAble() const override { return _decode_able; }
|
2022-04-09 10:22:41 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool _key;
|
|
|
|
|
bool _config;
|
|
|
|
|
bool _drop_able;
|
|
|
|
|
bool _decode_able;
|
|
|
|
|
Frame::Ptr _frame;
|
|
|
|
|
FrameImp::Ptr _buffer;
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-26 00:54:31 +08:00
|
|
|
|
//该类实现frame级别的时间戳覆盖
|
2022-07-22 16:20:47 +08:00
|
|
|
|
class FrameStamp : public Frame {
|
2022-06-26 00:54:31 +08:00
|
|
|
|
public:
|
2022-07-22 16:20:47 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameStamp>;
|
2022-11-29 11:07:13 +08:00
|
|
|
|
FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp);
|
2022-06-26 00:54:31 +08:00
|
|
|
|
~FrameStamp() override {}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
uint64_t dts() const override { return (uint64_t)_dts; }
|
|
|
|
|
uint64_t pts() const override { return (uint64_t)_pts; }
|
2022-07-22 16:20:47 +08:00
|
|
|
|
size_t prefixSize() const override { return _frame->prefixSize(); }
|
|
|
|
|
bool keyFrame() const override { return _frame->keyFrame(); }
|
|
|
|
|
bool configFrame() const override { return _frame->configFrame(); }
|
|
|
|
|
bool cacheAble() const override { return _frame->cacheAble(); }
|
|
|
|
|
bool dropAble() const override { return _frame->dropAble(); }
|
|
|
|
|
bool decodeAble() const override { return _frame->decodeAble(); }
|
|
|
|
|
char *data() const override { return _frame->data(); }
|
|
|
|
|
size_t size() const override { return _frame->size(); }
|
|
|
|
|
CodecId getCodecId() const override { return _frame->getCodecId(); }
|
2022-06-26 00:54:31 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int64_t _dts;
|
|
|
|
|
int64_t _pts;
|
|
|
|
|
Frame::Ptr _frame;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-21 14:56:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* 该对象可以把Buffer对象转换成可缓存的Frame对象
|
|
|
|
|
*/
|
|
|
|
|
template <typename Parent>
|
2022-07-22 16:20:47 +08:00
|
|
|
|
class FrameWrapper : public Parent {
|
2020-09-21 14:56:58 +08:00
|
|
|
|
public:
|
|
|
|
|
~FrameWrapper() = default;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造frame
|
|
|
|
|
* @param buf 数据缓存
|
|
|
|
|
* @param dts 解码时间戳
|
|
|
|
|
* @param pts 显示时间戳
|
|
|
|
|
* @param prefix 帧前缀长度
|
|
|
|
|
* @param offset buffer有效数据偏移量
|
|
|
|
|
*/
|
2022-08-08 17:13:39 +08:00
|
|
|
|
FrameWrapper(toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix, size_t offset)
|
2022-07-22 16:20:47 +08:00
|
|
|
|
: Parent(buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
|
|
|
|
_buf = std::move(buf);
|
2020-09-21 14:56:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造frame
|
|
|
|
|
* @param buf 数据缓存
|
|
|
|
|
* @param dts 解码时间戳
|
|
|
|
|
* @param pts 显示时间戳
|
|
|
|
|
* @param prefix 帧前缀长度
|
|
|
|
|
* @param offset buffer有效数据偏移量
|
|
|
|
|
* @param codec 帧类型
|
|
|
|
|
*/
|
2022-08-08 17:13:39 +08:00
|
|
|
|
FrameWrapper(toolkit::Buffer::Ptr buf, uint64_t dts, uint64_t pts, size_t prefix, size_t offset, CodecId codec)
|
2022-07-22 16:20:47 +08:00
|
|
|
|
: Parent(codec, buf->data() + offset, buf->size() - offset, dts, pts, prefix) {
|
|
|
|
|
_buf = std::move(buf);
|
2020-09-21 14:56:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 该帧可缓存
|
|
|
|
|
*/
|
2022-07-22 16:20:47 +08:00
|
|
|
|
bool cacheAble() const override { return true; }
|
2020-09-21 14:56:58 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Buffer::Ptr _buf;
|
2020-09-21 14:56:58 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-04-26 18:26:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* 合并一些时间戳相同的frame
|
|
|
|
|
*/
|
|
|
|
|
class FrameMerger {
|
|
|
|
|
public:
|
2022-08-08 17:13:39 +08:00
|
|
|
|
using onOutput = std::function<void(uint64_t dts, uint64_t pts, const toolkit::Buffer::Ptr &buffer, bool have_key_frame)>;
|
2021-06-29 17:42:32 +08:00
|
|
|
|
using Ptr = std::shared_ptr<FrameMerger>;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
enum {
|
|
|
|
|
none = 0,
|
|
|
|
|
h264_prefix,
|
|
|
|
|
mp4_nal_size,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FrameMerger(int type);
|
|
|
|
|
~FrameMerger() = default;
|
|
|
|
|
|
2022-10-16 19:49:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 刷新输出缓冲,注意此时会调用FrameMerger::inputFrame传入的onOutput回调
|
|
|
|
|
* 请注意回调捕获参数此时是否有效
|
|
|
|
|
*/
|
|
|
|
|
void flush();
|
2021-04-26 18:26:07 +08:00
|
|
|
|
void clear();
|
2022-10-16 19:49:56 +08:00
|
|
|
|
bool inputFrame(const Frame::Ptr &frame, onOutput cb, toolkit::BufferLikeString *buffer = nullptr);
|
2021-04-26 18:26:07 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool willFlush(const Frame::Ptr &frame) const;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
void doMerge(toolkit::BufferLikeString &buffer, const Frame::Ptr &frame) const;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int _type;
|
2021-07-09 13:38:20 +08:00
|
|
|
|
bool _have_decode_able_frame = false;
|
2022-10-16 19:49:56 +08:00
|
|
|
|
onOutput _cb;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::List<Frame::Ptr> _frame_cache;
|
2021-04-26 18:26:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-07-22 16:20:47 +08:00
|
|
|
|
} // namespace mediakit
|
|
|
|
|
#endif // ZLMEDIAKIT_FRAME_H
|