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