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
11 #include "api/audio_codecs/g722/audio_encoder_g722.h"
12
13 #include <memory>
14 #include <vector>
15
16 #include "absl/strings/match.h"
17 #include "modules/audio_coding/codecs/g722/audio_encoder_g722.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 #include "rtc_base/numerics/safe_minmax.h"
20 #include "rtc_base/string_to_number.h"
21
22 namespace webrtc {
23
SdpToConfig(const SdpAudioFormat & format)24 absl::optional<AudioEncoderG722Config> AudioEncoderG722::SdpToConfig(
25 const SdpAudioFormat& format) {
26 if (!absl::EqualsIgnoreCase(format.name, "g722") ||
27 format.clockrate_hz != 8000) {
28 return absl::nullopt;
29 }
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 }
41 if (!config.IsOk()) {
42 RTC_DCHECK_NOTREACHED();
43 return absl::nullopt;
44 }
45 return config;
46 }
47
AppendSupportedEncoders(std::vector<AudioCodecSpec> * specs)48 void AudioEncoderG722::AppendSupportedEncoders(
49 std::vector<AudioCodecSpec>* specs) {
50 const SdpAudioFormat fmt = {"G722", 8000, 1};
51 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
52 specs->push_back({fmt, info});
53 }
54
QueryAudioEncoder(const AudioEncoderG722Config & config)55 AudioCodecInfo AudioEncoderG722::QueryAudioEncoder(
56 const AudioEncoderG722Config& config) {
57 RTC_DCHECK(config.IsOk());
58 return {16000, rtc::dchecked_cast<size_t>(config.num_channels),
59 64000 * config.num_channels};
60 }
61
MakeAudioEncoder(const AudioEncoderG722Config & config,int payload_type,absl::optional<AudioCodecPairId>,const FieldTrialsView * field_trials)62 std::unique_ptr<AudioEncoder> AudioEncoderG722::MakeAudioEncoder(
63 const AudioEncoderG722Config& config,
64 int payload_type,
65 absl::optional<AudioCodecPairId> /*codec_pair_id*/,
66 const FieldTrialsView* field_trials) {
67 if (!config.IsOk()) {
68 RTC_DCHECK_NOTREACHED();
69 return nullptr;
70 }
71 return std::make_unique<AudioEncoderG722Impl>(config, payload_type);
72 }
73
74 } // namespace webrtc
75