blob: 0cc8dc4ddd29ef5b55ee071927a6943e60334d89 [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_encoder_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_encoder_pcm.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010019#include "rtc_base/numerics/safe_conversions.h"
20#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/string_to_number.h"
kwiberge985b902017-07-31 11:34:57 -070022
23namespace webrtc {
24
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020025absl::optional<AudioEncoderG711::Config> AudioEncoderG711::SdpToConfig(
kwiberge985b902017-07-31 11:34:57 -070026 const SdpAudioFormat& format) {
Niels Möller2edab4c2018-10-22 09:48:08 +020027 const bool is_pcmu = absl::EqualsIgnoreCase(format.name, "PCMU");
28 const bool is_pcma = absl::EqualsIgnoreCase(format.name, "PCMA");
kwiberge985b902017-07-31 11:34:57 -070029 if (format.clockrate_hz == 8000 && format.num_channels >= 1 &&
30 (is_pcmu || is_pcma)) {
31 Config config;
32 config.type = is_pcmu ? Config::Type::kPcmU : Config::Type::kPcmA;
33 config.num_channels = rtc::dchecked_cast<int>(format.num_channels);
34 config.frame_size_ms = 20;
35 auto ptime_iter = format.parameters.find("ptime");
36 if (ptime_iter != format.parameters.end()) {
37 const auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
38 if (ptime && *ptime > 0) {
39 config.frame_size_ms = rtc::SafeClamp(10 * (*ptime / 10), 10, 60);
40 }
41 }
42 RTC_DCHECK(config.IsOk());
Oskar Sundbom90657302017-11-16 10:52:34 +010043 return config;
kwiberge985b902017-07-31 11:34:57 -070044 } else {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020045 return absl::nullopt;
kwiberge985b902017-07-31 11:34:57 -070046 }
47}
48
49void AudioEncoderG711::AppendSupportedEncoders(
50 std::vector<AudioCodecSpec>* specs) {
51 for (const char* type : {"PCMU", "PCMA"}) {
52 specs->push_back({{type, 8000, 1}, {8000, 1, 64000}});
53 }
54}
55
56AudioCodecInfo AudioEncoderG711::QueryAudioEncoder(const Config& config) {
57 RTC_DCHECK(config.IsOk());
58 return {8000, rtc::dchecked_cast<size_t>(config.num_channels),
59 64000 * config.num_channels};
60}
61
62std::unique_ptr<AudioEncoder> AudioEncoderG711::MakeAudioEncoder(
63 const Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +010064 int payload_type,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020065 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
kwiberge985b902017-07-31 11:34:57 -070066 RTC_DCHECK(config.IsOk());
67 switch (config.type) {
68 case Config::Type::kPcmU: {
69 AudioEncoderPcmU::Config impl_config;
70 impl_config.num_channels = config.num_channels;
71 impl_config.frame_size_ms = config.frame_size_ms;
72 impl_config.payload_type = payload_type;
Karl Wiberg918f50c2018-07-05 11:40:33 +020073 return absl::make_unique<AudioEncoderPcmU>(impl_config);
kwiberge985b902017-07-31 11:34:57 -070074 }
75 case Config::Type::kPcmA: {
76 AudioEncoderPcmA::Config impl_config;
77 impl_config.num_channels = config.num_channels;
78 impl_config.frame_size_ms = config.frame_size_ms;
79 impl_config.payload_type = payload_type;
Karl Wiberg918f50c2018-07-05 11:40:33 +020080 return absl::make_unique<AudioEncoderPcmA>(impl_config);
kwiberge985b902017-07-31 11:34:57 -070081 }
82 default: { return nullptr; }
83 }
84}
85
86} // namespace webrtc