精简复用rtp encoder创建相关代码

This commit is contained in:
ziyue 2022-08-05 17:36:51 +08:00
parent d3ac296228
commit f85de2e108
5 changed files with 98 additions and 116 deletions

View File

@ -95,7 +95,6 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
}
}
Track::Ptr Factory::getTrackByAbstractTrack(const Track::Ptr& track) {
auto codec = track->getCodecId();
switch (codec) {
@ -121,28 +120,12 @@ Track::Ptr Factory::getTrackByAbstractTrack(const Track::Ptr& track) {
}
}
RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) {
RtpCodec::Ptr Factory::getRtpEncoderByCodecId(CodecId codec_id, uint32_t sample_rate, uint8_t pt, uint32_t ssrc) {
GET_CONFIG(uint32_t, audio_mtu, Rtp::kAudioMtuSize);
GET_CONFIG(uint32_t, video_mtu, Rtp::kVideoMtuSize);
// ssrc不冲突即可,可以为任意的32位整形
static atomic<uint32_t> s_ssrc(0);
uint32_t ssrc = s_ssrc++;
if(!ssrc){
//ssrc不能为0
ssrc = 1;
}
if(sdp->getTrackType() == TrackVideo){
//视频的ssrc是偶数方便调试
ssrc = 2 * ssrc;
}else{
//音频ssrc是奇数
ssrc = 2 * ssrc + 1;
}
auto mtu = (sdp->getTrackType() == TrackVideo ? video_mtu : audio_mtu);
auto sample_rate = sdp->getSampleRate();
auto pt = sdp->getPayloadType();
auto interleaved = sdp->getTrackType() * 2;
auto codec_id = sdp->getCodecId();
auto type = getTrackType(codec_id);
auto mtu = type == TrackVideo ? video_mtu : audio_mtu;
auto interleaved = type * 2;
switch (codec_id) {
case CodecH264: return std::make_shared<H264RtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
case CodecH265: return std::make_shared<H265RtpEncoder>(ssrc, mtu, sample_rate, pt, interleaved);
@ -160,6 +143,24 @@ RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) {
}
}
RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) {
// ssrc不冲突即可,可以为任意的32位整形
static atomic<uint32_t> s_ssrc(0);
uint32_t ssrc = s_ssrc++;
if (!ssrc) {
// ssrc不能为0
ssrc = 1;
}
if (sdp->getTrackType() == TrackVideo) {
//视频的ssrc是偶数方便调试
ssrc = 2 * ssrc;
} else {
//音频ssrc是奇数
ssrc = 2 * ssrc + 1;
}
return getRtpEncoderByCodecId(sdp->getCodecId(), sdp->getSampleRate(), sdp->getPayloadType(), ssrc);
}
RtpCodec::Ptr Factory::getRtpDecoderByTrack(const Track::Ptr &track) {
switch (track->getCodecId()){
case CodecH264 : return std::make_shared<H264RtpDecoder>();

View File

@ -38,6 +38,15 @@ public:
*/
static RtpCodec::Ptr getRtpEncoderBySdp(const Sdp::Ptr &sdp);
/**
* codec id生成rtp编码器
* @param codec_id id
* @param sample_rate 90000
* @param pt rtp payload type
* @param ssrc rtp ssrc
*/
static RtpCodec::Ptr getRtpEncoderByCodecId(CodecId codec_id, uint32_t sample_rate, uint8_t pt, uint32_t ssrc);
/**
* Track生成Rtp解包器
*/

View File

@ -11,90 +11,62 @@
#if defined(ENABLE_RTPPROXY)
#include "RawEncoder.h"
#include "Extension/H264Rtp.h"
#include "Extension/AACRtp.h"
#include "Extension/H265Rtp.h"
#include "Extension/CommonRtp.h"
#include "Extension/G711Rtp.h"
#include "Extension/Factory.h"
#include "Rtsp/RtspMuxer.h"
using namespace toolkit;
namespace mediakit {
RawEncoderImp::RawEncoderImp(uint32_t ssrc, uint8_t payload_type,bool sendAudio):_ssrc(ssrc),_payload_type(payload_type),_sendAudio(sendAudio) {
}
RawEncoderImp::RawEncoderImp(uint32_t ssrc, uint8_t payload_type, bool send_audio)
: _ssrc(ssrc)
, _payload_type(payload_type)
, _send_audio(send_audio) {}
RawEncoderImp::~RawEncoderImp() {
InfoL << this << " " << printSSRC(_ssrc);
}
bool RawEncoderImp::addTrack(const Track::Ptr &track) {
if(_sendAudio && track->getTrackType() == TrackType::TrackAudio && !_rtp_encoder){// audio
if (_send_audio && track->getTrackType() == TrackType::TrackAudio && !_rtp_encoder) { // audio
_rtp_encoder = createRtpEncoder(track);
_rtp_encoder->setRtpRing(std::make_shared<RtpRing::RingType>());
_rtp_encoder->getRtpRing()->setDelegate(std::make_shared<RingDelegateHelper>([this](RtpPacket::Ptr rtp, bool is_key){
onRTP(std::move(rtp),true);
}));
_rtp_encoder->getRtpRing()->setDelegate(std::make_shared<RingDelegateHelper>(
[this](RtpPacket::Ptr rtp, bool is_key) { onRTP(std::move(rtp), true); }));
return true;
}
if(!_sendAudio && track->getTrackType()==TrackType::TrackVideo && !_rtp_encoder){
if (!_send_audio && track->getTrackType() == TrackType::TrackVideo && !_rtp_encoder) {
_rtp_encoder = createRtpEncoder(track);
_rtp_encoder->setRtpRing(std::make_shared<RtpRing::RingType>());
_rtp_encoder->getRtpRing()->setDelegate(std::make_shared<RingDelegateHelper>([this](RtpPacket::Ptr rtp, bool is_key){
onRTP(std::move(rtp),is_key);
}));
_rtp_encoder->getRtpRing()->setDelegate(std::make_shared<RingDelegateHelper>(
[this](RtpPacket::Ptr rtp, bool is_key) { onRTP(std::move(rtp), is_key); }));
return true;
}
return true;
}
void RawEncoderImp::resetTracks() {
return;
}
bool RawEncoderImp::inputFrame(const Frame::Ptr &frame) {
if(frame->getTrackType() == TrackType::TrackAudio && _sendAudio && _rtp_encoder){
if (frame->getTrackType() == TrackType::TrackAudio && _send_audio && _rtp_encoder) {
_rtp_encoder->inputFrame(frame);
}
if(frame->getTrackType() == TrackType::TrackVideo && !_sendAudio && _rtp_encoder){
if (frame->getTrackType() == TrackType::TrackVideo && !_send_audio && _rtp_encoder) {
_rtp_encoder->inputFrame(frame);
}
return true;
}
RtpCodec::Ptr RawEncoderImp::createRtpEncoder(const Track::Ptr &track) {
GET_CONFIG(uint32_t,audio_mtu,Rtp::kAudioMtuSize);
GET_CONFIG(uint32_t,video_mtu,Rtp::kVideoMtuSize);
auto codec_id = track->getCodecId();
uint32_t sample_rate = 90000;
int channels = 1;
auto mtu = (track->getTrackType() == TrackVideo ? video_mtu : audio_mtu);
if (track->getTrackType() == TrackType::TrackAudio) {
AudioTrack::Ptr audioTrack = std::dynamic_pointer_cast<AudioTrack>(track);
sample_rate = audioTrack->getAudioSampleRate();
channels = audioTrack->getAudioChannel();
}
switch (codec_id){
case CodecH264 : return std::make_shared<H264RtpEncoder>(_ssrc, mtu, sample_rate, _payload_type, 0);
case CodecH265 : return std::make_shared<H265RtpEncoder>(_ssrc, mtu, sample_rate, _payload_type, 0);
case CodecAAC : return std::make_shared<AACRtpEncoder>(_ssrc, mtu, sample_rate, _payload_type, 0);
case CodecL16 :
case CodecOpus : return std::make_shared<CommonRtpEncoder>(codec_id, _ssrc, mtu, sample_rate, _payload_type, 0);
case CodecG711A :
case CodecG711U : {
if (_payload_type == Rtsp::PT_PCMA || _payload_type == Rtsp::PT_PCMU) {
return std::make_shared<G711RtpEncoder>(codec_id, _ssrc, mtu, sample_rate, _payload_type, 0, channels);
}
return std::make_shared<CommonRtpEncoder>(codec_id, _ssrc, mtu, sample_rate, _payload_type, 0);
}
default : WarnL << "暂不支持该CodecId:" << codec_id; return nullptr;
sample_rate = std::static_pointer_cast<AudioTrack>(track)->getAudioSampleRate();
}
return Factory::getRtpEncoderByCodecId(track->getCodecId(), sample_rate, _payload_type, _ssrc);
}
} // namespace mediakit

View File

@ -13,7 +13,6 @@
#if defined(ENABLE_RTPPROXY)
#include "Common/MediaSink.h"
#include "Common/Stamp.h"
#include "Extension/CommonRtp.h"
@ -22,7 +21,7 @@ namespace mediakit{
class RawEncoderImp : public MediaSinkInterface {
public:
RawEncoderImp(uint32_t ssrc, uint8_t payload_type = 96, bool sendAudio = true);
RawEncoderImp(uint32_t ssrc, uint8_t payload_type = 96, bool send_audio = true);
~RawEncoderImp() override;
/**
@ -30,7 +29,6 @@ public:
*/
bool addTrack(const Track::Ptr &track) override;
/**
*
*/
@ -44,11 +42,14 @@ public:
protected:
// rtp打包后回调
virtual void onRTP(toolkit::Buffer::Ptr rtp, bool is_key = false) = 0;
private:
RtpCodec::Ptr createRtpEncoder(const Track::Ptr &track);
uint32_t _ssrc;
private:
bool _send_audio;
uint8_t _payload_type;
bool _sendAudio;
uint32_t _ssrc;
RtpCodec::Ptr _rtp_encoder;
};

View File

@ -50,7 +50,6 @@ protected:
void onRTP(toolkit::Buffer::Ptr rtp,bool is_key = false) override;
};
class RtpCacheRaw : public RtpCache, public RawEncoderImp{
public:
RtpCacheRaw(onFlushed cb, uint32_t ssrc, uint8_t payload_type = 96, bool sendAudio = true) : RtpCache(std::move(cb)), RawEncoderImp(ssrc, payload_type,sendAudio) {};