• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 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/degraded_call.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "rtc_base/location.h"
17 
18 namespace webrtc {
19 
FakeNetworkPipeOnTaskQueue(TaskQueueFactory * task_queue_factory,Clock * clock,std::unique_ptr<NetworkBehaviorInterface> network_behavior)20 DegradedCall::FakeNetworkPipeOnTaskQueue::FakeNetworkPipeOnTaskQueue(
21     TaskQueueFactory* task_queue_factory,
22     Clock* clock,
23     std::unique_ptr<NetworkBehaviorInterface> network_behavior)
24     : clock_(clock),
25       task_queue_(task_queue_factory->CreateTaskQueue(
26           "DegradedSendQueue",
27           TaskQueueFactory::Priority::NORMAL)),
28       pipe_(clock, std::move(network_behavior)) {}
29 
SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options,Transport * transport)30 void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtp(
31     const uint8_t* packet,
32     size_t length,
33     const PacketOptions& options,
34     Transport* transport) {
35   pipe_.SendRtp(packet, length, options, transport);
36   Process();
37 }
38 
SendRtcp(const uint8_t * packet,size_t length,Transport * transport)39 void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtcp(const uint8_t* packet,
40                                                         size_t length,
41                                                         Transport* transport) {
42   pipe_.SendRtcp(packet, length, transport);
43   Process();
44 }
45 
AddActiveTransport(Transport * transport)46 void DegradedCall::FakeNetworkPipeOnTaskQueue::AddActiveTransport(
47     Transport* transport) {
48   pipe_.AddActiveTransport(transport);
49 }
50 
RemoveActiveTransport(Transport * transport)51 void DegradedCall::FakeNetworkPipeOnTaskQueue::RemoveActiveTransport(
52     Transport* transport) {
53   pipe_.RemoveActiveTransport(transport);
54 }
55 
Process()56 bool DegradedCall::FakeNetworkPipeOnTaskQueue::Process() {
57   pipe_.Process();
58   auto time_to_next = pipe_.TimeUntilNextProcess();
59   if (!time_to_next) {
60     // Packet was probably sent immediately.
61     return false;
62   }
63 
64   task_queue_.PostTask([this, time_to_next]() {
65     RTC_DCHECK_RUN_ON(&task_queue_);
66     int64_t next_process_time = *time_to_next + clock_->TimeInMilliseconds();
67     if (!next_process_ms_ || next_process_time < *next_process_ms_) {
68       next_process_ms_ = next_process_time;
69       task_queue_.PostDelayedTask(
70           [this]() {
71             RTC_DCHECK_RUN_ON(&task_queue_);
72             if (!Process()) {
73               next_process_ms_.reset();
74             }
75           },
76           *time_to_next);
77     }
78   });
79 
80   return true;
81 }
82 
FakeNetworkPipeTransportAdapter(FakeNetworkPipeOnTaskQueue * fake_network,Call * call,Clock * clock,Transport * real_transport)83 DegradedCall::FakeNetworkPipeTransportAdapter::FakeNetworkPipeTransportAdapter(
84     FakeNetworkPipeOnTaskQueue* fake_network,
85     Call* call,
86     Clock* clock,
87     Transport* real_transport)
88     : network_pipe_(fake_network),
89       call_(call),
90       clock_(clock),
91       real_transport_(real_transport) {
92   network_pipe_->AddActiveTransport(real_transport);
93 }
94 
95 DegradedCall::FakeNetworkPipeTransportAdapter::
~FakeNetworkPipeTransportAdapter()96     ~FakeNetworkPipeTransportAdapter() {
97   network_pipe_->RemoveActiveTransport(real_transport_);
98 }
99 
SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options)100 bool DegradedCall::FakeNetworkPipeTransportAdapter::SendRtp(
101     const uint8_t* packet,
102     size_t length,
103     const PacketOptions& options) {
104   // A call here comes from the RTP stack (probably pacer). We intercept it and
105   // put it in the fake network pipe instead, but report to Call that is has
106   // been sent, so that the bandwidth estimator sees the delay we add.
107   network_pipe_->SendRtp(packet, length, options, real_transport_);
108   if (options.packet_id != -1) {
109     rtc::SentPacket sent_packet;
110     sent_packet.packet_id = options.packet_id;
111     sent_packet.send_time_ms = clock_->TimeInMilliseconds();
112     sent_packet.info.included_in_feedback = options.included_in_feedback;
113     sent_packet.info.included_in_allocation = options.included_in_allocation;
114     sent_packet.info.packet_size_bytes = length;
115     sent_packet.info.packet_type = rtc::PacketType::kData;
116     call_->OnSentPacket(sent_packet);
117   }
118   return true;
119 }
120 
SendRtcp(const uint8_t * packet,size_t length)121 bool DegradedCall::FakeNetworkPipeTransportAdapter::SendRtcp(
122     const uint8_t* packet,
123     size_t length) {
124   network_pipe_->SendRtcp(packet, length, real_transport_);
125   return true;
126 }
127 
DegradedCall(std::unique_ptr<Call> call,absl::optional<BuiltInNetworkBehaviorConfig> send_config,absl::optional<BuiltInNetworkBehaviorConfig> receive_config,TaskQueueFactory * task_queue_factory)128 DegradedCall::DegradedCall(
129     std::unique_ptr<Call> call,
130     absl::optional<BuiltInNetworkBehaviorConfig> send_config,
131     absl::optional<BuiltInNetworkBehaviorConfig> receive_config,
132     TaskQueueFactory* task_queue_factory)
133     : clock_(Clock::GetRealTimeClock()),
134       call_(std::move(call)),
135       task_queue_factory_(task_queue_factory),
136       send_config_(send_config),
137       send_simulated_network_(nullptr),
138       receive_config_(receive_config) {
139   if (receive_config_) {
140     auto network = std::make_unique<SimulatedNetwork>(*receive_config_);
141     receive_simulated_network_ = network.get();
142     receive_pipe_ =
143         std::make_unique<webrtc::FakeNetworkPipe>(clock_, std::move(network));
144     receive_pipe_->SetReceiver(call_->Receiver());
145   }
146   if (send_config_) {
147     auto network = std::make_unique<SimulatedNetwork>(*send_config_);
148     send_simulated_network_ = network.get();
149     send_pipe_ = std::make_unique<FakeNetworkPipeOnTaskQueue>(
150         task_queue_factory_, clock_, std::move(network));
151   }
152 }
153 
154 DegradedCall::~DegradedCall() = default;
155 
CreateAudioSendStream(const AudioSendStream::Config & config)156 AudioSendStream* DegradedCall::CreateAudioSendStream(
157     const AudioSendStream::Config& config) {
158   if (send_config_) {
159     auto transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
160         send_pipe_.get(), call_.get(), clock_, config.send_transport);
161     AudioSendStream::Config degrade_config = config;
162     degrade_config.send_transport = transport_adapter.get();
163     AudioSendStream* send_stream = call_->CreateAudioSendStream(degrade_config);
164     if (send_stream) {
165       audio_send_transport_adapters_[send_stream] =
166           std::move(transport_adapter);
167     }
168     return send_stream;
169   }
170   return call_->CreateAudioSendStream(config);
171 }
172 
DestroyAudioSendStream(AudioSendStream * send_stream)173 void DegradedCall::DestroyAudioSendStream(AudioSendStream* send_stream) {
174   call_->DestroyAudioSendStream(send_stream);
175   audio_send_transport_adapters_.erase(send_stream);
176 }
177 
CreateAudioReceiveStream(const AudioReceiveStream::Config & config)178 AudioReceiveStream* DegradedCall::CreateAudioReceiveStream(
179     const AudioReceiveStream::Config& config) {
180   return call_->CreateAudioReceiveStream(config);
181 }
182 
DestroyAudioReceiveStream(AudioReceiveStream * receive_stream)183 void DegradedCall::DestroyAudioReceiveStream(
184     AudioReceiveStream* receive_stream) {
185   call_->DestroyAudioReceiveStream(receive_stream);
186 }
187 
CreateVideoSendStream(VideoSendStream::Config config,VideoEncoderConfig encoder_config)188 VideoSendStream* DegradedCall::CreateVideoSendStream(
189     VideoSendStream::Config config,
190     VideoEncoderConfig encoder_config) {
191   std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter;
192   if (send_config_) {
193     transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
194         send_pipe_.get(), call_.get(), clock_, config.send_transport);
195     config.send_transport = transport_adapter.get();
196   }
197   VideoSendStream* send_stream = call_->CreateVideoSendStream(
198       std::move(config), std::move(encoder_config));
199   if (send_stream && transport_adapter) {
200     video_send_transport_adapters_[send_stream] = std::move(transport_adapter);
201   }
202   return send_stream;
203 }
204 
CreateVideoSendStream(VideoSendStream::Config config,VideoEncoderConfig encoder_config,std::unique_ptr<FecController> fec_controller)205 VideoSendStream* DegradedCall::CreateVideoSendStream(
206     VideoSendStream::Config config,
207     VideoEncoderConfig encoder_config,
208     std::unique_ptr<FecController> fec_controller) {
209   std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter;
210   if (send_config_) {
211     transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
212         send_pipe_.get(), call_.get(), clock_, config.send_transport);
213     config.send_transport = transport_adapter.get();
214   }
215   VideoSendStream* send_stream = call_->CreateVideoSendStream(
216       std::move(config), std::move(encoder_config), std::move(fec_controller));
217   if (send_stream && transport_adapter) {
218     video_send_transport_adapters_[send_stream] = std::move(transport_adapter);
219   }
220   return send_stream;
221 }
222 
DestroyVideoSendStream(VideoSendStream * send_stream)223 void DegradedCall::DestroyVideoSendStream(VideoSendStream* send_stream) {
224   call_->DestroyVideoSendStream(send_stream);
225   video_send_transport_adapters_.erase(send_stream);
226 }
227 
CreateVideoReceiveStream(VideoReceiveStream::Config configuration)228 VideoReceiveStream* DegradedCall::CreateVideoReceiveStream(
229     VideoReceiveStream::Config configuration) {
230   return call_->CreateVideoReceiveStream(std::move(configuration));
231 }
232 
DestroyVideoReceiveStream(VideoReceiveStream * receive_stream)233 void DegradedCall::DestroyVideoReceiveStream(
234     VideoReceiveStream* receive_stream) {
235   call_->DestroyVideoReceiveStream(receive_stream);
236 }
237 
CreateFlexfecReceiveStream(const FlexfecReceiveStream::Config & config)238 FlexfecReceiveStream* DegradedCall::CreateFlexfecReceiveStream(
239     const FlexfecReceiveStream::Config& config) {
240   return call_->CreateFlexfecReceiveStream(config);
241 }
242 
DestroyFlexfecReceiveStream(FlexfecReceiveStream * receive_stream)243 void DegradedCall::DestroyFlexfecReceiveStream(
244     FlexfecReceiveStream* receive_stream) {
245   call_->DestroyFlexfecReceiveStream(receive_stream);
246 }
247 
AddAdaptationResource(rtc::scoped_refptr<Resource> resource)248 void DegradedCall::AddAdaptationResource(
249     rtc::scoped_refptr<Resource> resource) {
250   call_->AddAdaptationResource(std::move(resource));
251 }
252 
Receiver()253 PacketReceiver* DegradedCall::Receiver() {
254   if (receive_config_) {
255     return this;
256   }
257   return call_->Receiver();
258 }
259 
260 RtpTransportControllerSendInterface*
GetTransportControllerSend()261 DegradedCall::GetTransportControllerSend() {
262   return call_->GetTransportControllerSend();
263 }
264 
GetStats() const265 Call::Stats DegradedCall::GetStats() const {
266   return call_->GetStats();
267 }
268 
SignalChannelNetworkState(MediaType media,NetworkState state)269 void DegradedCall::SignalChannelNetworkState(MediaType media,
270                                              NetworkState state) {
271   call_->SignalChannelNetworkState(media, state);
272 }
273 
OnAudioTransportOverheadChanged(int transport_overhead_per_packet)274 void DegradedCall::OnAudioTransportOverheadChanged(
275     int transport_overhead_per_packet) {
276   call_->OnAudioTransportOverheadChanged(transport_overhead_per_packet);
277 }
278 
OnSentPacket(const rtc::SentPacket & sent_packet)279 void DegradedCall::OnSentPacket(const rtc::SentPacket& sent_packet) {
280   if (send_config_) {
281     // If we have a degraded send-transport, we have already notified call
282     // about the supposed network send time. Discard the actual network send
283     // time in order to properly fool the BWE.
284     return;
285   }
286   call_->OnSentPacket(sent_packet);
287 }
288 
DeliverPacket(MediaType media_type,rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)289 PacketReceiver::DeliveryStatus DegradedCall::DeliverPacket(
290     MediaType media_type,
291     rtc::CopyOnWriteBuffer packet,
292     int64_t packet_time_us) {
293   PacketReceiver::DeliveryStatus status = receive_pipe_->DeliverPacket(
294       media_type, std::move(packet), packet_time_us);
295   // This is not optimal, but there are many places where there are thread
296   // checks that fail if we're not using the worker thread call into this
297   // method. If we want to fix this we probably need a task queue to do handover
298   // of all overriden methods, which feels like overkill for the current use
299   // case.
300   // By just having this thread call out via the Process() method we work around
301   // that, with the tradeoff that a non-zero delay may become a little larger
302   // than anticipated at very low packet rates.
303   receive_pipe_->Process();
304   return status;
305 }
306 }  // namespace webrtc
307