Expose ILBC codec in webrtc/api/audio_codecs/

BUG=webrtc:7834, webrtc:7840

Review-Url: https://codereview.webrtc.org/2951873002
Cr-Commit-Position: refs/heads/master@{#18803}
diff --git a/webrtc/api/audio_codecs/ilbc/BUILD.gn b/webrtc/api/audio_codecs/ilbc/BUILD.gn
new file mode 100644
index 0000000..bba2662
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/BUILD.gn
@@ -0,0 +1,45 @@
+# 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.
+
+import("../../../webrtc.gni")
+if (is_android) {
+  import("//build/config/android/config.gni")
+  import("//build/config/android/rules.gni")
+}
+
+rtc_source_set("audio_encoder_ilbc_config") {
+  sources = [
+    "audio_encoder_ilbc_config.h",
+  ]
+}
+
+rtc_static_library("audio_encoder_ilbc") {
+  sources = [
+    "audio_encoder_ilbc.cc",
+    "audio_encoder_ilbc.h",
+  ]
+  deps = [
+    ":audio_encoder_ilbc_config",
+    "..:audio_codecs_api",
+    "../../../base:rtc_base_approved",
+    "../../../modules/audio_coding:ilbc",
+  ]
+}
+
+rtc_static_library("audio_decoder_ilbc") {
+  sources = [
+    "audio_decoder_ilbc.cc",
+    "audio_decoder_ilbc.h",
+  ]
+  deps = [
+    "..:audio_codecs_api",
+    "../../..:webrtc_common",
+    "../../../base:rtc_base_approved",
+    "../../../modules/audio_coding:ilbc",
+  ]
+}
diff --git a/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.cc b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.cc
new file mode 100644
index 0000000..da290af
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.cc
@@ -0,0 +1,40 @@
+/*
+ *  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/ilbc/audio_decoder_ilbc.h"
+
+#include <memory>
+#include <vector>
+
+#include "webrtc/base/ptr_util.h"
+#include "webrtc/common_types.h"
+#include "webrtc/modules/audio_coding/codecs/ilbc/audio_decoder_ilbc.h"
+
+namespace webrtc {
+
+rtc::Optional<AudioDecoderIlbc::Config> AudioDecoderIlbc::SdpToConfig(
+    const SdpAudioFormat& format) {
+  return STR_CASE_CMP(format.name.c_str(), "ilbc") == 0 &&
+                 format.clockrate_hz == 8000 && format.num_channels == 1
+             ? rtc::Optional<Config>(Config())
+             : rtc::Optional<Config>();
+}
+
+void AudioDecoderIlbc::AppendSupportedDecoders(
+    std::vector<AudioCodecSpec>* specs) {
+  specs->push_back({{"ILBC", 8000, 1}, {8000, 1, 13300}});
+}
+
+std::unique_ptr<AudioDecoder> AudioDecoderIlbc::MakeAudioDecoder(
+    Config config) {
+  return rtc::MakeUnique<AudioDecoderIlbcImpl>();
+}
+
+}  // namespace webrtc
diff --git a/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h
new file mode 100644
index 0000000..1d58810
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h
@@ -0,0 +1,36 @@
+/*
+ *  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_ILBC_AUDIO_DECODER_ILBC_H_
+#define WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_DECODER_ILBC_H_
+
+#include <memory>
+#include <vector>
+
+#include "webrtc/api/audio_codecs/audio_decoder.h"
+#include "webrtc/api/audio_codecs/audio_format.h"
+#include "webrtc/base/optional.h"
+
+namespace webrtc {
+
+// ILBC decoder API for use as a template parameter to
+// CreateAudioDecoderFactory<...>().
+//
+// NOTE: This struct is still under development and may change without notice.
+struct AudioDecoderIlbc {
+  struct Config {};  // Empty---no config values needed!
+  static rtc::Optional<Config> SdpToConfig(const SdpAudioFormat& audio_format);
+  static void AppendSupportedDecoders(std::vector<AudioCodecSpec>* specs);
+  static std::unique_ptr<AudioDecoder> MakeAudioDecoder(Config config);
+};
+
+}  // namespace webrtc
+
+#endif  // WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_DECODER_ILBC_H_
diff --git a/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.cc b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.cc
new file mode 100644
index 0000000..a4b3a38
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.cc
@@ -0,0 +1,63 @@
+/*
+ *  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/ilbc/audio_encoder_ilbc.h"
+
+#include <memory>
+#include <vector>
+
+#include "webrtc/base/ptr_util.h"
+#include "webrtc/base/safe_conversions.h"
+#include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
+
+namespace webrtc {
+namespace {
+int GetIlbcBitrate(int ptime) {
+  switch (ptime) {
+    case 20:
+    case 40:
+      // 38 bytes per frame of 20 ms => 15200 bits/s.
+      return 15200;
+    case 30:
+    case 60:
+      // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
+      return 13333;
+    default:
+      FATAL();
+  }
+}
+}  // namespace
+
+rtc::Optional<AudioEncoderIlbcConfig> AudioEncoderIlbc::SdpToConfig(
+    const SdpAudioFormat& format) {
+  return AudioEncoderIlbcImpl::SdpToConfig(format);
+}
+
+void AudioEncoderIlbc::AppendSupportedEncoders(
+    std::vector<AudioCodecSpec>* specs) {
+  const SdpAudioFormat fmt = {"ILBC", 8000, 1};
+  const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
+  specs->push_back({fmt, info});
+}
+
+AudioCodecInfo AudioEncoderIlbc::QueryAudioEncoder(
+    const AudioEncoderIlbcConfig& config) {
+  RTC_DCHECK(config.IsOk());
+  return {8000, 1, GetIlbcBitrate(config.frame_size_ms)};
+}
+
+std::unique_ptr<AudioEncoder> AudioEncoderIlbc::MakeAudioEncoder(
+    const AudioEncoderIlbcConfig& config,
+    int payload_type) {
+  RTC_DCHECK(config.IsOk());
+  return rtc::MakeUnique<AudioEncoderIlbcImpl>(config, payload_type);
+}
+
+}  // namespace webrtc
diff --git a/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h
new file mode 100644
index 0000000..4201035
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h
@@ -0,0 +1,40 @@
+/*
+ *  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_ILBC_AUDIO_ENCODER_ILBC_H_
+#define WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_ENCODER_ILBC_H_
+
+#include <memory>
+#include <vector>
+
+#include "webrtc/api/audio_codecs/audio_encoder.h"
+#include "webrtc/api/audio_codecs/audio_format.h"
+#include "webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc_config.h"
+#include "webrtc/base/optional.h"
+
+namespace webrtc {
+
+// ILBC encoder API for use as a template parameter to
+// CreateAudioEncoderFactory<...>().
+//
+// NOTE: This struct is still under development and may change without notice.
+struct AudioEncoderIlbc {
+  static rtc::Optional<AudioEncoderIlbcConfig> SdpToConfig(
+      const SdpAudioFormat& audio_format);
+  static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs);
+  static AudioCodecInfo QueryAudioEncoder(const AudioEncoderIlbcConfig& config);
+  static std::unique_ptr<AudioEncoder> MakeAudioEncoder(
+      const AudioEncoderIlbcConfig& config,
+      int payload_type);
+};
+
+}  // namespace webrtc
+
+#endif  // WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_ENCODER_ILBC_H_
diff --git a/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc_config.h b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc_config.h
new file mode 100644
index 0000000..429ac81
--- /dev/null
+++ b/webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc_config.h
@@ -0,0 +1,29 @@
+/*
+ *  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_ILBC_AUDIO_ENCODER_ILBC_CONFIG_H_
+#define WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_ENCODER_ILBC_CONFIG_H_
+
+namespace webrtc {
+
+// NOTE: This struct is still under development and may change without notice.
+struct AudioEncoderIlbcConfig {
+  bool IsOk() const {
+    return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 ||
+            frame_size_ms == 60);
+  }
+  int frame_size_ms = 30;  // Valid values are 20, 30, 40, and 60 ms.
+  // Note that frame size 40 ms produces encodings with two 20 ms frames in
+  // them, and frame size 60 ms consists of two 30 ms frames.
+};
+
+}  // namespace webrtc
+
+#endif  // WEBRTC_API_AUDIO_CODECS_ILBC_AUDIO_ENCODER_ILBC_CONFIG_H_
diff --git a/webrtc/api/audio_codecs/test/BUILD.gn b/webrtc/api/audio_codecs/test/BUILD.gn
index 08d527d..38ca736 100644
--- a/webrtc/api/audio_codecs/test/BUILD.gn
+++ b/webrtc/api/audio_codecs/test/BUILD.gn
@@ -26,6 +26,8 @@
       "../../../test:test_support",
       "../g722:audio_decoder_g722",
       "../g722:audio_encoder_g722",
+      "../ilbc:audio_decoder_ilbc",
+      "../ilbc:audio_encoder_ilbc",
       "//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 c05fcd6..283a5f5 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
@@ -10,6 +10,7 @@
 
 #include "webrtc/api/audio_codecs/audio_decoder_factory_template.h"
 #include "webrtc/api/audio_codecs/g722/audio_decoder_g722.h"
+#include "webrtc/api/audio_codecs/ilbc/audio_decoder_ilbc.h"
 #include "webrtc/base/ptr_util.h"
 #include "webrtc/test/gmock.h"
 #include "webrtc/test/gtest.h"
@@ -131,4 +132,17 @@
   ASSERT_EQ(nullptr, dec3);
 }
 
+TEST(AudioDecoderFactoryTemplateTest, Ilbc) {
+  auto factory = CreateAudioDecoderFactory<AudioDecoderIlbc>();
+  EXPECT_THAT(factory->GetSupportedDecoders(),
+              testing::ElementsAre(
+                  AudioCodecSpec{{"ILBC", 8000, 1}, {8000, 1, 13300}}));
+  EXPECT_FALSE(factory->IsSupportedDecoder({"foo", 8000, 1}));
+  EXPECT_TRUE(factory->IsSupportedDecoder({"ilbc", 8000, 1}));
+  EXPECT_EQ(nullptr, factory->MakeAudioDecoder({"bar", 8000, 1}));
+  auto dec = factory->MakeAudioDecoder({"ilbc", 8000, 1});
+  ASSERT_NE(nullptr, dec);
+  EXPECT_EQ(8000, dec->SampleRateHz());
+}
+
 }  // namespace webrtc
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 abca968..c3c07c6 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
@@ -10,6 +10,7 @@
 
 #include "webrtc/api/audio_codecs/audio_encoder_factory_template.h"
 #include "webrtc/api/audio_codecs/g722/audio_encoder_g722.h"
+#include "webrtc/api/audio_codecs/ilbc/audio_encoder_ilbc.h"
 #include "webrtc/base/ptr_util.h"
 #include "webrtc/test/gmock.h"
 #include "webrtc/test/gtest.h"
@@ -133,4 +134,19 @@
   EXPECT_EQ(16000, enc->SampleRateHz());
 }
 
+TEST(AudioEncoderFactoryTemplateTest, Ilbc) {
+  auto factory = CreateAudioEncoderFactory<AudioEncoderIlbc>();
+  EXPECT_THAT(factory->GetSupportedEncoders(),
+              testing::ElementsAre(
+                  AudioCodecSpec{{"ILBC", 8000, 1}, {8000, 1, 13333}}));
+  EXPECT_EQ(rtc::Optional<AudioCodecInfo>(),
+            factory->QueryAudioEncoder({"foo", 8000, 1}));
+  EXPECT_EQ(rtc::Optional<AudioCodecInfo>({8000, 1, 13333}),
+            factory->QueryAudioEncoder({"ilbc", 8000, 1}));
+  EXPECT_EQ(nullptr, factory->MakeAudioEncoder(17, {"bar", 8000, 1}));
+  auto enc = factory->MakeAudioEncoder(17, {"ilbc", 8000, 1});
+  ASSERT_NE(nullptr, enc);
+  EXPECT_EQ(8000, enc->SampleRateHz());
+}
+
 }  // namespace webrtc