提交 f3ffb583 authored 作者: José Luis Millán's avatar José Luis Millán

Consumer: select a desired RTP profile

上级 652dff13
......@@ -450,6 +450,42 @@ class Consumer extends EnhancedEventEmitter
this.safeEmit('resume', 'remote', appData);
}
/**
* Sets the preferred RTP profile.
*
* @return {Boolean} true if paused.
*/
setPreferredProfile(profile)
{
logger.debug('setPreferredProfile()');
if (this._closed)
{
logger.error('setPreferredProfile() | Consumer closed');
return false;
}
const data =
{
id : this.id,
peerName : this.peer.name,
profile : profile
};
this._channel.request('consumer.setPreferredProfile', this._internal, { profile })
.then(() =>
{
logger.debug('"consumer.setPreferredProfile" request succeeded');
this.emit('@notify', 'preferredProfileSet', data);
})
.catch((error) =>
{
logger.error('"consumer.setPreferredProfile" request failed: %s', String(error));
});
}
_handleWorkerNotifications()
{
// Subscribe to notifications.
......
......@@ -42,7 +42,8 @@ namespace Channel
CONSUMER_DUMP,
CONSUMER_ENABLE,
CONSUMER_PAUSE,
CONSUMER_RESUME
CONSUMER_RESUME,
CONSUMER_SET_PREFERRED_PROFILE
};
private:
......
......@@ -47,12 +47,13 @@ namespace RTC
bool IsEnabled() const;
const RTC::RtpParameters& GetParameters() const;
bool IsPaused() const;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpProfile profile);
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile);
void GetRtcp(RTC::RTCP::CompoundPacket* packet, uint64_t now);
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket);
void ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report);
uint32_t GetTransmissionRate(uint64_t now);
void RequestFullFrame();
void SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile);
private:
void FillSupportedCodecPayloadTypes();
......@@ -90,7 +91,8 @@ namespace RTC
uint32_t lastRecvRtpTimestamp{ 0 };
bool syncRequired{ true };
// RTP profiles.
RTC::RtpProfile effectiveProfile{ RTC::RtpProfile::DEFAULT };
RTC::RtpEncodingParameters::Profile preferredProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
RTC::RtpEncodingParameters::Profile effectiveProfile{ RTC::RtpEncodingParameters::Profile::DEFAULT };
};
/* Inline methods. */
......@@ -124,6 +126,19 @@ namespace RTC
{
return this->transmittedCounter.GetRate(now) + this->retransmittedCounter.GetRate(now);
}
inline void Consumer::SetPreferredProfile(const RTC::RtpEncodingParameters::Profile profile)
{
if (this->preferredProfile == profile)
return;
this->preferredProfile = profile;
// TODO: Set it dynamically dynamic.
this->effectiveProfile = profile;
this->syncRequired = true;
RequestFullFrame();
}
} // namespace RTC
#endif
......@@ -76,7 +76,7 @@ namespace RTC
void CreateRtpStream(RTC::RtpEncodingParameters& encoding);
void ClearRtpStreams();
void ApplyRtpMapping(RTC::RtpPacket* packet) const;
RTC::RtpProfile GetRtpProfile(RTC::RtpPacket* packet);
RTC::RtpEncodingParameters::Profile GetRtpProfile(RTC::RtpPacket* packet);
/* Pure virtual methods inherited from RTC::RtpStreamRecv::Listener. */
public:
......@@ -103,7 +103,7 @@ namespace RTC
// Allocated by this.
std::map<uint32_t, RTC::RtpStreamRecv*> rtpStreams;
std::map<uint32_t, RTC::RtpStreamRecv*> mapRtxStreams;
std::map<uint32_t, RTC::RtpProfile> rtpProfiles;
std::map<uint32_t, RTC::RtpEncodingParameters::Profile> rtpProfiles;
Timer* fullFrameRequestBlockTimer{ nullptr };
// Others.
std::vector<RtpEncodingParameters> outputEncodings;
......
......@@ -16,7 +16,8 @@ namespace RTC
virtual void OnProducerRtpParametersUpdated(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::RtpProfile profile) = 0;
virtual void OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile) = 0;
};
} // namespace RTC
......
......@@ -67,7 +67,10 @@ namespace RTC
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpProfile profile) override;
void OnProducerRtpPacket(
RTC::Producer* producer,
RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
......
......@@ -31,14 +31,6 @@ namespace RTC
static std::map<Kind, Json::StaticString> kind2Json;
};
enum class RtpProfile : uint8_t
{
DEFAULT = 0,
LOW,
MEDIUM,
HIGH
};
class RtpCodecMimeType
{
public:
......@@ -214,6 +206,19 @@ namespace RTC
class RtpEncodingParameters
{
public:
enum class Profile : uint8_t
{
DEFAULT = 0,
LOW,
MEDIUM,
HIGH
};
public:
static std::map<std::string, Profile> string2Profile;
static std::map<Profile, std::string> profile2String;
public:
RtpEncodingParameters(){};
explicit RtpEncodingParameters(Json::Value& data);
......@@ -234,7 +239,7 @@ namespace RTC
bool active{ true };
std::string encodingId;
std::vector<std::string> dependencyEncodingIds;
RTC::RtpProfile profile{ RTC::RtpProfile::DEFAULT };
Profile profile{ Profile::DEFAULT };
};
class RtpHeaderExtensionParameters
......
......@@ -153,7 +153,10 @@ namespace RTC
void OnProducerRtpParametersUpdated(RTC::Producer* producer) override;
void OnProducerPaused(RTC::Producer* producer) override;
void OnProducerResumed(RTC::Producer* producer) override;
void OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpProfile profile) override;
void OnProducerRtpPacket(
RTC::Producer* producer,
RTC::RtpPacket* packet,
RTC::RtpEncodingParameters::Profile profile) override;
/* Pure virtual methods inherited from RTC::ConsumerListener. */
public:
......
......@@ -37,7 +37,8 @@ namespace Channel
{ "consumer.dump", Request::MethodId::CONSUMER_DUMP },
{ "consumer.enable", Request::MethodId::CONSUMER_ENABLE },
{ "consumer.pause", Request::MethodId::CONSUMER_PAUSE },
{ "consumer.resume", Request::MethodId::CONSUMER_RESUME }
{ "consumer.resume", Request::MethodId::CONSUMER_RESUME },
{ "consumer.setPreferredProfile", Request::MethodId::CONSUMER_SET_PREFERRED_PROFILE }
};
// clang-format on
......
......@@ -66,6 +66,8 @@ namespace RTC
static const Json::StaticString JsonStringEnabled{ "enabled" };
static const Json::StaticString JsonStringPaused{ "paused" };
static const Json::StaticString JsonStringSourcePaused{ "sourcePaused" };
static const Json::StaticString JsonStringPreferredProfile{ "preferredProfile" };
static const Json::StaticString JsonStringEffectiveProfile{ "effectiveProfile" };
Json::Value json(Json::objectValue);
......@@ -85,6 +87,12 @@ namespace RTC
json[JsonStringSourcePaused] = this->sourcePaused;
json[JsonStringPreferredProfile] =
RTC::RtpEncodingParameters::profile2String[this->preferredProfile];
json[JsonStringEffectiveProfile] =
RTC::RtpEncodingParameters::profile2String[this->effectiveProfile];
return json;
}
......@@ -245,7 +253,7 @@ namespace RTC
this->retransmittedCounter.Reset();
}
void Consumer::SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpProfile profile)
void Consumer::SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile)
{
MS_TRACE();
......
......@@ -303,13 +303,13 @@ namespace RTC
if (this->paused)
return;
RTC::RtpProfile profile;
RTC::RtpEncodingParameters::Profile profile;
try
{
profile = GetRtpProfile(packet);
}
catch(const MediaSoupError& error)
catch (const MediaSoupError& error)
{
return;
}
......@@ -418,11 +418,9 @@ namespace RTC
return;
}
else
{
// Run the timer.
this->fullFrameRequestBlockTimer->Start(FullFrameRequestBlockTimeout);
}
for (auto& kv : this->rtpStreams)
{
......@@ -588,7 +586,7 @@ namespace RTC
}
}
RTC::RtpProfile Producer::GetRtpProfile(RTC::RtpPacket* packet)
RTC::RtpEncodingParameters::Profile Producer::GetRtpProfile(RTC::RtpPacket* packet)
{
auto ssrc = packet->GetSsrc();
......
......@@ -1071,6 +1071,47 @@ namespace RTC
break;
}
case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_PROFILE:
{
static const Json::StaticString JsonStringProfile{ "profile" };
RTC::Consumer* consumer;
try
{
consumer = GetConsumerFromRequest(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;
}
consumer->SetPreferredProfile(it->second);
request->Accept();
break;
}
default:
{
MS_ERROR("unknown method");
......@@ -1329,7 +1370,8 @@ namespace RTC
}
}
void Router::OnProducerRtpPacket(RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpProfile profile)
void Router::OnProducerRtpPacket(
RTC::Producer* producer, RTC::RtpPacket* packet, RTC::RtpEncodingParameters::Profile profile)
{
MS_TRACE();
......
......@@ -7,6 +7,26 @@
namespace RTC
{
/* Class static data. */
// clang-format off
std::map<std::string, RTC::RtpEncodingParameters::Profile> RTC::RtpEncodingParameters::string2Profile =
{
{ "default", RTC::RtpEncodingParameters::Profile::DEFAULT },
{ "low", RTC::RtpEncodingParameters::Profile::LOW },
{ "medium", RTC::RtpEncodingParameters::Profile::MEDIUM },
{ "high", RTC::RtpEncodingParameters::Profile::HIGH }
};
std::map<RTC::RtpEncodingParameters::Profile, std::string> RTC::RtpEncodingParameters::profile2String =
{
{ RTC::RtpEncodingParameters::Profile::DEFAULT, "default" },
{ RTC::RtpEncodingParameters::Profile::LOW, "low" },
{ RTC::RtpEncodingParameters::Profile::MEDIUM, "medium" },
{ RTC::RtpEncodingParameters::Profile::HIGH, "high" }
};
// clang-format on
/* Instance methods. */
RtpEncodingParameters::RtpEncodingParameters(Json::Value& data)
......@@ -23,6 +43,7 @@ namespace RTC
static const Json::StaticString JsonStringActive{ "active" };
static const Json::StaticString JsonStringEncodingId{ "encodingId" };
static const Json::StaticString JsonStringDependencyEncodingIds{ "dependencyEncodingIds" };
static const Json::StaticString JsonStringProfile{ "profile" };
if (!data.isObject())
MS_THROW_ERROR("RtpEncodingParameters is not an object");
......@@ -84,6 +105,17 @@ namespace RTC
this->dependencyEncodingIds.push_back(entry.asString());
}
}
// profile is optional.
if (data[JsonStringProfile].isString())
{
std::string profileStr = data[JsonStringProfile].asString();
if (string2Profile.find(profileStr) == string2Profile.end())
MS_THROW_ERROR("unknown profile");
this->profile = string2Profile[profileStr];
}
}
Json::Value RtpEncodingParameters::ToJson() const
......@@ -100,6 +132,7 @@ namespace RTC
static const Json::StaticString JsonStringActive{ "active" };
static const Json::StaticString JsonStringEncodingId{ "encodingId" };
static const Json::StaticString JsonStringDependencyEncodingIds{ "dependencyEncodingIds" };
static const Json::StaticString JsonStringProfile{ "profile" };
Json::Value json(Json::objectValue);
......@@ -149,6 +182,8 @@ namespace RTC
}
}
json[JsonStringProfile] = RtpEncodingParameters::profile2String[this->profile];
return json;
}
} // namespace RTC
......@@ -1555,7 +1555,10 @@ namespace RTC
// Do nothing.
}
void Transport::OnProducerRtpPacket(RTC::Producer* /*producer*/, RTC::RtpPacket* /*packet*/, RTC::RtpProfile /*profile*/)
void Transport::OnProducerRtpPacket(
RTC::Producer* /*producer*/,
RTC::RtpPacket* /*packet*/,
RTC::RtpEncodingParameters::Profile /*profile*/)
{
// Do nothing.
}
......
......@@ -259,6 +259,7 @@ void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Channel::R
case Channel::Request::MethodId::CONSUMER_ENABLE:
case Channel::Request::MethodId::CONSUMER_PAUSE:
case Channel::Request::MethodId::CONSUMER_RESUME:
case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_PROFILE:
{
RTC::Router* router;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论