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

- transport.consume() now allow passing preferredLayers.

- Remove Consumer::TransportConnected().
- Add Consumer::UseBandwidth() (fake for now).
- Add Consumer::ProducerRtpStream() (to differentiate it from ProducerNewRtpStream() method).
- SimulcastConsumer::Paused() now unset current and target layers to -1.
- SimulcastConsumer::Resumed() now calls to RecalculateTargetSpatialLayer().
上级 7445bae4
...@@ -361,6 +361,7 @@ class Transport extends EnhancedEventEmitter ...@@ -361,6 +361,7 @@ class Transport extends EnhancedEventEmitter
* @param {String} producerId * @param {String} producerId
* @param {RTCRtpCapabilities} rtpCapabilities - Remote RTP capabilities. * @param {RTCRtpCapabilities} rtpCapabilities - Remote RTP capabilities.
* @param {Boolean} [paused=false] - Whether the Consumer must start paused. * @param {Boolean} [paused=false] - Whether the Consumer must start paused.
* @param {Object} [preferredLayers] - Preferred spatial and temporal layers.
* @param {Object} [appData={}] - Custom app data. * @param {Object} [appData={}] - Custom app data.
* *
* @async * @async
...@@ -372,6 +373,7 @@ class Transport extends EnhancedEventEmitter ...@@ -372,6 +373,7 @@ class Transport extends EnhancedEventEmitter
producerId, producerId,
rtpCapabilities, rtpCapabilities,
paused = false, paused = false,
preferredLayers,
appData = {} appData = {}
} = {} } = {}
) )
...@@ -401,7 +403,8 @@ class Transport extends EnhancedEventEmitter ...@@ -401,7 +403,8 @@ class Transport extends EnhancedEventEmitter
rtpParameters, rtpParameters,
type : producer.type, type : producer.type,
consumableRtpEncodings : producer.consumableRtpParameters.encodings, consumableRtpEncodings : producer.consumableRtpParameters.encodings,
paused paused,
preferredLayers
}; };
const status = const status =
......
...@@ -345,6 +345,7 @@ test('transport.consume() succeeds', async () => ...@@ -345,6 +345,7 @@ test('transport.consume() succeeds', async () =>
producerId : videoProducer.id, producerId : videoProducer.id,
rtpCapabilities : consumerDeviceCapabilities, rtpCapabilities : consumerDeviceCapabilities,
paused : true, paused : true,
preferredLayers : { spatialLayer: 12 },
appData : { baz: 'LOL' } appData : { baz: 'LOL' }
}); });
......
...@@ -49,10 +49,11 @@ namespace RTC ...@@ -49,10 +49,11 @@ namespace RTC
const std::vector<uint32_t>& GetMediaSsrcs() const; const std::vector<uint32_t>& GetMediaSsrcs() const;
bool IsActive() const; bool IsActive() const;
bool IsPaused() const; bool IsPaused() const;
bool IsProducerPaused() const; // This is needed by the Transport. bool IsProducerPaused() const;
virtual void TransportConnected() = 0; virtual void UseBandwidth(uint32_t availableBandwidth) = 0;
void ProducerPaused(); void ProducerPaused();
void ProducerResumed(); void ProducerResumed();
virtual void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0;
virtual void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0; virtual void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) = 0;
virtual void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) = 0; virtual void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) = 0;
void ProducerClosed(); void ProducerClosed();
......
...@@ -17,7 +17,8 @@ namespace RTC ...@@ -17,7 +17,8 @@ namespace RTC
void FillJsonStats(json& jsonArray) const override; void FillJsonStats(json& jsonArray) const override;
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
void TransportConnected() override; void UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
......
...@@ -19,7 +19,8 @@ namespace RTC ...@@ -19,7 +19,8 @@ namespace RTC
void FillJsonStats(json& jsonArray) const override; void FillJsonStats(json& jsonArray) const override;
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
void TransportConnected() override; void UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
......
...@@ -19,7 +19,8 @@ namespace RTC ...@@ -19,7 +19,8 @@ namespace RTC
void FillJsonStats(json& jsonArray) const override; void FillJsonStats(json& jsonArray) const override;
void FillJsonScore(json& jsonObject) const override; void FillJsonScore(json& jsonObject) const override;
void HandleRequest(Channel::Request* request) override; void HandleRequest(Channel::Request* request) override;
void TransportConnected() override; void UseBandwidth(uint32_t availableBandwidth) override;
void ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override; void ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) override;
void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override; void ProducerRtpStreamScore(RTC::RtpStream* rtpStream, uint8_t score) override;
void SendRtpPacket(RTC::RtpPacket* packet) override; void SendRtpPacket(RTC::RtpPacket* packet) override;
...@@ -39,7 +40,7 @@ namespace RTC ...@@ -39,7 +40,7 @@ namespace RTC
void RetransmitRtpPacket(RTC::RtpPacket* packet); void RetransmitRtpPacket(RTC::RtpPacket* packet);
void EmitScore() const; void EmitScore() const;
void SetCurrentSpatialLayer(int16_t spatialLayer); void SetCurrentSpatialLayer(int16_t spatialLayer);
void RecalculateTargetSpatialLayer(bool force = false); void RecalculateTargetSpatialLayer();
RTC::RtpStream* GetProducerCurrentRtpStream() const; RTC::RtpStream* GetProducerCurrentRtpStream() const;
RTC::RtpStream* GetProducerTargetRtpStream() const; RTC::RtpStream* GetProducerTargetRtpStream() const;
......
...@@ -80,13 +80,20 @@ namespace RTC ...@@ -80,13 +80,20 @@ namespace RTC
} }
} }
void PipeConsumer::TransportConnected() void PipeConsumer::UseBandwidth(uint32_t availableBandwidth)
{ {
MS_TRACE(); MS_TRACE();
RequestKeyFrame(); RequestKeyFrame();
} }
void PipeConsumer::ProducerRtpStream(RTC::RtpStream* /*rtpStream*/, uint32_t /*mappedSsrc*/)
{
MS_TRACE();
// Do nothing.
}
void PipeConsumer::ProducerNewRtpStream(RTC::RtpStream* /*rtpStream*/, uint32_t /*mappedSsrc*/) void PipeConsumer::ProducerNewRtpStream(RTC::RtpStream* /*rtpStream*/, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -181,8 +188,7 @@ namespace RTC ...@@ -181,8 +188,7 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsActive()) // TODO: Must call the corresponding rtpStream->ReceiveKeyFrameRequest(messageType);
return;
RequestKeyFrame(); RequestKeyFrame();
} }
......
...@@ -35,16 +35,16 @@ namespace RTC ...@@ -35,16 +35,16 @@ namespace RTC
// TODO: Testing. // TODO: Testing.
uint32_t rembBitrate = static_cast<uint32_t>(remb->GetBitrate()); // uint32_t rembBitrate = static_cast<uint32_t>(remb->GetBitrate());
uint64_t now = DepLibUV::GetTime(); // uint64_t now = DepLibUV::GetTime();
uint32_t bitrate = this->transmissionCounter.GetRate(now); // uint32_t bitrate = this->transmissionCounter.GetRate(now);
int32_t diff = rembBitrate - bitrate; // int32_t diff = rembBitrate - bitrate;
MS_ERROR( // MS_ERROR(
"REMB received [sending bitrate:%" PRIu32 ", REMB available bitrate:%" PRIu32 // "REMB received [sending bitrate:%" PRIu32 ", REMB available bitrate:%" PRIu32
", diff:%" PRIi32 "]", // ", diff:%" PRIi32 "]",
bitrate, // bitrate,
rembBitrate, // rembBitrate,
diff); // diff);
} }
} // namespace RTC } // namespace RTC
...@@ -646,15 +646,12 @@ namespace RTC ...@@ -646,15 +646,12 @@ namespace RTC
this->mapConsumerProducer[consumer] = producer; this->mapConsumerProducer[consumer] = producer;
// Get all streams in the Producer and provide the Consumer with them. // Get all streams in the Producer and provide the Consumer with them.
// NOTE: This must be done at the end. Otherwise, if consumer->ProducerNewRtpStream()
// invokes Router::OnTransportConsumerKeyFrameRequested(), it would crash due to
// Consumer still not being present in Router maps.
for (auto& kv : producer->GetRtpStreams()) for (auto& kv : producer->GetRtpStreams())
{ {
auto* rtpStream = kv.first; auto* rtpStream = kv.first;
uint32_t mappedSsrc = kv.second; uint32_t mappedSsrc = kv.second;
consumer->ProducerNewRtpStream(rtpStream, mappedSsrc); consumer->ProducerRtpStream(rtpStream, mappedSsrc);
} }
} }
......
...@@ -99,13 +99,23 @@ namespace RTC ...@@ -99,13 +99,23 @@ namespace RTC
} }
} }
void SimpleConsumer::TransportConnected() void SimpleConsumer::UseBandwidth(uint32_t availableBandwidth)
{ {
MS_TRACE(); MS_TRACE();
RequestKeyFrame(); RequestKeyFrame();
} }
void SimpleConsumer::ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t /*mappedSsrc*/)
{
MS_TRACE();
this->producerRtpStream = rtpStream;
// Emit the score event.
EmitScore();
}
void SimpleConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t /*mappedSsrc*/) void SimpleConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t /*mappedSsrc*/)
{ {
MS_TRACE(); MS_TRACE();
...@@ -298,9 +308,6 @@ namespace RTC ...@@ -298,9 +308,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsActive())
return;
this->rtpStream->ReceiveKeyFrameRequest(messageType); this->rtpStream->ReceiveKeyFrameRequest(messageType);
RequestKeyFrame(); RequestKeyFrame();
......
...@@ -21,11 +21,7 @@ namespace RTC ...@@ -21,11 +21,7 @@ namespace RTC
if (this->consumableRtpEncodings.size() <= 1) if (this->consumableRtpEncodings.size() <= 1)
MS_THROW_TYPE_ERROR("invalid consumableRtpEncodings with size <= 1"); MS_THROW_TYPE_ERROR("invalid consumableRtpEncodings with size <= 1");
// Set the RTCP report generation interval. auto jsonPreferredLayersIt = data.find("preferredLayers");
if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
this->maxRtcpInterval = RTC::RTCP::MaxVideoIntervalMs;
// Fill mapMappedSsrcSpatialLayer. // Fill mapMappedSsrcSpatialLayer.
for (size_t idx{ 0 }; idx < this->consumableRtpEncodings.size(); ++idx) for (size_t idx{ 0 }; idx < this->consumableRtpEncodings.size(); ++idx)
...@@ -35,12 +31,42 @@ namespace RTC ...@@ -35,12 +31,42 @@ namespace RTC
this->mapMappedSsrcSpatialLayer[encoding.ssrc] = static_cast<int16_t>(idx); this->mapMappedSsrcSpatialLayer[encoding.ssrc] = static_cast<int16_t>(idx);
} }
// Set preferredLayers (if given).
if (jsonPreferredLayersIt != data.end() && jsonPreferredLayersIt->is_object())
{
// TODO: Handle temporalLayer.
auto jsonSpatialLayerIt = jsonPreferredLayersIt->find("spatialLayer");
if (jsonSpatialLayerIt == jsonPreferredLayersIt->end() || !jsonSpatialLayerIt->is_number_unsigned())
{
MS_THROW_TYPE_ERROR("missing preferredLayers.spatialLayer");
}
this->preferredSpatialLayer = jsonSpatialLayerIt->get<int16_t>();
if (this->preferredSpatialLayer >= static_cast<int16_t>(this->consumableRtpEncodings.size()))
{
this->preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1;
}
}
else
{
// Initially set preferreSpatialLayer to the maximum value.
this->preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1;
// TODO: Set initial temporal layer.
}
// Reserve space for the Producer RTP streams. // Reserve space for the Producer RTP streams.
this->producerRtpStreams.insert( this->producerRtpStreams.insert(
this->producerRtpStreams.begin(), this->consumableRtpEncodings.size(), nullptr); this->producerRtpStreams.begin(), this->consumableRtpEncodings.size(), nullptr);
// Initially set preferreSpatialLayer to the maximum value. // Set the RTCP report generation interval.
this->preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1; if (this->kind == RTC::Media::Kind::AUDIO)
this->maxRtcpInterval = RTC::RTCP::MaxAudioIntervalMs;
else
this->maxRtcpInterval = RTC::RTCP::MaxVideoIntervalMs;
// Create RtpStreamSend instance for sending a single stream to the remote. // Create RtpStreamSend instance for sending a single stream to the remote.
CreateRtpStream(); CreateRtpStream();
...@@ -122,6 +148,8 @@ namespace RTC ...@@ -122,6 +148,8 @@ namespace RTC
case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_LAYERS: case Channel::Request::MethodId::CONSUMER_SET_PREFERRED_LAYERS:
{ {
// TODO: Handle temporalLayer.
auto jsonSpatialLayerIt = request->data.find("spatialLayer"); auto jsonSpatialLayerIt = request->data.find("spatialLayer");
if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned()) if (jsonSpatialLayerIt == request->data.end() || !jsonSpatialLayerIt->is_number_unsigned())
...@@ -131,9 +159,9 @@ namespace RTC ...@@ -131,9 +159,9 @@ namespace RTC
auto preferredSpatialLayer = jsonSpatialLayerIt->get<int16_t>(); auto preferredSpatialLayer = jsonSpatialLayerIt->get<int16_t>();
if (preferredSpatialLayer >= static_cast<int16_t>(this->mapMappedSsrcSpatialLayer.size())) if (preferredSpatialLayer >= static_cast<int16_t>(this->consumableRtpEncodings.size()))
{ {
preferredSpatialLayer = static_cast<int16_t>(this->mapMappedSsrcSpatialLayer.size()) - 1; preferredSpatialLayer = static_cast<int16_t>(this->consumableRtpEncodings.size()) - 1;
} }
if (preferredSpatialLayer == this->preferredSpatialLayer) if (preferredSpatialLayer == this->preferredSpatialLayer)
...@@ -150,7 +178,8 @@ namespace RTC ...@@ -150,7 +178,8 @@ namespace RTC
this->preferredSpatialLayer, this->preferredSpatialLayer,
this->id.c_str()); this->id.c_str());
RecalculateTargetSpatialLayer(true /*force*/); if (IsActive())
RecalculateTargetSpatialLayer();
request->Accept(); request->Accept();
...@@ -165,11 +194,28 @@ namespace RTC ...@@ -165,11 +194,28 @@ namespace RTC
} }
} }
void SimulcastConsumer::TransportConnected() void SimulcastConsumer::UseBandwidth(uint32_t availableBandwidth)
{ {
MS_TRACE(); MS_TRACE();
RequestKeyFrame(); if (IsActive())
RecalculateTargetSpatialLayer();
}
void SimulcastConsumer::ProducerRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
{
MS_TRACE();
auto it = this->mapMappedSsrcSpatialLayer.find(mappedSsrc);
MS_ASSERT(it != this->mapMappedSsrcSpatialLayer.end(), "unknown mappedSsrc");
int16_t spatialLayer = it->second;
this->producerRtpStreams[spatialLayer] = rtpStream;
// Emit the score event.
EmitScore();
} }
void SimulcastConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc) void SimulcastConsumer::ProducerNewRtpStream(RTC::RtpStream* rtpStream, uint32_t mappedSsrc)
...@@ -185,10 +231,9 @@ namespace RTC ...@@ -185,10 +231,9 @@ namespace RTC
this->producerRtpStreams[spatialLayer] = rtpStream; this->producerRtpStreams[spatialLayer] = rtpStream;
// Recalculate layers. // Recalculate layers.
if (IsActive())
RecalculateTargetSpatialLayer(); RecalculateTargetSpatialLayer();
// TODO: Probably just if this stream is the current spatial layer.
//
// Emit the score event. // Emit the score event.
EmitScore(); EmitScore();
} }
...@@ -198,6 +243,7 @@ namespace RTC ...@@ -198,6 +243,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Recalculate layers. // Recalculate layers.
if (IsActive())
RecalculateTargetSpatialLayer(); RecalculateTargetSpatialLayer();
// Emit the score event. // Emit the score event.
...@@ -400,9 +446,6 @@ namespace RTC ...@@ -400,9 +446,6 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
if (!IsActive())
return;
this->rtpStream->ReceiveKeyFrameRequest(messageType); this->rtpStream->ReceiveKeyFrameRequest(messageType);
RequestKeyFrame(); RequestKeyFrame();
...@@ -449,6 +492,10 @@ namespace RTC ...@@ -449,6 +492,10 @@ namespace RTC
MS_TRACE(); MS_TRACE();
this->rtpStream->Pause(); this->rtpStream->Pause();
// Unset current and target layers.
this->targetSpatialLayer = -1;
this->currentSpatialLayer = -1;
} }
void SimulcastConsumer::Resumed(bool wasProducer) void SimulcastConsumer::Resumed(bool wasProducer)
...@@ -461,10 +508,8 @@ namespace RTC ...@@ -461,10 +508,8 @@ namespace RTC
// receiver will request lot of NACKs due to unknown RTP packets. // receiver will request lot of NACKs due to unknown RTP packets.
this->syncRequired = true; this->syncRequired = true;
// If we have been resumed due to the Producer becoming resumed, we don't if (IsActive())
// need to request a key frame since the Producer already requested it. RecalculateTargetSpatialLayer();
if (!wasProducer)
RequestKeyFrame();
} }
void SimulcastConsumer::CreateRtpStream() void SimulcastConsumer::CreateRtpStream()
...@@ -552,12 +597,12 @@ namespace RTC ...@@ -552,12 +597,12 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
auto* producerTargetRtpStream = GetProducerTargetRtpStream();
auto* producerCurrentRtpStream = GetProducerCurrentRtpStream();
if (!IsActive() || this->kind != RTC::Media::Kind::VIDEO) if (!IsActive() || this->kind != RTC::Media::Kind::VIDEO)
return; return;
auto* producerTargetRtpStream = GetProducerTargetRtpStream();
auto* producerCurrentRtpStream = GetProducerCurrentRtpStream();
if (producerTargetRtpStream) if (producerTargetRtpStream)
{ {
auto mappedSsrc = this->consumableRtpEncodings[this->targetSpatialLayer].ssrc; auto mappedSsrc = this->consumableRtpEncodings[this->targetSpatialLayer].ssrc;
...@@ -611,7 +656,7 @@ namespace RTC ...@@ -611,7 +656,7 @@ namespace RTC
EmitScore(); EmitScore();
} }
void SimulcastConsumer::RecalculateTargetSpatialLayer(bool /*force*/) void SimulcastConsumer::RecalculateTargetSpatialLayer()
{ {
MS_TRACE(); MS_TRACE();
...@@ -792,7 +837,8 @@ namespace RTC ...@@ -792,7 +837,8 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
this->RecalculateTargetSpatialLayer(); if (IsActive())
RecalculateTargetSpatialLayer();
// Emit the score event. // Emit the score event.
EmitScore(); EmitScore();
......
...@@ -338,9 +338,11 @@ namespace RTC ...@@ -338,9 +338,11 @@ namespace RTC
// Tell the subclass. // Tell the subclass.
UserOnNewConsumer(consumer); UserOnNewConsumer(consumer);
// If the Transport is already connected tell it to the new Consumer. // TODO: Update with new BWE API.
//
// If the Transport is already connected tell the new Consumer to ask for bandwidth.
if (IsConnected()) if (IsConnected())
consumer->TransportConnected(); consumer->UseBandwidth(0);
break; break;
} }
...@@ -437,13 +439,13 @@ namespace RTC ...@@ -437,13 +439,13 @@ namespace RTC
// Start the RTCP timer. // Start the RTCP timer.
this->rtcpTimer->Start(static_cast<uint64_t>(RTC::RTCP::MaxVideoIntervalMs / 2)); this->rtcpTimer->Start(static_cast<uint64_t>(RTC::RTCP::MaxVideoIntervalMs / 2));
// Iterate all Consumers and tell them that the Transport is connected, so they // Iterate all Consumers and tell them to use bandwidth.
// will request key frames.
for (auto& kv : this->mapConsumers) for (auto& kv : this->mapConsumers)
{ {
auto* consumer = kv.second; auto* consumer = kv.second;
consumer->TransportConnected(); // TODO: Update with new BWE API.
consumer->UseBandwidth(0);
} }
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论