audio_encoder_g722.cc (2691B)
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/g722/audio_encoder_g722.h" 12 13 #include <cstddef> 14 #include <map> 15 #include <memory> 16 #include <optional> 17 #include <vector> 18 19 #include "absl/strings/match.h" 20 #include "api/audio_codecs/audio_codec_pair_id.h" 21 #include "api/audio_codecs/audio_encoder.h" 22 #include "api/audio_codecs/audio_format.h" 23 #include "api/audio_codecs/g722/audio_encoder_g722_config.h" 24 #include "api/field_trials_view.h" 25 #include "modules/audio_coding/codecs/g722/audio_encoder_g722.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<AudioEncoderG722Config> AudioEncoderG722::SdpToConfig( 34 const SdpAudioFormat& format) { 35 if (!absl::EqualsIgnoreCase(format.name, "g722") || 36 format.clockrate_hz != 8000) { 37 return std::nullopt; 38 } 39 40 AudioEncoderG722Config config; 41 config.num_channels = checked_cast<int>(format.num_channels); 42 auto ptime_iter = format.parameters.find("ptime"); 43 if (ptime_iter != format.parameters.end()) { 44 auto ptime = StringToNumber<int>(ptime_iter->second); 45 if (ptime && *ptime > 0) { 46 const int whole_packets = *ptime / 10; 47 config.frame_size_ms = SafeClamp<int>(whole_packets * 10, 10, 60); 48 } 49 } 50 if (!config.IsOk()) { 51 RTC_DCHECK_NOTREACHED(); 52 return std::nullopt; 53 } 54 return config; 55 } 56 57 void AudioEncoderG722::AppendSupportedEncoders( 58 std::vector<AudioCodecSpec>* specs) { 59 const SdpAudioFormat fmt = {"G722", 8000, 1}; 60 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); 61 specs->push_back({fmt, info}); 62 } 63 64 AudioCodecInfo AudioEncoderG722::QueryAudioEncoder( 65 const AudioEncoderG722Config& config) { 66 RTC_DCHECK(config.IsOk()); 67 return {16000, dchecked_cast<size_t>(config.num_channels), 68 64000 * config.num_channels}; 69 } 70 71 std::unique_ptr<AudioEncoder> AudioEncoderG722::MakeAudioEncoder( 72 const AudioEncoderG722Config& config, 73 int payload_type, 74 std::optional<AudioCodecPairId> /*codec_pair_id*/, 75 const FieldTrialsView* /* field_trials */) { 76 if (!config.IsOk()) { 77 RTC_DCHECK_NOTREACHED(); 78 return nullptr; 79 } 80 return std::make_unique<AudioEncoderG722Impl>(config, payload_type); 81 } 82 83 } // namespace webrtc