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