mediasoup-demo-v3中添加flexfec需要RtpStreamSend.hpp如何修改
时间: 2024-03-09 18:44:44 浏览: 23
要在mediasoup-demo-v3中添加FlexFEC,需要修改RtpStreamSend.hpp文件中的代码。具体的步骤如下:
1. 打开mediasoup-demo-v3源代码目录中的`worker/src/RtpStreamSend.hpp`文件。
2. 在该文件中找到如下代码:
```
class RtpStreamSend :
public RTC::VideoSinkInterface<webrtc::VideoFrame>,
public RTC::AudioSinkInterface<webrtc::AudioFrame>,
public webrtc::Transport, public RtcpProcessor::Listener
{
public:
RtpStreamSend(
const RTC::scoped_refptr<webrtc::PeerConnectionFactoryInterface>& peerConnectionFactory,
const RTC::scoped_refptr<webrtc::AudioTrackInterface>& audioTrack,
const RTC::scoped_refptr<webrtc::VideoTrackInterface>& videoTrack,
const RTC::scoped_refptr<webrtc::AudioEncoderFactory>& audioEncoderFactory,
const RTC::scoped_refptr<webrtc::VideoEncoderFactory>& videoEncoderFactory,
const RTC::scoped_refptr<webrtc::AudioDecoderFactory>& audioDecoderFactory,
const RTC::scoped_refptr<webrtc::VideoDecoderFactory>& videoDecoderFactory,
const RTC::scoped_refptr<webrtc::AudioProcessing>& audioProcessing,
const RTC::scoped_refptr<webrtc::TaskQueueFactory>& taskQueueFactory,
const RTC::scoped_refptr<webrtc::Call>& call,
std::function<void(const uint8_t*, size_t)> sendVideoCallback,
std::function<void(const uint8_t*, size_t)> sendAudioCallback,
std::function<void(const uint8_t*, size_t)> sendRtpCallback,
std::function<void(const uint8_t*, size_t)> sendRtcpCallback,
std::function<void(const uint8_t*, size_t)> sendSrtpCallback,
std::function<void(const uint8_t*, size_t)> sendSrtcpCallback,
const Json::Value& peerCapabilities,
const Json::Value& appData);
~RtpStreamSend();
// Implementations of webrtc::Transport.
bool SendRtp(const uint8_t* data, size_t len, const webrtc::PacketOptions& options) override;
bool SendRtcp(const uint8_t* data, size_t len) override;
private:
// ...
};
```
3. 在该代码中添加FlexFEC相关的成员变量和方法,如下所示:
```
class RtpStreamSend :
public RTC::VideoSinkInterface<webrtc::VideoFrame>,
public RTC::AudioSinkInterface<webrtc::AudioFrame>,
public webrtc::Transport, public RtcpProcessor::Listener
{
public:
RtpStreamSend(
// ...
);
~RtpStreamSend();
// Implementations of webrtc::Transport.
bool SendRtp(const uint8_t* data, size_t len, const webrtc::PacketOptions& options) override;
bool SendRtcp(const uint8_t* data, size_t len) override;
// FlexFEC related members and methods
private:
std::unique_ptr<webrtc::FlexfecSender> _flexfecSender;
std::map<uint32_t, std::unique_ptr<webrtc::RtpPacketToSend>> _flexfecPackets;
void handleFlexfecPackets(std::map<uint32_t, std::unique_ptr<webrtc::RtpPacketToSend>>& packets);
void sendFlexfecPacket(std::unique_ptr<webrtc::RtpPacketToSend> packet);
};
```
4. 在该代码中实现FlexFEC相关的方法,如下所示:
```
void RtpStreamSend::handleFlexfecPackets(std::map<uint32_t, std::unique_ptr<webrtc::RtpPacketToSend>>& packets)
{
if (_flexfecSender)
{
std::vector<webrtc::RtpSequenceNumberMap::SequenceNumberWithTimestamp> fec_packets;
for (auto& kv : packets)
{
std::unique_ptr<webrtc::RtpPacketToSend>& packet = kv.second;
const webrtc::RtpPacket& rtp_packet = packet->packet;
// Add packet to FlexFEC
if (_flexfecSender->AddRtpPacket(rtp_packet) == webrtc::FlexfecSender::kSuccess)
{
fec_packets.push_back(
webrtc::RtpSequenceNumberMap::SequenceNumberWithTimestamp{
rtp_packet.SequenceNumber(), rtp_packet.Timestamp()});
}
}
if (!fec_packets.empty())
{
// Generate FEC packet
webrtc::RtpPacketToSend fec_packet(_flexfecSender->BuildFlexfecPacket(fec_packets));
if (fec_packet.size() > 0)
{
sendFlexfecPacket(std::make_unique<webrtc::RtpPacketToSend>(std::move(fec_packet)));
}
}
}
}
void RtpStreamSend::sendFlexfecPacket(std::unique_ptr<webrtc::RtpPacketToSend> packet)
{
const uint8_t* data = packet->data();
size_t length = packet->size();
// Send FlexFEC packet via transport
if (_sendRtpCallback)
{
_sendRtpCallback(data, length);
}
}
bool RtpStreamSend::SendRtp(const uint8_t* data, size_t len, const webrtc::PacketOptions& options)
{
// Create RTP packet
webrtc::RtpPacketToSend packet(&_rtpConfig);
if (!packet.Parse(data, len))
{
RTC_LOG(LS_ERROR) << "Failed to parse RTP packet.";
return false;
}
// Check if packet is FEC
if (packet.PayloadType() == _flexfecPayloadType)
{
// Add packet to FlexFEC buffer
uint32_t ssrc = packet.Ssrc();
_flexfecPackets[ssrc] = std::make_unique<webrtc::RtpPacketToSend>(packet);
handleFlexfecPackets(_flexfecPackets);
_flexfecPackets.erase(ssrc);
}
else
{
// Send RTP packet via transport
if (_sendRtpCallback)
{
_sendRtpCallback(data, len);
}
}
return true;
}
bool RtpStreamSend::SendRtcp(const uint8_t* data, size_t len)
{
// ...
}
```
5. 保存文件并重新编译mediasoup-demo-v3。
注意:上述代码只是添加了FlexFEC的基本实现,具体的实现方式可能根据实际情况有所不同。此外,代码中的`_flexfecPayloadType`需要根据实际情况设置为正确的FlexFEC负载类型。