kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/audio_codecs/L16/audio_encoder_L16.h" |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 15 | #include "absl/strings/match.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h" |
| 17 | #include "modules/audio_coding/codecs/pcm16b/pcm16b_common.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 18 | #include "rtc_base/numerics/safe_conversions.h" |
Karl Wiberg | 133cff0 | 2018-07-06 15:40:14 +0200 | [diff] [blame] | 19 | #include "rtc_base/numerics/safe_minmax.h" |
| 20 | #include "rtc_base/string_to_number.h" |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 24 | absl::optional<AudioEncoderL16::Config> AudioEncoderL16::SdpToConfig( |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 25 | const SdpAudioFormat& format) { |
Karl Wiberg | eea063f | 2017-09-14 13:49:13 +0200 | [diff] [blame] | 26 | if (!rtc::IsValueInRangeForNumericType<int>(format.num_channels)) { |
Ivo Creusen | deb1b1b | 2021-11-24 19:29:10 +0000 | [diff] [blame] | 27 | RTC_DCHECK_NOTREACHED(); |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 28 | return absl::nullopt; |
Karl Wiberg | eea063f | 2017-09-14 13:49:13 +0200 | [diff] [blame] | 29 | } |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 30 | Config config; |
| 31 | config.sample_rate_hz = format.clockrate_hz; |
Karl Wiberg | eea063f | 2017-09-14 13:49:13 +0200 | [diff] [blame] | 32 | config.num_channels = rtc::dchecked_cast<int>(format.num_channels); |
Karl Wiberg | 133cff0 | 2018-07-06 15:40:14 +0200 | [diff] [blame] | 33 | auto ptime_iter = format.parameters.find("ptime"); |
| 34 | if (ptime_iter != format.parameters.end()) { |
| 35 | const auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 36 | if (ptime && *ptime > 0) { |
| 37 | config.frame_size_ms = rtc::SafeClamp(10 * (*ptime / 10), 10, 60); |
| 38 | } |
| 39 | } |
Ivo Creusen | deb1b1b | 2021-11-24 19:29:10 +0000 | [diff] [blame] | 40 | if (absl::EqualsIgnoreCase(format.name, "L16") && config.IsOk()) { |
| 41 | return config; |
| 42 | } |
| 43 | return absl::nullopt; |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void AudioEncoderL16::AppendSupportedEncoders( |
| 47 | std::vector<AudioCodecSpec>* specs) { |
| 48 | Pcm16BAppendSupportedCodecSpecs(specs); |
| 49 | } |
| 50 | |
| 51 | AudioCodecInfo AudioEncoderL16::QueryAudioEncoder( |
| 52 | const AudioEncoderL16::Config& config) { |
| 53 | RTC_DCHECK(config.IsOk()); |
| 54 | return {config.sample_rate_hz, |
| 55 | rtc::dchecked_cast<size_t>(config.num_channels), |
| 56 | config.sample_rate_hz * config.num_channels * 16}; |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<AudioEncoder> AudioEncoderL16::MakeAudioEncoder( |
| 60 | const AudioEncoderL16::Config& config, |
Karl Wiberg | 17668ec | 2018-03-01 15:13:27 +0100 | [diff] [blame] | 61 | int payload_type, |
Jonas Oreland | 6e2b9e2 | 2022-03-15 14:29:00 +0100 | [diff] [blame] | 62 | absl::optional<AudioCodecPairId> /*codec_pair_id*/, |
| 63 | const WebRtcKeyValueConfig* field_trials) { |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 64 | AudioEncoderPcm16B::Config c; |
| 65 | c.sample_rate_hz = config.sample_rate_hz; |
| 66 | c.num_channels = config.num_channels; |
| 67 | c.frame_size_ms = config.frame_size_ms; |
| 68 | c.payload_type = payload_type; |
Ivo Creusen | deb1b1b | 2021-11-24 19:29:10 +0000 | [diff] [blame] | 69 | if (!config.IsOk()) { |
| 70 | RTC_DCHECK_NOTREACHED(); |
| 71 | return nullptr; |
| 72 | } |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 73 | return std::make_unique<AudioEncoderPcm16B>(c); |
kwiberg | 7ea6e59 | 2017-08-16 06:12:57 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace webrtc |