audio_encoder_L16.cc (2799B)
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/L16/audio_encoder_L16.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/field_trials_view.h" 24 #include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" 25 #include "modules/audio_coding/codecs/pcm16b/pcm16b_common.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<AudioEncoderL16::Config> AudioEncoderL16::SdpToConfig( 34 const SdpAudioFormat& format) { 35 if (!IsValueInRangeForNumericType<int>(format.num_channels)) { 36 RTC_DCHECK_NOTREACHED(); 37 return std::nullopt; 38 } 39 Config config; 40 config.sample_rate_hz = format.clockrate_hz; 41 config.num_channels = dchecked_cast<int>(format.num_channels); 42 auto ptime_iter = format.parameters.find("ptime"); 43 if (ptime_iter != format.parameters.end()) { 44 const auto ptime = StringToNumber<int>(ptime_iter->second); 45 if (ptime && *ptime > 0) { 46 config.frame_size_ms = SafeClamp(10 * (*ptime / 10), 10, 60); 47 } 48 } 49 if (absl::EqualsIgnoreCase(format.name, "L16") && config.IsOk()) { 50 return config; 51 } 52 return std::nullopt; 53 } 54 55 void AudioEncoderL16::AppendSupportedEncoders( 56 std::vector<AudioCodecSpec>* specs) { 57 Pcm16BAppendSupportedCodecSpecs(specs); 58 } 59 60 AudioCodecInfo AudioEncoderL16::QueryAudioEncoder( 61 const AudioEncoderL16::Config& config) { 62 RTC_DCHECK(config.IsOk()); 63 return {config.sample_rate_hz, dchecked_cast<size_t>(config.num_channels), 64 config.sample_rate_hz * config.num_channels * 16}; 65 } 66 67 std::unique_ptr<AudioEncoder> AudioEncoderL16::MakeAudioEncoder( 68 const AudioEncoderL16::Config& config, 69 int payload_type, 70 std::optional<AudioCodecPairId> /*codec_pair_id*/, 71 const FieldTrialsView* /* field_trials */) { 72 AudioEncoderPcm16B::Config c; 73 c.sample_rate_hz = config.sample_rate_hz; 74 c.num_channels = config.num_channels; 75 c.frame_size_ms = config.frame_size_ms; 76 c.payload_type = payload_type; 77 if (!config.IsOk()) { 78 RTC_DCHECK_NOTREACHED(); 79 return nullptr; 80 } 81 return std::make_unique<AudioEncoderPcm16B>(c); 82 } 83 84 } // namespace webrtc