xref: /aosp_15_r20/external/webrtc/call/call_factory.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "call/call_factory.h"
12 
13 #include <stdio.h>
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/memory/memory.h"
21 #include "absl/types/optional.h"
22 #include "api/test/simulated_network.h"
23 #include "api/units/time_delta.h"
24 #include "call/call.h"
25 #include "call/degraded_call.h"
26 #include "call/rtp_transport_config.h"
27 #include "rtc_base/checks.h"
28 #include "rtc_base/experiments/field_trial_list.h"
29 #include "rtc_base/experiments/field_trial_parser.h"
30 
31 namespace webrtc {
32 namespace {
33 using TimeScopedNetworkConfig = DegradedCall::TimeScopedNetworkConfig;
34 
GetNetworkConfigs(const FieldTrialsView & trials,bool send)35 std::vector<TimeScopedNetworkConfig> GetNetworkConfigs(
36     const FieldTrialsView& trials,
37     bool send) {
38   FieldTrialStructList<TimeScopedNetworkConfig> trials_list(
39       {FieldTrialStructMember("queue_length_packets",
40                               [](TimeScopedNetworkConfig* p) {
41                                 // FieldTrialParser does not natively support
42                                 // size_t type, so use this ugly cast as
43                                 // workaround.
44                                 return reinterpret_cast<unsigned*>(
45                                     &p->queue_length_packets);
46                               }),
47        FieldTrialStructMember(
48            "queue_delay_ms",
49            [](TimeScopedNetworkConfig* p) { return &p->queue_delay_ms; }),
50        FieldTrialStructMember("delay_standard_deviation_ms",
51                               [](TimeScopedNetworkConfig* p) {
52                                 return &p->delay_standard_deviation_ms;
53                               }),
54        FieldTrialStructMember(
55            "link_capacity_kbps",
56            [](TimeScopedNetworkConfig* p) { return &p->link_capacity_kbps; }),
57        FieldTrialStructMember(
58            "loss_percent",
59            [](TimeScopedNetworkConfig* p) { return &p->loss_percent; }),
60        FieldTrialStructMember(
61            "allow_reordering",
62            [](TimeScopedNetworkConfig* p) { return &p->allow_reordering; }),
63        FieldTrialStructMember("avg_burst_loss_length",
64                               [](TimeScopedNetworkConfig* p) {
65                                 return &p->avg_burst_loss_length;
66                               }),
67        FieldTrialStructMember(
68            "packet_overhead",
69            [](TimeScopedNetworkConfig* p) { return &p->packet_overhead; }),
70        FieldTrialStructMember(
71            "duration",
72            [](TimeScopedNetworkConfig* p) { return &p->duration; })},
73       {});
74   ParseFieldTrial({&trials_list},
75                   trials.Lookup(send ? "WebRTC-FakeNetworkSendConfig"
76                                      : "WebRTC-FakeNetworkReceiveConfig"));
77   return trials_list.Get();
78 }
79 
80 }  // namespace
81 
CallFactory()82 CallFactory::CallFactory() {
83   call_thread_.Detach();
84 }
85 
CreateCall(const Call::Config & config)86 Call* CallFactory::CreateCall(const Call::Config& config) {
87   RTC_DCHECK_RUN_ON(&call_thread_);
88   RTC_DCHECK(config.trials);
89 
90   std::vector<DegradedCall::TimeScopedNetworkConfig> send_degradation_configs =
91       GetNetworkConfigs(*config.trials, /*send=*/true);
92   std::vector<DegradedCall::TimeScopedNetworkConfig>
93       receive_degradation_configs =
94           GetNetworkConfigs(*config.trials, /*send=*/false);
95 
96   RtpTransportConfig transportConfig = config.ExtractTransportConfig();
97 
98   Call* call =
99       Call::Create(config, Clock::GetRealTimeClock(),
100                    config.rtp_transport_controller_send_factory->Create(
101                        transportConfig, Clock::GetRealTimeClock()));
102 
103   if (!send_degradation_configs.empty() ||
104       !receive_degradation_configs.empty()) {
105     return new DegradedCall(absl::WrapUnique(call), send_degradation_configs,
106                             receive_degradation_configs);
107   }
108 
109   return call;
110 }
111 
CreateCallFactory()112 std::unique_ptr<CallFactoryInterface> CreateCallFactory() {
113   return std::unique_ptr<CallFactoryInterface>(new CallFactory());
114 }
115 
116 }  // namespace webrtc
117