Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2004 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifdef HAVE_CONFIG_H
     29 #include <config.h>
     30 #endif
     31 
     32 #ifdef HAVE_WEBRTC_VOICE
     33 
     34 #include "talk/media/webrtc/webrtcvoiceengine.h"
     35 
     36 #include <algorithm>
     37 #include <cstdio>
     38 #include <string>
     39 #include <vector>
     40 
     41 #include "talk/media/base/audioframe.h"
     42 #include "talk/media/base/audiorenderer.h"
     43 #include "talk/media/base/constants.h"
     44 #include "talk/media/base/streamparams.h"
     45 #include "talk/media/webrtc/webrtcmediaengine.h"
     46 #include "talk/media/webrtc/webrtcvoe.h"
     47 #include "webrtc/audio/audio_sink.h"
     48 #include "webrtc/base/arraysize.h"
     49 #include "webrtc/base/base64.h"
     50 #include "webrtc/base/byteorder.h"
     51 #include "webrtc/base/common.h"
     52 #include "webrtc/base/helpers.h"
     53 #include "webrtc/base/logging.h"
     54 #include "webrtc/base/stringencode.h"
     55 #include "webrtc/base/stringutils.h"
     56 #include "webrtc/call/rtc_event_log.h"
     57 #include "webrtc/common.h"
     58 #include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
     59 #include "webrtc/modules/audio_processing/include/audio_processing.h"
     60 #include "webrtc/system_wrappers/include/field_trial.h"
     61 #include "webrtc/system_wrappers/include/trace.h"
     62 
     63 namespace cricket {
     64 namespace {
     65 
     66 const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
     67                                 webrtc::kTraceWarning | webrtc::kTraceError |
     68                                 webrtc::kTraceCritical;
     69 const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
     70                                  webrtc::kTraceInfo;
     71 
     72 // On Windows Vista and newer, Microsoft introduced the concept of "Default
     73 // Communications Device". This means that there are two types of default
     74 // devices (old Wave Audio style default and Default Communications Device).
     75 //
     76 // On Windows systems which only support Wave Audio style default, uses either
     77 // -1 or 0 to select the default device.
     78 #ifdef WIN32
     79 const int kDefaultAudioDeviceId = -1;
     80 #else
     81 const int kDefaultAudioDeviceId = 0;
     82 #endif
     83 
     84 // Parameter used for NACK.
     85 // This value is equivalent to 5 seconds of audio data at 20 ms per packet.
     86 const int kNackMaxPackets = 250;
     87 
     88 // Codec parameters for Opus.
     89 // draft-spittka-payload-rtp-opus-03
     90 
     91 // Recommended bitrates:
     92 // 8-12 kb/s for NB speech,
     93 // 16-20 kb/s for WB speech,
     94 // 28-40 kb/s for FB speech,
     95 // 48-64 kb/s for FB mono music, and
     96 // 64-128 kb/s for FB stereo music.
     97 // The current implementation applies the following values to mono signals,
     98 // and multiplies them by 2 for stereo.
     99 const int kOpusBitrateNb = 12000;
    100 const int kOpusBitrateWb = 20000;
    101 const int kOpusBitrateFb = 32000;
    102 
    103 // Opus bitrate should be in the range between 6000 and 510000.
    104 const int kOpusMinBitrate = 6000;
    105 const int kOpusMaxBitrate = 510000;
    106 
    107 // Default audio dscp value.
    108 // See http://tools.ietf.org/html/rfc2474 for details.
    109 // See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
    110 const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
    111 
    112 // Ensure we open the file in a writeable path on ChromeOS and Android. This
    113 // workaround can be removed when it's possible to specify a filename for audio
    114 // option based AEC dumps.
    115 //
    116 // TODO(grunell): Use a string in the options instead of hardcoding it here
    117 // and let the embedder choose the filename (crbug.com/264223).
    118 //
    119 // NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
    120 // below.
    121 #if defined(CHROMEOS)
    122 const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
    123 #elif defined(ANDROID)
    124 const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
    125 #else
    126 const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
    127 #endif
    128 
    129 // Constants from voice_engine_defines.h.
    130 const int kMinTelephoneEventCode = 0;           // RFC4733 (Section 2.3.1)
    131 const int kMaxTelephoneEventCode = 255;
    132 const int kMinTelephoneEventDuration = 100;
    133 const int kMaxTelephoneEventDuration = 60000;   // Actual limit is 2^16
    134 
    135 bool ValidateStreamParams(const StreamParams& sp) {
    136   if (sp.ssrcs.empty()) {
    137     LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
    138     return false;
    139   }
    140   if (sp.ssrcs.size() > 1) {
    141     LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
    142     return false;
    143   }
    144   return true;
    145 }
    146 
    147 // Dumps an AudioCodec in RFC 2327-ish format.
    148 std::string ToString(const AudioCodec& codec) {
    149   std::stringstream ss;
    150   ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
    151      << " (" << codec.id << ")";
    152   return ss.str();
    153 }
    154 
    155 std::string ToString(const webrtc::CodecInst& codec) {
    156   std::stringstream ss;
    157   ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
    158      << " (" << codec.pltype << ")";
    159   return ss.str();
    160 }
    161 
    162 bool IsCodec(const AudioCodec& codec, const char* ref_name) {
    163   return (_stricmp(codec.name.c_str(), ref_name) == 0);
    164 }
    165 
    166 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
    167   return (_stricmp(codec.plname, ref_name) == 0);
    168 }
    169 
    170 bool FindCodec(const std::vector<AudioCodec>& codecs,
    171                const AudioCodec& codec,
    172                AudioCodec* found_codec) {
    173   for (const AudioCodec& c : codecs) {
    174     if (c.Matches(codec)) {
    175       if (found_codec != NULL) {
    176         *found_codec = c;
    177       }
    178       return true;
    179     }
    180   }
    181   return false;
    182 }
    183 
    184 bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
    185   if (codecs.empty()) {
    186     return true;
    187   }
    188   std::vector<int> payload_types;
    189   for (const AudioCodec& codec : codecs) {
    190     payload_types.push_back(codec.id);
    191   }
    192   std::sort(payload_types.begin(), payload_types.end());
    193   auto it = std::unique(payload_types.begin(), payload_types.end());
    194   return it == payload_types.end();
    195 }
    196 
    197 bool IsNackEnabled(const AudioCodec& codec) {
    198   return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
    199                                               kParamValueEmpty));
    200 }
    201 
    202 // Return true if codec.params[feature] == "1", false otherwise.
    203 bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
    204   int value;
    205   return codec.GetParam(feature, &value) && value == 1;
    206 }
    207 
    208 // Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
    209 // otherwise. If the value (either from params or codec.bitrate) <=0, use the
    210 // default configuration. If the value is beyond feasible bit rate of Opus,
    211 // clamp it. Returns the Opus bit rate for operation.
    212 int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
    213   int bitrate = 0;
    214   bool use_param = true;
    215   if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
    216     bitrate = codec.bitrate;
    217     use_param = false;
    218   }
    219   if (bitrate <= 0) {
    220     if (max_playback_rate <= 8000) {
    221       bitrate = kOpusBitrateNb;
    222     } else if (max_playback_rate <= 16000) {
    223       bitrate = kOpusBitrateWb;
    224     } else {
    225       bitrate = kOpusBitrateFb;
    226     }
    227 
    228     if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
    229       bitrate *= 2;
    230     }
    231   } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
    232     bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
    233     std::string rate_source =
    234         use_param ? "Codec parameter \"maxaveragebitrate\"" :
    235             "Supplied Opus bitrate";
    236     LOG(LS_WARNING) << rate_source
    237                     << " is invalid and is replaced by: "
    238                     << bitrate;
    239   }
    240   return bitrate;
    241 }
    242 
    243 // Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
    244 // defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
    245 int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
    246   int value;
    247   if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
    248     return value;
    249   }
    250   return kOpusDefaultMaxPlaybackRate;
    251 }
    252 
    253 void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
    254                           bool* enable_codec_fec, int* max_playback_rate,
    255                           bool* enable_codec_dtx) {
    256   *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
    257   *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
    258   *max_playback_rate = GetOpusMaxPlaybackRate(codec);
    259 
    260   // If OPUS, change what we send according to the "stereo" codec
    261   // parameter, and not the "channels" parameter.  We set
    262   // voe_codec.channels to 2 if "stereo=1" and 1 otherwise.  If
    263   // the bitrate is not specified, i.e. is <= zero, we set it to the
    264   // appropriate default value for mono or stereo Opus.
    265 
    266   voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
    267   voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
    268 }
    269 
    270 webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
    271   webrtc::AudioState::Config config;
    272   config.voice_engine = voe_wrapper->engine();
    273   return config;
    274 }
    275 
    276 class WebRtcVoiceCodecs final {
    277  public:
    278   // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
    279   // list and add a test which verifies VoE supports the listed codecs.
    280   static std::vector<AudioCodec> SupportedCodecs() {
    281     LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
    282     std::vector<AudioCodec> result;
    283     for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
    284       // Change the sample rate of G722 to 8000 to match SDP.
    285       MaybeFixupG722(&voe_codec, 8000);
    286       // Skip uncompressed formats.
    287       if (IsCodec(voe_codec, kL16CodecName)) {
    288         continue;
    289       }
    290 
    291       const CodecPref* pref = NULL;
    292       for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
    293         if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
    294             kCodecPrefs[j].clockrate == voe_codec.plfreq &&
    295             kCodecPrefs[j].channels == voe_codec.channels) {
    296           pref = &kCodecPrefs[j];
    297           break;
    298         }
    299       }
    300 
    301       if (pref) {
    302         // Use the payload type that we've configured in our pref table;
    303         // use the offset in our pref table to determine the sort order.
    304         AudioCodec codec(
    305             pref->payload_type, voe_codec.plname, voe_codec.plfreq,
    306             voe_codec.rate, voe_codec.channels,
    307             static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
    308         LOG(LS_INFO) << ToString(codec);
    309         if (IsCodec(codec, kIsacCodecName)) {
    310           // Indicate auto-bitrate in signaling.
    311           codec.bitrate = 0;
    312         }
    313         if (IsCodec(codec, kOpusCodecName)) {
    314           // Only add fmtp parameters that differ from the spec.
    315           if (kPreferredMinPTime != kOpusDefaultMinPTime) {
    316             codec.params[kCodecParamMinPTime] =
    317                 rtc::ToString(kPreferredMinPTime);
    318           }
    319           if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
    320             codec.params[kCodecParamMaxPTime] =
    321                 rtc::ToString(kPreferredMaxPTime);
    322           }
    323           codec.SetParam(kCodecParamUseInbandFec, 1);
    324 
    325           // TODO(hellner): Add ptime, sprop-stereo, and stereo
    326           // when they can be set to values other than the default.
    327         }
    328         result.push_back(codec);
    329       } else {
    330         LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
    331       }
    332     }
    333     // Make sure they are in local preference order.
    334     std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
    335     return result;
    336   }
    337 
    338   static bool ToCodecInst(const AudioCodec& in,
    339                           webrtc::CodecInst* out) {
    340     for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
    341       // Change the sample rate of G722 to 8000 to match SDP.
    342       MaybeFixupG722(&voe_codec, 8000);
    343       AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
    344                        voe_codec.rate, voe_codec.channels, 0);
    345       bool multi_rate = IsCodecMultiRate(voe_codec);
    346       // Allow arbitrary rates for ISAC to be specified.
    347       if (multi_rate) {
    348         // Set codec.bitrate to 0 so the check for codec.Matches() passes.
    349         codec.bitrate = 0;
    350       }
    351       if (codec.Matches(in)) {
    352         if (out) {
    353           // Fixup the payload type.
    354           voe_codec.pltype = in.id;
    355 
    356           // Set bitrate if specified.
    357           if (multi_rate && in.bitrate != 0) {
    358             voe_codec.rate = in.bitrate;
    359           }
    360 
    361           // Reset G722 sample rate to 16000 to match WebRTC.
    362           MaybeFixupG722(&voe_codec, 16000);
    363 
    364           // Apply codec-specific settings.
    365           if (IsCodec(codec, kIsacCodecName)) {
    366             // If ISAC and an explicit bitrate is not specified,
    367             // enable auto bitrate adjustment.
    368             voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
    369           }
    370           *out = voe_codec;
    371         }
    372         return true;
    373       }
    374     }
    375     return false;
    376   }
    377 
    378   static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
    379     for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
    380       if (IsCodec(codec, kCodecPrefs[i].name) &&
    381           kCodecPrefs[i].clockrate == codec.plfreq) {
    382         return kCodecPrefs[i].is_multi_rate;
    383       }
    384     }
    385     return false;
    386   }
    387 
    388   // If the AudioCodec param kCodecParamPTime is set, then we will set it to
    389   // codec pacsize if it's valid, or we will pick the next smallest value we
    390   // support.
    391   // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
    392   static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
    393     for (const CodecPref& codec_pref : kCodecPrefs) {
    394       if ((IsCodec(*codec, codec_pref.name) &&
    395           codec_pref.clockrate == codec->plfreq) ||
    396           IsCodec(*codec, kG722CodecName)) {
    397         int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
    398         if (packet_size_ms) {
    399           // Convert unit from milli-seconds to samples.
    400           codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
    401           return true;
    402         }
    403       }
    404     }
    405     return false;
    406   }
    407 
    408  private:
    409   static const int kMaxNumPacketSize = 6;
    410   struct CodecPref {
    411     const char* name;
    412     int clockrate;
    413     size_t channels;
    414     int payload_type;
    415     bool is_multi_rate;
    416     int packet_sizes_ms[kMaxNumPacketSize];
    417   };
    418   // Note: keep the supported packet sizes in ascending order.
    419   static const CodecPref kCodecPrefs[12];
    420 
    421   static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
    422     int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
    423     for (int packet_size_ms : codec_pref.packet_sizes_ms) {
    424       if (packet_size_ms && packet_size_ms <= ptime_ms) {
    425         selected_packet_size_ms = packet_size_ms;
    426       }
    427     }
    428     return selected_packet_size_ms;
    429   }
    430 
    431   // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
    432   // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
    433   // codec.
    434   static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
    435     if (IsCodec(*voe_codec, kG722CodecName)) {
    436       // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
    437       // has changed, and this special case is no longer needed.
    438       RTC_DCHECK(voe_codec->plfreq != new_plfreq);
    439       voe_codec->plfreq = new_plfreq;
    440     }
    441   }
    442 };
    443 
    444 const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
    445   { kOpusCodecName,   48000, 2, 111, true,  { 10, 20, 40, 60 } },
    446   { kIsacCodecName,   16000, 1, 103, true,  { 30, 60 } },
    447   { kIsacCodecName,   32000, 1, 104, true,  { 30 } },
    448   // G722 should be advertised as 8000 Hz because of the RFC "bug".
    449   { kG722CodecName,   8000,  1, 9,   false, { 10, 20, 30, 40, 50, 60 } },
    450   { kIlbcCodecName,   8000,  1, 102, false, { 20, 30, 40, 60 } },
    451   { kPcmuCodecName,   8000,  1, 0,   false, { 10, 20, 30, 40, 50, 60 } },
    452   { kPcmaCodecName,   8000,  1, 8,   false, { 10, 20, 30, 40, 50, 60 } },
    453   { kCnCodecName,     32000, 1, 106, false, { } },
    454   { kCnCodecName,     16000, 1, 105, false, { } },
    455   { kCnCodecName,     8000,  1, 13,  false, { } },
    456   { kRedCodecName,    8000,  1, 127, false, { } },
    457   { kDtmfCodecName,   8000,  1, 126, false, { } },
    458 };
    459 } // namespace {
    460 
    461 bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
    462                                     webrtc::CodecInst* out) {
    463   return WebRtcVoiceCodecs::ToCodecInst(in, out);
    464 }
    465 
    466 WebRtcVoiceEngine::WebRtcVoiceEngine()
    467     : voe_wrapper_(new VoEWrapper()),
    468       audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
    469   Construct();
    470 }
    471 
    472 WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
    473     : voe_wrapper_(voe_wrapper) {
    474   Construct();
    475 }
    476 
    477 void WebRtcVoiceEngine::Construct() {
    478   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    479   LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
    480 
    481   signal_thread_checker_.DetachFromThread();
    482   std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
    483   voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
    484 
    485   webrtc::Trace::set_level_filter(kDefaultTraceFilter);
    486   webrtc::Trace::SetTraceCallback(this);
    487 
    488   // Load our audio codec list.
    489   codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
    490 }
    491 
    492 WebRtcVoiceEngine::~WebRtcVoiceEngine() {
    493   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    494   LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
    495   if (adm_) {
    496     voe_wrapper_.reset();
    497     adm_->Release();
    498     adm_ = NULL;
    499   }
    500   webrtc::Trace::SetTraceCallback(nullptr);
    501 }
    502 
    503 bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
    504   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    505   RTC_DCHECK(worker_thread == rtc::Thread::Current());
    506   LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
    507   bool res = InitInternal();
    508   if (res) {
    509     LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
    510   } else {
    511     LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
    512     Terminate();
    513   }
    514   return res;
    515 }
    516 
    517 bool WebRtcVoiceEngine::InitInternal() {
    518   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    519   // Temporarily turn logging level up for the Init call
    520   webrtc::Trace::set_level_filter(kElevatedTraceFilter);
    521   LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
    522   if (voe_wrapper_->base()->Init(adm_) == -1) {
    523     LOG_RTCERR0_EX(Init, voe_wrapper_->error());
    524     return false;
    525   }
    526   webrtc::Trace::set_level_filter(kDefaultTraceFilter);
    527 
    528   // Save the default AGC configuration settings. This must happen before
    529   // calling ApplyOptions or the default will be overwritten.
    530   if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
    531     LOG_RTCERR0(GetAgcConfig);
    532     return false;
    533   }
    534 
    535   // Print our codec list again for the call diagnostic log
    536   LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
    537   for (const AudioCodec& codec : codecs_) {
    538     LOG(LS_INFO) << ToString(codec);
    539   }
    540 
    541   SetDefaultDevices();
    542 
    543   initialized_ = true;
    544   return true;
    545 }
    546 
    547 void WebRtcVoiceEngine::Terminate() {
    548   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    549   LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
    550   initialized_ = false;
    551 
    552   StopAecDump();
    553 
    554   voe_wrapper_->base()->Terminate();
    555 }
    556 
    557 rtc::scoped_refptr<webrtc::AudioState>
    558     WebRtcVoiceEngine::GetAudioState() const {
    559   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    560   return audio_state_;
    561 }
    562 
    563 VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
    564     const AudioOptions& options) {
    565   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    566   return new WebRtcVoiceMediaChannel(this, options, call);
    567 }
    568 
    569 bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
    570   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    571   LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
    572 
    573   // Default engine options.
    574   AudioOptions options;
    575   options.echo_cancellation = rtc::Optional<bool>(true);
    576   options.auto_gain_control = rtc::Optional<bool>(true);
    577   options.noise_suppression = rtc::Optional<bool>(true);
    578   options.highpass_filter = rtc::Optional<bool>(true);
    579   options.stereo_swapping = rtc::Optional<bool>(false);
    580   options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
    581   options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
    582   options.typing_detection = rtc::Optional<bool>(true);
    583   options.adjust_agc_delta = rtc::Optional<int>(0);
    584   options.experimental_agc = rtc::Optional<bool>(false);
    585   options.extended_filter_aec = rtc::Optional<bool>(false);
    586   options.delay_agnostic_aec = rtc::Optional<bool>(false);
    587   options.experimental_ns = rtc::Optional<bool>(false);
    588   options.aec_dump = rtc::Optional<bool>(false);
    589 
    590   // Apply any given options on top.
    591   options.SetAll(options_in);
    592 
    593   // kEcConference is AEC with high suppression.
    594   webrtc::EcModes ec_mode = webrtc::kEcConference;
    595   webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
    596   webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
    597   webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
    598   if (options.aecm_generate_comfort_noise) {
    599     LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
    600                     << *options.aecm_generate_comfort_noise
    601                     << " (default is false).";
    602   }
    603 
    604 #if defined(WEBRTC_IOS)
    605   // On iOS, VPIO provides built-in EC and AGC.
    606   options.echo_cancellation = rtc::Optional<bool>(false);
    607   options.auto_gain_control = rtc::Optional<bool>(false);
    608   LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
    609 #elif defined(ANDROID)
    610   ec_mode = webrtc::kEcAecm;
    611 #endif
    612 
    613 #if defined(WEBRTC_IOS) || defined(ANDROID)
    614   // Set the AGC mode for iOS as well despite disabling it above, to avoid
    615   // unsupported configuration errors from webrtc.
    616   agc_mode = webrtc::kAgcFixedDigital;
    617   options.typing_detection = rtc::Optional<bool>(false);
    618   options.experimental_agc = rtc::Optional<bool>(false);
    619   options.extended_filter_aec = rtc::Optional<bool>(false);
    620   options.experimental_ns = rtc::Optional<bool>(false);
    621 #endif
    622 
    623   // Delay Agnostic AEC automatically turns on EC if not set except on iOS
    624   // where the feature is not supported.
    625   bool use_delay_agnostic_aec = false;
    626 #if !defined(WEBRTC_IOS)
    627   if (options.delay_agnostic_aec) {
    628     use_delay_agnostic_aec = *options.delay_agnostic_aec;
    629     if (use_delay_agnostic_aec) {
    630       options.echo_cancellation = rtc::Optional<bool>(true);
    631       options.extended_filter_aec = rtc::Optional<bool>(true);
    632       ec_mode = webrtc::kEcConference;
    633     }
    634   }
    635 #endif
    636 
    637   webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
    638 
    639   if (options.echo_cancellation) {
    640     // Check if platform supports built-in EC. Currently only supported on
    641     // Android and in combination with Java based audio layer.
    642     // TODO(henrika): investigate possibility to support built-in EC also
    643     // in combination with Open SL ES audio.
    644     const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
    645     if (built_in_aec) {
    646       // Built-in EC exists on this device and use_delay_agnostic_aec is not
    647       // overriding it. Enable/Disable it according to the echo_cancellation
    648       // audio option.
    649       const bool enable_built_in_aec =
    650           *options.echo_cancellation && !use_delay_agnostic_aec;
    651       if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
    652           enable_built_in_aec) {
    653         // Disable internal software EC if built-in EC is enabled,
    654         // i.e., replace the software EC with the built-in EC.
    655         options.echo_cancellation = rtc::Optional<bool>(false);
    656         LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
    657       }
    658     }
    659     if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
    660       LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
    661       return false;
    662     } else {
    663       LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
    664                    << " with mode " << ec_mode;
    665     }
    666 #if !defined(ANDROID)
    667     // TODO(ajm): Remove the error return on Android from webrtc.
    668     if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
    669       LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
    670       return false;
    671     }
    672 #endif
    673     if (ec_mode == webrtc::kEcAecm) {
    674       bool cn = options.aecm_generate_comfort_noise.value_or(false);
    675       if (voep->SetAecmMode(aecm_mode, cn) != 0) {
    676         LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
    677         return false;
    678       }
    679     }
    680   }
    681 
    682   if (options.auto_gain_control) {
    683     const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
    684     if (built_in_agc) {
    685       if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
    686               0 &&
    687           *options.auto_gain_control) {
    688         // Disable internal software AGC if built-in AGC is enabled,
    689         // i.e., replace the software AGC with the built-in AGC.
    690         options.auto_gain_control = rtc::Optional<bool>(false);
    691         LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
    692       }
    693     }
    694     if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
    695       LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
    696       return false;
    697     } else {
    698       LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
    699                    << " with mode " << agc_mode;
    700     }
    701   }
    702 
    703   if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
    704       options.tx_agc_limiter) {
    705     // Override default_agc_config_. Generally, an unset option means "leave
    706     // the VoE bits alone" in this function, so we want whatever is set to be
    707     // stored as the new "default". If we didn't, then setting e.g.
    708     // tx_agc_target_dbov would reset digital compression gain and limiter
    709     // settings.
    710     // Also, if we don't update default_agc_config_, then adjust_agc_delta
    711     // would be an offset from the original values, and not whatever was set
    712     // explicitly.
    713     default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
    714         default_agc_config_.targetLeveldBOv);
    715     default_agc_config_.digitalCompressionGaindB =
    716         options.tx_agc_digital_compression_gain.value_or(
    717             default_agc_config_.digitalCompressionGaindB);
    718     default_agc_config_.limiterEnable =
    719         options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
    720     if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
    721       LOG_RTCERR3(SetAgcConfig,
    722                   default_agc_config_.targetLeveldBOv,
    723                   default_agc_config_.digitalCompressionGaindB,
    724                   default_agc_config_.limiterEnable);
    725       return false;
    726     }
    727   }
    728 
    729   if (options.noise_suppression) {
    730     const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
    731     if (built_in_ns) {
    732       if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
    733               0 &&
    734           *options.noise_suppression) {
    735         // Disable internal software NS if built-in NS is enabled,
    736         // i.e., replace the software NS with the built-in NS.
    737         options.noise_suppression = rtc::Optional<bool>(false);
    738         LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
    739       }
    740     }
    741     if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
    742       LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
    743       return false;
    744     } else {
    745       LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
    746                    << " with mode " << ns_mode;
    747     }
    748   }
    749 
    750   if (options.highpass_filter) {
    751     LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
    752     if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
    753       LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
    754       return false;
    755     }
    756   }
    757 
    758   if (options.stereo_swapping) {
    759     LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
    760     voep->EnableStereoChannelSwapping(*options.stereo_swapping);
    761     if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
    762       LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
    763       return false;
    764     }
    765   }
    766 
    767   if (options.audio_jitter_buffer_max_packets) {
    768     LOG(LS_INFO) << "NetEq capacity is "
    769                  << *options.audio_jitter_buffer_max_packets;
    770     voe_config_.Set<webrtc::NetEqCapacityConfig>(
    771         new webrtc::NetEqCapacityConfig(
    772             *options.audio_jitter_buffer_max_packets));
    773   }
    774 
    775   if (options.audio_jitter_buffer_fast_accelerate) {
    776     LOG(LS_INFO) << "NetEq fast mode? "
    777                  << *options.audio_jitter_buffer_fast_accelerate;
    778     voe_config_.Set<webrtc::NetEqFastAccelerate>(
    779         new webrtc::NetEqFastAccelerate(
    780             *options.audio_jitter_buffer_fast_accelerate));
    781   }
    782 
    783   if (options.typing_detection) {
    784     LOG(LS_INFO) << "Typing detection is enabled? "
    785                  << *options.typing_detection;
    786     if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
    787       // In case of error, log the info and continue
    788       LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
    789     }
    790   }
    791 
    792   if (options.adjust_agc_delta) {
    793     LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
    794     if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
    795       return false;
    796     }
    797   }
    798 
    799   if (options.aec_dump) {
    800     LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
    801     if (*options.aec_dump)
    802       StartAecDump(kAecDumpByAudioOptionFilename);
    803     else
    804       StopAecDump();
    805   }
    806 
    807   webrtc::Config config;
    808 
    809   if (options.delay_agnostic_aec)
    810     delay_agnostic_aec_ = options.delay_agnostic_aec;
    811   if (delay_agnostic_aec_) {
    812     LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
    813     config.Set<webrtc::DelayAgnostic>(
    814         new webrtc::DelayAgnostic(*delay_agnostic_aec_));
    815   }
    816 
    817   if (options.extended_filter_aec) {
    818     extended_filter_aec_ = options.extended_filter_aec;
    819   }
    820   if (extended_filter_aec_) {
    821     LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
    822     config.Set<webrtc::ExtendedFilter>(
    823         new webrtc::ExtendedFilter(*extended_filter_aec_));
    824   }
    825 
    826   if (options.experimental_ns) {
    827     experimental_ns_ = options.experimental_ns;
    828   }
    829   if (experimental_ns_) {
    830     LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
    831     config.Set<webrtc::ExperimentalNs>(
    832         new webrtc::ExperimentalNs(*experimental_ns_));
    833   }
    834 
    835   // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
    836   // returns NULL on audio_processing().
    837   webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
    838   if (audioproc) {
    839     audioproc->SetExtraOptions(config);
    840   }
    841 
    842   if (options.recording_sample_rate) {
    843     LOG(LS_INFO) << "Recording sample rate is "
    844                  << *options.recording_sample_rate;
    845     if (voe_wrapper_->hw()->SetRecordingSampleRate(
    846             *options.recording_sample_rate)) {
    847       LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
    848     }
    849   }
    850 
    851   if (options.playout_sample_rate) {
    852     LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
    853     if (voe_wrapper_->hw()->SetPlayoutSampleRate(
    854             *options.playout_sample_rate)) {
    855       LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
    856     }
    857   }
    858 
    859   return true;
    860 }
    861 
    862 void WebRtcVoiceEngine::SetDefaultDevices() {
    863   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    864 #if !defined(WEBRTC_IOS)
    865   int in_id = kDefaultAudioDeviceId;
    866   int out_id = kDefaultAudioDeviceId;
    867   LOG(LS_INFO) << "Setting microphone to (id=" << in_id
    868                << ") and speaker to (id=" << out_id << ")";
    869 
    870   bool ret = true;
    871   if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
    872     LOG_RTCERR1(SetRecordingDevice, in_id);
    873     ret = false;
    874   }
    875   webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
    876   if (ap) {
    877     ap->Initialize();
    878   }
    879 
    880   if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
    881     LOG_RTCERR1(SetPlayoutDevice, out_id);
    882     ret = false;
    883   }
    884 
    885   if (ret) {
    886     LOG(LS_INFO) << "Set microphone to (id=" << in_id
    887                  << ") and speaker to (id=" << out_id << ")";
    888   }
    889 #endif  // !WEBRTC_IOS
    890 }
    891 
    892 bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
    893   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    894   unsigned int ulevel;
    895   if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
    896     LOG_RTCERR1(GetSpeakerVolume, level);
    897     return false;
    898   }
    899   *level = ulevel;
    900   return true;
    901 }
    902 
    903 bool WebRtcVoiceEngine::SetOutputVolume(int level) {
    904   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    905   RTC_DCHECK(level >= 0 && level <= 255);
    906   if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
    907     LOG_RTCERR1(SetSpeakerVolume, level);
    908     return false;
    909   }
    910   return true;
    911 }
    912 
    913 int WebRtcVoiceEngine::GetInputLevel() {
    914   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    915   unsigned int ulevel;
    916   return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
    917       static_cast<int>(ulevel) : -1;
    918 }
    919 
    920 const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
    921   RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
    922   return codecs_;
    923 }
    924 
    925 RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
    926   RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
    927   RtpCapabilities capabilities;
    928   capabilities.header_extensions.push_back(RtpHeaderExtension(
    929       kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
    930   capabilities.header_extensions.push_back(
    931       RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
    932                          kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
    933   return capabilities;
    934 }
    935 
    936 int WebRtcVoiceEngine::GetLastEngineError() {
    937   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    938   return voe_wrapper_->error();
    939 }
    940 
    941 void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
    942                               int length) {
    943   // Note: This callback can happen on any thread!
    944   rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
    945   if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
    946     sev = rtc::LS_ERROR;
    947   else if (level == webrtc::kTraceWarning)
    948     sev = rtc::LS_WARNING;
    949   else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
    950     sev = rtc::LS_INFO;
    951   else if (level == webrtc::kTraceTerseInfo)
    952     sev = rtc::LS_INFO;
    953 
    954   // Skip past boilerplate prefix text
    955   if (length < 72) {
    956     std::string msg(trace, length);
    957     LOG(LS_ERROR) << "Malformed webrtc log message: ";
    958     LOG_V(sev) << msg;
    959   } else {
    960     std::string msg(trace + 71, length - 72);
    961     LOG_V(sev) << "webrtc: " << msg;
    962   }
    963 }
    964 
    965 void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
    966   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    967   RTC_DCHECK(channel);
    968   channels_.push_back(channel);
    969 }
    970 
    971 void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
    972   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    973   auto it = std::find(channels_.begin(), channels_.end(), channel);
    974   RTC_DCHECK(it != channels_.end());
    975   channels_.erase(it);
    976 }
    977 
    978 // Adjusts the default AGC target level by the specified delta.
    979 // NB: If we start messing with other config fields, we'll want
    980 // to save the current webrtc::AgcConfig as well.
    981 bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
    982   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    983   webrtc::AgcConfig config = default_agc_config_;
    984   config.targetLeveldBOv -= delta;
    985 
    986   LOG(LS_INFO) << "Adjusting AGC level from default -"
    987                << default_agc_config_.targetLeveldBOv << "dB to -"
    988                << config.targetLeveldBOv << "dB";
    989 
    990   if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
    991     LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
    992     return false;
    993   }
    994   return true;
    995 }
    996 
    997 bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
    998   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
    999   if (initialized_) {
   1000     LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
   1001     return false;
   1002   }
   1003   if (adm_) {
   1004     adm_->Release();
   1005     adm_ = NULL;
   1006   }
   1007   if (adm) {
   1008     adm_ = adm;
   1009     adm_->AddRef();
   1010   }
   1011   return true;
   1012 }
   1013 
   1014 bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
   1015   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1016   FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
   1017   if (!aec_dump_file_stream) {
   1018     LOG(LS_ERROR) << "Could not open AEC dump file stream.";
   1019     if (!rtc::ClosePlatformFile(file))
   1020       LOG(LS_WARNING) << "Could not close file.";
   1021     return false;
   1022   }
   1023   StopAecDump();
   1024   if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
   1025       webrtc::AudioProcessing::kNoError) {
   1026     LOG_RTCERR0(StartDebugRecording);
   1027     fclose(aec_dump_file_stream);
   1028     return false;
   1029   }
   1030   is_dumping_aec_ = true;
   1031   return true;
   1032 }
   1033 
   1034 void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
   1035   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1036   if (!is_dumping_aec_) {
   1037     // Start dumping AEC when we are not dumping.
   1038     if (voe_wrapper_->processing()->StartDebugRecording(
   1039         filename.c_str()) != webrtc::AudioProcessing::kNoError) {
   1040       LOG_RTCERR1(StartDebugRecording, filename.c_str());
   1041     } else {
   1042       is_dumping_aec_ = true;
   1043     }
   1044   }
   1045 }
   1046 
   1047 void WebRtcVoiceEngine::StopAecDump() {
   1048   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1049   if (is_dumping_aec_) {
   1050     // Stop dumping AEC when we are dumping.
   1051     if (voe_wrapper_->processing()->StopDebugRecording() !=
   1052         webrtc::AudioProcessing::kNoError) {
   1053       LOG_RTCERR0(StopDebugRecording);
   1054     }
   1055     is_dumping_aec_ = false;
   1056   }
   1057 }
   1058 
   1059 bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
   1060   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1061   return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
   1062 }
   1063 
   1064 void WebRtcVoiceEngine::StopRtcEventLog() {
   1065   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1066   voe_wrapper_->codec()->GetEventLog()->StopLogging();
   1067 }
   1068 
   1069 int WebRtcVoiceEngine::CreateVoEChannel() {
   1070   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1071   return voe_wrapper_->base()->CreateChannel(voe_config_);
   1072 }
   1073 
   1074 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
   1075     : public AudioRenderer::Sink {
   1076  public:
   1077   WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
   1078                         uint32_t ssrc, const std::string& c_name,
   1079                         const std::vector<webrtc::RtpExtension>& extensions,
   1080                         webrtc::Call* call)
   1081       : voe_audio_transport_(voe_audio_transport),
   1082         call_(call),
   1083         config_(nullptr) {
   1084     RTC_DCHECK_GE(ch, 0);
   1085     // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
   1086     // RTC_DCHECK(voe_audio_transport);
   1087     RTC_DCHECK(call);
   1088     audio_capture_thread_checker_.DetachFromThread();
   1089     config_.rtp.ssrc = ssrc;
   1090     config_.rtp.c_name = c_name;
   1091     config_.voe_channel_id = ch;
   1092     RecreateAudioSendStream(extensions);
   1093   }
   1094 
   1095   ~WebRtcAudioSendStream() override {
   1096     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1097     Stop();
   1098     call_->DestroyAudioSendStream(stream_);
   1099   }
   1100 
   1101   void RecreateAudioSendStream(
   1102       const std::vector<webrtc::RtpExtension>& extensions) {
   1103     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1104     if (stream_) {
   1105       call_->DestroyAudioSendStream(stream_);
   1106       stream_ = nullptr;
   1107     }
   1108     config_.rtp.extensions = extensions;
   1109     RTC_DCHECK(!stream_);
   1110     stream_ = call_->CreateAudioSendStream(config_);
   1111     RTC_CHECK(stream_);
   1112   }
   1113 
   1114   bool SendTelephoneEvent(int payload_type, uint8_t event,
   1115                           uint32_t duration_ms) {
   1116     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1117     RTC_DCHECK(stream_);
   1118     return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
   1119   }
   1120 
   1121   webrtc::AudioSendStream::Stats GetStats() const {
   1122     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1123     RTC_DCHECK(stream_);
   1124     return stream_->GetStats();
   1125   }
   1126 
   1127   // Starts the rendering by setting a sink to the renderer to get data
   1128   // callback.
   1129   // This method is called on the libjingle worker thread.
   1130   // TODO(xians): Make sure Start() is called only once.
   1131   void Start(AudioRenderer* renderer) {
   1132     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1133     RTC_DCHECK(renderer);
   1134     if (renderer_) {
   1135       RTC_DCHECK(renderer_ == renderer);
   1136       return;
   1137     }
   1138     renderer->SetSink(this);
   1139     renderer_ = renderer;
   1140   }
   1141 
   1142   // Stops rendering by setting the sink of the renderer to nullptr. No data
   1143   // callback will be received after this method.
   1144   // This method is called on the libjingle worker thread.
   1145   void Stop() {
   1146     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1147     if (renderer_) {
   1148       renderer_->SetSink(nullptr);
   1149       renderer_ = nullptr;
   1150     }
   1151   }
   1152 
   1153   // AudioRenderer::Sink implementation.
   1154   // This method is called on the audio thread.
   1155   void OnData(const void* audio_data,
   1156               int bits_per_sample,
   1157               int sample_rate,
   1158               size_t number_of_channels,
   1159               size_t number_of_frames) override {
   1160     RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
   1161     RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
   1162     RTC_DCHECK(voe_audio_transport_);
   1163     voe_audio_transport_->OnData(config_.voe_channel_id,
   1164                                  audio_data,
   1165                                  bits_per_sample,
   1166                                  sample_rate,
   1167                                  number_of_channels,
   1168                                  number_of_frames);
   1169   }
   1170 
   1171   // Callback from the |renderer_| when it is going away. In case Start() has
   1172   // never been called, this callback won't be triggered.
   1173   void OnClose() override {
   1174     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1175     // Set |renderer_| to nullptr to make sure no more callback will get into
   1176     // the renderer.
   1177     renderer_ = nullptr;
   1178   }
   1179 
   1180   // Accessor to the VoE channel ID.
   1181   int channel() const {
   1182     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1183     return config_.voe_channel_id;
   1184   }
   1185 
   1186  private:
   1187   rtc::ThreadChecker worker_thread_checker_;
   1188   rtc::ThreadChecker audio_capture_thread_checker_;
   1189   webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
   1190   webrtc::Call* call_ = nullptr;
   1191   webrtc::AudioSendStream::Config config_;
   1192   // The stream is owned by WebRtcAudioSendStream and may be reallocated if
   1193   // configuration changes.
   1194   webrtc::AudioSendStream* stream_ = nullptr;
   1195 
   1196   // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
   1197   // PeerConnection will make sure invalidating the pointer before the object
   1198   // goes away.
   1199   AudioRenderer* renderer_ = nullptr;
   1200 
   1201   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
   1202 };
   1203 
   1204 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
   1205  public:
   1206   WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
   1207                            bool use_combined_bwe, const std::string& sync_group,
   1208                            const std::vector<webrtc::RtpExtension>& extensions,
   1209                            webrtc::Call* call)
   1210       : call_(call),
   1211         config_() {
   1212     RTC_DCHECK_GE(ch, 0);
   1213     RTC_DCHECK(call);
   1214     config_.rtp.remote_ssrc = remote_ssrc;
   1215     config_.rtp.local_ssrc = local_ssrc;
   1216     config_.voe_channel_id = ch;
   1217     config_.sync_group = sync_group;
   1218     RecreateAudioReceiveStream(use_combined_bwe, extensions);
   1219   }
   1220 
   1221   ~WebRtcAudioReceiveStream() {
   1222     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1223     call_->DestroyAudioReceiveStream(stream_);
   1224   }
   1225 
   1226   void RecreateAudioReceiveStream(
   1227       const std::vector<webrtc::RtpExtension>& extensions) {
   1228     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1229     RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
   1230   }
   1231   void RecreateAudioReceiveStream(bool use_combined_bwe) {
   1232     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1233     RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
   1234   }
   1235 
   1236   webrtc::AudioReceiveStream::Stats GetStats() const {
   1237     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1238     RTC_DCHECK(stream_);
   1239     return stream_->GetStats();
   1240   }
   1241 
   1242   int channel() const {
   1243     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1244     return config_.voe_channel_id;
   1245   }
   1246 
   1247   void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
   1248     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1249     stream_->SetSink(std::move(sink));
   1250   }
   1251 
   1252  private:
   1253   void RecreateAudioReceiveStream(bool use_combined_bwe,
   1254       const std::vector<webrtc::RtpExtension>& extensions) {
   1255     RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1256     if (stream_) {
   1257       call_->DestroyAudioReceiveStream(stream_);
   1258       stream_ = nullptr;
   1259     }
   1260     config_.rtp.extensions = extensions;
   1261     config_.combined_audio_video_bwe = use_combined_bwe;
   1262     RTC_DCHECK(!stream_);
   1263     stream_ = call_->CreateAudioReceiveStream(config_);
   1264     RTC_CHECK(stream_);
   1265   }
   1266 
   1267   rtc::ThreadChecker worker_thread_checker_;
   1268   webrtc::Call* call_ = nullptr;
   1269   webrtc::AudioReceiveStream::Config config_;
   1270   // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
   1271   // configuration changes.
   1272   webrtc::AudioReceiveStream* stream_ = nullptr;
   1273 
   1274   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
   1275 };
   1276 
   1277 WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
   1278                                                  const AudioOptions& options,
   1279                                                  webrtc::Call* call)
   1280     : engine_(engine), call_(call) {
   1281   LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
   1282   RTC_DCHECK(call);
   1283   engine->RegisterChannel(this);
   1284   SetOptions(options);
   1285 }
   1286 
   1287 WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
   1288   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1289   LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
   1290   // TODO(solenberg): Should be able to delete the streams directly, without
   1291   //                  going through RemoveNnStream(), once stream objects handle
   1292   //                  all (de)configuration.
   1293   while (!send_streams_.empty()) {
   1294     RemoveSendStream(send_streams_.begin()->first);
   1295   }
   1296   while (!recv_streams_.empty()) {
   1297     RemoveRecvStream(recv_streams_.begin()->first);
   1298   }
   1299   engine()->UnregisterChannel(this);
   1300 }
   1301 
   1302 bool WebRtcVoiceMediaChannel::SetSendParameters(
   1303     const AudioSendParameters& params) {
   1304   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1305   LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
   1306                << params.ToString();
   1307   // TODO(pthatcher): Refactor this to be more clean now that we have
   1308   // all the information at once.
   1309 
   1310   if (!SetSendCodecs(params.codecs)) {
   1311     return false;
   1312   }
   1313 
   1314   if (!ValidateRtpExtensions(params.extensions)) {
   1315     return false;
   1316   }
   1317   std::vector<webrtc::RtpExtension> filtered_extensions =
   1318       FilterRtpExtensions(params.extensions,
   1319                           webrtc::RtpExtension::IsSupportedForAudio, true);
   1320   if (send_rtp_extensions_ != filtered_extensions) {
   1321     send_rtp_extensions_.swap(filtered_extensions);
   1322     for (auto& it : send_streams_) {
   1323       it.second->RecreateAudioSendStream(send_rtp_extensions_);
   1324     }
   1325   }
   1326 
   1327   if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
   1328     return false;
   1329   }
   1330   return SetOptions(params.options);
   1331 }
   1332 
   1333 bool WebRtcVoiceMediaChannel::SetRecvParameters(
   1334     const AudioRecvParameters& params) {
   1335   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1336   LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
   1337                << params.ToString();
   1338   // TODO(pthatcher): Refactor this to be more clean now that we have
   1339   // all the information at once.
   1340 
   1341   if (!SetRecvCodecs(params.codecs)) {
   1342     return false;
   1343   }
   1344 
   1345   if (!ValidateRtpExtensions(params.extensions)) {
   1346     return false;
   1347   }
   1348   std::vector<webrtc::RtpExtension> filtered_extensions =
   1349       FilterRtpExtensions(params.extensions,
   1350                           webrtc::RtpExtension::IsSupportedForAudio, false);
   1351   if (recv_rtp_extensions_ != filtered_extensions) {
   1352     recv_rtp_extensions_.swap(filtered_extensions);
   1353     for (auto& it : recv_streams_) {
   1354       it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
   1355     }
   1356   }
   1357 
   1358   return true;
   1359 }
   1360 
   1361 bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
   1362   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1363   LOG(LS_INFO) << "Setting voice channel options: "
   1364                << options.ToString();
   1365 
   1366   // Check if DSCP value is changed from previous.
   1367   bool dscp_option_changed = (options_.dscp != options.dscp);
   1368 
   1369   // We retain all of the existing options, and apply the given ones
   1370   // on top.  This means there is no way to "clear" options such that
   1371   // they go back to the engine default.
   1372   options_.SetAll(options);
   1373   if (!engine()->ApplyOptions(options_)) {
   1374     LOG(LS_WARNING) <<
   1375         "Failed to apply engine options during channel SetOptions.";
   1376     return false;
   1377   }
   1378 
   1379   if (dscp_option_changed) {
   1380     rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
   1381     if (options_.dscp.value_or(false)) {
   1382       dscp = kAudioDscpValue;
   1383     }
   1384     if (MediaChannel::SetDscp(dscp) != 0) {
   1385       LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
   1386     }
   1387   }
   1388 
   1389   // TODO(solenberg): Don't recreate unless options changed.
   1390   for (auto& it : recv_streams_) {
   1391     it.second->RecreateAudioReceiveStream(
   1392         options_.combined_audio_video_bwe.value_or(false));
   1393   }
   1394 
   1395   LOG(LS_INFO) << "Set voice channel options.  Current options: "
   1396                << options_.ToString();
   1397   return true;
   1398 }
   1399 
   1400 bool WebRtcVoiceMediaChannel::SetRecvCodecs(
   1401     const std::vector<AudioCodec>& codecs) {
   1402   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1403 
   1404   // Set the payload types to be used for incoming media.
   1405   LOG(LS_INFO) << "Setting receive voice codecs.";
   1406 
   1407   if (!VerifyUniquePayloadTypes(codecs)) {
   1408     LOG(LS_ERROR) << "Codec payload types overlap.";
   1409     return false;
   1410   }
   1411 
   1412   std::vector<AudioCodec> new_codecs;
   1413   // Find all new codecs. We allow adding new codecs but don't allow changing
   1414   // the payload type of codecs that is already configured since we might
   1415   // already be receiving packets with that payload type.
   1416   for (const AudioCodec& codec : codecs) {
   1417     AudioCodec old_codec;
   1418     if (FindCodec(recv_codecs_, codec, &old_codec)) {
   1419       if (old_codec.id != codec.id) {
   1420         LOG(LS_ERROR) << codec.name << " payload type changed.";
   1421         return false;
   1422       }
   1423     } else {
   1424       new_codecs.push_back(codec);
   1425     }
   1426   }
   1427   if (new_codecs.empty()) {
   1428     // There are no new codecs to configure. Already configured codecs are
   1429     // never removed.
   1430     return true;
   1431   }
   1432 
   1433   if (playout_) {
   1434     // Receive codecs can not be changed while playing. So we temporarily
   1435     // pause playout.
   1436     PausePlayout();
   1437   }
   1438 
   1439   bool result = true;
   1440   for (const AudioCodec& codec : new_codecs) {
   1441     webrtc::CodecInst voe_codec;
   1442     if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
   1443       LOG(LS_INFO) << ToString(codec);
   1444       voe_codec.pltype = codec.id;
   1445       for (const auto& ch : recv_streams_) {
   1446         if (engine()->voe()->codec()->SetRecPayloadType(
   1447                 ch.second->channel(), voe_codec) == -1) {
   1448           LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
   1449                       ToString(voe_codec));
   1450           result = false;
   1451         }
   1452       }
   1453     } else {
   1454       LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
   1455       result = false;
   1456       break;
   1457     }
   1458   }
   1459   if (result) {
   1460     recv_codecs_ = codecs;
   1461   }
   1462 
   1463   if (desired_playout_ && !playout_) {
   1464     ResumePlayout();
   1465   }
   1466   return result;
   1467 }
   1468 
   1469 bool WebRtcVoiceMediaChannel::SetSendCodecs(
   1470     int channel, const std::vector<AudioCodec>& codecs) {
   1471   // Disable VAD, FEC, and RED unless we know the other side wants them.
   1472   engine()->voe()->codec()->SetVADStatus(channel, false);
   1473   engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
   1474   engine()->voe()->rtp()->SetREDStatus(channel, false);
   1475   engine()->voe()->codec()->SetFECStatus(channel, false);
   1476 
   1477   // Scan through the list to figure out the codec to use for sending, along
   1478   // with the proper configuration for VAD.
   1479   bool found_send_codec = false;
   1480   webrtc::CodecInst send_codec;
   1481   memset(&send_codec, 0, sizeof(send_codec));
   1482 
   1483   bool nack_enabled = nack_enabled_;
   1484   bool enable_codec_fec = false;
   1485   bool enable_opus_dtx = false;
   1486   int opus_max_playback_rate = 0;
   1487 
   1488   // Set send codec (the first non-telephone-event/CN codec)
   1489   for (const AudioCodec& codec : codecs) {
   1490     // Ignore codecs we don't know about. The negotiation step should prevent
   1491     // this, but double-check to be sure.
   1492     webrtc::CodecInst voe_codec;
   1493     if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
   1494       LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
   1495       continue;
   1496     }
   1497 
   1498     if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
   1499       // Skip telephone-event/CN codec, which will be handled later.
   1500       continue;
   1501     }
   1502 
   1503     // We'll use the first codec in the list to actually send audio data.
   1504     // Be sure to use the payload type requested by the remote side.
   1505     // "red", for RED audio, is a special case where the actual codec to be
   1506     // used is specified in params.
   1507     if (IsCodec(codec, kRedCodecName)) {
   1508       // Parse out the RED parameters. If we fail, just ignore RED;
   1509       // we don't support all possible params/usage scenarios.
   1510       if (!GetRedSendCodec(codec, codecs, &send_codec)) {
   1511         continue;
   1512       }
   1513 
   1514       // Enable redundant encoding of the specified codec. Treat any
   1515       // failure as a fatal internal error.
   1516       LOG(LS_INFO) << "Enabling RED on channel " << channel;
   1517       if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
   1518         LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
   1519         return false;
   1520       }
   1521     } else {
   1522       send_codec = voe_codec;
   1523       nack_enabled = IsNackEnabled(codec);
   1524       // For Opus as the send codec, we are to determine inband FEC, maximum
   1525       // playback rate, and opus internal dtx.
   1526       if (IsCodec(codec, kOpusCodecName)) {
   1527         GetOpusConfig(codec, &send_codec, &enable_codec_fec,
   1528                       &opus_max_playback_rate, &enable_opus_dtx);
   1529       }
   1530 
   1531       // Set packet size if the AudioCodec param kCodecParamPTime is set.
   1532       int ptime_ms = 0;
   1533       if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
   1534         if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
   1535           LOG(LS_WARNING) << "Failed to set packet size for codec "
   1536                           << send_codec.plname;
   1537           return false;
   1538         }
   1539       }
   1540     }
   1541     found_send_codec = true;
   1542     break;
   1543   }
   1544 
   1545   if (nack_enabled_ != nack_enabled) {
   1546     SetNack(channel, nack_enabled);
   1547     nack_enabled_ = nack_enabled;
   1548   }
   1549 
   1550   if (!found_send_codec) {
   1551     LOG(LS_WARNING) << "Received empty list of codecs.";
   1552     return false;
   1553   }
   1554 
   1555   // Set the codec immediately, since SetVADStatus() depends on whether
   1556   // the current codec is mono or stereo.
   1557   if (!SetSendCodec(channel, send_codec))
   1558     return false;
   1559 
   1560   // FEC should be enabled after SetSendCodec.
   1561   if (enable_codec_fec) {
   1562     LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
   1563                  << channel;
   1564     if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
   1565       // Enable codec internal FEC. Treat any failure as fatal internal error.
   1566       LOG_RTCERR2(SetFECStatus, channel, true);
   1567       return false;
   1568     }
   1569   }
   1570 
   1571   if (IsCodec(send_codec, kOpusCodecName)) {
   1572     // DTX and maxplaybackrate should be set after SetSendCodec. Because current
   1573     // send codec has to be Opus.
   1574 
   1575     // Set Opus internal DTX.
   1576     LOG(LS_INFO) << "Attempt to "
   1577                  << (enable_opus_dtx ? "enable" : "disable")
   1578                  << " Opus DTX on channel "
   1579                  << channel;
   1580     if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
   1581       LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
   1582       return false;
   1583     }
   1584 
   1585     // If opus_max_playback_rate <= 0, the default maximum playback rate
   1586     // (48 kHz) will be used.
   1587     if (opus_max_playback_rate > 0) {
   1588       LOG(LS_INFO) << "Attempt to set maximum playback rate to "
   1589                    << opus_max_playback_rate
   1590                    << " Hz on channel "
   1591                    << channel;
   1592       if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
   1593           channel, opus_max_playback_rate) == -1) {
   1594         LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
   1595         return false;
   1596       }
   1597     }
   1598   }
   1599 
   1600   // Always update the |send_codec_| to the currently set send codec.
   1601   send_codec_.reset(new webrtc::CodecInst(send_codec));
   1602 
   1603   if (send_bitrate_setting_) {
   1604     SetSendBitrateInternal(send_bitrate_bps_);
   1605   }
   1606 
   1607   // Loop through the codecs list again to config the CN codec.
   1608   for (const AudioCodec& codec : codecs) {
   1609     // Ignore codecs we don't know about. The negotiation step should prevent
   1610     // this, but double-check to be sure.
   1611     webrtc::CodecInst voe_codec;
   1612     if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
   1613       LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
   1614       continue;
   1615     }
   1616 
   1617     if (IsCodec(codec, kCnCodecName)) {
   1618       // Turn voice activity detection/comfort noise on if supported.
   1619       // Set the wideband CN payload type appropriately.
   1620       // (narrowband always uses the static payload type 13).
   1621       webrtc::PayloadFrequencies cn_freq;
   1622       switch (codec.clockrate) {
   1623         case 8000:
   1624           cn_freq = webrtc::kFreq8000Hz;
   1625           break;
   1626         case 16000:
   1627           cn_freq = webrtc::kFreq16000Hz;
   1628           break;
   1629         case 32000:
   1630           cn_freq = webrtc::kFreq32000Hz;
   1631           break;
   1632         default:
   1633           LOG(LS_WARNING) << "CN frequency " << codec.clockrate
   1634                           << " not supported.";
   1635           continue;
   1636       }
   1637       // Set the CN payloadtype and the VAD status.
   1638       // The CN payload type for 8000 Hz clockrate is fixed at 13.
   1639       if (cn_freq != webrtc::kFreq8000Hz) {
   1640         if (engine()->voe()->codec()->SetSendCNPayloadType(
   1641                 channel, codec.id, cn_freq) == -1) {
   1642           LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
   1643           // TODO(ajm): This failure condition will be removed from VoE.
   1644           // Restore the return here when we update to a new enough webrtc.
   1645           //
   1646           // Not returning false because the SetSendCNPayloadType will fail if
   1647           // the channel is already sending.
   1648           // This can happen if the remote description is applied twice, for
   1649           // example in the case of ROAP on top of JSEP, where both side will
   1650           // send the offer.
   1651         }
   1652       }
   1653       // Only turn on VAD if we have a CN payload type that matches the
   1654       // clockrate for the codec we are going to use.
   1655       if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
   1656         // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
   1657         // interaction between VAD and Opus FEC.
   1658         LOG(LS_INFO) << "Enabling VAD";
   1659         if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
   1660           LOG_RTCERR2(SetVADStatus, channel, true);
   1661           return false;
   1662         }
   1663       }
   1664     }
   1665   }
   1666   return true;
   1667 }
   1668 
   1669 bool WebRtcVoiceMediaChannel::SetSendCodecs(
   1670     const std::vector<AudioCodec>& codecs) {
   1671   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1672   // TODO(solenberg): Validate input - that payload types don't overlap, are
   1673   //                  within range, filter out codecs we don't support,
   1674   //                  redundant codecs etc.
   1675 
   1676   // Find the DTMF telephone event "codec" payload type.
   1677   dtmf_payload_type_ = rtc::Optional<int>();
   1678   for (const AudioCodec& codec : codecs) {
   1679     if (IsCodec(codec, kDtmfCodecName)) {
   1680       dtmf_payload_type_ = rtc::Optional<int>(codec.id);
   1681       break;
   1682     }
   1683   }
   1684 
   1685   // Cache the codecs in order to configure the channel created later.
   1686   send_codecs_ = codecs;
   1687   for (const auto& ch : send_streams_) {
   1688     if (!SetSendCodecs(ch.second->channel(), codecs)) {
   1689       return false;
   1690     }
   1691   }
   1692 
   1693   // Set nack status on receive channels and update |nack_enabled_|.
   1694   for (const auto& ch : recv_streams_) {
   1695     SetNack(ch.second->channel(), nack_enabled_);
   1696   }
   1697 
   1698   return true;
   1699 }
   1700 
   1701 void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
   1702   if (nack_enabled) {
   1703     LOG(LS_INFO) << "Enabling NACK for channel " << channel;
   1704     engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
   1705   } else {
   1706     LOG(LS_INFO) << "Disabling NACK for channel " << channel;
   1707     engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
   1708   }
   1709 }
   1710 
   1711 bool WebRtcVoiceMediaChannel::SetSendCodec(
   1712     int channel, const webrtc::CodecInst& send_codec) {
   1713   LOG(LS_INFO) << "Send channel " << channel <<  " selected voice codec "
   1714                << ToString(send_codec) << ", bitrate=" << send_codec.rate;
   1715 
   1716   webrtc::CodecInst current_codec;
   1717   if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
   1718       (send_codec == current_codec)) {
   1719     // Codec is already configured, we can return without setting it again.
   1720     return true;
   1721   }
   1722 
   1723   if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
   1724     LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
   1725     return false;
   1726   }
   1727   return true;
   1728 }
   1729 
   1730 bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
   1731   desired_playout_ = playout;
   1732   return ChangePlayout(desired_playout_);
   1733 }
   1734 
   1735 bool WebRtcVoiceMediaChannel::PausePlayout() {
   1736   return ChangePlayout(false);
   1737 }
   1738 
   1739 bool WebRtcVoiceMediaChannel::ResumePlayout() {
   1740   return ChangePlayout(desired_playout_);
   1741 }
   1742 
   1743 bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
   1744   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1745   if (playout_ == playout) {
   1746     return true;
   1747   }
   1748 
   1749   for (const auto& ch : recv_streams_) {
   1750     if (!SetPlayout(ch.second->channel(), playout)) {
   1751       LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
   1752                     << ch.second->channel() << " failed";
   1753       return false;
   1754     }
   1755   }
   1756   playout_ = playout;
   1757   return true;
   1758 }
   1759 
   1760 bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
   1761   desired_send_ = send;
   1762   if (!send_streams_.empty()) {
   1763     return ChangeSend(desired_send_);
   1764   }
   1765   return true;
   1766 }
   1767 
   1768 bool WebRtcVoiceMediaChannel::PauseSend() {
   1769   return ChangeSend(SEND_NOTHING);
   1770 }
   1771 
   1772 bool WebRtcVoiceMediaChannel::ResumeSend() {
   1773   return ChangeSend(desired_send_);
   1774 }
   1775 
   1776 bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
   1777   if (send_ == send) {
   1778     return true;
   1779   }
   1780 
   1781   // Apply channel specific options when channel is enabled for sending.
   1782   if (send == SEND_MICROPHONE) {
   1783     engine()->ApplyOptions(options_);
   1784   }
   1785 
   1786   // Change the settings on each send channel.
   1787   for (const auto& ch : send_streams_) {
   1788     if (!ChangeSend(ch.second->channel(), send)) {
   1789       return false;
   1790     }
   1791   }
   1792 
   1793   send_ = send;
   1794   return true;
   1795 }
   1796 
   1797 bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
   1798   if (send == SEND_MICROPHONE) {
   1799     if (engine()->voe()->base()->StartSend(channel) == -1) {
   1800       LOG_RTCERR1(StartSend, channel);
   1801       return false;
   1802     }
   1803   } else {  // SEND_NOTHING
   1804     RTC_DCHECK(send == SEND_NOTHING);
   1805     if (engine()->voe()->base()->StopSend(channel) == -1) {
   1806       LOG_RTCERR1(StopSend, channel);
   1807       return false;
   1808     }
   1809   }
   1810 
   1811   return true;
   1812 }
   1813 
   1814 bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
   1815                                            bool enable,
   1816                                            const AudioOptions* options,
   1817                                            AudioRenderer* renderer) {
   1818   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1819   // TODO(solenberg): The state change should be fully rolled back if any one of
   1820   //                  these calls fail.
   1821   if (!SetLocalRenderer(ssrc, renderer)) {
   1822     return false;
   1823   }
   1824   if (!MuteStream(ssrc, !enable)) {
   1825     return false;
   1826   }
   1827   if (enable && options) {
   1828     return SetOptions(*options);
   1829   }
   1830   return true;
   1831 }
   1832 
   1833 int WebRtcVoiceMediaChannel::CreateVoEChannel() {
   1834   int id = engine()->CreateVoEChannel();
   1835   if (id == -1) {
   1836     LOG_RTCERR0(CreateVoEChannel);
   1837     return -1;
   1838   }
   1839   if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
   1840     LOG_RTCERR2(RegisterExternalTransport, id, this);
   1841     engine()->voe()->base()->DeleteChannel(id);
   1842     return -1;
   1843   }
   1844   return id;
   1845 }
   1846 
   1847 bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
   1848   if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
   1849     LOG_RTCERR1(DeRegisterExternalTransport, channel);
   1850   }
   1851   if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
   1852     LOG_RTCERR1(DeleteChannel, channel);
   1853     return false;
   1854   }
   1855   return true;
   1856 }
   1857 
   1858 bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
   1859   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1860   LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
   1861 
   1862   uint32_t ssrc = sp.first_ssrc();
   1863   RTC_DCHECK(0 != ssrc);
   1864 
   1865   if (GetSendChannelId(ssrc) != -1) {
   1866     LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
   1867     return false;
   1868   }
   1869 
   1870   // Create a new channel for sending audio data.
   1871   int channel = CreateVoEChannel();
   1872   if (channel == -1) {
   1873     return false;
   1874   }
   1875 
   1876   // Save the channel to send_streams_, so that RemoveSendStream() can still
   1877   // delete the channel in case failure happens below.
   1878   webrtc::AudioTransport* audio_transport =
   1879       engine()->voe()->base()->audio_transport();
   1880   send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
   1881       channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
   1882 
   1883   // Set the current codecs to be used for the new channel. We need to do this
   1884   // after adding the channel to send_channels_, because of how max bitrate is
   1885   // currently being configured by SetSendCodec().
   1886   if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
   1887     RemoveSendStream(ssrc);
   1888     return false;
   1889   }
   1890 
   1891   // At this point the channel's local SSRC has been updated. If the channel is
   1892   // the first send channel make sure that all the receive channels are updated
   1893   // with the same SSRC in order to send receiver reports.
   1894   if (send_streams_.size() == 1) {
   1895     receiver_reports_ssrc_ = ssrc;
   1896     for (const auto& stream : recv_streams_) {
   1897       int recv_channel = stream.second->channel();
   1898       if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
   1899         LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
   1900         return false;
   1901       }
   1902       engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
   1903       LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
   1904                    << " is associated with channel #" << channel << ".";
   1905     }
   1906   }
   1907 
   1908   return ChangeSend(channel, desired_send_);
   1909 }
   1910 
   1911 bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
   1912   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1913   LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
   1914 
   1915   auto it = send_streams_.find(ssrc);
   1916   if (it == send_streams_.end()) {
   1917     LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
   1918                     << " which doesn't exist.";
   1919     return false;
   1920   }
   1921 
   1922   int channel = it->second->channel();
   1923   ChangeSend(channel, SEND_NOTHING);
   1924 
   1925   // Clean up and delete the send stream+channel.
   1926   LOG(LS_INFO) << "Removing audio send stream " << ssrc
   1927                << " with VoiceEngine channel #" << channel << ".";
   1928   delete it->second;
   1929   send_streams_.erase(it);
   1930   if (!DeleteVoEChannel(channel)) {
   1931     return false;
   1932   }
   1933   if (send_streams_.empty()) {
   1934     ChangeSend(SEND_NOTHING);
   1935   }
   1936   return true;
   1937 }
   1938 
   1939 bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
   1940   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   1941   LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
   1942 
   1943   if (!ValidateStreamParams(sp)) {
   1944     return false;
   1945   }
   1946 
   1947   const uint32_t ssrc = sp.first_ssrc();
   1948   if (ssrc == 0) {
   1949     LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
   1950     return false;
   1951   }
   1952 
   1953   // Remove the default receive stream if one had been created with this ssrc;
   1954   // we'll recreate it then.
   1955   if (IsDefaultRecvStream(ssrc)) {
   1956     RemoveRecvStream(ssrc);
   1957   }
   1958 
   1959   if (GetReceiveChannelId(ssrc) != -1) {
   1960     LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
   1961     return false;
   1962   }
   1963 
   1964   // Create a new channel for receiving audio data.
   1965   const int channel = CreateVoEChannel();
   1966   if (channel == -1) {
   1967     return false;
   1968   }
   1969 
   1970   // Turn off all supported codecs.
   1971   // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
   1972   for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
   1973     voe_codec.pltype = -1;
   1974     if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
   1975       LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
   1976       DeleteVoEChannel(channel);
   1977       return false;
   1978     }
   1979   }
   1980 
   1981   // Only enable those configured for this channel.
   1982   for (const auto& codec : recv_codecs_) {
   1983     webrtc::CodecInst voe_codec;
   1984     if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
   1985       voe_codec.pltype = codec.id;
   1986       if (engine()->voe()->codec()->SetRecPayloadType(
   1987           channel, voe_codec) == -1) {
   1988         LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
   1989         DeleteVoEChannel(channel);
   1990         return false;
   1991       }
   1992     }
   1993   }
   1994 
   1995   const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
   1996   if (send_channel != -1) {
   1997     // Associate receive channel with first send channel (so the receive channel
   1998     // can obtain RTT from the send channel)
   1999     engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
   2000     LOG(LS_INFO) << "VoiceEngine channel #" << channel
   2001                  << " is associated with channel #" << send_channel << ".";
   2002   }
   2003 
   2004   recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
   2005       channel, ssrc, receiver_reports_ssrc_,
   2006       options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
   2007       recv_rtp_extensions_, call_)));
   2008 
   2009   SetNack(channel, nack_enabled_);
   2010   SetPlayout(channel, playout_);
   2011 
   2012   return true;
   2013 }
   2014 
   2015 bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
   2016   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2017   LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
   2018 
   2019   const auto it = recv_streams_.find(ssrc);
   2020   if (it == recv_streams_.end()) {
   2021     LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
   2022                     << " which doesn't exist.";
   2023     return false;
   2024   }
   2025 
   2026   // Deregister default channel, if that's the one being destroyed.
   2027   if (IsDefaultRecvStream(ssrc)) {
   2028     default_recv_ssrc_ = -1;
   2029   }
   2030 
   2031   const int channel = it->second->channel();
   2032 
   2033   // Clean up and delete the receive stream+channel.
   2034   LOG(LS_INFO) << "Removing audio receive stream " << ssrc
   2035                << " with VoiceEngine channel #" << channel << ".";
   2036   it->second->SetRawAudioSink(nullptr);
   2037   delete it->second;
   2038   recv_streams_.erase(it);
   2039   return DeleteVoEChannel(channel);
   2040 }
   2041 
   2042 bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
   2043                                                AudioRenderer* renderer) {
   2044   auto it = send_streams_.find(ssrc);
   2045   if (it == send_streams_.end()) {
   2046     if (renderer) {
   2047       // Return an error if trying to set a valid renderer with an invalid ssrc.
   2048       LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
   2049       return false;
   2050     }
   2051 
   2052     // The channel likely has gone away, do nothing.
   2053     return true;
   2054   }
   2055 
   2056   if (renderer) {
   2057     it->second->Start(renderer);
   2058   } else {
   2059     it->second->Stop();
   2060   }
   2061 
   2062   return true;
   2063 }
   2064 
   2065 bool WebRtcVoiceMediaChannel::GetActiveStreams(
   2066     AudioInfo::StreamList* actives) {
   2067   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2068   actives->clear();
   2069   for (const auto& ch : recv_streams_) {
   2070     int level = GetOutputLevel(ch.second->channel());
   2071     if (level > 0) {
   2072       actives->push_back(std::make_pair(ch.first, level));
   2073     }
   2074   }
   2075   return true;
   2076 }
   2077 
   2078 int WebRtcVoiceMediaChannel::GetOutputLevel() {
   2079   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2080   int highest = 0;
   2081   for (const auto& ch : recv_streams_) {
   2082     highest = std::max(GetOutputLevel(ch.second->channel()), highest);
   2083   }
   2084   return highest;
   2085 }
   2086 
   2087 int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
   2088   int ret;
   2089   if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
   2090     // In case of error, log the info and continue
   2091     LOG_RTCERR0(TimeSinceLastTyping);
   2092     ret = -1;
   2093   } else {
   2094     ret *= 1000;  // We return ms, webrtc returns seconds.
   2095   }
   2096   return ret;
   2097 }
   2098 
   2099 void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
   2100     int cost_per_typing, int reporting_threshold, int penalty_decay,
   2101     int type_event_delay) {
   2102   if (engine()->voe()->processing()->SetTypingDetectionParameters(
   2103           time_window, cost_per_typing,
   2104           reporting_threshold, penalty_decay, type_event_delay) == -1) {
   2105     // In case of error, log the info and continue
   2106     LOG_RTCERR5(SetTypingDetectionParameters, time_window,
   2107                 cost_per_typing, reporting_threshold, penalty_decay,
   2108                 type_event_delay);
   2109   }
   2110 }
   2111 
   2112 bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
   2113   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2114   if (ssrc == 0) {
   2115     default_recv_volume_ = volume;
   2116     if (default_recv_ssrc_ == -1) {
   2117       return true;
   2118     }
   2119     ssrc = static_cast<uint32_t>(default_recv_ssrc_);
   2120   }
   2121   int ch_id = GetReceiveChannelId(ssrc);
   2122   if (ch_id < 0) {
   2123     LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
   2124     return false;
   2125   }
   2126 
   2127   if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
   2128                                                                      volume)) {
   2129     LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
   2130     return false;
   2131   }
   2132   LOG(LS_INFO) << "SetOutputVolume to " << volume
   2133                << " for channel " << ch_id << " and ssrc " << ssrc;
   2134   return true;
   2135 }
   2136 
   2137 bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
   2138   return dtmf_payload_type_ ? true : false;
   2139 }
   2140 
   2141 bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
   2142                                          int duration) {
   2143   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2144   LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
   2145   if (!dtmf_payload_type_) {
   2146     return false;
   2147   }
   2148 
   2149   // Figure out which WebRtcAudioSendStream to send the event on.
   2150   auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
   2151   if (it == send_streams_.end()) {
   2152     LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
   2153     return false;
   2154   }
   2155   if (event < kMinTelephoneEventCode ||
   2156       event > kMaxTelephoneEventCode) {
   2157     LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
   2158     return false;
   2159   }
   2160   if (duration < kMinTelephoneEventDuration ||
   2161       duration > kMaxTelephoneEventDuration) {
   2162     LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
   2163     return false;
   2164   }
   2165   return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
   2166 }
   2167 
   2168 void WebRtcVoiceMediaChannel::OnPacketReceived(
   2169     rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
   2170   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2171 
   2172   uint32_t ssrc = 0;
   2173   if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
   2174     return;
   2175   }
   2176 
   2177   // If we don't have a default channel, and the SSRC is unknown, create a
   2178   // default channel.
   2179   if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
   2180     StreamParams sp;
   2181     sp.ssrcs.push_back(ssrc);
   2182     LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
   2183     if (!AddRecvStream(sp)) {
   2184       LOG(LS_WARNING) << "Could not create default receive stream.";
   2185       return;
   2186     }
   2187     default_recv_ssrc_ = ssrc;
   2188     SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
   2189   }
   2190 
   2191   // Forward packet to Call. If the SSRC is unknown we'll return after this.
   2192   const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
   2193                                               packet_time.not_before);
   2194   webrtc::PacketReceiver::DeliveryStatus delivery_result =
   2195       call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
   2196           reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
   2197           webrtc_packet_time);
   2198   if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
   2199     // If the SSRC is unknown here, route it to the default channel, if we have
   2200     // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
   2201     if (default_recv_ssrc_ == -1) {
   2202       return;
   2203     } else {
   2204       ssrc = default_recv_ssrc_;
   2205     }
   2206   }
   2207 
   2208   // Find the channel to send this packet to. It must exist since webrtc::Call
   2209   // was able to demux the packet.
   2210   int channel = GetReceiveChannelId(ssrc);
   2211   RTC_DCHECK(channel != -1);
   2212 
   2213   // Pass it off to the decoder.
   2214   engine()->voe()->network()->ReceivedRTPPacket(
   2215       channel, packet->data(), packet->size(), webrtc_packet_time);
   2216 }
   2217 
   2218 void WebRtcVoiceMediaChannel::OnRtcpReceived(
   2219     rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
   2220   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2221 
   2222   // Forward packet to Call as well.
   2223   const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
   2224                                               packet_time.not_before);
   2225   call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
   2226       reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
   2227       webrtc_packet_time);
   2228 
   2229   // Sending channels need all RTCP packets with feedback information.
   2230   // Even sender reports can contain attached report blocks.
   2231   // Receiving channels need sender reports in order to create
   2232   // correct receiver reports.
   2233   int type = 0;
   2234   if (!GetRtcpType(packet->data(), packet->size(), &type)) {
   2235     LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
   2236     return;
   2237   }
   2238 
   2239   // If it is a sender report, find the receive channel that is listening.
   2240   if (type == kRtcpTypeSR) {
   2241     uint32_t ssrc = 0;
   2242     if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
   2243       return;
   2244     }
   2245     int recv_channel_id = GetReceiveChannelId(ssrc);
   2246     if (recv_channel_id != -1) {
   2247       engine()->voe()->network()->ReceivedRTCPPacket(
   2248           recv_channel_id, packet->data(), packet->size());
   2249     }
   2250   }
   2251 
   2252   // SR may continue RR and any RR entry may correspond to any one of the send
   2253   // channels. So all RTCP packets must be forwarded all send channels. VoE
   2254   // will filter out RR internally.
   2255   for (const auto& ch : send_streams_) {
   2256     engine()->voe()->network()->ReceivedRTCPPacket(
   2257         ch.second->channel(), packet->data(), packet->size());
   2258   }
   2259 }
   2260 
   2261 bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
   2262   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2263   int channel = GetSendChannelId(ssrc);
   2264   if (channel == -1) {
   2265     LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
   2266     return false;
   2267   }
   2268   if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
   2269     LOG_RTCERR2(SetInputMute, channel, muted);
   2270     return false;
   2271   }
   2272   // We set the AGC to mute state only when all the channels are muted.
   2273   // This implementation is not ideal, instead we should signal the AGC when
   2274   // the mic channel is muted/unmuted. We can't do it today because there
   2275   // is no good way to know which stream is mapping to the mic channel.
   2276   bool all_muted = muted;
   2277   for (const auto& ch : send_streams_) {
   2278     if (!all_muted) {
   2279       break;
   2280     }
   2281     if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
   2282                                                 all_muted)) {
   2283       LOG_RTCERR1(GetInputMute, ch.second->channel());
   2284       return false;
   2285     }
   2286   }
   2287 
   2288   webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
   2289   if (ap) {
   2290     ap->set_output_will_be_muted(all_muted);
   2291   }
   2292   return true;
   2293 }
   2294 
   2295 // TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
   2296 // SetMaxSendBitrate() in future.
   2297 bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
   2298   LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
   2299   return SetSendBitrateInternal(bps);
   2300 }
   2301 
   2302 bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
   2303   LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
   2304 
   2305   send_bitrate_setting_ = true;
   2306   send_bitrate_bps_ = bps;
   2307 
   2308   if (!send_codec_) {
   2309     LOG(LS_INFO) << "The send codec has not been set up yet. "
   2310                  << "The send bitrate setting will be applied later.";
   2311     return true;
   2312   }
   2313 
   2314   // Bitrate is auto by default.
   2315   // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
   2316   // SetMaxSendBandwith(0), the second call removes the previous limit.
   2317   if (bps <= 0)
   2318     return true;
   2319 
   2320   webrtc::CodecInst codec = *send_codec_;
   2321   bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
   2322 
   2323   if (is_multi_rate) {
   2324     // If codec is multi-rate then just set the bitrate.
   2325     codec.rate = bps;
   2326     for (const auto& ch : send_streams_) {
   2327       if (!SetSendCodec(ch.second->channel(), codec)) {
   2328         LOG(LS_INFO) << "Failed to set codec " << codec.plname
   2329                      << " to bitrate " << bps << " bps.";
   2330         return false;
   2331       }
   2332     }
   2333     return true;
   2334   } else {
   2335     // If codec is not multi-rate and |bps| is less than the fixed bitrate
   2336     // then fail. If codec is not multi-rate and |bps| exceeds or equal the
   2337     // fixed bitrate then ignore.
   2338     if (bps < codec.rate) {
   2339       LOG(LS_INFO) << "Failed to set codec " << codec.plname
   2340                    << " to bitrate " << bps << " bps"
   2341                    << ", requires at least " << codec.rate << " bps.";
   2342       return false;
   2343     }
   2344     return true;
   2345   }
   2346 }
   2347 
   2348 bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
   2349   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2350   RTC_DCHECK(info);
   2351 
   2352   // Get SSRC and stats for each sender.
   2353   RTC_DCHECK(info->senders.size() == 0);
   2354   for (const auto& stream : send_streams_) {
   2355     webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
   2356     VoiceSenderInfo sinfo;
   2357     sinfo.add_ssrc(stats.local_ssrc);
   2358     sinfo.bytes_sent = stats.bytes_sent;
   2359     sinfo.packets_sent = stats.packets_sent;
   2360     sinfo.packets_lost = stats.packets_lost;
   2361     sinfo.fraction_lost = stats.fraction_lost;
   2362     sinfo.codec_name = stats.codec_name;
   2363     sinfo.ext_seqnum = stats.ext_seqnum;
   2364     sinfo.jitter_ms = stats.jitter_ms;
   2365     sinfo.rtt_ms = stats.rtt_ms;
   2366     sinfo.audio_level = stats.audio_level;
   2367     sinfo.aec_quality_min = stats.aec_quality_min;
   2368     sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
   2369     sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
   2370     sinfo.echo_return_loss = stats.echo_return_loss;
   2371     sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
   2372     sinfo.typing_noise_detected =
   2373         (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
   2374     info->senders.push_back(sinfo);
   2375   }
   2376 
   2377   // Get SSRC and stats for each receiver.
   2378   RTC_DCHECK(info->receivers.size() == 0);
   2379   for (const auto& stream : recv_streams_) {
   2380     webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
   2381     VoiceReceiverInfo rinfo;
   2382     rinfo.add_ssrc(stats.remote_ssrc);
   2383     rinfo.bytes_rcvd = stats.bytes_rcvd;
   2384     rinfo.packets_rcvd = stats.packets_rcvd;
   2385     rinfo.packets_lost = stats.packets_lost;
   2386     rinfo.fraction_lost = stats.fraction_lost;
   2387     rinfo.codec_name = stats.codec_name;
   2388     rinfo.ext_seqnum = stats.ext_seqnum;
   2389     rinfo.jitter_ms = stats.jitter_ms;
   2390     rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
   2391     rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
   2392     rinfo.delay_estimate_ms = stats.delay_estimate_ms;
   2393     rinfo.audio_level = stats.audio_level;
   2394     rinfo.expand_rate = stats.expand_rate;
   2395     rinfo.speech_expand_rate = stats.speech_expand_rate;
   2396     rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
   2397     rinfo.accelerate_rate = stats.accelerate_rate;
   2398     rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
   2399     rinfo.decoding_calls_to_silence_generator =
   2400         stats.decoding_calls_to_silence_generator;
   2401     rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
   2402     rinfo.decoding_normal = stats.decoding_normal;
   2403     rinfo.decoding_plc = stats.decoding_plc;
   2404     rinfo.decoding_cng = stats.decoding_cng;
   2405     rinfo.decoding_plc_cng = stats.decoding_plc_cng;
   2406     rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
   2407     info->receivers.push_back(rinfo);
   2408   }
   2409 
   2410   return true;
   2411 }
   2412 
   2413 void WebRtcVoiceMediaChannel::SetRawAudioSink(
   2414     uint32_t ssrc,
   2415     rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
   2416   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2417   LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink";
   2418   const auto it = recv_streams_.find(ssrc);
   2419   if (it == recv_streams_.end()) {
   2420     LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
   2421     return;
   2422   }
   2423   it->second->SetRawAudioSink(std::move(sink));
   2424 }
   2425 
   2426 int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
   2427   unsigned int ulevel = 0;
   2428   int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
   2429   return (ret == 0) ? static_cast<int>(ulevel) : -1;
   2430 }
   2431 
   2432 int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
   2433   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2434   const auto it = recv_streams_.find(ssrc);
   2435   if (it != recv_streams_.end()) {
   2436     return it->second->channel();
   2437   }
   2438   return -1;
   2439 }
   2440 
   2441 int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
   2442   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   2443   const auto it = send_streams_.find(ssrc);
   2444   if (it != send_streams_.end()) {
   2445     return it->second->channel();
   2446   }
   2447   return -1;
   2448 }
   2449 
   2450 bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
   2451     const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
   2452   // Get the RED encodings from the parameter with no name. This may
   2453   // change based on what is discussed on the Jingle list.
   2454   // The encoding parameter is of the form "a/b"; we only support where
   2455   // a == b. Verify this and parse out the value into red_pt.
   2456   // If the parameter value is absent (as it will be until we wire up the
   2457   // signaling of this message), use the second codec specified (i.e. the
   2458   // one after "red") as the encoding parameter.
   2459   int red_pt = -1;
   2460   std::string red_params;
   2461   CodecParameterMap::const_iterator it = red_codec.params.find("");
   2462   if (it != red_codec.params.end()) {
   2463     red_params = it->second;
   2464     std::vector<std::string> red_pts;
   2465     if (rtc::split(red_params, '/', &red_pts) != 2 ||
   2466         red_pts[0] != red_pts[1] ||
   2467         !rtc::FromString(red_pts[0], &red_pt)) {
   2468       LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
   2469       return false;
   2470     }
   2471   } else if (red_codec.params.empty()) {
   2472     LOG(LS_WARNING) << "RED params not present, using defaults";
   2473     if (all_codecs.size() > 1) {
   2474       red_pt = all_codecs[1].id;
   2475     }
   2476   }
   2477 
   2478   // Try to find red_pt in |codecs|.
   2479   for (const AudioCodec& codec : all_codecs) {
   2480     if (codec.id == red_pt) {
   2481       // If we find the right codec, that will be the codec we pass to
   2482       // SetSendCodec, with the desired payload type.
   2483       if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
   2484         return true;
   2485       } else {
   2486         break;
   2487       }
   2488     }
   2489   }
   2490   LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
   2491   return false;
   2492 }
   2493 
   2494 bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
   2495   if (playout) {
   2496     LOG(LS_INFO) << "Starting playout for channel #" << channel;
   2497     if (engine()->voe()->base()->StartPlayout(channel) == -1) {
   2498       LOG_RTCERR1(StartPlayout, channel);
   2499       return false;
   2500     }
   2501   } else {
   2502     LOG(LS_INFO) << "Stopping playout for channel #" << channel;
   2503     engine()->voe()->base()->StopPlayout(channel);
   2504   }
   2505   return true;
   2506 }
   2507 }  // namespace cricket
   2508 
   2509 #endif  // HAVE_WEBRTC_VOICE
   2510