Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "audio/channel_send.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 20 | #include "api/array_view.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 21 | #include "api/call/transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "api/crypto/frame_encryptor_interface.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 23 | #include "api/rtc_event_log/rtc_event_log.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 24 | #include "audio/utility/audio_frame_operations.h" |
| 25 | #include "call/rtp_transport_controller_send_interface.h" |
| 26 | #include "logging/rtc_event_log/events/rtc_event_audio_playout.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 27 | #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 28 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 29 | #include "modules/audio_processing/rms_level.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 30 | #include "modules/pacing/packet_router.h" |
| 31 | #include "modules/utility/include/process_thread.h" |
| 32 | #include "rtc_base/checks.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 33 | #include "rtc_base/event.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 34 | #include "rtc_base/format_macros.h" |
| 35 | #include "rtc_base/location.h" |
| 36 | #include "rtc_base/logging.h" |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 37 | #include "rtc_base/numerics/safe_conversions.h" |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 38 | #include "rtc_base/race_checker.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 39 | #include "rtc_base/rate_limiter.h" |
| 40 | #include "rtc_base/task_queue.h" |
| 41 | #include "rtc_base/thread_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 42 | #include "rtc_base/time_utils.h" |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 43 | #include "system_wrappers/include/clock.h" |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 44 | #include "system_wrappers/include/field_trial.h" |
| 45 | #include "system_wrappers/include/metrics.h" |
| 46 | |
| 47 | namespace webrtc { |
| 48 | namespace voe { |
| 49 | |
| 50 | namespace { |
| 51 | |
| 52 | constexpr int64_t kMaxRetransmissionWindowMs = 1000; |
| 53 | constexpr int64_t kMinRetransmissionWindowMs = 30; |
| 54 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 55 | class RtpPacketSenderProxy; |
| 56 | class TransportFeedbackProxy; |
| 57 | class TransportSequenceNumberProxy; |
| 58 | class VoERtcpObserver; |
| 59 | |
Benjamin Wright | 17b050f | 2019-03-13 17:35:46 -0700 | [diff] [blame] | 60 | class ChannelSend : public ChannelSendInterface, |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 61 | public AudioPacketizationCallback { // receive encoded |
| 62 | // packets from the ACM |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 63 | public: |
| 64 | // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend |
| 65 | // declaration. |
| 66 | friend class VoERtcpObserver; |
| 67 | |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 68 | ChannelSend(Clock* clock, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 69 | TaskQueueFactory* task_queue_factory, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 70 | ProcessThread* module_process_thread, |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 71 | OverheadObserver* overhead_observer, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 72 | Transport* rtp_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 73 | RtcpRttStats* rtcp_rtt_stats, |
| 74 | RtcEventLog* rtc_event_log, |
| 75 | FrameEncryptorInterface* frame_encryptor, |
| 76 | const webrtc::CryptoOptions& crypto_options, |
| 77 | bool extmap_allow_mixed, |
Erik Språng | 4c2c412 | 2019-07-11 15:20:15 +0200 | [diff] [blame] | 78 | int rtcp_report_interval_ms, |
| 79 | uint32_t ssrc); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 80 | |
| 81 | ~ChannelSend() override; |
| 82 | |
| 83 | // Send using this encoder, with this payload type. |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 84 | void SetEncoder(int payload_type, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 85 | std::unique_ptr<AudioEncoder> encoder) override; |
| 86 | void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> |
| 87 | modifier) override; |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 88 | void CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 89 | |
| 90 | // API methods |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 91 | void StartSend() override; |
| 92 | void StopSend() override; |
| 93 | |
| 94 | // Codecs |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 95 | void OnBitrateAllocation(BitrateAllocationUpdate update) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 96 | int GetBitrate() const override; |
| 97 | |
| 98 | // Network |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 99 | void ReceivedRTCPPacket(const uint8_t* data, size_t length) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 100 | |
| 101 | // Muting, Volume and Level. |
| 102 | void SetInputMute(bool enable) override; |
| 103 | |
| 104 | // Stats. |
| 105 | ANAStats GetANAStatistics() const override; |
| 106 | |
| 107 | // Used by AudioSendStream. |
| 108 | RtpRtcp* GetRtpRtcp() const override; |
| 109 | |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 110 | void RegisterCngPayloadType(int payload_type, int payload_frequency) override; |
| 111 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 112 | // DTMF. |
| 113 | bool SendTelephoneEventOutband(int event, int duration_ms) override; |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 114 | void SetSendTelephoneEventPayloadType(int payload_type, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 115 | int payload_frequency) override; |
| 116 | |
| 117 | // RTP+RTCP |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 118 | void SetSendAudioLevelIndicationStatus(bool enable, int id) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 119 | |
| 120 | void RegisterSenderCongestionControlObjects( |
| 121 | RtpTransportControllerSendInterface* transport, |
| 122 | RtcpBandwidthObserver* bandwidth_observer) override; |
| 123 | void ResetSenderCongestionControlObjects() override; |
| 124 | void SetRTCP_CNAME(absl::string_view c_name) override; |
| 125 | std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const override; |
| 126 | CallSendStatistics GetRTCPStatistics() const override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 127 | |
| 128 | // ProcessAndEncodeAudio() posts a task on the shared encoder task queue, |
| 129 | // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where |
| 130 | // the actual processing of the audio takes place. The processing mainly |
| 131 | // consists of encoding and preparing the result for sending by adding it to a |
| 132 | // send queue. |
| 133 | // The main reason for using a task queue here is to release the native, |
| 134 | // OS-specific, audio capture thread as soon as possible to ensure that it |
| 135 | // can go back to sleep and be prepared to deliver an new captured audio |
| 136 | // packet. |
| 137 | void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) override; |
| 138 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 139 | int64_t GetRTT() const override; |
| 140 | |
| 141 | // E2EE Custom Audio Frame Encryption |
| 142 | void SetFrameEncryptor( |
| 143 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override; |
| 144 | |
| 145 | private: |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 146 | // From AudioPacketizationCallback in the ACM |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 147 | int32_t SendData(AudioFrameType frameType, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 148 | uint8_t payloadType, |
| 149 | uint32_t timeStamp, |
| 150 | const uint8_t* payloadData, |
Niels Möller | c35b6e6 | 2019-04-25 16:31:18 +0200 | [diff] [blame] | 151 | size_t payloadSize) override; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 152 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 153 | void OnUplinkPacketLossRate(float packet_loss_rate); |
| 154 | bool InputMute() const; |
| 155 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 156 | int32_t SendRtpAudio(AudioFrameType frameType, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 157 | uint8_t payloadType, |
| 158 | uint32_t timeStamp, |
Niels Möller | c35b6e6 | 2019-04-25 16:31:18 +0200 | [diff] [blame] | 159 | rtc::ArrayView<const uint8_t> payload) |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 160 | RTC_RUN_ON(encoder_queue_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 161 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 162 | void OnReceivedRtt(int64_t rtt_ms); |
| 163 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 164 | // Thread checkers document and lock usage of some methods on voe::Channel to |
| 165 | // specific threads we know about. The goal is to eventually split up |
| 166 | // voe::Channel into parts with single-threaded semantics, and thereby reduce |
| 167 | // the need for locks. |
| 168 | rtc::ThreadChecker worker_thread_checker_; |
| 169 | rtc::ThreadChecker module_process_thread_checker_; |
| 170 | // Methods accessed from audio and video threads are checked for sequential- |
| 171 | // only access. We don't necessarily own and control these threads, so thread |
| 172 | // checkers cannot be used. E.g. Chromium may transfer "ownership" from one |
| 173 | // audio thread to another, but access is still sequential. |
| 174 | rtc::RaceChecker audio_thread_race_checker_; |
| 175 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 176 | rtc::CriticalSection volume_settings_critsect_; |
| 177 | |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 178 | bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 179 | |
| 180 | RtcEventLog* const event_log_; |
| 181 | |
| 182 | std::unique_ptr<RtpRtcp> _rtpRtcpModule; |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 183 | std::unique_ptr<RTPSenderAudio> rtp_sender_audio_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 184 | |
| 185 | std::unique_ptr<AudioCodingModule> audio_coding_; |
| 186 | uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_); |
| 187 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 188 | // uses |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 189 | ProcessThread* const _moduleProcessThreadPtr; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 190 | RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_); |
| 191 | bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_); |
| 192 | bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_); |
| 193 | // VoeRTP_RTCP |
| 194 | // TODO(henrika): can today be accessed on the main thread and on the |
| 195 | // task queue; hence potential race. |
| 196 | bool _includeAudioLevelIndication; |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 197 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 198 | // RtcpBandwidthObserver |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 199 | const std::unique_ptr<VoERtcpObserver> rtcp_observer_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 200 | |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 201 | PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) = |
| 202 | nullptr; |
| 203 | const std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_; |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 204 | const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_pacer_proxy_; |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 205 | const std::unique_ptr<RateLimiter> retransmission_rate_limiter_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 206 | |
| 207 | rtc::ThreadChecker construction_thread_; |
| 208 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 209 | |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 210 | bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_) = false; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 211 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 212 | // E2EE Audio Frame Encryption |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 213 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_ |
| 214 | RTC_GUARDED_BY(encoder_queue_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 215 | // E2EE Frame Encryption Options |
Niels Möller | 985a1f3 | 2018-11-19 16:08:42 +0100 | [diff] [blame] | 216 | const webrtc::CryptoOptions crypto_options_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 217 | |
| 218 | rtc::CriticalSection bitrate_crit_section_; |
| 219 | int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0; |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 220 | |
| 221 | // Defined last to ensure that there are no running tasks when the other |
| 222 | // members are destroyed. |
| 223 | rtc::TaskQueue encoder_queue_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 224 | }; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 225 | |
| 226 | const int kTelephoneEventAttenuationdB = 10; |
| 227 | |
| 228 | class TransportFeedbackProxy : public TransportFeedbackObserver { |
| 229 | public: |
| 230 | TransportFeedbackProxy() : feedback_observer_(nullptr) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 231 | pacer_thread_.Detach(); |
| 232 | network_thread_.Detach(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void SetTransportFeedbackObserver( |
| 236 | TransportFeedbackObserver* feedback_observer) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 237 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 238 | rtc::CritScope lock(&crit_); |
| 239 | feedback_observer_ = feedback_observer; |
| 240 | } |
| 241 | |
| 242 | // Implements TransportFeedbackObserver. |
Erik Språng | 30a276b | 2019-04-23 12:00:11 +0200 | [diff] [blame] | 243 | void OnAddPacket(const RtpPacketSendInfo& packet_info) override { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 244 | RTC_DCHECK(pacer_thread_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 245 | rtc::CritScope lock(&crit_); |
| 246 | if (feedback_observer_) |
Erik Språng | 30a276b | 2019-04-23 12:00:11 +0200 | [diff] [blame] | 247 | feedback_observer_->OnAddPacket(packet_info); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 251 | RTC_DCHECK(network_thread_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 252 | rtc::CritScope lock(&crit_); |
| 253 | if (feedback_observer_) |
| 254 | feedback_observer_->OnTransportFeedback(feedback); |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | rtc::CriticalSection crit_; |
| 259 | rtc::ThreadChecker thread_checker_; |
| 260 | rtc::ThreadChecker pacer_thread_; |
| 261 | rtc::ThreadChecker network_thread_; |
| 262 | TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_); |
| 263 | }; |
| 264 | |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 265 | class RtpPacketSenderProxy : public RtpPacketSender { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 266 | public: |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 267 | RtpPacketSenderProxy() : rtp_packet_pacer_(nullptr) {} |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 268 | |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 269 | void SetPacketPacer(RtpPacketSender* rtp_packet_pacer) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 270 | RTC_DCHECK(thread_checker_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 271 | rtc::CritScope lock(&crit_); |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 272 | rtp_packet_pacer_ = rtp_packet_pacer; |
| 273 | } |
| 274 | |
Erik Språng | ea55b08 | 2019-10-02 14:57:46 +0200 | [diff] [blame] | 275 | void EnqueuePackets( |
| 276 | std::vector<std::unique_ptr<RtpPacketToSend>> packets) override { |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 277 | rtc::CritScope lock(&crit_); |
Erik Språng | ea55b08 | 2019-10-02 14:57:46 +0200 | [diff] [blame] | 278 | rtp_packet_pacer_->EnqueuePackets(std::move(packets)); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 281 | private: |
| 282 | rtc::ThreadChecker thread_checker_; |
| 283 | rtc::CriticalSection crit_; |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 284 | RtpPacketSender* rtp_packet_pacer_ RTC_GUARDED_BY(&crit_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | class VoERtcpObserver : public RtcpBandwidthObserver { |
| 288 | public: |
| 289 | explicit VoERtcpObserver(ChannelSend* owner) |
| 290 | : owner_(owner), bandwidth_observer_(nullptr) {} |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 291 | ~VoERtcpObserver() override {} |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 292 | |
| 293 | void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) { |
| 294 | rtc::CritScope lock(&crit_); |
| 295 | bandwidth_observer_ = bandwidth_observer; |
| 296 | } |
| 297 | |
| 298 | void OnReceivedEstimatedBitrate(uint32_t bitrate) override { |
| 299 | rtc::CritScope lock(&crit_); |
| 300 | if (bandwidth_observer_) { |
| 301 | bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks, |
| 306 | int64_t rtt, |
| 307 | int64_t now_ms) override { |
| 308 | { |
| 309 | rtc::CritScope lock(&crit_); |
| 310 | if (bandwidth_observer_) { |
| 311 | bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt, |
| 312 | now_ms); |
| 313 | } |
| 314 | } |
| 315 | // TODO(mflodman): Do we need to aggregate reports here or can we jut send |
| 316 | // what we get? I.e. do we ever get multiple reports bundled into one RTCP |
| 317 | // report for VoiceEngine? |
| 318 | if (report_blocks.empty()) |
| 319 | return; |
| 320 | |
| 321 | int fraction_lost_aggregate = 0; |
| 322 | int total_number_of_packets = 0; |
| 323 | |
| 324 | // If receiving multiple report blocks, calculate the weighted average based |
| 325 | // on the number of packets a report refers to. |
| 326 | for (ReportBlockList::const_iterator block_it = report_blocks.begin(); |
| 327 | block_it != report_blocks.end(); ++block_it) { |
| 328 | // Find the previous extended high sequence number for this remote SSRC, |
| 329 | // to calculate the number of RTP packets this report refers to. Ignore if |
| 330 | // we haven't seen this SSRC before. |
| 331 | std::map<uint32_t, uint32_t>::iterator seq_num_it = |
| 332 | extended_max_sequence_number_.find(block_it->source_ssrc); |
| 333 | int number_of_packets = 0; |
| 334 | if (seq_num_it != extended_max_sequence_number_.end()) { |
| 335 | number_of_packets = |
| 336 | block_it->extended_highest_sequence_number - seq_num_it->second; |
| 337 | } |
| 338 | fraction_lost_aggregate += number_of_packets * block_it->fraction_lost; |
| 339 | total_number_of_packets += number_of_packets; |
| 340 | |
| 341 | extended_max_sequence_number_[block_it->source_ssrc] = |
| 342 | block_it->extended_highest_sequence_number; |
| 343 | } |
| 344 | int weighted_fraction_lost = 0; |
| 345 | if (total_number_of_packets > 0) { |
| 346 | weighted_fraction_lost = |
| 347 | (fraction_lost_aggregate + total_number_of_packets / 2) / |
| 348 | total_number_of_packets; |
| 349 | } |
| 350 | owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f); |
| 351 | } |
| 352 | |
| 353 | private: |
| 354 | ChannelSend* owner_; |
| 355 | // Maps remote side ssrc to extended highest sequence number received. |
| 356 | std::map<uint32_t, uint32_t> extended_max_sequence_number_; |
| 357 | rtc::CriticalSection crit_; |
| 358 | RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_); |
| 359 | }; |
| 360 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 361 | int32_t ChannelSend::SendData(AudioFrameType frameType, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 362 | uint8_t payloadType, |
| 363 | uint32_t timeStamp, |
| 364 | const uint8_t* payloadData, |
Niels Möller | c35b6e6 | 2019-04-25 16:31:18 +0200 | [diff] [blame] | 365 | size_t payloadSize) { |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 366 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 367 | rtc::ArrayView<const uint8_t> payload(payloadData, payloadSize); |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 368 | return SendRtpAudio(frameType, payloadType, timeStamp, payload); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 371 | int32_t ChannelSend::SendRtpAudio(AudioFrameType frameType, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 372 | uint8_t payloadType, |
| 373 | uint32_t timeStamp, |
Niels Möller | c35b6e6 | 2019-04-25 16:31:18 +0200 | [diff] [blame] | 374 | rtc::ArrayView<const uint8_t> payload) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 375 | if (_includeAudioLevelIndication) { |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 376 | // Store current audio level in the RTP sender. |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 377 | // The level will be used in combination with voice-activity state |
| 378 | // (frameType) to add an RTP header extension |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 379 | rtp_sender_audio_->SetAudioLevel(rms_level_.Average()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 382 | // E2EE Custom Audio Frame Encryption (This is optional). |
| 383 | // Keep this buffer around for the lifetime of the send call. |
| 384 | rtc::Buffer encrypted_audio_payload; |
Minyue Li | 9ab520e | 2019-05-28 13:27:40 +0200 | [diff] [blame] | 385 | // We don't invoke encryptor if payload is empty, which means we are to send |
| 386 | // DTMF, or the encoder entered DTX. |
| 387 | // TODO(minyue): see whether DTMF packets should be encrypted or not. In |
| 388 | // current implementation, they are not. |
Minyue Li | f48bca7 | 2019-06-20 23:37:02 +0200 | [diff] [blame] | 389 | if (!payload.empty()) { |
| 390 | if (frame_encryptor_ != nullptr) { |
| 391 | // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline. |
| 392 | // Allocate a buffer to hold the maximum possible encrypted payload. |
| 393 | size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize( |
| 394 | cricket::MEDIA_TYPE_AUDIO, payload.size()); |
| 395 | encrypted_audio_payload.SetSize(max_ciphertext_size); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 396 | |
Minyue Li | f48bca7 | 2019-06-20 23:37:02 +0200 | [diff] [blame] | 397 | // Encrypt the audio payload into the buffer. |
| 398 | size_t bytes_written = 0; |
| 399 | int encrypt_status = frame_encryptor_->Encrypt( |
| 400 | cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(), |
| 401 | /*additional_data=*/nullptr, payload, encrypted_audio_payload, |
| 402 | &bytes_written); |
| 403 | if (encrypt_status != 0) { |
| 404 | RTC_DLOG(LS_ERROR) |
| 405 | << "Channel::SendData() failed encrypt audio payload: " |
| 406 | << encrypt_status; |
| 407 | return -1; |
| 408 | } |
| 409 | // Resize the buffer to the exact number of bytes actually used. |
| 410 | encrypted_audio_payload.SetSize(bytes_written); |
| 411 | // Rewrite the payloadData and size to the new encrypted payload. |
| 412 | payload = encrypted_audio_payload; |
| 413 | } else if (crypto_options_.sframe.require_frame_encryption) { |
| 414 | RTC_DLOG(LS_ERROR) << "Channel::SendData() failed sending audio payload: " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 415 | "A frame encryptor is required but one is not set."; |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 416 | return -1; |
| 417 | } |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 420 | // Push data from ACM to RTP/RTCP-module to deliver audio frame for |
| 421 | // packetization. |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 422 | if (!_rtpRtcpModule->OnSendingRtpFrame(timeStamp, |
| 423 | // Leaving the time when this frame was |
| 424 | // received from the capture device as |
| 425 | // undefined for voice for now. |
| 426 | -1, payloadType, |
| 427 | /*force_sender_report=*/false)) { |
Mirko Bonadei | f2c0818 | 2019-11-27 08:47:51 +0100 | [diff] [blame] | 428 | return -1; |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // RTCPSender has it's own copy of the timestamp offset, added in |
| 432 | // RTCPSender::BuildSR, hence we must not add the in the offset for the above |
| 433 | // call. |
| 434 | // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine |
| 435 | // knowledge of the offset to a single place. |
| 436 | const uint32_t rtp_timestamp = timeStamp + _rtpRtcpModule->StartTimestamp(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 437 | // This call will trigger Transport::SendPacket() from the RTP/RTCP module. |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 438 | if (!rtp_sender_audio_->SendAudio(frameType, payloadType, rtp_timestamp, |
| 439 | payload.data(), payload.size())) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 440 | RTC_DLOG(LS_ERROR) |
| 441 | << "ChannelSend::SendData() failed to send data to RTP/RTCP module"; |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 448 | ChannelSend::ChannelSend(Clock* clock, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 449 | TaskQueueFactory* task_queue_factory, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 450 | ProcessThread* module_process_thread, |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 451 | OverheadObserver* overhead_observer, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 452 | Transport* rtp_transport, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 453 | RtcpRttStats* rtcp_rtt_stats, |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 454 | RtcEventLog* rtc_event_log, |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 455 | FrameEncryptorInterface* frame_encryptor, |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 456 | const webrtc::CryptoOptions& crypto_options, |
Jiawei Ou | 5571812 | 2018-11-09 13:17:39 -0800 | [diff] [blame] | 457 | bool extmap_allow_mixed, |
Erik Språng | 4c2c412 | 2019-07-11 15:20:15 +0200 | [diff] [blame] | 458 | int rtcp_report_interval_ms, |
| 459 | uint32_t ssrc) |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 460 | : event_log_(rtc_event_log), |
| 461 | _timeStamp(0), // This is just an offset, RTP module will add it's own |
| 462 | // random offset |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 463 | _moduleProcessThreadPtr(module_process_thread), |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 464 | input_mute_(false), |
| 465 | previous_frame_muted_(false), |
| 466 | _includeAudioLevelIndication(false), |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 467 | rtcp_observer_(new VoERtcpObserver(this)), |
| 468 | feedback_observer_proxy_(new TransportFeedbackProxy()), |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 469 | rtp_packet_pacer_proxy_(new RtpPacketSenderProxy()), |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 470 | retransmission_rate_limiter_( |
| 471 | new RateLimiter(clock, kMaxRetransmissionWindowMs)), |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 472 | frame_encryptor_(frame_encryptor), |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 473 | crypto_options_(crypto_options), |
| 474 | encoder_queue_(task_queue_factory->CreateTaskQueue( |
| 475 | "AudioEncoder", |
| 476 | TaskQueueFactory::Priority::NORMAL)) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 477 | RTC_DCHECK(module_process_thread); |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 478 | module_process_thread_checker_.Detach(); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 479 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 480 | audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config())); |
| 481 | |
| 482 | RtpRtcp::Configuration configuration; |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 483 | configuration.overhead_observer = overhead_observer; |
| 484 | configuration.bandwidth_callback = rtcp_observer_.get(); |
| 485 | configuration.transport_feedback_callback = feedback_observer_proxy_.get(); |
Mirko Bonadei | f2c0818 | 2019-11-27 08:47:51 +0100 | [diff] [blame] | 486 | configuration.clock = (clock ? clock : Clock::GetRealTimeClock()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 487 | configuration.audio = true; |
Fredrik Solenberg | 3d2ed19 | 2018-12-18 09:18:33 +0100 | [diff] [blame] | 488 | configuration.outgoing_transport = rtp_transport; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 489 | |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 490 | configuration.paced_sender = rtp_packet_pacer_proxy_.get(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 491 | |
| 492 | configuration.event_log = event_log_; |
| 493 | configuration.rtt_stats = rtcp_rtt_stats; |
| 494 | configuration.retransmission_rate_limiter = |
| 495 | retransmission_rate_limiter_.get(); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 496 | configuration.extmap_allow_mixed = extmap_allow_mixed; |
Jiawei Ou | 8b5d9d8 | 2018-11-15 16:44:37 -0800 | [diff] [blame] | 497 | configuration.rtcp_report_interval_ms = rtcp_report_interval_ms; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 498 | |
Erik Språng | 54d5d2c | 2019-08-20 17:22:36 +0200 | [diff] [blame] | 499 | configuration.local_media_ssrc = ssrc; |
Erik Språng | 4c2c412 | 2019-07-11 15:20:15 +0200 | [diff] [blame] | 500 | |
Danil Chapovalov | c44f6cc | 2019-03-06 11:31:09 +0100 | [diff] [blame] | 501 | _rtpRtcpModule = RtpRtcp::Create(configuration); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 502 | _rtpRtcpModule->SetSendingMediaStatus(false); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 503 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 504 | rtp_sender_audio_ = std::make_unique<RTPSenderAudio>( |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 505 | configuration.clock, _rtpRtcpModule->RtpSender()); |
| 506 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 507 | _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE); |
| 508 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 509 | // Ensure that RTCP is enabled by default for the created channel. |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 510 | _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound); |
| 511 | |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 512 | int error = audio_coding_->RegisterTransportCallback(this); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 513 | RTC_DCHECK_EQ(0, error); |
| 514 | } |
| 515 | |
Fredrik Solenberg | 645a3af | 2018-11-16 12:51:15 +0100 | [diff] [blame] | 516 | ChannelSend::~ChannelSend() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 517 | RTC_DCHECK(construction_thread_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 518 | |
| 519 | StopSend(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 520 | int error = audio_coding_->RegisterTransportCallback(NULL); |
| 521 | RTC_DCHECK_EQ(0, error); |
| 522 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 523 | if (_moduleProcessThreadPtr) |
| 524 | _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 527 | void ChannelSend::StartSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 528 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 529 | RTC_DCHECK(!sending_); |
| 530 | sending_ = true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 531 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 532 | _rtpRtcpModule->SetSendingMediaStatus(true); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 533 | int ret = _rtpRtcpModule->SetSendingStatus(true); |
| 534 | RTC_DCHECK_EQ(0, ret); |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 535 | // It is now OK to start processing on the encoder task queue. |
| 536 | encoder_queue_.PostTask([this] { |
| 537 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 538 | encoder_queue_is_active_ = true; |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 539 | }); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void ChannelSend::StopSend() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 543 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 544 | if (!sending_) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 545 | return; |
| 546 | } |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 547 | sending_ = false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 548 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 549 | rtc::Event flush; |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 550 | encoder_queue_.PostTask([this, &flush]() { |
| 551 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 552 | encoder_queue_is_active_ = false; |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 553 | flush.Set(); |
| 554 | }); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 555 | flush.Wait(rtc::Event::kForever); |
| 556 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 557 | // Reset sending SSRC and sequence number and triggers direct transmission |
| 558 | // of RTCP BYE |
| 559 | if (_rtpRtcpModule->SetSendingStatus(false) == -1) { |
| 560 | RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending"; |
| 561 | } |
| 562 | _rtpRtcpModule->SetSendingMediaStatus(false); |
| 563 | } |
| 564 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 565 | void ChannelSend::SetEncoder(int payload_type, |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 566 | std::unique_ptr<AudioEncoder> encoder) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 567 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 568 | RTC_DCHECK_GE(payload_type, 0); |
| 569 | RTC_DCHECK_LE(payload_type, 127); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 570 | |
| 571 | // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate) |
| 572 | // 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] | 573 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, |
| 574 | encoder->RtpTimestampRateHz()); |
| 575 | rtp_sender_audio_->RegisterAudioPayload("audio", payload_type, |
| 576 | encoder->RtpTimestampRateHz(), |
| 577 | encoder->NumChannels(), 0); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 578 | |
| 579 | audio_coding_->SetEncoder(std::move(encoder)); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void ChannelSend::ModifyEncoder( |
| 583 | rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) { |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 584 | // This method can be called on the worker thread, module process thread |
| 585 | // or network thread. Audio coding is thread safe, so we do not need to |
| 586 | // enforce the calling thread. |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 587 | audio_coding_->ModifyEncoder(modifier); |
| 588 | } |
| 589 | |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 590 | void ChannelSend::CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) { |
| 591 | ModifyEncoder([modifier](std::unique_ptr<AudioEncoder>* encoder_ptr) { |
| 592 | if (*encoder_ptr) { |
| 593 | modifier(encoder_ptr->get()); |
| 594 | } else { |
| 595 | RTC_DLOG(LS_WARNING) << "Trying to call unset encoder."; |
| 596 | } |
| 597 | }); |
| 598 | } |
| 599 | |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 600 | void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 601 | // This method can be called on the worker thread, module process thread |
| 602 | // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged. |
| 603 | // TODO(solenberg): Figure out a good way to check this or enforce calling |
| 604 | // rules. |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 605 | // RTC_DCHECK(worker_thread_checker_.IsCurrent() || |
| 606 | // module_process_thread_checker_.IsCurrent()); |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 607 | rtc::CritScope lock(&bitrate_crit_section_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 608 | |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 609 | CallEncoder([&](AudioEncoder* encoder) { |
| 610 | encoder->OnReceivedUplinkAllocation(update); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 611 | }); |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 612 | retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps()); |
| 613 | configured_bitrate_bps_ = update.target_bitrate.bps(); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 616 | int ChannelSend::GetBitrate() const { |
Piotr (Peter) Slatala | 1eebec9 | 2018-11-16 09:03:35 -0800 | [diff] [blame] | 617 | rtc::CritScope lock(&bitrate_crit_section_); |
Sebastian Jansson | 359d60a | 2018-10-25 16:22:02 +0200 | [diff] [blame] | 618 | return configured_bitrate_bps_; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 621 | void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) { |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 622 | CallEncoder([&](AudioEncoder* encoder) { |
| 623 | encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 624 | }); |
| 625 | } |
| 626 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 627 | void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 628 | // Deliver RTCP packet to RTP/RTCP module for parsing |
| 629 | _rtpRtcpModule->IncomingRtcpPacket(data, length); |
| 630 | |
| 631 | int64_t rtt = GetRTT(); |
| 632 | if (rtt == 0) { |
| 633 | // Waiting for valid RTT. |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 634 | return; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | int64_t nack_window_ms = rtt; |
| 638 | if (nack_window_ms < kMinRetransmissionWindowMs) { |
| 639 | nack_window_ms = kMinRetransmissionWindowMs; |
| 640 | } else if (nack_window_ms > kMaxRetransmissionWindowMs) { |
| 641 | nack_window_ms = kMaxRetransmissionWindowMs; |
| 642 | } |
| 643 | retransmission_rate_limiter_->SetWindowSize(nack_window_ms); |
| 644 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 645 | OnReceivedRtt(rtt); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void ChannelSend::SetInputMute(bool enable) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 649 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 650 | rtc::CritScope cs(&volume_settings_critsect_); |
| 651 | input_mute_ = enable; |
| 652 | } |
| 653 | |
| 654 | bool ChannelSend::InputMute() const { |
| 655 | rtc::CritScope cs(&volume_settings_critsect_); |
| 656 | return input_mute_; |
| 657 | } |
| 658 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 659 | bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 660 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 661 | RTC_DCHECK_LE(0, event); |
| 662 | RTC_DCHECK_GE(255, event); |
| 663 | RTC_DCHECK_LE(0, duration_ms); |
| 664 | RTC_DCHECK_GE(65535, duration_ms); |
Fredrik Solenberg | eb13484 | 2018-11-19 14:13:15 +0100 | [diff] [blame] | 665 | if (!sending_) { |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 666 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 667 | } |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 668 | if (rtp_sender_audio_->SendTelephoneEvent( |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 669 | event, duration_ms, kTelephoneEventAttenuationdB) != 0) { |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 670 | RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event"; |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 671 | return false; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 672 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 673 | return true; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 676 | void ChannelSend::RegisterCngPayloadType(int payload_type, |
| 677 | int payload_frequency) { |
| 678 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency); |
| 679 | rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency, |
| 680 | 1, 0); |
| 681 | } |
| 682 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 683 | void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type, |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 684 | int payload_frequency) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 685 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 686 | RTC_DCHECK_LE(0, payload_type); |
| 687 | RTC_DCHECK_GE(127, payload_type); |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 688 | _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency); |
| 689 | rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type, |
| 690 | payload_frequency, 0, 0); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 693 | void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 694 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 695 | _includeAudioLevelIndication = enable; |
Sebastian Jansson | 6298b56 | 2020-01-14 17:55:19 +0100 | [diff] [blame^] | 696 | if (enable) { |
| 697 | _rtpRtcpModule->RegisterRtpHeaderExtension(AudioLevel::kUri, id); |
| 698 | } else { |
| 699 | _rtpRtcpModule->DeregisterSendRtpHeaderExtension(AudioLevel::kUri); |
| 700 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | void ChannelSend::RegisterSenderCongestionControlObjects( |
| 704 | RtpTransportControllerSendInterface* transport, |
| 705 | RtcpBandwidthObserver* bandwidth_observer) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 706 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 707 | RtpPacketSender* rtp_packet_pacer = transport->packet_sender(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 708 | TransportFeedbackObserver* transport_feedback_observer = |
| 709 | transport->transport_feedback_observer(); |
| 710 | PacketRouter* packet_router = transport->packet_router(); |
| 711 | |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 712 | RTC_DCHECK(rtp_packet_pacer); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 713 | RTC_DCHECK(transport_feedback_observer); |
| 714 | RTC_DCHECK(packet_router); |
| 715 | RTC_DCHECK(!packet_router_); |
| 716 | rtcp_observer_->SetBandwidthObserver(bandwidth_observer); |
| 717 | feedback_observer_proxy_->SetTransportFeedbackObserver( |
| 718 | transport_feedback_observer); |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 719 | rtp_packet_pacer_proxy_->SetPacketPacer(rtp_packet_pacer); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 720 | _rtpRtcpModule->SetStorePacketsStatus(true, 600); |
| 721 | constexpr bool remb_candidate = false; |
| 722 | packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate); |
| 723 | packet_router_ = packet_router; |
| 724 | } |
| 725 | |
| 726 | void ChannelSend::ResetSenderCongestionControlObjects() { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 727 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 728 | RTC_DCHECK(packet_router_); |
| 729 | _rtpRtcpModule->SetStorePacketsStatus(false, 600); |
| 730 | rtcp_observer_->SetBandwidthObserver(nullptr); |
| 731 | feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 732 | packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get()); |
| 733 | packet_router_ = nullptr; |
Erik Språng | 59b8654 | 2019-06-23 18:24:46 +0200 | [diff] [blame] | 734 | rtp_packet_pacer_proxy_->SetPacketPacer(nullptr); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 737 | void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 738 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 739 | // Note: SetCNAME() accepts a c string of length at most 255. |
| 740 | const std::string c_name_limited(c_name.substr(0, 255)); |
| 741 | int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0; |
| 742 | 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] | 743 | } |
| 744 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 745 | std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 746 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 747 | // Get the report blocks from the latest received RTCP Sender or Receiver |
| 748 | // Report. Each element in the vector contains the sender's SSRC and a |
| 749 | // report block according to RFC 3550. |
| 750 | std::vector<RTCPReportBlock> rtcp_report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 751 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 752 | int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks); |
| 753 | RTC_DCHECK_EQ(0, ret); |
| 754 | |
| 755 | std::vector<ReportBlock> report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 756 | |
| 757 | std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin(); |
| 758 | for (; it != rtcp_report_blocks.end(); ++it) { |
| 759 | ReportBlock report_block; |
| 760 | report_block.sender_SSRC = it->sender_ssrc; |
| 761 | report_block.source_SSRC = it->source_ssrc; |
| 762 | report_block.fraction_lost = it->fraction_lost; |
| 763 | report_block.cumulative_num_packets_lost = it->packets_lost; |
| 764 | report_block.extended_highest_sequence_number = |
| 765 | it->extended_highest_sequence_number; |
| 766 | report_block.interarrival_jitter = it->jitter; |
| 767 | report_block.last_SR_timestamp = it->last_sender_report_timestamp; |
| 768 | 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] | 769 | report_blocks.push_back(report_block); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 770 | } |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 771 | return report_blocks; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 774 | CallSendStatistics ChannelSend::GetRTCPStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 775 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 776 | CallSendStatistics stats = {0}; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 777 | stats.rttMs = GetRTT(); |
| 778 | |
Henrik Boström | cf96e0f | 2019-04-17 13:51:53 +0200 | [diff] [blame] | 779 | StreamDataCounters rtp_stats; |
| 780 | StreamDataCounters rtx_stats; |
| 781 | _rtpRtcpModule->GetSendStreamDataCounters(&rtp_stats, &rtx_stats); |
Niels Möller | ac0a4cb | 2019-10-09 15:01:33 +0200 | [diff] [blame] | 782 | stats.payload_bytes_sent = |
| 783 | rtp_stats.transmitted.payload_bytes + rtx_stats.transmitted.payload_bytes; |
| 784 | stats.header_and_padding_bytes_sent = |
| 785 | rtp_stats.transmitted.padding_bytes + rtp_stats.transmitted.header_bytes + |
| 786 | rtx_stats.transmitted.padding_bytes + rtx_stats.transmitted.header_bytes; |
| 787 | |
Henrik Boström | cf96e0f | 2019-04-17 13:51:53 +0200 | [diff] [blame] | 788 | // TODO(https://crbug.com/webrtc/10555): RTX retransmissions should show up in |
| 789 | // separate outbound-rtp stream objects. |
| 790 | stats.retransmitted_bytes_sent = rtp_stats.retransmitted.payload_bytes; |
| 791 | stats.packetsSent = |
| 792 | rtp_stats.transmitted.packets + rtx_stats.transmitted.packets; |
| 793 | stats.retransmitted_packets_sent = rtp_stats.retransmitted.packets; |
Henrik Boström | 6e436d1 | 2019-05-27 12:19:33 +0200 | [diff] [blame] | 794 | stats.report_block_datas = _rtpRtcpModule->GetLatestReportBlockData(); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 795 | |
Niels Möller | 2681523 | 2018-11-16 09:32:40 +0100 | [diff] [blame] | 796 | return stats; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 799 | void ChannelSend::ProcessAndEncodeAudio( |
| 800 | std::unique_ptr<AudioFrame> audio_frame) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 801 | RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 802 | RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0); |
| 803 | RTC_DCHECK_LE(audio_frame->num_channels_, 8); |
| 804 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 805 | // Profile time between when the audio frame is added to the task queue and |
| 806 | // when the task is actually executed. |
| 807 | audio_frame->UpdateProfileTimeStamp(); |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 808 | encoder_queue_.PostTask( |
| 809 | [this, audio_frame = std::move(audio_frame)]() mutable { |
| 810 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 811 | if (!encoder_queue_is_active_) { |
| 812 | return; |
| 813 | } |
| 814 | // Measure time between when the audio frame is added to the task queue |
| 815 | // and when the task is actually executed. Goal is to keep track of |
| 816 | // unwanted extra latency added by the task queue. |
| 817 | RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", |
| 818 | audio_frame->ElapsedProfileTimeMs()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 819 | |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 820 | bool is_muted = InputMute(); |
| 821 | AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_, |
| 822 | is_muted); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 823 | |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 824 | if (_includeAudioLevelIndication) { |
| 825 | size_t length = |
| 826 | audio_frame->samples_per_channel_ * audio_frame->num_channels_; |
| 827 | RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); |
| 828 | if (is_muted && previous_frame_muted_) { |
| 829 | rms_level_.AnalyzeMuted(length); |
| 830 | } else { |
| 831 | rms_level_.Analyze( |
| 832 | rtc::ArrayView<const int16_t>(audio_frame->data(), length)); |
| 833 | } |
| 834 | } |
| 835 | previous_frame_muted_ = is_muted; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 836 | |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 837 | // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 838 | |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 839 | // The ACM resamples internally. |
| 840 | audio_frame->timestamp_ = _timeStamp; |
| 841 | // This call will trigger AudioPacketizationCallback::SendData if |
| 842 | // encoding is done and payload is ready for packetization and |
| 843 | // transmission. Otherwise, it will return without invoking the |
| 844 | // callback. |
| 845 | if (audio_coding_->Add10MsData(*audio_frame) < 0) { |
| 846 | RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed."; |
| 847 | return; |
| 848 | } |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 849 | |
Sebastian Jansson | ee5ec9a | 2019-09-17 20:34:03 +0200 | [diff] [blame] | 850 | _timeStamp += static_cast<uint32_t>(audio_frame->samples_per_channel_); |
| 851 | }); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 854 | ANAStats ChannelSend::GetANAStatistics() const { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 855 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 856 | return audio_coding_->GetANAStats(); |
| 857 | } |
| 858 | |
| 859 | RtpRtcp* ChannelSend::GetRtpRtcp() const { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 860 | RTC_DCHECK(module_process_thread_checker_.IsCurrent()); |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 861 | return _rtpRtcpModule.get(); |
| 862 | } |
| 863 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 864 | int64_t ChannelSend::GetRTT() const { |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 865 | std::vector<RTCPReportBlock> report_blocks; |
| 866 | _rtpRtcpModule->RemoteRTCPStat(&report_blocks); |
| 867 | |
| 868 | if (report_blocks.empty()) { |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | int64_t rtt = 0; |
| 873 | int64_t avg_rtt = 0; |
| 874 | int64_t max_rtt = 0; |
| 875 | int64_t min_rtt = 0; |
| 876 | // We don't know in advance the remote ssrc used by the other end's receiver |
| 877 | // reports, so use the SSRC of the first report block for calculating the RTT. |
| 878 | if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt, |
| 879 | &min_rtt, &max_rtt) != 0) { |
| 880 | return 0; |
| 881 | } |
| 882 | return rtt; |
| 883 | } |
| 884 | |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 885 | void ChannelSend::SetFrameEncryptor( |
| 886 | rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) { |
Niels Möller | 26e88b0 | 2018-11-19 15:08:13 +0100 | [diff] [blame] | 887 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 888 | encoder_queue_.PostTask([this, frame_encryptor]() mutable { |
| 889 | RTC_DCHECK_RUN_ON(&encoder_queue_); |
Sebastian Jansson | 7949f21 | 2019-03-05 13:41:48 +0000 | [diff] [blame] | 890 | frame_encryptor_ = std::move(frame_encryptor); |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 891 | }); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 894 | void ChannelSend::OnReceivedRtt(int64_t rtt_ms) { |
| 895 | // Invoke audio encoders OnReceivedRtt(). |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 896 | CallEncoder( |
| 897 | [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); }); |
Piotr (Peter) Slatala | 179a392 | 2018-11-16 09:57:58 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 900 | } // namespace |
| 901 | |
| 902 | std::unique_ptr<ChannelSendInterface> CreateChannelSend( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 903 | Clock* clock, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 904 | TaskQueueFactory* task_queue_factory, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 905 | ProcessThread* module_process_thread, |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 906 | OverheadObserver* overhead_observer, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 907 | Transport* rtp_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 908 | RtcpRttStats* rtcp_rtt_stats, |
| 909 | RtcEventLog* rtc_event_log, |
| 910 | FrameEncryptorInterface* frame_encryptor, |
| 911 | const webrtc::CryptoOptions& crypto_options, |
| 912 | bool extmap_allow_mixed, |
Erik Språng | 4c2c412 | 2019-07-11 15:20:15 +0200 | [diff] [blame] | 913 | int rtcp_report_interval_ms, |
| 914 | uint32_t ssrc) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 915 | return std::make_unique<ChannelSend>( |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 916 | clock, task_queue_factory, module_process_thread, overhead_observer, |
| 917 | rtp_transport, rtcp_rtt_stats, rtc_event_log, frame_encryptor, |
| 918 | crypto_options, extmap_allow_mixed, rtcp_report_interval_ms, ssrc); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 919 | } |
| 920 | |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 921 | } // namespace voe |
| 922 | } // namespace webrtc |