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