1 /*
2  *  Copyright (c) 2004 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 "media/engine/webrtc_voice_engine.h"
12 
13 #include <algorithm>
14 #include <cstdio>
15 #include <functional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 #include "absl/algorithm/container.h"
21 #include "absl/strings/match.h"
22 #include "api/audio_codecs/audio_codec_pair_id.h"
23 #include "api/call/audio_sink.h"
24 #include "media/base/audio_source.h"
25 #include "media/base/media_constants.h"
26 #include "media/base/stream_params.h"
27 #include "media/engine/adm_helpers.h"
28 #include "media/engine/payload_type_mapper.h"
29 #include "media/engine/webrtc_media_engine.h"
30 #include "modules/audio_device/audio_device_impl.h"
31 #include "modules/audio_mixer/audio_mixer_impl.h"
32 #include "modules/audio_processing/aec_dump/aec_dump_factory.h"
33 #include "modules/audio_processing/include/audio_processing.h"
34 #include "rtc_base/arraysize.h"
35 #include "rtc_base/byte_order.h"
36 #include "rtc_base/constructor_magic.h"
37 #include "rtc_base/experiments/field_trial_parser.h"
38 #include "rtc_base/experiments/field_trial_units.h"
39 #include "rtc_base/experiments/struct_parameters_parser.h"
40 #include "rtc_base/helpers.h"
41 #include "rtc_base/ignore_wundef.h"
42 #include "rtc_base/logging.h"
43 #include "rtc_base/race_checker.h"
44 #include "rtc_base/strings/audio_format_to_string.h"
45 #include "rtc_base/strings/string_builder.h"
46 #include "rtc_base/third_party/base64/base64.h"
47 #include "rtc_base/trace_event.h"
48 #include "system_wrappers/include/field_trial.h"
49 #include "system_wrappers/include/metrics.h"
50 
51 #if WEBRTC_ENABLE_PROTOBUF
52 RTC_PUSH_IGNORING_WUNDEF()
53 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
54 #include "external/webrtc/webrtc/modules/audio_coding/audio_network_adaptor/config.pb.h"
55 #else
56 #include "modules/audio_coding/audio_network_adaptor/config.pb.h"
57 #endif
58 RTC_POP_IGNORING_WUNDEF()
59 #endif
60 
61 namespace cricket {
62 namespace {
63 
64 constexpr size_t kMaxUnsignaledRecvStreams = 4;
65 
66 constexpr int kNackRtpHistoryMs = 5000;
67 
68 const int kMinTelephoneEventCode = 0;  // RFC4733 (Section 2.3.1)
69 const int kMaxTelephoneEventCode = 255;
70 
71 const int kMinPayloadType = 0;
72 const int kMaxPayloadType = 127;
73 
74 class ProxySink : public webrtc::AudioSinkInterface {
75  public:
ProxySink(AudioSinkInterface * sink)76   explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) {
77     RTC_DCHECK(sink);
78   }
79 
OnData(const Data & audio)80   void OnData(const Data& audio) override { sink_->OnData(audio); }
81 
82  private:
83   webrtc::AudioSinkInterface* sink_;
84 };
85 
ValidateStreamParams(const StreamParams & sp)86 bool ValidateStreamParams(const StreamParams& sp) {
87   if (sp.ssrcs.empty()) {
88     RTC_DLOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
89     return false;
90   }
91   if (sp.ssrcs.size() > 1) {
92     RTC_DLOG(LS_ERROR) << "Multiple SSRCs in stream parameters: "
93                        << sp.ToString();
94     return false;
95   }
96   return true;
97 }
98 
99 // Dumps an AudioCodec in RFC 2327-ish format.
ToString(const AudioCodec & codec)100 std::string ToString(const AudioCodec& codec) {
101   rtc::StringBuilder ss;
102   ss << codec.name << "/" << codec.clockrate << "/" << codec.channels;
103   if (!codec.params.empty()) {
104     ss << " {";
105     for (const auto& param : codec.params) {
106       ss << " " << param.first << "=" << param.second;
107     }
108     ss << " }";
109   }
110   ss << " (" << codec.id << ")";
111   return ss.Release();
112 }
113 
114 // If this field trial is enabled, we will negotiate and use RFC 2198
115 // redundancy for opus audio.
IsAudioRedForOpusFieldTrialEnabled()116 bool IsAudioRedForOpusFieldTrialEnabled() {
117   return webrtc::field_trial::IsEnabled("WebRTC-Audio-Red-For-Opus");
118 }
119 
IsCodec(const AudioCodec & codec,const char * ref_name)120 bool IsCodec(const AudioCodec& codec, const char* ref_name) {
121   return absl::EqualsIgnoreCase(codec.name, ref_name);
122 }
123 
FindCodec(const std::vector<AudioCodec> & codecs,const AudioCodec & codec,AudioCodec * found_codec)124 bool FindCodec(const std::vector<AudioCodec>& codecs,
125                const AudioCodec& codec,
126                AudioCodec* found_codec) {
127   for (const AudioCodec& c : codecs) {
128     if (c.Matches(codec)) {
129       if (found_codec != NULL) {
130         *found_codec = c;
131       }
132       return true;
133     }
134   }
135   return false;
136 }
137 
VerifyUniquePayloadTypes(const std::vector<AudioCodec> & codecs)138 bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
139   if (codecs.empty()) {
140     return true;
141   }
142   std::vector<int> payload_types;
143   absl::c_transform(codecs, std::back_inserter(payload_types),
144                     [](const AudioCodec& codec) { return codec.id; });
145   absl::c_sort(payload_types);
146   return absl::c_adjacent_find(payload_types) == payload_types.end();
147 }
148 
GetAudioNetworkAdaptorConfig(const AudioOptions & options)149 absl::optional<std::string> GetAudioNetworkAdaptorConfig(
150     const AudioOptions& options) {
151   if (options.audio_network_adaptor && *options.audio_network_adaptor &&
152       options.audio_network_adaptor_config) {
153     // Turn on audio network adaptor only when |options_.audio_network_adaptor|
154     // equals true and |options_.audio_network_adaptor_config| has a value.
155     return options.audio_network_adaptor_config;
156   }
157   return absl::nullopt;
158 }
159 
160 // Returns its smallest positive argument. If neither argument is positive,
161 // returns an arbitrary nonpositive value.
MinPositive(int a,int b)162 int MinPositive(int a, int b) {
163   if (a <= 0) {
164     return b;
165   }
166   if (b <= 0) {
167     return a;
168   }
169   return std::min(a, b);
170 }
171 
172 // |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
173 // |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
ComputeSendBitrate(int max_send_bitrate_bps,absl::optional<int> rtp_max_bitrate_bps,const webrtc::AudioCodecSpec & spec)174 absl::optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
175                                        absl::optional<int> rtp_max_bitrate_bps,
176                                        const webrtc::AudioCodecSpec& spec) {
177   // If application-configured bitrate is set, take minimum of that and SDP
178   // bitrate.
179   const int bps = rtp_max_bitrate_bps
180                       ? MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
181                       : max_send_bitrate_bps;
182   if (bps <= 0) {
183     return spec.info.default_bitrate_bps;
184   }
185 
186   if (bps < spec.info.min_bitrate_bps) {
187     // If codec is not multi-rate and |bps| is less than the fixed bitrate then
188     // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
189     // bitrate then ignore.
190     RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
191                       << " to bitrate " << bps
192                       << " bps"
193                          ", requires at least "
194                       << spec.info.min_bitrate_bps << " bps.";
195     return absl::nullopt;
196   }
197 
198   if (spec.info.HasFixedBitrate()) {
199     return spec.info.default_bitrate_bps;
200   } else {
201     // If codec is multi-rate then just set the bitrate.
202     return std::min(bps, spec.info.max_bitrate_bps);
203   }
204 }
205 
206 struct AdaptivePtimeConfig {
207   bool enabled = false;
208   webrtc::DataRate min_payload_bitrate = webrtc::DataRate::KilobitsPerSec(16);
209   webrtc::DataRate min_encoder_bitrate = webrtc::DataRate::KilobitsPerSec(12);
210   bool use_slow_adaptation = true;
211 
212   absl::optional<std::string> audio_network_adaptor_config;
213 
Parsercricket::__anon7bb45d800111::AdaptivePtimeConfig214   std::unique_ptr<webrtc::StructParametersParser> Parser() {
215     return webrtc::StructParametersParser::Create(    //
216         "enabled", &enabled,                          //
217         "min_payload_bitrate", &min_payload_bitrate,  //
218         "min_encoder_bitrate", &min_encoder_bitrate,  //
219         "use_slow_adaptation", &use_slow_adaptation);
220   }
221 
AdaptivePtimeConfigcricket::__anon7bb45d800111::AdaptivePtimeConfig222   AdaptivePtimeConfig() {
223     Parser()->Parse(
224         webrtc::field_trial::FindFullName("WebRTC-Audio-AdaptivePtime"));
225 #if WEBRTC_ENABLE_PROTOBUF
226     webrtc::audio_network_adaptor::config::ControllerManager config;
227     auto* frame_length_controller =
228         config.add_controllers()->mutable_frame_length_controller_v2();
229     frame_length_controller->set_min_payload_bitrate_bps(
230         min_payload_bitrate.bps());
231     frame_length_controller->set_use_slow_adaptation(use_slow_adaptation);
232     config.add_controllers()->mutable_bitrate_controller();
233     audio_network_adaptor_config = config.SerializeAsString();
234 #endif
235   }
236 };
237 
238 }  // namespace
239 
WebRtcVoiceEngine(webrtc::TaskQueueFactory * task_queue_factory,webrtc::AudioDeviceModule * adm,const rtc::scoped_refptr<webrtc::AudioEncoderFactory> & encoder_factory,const rtc::scoped_refptr<webrtc::AudioDecoderFactory> & decoder_factory,rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)240 WebRtcVoiceEngine::WebRtcVoiceEngine(
241     webrtc::TaskQueueFactory* task_queue_factory,
242     webrtc::AudioDeviceModule* adm,
243     const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
244     const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
245     rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
246     rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
247     : task_queue_factory_(task_queue_factory),
248       adm_(adm),
249       encoder_factory_(encoder_factory),
250       decoder_factory_(decoder_factory),
251       audio_mixer_(audio_mixer),
252       apm_(audio_processing) {
253   // This may be called from any thread, so detach thread checkers.
254   worker_thread_checker_.Detach();
255   signal_thread_checker_.Detach();
256   RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
257   RTC_DCHECK(decoder_factory);
258   RTC_DCHECK(encoder_factory);
259   // The rest of our initialization will happen in Init.
260 }
261 
~WebRtcVoiceEngine()262 WebRtcVoiceEngine::~WebRtcVoiceEngine() {
263   RTC_DCHECK(worker_thread_checker_.IsCurrent());
264   RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
265   if (initialized_) {
266     StopAecDump();
267 
268     // Stop AudioDevice.
269     adm()->StopPlayout();
270     adm()->StopRecording();
271     adm()->RegisterAudioCallback(nullptr);
272     adm()->Terminate();
273   }
274 }
275 
Init()276 void WebRtcVoiceEngine::Init() {
277   RTC_DCHECK(worker_thread_checker_.IsCurrent());
278   RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
279 
280   // TaskQueue expects to be created/destroyed on the same thread.
281   low_priority_worker_queue_.reset(
282       new rtc::TaskQueue(task_queue_factory_->CreateTaskQueue(
283           "rtc-low-prio", webrtc::TaskQueueFactory::Priority::LOW)));
284 
285   // Load our audio codec lists.
286   RTC_LOG(LS_VERBOSE) << "Supported send codecs in order of preference:";
287   send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
288   for (const AudioCodec& codec : send_codecs_) {
289     RTC_LOG(LS_VERBOSE) << ToString(codec);
290   }
291 
292   RTC_LOG(LS_VERBOSE) << "Supported recv codecs in order of preference:";
293   recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
294   for (const AudioCodec& codec : recv_codecs_) {
295     RTC_LOG(LS_VERBOSE) << ToString(codec);
296   }
297 
298 #if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
299   // No ADM supplied? Create a default one.
300   if (!adm_) {
301     adm_ = webrtc::AudioDeviceModule::Create(
302         webrtc::AudioDeviceModule::kPlatformDefaultAudio, task_queue_factory_);
303   }
304 #endif  // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
305   RTC_CHECK(adm());
306   webrtc::adm_helpers::Init(adm());
307 
308   // Set up AudioState.
309   {
310     webrtc::AudioState::Config config;
311     if (audio_mixer_) {
312       config.audio_mixer = audio_mixer_;
313     } else {
314       config.audio_mixer = webrtc::AudioMixerImpl::Create();
315     }
316     config.audio_processing = apm_;
317     config.audio_device_module = adm_;
318     audio_state_ = webrtc::AudioState::Create(config);
319   }
320 
321   // Connect the ADM to our audio path.
322   adm()->RegisterAudioCallback(audio_state()->audio_transport());
323 
324   // Set default engine options.
325   {
326     AudioOptions options;
327     options.echo_cancellation = true;
328     options.auto_gain_control = true;
329     options.noise_suppression = true;
330     options.highpass_filter = true;
331     options.stereo_swapping = false;
332     options.audio_jitter_buffer_max_packets = 200;
333     options.audio_jitter_buffer_fast_accelerate = false;
334     options.audio_jitter_buffer_min_delay_ms = 0;
335     options.audio_jitter_buffer_enable_rtx_handling = false;
336     options.typing_detection = true;
337     options.experimental_agc = false;
338     options.experimental_ns = false;
339     options.residual_echo_detector = true;
340     bool error = ApplyOptions(options);
341     RTC_DCHECK(error);
342   }
343 
344   initialized_ = true;
345 }
346 
GetAudioState() const347 rtc::scoped_refptr<webrtc::AudioState> WebRtcVoiceEngine::GetAudioState()
348     const {
349   RTC_DCHECK(worker_thread_checker_.IsCurrent());
350   return audio_state_;
351 }
352 
CreateMediaChannel(webrtc::Call * call,const MediaConfig & config,const AudioOptions & options,const webrtc::CryptoOptions & crypto_options)353 VoiceMediaChannel* WebRtcVoiceEngine::CreateMediaChannel(
354     webrtc::Call* call,
355     const MediaConfig& config,
356     const AudioOptions& options,
357     const webrtc::CryptoOptions& crypto_options) {
358   RTC_DCHECK(worker_thread_checker_.IsCurrent());
359   return new WebRtcVoiceMediaChannel(this, config, options, crypto_options,
360                                      call);
361 }
362 
ApplyOptions(const AudioOptions & options_in)363 bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
364   RTC_DCHECK(worker_thread_checker_.IsCurrent());
365   RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
366                    << options_in.ToString();
367   AudioOptions options = options_in;  // The options are modified below.
368 
369   // Set and adjust echo canceller options.
370   // Use desktop AEC by default, when not using hardware AEC.
371   bool use_mobile_software_aec = false;
372 
373 #if defined(WEBRTC_IOS)
374   if (options.ios_force_software_aec_HACK &&
375       *options.ios_force_software_aec_HACK) {
376     // EC may be forced on for a device known to have non-functioning platform
377     // AEC.
378     options.echo_cancellation = true;
379     RTC_LOG(LS_WARNING)
380         << "Force software AEC on iOS. May conflict with platform AEC.";
381   } else {
382     // On iOS, VPIO provides built-in EC.
383     options.echo_cancellation = false;
384     RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
385   }
386 #elif defined(WEBRTC_ANDROID)
387   use_mobile_software_aec = true;
388 #endif
389 
390 // Set and adjust noise suppressor options.
391 #if defined(WEBRTC_IOS)
392   // On iOS, VPIO provides built-in NS.
393   options.noise_suppression = false;
394   options.typing_detection = false;
395   options.experimental_ns = false;
396   RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
397 #elif defined(WEBRTC_ANDROID)
398   options.typing_detection = false;
399   options.experimental_ns = false;
400 #endif
401 
402 // Set and adjust gain control options.
403 #if defined(WEBRTC_IOS)
404   // On iOS, VPIO provides built-in AGC.
405   options.auto_gain_control = false;
406   options.experimental_agc = false;
407   RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
408 #elif defined(WEBRTC_ANDROID)
409   options.experimental_agc = false;
410 #endif
411 
412 #if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
413   // Turn off the gain control if specified by the field trial.
414   // The purpose of the field trial is to reduce the amount of resampling
415   // performed inside the audio processing module on mobile platforms by
416   // whenever possible turning off the fixed AGC mode and the high-pass filter.
417   // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
418   if (webrtc::field_trial::IsEnabled(
419           "WebRTC-Audio-MinimizeResamplingOnMobile")) {
420     options.auto_gain_control = false;
421     RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
422     if (!(options.noise_suppression.value_or(false) ||
423           options.echo_cancellation.value_or(false))) {
424       // If possible, turn off the high-pass filter.
425       RTC_LOG(LS_INFO)
426           << "Disable high-pass filter in response to field trial.";
427       options.highpass_filter = false;
428     }
429   }
430 #endif
431 
432   if (options.echo_cancellation) {
433     // Check if platform supports built-in EC. Currently only supported on
434     // Android and in combination with Java based audio layer.
435     // TODO(henrika): investigate possibility to support built-in EC also
436     // in combination with Open SL ES audio.
437     const bool built_in_aec = adm()->BuiltInAECIsAvailable();
438     if (built_in_aec) {
439       // Built-in EC exists on this device. Enable/Disable it according to the
440       // echo_cancellation audio option.
441       const bool enable_built_in_aec = *options.echo_cancellation;
442       if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
443           enable_built_in_aec) {
444         // Disable internal software EC if built-in EC is enabled,
445         // i.e., replace the software EC with the built-in EC.
446         options.echo_cancellation = false;
447         RTC_LOG(LS_INFO)
448             << "Disabling EC since built-in EC will be used instead";
449       }
450     }
451   }
452 
453   if (options.auto_gain_control) {
454     bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
455     if (built_in_agc_avaliable) {
456       if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
457           *options.auto_gain_control) {
458         // Disable internal software AGC if built-in AGC is enabled,
459         // i.e., replace the software AGC with the built-in AGC.
460         options.auto_gain_control = false;
461         RTC_LOG(LS_INFO)
462             << "Disabling AGC since built-in AGC will be used instead";
463       }
464     }
465   }
466 
467   if (options.noise_suppression) {
468     if (adm()->BuiltInNSIsAvailable()) {
469       bool builtin_ns = *options.noise_suppression;
470       if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
471         // Disable internal software NS if built-in NS is enabled,
472         // i.e., replace the software NS with the built-in NS.
473         options.noise_suppression = false;
474         RTC_LOG(LS_INFO)
475             << "Disabling NS since built-in NS will be used instead";
476       }
477     }
478   }
479 
480   if (options.stereo_swapping) {
481     RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
482     audio_state()->SetStereoChannelSwapping(*options.stereo_swapping);
483   }
484 
485   if (options.audio_jitter_buffer_max_packets) {
486     RTC_LOG(LS_INFO) << "NetEq capacity is "
487                      << *options.audio_jitter_buffer_max_packets;
488     audio_jitter_buffer_max_packets_ =
489         std::max(20, *options.audio_jitter_buffer_max_packets);
490   }
491   if (options.audio_jitter_buffer_fast_accelerate) {
492     RTC_LOG(LS_INFO) << "NetEq fast mode? "
493                      << *options.audio_jitter_buffer_fast_accelerate;
494     audio_jitter_buffer_fast_accelerate_ =
495         *options.audio_jitter_buffer_fast_accelerate;
496   }
497   if (options.audio_jitter_buffer_min_delay_ms) {
498     RTC_LOG(LS_INFO) << "NetEq minimum delay is "
499                      << *options.audio_jitter_buffer_min_delay_ms;
500     audio_jitter_buffer_min_delay_ms_ =
501         *options.audio_jitter_buffer_min_delay_ms;
502   }
503   if (options.audio_jitter_buffer_enable_rtx_handling) {
504     RTC_LOG(LS_INFO) << "NetEq handle reordered packets? "
505                      << *options.audio_jitter_buffer_enable_rtx_handling;
506     audio_jitter_buffer_enable_rtx_handling_ =
507         *options.audio_jitter_buffer_enable_rtx_handling;
508   }
509 
510   webrtc::AudioProcessing* ap = apm();
511   if (!ap) {
512     RTC_LOG(LS_INFO)
513         << "No audio processing module present. No software-provided effects "
514            "(AEC, NS, AGC, ...) are activated";
515     return true;
516   }
517 
518   webrtc::Config config;
519 
520   if (options.experimental_ns) {
521     experimental_ns_ = options.experimental_ns;
522   }
523   if (experimental_ns_) {
524     RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
525     config.Set<webrtc::ExperimentalNs>(
526         new webrtc::ExperimentalNs(*experimental_ns_));
527   }
528 
529   webrtc::AudioProcessing::Config apm_config = ap->GetConfig();
530 
531   if (options.echo_cancellation) {
532     apm_config.echo_canceller.enabled = *options.echo_cancellation;
533     apm_config.echo_canceller.mobile_mode = use_mobile_software_aec;
534   }
535 
536   if (options.auto_gain_control) {
537     const bool enabled = *options.auto_gain_control;
538     apm_config.gain_controller1.enabled = enabled;
539 #if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
540     apm_config.gain_controller1.mode =
541         apm_config.gain_controller1.kFixedDigital;
542 #else
543     apm_config.gain_controller1.mode =
544         apm_config.gain_controller1.kAdaptiveAnalog;
545 #endif
546     constexpr int kMinVolumeLevel = 0;
547     constexpr int kMaxVolumeLevel = 255;
548     apm_config.gain_controller1.analog_level_minimum = kMinVolumeLevel;
549     apm_config.gain_controller1.analog_level_maximum = kMaxVolumeLevel;
550   }
551   if (options.tx_agc_target_dbov) {
552     apm_config.gain_controller1.target_level_dbfs = *options.tx_agc_target_dbov;
553   }
554   if (options.tx_agc_digital_compression_gain) {
555     apm_config.gain_controller1.compression_gain_db =
556         *options.tx_agc_digital_compression_gain;
557   }
558   if (options.tx_agc_limiter) {
559     apm_config.gain_controller1.enable_limiter = *options.tx_agc_limiter;
560   }
561 
562   if (options.highpass_filter) {
563     apm_config.high_pass_filter.enabled = *options.highpass_filter;
564   }
565 
566   if (options.residual_echo_detector) {
567     apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
568   }
569 
570   if (options.noise_suppression) {
571     const bool enabled = *options.noise_suppression;
572     apm_config.noise_suppression.enabled = enabled;
573     apm_config.noise_suppression.level =
574         webrtc::AudioProcessing::Config::NoiseSuppression::Level::kHigh;
575     RTC_LOG(LS_INFO) << "NS set to " << enabled;
576   }
577 
578   if (options.typing_detection) {
579     RTC_LOG(LS_INFO) << "Typing detection is enabled? "
580                      << *options.typing_detection;
581     apm_config.voice_detection.enabled = *options.typing_detection;
582   }
583 
584   ap->SetExtraOptions(config);
585   ap->ApplyConfig(apm_config);
586   return true;
587 }
588 
send_codecs() const589 const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
590   RTC_DCHECK(signal_thread_checker_.IsCurrent());
591   return send_codecs_;
592 }
593 
recv_codecs() const594 const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
595   RTC_DCHECK(signal_thread_checker_.IsCurrent());
596   return recv_codecs_;
597 }
598 
599 std::vector<webrtc::RtpHeaderExtensionCapability>
GetRtpHeaderExtensions() const600 WebRtcVoiceEngine::GetRtpHeaderExtensions() const {
601   RTC_DCHECK(signal_thread_checker_.IsCurrent());
602   std::vector<webrtc::RtpHeaderExtensionCapability> result;
603   int id = 1;
604   for (const auto& uri :
605        {webrtc::RtpExtension::kAudioLevelUri,
606         webrtc::RtpExtension::kAbsSendTimeUri,
607         webrtc::RtpExtension::kTransportSequenceNumberUri,
608         webrtc::RtpExtension::kMidUri, webrtc::RtpExtension::kRidUri,
609         webrtc::RtpExtension::kRepairedRidUri}) {
610     result.emplace_back(uri, id++, webrtc::RtpTransceiverDirection::kSendRecv);
611   }
612   return result;
613 }
614 
RegisterChannel(WebRtcVoiceMediaChannel * channel)615 void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
616   RTC_DCHECK(worker_thread_checker_.IsCurrent());
617   RTC_DCHECK(channel);
618   channels_.push_back(channel);
619 }
620 
UnregisterChannel(WebRtcVoiceMediaChannel * channel)621 void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
622   RTC_DCHECK(worker_thread_checker_.IsCurrent());
623   auto it = absl::c_find(channels_, channel);
624   RTC_DCHECK(it != channels_.end());
625   channels_.erase(it);
626 }
627 
StartAecDump(webrtc::FileWrapper file,int64_t max_size_bytes)628 bool WebRtcVoiceEngine::StartAecDump(webrtc::FileWrapper file,
629                                      int64_t max_size_bytes) {
630   RTC_DCHECK(worker_thread_checker_.IsCurrent());
631 
632   webrtc::AudioProcessing* ap = apm();
633   if (!ap) {
634     RTC_LOG(LS_WARNING)
635         << "Attempting to start aecdump when no audio processing module is "
636            "present, hence no aecdump is started.";
637     return false;
638   }
639 
640   return ap->CreateAndAttachAecDump(file.Release(), max_size_bytes,
641                                     low_priority_worker_queue_.get());
642 }
643 
StopAecDump()644 void WebRtcVoiceEngine::StopAecDump() {
645   RTC_DCHECK(worker_thread_checker_.IsCurrent());
646   webrtc::AudioProcessing* ap = apm();
647   if (ap) {
648     ap->DetachAecDump();
649   } else {
650     RTC_LOG(LS_WARNING) << "Attempting to stop aecdump when no audio "
651                            "processing module is present";
652   }
653 }
654 
adm()655 webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
656   RTC_DCHECK(worker_thread_checker_.IsCurrent());
657   RTC_DCHECK(adm_);
658   return adm_.get();
659 }
660 
apm() const661 webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
662   RTC_DCHECK(worker_thread_checker_.IsCurrent());
663   return apm_.get();
664 }
665 
audio_state()666 webrtc::AudioState* WebRtcVoiceEngine::audio_state() {
667   RTC_DCHECK(worker_thread_checker_.IsCurrent());
668   RTC_DCHECK(audio_state_);
669   return audio_state_.get();
670 }
671 
CollectCodecs(const std::vector<webrtc::AudioCodecSpec> & specs) const672 std::vector<AudioCodec> WebRtcVoiceEngine::CollectCodecs(
673     const std::vector<webrtc::AudioCodecSpec>& specs) const {
674   PayloadTypeMapper mapper;
675   std::vector<AudioCodec> out;
676 
677   // Only generate CN payload types for these clockrates:
678   std::map<int, bool, std::greater<int>> generate_cn = {
679       {8000, false}, {16000, false}, {32000, false}};
680   // Only generate telephone-event payload types for these clockrates:
681   std::map<int, bool, std::greater<int>> generate_dtmf = {
682       {8000, false}, {16000, false}, {32000, false}, {48000, false}};
683 
684   auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
685                               std::vector<AudioCodec>* out) {
686     absl::optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
687     if (opt_codec) {
688       if (out) {
689         out->push_back(*opt_codec);
690       }
691     } else {
692       RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
693                         << rtc::ToString(format);
694     }
695 
696     return opt_codec;
697   };
698 
699   for (const auto& spec : specs) {
700     // We need to do some extra stuff before adding the main codecs to out.
701     absl::optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
702     if (opt_codec) {
703       AudioCodec& codec = *opt_codec;
704       if (spec.info.supports_network_adaption) {
705         codec.AddFeedbackParam(
706             FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
707       }
708 
709       if (spec.info.allow_comfort_noise) {
710         // Generate a CN entry if the decoder allows it and we support the
711         // clockrate.
712         auto cn = generate_cn.find(spec.format.clockrate_hz);
713         if (cn != generate_cn.end()) {
714           cn->second = true;
715         }
716       }
717 
718       // Generate a telephone-event entry if we support the clockrate.
719       auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
720       if (dtmf != generate_dtmf.end()) {
721         dtmf->second = true;
722       }
723 
724       out.push_back(codec);
725     }
726   }
727 
728   // Add CN codecs after "proper" audio codecs.
729   for (const auto& cn : generate_cn) {
730     if (cn.second) {
731       map_format({kCnCodecName, cn.first, 1}, &out);
732     }
733   }
734 
735   // Add red codec.
736   if (IsAudioRedForOpusFieldTrialEnabled()) {
737     map_format({kRedCodecName, 48000, 2}, &out);
738   }
739 
740   // Add telephone-event codecs last.
741   for (const auto& dtmf : generate_dtmf) {
742     if (dtmf.second) {
743       map_format({kDtmfCodecName, dtmf.first, 1}, &out);
744     }
745   }
746 
747   return out;
748 }
749 
750 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
751     : public AudioSource::Sink {
752  public:
WebRtcAudioSendStream(uint32_t ssrc,const std::string & mid,const std::string & c_name,const std::string track_id,const absl::optional<webrtc::AudioSendStream::Config::SendCodecSpec> & send_codec_spec,bool extmap_allow_mixed,const std::vector<webrtc::RtpExtension> & extensions,int max_send_bitrate_bps,int rtcp_report_interval_ms,const absl::optional<std::string> & audio_network_adaptor_config,webrtc::Call * call,webrtc::Transport * send_transport,const rtc::scoped_refptr<webrtc::AudioEncoderFactory> & encoder_factory,const absl::optional<webrtc::AudioCodecPairId> codec_pair_id,rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor,const webrtc::CryptoOptions & crypto_options)753   WebRtcAudioSendStream(
754       uint32_t ssrc,
755       const std::string& mid,
756       const std::string& c_name,
757       const std::string track_id,
758       const absl::optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
759           send_codec_spec,
760       bool extmap_allow_mixed,
761       const std::vector<webrtc::RtpExtension>& extensions,
762       int max_send_bitrate_bps,
763       int rtcp_report_interval_ms,
764       const absl::optional<std::string>& audio_network_adaptor_config,
765       webrtc::Call* call,
766       webrtc::Transport* send_transport,
767       const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
768       const absl::optional<webrtc::AudioCodecPairId> codec_pair_id,
769       rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor,
770       const webrtc::CryptoOptions& crypto_options)
771       : call_(call),
772         config_(send_transport),
773         max_send_bitrate_bps_(max_send_bitrate_bps),
774         rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
775     RTC_DCHECK(call);
776     RTC_DCHECK(encoder_factory);
777     config_.rtp.ssrc = ssrc;
778     config_.rtp.mid = mid;
779     config_.rtp.c_name = c_name;
780     config_.rtp.extmap_allow_mixed = extmap_allow_mixed;
781     config_.rtp.extensions = extensions;
782     config_.has_dscp =
783         rtp_parameters_.encodings[0].network_priority != webrtc::Priority::kLow;
784     config_.encoder_factory = encoder_factory;
785     config_.codec_pair_id = codec_pair_id;
786     config_.track_id = track_id;
787     config_.frame_encryptor = frame_encryptor;
788     config_.crypto_options = crypto_options;
789     config_.rtcp_report_interval_ms = rtcp_report_interval_ms;
790     rtp_parameters_.encodings[0].ssrc = ssrc;
791     rtp_parameters_.rtcp.cname = c_name;
792     rtp_parameters_.header_extensions = extensions;
793 
794     audio_network_adaptor_config_from_options_ = audio_network_adaptor_config;
795     UpdateAudioNetworkAdaptorConfig();
796 
797     if (send_codec_spec) {
798       UpdateSendCodecSpec(*send_codec_spec);
799     }
800 
801     stream_ = call_->CreateAudioSendStream(config_);
802   }
803 
~WebRtcAudioSendStream()804   ~WebRtcAudioSendStream() override {
805     RTC_DCHECK(worker_thread_checker_.IsCurrent());
806     ClearSource();
807     call_->DestroyAudioSendStream(stream_);
808   }
809 
SetSendCodecSpec(const webrtc::AudioSendStream::Config::SendCodecSpec & send_codec_spec)810   void SetSendCodecSpec(
811       const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
812     UpdateSendCodecSpec(send_codec_spec);
813     ReconfigureAudioSendStream();
814   }
815 
SetRtpExtensions(const std::vector<webrtc::RtpExtension> & extensions)816   void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
817     RTC_DCHECK(worker_thread_checker_.IsCurrent());
818     config_.rtp.extensions = extensions;
819     rtp_parameters_.header_extensions = extensions;
820     ReconfigureAudioSendStream();
821   }
822 
SetExtmapAllowMixed(bool extmap_allow_mixed)823   void SetExtmapAllowMixed(bool extmap_allow_mixed) {
824     config_.rtp.extmap_allow_mixed = extmap_allow_mixed;
825     ReconfigureAudioSendStream();
826   }
827 
SetMid(const std::string & mid)828   void SetMid(const std::string& mid) {
829     RTC_DCHECK(worker_thread_checker_.IsCurrent());
830     if (config_.rtp.mid == mid) {
831       return;
832     }
833     config_.rtp.mid = mid;
834     ReconfigureAudioSendStream();
835   }
836 
SetFrameEncryptor(rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor)837   void SetFrameEncryptor(
838       rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor) {
839     RTC_DCHECK(worker_thread_checker_.IsCurrent());
840     config_.frame_encryptor = frame_encryptor;
841     ReconfigureAudioSendStream();
842   }
843 
SetAudioNetworkAdaptorConfig(const absl::optional<std::string> & audio_network_adaptor_config)844   void SetAudioNetworkAdaptorConfig(
845       const absl::optional<std::string>& audio_network_adaptor_config) {
846     RTC_DCHECK(worker_thread_checker_.IsCurrent());
847     if (audio_network_adaptor_config_from_options_ ==
848         audio_network_adaptor_config) {
849       return;
850     }
851     audio_network_adaptor_config_from_options_ = audio_network_adaptor_config;
852     UpdateAudioNetworkAdaptorConfig();
853     UpdateAllowedBitrateRange();
854     ReconfigureAudioSendStream();
855   }
856 
SetMaxSendBitrate(int bps)857   bool SetMaxSendBitrate(int bps) {
858     RTC_DCHECK(worker_thread_checker_.IsCurrent());
859     RTC_DCHECK(config_.send_codec_spec);
860     RTC_DCHECK(audio_codec_spec_);
861     auto send_rate = ComputeSendBitrate(
862         bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
863 
864     if (!send_rate) {
865       return false;
866     }
867 
868     max_send_bitrate_bps_ = bps;
869 
870     if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
871       config_.send_codec_spec->target_bitrate_bps = send_rate;
872       ReconfigureAudioSendStream();
873     }
874     return true;
875   }
876 
SendTelephoneEvent(int payload_type,int payload_freq,int event,int duration_ms)877   bool SendTelephoneEvent(int payload_type,
878                           int payload_freq,
879                           int event,
880                           int duration_ms) {
881     RTC_DCHECK(worker_thread_checker_.IsCurrent());
882     RTC_DCHECK(stream_);
883     return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
884                                        duration_ms);
885   }
886 
SetSend(bool send)887   void SetSend(bool send) {
888     RTC_DCHECK(worker_thread_checker_.IsCurrent());
889     send_ = send;
890     UpdateSendState();
891   }
892 
SetMuted(bool muted)893   void SetMuted(bool muted) {
894     RTC_DCHECK(worker_thread_checker_.IsCurrent());
895     RTC_DCHECK(stream_);
896     stream_->SetMuted(muted);
897     muted_ = muted;
898   }
899 
muted() const900   bool muted() const {
901     RTC_DCHECK(worker_thread_checker_.IsCurrent());
902     return muted_;
903   }
904 
GetStats(bool has_remote_tracks) const905   webrtc::AudioSendStream::Stats GetStats(bool has_remote_tracks) const {
906     RTC_DCHECK(worker_thread_checker_.IsCurrent());
907     RTC_DCHECK(stream_);
908     return stream_->GetStats(has_remote_tracks);
909   }
910 
911   // Starts the sending by setting ourselves as a sink to the AudioSource to
912   // get data callbacks.
913   // This method is called on the libjingle worker thread.
914   // TODO(xians): Make sure Start() is called only once.
SetSource(AudioSource * source)915   void SetSource(AudioSource* source) {
916     RTC_DCHECK(worker_thread_checker_.IsCurrent());
917     RTC_DCHECK(source);
918     if (source_) {
919       RTC_DCHECK(source_ == source);
920       return;
921     }
922     source->SetSink(this);
923     source_ = source;
924     UpdateSendState();
925   }
926 
927   // Stops sending by setting the sink of the AudioSource to nullptr. No data
928   // callback will be received after this method.
929   // This method is called on the libjingle worker thread.
ClearSource()930   void ClearSource() {
931     RTC_DCHECK(worker_thread_checker_.IsCurrent());
932     if (source_) {
933       source_->SetSink(nullptr);
934       source_ = nullptr;
935     }
936     UpdateSendState();
937   }
938 
939   // AudioSource::Sink implementation.
940   // This method is called on the audio thread.
OnData(const void * audio_data,int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames,absl::optional<int64_t> absolute_capture_timestamp_ms)941   void OnData(const void* audio_data,
942               int bits_per_sample,
943               int sample_rate,
944               size_t number_of_channels,
945               size_t number_of_frames,
946               absl::optional<int64_t> absolute_capture_timestamp_ms) override {
947     RTC_DCHECK_EQ(16, bits_per_sample);
948     RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
949     RTC_DCHECK(stream_);
950     std::unique_ptr<webrtc::AudioFrame> audio_frame(new webrtc::AudioFrame());
951     audio_frame->UpdateFrame(
952         audio_frame->timestamp_, static_cast<const int16_t*>(audio_data),
953         number_of_frames, sample_rate, audio_frame->speech_type_,
954         audio_frame->vad_activity_, number_of_channels);
955     // TODO(bugs.webrtc.org/10739): add dcheck that
956     // |absolute_capture_timestamp_ms| always receives a value.
957     if (absolute_capture_timestamp_ms) {
958       audio_frame->set_absolute_capture_timestamp_ms(
959           *absolute_capture_timestamp_ms);
960     }
961     stream_->SendAudioData(std::move(audio_frame));
962   }
963 
964   // Callback from the |source_| when it is going away. In case Start() has
965   // never been called, this callback won't be triggered.
OnClose()966   void OnClose() override {
967     RTC_DCHECK(worker_thread_checker_.IsCurrent());
968     // Set |source_| to nullptr to make sure no more callback will get into
969     // the source.
970     source_ = nullptr;
971     UpdateSendState();
972   }
973 
rtp_parameters() const974   const webrtc::RtpParameters& rtp_parameters() const {
975     return rtp_parameters_;
976   }
977 
SetRtpParameters(const webrtc::RtpParameters & parameters)978   webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters) {
979     webrtc::RTCError error = CheckRtpParametersInvalidModificationAndValues(
980         rtp_parameters_, parameters);
981     if (!error.ok()) {
982       return error;
983     }
984 
985     absl::optional<int> send_rate;
986     if (audio_codec_spec_) {
987       send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
988                                      parameters.encodings[0].max_bitrate_bps,
989                                      *audio_codec_spec_);
990       if (!send_rate) {
991         return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR);
992       }
993     }
994 
995     const absl::optional<int> old_rtp_max_bitrate =
996         rtp_parameters_.encodings[0].max_bitrate_bps;
997     double old_priority = rtp_parameters_.encodings[0].bitrate_priority;
998     webrtc::Priority old_dscp = rtp_parameters_.encodings[0].network_priority;
999     bool old_adaptive_ptime = rtp_parameters_.encodings[0].adaptive_ptime;
1000     rtp_parameters_ = parameters;
1001     config_.bitrate_priority = rtp_parameters_.encodings[0].bitrate_priority;
1002     config_.has_dscp = (rtp_parameters_.encodings[0].network_priority !=
1003                         webrtc::Priority::kLow);
1004 
1005     bool reconfigure_send_stream =
1006         (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) ||
1007         (rtp_parameters_.encodings[0].bitrate_priority != old_priority) ||
1008         (rtp_parameters_.encodings[0].network_priority != old_dscp) ||
1009         (rtp_parameters_.encodings[0].adaptive_ptime != old_adaptive_ptime);
1010     if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
1011       // Update the bitrate range.
1012       if (send_rate) {
1013         config_.send_codec_spec->target_bitrate_bps = send_rate;
1014       }
1015     }
1016     if (reconfigure_send_stream) {
1017       // Changing adaptive_ptime may update the audio network adaptor config
1018       // used.
1019       UpdateAudioNetworkAdaptorConfig();
1020       UpdateAllowedBitrateRange();
1021       ReconfigureAudioSendStream();
1022     }
1023 
1024     rtp_parameters_.rtcp.cname = config_.rtp.c_name;
1025     rtp_parameters_.rtcp.reduced_size = false;
1026 
1027     // parameters.encodings[0].active could have changed.
1028     UpdateSendState();
1029     return webrtc::RTCError::OK();
1030   }
1031 
SetEncoderToPacketizerFrameTransformer(rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)1032   void SetEncoderToPacketizerFrameTransformer(
1033       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
1034     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1035     config_.frame_transformer = std::move(frame_transformer);
1036     ReconfigureAudioSendStream();
1037   }
1038 
1039  private:
UpdateSendState()1040   void UpdateSendState() {
1041     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1042     RTC_DCHECK(stream_);
1043     RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1044     if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
1045       stream_->Start();
1046     } else {  // !send || source_ = nullptr
1047       stream_->Stop();
1048     }
1049   }
1050 
UpdateAllowedBitrateRange()1051   void UpdateAllowedBitrateRange() {
1052     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1053     // The order of precedence, from lowest to highest is:
1054     // - a reasonable default of 32kbps min/max
1055     // - fixed target bitrate from codec spec
1056     // - lower min bitrate if adaptive ptime is enabled
1057     // - bitrate configured in the rtp_parameter encodings settings
1058     const int kDefaultBitrateBps = 32000;
1059     config_.min_bitrate_bps = kDefaultBitrateBps;
1060     config_.max_bitrate_bps = kDefaultBitrateBps;
1061 
1062     if (config_.send_codec_spec &&
1063         config_.send_codec_spec->target_bitrate_bps) {
1064       config_.min_bitrate_bps = *config_.send_codec_spec->target_bitrate_bps;
1065       config_.max_bitrate_bps = *config_.send_codec_spec->target_bitrate_bps;
1066     }
1067 
1068     if (rtp_parameters_.encodings[0].adaptive_ptime) {
1069       config_.min_bitrate_bps = std::min(
1070           config_.min_bitrate_bps,
1071           static_cast<int>(adaptive_ptime_config_.min_encoder_bitrate.bps()));
1072     }
1073 
1074     if (rtp_parameters_.encodings[0].min_bitrate_bps) {
1075       config_.min_bitrate_bps = *rtp_parameters_.encodings[0].min_bitrate_bps;
1076     }
1077     if (rtp_parameters_.encodings[0].max_bitrate_bps) {
1078       config_.max_bitrate_bps = *rtp_parameters_.encodings[0].max_bitrate_bps;
1079     }
1080   }
1081 
UpdateSendCodecSpec(const webrtc::AudioSendStream::Config::SendCodecSpec & send_codec_spec)1082   void UpdateSendCodecSpec(
1083       const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1084     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1085     config_.send_codec_spec = send_codec_spec;
1086     auto info =
1087         config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1088     RTC_DCHECK(info);
1089     // If a specific target bitrate has been set for the stream, use that as
1090     // the new default bitrate when computing send bitrate.
1091     if (send_codec_spec.target_bitrate_bps) {
1092       info->default_bitrate_bps = std::max(
1093           info->min_bitrate_bps,
1094           std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1095     }
1096 
1097     audio_codec_spec_.emplace(
1098         webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1099 
1100     config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1101         max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1102         *audio_codec_spec_);
1103 
1104     UpdateAllowedBitrateRange();
1105   }
1106 
UpdateAudioNetworkAdaptorConfig()1107   void UpdateAudioNetworkAdaptorConfig() {
1108     if (adaptive_ptime_config_.enabled ||
1109         rtp_parameters_.encodings[0].adaptive_ptime) {
1110       config_.audio_network_adaptor_config =
1111           adaptive_ptime_config_.audio_network_adaptor_config;
1112       return;
1113     }
1114     config_.audio_network_adaptor_config =
1115         audio_network_adaptor_config_from_options_;
1116   }
1117 
ReconfigureAudioSendStream()1118   void ReconfigureAudioSendStream() {
1119     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1120     RTC_DCHECK(stream_);
1121     stream_->Reconfigure(config_);
1122   }
1123 
1124   const AdaptivePtimeConfig adaptive_ptime_config_;
1125   rtc::ThreadChecker worker_thread_checker_;
1126   rtc::RaceChecker audio_capture_race_checker_;
1127   webrtc::Call* call_ = nullptr;
1128   webrtc::AudioSendStream::Config config_;
1129   // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1130   // configuration changes.
1131   webrtc::AudioSendStream* stream_ = nullptr;
1132 
1133   // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
1134   // PeerConnection will make sure invalidating the pointer before the object
1135   // goes away.
1136   AudioSource* source_ = nullptr;
1137   bool send_ = false;
1138   bool muted_ = false;
1139   int max_send_bitrate_bps_;
1140   webrtc::RtpParameters rtp_parameters_;
1141   absl::optional<webrtc::AudioCodecSpec> audio_codec_spec_;
1142   // TODO(webrtc:11717): Remove this once audio_network_adaptor in AudioOptions
1143   // has been removed.
1144   absl::optional<std::string> audio_network_adaptor_config_from_options_;
1145 
1146   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1147 };
1148 
1149 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1150  public:
WebRtcAudioReceiveStream(uint32_t remote_ssrc,uint32_t local_ssrc,bool use_transport_cc,bool use_nack,const std::vector<std::string> & stream_ids,const std::vector<webrtc::RtpExtension> & extensions,webrtc::Call * call,webrtc::Transport * rtcp_send_transport,const rtc::scoped_refptr<webrtc::AudioDecoderFactory> & decoder_factory,const std::map<int,webrtc::SdpAudioFormat> & decoder_map,absl::optional<webrtc::AudioCodecPairId> codec_pair_id,size_t jitter_buffer_max_packets,bool jitter_buffer_fast_accelerate,int jitter_buffer_min_delay_ms,bool jitter_buffer_enable_rtx_handling,rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,const webrtc::CryptoOptions & crypto_options,rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)1151   WebRtcAudioReceiveStream(
1152       uint32_t remote_ssrc,
1153       uint32_t local_ssrc,
1154       bool use_transport_cc,
1155       bool use_nack,
1156       const std::vector<std::string>& stream_ids,
1157       const std::vector<webrtc::RtpExtension>& extensions,
1158       webrtc::Call* call,
1159       webrtc::Transport* rtcp_send_transport,
1160       const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1161       const std::map<int, webrtc::SdpAudioFormat>& decoder_map,
1162       absl::optional<webrtc::AudioCodecPairId> codec_pair_id,
1163       size_t jitter_buffer_max_packets,
1164       bool jitter_buffer_fast_accelerate,
1165       int jitter_buffer_min_delay_ms,
1166       bool jitter_buffer_enable_rtx_handling,
1167       rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
1168       const webrtc::CryptoOptions& crypto_options,
1169       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
1170       : call_(call), config_() {
1171     RTC_DCHECK(call);
1172     config_.rtp.remote_ssrc = remote_ssrc;
1173     config_.rtp.local_ssrc = local_ssrc;
1174     config_.rtp.transport_cc = use_transport_cc;
1175     config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1176     config_.rtp.extensions = extensions;
1177     config_.rtcp_send_transport = rtcp_send_transport;
1178     config_.jitter_buffer_max_packets = jitter_buffer_max_packets;
1179     config_.jitter_buffer_fast_accelerate = jitter_buffer_fast_accelerate;
1180     config_.jitter_buffer_min_delay_ms = jitter_buffer_min_delay_ms;
1181     config_.jitter_buffer_enable_rtx_handling =
1182         jitter_buffer_enable_rtx_handling;
1183     if (!stream_ids.empty()) {
1184       config_.sync_group = stream_ids[0];
1185     }
1186     config_.decoder_factory = decoder_factory;
1187     config_.decoder_map = decoder_map;
1188     config_.codec_pair_id = codec_pair_id;
1189     config_.frame_decryptor = frame_decryptor;
1190     config_.crypto_options = crypto_options;
1191     config_.frame_transformer = std::move(frame_transformer);
1192     RecreateAudioReceiveStream();
1193   }
1194 
~WebRtcAudioReceiveStream()1195   ~WebRtcAudioReceiveStream() {
1196     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1197     call_->DestroyAudioReceiveStream(stream_);
1198   }
1199 
SetFrameDecryptor(rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor)1200   void SetFrameDecryptor(
1201       rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) {
1202     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1203     config_.frame_decryptor = frame_decryptor;
1204     RecreateAudioReceiveStream();
1205   }
1206 
SetLocalSsrc(uint32_t local_ssrc)1207   void SetLocalSsrc(uint32_t local_ssrc) {
1208     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1209     if (local_ssrc != config_.rtp.local_ssrc) {
1210       config_.rtp.local_ssrc = local_ssrc;
1211       RecreateAudioReceiveStream();
1212     }
1213   }
1214 
SetUseTransportCcAndRecreateStream(bool use_transport_cc,bool use_nack)1215   void SetUseTransportCcAndRecreateStream(bool use_transport_cc,
1216                                           bool use_nack) {
1217     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1218     config_.rtp.transport_cc = use_transport_cc;
1219     config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1220     ReconfigureAudioReceiveStream();
1221   }
1222 
SetRtpExtensionsAndRecreateStream(const std::vector<webrtc::RtpExtension> & extensions)1223   void SetRtpExtensionsAndRecreateStream(
1224       const std::vector<webrtc::RtpExtension>& extensions) {
1225     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1226     config_.rtp.extensions = extensions;
1227     RecreateAudioReceiveStream();
1228   }
1229 
1230   // Set a new payload type -> decoder map.
SetDecoderMap(const std::map<int,webrtc::SdpAudioFormat> & decoder_map)1231   void SetDecoderMap(const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1232     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1233     config_.decoder_map = decoder_map;
1234     ReconfigureAudioReceiveStream();
1235   }
1236 
MaybeRecreateAudioReceiveStream(const std::vector<std::string> & stream_ids)1237   void MaybeRecreateAudioReceiveStream(
1238       const std::vector<std::string>& stream_ids) {
1239     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1240     std::string sync_group;
1241     if (!stream_ids.empty()) {
1242       sync_group = stream_ids[0];
1243     }
1244     if (config_.sync_group != sync_group) {
1245       RTC_LOG(LS_INFO) << "Recreating AudioReceiveStream for SSRC="
1246                        << config_.rtp.remote_ssrc
1247                        << " because of sync group change.";
1248       config_.sync_group = sync_group;
1249       RecreateAudioReceiveStream();
1250     }
1251   }
1252 
GetStats() const1253   webrtc::AudioReceiveStream::Stats GetStats() const {
1254     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1255     RTC_DCHECK(stream_);
1256     return stream_->GetStats();
1257   }
1258 
SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink)1259   void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1260     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1261     // Need to update the stream's sink first; once raw_audio_sink_ is
1262     // reassigned, whatever was in there before is destroyed.
1263     stream_->SetSink(sink.get());
1264     raw_audio_sink_ = std::move(sink);
1265   }
1266 
SetOutputVolume(double volume)1267   void SetOutputVolume(double volume) {
1268     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1269     output_volume_ = volume;
1270     stream_->SetGain(volume);
1271   }
1272 
SetPlayout(bool playout)1273   void SetPlayout(bool playout) {
1274     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1275     RTC_DCHECK(stream_);
1276     if (playout) {
1277       stream_->Start();
1278     } else {
1279       stream_->Stop();
1280     }
1281     playout_ = playout;
1282   }
1283 
SetBaseMinimumPlayoutDelayMs(int delay_ms)1284   bool SetBaseMinimumPlayoutDelayMs(int delay_ms) {
1285     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1286     RTC_DCHECK(stream_);
1287     if (stream_->SetBaseMinimumPlayoutDelayMs(delay_ms)) {
1288       // Memorize only valid delay because during stream recreation it will be
1289       // passed to the constructor and it must be valid value.
1290       config_.jitter_buffer_min_delay_ms = delay_ms;
1291       return true;
1292     } else {
1293       RTC_LOG(LS_ERROR) << "Failed to SetBaseMinimumPlayoutDelayMs"
1294                            " on AudioReceiveStream on SSRC="
1295                         << config_.rtp.remote_ssrc
1296                         << " with delay_ms=" << delay_ms;
1297       return false;
1298     }
1299   }
1300 
GetBaseMinimumPlayoutDelayMs() const1301   int GetBaseMinimumPlayoutDelayMs() const {
1302     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1303     RTC_DCHECK(stream_);
1304     return stream_->GetBaseMinimumPlayoutDelayMs();
1305   }
1306 
GetSources()1307   std::vector<webrtc::RtpSource> GetSources() {
1308     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1309     RTC_DCHECK(stream_);
1310     return stream_->GetSources();
1311   }
1312 
GetRtpParameters() const1313   webrtc::RtpParameters GetRtpParameters() const {
1314     webrtc::RtpParameters rtp_parameters;
1315     rtp_parameters.encodings.emplace_back();
1316     rtp_parameters.encodings[0].ssrc = config_.rtp.remote_ssrc;
1317     rtp_parameters.header_extensions = config_.rtp.extensions;
1318 
1319     return rtp_parameters;
1320   }
1321 
SetDepacketizerToDecoderFrameTransformer(rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)1322   void SetDepacketizerToDecoderFrameTransformer(
1323       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
1324     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1325     config_.frame_transformer = std::move(frame_transformer);
1326     ReconfigureAudioReceiveStream();
1327   }
1328 
1329  private:
RecreateAudioReceiveStream()1330   void RecreateAudioReceiveStream() {
1331     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1332     if (stream_) {
1333       call_->DestroyAudioReceiveStream(stream_);
1334     }
1335     stream_ = call_->CreateAudioReceiveStream(config_);
1336     RTC_CHECK(stream_);
1337     stream_->SetGain(output_volume_);
1338     SetPlayout(playout_);
1339     stream_->SetSink(raw_audio_sink_.get());
1340   }
1341 
ReconfigureAudioReceiveStream()1342   void ReconfigureAudioReceiveStream() {
1343     RTC_DCHECK(worker_thread_checker_.IsCurrent());
1344     RTC_DCHECK(stream_);
1345     stream_->Reconfigure(config_);
1346   }
1347 
1348   rtc::ThreadChecker worker_thread_checker_;
1349   webrtc::Call* call_ = nullptr;
1350   webrtc::AudioReceiveStream::Config config_;
1351   // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1352   // configuration changes.
1353   webrtc::AudioReceiveStream* stream_ = nullptr;
1354   bool playout_ = false;
1355   float output_volume_ = 1.0;
1356   std::unique_ptr<webrtc::AudioSinkInterface> raw_audio_sink_;
1357 
1358   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
1359 };
1360 
WebRtcVoiceMediaChannel(WebRtcVoiceEngine * engine,const MediaConfig & config,const AudioOptions & options,const webrtc::CryptoOptions & crypto_options,webrtc::Call * call)1361 WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(
1362     WebRtcVoiceEngine* engine,
1363     const MediaConfig& config,
1364     const AudioOptions& options,
1365     const webrtc::CryptoOptions& crypto_options,
1366     webrtc::Call* call)
1367     : VoiceMediaChannel(config),
1368       engine_(engine),
1369       call_(call),
1370       audio_config_(config.audio),
1371       crypto_options_(crypto_options) {
1372   RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
1373   RTC_DCHECK(call);
1374   engine->RegisterChannel(this);
1375   SetOptions(options);
1376 }
1377 
~WebRtcVoiceMediaChannel()1378 WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
1379   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1380   RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
1381   // TODO(solenberg): Should be able to delete the streams directly, without
1382   //                  going through RemoveNnStream(), once stream objects handle
1383   //                  all (de)configuration.
1384   while (!send_streams_.empty()) {
1385     RemoveSendStream(send_streams_.begin()->first);
1386   }
1387   while (!recv_streams_.empty()) {
1388     RemoveRecvStream(recv_streams_.begin()->first);
1389   }
1390   engine()->UnregisterChannel(this);
1391 }
1392 
SetSendParameters(const AudioSendParameters & params)1393 bool WebRtcVoiceMediaChannel::SetSendParameters(
1394     const AudioSendParameters& params) {
1395   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
1396   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1397   RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1398                    << params.ToString();
1399   // TODO(pthatcher): Refactor this to be more clean now that we have
1400   // all the information at once.
1401 
1402   if (!SetSendCodecs(params.codecs)) {
1403     return false;
1404   }
1405 
1406   if (!ValidateRtpExtensions(params.extensions)) {
1407     return false;
1408   }
1409 
1410   if (ExtmapAllowMixed() != params.extmap_allow_mixed) {
1411     SetExtmapAllowMixed(params.extmap_allow_mixed);
1412     for (auto& it : send_streams_) {
1413       it.second->SetExtmapAllowMixed(params.extmap_allow_mixed);
1414     }
1415   }
1416 
1417   std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions(
1418       params.extensions, webrtc::RtpExtension::IsSupportedForAudio, true);
1419   if (send_rtp_extensions_ != filtered_extensions) {
1420     send_rtp_extensions_.swap(filtered_extensions);
1421     for (auto& it : send_streams_) {
1422       it.second->SetRtpExtensions(send_rtp_extensions_);
1423     }
1424   }
1425   if (!params.mid.empty()) {
1426     mid_ = params.mid;
1427     for (auto& it : send_streams_) {
1428       it.second->SetMid(params.mid);
1429     }
1430   }
1431 
1432   if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
1433     return false;
1434   }
1435   return SetOptions(params.options);
1436 }
1437 
SetRecvParameters(const AudioRecvParameters & params)1438 bool WebRtcVoiceMediaChannel::SetRecvParameters(
1439     const AudioRecvParameters& params) {
1440   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
1441   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1442   RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1443                    << params.ToString();
1444   // TODO(pthatcher): Refactor this to be more clean now that we have
1445   // all the information at once.
1446 
1447   if (!SetRecvCodecs(params.codecs)) {
1448     return false;
1449   }
1450 
1451   if (!ValidateRtpExtensions(params.extensions)) {
1452     return false;
1453   }
1454   std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions(
1455       params.extensions, webrtc::RtpExtension::IsSupportedForAudio, false);
1456   if (recv_rtp_extensions_ != filtered_extensions) {
1457     recv_rtp_extensions_.swap(filtered_extensions);
1458     for (auto& it : recv_streams_) {
1459       it.second->SetRtpExtensionsAndRecreateStream(recv_rtp_extensions_);
1460     }
1461   }
1462   return true;
1463 }
1464 
GetRtpSendParameters(uint32_t ssrc) const1465 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
1466     uint32_t ssrc) const {
1467   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1468   auto it = send_streams_.find(ssrc);
1469   if (it == send_streams_.end()) {
1470     RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1471                            "with ssrc "
1472                         << ssrc << " which doesn't exist.";
1473     return webrtc::RtpParameters();
1474   }
1475 
1476   webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1477   // Need to add the common list of codecs to the send stream-specific
1478   // RTP parameters.
1479   for (const AudioCodec& codec : send_codecs_) {
1480     rtp_params.codecs.push_back(codec.ToCodecParameters());
1481   }
1482   return rtp_params;
1483 }
1484 
SetRtpSendParameters(uint32_t ssrc,const webrtc::RtpParameters & parameters)1485 webrtc::RTCError WebRtcVoiceMediaChannel::SetRtpSendParameters(
1486     uint32_t ssrc,
1487     const webrtc::RtpParameters& parameters) {
1488   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1489   auto it = send_streams_.find(ssrc);
1490   if (it == send_streams_.end()) {
1491     RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1492                            "with ssrc "
1493                         << ssrc << " which doesn't exist.";
1494     return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR);
1495   }
1496 
1497   // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1498   // different order (which should change the send codec).
1499   webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1500   if (current_parameters.codecs != parameters.codecs) {
1501     RTC_DLOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1502                           "is not currently supported.";
1503     return webrtc::RTCError(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER);
1504   }
1505 
1506   if (!parameters.encodings.empty()) {
1507     // Note that these values come from:
1508     // https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-16#section-5
1509     rtc::DiffServCodePoint new_dscp = rtc::DSCP_DEFAULT;
1510     switch (parameters.encodings[0].network_priority) {
1511       case webrtc::Priority::kVeryLow:
1512         new_dscp = rtc::DSCP_CS1;
1513         break;
1514       case webrtc::Priority::kLow:
1515         new_dscp = rtc::DSCP_DEFAULT;
1516         break;
1517       case webrtc::Priority::kMedium:
1518         new_dscp = rtc::DSCP_EF;
1519         break;
1520       case webrtc::Priority::kHigh:
1521         new_dscp = rtc::DSCP_EF;
1522         break;
1523     }
1524     SetPreferredDscp(new_dscp);
1525   }
1526 
1527   // TODO(minyue): The following legacy actions go into
1528   // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1529   // though there are two difference:
1530   // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1531   // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1532   // |SetSendCodecs|. The outcome should be the same.
1533   // 2. AudioSendStream can be recreated.
1534 
1535   // Codecs are handled at the WebRtcVoiceMediaChannel level.
1536   webrtc::RtpParameters reduced_params = parameters;
1537   reduced_params.codecs.clear();
1538   return it->second->SetRtpParameters(reduced_params);
1539 }
1540 
GetRtpReceiveParameters(uint32_t ssrc) const1541 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1542     uint32_t ssrc) const {
1543   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1544   webrtc::RtpParameters rtp_params;
1545   auto it = recv_streams_.find(ssrc);
1546   if (it == recv_streams_.end()) {
1547     RTC_LOG(LS_WARNING)
1548         << "Attempting to get RTP receive parameters for stream "
1549            "with ssrc "
1550         << ssrc << " which doesn't exist.";
1551     return webrtc::RtpParameters();
1552   }
1553   rtp_params = it->second->GetRtpParameters();
1554 
1555   for (const AudioCodec& codec : recv_codecs_) {
1556     rtp_params.codecs.push_back(codec.ToCodecParameters());
1557   }
1558   return rtp_params;
1559 }
1560 
GetDefaultRtpReceiveParameters() const1561 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetDefaultRtpReceiveParameters()
1562     const {
1563   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1564   webrtc::RtpParameters rtp_params;
1565   if (!default_sink_) {
1566     RTC_LOG(LS_WARNING) << "Attempting to get RTP parameters for the default, "
1567                            "unsignaled audio receive stream, but not yet "
1568                            "configured to receive such a stream.";
1569     return rtp_params;
1570   }
1571   rtp_params.encodings.emplace_back();
1572 
1573   for (const AudioCodec& codec : recv_codecs_) {
1574     rtp_params.codecs.push_back(codec.ToCodecParameters());
1575   }
1576   return rtp_params;
1577 }
1578 
SetOptions(const AudioOptions & options)1579 bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
1580   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1581   RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
1582 
1583   // We retain all of the existing options, and apply the given ones
1584   // on top.  This means there is no way to "clear" options such that
1585   // they go back to the engine default.
1586   options_.SetAll(options);
1587   if (!engine()->ApplyOptions(options_)) {
1588     RTC_LOG(LS_WARNING)
1589         << "Failed to apply engine options during channel SetOptions.";
1590     return false;
1591   }
1592 
1593   absl::optional<std::string> audio_network_adaptor_config =
1594       GetAudioNetworkAdaptorConfig(options_);
1595   for (auto& it : send_streams_) {
1596     it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
1597   }
1598 
1599   RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1600                    << options_.ToString();
1601   return true;
1602 }
1603 
SetRecvCodecs(const std::vector<AudioCodec> & codecs)1604 bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1605     const std::vector<AudioCodec>& codecs) {
1606   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1607 
1608   // Set the payload types to be used for incoming media.
1609   RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
1610 
1611   if (!VerifyUniquePayloadTypes(codecs)) {
1612     RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
1613     return false;
1614   }
1615 
1616   // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1617   // unless the factory claims to support all decoders.
1618   std::map<int, webrtc::SdpAudioFormat> decoder_map;
1619   for (const AudioCodec& codec : codecs) {
1620     // Log a warning if a codec's payload type is changing. This used to be
1621     // treated as an error. It's abnormal, but not really illegal.
1622     AudioCodec old_codec;
1623     if (FindCodec(recv_codecs_, codec, &old_codec) &&
1624         old_codec.id != codec.id) {
1625       RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1626                           << codec.id << ", was already mapped to "
1627                           << old_codec.id << ")";
1628     }
1629     auto format = AudioCodecToSdpAudioFormat(codec);
1630     if (!IsCodec(codec, kCnCodecName) && !IsCodec(codec, kDtmfCodecName) &&
1631         (!IsAudioRedForOpusFieldTrialEnabled() ||
1632          !IsCodec(codec, kRedCodecName)) &&
1633         !engine()->decoder_factory_->IsSupportedDecoder(format)) {
1634       RTC_LOG(LS_ERROR) << "Unsupported codec: " << rtc::ToString(format);
1635       return false;
1636     }
1637     // We allow adding new codecs but don't allow changing the payload type of
1638     // codecs that are already configured since we might already be receiving
1639     // packets with that payload type. See RFC3264, Section 8.3.2.
1640     // TODO(deadbeef): Also need to check for clashes with previously mapped
1641     // payload types, and not just currently mapped ones. For example, this
1642     // should be illegal:
1643     // 1. {100: opus/48000/2, 101: ISAC/16000}
1644     // 2. {100: opus/48000/2}
1645     // 3. {100: opus/48000/2, 101: ISAC/32000}
1646     // Though this check really should happen at a higher level, since this
1647     // conflict could happen between audio and video codecs.
1648     auto existing = decoder_map_.find(codec.id);
1649     if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
1650       RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1651                         << " for " << codec.name
1652                         << ", but it is already used for "
1653                         << existing->second.name;
1654       return false;
1655     }
1656     decoder_map.insert({codec.id, std::move(format)});
1657   }
1658 
1659   if (decoder_map == decoder_map_) {
1660     // There's nothing new to configure.
1661     return true;
1662   }
1663 
1664   if (playout_) {
1665     // Receive codecs can not be changed while playing. So we temporarily
1666     // pause playout.
1667     ChangePlayout(false);
1668   }
1669 
1670   decoder_map_ = std::move(decoder_map);
1671   for (auto& kv : recv_streams_) {
1672     kv.second->SetDecoderMap(decoder_map_);
1673   }
1674   recv_codecs_ = codecs;
1675 
1676   if (desired_playout_ && !playout_) {
1677     ChangePlayout(desired_playout_);
1678   }
1679   return true;
1680 }
1681 
1682 // Utility function called from SetSendParameters() to extract current send
1683 // codec settings from the given list of codecs (originally from SDP). Both send
1684 // and receive streams may be reconfigured based on the new settings.
SetSendCodecs(const std::vector<AudioCodec> & codecs)1685 bool WebRtcVoiceMediaChannel::SetSendCodecs(
1686     const std::vector<AudioCodec>& codecs) {
1687   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1688   dtmf_payload_type_ = absl::nullopt;
1689   dtmf_payload_freq_ = -1;
1690 
1691   // Validate supplied codecs list.
1692   for (const AudioCodec& codec : codecs) {
1693     // TODO(solenberg): Validate more aspects of input - that payload types
1694     //                  don't overlap, remove redundant/unsupported codecs etc -
1695     //                  the same way it is done for RtpHeaderExtensions.
1696     if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1697       RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1698                           << ToString(codec);
1699       return false;
1700     }
1701   }
1702 
1703   // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1704   // case we don't have a DTMF codec with a rate matching the send codec's, or
1705   // if this function returns early.
1706   std::vector<AudioCodec> dtmf_codecs;
1707   for (const AudioCodec& codec : codecs) {
1708     if (IsCodec(codec, kDtmfCodecName)) {
1709       dtmf_codecs.push_back(codec);
1710       if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
1711         dtmf_payload_type_ = codec.id;
1712         dtmf_payload_freq_ = codec.clockrate;
1713       }
1714     }
1715   }
1716 
1717   // Scan through the list to figure out the codec to use for sending.
1718   absl::optional<webrtc::AudioSendStream::Config::SendCodecSpec>
1719       send_codec_spec;
1720   webrtc::BitrateConstraints bitrate_config;
1721   absl::optional<webrtc::AudioCodecInfo> voice_codec_info;
1722   for (const AudioCodec& voice_codec : codecs) {
1723     if (!(IsCodec(voice_codec, kCnCodecName) ||
1724           IsCodec(voice_codec, kDtmfCodecName) ||
1725           IsCodec(voice_codec, kRedCodecName))) {
1726       webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1727                                     voice_codec.channels, voice_codec.params);
1728 
1729       voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1730       if (!voice_codec_info) {
1731         RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
1732         continue;
1733       }
1734 
1735       send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1736           voice_codec.id, format);
1737       if (voice_codec.bitrate > 0) {
1738         send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
1739       }
1740       send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1741       send_codec_spec->nack_enabled = HasNack(voice_codec);
1742       bitrate_config = GetBitrateConfigForCodec(voice_codec);
1743       break;
1744     }
1745   }
1746 
1747   if (!send_codec_spec) {
1748     return false;
1749   }
1750 
1751   RTC_DCHECK(voice_codec_info);
1752   if (voice_codec_info->allow_comfort_noise) {
1753     // Loop through the codecs list again to find the CN codec.
1754     // TODO(solenberg): Break out into a separate function?
1755     for (const AudioCodec& cn_codec : codecs) {
1756       if (IsCodec(cn_codec, kCnCodecName) &&
1757           cn_codec.clockrate == send_codec_spec->format.clockrate_hz &&
1758           cn_codec.channels == voice_codec_info->num_channels) {
1759         if (cn_codec.channels != 1) {
1760           RTC_LOG(LS_WARNING)
1761               << "CN #channels " << cn_codec.channels << " not supported.";
1762         } else if (cn_codec.clockrate != 8000 && cn_codec.clockrate != 16000 &&
1763                    cn_codec.clockrate != 32000) {
1764           RTC_LOG(LS_WARNING)
1765               << "CN frequency " << cn_codec.clockrate << " not supported.";
1766         } else {
1767           send_codec_spec->cng_payload_type = cn_codec.id;
1768         }
1769         break;
1770       }
1771     }
1772 
1773     // Find the telephone-event PT exactly matching the preferred send codec.
1774     for (const AudioCodec& dtmf_codec : dtmf_codecs) {
1775       if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
1776         dtmf_payload_type_ = dtmf_codec.id;
1777         dtmf_payload_freq_ = dtmf_codec.clockrate;
1778         break;
1779       }
1780     }
1781   }
1782 
1783   if (IsAudioRedForOpusFieldTrialEnabled()) {
1784     // Loop through the codecs to find the RED codec that matches opus
1785     // with respect to clockrate and number of channels.
1786     for (const AudioCodec& red_codec : codecs) {
1787       if (IsCodec(red_codec, kRedCodecName) &&
1788           red_codec.clockrate == send_codec_spec->format.clockrate_hz &&
1789           red_codec.channels == send_codec_spec->format.num_channels) {
1790         send_codec_spec->red_payload_type = red_codec.id;
1791         break;
1792       }
1793     }
1794   }
1795 
1796   if (send_codec_spec_ != send_codec_spec) {
1797     send_codec_spec_ = std::move(send_codec_spec);
1798     // Apply new settings to all streams.
1799     for (const auto& kv : send_streams_) {
1800       kv.second->SetSendCodecSpec(*send_codec_spec_);
1801     }
1802   } else {
1803     // If the codec isn't changing, set the start bitrate to -1 which means
1804     // "unchanged" so that BWE isn't affected.
1805     bitrate_config.start_bitrate_bps = -1;
1806   }
1807   call_->GetTransportControllerSend()->SetSdpBitrateParameters(bitrate_config);
1808 
1809   // Check if the transport cc feedback or NACK status has changed on the
1810   // preferred send codec, and in that case reconfigure all receive streams.
1811   if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1812       recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
1813     RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1814                         "codec has changed.";
1815     recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1816     recv_nack_enabled_ = send_codec_spec_->nack_enabled;
1817     for (auto& kv : recv_streams_) {
1818       kv.second->SetUseTransportCcAndRecreateStream(recv_transport_cc_enabled_,
1819                                                     recv_nack_enabled_);
1820     }
1821   }
1822 
1823   send_codecs_ = codecs;
1824   return true;
1825 }
1826 
SetPlayout(bool playout)1827 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1828   desired_playout_ = playout;
1829   return ChangePlayout(desired_playout_);
1830 }
1831 
ChangePlayout(bool playout)1832 void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1833   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
1834   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1835   if (playout_ == playout) {
1836     return;
1837   }
1838 
1839   for (const auto& kv : recv_streams_) {
1840     kv.second->SetPlayout(playout);
1841   }
1842   playout_ = playout;
1843 }
1844 
SetSend(bool send)1845 void WebRtcVoiceMediaChannel::SetSend(bool send) {
1846   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
1847   if (send_ == send) {
1848     return;
1849   }
1850 
1851   // Apply channel specific options, and initialize the ADM for recording (this
1852   // may take time on some platforms, e.g. Android).
1853   if (send) {
1854     engine()->ApplyOptions(options_);
1855 
1856     // InitRecording() may return an error if the ADM is already recording.
1857     if (!engine()->adm()->RecordingIsInitialized() &&
1858         !engine()->adm()->Recording()) {
1859       if (engine()->adm()->InitRecording() != 0) {
1860         RTC_LOG(LS_WARNING) << "Failed to initialize recording";
1861       }
1862     }
1863   }
1864 
1865   // Change the settings on each send channel.
1866   for (auto& kv : send_streams_) {
1867     kv.second->SetSend(send);
1868   }
1869 
1870   send_ = send;
1871 }
1872 
SetAudioSend(uint32_t ssrc,bool enable,const AudioOptions * options,AudioSource * source)1873 bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1874                                            bool enable,
1875                                            const AudioOptions* options,
1876                                            AudioSource* source) {
1877   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1878   // TODO(solenberg): The state change should be fully rolled back if any one of
1879   //                  these calls fail.
1880   if (!SetLocalSource(ssrc, source)) {
1881     return false;
1882   }
1883   if (!MuteStream(ssrc, !enable)) {
1884     return false;
1885   }
1886   if (enable && options) {
1887     return SetOptions(*options);
1888   }
1889   return true;
1890 }
1891 
AddSendStream(const StreamParams & sp)1892 bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
1893   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
1894   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1895   RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1896 
1897   uint32_t ssrc = sp.first_ssrc();
1898   RTC_DCHECK(0 != ssrc);
1899 
1900   if (send_streams_.find(ssrc) != send_streams_.end()) {
1901     RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
1902     return false;
1903   }
1904 
1905   absl::optional<std::string> audio_network_adaptor_config =
1906       GetAudioNetworkAdaptorConfig(options_);
1907   WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
1908       ssrc, mid_, sp.cname, sp.id, send_codec_spec_, ExtmapAllowMixed(),
1909       send_rtp_extensions_, max_send_bitrate_bps_,
1910       audio_config_.rtcp_report_interval_ms, audio_network_adaptor_config,
1911       call_, this, engine()->encoder_factory_, codec_pair_id_, nullptr,
1912       crypto_options_);
1913   send_streams_.insert(std::make_pair(ssrc, stream));
1914 
1915   // At this point the stream's local SSRC has been updated. If it is the first
1916   // send stream, make sure that all the receive streams are updated with the
1917   // same SSRC in order to send receiver reports.
1918   if (send_streams_.size() == 1) {
1919     receiver_reports_ssrc_ = ssrc;
1920     for (const auto& kv : recv_streams_) {
1921       // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1922       // streams instead, so we can avoid reconfiguring the streams here.
1923       kv.second->SetLocalSsrc(ssrc);
1924     }
1925   }
1926 
1927   send_streams_[ssrc]->SetSend(send_);
1928   return true;
1929 }
1930 
RemoveSendStream(uint32_t ssrc)1931 bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
1932   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
1933   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1934   RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1935 
1936   auto it = send_streams_.find(ssrc);
1937   if (it == send_streams_.end()) {
1938     RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1939                         << " which doesn't exist.";
1940     return false;
1941   }
1942 
1943   it->second->SetSend(false);
1944 
1945   // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1946   // the first active send stream and use that instead, reassociating receive
1947   // streams.
1948 
1949   delete it->second;
1950   send_streams_.erase(it);
1951   if (send_streams_.empty()) {
1952     SetSend(false);
1953   }
1954   return true;
1955 }
1956 
AddRecvStream(const StreamParams & sp)1957 bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
1958   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
1959   RTC_DCHECK(worker_thread_checker_.IsCurrent());
1960   RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1961 
1962   if (!sp.has_ssrcs()) {
1963     // This is a StreamParam with unsignaled SSRCs. Store it, so it can be used
1964     // later when we know the SSRCs on the first packet arrival.
1965     unsignaled_stream_params_ = sp;
1966     return true;
1967   }
1968 
1969   if (!ValidateStreamParams(sp)) {
1970     return false;
1971   }
1972 
1973   const uint32_t ssrc = sp.first_ssrc();
1974 
1975   // If this stream was previously received unsignaled, we promote it, possibly
1976   // recreating the AudioReceiveStream, if stream ids have changed.
1977   if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
1978     recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.stream_ids());
1979     return true;
1980   }
1981 
1982   if (recv_streams_.find(ssrc) != recv_streams_.end()) {
1983     RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
1984     return false;
1985   }
1986 
1987   // Create a new channel for receiving audio data.
1988   recv_streams_.insert(std::make_pair(
1989       ssrc, new WebRtcAudioReceiveStream(
1990                 ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1991                 recv_nack_enabled_, sp.stream_ids(), recv_rtp_extensions_,
1992                 call_, this, engine()->decoder_factory_, decoder_map_,
1993                 codec_pair_id_, engine()->audio_jitter_buffer_max_packets_,
1994                 engine()->audio_jitter_buffer_fast_accelerate_,
1995                 engine()->audio_jitter_buffer_min_delay_ms_,
1996                 engine()->audio_jitter_buffer_enable_rtx_handling_,
1997                 unsignaled_frame_decryptor_, crypto_options_, nullptr)));
1998   recv_streams_[ssrc]->SetPlayout(playout_);
1999 
2000   return true;
2001 }
2002 
RemoveRecvStream(uint32_t ssrc)2003 bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
2004   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
2005   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2006   RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2007 
2008   const auto it = recv_streams_.find(ssrc);
2009   if (it == recv_streams_.end()) {
2010     RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2011                         << " which doesn't exist.";
2012     return false;
2013   }
2014 
2015   MaybeDeregisterUnsignaledRecvStream(ssrc);
2016 
2017   it->second->SetRawAudioSink(nullptr);
2018   delete it->second;
2019   recv_streams_.erase(it);
2020   return true;
2021 }
2022 
ResetUnsignaledRecvStream()2023 void WebRtcVoiceMediaChannel::ResetUnsignaledRecvStream() {
2024   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2025   RTC_LOG(LS_INFO) << "ResetUnsignaledRecvStream.";
2026   unsignaled_stream_params_ = StreamParams();
2027 }
2028 
SetLocalSource(uint32_t ssrc,AudioSource * source)2029 bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2030                                              AudioSource* source) {
2031   auto it = send_streams_.find(ssrc);
2032   if (it == send_streams_.end()) {
2033     if (source) {
2034       // Return an error if trying to set a valid source with an invalid ssrc.
2035       RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
2036       return false;
2037     }
2038 
2039     // The channel likely has gone away, do nothing.
2040     return true;
2041   }
2042 
2043   if (source) {
2044     it->second->SetSource(source);
2045   } else {
2046     it->second->ClearSource();
2047   }
2048 
2049   return true;
2050 }
2051 
SetOutputVolume(uint32_t ssrc,double volume)2052 bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
2053   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2054   const auto it = recv_streams_.find(ssrc);
2055   if (it == recv_streams_.end()) {
2056     RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
2057     return false;
2058   }
2059   it->second->SetOutputVolume(volume);
2060   RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2061                    << " for recv stream with ssrc " << ssrc;
2062   return true;
2063 }
2064 
SetDefaultOutputVolume(double volume)2065 bool WebRtcVoiceMediaChannel::SetDefaultOutputVolume(double volume) {
2066   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2067   default_recv_volume_ = volume;
2068   for (uint32_t ssrc : unsignaled_recv_ssrcs_) {
2069     const auto it = recv_streams_.find(ssrc);
2070     if (it == recv_streams_.end()) {
2071       RTC_LOG(LS_WARNING) << "SetDefaultOutputVolume: no recv stream " << ssrc;
2072       return false;
2073     }
2074     it->second->SetOutputVolume(volume);
2075     RTC_LOG(LS_INFO) << "SetDefaultOutputVolume() to " << volume
2076                      << " for recv stream with ssrc " << ssrc;
2077   }
2078   return true;
2079 }
2080 
SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,int delay_ms)2081 bool WebRtcVoiceMediaChannel::SetBaseMinimumPlayoutDelayMs(uint32_t ssrc,
2082                                                            int delay_ms) {
2083   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2084   std::vector<uint32_t> ssrcs(1, ssrc);
2085   // SSRC of 0 represents the default receive stream.
2086   if (ssrc == 0) {
2087     default_recv_base_minimum_delay_ms_ = delay_ms;
2088     ssrcs = unsignaled_recv_ssrcs_;
2089   }
2090   for (uint32_t ssrc : ssrcs) {
2091     const auto it = recv_streams_.find(ssrc);
2092     if (it == recv_streams_.end()) {
2093       RTC_LOG(LS_WARNING) << "SetBaseMinimumPlayoutDelayMs: no recv stream "
2094                           << ssrc;
2095       return false;
2096     }
2097     it->second->SetBaseMinimumPlayoutDelayMs(delay_ms);
2098     RTC_LOG(LS_INFO) << "SetBaseMinimumPlayoutDelayMs() to " << delay_ms
2099                      << " for recv stream with ssrc " << ssrc;
2100   }
2101   return true;
2102 }
2103 
GetBaseMinimumPlayoutDelayMs(uint32_t ssrc) const2104 absl::optional<int> WebRtcVoiceMediaChannel::GetBaseMinimumPlayoutDelayMs(
2105     uint32_t ssrc) const {
2106   // SSRC of 0 represents the default receive stream.
2107   if (ssrc == 0) {
2108     return default_recv_base_minimum_delay_ms_;
2109   }
2110 
2111   const auto it = recv_streams_.find(ssrc);
2112 
2113   if (it != recv_streams_.end()) {
2114     return it->second->GetBaseMinimumPlayoutDelayMs();
2115   }
2116   return absl::nullopt;
2117 }
2118 
CanInsertDtmf()2119 bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2120   return dtmf_payload_type_.has_value() && send_;
2121 }
2122 
SetFrameDecryptor(uint32_t ssrc,rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor)2123 void WebRtcVoiceMediaChannel::SetFrameDecryptor(
2124     uint32_t ssrc,
2125     rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) {
2126   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2127   auto matching_stream = recv_streams_.find(ssrc);
2128   if (matching_stream != recv_streams_.end()) {
2129     matching_stream->second->SetFrameDecryptor(frame_decryptor);
2130   }
2131   // Handle unsignaled frame decryptors.
2132   if (ssrc == 0) {
2133     unsignaled_frame_decryptor_ = frame_decryptor;
2134   }
2135 }
2136 
SetFrameEncryptor(uint32_t ssrc,rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor)2137 void WebRtcVoiceMediaChannel::SetFrameEncryptor(
2138     uint32_t ssrc,
2139     rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor) {
2140   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2141   auto matching_stream = send_streams_.find(ssrc);
2142   if (matching_stream != send_streams_.end()) {
2143     matching_stream->second->SetFrameEncryptor(frame_encryptor);
2144   }
2145 }
2146 
InsertDtmf(uint32_t ssrc,int event,int duration)2147 bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2148                                          int event,
2149                                          int duration) {
2150   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2151   RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2152   if (!CanInsertDtmf()) {
2153     return false;
2154   }
2155 
2156   // Figure out which WebRtcAudioSendStream to send the event on.
2157   auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2158   if (it == send_streams_.end()) {
2159     RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2160     return false;
2161   }
2162   if (event < kMinTelephoneEventCode || event > kMaxTelephoneEventCode) {
2163     RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
2164     return false;
2165   }
2166   RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2167   return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2168                                         event, duration);
2169 }
2170 
OnPacketReceived(rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)2171 void WebRtcVoiceMediaChannel::OnPacketReceived(rtc::CopyOnWriteBuffer packet,
2172                                                int64_t packet_time_us) {
2173   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2174 
2175   webrtc::PacketReceiver::DeliveryStatus delivery_result =
2176       call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, packet,
2177                                        packet_time_us);
2178 
2179   if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2180     return;
2181   }
2182 
2183   // Create an unsignaled receive stream for this previously not received ssrc.
2184   // If there already is N unsignaled receive streams, delete the oldest.
2185   // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2186   uint32_t ssrc = 0;
2187   if (!GetRtpSsrc(packet.cdata(), packet.size(), &ssrc)) {
2188     return;
2189   }
2190   RTC_DCHECK(!absl::c_linear_search(unsignaled_recv_ssrcs_, ssrc));
2191 
2192   // Add new stream.
2193   StreamParams sp = unsignaled_stream_params_;
2194   sp.ssrcs.push_back(ssrc);
2195   RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
2196   if (!AddRecvStream(sp)) {
2197     RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
2198     return;
2199   }
2200   unsignaled_recv_ssrcs_.push_back(ssrc);
2201   RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.NumOfUnsignaledStreams",
2202                               unsignaled_recv_ssrcs_.size(), 1, 100, 101);
2203 
2204   // Remove oldest unsignaled stream, if we have too many.
2205   if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2206     uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
2207     RTC_DLOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2208                       << remove_ssrc;
2209     RemoveRecvStream(remove_ssrc);
2210   }
2211   RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2212 
2213   SetOutputVolume(ssrc, default_recv_volume_);
2214   SetBaseMinimumPlayoutDelayMs(ssrc, default_recv_base_minimum_delay_ms_);
2215 
2216   // The default sink can only be attached to one stream at a time, so we hook
2217   // it up to the *latest* unsignaled stream we've seen, in order to support the
2218   // case where the SSRC of one unsignaled stream changes.
2219   if (default_sink_) {
2220     for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2221       auto it = recv_streams_.find(drop_ssrc);
2222       it->second->SetRawAudioSink(nullptr);
2223     }
2224     std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2225         new ProxySink(default_sink_.get()));
2226     SetRawAudioSink(ssrc, std::move(proxy_sink));
2227   }
2228 
2229   delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2230                                                      packet, packet_time_us);
2231   RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
2232 }
2233 
OnNetworkRouteChanged(const std::string & transport_name,const rtc::NetworkRoute & network_route)2234 void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2235     const std::string& transport_name,
2236     const rtc::NetworkRoute& network_route) {
2237   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2238   call_->GetTransportControllerSend()->OnNetworkRouteChanged(transport_name,
2239                                                              network_route);
2240   call_->OnAudioTransportOverheadChanged(network_route.packet_overhead);
2241 }
2242 
MuteStream(uint32_t ssrc,bool muted)2243 bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
2244   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2245   const auto it = send_streams_.find(ssrc);
2246   if (it == send_streams_.end()) {
2247     RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2248     return false;
2249   }
2250   it->second->SetMuted(muted);
2251 
2252   // TODO(solenberg):
2253   // We set the AGC to mute state only when all the channels are muted.
2254   // This implementation is not ideal, instead we should signal the AGC when
2255   // the mic channel is muted/unmuted. We can't do it today because there
2256   // is no good way to know which stream is mapping to the mic channel.
2257   bool all_muted = muted;
2258   for (const auto& kv : send_streams_) {
2259     all_muted = all_muted && kv.second->muted();
2260   }
2261   webrtc::AudioProcessing* ap = engine()->apm();
2262   if (ap) {
2263     ap->set_output_will_be_muted(all_muted);
2264   }
2265 
2266   return true;
2267 }
2268 
SetMaxSendBitrate(int bps)2269 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2270   RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2271   max_send_bitrate_bps_ = bps;
2272   bool success = true;
2273   for (const auto& kv : send_streams_) {
2274     if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2275       success = false;
2276     }
2277   }
2278   return success;
2279 }
2280 
OnReadyToSend(bool ready)2281 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2282   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2283   RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2284   call_->SignalChannelNetworkState(
2285       webrtc::MediaType::AUDIO,
2286       ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2287 }
2288 
GetStats(VoiceMediaInfo * info)2289 bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
2290   TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
2291   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2292   RTC_DCHECK(info);
2293 
2294   // Get SSRC and stats for each sender.
2295   RTC_DCHECK_EQ(info->senders.size(), 0U);
2296   for (const auto& stream : send_streams_) {
2297     webrtc::AudioSendStream::Stats stats =
2298         stream.second->GetStats(recv_streams_.size() > 0);
2299     VoiceSenderInfo sinfo;
2300     sinfo.add_ssrc(stats.local_ssrc);
2301     sinfo.payload_bytes_sent = stats.payload_bytes_sent;
2302     sinfo.header_and_padding_bytes_sent = stats.header_and_padding_bytes_sent;
2303     sinfo.retransmitted_bytes_sent = stats.retransmitted_bytes_sent;
2304     sinfo.packets_sent = stats.packets_sent;
2305     sinfo.retransmitted_packets_sent = stats.retransmitted_packets_sent;
2306     sinfo.packets_lost = stats.packets_lost;
2307     sinfo.fraction_lost = stats.fraction_lost;
2308     sinfo.codec_name = stats.codec_name;
2309     sinfo.codec_payload_type = stats.codec_payload_type;
2310     sinfo.jitter_ms = stats.jitter_ms;
2311     sinfo.rtt_ms = stats.rtt_ms;
2312     sinfo.audio_level = stats.audio_level;
2313     sinfo.total_input_energy = stats.total_input_energy;
2314     sinfo.total_input_duration = stats.total_input_duration;
2315     sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
2316     sinfo.ana_statistics = stats.ana_statistics;
2317     sinfo.apm_statistics = stats.apm_statistics;
2318     sinfo.report_block_datas = std::move(stats.report_block_datas);
2319     info->senders.push_back(sinfo);
2320   }
2321 
2322   // Get SSRC and stats for each receiver.
2323   RTC_DCHECK_EQ(info->receivers.size(), 0U);
2324   for (const auto& stream : recv_streams_) {
2325     uint32_t ssrc = stream.first;
2326     // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2327     // multiple RTP streams can be received over time (if the SSRC changes for
2328     // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2329     // the stats for the most recent stream (the one whose audio is actually
2330     // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2331     // except for the most recent one (last in the vector). This is somewhat of
2332     // a hack, and means you don't get *any* stats for these inactive streams,
2333     // but it's slightly better than the previous behavior, which was "highest
2334     // SSRC wins".
2335     // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2336     if (!unsignaled_recv_ssrcs_.empty()) {
2337       auto end_it = --unsignaled_recv_ssrcs_.end();
2338       if (absl::linear_search(unsignaled_recv_ssrcs_.begin(), end_it, ssrc)) {
2339         continue;
2340       }
2341     }
2342     webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2343     VoiceReceiverInfo rinfo;
2344     rinfo.add_ssrc(stats.remote_ssrc);
2345     rinfo.payload_bytes_rcvd = stats.payload_bytes_rcvd;
2346     rinfo.header_and_padding_bytes_rcvd = stats.header_and_padding_bytes_rcvd;
2347     rinfo.packets_rcvd = stats.packets_rcvd;
2348     rinfo.fec_packets_received = stats.fec_packets_received;
2349     rinfo.fec_packets_discarded = stats.fec_packets_discarded;
2350     rinfo.packets_lost = stats.packets_lost;
2351     rinfo.codec_name = stats.codec_name;
2352     rinfo.codec_payload_type = stats.codec_payload_type;
2353     rinfo.jitter_ms = stats.jitter_ms;
2354     rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2355     rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2356     rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2357     rinfo.audio_level = stats.audio_level;
2358     rinfo.total_output_energy = stats.total_output_energy;
2359     rinfo.total_samples_received = stats.total_samples_received;
2360     rinfo.total_output_duration = stats.total_output_duration;
2361     rinfo.concealed_samples = stats.concealed_samples;
2362     rinfo.silent_concealed_samples = stats.silent_concealed_samples;
2363     rinfo.concealment_events = stats.concealment_events;
2364     rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
2365     rinfo.jitter_buffer_emitted_count = stats.jitter_buffer_emitted_count;
2366     rinfo.jitter_buffer_target_delay_seconds =
2367         stats.jitter_buffer_target_delay_seconds;
2368     rinfo.inserted_samples_for_deceleration =
2369         stats.inserted_samples_for_deceleration;
2370     rinfo.removed_samples_for_acceleration =
2371         stats.removed_samples_for_acceleration;
2372     rinfo.expand_rate = stats.expand_rate;
2373     rinfo.speech_expand_rate = stats.speech_expand_rate;
2374     rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2375     rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
2376     rinfo.accelerate_rate = stats.accelerate_rate;
2377     rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2378     rinfo.delayed_packet_outage_samples = stats.delayed_packet_outage_samples;
2379     rinfo.decoding_calls_to_silence_generator =
2380         stats.decoding_calls_to_silence_generator;
2381     rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2382     rinfo.decoding_normal = stats.decoding_normal;
2383     rinfo.decoding_plc = stats.decoding_plc;
2384     rinfo.decoding_codec_plc = stats.decoding_codec_plc;
2385     rinfo.decoding_cng = stats.decoding_cng;
2386     rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2387     rinfo.decoding_muted_output = stats.decoding_muted_output;
2388     rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2389     rinfo.last_packet_received_timestamp_ms =
2390         stats.last_packet_received_timestamp_ms;
2391     rinfo.estimated_playout_ntp_timestamp_ms =
2392         stats.estimated_playout_ntp_timestamp_ms;
2393     rinfo.jitter_buffer_flushes = stats.jitter_buffer_flushes;
2394     rinfo.relative_packet_arrival_delay_seconds =
2395         stats.relative_packet_arrival_delay_seconds;
2396     rinfo.interruption_count = stats.interruption_count;
2397     rinfo.total_interruption_duration_ms = stats.total_interruption_duration_ms;
2398 
2399     info->receivers.push_back(rinfo);
2400   }
2401 
2402   // Get codec info
2403   for (const AudioCodec& codec : send_codecs_) {
2404     webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2405     info->send_codecs.insert(
2406         std::make_pair(codec_params.payload_type, std::move(codec_params)));
2407   }
2408   for (const AudioCodec& codec : recv_codecs_) {
2409     webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2410     info->receive_codecs.insert(
2411         std::make_pair(codec_params.payload_type, std::move(codec_params)));
2412   }
2413   info->device_underrun_count = engine_->adm()->GetPlayoutUnderrunCount();
2414 
2415   return true;
2416 }
2417 
SetRawAudioSink(uint32_t ssrc,std::unique_ptr<webrtc::AudioSinkInterface> sink)2418 void WebRtcVoiceMediaChannel::SetRawAudioSink(
2419     uint32_t ssrc,
2420     std::unique_ptr<webrtc::AudioSinkInterface> sink) {
2421   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2422   RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2423                       << ssrc << " " << (sink ? "(ptr)" : "NULL");
2424   const auto it = recv_streams_.find(ssrc);
2425   if (it == recv_streams_.end()) {
2426     RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
2427     return;
2428   }
2429   it->second->SetRawAudioSink(std::move(sink));
2430 }
2431 
SetDefaultRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink)2432 void WebRtcVoiceMediaChannel::SetDefaultRawAudioSink(
2433     std::unique_ptr<webrtc::AudioSinkInterface> sink) {
2434   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2435   RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetDefaultRawAudioSink:";
2436   if (!unsignaled_recv_ssrcs_.empty()) {
2437     std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2438         sink ? new ProxySink(sink.get()) : nullptr);
2439     SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
2440   }
2441   default_sink_ = std::move(sink);
2442 }
2443 
GetSources(uint32_t ssrc) const2444 std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2445     uint32_t ssrc) const {
2446   auto it = recv_streams_.find(ssrc);
2447   if (it == recv_streams_.end()) {
2448     RTC_LOG(LS_ERROR) << "Attempting to get contributing sources for SSRC:"
2449                       << ssrc << " which doesn't exist.";
2450     return std::vector<webrtc::RtpSource>();
2451   }
2452   return it->second->GetSources();
2453 }
2454 
SetEncoderToPacketizerFrameTransformer(uint32_t ssrc,rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)2455 void WebRtcVoiceMediaChannel::SetEncoderToPacketizerFrameTransformer(
2456     uint32_t ssrc,
2457     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
2458   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2459   auto matching_stream = send_streams_.find(ssrc);
2460   if (matching_stream == send_streams_.end()) {
2461     RTC_LOG(LS_INFO) << "Attempting to set frame transformer for SSRC:" << ssrc
2462                      << " which doesn't exist.";
2463     return;
2464   }
2465   matching_stream->second->SetEncoderToPacketizerFrameTransformer(
2466       std::move(frame_transformer));
2467 }
2468 
SetDepacketizerToDecoderFrameTransformer(uint32_t ssrc,rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)2469 void WebRtcVoiceMediaChannel::SetDepacketizerToDecoderFrameTransformer(
2470     uint32_t ssrc,
2471     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
2472   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2473   auto matching_stream = recv_streams_.find(ssrc);
2474   if (matching_stream == recv_streams_.end()) {
2475     RTC_LOG(LS_INFO) << "Attempting to set frame transformer for SSRC:" << ssrc
2476                      << " which doesn't exist.";
2477     return;
2478   }
2479   matching_stream->second->SetDepacketizerToDecoderFrameTransformer(
2480       std::move(frame_transformer));
2481 }
2482 
MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc)2483 bool WebRtcVoiceMediaChannel::MaybeDeregisterUnsignaledRecvStream(
2484     uint32_t ssrc) {
2485   RTC_DCHECK(worker_thread_checker_.IsCurrent());
2486   auto it = absl::c_find(unsignaled_recv_ssrcs_, ssrc);
2487   if (it != unsignaled_recv_ssrcs_.end()) {
2488     unsignaled_recv_ssrcs_.erase(it);
2489     return true;
2490   }
2491   return false;
2492 }
2493 }  // namespace cricket
2494