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

Working on RtpStream::Params

上级 a022224c
......@@ -117,8 +117,8 @@ namespace RTC
}
public:
Type type = Type::UNSET;
Subtype subtype = Subtype::UNSET;
Type type = Type::UNSET;
Subtype subtype = Subtype::UNSET;
std::string name;
};
......@@ -304,7 +304,7 @@ namespace RTC
Json::Value toJson() const;
void ReduceCodecsAndEncodings(RtpCapabilities& capabilities);
void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions);
uint32_t GetClockRateForEncoding(RtpEncodingParameters& encoding) const;
RTC::RtpCodecParameters& GetCodecForEncoding(RtpEncodingParameters& encoding);
private:
void ValidateCodecs();
......
......@@ -57,6 +57,7 @@ namespace RTC
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet);
private:
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void ClearRtpStreams();
/* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */
......
......@@ -64,6 +64,7 @@ namespace RTC
uint32_t GetTransmissionRate(uint64_t now);
private:
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void RetransmitRtpPacket(RTC::RtpPacket* packet);
void EmitActiveChange() const;
......
......@@ -3,6 +3,7 @@
#include "common.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpDictionaries.hpp"
#include <json/json.h>
namespace RTC
......@@ -10,7 +11,21 @@ namespace RTC
class RtpStream
{
public:
explicit RtpStream(uint32_t ssrc, uint32_t clockRate);
struct Params
{
public:
Json::Value toJson() const;
public:
uint32_t ssrc = 0;
uint8_t payloadType = 0;
RTC::RtpCodecMime mime;
uint32_t clockRate = 0;
bool useNack = false;
};
public:
RtpStream(RTC::RtpStream::Params& params);
virtual ~RtpStream();
virtual Json::Value toJson() const = 0;
......@@ -27,8 +42,8 @@ namespace RTC
protected:
// Given as argument.
uint32_t ssrc = 0;
uint32_t clockRate = 0;
RtpStream::Params params;
// Others.
bool started = false; // Whether at least a RTP packet has been received.
// https://tools.ietf.org/html/rfc3550#appendix-A.1 stuff.
uint16_t max_seq = 0; // Highest seq. number seen.
......@@ -48,7 +63,7 @@ namespace RTC
inline
uint32_t RtpStream::GetSsrc()
{
return this->ssrc;
return this->params.ssrc;
}
}
......
......@@ -18,7 +18,7 @@ namespace RTC
};
public:
explicit RtpStreamRecv(Listener* listener, uint32_t ssrc, uint32_t clockRate, bool useNack);
RtpStreamRecv(Listener* listener, RTC::RtpStream::Params& params);
virtual ~RtpStreamRecv();
virtual Json::Value toJson() const override;
......@@ -38,7 +38,7 @@ namespace RTC
private:
// Passed by argument.
Listener* listener = nullptr;
bool useNack = false;
// Others.
uint32_t last_sr_timestamp = 0; // The middle 32 bits out of 64 in the NTP timestamp received in the most recent sender report.
uint64_t last_sr_received = 0; // Wallclock time representing the most recent sender report arrival.
uint32_t transit = 0; // Relative trans time for prev pkt.
......
......@@ -25,7 +25,7 @@ namespace RTC
};
public:
RtpStreamSend(uint32_t ssrc, uint32_t clockRate, size_t bufferSize);
RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize);
virtual ~RtpStreamSend();
virtual Json::Value toJson() const override;
......
......@@ -247,12 +247,12 @@ namespace RTC
this->headerExtensions = updatedHeaderExtensions;
}
uint32_t RtpParameters::GetClockRateForEncoding(RtpEncodingParameters& encoding) const
RTC::RtpCodecParameters& RtpParameters::GetCodecForEncoding(RtpEncodingParameters& encoding)
{
MS_TRACE();
static RTC::RtpCodecParameters fakeCodec;
uint8_t payloadType = encoding.codecPayloadType;
uint32_t clockRate = 0;
auto it = this->codecs.begin();
for (; it != this->codecs.end(); ++it)
......@@ -260,11 +260,7 @@ namespace RTC
auto& codec = *it;
if (codec.payloadType == payloadType)
{
clockRate = codec.clockRate;
break;
}
return codec;
}
// This should never happen.
if (it == this->codecs.end())
......@@ -272,7 +268,7 @@ namespace RTC
MS_ABORT("no valid codec payload type for the given encoding");
}
return clockRate;
return fakeCodec;
}
inline
......
......@@ -176,29 +176,10 @@ namespace RTC
// And notify again.
this->listener->onRtpReceiverParametersDone(this);
// Set the RtpStreamRecv instances.
// Create RtpStreamRecv instances.
for (auto& encoding : this->rtpParameters->encodings)
{
// Don't create an RtpStreamRecv if the encoding has no SSRC.
// TODO: For simulcast or, if not announced, this would be done
// dynamicall by the RtpListener when matching a RID with its SSRC.
if (!encoding.ssrc)
continue;
uint32_t ssrc = encoding.ssrc;
// Don't create a RtpStreamRecv if there is already one for the same SSRC.
// TODO: This may not work for SVC codecs.
if (this->rtpStreams.find(ssrc) != this->rtpStreams.end())
continue;
// Get the clock rate of the stream/encoding.
uint32_t streamClockRate = this->rtpParameters->GetClockRateForEncoding(encoding);
// TODO: Let's assume that, if video, NACK is negotiated. Must do this better.
bool useNack = (this->kind != RTC::Media::Kind::AUDIO);
// Create a RtpStreamRecv for receiving a media stream.
this->rtpStreams[ssrc] = new RTC::RtpStreamRecv(this, ssrc, streamClockRate, useNack);
CreateRtpStream(encoding);
}
break;
......@@ -371,6 +352,39 @@ namespace RTC
}
}
void RtpReceiver::CreateRtpStream(RTC::RtpEncodingParameters& encoding)
{
MS_TRACE();
// Don't create an RtpStreamRecv if the encoding has no SSRC.
// TODO: For simulcast or, if not announced, this would be done
// dynamicall by the RtpListener when matching a RID with its SSRC.
if (!encoding.ssrc)
return;
uint32_t ssrc = encoding.ssrc;
// Don't create a RtpStreamRecv if there is already one for the same SSRC.
// TODO: This may not work for SVC codecs.
if (this->rtpStreams.find(ssrc) != this->rtpStreams.end())
return;
// Get the codec of the stream/encoding.
auto& codec = this->rtpParameters->GetCodecForEncoding(encoding);
// Create stream params.
RTC::RtpStream::Params params;
params.ssrc = ssrc;
params.payloadType = codec.payloadType;
params.mime = codec.mime;
params.clockRate = codec.clockRate;
params.useNack = (this->kind != RTC::Media::Kind::AUDIO);
// Create a RtpStreamRecv for receiving a media stream.
this->rtpStreams[ssrc] = new RTC::RtpStreamRecv(this, params);
}
void RtpReceiver::ClearRtpStreams()
{
MS_TRACE();
......
......@@ -261,31 +261,7 @@ namespace RTC
this->available = true;
// NOTE: We assume a single stream/encoding when sending to remote peers.
auto encoding = encodings[0];
uint32_t ssrc = encoding.ssrc;
uint32_t streamClockRate = this->rtpParameters->GetClockRateForEncoding(encoding);
// Create a RtpStreamSend for sending a single media stream.
switch (this->kind)
{
case RTC::Media::Kind::VIDEO:
case RTC::Media::Kind::DEPTH:
{
// Buffer up to N packets.
this->rtpStream = new RTC::RtpStreamSend(ssrc, streamClockRate, 200);
break;
}
case RTC::Media::Kind::AUDIO:
{
// No buffer for audio streams.
this->rtpStream = new RTC::RtpStreamSend(ssrc, streamClockRate, 0);
break;
}
default:
;
}
CreateRtpStream(encodings[0]);
}
else
{
......@@ -409,6 +385,46 @@ namespace RTC
}
}
void RtpSender::CreateRtpStream(RTC::RtpEncodingParameters& encoding)
{
MS_TRACE();
uint32_t ssrc = encoding.ssrc;
// Get the codec of the stream/encoding.
auto& codec = this->rtpParameters->GetCodecForEncoding(encoding);
// Create stream params.
RTC::RtpStream::Params params;
params.ssrc = ssrc;
params.payloadType = codec.payloadType;
params.mime = codec.mime;
params.clockRate = codec.clockRate;
params.useNack = (this->kind != RTC::Media::Kind::AUDIO);
// Create a RtpStreamSend for sending a single media stream.
switch (this->kind)
{
case RTC::Media::Kind::VIDEO:
case RTC::Media::Kind::DEPTH:
{
// Buffer up to N packets.
this->rtpStream = new RTC::RtpStreamSend(params, 200);
break;
}
case RTC::Media::Kind::AUDIO:
{
// No buffer for audio streams.
this->rtpStream = new RTC::RtpStreamSend(params, 0);
break;
}
default:
;
}
}
void RtpSender::RetransmitRtpPacket(RTC::RtpPacket* packet)
{
MS_TRACE();
......
......@@ -15,9 +15,8 @@ namespace RTC
{
/* Instance methods. */
RtpStream::RtpStream(uint32_t ssrc, uint32_t clockRate) :
ssrc(ssrc),
clockRate(clockRate)
RtpStream::RtpStream(RTC::RtpStream::Params& params) :
params(params)
{
MS_TRACE();
}
......@@ -154,4 +153,25 @@ namespace RTC
return true;
}
Json::Value RtpStream::Params::toJson() const
{
MS_TRACE();
static const Json::StaticString k_ssrc("ssrc");
static const Json::StaticString k_payloadType("payloadType");
static const Json::StaticString k_mime("mime");
static const Json::StaticString k_clockRate("clockRate");
static const Json::StaticString k_useNack("useNack");
Json::Value json(Json::objectValue);
json[k_ssrc] = (Json::UInt)this->ssrc;
json[k_payloadType] = (Json::UInt)this->payloadType;
json[k_mime] = this->mime.name;
json[k_clockRate] = (Json::UInt)this->clockRate;
json[k_useNack] = this->useNack;
return json;
}
}
......@@ -10,10 +10,9 @@ namespace RTC
{
/* Instance methods. */
RtpStreamRecv::RtpStreamRecv(Listener* listener, uint32_t ssrc, uint32_t clockRate, bool useNack) :
RtpStream::RtpStream(ssrc, clockRate),
listener(listener),
useNack(useNack)
RtpStreamRecv::RtpStreamRecv(Listener* listener, RTC::RtpStream::Params& params) :
RtpStream::RtpStream(params),
listener(listener)
{
MS_TRACE();
}
......@@ -27,9 +26,7 @@ namespace RTC
{
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_ssrc("ssrc");
static const Json::StaticString k_clockRate("clockRate");
static const Json::StaticString k_params("params");
static const Json::StaticString k_received("received");
static const Json::StaticString k_maxTimestamp("maxTimestamp");
static const Json::StaticString k_transit("transit");
......@@ -37,8 +34,7 @@ namespace RTC
Json::Value json(Json::objectValue);
json[k_ssrc] = (Json::UInt)this->ssrc;
json[k_clockRate] = (Json::UInt)this->clockRate;
json[k_params] = this->params.toJson();
json[k_received] = (Json::UInt)this->received;
json[k_maxTimestamp] = (Json::UInt)this->max_timestamp;
......@@ -60,7 +56,7 @@ namespace RTC
CalculateJitter(packet->GetTimestamp());
// May trigger a NACK to the sender.
if (this->useNack)
if (this->params.useNack)
MayTriggerNack(packet);
return true;
......@@ -129,10 +125,10 @@ namespace RTC
void RtpStreamRecv::CalculateJitter(uint32_t rtpTimestamp)
{
if (!this->clockRate)
if (!this->params.clockRate)
return;
int transit = DepLibUV::GetTime() - (rtpTimestamp * 1000 / this->clockRate);
int transit = DepLibUV::GetTime() - (rtpTimestamp * 1000 / this->params.clockRate);
int d = transit - this->transit;
this->transit = transit;
......@@ -160,7 +156,7 @@ namespace RTC
else
this->last_seq32 = seq32;
// Just received next expected seq, do nothing else.
// Just received next expected seq, do nothing.
if (diff_seq32 == 1)
return;
......@@ -178,7 +174,7 @@ namespace RTC
uint16_t nack_bitmask = (uint16_t)nack_bitset.to_ulong();
MS_DEBUG_TAG(rtcp, "NACK triggered [ssrc:%" PRIu32 ", seq:%" PRIu16 ", bitmask:" MS_UINT16_TO_BINARY_PATTERN "]",
this->ssrc, nack_seq, MS_UINT16_TO_BINARY(nack_bitmask));
this->params.ssrc, nack_seq, MS_UINT16_TO_BINARY(nack_bitmask));
this->listener->onNackRequired(this, nack_seq, nack_bitmask);
}
......
......@@ -13,8 +13,8 @@ namespace RTC
{
/* Instance methods. */
RtpStreamSend::RtpStreamSend(uint32_t ssrc, uint32_t clockRate, size_t bufferSize) :
RtpStream::RtpStream(ssrc, clockRate),
RtpStreamSend::RtpStreamSend(RTC::RtpStream::Params& params, size_t bufferSize) :
RtpStream::RtpStream(params),
storage(bufferSize)
{
MS_TRACE();
......@@ -32,17 +32,14 @@ namespace RTC
{
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_ssrc("ssrc");
static const Json::StaticString k_clockRate("clockRate");
static const Json::StaticString k_params("params");
static const Json::StaticString k_received("received");
static const Json::StaticString k_maxTimestamp("maxTimestamp");
static const Json::StaticString k_receivedBytes("receivedBytes");
Json::Value json(Json::objectValue);
json[k_ssrc] = (Json::UInt)this->ssrc;
json[k_clockRate] = (Json::UInt)this->clockRate;
json[k_params] = this->params.toJson();
json[k_received] = (Json::UInt)this->received;
json[k_maxTimestamp] = (Json::UInt)this->max_timestamp;
......@@ -156,7 +153,7 @@ namespace RTC
if (current_seq32 == seq32)
{
auto current_packet = (*buffer_it).packet;
uint32_t diff = (this->max_timestamp - current_packet->GetTimestamp()) * 1000 / this->clockRate;
uint32_t diff = (this->max_timestamp - current_packet->GetTimestamp()) * 1000 / this->params.clockRate;
// Just provide the packet if no older than MAX_RETRANSMISSION_AGE ms.
if (diff <= MAX_RETRANSMISSION_AGE)
......@@ -235,7 +232,7 @@ namespace RTC
// Calculate RTP timestamp diff between now and last received RTP packet.
uint32_t diffMs = now - this->lastPacketTimeMs;
uint32_t diffRtpTimestamp = diffMs * this->clockRate / 1000;
uint32_t diffRtpTimestamp = diffMs * this->params.clockRate / 1000;
report->SetRtpTs(this->lastPacketRtpTimestamp + diffRtpTimestamp);
......
......@@ -2,6 +2,7 @@
#include "common.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RTCP/FeedbackRtpNack.hpp"
#include "RTC/RtpStream.hpp"
#include "RTC/RtpStreamSend.hpp"
#include <vector>
......@@ -59,8 +60,14 @@ SCENARIO("NACK and RTP packets retransmission", "[rtp][rtcp]")
REQUIRE(packet5->GetSequenceNumber() == 21010);
REQUIRE(packet5->GetTimestamp() == 1533796931);
RtpStream::Params params;
params.ssrc = packet1->GetSsrc();
params.clockRate = 90000;
params.useNack = true;
// Create a RtpStreamSend.
RtpStreamSend* stream = new RtpStreamSend(packet1->GetSsrc(), 90000, 200);
RtpStreamSend* stream = new RtpStreamSend(params, 200);
// Receive all the packets in order into the stream.
stream->ReceivePacket(packet1);
......
......@@ -2,6 +2,7 @@
#include "include/helpers.hpp"
#include "common.hpp"
#include "RTC/RtpPacket.hpp"
#include "RTC/RtpStream.hpp"
#include "RTC/RtpStreamRecv.hpp"
#include "Logger.hpp"
#include <bitset> // std::bitset()
......@@ -49,8 +50,14 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
REQUIRE(packet->GetSequenceNumber() == 100);
RtpStream::Params params;
params.ssrc = packet->GetSsrc();
params.clockRate = 90000;
params.useNack = true;
RtpStreamRecvListener listener;
RtpStreamRecv rtpStream(&listener, packet->GetSsrc(), 90000, true);
RtpStreamRecv rtpStream(&listener, params);
rtpStream.ReceivePacket(packet);
......@@ -65,6 +72,7 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
listener.expected_nack_seq = 102;
listener.expected_nack_bitmask = 0b0000000000000001;
rtpStream.ReceivePacket(packet);
REQUIRE(listener.should_trigger == false);
packet->SetSequenceNumber(104);
rtpStream.ReceivePacket(packet);
......@@ -80,6 +88,7 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
listener.expected_nack_seq = 105;
listener.expected_nack_bitmask = 0b0000000000000011;
rtpStream.ReceivePacket(packet);
REQUIRE(listener.should_trigger == false);
delete packet;
}
......@@ -100,8 +109,14 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
REQUIRE(packet->GetSequenceNumber() == 100);
RtpStream::Params params;
params.ssrc = packet->GetSsrc();
params.clockRate = 90000;
params.useNack = true;
RtpStreamRecvListener listener;
RtpStreamRecv rtpStream(&listener, packet->GetSsrc(), 90000, true);
RtpStreamRecv rtpStream(&listener, params);
rtpStream.ReceivePacket(packet);
......@@ -110,6 +125,7 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
listener.expected_nack_seq = 103;
listener.expected_nack_bitmask = 0b1111111111111111;
rtpStream.ReceivePacket(packet);
REQUIRE(listener.should_trigger == false);
delete packet;
}
......@@ -130,8 +146,14 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
REQUIRE(packet->GetSequenceNumber() == 65535);
RtpStream::Params params;
params.ssrc = packet->GetSsrc();
params.clockRate = 90000;
params.useNack = true;
RtpStreamRecvListener listener;
RtpStreamRecv rtpStream(&listener, packet->GetSsrc(), 90000, true);
RtpStreamRecv rtpStream(&listener, params);
rtpStream.ReceivePacket(packet);
......@@ -140,6 +162,7 @@ SCENARIO("receive RTP packets and trigger NACK", "[rtp][rtpstream]")
listener.expected_nack_seq = 3;
listener.expected_nack_bitmask = 0b1111111111111111;
rtpStream.ReceivePacket(packet);
REQUIRE(listener.should_trigger == false);
delete packet;
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论