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 | // TODO(sukhanov): Overhead observer is only needed for RTP path, because in |
| 692 | // media transport audio overhead is currently considered constant (see |
| 693 | // getter MediaTransportInterface::GetAudioPacketOverhead). In the future |
| 694 | // when we introduce RTP media transport we should make audio overhead |
| 695 | // interface consistent and work for both RTP and non-RTP implementations. |
| 696 | configuration.overhead_observer = overhead_observer; |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 697 | configuration.bandwidth_callback = rtcp_observer_.get(); |
| 698 | configuration.transport_feedback_callback = feedback_observer_proxy_.get(); |
| 699 | } |
| 700 | |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 701 | configuration.clock = clock; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 702 | configuration.audio = true; |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 703 | configuration.clock = Clock::GetRealTimeClock(); |
Fredrik Solenberg | 3d2ed19 | 2018-12-18 09:18:33 +0100 | [diff] [blame] | 704 | configuration.outgoing_transport = rtp_transport; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 705 | |
| 706 | configuration.paced_sender = rtp_packet_sender_proxy_.get(); |
| 707 | configuration.transport_sequence_number_allocator = |
| 708 | seq_num_allocator_proxy_.get(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 709 | |
| 710 | configuration.event_log = event_log_; |
| 711 | configuration.rtt_stats = rtcp_rtt_stats; |
| 712 | configuration.retransmission_rate_limiter = |
| 713 | retransmission_rate_limiter_.get(); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 714 | configuration.extmap_allow_mixed = extmap_allow_mixed; |
Jiawei Ou | 8b5d9d8 | 2018-11-15 16:44:37 -0800 | [diff] [blame] | 715 | configuration.rtcp_report_interval_ms = rtcp_report_interval_ms; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 716 | |
Danil Chapovalov | c44f6cc | 2019-03-06 11:31:09 +0100 | [diff] [blame] | 717 | _rtpRtcpModule = RtpRtcp::Create(configuration); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 718 | _rtpRtcpModule->SetSendingMediaStatus(false); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 719 | |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 720 | rtp_sender_audio_ = absl::make_unique<RTPSenderAudio>( |
| 721 | configuration.clock, _rtpRtcpModule->RtpSender()); |
| 722 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 723 | // We want to invoke the 'TargetRateObserver' and |OnOverheadChanged| |
| 724 | // callbacks after the audio_coding_ is fully initialized. |
| 725 | if (media_transport_) { |
| 726 | RTC_DLOG(LS_INFO) << "Setting media_transport_ rate observers."; |
| 727 | media_transport_->AddTargetTransferRateObserver(this); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 728 | } else { |
| 729 | RTC_DLOG(LS_INFO) << "Not setting media_transport_ rate observers."; |
| 730 | } |
| 731 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 732 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 733 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 734 | // Ensure that RTCP is enabled by default for the created channel. |
| 735 | // Note that, the module will keep generating RTCP until it is explicitly |
| 736 | // disabled by the user. |
| 737 | // After StopListen (when no sockets exists), RTCP packets will no longer |
| 738 | // be transmitted since the Transport object will then be invalid. |
| 739 | // RTCP is enabled by default. |
| 740 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 741 | |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 742 | int error = audio_coding_->RegisterTransportCallback(this); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 743 | RTC_DCHECK_EQ(0, error); |
| 744 | } |
| 745 | |
Fredrik Solenberg | 645a3af | 2018-11-16 12:51:15 +0100 | [diff] [blame] | 746 | ChannelSend::~ChannelSend() { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 747 | RTC_DCHECK(construction_thread_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 748 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 749 | if (media_transport_) { |
| 750 | media_transport_->RemoveTargetTransferRateObserver(this); |
| 751 | } |
| 752 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 753 | StopSend(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 754 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 755 | RTC_DCHECK_EQ(0, error); |
| 756 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 757 | if (_moduleProcessThreadPtr) |
| 758 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 761 | void ChannelSend::StartSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 762 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 763 | RTC_DCHECK(!sending_); |
| 764 | sending_ = true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 765 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 766 | _rtpRtcpModule->SetSendingMediaStatus(true); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 767 | int ret = _rtpRtcpModule->SetSendingStatus(true); |
| 768 | RTC_DCHECK_EQ(0, ret); |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 769 | { |
| 770 | // It is now OK to start posting tasks to the encoder task queue. |
| 771 | rtc::CritScope cs(&encoder_queue_lock_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 772 | encoder_queue_is_active_ = true; |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 773 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void ChannelSend::StopSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 777 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 778 | if (!sending_) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 779 | return; |
| 780 | } |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 781 | sending_ = false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 782 | |
| 783 | // Post a task to the encoder thread which sets an event when the task is |
| 784 | // executed. We know that no more encoding tasks will be added to the task |
| 785 | // queue for this channel since sending is now deactivated. It means that, |
| 786 | // if we wait for the event to bet set, we know that no more pending tasks |
| 787 | // exists and it is therfore guaranteed that the task queue will never try |
| 788 | // to acccess and invalid channel object. |
| 789 | RTC_DCHECK(encoder_queue_); |
| 790 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 791 | rtc::Event flush; |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 792 | { |
| 793 | // Clear |encoder_queue_is_active_| under lock to prevent any other tasks |
| 794 | // than this final "flush task" to be posted on the queue. |
| 795 | rtc::CritScope cs(&encoder_queue_lock_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 796 | encoder_queue_is_active_ = false; |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 797 | encoder_queue_->PostTask([&flush]() { flush.Set(); }); |
| 798 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 799 | flush.Wait(rtc::Event::kForever); |
| 800 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 801 | // Reset sending SSRC and sequence number and triggers direct transmission |
| 802 | // of RTCP BYE |
| 803 | if (_rtpRtcpModule->SetSendingStatus(false) == -1) { |
| 804 | RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending"; |
| 805 | } |
| 806 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 807 | } |
| 808 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 809 | void ChannelSend::SetEncoder(int payload_type, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 810 | std::unique_ptr<AudioEncoder> encoder) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 811 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 812 | RTC_DCHECK_GE(payload_type, 0); |
| 813 | RTC_DCHECK_LE(payload_type, 127); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 814 | |
| 815 | // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate) |
| 816 | // 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] | 817 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, |
| 818 | encoder->RtpTimestampRateHz()); |
| 819 | rtp_sender_audio_->RegisterAudioPayload("audio", payload_type, |
| 820 | encoder->RtpTimestampRateHz(), |
| 821 | encoder->NumChannels(), 0); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 822 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 823 | if (media_transport_) { |
| 824 | rtc::CritScope cs(&media_transport_lock_); |
| 825 | media_transport_payload_type_ = payload_type; |
| 826 | // TODO(nisse): Currently broken for G722, since timestamps passed through |
| 827 | // encoder use RTP clock rather than sample count, and they differ for G722. |
| 828 | media_transport_sampling_frequency_ = encoder->RtpTimestampRateHz(); |
| 829 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 830 | audio_coding_->SetEncoder(std::move(encoder)); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void ChannelSend::ModifyEncoder( |
| 834 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 835 | // This method can be called on the worker thread, module process thread |
| 836 | // or network thread. Audio coding is thread safe, so we do not need to |
| 837 | // enforce the calling thread. |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 838 | audio_coding_->ModifyEncoder(modifier); |
| 839 | } |
| 840 | |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 841 | void ChannelSend::CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) { |
| 842 | ModifyEncoder([modifier](std::unique_ptr<AudioEncoder>* encoder_ptr) { |
| 843 | if (*encoder_ptr) { |
| 844 | modifier(encoder_ptr->get()); |
| 845 | } else { |
| 846 | RTC_DLOG(LS_WARNING) << "Trying to call unset encoder."; |
| 847 | } |
| 848 | }); |
| 849 | } |
| 850 | |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 851 | void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 852 | // This method can be called on the worker thread, module process thread |
| 853 | // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged. |
| 854 | // TODO(solenberg): Figure out a good way to check this or enforce calling |
| 855 | // rules. |
| 856 | // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() || |
| 857 | // module_process_thread_checker_.CalledOnValidThread()); |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 858 | rtc::CritScope lock(&bitrate_crit_section_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 859 | |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 860 | CallEncoder([&](AudioEncoder* encoder) { |
| 861 | encoder->OnReceivedUplinkAllocation(update); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 862 | }); |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 863 | retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps()); |
| 864 | configured_bitrate_bps_ = update.target_bitrate.bps(); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 867 | int ChannelSend::GetBitrate() const { |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 868 | rtc::CritScope lock(&bitrate_crit_section_); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 869 | return configured_bitrate_bps_; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 873 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 874 | if (!use_twcc_plr_for_ana_) |
| 875 | return; |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 876 | CallEncoder([&](AudioEncoder* encoder) { |
| 877 | encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 878 | }); |
| 879 | } |
| 880 | |
| 881 | void ChannelSend::OnRecoverableUplinkPacketLossRate( |
| 882 | float recoverable_packet_loss_rate) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 883 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 884 | CallEncoder([&](AudioEncoder* encoder) { |
| 885 | encoder->OnReceivedUplinkRecoverablePacketLossFraction( |
| 886 | recoverable_packet_loss_rate); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 887 | }); |
| 888 | } |
| 889 | |
| 890 | void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) { |
| 891 | if (use_twcc_plr_for_ana_) |
| 892 | return; |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 893 | CallEncoder([&](AudioEncoder* encoder) { |
| 894 | encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 895 | }); |
| 896 | } |
| 897 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 898 | void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 899 | // May be called on either worker thread or network thread. |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 900 | if (media_transport_) { |
| 901 | // Ignore RTCP packets while media transport is used. |
| 902 | // Those packets should not arrive, but we are seeing occasional packets. |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 903 | return; |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 906 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 907 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 908 | |
| 909 | int64_t rtt = GetRTT(); |
| 910 | if (rtt == 0) { |
| 911 | // Waiting for valid RTT. |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 912 | return; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | int64_t nack_window_ms = rtt; |
| 916 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 917 | nack_window_ms = kMinRetransmissionWindowMs; |
| 918 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 919 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 920 | } |
| 921 | retransmission_rate_limiter_->SetWindowSize(nack_window_ms); |
| 922 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 923 | OnReceivedRtt(rtt); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void ChannelSend::SetInputMute(bool enable) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 927 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 928 | rtc::CritScope cs(&volume_settings_critsect_); |
| 929 | input_mute_ = enable; |
| 930 | } |
| 931 | |
| 932 | bool ChannelSend::InputMute() const { |
| 933 | rtc::CritScope cs(&volume_settings_critsect_); |
| 934 | return input_mute_; |
| 935 | } |
| 936 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 937 | bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 938 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 939 | RTC_DCHECK_LE(0, event); |
| 940 | RTC_DCHECK_GE(255, event); |
| 941 | RTC_DCHECK_LE(0, duration_ms); |
| 942 | RTC_DCHECK_GE(65535, duration_ms); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 943 | if (!sending_) { |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 944 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 945 | } |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 946 | if (rtp_sender_audio_->SendTelephoneEvent( |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 947 | event, duration_ms, kTelephoneEventAttenuationdB) != 0) { |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 948 | RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event"; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 949 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 950 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 951 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 954 | void ChannelSend::RegisterCngPayloadType(int payload_type, |
| 955 | int payload_frequency) { |
| 956 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency); |
| 957 | rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency, |
| 958 | 1, 0); |
| 959 | } |
| 960 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 961 | void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type, |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 962 | int payload_frequency) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 963 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 964 | RTC_DCHECK_LE(0, payload_type); |
| 965 | RTC_DCHECK_GE(127, payload_type); |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 966 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency); |
| 967 | rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type, |
| 968 | payload_frequency, 0, 0); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 971 | void ChannelSend::SetLocalSSRC(uint32_t ssrc) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 972 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 973 | RTC_DCHECK(!sending_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 974 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 975 | if (media_transport_) { |
| 976 | rtc::CritScope cs(&media_transport_lock_); |
| 977 | media_transport_channel_id_ = ssrc; |
| 978 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 979 | _rtpRtcpModule->SetSSRC(ssrc); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 982 | void ChannelSend::SetRid(const std::string& rid, |
| 983 | int extension_id, |
| 984 | int repaired_extension_id) { |
| 985 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
| 986 | if (extension_id != 0) { |
| 987 | int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId, |
| 988 | extension_id); |
| 989 | RTC_DCHECK_EQ(0, ret); |
| 990 | } |
| 991 | if (repaired_extension_id != 0) { |
| 992 | int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId, |
| 993 | repaired_extension_id); |
| 994 | RTC_DCHECK_EQ(0, ret); |
| 995 | } |
| 996 | _rtpRtcpModule->SetRid(rid); |
| 997 | } |
| 998 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 999 | void ChannelSend::SetMid(const std::string& mid, int extension_id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1000 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1001 | int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id); |
| 1002 | RTC_DCHECK_EQ(0, ret); |
| 1003 | _rtpRtcpModule->SetMid(mid); |
| 1004 | } |
| 1005 | |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 1006 | void ChannelSend::SetExtmapAllowMixed(bool extmap_allow_mixed) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1007 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 1008 | _rtpRtcpModule->SetExtmapAllowMixed(extmap_allow_mixed); |
| 1009 | } |
| 1010 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1011 | void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1012 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1013 | _includeAudioLevelIndication = enable; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1014 | int ret = SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id); |
| 1015 | RTC_DCHECK_EQ(0, ret); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | void ChannelSend::EnableSendTransportSequenceNumber(int id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1019 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1020 | int ret = |
| 1021 | SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id); |
| 1022 | RTC_DCHECK_EQ(0, ret); |
| 1023 | } |
| 1024 | |
| 1025 | void ChannelSend::RegisterSenderCongestionControlObjects( |
| 1026 | RtpTransportControllerSendInterface* transport, |
| 1027 | RtcpBandwidthObserver* bandwidth_observer) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1028 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1029 | RtpPacketSender* rtp_packet_sender = transport->packet_sender(); |
| 1030 | TransportFeedbackObserver* transport_feedback_observer = |
| 1031 | transport->transport_feedback_observer(); |
| 1032 | PacketRouter* packet_router = transport->packet_router(); |
| 1033 | |
| 1034 | RTC_DCHECK(rtp_packet_sender); |
| 1035 | RTC_DCHECK(transport_feedback_observer); |
| 1036 | RTC_DCHECK(packet_router); |
| 1037 | RTC_DCHECK(!packet_router_); |
| 1038 | rtcp_observer_->SetBandwidthObserver(bandwidth_observer); |
| 1039 | feedback_observer_proxy_->SetTransportFeedbackObserver( |
| 1040 | transport_feedback_observer); |
| 1041 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router); |
| 1042 | rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender); |
| 1043 | _rtpRtcpModule->SetStorePacketsStatus(true, 600); |
| 1044 | constexpr bool remb_candidate = false; |
| 1045 | packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 1046 | packet_router_ = packet_router; |
| 1047 | } |
| 1048 | |
| 1049 | void ChannelSend::ResetSenderCongestionControlObjects() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1050 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1051 | RTC_DCHECK(packet_router_); |
| 1052 | _rtpRtcpModule->SetStorePacketsStatus(false, 600); |
| 1053 | rtcp_observer_->SetBandwidthObserver(nullptr); |
| 1054 | feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr); |
| 1055 | seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr); |
| 1056 | packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get()); |
| 1057 | packet_router_ = nullptr; |
| 1058 | rtp_packet_sender_proxy_->SetPacketSender(nullptr); |
| 1059 | } |
| 1060 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1061 | void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1062 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1063 | // Note: SetCNAME() accepts a c string of length at most 255. |
| 1064 | const std::string c_name_limited(c_name.substr(0, 255)); |
| 1065 | int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0; |
| 1066 | 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] | 1067 | } |
| 1068 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1069 | std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1070 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1071 | // Get the report blocks from the latest received RTCP Sender or Receiver |
| 1072 | // Report. Each element in the vector contains the sender's SSRC and a |
| 1073 | // report block according to RFC 3550. |
| 1074 | std::vector<RTCPReportBlock> rtcp_report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1075 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1076 | int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks); |
| 1077 | RTC_DCHECK_EQ(0, ret); |
| 1078 | |
| 1079 | std::vector<ReportBlock> report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1080 | |
| 1081 | std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin(); |
| 1082 | for (; it != rtcp_report_blocks.end(); ++it) { |
| 1083 | ReportBlock report_block; |
| 1084 | report_block.sender_SSRC = it->sender_ssrc; |
| 1085 | report_block.source_SSRC = it->source_ssrc; |
| 1086 | report_block.fraction_lost = it->fraction_lost; |
| 1087 | report_block.cumulative_num_packets_lost = it->packets_lost; |
| 1088 | report_block.extended_highest_sequence_number = |
| 1089 | it->extended_highest_sequence_number; |
| 1090 | report_block.interarrival_jitter = it->jitter; |
| 1091 | report_block.last_SR_timestamp = it->last_sender_report_timestamp; |
| 1092 | 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] | 1093 | report_blocks.push_back(report_block); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1094 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1095 | return report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1098 | CallSendStatistics ChannelSend::GetRTCPStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1099 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1100 | CallSendStatistics stats = {0}; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1101 | stats.rttMs = GetRTT(); |
| 1102 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1103 | size_t bytesSent(0); |
| 1104 | uint32_t packetsSent(0); |
| 1105 | |
| 1106 | if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) { |
| 1107 | RTC_DLOG(LS_WARNING) |
| 1108 | << "GetRTPStatistics() failed to retrieve RTP datacounters" |
| 1109 | << " => output will not be complete"; |
| 1110 | } |
| 1111 | |
| 1112 | stats.bytesSent = bytesSent; |
| 1113 | stats.packetsSent = packetsSent; |
| 1114 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1115 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1118 | void ChannelSend::ProcessAndEncodeAudio( |
| 1119 | std::unique_ptr<AudioFrame> audio_frame) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1120 | RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 1121 | // Avoid posting any new tasks if sending was already stopped in StopSend(). |
| 1122 | rtc::CritScope cs(&encoder_queue_lock_); |
| 1123 | if (!encoder_queue_is_active_) { |
| 1124 | return; |
| 1125 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1126 | // Profile time between when the audio frame is added to the task queue and |
| 1127 | // when the task is actually executed. |
| 1128 | audio_frame->UpdateProfileTimeStamp(); |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 1129 | encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>( |
| 1130 | new ProcessAndEncodeAudioTask(std::move(audio_frame), this))); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) { |
| 1134 | RTC_DCHECK_RUN_ON(encoder_queue_); |
| 1135 | RTC_DCHECK_GT(audio_input->samples_per_channel_, 0); |
| 1136 | RTC_DCHECK_LE(audio_input->num_channels_, 2); |
| 1137 | |
| 1138 | // Measure time between when the audio frame is added to the task queue and |
| 1139 | // when the task is actually executed. Goal is to keep track of unwanted |
| 1140 | // extra latency added by the task queue. |
| 1141 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", |
| 1142 | audio_input->ElapsedProfileTimeMs()); |
| 1143 | |
| 1144 | bool is_muted = InputMute(); |
| 1145 | AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted); |
| 1146 | |
| 1147 | if (_includeAudioLevelIndication) { |
| 1148 | size_t length = |
| 1149 | audio_input->samples_per_channel_ * audio_input->num_channels_; |
| 1150 | RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); |
| 1151 | if (is_muted && previous_frame_muted_) { |
| 1152 | rms_level_.AnalyzeMuted(length); |
| 1153 | } else { |
| 1154 | rms_level_.Analyze( |
| 1155 | rtc::ArrayView<const int16_t>(audio_input->data(), length)); |
| 1156 | } |
| 1157 | } |
| 1158 | previous_frame_muted_ = is_muted; |
| 1159 | |
| 1160 | // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. |
| 1161 | |
| 1162 | // The ACM resamples internally. |
| 1163 | audio_input->timestamp_ = _timeStamp; |
| 1164 | // This call will trigger AudioPacketizationCallback::SendData if encoding |
| 1165 | // is done and payload is ready for packetization and transmission. |
| 1166 | // Otherwise, it will return without invoking the callback. |
| 1167 | if (audio_coding_->Add10MsData(*audio_input) < 0) { |
| 1168 | RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed."; |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_); |
| 1173 | } |
| 1174 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1175 | ANAStats ChannelSend::GetANAStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1176 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1177 | return audio_coding_->GetANAStats(); |
| 1178 | } |
| 1179 | |
| 1180 | RtpRtcp* ChannelSend::GetRtpRtcp() const { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1181 | RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1182 | return _rtpRtcpModule.get(); |
| 1183 | } |
| 1184 | |
| 1185 | int ChannelSend::SetSendRtpHeaderExtension(bool enable, |
| 1186 | RTPExtensionType type, |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1187 | int id) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1188 | int error = 0; |
| 1189 | _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type); |
| 1190 | if (enable) { |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 1191 | // TODO(nisse): RtpRtcp::RegisterSendRtpHeaderExtension to take an int |
| 1192 | // argument. Currently it wants an uint8_t. |
| 1193 | error = _rtpRtcpModule->RegisterSendRtpHeaderExtension( |
| 1194 | type, rtc::dchecked_cast<uint8_t>(id)); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1195 | } |
| 1196 | return error; |
| 1197 | } |
| 1198 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1199 | int64_t ChannelSend::GetRTT() const { |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 1200 | if (media_transport_) { |
| 1201 | // GetRTT is generally used in the RTCP codepath, where media transport is |
| 1202 | // not present and so it shouldn't be needed. But it's also invoked in |
| 1203 | // 'GetStats' method, and for now returning media transport RTT here gives |
| 1204 | // us "free" rtt stats for media transport. |
| 1205 | auto target_rate = media_transport_->GetLatestTargetTransferRate(); |
| 1206 | if (target_rate.has_value()) { |
| 1207 | return target_rate.value().network_estimate.round_trip_time.ms(); |
| 1208 | } |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1212 | RtcpMode method = _rtpRtcpModule->RTCP(); |
| 1213 | if (method == RtcpMode::kOff) { |
| 1214 | return 0; |
| 1215 | } |
| 1216 | std::vector<RTCPReportBlock> report_blocks; |
| 1217 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 1218 | |
| 1219 | if (report_blocks.empty()) { |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | int64_t rtt = 0; |
| 1224 | int64_t avg_rtt = 0; |
| 1225 | int64_t max_rtt = 0; |
| 1226 | int64_t min_rtt = 0; |
| 1227 | // We don't know in advance the remote ssrc used by the other end's receiver |
| 1228 | // reports, so use the SSRC of the first report block for calculating the RTT. |
| 1229 | if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt, |
| 1230 | &min_rtt, &max_rtt) != 0) { |
| 1231 | return 0; |
| 1232 | } |
| 1233 | return rtt; |
| 1234 | } |
| 1235 | |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 1236 | void ChannelSend::SetFrameEncryptor( |
| 1237 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 1238 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 1239 | rtc::CritScope cs(&encoder_queue_lock_); |
| 1240 | if (encoder_queue_is_active_) { |
Mirko Bonadei | 80a8687 | 2019-02-04 15:01:43 +0100 | [diff] [blame] | 1241 | encoder_queue_->PostTask([this, frame_encryptor]() mutable { |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 1242 | this->frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1243 | }); |
| 1244 | } else { |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 1245 | frame_encryptor_ = std::move(frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 1249 | // TODO(sukhanov): Consider moving TargetTransferRate observer to |
| 1250 | // AudioSendStream. Since AudioSendStream owns encoder and configures ANA, it |
| 1251 | // makes sense to consolidate all rate (and overhead) calculation there. |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 1252 | void ChannelSend::OnTargetTransferRate(TargetTransferRate rate) { |
| 1253 | RTC_DCHECK(media_transport_); |
| 1254 | OnReceivedRtt(rate.network_estimate.round_trip_time.ms()); |
| 1255 | } |
| 1256 | |
| 1257 | void ChannelSend::OnReceivedRtt(int64_t rtt_ms) { |
| 1258 | // Invoke audio encoders OnReceivedRtt(). |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 1259 | CallEncoder( |
| 1260 | [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); }); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1263 | } // namespace |
| 1264 | |
| 1265 | std::unique_ptr<ChannelSendInterface> CreateChannelSend( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 1266 | Clock* clock, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1267 | rtc::TaskQueue* encoder_queue, |
| 1268 | ProcessThread* module_process_thread, |
| 1269 | MediaTransportInterface* media_transport, |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 1270 | OverheadObserver* overhead_observer, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 1271 | Transport* rtp_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1272 | RtcpRttStats* rtcp_rtt_stats, |
| 1273 | RtcEventLog* rtc_event_log, |
| 1274 | FrameEncryptorInterface* frame_encryptor, |
| 1275 | const webrtc::CryptoOptions& crypto_options, |
| 1276 | bool extmap_allow_mixed, |
| 1277 | int rtcp_report_interval_ms) { |
| 1278 | return absl::make_unique<ChannelSend>( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 1279 | clock, encoder_queue, module_process_thread, media_transport, |
| 1280 | overhead_observer, rtp_transport, rtcp_rtt_stats, rtc_event_log, |
| 1281 | frame_encryptor, crypto_options, extmap_allow_mixed, |
| 1282 | rtcp_report_interval_ms); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1285 | } // namespace voe |
| 1286 | } // namespace webrtc |