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