提交 e9333d02 authored 作者: José Luis Millán's avatar José Luis Millán 提交者: GitHub

RTX (#120)

* Apply const correctness on RTC::Parameters

* RTP retransmissions implementation (RFC 4588)

* Style fixes

* Remove unnecesary check in RTC::RtpPacket

* Remove duplicated code in RTC::RtpStreamRecv

* Make RTC::RtpPacket::RtxDecode return a boolean to avoid external checks

* Remove unused instance member in favour of a local variable

* Make RTC::RtpStreamRecv::SetHeaderExtensions method private
上级 924a689d
......@@ -418,6 +418,9 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
// SSRCs.
objMedia.ssrcs = [];
// SSRC groups.
objMedia.ssrcGroups = [];
for (let rtpSender of self._peer.rtpSenders)
{
// Just the same kind.
......@@ -443,6 +446,31 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
attribute : 'msid',
value : rtpSender.rtpParameters.userParameters.msid
});
// RTX.
if (encoding.rtx !== undefined && encoding.rtx.ssrc !== 0)
{
objMedia.ssrcs.push(
{
id : encoding.rtx.ssrc,
attribute : 'cname',
value : rtpSender.rtpParameters.rtcp.cname
});
objMedia.ssrcs.push(
{
id : encoding.rtx.ssrc,
attribute : 'msid',
value : rtpSender.rtpParameters.userParameters.msid
});
// Associate original and retransmission SSRC.
objMedia.ssrcGroups.push(
{
semantics : 'FID',
ssrcs : [ encoding.ssrc, encoding.rtx.ssrc ].join(' ')
});
}
}
}
......@@ -480,7 +508,23 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
_createRtpReceiverForSsrc(parsedMedia, mediaSsrcs, trackInfo)
{
// TODO: We assume no RTX/FEC in client's SDP.
// TODO: We assume no FEC in client's SDP.
let hasRtx = false;
if (Array.isArray(parsedMedia.ssrcGroups))
{
for (let group of parsedMedia.ssrcGroups)
{
switch (group.semantics)
{
case 'FID':
{
hasRtx = true;
break;
}
}
}
}
if (!Array.isArray(mediaSsrcs))
mediaSsrcs = [ mediaSsrcs ];
......@@ -555,14 +599,15 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
if (codecPayloadType)
{
for (let ssrc of mediaSsrcs)
{
encodings.push(
{
ssrc : ssrc,
codecPayloadType : codecPayloadType
});
}
let encoding = {
ssrc : mediaSsrcs[0],
codecPayloadType : codecPayloadType
};
if (hasRtx && mediaSsrcs.length >= 2)
encoding.rtx = { ssrc: mediaSsrcs[1] };
encodings.push(encoding);
}
// Array of RtpHeaderExtensionParameters.
......
......@@ -415,32 +415,38 @@ class RTCPeerConnectionUnifiedPlan extends RTCPeerConnectionCommon
}
}
let mediaSsrc;
let cname;
let msid = parsedMedia.msid;
let hasRtx = false;
// Take the first RTCP CNAME found.
for (let ssrc of parsedMedia.ssrcs)
if (Array.isArray(parsedMedia.ssrcGroups))
{
if (ssrc.attribute === 'cname')
for (let group of parsedMedia.ssrcGroups)
{
mediaSsrc = ssrc.id;
cname = ssrc.value;
break;
switch (group.semantics)
{
case 'FID':
{
hasRtx = true;
break;
}
}
}
}
// Array of RtpEncodingParameters.
// NOTE: Just a single encoding will be created with the first media codec
// in the answer.
let encodings =
[
{
ssrc : mediaSsrc,
let encodings = [];
let encoding =
{
ssrc : parsedMedia.ssrcs[0].id,
codecPayloadType : mapCodecs.values().next().value.payloadType
}
];
}
if (hasRtx && parsedMedia.ssrcs.length >= 2)
encoding.rtx = { ssrc: parsedMedia.ssrcs[1].id };
encodings.push(encoding);
// Array of RtpHeaderExtensionParameters.
let headerExtensions = sdpUtils.descToRtpHeaderExtensionParameters(parsedMedia);
......@@ -457,11 +463,11 @@ class RTCPeerConnectionUnifiedPlan extends RTCPeerConnectionCommon
headerExtensions : headerExtensions,
rtcp :
{
cname : cname
cname : parsedMedia.ssrcs[0].value
},
userParameters :
{
msid : msid
msid : parsedMedia.msid
}
};
......
......@@ -278,6 +278,8 @@ class RTCRtpTransceiver
{
objMedia.ssrcs = [];
objMedia.ssrcGroups = [];
// a=msid attribute (used by Firefox).
if (this._sender.rtpParameters.userParameters.msid)
objMedia.msid = this._sender.rtpParameters.userParameters.msid;
......@@ -290,6 +292,24 @@ class RTCRtpTransceiver
attribute : 'cname',
value : this._sender.rtpParameters.rtcp.cname
});
// RTX.
if (encoding.rtx !== undefined && encoding.rtx.ssrc !== 0)
{
objMedia.ssrcs.push(
{
id : encoding.rtx.ssrc,
attribute : 'cname',
value : this._sender.rtpParameters.rtcp.cname
});
// Associate original and retransmission SSRC.
objMedia.ssrcGroups.push(
{
semantics : 'FID',
ssrcs : [encoding.ssrc, encoding.rtx.ssrc].join(' ')
});
}
}
}
......
......@@ -22,7 +22,6 @@ module.exports =
// Ignore feature codecs.
switch (rtp.codec.toLowerCase())
{
case 'rtx':
case 'ulpfec':
case 'flexfec':
case 'red':
......@@ -196,15 +195,16 @@ module.exports =
},
/**
* This assumes no RTX/FEC.
* This assumes no FEC.
*/
getPlanBSsrcs(parsedMedia)
{
let singleSsrcs = new Set();
let simulcastSsrcs = new Set();
let rtxSsrcs = new Set();
let ssrcs = [];
// Simulcast.
// Simulcast/RTX.
if (Array.isArray(parsedMedia.ssrcGroups))
{
for (let group of parsedMedia.ssrcGroups)
......@@ -226,6 +226,22 @@ module.exports =
break;
}
case 'FID':
{
let rtx = [];
for (let ssrc of group.ssrcs.split(' '))
{
ssrc = Number(ssrc);
rtxSsrcs.add(ssrc);
rtx.push(ssrc);
}
ssrcs.push(rtx);
break;
}
}
}
}
......@@ -237,7 +253,7 @@ module.exports =
{
let ssrc = ssrcObj.id;
if (simulcastSsrcs.has(ssrc) || singleSsrcs.has(ssrc))
if (simulcastSsrcs.has(ssrc) || singleSsrcs.has(ssrc) || rtxSsrcs.has(ssrc))
continue;
singleSsrcs.add(ssrc);
......
......@@ -63,24 +63,24 @@ namespace RTC
Json::Value ToJson() const;
void Set(Json::Value& data);
bool HasBoolean(std::string& key);
bool HasInteger(std::string& key);
bool HasDouble(std::string& key);
bool HasString(std::string& key);
bool HasArrayOfIntegers(std::string& key);
bool IncludesInteger(std::string& key, int32_t integer);
bool GetBoolean(std::string& key);
int32_t GetInteger(std::string& key);
double GetDouble(std::string& key);
std::string& GetString(std::string& key);
std::vector<int32_t>& GetArrayOfIntegers(std::string& key);
void SetBoolean(std::string& key, bool booleanValue);
void SetInteger(std::string& key, int32_t integerValue);
void SetDouble(std::string& key, double doubleValue);
void SetString(std::string& key, std::string& stringValue);
void SetArrayOfIntegers(std::string& key, std::vector<int32_t>& arrayOfIntegers);
bool HasBoolean(const std::string& key);
bool HasInteger(const std::string& key);
bool HasDouble(const std::string& key);
bool HasString(const std::string& key);
bool HasArrayOfIntegers(const std::string& key);
bool IncludesInteger(const std::string& key, int32_t integer);
bool GetBoolean(const std::string& key);
int32_t GetInteger(const std::string& key);
double GetDouble(const std::string& key);
std::string& GetString(const std::string& key);
std::vector<int32_t>& GetArrayOfIntegers(const std::string& key);
void SetBoolean(const std::string& key, bool booleanValue);
void SetInteger(const std::string& key, int32_t integerValue);
void SetDouble(const std::string& key, double doubleValue);
void SetString(const std::string& key, std::string& stringValue);
void SetArrayOfIntegers(const std::string& key, std::vector<int32_t>& arrayOfIntegers);
private:
std::unordered_map<std::string, Value> mapKeyValues;
......@@ -88,7 +88,7 @@ namespace RTC
/* Inline methods. */
inline bool Parameters::HasBoolean(std::string& key)
inline bool Parameters::HasBoolean(const std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -96,7 +96,7 @@ namespace RTC
return this->mapKeyValues[key].type == Value::Type::BOOLEAN;
}
inline bool Parameters::HasInteger(std::string& key)
inline bool Parameters::HasInteger(const std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -104,7 +104,7 @@ namespace RTC
return this->mapKeyValues[key].type == Value::Type::INTEGER;
}
inline bool Parameters::HasDouble(std::string& key)
inline bool Parameters::HasDouble(const std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -112,7 +112,7 @@ namespace RTC
return this->mapKeyValues[key].type == Value::Type::DOUBLE;
}
inline bool Parameters::HasString(std::string& key)
inline bool Parameters::HasString(const std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -120,7 +120,7 @@ namespace RTC
return this->mapKeyValues[key].type == Value::Type::STRING;
}
inline bool Parameters::HasArrayOfIntegers(std::string& key)
inline bool Parameters::HasArrayOfIntegers(const std::string& key)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -128,7 +128,7 @@ namespace RTC
return this->mapKeyValues[key].type == Value::Type::ARRAY_OF_INTEGERS;
}
inline bool Parameters::IncludesInteger(std::string& key, int32_t integer)
inline bool Parameters::IncludesInteger(const std::string& key, int32_t integer)
{
if (this->mapKeyValues.find(key) == this->mapKeyValues.end())
return false;
......@@ -138,27 +138,27 @@ namespace RTC
return std::find(array.begin(), array.end(), integer) != array.end();
}
inline void Parameters::SetBoolean(std::string& key, bool booleanValue)
inline void Parameters::SetBoolean(const std::string& key, bool booleanValue)
{
this->mapKeyValues[key] = Value(booleanValue);
}
inline void Parameters::SetInteger(std::string& key, int32_t integerValue)
inline void Parameters::SetInteger(const std::string& key, int32_t integerValue)
{
this->mapKeyValues[key] = Value(integerValue);
}
inline void Parameters::SetDouble(std::string& key, double doubleValue)
inline void Parameters::SetDouble(const std::string& key, double doubleValue)
{
this->mapKeyValues[key] = Value(doubleValue);
}
inline void Parameters::SetString(std::string& key, std::string& stringValue)
inline void Parameters::SetString(const std::string& key, std::string& stringValue)
{
this->mapKeyValues[key] = Value(stringValue);
}
inline void Parameters::SetArrayOfIntegers(std::string& key, std::vector<int32_t>& arrayOfIntegers)
inline void Parameters::SetArrayOfIntegers(const std::string& key, std::vector<int32_t>& arrayOfIntegers)
{
this->mapKeyValues[key] = Value(arrayOfIntegers);
}
......
......@@ -89,6 +89,7 @@ namespace RTC
Timer* audioLevelsTimer{ nullptr };
// Others.
RTC::RtpCapabilities capabilities;
std::unordered_map<uint8_t, RTC::RtpCodecParameters> mapPayloadRtxCodecParameters;
std::unordered_map<uint32_t, RTC::Peer*> peers;
std::unordered_map<const RTC::RtpReceiver*, std::unordered_set<RTC::RtpSender*>> mapRtpReceiverRtpSenders;
std::unordered_map<const RTC::RtpSender*, const RTC::RtpReceiver*> mapRtpSenderRtpReceiver;
......
......@@ -96,7 +96,7 @@ namespace RTC
return !(*this == other);
}
void SetName(std::string& name);
void SetName(const std::string& name);
const std::string& GetName() const
{
......@@ -307,6 +307,7 @@ namespace RTC
void ReduceCodecsAndEncodings(RtpCapabilities& capabilities);
void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions);
RTC::RtpCodecParameters& GetCodecForEncoding(RtpEncodingParameters& encoding);
RTC::RtpCodecParameters& GetRtxCodecForEncoding(RtpEncodingParameters& encoding);
private:
void ValidateCodecs();
......
......@@ -117,6 +117,8 @@ namespace RTC
size_t GetPayloadLength() const;
void Serialize(uint8_t* buffer);
RtpPacket* Clone(uint8_t* buffer) const;
void RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq);
bool RtxDecode(uint8_t payloadType, uint32_t ssrc);
private:
void ParseExtensions();
......
......@@ -79,6 +79,7 @@ namespace RTC
// Allocated by this.
RTC::RtpParameters* rtpParameters{ nullptr };
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> rtxStreamMap;
// Others.
bool rtpRawEventEnabled{ false };
bool rtpObjectEventEnabled{ false };
......
......@@ -85,8 +85,7 @@ namespace RTC
uint16_t maxRtcpInterval{ 0 };
// RTP counters.
RTC::RtpDataCounter transmittedCounter;
// TODO: keep track of retransmitted data too.
// RTC::RtpDataCounter retransmitted;
RTC::RtpDataCounter retransmittedCounter;
};
/* Inline methods. */
......@@ -129,7 +128,7 @@ namespace RTC
inline uint32_t RtpSender::GetTransmissionRate(uint64_t now)
{
return this->transmittedCounter.GetRate(now);
return this->transmittedCounter.GetRate(now) + this->retransmittedCounter.GetRate(now);
}
} // namespace RTC
......
......@@ -25,12 +25,15 @@ namespace RTC
Json::Value ToJson() const override;
bool ReceivePacket(RTC::RtpPacket* packet) override;
bool ReceiveRtxPacket(RTC::RtpPacket* packet);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void SetRtx(uint8_t payloadType, uint32_t ssrc);
void RequestFullFrame();
private:
void CalculateJitter(uint32_t rtpTimestamp);
void SetHeaderExtensions(RTC::RtpPacket* packet) const;
/* Pure virtual methods inherited from RtpStream. */
protected:
......@@ -52,6 +55,11 @@ namespace RTC
uint32_t transit{ 0 }; // Relative trans time for prev pkt.
uint32_t jitter{ 0 }; // Estimated jitter.
std::unique_ptr<RTC::NackGenerator> nackGenerator;
// RTX related.
bool hasRtx { false };
uint8_t rtxPayloadType{ 0 };
uint32_t rtxSsrc{ 0 };
};
} // namespace RTC
......
......@@ -36,6 +36,9 @@ namespace RTC
uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now);
uint32_t GetRtt() const;
void SetRtx(uint8_t payloadType, uint32_t ssrc);
bool HasRtx() const;
void RtxEncode(RtpPacket* packet);
private:
void ClearBuffer();
......@@ -55,12 +58,23 @@ namespace RTC
uint64_t lastPacketTimeMs{ 0 }; // Time (MS) when the last packet was received.
uint32_t lastPacketRtpTimestamp{ 0 }; // RTP Timestamp of the last packet.
uint32_t rtt{ 0 }; // Round trip time.
// RTX related.
bool hasRtx { false };
uint8_t rtxPayloadType{ 0 };
uint32_t rtxSsrc{ 0 };
uint16_t rtxSeq{ 0 };
};
inline uint32_t RtpStreamSend::GetRtt() const
{
return this->rtt;
}
inline bool RtpStreamSend::HasRtx() const
{
return this->hasRtx;
}
} // namespace RTC
#endif
......@@ -477,6 +477,44 @@ namespace RTC
MS_THROW_ERROR("no more available dynamic payload types for given media codecs");
}
// Build the RTX codec parameters for the video codec.
if (mediaCodec.kind == RTC::Media::Kind::VIDEO)
{
RTC::RtpCodecParameters rtxCodec;
// Get the next available payload type.
while (dynamicPayloadTypeIt != DynamicPayloadTypes.end())
{
uint8_t payloadType = *dynamicPayloadTypeIt;
++dynamicPayloadTypeIt;
if (roomPayloadTypes.find(payloadType) == roomPayloadTypes.end())
{
// Assign PT.
rtxCodec.payloadType = payloadType;
rtxCodec.hasPayloadType = true;
break;
}
}
// If no one found, throw.
if (!rtxCodec.hasPayloadType)
MS_THROW_ERROR("no more available dynamic payload types for given RTX codec");
static const std::string associatedPayloadType = "apt";
static const std::string videoRtx = "video/rtx";
rtxCodec.kind = RTC::Media::Kind::VIDEO;
rtxCodec.mime.SetName(videoRtx);
rtxCodec.clockRate = mediaCodec.clockRate;
rtxCodec.parameters.SetInteger(associatedPayloadType, mediaCodec.payloadType);
// Associate the RTX codec with the original's payload type.
this->mapPayloadRtxCodecParameters[mediaCodec.payloadType] = rtxCodec;
}
// Store the selected PT.
roomPayloadTypes.insert(mediaCodec.payloadType);
......@@ -524,6 +562,8 @@ namespace RTC
{
MS_TRACE();
std::vector<RTC::RtpCodecParameters> rtxCodecs;
// Remove those peer's capabilities not supported by the room.
// Remove unsupported codecs and set the same PT.
......@@ -545,6 +585,20 @@ namespace RTC
// Remove the unsupported RTCP feedback from the given codec.
peerCodecCapability.ReduceRtcpFeedback(roomCodecCapability.rtcpFeedback);
// Add RTX codec parameters.
// TODO: We should not add rtx codecs in case the peer does not support it.
// Must be done by the time we generate answers.
if (it->kind == RTC::Media::Kind::VIDEO)
{
auto payloadType = peerCodecCapability.payloadType;
MS_ASSERT(this->mapPayloadRtxCodecParameters.find(payloadType) != this->mapPayloadRtxCodecParameters.end(), "missing RTX codec parameters");
auto& rtxCodec = this->mapPayloadRtxCodecParameters[payloadType];
rtxCodecs.push_back(rtxCodec);
}
break;
}
}
......@@ -555,6 +609,14 @@ namespace RTC
it = capabilities->codecs.erase(it);
}
// Add the RTX codecs.
for (auto it = rtxCodecs.begin(); it != rtxCodecs.end(); ++it)
{
auto& codec = *it;
capabilities->codecs.push_back(codec);
}
// Remove unsupported header extensions.
capabilities->ReduceHeaderExtensions(this->capabilities.headerExtensions);
......
......@@ -146,7 +146,7 @@ namespace RTC
}
}
bool Parameters::GetBoolean(std::string& key)
bool Parameters::GetBoolean(const std::string& key)
{
MS_TRACE();
......@@ -158,7 +158,7 @@ namespace RTC
return this->mapKeyValues[key].booleanValue;
}
int32_t Parameters::GetInteger(std::string& key)
int32_t Parameters::GetInteger(const std::string& key)
{
MS_TRACE();
......@@ -170,7 +170,7 @@ namespace RTC
return this->mapKeyValues[key].integerValue;
}
double Parameters::GetDouble(std::string& key)
double Parameters::GetDouble(const std::string& key)
{
MS_TRACE();
......@@ -182,7 +182,7 @@ namespace RTC
return this->mapKeyValues[key].doubleValue;
}
std::string& Parameters::GetString(std::string& key)
std::string& Parameters::GetString(const std::string& key)
{
MS_TRACE();
......@@ -194,7 +194,7 @@ namespace RTC
return this->mapKeyValues[key].stringValue;
}
std::vector<int32_t>& Parameters::GetArrayOfIntegers(std::string& key)
std::vector<int32_t>& Parameters::GetArrayOfIntegers(const std::string& key)
{
MS_TRACE();
......
......@@ -77,7 +77,7 @@ namespace RTC
/* Instance methods. */
void RtpCodecMime::SetName(std::string& name)
void RtpCodecMime::SetName(const std::string& name)
{
MS_TRACE();
......
......@@ -271,6 +271,30 @@ namespace RTC
return fakeCodec;
}
RTC::RtpCodecParameters& RtpParameters::GetRtxCodecForEncoding(RtpEncodingParameters& encoding)
{
MS_TRACE();
static const std::string associatedPayloadType = "apt";
static RTC::RtpCodecParameters fakeCodec;
uint8_t payloadType = encoding.codecPayloadType;
auto it = this->codecs.begin();
for (; it != this->codecs.end(); ++it)
{
auto& codec = *it;
if (codec.mime.IsFeatureCodec() &&
codec.parameters.GetInteger(associatedPayloadType) == payloadType)
{
return codec;
}
}
return fakeCodec;
}
inline void RtpParameters::ValidateCodecs()
{
MS_TRACE();
......
......@@ -111,9 +111,6 @@ namespace RTC
payloadLength -= size_t{ payloadPadding };
}
if (payloadLength == 0)
payload = nullptr;
MS_ASSERT(
len == sizeof(Header) + csrcListSize + (extensionHeader ? 4 + extensionValueSize : 0) +
payloadLength + static_cast<size_t>(payloadPadding),
......@@ -169,6 +166,10 @@ namespace RTC
MS_DUMP(" timestamp : %" PRIu32, GetTimestamp());
MS_DUMP(" ssrc : %" PRIu32, GetSsrc());
MS_DUMP(" payload size : %zu bytes", GetPayloadLength());
if (this->header->padding)
{
MS_DUMP(" padding size : %" PRIu8 " bytes", this->payloadPadding);
}
MS_DUMP("</RtpPacket>");
}
......@@ -301,6 +302,65 @@ namespace RTC
return packet;
}
void RtpPacket::RtxEncode(uint8_t payloadType, uint32_t ssrc, uint16_t seq)
{
MS_TRACE();
// Rewrite the payload type.
this->SetPayloadType(payloadType);
// Rewrite the SSRC.
this->SetSsrc(ssrc);
// Write the original sequence number at the begining of the payload.
std::memmove(this->payload+2, this->payload, this->payloadLength);
Utils::Byte::Set2Bytes(this->payload, 0, this->GetSequenceNumber());
// Rewrite the sequence number.
this->SetSequenceNumber(seq);
// Fix the payload length.
this->payloadLength += 2;
// Fix the packet size.
this->size += 2;
// Remove padding.
this->payloadPadding = 0u;
}
bool RtpPacket::RtxDecode(uint8_t payloadType, uint32_t ssrc)
{
MS_TRACE();
if (this->payloadLength < 2)
{
MS_WARN_TAG(rtx, "not enough space for a RTX header field");
return false;
}
// Rewrite the payload type.
this->SetPayloadType(payloadType);
// Rewrite the SSRC.
this->SetSsrc(ssrc);
// Rewrite the sequence number.
this->SetSequenceNumber(Utils::Byte::Get2Bytes(this->payload, 0));
// Fix the payload and payload length.
this->payload += 2;
this->payloadLength -= 2;
// Fix the packet size.
this->size -= 2;
// Remove padding.
this->payloadPadding = 0u;
return true;
}
void RtpPacket::ParseExtensions()
{
MS_TRACE();
......
......@@ -247,20 +247,36 @@ namespace RTC
// Find the corresponding RtpStreamRecv.
uint32_t ssrc = packet->GetSsrc();
RTC::RtpStreamRecv* rtpStream { nullptr };
if (this->rtpStreams.find(ssrc) == this->rtpStreams.end())
{
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
if (this->rtxStreamMap.find(ssrc) == this->rtxStreamMap.end())
{
return;
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
return;
}
else
{
rtpStream = this->rtxStreamMap[ssrc];
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
return;
}
}
auto rtpStream = this->rtpStreams[ssrc];
else
{
rtpStream = this->rtpStreams[ssrc];
// Process the packet.
// TODO: Must check what kind of packet we are checking. For example, RTX
// packets (once implemented) should have a different handling.
if (!rtpStream->ReceivePacket(packet))
return;
// Process the packet.
if (!rtpStream->ReceivePacket(packet))
return;
}
// Notify the listener.
this->listener->OnRtpPacket(this, packet);
......@@ -443,6 +459,23 @@ namespace RTC
// Enable REMB in the transport if requested.
if (useRemb)
this->transport->EnableRemb();
// Check rtx capabilities.
if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{
if (this->rtxStreamMap.find(encoding.rtx.ssrc) != this->rtxStreamMap.end())
return;
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
if (codec.hasPayloadType)
{
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
this->rtxStreamMap[encoding.rtx.ssrc] = rtpStream;
}
}
}
void RtpReceiver::ClearRtpStreams()
......
......@@ -320,15 +320,13 @@ namespace RTC
}
// Process the packet.
// TODO: Must check what kind of packet we are checking. For example, RTX
// packets (once implemented) should have a different handling.
if (!this->rtpStream->ReceivePacket(packet))
return;
// Send the packet.
this->transport->SendRtpPacket(packet);
// Save RTP data.
// Update transmitted RTP data counter.
this->transmittedCounter.Update(packet);
}
......@@ -466,6 +464,16 @@ namespace RTC
this->rtpStream = new RTC::RtpStreamSend(params, 750);
else
this->rtpStream = new RTC::RtpStreamSend(params, 0);
if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{
auto& codec = this->rtpParameters->GetRtxCodecForEncoding(encoding);
if (codec.hasPayloadType)
{
this->rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
}
}
}
void RtpSender::RetransmitRtpPacket(RTC::RtpPacket* packet)
......@@ -475,14 +483,41 @@ namespace RTC
if (!this->GetActive())
return;
// If the peer supports RTX create a RTX packet and insert the given media
// packet as payload. Otherwise just send the packet as usual.
// TODO: No RTX for now so just send as usual.
MS_ASSERT(this->rtpStream, "no RtpStream set");
RTC::RtpPacket* rtxPacket = { nullptr };
if (this->rtpStream->HasRtx())
{
static uint8_t RtxBuffer[MtuSize];
rtxPacket = packet->Clone(RtxBuffer);
this->rtpStream->RtxEncode(rtxPacket);
MS_DEBUG_TAG(rtx, "sending rtx packet [ssrc: %" PRIu32 " seqnr: %" PRIu16 "] recovering original [ssrc: %" PRIu32 " seqnr: %" PRIu16 "]",
rtxPacket->GetSsrc(),
rtxPacket->GetSequenceNumber(),
packet->GetSsrc(),
packet->GetSequenceNumber());
}
else
{
rtxPacket = packet;
MS_DEBUG_TAG(rtx, "retransmitting packet [ssrc: %" PRIu32 " seqnr: %" PRIu16 "]",
rtxPacket->GetSsrc(),
rtxPacket->GetSequenceNumber());
}
// Update retransmitted RTP data counter.
this->retransmittedCounter.Update(rtxPacket);
// Send the packet.
this->transport->SendRtpPacket(packet);
this->transport->SendRtpPacket(rtxPacket);
// Delete the RTX RtpPacket if it was created.
if (rtxPacket != packet)
delete rtxPacket;
}
inline void RtpSender::EmitActiveChange() const
......
......@@ -53,18 +53,59 @@ namespace RTC
CalculateJitter(packet->GetTimestamp());
// Set RTP header extension ids.
if (this->params.ssrcAudioLevelId != 0u)
this->SetHeaderExtensions(packet);
// Pass the packet to the NackGenerator.
if (this->params.useNack)
this->nackGenerator->ReceivePacket(packet);
return true;
}
bool RtpStreamRecv::ReceiveRtxPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
MS_ASSERT(packet->GetSsrc() == this->rtxSsrc, "invalid ssrc on rtx packet");
// Check that the payload type corresponds to the one negotiated.
if (packet->GetPayloadType() != this->rtxPayloadType)
{
packet->AddExtensionMapping(
RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL, this->params.ssrcAudioLevelId);
MS_WARN_TAG(rtx, "ignoring rtx packet with invalid payload type [ssrc: %" PRIu32 " seqnr: %" PRIu16 " payload type: %" PRIu8 "]",
packet->GetSsrc(),
packet->GetSequenceNumber(),
packet->GetPayloadType());
return false;
}
if (this->params.absSendTimeId != 0u)
// Get the rtx packet sequence number for logging purposes.
auto rtxSeq = packet->GetSequenceNumber();
// Get the original rtp packet.
if (!packet->RtxDecode(this->params.payloadType, this->params.ssrc))
{
packet->AddExtensionMapping(
RtpHeaderExtensionUri::Type::ABS_SEND_TIME, this->params.absSendTimeId);
MS_WARN_TAG(rtx, "ignoring malformed rtx packet [ssrc: %" PRIu32 " seqnr: %" PRIu16 " payload type: %" PRIu8 "]",
packet->GetSsrc(),
packet->GetSequenceNumber(),
packet->GetPayloadType());
return false;
}
MS_DEBUG_TAG(rtx, "received rtx packet [ssrc: %" PRIu32 " seqnr: %" PRIu16 "] recovering original [ssrc: %" PRIu32 " seqnr: %" PRIu16 "]",
this->rtxSsrc,
rtxSeq,
packet->GetSsrc(),
packet->GetSequenceNumber());
// Set the extended sequence number into the packet.
packet->SetExtendedSequenceNumber(
this->cycles + static_cast<uint32_t>(packet->GetSequenceNumber()));
// Set RTP header extension ids.
this->SetHeaderExtensions(packet);
// Pass the packet to the NackGenerator.
if (this->params.useNack)
this->nackGenerator->ReceivePacket(packet);
......@@ -215,4 +256,30 @@ namespace RTC
this->listener->OnPliRequired(this);
}
void RtpStreamRecv::SetRtx(uint8_t payloadType, uint32_t ssrc)
{
MS_TRACE();
this->hasRtx = true;
this->rtxPayloadType = payloadType;
this->rtxSsrc = ssrc;
}
void RtpStreamRecv::SetHeaderExtensions(RTC::RtpPacket* packet) const
{
MS_TRACE();
if (this->params.ssrcAudioLevelId != 0u)
{
packet->AddExtensionMapping(
RtpHeaderExtensionUri::Type::SSRC_AUDIO_LEVEL, this->params.ssrcAudioLevelId);
}
if (this->params.absSendTimeId != 0u)
{
packet->AddExtensionMapping(
RtpHeaderExtensionUri::Type::ABS_SEND_TIME, this->params.absSendTimeId);
}
}
} // namespace RTC
......@@ -425,4 +425,24 @@ namespace RTC
// Clear the RTP buffer.
ClearBuffer();
}
void RtpStreamSend::SetRtx(uint8_t payloadType, uint32_t ssrc)
{
MS_TRACE();
this->hasRtx = true;
this->rtxPayloadType = payloadType;
this->rtxSsrc = ssrc;
this->rtxSeq = Utils::Crypto::GetRandomUInt(0u, 0xFFFF);
}
void RtpStreamSend::RtxEncode(RTC::RtpPacket* packet)
{
MS_TRACE();
MS_ASSERT(this->hasRtx, "rtx is not enabled on this stream");
packet->RtxEncode(this->rtxPayloadType, this->rtxSsrc, ++this->rtxSeq);
}
} // namespace RTC
......@@ -282,4 +282,71 @@ SCENARIO("parse RTP packets", "[parser][rtp]")
delete packet;
}
SECTION("rtx encryption-decryption")
{
uint8_t buffer[] =
{
0b10010000, 0b00000001, 0, 8,
0, 0, 0, 4,
0, 0, 0, 5,
0b00010000, 0, 0, 3, // Extension header
1, 0, 2, 1,
0xFF, 0, 3, 4,
0xFF, 0xFF, 0xFF, 0xFF
};
uint8_t rtxPayloadType = 102;
uint32_t rtxSsrc = 6;
uint16_t rtxSeq = 80;
RtpPacket* packet = RtpPacket::Parse(buffer, sizeof(buffer));
if (!packet)
FAIL("not a RTP packet");
REQUIRE(packet->HasMarker() == false);
REQUIRE(packet->HasExtensionHeader() == true);
REQUIRE(packet->GetExtensionHeaderLength() == 12);
REQUIRE(packet->GetPayloadType() == 1);
REQUIRE(packet->GetSequenceNumber() == 8);
REQUIRE(packet->GetTimestamp() == 4);
REQUIRE(packet->GetSsrc() == 5);
REQUIRE(packet->GetPayloadLength() == 0);
REQUIRE(!packet->HasOneByteExtensions());
REQUIRE(packet->HasTwoBytesExtensions());
static uint8_t RtxBuffer[MtuSize];
auto rtxPacket = packet->Clone(RtxBuffer);
rtxPacket->RtxEncode(rtxPayloadType, rtxSsrc, rtxSeq);
REQUIRE(rtxPacket->HasMarker() == false);
REQUIRE(rtxPacket->HasExtensionHeader() == true);
REQUIRE(rtxPacket->GetExtensionHeaderLength() == 12);
REQUIRE(rtxPacket->GetPayloadType() == rtxPayloadType);
REQUIRE(rtxPacket->GetSequenceNumber() == rtxSeq);
REQUIRE(rtxPacket->GetTimestamp() == 4);
REQUIRE(rtxPacket->GetSsrc() == rtxSsrc);
REQUIRE(rtxPacket->GetPayloadLength() == 2);
REQUIRE(!rtxPacket->HasOneByteExtensions());
REQUIRE(rtxPacket->HasTwoBytesExtensions());
rtxPacket->RtxDecode(1, 5);
REQUIRE(rtxPacket->HasMarker() == false);
REQUIRE(rtxPacket->HasExtensionHeader() == true);
REQUIRE(rtxPacket->GetExtensionHeaderLength() == 12);
REQUIRE(rtxPacket->GetPayloadType() == 1);
REQUIRE(rtxPacket->GetSequenceNumber() == 8);
REQUIRE(rtxPacket->GetTimestamp() == 4);
REQUIRE(rtxPacket->GetSsrc() == 5);
REQUIRE(rtxPacket->GetPayloadLength() == 0);
REQUIRE(!rtxPacket->HasOneByteExtensions());
REQUIRE(rtxPacket->HasTwoBytesExtensions());
delete packet;
delete rtxPacket;
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论