blob: 5ca6f6a975ec7c3daa5fce01ca8525b72ae11e3f [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
Benjamin Wright17b050f2019-03-13 17:35:46 -070079class ChannelSend : public ChannelSendInterface,
80 public AudioPacketizationCallback, // receive encoded
81 // packets from the ACM
82 public TargetTransferRateObserver {
Niels Möllerdced9f62018-11-19 10:27:07 +010083 public:
84 // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
85 // declaration.
86 friend class VoERtcpObserver;
87
Sebastian Jansson977b3352019-03-04 17:43:34 +010088 ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010089 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +010090 ProcessThread* module_process_thread,
91 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -080092 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +010093 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +010094 RtcpRttStats* rtcp_rtt_stats,
95 RtcEventLog* rtc_event_log,
96 FrameEncryptorInterface* frame_encryptor,
97 const webrtc::CryptoOptions& crypto_options,
98 bool extmap_allow_mixed,
99 int rtcp_report_interval_ms);
100
101 ~ChannelSend() override;
102
103 // Send using this encoder, with this payload type.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100104 void SetEncoder(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +0100105 std::unique_ptr<AudioEncoder> encoder) override;
106 void ModifyEncoder(rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)>
107 modifier) override;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100108 void CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100109
110 // API methods
Niels Möllerdced9f62018-11-19 10:27:07 +0100111 void StartSend() override;
112 void StopSend() override;
113
114 // Codecs
Sebastian Jansson254d8692018-11-21 19:19:00 +0100115 void OnBitrateAllocation(BitrateAllocationUpdate update) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100116 int GetBitrate() const override;
117
118 // Network
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100119 void ReceivedRTCPPacket(const uint8_t* data, size_t length) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100120
121 // Muting, Volume and Level.
122 void SetInputMute(bool enable) override;
123
124 // Stats.
125 ANAStats GetANAStatistics() const override;
126
127 // Used by AudioSendStream.
128 RtpRtcp* GetRtpRtcp() const override;
129
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100130 void RegisterCngPayloadType(int payload_type, int payload_frequency) override;
131
Niels Möllerdced9f62018-11-19 10:27:07 +0100132 // DTMF.
133 bool SendTelephoneEventOutband(int event, int duration_ms) override;
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100134 void SetSendTelephoneEventPayloadType(int payload_type,
Niels Möllerdced9f62018-11-19 10:27:07 +0100135 int payload_frequency) override;
136
137 // RTP+RTCP
138 void SetLocalSSRC(uint32_t ssrc) override;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800139 void SetRid(const std::string& rid,
140 int extension_id,
141 int repaired_extension_id) override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100142 void SetMid(const std::string& mid, int extension_id) override;
143 void SetExtmapAllowMixed(bool extmap_allow_mixed) override;
144 void SetSendAudioLevelIndicationStatus(bool enable, int id) override;
145 void EnableSendTransportSequenceNumber(int id) override;
146
147 void RegisterSenderCongestionControlObjects(
148 RtpTransportControllerSendInterface* transport,
149 RtcpBandwidthObserver* bandwidth_observer) override;
150 void ResetSenderCongestionControlObjects() override;
151 void SetRTCP_CNAME(absl::string_view c_name) override;
152 std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const override;
153 CallSendStatistics GetRTCPStatistics() const override;
Niels Möllerdced9f62018-11-19 10:27:07 +0100154
155 // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
156 // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
157 // the actual processing of the audio takes place. The processing mainly
158 // consists of encoding and preparing the result for sending by adding it to a
159 // send queue.
160 // The main reason for using a task queue here is to release the native,
161 // OS-specific, audio capture thread as soon as possible to ensure that it
162 // can go back to sleep and be prepared to deliver an new captured audio
163 // packet.
164 void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) override;
165
Niels Möllerdced9f62018-11-19 10:27:07 +0100166 // The existence of this function alongside OnUplinkPacketLossRate is
167 // a compromise. We want the encoder to be agnostic of the PLR source, but
168 // we also don't want it to receive conflicting information from TWCC and
169 // from RTCP-XR.
170 void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) override;
171
172 void OnRecoverableUplinkPacketLossRate(
173 float recoverable_packet_loss_rate) override;
174
175 int64_t GetRTT() const override;
176
177 // E2EE Custom Audio Frame Encryption
178 void SetFrameEncryptor(
179 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) override;
180
181 private:
Niels Möllerdced9f62018-11-19 10:27:07 +0100182 // From AudioPacketizationCallback in the ACM
Niels Möller87e2d782019-03-07 10:18:23 +0100183 int32_t SendData(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100184 uint8_t payloadType,
185 uint32_t timeStamp,
186 const uint8_t* payloadData,
187 size_t payloadSize,
188 const RTPFragmentationHeader* fragmentation) override;
189
Niels Möllerdced9f62018-11-19 10:27:07 +0100190 void OnUplinkPacketLossRate(float packet_loss_rate);
191 bool InputMute() const;
192
Niels Möllerdced9f62018-11-19 10:27:07 +0100193 int SetSendRtpHeaderExtension(bool enable, RTPExtensionType type, int id);
194
Niels Möller87e2d782019-03-07 10:18:23 +0100195 int32_t SendRtpAudio(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100196 uint8_t payloadType,
197 uint32_t timeStamp,
198 rtc::ArrayView<const uint8_t> payload,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100199 const RTPFragmentationHeader* fragmentation)
200 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100201
Niels Möller87e2d782019-03-07 10:18:23 +0100202 int32_t SendMediaTransportAudio(AudioFrameType frameType,
Niels Möllerdced9f62018-11-19 10:27:07 +0100203 uint8_t payloadType,
204 uint32_t timeStamp,
205 rtc::ArrayView<const uint8_t> payload,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100206 const RTPFragmentationHeader* fragmentation)
207 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100208
209 // Return media transport or nullptr if using RTP.
210 MediaTransportInterface* media_transport() { return media_transport_; }
211
212 // Called on the encoder task queue when a new input audio frame is ready
213 // for encoding.
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100214 void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input)
215 RTC_RUN_ON(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100216
217 void OnReceivedRtt(int64_t rtt_ms);
218
219 void OnTargetTransferRate(TargetTransferRate) override;
220
221 // Thread checkers document and lock usage of some methods on voe::Channel to
222 // specific threads we know about. The goal is to eventually split up
223 // voe::Channel into parts with single-threaded semantics, and thereby reduce
224 // the need for locks.
225 rtc::ThreadChecker worker_thread_checker_;
226 rtc::ThreadChecker module_process_thread_checker_;
227 // Methods accessed from audio and video threads are checked for sequential-
228 // only access. We don't necessarily own and control these threads, so thread
229 // checkers cannot be used. E.g. Chromium may transfer "ownership" from one
230 // audio thread to another, but access is still sequential.
231 rtc::RaceChecker audio_thread_race_checker_;
232
Niels Möllerdced9f62018-11-19 10:27:07 +0100233 rtc::CriticalSection volume_settings_critsect_;
234
Niels Möller26e88b02018-11-19 15:08:13 +0100235 bool sending_ RTC_GUARDED_BY(&worker_thread_checker_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100236
237 RtcEventLog* const event_log_;
238
239 std::unique_ptr<RtpRtcp> _rtpRtcpModule;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100240 std::unique_ptr<RTPSenderAudio> rtp_sender_audio_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100241
242 std::unique_ptr<AudioCodingModule> audio_coding_;
243 uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
244
Niels Möllerdced9f62018-11-19 10:27:07 +0100245 // uses
Niels Möller985a1f32018-11-19 16:08:42 +0100246 ProcessThread* const _moduleProcessThreadPtr;
Niels Möllerdced9f62018-11-19 10:27:07 +0100247 RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
248 bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
249 bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
250 // VoeRTP_RTCP
251 // TODO(henrika): can today be accessed on the main thread and on the
252 // task queue; hence potential race.
253 bool _includeAudioLevelIndication;
Anton Sukhanov626015d2019-02-04 15:16:06 -0800254
Niels Möllerdced9f62018-11-19 10:27:07 +0100255 // RtcpBandwidthObserver
Niels Möller985a1f32018-11-19 16:08:42 +0100256 const std::unique_ptr<VoERtcpObserver> rtcp_observer_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100257
Niels Möller985a1f32018-11-19 16:08:42 +0100258 PacketRouter* packet_router_ RTC_GUARDED_BY(&worker_thread_checker_) =
259 nullptr;
260 const std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
261 const std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
262 const std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
263 const std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100264
265 rtc::ThreadChecker construction_thread_;
266
267 const bool use_twcc_plr_for_ana_;
268
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100269 bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_) = false;
Niels Möllerdced9f62018-11-19 10:27:07 +0100270
271 MediaTransportInterface* const media_transport_;
272 int media_transport_sequence_number_ RTC_GUARDED_BY(encoder_queue_) = 0;
273
274 rtc::CriticalSection media_transport_lock_;
275 // Currently set by SetLocalSSRC.
276 uint64_t media_transport_channel_id_ RTC_GUARDED_BY(&media_transport_lock_) =
277 0;
278 // Cache payload type and sampling frequency from most recent call to
279 // SetEncoder. Needed to set MediaTransportEncodedAudioFrame metadata, and
280 // invalidate on encoder change.
281 int media_transport_payload_type_ RTC_GUARDED_BY(&media_transport_lock_);
282 int media_transport_sampling_frequency_
283 RTC_GUARDED_BY(&media_transport_lock_);
284
285 // E2EE Audio Frame Encryption
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100286 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor_
287 RTC_GUARDED_BY(encoder_queue_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100288 // E2EE Frame Encryption Options
Niels Möller985a1f32018-11-19 16:08:42 +0100289 const webrtc::CryptoOptions crypto_options_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100290
291 rtc::CriticalSection bitrate_crit_section_;
292 int configured_bitrate_bps_ RTC_GUARDED_BY(bitrate_crit_section_) = 0;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100293
294 // Defined last to ensure that there are no running tasks when the other
295 // members are destroyed.
296 rtc::TaskQueue encoder_queue_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100297};
Niels Möller530ead42018-10-04 14:28:39 +0200298
299const int kTelephoneEventAttenuationdB = 10;
300
301class TransportFeedbackProxy : public TransportFeedbackObserver {
302 public:
303 TransportFeedbackProxy() : feedback_observer_(nullptr) {
304 pacer_thread_.DetachFromThread();
305 network_thread_.DetachFromThread();
306 }
307
308 void SetTransportFeedbackObserver(
309 TransportFeedbackObserver* feedback_observer) {
310 RTC_DCHECK(thread_checker_.CalledOnValidThread());
311 rtc::CritScope lock(&crit_);
312 feedback_observer_ = feedback_observer;
313 }
314
315 // Implements TransportFeedbackObserver.
316 void AddPacket(uint32_t ssrc,
317 uint16_t sequence_number,
318 size_t length,
319 const PacedPacketInfo& pacing_info) override {
320 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
321 rtc::CritScope lock(&crit_);
322 if (feedback_observer_)
323 feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
324 }
325
326 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
327 RTC_DCHECK(network_thread_.CalledOnValidThread());
328 rtc::CritScope lock(&crit_);
329 if (feedback_observer_)
330 feedback_observer_->OnTransportFeedback(feedback);
331 }
332
333 private:
334 rtc::CriticalSection crit_;
335 rtc::ThreadChecker thread_checker_;
336 rtc::ThreadChecker pacer_thread_;
337 rtc::ThreadChecker network_thread_;
338 TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
339};
340
341class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
342 public:
343 TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
344 pacer_thread_.DetachFromThread();
345 }
346
347 void SetSequenceNumberAllocator(
348 TransportSequenceNumberAllocator* seq_num_allocator) {
349 RTC_DCHECK(thread_checker_.CalledOnValidThread());
350 rtc::CritScope lock(&crit_);
351 seq_num_allocator_ = seq_num_allocator;
352 }
353
354 // Implements TransportSequenceNumberAllocator.
355 uint16_t AllocateSequenceNumber() override {
356 RTC_DCHECK(pacer_thread_.CalledOnValidThread());
357 rtc::CritScope lock(&crit_);
358 if (!seq_num_allocator_)
359 return 0;
360 return seq_num_allocator_->AllocateSequenceNumber();
361 }
362
363 private:
364 rtc::CriticalSection crit_;
365 rtc::ThreadChecker thread_checker_;
366 rtc::ThreadChecker pacer_thread_;
367 TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
368};
369
370class RtpPacketSenderProxy : public RtpPacketSender {
371 public:
372 RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
373
374 void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
375 RTC_DCHECK(thread_checker_.CalledOnValidThread());
376 rtc::CritScope lock(&crit_);
377 rtp_packet_sender_ = rtp_packet_sender;
378 }
379
380 // Implements RtpPacketSender.
381 void InsertPacket(Priority priority,
382 uint32_t ssrc,
383 uint16_t sequence_number,
384 int64_t capture_time_ms,
385 size_t bytes,
386 bool retransmission) override {
387 rtc::CritScope lock(&crit_);
388 if (rtp_packet_sender_) {
389 rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
390 capture_time_ms, bytes, retransmission);
391 }
392 }
393
394 void SetAccountForAudioPackets(bool account_for_audio) override {
395 RTC_NOTREACHED();
396 }
397
398 private:
399 rtc::ThreadChecker thread_checker_;
400 rtc::CriticalSection crit_;
401 RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
402};
403
404class VoERtcpObserver : public RtcpBandwidthObserver {
405 public:
406 explicit VoERtcpObserver(ChannelSend* owner)
407 : owner_(owner), bandwidth_observer_(nullptr) {}
Mirko Bonadeife055c12019-01-29 22:53:28 +0100408 ~VoERtcpObserver() override {}
Niels Möller530ead42018-10-04 14:28:39 +0200409
410 void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
411 rtc::CritScope lock(&crit_);
412 bandwidth_observer_ = bandwidth_observer;
413 }
414
415 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
416 rtc::CritScope lock(&crit_);
417 if (bandwidth_observer_) {
418 bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
419 }
420 }
421
422 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
423 int64_t rtt,
424 int64_t now_ms) override {
425 {
426 rtc::CritScope lock(&crit_);
427 if (bandwidth_observer_) {
428 bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
429 now_ms);
430 }
431 }
432 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
433 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
434 // report for VoiceEngine?
435 if (report_blocks.empty())
436 return;
437
438 int fraction_lost_aggregate = 0;
439 int total_number_of_packets = 0;
440
441 // If receiving multiple report blocks, calculate the weighted average based
442 // on the number of packets a report refers to.
443 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
444 block_it != report_blocks.end(); ++block_it) {
445 // Find the previous extended high sequence number for this remote SSRC,
446 // to calculate the number of RTP packets this report refers to. Ignore if
447 // we haven't seen this SSRC before.
448 std::map<uint32_t, uint32_t>::iterator seq_num_it =
449 extended_max_sequence_number_.find(block_it->source_ssrc);
450 int number_of_packets = 0;
451 if (seq_num_it != extended_max_sequence_number_.end()) {
452 number_of_packets =
453 block_it->extended_highest_sequence_number - seq_num_it->second;
454 }
455 fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
456 total_number_of_packets += number_of_packets;
457
458 extended_max_sequence_number_[block_it->source_ssrc] =
459 block_it->extended_highest_sequence_number;
460 }
461 int weighted_fraction_lost = 0;
462 if (total_number_of_packets > 0) {
463 weighted_fraction_lost =
464 (fraction_lost_aggregate + total_number_of_packets / 2) /
465 total_number_of_packets;
466 }
467 owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
468 }
469
470 private:
471 ChannelSend* owner_;
472 // Maps remote side ssrc to extended highest sequence number received.
473 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
474 rtc::CriticalSection crit_;
475 RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
476};
477
Niels Möller87e2d782019-03-07 10:18:23 +0100478int32_t ChannelSend::SendData(AudioFrameType frameType,
Niels Möller530ead42018-10-04 14:28:39 +0200479 uint8_t payloadType,
480 uint32_t timeStamp,
481 const uint8_t* payloadData,
482 size_t payloadSize,
483 const RTPFragmentationHeader* fragmentation) {
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100484 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200485 rtc::ArrayView<const uint8_t> payload(payloadData, payloadSize);
486
487 if (media_transport() != nullptr) {
Piotr (Peter) Slatala3cdd4d52019-02-28 07:10:56 -0800488 if (frameType == kEmptyFrame) {
489 // TODO(bugs.webrtc.org/9719): Media transport Send doesn't support
490 // sending empty frames.
491 return 0;
492 }
493
Niels Möller7d76a312018-10-26 12:57:07 +0200494 return SendMediaTransportAudio(frameType, payloadType, timeStamp, payload,
495 fragmentation);
496 } else {
497 return SendRtpAudio(frameType, payloadType, timeStamp, payload,
498 fragmentation);
499 }
500}
501
Niels Möller87e2d782019-03-07 10:18:23 +0100502int32_t ChannelSend::SendRtpAudio(AudioFrameType frameType,
Niels Möller7d76a312018-10-26 12:57:07 +0200503 uint8_t payloadType,
504 uint32_t timeStamp,
505 rtc::ArrayView<const uint8_t> payload,
506 const RTPFragmentationHeader* fragmentation) {
Niels Möller530ead42018-10-04 14:28:39 +0200507 if (_includeAudioLevelIndication) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100508 // Store current audio level in the RTP sender.
Niels Möller530ead42018-10-04 14:28:39 +0200509 // The level will be used in combination with voice-activity state
510 // (frameType) to add an RTP header extension
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100511 rtp_sender_audio_->SetAudioLevel(rms_level_.Average());
Niels Möller530ead42018-10-04 14:28:39 +0200512 }
513
Benjamin Wright84583f62018-10-04 14:22:34 -0700514 // E2EE Custom Audio Frame Encryption (This is optional).
515 // Keep this buffer around for the lifetime of the send call.
516 rtc::Buffer encrypted_audio_payload;
517 if (frame_encryptor_ != nullptr) {
518 // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline.
519 // Allocate a buffer to hold the maximum possible encrypted payload.
520 size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize(
Niels Möller7d76a312018-10-26 12:57:07 +0200521 cricket::MEDIA_TYPE_AUDIO, payload.size());
Benjamin Wright84583f62018-10-04 14:22:34 -0700522 encrypted_audio_payload.SetSize(max_ciphertext_size);
523
524 // Encrypt the audio payload into the buffer.
525 size_t bytes_written = 0;
526 int encrypt_status = frame_encryptor_->Encrypt(
527 cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(),
Niels Möller7d76a312018-10-26 12:57:07 +0200528 /*additional_data=*/nullptr, payload, encrypted_audio_payload,
529 &bytes_written);
Benjamin Wright84583f62018-10-04 14:22:34 -0700530 if (encrypt_status != 0) {
531 RTC_DLOG(LS_ERROR) << "Channel::SendData() failed encrypt audio payload: "
532 << encrypt_status;
533 return -1;
534 }
535 // Resize the buffer to the exact number of bytes actually used.
536 encrypted_audio_payload.SetSize(bytes_written);
537 // Rewrite the payloadData and size to the new encrypted payload.
Niels Möller7d76a312018-10-26 12:57:07 +0200538 payload = encrypted_audio_payload;
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700539 } else if (crypto_options_.sframe.require_frame_encryption) {
540 RTC_DLOG(LS_ERROR) << "Channel::SendData() failed sending audio payload: "
541 << "A frame encryptor is required but one is not set.";
542 return -1;
Benjamin Wright84583f62018-10-04 14:22:34 -0700543 }
544
Niels Möller530ead42018-10-04 14:28:39 +0200545 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
546 // packetization.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100547 if (!_rtpRtcpModule->OnSendingRtpFrame(timeStamp,
548 // Leaving the time when this frame was
549 // received from the capture device as
550 // undefined for voice for now.
551 -1, payloadType,
552 /*force_sender_report=*/false)) {
553 return false;
554 }
555
556 // RTCPSender has it's own copy of the timestamp offset, added in
557 // RTCPSender::BuildSR, hence we must not add the in the offset for the above
558 // call.
559 // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine
560 // knowledge of the offset to a single place.
561 const uint32_t rtp_timestamp = timeStamp + _rtpRtcpModule->StartTimestamp();
Niels Möller530ead42018-10-04 14:28:39 +0200562 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100563 if (!rtp_sender_audio_->SendAudio(frameType, payloadType, rtp_timestamp,
564 payload.data(), payload.size())) {
Niels Möller530ead42018-10-04 14:28:39 +0200565 RTC_DLOG(LS_ERROR)
566 << "ChannelSend::SendData() failed to send data to RTP/RTCP module";
567 return -1;
568 }
569
570 return 0;
571}
572
Niels Möller7d76a312018-10-26 12:57:07 +0200573int32_t ChannelSend::SendMediaTransportAudio(
Niels Möller87e2d782019-03-07 10:18:23 +0100574 AudioFrameType frameType,
Niels Möller7d76a312018-10-26 12:57:07 +0200575 uint8_t payloadType,
576 uint32_t timeStamp,
577 rtc::ArrayView<const uint8_t> payload,
578 const RTPFragmentationHeader* fragmentation) {
Niels Möller7d76a312018-10-26 12:57:07 +0200579 // TODO(nisse): Use null _transportPtr for MediaTransport.
580 // RTC_DCHECK(_transportPtr == nullptr);
581 uint64_t channel_id;
582 int sampling_rate_hz;
583 {
584 rtc::CritScope cs(&media_transport_lock_);
585 if (media_transport_payload_type_ != payloadType) {
586 // Payload type is being changed, media_transport_sampling_frequency_,
587 // no longer current.
588 return -1;
589 }
590 sampling_rate_hz = media_transport_sampling_frequency_;
591 channel_id = media_transport_channel_id_;
592 }
Mirko Bonadei1c546052019-02-04 14:50:38 +0100593 MediaTransportEncodedAudioFrame frame(
Niels Möller7d76a312018-10-26 12:57:07 +0200594 /*sampling_rate_hz=*/sampling_rate_hz,
595
596 // TODO(nisse): Timestamp and sample index are the same for all supported
597 // audio codecs except G722. Refactor audio coding module to only use
598 // sample index, and leave translation to RTP time, when needed, for
599 // RTP-specific code.
600 /*starting_sample_index=*/timeStamp,
601
602 // Sample count isn't conveniently available from the AudioCodingModule,
603 // and needs some refactoring to wire up in a good way. For now, left as
604 // zero.
Benjamin Wright17b050f2019-03-13 17:35:46 -0700605 /*samples_per_channel=*/0,
Niels Möller7d76a312018-10-26 12:57:07 +0200606
607 /*sequence_number=*/media_transport_sequence_number_,
608 MediaTransportFrameTypeForWebrtcFrameType(frameType), payloadType,
609 std::vector<uint8_t>(payload.begin(), payload.end()));
610
611 // TODO(nisse): Introduce a MediaTransportSender object bound to a specific
612 // channel id.
613 RTCError rtc_error =
614 media_transport()->SendAudioFrame(channel_id, std::move(frame));
615
616 if (!rtc_error.ok()) {
617 RTC_LOG(LS_ERROR) << "Failed to send frame, rtc_error="
618 << ToString(rtc_error.type()) << ", "
619 << rtc_error.message();
620 return -1;
621 }
622
623 ++media_transport_sequence_number_;
624
625 return 0;
626}
627
Sebastian Jansson977b3352019-03-04 17:43:34 +0100628ChannelSend::ChannelSend(Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100629 TaskQueueFactory* task_queue_factory,
Niels Möller530ead42018-10-04 14:28:39 +0200630 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +0200631 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -0800632 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +0100633 Transport* rtp_transport,
Niels Möller530ead42018-10-04 14:28:39 +0200634 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700635 RtcEventLog* rtc_event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700636 FrameEncryptorInterface* frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +0100637 const webrtc::CryptoOptions& crypto_options,
Jiawei Ou55718122018-11-09 13:17:39 -0800638 bool extmap_allow_mixed,
639 int rtcp_report_interval_ms)
Niels Möller530ead42018-10-04 14:28:39 +0200640 : event_log_(rtc_event_log),
641 _timeStamp(0), // This is just an offset, RTP module will add it's own
642 // random offset
Niels Möller530ead42018-10-04 14:28:39 +0200643 _moduleProcessThreadPtr(module_process_thread),
Niels Möller530ead42018-10-04 14:28:39 +0200644 input_mute_(false),
645 previous_frame_muted_(false),
646 _includeAudioLevelIndication(false),
Niels Möller530ead42018-10-04 14:28:39 +0200647 rtcp_observer_(new VoERtcpObserver(this)),
648 feedback_observer_proxy_(new TransportFeedbackProxy()),
649 seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
650 rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
Sebastian Jansson977b3352019-03-04 17:43:34 +0100651 retransmission_rate_limiter_(
652 new RateLimiter(clock, kMaxRetransmissionWindowMs)),
Niels Möller530ead42018-10-04 14:28:39 +0200653 use_twcc_plr_for_ana_(
654 webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled"),
Niels Möller7d76a312018-10-26 12:57:07 +0200655 media_transport_(media_transport),
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700656 frame_encryptor_(frame_encryptor),
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100657 crypto_options_(crypto_options),
658 encoder_queue_(task_queue_factory->CreateTaskQueue(
659 "AudioEncoder",
660 TaskQueueFactory::Priority::NORMAL)) {
Niels Möller530ead42018-10-04 14:28:39 +0200661 RTC_DCHECK(module_process_thread);
Niels Möllerdced9f62018-11-19 10:27:07 +0100662 module_process_thread_checker_.DetachFromThread();
663
Niels Möller530ead42018-10-04 14:28:39 +0200664 audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config()));
665
666 RtpRtcp::Configuration configuration;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800667
668 // We gradually remove codepaths that depend on RTP when using media
669 // transport. All of this logic should be moved to the future
670 // RTPMediaTransport. In this case it means that overhead and bandwidth
671 // observers should not be called when using media transport.
672 if (!media_transport_) {
Anton Sukhanov626015d2019-02-04 15:16:06 -0800673 configuration.overhead_observer = overhead_observer;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800674 configuration.bandwidth_callback = rtcp_observer_.get();
675 configuration.transport_feedback_callback = feedback_observer_proxy_.get();
676 }
677
Sebastian Jansson977b3352019-03-04 17:43:34 +0100678 configuration.clock = clock;
Niels Möller530ead42018-10-04 14:28:39 +0200679 configuration.audio = true;
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100680 configuration.clock = Clock::GetRealTimeClock();
Fredrik Solenberg3d2ed192018-12-18 09:18:33 +0100681 configuration.outgoing_transport = rtp_transport;
Niels Möller530ead42018-10-04 14:28:39 +0200682
683 configuration.paced_sender = rtp_packet_sender_proxy_.get();
684 configuration.transport_sequence_number_allocator =
685 seq_num_allocator_proxy_.get();
Niels Möller530ead42018-10-04 14:28:39 +0200686
687 configuration.event_log = event_log_;
688 configuration.rtt_stats = rtcp_rtt_stats;
689 configuration.retransmission_rate_limiter =
690 retransmission_rate_limiter_.get();
Johannes Kron9190b822018-10-29 11:22:05 +0100691 configuration.extmap_allow_mixed = extmap_allow_mixed;
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800692 configuration.rtcp_report_interval_ms = rtcp_report_interval_ms;
Niels Möller530ead42018-10-04 14:28:39 +0200693
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +0100694 _rtpRtcpModule = RtpRtcp::Create(configuration);
Niels Möller530ead42018-10-04 14:28:39 +0200695 _rtpRtcpModule->SetSendingMediaStatus(false);
Niels Möller530ead42018-10-04 14:28:39 +0200696
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100697 rtp_sender_audio_ = absl::make_unique<RTPSenderAudio>(
698 configuration.clock, _rtpRtcpModule->RtpSender());
699
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800700 // We want to invoke the 'TargetRateObserver' and |OnOverheadChanged|
701 // callbacks after the audio_coding_ is fully initialized.
702 if (media_transport_) {
703 RTC_DLOG(LS_INFO) << "Setting media_transport_ rate observers.";
704 media_transport_->AddTargetTransferRateObserver(this);
Niels Möllerd5af4022019-03-05 08:56:48 +0100705 media_transport_->SetAudioOverheadObserver(overhead_observer);
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800706 } else {
707 RTC_DLOG(LS_INFO) << "Not setting media_transport_ rate observers.";
708 }
709
Niels Möller530ead42018-10-04 14:28:39 +0200710 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
711
Niels Möller530ead42018-10-04 14:28:39 +0200712 // Ensure that RTCP is enabled by default for the created channel.
713 // Note that, the module will keep generating RTCP until it is explicitly
714 // disabled by the user.
715 // After StopListen (when no sockets exists), RTCP packets will no longer
716 // be transmitted since the Transport object will then be invalid.
717 // RTCP is enabled by default.
718 _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
719
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100720 int error = audio_coding_->RegisterTransportCallback(this);
Niels Möller530ead42018-10-04 14:28:39 +0200721 RTC_DCHECK_EQ(0, error);
722}
723
Fredrik Solenberg645a3af2018-11-16 12:51:15 +0100724ChannelSend::~ChannelSend() {
Niels Möller530ead42018-10-04 14:28:39 +0200725 RTC_DCHECK(construction_thread_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +0200726
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800727 if (media_transport_) {
728 media_transport_->RemoveTargetTransferRateObserver(this);
Niels Möllerd5af4022019-03-05 08:56:48 +0100729 media_transport_->SetAudioOverheadObserver(nullptr);
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800730 }
731
Niels Möller530ead42018-10-04 14:28:39 +0200732 StopSend();
Niels Möller530ead42018-10-04 14:28:39 +0200733 int error = audio_coding_->RegisterTransportCallback(NULL);
734 RTC_DCHECK_EQ(0, error);
735
Niels Möller530ead42018-10-04 14:28:39 +0200736 if (_moduleProcessThreadPtr)
737 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
Niels Möller530ead42018-10-04 14:28:39 +0200738}
739
Niels Möller26815232018-11-16 09:32:40 +0100740void ChannelSend::StartSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100741 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100742 RTC_DCHECK(!sending_);
743 sending_ = true;
Niels Möller530ead42018-10-04 14:28:39 +0200744
Niels Möller530ead42018-10-04 14:28:39 +0200745 _rtpRtcpModule->SetSendingMediaStatus(true);
Niels Möller26815232018-11-16 09:32:40 +0100746 int ret = _rtpRtcpModule->SetSendingStatus(true);
747 RTC_DCHECK_EQ(0, ret);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100748 // It is now OK to start processing on the encoder task queue.
749 encoder_queue_.PostTask([this] {
750 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller530ead42018-10-04 14:28:39 +0200751 encoder_queue_is_active_ = true;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100752 });
Niels Möller530ead42018-10-04 14:28:39 +0200753}
754
755void ChannelSend::StopSend() {
Niels Möller26e88b02018-11-19 15:08:13 +0100756 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100757 if (!sending_) {
Niels Möller530ead42018-10-04 14:28:39 +0200758 return;
759 }
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100760 sending_ = false;
Niels Möller530ead42018-10-04 14:28:39 +0200761
Niels Möllerc572ff32018-11-07 08:43:50 +0100762 rtc::Event flush;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100763 encoder_queue_.PostTask([this, &flush]() {
764 RTC_DCHECK_RUN_ON(&encoder_queue_);
Niels Möller530ead42018-10-04 14:28:39 +0200765 encoder_queue_is_active_ = false;
Sebastian Jansson44dd9f22019-03-08 14:50:30 +0100766 flush.Set();
767 });
Niels Möller530ead42018-10-04 14:28:39 +0200768 flush.Wait(rtc::Event::kForever);
769
Niels Möller530ead42018-10-04 14:28:39 +0200770 // Reset sending SSRC and sequence number and triggers direct transmission
771 // of RTCP BYE
772 if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
773 RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
774 }
775 _rtpRtcpModule->SetSendingMediaStatus(false);
776}
777
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100778void ChannelSend::SetEncoder(int payload_type,
Niels Möller530ead42018-10-04 14:28:39 +0200779 std::unique_ptr<AudioEncoder> encoder) {
Niels Möller26e88b02018-11-19 15:08:13 +0100780 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200781 RTC_DCHECK_GE(payload_type, 0);
782 RTC_DCHECK_LE(payload_type, 127);
Niels Möller530ead42018-10-04 14:28:39 +0200783
784 // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
785 // as well as some other things, so we collect this info and send it along.
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100786 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type,
787 encoder->RtpTimestampRateHz());
788 rtp_sender_audio_->RegisterAudioPayload("audio", payload_type,
789 encoder->RtpTimestampRateHz(),
790 encoder->NumChannels(), 0);
Niels Möller530ead42018-10-04 14:28:39 +0200791
Niels Möller7d76a312018-10-26 12:57:07 +0200792 if (media_transport_) {
793 rtc::CritScope cs(&media_transport_lock_);
794 media_transport_payload_type_ = payload_type;
795 // TODO(nisse): Currently broken for G722, since timestamps passed through
796 // encoder use RTP clock rather than sample count, and they differ for G722.
797 media_transport_sampling_frequency_ = encoder->RtpTimestampRateHz();
798 }
Niels Möller530ead42018-10-04 14:28:39 +0200799 audio_coding_->SetEncoder(std::move(encoder));
Niels Möller530ead42018-10-04 14:28:39 +0200800}
801
802void ChannelSend::ModifyEncoder(
803 rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
Anton Sukhanov626015d2019-02-04 15:16:06 -0800804 // This method can be called on the worker thread, module process thread
805 // or network thread. Audio coding is thread safe, so we do not need to
806 // enforce the calling thread.
Niels Möller530ead42018-10-04 14:28:39 +0200807 audio_coding_->ModifyEncoder(modifier);
808}
809
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100810void ChannelSend::CallEncoder(rtc::FunctionView<void(AudioEncoder*)> modifier) {
811 ModifyEncoder([modifier](std::unique_ptr<AudioEncoder>* encoder_ptr) {
812 if (*encoder_ptr) {
813 modifier(encoder_ptr->get());
814 } else {
815 RTC_DLOG(LS_WARNING) << "Trying to call unset encoder.";
816 }
817 });
818}
819
Sebastian Jansson254d8692018-11-21 19:19:00 +0100820void ChannelSend::OnBitrateAllocation(BitrateAllocationUpdate update) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100821 // This method can be called on the worker thread, module process thread
822 // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged.
823 // TODO(solenberg): Figure out a good way to check this or enforce calling
824 // rules.
825 // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
826 // module_process_thread_checker_.CalledOnValidThread());
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800827 rtc::CritScope lock(&bitrate_crit_section_);
Niels Möllerdced9f62018-11-19 10:27:07 +0100828
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100829 CallEncoder([&](AudioEncoder* encoder) {
830 encoder->OnReceivedUplinkAllocation(update);
Niels Möller530ead42018-10-04 14:28:39 +0200831 });
Sebastian Jansson254d8692018-11-21 19:19:00 +0100832 retransmission_rate_limiter_->SetMaxRate(update.target_bitrate.bps());
833 configured_bitrate_bps_ = update.target_bitrate.bps();
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200834}
835
Niels Möllerdced9f62018-11-19 10:27:07 +0100836int ChannelSend::GetBitrate() const {
Piotr (Peter) Slatala1eebec92018-11-16 09:03:35 -0800837 rtc::CritScope lock(&bitrate_crit_section_);
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200838 return configured_bitrate_bps_;
Niels Möller530ead42018-10-04 14:28:39 +0200839}
840
841void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
Niels Möller26e88b02018-11-19 15:08:13 +0100842 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200843 if (!use_twcc_plr_for_ana_)
844 return;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100845 CallEncoder([&](AudioEncoder* encoder) {
846 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200847 });
848}
849
850void ChannelSend::OnRecoverableUplinkPacketLossRate(
851 float recoverable_packet_loss_rate) {
Niels Möller26e88b02018-11-19 15:08:13 +0100852 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100853 CallEncoder([&](AudioEncoder* encoder) {
854 encoder->OnReceivedUplinkRecoverablePacketLossFraction(
855 recoverable_packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200856 });
857}
858
859void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) {
860 if (use_twcc_plr_for_ana_)
861 return;
Sebastian Jansson14a7cf92019-02-13 15:11:42 +0100862 CallEncoder([&](AudioEncoder* encoder) {
863 encoder->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
Niels Möller530ead42018-10-04 14:28:39 +0200864 });
865}
866
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100867void ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
Niels Möllerdced9f62018-11-19 10:27:07 +0100868 // May be called on either worker thread or network thread.
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800869 if (media_transport_) {
870 // Ignore RTCP packets while media transport is used.
871 // Those packets should not arrive, but we are seeing occasional packets.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100872 return;
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800873 }
874
Niels Möller530ead42018-10-04 14:28:39 +0200875 // Deliver RTCP packet to RTP/RTCP module for parsing
876 _rtpRtcpModule->IncomingRtcpPacket(data, length);
877
878 int64_t rtt = GetRTT();
879 if (rtt == 0) {
880 // Waiting for valid RTT.
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100881 return;
Niels Möller530ead42018-10-04 14:28:39 +0200882 }
883
884 int64_t nack_window_ms = rtt;
885 if (nack_window_ms < kMinRetransmissionWindowMs) {
886 nack_window_ms = kMinRetransmissionWindowMs;
887 } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
888 nack_window_ms = kMaxRetransmissionWindowMs;
889 }
890 retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
891
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -0800892 OnReceivedRtt(rtt);
Niels Möller530ead42018-10-04 14:28:39 +0200893}
894
895void ChannelSend::SetInputMute(bool enable) {
Niels Möller26e88b02018-11-19 15:08:13 +0100896 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200897 rtc::CritScope cs(&volume_settings_critsect_);
898 input_mute_ = enable;
899}
900
901bool ChannelSend::InputMute() const {
902 rtc::CritScope cs(&volume_settings_critsect_);
903 return input_mute_;
904}
905
Niels Möller26815232018-11-16 09:32:40 +0100906bool ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
Niels Möller26e88b02018-11-19 15:08:13 +0100907 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200908 RTC_DCHECK_LE(0, event);
909 RTC_DCHECK_GE(255, event);
910 RTC_DCHECK_LE(0, duration_ms);
911 RTC_DCHECK_GE(65535, duration_ms);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100912 if (!sending_) {
Niels Möller26815232018-11-16 09:32:40 +0100913 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200914 }
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100915 if (rtp_sender_audio_->SendTelephoneEvent(
Niels Möller530ead42018-10-04 14:28:39 +0200916 event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100917 RTC_DLOG(LS_ERROR) << "SendTelephoneEvent() failed to send event";
Niels Möller26815232018-11-16 09:32:40 +0100918 return false;
Niels Möller530ead42018-10-04 14:28:39 +0200919 }
Niels Möller26815232018-11-16 09:32:40 +0100920 return true;
Niels Möller530ead42018-10-04 14:28:39 +0200921}
922
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100923void ChannelSend::RegisterCngPayloadType(int payload_type,
924 int payload_frequency) {
925 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
926 rtp_sender_audio_->RegisterAudioPayload("CN", payload_type, payload_frequency,
927 1, 0);
928}
929
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100930void ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
Niels Möller26815232018-11-16 09:32:40 +0100931 int payload_frequency) {
Niels Möller26e88b02018-11-19 15:08:13 +0100932 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200933 RTC_DCHECK_LE(0, payload_type);
934 RTC_DCHECK_GE(127, payload_type);
Niels Mölleree5ccbc2019-03-06 16:47:29 +0100935 _rtpRtcpModule->RegisterSendPayloadFrequency(payload_type, payload_frequency);
936 rtp_sender_audio_->RegisterAudioPayload("telephone-event", payload_type,
937 payload_frequency, 0, 0);
Niels Möller530ead42018-10-04 14:28:39 +0200938}
939
Niels Möllerdced9f62018-11-19 10:27:07 +0100940void ChannelSend::SetLocalSSRC(uint32_t ssrc) {
Niels Möller26e88b02018-11-19 15:08:13 +0100941 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenbergeb134842018-11-19 14:13:15 +0100942 RTC_DCHECK(!sending_);
Niels Möller26815232018-11-16 09:32:40 +0100943
Niels Möller7d76a312018-10-26 12:57:07 +0200944 if (media_transport_) {
945 rtc::CritScope cs(&media_transport_lock_);
946 media_transport_channel_id_ = ssrc;
947 }
Niels Möller530ead42018-10-04 14:28:39 +0200948 _rtpRtcpModule->SetSSRC(ssrc);
Niels Möller530ead42018-10-04 14:28:39 +0200949}
950
Amit Hilbuch77938e62018-12-21 09:23:38 -0800951void ChannelSend::SetRid(const std::string& rid,
952 int extension_id,
953 int repaired_extension_id) {
954 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
955 if (extension_id != 0) {
956 int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId,
957 extension_id);
958 RTC_DCHECK_EQ(0, ret);
959 }
960 if (repaired_extension_id != 0) {
961 int ret = SetSendRtpHeaderExtension(!rid.empty(), kRtpExtensionRtpStreamId,
962 repaired_extension_id);
963 RTC_DCHECK_EQ(0, ret);
964 }
965 _rtpRtcpModule->SetRid(rid);
966}
967
Niels Möller530ead42018-10-04 14:28:39 +0200968void ChannelSend::SetMid(const std::string& mid, int extension_id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100969 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200970 int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id);
971 RTC_DCHECK_EQ(0, ret);
972 _rtpRtcpModule->SetMid(mid);
973}
974
Johannes Kron9190b822018-10-29 11:22:05 +0100975void ChannelSend::SetExtmapAllowMixed(bool extmap_allow_mixed) {
Niels Möller26e88b02018-11-19 15:08:13 +0100976 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Johannes Kron9190b822018-10-29 11:22:05 +0100977 _rtpRtcpModule->SetExtmapAllowMixed(extmap_allow_mixed);
978}
979
Niels Möller26815232018-11-16 09:32:40 +0100980void ChannelSend::SetSendAudioLevelIndicationStatus(bool enable, int id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100981 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200982 _includeAudioLevelIndication = enable;
Niels Möller26815232018-11-16 09:32:40 +0100983 int ret = SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
984 RTC_DCHECK_EQ(0, ret);
Niels Möller530ead42018-10-04 14:28:39 +0200985}
986
987void ChannelSend::EnableSendTransportSequenceNumber(int id) {
Niels Möller26e88b02018-11-19 15:08:13 +0100988 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200989 int ret =
990 SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
991 RTC_DCHECK_EQ(0, ret);
992}
993
994void ChannelSend::RegisterSenderCongestionControlObjects(
995 RtpTransportControllerSendInterface* transport,
996 RtcpBandwidthObserver* bandwidth_observer) {
Niels Möller26e88b02018-11-19 15:08:13 +0100997 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +0200998 RtpPacketSender* rtp_packet_sender = transport->packet_sender();
999 TransportFeedbackObserver* transport_feedback_observer =
1000 transport->transport_feedback_observer();
1001 PacketRouter* packet_router = transport->packet_router();
1002
1003 RTC_DCHECK(rtp_packet_sender);
1004 RTC_DCHECK(transport_feedback_observer);
1005 RTC_DCHECK(packet_router);
1006 RTC_DCHECK(!packet_router_);
1007 rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
1008 feedback_observer_proxy_->SetTransportFeedbackObserver(
1009 transport_feedback_observer);
1010 seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
1011 rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
1012 _rtpRtcpModule->SetStorePacketsStatus(true, 600);
1013 constexpr bool remb_candidate = false;
1014 packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
1015 packet_router_ = packet_router;
1016}
1017
1018void ChannelSend::ResetSenderCongestionControlObjects() {
Niels Möller26e88b02018-11-19 15:08:13 +01001019 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001020 RTC_DCHECK(packet_router_);
1021 _rtpRtcpModule->SetStorePacketsStatus(false, 600);
1022 rtcp_observer_->SetBandwidthObserver(nullptr);
1023 feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
1024 seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
1025 packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
1026 packet_router_ = nullptr;
1027 rtp_packet_sender_proxy_->SetPacketSender(nullptr);
1028}
1029
Niels Möller26815232018-11-16 09:32:40 +01001030void ChannelSend::SetRTCP_CNAME(absl::string_view c_name) {
Niels Möller26e88b02018-11-19 15:08:13 +01001031 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +01001032 // Note: SetCNAME() accepts a c string of length at most 255.
1033 const std::string c_name_limited(c_name.substr(0, 255));
1034 int ret = _rtpRtcpModule->SetCNAME(c_name_limited.c_str()) != 0;
1035 RTC_DCHECK_EQ(0, ret) << "SetRTCP_CNAME() failed to set RTCP CNAME";
Niels Möller530ead42018-10-04 14:28:39 +02001036}
1037
Niels Möller26815232018-11-16 09:32:40 +01001038std::vector<ReportBlock> ChannelSend::GetRemoteRTCPReportBlocks() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001039 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001040 // Get the report blocks from the latest received RTCP Sender or Receiver
1041 // Report. Each element in the vector contains the sender's SSRC and a
1042 // report block according to RFC 3550.
1043 std::vector<RTCPReportBlock> rtcp_report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001044
Niels Möller26815232018-11-16 09:32:40 +01001045 int ret = _rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks);
1046 RTC_DCHECK_EQ(0, ret);
1047
1048 std::vector<ReportBlock> report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001049
1050 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
1051 for (; it != rtcp_report_blocks.end(); ++it) {
1052 ReportBlock report_block;
1053 report_block.sender_SSRC = it->sender_ssrc;
1054 report_block.source_SSRC = it->source_ssrc;
1055 report_block.fraction_lost = it->fraction_lost;
1056 report_block.cumulative_num_packets_lost = it->packets_lost;
1057 report_block.extended_highest_sequence_number =
1058 it->extended_highest_sequence_number;
1059 report_block.interarrival_jitter = it->jitter;
1060 report_block.last_SR_timestamp = it->last_sender_report_timestamp;
1061 report_block.delay_since_last_SR = it->delay_since_last_sender_report;
Niels Möller26815232018-11-16 09:32:40 +01001062 report_blocks.push_back(report_block);
Niels Möller530ead42018-10-04 14:28:39 +02001063 }
Niels Möller26815232018-11-16 09:32:40 +01001064 return report_blocks;
Niels Möller530ead42018-10-04 14:28:39 +02001065}
1066
Niels Möller26815232018-11-16 09:32:40 +01001067CallSendStatistics ChannelSend::GetRTCPStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001068 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller26815232018-11-16 09:32:40 +01001069 CallSendStatistics stats = {0};
Niels Möller530ead42018-10-04 14:28:39 +02001070 stats.rttMs = GetRTT();
1071
Niels Möller530ead42018-10-04 14:28:39 +02001072 size_t bytesSent(0);
1073 uint32_t packetsSent(0);
1074
1075 if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
1076 RTC_DLOG(LS_WARNING)
1077 << "GetRTPStatistics() failed to retrieve RTP datacounters"
1078 << " => output will not be complete";
1079 }
1080
1081 stats.bytesSent = bytesSent;
1082 stats.packetsSent = packetsSent;
1083
Niels Möller26815232018-11-16 09:32:40 +01001084 return stats;
Niels Möller530ead42018-10-04 14:28:39 +02001085}
1086
Niels Möller530ead42018-10-04 14:28:39 +02001087void ChannelSend::ProcessAndEncodeAudio(
1088 std::unique_ptr<AudioFrame> audio_frame) {
Niels Möllerdced9f62018-11-19 10:27:07 +01001089 RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001090 struct ProcessAndEncodeAudio {
1091 void operator()() {
1092 RTC_DCHECK_RUN_ON(&channel->encoder_queue_);
1093 if (!channel->encoder_queue_is_active_) {
1094 return;
1095 }
1096 channel->ProcessAndEncodeAudioOnTaskQueue(audio_frame.get());
1097 }
1098 std::unique_ptr<AudioFrame> audio_frame;
1099 ChannelSend* const channel;
1100 };
Niels Möller530ead42018-10-04 14:28:39 +02001101 // Profile time between when the audio frame is added to the task queue and
1102 // when the task is actually executed.
1103 audio_frame->UpdateProfileTimeStamp();
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001104 encoder_queue_.PostTask(ProcessAndEncodeAudio{std::move(audio_frame), this});
Niels Möller530ead42018-10-04 14:28:39 +02001105}
1106
1107void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
Niels Möller530ead42018-10-04 14:28:39 +02001108 RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
1109 RTC_DCHECK_LE(audio_input->num_channels_, 2);
1110
1111 // Measure time between when the audio frame is added to the task queue and
1112 // when the task is actually executed. Goal is to keep track of unwanted
1113 // extra latency added by the task queue.
1114 RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
1115 audio_input->ElapsedProfileTimeMs());
1116
1117 bool is_muted = InputMute();
1118 AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
1119
1120 if (_includeAudioLevelIndication) {
1121 size_t length =
1122 audio_input->samples_per_channel_ * audio_input->num_channels_;
1123 RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
1124 if (is_muted && previous_frame_muted_) {
1125 rms_level_.AnalyzeMuted(length);
1126 } else {
1127 rms_level_.Analyze(
1128 rtc::ArrayView<const int16_t>(audio_input->data(), length));
1129 }
1130 }
1131 previous_frame_muted_ = is_muted;
1132
1133 // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
1134
1135 // The ACM resamples internally.
1136 audio_input->timestamp_ = _timeStamp;
1137 // This call will trigger AudioPacketizationCallback::SendData if encoding
1138 // is done and payload is ready for packetization and transmission.
1139 // Otherwise, it will return without invoking the callback.
1140 if (audio_coding_->Add10MsData(*audio_input) < 0) {
1141 RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
1142 return;
1143 }
1144
1145 _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
1146}
1147
Niels Möller530ead42018-10-04 14:28:39 +02001148ANAStats ChannelSend::GetANAStatistics() const {
Niels Möller26e88b02018-11-19 15:08:13 +01001149 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller530ead42018-10-04 14:28:39 +02001150 return audio_coding_->GetANAStats();
1151}
1152
1153RtpRtcp* ChannelSend::GetRtpRtcp() const {
Niels Möllerdced9f62018-11-19 10:27:07 +01001154 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
Niels Möller530ead42018-10-04 14:28:39 +02001155 return _rtpRtcpModule.get();
1156}
1157
1158int ChannelSend::SetSendRtpHeaderExtension(bool enable,
1159 RTPExtensionType type,
Niels Möller26815232018-11-16 09:32:40 +01001160 int id) {
Niels Möller530ead42018-10-04 14:28:39 +02001161 int error = 0;
1162 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
1163 if (enable) {
Niels Möller26815232018-11-16 09:32:40 +01001164 // TODO(nisse): RtpRtcp::RegisterSendRtpHeaderExtension to take an int
1165 // argument. Currently it wants an uint8_t.
1166 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(
1167 type, rtc::dchecked_cast<uint8_t>(id));
Niels Möller530ead42018-10-04 14:28:39 +02001168 }
1169 return error;
1170}
1171
Niels Möller530ead42018-10-04 14:28:39 +02001172int64_t ChannelSend::GetRTT() const {
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001173 if (media_transport_) {
1174 // GetRTT is generally used in the RTCP codepath, where media transport is
1175 // not present and so it shouldn't be needed. But it's also invoked in
1176 // 'GetStats' method, and for now returning media transport RTT here gives
1177 // us "free" rtt stats for media transport.
1178 auto target_rate = media_transport_->GetLatestTargetTransferRate();
1179 if (target_rate.has_value()) {
1180 return target_rate.value().network_estimate.round_trip_time.ms();
1181 }
1182
1183 return 0;
1184 }
Niels Möller530ead42018-10-04 14:28:39 +02001185 RtcpMode method = _rtpRtcpModule->RTCP();
1186 if (method == RtcpMode::kOff) {
1187 return 0;
1188 }
1189 std::vector<RTCPReportBlock> report_blocks;
1190 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
1191
1192 if (report_blocks.empty()) {
1193 return 0;
1194 }
1195
1196 int64_t rtt = 0;
1197 int64_t avg_rtt = 0;
1198 int64_t max_rtt = 0;
1199 int64_t min_rtt = 0;
1200 // We don't know in advance the remote ssrc used by the other end's receiver
1201 // reports, so use the SSRC of the first report block for calculating the RTT.
1202 if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt,
1203 &min_rtt, &max_rtt) != 0) {
1204 return 0;
1205 }
1206 return rtt;
1207}
1208
Benjamin Wright78410ad2018-10-25 09:52:57 -07001209void ChannelSend::SetFrameEncryptor(
1210 rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
Niels Möller26e88b02018-11-19 15:08:13 +01001211 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001212 encoder_queue_.PostTask([this, frame_encryptor]() mutable {
1213 RTC_DCHECK_RUN_ON(&encoder_queue_);
Sebastian Jansson7949f212019-03-05 13:41:48 +00001214 frame_encryptor_ = std::move(frame_encryptor);
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001215 });
Benjamin Wright84583f62018-10-04 14:22:34 -07001216}
1217
Anton Sukhanov626015d2019-02-04 15:16:06 -08001218// TODO(sukhanov): Consider moving TargetTransferRate observer to
1219// AudioSendStream. Since AudioSendStream owns encoder and configures ANA, it
1220// makes sense to consolidate all rate (and overhead) calculation there.
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001221void ChannelSend::OnTargetTransferRate(TargetTransferRate rate) {
1222 RTC_DCHECK(media_transport_);
1223 OnReceivedRtt(rate.network_estimate.round_trip_time.ms());
1224}
1225
1226void ChannelSend::OnReceivedRtt(int64_t rtt_ms) {
1227 // Invoke audio encoders OnReceivedRtt().
Sebastian Jansson14a7cf92019-02-13 15:11:42 +01001228 CallEncoder(
1229 [rtt_ms](AudioEncoder* encoder) { encoder->OnReceivedRtt(rtt_ms); });
Piotr (Peter) Slatala179a3922018-11-16 09:57:58 -08001230}
1231
Niels Möllerdced9f62018-11-19 10:27:07 +01001232} // namespace
1233
1234std::unique_ptr<ChannelSendInterface> CreateChannelSend(
Sebastian Jansson977b3352019-03-04 17:43:34 +01001235 Clock* clock,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001236 TaskQueueFactory* task_queue_factory,
Niels Möllerdced9f62018-11-19 10:27:07 +01001237 ProcessThread* module_process_thread,
1238 MediaTransportInterface* media_transport,
Anton Sukhanov626015d2019-02-04 15:16:06 -08001239 OverheadObserver* overhead_observer,
Niels Möllere9771992018-11-26 10:55:07 +01001240 Transport* rtp_transport,
Niels Möllerdced9f62018-11-19 10:27:07 +01001241 RtcpRttStats* rtcp_rtt_stats,
1242 RtcEventLog* rtc_event_log,
1243 FrameEncryptorInterface* frame_encryptor,
1244 const webrtc::CryptoOptions& crypto_options,
1245 bool extmap_allow_mixed,
1246 int rtcp_report_interval_ms) {
1247 return absl::make_unique<ChannelSend>(
Sebastian Jansson44dd9f22019-03-08 14:50:30 +01001248 clock, task_queue_factory, module_process_thread, media_transport,
Sebastian Jansson977b3352019-03-04 17:43:34 +01001249 overhead_observer, rtp_transport, rtcp_rtt_stats, rtc_event_log,
1250 frame_encryptor, crypto_options, extmap_allow_mixed,
1251 rtcp_report_interval_ms);
Niels Möllerdced9f62018-11-19 10:27:07 +01001252}
1253
Niels Möller530ead42018-10-04 14:28:39 +02001254} // namespace voe
1255} // namespace webrtc