tor-browser

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

audio_decoder_g711.cc (2363B)


      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_decoder_g711.h"
     12 
     13 #include <initializer_list>
     14 #include <memory>
     15 #include <optional>
     16 #include <vector>
     17 
     18 #include "absl/strings/match.h"
     19 #include "api/audio_codecs/audio_codec_pair_id.h"
     20 #include "api/audio_codecs/audio_decoder.h"
     21 #include "api/audio_codecs/audio_format.h"
     22 #include "api/field_trials_view.h"
     23 #include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
     24 #include "rtc_base/checks.h"
     25 #include "rtc_base/numerics/safe_conversions.h"
     26 
     27 namespace webrtc {
     28 
     29 std::optional<AudioDecoderG711::Config> AudioDecoderG711::SdpToConfig(
     30    const SdpAudioFormat& format) {
     31  const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU");
     32  const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA");
     33  if (format.clockrate_hz == 8000 && format.num_channels >= 1 &&
     34      (is_pcmu || is_pcma)) {
     35    Config config;
     36    config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA;
     37    config.num_channels = dchecked_cast<int>(format.num_channels);
     38    if (!config.IsOk()) {
     39      RTC_DCHECK_NOTREACHED();
     40      return std::nullopt;
     41    }
     42    return config;
     43  } else {
     44    return std::nullopt;
     45  }
     46 }
     47 
     48 void AudioDecoderG711::AppendSupportedDecoders(
     49    std::vector<AudioCodecSpec>* specs) {
     50  for (const char* type : {"PCMU", "PCMA"}) {
     51    specs->push_back({{type, 8000, 1}, {8000, 1, 64000}});
     52  }
     53 }
     54 
     55 std::unique_ptr<AudioDecoder> AudioDecoderG711::MakeAudioDecoder(
     56    const Config& config,
     57    std::optional<AudioCodecPairId> /*codec_pair_id*/,
     58    const FieldTrialsView* /* field_trials */) {
     59  if (!config.IsOk()) {
     60    RTC_DCHECK_NOTREACHED();
     61    return nullptr;
     62  }
     63  switch (config.type) {
     64    case Config::Type::kPcmU:
     65      return std::make_unique<AudioDecoderPcmU>(config.num_channels);
     66    case Config::Type::kPcmA:
     67      return std::make_unique<AudioDecoderPcmA>(config.num_channels);
     68    default:
     69      RTC_DCHECK_NOTREACHED();
     70      return nullptr;
     71  }
     72 }
     73 
     74 }  // namespace webrtc