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