kwiberg | e985b90 | 2017-07-31 11:34: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/g711/audio_decoder_g711.h" |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <vector> |
| 15 | |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 16 | #include "absl/strings/match.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 18 | #include "rtc_base/numerics/safe_conversions.h" |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 22 | absl::optional<AudioDecoderG711::Config> AudioDecoderG711::SdpToConfig( |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 23 | const SdpAudioFormat& format) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 24 | const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU"); |
| 25 | const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA"); |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 26 | if (format.clockrate_hz == 8000 && format.num_channels >= 1 && |
| 27 | (is_pcmu || is_pcma)) { |
| 28 | Config config; |
| 29 | config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA; |
| 30 | config.num_channels = rtc::dchecked_cast<int>(format.num_channels); |
| 31 | RTC_DCHECK(config.IsOk()); |
Oskar Sundbom | 9065730 | 2017-11-16 10:52:34 +0100 | [diff] [blame] | 32 | return config; |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 33 | } else { |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 34 | return absl::nullopt; |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | void AudioDecoderG711::AppendSupportedDecoders( |
| 39 | std::vector<AudioCodecSpec>* specs) { |
| 40 | for (const char* type : {"PCMU", "PCMA"}) { |
| 41 | specs->push_back({{type, 8000, 1}, {8000, 1, 64000}}); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | std::unique_ptr<AudioDecoder> AudioDecoderG711::MakeAudioDecoder( |
Karl Wiberg | 17668ec | 2018-03-01 15:13:27 +0100 | [diff] [blame] | 46 | const Config& config, |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 47 | absl::optional<AudioCodecPairId> /*codec_pair_id*/) { |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 48 | RTC_DCHECK(config.IsOk()); |
| 49 | switch (config.type) { |
| 50 | case Config::Type::kPcmU: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 51 | return std::make_unique<AudioDecoderPcmU>(config.num_channels); |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 52 | case Config::Type::kPcmA: |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 53 | return std::make_unique<AudioDecoderPcmA>(config.num_channels); |
kwiberg | e985b90 | 2017-07-31 11:34:57 -0700 | [diff] [blame] | 54 | default: |
| 55 | return nullptr; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | } // namespace webrtc |