blob: 2fa07067796517cb9730333df0051ef6f3e6a92e [file] [log] [blame]
Niels Möller530ead42018-10-04 14:28:39 +02001/*
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öller530ead42018-10-04 14:28:39 +020020#include "api/array_view.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010021#include "api/call/transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto/frame_encryptor_interface.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020023#include "api/rtc_event_log/rtc_event_log.h"
Niels Möller530ead42018-10-04 14:28:39 +020024#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öller530ead42018-10-04 14:28:39 +020027#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010028#include "modules/audio_coding/include/audio_coding_module.h"
29#include "modules/audio_processing/rms_level.h"
Niels Möller530ead42018-10-04 14:28:39 +020030#include "modules/pacing/packet_router.h"
31#include "modules/utility/include/process_thread.h"
32#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020033#include "rtc_base/event.h"
Niels Möller530ead42018-10-04 14:28:39 +020034#include "rtc_base/format_macros.h"
35#include "rtc_base/location.h"
36#include "rtc_base/logging.h"
Niels Möller26815232018-11-16 09:32:40 +010037#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010038#include "rtc_base/race_checker.h"
Niels Möller530ead42018-10-04 14:28:39 +020039#include "rtc_base/rate_limiter.h"
40#include "rtc_base/task_queue.h"
41#include "rtc_base/thread_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080042#include "rtc_base/time_utils.h"
Sebastian Jansson977b3352019-03-04 17:43:34 +010043#include "system_wrappers/include/clock.h"
Niels Möller530ead42018-10-04 14:28:39 +020044#include "system_wrappers/include/field_trial.h"
45#include "system_wrappers/include/metrics.h"
46
47namespace webrtc {
48namespace voe {
49
50namespace {
51
52constexpr int64_t kMaxRetransmissionWindowMs = 1000;
53constexpr int64_t kMinRetransmissionWindowMs = 30;
54
Niels Möllerdced9f62018-11-19 10:27:07 +010055class RtpPacketSenderProxy;
56class TransportFeedbackProxy;
57class TransportSequenceNumberProxy;
58class VoERtcpObserver;
59
Benjamin Wright17b050f2019-03-13 17:35:46 -070060class ChannelSend : public ChannelSendInterface,
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -080061 public AudioPacketizationCallback { // receive encoded
62 // packets from the ACM
Niels Möllerdced9f62018-11-19 10:27:07 +010063 public:
64 // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
65 // declaration.
66 friend class VoERtcpObserver;
67
Sebastian Jansson977b3352019-03-04 17:43:34 +010068 ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010069 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +010070 ProcessThread* module_process_thread,
Anton Sukhanov626015d2019-02-04 15:16:06 -080071 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +010072 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +010073 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ång4c2c4122019-07-11 15:20:15 +020078 int rtcp_report_interval_ms,
79 uint32_t ssrc);
Niels Möllerdced9f62018-11-19 10:27:07 +010080
81 ~ChannelSend() override;
82
83 // Send using this encoder, with this payload type.
Niels Möller8fb1a6a2019-03-05 14:29:42 +010084 void SetEncoder(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +010085 std::unique_ptr<AudioEncoder> encoder) override;
86 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
87 modifier) override;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +010088 void CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) override;
Niels Möllerdced9f62018-11-19 10:27:07 +010089
90 // API methods
Niels Möllerdced9f62018-11-19 10:27:07 +010091 void StartSend() override;
92 void StopSend() override;
93
94 // Codecs
Sebastian Jansson254d8692018-11-21 19:19:00 +010095 void OnBitrateAllocation(BitrateAllocationUpdate update) override;
Niels Möllerdced9f62018-11-19 10:27:07 +010096 int GetBitrate() const override;
97
98 // Network
Niels Möller8fb1a6a2019-03-05 14:29:42 +010099 void ReceivedRTCPPacket(const uint8_t* data, size_t length) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100100
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ölleree5ccbc2019-03-06 16:47:29 +0100110 void RegisterCngPayloadType(int payload_type, int payload_frequency) override;
111
Niels Möllerdced9f62018-11-19 10:27:07 +0100112 // DTMF.
113 bool SendTelephoneEventOutband(int event, int duration_ms) override;
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100114 void SetSendTelephoneEventPayloadType(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +0100115 int payload_frequency) override;
116
117 // RTP+RTCP
Niels Möllerdced9f62018-11-19 10:27:07 +0100118 void SetSendAudioLevelIndicationStatus(bool enable, int id) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100119
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öllerdced9f62018-11-19 10:27:07 +0100127
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öllerdced9f62018-11-19 10:27:07 +0100139 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öllerdced9f62018-11-19 10:27:07 +0100146 // From AudioPacketizationCallback in the ACM
Niels Möller87e2d782019-03-07 10:18:23 +0100147 int32_t SendData(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100148 uint8_t payloadType,
149 uint32_t timeStamp,
150 const uint8_t* payloadData,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200151 size_t payloadSize) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100152
Niels Möllerdced9f62018-11-19 10:27:07 +0100153 void OnUplinkPacketLossRate(float packet_loss_rate);
154 bool InputMute() const;
155
Niels Möller87e2d782019-03-07 10:18:23 +0100156 int32_t SendRtpAudio(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100157 uint8_t payloadType,
158 uint32_t timeStamp,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200159 rtc::ArrayView<const uint8_t> payload)
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100160 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100161
Niels Möllerdced9f62018-11-19 10:27:07 +0100162 void OnReceivedRtt(int64_t rtt_ms);
163
Niels Möllerdced9f62018-11-19 10:27:07 +0100164 // 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öllerdced9f62018-11-19 10:27:07 +0100176 rtc::CriticalSection volume_settings_critsect_;
177
Niels Möller26e88b02018-11-19 15:08:13 +0100178 bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100179
180 RtcEventLog* const event_log_;
181
182 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100183 std::unique_ptr<RTPSenderAudio> rtp_sender_audio_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100184
185 std::unique_ptr<AudioCodingModule> audio_coding_;
186 uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
187
Niels Möllerdced9f62018-11-19 10:27:07 +0100188 // uses
Niels Möller985a1f32018-11-19 16:08:42 +0100189 ProcessThread* const _moduleProcessThreadPtr;
Niels Möllerdced9f62018-11-19 10:27:07 +0100190 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 Sukhanov626015d2019-02-04 15:16:06 -0800197
Niels Möllerdced9f62018-11-19 10:27:07 +0100198 // RtcpBandwidthObserver
Niels Möller985a1f32018-11-19 16:08:42 +0100199 const std::unique_ptr<VoERtcpObserver> rtcp_observer_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100200
Niels Möller985a1f32018-11-19 16:08:42 +0100201 PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) =
202 nullptr;
203 const std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
Erik Språng59b86542019-06-23 18:24:46 +0200204 const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_pacer_proxy_;
Niels Möller985a1f32018-11-19 16:08:42 +0100205 const std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100206
207 rtc::ThreadChecker construction_thread_;
208
Niels Möllerdced9f62018-11-19 10:27:07 +0100209
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100210 bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100211
Niels Möllerdced9f62018-11-19 10:27:07 +0100212 // E2EE Audio Frame Encryption
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100213 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_
214 RTC_GUARDED_BY(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100215 // E2EE Frame Encryption Options
Niels Möller985a1f32018-11-19 16:08:42 +0100216 const webrtc::CryptoOptions crypto_options_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100217
218 rtc::CriticalSection bitrate_crit_section_;
219 int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100220
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öllerdced9f62018-11-19 10:27:07 +0100224};
Niels Möller530ead42018-10-04 14:28:39 +0200225
226const int kTelephoneEventAttenuationdB = 10;
227
228class TransportFeedbackProxy : public TransportFeedbackObserver {
229 public:
230 TransportFeedbackProxy() : feedback_observer_(nullptr) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200231 pacer_thread_.Detach();
232 network_thread_.Detach();
Niels Möller530ead42018-10-04 14:28:39 +0200233 }
234
235 void SetTransportFeedbackObserver(
236 TransportFeedbackObserver* feedback_observer) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200237 RTC_DCHECK(thread_checker_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200238 rtc::CritScope lock(&crit_);
239 feedback_observer_ = feedback_observer;
240 }
241
242 // Implements TransportFeedbackObserver.
Erik Språng30a276b2019-04-23 12:00:11 +0200243 void OnAddPacket(const RtpPacketSendInfo& packet_info) override {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200244 RTC_DCHECK(pacer_thread_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200245 rtc::CritScope lock(&crit_);
246 if (feedback_observer_)
Erik Språng30a276b2019-04-23 12:00:11 +0200247 feedback_observer_->OnAddPacket(packet_info);
Niels Möller530ead42018-10-04 14:28:39 +0200248 }
249
250 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200251 RTC_DCHECK(network_thread_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200252 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ångaa59eca2019-07-24 14:52:55 +0200265class RtpPacketSenderProxy : public RtpPacketSender {
Niels Möller530ead42018-10-04 14:28:39 +0200266 public:
Erik Språng59b86542019-06-23 18:24:46 +0200267 RtpPacketSenderProxy() : rtp_packet_pacer_(nullptr) {}
Niels Möller530ead42018-10-04 14:28:39 +0200268
Erik Språngaa59eca2019-07-24 14:52:55 +0200269 void SetPacketPacer(RtpPacketSender* rtp_packet_pacer) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200270 RTC_DCHECK(thread_checker_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200271 rtc::CritScope lock(&crit_);
Erik Språng59b86542019-06-23 18:24:46 +0200272 rtp_packet_pacer_ = rtp_packet_pacer;
273 }
274
Erik Språngea55b082019-10-02 14:57:46 +0200275 void EnqueuePackets(
276 std::vector<std::unique_ptr<RtpPacketToSend>> packets) override {
Erik Språng59b86542019-06-23 18:24:46 +0200277 rtc::CritScope lock(&crit_);
Erik Språngea55b082019-10-02 14:57:46 +0200278 rtp_packet_pacer_->EnqueuePackets(std::move(packets));
Niels Möller530ead42018-10-04 14:28:39 +0200279 }
280
Niels Möller530ead42018-10-04 14:28:39 +0200281 private:
282 rtc::ThreadChecker thread_checker_;
283 rtc::CriticalSection crit_;
Erik Språngaa59eca2019-07-24 14:52:55 +0200284 RtpPacketSender* rtp_packet_pacer_ RTC_GUARDED_BY(&crit_);
Niels Möller530ead42018-10-04 14:28:39 +0200285};
286
287class VoERtcpObserver : public RtcpBandwidthObserver {
288 public:
289 explicit VoERtcpObserver(ChannelSend* owner)
290 : owner_(owner), bandwidth_observer_(nullptr) {}
Mirko Bonadeife055c12019-01-29 22:53:28 +0100291 ~VoERtcpObserver() override {}
Niels Möller530ead42018-10-04 14:28:39 +0200292
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öller87e2d782019-03-07 10:18:23 +0100361int32_t ChannelSend::SendData(AudioFrameType frameType,
Niels Möller530ead42018-10-04 14:28:39 +0200362 uint8_t payloadType,
363 uint32_t timeStamp,
364 const uint8_t* payloadData,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200365 size_t payloadSize) {
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100366 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200367 rtc::ArrayView<const uint8_t> payload(payloadData, payloadSize);
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -0800368 return SendRtpAudio(frameType, payloadType, timeStamp, payload);
Niels Möller7d76a312018-10-26 12:57:07 +0200369}
370
Niels Möller87e2d782019-03-07 10:18:23 +0100371int32_t ChannelSend::SendRtpAudio(AudioFrameType frameType,
Niels Möller7d76a312018-10-26 12:57:07 +0200372 uint8_t payloadType,
373 uint32_t timeStamp,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200374 rtc::ArrayView<const uint8_t> payload) {
Niels Möller530ead42018-10-04 14:28:39 +0200375 if (_includeAudioLevelIndication) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100376 // Store current audio level in the RTP sender.
Niels Möller530ead42018-10-04 14:28:39 +0200377 // The level will be used in combination with voice-activity state
378 // (frameType) to add an RTP header extension
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100379 rtp_sender_audio_->SetAudioLevel(rms_level_.Average());
Niels Möller530ead42018-10-04 14:28:39 +0200380 }
381
Benjamin Wright84583f62018-10-04 14:22:34 -0700382 // 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 Li9ab520e2019-05-28 13:27:40 +0200385 // 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 Lif48bca72019-06-20 23:37:02 +0200389 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 Wright84583f62018-10-04 14:22:34 -0700396
Minyue Lif48bca72019-06-20 23:37:02 +0200397 // 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 Olssonb2b20312020-01-14 12:11:31 +0100415 "A frame encryptor is required but one is not set.";
Benjamin Wright84583f62018-10-04 14:22:34 -0700416 return -1;
417 }
Benjamin Wright84583f62018-10-04 14:22:34 -0700418 }
419
Niels Möller530ead42018-10-04 14:28:39 +0200420 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
421 // packetization.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100422 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 Bonadeif2c08182019-11-27 08:47:51 +0100428 return -1;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100429 }
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öller530ead42018-10-04 14:28:39 +0200437 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100438 if (!rtp_sender_audio_->SendAudio(frameType, payloadType, rtp_timestamp,
439 payload.data(), payload.size())) {
Niels Möller530ead42018-10-04 14:28:39 +0200440 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 Jansson977b3352019-03-04 17:43:34 +0100448ChannelSend::ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100449 TaskQueueFactory* task_queue_factory,
Niels Möller530ead42018-10-04 14:28:39 +0200450 ProcessThread* module_process_thread,
Anton Sukhanov626015d2019-02-04 15:16:06 -0800451 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +0100452 Transport* rtp_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200453 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700454 RtcEventLog* rtc_event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700455 FrameEncryptorInterface* frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +0100456 const webrtc::CryptoOptions& crypto_options,
Jiawei Ou55718122018-11-09 13:17:39 -0800457 bool extmap_allow_mixed,
Erik Språng4c2c4122019-07-11 15:20:15 +0200458 int rtcp_report_interval_ms,
459 uint32_t ssrc)
Niels Möller530ead42018-10-04 14:28:39 +0200460 : 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öller530ead42018-10-04 14:28:39 +0200463 _moduleProcessThreadPtr(module_process_thread),
Niels Möller530ead42018-10-04 14:28:39 +0200464 input_mute_(false),
465 previous_frame_muted_(false),
466 _includeAudioLevelIndication(false),
Niels Möller530ead42018-10-04 14:28:39 +0200467 rtcp_observer_(new VoERtcpObserver(this)),
468 feedback_observer_proxy_(new TransportFeedbackProxy()),
Erik Språng59b86542019-06-23 18:24:46 +0200469 rtp_packet_pacer_proxy_(new RtpPacketSenderProxy()),
Sebastian Jansson977b3352019-03-04 17:43:34 +0100470 retransmission_rate_limiter_(
471 new RateLimiter(clock, kMaxRetransmissionWindowMs)),
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700472 frame_encryptor_(frame_encryptor),
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100473 crypto_options_(crypto_options),
474 encoder_queue_(task_queue_factory->CreateTaskQueue(
475 "AudioEncoder",
476 TaskQueueFactory::Priority::NORMAL)) {
Niels Möller530ead42018-10-04 14:28:39 +0200477 RTC_DCHECK(module_process_thread);
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200478 module_process_thread_checker_.Detach();
Niels Möllerdced9f62018-11-19 10:27:07 +0100479
Niels Möller530ead42018-10-04 14:28:39 +0200480 audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config()));
481
482 RtpRtcp::Configuration configuration;
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -0800483 configuration.overhead_observer = overhead_observer;
484 configuration.bandwidth_callback = rtcp_observer_.get();
485 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
Mirko Bonadeif2c08182019-11-27 08:47:51 +0100486 configuration.clock = (clock ? clock : Clock::GetRealTimeClock());
Niels Möller530ead42018-10-04 14:28:39 +0200487 configuration.audio = true;
Fredrik Solenberg3d2ed192018-12-18 09:18:33 +0100488 configuration.outgoing_transport = rtp_transport;
Niels Möller530ead42018-10-04 14:28:39 +0200489
Erik Språng59b86542019-06-23 18:24:46 +0200490 configuration.paced_sender = rtp_packet_pacer_proxy_.get();
Niels Möller530ead42018-10-04 14:28:39 +0200491
492 configuration.event_log = event_log_;
493 configuration.rtt_stats = rtcp_rtt_stats;
494 configuration.retransmission_rate_limiter =
495 retransmission_rate_limiter_.get();
Johannes Kron9190b822018-10-29 11:22:05 +0100496 configuration.extmap_allow_mixed = extmap_allow_mixed;
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800497 configuration.rtcp_report_interval_ms = rtcp_report_interval_ms;
Niels Möller530ead42018-10-04 14:28:39 +0200498
Erik Språng54d5d2c2019-08-20 17:22:36 +0200499 configuration.local_media_ssrc = ssrc;
Erik Språng4c2c4122019-07-11 15:20:15 +0200500
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +0100501 _rtpRtcpModule = RtpRtcp::Create(configuration);
Niels Möller530ead42018-10-04 14:28:39 +0200502 _rtpRtcpModule->SetSendingMediaStatus(false);
Niels Möller530ead42018-10-04 14:28:39 +0200503
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200504 rtp_sender_audio_ = std::make_unique<RTPSenderAudio>(
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100505 configuration.clock, _rtpRtcpModule->RtpSender());
506
Niels Möller530ead42018-10-04 14:28:39 +0200507 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
508
Niels Möller530ead42018-10-04 14:28:39 +0200509 // Ensure that RTCP is enabled by default for the created channel.
Niels Möller530ead42018-10-04 14:28:39 +0200510 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
511
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100512 int error = audio_coding_->RegisterTransportCallback(this);
Niels Möller530ead42018-10-04 14:28:39 +0200513 RTC_DCHECK_EQ(0, error);
514}
515
Fredrik Solenberg645a3af2018-11-16 12:51:15 +0100516ChannelSend::~ChannelSend() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200517 RTC_DCHECK(construction_thread_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200518
519 StopSend();
Niels Möller530ead42018-10-04 14:28:39 +0200520 int error = audio_coding_->RegisterTransportCallback(NULL);
521 RTC_DCHECK_EQ(0, error);
522
Niels Möller530ead42018-10-04 14:28:39 +0200523 if (_moduleProcessThreadPtr)
524 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
Niels Möller530ead42018-10-04 14:28:39 +0200525}
526
Niels Möller26815232018-11-16 09:32:40 +0100527void ChannelSend::StartSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100528 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100529 RTC_DCHECK(!sending_);
530 sending_ = true;
Niels Möller530ead42018-10-04 14:28:39 +0200531
Niels Möller530ead42018-10-04 14:28:39 +0200532 _rtpRtcpModule->SetSendingMediaStatus(true);
Niels Möller26815232018-11-16 09:32:40 +0100533 int ret = _rtpRtcpModule->SetSendingStatus(true);
534 RTC_DCHECK_EQ(0, ret);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100535 // 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öller530ead42018-10-04 14:28:39 +0200538 encoder_queue_is_active_ = true;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100539 });
Niels Möller530ead42018-10-04 14:28:39 +0200540}
541
542void ChannelSend::StopSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100543 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100544 if (!sending_) {
Niels Möller530ead42018-10-04 14:28:39 +0200545 return;
546 }
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100547 sending_ = false;
Niels Möller530ead42018-10-04 14:28:39 +0200548
Niels Möllerc572ff32018-11-07 08:43:50 +0100549 rtc::Event flush;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100550 encoder_queue_.PostTask([this, &flush]() {
551 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller530ead42018-10-04 14:28:39 +0200552 encoder_queue_is_active_ = false;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100553 flush.Set();
554 });
Niels Möller530ead42018-10-04 14:28:39 +0200555 flush.Wait(rtc::Event::kForever);
556
Niels Möller530ead42018-10-04 14:28:39 +0200557 // 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öller8fb1a6a2019-03-05 14:29:42 +0100565void ChannelSend::SetEncoder(int payload_type,
Niels Möller530ead42018-10-04 14:28:39 +0200566 std::unique_ptr<AudioEncoder> encoder) {
Niels Möller26e88b02018-11-19 15:08:13 +0100567 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200568 RTC_DCHECK_GE(payload_type, 0);
569 RTC_DCHECK_LE(payload_type, 127);
Niels Möller530ead42018-10-04 14:28:39 +0200570
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ölleree5ccbc2019-03-06 16:47:29 +0100573 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type,
574 encoder->RtpTimestampRateHz());
575 rtp_sender_audio_->RegisterAudioPayload("audio", payload_type,
576 encoder->RtpTimestampRateHz(),
577 encoder->NumChannels(), 0);
Niels Möller530ead42018-10-04 14:28:39 +0200578
579 audio_coding_->SetEncoder(std::move(encoder));
Niels Möller530ead42018-10-04 14:28:39 +0200580}
581
582void ChannelSend::ModifyEncoder(
583 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
Anton Sukhanov626015d2019-02-04 15:16:06 -0800584 // 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öller530ead42018-10-04 14:28:39 +0200587 audio_coding_->ModifyEncoder(modifier);
588}
589
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100590void 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 Jansson254d8692018-11-21 19:19:00 +0100600void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100601 // 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 Janssonc01367d2019-04-08 15:20:44 +0200605 // RTC_DCHECK(worker_thread_checker_.IsCurrent() ||
606 // module_process_thread_checker_.IsCurrent());
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800607 rtc::CritScope lock(&bitrate_crit_section_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100608
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100609 CallEncoder([&](AudioEncoder* encoder) {
610 encoder->OnReceivedUplinkAllocation(update);
Niels Möller530ead42018-10-04 14:28:39 +0200611 });
Sebastian Jansson254d8692018-11-21 19:19:00 +0100612 retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps());
613 configured_bitrate_bps_ = update.target_bitrate.bps();
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200614}
615
Niels Möllerdced9f62018-11-19 10:27:07 +0100616int ChannelSend::GetBitrate() const {
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800617 rtc::CritScope lock(&bitrate_crit_section_);
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200618 return configured_bitrate_bps_;
Niels Möller530ead42018-10-04 14:28:39 +0200619}
620
Niels Möller530ead42018-10-04 14:28:39 +0200621void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) {
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100622 CallEncoder([&](AudioEncoder* encoder) {
623 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200624 });
625}
626
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100627void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
Niels Möller530ead42018-10-04 14:28:39 +0200628 // 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öller8fb1a6a2019-03-05 14:29:42 +0100634 return;
Niels Möller530ead42018-10-04 14:28:39 +0200635 }
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) Slatala179a3922018-11-16 09:57:58 -0800645 OnReceivedRtt(rtt);
Niels Möller530ead42018-10-04 14:28:39 +0200646}
647
648void ChannelSend::SetInputMute(bool enable) {
Niels Möller26e88b02018-11-19 15:08:13 +0100649 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200650 rtc::CritScope cs(&volume_settings_critsect_);
651 input_mute_ = enable;
652}
653
654bool ChannelSend::InputMute() const {
655 rtc::CritScope cs(&volume_settings_critsect_);
656 return input_mute_;
657}
658
Niels Möller26815232018-11-16 09:32:40 +0100659bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
Niels Möller26e88b02018-11-19 15:08:13 +0100660 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200661 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 Solenbergeb134842018-11-19 14:13:15 +0100665 if (!sending_) {
Niels Möller26815232018-11-16 09:32:40 +0100666 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200667 }
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100668 if (rtp_sender_audio_->SendTelephoneEvent(
Niels Möller530ead42018-10-04 14:28:39 +0200669 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100670 RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event";
Niels Möller26815232018-11-16 09:32:40 +0100671 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200672 }
Niels Möller26815232018-11-16 09:32:40 +0100673 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200674}
675
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100676void 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öller8fb1a6a2019-03-05 14:29:42 +0100683void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
Niels Möller26815232018-11-16 09:32:40 +0100684 int payload_frequency) {
Niels Möller26e88b02018-11-19 15:08:13 +0100685 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200686 RTC_DCHECK_LE(0, payload_type);
687 RTC_DCHECK_GE(127, payload_type);
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100688 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
689 rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type,
690 payload_frequency, 0, 0);
Niels Möller530ead42018-10-04 14:28:39 +0200691}
692
Niels Möller26815232018-11-16 09:32:40 +0100693void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100694 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200695 _includeAudioLevelIndication = enable;
Sebastian Jansson6298b562020-01-14 17:55:19 +0100696 if (enable) {
697 _rtpRtcpModule->RegisterRtpHeaderExtension(AudioLevel::kUri, id);
698 } else {
699 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(AudioLevel::kUri);
700 }
Niels Möller530ead42018-10-04 14:28:39 +0200701}
702
703void ChannelSend::RegisterSenderCongestionControlObjects(
704 RtpTransportControllerSendInterface* transport,
705 RtcpBandwidthObserver* bandwidth_observer) {
Niels Möller26e88b02018-11-19 15:08:13 +0100706 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Erik Språngaa59eca2019-07-24 14:52:55 +0200707 RtpPacketSender* rtp_packet_pacer = transport->packet_sender();
Niels Möller530ead42018-10-04 14:28:39 +0200708 TransportFeedbackObserver* transport_feedback_observer =
709 transport->transport_feedback_observer();
710 PacketRouter* packet_router = transport->packet_router();
711
Erik Språng59b86542019-06-23 18:24:46 +0200712 RTC_DCHECK(rtp_packet_pacer);
Niels Möller530ead42018-10-04 14:28:39 +0200713 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ång59b86542019-06-23 18:24:46 +0200719 rtp_packet_pacer_proxy_->SetPacketPacer(rtp_packet_pacer);
Niels Möller530ead42018-10-04 14:28:39 +0200720 _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
726void ChannelSend::ResetSenderCongestionControlObjects() {
Niels Möller26e88b02018-11-19 15:08:13 +0100727 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200728 RTC_DCHECK(packet_router_);
729 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
730 rtcp_observer_->SetBandwidthObserver(nullptr);
731 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
Niels Möller530ead42018-10-04 14:28:39 +0200732 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
733 packet_router_ = nullptr;
Erik Språng59b86542019-06-23 18:24:46 +0200734 rtp_packet_pacer_proxy_->SetPacketPacer(nullptr);
Niels Möller530ead42018-10-04 14:28:39 +0200735}
736
Niels Möller26815232018-11-16 09:32:40 +0100737void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) {
Niels Möller26e88b02018-11-19 15:08:13 +0100738 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +0100739 // 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öller530ead42018-10-04 14:28:39 +0200743}
744
Niels Möller26815232018-11-16 09:32:40 +0100745std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const {
Niels Möller26e88b02018-11-19 15:08:13 +0100746 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200747 // 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öller530ead42018-10-04 14:28:39 +0200751
Niels Möller26815232018-11-16 09:32:40 +0100752 int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks);
753 RTC_DCHECK_EQ(0, ret);
754
755 std::vector<ReportBlock> report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +0200756
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öller26815232018-11-16 09:32:40 +0100769 report_blocks.push_back(report_block);
Niels Möller530ead42018-10-04 14:28:39 +0200770 }
Niels Möller26815232018-11-16 09:32:40 +0100771 return report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +0200772}
773
Niels Möller26815232018-11-16 09:32:40 +0100774CallSendStatistics ChannelSend::GetRTCPStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +0100775 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +0100776 CallSendStatistics stats = {0};
Niels Möller530ead42018-10-04 14:28:39 +0200777 stats.rttMs = GetRTT();
778
Henrik Boströmcf96e0f2019-04-17 13:51:53 +0200779 StreamDataCounters rtp_stats;
780 StreamDataCounters rtx_stats;
781 _rtpRtcpModule->GetSendStreamDataCounters(&rtp_stats, &rtx_stats);
Niels Möllerac0a4cb2019-10-09 15:01:33 +0200782 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ömcf96e0f2019-04-17 13:51:53 +0200788 // 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öm6e436d12019-05-27 12:19:33 +0200794 stats.report_block_datas = _rtpRtcpModule->GetLatestReportBlockData();
Niels Möller530ead42018-10-04 14:28:39 +0200795
Niels Möller26815232018-11-16 09:32:40 +0100796 return stats;
Niels Möller530ead42018-10-04 14:28:39 +0200797}
798
Niels Möller530ead42018-10-04 14:28:39 +0200799void ChannelSend::ProcessAndEncodeAudio(
800 std::unique_ptr<AudioFrame> audio_frame) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100801 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200802 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
803 RTC_DCHECK_LE(audio_frame->num_channels_, 8);
804
Niels Möller530ead42018-10-04 14:28:39 +0200805 // 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 Janssonee5ec9a2019-09-17 20:34:03 +0200808 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öller530ead42018-10-04 14:28:39 +0200819
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200820 bool is_muted = InputMute();
821 AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_,
822 is_muted);
Niels Möller530ead42018-10-04 14:28:39 +0200823
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200824 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öller530ead42018-10-04 14:28:39 +0200836
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200837 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
Niels Möller530ead42018-10-04 14:28:39 +0200838
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200839 // 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öller530ead42018-10-04 14:28:39 +0200849
Sebastian Janssonee5ec9a2019-09-17 20:34:03 +0200850 _timeStamp += static_cast<uint32_t>(audio_frame->samples_per_channel_);
851 });
Niels Möller530ead42018-10-04 14:28:39 +0200852}
853
Niels Möller530ead42018-10-04 14:28:39 +0200854ANAStats ChannelSend::GetANAStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +0100855 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200856 return audio_coding_->GetANAStats();
857}
858
859RtpRtcp* ChannelSend::GetRtpRtcp() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200860 RTC_DCHECK(module_process_thread_checker_.IsCurrent());
Niels Möller530ead42018-10-04 14:28:39 +0200861 return _rtpRtcpModule.get();
862}
863
Niels Möller530ead42018-10-04 14:28:39 +0200864int64_t ChannelSend::GetRTT() const {
Niels Möller530ead42018-10-04 14:28:39 +0200865 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 Wright78410ad2018-10-25 09:52:57 -0700885void ChannelSend::SetFrameEncryptor(
886 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
Niels Möller26e88b02018-11-19 15:08:13 +0100887 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100888 encoder_queue_.PostTask([this, frame_encryptor]() mutable {
889 RTC_DCHECK_RUN_ON(&encoder_queue_);
Sebastian Jansson7949f212019-03-05 13:41:48 +0000890 frame_encryptor_ = std::move(frame_encryptor);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100891 });
Benjamin Wright84583f62018-10-04 14:22:34 -0700892}
893
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800894void ChannelSend::OnReceivedRtt(int64_t rtt_ms) {
895 // Invoke audio encoders OnReceivedRtt().
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100896 CallEncoder(
897 [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); });
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800898}
899
Niels Möllerdced9f62018-11-19 10:27:07 +0100900} // namespace
901
902std::unique_ptr<ChannelSendInterface> CreateChannelSend(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100903 Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100904 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +0100905 ProcessThread* module_process_thread,
Anton Sukhanov626015d2019-02-04 15:16:06 -0800906 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +0100907 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +0100908 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ång4c2c4122019-07-11 15:20:15 +0200913 int rtcp_report_interval_ms,
914 uint32_t ssrc) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200915 return std::make_unique<ChannelSend>(
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -0800916 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öllerdced9f62018-11-19 10:27:07 +0100919}
920
Niels Möller530ead42018-10-04 14:28:39 +0200921} // namespace voe
922} // namespace webrtc