Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 11 | #include <stdint.h> |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 12 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <string> |
| 15 | |
| 16 | #include "absl/types/optional.h" |
| 17 | #include "api/adaptation/resource.h" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 18 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 19 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 20 | #include "api/media_stream_interface.h" |
| 21 | #include "api/peer_connection_interface.h" |
| 22 | #include "api/rtc_error.h" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 23 | #include "api/rtp_parameters.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 24 | #include "api/rtp_sender_interface.h" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 25 | #include "api/scoped_refptr.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 26 | #include "api/video/video_source_interface.h" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 27 | #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" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 33 | #include "rtc_base/thread.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 34 | #include "rtc_base/time_utils.h" |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 35 | #include "rtc_base/virtual_socket_server.h" |
| 36 | #include "test/gtest.h" |
| 37 | |
| 38 | namespace webrtc { |
| 39 | |
| 40 | const int64_t kDefaultTimeoutMs = 5000; |
| 41 | |
| 42 | struct TrackWithPeriodicSource { |
| 43 | rtc::scoped_refptr<VideoTrackInterface> track; |
| 44 | rtc::scoped_refptr<FakePeriodicVideoTrackSource> periodic_track_source; |
| 45 | }; |
| 46 | |
| 47 | // Performs an O/A exchange and waits until the signaling state is stable again. |
| 48 | void Negotiate(rtc::scoped_refptr<PeerConnectionTestWrapper> caller, |
| 49 | rtc::scoped_refptr<PeerConnectionTestWrapper> callee) { |
| 50 | // Wire up callbacks and listeners such that a full O/A is performed in |
| 51 | // response to CreateOffer(). |
| 52 | PeerConnectionTestWrapper::Connect(caller.get(), callee.get()); |
| 53 | caller->CreateOffer(PeerConnectionInterface::RTCOfferAnswerOptions()); |
| 54 | caller->WaitForNegotiation(); |
| 55 | } |
| 56 | |
| 57 | TrackWithPeriodicSource CreateTrackWithPeriodicSource( |
| 58 | rtc::scoped_refptr<PeerConnectionFactoryInterface> factory) { |
| 59 | FakePeriodicVideoSource::Config periodic_track_source_config; |
| 60 | periodic_track_source_config.frame_interval_ms = 100; |
| 61 | periodic_track_source_config.timestamp_offset_ms = rtc::TimeMillis(); |
| 62 | rtc::scoped_refptr<FakePeriodicVideoTrackSource> periodic_track_source = |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 63 | rtc::make_ref_counted<FakePeriodicVideoTrackSource>( |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 64 | periodic_track_source_config, /* remote */ false); |
| 65 | TrackWithPeriodicSource track_with_source; |
| 66 | track_with_source.track = |
Niels Möller | afb246b | 2022-04-20 14:26:50 +0200 | [diff] [blame] | 67 | factory->CreateVideoTrack("PeriodicTrack", periodic_track_source.get()); |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 68 | track_with_source.periodic_track_source = periodic_track_source; |
| 69 | return track_with_source; |
| 70 | } |
| 71 | |
| 72 | // Triggers overuse and obtains VideoSinkWants. Adaptation processing happens in |
| 73 | // parallel and this function makes no guarantee that the returnd VideoSinkWants |
| 74 | // have yet to reflect the overuse signal. Used together with EXPECT_TRUE_WAIT |
| 75 | // to "spam overuse until a change is observed". |
| 76 | rtc::VideoSinkWants TriggerOveruseAndGetSinkWants( |
| 77 | rtc::scoped_refptr<FakeResource> fake_resource, |
| 78 | const FakePeriodicVideoSource& source) { |
| 79 | fake_resource->SetUsageState(ResourceUsageState::kOveruse); |
| 80 | return source.wants(); |
| 81 | } |
| 82 | |
| 83 | class PeerConnectionAdaptationIntegrationTest : public ::testing::Test { |
| 84 | public: |
| 85 | PeerConnectionAdaptationIntegrationTest() |
| 86 | : virtual_socket_server_(), |
| 87 | network_thread_(new rtc::Thread(&virtual_socket_server_)), |
| 88 | worker_thread_(rtc::Thread::Create()) { |
| 89 | RTC_CHECK(network_thread_->Start()); |
| 90 | RTC_CHECK(worker_thread_->Start()); |
| 91 | } |
| 92 | |
| 93 | rtc::scoped_refptr<PeerConnectionTestWrapper> CreatePcWrapper( |
| 94 | const char* name) { |
| 95 | rtc::scoped_refptr<PeerConnectionTestWrapper> pc_wrapper = |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 96 | rtc::make_ref_counted<PeerConnectionTestWrapper>( |
Henrik Boström | 4c1e7cc | 2020-06-11 12:26:53 +0200 | [diff] [blame] | 97 | name, network_thread_.get(), worker_thread_.get()); |
| 98 | PeerConnectionInterface::RTCConfiguration config; |
| 99 | config.sdp_semantics = SdpSemantics::kUnifiedPlan; |
| 100 | EXPECT_TRUE(pc_wrapper->CreatePc(config, CreateBuiltinAudioEncoderFactory(), |
| 101 | CreateBuiltinAudioDecoderFactory())); |
| 102 | return pc_wrapper; |
| 103 | } |
| 104 | |
| 105 | protected: |
| 106 | rtc::VirtualSocketServer virtual_socket_server_; |
| 107 | std::unique_ptr<rtc::Thread> network_thread_; |
| 108 | std::unique_ptr<rtc::Thread> worker_thread_; |
| 109 | }; |
| 110 | |
| 111 | TEST_F(PeerConnectionAdaptationIntegrationTest, |
| 112 | ResouceInjectedAfterNegotiationCausesReductionInResolution) { |
| 113 | auto caller_wrapper = CreatePcWrapper("caller"); |
| 114 | auto caller = caller_wrapper->pc(); |
| 115 | auto callee_wrapper = CreatePcWrapper("callee"); |
| 116 | |
| 117 | // Adding a track and negotiating ensures that a VideoSendStream exists. |
| 118 | TrackWithPeriodicSource track_with_source = |
| 119 | CreateTrackWithPeriodicSource(caller_wrapper->pc_factory()); |
| 120 | auto sender = caller->AddTrack(track_with_source.track, {}).value(); |
| 121 | Negotiate(caller_wrapper, callee_wrapper); |
| 122 | // Prefer degrading resolution. |
| 123 | auto parameters = sender->GetParameters(); |
| 124 | parameters.degradation_preference = DegradationPreference::MAINTAIN_FRAMERATE; |
| 125 | sender->SetParameters(parameters); |
| 126 | |
| 127 | const auto& source = |
| 128 | track_with_source.periodic_track_source->fake_periodic_source(); |
| 129 | int pixel_count_before_overuse = source.wants().max_pixel_count; |
| 130 | |
| 131 | // Inject a fake resource and spam kOveruse until resolution becomes limited. |
| 132 | auto fake_resource = FakeResource::Create("FakeResource"); |
| 133 | caller->AddAdaptationResource(fake_resource); |
| 134 | EXPECT_TRUE_WAIT( |
| 135 | TriggerOveruseAndGetSinkWants(fake_resource, source).max_pixel_count < |
| 136 | pixel_count_before_overuse, |
| 137 | kDefaultTimeoutMs); |
| 138 | } |
| 139 | |
| 140 | TEST_F(PeerConnectionAdaptationIntegrationTest, |
| 141 | ResouceInjectedBeforeNegotiationCausesReductionInResolution) { |
| 142 | auto caller_wrapper = CreatePcWrapper("caller"); |
| 143 | auto caller = caller_wrapper->pc(); |
| 144 | auto callee_wrapper = CreatePcWrapper("callee"); |
| 145 | |
| 146 | // Inject a fake resource before adding any tracks or negotiating. |
| 147 | auto fake_resource = FakeResource::Create("FakeResource"); |
| 148 | caller->AddAdaptationResource(fake_resource); |
| 149 | |
| 150 | // Adding a track and negotiating ensures that a VideoSendStream exists. |
| 151 | TrackWithPeriodicSource track_with_source = |
| 152 | CreateTrackWithPeriodicSource(caller_wrapper->pc_factory()); |
| 153 | auto sender = caller->AddTrack(track_with_source.track, {}).value(); |
| 154 | Negotiate(caller_wrapper, callee_wrapper); |
| 155 | // Prefer degrading resolution. |
| 156 | auto parameters = sender->GetParameters(); |
| 157 | parameters.degradation_preference = DegradationPreference::MAINTAIN_FRAMERATE; |
| 158 | sender->SetParameters(parameters); |
| 159 | |
| 160 | const auto& source = |
| 161 | track_with_source.periodic_track_source->fake_periodic_source(); |
| 162 | int pixel_count_before_overuse = source.wants().max_pixel_count; |
| 163 | |
| 164 | // Spam kOveruse until resolution becomes limited. |
| 165 | EXPECT_TRUE_WAIT( |
| 166 | TriggerOveruseAndGetSinkWants(fake_resource, source).max_pixel_count < |
| 167 | pixel_count_before_overuse, |
| 168 | kDefaultTimeoutMs); |
| 169 | } |
| 170 | |
| 171 | } // namespace webrtc |