tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

webrtc_voice_engine.cc (103333B)


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