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