提交 3b17e1be authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Add producer.setPreferredProfile():

- It sets the preferred RTP profile for all its associated Consumers.
- If consumer.setPreferredProfile() is called, the computed preferredProfile is the most restrictive.
上级 65929f6e
......@@ -54,7 +54,7 @@ class Consumer extends EnhancedEventEmitter
// Preferred profile.
// @type {String}
this._preferredProfile = null;
this._preferredProfile = 'default';
// Effective profile.
// @type {String}
......
......@@ -4,6 +4,8 @@ const Logger = require('./Logger');
const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const errors = require('./errors');
const PROFILES = new Set([ 'default', 'low', 'medium', 'high' ]);
const logger = new Logger('Producer');
class Producer extends EnhancedEventEmitter
......@@ -45,6 +47,10 @@ class Producer extends EnhancedEventEmitter
// @type {Boolean}
this._remotelyPaused = Boolean(options.remotelyPaused);
// Preferred profile.
// @type {String}
this._preferredProfile = 'default';
this._handleTransportEvents();
this._handleWorkerNotifications();
......@@ -125,6 +131,16 @@ class Producer extends EnhancedEventEmitter
return this._locallyPaused || this._remotelyPaused;
}
/**
* The preferred profile.
*
* @type {String}
*/
get preferredProfile()
{
return this._preferredProfile;
}
/**
* Close the Producer.
*
......@@ -395,6 +411,49 @@ class Producer extends EnhancedEventEmitter
this.safeEmit('resume', 'remote', appData);
}
/**
* Sets the preferred RTP profile.
*
* @param {String} profile
*/
setPreferredProfile(profile)
{
logger.debug('setPreferredProfile() [profile:%s]', profile);
if (this._closed)
{
logger.error('setPreferredProfile() | Producer closed');
return;
}
else if (profile === this._preferredProfile)
{
return;
}
else if (!PROFILES.has(profile))
{
logger.error('setPreferredProfile() | invalid profile "%s"', profile);
return;
}
this._channel.request('producer.setPreferredProfile', this._internal,
{
profile
})
.then(() =>
{
logger.debug('"producer.setPreferredProfile" request succeeded');
this._preferredProfile = profile;
})
.catch((error) =>
{
logger.error(
'"producer.setPreferredProfile" request failed: %s', String(error));
});
}
/**
* Change receiving RTP parameters.
*
......
......@@ -39,6 +39,7 @@ namespace Channel
PRODUCER_UPDATE_RTP_PARAMETERS,
PRODUCER_PAUSE,
PRODUCER_RESUME,
PRODUCER_SET_PREFERRED_PROFILE,
CONSUMER_CLOSE,
CONSUMER_DUMP,
CONSUMER_ENABLE,
......
......@@ -47,10 +47,12 @@ namespace RTC
void RemoveProfile(const RTC::RtpEncodingParameters::Profile profile);
void SourceRtpParametersUpdated();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
void Disable();
bool IsEnabled() const;
const RTC::RtpParameters& GetParameters() const;
bool IsPaused() const;
RTC::RtpEncodingParameters::Profile GetPreferredProfile() const;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket);
......@@ -103,11 +105,13 @@ namespace RTC
bool syncRequired{ true };
// RTP payload descriptor encoding.
std::unique_ptr<RTC::Codecs::EncodingContext> encodingContext;
// RTP profiles.
std::set<RTC::RtpEncodingParameters::Profile> profiles;
std::map<RTC::RtpEncodingParameters::Profile, const RTC::RtpStream*> mapProfileRtpStream;
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile sourcePreferredProfile{
RTC::RtpEncodingParameters::Profile::DEFAULT
};
RTC::RtpEncodingParameters::Profile targetProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile probingProfile{ RTC::RtpEncodingParameters::Profile::NONE };
......@@ -143,6 +147,20 @@ namespace RTC
return this->paused || this->sourcePaused;
}
inline RTC::RtpEncodingParameters::Profile Consumer::GetPreferredProfile() const
{
if (this->preferredProfile == this->sourcePreferredProfile)
return this->preferredProfile;
if (this->preferredProfile == RTC::RtpEncodingParameters::Profile::DEFAULT)
return this->sourcePreferredProfile;
if (this->sourcePreferredProfile == RTC::RtpEncodingParameters::Profile::DEFAULT)
return this->preferredProfile;
return std::min(this->preferredProfile, this->sourcePreferredProfile);
}
inline uint32_t Consumer::GetTransmissionRate(uint64_t now)
{
return this->rtpStream->GetRate(now) + this->retransmittedCounter.GetRate(now);
......
......@@ -60,9 +60,11 @@ namespace RTC
void UpdateRtpParameters(RTC::RtpParameters& rtpParameters);
void Pause();
void Resume();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
const RTC::RtpParameters& GetParameters() const;
const struct RTC::Transport::HeaderExtensionIds& GetTransportHeaderExtensionIds() const;
bool IsPaused() const;
RTC::RtpEncodingParameters::Profile GetPreferredProfile() const;
void ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
......@@ -122,6 +124,8 @@ namespace RTC
// Timestamp when last RTCP was sent.
uint64_t lastRtcpSentTime{ 0 };
uint16_t maxRtcpInterval{ 0 };
// RTP preferred profile.
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
};
/* Inline methods. */
......@@ -136,11 +140,21 @@ namespace RTC
this->listeners.erase(listener);
}
inline void Producer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
{
this->preferredProfile = profile;
}
inline const RTC::RtpParameters& Producer::GetParameters() const
{
return this->rtpParameters;
}
inline RTC::RtpEncodingParameters::Profile Producer::GetPreferredProfile() const
{
return this->preferredProfile;
}
inline const struct RTC::Transport::HeaderExtensionIds& Producer::GetTransportHeaderExtensionIds() const
{
return this->transportHeaderExtensionIds;
......
......@@ -34,6 +34,7 @@ namespace Channel
{ "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 },
{ "consumer.close", Request::MethodId::CONSUMER_CLOSE },
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
{ "consumer.enable", Request::MethodId::CONSUMER_ENABLE },
......
......@@ -90,7 +90,7 @@ namespace RTC
json[JsonStringSourcePaused] = this->sourcePaused;
json[JsonStringPreferredProfile] =
RTC::RtpEncodingParameters::profile2String[this->preferredProfile];
RTC::RtpEncodingParameters::profile2String[GetPreferredProfile()];
json[JsonStringEffectiveProfile] =
RTC::RtpEncodingParameters::profile2String[this->effectiveProfile];
......@@ -331,6 +331,23 @@ namespace RTC
RecalculateTargetProfile(true /*forcePreferred*/);
}
void Consumer::SetSourcePreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
{
MS_TRACE();
if (this->sourcePreferredProfile == profile)
return;
this->sourcePreferredProfile = profile;
MS_DEBUG_TAG(
rtp,
"source preferred profile set [profile:%s]",
RTC::RtpEncodingParameters::profile2String[profile].c_str());
RecalculateTargetProfile(true /*forcePreferred*/);
}
/**
* Called when the Transport assigned to this Consumer has been closed, so this
* Consumer becomes unhandled.
......@@ -902,7 +919,7 @@ namespace RTC
newTargetProfile = RtpEncodingParameters::Profile::DEFAULT;
}
// If there is no preferred profile, try the next higher profile.
else if (this->preferredProfile == RTC::RtpEncodingParameters::Profile::DEFAULT)
else if (GetPreferredProfile() == RTC::RtpEncodingParameters::Profile::DEFAULT)
{
newTargetProfile = this->effectiveProfile;
auto it = this->profiles.upper_bound(this->effectiveProfile);
......@@ -920,7 +937,7 @@ namespace RTC
{
auto profile = *it;
if (profile <= this->preferredProfile)
if (profile <= GetPreferredProfile())
{
newTargetProfile = profile;
break;
......@@ -933,7 +950,7 @@ namespace RTC
newTargetProfile = this->effectiveProfile;
auto it = this->profiles.upper_bound(this->effectiveProfile);
if (it != this->profiles.end() && *it <= this->preferredProfile)
if (it != this->profiles.end() && *it <= GetPreferredProfile())
newTargetProfile = *it;
}
......
......@@ -508,6 +508,9 @@ namespace RTC
if (producer->IsPaused())
consumer->SourcePause();
// Provide the preferred RTP profile of the Producer.
consumer->SetSourcePreferredProfile(producer->GetPreferredProfile());
// Add us as listener.
consumer->AddListener(this);
......@@ -1014,6 +1017,66 @@ namespace RTC
break;
}
case Channel::Request::MethodId::PRODUCER_SET_PREFERRED_PROFILE:
{
static const Json::StaticString JsonStringProfile{ "profile" };
RTC::Producer* producer;
try
{
producer = GetProducerFromRequest(request);
}
catch (const MediaSoupError& error)
{
request->Reject(error.what());
return;
}
if (!request->data[JsonStringProfile].isString())
{
request->Reject("missing data.profile");
return;
}
std::string profileStr = request->data[JsonStringProfile].asString();
auto it = RTC::RtpEncodingParameters::string2Profile.find(profileStr);
if (it == RTC::RtpEncodingParameters::string2Profile.end())
{
request->Reject("unknown profile");
return;
}
auto profile = it->second;
if (profile == RTC::RtpEncodingParameters::Profile::NONE)
{
request->Reject("invalid profile");
return;
}
if (profile != producer->GetPreferredProfile())
{
producer->SetPreferredProfile(profile);
auto& consumers = this->mapProducerConsumers[producer];
for (auto* consumer : consumers)
{
consumer->SetSourcePreferredProfile(profile);
}
}
request->Accept();
break;
}
case Channel::Request::MethodId::CONSUMER_CLOSE:
{
RTC::Consumer* consumer;
......
......@@ -255,6 +255,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
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:
case Channel::Request::MethodId::CONSUMER_CLOSE:
case Channel::Request::MethodId::CONSUMER_DUMP:
case Channel::Request::MethodId::CONSUMER_ENABLE:
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论