| 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.h" |
| 12 |
| 13 | #include <string.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <atomic> |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <set> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 |
| 23 | #include "absl/functional/bind_front.h" |
| 24 | #include "absl/types/optional.h" |
| 25 | #include "api/rtc_event_log/rtc_event_log.h" |
| 26 | #include "api/sequence_checker.h" |
| 27 | #include "api/transport/network_control.h" |
| 28 | #include "audio/audio_receive_stream.h" |
| 29 | #include "audio/audio_send_stream.h" |
| 30 | #include "audio/audio_state.h" |
| 31 | #include "call/adaptation/broadcast_resource_listener.h" |
| 32 | #include "call/bitrate_allocator.h" |
| 33 | #include "call/flexfec_receive_stream_impl.h" |
| 34 | #include "call/receive_time_calculator.h" |
| 35 | #include "call/rtp_stream_receiver_controller.h" |
| 36 | #include "call/rtp_transport_controller_send.h" |
| 37 | #include "call/rtp_transport_controller_send_factory.h" |
| 38 | #include "call/version.h" |
| 39 | #include "logging/rtc_event_log/events/rtc_event_audio_receive_stream_config.h" |
| 40 | #include "logging/rtc_event_log/events/rtc_event_rtcp_packet_incoming.h" |
| 41 | #include "logging/rtc_event_log/events/rtc_event_rtp_packet_incoming.h" |
| 42 | #include "logging/rtc_event_log/events/rtc_event_video_receive_stream_config.h" |
| 43 | #include "logging/rtc_event_log/events/rtc_event_video_send_stream_config.h" |
| 44 | #include "logging/rtc_event_log/rtc_stream_config.h" |
| 45 | #include "modules/congestion_controller/include/receive_side_congestion_controller.h" |
| 46 | #include "modules/rtp_rtcp/include/flexfec_receiver.h" |
| 47 | #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" |
| 48 | #include "modules/rtp_rtcp/source/byte_io.h" |
| 49 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
| 50 | #include "modules/rtp_rtcp/source/rtp_util.h" |
| 51 | #include "modules/utility/include/process_thread.h" |
| 52 | #include "modules/video_coding/fec_controller_default.h" |
| 53 | #include "rtc_base/checks.h" |
| 54 | #include "rtc_base/constructor_magic.h" |
| 55 | #include "rtc_base/location.h" |
| 56 | #include "rtc_base/logging.h" |
| 57 | #include "rtc_base/strings/string_builder.h" |
| 58 | #include "rtc_base/system/no_unique_address.h" |
| 59 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 60 | #include "rtc_base/thread_annotations.h" |
| 61 | #include "rtc_base/time_utils.h" |
| 62 | #include "rtc_base/trace_event.h" |
| 63 | #include "system_wrappers/include/clock.h" |
| 64 | #include "system_wrappers/include/cpu_info.h" |
| 65 | #include "system_wrappers/include/field_trial.h" |
| 66 | #include "system_wrappers/include/metrics.h" |
| 67 | #include "video/call_stats2.h" |
| 68 | #include "video/send_delay_stats.h" |
| 69 | #include "video/stats_counter.h" |
| 70 | #include "video/video_receive_stream2.h" |
| 71 | #include "video/video_send_stream.h" |
| 72 |
| 73 | namespace webrtc { |
| 74 |
| 75 | namespace { |
| 76 | bool SendPeriodicFeedback(const std::vector<RtpExtension>& extensions) { |
| 77 | for (const auto& extension : extensions) { |
| 78 | if (extension.uri == RtpExtension::kTransportSequenceNumberV2Uri) |
| 79 | return false; |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 |
| 84 | bool UseSendSideBwe(const ReceiveStream::RtpConfig& rtp) { |
| 85 | if (!rtp.transport_cc) |
| 86 | return false; |
| 87 | for (const auto& extension : rtp.extensions) { |
| 88 | if (extension.uri == RtpExtension::kTransportSequenceNumberUri || |
| 89 | extension.uri == RtpExtension::kTransportSequenceNumberV2Uri) |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | } |
| 94 |
| 95 | const int* FindKeyByValue(const std::map<int, int>& m, int v) { |
| 96 | for (const auto& kv : m) { |
| 97 | if (kv.second == v) |
| 98 | return &kv.first; |
| 99 | } |
| 100 | return nullptr; |
| 101 | } |
| 102 |
| 103 | std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig( |
| 104 | const VideoReceiveStream::Config& config) { |
| 105 | auto rtclog_config = std::make_unique<rtclog::StreamConfig>(); |
| 106 | rtclog_config->remote_ssrc = config.rtp.remote_ssrc; |
| 107 | rtclog_config->local_ssrc = config.rtp.local_ssrc; |
| 108 | rtclog_config->rtx_ssrc = config.rtp.rtx_ssrc; |
| 109 | rtclog_config->rtcp_mode = config.rtp.rtcp_mode; |
| 110 | rtclog_config->rtp_extensions = config.rtp.extensions; |
| 111 |
| 112 | for (const auto& d : config.decoders) { |
| 113 | const int* search = |
| 114 | FindKeyByValue(config.rtp.rtx_associated_payload_types, d.payload_type); |
| 115 | rtclog_config->codecs.emplace_back(d.video_format.name, d.payload_type, |
| 116 | search ? *search : 0); |
| 117 | } |
| 118 | return rtclog_config; |
| 119 | } |
| 120 |
| 121 | std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig( |
| 122 | const VideoSendStream::Config& config, |
| 123 | size_t ssrc_index) { |
| 124 | auto rtclog_config = std::make_unique<rtclog::StreamConfig>(); |
| 125 | rtclog_config->local_ssrc = config.rtp.ssrcs[ssrc_index]; |
| 126 | if (ssrc_index < config.rtp.rtx.ssrcs.size()) { |
| 127 | rtclog_config->rtx_ssrc = config.rtp.rtx.ssrcs[ssrc_index]; |
| 128 | } |
| 129 | rtclog_config->rtcp_mode = config.rtp.rtcp_mode; |
| 130 | rtclog_config->rtp_extensions = config.rtp.extensions; |
| 131 |
| 132 | rtclog_config->codecs.emplace_back(config.rtp.payload_name, |
| 133 | config.rtp.payload_type, |
| 134 | config.rtp.rtx.payload_type); |
| 135 | return rtclog_config; |
| 136 | } |
| 137 |
| 138 | std::unique_ptr<rtclog::StreamConfig> CreateRtcLogStreamConfig( |
| 139 | const AudioReceiveStream::Config& config) { |
| 140 | auto rtclog_config = std::make_unique<rtclog::StreamConfig>(); |
| 141 | rtclog_config->remote_ssrc = config.rtp.remote_ssrc; |
| 142 | rtclog_config->local_ssrc = config.rtp.local_ssrc; |
| 143 | rtclog_config->rtp_extensions = config.rtp.extensions; |
| 144 | return rtclog_config; |
| 145 | } |
| 146 |
| 147 | TaskQueueBase* GetCurrentTaskQueueOrThread() { |
| 148 | TaskQueueBase* current = TaskQueueBase::Current(); |
| 149 | if (!current) |
| 150 | current = rtc::ThreadManager::Instance()->CurrentThread(); |
| 151 | return current; |
| 152 | } |
| 153 |
| 154 | } // namespace |
| 155 |
| 156 | namespace internal { |
| 157 |
| 158 | // Wraps an injected resource in a BroadcastResourceListener and handles adding |
| 159 | // and removing adapter resources to individual VideoSendStreams. |
| 160 | class ResourceVideoSendStreamForwarder { |
| 161 | public: |
| 162 | ResourceVideoSendStreamForwarder( |
| 163 | rtc::scoped_refptr<webrtc::Resource> resource) |
| 164 | : broadcast_resource_listener_(resource) { |
| 165 | broadcast_resource_listener_.StartListening(); |
| 166 | } |
| 167 | ~ResourceVideoSendStreamForwarder() { |
| 168 | RTC_DCHECK(adapter_resources_.empty()); |
| 169 | broadcast_resource_listener_.StopListening(); |
| 170 | } |
| 171 |
| 172 | rtc::scoped_refptr<webrtc::Resource> Resource() const { |
| 173 | return broadcast_resource_listener_.SourceResource(); |
| 174 | } |
| 175 |
| 176 | void OnCreateVideoSendStream(VideoSendStream* video_send_stream) { |
| 177 | RTC_DCHECK(adapter_resources_.find(video_send_stream) == |
| 178 | adapter_resources_.end()); |
| 179 | auto adapter_resource = |
| 180 | broadcast_resource_listener_.CreateAdapterResource(); |
| 181 | video_send_stream->AddAdaptationResource(adapter_resource); |
| 182 | adapter_resources_.insert( |
| 183 | std::make_pair(video_send_stream, adapter_resource)); |
| 184 | } |
| 185 |
| 186 | void OnDestroyVideoSendStream(VideoSendStream* video_send_stream) { |
| 187 | auto it = adapter_resources_.find(video_send_stream); |
| 188 | RTC_DCHECK(it != adapter_resources_.end()); |
| 189 | broadcast_resource_listener_.RemoveAdapterResource(it->second); |
| 190 | adapter_resources_.erase(it); |
| 191 | } |
| 192 |
| 193 | private: |
| 194 | BroadcastResourceListener broadcast_resource_listener_; |
| 195 | std::map<VideoSendStream*, rtc::scoped_refptr<webrtc::Resource>> |
| 196 | adapter_resources_; |
| 197 | }; |
| 198 |
| 199 | class Call final : public webrtc::Call, |
| 200 | public PacketReceiver, |
| 201 | public RecoveredPacketReceiver, |
| 202 | public TargetTransferRateObserver, |
| 203 | public BitrateAllocator::LimitObserver { |
| 204 | public: |
| 205 | Call(Clock* clock, |
| 206 | const Call::Config& config, |
| 207 | std::unique_ptr<RtpTransportControllerSendInterface> transport_send, |
| 208 | rtc::scoped_refptr<SharedModuleThread> module_process_thread, |
| 209 | TaskQueueFactory* task_queue_factory); |
| 210 | ~Call() override; |
| 211 |
| 212 | // Implements webrtc::Call. |
| 213 | PacketReceiver* Receiver() override; |
| 214 |
| 215 | webrtc::AudioSendStream* CreateAudioSendStream( |
| 216 | const webrtc::AudioSendStream::Config& config) override; |
| 217 | void DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) override; |
| 218 |
| 219 | webrtc::AudioReceiveStream* CreateAudioReceiveStream( |
| 220 | const webrtc::AudioReceiveStream::Config& config) override; |
| 221 | void DestroyAudioReceiveStream( |
| 222 | webrtc::AudioReceiveStream* receive_stream) override; |
| 223 |
| 224 | webrtc::VideoSendStream* CreateVideoSendStream( |
| 225 | webrtc::VideoSendStream::Config config, |
| 226 | VideoEncoderConfig encoder_config) override; |
| 227 | webrtc::VideoSendStream* CreateVideoSendStream( |
| 228 | webrtc::VideoSendStream::Config config, |
| 229 | VideoEncoderConfig encoder_config, |
| 230 | std::unique_ptr<FecController> fec_controller) override; |
| 231 | void DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) override; |
| 232 |
| 233 | webrtc::VideoReceiveStream* CreateVideoReceiveStream( |
| 234 | webrtc::VideoReceiveStream::Config configuration) override; |
| 235 | void DestroyVideoReceiveStream( |
| 236 | webrtc::VideoReceiveStream* receive_stream) override; |
| 237 |
| 238 | FlexfecReceiveStream* CreateFlexfecReceiveStream( |
| 239 | const FlexfecReceiveStream::Config& config) override; |
| 240 | void DestroyFlexfecReceiveStream( |
| 241 | FlexfecReceiveStream* receive_stream) override; |
| 242 |
| 243 | void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) override; |
| 244 |
| 245 | RtpTransportControllerSendInterface* GetTransportControllerSend() override; |
| 246 |
| 247 | Stats GetStats() const override; |
| 248 |
| 249 | const WebRtcKeyValueConfig& trials() const override; |
| 250 |
| 251 | TaskQueueBase* network_thread() const override; |
| 252 | TaskQueueBase* worker_thread() const override; |
| 253 |
| 254 | // Implements PacketReceiver. |
| 255 | DeliveryStatus DeliverPacket(MediaType media_type, |
| 256 | rtc::CopyOnWriteBuffer packet, |
| 257 | int64_t packet_time_us) override; |
| 258 |
| 259 | // Implements RecoveredPacketReceiver. |
| 260 | void OnRecoveredPacket(const uint8_t* packet, size_t length) override; |
| 261 |
| 262 | void SignalChannelNetworkState(MediaType media, NetworkState state) override; |
| 263 |
| 264 | void OnAudioTransportOverheadChanged( |
| 265 | int transport_overhead_per_packet) override; |
| 266 |
| 267 | void OnLocalSsrcUpdated(webrtc::AudioReceiveStream& stream, |
| 268 | uint32_t local_ssrc) override; |
| 269 |
| 270 | void OnUpdateSyncGroup(webrtc::AudioReceiveStream& stream, |
| 271 | const std::string& sync_group) override; |
| 272 |
| 273 | void OnSentPacket(const rtc::SentPacket& sent_packet) override; |
| 274 |
| 275 | // Implements TargetTransferRateObserver, |
| 276 | void OnTargetTransferRate(TargetTransferRate msg) override; |
| 277 | void OnStartRateUpdate(DataRate start_rate) override; |
| 278 |
| 279 | // Implements BitrateAllocator::LimitObserver. |
| 280 | void OnAllocationLimitsChanged(BitrateAllocationLimits limits) override; |
| 281 |
| 282 | void SetClientBitratePreferences(const BitrateSettings& preferences) override; |
| 283 |
| 284 | private: |
| 285 | // Thread-compatible class that collects received packet stats and exposes |
| 286 | // them as UMA histograms on destruction. |
| 287 | class ReceiveStats { |
| 288 | public: |
| 289 | explicit ReceiveStats(Clock* clock); |
| 290 | ~ReceiveStats(); |
| 291 |
| 292 | void AddReceivedRtcpBytes(int bytes); |
| 293 | void AddReceivedAudioBytes(int bytes, webrtc::Timestamp arrival_time); |
| 294 | void AddReceivedVideoBytes(int bytes, webrtc::Timestamp arrival_time); |
| 295 |
| 296 | private: |
| 297 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
| 298 | RateCounter received_bytes_per_second_counter_ |
| 299 | RTC_GUARDED_BY(sequence_checker_); |
| 300 | RateCounter received_audio_bytes_per_second_counter_ |
| 301 | RTC_GUARDED_BY(sequence_checker_); |
| 302 | RateCounter received_video_bytes_per_second_counter_ |
| 303 | RTC_GUARDED_BY(sequence_checker_); |
| 304 | RateCounter received_rtcp_bytes_per_second_counter_ |
| 305 | RTC_GUARDED_BY(sequence_checker_); |
| 306 | absl::optional<Timestamp> first_received_rtp_audio_timestamp_ |
| 307 | RTC_GUARDED_BY(sequence_checker_); |
| 308 | absl::optional<Timestamp> last_received_rtp_audio_timestamp_ |
| 309 | RTC_GUARDED_BY(sequence_checker_); |
| 310 | absl::optional<Timestamp> first_received_rtp_video_timestamp_ |
| 311 | RTC_GUARDED_BY(sequence_checker_); |
| 312 | absl::optional<Timestamp> last_received_rtp_video_timestamp_ |
| 313 | RTC_GUARDED_BY(sequence_checker_); |
| 314 | }; |
| 315 |
| 316 | // Thread-compatible class that collects sent packet stats and exposes |
| 317 | // them as UMA histograms on destruction, provided SetFirstPacketTime was |
| 318 | // called with a non-empty packet timestamp before the destructor. |
| 319 | class SendStats { |
| 320 | public: |
| 321 | explicit SendStats(Clock* clock); |
| 322 | ~SendStats(); |
| 323 |
| 324 | void SetFirstPacketTime(absl::optional<Timestamp> first_sent_packet_time); |
| 325 | void PauseSendAndPacerBitrateCounters(); |
| 326 | void AddTargetBitrateSample(uint32_t target_bitrate_bps); |
| 327 | void SetMinAllocatableRate(BitrateAllocationLimits limits); |
| 328 |
| 329 | private: |
| 330 | RTC_NO_UNIQUE_ADDRESS SequenceChecker destructor_sequence_checker_; |
| 331 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
| 332 | Clock* const clock_ RTC_GUARDED_BY(destructor_sequence_checker_); |
| 333 | AvgCounter estimated_send_bitrate_kbps_counter_ |
| 334 | RTC_GUARDED_BY(sequence_checker_); |
| 335 | AvgCounter pacer_bitrate_kbps_counter_ RTC_GUARDED_BY(sequence_checker_); |
| 336 | uint32_t min_allocated_send_bitrate_bps_ RTC_GUARDED_BY(sequence_checker_){ |
| 337 | 0}; |
| 338 | absl::optional<Timestamp> first_sent_packet_time_ |
| 339 | RTC_GUARDED_BY(destructor_sequence_checker_); |
| 340 | }; |
| 341 |
| 342 | void DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) |
| 343 | RTC_RUN_ON(network_thread_); |
| 344 | DeliveryStatus DeliverRtp(MediaType media_type, |
| 345 | rtc::CopyOnWriteBuffer packet, |
| 346 | int64_t packet_time_us) RTC_RUN_ON(worker_thread_); |
| 347 | void ConfigureSync(const std::string& sync_group) RTC_RUN_ON(worker_thread_); |
| 348 |
| 349 | void NotifyBweOfReceivedPacket(const RtpPacketReceived& packet, |
| 350 | MediaType media_type) |
| 351 | RTC_RUN_ON(worker_thread_); |
| 352 |
| 353 | void UpdateAggregateNetworkState(); |
| 354 |
| 355 | // Ensure that necessary process threads are started, and any required |
| 356 | // callbacks have been registered. |
| 357 | void EnsureStarted() RTC_RUN_ON(worker_thread_); |
| 358 |
| 359 | Clock* const clock_; |
| 360 | TaskQueueFactory* const task_queue_factory_; |
| 361 | TaskQueueBase* const worker_thread_; |
| 362 | TaskQueueBase* const network_thread_; |
| 363 | RTC_NO_UNIQUE_ADDRESS SequenceChecker send_transport_sequence_checker_; |
| 364 |
| 365 | const int num_cpu_cores_; |
| 366 | const rtc::scoped_refptr<SharedModuleThread> module_process_thread_; |
| 367 | const std::unique_ptr<CallStats> call_stats_; |
| 368 | const std::unique_ptr<BitrateAllocator> bitrate_allocator_; |
| 369 | const Call::Config config_ RTC_GUARDED_BY(worker_thread_); |
| 370 | // Maps to config_.trials, can be used from any thread via `trials()`. |
| 371 | const WebRtcKeyValueConfig& trials_; |
| 372 |
| 373 | NetworkState audio_network_state_ RTC_GUARDED_BY(worker_thread_); |
| 374 | NetworkState video_network_state_ RTC_GUARDED_BY(worker_thread_); |
| 375 | // TODO(bugs.webrtc.org/11993): Move aggregate_network_up_ over to the |
| 376 | // network thread. |
| 377 | bool aggregate_network_up_ RTC_GUARDED_BY(worker_thread_); |
| 378 |
| 379 | // Schedules nack periodic processing on behalf of all streams. |
| 380 | NackPeriodicProcessor nack_periodic_processor_; |
| 381 |
| 382 | // Audio, Video, and FlexFEC receive streams are owned by the client that |
| 383 | // creates them. |
| 384 | // TODO(bugs.webrtc.org/11993): Move audio_receive_streams_, |
| 385 | // video_receive_streams_ and sync_stream_mapping_ over to the network thread. |
| 386 | std::set<AudioReceiveStream*> audio_receive_streams_ |
| 387 | RTC_GUARDED_BY(worker_thread_); |
| 388 | std::set<VideoReceiveStream2*> video_receive_streams_ |
| 389 | RTC_GUARDED_BY(worker_thread_); |
| 390 | std::map<std::string, AudioReceiveStream*> sync_stream_mapping_ |
| 391 | RTC_GUARDED_BY(worker_thread_); |
| 392 |
| 393 | // TODO(nisse): Should eventually be injected at creation, |
| 394 | // with a single object in the bundled case. |
| 395 | RtpStreamReceiverController audio_receiver_controller_ |
| 396 | RTC_GUARDED_BY(worker_thread_); |
| 397 | RtpStreamReceiverController video_receiver_controller_ |
| 398 | RTC_GUARDED_BY(worker_thread_); |
| 399 |
| 400 | // This extra map is used for receive processing which is |
| 401 | // independent of media type. |
| 402 |
| 403 | // TODO(bugs.webrtc.org/11993): Move receive_rtp_config_ over to the |
| 404 | // network thread. |
| 405 | std::map<uint32_t, ReceiveStream*> receive_rtp_config_ |
| 406 | RTC_GUARDED_BY(worker_thread_); |
| 407 |
| 408 | // Audio and Video send streams are owned by the client that creates them. |
| 409 | std::map<uint32_t, AudioSendStream*> audio_send_ssrcs_ |
| 410 | RTC_GUARDED_BY(worker_thread_); |
| 411 | std::map<uint32_t, VideoSendStream*> video_send_ssrcs_ |
| 412 | RTC_GUARDED_BY(worker_thread_); |
| 413 | std::set<VideoSendStream*> video_send_streams_ RTC_GUARDED_BY(worker_thread_); |
| 414 | // True if `video_send_streams_` is empty, false if not. The atomic variable |
| 415 | // is used to decide UMA send statistics behavior and enables avoiding a |
| 416 | // PostTask(). |
| 417 | std::atomic<bool> video_send_streams_empty_{true}; |
| 418 |
| 419 | // Each forwarder wraps an adaptation resource that was added to the call. |
| 420 | std::vector<std::unique_ptr<ResourceVideoSendStreamForwarder>> |
| 421 | adaptation_resource_forwarders_ RTC_GUARDED_BY(worker_thread_); |
| 422 |
| 423 | using RtpStateMap = std::map<uint32_t, RtpState>; |
| 424 | RtpStateMap suspended_audio_send_ssrcs_ RTC_GUARDED_BY(worker_thread_); |
| 425 | RtpStateMap suspended_video_send_ssrcs_ RTC_GUARDED_BY(worker_thread_); |
| 426 |
| 427 | using RtpPayloadStateMap = std::map<uint32_t, RtpPayloadState>; |
| 428 | RtpPayloadStateMap suspended_video_payload_states_ |
| 429 | RTC_GUARDED_BY(worker_thread_); |
| 430 |
| 431 | webrtc::RtcEventLog* const event_log_; |
| 432 |
| 433 | // TODO(bugs.webrtc.org/11993) ready to move stats access to the network |
| 434 | // thread. |
| 435 | ReceiveStats receive_stats_ RTC_GUARDED_BY(worker_thread_); |
| 436 | SendStats send_stats_ RTC_GUARDED_BY(send_transport_sequence_checker_); |
| 437 | // `last_bandwidth_bps_` and `configured_max_padding_bitrate_bps_` being |
| 438 | // atomic avoids a PostTask. The variables are used for stats gathering. |
| 439 | std::atomic<uint32_t> last_bandwidth_bps_{0}; |
| 440 | std::atomic<uint32_t> configured_max_padding_bitrate_bps_{0}; |
| 441 |
| 442 | ReceiveSideCongestionController receive_side_cc_; |
| 443 |
| 444 | const std::unique_ptr<ReceiveTimeCalculator> receive_time_calculator_; |
| 445 |
| 446 | const std::unique_ptr<SendDelayStats> video_send_delay_stats_; |
| 447 | const Timestamp start_of_call_; |
| 448 |
| 449 | // Note that `task_safety_` needs to be at a greater scope than the task queue |
| 450 | // owned by `transport_send_` since calls might arrive on the network thread |
| 451 | // while Call is being deleted and the task queue is being torn down. |
| 452 | const ScopedTaskSafety task_safety_; |
| 453 |
| 454 | // Caches transport_send_.get(), to avoid racing with destructor. |
| 455 | // Note that this is declared before transport_send_ to ensure that it is not |
| 456 | // invalidated until no more tasks can be running on the transport_send_ task |
| 457 | // queue. |
| 458 | // For more details on the background of this member variable, see: |
| 459 | // https://webrtc-review.googlesource.com/c/src/+/63023/9/call/call.cc |
| 460 | // https://bugs.chromium.org/p/chromium/issues/detail?id=992640 |
| 461 | RtpTransportControllerSendInterface* const transport_send_ptr_ |
| 462 | RTC_GUARDED_BY(send_transport_sequence_checker_); |
| 463 | // Declared last since it will issue callbacks from a task queue. Declaring it |
| 464 | // last ensures that it is destroyed first and any running tasks are finished. |
| 465 | const std::unique_ptr<RtpTransportControllerSendInterface> transport_send_; |
| 466 |
| 467 | bool is_started_ RTC_GUARDED_BY(worker_thread_) = false; |
| 468 |
| 469 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sent_packet_sequence_checker_; |
| 470 | absl::optional<rtc::SentPacket> last_sent_packet_ |
| 471 | RTC_GUARDED_BY(sent_packet_sequence_checker_); |
| 472 |
| 473 | RTC_DISALLOW_COPY_AND_ASSIGN(Call); |
| 474 | }; |
| 475 | } // namespace internal |
| 476 |
| 477 | std::string Call::Stats::ToString(int64_t time_ms) const { |
| 478 | char buf[1024]; |
| 479 | rtc::SimpleStringBuilder ss(buf); |
| 480 | ss << "Call stats: " << time_ms << ", {"; |
| 481 | ss << "send_bw_bps: " << send_bandwidth_bps << ", "; |
| 482 | ss << "recv_bw_bps: " << recv_bandwidth_bps << ", "; |
| 483 | ss << "max_pad_bps: " << max_padding_bitrate_bps << ", "; |
| 484 | ss << "pacer_delay_ms: " << pacer_delay_ms << ", "; |
| 485 | ss << "rtt_ms: " << rtt_ms; |
| 486 | ss << '}'; |
| 487 | return ss.str(); |
| 488 | } |
| 489 |
| 490 | Call* Call::Create(const Call::Config& config) { |
| 491 | rtc::scoped_refptr<SharedModuleThread> call_thread = |
| 492 | SharedModuleThread::Create(ProcessThread::Create("ModuleProcessThread"), |
| 493 | nullptr); |
| 494 | return Create(config, Clock::GetRealTimeClock(), std::move(call_thread), |
| 495 | ProcessThread::Create("PacerThread")); |
| 496 | } |
| 497 |
| 498 | Call* Call::Create(const Call::Config& config, |
| 499 | Clock* clock, |
| 500 | rtc::scoped_refptr<SharedModuleThread> call_thread, |
| 501 | std::unique_ptr<ProcessThread> pacer_thread) { |
| 502 | RTC_DCHECK(config.task_queue_factory); |
| 503 |
| 504 | RtpTransportControllerSendFactory transport_controller_factory_; |
| 505 |
| 506 | RtpTransportConfig transportConfig = config.ExtractTransportConfig(); |
| 507 |
| 508 | return new internal::Call( |
| 509 | clock, config, |
| 510 | transport_controller_factory_.Create(transportConfig, clock, |
| 511 | std::move(pacer_thread)), |
| 512 | std::move(call_thread), config.task_queue_factory); |
| 513 | } |
| 514 |
| 515 | Call* Call::Create(const Call::Config& config, |
| 516 | Clock* clock, |
| 517 | rtc::scoped_refptr<SharedModuleThread> call_thread, |
| 518 | std::unique_ptr<RtpTransportControllerSendInterface> |
| 519 | transportControllerSend) { |
| 520 | RTC_DCHECK(config.task_queue_factory); |
| 521 | return new internal::Call(clock, config, std::move(transportControllerSend), |
| 522 | std::move(call_thread), config.task_queue_factory); |
| 523 | } |
| 524 |
| 525 | class SharedModuleThread::Impl { |
| 526 | public: |
| 527 | Impl(std::unique_ptr<ProcessThread> process_thread, |
| 528 | std::function<void()> on_one_ref_remaining) |
| 529 | : module_thread_(std::move(process_thread)), |
| 530 | on_one_ref_remaining_(std::move(on_one_ref_remaining)) {} |
| 531 |
| 532 | void EnsureStarted() { |
| 533 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 534 | if (started_) |
| 535 | return; |
| 536 | started_ = true; |
| 537 | module_thread_->Start(); |
| 538 | } |
| 539 |
| 540 | ProcessThread* process_thread() { |
| 541 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 542 | return module_thread_.get(); |
| 543 | } |
| 544 |
| 545 | void AddRef() const { |
| 546 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 547 | ++ref_count_; |
| 548 | } |
| 549 |
| 550 | rtc::RefCountReleaseStatus Release() const { |
| 551 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 552 | --ref_count_; |
| 553 |
| 554 | if (ref_count_ == 0) { |
| 555 | module_thread_->Stop(); |
| 556 | return rtc::RefCountReleaseStatus::kDroppedLastRef; |
| 557 | } |
| 558 |
| 559 | if (ref_count_ == 1 && on_one_ref_remaining_) { |
| 560 | auto moved_fn = std::move(on_one_ref_remaining_); |
| 561 | // NOTE: after this function returns, chances are that `this` has been |
| 562 | // deleted - do not touch any member variables. |
| 563 | // If the owner of the last reference implements a lambda that releases |
| 564 | // that last reference inside of the callback (which is legal according |
| 565 | // to this implementation), we will recursively enter Release() above, |
| 566 | // call Stop() and release the last reference. |
| 567 | moved_fn(); |
| 568 | } |
| 569 |
| 570 | return rtc::RefCountReleaseStatus::kOtherRefsRemained; |
| 571 | } |
| 572 |
| 573 | private: |
| 574 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_checker_; |
| 575 | mutable int ref_count_ RTC_GUARDED_BY(sequence_checker_) = 0; |
| 576 | std::unique_ptr<ProcessThread> const module_thread_; |
| 577 | std::function<void()> const on_one_ref_remaining_; |
| 578 | bool started_ = false; |
| 579 | }; |
| 580 |
| 581 | SharedModuleThread::SharedModuleThread( |
| 582 | std::unique_ptr<ProcessThread> process_thread, |
| 583 | std::function<void()> on_one_ref_remaining) |
| 584 | : impl_(std::make_unique<Impl>(std::move(process_thread), |
| 585 | std::move(on_one_ref_remaining))) {} |
| 586 |
| 587 | SharedModuleThread::~SharedModuleThread() = default; |
| 588 |
| 589 | // static |
| 590 |
| 591 | rtc::scoped_refptr<SharedModuleThread> SharedModuleThread::Create( |
| 592 | std::unique_ptr<ProcessThread> process_thread, |
| 593 | std::function<void()> on_one_ref_remaining) { |
| 594 | return new SharedModuleThread(std::move(process_thread), |
| 595 | std::move(on_one_ref_remaining)); |
| 596 | } |
| 597 |
| 598 | void SharedModuleThread::EnsureStarted() { |
| 599 | impl_->EnsureStarted(); |
| 600 | } |
| 601 |
| 602 | ProcessThread* SharedModuleThread::process_thread() { |
| 603 | return impl_->process_thread(); |
| 604 | } |
| 605 |
| 606 | void SharedModuleThread::AddRef() const { |
| 607 | impl_->AddRef(); |
| 608 | } |
| 609 |
| 610 | rtc::RefCountReleaseStatus SharedModuleThread::Release() const { |
| 611 | auto ret = impl_->Release(); |
| 612 | if (ret == rtc::RefCountReleaseStatus::kDroppedLastRef) |
| 613 | delete this; |
| 614 | return ret; |
| 615 | } |
| 616 |
| 617 | // This method here to avoid subclasses has to implement this method. |
| 618 | // Call perf test will use Internal::Call::CreateVideoSendStream() to inject |
| 619 | // FecController. |
| 620 | VideoSendStream* Call::CreateVideoSendStream( |
| 621 | VideoSendStream::Config config, |
| 622 | VideoEncoderConfig encoder_config, |
| 623 | std::unique_ptr<FecController> fec_controller) { |
| 624 | return nullptr; |
| 625 | } |
| 626 |
| 627 | namespace internal { |
| 628 |
| 629 | Call::ReceiveStats::ReceiveStats(Clock* clock) |
| 630 | : received_bytes_per_second_counter_(clock, nullptr, false), |
| 631 | received_audio_bytes_per_second_counter_(clock, nullptr, false), |
| 632 | received_video_bytes_per_second_counter_(clock, nullptr, false), |
| 633 | received_rtcp_bytes_per_second_counter_(clock, nullptr, false) { |
| 634 | sequence_checker_.Detach(); |
| 635 | } |
| 636 |
| 637 | void Call::ReceiveStats::AddReceivedRtcpBytes(int bytes) { |
| 638 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 639 | if (received_bytes_per_second_counter_.HasSample()) { |
| 640 | // First RTP packet has been received. |
| 641 | received_bytes_per_second_counter_.Add(static_cast<int>(bytes)); |
| 642 | received_rtcp_bytes_per_second_counter_.Add(static_cast<int>(bytes)); |
| 643 | } |
| 644 | } |
| 645 |
| 646 | void Call::ReceiveStats::AddReceivedAudioBytes(int bytes, |
| 647 | webrtc::Timestamp arrival_time) { |
| 648 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 649 | received_bytes_per_second_counter_.Add(bytes); |
| 650 | received_audio_bytes_per_second_counter_.Add(bytes); |
| 651 | if (!first_received_rtp_audio_timestamp_) |
| 652 | first_received_rtp_audio_timestamp_ = arrival_time; |
| 653 | last_received_rtp_audio_timestamp_ = arrival_time; |
| 654 | } |
| 655 |
| 656 | void Call::ReceiveStats::AddReceivedVideoBytes(int bytes, |
| 657 | webrtc::Timestamp arrival_time) { |
| 658 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 659 | received_bytes_per_second_counter_.Add(bytes); |
| 660 | received_video_bytes_per_second_counter_.Add(bytes); |
| 661 | if (!first_received_rtp_video_timestamp_) |
| 662 | first_received_rtp_video_timestamp_ = arrival_time; |
| 663 | last_received_rtp_video_timestamp_ = arrival_time; |
| 664 | } |
| 665 |
| 666 | Call::ReceiveStats::~ReceiveStats() { |
| 667 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 668 | if (first_received_rtp_audio_timestamp_) { |
| 669 | RTC_HISTOGRAM_COUNTS_100000( |
| 670 | "WebRTC.Call.TimeReceivingAudioRtpPacketsInSeconds", |
| 671 | (*last_received_rtp_audio_timestamp_ - |
| 672 | *first_received_rtp_audio_timestamp_) |
| 673 | .seconds()); |
| 674 | } |
| 675 | if (first_received_rtp_video_timestamp_) { |
| 676 | RTC_HISTOGRAM_COUNTS_100000( |
| 677 | "WebRTC.Call.TimeReceivingVideoRtpPacketsInSeconds", |
| 678 | (*last_received_rtp_video_timestamp_ - |
| 679 | *first_received_rtp_video_timestamp_) |
| 680 | .seconds()); |
| 681 | } |
| 682 | const int kMinRequiredPeriodicSamples = 5; |
| 683 | AggregatedStats video_bytes_per_sec = |
| 684 | received_video_bytes_per_second_counter_.GetStats(); |
| 685 | if (video_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) { |
| 686 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.VideoBitrateReceivedInKbps", |
| 687 | video_bytes_per_sec.average * 8 / 1000); |
| 688 | RTC_LOG(LS_INFO) << "WebRTC.Call.VideoBitrateReceivedInBps, " |
| 689 | << video_bytes_per_sec.ToStringWithMultiplier(8); |
| 690 | } |
| 691 | AggregatedStats audio_bytes_per_sec = |
| 692 | received_audio_bytes_per_second_counter_.GetStats(); |
| 693 | if (audio_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) { |
| 694 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.AudioBitrateReceivedInKbps", |
| 695 | audio_bytes_per_sec.average * 8 / 1000); |
| 696 | RTC_LOG(LS_INFO) << "WebRTC.Call.AudioBitrateReceivedInBps, " |
| 697 | << audio_bytes_per_sec.ToStringWithMultiplier(8); |
| 698 | } |
| 699 | AggregatedStats rtcp_bytes_per_sec = |
| 700 | received_rtcp_bytes_per_second_counter_.GetStats(); |
| 701 | if (rtcp_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) { |
| 702 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.RtcpBitrateReceivedInBps", |
| 703 | rtcp_bytes_per_sec.average * 8); |
| 704 | RTC_LOG(LS_INFO) << "WebRTC.Call.RtcpBitrateReceivedInBps, " |
| 705 | << rtcp_bytes_per_sec.ToStringWithMultiplier(8); |
| 706 | } |
| 707 | AggregatedStats recv_bytes_per_sec = |
| 708 | received_bytes_per_second_counter_.GetStats(); |
| 709 | if (recv_bytes_per_sec.num_samples > kMinRequiredPeriodicSamples) { |
| 710 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.BitrateReceivedInKbps", |
| 711 | recv_bytes_per_sec.average * 8 / 1000); |
| 712 | RTC_LOG(LS_INFO) << "WebRTC.Call.BitrateReceivedInBps, " |
| 713 | << recv_bytes_per_sec.ToStringWithMultiplier(8); |
| 714 | } |
| 715 | } |
| 716 |
| 717 | Call::SendStats::SendStats(Clock* clock) |
| 718 | : clock_(clock), |
| 719 | estimated_send_bitrate_kbps_counter_(clock, nullptr, true), |
| 720 | pacer_bitrate_kbps_counter_(clock, nullptr, true) { |
| 721 | destructor_sequence_checker_.Detach(); |
| 722 | sequence_checker_.Detach(); |
| 723 | } |
| 724 |
| 725 | Call::SendStats::~SendStats() { |
| 726 | RTC_DCHECK_RUN_ON(&destructor_sequence_checker_); |
| 727 | if (!first_sent_packet_time_) |
| 728 | return; |
| 729 |
| 730 | TimeDelta elapsed = clock_->CurrentTime() - *first_sent_packet_time_; |
| 731 | if (elapsed.seconds() < metrics::kMinRunTimeInSeconds) |
| 732 | return; |
| 733 |
| 734 | const int kMinRequiredPeriodicSamples = 5; |
| 735 | AggregatedStats send_bitrate_stats = |
| 736 | estimated_send_bitrate_kbps_counter_.ProcessAndGetStats(); |
| 737 | if (send_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) { |
| 738 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.EstimatedSendBitrateInKbps", |
| 739 | send_bitrate_stats.average); |
| 740 | RTC_LOG(LS_INFO) << "WebRTC.Call.EstimatedSendBitrateInKbps, " |
| 741 | << send_bitrate_stats.ToString(); |
| 742 | } |
| 743 | AggregatedStats pacer_bitrate_stats = |
| 744 | pacer_bitrate_kbps_counter_.ProcessAndGetStats(); |
| 745 | if (pacer_bitrate_stats.num_samples > kMinRequiredPeriodicSamples) { |
| 746 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Call.PacerBitrateInKbps", |
| 747 | pacer_bitrate_stats.average); |
| 748 | RTC_LOG(LS_INFO) << "WebRTC.Call.PacerBitrateInKbps, " |
| 749 | << pacer_bitrate_stats.ToString(); |
| 750 | } |
| 751 | } |
| 752 |
| 753 | void Call::SendStats::SetFirstPacketTime( |
| 754 | absl::optional<Timestamp> first_sent_packet_time) { |
| 755 | RTC_DCHECK_RUN_ON(&destructor_sequence_checker_); |
| 756 | first_sent_packet_time_ = first_sent_packet_time; |
| 757 | } |
| 758 |
| 759 | void Call::SendStats::PauseSendAndPacerBitrateCounters() { |
| 760 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 761 | estimated_send_bitrate_kbps_counter_.ProcessAndPause(); |
| 762 | pacer_bitrate_kbps_counter_.ProcessAndPause(); |
| 763 | } |
| 764 |
| 765 | void Call::SendStats::AddTargetBitrateSample(uint32_t target_bitrate_bps) { |
| 766 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 767 | estimated_send_bitrate_kbps_counter_.Add(target_bitrate_bps / 1000); |
| 768 | // Pacer bitrate may be higher than bitrate estimate if enforcing min |
| 769 | // bitrate. |
| 770 | uint32_t pacer_bitrate_bps = |
| 771 | std::max(target_bitrate_bps, min_allocated_send_bitrate_bps_); |
| 772 | pacer_bitrate_kbps_counter_.Add(pacer_bitrate_bps / 1000); |
| 773 | } |
| 774 |
| 775 | void Call::SendStats::SetMinAllocatableRate(BitrateAllocationLimits limits) { |
| 776 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 777 | min_allocated_send_bitrate_bps_ = limits.min_allocatable_rate.bps(); |
| 778 | } |
| 779 |
| 780 | Call::Call(Clock* clock, |
| 781 | const Call::Config& config, |
| 782 | std::unique_ptr<RtpTransportControllerSendInterface> transport_send, |
| 783 | rtc::scoped_refptr<SharedModuleThread> module_process_thread, |
| 784 | TaskQueueFactory* task_queue_factory) |
| 785 | : clock_(clock), |
| 786 | task_queue_factory_(task_queue_factory), |
| 787 | worker_thread_(GetCurrentTaskQueueOrThread()), |
| 788 | // If `network_task_queue_` was set to nullptr, network related calls |
| 789 | // must be made on `worker_thread_` (i.e. they're one and the same). |
| 790 | network_thread_(config.network_task_queue_ ? config.network_task_queue_ |
| 791 | : worker_thread_), |
| 792 | num_cpu_cores_(CpuInfo::DetectNumberOfCores()), |
| 793 | module_process_thread_(std::move(module_process_thread)), |
| 794 | call_stats_(new CallStats(clock_, worker_thread_)), |
| 795 | bitrate_allocator_(new BitrateAllocator(this)), |
| 796 | config_(config), |
| 797 | trials_(*config.trials), |
| 798 | audio_network_state_(kNetworkDown), |
| 799 | video_network_state_(kNetworkDown), |
| 800 | aggregate_network_up_(false), |
| 801 | event_log_(config.event_log), |
| 802 | receive_stats_(clock_), |
| 803 | send_stats_(clock_), |
| 804 | receive_side_cc_(clock, |
| 805 | absl::bind_front(&PacketRouter::SendCombinedRtcpPacket, |
| 806 | transport_send->packet_router()), |
| 807 | absl::bind_front(&PacketRouter::SendRemb, |
| 808 | transport_send->packet_router()), |
| 809 | /*network_state_estimator=*/nullptr), |
| 810 | receive_time_calculator_(ReceiveTimeCalculator::CreateFromFieldTrial()), |
| 811 | video_send_delay_stats_(new SendDelayStats(clock_)), |
| 812 | start_of_call_(clock_->CurrentTime()), |
| 813 | transport_send_ptr_(transport_send.get()), |
| 814 | transport_send_(std::move(transport_send)) { |
| 815 | RTC_DCHECK(config.event_log != nullptr); |
| 816 | RTC_DCHECK(config.trials != nullptr); |
| 817 | RTC_DCHECK(network_thread_); |
| 818 | RTC_DCHECK(worker_thread_->IsCurrent()); |
| 819 |
| 820 | send_transport_sequence_checker_.Detach(); |
| 821 | sent_packet_sequence_checker_.Detach(); |
| 822 |
| 823 | // Do not remove this call; it is here to convince the compiler that the |
| 824 | // WebRTC source timestamp string needs to be in the final binary. |
| 825 | LoadWebRTCVersionInRegister(); |
| 826 |
| 827 | call_stats_->RegisterStatsObserver(&receive_side_cc_); |
| 828 |
| 829 | module_process_thread_->process_thread()->RegisterModule( |
| 830 | receive_side_cc_.GetRemoteBitrateEstimator(true), RTC_FROM_HERE); |
| 831 | module_process_thread_->process_thread()->RegisterModule(&receive_side_cc_, |
| 832 | RTC_FROM_HERE); |
| 833 | } |
| 834 |
| 835 | Call::~Call() { |
| 836 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 837 |
| 838 | RTC_CHECK(audio_send_ssrcs_.empty()); |
| 839 | RTC_CHECK(video_send_ssrcs_.empty()); |
| 840 | RTC_CHECK(video_send_streams_.empty()); |
| 841 | RTC_CHECK(audio_receive_streams_.empty()); |
| 842 | RTC_CHECK(video_receive_streams_.empty()); |
| 843 |
| 844 | module_process_thread_->process_thread()->DeRegisterModule( |
| 845 | receive_side_cc_.GetRemoteBitrateEstimator(true)); |
| 846 | module_process_thread_->process_thread()->DeRegisterModule(&receive_side_cc_); |
| 847 | call_stats_->DeregisterStatsObserver(&receive_side_cc_); |
| 848 | send_stats_.SetFirstPacketTime(transport_send_->GetFirstPacketTime()); |
| 849 |
| 850 | RTC_HISTOGRAM_COUNTS_100000( |
| 851 | "WebRTC.Call.LifetimeInSeconds", |
| 852 | (clock_->CurrentTime() - start_of_call_).seconds()); |
| 853 | } |
| 854 |
| 855 | void Call::EnsureStarted() { |
| 856 | if (is_started_) { |
| 857 | return; |
| 858 | } |
| 859 | is_started_ = true; |
| 860 |
| 861 | call_stats_->EnsureStarted(); |
| 862 |
| 863 | // This call seems to kick off a number of things, so probably better left |
| 864 | // off being kicked off on request rather than in the ctor. |
| 865 | transport_send_->RegisterTargetTransferRateObserver(this); |
| 866 |
| 867 | module_process_thread_->EnsureStarted(); |
| 868 | transport_send_->EnsureStarted(); |
| 869 | } |
| 870 |
| 871 | void Call::SetClientBitratePreferences(const BitrateSettings& preferences) { |
| 872 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 873 | GetTransportControllerSend()->SetClientBitratePreferences(preferences); |
| 874 | } |
| 875 |
| 876 | PacketReceiver* Call::Receiver() { |
| 877 | return this; |
| 878 | } |
| 879 |
| 880 | webrtc::AudioSendStream* Call::CreateAudioSendStream( |
| 881 | const webrtc::AudioSendStream::Config& config) { |
| 882 | TRACE_EVENT0("webrtc", "Call::CreateAudioSendStream"); |
| 883 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 884 |
| 885 | EnsureStarted(); |
| 886 |
| 887 | // Stream config is logged in AudioSendStream::ConfigureStream, as it may |
| 888 | // change during the stream's lifetime. |
| 889 | absl::optional<RtpState> suspended_rtp_state; |
| 890 | { |
| 891 | const auto& iter = suspended_audio_send_ssrcs_.find(config.rtp.ssrc); |
| 892 | if (iter != suspended_audio_send_ssrcs_.end()) { |
| 893 | suspended_rtp_state.emplace(iter->second); |
| 894 | } |
| 895 | } |
| 896 |
| 897 | AudioSendStream* send_stream = new AudioSendStream( |
| 898 | clock_, config, config_.audio_state, task_queue_factory_, |
| 899 | transport_send_.get(), bitrate_allocator_.get(), event_log_, |
| 900 | call_stats_->AsRtcpRttStats(), suspended_rtp_state); |
| 901 | RTC_DCHECK(audio_send_ssrcs_.find(config.rtp.ssrc) == |
| 902 | audio_send_ssrcs_.end()); |
| 903 | audio_send_ssrcs_[config.rtp.ssrc] = send_stream; |
| 904 |
| 905 | // TODO(bugs.webrtc.org/11993): call AssociateSendStream and |
| 906 | // UpdateAggregateNetworkState asynchronously on the network thread. |
| 907 | for (AudioReceiveStream* stream : audio_receive_streams_) { |
| 908 | if (stream->local_ssrc() == config.rtp.ssrc) { |
| 909 | stream->AssociateSendStream(send_stream); |
| 910 | } |
| 911 | } |
| 912 |
| 913 | UpdateAggregateNetworkState(); |
| 914 |
| 915 | return send_stream; |
| 916 | } |
| 917 |
| 918 | void Call::DestroyAudioSendStream(webrtc::AudioSendStream* send_stream) { |
| 919 | TRACE_EVENT0("webrtc", "Call::DestroyAudioSendStream"); |
| 920 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 921 | RTC_DCHECK(send_stream != nullptr); |
| 922 |
| 923 | send_stream->Stop(); |
| 924 |
| 925 | const uint32_t ssrc = send_stream->GetConfig().rtp.ssrc; |
| 926 | webrtc::internal::AudioSendStream* audio_send_stream = |
| 927 | static_cast<webrtc::internal::AudioSendStream*>(send_stream); |
| 928 | suspended_audio_send_ssrcs_[ssrc] = audio_send_stream->GetRtpState(); |
| 929 |
| 930 | size_t num_deleted = audio_send_ssrcs_.erase(ssrc); |
| 931 | RTC_DCHECK_EQ(1, num_deleted); |
| 932 |
| 933 | // TODO(bugs.webrtc.org/11993): call AssociateSendStream and |
| 934 | // UpdateAggregateNetworkState asynchronously on the network thread. |
| 935 | for (AudioReceiveStream* stream : audio_receive_streams_) { |
| 936 | if (stream->local_ssrc() == ssrc) { |
| 937 | stream->AssociateSendStream(nullptr); |
| 938 | } |
| 939 | } |
| 940 |
| 941 | UpdateAggregateNetworkState(); |
| 942 |
| 943 | delete send_stream; |
| 944 | } |
| 945 |
| 946 | webrtc::AudioReceiveStream* Call::CreateAudioReceiveStream( |
| 947 | const webrtc::AudioReceiveStream::Config& config) { |
| 948 | TRACE_EVENT0("webrtc", "Call::CreateAudioReceiveStream"); |
| 949 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 950 | EnsureStarted(); |
| 951 | event_log_->Log(std::make_unique<RtcEventAudioReceiveStreamConfig>( |
| 952 | CreateRtcLogStreamConfig(config))); |
| 953 |
| 954 | AudioReceiveStream* receive_stream = new AudioReceiveStream( |
| 955 | clock_, transport_send_->packet_router(), config_.neteq_factory, config, |
| 956 | config_.audio_state, event_log_); |
| 957 | audio_receive_streams_.insert(receive_stream); |
| 958 |
| 959 | // TODO(bugs.webrtc.org/11993): Make the registration on the network thread |
| 960 | // (asynchronously). The registration and `audio_receiver_controller_` need |
| 961 | // to live on the network thread. |
| 962 | receive_stream->RegisterWithTransport(&audio_receiver_controller_); |
| 963 |
| 964 | // TODO(bugs.webrtc.org/11993): Update the below on the network thread. |
| 965 | // We could possibly set up the audio_receiver_controller_ association up |
| 966 | // as part of the async setup. |
| 967 | receive_rtp_config_.emplace(config.rtp.remote_ssrc, receive_stream); |
| 968 |
| 969 | ConfigureSync(config.sync_group); |
| 970 |
| 971 | auto it = audio_send_ssrcs_.find(config.rtp.local_ssrc); |
| 972 | if (it != audio_send_ssrcs_.end()) { |
| 973 | receive_stream->AssociateSendStream(it->second); |
| 974 | } |
| 975 |
| 976 | UpdateAggregateNetworkState(); |
| 977 | return receive_stream; |
| 978 | } |
| 979 |
| 980 | void Call::DestroyAudioReceiveStream( |
| 981 | webrtc::AudioReceiveStream* receive_stream) { |
| 982 | TRACE_EVENT0("webrtc", "Call::DestroyAudioReceiveStream"); |
| 983 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 984 | RTC_DCHECK(receive_stream != nullptr); |
| 985 | webrtc::internal::AudioReceiveStream* audio_receive_stream = |
| 986 | static_cast<webrtc::internal::AudioReceiveStream*>(receive_stream); |
| 987 |
| 988 | // TODO(bugs.webrtc.org/11993): Access the map, rtp config, call ConfigureSync |
| 989 | // and UpdateAggregateNetworkState on the network thread. The call to |
| 990 | // `UnregisterFromTransport` should also happen on the network thread. |
| 991 | audio_receive_stream->UnregisterFromTransport(); |
| 992 |
| 993 | uint32_t ssrc = audio_receive_stream->remote_ssrc(); |
| 994 | const AudioReceiveStream::Config& config = audio_receive_stream->config(); |
| 995 | receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(config.rtp)) |
| 996 | ->RemoveStream(ssrc); |
| 997 |
| 998 | audio_receive_streams_.erase(audio_receive_stream); |
| 999 |
| 1000 | const auto it = sync_stream_mapping_.find(config.sync_group); |
| 1001 | if (it != sync_stream_mapping_.end() && it->second == audio_receive_stream) { |
| 1002 | sync_stream_mapping_.erase(it); |
| 1003 | ConfigureSync(config.sync_group); |
| 1004 | } |
| 1005 | receive_rtp_config_.erase(ssrc); |
| 1006 |
| 1007 | UpdateAggregateNetworkState(); |
| 1008 | // TODO(bugs.webrtc.org/11993): Consider if deleting `audio_receive_stream` |
| 1009 | // on the network thread would be better or if we'd need to tear down the |
| 1010 | // state in two phases. |
| 1011 | delete audio_receive_stream; |
| 1012 | } |
| 1013 |
| 1014 | // This method can be used for Call tests with external fec controller factory. |
| 1015 | webrtc::VideoSendStream* Call::CreateVideoSendStream( |
| 1016 | webrtc::VideoSendStream::Config config, |
| 1017 | VideoEncoderConfig encoder_config, |
| 1018 | std::unique_ptr<FecController> fec_controller) { |
| 1019 | TRACE_EVENT0("webrtc", "Call::CreateVideoSendStream"); |
| 1020 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1021 |
| 1022 | EnsureStarted(); |
| 1023 |
| 1024 | video_send_delay_stats_->AddSsrcs(config); |
| 1025 | for (size_t ssrc_index = 0; ssrc_index < config.rtp.ssrcs.size(); |
| 1026 | ++ssrc_index) { |
| 1027 | event_log_->Log(std::make_unique<RtcEventVideoSendStreamConfig>( |
| 1028 | CreateRtcLogStreamConfig(config, ssrc_index))); |
| 1029 | } |
| 1030 |
| 1031 | // TODO(mflodman): Base the start bitrate on a current bandwidth estimate, if |
| 1032 | // the call has already started. |
| 1033 | // Copy ssrcs from `config` since `config` is moved. |
| 1034 | std::vector<uint32_t> ssrcs = config.rtp.ssrcs; |
| 1035 |
| 1036 | VideoSendStream* send_stream = new VideoSendStream( |
| 1037 | clock_, num_cpu_cores_, task_queue_factory_, network_thread_, |
| 1038 | call_stats_->AsRtcpRttStats(), transport_send_.get(), |
| 1039 | bitrate_allocator_.get(), video_send_delay_stats_.get(), event_log_, |
| 1040 | std::move(config), std::move(encoder_config), suspended_video_send_ssrcs_, |
| 1041 | suspended_video_payload_states_, std::move(fec_controller)); |
| 1042 |
| 1043 | for (uint32_t ssrc : ssrcs) { |
| 1044 | RTC_DCHECK(video_send_ssrcs_.find(ssrc) == video_send_ssrcs_.end()); |
| 1045 | video_send_ssrcs_[ssrc] = send_stream; |
| 1046 | } |
| 1047 | video_send_streams_.insert(send_stream); |
| 1048 | video_send_streams_empty_.store(false, std::memory_order_relaxed); |
| 1049 |
| 1050 | // Forward resources that were previously added to the call to the new stream. |
| 1051 | for (const auto& resource_forwarder : adaptation_resource_forwarders_) { |
| 1052 | resource_forwarder->OnCreateVideoSendStream(send_stream); |
| 1053 | } |
| 1054 |
| 1055 | UpdateAggregateNetworkState(); |
| 1056 |
| 1057 | return send_stream; |
| 1058 | } |
| 1059 |
| 1060 | webrtc::VideoSendStream* Call::CreateVideoSendStream( |
| 1061 | webrtc::VideoSendStream::Config config, |
| 1062 | VideoEncoderConfig encoder_config) { |
| 1063 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1064 | if (config_.fec_controller_factory) { |
| 1065 | RTC_LOG(LS_INFO) << "External FEC Controller will be used."; |
| 1066 | } |
| 1067 | std::unique_ptr<FecController> fec_controller = |
| 1068 | config_.fec_controller_factory |
| 1069 | ? config_.fec_controller_factory->CreateFecController() |
| 1070 | : std::make_unique<FecControllerDefault>(clock_); |
| 1071 | return CreateVideoSendStream(std::move(config), std::move(encoder_config), |
| 1072 | std::move(fec_controller)); |
| 1073 | } |
| 1074 |
| 1075 | void Call::DestroyVideoSendStream(webrtc::VideoSendStream* send_stream) { |
| 1076 | TRACE_EVENT0("webrtc", "Call::DestroyVideoSendStream"); |
| 1077 | RTC_DCHECK(send_stream != nullptr); |
| 1078 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1079 |
| 1080 | VideoSendStream* send_stream_impl = |
| 1081 | static_cast<VideoSendStream*>(send_stream); |
| 1082 | VideoSendStream::RtpStateMap rtp_states; |
| 1083 | VideoSendStream::RtpPayloadStateMap rtp_payload_states; |
| 1084 | send_stream_impl->StopPermanentlyAndGetRtpStates(&rtp_states, |
| 1085 | &rtp_payload_states); |
| 1086 |
| 1087 | auto it = video_send_ssrcs_.begin(); |
| 1088 | while (it != video_send_ssrcs_.end()) { |
| 1089 | if (it->second == static_cast<VideoSendStream*>(send_stream)) { |
| 1090 | send_stream_impl = it->second; |
| 1091 | video_send_ssrcs_.erase(it++); |
| 1092 | } else { |
| 1093 | ++it; |
| 1094 | } |
| 1095 | } |
| 1096 |
| 1097 | // Stop forwarding resources to the stream being destroyed. |
| 1098 | for (const auto& resource_forwarder : adaptation_resource_forwarders_) { |
| 1099 | resource_forwarder->OnDestroyVideoSendStream(send_stream_impl); |
| 1100 | } |
| 1101 | video_send_streams_.erase(send_stream_impl); |
| 1102 | if (video_send_streams_.empty()) |
| 1103 | video_send_streams_empty_.store(true, std::memory_order_relaxed); |
| 1104 |
| 1105 | for (const auto& kv : rtp_states) { |
| 1106 | suspended_video_send_ssrcs_[kv.first] = kv.second; |
| 1107 | } |
| 1108 | for (const auto& kv : rtp_payload_states) { |
| 1109 | suspended_video_payload_states_[kv.first] = kv.second; |
| 1110 | } |
| 1111 |
| 1112 | UpdateAggregateNetworkState(); |
| 1113 | // TODO(tommi): consider deleting on the same thread as runs |
| 1114 | // StopPermanentlyAndGetRtpStates. |
| 1115 | delete send_stream_impl; |
| 1116 | } |
| 1117 |
| 1118 | webrtc::VideoReceiveStream* Call::CreateVideoReceiveStream( |
| 1119 | webrtc::VideoReceiveStream::Config configuration) { |
| 1120 | TRACE_EVENT0("webrtc", "Call::CreateVideoReceiveStream"); |
| 1121 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1122 |
| 1123 | receive_side_cc_.SetSendPeriodicFeedback( |
| 1124 | SendPeriodicFeedback(configuration.rtp.extensions)); |
| 1125 |
| 1126 | EnsureStarted(); |
| 1127 |
| 1128 | event_log_->Log(std::make_unique<RtcEventVideoReceiveStreamConfig>( |
| 1129 | CreateRtcLogStreamConfig(configuration))); |
| 1130 |
| 1131 | // TODO(bugs.webrtc.org/11993): Move the registration between `receive_stream` |
| 1132 | // and `video_receiver_controller_` out of VideoReceiveStream2 construction |
| 1133 | // and set it up asynchronously on the network thread (the registration and |
| 1134 | // `video_receiver_controller_` need to live on the network thread). |
| 1135 | VideoReceiveStream2* receive_stream = new VideoReceiveStream2( |
| 1136 | task_queue_factory_, this, num_cpu_cores_, |
| 1137 | transport_send_->packet_router(), std::move(configuration), |
| 1138 | call_stats_.get(), clock_, new VCMTiming(clock_), |
| 1139 | &nack_periodic_processor_); |
| 1140 | // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network |
| 1141 | // thread. |
| 1142 | receive_stream->RegisterWithTransport(&video_receiver_controller_); |
| 1143 |
| 1144 | const webrtc::VideoReceiveStream::Config::Rtp& rtp = receive_stream->rtp(); |
| 1145 | if (rtp.rtx_ssrc) { |
| 1146 | // We record identical config for the rtx stream as for the main |
| 1147 | // stream. Since the transport_send_cc negotiation is per payload |
| 1148 | // type, we may get an incorrect value for the rtx stream, but |
| 1149 | // that is unlikely to matter in practice. |
| 1150 | receive_rtp_config_.emplace(rtp.rtx_ssrc, receive_stream); |
| 1151 | } |
| 1152 | receive_rtp_config_.emplace(rtp.remote_ssrc, receive_stream); |
| 1153 | video_receive_streams_.insert(receive_stream); |
| 1154 |
| 1155 | ConfigureSync(receive_stream->sync_group()); |
| 1156 |
| 1157 | receive_stream->SignalNetworkState(video_network_state_); |
| 1158 | UpdateAggregateNetworkState(); |
| 1159 | return receive_stream; |
| 1160 | } |
| 1161 |
| 1162 | void Call::DestroyVideoReceiveStream( |
| 1163 | webrtc::VideoReceiveStream* receive_stream) { |
| 1164 | TRACE_EVENT0("webrtc", "Call::DestroyVideoReceiveStream"); |
| 1165 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1166 | RTC_DCHECK(receive_stream != nullptr); |
| 1167 | VideoReceiveStream2* receive_stream_impl = |
| 1168 | static_cast<VideoReceiveStream2*>(receive_stream); |
| 1169 | // TODO(bugs.webrtc.org/11993): Unregister on the network thread. |
| 1170 | receive_stream_impl->UnregisterFromTransport(); |
| 1171 |
| 1172 | const webrtc::VideoReceiveStream::Config::Rtp& rtp = |
| 1173 | receive_stream_impl->rtp(); |
| 1174 |
| 1175 | // Remove all ssrcs pointing to a receive stream. As RTX retransmits on a |
| 1176 | // separate SSRC there can be either one or two. |
| 1177 | receive_rtp_config_.erase(rtp.remote_ssrc); |
| 1178 | if (rtp.rtx_ssrc) { |
| 1179 | receive_rtp_config_.erase(rtp.rtx_ssrc); |
| 1180 | } |
| 1181 | video_receive_streams_.erase(receive_stream_impl); |
| 1182 | ConfigureSync(receive_stream_impl->sync_group()); |
| 1183 |
| 1184 | receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(rtp)) |
| 1185 | ->RemoveStream(rtp.remote_ssrc); |
| 1186 |
| 1187 | UpdateAggregateNetworkState(); |
| 1188 | delete receive_stream_impl; |
| 1189 | } |
| 1190 |
| 1191 | FlexfecReceiveStream* Call::CreateFlexfecReceiveStream( |
| 1192 | const FlexfecReceiveStream::Config& config) { |
| 1193 | TRACE_EVENT0("webrtc", "Call::CreateFlexfecReceiveStream"); |
| 1194 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1195 |
| 1196 | RecoveredPacketReceiver* recovered_packet_receiver = this; |
| 1197 |
| 1198 | FlexfecReceiveStreamImpl* receive_stream; |
| 1199 |
| 1200 | // Unlike the video and audio receive streams, FlexfecReceiveStream implements |
| 1201 | // RtpPacketSinkInterface itself, and hence its constructor passes its `this` |
| 1202 | // pointer to video_receiver_controller_->CreateStream(). Calling the |
| 1203 | // constructor while on the worker thread ensures that we don't call |
| 1204 | // OnRtpPacket until the constructor is finished and the object is |
| 1205 | // in a valid state, since OnRtpPacket runs on the same thread. |
| 1206 | receive_stream = new FlexfecReceiveStreamImpl( |
| 1207 | clock_, config, recovered_packet_receiver, call_stats_->AsRtcpRttStats()); |
| 1208 |
| 1209 | // TODO(bugs.webrtc.org/11993): Set this up asynchronously on the network |
| 1210 | // thread. |
| 1211 | receive_stream->RegisterWithTransport(&video_receiver_controller_); |
| 1212 |
| 1213 | RTC_DCHECK(receive_rtp_config_.find(config.rtp.remote_ssrc) == |
| 1214 | receive_rtp_config_.end()); |
| 1215 | receive_rtp_config_.emplace(config.rtp.remote_ssrc, receive_stream); |
| 1216 |
| 1217 | // TODO(brandtr): Store config in RtcEventLog here. |
| 1218 |
| 1219 | return receive_stream; |
| 1220 | } |
| 1221 |
| 1222 | void Call::DestroyFlexfecReceiveStream(FlexfecReceiveStream* receive_stream) { |
| 1223 | TRACE_EVENT0("webrtc", "Call::DestroyFlexfecReceiveStream"); |
| 1224 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1225 |
| 1226 | FlexfecReceiveStreamImpl* receive_stream_impl = |
| 1227 | static_cast<FlexfecReceiveStreamImpl*>(receive_stream); |
| 1228 | // TODO(bugs.webrtc.org/11993): Unregister on the network thread. |
| 1229 | receive_stream_impl->UnregisterFromTransport(); |
| 1230 |
| 1231 | RTC_DCHECK(receive_stream != nullptr); |
| 1232 | const FlexfecReceiveStream::RtpConfig& rtp = receive_stream->rtp_config(); |
| 1233 | receive_rtp_config_.erase(rtp.remote_ssrc); |
| 1234 |
| 1235 | // Remove all SSRCs pointing to the FlexfecReceiveStreamImpl to be |
| 1236 | // destroyed. |
| 1237 | receive_side_cc_.GetRemoteBitrateEstimator(UseSendSideBwe(rtp)) |
| 1238 | ->RemoveStream(rtp.remote_ssrc); |
| 1239 |
| 1240 | delete receive_stream; |
| 1241 | } |
| 1242 |
| 1243 | void Call::AddAdaptationResource(rtc::scoped_refptr<Resource> resource) { |
| 1244 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1245 | adaptation_resource_forwarders_.push_back( |
| 1246 | std::make_unique<ResourceVideoSendStreamForwarder>(resource)); |
| 1247 | const auto& resource_forwarder = adaptation_resource_forwarders_.back(); |
| 1248 | for (VideoSendStream* send_stream : video_send_streams_) { |
| 1249 | resource_forwarder->OnCreateVideoSendStream(send_stream); |
| 1250 | } |
| 1251 | } |
| 1252 |
| 1253 | RtpTransportControllerSendInterface* Call::GetTransportControllerSend() { |
| 1254 | return transport_send_.get(); |
| 1255 | } |
| 1256 |
| 1257 | Call::Stats Call::GetStats() const { |
| 1258 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1259 |
| 1260 | Stats stats; |
| 1261 | // TODO(srte): It is unclear if we only want to report queues if network is |
| 1262 | // available. |
| 1263 | stats.pacer_delay_ms = |
| 1264 | aggregate_network_up_ ? transport_send_->GetPacerQueuingDelayMs() : 0; |
| 1265 |
| 1266 | stats.rtt_ms = call_stats_->LastProcessedRtt(); |
| 1267 |
| 1268 | // Fetch available send/receive bitrates. |
| 1269 | std::vector<unsigned int> ssrcs; |
| 1270 | uint32_t recv_bandwidth = 0; |
| 1271 | receive_side_cc_.GetRemoteBitrateEstimator(false)->LatestEstimate( |
| 1272 | &ssrcs, &recv_bandwidth); |
| 1273 | stats.recv_bandwidth_bps = recv_bandwidth; |
| 1274 | stats.send_bandwidth_bps = |
| 1275 | last_bandwidth_bps_.load(std::memory_order_relaxed); |
| 1276 | stats.max_padding_bitrate_bps = |
| 1277 | configured_max_padding_bitrate_bps_.load(std::memory_order_relaxed); |
| 1278 |
| 1279 | return stats; |
| 1280 | } |
| 1281 |
| 1282 | const WebRtcKeyValueConfig& Call::trials() const { |
| 1283 | return trials_; |
| 1284 | } |
| 1285 |
| 1286 | TaskQueueBase* Call::network_thread() const { |
| 1287 | return network_thread_; |
| 1288 | } |
| 1289 |
| 1290 | TaskQueueBase* Call::worker_thread() const { |
| 1291 | return worker_thread_; |
| 1292 | } |
| 1293 |
| 1294 | void Call::SignalChannelNetworkState(MediaType media, NetworkState state) { |
| 1295 | RTC_DCHECK_RUN_ON(network_thread_); |
| 1296 | RTC_DCHECK(media == MediaType::AUDIO || media == MediaType::VIDEO); |
| 1297 |
| 1298 | auto closure = [this, media, state]() { |
| 1299 | // TODO(bugs.webrtc.org/11993): Move this over to the network thread. |
| 1300 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1301 | if (media == MediaType::AUDIO) { |
| 1302 | audio_network_state_ = state; |
| 1303 | } else { |
| 1304 | RTC_DCHECK_EQ(media, MediaType::VIDEO); |
| 1305 | video_network_state_ = state; |
| 1306 | } |
| 1307 |
| 1308 | // TODO(tommi): Is it necessary to always do this, including if there |
| 1309 | // was no change in state? |
| 1310 | UpdateAggregateNetworkState(); |
| 1311 |
| 1312 | // TODO(tommi): Is it right to do this if media == AUDIO? |
| 1313 | for (VideoReceiveStream2* video_receive_stream : video_receive_streams_) { |
| 1314 | video_receive_stream->SignalNetworkState(video_network_state_); |
| 1315 | } |
| 1316 | }; |
| 1317 |
| 1318 | if (network_thread_ == worker_thread_) { |
| 1319 | closure(); |
| 1320 | } else { |
| 1321 | // TODO(bugs.webrtc.org/11993): Remove workaround when we no longer need to |
| 1322 | // post to the worker thread. |
| 1323 | worker_thread_->PostTask(ToQueuedTask(task_safety_, std::move(closure))); |
| 1324 | } |
| 1325 | } |
| 1326 |
| 1327 | void Call::OnAudioTransportOverheadChanged(int transport_overhead_per_packet) { |
| 1328 | RTC_DCHECK_RUN_ON(network_thread_); |
| 1329 | worker_thread_->PostTask( |
| 1330 | ToQueuedTask(task_safety_, [this, transport_overhead_per_packet]() { |
| 1331 | // TODO(bugs.webrtc.org/11993): Move this over to the network thread. |
| 1332 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1333 | for (auto& kv : audio_send_ssrcs_) { |
| 1334 | kv.second->SetTransportOverhead(transport_overhead_per_packet); |
| 1335 | } |
| 1336 | })); |
| 1337 | } |
| 1338 |
| 1339 | void Call::UpdateAggregateNetworkState() { |
| 1340 | // TODO(bugs.webrtc.org/11993): Move this over to the network thread. |
| 1341 | // RTC_DCHECK_RUN_ON(network_thread_); |
| 1342 |
| 1343 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1344 |
| 1345 | bool have_audio = |
| 1346 | !audio_send_ssrcs_.empty() || !audio_receive_streams_.empty(); |
| 1347 | bool have_video = |
| 1348 | !video_send_ssrcs_.empty() || !video_receive_streams_.empty(); |
| 1349 |
| 1350 | bool aggregate_network_up = |
| 1351 | ((have_video && video_network_state_ == kNetworkUp) || |
| 1352 | (have_audio && audio_network_state_ == kNetworkUp)); |
| 1353 |
| 1354 | if (aggregate_network_up != aggregate_network_up_) { |
| 1355 | RTC_LOG(LS_INFO) |
| 1356 | << "UpdateAggregateNetworkState: aggregate_state change to " |
| 1357 | << (aggregate_network_up ? "up" : "down"); |
| 1358 | } else { |
| 1359 | RTC_LOG(LS_VERBOSE) |
| 1360 | << "UpdateAggregateNetworkState: aggregate_state remains at " |
| 1361 | << (aggregate_network_up ? "up" : "down"); |
| 1362 | } |
| 1363 | aggregate_network_up_ = aggregate_network_up; |
| 1364 |
| 1365 | transport_send_->OnNetworkAvailability(aggregate_network_up); |
| 1366 | } |
| 1367 |
| 1368 | void Call::OnLocalSsrcUpdated(webrtc::AudioReceiveStream& stream, |
| 1369 | uint32_t local_ssrc) { |
| 1370 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1371 | webrtc::internal::AudioReceiveStream& receive_stream = |
| 1372 | static_cast<webrtc::internal::AudioReceiveStream&>(stream); |
| 1373 |
| 1374 | receive_stream.SetLocalSsrc(local_ssrc); |
| 1375 | auto it = audio_send_ssrcs_.find(local_ssrc); |
| 1376 | receive_stream.AssociateSendStream(it != audio_send_ssrcs_.end() ? it->second |
| 1377 | : nullptr); |
| 1378 | } |
| 1379 |
| 1380 | void Call::OnUpdateSyncGroup(webrtc::AudioReceiveStream& stream, |
| 1381 | const std::string& sync_group) { |
| 1382 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1383 | webrtc::internal::AudioReceiveStream& receive_stream = |
| 1384 | static_cast<webrtc::internal::AudioReceiveStream&>(stream); |
| 1385 | receive_stream.SetSyncGroup(sync_group); |
| 1386 | ConfigureSync(sync_group); |
| 1387 | } |
| 1388 |
| 1389 | void Call::OnSentPacket(const rtc::SentPacket& sent_packet) { |
| 1390 | RTC_DCHECK_RUN_ON(&sent_packet_sequence_checker_); |
| 1391 | // When bundling is in effect, multiple senders may be sharing the same |
| 1392 | // transport. It means every |sent_packet| will be multiply notified from |
| 1393 | // different channels, WebRtcVoiceMediaChannel or WebRtcVideoChannel. Record |
| 1394 | // |last_sent_packet_| to deduplicate redundant notifications to downstream. |
| 1395 | // (https://crbug.com/webrtc/13437): Pass all packets without a |packet_id| to |
| 1396 | // downstream. |
| 1397 | if (last_sent_packet_.has_value() && last_sent_packet_->packet_id != -1 && |
| 1398 | last_sent_packet_->packet_id == sent_packet.packet_id && |
| 1399 | last_sent_packet_->send_time_ms == sent_packet.send_time_ms) { |
| 1400 | return; |
| 1401 | } |
| 1402 | last_sent_packet_ = sent_packet; |
| 1403 |
| 1404 | // In production and with most tests, this method will be called on the |
| 1405 | // network thread. However some test classes such as DirectTransport don't |
| 1406 | // incorporate a network thread. This means that tests for RtpSenderEgress |
| 1407 | // and ModuleRtpRtcpImpl2 that use DirectTransport, will call this method |
| 1408 | // on a ProcessThread. This is alright as is since we forward the call to |
| 1409 | // implementations that either just do a PostTask or use locking. |
| 1410 | video_send_delay_stats_->OnSentPacket(sent_packet.packet_id, |
| 1411 | clock_->TimeInMilliseconds()); |
| 1412 | transport_send_->OnSentPacket(sent_packet); |
| 1413 | } |
| 1414 |
| 1415 | void Call::OnStartRateUpdate(DataRate start_rate) { |
| 1416 | RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_); |
| 1417 | bitrate_allocator_->UpdateStartRate(start_rate.bps<uint32_t>()); |
| 1418 | } |
| 1419 |
| 1420 | void Call::OnTargetTransferRate(TargetTransferRate msg) { |
| 1421 | RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_); |
| 1422 |
| 1423 | uint32_t target_bitrate_bps = msg.target_rate.bps(); |
| 1424 | // For controlling the rate of feedback messages. |
| 1425 | receive_side_cc_.OnBitrateChanged(target_bitrate_bps); |
| 1426 | bitrate_allocator_->OnNetworkEstimateChanged(msg); |
| 1427 |
| 1428 | last_bandwidth_bps_.store(target_bitrate_bps, std::memory_order_relaxed); |
| 1429 |
| 1430 | // Ignore updates if bitrate is zero (the aggregate network state is |
| 1431 | // down) or if we're not sending video. |
| 1432 | // Using `video_send_streams_empty_` is racy but as the caller can't |
| 1433 | // reasonably expect synchronize with changes in `video_send_streams_` (being |
| 1434 | // on `send_transport_sequence_checker`), we can avoid a PostTask this way. |
| 1435 | if (target_bitrate_bps == 0 || |
| 1436 | video_send_streams_empty_.load(std::memory_order_relaxed)) { |
| 1437 | send_stats_.PauseSendAndPacerBitrateCounters(); |
| 1438 | } else { |
| 1439 | send_stats_.AddTargetBitrateSample(target_bitrate_bps); |
| 1440 | } |
| 1441 | } |
| 1442 |
| 1443 | void Call::OnAllocationLimitsChanged(BitrateAllocationLimits limits) { |
| 1444 | RTC_DCHECK_RUN_ON(&send_transport_sequence_checker_); |
| 1445 |
| 1446 | transport_send_ptr_->SetAllocatedSendBitrateLimits(limits); |
| 1447 | send_stats_.SetMinAllocatableRate(limits); |
| 1448 | configured_max_padding_bitrate_bps_.store(limits.max_padding_rate.bps(), |
| 1449 | std::memory_order_relaxed); |
| 1450 | } |
| 1451 |
| 1452 | // RTC_RUN_ON(worker_thread_) |
| 1453 | void Call::ConfigureSync(const std::string& sync_group) { |
| 1454 | // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread. |
| 1455 | // Set sync only if there was no previous one. |
| 1456 | if (sync_group.empty()) |
| 1457 | return; |
| 1458 |
| 1459 | AudioReceiveStream* sync_audio_stream = nullptr; |
| 1460 | // Find existing audio stream. |
| 1461 | const auto it = sync_stream_mapping_.find(sync_group); |
| 1462 | if (it != sync_stream_mapping_.end()) { |
| 1463 | sync_audio_stream = it->second; |
| 1464 | } else { |
| 1465 | // No configured audio stream, see if we can find one. |
| 1466 | for (AudioReceiveStream* stream : audio_receive_streams_) { |
| 1467 | if (stream->config().sync_group == sync_group) { |
| 1468 | if (sync_audio_stream != nullptr) { |
| 1469 | RTC_LOG(LS_WARNING) |
| 1470 | << "Attempting to sync more than one audio stream " |
| 1471 | "within the same sync group. This is not " |
| 1472 | "supported in the current implementation."; |
| 1473 | break; |
| 1474 | } |
| 1475 | sync_audio_stream = stream; |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | if (sync_audio_stream) |
| 1480 | sync_stream_mapping_[sync_group] = sync_audio_stream; |
| 1481 | size_t num_synced_streams = 0; |
| 1482 | for (VideoReceiveStream2* video_stream : video_receive_streams_) { |
| 1483 | if (video_stream->sync_group() != sync_group) |
| 1484 | continue; |
| 1485 | ++num_synced_streams; |
| 1486 | if (num_synced_streams > 1) { |
| 1487 | // TODO(pbos): Support synchronizing more than one A/V pair. |
| 1488 | // https://code.google.com/p/webrtc/issues/detail?id=4762 |
| 1489 | RTC_LOG(LS_WARNING) |
| 1490 | << "Attempting to sync more than one audio/video pair " |
| 1491 | "within the same sync group. This is not supported in " |
| 1492 | "the current implementation."; |
| 1493 | } |
| 1494 | // Only sync the first A/V pair within this sync group. |
| 1495 | if (num_synced_streams == 1) { |
| 1496 | // sync_audio_stream may be null and that's ok. |
| 1497 | video_stream->SetSync(sync_audio_stream); |
| 1498 | } else { |
| 1499 | video_stream->SetSync(nullptr); |
| 1500 | } |
| 1501 | } |
| 1502 | } |
| 1503 |
| 1504 | // RTC_RUN_ON(network_thread_) |
| 1505 | void Call::DeliverRtcp(MediaType media_type, rtc::CopyOnWriteBuffer packet) { |
| 1506 | TRACE_EVENT0("webrtc", "Call::DeliverRtcp"); |
| 1507 |
| 1508 | // TODO(bugs.webrtc.org/11993): This DCHECK is here just to maintain the |
| 1509 | // invariant that currently the only call path to this function is via |
| 1510 | // `PeerConnection::InitializeRtcpCallback()`. DeliverRtp on the other hand |
| 1511 | // gets called via the channel classes and |
| 1512 | // WebRtc[Audio|Video]Channel's `OnPacketReceived`. We'll remove the |
| 1513 | // PeerConnection involvement as well as |
| 1514 | // `JsepTransportController::OnRtcpPacketReceived_n` and `rtcp_handler` |
| 1515 | // and make sure that the flow of packets is consistent from the |
| 1516 | // `RtpTransport` class, via the *Channel and *Engine classes and into Call. |
| 1517 | // This way we'll also know more about the context of the packet. |
| 1518 | RTC_DCHECK_EQ(media_type, MediaType::ANY); |
| 1519 |
| 1520 | // TODO(bugs.webrtc.org/11993): This should execute directly on the network |
| 1521 | // thread. |
| 1522 | worker_thread_->PostTask( |
| 1523 | ToQueuedTask(task_safety_, [this, packet = std::move(packet)]() { |
| 1524 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1525 |
| 1526 | receive_stats_.AddReceivedRtcpBytes(static_cast<int>(packet.size())); |
| 1527 | bool rtcp_delivered = false; |
| 1528 | for (VideoReceiveStream2* stream : video_receive_streams_) { |
| 1529 | if (stream->DeliverRtcp(packet.cdata(), packet.size())) |
| 1530 | rtcp_delivered = true; |
| 1531 | } |
| 1532 |
| 1533 | for (AudioReceiveStream* stream : audio_receive_streams_) { |
| 1534 | stream->DeliverRtcp(packet.cdata(), packet.size()); |
| 1535 | rtcp_delivered = true; |
| 1536 | } |
| 1537 |
| 1538 | for (VideoSendStream* stream : video_send_streams_) { |
| 1539 | stream->DeliverRtcp(packet.cdata(), packet.size()); |
| 1540 | rtcp_delivered = true; |
| 1541 | } |
| 1542 |
| 1543 | for (auto& kv : audio_send_ssrcs_) { |
| 1544 | kv.second->DeliverRtcp(packet.cdata(), packet.size()); |
| 1545 | rtcp_delivered = true; |
| 1546 | } |
| 1547 |
| 1548 | if (rtcp_delivered) { |
| 1549 | event_log_->Log(std::make_unique<RtcEventRtcpPacketIncoming>( |
| 1550 | rtc::MakeArrayView(packet.cdata(), packet.size()))); |
| 1551 | } |
| 1552 | })); |
| 1553 | } |
| 1554 |
| 1555 | PacketReceiver::DeliveryStatus Call::DeliverRtp(MediaType media_type, |
| 1556 | rtc::CopyOnWriteBuffer packet, |
| 1557 | int64_t packet_time_us) { |
| 1558 | TRACE_EVENT0("webrtc", "Call::DeliverRtp"); |
| 1559 | RTC_DCHECK_NE(media_type, MediaType::ANY); |
| 1560 |
| 1561 | RtpPacketReceived parsed_packet; |
| 1562 | if (!parsed_packet.Parse(std::move(packet))) |
| 1563 | return DELIVERY_PACKET_ERROR; |
| 1564 |
| 1565 | if (packet_time_us != -1) { |
| 1566 | if (receive_time_calculator_) { |
| 1567 | // Repair packet_time_us for clock resets by comparing a new read of |
| 1568 | // the same clock (TimeUTCMicros) to a monotonic clock reading. |
| 1569 | packet_time_us = receive_time_calculator_->ReconcileReceiveTimes( |
| 1570 | packet_time_us, rtc::TimeUTCMicros(), clock_->TimeInMicroseconds()); |
| 1571 | } |
| 1572 | parsed_packet.set_arrival_time(Timestamp::Micros(packet_time_us)); |
| 1573 | } else { |
| 1574 | parsed_packet.set_arrival_time(clock_->CurrentTime()); |
| 1575 | } |
| 1576 |
| 1577 | // We might get RTP keep-alive packets in accordance with RFC6263 section 4.6. |
| 1578 | // These are empty (zero length payload) RTP packets with an unsignaled |
| 1579 | // payload type. |
| 1580 | const bool is_keep_alive_packet = parsed_packet.payload_size() == 0; |
| 1581 |
| 1582 | RTC_DCHECK(media_type == MediaType::AUDIO || media_type == MediaType::VIDEO || |
| 1583 | is_keep_alive_packet); |
| 1584 |
| 1585 | auto it = receive_rtp_config_.find(parsed_packet.Ssrc()); |
| 1586 | if (it == receive_rtp_config_.end()) { |
| 1587 | RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc " |
| 1588 | << parsed_packet.Ssrc(); |
| 1589 | // Destruction of the receive stream, including deregistering from the |
| 1590 | // RtpDemuxer, is not protected by the `worker_thread_`. |
| 1591 | // But deregistering in the `receive_rtp_config_` map is. So by not passing |
| 1592 | // the packet on to demuxing in this case, we prevent incoming packets to be |
| 1593 | // passed on via the demuxer to a receive stream which is being torned down. |
| 1594 | return DELIVERY_UNKNOWN_SSRC; |
| 1595 | } |
| 1596 |
| 1597 | parsed_packet.IdentifyExtensions( |
| 1598 | RtpHeaderExtensionMap(it->second->rtp_config().extensions)); |
| 1599 |
| 1600 | NotifyBweOfReceivedPacket(parsed_packet, media_type); |
| 1601 |
| 1602 | // RateCounters expect input parameter as int, save it as int, |
| 1603 | // instead of converting each time it is passed to RateCounter::Add below. |
| 1604 | int length = static_cast<int>(parsed_packet.size()); |
| 1605 | if (media_type == MediaType::AUDIO) { |
| 1606 | if (audio_receiver_controller_.OnRtpPacket(parsed_packet)) { |
| 1607 | receive_stats_.AddReceivedAudioBytes(length, |
| 1608 | parsed_packet.arrival_time()); |
| 1609 | event_log_->Log( |
| 1610 | std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet)); |
| 1611 | return DELIVERY_OK; |
| 1612 | } |
| 1613 | } else if (media_type == MediaType::VIDEO) { |
| 1614 | parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency); |
| 1615 | if (video_receiver_controller_.OnRtpPacket(parsed_packet)) { |
| 1616 | receive_stats_.AddReceivedVideoBytes(length, |
| 1617 | parsed_packet.arrival_time()); |
| 1618 | event_log_->Log( |
| 1619 | std::make_unique<RtcEventRtpPacketIncoming>(parsed_packet)); |
| 1620 | return DELIVERY_OK; |
| 1621 | } |
| 1622 | } |
| 1623 | return DELIVERY_UNKNOWN_SSRC; |
| 1624 | } |
| 1625 |
| 1626 | PacketReceiver::DeliveryStatus Call::DeliverPacket( |
| 1627 | MediaType media_type, |
| 1628 | rtc::CopyOnWriteBuffer packet, |
| 1629 | int64_t packet_time_us) { |
| 1630 | if (IsRtcpPacket(packet)) { |
| 1631 | RTC_DCHECK_RUN_ON(network_thread_); |
| 1632 | DeliverRtcp(media_type, std::move(packet)); |
| 1633 | return DELIVERY_OK; |
| 1634 | } |
| 1635 |
| 1636 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1637 | return DeliverRtp(media_type, std::move(packet), packet_time_us); |
| 1638 | } |
| 1639 |
| 1640 | void Call::OnRecoveredPacket(const uint8_t* packet, size_t length) { |
| 1641 | // TODO(bugs.webrtc.org/11993): Expect to be called on the network thread. |
| 1642 | // This method is called synchronously via `OnRtpPacket()` (see DeliverRtp) |
| 1643 | // on the same thread. |
| 1644 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 1645 | RtpPacketReceived parsed_packet; |
| 1646 | if (!parsed_packet.Parse(packet, length)) |
| 1647 | return; |
| 1648 |
| 1649 | parsed_packet.set_recovered(true); |
| 1650 |
| 1651 | auto it = receive_rtp_config_.find(parsed_packet.Ssrc()); |
| 1652 | if (it == receive_rtp_config_.end()) { |
| 1653 | RTC_LOG(LS_ERROR) << "receive_rtp_config_ lookup failed for ssrc " |
| 1654 | << parsed_packet.Ssrc(); |
| 1655 | // Destruction of the receive stream, including deregistering from the |
| 1656 | // RtpDemuxer, is not protected by the `worker_thread_`. |
| 1657 | // But deregistering in the `receive_rtp_config_` map is. |
| 1658 | // So by not passing the packet on to demuxing in this case, we prevent |
| 1659 | // incoming packets to be passed on via the demuxer to a receive stream |
| 1660 | // which is being torn down. |
| 1661 | return; |
| 1662 | } |
| 1663 | parsed_packet.IdentifyExtensions( |
| 1664 | RtpHeaderExtensionMap(it->second->rtp_config().extensions)); |
| 1665 |
| 1666 | // TODO(brandtr): Update here when we support protecting audio packets too. |
| 1667 | parsed_packet.set_payload_type_frequency(kVideoPayloadTypeFrequency); |
| 1668 | video_receiver_controller_.OnRtpPacket(parsed_packet); |
| 1669 | } |
| 1670 |
| 1671 | // RTC_RUN_ON(worker_thread_) |
| 1672 | void Call::NotifyBweOfReceivedPacket(const RtpPacketReceived& packet, |
| 1673 | MediaType media_type) { |
| 1674 | auto it = receive_rtp_config_.find(packet.Ssrc()); |
| 1675 | bool use_send_side_bwe = (it != receive_rtp_config_.end()) && |
| 1676 | UseSendSideBwe(it->second->rtp_config()); |
| 1677 |
| 1678 | RTPHeader header; |
| 1679 | packet.GetHeader(&header); |
| 1680 |
| 1681 | ReceivedPacket packet_msg; |
| 1682 | packet_msg.size = DataSize::Bytes(packet.payload_size()); |
| 1683 | packet_msg.receive_time = packet.arrival_time(); |
| 1684 | if (header.extension.hasAbsoluteSendTime) { |
| 1685 | packet_msg.send_time = header.extension.GetAbsoluteSendTimestamp(); |
| 1686 | } |
| 1687 | transport_send_->OnReceivedPacket(packet_msg); |
| 1688 |
| 1689 | if (!use_send_side_bwe && header.extension.hasTransportSequenceNumber) { |
| 1690 | // Inconsistent configuration of send side BWE. Do nothing. |
| 1691 | // TODO(nisse): Without this check, we may produce RTCP feedback |
| 1692 | // packets even when not negotiated. But it would be cleaner to |
| 1693 | // move the check down to RTCPSender::SendFeedbackPacket, which |
| 1694 | // would also help the PacketRouter to select an appropriate rtp |
| 1695 | // module in the case that some, but not all, have RTCP feedback |
| 1696 | // enabled. |
| 1697 | return; |
| 1698 | } |
| 1699 | // For audio, we only support send side BWE. |
| 1700 | if (media_type == MediaType::VIDEO || |
| 1701 | (use_send_side_bwe && header.extension.hasTransportSequenceNumber)) { |
| 1702 | receive_side_cc_.OnReceivedPacket( |
| 1703 | packet.arrival_time().ms(), |
| 1704 | packet.payload_size() + packet.padding_size(), header); |
| 1705 | } |
| 1706 | } |
| 1707 |
| 1708 | } // namespace internal |
| 1709 |
| 1710 | } // namespace webrtc |
| 1711 |