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