ZLMediaKit/ext-codec/H264Rtp.h

114 lines
3.2 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2018-10-25 10:00:17 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* 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_H264RTPCODEC_H
#define ZLMEDIAKIT_H264RTPCODEC_H
2023-12-10 10:21:40 +08:00
#include "H264.h"
// for DtsGenerator
#include "Common/Stamp.h"
2023-12-10 10:21:40 +08:00
#include "Rtsp/RtpCodec.h"
2018-10-18 23:48:00 +08:00
2023-12-10 10:21:40 +08:00
namespace mediakit {
2018-10-21 21:21:14 +08:00
2018-10-21 22:24:24 +08:00
/**
* h264 rtp解码类
2019-08-30 11:17:27 +08:00
* h264 over rtsp-rtp h264-Frame
* rfc3984
* h264 rtp decoder class
* Demultiplex h264-Frame from h264 over rtsp-rtp
* rfc3984
* [AUTO-TRANSLATED:84b4831b]
2018-10-21 22:24:24 +08:00
*/
2021-02-05 11:51:16 +08:00
class H264RtpDecoder : public RtpCodec{
2018-10-18 23:48:00 +08:00
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<H264RtpDecoder>;
2018-10-23 18:39:17 +08:00
2018-10-21 22:24:24 +08:00
H264RtpDecoder();
2018-10-21 21:45:44 +08:00
2018-10-21 22:24:24 +08:00
/**
* 264 rtp包
* @param rtp rtp包
* @param key_pos
* Input 264 rtp packet
* @param rtp rtp packet
* @param key_pos This parameter is ignored
* [AUTO-TRANSLATED:a9ed29db]
2018-10-21 22:24:24 +08:00
*/
2018-10-23 18:39:17 +08:00
bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos = true) override;
2018-10-23 11:09:21 +08:00
2018-10-18 23:48:00 +08:00
private:
2022-08-08 17:13:39 +08:00
bool singleFrame(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint64_t stamp);
bool unpackStapA(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint64_t stamp);
bool mergeFu(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint64_t stamp, uint16_t seq);
2018-10-21 22:24:24 +08:00
bool decodeRtp(const RtpPacket::Ptr &rtp);
H264Frame::Ptr obtainFrame();
void outputFrame(const RtpPacket::Ptr &rtp, const H264Frame::Ptr &frame);
2018-10-18 23:48:00 +08:00
private:
bool _is_gop = false;
bool _gop_dropped = false;
bool _fu_dropped = true;
2021-02-07 18:29:48 +08:00
uint16_t _last_seq = 0;
H264Frame::Ptr _frame;
DtsGenerator _dts_generator;
2018-10-18 23:48:00 +08:00
};
2018-10-21 22:24:24 +08:00
/**
* 264 rtp打包类
* 264 rtp packaging class
* [AUTO-TRANSLATED:baed5b50]
2018-10-21 22:24:24 +08:00
*/
2023-12-09 16:23:51 +08:00
class H264RtpEncoder : public RtpCodec {
2018-10-21 21:45:44 +08:00
public:
2022-12-02 14:43:06 +08:00
using Ptr = std::shared_ptr<H264RtpEncoder>;
2018-10-21 22:24:24 +08:00
/**
* 264
* @param frame
* Input 264 frame
* @param frame Frame data, required
* [AUTO-TRANSLATED:1190bc60]
2018-10-21 22:24:24 +08:00
*/
bool inputFrame(const Frame::Ptr &frame) override;
2022-10-16 19:49:56 +08:00
/**
* frame缓存
* Flush all frame buffers in the output
* [AUTO-TRANSLATED:adaea568]
2022-10-16 19:49:56 +08:00
*/
void flush() override;
2018-10-21 21:45:44 +08:00
private:
2022-08-08 17:13:39 +08:00
void insertConfigFrame(uint64_t pts);
bool inputFrame_l(const Frame::Ptr &frame, bool is_mark);
2022-08-08 17:13:39 +08:00
void packRtp(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos);
void packRtpFu(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos);
void packRtpStapA(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos);
void packRtpSingleNalu(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos);
void packRtpSmallFrame(const char *data, size_t len, uint64_t pts, bool is_mark, bool gop_pos);
2021-04-15 19:39:24 +08:00
private:
2021-04-15 19:46:45 +08:00
Frame::Ptr _sps;
Frame::Ptr _pps;
Frame::Ptr _last_frame;
2018-10-21 21:45:44 +08:00
};
}//namespace mediakit
2018-10-18 23:48:00 +08:00
#endif //ZLMEDIAKIT_H264RTPCODEC_H