GrabBag/Module/FFMediaStream/_Inc/CVrFFMediaPusher.h
2026-06-08 17:36:13 +08:00

77 lines
2.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include "IVrFFMediaPusher.h"
#include <atomic>
#include <mutex>
#include <memory>
#include <thread>
#include <cstring>
// MPP 头(仅在 Linux/RK3588 平台真正可用)
#if !defined(FFMEDIA_PLATFORM_DUMMY)
#include "rk_type.h"
#include "rk_mpi.h"
#include "mpp_frame.h"
#include "mpp_packet.h"
#include "mpp_buffer.h"
#include "rk_venc_cfg.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/log.h>
}
#endif
class CVrFFMediaPusher : public IVrFFMediaPusher
{
public:
CVrFFMediaPusher();
~CVrFFMediaPusher() override;
int Init(const VrFFPushConfig& config) override;
int Start() override;
int PushFrame(const void* data, size_t size, int64_t ptsUs = 0) override;
int Stop() override;
int UnInit() override;
std::string GetRtspUrl(const std::string& localIp) const override;
private:
VrFFPushConfig m_config;
std::atomic<bool> m_bStarted{false};
std::atomic<bool> m_bInited{false};
std::mutex m_mutex;
int64_t m_lastPtsUs{0};
#if !defined(FFMEDIA_PLATFORM_DUMMY)
// MPP 编码器
MppCtx m_mppCtx{nullptr};
MppApi* m_mppApi{nullptr};
MppBufferGroup m_mppBufGroup{nullptr};
// FFmpeg RTSP 输出
AVFormatContext* m_fmtCtx{nullptr};
std::atomic<bool> m_headerWritten{false};
std::mutex m_writeMutex; // 保护 write_frame / write_trailer 跨线程并发
// libswscale 格式转换(非 NV12 输入 → NV12直接写入 MppBuffer
SwsContext* m_swsCtx{nullptr};
// 编码器参数16 对齐后的 stride
int m_encWidth{0};
int m_encHeight{0};
int m_encHorStride{0};
int m_encVerStride{0};
bool m_needConvert{false};
// 辅助函数
static uint32_t AlignTo(uint32_t value, uint32_t align);
static MppCodingType ToMppCoding(EFFEncodeType t);
static int ToAvPixelFormat(EFFPushPixelFormat fmt);
int initMppEncoder();
int initRtspOutput();
int initSwscale();
#endif
};