提交 4346c876 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Improve stuff needed for simulcast (#62)

* PlanB: Remove rtpParameters.muxId and add a=x-google-flag:conference
* PlanB: improve SSRC handling
* Play a bit more with Chrome simulcast
* RtpReceiver: remove useless FillRtpParameters() method
* RtpReceiver: manage multiple RtpStreamRecv instances indexed by SSRC
* Remove unused function RtpStreamSend::Dump()
* PlanB SDP: Properly manage simulcast streams
上级 b6c6bdbb
......@@ -18,8 +18,8 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
{
super(options);
// Map of RtpReceivers indexed by SSRC.
this._mapRtpReceivers = new Map();
// Map of RtpReceivers indexed by MediaStreamTrack.id (a=label).
this._mapTrackToRtpReceiver = new Map();
}
_setUpOffer()
......@@ -102,38 +102,27 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
for (let parsedMedia of parsed.media)
{
// If the answer has RTX or FEC it will have a=ssrc-group attribute(s)
// so media SSRC is the first value on it. Example:
// a=ssrc-group:FID 369944027 1319041533
// => mediaSsrc = 369944027
if (Array.isArray(parsedMedia.ssrcGroups))
{
for (let ssrcGroup of parsedMedia.ssrcGroups)
{
let mediaSsrc = Number(ssrcGroup.ssrcs.split(' ')[0]);
let promise = this._createRtpReceiverForSsrc(mediaSsrc, parsedMedia);
// Get all the media ssrcs.
let ssrcs = sdpUtils.getPlanBSsrcs(parsedMedia);
promises.push(promise);
}
}
// Otherwise (no RTX/FEC) each a=ssrc means a sending MediaStreamTrack.
else if (Array.isArray(parsedMedia.ssrcs))
for (let ssrc of ssrcs)
{
let mediaSsrcs = new Set();
for (let ssrcObj of parsedMedia.ssrcs)
{
let mediaSsrc = ssrcObj.id;
if (mediaSsrcs.has(mediaSsrc))
continue;
mediaSsrcs.add(mediaSsrc);
let promise = this._createRtpReceiverForSsrc(mediaSsrc, parsedMedia);
// Get MediaStream and MediaStreamTracks info.
let trackInfo = sdpUtils.getPlanBTrackInfo(parsedMedia, ssrc);
let promise = this._createRtpReceiverForSsrc(parsedMedia, ssrc, trackInfo)
.then((rtpReceiver) =>
{
// Store it in the map.
this._mapTrackToRtpReceiver.set(trackInfo.trackId, rtpReceiver);
// When closed, remove from the map.
rtpReceiver.on('close', () =>
{
this._mapTrackToRtpReceiver.delete(trackInfo.trackId);
});
});
promises.push(promise);
}
promises.push(promise);
}
}
......@@ -151,68 +140,51 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
// Check new/removed RtpReceivers.
.then(() =>
{
let receivingMediaSsrcs = [];
let receivingTrackIds = new Set();
let promises = [];
for (let parsedMedia of parsed.media)
{
// If the answer has RTX or FEC it will have a=ssrc-group attribute(s)
// so media SSRC is the first value on it. Example:
// a=ssrc-group:FID 369944027 1319041533
// => mediaSsrc = 369944027
if (Array.isArray(parsedMedia.ssrcGroups))
{
for (let ssrcGroup of parsedMedia.ssrcGroups)
{
let mediaSsrc = Number(ssrcGroup.ssrcs.split(' ')[0]);
receivingMediaSsrcs.push(mediaSsrc);
if (!this._mapRtpReceivers.has(mediaSsrc))
{
this._logger.debug('_handleRemoteReAnswer() | new receiving SSRC [ssrc:%s]', mediaSsrc);
let promise = this._createRtpReceiverForSsrc(mediaSsrc, parsedMedia);
// Get all the media ssrcs.
let ssrcs = sdpUtils.getPlanBSsrcs(parsedMedia);
promises.push(promise);
}
}
}
// Otherwise (no RTX/FEC) each a=ssrc means a sending MediaStreamTrack.
else if (Array.isArray(parsedMedia.ssrcs))
for (let ssrc of ssrcs)
{
let mediaSsrcs = new Set();
for (let ssrcObj of parsedMedia.ssrcs)
{
let mediaSsrc = ssrcObj.id;
// Get MediaStream and MediaStreamTracks info.
let trackInfo = sdpUtils.getPlanBTrackInfo(parsedMedia, ssrc);
if (mediaSsrcs.has(mediaSsrc))
continue;
receivingTrackIds.add(trackInfo.trackId);
mediaSsrcs.add(mediaSsrc);
receivingMediaSsrcs.push(mediaSsrc);
if (!this._mapTrackToRtpReceiver.has(trackInfo.trackId))
{
this._logger.debug('_handleRemoteReAnswer() | new receiving track [ssrc:%s, trackId:"%s"]', ssrc, trackInfo.trackId);
if (!this._mapRtpReceivers.has(mediaSsrc))
{
this._logger.debug('_handleRemoteReAnswer() | new receiving SSRC [ssrc:%s]', mediaSsrc);
let promise = this._createRtpReceiverForSsrc(parsedMedia, ssrc, trackInfo)
.then((rtpReceiver) =>
{
// Store it in the map.
this._mapTrackToRtpReceiver.set(trackInfo.trackId, rtpReceiver);
let promise = this._createRtpReceiverForSsrc(mediaSsrc, parsedMedia);
// When closed, remove from the map.
rtpReceiver.on('close', () =>
{
this._mapTrackToRtpReceiver.delete(trackInfo.trackId);
});
});
promises.push(promise);
}
promises.push(promise);
}
}
}
// Check removed receiving SSRC.
for (let mediaSsrc of this._mapRtpReceivers.keys())
for (let trackId of this._mapTrackToRtpReceiver.keys())
{
if (receivingMediaSsrcs.indexOf(mediaSsrc) === -1)
if (!receivingTrackIds.has(trackId))
{
this._logger.debug('_handleRemoteReAnswer() | receiving SSRC removed [ssrc:%s]', mediaSsrc);
this._logger.debug('_handleRemoteReAnswer() | receiving track removed [trackId:"%s"]', trackId);
let rtpReceiver = this._mapRtpReceivers.get(mediaSsrc);
let rtpReceiver = this._mapTrackToRtpReceiver.get(trackId);
rtpReceiver.close();
}
......@@ -458,6 +430,9 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
// a=rtcp-rsize
objMedia.rtcpRsize = 'rtcp-rsize';
// a=x-google-flag:conference.
objMedia.xGoogleFlag = 'conference';
// Bandwidth.
if (self._options.bandwidth && self._options.bandwidth[kind])
{
......@@ -472,10 +447,13 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
}
}
_createRtpReceiverForSsrc(mediaSsrc, parsedMedia)
_createRtpReceiverForSsrc(parsedMedia, mediaSsrcs, trackInfo)
{
// TODO: We assume no RTX/FEC in client's SDP.
if (!Array.isArray(mediaSsrcs))
mediaSsrcs = [ mediaSsrcs ];
let transport = this._peer.transports[0];
let kind = parsedMedia.type;
// Map of RtpCodecParameters indexed by payload type.
......@@ -537,70 +515,38 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
}
// Array of RtpEncodingParameters.
// NOTE: Just a single encoding will be created with the first media codec
// in the answer.
let encodings =
[
{
ssrc : mediaSsrc,
codecPayloadType : mapCodecs.values().next().value.payloadType
}
];
// Array of RtpHeaderExtensionParameters.
let headerExtensions = sdpUtils.descToRtpHeaderExtensionParameters(parsedMedia);
let encodings = [];
// NOTE: Here we assume that the first codec in the list is the media one.
let codecPayloadType = mapCodecs.values().next().value.payloadType;
let cname;
let msid;
for (let ssrc of parsedMedia.ssrcs)
for (let ssrc of mediaSsrcs)
{
if (ssrc.id !== mediaSsrc)
continue;
switch (ssrc.attribute)
{
case 'cname':
cname = ssrc.value;
break;
// Chrome uses:
// a=ssrc:xxxx msid:yyyy zzzz
// a=ssrc:xxxx mslabel:yyyy
// a=ssrc:xxxx label:zzzz
// Where yyyy is the MediaStream.id and zzzz the MediaStreamTrack.id.
case 'msid':
msid = ssrc.value;
break;
}
encodings.push(
{
ssrc : ssrc,
codecPayloadType : codecPayloadType
});
}
// Array of RtpHeaderExtensionParameters.
let headerExtensions = sdpUtils.descToRtpHeaderExtensionParameters(parsedMedia);
// Create a RtpReceiver.
let rtpReceiver = this._peer.RtpReceiver(kind, transport);
// Store it in the map.
this._mapRtpReceivers.set(mediaSsrc, rtpReceiver);
// When closed, remove from the map.
rtpReceiver.on('close', () =>
{
this._mapRtpReceivers.delete(mediaSsrc);
});
// RtpParameters object.
let rtpParameters =
{
muxId : parsedMedia.mid,
codecs : Array.from(mapCodecs.values()),
encodings : encodings,
headerExtensions : headerExtensions,
rtcp :
{
cname : cname
cname : trackInfo.cname
},
userParameters :
{
msid : msid
msid : trackInfo.msid
}
};
......
......@@ -193,5 +193,103 @@ module.exports =
}
return headerExtensions;
},
/**
* This assumes no RTX/FEC.
*/
getPlanBSsrcs(parsedMedia)
{
let singleSsrcs = new Set();
let simulcastSsrcs = new Set();
let ssrcs = [];
// Simulcast.
if (Array.isArray(parsedMedia.ssrcGroups))
{
for (let group of parsedMedia.ssrcGroups)
{
switch (group.semantics)
{
case 'SIM':
{
let simulcast = [];
group.ssrcs.split(' ').forEach((ssrc) =>
{
ssrc = Number(ssrc);
simulcastSsrcs.add(ssrc);
simulcast.push(ssrc);
});
ssrcs.push(simulcast);
break;
}
}
}
}
// Single streams.
if (Array.isArray(parsedMedia.ssrcs))
{
for (let ssrcObj of parsedMedia.ssrcs)
{
let ssrc = ssrcObj.id;
if (simulcastSsrcs.has(ssrc) || singleSsrcs.has(ssrc))
continue;
singleSsrcs.add(ssrc);
ssrcs.push(ssrc);
}
}
return ssrcs;
},
getPlanBTrackInfo(parsedMedia, ssrc)
{
let info =
{
cname : null,
msid : null,
trackId : null
};
if (!Array.isArray(parsedMedia.ssrcs))
return info;
// If the given ssrc is a simulcast array of ssrcs, just take the first one.
if (Array.isArray(ssrc))
ssrc = ssrc[0];
for (let ssrcObj of parsedMedia.ssrcs)
{
if (ssrcObj.id !== ssrc)
continue;
// Chrome uses:
// a=ssrc:xxxx msid:yyyy zzzz
// a=ssrc:xxxx mslabel:yyyy
// a=ssrc:xxxx label:zzzz
// Where yyyy is the MediaStream.id and zzzz the MediaStreamTrack.id.
switch (ssrcObj.attribute)
{
case 'cname':
info.cname = ssrcObj.value;
break;
case 'msid':
info.msid = ssrcObj.value;
break;
case 'label':
info.trackId = ssrcObj.value;
break;
}
}
return info;
}
};
......@@ -270,7 +270,7 @@ namespace RTC
public:
std::string cname;
uint32_t ssrc = 0;
bool reducedSize = false;
bool reducedSize = true;
};
class RtpParameters
......@@ -282,8 +282,7 @@ namespace RTC
Json::Value toJson();
void ReduceCodecsAndEncodings(RtpCapabilities& capabilities);
void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions);
uint32_t GetEncodingClockRate(size_t encodingIdx);
uint32_t GetEncodingMediaSsrc(size_t encodingIdx);
uint32_t GetClockRateForEncoding(RtpEncodingParameters& encoding);
private:
void ValidateCodecs();
......
......@@ -11,6 +11,7 @@
#include "Channel/Request.hpp"
#include "Channel/Notifier.hpp"
#include <string>
#include <map>
#include <json/json.h>
namespace RTC
......@@ -55,7 +56,7 @@ namespace RTC
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet);
private:
void FillRtpParameters();
void ClearRtpStreams();
public:
// Passed by argument.
......@@ -69,8 +70,7 @@ namespace RTC
RTC::Transport* transport = nullptr;
// Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr;
// TODO: For now just one.
RTC::RtpStreamRecv* rtpStream = nullptr;
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
// Others.
bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false;
......@@ -109,10 +109,13 @@ namespace RTC
inline
void RtpReceiver::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{
// TODO: This assumes a single stream for now.
if (this->rtpStream)
this->rtpStream->ReceiveRtcpSenderReport(report);
auto it = this->rtpStreams.find(report->GetSsrc());
if (it != this->rtpStreams.end())
{
auto rtpStream = it->second;
rtpStream->ReceiveRtcpSenderReport(report);
}
}
}
......
......@@ -4,6 +4,7 @@
#include "RTC/RtpStream.hpp"
#include "RTC/RTCP/ReceiverReport.hpp"
#include "RTC/RTCP/SenderReport.hpp"
#include <json/json.h>
namespace RTC
{
......@@ -14,6 +15,7 @@ namespace RTC
explicit RtpStreamRecv(uint32_t clockRate);
virtual ~RtpStreamRecv();
Json::Value toJson();
virtual bool ReceivePacket(RTC::RtpPacket* packet);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
......
......@@ -5,6 +5,7 @@
#include "RTC/RTCP/SenderReport.hpp"
#include <vector>
#include <list>
#include <json/json.h>
namespace RTC
{
......@@ -28,6 +29,7 @@ namespace RTC
RtpStreamSend(uint32_t clockRate, size_t bufferSize);
virtual ~RtpStreamSend();
Json::Value toJson();
bool ReceivePacket(RTC::RtpPacket* packet);
void RequestRtpRetransmission(uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now);
......@@ -35,7 +37,6 @@ namespace RTC
private:
void ClearBuffer();
void StorePacket(RTC::RtpPacket* packet);
void Dump();
private:
std::vector<StorageItem> storage;
......
......@@ -246,18 +246,14 @@ namespace RTC
this->headerExtensions = updatedHeaderExtensions;
}
uint32_t RtpParameters::GetEncodingClockRate(size_t encodingIdx)
uint32_t RtpParameters::GetClockRateForEncoding(RtpEncodingParameters& encoding)
{
MS_TRACE();
if (this->encodings.size() < encodingIdx + 1)
MS_ABORT("no such a encoding [encodingIdx:%zu]", encodingIdx);
uint8_t payloadType = this->encodings[encodingIdx].codecPayloadType;
uint8_t payloadType = encoding.codecPayloadType;
uint32_t clockRate = 0;
auto it = this->codecs.begin();
for (; it != this->codecs.end(); ++it)
{
auto& codec = *it;
......@@ -272,22 +268,12 @@ namespace RTC
// This should never happen.
if (it == this->codecs.end())
{
MS_ABORT("no valid codec payload type for the requested encoding [encodingIdx:%zu]", encodingIdx);
MS_ABORT("no valid codec payload type for the given encoding");
}
return clockRate;
}
uint32_t RtpParameters::GetEncodingMediaSsrc(size_t encodingIdx)
{
MS_TRACE();
if (this->encodings.size() < encodingIdx + 1)
MS_ABORT("no such a encoding [encodingIdx:%zu]", encodingIdx);
return this->encodings[encodingIdx].ssrc;
}
inline
void RtpParameters::ValidateCodecs()
{
......
......@@ -266,6 +266,18 @@ namespace RTC
{
MS_TRACE();
// TODO: REMOVE.
// uint8_t* rid_pointer = packet->GetExtensionElementValue(5);
// if (rid_pointer)
// {
// uint8_t rid_length = packet->GetExtensionElementLength(5);
// std::string rid_value = std::string((const char*)rid_pointer, (size_t)rid_length);
// MS_DEBUG_TAG(rtp, "RID [length:%zu, value:'%s', pointer[0]:'%c']", (size_t)rid_length, rid_value.c_str(), (char)rid_pointer[0]);
// if ((char)rid_pointer[0] == '\0')
// MS_WARN_TAG(rtp, "RID data[0] is 0");
// }
// First lookup into the SSRC table.
{
auto it = this->ssrcTable.find(packet->GetSsrc());
......@@ -291,8 +303,10 @@ namespace RTC
}
}
// TODO: RID table? Sure.
// Otherwise lookup into the muxId table.
// TODO: do it
// TODO: do it.
// Otherwise lookup into the PT table.
{
......
......@@ -36,8 +36,7 @@ namespace RTC
if (this->rtpParameters)
delete this->rtpParameters;
if (this->rtpStream)
delete this->rtpStream;
ClearRtpStreams();
}
void RtpReceiver::Close()
......@@ -69,6 +68,9 @@ namespace RTC
static const Json::StaticString k_hasTransport("hasTransport");
static const Json::StaticString k_rtpRawEventEnabled("rtpRawEventEnabled");
static const Json::StaticString k_rtpObjectEventEnabled("rtpObjectEventEnabled");
static const Json::StaticString k_rtpStreams("rtpStreams");
static const Json::StaticString k_ssrc("ssrc");
static const Json::StaticString k_rtpStream("rtpStream");
Json::Value json(Json::objectValue);
......@@ -87,6 +89,20 @@ namespace RTC
json[k_rtpObjectEventEnabled] = this->rtpObjectEventEnabled;
json[k_rtpStreams] = Json::arrayValue;
for (auto& kv : this->rtpStreams)
{
auto ssrc = kv.first;
auto rtpStream = kv.second;
Json::Value json_rtpStream(Json::objectValue);
json_rtpStream[k_ssrc] = (Json::UInt)ssrc;
json_rtpStream[k_rtpStream] = rtpStream->toJson();
json[k_rtpStreams].append(json_rtpStream);
}
return json;
}
......@@ -151,26 +167,42 @@ namespace RTC
return;
}
// Free the previous rtpParameters.
// Free the previous parameters.
if (previousRtpParameters)
delete previousRtpParameters;
// Free previous RTP streams.
ClearRtpStreams();
Json::Value data = this->rtpParameters->toJson();
request->Accept(data);
// Fill RTP parameters.
FillRtpParameters();
// And notify again.
this->listener->onRtpReceiverParametersDone(this);
// Set the RtpStreamRecv.
// TODO: This assumes a single receiving stream for now.
uint32_t streamClockRate = this->rtpParameters->GetEncodingClockRate(0);
// Set the 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;
// Create a RtpStreamRecv for receiving a media stream.
this->rtpStream = new RTC::RtpStreamRecv(streamClockRate);
// 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);
// Create a RtpStreamRecv for receiving a media stream.
this->rtpStreams[ssrc] = new RTC::RtpStreamRecv(streamClockRate);
}
break;
}
......@@ -234,13 +266,22 @@ namespace RTC
// TODO: Check if stopped, etc (not yet done).
// TODO: Handle multiple streams.
MS_ASSERT(this->rtpStream, "no RtpStream set");
// Find the corresponding RtpStreamRecv.
auto ssrc = packet->GetSsrc();
if (this->rtpStreams.find(ssrc) == this->rtpStreams.end())
{
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
return;
}
auto 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 (!this->rtpStream->ReceivePacket(packet))
if (!rtpStream->ReceivePacket(packet))
return;
// Notify the listener.
......@@ -276,34 +317,22 @@ namespace RTC
}
}
void RtpReceiver::FillRtpParameters()
{
MS_TRACE();
// Set a random muxId.
// TODO: This is wrong, we are replacing the given muxId. Instead, this
// method should set a random muxId and map the original value.
this->rtpParameters->muxId = Utils::Crypto::GetRandomString(8);
// TODO: Fill SSRCs with random values and set some mechanism to replace
// SSRC values in received RTP packets to match the chosen random values.
}
void RtpReceiver::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{
if (this->rtpStream)
{
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
RTC::RTCP::ReceiverReport* report = this->rtpStream->GetRtcpReceiverReport();
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return;
// TODO: This assumes a single stream for now.
report->SetSsrc(this->rtpParameters->GetEncodingMediaSsrc(0));
packet->AddReceiverReport(report);
for (auto& kv : this->rtpStreams)
{
auto ssrc = kv.first;
auto rtpStream = kv.second;
RTC::RTCP::ReceiverReport* report = rtpStream->GetRtcpReceiverReport();
this->lastRtcpSentTime = now;
}
report->SetSsrc(ssrc);
packet->AddReceiverReport(report);
}
this->lastRtcpSentTime = now;
}
void RtpReceiver::ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet)
......@@ -345,4 +374,18 @@ namespace RTC
this->transport->SendRtcpPacket(packet);
}
}
void RtpReceiver::ClearRtpStreams()
{
MS_TRACE();
for (auto& kv : this->rtpStreams)
{
auto rtpStream = kv.second;
delete rtpStream;
}
this->rtpStreams.clear();
}
}
......@@ -4,6 +4,7 @@
#include "RTC/RtpSender.hpp"
#include "RTC/RTCP/SenderReport.hpp"
#include "RTC/RTCP/FeedbackRtpNack.hpp"
#include "Utils.hpp"
#include "MediaSoupError.hpp"
#include "Logger.hpp"
#include <unordered_set>
......@@ -70,6 +71,7 @@ namespace RTC
static const Json::StaticString k_hasTransport("hasTransport");
static const Json::StaticString k_available("available");
static const Json::StaticString k_supportedPayloadTypes("supportedPayloadTypes");
static const Json::StaticString k_rtpStream("rtpStream");
Json::Value json(Json::objectValue);
......@@ -93,6 +95,9 @@ namespace RTC
json[k_supportedPayloadTypes].append((Json::UInt)payloadType);
}
if (this->rtpStream)
json[k_rtpStream] = this->rtpStream->toJson();
return json;
}
......@@ -194,17 +199,32 @@ namespace RTC
}
}
// TODO: Temporal. To be refactored.
// Remove all the encodings but the first one.
if (this->rtpParameters->encodings.size() > 1)
{
RTC::RtpEncodingParameters encoding = this->rtpParameters->encodings[0];
this->rtpParameters->encodings.clear();
this->rtpParameters->encodings.push_back(encoding);
}
// Remove unsupported header extensions.
this->rtpParameters->ReduceHeaderExtensions(this->peerCapabilities->headerExtensions);
// Set a random muxId.
this->rtpParameters->muxId = Utils::Crypto::GetRandomString(8);
// If there are no encodings set not available.
if (this->rtpParameters->encodings.size() > 0)
{
this->available = true;
// NOTE: We assume a single stream/encoding when sending to remote peers.
auto encoding = this->rtpParameters->encodings[0];
// Set the RtpStreamSend.
// NOTE: We assume a single stream when sending to remote peers.
uint32_t streamClockRate = this->rtpParameters->GetEncodingClockRate(0);
uint32_t streamClockRate = this->rtpParameters->GetClockRateForEncoding(encoding);
// Create a RtpStreamSend for sending a single media stream.
switch (this->kind)
......@@ -255,6 +275,16 @@ namespace RTC
MS_ASSERT(this->rtpStream, "no RtpStream set");
// TODO: Must refactor for simulcast.
// Ignore the packet if the SSRC is not the single one in the sender
// RTP parameters.
if (packet->GetSsrc() != this->rtpParameters->encodings[0].ssrc)
{
MS_DEBUG_TAG(rtp, "ignoring packet with unknown SSRC [ssrc:%" PRIu32 "]", packet->GetSsrc());
return;
}
// Process the packet.
// TODO: Must check what kind of packet we are checking. For example, RTX
// packets (once implemented) should have a different handling.
......@@ -283,32 +313,31 @@ namespace RTC
void RtpSender::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{
if (this->rtpStream)
{
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
RTC::RTCP::SenderReport* report = this->rtpStream->GetRtcpSenderReport(now);
if (!this->rtpStream)
return;
if (report)
{
// TODO: This assumes a single stream for now.
uint32_t ssrc = this->rtpParameters->GetEncodingMediaSsrc(0);
std::string cname = this->rtpParameters->rtcp.cname;
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return;
report->SetSsrc(ssrc);
packet->AddSenderReport(report);
RTC::RTCP::SenderReport* report = this->rtpStream->GetRtcpSenderReport(now);
if (!report)
return;
// Build sdes chunk for this sender.
RTC::RTCP::SdesChunk *sdesChunk = new RTC::RTCP::SdesChunk(ssrc);
RTC::RTCP::SdesItem *sdesItem = new RTC::RTCP::SdesItem(RTC::RTCP::SdesItem::Type::CNAME, cname.size(), cname.c_str());
// NOTE: This assumes a single stream.
uint32_t ssrc = this->rtpParameters->encodings[0].ssrc;
std::string cname = this->rtpParameters->rtcp.cname;
sdesChunk->AddItem(sdesItem);
packet->AddSdesChunk(sdesChunk);
report->SetSsrc(ssrc);
packet->AddSenderReport(report);
this->lastRtcpSentTime = now;
}
}
}
// Build sdes chunk for this sender.
RTC::RTCP::SdesChunk *sdesChunk = new RTC::RTCP::SdesChunk(ssrc);
RTC::RTCP::SdesItem *sdesItem = new RTC::RTCP::SdesItem(RTC::RTCP::SdesItem::Type::CNAME, cname.size(), cname.c_str());
sdesChunk->AddItem(sdesItem);
packet->AddSdesChunk(sdesChunk);
this->lastRtcpSentTime = now;
}
void RtpSender::ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket)
......
......@@ -20,6 +20,29 @@ namespace RTC
MS_TRACE();
}
Json::Value RtpStreamRecv::toJson()
{
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_clockRate("clockRate");
static const Json::StaticString k_received("received");
static const Json::StaticString k_maxTimestamp("maxTimestamp");
static const Json::StaticString k_transit("transit");
static const Json::StaticString k_jitter("jitter");
Json::Value json(Json::objectValue);
json[k_clockRate] = (Json::UInt)this->clockRate;
json[k_received] = (Json::UInt)this->received;
json[k_maxTimestamp] = (Json::UInt)this->max_timestamp;
json[k_transit] = (Json::UInt)this->transit;
json[k_jitter] = (Json::UInt)this->jitter;
return json;
}
bool RtpStreamRecv::ReceivePacket(RTC::RtpPacket* packet)
{
MS_TRACE();
......
......@@ -49,6 +49,27 @@ namespace RTC
ClearBuffer();
}
Json::Value RtpStreamSend::toJson()
{
MS_TRACE();
static Json::Value null_data(Json::nullValue);
static const Json::StaticString k_clockRate("clockRate");
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_clockRate] = (Json::UInt)this->clockRate;
json[k_received] = (Json::UInt)this->received;
json[k_maxTimestamp] = (Json::UInt)this->max_timestamp;
json[k_receivedBytes] = (Json::UInt)this->receivedBytes;
return json;
}
bool RtpStreamSend::ReceivePacket(RTC::RtpPacket* packet)
{
MS_TRACE();
......@@ -203,6 +224,33 @@ namespace RTC
container[container_idx] = nullptr;
}
RTC::RTCP::SenderReport* RtpStreamSend::GetRtcpSenderReport(uint64_t now)
{
MS_TRACE();
if (!received)
return nullptr;
RTC::RTCP::SenderReport* report = new RTC::RTCP::SenderReport();
report->SetPacketCount(this->received);
report->SetOctetCount(this->receivedBytes);
Utils::Time::Ntp ntp;
Utils::Time::CurrentTimeNtp(ntp);
report->SetNtpSec(ntp.seconds);
report->SetNtpFrac(ntp.fractions);
// Calculate RTP timestamp diff between now and last received RTP packet.
uint32_t diffMs = now - this->lastPacketTimeMs;
uint32_t diffRtpTimestamp = diffMs * this->clockRate / 1000;
report->SetRtpTs(this->lastPacketRtpTimestamp + diffRtpTimestamp);
return report;
}
void RtpStreamSend::ClearBuffer()
{
MS_TRACE();
......@@ -293,47 +341,4 @@ namespace RTC
// Update the new buffer item so it points to the cloned packed.
(*new_buffer_it).packet = packet->Clone(store);
}
// TODO: TMP
void RtpStreamSend::Dump()
{
MS_TRACE();
MS_DUMP("<RtpStreamSend>");
MS_DUMP(" [buffer.size:%zu, storage.size:%zu]", this->buffer.size(), this->storage.size());
for (auto& buffer_item : this->buffer)
{
auto packet = buffer_item.packet;
MS_DUMP(" packet [seq:%" PRIu16 ", seq32:%" PRIu32 "]", packet->GetSequenceNumber(), buffer_item.seq32);
}
MS_DUMP("</RtpStreamSend>");
}
RTC::RTCP::SenderReport* RtpStreamSend::GetRtcpSenderReport(uint64_t now)
{
MS_TRACE();
if (!received)
return nullptr;
RTC::RTCP::SenderReport* report = new RTC::RTCP::SenderReport();
report->SetPacketCount(this->received);
report->SetOctetCount(this->receivedBytes);
Utils::Time::Ntp ntp;
Utils::Time::CurrentTimeNtp(ntp);
report->SetNtpSec(ntp.seconds);
report->SetNtpFrac(ntp.fractions);
// Calculate RTP timestamp diff between now and last received RTP packet.
uint32_t diffMs = now - this->lastPacketTimeMs;
uint32_t diffRtpTimestamp = diffMs * this->clockRate / 1000;
report->SetRtpTs(this->lastPacketRtpTimestamp + diffRtpTimestamp);
return report;
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论