diff --git a/webrtc/api/audio_codecs/isac/BUILD.gn b/webrtc/api/audio_codecs/isac/BUILD.gn index 164babf92c..3340e28a06 100644 --- a/webrtc/api/audio_codecs/isac/BUILD.gn +++ b/webrtc/api/audio_codecs/isac/BUILD.gn @@ -37,3 +37,29 @@ rtc_static_library("audio_decoder_isac_fix") { "../../../rtc_base:rtc_base_approved", ] } + +rtc_static_library("audio_encoder_isac_float") { + sources = [ + "audio_encoder_isac_float.cc", + "audio_encoder_isac_float.h", + ] + deps = [ + "..:audio_codecs_api", + "../../..:webrtc_common", + "../../../modules/audio_coding:isac", + "../../../rtc_base:rtc_base_approved", + ] +} + +rtc_static_library("audio_decoder_isac_float") { + sources = [ + "audio_decoder_isac_float.cc", + "audio_decoder_isac_float.h", + ] + deps = [ + "..:audio_codecs_api", + "../../..:webrtc_common", + "../../../modules/audio_coding:isac", + "../../../rtc_base:rtc_base_approved", + ] +} diff --git a/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.cc b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.cc new file mode 100644 index 0000000000..e26e6514bb --- /dev/null +++ b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.cc @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h" + +#include "webrtc/common_types.h" +#include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_decoder_isac.h" +#include "webrtc/rtc_base/ptr_util.h" + +namespace webrtc { + +rtc::Optional AudioDecoderIsacFloat::SdpToConfig( + const SdpAudioFormat& format) { + if (STR_CASE_CMP(format.name.c_str(), "ISAC") == 0 && + (format.clockrate_hz == 16000 || format.clockrate_hz == 32000) && + format.num_channels == 1) { + Config config; + config.sample_rate_hz = format.clockrate_hz; + return rtc::Optional(config); + } else { + return rtc::Optional(); + } +} + +void AudioDecoderIsacFloat::AppendSupportedDecoders( + std::vector* specs) { + specs->push_back({{"ISAC", 16000, 1}, {16000, 1, 32000, 10000, 32000}}); + specs->push_back({{"ISAC", 32000, 1}, {32000, 1, 56000, 10000, 56000}}); +} + +std::unique_ptr AudioDecoderIsacFloat::MakeAudioDecoder( + Config config) { + RTC_DCHECK(config.IsOk()); + return rtc::MakeUnique(config.sample_rate_hz); +} + +} // namespace webrtc diff --git a/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h new file mode 100644 index 0000000000..c0dc880dae --- /dev/null +++ b/webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_DECODER_ISAC_FLOAT_H_ +#define WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_DECODER_ISAC_FLOAT_H_ + +#include +#include + +#include "webrtc/api/audio_codecs/audio_decoder.h" +#include "webrtc/api/audio_codecs/audio_format.h" +#include "webrtc/rtc_base/optional.h" + +namespace webrtc { + +// iSAC decoder API (floating-point implementation) for use as a template +// parameter to CreateAudioDecoderFactory<...>(). +// +// NOTE: This struct is still under development and may change without notice. +struct AudioDecoderIsacFloat { + struct Config { + bool IsOk() const { + return sample_rate_hz == 16000 || sample_rate_hz == 32000; + } + int sample_rate_hz = 16000; + }; + static rtc::Optional SdpToConfig(const SdpAudioFormat& audio_format); + static void AppendSupportedDecoders(std::vector* specs); + static std::unique_ptr MakeAudioDecoder(Config config); +}; + +} // namespace webrtc + +#endif // WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_DECODER_ISAC_FLOAT_H_ diff --git a/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.cc b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.cc new file mode 100644 index 0000000000..500cfd1797 --- /dev/null +++ b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.cc @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h" + +#include "webrtc/common_types.h" +#include "webrtc/modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h" +#include "webrtc/rtc_base/ptr_util.h" +#include "webrtc/rtc_base/string_to_number.h" + +namespace webrtc { + +rtc::Optional AudioEncoderIsacFloat::SdpToConfig( + const SdpAudioFormat& format) { + if (STR_CASE_CMP(format.name.c_str(), "ISAC") == 0 && + (format.clockrate_hz == 16000 || format.clockrate_hz == 32000) && + format.num_channels == 1) { + Config config; + config.sample_rate_hz = format.clockrate_hz; + if (config.sample_rate_hz == 16000) { + // For sample rate 16 kHz, optionally use 60 ms frames, instead of the + // default 30 ms. + const auto ptime_iter = format.parameters.find("ptime"); + if (ptime_iter != format.parameters.end()) { + const auto ptime = rtc::StringToNumber(ptime_iter->second); + if (ptime && *ptime >= 60) { + config.frame_size_ms = 60; + } + } + } + return rtc::Optional(config); + } else { + return rtc::Optional(); + } +} + +void AudioEncoderIsacFloat::AppendSupportedEncoders( + std::vector* specs) { + for (int sample_rate_hz : {16000, 32000}) { + const SdpAudioFormat fmt = {"ISAC", sample_rate_hz, 1}; + const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt)); + specs->push_back({fmt, info}); + } +} + +AudioCodecInfo AudioEncoderIsacFloat::QueryAudioEncoder( + const AudioEncoderIsacFloat::Config& config) { + RTC_DCHECK(config.IsOk()); + constexpr int min_bitrate = 10000; + const int max_bitrate = config.sample_rate_hz == 16000 ? 32000 : 56000; + const int default_bitrate = max_bitrate; + return {config.sample_rate_hz, 1, default_bitrate, min_bitrate, max_bitrate}; +} + +std::unique_ptr AudioEncoderIsacFloat::MakeAudioEncoder( + const AudioEncoderIsacFloat::Config& config, + int payload_type) { + RTC_DCHECK(config.IsOk()); + AudioEncoderIsacFloatImpl::Config c; + c.sample_rate_hz = config.sample_rate_hz; + c.frame_size_ms = config.frame_size_ms; + c.payload_type = payload_type; + return rtc::MakeUnique(c); +} + +} // namespace webrtc diff --git a/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h new file mode 100644 index 0000000000..35bc94b464 --- /dev/null +++ b/webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_ENCODER_ISAC_FLOAT_H_ +#define WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_ENCODER_ISAC_FLOAT_H_ + +#include +#include + +#include "webrtc/api/audio_codecs/audio_encoder.h" +#include "webrtc/api/audio_codecs/audio_format.h" +#include "webrtc/rtc_base/optional.h" + +namespace webrtc { + +// iSAC encoder API (floating-point implementation) for use as a template +// parameter to CreateAudioEncoderFactory<...>(). +// +// NOTE: This struct is still under development and may change without notice. +struct AudioEncoderIsacFloat { + struct Config { + bool IsOk() const { + return (sample_rate_hz == 16000 && + (frame_size_ms == 30 || frame_size_ms == 60)) || + (sample_rate_hz == 32000 && frame_size_ms == 30); + } + int sample_rate_hz = 16000; + int frame_size_ms = 30; + }; + static rtc::Optional SdpToConfig(const SdpAudioFormat& audio_format); + static void AppendSupportedEncoders(std::vector* specs); + static AudioCodecInfo QueryAudioEncoder(const Config& config); + static std::unique_ptr MakeAudioEncoder(const Config& config, + int payload_type); +}; + +} // namespace webrtc + +#endif // WEBRTC_API_AUDIO_CODECS_ISAC_AUDIO_ENCODER_ISAC_FLOAT_H_ diff --git a/webrtc/api/audio_codecs/test/BUILD.gn b/webrtc/api/audio_codecs/test/BUILD.gn index ce6ed92544..16fdeb9698 100644 --- a/webrtc/api/audio_codecs/test/BUILD.gn +++ b/webrtc/api/audio_codecs/test/BUILD.gn @@ -34,7 +34,9 @@ if (rtc_include_tests) { "../ilbc:audio_decoder_ilbc", "../ilbc:audio_encoder_ilbc", "../isac:audio_decoder_isac_fix", + "../isac:audio_decoder_isac_float", "../isac:audio_encoder_isac_fix", + "../isac:audio_encoder_isac_float", "../opus:audio_decoder_opus", "../opus:audio_encoder_opus", "//testing/gmock", diff --git a/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc b/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc index c27f24218b..8d65a659a9 100644 --- a/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc +++ b/webrtc/api/audio_codecs/test/audio_decoder_factory_template_unittest.cc @@ -14,6 +14,7 @@ #include "webrtc/api/audio_codecs/g722/audio_decoder_g722.h" #include "webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h" #include "webrtc/api/audio_codecs/isac/audio_decoder_isac_fix.h" +#include "webrtc/api/audio_codecs/isac/audio_decoder_isac_float.h" #include "webrtc/api/audio_codecs/opus/audio_decoder_opus.h" #include "webrtc/rtc_base/ptr_util.h" #include "webrtc/test/gmock.h" @@ -174,12 +175,32 @@ TEST(AudioDecoderFactoryTemplateTest, IsacFix) { {"ISAC", 16000, 1}, {16000, 1, 32000, 10000, 32000}})); EXPECT_FALSE(factory->IsSupportedDecoder({"isac", 16000, 2})); EXPECT_TRUE(factory->IsSupportedDecoder({"isac", 16000, 1})); + EXPECT_FALSE(factory->IsSupportedDecoder({"isac", 32000, 1})); EXPECT_EQ(nullptr, factory->MakeAudioDecoder({"isac", 8000, 1})); auto dec = factory->MakeAudioDecoder({"isac", 16000, 1}); ASSERT_NE(nullptr, dec); EXPECT_EQ(16000, dec->SampleRateHz()); } +TEST(AudioDecoderFactoryTemplateTest, IsacFloat) { + auto factory = CreateAudioDecoderFactory(); + EXPECT_THAT( + factory->GetSupportedDecoders(), + testing::ElementsAre( + AudioCodecSpec{{"ISAC", 16000, 1}, {16000, 1, 32000, 10000, 32000}}, + AudioCodecSpec{{"ISAC", 32000, 1}, {32000, 1, 56000, 10000, 56000}})); + EXPECT_FALSE(factory->IsSupportedDecoder({"isac", 16000, 2})); + EXPECT_TRUE(factory->IsSupportedDecoder({"isac", 16000, 1})); + EXPECT_TRUE(factory->IsSupportedDecoder({"isac", 32000, 1})); + EXPECT_EQ(nullptr, factory->MakeAudioDecoder({"isac", 8000, 1})); + auto dec1 = factory->MakeAudioDecoder({"isac", 16000, 1}); + ASSERT_NE(nullptr, dec1); + EXPECT_EQ(16000, dec1->SampleRateHz()); + auto dec2 = factory->MakeAudioDecoder({"isac", 32000, 1}); + ASSERT_NE(nullptr, dec2); + EXPECT_EQ(32000, dec2->SampleRateHz()); +} + TEST(AudioDecoderFactoryTemplateTest, L16) { auto factory = CreateAudioDecoderFactory(); EXPECT_THAT( diff --git a/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc b/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc index 4120978291..7d8f3a8f95 100644 --- a/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc +++ b/webrtc/api/audio_codecs/test/audio_encoder_factory_template_unittest.cc @@ -14,6 +14,7 @@ #include "webrtc/api/audio_codecs/g722/audio_encoder_g722.h" #include "webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h" #include "webrtc/api/audio_codecs/isac/audio_encoder_isac_fix.h" +#include "webrtc/api/audio_codecs/isac/audio_encoder_isac_float.h" #include "webrtc/api/audio_codecs/opus/audio_encoder_opus.h" #include "webrtc/rtc_base/ptr_util.h" #include "webrtc/test/gmock.h" @@ -181,6 +182,8 @@ TEST(AudioEncoderFactoryTemplateTest, IsacFix) { factory->QueryAudioEncoder({"isac", 16000, 2})); EXPECT_EQ(rtc::Optional({16000, 1, 32000, 10000, 32000}), factory->QueryAudioEncoder({"isac", 16000, 1})); + EXPECT_EQ(rtc::Optional(), + factory->QueryAudioEncoder({"isac", 32000, 1})); EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"isac", 8000, 1})); auto enc1 = factory->MakeAudioEncoder(17, {"isac", 16000, 1}); ASSERT_NE(nullptr, enc1); @@ -192,6 +195,28 @@ TEST(AudioEncoderFactoryTemplateTest, IsacFix) { EXPECT_EQ(6u, enc2->Num10MsFramesInNextPacket()); } +TEST(AudioEncoderFactoryTemplateTest, IsacFloat) { + auto factory = CreateAudioEncoderFactory(); + EXPECT_THAT( + factory->GetSupportedEncoders(), + testing::ElementsAre( + AudioCodecSpec{{"ISAC", 16000, 1}, {16000, 1, 32000, 10000, 32000}}, + AudioCodecSpec{{"ISAC", 32000, 1}, {32000, 1, 56000, 10000, 56000}})); + EXPECT_EQ(rtc::Optional(), + factory->QueryAudioEncoder({"isac", 16000, 2})); + EXPECT_EQ(rtc::Optional({16000, 1, 32000, 10000, 32000}), + factory->QueryAudioEncoder({"isac", 16000, 1})); + EXPECT_EQ(rtc::Optional({32000, 1, 56000, 10000, 56000}), + factory->QueryAudioEncoder({"isac", 32000, 1})); + EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"isac", 8000, 1})); + auto enc1 = factory->MakeAudioEncoder(17, {"isac", 16000, 1}); + ASSERT_NE(nullptr, enc1); + EXPECT_EQ(16000, enc1->SampleRateHz()); + auto enc2 = factory->MakeAudioEncoder(17, {"isac", 32000, 1}); + ASSERT_NE(nullptr, enc2); + EXPECT_EQ(32000, enc2->SampleRateHz()); +} + TEST(AudioEncoderFactoryTemplateTest, L16) { auto factory = CreateAudioEncoderFactory(); EXPECT_THAT(