blob: 6d4b2aa211e4ce1e0b0881be042cc604f054c1ea [file] [log] [blame]
zhihuang38ede132017-06-15 12:52:32 -07001/*
2 * Copyright 2017 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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "call/call_factory.h"
zhihuang38ede132017-06-15 12:52:32 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdio.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
zhihuang38ede132017-06-15 12:52:32 -070015#include <memory>
Erik Språng09708512018-03-14 15:16:50 +010016#include <string>
Vojin Ilic504fc192021-05-31 14:02:28 +020017#include <utility>
Erik Språngb5cba85c2022-02-08 12:35:46 +010018#include <vector>
zhihuang38ede132017-06-15 12:52:32 -070019
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020020#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "api/test/simulated_network.h"
Erik Språngb5cba85c2022-02-08 12:35:46 +010022#include "api/units/time_delta.h"
Niels Möller8366e172018-02-14 12:20:13 +010023#include "call/call.h"
Erik Språng09708512018-03-14 15:16:50 +010024#include "call/degraded_call.h"
Vojin Ilic504fc192021-05-31 14:02:28 +020025#include "call/rtp_transport_config.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/checks.h"
Erik Språngb5cba85c2022-02-08 12:35:46 +010027#include "rtc_base/experiments/field_trial_list.h"
28#include "rtc_base/experiments/field_trial_parser.h"
Niels Möller8366e172018-02-14 12:20:13 +010029
zhihuang38ede132017-06-15 12:52:32 -070030namespace webrtc {
Erik Språng09708512018-03-14 15:16:50 +010031namespace {
Erik Språngb5cba85c2022-02-08 12:35:46 +010032using TimeScopedNetworkConfig = DegradedCall::TimeScopedNetworkConfig;
33
Jonas Orelande62c2f22022-03-29 11:04:48 +020034bool ParseConfigParam(const FieldTrialsView& trials,
Erik Språngb5cba85c2022-02-08 12:35:46 +010035 absl::string_view exp_name,
36 int* field) {
37 std::string group = trials.Lookup(exp_name);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -070038 if (group.empty())
Erik Språng09708512018-03-14 15:16:50 +010039 return false;
40
41 return (sscanf(group.c_str(), "%d", field) == 1);
42}
43
Erik Språngb5cba85c2022-02-08 12:35:46 +010044absl::optional<TimeScopedNetworkConfig> ParseDegradationConfig(
Jonas Orelande62c2f22022-03-29 11:04:48 +020045 const FieldTrialsView& trials,
Erik Språng09708512018-03-14 15:16:50 +010046 bool send) {
47 std::string exp_prefix = "WebRTCFakeNetwork";
48 if (send) {
49 exp_prefix += "Send";
50 } else {
51 exp_prefix += "Receive";
52 }
53
Erik Språngb5cba85c2022-02-08 12:35:46 +010054 TimeScopedNetworkConfig config;
Erik Språng09708512018-03-14 15:16:50 +010055 bool configured = false;
56 configured |=
Erik Språngb5cba85c2022-02-08 12:35:46 +010057 ParseConfigParam(trials, exp_prefix + "DelayMs", &config.queue_delay_ms);
58 configured |= ParseConfigParam(trials, exp_prefix + "DelayStdDevMs",
Erik Språng09708512018-03-14 15:16:50 +010059 &config.delay_standard_deviation_ms);
60 int queue_length = 0;
Erik Språngb5cba85c2022-02-08 12:35:46 +010061 if (ParseConfigParam(trials, exp_prefix + "QueueLength", &queue_length)) {
Erik Språng09708512018-03-14 15:16:50 +010062 RTC_CHECK_GE(queue_length, 0);
63 config.queue_length_packets = queue_length;
64 configured = true;
65 }
Erik Språngb5cba85c2022-02-08 12:35:46 +010066 configured |= ParseConfigParam(trials, exp_prefix + "CapacityKbps",
67 &config.link_capacity_kbps);
68 configured |= ParseConfigParam(trials, exp_prefix + "LossPercent",
69 &config.loss_percent);
Erik Språng09708512018-03-14 15:16:50 +010070 int allow_reordering = 0;
Erik Språngb5cba85c2022-02-08 12:35:46 +010071 if (ParseConfigParam(trials, exp_prefix + "AllowReordering",
72 &allow_reordering)) {
Erik Språng09708512018-03-14 15:16:50 +010073 config.allow_reordering = true;
74 configured = true;
75 }
Erik Språngb5cba85c2022-02-08 12:35:46 +010076 configured |= ParseConfigParam(trials, exp_prefix + "AvgBurstLossLength",
Erik Språng09708512018-03-14 15:16:50 +010077 &config.avg_burst_loss_length);
Erik Språngb5cba85c2022-02-08 12:35:46 +010078 return configured ? absl::optional<TimeScopedNetworkConfig>(config)
79 : absl::nullopt;
Erik Språng09708512018-03-14 15:16:50 +010080}
Erik Språngb5cba85c2022-02-08 12:35:46 +010081
82std::vector<TimeScopedNetworkConfig> GetNetworkConfigs(
Jonas Orelande62c2f22022-03-29 11:04:48 +020083 const FieldTrialsView& trials,
Erik Språngb5cba85c2022-02-08 12:35:46 +010084 bool send) {
85 FieldTrialStructList<TimeScopedNetworkConfig> trials_list(
86 {FieldTrialStructMember("queue_length_packets",
87 [](TimeScopedNetworkConfig* p) {
88 // FieldTrialParser does not natively support
89 // size_t type, so use this ugly cast as
90 // workaround.
91 return reinterpret_cast<unsigned*>(
92 &p->queue_length_packets);
93 }),
94 FieldTrialStructMember(
95 "queue_delay_ms",
96 [](TimeScopedNetworkConfig* p) { return &p->queue_delay_ms; }),
97 FieldTrialStructMember("delay_standard_deviation_ms",
98 [](TimeScopedNetworkConfig* p) {
99 return &p->delay_standard_deviation_ms;
100 }),
101 FieldTrialStructMember(
102 "link_capacity_kbps",
103 [](TimeScopedNetworkConfig* p) { return &p->link_capacity_kbps; }),
104 FieldTrialStructMember(
105 "loss_percent",
106 [](TimeScopedNetworkConfig* p) { return &p->loss_percent; }),
107 FieldTrialStructMember(
108 "allow_reordering",
109 [](TimeScopedNetworkConfig* p) { return &p->allow_reordering; }),
110 FieldTrialStructMember("avg_burst_loss_length",
111 [](TimeScopedNetworkConfig* p) {
112 return &p->avg_burst_loss_length;
113 }),
114 FieldTrialStructMember(
115 "packet_overhead",
116 [](TimeScopedNetworkConfig* p) { return &p->packet_overhead; }),
117 FieldTrialStructMember("codel_active_queue_management",
118 [](TimeScopedNetworkConfig* p) {
119 return &p->codel_active_queue_management;
120 }),
121 FieldTrialStructMember(
122 "duration",
123 [](TimeScopedNetworkConfig* p) { return &p->duration; })},
124 {});
125 ParseFieldTrial({&trials_list},
126 trials.Lookup(send ? "WebRTC-FakeNetworkSendConfig"
127 : "WebRTC-FakeNetworkReceiveConfig"));
128 std::vector<TimeScopedNetworkConfig> configs = trials_list.Get();
129 if (configs.empty()) {
130 // Try legacy fallback trials.
131 absl::optional<DegradedCall::TimeScopedNetworkConfig> fallback_config =
132 ParseDegradationConfig(trials, send);
133 if (fallback_config.has_value()) {
134 configs.push_back(*fallback_config);
135 }
136 }
137 return configs;
138}
139
Erik Språng09708512018-03-14 15:16:50 +0100140} // namespace
zhihuang38ede132017-06-15 12:52:32 -0700141
Tommi25c77c12020-05-25 17:44:55 +0200142CallFactory::CallFactory() {
143 call_thread_.Detach();
144}
145
zhihuang38ede132017-06-15 12:52:32 -0700146Call* CallFactory::CreateCall(const Call::Config& config) {
Tommi25c77c12020-05-25 17:44:55 +0200147 RTC_DCHECK_RUN_ON(&call_thread_);
Erik Språngb5cba85c2022-02-08 12:35:46 +0100148 RTC_DCHECK(config.trials);
149
150 std::vector<DegradedCall::TimeScopedNetworkConfig> send_degradation_configs =
151 GetNetworkConfigs(*config.trials, /*send=*/true);
152 std::vector<DegradedCall::TimeScopedNetworkConfig>
153 receive_degradation_configs =
154 GetNetworkConfigs(*config.trials, /*send=*/false);
Erik Språng09708512018-03-14 15:16:50 +0100155
Vojin Ilic504fc192021-05-31 14:02:28 +0200156 RtpTransportConfig transportConfig = config.ExtractTransportConfig();
157
Erik Språngb5cba85c2022-02-08 12:35:46 +0100158 if (!send_degradation_configs.empty() ||
159 !receive_degradation_configs.empty()) {
Vojin Ilic504fc192021-05-31 14:02:28 +0200160 return new DegradedCall(
161 std::unique_ptr<Call>(Call::Create(
162 config, Clock::GetRealTimeClock(),
163 SharedModuleThread::Create(
164 ProcessThread::Create("ModuleProcessThread"), nullptr),
165 config.rtp_transport_controller_send_factory->Create(
166 transportConfig, Clock::GetRealTimeClock(),
167 ProcessThread::Create("PacerThread")))),
Erik Språng42abc132022-02-23 11:34:46 +0100168 send_degradation_configs, receive_degradation_configs);
Erik Språng09708512018-03-14 15:16:50 +0100169 }
170
Tommi25c77c12020-05-25 17:44:55 +0200171 if (!module_thread_) {
Per Kjellander4c50e702020-06-30 14:39:43 +0200172 module_thread_ = SharedModuleThread::Create(
173 ProcessThread::Create("SharedModThread"), [this]() {
174 RTC_DCHECK_RUN_ON(&call_thread_);
175 module_thread_ = nullptr;
176 });
Tommi25c77c12020-05-25 17:44:55 +0200177 }
178
Vojin Ilic504fc192021-05-31 14:02:28 +0200179 return Call::Create(config, Clock::GetRealTimeClock(), module_thread_,
180 config.rtp_transport_controller_send_factory->Create(
181 transportConfig, Clock::GetRealTimeClock(),
182 ProcessThread::Create("PacerThread")));
zhihuang38ede132017-06-15 12:52:32 -0700183}
184
185std::unique_ptr<CallFactoryInterface> CreateCallFactory() {
186 return std::unique_ptr<CallFactoryInterface>(new CallFactory());
187}
188
189} // namespace webrtc