blob: 12c1746eb7eab6e66c7334e2ccaf280ebf55bb15 [file] [log] [blame]
kwibergb8727ae2017-06-17 17:41:59 -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/g722/audio_encoder_g722.h"
kwibergb8727ae2017-06-17 17:41:59 -070012
13#include <memory>
14#include <vector>
15
Niels Möller2edab4c2018-10-22 09:48:08 +020016#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/codecs/g722/audio_encoder_g722.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
19#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/string_to_number.h"
kwibergb8727ae2017-06-17 17:41:59 -070021
22namespace webrtc {
23
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020024absl::optional<AudioEncoderG722Config> AudioEncoderG722::SdpToConfig(
kwibergb8727ae2017-06-17 17:41:59 -070025 const SdpAudioFormat& format) {
Niels Möller2edab4c2018-10-22 09:48:08 +020026 if (!absl::EqualsIgnoreCase(format.name, "g722") ||
kwibergd1d79f62017-08-25 22:22:42 -070027 format.clockrate_hz != 8000) {
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020028 return absl::nullopt;
kwibergd1d79f62017-08-25 22:22:42 -070029 }
30
31 AudioEncoderG722Config config;
32 config.num_channels = rtc::checked_cast<int>(format.num_channels);
33 auto ptime_iter = format.parameters.find("ptime");
34 if (ptime_iter != format.parameters.end()) {
35 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
36 if (ptime && *ptime > 0) {
37 const int whole_packets = *ptime / 10;
38 config.frame_size_ms = rtc::SafeClamp<int>(whole_packets * 10, 10, 60);
39 }
40 }
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020041 return config.IsOk() ? absl::optional<AudioEncoderG722Config>(config)
42 : absl::nullopt;
kwibergb8727ae2017-06-17 17:41:59 -070043}
44
45void AudioEncoderG722::AppendSupportedEncoders(
46 std::vector<AudioCodecSpec>* specs) {
kwiberge5eb7242017-08-25 03:10:50 -070047 const SdpAudioFormat fmt = {"G722", 8000, 1};
kwibergb8727ae2017-06-17 17:41:59 -070048 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
49 specs->push_back({fmt, info});
50}
51
52AudioCodecInfo AudioEncoderG722::QueryAudioEncoder(
53 const AudioEncoderG722Config& config) {
54 RTC_DCHECK(config.IsOk());
55 return {16000, rtc::dchecked_cast<size_t>(config.num_channels),
56 64000 * config.num_channels};
57}
58
59std::unique_ptr<AudioEncoder> AudioEncoderG722::MakeAudioEncoder(
60 const AudioEncoderG722Config& config,
Karl Wiberg17668ec2018-03-01 15:13:27 +010061 int payload_type,
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020062 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
kwibergb8727ae2017-06-17 17:41:59 -070063 RTC_DCHECK(config.IsOk());
Mirko Bonadei317a1f02019-09-17 17:06:18 +020064 return std::make_unique<AudioEncoderG722Impl>(config, payload_type);
kwibergb8727ae2017-06-17 17:41:59 -070065}
66
67} // namespace webrtc