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
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':
{
const { id, appData } = notification;
......
......@@ -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.
*
......
......@@ -39,7 +39,6 @@ namespace Channel
PRODUCER_CLOSE,
PRODUCER_DUMP,
PRODUCER_GET_STATS,
PRODUCER_UPDATE_RTP_PARAMETERS,
PRODUCER_PAUSE,
PRODUCER_RESUME,
PRODUCER_SET_PREFERRED_PROFILE,
......
......@@ -51,7 +51,6 @@ namespace RTC
void SourceResume();
void AddProfile(const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream);
void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile);
void SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetEncodingPreferences(const RTC::Codecs::EncodingContext::Preferences preferences);
......
......@@ -14,7 +14,6 @@
#include "handles/Timer.hpp"
#include <json/json.h>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
......@@ -39,6 +38,16 @@ namespace RTC
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:
Producer(
Channel::Notifier* notifier,
......@@ -59,7 +68,6 @@ namespace RTC
Json::Value GetStats() const;
void AddListener(RTC::ProducerListener* listener);
void RemoveListener(RTC::ProducerListener* listener);
void UpdateRtpParameters(RTC::RtpParameters& rtpParameters);
void Pause();
void Resume();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
......@@ -79,12 +87,10 @@ namespace RTC
void FillHeaderExtensionIds();
void MayNeedNewStream(RTC::RtpPacket* packet);
void CreateRtpStream(RTC::RtpEncodingParameters& encoding, uint32_t ssrc);
void ClearRtpStream(RTC::RtpStreamRecv* rtpStream);
void ClearRtpStreams();
void ApplyRtpMapping(RTC::RtpPacket* packet) const;
RTC::RtpEncodingParameters::Profile GetProfile(RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet);
void ActivateStreamProfiles(RTC::RtpStreamRecv* rtpStream);
void DeactivateStreamProfiles(RTC::RtpStreamRecv* rtpStream);
void ActivateStream(RTC::RtpStreamRecv* rtpStream);
void DeactivateStream(RTC::RtpStreamRecv* rtpStream);
// TODO: Remove?
bool IsStreamActive(const RTC::RtpStream* rtpStream) const;
/* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */
......@@ -112,9 +118,7 @@ namespace RTC
struct RtpMapping rtpMapping;
std::unordered_set<RTC::ProducerListener*> listeners;
// Allocated by this.
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
std::map<RTC::RtpStreamRecv*, std::set<RTC::RtpEncodingParameters::Profile>> mapRtpStreamProfiles;
std::map<uint32_t, RtpStreamInfo> mapSsrcRtpStreamInfo;
std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStream*> mapActiveProfiles;
Timer* keyFrameRequestBlockTimer{ nullptr };
// Others.
......@@ -169,11 +173,11 @@ namespace RTC
inline void Producer::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
{
auto it = this->rtpStreams.find(report->GetSsrc());
if (it != this->rtpStreams.end())
auto it = this->mapSsrcRtpStreamInfo.find(report->GetSsrc());
if (it != this->mapSsrcRtpStreamInfo.end())
{
auto rtpStream = it->second;
auto& info = it->second;
auto* rtpStream = info.rtpStream;
rtpStream->ReceiveRtcpSenderReport(report);
}
......
......@@ -13,10 +13,9 @@ namespace RTC
class ProducerListener
{
public:
virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpParametersUpdated(RTC::Producer* producer) = 0;
virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0;
virtual void OnProducerClosed(RTC::Producer* producer) = 0;
virtual void OnProducerPaused(RTC::Producer* producer) = 0;
virtual void OnProducerResumed(RTC::Producer* producer) = 0;
virtual void OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0;
virtual void OnProducerProfileEnabled(
......
......@@ -64,7 +64,6 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket(
......
......@@ -97,7 +97,6 @@ namespace RTC
/* Pure virtual methods inherited from RTC::ProducerListener. */
public:
void OnProducerClosed(RTC::Producer* producer) override;
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket(
......
......@@ -34,7 +34,6 @@ namespace Channel
{ "producer.close", Request::MethodId::PRODUCER_CLOSE },
{ "producer.dump", Request::MethodId::PRODUCER_DUMP },
{ "producer.getStats", Request::MethodId::PRODUCER_GET_STATS },
{ "producer.updateRtpParameters", Request::MethodId::PRODUCER_UPDATE_RTP_PARAMETERS },
{ "producer.pause", Request::MethodId::PRODUCER_PAUSE },
{ "producer.resume" , Request::MethodId::PRODUCER_RESUME },
{ "producer.setPreferredProfile", Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE },
......
......@@ -256,16 +256,6 @@ namespace RTC
}
}
void Consumer::SourceRtpParametersUpdated()
{
MS_TRACE();
if (!IsEnabled())
return;
this->syncRequired = true;
}
void Consumer::AddProfile(
const RTC::RtpEncodingParameters::Profile profile, const RTC::RtpStream* rtpStream)
{
......
......@@ -49,9 +49,14 @@ namespace RTC
{
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()
......@@ -76,17 +81,26 @@ namespace RTC
static const Json::StaticString JsonStringProducerId{ "producerId" };
static const Json::StaticString JsonStringKind{ "kind" };
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 JsonStringSsrcAudioLevel{ "ssrcAudioLevel" };
static const Json::StaticString JsonStringAbsSendTime{ "absSendTime" };
static const Json::StaticString JsonStringRid{ "rid" };
static const Json::StaticString JsonStringPaused{ "paused" };
static const Json::StaticString JsonStringLossPercentage{ "lossPercentage" };
Json::Value json(Json::objectValue);
Json::Value jsonHeaderExtensionIds(Json::objectValue);
Json::Value jsonRtpStreams(Json::arrayValue);
Json::Value jsonRtpStreamInfos(Json::arrayValue);
json[JsonStringProducerId] = Json::UInt{ this->producerId };
......@@ -96,18 +110,47 @@ namespace RTC
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());
lossPercentage += rtpStream->GetLossPercentage();
jsonRtpStreamInfo[JsonStringRtpStream] = info.rtpStream->ToJson();
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())
lossPercentage = lossPercentage / this->rtpStreams.size();
if (!this->mapSsrcRtpStreamInfo.empty())
lossPercentage = lossPercentage / this->mapSsrcRtpStreamInfo.size();
json[JsonStringLossPercentage] = lossPercentage;
......@@ -135,15 +178,14 @@ namespace RTC
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();
if (this->transport != nullptr)
{
jsonRtpStream[JsonStringTransportId] = this->transport->transportId;
}
json.append(jsonRtpStream);
}
......@@ -151,24 +193,6 @@ namespace RTC
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()
{
MS_TRACE();
......@@ -220,11 +244,17 @@ namespace RTC
// Find the corresponding RtpStreamRecv.
uint32_t ssrc = packet->GetSsrc();
RTC::RtpStreamRecv* rtpStream{ nullptr };
RTC::RtpEncodingParameters::Profile profile;
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
// stuff that would change its size.
......@@ -235,40 +265,44 @@ namespace RTC
if (!rtpStream->ReceivePacket(packet))
return;
}
else if (this->mapRtxStreams.find(ssrc) != this->mapRtxStreams.end())
// Otherwise look for RTX SSRCs.
else
{
rtpStream = this->mapRtxStreams[ssrc];
// Let's clone the RTP packet so we can mangle the payload (if needed) and other
// stuff that would change its size.
clonedPacket.reset(packet->Clone(ClonedPacketBuffer));
packet = clonedPacket.get();
for (auto& kv : this->mapSsrcRtpStreamInfo)
{
auto& info = kv.second;
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
return;
if (info.rtxSsrc != 0u && info.rtxSsrc == ssrc)
{
rtpStream = info.rtpStream;
profile = info.profile;
// Packet repaired after applying RTX.
rtpStream->packetsRepaired++;
}
else
{
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
// Let's clone the RTP packet so we can mangle the payload (if needed) and
// other stuff that would change its size.
clonedPacket.reset(packet->Clone(ClonedPacketBuffer));
packet = clonedPacket.get();
return;
}
// Process the packet.
if (!rtpStream->ReceiveRtxPacket(packet))
return;
RTC::RtpEncodingParameters::Profile profile;
// Packet repaired after applying RTX.
rtpStream->packetsRepaired++;
try
{
profile = GetProfile(rtpStream, packet);
break;
}
}
}
catch (const MediaSoupError& error)
// Not found.
if (!rtpStream)
{
MS_WARN_TAG(rtp, "no RtpStream found for given RTP packet [ssrc:%" PRIu32 "]", ssrc);
return;
}
// TODO: Remove this, it's CPU consuming and provides nothing.
if (packet->IsKeyFrame())
{
MS_DEBUG_TAG(
......@@ -298,10 +332,11 @@ namespace RTC
if (static_cast<float>((now - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return;
for (auto& kv : this->rtpStreams)
for (auto& kv : this->mapSsrcRtpStreamInfo)
{
auto rtpStream = kv.second;
auto* report = rtpStream->GetRtcpReceiverReport();
auto& info = kv.second;
auto* rtpStream = info.rtpStream;
auto* report = rtpStream->GetRtcpReceiverReport();
report->SetSsrc(rtpStream->GetSsrc());
packet->AddReceiverReport(report);
......@@ -367,9 +402,10 @@ namespace RTC
// Run the timer.
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();
}
......@@ -443,47 +479,67 @@ namespace RTC
uint32_t ssrc = packet->GetSsrc();
// If already exists, do nothing.
if (
this->rtpStreams.find(ssrc) != this->rtpStreams.end() ||
this->mapRtxStreams.find(ssrc) != this->mapRtxStreams.end())
{
// If this is an already known media SSRC, we are done.
if (this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end())
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;
size_t ridLen;
if (encoding.encodingId != rid)
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));
std::string rid(charRidPtr, ridLen);
// TODO: Here we should fill rtxSsrc in the corresponding RtpStreamInfo with
// 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)
{
CreateRtpStream(encoding, ssrc);
MS_WARN_TAG(rtp, "ignoring media packet with RID already handled [ssrc:%" PRIu32 "]", ssrc);
return;
}
return;
}
}
CreateRtpStream(encoding, ssrc);
return;
}
}
......@@ -491,7 +547,9 @@ namespace RTC
{
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.
auto& codec = this->rtpParameters.GetCodecForEncoding(encoding);
......@@ -534,80 +592,42 @@ namespace RTC
// Create a RtpStreamRecv for receiving a media stream.
auto* rtpStream = new RTC::RtpStreamRecv(this, params);
this->rtpStreams[ssrc] = rtpStream;
// Check RTX capabilities.
// Create a RtpStreamInfo struct.
struct RtpStreamInfo info;
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 (this->mapRtxStreams.find(encoding.rtx.ssrc) != this->mapRtxStreams.end())
return;
auto& rtxCodec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
auto& codec = this->rtpParameters.GetRtxCodecForEncoding(encoding);
auto rtpStream = this->rtpStreams[ssrc];
rtpStream->SetRtx(rtxCodec.payloadType, encoding.rtx.ssrc);
rtpStream->SetRtx(codec.payloadType, encoding.rtx.ssrc);
this->mapRtxStreams[encoding.rtx.ssrc] = rtpStream;
// Set the info rtxSsrc field.
this->mapSsrcRtpStreamInfo[ssrc].rtxSsrc = encoding.rtx.ssrc;
}
// TODO: For SVC, the dependencyEncodingIds must be checked and their respective profiles must
// be also added into mapRtpStreamProfiles.
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);
// Activate the stream.
ActivateStream(rtpStream);
// Request a key frame since we may have lost the first packets of this stream.
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
{
MS_TRACE();
......@@ -643,73 +663,56 @@ namespace RTC
}
}
RTC::RtpEncodingParameters::Profile Producer::GetProfile(
RTC::RtpStreamRecv* rtpStream, RTC::RtpPacket* packet)
void Producer::ActivateStream(RTC::RtpStreamRecv* rtpStream)
{
// The stream is already mapped to a profile.
if (this->mapRtpStreamProfiles.find(rtpStream) != this->mapRtpStreamProfiles.end())
{
auto it = this->mapRtpStreamProfiles[rtpStream].begin();
auto profile = *it;
auto ssrc = rtpStream->GetSsrc();
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)
{
auto& profiles = this->mapRtpStreamProfiles[rtpStream];
MS_ASSERT(info.profile != RTC::RtpEncodingParameters::Profile::NONE, "stream has no profile");
MS_ASSERT(info.active == false, "stream already active");
// Notify about the profiles being enabled.
for (auto& profile : profiles)
{
MS_ASSERT(
this->mapActiveProfiles.find(profile) == this->mapActiveProfiles.end(),
"profile already in active profiles map");
info.active = true;
// Add the profile to the active profiles map.
this->mapActiveProfiles[profile] = rtpStream;
// Update the active profiles map.
this->mapActiveProfiles[profile] = rtpStream;
for (auto& listener : this->listeners)
{
listener->OnProducerProfileEnabled(this, profile, rtpStream);
}
// Notify about the profile being enabled.
for (auto& listener : this->listeners)
{
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.
for (auto& profile : profiles)
{
if (this->mapActiveProfiles.find(profile) != this->mapActiveProfiles.end())
{
// Remove the profile from the active profiles map.
this->mapActiveProfiles.erase(profile);
MS_ASSERT(
this->mapSsrcRtpStreamInfo.find(ssrc) != this->mapSsrcRtpStreamInfo.end(),
"stream not present in the map");
for (auto& listener : this->listeners)
{
listener->OnProducerProfileDisabled(this, profile);
}
}
}
}
auto& info = this->mapSsrcRtpStreamInfo[ssrc];
auto profile = info.profile;
bool Producer::IsStreamActive(const RTC::RtpStream* rtpStream) const
{
for (auto it : this->mapActiveProfiles)
{
auto activeRtpStream = it.second;
MS_ASSERT(info.profile != RTC::RtpEncodingParameters::Profile::NONE, "stream has no profile");
MS_ASSERT(info.active == true, "stream already inactive");
if (activeRtpStream == rtpStream)
return true;
}
info.active = false;
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(
......@@ -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);
}
......@@ -773,8 +776,6 @@ namespace RTC
packet.Serialize(RTC::RTCP::Buffer);
// Send two, because it's free.
this->transport->SendRtcpPacket(&packet);
this->transport->SendRtcpPacket(&packet);
rtpStream->pliCount++;
......@@ -784,52 +785,59 @@ namespace RTC
{
MS_TRACE();
auto rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream);
MS_ASSERT(
this->mapRtpStreamProfiles.find(rtpStreamRecv) != this->mapRtpStreamProfiles.end(),
"stream not present in mapRtpStreamProfiles");
// Single active profile present. Ignore.
if (this->mapActiveProfiles.size() == 1)
return;
size_t numActiveStreams = 0;
uint32_t totalBitrate = 0;
uint64_t now = DepLibUV::GetTime();
// Simulcast. Check whether any RTP is being received at all.
uint32_t totalBitrate = 0;
uint64_t now = DepLibUV::GetTime();
for (auto it : this->mapActiveProfiles)
for (auto& kv : this->mapSsrcRtpStreamInfo)
{
auto activeRtpStream = it.second;
auto ssrc = activeRtpStream->GetSsrc();
auto& info = kv.second;
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)
return;
// Deactivate stream profiles if active.
if (IsStreamActive(rtpStream))
DeactivateStreamProfiles(rtpStreamRecv);
auto ssrc = rtpStream->GetSsrc();
auto active = this->mapSsrcRtpStreamInfo[ssrc].active;
// 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)
{
MS_TRACE();
auto rtpStreamRecv = dynamic_cast<RtpStreamRecv*>(rtpStream);
MS_ASSERT(
this->mapRtpStreamProfiles.find(rtpStreamRecv) != this->mapRtpStreamProfiles.end(),
"stream not present in mapRtpStreamProfiles");
auto ssrc = rtpStream->GetSsrc();
auto active = this->mapSsrcRtpStreamInfo[ssrc].active;
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
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:
{
RTC::Producer* producer;
......@@ -1692,20 +1646,6 @@ namespace RTC
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)
{
MS_TRACE();
......
......@@ -524,15 +524,6 @@ namespace RTC
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*/)
{
// Do nothing.
......
......@@ -255,7 +255,6 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::PRODUCER_CLOSE:
case Channel::Request::MethodId::PRODUCER_DUMP:
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_RESUME:
case Channel::Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论