Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "audio/channel_send.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "absl/memory/memory.h" |
| 21 | #include "api/array_view.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 22 | #include "api/call/transport.h" |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 23 | #include "api/crypto/frameencryptorinterface.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 24 | #include "audio/utility/audio_frame_operations.h" |
| 25 | #include "call/rtp_transport_controller_send_interface.h" |
| 26 | #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" |
| 27 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 28 | #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 29 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 30 | #include "modules/audio_processing/rms_level.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 31 | #include "modules/pacing/packet_router.h" |
| 32 | #include "modules/utility/include/process_thread.h" |
| 33 | #include "rtc_base/checks.h" |
| 34 | #include "rtc_base/criticalsection.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 35 | #include "rtc_base/event.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 36 | #include "rtc_base/format_macros.h" |
| 37 | #include "rtc_base/location.h" |
| 38 | #include "rtc_base/logging.h" |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 39 | #include "rtc_base/numerics/safe_conversions.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 40 | #include "rtc_base/race_checker.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 41 | #include "rtc_base/rate_limiter.h" |
| 42 | #include "rtc_base/task_queue.h" |
| 43 | #include "rtc_base/thread_checker.h" |
| 44 | #include "rtc_base/timeutils.h" |
| 45 | #include "system_wrappers/include/field_trial.h" |
| 46 | #include "system_wrappers/include/metrics.h" |
| 47 | |
| 48 | namespace webrtc { |
| 49 | namespace voe { |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 54 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 55 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 56 | MediaTransportEncodedAudioFrame::FrameType |
| 57 | MediaTransportFrameTypeForWebrtcFrameType(webrtc::FrameType frame_type) { |
| 58 | switch (frame_type) { |
| 59 | case kAudioFrameSpeech: |
| 60 | return MediaTransportEncodedAudioFrame::FrameType::kSpeech; |
| 61 | break; |
| 62 | |
| 63 | case kAudioFrameCN: |
| 64 | return MediaTransportEncodedAudioFrame::FrameType:: |
| 65 | kDiscontinuousTransmission; |
| 66 | break; |
| 67 | |
| 68 | default: |
| 69 | RTC_CHECK(false) << "Unexpected frame type=" << frame_type; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 74 | class RtpPacketSenderProxy; |
| 75 | class TransportFeedbackProxy; |
| 76 | class TransportSequenceNumberProxy; |
| 77 | class VoERtcpObserver; |
| 78 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 79 | class ChannelSend |
| 80 | : public ChannelSendInterface, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 81 | public OverheadObserver, |
| 82 | public AudioPacketizationCallback, // receive encoded packets from the |
| 83 | // ACM |
| 84 | public TargetTransferRateObserver { |
| 85 | public: |
| 86 | // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend |
| 87 | // declaration. |
| 88 | friend class VoERtcpObserver; |
| 89 | |
| 90 | ChannelSend(rtc::TaskQueue* encoder_queue, |
| 91 | ProcessThread* module_process_thread, |
| 92 | MediaTransportInterface* media_transport, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 93 | Transport* rtp_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 94 | RtcpRttStats* rtcp_rtt_stats, |
| 95 | RtcEventLog* rtc_event_log, |
| 96 | FrameEncryptorInterface* frame_encryptor, |
| 97 | const webrtc::CryptoOptions& crypto_options, |
| 98 | bool extmap_allow_mixed, |
| 99 | int rtcp_report_interval_ms); |
| 100 | |
| 101 | ~ChannelSend() override; |
| 102 | |
| 103 | // Send using this encoder, with this payload type. |
| 104 | bool SetEncoder(int payload_type, |
| 105 | std::unique_ptr<AudioEncoder> encoder) override; |
| 106 | void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> |
| 107 | modifier) override; |
| 108 | |
| 109 | // API methods |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 110 | void StartSend() override; |
| 111 | void StopSend() override; |
| 112 | |
| 113 | // Codecs |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 114 | void OnBitrateAllocation(BitrateAllocationUpdate update) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 115 | int GetBitrate() const override; |
| 116 | |
| 117 | // Network |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 118 | bool ReceivedRTCPPacket(const uint8_t* data, size_t length) override; |
| 119 | |
| 120 | // Muting, Volume and Level. |
| 121 | void SetInputMute(bool enable) override; |
| 122 | |
| 123 | // Stats. |
| 124 | ANAStats GetANAStatistics() const override; |
| 125 | |
| 126 | // Used by AudioSendStream. |
| 127 | RtpRtcp* GetRtpRtcp() const override; |
| 128 | |
| 129 | // DTMF. |
| 130 | bool SendTelephoneEventOutband(int event, int duration_ms) override; |
| 131 | bool SetSendTelephoneEventPayloadType(int payload_type, |
| 132 | int payload_frequency) override; |
| 133 | |
| 134 | // RTP+RTCP |
| 135 | void SetLocalSSRC(uint32_t ssrc) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 136 | void SetMid(const std::string& mid, int extension_id) override; |
| 137 | void SetExtmapAllowMixed(bool extmap_allow_mixed) override; |
| 138 | void SetSendAudioLevelIndicationStatus(bool enable, int id) override; |
| 139 | void EnableSendTransportSequenceNumber(int id) override; |
| 140 | |
| 141 | void RegisterSenderCongestionControlObjects( |
| 142 | RtpTransportControllerSendInterface* transport, |
| 143 | RtcpBandwidthObserver* bandwidth_observer) override; |
| 144 | void ResetSenderCongestionControlObjects() override; |
| 145 | void SetRTCP_CNAME(absl::string_view c_name) override; |
| 146 | std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const override; |
| 147 | CallSendStatistics GetRTCPStatistics() const override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 148 | |
| 149 | // ProcessAndEncodeAudio() posts a task on the shared encoder task queue, |
| 150 | // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where |
| 151 | // the actual processing of the audio takes place. The processing mainly |
| 152 | // consists of encoding and preparing the result for sending by adding it to a |
| 153 | // send queue. |
| 154 | // The main reason for using a task queue here is to release the native, |
| 155 | // OS-specific, audio capture thread as soon as possible to ensure that it |
| 156 | // can go back to sleep and be prepared to deliver an new captured audio |
| 157 | // packet. |
| 158 | void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) override; |
| 159 | |
| 160 | void SetTransportOverhead(size_t transport_overhead_per_packet) override; |
| 161 | |
| 162 | // The existence of this function alongside OnUplinkPacketLossRate is |
| 163 | // a compromise. We want the encoder to be agnostic of the PLR source, but |
| 164 | // we also don't want it to receive conflicting information from TWCC and |
| 165 | // from RTCP-XR. |
| 166 | void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) override; |
| 167 | |
| 168 | void OnRecoverableUplinkPacketLossRate( |
| 169 | float recoverable_packet_loss_rate) override; |
| 170 | |
| 171 | int64_t GetRTT() const override; |
| 172 | |
| 173 | // E2EE Custom Audio Frame Encryption |
| 174 | void SetFrameEncryptor( |
| 175 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override; |
| 176 | |
| 177 | private: |
| 178 | class ProcessAndEncodeAudioTask; |
| 179 | |
| 180 | // From AudioPacketizationCallback in the ACM |
| 181 | int32_t SendData(FrameType frameType, |
| 182 | uint8_t payloadType, |
| 183 | uint32_t timeStamp, |
| 184 | const uint8_t* payloadData, |
| 185 | size_t payloadSize, |
| 186 | const RTPFragmentationHeader* fragmentation) override; |
| 187 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 188 | // From OverheadObserver in the RTP/RTCP module |
| 189 | void OnOverheadChanged(size_t overhead_bytes_per_packet) override; |
| 190 | |
| 191 | void OnUplinkPacketLossRate(float packet_loss_rate); |
| 192 | bool InputMute() const; |
| 193 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 194 | int SetSendRtpHeaderExtension(bool enable, RTPExtensionType type, int id); |
| 195 | |
| 196 | void UpdateOverheadForEncoder() |
| 197 | RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_); |
| 198 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 199 | int32_t SendRtpAudio(FrameType frameType, |
| 200 | uint8_t payloadType, |
| 201 | uint32_t timeStamp, |
| 202 | rtc::ArrayView<const uint8_t> payload, |
| 203 | const RTPFragmentationHeader* fragmentation); |
| 204 | |
| 205 | int32_t SendMediaTransportAudio(FrameType frameType, |
| 206 | uint8_t payloadType, |
| 207 | uint32_t timeStamp, |
| 208 | rtc::ArrayView<const uint8_t> payload, |
| 209 | const RTPFragmentationHeader* fragmentation); |
| 210 | |
| 211 | // Return media transport or nullptr if using RTP. |
| 212 | MediaTransportInterface* media_transport() { return media_transport_; } |
| 213 | |
| 214 | // Called on the encoder task queue when a new input audio frame is ready |
| 215 | // for encoding. |
| 216 | void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input); |
| 217 | |
| 218 | void OnReceivedRtt(int64_t rtt_ms); |
| 219 | |
| 220 | void OnTargetTransferRate(TargetTransferRate) override; |
| 221 | |
| 222 | // Thread checkers document and lock usage of some methods on voe::Channel to |
| 223 | // specific threads we know about. The goal is to eventually split up |
| 224 | // voe::Channel into parts with single-threaded semantics, and thereby reduce |
| 225 | // the need for locks. |
| 226 | rtc::ThreadChecker worker_thread_checker_; |
| 227 | rtc::ThreadChecker module_process_thread_checker_; |
| 228 | // Methods accessed from audio and video threads are checked for sequential- |
| 229 | // only access. We don't necessarily own and control these threads, so thread |
| 230 | // checkers cannot be used. E.g. Chromium may transfer "ownership" from one |
| 231 | // audio thread to another, but access is still sequential. |
| 232 | rtc::RaceChecker audio_thread_race_checker_; |
| 233 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 234 | rtc::CriticalSection volume_settings_critsect_; |
| 235 | |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 236 | bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 237 | |
| 238 | RtcEventLog* const event_log_; |
| 239 | |
| 240 | std::unique_ptr<RtpRtcp> _rtpRtcpModule; |
| 241 | |
| 242 | std::unique_ptr<AudioCodingModule> audio_coding_; |
| 243 | uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_); |
| 244 | |
| 245 | uint16_t send_sequence_number_; |
| 246 | |
| 247 | // uses |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 248 | ProcessThread* const _moduleProcessThreadPtr; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 249 | RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_); |
| 250 | bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_); |
| 251 | bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_); |
| 252 | // VoeRTP_RTCP |
| 253 | // TODO(henrika): can today be accessed on the main thread and on the |
| 254 | // task queue; hence potential race. |
| 255 | bool _includeAudioLevelIndication; |
| 256 | size_t transport_overhead_per_packet_ |
| 257 | RTC_GUARDED_BY(overhead_per_packet_lock_); |
| 258 | size_t rtp_overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_); |
| 259 | rtc::CriticalSection overhead_per_packet_lock_; |
| 260 | // RtcpBandwidthObserver |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 261 | const std::unique_ptr<VoERtcpObserver> rtcp_observer_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 262 | |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 263 | PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) = |
| 264 | nullptr; |
| 265 | const std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_; |
| 266 | const std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_; |
| 267 | const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_; |
| 268 | const std::unique_ptr<RateLimiter> retransmission_rate_limiter_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 269 | |
| 270 | rtc::ThreadChecker construction_thread_; |
| 271 | |
| 272 | const bool use_twcc_plr_for_ana_; |
| 273 | |
| 274 | rtc::CriticalSection encoder_queue_lock_; |
| 275 | bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_lock_) = false; |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 276 | rtc::TaskQueue* const encoder_queue_ = nullptr; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 277 | |
| 278 | MediaTransportInterface* const media_transport_; |
| 279 | int media_transport_sequence_number_ RTC_GUARDED_BY(encoder_queue_) = 0; |
| 280 | |
| 281 | rtc::CriticalSection media_transport_lock_; |
| 282 | // Currently set by SetLocalSSRC. |
| 283 | uint64_t media_transport_channel_id_ RTC_GUARDED_BY(&media_transport_lock_) = |
| 284 | 0; |
| 285 | // Cache payload type and sampling frequency from most recent call to |
| 286 | // SetEncoder. Needed to set MediaTransportEncodedAudioFrame metadata, and |
| 287 | // invalidate on encoder change. |
| 288 | int media_transport_payload_type_ RTC_GUARDED_BY(&media_transport_lock_); |
| 289 | int media_transport_sampling_frequency_ |
| 290 | RTC_GUARDED_BY(&media_transport_lock_); |
| 291 | |
| 292 | // E2EE Audio Frame Encryption |
| 293 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_; |
| 294 | // E2EE Frame Encryption Options |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 295 | const webrtc::CryptoOptions crypto_options_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 296 | |
| 297 | rtc::CriticalSection bitrate_crit_section_; |
| 298 | int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0; |
| 299 | }; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 300 | |
| 301 | const int kTelephoneEventAttenuationdB = 10; |
| 302 | |
| 303 | class TransportFeedbackProxy : public TransportFeedbackObserver { |
| 304 | public: |
| 305 | TransportFeedbackProxy() : feedback_observer_(nullptr) { |
| 306 | pacer_thread_.DetachFromThread(); |
| 307 | network_thread_.DetachFromThread(); |
| 308 | } |
| 309 | |
| 310 | void SetTransportFeedbackObserver( |
| 311 | TransportFeedbackObserver* feedback_observer) { |
| 312 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 313 | rtc::CritScope lock(&crit_); |
| 314 | feedback_observer_ = feedback_observer; |
| 315 | } |
| 316 | |
| 317 | // Implements TransportFeedbackObserver. |
| 318 | void AddPacket(uint32_t ssrc, |
| 319 | uint16_t sequence_number, |
| 320 | size_t length, |
| 321 | const PacedPacketInfo& pacing_info) override { |
| 322 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 323 | rtc::CritScope lock(&crit_); |
| 324 | if (feedback_observer_) |
| 325 | feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info); |
| 326 | } |
| 327 | |
| 328 | void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override { |
| 329 | RTC_DCHECK(network_thread_.CalledOnValidThread()); |
| 330 | rtc::CritScope lock(&crit_); |
| 331 | if (feedback_observer_) |
| 332 | feedback_observer_->OnTransportFeedback(feedback); |
| 333 | } |
| 334 | |
| 335 | private: |
| 336 | rtc::CriticalSection crit_; |
| 337 | rtc::ThreadChecker thread_checker_; |
| 338 | rtc::ThreadChecker pacer_thread_; |
| 339 | rtc::ThreadChecker network_thread_; |
| 340 | TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_); |
| 341 | }; |
| 342 | |
| 343 | class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator { |
| 344 | public: |
| 345 | TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) { |
| 346 | pacer_thread_.DetachFromThread(); |
| 347 | } |
| 348 | |
| 349 | void SetSequenceNumberAllocator( |
| 350 | TransportSequenceNumberAllocator* seq_num_allocator) { |
| 351 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 352 | rtc::CritScope lock(&crit_); |
| 353 | seq_num_allocator_ = seq_num_allocator; |
| 354 | } |
| 355 | |
| 356 | // Implements TransportSequenceNumberAllocator. |
| 357 | uint16_t AllocateSequenceNumber() override { |
| 358 | RTC_DCHECK(pacer_thread_.CalledOnValidThread()); |
| 359 | rtc::CritScope lock(&crit_); |
| 360 | if (!seq_num_allocator_) |
| 361 | return 0; |
| 362 | return seq_num_allocator_->AllocateSequenceNumber(); |
| 363 | } |
| 364 | |
| 365 | private: |
| 366 | rtc::CriticalSection crit_; |
| 367 | rtc::ThreadChecker thread_checker_; |
| 368 | rtc::ThreadChecker pacer_thread_; |
| 369 | TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_); |
| 370 | }; |
| 371 | |
| 372 | class RtpPacketSenderProxy : public RtpPacketSender { |
| 373 | public: |
| 374 | RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {} |
| 375 | |
| 376 | void SetPacketSender(RtpPacketSender* rtp_packet_sender) { |
| 377 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 378 | rtc::CritScope lock(&crit_); |
| 379 | rtp_packet_sender_ = rtp_packet_sender; |
| 380 | } |
| 381 | |
| 382 | // Implements RtpPacketSender. |
| 383 | void InsertPacket(Priority priority, |
| 384 | uint32_t ssrc, |
| 385 | uint16_t sequence_number, |
| 386 | int64_t capture_time_ms, |
| 387 | size_t bytes, |
| 388 | bool retransmission) override { |
| 389 | rtc::CritScope lock(&crit_); |
| 390 | if (rtp_packet_sender_) { |
| 391 | rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number, |
| 392 | capture_time_ms, bytes, retransmission); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void SetAccountForAudioPackets(bool account_for_audio) override { |
| 397 | RTC_NOTREACHED(); |
| 398 | } |
| 399 | |
| 400 | private: |
| 401 | rtc::ThreadChecker thread_checker_; |
| 402 | rtc::CriticalSection crit_; |
| 403 | RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_); |
| 404 | }; |
| 405 | |
| 406 | class VoERtcpObserver : public RtcpBandwidthObserver { |
| 407 | public: |
| 408 | explicit VoERtcpObserver(ChannelSend* owner) |
| 409 | : owner_(owner), bandwidth_observer_(nullptr) {} |
| 410 | virtual ~VoERtcpObserver() {} |
| 411 | |
| 412 | void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) { |
| 413 | rtc::CritScope lock(&crit_); |
| 414 | bandwidth_observer_ = bandwidth_observer; |
| 415 | } |
| 416 | |
| 417 | void OnReceivedEstimatedBitrate(uint32_t bitrate) override { |
| 418 | rtc::CritScope lock(&crit_); |
| 419 | if (bandwidth_observer_) { |
| 420 | bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks, |
| 425 | int64_t rtt, |
| 426 | int64_t now_ms) override { |
| 427 | { |
| 428 | rtc::CritScope lock(&crit_); |
| 429 | if (bandwidth_observer_) { |
| 430 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt, |
| 431 | now_ms); |
| 432 | } |
| 433 | } |
| 434 | // TODO(mflodman): Do we need to aggregate reports here or can we jut send |
| 435 | // what we get? I.e. do we ever get multiple reports bundled into one RTCP |
| 436 | // report for VoiceEngine? |
| 437 | if (report_blocks.empty()) |
| 438 | return; |
| 439 | |
| 440 | int fraction_lost_aggregate = 0; |
| 441 | int total_number_of_packets = 0; |
| 442 | |
| 443 | // If receiving multiple report blocks, calculate the weighted average based |
| 444 | // on the number of packets a report refers to. |
| 445 | for (ReportBlockList::const_iterator block_it = report_blocks.begin(); |
| 446 | block_it != report_blocks.end(); ++block_it) { |
| 447 | // Find the previous extended high sequence number for this remote SSRC, |
| 448 | // to calculate the number of RTP packets this report refers to. Ignore if |
| 449 | // we haven't seen this SSRC before. |
| 450 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 451 | extended_max_sequence_number_.find(block_it->source_ssrc); |
| 452 | int number_of_packets = 0; |
| 453 | if (seq_num_it != extended_max_sequence_number_.end()) { |
| 454 | number_of_packets = |
| 455 | block_it->extended_highest_sequence_number - seq_num_it->second; |
| 456 | } |
| 457 | fraction_lost_aggregate += number_of_packets * block_it->fraction_lost; |
| 458 | total_number_of_packets += number_of_packets; |
| 459 | |
| 460 | extended_max_sequence_number_[block_it->source_ssrc] = |
| 461 | block_it->extended_highest_sequence_number; |
| 462 | } |
| 463 | int weighted_fraction_lost = 0; |
| 464 | if (total_number_of_packets > 0) { |
| 465 | weighted_fraction_lost = |
| 466 | (fraction_lost_aggregate + total_number_of_packets / 2) / |
| 467 | total_number_of_packets; |
| 468 | } |
| 469 | owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f); |
| 470 | } |
| 471 | |
| 472 | private: |
| 473 | ChannelSend* owner_; |
| 474 | // Maps remote side ssrc to extended highest sequence number received. |
| 475 | std::map<uint32_t, uint32_t> extended_max_sequence_number_; |
| 476 | rtc::CriticalSection crit_; |
| 477 | RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_); |
| 478 | }; |
| 479 | |
| 480 | class ChannelSend::ProcessAndEncodeAudioTask : public rtc::QueuedTask { |
| 481 | public: |
| 482 | ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame, |
| 483 | ChannelSend* channel) |
| 484 | : audio_frame_(std::move(audio_frame)), channel_(channel) { |
| 485 | RTC_DCHECK(channel_); |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | bool Run() override { |
| 490 | RTC_DCHECK_RUN_ON(channel_->encoder_queue_); |
| 491 | channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get()); |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | std::unique_ptr<AudioFrame> audio_frame_; |
| 496 | ChannelSend* const channel_; |
| 497 | }; |
| 498 | |
| 499 | int32_t ChannelSend::SendData(FrameType frameType, |
| 500 | uint8_t payloadType, |
| 501 | uint32_t timeStamp, |
| 502 | const uint8_t* payloadData, |
| 503 | size_t payloadSize, |
| 504 | const RTPFragmentationHeader* fragmentation) { |
| 505 | RTC_DCHECK_RUN_ON(encoder_queue_); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 506 | rtc::ArrayView<const uint8_t> payload(payloadData, payloadSize); |
| 507 | |
| 508 | if (media_transport() != nullptr) { |
| 509 | return SendMediaTransportAudio(frameType, payloadType, timeStamp, payload, |
| 510 | fragmentation); |
| 511 | } else { |
| 512 | return SendRtpAudio(frameType, payloadType, timeStamp, payload, |
| 513 | fragmentation); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | int32_t ChannelSend::SendRtpAudio(FrameType frameType, |
| 518 | uint8_t payloadType, |
| 519 | uint32_t timeStamp, |
| 520 | rtc::ArrayView<const uint8_t> payload, |
| 521 | const RTPFragmentationHeader* fragmentation) { |
| 522 | RTC_DCHECK_RUN_ON(encoder_queue_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 523 | if (_includeAudioLevelIndication) { |
| 524 | // Store current audio level in the RTP/RTCP module. |
| 525 | // The level will be used in combination with voice-activity state |
| 526 | // (frameType) to add an RTP header extension |
| 527 | _rtpRtcpModule->SetAudioLevel(rms_level_.Average()); |
| 528 | } |
| 529 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 530 | // E2EE Custom Audio Frame Encryption (This is optional). |
| 531 | // Keep this buffer around for the lifetime of the send call. |
| 532 | rtc::Buffer encrypted_audio_payload; |
| 533 | if (frame_encryptor_ != nullptr) { |
| 534 | // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline. |
| 535 | // Allocate a buffer to hold the maximum possible encrypted payload. |
| 536 | size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize( |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 537 | cricket::MEDIA_TYPE_AUDIO, payload.size()); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 538 | encrypted_audio_payload.SetSize(max_ciphertext_size); |
| 539 | |
| 540 | // Encrypt the audio payload into the buffer. |
| 541 | size_t bytes_written = 0; |
| 542 | int encrypt_status = frame_encryptor_->Encrypt( |
| 543 | cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(), |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 544 | /*additional_data=*/nullptr, payload, encrypted_audio_payload, |
| 545 | &bytes_written); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 546 | if (encrypt_status != 0) { |
| 547 | RTC_DLOG(LS_ERROR) << "Channel::SendData() failed encrypt audio payload: " |
| 548 | << encrypt_status; |
| 549 | return -1; |
| 550 | } |
| 551 | // Resize the buffer to the exact number of bytes actually used. |
| 552 | encrypted_audio_payload.SetSize(bytes_written); |
| 553 | // Rewrite the payloadData and size to the new encrypted payload. |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 554 | payload = encrypted_audio_payload; |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 555 | } else if (crypto_options_.sframe.require_frame_encryption) { |
| 556 | RTC_DLOG(LS_ERROR) << "Channel::SendData() failed sending audio payload: " |
| 557 | << "A frame encryptor is required but one is not set."; |
| 558 | return -1; |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 561 | // Push data from ACM to RTP/RTCP-module to deliver audio frame for |
| 562 | // packetization. |
| 563 | // This call will trigger Transport::SendPacket() from the RTP/RTCP module. |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 564 | if (!_rtpRtcpModule->SendOutgoingData((FrameType&)frameType, payloadType, |
| 565 | timeStamp, |
| 566 | // Leaving the time when this frame was |
| 567 | // received from the capture device as |
| 568 | // undefined for voice for now. |
| 569 | -1, payload.data(), payload.size(), |
| 570 | fragmentation, nullptr, nullptr)) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 571 | RTC_DLOG(LS_ERROR) |
| 572 | << "ChannelSend::SendData() failed to send data to RTP/RTCP module"; |
| 573 | return -1; |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 579 | int32_t ChannelSend::SendMediaTransportAudio( |
| 580 | FrameType frameType, |
| 581 | uint8_t payloadType, |
| 582 | uint32_t timeStamp, |
| 583 | rtc::ArrayView<const uint8_t> payload, |
| 584 | const RTPFragmentationHeader* fragmentation) { |
| 585 | RTC_DCHECK_RUN_ON(encoder_queue_); |
| 586 | // TODO(nisse): Use null _transportPtr for MediaTransport. |
| 587 | // RTC_DCHECK(_transportPtr == nullptr); |
| 588 | uint64_t channel_id; |
| 589 | int sampling_rate_hz; |
| 590 | { |
| 591 | rtc::CritScope cs(&media_transport_lock_); |
| 592 | if (media_transport_payload_type_ != payloadType) { |
| 593 | // Payload type is being changed, media_transport_sampling_frequency_, |
| 594 | // no longer current. |
| 595 | return -1; |
| 596 | } |
| 597 | sampling_rate_hz = media_transport_sampling_frequency_; |
| 598 | channel_id = media_transport_channel_id_; |
| 599 | } |
| 600 | const MediaTransportEncodedAudioFrame frame( |
| 601 | /*sampling_rate_hz=*/sampling_rate_hz, |
| 602 | |
| 603 | // TODO(nisse): Timestamp and sample index are the same for all supported |
| 604 | // audio codecs except G722. Refactor audio coding module to only use |
| 605 | // sample index, and leave translation to RTP time, when needed, for |
| 606 | // RTP-specific code. |
| 607 | /*starting_sample_index=*/timeStamp, |
| 608 | |
| 609 | // Sample count isn't conveniently available from the AudioCodingModule, |
| 610 | // and needs some refactoring to wire up in a good way. For now, left as |
| 611 | // zero. |
| 612 | /*sample_count=*/0, |
| 613 | |
| 614 | /*sequence_number=*/media_transport_sequence_number_, |
| 615 | MediaTransportFrameTypeForWebrtcFrameType(frameType), payloadType, |
| 616 | std::vector<uint8_t>(payload.begin(), payload.end())); |
| 617 | |
| 618 | // TODO(nisse): Introduce a MediaTransportSender object bound to a specific |
| 619 | // channel id. |
| 620 | RTCError rtc_error = |
| 621 | media_transport()->SendAudioFrame(channel_id, std::move(frame)); |
| 622 | |
| 623 | if (!rtc_error.ok()) { |
| 624 | RTC_LOG(LS_ERROR) << "Failed to send frame, rtc_error=" |
| 625 | << ToString(rtc_error.type()) << ", " |
| 626 | << rtc_error.message(); |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | ++media_transport_sequence_number_; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 635 | ChannelSend::ChannelSend(rtc::TaskQueue* encoder_queue, |
| 636 | ProcessThread* module_process_thread, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 637 | MediaTransportInterface* media_transport, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 638 | Transport* rtp_transport, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 639 | RtcpRttStats* rtcp_rtt_stats, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 640 | RtcEventLog* rtc_event_log, |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 641 | FrameEncryptorInterface* frame_encryptor, |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 642 | const webrtc::CryptoOptions& crypto_options, |
Jiawei Ou | 5571812 | 2018-11-09 13:17:39 -0800 | [diff] [blame] | 643 | bool extmap_allow_mixed, |
| 644 | int rtcp_report_interval_ms) |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 645 | : event_log_(rtc_event_log), |
| 646 | _timeStamp(0), // This is just an offset, RTP module will add it's own |
| 647 | // random offset |
| 648 | send_sequence_number_(0), |
| 649 | _moduleProcessThreadPtr(module_process_thread), |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 650 | input_mute_(false), |
| 651 | previous_frame_muted_(false), |
| 652 | _includeAudioLevelIndication(false), |
| 653 | transport_overhead_per_packet_(0), |
| 654 | rtp_overhead_per_packet_(0), |
| 655 | rtcp_observer_(new VoERtcpObserver(this)), |
| 656 | feedback_observer_proxy_(new TransportFeedbackProxy()), |
| 657 | seq_num_allocator_proxy_(new TransportSequenceNumberProxy()), |
| 658 | rtp_packet_sender_proxy_(new RtpPacketSenderProxy()), |
| 659 | retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(), |
| 660 | kMaxRetransmissionWindowMs)), |
| 661 | use_twcc_plr_for_ana_( |
| 662 | webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled"), |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 663 | encoder_queue_(encoder_queue), |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 664 | media_transport_(media_transport), |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 665 | frame_encryptor_(frame_encryptor), |
| 666 | crypto_options_(crypto_options) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 667 | RTC_DCHECK(module_process_thread); |
| 668 | RTC_DCHECK(encoder_queue); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 669 | module_process_thread_checker_.DetachFromThread(); |
| 670 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 671 | audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config())); |
| 672 | |
| 673 | RtpRtcp::Configuration configuration; |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 674 | |
| 675 | // We gradually remove codepaths that depend on RTP when using media |
| 676 | // transport. All of this logic should be moved to the future |
| 677 | // RTPMediaTransport. In this case it means that overhead and bandwidth |
| 678 | // observers should not be called when using media transport. |
| 679 | if (!media_transport_) { |
| 680 | configuration.overhead_observer = this; |
| 681 | configuration.bandwidth_callback = rtcp_observer_.get(); |
| 682 | configuration.transport_feedback_callback = feedback_observer_proxy_.get(); |
| 683 | } |
| 684 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 685 | configuration.audio = true; |
Fredrik Solenberg | 3d2ed19 | 2018-12-18 09:18:33 +0100 | [diff] [blame] | 686 | configuration.outgoing_transport = rtp_transport; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 687 | |
| 688 | configuration.paced_sender = rtp_packet_sender_proxy_.get(); |
| 689 | configuration.transport_sequence_number_allocator = |
| 690 | seq_num_allocator_proxy_.get(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 691 | |
| 692 | configuration.event_log = event_log_; |
| 693 | configuration.rtt_stats = rtcp_rtt_stats; |
| 694 | configuration.retransmission_rate_limiter = |
| 695 | retransmission_rate_limiter_.get(); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 696 | configuration.extmap_allow_mixed = extmap_allow_mixed; |
Jiawei Ou | 8b5d9d8 | 2018-11-15 16:44:37 -0800 | [diff] [blame] | 697 | configuration.rtcp_report_interval_ms = rtcp_report_interval_ms; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 698 | |
| 699 | _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
| 700 | _rtpRtcpModule->SetSendingMediaStatus(false); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 701 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 702 | // We want to invoke the 'TargetRateObserver' and |OnOverheadChanged| |
| 703 | // callbacks after the audio_coding_ is fully initialized. |
| 704 | if (media_transport_) { |
| 705 | RTC_DLOG(LS_INFO) << "Setting media_transport_ rate observers."; |
| 706 | media_transport_->AddTargetTransferRateObserver(this); |
| 707 | OnOverheadChanged(media_transport_->GetAudioPacketOverhead()); |
| 708 | } else { |
| 709 | RTC_DLOG(LS_INFO) << "Not setting media_transport_ rate observers."; |
| 710 | } |
| 711 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 712 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 713 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 714 | // Ensure that RTCP is enabled by default for the created channel. |
| 715 | // Note that, the module will keep generating RTCP until it is explicitly |
| 716 | // disabled by the user. |
| 717 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 718 | // be transmitted since the Transport object will then be invalid. |
| 719 | // RTCP is enabled by default. |
| 720 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 721 | |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 722 | int error = audio_coding_->RegisterTransportCallback(this); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 723 | RTC_DCHECK_EQ(0, error); |
| 724 | } |
| 725 | |
Fredrik Solenberg | 645a3af | 2018-11-16 12:51:15 +0100 | [diff] [blame] | 726 | ChannelSend::~ChannelSend() { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 727 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 728 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 729 | if (media_transport_) { |
| 730 | media_transport_->RemoveTargetTransferRateObserver(this); |
| 731 | } |
| 732 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 733 | StopSend(); |
| 734 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 735 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 736 | RTC_DCHECK_EQ(0, error); |
| 737 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 738 | if (_moduleProcessThreadPtr) |
| 739 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 742 | void ChannelSend::StartSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 743 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 744 | RTC_DCHECK(!sending_); |
| 745 | sending_ = true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 746 | |
| 747 | // Resume the previous sequence number which was reset by StopSend(). This |
| 748 | // needs to be done before |sending| is set to true on the RTP/RTCP module. |
| 749 | if (send_sequence_number_) { |
| 750 | _rtpRtcpModule->SetSequenceNumber(send_sequence_number_); |
| 751 | } |
| 752 | _rtpRtcpModule->SetSendingMediaStatus(true); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 753 | int ret = _rtpRtcpModule->SetSendingStatus(true); |
| 754 | RTC_DCHECK_EQ(0, ret); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 755 | { |
| 756 | // It is now OK to start posting tasks to the encoder task queue. |
| 757 | rtc::CritScope cs(&encoder_queue_lock_); |
| 758 | encoder_queue_is_active_ = true; |
| 759 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | void ChannelSend::StopSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 763 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 764 | if (!sending_) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 765 | return; |
| 766 | } |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 767 | sending_ = false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 768 | |
| 769 | // Post a task to the encoder thread which sets an event when the task is |
| 770 | // executed. We know that no more encoding tasks will be added to the task |
| 771 | // queue for this channel since sending is now deactivated. It means that, |
| 772 | // if we wait for the event to bet set, we know that no more pending tasks |
| 773 | // exists and it is therfore guaranteed that the task queue will never try |
| 774 | // to acccess and invalid channel object. |
| 775 | RTC_DCHECK(encoder_queue_); |
| 776 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 777 | rtc::Event flush; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 778 | { |
| 779 | // Clear |encoder_queue_is_active_| under lock to prevent any other tasks |
| 780 | // than this final "flush task" to be posted on the queue. |
| 781 | rtc::CritScope cs(&encoder_queue_lock_); |
| 782 | encoder_queue_is_active_ = false; |
| 783 | encoder_queue_->PostTask([&flush]() { flush.Set(); }); |
| 784 | } |
| 785 | flush.Wait(rtc::Event::kForever); |
| 786 | |
| 787 | // Store the sequence number to be able to pick up the same sequence for |
| 788 | // the next StartSend(). This is needed for restarting device, otherwise |
| 789 | // it might cause libSRTP to complain about packets being replayed. |
| 790 | // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring |
| 791 | // CL is landed. See issue |
| 792 | // https://code.google.com/p/webrtc/issues/detail?id=2111 . |
| 793 | send_sequence_number_ = _rtpRtcpModule->SequenceNumber(); |
| 794 | |
| 795 | // Reset sending SSRC and sequence number and triggers direct transmission |
| 796 | // of RTCP BYE |
| 797 | if (_rtpRtcpModule->SetSendingStatus(false) == -1) { |
| 798 | RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending"; |
| 799 | } |
| 800 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 801 | } |
| 802 | |
| 803 | bool ChannelSend::SetEncoder(int payload_type, |
| 804 | std::unique_ptr<AudioEncoder> encoder) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 805 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 806 | RTC_DCHECK_GE(payload_type, 0); |
| 807 | RTC_DCHECK_LE(payload_type, 127); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 808 | |
| 809 | // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate) |
| 810 | // as well as some other things, so we collect this info and send it along. |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 811 | _rtpRtcpModule->RegisterAudioSendPayload(payload_type, |
| 812 | "audio", |
| 813 | encoder->RtpTimestampRateHz(), |
| 814 | encoder->NumChannels(), |
| 815 | 0); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 816 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 817 | if (media_transport_) { |
| 818 | rtc::CritScope cs(&media_transport_lock_); |
| 819 | media_transport_payload_type_ = payload_type; |
| 820 | // TODO(nisse): Currently broken for G722, since timestamps passed through |
| 821 | // encoder use RTP clock rather than sample count, and they differ for G722. |
| 822 | media_transport_sampling_frequency_ = encoder->RtpTimestampRateHz(); |
| 823 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 824 | audio_coding_->SetEncoder(std::move(encoder)); |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | void ChannelSend::ModifyEncoder( |
| 829 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 830 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 831 | audio_coding_->ModifyEncoder(modifier); |
| 832 | } |
| 833 | |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 834 | void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 835 | // This method can be called on the worker thread, module process thread |
| 836 | // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged. |
| 837 | // TODO(solenberg): Figure out a good way to check this or enforce calling |
| 838 | // rules. |
| 839 | // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() || |
| 840 | // module_process_thread_checker_.CalledOnValidThread()); |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 841 | rtc::CritScope lock(&bitrate_crit_section_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 842 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 843 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 844 | if (*encoder) { |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 845 | (*encoder)->OnReceivedUplinkAllocation(update); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 846 | } |
| 847 | }); |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 848 | retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps()); |
| 849 | configured_bitrate_bps_ = update.target_bitrate.bps(); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 850 | } |
| 851 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 852 | int ChannelSend::GetBitrate() const { |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 853 | rtc::CritScope lock(&bitrate_crit_section_); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 854 | return configured_bitrate_bps_; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 858 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 859 | if (!use_twcc_plr_for_ana_) |
| 860 | return; |
| 861 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 862 | if (*encoder) { |
| 863 | (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
| 864 | } |
| 865 | }); |
| 866 | } |
| 867 | |
| 868 | void ChannelSend::OnRecoverableUplinkPacketLossRate( |
| 869 | float recoverable_packet_loss_rate) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 870 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 871 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 872 | if (*encoder) { |
| 873 | (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction( |
| 874 | recoverable_packet_loss_rate); |
| 875 | } |
| 876 | }); |
| 877 | } |
| 878 | |
| 879 | void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) { |
| 880 | if (use_twcc_plr_for_ana_) |
| 881 | return; |
| 882 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 883 | if (*encoder) { |
| 884 | (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
| 885 | } |
| 886 | }); |
| 887 | } |
| 888 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 889 | // TODO(nisse): Delete always-true return value. |
| 890 | bool ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 891 | // May be called on either worker thread or network thread. |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 892 | if (media_transport_) { |
| 893 | // Ignore RTCP packets while media transport is used. |
| 894 | // Those packets should not arrive, but we are seeing occasional packets. |
| 895 | return 0; |
| 896 | } |
| 897 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 898 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 899 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 900 | |
| 901 | int64_t rtt = GetRTT(); |
| 902 | if (rtt == 0) { |
| 903 | // Waiting for valid RTT. |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 904 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | int64_t nack_window_ms = rtt; |
| 908 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 909 | nack_window_ms = kMinRetransmissionWindowMs; |
| 910 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 911 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 912 | } |
| 913 | retransmission_rate_limiter_->SetWindowSize(nack_window_ms); |
| 914 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 915 | OnReceivedRtt(rtt); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 916 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void ChannelSend::SetInputMute(bool enable) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 920 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 921 | rtc::CritScope cs(&volume_settings_critsect_); |
| 922 | input_mute_ = enable; |
| 923 | } |
| 924 | |
| 925 | bool ChannelSend::InputMute() const { |
| 926 | rtc::CritScope cs(&volume_settings_critsect_); |
| 927 | return input_mute_; |
| 928 | } |
| 929 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 930 | bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 931 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 932 | RTC_DCHECK_LE(0, event); |
| 933 | RTC_DCHECK_GE(255, event); |
| 934 | RTC_DCHECK_LE(0, duration_ms); |
| 935 | RTC_DCHECK_GE(65535, duration_ms); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 936 | if (!sending_) { |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 937 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 938 | } |
| 939 | if (_rtpRtcpModule->SendTelephoneEventOutband( |
| 940 | event, duration_ms, kTelephoneEventAttenuationdB) != 0) { |
| 941 | RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event"; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 942 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 943 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 944 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 947 | bool ChannelSend::SetSendTelephoneEventPayloadType(int payload_type, |
| 948 | int payload_frequency) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 949 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 950 | RTC_DCHECK_LE(0, payload_type); |
| 951 | RTC_DCHECK_GE(127, payload_type); |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 952 | _rtpRtcpModule->RegisterAudioSendPayload(payload_type, "telephone-event", |
| 953 | payload_frequency, 0, 0); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 954 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 957 | void ChannelSend::SetLocalSSRC(uint32_t ssrc) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 958 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 959 | RTC_DCHECK(!sending_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 960 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 961 | if (media_transport_) { |
| 962 | rtc::CritScope cs(&media_transport_lock_); |
| 963 | media_transport_channel_id_ = ssrc; |
| 964 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 965 | _rtpRtcpModule->SetSSRC(ssrc); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | void ChannelSend::SetMid(const std::string& mid, int extension_id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 969 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 970 | int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id); |
| 971 | RTC_DCHECK_EQ(0, ret); |
| 972 | _rtpRtcpModule->SetMid(mid); |
| 973 | } |
| 974 | |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 975 | void ChannelSend::SetExtmapAllowMixed(bool extmap_allow_mixed) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 976 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 977 | _rtpRtcpModule->SetExtmapAllowMixed(extmap_allow_mixed); |
| 978 | } |
| 979 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 980 | void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 981 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 982 | _includeAudioLevelIndication = enable; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 983 | int ret = SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id); |
| 984 | RTC_DCHECK_EQ(0, ret); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | void ChannelSend::EnableSendTransportSequenceNumber(int id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 988 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 989 | int ret = |
| 990 | SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id); |
| 991 | RTC_DCHECK_EQ(0, ret); |
| 992 | } |
| 993 | |
| 994 | void ChannelSend::RegisterSenderCongestionControlObjects( |
| 995 | RtpTransportControllerSendInterface* transport, |
| 996 | RtcpBandwidthObserver* bandwidth_observer) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 997 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 998 | RtpPacketSender* rtp_packet_sender = transport->packet_sender(); |
| 999 | TransportFeedbackObserver* transport_feedback_observer = |
| 1000 | transport->transport_feedback_observer(); |
| 1001 | PacketRouter* packet_router = transport->packet_router(); |
| 1002 | |
| 1003 | RTC_DCHECK(rtp_packet_sender); |
| 1004 | RTC_DCHECK(transport_feedback_observer); |
| 1005 | RTC_DCHECK(packet_router); |
| 1006 | RTC_DCHECK(!packet_router_); |
| 1007 | rtcp_observer_->SetBandwidthObserver(bandwidth_observer); |
| 1008 | feedback_observer_proxy_->SetTransportFeedbackObserver( |
| 1009 | transport_feedback_observer); |
| 1010 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router); |
| 1011 | rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender); |
| 1012 | _rtpRtcpModule->SetStorePacketsStatus(true, 600); |
| 1013 | constexpr bool remb_candidate = false; |
| 1014 | packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 1015 | packet_router_ = packet_router; |
| 1016 | } |
| 1017 | |
| 1018 | void ChannelSend::ResetSenderCongestionControlObjects() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1019 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1020 | RTC_DCHECK(packet_router_); |
| 1021 | _rtpRtcpModule->SetStorePacketsStatus(false, 600); |
| 1022 | rtcp_observer_->SetBandwidthObserver(nullptr); |
| 1023 | feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr); |
| 1024 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr); |
| 1025 | packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get()); |
| 1026 | packet_router_ = nullptr; |
| 1027 | rtp_packet_sender_proxy_->SetPacketSender(nullptr); |
| 1028 | } |
| 1029 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1030 | void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1031 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1032 | // Note: SetCNAME() accepts a c string of length at most 255. |
| 1033 | const std::string c_name_limited(c_name.substr(0, 255)); |
| 1034 | int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0; |
| 1035 | RTC_DCHECK_EQ(0, ret) << "SetRTCP_CNAME() failed to set RTCP CNAME"; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1038 | std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1039 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1040 | // Get the report blocks from the latest received RTCP Sender or Receiver |
| 1041 | // Report. Each element in the vector contains the sender's SSRC and a |
| 1042 | // report block according to RFC 3550. |
| 1043 | std::vector<RTCPReportBlock> rtcp_report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1044 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1045 | int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks); |
| 1046 | RTC_DCHECK_EQ(0, ret); |
| 1047 | |
| 1048 | std::vector<ReportBlock> report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1049 | |
| 1050 | std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin(); |
| 1051 | for (; it != rtcp_report_blocks.end(); ++it) { |
| 1052 | ReportBlock report_block; |
| 1053 | report_block.sender_SSRC = it->sender_ssrc; |
| 1054 | report_block.source_SSRC = it->source_ssrc; |
| 1055 | report_block.fraction_lost = it->fraction_lost; |
| 1056 | report_block.cumulative_num_packets_lost = it->packets_lost; |
| 1057 | report_block.extended_highest_sequence_number = |
| 1058 | it->extended_highest_sequence_number; |
| 1059 | report_block.interarrival_jitter = it->jitter; |
| 1060 | report_block.last_SR_timestamp = it->last_sender_report_timestamp; |
| 1061 | report_block.delay_since_last_SR = it->delay_since_last_sender_report; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1062 | report_blocks.push_back(report_block); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1063 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1064 | return report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1067 | CallSendStatistics ChannelSend::GetRTCPStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1068 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1069 | CallSendStatistics stats = {0}; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1070 | stats.rttMs = GetRTT(); |
| 1071 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1072 | size_t bytesSent(0); |
| 1073 | uint32_t packetsSent(0); |
| 1074 | |
| 1075 | if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) { |
| 1076 | RTC_DLOG(LS_WARNING) |
| 1077 | << "GetRTPStatistics() failed to retrieve RTP datacounters" |
| 1078 | << " => output will not be complete"; |
| 1079 | } |
| 1080 | |
| 1081 | stats.bytesSent = bytesSent; |
| 1082 | stats.packetsSent = packetsSent; |
| 1083 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1084 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1087 | void ChannelSend::ProcessAndEncodeAudio( |
| 1088 | std::unique_ptr<AudioFrame> audio_frame) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1089 | RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1090 | // Avoid posting any new tasks if sending was already stopped in StopSend(). |
| 1091 | rtc::CritScope cs(&encoder_queue_lock_); |
| 1092 | if (!encoder_queue_is_active_) { |
| 1093 | return; |
| 1094 | } |
| 1095 | // Profile time between when the audio frame is added to the task queue and |
| 1096 | // when the task is actually executed. |
| 1097 | audio_frame->UpdateProfileTimeStamp(); |
| 1098 | encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>( |
| 1099 | new ProcessAndEncodeAudioTask(std::move(audio_frame), this))); |
| 1100 | } |
| 1101 | |
| 1102 | void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) { |
| 1103 | RTC_DCHECK_RUN_ON(encoder_queue_); |
| 1104 | RTC_DCHECK_GT(audio_input->samples_per_channel_, 0); |
| 1105 | RTC_DCHECK_LE(audio_input->num_channels_, 2); |
| 1106 | |
| 1107 | // Measure time between when the audio frame is added to the task queue and |
| 1108 | // when the task is actually executed. Goal is to keep track of unwanted |
| 1109 | // extra latency added by the task queue. |
| 1110 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", |
| 1111 | audio_input->ElapsedProfileTimeMs()); |
| 1112 | |
| 1113 | bool is_muted = InputMute(); |
| 1114 | AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted); |
| 1115 | |
| 1116 | if (_includeAudioLevelIndication) { |
| 1117 | size_t length = |
| 1118 | audio_input->samples_per_channel_ * audio_input->num_channels_; |
| 1119 | RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); |
| 1120 | if (is_muted && previous_frame_muted_) { |
| 1121 | rms_level_.AnalyzeMuted(length); |
| 1122 | } else { |
| 1123 | rms_level_.Analyze( |
| 1124 | rtc::ArrayView<const int16_t>(audio_input->data(), length)); |
| 1125 | } |
| 1126 | } |
| 1127 | previous_frame_muted_ = is_muted; |
| 1128 | |
| 1129 | // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. |
| 1130 | |
| 1131 | // The ACM resamples internally. |
| 1132 | audio_input->timestamp_ = _timeStamp; |
| 1133 | // This call will trigger AudioPacketizationCallback::SendData if encoding |
| 1134 | // is done and payload is ready for packetization and transmission. |
| 1135 | // Otherwise, it will return without invoking the callback. |
| 1136 | if (audio_coding_->Add10MsData(*audio_input) < 0) { |
| 1137 | RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed."; |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_); |
| 1142 | } |
| 1143 | |
| 1144 | void ChannelSend::UpdateOverheadForEncoder() { |
| 1145 | size_t overhead_per_packet = |
| 1146 | transport_overhead_per_packet_ + rtp_overhead_per_packet_; |
| 1147 | audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) { |
| 1148 | if (*encoder) { |
| 1149 | (*encoder)->OnReceivedOverhead(overhead_per_packet); |
| 1150 | } |
| 1151 | }); |
| 1152 | } |
| 1153 | |
| 1154 | void ChannelSend::SetTransportOverhead(size_t transport_overhead_per_packet) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1155 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1156 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 1157 | transport_overhead_per_packet_ = transport_overhead_per_packet; |
| 1158 | UpdateOverheadForEncoder(); |
| 1159 | } |
| 1160 | |
| 1161 | // TODO(solenberg): Make AudioSendStream an OverheadObserver instead. |
| 1162 | void ChannelSend::OnOverheadChanged(size_t overhead_bytes_per_packet) { |
| 1163 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 1164 | rtp_overhead_per_packet_ = overhead_bytes_per_packet; |
| 1165 | UpdateOverheadForEncoder(); |
| 1166 | } |
| 1167 | |
| 1168 | ANAStats ChannelSend::GetANAStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1169 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1170 | return audio_coding_->GetANAStats(); |
| 1171 | } |
| 1172 | |
| 1173 | RtpRtcp* ChannelSend::GetRtpRtcp() const { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1174 | RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1175 | return _rtpRtcpModule.get(); |
| 1176 | } |
| 1177 | |
| 1178 | int ChannelSend::SetSendRtpHeaderExtension(bool enable, |
| 1179 | RTPExtensionType type, |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1180 | int id) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1181 | int error = 0; |
| 1182 | _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type); |
| 1183 | if (enable) { |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1184 | // TODO(nisse): RtpRtcp::RegisterSendRtpHeaderExtension to take an int |
| 1185 | // argument. Currently it wants an uint8_t. |
| 1186 | error = _rtpRtcpModule->RegisterSendRtpHeaderExtension( |
| 1187 | type, rtc::dchecked_cast<uint8_t>(id)); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1188 | } |
| 1189 | return error; |
| 1190 | } |
| 1191 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1192 | int64_t ChannelSend::GetRTT() const { |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 1193 | if (media_transport_) { |
| 1194 | // GetRTT is generally used in the RTCP codepath, where media transport is |
| 1195 | // not present and so it shouldn't be needed. But it's also invoked in |
| 1196 | // 'GetStats' method, and for now returning media transport RTT here gives |
| 1197 | // us "free" rtt stats for media transport. |
| 1198 | auto target_rate = media_transport_->GetLatestTargetTransferRate(); |
| 1199 | if (target_rate.has_value()) { |
| 1200 | return target_rate.value().network_estimate.round_trip_time.ms(); |
| 1201 | } |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1205 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 1206 | if (method == RtcpMode::kOff) { |
| 1207 | return 0; |
| 1208 | } |
| 1209 | std::vector<RTCPReportBlock> report_blocks; |
| 1210 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 1211 | |
| 1212 | if (report_blocks.empty()) { |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | int64_t rtt = 0; |
| 1217 | int64_t avg_rtt = 0; |
| 1218 | int64_t max_rtt = 0; |
| 1219 | int64_t min_rtt = 0; |
| 1220 | // We don't know in advance the remote ssrc used by the other end's receiver |
| 1221 | // reports, so use the SSRC of the first report block for calculating the RTT. |
| 1222 | if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt, |
| 1223 | &min_rtt, &max_rtt) != 0) { |
| 1224 | return 0; |
| 1225 | } |
| 1226 | return rtt; |
| 1227 | } |
| 1228 | |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 1229 | void ChannelSend::SetFrameEncryptor( |
| 1230 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1231 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1232 | rtc::CritScope cs(&encoder_queue_lock_); |
| 1233 | if (encoder_queue_is_active_) { |
| 1234 | encoder_queue_->PostTask([this, frame_encryptor]() { |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 1235 | this->frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1236 | }); |
| 1237 | } else { |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 1238 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 1242 | void ChannelSend::OnTargetTransferRate(TargetTransferRate rate) { |
| 1243 | RTC_DCHECK(media_transport_); |
| 1244 | OnReceivedRtt(rate.network_estimate.round_trip_time.ms()); |
| 1245 | } |
| 1246 | |
| 1247 | void ChannelSend::OnReceivedRtt(int64_t rtt_ms) { |
| 1248 | // Invoke audio encoders OnReceivedRtt(). |
| 1249 | audio_coding_->ModifyEncoder( |
| 1250 | [rtt_ms](std::unique_ptr<AudioEncoder>* encoder) { |
| 1251 | if (*encoder) { |
| 1252 | (*encoder)->OnReceivedRtt(rtt_ms); |
| 1253 | } |
| 1254 | }); |
| 1255 | } |
| 1256 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1257 | } // namespace |
| 1258 | |
| 1259 | std::unique_ptr<ChannelSendInterface> CreateChannelSend( |
| 1260 | rtc::TaskQueue* encoder_queue, |
| 1261 | ProcessThread* module_process_thread, |
| 1262 | MediaTransportInterface* media_transport, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 1263 | Transport* rtp_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1264 | RtcpRttStats* rtcp_rtt_stats, |
| 1265 | RtcEventLog* rtc_event_log, |
| 1266 | FrameEncryptorInterface* frame_encryptor, |
| 1267 | const webrtc::CryptoOptions& crypto_options, |
| 1268 | bool extmap_allow_mixed, |
| 1269 | int rtcp_report_interval_ms) { |
| 1270 | return absl::make_unique<ChannelSend>( |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 1271 | encoder_queue, module_process_thread, media_transport, rtp_transport, |
| 1272 | rtcp_rtt_stats, rtc_event_log, frame_encryptor, crypto_options, |
| 1273 | extmap_allow_mixed, rtcp_report_interval_ms); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1276 | } // namespace voe |
| 1277 | } // namespace webrtc |