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/isac/audio_encoder_isac_float.h"
12
13 #include <memory>
14
15 #include "absl/strings/match.h"
16 #include "modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
17 #include "rtc_base/string_to_number.h"
18
19 namespace webrtc {
20
21 absl::optional<AudioEncoderIsacFloat::Config>
SdpToConfig(const SdpAudioFormat & format)22 AudioEncoderIsacFloat::SdpToConfig(const SdpAudioFormat& format) {
23 if (absl::EqualsIgnoreCase(format.name, "ISAC") &&
24 (format.clockrate_hz == 16000 || format.clockrate_hz == 32000) &&
25 format.num_channels == 1) {
26 Config config;
27 config.sample_rate_hz = format.clockrate_hz;
28 config.bit_rate = format.clockrate_hz == 16000 ? 32000 : 56000;
29 if (config.sample_rate_hz == 16000) {
30 // For sample rate 16 kHz, optionally use 60 ms frames, instead of the
31 // default 30 ms.
32 const auto ptime_iter = format.parameters.find("ptime");
33 if (ptime_iter != format.parameters.end()) {
34 const auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
35 if (ptime && *ptime >= 60) {
36 config.frame_size_ms = 60;
37 }
38 }
39 }
40 return config;
41 } else {
42 return absl::nullopt;
43 }
44 }
45
AppendSupportedEncoders(std::vector<AudioCodecSpec> * specs)46 void AudioEncoderIsacFloat::AppendSupportedEncoders(
47 std::vector<AudioCodecSpec>* specs) {
48 for (int sample_rate_hz : {16000, 32000}) {
49 const SdpAudioFormat fmt = {"ISAC", sample_rate_hz, 1};
50 const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
51 specs->push_back({fmt, info});
52 }
53 }
54
QueryAudioEncoder(const AudioEncoderIsacFloat::Config & config)55 AudioCodecInfo AudioEncoderIsacFloat::QueryAudioEncoder(
56 const AudioEncoderIsacFloat::Config& config) {
57 RTC_DCHECK(config.IsOk());
58 constexpr int min_bitrate = 10000;
59 const int max_bitrate = config.sample_rate_hz == 16000 ? 32000 : 56000;
60 const int default_bitrate = max_bitrate;
61 return {config.sample_rate_hz, 1, default_bitrate, min_bitrate, max_bitrate};
62 }
63
MakeAudioEncoder(const AudioEncoderIsacFloat::Config & config,int payload_type,absl::optional<AudioCodecPairId>)64 std::unique_ptr<AudioEncoder> AudioEncoderIsacFloat::MakeAudioEncoder(
65 const AudioEncoderIsacFloat::Config& config,
66 int payload_type,
67 absl::optional<AudioCodecPairId> /*codec_pair_id*/) {
68 RTC_DCHECK(config.IsOk());
69 AudioEncoderIsacFloatImpl::Config c;
70 c.payload_type = payload_type;
71 c.sample_rate_hz = config.sample_rate_hz;
72 c.frame_size_ms = config.frame_size_ms;
73 c.bit_rate = config.bit_rate;
74 return std::make_unique<AudioEncoderIsacFloatImpl>(c);
75 }
76
77 } // namespace webrtc
78