tor-browser

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

audio_encoder_g711.cc (3436B)


      1 /*
      2 *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "api/audio_codecs/g711/audio_encoder_g711.h"
     12 
     13 #include <cstddef>
     14 #include <initializer_list>
     15 #include <map>
     16 #include <memory>
     17 #include <optional>
     18 #include <vector>
     19 
     20 #include "absl/strings/match.h"
     21 #include "api/audio_codecs/audio_codec_pair_id.h"
     22 #include "api/audio_codecs/audio_encoder.h"
     23 #include "api/audio_codecs/audio_format.h"
     24 #include "api/field_trials_view.h"
     25 #include "modules/audio_coding/codecs/g711/audio_encoder_pcm.h"
     26 #include "rtc_base/checks.h"
     27 #include "rtc_base/numerics/safe_conversions.h"
     28 #include "rtc_base/numerics/safe_minmax.h"
     29 #include "rtc_base/string_to_number.h"
     30 
     31 namespace webrtc {
     32 
     33 std::optional<AudioEncoderG711::Config> AudioEncoderG711::SdpToConfig(
     34    const SdpAudioFormat& format) {
     35  const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU");
     36  const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA");
     37  if (format.clockrate_hz == 8000 && format.num_channels >= 1 &&
     38      (is_pcmu || is_pcma)) {
     39    Config config;
     40    config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA;
     41    config.num_channels = dchecked_cast<int>(format.num_channels);
     42    config.frame_size_ms = 20;
     43    auto ptime_iter = format.parameters.find("ptime");
     44    if (ptime_iter != format.parameters.end()) {
     45      const auto ptime = StringToNumber<int>(ptime_iter->second);
     46      if (ptime && *ptime > 0) {
     47        config.frame_size_ms = SafeClamp(10 * (*ptime / 10), 10, 60);
     48      }
     49    }
     50    if (!config.IsOk()) {
     51      RTC_DCHECK_NOTREACHED();
     52      return std::nullopt;
     53    }
     54    return config;
     55  } else {
     56    return std::nullopt;
     57  }
     58 }
     59 
     60 void AudioEncoderG711::AppendSupportedEncoders(
     61    std::vector<AudioCodecSpec>* specs) {
     62  for (const char* type : {"PCMU", "PCMA"}) {
     63    specs->push_back({{type, 8000, 1}, {8000, 1, 64000}});
     64  }
     65 }
     66 
     67 AudioCodecInfo AudioEncoderG711::QueryAudioEncoder(const Config& config) {
     68  RTC_DCHECK(config.IsOk());
     69  return {8000, dchecked_cast<size_t>(config.num_channels),
     70          64000 * config.num_channels};
     71 }
     72 
     73 std::unique_ptr<AudioEncoder> AudioEncoderG711::MakeAudioEncoder(
     74    const Config& config,
     75    int payload_type,
     76    std::optional<AudioCodecPairId> /*codec_pair_id*/,
     77    const FieldTrialsView* /* field_trials */) {
     78  if (!config.IsOk()) {
     79    RTC_DCHECK_NOTREACHED();
     80    return nullptr;
     81  }
     82  switch (config.type) {
     83    case Config::Type::kPcmU: {
     84      AudioEncoderPcmU::Config impl_config;
     85      impl_config.num_channels = config.num_channels;
     86      impl_config.frame_size_ms = config.frame_size_ms;
     87      impl_config.payload_type = payload_type;
     88      return std::make_unique<AudioEncoderPcmU>(impl_config);
     89    }
     90    case Config::Type::kPcmA: {
     91      AudioEncoderPcmA::Config impl_config;
     92      impl_config.num_channels = config.num_channels;
     93      impl_config.frame_size_ms = config.frame_size_ms;
     94      impl_config.payload_type = payload_type;
     95      return std::make_unique<AudioEncoderPcmA>(impl_config);
     96    }
     97    default: {
     98      RTC_DCHECK_NOTREACHED();
     99      return nullptr;
    100    }
    101  }
    102 }
    103 
    104 }  // namespace webrtc