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

RtpListener: Lookup in RID table.

上级 593f861e
# TODO in mediasoup v2 (server-side)
* Properly set extended seq32 for nacked packets.
* Check NackGenerator intervals.
* Remove `producer.on('rtprawpacket')` and, instead, create a special `RtpConsumer` or something like that.
* Set a proper value for Producer::FullFrameRequestBlockTimeout (currently 1 second).
......
......@@ -31,18 +31,6 @@ namespace RTC
std::map<uint8_t, uint8_t> headerExtensionIds;
};
public:
// TODO: YES?
// RTP header extension ids that must be shared by all the Producers using
// the same Transport.
// NOTE: These ids are the original ids in the RTP packet (before the Producer
// maps them to the corresponding ids in the room).
struct HeaderExtensionIdsForTransport
{
uint8_t absSendTime{ 0 }; // 0 means no abs-send-time id.
uint8_t rid{ 0 }; // 0 means no RID id.
};
private:
struct HeaderExtensionIds
{
......@@ -77,6 +65,7 @@ namespace RTC
void SetRtpRawEvent(bool enabled);
void SetRtpObjectEvent(bool enabled);
const RTC::RtpParameters& GetParameters() const;
const struct RTC::Transport::HeaderExtensionIds& GetTransportHeaderExtensionIds() const;
bool IsPaused() const;
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
......@@ -124,6 +113,7 @@ namespace RTC
Timer* fullFrameRequestBlockTimer{ nullptr };
// Others.
std::vector<RtpEncodingParameters> outputEncodings;
struct RTC::Transport::HeaderExtensionIds transportHeaderExtensionIds;
struct HeaderExtensionIds headerExtensionIds;
bool paused{ false };
bool rtpRawEventEnabled{ false };
......@@ -151,6 +141,11 @@ namespace RTC
return this->rtpParameters;
}
inline const struct RTC::Transport::HeaderExtensionIds& Producer::GetTransportHeaderExtensionIds() const
{
return this->transportHeaderExtensionIds;
}
inline bool Producer::IsPaused() const
{
return this->paused;
......
......@@ -19,6 +19,7 @@ namespace RTC
Json::Value ToJson() const;
bool HasSsrc(uint32_t ssrc, const RTC::Producer* producer) const;
bool HasMuxId(const std::string& muxId, const RTC::Producer* producer) const;
bool HasRid(const std::string& rid, const RTC::Producer* producer) const;
void AddProducer(RTC::Producer* producer);
void RemoveProducer(const RTC::Producer* producer);
RTC::Producer* GetProducer(RTC::RtpPacket* packet);
......@@ -26,13 +27,18 @@ namespace RTC
private:
void RollbackProducer(
RTC::Producer* producer, std::vector<uint32_t>& previousSsrcs, std::string& previousMuxId);
RTC::Producer* producer,
std::vector<uint32_t>& previousSsrcs,
std::string& previousMuxId,
std::string& previousRid);
public:
// Table of SSRC / Producer pairs.
std::unordered_map<uint32_t, RTC::Producer*> ssrcTable;
// Table of MID RTP header extension / Producer pairs.
std::unordered_map<std::string, const RTC::Producer*> muxIdTable;
std::unordered_map<std::string, RTC::Producer*> muxIdTable;
// Table of RID RTP header extension / Producer pairs.
std::unordered_map<std::string, RTC::Producer*> ridTable;
};
/* Inline instance methods. */
......@@ -56,6 +62,16 @@ namespace RTC
else
return (it->second != producer);
}
inline bool RtpListener::HasRid(const std::string& rid, const RTC::Producer* producer) const
{
auto it = this->ridTable.find(rid);
if (it == this->ridTable.end())
return false;
else
return (it->second != producer);
}
} // namespace RTC
#endif
......@@ -114,6 +114,7 @@ namespace RTC
uint8_t* GetExtension(RTC::RtpHeaderExtensionUri::Type uri, uint8_t* len) const;
bool ReadAudioLevel(uint8_t* volume, bool* voice) const;
bool ReadAbsSendTime(uint32_t* time) const;
bool ReadRid(const uint8_t** data, size_t* len) const;
uint8_t* GetPayload() const;
size_t GetPayloadLength() const;
void Serialize(uint8_t* buffer);
......@@ -336,6 +337,22 @@ namespace RTC
return true;
}
inline bool RtpPacket::ReadRid(const uint8_t** data, size_t* len) const
{
uint8_t extenLen;
uint8_t* extenValue;
extenValue = GetExtension(RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID, &extenLen);
if (!extenValue || extenLen == 0)
return false;
*data = extenValue;
*len = static_cast<size_t>(extenLen);
return true;
}
inline uint8_t* RtpPacket::GetPayload() const
{
return this->payload;
......
......@@ -54,6 +54,17 @@ namespace RTC
RTC::Transport* transport, RTC::Consumer* consumer, RTC::RTCP::FeedbackPsPacket* packet) = 0;
};
public:
// RTP header extension ids that must be shared by all the Producers using
// the same Transport.
// NOTE: These ids are the original ids in the RTP packet (before the Producer
// maps them to the corresponding ids in the room).
struct HeaderExtensionIds
{
uint8_t absSendTime{ 0 }; // 0 means no abs-send-time id.
uint8_t rid{ 0 }; // 0 means no RID id.
};
public:
struct TransportOptions
{
......@@ -200,6 +211,7 @@ namespace RTC
RTC::DtlsTransport::Role dtlsLocalRole{ RTC::DtlsTransport::Role::AUTO };
// Others (RtpListener).
RtpListener rtpListener;
struct HeaderExtensionIds headerExtensionIds;
// Others (REMB and bitrate stuff).
std::unique_ptr<RTC::RemoteBitrateEstimatorAbsSendTime> remoteBitrateEstimator;
uint32_t maxBitrate{ 0 };
......
......@@ -471,7 +471,8 @@ namespace RTC
else
absSendTimeId = exten.id;
this->headerExtensionIds.absSendTime = absSendTimeId;
this->headerExtensionIds.absSendTime = absSendTimeId;
this->transportHeaderExtensionIds.absSendTime = exten.id;
}
if ((ridId == 0u) && exten.type == RTC::RtpHeaderExtensionUri::Type::RTP_STREAM_ID)
......@@ -481,7 +482,8 @@ namespace RTC
else
ridId = exten.id;
this->headerExtensionIds.rid = ridId;
this->headerExtensionIds.rid = ridId;
this->transportHeaderExtensionIds.rid = exten.id;
}
}
}
......
......@@ -16,10 +16,12 @@ namespace RTC
static const Json::StaticString JsonStringSsrcTable{ "ssrcTable" };
static const Json::StaticString JsonStringMuxIdTable{ "muxIdTable" };
static const Json::StaticString JsonStringRidTable{ "ridTable" };
Json::Value json(Json::objectValue);
Json::Value jsonSsrcTable(Json::objectValue);
Json::Value jsonMuxIdTable(Json::objectValue);
Json::Value jsonRidTable(Json::objectValue);
// Add ssrcTable.
for (auto& kv : this->ssrcTable)
......@@ -41,6 +43,16 @@ namespace RTC
}
json[JsonStringMuxIdTable] = jsonMuxIdTable;
// Add ridTable.
for (auto& kv : this->ridTable)
{
auto rid = kv.first;
auto producer = kv.second;
jsonRidTable[rid] = std::to_string(producer->producerId);
}
json[JsonStringRidTable] = jsonRidTable;
return json;
}
......@@ -54,6 +66,7 @@ namespace RTC
std::vector<uint32_t> previousSsrcs;
std::string previousMuxId;
std::string previousRid;
for (auto& kv : this->ssrcTable)
{
......@@ -77,6 +90,19 @@ namespace RTC
}
}
for (auto& kv : this->ridTable)
{
auto& rid = kv.first;
auto& existingProducer = kv.second;
if (existingProducer == producer)
{
previousRid = rid;
break;
}
}
// First remove from the listener tables all the entries pointing to
// the given Producer.
RemoveProducer(producer);
......@@ -100,7 +126,7 @@ namespace RTC
else
{
RemoveProducer(producer);
RollbackProducer(producer, previousSsrcs, previousMuxId);
RollbackProducer(producer, previousSsrcs, previousMuxId, previousRid);
MS_THROW_ERROR("ssrc already exists in RTP listener [ssrc:%" PRIu32 "]", ssrc);
}
......@@ -119,7 +145,7 @@ namespace RTC
else
{
RemoveProducer(producer);
RollbackProducer(producer, previousSsrcs, previousMuxId);
RollbackProducer(producer, previousSsrcs, previousMuxId, previousRid);
MS_THROW_ERROR("ssrc already exists in RTP listener [ssrc:%" PRIu32 "]", ssrc);
}
......@@ -138,7 +164,7 @@ namespace RTC
else
{
RemoveProducer(producer);
RollbackProducer(producer, previousSsrcs, previousMuxId);
RollbackProducer(producer, previousSsrcs, previousMuxId, previousRid);
MS_THROW_ERROR("ssrc already exists in RTP listener [ssrc:%" PRIu32 "]", ssrc);
}
......@@ -159,12 +185,35 @@ namespace RTC
else
{
RemoveProducer(producer);
RollbackProducer(producer, previousSsrcs, previousMuxId);
RollbackProducer(producer, previousSsrcs, previousMuxId, previousRid);
MS_THROW_ERROR("muxId already exists in RTP listener [muxId:'%s']", muxId.c_str());
}
}
}
// Add entries into ridTable.
{
for (auto& encoding : rtpParameters.encodings)
{
auto& rid = encoding.encodingId;
if (rid.empty())
continue;
if (!this->HasRid(rid, producer))
{
this->ridTable[rid] = producer;
}
else
{
RemoveProducer(producer);
RollbackProducer(producer, previousSsrcs, previousMuxId, previousRid);
MS_THROW_ERROR("rid already exists in RTP listener [rid:'%s']", rid.c_str());
}
}
}
}
void RtpListener::RemoveProducer(const RTC::Producer* producer)
......@@ -189,6 +238,14 @@ namespace RTC
else
++it;
}
for (auto it = this->ridTable.begin(); it != this->ridTable.end();)
{
if (it->second == producer)
it = this->ridTable.erase(it);
else
++it;
}
}
RTC::Producer* RtpListener::GetProducer(RTC::RtpPacket* packet)
......@@ -198,33 +255,40 @@ namespace RTC
// First lookup into the SSRC table.
{
auto it = this->ssrcTable.find(packet->GetSsrc());
if (it != this->ssrcTable.end())
{
auto producer = it->second;
auto& rtpParameters = producer->GetParameters();
auto producer = it->second;
return producer;
}
}
// Otherwise lookup into the muxId table.
// TODO: Do it.
// Otherwise lookup into the RID table.
{
const uint8_t* ridPtr;
size_t ridLen;
if (packet->ReadRid(&ridPtr, &ridLen))
{
auto* charRidPtr = const_cast<char*>(reinterpret_cast<const char*>(ridPtr));
std::string rid(charRidPtr, ridLen);
// Ensure the RTP PT is present in RtpParameters.
for (auto& codec : rtpParameters.codecs)
auto it = this->ridTable.find(rid);
if (it != this->ridTable.end())
{
// Check payloads.
if (codec.payloadType == packet->GetPayloadType())
return producer;
}
auto producer = it->second;
// RTP PT not present.
MS_WARN_TAG(rtp, "unknown RTP payloadType [payloadType:%" PRIu8 "]", packet->GetPayloadType());
// Fill the ssrc table.
this->ssrcTable[packet->GetSsrc()] = producer;
// TODO: We may emit "unhandledrtp" event.
return nullptr;
return producer;
}
}
}
// TODO: RID table? Sure.
// Otherwise lookup into the muxId table.
// TODO: do it.
// TODO: We may emit "unhandledrtp" event.
return nullptr;
}
......@@ -243,7 +307,10 @@ namespace RTC
}
void RtpListener::RollbackProducer(
RTC::Producer* producer, std::vector<uint32_t>& previousSsrcs, std::string& previousMuxId)
RTC::Producer* producer,
std::vector<uint32_t>& previousSsrcs,
std::string& previousMuxId,
std::string& previousRid)
{
MS_TRACE();
......@@ -254,5 +321,8 @@ namespace RTC
if (!previousMuxId.empty())
this->muxIdTable[previousMuxId] = producer;
if (!previousRid.empty())
this->ridTable[previousRid] = producer;
}
} // namespace RTC
......@@ -10,6 +10,7 @@
#include "RTC/Consumer.hpp"
#include "RTC/Producer.hpp"
#include "RTC/RTCP/FeedbackPsRemb.hpp"
#include "RTC/RtpDictionaries.hpp"
#include <cmath> // std::pow()
#include <iterator> // std::ostream_iterator
#include <sstream> // std::ostringstream
......@@ -19,9 +20,9 @@
static constexpr uint16_t IceCandidateDefaultLocalPriority{ 20000 };
static constexpr uint16_t IceCandidateLocalPriorityPreferFamilyIncrement{ 10000 };
static constexpr uint16_t IceCandidateLocalPriorityPreferProtocolIncrement{ 5000 };
// We just provide "host" candidates so `type preference` is fixed.
// We just provide "host" candidates so type preference is fixed.
static constexpr uint16_t IceTypePreference{ 64 };
// We do not support non rtcp-mux so `component` is always 1.
// We do not support non rtcp-mux so component is always 1.
static constexpr uint16_t IceComponent{ 1 };
/* Static helpers. */
......@@ -283,36 +284,40 @@ namespace RTC
static const Json::StaticString JsonStringConnecting{ "connecting" };
static const Json::StaticString JsonStringClosed{ "closed" };
static const Json::StaticString JsonStringFailed{ "failed" };
static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" };
static const Json::StaticString JsonStringAbsSendTime{ "absSendTime" };
static const Json::StaticString JsonStringRid{ "rid" };
static const Json::StaticString JsonStringUseRemb{ "useRemb" };
static const Json::StaticString JsonStringMaxBitrate{ "maxBitrate" };
static const Json::StaticString JsonStringEffectiveMaxBitrate{ "effectiveMaxBitrate" };
static const Json::StaticString JsonStringRtpListener{ "rtpListener" };
Json::Value json(Json::objectValue);
Json::Value jsonHeaderExtensionIds(Json::objectValue);
json[JsonStringTransportId] = Json::UInt{ this->transportId };
// Add `iceRole` (we are always "controlled").
// Add iceRole (we are always "controlled").
json[JsonStringIceRole] = JsonStringControlled;
// Add `iceLocalParameters`.
// Add iceLocalParameters.
json[JsonStringIceLocalParameters][JsonStringUsernameFragment] =
this->iceServer->GetUsernameFragment();
json[JsonStringIceLocalParameters][JsonStringPassword] = this->iceServer->GetPassword();
json[JsonStringIceLocalParameters][JsonStringIceLite] = true;
// Add `iceLocalCandidates`.
// Add iceLocalCandidates.
json[JsonStringIceLocalCandidates] = Json::arrayValue;
for (const auto& iceCandidate : this->iceLocalCandidates)
{
json[JsonStringIceLocalCandidates].append(iceCandidate.ToJson());
}
// Add `iceSelectedTuple`.
// Add iceSelectedTuple.
if (this->selectedTuple != nullptr)
json[JsonStringIceSelectedTuple] = this->selectedTuple->ToJson();
// Add `iceState`.
// Add iceState.
switch (this->iceServer->GetState())
{
case RTC::IceServer::IceState::NEW:
......@@ -329,11 +334,11 @@ namespace RTC
break;
}
// Add `dtlsLocalParameters.fingerprints`.
// Add dtlsLocalParameters.fingerprints.
json[JsonStringDtlsLocalParameters][JsonStringFingerprints] =
RTC::DtlsTransport::GetLocalFingerprints();
// Add `dtlsLocalParameters.role`.
// Add dtlsLocalParameters.role.
switch (this->dtlsLocalRole)
{
case RTC::DtlsTransport::Role::AUTO:
......@@ -349,7 +354,7 @@ namespace RTC
MS_ABORT("invalid local DTLS role");
}
// Add `dtlsState`.
// Add dtlsState.
switch (this->dtlsTransport->GetState())
{
case DtlsTransport::DtlsState::NEW:
......@@ -369,16 +374,26 @@ namespace RTC
break;
}
// Add `useRemb`.
// Add headerExtensionIds.
if (this->headerExtensionIds.absSendTime != 0u)
jsonHeaderExtensionIds[JsonStringAbsSendTime] = this->headerExtensionIds.absSendTime;
if (this->headerExtensionIds.rid != 0u)
jsonHeaderExtensionIds[JsonStringRid] = this->headerExtensionIds.rid;
json[JsonStringHeaderExtensionIds] = jsonHeaderExtensionIds;
// Add useRemb.
json[JsonStringUseRemb] = (static_cast<bool>(this->remoteBitrateEstimator));
// Add `maxBitrate`.
// Add maxBitrate.
json[JsonStringMaxBitrate] = Json::UInt{ this->maxBitrate };
// Add `effectiveMaxBitrate`.
// Add effectiveMaxBitrate.
json[JsonStringEffectiveMaxBitrate] = Json::UInt{ this->effectiveMaxBitrate };
// Add `rtpListener`.
// Add rtpListener.
json[JsonStringRtpListener] = this->rtpListener.ToJson();
return json;
......@@ -421,6 +436,19 @@ namespace RTC
// Add us as listener.
producer->AddListener(this);
// Take the transport related RTP header extension ids of the Producer
// and add them to the Transport.
if (producer->GetTransportHeaderExtensionIds().absSendTime != 0u)
{
this->headerExtensionIds.absSendTime = producer->GetTransportHeaderExtensionIds().absSendTime;
}
if (producer->GetTransportHeaderExtensionIds().rid != 0u)
{
this->headerExtensionIds.rid = producer->GetTransportHeaderExtensionIds().rid;
}
}
void Transport::HandleConsumer(RTC::Consumer* consumer)
......@@ -1099,6 +1127,26 @@ namespace RTC
return;
}
// Apply the Transport RTP header extension ids so the RTP listener can use them.
if (this->headerExtensionIds.rid != 0u)
{
packet->AddExtensionMapping(
RtpHeaderExtensionUri::Type::RTP_STREAM_ID, this->headerExtensionIds.rid);
}
// TODO: test getting RID.
// const uint8_t* rid;
// size_t ridLen;
// if (packet->ReadRid(&rid, &ridLen))
// {
// std::string ridStr((const char*)rid, ridLen);
// MS_ERROR("---------- HABEMUS RID !!!: %s", ridStr.c_str());
// }
// Get the associated Producer.
RTC::Producer* producer = this->rtpListener.GetProducer(packet);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论