kwiberg | b8727ae | 2017-06-17 17:41:59 -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/g722/audio_encoder_g722.h" |
kwiberg | b8727ae | 2017-06-17 17:41:59 -0700 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
| 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 16 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_coding/codecs/g722/audio_encoder_g722.h" |
| 18 | #include "rtc_base/ptr_util.h" |
| 19 | #include "rtc_base/safe_conversions.h" |
| 20 | #include "rtc_base/safe_minmax.h" |
| 21 | #include "rtc_base/string_to_number.h" |
kwiberg | b8727ae | 2017-06-17 17:41:59 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | rtc::Optional<AudioEncoderG722Config> AudioEncoderG722::SdpToConfig( |
| 26 | const SdpAudioFormat& format) { |
kwiberg | d1d79f6 | 2017-08-25 22:22:42 -0700 | [diff] [blame] | 27 | if (STR_CASE_CMP(format.name.c_str(), "g722") != 0 || |
| 28 | format.clockrate_hz != 8000) { |
| 29 | return rtc::Optional<AudioEncoderG722Config>(); |
| 30 | } |
| 31 | |
| 32 | AudioEncoderG722Config config; |
| 33 | config.num_channels = rtc::checked_cast<int>(format.num_channels); |
| 34 | auto ptime_iter = format.parameters.find("ptime"); |
| 35 | if (ptime_iter != format.parameters.end()) { |
| 36 | auto ptime = rtc::StringToNumber<int>(ptime_iter->second); |
| 37 | if (ptime && *ptime > 0) { |
| 38 | const int whole_packets = *ptime / 10; |
| 39 | config.frame_size_ms = rtc::SafeClamp<int>(whole_packets * 10, 10, 60); |
| 40 | } |
| 41 | } |
| 42 | return config.IsOk() ? rtc::Optional<AudioEncoderG722Config>(config) |
| 43 | : rtc::Optional<AudioEncoderG722Config>(); |
kwiberg | b8727ae | 2017-06-17 17:41:59 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void AudioEncoderG722::AppendSupportedEncoders( |
| 47 | std::vector<AudioCodecSpec>* specs) { |
kwiberg | e5eb724 | 2017-08-25 03:10:50 -0700 | [diff] [blame] | 48 | const SdpAudioFormat fmt = {"G722", 8000, 1}; |
kwiberg | b8727ae | 2017-06-17 17:41:59 -0700 | [diff] [blame] | 49 | const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); |
| 50 | specs->push_back({fmt, info}); |
| 51 | } |
| 52 | |
| 53 | AudioCodecInfo AudioEncoderG722::QueryAudioEncoder( |
| 54 | const AudioEncoderG722Config& config) { |
| 55 | RTC_DCHECK(config.IsOk()); |
| 56 | return {16000, rtc::dchecked_cast<size_t>(config.num_channels), |
| 57 | 64000 * config.num_channels}; |
| 58 | } |
| 59 | |
| 60 | std::unique_ptr<AudioEncoder> AudioEncoderG722::MakeAudioEncoder( |
| 61 | const AudioEncoderG722Config& config, |
| 62 | int payload_type) { |
| 63 | RTC_DCHECK(config.IsOk()); |
| 64 | return rtc::MakeUnique<AudioEncoderG722Impl>(config, payload_type); |
| 65 | } |
| 66 | |
| 67 | } // namespace webrtc |