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_RTPCODEC_H
|
|
|
|
|
#define ZLMEDIAKIT_RTPCODEC_H
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Extension/Frame.h"
|
2018-10-18 23:48:00 +08:00
|
|
|
|
#include "Util/RingBuffer.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
namespace mediakit {
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
class RtpRing {
|
2018-10-23 11:09:21 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtpRing>;
|
|
|
|
|
using RingType = toolkit::RingBuffer<RtpPacket::Ptr>;
|
2018-10-23 11:09:21 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual ~RtpRing() = default;
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置rtp环形缓存
|
|
|
|
|
* @param ring
|
|
|
|
|
*/
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void setRtpRing(RingType::Ptr ring) {
|
|
|
|
|
_ring = std::move(ring);
|
2019-12-26 09:43:44 +08:00
|
|
|
|
}
|
2018-10-23 18:39:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入rtp包
|
|
|
|
|
* @param rtp rtp包
|
|
|
|
|
* @param key_pos 是否为关键帧第一个rtp包
|
|
|
|
|
* @return 是否为关键帧第一个rtp包
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual bool inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
|
|
|
|
|
if (_ring) {
|
|
|
|
|
_ring->write(rtp, key_pos);
|
2018-10-25 11:53:45 +08:00
|
|
|
|
}
|
2018-10-23 18:39:17 +08:00
|
|
|
|
return key_pos;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2018-10-23 11:09:21 +08:00
|
|
|
|
protected:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2022-11-29 11:07:13 +08:00
|
|
|
|
class RtpInfo {
|
2018-10-18 23:48:00 +08:00
|
|
|
|
public:
|
2021-02-05 11:28:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtpInfo>;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
RtpInfo(uint32_t ssrc, size_t mtu_size, uint32_t sample_rate, uint8_t pt, uint8_t interleaved) {
|
|
|
|
|
if (ssrc == 0) {
|
|
|
|
|
ssrc = ((uint64_t) this) & 0xFFFFFFFF;
|
2018-10-23 11:38:56 +08:00
|
|
|
|
}
|
2021-01-31 19:19:24 +08:00
|
|
|
|
_pt = pt;
|
|
|
|
|
_ssrc = ssrc;
|
|
|
|
|
_mtu_size = mtu_size;
|
|
|
|
|
_sample_rate = sample_rate;
|
|
|
|
|
_interleaved = interleaved;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
//返回rtp负载最大长度
|
|
|
|
|
size_t getMaxSize() const {
|
|
|
|
|
return _mtu_size - RtpPacket::kRtpHeaderSize;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
RtpPacket::Ptr makeRtp(TrackType type,const void *data, size_t len, bool mark, uint64_t stamp);
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
private:
|
|
|
|
|
uint8_t _pt;
|
|
|
|
|
uint8_t _interleaved;
|
|
|
|
|
uint16_t _seq = 0;
|
|
|
|
|
uint32_t _ssrc;
|
|
|
|
|
uint32_t _sample_rate;
|
|
|
|
|
size_t _mtu_size;
|
2018-10-18 23:48:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
class RtpCodec : public RtpRing, public FrameDispatcher {
|
2018-10-23 11:09:21 +08:00
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using Ptr = std::shared_ptr<RtpCodec>;
|
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
void setRtpInfo(uint32_t ssrc, size_t mtu_size, uint32_t sample_rate, uint8_t pt, uint8_t interleaved) {
|
|
|
|
|
_rtp_info.reset(new RtpInfo(ssrc, mtu_size, sample_rate, pt, interleaved));
|
|
|
|
|
}
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
RtpInfo &getRtpInfo() { return *_rtp_info; }
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
private:
|
|
|
|
|
std::unique_ptr<RtpInfo> _rtp_info;
|
|
|
|
|
};
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2023-12-09 16:23:51 +08:00
|
|
|
|
}//namespace mediakit
|
2018-10-18 23:48:00 +08:00
|
|
|
|
|
2018-10-23 11:38:56 +08:00
|
|
|
|
|
2018-10-18 23:48:00 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_RTPCODEC_H
|