| 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 "absl/strings/string_view.h" |
| 17 | #include "api/sequence_checker.h" |
| 18 | #include "modules/rtp_rtcp/source/rtp_util.h" |
| 19 | #include "rtc_base/thread.h" |
| 20 |
| 21 | namespace webrtc { |
| 22 |
| 23 | DegradedCall::FakeNetworkPipeOnTaskQueue::FakeNetworkPipeOnTaskQueue( |
| 24 | TaskQueueBase* task_queue, |
| 25 | rtc::scoped_refptr<PendingTaskSafetyFlag> call_alive, |
| 26 | Clock* clock, |
| 27 | std::unique_ptr<NetworkBehaviorInterface> network_behavior) |
| 28 | : clock_(clock), |
| 29 | task_queue_(task_queue), |
| 30 | call_alive_(std::move(call_alive)), |
| 31 | pipe_(clock, std::move(network_behavior)) {} |
| 32 |
| 33 | void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtp( |
| 34 | rtc::ArrayView<const uint8_t> packet, |
| 35 | const PacketOptions& options, |
| 36 | Transport* transport) { |
| 37 | pipe_.SendRtp(packet, options, transport); |
| 38 | Process(); |
| 39 | } |
| 40 |
| 41 | void DegradedCall::FakeNetworkPipeOnTaskQueue::SendRtcp( |
| 42 | rtc::ArrayView<const uint8_t> packet, |
| 43 | Transport* transport) { |
| 44 | pipe_.SendRtcp(packet, transport); |
| 45 | Process(); |
| 46 | } |
| 47 |
| 48 | void DegradedCall::FakeNetworkPipeOnTaskQueue::AddActiveTransport( |
| 49 | Transport* transport) { |
| 50 | pipe_.AddActiveTransport(transport); |
| 51 | } |
| 52 |
| 53 | void DegradedCall::FakeNetworkPipeOnTaskQueue::RemoveActiveTransport( |
| 54 | Transport* transport) { |
| 55 | pipe_.RemoveActiveTransport(transport); |
| 56 | } |
| 57 |
| 58 | bool DegradedCall::FakeNetworkPipeOnTaskQueue::Process() { |
| 59 | pipe_.Process(); |
| 60 | auto time_to_next = pipe_.TimeUntilNextProcess(); |
| 61 | if (!time_to_next) { |
| 62 | // Packet was probably sent immediately. |
| 63 | return false; |
| 64 | } |
| 65 |
| 66 | task_queue_->PostTask(SafeTask(call_alive_, [this, time_to_next] { |
| 67 | RTC_DCHECK_RUN_ON(task_queue_); |
| 68 | int64_t next_process_time = *time_to_next + clock_->TimeInMilliseconds(); |
| 69 | if (!next_process_ms_ || next_process_time < *next_process_ms_) { |
| 70 | next_process_ms_ = next_process_time; |
| 71 | task_queue_->PostDelayedHighPrecisionTask( |
| 72 | SafeTask(call_alive_, |
| 73 | [this] { |
| 74 | RTC_DCHECK_RUN_ON(task_queue_); |
| 75 | if (!Process()) { |
| 76 | next_process_ms_.reset(); |
| 77 | } |
| 78 | }), |
| 79 | TimeDelta::Millis(*time_to_next)); |
| 80 | } |
| 81 | })); |
| 82 |
| 83 | return true; |
| 84 | } |
| 85 |
| 86 | DegradedCall::FakeNetworkPipeTransportAdapter::FakeNetworkPipeTransportAdapter( |
| 87 | FakeNetworkPipeOnTaskQueue* fake_network, |
| 88 | Call* call, |
| 89 | Clock* clock, |
| 90 | Transport* real_transport) |
| 91 | : network_pipe_(fake_network), |
| 92 | call_(call), |
| 93 | clock_(clock), |
| 94 | real_transport_(real_transport) { |
| 95 | network_pipe_->AddActiveTransport(real_transport); |
| 96 | } |
| 97 |
| 98 | DegradedCall::FakeNetworkPipeTransportAdapter:: |
| 99 | ~FakeNetworkPipeTransportAdapter() { |
| 100 | network_pipe_->RemoveActiveTransport(real_transport_); |
| 101 | } |
| 102 |
| 103 | bool DegradedCall::FakeNetworkPipeTransportAdapter::SendRtp( |
| 104 | rtc::ArrayView<const uint8_t> packet, |
| 105 | const PacketOptions& options) { |
| 106 | // A call here comes from the RTP stack (probably pacer). We intercept it and |
| 107 | // put it in the fake network pipe instead, but report to Call that is has |
| 108 | // been sent, so that the bandwidth estimator sees the delay we add. |
| 109 | network_pipe_->SendRtp(packet, options, real_transport_); |
| 110 | if (options.packet_id != -1) { |
| 111 | rtc::SentPacket sent_packet; |
| 112 | sent_packet.packet_id = options.packet_id; |
| 113 | sent_packet.send_time_ms = clock_->TimeInMilliseconds(); |
| 114 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 115 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
| 116 | sent_packet.info.packet_size_bytes = packet.size(); |
| 117 | sent_packet.info.packet_type = rtc::PacketType::kData; |
| 118 | call_->OnSentPacket(sent_packet); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 |
| 123 | bool DegradedCall::FakeNetworkPipeTransportAdapter::SendRtcp( |
| 124 | rtc::ArrayView<const uint8_t> packet) { |
| 125 | network_pipe_->SendRtcp(packet, real_transport_); |
| 126 | return true; |
| 127 | } |
| 128 |
| 129 | DegradedCall::DegradedCall( |
| 130 | std::unique_ptr<Call> call, |
| 131 | const std::vector<TimeScopedNetworkConfig>& send_configs, |
| 132 | const std::vector<TimeScopedNetworkConfig>& receive_configs) |
| 133 | : clock_(Clock::GetRealTimeClock()), |
| 134 | call_(std::move(call)), |
| 135 | call_alive_(PendingTaskSafetyFlag::CreateDetached()), |
| 136 | send_config_index_(0), |
| 137 | send_configs_(send_configs), |
| 138 | send_simulated_network_(nullptr), |
| 139 | receive_config_index_(0), |
| 140 | receive_configs_(receive_configs) { |
| 141 | if (!receive_configs_.empty()) { |
| 142 | auto network = std::make_unique<SimulatedNetwork>(receive_configs_[0]); |
| 143 | receive_simulated_network_ = network.get(); |
| 144 | receive_pipe_ = |
| 145 | std::make_unique<webrtc::FakeNetworkPipe>(clock_, std::move(network)); |
| 146 | receive_pipe_->SetReceiver(call_->Receiver()); |
| 147 | if (receive_configs_.size() > 1) { |
| 148 | call_->network_thread()->PostDelayedTask( |
| 149 | SafeTask(call_alive_, [this] { UpdateReceiveNetworkConfig(); }), |
| 150 | receive_configs_[0].duration); |
| 151 | } |
| 152 | } |
| 153 | if (!send_configs_.empty()) { |
| 154 | auto network = std::make_unique<SimulatedNetwork>(send_configs_[0]); |
| 155 | send_simulated_network_ = network.get(); |
| 156 | send_pipe_ = std::make_unique<FakeNetworkPipeOnTaskQueue>( |
| 157 | call_->network_thread(), call_alive_, clock_, std::move(network)); |
| 158 | if (send_configs_.size() > 1) { |
| 159 | call_->network_thread()->PostDelayedTask( |
| 160 | SafeTask(call_alive_, [this] { UpdateSendNetworkConfig(); }), |
| 161 | send_configs_[0].duration); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 |
| 166 | DegradedCall::~DegradedCall() { |
| 167 | RTC_DCHECK_RUN_ON(call_->worker_thread()); |
| 168 | // Thread synchronization is required to call `SetNotAlive`. |
| 169 | // Otherwise, when the `DegradedCall` object is destroyed but |
| 170 | // `SetNotAlive` has not yet been called, |
| 171 | // another Closure guarded by `call_alive_` may be called. |
| 172 | // TODO(https://crbug.com/webrtc/12649): Remove this block-invoke. |
| 173 | static_cast<rtc::Thread*>(call_->network_thread()) |
| 174 | ->BlockingCall( |
| 175 | [flag = std::move(call_alive_)]() mutable { flag->SetNotAlive(); }); |
| 176 | } |
| 177 |
| 178 | AudioSendStream* DegradedCall::CreateAudioSendStream( |
| 179 | const AudioSendStream::Config& config) { |
| 180 | if (!send_configs_.empty()) { |
| 181 | auto transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>( |
| 182 | send_pipe_.get(), call_.get(), clock_, config.send_transport); |
| 183 | AudioSendStream::Config degrade_config = config; |
| 184 | degrade_config.send_transport = transport_adapter.get(); |
| 185 | AudioSendStream* send_stream = call_->CreateAudioSendStream(degrade_config); |
| 186 | if (send_stream) { |
| 187 | audio_send_transport_adapters_[send_stream] = |
| 188 | std::move(transport_adapter); |
| 189 | } |
| 190 | return send_stream; |
| 191 | } |
| 192 | return call_->CreateAudioSendStream(config); |
| 193 | } |
| 194 |
| 195 | void DegradedCall::DestroyAudioSendStream(AudioSendStream* send_stream) { |
| 196 | call_->DestroyAudioSendStream(send_stream); |
| 197 | audio_send_transport_adapters_.erase(send_stream); |
| 198 | } |
| 199 |
| 200 | AudioReceiveStreamInterface* DegradedCall::CreateAudioReceiveStream( |
| 201 | const AudioReceiveStreamInterface::Config& config) { |
| 202 | return call_->CreateAudioReceiveStream(config); |
| 203 | } |
| 204 |
| 205 | void DegradedCall::DestroyAudioReceiveStream( |
| 206 | AudioReceiveStreamInterface* receive_stream) { |
| 207 | call_->DestroyAudioReceiveStream(receive_stream); |
| 208 | } |
| 209 |
| 210 | VideoSendStream* DegradedCall::CreateVideoSendStream( |
| 211 | VideoSendStream::Config config, |
| 212 | VideoEncoderConfig encoder_config) { |
| 213 | std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter; |
| 214 | if (!send_configs_.empty()) { |
| 215 | transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>( |
| 216 | send_pipe_.get(), call_.get(), clock_, config.send_transport); |
| 217 | config.send_transport = transport_adapter.get(); |
| 218 | } |
| 219 | VideoSendStream* send_stream = call_->CreateVideoSendStream( |
| 220 | std::move(config), std::move(encoder_config)); |
| 221 | if (send_stream && transport_adapter) { |
| 222 | video_send_transport_adapters_[send_stream] = std::move(transport_adapter); |
| 223 | } |
| 224 | return send_stream; |
| 225 | } |
| 226 |
| 227 | VideoSendStream* DegradedCall::CreateVideoSendStream( |
| 228 | VideoSendStream::Config config, |
| 229 | VideoEncoderConfig encoder_config, |
| 230 | std::unique_ptr<FecController> fec_controller) { |
| 231 | std::unique_ptr<FakeNetworkPipeTransportAdapter> transport_adapter; |
| 232 | if (!send_configs_.empty()) { |
| 233 | transport_adapter = std::make_unique<FakeNetworkPipeTransportAdapter>( |
| 234 | send_pipe_.get(), call_.get(), clock_, config.send_transport); |
| 235 | config.send_transport = transport_adapter.get(); |
| 236 | } |
| 237 | VideoSendStream* send_stream = call_->CreateVideoSendStream( |
| 238 | std::move(config), std::move(encoder_config), std::move(fec_controller)); |
| 239 | if (send_stream && transport_adapter) { |
| 240 | video_send_transport_adapters_[send_stream] = std::move(transport_adapter); |
| 241 | } |
| 242 | return send_stream; |
| 243 | } |
| 244 |
| 245 | void DegradedCall::DestroyVideoSendStream(VideoSendStream* send_stream) { |
| 246 | call_->DestroyVideoSendStream(send_stream); |
| 247 | video_send_transport_adapters_.erase(send_stream); |
| 248 | } |
| 249 |
| 250 | VideoReceiveStreamInterface* DegradedCall::CreateVideoReceiveStream( |
| 251 | VideoReceiveStreamInterface::Config configuration) { |
| 252 | return call_->CreateVideoReceiveStream(std::move(configuration)); |
| 253 | } |
| 254 |
| 255 | void DegradedCall::DestroyVideoReceiveStream( |
| 256 | VideoReceiveStreamInterface* receive_stream) { |
| 257 | call_->DestroyVideoReceiveStream(receive_stream); |
| 258 | } |
| 259 |
| 260 | FlexfecReceiveStream* DegradedCall::CreateFlexfecReceiveStream( |
| 261 | const FlexfecReceiveStream::Config config) { |
| 262 | return call_->CreateFlexfecReceiveStream(std::move(config)); |
| 263 | } |
| 264 |
| 265 | void DegradedCall::DestroyFlexfecReceiveStream( |
| 266 | FlexfecReceiveStream* receive_stream) { |
| 267 | call_->DestroyFlexfecReceiveStream(receive_stream); |
| 268 | } |
| 269 |
| 270 | void DegradedCall::AddAdaptationResource( |
| 271 | rtc::scoped_refptr<Resource> resource) { |
| 272 | call_->AddAdaptationResource(std::move(resource)); |
| 273 | } |
| 274 |
| 275 | PacketReceiver* DegradedCall::Receiver() { |
| 276 | if (!receive_configs_.empty()) { |
| 277 | return this; |
| 278 | } |
| 279 | return call_->Receiver(); |
| 280 | } |
| 281 |
| 282 | RtpTransportControllerSendInterface* |
| 283 | DegradedCall::GetTransportControllerSend() { |
| 284 | return call_->GetTransportControllerSend(); |
| 285 | } |
| 286 |
| 287 | Call::Stats DegradedCall::GetStats() const { |
| 288 | return call_->GetStats(); |
| 289 | } |
| 290 |
| 291 | const FieldTrialsView& DegradedCall::trials() const { |
| 292 | return call_->trials(); |
| 293 | } |
| 294 |
| 295 | TaskQueueBase* DegradedCall::network_thread() const { |
| 296 | return call_->network_thread(); |
| 297 | } |
| 298 |
| 299 | TaskQueueBase* DegradedCall::worker_thread() const { |
| 300 | return call_->worker_thread(); |
| 301 | } |
| 302 |
| 303 | void DegradedCall::SignalChannelNetworkState(MediaType media, |
| 304 | NetworkState state) { |
| 305 | call_->SignalChannelNetworkState(media, state); |
| 306 | } |
| 307 |
| 308 | void DegradedCall::OnAudioTransportOverheadChanged( |
| 309 | int transport_overhead_per_packet) { |
| 310 | call_->OnAudioTransportOverheadChanged(transport_overhead_per_packet); |
| 311 | } |
| 312 |
| 313 | void DegradedCall::OnLocalSsrcUpdated(AudioReceiveStreamInterface& stream, |
| 314 | uint32_t local_ssrc) { |
| 315 | call_->OnLocalSsrcUpdated(stream, local_ssrc); |
| 316 | } |
| 317 |
| 318 | void DegradedCall::OnLocalSsrcUpdated(VideoReceiveStreamInterface& stream, |
| 319 | uint32_t local_ssrc) { |
| 320 | call_->OnLocalSsrcUpdated(stream, local_ssrc); |
| 321 | } |
| 322 |
| 323 | void DegradedCall::OnLocalSsrcUpdated(FlexfecReceiveStream& stream, |
| 324 | uint32_t local_ssrc) { |
| 325 | call_->OnLocalSsrcUpdated(stream, local_ssrc); |
| 326 | } |
| 327 |
| 328 | void DegradedCall::OnUpdateSyncGroup(AudioReceiveStreamInterface& stream, |
| 329 | absl::string_view sync_group) { |
| 330 | call_->OnUpdateSyncGroup(stream, sync_group); |
| 331 | } |
| 332 |
| 333 | void DegradedCall::OnSentPacket(const rtc::SentPacket& sent_packet) { |
| 334 | if (!send_configs_.empty()) { |
| 335 | // If we have a degraded send-transport, we have already notified call |
| 336 | // about the supposed network send time. Discard the actual network send |
| 337 | // time in order to properly fool the BWE. |
| 338 | return; |
| 339 | } |
| 340 | call_->OnSentPacket(sent_packet); |
| 341 | } |
| 342 |
| 343 | void DegradedCall::DeliverRtpPacket( |
| 344 | MediaType media_type, |
| 345 | RtpPacketReceived packet, |
| 346 | OnUndemuxablePacketHandler undemuxable_packet_handler) { |
| 347 | RTC_DCHECK_RUN_ON(&received_packet_sequence_checker_); |
| 348 | receive_pipe_->DeliverRtpPacket(media_type, std::move(packet), |
| 349 | std::move(undemuxable_packet_handler)); |
| 350 | receive_pipe_->Process(); |
| 351 | } |
| 352 |
| 353 | void DegradedCall::DeliverRtcpPacket(rtc::CopyOnWriteBuffer packet) { |
| 354 | RTC_DCHECK_RUN_ON(&received_packet_sequence_checker_); |
| 355 | receive_pipe_->DeliverRtcpPacket(std::move(packet)); |
| 356 | receive_pipe_->Process(); |
| 357 | } |
| 358 |
| 359 | void DegradedCall::SetClientBitratePreferences( |
| 360 | const webrtc::BitrateSettings& preferences) { |
| 361 | call_->SetClientBitratePreferences(preferences); |
| 362 | } |
| 363 |
| 364 | void DegradedCall::UpdateSendNetworkConfig() { |
| 365 | send_config_index_ = (send_config_index_ + 1) % send_configs_.size(); |
| 366 | send_simulated_network_->SetConfig(send_configs_[send_config_index_]); |
| 367 | call_->network_thread()->PostDelayedTask( |
| 368 | SafeTask(call_alive_, [this] { UpdateSendNetworkConfig(); }), |
| 369 | send_configs_[send_config_index_].duration); |
| 370 | } |
| 371 |
| 372 | void DegradedCall::UpdateReceiveNetworkConfig() { |
| 373 | receive_config_index_ = (receive_config_index_ + 1) % receive_configs_.size(); |
| 374 | receive_simulated_network_->SetConfig( |
| 375 | receive_configs_[receive_config_index_]); |
| 376 | call_->network_thread()->PostDelayedTask( |
| 377 | SafeTask(call_alive_, [this] { UpdateReceiveNetworkConfig(); }), |
| 378 | receive_configs_[receive_config_index_].duration); |
| 379 | } |
| 380 | } // namespace webrtc |
| 381 |