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