2022-04-03 18:38:07 +08:00
|
|
|
|
/*
|
2022-04-01 18:28:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
|
|
|
|
|
|
|
|
|
#include "RawEncoder.h"
|
2022-08-05 17:36:51 +08:00
|
|
|
|
#include "Extension/Factory.h"
|
2022-04-01 18:28:09 +08:00
|
|
|
|
#include "Rtsp/RtspMuxer.h"
|
|
|
|
|
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
namespace mediakit {
|
2022-04-01 18:28:09 +08:00
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
RawEncoderImp::RawEncoderImp(uint32_t ssrc, uint8_t payload_type, bool send_audio)
|
2022-08-08 15:28:43 +08:00
|
|
|
|
: _send_audio(send_audio)
|
2022-08-05 17:36:51 +08:00
|
|
|
|
, _payload_type(payload_type)
|
2022-08-08 15:28:43 +08:00
|
|
|
|
, _ssrc(ssrc) {}
|
2022-04-01 18:28:09 +08:00
|
|
|
|
|
|
|
|
|
RawEncoderImp::~RawEncoderImp() {
|
|
|
|
|
InfoL << this << " " << printSSRC(_ssrc);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
bool RawEncoderImp::addTrack(const Track::Ptr &track) {
|
|
|
|
|
if (_send_audio && track->getTrackType() == TrackType::TrackAudio && !_rtp_encoder) { // audio
|
2022-04-01 18:28:09 +08:00
|
|
|
|
_rtp_encoder = createRtpEncoder(track);
|
|
|
|
|
_rtp_encoder->setRtpRing(std::make_shared<RtpRing::RingType>());
|
2022-08-05 17:36:51 +08:00
|
|
|
|
_rtp_encoder->getRtpRing()->setDelegate(std::make_shared<RingDelegateHelper>(
|
|
|
|
|
[this](RtpPacket::Ptr rtp, bool is_key) { onRTP(std::move(rtp), true); }));
|
2022-04-01 18:28:09 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
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); }));
|
2022-04-01 18:28:09 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
void RawEncoderImp::resetTracks() {
|
2022-04-01 18:28:09 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
bool RawEncoderImp::inputFrame(const Frame::Ptr &frame) {
|
|
|
|
|
if (frame->getTrackType() == TrackType::TrackAudio && _send_audio && _rtp_encoder) {
|
2022-04-01 18:28:09 +08:00
|
|
|
|
_rtp_encoder->inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
if (frame->getTrackType() == TrackType::TrackVideo && !_send_audio && _rtp_encoder) {
|
|
|
|
|
_rtp_encoder->inputFrame(frame);
|
2022-04-01 18:28:09 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
RtpCodec::Ptr RawEncoderImp::createRtpEncoder(const Track::Ptr &track) {
|
2022-04-01 18:28:09 +08:00
|
|
|
|
uint32_t sample_rate = 90000;
|
2022-08-05 17:36:51 +08:00
|
|
|
|
if (track->getTrackType() == TrackType::TrackAudio) {
|
|
|
|
|
sample_rate = std::static_pointer_cast<AudioTrack>(track)->getAudioSampleRate();
|
2022-04-01 18:28:09 +08:00
|
|
|
|
}
|
2022-08-05 17:36:51 +08:00
|
|
|
|
return Factory::getRtpEncoderByCodecId(track->getCodecId(), sample_rate, _payload_type, _ssrc);
|
2022-04-01 18:28:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
} // namespace mediakit
|
2022-04-01 18:28:09 +08:00
|
|
|
|
|
2022-08-05 17:36:51 +08:00
|
|
|
|
#endif // defined(ENABLE_RTPPROXY)
|