blob: 91599c416adf93c459895c3003cdfdf32789b6a8 [file] [log] [blame]
kwiberge985b902017-07-31 11:34:57 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/audio_codecs/g711/audio_decoder_g711.h"
kwiberge985b902017-07-31 11:34:57 -070012
13#include <memory>
14#include <vector>
15
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020017#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010019#include "rtc_base/numerics/safe_conversions.h"
kwiberge985b902017-07-31 11:34:57 -070020
21namespace webrtc {
22
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020023absl::optional<AudioDecoderG711::Config> AudioDecoderG711::SdpToConfig(
kwiberge985b902017-07-31 11:34:57 -070024 const SdpAudioFormat& format) {
Niels Möller2edab4c2018-10-22 09:48:08 +020025 const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU");
26 const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA");
kwiberge985b902017-07-31 11:34:57 -070027 if (format.clockrate_hz == 8000 && format.num_channels >= 1 &&
28 (is_pcmu || is_pcma)) {
29 Config config;
30 config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA;
31 config.num_channels = rtc::dchecked_cast<int>(format.num_channels);
32 RTC_DCHECK(config.IsOk());
Oskar Sundbom90657302017-11-16 10:52:34 +010033 return config;
kwiberge985b902017-07-31 11:34:57 -070034 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020035 return absl::nullopt;
kwiberge985b902017-07-31 11:34:57 -070036 }
37}
38
39void AudioDecoderG711::AppendSupportedDecoders(
40 std::vector<AudioCodecSpec>* specs) {
41 for (const char* type : {"PCMU", "PCMA"}) {
42 specs->push_back({{type, 8000, 1}, {8000, 1, 64000}});
43 }
44}
45
46std::unique_ptr<AudioDecoder> AudioDecoderG711::MakeAudioDecoder(
Karl Wiberg17668ec2018-03-01 15:13:27 +010047 const Config& config,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020048 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
kwiberge985b902017-07-31 11:34:57 -070049 RTC_DCHECK(config.IsOk());
50 switch (config.type) {
51 case Config::Type::kPcmU:
Karl Wiberg918f50c2018-07-05 11:40:33 +020052 return absl::make_unique<AudioDecoderPcmU>(config.num_channels);
kwiberge985b902017-07-31 11:34:57 -070053 case Config::Type::kPcmA:
Karl Wiberg918f50c2018-07-05 11:40:33 +020054 return absl::make_unique<AudioDecoderPcmA>(config.num_channels);
kwiberge985b902017-07-31 11:34:57 -070055 default:
56 return nullptr;
57 }
58}
59
60} // namespace webrtc