2016-06-02 15:45:42 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-22 16:50:43 +02:00
|
|
|
#include "modules/video_coding/fec_controller_default.h" // NOLINT
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <stdlib.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-10-22 16:50:43 +02:00
|
|
|
#include <algorithm>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <string>
|
2018-10-22 16:50:43 +02:00
|
|
|
|
2024-01-09 11:46:59 +01:00
|
|
|
#include "api/environment/environment.h"
|
|
|
|
|
#include "api/field_trials_view.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "modules/include/module_fec_types.h"
|
2018-10-22 16:50:43 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2024-01-09 11:46:59 +01:00
|
|
|
#include "system_wrappers/include/clock.h"
|
2016-06-02 15:45:42 +02:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
2018-10-22 16:50:43 +02:00
|
|
|
|
|
|
|
|
const float kProtectionOverheadRateThreshold = 0.5;
|
|
|
|
|
|
2018-01-19 17:58:57 +01:00
|
|
|
FecControllerDefault::FecControllerDefault(
|
2024-01-09 11:46:59 +01:00
|
|
|
const Environment& env,
|
2016-06-02 15:45:42 +02:00
|
|
|
VCMProtectionCallback* protection_callback)
|
2024-01-09 11:46:59 +01:00
|
|
|
: env_(env),
|
2016-06-02 15:45:42 +02:00
|
|
|
protection_callback_(protection_callback),
|
2024-05-10 15:10:24 +02:00
|
|
|
loss_prot_logic_(new media_optimization::VCMLossProtectionLogic(env_)),
|
2018-10-22 16:50:43 +02:00
|
|
|
max_payload_size_(1460),
|
|
|
|
|
overhead_threshold_(GetProtectionOverheadRateThreshold()) {}
|
2016-06-02 15:45:42 +02:00
|
|
|
|
2024-01-09 11:46:59 +01:00
|
|
|
FecControllerDefault::FecControllerDefault(const Environment& env)
|
|
|
|
|
: FecControllerDefault(env, nullptr) {}
|
2018-01-19 17:58:57 +01:00
|
|
|
|
|
|
|
|
FecControllerDefault::~FecControllerDefault(void) {
|
2016-06-02 15:45:42 +02:00
|
|
|
loss_prot_logic_->Release();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 17:58:57 +01:00
|
|
|
void FecControllerDefault::SetProtectionCallback(
|
|
|
|
|
VCMProtectionCallback* protection_callback) {
|
|
|
|
|
protection_callback_ = protection_callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FecControllerDefault::SetEncodingData(size_t width,
|
|
|
|
|
size_t height,
|
|
|
|
|
size_t num_temporal_layers,
|
|
|
|
|
size_t max_payload_size) {
|
2020-07-07 12:17:12 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2016-06-02 15:45:42 +02:00
|
|
|
loss_prot_logic_->UpdateFrameSize(width, height);
|
|
|
|
|
loss_prot_logic_->UpdateNumLayers(num_temporal_layers);
|
|
|
|
|
max_payload_size_ = max_payload_size;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 16:50:43 +02:00
|
|
|
float FecControllerDefault::GetProtectionOverheadRateThreshold() {
|
|
|
|
|
float overhead_threshold =
|
2024-01-09 11:46:59 +01:00
|
|
|
strtof(env_.field_trials()
|
|
|
|
|
.Lookup("WebRTC-ProtectionOverheadRateThreshold")
|
2018-10-22 16:50:43 +02:00
|
|
|
.c_str(),
|
|
|
|
|
nullptr);
|
|
|
|
|
if (overhead_threshold > 0 && overhead_threshold <= 1) {
|
|
|
|
|
RTC_LOG(LS_INFO) << "ProtectionOverheadRateThreshold is set to "
|
|
|
|
|
<< overhead_threshold;
|
|
|
|
|
return overhead_threshold;
|
|
|
|
|
} else if (overhead_threshold < 0 || overhead_threshold > 1) {
|
2021-11-27 21:31:08 +00:00
|
|
|
RTC_LOG(LS_WARNING)
|
|
|
|
|
<< "ProtectionOverheadRateThreshold field trial is set to "
|
|
|
|
|
"an invalid value, expecting a value between (0, 1].";
|
2018-10-22 16:50:43 +02:00
|
|
|
}
|
|
|
|
|
// WebRTC-ProtectionOverheadRateThreshold field trial string is not found, use
|
|
|
|
|
// the default value.
|
|
|
|
|
return kProtectionOverheadRateThreshold;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 17:58:57 +01:00
|
|
|
uint32_t FecControllerDefault::UpdateFecRates(
|
2016-06-02 15:45:42 +02:00
|
|
|
uint32_t estimated_bitrate_bps,
|
|
|
|
|
int actual_framerate_fps,
|
|
|
|
|
uint8_t fraction_lost,
|
2018-01-19 17:58:57 +01:00
|
|
|
std::vector<bool> loss_mask_vector,
|
2016-06-02 15:45:42 +02:00
|
|
|
int64_t round_trip_time_ms) {
|
|
|
|
|
float target_bitrate_kbps =
|
|
|
|
|
static_cast<float>(estimated_bitrate_bps) / 1000.0f;
|
|
|
|
|
// Sanity check.
|
|
|
|
|
if (actual_framerate_fps < 1.0) {
|
|
|
|
|
actual_framerate_fps = 1.0;
|
|
|
|
|
}
|
|
|
|
|
FecProtectionParams delta_fec_params;
|
|
|
|
|
FecProtectionParams key_fec_params;
|
|
|
|
|
{
|
2020-07-07 12:17:12 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2016-06-02 15:45:42 +02:00
|
|
|
loss_prot_logic_->UpdateBitRate(target_bitrate_kbps);
|
|
|
|
|
loss_prot_logic_->UpdateRtt(round_trip_time_ms);
|
|
|
|
|
// Update frame rate for the loss protection logic class: frame rate should
|
|
|
|
|
// be the actual/sent rate.
|
|
|
|
|
loss_prot_logic_->UpdateFrameRate(actual_framerate_fps);
|
|
|
|
|
// Returns the filtered packet loss, used for the protection setting.
|
|
|
|
|
// The filtered loss may be the received loss (no filter), or some
|
|
|
|
|
// filtered value (average or max window filter).
|
|
|
|
|
// Use max window filter for now.
|
|
|
|
|
media_optimization::FilterPacketLossMode filter_mode =
|
|
|
|
|
media_optimization::kMaxFilter;
|
|
|
|
|
uint8_t packet_loss_enc = loss_prot_logic_->FilteredLoss(
|
2024-01-09 11:46:59 +01:00
|
|
|
env_.clock().TimeInMilliseconds(), filter_mode, fraction_lost);
|
2016-06-02 15:45:42 +02:00
|
|
|
// For now use the filtered loss for computing the robustness settings.
|
|
|
|
|
loss_prot_logic_->UpdateFilteredLossPr(packet_loss_enc);
|
|
|
|
|
if (loss_prot_logic_->SelectedType() == media_optimization::kNone) {
|
|
|
|
|
return estimated_bitrate_bps;
|
|
|
|
|
}
|
|
|
|
|
// Update method will compute the robustness settings for the given
|
|
|
|
|
// protection method and the overhead cost
|
|
|
|
|
// the protection method is set by the user via SetVideoProtection.
|
|
|
|
|
loss_prot_logic_->UpdateMethod();
|
|
|
|
|
// Get the bit cost of protection method, based on the amount of
|
|
|
|
|
// overhead data actually transmitted (including headers) the last
|
|
|
|
|
// second.
|
|
|
|
|
// Get the FEC code rate for Key frames (set to 0 when NA).
|
|
|
|
|
key_fec_params.fec_rate =
|
|
|
|
|
loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorK();
|
|
|
|
|
// Get the FEC code rate for Delta frames (set to 0 when NA).
|
|
|
|
|
delta_fec_params.fec_rate =
|
|
|
|
|
loss_prot_logic_->SelectedMethod()->RequiredProtectionFactorD();
|
2021-08-09 13:02:57 +02:00
|
|
|
// The RTP module currently requires the same `max_fec_frames` for both
|
2016-06-02 15:45:42 +02:00
|
|
|
// key and delta frames.
|
|
|
|
|
delta_fec_params.max_fec_frames =
|
|
|
|
|
loss_prot_logic_->SelectedMethod()->MaxFramesFec();
|
|
|
|
|
key_fec_params.max_fec_frames =
|
|
|
|
|
loss_prot_logic_->SelectedMethod()->MaxFramesFec();
|
|
|
|
|
}
|
2021-08-09 13:02:57 +02:00
|
|
|
// Set the FEC packet mask type. `kFecMaskBursty` is more effective for
|
2016-06-02 15:45:42 +02:00
|
|
|
// consecutive losses and little/no packet re-ordering. As we currently
|
|
|
|
|
// do not have feedback data on the degree of correlated losses and packet
|
2021-08-09 13:02:57 +02:00
|
|
|
// re-ordering, we keep default setting to `kFecMaskRandom` for now.
|
2016-06-02 15:45:42 +02:00
|
|
|
delta_fec_params.fec_mask_type = kFecMaskRandom;
|
|
|
|
|
key_fec_params.fec_mask_type = kFecMaskRandom;
|
|
|
|
|
// Update protection callback with protection settings.
|
|
|
|
|
uint32_t sent_video_rate_bps = 0;
|
|
|
|
|
uint32_t sent_nack_rate_bps = 0;
|
|
|
|
|
uint32_t sent_fec_rate_bps = 0;
|
|
|
|
|
// Rate cost of the protection methods.
|
|
|
|
|
float protection_overhead_rate = 0.0f;
|
|
|
|
|
// TODO(Marco): Pass FEC protection values per layer.
|
|
|
|
|
protection_callback_->ProtectionRequest(
|
|
|
|
|
&delta_fec_params, &key_fec_params, &sent_video_rate_bps,
|
|
|
|
|
&sent_nack_rate_bps, &sent_fec_rate_bps);
|
|
|
|
|
uint32_t sent_total_rate_bps =
|
|
|
|
|
sent_video_rate_bps + sent_nack_rate_bps + sent_fec_rate_bps;
|
|
|
|
|
// Estimate the overhead costs of the next second as staying the same
|
|
|
|
|
// wrt the source bitrate.
|
|
|
|
|
if (sent_total_rate_bps > 0) {
|
|
|
|
|
protection_overhead_rate =
|
|
|
|
|
static_cast<float>(sent_nack_rate_bps + sent_fec_rate_bps) /
|
|
|
|
|
sent_total_rate_bps;
|
|
|
|
|
}
|
2018-10-22 16:50:43 +02:00
|
|
|
// Cap the overhead estimate to a threshold, default is 50%.
|
|
|
|
|
protection_overhead_rate =
|
|
|
|
|
std::min(protection_overhead_rate, overhead_threshold_);
|
2016-06-02 15:45:42 +02:00
|
|
|
// Source coding rate: total rate - protection overhead.
|
|
|
|
|
return estimated_bitrate_bps * (1.0 - protection_overhead_rate);
|
|
|
|
|
}
|
2019-06-05 11:20:36 +02:00
|
|
|
|
2018-01-19 17:58:57 +01:00
|
|
|
void FecControllerDefault::SetProtectionMethod(bool enable_fec,
|
|
|
|
|
bool enable_nack) {
|
2016-06-02 15:45:42 +02:00
|
|
|
media_optimization::VCMProtectionMethodEnum method(media_optimization::kNone);
|
|
|
|
|
if (enable_fec && enable_nack) {
|
|
|
|
|
method = media_optimization::kNackFec;
|
|
|
|
|
} else if (enable_nack) {
|
|
|
|
|
method = media_optimization::kNack;
|
|
|
|
|
} else if (enable_fec) {
|
|
|
|
|
method = media_optimization::kFec;
|
|
|
|
|
}
|
2020-07-07 12:17:12 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2016-06-02 15:45:42 +02:00
|
|
|
loss_prot_logic_->SetMethod(method);
|
|
|
|
|
}
|
2019-06-05 11:20:36 +02:00
|
|
|
|
2018-01-19 17:58:57 +01:00
|
|
|
void FecControllerDefault::UpdateWithEncodedData(
|
Revert "Revert "Enables PeerConnectionFactory using external fec controller""
This reverts commit 00733015fafbbc61ddc12dfdc88b21a9fcd9d122.
Reason for revert: The reason for a downstream test failure on the original commit and a workaround has been found. Solution is to keep a PeerConnectionFactory constructor implementation as the same as before.
Original change's description:
> Revert "Enables PeerConnectionFactory using external fec controller"
>
> This reverts commit 4f07bdb25567d8ef528311e0b50a62c61d543fc3.
>
> Reason for revert: Speculatively reverting, because downstream test is now hitting "PeerConnectionFactory.initialize was not called before creating a PeerConnectionFactory" error, even though it did call initialize. I don't see how any change in this CL could cause that, but it's the only CL on the blamelist, and it does modify PeerConnectionFactory.java
>
> Original change's description:
> > Enables PeerConnectionFactory using external fec controller
> >
> > Bug: webrtc:8799
> > Change-Id: Ieb2cf6163b9a83844ab9ed4822b4a7f1db4c24b8
> > Reviewed-on: https://webrtc-review.googlesource.com/43961
> > Commit-Queue: Ying Wang <yinwa@webrtc.org>
> > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> > Reviewed-by: Niels Moller <nisse@webrtc.org>
> > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#22038}
>
> TBR=sakal@webrtc.org,kwiberg@webrtc.org,nisse@webrtc.org,stefan@webrtc.org,yinwa@webrtc.org
>
> Change-Id: I95868c35d6f9973e0ebf563814cd71d0fcbd433d
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:8799
> Reviewed-on: https://webrtc-review.googlesource.com/54080
> Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
> Commit-Queue: Taylor Brandstetter <deadbeef@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#22040}
TBR=deadbeef@webrtc.org,sakal@webrtc.org,kwiberg@webrtc.org,nisse@webrtc.org,stefan@webrtc.org,yinwa@webrtc.org
Bug: webrtc:8799
Change-Id: If9f3292bfcc739782967530c49f006d0abbc38a8
Reviewed-on: https://webrtc-review.googlesource.com/55400
Commit-Queue: Ying Wang <yinwa@webrtc.org>
Reviewed-by: Ying Wang <yinwa@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22100}
2018-02-20 12:50:27 +01:00
|
|
|
const size_t encoded_image_length,
|
2019-03-07 10:18:23 +01:00
|
|
|
const VideoFrameType encoded_image_frametype) {
|
Revert "Revert "Enables PeerConnectionFactory using external fec controller""
This reverts commit 00733015fafbbc61ddc12dfdc88b21a9fcd9d122.
Reason for revert: The reason for a downstream test failure on the original commit and a workaround has been found. Solution is to keep a PeerConnectionFactory constructor implementation as the same as before.
Original change's description:
> Revert "Enables PeerConnectionFactory using external fec controller"
>
> This reverts commit 4f07bdb25567d8ef528311e0b50a62c61d543fc3.
>
> Reason for revert: Speculatively reverting, because downstream test is now hitting "PeerConnectionFactory.initialize was not called before creating a PeerConnectionFactory" error, even though it did call initialize. I don't see how any change in this CL could cause that, but it's the only CL on the blamelist, and it does modify PeerConnectionFactory.java
>
> Original change's description:
> > Enables PeerConnectionFactory using external fec controller
> >
> > Bug: webrtc:8799
> > Change-Id: Ieb2cf6163b9a83844ab9ed4822b4a7f1db4c24b8
> > Reviewed-on: https://webrtc-review.googlesource.com/43961
> > Commit-Queue: Ying Wang <yinwa@webrtc.org>
> > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> > Reviewed-by: Niels Moller <nisse@webrtc.org>
> > Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#22038}
>
> TBR=sakal@webrtc.org,kwiberg@webrtc.org,nisse@webrtc.org,stefan@webrtc.org,yinwa@webrtc.org
>
> Change-Id: I95868c35d6f9973e0ebf563814cd71d0fcbd433d
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:8799
> Reviewed-on: https://webrtc-review.googlesource.com/54080
> Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
> Commit-Queue: Taylor Brandstetter <deadbeef@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#22040}
TBR=deadbeef@webrtc.org,sakal@webrtc.org,kwiberg@webrtc.org,nisse@webrtc.org,stefan@webrtc.org,yinwa@webrtc.org
Bug: webrtc:8799
Change-Id: If9f3292bfcc739782967530c49f006d0abbc38a8
Reviewed-on: https://webrtc-review.googlesource.com/55400
Commit-Queue: Ying Wang <yinwa@webrtc.org>
Reviewed-by: Ying Wang <yinwa@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22100}
2018-02-20 12:50:27 +01:00
|
|
|
const size_t encoded_length = encoded_image_length;
|
2020-07-07 12:17:12 +02:00
|
|
|
MutexLock lock(&mutex_);
|
2016-06-02 15:45:42 +02:00
|
|
|
if (encoded_length > 0) {
|
2019-03-21 15:43:58 +01:00
|
|
|
const bool delta_frame =
|
|
|
|
|
encoded_image_frametype != VideoFrameType::kVideoFrameKey;
|
2016-06-02 15:45:42 +02:00
|
|
|
if (max_payload_size_ > 0 && encoded_length > 0) {
|
|
|
|
|
const float min_packets_per_frame =
|
|
|
|
|
encoded_length / static_cast<float>(max_payload_size_);
|
|
|
|
|
if (delta_frame) {
|
2024-01-09 11:46:59 +01:00
|
|
|
loss_prot_logic_->UpdatePacketsPerFrame(
|
|
|
|
|
min_packets_per_frame, env_.clock().TimeInMilliseconds());
|
2016-06-02 15:45:42 +02:00
|
|
|
} else {
|
|
|
|
|
loss_prot_logic_->UpdatePacketsPerFrameKey(
|
2024-01-09 11:46:59 +01:00
|
|
|
min_packets_per_frame, env_.clock().TimeInMilliseconds());
|
2016-06-02 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!delta_frame && encoded_length > 0) {
|
|
|
|
|
loss_prot_logic_->UpdateKeyFrameSize(static_cast<float>(encoded_length));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-28 16:30:18 +02:00
|
|
|
|
|
|
|
|
bool FecControllerDefault::UseLossVectorMask() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 15:45:42 +02:00
|
|
|
} // namespace webrtc
|