xref: /aosp_15_r20/external/webrtc/api/audio_codecs/g711/audio_encoder_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_encoder_g711.h"
12 
13 #include <memory>
14 #include <vector>
15 
16 #include "absl/strings/match.h"
17 #include "modules/audio_coding/codecs/g711/audio_encoder_pcm.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 #include "rtc_base/numerics/safe_minmax.h"
20 #include "rtc_base/string_to_number.h"
21 
22 namespace webrtc {
23 
SdpToConfig(const SdpAudioFormat & format)24 absl::optional<AudioEncoderG711::Config> AudioEncoderG711::SdpToConfig(
25     const SdpAudioFormat& format) {
26   const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU");
27   const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA");
28   if (format.clockrate_hz == 8000 && format.num_channels >= 1 &&
29       (is_pcmu || is_pcma)) {
30     Config config;
31     config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA;
32     config.num_channels = rtc::dchecked_cast<int>(format.num_channels);
33     config.frame_size_ms = 20;
34     auto ptime_iter = format.parameters.find("ptime");
35     if (ptime_iter != format.parameters.end()) {
36       const auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
37       if (ptime && *ptime > 0) {
38         config.frame_size_ms = rtc::SafeClamp(10 * (*ptime / 10), 10, 60);
39       }
40     }
41     if (!config.IsOk()) {
42       RTC_DCHECK_NOTREACHED();
43       return absl::nullopt;
44     }
45     return config;
46   } else {
47     return absl::nullopt;
48   }
49 }
50 
AppendSupportedEncoders(std::vector<AudioCodecSpec> * specs)51 void AudioEncoderG711::AppendSupportedEncoders(
52     std::vector<AudioCodecSpec>* specs) {
53   for (const char* type : {"PCMU", "PCMA"}) {
54     specs->push_back({{type, 8000, 1}, {8000, 1, 64000}});
55   }
56 }
57 
QueryAudioEncoder(const Config & config)58 AudioCodecInfo AudioEncoderG711::QueryAudioEncoder(const Config& config) {
59   RTC_DCHECK(config.IsOk());
60   return {8000, rtc::dchecked_cast<size_t>(config.num_channels),
61           64000 * config.num_channels};
62 }
63 
MakeAudioEncoder(const Config & config,int payload_type,absl::optional<AudioCodecPairId>,const FieldTrialsView * field_trials)64 std::unique_ptr<AudioEncoder> AudioEncoderG711::MakeAudioEncoder(
65     const Config& config,
66     int payload_type,
67     absl::optional<AudioCodecPairId> /*codec_pair_id*/,
68     const FieldTrialsView* field_trials) {
69   if (!config.IsOk()) {
70     RTC_DCHECK_NOTREACHED();
71     return nullptr;
72   }
73   switch (config.type) {
74     case Config::Type::kPcmU: {
75       AudioEncoderPcmU::Config impl_config;
76       impl_config.num_channels = config.num_channels;
77       impl_config.frame_size_ms = config.frame_size_ms;
78       impl_config.payload_type = payload_type;
79       return std::make_unique<AudioEncoderPcmU>(impl_config);
80     }
81     case Config::Type::kPcmA: {
82       AudioEncoderPcmA::Config impl_config;
83       impl_config.num_channels = config.num_channels;
84       impl_config.frame_size_ms = config.frame_size_ms;
85       impl_config.payload_type = payload_type;
86       return std::make_unique<AudioEncoderPcmA>(impl_config);
87     }
88     default: {
89       RTC_DCHECK_NOTREACHED();
90       return nullptr;
91     }
92   }
93 }
94 
95 }  // namespace webrtc
96