提交 c891068c authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

WIP: PipeConsumer

上级 77d6a308
......@@ -3,6 +3,7 @@
#include "RTC/Consumer.hpp"
#include "RTC/RtpStreamSend.hpp"
#include "RTC/SeqManager.hpp"
namespace RTC
{
......@@ -47,6 +48,10 @@ namespace RTC
std::unordered_map<uint32_t, RTC::RtpStreamSend*> mapMappedSsrcRtpStream;
// Others.
std::vector<RTC::RtpStreamSend*> rtpStreams;
bool keyFrameSupported{ false };
std::unordered_map<RTC::RtpStreamSend*, bool> mapRtpStreamSyncRequired;
std::unordered_map<RTC::RtpStreamSend*, RTC::SeqManager<uint16_t>> mapRtpStreamRtpSeqManager;
std::unordered_map<RTC::RtpStreamSend*, RTC::SeqManager<uint32_t>> mapRtpStreamRtpTimestampManager;
};
// Inline methods.
......
......@@ -125,6 +125,12 @@ namespace RTC
{
this->mediaSsrcs.push_back(encoding.ssrc);
}
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
this->maxRtcpInterval = RTC::RTCP::MaxVideoIntervalMs;
}
Consumer::~Consumer()
......
......@@ -2,8 +2,10 @@
// #define MS_LOG_DEV
#include "RTC/PipeConsumer.hpp"
#include "DepLibUV.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "RTC/Codecs/Codecs.hpp"
namespace RTC
{
......@@ -43,11 +45,18 @@ namespace RTC
RTC::Consumer::FillJson(jsonObject);
}
void PipeConsumer::FillJsonStats(json& /*jsonArray*/) const
void PipeConsumer::FillJsonStats(json& jsonArray) const
{
MS_TRACE();
// Do nothing.
// Add stats of our send streams.
for (auto& kv : this->mapMappedSsrcRtpStream)
{
auto* rtpStream = kv.second;
jsonArray.emplace_back(json::value_t::object);
rtpStream->FillJsonStats(jsonArray[jsonArray.size() - 1]);
}
}
void PipeConsumer::FillJsonScore(json& /*jsonObject*/) const
......@@ -110,7 +119,83 @@ namespace RTC
if (!IsActive())
return;
auto* rtpStream = this->mapMappedSsrcRtpStream.at(packet->GetSsrc());
auto payloadType = packet->GetPayloadType();
// NOTE: This may happen if this Consumer supports just some codecs of those
// in the corresponding Producer.
if (this->supportedCodecPayloadTypes.find(payloadType) == this->supportedCodecPayloadTypes.end())
{
MS_DEBUG_DEV("payload type not supported [payloadType:%" PRIu8 "]", payloadType);
return;
}
auto* rtpStream = this->mapMappedSsrcRtpStream.at(packet->GetSsrc());
auto& syncRequired = this->mapRtpStreamSyncRequired.at(rtpStream);
auto& rtpSeqManager = this->mapRtpStreamRtpSeqManager.at(rtpStream);
auto& rtpTimestampManager = this->mapRtpStreamRtpTimestampManager.at(rtpStream);
// If we need to sync, support key frames and this is not a key frame, ignore
// the packet.
if (syncRequired && this->keyFrameSupported && !packet->IsKeyFrame())
return;
// Whether this is the first packet after re-sync.
bool isSyncPacket = syncRequired;
// Sync sequence number and timestamp if required.
if (isSyncPacket)
{
if (packet->IsKeyFrame())
MS_DEBUG_TAG(rtp, "sync key frame received");
rtpSeqManager.Sync(packet->GetSequenceNumber());
rtpTimestampManager.Sync(packet->GetTimestamp());
// Calculate RTP timestamp diff between now and last sent RTP packet.
if (rtpStream->GetMaxPacketMs() != 0u)
{
auto now = DepLibUV::GetTime();
auto diffMs = now - rtpStream->GetMaxPacketMs();
auto diffTs = diffMs * rtpStream->GetClockRate() / 1000;
rtpTimestampManager.Offset(diffTs);
}
// TODO: Is this correct? syncRequired is taken by reference from the map.
// I assume that changing its value here also modifies its value in the map.
syncRequired = false;
}
// Update RTP seq number and timestamp.
uint16_t seq;
uint32_t timestamp;
rtpSeqManager.Input(packet->GetSequenceNumber(), seq);
rtpTimestampManager.Input(packet->GetTimestamp(), timestamp);
// Save original packet fields.
auto origSsrc = packet->GetSsrc();
auto origSeq = packet->GetSequenceNumber();
auto origTimestamp = packet->GetTimestamp();
// Rewrite packet.
packet->SetSsrc(this->rtpParameters.encodings[0].ssrc);
packet->SetSequenceNumber(seq);
packet->SetTimestamp(timestamp);
if (isSyncPacket)
{
MS_DEBUG_TAG(
rtp,
"sending sync packet [ssrc:%" PRIu32 ", seq:%" PRIu16 ", ts:%" PRIu32
"] from original [seq:%" PRIu16 ", ts:%" PRIu32 "]",
packet->GetSsrc(),
packet->GetSequenceNumber(),
packet->GetTimestamp(),
origSeq,
origTimestamp);
}
// Process the packet.
if (rtpStream->ReceivePacket(packet))
......@@ -122,10 +207,19 @@ namespace RTC
{
MS_WARN_TAG(
rtp,
"failed to send packet [ssrc:%" PRIu32 ", seq:%" PRIu16 "]",
"failed to send packet [ssrc:%" PRIu32 ", seq:%" PRIu16 ", ts:%" PRIu32
"] from original [seq:%" PRIu16 ", ts:%" PRIu32 "]",
packet->GetSsrc(),
packet->GetSequenceNumber());
packet->GetSequenceNumber(),
packet->GetTimestamp(),
origSeq,
origTimestamp);
}
// Restore packet fields.
packet->SetSsrc(origSsrc);
packet->SetSequenceNumber(origSeq);
packet->SetTimestamp(origTimestamp);
}
void PipeConsumer::GetRtcp(
......@@ -184,6 +278,16 @@ namespace RTC
{
MS_TRACE();
// TODO: Must get the associated RTP stream (based on the media ssrc of the
// feedback, and it may not exist so ignore it) and call:
// rtpStream->ReceiveKeyFrameRequest(messageType);
//
// NOTE: In order to match the ssrc take into account what is done below
// in ReceiveRtcpReceiverReport().
//
// TODO: This is impossible since this method does not receive the feedback
// message but just its messageType. The signature must be changed.
if (IsActive())
RequestKeyFrame();
}
......@@ -192,17 +296,15 @@ namespace RTC
{
MS_TRACE();
for (auto& kv : this->mapMappedSsrcRtpStream)
{
auto ssrc = kv.first;
auto& rtpStream = kv.second;
// TODO: Review this.
if (report->GetSsrc() == ssrc)
{
rtpStream->ReceiveRtcpReceiverReport(report);
auto it = this->mapMappedSsrcRtpStream.find(report->GetSsrc());
break;
}
if (it != this->mapMappedSsrcRtpStream.end())
{
auto* rtpStream = it->second;
rtpStream->ReceiveRtcpReceiverReport(report);
}
}
......@@ -229,6 +331,11 @@ namespace RTC
{
MS_TRACE();
for (auto& kv : this->mapRtpStreamSyncRequired)
{
kv.second = true;
}
if (IsActive())
{
for (auto& kv : this->mapMappedSsrcRtpStream)
......@@ -270,6 +377,11 @@ namespace RTC
{
MS_TRACE();
for (auto& kv : this->mapRtpStreamSyncRequired)
{
kv.second = true;
}
if (IsActive())
{
for (auto& kv : this->mapMappedSsrcRtpStream)
......@@ -363,8 +475,13 @@ namespace RTC
if (rtxCodec && encoding.hasRtx)
rtpStream->SetRtx(rtxCodec->payloadType, encoding.rtx.ssrc);
this->keyFrameSupported = RTC::Codecs::CanBeKeyFrame(mediaCodec->mimeType);
this->mapMappedSsrcRtpStream[encoding.ssrc] = rtpStream;
this->rtpStreams.push_back(rtpStream);
this->mapRtpStreamSyncRequired[rtpStream] = false;
this->mapRtpStreamRtpSeqManager[rtpStream];
this->mapRtpStreamRtpTimestampManager[rtpStream];
}
}
......@@ -375,9 +492,12 @@ namespace RTC
if (this->kind != RTC::Media::Kind::VIDEO)
return;
for (auto& encoding : this->rtpParameters.encodings)
// TODO: Review this (just call pipeConsumer.requestKeyFrame() in Node and
// verify that the sender browser receives a PLI for each simulcast stream).
for (auto& consumableRtpEncoding : this->consumableRtpEncodings)
{
auto mappedSsrc = encoding.ssrc;
auto mappedSsrc = consumableRtpEncoding.ssrc;
this->listener->OnConsumerKeyFrameRequested(this, mappedSsrc);
}
......
......@@ -21,12 +21,6 @@ namespace RTC
if (this->consumableRtpEncodings.size() != 1)
MS_THROW_TYPE_ERROR("invalid consumableRtpEncodings with size != 1");
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
this->maxRtcpInterval = RTC::RTCP::MaxVideoIntervalMs;
// Create RtpStreamSend instance for sending a single stream to the remote.
CreateRtpStream();
}
......@@ -432,7 +426,7 @@ namespace RTC
if (rtxCodec && encoding.hasRtx)
this->rtpStream->SetRtx(rtxCodec->payloadType, encoding.rtx.ssrc);
this->keyFrameSupported = Codecs::CanBeKeyFrame(mediaCodec->mimeType);
this->keyFrameSupported = RTC::Codecs::CanBeKeyFrame(mediaCodec->mimeType);
}
void SimpleConsumer::RequestKeyFrame()
......
......@@ -80,12 +80,6 @@ namespace RTC
this->producerRtpStreams.insert(
this->producerRtpStreams.begin(), this->consumableRtpEncodings.size(), nullptr);
// Set the RTCP report generation interval.
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
this->maxRtcpInterval = RTC::RTCP::MaxVideoIntervalMs;
// Create RtpStreamSend instance for sending a single stream to the remote.
CreateRtpStream();
}
......@@ -914,7 +908,7 @@ namespace RTC
if (rtxCodec && encoding.hasRtx)
this->rtpStream->SetRtx(rtxCodec->payloadType, encoding.rtx.ssrc);
this->keyFrameSupported = Codecs::CanBeKeyFrame(mediaCodec->mimeType);
this->keyFrameSupported = RTC::Codecs::CanBeKeyFrame(mediaCodec->mimeType);
this->encodingContext.reset(RTC::Codecs::GetEncodingContext(mediaCodec->mimeType));
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论