Unverified 提交 bfbb839c authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Producer improvements (related to issue #235) (#238)

* - Producer.cpp: Remove UpdateRtpParameters(). It was broken since Consumers
  were not notified about profile removed and so on, so they may crash.
- Producer.cpp: Remove some maps and simplify streams handling by having a
  single mapSsrcRtpStreamInfo. Just keep mapActiveProfiles because
  GetActiveProfiles() method needs it.
- Producer::MayNeedNewStream(): Ignore new media streams with new SSRC if
  its RID is already in use by other media stream (should fix #235).
上级 b78b3682
...@@ -580,19 +580,6 @@ class Peer extends EnhancedEventEmitter ...@@ -580,19 +580,6 @@ class Peer extends EnhancedEventEmitter
break; break;
} }
case 'updateProducer':
{
const { id, rtpParameters } = notification;
const producer = this._producers.get(id);
if (!producer)
throw new Error('Producer not found');
producer.updateRtpParameters(rtpParameters);
break;
}
case 'pauseProducer': case 'pauseProducer':
{ {
const { id, appData } = notification; const { id, appData } = notification;
......
...@@ -473,37 +473,6 @@ class Producer extends EnhancedEventEmitter ...@@ -473,37 +473,6 @@ class Producer extends EnhancedEventEmitter
}); });
} }
/**
* Change receiving RTP parameters.
*
* @private
*
* @param {RTCRtpParameters} rtpParameters - New RTP parameters.
*
* @return {Promise}
*/
updateRtpParameters(rtpParameters)
{
logger.debug('updateRtpParameters()');
if (this._closed)
return Promise.reject(new errors.InvalidStateError('Producer closed'));
return this._channel.request(
'producer.updateRtpParameters', this._internal, { rtpParameters })
.then(() =>
{
logger.debug('"producer.updateRtpParameters" request succeeded');
this._data.rtpParameters = rtpParameters;
})
.catch((error) =>
{
logger.error(
'"producer.updateRtpParameters" request failed: %s', String(error));
});
}
/** /**
* Get the Producer stats. * Get the Producer stats.
* *
......
...@@ -39,7 +39,6 @@ namespace Channel ...@@ -39,7 +39,6 @@ namespace Channel
PRODUCER_CLOSE, PRODUCER_CLOSE,
PRODUCER_DUMP, PRODUCER_DUMP,
PRODUCER_GET_STATS, PRODUCER_GET_STATS,
PRODUCER_UPDATE_RTP_PARAMETERS,
PRODUCER_PAUSE, PRODUCER_PAUSE,
PRODUCER_RESUME, PRODUCER_RESUME,
PRODUCER_SET_PREFERRED_PROFILE, PRODUCER_SET_PREFERRED_PROFILE,
......
...@@ -51,7 +51,6 @@ namespace RTC ...@@ -51,7 +51,6 @@ namespace RTC
void SourceResume(); void SourceResume();
void AddProfile(const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream); void AddProfile(const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream);
void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile); void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile);
void SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile); void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile); void SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetEncodingPreferences(const RTC::Codecs::EncodingContext::Preferences preferences); void SetEncodingPreferences(const RTC::Codecs::EncodingContext::Preferences preferences);
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "handles/Timer.hpp" #include "handles/Timer.hpp"
#include <json/json.h> #include <json/json.h>
#include <map> #include <map>
#include <set>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
...@@ -39,6 +38,16 @@ namespace RTC ...@@ -39,6 +38,16 @@ namespace RTC
uint8_t rid{ 0 }; // 0 means no RID id. uint8_t rid{ 0 }; // 0 means no RID id.
}; };
private:
struct RtpStreamInfo
{
RTC::RtpStreamRecv* rtpStream{ nullptr };
std::string rid{};
RTC::RtpEncodingParameters::Profile profile{ RTC::RtpEncodingParameters::Profile::NONE };
uint32_t rtxSsrc{ 0 };
bool active{ false };
};
public: public:
Producer( Producer(
Channel::Notifier* notifier, Channel::Notifier* notifier,
...@@ -59,7 +68,6 @@ namespace RTC ...@@ -59,7 +68,6 @@ namespace RTC
Json::Value GetStats() const; Json::Value GetStats() const;
void AddListener(RTC::ProducerListener* listener); void AddListener(RTC::ProducerListener* listener);
void RemoveListener(RTC::ProducerListener* listener); void RemoveListener(RTC::ProducerListener* listener);
void UpdateRtpParameters(RTC::RtpParameters& rtpParameters);
void Pause(); void Pause();
void Resume(); void Resume();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile); void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
...@@ -79,12 +87,10 @@ namespace RTC ...@@ -79,12 +87,10 @@ namespace RTC
void FillHeaderExtensionIds(); void FillHeaderExtensionIds();
void MayNeedNewStream(RTC::RtpPacket* packet); void MayNeedNewStream(RTC::RtpPacket* packet);
void CreateRtpStream(RTC::RtpEncodingParameters& encoding, uint32_t ssrc); void CreateRtpStream(RTC::RtpEncodingParameters& encoding, uint32_t ssrc);
void ClearRtpStream(RTC::RtpStreamRecv* rtpStream);
void ClearRtpStreams();
void ApplyRtpMapping(RTC::RtpPacket* packet) const; void ApplyRtpMapping(RTC::RtpPacket* packet) const;
RTC::RtpEncodingParameters::Profile GetProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet); void ActivateStream(RTC::RtpStreamRecv* rtpStream);
void ActivateStreamProfiles(RTC::RtpStreamRecv* rtpStream); void DeactivateStream(RTC::RtpStreamRecv* rtpStream);
void DeactivateStreamProfiles(RTC::RtpStreamRecv* rtpStream); // TODO: Remove?
bool IsStreamActive(const RTC::RtpStream* rtpStream) const; bool IsStreamActive(const RTC::RtpStream* rtpStream) const;
/* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */ /* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */
...@@ -112,9 +118,7 @@ namespace RTC ...@@ -112,9 +118,7 @@ namespace RTC
struct RtpMapping rtpMapping; struct RtpMapping rtpMapping;
std::unordered_set<RTC::ProducerListener*> listeners; std::unordered_set<RTC::ProducerListener*> listeners;
// Allocated by this. // Allocated by this.
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams; std::map<uint32_t, RtpStreamInfo> mapSsrcRtpStreamInfo;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
std::map<RTC::RtpStreamRecv*, std::set<RTC::RtpEncodingParameters::Profile>> mapRtpStreamProfiles;
std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStream*> mapActiveProfiles; std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStream*> mapActiveProfiles;
Timer* keyFrameRequestBlockTimer{ nullptr }; Timer* keyFrameRequestBlockTimer{ nullptr };
// Others. // Others.
...@@ -169,11 +173,11 @@ namespace RTC ...@@ -169,11 +173,11 @@ namespace RTC
inline void Producer::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report) inline void Producer::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{ {
auto it = this->rtpStreams.find(report->GetSsrc()); auto it = this->mapSsrcRtpStreamInfo.find(report->GetSsrc());
if (it != this->mapSsrcRtpStreamInfo.end())
if (it != this->rtpStreams.end())
{ {
auto rtpStream = it->second; auto& info = it->second;
auto* rtpStream = info.rtpStream;
rtpStream->ReceiveRtcpSenderReport(report); rtpStream->ReceiveRtcpSenderReport(report);
} }
......
...@@ -13,10 +13,9 @@ namespace RTC ...@@ -13,10 +13,9 @@ namespace RTC
class ProducerListener class ProducerListener
{ {
public: public:
virtual void OnProducerClosed(RTC::Producer* producer) = 0; virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpParametersUpdated(RTC::Producer* producer) = 0; virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerPaused(RTC::Producer* producer) = 0; virtual void OnProducerResumed(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpPacket( virtual void OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0; RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0;
virtual void OnProducerProfileEnabled( virtual void OnProducerProfileEnabled(
......
...@@ -64,7 +64,6 @@ namespace RTC ...@@ -64,7 +64,6 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */ /* Pure virtual methods inherited from RTC::ProducerListener. */
public: public:
void OnProducerClosed(RTC::Producer* producer) override; void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override; void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override; void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket( void OnProducerRtpPacket(
......
...@@ -97,7 +97,6 @@ namespace RTC ...@@ -97,7 +97,6 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */ /* Pure virtual methods inherited from RTC::ProducerListener. */
public: public:
void OnProducerClosed(RTC::Producer* producer) override; void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override; void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override; void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket( void OnProducerRtpPacket(
......
...@@ -34,7 +34,6 @@ namespace Channel ...@@ -34,7 +34,6 @@ namespace Channel
{ "producer.close", Request::MethodId::PRODUCER_CLOSE }, { "producer.close", Request::MethodId::PRODUCER_CLOSE },
{ "producer.dump", Request::MethodId::PRODUCER_DUMP }, { "producer.dump", Request::MethodId::PRODUCER_DUMP },
{ "producer.getStats", Request::MethodId::PRODUCER_GET_STATS }, { "producer.getStats", Request::MethodId::PRODUCER_GET_STATS },
{ "producer.updateRtpParameters", Request::MethodId::PRODUCER_UPDATE_RTP_PARAMETERS },
{ "producer.pause", Request::MethodId::PRODUCER_PAUSE }, { "producer.pause", Request::MethodId::PRODUCER_PAUSE },
{ "producer.resume" , Request::MethodId::PRODUCER_RESUME }, { "producer.resume" , Request::MethodId::PRODUCER_RESUME },
{ "producer.setPreferredProfile", Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE }, { "producer.setPreferredProfile", Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE },
......
...@@ -256,16 +256,6 @@ namespace RTC ...@@ -256,16 +256,6 @@ namespace RTC
} }
} }
void Consumer::SourceRtpParametersUpdated()
{
MS_TRACE();
if (!IsEnabled())
return;
this->syncRequired = true;
}
void Consumer::AddProfile( void Consumer::AddProfile(
const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream) const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream)
{ {
......
...@@ -49,9 +49,14 @@ namespace RTC ...@@ -49,9 +49,14 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
this->mapRtpStreamProfiles.clear(); // Must delete all the RecvRtpStream instances.
for (auto& kv : this->mapSsrcRtpStreamInfo)
{
auto& info = kv.second;
auto* rtpStream = info.rtpStream;
ClearRtpStreams(); delete rtpStream;
}
} }
void Producer::Destroy() void Producer::Destroy()
...@@ -76,17 +81,26 @@ namespace RTC ...@@ -76,17 +81,26 @@ namespace RTC
static const Json::StaticString JsonStringProducerId{ "producerId" }; static const Json::StaticString JsonStringProducerId{ "producerId" };
static const Json::StaticString JsonStringKind{ "kind" }; static const Json::StaticString JsonStringKind{ "kind" };
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" }; static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
static const Json::StaticString JsonStringRtpStreams{ "rtpStreams" }; static const Json::StaticString JsonStringRtpStreamInfos{ "rtpStreamInfos" };
static const Json::StaticString JsonStringRtpStream{ "rtpStream" };
static const Json::StaticString JsonStringRid{ "rid" };
static const Json::StaticString JsonStringProfile{ "profile" };
static const Json::StaticString JsonStringNone{ "none" };
static const Json::StaticString JsonStringDefault{ "default" };
static const Json::StaticString JsonStringLow{ "low" };
static const Json::StaticString JsonStringMedium{ "medium" };
static const Json::StaticString JsonStringHigh{ "high" };
static const Json::StaticString JsonStringRtxSsrc{ "rtxSsrc" };
static const Json::StaticString JsonStringActive{ "active" };
static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" }; static const Json::StaticString JsonStringHeaderExtensionIds{ "headerExtensionIds" };
static const Json::StaticString JsonStringSsrcAudioLevel{ "ssrcAudioLevel" }; static const Json::StaticString JsonStringSsrcAudioLevel{ "ssrcAudioLevel" };
static const Json::StaticString JsonStringAbsSendTime{ "absSendTime" }; static const Json::StaticString JsonStringAbsSendTime{ "absSendTime" };
static const Json::StaticString JsonStringRid{ "rid" };
static const Json::StaticString JsonStringPaused{ "paused" }; static const Json::StaticString JsonStringPaused{ "paused" };
static const Json::StaticString JsonStringLossPercentage{ "lossPercentage" }; static const Json::StaticString JsonStringLossPercentage{ "lossPercentage" };
Json::Value json(Json::objectValue); Json::Value json(Json::objectValue);
Json::Value jsonHeaderExtensionIds(Json::objectValue); Json::Value jsonHeaderExtensionIds(Json::objectValue);
Json::Value jsonRtpStreams(Json::arrayValue); Json::Value jsonRtpStreamInfos(Json::arrayValue);
json[JsonStringProducerId] = Json::UInt{ this->producerId }; json[JsonStringProducerId] = Json::UInt{ this->producerId };
...@@ -96,18 +110,47 @@ namespace RTC ...@@ -96,18 +110,47 @@ namespace RTC
float lossPercentage = 0; float lossPercentage = 0;
for (auto& kv : this->rtpStreams) for (auto& kv : this->mapSsrcRtpStreamInfo)
{ {
auto rtpStream = kv.second; Json::Value jsonRtpStreamInfo(Json::objectValue);
auto& info = kv.second;
jsonRtpStreams.append(rtpStream->ToJson()); jsonRtpStreamInfo[JsonStringRtpStream] = info.rtpStream->ToJson();
lossPercentage += rtpStream->GetLossPercentage();
jsonRtpStreamInfo[JsonStringRid] = info.rid;
jsonRtpStreamInfo[JsonStringRtxSsrc] = info.rtxSsrc;
jsonRtpStreamInfo[JsonStringActive] = info.active;
switch (info.profile)
{
case RTC::RtpEncodingParameters::Profile::NONE:
jsonRtpStreamInfo[JsonStringProfile] = JsonStringNone;
break;
case RTC::RtpEncodingParameters::Profile::DEFAULT:
jsonRtpStreamInfo[JsonStringProfile] = JsonStringDefault;
break;
case RTC::RtpEncodingParameters::Profile::LOW:
jsonRtpStreamInfo[JsonStringProfile] = JsonStringLow;
break;
case RTC::RtpEncodingParameters::Profile::MEDIUM:
jsonRtpStreamInfo[JsonStringProfile] = JsonStringMedium;
break;
case RTC::RtpEncodingParameters::Profile::HIGH:
jsonRtpStreamInfo[JsonStringProfile] = JsonStringHigh;
break;
}
jsonRtpStreamInfos.append(jsonRtpStreamInfo);
lossPercentage += info.rtpStream->GetLossPercentage();
} }
json[JsonStringRtpStreams] = jsonRtpStreams; json[JsonStringRtpStreamInfos] = jsonRtpStreamInfos;
if (!this->rtpStreams.empty()) if (!this->mapSsrcRtpStreamInfo.empty())
lossPercentage = lossPercentage / this->rtpStreams.size(); lossPercentage = lossPercentage / this->mapSsrcRtpStreamInfo.size();
json[JsonStringLossPercentage] = lossPercentage; json[JsonStringLossPercentage] = lossPercentage;
...@@ -135,15 +178,14 @@ namespace RTC ...@@ -135,15 +178,14 @@ namespace RTC
Json::Value json(Json::arrayValue); Json::Value json(Json::arrayValue);
for (auto it : this->rtpStreams) for (auto& kv : this->mapSsrcRtpStreamInfo)
{ {
auto rtpStream = it.second; auto& info = kv.second;
auto* rtpStream = info.rtpStream;
auto jsonRtpStream = rtpStream->GetStats(); auto jsonRtpStream = rtpStream->GetStats();
if (this->transport != nullptr) if (this->transport != nullptr)
{
jsonRtpStream[JsonStringTransportId] = this->transport->transportId; jsonRtpStream[JsonStringTransportId] = this->transport->transportId;
}
json.append(jsonRtpStream); json.append(jsonRtpStream);
} }
...@@ -151,24 +193,6 @@ namespace RTC ...@@ -151,24 +193,6 @@ namespace RTC
return json; return json;
} }
void Producer::UpdateRtpParameters(RTC::RtpParameters& rtpParameters)
{
MS_TRACE();
this->rtpParameters = rtpParameters;
// Clear previous RtpStreamRecv instances.
ClearRtpStreams();
for (auto& listener : this->listeners)
{
// NOTE: This may throw.
listener->OnProducerRtpParametersUpdated(this);
}
MS_DEBUG_DEV("Producer RTP parameters updated [producerId:%" PRIu32 "]", this->producerId);
}
void Producer::Pause() void Producer::Pause()
{ {
MS_TRACE(); MS_TRACE();
...@@ -220,11 +244,17 @@ namespace RTC ...@@ -220,11 +244,17 @@ namespace RTC
// Find the corresponding RtpStreamRecv. // Find the corresponding RtpStreamRecv.
uint32_t ssrc = packet->GetSsrc(); uint32_t ssrc = packet->GetSsrc();
RTC::RtpStreamRecv* rtpStream{ nullptr }; RTC::RtpStreamRecv* rtpStream{ nullptr };
RTC::RtpEncodingParameters::Profile profile;
std::unique_ptr<RTC::RtpPacket> clonedPacket; std::unique_ptr<RTC::RtpPacket> clonedPacket;
if (this->rtpStreams.find(ssrc) != this->rtpStreams.end()) // Media RTP stream found.
if (this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end())
{ {
rtpStream = this->rtpStreams[ssrc]; rtpStream = this->mapSsrcRtpStreamInfo[ssrc].rtpStream;
auto& info = this->mapSsrcRtpStreamInfo[ssrc];
rtpStream = info.rtpStream;
profile = info.profile;
// Let's clone the RTP packet so we can mangle the payload (if needed) and other // Let's clone the RTP packet so we can mangle the payload (if needed) and other
// stuff that would change its size. // stuff that would change its size.
...@@ -235,40 +265,44 @@ namespace RTC ...@@ -235,40 +265,44 @@ namespace RTC
if (!rtpStream->ReceivePacket(packet)) if (!rtpStream->ReceivePacket(packet))
return; return;
} }
else if (this->mapRtxStreams.find(ssrc) != this->mapRtxStreams.end()) // Otherwise look for RTX SSRCs.
else
{ {
rtpStream = this->mapRtxStreams[ssrc]; for (auto& kv : this->mapSsrcRtpStreamInfo)
{
// Let's clone the RTP packet so we can mangle the payload (if needed) and other auto& info = kv.second;
// stuff that would change its size.
clonedPacket.reset(packet->Clone(ClonedPacketBuffer));
packet = clonedPacket.get();
// Process the packet. if (info.rtxSsrc != 0u && info.rtxSsrc == ssrc)
if (!rtpStream->ReceiveRtxPacket(packet)) {
return; rtpStream = info.rtpStream;
profile = info.profile;
// Packet repaired after applying RTX. // Let's clone the RTP packet so we can mangle the payload (if needed) and
rtpStream->packetsRepaired++; // other stuff that would change its size.
} clonedPacket.reset(packet->Clone(ClonedPacketBuffer));
else packet = clonedPacket.get();
{
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
return; // Process the packet.
} if (!rtpStream->ReceiveRtxPacket(packet))
return;
RTC::RtpEncodingParameters::Profile profile; // Packet repaired after applying RTX.
rtpStream->packetsRepaired++;
try break;
{ }
profile = GetProfile(rtpStream, packet); }
} }
catch (const MediaSoupError& error)
// Not found.
if (!rtpStream)
{ {
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
return; return;
} }
// TODO: Remove this, it's CPU consuming and provides nothing.
if (packet->IsKeyFrame()) if (packet->IsKeyFrame())
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
...@@ -298,10 +332,11 @@ namespace RTC ...@@ -298,10 +332,11 @@ namespace RTC
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval) if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return; return;
for (auto& kv : this->rtpStreams) for (auto& kv : this->mapSsrcRtpStreamInfo)
{ {
auto rtpStream = kv.second; auto& info = kv.second;
auto* report = rtpStream->GetRtcpReceiverReport(); auto* rtpStream = info.rtpStream;
auto* report = rtpStream->GetRtcpReceiverReport();
report->SetSsrc(rtpStream->GetSsrc()); report->SetSsrc(rtpStream->GetSsrc());
packet->AddReceiverReport(report); packet->AddReceiverReport(report);
...@@ -367,9 +402,10 @@ namespace RTC ...@@ -367,9 +402,10 @@ namespace RTC
// Run the timer. // Run the timer.
this->keyFrameRequestBlockTimer->Start(KeyFrameRequestBlockTimeout); this->keyFrameRequestBlockTimer->Start(KeyFrameRequestBlockTimeout);
for (auto& kv : this->rtpStreams) for (auto& kv : this->mapSsrcRtpStreamInfo)
{ {
auto rtpStream = kv.second; auto& info = kv.second;
auto* rtpStream = info.rtpStream;
rtpStream->RequestKeyFrame(); rtpStream->RequestKeyFrame();
} }
...@@ -443,47 +479,67 @@ namespace RTC ...@@ -443,47 +479,67 @@ namespace RTC
uint32_t ssrc = packet->GetSsrc(); uint32_t ssrc = packet->GetSsrc();
// If already exists, do nothing. // If this is an already known media SSRC, we are done.
if ( if (this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end())
this->rtpStreams.find(ssrc) != this->rtpStreams.end() ||
this->mapRtxStreams.find(ssrc) != this->mapRtxStreams.end())
{
return; return;
}
// First look for encodings with ssrc field. // Otherwise, first look for encodings with ssrc field.
for (auto& encoding : this->rtpParameters.encodings)
{ {
for (auto& encoding : this->rtpParameters.encodings) if (encoding.ssrc == ssrc)
{ {
if (encoding.ssrc == ssrc) CreateRtpStream(encoding, ssrc);
{
CreateRtpStream(encoding, ssrc);
return; return;
}
} }
} }
// If not found, look for encodings with encodingId (RID) field. // If not found, look for an encoding with encodingId (RID) field.
const uint8_t* ridPtr;
size_t ridLen;
if (!packet->ReadRid(&ridPtr, &ridLen))
return;
auto* charRidPtr = reinterpret_cast<const char*>(ridPtr);
std::string rid(charRidPtr, ridLen);
for (auto& encoding : this->rtpParameters.encodings)
{ {
const uint8_t* ridPtr; if (encoding.encodingId != rid)
size_t ridLen; continue;
// Ignore if RTX.
auto& rtxCodec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
if (packet->ReadRid(&ridPtr, &ridLen)) if (rtxCodec.payloadType != 0u && packet->GetPayloadType() == rtxCodec.payloadType)
{ {
auto* charRidPtr = const_cast<char*>(reinterpret_cast<const char*>(ridPtr)); // TODO: Here we should fill rtxSsrc in the corresponding RtpStreamInfo with
std::string rid(charRidPtr, ridLen); // same profile (if it exists in the map).
//
// https://github.com/versatica/mediasoup/issues/237
return;
}
for (auto& encoding : this->rtpParameters.encodings) // If the matching encoding.profile is already present in the map, it means
// that the sender is using a new SSRC for the same profile, so ignore it.
for (auto& kv : this->mapSsrcRtpStreamInfo)
{
auto& info = kv.second;
auto profile = info.profile;
if (profile == encoding.profile)
{ {
if (encoding.encodingId == rid) MS_WARN_TAG(rtp, "ignoring media packet with RID already handled [ssrc:%" PRIu32 "]", ssrc);
{
CreateRtpStream(encoding, ssrc);
return; return;
}
} }
} }
CreateRtpStream(encoding, ssrc);
return;
} }
} }
...@@ -491,7 +547,9 @@ namespace RTC ...@@ -491,7 +547,9 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
MS_ASSERT(this->rtpStreams.find(ssrc) == this->rtpStreams.end(), "stream already exists"); MS_ASSERT(
this->mapSsrcRtpStreamInfo.find(ssrc) == this->mapSsrcRtpStreamInfo.end(),
"stream already exists");
// Get the codec of the stream/encoding. // Get the codec of the stream/encoding.
auto& codec = this->rtpParameters.GetCodecForEncoding(encoding); auto& codec = this->rtpParameters.GetCodecForEncoding(encoding);
...@@ -534,80 +592,42 @@ namespace RTC ...@@ -534,80 +592,42 @@ namespace RTC
// Create a RtpStreamRecv for receiving a media stream. // Create a RtpStreamRecv for receiving a media stream.
auto* rtpStream = new RTC::RtpStreamRecv(this, params); auto* rtpStream = new RTC::RtpStreamRecv(this, params);
this->rtpStreams[ssrc] = rtpStream; // Create a RtpStreamInfo struct.
struct RtpStreamInfo info;
// Check RTX capabilities.
info.rtpStream = rtpStream;
info.rid = encoding.encodingId;
info.profile = encoding.profile;
info.rtxSsrc = 0;
info.active = false;
// Insert into the map.
//
// TODO: Could this be optimized by using some exotic C++ "struct move instead
// of copy"?
this->mapSsrcRtpStreamInfo[ssrc] = info;
// Check RTX capabilities and fill them.
//
// TODO: Not valid for RID+RTX:
// https://github.com/versatica/mediasoup/issues/237
if (encoding.hasRtx && encoding.rtx.ssrc != 0u) if (encoding.hasRtx && encoding.rtx.ssrc != 0u)
{ {
if (this->mapRtxStreams.find(encoding.rtx.ssrc) != this->mapRtxStreams.end()) auto& rtxCodec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
return;
auto& codec = this->rtpParameters.GetRtxCodecForEncoding(encoding); rtpStream->SetRtx(rtxCodec.payloadType, encoding.rtx.ssrc);
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc); // Set the info rtxSsrc field.
this->mapRtxStreams[encoding.rtx.ssrc] = rtpStream; this->mapSsrcRtpStreamInfo[ssrc].rtxSsrc = encoding.rtx.ssrc;
} }
// TODO: For SVC, the dependencyEncodingIds must be checked and their respective profiles must // Activate the stream.
// be also added into mapRtpStreamProfiles. ActivateStream(rtpStream);
auto profile = encoding.profile;
// Add the stream to the profiles map.
this->mapRtpStreamProfiles[rtpStream].insert(profile);
// Add new profile/s into active profiles map and notify the listener.
ActivateStreamProfiles(rtpStream);
// Request a key frame since we may have lost the first packets of this stream. // Request a key frame since we may have lost the first packets of this stream.
RequestKeyFrame(true); RequestKeyFrame(true);
} }
void Producer::ClearRtpStream(RTC::RtpStreamRecv* rtpStream)
{
MS_TRACE();
// Remove the profiles related to this stream from active profiles map and notify the listener.
DeactivateStreamProfiles(rtpStream);
for (auto& kv : this->mapRtxStreams)
{
auto rtxSsrc = kv.first;
auto* mediaStream = kv.second;
if (mediaStream == rtpStream)
{
this->mapRtxStreams.erase(rtxSsrc);
break;
}
}
this->mapRtpStreamProfiles.erase(rtpStream);
delete rtpStream;
}
void Producer::ClearRtpStreams()
{
MS_TRACE();
auto it = this->rtpStreams.begin();
while (it != this->rtpStreams.end())
{
auto rtpStream = it->second;
ClearRtpStream(rtpStream);
it = this->rtpStreams.erase(it);
}
this->rtpStreams.clear();
this->mapRtxStreams.clear();
this->mapRtpStreamProfiles.clear();
this->mapActiveProfiles.clear();
}
void Producer::ApplyRtpMapping(RTC::RtpPacket* packet) const void Producer::ApplyRtpMapping(RTC::RtpPacket* packet) const
{ {
MS_TRACE(); MS_TRACE();
...@@ -643,73 +663,56 @@ namespace RTC ...@@ -643,73 +663,56 @@ namespace RTC
} }
} }
RTC::RtpEncodingParameters::Profile Producer::GetProfile( void Producer::ActivateStream(RTC::RtpStreamRecv* rtpStream)
RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet)
{ {
// The stream is already mapped to a profile. auto ssrc = rtpStream->GetSsrc();
if (this->mapRtpStreamProfiles.find(rtpStream) != this->mapRtpStreamProfiles.end())
{
auto it = this->mapRtpStreamProfiles[rtpStream].begin();
auto profile = *it;
return profile; MS_ASSERT(
} this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end(),
"stream not present in the map");
MS_THROW_ERROR("unknown RTP packet received [ssrc:%" PRIu32 "]", packet->GetSsrc()); auto& info = this->mapSsrcRtpStreamInfo[ssrc];
} auto profile = info.profile;
void Producer::ActivateStreamProfiles(RTC::RtpStreamRecv* rtpStream) MS_ASSERT(info.profile != RTC::RtpEncodingParameters::Profile::NONE, "stream has no profile");
{ MS_ASSERT(info.active == false, "stream already active");
auto& profiles = this->mapRtpStreamProfiles[rtpStream];
// Notify about the profiles being enabled. info.active = true;
for (auto& profile : profiles)
{
MS_ASSERT(
this->mapActiveProfiles.find(profile) == this->mapActiveProfiles.end(),
"profile already in active profiles map");
// Add the profile to the active profiles map. // Update the active profiles map.
this->mapActiveProfiles[profile] = rtpStream; this->mapActiveProfiles[profile] = rtpStream;
for (auto& listener : this->listeners) // Notify about the profile being enabled.
{ for (auto& listener : this->listeners)
listener->OnProducerProfileEnabled(this, profile, rtpStream); {
} listener->OnProducerProfileEnabled(this, profile, rtpStream);
} }
} }
void Producer::DeactivateStreamProfiles(RTC::RtpStreamRecv* rtpStream) void Producer::DeactivateStream(RTC::RtpStreamRecv* rtpStream)
{ {
auto& profiles = this->mapRtpStreamProfiles[rtpStream]; auto ssrc = rtpStream->GetSsrc();
// Notify about the active profiles being disabled. MS_ASSERT(
for (auto& profile : profiles) this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end(),
{ "stream not present in the map");
if (this->mapActiveProfiles.find(profile) != this->mapActiveProfiles.end())
{
// Remove the profile from the active profiles map.
this->mapActiveProfiles.erase(profile);
for (auto& listener : this->listeners) auto& info = this->mapSsrcRtpStreamInfo[ssrc];
{ auto profile = info.profile;
listener->OnProducerProfileDisabled(this, profile);
}
}
}
}
bool Producer::IsStreamActive(const RTC::RtpStream* rtpStream) const MS_ASSERT(info.profile != RTC::RtpEncodingParameters::Profile::NONE, "stream has no profile");
{ MS_ASSERT(info.active == true, "stream already inactive");
for (auto it : this->mapActiveProfiles)
{
auto activeRtpStream = it.second;
if (activeRtpStream == rtpStream) info.active = false;
return true;
}
return false; // Update the active profiles map.
this->mapActiveProfiles.erase(profile);
// Notify about the profile being disabled.
for (auto& listener : this->listeners)
{
listener->OnProducerProfileDisabled(this, profile);
}
} }
void Producer::OnRtpStreamRecvNackRequired( void Producer::OnRtpStreamRecvNackRequired(
...@@ -744,7 +747,7 @@ namespace RTC ...@@ -744,7 +747,7 @@ namespace RTC
} }
} }
auto nackItem = new RTC::RTCP::FeedbackRtpNackItem(seq, bitmask); auto* nackItem = new RTC::RTCP::FeedbackRtpNackItem(seq, bitmask);
packet.AddItem(nackItem); packet.AddItem(nackItem);
} }
...@@ -773,8 +776,6 @@ namespace RTC ...@@ -773,8 +776,6 @@ namespace RTC
packet.Serialize(RTC::RTCP::Buffer); packet.Serialize(RTC::RTCP::Buffer);
// Send two, because it's free.
this->transport->SendRtcpPacket(&packet);
this->transport->SendRtcpPacket(&packet); this->transport->SendRtcpPacket(&packet);
rtpStream->pliCount++; rtpStream->pliCount++;
...@@ -784,52 +785,59 @@ namespace RTC ...@@ -784,52 +785,59 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
auto rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream); size_t numActiveStreams = 0;
uint32_t totalBitrate = 0;
MS_ASSERT( uint64_t now = DepLibUV::GetTime();
this->mapRtpStreamProfiles.find(rtpStreamRecv) != this->mapRtpStreamProfiles.end(),
"stream not present in mapRtpStreamProfiles");
// Single active profile present. Ignore.
if (this->mapActiveProfiles.size() == 1)
return;
// Simulcast. Check whether any RTP is being received at all. for (auto& kv : this->mapSsrcRtpStreamInfo)
uint32_t totalBitrate = 0;
uint64_t now = DepLibUV::GetTime();
for (auto it : this->mapActiveProfiles)
{ {
auto activeRtpStream = it.second; auto& info = kv.second;
auto ssrc = activeRtpStream->GetSsrc(); auto* otherRtpStream = info.rtpStream;
totalBitrate += this->rtpStreams[ssrc]->GetRate(now); if (info.active)
{
numActiveStreams++;
totalBitrate += otherRtpStream->GetRate(now);
}
} }
// No RTP is being received at all. Ignore. // If there is a single active stream, do nothing.
if (numActiveStreams <= 1)
return;
// Simulcast. No RTP is being received at all. Ignore.
if (totalBitrate == 0) if (totalBitrate == 0)
return; return;
// Deactivate stream profiles if active. auto ssrc = rtpStream->GetSsrc();
if (IsStreamActive(rtpStream)) auto active = this->mapSsrcRtpStreamInfo[ssrc].active;
DeactivateStreamProfiles(rtpStreamRecv);
// Deactivate stream if active.
if (active)
{
MS_DEBUG_TAG(rtp, "stream is now inactive [ssrc:%" PRIu32 "]", ssrc);
auto* rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream);
DeactivateStream(rtpStreamRecv);
}
} }
void Producer::OnRtpStreamActive(RTC::RtpStream* rtpStream) void Producer::OnRtpStreamActive(RTC::RtpStream* rtpStream)
{ {
MS_TRACE(); MS_TRACE();
auto rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream); auto ssrc = rtpStream->GetSsrc();
auto active = this->mapSsrcRtpStreamInfo[ssrc].active;
MS_ASSERT(
this->mapRtpStreamProfiles.find(rtpStreamRecv) != this->mapRtpStreamProfiles.end(),
"stream not present in mapRtpStreamProfiles");
if (!IsStreamActive(rtpStream)) // Activate stream if inactive.
if (!active)
{ {
ActivateStreamProfiles(rtpStreamRecv); MS_DEBUG_TAG(rtp, "stream is now active [ssrc:%" PRIu32 "]", ssrc);
auto* rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream);
MS_DEBUG_TAG(rtp, "stream is now active [ssrc:%" PRIu32 "]", rtpStream->GetSsrc()); ActivateStream(rtpStreamRecv);
} }
} }
......
...@@ -1052,52 +1052,6 @@ namespace RTC ...@@ -1052,52 +1052,6 @@ namespace RTC
break; break;
} }
case Channel::Request::MethodId::PRODUCER_UPDATE_RTP_PARAMETERS:
{
static const Json::StaticString JsonStringRtpParameters{ "rtpParameters" };
RTC::Producer* producer;
try
{
producer = GetProducerFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
if (!request->data[JsonStringRtpParameters].isObject())
{
request->Reject("missing data.rtpParameters");
return;
}
RTC::RtpParameters rtpParameters;
try
{
// NOTE: This may throw.
rtpParameters = RTC::RtpParameters(request->data[JsonStringRtpParameters]);
// NOTE: This may throw.
producer->UpdateRtpParameters(rtpParameters);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
request->Accept();
break;
}
case Channel::Request::MethodId::PRODUCER_PAUSE: case Channel::Request::MethodId::PRODUCER_PAUSE:
{ {
RTC::Producer* producer; RTC::Producer* producer;
...@@ -1692,20 +1646,6 @@ namespace RTC ...@@ -1692,20 +1646,6 @@ namespace RTC
this->mapProducerAudioLevelContainer.erase(producer); this->mapProducerAudioLevelContainer.erase(producer);
} }
void Router::OnProducerRtpParametersUpdated(RTC::Producer* producer)
{
MS_ASSERT(
this->mapProducerConsumers.find(producer) != this->mapProducerConsumers.end(),
"Producer not present in mapProducerConsumers");
auto& consumers = this->mapProducerConsumers[producer];
for (auto* consumer : consumers)
{
consumer->SourceRtpParametersUpdated();
}
}
void Router::OnProducerPaused(RTC::Producer* producer) void Router::OnProducerPaused(RTC::Producer* producer)
{ {
MS_TRACE(); MS_TRACE();
......
...@@ -524,15 +524,6 @@ namespace RTC ...@@ -524,15 +524,6 @@ namespace RTC
this->rtpListener.RemoveProducer(producer); this->rtpListener.RemoveProducer(producer);
} }
void Transport::OnProducerRtpParametersUpdated(RTC::Producer* producer)
{
MS_TRACE();
// Update our RtpListener.
// NOTE: This may throw.
this->rtpListener.AddProducer(producer);
}
void Transport::OnProducerPaused(RTC::Producer* /*producer*/) void Transport::OnProducerPaused(RTC::Producer* /*producer*/)
{ {
// Do nothing. // Do nothing.
......
...@@ -255,7 +255,6 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R ...@@ -255,7 +255,6 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::PRODUCER_CLOSE: case Channel::Request::MethodId::PRODUCER_CLOSE:
case Channel::Request::MethodId::PRODUCER_DUMP: case Channel::Request::MethodId::PRODUCER_DUMP:
case Channel::Request::MethodId::PRODUCER_GET_STATS: case Channel::Request::MethodId::PRODUCER_GET_STATS:
case Channel::Request::MethodId::PRODUCER_UPDATE_RTP_PARAMETERS:
case Channel::Request::MethodId::PRODUCER_PAUSE: case Channel::Request::MethodId::PRODUCER_PAUSE:
case Channel::Request::MethodId::PRODUCER_RESUME: case Channel::Request::MethodId::PRODUCER_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE: case Channel::Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论