* Copyright (c) 2020 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.
*/
#include "modules/video_coding/svc/scalability_structure_key_svc.h"
#include <vector>
#include "api/array_view.h"
#include "api/transport/rtp/dependency_descriptor.h"
#include "common_video/generic_frame_descriptor/generic_frame_info.h"
#include "modules/video_coding/svc/scalability_structure_test_helpers.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::SizeIs;
TEST(ScalabilityStructureL3T3KeyTest,
SkipingT1FrameOnOneSpatialLayerKeepsStructureValid) {
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
std::vector<GenericFrameInfo> frames;
structure.OnRatesUpdated(EnableTemporalLayers(3, 3));
wrapper.GenerateFrames(2, frames);
EXPECT_THAT(frames, SizeIs(4));
structure.OnRatesUpdated(EnableTemporalLayers(3, 1));
wrapper.GenerateFrames(1, frames);
EXPECT_THAT(frames, SizeIs(5));
structure.OnRatesUpdated(EnableTemporalLayers(3, 3));
wrapper.GenerateFrames(1, frames);
ASSERT_THAT(frames, SizeIs(7));
EXPECT_EQ(frames[0].temporal_id, 0);
EXPECT_EQ(frames[1].temporal_id, 0);
EXPECT_EQ(frames[2].temporal_id, 2);
EXPECT_EQ(frames[3].temporal_id, 2);
EXPECT_EQ(frames[4].temporal_id, 1);
EXPECT_EQ(frames[5].temporal_id, 2);
EXPECT_EQ(frames[6].temporal_id, 2);
EXPECT_TRUE(wrapper.FrameReferencesAreValid(frames));
}
TEST(ScalabilityStructureL3T3KeyTest,
SkipT1FrameByEncoderKeepsReferencesValid) {
std::vector<GenericFrameInfo> frames;
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
wrapper.GenerateFrames(2, frames);
structure.NextFrameConfig(false);
wrapper.GenerateFrames(1, frames);
EXPECT_THAT(frames, SizeIs(9));
EXPECT_TRUE(wrapper.FrameReferencesAreValid(frames));
}
TEST(ScalabilityStructureL3T3KeyTest,
SkippingFrameReusePreviousFrameConfiguration) {
std::vector<GenericFrameInfo> frames;
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
wrapper.GenerateFrames(2, frames);
ASSERT_THAT(frames, SizeIs(6));
ASSERT_EQ(frames[0].temporal_id, 0);
ASSERT_EQ(frames[3].temporal_id, 2);
structure.NextFrameConfig(false);
wrapper.GenerateFrames(2, frames);
ASSERT_THAT(frames, SizeIs(12));
EXPECT_EQ(frames[6].temporal_id, 1);
EXPECT_EQ(frames[9].temporal_id, 2);
}
TEST(ScalabilityStructureL3T3KeyTest, SkippingKeyFrameTriggersNewKeyFrame) {
std::vector<GenericFrameInfo> frames;
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
structure.NextFrameConfig(false);
wrapper.GenerateFrames(2, frames);
ASSERT_THAT(frames, SizeIs(6));
ASSERT_EQ(frames[0].temporal_id, 0);
ASSERT_EQ(frames[3].temporal_id, 2);
EXPECT_TRUE(wrapper.FrameReferencesAreValid(frames));
}
TEST(ScalabilityStructureL3T3KeyTest,
SkippingT2FrameAndDisablingT2LayerProduceT1AsNextFrame) {
std::vector<GenericFrameInfo> frames;
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
wrapper.GenerateFrames(1, frames);
auto config = structure.NextFrameConfig(false);
ASSERT_THAT(config, Not(IsEmpty()));
ASSERT_EQ(config.front().TemporalId(), 2);
structure.OnRatesUpdated(EnableTemporalLayers(2, 2, 2));
config = structure.NextFrameConfig(false);
ASSERT_THAT(config, Not(IsEmpty()));
EXPECT_EQ(config.front().TemporalId(), 1);
}
TEST(ScalabilityStructureL3T3KeyTest, EnableT2LayerWhileProducingT1Frame) {
std::vector<GenericFrameInfo> frames;
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
structure.OnRatesUpdated(EnableTemporalLayers(2, 2, 2));
wrapper.GenerateFrames(1, frames);
ASSERT_THAT(frames, SizeIs(3));
EXPECT_EQ(frames[0].temporal_id, 0);
auto config = structure.NextFrameConfig(false);
ASSERT_THAT(config, Not(IsEmpty()));
ASSERT_EQ(config.front().TemporalId(), 1);
structure.OnRatesUpdated(EnableTemporalLayers(3, 3, 3));
for (auto layer_config : config) {
GenericFrameInfo info = structure.OnEncodeDone(layer_config);
EXPECT_EQ(info.temporal_id, 1);
frames.push_back(info);
}
ASSERT_THAT(frames, SizeIs(6));
wrapper.GenerateFrames(4, frames);
ASSERT_THAT(frames, SizeIs(18));
EXPECT_EQ(frames[6].temporal_id, 2);
EXPECT_EQ(frames[9].temporal_id, 0);
EXPECT_EQ(frames[12].temporal_id, 2);
EXPECT_EQ(frames[15].temporal_id, 1);
EXPECT_TRUE(wrapper.FrameReferencesAreValid(frames));
}
TEST(ScalabilityStructureL3T3KeyTest,
ReenablingSpatialLayerBeforeMissedT0FrameDoesntTriggerAKeyFrame) {
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
std::vector<GenericFrameInfo> frames;
structure.OnRatesUpdated(EnableTemporalLayers(2, 2));
wrapper.GenerateFrames(1, frames);
EXPECT_THAT(frames, SizeIs(2));
structure.OnRatesUpdated(EnableTemporalLayers(2, 0));
wrapper.GenerateFrames(1, frames);
EXPECT_THAT(frames, SizeIs(3));
structure.OnRatesUpdated(EnableTemporalLayers(2, 2));
wrapper.GenerateFrames(1, frames);
EXPECT_THAT(frames, SizeIs(5));
EXPECT_EQ(frames[0].temporal_id, 0);
EXPECT_EQ(frames[1].temporal_id, 0);
EXPECT_EQ(frames[2].temporal_id, 1);
EXPECT_EQ(frames[3].temporal_id, 0);
EXPECT_EQ(frames[4].temporal_id, 0);
EXPECT_THAT(frames[3].frame_diffs, SizeIs(1));
EXPECT_THAT(frames[4].frame_diffs, SizeIs(1));
EXPECT_TRUE(wrapper.FrameReferencesAreValid(frames));
}
TEST(ScalabilityStructureL3T3KeyTest, ReenablingSpatialLayerTriggersKeyFrame) {
ScalabilityStructureL3T3Key structure;
ScalabilityStructureWrapper wrapper(structure);
std::vector<GenericFrameInfo> frames;
structure.OnRatesUpdated(EnableTemporalLayers(2, 2, 2));
wrapper.GenerateFrames(3, frames);
EXPECT_THAT(frames, SizeIs(9));
structure.OnRatesUpdated(EnableTemporalLayers(2, 0, 2));
wrapper.GenerateFrames(2, frames);
EXPECT_THAT(frames, SizeIs(13));
structure.OnRatesUpdated(EnableTemporalLayers(2, 2, 2));
wrapper.GenerateFrames(1, frames);
ASSERT_THAT(frames, SizeIs(16));
EXPECT_EQ(frames[0].temporal_id, 0);
EXPECT_EQ(frames[3].temporal_id, 1);
EXPECT_EQ(frames[6].temporal_id, 0);
EXPECT_EQ(frames[9].spatial_id, 0);
EXPECT_EQ(frames[9].temporal_id, 1);
EXPECT_EQ(frames[10].spatial_id, 2);
EXPECT_EQ(frames[10].temporal_id, 1);
EXPECT_EQ(frames[11].spatial_id, 0);
EXPECT_EQ(frames[11].temporal_id, 0);
EXPECT_EQ(frames[12].spatial_id, 2);
EXPECT_EQ(frames[12].temporal_id, 0);
EXPECT_THAT(frames[13].frame_diffs, IsEmpty());
EXPECT_THAT(frames[14].frame_diffs, ElementsAre(1));
EXPECT_THAT(frames[15].frame_diffs, ElementsAre(1));
EXPECT_EQ(frames[13].temporal_id, 0);
EXPECT_EQ(frames[14].temporal_id, 0);
EXPECT_EQ(frames[15].temporal_id, 0);
auto all_frames = rtc::MakeArrayView(frames.data(), frames.size());
EXPECT_TRUE(wrapper.FrameReferencesAreValid(all_frames.subview(0, 13)));
EXPECT_TRUE(wrapper.FrameReferencesAreValid(all_frames.subview(13)));
}
}
}