blob: 5dc26e02ca09a4340293ed29eafe0545f3d2202a [file] [log] [blame]
Henrik Boström4c1e7cc2020-06-11 12:26:53 +02001/*
2 * Copyright 2020 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Harald Alvestrandc24a2182022-02-23 13:44:59 +000011#include <stdint.h>
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020012
Harald Alvestrandc24a2182022-02-23 13:44:59 +000013#include <memory>
14#include <string>
15
16#include "absl/types/optional.h"
17#include "api/adaptation/resource.h"
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
19#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000020#include "api/media_stream_interface.h"
21#include "api/peer_connection_interface.h"
22#include "api/rtc_error.h"
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020023#include "api/rtp_parameters.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000024#include "api/rtp_sender_interface.h"
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020025#include "api/scoped_refptr.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000026#include "api/video/video_source_interface.h"
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020027#include "call/adaptation/test/fake_resource.h"
28#include "pc/test/fake_periodic_video_source.h"
29#include "pc/test/fake_periodic_video_track_source.h"
30#include "pc/test/peer_connection_test_wrapper.h"
31#include "rtc_base/checks.h"
32#include "rtc_base/gunit.h"
33#include "rtc_base/ref_counted_object.h"
34#include "rtc_base/thread.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000035#include "rtc_base/time_utils.h"
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020036#include "rtc_base/virtual_socket_server.h"
37#include "test/gtest.h"
38
39namespace webrtc {
40
41const int64_t kDefaultTimeoutMs = 5000;
42
43struct TrackWithPeriodicSource {
44 rtc::scoped_refptr<VideoTrackInterface> track;
45 rtc::scoped_refptr<FakePeriodicVideoTrackSource> periodic_track_source;
46};
47
48// Performs an O/A exchange and waits until the signaling state is stable again.
49void Negotiate(rtc::scoped_refptr<PeerConnectionTestWrapper> caller,
50 rtc::scoped_refptr<PeerConnectionTestWrapper> callee) {
51 // Wire up callbacks and listeners such that a full O/A is performed in
52 // response to CreateOffer().
53 PeerConnectionTestWrapper::Connect(caller.get(), callee.get());
54 caller->CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions());
55 caller->WaitForNegotiation();
56}
57
58TrackWithPeriodicSource CreateTrackWithPeriodicSource(
59 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory) {
60 FakePeriodicVideoSource::Config periodic_track_source_config;
61 periodic_track_source_config.frame_interval_ms = 100;
62 periodic_track_source_config.timestamp_offset_ms = rtc::TimeMillis();
63 rtc::scoped_refptr<FakePeriodicVideoTrackSource> periodic_track_source =
Tommi87f70902021-04-27 14:43:08 +020064 rtc::make_ref_counted<FakePeriodicVideoTrackSource>(
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020065 periodic_track_source_config, /* remote */ false);
66 TrackWithPeriodicSource track_with_source;
67 track_with_source.track =
Niels Möllerafb246b2022-04-20 14:26:50 +020068 factory->CreateVideoTrack("PeriodicTrack", periodic_track_source.get());
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020069 track_with_source.periodic_track_source = periodic_track_source;
70 return track_with_source;
71}
72
73// Triggers overuse and obtains VideoSinkWants. Adaptation processing happens in
74// parallel and this function makes no guarantee that the returnd VideoSinkWants
75// have yet to reflect the overuse signal. Used together with EXPECT_TRUE_WAIT
76// to "spam overuse until a change is observed".
77rtc::VideoSinkWants TriggerOveruseAndGetSinkWants(
78 rtc::scoped_refptr<FakeResource> fake_resource,
79 const FakePeriodicVideoSource& source) {
80 fake_resource->SetUsageState(ResourceUsageState::kOveruse);
81 return source.wants();
82}
83
84class PeerConnectionAdaptationIntegrationTest : public ::testing::Test {
85 public:
86 PeerConnectionAdaptationIntegrationTest()
87 : virtual_socket_server_(),
88 network_thread_(new rtc::Thread(&virtual_socket_server_)),
89 worker_thread_(rtc::Thread::Create()) {
90 RTC_CHECK(network_thread_->Start());
91 RTC_CHECK(worker_thread_->Start());
92 }
93
94 rtc::scoped_refptr<PeerConnectionTestWrapper> CreatePcWrapper(
95 const char* name) {
96 rtc::scoped_refptr<PeerConnectionTestWrapper> pc_wrapper =
Tommi87f70902021-04-27 14:43:08 +020097 rtc::make_ref_counted<PeerConnectionTestWrapper>(
Henrik Boström4c1e7cc2020-06-11 12:26:53 +020098 name, network_thread_.get(), worker_thread_.get());
99 PeerConnectionInterface::RTCConfiguration config;
100 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
101 EXPECT_TRUE(pc_wrapper->CreatePc(config, CreateBuiltinAudioEncoderFactory(),
102 CreateBuiltinAudioDecoderFactory()));
103 return pc_wrapper;
104 }
105
106 protected:
107 rtc::VirtualSocketServer virtual_socket_server_;
108 std::unique_ptr<rtc::Thread> network_thread_;
109 std::unique_ptr<rtc::Thread> worker_thread_;
110};
111
112TEST_F(PeerConnectionAdaptationIntegrationTest,
113 ResouceInjectedAfterNegotiationCausesReductionInResolution) {
114 auto caller_wrapper = CreatePcWrapper("caller");
115 auto caller = caller_wrapper->pc();
116 auto callee_wrapper = CreatePcWrapper("callee");
117
118 // Adding a track and negotiating ensures that a VideoSendStream exists.
119 TrackWithPeriodicSource track_with_source =
120 CreateTrackWithPeriodicSource(caller_wrapper->pc_factory());
121 auto sender = caller->AddTrack(track_with_source.track, {}).value();
122 Negotiate(caller_wrapper, callee_wrapper);
123 // Prefer degrading resolution.
124 auto parameters = sender->GetParameters();
125 parameters.degradation_preference = DegradationPreference::MAINTAIN_FRAMERATE;
126 sender->SetParameters(parameters);
127
128 const auto& source =
129 track_with_source.periodic_track_source->fake_periodic_source();
130 int pixel_count_before_overuse = source.wants().max_pixel_count;
131
132 // Inject a fake resource and spam kOveruse until resolution becomes limited.
133 auto fake_resource = FakeResource::Create("FakeResource");
134 caller->AddAdaptationResource(fake_resource);
135 EXPECT_TRUE_WAIT(
136 TriggerOveruseAndGetSinkWants(fake_resource, source).max_pixel_count <
137 pixel_count_before_overuse,
138 kDefaultTimeoutMs);
139}
140
141TEST_F(PeerConnectionAdaptationIntegrationTest,
142 ResouceInjectedBeforeNegotiationCausesReductionInResolution) {
143 auto caller_wrapper = CreatePcWrapper("caller");
144 auto caller = caller_wrapper->pc();
145 auto callee_wrapper = CreatePcWrapper("callee");
146
147 // Inject a fake resource before adding any tracks or negotiating.
148 auto fake_resource = FakeResource::Create("FakeResource");
149 caller->AddAdaptationResource(fake_resource);
150
151 // Adding a track and negotiating ensures that a VideoSendStream exists.
152 TrackWithPeriodicSource track_with_source =
153 CreateTrackWithPeriodicSource(caller_wrapper->pc_factory());
154 auto sender = caller->AddTrack(track_with_source.track, {}).value();
155 Negotiate(caller_wrapper, callee_wrapper);
156 // Prefer degrading resolution.
157 auto parameters = sender->GetParameters();
158 parameters.degradation_preference = DegradationPreference::MAINTAIN_FRAMERATE;
159 sender->SetParameters(parameters);
160
161 const auto& source =
162 track_with_source.periodic_track_source->fake_periodic_source();
163 int pixel_count_before_overuse = source.wants().max_pixel_count;
164
165 // Spam kOveruse until resolution becomes limited.
166 EXPECT_TRUE_WAIT(
167 TriggerOveruseAndGetSinkWants(fake_resource, source).max_pixel_count <
168 pixel_count_before_overuse,
169 kDefaultTimeoutMs);
170}
171
172} // namespace webrtc