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