blob: aeb3cbdaa700919a85b3aa0469f13461aa5ec9d5 [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>
zhihuang38ede132017-06-15 12:52:32 -070018
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020019#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/test/simulated_network.h"
Niels Möller8366e172018-02-14 12:20:13 +010021#include "call/call.h"
Erik Språng09708512018-03-14 15:16:50 +010022#include "call/degraded_call.h"
Vojin Ilic504fc192021-05-31 14:02:28 +020023#include "call/rtp_transport_config.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/checks.h"
Erik Språng09708512018-03-14 15:16:50 +010025#include "system_wrappers/include/field_trial.h"
Niels Möller8366e172018-02-14 12:20:13 +010026
zhihuang38ede132017-06-15 12:52:32 -070027namespace webrtc {
Erik Språng09708512018-03-14 15:16:50 +010028namespace {
29bool ParseConfigParam(std::string exp_name, int* field) {
30 std::string group = field_trial::FindFullName(exp_name);
Benjamin Wright41f9f2c2019-03-13 18:03:29 -070031 if (group.empty())
Erik Språng09708512018-03-14 15:16:50 +010032 return false;
33
34 return (sscanf(group.c_str(), "%d", field) == 1);
35}
36
Artem Titov75e36472018-10-08 12:28:56 +020037absl::optional<webrtc::BuiltInNetworkBehaviorConfig> ParseDegradationConfig(
Erik Språng09708512018-03-14 15:16:50 +010038 bool send) {
39 std::string exp_prefix = "WebRTCFakeNetwork";
40 if (send) {
41 exp_prefix += "Send";
42 } else {
43 exp_prefix += "Receive";
44 }
45
Artem Titov75e36472018-10-08 12:28:56 +020046 webrtc::BuiltInNetworkBehaviorConfig config;
Erik Språng09708512018-03-14 15:16:50 +010047 bool configured = false;
48 configured |=
49 ParseConfigParam(exp_prefix + "DelayMs", &config.queue_delay_ms);
50 configured |= ParseConfigParam(exp_prefix + "DelayStdDevMs",
51 &config.delay_standard_deviation_ms);
52 int queue_length = 0;
53 if (ParseConfigParam(exp_prefix + "QueueLength", &queue_length)) {
54 RTC_CHECK_GE(queue_length, 0);
55 config.queue_length_packets = queue_length;
56 configured = true;
57 }
58 configured |=
59 ParseConfigParam(exp_prefix + "CapacityKbps", &config.link_capacity_kbps);
60 configured |=
61 ParseConfigParam(exp_prefix + "LossPercent", &config.loss_percent);
62 int allow_reordering = 0;
63 if (ParseConfigParam(exp_prefix + "AllowReordering", &allow_reordering)) {
64 config.allow_reordering = true;
65 configured = true;
66 }
67 configured |= ParseConfigParam(exp_prefix + "AvgBurstLossLength",
68 &config.avg_burst_loss_length);
Artem Titov4ff63cc2018-08-23 10:54:54 +020069 return configured
Artem Titov75e36472018-10-08 12:28:56 +020070 ? absl::optional<webrtc::BuiltInNetworkBehaviorConfig>(config)
Artem Titov4ff63cc2018-08-23 10:54:54 +020071 : absl::nullopt;
Erik Språng09708512018-03-14 15:16:50 +010072}
73} // namespace
zhihuang38ede132017-06-15 12:52:32 -070074
Tommi25c77c12020-05-25 17:44:55 +020075CallFactory::CallFactory() {
76 call_thread_.Detach();
77}
78
zhihuang38ede132017-06-15 12:52:32 -070079Call* CallFactory::CreateCall(const Call::Config& config) {
Tommi25c77c12020-05-25 17:44:55 +020080 RTC_DCHECK_RUN_ON(&call_thread_);
Artem Titov75e36472018-10-08 12:28:56 +020081 absl::optional<webrtc::BuiltInNetworkBehaviorConfig> send_degradation_config =
82 ParseDegradationConfig(true);
83 absl::optional<webrtc::BuiltInNetworkBehaviorConfig>
Artem Titov4ff63cc2018-08-23 10:54:54 +020084 receive_degradation_config = ParseDegradationConfig(false);
Erik Språng09708512018-03-14 15:16:50 +010085
Vojin Ilic504fc192021-05-31 14:02:28 +020086 RtpTransportConfig transportConfig = config.ExtractTransportConfig();
87
Erik Språng09708512018-03-14 15:16:50 +010088 if (send_degradation_config || receive_degradation_config) {
Vojin Ilic504fc192021-05-31 14:02:28 +020089 return new DegradedCall(
90 std::unique_ptr<Call>(Call::Create(
91 config, Clock::GetRealTimeClock(),
92 SharedModuleThread::Create(
93 ProcessThread::Create("ModuleProcessThread"), nullptr),
94 config.rtp_transport_controller_send_factory->Create(
95 transportConfig, Clock::GetRealTimeClock(),
96 ProcessThread::Create("PacerThread")))),
97 send_degradation_config, receive_degradation_config,
98 config.task_queue_factory);
Erik Språng09708512018-03-14 15:16:50 +010099 }
100
Tommi25c77c12020-05-25 17:44:55 +0200101 if (!module_thread_) {
Per Kjellander4c50e702020-06-30 14:39:43 +0200102 module_thread_ = SharedModuleThread::Create(
103 ProcessThread::Create("SharedModThread"), [this]() {
104 RTC_DCHECK_RUN_ON(&call_thread_);
105 module_thread_ = nullptr;
106 });
Tommi25c77c12020-05-25 17:44:55 +0200107 }
108
Vojin Ilic504fc192021-05-31 14:02:28 +0200109 return Call::Create(config, Clock::GetRealTimeClock(), module_thread_,
110 config.rtp_transport_controller_send_factory->Create(
111 transportConfig, Clock::GetRealTimeClock(),
112 ProcessThread::Create("PacerThread")));
zhihuang38ede132017-06-15 12:52:32 -0700113}
114
115std::unique_ptr<CallFactoryInterface> CreateCallFactory() {
116 return std::unique_ptr<CallFactoryInterface>(new CallFactory());
117}
118
119} // namespace webrtc