Migrate software fallback wrapper to new VideoDecoder::Configure

Bug: webrtc:13045
Change-Id: I5082a5d12a43313842f8d5eb1fa70a12671e572c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/228434
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34733}
This commit is contained in:
Danil Chapovalov 2021-08-12 11:04:45 +02:00 committed by WebRTC LUCI CQ
parent 647d326438
commit 7fa3f40626
2 changed files with 44 additions and 72 deletions

View File

@ -15,7 +15,6 @@
#include "absl/types/optional.h"
#include "api/video/encoded_image.h"
#include "api/video/video_frame.h"
#include "api/video_codecs/video_codec.h"
#include "api/video_codecs/video_decoder.h"
#include "modules/video_coding/codecs/vp8/include/vp8.h"
#include "modules/video_coding/include/video_codec_interface.h"
@ -40,10 +39,9 @@ class VideoDecoderSoftwareFallbackWrapperTest : public ::testing::Test {
class CountingFakeDecoder : public VideoDecoder {
public:
int32_t InitDecode(const VideoCodec* codec_settings,
int32_t number_of_cores) override {
++init_decode_count_;
return init_decode_return_code_;
bool Configure(const Settings& settings) override {
++configure_count_;
return configure_return_value_;
}
int32_t Decode(const EncodedImage& input_image,
@ -66,9 +64,9 @@ class VideoDecoderSoftwareFallbackWrapperTest : public ::testing::Test {
const char* ImplementationName() const override { return "fake-decoder"; }
int init_decode_count_ = 0;
int configure_count_ = 0;
int decode_count_ = 0;
int32_t init_decode_return_code_ = WEBRTC_VIDEO_CODEC_OK;
bool configure_return_value_ = true;
int32_t decode_return_code_ = WEBRTC_VIDEO_CODEC_OK;
DecodedImageCallback* decode_complete_callback_ = nullptr;
int release_count_ = 0;
@ -81,29 +79,27 @@ class VideoDecoderSoftwareFallbackWrapperTest : public ::testing::Test {
};
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, InitializesDecoder) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
EXPECT_EQ(1, fake_decoder_->init_decode_count_);
fallback_wrapper_->Configure({});
EXPECT_EQ(1, fake_decoder_->configure_count_);
EncodedImage encoded_image;
encoded_image._frameType = VideoFrameType::kVideoFrameKey;
fallback_wrapper_->Decode(encoded_image, false, -1);
EXPECT_EQ(1, fake_decoder_->init_decode_count_)
EXPECT_EQ(1, fake_decoder_->configure_count_)
<< "Initialized decoder should not be reinitialized.";
EXPECT_EQ(1, fake_decoder_->decode_count_);
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
UsesFallbackDecoderAfterAnyInitDecodeFailure) {
VideoCodec codec = {};
fake_decoder_->init_decode_return_code_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED;
fallback_wrapper_->InitDecode(&codec, 2);
EXPECT_EQ(1, fake_decoder_->init_decode_count_);
fake_decoder_->configure_return_value_ = false;
fallback_wrapper_->Configure({});
EXPECT_EQ(1, fake_decoder_->configure_count_);
EncodedImage encoded_image;
encoded_image._frameType = VideoFrameType::kVideoFrameKey;
fallback_wrapper_->Decode(encoded_image, false, -1);
EXPECT_EQ(1, fake_decoder_->init_decode_count_)
EXPECT_EQ(1, fake_decoder_->configure_count_)
<< "Should not have attempted reinitializing the fallback decoder on "
"keyframe.";
// Unfortunately faking a VP8 frame is hard. Rely on no Decode -> using SW
@ -113,8 +109,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, IsSoftwareFallbackSticky) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
EncodedImage encoded_image;
@ -128,12 +123,11 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest, IsSoftwareFallbackSticky) {
<< "Decoder shouldn't be used after failure.";
// fake_decoder_ should have only been initialized once during the test.
EXPECT_EQ(1, fake_decoder_->init_decode_count_);
EXPECT_EQ(1, fake_decoder_->configure_count_);
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, DoesNotFallbackOnEveryError) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_ERROR;
EncodedImage encoded_image;
EXPECT_EQ(fake_decoder_->decode_return_code_,
@ -146,8 +140,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest, DoesNotFallbackOnEveryError) {
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, UsesHwDecoderAfterReinit) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
EncodedImage encoded_image;
@ -155,7 +148,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest, UsesHwDecoderAfterReinit) {
EXPECT_EQ(1, fake_decoder_->decode_count_);
fallback_wrapper_->Release();
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_OK;
fallback_wrapper_->Decode(encoded_image, false, -1);
@ -164,12 +157,11 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest, UsesHwDecoderAfterReinit) {
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, ForwardsReleaseCall) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fallback_wrapper_->Release();
EXPECT_EQ(1, fake_decoder_->release_count_);
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
EncodedImage encoded_image;
fallback_wrapper_->Decode(encoded_image, false, -1);
@ -197,16 +189,14 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
}
} callback;
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fallback_wrapper_->RegisterDecodeCompleteCallback(&callback);
EXPECT_EQ(&callback, fake_decoder_->decode_complete_callback_);
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
ReportsFallbackImplementationName) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
EncodedImage encoded_image;
@ -219,8 +209,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
}
TEST_F(VideoDecoderSoftwareFallbackWrapperTest, FallbacksOnTooManyErrors) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_ERROR;
EncodedImage encoded_image;
@ -243,8 +232,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest, FallbacksOnTooManyErrors) {
TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
DoesNotFallbackOnDeltaFramesErrors) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
fake_decoder_->decode_return_code_ = WEBRTC_VIDEO_CODEC_ERROR;
EncodedImage encoded_image;
@ -262,8 +250,7 @@ TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
TEST_F(VideoDecoderSoftwareFallbackWrapperTest,
DoesNotFallbacksOnNonConsequtiveErrors) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
fallback_wrapper_->Configure({});
EncodedImage encoded_image;
encoded_image._frameType = VideoFrameType::kVideoFrameKey;
@ -297,21 +284,20 @@ class ForcedSoftwareDecoderFallbackTest
};
TEST_F(ForcedSoftwareDecoderFallbackTest, UsesForcedFallback) {
VideoCodec codec = {};
fallback_wrapper_->InitDecode(&codec, 2);
EXPECT_EQ(1, sw_fallback_decoder_->init_decode_count_);
fallback_wrapper_->Configure({});
EXPECT_EQ(1, sw_fallback_decoder_->configure_count_);
EncodedImage encoded_image;
encoded_image._frameType = VideoFrameType::kVideoFrameKey;
fallback_wrapper_->Decode(encoded_image, false, -1);
EXPECT_EQ(1, sw_fallback_decoder_->init_decode_count_);
EXPECT_EQ(1, sw_fallback_decoder_->configure_count_);
EXPECT_EQ(1, sw_fallback_decoder_->decode_count_);
fallback_wrapper_->Release();
EXPECT_EQ(1, sw_fallback_decoder_->release_count_);
// Only fallback decoder should have been used.
EXPECT_EQ(0, fake_decoder_->init_decode_count_);
EXPECT_EQ(0, fake_decoder_->configure_count_);
EXPECT_EQ(0, fake_decoder_->decode_count_);
EXPECT_EQ(0, fake_decoder_->release_count_);
}

View File

@ -18,7 +18,7 @@
#include "absl/base/macros.h"
#include "api/video/encoded_image.h"
#include "api/video_codecs/video_codec.h"
#include "api/video_codecs/video_decoder.h"
#include "modules/video_coding/include/video_error_codes.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
@ -39,8 +39,7 @@ class VideoDecoderSoftwareFallbackWrapper final : public VideoDecoder {
std::unique_ptr<VideoDecoder> hw_decoder);
~VideoDecoderSoftwareFallbackWrapper() override;
int32_t InitDecode(const VideoCodec* codec_settings,
int32_t number_of_cores) override;
bool Configure(const Settings& settings) override;
int32_t Decode(const EncodedImage& input_image,
bool missing_frames,
@ -58,7 +57,7 @@ class VideoDecoderSoftwareFallbackWrapper final : public VideoDecoder {
bool InitFallbackDecoder();
void UpdateFallbackDecoderHistograms();
int32_t InitHwDecoder();
bool InitHwDecoder();
VideoDecoder& active_decoder() const;
@ -70,8 +69,7 @@ class VideoDecoderSoftwareFallbackWrapper final : public VideoDecoder {
} decoder_type_;
std::unique_ptr<VideoDecoder> hw_decoder_;
VideoCodec codec_settings_;
int32_t number_of_cores_;
Settings decoder_settings_;
const std::unique_ptr<VideoDecoder> fallback_decoder_;
const std::string fallback_implementation_name_;
DecodedImageCallback* callback_;
@ -94,51 +92,39 @@ VideoDecoderSoftwareFallbackWrapper::VideoDecoderSoftwareFallbackWrapper(
VideoDecoderSoftwareFallbackWrapper::~VideoDecoderSoftwareFallbackWrapper() =
default;
int32_t VideoDecoderSoftwareFallbackWrapper::InitDecode(
const VideoCodec* codec_settings,
int32_t number_of_cores) {
codec_settings_ = *codec_settings;
number_of_cores_ = number_of_cores;
bool VideoDecoderSoftwareFallbackWrapper::Configure(const Settings& settings) {
decoder_settings_ = settings;
if (webrtc::field_trial::IsEnabled("WebRTC-Video-ForcedSwDecoderFallback")) {
RTC_LOG(LS_INFO) << "Forced software decoder fallback enabled.";
RTC_DCHECK(decoder_type_ == DecoderType::kNone);
return InitFallbackDecoder() ? WEBRTC_VIDEO_CODEC_OK
: WEBRTC_VIDEO_CODEC_ERROR;
return InitFallbackDecoder();
}
int32_t status = InitHwDecoder();
if (status == WEBRTC_VIDEO_CODEC_OK) {
return WEBRTC_VIDEO_CODEC_OK;
if (InitHwDecoder()) {
return true;
}
RTC_DCHECK(decoder_type_ == DecoderType::kNone);
if (InitFallbackDecoder()) {
return WEBRTC_VIDEO_CODEC_OK;
}
return status;
return InitFallbackDecoder();
}
int32_t VideoDecoderSoftwareFallbackWrapper::InitHwDecoder() {
bool VideoDecoderSoftwareFallbackWrapper::InitHwDecoder() {
RTC_DCHECK(decoder_type_ == DecoderType::kNone);
int32_t status = hw_decoder_->InitDecode(&codec_settings_, number_of_cores_);
if (status != WEBRTC_VIDEO_CODEC_OK) {
return status;
if (!hw_decoder_->Configure(decoder_settings_)) {
return false;
}
decoder_type_ = DecoderType::kHardware;
if (callback_)
hw_decoder_->RegisterDecodeCompleteCallback(callback_);
return status;
return true;
}
bool VideoDecoderSoftwareFallbackWrapper::InitFallbackDecoder() {
RTC_DCHECK(decoder_type_ == DecoderType::kNone ||
decoder_type_ == DecoderType::kHardware);
RTC_LOG(LS_WARNING) << "Decoder falling back to software decoding.";
int32_t status =
fallback_decoder_->InitDecode(&codec_settings_, number_of_cores_);
if (status != WEBRTC_VIDEO_CODEC_OK) {
if (!fallback_decoder_->Configure(decoder_settings_)) {
RTC_LOG(LS_ERROR) << "Failed to initialize software-decoder fallback.";
return false;
}
@ -160,7 +146,7 @@ void VideoDecoderSoftwareFallbackWrapper::UpdateFallbackDecoderHistograms() {
"WebRTC.Video.HardwareDecodedFramesBetweenSoftwareFallbacks.";
// Each histogram needs its own code path for this to work otherwise the
// histogram names will be mixed up by the optimization that takes place.
switch (codec_settings_.codecType) {
switch (decoder_settings_.codec_type()) {
case kVideoCodecGeneric:
RTC_HISTOGRAM_COUNTS_100000(kFallbackHistogramsUmaPrefix + "Generic",
hw_decoded_frames_since_last_fallback_);