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