blob: 9f95ebb90bbfb78ade60bf026bfc80e6bea3edf2 [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
20#include "absl/memory/memory.h"
21#include "api/array_view.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010022#include "api/call/transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/crypto/frame_encryptor_interface.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"
27#include "logging/rtc_event_log/rtc_event_log.h"
28#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010029#include "modules/audio_coding/include/audio_coding_module.h"
30#include "modules/audio_processing/rms_level.h"
Niels Möller530ead42018-10-04 14:28:39 +020031#include "modules/pacing/packet_router.h"
32#include "modules/utility/include/process_thread.h"
33#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020034#include "rtc_base/event.h"
Niels Möller530ead42018-10-04 14:28:39 +020035#include "rtc_base/format_macros.h"
36#include "rtc_base/location.h"
37#include "rtc_base/logging.h"
Niels Möller26815232018-11-16 09:32:40 +010038#include "rtc_base/numerics/safe_conversions.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010039#include "rtc_base/race_checker.h"
Niels Möller530ead42018-10-04 14:28:39 +020040#include "rtc_base/rate_limiter.h"
41#include "rtc_base/task_queue.h"
42#include "rtc_base/thread_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/time_utils.h"
Sebastian Jansson977b3352019-03-04 17:43:34 +010044#include "system_wrappers/include/clock.h"
Niels Möller530ead42018-10-04 14:28:39 +020045#include "system_wrappers/include/field_trial.h"
46#include "system_wrappers/include/metrics.h"
47
48namespace webrtc {
49namespace voe {
50
51namespace {
52
53constexpr int64_t kMaxRetransmissionWindowMs = 1000;
54constexpr int64_t kMinRetransmissionWindowMs = 30;
55
Niels Möller7d76a312018-10-26 12:57:07 +020056MediaTransportEncodedAudioFrame::FrameType
Niels Möller87e2d782019-03-07 10:18:23 +010057MediaTransportFrameTypeForWebrtcFrameType(webrtc::AudioFrameType frame_type) {
Niels Möller7d76a312018-10-26 12:57:07 +020058 switch (frame_type) {
59 case kAudioFrameSpeech:
60 return MediaTransportEncodedAudioFrame::FrameType::kSpeech;
61 break;
62
63 case kAudioFrameCN:
64 return MediaTransportEncodedAudioFrame::FrameType::
65 kDiscontinuousTransmission;
66 break;
67
68 default:
69 RTC_CHECK(false) << "Unexpected frame type=" << frame_type;
70 break;
71 }
72}
73
Niels Möllerdced9f62018-11-19 10:27:07 +010074class RtpPacketSenderProxy;
75class TransportFeedbackProxy;
76class TransportSequenceNumberProxy;
77class VoERtcpObserver;
78
Niels Möllerdced9f62018-11-19 10:27:07 +010079class ChannelSend
80 : public ChannelSendInterface,
Niels Möllerdced9f62018-11-19 10:27:07 +010081 public AudioPacketizationCallback, // receive encoded packets from the
82 // ACM
83 public TargetTransferRateObserver {
84 public:
85 // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
86 // declaration.
87 friend class VoERtcpObserver;
88
Sebastian Jansson977b3352019-03-04 17:43:34 +010089 ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010090 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +010091 ProcessThread* module_process_thread,
92 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -080093 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +010094 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +010095 RtcpRttStats* rtcp_rtt_stats,
96 RtcEventLog* rtc_event_log,
97 FrameEncryptorInterface* frame_encryptor,
98 const webrtc::CryptoOptions& crypto_options,
99 bool extmap_allow_mixed,
100 int rtcp_report_interval_ms);
101
102 ~ChannelSend() override;
103
104 // Send using this encoder, with this payload type.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100105 void SetEncoder(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +0100106 std::unique_ptr<AudioEncoder> encoder) override;
107 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
108 modifier) override;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100109 void CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100110
111 // API methods
Niels Möllerdced9f62018-11-19 10:27:07 +0100112 void StartSend() override;
113 void StopSend() override;
114
115 // Codecs
Sebastian Jansson254d8692018-11-21 19:19:00 +0100116 void OnBitrateAllocation(BitrateAllocationUpdate update) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100117 int GetBitrate() const override;
118
119 // Network
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100120 void ReceivedRTCPPacket(const uint8_t* data, size_t length) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100121
122 // Muting, Volume and Level.
123 void SetInputMute(bool enable) override;
124
125 // Stats.
126 ANAStats GetANAStatistics() const override;
127
128 // Used by AudioSendStream.
129 RtpRtcp* GetRtpRtcp() const override;
130
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100131 void RegisterCngPayloadType(int payload_type, int payload_frequency) override;
132
Niels Möllerdced9f62018-11-19 10:27:07 +0100133 // DTMF.
134 bool SendTelephoneEventOutband(int event, int duration_ms) override;
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100135 void SetSendTelephoneEventPayloadType(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +0100136 int payload_frequency) override;
137
138 // RTP+RTCP
139 void SetLocalSSRC(uint32_t ssrc) override;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800140 void SetRid(const std::string& rid,
141 int extension_id,
142 int repaired_extension_id) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100143 void SetMid(const std::string& mid, int extension_id) override;
144 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
145 void SetSendAudioLevelIndicationStatus(bool enable, int id) override;
146 void EnableSendTransportSequenceNumber(int id) override;
147
148 void RegisterSenderCongestionControlObjects(
149 RtpTransportControllerSendInterface* transport,
150 RtcpBandwidthObserver* bandwidth_observer) override;
151 void ResetSenderCongestionControlObjects() override;
152 void SetRTCP_CNAME(absl::string_view c_name) override;
153 std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const override;
154 CallSendStatistics GetRTCPStatistics() const override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100155
156 // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
157 // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
158 // the actual processing of the audio takes place. The processing mainly
159 // consists of encoding and preparing the result for sending by adding it to a
160 // send queue.
161 // The main reason for using a task queue here is to release the native,
162 // OS-specific, audio capture thread as soon as possible to ensure that it
163 // can go back to sleep and be prepared to deliver an new captured audio
164 // packet.
165 void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) override;
166
Niels Möllerdced9f62018-11-19 10:27:07 +0100167 // The existence of this function alongside OnUplinkPacketLossRate is
168 // a compromise. We want the encoder to be agnostic of the PLR source, but
169 // we also don't want it to receive conflicting information from TWCC and
170 // from RTCP-XR.
171 void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) override;
172
173 void OnRecoverableUplinkPacketLossRate(
174 float recoverable_packet_loss_rate) override;
175
176 int64_t GetRTT() const override;
177
178 // E2EE Custom Audio Frame Encryption
179 void SetFrameEncryptor(
180 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
181
182 private:
Niels Möllerdced9f62018-11-19 10:27:07 +0100183 // From AudioPacketizationCallback in the ACM
Niels Möller87e2d782019-03-07 10:18:23 +0100184 int32_t SendData(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100185 uint8_t payloadType,
186 uint32_t timeStamp,
187 const uint8_t* payloadData,
188 size_t payloadSize,
189 const RTPFragmentationHeader* fragmentation) override;
190
Niels Möllerdced9f62018-11-19 10:27:07 +0100191 void OnUplinkPacketLossRate(float packet_loss_rate);
192 bool InputMute() const;
193
Niels Möllerdced9f62018-11-19 10:27:07 +0100194 int SetSendRtpHeaderExtension(bool enable, RTPExtensionType type, int id);
195
Niels Möller87e2d782019-03-07 10:18:23 +0100196 int32_t SendRtpAudio(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100197 uint8_t payloadType,
198 uint32_t timeStamp,
199 rtc::ArrayView<const uint8_t> payload,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100200 const RTPFragmentationHeader* fragmentation)
201 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100202
Niels Möller87e2d782019-03-07 10:18:23 +0100203 int32_t SendMediaTransportAudio(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100204 uint8_t payloadType,
205 uint32_t timeStamp,
206 rtc::ArrayView<const uint8_t> payload,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100207 const RTPFragmentationHeader* fragmentation)
208 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100209
210 // Return media transport or nullptr if using RTP.
211 MediaTransportInterface* media_transport() { return media_transport_; }
212
213 // Called on the encoder task queue when a new input audio frame is ready
214 // for encoding.
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100215 void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input)
216 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100217
218 void OnReceivedRtt(int64_t rtt_ms);
219
220 void OnTargetTransferRate(TargetTransferRate) override;
221
222 // Thread checkers document and lock usage of some methods on voe::Channel to
223 // specific threads we know about. The goal is to eventually split up
224 // voe::Channel into parts with single-threaded semantics, and thereby reduce
225 // the need for locks.
226 rtc::ThreadChecker worker_thread_checker_;
227 rtc::ThreadChecker module_process_thread_checker_;
228 // Methods accessed from audio and video threads are checked for sequential-
229 // only access. We don't necessarily own and control these threads, so thread
230 // checkers cannot be used. E.g. Chromium may transfer "ownership" from one
231 // audio thread to another, but access is still sequential.
232 rtc::RaceChecker audio_thread_race_checker_;
233
Niels Möllerdced9f62018-11-19 10:27:07 +0100234 rtc::CriticalSection volume_settings_critsect_;
235
Niels Möller26e88b02018-11-19 15:08:13 +0100236 bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100237
238 RtcEventLog* const event_log_;
239
240 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100241 std::unique_ptr<RTPSenderAudio> rtp_sender_audio_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100242
243 std::unique_ptr<AudioCodingModule> audio_coding_;
244 uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
245
Niels Möllerdced9f62018-11-19 10:27:07 +0100246 // uses
Niels Möller985a1f32018-11-19 16:08:42 +0100247 ProcessThread* const _moduleProcessThreadPtr;
Niels Möllerdced9f62018-11-19 10:27:07 +0100248 RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
249 bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
250 bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
251 // VoeRTP_RTCP
252 // TODO(henrika): can today be accessed on the main thread and on the
253 // task queue; hence potential race.
254 bool _includeAudioLevelIndication;
Anton Sukhanov626015d2019-02-04 15:16:06 -0800255
Niels Möllerdced9f62018-11-19 10:27:07 +0100256 // RtcpBandwidthObserver
Niels Möller985a1f32018-11-19 16:08:42 +0100257 const std::unique_ptr<VoERtcpObserver> rtcp_observer_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100258
Niels Möller985a1f32018-11-19 16:08:42 +0100259 PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) =
260 nullptr;
261 const std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
262 const std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
263 const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
264 const std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100265
266 rtc::ThreadChecker construction_thread_;
267
268 const bool use_twcc_plr_for_ana_;
269
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100270 bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100271
272 MediaTransportInterface* const media_transport_;
273 int media_transport_sequence_number_ RTC_GUARDED_BY(encoder_queue_) = 0;
274
275 rtc::CriticalSection media_transport_lock_;
276 // Currently set by SetLocalSSRC.
277 uint64_t media_transport_channel_id_ RTC_GUARDED_BY(&media_transport_lock_) =
278 0;
279 // Cache payload type and sampling frequency from most recent call to
280 // SetEncoder. Needed to set MediaTransportEncodedAudioFrame metadata, and
281 // invalidate on encoder change.
282 int media_transport_payload_type_ RTC_GUARDED_BY(&media_transport_lock_);
283 int media_transport_sampling_frequency_
284 RTC_GUARDED_BY(&media_transport_lock_);
285
286 // E2EE Audio Frame Encryption
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100287 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_
288 RTC_GUARDED_BY(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100289 // E2EE Frame Encryption Options
Niels Möller985a1f32018-11-19 16:08:42 +0100290 const webrtc::CryptoOptions crypto_options_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100291
292 rtc::CriticalSection bitrate_crit_section_;
293 int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100294
295 // Defined last to ensure that there are no running tasks when the other
296 // members are destroyed.
297 rtc::TaskQueue encoder_queue_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100298};
Niels Möller530ead42018-10-04 14:28:39 +0200299
300const int kTelephoneEventAttenuationdB = 10;
301
302class TransportFeedbackProxy : public TransportFeedbackObserver {
303 public:
304 TransportFeedbackProxy() : feedback_observer_(nullptr) {
305 pacer_thread_.DetachFromThread();
306 network_thread_.DetachFromThread();
307 }
308
309 void SetTransportFeedbackObserver(
310 TransportFeedbackObserver* feedback_observer) {
311 RTC_DCHECK(thread_checker_.CalledOnValidThread());
312 rtc::CritScope lock(&crit_);
313 feedback_observer_ = feedback_observer;
314 }
315
316 // Implements TransportFeedbackObserver.
317 void AddPacket(uint32_t ssrc,
318 uint16_t sequence_number,
319 size_t length,
320 const PacedPacketInfo& pacing_info) override {
321 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
322 rtc::CritScope lock(&crit_);
323 if (feedback_observer_)
324 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
325 }
326
327 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
328 RTC_DCHECK(network_thread_.CalledOnValidThread());
329 rtc::CritScope lock(&crit_);
330 if (feedback_observer_)
331 feedback_observer_->OnTransportFeedback(feedback);
332 }
333
334 private:
335 rtc::CriticalSection crit_;
336 rtc::ThreadChecker thread_checker_;
337 rtc::ThreadChecker pacer_thread_;
338 rtc::ThreadChecker network_thread_;
339 TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
340};
341
342class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
343 public:
344 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
345 pacer_thread_.DetachFromThread();
346 }
347
348 void SetSequenceNumberAllocator(
349 TransportSequenceNumberAllocator* seq_num_allocator) {
350 RTC_DCHECK(thread_checker_.CalledOnValidThread());
351 rtc::CritScope lock(&crit_);
352 seq_num_allocator_ = seq_num_allocator;
353 }
354
355 // Implements TransportSequenceNumberAllocator.
356 uint16_t AllocateSequenceNumber() override {
357 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
358 rtc::CritScope lock(&crit_);
359 if (!seq_num_allocator_)
360 return 0;
361 return seq_num_allocator_->AllocateSequenceNumber();
362 }
363
364 private:
365 rtc::CriticalSection crit_;
366 rtc::ThreadChecker thread_checker_;
367 rtc::ThreadChecker pacer_thread_;
368 TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
369};
370
371class RtpPacketSenderProxy : public RtpPacketSender {
372 public:
373 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
374
375 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
376 RTC_DCHECK(thread_checker_.CalledOnValidThread());
377 rtc::CritScope lock(&crit_);
378 rtp_packet_sender_ = rtp_packet_sender;
379 }
380
381 // Implements RtpPacketSender.
382 void InsertPacket(Priority priority,
383 uint32_t ssrc,
384 uint16_t sequence_number,
385 int64_t capture_time_ms,
386 size_t bytes,
387 bool retransmission) override {
388 rtc::CritScope lock(&crit_);
389 if (rtp_packet_sender_) {
390 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
391 capture_time_ms, bytes, retransmission);
392 }
393 }
394
395 void SetAccountForAudioPackets(bool account_for_audio) override {
396 RTC_NOTREACHED();
397 }
398
399 private:
400 rtc::ThreadChecker thread_checker_;
401 rtc::CriticalSection crit_;
402 RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
403};
404
405class VoERtcpObserver : public RtcpBandwidthObserver {
406 public:
407 explicit VoERtcpObserver(ChannelSend* owner)
408 : owner_(owner), bandwidth_observer_(nullptr) {}
Mirko Bonadeife055c12019-01-29 22:53:28 +0100409 ~VoERtcpObserver() override {}
Niels Möller530ead42018-10-04 14:28:39 +0200410
411 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
412 rtc::CritScope lock(&crit_);
413 bandwidth_observer_ = bandwidth_observer;
414 }
415
416 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
417 rtc::CritScope lock(&crit_);
418 if (bandwidth_observer_) {
419 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
420 }
421 }
422
423 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
424 int64_t rtt,
425 int64_t now_ms) override {
426 {
427 rtc::CritScope lock(&crit_);
428 if (bandwidth_observer_) {
429 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
430 now_ms);
431 }
432 }
433 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
434 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
435 // report for VoiceEngine?
436 if (report_blocks.empty())
437 return;
438
439 int fraction_lost_aggregate = 0;
440 int total_number_of_packets = 0;
441
442 // If receiving multiple report blocks, calculate the weighted average based
443 // on the number of packets a report refers to.
444 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
445 block_it != report_blocks.end(); ++block_it) {
446 // Find the previous extended high sequence number for this remote SSRC,
447 // to calculate the number of RTP packets this report refers to. Ignore if
448 // we haven't seen this SSRC before.
449 std::map<uint32_t, uint32_t>::iterator seq_num_it =
450 extended_max_sequence_number_.find(block_it->source_ssrc);
451 int number_of_packets = 0;
452 if (seq_num_it != extended_max_sequence_number_.end()) {
453 number_of_packets =
454 block_it->extended_highest_sequence_number - seq_num_it->second;
455 }
456 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
457 total_number_of_packets += number_of_packets;
458
459 extended_max_sequence_number_[block_it->source_ssrc] =
460 block_it->extended_highest_sequence_number;
461 }
462 int weighted_fraction_lost = 0;
463 if (total_number_of_packets > 0) {
464 weighted_fraction_lost =
465 (fraction_lost_aggregate + total_number_of_packets / 2) /
466 total_number_of_packets;
467 }
468 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
469 }
470
471 private:
472 ChannelSend* owner_;
473 // Maps remote side ssrc to extended highest sequence number received.
474 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
475 rtc::CriticalSection crit_;
476 RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
477};
478
Niels Möller87e2d782019-03-07 10:18:23 +0100479int32_t ChannelSend::SendData(AudioFrameType frameType,
Niels Möller530ead42018-10-04 14:28:39 +0200480 uint8_t payloadType,
481 uint32_t timeStamp,
482 const uint8_t* payloadData,
483 size_t payloadSize,
484 const RTPFragmentationHeader* fragmentation) {
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100485 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200486 rtc::ArrayView<const uint8_t> payload(payloadData, payloadSize);
487
488 if (media_transport() != nullptr) {
Piotr (Peter) Slatala3cdd4d52019-02-28 07:10:56 -0800489 if (frameType == kEmptyFrame) {
490 // TODO(bugs.webrtc.org/9719): Media transport Send doesn't support
491 // sending empty frames.
492 return 0;
493 }
494
Niels Möller7d76a312018-10-26 12:57:07 +0200495 return SendMediaTransportAudio(frameType, payloadType, timeStamp, payload,
496 fragmentation);
497 } else {
498 return SendRtpAudio(frameType, payloadType, timeStamp, payload,
499 fragmentation);
500 }
501}
502
Niels Möller87e2d782019-03-07 10:18:23 +0100503int32_t ChannelSend::SendRtpAudio(AudioFrameType frameType,
Niels Möller7d76a312018-10-26 12:57:07 +0200504 uint8_t payloadType,
505 uint32_t timeStamp,
506 rtc::ArrayView<const uint8_t> payload,
507 const RTPFragmentationHeader* fragmentation) {
Niels Möller530ead42018-10-04 14:28:39 +0200508 if (_includeAudioLevelIndication) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100509 // Store current audio level in the RTP sender.
Niels Möller530ead42018-10-04 14:28:39 +0200510 // The level will be used in combination with voice-activity state
511 // (frameType) to add an RTP header extension
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100512 rtp_sender_audio_->SetAudioLevel(rms_level_.Average());
Niels Möller530ead42018-10-04 14:28:39 +0200513 }
514
Benjamin Wright84583f62018-10-04 14:22:34 -0700515 // E2EE Custom Audio Frame Encryption (This is optional).
516 // Keep this buffer around for the lifetime of the send call.
517 rtc::Buffer encrypted_audio_payload;
518 if (frame_encryptor_ != nullptr) {
519 // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline.
520 // Allocate a buffer to hold the maximum possible encrypted payload.
521 size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize(
Niels Möller7d76a312018-10-26 12:57:07 +0200522 cricket::MEDIA_TYPE_AUDIO, payload.size());
Benjamin Wright84583f62018-10-04 14:22:34 -0700523 encrypted_audio_payload.SetSize(max_ciphertext_size);
524
525 // Encrypt the audio payload into the buffer.
526 size_t bytes_written = 0;
527 int encrypt_status = frame_encryptor_->Encrypt(
528 cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(),
Niels Möller7d76a312018-10-26 12:57:07 +0200529 /*additional_data=*/nullptr, payload, encrypted_audio_payload,
530 &bytes_written);
Benjamin Wright84583f62018-10-04 14:22:34 -0700531 if (encrypt_status != 0) {
532 RTC_DLOG(LS_ERROR) << "Channel::SendData() failed encrypt audio payload: "
533 << encrypt_status;
534 return -1;
535 }
536 // Resize the buffer to the exact number of bytes actually used.
537 encrypted_audio_payload.SetSize(bytes_written);
538 // Rewrite the payloadData and size to the new encrypted payload.
Niels Möller7d76a312018-10-26 12:57:07 +0200539 payload = encrypted_audio_payload;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700540 } else if (crypto_options_.sframe.require_frame_encryption) {
541 RTC_DLOG(LS_ERROR) << "Channel::SendData() failed sending audio payload: "
542 << "A frame encryptor is required but one is not set.";
543 return -1;
Benjamin Wright84583f62018-10-04 14:22:34 -0700544 }
545
Niels Möller530ead42018-10-04 14:28:39 +0200546 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
547 // packetization.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100548 if (!_rtpRtcpModule->OnSendingRtpFrame(timeStamp,
549 // Leaving the time when this frame was
550 // received from the capture device as
551 // undefined for voice for now.
552 -1, payloadType,
553 /*force_sender_report=*/false)) {
554 return false;
555 }
556
557 // RTCPSender has it's own copy of the timestamp offset, added in
558 // RTCPSender::BuildSR, hence we must not add the in the offset for the above
559 // call.
560 // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine
561 // knowledge of the offset to a single place.
562 const uint32_t rtp_timestamp = timeStamp + _rtpRtcpModule->StartTimestamp();
Niels Möller530ead42018-10-04 14:28:39 +0200563 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100564 if (!rtp_sender_audio_->SendAudio(frameType, payloadType, rtp_timestamp,
565 payload.data(), payload.size())) {
Niels Möller530ead42018-10-04 14:28:39 +0200566 RTC_DLOG(LS_ERROR)
567 << "ChannelSend::SendData() failed to send data to RTP/RTCP module";
568 return -1;
569 }
570
571 return 0;
572}
573
Niels Möller7d76a312018-10-26 12:57:07 +0200574int32_t ChannelSend::SendMediaTransportAudio(
Niels Möller87e2d782019-03-07 10:18:23 +0100575 AudioFrameType frameType,
Niels Möller7d76a312018-10-26 12:57:07 +0200576 uint8_t payloadType,
577 uint32_t timeStamp,
578 rtc::ArrayView<const uint8_t> payload,
579 const RTPFragmentationHeader* fragmentation) {
Niels Möller7d76a312018-10-26 12:57:07 +0200580 // TODO(nisse): Use null _transportPtr for MediaTransport.
581 // RTC_DCHECK(_transportPtr == nullptr);
582 uint64_t channel_id;
583 int sampling_rate_hz;
584 {
585 rtc::CritScope cs(&media_transport_lock_);
586 if (media_transport_payload_type_ != payloadType) {
587 // Payload type is being changed, media_transport_sampling_frequency_,
588 // no longer current.
589 return -1;
590 }
591 sampling_rate_hz = media_transport_sampling_frequency_;
592 channel_id = media_transport_channel_id_;
593 }
Mirko Bonadei1c546052019-02-04 14:50:38 +0100594 MediaTransportEncodedAudioFrame frame(
Niels Möller7d76a312018-10-26 12:57:07 +0200595 /*sampling_rate_hz=*/sampling_rate_hz,
596
597 // TODO(nisse): Timestamp and sample index are the same for all supported
598 // audio codecs except G722. Refactor audio coding module to only use
599 // sample index, and leave translation to RTP time, when needed, for
600 // RTP-specific code.
601 /*starting_sample_index=*/timeStamp,
602
603 // Sample count isn't conveniently available from the AudioCodingModule,
604 // and needs some refactoring to wire up in a good way. For now, left as
605 // zero.
606 /*sample_count=*/0,
607
608 /*sequence_number=*/media_transport_sequence_number_,
609 MediaTransportFrameTypeForWebrtcFrameType(frameType), payloadType,
610 std::vector<uint8_t>(payload.begin(), payload.end()));
611
612 // TODO(nisse): Introduce a MediaTransportSender object bound to a specific
613 // channel id.
614 RTCError rtc_error =
615 media_transport()->SendAudioFrame(channel_id, std::move(frame));
616
617 if (!rtc_error.ok()) {
618 RTC_LOG(LS_ERROR) << "Failed to send frame, rtc_error="
619 << ToString(rtc_error.type()) << ", "
620 << rtc_error.message();
621 return -1;
622 }
623
624 ++media_transport_sequence_number_;
625
626 return 0;
627}
628
Sebastian Jansson977b3352019-03-04 17:43:34 +0100629ChannelSend::ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100630 TaskQueueFactory* task_queue_factory,
Niels Möller530ead42018-10-04 14:28:39 +0200631 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +0200632 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -0800633 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +0100634 Transport* rtp_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200635 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700636 RtcEventLog* rtc_event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700637 FrameEncryptorInterface* frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +0100638 const webrtc::CryptoOptions& crypto_options,
Jiawei Ou55718122018-11-09 13:17:39 -0800639 bool extmap_allow_mixed,
640 int rtcp_report_interval_ms)
Niels Möller530ead42018-10-04 14:28:39 +0200641 : event_log_(rtc_event_log),
642 _timeStamp(0), // This is just an offset, RTP module will add it's own
643 // random offset
Niels Möller530ead42018-10-04 14:28:39 +0200644 _moduleProcessThreadPtr(module_process_thread),
Niels Möller530ead42018-10-04 14:28:39 +0200645 input_mute_(false),
646 previous_frame_muted_(false),
647 _includeAudioLevelIndication(false),
Niels Möller530ead42018-10-04 14:28:39 +0200648 rtcp_observer_(new VoERtcpObserver(this)),
649 feedback_observer_proxy_(new TransportFeedbackProxy()),
650 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
651 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Sebastian Jansson977b3352019-03-04 17:43:34 +0100652 retransmission_rate_limiter_(
653 new RateLimiter(clock, kMaxRetransmissionWindowMs)),
Niels Möller530ead42018-10-04 14:28:39 +0200654 use_twcc_plr_for_ana_(
655 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled"),
Niels Möller7d76a312018-10-26 12:57:07 +0200656 media_transport_(media_transport),
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700657 frame_encryptor_(frame_encryptor),
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100658 crypto_options_(crypto_options),
659 encoder_queue_(task_queue_factory->CreateTaskQueue(
660 "AudioEncoder",
661 TaskQueueFactory::Priority::NORMAL)) {
Niels Möller530ead42018-10-04 14:28:39 +0200662 RTC_DCHECK(module_process_thread);
Niels Möllerdced9f62018-11-19 10:27:07 +0100663 module_process_thread_checker_.DetachFromThread();
664
Niels Möller530ead42018-10-04 14:28:39 +0200665 audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config()));
666
667 RtpRtcp::Configuration configuration;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800668
669 // We gradually remove codepaths that depend on RTP when using media
670 // transport. All of this logic should be moved to the future
671 // RTPMediaTransport. In this case it means that overhead and bandwidth
672 // observers should not be called when using media transport.
673 if (!media_transport_) {
Anton Sukhanov626015d2019-02-04 15:16:06 -0800674 configuration.overhead_observer = overhead_observer;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800675 configuration.bandwidth_callback = rtcp_observer_.get();
676 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
677 }
678
Sebastian Jansson977b3352019-03-04 17:43:34 +0100679 configuration.clock = clock;
Niels Möller530ead42018-10-04 14:28:39 +0200680 configuration.audio = true;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100681 configuration.clock = Clock::GetRealTimeClock();
Fredrik Solenberg3d2ed192018-12-18 09:18:33 +0100682 configuration.outgoing_transport = rtp_transport;
Niels Möller530ead42018-10-04 14:28:39 +0200683
684 configuration.paced_sender = rtp_packet_sender_proxy_.get();
685 configuration.transport_sequence_number_allocator =
686 seq_num_allocator_proxy_.get();
Niels Möller530ead42018-10-04 14:28:39 +0200687
688 configuration.event_log = event_log_;
689 configuration.rtt_stats = rtcp_rtt_stats;
690 configuration.retransmission_rate_limiter =
691 retransmission_rate_limiter_.get();
Johannes Kron9190b822018-10-29 11:22:05 +0100692 configuration.extmap_allow_mixed = extmap_allow_mixed;
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800693 configuration.rtcp_report_interval_ms = rtcp_report_interval_ms;
Niels Möller530ead42018-10-04 14:28:39 +0200694
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +0100695 _rtpRtcpModule = RtpRtcp::Create(configuration);
Niels Möller530ead42018-10-04 14:28:39 +0200696 _rtpRtcpModule->SetSendingMediaStatus(false);
Niels Möller530ead42018-10-04 14:28:39 +0200697
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100698 rtp_sender_audio_ = absl::make_unique<RTPSenderAudio>(
699 configuration.clock, _rtpRtcpModule->RtpSender());
700
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800701 // We want to invoke the 'TargetRateObserver' and |OnOverheadChanged|
702 // callbacks after the audio_coding_ is fully initialized.
703 if (media_transport_) {
704 RTC_DLOG(LS_INFO) << "Setting media_transport_ rate observers.";
705 media_transport_->AddTargetTransferRateObserver(this);
Niels Möllerd5af4022019-03-05 08:56:48 +0100706 media_transport_->SetAudioOverheadObserver(overhead_observer);
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800707 } else {
708 RTC_DLOG(LS_INFO) << "Not setting media_transport_ rate observers.";
709 }
710
Niels Möller530ead42018-10-04 14:28:39 +0200711 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
712
Niels Möller530ead42018-10-04 14:28:39 +0200713 // Ensure that RTCP is enabled by default for the created channel.
714 // Note that, the module will keep generating RTCP until it is explicitly
715 // disabled by the user.
716 // After StopListen (when no sockets exists), RTCP packets will no longer
717 // be transmitted since the Transport object will then be invalid.
718 // RTCP is enabled by default.
719 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
720
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100721 int error = audio_coding_->RegisterTransportCallback(this);
Niels Möller530ead42018-10-04 14:28:39 +0200722 RTC_DCHECK_EQ(0, error);
723}
724
Fredrik Solenberg645a3af2018-11-16 12:51:15 +0100725ChannelSend::~ChannelSend() {
Niels Möller530ead42018-10-04 14:28:39 +0200726 RTC_DCHECK(construction_thread_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200727
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800728 if (media_transport_) {
729 media_transport_->RemoveTargetTransferRateObserver(this);
Niels Möllerd5af4022019-03-05 08:56:48 +0100730 media_transport_->SetAudioOverheadObserver(nullptr);
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800731 }
732
Niels Möller530ead42018-10-04 14:28:39 +0200733 StopSend();
Niels Möller530ead42018-10-04 14:28:39 +0200734 int error = audio_coding_->RegisterTransportCallback(NULL);
735 RTC_DCHECK_EQ(0, error);
736
Niels Möller530ead42018-10-04 14:28:39 +0200737 if (_moduleProcessThreadPtr)
738 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
Niels Möller530ead42018-10-04 14:28:39 +0200739}
740
Niels Möller26815232018-11-16 09:32:40 +0100741void ChannelSend::StartSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100742 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100743 RTC_DCHECK(!sending_);
744 sending_ = true;
Niels Möller530ead42018-10-04 14:28:39 +0200745
Niels Möller530ead42018-10-04 14:28:39 +0200746 _rtpRtcpModule->SetSendingMediaStatus(true);
Niels Möller26815232018-11-16 09:32:40 +0100747 int ret = _rtpRtcpModule->SetSendingStatus(true);
748 RTC_DCHECK_EQ(0, ret);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100749 // It is now OK to start processing on the encoder task queue.
750 encoder_queue_.PostTask([this] {
751 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller530ead42018-10-04 14:28:39 +0200752 encoder_queue_is_active_ = true;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100753 });
Niels Möller530ead42018-10-04 14:28:39 +0200754}
755
756void ChannelSend::StopSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100757 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100758 if (!sending_) {
Niels Möller530ead42018-10-04 14:28:39 +0200759 return;
760 }
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100761 sending_ = false;
Niels Möller530ead42018-10-04 14:28:39 +0200762
Niels Möllerc572ff32018-11-07 08:43:50 +0100763 rtc::Event flush;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100764 encoder_queue_.PostTask([this, &flush]() {
765 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller530ead42018-10-04 14:28:39 +0200766 encoder_queue_is_active_ = false;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100767 flush.Set();
768 });
Niels Möller530ead42018-10-04 14:28:39 +0200769 flush.Wait(rtc::Event::kForever);
770
Niels Möller530ead42018-10-04 14:28:39 +0200771 // Reset sending SSRC and sequence number and triggers direct transmission
772 // of RTCP BYE
773 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
774 RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
775 }
776 _rtpRtcpModule->SetSendingMediaStatus(false);
777}
778
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100779void ChannelSend::SetEncoder(int payload_type,
Niels Möller530ead42018-10-04 14:28:39 +0200780 std::unique_ptr<AudioEncoder> encoder) {
Niels Möller26e88b02018-11-19 15:08:13 +0100781 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200782 RTC_DCHECK_GE(payload_type, 0);
783 RTC_DCHECK_LE(payload_type, 127);
Niels Möller530ead42018-10-04 14:28:39 +0200784
785 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
786 // as well as some other things, so we collect this info and send it along.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100787 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type,
788 encoder->RtpTimestampRateHz());
789 rtp_sender_audio_->RegisterAudioPayload("audio", payload_type,
790 encoder->RtpTimestampRateHz(),
791 encoder->NumChannels(), 0);
Niels Möller530ead42018-10-04 14:28:39 +0200792
Niels Möller7d76a312018-10-26 12:57:07 +0200793 if (media_transport_) {
794 rtc::CritScope cs(&media_transport_lock_);
795 media_transport_payload_type_ = payload_type;
796 // TODO(nisse): Currently broken for G722, since timestamps passed through
797 // encoder use RTP clock rather than sample count, and they differ for G722.
798 media_transport_sampling_frequency_ = encoder->RtpTimestampRateHz();
799 }
Niels Möller530ead42018-10-04 14:28:39 +0200800 audio_coding_->SetEncoder(std::move(encoder));
Niels Möller530ead42018-10-04 14:28:39 +0200801}
802
803void ChannelSend::ModifyEncoder(
804 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
Anton Sukhanov626015d2019-02-04 15:16:06 -0800805 // This method can be called on the worker thread, module process thread
806 // or network thread. Audio coding is thread safe, so we do not need to
807 // enforce the calling thread.
Niels Möller530ead42018-10-04 14:28:39 +0200808 audio_coding_->ModifyEncoder(modifier);
809}
810
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100811void ChannelSend::CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) {
812 ModifyEncoder([modifier](std::unique_ptr<AudioEncoder>* encoder_ptr) {
813 if (*encoder_ptr) {
814 modifier(encoder_ptr->get());
815 } else {
816 RTC_DLOG(LS_WARNING) << "Trying to call unset encoder.";
817 }
818 });
819}
820
Sebastian Jansson254d8692018-11-21 19:19:00 +0100821void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100822 // This method can be called on the worker thread, module process thread
823 // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged.
824 // TODO(solenberg): Figure out a good way to check this or enforce calling
825 // rules.
826 // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
827 // module_process_thread_checker_.CalledOnValidThread());
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800828 rtc::CritScope lock(&bitrate_crit_section_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100829
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100830 CallEncoder([&](AudioEncoder* encoder) {
831 encoder->OnReceivedUplinkAllocation(update);
Niels Möller530ead42018-10-04 14:28:39 +0200832 });
Sebastian Jansson254d8692018-11-21 19:19:00 +0100833 retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps());
834 configured_bitrate_bps_ = update.target_bitrate.bps();
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200835}
836
Niels Möllerdced9f62018-11-19 10:27:07 +0100837int ChannelSend::GetBitrate() const {
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800838 rtc::CritScope lock(&bitrate_crit_section_);
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200839 return configured_bitrate_bps_;
Niels Möller530ead42018-10-04 14:28:39 +0200840}
841
842void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
Niels Möller26e88b02018-11-19 15:08:13 +0100843 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200844 if (!use_twcc_plr_for_ana_)
845 return;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100846 CallEncoder([&](AudioEncoder* encoder) {
847 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200848 });
849}
850
851void ChannelSend::OnRecoverableUplinkPacketLossRate(
852 float recoverable_packet_loss_rate) {
Niels Möller26e88b02018-11-19 15:08:13 +0100853 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100854 CallEncoder([&](AudioEncoder* encoder) {
855 encoder->OnReceivedUplinkRecoverablePacketLossFraction(
856 recoverable_packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200857 });
858}
859
860void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) {
861 if (use_twcc_plr_for_ana_)
862 return;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100863 CallEncoder([&](AudioEncoder* encoder) {
864 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200865 });
866}
867
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100868void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100869 // May be called on either worker thread or network thread.
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800870 if (media_transport_) {
871 // Ignore RTCP packets while media transport is used.
872 // Those packets should not arrive, but we are seeing occasional packets.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100873 return;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800874 }
875
Niels Möller530ead42018-10-04 14:28:39 +0200876 // Deliver RTCP packet to RTP/RTCP module for parsing
877 _rtpRtcpModule->IncomingRtcpPacket(data, length);
878
879 int64_t rtt = GetRTT();
880 if (rtt == 0) {
881 // Waiting for valid RTT.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100882 return;
Niels Möller530ead42018-10-04 14:28:39 +0200883 }
884
885 int64_t nack_window_ms = rtt;
886 if (nack_window_ms < kMinRetransmissionWindowMs) {
887 nack_window_ms = kMinRetransmissionWindowMs;
888 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
889 nack_window_ms = kMaxRetransmissionWindowMs;
890 }
891 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
892
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800893 OnReceivedRtt(rtt);
Niels Möller530ead42018-10-04 14:28:39 +0200894}
895
896void ChannelSend::SetInputMute(bool enable) {
Niels Möller26e88b02018-11-19 15:08:13 +0100897 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200898 rtc::CritScope cs(&volume_settings_critsect_);
899 input_mute_ = enable;
900}
901
902bool ChannelSend::InputMute() const {
903 rtc::CritScope cs(&volume_settings_critsect_);
904 return input_mute_;
905}
906
Niels Möller26815232018-11-16 09:32:40 +0100907bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
Niels Möller26e88b02018-11-19 15:08:13 +0100908 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200909 RTC_DCHECK_LE(0, event);
910 RTC_DCHECK_GE(255, event);
911 RTC_DCHECK_LE(0, duration_ms);
912 RTC_DCHECK_GE(65535, duration_ms);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100913 if (!sending_) {
Niels Möller26815232018-11-16 09:32:40 +0100914 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200915 }
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100916 if (rtp_sender_audio_->SendTelephoneEvent(
Niels Möller530ead42018-10-04 14:28:39 +0200917 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100918 RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event";
Niels Möller26815232018-11-16 09:32:40 +0100919 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200920 }
Niels Möller26815232018-11-16 09:32:40 +0100921 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200922}
923
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100924void ChannelSend::RegisterCngPayloadType(int payload_type,
925 int payload_frequency) {
926 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
927 rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency,
928 1, 0);
929}
930
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100931void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
Niels Möller26815232018-11-16 09:32:40 +0100932 int payload_frequency) {
Niels Möller26e88b02018-11-19 15:08:13 +0100933 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200934 RTC_DCHECK_LE(0, payload_type);
935 RTC_DCHECK_GE(127, payload_type);
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100936 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
937 rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type,
938 payload_frequency, 0, 0);
Niels Möller530ead42018-10-04 14:28:39 +0200939}
940
Niels Möllerdced9f62018-11-19 10:27:07 +0100941void ChannelSend::SetLocalSSRC(uint32_t ssrc) {
Niels Möller26e88b02018-11-19 15:08:13 +0100942 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100943 RTC_DCHECK(!sending_);
Niels Möller26815232018-11-16 09:32:40 +0100944
Niels Möller7d76a312018-10-26 12:57:07 +0200945 if (media_transport_) {
946 rtc::CritScope cs(&media_transport_lock_);
947 media_transport_channel_id_ = ssrc;
948 }
Niels Möller530ead42018-10-04 14:28:39 +0200949 _rtpRtcpModule->SetSSRC(ssrc);
Niels Möller530ead42018-10-04 14:28:39 +0200950}
951
Amit Hilbuch77938e62018-12-21 09:23:38 -0800952void ChannelSend::SetRid(const std::string& rid,
953 int extension_id,
954 int repaired_extension_id) {
955 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
956 if (extension_id != 0) {
957 int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId,
958 extension_id);
959 RTC_DCHECK_EQ(0, ret);
960 }
961 if (repaired_extension_id != 0) {
962 int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId,
963 repaired_extension_id);
964 RTC_DCHECK_EQ(0, ret);
965 }
966 _rtpRtcpModule->SetRid(rid);
967}
968
Niels Möller530ead42018-10-04 14:28:39 +0200969void ChannelSend::SetMid(const std::string& mid, int extension_id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100970 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200971 int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id);
972 RTC_DCHECK_EQ(0, ret);
973 _rtpRtcpModule->SetMid(mid);
974}
975
Johannes Kron9190b822018-10-29 11:22:05 +0100976void ChannelSend::SetExtmapAllowMixed(bool extmap_allow_mixed) {
Niels Möller26e88b02018-11-19 15:08:13 +0100977 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Johannes Kron9190b822018-10-29 11:22:05 +0100978 _rtpRtcpModule->SetExtmapAllowMixed(extmap_allow_mixed);
979}
980
Niels Möller26815232018-11-16 09:32:40 +0100981void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100982 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200983 _includeAudioLevelIndication = enable;
Niels Möller26815232018-11-16 09:32:40 +0100984 int ret = SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
985 RTC_DCHECK_EQ(0, ret);
Niels Möller530ead42018-10-04 14:28:39 +0200986}
987
988void ChannelSend::EnableSendTransportSequenceNumber(int id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100989 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200990 int ret =
991 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
992 RTC_DCHECK_EQ(0, ret);
993}
994
995void ChannelSend::RegisterSenderCongestionControlObjects(
996 RtpTransportControllerSendInterface* transport,
997 RtcpBandwidthObserver* bandwidth_observer) {
Niels Möller26e88b02018-11-19 15:08:13 +0100998 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200999 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
1000 TransportFeedbackObserver* transport_feedback_observer =
1001 transport->transport_feedback_observer();
1002 PacketRouter* packet_router = transport->packet_router();
1003
1004 RTC_DCHECK(rtp_packet_sender);
1005 RTC_DCHECK(transport_feedback_observer);
1006 RTC_DCHECK(packet_router);
1007 RTC_DCHECK(!packet_router_);
1008 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
1009 feedback_observer_proxy_->SetTransportFeedbackObserver(
1010 transport_feedback_observer);
1011 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
1012 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
1013 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
1014 constexpr bool remb_candidate = false;
1015 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
1016 packet_router_ = packet_router;
1017}
1018
1019void ChannelSend::ResetSenderCongestionControlObjects() {
Niels Möller26e88b02018-11-19 15:08:13 +01001020 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001021 RTC_DCHECK(packet_router_);
1022 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
1023 rtcp_observer_->SetBandwidthObserver(nullptr);
1024 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
1025 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
1026 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
1027 packet_router_ = nullptr;
1028 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
1029}
1030
Niels Möller26815232018-11-16 09:32:40 +01001031void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) {
Niels Möller26e88b02018-11-19 15:08:13 +01001032 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +01001033 // Note: SetCNAME() accepts a c string of length at most 255.
1034 const std::string c_name_limited(c_name.substr(0, 255));
1035 int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0;
1036 RTC_DCHECK_EQ(0, ret) << "SetRTCP_CNAME() failed to set RTCP CNAME";
Niels Möller530ead42018-10-04 14:28:39 +02001037}
1038
Niels Möller26815232018-11-16 09:32:40 +01001039std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001040 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001041 // Get the report blocks from the latest received RTCP Sender or Receiver
1042 // Report. Each element in the vector contains the sender's SSRC and a
1043 // report block according to RFC 3550.
1044 std::vector<RTCPReportBlock> rtcp_report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001045
Niels Möller26815232018-11-16 09:32:40 +01001046 int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks);
1047 RTC_DCHECK_EQ(0, ret);
1048
1049 std::vector<ReportBlock> report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001050
1051 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
1052 for (; it != rtcp_report_blocks.end(); ++it) {
1053 ReportBlock report_block;
1054 report_block.sender_SSRC = it->sender_ssrc;
1055 report_block.source_SSRC = it->source_ssrc;
1056 report_block.fraction_lost = it->fraction_lost;
1057 report_block.cumulative_num_packets_lost = it->packets_lost;
1058 report_block.extended_highest_sequence_number =
1059 it->extended_highest_sequence_number;
1060 report_block.interarrival_jitter = it->jitter;
1061 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
1062 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
Niels Möller26815232018-11-16 09:32:40 +01001063 report_blocks.push_back(report_block);
Niels Möller530ead42018-10-04 14:28:39 +02001064 }
Niels Möller26815232018-11-16 09:32:40 +01001065 return report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001066}
1067
Niels Möller26815232018-11-16 09:32:40 +01001068CallSendStatistics ChannelSend::GetRTCPStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001069 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +01001070 CallSendStatistics stats = {0};
Niels Möller530ead42018-10-04 14:28:39 +02001071 stats.rttMs = GetRTT();
1072
Niels Möller530ead42018-10-04 14:28:39 +02001073 size_t bytesSent(0);
1074 uint32_t packetsSent(0);
1075
1076 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
1077 RTC_DLOG(LS_WARNING)
1078 << "GetRTPStatistics() failed to retrieve RTP datacounters"
1079 << " => output will not be complete";
1080 }
1081
1082 stats.bytesSent = bytesSent;
1083 stats.packetsSent = packetsSent;
1084
Niels Möller26815232018-11-16 09:32:40 +01001085 return stats;
Niels Möller530ead42018-10-04 14:28:39 +02001086}
1087
Niels Möller530ead42018-10-04 14:28:39 +02001088void ChannelSend::ProcessAndEncodeAudio(
1089 std::unique_ptr<AudioFrame> audio_frame) {
Niels Möllerdced9f62018-11-19 10:27:07 +01001090 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001091 struct ProcessAndEncodeAudio {
1092 void operator()() {
1093 RTC_DCHECK_RUN_ON(&channel->encoder_queue_);
1094 if (!channel->encoder_queue_is_active_) {
1095 return;
1096 }
1097 channel->ProcessAndEncodeAudioOnTaskQueue(audio_frame.get());
1098 }
1099 std::unique_ptr<AudioFrame> audio_frame;
1100 ChannelSend* const channel;
1101 };
Niels Möller530ead42018-10-04 14:28:39 +02001102 // Profile time between when the audio frame is added to the task queue and
1103 // when the task is actually executed.
1104 audio_frame->UpdateProfileTimeStamp();
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001105 encoder_queue_.PostTask(ProcessAndEncodeAudio{std::move(audio_frame), this});
Niels Möller530ead42018-10-04 14:28:39 +02001106}
1107
1108void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
Niels Möller530ead42018-10-04 14:28:39 +02001109 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
1110 RTC_DCHECK_LE(audio_input->num_channels_, 2);
1111
1112 // Measure time between when the audio frame is added to the task queue and
1113 // when the task is actually executed. Goal is to keep track of unwanted
1114 // extra latency added by the task queue.
1115 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
1116 audio_input->ElapsedProfileTimeMs());
1117
1118 bool is_muted = InputMute();
1119 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
1120
1121 if (_includeAudioLevelIndication) {
1122 size_t length =
1123 audio_input->samples_per_channel_ * audio_input->num_channels_;
1124 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
1125 if (is_muted && previous_frame_muted_) {
1126 rms_level_.AnalyzeMuted(length);
1127 } else {
1128 rms_level_.Analyze(
1129 rtc::ArrayView<const int16_t>(audio_input->data(), length));
1130 }
1131 }
1132 previous_frame_muted_ = is_muted;
1133
1134 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
1135
1136 // The ACM resamples internally.
1137 audio_input->timestamp_ = _timeStamp;
1138 // This call will trigger AudioPacketizationCallback::SendData if encoding
1139 // is done and payload is ready for packetization and transmission.
1140 // Otherwise, it will return without invoking the callback.
1141 if (audio_coding_->Add10MsData(*audio_input) < 0) {
1142 RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
1143 return;
1144 }
1145
1146 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
1147}
1148
Niels Möller530ead42018-10-04 14:28:39 +02001149ANAStats ChannelSend::GetANAStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001150 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001151 return audio_coding_->GetANAStats();
1152}
1153
1154RtpRtcp* ChannelSend::GetRtpRtcp() const {
Niels Möllerdced9f62018-11-19 10:27:07 +01001155 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +02001156 return _rtpRtcpModule.get();
1157}
1158
1159int ChannelSend::SetSendRtpHeaderExtension(bool enable,
1160 RTPExtensionType type,
Niels Möller26815232018-11-16 09:32:40 +01001161 int id) {
Niels Möller530ead42018-10-04 14:28:39 +02001162 int error = 0;
1163 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
1164 if (enable) {
Niels Möller26815232018-11-16 09:32:40 +01001165 // TODO(nisse): RtpRtcp::RegisterSendRtpHeaderExtension to take an int
1166 // argument. Currently it wants an uint8_t.
1167 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(
1168 type, rtc::dchecked_cast<uint8_t>(id));
Niels Möller530ead42018-10-04 14:28:39 +02001169 }
1170 return error;
1171}
1172
Niels Möller530ead42018-10-04 14:28:39 +02001173int64_t ChannelSend::GetRTT() const {
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001174 if (media_transport_) {
1175 // GetRTT is generally used in the RTCP codepath, where media transport is
1176 // not present and so it shouldn't be needed. But it's also invoked in
1177 // 'GetStats' method, and for now returning media transport RTT here gives
1178 // us "free" rtt stats for media transport.
1179 auto target_rate = media_transport_->GetLatestTargetTransferRate();
1180 if (target_rate.has_value()) {
1181 return target_rate.value().network_estimate.round_trip_time.ms();
1182 }
1183
1184 return 0;
1185 }
Niels Möller530ead42018-10-04 14:28:39 +02001186 RtcpMode method = _rtpRtcpModule->RTCP();
1187 if (method == RtcpMode::kOff) {
1188 return 0;
1189 }
1190 std::vector<RTCPReportBlock> report_blocks;
1191 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
1192
1193 if (report_blocks.empty()) {
1194 return 0;
1195 }
1196
1197 int64_t rtt = 0;
1198 int64_t avg_rtt = 0;
1199 int64_t max_rtt = 0;
1200 int64_t min_rtt = 0;
1201 // We don't know in advance the remote ssrc used by the other end's receiver
1202 // reports, so use the SSRC of the first report block for calculating the RTT.
1203 if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt,
1204 &min_rtt, &max_rtt) != 0) {
1205 return 0;
1206 }
1207 return rtt;
1208}
1209
Benjamin Wright78410ad2018-10-25 09:52:57 -07001210void ChannelSend::SetFrameEncryptor(
1211 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
Niels Möller26e88b02018-11-19 15:08:13 +01001212 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001213 encoder_queue_.PostTask([this, frame_encryptor]() mutable {
1214 RTC_DCHECK_RUN_ON(&encoder_queue_);
Sebastian Jansson7949f212019-03-05 13:41:48 +00001215 frame_encryptor_ = std::move(frame_encryptor);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001216 });
Benjamin Wright84583f62018-10-04 14:22:34 -07001217}
1218
Anton Sukhanov626015d2019-02-04 15:16:06 -08001219// TODO(sukhanov): Consider moving TargetTransferRate observer to
1220// AudioSendStream. Since AudioSendStream owns encoder and configures ANA, it
1221// makes sense to consolidate all rate (and overhead) calculation there.
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001222void ChannelSend::OnTargetTransferRate(TargetTransferRate rate) {
1223 RTC_DCHECK(media_transport_);
1224 OnReceivedRtt(rate.network_estimate.round_trip_time.ms());
1225}
1226
1227void ChannelSend::OnReceivedRtt(int64_t rtt_ms) {
1228 // Invoke audio encoders OnReceivedRtt().
Sebastian Jansson14a7cf92019-02-13 15:11:42 +01001229 CallEncoder(
1230 [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); });
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001231}
1232
Niels Möllerdced9f62018-11-19 10:27:07 +01001233} // namespace
1234
1235std::unique_ptr<ChannelSendInterface> CreateChannelSend(
Sebastian Jansson977b3352019-03-04 17:43:34 +01001236 Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001237 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +01001238 ProcessThread* module_process_thread,
1239 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -08001240 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +01001241 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +01001242 RtcpRttStats* rtcp_rtt_stats,
1243 RtcEventLog* rtc_event_log,
1244 FrameEncryptorInterface* frame_encryptor,
1245 const webrtc::CryptoOptions& crypto_options,
1246 bool extmap_allow_mixed,
1247 int rtcp_report_interval_ms) {
1248 return absl::make_unique<ChannelSend>(
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001249 clock, task_queue_factory, module_process_thread, media_transport,
Sebastian Jansson977b3352019-03-04 17:43:34 +01001250 overhead_observer, rtp_transport, rtcp_rtt_stats, rtc_event_log,
1251 frame_encryptor, crypto_options, extmap_allow_mixed,
1252 rtcp_report_interval_ms);
Niels Möllerdced9f62018-11-19 10:27:07 +01001253}
1254
Niels Möller530ead42018-10-04 14:28:39 +02001255} // namespace voe
1256} // namespace webrtc