blob: 9c8d2be508118d1bd337e0523a9e5c994635a1d9 [file] [log] [blame]
Erik Språng09708512018-03-14 15:16:50 +01001/*
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "call/degraded_call.h"
12
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
Erik Språng09708512018-03-14 15:16:50 +010014#include <utility>
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/location.h"
Erik Språng09708512018-03-14 15:16:50 +010017
18namespace webrtc {
Sebastian Jansson836fee12019-02-08 16:08:10 +010019
Erik Språngc6488192019-08-06 15:54:23 +020020DegradedCall::FakeNetworkPipeOnTaskQueue::FakeNetworkPipeOnTaskQueue(
21 TaskQueueFactory* task_queue_factory,
Sebastian Jansson836fee12019-02-08 16:08:10 +010022 Clock* clock,
Erik Språngeea605d2019-08-12 15:56:51 +020023 std::unique_ptr<NetworkBehaviorInterface> network_behavior)
Erik Språngc6488192019-08-06 15:54:23 +020024 : clock_(clock),
25 task_queue_(task_queue_factory->CreateTaskQueue(
26 "DegradedSendQueue",
27 TaskQueueFactory::Priority::NORMAL)),
Erik Språngeea605d2019-08-12 15:56:51 +020028 pipe_(clock, std::move(network_behavior)) {}
Sebastian Jansson836fee12019-02-08 16:08:10 +010029
Erik Språngc6488192019-08-06 15:54:23 +020030void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtp(
31 const uint8_t* packet,
32 size_t length,
Erik Språngeea605d2019-08-12 15:56:51 +020033 const PacketOptions& options,
34 Transport* transport) {
35 pipe_.SendRtp(packet, length, options, transport);
Erik Språngc6488192019-08-06 15:54:23 +020036 Process();
Sebastian Jansson836fee12019-02-08 16:08:10 +010037}
38
Erik Språngc6488192019-08-06 15:54:23 +020039void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtcp(const uint8_t* packet,
Erik Språngeea605d2019-08-12 15:56:51 +020040 size_t length,
41 Transport* transport) {
42 pipe_.SendRtcp(packet, length, transport);
Erik Språngc6488192019-08-06 15:54:23 +020043 Process();
Sebastian Jansson836fee12019-02-08 16:08:10 +010044}
45
Erik Språngeea605d2019-08-12 15:56:51 +020046void DegradedCall::FakeNetworkPipeOnTaskQueue::AddActiveTransport(
47 Transport* transport) {
48 pipe_.AddActiveTransport(transport);
49}
50
51void DegradedCall::FakeNetworkPipeOnTaskQueue::RemoveActiveTransport(
52 Transport* transport) {
53 pipe_.RemoveActiveTransport(transport);
54}
55
Erik Språngc6488192019-08-06 15:54:23 +020056bool DegradedCall::FakeNetworkPipeOnTaskQueue::Process() {
Sebastian Jansson836fee12019-02-08 16:08:10 +010057 pipe_.Process();
Erik Språngc6488192019-08-06 15:54:23 +020058 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;
Sebastian Jansson836fee12019-02-08 16:08:10 +010081}
82
Erik Språngeea605d2019-08-12 15:56:51 +020083DegradedCall::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
95DegradedCall::FakeNetworkPipeTransportAdapter::
96 ~FakeNetworkPipeTransportAdapter() {
97 network_pipe_->RemoveActiveTransport(real_transport_);
98}
99
100bool 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
121bool 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
Erik Språng09708512018-03-14 15:16:50 +0100128DegradedCall::DegradedCall(
129 std::unique_ptr<Call> call,
Artem Titov75e36472018-10-08 12:28:56 +0200130 absl::optional<BuiltInNetworkBehaviorConfig> send_config,
Erik Språngc6488192019-08-06 15:54:23 +0200131 absl::optional<BuiltInNetworkBehaviorConfig> receive_config,
132 TaskQueueFactory* task_queue_factory)
Erik Språng09708512018-03-14 15:16:50 +0100133 : clock_(Clock::GetRealTimeClock()),
134 call_(std::move(call)),
Erik Språngc6488192019-08-06 15:54:23 +0200135 task_queue_factory_(task_queue_factory),
Erik Språng09708512018-03-14 15:16:50 +0100136 send_config_(send_config),
Erik Språngc6488192019-08-06 15:54:23 +0200137 send_simulated_network_(nullptr),
Erik Språng09708512018-03-14 15:16:50 +0100138 receive_config_(receive_config) {
139 if (receive_config_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200140 auto network = std::make_unique<SimulatedNetwork>(*receive_config_);
Artem Titov3229d652018-08-17 13:00:54 +0200141 receive_simulated_network_ = network.get();
Erik Språng09708512018-03-14 15:16:50 +0100142 receive_pipe_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200143 std::make_unique<webrtc::FakeNetworkPipe>(clock_, std::move(network));
Erik Språng09708512018-03-14 15:16:50 +0100144 receive_pipe_->SetReceiver(call_->Receiver());
145 }
Erik Språngeea605d2019-08-12 15:56:51 +0200146 if (send_config_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200147 auto network = std::make_unique<SimulatedNetwork>(*send_config_);
Erik Språngeea605d2019-08-12 15:56:51 +0200148 send_simulated_network_ = network.get();
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200149 send_pipe_ = std::make_unique<FakeNetworkPipeOnTaskQueue>(
Erik Språngeea605d2019-08-12 15:56:51 +0200150 task_queue_factory_, clock_, std::move(network));
151 }
Erik Språng09708512018-03-14 15:16:50 +0100152}
153
Erik Språngc6488192019-08-06 15:54:23 +0200154DegradedCall::~DegradedCall() = default;
Erik Språng09708512018-03-14 15:16:50 +0100155
156AudioSendStream* DegradedCall::CreateAudioSendStream(
157 const AudioSendStream::Config& config) {
Erik Språngeea605d2019-08-12 15:56:51 +0200158 if (send_config_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200159 auto transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
Erik Språngeea605d2019-08-12 15:56:51 +0200160 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 }
Erik Språng09708512018-03-14 15:16:50 +0100170 return call_->CreateAudioSendStream(config);
171}
172
173void DegradedCall::DestroyAudioSendStream(AudioSendStream* send_stream) {
174 call_->DestroyAudioSendStream(send_stream);
Erik Språngeea605d2019-08-12 15:56:51 +0200175 audio_send_transport_adapters_.erase(send_stream);
Erik Språng09708512018-03-14 15:16:50 +0100176}
177
178AudioReceiveStream* DegradedCall::CreateAudioReceiveStream(
179 const AudioReceiveStream::Config& config) {
180 return call_->CreateAudioReceiveStream(config);
181}
182
183void DegradedCall::DestroyAudioReceiveStream(
184 AudioReceiveStream* receive_stream) {
185 call_->DestroyAudioReceiveStream(receive_stream);
186}
187
188VideoSendStream* DegradedCall::CreateVideoSendStream(
189 VideoSendStream::Config config,
190 VideoEncoderConfig encoder_config) {
Erik Språngeea605d2019-08-12 15:56:51 +0200191 std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter;
192 if (send_config_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200193 transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
Erik Språngeea605d2019-08-12 15:56:51 +0200194 send_pipe_.get(), call_.get(), clock_, config.send_transport);
195 config.send_transport = transport_adapter.get();
Erik Språng09708512018-03-14 15:16:50 +0100196 }
Erik Språngeea605d2019-08-12 15:56:51 +0200197 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;
Erik Språng09708512018-03-14 15:16:50 +0100203}
204
205VideoSendStream* DegradedCall::CreateVideoSendStream(
206 VideoSendStream::Config config,
207 VideoEncoderConfig encoder_config,
208 std::unique_ptr<FecController> fec_controller) {
Erik Språngeea605d2019-08-12 15:56:51 +0200209 std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter;
210 if (send_config_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200211 transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>(
Erik Språngeea605d2019-08-12 15:56:51 +0200212 send_pipe_.get(), call_.get(), clock_, config.send_transport);
213 config.send_transport = transport_adapter.get();
Erik Språng09708512018-03-14 15:16:50 +0100214 }
Erik Språngeea605d2019-08-12 15:56:51 +0200215 VideoSendStream* send_stream = call_->CreateVideoSendStream(
Erik Språng09708512018-03-14 15:16:50 +0100216 std::move(config), std::move(encoder_config), std::move(fec_controller));
Erik Språngeea605d2019-08-12 15:56:51 +0200217 if (send_stream && transport_adapter) {
218 video_send_transport_adapters_[send_stream] = std::move(transport_adapter);
219 }
220 return send_stream;
Erik Språng09708512018-03-14 15:16:50 +0100221}
222
223void DegradedCall::DestroyVideoSendStream(VideoSendStream* send_stream) {
Erik Språngeef09fc2018-03-16 10:47:16 +0100224 call_->DestroyVideoSendStream(send_stream);
Erik Språngeea605d2019-08-12 15:56:51 +0200225 video_send_transport_adapters_.erase(send_stream);
Erik Språng09708512018-03-14 15:16:50 +0100226}
227
228VideoReceiveStream* DegradedCall::CreateVideoReceiveStream(
229 VideoReceiveStream::Config configuration) {
230 return call_->CreateVideoReceiveStream(std::move(configuration));
231}
232
233void DegradedCall::DestroyVideoReceiveStream(
234 VideoReceiveStream* receive_stream) {
235 call_->DestroyVideoReceiveStream(receive_stream);
236}
237
238FlexfecReceiveStream* DegradedCall::CreateFlexfecReceiveStream(
239 const FlexfecReceiveStream::Config& config) {
240 return call_->CreateFlexfecReceiveStream(config);
241}
242
243void DegradedCall::DestroyFlexfecReceiveStream(
244 FlexfecReceiveStream* receive_stream) {
245 call_->DestroyFlexfecReceiveStream(receive_stream);
246}
247
248PacketReceiver* DegradedCall::Receiver() {
249 if (receive_config_) {
250 return this;
251 }
252 return call_->Receiver();
253}
254
255RtpTransportControllerSendInterface*
256DegradedCall::GetTransportControllerSend() {
257 return call_->GetTransportControllerSend();
258}
259
260Call::Stats DegradedCall::GetStats() const {
261 return call_->GetStats();
262}
263
Erik Språng09708512018-03-14 15:16:50 +0100264void DegradedCall::SignalChannelNetworkState(MediaType media,
265 NetworkState state) {
266 call_->SignalChannelNetworkState(media, state);
267}
268
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200269void DegradedCall::OnAudioTransportOverheadChanged(
Erik Språng09708512018-03-14 15:16:50 +0100270 int transport_overhead_per_packet) {
Stefan Holmer64be7fa2018-10-04 15:21:55 +0200271 call_->OnAudioTransportOverheadChanged(transport_overhead_per_packet);
Erik Språng09708512018-03-14 15:16:50 +0100272}
273
274void DegradedCall::OnSentPacket(const rtc::SentPacket& sent_packet) {
275 if (send_config_) {
276 // If we have a degraded send-transport, we have already notified call
277 // about the supposed network send time. Discard the actual network send
278 // time in order to properly fool the BWE.
279 return;
280 }
281 call_->OnSentPacket(sent_packet);
282}
283
Erik Språng09708512018-03-14 15:16:50 +0100284PacketReceiver::DeliveryStatus DegradedCall::DeliverPacket(
285 MediaType media_type,
286 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 11:03:12 +0200287 int64_t packet_time_us) {
288 PacketReceiver::DeliveryStatus status = receive_pipe_->DeliverPacket(
289 media_type, std::move(packet), packet_time_us);
Erik Språng09708512018-03-14 15:16:50 +0100290 // This is not optimal, but there are many places where there are thread
291 // checks that fail if we're not using the worker thread call into this
292 // method. If we want to fix this we probably need a task queue to do handover
Erik Språngeea605d2019-08-12 15:56:51 +0200293 // of all overriden methods, which feels like overkill for the current use
Erik Språng09708512018-03-14 15:16:50 +0100294 // case.
295 // By just having this thread call out via the Process() method we work around
296 // that, with the tradeoff that a non-zero delay may become a little larger
297 // than anticipated at very low packet rates.
298 receive_pipe_->Process();
299 return status;
300}
Erik Språng09708512018-03-14 15:16:50 +0100301} // namespace webrtc