提交 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 ...@@ -18,8 +18,8 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
{ {
super(options); super(options);
// Map of RtpReceivers indexed by SSRC. // Map of RtpReceivers indexed by MediaStreamTrack.id (a=label).
this._mapRtpReceivers = new Map(); this._mapTrackToRtpReceiver = new Map();
} }
_setUpOffer() _setUpOffer()
...@@ -102,38 +102,27 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon ...@@ -102,38 +102,27 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
for (let parsedMedia of parsed.media) for (let parsedMedia of parsed.media)
{ {
// If the answer has RTX or FEC it will have a=ssrc-group attribute(s) // Get all the media ssrcs.
// so media SSRC is the first value on it. Example: let ssrcs = sdpUtils.getPlanBSsrcs(parsedMedia);
// 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);
promises.push(promise); for (let ssrc of ssrcs)
}
}
// Otherwise (no RTX/FEC) each a=ssrc means a sending MediaStreamTrack.
else if (Array.isArray(parsedMedia.ssrcs))
{ {
let mediaSsrcs = new Set(); // Get MediaStream and MediaStreamTracks info.
let trackInfo = sdpUtils.getPlanBTrackInfo(parsedMedia, ssrc);
for (let ssrcObj of parsedMedia.ssrcs) let promise = this._createRtpReceiverForSsrc(parsedMedia, ssrc, trackInfo)
{ .then((rtpReceiver) =>
let mediaSsrc = ssrcObj.id; {
// Store it in the map.
if (mediaSsrcs.has(mediaSsrc)) this._mapTrackToRtpReceiver.set(trackInfo.trackId, rtpReceiver);
continue;
// When closed, remove from the map.
mediaSsrcs.add(mediaSsrc); rtpReceiver.on('close', () =>
{
let promise = this._createRtpReceiverForSsrc(mediaSsrc, parsedMedia); this._mapTrackToRtpReceiver.delete(trackInfo.trackId);
});
});
promises.push(promise); promises.push(promise);
}
} }
} }
...@@ -151,68 +140,51 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon ...@@ -151,68 +140,51 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
// Check new/removed RtpReceivers. // Check new/removed RtpReceivers.
.then(() => .then(() =>
{ {
let receivingMediaSsrcs = []; let receivingTrackIds = new Set();
let promises = []; let promises = [];
for (let parsedMedia of parsed.media) for (let parsedMedia of parsed.media)
{ {
// If the answer has RTX or FEC it will have a=ssrc-group attribute(s) // Get all the media ssrcs.
// so media SSRC is the first value on it. Example: let ssrcs = sdpUtils.getPlanBSsrcs(parsedMedia);
// 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);
promises.push(promise); for (let ssrc of ssrcs)
}
}
}
// Otherwise (no RTX/FEC) each a=ssrc means a sending MediaStreamTrack.
else if (Array.isArray(parsedMedia.ssrcs))
{ {
let mediaSsrcs = new Set(); // Get MediaStream and MediaStreamTracks info.
let trackInfo = sdpUtils.getPlanBTrackInfo(parsedMedia, ssrc);
for (let ssrcObj of parsedMedia.ssrcs)
{
let mediaSsrc = ssrcObj.id;
if (mediaSsrcs.has(mediaSsrc)) receivingTrackIds.add(trackInfo.trackId);
continue;
mediaSsrcs.add(mediaSsrc); if (!this._mapTrackToRtpReceiver.has(trackInfo.trackId))
receivingMediaSsrcs.push(mediaSsrc); {
this._logger.debug('_handleRemoteReAnswer() | new receiving track [ssrc:%s, trackId:"%s"]', ssrc, trackInfo.trackId);
if (!this._mapRtpReceivers.has(mediaSsrc)) let promise = this._createRtpReceiverForSsrc(parsedMedia, ssrc, trackInfo)
{ .then((rtpReceiver) =>
this._logger.debug('_handleRemoteReAnswer() | new receiving SSRC [ssrc:%s]', mediaSsrc); {
// 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. // 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(); rtpReceiver.close();
} }
...@@ -458,6 +430,9 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon ...@@ -458,6 +430,9 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
// a=rtcp-rsize // a=rtcp-rsize
objMedia.rtcpRsize = 'rtcp-rsize'; objMedia.rtcpRsize = 'rtcp-rsize';
// a=x-google-flag:conference.
objMedia.xGoogleFlag = 'conference';
// Bandwidth. // Bandwidth.
if (self._options.bandwidth && self._options.bandwidth[kind]) if (self._options.bandwidth && self._options.bandwidth[kind])
{ {
...@@ -472,10 +447,13 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon ...@@ -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. // TODO: We assume no RTX/FEC in client's SDP.
if (!Array.isArray(mediaSsrcs))
mediaSsrcs = [ mediaSsrcs ];
let transport = this._peer.transports[0]; let transport = this._peer.transports[0];
let kind = parsedMedia.type; let kind = parsedMedia.type;
// Map of RtpCodecParameters indexed by payload type. // Map of RtpCodecParameters indexed by payload type.
...@@ -537,70 +515,38 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon ...@@ -537,70 +515,38 @@ class RTCPeerConnectionPlanB extends RTCPeerConnectionCommon
} }
// Array of RtpEncodingParameters. // Array of RtpEncodingParameters.
// NOTE: Just a single encoding will be created with the first media codec let encodings = [];
// in the answer. // NOTE: Here we assume that the first codec in the list is the media one.
let encodings = let codecPayloadType = mapCodecs.values().next().value.payloadType;
[
{
ssrc : mediaSsrc,
codecPayloadType : mapCodecs.values().next().value.payloadType
}
];
// Array of RtpHeaderExtensionParameters.
let headerExtensions = sdpUtils.descToRtpHeaderExtensionParameters(parsedMedia);
let cname; for (let ssrc of mediaSsrcs)
let msid;
for (let ssrc of parsedMedia.ssrcs)
{ {
if (ssrc.id !== mediaSsrc) encodings.push(
continue; {
ssrc : ssrc,
switch (ssrc.attribute) codecPayloadType : codecPayloadType
{ });
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;
}
} }
// Array of RtpHeaderExtensionParameters.
let headerExtensions = sdpUtils.descToRtpHeaderExtensionParameters(parsedMedia);
// Create a RtpReceiver. // Create a RtpReceiver.
let rtpReceiver = this._peer.RtpReceiver(kind, transport); 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. // RtpParameters object.
let rtpParameters = let rtpParameters =
{ {
muxId : parsedMedia.mid,
codecs : Array.from(mapCodecs.values()), codecs : Array.from(mapCodecs.values()),
encodings : encodings, encodings : encodings,
headerExtensions : headerExtensions, headerExtensions : headerExtensions,
rtcp : rtcp :
{ {
cname : cname cname : trackInfo.cname
}, },
userParameters : userParameters :
{ {
msid : msid msid : trackInfo.msid
} }
}; };
......
...@@ -193,5 +193,103 @@ module.exports = ...@@ -193,5 +193,103 @@ module.exports =
} }
return headerExtensions; 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 ...@@ -270,7 +270,7 @@ namespace RTC
public: public:
std::string cname; std::string cname;
uint32_t ssrc = 0; uint32_t ssrc = 0;
bool reducedSize = false; bool reducedSize = true;
}; };
class RtpParameters class RtpParameters
...@@ -282,8 +282,7 @@ namespace RTC ...@@ -282,8 +282,7 @@ namespace RTC
Json::Value toJson(); Json::Value toJson();
void ReduceCodecsAndEncodings(RtpCapabilities& capabilities); void ReduceCodecsAndEncodings(RtpCapabilities& capabilities);
void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions); void ReduceHeaderExtensions(std::vector<RtpHeaderExtension>& supportedHeaderExtensions);
uint32_t GetEncodingClockRate(size_t encodingIdx); uint32_t GetClockRateForEncoding(RtpEncodingParameters& encoding);
uint32_t GetEncodingMediaSsrc(size_t encodingIdx);
private: private:
void ValidateCodecs(); void ValidateCodecs();
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "Channel/Request.hpp" #include "Channel/Request.hpp"
#include "Channel/Notifier.hpp" #include "Channel/Notifier.hpp"
#include <string> #include <string>
#include <map>
#include <json/json.h> #include <json/json.h>
namespace RTC namespace RTC
...@@ -55,7 +56,7 @@ namespace RTC ...@@ -55,7 +56,7 @@ namespace RTC
void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet); void ReceiveRtcpFeedback(RTC::RTCP::FeedbackRtpPacket* packet);
private: private:
void FillRtpParameters(); void ClearRtpStreams();
public: public:
// Passed by argument. // Passed by argument.
...@@ -69,8 +70,7 @@ namespace RTC ...@@ -69,8 +70,7 @@ namespace RTC
RTC::Transport* transport = nullptr; RTC::Transport* transport = nullptr;
// Allocated by this. // Allocated by this.
RTC::RtpParameters* rtpParameters = nullptr; RTC::RtpParameters* rtpParameters = nullptr;
// TODO: For now just one. std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
RTC::RtpStreamRecv* rtpStream = nullptr;
// Others. // Others.
bool rtpRawEventEnabled = false; bool rtpRawEventEnabled = false;
bool rtpObjectEventEnabled = false; bool rtpObjectEventEnabled = false;
...@@ -109,10 +109,13 @@ namespace RTC ...@@ -109,10 +109,13 @@ namespace RTC
inline inline
void RtpReceiver::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report) void RtpReceiver::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{ {
// TODO: This assumes a single stream for now. auto it = this->rtpStreams.find(report->GetSsrc());
if (this->rtpStream) if (it != this->rtpStreams.end())
this->rtpStream->ReceiveRtcpSenderReport(report); {
auto rtpStream = it->second;
rtpStream->ReceiveRtcpSenderReport(report);
}
} }
} }
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "RTC/RtpStream.hpp" #include "RTC/RtpStream.hpp"
#include "RTC/RTCP/ReceiverReport.hpp" #include "RTC/RTCP/ReceiverReport.hpp"
#include "RTC/RTCP/SenderReport.hpp" #include "RTC/RTCP/SenderReport.hpp"
#include <json/json.h>
namespace RTC namespace RTC
{ {
...@@ -14,6 +15,7 @@ namespace RTC ...@@ -14,6 +15,7 @@ namespace RTC
explicit RtpStreamRecv(uint32_t clockRate); explicit RtpStreamRecv(uint32_t clockRate);
virtual ~RtpStreamRecv(); virtual ~RtpStreamRecv();
Json::Value toJson();
virtual bool ReceivePacket(RTC::RtpPacket* packet); virtual bool ReceivePacket(RTC::RtpPacket* packet);
RTC::RTCP::ReceiverReport* GetRtcpReceiverReport(); RTC::RTCP::ReceiverReport* GetRtcpReceiverReport();
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report); void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "RTC/RTCP/SenderReport.hpp" #include "RTC/RTCP/SenderReport.hpp"
#include <vector> #include <vector>
#include <list> #include <list>
#include <json/json.h>
namespace RTC namespace RTC
{ {
...@@ -28,6 +29,7 @@ namespace RTC ...@@ -28,6 +29,7 @@ namespace RTC
RtpStreamSend(uint32_t clockRate, size_t bufferSize); RtpStreamSend(uint32_t clockRate, size_t bufferSize);
virtual ~RtpStreamSend(); virtual ~RtpStreamSend();
Json::Value toJson();
bool ReceivePacket(RTC::RtpPacket* packet); bool ReceivePacket(RTC::RtpPacket* packet);
void RequestRtpRetransmission(uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container); void RequestRtpRetransmission(uint16_t seq, uint16_t bitmask, std::vector<RTC::RtpPacket*>& container);
RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now); RTC::RTCP::SenderReport* GetRtcpSenderReport(uint64_t now);
...@@ -35,7 +37,6 @@ namespace RTC ...@@ -35,7 +37,6 @@ namespace RTC
private: private:
void ClearBuffer(); void ClearBuffer();
void StorePacket(RTC::RtpPacket* packet); void StorePacket(RTC::RtpPacket* packet);
void Dump();
private: private:
std::vector<StorageItem> storage; std::vector<StorageItem> storage;
......
...@@ -246,18 +246,14 @@ namespace RTC ...@@ -246,18 +246,14 @@ namespace RTC
this->headerExtensions = updatedHeaderExtensions; this->headerExtensions = updatedHeaderExtensions;
} }
uint32_t RtpParameters::GetEncodingClockRate(size_t encodingIdx) uint32_t RtpParameters::GetClockRateForEncoding(RtpEncodingParameters& encoding)
{ {
MS_TRACE(); MS_TRACE();
if (this->encodings.size() < encodingIdx + 1) uint8_t payloadType = encoding.codecPayloadType;
MS_ABORT("no such a encoding [encodingIdx:%zu]", encodingIdx);
uint8_t payloadType = this->encodings[encodingIdx].codecPayloadType;
uint32_t clockRate = 0; uint32_t clockRate = 0;
auto it = this->codecs.begin(); auto it = this->codecs.begin();
for (; it != this->codecs.end(); ++it) for (; it != this->codecs.end(); ++it)
{ {
auto& codec = *it; auto& codec = *it;
...@@ -272,22 +268,12 @@ namespace RTC ...@@ -272,22 +268,12 @@ namespace RTC
// This should never happen. // This should never happen.
if (it == this->codecs.end()) 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; 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 inline
void RtpParameters::ValidateCodecs() void RtpParameters::ValidateCodecs()
{ {
......
...@@ -266,6 +266,18 @@ namespace RTC ...@@ -266,6 +266,18 @@ namespace RTC
{ {
MS_TRACE(); 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. // First lookup into the SSRC table.
{ {
auto it = this->ssrcTable.find(packet->GetSsrc()); auto it = this->ssrcTable.find(packet->GetSsrc());
...@@ -291,8 +303,10 @@ namespace RTC ...@@ -291,8 +303,10 @@ namespace RTC
} }
} }
// TODO: RID table? Sure.
// Otherwise lookup into the muxId table. // Otherwise lookup into the muxId table.
// TODO: do it // TODO: do it.
// Otherwise lookup into the PT table. // Otherwise lookup into the PT table.
{ {
......
...@@ -36,8 +36,7 @@ namespace RTC ...@@ -36,8 +36,7 @@ namespace RTC
if (this->rtpParameters) if (this->rtpParameters)
delete this->rtpParameters; delete this->rtpParameters;
if (this->rtpStream) ClearRtpStreams();
delete this->rtpStream;
} }
void RtpReceiver::Close() void RtpReceiver::Close()
...@@ -69,6 +68,9 @@ namespace RTC ...@@ -69,6 +68,9 @@ namespace RTC
static const Json::StaticString k_hasTransport("hasTransport"); static const Json::StaticString k_hasTransport("hasTransport");
static const Json::StaticString k_rtpRawEventEnabled("rtpRawEventEnabled"); static const Json::StaticString k_rtpRawEventEnabled("rtpRawEventEnabled");
static const Json::StaticString k_rtpObjectEventEnabled("rtpObjectEventEnabled"); 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); Json::Value json(Json::objectValue);
...@@ -87,6 +89,20 @@ namespace RTC ...@@ -87,6 +89,20 @@ namespace RTC
json[k_rtpObjectEventEnabled] = this->rtpObjectEventEnabled; 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; return json;
} }
...@@ -151,26 +167,42 @@ namespace RTC ...@@ -151,26 +167,42 @@ namespace RTC
return; return;
} }
// Free the previous rtpParameters. // Free the previous parameters.
if (previousRtpParameters) if (previousRtpParameters)
delete previousRtpParameters; delete previousRtpParameters;
// Free previous RTP streams.
ClearRtpStreams();
Json::Value data = this->rtpParameters->toJson(); Json::Value data = this->rtpParameters->toJson();
request->Accept(data); request->Accept(data);
// Fill RTP parameters.
FillRtpParameters();
// And notify again. // And notify again.
this->listener->onRtpReceiverParametersDone(this); this->listener->onRtpReceiverParametersDone(this);
// Set the RtpStreamRecv. // Set the RtpStreamRecv instances.
// TODO: This assumes a single receiving stream for now. for (auto& encoding : this->rtpParameters->encodings)
uint32_t streamClockRate = this->rtpParameters->GetEncodingClockRate(0); {
// 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. // Don't create a RtpStreamRecv if there is already one for the same SSRC.
this->rtpStream = new RTC::RtpStreamRecv(streamClockRate); // 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; break;
} }
...@@ -234,13 +266,22 @@ namespace RTC ...@@ -234,13 +266,22 @@ namespace RTC
// TODO: Check if stopped, etc (not yet done). // TODO: Check if stopped, etc (not yet done).
// TODO: Handle multiple streams. // Find the corresponding RtpStreamRecv.
MS_ASSERT(this->rtpStream, "no RtpStream set"); 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. // Process the packet.
// TODO: Must check what kind of packet we are checking. For example, RTX // TODO: Must check what kind of packet we are checking. For example, RTX
// packets (once implemented) should have a different handling. // packets (once implemented) should have a different handling.
if (!this->rtpStream->ReceivePacket(packet)) if (!rtpStream->ReceivePacket(packet))
return; return;
// Notify the listener. // Notify the listener.
...@@ -276,34 +317,22 @@ namespace RTC ...@@ -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) void RtpReceiver::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{ {
if (this->rtpStream) if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
{ return;
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
RTC::RTCP::ReceiverReport* report = this->rtpStream->GetRtcpReceiverReport();
// TODO: This assumes a single stream for now. for (auto& kv : this->rtpStreams)
report->SetSsrc(this->rtpParameters->GetEncodingMediaSsrc(0)); {
packet->AddReceiverReport(report); 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) void RtpReceiver::ReceiveRtcpFeedback(RTC::RTCP::FeedbackPsPacket* packet)
...@@ -345,4 +374,18 @@ namespace RTC ...@@ -345,4 +374,18 @@ namespace RTC
this->transport->SendRtcpPacket(packet); 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 @@ ...@@ -4,6 +4,7 @@
#include "RTC/RtpSender.hpp" #include "RTC/RtpSender.hpp"
#include "RTC/RTCP/SenderReport.hpp" #include "RTC/RTCP/SenderReport.hpp"
#include "RTC/RTCP/FeedbackRtpNack.hpp" #include "RTC/RTCP/FeedbackRtpNack.hpp"
#include "Utils.hpp"
#include "MediaSoupError.hpp" #include "MediaSoupError.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <unordered_set> #include <unordered_set>
...@@ -70,6 +71,7 @@ namespace RTC ...@@ -70,6 +71,7 @@ namespace RTC
static const Json::StaticString k_hasTransport("hasTransport"); static const Json::StaticString k_hasTransport("hasTransport");
static const Json::StaticString k_available("available"); static const Json::StaticString k_available("available");
static const Json::StaticString k_supportedPayloadTypes("supportedPayloadTypes"); static const Json::StaticString k_supportedPayloadTypes("supportedPayloadTypes");
static const Json::StaticString k_rtpStream("rtpStream");
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
...@@ -93,6 +95,9 @@ namespace RTC ...@@ -93,6 +95,9 @@ namespace RTC
json[k_supportedPayloadTypes].append((Json::UInt)payloadType); json[k_supportedPayloadTypes].append((Json::UInt)payloadType);
} }
if (this->rtpStream)
json[k_rtpStream] = this->rtpStream->toJson();
return json; return json;
} }
...@@ -194,17 +199,32 @@ namespace RTC ...@@ -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. // Remove unsupported header extensions.
this->rtpParameters->ReduceHeaderExtensions(this->peerCapabilities->headerExtensions); 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 there are no encodings set not available.
if (this->rtpParameters->encodings.size() > 0) if (this->rtpParameters->encodings.size() > 0)
{ {
this->available = true; this->available = true;
// NOTE: We assume a single stream/encoding when sending to remote peers.
auto encoding = this->rtpParameters->encodings[0];
// Set the RtpStreamSend. // Set the RtpStreamSend.
// NOTE: We assume a single stream when sending to remote peers. uint32_t streamClockRate = this->rtpParameters->GetClockRateForEncoding(encoding);
uint32_t streamClockRate = this->rtpParameters->GetEncodingClockRate(0);
// Create a RtpStreamSend for sending a single media stream. // Create a RtpStreamSend for sending a single media stream.
switch (this->kind) switch (this->kind)
...@@ -255,6 +275,16 @@ namespace RTC ...@@ -255,6 +275,16 @@ namespace RTC
MS_ASSERT(this->rtpStream, "no RtpStream set"); 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. // Process the packet.
// TODO: Must check what kind of packet we are checking. For example, RTX // TODO: Must check what kind of packet we are checking. For example, RTX
// packets (once implemented) should have a different handling. // packets (once implemented) should have a different handling.
...@@ -283,32 +313,31 @@ namespace RTC ...@@ -283,32 +313,31 @@ namespace RTC
void RtpSender::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now) void RtpSender::GetRtcp(RTC::RTCP::CompoundPacket *packet, uint64_t now)
{ {
if (this->rtpStream) if (!this->rtpStream)
{ return;
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) >= this->maxRtcpInterval)
{
RTC::RTCP::SenderReport* report = this->rtpStream->GetRtcpSenderReport(now);
if (report) if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
{ return;
// TODO: This assumes a single stream for now.
uint32_t ssrc = this->rtpParameters->GetEncodingMediaSsrc(0);
std::string cname = this->rtpParameters->rtcp.cname;
report->SetSsrc(ssrc); RTC::RTCP::SenderReport* report = this->rtpStream->GetRtcpSenderReport(now);
packet->AddSenderReport(report); if (!report)
return;
// Build sdes chunk for this sender. // NOTE: This assumes a single stream.
RTC::RTCP::SdesChunk *sdesChunk = new RTC::RTCP::SdesChunk(ssrc); uint32_t ssrc = this->rtpParameters->encodings[0].ssrc;
RTC::RTCP::SdesItem *sdesItem = new RTC::RTCP::SdesItem(RTC::RTCP::SdesItem::Type::CNAME, cname.size(), cname.c_str()); std::string cname = this->rtpParameters->rtcp.cname;
sdesChunk->AddItem(sdesItem); report->SetSsrc(ssrc);
packet->AddSdesChunk(sdesChunk); 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) void RtpSender::ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket)
......
...@@ -20,6 +20,29 @@ namespace RTC ...@@ -20,6 +20,29 @@ namespace RTC
MS_TRACE(); 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) bool RtpStreamRecv::ReceivePacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -49,6 +49,27 @@ namespace RTC ...@@ -49,6 +49,27 @@ namespace RTC
ClearBuffer(); 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) bool RtpStreamSend::ReceivePacket(RTC::RtpPacket* packet)
{ {
MS_TRACE(); MS_TRACE();
...@@ -203,6 +224,33 @@ namespace RTC ...@@ -203,6 +224,33 @@ namespace RTC
container[container_idx] = nullptr; 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() void RtpStreamSend::ClearBuffer()
{ {
MS_TRACE(); MS_TRACE();
...@@ -293,47 +341,4 @@ namespace RTC ...@@ -293,47 +341,4 @@ namespace RTC
// Update the new buffer item so it points to the cloned packed. // Update the new buffer item so it points to the cloned packed.
(*new_buffer_it).packet = packet->Clone(store); (*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 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论