Uprev to webrtc branch-heads/71

To commit "Merge to M71: AEC3: Simplify render buffering"

https://webrtc.googlesource.com/src/+/0ba24191ee90c70679d2452363676c2d65b6b751

BUG=chromium:906512
TEST=Build and test on nocturne

Change-Id: I34fdbb3712e26d12c1d635d8e16274f424e5a7e9
Reviewed-on: https://chromium-review.googlesource.com/1337169
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Hsinyu Chao <hychao@chromium.org>
Reviewed-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-by: Per Ã…hgren <peah@chromium.org>
diff --git a/absl/CMakeLists.txt b/absl/CMakeLists.txt
index 689f64e..1d09b19 100644
--- a/absl/CMakeLists.txt
+++ b/absl/CMakeLists.txt
@@ -20,6 +20,7 @@
 add_subdirectory(algorithm)
 add_subdirectory(container)
 add_subdirectory(debugging)
+add_subdirectory(hash)
 add_subdirectory(memory)
 add_subdirectory(meta)
 add_subdirectory(numeric)
diff --git a/absl/algorithm/container.h b/absl/algorithm/container.h
index 6af8c09..53ab156 100644
--- a/absl/algorithm/container.h
+++ b/absl/algorithm/container.h
@@ -494,7 +494,7 @@
 // Container-based version of the <algorithm> `std::move()` function to move
 // a container's elements into an iterator.
 template <typename C, typename OutputIterator>
-OutputIterator c_move(C& src, OutputIterator dest) {
+OutputIterator c_move(C&& src, OutputIterator dest) {
   return std::move(container_algorithm_internal::c_begin(src),
                    container_algorithm_internal::c_end(src), dest);
 }
diff --git a/absl/algorithm/container_test.cc b/absl/algorithm/container_test.cc
index de66f14..1502b17 100644
--- a/absl/algorithm/container_test.cc
+++ b/absl/algorithm/container_test.cc
@@ -636,6 +636,21 @@
                                 Pointee(5)));
 }
 
+TEST(MutatingTest, MoveWithRvalue) {
+  auto MakeRValueSrc = [] {
+    std::vector<std::unique_ptr<int>> src;
+    src.emplace_back(absl::make_unique<int>(1));
+    src.emplace_back(absl::make_unique<int>(2));
+    src.emplace_back(absl::make_unique<int>(3));
+    return src;
+  };
+
+  std::vector<std::unique_ptr<int>> dest = MakeRValueSrc();
+  absl::c_move(MakeRValueSrc(), std::back_inserter(dest));
+  EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(1),
+                                Pointee(2), Pointee(3)));
+}
+
 TEST(MutatingTest, SwapRanges) {
   std::vector<int> odds = {2, 4, 6};
   std::vector<int> evens = {1, 3, 5};
diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel
index 06d092e..f7d8101 100644
--- a/absl/base/BUILD.bazel
+++ b/absl/base/BUILD.bazel
@@ -19,6 +19,7 @@
     "ABSL_DEFAULT_COPTS",
     "ABSL_TEST_COPTS",
     "ABSL_EXCEPTIONS_FLAG",
+    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
 )
 
 package(default_visibility = ["//visibility:public"])
@@ -29,6 +30,7 @@
     name = "spinlock_wait",
     srcs = [
         "internal/spinlock_akaros.inc",
+        "internal/spinlock_linux.inc",
         "internal/spinlock_posix.inc",
         "internal/spinlock_wait.cc",
         "internal/spinlock_win32.inc",
@@ -179,6 +181,7 @@
     srcs = ["internal/throw_delegate.cc"],
     hdrs = ["internal/throw_delegate.h"],
     copts = ABSL_DEFAULT_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     visibility = [
         "//absl:__subpackages__",
     ],
@@ -193,6 +196,7 @@
     name = "throw_delegate_test",
     srcs = ["throw_delegate_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":throw_delegate",
         "@com_google_googletest//:gtest_main",
@@ -225,6 +229,7 @@
     srcs = ["internal/exception_safety_testing.cc"],
     hdrs = ["internal/exception_safety_testing.h"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":base",
         ":config",
@@ -241,6 +246,7 @@
     name = "exception_safety_testing_test",
     srcs = ["exception_safety_testing_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":exception_safety_testing",
         "//absl/memory",
@@ -299,6 +305,7 @@
     size = "medium",
     srcs = ["spinlock_test_common.cc"],
     copts = ABSL_TEST_COPTS,
+    tags = ["no_test_wasm"],
     deps = [
         ":base",
         ":core_headers",
@@ -337,6 +344,9 @@
     name = "config_test",
     srcs = ["config_test.cc"],
     copts = ABSL_TEST_COPTS,
+    tags = [
+        "no_test_wasm",
+    ],
     deps = [
         ":config",
         "//absl/synchronization:thread_pool",
@@ -348,6 +358,9 @@
     name = "call_once_test",
     srcs = ["call_once_test.cc"],
     copts = ABSL_TEST_COPTS,
+    tags = [
+        "no_test_wasm",
+    ],
     deps = [
         ":base",
         ":core_headers",
@@ -401,6 +414,9 @@
         "//absl:windows": [],
         "//conditions:default": ["-pthread"],
     }),
+    tags = [
+        "no_test_wasm",
+    ],
     deps = [
         ":base",
         ":core_headers",
@@ -421,3 +437,23 @@
         "@com_github_google_benchmark//:benchmark_main",
     ],
 )
+
+cc_library(
+    name = "bits",
+    hdrs = ["internal/bits.h"],
+    visibility = [
+        "//absl:__subpackages__",
+    ],
+    deps = [":core_headers"],
+)
+
+cc_test(
+    name = "bits_test",
+    size = "small",
+    srcs = ["internal/bits_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":bits",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
diff --git a/absl/base/BUILD.gn b/absl/base/BUILD.gn
index a714656..6c540f3 100644
--- a/absl/base/BUILD.gn
+++ b/absl/base/BUILD.gn
@@ -23,6 +23,7 @@
   public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
   sources = [
     "internal/spinlock_akaros.inc",
+    "internal/spinlock_linux.inc",
     "internal/spinlock_posix.inc",
     "internal/spinlock_wait.cc",
     "internal/spinlock_win32.inc",
@@ -71,6 +72,7 @@
   public = [
     "dynamic_annotations.h",
   ]
+
   # Abseil's dynamic annotations are only visible inside Abseil because
   # their usage is deprecated in Chromium (see README.chromium for more info).
   visibility = []
@@ -296,3 +298,19 @@
     ":core_headers",
   ]
 }
+
+source_set("bits") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public = [
+    "internal/bits.h",
+  ]
+  deps = [
+    ":core_headers",
+  ]
+  visibility = []
+  visibility += [ "../*" ]
+}
diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt
index 01d2af0..04a6eb3 100644
--- a/absl/base/CMakeLists.txt
+++ b/absl/base/CMakeLists.txt
@@ -31,6 +31,7 @@
 
 list(APPEND BASE_INTERNAL_HEADERS
   "internal/atomic_hook.h"
+  "internal/bits.h"
   "internal/cycleclock.h"
   "internal/direct_mmap.h"
   "internal/endian.h"
diff --git a/absl/base/attributes.h b/absl/base/attributes.h
index b1883b6..e850022 100644
--- a/absl/base/attributes.h
+++ b/absl/base/attributes.h
@@ -100,7 +100,7 @@
 // ABSL_PRINTF_ATTRIBUTE
 // ABSL_SCANF_ATTRIBUTE
 //
-// Tells the compiler to perform `printf` format std::string checking if the
+// Tells the compiler to perform `printf` format string checking if the
 // compiler supports it; see the 'format' attribute in
 // <http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Function-Attributes.html>.
 //
@@ -494,14 +494,27 @@
 #define ABSL_XRAY_LOG_ARGS(N)
 #endif
 
+// ABSL_ATTRIBUTE_REINITIALIZES
+//
+// Indicates that a member function reinitializes the entire object to a known
+// state, independent of the previous state of the object.
+//
+// The clang-tidy check bugprone-use-after-move allows member functions marked
+// with this attribute to be called on objects that have been moved from;
+// without the attribute, this would result in a use-after-move warning.
+#if ABSL_HAVE_CPP_ATTRIBUTE(clang::reinitializes)
+#define ABSL_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
+#else
+#define ABSL_ATTRIBUTE_REINITIALIZES
+#endif
+
 // -----------------------------------------------------------------------------
 // Variable Attributes
 // -----------------------------------------------------------------------------
 
 // ABSL_ATTRIBUTE_UNUSED
 //
-// Prevents the compiler from complaining about or optimizing away variables
-// that appear unused.
+// Prevents the compiler from complaining about variables that appear unused.
 #if ABSL_HAVE_ATTRIBUTE(unused) || (defined(__GNUC__) && !defined(__clang__))
 #undef ABSL_ATTRIBUTE_UNUSED
 #define ABSL_ATTRIBUTE_UNUSED __attribute__((__unused__))
diff --git a/absl/base/config.h b/absl/base/config.h
index 6890e31..d4eb7d0 100644
--- a/absl/base/config.h
+++ b/absl/base/config.h
@@ -199,7 +199,7 @@
 #define ABSL_HAVE_INTRINSIC_INT128 1
 #elif defined(__CUDACC__)
 // __CUDACC_VER__ is a full version number before CUDA 9, and is defined to a
-// std::string explaining that it has been removed starting with CUDA 9. We use
+// string explaining that it has been removed starting with CUDA 9. We use
 // nested #ifs because there is no short-circuiting in the preprocessor.
 // NOTE: `__CUDACC__` could be undefined while `__CUDACC_VER__` is defined.
 #if __CUDACC_VER__ >= 70000
@@ -414,14 +414,13 @@
 // <string_view>, <variant> is implemented) or higher. Also, `__cplusplus` is
 // not correctly set by MSVC, so we use `_MSVC_LANG` to check the language
 // version.
-// TODO(zhangxy): fix tests before enabling aliasing for `std::any`,
-// `std::string_view`.
+// TODO(zhangxy): fix tests before enabling aliasing for `std::any`.
 #if defined(_MSC_VER) && _MSC_VER >= 1910 && \
     ((defined(_MSVC_LANG) && _MSVC_LANG > 201402) || __cplusplus > 201402)
 // #define ABSL_HAVE_STD_ANY 1
 #define ABSL_HAVE_STD_OPTIONAL 1
 #define ABSL_HAVE_STD_VARIANT 1
-// #define ABSL_HAVE_STD_STRING_VIEW 1
+#define ABSL_HAVE_STD_STRING_VIEW 1
 #endif
 
 #endif  // ABSL_BASE_CONFIG_H_
diff --git a/absl/base/exception_safety_testing_test.cc b/absl/base/exception_safety_testing_test.cc
index 97c8d6f..106bc34 100644
--- a/absl/base/exception_safety_testing_test.cc
+++ b/absl/base/exception_safety_testing_test.cc
@@ -38,7 +38,7 @@
 void ExpectNoThrow(const F& f) {
   try {
     f();
-  } catch (TestException e) {
+  } catch (const TestException& e) {
     ADD_FAILURE() << "Unexpected exception thrown from " << e.what();
   }
 }
@@ -179,7 +179,7 @@
 }
 
 // Tests the operator<< of ThrowingValue by forcing ConstructorTracker to emit
-// a nonfatal failure that contains the std::string representation of the Thrower
+// a nonfatal failure that contains the string representation of the Thrower
 TEST(ThrowingValueTest, StreamOpsOutput) {
   using ::testing::TypeSpec;
   exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
@@ -548,21 +548,21 @@
   // Test that providing operation and inveriants still does not allow for the
   // the invocation of .Test() and .Test(op) because it lacks a factory
   auto without_fac =
-      testing::MakeExceptionSafetyTester().WithOperation(op).WithInvariants(
+      testing::MakeExceptionSafetyTester().WithOperation(op).WithContracts(
           inv, testing::strong_guarantee);
   EXPECT_FALSE(HasNullaryTest(without_fac));
   EXPECT_FALSE(HasUnaryTest(without_fac));
 
-  // Test that providing invariants and factory allows the invocation of
+  // Test that providing contracts and factory allows the invocation of
   // .Test(op) but does not allow for .Test() because it lacks an operation
   auto without_op = testing::MakeExceptionSafetyTester()
-                        .WithInvariants(inv, testing::strong_guarantee)
+                        .WithContracts(inv, testing::strong_guarantee)
                         .WithFactory(fac);
   EXPECT_FALSE(HasNullaryTest(without_op));
   EXPECT_TRUE(HasUnaryTest(without_op));
 
   // Test that providing operation and factory still does not allow for the
-  // the invocation of .Test() and .Test(op) because it lacks invariants
+  // the invocation of .Test() and .Test(op) because it lacks contracts
   auto without_inv =
       testing::MakeExceptionSafetyTester().WithOperation(op).WithFactory(fac);
   EXPECT_FALSE(HasNullaryTest(without_inv));
@@ -577,7 +577,7 @@
 
 void ExampleFunctionOperation(ExampleStruct*) {}
 
-testing::AssertionResult ExampleFunctionInvariant(ExampleStruct*) {
+testing::AssertionResult ExampleFunctionContract(ExampleStruct*) {
   return testing::AssertionSuccess();
 }
 
@@ -593,16 +593,16 @@
 
 struct {
   testing::AssertionResult operator()(ExampleStruct* example_struct) const {
-    return ExampleFunctionInvariant(example_struct);
+    return ExampleFunctionContract(example_struct);
   }
-} example_struct_invariant;
+} example_struct_contract;
 
 auto example_lambda_factory = []() { return ExampleFunctionFactory(); };
 
 auto example_lambda_operation = [](ExampleStruct*) {};
 
-auto example_lambda_invariant = [](ExampleStruct* example_struct) {
-  return ExampleFunctionInvariant(example_struct);
+auto example_lambda_contract = [](ExampleStruct* example_struct) {
+  return ExampleFunctionContract(example_struct);
 };
 
 // Testing that function references, pointers, structs with operator() and
@@ -612,28 +612,28 @@
   EXPECT_TRUE(testing::MakeExceptionSafetyTester()
                   .WithFactory(ExampleFunctionFactory)
                   .WithOperation(ExampleFunctionOperation)
-                  .WithInvariants(ExampleFunctionInvariant)
+                  .WithContracts(ExampleFunctionContract)
                   .Test());
 
   // function pointer
   EXPECT_TRUE(testing::MakeExceptionSafetyTester()
                   .WithFactory(&ExampleFunctionFactory)
                   .WithOperation(&ExampleFunctionOperation)
-                  .WithInvariants(&ExampleFunctionInvariant)
+                  .WithContracts(&ExampleFunctionContract)
                   .Test());
 
   // struct
   EXPECT_TRUE(testing::MakeExceptionSafetyTester()
                   .WithFactory(example_struct_factory)
                   .WithOperation(example_struct_operation)
-                  .WithInvariants(example_struct_invariant)
+                  .WithContracts(example_struct_contract)
                   .Test());
 
   // lambda
   EXPECT_TRUE(testing::MakeExceptionSafetyTester()
                   .WithFactory(example_lambda_factory)
                   .WithOperation(example_lambda_operation)
-                  .WithInvariants(example_lambda_invariant)
+                  .WithContracts(example_lambda_contract)
                   .Test());
 }
 
@@ -658,9 +658,9 @@
 } invoker;
 
 auto tester =
-    testing::MakeExceptionSafetyTester().WithOperation(invoker).WithInvariants(
+    testing::MakeExceptionSafetyTester().WithOperation(invoker).WithContracts(
         CheckNonNegativeInvariants);
-auto strong_tester = tester.WithInvariants(testing::strong_guarantee);
+auto strong_tester = tester.WithContracts(testing::strong_guarantee);
 
 struct FailsBasicGuarantee : public NonNegative {
   void operator()() {
@@ -690,7 +690,7 @@
   EXPECT_FALSE(strong_tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
 }
 
-struct BasicGuaranteeWithExtraInvariants : public NonNegative {
+struct BasicGuaranteeWithExtraContracts : public NonNegative {
   // After operator(), i is incremented.  If operator() throws, i is set to 9999
   void operator()() {
     int old_i = i;
@@ -701,21 +701,21 @@
 
   static constexpr int kExceptionSentinel = 9999;
 };
-constexpr int BasicGuaranteeWithExtraInvariants::kExceptionSentinel;
+constexpr int BasicGuaranteeWithExtraContracts::kExceptionSentinel;
 
-TEST(ExceptionCheckTest, BasicGuaranteeWithExtraInvariants) {
+TEST(ExceptionCheckTest, BasicGuaranteeWithExtraContracts) {
   auto tester_with_val =
-      tester.WithInitialValue(BasicGuaranteeWithExtraInvariants{});
+      tester.WithInitialValue(BasicGuaranteeWithExtraContracts{});
   EXPECT_TRUE(tester_with_val.Test());
   EXPECT_TRUE(
       tester_with_val
-          .WithInvariants([](BasicGuaranteeWithExtraInvariants* o) {
-            if (o->i == BasicGuaranteeWithExtraInvariants::kExceptionSentinel) {
+          .WithContracts([](BasicGuaranteeWithExtraContracts* o) {
+            if (o->i == BasicGuaranteeWithExtraContracts::kExceptionSentinel) {
               return testing::AssertionSuccess();
             }
             return testing::AssertionFailure()
                    << "i should be "
-                   << BasicGuaranteeWithExtraInvariants::kExceptionSentinel
+                   << BasicGuaranteeWithExtraContracts::kExceptionSentinel
                    << ", but is " << o->i;
           })
           .Test());
@@ -740,7 +740,7 @@
   void reset() { i = 0; }
 };
 
-testing::AssertionResult CheckHasResetInvariants(HasReset* h) {
+testing::AssertionResult CheckHasResetContracts(HasReset* h) {
   h->reset();
   return testing::AssertionResult(h->i == 0);
 }
@@ -759,14 +759,14 @@
   };
 
   EXPECT_FALSE(tester.WithInitialValue(FollowsBasicGuarantee{})
-                   .WithInvariants(set_to_1000, is_1000)
+                   .WithContracts(set_to_1000, is_1000)
                    .Test());
   EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{})
-                  .WithInvariants(increment)
+                  .WithContracts(increment)
                   .Test());
   EXPECT_TRUE(testing::MakeExceptionSafetyTester()
                   .WithInitialValue(HasReset{})
-                  .WithInvariants(CheckHasResetInvariants)
+                  .WithContracts(CheckHasResetContracts)
                   .Test(invoker));
 }
 
@@ -799,7 +799,7 @@
     return testing::AssertionResult(nec->i == NonEqualityComparable().i);
   };
   auto strong_nec_tester = tester.WithInitialValue(NonEqualityComparable{})
-                               .WithInvariants(nec_is_strong);
+                               .WithContracts(nec_is_strong);
 
   EXPECT_TRUE(strong_nec_tester.Test());
   EXPECT_FALSE(strong_nec_tester.Test(
@@ -833,14 +833,14 @@
   testing::AssertionResult operator()(ExhaustivenessTester<T>*) const {
     return testing::AssertionSuccess();
   }
-} CheckExhaustivenessTesterInvariants;
+} CheckExhaustivenessTesterContracts;
 
 template <typename T>
 unsigned char ExhaustivenessTester<T>::successes = 0;
 
 TEST(ExceptionCheckTest, Exhaustiveness) {
   auto exhaust_tester = testing::MakeExceptionSafetyTester()
-                            .WithInvariants(CheckExhaustivenessTesterInvariants)
+                            .WithContracts(CheckExhaustivenessTesterContracts)
                             .WithOperation(invoker);
 
   EXPECT_TRUE(
@@ -849,7 +849,7 @@
 
   EXPECT_TRUE(
       exhaust_tester.WithInitialValue(ExhaustivenessTester<ThrowingValue<>>{})
-          .WithInvariants(testing::strong_guarantee)
+          .WithContracts(testing::strong_guarantee)
           .Test());
   EXPECT_EQ(ExhaustivenessTester<ThrowingValue<>>::successes, 0xF);
 }
@@ -931,8 +931,8 @@
 }
 
 TEST(ThrowingAllocatorTraitsTest, Assignablility) {
-  EXPECT_TRUE(std::is_move_assignable<ThrowingAllocator<int>>::value);
-  EXPECT_TRUE(std::is_copy_assignable<ThrowingAllocator<int>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<ThrowingAllocator<int>>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<ThrowingAllocator<int>>::value);
   EXPECT_TRUE(std::is_nothrow_move_assignable<ThrowingAllocator<int>>::value);
   EXPECT_TRUE(std::is_nothrow_copy_assignable<ThrowingAllocator<int>>::value);
 }
diff --git a/absl/base/internal/bits.h b/absl/base/internal/bits.h
new file mode 100644
index 0000000..bc7faae
--- /dev/null
+++ b/absl/base/internal/bits.h
@@ -0,0 +1,193 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_BASE_INTERNAL_BITS_H_
+#define ABSL_BASE_INTERNAL_BITS_H_
+
+// This file contains bitwise ops which are implementation details of various
+// absl libraries.
+
+#include <cstdint>
+
+// Clang on Windows has __builtin_clzll; otherwise we need to use the
+// windows intrinsic functions.
+#if defined(_MSC_VER)
+#include <intrin.h>
+#if defined(_M_X64)
+#pragma intrinsic(_BitScanReverse64)
+#pragma intrinsic(_BitScanForward64)
+#endif
+#pragma intrinsic(_BitScanReverse)
+#pragma intrinsic(_BitScanForward)
+#endif
+
+#include "absl/base/attributes.h"
+
+#if defined(_MSC_VER)
+// We can achieve something similar to attribute((always_inline)) with MSVC by
+// using the __forceinline keyword, however this is not perfect. MSVC is
+// much less aggressive about inlining, and even with the __forceinline keyword.
+#define ABSL_BASE_INTERNAL_FORCEINLINE __forceinline
+#else
+// Use default attribute inline.
+#define ABSL_BASE_INTERNAL_FORCEINLINE inline ABSL_ATTRIBUTE_ALWAYS_INLINE
+#endif
+
+
+namespace absl {
+namespace base_internal {
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n) {
+  int zeroes = 60;
+  if (n >> 32) zeroes -= 32, n >>= 32;
+  if (n >> 16) zeroes -= 16, n >>= 16;
+  if (n >> 8) zeroes -= 8, n >>= 8;
+  if (n >> 4) zeroes -= 4, n >>= 4;
+  return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n) {
+#if defined(_MSC_VER) && defined(_M_X64)
+  // MSVC does not have __buitin_clzll. Use _BitScanReverse64.
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  if (_BitScanReverse64(&result, n)) {
+    return 63 - result;
+  }
+  return 64;
+#elif defined(_MSC_VER)
+  // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  if ((n >> 32) && _BitScanReverse(&result, n >> 32)) {
+    return 31 - result;
+  }
+  if (_BitScanReverse(&result, n)) {
+    return 63 - result;
+  }
+  return 64;
+#elif defined(__GNUC__)
+  // Use __builtin_clzll, which uses the following instructions:
+  //  x86: bsr
+  //  ARM64: clz
+  //  PPC: cntlzd
+  static_assert(sizeof(unsigned long long) == sizeof(n),  // NOLINT(runtime/int)
+                "__builtin_clzll does not take 64-bit arg");
+
+  // Handle 0 as a special case because __builtin_clzll(0) is undefined.
+  if (n == 0) {
+    return 64;
+  }
+  return __builtin_clzll(n);
+#else
+  return CountLeadingZeros64Slow(n);
+#endif
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n) {
+  int zeroes = 28;
+  if (n >> 16) zeroes -= 16, n >>= 16;
+  if (n >> 8) zeroes -= 8, n >>= 8;
+  if (n >> 4) zeroes -= 4, n >>= 4;
+  return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32(uint32_t n) {
+#if defined(_MSC_VER)
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  if (_BitScanReverse(&result, n)) {
+    return 31 - result;
+  }
+  return 32;
+#elif defined(__GNUC__)
+  // Use __builtin_clz, which uses the following instructions:
+  //  x86: bsr
+  //  ARM64: clz
+  //  PPC: cntlzd
+  static_assert(sizeof(int) == sizeof(n),
+                "__builtin_clz does not take 32-bit arg");
+
+  // Handle 0 as a special case because __builtin_clz(0) is undefined.
+  if (n == 0) {
+    return 32;
+  }
+  return __builtin_clz(n);
+#else
+  return CountLeadingZeros32Slow(n);
+#endif
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64Slow(uint64_t n) {
+  int c = 63;
+  n &= ~n + 1;
+  if (n & 0x00000000FFFFFFFF) c -= 32;
+  if (n & 0x0000FFFF0000FFFF) c -= 16;
+  if (n & 0x00FF00FF00FF00FF) c -= 8;
+  if (n & 0x0F0F0F0F0F0F0F0F) c -= 4;
+  if (n & 0x3333333333333333) c -= 2;
+  if (n & 0x5555555555555555) c -= 1;
+  return c;
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64(uint64_t n) {
+#if defined(_MSC_VER) && defined(_M_X64)
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  _BitScanForward64(&result, n);
+  return result;
+#elif defined(_MSC_VER)
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  if (static_cast<uint32_t>(n) == 0) {
+    _BitScanForward(&result, n >> 32);
+    return result + 32;
+  }
+  _BitScanForward(&result, n);
+  return result;
+#elif defined(__GNUC__)
+  static_assert(sizeof(unsigned long long) == sizeof(n),  // NOLINT(runtime/int)
+                "__builtin_ctzll does not take 64-bit arg");
+  return __builtin_ctzll(n);
+#else
+  return CountTrailingZerosNonZero64Slow(n);
+#endif
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32Slow(uint32_t n) {
+  int c = 31;
+  n &= ~n + 1;
+  if (n & 0x0000FFFF) c -= 16;
+  if (n & 0x00FF00FF) c -= 8;
+  if (n & 0x0F0F0F0F) c -= 4;
+  if (n & 0x33333333) c -= 2;
+  if (n & 0x55555555) c -= 1;
+  return c;
+}
+
+ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32(uint32_t n) {
+#if defined(_MSC_VER)
+  unsigned long result = 0;  // NOLINT(runtime/int)
+  _BitScanForward(&result, n);
+  return result;
+#elif defined(__GNUC__)
+  static_assert(sizeof(int) == sizeof(n),
+                "__builtin_ctz does not take 32-bit arg");
+  return __builtin_ctz(n);
+#else
+  return CountTrailingZerosNonZero32Slow(n);
+#endif
+}
+
+#undef ABSL_BASE_INTERNAL_FORCEINLINE
+
+}  // namespace base_internal
+}  // namespace absl
+
+#endif  // ABSL_BASE_INTERNAL_BITS_H_
diff --git a/absl/base/internal/bits_test.cc b/absl/base/internal/bits_test.cc
new file mode 100644
index 0000000..e5d991d
--- /dev/null
+++ b/absl/base/internal/bits_test.cc
@@ -0,0 +1,97 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/base/internal/bits.h"
+
+#include "gtest/gtest.h"
+
+namespace {
+
+int CLZ64(uint64_t n) {
+  int fast = absl::base_internal::CountLeadingZeros64(n);
+  int slow = absl::base_internal::CountLeadingZeros64Slow(n);
+  EXPECT_EQ(fast, slow) << n;
+  return fast;
+}
+
+TEST(BitsTest, CountLeadingZeros64) {
+  EXPECT_EQ(64, CLZ64(uint64_t{}));
+  EXPECT_EQ(0, CLZ64(~uint64_t{}));
+
+  for (int index = 0; index < 64; index++) {
+    uint64_t x = static_cast<uint64_t>(1) << index;
+    const auto cnt = 63 - index;
+    ASSERT_EQ(cnt, CLZ64(x)) << index;
+    ASSERT_EQ(cnt, CLZ64(x + x - 1)) << index;
+  }
+}
+
+int CLZ32(uint32_t n) {
+  int fast = absl::base_internal::CountLeadingZeros32(n);
+  int slow = absl::base_internal::CountLeadingZeros32Slow(n);
+  EXPECT_EQ(fast, slow) << n;
+  return fast;
+}
+
+TEST(BitsTest, CountLeadingZeros32) {
+  EXPECT_EQ(32, CLZ32(uint32_t{}));
+  EXPECT_EQ(0, CLZ32(~uint32_t{}));
+
+  for (int index = 0; index < 32; index++) {
+    uint32_t x = static_cast<uint32_t>(1) << index;
+    const auto cnt = 31 - index;
+    ASSERT_EQ(cnt, CLZ32(x)) << index;
+    ASSERT_EQ(cnt, CLZ32(x + x - 1)) << index;
+    ASSERT_EQ(CLZ64(x), CLZ32(x) + 32);
+  }
+}
+
+int CTZ64(uint64_t n) {
+  int fast = absl::base_internal::CountTrailingZerosNonZero64(n);
+  int slow = absl::base_internal::CountTrailingZerosNonZero64Slow(n);
+  EXPECT_EQ(fast, slow) << n;
+  return fast;
+}
+
+TEST(BitsTest, CountTrailingZerosNonZero64) {
+  EXPECT_EQ(0, CTZ64(~uint64_t{}));
+
+  for (int index = 0; index < 64; index++) {
+    uint64_t x = static_cast<uint64_t>(1) << index;
+    const auto cnt = index;
+    ASSERT_EQ(cnt, CTZ64(x)) << index;
+    ASSERT_EQ(cnt, CTZ64(~(x - 1))) << index;
+  }
+}
+
+int CTZ32(uint32_t n) {
+  int fast = absl::base_internal::CountTrailingZerosNonZero32(n);
+  int slow = absl::base_internal::CountTrailingZerosNonZero32Slow(n);
+  EXPECT_EQ(fast, slow) << n;
+  return fast;
+}
+
+TEST(BitsTest, CountTrailingZerosNonZero32) {
+  EXPECT_EQ(0, CTZ32(~uint32_t{}));
+
+  for (int index = 0; index < 32; index++) {
+    uint32_t x = static_cast<uint32_t>(1) << index;
+    const auto cnt = index;
+    ASSERT_EQ(cnt, CTZ32(x)) << index;
+    ASSERT_EQ(cnt, CTZ32(~(x - 1))) << index;
+  }
+}
+
+
+}  // namespace
diff --git a/absl/base/internal/exception_safety_testing.h b/absl/base/internal/exception_safety_testing.h
index 8c2f509..5665a1b 100644
--- a/absl/base/internal/exception_safety_testing.h
+++ b/absl/base/internal/exception_safety_testing.h
@@ -191,19 +191,19 @@
   ~TrackedObject() noexcept { ConstructorTracker::ObjectDestructed(this); }
 };
 
-template <typename Factory, typename Operation, typename Invariant>
-absl::optional<testing::AssertionResult> TestSingleInvariantAtCountdownImpl(
+template <typename Factory, typename Operation, typename Contract>
+absl::optional<testing::AssertionResult> TestSingleContractAtCountdownImpl(
     const Factory& factory, const Operation& operation, int count,
-    const Invariant& invariant) {
+    const Contract& contract) {
   auto t_ptr = factory();
   absl::optional<testing::AssertionResult> current_res;
   SetCountdown(count);
   try {
     operation(t_ptr.get());
   } catch (const exceptions_internal::TestException& e) {
-    current_res.emplace(invariant(t_ptr.get()));
+    current_res.emplace(contract(t_ptr.get()));
     if (!current_res.value()) {
-      *current_res << e.what() << " failed invariant check";
+      *current_res << e.what() << " failed contract check";
     }
   }
   UnsetCountdown();
@@ -211,22 +211,22 @@
 }
 
 template <typename Factory, typename Operation>
-absl::optional<testing::AssertionResult> TestSingleInvariantAtCountdownImpl(
+absl::optional<testing::AssertionResult> TestSingleContractAtCountdownImpl(
     const Factory& factory, const Operation& operation, int count,
     StrongGuaranteeTagType) {
   using TPtr = typename decltype(factory())::pointer;
   auto t_is_strong = [&](TPtr t) { return *t == *factory(); };
-  return TestSingleInvariantAtCountdownImpl(factory, operation, count,
-                                            t_is_strong);
+  return TestSingleContractAtCountdownImpl(factory, operation, count,
+                                           t_is_strong);
 }
 
-template <typename Factory, typename Operation, typename Invariant>
-int TestSingleInvariantAtCountdown(
+template <typename Factory, typename Operation, typename Contract>
+int TestSingleContractAtCountdown(
     const Factory& factory, const Operation& operation, int count,
-    const Invariant& invariant,
+    const Contract& contract,
     absl::optional<testing::AssertionResult>* reduced_res) {
   // If reduced_res is empty, it means the current call to
-  // TestSingleInvariantAtCountdown(...) is the first test being run so we do
+  // TestSingleContractAtCountdown(...) is the first test being run so we do
   // want to run it. Alternatively, if it's not empty (meaning a previous test
   // has run) we want to check if it passed. If the previous test did pass, we
   // want to contine running tests so we do want to run the current one. If it
@@ -234,22 +234,22 @@
   // output. If that's the case, we do not run the current test and instead we
   // simply return.
   if (!reduced_res->has_value() || reduced_res->value()) {
-    *reduced_res = TestSingleInvariantAtCountdownImpl(factory, operation, count,
-                                                      invariant);
+    *reduced_res =
+        TestSingleContractAtCountdownImpl(factory, operation, count, contract);
   }
   return 0;
 }
 
-template <typename Factory, typename Operation, typename... Invariants>
-inline absl::optional<testing::AssertionResult> TestAllInvariantsAtCountdown(
+template <typename Factory, typename Operation, typename... Contracts>
+inline absl::optional<testing::AssertionResult> TestAllContractsAtCountdown(
     const Factory& factory, const Operation& operation, int count,
-    const Invariants&... invariants) {
+    const Contracts&... contracts) {
   absl::optional<testing::AssertionResult> reduced_res;
 
   // Run each checker, short circuiting after the first failure
   int dummy[] = {
-      0, (TestSingleInvariantAtCountdown(factory, operation, count, invariants,
-                                         &reduced_res))...};
+      0, (TestSingleContractAtCountdown(factory, operation, count, contracts,
+                                        &reduced_res))...};
   static_cast<void>(dummy);
   return reduced_res;
 }
@@ -858,7 +858,7 @@
   try {
     operation();
     return testing::AssertionSuccess();
-  } catch (exceptions_internal::TestException) {
+  } catch (const exceptions_internal::TestException&) {
     return testing::AssertionFailure()
            << "TestException thrown during call to operation() when nothrow "
               "guarantee was expected.";
@@ -884,15 +884,15 @@
   T t_;
 };
 
-template <size_t LazyInvariantsCount, typename LazyFactory,
+template <size_t LazyContractsCount, typename LazyFactory,
           typename LazyOperation>
 using EnableIfTestable = typename absl::enable_if_t<
-    LazyInvariantsCount != 0 &&
+    LazyContractsCount != 0 &&
     !std::is_same<LazyFactory, UninitializedT>::value &&
     !std::is_same<LazyOperation, UninitializedT>::value>;
 
 template <typename Factory = UninitializedT,
-          typename Operation = UninitializedT, typename... Invariants>
+          typename Operation = UninitializedT, typename... Contracts>
 class ExceptionSafetyTester;
 
 }  // namespace exceptions_internal
@@ -903,7 +903,7 @@
 
 /*
  * Builds a tester object that tests if performing a operation on a T follows
- * exception safety guarantees. Verification is done via invariant assertion
+ * exception safety guarantees. Verification is done via contract assertion
  * callbacks applied to T instances post-throw.
  *
  * Template parameters for ExceptionSafetyTester:
@@ -921,18 +921,18 @@
  *   fresh T instance so it's free to modify and destroy the T instances as it
  *   pleases.
  *
- * - Invariants...: The invariant assertion callback objects (passed in via
- *   tester.WithInvariants(...)) must be invocable with the signature
+ * - Contracts...: The contract assertion callback objects (passed in via
+ *   tester.WithContracts(...)) must be invocable with the signature
  *   `testing::AssertionResult operator()(T*) const` where T is the type being
- *   tested. Invariant assertion callbacks are provided T instances post-throw.
- *   They must return testing::AssertionSuccess when the type invariants of the
- *   provided T instance hold. If the type invariants of the T instance do not
+ *   tested. Contract assertion callbacks are provided T instances post-throw.
+ *   They must return testing::AssertionSuccess when the type contracts of the
+ *   provided T instance hold. If the type contracts of the T instance do not
  *   hold, they must return testing::AssertionFailure. Execution order of
- *   Invariants... is unspecified. They will each individually get a fresh T
+ *   Contracts... is unspecified. They will each individually get a fresh T
  *   instance so they are free to modify and destroy the T instances as they
  *   please.
  */
-template <typename Factory, typename Operation, typename... Invariants>
+template <typename Factory, typename Operation, typename... Contracts>
 class ExceptionSafetyTester {
  public:
   /*
@@ -948,7 +948,7 @@
    *   tester.WithFactory(...).
    */
   template <typename T>
-  ExceptionSafetyTester<DefaultFactory<T>, Operation, Invariants...>
+  ExceptionSafetyTester<DefaultFactory<T>, Operation, Contracts...>
   WithInitialValue(const T& t) const {
     return WithFactory(DefaultFactory<T>(t));
   }
@@ -961,9 +961,9 @@
    * method tester.WithInitialValue(...).
    */
   template <typename NewFactory>
-  ExceptionSafetyTester<absl::decay_t<NewFactory>, Operation, Invariants...>
+  ExceptionSafetyTester<absl::decay_t<NewFactory>, Operation, Contracts...>
   WithFactory(const NewFactory& new_factory) const {
-    return {new_factory, operation_, invariants_};
+    return {new_factory, operation_, contracts_};
   }
 
   /*
@@ -972,39 +972,39 @@
    * tester.
    */
   template <typename NewOperation>
-  ExceptionSafetyTester<Factory, absl::decay_t<NewOperation>, Invariants...>
+  ExceptionSafetyTester<Factory, absl::decay_t<NewOperation>, Contracts...>
   WithOperation(const NewOperation& new_operation) const {
-    return {factory_, new_operation, invariants_};
+    return {factory_, new_operation, contracts_};
   }
 
   /*
-   * Returns a new ExceptionSafetyTester with the provided MoreInvariants...
-   * combined with the Invariants... that were already included in the instance
-   * on which the method was called. Invariants... cannot be removed or replaced
+   * Returns a new ExceptionSafetyTester with the provided MoreContracts...
+   * combined with the Contracts... that were already included in the instance
+   * on which the method was called. Contracts... cannot be removed or replaced
    * once added to an ExceptionSafetyTester instance. A fresh object must be
-   * created in order to get an empty Invariants... list.
+   * created in order to get an empty Contracts... list.
    *
-   * In addition to passing in custom invariant assertion callbacks, this method
+   * In addition to passing in custom contract assertion callbacks, this method
    * accepts `testing::strong_guarantee` as an argument which checks T instances
    * post-throw against freshly created T instances via operator== to verify
    * that any state changes made during the execution of the operation were
    * properly rolled back.
    */
-  template <typename... MoreInvariants>
-  ExceptionSafetyTester<Factory, Operation, Invariants...,
-                        absl::decay_t<MoreInvariants>...>
-  WithInvariants(const MoreInvariants&... more_invariants) const {
-    return {factory_, operation_,
-            std::tuple_cat(invariants_,
-                           std::tuple<absl::decay_t<MoreInvariants>...>(
-                               more_invariants...))};
+  template <typename... MoreContracts>
+  ExceptionSafetyTester<Factory, Operation, Contracts...,
+                        absl::decay_t<MoreContracts>...>
+  WithContracts(const MoreContracts&... more_contracts) const {
+    return {
+        factory_, operation_,
+        std::tuple_cat(contracts_, std::tuple<absl::decay_t<MoreContracts>...>(
+                                       more_contracts...))};
   }
 
   /*
    * Returns a testing::AssertionResult that is the reduced result of the
    * exception safety algorithm. The algorithm short circuits and returns
-   * AssertionFailure after the first invariant callback returns an
-   * AssertionFailure. Otherwise, if all invariant callbacks return an
+   * AssertionFailure after the first contract callback returns an
+   * AssertionFailure. Otherwise, if all contract callbacks return an
    * AssertionSuccess, the reduced result is AssertionSuccess.
    *
    * The passed-in testable operation will not be saved in a new tester instance
@@ -1013,33 +1013,33 @@
    *
    * Preconditions for tester.Test(const NewOperation& new_operation):
    *
-   * - May only be called after at least one invariant assertion callback and a
+   * - May only be called after at least one contract assertion callback and a
    *   factory or initial value have been provided.
    */
   template <
       typename NewOperation,
-      typename = EnableIfTestable<sizeof...(Invariants), Factory, NewOperation>>
+      typename = EnableIfTestable<sizeof...(Contracts), Factory, NewOperation>>
   testing::AssertionResult Test(const NewOperation& new_operation) const {
-    return TestImpl(new_operation, absl::index_sequence_for<Invariants...>());
+    return TestImpl(new_operation, absl::index_sequence_for<Contracts...>());
   }
 
   /*
    * Returns a testing::AssertionResult that is the reduced result of the
    * exception safety algorithm. The algorithm short circuits and returns
-   * AssertionFailure after the first invariant callback returns an
-   * AssertionFailure. Otherwise, if all invariant callbacks return an
+   * AssertionFailure after the first contract callback returns an
+   * AssertionFailure. Otherwise, if all contract callbacks return an
    * AssertionSuccess, the reduced result is AssertionSuccess.
    *
    * Preconditions for tester.Test():
    *
-   * - May only be called after at least one invariant assertion callback, a
+   * - May only be called after at least one contract assertion callback, a
    *   factory or initial value and a testable operation have been provided.
    */
-  template <typename LazyOperation = Operation,
-            typename =
-                EnableIfTestable<sizeof...(Invariants), Factory, LazyOperation>>
+  template <
+      typename LazyOperation = Operation,
+      typename = EnableIfTestable<sizeof...(Contracts), Factory, LazyOperation>>
   testing::AssertionResult Test() const {
-    return TestImpl(operation_, absl::index_sequence_for<Invariants...>());
+    return TestImpl(operation_, absl::index_sequence_for<Contracts...>());
   }
 
  private:
@@ -1051,8 +1051,8 @@
   ExceptionSafetyTester() {}
 
   ExceptionSafetyTester(const Factory& f, const Operation& o,
-                        const std::tuple<Invariants...>& i)
-      : factory_(f), operation_(o), invariants_(i) {}
+                        const std::tuple<Contracts...>& i)
+      : factory_(f), operation_(o), contracts_(i) {}
 
   template <typename SelectedOperation, size_t... Indices>
   testing::AssertionResult TestImpl(const SelectedOperation& selected_operation,
@@ -1064,28 +1064,28 @@
 
       // Run the full exception safety test algorithm for the current countdown
       auto reduced_res =
-          TestAllInvariantsAtCountdown(factory_, selected_operation, count,
-                                       std::get<Indices>(invariants_)...);
-      // If there is no value in the optional, no invariants were run because no
+          TestAllContractsAtCountdown(factory_, selected_operation, count,
+                                      std::get<Indices>(contracts_)...);
+      // If there is no value in the optional, no contracts were run because no
       // exception was thrown. This means that the test is complete and the loop
       // can exit successfully.
       if (!reduced_res.has_value()) {
         return testing::AssertionSuccess();
       }
-      // If the optional is not empty and the value is falsy, an invariant check
+      // If the optional is not empty and the value is falsy, an contract check
       // failed so the test must exit to propegate the failure.
       if (!reduced_res.value()) {
         return reduced_res.value();
       }
       // If the optional is not empty and the value is not falsy, it means
-      // exceptions were thrown but the invariants passed so the test must
+      // exceptions were thrown but the contracts passed so the test must
       // continue to run.
     }
   }
 
   Factory factory_;
   Operation operation_;
-  std::tuple<Invariants...> invariants_;
+  std::tuple<Contracts...> contracts_;
 };
 
 }  // namespace exceptions_internal
@@ -1096,7 +1096,7 @@
  * instances of ExceptionSafetyTester.
  *
  * In order to test a T for exception safety, a factory for that T, a testable
- * operation, and at least one invariant callback returning an assertion
+ * operation, and at least one contract callback returning an assertion
  * result must be applied using the respective methods.
  */
 inline exceptions_internal::ExceptionSafetyTester<>
diff --git a/absl/base/internal/low_level_alloc.cc b/absl/base/internal/low_level_alloc.cc
index 0626cd5..159f945 100644
--- a/absl/base/internal/low_level_alloc.cc
+++ b/absl/base/internal/low_level_alloc.cc
@@ -401,16 +401,20 @@
     ABSL_RAW_CHECK(munmap_result != 0,
                    "LowLevelAlloc::DeleteArena: VitualFree failed");
 #else
+#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
     if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) == 0) {
       munmap_result = munmap(region, size);
     } else {
       munmap_result = base_internal::DirectMunmap(region, size);
     }
+#else
+    munmap_result = munmap(region, size);
+#endif  // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
     if (munmap_result != 0) {
       ABSL_RAW_LOG(FATAL, "LowLevelAlloc::DeleteArena: munmap failed: %d",
                    errno);
     }
-#endif
+#endif  // _WIN32
   }
   section.Leave();
   arena->~Arena();
@@ -545,6 +549,7 @@
                                MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
       ABSL_RAW_CHECK(new_pages != nullptr, "VirtualAlloc failed");
 #else
+#ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
       if ((arena->flags & LowLevelAlloc::kAsyncSignalSafe) != 0) {
         new_pages = base_internal::DirectMmap(nullptr, new_pages_size,
             PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
@@ -552,10 +557,15 @@
         new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ,
                          MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
       }
+#else
+      new_pages = mmap(nullptr, new_pages_size, PROT_WRITE | PROT_READ,
+                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+#endif  // ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
       if (new_pages == MAP_FAILED) {
         ABSL_RAW_LOG(FATAL, "mmap error: %d", errno);
       }
-#endif
+
+#endif  // _WIN32
       arena->mu.Lock();
       s = reinterpret_cast<AllocList *>(new_pages);
       s->header.size = new_pages_size;
diff --git a/absl/base/internal/low_level_alloc.h b/absl/base/internal/low_level_alloc.h
index 3c15605..fba9466 100644
--- a/absl/base/internal/low_level_alloc.h
+++ b/absl/base/internal/low_level_alloc.h
@@ -39,10 +39,13 @@
 #define ABSL_LOW_LEVEL_ALLOC_MISSING 1
 #endif
 
-// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows.
+// Using LowLevelAlloc with kAsyncSignalSafe isn't supported on Windows or
+// asm.js / WebAssembly.
+// See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
+// for more information.
 #ifdef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING
 #error ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING cannot be directly set
-#elif defined(_WIN32)
+#elif defined(_WIN32) || defined(__asmjs__) || defined(__wasm__)
 #define ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING 1
 #endif
 
diff --git a/absl/base/internal/raw_logging.h b/absl/base/internal/raw_logging.h
index 67abfd3..79a7bb9 100644
--- a/absl/base/internal/raw_logging.h
+++ b/absl/base/internal/raw_logging.h
@@ -114,7 +114,7 @@
 
 // compile-time function to get the "base" filename, that is, the part of
 // a filename after the last "/" or "\" path separator.  The search starts at
-// the end of the std::string; the second parameter is the length of the std::string.
+// the end of the string; the second parameter is the length of the string.
 constexpr const char* Basename(const char* fname, int offset) {
   return offset == 0 || fname[offset - 1] == '/' || fname[offset - 1] == '\\'
              ? fname + offset
diff --git a/absl/base/internal/spinlock_linux.inc b/absl/base/internal/spinlock_linux.inc
new file mode 100644
index 0000000..94c861d
--- /dev/null
+++ b/absl/base/internal/spinlock_linux.inc
@@ -0,0 +1,72 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// This file is a Linux-specific part of spinlock_wait.cc
+
+#include <linux/futex.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <atomic>
+#include <cerrno>
+#include <climits>
+#include <cstdint>
+#include <ctime>
+
+#include "absl/base/attributes.h"
+
+// The SpinLock lockword is `std::atomic<uint32_t>`. Here we assert that
+// `std::atomic<uint32_t>` is bitwise equivalent of the `int` expected
+// by SYS_futex. We also assume that reads/writes done to the lockword
+// by SYS_futex have rational semantics with regard to the
+// std::atomic<> API. C++ provides no guarantees of these assumptions,
+// but they are believed to hold in practice.
+static_assert(sizeof(std::atomic<uint32_t>) == sizeof(int),
+              "SpinLock lockword has the wrong size for a futex");
+
+// Some Android headers are missing these definitions even though they
+// support these futex operations.
+#ifdef __BIONIC__
+#ifndef SYS_futex
+#define SYS_futex __NR_futex
+#endif
+#ifndef FUTEX_PRIVATE_FLAG
+#define FUTEX_PRIVATE_FLAG 128
+#endif
+#endif
+
+extern "C" {
+
+ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockDelay(
+    std::atomic<uint32_t> *w, uint32_t value, int loop,
+    absl::base_internal::SchedulingMode) {
+  if (loop != 0) {
+    int save_errno = errno;
+    struct timespec tm;
+    tm.tv_sec = 0;
+    // Increase the delay; we expect (but do not rely on) explicit wakeups.
+    // We don't rely on explicit wakeups because we intentionally allow for
+    // a race on the kSpinLockSleeper bit.
+    tm.tv_nsec = 16 * absl::base_internal::SpinLockSuggestedDelayNS(loop);
+    syscall(SYS_futex, w, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, &tm);
+    errno = save_errno;
+  }
+}
+
+ABSL_ATTRIBUTE_WEAK void AbslInternalSpinLockWake(std::atomic<uint32_t> *w,
+                                                  bool all) {
+  syscall(SYS_futex, w, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, all ? INT_MAX : 1, 0);
+}
+
+}  // extern "C"
diff --git a/absl/base/internal/spinlock_wait.cc b/absl/base/internal/spinlock_wait.cc
index 9f6e991..0fde9c0 100644
--- a/absl/base/internal/spinlock_wait.cc
+++ b/absl/base/internal/spinlock_wait.cc
@@ -13,7 +13,7 @@
 // limitations under the License.
 
 // The OS-specific header included below must provide two calls:
-// base::subtle::SpinLockDelay() and base::subtle::SpinLockWake().
+// AbslInternalSpinLockDelay() and AbslInternalSpinLockWake().
 // See spinlock_wait.h for the specs.
 
 #include <atomic>
@@ -23,6 +23,8 @@
 
 #if defined(_WIN32)
 #include "absl/base/internal/spinlock_win32.inc"
+#elif defined(__linux__)
+#include "absl/base/internal/spinlock_linux.inc"
 #elif defined(__akaros__)
 #include "absl/base/internal/spinlock_akaros.inc"
 #else
diff --git a/absl/base/internal/thread_identity.cc b/absl/base/internal/thread_identity.cc
index 678e856..cff9c1b 100644
--- a/absl/base/internal/thread_identity.cc
+++ b/absl/base/internal/thread_identity.cc
@@ -68,6 +68,14 @@
   // NOTE: Not async-safe.  But can be open-coded.
   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
                   reclaimer);
+
+#ifdef __EMSCRIPTEN__
+  // Emscripten PThread implementation does not support signals.
+  // See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
+  // for more information.
+  pthread_setspecific(thread_identity_pthread_key,
+                      reinterpret_cast<void*>(identity));
+#else
   // We must mask signals around the call to setspecific as with current glibc,
   // a concurrent getspecific (needed for GetCurrentThreadIdentityIfPresent())
   // may zero our value.
@@ -81,6 +89,8 @@
   pthread_setspecific(thread_identity_pthread_key,
                       reinterpret_cast<void*>(identity));
   pthread_sigmask(SIG_SETMASK, &curr_signals, nullptr);
+#endif  // !__EMSCRIPTEN__
+
 #elif ABSL_THREAD_IDENTITY_MODE == ABSL_THREAD_IDENTITY_MODE_USE_TLS
   // NOTE: Not async-safe.  But can be open-coded.
   absl::call_once(init_thread_identity_key_once, AllocateThreadIdentityKey,
diff --git a/absl/base/internal/unaligned_access.h b/absl/base/internal/unaligned_access.h
index 5c7517a..f9df3b7 100644
--- a/absl/base/internal/unaligned_access.h
+++ b/absl/base/internal/unaligned_access.h
@@ -65,6 +65,7 @@
 }  // extern "C"
 
 namespace absl {
+namespace base_internal {
 
 inline uint16_t UnalignedLoad16(const void *p) {
   return __sanitizer_unaligned_load16(p);
@@ -90,22 +91,27 @@
   __sanitizer_unaligned_store64(p, v);
 }
 
+}  // namespace base_internal
 }  // namespace absl
 
-#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) (absl::UnalignedLoad16(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) (absl::UnalignedLoad32(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) (absl::UnalignedLoad64(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
+  (absl::base_internal::UnalignedLoad16(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
+  (absl::base_internal::UnalignedLoad32(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
+  (absl::base_internal::UnalignedLoad64(_p))
 
 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
-  (absl::UnalignedStore16(_p, _val))
+  (absl::base_internal::UnalignedStore16(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
-  (absl::UnalignedStore32(_p, _val))
+  (absl::base_internal::UnalignedStore32(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
-  (absl::UnalignedStore64(_p, _val))
+  (absl::base_internal::UnalignedStore64(_p, _val))
 
 #elif defined(UNDEFINED_BEHAVIOR_SANITIZER)
 
 namespace absl {
+namespace base_internal {
 
 inline uint16_t UnalignedLoad16(const void *p) {
   uint16_t t;
@@ -131,18 +137,22 @@
 
 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
 
+}  // namespace base_internal
 }  // namespace absl
 
-#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) (absl::UnalignedLoad16(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) (absl::UnalignedLoad32(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) (absl::UnalignedLoad64(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
+  (absl::base_internal::UnalignedLoad16(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
+  (absl::base_internal::UnalignedLoad32(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
+  (absl::base_internal::UnalignedLoad64(_p))
 
 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
-  (absl::UnalignedStore16(_p, _val))
+  (absl::base_internal::UnalignedStore16(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
-  (absl::UnalignedStore32(_p, _val))
+  (absl::base_internal::UnalignedStore32(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
-  (absl::UnalignedStore64(_p, _val))
+  (absl::base_internal::UnalignedStore64(_p, _val))
 
 #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386) || \
     defined(_M_IX86) || defined(__ppc__) || defined(__PPC__) ||    \
@@ -199,7 +209,7 @@
 // so we do that.
 
 namespace absl {
-namespace internal {
+namespace base_internal {
 
 struct Unaligned16Struct {
   uint16_t value;
@@ -211,22 +221,25 @@
   uint8_t dummy;  // To make the size non-power-of-two.
 } ABSL_ATTRIBUTE_PACKED;
 
-}  // namespace internal
+}  // namespace base_internal
 }  // namespace absl
 
-#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
-  ((reinterpret_cast<const ::absl::internal::Unaligned16Struct *>(_p))->value)
-#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
-  ((reinterpret_cast<const ::absl::internal::Unaligned32Struct *>(_p))->value)
+#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p)                                  \
+  ((reinterpret_cast<const ::absl::base_internal::Unaligned16Struct *>(_p)) \
+       ->value)
+#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p)                                  \
+  ((reinterpret_cast<const ::absl::base_internal::Unaligned32Struct *>(_p)) \
+       ->value)
 
-#define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val)                          \
-  ((reinterpret_cast< ::absl::internal::Unaligned16Struct *>(_p))->value = \
-       (_val))
-#define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val)                          \
-  ((reinterpret_cast< ::absl::internal::Unaligned32Struct *>(_p))->value = \
-       (_val))
+#define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val)                      \
+  ((reinterpret_cast< ::absl::base_internal::Unaligned16Struct *>(_p)) \
+       ->value = (_val))
+#define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val)                      \
+  ((reinterpret_cast< ::absl::base_internal::Unaligned32Struct *>(_p)) \
+       ->value = (_val))
 
 namespace absl {
+namespace base_internal {
 
 inline uint64_t UnalignedLoad64(const void *p) {
   uint64_t t;
@@ -236,11 +249,13 @@
 
 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
 
+}  // namespace base_internal
 }  // namespace absl
 
-#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) (absl::UnalignedLoad64(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
+  (absl::base_internal::UnalignedLoad64(_p))
 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
-  (absl::UnalignedStore64(_p, _val))
+  (absl::base_internal::UnalignedStore64(_p, _val))
 
 #else
 
@@ -252,6 +267,7 @@
 // unaligned loads and stores.
 
 namespace absl {
+namespace base_internal {
 
 inline uint16_t UnalignedLoad16(const void *p) {
   uint16_t t;
@@ -277,18 +293,22 @@
 
 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
 
+}  // namespace base_internal
 }  // namespace absl
 
-#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) (absl::UnalignedLoad16(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) (absl::UnalignedLoad32(_p))
-#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) (absl::UnalignedLoad64(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
+  (absl::base_internal::UnalignedLoad16(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
+  (absl::base_internal::UnalignedLoad32(_p))
+#define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
+  (absl::base_internal::UnalignedLoad64(_p))
 
 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
-  (absl::UnalignedStore16(_p, _val))
+  (absl::base_internal::UnalignedStore16(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
-  (absl::UnalignedStore32(_p, _val))
+  (absl::base_internal::UnalignedStore32(_p, _val))
 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
-  (absl::UnalignedStore64(_p, _val))
+  (absl::base_internal::UnalignedStore64(_p, _val))
 
 #endif
 
diff --git a/absl/base/log_severity.h b/absl/base/log_severity.h
index e2931c3..5770d36 100644
--- a/absl/base/log_severity.h
+++ b/absl/base/log_severity.h
@@ -39,7 +39,7 @@
            absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
 }
 
-// Returns the all-caps std::string representation (e.g. "INFO") of the specified
+// Returns the all-caps string representation (e.g. "INFO") of the specified
 // severity level if it is one of the normal levels and "UNKNOWN" otherwise.
 constexpr const char* LogSeverityName(absl::LogSeverity s) {
   return s == absl::LogSeverity::kInfo
diff --git a/absl/base/raw_logging_test.cc b/absl/base/raw_logging_test.cc
index ebbc5db..b21cf65 100644
--- a/absl/base/raw_logging_test.cc
+++ b/absl/base/raw_logging_test.cc
@@ -40,7 +40,7 @@
 }
 
 // Not all platforms support output from raw log, so we don't verify any
-// particular output for RAW check failures (expecting the empty std::string
+// particular output for RAW check failures (expecting the empty string
 // accomplishes this).  This test is primarily a compilation test, but we
 // are verifying process death when EXPECT_DEATH works for a platform.
 const char kExpectedDeathOutput[] = "";
diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel
index 6d5c958..f2210e3 100644
--- a/absl/container/BUILD.bazel
+++ b/absl/container/BUILD.bazel
@@ -19,6 +19,7 @@
     "ABSL_DEFAULT_COPTS",
     "ABSL_TEST_COPTS",
     "ABSL_EXCEPTIONS_FLAG",
+    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
 )
 
 package(default_visibility = ["//visibility:public"])
@@ -62,9 +63,11 @@
     name = "fixed_array_test",
     srcs = ["fixed_array_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":fixed_array",
         "//absl/base:exception_testing",
+        "//absl/hash:hash_testing",
         "//absl/memory",
         "@com_google_googletest//:gtest_main",
     ],
@@ -77,6 +80,7 @@
     deps = [
         ":fixed_array",
         "//absl/base:exception_testing",
+        "//absl/hash:hash_testing",
         "//absl/memory",
         "@com_google_googletest//:gtest_main",
     ],
@@ -86,6 +90,7 @@
     name = "fixed_array_exception_safety_test",
     srcs = ["fixed_array_exception_safety_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":fixed_array",
         "//absl/base:exception_safety_testing",
@@ -120,12 +125,14 @@
     name = "inlined_vector_test",
     srcs = ["inlined_vector_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":inlined_vector",
         ":test_instance_tracker",
         "//absl/base",
         "//absl/base:core_headers",
         "//absl/base:exception_testing",
+        "//absl/hash:hash_testing",
         "//absl/memory",
         "//absl/strings",
         "@com_google_googletest//:gtest_main",
@@ -142,6 +149,7 @@
         "//absl/base",
         "//absl/base:core_headers",
         "//absl/base:exception_testing",
+        "//absl/hash:hash_testing",
         "//absl/memory",
         "//absl/strings",
         "@com_google_googletest//:gtest_main",
@@ -181,3 +189,459 @@
         "@com_google_googletest//:gtest_main",
     ],
 )
+
+NOTEST_TAGS_NONMOBILE = [
+    "no_test_darwin_x86_64",
+    "no_test_loonix",
+]
+
+NOTEST_TAGS_MOBILE = [
+    "no_test_android_arm",
+    "no_test_android_arm64",
+    "no_test_android_x86",
+    "no_test_ios_x86_64",
+]
+
+NOTEST_TAGS = NOTEST_TAGS_MOBILE + NOTEST_TAGS_NONMOBILE
+
+cc_library(
+    name = "flat_hash_map",
+    hdrs = ["flat_hash_map.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":container_memory",
+        ":hash_function_defaults",
+        ":raw_hash_map",
+        "//absl/memory",
+    ],
+)
+
+cc_test(
+    name = "flat_hash_map_test",
+    srcs = ["flat_hash_map_test.cc"],
+    copts = ABSL_TEST_COPTS + ["-DUNORDERED_MAP_CXX17"],
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":flat_hash_map",
+        ":hash_generator_testing",
+        ":unordered_map_constructor_test",
+        ":unordered_map_lookup_test",
+        ":unordered_map_modifiers_test",
+        "//absl/types:any",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "flat_hash_set",
+    hdrs = ["flat_hash_set.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":container_memory",
+        ":hash_function_defaults",
+        ":raw_hash_set",
+        "//absl/base:core_headers",
+        "//absl/memory",
+    ],
+)
+
+cc_test(
+    name = "flat_hash_set_test",
+    srcs = ["flat_hash_set_test.cc"],
+    copts = ABSL_TEST_COPTS + ["-DUNORDERED_SET_CXX17"],
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":flat_hash_set",
+        ":hash_generator_testing",
+        ":unordered_set_constructor_test",
+        ":unordered_set_lookup_test",
+        ":unordered_set_modifiers_test",
+        "//absl/memory",
+        "//absl/strings",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "node_hash_map",
+    hdrs = ["node_hash_map.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":container_memory",
+        ":hash_function_defaults",
+        ":node_hash_policy",
+        ":raw_hash_map",
+        "//absl/memory",
+    ],
+)
+
+cc_test(
+    name = "node_hash_map_test",
+    srcs = ["node_hash_map_test.cc"],
+    copts = ABSL_TEST_COPTS + ["-DUNORDERED_MAP_CXX17"],
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":hash_generator_testing",
+        ":node_hash_map",
+        ":tracked",
+        ":unordered_map_constructor_test",
+        ":unordered_map_lookup_test",
+        ":unordered_map_modifiers_test",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "node_hash_set",
+    hdrs = ["node_hash_set.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":container_memory",
+        ":hash_function_defaults",
+        ":node_hash_policy",
+        ":raw_hash_set",
+        "//absl/memory",
+    ],
+)
+
+cc_test(
+    name = "node_hash_set_test",
+    srcs = ["node_hash_set_test.cc"],
+    copts = ABSL_TEST_COPTS + ["-DUNORDERED_SET_CXX17"],
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":hash_generator_testing",
+        ":node_hash_set",
+        ":unordered_set_constructor_test",
+        ":unordered_set_lookup_test",
+        ":unordered_set_modifiers_test",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "container_memory",
+    hdrs = ["internal/container_memory.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        "//absl/memory",
+        "//absl/utility",
+    ],
+)
+
+cc_test(
+    name = "container_memory_test",
+    srcs = ["internal/container_memory_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":container_memory",
+        "//absl/strings",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "hash_function_defaults",
+    hdrs = ["internal/hash_function_defaults.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        "//absl/base:config",
+        "//absl/hash",
+        "//absl/strings",
+    ],
+)
+
+cc_test(
+    name = "hash_function_defaults_test",
+    srcs = ["internal/hash_function_defaults_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    tags = NOTEST_TAGS,
+    deps = [
+        ":hash_function_defaults",
+        "//absl/hash",
+        "//absl/strings",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "hash_generator_testing",
+    testonly = 1,
+    srcs = ["internal/hash_generator_testing.cc"],
+    hdrs = ["internal/hash_generator_testing.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_policy_testing",
+        "//absl/meta:type_traits",
+        "//absl/strings",
+    ],
+)
+
+cc_library(
+    name = "hash_policy_testing",
+    testonly = 1,
+    hdrs = ["internal/hash_policy_testing.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        "//absl/hash",
+        "//absl/strings",
+    ],
+)
+
+cc_test(
+    name = "hash_policy_testing_test",
+    srcs = ["internal/hash_policy_testing_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "hash_policy_traits",
+    hdrs = ["internal/hash_policy_traits.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = ["//absl/meta:type_traits"],
+)
+
+cc_test(
+    name = "hash_policy_traits_test",
+    srcs = ["internal/hash_policy_traits_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_policy_traits",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "hashtable_debug",
+    hdrs = ["internal/hashtable_debug.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":hashtable_debug_hooks",
+    ],
+)
+
+cc_library(
+    name = "hashtable_debug_hooks",
+    hdrs = ["internal/hashtable_debug_hooks.h"],
+    copts = ABSL_DEFAULT_COPTS,
+)
+
+cc_library(
+    name = "node_hash_policy",
+    hdrs = ["internal/node_hash_policy.h"],
+    copts = ABSL_DEFAULT_COPTS,
+)
+
+cc_test(
+    name = "node_hash_policy_test",
+    srcs = ["internal/node_hash_policy_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_policy_traits",
+        ":node_hash_policy",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "raw_hash_map",
+    hdrs = ["internal/raw_hash_map.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":container_memory",
+        ":raw_hash_set",
+    ],
+)
+
+cc_library(
+    name = "raw_hash_set",
+    srcs = ["internal/raw_hash_set.cc"],
+    hdrs = ["internal/raw_hash_set.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":compressed_tuple",
+        ":container_memory",
+        ":hash_policy_traits",
+        ":hashtable_debug_hooks",
+        ":layout",
+        "//absl/base:bits",
+        "//absl/base:config",
+        "//absl/base:core_headers",
+        "//absl/base:endian",
+        "//absl/memory",
+        "//absl/meta:type_traits",
+        "//absl/types:optional",
+        "//absl/utility",
+    ],
+)
+
+cc_test(
+    name = "raw_hash_set_test",
+    srcs = ["internal/raw_hash_set_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    linkstatic = 1,
+    tags = NOTEST_TAGS,
+    deps = [
+        ":container_memory",
+        ":hash_function_defaults",
+        ":hash_policy_testing",
+        ":hashtable_debug",
+        ":raw_hash_set",
+        "//absl/base",
+        "//absl/base:core_headers",
+        "//absl/strings",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_test(
+    name = "raw_hash_set_allocator_test",
+    size = "small",
+    srcs = ["internal/raw_hash_set_allocator_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":raw_hash_set",
+        ":tracked",
+        "//absl/base:core_headers",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "layout",
+    hdrs = ["internal/layout.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        "//absl/base:core_headers",
+        "//absl/meta:type_traits",
+        "//absl/strings",
+        "//absl/types:span",
+        "//absl/utility",
+    ],
+)
+
+cc_test(
+    name = "layout_test",
+    size = "small",
+    srcs = ["internal/layout_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    tags = NOTEST_TAGS,
+    visibility = ["//visibility:private"],
+    deps = [
+        ":layout",
+        "//absl/base",
+        "//absl/base:core_headers",
+        "//absl/types:span",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "tracked",
+    testonly = 1,
+    hdrs = ["internal/tracked.h"],
+    copts = ABSL_TEST_COPTS,
+)
+
+cc_library(
+    name = "unordered_map_constructor_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_map_constructor_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "unordered_map_lookup_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_map_lookup_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "unordered_map_modifiers_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_map_modifiers_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "unordered_set_constructor_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_set_constructor_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "unordered_set_lookup_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_set_lookup_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_library(
+    name = "unordered_set_modifiers_test",
+    testonly = 1,
+    hdrs = ["internal/unordered_set_modifiers_test.h"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash_generator_testing",
+        ":hash_policy_testing",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_test(
+    name = "unordered_set_test",
+    srcs = ["internal/unordered_set_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":unordered_set_constructor_test",
+        ":unordered_set_lookup_test",
+        ":unordered_set_modifiers_test",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_test(
+    name = "unordered_map_test",
+    srcs = ["internal/unordered_map_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    tags = NOTEST_TAGS_NONMOBILE,
+    deps = [
+        ":unordered_map_constructor_test",
+        ":unordered_map_lookup_test",
+        ":unordered_map_modifiers_test",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
diff --git a/absl/container/BUILD.gn b/absl/container/BUILD.gn
index 001a2a3..a2fbd54 100644
--- a/absl/container/BUILD.gn
+++ b/absl/container/BUILD.gn
@@ -84,3 +84,389 @@
   visibility = []
   visibility += [ "../*" ]
 }
+
+source_set("flat_hash_map") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "flat_hash_map.h",
+  ]
+  deps = [
+    ":container_memory",
+    ":hash_function_defaults",
+    ":raw_hash_map",
+    "../memory",
+  ]
+}
+
+source_set("flat_hash_set") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "flat_hash_set.h",
+  ]
+  deps = [
+    ":container_memory",
+    ":hash_function_defaults",
+    ":raw_hash_set",
+    "../base:core_headers",
+    "../memory",
+  ]
+}
+
+source_set("node_hash_map") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "node_hash_map.h",
+  ]
+  deps = [
+    ":container_memory",
+    ":hash_function_defaults",
+    ":node_hash_policy",
+    ":raw_hash_map",
+    "../memory",
+  ]
+}
+
+source_set("node_hash_set") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "node_hash_set.h",
+  ]
+  deps = [
+    ":container_memory",
+    ":hash_function_defaults",
+    ":node_hash_policy",
+    ":raw_hash_set",
+    "../memory",
+  ]
+}
+
+source_set("container_memory") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/container_memory.h",
+  ]
+  deps = [
+    "../memory",
+    "../utility",
+  ]
+}
+
+source_set("hash_function_defaults") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/hash_function_defaults.h",
+  ]
+  deps = [
+    "../base:config",
+    "../hash",
+    "../strings",
+  ]
+}
+
+source_set("hash_generator_testing") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  sources = [
+    "internal/hash_generator_testing.cc",
+  ]
+  public = [
+    "internal/hash_generator_testing.h",
+  ]
+  deps = [
+    ":hash_policy_testing",
+    "../meta:type_traits",
+    "../strings",
+  ]
+}
+
+source_set("hash_policy_testing") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/hash_policy_testing.h",
+  ]
+  deps = [
+    "../hash",
+    "../strings",
+  ]
+}
+
+source_set("hash_policy_traits") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/hash_policy_traits.h",
+  ]
+  deps = [
+    "../meta:type_traits",
+  ]
+}
+
+source_set("hashtable_debug") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/hashtable_debug.h",
+  ]
+  deps = [
+    ":hashtable_debug_hooks",
+  ]
+}
+
+source_set("hashtable_debug_hooks") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/hashtable_debug_hooks.h",
+  ]
+}
+
+source_set("node_hash_policy") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/node_hash_policy.h",
+  ]
+}
+
+source_set("raw_hash_map") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/raw_hash_map.h",
+  ]
+  deps = [
+    ":container_memory",
+    ":raw_hash_set",
+  ]
+}
+
+source_set("raw_hash_set") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  sources = [
+    "internal/raw_hash_set.cc",
+  ]
+  public = [
+    "internal/raw_hash_set.h",
+  ]
+  deps = [
+    ":compressed_tuple",
+    ":container_memory",
+    ":hash_policy_traits",
+    ":hashtable_debug_hooks",
+    ":layout",
+    "../base:bits",
+    "../base:config",
+    "../base:core_headers",
+    "../base:endian",
+    "../memory",
+    "../meta:type_traits",
+    "../types:optional",
+    "../utility",
+  ]
+}
+
+source_set("layout") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/layout.h",
+  ]
+  deps = [
+    "../base:core_headers",
+    "../meta:type_traits",
+    "../strings",
+    "../types:span",
+    "../utility",
+  ]
+}
+
+source_set("tracked") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/tracked.h",
+  ]
+}
+
+source_set("unordered_map_constructor_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_map_constructor_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
+
+source_set("unordered_map_lookup_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_map_lookup_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
+
+source_set("unordered_map_modifiers_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_map_modifiers_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
+
+source_set("unordered_set_constructor_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_set_constructor_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
+
+source_set("unordered_set_lookup_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_set_lookup_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
+
+source_set("unordered_set_modifiers_test") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/unordered_set_modifiers_test.h",
+  ]
+  deps = [
+    ":hash_generator_testing",
+    ":hash_policy_testing",
+    "//testing/gtest",
+  ]
+}
diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt
index 123e4c4..9e40690 100644
--- a/absl/container/CMakeLists.txt
+++ b/absl/container/CMakeLists.txt
@@ -17,18 +17,42 @@
 
 list(APPEND CONTAINER_PUBLIC_HEADERS
   "fixed_array.h"
+  "flat_hash_map.h"
+  "flat_hash_set.h"
   "inlined_vector.h"
+  "node_hash_map.h"
+  "node_hash_set.h"
 )
 
 
 list(APPEND CONTAINER_INTERNAL_HEADERS
+  "internal/compressed_tuple.h"
+  "internal/container_memory.h"
+  "internal/hash_function_defaults.h"
+  "internal/hash_generator_testing.h"
+  "internal/hash_policy_testing.h"
+  "internal/hash_policy_traits.h"
+  "internal/hashtable_debug.h"
+  "internal/layout.h"
+  "internal/node_hash_policy.h"
+  "internal/raw_hash_map.h"
+  "internal/raw_hash_set.h"
   "internal/test_instance_tracker.h"
+  "internal/tracked.h"
+  "internal/unordered_map_constructor_test.h"
+  "internal/unordered_map_lookup_test.h"
+  "internal/unordered_map_modifiers_test.h"
+  "internal/unordered_set_constructor_test.h"
+  "internal/unordered_set_lookup_test.h"
+  "internal/unordered_set_modifiers_test.h"
 )
 
 
-absl_header_library(
+absl_library(
   TARGET
     absl_container
+  SOURCES
+    "internal/raw_hash_set.cc"
   EXPORT_NAME
     container
 )
@@ -142,3 +166,11 @@
 )
 
 
+absl_test(
+  TARGET
+    raw_hash_set_test
+  SOURCES
+    "internal/raw_hash_set_test.cc"
+  PUBLIC_LIBRARIES
+    absl::base absl::hash absl_throw_delegate test_instance_tracker_lib
+)
diff --git a/absl/container/fixed_array.h b/absl/container/fixed_array.h
index 182258f..6d9fa5f 100644
--- a/absl/container/fixed_array.h
+++ b/absl/container/fixed_array.h
@@ -138,8 +138,8 @@
   explicit FixedArray(size_type n, const allocator_type& a = allocator_type())
       : storage_(n, a) {
     if (DefaultConstructorIsNonTrivial()) {
-      memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(),
-                                        storage_.end());
+      memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
+                                      storage_.end());
     }
   }
 
@@ -147,8 +147,8 @@
   FixedArray(size_type n, const value_type& val,
              const allocator_type& a = allocator_type())
       : storage_(n, a) {
-    memory_internal::ConstructStorage(storage_.alloc(), storage_.begin(),
-                                      storage_.end(), val);
+    memory_internal::ConstructRange(storage_.alloc(), storage_.begin(),
+                                    storage_.end(), val);
   }
 
   // Creates an array initialized with the size and contents of `init_list`.
@@ -163,13 +163,12 @@
   FixedArray(Iterator first, Iterator last,
              const allocator_type& a = allocator_type())
       : storage_(std::distance(first, last), a) {
-    memory_internal::CopyToStorageFromRange(storage_.alloc(), storage_.begin(),
-                                            first, last);
+    memory_internal::CopyRange(storage_.alloc(), storage_.begin(), first, last);
   }
 
   ~FixedArray() noexcept {
     for (auto* cur = storage_.begin(); cur != storage_.end(); ++cur) {
-      AllocatorTraits::destroy(*storage_.alloc(), cur);
+      AllocatorTraits::destroy(storage_.alloc(), cur);
     }
   }
 
@@ -359,6 +358,13 @@
   friend bool operator>=(const FixedArray& lhs, const FixedArray& rhs) {
     return !(lhs < rhs);
   }
+
+  template <typename H>
+  friend H AbslHashValue(H h, const FixedArray& v) {
+    return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
+                      v.size());
+  }
+
  private:
   // StorageElement
   //
@@ -446,15 +452,15 @@
       if (UsingInlinedStorage(size())) {
         InlinedStorage::AnnotateDestruct(size());
       } else {
-        AllocatorTraits::deallocate(*alloc(), AsValueType(begin()), size());
+        AllocatorTraits::deallocate(alloc(), AsValueType(begin()), size());
       }
     }
 
     size_type size() const { return size_alloc_.template get<0>(); }
     StorageElement* begin() const { return data_; }
     StorageElement* end() const { return begin() + size(); }
-    allocator_type* alloc() {
-      return std::addressof(size_alloc_.template get<1>());
+    allocator_type& alloc() {
+      return size_alloc_.template get<1>();
     }
 
    private:
@@ -468,7 +474,7 @@
         return InlinedStorage::data();
       } else {
         return reinterpret_cast<StorageElement*>(
-            AllocatorTraits::allocate(*alloc(), size()));
+            AllocatorTraits::allocate(alloc(), size()));
       }
     }
 
diff --git a/absl/container/fixed_array_exception_safety_test.cc b/absl/container/fixed_array_exception_safety_test.cc
index c123c2a..da63dbf 100644
--- a/absl/container/fixed_array_exception_safety_test.cc
+++ b/absl/container/fixed_array_exception_safety_test.cc
@@ -97,7 +97,7 @@
 
 TEST(FixedArrayExceptionSafety, Fill) {
   auto test_fill = testing::MakeExceptionSafetyTester()
-                       .WithInvariants(ReadMemory)
+                       .WithContracts(ReadMemory)
                        .WithOperation([&](FixedArr* fixed_arr_ptr) {
                          auto thrower =
                              Thrower(kUpdatedValue, testing::nothrow_ctor);
diff --git a/absl/container/fixed_array_test.cc b/absl/container/fixed_array_test.cc
index b07ebcb..205ff41 100644
--- a/absl/container/fixed_array_test.cc
+++ b/absl/container/fixed_array_test.cc
@@ -27,6 +27,7 @@
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include "absl/base/internal/exception_testing.h"
+#include "absl/hash/hash_testing.h"
 #include "absl/memory/memory.h"
 
 using ::testing::ElementsAreArray;
@@ -867,4 +868,5 @@
   EXPECT_DEATH(raw[21] = ThreeInts(), "container-overflow");
 }
 #endif  // ADDRESS_SANITIZER
+
 }  // namespace
diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h
new file mode 100644
index 0000000..e5570d1
--- /dev/null
+++ b/absl/container/flat_hash_map.h
@@ -0,0 +1,528 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: flat_hash_map.h
+// -----------------------------------------------------------------------------
+//
+// An `absl::flat_hash_map<K, V>` is an unordered associative container of
+// unique keys and associated values designed to be a more efficient replacement
+// for `std::unordered_map`. Like `unordered_map`, search, insertion, and
+// deletion of map elements can be done as an `O(1)` operation. However,
+// `flat_hash_map` (and other unordered associative containers known as the
+// collection of Abseil "Swiss tables") contain other optimizations that result
+// in both memory and computation advantages.
+//
+// In most cases, your default choice for a hash map should be a map of type
+// `flat_hash_map`.
+
+#ifndef ABSL_CONTAINER_FLAT_HASH_MAP_H_
+#define ABSL_CONTAINER_FLAT_HASH_MAP_H_
+
+#include <cstddef>
+#include <new>
+#include <type_traits>
+#include <utility>
+
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/hash_function_defaults.h"  // IWYU pragma: export
+#include "absl/container/internal/raw_hash_map.h"  // IWYU pragma: export
+#include "absl/memory/memory.h"
+
+namespace absl {
+namespace container_internal {
+template <class K, class V>
+struct FlatHashMapPolicy;
+}  // namespace container_internal
+
+// -----------------------------------------------------------------------------
+// absl::flat_hash_map
+// -----------------------------------------------------------------------------
+//
+// An `absl::flat_hash_map<K, V>` is an unordered associative container which
+// has been optimized for both speed and memory footprint in most common use
+// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
+// the following notable differences:
+//
+// * Requires keys that are CopyConstructible
+// * Requires values that are MoveConstructible
+// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
+//   `insert()`, provided that the map is provided a compatible heterogeneous
+//   hashing function and equality operator.
+// * Invalidates any references and pointers to elements within the table after
+//   `rehash()`.
+// * Contains a `capacity()` member function indicating the number of element
+//   slots (open, deleted, and empty) within the hash map.
+// * Returns `void` from the `erase(iterator)` overload.
+//
+// By default, `flat_hash_map` uses the `absl::Hash` hashing framework.
+// All fundamental and Abseil types that support the `absl::Hash` framework have
+// a compatible equality operator for comparing insertions into `flat_hash_map`.
+// If your type is not yet supported by the `asbl::Hash` framework, see
+// absl/hash/hash.h for information on extending Abseil hashing to user-defined
+// types.
+//
+// NOTE: A `flat_hash_map` stores its value types directly inside its
+// implementation array to avoid memory indirection. Because a `flat_hash_map`
+// is designed to move data when rehashed, map values will not retain pointer
+// stability. If you require pointer stability, or your values are large,
+// consider using `absl::flat_hash_map<Key, std::unique_ptr<Value>>` instead.
+// If your types are not moveable or you require pointer stability for keys,
+// consider `absl::node_hash_map`.
+//
+// Example:
+//
+//   // Create a flat hash map of three strings (that map to strings)
+//   absl::flat_hash_map<std::string, std::string> ducks =
+//     {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
+//
+//  // Insert a new element into the flat hash map
+//  ducks.insert({"d", "donald"});
+//
+//  // Force a rehash of the flat hash map
+//  ducks.rehash(0);
+//
+//  // Find the element with the key "b"
+//  std::string search_key = "b";
+//  auto result = ducks.find(search_key);
+//  if (result != ducks.end()) {
+//    std::cout << "Result: " << result->second << std::endl;
+//  }
+template <class K, class V,
+          class Hash = absl::container_internal::hash_default_hash<K>,
+          class Eq = absl::container_internal::hash_default_eq<K>,
+          class Allocator = std::allocator<std::pair<const K, V>>>
+class flat_hash_map : public absl::container_internal::raw_hash_map<
+                          absl::container_internal::FlatHashMapPolicy<K, V>,
+                          Hash, Eq, Allocator> {
+  using Base = typename flat_hash_map::raw_hash_map;
+
+ public:
+  flat_hash_map() {}
+  using Base::Base;
+
+  // flat_hash_map::begin()
+  //
+  // Returns an iterator to the beginning of the `flat_hash_map`.
+  using Base::begin;
+
+  // flat_hash_map::cbegin()
+  //
+  // Returns a const iterator to the beginning of the `flat_hash_map`.
+  using Base::cbegin;
+
+  // flat_hash_map::cend()
+  //
+  // Returns a const iterator to the end of the `flat_hash_map`.
+  using Base::cend;
+
+  // flat_hash_map::end()
+  //
+  // Returns an iterator to the end of the `flat_hash_map`.
+  using Base::end;
+
+  // flat_hash_map::capacity()
+  //
+  // Returns the number of element slots (assigned, deleted, and empty)
+  // available within the `flat_hash_map`.
+  //
+  // NOTE: this member function is particular to `absl::flat_hash_map` and is
+  // not provided in the `std::unordered_map` API.
+  using Base::capacity;
+
+  // flat_hash_map::empty()
+  //
+  // Returns whether or not the `flat_hash_map` is empty.
+  using Base::empty;
+
+  // flat_hash_map::max_size()
+  //
+  // Returns the largest theoretical possible number of elements within a
+  // `flat_hash_map` under current memory constraints. This value can be thought
+  // of the largest value of `std::distance(begin(), end())` for a
+  // `flat_hash_map<K, V>`.
+  using Base::max_size;
+
+  // flat_hash_map::size()
+  //
+  // Returns the number of elements currently within the `flat_hash_map`.
+  using Base::size;
+
+  // flat_hash_map::clear()
+  //
+  // Removes all elements from the `flat_hash_map`. Invalidates any references,
+  // pointers, or iterators referring to contained elements.
+  //
+  // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
+  // the underlying buffer call `erase(begin(), end())`.
+  using Base::clear;
+
+  // flat_hash_map::erase()
+  //
+  // Erases elements within the `flat_hash_map`. Erasing does not trigger a
+  // rehash. Overloads are listed below.
+  //
+  // void erase(const_iterator pos):
+  //
+  //   Erases the element at `position` of the `flat_hash_map`, returning
+  //   `void`.
+  //
+  //   NOTE: this return behavior is different than that of STL containers in
+  //   general and `std::unordered_map` in particular.
+  //
+  // iterator erase(const_iterator first, const_iterator last):
+  //
+  //   Erases the elements in the open interval [`first`, `last`), returning an
+  //   iterator pointing to `last`.
+  //
+  // size_type erase(const key_type& key):
+  //
+  //   Erases the element with the matching key, if it exists.
+  using Base::erase;
+
+  // flat_hash_map::insert()
+  //
+  // Inserts an element of the specified value into the `flat_hash_map`,
+  // returning an iterator pointing to the newly inserted element, provided that
+  // an element with the given key does not already exist. If rehashing occurs
+  // due to the insertion, all iterators are invalidated. Overloads are listed
+  // below.
+  //
+  // std::pair<iterator,bool> insert(const init_type& value):
+  //
+  //   Inserts a value into the `flat_hash_map`. Returns a pair consisting of an
+  //   iterator to the inserted element (or to the element that prevented the
+  //   insertion) and a bool denoting whether the insertion took place.
+  //
+  // std::pair<iterator,bool> insert(T&& value):
+  // std::pair<iterator,bool> insert(init_type&& value):
+  //
+  //   Inserts a moveable value into the `flat_hash_map`. Returns a pair
+  //   consisting of an iterator to the inserted element (or to the element that
+  //   prevented the insertion) and a bool denoting whether the insertion took
+  //   place.
+  //
+  // iterator insert(const_iterator hint, const init_type& value):
+  // iterator insert(const_iterator hint, T&& value):
+  // iterator insert(const_iterator hint, init_type&& value);
+  //
+  //   Inserts a value, using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search. Returns an iterator to the
+  //   inserted element, or to the existing element that prevented the
+  //   insertion.
+  //
+  // void insert(InputIterator first, InputIterator last):
+  //
+  //   Inserts a range of values [`first`, `last`).
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently, for `flat_hash_map` we guarantee the
+  //   first match is inserted.
+  //
+  // void insert(std::initializer_list<init_type> ilist):
+  //
+  //   Inserts the elements within the initializer list `ilist`.
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently within the initializer list, for
+  //   `flat_hash_map` we guarantee the first match is inserted.
+  using Base::insert;
+
+  // flat_hash_map::insert_or_assign()
+  //
+  // Inserts an element of the specified value into the `flat_hash_map` provided
+  // that a value with the given key does not already exist, or replaces it with
+  // the element value if a key for that value already exists, returning an
+  // iterator pointing to the newly inserted element.  If rehashing occurs due
+  // to the insertion, all existing iterators are invalidated. Overloads are
+  // listed below.
+  //
+  // pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
+  // pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
+  //
+  //   Inserts/Assigns (or moves) the element of the specified key into the
+  //   `flat_hash_map`.
+  //
+  // iterator insert_or_assign(const_iterator hint,
+  //                           const init_type& k, T&& obj):
+  // iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
+  //
+  //   Inserts/Assigns (or moves) the element of the specified key into the
+  //   `flat_hash_map` using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search.
+  using Base::insert_or_assign;
+
+  // flat_hash_map::emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `flat_hash_map`, provided that no element with the given key
+  // already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace;
+
+  // flat_hash_map::emplace_hint()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `flat_hash_map`, using the position of `hint` as a non-binding
+  // suggestion for where to begin the insertion search, and only inserts
+  // provided that no element with the given key already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace_hint;
+
+  // flat_hash_map::try_emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `flat_hash_map`, provided that no element with the given key
+  // already exists. Unlike `emplace()`, if an element with the given key
+  // already exists, we guarantee that no element is constructed.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  // Overloads are listed below.
+  //
+  //   pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
+  //   pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
+  //
+  // Inserts (via copy or move) the element of the specified key into the
+  // `flat_hash_map`.
+  //
+  //   iterator try_emplace(const_iterator hint,
+  //                        const init_type& k, Args&&... args):
+  //   iterator try_emplace(const_iterator hint, init_type&& k, Args&&... args):
+  //
+  // Inserts (via copy or move) the element of the specified key into the
+  // `flat_hash_map` using the position of `hint` as a non-binding suggestion
+  // for where to begin the insertion search.
+  using Base::try_emplace;
+
+  // flat_hash_map::extract()
+  //
+  // Extracts the indicated element, erasing it in the process, and returns it
+  // as a C++17-compatible node handle. Overloads are listed below.
+  //
+  // node_type extract(const_iterator position):
+  //
+  //   Extracts the key,value pair of the element at the indicated position and
+  //   returns a node handle owning that extracted data.
+  //
+  // node_type extract(const key_type& x):
+  //
+  //   Extracts the key,value pair of the element with a key matching the passed
+  //   key value and returns a node handle owning that extracted data. If the
+  //   `flat_hash_map` does not contain an element with a matching key, this
+  //   function returns an empty node handle.
+  using Base::extract;
+
+  // flat_hash_map::merge()
+  //
+  // Extracts elements from a given `source` flat hash map into this
+  // `flat_hash_map`. If the destination `flat_hash_map` already contains an
+  // element with an equivalent key, that element is not extracted.
+  using Base::merge;
+
+  // flat_hash_map::swap(flat_hash_map& other)
+  //
+  // Exchanges the contents of this `flat_hash_map` with those of the `other`
+  // flat hash map, avoiding invocation of any move, copy, or swap operations on
+  // individual elements.
+  //
+  // All iterators and references on the `flat_hash_map` remain valid, excepting
+  // for the past-the-end iterator, which is invalidated.
+  //
+  // `swap()` requires that the flat hash map's hashing and key equivalence
+  // functions be Swappable, and are exchaged using unqualified calls to
+  // non-member `swap()`. If the map's allocator has
+  // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
+  // set to `true`, the allocators are also exchanged using an unqualified call
+  // to non-member `swap()`; otherwise, the allocators are not swapped.
+  using Base::swap;
+
+  // flat_hash_map::rehash(count)
+  //
+  // Rehashes the `flat_hash_map`, setting the number of slots to be at least
+  // the passed value. If the new number of slots increases the load factor more
+  // than the current maximum load factor
+  // (`count` < `size()` / `max_load_factor()`), then the new number of slots
+  // will be at least `size()` / `max_load_factor()`.
+  //
+  // To force a rehash, pass rehash(0).
+  //
+  // NOTE: unlike behavior in `std::unordered_map`, references are also
+  // invalidated upon a `rehash()`.
+  using Base::rehash;
+
+  // flat_hash_map::reserve(count)
+  //
+  // Sets the number of slots in the `flat_hash_map` to the number needed to
+  // accommodate at least `count` total elements without exceeding the current
+  // maximum load factor, and may rehash the container if needed.
+  using Base::reserve;
+
+  // flat_hash_map::at()
+  //
+  // Returns a reference to the mapped value of the element with key equivalent
+  // to the passed key.
+  using Base::at;
+
+  // flat_hash_map::contains()
+  //
+  // Determines whether an element with a key comparing equal to the given `key`
+  // exists within the `flat_hash_map`, returning `true` if so or `false`
+  // otherwise.
+  using Base::contains;
+
+  // flat_hash_map::count(const Key& key) const
+  //
+  // Returns the number of elements with a key comparing equal to the given
+  // `key` within the `flat_hash_map`. note that this function will return
+  // either `1` or `0` since duplicate keys are not allowed within a
+  // `flat_hash_map`.
+  using Base::count;
+
+  // flat_hash_map::equal_range()
+  //
+  // Returns a closed range [first, last], defined by a `std::pair` of two
+  // iterators, containing all elements with the passed key in the
+  // `flat_hash_map`.
+  using Base::equal_range;
+
+  // flat_hash_map::find()
+  //
+  // Finds an element with the passed `key` within the `flat_hash_map`.
+  using Base::find;
+
+  // flat_hash_map::operator[]()
+  //
+  // Returns a reference to the value mapped to the passed key within the
+  // `flat_hash_map`, performing an `insert()` if the key does not already
+  // exist.
+  //
+  // If an insertion occurs and results in a rehashing of the container, all
+  // iterators are invalidated. Otherwise iterators are not affected and
+  // references are not invalidated. Overloads are listed below.
+  //
+  // T& operator[](const Key& key):
+  //
+  //   Inserts an init_type object constructed in-place if the element with the
+  //   given key does not exist.
+  //
+  // T& operator[](Key&& key):
+  //
+  //   Inserts an init_type object constructed in-place provided that an element
+  //   with the given key does not exist.
+  using Base::operator[];
+
+  // flat_hash_map::bucket_count()
+  //
+  // Returns the number of "buckets" within the `flat_hash_map`. Note that
+  // because a flat hash map contains all elements within its internal storage,
+  // this value simply equals the current capacity of the `flat_hash_map`.
+  using Base::bucket_count;
+
+  // flat_hash_map::load_factor()
+  //
+  // Returns the current load factor of the `flat_hash_map` (the average number
+  // of slots occupied with a value within the hash map).
+  using Base::load_factor;
+
+  // flat_hash_map::max_load_factor()
+  //
+  // Manages the maximum load factor of the `flat_hash_map`. Overloads are
+  // listed below.
+  //
+  // float flat_hash_map::max_load_factor()
+  //
+  //   Returns the current maximum load factor of the `flat_hash_map`.
+  //
+  // void flat_hash_map::max_load_factor(float ml)
+  //
+  //   Sets the maximum load factor of the `flat_hash_map` to the passed value.
+  //
+  //   NOTE: This overload is provided only for API compatibility with the STL;
+  //   `flat_hash_map` will ignore any set load factor and manage its rehashing
+  //   internally as an implementation detail.
+  using Base::max_load_factor;
+
+  // flat_hash_map::get_allocator()
+  //
+  // Returns the allocator function associated with this `flat_hash_map`.
+  using Base::get_allocator;
+
+  // flat_hash_map::hash_function()
+  //
+  // Returns the hashing function used to hash the keys within this
+  // `flat_hash_map`.
+  using Base::hash_function;
+
+  // flat_hash_map::key_eq()
+  //
+  // Returns the function used for comparing keys equality.
+  using Base::key_eq;
+};
+
+namespace container_internal {
+
+template <class K, class V>
+struct FlatHashMapPolicy {
+  using slot_type = container_internal::slot_type<K, V>;
+  using key_type = K;
+  using mapped_type = V;
+  using init_type = std::pair</*non const*/ key_type, mapped_type>;
+
+  template <class Allocator, class... Args>
+  static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
+    slot_type::construct(alloc, slot, std::forward<Args>(args)...);
+  }
+
+  template <class Allocator>
+  static void destroy(Allocator* alloc, slot_type* slot) {
+    slot_type::destroy(alloc, slot);
+  }
+
+  template <class Allocator>
+  static void transfer(Allocator* alloc, slot_type* new_slot,
+                       slot_type* old_slot) {
+    slot_type::transfer(alloc, new_slot, old_slot);
+  }
+
+  template <class F, class... Args>
+  static decltype(absl::container_internal::DecomposePair(
+      std::declval<F>(), std::declval<Args>()...))
+  apply(F&& f, Args&&... args) {
+    return absl::container_internal::DecomposePair(std::forward<F>(f),
+                                                   std::forward<Args>(args)...);
+  }
+
+  static size_t space_used(const slot_type*) { return 0; }
+
+  static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
+
+  static V& value(std::pair<const K, V>* kv) { return kv->second; }
+  static const V& value(const std::pair<const K, V>* kv) { return kv->second; }
+};
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_FLAT_HASH_MAP_H_
diff --git a/absl/container/flat_hash_map_test.cc b/absl/container/flat_hash_map_test.cc
new file mode 100644
index 0000000..10a781f
--- /dev/null
+++ b/absl/container/flat_hash_map_test.cc
@@ -0,0 +1,241 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/flat_hash_map.h"
+
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/unordered_map_constructor_test.h"
+#include "absl/container/internal/unordered_map_lookup_test.h"
+#include "absl/container/internal/unordered_map_modifiers_test.h"
+#include "absl/types/any.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+using ::absl::container_internal::hash_internal::Enum;
+using ::absl::container_internal::hash_internal::EnumClass;
+using ::testing::_;
+using ::testing::Pair;
+using ::testing::UnorderedElementsAre;
+
+template <class K, class V>
+using Map =
+    flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual, Alloc<>>;
+
+static_assert(!std::is_standard_layout<NonStandardLayout>(), "");
+
+using MapTypes =
+    ::testing::Types<Map<int, int>, Map<std::string, int>, Map<Enum, std::string>,
+                     Map<EnumClass, int>, Map<int, NonStandardLayout>,
+                     Map<NonStandardLayout, int>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashMap, ConstructorTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashMap, LookupTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashMap, ModifiersTest, MapTypes);
+
+TEST(FlatHashMap, StandardLayout) {
+  struct Int {
+    explicit Int(size_t value) : value(value) {}
+    Int() : value(0) { ADD_FAILURE(); }
+    Int(const Int& other) : value(other.value) { ADD_FAILURE(); }
+    Int(Int&&) = default;
+    bool operator==(const Int& other) const { return value == other.value; }
+    size_t value;
+  };
+  static_assert(std::is_standard_layout<Int>(), "");
+
+  struct Hash {
+    size_t operator()(const Int& obj) const { return obj.value; }
+  };
+
+  // Verify that neither the key nor the value get default-constructed or
+  // copy-constructed.
+  {
+    flat_hash_map<Int, Int, Hash> m;
+    m.try_emplace(Int(1), Int(2));
+    m.try_emplace(Int(3), Int(4));
+    m.erase(Int(1));
+    m.rehash(2 * m.bucket_count());
+  }
+  {
+    flat_hash_map<Int, Int, Hash> m;
+    m.try_emplace(Int(1), Int(2));
+    m.try_emplace(Int(3), Int(4));
+    m.erase(Int(1));
+    m.clear();
+  }
+}
+
+// gcc becomes unhappy if this is inside the method, so pull it out here.
+struct balast {};
+
+TEST(FlatHashMap, IteratesMsan) {
+  // Because SwissTable randomizes on pointer addresses, we keep old tables
+  // around to ensure we don't reuse old memory.
+  std::vector<absl::flat_hash_map<int, balast>> garbage;
+  for (int i = 0; i < 100; ++i) {
+    absl::flat_hash_map<int, balast> t;
+    for (int j = 0; j < 100; ++j) {
+      t[j];
+      for (const auto& p : t) EXPECT_THAT(p, Pair(_, _));
+    }
+    garbage.push_back(std::move(t));
+  }
+}
+
+// Demonstration of the "Lazy Key" pattern.  This uses heterogenous insert to
+// avoid creating expensive key elements when the item is already present in the
+// map.
+struct LazyInt {
+  explicit LazyInt(size_t value, int* tracker)
+      : value(value), tracker(tracker) {}
+
+  explicit operator size_t() const {
+    ++*tracker;
+    return value;
+  }
+
+  size_t value;
+  int* tracker;
+};
+
+struct Hash {
+  using is_transparent = void;
+  int* tracker;
+  size_t operator()(size_t obj) const {
+    ++*tracker;
+    return obj;
+  }
+  size_t operator()(const LazyInt& obj) const {
+    ++*tracker;
+    return obj.value;
+  }
+};
+
+struct Eq {
+  using is_transparent = void;
+  bool operator()(size_t lhs, size_t rhs) const {
+    return lhs == rhs;
+  }
+  bool operator()(size_t lhs, const LazyInt& rhs) const {
+    return lhs == rhs.value;
+  }
+};
+
+TEST(FlatHashMap, LazyKeyPattern) {
+  // hashes are only guaranteed in opt mode, we use assertions to track internal
+  // state that can cause extra calls to hash.
+  int conversions = 0;
+  int hashes = 0;
+  flat_hash_map<size_t, size_t, Hash, Eq> m(0, Hash{&hashes});
+
+  m[LazyInt(1, &conversions)] = 1;
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 1)));
+  EXPECT_EQ(conversions, 1);
+#ifdef NDEBUG
+  EXPECT_EQ(hashes, 1);
+#endif
+
+  m[LazyInt(1, &conversions)] = 2;
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2)));
+  EXPECT_EQ(conversions, 1);
+#ifdef NDEBUG
+  EXPECT_EQ(hashes, 2);
+#endif
+
+  m.try_emplace(LazyInt(2, &conversions), 3);
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
+  EXPECT_EQ(conversions, 2);
+#ifdef NDEBUG
+  EXPECT_EQ(hashes, 3);
+#endif
+
+  m.try_emplace(LazyInt(2, &conversions), 4);
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3)));
+  EXPECT_EQ(conversions, 2);
+#ifdef NDEBUG
+  EXPECT_EQ(hashes, 4);
+#endif
+}
+
+TEST(FlatHashMap, BitfieldArgument) {
+  union {
+    int n : 1;
+  };
+  n = 0;
+  flat_hash_map<int, int> m;
+  m.erase(n);
+  m.count(n);
+  m.prefetch(n);
+  m.find(n);
+  m.contains(n);
+  m.equal_range(n);
+  m.insert_or_assign(n, n);
+  m.insert_or_assign(m.end(), n, n);
+  m.try_emplace(n);
+  m.try_emplace(m.end(), n);
+  m.at(n);
+  m[n];
+}
+
+TEST(FlatHashMap, MergeExtractInsert) {
+  // We can't test mutable keys, or non-copyable keys with flat_hash_map.
+  // Test that the nodes have the proper API.
+  absl::flat_hash_map<int, int> m = {{1, 7}, {2, 9}};
+  auto node = m.extract(1);
+  EXPECT_TRUE(node);
+  EXPECT_EQ(node.key(), 1);
+  EXPECT_EQ(node.mapped(), 7);
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(2, 9)));
+
+  node.mapped() = 17;
+  m.insert(std::move(node));
+  EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
+}
+#if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__)
+TEST(FlatHashMap, Any) {
+  absl::flat_hash_map<int, absl::any> m;
+  m.emplace(1, 7);
+  auto it = m.find(1);
+  ASSERT_NE(it, m.end());
+  EXPECT_EQ(7, absl::any_cast<int>(it->second));
+
+  m.emplace(std::piecewise_construct, std::make_tuple(2), std::make_tuple(8));
+  it = m.find(2);
+  ASSERT_NE(it, m.end());
+  EXPECT_EQ(8, absl::any_cast<int>(it->second));
+
+  m.emplace(std::piecewise_construct, std::make_tuple(3),
+            std::make_tuple(absl::any(9)));
+  it = m.find(3);
+  ASSERT_NE(it, m.end());
+  EXPECT_EQ(9, absl::any_cast<int>(it->second));
+
+  struct H {
+    size_t operator()(const absl::any&) const { return 0; }
+  };
+  struct E {
+    bool operator()(const absl::any&, const absl::any&) const { return true; }
+  };
+  absl::flat_hash_map<absl::any, int, H, E> m2;
+  m2.emplace(1, 7);
+  auto it2 = m2.find(1);
+  ASSERT_NE(it2, m2.end());
+  EXPECT_EQ(7, it2->second);
+}
+#endif  // __ANDROID__
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/flat_hash_set.h b/absl/container/flat_hash_set.h
new file mode 100644
index 0000000..98aead1
--- /dev/null
+++ b/absl/container/flat_hash_set.h
@@ -0,0 +1,439 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: flat_hash_set.h
+// -----------------------------------------------------------------------------
+//
+// An `absl::flat_hash_set<T>` is an unordered associative container designed to
+// be a more efficient replacement for `std::unordered_set`. Like
+// `unordered_set`, search, insertion, and deletion of set elements can be done
+// as an `O(1)` operation. However, `flat_hash_set` (and other unordered
+// associative containers known as the collection of Abseil "Swiss tables")
+// contain other optimizations that result in both memory and computation
+// advantages.
+//
+// In most cases, your default choice for a hash set should be a set of type
+// `flat_hash_set`.
+#ifndef ABSL_CONTAINER_FLAT_HASH_SET_H_
+#define ABSL_CONTAINER_FLAT_HASH_SET_H_
+
+#include <type_traits>
+#include <utility>
+
+#include "absl/base/macros.h"
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/hash_function_defaults.h"  // IWYU pragma: export
+#include "absl/container/internal/raw_hash_set.h"  // IWYU pragma: export
+#include "absl/memory/memory.h"
+
+namespace absl {
+namespace container_internal {
+template <typename T>
+struct FlatHashSetPolicy;
+}  // namespace container_internal
+
+// -----------------------------------------------------------------------------
+// absl::flat_hash_set
+// -----------------------------------------------------------------------------
+//
+// An `absl::flat_hash_set<T>` is an unordered associative container which has
+// been optimized for both speed and memory footprint in most common use cases.
+// Its interface is similar to that of `std::unordered_set<T>` with the
+// following notable differences:
+//
+// * Requires keys that are CopyConstructible
+// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
+//   `insert()`, provided that the set is provided a compatible heterogeneous
+//   hashing function and equality operator.
+// * Invalidates any references and pointers to elements within the table after
+//   `rehash()`.
+// * Contains a `capacity()` member function indicating the number of element
+//   slots (open, deleted, and empty) within the hash set.
+// * Returns `void` from the `erase(iterator)` overload.
+//
+// By default, `flat_hash_set` uses the `absl::Hash` hashing framework. All
+// fundamental and Abseil types that support the `absl::Hash` framework have a
+// compatible equality operator for comparing insertions into `flat_hash_map`.
+// If your type is not yet supported by the `asbl::Hash` framework, see
+// absl/hash/hash.h for information on extending Abseil hashing to user-defined
+// types.
+//
+// NOTE: A `flat_hash_set` stores its keys directly inside its implementation
+// array to avoid memory indirection. Because a `flat_hash_set` is designed to
+// move data when rehashed, set keys will not retain pointer stability. If you
+// require pointer stability, consider using
+// `absl::flat_hash_set<std::unique_ptr<T>>`. If your type is not moveable and
+// you require pointer stability, consider `absl::node_hash_set` instead.
+//
+// Example:
+//
+//   // Create a flat hash set of three strings
+//   absl::flat_hash_set<std::string> ducks =
+//     {"huey", "dewey", "louie"};
+//
+//  // Insert a new element into the flat hash set
+//  ducks.insert("donald"};
+//
+//  // Force a rehash of the flat hash set
+//  ducks.rehash(0);
+//
+//  // See if "dewey" is present
+//  if (ducks.contains("dewey")) {
+//    std::cout << "We found dewey!" << std::endl;
+//  }
+template <class T, class Hash = absl::container_internal::hash_default_hash<T>,
+          class Eq = absl::container_internal::hash_default_eq<T>,
+          class Allocator = std::allocator<T>>
+class flat_hash_set
+    : public absl::container_internal::raw_hash_set<
+          absl::container_internal::FlatHashSetPolicy<T>, Hash, Eq, Allocator> {
+  using Base = typename flat_hash_set::raw_hash_set;
+
+ public:
+  flat_hash_set() {}
+  using Base::Base;
+
+  // flat_hash_set::begin()
+  //
+  // Returns an iterator to the beginning of the `flat_hash_set`.
+  using Base::begin;
+
+  // flat_hash_set::cbegin()
+  //
+  // Returns a const iterator to the beginning of the `flat_hash_set`.
+  using Base::cbegin;
+
+  // flat_hash_set::cend()
+  //
+  // Returns a const iterator to the end of the `flat_hash_set`.
+  using Base::cend;
+
+  // flat_hash_set::end()
+  //
+  // Returns an iterator to the end of the `flat_hash_set`.
+  using Base::end;
+
+  // flat_hash_set::capacity()
+  //
+  // Returns the number of element slots (assigned, deleted, and empty)
+  // available within the `flat_hash_set`.
+  //
+  // NOTE: this member function is particular to `absl::flat_hash_set` and is
+  // not provided in the `std::unordered_map` API.
+  using Base::capacity;
+
+  // flat_hash_set::empty()
+  //
+  // Returns whether or not the `flat_hash_set` is empty.
+  using Base::empty;
+
+  // flat_hash_set::max_size()
+  //
+  // Returns the largest theoretical possible number of elements within a
+  // `flat_hash_set` under current memory constraints. This value can be thought
+  // of the largest value of `std::distance(begin(), end())` for a
+  // `flat_hash_set<T>`.
+  using Base::max_size;
+
+  // flat_hash_set::size()
+  //
+  // Returns the number of elements currently within the `flat_hash_set`.
+  using Base::size;
+
+  // flat_hash_set::clear()
+  //
+  // Removes all elements from the `flat_hash_set`. Invalidates any references,
+  // pointers, or iterators referring to contained elements.
+  //
+  // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
+  // the underlying buffer call `erase(begin(), end())`.
+  using Base::clear;
+
+  // flat_hash_set::erase()
+  //
+  // Erases elements within the `flat_hash_set`. Erasing does not trigger a
+  // rehash. Overloads are listed below.
+  //
+  // void erase(const_iterator pos):
+  //
+  //   Erases the element at `position` of the `flat_hash_set`, returning
+  //   `void`.
+  //
+  //   NOTE: this return behavior is different than that of STL containers in
+  //   general and `std::unordered_map` in particular.
+  //
+  // iterator erase(const_iterator first, const_iterator last):
+  //
+  //   Erases the elements in the open interval [`first`, `last`), returning an
+  //   iterator pointing to `last`.
+  //
+  // size_type erase(const key_type& key):
+  //
+  //   Erases the element with the matching key, if it exists.
+  using Base::erase;
+
+  // flat_hash_set::insert()
+  //
+  // Inserts an element of the specified value into the `flat_hash_set`,
+  // returning an iterator pointing to the newly inserted element, provided that
+  // an element with the given key does not already exist. If rehashing occurs
+  // due to the insertion, all iterators are invalidated. Overloads are listed
+  // below.
+  //
+  // std::pair<iterator,bool> insert(const T& value):
+  //
+  //   Inserts a value into the `flat_hash_set`. Returns a pair consisting of an
+  //   iterator to the inserted element (or to the element that prevented the
+  //   insertion) and a bool denoting whether the insertion took place.
+  //
+  // std::pair<iterator,bool> insert(T&& value):
+  //
+  //   Inserts a moveable value into the `flat_hash_set`. Returns a pair
+  //   consisting of an iterator to the inserted element (or to the element that
+  //   prevented the insertion) and a bool denoting whether the insertion took
+  //   place.
+  //
+  // iterator insert(const_iterator hint, const T& value):
+  // iterator insert(const_iterator hint, T&& value):
+  //
+  //   Inserts a value, using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search. Returns an iterator to the
+  //   inserted element, or to the existing element that prevented the
+  //   insertion.
+  //
+  // void insert(InputIterator first, InputIterator last):
+  //
+  //   Inserts a range of values [`first`, `last`).
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently, for `flat_hash_set` we guarantee the
+  //   first match is inserted.
+  //
+  // void insert(std::initializer_list<T> ilist):
+  //
+  //   Inserts the elements within the initializer list `ilist`.
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently within the initializer list, for
+  //   `flat_hash_set` we guarantee the first match is inserted.
+  using Base::insert;
+
+  // flat_hash_set::emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `flat_hash_set`, provided that no element with the given key
+  // already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace;
+
+  // flat_hash_set::emplace_hint()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `flat_hash_set`, using the position of `hint` as a non-binding
+  // suggestion for where to begin the insertion search, and only inserts
+  // provided that no element with the given key already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace_hint;
+
+  // flat_hash_set::extract()
+  //
+  // Extracts the indicated element, erasing it in the process, and returns it
+  // as a C++17-compatible node handle. Overloads are listed below.
+  //
+  // node_type extract(const_iterator position):
+  //
+  //   Extracts the element at the indicated position and returns a node handle
+  //   owning that extracted data.
+  //
+  // node_type extract(const key_type& x):
+  //
+  //   Extracts the element with the key matching the passed key value and
+  //   returns a node handle owning that extracted data. If the `flat_hash_set`
+  //   does not contain an element with a matching key, this function returns an
+  //   empty node handle.
+  using Base::extract;
+
+  // flat_hash_set::merge()
+  //
+  // Extracts elements from a given `source` flat hash map into this
+  // `flat_hash_set`. If the destination `flat_hash_set` already contains an
+  // element with an equivalent key, that element is not extracted.
+  using Base::merge;
+
+  // flat_hash_set::swap(flat_hash_set& other)
+  //
+  // Exchanges the contents of this `flat_hash_set` with those of the `other`
+  // flat hash map, avoiding invocation of any move, copy, or swap operations on
+  // individual elements.
+  //
+  // All iterators and references on the `flat_hash_set` remain valid, excepting
+  // for the past-the-end iterator, which is invalidated.
+  //
+  // `swap()` requires that the flat hash set's hashing and key equivalence
+  // functions be Swappable, and are exchaged using unqualified calls to
+  // non-member `swap()`. If the map's allocator has
+  // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
+  // set to `true`, the allocators are also exchanged using an unqualified call
+  // to non-member `swap()`; otherwise, the allocators are not swapped.
+  using Base::swap;
+
+  // flat_hash_set::rehash(count)
+  //
+  // Rehashes the `flat_hash_set`, setting the number of slots to be at least
+  // the passed value. If the new number of slots increases the load factor more
+  // than the current maximum load factor
+  // (`count` < `size()` / `max_load_factor()`), then the new number of slots
+  // will be at least `size()` / `max_load_factor()`.
+  //
+  // To force a rehash, pass rehash(0).
+  //
+  // NOTE: unlike behavior in `std::unordered_set`, references are also
+  // invalidated upon a `rehash()`.
+  using Base::rehash;
+
+  // flat_hash_set::reserve(count)
+  //
+  // Sets the number of slots in the `flat_hash_set` to the number needed to
+  // accommodate at least `count` total elements without exceeding the current
+  // maximum load factor, and may rehash the container if needed.
+  using Base::reserve;
+
+  // flat_hash_set::contains()
+  //
+  // Determines whether an element comparing equal to the given `key` exists
+  // within the `flat_hash_set`, returning `true` if so or `false` otherwise.
+  using Base::contains;
+
+  // flat_hash_set::count(const Key& key) const
+  //
+  // Returns the number of elements comparing equal to the given `key` within
+  // the `flat_hash_set`. note that this function will return either `1` or `0`
+  // since duplicate elements are not allowed within a `flat_hash_set`.
+  using Base::count;
+
+  // flat_hash_set::equal_range()
+  //
+  // Returns a closed range [first, last], defined by a `std::pair` of two
+  // iterators, containing all elements with the passed key in the
+  // `flat_hash_set`.
+  using Base::equal_range;
+
+  // flat_hash_set::find()
+  //
+  // Finds an element with the passed `key` within the `flat_hash_set`.
+  using Base::find;
+
+  // flat_hash_set::bucket_count()
+  //
+  // Returns the number of "buckets" within the `flat_hash_set`. Note that
+  // because a flat hash map contains all elements within its internal storage,
+  // this value simply equals the current capacity of the `flat_hash_set`.
+  using Base::bucket_count;
+
+  // flat_hash_set::load_factor()
+  //
+  // Returns the current load factor of the `flat_hash_set` (the average number
+  // of slots occupied with a value within the hash map).
+  using Base::load_factor;
+
+  // flat_hash_set::max_load_factor()
+  //
+  // Manages the maximum load factor of the `flat_hash_set`. Overloads are
+  // listed below.
+  //
+  // float flat_hash_set::max_load_factor()
+  //
+  //   Returns the current maximum load factor of the `flat_hash_set`.
+  //
+  // void flat_hash_set::max_load_factor(float ml)
+  //
+  //   Sets the maximum load factor of the `flat_hash_set` to the passed value.
+  //
+  //   NOTE: This overload is provided only for API compatibility with the STL;
+  //   `flat_hash_set` will ignore any set load factor and manage its rehashing
+  //   internally as an implementation detail.
+  using Base::max_load_factor;
+
+  // flat_hash_set::get_allocator()
+  //
+  // Returns the allocator function associated with this `flat_hash_set`.
+  using Base::get_allocator;
+
+  // flat_hash_set::hash_function()
+  //
+  // Returns the hashing function used to hash the keys within this
+  // `flat_hash_set`.
+  using Base::hash_function;
+
+  // flat_hash_set::key_eq()
+  //
+  // Returns the function used for comparing keys equality.
+  using Base::key_eq;
+};
+
+namespace container_internal {
+
+template <class T>
+struct FlatHashSetPolicy {
+  using slot_type = T;
+  using key_type = T;
+  using init_type = T;
+  using constant_iterators = std::true_type;
+
+  template <class Allocator, class... Args>
+  static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
+    absl::allocator_traits<Allocator>::construct(*alloc, slot,
+                                                 std::forward<Args>(args)...);
+  }
+
+  template <class Allocator>
+  static void destroy(Allocator* alloc, slot_type* slot) {
+    absl::allocator_traits<Allocator>::destroy(*alloc, slot);
+  }
+
+  template <class Allocator>
+  static void transfer(Allocator* alloc, slot_type* new_slot,
+                       slot_type* old_slot) {
+    construct(alloc, new_slot, std::move(*old_slot));
+    destroy(alloc, old_slot);
+  }
+
+  static T& element(slot_type* slot) { return *slot; }
+
+  template <class F, class... Args>
+  static decltype(absl::container_internal::DecomposeValue(
+      std::declval<F>(), std::declval<Args>()...))
+  apply(F&& f, Args&&... args) {
+    return absl::container_internal::DecomposeValue(
+        std::forward<F>(f), std::forward<Args>(args)...);
+  }
+
+  static size_t space_used(const T*) { return 0; }
+};
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_FLAT_HASH_SET_H_
diff --git a/absl/container/flat_hash_set_test.cc b/absl/container/flat_hash_set_test.cc
new file mode 100644
index 0000000..e52fd53
--- /dev/null
+++ b/absl/container/flat_hash_set_test.cc
@@ -0,0 +1,126 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/flat_hash_set.h"
+
+#include <vector>
+
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/unordered_set_constructor_test.h"
+#include "absl/container/internal/unordered_set_lookup_test.h"
+#include "absl/container/internal/unordered_set_modifiers_test.h"
+#include "absl/memory/memory.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::absl::container_internal::hash_internal::Enum;
+using ::absl::container_internal::hash_internal::EnumClass;
+using ::testing::Pointee;
+using ::testing::UnorderedElementsAre;
+using ::testing::UnorderedElementsAreArray;
+
+template <class T>
+using Set =
+    absl::flat_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;
+
+using SetTypes =
+    ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashSet, ConstructorTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashSet, LookupTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(FlatHashSet, ModifiersTest, SetTypes);
+
+TEST(FlatHashSet, EmplaceString) {
+  std::vector<std::string> v = {"a", "b"};
+  absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
+  EXPECT_THAT(hs, UnorderedElementsAreArray(v));
+}
+
+TEST(FlatHashSet, BitfieldArgument) {
+  union {
+    int n : 1;
+  };
+  n = 0;
+  absl::flat_hash_set<int> s = {n};
+  s.insert(n);
+  s.insert(s.end(), n);
+  s.insert({n});
+  s.erase(n);
+  s.count(n);
+  s.prefetch(n);
+  s.find(n);
+  s.contains(n);
+  s.equal_range(n);
+}
+
+TEST(FlatHashSet, MergeExtractInsert) {
+  struct Hash {
+    size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
+  };
+  struct Eq {
+    bool operator()(const std::unique_ptr<int>& a,
+                    const std::unique_ptr<int>& b) const {
+      return *a == *b;
+    }
+  };
+  absl::flat_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
+  set1.insert(absl::make_unique<int>(7));
+  set1.insert(absl::make_unique<int>(17));
+
+  set2.insert(absl::make_unique<int>(7));
+  set2.insert(absl::make_unique<int>(19));
+
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
+
+  set1.merge(set2);
+
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
+
+  auto node = set1.extract(absl::make_unique<int>(7));
+  EXPECT_TRUE(node);
+  EXPECT_THAT(node.value(), Pointee(7));
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
+
+  auto insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_FALSE(insert_result.inserted);
+  EXPECT_TRUE(insert_result.node);
+  EXPECT_THAT(insert_result.node.value(), Pointee(7));
+  EXPECT_EQ(**insert_result.position, 7);
+  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
+
+  node = set1.extract(absl::make_unique<int>(17));
+  EXPECT_TRUE(node);
+  EXPECT_THAT(node.value(), Pointee(17));
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
+
+  node.value() = absl::make_unique<int>(23);
+
+  insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_TRUE(insert_result.inserted);
+  EXPECT_FALSE(insert_result.node);
+  EXPECT_EQ(**insert_result.position, 23);
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h
index ca36fd3..12756bb 100644
--- a/absl/container/inlined_vector.h
+++ b/absl/container/inlined_vector.h
@@ -620,6 +620,12 @@
   // Returns the allocator of this inlined vector.
   allocator_type get_allocator() const { return allocator(); }
 
+  template <typename H>
+  friend H AbslHashValue(H h, const InlinedVector& v) {
+    return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
+                      v.size());
+  }
+
  private:
   static_assert(N > 0, "inlined vector with nonpositive size");
 
@@ -648,8 +654,7 @@
   // our instance of it for free.
   class AllocatorAndTag : private allocator_type {
    public:
-    explicit AllocatorAndTag(const allocator_type& a, Tag t = Tag())
-        : allocator_type(a), tag_(t) {}
+    explicit AllocatorAndTag(const allocator_type& a) : allocator_type(a) {}
     Tag& tag() { return tag_; }
     const Tag& tag() const { return tag_; }
     allocator_type& allocator() { return *this; }
diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc
index 24f2174..a3ad0f8 100644
--- a/absl/container/inlined_vector_benchmark.cc
+++ b/absl/container/inlined_vector_benchmark.cc
@@ -66,7 +66,7 @@
 // The purpose of the next two benchmarks is to verify that
 // absl::InlinedVector is efficient when moving is more efficent than
 // copying. To do so, we use strings that are larger than the short
-// std::string optimization.
+// string optimization.
 bool StringRepresentedInline(std::string s) {
   const char* chars = s.data();
   std::string s1 = std::move(s);
diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc
index 196a1be..08dcd3e 100644
--- a/absl/container/inlined_vector_test.cc
+++ b/absl/container/inlined_vector_test.cc
@@ -31,6 +31,7 @@
 #include "absl/base/internal/raw_logging.h"
 #include "absl/base/macros.h"
 #include "absl/container/internal/test_instance_tracker.h"
+#include "absl/hash/hash_testing.h"
 #include "absl/memory/memory.h"
 #include "absl/strings/str_cat.h"
 
@@ -1788,4 +1789,5 @@
     EXPECT_THAT(v, AllOf(SizeIs(len), Each(0)));
   }
 }
+
 }  // anonymous namespace
diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h
new file mode 100644
index 0000000..56c5d2d
--- /dev/null
+++ b/absl/container/internal/container_memory.h
@@ -0,0 +1,405 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_
+#define ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_
+
+#ifdef ADDRESS_SANITIZER
+#include <sanitizer/asan_interface.h>
+#endif
+
+#ifdef MEMORY_SANITIZER
+#include <sanitizer/msan_interface.h>
+#endif
+
+#include <cassert>
+#include <cstddef>
+#include <memory>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include "absl/memory/memory.h"
+#include "absl/utility/utility.h"
+
+namespace absl {
+namespace container_internal {
+
+// Allocates at least n bytes aligned to the specified alignment.
+// Alignment must be a power of 2. It must be positive.
+//
+// Note that many allocators don't honor alignment requirements above certain
+// threshold (usually either alignof(std::max_align_t) or alignof(void*)).
+// Allocate() doesn't apply alignment corrections. If the underlying allocator
+// returns insufficiently alignment pointer, that's what you are going to get.
+template <size_t Alignment, class Alloc>
+void* Allocate(Alloc* alloc, size_t n) {
+  static_assert(Alignment > 0, "");
+  assert(n && "n must be positive");
+  struct alignas(Alignment) M {};
+  using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
+  using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
+  A mem_alloc(*alloc);
+  void* p = AT::allocate(mem_alloc, (n + sizeof(M) - 1) / sizeof(M));
+  assert(reinterpret_cast<uintptr_t>(p) % Alignment == 0 &&
+         "allocator does not respect alignment");
+  return p;
+}
+
+// The pointer must have been previously obtained by calling
+// Allocate<Alignment>(alloc, n).
+template <size_t Alignment, class Alloc>
+void Deallocate(Alloc* alloc, void* p, size_t n) {
+  static_assert(Alignment > 0, "");
+  assert(n && "n must be positive");
+  struct alignas(Alignment) M {};
+  using A = typename absl::allocator_traits<Alloc>::template rebind_alloc<M>;
+  using AT = typename absl::allocator_traits<Alloc>::template rebind_traits<M>;
+  A mem_alloc(*alloc);
+  AT::deallocate(mem_alloc, static_cast<M*>(p),
+                 (n + sizeof(M) - 1) / sizeof(M));
+}
+
+namespace memory_internal {
+
+// Constructs T into uninitialized storage pointed by `ptr` using the args
+// specified in the tuple.
+template <class Alloc, class T, class Tuple, size_t... I>
+void ConstructFromTupleImpl(Alloc* alloc, T* ptr, Tuple&& t,
+                            absl::index_sequence<I...>) {
+  absl::allocator_traits<Alloc>::construct(
+      *alloc, ptr, std::get<I>(std::forward<Tuple>(t))...);
+}
+
+template <class T, class F>
+struct WithConstructedImplF {
+  template <class... Args>
+  decltype(std::declval<F>()(std::declval<T>())) operator()(
+      Args&&... args) const {
+    return std::forward<F>(f)(T(std::forward<Args>(args)...));
+  }
+  F&& f;
+};
+
+template <class T, class Tuple, size_t... Is, class F>
+decltype(std::declval<F>()(std::declval<T>())) WithConstructedImpl(
+    Tuple&& t, absl::index_sequence<Is...>, F&& f) {
+  return WithConstructedImplF<T, F>{std::forward<F>(f)}(
+      std::get<Is>(std::forward<Tuple>(t))...);
+}
+
+template <class T, size_t... Is>
+auto TupleRefImpl(T&& t, absl::index_sequence<Is...>)
+    -> decltype(std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...)) {
+  return std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...);
+}
+
+// Returns a tuple of references to the elements of the input tuple. T must be a
+// tuple.
+template <class T>
+auto TupleRef(T&& t) -> decltype(
+    TupleRefImpl(std::forward<T>(t),
+                 absl::make_index_sequence<
+                     std::tuple_size<typename std::decay<T>::type>::value>())) {
+  return TupleRefImpl(
+      std::forward<T>(t),
+      absl::make_index_sequence<
+          std::tuple_size<typename std::decay<T>::type>::value>());
+}
+
+template <class F, class K, class V>
+decltype(std::declval<F>()(std::declval<const K&>(), std::piecewise_construct,
+                           std::declval<std::tuple<K>>(), std::declval<V>()))
+DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
+  const auto& key = std::get<0>(p.first);
+  return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
+                            std::move(p.second));
+}
+
+}  // namespace memory_internal
+
+// Constructs T into uninitialized storage pointed by `ptr` using the args
+// specified in the tuple.
+template <class Alloc, class T, class Tuple>
+void ConstructFromTuple(Alloc* alloc, T* ptr, Tuple&& t) {
+  memory_internal::ConstructFromTupleImpl(
+      alloc, ptr, std::forward<Tuple>(t),
+      absl::make_index_sequence<
+          std::tuple_size<typename std::decay<Tuple>::type>::value>());
+}
+
+// Constructs T using the args specified in the tuple and calls F with the
+// constructed value.
+template <class T, class Tuple, class F>
+decltype(std::declval<F>()(std::declval<T>())) WithConstructed(
+    Tuple&& t, F&& f) {
+  return memory_internal::WithConstructedImpl<T>(
+      std::forward<Tuple>(t),
+      absl::make_index_sequence<
+          std::tuple_size<typename std::decay<Tuple>::type>::value>(),
+      std::forward<F>(f));
+}
+
+// Given arguments of an std::pair's consructor, PairArgs() returns a pair of
+// tuples with references to the passed arguments. The tuples contain
+// constructor arguments for the first and the second elements of the pair.
+//
+// The following two snippets are equivalent.
+//
+// 1. std::pair<F, S> p(args...);
+//
+// 2. auto a = PairArgs(args...);
+//    std::pair<F, S> p(std::piecewise_construct,
+//                      std::move(p.first), std::move(p.second));
+inline std::pair<std::tuple<>, std::tuple<>> PairArgs() { return {}; }
+template <class F, class S>
+std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) {
+  return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)),
+          std::forward_as_tuple(std::forward<S>(s))};
+}
+template <class F, class S>
+std::pair<std::tuple<const F&>, std::tuple<const S&>> PairArgs(
+    const std::pair<F, S>& p) {
+  return PairArgs(p.first, p.second);
+}
+template <class F, class S>
+std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(std::pair<F, S>&& p) {
+  return PairArgs(std::forward<F>(p.first), std::forward<S>(p.second));
+}
+template <class F, class S>
+auto PairArgs(std::piecewise_construct_t, F&& f, S&& s)
+    -> decltype(std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
+                               memory_internal::TupleRef(std::forward<S>(s)))) {
+  return std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
+                        memory_internal::TupleRef(std::forward<S>(s)));
+}
+
+// A helper function for implementing apply() in map policies.
+template <class F, class... Args>
+auto DecomposePair(F&& f, Args&&... args)
+    -> decltype(memory_internal::DecomposePairImpl(
+        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
+  return memory_internal::DecomposePairImpl(
+      std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
+}
+
+// A helper function for implementing apply() in set policies.
+template <class F, class Arg>
+decltype(std::declval<F>()(std::declval<const Arg&>(), std::declval<Arg>()))
+DecomposeValue(F&& f, Arg&& arg) {
+  const auto& key = arg;
+  return std::forward<F>(f)(key, std::forward<Arg>(arg));
+}
+
+// Helper functions for asan and msan.
+inline void SanitizerPoisonMemoryRegion(const void* m, size_t s) {
+#ifdef ADDRESS_SANITIZER
+  ASAN_POISON_MEMORY_REGION(m, s);
+#endif
+#ifdef MEMORY_SANITIZER
+  __msan_poison(m, s);
+#endif
+  (void)m;
+  (void)s;
+}
+
+inline void SanitizerUnpoisonMemoryRegion(const void* m, size_t s) {
+#ifdef ADDRESS_SANITIZER
+  ASAN_UNPOISON_MEMORY_REGION(m, s);
+#endif
+#ifdef MEMORY_SANITIZER
+  __msan_unpoison(m, s);
+#endif
+  (void)m;
+  (void)s;
+}
+
+template <typename T>
+inline void SanitizerPoisonObject(const T* object) {
+  SanitizerPoisonMemoryRegion(object, sizeof(T));
+}
+
+template <typename T>
+inline void SanitizerUnpoisonObject(const T* object) {
+  SanitizerUnpoisonMemoryRegion(object, sizeof(T));
+}
+
+namespace memory_internal {
+
+// If Pair is a standard-layout type, OffsetOf<Pair>::kFirst and
+// OffsetOf<Pair>::kSecond are equivalent to offsetof(Pair, first) and
+// offsetof(Pair, second) respectively. Otherwise they are -1.
+//
+// The purpose of OffsetOf is to avoid calling offsetof() on non-standard-layout
+// type, which is non-portable.
+template <class Pair, class = std::true_type>
+struct OffsetOf {
+  static constexpr size_t kFirst = -1;
+  static constexpr size_t kSecond = -1;
+};
+
+template <class Pair>
+struct OffsetOf<Pair, typename std::is_standard_layout<Pair>::type> {
+  static constexpr size_t kFirst = offsetof(Pair, first);
+  static constexpr size_t kSecond = offsetof(Pair, second);
+};
+
+template <class K, class V>
+struct IsLayoutCompatible {
+ private:
+  struct Pair {
+    K first;
+    V second;
+  };
+
+  // Is P layout-compatible with Pair?
+  template <class P>
+  static constexpr bool LayoutCompatible() {
+    return std::is_standard_layout<P>() && sizeof(P) == sizeof(Pair) &&
+           alignof(P) == alignof(Pair) &&
+           memory_internal::OffsetOf<P>::kFirst ==
+               memory_internal::OffsetOf<Pair>::kFirst &&
+           memory_internal::OffsetOf<P>::kSecond ==
+               memory_internal::OffsetOf<Pair>::kSecond;
+  }
+
+ public:
+  // Whether pair<const K, V> and pair<K, V> are layout-compatible. If they are,
+  // then it is safe to store them in a union and read from either.
+  static constexpr bool value = std::is_standard_layout<K>() &&
+                                std::is_standard_layout<Pair>() &&
+                                memory_internal::OffsetOf<Pair>::kFirst == 0 &&
+                                LayoutCompatible<std::pair<K, V>>() &&
+                                LayoutCompatible<std::pair<const K, V>>();
+};
+
+}  // namespace memory_internal
+
+// If kMutableKeys is false, only the value member is accessed.
+//
+// If kMutableKeys is true, key is accessed through all slots while value and
+// mutable_value are accessed only via INITIALIZED slots. Slots are created and
+// destroyed via mutable_value so that the key can be moved later.
+template <class K, class V>
+union slot_type {
+ private:
+  static void emplace(slot_type* slot) {
+    // The construction of union doesn't do anything at runtime but it allows us
+    // to access its members without violating aliasing rules.
+    new (slot) slot_type;
+  }
+  // If pair<const K, V> and pair<K, V> are layout-compatible, we can accept one
+  // or the other via slot_type. We are also free to access the key via
+  // slot_type::key in this case.
+  using kMutableKeys =
+      std::integral_constant<bool,
+                             memory_internal::IsLayoutCompatible<K, V>::value>;
+
+ public:
+  slot_type() {}
+  ~slot_type() = delete;
+  using value_type = std::pair<const K, V>;
+  using mutable_value_type = std::pair<K, V>;
+
+  value_type value;
+  mutable_value_type mutable_value;
+  K key;
+
+  template <class Allocator, class... Args>
+  static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
+    emplace(slot);
+    if (kMutableKeys::value) {
+      absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value,
+                                                   std::forward<Args>(args)...);
+    } else {
+      absl::allocator_traits<Allocator>::construct(*alloc, &slot->value,
+                                                   std::forward<Args>(args)...);
+    }
+  }
+
+  // Construct this slot by moving from another slot.
+  template <class Allocator>
+  static void construct(Allocator* alloc, slot_type* slot, slot_type* other) {
+    emplace(slot);
+    if (kMutableKeys::value) {
+      absl::allocator_traits<Allocator>::construct(
+          *alloc, &slot->mutable_value, std::move(other->mutable_value));
+    } else {
+      absl::allocator_traits<Allocator>::construct(*alloc, &slot->value,
+                                                   std::move(other->value));
+    }
+  }
+
+  template <class Allocator>
+  static void destroy(Allocator* alloc, slot_type* slot) {
+    if (kMutableKeys::value) {
+      absl::allocator_traits<Allocator>::destroy(*alloc, &slot->mutable_value);
+    } else {
+      absl::allocator_traits<Allocator>::destroy(*alloc, &slot->value);
+    }
+  }
+
+  template <class Allocator>
+  static void transfer(Allocator* alloc, slot_type* new_slot,
+                       slot_type* old_slot) {
+    emplace(new_slot);
+    if (kMutableKeys::value) {
+      absl::allocator_traits<Allocator>::construct(
+          *alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value));
+    } else {
+      absl::allocator_traits<Allocator>::construct(*alloc, &new_slot->value,
+                                                   std::move(old_slot->value));
+    }
+    destroy(alloc, old_slot);
+  }
+
+  template <class Allocator>
+  static void swap(Allocator* alloc, slot_type* a, slot_type* b) {
+    if (kMutableKeys::value) {
+      using std::swap;
+      swap(a->mutable_value, b->mutable_value);
+    } else {
+      value_type tmp = std::move(a->value);
+      absl::allocator_traits<Allocator>::destroy(*alloc, &a->value);
+      absl::allocator_traits<Allocator>::construct(*alloc, &a->value,
+                                                   std::move(b->value));
+      absl::allocator_traits<Allocator>::destroy(*alloc, &b->value);
+      absl::allocator_traits<Allocator>::construct(*alloc, &b->value,
+                                                   std::move(tmp));
+    }
+  }
+
+  template <class Allocator>
+  static void move(Allocator* alloc, slot_type* src, slot_type* dest) {
+    if (kMutableKeys::value) {
+      dest->mutable_value = std::move(src->mutable_value);
+    } else {
+      absl::allocator_traits<Allocator>::destroy(*alloc, &dest->value);
+      absl::allocator_traits<Allocator>::construct(*alloc, &dest->value,
+                                                   std::move(src->value));
+    }
+  }
+
+  template <class Allocator>
+  static void move(Allocator* alloc, slot_type* first, slot_type* last,
+                   slot_type* result) {
+    for (slot_type *src = first, *dest = result; src != last; ++src, ++dest)
+      move(alloc, src, dest);
+  }
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_CONTAINER_MEMORY_H_
diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc
new file mode 100644
index 0000000..f1c4058
--- /dev/null
+++ b/absl/container/internal/container_memory_test.cc
@@ -0,0 +1,188 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/container_memory.h"
+
+#include <cstdint>
+#include <tuple>
+#include <utility>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::testing::Pair;
+
+TEST(Memory, AlignmentLargerThanBase) {
+  std::allocator<int8_t> alloc;
+  void* mem = Allocate<2>(&alloc, 3);
+  EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
+  memcpy(mem, "abc", 3);
+  Deallocate<2>(&alloc, mem, 3);
+}
+
+TEST(Memory, AlignmentSmallerThanBase) {
+  std::allocator<int64_t> alloc;
+  void* mem = Allocate<2>(&alloc, 3);
+  EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
+  memcpy(mem, "abc", 3);
+  Deallocate<2>(&alloc, mem, 3);
+}
+
+class Fixture : public ::testing::Test {
+  using Alloc = std::allocator<std::string>;
+
+ public:
+  Fixture() { ptr_ = std::allocator_traits<Alloc>::allocate(*alloc(), 1); }
+  ~Fixture() override {
+    std::allocator_traits<Alloc>::destroy(*alloc(), ptr_);
+    std::allocator_traits<Alloc>::deallocate(*alloc(), ptr_, 1);
+  }
+  std::string* ptr() { return ptr_; }
+  Alloc* alloc() { return &alloc_; }
+
+ private:
+  Alloc alloc_;
+  std::string* ptr_;
+};
+
+TEST_F(Fixture, ConstructNoArgs) {
+  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple());
+  EXPECT_EQ(*ptr(), "");
+}
+
+TEST_F(Fixture, ConstructOneArg) {
+  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple("abcde"));
+  EXPECT_EQ(*ptr(), "abcde");
+}
+
+TEST_F(Fixture, ConstructTwoArg) {
+  ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple(5, 'a'));
+  EXPECT_EQ(*ptr(), "aaaaa");
+}
+
+TEST(PairArgs, NoArgs) {
+  EXPECT_THAT(PairArgs(),
+              Pair(std::forward_as_tuple(), std::forward_as_tuple()));
+}
+
+TEST(PairArgs, TwoArgs) {
+  EXPECT_EQ(
+      std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
+      PairArgs(1, 'A'));
+}
+
+TEST(PairArgs, Pair) {
+  EXPECT_EQ(
+      std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
+      PairArgs(std::make_pair(1, 'A')));
+}
+
+TEST(PairArgs, Piecewise) {
+  EXPECT_EQ(
+      std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
+      PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
+               std::forward_as_tuple('A')));
+}
+
+TEST(WithConstructed, Simple) {
+  EXPECT_EQ(1, WithConstructed<absl::string_view>(
+                   std::make_tuple(std::string("a")),
+                   [](absl::string_view str) { return str.size(); }));
+}
+
+template <class F, class Arg>
+decltype(DecomposeValue(std::declval<F>(), std::declval<Arg>()))
+DecomposeValueImpl(int, F&& f, Arg&& arg) {
+  return DecomposeValue(std::forward<F>(f), std::forward<Arg>(arg));
+}
+
+template <class F, class Arg>
+const char* DecomposeValueImpl(char, F&& f, Arg&& arg) {
+  return "not decomposable";
+}
+
+template <class F, class Arg>
+decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
+TryDecomposeValue(F&& f, Arg&& arg) {
+  return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(arg));
+}
+
+TEST(DecomposeValue, Decomposable) {
+  auto f = [](const int& x, int&& y) {
+    EXPECT_EQ(&x, &y);
+    EXPECT_EQ(42, x);
+    return 'A';
+  };
+  EXPECT_EQ('A', TryDecomposeValue(f, 42));
+}
+
+TEST(DecomposeValue, NotDecomposable) {
+  auto f = [](void*) {
+    ADD_FAILURE() << "Must not be called";
+    return 'A';
+  };
+  EXPECT_STREQ("not decomposable", TryDecomposeValue(f, 42));
+}
+
+template <class F, class... Args>
+decltype(DecomposePair(std::declval<F>(), std::declval<Args>()...))
+DecomposePairImpl(int, F&& f, Args&&... args) {
+  return DecomposePair(std::forward<F>(f), std::forward<Args>(args)...);
+}
+
+template <class F, class... Args>
+const char* DecomposePairImpl(char, F&& f, Args&&... args) {
+  return "not decomposable";
+}
+
+template <class F, class... Args>
+decltype(DecomposePairImpl(0, std::declval<F>(), std::declval<Args>()...))
+TryDecomposePair(F&& f, Args&&... args) {
+  return DecomposePairImpl(0, std::forward<F>(f), std::forward<Args>(args)...);
+}
+
+TEST(DecomposePair, Decomposable) {
+  auto f = [](const int& x, std::piecewise_construct_t, std::tuple<int&&> k,
+              std::tuple<double>&& v) {
+    EXPECT_EQ(&x, &std::get<0>(k));
+    EXPECT_EQ(42, x);
+    EXPECT_EQ(0.5, std::get<0>(v));
+    return 'A';
+  };
+  EXPECT_EQ('A', TryDecomposePair(f, 42, 0.5));
+  EXPECT_EQ('A', TryDecomposePair(f, std::make_pair(42, 0.5)));
+  EXPECT_EQ('A', TryDecomposePair(f, std::piecewise_construct,
+                                  std::make_tuple(42), std::make_tuple(0.5)));
+}
+
+TEST(DecomposePair, NotDecomposable) {
+  auto f = [](...) {
+    ADD_FAILURE() << "Must not be called";
+    return 'A';
+  };
+  EXPECT_STREQ("not decomposable",
+               TryDecomposePair(f));
+  EXPECT_STREQ("not decomposable",
+               TryDecomposePair(f, std::piecewise_construct, std::make_tuple(),
+                                std::make_tuple(0.5)));
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/hash_function_defaults.h b/absl/container/internal/hash_function_defaults.h
new file mode 100644
index 0000000..1f0d794
--- /dev/null
+++ b/absl/container/internal/hash_function_defaults.h
@@ -0,0 +1,143 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Define the default Hash and Eq functions for SwissTable containers.
+//
+// std::hash<T> and std::equal_to<T> are not appropriate hash and equal
+// functions for SwissTable containers. There are two reasons for this.
+//
+// SwissTable containers are power of 2 sized containers:
+//
+// This means they use the lower bits of the hash value to find the slot for
+// each entry. The typical hash function for integral types is the identity.
+// This is a very weak hash function for SwissTable and any power of 2 sized
+// hashtable implementation which will lead to excessive collisions. For
+// SwissTable we use murmur3 style mixing to reduce collisions to a minimum.
+//
+// SwissTable containers support heterogeneous lookup:
+//
+// In order to make heterogeneous lookup work, hash and equal functions must be
+// polymorphic. At the same time they have to satisfy the same requirements the
+// C++ standard imposes on hash functions and equality operators. That is:
+//
+//   if hash_default_eq<T>(a, b) returns true for any a and b of type T, then
+//   hash_default_hash<T>(a) must equal hash_default_hash<T>(b)
+//
+// For SwissTable containers this requirement is relaxed to allow a and b of
+// any and possibly different types. Note that like the standard the hash and
+// equal functions are still bound to T. This is important because some type U
+// can be hashed by/tested for equality differently depending on T. A notable
+// example is `const char*`. `const char*` is treated as a c-style string when
+// the hash function is hash<string> but as a pointer when the hash function is
+// hash<void*>.
+//
+#ifndef ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
+#define ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
+
+#include <stdint.h>
+#include <cstddef>
+#include <memory>
+#include <string>
+#include <type_traits>
+
+#include "absl/base/config.h"
+#include "absl/hash/hash.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+
+// The hash of an object of type T is computed by using absl::Hash.
+template <class T, class E = void>
+struct HashEq {
+  using Hash = absl::Hash<T>;
+  using Eq = std::equal_to<T>;
+};
+
+struct StringHash {
+  using is_transparent = void;
+
+  size_t operator()(absl::string_view v) const {
+    return absl::Hash<absl::string_view>{}(v);
+  }
+};
+
+// Supports heterogeneous lookup for string-like elements.
+struct StringHashEq {
+  using Hash = StringHash;
+  struct Eq {
+    using is_transparent = void;
+    bool operator()(absl::string_view lhs, absl::string_view rhs) const {
+      return lhs == rhs;
+    }
+  };
+};
+template <>
+struct HashEq<std::string> : StringHashEq {};
+template <>
+struct HashEq<absl::string_view> : StringHashEq {};
+
+// Supports heterogeneous lookup for pointers and smart pointers.
+template <class T>
+struct HashEq<T*> {
+  struct Hash {
+    using is_transparent = void;
+    template <class U>
+    size_t operator()(const U& ptr) const {
+      return absl::Hash<const T*>{}(HashEq::ToPtr(ptr));
+    }
+  };
+  struct Eq {
+    using is_transparent = void;
+    template <class A, class B>
+    bool operator()(const A& a, const B& b) const {
+      return HashEq::ToPtr(a) == HashEq::ToPtr(b);
+    }
+  };
+
+ private:
+  static const T* ToPtr(const T* ptr) { return ptr; }
+  template <class U, class D>
+  static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
+    return ptr.get();
+  }
+  template <class U>
+  static const T* ToPtr(const std::shared_ptr<U>& ptr) {
+    return ptr.get();
+  }
+};
+
+template <class T, class D>
+struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
+template <class T>
+struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
+
+// This header's visibility is restricted.  If you need to access the default
+// hasher please use the container's ::hasher alias instead.
+//
+// Example: typename Hash = typename absl::flat_hash_map<K, V>::hasher
+template <class T>
+using hash_default_hash = typename container_internal::HashEq<T>::Hash;
+
+// This header's visibility is restricted.  If you need to access the default
+// key equal please use the container's ::key_equal alias instead.
+//
+// Example: typename Eq = typename absl::flat_hash_map<K, V, Hash>::key_equal
+template <class T>
+using hash_default_eq = typename container_internal::HashEq<T>::Eq;
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASH_FUNCTION_DEFAULTS_H_
diff --git a/absl/container/internal/hash_function_defaults_test.cc b/absl/container/internal/hash_function_defaults_test.cc
new file mode 100644
index 0000000..464baae
--- /dev/null
+++ b/absl/container/internal/hash_function_defaults_test.cc
@@ -0,0 +1,299 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/hash_function_defaults.h"
+
+#include <functional>
+#include <type_traits>
+#include <utility>
+
+#include "gtest/gtest.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::testing::Types;
+
+TEST(Eq, Int32) {
+  hash_default_eq<int32_t> eq;
+  EXPECT_TRUE(eq(1, 1u));
+  EXPECT_TRUE(eq(1, char{1}));
+  EXPECT_TRUE(eq(1, true));
+  EXPECT_TRUE(eq(1, double{1.1}));
+  EXPECT_FALSE(eq(1, char{2}));
+  EXPECT_FALSE(eq(1, 2u));
+  EXPECT_FALSE(eq(1, false));
+  EXPECT_FALSE(eq(1, 2.));
+}
+
+TEST(Hash, Int32) {
+  hash_default_hash<int32_t> hash;
+  auto h = hash(1);
+  EXPECT_EQ(h, hash(1u));
+  EXPECT_EQ(h, hash(char{1}));
+  EXPECT_EQ(h, hash(true));
+  EXPECT_EQ(h, hash(double{1.1}));
+  EXPECT_NE(h, hash(2u));
+  EXPECT_NE(h, hash(char{2}));
+  EXPECT_NE(h, hash(false));
+  EXPECT_NE(h, hash(2.));
+}
+
+enum class MyEnum { A, B, C, D };
+
+TEST(Eq, Enum) {
+  hash_default_eq<MyEnum> eq;
+  EXPECT_TRUE(eq(MyEnum::A, MyEnum::A));
+  EXPECT_FALSE(eq(MyEnum::A, MyEnum::B));
+}
+
+TEST(Hash, Enum) {
+  hash_default_hash<MyEnum> hash;
+
+  for (MyEnum e : {MyEnum::A, MyEnum::B, MyEnum::C}) {
+    auto h = hash(e);
+    EXPECT_EQ(h, hash_default_hash<int>{}(static_cast<int>(e)));
+    EXPECT_NE(h, hash(MyEnum::D));
+  }
+}
+
+using StringTypes = ::testing::Types<std::string, absl::string_view>;
+
+template <class T>
+struct EqString : ::testing::Test {
+  hash_default_eq<T> key_eq;
+};
+
+TYPED_TEST_CASE(EqString, StringTypes);
+
+template <class T>
+struct HashString : ::testing::Test {
+  hash_default_hash<T> hasher;
+};
+
+TYPED_TEST_CASE(HashString, StringTypes);
+
+TYPED_TEST(EqString, Works) {
+  auto eq = this->key_eq;
+  EXPECT_TRUE(eq("a", "a"));
+  EXPECT_TRUE(eq("a", absl::string_view("a")));
+  EXPECT_TRUE(eq("a", std::string("a")));
+  EXPECT_FALSE(eq("a", "b"));
+  EXPECT_FALSE(eq("a", absl::string_view("b")));
+  EXPECT_FALSE(eq("a", std::string("b")));
+}
+
+TYPED_TEST(HashString, Works) {
+  auto hash = this->hasher;
+  auto h = hash("a");
+  EXPECT_EQ(h, hash(absl::string_view("a")));
+  EXPECT_EQ(h, hash(std::string("a")));
+  EXPECT_NE(h, hash(absl::string_view("b")));
+  EXPECT_NE(h, hash(std::string("b")));
+}
+
+struct NoDeleter {
+  template <class T>
+  void operator()(const T* ptr) const {}
+};
+
+using PointerTypes =
+    ::testing::Types<const int*, int*, std::unique_ptr<const int>,
+                     std::unique_ptr<const int, NoDeleter>,
+                     std::unique_ptr<int>, std::unique_ptr<int, NoDeleter>,
+                     std::shared_ptr<const int>, std::shared_ptr<int>>;
+
+template <class T>
+struct EqPointer : ::testing::Test {
+  hash_default_eq<T> key_eq;
+};
+
+TYPED_TEST_CASE(EqPointer, PointerTypes);
+
+template <class T>
+struct HashPointer : ::testing::Test {
+  hash_default_hash<T> hasher;
+};
+
+TYPED_TEST_CASE(HashPointer, PointerTypes);
+
+TYPED_TEST(EqPointer, Works) {
+  int dummy;
+  auto eq = this->key_eq;
+  auto sptr = std::make_shared<int>();
+  std::shared_ptr<const int> csptr = sptr;
+  int* ptr = sptr.get();
+  const int* cptr = ptr;
+  std::unique_ptr<int, NoDeleter> uptr(ptr);
+  std::unique_ptr<const int, NoDeleter> cuptr(ptr);
+
+  EXPECT_TRUE(eq(ptr, cptr));
+  EXPECT_TRUE(eq(ptr, sptr));
+  EXPECT_TRUE(eq(ptr, uptr));
+  EXPECT_TRUE(eq(ptr, csptr));
+  EXPECT_TRUE(eq(ptr, cuptr));
+  EXPECT_FALSE(eq(&dummy, cptr));
+  EXPECT_FALSE(eq(&dummy, sptr));
+  EXPECT_FALSE(eq(&dummy, uptr));
+  EXPECT_FALSE(eq(&dummy, csptr));
+  EXPECT_FALSE(eq(&dummy, cuptr));
+}
+
+TEST(Hash, DerivedAndBase) {
+  struct Base {};
+  struct Derived : Base {};
+
+  hash_default_hash<Base*> hasher;
+
+  Base base;
+  Derived derived;
+  EXPECT_NE(hasher(&base), hasher(&derived));
+  EXPECT_EQ(hasher(static_cast<Base*>(&derived)), hasher(&derived));
+
+  auto dp = std::make_shared<Derived>();
+  EXPECT_EQ(hasher(static_cast<Base*>(dp.get())), hasher(dp));
+}
+
+TEST(Hash, FunctionPointer) {
+  using Func = int (*)();
+  hash_default_hash<Func> hasher;
+  hash_default_eq<Func> eq;
+
+  Func p1 = [] { return 1; }, p2 = [] { return 2; };
+  EXPECT_EQ(hasher(p1), hasher(p1));
+  EXPECT_TRUE(eq(p1, p1));
+
+  EXPECT_NE(hasher(p1), hasher(p2));
+  EXPECT_FALSE(eq(p1, p2));
+}
+
+TYPED_TEST(HashPointer, Works) {
+  int dummy;
+  auto hash = this->hasher;
+  auto sptr = std::make_shared<int>();
+  std::shared_ptr<const int> csptr = sptr;
+  int* ptr = sptr.get();
+  const int* cptr = ptr;
+  std::unique_ptr<int, NoDeleter> uptr(ptr);
+  std::unique_ptr<const int, NoDeleter> cuptr(ptr);
+
+  EXPECT_EQ(hash(ptr), hash(cptr));
+  EXPECT_EQ(hash(ptr), hash(sptr));
+  EXPECT_EQ(hash(ptr), hash(uptr));
+  EXPECT_EQ(hash(ptr), hash(csptr));
+  EXPECT_EQ(hash(ptr), hash(cuptr));
+  EXPECT_NE(hash(&dummy), hash(cptr));
+  EXPECT_NE(hash(&dummy), hash(sptr));
+  EXPECT_NE(hash(&dummy), hash(uptr));
+  EXPECT_NE(hash(&dummy), hash(csptr));
+  EXPECT_NE(hash(&dummy), hash(cuptr));
+}
+
+// Cartesian product of (string, std::string, absl::string_view)
+// with (string, std::string, absl::string_view, const char*).
+using StringTypesCartesianProduct = Types<
+    // clang-format off
+
+    std::pair<std::string, std::string>,
+    std::pair<std::string, absl::string_view>,
+    std::pair<std::string, const char*>,
+
+    std::pair<absl::string_view, std::string>,
+    std::pair<absl::string_view, absl::string_view>,
+    std::pair<absl::string_view, const char*>>;
+// clang-format on
+
+constexpr char kFirstString[] = "abc123";
+constexpr char kSecondString[] = "ijk456";
+
+template <typename T>
+struct StringLikeTest : public ::testing::Test {
+  typename T::first_type a1{kFirstString};
+  typename T::second_type b1{kFirstString};
+  typename T::first_type a2{kSecondString};
+  typename T::second_type b2{kSecondString};
+  hash_default_eq<typename T::first_type> eq;
+  hash_default_hash<typename T::first_type> hash;
+};
+
+TYPED_TEST_CASE_P(StringLikeTest);
+
+TYPED_TEST_P(StringLikeTest, Eq) {
+  EXPECT_TRUE(this->eq(this->a1, this->b1));
+  EXPECT_TRUE(this->eq(this->b1, this->a1));
+}
+
+TYPED_TEST_P(StringLikeTest, NotEq) {
+  EXPECT_FALSE(this->eq(this->a1, this->b2));
+  EXPECT_FALSE(this->eq(this->b2, this->a1));
+}
+
+TYPED_TEST_P(StringLikeTest, HashEq) {
+  EXPECT_EQ(this->hash(this->a1), this->hash(this->b1));
+  EXPECT_EQ(this->hash(this->a2), this->hash(this->b2));
+  // It would be a poor hash function which collides on these strings.
+  EXPECT_NE(this->hash(this->a1), this->hash(this->b2));
+}
+
+TYPED_TEST_CASE(StringLikeTest, StringTypesCartesianProduct);
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
+
+enum Hash : size_t {
+  kStd = 0x2,       // std::hash
+#ifdef _MSC_VER
+  kExtension = kStd,  // In MSVC, std::hash == ::hash
+#else                 // _MSC_VER
+  kExtension = 0x4,  // ::hash (GCC extension)
+#endif                // _MSC_VER
+};
+
+// H is a bitmask of Hash enumerations.
+// Hashable<H> is hashable via all means specified in H.
+template <int H>
+struct Hashable {
+  static constexpr bool HashableBy(Hash h) { return h & H; }
+};
+
+namespace std {
+template <int H>
+struct hash<Hashable<H>> {
+  template <class E = Hashable<H>,
+            class = typename std::enable_if<E::HashableBy(kStd)>::type>
+  size_t operator()(E) const {
+    return kStd;
+  }
+};
+}  // namespace std
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+template <class T>
+size_t Hash(const T& v) {
+  return hash_default_hash<T>()(v);
+}
+
+TEST(Delegate, HashDispatch) {
+  EXPECT_EQ(Hash(kStd), Hash(Hashable<kStd>()));
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/hash_generator_testing.cc b/absl/container/internal/hash_generator_testing.cc
new file mode 100644
index 0000000..0d6a9df
--- /dev/null
+++ b/absl/container/internal/hash_generator_testing.cc
@@ -0,0 +1,72 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/hash_generator_testing.h"
+
+#include <deque>
+
+namespace absl {
+namespace container_internal {
+namespace hash_internal {
+namespace {
+
+class RandomDeviceSeedSeq {
+ public:
+  using result_type = typename std::random_device::result_type;
+
+  template <class Iterator>
+  void generate(Iterator start, Iterator end) {
+    while (start != end) {
+      *start = gen_();
+      ++start;
+    }
+  }
+
+ private:
+  std::random_device gen_;
+};
+
+}  // namespace
+
+std::mt19937_64* GetThreadLocalRng() {
+  RandomDeviceSeedSeq seed_seq;
+  thread_local auto* rng = new std::mt19937_64(seed_seq);
+  return rng;
+}
+
+std::string Generator<std::string>::operator()() const {
+  // NOLINTNEXTLINE(runtime/int)
+  std::uniform_int_distribution<short> chars(0x20, 0x7E);
+  std::string res;
+  res.resize(32);
+  std::generate(res.begin(), res.end(),
+                [&]() { return chars(*GetThreadLocalRng()); });
+  return res;
+}
+
+absl::string_view Generator<absl::string_view>::operator()() const {
+  static auto* arena = new std::deque<std::string>();
+  // NOLINTNEXTLINE(runtime/int)
+  std::uniform_int_distribution<short> chars(0x20, 0x7E);
+  arena->emplace_back();
+  auto& res = arena->back();
+  res.resize(32);
+  std::generate(res.begin(), res.end(),
+                [&]() { return chars(*GetThreadLocalRng()); });
+  return res;
+}
+
+}  // namespace hash_internal
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/hash_generator_testing.h b/absl/container/internal/hash_generator_testing.h
new file mode 100644
index 0000000..50d7710
--- /dev/null
+++ b/absl/container/internal/hash_generator_testing.h
@@ -0,0 +1,150 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Generates random values for testing. Specialized only for the few types we
+// care about.
+
+#ifndef ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
+#define ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
+
+#include <stdint.h>
+#include <algorithm>
+#include <iosfwd>
+#include <random>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include "absl/container/internal/hash_policy_testing.h"
+#include "absl/meta/type_traits.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+namespace hash_internal {
+namespace generator_internal {
+
+template <class Container, class = void>
+struct IsMap : std::false_type {};
+
+template <class Map>
+struct IsMap<Map, absl::void_t<typename Map::mapped_type>> : std::true_type {};
+
+}  // namespace generator_internal
+
+std::mt19937_64* GetThreadLocalRng();
+
+enum Enum {
+  kEnumEmpty,
+  kEnumDeleted,
+};
+
+enum class EnumClass : uint64_t {
+  kEmpty,
+  kDeleted,
+};
+
+inline std::ostream& operator<<(std::ostream& o, const EnumClass& ec) {
+  return o << static_cast<uint64_t>(ec);
+}
+
+template <class T, class E = void>
+struct Generator;
+
+template <class T>
+struct Generator<T, typename std::enable_if<std::is_integral<T>::value>::type> {
+  T operator()() const {
+    std::uniform_int_distribution<T> dist;
+    return dist(*GetThreadLocalRng());
+  }
+};
+
+template <>
+struct Generator<Enum> {
+  Enum operator()() const {
+    std::uniform_int_distribution<typename std::underlying_type<Enum>::type>
+        dist;
+    while (true) {
+      auto variate = dist(*GetThreadLocalRng());
+      if (variate != kEnumEmpty && variate != kEnumDeleted)
+        return static_cast<Enum>(variate);
+    }
+  }
+};
+
+template <>
+struct Generator<EnumClass> {
+  EnumClass operator()() const {
+    std::uniform_int_distribution<
+        typename std::underlying_type<EnumClass>::type>
+        dist;
+    while (true) {
+      EnumClass variate = static_cast<EnumClass>(dist(*GetThreadLocalRng()));
+      if (variate != EnumClass::kEmpty && variate != EnumClass::kDeleted)
+        return static_cast<EnumClass>(variate);
+    }
+  }
+};
+
+template <>
+struct Generator<std::string> {
+  std::string operator()() const;
+};
+
+template <>
+struct Generator<absl::string_view> {
+  absl::string_view operator()() const;
+};
+
+template <>
+struct Generator<NonStandardLayout> {
+  NonStandardLayout operator()() const {
+    return NonStandardLayout(Generator<std::string>()());
+  }
+};
+
+template <class K, class V>
+struct Generator<std::pair<K, V>> {
+  std::pair<K, V> operator()() const {
+    return std::pair<K, V>(Generator<typename std::decay<K>::type>()(),
+                           Generator<typename std::decay<V>::type>()());
+  }
+};
+
+template <class... Ts>
+struct Generator<std::tuple<Ts...>> {
+  std::tuple<Ts...> operator()() const {
+    return std::tuple<Ts...>(Generator<typename std::decay<Ts>::type>()()...);
+  }
+};
+
+template <class U>
+struct Generator<U, absl::void_t<decltype(std::declval<U&>().key()),
+                                decltype(std::declval<U&>().value())>>
+    : Generator<std::pair<
+          typename std::decay<decltype(std::declval<U&>().key())>::type,
+          typename std::decay<decltype(std::declval<U&>().value())>::type>> {};
+
+template <class Container>
+using GeneratedType = decltype(
+    std::declval<const Generator<
+        typename std::conditional<generator_internal::IsMap<Container>::value,
+                                  typename Container::value_type,
+                                  typename Container::key_type>::type>&>()());
+
+}  // namespace hash_internal
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASH_GENERATOR_TESTING_H_
diff --git a/absl/container/internal/hash_policy_testing.h b/absl/container/internal/hash_policy_testing.h
new file mode 100644
index 0000000..ffc76ea
--- /dev/null
+++ b/absl/container/internal/hash_policy_testing.h
@@ -0,0 +1,178 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Utilities to help tests verify that hash tables properly handle stateful
+// allocators and hash functions.
+
+#ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
+#define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
+
+#include <cstdlib>
+#include <limits>
+#include <memory>
+#include <ostream>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "absl/hash/hash.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+namespace hash_testing_internal {
+
+template <class Derived>
+struct WithId {
+  WithId() : id_(next_id<Derived>()) {}
+  WithId(const WithId& that) : id_(that.id_) {}
+  WithId(WithId&& that) : id_(that.id_) { that.id_ = 0; }
+  WithId& operator=(const WithId& that) {
+    id_ = that.id_;
+    return *this;
+  }
+  WithId& operator=(WithId&& that) {
+    id_ = that.id_;
+    that.id_ = 0;
+    return *this;
+  }
+
+  size_t id() const { return id_; }
+
+  friend bool operator==(const WithId& a, const WithId& b) {
+    return a.id_ == b.id_;
+  }
+  friend bool operator!=(const WithId& a, const WithId& b) { return !(a == b); }
+
+ protected:
+  explicit WithId(size_t id) : id_(id) {}
+
+ private:
+  size_t id_;
+
+  template <class T>
+  static size_t next_id() {
+    // 0 is reserved for moved from state.
+    static size_t gId = 1;
+    return gId++;
+  }
+};
+
+}  // namespace hash_testing_internal
+
+struct NonStandardLayout {
+  NonStandardLayout() {}
+  explicit NonStandardLayout(std::string s) : value(std::move(s)) {}
+  virtual ~NonStandardLayout() {}
+
+  friend bool operator==(const NonStandardLayout& a,
+                         const NonStandardLayout& b) {
+    return a.value == b.value;
+  }
+  friend bool operator!=(const NonStandardLayout& a,
+                         const NonStandardLayout& b) {
+    return a.value != b.value;
+  }
+
+  template <typename H>
+  friend H AbslHashValue(H h, const NonStandardLayout& v) {
+    return H::combine(std::move(h), v.value);
+  }
+
+  std::string value;
+};
+
+struct StatefulTestingHash
+    : absl::container_internal::hash_testing_internal::WithId<
+          StatefulTestingHash> {
+  template <class T>
+  size_t operator()(const T& t) const {
+    return absl::Hash<T>{}(t);
+  }
+};
+
+struct StatefulTestingEqual
+    : absl::container_internal::hash_testing_internal::WithId<
+          StatefulTestingEqual> {
+  template <class T, class U>
+  bool operator()(const T& t, const U& u) const {
+    return t == u;
+  }
+};
+
+// It is expected that Alloc() == Alloc() for all allocators so we cannot use
+// WithId base. We need to explicitly assign ids.
+template <class T = int>
+struct Alloc : std::allocator<T> {
+  using propagate_on_container_swap = std::true_type;
+
+  // Using old paradigm for this to ensure compatibility.
+  explicit Alloc(size_t id = 0) : id_(id) {}
+
+  Alloc(const Alloc&) = default;
+  Alloc& operator=(const Alloc&) = default;
+
+  template <class U>
+  Alloc(const Alloc<U>& that) : std::allocator<T>(that), id_(that.id()) {}
+
+  template <class U>
+  struct rebind {
+    using other = Alloc<U>;
+  };
+
+  size_t id() const { return id_; }
+
+  friend bool operator==(const Alloc& a, const Alloc& b) {
+    return a.id_ == b.id_;
+  }
+  friend bool operator!=(const Alloc& a, const Alloc& b) { return !(a == b); }
+
+ private:
+  size_t id_ = std::numeric_limits<size_t>::max();
+};
+
+template <class Map>
+auto items(const Map& m) -> std::vector<
+    std::pair<typename Map::key_type, typename Map::mapped_type>> {
+  using std::get;
+  std::vector<std::pair<typename Map::key_type, typename Map::mapped_type>> res;
+  res.reserve(m.size());
+  for (const auto& v : m) res.emplace_back(get<0>(v), get<1>(v));
+  return res;
+}
+
+template <class Set>
+auto keys(const Set& s)
+    -> std::vector<typename std::decay<typename Set::key_type>::type> {
+  std::vector<typename std::decay<typename Set::key_type>::type> res;
+  res.reserve(s.size());
+  for (const auto& v : s) res.emplace_back(v);
+  return res;
+}
+
+}  // namespace container_internal
+}  // namespace absl
+
+// ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS is false for glibcxx versions
+// where the unordered containers are missing certain constructors that
+// take allocator arguments. This test is defined ad-hoc for the platforms
+// we care about (notably Crosstool 17) because libstdcxx's useless
+// versioning scheme precludes a more principled solution.
+#if defined(__GLIBCXX__) && __GLIBCXX__ <= 20140425
+#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 0
+#else
+#define ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS 1
+#endif
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TESTING_H_
diff --git a/absl/container/internal/hash_policy_testing_test.cc b/absl/container/internal/hash_policy_testing_test.cc
new file mode 100644
index 0000000..c215c42
--- /dev/null
+++ b/absl/container/internal/hash_policy_testing_test.cc
@@ -0,0 +1,43 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/hash_policy_testing.h"
+
+#include "gtest/gtest.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+TEST(_, Hash) {
+  StatefulTestingHash h1;
+  EXPECT_EQ(1, h1.id());
+  StatefulTestingHash h2;
+  EXPECT_EQ(2, h2.id());
+  StatefulTestingHash h1c(h1);
+  EXPECT_EQ(1, h1c.id());
+  StatefulTestingHash h2m(std::move(h2));
+  EXPECT_EQ(2, h2m.id());
+  EXPECT_EQ(0, h2.id());
+  StatefulTestingHash h3;
+  EXPECT_EQ(3, h3.id());
+  h3 = StatefulTestingHash();
+  EXPECT_EQ(4, h3.id());
+  h3 = std::move(h1);
+  EXPECT_EQ(1, h3.id());
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h
new file mode 100644
index 0000000..029e47e
--- /dev/null
+++ b/absl/container/internal/hash_policy_traits.h
@@ -0,0 +1,189 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
+#define ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
+
+#include <cstddef>
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+#include "absl/meta/type_traits.h"
+
+namespace absl {
+namespace container_internal {
+
+// Defines how slots are initialized/destroyed/moved.
+template <class Policy, class = void>
+struct hash_policy_traits {
+ private:
+  struct ReturnKey {
+    // We return `Key` here.
+    // When Key=T&, we forward the lvalue reference.
+    // When Key=T, we return by value to avoid a dangling reference.
+    // eg, for string_hash_map.
+    template <class Key, class... Args>
+    Key operator()(Key&& k, const Args&...) const {
+      return std::forward<Key>(k);
+    }
+  };
+
+  template <class P = Policy, class = void>
+  struct ConstantIteratorsImpl : std::false_type {};
+
+  template <class P>
+  struct ConstantIteratorsImpl<P, absl::void_t<typename P::constant_iterators>>
+      : P::constant_iterators {};
+
+ public:
+  // The actual object stored in the hash table.
+  using slot_type = typename Policy::slot_type;
+
+  // The type of the keys stored in the hashtable.
+  using key_type = typename Policy::key_type;
+
+  // The argument type for insertions into the hashtable. This is different
+  // from value_type for increased performance. See initializer_list constructor
+  // and insert() member functions for more details.
+  using init_type = typename Policy::init_type;
+
+  using reference = decltype(Policy::element(std::declval<slot_type*>()));
+  using pointer = typename std::remove_reference<reference>::type*;
+  using value_type = typename std::remove_reference<reference>::type;
+
+  // Policies can set this variable to tell raw_hash_set that all iterators
+  // should be constant, even `iterator`. This is useful for set-like
+  // containers.
+  // Defaults to false if not provided by the policy.
+  using constant_iterators = ConstantIteratorsImpl<>;
+
+  // PRECONDITION: `slot` is UNINITIALIZED
+  // POSTCONDITION: `slot` is INITIALIZED
+  template <class Alloc, class... Args>
+  static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
+    Policy::construct(alloc, slot, std::forward<Args>(args)...);
+  }
+
+  // PRECONDITION: `slot` is INITIALIZED
+  // POSTCONDITION: `slot` is UNINITIALIZED
+  template <class Alloc>
+  static void destroy(Alloc* alloc, slot_type* slot) {
+    Policy::destroy(alloc, slot);
+  }
+
+  // Transfers the `old_slot` to `new_slot`. Any memory allocated by the
+  // allocator inside `old_slot` to `new_slot` can be transfered.
+  //
+  // OPTIONAL: defaults to:
+  //
+  //     clone(new_slot, std::move(*old_slot));
+  //     destroy(old_slot);
+  //
+  // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
+  // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
+  //                UNINITIALIZED
+  template <class Alloc>
+  static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
+    transfer_impl(alloc, new_slot, old_slot, 0);
+  }
+
+  // PRECONDITION: `slot` is INITIALIZED
+  // POSTCONDITION: `slot` is INITIALIZED
+  template <class P = Policy>
+  static auto element(slot_type* slot) -> decltype(P::element(slot)) {
+    return P::element(slot);
+  }
+
+  // Returns the amount of memory owned by `slot`, exclusive of `sizeof(*slot)`.
+  //
+  // If `slot` is nullptr, returns the constant amount of memory owned by any
+  // full slot or -1 if slots own variable amounts of memory.
+  //
+  // PRECONDITION: `slot` is INITIALIZED or nullptr
+  template <class P = Policy>
+  static size_t space_used(const slot_type* slot) {
+    return P::space_used(slot);
+  }
+
+  // Provides generalized access to the key for elements, both for elements in
+  // the table and for elements that have not yet been inserted (or even
+  // constructed).  We would like an API that allows us to say: `key(args...)`
+  // but we cannot do that for all cases, so we use this more general API that
+  // can be used for many things, including the following:
+  //
+  //   - Given an element in a table, get its key.
+  //   - Given an element initializer, get its key.
+  //   - Given `emplace()` arguments, get the element key.
+  //
+  // Implementations of this must adhere to a very strict technical
+  // specification around aliasing and consuming arguments:
+  //
+  // Let `value_type` be the result type of `element()` without ref- and
+  // cv-qualifiers. The first argument is a functor, the rest are constructor
+  // arguments for `value_type`. Returns `std::forward<F>(f)(k, xs...)`, where
+  // `k` is the element key, and `xs...` are the new constructor arguments for
+  // `value_type`. It's allowed for `k` to alias `xs...`, and for both to alias
+  // `ts...`. The key won't be touched once `xs...` are used to construct an
+  // element; `ts...` won't be touched at all, which allows `apply()` to consume
+  // any rvalues among them.
+  //
+  // If `value_type` is constructible from `Ts&&...`, `Policy::apply()` must not
+  // trigger a hard compile error unless it originates from `f`. In other words,
+  // `Policy::apply()` must be SFINAE-friendly. If `value_type` is not
+  // constructible from `Ts&&...`, either SFINAE or a hard compile error is OK.
+  //
+  // If `Ts...` is `[cv] value_type[&]` or `[cv] init_type[&]`,
+  // `Policy::apply()` must work. A compile error is not allowed, SFINAE or not.
+  template <class F, class... Ts, class P = Policy>
+  static auto apply(F&& f, Ts&&... ts)
+      -> decltype(P::apply(std::forward<F>(f), std::forward<Ts>(ts)...)) {
+    return P::apply(std::forward<F>(f), std::forward<Ts>(ts)...);
+  }
+
+  // Returns the "key" portion of the slot.
+  // Used for node handle manipulation.
+  template <class P = Policy>
+  static auto key(slot_type* slot)
+      -> decltype(P::apply(ReturnKey(), element(slot))) {
+    return P::apply(ReturnKey(), element(slot));
+  }
+
+  // Returns the "value" (as opposed to the "key") portion of the element. Used
+  // by maps to implement `operator[]`, `at()` and `insert_or_assign()`.
+  template <class T, class P = Policy>
+  static auto value(T* elem) -> decltype(P::value(elem)) {
+    return P::value(elem);
+  }
+
+ private:
+  // Use auto -> decltype as an enabler.
+  template <class Alloc, class P = Policy>
+  static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
+                            slot_type* old_slot, int)
+      -> decltype((void)P::transfer(alloc, new_slot, old_slot)) {
+    P::transfer(alloc, new_slot, old_slot);
+  }
+  template <class Alloc>
+  static void transfer_impl(Alloc* alloc, slot_type* new_slot,
+                            slot_type* old_slot, char) {
+    construct(alloc, new_slot, std::move(element(old_slot)));
+    destroy(alloc, old_slot);
+  }
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASH_POLICY_TRAITS_H_
diff --git a/absl/container/internal/hash_policy_traits_test.cc b/absl/container/internal/hash_policy_traits_test.cc
new file mode 100644
index 0000000..423f154
--- /dev/null
+++ b/absl/container/internal/hash_policy_traits_test.cc
@@ -0,0 +1,142 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/hash_policy_traits.h"
+
+#include <functional>
+#include <memory>
+#include <new>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::testing::MockFunction;
+using ::testing::Return;
+using ::testing::ReturnRef;
+
+using Alloc = std::allocator<int>;
+using Slot = int;
+
+struct PolicyWithoutOptionalOps {
+  using slot_type = Slot;
+  using key_type = Slot;
+  using init_type = Slot;
+
+  static std::function<void(void*, Slot*, Slot)> construct;
+  static std::function<void(void*, Slot*)> destroy;
+
+  static std::function<Slot&(Slot*)> element;
+  static int apply(int v) { return apply_impl(v); }
+  static std::function<int(int)> apply_impl;
+  static std::function<Slot&(Slot*)> value;
+};
+
+std::function<void(void*, Slot*, Slot)> PolicyWithoutOptionalOps::construct;
+std::function<void(void*, Slot*)> PolicyWithoutOptionalOps::destroy;
+
+std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::element;
+std::function<int(int)> PolicyWithoutOptionalOps::apply_impl;
+std::function<Slot&(Slot*)> PolicyWithoutOptionalOps::value;
+
+struct PolicyWithOptionalOps : PolicyWithoutOptionalOps {
+  static std::function<void(void*, Slot*, Slot*)> transfer;
+};
+
+std::function<void(void*, Slot*, Slot*)> PolicyWithOptionalOps::transfer;
+
+struct Test : ::testing::Test {
+  Test() {
+    PolicyWithoutOptionalOps::construct = [&](void* a1, Slot* a2, Slot a3) {
+      construct.Call(a1, a2, std::move(a3));
+    };
+    PolicyWithoutOptionalOps::destroy = [&](void* a1, Slot* a2) {
+      destroy.Call(a1, a2);
+    };
+
+    PolicyWithoutOptionalOps::element = [&](Slot* a1) -> Slot& {
+      return element.Call(a1);
+    };
+    PolicyWithoutOptionalOps::apply_impl = [&](int a1) -> int {
+      return apply.Call(a1);
+    };
+    PolicyWithoutOptionalOps::value = [&](Slot* a1) -> Slot& {
+      return value.Call(a1);
+    };
+
+    PolicyWithOptionalOps::transfer = [&](void* a1, Slot* a2, Slot* a3) {
+      return transfer.Call(a1, a2, a3);
+    };
+  }
+
+  std::allocator<int> alloc;
+  int a = 53;
+
+  MockFunction<void(void*, Slot*, Slot)> construct;
+  MockFunction<void(void*, Slot*)> destroy;
+
+  MockFunction<Slot&(Slot*)> element;
+  MockFunction<int(int)> apply;
+  MockFunction<Slot&(Slot*)> value;
+
+  MockFunction<void(void*, Slot*, Slot*)> transfer;
+};
+
+TEST_F(Test, construct) {
+  EXPECT_CALL(construct, Call(&alloc, &a, 53));
+  hash_policy_traits<PolicyWithoutOptionalOps>::construct(&alloc, &a, 53);
+}
+
+TEST_F(Test, destroy) {
+  EXPECT_CALL(destroy, Call(&alloc, &a));
+  hash_policy_traits<PolicyWithoutOptionalOps>::destroy(&alloc, &a);
+}
+
+TEST_F(Test, element) {
+  int b = 0;
+  EXPECT_CALL(element, Call(&a)).WillOnce(ReturnRef(b));
+  EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::element(&a));
+}
+
+TEST_F(Test, apply) {
+  EXPECT_CALL(apply, Call(42)).WillOnce(Return(1337));
+  EXPECT_EQ(1337, (hash_policy_traits<PolicyWithoutOptionalOps>::apply(42)));
+}
+
+TEST_F(Test, value) {
+  int b = 0;
+  EXPECT_CALL(value, Call(&a)).WillOnce(ReturnRef(b));
+  EXPECT_EQ(&b, &hash_policy_traits<PolicyWithoutOptionalOps>::value(&a));
+}
+
+TEST_F(Test, without_transfer) {
+  int b = 42;
+  EXPECT_CALL(element, Call(&b)).WillOnce(::testing::ReturnRef(b));
+  EXPECT_CALL(construct, Call(&alloc, &a, b));
+  EXPECT_CALL(destroy, Call(&alloc, &b));
+  hash_policy_traits<PolicyWithoutOptionalOps>::transfer(&alloc, &a, &b);
+}
+
+TEST_F(Test, with_transfer) {
+  int b = 42;
+  EXPECT_CALL(transfer, Call(&alloc, &a, &b));
+  hash_policy_traits<PolicyWithOptionalOps>::transfer(&alloc, &a, &b);
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/hashtable_debug.h b/absl/container/internal/hashtable_debug.h
new file mode 100644
index 0000000..c3bd65c
--- /dev/null
+++ b/absl/container/internal/hashtable_debug.h
@@ -0,0 +1,108 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// This library provides APIs to debug the probing behavior of hash tables.
+//
+// In general, the probing behavior is a black box for users and only the
+// side effects can be measured in the form of performance differences.
+// These APIs give a glimpse on the actual behavior of the probing algorithms in
+// these hashtables given a specified hash function and a set of elements.
+//
+// The probe count distribution can be used to assess the quality of the hash
+// function for that particular hash table. Note that a hash function that
+// performs well in one hash table implementation does not necessarily performs
+// well in a different one.
+//
+// This library supports std::unordered_{set,map}, dense_hash_{set,map} and
+// absl::{flat,node,string}_hash_{set,map}.
+
+#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
+#define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
+
+#include <cstddef>
+#include <algorithm>
+#include <type_traits>
+#include <vector>
+
+#include "absl/container/internal/hashtable_debug_hooks.h"
+
+namespace absl {
+namespace container_internal {
+
+// Returns the number of probes required to lookup `key`.  Returns 0 for a
+// search with no collisions.  Higher values mean more hash collisions occurred;
+// however, the exact meaning of this number varies according to the container
+// type.
+template <typename C>
+size_t GetHashtableDebugNumProbes(
+    const C& c, const typename C::key_type& key) {
+  return absl::container_internal::hashtable_debug_internal::
+      HashtableDebugAccess<C>::GetNumProbes(c, key);
+}
+
+// Gets a histogram of the number of probes for each elements in the container.
+// The sum of all the values in the vector is equal to container.size().
+template <typename C>
+std::vector<size_t> GetHashtableDebugNumProbesHistogram(const C& container) {
+  std::vector<size_t> v;
+  for (auto it = container.begin(); it != container.end(); ++it) {
+    size_t num_probes = GetHashtableDebugNumProbes(
+        container,
+        absl::container_internal::hashtable_debug_internal::GetKey<C>(*it, 0));
+    v.resize(std::max(v.size(), num_probes + 1));
+    v[num_probes]++;
+  }
+  return v;
+}
+
+struct HashtableDebugProbeSummary {
+  size_t total_elements;
+  size_t total_num_probes;
+  double mean;
+};
+
+// Gets a summary of the probe count distribution for the elements in the
+// container.
+template <typename C>
+HashtableDebugProbeSummary GetHashtableDebugProbeSummary(const C& container) {
+  auto probes = GetHashtableDebugNumProbesHistogram(container);
+  HashtableDebugProbeSummary summary = {};
+  for (size_t i = 0; i < probes.size(); ++i) {
+    summary.total_elements += probes[i];
+    summary.total_num_probes += probes[i] * i;
+  }
+  summary.mean = 1.0 * summary.total_num_probes / summary.total_elements;
+  return summary;
+}
+
+// Returns the number of bytes requested from the allocator by the container
+// and not freed.
+template <typename C>
+size_t AllocatedByteSize(const C& c) {
+  return absl::container_internal::hashtable_debug_internal::
+      HashtableDebugAccess<C>::AllocatedByteSize(c);
+}
+
+// Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type `C`
+// and `c.size()` is equal to `num_elements`.
+template <typename C>
+size_t LowerBoundAllocatedByteSize(size_t num_elements) {
+  return absl::container_internal::hashtable_debug_internal::
+      HashtableDebugAccess<C>::LowerBoundAllocatedByteSize(num_elements);
+}
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_H_
diff --git a/absl/container/internal/hashtable_debug_hooks.h b/absl/container/internal/hashtable_debug_hooks.h
new file mode 100644
index 0000000..8f21972
--- /dev/null
+++ b/absl/container/internal/hashtable_debug_hooks.h
@@ -0,0 +1,81 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Provides the internal API for hashtable_debug.h.
+
+#ifndef ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
+#define ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
+
+#include <cstddef>
+
+#include <algorithm>
+#include <type_traits>
+#include <vector>
+
+namespace absl {
+namespace container_internal {
+namespace hashtable_debug_internal {
+
+// If it is a map, call get<0>().
+using std::get;
+template <typename T, typename = typename T::mapped_type>
+auto GetKey(const typename T::value_type& pair, int) -> decltype(get<0>(pair)) {
+  return get<0>(pair);
+}
+
+// If it is not a map, return the value directly.
+template <typename T>
+const typename T::key_type& GetKey(const typename T::key_type& key, char) {
+  return key;
+}
+
+// Containers should specialize this to provide debug information for that
+// container.
+template <class Container, typename Enabler = void>
+struct HashtableDebugAccess {
+  // Returns the number of probes required to find `key` in `c`.  The "number of
+  // probes" is a concept that can vary by container.  Implementations should
+  // return 0 when `key` was found in the minimum number of operations and
+  // should increment the result for each non-trivial operation required to find
+  // `key`.
+  //
+  // The default implementation uses the bucket api from the standard and thus
+  // works for `std::unordered_*` containers.
+  static size_t GetNumProbes(const Container& c,
+                             const typename Container::key_type& key) {
+    if (!c.bucket_count()) return {};
+    size_t num_probes = 0;
+    size_t bucket = c.bucket(key);
+    for (auto it = c.begin(bucket), e = c.end(bucket);; ++it, ++num_probes) {
+      if (it == e) return num_probes;
+      if (c.key_eq()(key, GetKey<Container>(*it, 0))) return num_probes;
+    }
+  }
+
+  // Returns the number of bytes requested from the allocator by the container
+  // and not freed.
+  //
+  // static size_t AllocatedByteSize(const Container& c);
+
+  // Returns a tight lower bound for AllocatedByteSize(c) where `c` is of type
+  // `Container` and `c.size()` is equal to `num_elements`.
+  //
+  // static size_t LowerBoundAllocatedByteSize(size_t num_elements);
+};
+
+}  // namespace hashtable_debug_internal
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_HASHTABLE_DEBUG_HOOKS_H_
diff --git a/absl/container/internal/layout.h b/absl/container/internal/layout.h
new file mode 100644
index 0000000..676c7d6
--- /dev/null
+++ b/absl/container/internal/layout.h
@@ -0,0 +1,732 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//                           MOTIVATION AND TUTORIAL
+//
+// If you want to put in a single heap allocation N doubles followed by M ints,
+// it's easy if N and M are known at compile time.
+//
+//   struct S {
+//     double a[N];
+//     int b[M];
+//   };
+//
+//   S* p = new S;
+//
+// But what if N and M are known only in run time? Class template Layout to the
+// rescue! It's a portable generalization of the technique known as struct hack.
+//
+//   // This object will tell us everything we need to know about the memory
+//   // layout of double[N] followed by int[M]. It's structurally identical to
+//   // size_t[2] that stores N and M. It's very cheap to create.
+//   const Layout<double, int> layout(N, M);
+//
+//   // Allocate enough memory for both arrays. `AllocSize()` tells us how much
+//   // memory is needed. We are free to use any allocation function we want as
+//   // long as it returns aligned memory.
+//   std::unique_ptr<unsigned char[]> p(new unsigned char[layout.AllocSize()]);
+//
+//   // Obtain the pointer to the array of doubles.
+//   // Equivalent to `reinterpret_cast<double*>(p.get())`.
+//   //
+//   // We could have written layout.Pointer<0>(p) instead. If all the types are
+//   // unique you can use either form, but if some types are repeated you must
+//   // use the index form.
+//   double* a = layout.Pointer<double>(p.get());
+//
+//   // Obtain the pointer to the array of ints.
+//   // Equivalent to `reinterpret_cast<int*>(p.get() + N * 8)`.
+//   int* b = layout.Pointer<int>(p);
+//
+// If we are unable to specify sizes of all fields, we can pass as many sizes as
+// we can to `Partial()`. In return, it'll allow us to access the fields whose
+// locations and sizes can be computed from the provided information.
+// `Partial()` comes in handy when the array sizes are embedded into the
+// allocation.
+//
+//   // size_t[1] containing N, size_t[1] containing M, double[N], int[M].
+//   using L = Layout<size_t, size_t, double, int>;
+//
+//   unsigned char* Allocate(size_t n, size_t m) {
+//     const L layout(1, 1, n, m);
+//     unsigned char* p = new unsigned char[layout.AllocSize()];
+//     *layout.Pointer<0>(p) = n;
+//     *layout.Pointer<1>(p) = m;
+//     return p;
+//   }
+//
+//   void Use(unsigned char* p) {
+//     // First, extract N and M.
+//     // Specify that the first array has only one element. Using `prefix` we
+//     // can access the first two arrays but not more.
+//     constexpr auto prefix = L::Partial(1);
+//     size_t n = *prefix.Pointer<0>(p);
+//     size_t m = *prefix.Pointer<1>(p);
+//
+//     // Now we can get pointers to the payload.
+//     const L layout(1, 1, n, m);
+//     double* a = layout.Pointer<double>(p);
+//     int* b = layout.Pointer<int>(p);
+//   }
+//
+// The layout we used above combines fixed-size with dynamically-sized fields.
+// This is quite common. Layout is optimized for this use case and generates
+// optimal code. All computations that can be performed at compile time are
+// indeed performed at compile time.
+//
+// Efficiency tip: The order of fields matters. In `Layout<T1, ..., TN>` try to
+// ensure that `alignof(T1) >= ... >= alignof(TN)`. This way you'll have no
+// padding in between arrays.
+//
+// You can manually override the alignment of an array by wrapping the type in
+// `Aligned<T, N>`. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
+// and behavior as `Layout<..., T, ...>` except that the first element of the
+// array of `T` is aligned to `N` (the rest of the elements follow without
+// padding). `N` cannot be less than `alignof(T)`.
+//
+// `AllocSize()` and `Pointer()` are the most basic methods for dealing with
+// memory layouts. Check out the reference or code below to discover more.
+//
+//                            EXAMPLE
+//
+//   // Immutable move-only string with sizeof equal to sizeof(void*). The
+//   // string size and the characters are kept in the same heap allocation.
+//   class CompactString {
+//    public:
+//     CompactString(const char* s = "") {
+//       const size_t size = strlen(s);
+//       // size_t[1] followed by char[size + 1].
+//       const L layout(1, size + 1);
+//       p_.reset(new unsigned char[layout.AllocSize()]);
+//       // If running under ASAN, mark the padding bytes, if any, to catch
+//       // memory errors.
+//       layout.PoisonPadding(p_.get());
+//       // Store the size in the allocation.
+//       *layout.Pointer<size_t>(p_.get()) = size;
+//       // Store the characters in the allocation.
+//       memcpy(layout.Pointer<char>(p_.get()), s, size + 1);
+//     }
+//
+//     size_t size() const {
+//       // Equivalent to reinterpret_cast<size_t&>(*p).
+//       return *L::Partial().Pointer<size_t>(p_.get());
+//     }
+//
+//     const char* c_str() const {
+//       // Equivalent to reinterpret_cast<char*>(p.get() + sizeof(size_t)).
+//       // The argument in Partial(1) specifies that we have size_t[1] in front
+//       // of the characters.
+//       return L::Partial(1).Pointer<char>(p_.get());
+//     }
+//
+//    private:
+//     // Our heap allocation contains a size_t followed by an array of chars.
+//     using L = Layout<size_t, char>;
+//     std::unique_ptr<unsigned char[]> p_;
+//   };
+//
+//   int main() {
+//     CompactString s = "hello";
+//     assert(s.size() == 5);
+//     assert(strcmp(s.c_str(), "hello") == 0);
+//   }
+//
+//                               DOCUMENTATION
+//
+// The interface exported by this file consists of:
+// - class `Layout<>` and its public members.
+// - The public members of class `internal_layout::LayoutImpl<>`. That class
+//   isn't intended to be used directly, and its name and template parameter
+//   list are internal implementation details, but the class itself provides
+//   most of the functionality in this file. See comments on its members for
+//   detailed documentation.
+//
+// `Layout<T1,... Tn>::Partial(count1,..., countm)` (where `m` <= `n`) returns a
+// `LayoutImpl<>` object. `Layout<T1,..., Tn> layout(count1,..., countn)`
+// creates a `Layout` object, which exposes the same functionality by inheriting
+// from `LayoutImpl<>`.
+
+#ifndef ABSL_CONTAINER_INTERNAL_LAYOUT_H_
+#define ABSL_CONTAINER_INTERNAL_LAYOUT_H_
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <ostream>
+#include <string>
+#include <tuple>
+#include <type_traits>
+#include <typeinfo>
+#include <utility>
+
+#ifdef ADDRESS_SANITIZER
+#include <sanitizer/asan_interface.h>
+#endif
+
+#include "absl/meta/type_traits.h"
+#include "absl/strings/str_cat.h"
+#include "absl/types/span.h"
+#include "absl/utility/utility.h"
+
+#if defined(__GXX_RTTI)
+#define ABSL_INTERNAL_HAS_CXA_DEMANGLE
+#endif
+
+#ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE
+#include <cxxabi.h>
+#endif
+
+namespace absl {
+namespace container_internal {
+
+// A type wrapper that instructs `Layout` to use the specific alignment for the
+// array. `Layout<..., Aligned<T, N>, ...>` has exactly the same API
+// and behavior as `Layout<..., T, ...>` except that the first element of the
+// array of `T` is aligned to `N` (the rest of the elements follow without
+// padding).
+//
+// Requires: `N >= alignof(T)` and `N` is a power of 2.
+template <class T, size_t N>
+struct Aligned;
+
+namespace internal_layout {
+
+template <class T>
+struct NotAligned {};
+
+template <class T, size_t N>
+struct NotAligned<const Aligned<T, N>> {
+  static_assert(sizeof(T) == 0, "Aligned<T, N> cannot be const-qualified");
+};
+
+template <size_t>
+using IntToSize = size_t;
+
+template <class>
+using TypeToSize = size_t;
+
+template <class T>
+struct Type : NotAligned<T> {
+  using type = T;
+};
+
+template <class T, size_t N>
+struct Type<Aligned<T, N>> {
+  using type = T;
+};
+
+template <class T>
+struct SizeOf : NotAligned<T>, std::integral_constant<size_t, sizeof(T)> {};
+
+template <class T, size_t N>
+struct SizeOf<Aligned<T, N>> : std::integral_constant<size_t, sizeof(T)> {};
+
+template <class T>
+struct AlignOf : NotAligned<T>, std::integral_constant<size_t, alignof(T)> {};
+
+template <class T, size_t N>
+struct AlignOf<Aligned<T, N>> : std::integral_constant<size_t, N> {
+  static_assert(N % alignof(T) == 0,
+                "Custom alignment can't be lower than the type's alignment");
+};
+
+// Does `Ts...` contain `T`?
+template <class T, class... Ts>
+using Contains = absl::disjunction<std::is_same<T, Ts>...>;
+
+template <class From, class To>
+using CopyConst =
+    typename std::conditional<std::is_const<From>::value, const To, To>::type;
+
+template <class T>
+using SliceType = absl::Span<T>;
+
+// This namespace contains no types. It prevents functions defined in it from
+// being found by ADL.
+namespace adl_barrier {
+
+template <class Needle, class... Ts>
+constexpr size_t Find(Needle, Needle, Ts...) {
+  static_assert(!Contains<Needle, Ts...>(), "Duplicate element type");
+  return 0;
+}
+
+template <class Needle, class T, class... Ts>
+constexpr size_t Find(Needle, T, Ts...) {
+  return adl_barrier::Find(Needle(), Ts()...) + 1;
+}
+
+constexpr bool IsPow2(size_t n) { return !(n & (n - 1)); }
+
+// Returns `q * m` for the smallest `q` such that `q * m >= n`.
+// Requires: `m` is a power of two. It's enforced by IsLegalElementType below.
+constexpr size_t Align(size_t n, size_t m) { return (n + m - 1) & ~(m - 1); }
+
+constexpr size_t Min(size_t a, size_t b) { return b < a ? b : a; }
+
+constexpr size_t Max(size_t a) { return a; }
+
+template <class... Ts>
+constexpr size_t Max(size_t a, size_t b, Ts... rest) {
+  return adl_barrier::Max(b < a ? a : b, rest...);
+}
+
+template <class T>
+std::string TypeName() {
+  std::string out;
+  int status = 0;
+  char* demangled = nullptr;
+#ifdef ABSL_INTERNAL_HAS_CXA_DEMANGLE
+  demangled = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
+#endif
+  if (status == 0 && demangled != nullptr) {  // Demangling succeeeded.
+    absl::StrAppend(&out, "<", demangled, ">");
+    free(demangled);
+  } else {
+#if defined(__GXX_RTTI) || defined(_CPPRTTI)
+    absl::StrAppend(&out, "<", typeid(T).name(), ">");
+#endif
+  }
+  return out;
+}
+
+}  // namespace adl_barrier
+
+template <bool C>
+using EnableIf = typename std::enable_if<C, int>::type;
+
+// Can `T` be a template argument of `Layout`?
+template <class T>
+using IsLegalElementType = std::integral_constant<
+    bool, !std::is_reference<T>::value && !std::is_volatile<T>::value &&
+              !std::is_reference<typename Type<T>::type>::value &&
+              !std::is_volatile<typename Type<T>::type>::value &&
+              adl_barrier::IsPow2(AlignOf<T>::value)>;
+
+template <class Elements, class SizeSeq, class OffsetSeq>
+class LayoutImpl;
+
+// Public base class of `Layout` and the result type of `Layout::Partial()`.
+//
+// `Elements...` contains all template arguments of `Layout` that created this
+// instance.
+//
+// `SizeSeq...` is `[0, NumSizes)` where `NumSizes` is the number of arguments
+// passed to `Layout::Partial()` or `Layout::Layout()`.
+//
+// `OffsetSeq...` is `[0, NumOffsets)` where `NumOffsets` is
+// `Min(sizeof...(Elements), NumSizes + 1)` (the number of arrays for which we
+// can compute offsets).
+template <class... Elements, size_t... SizeSeq, size_t... OffsetSeq>
+class LayoutImpl<std::tuple<Elements...>, absl::index_sequence<SizeSeq...>,
+                 absl::index_sequence<OffsetSeq...>> {
+ private:
+  static_assert(sizeof...(Elements) > 0, "At least one field is required");
+  static_assert(absl::conjunction<IsLegalElementType<Elements>...>::value,
+                "Invalid element type (see IsLegalElementType)");
+
+  enum {
+    NumTypes = sizeof...(Elements),
+    NumSizes = sizeof...(SizeSeq),
+    NumOffsets = sizeof...(OffsetSeq),
+  };
+
+  // These are guaranteed by `Layout`.
+  static_assert(NumOffsets == adl_barrier::Min(NumTypes, NumSizes + 1),
+                "Internal error");
+  static_assert(NumTypes > 0, "Internal error");
+
+  // Returns the index of `T` in `Elements...`. Results in a compilation error
+  // if `Elements...` doesn't contain exactly one instance of `T`.
+  template <class T>
+  static constexpr size_t ElementIndex() {
+    static_assert(Contains<Type<T>, Type<typename Type<Elements>::type>...>(),
+                  "Type not found");
+    return adl_barrier::Find(Type<T>(),
+                             Type<typename Type<Elements>::type>()...);
+  }
+
+  template <size_t N>
+  using ElementAlignment =
+      AlignOf<typename std::tuple_element<N, std::tuple<Elements...>>::type>;
+
+ public:
+  // Element types of all arrays packed in a tuple.
+  using ElementTypes = std::tuple<typename Type<Elements>::type...>;
+
+  // Element type of the Nth array.
+  template <size_t N>
+  using ElementType = typename std::tuple_element<N, ElementTypes>::type;
+
+  constexpr explicit LayoutImpl(IntToSize<SizeSeq>... sizes)
+      : size_{sizes...} {}
+
+  // Alignment of the layout, equal to the strictest alignment of all elements.
+  // All pointers passed to the methods of layout must be aligned to this value.
+  static constexpr size_t Alignment() {
+    return adl_barrier::Max(AlignOf<Elements>::value...);
+  }
+
+  // Offset in bytes of the Nth array.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   assert(x.Offset<0>() == 0);   // The ints starts from 0.
+  //   assert(x.Offset<1>() == 16);  // The doubles starts from 16.
+  //
+  // Requires: `N <= NumSizes && N < sizeof...(Ts)`.
+  template <size_t N, EnableIf<N == 0> = 0>
+  constexpr size_t Offset() const {
+    return 0;
+  }
+
+  template <size_t N, EnableIf<N != 0> = 0>
+  constexpr size_t Offset() const {
+    static_assert(N < NumOffsets, "Index out of bounds");
+    return adl_barrier::Align(
+        Offset<N - 1>() + SizeOf<ElementType<N - 1>>() * size_[N - 1],
+        ElementAlignment<N>());
+  }
+
+  // Offset in bytes of the array with the specified element type. There must
+  // be exactly one such array and its zero-based index must be at most
+  // `NumSizes`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   assert(x.Offset<int>() == 0);      // The ints starts from 0.
+  //   assert(x.Offset<double>() == 16);  // The doubles starts from 16.
+  template <class T>
+  constexpr size_t Offset() const {
+    return Offset<ElementIndex<T>()>();
+  }
+
+  // Offsets in bytes of all arrays for which the offsets are known.
+  constexpr std::array<size_t, NumOffsets> Offsets() const {
+    return {{Offset<OffsetSeq>()...}};
+  }
+
+  // The number of elements in the Nth array. This is the Nth argument of
+  // `Layout::Partial()` or `Layout::Layout()` (zero-based).
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   assert(x.Size<0>() == 3);
+  //   assert(x.Size<1>() == 4);
+  //
+  // Requires: `N < NumSizes`.
+  template <size_t N>
+  constexpr size_t Size() const {
+    static_assert(N < NumSizes, "Index out of bounds");
+    return size_[N];
+  }
+
+  // The number of elements in the array with the specified element type.
+  // There must be exactly one such array and its zero-based index must be
+  // at most `NumSizes`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   assert(x.Size<int>() == 3);
+  //   assert(x.Size<double>() == 4);
+  template <class T>
+  constexpr size_t Size() const {
+    return Size<ElementIndex<T>()>();
+  }
+
+    // The number of elements of all arrays for which they are known.
+  constexpr std::array<size_t, NumSizes> Sizes() const {
+    return {{Size<SizeSeq>()...}};
+  }
+
+  // Pointer to the beginning of the Nth array.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //   int* ints = x.Pointer<0>(p);
+  //   double* doubles = x.Pointer<1>(p);
+  //
+  // Requires: `N <= NumSizes && N < sizeof...(Ts)`.
+  // Requires: `p` is aligned to `Alignment()`.
+  template <size_t N, class Char>
+  CopyConst<Char, ElementType<N>>* Pointer(Char* p) const {
+    using C = typename std::remove_const<Char>::type;
+    static_assert(
+        std::is_same<C, char>() || std::is_same<C, unsigned char>() ||
+            std::is_same<C, signed char>(),
+        "The argument must be a pointer to [const] [signed|unsigned] char");
+    constexpr size_t alignment = Alignment();
+    (void)alignment;
+    assert(reinterpret_cast<uintptr_t>(p) % alignment == 0);
+    return reinterpret_cast<CopyConst<Char, ElementType<N>>*>(p + Offset<N>());
+  }
+
+  // Pointer to the beginning of the array with the specified element type.
+  // There must be exactly one such array and its zero-based index must be at
+  // most `NumSizes`.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //   int* ints = x.Pointer<int>(p);
+  //   double* doubles = x.Pointer<double>(p);
+  //
+  // Requires: `p` is aligned to `Alignment()`.
+  template <class T, class Char>
+  CopyConst<Char, T>* Pointer(Char* p) const {
+    return Pointer<ElementIndex<T>()>(p);
+  }
+
+  // Pointers to all arrays for which pointers are known.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //
+  //   int* ints;
+  //   double* doubles;
+  //   std::tie(ints, doubles) = x.Pointers(p);
+  //
+  // Requires: `p` is aligned to `Alignment()`.
+  //
+  // Note: We're not using ElementType alias here because it does not compile
+  // under MSVC.
+  template <class Char>
+  std::tuple<CopyConst<
+      Char, typename std::tuple_element<OffsetSeq, ElementTypes>::type>*...>
+  Pointers(Char* p) const {
+    return std::tuple<CopyConst<Char, ElementType<OffsetSeq>>*...>(
+        Pointer<OffsetSeq>(p)...);
+  }
+
+  // The Nth array.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //   Span<int> ints = x.Slice<0>(p);
+  //   Span<double> doubles = x.Slice<1>(p);
+  //
+  // Requires: `N < NumSizes`.
+  // Requires: `p` is aligned to `Alignment()`.
+  template <size_t N, class Char>
+  SliceType<CopyConst<Char, ElementType<N>>> Slice(Char* p) const {
+    return SliceType<CopyConst<Char, ElementType<N>>>(Pointer<N>(p), Size<N>());
+  }
+
+  // The array with the specified element type. There must be exactly one
+  // such array and its zero-based index must be less than `NumSizes`.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //   Span<int> ints = x.Slice<int>(p);
+  //   Span<double> doubles = x.Slice<double>(p);
+  //
+  // Requires: `p` is aligned to `Alignment()`.
+  template <class T, class Char>
+  SliceType<CopyConst<Char, T>> Slice(Char* p) const {
+    return Slice<ElementIndex<T>()>(p);
+  }
+
+  // All arrays with known sizes.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];
+  //
+  //   Span<int> ints;
+  //   Span<double> doubles;
+  //   std::tie(ints, doubles) = x.Slices(p);
+  //
+  // Requires: `p` is aligned to `Alignment()`.
+  //
+  // Note: We're not using ElementType alias here because it does not compile
+  // under MSVC.
+  template <class Char>
+  std::tuple<SliceType<CopyConst<
+      Char, typename std::tuple_element<SizeSeq, ElementTypes>::type>>...>
+  Slices(Char* p) const {
+    // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63875 (fixed
+    // in 6.1).
+    (void)p;
+    return std::tuple<SliceType<CopyConst<Char, ElementType<SizeSeq>>>...>(
+        Slice<SizeSeq>(p)...);
+  }
+
+  // The size of the allocation that fits all arrays.
+  //
+  //   // int[3], 4 bytes of padding, double[4].
+  //   Layout<int, double> x(3, 4);
+  //   unsigned char* p = new unsigned char[x.AllocSize()];  // 48 bytes
+  //
+  // Requires: `NumSizes == sizeof...(Ts)`.
+  constexpr size_t AllocSize() const {
+    static_assert(NumTypes == NumSizes, "You must specify sizes of all fields");
+    return Offset<NumTypes - 1>() +
+           SizeOf<ElementType<NumTypes - 1>>() * size_[NumTypes - 1];
+  }
+
+  // If built with --config=asan, poisons padding bytes (if any) in the
+  // allocation. The pointer must point to a memory block at least
+  // `AllocSize()` bytes in length.
+  //
+  // `Char` must be `[const] [signed|unsigned] char`.
+  //
+  // Requires: `p` is aligned to `Alignment()`.
+  template <class Char, size_t N = NumOffsets - 1, EnableIf<N == 0> = 0>
+  void PoisonPadding(const Char* p) const {
+    Pointer<0>(p);  // verify the requirements on `Char` and `p`
+  }
+
+  template <class Char, size_t N = NumOffsets - 1, EnableIf<N != 0> = 0>
+  void PoisonPadding(const Char* p) const {
+    static_assert(N < NumOffsets, "Index out of bounds");
+    (void)p;
+#ifdef ADDRESS_SANITIZER
+    PoisonPadding<Char, N - 1>(p);
+    // The `if` is an optimization. It doesn't affect the observable behaviour.
+    if (ElementAlignment<N - 1>() % ElementAlignment<N>()) {
+      size_t start =
+          Offset<N - 1>() + SizeOf<ElementType<N - 1>>() * size_[N - 1];
+      ASAN_POISON_MEMORY_REGION(p + start, Offset<N>() - start);
+    }
+#endif
+  }
+
+  // Human-readable description of the memory layout. Useful for debugging.
+  // Slow.
+  //
+  //   // char[5], 3 bytes of padding, int[3], 4 bytes of padding, followed
+  //   // by an unknown number of doubles.
+  //   auto x = Layout<char, int, double>::Partial(5, 3);
+  //   assert(x.DebugString() ==
+  //          "@0<char>(1)[5]; @8<int>(4)[3]; @24<double>(8)");
+  //
+  // Each field is in the following format: @offset<type>(sizeof)[size] (<type>
+  // may be missing depending on the target platform). For example,
+  // @8<int>(4)[3] means that at offset 8 we have an array of ints, where each
+  // int is 4 bytes, and we have 3 of those ints. The size of the last field may
+  // be missing (as in the example above). Only fields with known offsets are
+  // described. Type names may differ across platforms: one compiler might
+  // produce "unsigned*" where another produces "unsigned int *".
+  std::string DebugString() const {
+    const auto offsets = Offsets();
+    const size_t sizes[] = {SizeOf<ElementType<OffsetSeq>>()...};
+    const std::string types[] = {adl_barrier::TypeName<ElementType<OffsetSeq>>()...};
+    std::string res = absl::StrCat("@0", types[0], "(", sizes[0], ")");
+    for (size_t i = 0; i != NumOffsets - 1; ++i) {
+      absl::StrAppend(&res, "[", size_[i], "]; @", offsets[i + 1], types[i + 1],
+                      "(", sizes[i + 1], ")");
+    }
+    // NumSizes is a constant that may be zero. Some compilers cannot see that
+    // inside the if statement "size_[NumSizes - 1]" must be valid.
+    int last = static_cast<int>(NumSizes) - 1;
+    if (NumTypes == NumSizes && last >= 0) {
+      absl::StrAppend(&res, "[", size_[last], "]");
+    }
+    return res;
+  }
+
+ private:
+  // Arguments of `Layout::Partial()` or `Layout::Layout()`.
+  size_t size_[NumSizes > 0 ? NumSizes : 1];
+};
+
+template <size_t NumSizes, class... Ts>
+using LayoutType = LayoutImpl<
+    std::tuple<Ts...>, absl::make_index_sequence<NumSizes>,
+    absl::make_index_sequence<adl_barrier::Min(sizeof...(Ts), NumSizes + 1)>>;
+
+}  // namespace internal_layout
+
+// Descriptor of arrays of various types and sizes laid out in memory one after
+// another. See the top of the file for documentation.
+//
+// Check out the public API of internal_layout::LayoutImpl above. The type is
+// internal to the library but its methods are public, and they are inherited
+// by `Layout`.
+template <class... Ts>
+class Layout : public internal_layout::LayoutType<sizeof...(Ts), Ts...> {
+ public:
+  static_assert(sizeof...(Ts) > 0, "At least one field is required");
+  static_assert(
+      absl::conjunction<internal_layout::IsLegalElementType<Ts>...>::value,
+      "Invalid element type (see IsLegalElementType)");
+
+  // The result type of `Partial()` with `NumSizes` arguments.
+  template <size_t NumSizes>
+  using PartialType = internal_layout::LayoutType<NumSizes, Ts...>;
+
+  // `Layout` knows the element types of the arrays we want to lay out in
+  // memory but not the number of elements in each array.
+  // `Partial(size1, ..., sizeN)` allows us to specify the latter. The
+  // resulting immutable object can be used to obtain pointers to the
+  // individual arrays.
+  //
+  // It's allowed to pass fewer array sizes than the number of arrays. E.g.,
+  // if all you need is to the offset of the second array, you only need to
+  // pass one argument -- the number of elements in the first arrays.
+  //
+  //   // int[3] followed by 4 bytes of padding and an unknown number of
+  //   // doubles.
+  //   auto x = Layout<int, double>::Partial(3);
+  //   // doubles start at byte 16.
+  //   assert(x.Offset<1>() == 16);
+  //
+  // If you know the number of elements in all arrays, you can still call
+  // `Partial()` but it's more convenient to use the constructor of `Layout`.
+  //
+  //   Layout<int, double> x(3, 5);
+  //
+  // Note: The sizes of the arrays must be specified in number of elements,
+  // not in bytes.
+  //
+  // Requires: `sizeof...(Sizes) <= sizeof...(Ts)`.
+  // Requires: all arguments are convertible to `size_t`.
+  template <class... Sizes>
+  static constexpr PartialType<sizeof...(Sizes)> Partial(Sizes&&... sizes) {
+    static_assert(sizeof...(Sizes) <= sizeof...(Ts), "");
+    return PartialType<sizeof...(Sizes)>(absl::forward<Sizes>(sizes)...);
+  }
+
+  // Creates a layout with the sizes of all arrays specified. If you know
+  // only the sizes of the first N arrays (where N can be zero), you can use
+  // `Partial()` defined above. The constructor is essentially equivalent to
+  // calling `Partial()` and passing in all array sizes; the constructor is
+  // provided as a convenient abbreviation.
+  //
+  // Note: The sizes of the arrays must be specified in number of elements,
+  // not in bytes.
+  constexpr explicit Layout(internal_layout::TypeToSize<Ts>... sizes)
+      : internal_layout::LayoutType<sizeof...(Ts), Ts...>(sizes...) {}
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_LAYOUT_H_
diff --git a/absl/container/internal/layout_test.cc b/absl/container/internal/layout_test.cc
new file mode 100644
index 0000000..f35157a
--- /dev/null
+++ b/absl/container/internal/layout_test.cc
@@ -0,0 +1,1552 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/layout.h"
+
+// We need ::max_align_t because some libstdc++ versions don't provide
+// std::max_align_t
+#include <stddef.h>
+#include <cstdint>
+#include <memory>
+#include <sstream>
+#include <type_traits>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/base/internal/raw_logging.h"
+#include "absl/types/span.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::absl::Span;
+using ::testing::ElementsAre;
+
+size_t Distance(const void* from, const void* to) {
+  ABSL_RAW_CHECK(from <= to, "Distance must be non-negative");
+  return static_cast<const char*>(to) - static_cast<const char*>(from);
+}
+
+template <class Expected, class Actual>
+Expected Type(Actual val) {
+  static_assert(std::is_same<Expected, Actual>(), "");
+  return val;
+}
+
+using Int128 = int64_t[2];
+
+// Properties of types that this test relies on.
+static_assert(sizeof(int8_t) == 1, "");
+static_assert(alignof(int8_t) == 1, "");
+static_assert(sizeof(int16_t) == 2, "");
+static_assert(alignof(int16_t) == 2, "");
+static_assert(sizeof(int32_t) == 4, "");
+static_assert(alignof(int32_t) == 4, "");
+static_assert(sizeof(Int128) == 16, "");
+static_assert(alignof(Int128) == 8, "");
+
+template <class Expected, class Actual>
+void SameType() {
+  static_assert(std::is_same<Expected, Actual>(), "");
+}
+
+TEST(Layout, ElementType) {
+  {
+    using L = Layout<int32_t>;
+    SameType<int32_t, L::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial())::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial(0))::ElementType<0>>();
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    SameType<int32_t, L::ElementType<0>>();
+    SameType<int32_t, L::ElementType<1>>();
+    SameType<int32_t, decltype(L::Partial())::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial())::ElementType<1>>();
+    SameType<int32_t, decltype(L::Partial(0))::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial(0))::ElementType<1>>();
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    SameType<int8_t, L::ElementType<0>>();
+    SameType<int32_t, L::ElementType<1>>();
+    SameType<Int128, L::ElementType<2>>();
+    SameType<int8_t, decltype(L::Partial())::ElementType<0>>();
+    SameType<int8_t, decltype(L::Partial(0))::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial(0))::ElementType<1>>();
+    SameType<int8_t, decltype(L::Partial(0, 0))::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial(0, 0))::ElementType<1>>();
+    SameType<Int128, decltype(L::Partial(0, 0))::ElementType<2>>();
+    SameType<int8_t, decltype(L::Partial(0, 0, 0))::ElementType<0>>();
+    SameType<int32_t, decltype(L::Partial(0, 0, 0))::ElementType<1>>();
+    SameType<Int128, decltype(L::Partial(0, 0, 0))::ElementType<2>>();
+  }
+}
+
+TEST(Layout, ElementTypes) {
+  {
+    using L = Layout<int32_t>;
+    SameType<std::tuple<int32_t>, L::ElementTypes>();
+    SameType<std::tuple<int32_t>, decltype(L::Partial())::ElementTypes>();
+    SameType<std::tuple<int32_t>, decltype(L::Partial(0))::ElementTypes>();
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    SameType<std::tuple<int32_t, int32_t>, L::ElementTypes>();
+    SameType<std::tuple<int32_t, int32_t>, decltype(L::Partial())::ElementTypes>();
+    SameType<std::tuple<int32_t, int32_t>, decltype(L::Partial(0))::ElementTypes>();
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    SameType<std::tuple<int8_t, int32_t, Int128>, L::ElementTypes>();
+    SameType<std::tuple<int8_t, int32_t, Int128>,
+             decltype(L::Partial())::ElementTypes>();
+    SameType<std::tuple<int8_t, int32_t, Int128>,
+             decltype(L::Partial(0))::ElementTypes>();
+    SameType<std::tuple<int8_t, int32_t, Int128>,
+             decltype(L::Partial(0, 0))::ElementTypes>();
+    SameType<std::tuple<int8_t, int32_t, Int128>,
+             decltype(L::Partial(0, 0, 0))::ElementTypes>();
+  }
+}
+
+TEST(Layout, OffsetByIndex) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial().Offset<0>());
+    EXPECT_EQ(0, L::Partial(3).Offset<0>());
+    EXPECT_EQ(0, L(3).Offset<0>());
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(0, L::Partial().Offset<0>());
+    EXPECT_EQ(0, L::Partial(3).Offset<0>());
+    EXPECT_EQ(12, L::Partial(3).Offset<1>());
+    EXPECT_EQ(0, L::Partial(3, 5).Offset<0>());
+    EXPECT_EQ(12, L::Partial(3, 5).Offset<1>());
+    EXPECT_EQ(0, L(3, 5).Offset<0>());
+    EXPECT_EQ(12, L(3, 5).Offset<1>());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, L::Partial().Offset<0>());
+    EXPECT_EQ(0, L::Partial(0).Offset<0>());
+    EXPECT_EQ(0, L::Partial(0).Offset<1>());
+    EXPECT_EQ(0, L::Partial(1).Offset<0>());
+    EXPECT_EQ(4, L::Partial(1).Offset<1>());
+    EXPECT_EQ(0, L::Partial(5).Offset<0>());
+    EXPECT_EQ(8, L::Partial(5).Offset<1>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<0>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<1>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<2>());
+    EXPECT_EQ(0, L::Partial(1, 0).Offset<0>());
+    EXPECT_EQ(4, L::Partial(1, 0).Offset<1>());
+    EXPECT_EQ(8, L::Partial(1, 0).Offset<2>());
+    EXPECT_EQ(0, L::Partial(5, 3).Offset<0>());
+    EXPECT_EQ(8, L::Partial(5, 3).Offset<1>());
+    EXPECT_EQ(24, L::Partial(5, 3).Offset<2>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<0>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<1>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<2>());
+    EXPECT_EQ(0, L::Partial(1, 0, 0).Offset<0>());
+    EXPECT_EQ(4, L::Partial(1, 0, 0).Offset<1>());
+    EXPECT_EQ(8, L::Partial(1, 0, 0).Offset<2>());
+    EXPECT_EQ(0, L::Partial(5, 3, 1).Offset<0>());
+    EXPECT_EQ(24, L::Partial(5, 3, 1).Offset<2>());
+    EXPECT_EQ(8, L::Partial(5, 3, 1).Offset<1>());
+    EXPECT_EQ(0, L(5, 3, 1).Offset<0>());
+    EXPECT_EQ(24, L(5, 3, 1).Offset<2>());
+    EXPECT_EQ(8, L(5, 3, 1).Offset<1>());
+  }
+}
+
+TEST(Layout, OffsetByType) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial().Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(3).Offset<int32_t>());
+    EXPECT_EQ(0, L(3).Offset<int32_t>());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, L::Partial().Offset<int8_t>());
+    EXPECT_EQ(0, L::Partial(0).Offset<int8_t>());
+    EXPECT_EQ(0, L::Partial(0).Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(1).Offset<int8_t>());
+    EXPECT_EQ(4, L::Partial(1).Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(5).Offset<int8_t>());
+    EXPECT_EQ(8, L::Partial(5).Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<int8_t>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(0, 0).Offset<Int128>());
+    EXPECT_EQ(0, L::Partial(1, 0).Offset<int8_t>());
+    EXPECT_EQ(4, L::Partial(1, 0).Offset<int32_t>());
+    EXPECT_EQ(8, L::Partial(1, 0).Offset<Int128>());
+    EXPECT_EQ(0, L::Partial(5, 3).Offset<int8_t>());
+    EXPECT_EQ(8, L::Partial(5, 3).Offset<int32_t>());
+    EXPECT_EQ(24, L::Partial(5, 3).Offset<Int128>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<int8_t>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<int32_t>());
+    EXPECT_EQ(0, L::Partial(0, 0, 0).Offset<Int128>());
+    EXPECT_EQ(0, L::Partial(1, 0, 0).Offset<int8_t>());
+    EXPECT_EQ(4, L::Partial(1, 0, 0).Offset<int32_t>());
+    EXPECT_EQ(8, L::Partial(1, 0, 0).Offset<Int128>());
+    EXPECT_EQ(0, L::Partial(5, 3, 1).Offset<int8_t>());
+    EXPECT_EQ(24, L::Partial(5, 3, 1).Offset<Int128>());
+    EXPECT_EQ(8, L::Partial(5, 3, 1).Offset<int32_t>());
+    EXPECT_EQ(0, L(5, 3, 1).Offset<int8_t>());
+    EXPECT_EQ(24, L(5, 3, 1).Offset<Int128>());
+    EXPECT_EQ(8, L(5, 3, 1).Offset<int32_t>());
+  }
+}
+
+TEST(Layout, Offsets) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0));
+    EXPECT_THAT(L::Partial(3).Offsets(), ElementsAre(0));
+    EXPECT_THAT(L(3).Offsets(), ElementsAre(0));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0));
+    EXPECT_THAT(L::Partial(3).Offsets(), ElementsAre(0, 12));
+    EXPECT_THAT(L::Partial(3, 5).Offsets(), ElementsAre(0, 12));
+    EXPECT_THAT(L(3, 5).Offsets(), ElementsAre(0, 12));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_THAT(L::Partial().Offsets(), ElementsAre(0));
+    EXPECT_THAT(L::Partial(1).Offsets(), ElementsAre(0, 4));
+    EXPECT_THAT(L::Partial(5).Offsets(), ElementsAre(0, 8));
+    EXPECT_THAT(L::Partial(0, 0).Offsets(), ElementsAre(0, 0, 0));
+    EXPECT_THAT(L::Partial(1, 0).Offsets(), ElementsAre(0, 4, 8));
+    EXPECT_THAT(L::Partial(5, 3).Offsets(), ElementsAre(0, 8, 24));
+    EXPECT_THAT(L::Partial(0, 0, 0).Offsets(), ElementsAre(0, 0, 0));
+    EXPECT_THAT(L::Partial(1, 0, 0).Offsets(), ElementsAre(0, 4, 8));
+    EXPECT_THAT(L::Partial(5, 3, 1).Offsets(), ElementsAre(0, 8, 24));
+    EXPECT_THAT(L(5, 3, 1).Offsets(), ElementsAre(0, 8, 24));
+  }
+}
+
+TEST(Layout, AllocSize) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).AllocSize());
+    EXPECT_EQ(12, L::Partial(3).AllocSize());
+    EXPECT_EQ(12, L(3).AllocSize());
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(32, L::Partial(3, 5).AllocSize());
+    EXPECT_EQ(32, L(3, 5).AllocSize());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, L::Partial(0, 0, 0).AllocSize());
+    EXPECT_EQ(8, L::Partial(1, 0, 0).AllocSize());
+    EXPECT_EQ(8, L::Partial(0, 1, 0).AllocSize());
+    EXPECT_EQ(16, L::Partial(0, 0, 1).AllocSize());
+    EXPECT_EQ(24, L::Partial(1, 1, 1).AllocSize());
+    EXPECT_EQ(136, L::Partial(3, 5, 7).AllocSize());
+    EXPECT_EQ(136, L(3, 5, 7).AllocSize());
+  }
+}
+
+TEST(Layout, SizeByIndex) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Size<0>());
+    EXPECT_EQ(3, L::Partial(3).Size<0>());
+    EXPECT_EQ(3, L(3).Size<0>());
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Size<0>());
+    EXPECT_EQ(3, L::Partial(3).Size<0>());
+    EXPECT_EQ(3, L::Partial(3, 5).Size<0>());
+    EXPECT_EQ(5, L::Partial(3, 5).Size<1>());
+    EXPECT_EQ(3, L(3, 5).Size<0>());
+    EXPECT_EQ(5, L(3, 5).Size<1>());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Size<0>());
+    EXPECT_EQ(3, L::Partial(3, 5).Size<0>());
+    EXPECT_EQ(5, L::Partial(3, 5).Size<1>());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Size<0>());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Size<1>());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Size<2>());
+    EXPECT_EQ(3, L(3, 5, 7).Size<0>());
+    EXPECT_EQ(5, L(3, 5, 7).Size<1>());
+    EXPECT_EQ(7, L(3, 5, 7).Size<2>());
+  }
+}
+
+TEST(Layout, SizeByType) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Size<int32_t>());
+    EXPECT_EQ(3, L::Partial(3).Size<int32_t>());
+    EXPECT_EQ(3, L(3).Size<int32_t>());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Size<int8_t>());
+    EXPECT_EQ(3, L::Partial(3, 5).Size<int8_t>());
+    EXPECT_EQ(5, L::Partial(3, 5).Size<int32_t>());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Size<int8_t>());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Size<int32_t>());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Size<Int128>());
+    EXPECT_EQ(3, L(3, 5, 7).Size<int8_t>());
+    EXPECT_EQ(5, L(3, 5, 7).Size<int32_t>());
+    EXPECT_EQ(7, L(3, 5, 7).Size<Int128>());
+  }
+}
+
+TEST(Layout, Sizes) {
+  {
+    using L = Layout<int32_t>;
+    EXPECT_THAT(L::Partial().Sizes(), ElementsAre());
+    EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3));
+    EXPECT_THAT(L(3).Sizes(), ElementsAre(3));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_THAT(L::Partial().Sizes(), ElementsAre());
+    EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3));
+    EXPECT_THAT(L::Partial(3, 5).Sizes(), ElementsAre(3, 5));
+    EXPECT_THAT(L(3, 5).Sizes(), ElementsAre(3, 5));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_THAT(L::Partial().Sizes(), ElementsAre());
+    EXPECT_THAT(L::Partial(3).Sizes(), ElementsAre(3));
+    EXPECT_THAT(L::Partial(3, 5).Sizes(), ElementsAre(3, 5));
+    EXPECT_THAT(L::Partial(3, 5, 7).Sizes(), ElementsAre(3, 5, 7));
+    EXPECT_THAT(L(3, 5, 7).Sizes(), ElementsAre(3, 5, 7));
+  }
+}
+
+TEST(Layout, PointerByIndex) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L::Partial(3).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L(3).Pointer<0>(p))));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L::Partial(3).Pointer<0>(p))));
+    EXPECT_EQ(12, Distance(p, Type<const int32_t*>(L::Partial(3).Pointer<1>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int32_t*>(L::Partial(3, 5).Pointer<0>(p))));
+    EXPECT_EQ(12,
+              Distance(p, Type<const int32_t*>(L::Partial(3, 5).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L(3, 5).Pointer<0>(p))));
+    EXPECT_EQ(12, Distance(p, Type<const int32_t*>(L(3, 5).Pointer<1>(p))));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L::Partial(0).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L::Partial(0).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L::Partial(1).Pointer<0>(p))));
+    EXPECT_EQ(4, Distance(p, Type<const int32_t*>(L::Partial(1).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L::Partial(5).Pointer<0>(p))));
+    EXPECT_EQ(8, Distance(p, Type<const int32_t*>(L::Partial(5).Pointer<1>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(0, 0).Pointer<0>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int32_t*>(L::Partial(0, 0).Pointer<1>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const Int128*>(L::Partial(0, 0).Pointer<2>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(1, 0).Pointer<0>(p))));
+    EXPECT_EQ(4,
+              Distance(p, Type<const int32_t*>(L::Partial(1, 0).Pointer<1>(p))));
+    EXPECT_EQ(8,
+              Distance(p, Type<const Int128*>(L::Partial(1, 0).Pointer<2>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(5, 3).Pointer<0>(p))));
+    EXPECT_EQ(8,
+              Distance(p, Type<const int32_t*>(L::Partial(5, 3).Pointer<1>(p))));
+    EXPECT_EQ(24,
+              Distance(p, Type<const Int128*>(L::Partial(5, 3).Pointer<2>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(0, 0, 0).Pointer<0>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int32_t*>(L::Partial(0, 0, 0).Pointer<1>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const Int128*>(L::Partial(0, 0, 0).Pointer<2>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(1, 0, 0).Pointer<0>(p))));
+    EXPECT_EQ(
+        4, Distance(p, Type<const int32_t*>(L::Partial(1, 0, 0).Pointer<1>(p))));
+    EXPECT_EQ(
+        8, Distance(p, Type<const Int128*>(L::Partial(1, 0, 0).Pointer<2>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(5, 3, 1).Pointer<0>(p))));
+    EXPECT_EQ(
+        24,
+        Distance(p, Type<const Int128*>(L::Partial(5, 3, 1).Pointer<2>(p))));
+    EXPECT_EQ(
+        8, Distance(p, Type<const int32_t*>(L::Partial(5, 3, 1).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L(5, 3, 1).Pointer<0>(p))));
+    EXPECT_EQ(24, Distance(p, Type<const Int128*>(L(5, 3, 1).Pointer<2>(p))));
+    EXPECT_EQ(8, Distance(p, Type<const int32_t*>(L(5, 3, 1).Pointer<1>(p))));
+  }
+}
+
+TEST(Layout, PointerByType) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0,
+              Distance(p, Type<const int32_t*>(L::Partial().Pointer<int32_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int32_t*>(L::Partial(3).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const int32_t*>(L(3).Pointer<int32_t>(p))));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, Distance(p, Type<const int8_t*>(L::Partial().Pointer<int8_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(0).Pointer<int8_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int32_t*>(L::Partial(0).Pointer<int32_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(1).Pointer<int8_t>(p))));
+    EXPECT_EQ(4,
+              Distance(p, Type<const int32_t*>(L::Partial(1).Pointer<int32_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<const int8_t*>(L::Partial(5).Pointer<int8_t>(p))));
+    EXPECT_EQ(8,
+              Distance(p, Type<const int32_t*>(L::Partial(5).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int32_t*>(L::Partial(0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<const Int128*>(L::Partial(0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(1, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        4, Distance(p, Type<const int32_t*>(L::Partial(1, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        8,
+        Distance(p, Type<const Int128*>(L::Partial(1, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<const int8_t*>(L::Partial(5, 3).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        8, Distance(p, Type<const int32_t*>(L::Partial(5, 3).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        24,
+        Distance(p, Type<const Int128*>(L::Partial(5, 3).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<const int8_t*>(L::Partial(0, 0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<const int32_t*>(L::Partial(0, 0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<const Int128*>(
+                                 L::Partial(0, 0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<const int8_t*>(L::Partial(1, 0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        4,
+        Distance(p, Type<const int32_t*>(L::Partial(1, 0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(8, Distance(p, Type<const Int128*>(
+                                 L::Partial(1, 0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<const int8_t*>(L::Partial(5, 3, 1).Pointer<int8_t>(p))));
+    EXPECT_EQ(24, Distance(p, Type<const Int128*>(
+                                  L::Partial(5, 3, 1).Pointer<Int128>(p))));
+    EXPECT_EQ(
+        8,
+        Distance(p, Type<const int32_t*>(L::Partial(5, 3, 1).Pointer<int32_t>(p))));
+    EXPECT_EQ(24,
+              Distance(p, Type<const Int128*>(L(5, 3, 1).Pointer<Int128>(p))));
+    EXPECT_EQ(8, Distance(p, Type<const int32_t*>(L(5, 3, 1).Pointer<int32_t>(p))));
+  }
+}
+
+TEST(Layout, MutablePointerByIndex) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(3).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L(3).Pointer<0>(p))));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(3).Pointer<0>(p))));
+    EXPECT_EQ(12, Distance(p, Type<int32_t*>(L::Partial(3).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(3, 5).Pointer<0>(p))));
+    EXPECT_EQ(12, Distance(p, Type<int32_t*>(L::Partial(3, 5).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L(3, 5).Pointer<0>(p))));
+    EXPECT_EQ(12, Distance(p, Type<int32_t*>(L(3, 5).Pointer<1>(p))));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial().Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(0).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(0).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(1).Pointer<0>(p))));
+    EXPECT_EQ(4, Distance(p, Type<int32_t*>(L::Partial(1).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(5).Pointer<0>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L::Partial(5).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(0, 0).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(0, 0).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<Int128*>(L::Partial(0, 0).Pointer<2>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(1, 0).Pointer<0>(p))));
+    EXPECT_EQ(4, Distance(p, Type<int32_t*>(L::Partial(1, 0).Pointer<1>(p))));
+    EXPECT_EQ(8, Distance(p, Type<Int128*>(L::Partial(1, 0).Pointer<2>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(5, 3).Pointer<0>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L::Partial(5, 3).Pointer<1>(p))));
+    EXPECT_EQ(24, Distance(p, Type<Int128*>(L::Partial(5, 3).Pointer<2>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(0, 0, 0).Pointer<0>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(0, 0, 0).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<Int128*>(L::Partial(0, 0, 0).Pointer<2>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(1, 0, 0).Pointer<0>(p))));
+    EXPECT_EQ(4, Distance(p, Type<int32_t*>(L::Partial(1, 0, 0).Pointer<1>(p))));
+    EXPECT_EQ(8, Distance(p, Type<Int128*>(L::Partial(1, 0, 0).Pointer<2>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(5, 3, 1).Pointer<0>(p))));
+    EXPECT_EQ(24,
+              Distance(p, Type<Int128*>(L::Partial(5, 3, 1).Pointer<2>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L::Partial(5, 3, 1).Pointer<1>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L(5, 3, 1).Pointer<0>(p))));
+    EXPECT_EQ(24, Distance(p, Type<Int128*>(L(5, 3, 1).Pointer<2>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L(5, 3, 1).Pointer<1>(p))));
+  }
+}
+
+TEST(Layout, MutablePointerByType) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial().Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(3).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L(3).Pointer<int32_t>(p))));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial().Pointer<int8_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(0).Pointer<int8_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(0).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(1).Pointer<int8_t>(p))));
+    EXPECT_EQ(4, Distance(p, Type<int32_t*>(L::Partial(1).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(5).Pointer<int8_t>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L::Partial(5).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int32_t*>(L::Partial(0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<Int128*>(L::Partial(0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(1, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(4, Distance(p, Type<int32_t*>(L::Partial(1, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(8,
+              Distance(p, Type<Int128*>(L::Partial(1, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L::Partial(5, 3).Pointer<int8_t>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L::Partial(5, 3).Pointer<int32_t>(p))));
+    EXPECT_EQ(24,
+              Distance(p, Type<Int128*>(L::Partial(5, 3).Pointer<Int128>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<int8_t*>(L::Partial(0, 0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<int32_t*>(L::Partial(0, 0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        0, Distance(p, Type<Int128*>(L::Partial(0, 0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<int8_t*>(L::Partial(1, 0, 0).Pointer<int8_t>(p))));
+    EXPECT_EQ(4,
+              Distance(p, Type<int32_t*>(L::Partial(1, 0, 0).Pointer<int32_t>(p))));
+    EXPECT_EQ(
+        8, Distance(p, Type<Int128*>(L::Partial(1, 0, 0).Pointer<Int128>(p))));
+    EXPECT_EQ(0,
+              Distance(p, Type<int8_t*>(L::Partial(5, 3, 1).Pointer<int8_t>(p))));
+    EXPECT_EQ(
+        24, Distance(p, Type<Int128*>(L::Partial(5, 3, 1).Pointer<Int128>(p))));
+    EXPECT_EQ(8,
+              Distance(p, Type<int32_t*>(L::Partial(5, 3, 1).Pointer<int32_t>(p))));
+    EXPECT_EQ(0, Distance(p, Type<int8_t*>(L(5, 3, 1).Pointer<int8_t>(p))));
+    EXPECT_EQ(24, Distance(p, Type<Int128*>(L(5, 3, 1).Pointer<Int128>(p))));
+    EXPECT_EQ(8, Distance(p, Type<int32_t*>(L(5, 3, 1).Pointer<int32_t>(p))));
+  }
+}
+
+TEST(Layout, Pointers) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  using L = Layout<int8_t, int8_t, Int128>;
+  {
+    const auto x = L::Partial();
+    EXPECT_EQ(std::make_tuple(x.Pointer<0>(p)),
+              Type<std::tuple<const int8_t*>>(x.Pointers(p)));
+  }
+  {
+    const auto x = L::Partial(1);
+    EXPECT_EQ(std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p)),
+              (Type<std::tuple<const int8_t*, const int8_t*>>(x.Pointers(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<const int8_t*, const int8_t*, const Int128*>>(
+            x.Pointers(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2, 3);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<const int8_t*, const int8_t*, const Int128*>>(
+            x.Pointers(p))));
+  }
+  {
+    const L x(1, 2, 3);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<const int8_t*, const int8_t*, const Int128*>>(
+            x.Pointers(p))));
+  }
+}
+
+TEST(Layout, MutablePointers) {
+  alignas(max_align_t) unsigned char p[100];
+  using L = Layout<int8_t, int8_t, Int128>;
+  {
+    const auto x = L::Partial();
+    EXPECT_EQ(std::make_tuple(x.Pointer<0>(p)),
+              Type<std::tuple<int8_t*>>(x.Pointers(p)));
+  }
+  {
+    const auto x = L::Partial(1);
+    EXPECT_EQ(std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p)),
+              (Type<std::tuple<int8_t*, int8_t*>>(x.Pointers(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<int8_t*, int8_t*, Int128*>>(x.Pointers(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2, 3);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<int8_t*, int8_t*, Int128*>>(x.Pointers(p))));
+  }
+  {
+    const L x(1, 2, 3);
+    EXPECT_EQ(
+        std::make_tuple(x.Pointer<0>(p), x.Pointer<1>(p), x.Pointer<2>(p)),
+        (Type<std::tuple<int8_t*, int8_t*, Int128*>>(x.Pointers(p))));
+  }
+}
+
+TEST(Layout, SliceByIndexSize) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Slice<0>(p).size());
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(3, L(3).Slice<0>(p).size());
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size());
+    EXPECT_EQ(5, L(3, 5).Slice<1>(p).size());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<1>(p).size());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<2>(p).size());
+    EXPECT_EQ(3, L(3, 5, 7).Slice<0>(p).size());
+    EXPECT_EQ(5, L(3, 5, 7).Slice<1>(p).size());
+    EXPECT_EQ(7, L(3, 5, 7).Slice<2>(p).size());
+  }
+}
+
+TEST(Layout, SliceByTypeSize) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L(3).Slice<int32_t>(p).size());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Slice<int8_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<int32_t>(p).size());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<Int128>(p).size());
+    EXPECT_EQ(3, L(3, 5, 7).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L(3, 5, 7).Slice<int32_t>(p).size());
+    EXPECT_EQ(7, L(3, 5, 7).Slice<Int128>(p).size());
+  }
+}
+
+TEST(Layout, MutableSliceByIndexSize) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Slice<0>(p).size());
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(3, L(3).Slice<0>(p).size());
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size());
+    EXPECT_EQ(5, L(3, 5).Slice<1>(p).size());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Slice<0>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<1>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<0>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<1>(p).size());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<2>(p).size());
+    EXPECT_EQ(3, L(3, 5, 7).Slice<0>(p).size());
+    EXPECT_EQ(5, L(3, 5, 7).Slice<1>(p).size());
+    EXPECT_EQ(7, L(3, 5, 7).Slice<2>(p).size());
+  }
+}
+
+TEST(Layout, MutableSliceByTypeSize) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0, L::Partial(0).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L(3).Slice<int32_t>(p).size());
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(3, L::Partial(3).Slice<int8_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5).Slice<int32_t>(p).size());
+    EXPECT_EQ(3, L::Partial(3, 5, 7).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L::Partial(3, 5, 7).Slice<int32_t>(p).size());
+    EXPECT_EQ(7, L::Partial(3, 5, 7).Slice<Int128>(p).size());
+    EXPECT_EQ(3, L(3, 5, 7).Slice<int8_t>(p).size());
+    EXPECT_EQ(5, L(3, 5, 7).Slice<int32_t>(p).size());
+    EXPECT_EQ(7, L(3, 5, 7).Slice<Int128>(p).size());
+  }
+}
+
+TEST(Layout, SliceByIndexData) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(3).Slice<0>(p)).data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<const int32_t>>(L(3).Slice<0>(p)).data()));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(3).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p,
+                 Type<Span<const int32_t>>(L::Partial(3, 5).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        12,
+        Distance(p,
+                 Type<Span<const int32_t>>(L::Partial(3, 5).Slice<1>(p)).data()));
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<const int32_t>>(L(3, 5).Slice<0>(p)).data()));
+    EXPECT_EQ(12,
+              Distance(p, Type<Span<const int32_t>>(L(3, 5).Slice<1>(p)).data()));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int8_t>>(L::Partial(0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int8_t>>(L::Partial(1).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int8_t>>(L::Partial(5).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p,
+                 Type<Span<const int32_t>>(L::Partial(0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(1, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(p,
+                 Type<Span<const int32_t>>(L::Partial(1, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(5, 3).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(p,
+                 Type<Span<const int32_t>>(L::Partial(5, 3).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(0, 0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(0, 0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const Int128>>(L::Partial(0, 0, 0).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(1, 0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(1, 0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(
+            p,
+            Type<Span<const Int128>>(L::Partial(1, 0, 0).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(5, 3, 1).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        24,
+        Distance(
+            p,
+            Type<Span<const Int128>>(L::Partial(5, 3, 1).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(5, 3, 1).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<const int8_t>>(L(5, 3, 1).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        24,
+        Distance(p, Type<Span<const Int128>>(L(5, 3, 1).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(p, Type<Span<const int32_t>>(L(5, 3, 1).Slice<1>(p)).data()));
+  }
+}
+
+TEST(Layout, SliceByTypeData) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int32_t>>(L::Partial(0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int32_t>>(L::Partial(3).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<const int32_t>>(L(3).Slice<int32_t>(p)).data()));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<const int8_t>>(L::Partial(5).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(0, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(1, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(1, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<const int8_t>>(L::Partial(5, 3).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(
+            p,
+            Type<Span<const int32_t>>(L::Partial(5, 3).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const int8_t>>(L::Partial(0, 0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(0, 0, 0).Slice<int32_t>(p))
+                        .data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<const Int128>>(
+                                 L::Partial(0, 0, 0).Slice<Int128>(p))
+                                 .data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const int8_t>>(L::Partial(1, 0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(1, 0, 0).Slice<int32_t>(p))
+                        .data()));
+    EXPECT_EQ(8, Distance(p, Type<Span<const Int128>>(
+                                 L::Partial(1, 0, 0).Slice<Int128>(p))
+                                 .data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<const int8_t>>(L::Partial(5, 3, 1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(24, Distance(p, Type<Span<const Int128>>(
+                                  L::Partial(5, 3, 1).Slice<Int128>(p))
+                                  .data()));
+    EXPECT_EQ(
+        8,
+        Distance(p, Type<Span<const int32_t>>(L::Partial(5, 3, 1).Slice<int32_t>(p))
+                        .data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<const int8_t>>(L(5, 3, 1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        24,
+        Distance(p,
+                 Type<Span<const Int128>>(L(5, 3, 1).Slice<Int128>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(
+               p, Type<Span<const int32_t>>(L(5, 3, 1).Slice<int32_t>(p)).data()));
+  }
+}
+
+TEST(Layout, MutableSliceByIndexData) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int32_t>>(L::Partial(0).Slice<0>(p)).data()));
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int32_t>>(L::Partial(3).Slice<0>(p)).data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<int32_t>>(L(3).Slice<0>(p)).data()));
+  }
+  {
+    using L = Layout<int32_t, int32_t>;
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int32_t>>(L::Partial(3).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int32_t>>(L::Partial(3, 5).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        12,
+        Distance(p, Type<Span<int32_t>>(L::Partial(3, 5).Slice<1>(p)).data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<int32_t>>(L(3, 5).Slice<0>(p)).data()));
+    EXPECT_EQ(12, Distance(p, Type<Span<int32_t>>(L(3, 5).Slice<1>(p)).data()));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int8_t>>(L::Partial(0).Slice<0>(p)).data()));
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int8_t>>(L::Partial(1).Slice<0>(p)).data()));
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int8_t>>(L::Partial(5).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int32_t>>(L::Partial(0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(1, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        4, Distance(p, Type<Span<int32_t>>(L::Partial(1, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(5, 3).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(p, Type<Span<int32_t>>(L::Partial(5, 3).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(0, 0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int32_t>>(L::Partial(0, 0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<Int128>>(L::Partial(0, 0, 0).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(1, 0, 0).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(p, Type<Span<int32_t>>(L::Partial(1, 0, 0).Slice<1>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(
+               p, Type<Span<Int128>>(L::Partial(1, 0, 0).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(5, 3, 1).Slice<0>(p)).data()));
+    EXPECT_EQ(
+        24, Distance(
+                p, Type<Span<Int128>>(L::Partial(5, 3, 1).Slice<2>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(p, Type<Span<int32_t>>(L::Partial(5, 3, 1).Slice<1>(p)).data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<int8_t>>(L(5, 3, 1).Slice<0>(p)).data()));
+    EXPECT_EQ(24,
+              Distance(p, Type<Span<Int128>>(L(5, 3, 1).Slice<2>(p)).data()));
+    EXPECT_EQ(8, Distance(p, Type<Span<int32_t>>(L(5, 3, 1).Slice<1>(p)).data()));
+  }
+}
+
+TEST(Layout, MutableSliceByTypeData) {
+  alignas(max_align_t) unsigned char p[100];
+  {
+    using L = Layout<int32_t>;
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int32_t>>(L::Partial(0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int32_t>>(L::Partial(3).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(0, Distance(p, Type<Span<int32_t>>(L(3).Slice<int32_t>(p)).data()));
+  }
+  {
+    using L = Layout<int8_t, int32_t, Int128>;
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(p, Type<Span<int8_t>>(L::Partial(5).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<int32_t>>(L::Partial(0, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(1, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        4, Distance(
+               p, Type<Span<int32_t>>(L::Partial(1, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(p, Type<Span<int8_t>>(L::Partial(5, 3).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(
+               p, Type<Span<int32_t>>(L::Partial(5, 3).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<int8_t>>(L::Partial(0, 0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p, Type<Span<int32_t>>(L::Partial(0, 0, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        0,
+        Distance(
+            p,
+            Type<Span<Int128>>(L::Partial(0, 0, 0).Slice<Int128>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<int8_t>>(L::Partial(1, 0, 0).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        4,
+        Distance(
+            p, Type<Span<int32_t>>(L::Partial(1, 0, 0).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(
+            p,
+            Type<Span<Int128>>(L::Partial(1, 0, 0).Slice<Int128>(p)).data()));
+    EXPECT_EQ(
+        0, Distance(
+               p, Type<Span<int8_t>>(L::Partial(5, 3, 1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        24,
+        Distance(
+            p,
+            Type<Span<Int128>>(L::Partial(5, 3, 1).Slice<Int128>(p)).data()));
+    EXPECT_EQ(
+        8,
+        Distance(
+            p, Type<Span<int32_t>>(L::Partial(5, 3, 1).Slice<int32_t>(p)).data()));
+    EXPECT_EQ(0,
+              Distance(p, Type<Span<int8_t>>(L(5, 3, 1).Slice<int8_t>(p)).data()));
+    EXPECT_EQ(
+        24,
+        Distance(p, Type<Span<Int128>>(L(5, 3, 1).Slice<Int128>(p)).data()));
+    EXPECT_EQ(
+        8, Distance(p, Type<Span<int32_t>>(L(5, 3, 1).Slice<int32_t>(p)).data()));
+  }
+}
+
+MATCHER_P(IsSameSlice, slice, "") {
+  return arg.size() == slice.size() && arg.data() == slice.data();
+}
+
+template <typename... M>
+class TupleMatcher {
+ public:
+  explicit TupleMatcher(M... matchers) : matchers_(std::move(matchers)...) {}
+
+  template <typename Tuple>
+  bool MatchAndExplain(const Tuple& p,
+                       testing::MatchResultListener* /* listener */) const {
+    static_assert(std::tuple_size<Tuple>::value == sizeof...(M), "");
+    return MatchAndExplainImpl(
+        p, absl::make_index_sequence<std::tuple_size<Tuple>::value>{});
+  }
+
+  // For the matcher concept. Left empty as we don't really need the diagnostics
+  // right now.
+  void DescribeTo(::std::ostream* os) const {}
+  void DescribeNegationTo(::std::ostream* os) const {}
+
+ private:
+  template <typename Tuple, size_t... Is>
+  bool MatchAndExplainImpl(const Tuple& p, absl::index_sequence<Is...>) const {
+    // Using std::min as a simple variadic "and".
+    return std::min(
+        {true, testing::SafeMatcherCast<
+                   const typename std::tuple_element<Is, Tuple>::type&>(
+                   std::get<Is>(matchers_))
+                   .Matches(std::get<Is>(p))...});
+  }
+
+  std::tuple<M...> matchers_;
+};
+
+template <typename... M>
+testing::PolymorphicMatcher<TupleMatcher<M...>> Tuple(M... matchers) {
+  return testing::MakePolymorphicMatcher(
+      TupleMatcher<M...>(std::move(matchers)...));
+}
+
+TEST(Layout, Slices) {
+  alignas(max_align_t) const unsigned char p[100] = {};
+  using L = Layout<int8_t, int8_t, Int128>;
+  {
+    const auto x = L::Partial();
+    EXPECT_THAT(Type<std::tuple<>>(x.Slices(p)), Tuple());
+  }
+  {
+    const auto x = L::Partial(1);
+    EXPECT_THAT(Type<std::tuple<Span<const int8_t>>>(x.Slices(p)),
+                Tuple(IsSameSlice(x.Slice<0>(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2);
+    EXPECT_THAT(
+        (Type<std::tuple<Span<const int8_t>, Span<const int8_t>>>(x.Slices(p))),
+        Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2, 3);
+    EXPECT_THAT((Type<std::tuple<Span<const int8_t>, Span<const int8_t>,
+                                 Span<const Int128>>>(x.Slices(p))),
+                Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)),
+                      IsSameSlice(x.Slice<2>(p))));
+  }
+  {
+    const L x(1, 2, 3);
+    EXPECT_THAT((Type<std::tuple<Span<const int8_t>, Span<const int8_t>,
+                                 Span<const Int128>>>(x.Slices(p))),
+                Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)),
+                      IsSameSlice(x.Slice<2>(p))));
+  }
+}
+
+TEST(Layout, MutableSlices) {
+  alignas(max_align_t) unsigned char p[100] = {};
+  using L = Layout<int8_t, int8_t, Int128>;
+  {
+    const auto x = L::Partial();
+    EXPECT_THAT(Type<std::tuple<>>(x.Slices(p)), Tuple());
+  }
+  {
+    const auto x = L::Partial(1);
+    EXPECT_THAT(Type<std::tuple<Span<int8_t>>>(x.Slices(p)),
+                Tuple(IsSameSlice(x.Slice<0>(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2);
+    EXPECT_THAT((Type<std::tuple<Span<int8_t>, Span<int8_t>>>(x.Slices(p))),
+                Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p))));
+  }
+  {
+    const auto x = L::Partial(1, 2, 3);
+    EXPECT_THAT(
+        (Type<std::tuple<Span<int8_t>, Span<int8_t>, Span<Int128>>>(x.Slices(p))),
+        Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)),
+              IsSameSlice(x.Slice<2>(p))));
+  }
+  {
+    const L x(1, 2, 3);
+    EXPECT_THAT(
+        (Type<std::tuple<Span<int8_t>, Span<int8_t>, Span<Int128>>>(x.Slices(p))),
+        Tuple(IsSameSlice(x.Slice<0>(p)), IsSameSlice(x.Slice<1>(p)),
+              IsSameSlice(x.Slice<2>(p))));
+  }
+}
+
+TEST(Layout, UnalignedTypes) {
+  constexpr Layout<unsigned char, unsigned char, unsigned char> x(1, 2, 3);
+  alignas(max_align_t) unsigned char p[x.AllocSize() + 1];
+  EXPECT_THAT(x.Pointers(p + 1), Tuple(p + 1, p + 2, p + 4));
+}
+
+TEST(Layout, CustomAlignment) {
+  constexpr Layout<unsigned char, Aligned<unsigned char, 8>> x(1, 2);
+  alignas(max_align_t) unsigned char p[x.AllocSize()];
+  EXPECT_EQ(10, x.AllocSize());
+  EXPECT_THAT(x.Pointers(p), Tuple(p + 0, p + 8));
+}
+
+TEST(Layout, OverAligned) {
+  constexpr size_t M = alignof(max_align_t);
+  constexpr Layout<unsigned char, Aligned<unsigned char, 2 * M>> x(1, 3);
+  alignas(2 * M) unsigned char p[x.AllocSize()];
+  EXPECT_EQ(2 * M + 3, x.AllocSize());
+  EXPECT_THAT(x.Pointers(p), Tuple(p + 0, p + 2 * M));
+}
+
+TEST(Layout, Alignment) {
+  static_assert(Layout<int8_t>::Alignment() == 1, "");
+  static_assert(Layout<int32_t>::Alignment() == 4, "");
+  static_assert(Layout<int64_t>::Alignment() == 8, "");
+  static_assert(Layout<Aligned<int8_t, 64>>::Alignment() == 64, "");
+  static_assert(Layout<int8_t, int32_t, int64_t>::Alignment() == 8, "");
+  static_assert(Layout<int8_t, int64_t, int32_t>::Alignment() == 8, "");
+  static_assert(Layout<int32_t, int8_t, int64_t>::Alignment() == 8, "");
+  static_assert(Layout<int32_t, int64_t, int8_t>::Alignment() == 8, "");
+  static_assert(Layout<int64_t, int8_t, int32_t>::Alignment() == 8, "");
+  static_assert(Layout<int64_t, int32_t, int8_t>::Alignment() == 8, "");
+}
+
+TEST(Layout, ConstexprPartial) {
+  constexpr size_t M = alignof(max_align_t);
+  constexpr Layout<unsigned char, Aligned<unsigned char, 2 * M>> x(1, 3);
+  static_assert(x.Partial(1).template Offset<1>() == 2 * M, "");
+}
+// [from, to)
+struct Region {
+  size_t from;
+  size_t to;
+};
+
+void ExpectRegionPoisoned(const unsigned char* p, size_t n, bool poisoned) {
+#ifdef ADDRESS_SANITIZER
+  for (size_t i = 0; i != n; ++i) {
+    EXPECT_EQ(poisoned, __asan_address_is_poisoned(p + i));
+  }
+#endif
+}
+
+template <size_t N>
+void ExpectPoisoned(const unsigned char (&buf)[N],
+                    std::initializer_list<Region> reg) {
+  size_t prev = 0;
+  for (const Region& r : reg) {
+    ExpectRegionPoisoned(buf + prev, r.from - prev, false);
+    ExpectRegionPoisoned(buf + r.from, r.to - r.from, true);
+    prev = r.to;
+  }
+  ExpectRegionPoisoned(buf + prev, N - prev, false);
+}
+
+TEST(Layout, PoisonPadding) {
+  using L = Layout<int8_t, int64_t, int32_t, Int128>;
+
+  constexpr size_t n = L::Partial(1, 2, 3, 4).AllocSize();
+  {
+    constexpr auto x = L::Partial();
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {});
+  }
+  {
+    constexpr auto x = L::Partial(1);
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {{1, 8}});
+  }
+  {
+    constexpr auto x = L::Partial(1, 2);
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {{1, 8}});
+  }
+  {
+    constexpr auto x = L::Partial(1, 2, 3);
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {{1, 8}, {36, 40}});
+  }
+  {
+    constexpr auto x = L::Partial(1, 2, 3, 4);
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {{1, 8}, {36, 40}});
+  }
+  {
+    constexpr L x(1, 2, 3, 4);
+    alignas(max_align_t) const unsigned char c[n] = {};
+    x.PoisonPadding(c);
+    EXPECT_EQ(x.Slices(c), x.Slices(c));
+    ExpectPoisoned(c, {{1, 8}, {36, 40}});
+  }
+}
+
+TEST(Layout, DebugString) {
+  const std::string int64_type =
+#ifdef _MSC_VER
+  "__int64";
+#else   // _MSC_VER
+  std::is_same<int64_t, long long>::value ? "long long" : "long";  // NOLINT
+#endif  // _MSC_VER
+  {
+    constexpr auto x = Layout<int8_t, int32_t, int8_t, Int128>::Partial();
+    EXPECT_EQ("@0<signed char>(1)", x.DebugString());
+  }
+  {
+    constexpr auto x = Layout<int8_t, int32_t, int8_t, Int128>::Partial(1);
+    EXPECT_EQ("@0<signed char>(1)[1]; @4<int>(4)", x.DebugString());
+  }
+  {
+    constexpr auto x = Layout<int8_t, int32_t, int8_t, Int128>::Partial(1, 2);
+    EXPECT_EQ("@0<signed char>(1)[1]; @4<int>(4)[2]; @12<signed char>(1)",
+              x.DebugString());
+  }
+  {
+    constexpr auto x = Layout<int8_t, int32_t, int8_t, Int128>::Partial(1, 2, 3);
+    EXPECT_EQ(
+        "@0<signed char>(1)[1]; @4<int>(4)[2]; @12<signed char>(1)[3]; "
+        "@16<" +
+            int64_type + " [2]>(16)",
+        x.DebugString());
+  }
+  {
+    constexpr auto x = Layout<int8_t, int32_t, int8_t, Int128>::Partial(1, 2, 3, 4);
+    EXPECT_EQ(
+        "@0<signed char>(1)[1]; @4<int>(4)[2]; @12<signed char>(1)[3]; "
+        "@16<" +
+            int64_type + " [2]>(16)[4]",
+        x.DebugString());
+  }
+  {
+    constexpr Layout<int8_t, int32_t, int8_t, Int128> x(1, 2, 3, 4);
+    EXPECT_EQ(
+        "@0<signed char>(1)[1]; @4<int>(4)[2]; @12<signed char>(1)[3]; "
+        "@16<" +
+            int64_type + " [2]>(16)[4]",
+        x.DebugString());
+  }
+}
+
+TEST(Layout, CharTypes) {
+  constexpr Layout<int32_t> x(1);
+  alignas(max_align_t) char c[x.AllocSize()] = {};
+  alignas(max_align_t) unsigned char uc[x.AllocSize()] = {};
+  alignas(max_align_t) signed char sc[x.AllocSize()] = {};
+  alignas(max_align_t) const char cc[x.AllocSize()] = {};
+  alignas(max_align_t) const unsigned char cuc[x.AllocSize()] = {};
+  alignas(max_align_t) const signed char csc[x.AllocSize()] = {};
+
+  Type<int32_t*>(x.Pointer<0>(c));
+  Type<int32_t*>(x.Pointer<0>(uc));
+  Type<int32_t*>(x.Pointer<0>(sc));
+  Type<const int32_t*>(x.Pointer<0>(cc));
+  Type<const int32_t*>(x.Pointer<0>(cuc));
+  Type<const int32_t*>(x.Pointer<0>(csc));
+
+  Type<int32_t*>(x.Pointer<int32_t>(c));
+  Type<int32_t*>(x.Pointer<int32_t>(uc));
+  Type<int32_t*>(x.Pointer<int32_t>(sc));
+  Type<const int32_t*>(x.Pointer<int32_t>(cc));
+  Type<const int32_t*>(x.Pointer<int32_t>(cuc));
+  Type<const int32_t*>(x.Pointer<int32_t>(csc));
+
+  Type<std::tuple<int32_t*>>(x.Pointers(c));
+  Type<std::tuple<int32_t*>>(x.Pointers(uc));
+  Type<std::tuple<int32_t*>>(x.Pointers(sc));
+  Type<std::tuple<const int32_t*>>(x.Pointers(cc));
+  Type<std::tuple<const int32_t*>>(x.Pointers(cuc));
+  Type<std::tuple<const int32_t*>>(x.Pointers(csc));
+
+  Type<Span<int32_t>>(x.Slice<0>(c));
+  Type<Span<int32_t>>(x.Slice<0>(uc));
+  Type<Span<int32_t>>(x.Slice<0>(sc));
+  Type<Span<const int32_t>>(x.Slice<0>(cc));
+  Type<Span<const int32_t>>(x.Slice<0>(cuc));
+  Type<Span<const int32_t>>(x.Slice<0>(csc));
+
+  Type<std::tuple<Span<int32_t>>>(x.Slices(c));
+  Type<std::tuple<Span<int32_t>>>(x.Slices(uc));
+  Type<std::tuple<Span<int32_t>>>(x.Slices(sc));
+  Type<std::tuple<Span<const int32_t>>>(x.Slices(cc));
+  Type<std::tuple<Span<const int32_t>>>(x.Slices(cuc));
+  Type<std::tuple<Span<const int32_t>>>(x.Slices(csc));
+}
+
+TEST(Layout, ConstElementType) {
+  constexpr Layout<const int32_t> x(1);
+  alignas(int32_t) char c[x.AllocSize()] = {};
+  const char* cc = c;
+  const int32_t* p = reinterpret_cast<const int32_t*>(cc);
+
+  EXPECT_EQ(alignof(int32_t), x.Alignment());
+
+  EXPECT_EQ(0, x.Offset<0>());
+  EXPECT_EQ(0, x.Offset<const int32_t>());
+
+  EXPECT_THAT(x.Offsets(), ElementsAre(0));
+
+  EXPECT_EQ(1, x.Size<0>());
+  EXPECT_EQ(1, x.Size<const int32_t>());
+
+  EXPECT_THAT(x.Sizes(), ElementsAre(1));
+
+  EXPECT_EQ(sizeof(int32_t), x.AllocSize());
+
+  EXPECT_EQ(p, Type<const int32_t*>(x.Pointer<0>(c)));
+  EXPECT_EQ(p, Type<const int32_t*>(x.Pointer<0>(cc)));
+
+  EXPECT_EQ(p, Type<const int32_t*>(x.Pointer<const int32_t>(c)));
+  EXPECT_EQ(p, Type<const int32_t*>(x.Pointer<const int32_t>(cc)));
+
+  EXPECT_THAT(Type<std::tuple<const int32_t*>>(x.Pointers(c)), Tuple(p));
+  EXPECT_THAT(Type<std::tuple<const int32_t*>>(x.Pointers(cc)), Tuple(p));
+
+  EXPECT_THAT(Type<Span<const int32_t>>(x.Slice<0>(c)),
+              IsSameSlice(Span<const int32_t>(p, 1)));
+  EXPECT_THAT(Type<Span<const int32_t>>(x.Slice<0>(cc)),
+              IsSameSlice(Span<const int32_t>(p, 1)));
+
+  EXPECT_THAT(Type<Span<const int32_t>>(x.Slice<const int32_t>(c)),
+              IsSameSlice(Span<const int32_t>(p, 1)));
+  EXPECT_THAT(Type<Span<const int32_t>>(x.Slice<const int32_t>(cc)),
+              IsSameSlice(Span<const int32_t>(p, 1)));
+
+  EXPECT_THAT(Type<std::tuple<Span<const int32_t>>>(x.Slices(c)),
+              Tuple(IsSameSlice(Span<const int32_t>(p, 1))));
+  EXPECT_THAT(Type<std::tuple<Span<const int32_t>>>(x.Slices(cc)),
+              Tuple(IsSameSlice(Span<const int32_t>(p, 1))));
+}
+
+namespace example {
+
+// Immutable move-only string with sizeof equal to sizeof(void*). The string
+// size and the characters are kept in the same heap allocation.
+class CompactString {
+ public:
+  CompactString(const char* s = "") {  // NOLINT
+    const size_t size = strlen(s);
+    // size_t[1], followed by char[size + 1].
+    // This statement doesn't allocate memory.
+    const L layout(1, size + 1);
+    // AllocSize() tells us how much memory we need to allocate for all our
+    // data.
+    p_.reset(new unsigned char[layout.AllocSize()]);
+    // If running under ASAN, mark the padding bytes, if any, to catch memory
+    // errors.
+    layout.PoisonPadding(p_.get());
+    // Store the size in the allocation.
+    // Pointer<size_t>() is a synonym for Pointer<0>().
+    *layout.Pointer<size_t>(p_.get()) = size;
+    // Store the characters in the allocation.
+    memcpy(layout.Pointer<char>(p_.get()), s, size + 1);
+  }
+
+  size_t size() const {
+    // Equivalent to reinterpret_cast<size_t&>(*p).
+    return *L::Partial().Pointer<size_t>(p_.get());
+  }
+
+  const char* c_str() const {
+    // Equivalent to reinterpret_cast<char*>(p.get() + sizeof(size_t)).
+    // The argument in Partial(1) specifies that we have size_t[1] in front of
+    // the
+    // characters.
+    return L::Partial(1).Pointer<char>(p_.get());
+  }
+
+ private:
+  // Our heap allocation contains a size_t followed by an array of chars.
+  using L = Layout<size_t, char>;
+  std::unique_ptr<unsigned char[]> p_;
+};
+
+TEST(CompactString, Works) {
+  CompactString s = "hello";
+  EXPECT_EQ(5, s.size());
+  EXPECT_STREQ("hello", s.c_str());
+}
+
+}  // namespace example
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/node_hash_policy.h b/absl/container/internal/node_hash_policy.h
new file mode 100644
index 0000000..065e700
--- /dev/null
+++ b/absl/container/internal/node_hash_policy.h
@@ -0,0 +1,88 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Adapts a policy for nodes.
+//
+// The node policy should model:
+//
+// struct Policy {
+//   // Returns a new node allocated and constructed using the allocator, using
+//   // the specified arguments.
+//   template <class Alloc, class... Args>
+//   value_type* new_element(Alloc* alloc, Args&&... args) const;
+//
+//   // Destroys and deallocates node using the allocator.
+//   template <class Alloc>
+//   void delete_element(Alloc* alloc, value_type* node) const;
+// };
+//
+// It may also optionally define `value()` and `apply()`. For documentation on
+// these, see hash_policy_traits.h.
+
+#ifndef ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_
+#define ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_
+
+#include <cassert>
+#include <cstddef>
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+namespace absl {
+namespace container_internal {
+
+template <class Reference, class Policy>
+struct node_hash_policy {
+  static_assert(std::is_lvalue_reference<Reference>::value, "");
+
+  using slot_type = typename std::remove_cv<
+      typename std::remove_reference<Reference>::type>::type*;
+
+  template <class Alloc, class... Args>
+  static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
+    *slot = Policy::new_element(alloc, std::forward<Args>(args)...);
+  }
+
+  template <class Alloc>
+  static void destroy(Alloc* alloc, slot_type* slot) {
+    Policy::delete_element(alloc, *slot);
+  }
+
+  template <class Alloc>
+  static void transfer(Alloc*, slot_type* new_slot, slot_type* old_slot) {
+    *new_slot = *old_slot;
+  }
+
+  static size_t space_used(const slot_type* slot) {
+    if (slot == nullptr) return Policy::element_space_used(nullptr);
+    return Policy::element_space_used(*slot);
+  }
+
+  static Reference element(slot_type* slot) { return **slot; }
+
+  template <class T, class P = Policy>
+  static auto value(T* elem) -> decltype(P::value(elem)) {
+    return P::value(elem);
+  }
+
+  template <class... Ts, class P = Policy>
+  static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) {
+    return P::apply(std::forward<Ts>(ts)...);
+  }
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_
diff --git a/absl/container/internal/node_hash_policy_test.cc b/absl/container/internal/node_hash_policy_test.cc
new file mode 100644
index 0000000..43d287e
--- /dev/null
+++ b/absl/container/internal/node_hash_policy_test.cc
@@ -0,0 +1,67 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/node_hash_policy.h"
+
+#include <memory>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_policy_traits.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::testing::Pointee;
+
+struct Policy : node_hash_policy<int&, Policy> {
+  using key_type = int;
+  using init_type = int;
+
+  template <class Alloc>
+  static int* new_element(Alloc* alloc, int value) {
+    return new int(value);
+  }
+
+  template <class Alloc>
+  static void delete_element(Alloc* alloc, int* elem) {
+    delete elem;
+  }
+};
+
+using NodePolicy = hash_policy_traits<Policy>;
+
+struct NodeTest : ::testing::Test {
+  std::allocator<int> alloc;
+  int n = 53;
+  int* a = &n;
+};
+
+TEST_F(NodeTest, ConstructDestroy) {
+  NodePolicy::construct(&alloc, &a, 42);
+  EXPECT_THAT(a, Pointee(42));
+  NodePolicy::destroy(&alloc, &a);
+}
+
+TEST_F(NodeTest, transfer) {
+  int s = 42;
+  int* b = &s;
+  NodePolicy::transfer(&alloc, &a, &b);
+  EXPECT_EQ(&s, a);
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/raw_hash_map.h b/absl/container/internal/raw_hash_map.h
new file mode 100644
index 0000000..1edc007
--- /dev/null
+++ b/absl/container/internal/raw_hash_map.h
@@ -0,0 +1,182 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
+#define ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
+
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/raw_hash_set.h"  // IWYU pragma: export
+
+namespace absl {
+namespace container_internal {
+
+template <class Policy, class Hash, class Eq, class Alloc>
+class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
+  // P is Policy. It's passed as a template argument to support maps that have
+  // incomplete types as values, as in unordered_map<K, IncompleteType>.
+  // MappedReference<> may be a non-reference type.
+  template <class P>
+  using MappedReference = decltype(P::value(
+      std::addressof(std::declval<typename raw_hash_map::reference>())));
+
+  // MappedConstReference<> may be a non-reference type.
+  template <class P>
+  using MappedConstReference = decltype(P::value(
+      std::addressof(std::declval<typename raw_hash_map::const_reference>())));
+
+ public:
+  using key_type = typename Policy::key_type;
+  using mapped_type = typename Policy::mapped_type;
+  template <typename K>
+  using key_arg = typename raw_hash_map::raw_hash_set::template key_arg<K>;
+
+  static_assert(!std::is_reference<key_type>::value, "");
+  // TODO(alkis): remove this assertion and verify that reference mapped_type is
+  // supported.
+  static_assert(!std::is_reference<mapped_type>::value, "");
+
+  using iterator = typename raw_hash_map::raw_hash_set::iterator;
+  using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator;
+
+  raw_hash_map() {}
+  using raw_hash_map::raw_hash_set::raw_hash_set;
+
+  // The last two template parameters ensure that both arguments are rvalues
+  // (lvalue arguments are handled by the overloads below). This is necessary
+  // for supporting bitfield arguments.
+  //
+  //   union { int n : 1; };
+  //   flat_hash_map<int, int> m;
+  //   m.insert_or_assign(n, n);
+  template <class K = key_type, class V = mapped_type, K* = nullptr,
+            V* = nullptr>
+  std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, V&& v) {
+    return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
+  }
+
+  template <class K = key_type, class V = mapped_type, K* = nullptr>
+  std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v) {
+    return insert_or_assign_impl(std::forward<K>(k), v);
+  }
+
+  template <class K = key_type, class V = mapped_type, V* = nullptr>
+  std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v) {
+    return insert_or_assign_impl(k, std::forward<V>(v));
+  }
+
+  template <class K = key_type, class V = mapped_type>
+  std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v) {
+    return insert_or_assign_impl(k, v);
+  }
+
+  template <class K = key_type, class V = mapped_type, K* = nullptr,
+            V* = nullptr>
+  iterator insert_or_assign(const_iterator, key_arg<K>&& k, V&& v) {
+    return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
+  }
+
+  template <class K = key_type, class V = mapped_type, K* = nullptr>
+  iterator insert_or_assign(const_iterator, key_arg<K>&& k, const V& v) {
+    return insert_or_assign(std::forward<K>(k), v).first;
+  }
+
+  template <class K = key_type, class V = mapped_type, V* = nullptr>
+  iterator insert_or_assign(const_iterator, const key_arg<K>& k, V&& v) {
+    return insert_or_assign(k, std::forward<V>(v)).first;
+  }
+
+  template <class K = key_type, class V = mapped_type>
+  iterator insert_or_assign(const_iterator, const key_arg<K>& k, const V& v) {
+    return insert_or_assign(k, v).first;
+  }
+
+  template <class K = key_type, class... Args,
+            typename std::enable_if<
+                !std::is_convertible<K, const_iterator>::value, int>::type = 0,
+            K* = nullptr>
+  std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args) {
+    return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
+  }
+
+  template <class K = key_type, class... Args,
+            typename std::enable_if<
+                !std::is_convertible<K, const_iterator>::value, int>::type = 0>
+  std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args) {
+    return try_emplace_impl(k, std::forward<Args>(args)...);
+  }
+
+  template <class K = key_type, class... Args, K* = nullptr>
+  iterator try_emplace(const_iterator, key_arg<K>&& k, Args&&... args) {
+    return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
+  }
+
+  template <class K = key_type, class... Args>
+  iterator try_emplace(const_iterator, const key_arg<K>& k, Args&&... args) {
+    return try_emplace(k, std::forward<Args>(args)...).first;
+  }
+
+  template <class K = key_type, class P = Policy>
+  MappedReference<P> at(const key_arg<K>& key) {
+    auto it = this->find(key);
+    if (it == this->end()) std::abort();
+    return Policy::value(&*it);
+  }
+
+  template <class K = key_type, class P = Policy>
+  MappedConstReference<P> at(const key_arg<K>& key) const {
+    auto it = this->find(key);
+    if (it == this->end()) std::abort();
+    return Policy::value(&*it);
+  }
+
+  template <class K = key_type, class P = Policy, K* = nullptr>
+  MappedReference<P> operator[](key_arg<K>&& key) {
+    return Policy::value(&*try_emplace(std::forward<K>(key)).first);
+  }
+
+  template <class K = key_type, class P = Policy>
+  MappedReference<P> operator[](const key_arg<K>& key) {
+    return Policy::value(&*try_emplace(key).first);
+  }
+
+ private:
+  template <class K, class V>
+  std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
+    auto res = this->find_or_prepare_insert(k);
+    if (res.second)
+      this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
+    else
+      Policy::value(&*this->iterator_at(res.first)) = std::forward<V>(v);
+    return {this->iterator_at(res.first), res.second};
+  }
+
+  template <class K = key_type, class... Args>
+  std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args) {
+    auto res = this->find_or_prepare_insert(k);
+    if (res.second)
+      this->emplace_at(res.first, std::piecewise_construct,
+                       std::forward_as_tuple(std::forward<K>(k)),
+                       std::forward_as_tuple(std::forward<Args>(args)...));
+    return {this->iterator_at(res.first), res.second};
+  }
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
diff --git a/absl/container/internal/raw_hash_set.cc b/absl/container/internal/raw_hash_set.cc
new file mode 100644
index 0000000..1015312
--- /dev/null
+++ b/absl/container/internal/raw_hash_set.cc
@@ -0,0 +1,45 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/raw_hash_set.h"
+
+#include <cstddef>
+
+#include "absl/base/config.h"
+
+namespace absl {
+namespace container_internal {
+
+constexpr size_t Group::kWidth;
+
+// Returns "random" seed.
+inline size_t RandomSeed() {
+#if ABSL_HAVE_THREAD_LOCAL
+  static thread_local size_t counter = 0;
+  size_t value = ++counter;
+#else   // ABSL_HAVE_THREAD_LOCAL
+  static std::atomic<size_t> counter;
+  size_t value = counter.fetch_add(1, std::memory_order_relaxed);
+#endif  // ABSL_HAVE_THREAD_LOCAL
+  return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
+}
+
+bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl) {
+  // To avoid problems with weak hashes and single bit tests, we use % 13.
+  // TODO(kfm,sbenza): revisit after we do unconditional mixing
+  return (H1(hash, ctrl) ^ RandomSeed()) % 13 > 6;
+}
+
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h
new file mode 100644
index 0000000..70da90f
--- /dev/null
+++ b/absl/container/internal/raw_hash_set.h
@@ -0,0 +1,1915 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// An open-addressing
+// hashtable with quadratic probing.
+//
+// This is a low level hashtable on top of which different interfaces can be
+// implemented, like flat_hash_set, node_hash_set, string_hash_set, etc.
+//
+// The table interface is similar to that of std::unordered_set. Notable
+// differences are that most member functions support heterogeneous keys when
+// BOTH the hash and eq functions are marked as transparent. They do so by
+// providing a typedef called `is_transparent`.
+//
+// When heterogeneous lookup is enabled, functions that take key_type act as if
+// they have an overload set like:
+//
+//   iterator find(const key_type& key);
+//   template <class K>
+//   iterator find(const K& key);
+//
+//   size_type erase(const key_type& key);
+//   template <class K>
+//   size_type erase(const K& key);
+//
+//   std::pair<iterator, iterator> equal_range(const key_type& key);
+//   template <class K>
+//   std::pair<iterator, iterator> equal_range(const K& key);
+//
+// When heterogeneous lookup is disabled, only the explicit `key_type` overloads
+// exist.
+//
+// find() also supports passing the hash explicitly:
+//
+//   iterator find(const key_type& key, size_t hash);
+//   template <class U>
+//   iterator find(const U& key, size_t hash);
+//
+// In addition the pointer to element and iterator stability guarantees are
+// weaker: all iterators and pointers are invalidated after a new element is
+// inserted.
+//
+// IMPLEMENTATION DETAILS
+//
+// The table stores elements inline in a slot array. In addition to the slot
+// array the table maintains some control state per slot. The extra state is one
+// byte per slot and stores empty or deleted marks, or alternatively 7 bits from
+// the hash of an occupied slot. The table is split into logical groups of
+// slots, like so:
+//
+//      Group 1         Group 2        Group 3
+// +---------------+---------------+---------------+
+// | | | | | | | | | | | | | | | | | | | | | | | | |
+// +---------------+---------------+---------------+
+//
+// On lookup the hash is split into two parts:
+// - H2: 7 bits (those stored in the control bytes)
+// - H1: the rest of the bits
+// The groups are probed using H1. For each group the slots are matched to H2 in
+// parallel. Because H2 is 7 bits (128 states) and the number of slots per group
+// is low (8 or 16) in almost all cases a match in H2 is also a lookup hit.
+//
+// On insert, once the right group is found (as in lookup), its slots are
+// filled in order.
+//
+// On erase a slot is cleared. In case the group did not have any empty slots
+// before the erase, the erased slot is marked as deleted.
+//
+// Groups without empty slots (but maybe with deleted slots) extend the probe
+// sequence. The probing algorithm is quadratic. Given N the number of groups,
+// the probing function for the i'th probe is:
+//
+//   P(0) = H1 % N
+//
+//   P(i) = (P(i - 1) + i) % N
+//
+// This probing function guarantees that after N probes, all the groups of the
+// table will be probed exactly once.
+
+#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_
+#define ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_
+
+#ifndef SWISSTABLE_HAVE_SSE2
+#ifdef __SSE2__
+#define SWISSTABLE_HAVE_SSE2 1
+#else
+#define SWISSTABLE_HAVE_SSE2 0
+#endif
+#endif
+
+#ifndef SWISSTABLE_HAVE_SSSE3
+#ifdef __SSSE3__
+#define SWISSTABLE_HAVE_SSSE3 1
+#else
+#define SWISSTABLE_HAVE_SSSE3 0
+#endif
+#endif
+
+#if SWISSTABLE_HAVE_SSSE3 && !SWISSTABLE_HAVE_SSE2
+#error "Bad configuration!"
+#endif
+
+#if SWISSTABLE_HAVE_SSE2
+#include <x86intrin.h>
+#endif
+
+#include <algorithm>
+#include <cmath>
+#include <cstdint>
+#include <cstring>
+#include <iterator>
+#include <limits>
+#include <memory>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include "absl/base/internal/bits.h"
+#include "absl/base/internal/endian.h"
+#include "absl/base/port.h"
+#include "absl/container/internal/compressed_tuple.h"
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/hash_policy_traits.h"
+#include "absl/container/internal/hashtable_debug_hooks.h"
+#include "absl/container/internal/layout.h"
+#include "absl/memory/memory.h"
+#include "absl/meta/type_traits.h"
+#include "absl/types/optional.h"
+#include "absl/utility/utility.h"
+
+namespace absl {
+namespace container_internal {
+
+template <size_t Width>
+class probe_seq {
+ public:
+  probe_seq(size_t hash, size_t mask) {
+    assert(((mask + 1) & mask) == 0 && "not a mask");
+    mask_ = mask;
+    offset_ = hash & mask_;
+  }
+  size_t offset() const { return offset_; }
+  size_t offset(size_t i) const { return (offset_ + i) & mask_; }
+
+  void next() {
+    index_ += Width;
+    offset_ += index_;
+    offset_ &= mask_;
+  }
+  // 0-based probe index. The i-th probe in the probe sequence.
+  size_t index() const { return index_; }
+
+ private:
+  size_t mask_;
+  size_t offset_;
+  size_t index_ = 0;
+};
+
+template <class ContainerKey, class Hash, class Eq>
+struct RequireUsableKey {
+  template <class PassedKey, class... Args>
+  std::pair<
+      decltype(std::declval<const Hash&>()(std::declval<const PassedKey&>())),
+      decltype(std::declval<const Eq&>()(std::declval<const ContainerKey&>(),
+                                         std::declval<const PassedKey&>()))>*
+  operator()(const PassedKey&, const Args&...) const;
+};
+
+template <class E, class Policy, class Hash, class Eq, class... Ts>
+struct IsDecomposable : std::false_type {};
+
+template <class Policy, class Hash, class Eq, class... Ts>
+struct IsDecomposable<
+    absl::void_t<decltype(
+        Policy::apply(RequireUsableKey<typename Policy::key_type, Hash, Eq>(),
+                      std::declval<Ts>()...))>,
+    Policy, Hash, Eq, Ts...> : std::true_type {};
+
+template <class, class = void>
+struct IsTransparent : std::false_type {};
+template <class T>
+struct IsTransparent<T, absl::void_t<typename T::is_transparent>>
+    : std::true_type {};
+
+// TODO(alkis): Switch to std::is_nothrow_swappable when gcc/clang supports it.
+template <class T>
+constexpr bool IsNoThrowSwappable() {
+  using std::swap;
+  return noexcept(swap(std::declval<T&>(), std::declval<T&>()));
+}
+
+template <typename T>
+int TrailingZeros(T x) {
+  return sizeof(T) == 8 ? base_internal::CountTrailingZerosNonZero64(x)
+                        : base_internal::CountTrailingZerosNonZero32(x);
+}
+
+template <typename T>
+int LeadingZeros(T x) {
+  return sizeof(T) == 8 ? base_internal::CountLeadingZeros64(x)
+                        : base_internal::CountLeadingZeros32(x);
+}
+
+// An abstraction over a bitmask. It provides an easy way to iterate through the
+// indexes of the set bits of a bitmask.  When Shift=0 (platforms with SSE),
+// this is a true bitmask.  On non-SSE, platforms the arithematic used to
+// emulate the SSE behavior works in bytes (Shift=3) and leaves each bytes as
+// either 0x00 or 0x80.
+//
+// For example:
+//   for (int i : BitMask<uint32_t, 16>(0x5)) -> yields 0, 2
+//   for (int i : BitMask<uint64_t, 8, 3>(0x0000000080800000)) -> yields 2, 3
+template <class T, int SignificantBits, int Shift = 0>
+class BitMask {
+  static_assert(std::is_unsigned<T>::value, "");
+  static_assert(Shift == 0 || Shift == 3, "");
+
+ public:
+  // These are useful for unit tests (gunit).
+  using value_type = int;
+  using iterator = BitMask;
+  using const_iterator = BitMask;
+
+  explicit BitMask(T mask) : mask_(mask) {}
+  BitMask& operator++() {
+    mask_ &= (mask_ - 1);
+    return *this;
+  }
+  explicit operator bool() const { return mask_ != 0; }
+  int operator*() const { return LowestBitSet(); }
+  int LowestBitSet() const {
+    return container_internal::TrailingZeros(mask_) >> Shift;
+  }
+  int HighestBitSet() const {
+    return (sizeof(T) * CHAR_BIT - container_internal::LeadingZeros(mask_) -
+            1) >>
+           Shift;
+  }
+
+  BitMask begin() const { return *this; }
+  BitMask end() const { return BitMask(0); }
+
+  int TrailingZeros() const {
+    return container_internal::TrailingZeros(mask_) >> Shift;
+  }
+
+  int LeadingZeros() const {
+    constexpr int total_significant_bits = SignificantBits << Shift;
+    constexpr int extra_bits = sizeof(T) * 8 - total_significant_bits;
+    return container_internal::LeadingZeros(mask_ << extra_bits) >> Shift;
+  }
+
+ private:
+  friend bool operator==(const BitMask& a, const BitMask& b) {
+    return a.mask_ == b.mask_;
+  }
+  friend bool operator!=(const BitMask& a, const BitMask& b) {
+    return a.mask_ != b.mask_;
+  }
+
+  T mask_;
+};
+
+using ctrl_t = signed char;
+using h2_t = uint8_t;
+
+// The values here are selected for maximum performance. See the static asserts
+// below for details.
+enum Ctrl : ctrl_t {
+  kEmpty = -128,   // 0b10000000
+  kDeleted = -2,   // 0b11111110
+  kSentinel = -1,  // 0b11111111
+};
+static_assert(
+    kEmpty & kDeleted & kSentinel & 0x80,
+    "Special markers need to have the MSB to make checking for them efficient");
+static_assert(kEmpty < kSentinel && kDeleted < kSentinel,
+              "kEmpty and kDeleted must be smaller than kSentinel to make the "
+              "SIMD test of IsEmptyOrDeleted() efficient");
+static_assert(kSentinel == -1,
+              "kSentinel must be -1 to elide loading it from memory into SIMD "
+              "registers (pcmpeqd xmm, xmm)");
+static_assert(kEmpty == -128,
+              "kEmpty must be -128 to make the SIMD check for its "
+              "existence efficient (psignb xmm, xmm)");
+static_assert(~kEmpty & ~kDeleted & kSentinel & 0x7F,
+              "kEmpty and kDeleted must share an unset bit that is not shared "
+              "by kSentinel to make the scalar test for MatchEmptyOrDeleted() "
+              "efficient");
+static_assert(kDeleted == -2,
+              "kDeleted must be -2 to make the implementation of "
+              "ConvertSpecialToEmptyAndFullToDeleted efficient");
+
+// A single block of empty control bytes for tables without any slots allocated.
+// This enables removing a branch in the hot path of find().
+inline ctrl_t* EmptyGroup() {
+  alignas(16) static constexpr ctrl_t empty_group[] = {
+      kSentinel, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty,
+      kEmpty,    kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty};
+  return const_cast<ctrl_t*>(empty_group);
+}
+
+// Mixes a randomly generated per-process seed with `hash` and `ctrl` to
+// randomize insertion order within groups.
+bool ShouldInsertBackwards(size_t hash, ctrl_t* ctrl);
+
+// Returns a hash seed.
+//
+// The seed consists of the ctrl_ pointer, which adds enough entropy to ensure
+// non-determinism of iteration order in most cases.
+inline size_t HashSeed(const ctrl_t* ctrl) {
+  // The low bits of the pointer have little or no entropy because of
+  // alignment. We shift the pointer to try to use higher entropy bits. A
+  // good number seems to be 12 bits, because that aligns with page size.
+  return reinterpret_cast<uintptr_t>(ctrl) >> 12;
+}
+
+inline size_t H1(size_t hash, const ctrl_t* ctrl) {
+  return (hash >> 7) ^ HashSeed(ctrl);
+}
+inline ctrl_t H2(size_t hash) { return hash & 0x7F; }
+
+inline bool IsEmpty(ctrl_t c) { return c == kEmpty; }
+inline bool IsFull(ctrl_t c) { return c >= 0; }
+inline bool IsDeleted(ctrl_t c) { return c == kDeleted; }
+inline bool IsEmptyOrDeleted(ctrl_t c) { return c < kSentinel; }
+
+#if SWISSTABLE_HAVE_SSE2
+struct Group {
+  static constexpr size_t kWidth = 16;  // the number of slots per group
+
+  explicit Group(const ctrl_t* pos) {
+    ctrl = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pos));
+  }
+
+  // Returns a bitmask representing the positions of slots that match hash.
+  BitMask<uint32_t, kWidth> Match(h2_t hash) const {
+    auto match = _mm_set1_epi8(hash);
+    return BitMask<uint32_t, kWidth>(
+        _mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl)));
+  }
+
+  // Returns a bitmask representing the positions of empty slots.
+  BitMask<uint32_t, kWidth> MatchEmpty() const {
+#if SWISSTABLE_HAVE_SSSE3
+    // This only works because kEmpty is -128.
+    return BitMask<uint32_t, kWidth>(
+        _mm_movemask_epi8(_mm_sign_epi8(ctrl, ctrl)));
+#else
+    return Match(kEmpty);
+#endif
+  }
+
+  // Returns a bitmask representing the positions of empty or deleted slots.
+  BitMask<uint32_t, kWidth> MatchEmptyOrDeleted() const {
+    auto special = _mm_set1_epi8(kSentinel);
+    return BitMask<uint32_t, kWidth>(
+        _mm_movemask_epi8(_mm_cmpgt_epi8(special, ctrl)));
+  }
+
+  // Returns the number of trailing empty or deleted elements in the group.
+  uint32_t CountLeadingEmptyOrDeleted() const {
+    auto special = _mm_set1_epi8(kSentinel);
+    return TrailingZeros(_mm_movemask_epi8(_mm_cmpgt_epi8(special, ctrl)) + 1);
+  }
+
+  void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
+    auto msbs = _mm_set1_epi8(0x80);
+    auto x126 = _mm_set1_epi8(126);
+#if SWISSTABLE_HAVE_SSSE3
+    auto res = _mm_or_si128(_mm_shuffle_epi8(x126, ctrl), msbs);
+#else
+    auto zero = _mm_setzero_si128();
+    auto special_mask = _mm_cmpgt_epi8(zero, ctrl);
+    auto res = _mm_or_si128(msbs, _mm_andnot_si128(special_mask, x126));
+#endif
+    _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), res);
+  }
+
+  __m128i ctrl;
+};
+#else
+struct Group {
+  static constexpr size_t kWidth = 8;
+
+  explicit Group(const ctrl_t* pos) : ctrl(little_endian::Load64(pos)) {}
+
+  BitMask<uint64_t, kWidth, 3> Match(h2_t hash) const {
+    // For the technique, see:
+    // http://graphics.stanford.edu/~seander/bithacks.html##ValueInWord
+    // (Determine if a word has a byte equal to n).
+    //
+    // Caveat: there are false positives but:
+    // - they only occur if there is a real match
+    // - they never occur on kEmpty, kDeleted, kSentinel
+    // - they will be handled gracefully by subsequent checks in code
+    //
+    // Example:
+    //   v = 0x1716151413121110
+    //   hash = 0x12
+    //   retval = (v - lsbs) & ~v & msbs = 0x0000000080800000
+    constexpr uint64_t msbs = 0x8080808080808080ULL;
+    constexpr uint64_t lsbs = 0x0101010101010101ULL;
+    auto x = ctrl ^ (lsbs * hash);
+    return BitMask<uint64_t, kWidth, 3>((x - lsbs) & ~x & msbs);
+  }
+
+  BitMask<uint64_t, kWidth, 3> MatchEmpty() const {
+    constexpr uint64_t msbs = 0x8080808080808080ULL;
+    return BitMask<uint64_t, kWidth, 3>((ctrl & (~ctrl << 6)) & msbs);
+  }
+
+  BitMask<uint64_t, kWidth, 3> MatchEmptyOrDeleted() const {
+    constexpr uint64_t msbs = 0x8080808080808080ULL;
+    return BitMask<uint64_t, kWidth, 3>((ctrl & (~ctrl << 7)) & msbs);
+  }
+
+  uint32_t CountLeadingEmptyOrDeleted() const {
+    constexpr uint64_t gaps = 0x00FEFEFEFEFEFEFEULL;
+    return (TrailingZeros(((~ctrl & (ctrl >> 7)) | gaps) + 1) + 7) >> 3;
+  }
+
+  void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
+    constexpr uint64_t msbs = 0x8080808080808080ULL;
+    constexpr uint64_t lsbs = 0x0101010101010101ULL;
+    auto x = ctrl & msbs;
+    auto res = (~x + (x >> 7)) & ~lsbs;
+    little_endian::Store64(dst, res);
+  }
+
+  uint64_t ctrl;
+};
+#endif  // SWISSTABLE_HAVE_SSE2
+
+template <class Policy, class Hash, class Eq, class Alloc>
+class raw_hash_set;
+
+
+inline bool IsValidCapacity(size_t n) {
+  return ((n + 1) & n) == 0 && n >= Group::kWidth - 1;
+}
+
+// PRECONDITION:
+//   IsValidCapacity(capacity)
+//   ctrl[capacity] == kSentinel
+//   ctrl[i] != kSentinel for all i < capacity
+// Applies mapping for every byte in ctrl:
+//   DELETED -> EMPTY
+//   EMPTY -> EMPTY
+//   FULL -> DELETED
+inline void ConvertDeletedToEmptyAndFullToDeleted(
+    ctrl_t* ctrl, size_t capacity) {
+  assert(ctrl[capacity] == kSentinel);
+  assert(IsValidCapacity(capacity));
+  for (ctrl_t* pos = ctrl; pos != ctrl + capacity + 1; pos += Group::kWidth) {
+    Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
+  }
+  // Copy the cloned ctrl bytes.
+  std::memcpy(ctrl + capacity + 1, ctrl, Group::kWidth);
+  ctrl[capacity] = kSentinel;
+}
+
+// Rounds up the capacity to the next power of 2 minus 1 and ensures it is
+// greater or equal to Group::kWidth - 1.
+inline size_t NormalizeCapacity(size_t n) {
+  constexpr size_t kMinCapacity = Group::kWidth - 1;
+  return n <= kMinCapacity
+             ? kMinCapacity
+             : std::numeric_limits<size_t>::max() >> LeadingZeros(n);
+}
+
+// The node_handle concept from C++17.
+// We specialize node_handle for sets and maps. node_handle_base holds the
+// common API of both.
+template <typename Policy, typename Alloc>
+class node_handle_base {
+ protected:
+  using PolicyTraits = hash_policy_traits<Policy>;
+  using slot_type = typename PolicyTraits::slot_type;
+
+ public:
+  using allocator_type = Alloc;
+
+  constexpr node_handle_base() {}
+  node_handle_base(node_handle_base&& other) noexcept {
+    *this = std::move(other);
+  }
+  ~node_handle_base() { destroy(); }
+  node_handle_base& operator=(node_handle_base&& other) {
+    destroy();
+    if (!other.empty()) {
+      alloc_ = other.alloc_;
+      PolicyTraits::transfer(alloc(), slot(), other.slot());
+      other.reset();
+    }
+    return *this;
+  }
+
+  bool empty() const noexcept { return !alloc_; }
+  explicit operator bool() const noexcept { return !empty(); }
+  allocator_type get_allocator() const { return *alloc_; }
+
+ protected:
+  template <typename, typename, typename, typename>
+  friend class raw_hash_set;
+
+  node_handle_base(const allocator_type& a, slot_type* s) : alloc_(a) {
+    PolicyTraits::transfer(alloc(), slot(), s);
+  }
+
+  void destroy() {
+    if (!empty()) {
+      PolicyTraits::destroy(alloc(), slot());
+      reset();
+    }
+  }
+
+  void reset() {
+    assert(alloc_.has_value());
+    alloc_ = absl::nullopt;
+  }
+
+  slot_type* slot() const {
+    assert(!empty());
+    return reinterpret_cast<slot_type*>(std::addressof(slot_space_));
+  }
+  allocator_type* alloc() { return std::addressof(*alloc_); }
+
+ private:
+  absl::optional<allocator_type> alloc_;
+  mutable absl::aligned_storage_t<sizeof(slot_type), alignof(slot_type)>
+      slot_space_;
+};
+
+// For sets.
+template <typename Policy, typename Alloc, typename = void>
+class node_handle : public node_handle_base<Policy, Alloc> {
+  using Base = typename node_handle::node_handle_base;
+
+ public:
+  using value_type = typename Base::PolicyTraits::value_type;
+
+  constexpr node_handle() {}
+
+  value_type& value() const {
+    return Base::PolicyTraits::element(this->slot());
+  }
+
+ private:
+  template <typename, typename, typename, typename>
+  friend class raw_hash_set;
+
+  node_handle(const Alloc& a, typename Base::slot_type* s) : Base(a, s) {}
+};
+
+// For maps.
+template <typename Policy, typename Alloc>
+class node_handle<Policy, Alloc, absl::void_t<typename Policy::mapped_type>>
+    : public node_handle_base<Policy, Alloc> {
+  using Base = typename node_handle::node_handle_base;
+
+ public:
+  using key_type = typename Policy::key_type;
+  using mapped_type = typename Policy::mapped_type;
+
+  constexpr node_handle() {}
+
+  auto key() const -> decltype(Base::PolicyTraits::key(this->slot())) {
+    return Base::PolicyTraits::key(this->slot());
+  }
+
+  mapped_type& mapped() const {
+    return Base::PolicyTraits::value(
+        &Base::PolicyTraits::element(this->slot()));
+  }
+
+ private:
+  template <typename, typename, typename, typename>
+  friend class raw_hash_set;
+
+  node_handle(const Alloc& a, typename Base::slot_type* s) : Base(a, s) {}
+};
+
+// Implement the insert_return_type<> concept of C++17.
+template <class Iterator, class NodeType>
+struct insert_return_type {
+  Iterator position;
+  bool inserted;
+  NodeType node;
+};
+
+// Helper trait to allow or disallow arbitrary keys when the hash and
+// eq functions are transparent.
+// It is very important that the inner template is an alias and that the type it
+// produces is not a dependent type. Otherwise, type deduction would fail.
+template <bool is_transparent>
+struct KeyArg {
+  // Transparent. Forward `K`.
+  template <typename K, typename key_type>
+  using type = K;
+};
+
+template <>
+struct KeyArg<false> {
+  // Not transparent. Always use `key_type`.
+  template <typename K, typename key_type>
+  using type = key_type;
+};
+
+// Policy: a policy defines how to perform different operations on
+// the slots of the hashtable (see hash_policy_traits.h for the full interface
+// of policy).
+//
+// Hash: a (possibly polymorphic) functor that hashes keys of the hashtable. The
+// functor should accept a key and return size_t as hash. For best performance
+// it is important that the hash function provides high entropy across all bits
+// of the hash.
+//
+// Eq: a (possibly polymorphic) functor that compares two keys for equality. It
+// should accept two (of possibly different type) keys and return a bool: true
+// if they are equal, false if they are not. If two keys compare equal, then
+// their hash values as defined by Hash MUST be equal.
+//
+// Allocator: an Allocator [http://devdocs.io/cpp/concept/allocator] with which
+// the storage of the hashtable will be allocated and the elements will be
+// constructed and destroyed.
+template <class Policy, class Hash, class Eq, class Alloc>
+class raw_hash_set {
+  using PolicyTraits = hash_policy_traits<Policy>;
+  using KeyArgImpl = container_internal::KeyArg<IsTransparent<Eq>::value &&
+                                                IsTransparent<Hash>::value>;
+
+ public:
+  using init_type = typename PolicyTraits::init_type;
+  using key_type = typename PolicyTraits::key_type;
+  // TODO(sbenza): Hide slot_type as it is an implementation detail. Needs user
+  // code fixes!
+  using slot_type = typename PolicyTraits::slot_type;
+  using allocator_type = Alloc;
+  using size_type = size_t;
+  using difference_type = ptrdiff_t;
+  using hasher = Hash;
+  using key_equal = Eq;
+  using policy_type = Policy;
+  using value_type = typename PolicyTraits::value_type;
+  using reference = value_type&;
+  using const_reference = const value_type&;
+  using pointer = typename absl::allocator_traits<
+      allocator_type>::template rebind_traits<value_type>::pointer;
+  using const_pointer = typename absl::allocator_traits<
+      allocator_type>::template rebind_traits<value_type>::const_pointer;
+
+  // Alias used for heterogeneous lookup functions.
+  // `key_arg<K>` evaluates to `K` when the functors are tranparent and to
+  // `key_type` otherwise. It permits template argument deduction on `K` for the
+  // transparent case.
+  template <class K>
+  using key_arg = typename KeyArgImpl::template type<K, key_type>;
+
+ private:
+  // Give an early error when key_type is not hashable/eq.
+  auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));
+  auto KeyTypeCanBeEq(const Eq& eq, const key_type& k) -> decltype(eq(k, k));
+
+  using Layout = absl::container_internal::Layout<ctrl_t, slot_type>;
+
+  static Layout MakeLayout(size_t capacity) {
+    assert(IsValidCapacity(capacity));
+    return Layout(capacity + Group::kWidth + 1, capacity);
+  }
+
+  using AllocTraits = absl::allocator_traits<allocator_type>;
+  using SlotAlloc = typename absl::allocator_traits<
+      allocator_type>::template rebind_alloc<slot_type>;
+  using SlotAllocTraits = typename absl::allocator_traits<
+      allocator_type>::template rebind_traits<slot_type>;
+
+  static_assert(std::is_lvalue_reference<reference>::value,
+                "Policy::element() must return a reference");
+
+  template <typename T>
+  struct SameAsElementReference
+      : std::is_same<typename std::remove_cv<
+                         typename std::remove_reference<reference>::type>::type,
+                     typename std::remove_cv<
+                         typename std::remove_reference<T>::type>::type> {};
+
+  // An enabler for insert(T&&): T must be convertible to init_type or be the
+  // same as [cv] value_type [ref].
+  // Note: we separate SameAsElementReference into its own type to avoid using
+  // reference unless we need to. MSVC doesn't seem to like it in some
+  // cases.
+  template <class T>
+  using RequiresInsertable = typename std::enable_if<
+      absl::disjunction<std::is_convertible<T, init_type>,
+                        SameAsElementReference<T>>::value,
+      int>::type;
+
+  // RequiresNotInit is a workaround for gcc prior to 7.1.
+  // See https://godbolt.org/g/Y4xsUh.
+  template <class T>
+  using RequiresNotInit =
+      typename std::enable_if<!std::is_same<T, init_type>::value, int>::type;
+
+  template <class... Ts>
+  using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
+
+ public:
+  static_assert(std::is_same<pointer, value_type*>::value,
+                "Allocators with custom pointer types are not supported");
+  static_assert(std::is_same<const_pointer, const value_type*>::value,
+                "Allocators with custom pointer types are not supported");
+
+  class iterator {
+    friend class raw_hash_set;
+
+   public:
+    using iterator_category = std::forward_iterator_tag;
+    using value_type = typename raw_hash_set::value_type;
+    using reference =
+        absl::conditional_t<PolicyTraits::constant_iterators::value,
+                            const value_type&, value_type&>;
+    using pointer = absl::remove_reference_t<reference>*;
+    using difference_type = typename raw_hash_set::difference_type;
+
+    iterator() {}
+
+    // PRECONDITION: not an end() iterator.
+    reference operator*() const { return PolicyTraits::element(slot_); }
+
+    // PRECONDITION: not an end() iterator.
+    pointer operator->() const { return &operator*(); }
+
+    // PRECONDITION: not an end() iterator.
+    iterator& operator++() {
+      ++ctrl_;
+      ++slot_;
+      skip_empty_or_deleted();
+      return *this;
+    }
+    // PRECONDITION: not an end() iterator.
+    iterator operator++(int) {
+      auto tmp = *this;
+      ++*this;
+      return tmp;
+    }
+
+    friend bool operator==(const iterator& a, const iterator& b) {
+      return a.ctrl_ == b.ctrl_;
+    }
+    friend bool operator!=(const iterator& a, const iterator& b) {
+      return !(a == b);
+    }
+
+   private:
+    iterator(ctrl_t* ctrl) : ctrl_(ctrl) {}  // for end()
+    iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {}
+
+    void skip_empty_or_deleted() {
+      while (IsEmptyOrDeleted(*ctrl_)) {
+        // ctrl is not necessarily aligned to Group::kWidth. It is also likely
+        // to read past the space for ctrl bytes and into slots. This is ok
+        // because ctrl has sizeof() == 1 and slot has sizeof() >= 1 so there
+        // is no way to read outside the combined slot array.
+        uint32_t shift = Group{ctrl_}.CountLeadingEmptyOrDeleted();
+        ctrl_ += shift;
+        slot_ += shift;
+      }
+    }
+
+    ctrl_t* ctrl_ = nullptr;
+    slot_type* slot_;
+  };
+
+  class const_iterator {
+    friend class raw_hash_set;
+
+   public:
+    using iterator_category = typename iterator::iterator_category;
+    using value_type = typename raw_hash_set::value_type;
+    using reference = typename raw_hash_set::const_reference;
+    using pointer = typename raw_hash_set::const_pointer;
+    using difference_type = typename raw_hash_set::difference_type;
+
+    const_iterator() {}
+    // Implicit construction from iterator.
+    const_iterator(iterator i) : inner_(std::move(i)) {}
+
+    reference operator*() const { return *inner_; }
+    pointer operator->() const { return inner_.operator->(); }
+
+    const_iterator& operator++() {
+      ++inner_;
+      return *this;
+    }
+    const_iterator operator++(int) { return inner_++; }
+
+    friend bool operator==(const const_iterator& a, const const_iterator& b) {
+      return a.inner_ == b.inner_;
+    }
+    friend bool operator!=(const const_iterator& a, const const_iterator& b) {
+      return !(a == b);
+    }
+
+   private:
+    const_iterator(const ctrl_t* ctrl, const slot_type* slot)
+        : inner_(const_cast<ctrl_t*>(ctrl), const_cast<slot_type*>(slot)) {}
+
+    iterator inner_;
+  };
+
+  using node_type = container_internal::node_handle<Policy, Alloc>;
+
+  raw_hash_set() noexcept(
+      std::is_nothrow_default_constructible<hasher>::value&&
+          std::is_nothrow_default_constructible<key_equal>::value&&
+              std::is_nothrow_default_constructible<allocator_type>::value) {}
+
+  explicit raw_hash_set(size_t bucket_count, const hasher& hash = hasher(),
+                        const key_equal& eq = key_equal(),
+                        const allocator_type& alloc = allocator_type())
+      : ctrl_(EmptyGroup()), settings_(0, hash, eq, alloc) {
+    if (bucket_count) {
+      capacity_ = NormalizeCapacity(bucket_count);
+      growth_left() = static_cast<size_t>(capacity_ * kMaxLoadFactor);
+      initialize_slots();
+    }
+  }
+
+  raw_hash_set(size_t bucket_count, const hasher& hash,
+               const allocator_type& alloc)
+      : raw_hash_set(bucket_count, hash, key_equal(), alloc) {}
+
+  raw_hash_set(size_t bucket_count, const allocator_type& alloc)
+      : raw_hash_set(bucket_count, hasher(), key_equal(), alloc) {}
+
+  explicit raw_hash_set(const allocator_type& alloc)
+      : raw_hash_set(0, hasher(), key_equal(), alloc) {}
+
+  template <class InputIter>
+  raw_hash_set(InputIter first, InputIter last, size_t bucket_count = 0,
+               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
+               const allocator_type& alloc = allocator_type())
+      : raw_hash_set(bucket_count, hash, eq, alloc) {
+    insert(first, last);
+  }
+
+  template <class InputIter>
+  raw_hash_set(InputIter first, InputIter last, size_t bucket_count,
+               const hasher& hash, const allocator_type& alloc)
+      : raw_hash_set(first, last, bucket_count, hash, key_equal(), alloc) {}
+
+  template <class InputIter>
+  raw_hash_set(InputIter first, InputIter last, size_t bucket_count,
+               const allocator_type& alloc)
+      : raw_hash_set(first, last, bucket_count, hasher(), key_equal(), alloc) {}
+
+  template <class InputIter>
+  raw_hash_set(InputIter first, InputIter last, const allocator_type& alloc)
+      : raw_hash_set(first, last, 0, hasher(), key_equal(), alloc) {}
+
+  // Instead of accepting std::initializer_list<value_type> as the first
+  // argument like std::unordered_set<value_type> does, we have two overloads
+  // that accept std::initializer_list<T> and std::initializer_list<init_type>.
+  // This is advantageous for performance.
+  //
+  //   // Turns {"abc", "def"} into std::initializer_list<std::string>, then copies
+  //   // the strings into the set.
+  //   std::unordered_set<std::string> s = {"abc", "def"};
+  //
+  //   // Turns {"abc", "def"} into std::initializer_list<const char*>, then
+  //   // copies the strings into the set.
+  //   absl::flat_hash_set<std::string> s = {"abc", "def"};
+  //
+  // The same trick is used in insert().
+  //
+  // The enabler is necessary to prevent this constructor from triggering where
+  // the copy constructor is meant to be called.
+  //
+  //   absl::flat_hash_set<int> a, b{a};
+  //
+  // RequiresNotInit<T> is a workaround for gcc prior to 7.1.
+  template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
+  raw_hash_set(std::initializer_list<T> init, size_t bucket_count = 0,
+               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
+               const allocator_type& alloc = allocator_type())
+      : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {}
+
+  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count = 0,
+               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
+               const allocator_type& alloc = allocator_type())
+      : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {}
+
+  template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
+  raw_hash_set(std::initializer_list<T> init, size_t bucket_count,
+               const hasher& hash, const allocator_type& alloc)
+      : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {}
+
+  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count,
+               const hasher& hash, const allocator_type& alloc)
+      : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {}
+
+  template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
+  raw_hash_set(std::initializer_list<T> init, size_t bucket_count,
+               const allocator_type& alloc)
+      : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {}
+
+  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count,
+               const allocator_type& alloc)
+      : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {}
+
+  template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
+  raw_hash_set(std::initializer_list<T> init, const allocator_type& alloc)
+      : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
+
+  raw_hash_set(std::initializer_list<init_type> init,
+               const allocator_type& alloc)
+      : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
+
+  raw_hash_set(const raw_hash_set& that)
+      : raw_hash_set(that, AllocTraits::select_on_container_copy_construction(
+                               that.alloc_ref())) {}
+
+  raw_hash_set(const raw_hash_set& that, const allocator_type& a)
+      : raw_hash_set(0, that.hash_ref(), that.eq_ref(), a) {
+    reserve(that.size());
+    // Because the table is guaranteed to be empty, we can do something faster
+    // than a full `insert`.
+    for (const auto& v : that) {
+      const size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, v);
+      const size_t i = find_first_non_full(hash);
+      set_ctrl(i, H2(hash));
+      emplace_at(i, v);
+    }
+    size_ = that.size();
+    growth_left() -= that.size();
+  }
+
+  raw_hash_set(raw_hash_set&& that) noexcept(
+      std::is_nothrow_copy_constructible<hasher>::value&&
+          std::is_nothrow_copy_constructible<key_equal>::value&&
+              std::is_nothrow_copy_constructible<allocator_type>::value)
+      : ctrl_(absl::exchange(that.ctrl_, EmptyGroup())),
+        slots_(absl::exchange(that.slots_, nullptr)),
+        size_(absl::exchange(that.size_, 0)),
+        capacity_(absl::exchange(that.capacity_, 0)),
+        // Hash, equality and allocator are copied instead of moved because
+        // `that` must be left valid. If Hash is std::function<Key>, moving it
+        // would create a nullptr functor that cannot be called.
+        settings_(that.settings_) {
+    // growth_left was copied above, reset the one from `that`.
+    that.growth_left() = 0;
+  }
+
+  raw_hash_set(raw_hash_set&& that, const allocator_type& a)
+      : ctrl_(EmptyGroup()),
+        slots_(nullptr),
+        size_(0),
+        capacity_(0),
+        settings_(0, that.hash_ref(), that.eq_ref(), a) {
+    if (a == that.alloc_ref()) {
+      std::swap(ctrl_, that.ctrl_);
+      std::swap(slots_, that.slots_);
+      std::swap(size_, that.size_);
+      std::swap(capacity_, that.capacity_);
+      std::swap(growth_left(), that.growth_left());
+    } else {
+      reserve(that.size());
+      // Note: this will copy elements of dense_set and unordered_set instead of
+      // moving them. This can be fixed if it ever becomes an issue.
+      for (auto& elem : that) insert(std::move(elem));
+    }
+  }
+
+  raw_hash_set& operator=(const raw_hash_set& that) {
+    raw_hash_set tmp(that,
+                     AllocTraits::propagate_on_container_copy_assignment::value
+                         ? that.alloc_ref()
+                         : alloc_ref());
+    swap(tmp);
+    return *this;
+  }
+
+  raw_hash_set& operator=(raw_hash_set&& that) noexcept(
+      absl::allocator_traits<allocator_type>::is_always_equal::value&&
+          std::is_nothrow_move_assignable<hasher>::value&&
+              std::is_nothrow_move_assignable<key_equal>::value) {
+    // TODO(sbenza): We should only use the operations from the noexcept clause
+    // to make sure we actually adhere to that contract.
+    return move_assign(
+        std::move(that),
+        typename AllocTraits::propagate_on_container_move_assignment());
+  }
+
+  ~raw_hash_set() { destroy_slots(); }
+
+  iterator begin() {
+    auto it = iterator_at(0);
+    it.skip_empty_or_deleted();
+    return it;
+  }
+  iterator end() { return {ctrl_ + capacity_}; }
+
+  const_iterator begin() const {
+    return const_cast<raw_hash_set*>(this)->begin();
+  }
+  const_iterator end() const { return const_cast<raw_hash_set*>(this)->end(); }
+  const_iterator cbegin() const { return begin(); }
+  const_iterator cend() const { return end(); }
+
+  bool empty() const { return !size(); }
+  size_t size() const { return size_; }
+  size_t capacity() const { return capacity_; }
+  size_t max_size() const { return std::numeric_limits<size_t>::max(); }
+
+  void clear() {
+    // Iterating over this container is O(bucket_count()). When bucket_count()
+    // is much greater than size(), iteration becomes prohibitively expensive.
+    // For clear() it is more important to reuse the allocated array when the
+    // container is small because allocation takes comparatively long time
+    // compared to destruction of the elements of the container. So we pick the
+    // largest bucket_count() threshold for which iteration is still fast and
+    // past that we simply deallocate the array.
+    if (capacity_ > 127) {
+      destroy_slots();
+    } else if (capacity_) {
+      for (size_t i = 0; i != capacity_; ++i) {
+        if (IsFull(ctrl_[i])) {
+          PolicyTraits::destroy(&alloc_ref(), slots_ + i);
+        }
+      }
+      size_ = 0;
+      reset_ctrl();
+      growth_left() = static_cast<size_t>(capacity_ * kMaxLoadFactor);
+    }
+    assert(empty());
+  }
+
+  // This overload kicks in when the argument is an rvalue of insertable and
+  // decomposable type other than init_type.
+  //
+  //   flat_hash_map<std::string, int> m;
+  //   m.insert(std::make_pair("abc", 42));
+  template <class T, RequiresInsertable<T> = 0,
+            typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
+            T* = nullptr>
+  std::pair<iterator, bool> insert(T&& value) {
+    return emplace(std::forward<T>(value));
+  }
+
+  // This overload kicks in when the argument is a bitfield or an lvalue of
+  // insertable and decomposable type.
+  //
+  //   union { int n : 1; };
+  //   flat_hash_set<int> s;
+  //   s.insert(n);
+  //
+  //   flat_hash_set<std::string> s;
+  //   const char* p = "hello";
+  //   s.insert(p);
+  //
+  // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
+  // RequiresInsertable<T> with RequiresInsertable<const T&>.
+  // We are hitting this bug: https://godbolt.org/g/1Vht4f.
+  template <
+      class T, RequiresInsertable<T> = 0,
+      typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
+  std::pair<iterator, bool> insert(const T& value) {
+    return emplace(value);
+  }
+
+  // This overload kicks in when the argument is an rvalue of init_type. Its
+  // purpose is to handle brace-init-list arguments.
+  //
+  //   flat_hash_set<std::string, int> s;
+  //   s.insert({"abc", 42});
+  std::pair<iterator, bool> insert(init_type&& value) {
+    return emplace(std::move(value));
+  }
+
+  template <class T, RequiresInsertable<T> = 0,
+            typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
+            T* = nullptr>
+  iterator insert(const_iterator, T&& value) {
+    return insert(std::forward<T>(value)).first;
+  }
+
+  // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
+  // RequiresInsertable<T> with RequiresInsertable<const T&>.
+  // We are hitting this bug: https://godbolt.org/g/1Vht4f.
+  template <
+      class T, RequiresInsertable<T> = 0,
+      typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
+  iterator insert(const_iterator, const T& value) {
+    return insert(value).first;
+  }
+
+  iterator insert(const_iterator, init_type&& value) {
+    return insert(std::move(value)).first;
+  }
+
+  template <class InputIt>
+  void insert(InputIt first, InputIt last) {
+    for (; first != last; ++first) insert(*first);
+  }
+
+  template <class T, RequiresNotInit<T> = 0, RequiresInsertable<const T&> = 0>
+  void insert(std::initializer_list<T> ilist) {
+    insert(ilist.begin(), ilist.end());
+  }
+
+  void insert(std::initializer_list<init_type> ilist) {
+    insert(ilist.begin(), ilist.end());
+  }
+
+  insert_return_type<iterator, node_type> insert(node_type&& node) {
+    if (!node) return {end(), false, node_type()};
+    const auto& elem = PolicyTraits::element(node.slot());
+    auto res = PolicyTraits::apply(
+        InsertSlot<false>{*this, std::move(*node.slot())}, elem);
+    if (res.second) {
+      node.reset();
+      return {res.first, true, node_type()};
+    } else {
+      return {res.first, false, std::move(node)};
+    }
+  }
+
+  iterator insert(const_iterator, node_type&& node) {
+    return insert(std::move(node)).first;
+  }
+
+  // This overload kicks in if we can deduce the key from args. This enables us
+  // to avoid constructing value_type if an entry with the same key already
+  // exists.
+  //
+  // For example:
+  //
+  //   flat_hash_map<std::string, std::string> m = {{"abc", "def"}};
+  //   // Creates no std::string copies and makes no heap allocations.
+  //   m.emplace("abc", "xyz");
+  template <class... Args, typename std::enable_if<
+                               IsDecomposable<Args...>::value, int>::type = 0>
+  std::pair<iterator, bool> emplace(Args&&... args) {
+    return PolicyTraits::apply(EmplaceDecomposable{*this},
+                               std::forward<Args>(args)...);
+  }
+
+  // This overload kicks in if we cannot deduce the key from args. It constructs
+  // value_type unconditionally and then either moves it into the table or
+  // destroys.
+  template <class... Args, typename std::enable_if<
+                               !IsDecomposable<Args...>::value, int>::type = 0>
+  std::pair<iterator, bool> emplace(Args&&... args) {
+    typename std::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type
+        raw;
+    slot_type* slot = reinterpret_cast<slot_type*>(&raw);
+
+    PolicyTraits::construct(&alloc_ref(), slot, std::forward<Args>(args)...);
+    const auto& elem = PolicyTraits::element(slot);
+    return PolicyTraits::apply(InsertSlot<true>{*this, std::move(*slot)}, elem);
+  }
+
+  template <class... Args>
+  iterator emplace_hint(const_iterator, Args&&... args) {
+    return emplace(std::forward<Args>(args)...).first;
+  }
+
+  // Extension API: support for lazy emplace.
+  //
+  // Looks up key in the table. If found, returns the iterator to the element.
+  // Otherwise calls f with one argument of type raw_hash_set::constructor. f
+  // MUST call raw_hash_set::constructor with arguments as if a
+  // raw_hash_set::value_type is constructed, otherwise the behavior is
+  // undefined.
+  //
+  // For example:
+  //
+  //   std::unordered_set<ArenaString> s;
+  //   // Makes ArenaStr even if "abc" is in the map.
+  //   s.insert(ArenaString(&arena, "abc"));
+  //
+  //   flat_hash_set<ArenaStr> s;
+  //   // Makes ArenaStr only if "abc" is not in the map.
+  //   s.lazy_emplace("abc", [&](const constructor& ctor) {
+  //     ctor(&arena, "abc");
+  //   });
+  //
+  // WARNING: This API is currently experimental. If there is a way to implement
+  // the same thing with the rest of the API, prefer that.
+  class constructor {
+    friend class raw_hash_set;
+
+   public:
+    template <class... Args>
+    void operator()(Args&&... args) const {
+      assert(*slot_);
+      PolicyTraits::construct(alloc_, *slot_, std::forward<Args>(args)...);
+      *slot_ = nullptr;
+    }
+
+   private:
+    constructor(allocator_type* a, slot_type** slot) : alloc_(a), slot_(slot) {}
+
+    allocator_type* alloc_;
+    slot_type** slot_;
+  };
+
+  template <class K = key_type, class F>
+  iterator lazy_emplace(const key_arg<K>& key, F&& f) {
+    auto res = find_or_prepare_insert(key);
+    if (res.second) {
+      slot_type* slot = slots_ + res.first;
+      std::forward<F>(f)(constructor(&alloc_ref(), &slot));
+      assert(!slot);
+    }
+    return iterator_at(res.first);
+  }
+
+  // Extension API: support for heterogeneous keys.
+  //
+  //   std::unordered_set<std::string> s;
+  //   // Turns "abc" into std::string.
+  //   s.erase("abc");
+  //
+  //   flat_hash_set<std::string> s;
+  //   // Uses "abc" directly without copying it into std::string.
+  //   s.erase("abc");
+  template <class K = key_type>
+  size_type erase(const key_arg<K>& key) {
+    auto it = find(key);
+    if (it == end()) return 0;
+    erase(it);
+    return 1;
+  }
+
+  // Erases the element pointed to by `it`.  Unlike `std::unordered_set::erase`,
+  // this method returns void to reduce algorithmic complexity to O(1).  In
+  // order to erase while iterating across a map, use the following idiom (which
+  // also works for standard containers):
+  //
+  // for (auto it = m.begin(), end = m.end(); it != end;) {
+  //   if (<pred>) {
+  //     m.erase(it++);
+  //   } else {
+  //     ++it;
+  //   }
+  // }
+  void erase(const_iterator cit) { erase(cit.inner_); }
+
+  // This overload is necessary because otherwise erase<K>(const K&) would be
+  // a better match if non-const iterator is passed as an argument.
+  void erase(iterator it) {
+    assert(it != end());
+    PolicyTraits::destroy(&alloc_ref(), it.slot_);
+    erase_meta_only(it);
+  }
+
+  iterator erase(const_iterator first, const_iterator last) {
+    while (first != last) {
+      erase(first++);
+    }
+    return last.inner_;
+  }
+
+  // Moves elements from `src` into `this`.
+  // If the element already exists in `this`, it is left unmodified in `src`.
+  template <typename H, typename E>
+  void merge(raw_hash_set<Policy, H, E, Alloc>& src) {  // NOLINT
+    assert(this != &src);
+    for (auto it = src.begin(), e = src.end(); it != e; ++it) {
+      if (PolicyTraits::apply(InsertSlot<false>{*this, std::move(*it.slot_)},
+                              PolicyTraits::element(it.slot_))
+              .second) {
+        src.erase_meta_only(it);
+      }
+    }
+  }
+
+  template <typename H, typename E>
+  void merge(raw_hash_set<Policy, H, E, Alloc>&& src) {
+    merge(src);
+  }
+
+  node_type extract(const_iterator position) {
+    node_type node(alloc_ref(), position.inner_.slot_);
+    erase_meta_only(position);
+    return node;
+  }
+
+  template <
+      class K = key_type,
+      typename std::enable_if<!std::is_same<K, iterator>::value, int>::type = 0>
+  node_type extract(const key_arg<K>& key) {
+    auto it = find(key);
+    return it == end() ? node_type() : extract(const_iterator{it});
+  }
+
+  void swap(raw_hash_set& that) noexcept(
+      IsNoThrowSwappable<hasher>() && IsNoThrowSwappable<key_equal>() &&
+      (!AllocTraits::propagate_on_container_swap::value ||
+       IsNoThrowSwappable<allocator_type>())) {
+    using std::swap;
+    swap(ctrl_, that.ctrl_);
+    swap(slots_, that.slots_);
+    swap(size_, that.size_);
+    swap(capacity_, that.capacity_);
+    swap(growth_left(), that.growth_left());
+    swap(hash_ref(), that.hash_ref());
+    swap(eq_ref(), that.eq_ref());
+    if (AllocTraits::propagate_on_container_swap::value) {
+      swap(alloc_ref(), that.alloc_ref());
+    } else {
+      // If the allocators do not compare equal it is officially undefined
+      // behavior. We choose to do nothing.
+    }
+  }
+
+  void rehash(size_t n) {
+    if (n == 0 && capacity_ == 0) return;
+    if (n == 0 && size_ == 0) return destroy_slots();
+    auto m = NormalizeCapacity(std::max(n, NumSlotsFast(size())));
+    // n == 0 unconditionally rehashes as per the standard.
+    if (n == 0 || m > capacity_) {
+      resize(m);
+    }
+  }
+
+  void reserve(size_t n) {
+    rehash(NumSlotsFast(n));
+  }
+
+  // Extension API: support for heterogeneous keys.
+  //
+  //   std::unordered_set<std::string> s;
+  //   // Turns "abc" into std::string.
+  //   s.count("abc");
+  //
+  //   ch_set<std::string> s;
+  //   // Uses "abc" directly without copying it into std::string.
+  //   s.count("abc");
+  template <class K = key_type>
+  size_t count(const key_arg<K>& key) const {
+    return find(key) == end() ? 0 : 1;
+  }
+
+  // Issues CPU prefetch instructions for the memory needed to find or insert
+  // a key.  Like all lookup functions, this support heterogeneous keys.
+  //
+  // NOTE: This is a very low level operation and should not be used without
+  // specific benchmarks indicating its importance.
+  template <class K = key_type>
+  void prefetch(const key_arg<K>& key) const {
+    (void)key;
+#if defined(__GNUC__)
+    auto seq = probe(hash_ref()(key));
+    __builtin_prefetch(static_cast<const void*>(ctrl_ + seq.offset()));
+    __builtin_prefetch(static_cast<const void*>(slots_ + seq.offset()));
+#endif  // __GNUC__
+  }
+
+  // The API of find() has two extensions.
+  //
+  // 1. The hash can be passed by the user. It must be equal to the hash of the
+  // key.
+  //
+  // 2. The type of the key argument doesn't have to be key_type. This is so
+  // called heterogeneous key support.
+  template <class K = key_type>
+  iterator find(const key_arg<K>& key, size_t hash) {
+    auto seq = probe(hash);
+    while (true) {
+      Group g{ctrl_ + seq.offset()};
+      for (int i : g.Match(H2(hash))) {
+        if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
+                EqualElement<K>{key, eq_ref()},
+                PolicyTraits::element(slots_ + seq.offset(i)))))
+          return iterator_at(seq.offset(i));
+      }
+      if (ABSL_PREDICT_TRUE(g.MatchEmpty())) return end();
+      seq.next();
+    }
+  }
+  template <class K = key_type>
+  iterator find(const key_arg<K>& key) {
+    return find(key, hash_ref()(key));
+  }
+
+  template <class K = key_type>
+  const_iterator find(const key_arg<K>& key, size_t hash) const {
+    return const_cast<raw_hash_set*>(this)->find(key, hash);
+  }
+  template <class K = key_type>
+  const_iterator find(const key_arg<K>& key) const {
+    return find(key, hash_ref()(key));
+  }
+
+  template <class K = key_type>
+  bool contains(const key_arg<K>& key) const {
+    return find(key) != end();
+  }
+
+  template <class K = key_type>
+  std::pair<iterator, iterator> equal_range(const key_arg<K>& key) {
+    auto it = find(key);
+    if (it != end()) return {it, std::next(it)};
+    return {it, it};
+  }
+  template <class K = key_type>
+  std::pair<const_iterator, const_iterator> equal_range(
+      const key_arg<K>& key) const {
+    auto it = find(key);
+    if (it != end()) return {it, std::next(it)};
+    return {it, it};
+  }
+
+  size_t bucket_count() const { return capacity_; }
+  float load_factor() const {
+    return capacity_ ? static_cast<double>(size()) / capacity_ : 0.0;
+  }
+  float max_load_factor() const { return 1.0f; }
+  void max_load_factor(float) {
+    // Does nothing.
+  }
+
+  hasher hash_function() const { return hash_ref(); }
+  key_equal key_eq() const { return eq_ref(); }
+  allocator_type get_allocator() const { return alloc_ref(); }
+
+  friend bool operator==(const raw_hash_set& a, const raw_hash_set& b) {
+    if (a.size() != b.size()) return false;
+    const raw_hash_set* outer = &a;
+    const raw_hash_set* inner = &b;
+    if (outer->capacity() > inner->capacity()) std::swap(outer, inner);
+    for (const value_type& elem : *outer)
+      if (!inner->has_element(elem)) return false;
+    return true;
+  }
+
+  friend bool operator!=(const raw_hash_set& a, const raw_hash_set& b) {
+    return !(a == b);
+  }
+
+  friend void swap(raw_hash_set& a,
+                   raw_hash_set& b) noexcept(noexcept(a.swap(b))) {
+    a.swap(b);
+  }
+
+ private:
+  template <class Container, typename Enabler>
+  friend struct absl::container_internal::hashtable_debug_internal::
+      HashtableDebugAccess;
+
+  struct FindElement {
+    template <class K, class... Args>
+    const_iterator operator()(const K& key, Args&&...) const {
+      return s.find(key);
+    }
+    const raw_hash_set& s;
+  };
+
+  struct HashElement {
+    template <class K, class... Args>
+    size_t operator()(const K& key, Args&&...) const {
+      return h(key);
+    }
+    const hasher& h;
+  };
+
+  template <class K1>
+  struct EqualElement {
+    template <class K2, class... Args>
+    bool operator()(const K2& lhs, Args&&...) const {
+      return eq(lhs, rhs);
+    }
+    const K1& rhs;
+    const key_equal& eq;
+  };
+
+  struct EmplaceDecomposable {
+    template <class K, class... Args>
+    std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
+      auto res = s.find_or_prepare_insert(key);
+      if (res.second) {
+        s.emplace_at(res.first, std::forward<Args>(args)...);
+      }
+      return {s.iterator_at(res.first), res.second};
+    }
+    raw_hash_set& s;
+  };
+
+  template <bool do_destroy>
+  struct InsertSlot {
+    template <class K, class... Args>
+    std::pair<iterator, bool> operator()(const K& key, Args&&...) && {
+      auto res = s.find_or_prepare_insert(key);
+      if (res.second) {
+        PolicyTraits::transfer(&s.alloc_ref(), s.slots_ + res.first, &slot);
+      } else if (do_destroy) {
+        PolicyTraits::destroy(&s.alloc_ref(), &slot);
+      }
+      return {s.iterator_at(res.first), res.second};
+    }
+    raw_hash_set& s;
+    // Constructed slot. Either moved into place or destroyed.
+    slot_type&& slot;
+  };
+
+  // Computes std::ceil(n / kMaxLoadFactor). Faster than calling std::ceil.
+  static inline size_t NumSlotsFast(size_t n) {
+    return static_cast<size_t>(
+        (n * kMaxLoadFactorDenominator + (kMaxLoadFactorNumerator - 1)) /
+        kMaxLoadFactorNumerator);
+  }
+
+  // "erases" the object from the container, except that it doesn't actually
+  // destroy the object. It only updates all the metadata of the class.
+  // This can be used in conjunction with Policy::transfer to move the object to
+  // another place.
+  void erase_meta_only(const_iterator it) {
+    assert(IsFull(*it.inner_.ctrl_) && "erasing a dangling iterator");
+    --size_;
+    const size_t index = it.inner_.ctrl_ - ctrl_;
+    const size_t index_before = (index - Group::kWidth) & capacity_;
+    const auto empty_after = Group(it.inner_.ctrl_).MatchEmpty();
+    const auto empty_before = Group(ctrl_ + index_before).MatchEmpty();
+
+    // We count how many consecutive non empties we have to the right and to the
+    // left of `it`. If the sum is >= kWidth then there is at least one probe
+    // window that might have seen a full group.
+    bool was_never_full =
+        empty_before && empty_after &&
+        static_cast<size_t>(empty_after.TrailingZeros() +
+                            empty_before.LeadingZeros()) < Group::kWidth;
+
+    set_ctrl(index, was_never_full ? kEmpty : kDeleted);
+    growth_left() += was_never_full;
+  }
+
+  void initialize_slots() {
+    assert(capacity_);
+    auto layout = MakeLayout(capacity_);
+    char* mem = static_cast<char*>(
+        Allocate<Layout::Alignment()>(&alloc_ref(), layout.AllocSize()));
+    ctrl_ = reinterpret_cast<ctrl_t*>(layout.template Pointer<0>(mem));
+    slots_ = layout.template Pointer<1>(mem);
+    reset_ctrl();
+    growth_left() = static_cast<size_t>(capacity_ * kMaxLoadFactor) - size_;
+  }
+
+  void destroy_slots() {
+    if (!capacity_) return;
+    for (size_t i = 0; i != capacity_; ++i) {
+      if (IsFull(ctrl_[i])) {
+        PolicyTraits::destroy(&alloc_ref(), slots_ + i);
+      }
+    }
+    auto layout = MakeLayout(capacity_);
+    // Unpoison before returning the memory to the allocator.
+    SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
+    Deallocate<Layout::Alignment()>(&alloc_ref(), ctrl_, layout.AllocSize());
+    ctrl_ = EmptyGroup();
+    slots_ = nullptr;
+    size_ = 0;
+    capacity_ = 0;
+    growth_left() = 0;
+  }
+
+  void resize(size_t new_capacity) {
+    assert(IsValidCapacity(new_capacity));
+    auto* old_ctrl = ctrl_;
+    auto* old_slots = slots_;
+    const size_t old_capacity = capacity_;
+    capacity_ = new_capacity;
+    initialize_slots();
+
+    for (size_t i = 0; i != old_capacity; ++i) {
+      if (IsFull(old_ctrl[i])) {
+        size_t hash = PolicyTraits::apply(HashElement{hash_ref()},
+                                          PolicyTraits::element(old_slots + i));
+        size_t new_i = find_first_non_full(hash);
+        set_ctrl(new_i, H2(hash));
+        PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, old_slots + i);
+      }
+    }
+    if (old_capacity) {
+      SanitizerUnpoisonMemoryRegion(old_slots,
+                                    sizeof(slot_type) * old_capacity);
+      auto layout = MakeLayout(old_capacity);
+      Deallocate<Layout::Alignment()>(&alloc_ref(), old_ctrl,
+                                      layout.AllocSize());
+    }
+  }
+
+  void drop_deletes_without_resize() ABSL_ATTRIBUTE_NOINLINE {
+    assert(IsValidCapacity(capacity_));
+    // Algorithm:
+    // - mark all DELETED slots as EMPTY
+    // - mark all FULL slots as DELETED
+    // - for each slot marked as DELETED
+    //     hash = Hash(element)
+    //     target = find_first_non_full(hash)
+    //     if target is in the same group
+    //       mark slot as FULL
+    //     else if target is EMPTY
+    //       transfer element to target
+    //       mark slot as EMPTY
+    //       mark target as FULL
+    //     else if target is DELETED
+    //       swap current element with target element
+    //       mark target as FULL
+    //       repeat procedure for current slot with moved from element (target)
+    ConvertDeletedToEmptyAndFullToDeleted(ctrl_, capacity_);
+    typename std::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type
+        raw;
+    slot_type* slot = reinterpret_cast<slot_type*>(&raw);
+    for (size_t i = 0; i != capacity_; ++i) {
+      if (!IsDeleted(ctrl_[i])) continue;
+      size_t hash = PolicyTraits::apply(HashElement{hash_ref()},
+                                        PolicyTraits::element(slots_ + i));
+      size_t new_i = find_first_non_full(hash);
+
+      // Verify if the old and new i fall within the same group wrt the hash.
+      // If they do, we don't need to move the object as it falls already in the
+      // best probe we can.
+      const auto probe_index = [&](size_t pos) {
+        return ((pos - probe(hash).offset()) & capacity_) / Group::kWidth;
+      };
+
+      // Element doesn't move.
+      if (ABSL_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
+        set_ctrl(i, H2(hash));
+        continue;
+      }
+      if (IsEmpty(ctrl_[new_i])) {
+        // Transfer element to the empty spot.
+        // set_ctrl poisons/unpoisons the slots so we have to call it at the
+        // right time.
+        set_ctrl(new_i, H2(hash));
+        PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slots_ + i);
+        set_ctrl(i, kEmpty);
+      } else {
+        assert(IsDeleted(ctrl_[new_i]));
+        set_ctrl(new_i, H2(hash));
+        // Until we are done rehashing, DELETED marks previously FULL slots.
+        // Swap i and new_i elements.
+        PolicyTraits::transfer(&alloc_ref(), slot, slots_ + i);
+        PolicyTraits::transfer(&alloc_ref(), slots_ + i, slots_ + new_i);
+        PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slot);
+        --i;  // repeat
+      }
+    }
+    growth_left() = static_cast<size_t>(capacity_ * kMaxLoadFactor) - size_;
+  }
+
+  void rehash_and_grow_if_necessary() {
+    if (capacity_ == 0) {
+      resize(Group::kWidth - 1);
+    } else if (size() <= kMaxLoadFactor / 2 * capacity_) {
+      // Squash DELETED without growing if there is enough capacity.
+      drop_deletes_without_resize();
+    } else {
+      // Otherwise grow the container.
+      resize(capacity_ * 2 + 1);
+    }
+  }
+
+  bool has_element(const value_type& elem) const {
+    size_t hash = PolicyTraits::apply(HashElement{hash_ref()}, elem);
+    auto seq = probe(hash);
+    while (true) {
+      Group g{ctrl_ + seq.offset()};
+      for (int i : g.Match(H2(hash))) {
+        if (ABSL_PREDICT_TRUE(PolicyTraits::element(slots_ + seq.offset(i)) ==
+                              elem))
+          return true;
+      }
+      if (ABSL_PREDICT_TRUE(g.MatchEmpty())) return false;
+      seq.next();
+      assert(seq.index() < capacity_ && "full table!");
+    }
+    return false;
+  }
+
+  // Probes the raw_hash_set with the probe sequence for hash and returns the
+  // pointer to the first empty or deleted slot.
+  // NOTE: this function must work with tables having both kEmpty and kDelete
+  // in one group. Such tables appears during drop_deletes_without_resize.
+  //
+  // This function is very useful when insertions happen and:
+  // - the input is already a set
+  // - there are enough slots
+  // - the element with the hash is not in the table
+  size_t find_first_non_full(size_t hash) {
+    auto seq = probe(hash);
+    while (true) {
+      Group g{ctrl_ + seq.offset()};
+      auto mask = g.MatchEmptyOrDeleted();
+      if (mask) {
+#if !defined(NDEBUG)
+        // We want to force small tables to have random entries too, so
+        // in debug build we will randomly insert in either the front or back of
+        // the group.
+        // TODO(kfm,sbenza): revisit after we do unconditional mixing
+        if (ShouldInsertBackwards(hash, ctrl_))
+          return seq.offset(mask.HighestBitSet());
+        else
+          return seq.offset(mask.LowestBitSet());
+#else
+        return seq.offset(mask.LowestBitSet());
+#endif
+      }
+      assert(seq.index() < capacity_ && "full table!");
+      seq.next();
+    }
+  }
+
+  // TODO(alkis): Optimize this assuming *this and that don't overlap.
+  raw_hash_set& move_assign(raw_hash_set&& that, std::true_type) {
+    raw_hash_set tmp(std::move(that));
+    swap(tmp);
+    return *this;
+  }
+  raw_hash_set& move_assign(raw_hash_set&& that, std::false_type) {
+    raw_hash_set tmp(std::move(that), alloc_ref());
+    swap(tmp);
+    return *this;
+  }
+
+ protected:
+  template <class K>
+  std::pair<size_t, bool> find_or_prepare_insert(const K& key) {
+    auto hash = hash_ref()(key);
+    auto seq = probe(hash);
+    while (true) {
+      Group g{ctrl_ + seq.offset()};
+      for (int i : g.Match(H2(hash))) {
+        if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
+                EqualElement<K>{key, eq_ref()},
+                PolicyTraits::element(slots_ + seq.offset(i)))))
+          return {seq.offset(i), false};
+      }
+      if (ABSL_PREDICT_TRUE(g.MatchEmpty())) break;
+      seq.next();
+    }
+    return {prepare_insert(hash), true};
+  }
+
+  size_t prepare_insert(size_t hash) ABSL_ATTRIBUTE_NOINLINE {
+    size_t target = find_first_non_full(hash);
+    if (ABSL_PREDICT_FALSE(growth_left() == 0 && !IsDeleted(ctrl_[target]))) {
+      rehash_and_grow_if_necessary();
+      target = find_first_non_full(hash);
+    }
+    ++size_;
+    growth_left() -= IsEmpty(ctrl_[target]);
+    set_ctrl(target, H2(hash));
+    return target;
+  }
+
+  // Constructs the value in the space pointed by the iterator. This only works
+  // after an unsuccessful find_or_prepare_insert() and before any other
+  // modifications happen in the raw_hash_set.
+  //
+  // PRECONDITION: i is an index returned from find_or_prepare_insert(k), where
+  // k is the key decomposed from `forward<Args>(args)...`, and the bool
+  // returned by find_or_prepare_insert(k) was true.
+  // POSTCONDITION: *m.iterator_at(i) == value_type(forward<Args>(args)...).
+  template <class... Args>
+  void emplace_at(size_t i, Args&&... args) {
+    PolicyTraits::construct(&alloc_ref(), slots_ + i,
+                            std::forward<Args>(args)...);
+
+    assert(PolicyTraits::apply(FindElement{*this}, *iterator_at(i)) ==
+               iterator_at(i) &&
+           "constructed value does not match the lookup key");
+  }
+
+  iterator iterator_at(size_t i) { return {ctrl_ + i, slots_ + i}; }
+  const_iterator iterator_at(size_t i) const { return {ctrl_ + i, slots_ + i}; }
+
+ private:
+  friend struct RawHashSetTestOnlyAccess;
+
+  probe_seq<Group::kWidth> probe(size_t hash) const {
+    return probe_seq<Group::kWidth>(H1(hash, ctrl_), capacity_);
+  }
+
+  // Reset all ctrl bytes back to kEmpty, except the sentinel.
+  void reset_ctrl() {
+    std::memset(ctrl_, kEmpty, capacity_ + Group::kWidth);
+    ctrl_[capacity_] = kSentinel;
+    SanitizerPoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
+  }
+
+  // Sets the control byte, and if `i < Group::kWidth`, set the cloned byte at
+  // the end too.
+  void set_ctrl(size_t i, ctrl_t h) {
+    assert(i < capacity_);
+
+    if (IsFull(h)) {
+      SanitizerUnpoisonObject(slots_ + i);
+    } else {
+      SanitizerPoisonObject(slots_ + i);
+    }
+
+    ctrl_[i] = h;
+    ctrl_[((i - Group::kWidth) & capacity_) + Group::kWidth] = h;
+  }
+
+  size_t& growth_left() { return settings_.template get<0>(); }
+
+  hasher& hash_ref() { return settings_.template get<1>(); }
+  const hasher& hash_ref() const { return settings_.template get<1>(); }
+  key_equal& eq_ref() { return settings_.template get<2>(); }
+  const key_equal& eq_ref() const { return settings_.template get<2>(); }
+  allocator_type& alloc_ref() { return settings_.template get<3>(); }
+  const allocator_type& alloc_ref() const {
+    return settings_.template get<3>();
+  }
+
+  // On average each group has 2 empty slot (for the vectorized case).
+  static constexpr int64_t kMaxLoadFactorNumerator = 14;
+  static constexpr int64_t kMaxLoadFactorDenominator = 16;
+  static constexpr float kMaxLoadFactor =
+      1.0 * kMaxLoadFactorNumerator / kMaxLoadFactorDenominator;
+
+  // TODO(alkis): Investigate removing some of these fields:
+  // - ctrl/slots can be derived from each other
+  // - size can be moved into the slot array
+  ctrl_t* ctrl_ = EmptyGroup();    // [(capacity + 1) * ctrl_t]
+  slot_type* slots_ = nullptr;     // [capacity * slot_type]
+  size_t size_ = 0;                // number of full slots
+  size_t capacity_ = 0;            // total number of slots
+  absl::container_internal::CompressedTuple<size_t /* growth_left */, hasher,
+                                            key_equal, allocator_type>
+      settings_{0, hasher{}, key_equal{}, allocator_type{}};
+};
+
+namespace hashtable_debug_internal {
+template <typename Set>
+struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
+  using Traits = typename Set::PolicyTraits;
+  using Slot = typename Traits::slot_type;
+
+  static size_t GetNumProbes(const Set& set,
+                             const typename Set::key_type& key) {
+    size_t num_probes = 0;
+    size_t hash = set.hash_ref()(key);
+    auto seq = set.probe(hash);
+    while (true) {
+      container_internal::Group g{set.ctrl_ + seq.offset()};
+      for (int i : g.Match(container_internal::H2(hash))) {
+        if (Traits::apply(
+                typename Set::template EqualElement<typename Set::key_type>{
+                    key, set.eq_ref()},
+                Traits::element(set.slots_ + seq.offset(i))))
+          return num_probes;
+        ++num_probes;
+      }
+      if (g.MatchEmpty()) return num_probes;
+      seq.next();
+      ++num_probes;
+    }
+  }
+
+  static size_t AllocatedByteSize(const Set& c) {
+    size_t capacity = c.capacity_;
+    if (capacity == 0) return 0;
+    auto layout = Set::MakeLayout(capacity);
+    size_t m = layout.AllocSize();
+
+    size_t per_slot = Traits::space_used(static_cast<const Slot*>(nullptr));
+    if (per_slot != ~size_t{}) {
+      m += per_slot * c.size();
+    } else {
+      for (size_t i = 0; i != capacity; ++i) {
+        if (container_internal::IsFull(c.ctrl_[i])) {
+          m += Traits::space_used(c.slots_ + i);
+        }
+      }
+    }
+    return m;
+  }
+
+  static size_t LowerBoundAllocatedByteSize(size_t size) {
+    size_t capacity = container_internal::NormalizeCapacity(
+        std::ceil(size / Set::kMaxLoadFactor));
+    if (capacity == 0) return 0;
+    auto layout = Set::MakeLayout(capacity);
+    size_t m = layout.AllocSize();
+    size_t per_slot = Traits::space_used(static_cast<const Slot*>(nullptr));
+    if (per_slot != ~size_t{}) {
+      m += per_slot * size;
+    }
+    return m;
+  }
+};
+
+}  // namespace hashtable_debug_internal
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_
diff --git a/absl/container/internal/raw_hash_set_allocator_test.cc b/absl/container/internal/raw_hash_set_allocator_test.cc
new file mode 100644
index 0000000..891fa45
--- /dev/null
+++ b/absl/container/internal/raw_hash_set_allocator_test.cc
@@ -0,0 +1,428 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <limits>
+#include <scoped_allocator>
+
+#include "gtest/gtest.h"
+#include "absl/container/internal/raw_hash_set.h"
+#include "absl/container/internal/tracked.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+enum AllocSpec {
+  kPropagateOnCopy = 1,
+  kPropagateOnMove = 2,
+  kPropagateOnSwap = 4,
+};
+
+struct AllocState {
+  size_t num_allocs = 0;
+  std::set<void*> owned;
+};
+
+template <class T,
+          int Spec = kPropagateOnCopy | kPropagateOnMove | kPropagateOnSwap>
+class CheckedAlloc {
+ public:
+  template <class, int>
+  friend class CheckedAlloc;
+
+  using value_type = T;
+
+  CheckedAlloc() {}
+  explicit CheckedAlloc(size_t id) : id_(id) {}
+  CheckedAlloc(const CheckedAlloc&) = default;
+  CheckedAlloc& operator=(const CheckedAlloc&) = default;
+
+  template <class U>
+  CheckedAlloc(const CheckedAlloc<U, Spec>& that)
+      : id_(that.id_), state_(that.state_) {}
+
+  template <class U>
+  struct rebind {
+    using other = CheckedAlloc<U, Spec>;
+  };
+
+  using propagate_on_container_copy_assignment =
+      std::integral_constant<bool, (Spec & kPropagateOnCopy) != 0>;
+
+  using propagate_on_container_move_assignment =
+      std::integral_constant<bool, (Spec & kPropagateOnMove) != 0>;
+
+  using propagate_on_container_swap =
+      std::integral_constant<bool, (Spec & kPropagateOnSwap) != 0>;
+
+  CheckedAlloc select_on_container_copy_construction() const {
+    if (Spec & kPropagateOnCopy) return *this;
+    return {};
+  }
+
+  T* allocate(size_t n) {
+    T* ptr = std::allocator<T>().allocate(n);
+    track_alloc(ptr);
+    return ptr;
+  }
+  void deallocate(T* ptr, size_t n) {
+    memset(ptr, 0, n * sizeof(T));  // The freed memory must be unpoisoned.
+    track_dealloc(ptr);
+    return std::allocator<T>().deallocate(ptr, n);
+  }
+
+  friend bool operator==(const CheckedAlloc& a, const CheckedAlloc& b) {
+    return a.id_ == b.id_;
+  }
+  friend bool operator!=(const CheckedAlloc& a, const CheckedAlloc& b) {
+    return !(a == b);
+  }
+
+  size_t num_allocs() const { return state_->num_allocs; }
+
+  void swap(CheckedAlloc& that) {
+    using std::swap;
+    swap(id_, that.id_);
+    swap(state_, that.state_);
+  }
+
+  friend void swap(CheckedAlloc& a, CheckedAlloc& b) { a.swap(b); }
+
+  friend std::ostream& operator<<(std::ostream& o, const CheckedAlloc& a) {
+    return o << "alloc(" << a.id_ << ")";
+  }
+
+ private:
+  void track_alloc(void* ptr) {
+    AllocState* state = state_.get();
+    ++state->num_allocs;
+    if (!state->owned.insert(ptr).second)
+      ADD_FAILURE() << *this << " got previously allocated memory: " << ptr;
+  }
+  void track_dealloc(void* ptr) {
+    if (state_->owned.erase(ptr) != 1)
+      ADD_FAILURE() << *this
+                    << " deleting memory owned by another allocator: " << ptr;
+  }
+
+  size_t id_ = std::numeric_limits<size_t>::max();
+
+  std::shared_ptr<AllocState> state_ = std::make_shared<AllocState>();
+};
+
+struct Identity {
+  int32_t operator()(int32_t v) const { return v; }
+};
+
+struct Policy {
+  using slot_type = Tracked<int32_t>;
+  using init_type = Tracked<int32_t>;
+  using key_type = int32_t;
+
+  template <class allocator_type, class... Args>
+  static void construct(allocator_type* alloc, slot_type* slot,
+                        Args&&... args) {
+    std::allocator_traits<allocator_type>::construct(
+        *alloc, slot, std::forward<Args>(args)...);
+  }
+
+  template <class allocator_type>
+  static void destroy(allocator_type* alloc, slot_type* slot) {
+    std::allocator_traits<allocator_type>::destroy(*alloc, slot);
+  }
+
+  template <class allocator_type>
+  static void transfer(allocator_type* alloc, slot_type* new_slot,
+                       slot_type* old_slot) {
+    construct(alloc, new_slot, std::move(*old_slot));
+    destroy(alloc, old_slot);
+  }
+
+  template <class F>
+  static auto apply(F&& f, int32_t v) -> decltype(std::forward<F>(f)(v, v)) {
+    return std::forward<F>(f)(v, v);
+  }
+
+  template <class F>
+  static auto apply(F&& f, const slot_type& v)
+      -> decltype(std::forward<F>(f)(v.val(), v)) {
+    return std::forward<F>(f)(v.val(), v);
+  }
+
+  template <class F>
+  static auto apply(F&& f, slot_type&& v)
+      -> decltype(std::forward<F>(f)(v.val(), std::move(v))) {
+    return std::forward<F>(f)(v.val(), std::move(v));
+  }
+
+  static slot_type& element(slot_type* slot) { return *slot; }
+};
+
+template <int Spec>
+struct PropagateTest : public ::testing::Test {
+  using Alloc = CheckedAlloc<Tracked<int32_t>, Spec>;
+
+  using Table = raw_hash_set<Policy, Identity, std::equal_to<int32_t>, Alloc>;
+
+  PropagateTest() {
+    EXPECT_EQ(a1, t1.get_allocator());
+    EXPECT_NE(a2, t1.get_allocator());
+  }
+
+  Alloc a1 = Alloc(1);
+  Table t1 = Table(0, a1);
+  Alloc a2 = Alloc(2);
+};
+
+using PropagateOnAll =
+    PropagateTest<kPropagateOnCopy | kPropagateOnMove | kPropagateOnSwap>;
+using NoPropagateOnCopy = PropagateTest<kPropagateOnMove | kPropagateOnSwap>;
+using NoPropagateOnMove = PropagateTest<kPropagateOnCopy | kPropagateOnSwap>;
+
+TEST_F(PropagateOnAll, Empty) { EXPECT_EQ(0, a1.num_allocs()); }
+
+TEST_F(PropagateOnAll, InsertAllocates) {
+  auto it = t1.insert(0).first;
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, InsertDecomposes) {
+  auto it = t1.insert(0).first;
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+
+  EXPECT_FALSE(t1.insert(0).second);
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, RehashMoves) {
+  auto it = t1.insert(0).first;
+  EXPECT_EQ(0, it->num_moves());
+  t1.rehash(2 * t1.capacity());
+  EXPECT_EQ(2, a1.num_allocs());
+  it = t1.find(0);
+  EXPECT_EQ(1, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, CopyConstructor) {
+  auto it = t1.insert(0).first;
+  Table u(t1);
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(NoPropagateOnCopy, CopyConstructor) {
+  auto it = t1.insert(0).first;
+  Table u(t1);
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, u.get_allocator().num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, CopyConstructorWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(t1, a1);
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(NoPropagateOnCopy, CopyConstructorWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(t1, a1);
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, CopyConstructorWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(t1, a2);
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(NoPropagateOnCopy, CopyConstructorWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(t1, a2);
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, MoveConstructor) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1));
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(NoPropagateOnMove, MoveConstructor) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1));
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, MoveConstructorWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1), a1);
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(NoPropagateOnMove, MoveConstructorWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1), a1);
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, MoveConstructorWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1), a2);
+  it = u.find(0);
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(1, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(NoPropagateOnMove, MoveConstructorWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(std::move(t1), a2);
+  it = u.find(0);
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(1, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, CopyAssignmentWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a1);
+  u = t1;
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(NoPropagateOnCopy, CopyAssignmentWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a1);
+  u = t1;
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, CopyAssignmentWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a2);
+  u = t1;
+  EXPECT_EQ(a1, u.get_allocator());
+  EXPECT_EQ(2, a1.num_allocs());
+  EXPECT_EQ(0, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(NoPropagateOnCopy, CopyAssignmentWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a2);
+  u = t1;
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(1, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, MoveAssignmentWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a1);
+  u = std::move(t1);
+  EXPECT_EQ(a1, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(NoPropagateOnMove, MoveAssignmentWithSameAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a1);
+  u = std::move(t1);
+  EXPECT_EQ(a1, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, MoveAssignmentWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a2);
+  u = std::move(t1);
+  EXPECT_EQ(a1, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(NoPropagateOnMove, MoveAssignmentWithDifferentAlloc) {
+  auto it = t1.insert(0).first;
+  Table u(0, a2);
+  u = std::move(t1);
+  it = u.find(0);
+  EXPECT_EQ(a2, u.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(1, a2.num_allocs());
+  EXPECT_EQ(1, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+TEST_F(PropagateOnAll, Swap) {
+  auto it = t1.insert(0).first;
+  Table u(0, a2);
+  u.swap(t1);
+  EXPECT_EQ(a1, u.get_allocator());
+  EXPECT_EQ(a2, t1.get_allocator());
+  EXPECT_EQ(1, a1.num_allocs());
+  EXPECT_EQ(0, a2.num_allocs());
+  EXPECT_EQ(0, it->num_moves());
+  EXPECT_EQ(0, it->num_copies());
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/raw_hash_set_test.cc b/absl/container/internal/raw_hash_set_test.cc
new file mode 100644
index 0000000..cd33a3a
--- /dev/null
+++ b/absl/container/internal/raw_hash_set_test.cc
@@ -0,0 +1,1961 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/internal/raw_hash_set.h"
+
+#include <array>
+#include <cmath>
+#include <cstdint>
+#include <deque>
+#include <functional>
+#include <memory>
+#include <numeric>
+#include <random>
+#include <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/base/attributes.h"
+#include "absl/base/internal/cycleclock.h"
+#include "absl/base/internal/raw_logging.h"
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/hash_function_defaults.h"
+#include "absl/container/internal/hash_policy_testing.h"
+#include "absl/container/internal/hashtable_debug.h"
+#include "absl/strings/string_view.h"
+
+namespace absl {
+namespace container_internal {
+
+struct RawHashSetTestOnlyAccess {
+  template <typename C>
+  static auto GetSlots(const C& c) -> decltype(c.slots_) {
+    return c.slots_;
+  }
+};
+
+namespace {
+
+using ::testing::DoubleNear;
+using ::testing::ElementsAre;
+using ::testing::Optional;
+using ::testing::Pair;
+using ::testing::UnorderedElementsAre;
+
+TEST(Util, NormalizeCapacity) {
+  constexpr size_t kMinCapacity = Group::kWidth - 1;
+  EXPECT_EQ(kMinCapacity, NormalizeCapacity(0));
+  EXPECT_EQ(kMinCapacity, NormalizeCapacity(1));
+  EXPECT_EQ(kMinCapacity, NormalizeCapacity(2));
+  EXPECT_EQ(kMinCapacity, NormalizeCapacity(kMinCapacity));
+  EXPECT_EQ(kMinCapacity * 2 + 1, NormalizeCapacity(kMinCapacity + 1));
+  EXPECT_EQ(kMinCapacity * 2 + 1, NormalizeCapacity(kMinCapacity + 2));
+}
+
+TEST(Util, probe_seq) {
+  probe_seq<16> seq(0, 127);
+  auto gen = [&]() {
+    size_t res = seq.offset();
+    seq.next();
+    return res;
+  };
+  std::vector<size_t> offsets(8);
+  std::generate_n(offsets.begin(), 8, gen);
+  EXPECT_THAT(offsets, ElementsAre(0, 16, 48, 96, 32, 112, 80, 64));
+  seq = probe_seq<16>(128, 127);
+  std::generate_n(offsets.begin(), 8, gen);
+  EXPECT_THAT(offsets, ElementsAre(0, 16, 48, 96, 32, 112, 80, 64));
+}
+
+TEST(BitMask, Smoke) {
+  EXPECT_FALSE((BitMask<uint8_t, 8>(0)));
+  EXPECT_TRUE((BitMask<uint8_t, 8>(5)));
+
+  EXPECT_THAT((BitMask<uint8_t, 8>(0)), ElementsAre());
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x1)), ElementsAre(0));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x2)), ElementsAre(1));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x3)), ElementsAre(0, 1));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x4)), ElementsAre(2));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x5)), ElementsAre(0, 2));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0x55)), ElementsAre(0, 2, 4, 6));
+  EXPECT_THAT((BitMask<uint8_t, 8>(0xAA)), ElementsAre(1, 3, 5, 7));
+}
+
+TEST(BitMask, WithShift) {
+  // See the non-SSE version of Group for details on what this math is for.
+  uint64_t ctrl = 0x1716151413121110;
+  uint64_t hash = 0x12;
+  constexpr uint64_t msbs = 0x8080808080808080ULL;
+  constexpr uint64_t lsbs = 0x0101010101010101ULL;
+  auto x = ctrl ^ (lsbs * hash);
+  uint64_t mask = (x - lsbs) & ~x & msbs;
+  EXPECT_EQ(0x0000000080800000, mask);
+
+  BitMask<uint64_t, 8, 3> b(mask);
+  EXPECT_EQ(*b, 2);
+}
+
+TEST(BitMask, LeadingTrailing) {
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b0001101001000000).LeadingZeros()), 3);
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b0001101001000000).TrailingZeros()), 6);
+
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b0000000000000001).LeadingZeros()), 15);
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b0000000000000001).TrailingZeros()), 0);
+
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b1000000000000000).LeadingZeros()), 0);
+  EXPECT_EQ((BitMask<uint32_t, 16>(0b1000000000000000).TrailingZeros()), 15);
+
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x0000008080808000).LeadingZeros()), 3);
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x0000008080808000).TrailingZeros()), 1);
+
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x0000000000000080).LeadingZeros()), 7);
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x0000000000000080).TrailingZeros()), 0);
+
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x8000000000000000).LeadingZeros()), 0);
+  EXPECT_EQ((BitMask<uint64_t, 8, 3>(0x8000000000000000).TrailingZeros()), 7);
+}
+
+TEST(Group, EmptyGroup) {
+  for (h2_t h = 0; h != 128; ++h) EXPECT_FALSE(Group{EmptyGroup()}.Match(h));
+}
+
+#if SWISSTABLE_HAVE_SSE2
+TEST(Group, Match) {
+  ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7,
+                    7,      5, 3,        1, 1,      1, 1,         1};
+  EXPECT_THAT(Group{group}.Match(0), ElementsAre());
+  EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 11, 12, 13, 14, 15));
+  EXPECT_THAT(Group{group}.Match(3), ElementsAre(3, 10));
+  EXPECT_THAT(Group{group}.Match(5), ElementsAre(5, 9));
+  EXPECT_THAT(Group{group}.Match(7), ElementsAre(7, 8));
+}
+
+TEST(Group, MatchEmpty) {
+  ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7,
+                    7,      5, 3,        1, 1,      1, 1,         1};
+  EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0, 4));
+}
+
+TEST(Group, MatchEmptyOrDeleted) {
+  ctrl_t group[] = {kEmpty, 1, kDeleted, 3, kEmpty, 5, kSentinel, 7,
+                    7,      5, 3,        1, 1,      1, 1,         1};
+  EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 2, 4));
+}
+#else
+TEST(Group, Match) {
+  ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1};
+  EXPECT_THAT(Group{group}.Match(0), ElementsAre());
+  EXPECT_THAT(Group{group}.Match(1), ElementsAre(1, 5, 7));
+  EXPECT_THAT(Group{group}.Match(2), ElementsAre(2, 4));
+}
+TEST(Group, MatchEmpty) {
+  ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1};
+  EXPECT_THAT(Group{group}.MatchEmpty(), ElementsAre(0));
+}
+
+TEST(Group, MatchEmptyOrDeleted) {
+  ctrl_t group[] = {kEmpty, 1, 2, kDeleted, 2, 1, kSentinel, 1};
+  EXPECT_THAT(Group{group}.MatchEmptyOrDeleted(), ElementsAre(0, 3));
+}
+#endif
+
+TEST(Batch, DropDeletes) {
+  constexpr size_t kCapacity = 63;
+  constexpr size_t kGroupWidth = container_internal::Group::kWidth;
+  std::vector<ctrl_t> ctrl(kCapacity + 1 + kGroupWidth);
+  ctrl[kCapacity] = kSentinel;
+  std::vector<ctrl_t> pattern = {kEmpty, 2, kDeleted, 2, kEmpty, 1, kDeleted};
+  for (size_t i = 0; i != kCapacity; ++i) {
+    ctrl[i] = pattern[i % pattern.size()];
+    if (i < kGroupWidth - 1)
+      ctrl[i + kCapacity + 1] = pattern[i % pattern.size()];
+  }
+  ConvertDeletedToEmptyAndFullToDeleted(ctrl.data(), kCapacity);
+  ASSERT_EQ(ctrl[kCapacity], kSentinel);
+  for (size_t i = 0; i < kCapacity + 1 + kGroupWidth; ++i) {
+    ctrl_t expected = pattern[i % (kCapacity + 1) % pattern.size()];
+    if (i == kCapacity) expected = kSentinel;
+    if (expected == kDeleted) expected = kEmpty;
+    if (IsFull(expected)) expected = kDeleted;
+    EXPECT_EQ(ctrl[i], expected)
+        << i << " " << int{pattern[i % pattern.size()]};
+  }
+}
+
+TEST(Group, CountLeadingEmptyOrDeleted) {
+  const std::vector<ctrl_t> empty_examples = {kEmpty, kDeleted};
+  const std::vector<ctrl_t> full_examples = {0, 1, 2, 3, 5, 9, 127, kSentinel};
+
+  for (ctrl_t empty : empty_examples) {
+    std::vector<ctrl_t> e(Group::kWidth, empty);
+    EXPECT_EQ(Group::kWidth, Group{e.data()}.CountLeadingEmptyOrDeleted());
+    for (ctrl_t full : full_examples) {
+      for (size_t i = 0; i != Group::kWidth; ++i) {
+        std::vector<ctrl_t> f(Group::kWidth, empty);
+        f[i] = full;
+        EXPECT_EQ(i, Group{f.data()}.CountLeadingEmptyOrDeleted());
+      }
+      std::vector<ctrl_t> f(Group::kWidth, empty);
+      f[Group::kWidth * 2 / 3] = full;
+      f[Group::kWidth / 2] = full;
+      EXPECT_EQ(
+          Group::kWidth / 2, Group{f.data()}.CountLeadingEmptyOrDeleted());
+    }
+  }
+}
+
+struct IntPolicy {
+  using slot_type = int64_t;
+  using key_type = int64_t;
+  using init_type = int64_t;
+
+  static void construct(void*, int64_t* slot, int64_t v) { *slot = v; }
+  static void destroy(void*, int64_t*) {}
+  static void transfer(void*, int64_t* new_slot, int64_t* old_slot) {
+    *new_slot = *old_slot;
+  }
+
+  static int64_t& element(slot_type* slot) { return *slot; }
+
+  template <class F>
+  static auto apply(F&& f, int64_t x) -> decltype(std::forward<F>(f)(x, x)) {
+    return std::forward<F>(f)(x, x);
+  }
+};
+
+class StringPolicy {
+  template <class F, class K, class V,
+            class = typename std::enable_if<
+                std::is_convertible<const K&, absl::string_view>::value>::type>
+  decltype(std::declval<F>()(
+      std::declval<const absl::string_view&>(), std::piecewise_construct,
+      std::declval<std::tuple<K>>(),
+      std::declval<V>())) static apply_impl(F&& f,
+                                            std::pair<std::tuple<K>, V> p) {
+    const absl::string_view& key = std::get<0>(p.first);
+    return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
+                              std::move(p.second));
+  }
+
+ public:
+  struct slot_type {
+    struct ctor {};
+
+    template <class... Ts>
+    slot_type(ctor, Ts&&... ts) : pair(std::forward<Ts>(ts)...) {}
+
+    std::pair<std::string, std::string> pair;
+  };
+
+  using key_type = std::string;
+  using init_type = std::pair<std::string, std::string>;
+
+  template <class allocator_type, class... Args>
+  static void construct(allocator_type* alloc, slot_type* slot, Args... args) {
+    std::allocator_traits<allocator_type>::construct(
+        *alloc, slot, typename slot_type::ctor(), std::forward<Args>(args)...);
+  }
+
+  template <class allocator_type>
+  static void destroy(allocator_type* alloc, slot_type* slot) {
+    std::allocator_traits<allocator_type>::destroy(*alloc, slot);
+  }
+
+  template <class allocator_type>
+  static void transfer(allocator_type* alloc, slot_type* new_slot,
+                       slot_type* old_slot) {
+    construct(alloc, new_slot, std::move(old_slot->pair));
+    destroy(alloc, old_slot);
+  }
+
+  static std::pair<std::string, std::string>& element(slot_type* slot) {
+    return slot->pair;
+  }
+
+  template <class F, class... Args>
+  static auto apply(F&& f, Args&&... args)
+      -> decltype(apply_impl(std::forward<F>(f),
+                             PairArgs(std::forward<Args>(args)...))) {
+    return apply_impl(std::forward<F>(f),
+                      PairArgs(std::forward<Args>(args)...));
+  }
+};
+
+struct StringHash : absl::Hash<absl::string_view> {
+  using is_transparent = void;
+};
+struct StringEq : std::equal_to<absl::string_view> {
+  using is_transparent = void;
+};
+
+struct StringTable
+    : raw_hash_set<StringPolicy, StringHash, StringEq, std::allocator<int>> {
+  using Base = typename StringTable::raw_hash_set;
+  StringTable() {}
+  using Base::Base;
+};
+
+struct IntTable
+    : raw_hash_set<IntPolicy, container_internal::hash_default_hash<int64_t>,
+                   std::equal_to<int64_t>, std::allocator<int64_t>> {
+  using Base = typename IntTable::raw_hash_set;
+  IntTable() {}
+  using Base::Base;
+};
+
+struct BadFastHash {
+  template <class T>
+  size_t operator()(const T&) const {
+    return 0;
+  }
+};
+
+struct BadTable : raw_hash_set<IntPolicy, BadFastHash, std::equal_to<int>,
+                               std::allocator<int>> {
+  using Base = typename BadTable::raw_hash_set;
+  BadTable() {}
+  using Base::Base;
+};
+
+TEST(Table, EmptyFunctorOptimization) {
+  static_assert(std::is_empty<std::equal_to<absl::string_view>>::value, "");
+  static_assert(std::is_empty<std::allocator<int>>::value, "");
+
+  struct MockTable {
+    void* ctrl;
+    void* slots;
+    size_t size;
+    size_t capacity;
+    size_t growth_left;
+  };
+  struct StatelessHash {
+    size_t operator()(absl::string_view) const { return 0; }
+  };
+  struct StatefulHash : StatelessHash {
+    size_t dummy;
+  };
+
+  EXPECT_EQ(
+      sizeof(MockTable),
+      sizeof(
+          raw_hash_set<StringPolicy, StatelessHash,
+                       std::equal_to<absl::string_view>, std::allocator<int>>));
+
+  EXPECT_EQ(
+      sizeof(MockTable) + sizeof(StatefulHash),
+      sizeof(
+          raw_hash_set<StringPolicy, StatefulHash,
+                       std::equal_to<absl::string_view>, std::allocator<int>>));
+}
+
+TEST(Table, Empty) {
+  IntTable t;
+  EXPECT_EQ(0, t.size());
+  EXPECT_TRUE(t.empty());
+}
+
+#ifdef __GNUC__
+template <class T>
+ABSL_ATTRIBUTE_ALWAYS_INLINE inline void DoNotOptimize(const T& v) {
+  asm volatile("" : : "r,m"(v) : "memory");
+}
+#endif
+
+TEST(Table, Prefetch) {
+  IntTable t;
+  t.emplace(1);
+  // Works for both present and absent keys.
+  t.prefetch(1);
+  t.prefetch(2);
+
+  // Do not run in debug mode, when prefetch is not implemented, or when
+  // sanitizers are enabled.
+#if defined(NDEBUG) && defined(__GNUC__) && !defined(ADDRESS_SANITIZER) && \
+    !defined(MEMORY_SANITIZER) && !defined(THREAD_SANITIZER) &&            \
+    !defined(UNDEFINED_BEHAVIOR_SANITIZER)
+  const auto now = [] { return absl::base_internal::CycleClock::Now(); };
+
+  static constexpr int size = 1000000;
+  for (int i = 0; i < size; ++i) t.insert(i);
+
+  int64_t no_prefetch = 0, prefetch = 0;
+  for (int iter = 0; iter < 10; ++iter) {
+    int64_t time = now();
+    for (int i = 0; i < size; ++i) {
+      DoNotOptimize(t.find(i));
+    }
+    no_prefetch += now() - time;
+
+    time = now();
+    for (int i = 0; i < size; ++i) {
+      t.prefetch(i + 20);
+      DoNotOptimize(t.find(i));
+    }
+    prefetch += now() - time;
+  }
+
+  // no_prefetch is at least 30% slower.
+  EXPECT_GE(1.0 * no_prefetch / prefetch, 1.3);
+#endif
+}
+
+TEST(Table, LookupEmpty) {
+  IntTable t;
+  auto it = t.find(0);
+  EXPECT_TRUE(it == t.end());
+}
+
+TEST(Table, Insert1) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  auto res = t.emplace(0);
+  EXPECT_TRUE(res.second);
+  EXPECT_THAT(*res.first, 0);
+  EXPECT_EQ(1, t.size());
+  EXPECT_THAT(*t.find(0), 0);
+}
+
+TEST(Table, Insert2) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  auto res = t.emplace(0);
+  EXPECT_TRUE(res.second);
+  EXPECT_THAT(*res.first, 0);
+  EXPECT_EQ(1, t.size());
+  EXPECT_TRUE(t.find(1) == t.end());
+  res = t.emplace(1);
+  EXPECT_TRUE(res.second);
+  EXPECT_THAT(*res.first, 1);
+  EXPECT_EQ(2, t.size());
+  EXPECT_THAT(*t.find(0), 0);
+  EXPECT_THAT(*t.find(1), 1);
+}
+
+TEST(Table, InsertCollision) {
+  BadTable t;
+  EXPECT_TRUE(t.find(1) == t.end());
+  auto res = t.emplace(1);
+  EXPECT_TRUE(res.second);
+  EXPECT_THAT(*res.first, 1);
+  EXPECT_EQ(1, t.size());
+
+  EXPECT_TRUE(t.find(2) == t.end());
+  res = t.emplace(2);
+  EXPECT_THAT(*res.first, 2);
+  EXPECT_TRUE(res.second);
+  EXPECT_EQ(2, t.size());
+
+  EXPECT_THAT(*t.find(1), 1);
+  EXPECT_THAT(*t.find(2), 2);
+}
+
+// Test that we do not add existent element in case we need to search through
+// many groups with deleted elements
+TEST(Table, InsertCollisionAndFindAfterDelete) {
+  BadTable t;  // all elements go to the same group.
+  // Have at least 2 groups with Group::kWidth collisions
+  // plus some extra collisions in the last group.
+  constexpr size_t kNumInserts = Group::kWidth * 2 + 5;
+  for (size_t i = 0; i < kNumInserts; ++i) {
+    auto res = t.emplace(i);
+    EXPECT_TRUE(res.second);
+    EXPECT_THAT(*res.first, i);
+    EXPECT_EQ(i + 1, t.size());
+  }
+
+  // Remove elements one by one and check
+  // that we still can find all other elements.
+  for (size_t i = 0; i < kNumInserts; ++i) {
+    EXPECT_EQ(1, t.erase(i)) << i;
+    for (size_t j = i + 1; j < kNumInserts; ++j) {
+      EXPECT_THAT(*t.find(j), j);
+      auto res = t.emplace(j);
+      EXPECT_FALSE(res.second) << i << " " << j;
+      EXPECT_THAT(*res.first, j);
+      EXPECT_EQ(kNumInserts - i - 1, t.size());
+    }
+  }
+  EXPECT_TRUE(t.empty());
+}
+
+TEST(Table, LazyEmplace) {
+  StringTable t;
+  bool called = false;
+  auto it = t.lazy_emplace("abc", [&](const StringTable::constructor& f) {
+    called = true;
+    f("abc", "ABC");
+  });
+  EXPECT_TRUE(called);
+  EXPECT_THAT(*it, Pair("abc", "ABC"));
+  called = false;
+  it = t.lazy_emplace("abc", [&](const StringTable::constructor& f) {
+    called = true;
+    f("abc", "DEF");
+  });
+  EXPECT_FALSE(called);
+  EXPECT_THAT(*it, Pair("abc", "ABC"));
+}
+
+TEST(Table, ContainsEmpty) {
+  IntTable t;
+
+  EXPECT_FALSE(t.contains(0));
+}
+
+TEST(Table, Contains1) {
+  IntTable t;
+
+  EXPECT_TRUE(t.insert(0).second);
+  EXPECT_TRUE(t.contains(0));
+  EXPECT_FALSE(t.contains(1));
+
+  EXPECT_EQ(1, t.erase(0));
+  EXPECT_FALSE(t.contains(0));
+}
+
+TEST(Table, Contains2) {
+  IntTable t;
+
+  EXPECT_TRUE(t.insert(0).second);
+  EXPECT_TRUE(t.contains(0));
+  EXPECT_FALSE(t.contains(1));
+
+  t.clear();
+  EXPECT_FALSE(t.contains(0));
+}
+
+int decompose_constructed;
+struct DecomposeType {
+  DecomposeType(int i) : i(i) {  // NOLINT
+    ++decompose_constructed;
+  }
+
+  explicit DecomposeType(const char* d) : DecomposeType(*d) {}
+
+  int i;
+};
+
+struct DecomposeHash {
+  using is_transparent = void;
+  size_t operator()(DecomposeType a) const { return a.i; }
+  size_t operator()(int a) const { return a; }
+  size_t operator()(const char* a) const { return *a; }
+};
+
+struct DecomposeEq {
+  using is_transparent = void;
+  bool operator()(DecomposeType a, DecomposeType b) const { return a.i == b.i; }
+  bool operator()(DecomposeType a, int b) const { return a.i == b; }
+  bool operator()(DecomposeType a, const char* b) const { return a.i == *b; }
+};
+
+struct DecomposePolicy {
+  using slot_type = DecomposeType;
+  using key_type = DecomposeType;
+  using init_type = DecomposeType;
+
+  template <typename T>
+  static void construct(void*, DecomposeType* slot, T&& v) {
+    *slot = DecomposeType(std::forward<T>(v));
+  }
+  static void destroy(void*, DecomposeType*) {}
+  static DecomposeType& element(slot_type* slot) { return *slot; }
+
+  template <class F, class T>
+  static auto apply(F&& f, const T& x) -> decltype(std::forward<F>(f)(x, x)) {
+    return std::forward<F>(f)(x, x);
+  }
+};
+
+template <typename Hash, typename Eq>
+void TestDecompose(bool construct_three) {
+  DecomposeType elem{0};
+  const int one = 1;
+  const char* three_p = "3";
+  const auto& three = three_p;
+
+  raw_hash_set<DecomposePolicy, Hash, Eq, std::allocator<int>> set1;
+
+  decompose_constructed = 0;
+  int expected_constructed = 0;
+  EXPECT_EQ(expected_constructed, decompose_constructed);
+  set1.insert(elem);
+  EXPECT_EQ(expected_constructed, decompose_constructed);
+  set1.insert(1);
+  EXPECT_EQ(++expected_constructed, decompose_constructed);
+  set1.emplace("3");
+  EXPECT_EQ(++expected_constructed, decompose_constructed);
+  EXPECT_EQ(expected_constructed, decompose_constructed);
+
+  {  // insert(T&&)
+    set1.insert(1);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+
+  {  // insert(const T&)
+    set1.insert(one);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+
+  {  // insert(hint, T&&)
+    set1.insert(set1.begin(), 1);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+
+  {  // insert(hint, const T&)
+    set1.insert(set1.begin(), one);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+
+  {  // emplace(...)
+    set1.emplace(1);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace("3");
+    expected_constructed += construct_three;
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace(one);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace(three);
+    expected_constructed += construct_three;
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+
+  {  // emplace_hint(...)
+    set1.emplace_hint(set1.begin(), 1);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace_hint(set1.begin(), "3");
+    expected_constructed += construct_three;
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace_hint(set1.begin(), one);
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+    set1.emplace_hint(set1.begin(), three);
+    expected_constructed += construct_three;
+    EXPECT_EQ(expected_constructed, decompose_constructed);
+  }
+}
+
+TEST(Table, Decompose) {
+  TestDecompose<DecomposeHash, DecomposeEq>(false);
+
+  struct TransparentHashIntOverload {
+    size_t operator()(DecomposeType a) const { return a.i; }
+    size_t operator()(int a) const { return a; }
+  };
+  struct TransparentEqIntOverload {
+    bool operator()(DecomposeType a, DecomposeType b) const {
+      return a.i == b.i;
+    }
+    bool operator()(DecomposeType a, int b) const { return a.i == b; }
+  };
+  TestDecompose<TransparentHashIntOverload, DecomposeEq>(true);
+  TestDecompose<TransparentHashIntOverload, TransparentEqIntOverload>(true);
+  TestDecompose<DecomposeHash, TransparentEqIntOverload>(true);
+}
+
+// Returns the largest m such that a table with m elements has the same number
+// of buckets as a table with n elements.
+size_t MaxDensitySize(size_t n) {
+  IntTable t;
+  t.reserve(n);
+  for (size_t i = 0; i != n; ++i) t.emplace(i);
+  const size_t c = t.bucket_count();
+  while (c == t.bucket_count()) t.emplace(n++);
+  return t.size() - 1;
+}
+
+struct Modulo1000Hash {
+  size_t operator()(int x) const { return x % 1000; }
+};
+
+struct Modulo1000HashTable
+    : public raw_hash_set<IntPolicy, Modulo1000Hash, std::equal_to<int>,
+                          std::allocator<int>> {};
+
+// Test that rehash with no resize happen in case of many deleted slots.
+TEST(Table, RehashWithNoResize) {
+  Modulo1000HashTable t;
+  // Adding the same length (and the same hash) strings
+  // to have at least kMinFullGroups groups
+  // with Group::kWidth collisions. Then feel upto MaxDensitySize;
+  const size_t kMinFullGroups = 7;
+  std::vector<int> keys;
+  for (size_t i = 0; i < MaxDensitySize(Group::kWidth * kMinFullGroups); ++i) {
+    int k = i * 1000;
+    t.emplace(k);
+    keys.push_back(k);
+  }
+  const size_t capacity = t.capacity();
+
+  // Remove elements from all groups except the first and the last one.
+  // All elements removed from full groups will be marked as kDeleted.
+  const size_t erase_begin = Group::kWidth / 2;
+  const size_t erase_end = (t.size() / Group::kWidth - 1) * Group::kWidth;
+  for (size_t i = erase_begin; i < erase_end; ++i) {
+    EXPECT_EQ(1, t.erase(keys[i])) << i;
+  }
+  keys.erase(keys.begin() + erase_begin, keys.begin() + erase_end);
+
+  auto last_key = keys.back();
+  size_t last_key_num_probes = GetHashtableDebugNumProbes(t, last_key);
+
+  // Make sure that we have to make a lot of probes for last key.
+  ASSERT_GT(last_key_num_probes, kMinFullGroups);
+
+  int x = 1;
+  // Insert and erase one element, before inplace rehash happen.
+  while (last_key_num_probes == GetHashtableDebugNumProbes(t, last_key)) {
+    t.emplace(x);
+    ASSERT_EQ(capacity, t.capacity());
+    // All elements should be there.
+    ASSERT_TRUE(t.find(x) != t.end()) << x;
+    for (const auto& k : keys) {
+      ASSERT_TRUE(t.find(k) != t.end()) << k;
+    }
+    t.erase(x);
+    ++x;
+  }
+}
+
+TEST(Table, InsertEraseStressTest) {
+  IntTable t;
+  const size_t kMinElementCount = 250;
+  std::deque<int> keys;
+  size_t i = 0;
+  for (; i < MaxDensitySize(kMinElementCount); ++i) {
+    t.emplace(i);
+    keys.push_back(i);
+  }
+  const size_t kNumIterations = 1000000;
+  for (; i < kNumIterations; ++i) {
+    ASSERT_EQ(1, t.erase(keys.front()));
+    keys.pop_front();
+    t.emplace(i);
+    keys.push_back(i);
+  }
+}
+
+TEST(Table, InsertOverloads) {
+  StringTable t;
+  // These should all trigger the insert(init_type) overload.
+  t.insert({{}, {}});
+  t.insert({"ABC", {}});
+  t.insert({"DEF", "!!!"});
+
+  EXPECT_THAT(t, UnorderedElementsAre(Pair("", ""), Pair("ABC", ""),
+                                      Pair("DEF", "!!!")));
+}
+
+TEST(Table, LargeTable) {
+  IntTable t;
+  for (int64_t i = 0; i != 100000; ++i) t.emplace(i << 40);
+  for (int64_t i = 0; i != 100000; ++i) ASSERT_EQ(i << 40, *t.find(i << 40));
+}
+
+// Timeout if copy is quadratic as it was in Rust.
+TEST(Table, EnsureNonQuadraticAsInRust) {
+  static const size_t kLargeSize = 1 << 15;
+
+  IntTable t;
+  for (size_t i = 0; i != kLargeSize; ++i) {
+    t.insert(i);
+  }
+
+  // If this is quadratic, the test will timeout.
+  IntTable t2;
+  for (const auto& entry : t) t2.insert(entry);
+}
+
+TEST(Table, ClearBug) {
+  IntTable t;
+  constexpr size_t capacity = container_internal::Group::kWidth - 1;
+  constexpr size_t max_size = capacity / 2;
+  for (size_t i = 0; i < max_size; ++i) {
+    t.insert(i);
+  }
+  ASSERT_EQ(capacity, t.capacity());
+  intptr_t original = reinterpret_cast<intptr_t>(&*t.find(2));
+  t.clear();
+  ASSERT_EQ(capacity, t.capacity());
+  for (size_t i = 0; i < max_size; ++i) {
+    t.insert(i);
+  }
+  ASSERT_EQ(capacity, t.capacity());
+  intptr_t second = reinterpret_cast<intptr_t>(&*t.find(2));
+  // We are checking that original and second are close enough to each other
+  // that they are probably still in the same group.  This is not strictly
+  // guaranteed.
+  EXPECT_LT(std::abs(original - second),
+            capacity * sizeof(IntTable::value_type));
+}
+
+TEST(Table, Erase) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  auto res = t.emplace(0);
+  EXPECT_TRUE(res.second);
+  EXPECT_EQ(1, t.size());
+  t.erase(res.first);
+  EXPECT_EQ(0, t.size());
+  EXPECT_TRUE(t.find(0) == t.end());
+}
+
+// Collect N bad keys by following algorithm:
+// 1. Create an empty table and reserve it to 2 * N.
+// 2. Insert N random elements.
+// 3. Take first Group::kWidth - 1 to bad_keys array.
+// 4. Clear the table without resize.
+// 5. Go to point 2 while N keys not collected
+std::vector<int64_t> CollectBadMergeKeys(size_t N) {
+  static constexpr int kGroupSize = Group::kWidth - 1;
+
+  auto topk_range = [](size_t b, size_t e, IntTable* t) -> std::vector<int64_t> {
+    for (size_t i = b; i != e; ++i) {
+      t->emplace(i);
+    }
+    std::vector<int64_t> res;
+    res.reserve(kGroupSize);
+    auto it = t->begin();
+    for (size_t i = b; i != e && i != b + kGroupSize; ++i, ++it) {
+      res.push_back(*it);
+    }
+    return res;
+  };
+
+  std::vector<int64_t> bad_keys;
+  bad_keys.reserve(N);
+  IntTable t;
+  t.reserve(N * 2);
+
+  for (size_t b = 0; bad_keys.size() < N; b += N) {
+    auto keys = topk_range(b, b + N, &t);
+    bad_keys.insert(bad_keys.end(), keys.begin(), keys.end());
+    t.erase(t.begin(), t.end());
+    EXPECT_TRUE(t.empty());
+  }
+  return bad_keys;
+}
+
+struct ProbeStats {
+  // Number of elements with specific probe length over all tested tables.
+  std::vector<size_t> all_probes_histogram;
+  // Ratios total_probe_length/size for every tested table.
+  std::vector<double> single_table_ratios;
+
+  friend ProbeStats operator+(const ProbeStats& a, const ProbeStats& b) {
+    ProbeStats res = a;
+    res.all_probes_histogram.resize(std::max(res.all_probes_histogram.size(),
+                                             b.all_probes_histogram.size()));
+    std::transform(b.all_probes_histogram.begin(), b.all_probes_histogram.end(),
+                   res.all_probes_histogram.begin(),
+                   res.all_probes_histogram.begin(), std::plus<size_t>());
+    res.single_table_ratios.insert(res.single_table_ratios.end(),
+                                   b.single_table_ratios.begin(),
+                                   b.single_table_ratios.end());
+    return res;
+  }
+
+  // Average ratio total_probe_length/size over tables.
+  double AvgRatio() const {
+    return std::accumulate(single_table_ratios.begin(),
+                           single_table_ratios.end(), 0.0) /
+           single_table_ratios.size();
+  }
+
+  // Maximum ratio total_probe_length/size over tables.
+  double MaxRatio() const {
+    return *std::max_element(single_table_ratios.begin(),
+                             single_table_ratios.end());
+  }
+
+  // Percentile ratio total_probe_length/size over tables.
+  double PercentileRatio(double Percentile = 0.95) const {
+    auto r = single_table_ratios;
+    auto mid = r.begin() + static_cast<size_t>(r.size() * Percentile);
+    if (mid != r.end()) {
+      std::nth_element(r.begin(), mid, r.end());
+      return *mid;
+    } else {
+      return MaxRatio();
+    }
+  }
+
+  // Maximum probe length over all elements and all tables.
+  size_t MaxProbe() const { return all_probes_histogram.size(); }
+
+  // Fraction of elements with specified probe length.
+  std::vector<double> ProbeNormalizedHistogram() const {
+    double total_elements = std::accumulate(all_probes_histogram.begin(),
+                                            all_probes_histogram.end(), 0ull);
+    std::vector<double> res;
+    for (size_t p : all_probes_histogram) {
+      res.push_back(p / total_elements);
+    }
+    return res;
+  }
+
+  size_t PercentileProbe(double Percentile = 0.99) const {
+    size_t idx = 0;
+    for (double p : ProbeNormalizedHistogram()) {
+      if (Percentile > p) {
+        Percentile -= p;
+        ++idx;
+      } else {
+        return idx;
+      }
+    }
+    return idx;
+  }
+
+  friend std::ostream& operator<<(std::ostream& out, const ProbeStats& s) {
+    out << "{AvgRatio:" << s.AvgRatio() << ", MaxRatio:" << s.MaxRatio()
+        << ", PercentileRatio:" << s.PercentileRatio()
+        << ", MaxProbe:" << s.MaxProbe() << ", Probes=[";
+    for (double p : s.ProbeNormalizedHistogram()) {
+      out << p << ",";
+    }
+    out << "]}";
+
+    return out;
+  }
+};
+
+struct ExpectedStats {
+  double avg_ratio;
+  double max_ratio;
+  std::vector<std::pair<double, double>> pecentile_ratios;
+  std::vector<std::pair<double, double>> pecentile_probes;
+
+  friend std::ostream& operator<<(std::ostream& out, const ExpectedStats& s) {
+    out << "{AvgRatio:" << s.avg_ratio << ", MaxRatio:" << s.max_ratio
+        << ", PercentileRatios: [";
+    for (auto el : s.pecentile_ratios) {
+      out << el.first << ":" << el.second << ", ";
+    }
+    out << "], PercentileProbes: [";
+    for (auto el : s.pecentile_probes) {
+      out << el.first << ":" << el.second << ", ";
+    }
+    out << "]}";
+
+    return out;
+  }
+};
+
+void VerifyStats(size_t size, const ExpectedStats& exp,
+                 const ProbeStats& stats) {
+  EXPECT_LT(stats.AvgRatio(), exp.avg_ratio) << size << " " << stats;
+  EXPECT_LT(stats.MaxRatio(), exp.max_ratio) << size << " " << stats;
+  for (auto pr : exp.pecentile_ratios) {
+    EXPECT_LE(stats.PercentileRatio(pr.first), pr.second)
+        << size << " " << pr.first << " " << stats;
+  }
+
+  for (auto pr : exp.pecentile_probes) {
+    EXPECT_LE(stats.PercentileProbe(pr.first), pr.second)
+        << size << " " << pr.first << " " << stats;
+  }
+}
+
+using ProbeStatsPerSize = std::map<size_t, ProbeStats>;
+
+// Collect total ProbeStats on num_iters iterations of the following algorithm:
+// 1. Create new table and reserve it to keys.size() * 2
+// 2. Insert all keys xored with seed
+// 3. Collect ProbeStats from final table.
+ProbeStats CollectProbeStatsOnKeysXoredWithSeed(const std::vector<int64_t>& keys,
+                                                size_t num_iters) {
+  const size_t reserve_size = keys.size() * 2;
+
+  ProbeStats stats;
+
+  int64_t seed = 0x71b1a19b907d6e33;
+  while (num_iters--) {
+    seed = static_cast<int64_t>(static_cast<uint64_t>(seed) * 17 + 13);
+    IntTable t1;
+    t1.reserve(reserve_size);
+    for (const auto& key : keys) {
+      t1.emplace(key ^ seed);
+    }
+
+    auto probe_histogram = GetHashtableDebugNumProbesHistogram(t1);
+    stats.all_probes_histogram.resize(
+        std::max(stats.all_probes_histogram.size(), probe_histogram.size()));
+    std::transform(probe_histogram.begin(), probe_histogram.end(),
+                   stats.all_probes_histogram.begin(),
+                   stats.all_probes_histogram.begin(), std::plus<size_t>());
+
+    size_t total_probe_seq_length = 0;
+    for (size_t i = 0; i < probe_histogram.size(); ++i) {
+      total_probe_seq_length += i * probe_histogram[i];
+    }
+    stats.single_table_ratios.push_back(total_probe_seq_length * 1.0 /
+                                        keys.size());
+    t1.erase(t1.begin(), t1.end());
+  }
+  return stats;
+}
+
+ExpectedStats XorSeedExpectedStats() {
+  constexpr bool kRandomizesInserts =
+#if NDEBUG
+      false;
+#else   // NDEBUG
+      true;
+#endif  // NDEBUG
+
+  // The effective load factor is larger in non-opt mode because we insert
+  // elements out of order.
+  switch (container_internal::Group::kWidth) {
+    case 8:
+      if (kRandomizesInserts) {
+  return {0.05,
+          1.0,
+          {{0.95, 0.5}},
+          {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}};
+      } else {
+  return {0.05,
+          2.0,
+          {{0.95, 0.1}},
+          {{0.95, 0}, {0.99, 2}, {0.999, 4}, {0.9999, 10}}};
+      }
+      break;
+    case 16:
+      if (kRandomizesInserts) {
+        return {0.1,
+                1.0,
+                {{0.95, 0.1}},
+                {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}};
+      } else {
+        return {0.05,
+                1.0,
+                {{0.95, 0.05}},
+                {{0.95, 0}, {0.99, 1}, {0.999, 4}, {0.9999, 10}}};
+      }
+      break;
+    default:
+      ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width");
+  }
+  return {};
+}
+TEST(Table, DISABLED_EnsureNonQuadraticTopNXorSeedByProbeSeqLength) {
+  ProbeStatsPerSize stats;
+  std::vector<size_t> sizes = {Group::kWidth << 5, Group::kWidth << 10};
+  for (size_t size : sizes) {
+    stats[size] =
+        CollectProbeStatsOnKeysXoredWithSeed(CollectBadMergeKeys(size), 200);
+  }
+  auto expected = XorSeedExpectedStats();
+  for (size_t size : sizes) {
+    auto& stat = stats[size];
+    VerifyStats(size, expected, stat);
+  }
+}
+
+// Collect total ProbeStats on num_iters iterations of the following algorithm:
+// 1. Create new table
+// 2. Select 10% of keys and insert 10 elements key * 17 + j * 13
+// 3. Collect ProbeStats from final table
+ProbeStats CollectProbeStatsOnLinearlyTransformedKeys(
+    const std::vector<int64_t>& keys, size_t num_iters) {
+  ProbeStats stats;
+
+  std::random_device rd;
+  std::mt19937 rng(rd());
+  auto linear_transform = [](size_t x, size_t y) { return x * 17 + y * 13; };
+  std::uniform_int_distribution<size_t> dist(0, keys.size()-1);
+  while (num_iters--) {
+    IntTable t1;
+    size_t num_keys = keys.size() / 10;
+    size_t start = dist(rng);
+    for (size_t i = 0; i != num_keys; ++i) {
+      for (size_t j = 0; j != 10; ++j) {
+        t1.emplace(linear_transform(keys[(i + start) % keys.size()], j));
+      }
+    }
+
+    auto probe_histogram = GetHashtableDebugNumProbesHistogram(t1);
+    stats.all_probes_histogram.resize(
+        std::max(stats.all_probes_histogram.size(), probe_histogram.size()));
+    std::transform(probe_histogram.begin(), probe_histogram.end(),
+                   stats.all_probes_histogram.begin(),
+                   stats.all_probes_histogram.begin(), std::plus<size_t>());
+
+    size_t total_probe_seq_length = 0;
+    for (size_t i = 0; i < probe_histogram.size(); ++i) {
+      total_probe_seq_length += i * probe_histogram[i];
+    }
+    stats.single_table_ratios.push_back(total_probe_seq_length * 1.0 /
+                                        t1.size());
+    t1.erase(t1.begin(), t1.end());
+  }
+  return stats;
+}
+
+ExpectedStats LinearTransformExpectedStats() {
+  constexpr bool kRandomizesInserts =
+#if NDEBUG
+      false;
+#else   // NDEBUG
+      true;
+#endif  // NDEBUG
+
+  // The effective load factor is larger in non-opt mode because we insert
+  // elements out of order.
+  switch (container_internal::Group::kWidth) {
+    case 8:
+      if (kRandomizesInserts) {
+        return {0.1,
+                0.5,
+                {{0.95, 0.3}},
+                {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}};
+      } else {
+        return {0.15,
+                0.5,
+                {{0.95, 0.3}},
+                {{0.95, 0}, {0.99, 3}, {0.999, 15}, {0.9999, 25}}};
+      }
+      break;
+    case 16:
+      if (kRandomizesInserts) {
+        return {0.1,
+                0.4,
+                {{0.95, 0.3}},
+                {{0.95, 0}, {0.99, 1}, {0.999, 8}, {0.9999, 15}}};
+      } else {
+        return {0.05,
+                0.2,
+                {{0.95, 0.1}},
+                {{0.95, 0}, {0.99, 1}, {0.999, 6}, {0.9999, 10}}};
+      }
+      break;
+    default:
+      ABSL_RAW_LOG(FATAL, "%s", "Unknown Group width");
+  }
+  return {};
+}
+TEST(Table, DISABLED_EnsureNonQuadraticTopNLinearTransformByProbeSeqLength) {
+  ProbeStatsPerSize stats;
+  std::vector<size_t> sizes = {Group::kWidth << 5, Group::kWidth << 10};
+  for (size_t size : sizes) {
+    stats[size] = CollectProbeStatsOnLinearlyTransformedKeys(
+        CollectBadMergeKeys(size), 300);
+  }
+  auto expected = LinearTransformExpectedStats();
+  for (size_t size : sizes) {
+    auto& stat = stats[size];
+    VerifyStats(size, expected, stat);
+  }
+}
+
+TEST(Table, EraseCollision) {
+  BadTable t;
+
+  // 1 2 3
+  t.emplace(1);
+  t.emplace(2);
+  t.emplace(3);
+  EXPECT_THAT(*t.find(1), 1);
+  EXPECT_THAT(*t.find(2), 2);
+  EXPECT_THAT(*t.find(3), 3);
+  EXPECT_EQ(3, t.size());
+
+  // 1 DELETED 3
+  t.erase(t.find(2));
+  EXPECT_THAT(*t.find(1), 1);
+  EXPECT_TRUE(t.find(2) == t.end());
+  EXPECT_THAT(*t.find(3), 3);
+  EXPECT_EQ(2, t.size());
+
+  // DELETED DELETED 3
+  t.erase(t.find(1));
+  EXPECT_TRUE(t.find(1) == t.end());
+  EXPECT_TRUE(t.find(2) == t.end());
+  EXPECT_THAT(*t.find(3), 3);
+  EXPECT_EQ(1, t.size());
+
+  // DELETED DELETED DELETED
+  t.erase(t.find(3));
+  EXPECT_TRUE(t.find(1) == t.end());
+  EXPECT_TRUE(t.find(2) == t.end());
+  EXPECT_TRUE(t.find(3) == t.end());
+  EXPECT_EQ(0, t.size());
+}
+
+TEST(Table, EraseInsertProbing) {
+  BadTable t(100);
+
+  // 1 2 3 4
+  t.emplace(1);
+  t.emplace(2);
+  t.emplace(3);
+  t.emplace(4);
+
+  // 1 DELETED 3 DELETED
+  t.erase(t.find(2));
+  t.erase(t.find(4));
+
+  // 1 10 3 11 12
+  t.emplace(10);
+  t.emplace(11);
+  t.emplace(12);
+
+  EXPECT_EQ(5, t.size());
+  EXPECT_THAT(t, UnorderedElementsAre(1, 10, 3, 11, 12));
+}
+
+TEST(Table, Clear) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  t.clear();
+  EXPECT_TRUE(t.find(0) == t.end());
+  auto res = t.emplace(0);
+  EXPECT_TRUE(res.second);
+  EXPECT_EQ(1, t.size());
+  t.clear();
+  EXPECT_EQ(0, t.size());
+  EXPECT_TRUE(t.find(0) == t.end());
+}
+
+TEST(Table, Swap) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  auto res = t.emplace(0);
+  EXPECT_TRUE(res.second);
+  EXPECT_EQ(1, t.size());
+  IntTable u;
+  t.swap(u);
+  EXPECT_EQ(0, t.size());
+  EXPECT_EQ(1, u.size());
+  EXPECT_TRUE(t.find(0) == t.end());
+  EXPECT_THAT(*u.find(0), 0);
+}
+
+TEST(Table, Rehash) {
+  IntTable t;
+  EXPECT_TRUE(t.find(0) == t.end());
+  t.emplace(0);
+  t.emplace(1);
+  EXPECT_EQ(2, t.size());
+  t.rehash(128);
+  EXPECT_EQ(2, t.size());
+  EXPECT_THAT(*t.find(0), 0);
+  EXPECT_THAT(*t.find(1), 1);
+}
+
+TEST(Table, RehashDoesNotRehashWhenNotNecessary) {
+  IntTable t;
+  t.emplace(0);
+  t.emplace(1);
+  auto* p = &*t.find(0);
+  t.rehash(1);
+  EXPECT_EQ(p, &*t.find(0));
+}
+
+TEST(Table, RehashZeroDoesNotAllocateOnEmptyTable) {
+  IntTable t;
+  t.rehash(0);
+  EXPECT_EQ(0, t.bucket_count());
+}
+
+TEST(Table, RehashZeroDeallocatesEmptyTable) {
+  IntTable t;
+  t.emplace(0);
+  t.clear();
+  EXPECT_NE(0, t.bucket_count());
+  t.rehash(0);
+  EXPECT_EQ(0, t.bucket_count());
+}
+
+TEST(Table, RehashZeroForcesRehash) {
+  IntTable t;
+  t.emplace(0);
+  t.emplace(1);
+  auto* p = &*t.find(0);
+  t.rehash(0);
+  EXPECT_NE(p, &*t.find(0));
+}
+
+TEST(Table, ConstructFromInitList) {
+  using P = std::pair<std::string, std::string>;
+  struct Q {
+    operator P() const { return {}; }
+  };
+  StringTable t = {P(), Q(), {}, {{}, {}}};
+}
+
+TEST(Table, CopyConstruct) {
+  IntTable t;
+  t.max_load_factor(.321f);
+  t.emplace(0);
+  EXPECT_EQ(1, t.size());
+  {
+    IntTable u(t);
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(t.max_load_factor(), u.max_load_factor());
+    EXPECT_THAT(*u.find(0), 0);
+  }
+  {
+    IntTable u{t};
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(t.max_load_factor(), u.max_load_factor());
+    EXPECT_THAT(*u.find(0), 0);
+  }
+  {
+    IntTable u = t;
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(t.max_load_factor(), u.max_load_factor());
+    EXPECT_THAT(*u.find(0), 0);
+  }
+}
+
+TEST(Table, CopyConstructWithAlloc) {
+  StringTable t;
+  t.max_load_factor(.321f);
+  t.emplace("a", "b");
+  EXPECT_EQ(1, t.size());
+  StringTable u(t, Alloc<std::pair<std::string, std::string>>());
+  EXPECT_EQ(1, u.size());
+  EXPECT_EQ(t.max_load_factor(), u.max_load_factor());
+  EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+}
+
+struct ExplicitAllocIntTable
+    : raw_hash_set<IntPolicy, container_internal::hash_default_hash<int64_t>,
+                   std::equal_to<int64_t>, Alloc<int64_t>> {
+  ExplicitAllocIntTable() {}
+};
+
+TEST(Table, AllocWithExplicitCtor) {
+  ExplicitAllocIntTable t;
+  EXPECT_EQ(0, t.size());
+}
+
+TEST(Table, MoveConstruct) {
+  {
+    StringTable t;
+    t.max_load_factor(.321f);
+    const float lf = t.max_load_factor();
+    t.emplace("a", "b");
+    EXPECT_EQ(1, t.size());
+
+    StringTable u(std::move(t));
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(lf, u.max_load_factor());
+    EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+  }
+  {
+    StringTable t;
+    t.max_load_factor(.321f);
+    const float lf = t.max_load_factor();
+    t.emplace("a", "b");
+    EXPECT_EQ(1, t.size());
+
+    StringTable u{std::move(t)};
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(lf, u.max_load_factor());
+    EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+  }
+  {
+    StringTable t;
+    t.max_load_factor(.321f);
+    const float lf = t.max_load_factor();
+    t.emplace("a", "b");
+    EXPECT_EQ(1, t.size());
+
+    StringTable u = std::move(t);
+    EXPECT_EQ(1, u.size());
+    EXPECT_EQ(lf, u.max_load_factor());
+    EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+  }
+}
+
+TEST(Table, MoveConstructWithAlloc) {
+  StringTable t;
+  t.max_load_factor(.321f);
+  const float lf = t.max_load_factor();
+  t.emplace("a", "b");
+  EXPECT_EQ(1, t.size());
+  StringTable u(std::move(t), Alloc<std::pair<std::string, std::string>>());
+  EXPECT_EQ(1, u.size());
+  EXPECT_EQ(lf, u.max_load_factor());
+  EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+}
+
+TEST(Table, CopyAssign) {
+  StringTable t;
+  t.max_load_factor(.321f);
+  t.emplace("a", "b");
+  EXPECT_EQ(1, t.size());
+  StringTable u;
+  u = t;
+  EXPECT_EQ(1, u.size());
+  EXPECT_EQ(t.max_load_factor(), u.max_load_factor());
+  EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+}
+
+TEST(Table, CopySelfAssign) {
+  StringTable t;
+  t.max_load_factor(.321f);
+  const float lf = t.max_load_factor();
+  t.emplace("a", "b");
+  EXPECT_EQ(1, t.size());
+  t = *&t;
+  EXPECT_EQ(1, t.size());
+  EXPECT_EQ(lf, t.max_load_factor());
+  EXPECT_THAT(*t.find("a"), Pair("a", "b"));
+}
+
+TEST(Table, MoveAssign) {
+  StringTable t;
+  t.max_load_factor(.321f);
+  const float lf = t.max_load_factor();
+  t.emplace("a", "b");
+  EXPECT_EQ(1, t.size());
+  StringTable u;
+  u = std::move(t);
+  EXPECT_EQ(1, u.size());
+  EXPECT_EQ(lf, u.max_load_factor());
+  EXPECT_THAT(*u.find("a"), Pair("a", "b"));
+}
+
+TEST(Table, Equality) {
+  StringTable t;
+  std::vector<std::pair<std::string, std::string>> v = {{"a", "b"}, {"aa", "bb"}};
+  t.insert(std::begin(v), std::end(v));
+  StringTable u = t;
+  EXPECT_EQ(u, t);
+}
+
+TEST(Table, Equality2) {
+  StringTable t;
+  std::vector<std::pair<std::string, std::string>> v1 = {{"a", "b"}, {"aa", "bb"}};
+  t.insert(std::begin(v1), std::end(v1));
+  StringTable u;
+  std::vector<std::pair<std::string, std::string>> v2 = {{"a", "a"}, {"aa", "aa"}};
+  u.insert(std::begin(v2), std::end(v2));
+  EXPECT_NE(u, t);
+}
+
+TEST(Table, Equality3) {
+  StringTable t;
+  std::vector<std::pair<std::string, std::string>> v1 = {{"b", "b"}, {"bb", "bb"}};
+  t.insert(std::begin(v1), std::end(v1));
+  StringTable u;
+  std::vector<std::pair<std::string, std::string>> v2 = {{"a", "a"}, {"aa", "aa"}};
+  u.insert(std::begin(v2), std::end(v2));
+  EXPECT_NE(u, t);
+}
+
+TEST(Table, NumDeletedRegression) {
+  IntTable t;
+  t.emplace(0);
+  t.erase(t.find(0));
+  // construct over a deleted slot.
+  t.emplace(0);
+  t.clear();
+}
+
+TEST(Table, FindFullDeletedRegression) {
+  IntTable t;
+  for (int i = 0; i < 1000; ++i) {
+    t.emplace(i);
+    t.erase(t.find(i));
+  }
+  EXPECT_EQ(0, t.size());
+}
+
+TEST(Table, ReplacingDeletedSlotDoesNotRehash) {
+  size_t n;
+  {
+    // Compute n such that n is the maximum number of elements before rehash.
+    IntTable t;
+    t.emplace(0);
+    size_t c = t.bucket_count();
+    for (n = 1; c == t.bucket_count(); ++n) t.emplace(n);
+    --n;
+  }
+  IntTable t;
+  t.rehash(n);
+  const size_t c = t.bucket_count();
+  for (size_t i = 0; i != n; ++i) t.emplace(i);
+  EXPECT_EQ(c, t.bucket_count()) << "rehashing threshold = " << n;
+  t.erase(0);
+  t.emplace(0);
+  EXPECT_EQ(c, t.bucket_count()) << "rehashing threshold = " << n;
+}
+
+TEST(Table, NoThrowMoveConstruct) {
+  ASSERT_TRUE(
+      std::is_nothrow_copy_constructible<absl::Hash<absl::string_view>>::value);
+  ASSERT_TRUE(std::is_nothrow_copy_constructible<
+              std::equal_to<absl::string_view>>::value);
+  ASSERT_TRUE(std::is_nothrow_copy_constructible<std::allocator<int>>::value);
+  EXPECT_TRUE(std::is_nothrow_move_constructible<StringTable>::value);
+}
+
+TEST(Table, NoThrowMoveAssign) {
+  ASSERT_TRUE(
+      std::is_nothrow_move_assignable<absl::Hash<absl::string_view>>::value);
+  ASSERT_TRUE(
+      std::is_nothrow_move_assignable<std::equal_to<absl::string_view>>::value);
+  ASSERT_TRUE(std::is_nothrow_move_assignable<std::allocator<int>>::value);
+  ASSERT_TRUE(
+      absl::allocator_traits<std::allocator<int>>::is_always_equal::value);
+  EXPECT_TRUE(std::is_nothrow_move_assignable<StringTable>::value);
+}
+
+TEST(Table, NoThrowSwappable) {
+  ASSERT_TRUE(
+      container_internal::IsNoThrowSwappable<absl::Hash<absl::string_view>>());
+  ASSERT_TRUE(container_internal::IsNoThrowSwappable<
+              std::equal_to<absl::string_view>>());
+  ASSERT_TRUE(container_internal::IsNoThrowSwappable<std::allocator<int>>());
+  EXPECT_TRUE(container_internal::IsNoThrowSwappable<StringTable>());
+}
+
+TEST(Table, HeterogeneousLookup) {
+  struct Hash {
+    size_t operator()(int64_t i) const { return i; }
+    size_t operator()(double i) const {
+      ADD_FAILURE();
+      return i;
+    }
+  };
+  struct Eq {
+    bool operator()(int64_t a, int64_t b) const { return a == b; }
+    bool operator()(double a, int64_t b) const {
+      ADD_FAILURE();
+      return a == b;
+    }
+    bool operator()(int64_t a, double b) const {
+      ADD_FAILURE();
+      return a == b;
+    }
+    bool operator()(double a, double b) const {
+      ADD_FAILURE();
+      return a == b;
+    }
+  };
+
+  struct THash {
+    using is_transparent = void;
+    size_t operator()(int64_t i) const { return i; }
+    size_t operator()(double i) const { return i; }
+  };
+  struct TEq {
+    using is_transparent = void;
+    bool operator()(int64_t a, int64_t b) const { return a == b; }
+    bool operator()(double a, int64_t b) const { return a == b; }
+    bool operator()(int64_t a, double b) const { return a == b; }
+    bool operator()(double a, double b) const { return a == b; }
+  };
+
+  raw_hash_set<IntPolicy, Hash, Eq, Alloc<int64_t>> s{0, 1, 2};
+  // It will convert to int64_t before the query.
+  EXPECT_EQ(1, *s.find(double{1.1}));
+
+  raw_hash_set<IntPolicy, THash, TEq, Alloc<int64_t>> ts{0, 1, 2};
+  // It will try to use the double, and fail to find the object.
+  EXPECT_TRUE(ts.find(1.1) == ts.end());
+}
+
+template <class Table>
+using CallFind = decltype(std::declval<Table&>().find(17));
+
+template <class Table>
+using CallErase = decltype(std::declval<Table&>().erase(17));
+
+template <class Table>
+using CallExtract = decltype(std::declval<Table&>().extract(17));
+
+template <class Table>
+using CallPrefetch = decltype(std::declval<Table&>().prefetch(17));
+
+template <class Table>
+using CallCount = decltype(std::declval<Table&>().count(17));
+
+template <template <typename> class C, class Table, class = void>
+struct VerifyResultOf : std::false_type {};
+
+template <template <typename> class C, class Table>
+struct VerifyResultOf<C, Table, absl::void_t<C<Table>>> : std::true_type {};
+
+TEST(Table, HeterogeneousLookupOverloads) {
+  using NonTransparentTable =
+      raw_hash_set<StringPolicy, absl::Hash<absl::string_view>,
+                   std::equal_to<absl::string_view>, std::allocator<int>>;
+
+  EXPECT_FALSE((VerifyResultOf<CallFind, NonTransparentTable>()));
+  EXPECT_FALSE((VerifyResultOf<CallErase, NonTransparentTable>()));
+  EXPECT_FALSE((VerifyResultOf<CallExtract, NonTransparentTable>()));
+  EXPECT_FALSE((VerifyResultOf<CallPrefetch, NonTransparentTable>()));
+  EXPECT_FALSE((VerifyResultOf<CallCount, NonTransparentTable>()));
+
+  using TransparentTable = raw_hash_set<
+      StringPolicy,
+      absl::container_internal::hash_default_hash<absl::string_view>,
+      absl::container_internal::hash_default_eq<absl::string_view>,
+      std::allocator<int>>;
+
+  EXPECT_TRUE((VerifyResultOf<CallFind, TransparentTable>()));
+  EXPECT_TRUE((VerifyResultOf<CallErase, TransparentTable>()));
+  EXPECT_TRUE((VerifyResultOf<CallExtract, TransparentTable>()));
+  EXPECT_TRUE((VerifyResultOf<CallPrefetch, TransparentTable>()));
+  EXPECT_TRUE((VerifyResultOf<CallCount, TransparentTable>()));
+}
+
+// TODO(alkis): Expand iterator tests.
+TEST(Iterator, IsDefaultConstructible) {
+  StringTable::iterator i;
+  EXPECT_TRUE(i == StringTable::iterator());
+}
+
+TEST(ConstIterator, IsDefaultConstructible) {
+  StringTable::const_iterator i;
+  EXPECT_TRUE(i == StringTable::const_iterator());
+}
+
+TEST(Iterator, ConvertsToConstIterator) {
+  StringTable::iterator i;
+  EXPECT_TRUE(i == StringTable::const_iterator());
+}
+
+TEST(Iterator, Iterates) {
+  IntTable t;
+  for (size_t i = 3; i != 6; ++i) EXPECT_TRUE(t.emplace(i).second);
+  EXPECT_THAT(t, UnorderedElementsAre(3, 4, 5));
+}
+
+TEST(Table, Merge) {
+  StringTable t1, t2;
+  t1.emplace("0", "-0");
+  t1.emplace("1", "-1");
+  t2.emplace("0", "~0");
+  t2.emplace("2", "~2");
+
+  EXPECT_THAT(t1, UnorderedElementsAre(Pair("0", "-0"), Pair("1", "-1")));
+  EXPECT_THAT(t2, UnorderedElementsAre(Pair("0", "~0"), Pair("2", "~2")));
+
+  t1.merge(t2);
+  EXPECT_THAT(t1, UnorderedElementsAre(Pair("0", "-0"), Pair("1", "-1"),
+                                       Pair("2", "~2")));
+  EXPECT_THAT(t2, UnorderedElementsAre(Pair("0", "~0")));
+}
+
+TEST(Nodes, EmptyNodeType) {
+  using node_type = StringTable::node_type;
+  node_type n;
+  EXPECT_FALSE(n);
+  EXPECT_TRUE(n.empty());
+
+  EXPECT_TRUE((std::is_same<node_type::allocator_type,
+                            StringTable::allocator_type>::value));
+}
+
+TEST(Nodes, ExtractInsert) {
+  constexpr char k0[] = "Very long std::string zero.";
+  constexpr char k1[] = "Very long std::string one.";
+  constexpr char k2[] = "Very long std::string two.";
+  StringTable t = {{k0, ""}, {k1, ""}, {k2, ""}};
+  EXPECT_THAT(t,
+              UnorderedElementsAre(Pair(k0, ""), Pair(k1, ""), Pair(k2, "")));
+
+  auto node = t.extract(k0);
+  EXPECT_THAT(t, UnorderedElementsAre(Pair(k1, ""), Pair(k2, "")));
+  EXPECT_TRUE(node);
+  EXPECT_FALSE(node.empty());
+
+  StringTable t2;
+  auto res = t2.insert(std::move(node));
+  EXPECT_TRUE(res.inserted);
+  EXPECT_THAT(*res.position, Pair(k0, ""));
+  EXPECT_FALSE(res.node);
+  EXPECT_THAT(t2, UnorderedElementsAre(Pair(k0, "")));
+
+  // Not there.
+  EXPECT_THAT(t, UnorderedElementsAre(Pair(k1, ""), Pair(k2, "")));
+  node = t.extract("Not there!");
+  EXPECT_THAT(t, UnorderedElementsAre(Pair(k1, ""), Pair(k2, "")));
+  EXPECT_FALSE(node);
+
+  // Inserting nothing.
+  res = t2.insert(std::move(node));
+  EXPECT_FALSE(res.inserted);
+  EXPECT_EQ(res.position, t2.end());
+  EXPECT_FALSE(res.node);
+  EXPECT_THAT(t2, UnorderedElementsAre(Pair(k0, "")));
+
+  t.emplace(k0, "1");
+  node = t.extract(k0);
+
+  // Insert duplicate.
+  res = t2.insert(std::move(node));
+  EXPECT_FALSE(res.inserted);
+  EXPECT_THAT(*res.position, Pair(k0, ""));
+  EXPECT_TRUE(res.node);
+  EXPECT_FALSE(node);
+}
+
+StringTable MakeSimpleTable(size_t size) {
+  StringTable t;
+  for (size_t i = 0; i < size; ++i) t.emplace(std::string(1, 'A' + i), "");
+  return t;
+}
+
+std::string OrderOfIteration(const StringTable& t) {
+  std::string order;
+  for (auto& p : t) order += p.first;
+  return order;
+}
+
+TEST(Table, IterationOrderChangesByInstance) {
+  // Needs to be more than kWidth elements to be able to affect order.
+  const StringTable reference = MakeSimpleTable(20);
+
+  // Since order is non-deterministic we can't just try once and verify.
+  // We'll try until we find that order changed. It should not take many tries
+  // for that.
+  // Important: we have to keep the old tables around. Otherwise tcmalloc will
+  // just give us the same blocks and we would be doing the same order again.
+  std::vector<StringTable> garbage;
+  for (int i = 0; i < 10; ++i) {
+    auto trial = MakeSimpleTable(20);
+    if (OrderOfIteration(trial) != OrderOfIteration(reference)) {
+      // We are done.
+      return;
+    }
+    garbage.push_back(std::move(trial));
+  }
+  FAIL();
+}
+
+TEST(Table, IterationOrderChangesOnRehash) {
+  // Since order is non-deterministic we can't just try once and verify.
+  // We'll try until we find that order changed. It should not take many tries
+  // for that.
+  // Important: we have to keep the old tables around. Otherwise tcmalloc will
+  // just give us the same blocks and we would be doing the same order again.
+  std::vector<StringTable> garbage;
+  for (int i = 0; i < 10; ++i) {
+    // Needs to be more than kWidth elements to be able to affect order.
+    StringTable t = MakeSimpleTable(20);
+    const std::string reference = OrderOfIteration(t);
+    // Force rehash to the same size.
+    t.rehash(0);
+    std::string trial = OrderOfIteration(t);
+    if (trial != reference) {
+      // We are done.
+      return;
+    }
+    garbage.push_back(std::move(t));
+  }
+  FAIL();
+}
+
+TEST(Table, IterationOrderChangesForSmallTables) {
+  // Since order is non-deterministic we can't just try once and verify.
+  // We'll try until we find that order changed.
+  // Important: we have to keep the old tables around. Otherwise tcmalloc will
+  // just give us the same blocks and we would be doing the same order again.
+  StringTable reference_table = MakeSimpleTable(5);
+  const std::string reference = OrderOfIteration(reference_table);
+  std::vector<StringTable> garbage;
+  for (int i = 0; i < 50; ++i) {
+    StringTable t = MakeSimpleTable(5);
+    std::string trial = OrderOfIteration(t);
+    if (trial != reference) {
+      // We are done.
+      return;
+    }
+    garbage.push_back(std::move(t));
+  }
+  FAIL() << "Iteration order remained the same across many attempts.";
+}
+
+// Fill the table to 3 different load factors (min, median, max) and evaluate
+// the percentage of perfect hits using the debug API.
+template <class Table, class AddFn>
+std::vector<double> CollectPerfectRatios(Table t, AddFn add) {
+  using Key = typename Table::key_type;
+
+  // First, fill enough to have a good distribution.
+  constexpr size_t kMinSize = 10000;
+  std::vector<Key> keys;
+  while (t.size() < kMinSize) keys.push_back(add(t));
+  // Then, insert until we reach min load factor.
+  double lf = t.load_factor();
+  while (lf <= t.load_factor()) keys.push_back(add(t));
+
+  // We are now at min load factor. Take a snapshot.
+  size_t perfect = 0;
+  auto update_perfect = [&](Key k) {
+    perfect += GetHashtableDebugNumProbes(t, k) == 0;
+  };
+  for (const auto& k : keys) update_perfect(k);
+
+  std::vector<double> perfect_ratios;
+  // Keep going until we hit max load factor.
+  while (t.load_factor() < .6) {
+    perfect_ratios.push_back(1.0 * perfect / t.size());
+    update_perfect(add(t));
+  }
+  while (t.load_factor() > .5) {
+    perfect_ratios.push_back(1.0 * perfect / t.size());
+    update_perfect(add(t));
+  }
+  return perfect_ratios;
+}
+
+std::vector<std::pair<double, double>> StringTablePefectRatios() {
+  constexpr bool kRandomizesInserts =
+#if NDEBUG
+      false;
+#else   // NDEBUG
+      true;
+#endif  // NDEBUG
+
+  // The effective load factor is larger in non-opt mode because we insert
+  // elements out of order.
+  switch (container_internal::Group::kWidth) {
+    case 8:
+      if (kRandomizesInserts) {
+        return {{0.986, 0.02}, {0.95, 0.02}, {0.89, 0.02}};
+      } else {
+        return {{0.995, 0.01}, {0.97, 0.01}, {0.89, 0.02}};
+      }
+      break;
+    case 16:
+      if (kRandomizesInserts) {
+        return {{0.973, 0.01}, {0.965, 0.01}, {0.92, 0.02}};
+      } else {
+        return {{0.995, 0.005}, {0.99, 0.005}, {0.94, 0.01}};
+      }
+      break;
+    default:
+      // Ignore anything else.
+      return {};
+  }
+}
+
+// This is almost a change detector, but it allows us to know how we are
+// affecting the probe distribution.
+TEST(Table, EffectiveLoadFactorStrings) {
+  std::vector<double> perfect_ratios =
+      CollectPerfectRatios(StringTable(), [](StringTable& t) {
+        return t.emplace(std::to_string(t.size()), "").first->first;
+      });
+
+  auto ratios = StringTablePefectRatios();
+  if (ratios.empty()) return;
+
+  EXPECT_THAT(perfect_ratios.front(),
+              DoubleNear(ratios[0].first, ratios[0].second));
+  EXPECT_THAT(perfect_ratios[perfect_ratios.size() / 2],
+              DoubleNear(ratios[1].first, ratios[1].second));
+  EXPECT_THAT(perfect_ratios.back(),
+              DoubleNear(ratios[2].first, ratios[2].second));
+}
+
+std::vector<std::pair<double, double>> IntTablePefectRatios() {
+  constexpr bool kRandomizesInserts =
+#ifdef NDEBUG
+      false;
+#else   // NDEBUG
+      true;
+#endif  // NDEBUG
+
+  // The effective load factor is larger in non-opt mode because we insert
+  // elements out of order.
+  switch (container_internal::Group::kWidth) {
+    case 8:
+      if (kRandomizesInserts) {
+        return {{0.99, 0.02}, {0.985, 0.02}, {0.95, 0.05}};
+      } else {
+        return {{0.99, 0.01}, {0.99, 0.01}, {0.95, 0.02}};
+      }
+      break;
+    case 16:
+      if (kRandomizesInserts) {
+        return {{0.98, 0.02}, {0.978, 0.02}, {0.96, 0.02}};
+      } else {
+        return {{0.998, 0.003}, {0.995, 0.01}, {0.975, 0.02}};
+      }
+      break;
+    default:
+      // Ignore anything else.
+      return {};
+  }
+}
+
+// This is almost a change detector, but it allows us to know how we are
+// affecting the probe distribution.
+TEST(Table, EffectiveLoadFactorInts) {
+  std::vector<double> perfect_ratios = CollectPerfectRatios(
+      IntTable(), [](IntTable& t) { return *t.emplace(t.size()).first; });
+
+  auto ratios = IntTablePefectRatios();
+  if (ratios.empty()) return;
+
+  EXPECT_THAT(perfect_ratios.front(),
+              DoubleNear(ratios[0].first, ratios[0].second));
+  EXPECT_THAT(perfect_ratios[perfect_ratios.size() / 2],
+              DoubleNear(ratios[1].first, ratios[1].second));
+  EXPECT_THAT(perfect_ratios.back(),
+              DoubleNear(ratios[2].first, ratios[2].second));
+}
+
+// Confirm that we assert if we try to erase() end().
+TEST(TableDeathTest, EraseOfEndAsserts) {
+  // Use an assert with side-effects to figure out if they are actually enabled.
+  bool assert_enabled = false;
+  assert([&]() {
+    assert_enabled = true;
+    return true;
+  }());
+  if (!assert_enabled) return;
+
+  IntTable t;
+  // Extra simple "regexp" as regexp support is highly varied across platforms.
+  constexpr char kDeathMsg[] = "it != end";
+  EXPECT_DEATH_IF_SUPPORTED(t.erase(t.end()), kDeathMsg);
+}
+
+#ifdef ADDRESS_SANITIZER
+TEST(Sanitizer, PoisoningUnused) {
+  IntTable t;
+  // Insert something to force an allocation.
+  int64_t& v1 = *t.insert(0).first;
+
+  // Make sure there is something to test.
+  ASSERT_GT(t.capacity(), 1);
+
+  int64_t* slots = RawHashSetTestOnlyAccess::GetSlots(t);
+  for (size_t i = 0; i < t.capacity(); ++i) {
+    EXPECT_EQ(slots + i != &v1, __asan_address_is_poisoned(slots + i));
+  }
+}
+
+TEST(Sanitizer, PoisoningOnErase) {
+  IntTable t;
+  int64_t& v = *t.insert(0).first;
+
+  EXPECT_FALSE(__asan_address_is_poisoned(&v));
+  t.erase(0);
+  EXPECT_TRUE(__asan_address_is_poisoned(&v));
+}
+#endif  // ADDRESS_SANITIZER
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/test_instance_tracker.cc b/absl/container/internal/test_instance_tracker.cc
index fe00aca..b18e0bb 100644
--- a/absl/container/internal/test_instance_tracker.cc
+++ b/absl/container/internal/test_instance_tracker.cc
@@ -21,6 +21,7 @@
 int BaseCountedInstance::num_moves_ = 0;
 int BaseCountedInstance::num_copies_ = 0;
 int BaseCountedInstance::num_swaps_ = 0;
+int BaseCountedInstance::num_comparisons_ = 0;
 
 }  // namespace test_internal
 }  // namespace absl
diff --git a/absl/container/internal/test_instance_tracker.h b/absl/container/internal/test_instance_tracker.h
index cf8f3a5..ec45f57 100644
--- a/absl/container/internal/test_instance_tracker.h
+++ b/absl/container/internal/test_instance_tracker.h
@@ -22,8 +22,8 @@
 namespace test_internal {
 
 // A type that counts number of occurences of the type, the live occurrences of
-// the type, as well as the number of copies, moves, and swaps that have
-// occurred on the type. This is used as a base class for the copyable,
+// the type, as well as the number of copies, moves, swaps, and comparisons that
+// have occurred on the type. This is used as a base class for the copyable,
 // copyable+movable, and movable types below that are used in actual tests. Use
 // InstanceTracker in tests to track the number of instances.
 class BaseCountedInstance {
@@ -66,6 +66,36 @@
     return *this;
   }
 
+  bool operator==(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ == x.value_;
+  }
+
+  bool operator!=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ != x.value_;
+  }
+
+  bool operator<(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ < x.value_;
+  }
+
+  bool operator>(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ > x.value_;
+  }
+
+  bool operator<=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ <= x.value_;
+  }
+
+  bool operator>=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ >= x.value_;
+  }
+
   int value() const {
     if (!is_live_) std::abort();
     return value_;
@@ -108,6 +138,9 @@
 
   // Number of times that BaseCountedInstance objects were swapped.
   static int num_swaps_;
+
+  // Number of times that BaseCountedInstance objects were compared.
+  static int num_comparisons_;
 };
 
 // Helper to track the BaseCountedInstance instance counters. Expects that the
@@ -152,13 +185,21 @@
   // construction or the last call to ResetCopiesMovesSwaps().
   int swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; }
 
-  // Resets the base values for moves, copies and swaps to the current values,
-  // so that subsequent Get*() calls for moves, copies and swaps will compare to
-  // the situation at the point of this call.
+  // Returns the number of comparisons on BaseCountedInstance objects since
+  // construction or the last call to ResetCopiesMovesSwaps().
+  int comparisons() const {
+    return BaseCountedInstance::num_comparisons_ - start_comparisons_;
+  }
+
+  // Resets the base values for moves, copies, comparisons, and swaps to the
+  // current values, so that subsequent Get*() calls for moves, copies,
+  // comparisons, and swaps will compare to the situation at the point of this
+  // call.
   void ResetCopiesMovesSwaps() {
     start_moves_ = BaseCountedInstance::num_moves_;
     start_copies_ = BaseCountedInstance::num_copies_;
     start_swaps_ = BaseCountedInstance::num_swaps_;
+    start_comparisons_ = BaseCountedInstance::num_comparisons_;
   }
 
  private:
@@ -167,6 +208,7 @@
   int start_moves_;
   int start_copies_;
   int start_swaps_;
+  int start_comparisons_;
 };
 
 // Copyable, not movable.
diff --git a/absl/container/internal/test_instance_tracker_test.cc b/absl/container/internal/test_instance_tracker_test.cc
index 9efb677..0ae5763 100644
--- a/absl/container/internal/test_instance_tracker_test.cc
+++ b/absl/container/internal/test_instance_tracker_test.cc
@@ -157,4 +157,26 @@
   EXPECT_EQ(1, tracker.moves());
 }
 
+TEST(TestInstanceTracker, Comparisons) {
+  InstanceTracker tracker;
+  MovableOnlyInstance one(1), two(2);
+
+  EXPECT_EQ(0, tracker.comparisons());
+  EXPECT_FALSE(one == two);
+  EXPECT_EQ(1, tracker.comparisons());
+  EXPECT_TRUE(one != two);
+  EXPECT_EQ(2, tracker.comparisons());
+  EXPECT_TRUE(one < two);
+  EXPECT_EQ(3, tracker.comparisons());
+  EXPECT_FALSE(one > two);
+  EXPECT_EQ(4, tracker.comparisons());
+  EXPECT_TRUE(one <= two);
+  EXPECT_EQ(5, tracker.comparisons());
+  EXPECT_FALSE(one >= two);
+  EXPECT_EQ(6, tracker.comparisons());
+
+  tracker.ResetCopiesMovesSwaps();
+  EXPECT_EQ(0, tracker.comparisons());
+}
+
 }  // namespace
diff --git a/absl/container/internal/tracked.h b/absl/container/internal/tracked.h
new file mode 100644
index 0000000..7d14af0
--- /dev/null
+++ b/absl/container/internal/tracked.h
@@ -0,0 +1,78 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_TRACKED_H_
+#define ABSL_CONTAINER_INTERNAL_TRACKED_H_
+
+#include <stddef.h>
+#include <memory>
+#include <utility>
+
+namespace absl {
+namespace container_internal {
+
+// A class that tracks its copies and moves so that it can be queried in tests.
+template <class T>
+class Tracked {
+ public:
+  Tracked() {}
+  // NOLINTNEXTLINE(runtime/explicit)
+  Tracked(const T& val) : val_(val) {}
+  Tracked(const Tracked& that)
+      : val_(that.val_),
+        num_moves_(that.num_moves_),
+        num_copies_(that.num_copies_) {
+    ++(*num_copies_);
+  }
+  Tracked(Tracked&& that)
+      : val_(std::move(that.val_)),
+        num_moves_(std::move(that.num_moves_)),
+        num_copies_(std::move(that.num_copies_)) {
+    ++(*num_moves_);
+  }
+  Tracked& operator=(const Tracked& that) {
+    val_ = that.val_;
+    num_moves_ = that.num_moves_;
+    num_copies_ = that.num_copies_;
+    ++(*num_copies_);
+  }
+  Tracked& operator=(Tracked&& that) {
+    val_ = std::move(that.val_);
+    num_moves_ = std::move(that.num_moves_);
+    num_copies_ = std::move(that.num_copies_);
+    ++(*num_moves_);
+  }
+
+  const T& val() const { return val_; }
+
+  friend bool operator==(const Tracked& a, const Tracked& b) {
+    return a.val_ == b.val_;
+  }
+  friend bool operator!=(const Tracked& a, const Tracked& b) {
+    return !(a == b);
+  }
+
+  size_t num_copies() { return *num_copies_; }
+  size_t num_moves() { return *num_moves_; }
+
+ private:
+  T val_;
+  std::shared_ptr<size_t> num_moves_ = std::make_shared<size_t>(0);
+  std::shared_ptr<size_t> num_copies_ = std::make_shared<size_t>(0);
+};
+
+}  // namespace container_internal
+}  // namespace absl
+
+#endif  // ABSL_CONTAINER_INTERNAL_TRACKED_H_
diff --git a/absl/container/internal/unordered_map_constructor_test.h b/absl/container/internal/unordered_map_constructor_test.h
new file mode 100644
index 0000000..2ffb646
--- /dev/null
+++ b/absl/container/internal/unordered_map_constructor_test.h
@@ -0,0 +1,404 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
+
+#include <algorithm>
+#include <vector>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordMap>
+class ConstructorTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(ConstructorTest);
+
+TYPED_TEST_P(ConstructorTest, NoArgs) {
+  TypeParam m;
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCount) {
+  TypeParam m(123);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHash) {
+  using H = typename TypeParam::hasher;
+  H hasher;
+  TypeParam m(123, hasher);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  H hasher;
+  E equal;
+  TypeParam m(123, hasher, equal);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  TypeParam m(123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  TypeParam m(123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, BucketAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  TypeParam m(alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(m, ::testing::UnorderedElementsAre());
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end(), 123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, CopyConstructor) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam n(m);
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam n(m, A(11));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_NE(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+#endif
+}
+
+// TODO(alkis): Test non-propagating allocators on copy constructors.
+
+TYPED_TEST_P(ConstructorTest, MoveConstructor) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam t(m);
+  TypeParam n(std::move(t));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam t(m);
+  TypeParam n(std::move(t), A(1));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_NE(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+#endif
+}
+
+// TODO(alkis): Test non-propagating allocators on move constructors.
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(values, 123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using A = typename TypeParam::allocator_type;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  A alloc(0);
+  TypeParam m(values, 123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
+#if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m(values, 123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, Assignment) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
+  TypeParam n;
+  n = m;
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m, n);
+}
+
+// TODO(alkis): Test [non-]propagating allocators on move/copy assignments
+// (it depends on traits).
+
+TYPED_TEST_P(ConstructorTest, MoveAssignment) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
+  TypeParam t(m);
+  TypeParam n;
+  n = std::move(t);
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m;
+  m = values;
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()});
+  TypeParam n({gen()});
+  n = m;
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()});
+  TypeParam t(m);
+  TypeParam n({gen()});
+  n = std::move(t);
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m;
+  m = values;
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m(values);
+  m = *&m;  // Avoid -Wself-assign
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+// We cannot test self move as standard states that it leaves standard
+// containers in unspecified state (and in practice in causes memory-leak
+// according to heap-checker!).
+
+REGISTER_TYPED_TEST_CASE_P(
+    ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
+    BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc,
+    BucketAlloc, InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
+    InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
+    MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
+    InitializerListBucketAlloc, InitializerListBucketHashAlloc, Assignment,
+    MoveAssignment, AssignmentFromInitializerList,
+    AssignmentOverwritesExisting, MoveAssignmentOverwritesExisting,
+    AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
diff --git a/absl/container/internal/unordered_map_lookup_test.h b/absl/container/internal/unordered_map_lookup_test.h
new file mode 100644
index 0000000..1f1b6b4
--- /dev/null
+++ b/absl/container/internal/unordered_map_lookup_test.h
@@ -0,0 +1,114 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordMap>
+class LookupTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(LookupTest);
+
+TYPED_TEST_P(LookupTest, At) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  for (const auto& p : values) {
+    const auto& val = m.at(p.first);
+    EXPECT_EQ(p.second, val) << ::testing::PrintToString(p.first);
+  }
+}
+
+TYPED_TEST_P(LookupTest, OperatorBracket) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& p : values) {
+    auto& val = m[p.first];
+    EXPECT_EQ(V(), val) << ::testing::PrintToString(p.first);
+    val = p.second;
+  }
+  for (const auto& p : values)
+    EXPECT_EQ(p.second, m[p.first]) << ::testing::PrintToString(p.first);
+}
+
+TYPED_TEST_P(LookupTest, Count) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& p : values)
+    EXPECT_EQ(0, m.count(p.first)) << ::testing::PrintToString(p.first);
+  m.insert(values.begin(), values.end());
+  for (const auto& p : values)
+    EXPECT_EQ(1, m.count(p.first)) << ::testing::PrintToString(p.first);
+}
+
+TYPED_TEST_P(LookupTest, Find) {
+  using std::get;
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& p : values)
+    EXPECT_TRUE(m.end() == m.find(p.first))
+        << ::testing::PrintToString(p.first);
+  m.insert(values.begin(), values.end());
+  for (const auto& p : values) {
+    auto it = m.find(p.first);
+    EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(p.first);
+    EXPECT_EQ(p.second, get<1>(*it)) << ::testing::PrintToString(p.first);
+  }
+}
+
+TYPED_TEST_P(LookupTest, EqualRange) {
+  using std::get;
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& p : values) {
+    auto r = m.equal_range(p.first);
+    ASSERT_EQ(0, std::distance(r.first, r.second));
+  }
+  m.insert(values.begin(), values.end());
+  for (const auto& p : values) {
+    auto r = m.equal_range(p.first);
+    ASSERT_EQ(1, std::distance(r.first, r.second));
+    EXPECT_EQ(p.second, get<1>(*r.first)) << ::testing::PrintToString(p.first);
+  }
+}
+
+REGISTER_TYPED_TEST_CASE_P(LookupTest, At, OperatorBracket, Count, Find,
+                           EqualRange);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
diff --git a/absl/container/internal/unordered_map_modifiers_test.h b/absl/container/internal/unordered_map_modifiers_test.h
new file mode 100644
index 0000000..b6c633a
--- /dev/null
+++ b/absl/container/internal/unordered_map_modifiers_test.h
@@ -0,0 +1,272 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordMap>
+class ModifiersTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(ModifiersTest);
+
+TYPED_TEST_P(ModifiersTest, Clear) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  m.clear();
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
+  EXPECT_TRUE(m.empty());
+}
+
+TYPED_TEST_P(ModifiersTest, Insert) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  auto p = m.insert(val);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(val, *p.first);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  p = m.insert(val2);
+  EXPECT_FALSE(p.second);
+  EXPECT_EQ(val, *p.first);
+}
+
+TYPED_TEST_P(ModifiersTest, InsertHint) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  auto it = m.insert(m.end(), val);
+  EXPECT_TRUE(it != m.end());
+  EXPECT_EQ(val, *it);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  it = m.insert(it, val2);
+  EXPECT_TRUE(it != m.end());
+  EXPECT_EQ(val, *it);
+}
+
+TYPED_TEST_P(ModifiersTest, InsertRange) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  m.insert(values.begin(), values.end());
+  ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ModifiersTest, InsertOrAssign) {
+#ifdef UNORDERED_MAP_CXX17
+  using std::get;
+  using K = typename TypeParam::key_type;
+  using V = typename TypeParam::mapped_type;
+  K k = hash_internal::Generator<K>()();
+  V val = hash_internal::Generator<V>()();
+  TypeParam m;
+  auto p = m.insert_or_assign(k, val);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(k, get<0>(*p.first));
+  EXPECT_EQ(val, get<1>(*p.first));
+  V val2 = hash_internal::Generator<V>()();
+  p = m.insert_or_assign(k, val2);
+  EXPECT_FALSE(p.second);
+  EXPECT_EQ(k, get<0>(*p.first));
+  EXPECT_EQ(val2, get<1>(*p.first));
+#endif
+}
+
+TYPED_TEST_P(ModifiersTest, InsertOrAssignHint) {
+#ifdef UNORDERED_MAP_CXX17
+  using std::get;
+  using K = typename TypeParam::key_type;
+  using V = typename TypeParam::mapped_type;
+  K k = hash_internal::Generator<K>()();
+  V val = hash_internal::Generator<V>()();
+  TypeParam m;
+  auto it = m.insert_or_assign(m.end(), k, val);
+  EXPECT_TRUE(it != m.end());
+  EXPECT_EQ(k, get<0>(*it));
+  EXPECT_EQ(val, get<1>(*it));
+  V val2 = hash_internal::Generator<V>()();
+  it = m.insert_or_assign(it, k, val2);
+  EXPECT_EQ(k, get<0>(*it));
+  EXPECT_EQ(val2, get<1>(*it));
+#endif
+}
+
+TYPED_TEST_P(ModifiersTest, Emplace) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto p = m.emplace(val);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(val, *p.first);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  p = m.emplace(val2);
+  EXPECT_FALSE(p.second);
+  EXPECT_EQ(val, *p.first);
+}
+
+TYPED_TEST_P(ModifiersTest, EmplaceHint) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto it = m.emplace_hint(m.end(), val);
+  EXPECT_EQ(val, *it);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  it = m.emplace_hint(it, val2);
+  EXPECT_EQ(val, *it);
+}
+
+TYPED_TEST_P(ModifiersTest, TryEmplace) {
+#ifdef UNORDERED_MAP_CXX17
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto p = m.try_emplace(val.first, val.second);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(val, *p.first);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  p = m.try_emplace(val2.first, val2.second);
+  EXPECT_FALSE(p.second);
+  EXPECT_EQ(val, *p.first);
+#endif
+}
+
+TYPED_TEST_P(ModifiersTest, TryEmplaceHint) {
+#ifdef UNORDERED_MAP_CXX17
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using V = typename TypeParam::mapped_type;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto it = m.try_emplace(m.end(), val.first, val.second);
+  EXPECT_EQ(val, *it);
+  T val2 = {val.first, hash_internal::Generator<V>()()};
+  it = m.try_emplace(it, val2.first, val2.second);
+  EXPECT_EQ(val, *it);
+#endif
+}
+
+template <class V>
+using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
+
+// In openmap we chose not to return the iterator from erase because that's
+// more expensive. As such we adapt erase to return an iterator here.
+struct EraseFirst {
+  template <class Map>
+  auto operator()(Map* m, int) const
+      -> IfNotVoid<decltype(m->erase(m->begin()))> {
+    return m->erase(m->begin());
+  }
+  template <class Map>
+  typename Map::iterator operator()(Map* m, ...) const {
+    auto it = m->begin();
+    m->erase(it++);
+    return it;
+  }
+};
+
+TYPED_TEST_P(ModifiersTest, Erase) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using std::get;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  auto& first = *m.begin();
+  std::vector<T> values2;
+  for (const auto& val : values)
+    if (get<0>(val) != get<0>(first)) values2.push_back(val);
+  auto it = EraseFirst()(&m, 0);
+  ASSERT_TRUE(it != m.end());
+  EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values2.begin(),
+                                                             values2.end()));
+}
+
+TYPED_TEST_P(ModifiersTest, EraseRange) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  auto it = m.erase(m.begin(), m.end());
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
+  EXPECT_TRUE(it == m.end());
+}
+
+TYPED_TEST_P(ModifiersTest, EraseKey) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_EQ(1, m.erase(values[0].first));
+  EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
+  EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
+                                                             values.end()));
+}
+
+TYPED_TEST_P(ModifiersTest, Swap) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> v1;
+  std::vector<T> v2;
+  std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
+  std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
+  TypeParam m1(v1.begin(), v1.end());
+  TypeParam m2(v2.begin(), v2.end());
+  EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v1));
+  EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v2));
+  m1.swap(m2);
+  EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v2));
+  EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v1));
+}
+
+// TODO(alkis): Write tests for extract.
+// TODO(alkis): Write tests for merge.
+
+REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
+                           InsertRange, InsertOrAssign, InsertOrAssignHint,
+                           Emplace, EmplaceHint, TryEmplace, TryEmplaceHint,
+                           Erase, EraseRange, EraseKey, Swap);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
diff --git a/absl/container/internal/unordered_map_test.cc b/absl/container/internal/unordered_map_test.cc
new file mode 100644
index 0000000..40e799c
--- /dev/null
+++ b/absl/container/internal/unordered_map_test.cc
@@ -0,0 +1,38 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <unordered_map>
+
+#include "absl/container/internal/unordered_map_constructor_test.h"
+#include "absl/container/internal/unordered_map_lookup_test.h"
+#include "absl/container/internal/unordered_map_modifiers_test.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using MapTypes = ::testing::Types<
+    std::unordered_map<int, int, StatefulTestingHash, StatefulTestingEqual,
+                       Alloc<std::pair<const int, int>>>,
+    std::unordered_map<std::string, std::string, StatefulTestingHash,
+                       StatefulTestingEqual,
+                       Alloc<std::pair<const std::string, std::string>>>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedMap, ConstructorTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedMap, LookupTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedMap, ModifiersTest, MapTypes);
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/internal/unordered_set_constructor_test.h b/absl/container/internal/unordered_set_constructor_test.h
new file mode 100644
index 0000000..cb59370
--- /dev/null
+++ b/absl/container/internal/unordered_set_constructor_test.h
@@ -0,0 +1,408 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
+
+#include <algorithm>
+#include <vector>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordMap>
+class ConstructorTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(ConstructorTest);
+
+TYPED_TEST_P(ConstructorTest, NoArgs) {
+  TypeParam m;
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCount) {
+  TypeParam m(123);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHash) {
+  using H = typename TypeParam::hasher;
+  H hasher;
+  TypeParam m(123, hasher);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  H hasher;
+  E equal;
+  TypeParam m(123, hasher, equal);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+
+  const auto& cm = m;
+  EXPECT_EQ(cm.hash_function(), hasher);
+  EXPECT_EQ(cm.key_eq(), equal);
+  EXPECT_EQ(cm.get_allocator(), alloc);
+  EXPECT_TRUE(cm.empty());
+  EXPECT_THAT(keys(cm), ::testing::UnorderedElementsAre());
+  EXPECT_GE(cm.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  TypeParam m(123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  TypeParam m(123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, BucketAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  TypeParam m(alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_TRUE(m.empty());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  std::vector<T> values;
+  for (size_t i = 0; i != 10; ++i)
+    values.push_back(hash_internal::Generator<T>()());
+  TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using A = typename TypeParam::allocator_type;
+  A alloc(0);
+  std::vector<T> values;
+  for (size_t i = 0; i != 10; ++i)
+    values.push_back(hash_internal::Generator<T>()());
+  TypeParam m(values.begin(), values.end(), 123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  std::vector<T> values;
+  for (size_t i = 0; i != 10; ++i)
+    values.push_back(hash_internal::Generator<T>()());
+  TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, CopyConstructor) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam n(m);
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam n(m, A(11));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_NE(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+#endif
+}
+
+// TODO(alkis): Test non-propagating allocators on copy constructors.
+
+TYPED_TEST_P(ConstructorTest, MoveConstructor) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam t(m);
+  TypeParam n(std::move(t));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
+#if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(123, hasher, equal, alloc);
+  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
+  TypeParam t(m);
+  TypeParam n(std::move(t), A(1));
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_NE(m.get_allocator(), n.get_allocator());
+  EXPECT_EQ(m, n);
+#endif
+}
+
+// TODO(alkis): Test non-propagating allocators on move constructors.
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  TypeParam m(values, 123, hasher, equal, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.key_eq(), equal);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+}
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using A = typename TypeParam::allocator_type;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  A alloc(0);
+  TypeParam m(values, 123, alloc);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
+#if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m(values, 123, hasher, alloc);
+  EXPECT_EQ(m.hash_function(), hasher);
+  EXPECT_EQ(m.get_allocator(), alloc);
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_GE(m.bucket_count(), 123);
+#endif
+}
+
+TYPED_TEST_P(ConstructorTest, Assignment) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
+  TypeParam n;
+  n = m;
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m, n);
+}
+
+// TODO(alkis): Test [non-]propagating allocators on move/copy assignments
+// (it depends on traits).
+
+TYPED_TEST_P(ConstructorTest, MoveAssignment) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  using H = typename TypeParam::hasher;
+  using E = typename TypeParam::key_equal;
+  using A = typename TypeParam::allocator_type;
+  H hasher;
+  E equal;
+  A alloc(0);
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
+  TypeParam t(m);
+  TypeParam n;
+  n = std::move(t);
+  EXPECT_EQ(m.hash_function(), n.hash_function());
+  EXPECT_EQ(m.key_eq(), n.key_eq());
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m;
+  m = values;
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()});
+  TypeParam n({gen()});
+  n = m;
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  TypeParam m({gen(), gen(), gen()});
+  TypeParam t(m);
+  TypeParam n({gen()});
+  n = std::move(t);
+  EXPECT_EQ(m, n);
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m;
+  m = values;
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  hash_internal::Generator<T> gen;
+  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
+  TypeParam m(values);
+  m = *&m;  // Avoid -Wself-assign.
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+REGISTER_TYPED_TEST_CASE_P(
+    ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
+    BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc,
+    BucketAlloc, InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
+    InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
+    MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
+    InitializerListBucketAlloc, InitializerListBucketHashAlloc, Assignment,
+    MoveAssignment, AssignmentFromInitializerList,
+    AssignmentOverwritesExisting, MoveAssignmentOverwritesExisting,
+    AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
diff --git a/absl/container/internal/unordered_set_lookup_test.h b/absl/container/internal/unordered_set_lookup_test.h
new file mode 100644
index 0000000..aca9c6a
--- /dev/null
+++ b/absl/container/internal/unordered_set_lookup_test.h
@@ -0,0 +1,88 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordSet>
+class LookupTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(LookupTest);
+
+TYPED_TEST_P(LookupTest, Count) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& v : values)
+    EXPECT_EQ(0, m.count(v)) << ::testing::PrintToString(v);
+  m.insert(values.begin(), values.end());
+  for (const auto& v : values)
+    EXPECT_EQ(1, m.count(v)) << ::testing::PrintToString(v);
+}
+
+TYPED_TEST_P(LookupTest, Find) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& v : values)
+    EXPECT_TRUE(m.end() == m.find(v)) << ::testing::PrintToString(v);
+  m.insert(values.begin(), values.end());
+  for (const auto& v : values) {
+    typename TypeParam::iterator it = m.find(v);
+    static_assert(std::is_same<const typename TypeParam::value_type&,
+                               decltype(*it)>::value,
+                  "");
+    static_assert(std::is_same<const typename TypeParam::value_type*,
+                               decltype(it.operator->())>::value,
+                  "");
+    EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(v);
+    EXPECT_EQ(v, *it) << ::testing::PrintToString(v);
+  }
+}
+
+TYPED_TEST_P(LookupTest, EqualRange) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  for (const auto& v : values) {
+    auto r = m.equal_range(v);
+    ASSERT_EQ(0, std::distance(r.first, r.second));
+  }
+  m.insert(values.begin(), values.end());
+  for (const auto& v : values) {
+    auto r = m.equal_range(v);
+    ASSERT_EQ(1, std::distance(r.first, r.second));
+    EXPECT_EQ(v, *r.first);
+  }
+}
+
+REGISTER_TYPED_TEST_CASE_P(LookupTest, Count, Find, EqualRange);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_
diff --git a/absl/container/internal/unordered_set_modifiers_test.h b/absl/container/internal/unordered_set_modifiers_test.h
new file mode 100644
index 0000000..9beacf3
--- /dev/null
+++ b/absl/container/internal/unordered_set_modifiers_test.h
@@ -0,0 +1,187 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
+#define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/internal/hash_generator_testing.h"
+#include "absl/container/internal/hash_policy_testing.h"
+
+namespace absl {
+namespace container_internal {
+
+template <class UnordSet>
+class ModifiersTest : public ::testing::Test {};
+
+TYPED_TEST_CASE_P(ModifiersTest);
+
+TYPED_TEST_P(ModifiersTest, Clear) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  m.clear();
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_TRUE(m.empty());
+}
+
+TYPED_TEST_P(ModifiersTest, Insert) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  auto p = m.insert(val);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(val, *p.first);
+  p = m.insert(val);
+  EXPECT_FALSE(p.second);
+}
+
+TYPED_TEST_P(ModifiersTest, InsertHint) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  auto it = m.insert(m.end(), val);
+  EXPECT_TRUE(it != m.end());
+  EXPECT_EQ(val, *it);
+  it = m.insert(it, val);
+  EXPECT_TRUE(it != m.end());
+  EXPECT_EQ(val, *it);
+}
+
+TYPED_TEST_P(ModifiersTest, InsertRange) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m;
+  m.insert(values.begin(), values.end());
+  ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+}
+
+TYPED_TEST_P(ModifiersTest, Emplace) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto p = m.emplace(val);
+  EXPECT_TRUE(p.second);
+  EXPECT_EQ(val, *p.first);
+  p = m.emplace(val);
+  EXPECT_FALSE(p.second);
+  EXPECT_EQ(val, *p.first);
+}
+
+TYPED_TEST_P(ModifiersTest, EmplaceHint) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  T val = hash_internal::Generator<T>()();
+  TypeParam m;
+  // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
+  // with test traits/policy.
+  auto it = m.emplace_hint(m.end(), val);
+  EXPECT_EQ(val, *it);
+  it = m.emplace_hint(it, val);
+  EXPECT_EQ(val, *it);
+}
+
+template <class V>
+using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
+
+// In openmap we chose not to return the iterator from erase because that's
+// more expensive. As such we adapt erase to return an iterator here.
+struct EraseFirst {
+  template <class Map>
+  auto operator()(Map* m, int) const
+      -> IfNotVoid<decltype(m->erase(m->begin()))> {
+    return m->erase(m->begin());
+  }
+  template <class Map>
+  typename Map::iterator operator()(Map* m, ...) const {
+    auto it = m->begin();
+    m->erase(it++);
+    return it;
+  }
+};
+
+TYPED_TEST_P(ModifiersTest, Erase) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  std::vector<T> values2;
+  for (const auto& val : values)
+    if (val != *m.begin()) values2.push_back(val);
+  auto it = EraseFirst()(&m, 0);
+  ASSERT_TRUE(it != m.end());
+  EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values2.begin(),
+                                                            values2.end()));
+}
+
+TYPED_TEST_P(ModifiersTest, EraseRange) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  auto it = m.erase(m.begin(), m.end());
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
+  EXPECT_TRUE(it == m.end());
+}
+
+TYPED_TEST_P(ModifiersTest, EraseKey) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> values;
+  std::generate_n(std::back_inserter(values), 10,
+                  hash_internal::Generator<T>());
+  TypeParam m(values.begin(), values.end());
+  ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
+  EXPECT_EQ(1, m.erase(values[0]));
+  EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
+  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
+                                                            values.end()));
+}
+
+TYPED_TEST_P(ModifiersTest, Swap) {
+  using T = hash_internal::GeneratedType<TypeParam>;
+  std::vector<T> v1;
+  std::vector<T> v2;
+  std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
+  std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
+  TypeParam m1(v1.begin(), v1.end());
+  TypeParam m2(v2.begin(), v2.end());
+  EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v1));
+  EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v2));
+  m1.swap(m2);
+  EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v2));
+  EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v1));
+}
+
+// TODO(alkis): Write tests for extract.
+// TODO(alkis): Write tests for merge.
+
+REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
+                           InsertRange, Emplace, EmplaceHint, Erase, EraseRange,
+                           EraseKey, Swap);
+
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
diff --git a/absl/container/internal/unordered_set_test.cc b/absl/container/internal/unordered_set_test.cc
new file mode 100644
index 0000000..1281ce5
--- /dev/null
+++ b/absl/container/internal/unordered_set_test.cc
@@ -0,0 +1,37 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <unordered_set>
+
+#include "absl/container/internal/unordered_set_constructor_test.h"
+#include "absl/container/internal/unordered_set_lookup_test.h"
+#include "absl/container/internal/unordered_set_modifiers_test.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using SetTypes =
+    ::testing::Types<std::unordered_set<int, StatefulTestingHash,
+                                        StatefulTestingEqual, Alloc<int>>,
+                     std::unordered_set<std::string, StatefulTestingHash,
+                                        StatefulTestingEqual, Alloc<std::string>>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedSet, ConstructorTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedSet, LookupTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(UnorderedSet, ModifiersTest, SetTypes);
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h
new file mode 100644
index 0000000..6369ec3
--- /dev/null
+++ b/absl/container/node_hash_map.h
@@ -0,0 +1,530 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: node_hash_map.h
+// -----------------------------------------------------------------------------
+//
+// An `absl::node_hash_map<K, V>` is an unordered associative container of
+// unique keys and associated values designed to be a more efficient replacement
+// for `std::unordered_map`. Like `unordered_map`, search, insertion, and
+// deletion of map elements can be done as an `O(1)` operation. However,
+// `node_hash_map` (and other unordered associative containers known as the
+// collection of Abseil "Swiss tables") contain other optimizations that result
+// in both memory and computation advantages.
+//
+// In most cases, your default choice for a hash map should be a map of type
+// `flat_hash_map`. However, if you need pointer stability and cannot store
+// a `flat_hash_map` with `unique_ptr` elements, a `node_hash_map` may be a
+// valid alternative. As well, if you are migrating your code from using
+// `std::unordered_map`, a `node_hash_map` provides a more straightforward
+// migration, because it guarantees pointer stability. Consider migrating to
+// `node_hash_map` and perhaps converting to a more efficient `flat_hash_map`
+// upon further review.
+
+#ifndef ABSL_CONTAINER_NODE_HASH_MAP_H_
+#define ABSL_CONTAINER_NODE_HASH_MAP_H_
+
+#include <tuple>
+#include <type_traits>
+#include <utility>
+
+#include "absl/container/internal/container_memory.h"
+#include "absl/container/internal/hash_function_defaults.h"  // IWYU pragma: export
+#include "absl/container/internal/node_hash_policy.h"
+#include "absl/container/internal/raw_hash_map.h"  // IWYU pragma: export
+#include "absl/memory/memory.h"
+
+namespace absl {
+namespace container_internal {
+template <class Key, class Value>
+class NodeHashMapPolicy;
+}  // namespace container_internal
+
+// -----------------------------------------------------------------------------
+// absl::node_hash_map
+// -----------------------------------------------------------------------------
+//
+// An `absl::node_hash_map<K, V>` is an unordered associative container which
+// has been optimized for both speed and memory footprint in most common use
+// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
+// the following notable differences:
+//
+// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
+//   `insert()`, provided that the map is provided a compatible heterogeneous
+//   hashing function and equality operator.
+// * Contains a `capacity()` member function indicating the number of element
+//   slots (open, deleted, and empty) within the hash map.
+// * Returns `void` from the `erase(iterator)` overload.
+//
+// By default, `node_hash_map` uses the `absl::Hash` hashing framework.
+// All fundamental and Abseil types that support the `absl::Hash` framework have
+// a compatible equality operator for comparing insertions into `node_hash_map`.
+// If your type is not yet supported by the `asbl::Hash` framework, see
+// absl/hash/hash.h for information on extending Abseil hashing to user-defined
+// types.
+//
+// Example:
+//
+//   // Create a node hash map of three strings (that map to strings)
+//   absl::node_hash_map<std::string, std::string> ducks =
+//     {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
+//
+//  // Insert a new element into the node hash map
+//  ducks.insert({"d", "donald"}};
+//
+//  // Force a rehash of the node hash map
+//  ducks.rehash(0);
+//
+//  // Find the element with the key "b"
+//  std::string search_key = "b";
+//  auto result = ducks.find(search_key);
+//  if (result != ducks.end()) {
+//    std::cout << "Result: " << search_key->second << std::endl;
+//  }
+template <class Key, class Value,
+          class Hash = absl::container_internal::hash_default_hash<Key>,
+          class Eq = absl::container_internal::hash_default_eq<Key>,
+          class Alloc = std::allocator<std::pair<const Key, Value>>>
+class node_hash_map
+    : public absl::container_internal::raw_hash_map<
+          absl::container_internal::NodeHashMapPolicy<Key, Value>, Hash, Eq,
+          Alloc> {
+  using Base = typename node_hash_map::raw_hash_map;
+
+ public:
+  node_hash_map() {}
+  using Base::Base;
+
+  // node_hash_map::begin()
+  //
+  // Returns an iterator to the beginning of the `node_hash_map`.
+  using Base::begin;
+
+  // node_hash_map::cbegin()
+  //
+  // Returns a const iterator to the beginning of the `node_hash_map`.
+  using Base::cbegin;
+
+  // node_hash_map::cend()
+  //
+  // Returns a const iterator to the end of the `node_hash_map`.
+  using Base::cend;
+
+  // node_hash_map::end()
+  //
+  // Returns an iterator to the end of the `node_hash_map`.
+  using Base::end;
+
+  // node_hash_map::capacity()
+  //
+  // Returns the number of element slots (assigned, deleted, and empty)
+  // available within the `node_hash_map`.
+  //
+  // NOTE: this member function is particular to `absl::node_hash_map` and is
+  // not provided in the `std::unordered_map` API.
+  using Base::capacity;
+
+  // node_hash_map::empty()
+  //
+  // Returns whether or not the `node_hash_map` is empty.
+  using Base::empty;
+
+  // node_hash_map::max_size()
+  //
+  // Returns the largest theoretical possible number of elements within a
+  // `node_hash_map` under current memory constraints. This value can be thought
+  // of as the largest value of `std::distance(begin(), end())` for a
+  // `node_hash_map<K, V>`.
+  using Base::max_size;
+
+  // node_hash_map::size()
+  //
+  // Returns the number of elements currently within the `node_hash_map`.
+  using Base::size;
+
+  // node_hash_map::clear()
+  //
+  // Removes all elements from the `node_hash_map`. Invalidates any references,
+  // pointers, or iterators referring to contained elements.
+  //
+  // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
+  // the underlying buffer call `erase(begin(), end())`.
+  using Base::clear;
+
+  // node_hash_map::erase()
+  //
+  // Erases elements within the `node_hash_map`. Erasing does not trigger a
+  // rehash. Overloads are listed below.
+  //
+  // void erase(const_iterator pos):
+  //
+  //   Erases the element at `position` of the `node_hash_map`, returning
+  //   `void`.
+  //
+  //   NOTE: this return behavior is different than that of STL containers in
+  //   general and `std::unordered_map` in particular.
+  //
+  // iterator erase(const_iterator first, const_iterator last):
+  //
+  //   Erases the elements in the open interval [`first`, `last`), returning an
+  //   iterator pointing to `last`.
+  //
+  // size_type erase(const key_type& key):
+  //
+  //   Erases the element with the matching key, if it exists.
+  using Base::erase;
+
+  // node_hash_map::insert()
+  //
+  // Inserts an element of the specified value into the `node_hash_map`,
+  // returning an iterator pointing to the newly inserted element, provided that
+  // an element with the given key does not already exist. If rehashing occurs
+  // due to the insertion, all iterators are invalidated. Overloads are listed
+  // below.
+  //
+  // std::pair<iterator,bool> insert(const init_type& value):
+  //
+  //   Inserts a value into the `node_hash_map`. Returns a pair consisting of an
+  //   iterator to the inserted element (or to the element that prevented the
+  //   insertion) and a `bool` denoting whether the insertion took place.
+  //
+  // std::pair<iterator,bool> insert(T&& value):
+  // std::pair<iterator,bool> insert(init_type&& value):
+  //
+  //   Inserts a moveable value into the `node_hash_map`. Returns a `std::pair`
+  //   consisting of an iterator to the inserted element (or to the element that
+  //   prevented the insertion) and a `bool` denoting whether the insertion took
+  //   place.
+  //
+  // iterator insert(const_iterator hint, const init_type& value):
+  // iterator insert(const_iterator hint, T&& value):
+  // iterator insert(const_iterator hint, init_type&& value);
+  //
+  //   Inserts a value, using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search. Returns an iterator to the
+  //   inserted element, or to the existing element that prevented the
+  //   insertion.
+  //
+  // void insert(InputIterator first, InputIterator last):
+  //
+  //   Inserts a range of values [`first`, `last`).
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently, for `node_hash_map` we guarantee the
+  //   first match is inserted.
+  //
+  // void insert(std::initializer_list<init_type> ilist):
+  //
+  //   Inserts the elements within the initializer list `ilist`.
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently within the initializer list, for
+  //   `node_hash_map` we guarantee the first match is inserted.
+  using Base::insert;
+
+  // node_hash_map::insert_or_assign()
+  //
+  // Inserts an element of the specified value into the `node_hash_map` provided
+  // that a value with the given key does not already exist, or replaces it with
+  // the element value if a key for that value already exists, returning an
+  // iterator pointing to the newly inserted element. If rehashing occurs due to
+  // the insertion, all iterators are invalidated. Overloads are listed
+  // below.
+  //
+  // std::pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
+  // std::pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
+  //
+  //   Inserts/Assigns (or moves) the element of the specified key into the
+  //   `node_hash_map`.
+  //
+  // iterator insert_or_assign(const_iterator hint,
+  //                           const init_type& k, T&& obj):
+  // iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
+  //
+  //   Inserts/Assigns (or moves) the element of the specified key into the
+  //   `node_hash_map` using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search.
+  using Base::insert_or_assign;
+
+  // node_hash_map::emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `node_hash_map`, provided that no element with the given key
+  // already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace;
+
+  // node_hash_map::emplace_hint()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `node_hash_map`, using the position of `hint` as a non-binding
+  // suggestion for where to begin the insertion search, and only inserts
+  // provided that no element with the given key already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace_hint;
+
+  // node_hash_map::try_emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `node_hash_map`, provided that no element with the given key
+  // already exists. Unlike `emplace()`, if an element with the given key
+  // already exists, we guarantee that no element is constructed.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  // Overloads are listed below.
+  //
+  //   std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
+  //   std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
+  //
+  // Inserts (via copy or move) the element of the specified key into the
+  // `node_hash_map`.
+  //
+  //   iterator try_emplace(const_iterator hint,
+  //                        const init_type& k, Args&&... args):
+  //   iterator try_emplace(const_iterator hint, init_type&& k, Args&&... args):
+  //
+  // Inserts (via copy or move) the element of the specified key into the
+  // `node_hash_map` using the position of `hint` as a non-binding suggestion
+  // for where to begin the insertion search.
+  using Base::try_emplace;
+
+  // node_hash_map::extract()
+  //
+  // Extracts the indicated element, erasing it in the process, and returns it
+  // as a C++17-compatible node handle. Overloads are listed below.
+  //
+  // node_type extract(const_iterator position):
+  //
+  //   Extracts the key,value pair of the element at the indicated position and
+  //   returns a node handle owning that extracted data.
+  //
+  // node_type extract(const key_type& x):
+  //
+  //   Extracts the key,value pair of the element with a key matching the passed
+  //   key value and returns a node handle owning that extracted data. If the
+  //   `node_hash_map` does not contain an element with a matching key, this
+  //   function returns an empty node handle.
+  using Base::extract;
+
+  // node_hash_map::merge()
+  //
+  // Extracts elements from a given `source` node hash map into this
+  // `node_hash_map`. If the destination `node_hash_map` already contains an
+  // element with an equivalent key, that element is not extracted.
+  using Base::merge;
+
+  // node_hash_map::swap(node_hash_map& other)
+  //
+  // Exchanges the contents of this `node_hash_map` with those of the `other`
+  // node hash map, avoiding invocation of any move, copy, or swap operations on
+  // individual elements.
+  //
+  // All iterators and references on the `node_hash_map` remain valid, excepting
+  // for the past-the-end iterator, which is invalidated.
+  //
+  // `swap()` requires that the node hash map's hashing and key equivalence
+  // functions be Swappable, and are exchaged using unqualified calls to
+  // non-member `swap()`. If the map's allocator has
+  // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
+  // set to `true`, the allocators are also exchanged using an unqualified call
+  // to non-member `swap()`; otherwise, the allocators are not swapped.
+  using Base::swap;
+
+  // node_hash_map::rehash(count)
+  //
+  // Rehashes the `node_hash_map`, setting the number of slots to be at least
+  // the passed value. If the new number of slots increases the load factor more
+  // than the current maximum load factor
+  // (`count` < `size()` / `max_load_factor()`), then the new number of slots
+  // will be at least `size()` / `max_load_factor()`.
+  //
+  // To force a rehash, pass rehash(0).
+  using Base::rehash;
+
+  // node_hash_map::reserve(count)
+  //
+  // Sets the number of slots in the `node_hash_map` to the number needed to
+  // accommodate at least `count` total elements without exceeding the current
+  // maximum load factor, and may rehash the container if needed.
+  using Base::reserve;
+
+  // node_hash_map::at()
+  //
+  // Returns a reference to the mapped value of the element with key equivalent
+  // to the passed key.
+  using Base::at;
+
+  // node_hash_map::contains()
+  //
+  // Determines whether an element with a key comparing equal to the given `key`
+  // exists within the `node_hash_map`, returning `true` if so or `false`
+  // otherwise.
+  using Base::contains;
+
+  // node_hash_map::count(const Key& key) const
+  //
+  // Returns the number of elements with a key comparing equal to the given
+  // `key` within the `node_hash_map`. note that this function will return
+  // either `1` or `0` since duplicate keys are not allowed within a
+  // `node_hash_map`.
+  using Base::count;
+
+  // node_hash_map::equal_range()
+  //
+  // Returns a closed range [first, last], defined by a `std::pair` of two
+  // iterators, containing all elements with the passed key in the
+  // `node_hash_map`.
+  using Base::equal_range;
+
+  // node_hash_map::find()
+  //
+  // Finds an element with the passed `key` within the `node_hash_map`.
+  using Base::find;
+
+  // node_hash_map::operator[]()
+  //
+  // Returns a reference to the value mapped to the passed key within the
+  // `node_hash_map`, performing an `insert()` if the key does not already
+  // exist. If an insertion occurs and results in a rehashing of the container,
+  // all iterators are invalidated. Otherwise iterators are not affected and
+  // references are not invalidated. Overloads are listed below.
+  //
+  // T& operator[](const Key& key):
+  //
+  //   Inserts an init_type object constructed in-place if the element with the
+  //   given key does not exist.
+  //
+  // T& operator[](Key&& key):
+  //
+  //   Inserts an init_type object constructed in-place provided that an element
+  //   with the given key does not exist.
+  using Base::operator[];
+
+  // node_hash_map::bucket_count()
+  //
+  // Returns the number of "buckets" within the `node_hash_map`.
+  using Base::bucket_count;
+
+  // node_hash_map::load_factor()
+  //
+  // Returns the current load factor of the `node_hash_map` (the average number
+  // of slots occupied with a value within the hash map).
+  using Base::load_factor;
+
+  // node_hash_map::max_load_factor()
+  //
+  // Manages the maximum load factor of the `node_hash_map`. Overloads are
+  // listed below.
+  //
+  // float node_hash_map::max_load_factor()
+  //
+  //   Returns the current maximum load factor of the `node_hash_map`.
+  //
+  // void node_hash_map::max_load_factor(float ml)
+  //
+  //   Sets the maximum load factor of the `node_hash_map` to the passed value.
+  //
+  //   NOTE: This overload is provided only for API compatibility with the STL;
+  //   `node_hash_map` will ignore any set load factor and manage its rehashing
+  //   internally as an implementation detail.
+  using Base::max_load_factor;
+
+  // node_hash_map::get_allocator()
+  //
+  // Returns the allocator function associated with this `node_hash_map`.
+  using Base::get_allocator;
+
+  // node_hash_map::hash_function()
+  //
+  // Returns the hashing function used to hash the keys within this
+  // `node_hash_map`.
+  using Base::hash_function;
+
+  // node_hash_map::key_eq()
+  //
+  // Returns the function used for comparing keys equality.
+  using Base::key_eq;
+
+  ABSL_DEPRECATED("Call `hash_function()` instead.")
+  typename Base::hasher hash_funct() { return this->hash_function(); }
+
+  ABSL_DEPRECATED("Call `rehash()` instead.")
+  void resize(typename Base::size_type hint) { this->rehash(hint); }
+};
+
+namespace container_internal {
+
+template <class Key, class Value>
+class NodeHashMapPolicy
+    : public absl::container_internal::node_hash_policy<
+          std::pair<const Key, Value>&, NodeHashMapPolicy<Key, Value>> {
+  using value_type = std::pair<const Key, Value>;
+
+ public:
+  using key_type = Key;
+  using mapped_type = Value;
+  using init_type = std::pair</*non const*/ key_type, mapped_type>;
+
+  template <class Allocator, class... Args>
+  static value_type* new_element(Allocator* alloc, Args&&... args) {
+    using PairAlloc = typename absl::allocator_traits<
+        Allocator>::template rebind_alloc<value_type>;
+    PairAlloc pair_alloc(*alloc);
+    value_type* res =
+        absl::allocator_traits<PairAlloc>::allocate(pair_alloc, 1);
+    absl::allocator_traits<PairAlloc>::construct(pair_alloc, res,
+                                                 std::forward<Args>(args)...);
+    return res;
+  }
+
+  template <class Allocator>
+  static void delete_element(Allocator* alloc, value_type* pair) {
+    using PairAlloc = typename absl::allocator_traits<
+        Allocator>::template rebind_alloc<value_type>;
+    PairAlloc pair_alloc(*alloc);
+    absl::allocator_traits<PairAlloc>::destroy(pair_alloc, pair);
+    absl::allocator_traits<PairAlloc>::deallocate(pair_alloc, pair, 1);
+  }
+
+  template <class F, class... Args>
+  static decltype(absl::container_internal::DecomposePair(
+      std::declval<F>(), std::declval<Args>()...))
+  apply(F&& f, Args&&... args) {
+    return absl::container_internal::DecomposePair(std::forward<F>(f),
+                                                   std::forward<Args>(args)...);
+  }
+
+  static size_t element_space_used(const value_type*) {
+    return sizeof(value_type);
+  }
+
+  static Value& value(value_type* elem) { return elem->second; }
+  static const Value& value(const value_type* elem) { return elem->second; }
+};
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_NODE_HASH_MAP_H_
diff --git a/absl/container/node_hash_map_test.cc b/absl/container/node_hash_map_test.cc
new file mode 100644
index 0000000..bd78964
--- /dev/null
+++ b/absl/container/node_hash_map_test.cc
@@ -0,0 +1,218 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/node_hash_map.h"
+
+#include "absl/container/internal/tracked.h"
+#include "absl/container/internal/unordered_map_constructor_test.h"
+#include "absl/container/internal/unordered_map_lookup_test.h"
+#include "absl/container/internal/unordered_map_modifiers_test.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+
+using ::testing::Field;
+using ::testing::Pair;
+using ::testing::UnorderedElementsAre;
+
+using MapTypes = ::testing::Types<
+    absl::node_hash_map<int, int, StatefulTestingHash, StatefulTestingEqual,
+                        Alloc<std::pair<const int, int>>>,
+    absl::node_hash_map<std::string, std::string, StatefulTestingHash,
+                        StatefulTestingEqual,
+                        Alloc<std::pair<const std::string, std::string>>>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashMap, ConstructorTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashMap, LookupTest, MapTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashMap, ModifiersTest, MapTypes);
+
+using M = absl::node_hash_map<std::string, Tracked<int>>;
+
+TEST(NodeHashMap, Emplace) {
+  M m;
+  Tracked<int> t(53);
+  m.emplace("a", t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(1, t.num_copies());
+
+  m.emplace(std::string("a"), t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(1, t.num_copies());
+
+  std::string a("a");
+  m.emplace(a, t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(1, t.num_copies());
+
+  const std::string ca("a");
+  m.emplace(a, t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(1, t.num_copies());
+
+  m.emplace(std::make_pair("a", t));
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(2, t.num_copies());
+
+  m.emplace(std::make_pair(std::string("a"), t));
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(3, t.num_copies());
+
+  std::pair<std::string, Tracked<int>> p("a", t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(4, t.num_copies());
+  m.emplace(p);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(4, t.num_copies());
+
+  const std::pair<std::string, Tracked<int>> cp("a", t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(5, t.num_copies());
+  m.emplace(cp);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(5, t.num_copies());
+
+  std::pair<const std::string, Tracked<int>> pc("a", t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(6, t.num_copies());
+  m.emplace(pc);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(6, t.num_copies());
+
+  const std::pair<const std::string, Tracked<int>> cpc("a", t);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(7, t.num_copies());
+  m.emplace(cpc);
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(7, t.num_copies());
+
+  m.emplace(std::piecewise_construct, std::forward_as_tuple("a"),
+            std::forward_as_tuple(t));
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(7, t.num_copies());
+
+  m.emplace(std::piecewise_construct, std::forward_as_tuple(std::string("a")),
+            std::forward_as_tuple(t));
+  ASSERT_EQ(0, t.num_moves());
+  ASSERT_EQ(7, t.num_copies());
+}
+
+TEST(NodeHashMap, AssignRecursive) {
+  struct Tree {
+    // Verify that unordered_map<K, IncompleteType> can be instantiated.
+    absl::node_hash_map<int, Tree> children;
+  };
+  Tree root;
+  const Tree& child = root.children.emplace().first->second;
+  // Verify that `lhs = rhs` doesn't read rhs after clearing lhs.
+  root = child;
+}
+
+TEST(FlatHashMap, MoveOnlyKey) {
+  struct Key {
+    Key() = default;
+    Key(Key&&) = default;
+    Key& operator=(Key&&) = default;
+  };
+  struct Eq {
+    bool operator()(const Key&, const Key&) const { return true; }
+  };
+  struct Hash {
+    size_t operator()(const Key&) const { return 0; }
+  };
+  absl::node_hash_map<Key, int, Hash, Eq> m;
+  m[Key()];
+}
+
+struct NonMovableKey {
+  explicit NonMovableKey(int i) : i(i) {}
+  NonMovableKey(NonMovableKey&&) = delete;
+  int i;
+};
+struct NonMovableKeyHash {
+  using is_transparent = void;
+  size_t operator()(const NonMovableKey& k) const { return k.i; }
+  size_t operator()(int k) const { return k; }
+};
+struct NonMovableKeyEq {
+  using is_transparent = void;
+  bool operator()(const NonMovableKey& a, const NonMovableKey& b) const {
+    return a.i == b.i;
+  }
+  bool operator()(const NonMovableKey& a, int b) const { return a.i == b; }
+};
+
+TEST(NodeHashMap, MergeExtractInsert) {
+  absl::node_hash_map<NonMovableKey, int, NonMovableKeyHash, NonMovableKeyEq>
+      set1, set2;
+  set1.emplace(std::piecewise_construct, std::make_tuple(7),
+               std::make_tuple(-7));
+  set1.emplace(std::piecewise_construct, std::make_tuple(17),
+               std::make_tuple(-17));
+
+  set2.emplace(std::piecewise_construct, std::make_tuple(7),
+               std::make_tuple(-70));
+  set2.emplace(std::piecewise_construct, std::make_tuple(19),
+               std::make_tuple(-190));
+
+  auto Elem = [](int key, int value) {
+    return Pair(Field(&NonMovableKey::i, key), value);
+  };
+
+  EXPECT_THAT(set1, UnorderedElementsAre(Elem(7, -7), Elem(17, -17)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(19, -190)));
+
+  // NonMovableKey is neither copyable nor movable. We should still be able to
+  // move nodes around.
+  static_assert(!std::is_move_constructible<NonMovableKey>::value, "");
+  set1.merge(set2);
+
+  EXPECT_THAT(set1,
+              UnorderedElementsAre(Elem(7, -7), Elem(17, -17), Elem(19, -190)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
+
+  auto node = set1.extract(7);
+  EXPECT_TRUE(node);
+  EXPECT_EQ(node.key().i, 7);
+  EXPECT_EQ(node.mapped(), -7);
+  EXPECT_THAT(set1, UnorderedElementsAre(Elem(17, -17), Elem(19, -190)));
+
+  auto insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_FALSE(insert_result.inserted);
+  EXPECT_TRUE(insert_result.node);
+  EXPECT_EQ(insert_result.node.key().i, 7);
+  EXPECT_EQ(insert_result.node.mapped(), -7);
+  EXPECT_THAT(*insert_result.position, Elem(7, -70));
+  EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
+
+  node = set1.extract(17);
+  EXPECT_TRUE(node);
+  EXPECT_EQ(node.key().i, 17);
+  EXPECT_EQ(node.mapped(), -17);
+  EXPECT_THAT(set1, UnorderedElementsAre(Elem(19, -190)));
+
+  node.mapped() = 23;
+
+  insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_TRUE(insert_result.inserted);
+  EXPECT_FALSE(insert_result.node);
+  EXPECT_THAT(*insert_result.position, Elem(17, 23));
+  EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(17, 23)));
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h
new file mode 100644
index 0000000..90d4ce0
--- /dev/null
+++ b/absl/container/node_hash_set.h
@@ -0,0 +1,439 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: node_hash_set.h
+// -----------------------------------------------------------------------------
+//
+// An `absl::node_hash_set<T>` is an unordered associative container designed to
+// be a more efficient replacement for `std::unordered_set`. Like
+// `unordered_set`, search, insertion, and deletion of map elements can be done
+// as an `O(1)` operation. However, `node_hash_set` (and other unordered
+// associative containers known as the collection of Abseil "Swiss tables")
+// contain other optimizations that result in both memory and computation
+// advantages.
+//
+// In most cases, your default choice for a hash table should be a map of type
+// `flat_hash_map` or a set of type `flat_hash_set`. However, if you need
+// pointer stability, a `node_hash_set` should be your preferred choice. As
+// well, if you are migrating your code from using `std::unordered_set`, a
+// `node_hash_set` should be an easy migration. Consider migrating to
+// `node_hash_set` and perhaps converting to a more efficient `flat_hash_set`
+// upon further review.
+
+#ifndef ABSL_CONTAINER_NODE_HASH_SET_H_
+#define ABSL_CONTAINER_NODE_HASH_SET_H_
+
+#include <type_traits>
+
+#include "absl/container/internal/hash_function_defaults.h"  // IWYU pragma: export
+#include "absl/container/internal/node_hash_policy.h"
+#include "absl/container/internal/raw_hash_set.h"  // IWYU pragma: export
+#include "absl/memory/memory.h"
+
+namespace absl {
+namespace container_internal {
+template <typename T>
+struct NodeHashSetPolicy;
+}  // namespace container_internal
+
+// -----------------------------------------------------------------------------
+// absl::node_hash_set
+// -----------------------------------------------------------------------------
+//
+// An `absl::node_hash_set<T>` is an unordered associative container which
+// has been optimized for both speed and memory footprint in most common use
+// cases. Its interface is similar to that of `std::unordered_set<T>` with the
+// following notable differences:
+//
+// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
+//   `insert()`, provided that the map is provided a compatible heterogeneous
+//   hashing function and equality operator.
+// * Contains a `capacity()` member function indicating the number of element
+//   slots (open, deleted, and empty) within the hash set.
+// * Returns `void` from the `erase(iterator)` overload.
+//
+// By default, `node_hash_set` uses the `absl::Hash` hashing framework.
+// All fundamental and Abseil types that support the `absl::Hash` framework have
+// a compatible equality operator for comparing insertions into `node_hash_set`.
+// If your type is not yet supported by the `asbl::Hash` framework, see
+// absl/hash/hash.h for information on extending Abseil hashing to user-defined
+// types.
+//
+// Example:
+//
+//   // Create a node hash set of three strings
+//   absl::node_hash_map<std::string, std::string> ducks =
+//     {"huey", "dewey"}, "louie"};
+//
+//  // Insert a new element into the node hash map
+//  ducks.insert("donald"};
+//
+//  // Force a rehash of the node hash map
+//  ducks.rehash(0);
+//
+//  // See if "dewey" is present
+//  if (ducks.contains("dewey")) {
+//    std::cout << "We found dewey!" << std::endl;
+//  }
+template <class T, class Hash = absl::container_internal::hash_default_hash<T>,
+          class Eq = absl::container_internal::hash_default_eq<T>,
+          class Alloc = std::allocator<T>>
+class node_hash_set
+    : public absl::container_internal::raw_hash_set<
+          absl::container_internal::NodeHashSetPolicy<T>, Hash, Eq, Alloc> {
+  using Base = typename node_hash_set::raw_hash_set;
+
+ public:
+  node_hash_set() {}
+  using Base::Base;
+
+  // node_hash_set::begin()
+  //
+  // Returns an iterator to the beginning of the `node_hash_set`.
+  using Base::begin;
+
+  // node_hash_set::cbegin()
+  //
+  // Returns a const iterator to the beginning of the `node_hash_set`.
+  using Base::cbegin;
+
+  // node_hash_set::cend()
+  //
+  // Returns a const iterator to the end of the `node_hash_set`.
+  using Base::cend;
+
+  // node_hash_set::end()
+  //
+  // Returns an iterator to the end of the `node_hash_set`.
+  using Base::end;
+
+  // node_hash_set::capacity()
+  //
+  // Returns the number of element slots (assigned, deleted, and empty)
+  // available within the `node_hash_set`.
+  //
+  // NOTE: this member function is particular to `absl::node_hash_set` and is
+  // not provided in the `std::unordered_map` API.
+  using Base::capacity;
+
+  // node_hash_set::empty()
+  //
+  // Returns whether or not the `node_hash_set` is empty.
+  using Base::empty;
+
+  // node_hash_set::max_size()
+  //
+  // Returns the largest theoretical possible number of elements within a
+  // `node_hash_set` under current memory constraints. This value can be thought
+  // of the largest value of `std::distance(begin(), end())` for a
+  // `node_hash_set<T>`.
+  using Base::max_size;
+
+  // node_hash_set::size()
+  //
+  // Returns the number of elements currently within the `node_hash_set`.
+  using Base::size;
+
+  // node_hash_set::clear()
+  //
+  // Removes all elements from the `node_hash_set`. Invalidates any references,
+  // pointers, or iterators referring to contained elements.
+  //
+  // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
+  // the underlying buffer call `erase(begin(), end())`.
+  using Base::clear;
+
+  // node_hash_set::erase()
+  //
+  // Erases elements within the `node_hash_set`. Erasing does not trigger a
+  // rehash. Overloads are listed below.
+  //
+  // void erase(const_iterator pos):
+  //
+  //   Erases the element at `position` of the `node_hash_set`, returning
+  //   `void`.
+  //
+  //   NOTE: this return behavior is different than that of STL containers in
+  //   general and `std::unordered_map` in particular.
+  //
+  // iterator erase(const_iterator first, const_iterator last):
+  //
+  //   Erases the elements in the open interval [`first`, `last`), returning an
+  //   iterator pointing to `last`.
+  //
+  // size_type erase(const key_type& key):
+  //
+  //   Erases the element with the matching key, if it exists.
+  using Base::erase;
+
+  // node_hash_set::insert()
+  //
+  // Inserts an element of the specified value into the `node_hash_set`,
+  // returning an iterator pointing to the newly inserted element, provided that
+  // an element with the given key does not already exist. If rehashing occurs
+  // due to the insertion, all iterators are invalidated. Overloads are listed
+  // below.
+  //
+  // std::pair<iterator,bool> insert(const T& value):
+  //
+  //   Inserts a value into the `node_hash_set`. Returns a pair consisting of an
+  //   iterator to the inserted element (or to the element that prevented the
+  //   insertion) and a bool denoting whether the insertion took place.
+  //
+  // std::pair<iterator,bool> insert(T&& value):
+  //
+  //   Inserts a moveable value into the `node_hash_set`. Returns a pair
+  //   consisting of an iterator to the inserted element (or to the element that
+  //   prevented the insertion) and a bool denoting whether the insertion took
+  //   place.
+  //
+  // iterator insert(const_iterator hint, const T& value):
+  // iterator insert(const_iterator hint, T&& value):
+  //
+  //   Inserts a value, using the position of `hint` as a non-binding suggestion
+  //   for where to begin the insertion search. Returns an iterator to the
+  //   inserted element, or to the existing element that prevented the
+  //   insertion.
+  //
+  // void insert(InputIterator first, InputIterator last):
+  //
+  //   Inserts a range of values [`first`, `last`).
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently, for `node_hash_set` we guarantee the
+  //   first match is inserted.
+  //
+  // void insert(std::initializer_list<T> ilist):
+  //
+  //   Inserts the elements within the initializer list `ilist`.
+  //
+  //   NOTE: Although the STL does not specify which element may be inserted if
+  //   multiple keys compare equivalently within the initializer list, for
+  //   `node_hash_set` we guarantee the first match is inserted.
+  using Base::insert;
+
+  // node_hash_set::emplace()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `node_hash_set`, provided that no element with the given key
+  // already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace;
+
+  // node_hash_set::emplace_hint()
+  //
+  // Inserts an element of the specified value by constructing it in-place
+  // within the `node_hash_set`, using the position of `hint` as a non-binding
+  // suggestion for where to begin the insertion search, and only inserts
+  // provided that no element with the given key already exists.
+  //
+  // The element may be constructed even if there already is an element with the
+  // key in the container, in which case the newly constructed element will be
+  // destroyed immediately. Prefer `try_emplace()` unless your key is not
+  // copyable or moveable.
+  //
+  // If rehashing occurs due to the insertion, all iterators are invalidated.
+  using Base::emplace_hint;
+
+  // node_hash_set::extract()
+  //
+  // Extracts the indicated element, erasing it in the process, and returns it
+  // as a C++17-compatible node handle. Overloads are listed below.
+  //
+  // node_type extract(const_iterator position):
+  //
+  //   Extracts the element at the indicated position and returns a node handle
+  //   owning that extracted data.
+  //
+  // node_type extract(const key_type& x):
+  //
+  //   Extracts the element with the key matching the passed key value and
+  //   returns a node handle owning that extracted data. If the `node_hash_set`
+  //   does not contain an element with a matching key, this function returns an
+  // empty node handle.
+  using Base::extract;
+
+  // node_hash_set::merge()
+  //
+  // Extracts elements from a given `source` flat hash map into this
+  // `node_hash_set`. If the destination `node_hash_set` already contains an
+  // element with an equivalent key, that element is not extracted.
+  using Base::merge;
+
+  // node_hash_set::swap(node_hash_set& other)
+  //
+  // Exchanges the contents of this `node_hash_set` with those of the `other`
+  // flat hash map, avoiding invocation of any move, copy, or swap operations on
+  // individual elements.
+  //
+  // All iterators and references on the `node_hash_set` remain valid, excepting
+  // for the past-the-end iterator, which is invalidated.
+  //
+  // `swap()` requires that the flat hash set's hashing and key equivalence
+  // functions be Swappable, and are exchaged using unqualified calls to
+  // non-member `swap()`. If the map's allocator has
+  // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
+  // set to `true`, the allocators are also exchanged using an unqualified call
+  // to non-member `swap()`; otherwise, the allocators are not swapped.
+  using Base::swap;
+
+  // node_hash_set::rehash(count)
+  //
+  // Rehashes the `node_hash_set`, setting the number of slots to be at least
+  // the passed value. If the new number of slots increases the load factor more
+  // than the current maximum load factor
+  // (`count` < `size()` / `max_load_factor()`), then the new number of slots
+  // will be at least `size()` / `max_load_factor()`.
+  //
+  // To force a rehash, pass rehash(0).
+  //
+  // NOTE: unlike behavior in `std::unordered_set`, references are also
+  // invalidated upon a `rehash()`.
+  using Base::rehash;
+
+  // node_hash_set::reserve(count)
+  //
+  // Sets the number of slots in the `node_hash_set` to the number needed to
+  // accommodate at least `count` total elements without exceeding the current
+  // maximum load factor, and may rehash the container if needed.
+  using Base::reserve;
+
+  // node_hash_set::contains()
+  //
+  // Determines whether an element comparing equal to the given `key` exists
+  // within the `node_hash_set`, returning `true` if so or `false` otherwise.
+  using Base::contains;
+
+  // node_hash_set::count(const Key& key) const
+  //
+  // Returns the number of elements comparing equal to the given `key` within
+  // the `node_hash_set`. note that this function will return either `1` or `0`
+  // since duplicate elements are not allowed within a `node_hash_set`.
+  using Base::count;
+
+  // node_hash_set::equal_range()
+  //
+  // Returns a closed range [first, last], defined by a `std::pair` of two
+  // iterators, containing all elements with the passed key in the
+  // `node_hash_set`.
+  using Base::equal_range;
+
+  // node_hash_set::find()
+  //
+  // Finds an element with the passed `key` within the `node_hash_set`.
+  using Base::find;
+
+  // node_hash_set::bucket_count()
+  //
+  // Returns the number of "buckets" within the `node_hash_set`. Note that
+  // because a flat hash map contains all elements within its internal storage,
+  // this value simply equals the current capacity of the `node_hash_set`.
+  using Base::bucket_count;
+
+  // node_hash_set::load_factor()
+  //
+  // Returns the current load factor of the `node_hash_set` (the average number
+  // of slots occupied with a value within the hash map).
+  using Base::load_factor;
+
+  // node_hash_set::max_load_factor()
+  //
+  // Manages the maximum load factor of the `node_hash_set`. Overloads are
+  // listed below.
+  //
+  // float node_hash_set::max_load_factor()
+  //
+  //   Returns the current maximum load factor of the `node_hash_set`.
+  //
+  // void node_hash_set::max_load_factor(float ml)
+  //
+  //   Sets the maximum load factor of the `node_hash_set` to the passed value.
+  //
+  //   NOTE: This overload is provided only for API compatibility with the STL;
+  //   `node_hash_set` will ignore any set load factor and manage its rehashing
+  //   internally as an implementation detail.
+  using Base::max_load_factor;
+
+  // node_hash_set::get_allocator()
+  //
+  // Returns the allocator function associated with this `node_hash_set`.
+  using Base::get_allocator;
+
+  // node_hash_set::hash_function()
+  //
+  // Returns the hashing function used to hash the keys within this
+  // `node_hash_set`.
+  using Base::hash_function;
+
+  // node_hash_set::key_eq()
+  //
+  // Returns the function used for comparing keys equality.
+  using Base::key_eq;
+
+  ABSL_DEPRECATED("Call `hash_function()` instead.")
+  typename Base::hasher hash_funct() { return this->hash_function(); }
+
+  ABSL_DEPRECATED("Call `rehash()` instead.")
+  void resize(typename Base::size_type hint) { this->rehash(hint); }
+};
+
+namespace container_internal {
+
+template <class T>
+struct NodeHashSetPolicy
+    : absl::container_internal::node_hash_policy<T&, NodeHashSetPolicy<T>> {
+  using key_type = T;
+  using init_type = T;
+  using constant_iterators = std::true_type;
+
+  template <class Allocator, class... Args>
+  static T* new_element(Allocator* alloc, Args&&... args) {
+    using ValueAlloc =
+        typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
+    ValueAlloc value_alloc(*alloc);
+    T* res = absl::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
+    absl::allocator_traits<ValueAlloc>::construct(value_alloc, res,
+                                                  std::forward<Args>(args)...);
+    return res;
+  }
+
+  template <class Allocator>
+  static void delete_element(Allocator* alloc, T* elem) {
+    using ValueAlloc =
+        typename absl::allocator_traits<Allocator>::template rebind_alloc<T>;
+    ValueAlloc value_alloc(*alloc);
+    absl::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
+    absl::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
+  }
+
+  template <class F, class... Args>
+  static decltype(absl::container_internal::DecomposeValue(
+      std::declval<F>(), std::declval<Args>()...))
+  apply(F&& f, Args&&... args) {
+    return absl::container_internal::DecomposeValue(
+        std::forward<F>(f), std::forward<Args>(args)...);
+  }
+
+  static size_t element_space_used(const T*) { return sizeof(T); }
+};
+}  // namespace container_internal
+}  // namespace absl
+#endif  // ABSL_CONTAINER_NODE_HASH_SET_H_
diff --git a/absl/container/node_hash_set_test.cc b/absl/container/node_hash_set_test.cc
new file mode 100644
index 0000000..7e498f0
--- /dev/null
+++ b/absl/container/node_hash_set_test.cc
@@ -0,0 +1,103 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/container/node_hash_set.h"
+
+#include "absl/container/internal/unordered_set_constructor_test.h"
+#include "absl/container/internal/unordered_set_lookup_test.h"
+#include "absl/container/internal/unordered_set_modifiers_test.h"
+
+namespace absl {
+namespace container_internal {
+namespace {
+using ::absl::container_internal::hash_internal::Enum;
+using ::absl::container_internal::hash_internal::EnumClass;
+using ::testing::Pointee;
+using ::testing::UnorderedElementsAre;
+
+using SetTypes = ::testing::Types<
+    node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
+    node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
+                  Alloc<int>>,
+    node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
+    node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
+                  Alloc<EnumClass>>>;
+
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashSet, ConstructorTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashSet, LookupTest, SetTypes);
+INSTANTIATE_TYPED_TEST_CASE_P(NodeHashSet, ModifiersTest, SetTypes);
+
+TEST(NodeHashSet, MoveableNotCopyableCompiles) {
+  node_hash_set<std::unique_ptr<void*>> t;
+  node_hash_set<std::unique_ptr<void*>> u;
+  u = std::move(t);
+}
+
+TEST(NodeHashSet, MergeExtractInsert) {
+  struct Hash {
+    size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
+  };
+  struct Eq {
+    bool operator()(const std::unique_ptr<int>& a,
+                    const std::unique_ptr<int>& b) const {
+      return *a == *b;
+    }
+  };
+  absl::node_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
+  set1.insert(absl::make_unique<int>(7));
+  set1.insert(absl::make_unique<int>(17));
+
+  set2.insert(absl::make_unique<int>(7));
+  set2.insert(absl::make_unique<int>(19));
+
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
+
+  set1.merge(set2);
+
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
+
+  auto node = set1.extract(absl::make_unique<int>(7));
+  EXPECT_TRUE(node);
+  EXPECT_THAT(node.value(), Pointee(7));
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
+
+  auto insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_FALSE(insert_result.inserted);
+  EXPECT_TRUE(insert_result.node);
+  EXPECT_THAT(insert_result.node.value(), Pointee(7));
+  EXPECT_EQ(**insert_result.position, 7);
+  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
+
+  node = set1.extract(absl::make_unique<int>(17));
+  EXPECT_TRUE(node);
+  EXPECT_THAT(node.value(), Pointee(17));
+  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
+
+  node.value() = absl::make_unique<int>(23);
+
+  insert_result = set2.insert(std::move(node));
+  EXPECT_FALSE(node);
+  EXPECT_TRUE(insert_result.inserted);
+  EXPECT_FALSE(insert_result.node);
+  EXPECT_EQ(**insert_result.position, 23);
+  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
+}
+
+}  // namespace
+}  // namespace container_internal
+}  // namespace absl
diff --git a/absl/copts.bzl b/absl/copts.bzl
index 0168ac5..5c508f1 100644
--- a/absl/copts.bzl
+++ b/absl/copts.bzl
@@ -35,7 +35,6 @@
 # Docs on groups of flags is preceded by ###.
 
 LLVM_FLAGS = [
-    # All warnings are treated as errors by implicit -Werror flag
     "-Wall",
     "-Wextra",
     "-Weverything",
@@ -54,6 +53,10 @@
     "-Wno-packed",
     "-Wno-padded",
     ###
+    # Google style does not use unsigned integers, though STL containers
+    # have unsigned types.
+    "-Wno-sign-compare",
+    ###
     "-Wno-float-conversion",
     "-Wno-float-equal",
     "-Wno-format-nonliteral",
@@ -101,6 +104,7 @@
     "-Wno-c99-extensions",
     "-Wno-missing-noreturn",
     "-Wno-missing-prototypes",
+    "-Wno-missing-variable-declarations",
     "-Wno-null-conversion",
     "-Wno-shadow",
     "-Wno-shift-sign-overflow",
@@ -112,13 +116,15 @@
     "-Wno-unused-template",
     "-Wno-used-but-marked-unused",
     "-Wno-zero-as-null-pointer-constant",
+    # gtest depends on this GNU extension being offered.
+    "-Wno-gnu-zero-variadic-macro-arguments",
 ]
 
 MSVC_FLAGS = [
     "/W3",
-    "/WX",
     "/wd4005",  # macro-redefinition
     "/wd4068",  # unknown pragma
+    "/wd4180",  # qualifier applied to function type has no meaning; ignored
     "/wd4244",  # conversion from 'type1' to 'type2', possible loss of data
     "/wd4267",  # conversion from 'size_t' to 'type', possible loss of data
     "/wd4800",  # forcing value to bool 'true' or 'false' (performance warning)
@@ -152,3 +158,7 @@
     "//absl:windows": ["/U_HAS_EXCEPTIONS", "/D_HAS_EXCEPTIONS=1", "/EHsc"],
     "//conditions:default": ["-fexceptions"],
 })
+
+ABSL_EXCEPTIONS_FLAG_LINKOPTS = select({
+    "//conditions:default": [],
+})
diff --git a/absl/debugging/BUILD.gn b/absl/debugging/BUILD.gn
index b50b0a6..d3582d6 100644
--- a/absl/debugging/BUILD.gn
+++ b/absl/debugging/BUILD.gn
@@ -92,10 +92,10 @@
   ]
   public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
   sources = [
-    "failure_signal_handler.cc"
+    "failure_signal_handler.cc",
   ]
   public = [
-    "failure_signal_handler.h"
+    "failure_signal_handler.h",
   ]
   deps = [
     ":examine_stack",
@@ -126,6 +126,7 @@
   ]
   deps = [
     "../base",
+    "../base:core_headers",
     "../base:dynamic_annotations",
   ]
 }
diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc
index c9ca2f3..4835445 100644
--- a/absl/debugging/internal/demangle.cc
+++ b/absl/debugging/internal/demangle.cc
@@ -340,7 +340,7 @@
 }
 
 // Append "str" at "out_cur_idx".  If there is an overflow, out_cur_idx is
-// set to out_end_idx+1.  The output std::string is ensured to
+// set to out_end_idx+1.  The output string is ensured to
 // always terminate with '\0' as long as there is no overflow.
 static void Append(State *state, const char *const str, const int length) {
   for (int i = 0; i < length; ++i) {
@@ -840,7 +840,7 @@
 }
 
 // Floating-point literals are encoded using a fixed-length lowercase
-// hexadecimal std::string.
+// hexadecimal string.
 static bool ParseFloatNumber(State *state) {
   ComplexityGuard guard(state);
   if (guard.IsTooComplex()) return false;
diff --git a/absl/debugging/stacktrace.cc b/absl/debugging/stacktrace.cc
index 61fee61..463fad2 100644
--- a/absl/debugging/stacktrace.cc
+++ b/absl/debugging/stacktrace.cc
@@ -80,11 +80,13 @@
 
 }  // anonymous namespace
 
+ABSL_ATTRIBUTE_NOINLINE
 int GetStackFrames(void** result, int* sizes, int max_depth, int skip_count) {
   return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
                              nullptr);
 }
 
+ABSL_ATTRIBUTE_NOINLINE
 int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
                               int skip_count, const void* uc,
                               int* min_dropped_frames) {
@@ -92,11 +94,13 @@
                             min_dropped_frames);
 }
 
+ABSL_ATTRIBUTE_NOINLINE
 int GetStackTrace(void** result, int max_depth, int skip_count) {
   return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
                               nullptr);
 }
 
+ABSL_ATTRIBUTE_NOINLINE
 int GetStackTraceWithContext(void** result, int max_depth, int skip_count,
                              const void* uc, int* min_dropped_frames) {
   return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
diff --git a/absl/debugging/symbolize_test.cc b/absl/debugging/symbolize_test.cc
index 5f2af47..8029fbe 100644
--- a/absl/debugging/symbolize_test.cc
+++ b/absl/debugging/symbolize_test.cc
@@ -321,7 +321,7 @@
   }
 }
 
-// Appends std::string(*args->arg) to args->symbol_buf.
+// Appends string(*args->arg) to args->symbol_buf.
 static void DummySymbolDecorator(
     const absl::debugging_internal::SymbolDecoratorArgs *args) {
   std::string *message = static_cast<std::string *>(args->arg);
diff --git a/absl/hash/BUILD.bazel b/absl/hash/BUILD.bazel
new file mode 100644
index 0000000..50aa550
--- /dev/null
+++ b/absl/hash/BUILD.bazel
@@ -0,0 +1,114 @@
+#
+# Copyright 2018 The Abseil Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+load(
+    "//absl:copts.bzl",
+    "ABSL_DEFAULT_COPTS",
+    "ABSL_TEST_COPTS",
+)
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])  # Apache 2.0
+
+cc_library(
+    name = "hash",
+    srcs = [
+        "internal/hash.cc",
+        "internal/hash.h",
+    ],
+    hdrs = ["hash.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        ":city",
+        "//absl/base:core_headers",
+        "//absl/base:endian",
+        "//absl/container:fixed_array",
+        "//absl/meta:type_traits",
+        "//absl/numeric:int128",
+        "//absl/strings",
+        "//absl/types:optional",
+        "//absl/types:variant",
+        "//absl/utility",
+    ],
+)
+
+cc_library(
+    name = "hash_testing",
+    testonly = 1,
+    hdrs = ["hash_testing.h"],
+    deps = [
+        ":spy_hash_state",
+        "//absl/meta:type_traits",
+        "//absl/strings",
+        "//absl/types:variant",
+        "@com_google_googletest//:gtest",
+    ],
+)
+
+cc_test(
+    name = "hash_test",
+    srcs = ["hash_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":hash",
+        ":hash_testing",
+        "//absl/base:core_headers",
+        "//absl/container:flat_hash_set",
+        "//absl/hash:spy_hash_state",
+        "//absl/meta:type_traits",
+        "//absl/numeric:int128",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
+    name = "spy_hash_state",
+    testonly = 1,
+    hdrs = ["internal/spy_hash_state.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    visibility = ["//visibility:private"],
+    deps = [
+        ":hash",
+        "//absl/strings",
+        "//absl/strings:str_format",
+    ],
+)
+
+cc_library(
+    name = "city",
+    srcs = ["internal/city.cc"],
+    hdrs = [
+        "internal/city.h",
+        "internal/city_crc.h",
+    ],
+    copts = ABSL_DEFAULT_COPTS,
+    deps = [
+        "//absl/base:config",
+        "//absl/base:core_headers",
+        "//absl/base:endian",
+    ],
+)
+
+cc_test(
+    name = "city_test",
+    srcs = ["internal/city_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    deps = [
+        ":city",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
diff --git a/absl/hash/BUILD.gn b/absl/hash/BUILD.gn
new file mode 100644
index 0000000..40b6b3a
--- /dev/null
+++ b/absl/hash/BUILD.gn
@@ -0,0 +1,104 @@
+# Copyright 2018 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//build_overrides/build.gni")
+
+if (build_with_chromium) {
+  visibility = [
+    "//third_party/webrtc/*",
+    "//third_party/abseil-cpp/*",
+    "//third_party/googletest:gtest",
+  ]
+} else {
+  visibility = [ "*" ]
+}
+
+source_set("hash") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  sources = [
+    "internal/hash.cc",
+    "internal/hash.h",
+  ]
+  public = [
+    "hash.h",
+  ]
+  deps = [
+    ":city",
+    "../base:core_headers",
+    "../base:endian",
+    "../container:fixed_array",
+    "../meta:type_traits",
+    "../numeric:int128",
+    "../strings",
+    "../types:optional",
+    "../types:variant",
+    "../utility",
+  ]
+}
+
+source_set("hash_testing") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "hash_testing.h",
+  ]
+  deps = [
+    ":spy_hash_state",
+    "../meta:type_traits",
+    "../strings",
+    "../types:variant",
+    "//testing/gtest",
+  ]
+}
+
+source_set("spy_hash_state") {
+  testonly = true
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/spy_hash_state.h",
+  ]
+  deps = [
+    ":hash",
+    "../strings",
+    "../strings:str_format",
+  ]
+  visibility = []
+  visibility += [ "../*" ]
+}
+
+source_set("city") {
+  configs -= [ "//build/config/compiler:chromium_code" ]
+  configs += [
+    "//build/config/compiler:no_chromium_code",
+    "//third_party/abseil-cpp:absl_default_cflags_cc",
+  ]
+  public_configs = [ "//third_party/abseil-cpp:absl_include_config" ]
+  public = [
+    "internal/city.h",
+    "internal/city_crc.h",
+  ]
+  sources = [
+    "internal/city.cc",
+  ]
+  deps = [
+    "../base:config",
+    "../base:core_headers",
+    "../base:endian",
+  ]
+}
diff --git a/absl/hash/CMakeLists.txt b/absl/hash/CMakeLists.txt
new file mode 100644
index 0000000..35081e3
--- /dev/null
+++ b/absl/hash/CMakeLists.txt
@@ -0,0 +1,80 @@
+#
+# Copyright 2018 The Abseil Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+list(APPEND HASH_PUBLIC_HEADERS
+  "hash.h"
+)
+
+list(APPEND HASH_INTERNAL_HEADERS
+  "internal/city.h"
+  "internal/city_crc.h"
+  "internal/hash.h"
+)
+
+# absl_hash library
+list(APPEND HASH_SRC
+  "internal/city.cc"
+  "internal/hash.cc"
+  ${HASH_PUBLIC_HEADERS}
+  ${HASH_INTERNAL_HEADERS}
+)
+
+set(HASH_PUBLIC_LIBRARIES absl::hash absl::container absl::strings absl::str_format absl::utility)
+
+absl_library(
+  TARGET
+    absl_hash
+  SOURCES
+    ${HASH_SRC}
+  PUBLIC_LIBRARIES
+    ${HASH_PUBLIC_LIBRARIES}
+  EXPORT_NAME
+    hash
+)
+
+#
+## TESTS
+#
+
+# testing support
+set(HASH_TEST_HEADERS hash_testing.h internal/spy_hash_state.h)
+set(HASH_TEST_PUBLIC_LIBRARIES absl::hash absl::container absl::numeric absl::strings absl::str_format)
+
+# hash_test
+set(HASH_TEST_SRC "hash_test.cc" ${HASH_TEST_HEADERS})
+
+absl_test(
+  TARGET
+    hash_test
+  SOURCES
+    ${HASH_TEST_SRC}
+  PUBLIC_LIBRARIES
+    ${HASH_TEST_PUBLIC_LIBRARIES}
+)
+
+# hash_test
+set(CITY_TEST_SRC "internal/city_test.cc")
+
+absl_test(
+  TARGET
+    city_test
+  SOURCES
+    ${CITY_TEST_SRC}
+  PUBLIC_LIBRARIES
+    ${HASH_TEST_PUBLIC_LIBRARIES}
+)
+
+
diff --git a/absl/hash/hash.h b/absl/hash/hash.h
new file mode 100644
index 0000000..c7ba4c2
--- /dev/null
+++ b/absl/hash/hash.h
@@ -0,0 +1,312 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: hash.h
+// -----------------------------------------------------------------------------
+//
+// This header file defines the Abseil `hash` library and the Abseil hashing
+// framework. This framework consists of the following:
+//
+//   * The `absl::Hash` functor, which is used to invoke the hasher within the
+//     Abseil hashing framework. `absl::Hash<T>` supports most basic types and
+//     a number of Abseil types out of the box.
+//   * `AbslHashValue`, an extension point that allows you to extend types to
+//     support Abseil hashing without requiring you to define a hashing
+//     algorithm.
+//   * `HashState`, a type-erased class which implement the manipulation of the
+//     hash state (H) itself. containing member functions `combine()` and
+//     `combine_contiguous()`, which you can use to contribute to an existing
+//     hash state when hashing your types.
+//
+// Unlike `std::hash` or other hashing frameworks, the Abseil hashing framework
+// provides most of its utility by abstracting away the hash algorithm (and its
+// implementation) entirely. Instead, a type invokes the Abseil hashing
+// framework by simply combining its state with the state of known, hashable
+// types. Hashing of that combined state is separately done by `absl::Hash`.
+//
+// Example:
+//
+//   // Suppose we have a class `Circle` for which we want to add hashing
+//   class Circle {
+//     public:
+//       ...
+//     private:
+//       std::pair<int, int> center_;
+//       int radius_;
+//     };
+//
+//   // To add hashing support to `Circle`, we simply need to add an ordinary
+//   // function `AbslHashValue()`, and return the combined hash state of the
+//   // existing hash state and the class state:
+//
+//     template <typename H>
+//     friend H AbslHashValue(H h, const Circle& c) {
+//       return H::combine(std::move(h), c.center_, c.radius_);
+//     }
+//
+// For more information, see Adding Type Support to `absl::Hash` below.
+//
+#ifndef ABSL_HASH_HASH_H_
+#define ABSL_HASH_HASH_H_
+
+#include "absl/hash/internal/hash.h"
+
+namespace absl {
+
+// -----------------------------------------------------------------------------
+// `absl::Hash`
+// -----------------------------------------------------------------------------
+//
+// `absl::Hash<T>` is a convenient general-purpose hash functor for a type `T`
+// satisfying any of the following conditions (in order):
+//
+//  * T is an arithmetic or pointer type
+//  * T defines an overload for `AbslHashValue(H, const T&)` for an arbitrary
+//    hash state `H`.
+//  - T defines a specialization of `HASH_NAMESPACE::hash<T>`
+//  - T defines a specialization of `std::hash<T>`
+//
+// `absl::Hash` intrinsically supports the following types:
+//
+//   * All integral types (including bool)
+//   * All enum types
+//   * All floating-point types (although hashing them is discouraged)
+//   * All pointer types, including nullptr_t
+//   * std::pair<T1, T2>, if T1 and T2 are hashable
+//   * std::tuple<Ts...>, if all the Ts... are hashable
+//   * std::unique_ptr and std::shared_ptr
+//   * All string-like types including:
+//     * std::string
+//     * std::string_view (as well as any instance of std::basic_string that
+//       uses char and std::char_traits)
+//  * All the standard sequence containers (provided the elements are hashable)
+//  * All the standard ordered associative containers (provided the elements are
+//    hashable)
+//  * absl types such as the following:
+//    * absl::string_view
+//    * absl::InlinedVector
+//    * absl::FixedArray
+//    * absl::unit128
+//    * absl::Time, absl::Duration, and absl::TimeZone
+//
+// Note: the list above is not meant to be exhaustive. Additional type support
+// may be added, in which case the above list will be updated.
+//
+// -----------------------------------------------------------------------------
+// absl::Hash Invocation Evaluation
+// -----------------------------------------------------------------------------
+//
+// When invoked, `absl::Hash<T>` searches for supplied hash functions in the
+// following order:
+//
+//   * Natively supported types out of the box (see above)
+//   * Types for which an `AbslHashValue()` overload is provided (such as
+//     user-defined types). See "Adding Type Support to `absl::Hash`" below.
+//   * Types which define a `HASH_NAMESPACE::hash<T>` specialization (aka
+//     `__gnu_cxx::hash<T>` for gcc/Clang or `stdext::hash<T>` for MSVC)
+//   * Types which define a `std::hash<T>` specialization
+//
+// The fallback to legacy hash functions exists mainly for backwards
+// compatibility. If you have a choice, prefer defining an `AbslHashValue`
+// overload instead of specializing any legacy hash functors.
+//
+// -----------------------------------------------------------------------------
+// The Hash State Concept, and using `HashState` for Type Erasure
+// -----------------------------------------------------------------------------
+//
+// The `absl::Hash` framework relies on the Concept of a "hash state." Such a
+// hash state is used in several places:
+//
+// * Within existing implementations of `absl::Hash<T>` to store the hashed
+//   state of an object. Note that it is up to the implementation how it stores
+//   such state. A hash table, for example, may mix the state to produce an
+//   integer value; a testing framework may simply hold a vector of that state.
+// * Within implementations of `AbslHashValue()` used to extend user-defined
+//   types. (See "Adding Type Support to absl::Hash" below.)
+// * Inside a `HashState`, providing type erasure for the concept of a hash
+//   state, which you can use to extend the `absl::Hash` framework for types
+//   that are otherwise difficult to extend using `AbslHashValue()`. (See the
+//   `HashState` class below.)
+//
+// The "hash state" concept contains two member functions for mixing hash state:
+//
+// * `H::combine()`
+//
+//   Combines an arbitrary number of values into a hash state, returning the
+//   updated state. Note that the existing hash state is move-only and must be
+//   passed by value.
+//
+//   Each of the value types T must be hashable by H.
+//
+//   NOTE:
+//
+//     state = H::combine(std::move(state), value1, value2, value3);
+//
+//   must be guaranteed to produce the same hash expansion as
+//
+//     state = H::combine(std::move(state), value1);
+//     state = H::combine(std::move(state), value2);
+//     state = H::combine(std::move(state), value3);
+//
+// * `H::combine_contiguous()`
+//
+//    Combines a contiguous array of `size` elements into a hash state,
+//    returning the updated state. Note that the existing hash state is
+//    move-only and must be passed by value.
+//
+//    NOTE:
+//
+//      state = H::combine_contiguous(std::move(state), data, size);
+//
+//    need NOT be guaranteed to produce the same hash expansion as a loop
+//    (it may perform internal optimizations). If you need this guarantee, use a
+//    loop instead.
+//
+// -----------------------------------------------------------------------------
+// Adding Type Support to `absl::Hash`
+// -----------------------------------------------------------------------------
+//
+// To add support for your user-defined type, add a proper `AbslHashValue()`
+// overload as a free (non-member) function. The overload will take an
+// existing hash state and should combine that state with state from the type.
+//
+// Example:
+//
+//   template <typename H>
+//   H AbslHashValue(H state, const MyType& v) {
+//     return H::combine(std::move(state), v.field1, ..., v.fieldN);
+//   }
+//
+// where `(field1, ..., fieldN)` are the members you would use on your
+// `operator==` to define equality.
+//
+// Notice that `AbslHashValue` is not a class member, but an ordinary function.
+// An `AbslHashValue` overload for a type should only be declared in the same
+// file and namespace as said type. The proper `AbslHashValue` implementation
+// for a given type will be discovered via ADL.
+//
+// Note: unlike `std::hash', `absl::Hash` should never be specialized. It must
+// only be extended by adding `AbslHashValue()` overloads.
+//
+template <typename T>
+using Hash = absl::hash_internal::Hash<T>;
+
+// HashState
+//
+// A type erased version of the hash state concept, for use in user-defined
+// `AbslHashValue` implementations that can't use templates (such as PImpl
+// classes, virtual functions, etc.). The type erasure adds overhead so it
+// should be avoided unless necessary.
+//
+// Note: This wrapper will only erase calls to:
+//     combine_contiguous(H, const unsigned char*, size_t)
+//
+// All other calls will be handled internally and will not invoke overloads
+// provided by the wrapped class.
+//
+// Users of this class should still define a template `AbslHashValue` function,
+// but can use `absl::HashState::Create(&state)` to erase the type of the hash
+// state and dispatch to their private hashing logic.
+//
+// This state can be used like any other hash state. In particular, you can call
+// `HashState::combine()` and `HashState::combine_contiguous()` on it.
+//
+// Example:
+//
+//   class Interface {
+//    public:
+//     template <typename H>
+//     friend H AbslHashValue(H state, const Interface& value) {
+//       state = H::combine(std::move(state), std::type_index(typeid(*this)));
+//       value.HashValue(absl::HashState::Create(&state));
+//       return state;
+//     }
+//    private:
+//     virtual void HashValue(absl::HashState state) const = 0;
+//  };
+//
+//   class Impl : Interface {
+//    private:
+//     void HashValue(absl::HashState state) const override {
+//       absl::HashState::combine(std::move(state), v1_, v2_);
+//     }
+//     int v1_;
+//     string v2_;
+//   };
+class HashState : public hash_internal::HashStateBase<HashState> {
+ public:
+  // HashState::Create()
+  //
+  // Create a new `HashState` instance that wraps `state`. All calls to
+  // `combine()` and `combine_contiguous()` on the new instance will be
+  // redirected to the original `state` object. The `state` object must outlive
+  // the `HashState` instance.
+  template <typename T>
+  static HashState Create(T* state) {
+    HashState s;
+    s.Init(state);
+    return s;
+  }
+
+  HashState(const HashState&) = delete;
+  HashState& operator=(const HashState&) = delete;
+  HashState(HashState&&) = default;
+  HashState& operator=(HashState&&) = default;
+
+  // HashState::combine()
+  //
+  // Combines an arbitrary number of values into a hash state, returning the
+  // updated state.
+  using HashState::HashStateBase::combine;
+
+  // HashState::combine_contiguous()
+  //
+  // Combines a contiguous array of `size` elements into a hash state, returning
+  // the updated state.
+  static HashState combine_contiguous(HashState hash_state,
+                                      const unsigned char* first, size_t size) {
+    hash_state.combine_contiguous_(hash_state.state_, first, size);
+    return hash_state;
+  }
+  using HashState::HashStateBase::combine_contiguous;
+
+ private:
+  HashState() = default;
+
+  template <typename T>
+  static void CombineContiguousImpl(void* p, const unsigned char* first,
+                                    size_t size) {
+    T& state = *static_cast<T*>(p);
+    state = T::combine_contiguous(std::move(state), first, size);
+  }
+
+  template <typename T>
+  void Init(T* state) {
+    state_ = state;
+    combine_contiguous_ = &CombineContiguousImpl<T>;
+  }
+
+  // Do not erase an already erased state.
+  void Init(HashState* state) {
+    state_ = state->state_;
+    combine_contiguous_ = state->combine_contiguous_;
+  }
+
+  void* state_;
+  void (*combine_contiguous_)(void*, const unsigned char*, size_t);
+};
+
+}  // namespace absl
+#endif  // ABSL_HASH_HASH_H_
diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc
new file mode 100644
index 0000000..7b6fb2e
--- /dev/null
+++ b/absl/hash/hash_test.cc
@@ -0,0 +1,425 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/hash/hash.h"
+
+#include <array>
+#include <cstring>
+#include <deque>
+#include <forward_list>
+#include <functional>
+#include <iterator>
+#include <limits>
+#include <list>
+#include <map>
+#include <memory>
+#include <numeric>
+#include <random>
+#include <set>
+#include <string>
+#include <tuple>
+#include <type_traits>
+#include <unordered_map>
+#include <utility>
+#include <vector>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/container/flat_hash_set.h"
+#include "absl/hash/hash_testing.h"
+#include "absl/hash/internal/spy_hash_state.h"
+#include "absl/meta/type_traits.h"
+#include "absl/numeric/int128.h"
+
+namespace {
+
+using absl::Hash;
+using absl::hash_internal::SpyHashState;
+
+template <typename T>
+class HashValueIntTest : public testing::Test {
+};
+TYPED_TEST_CASE_P(HashValueIntTest);
+
+template <typename T>
+SpyHashState SpyHash(const T& value) {
+  return SpyHashState::combine(SpyHashState(), value);
+}
+
+// Helper trait to verify if T is hashable. We use absl::Hash's poison status to
+// detect it.
+template <typename T>
+using is_hashable = std::is_default_constructible<absl::Hash<T>>;
+
+TYPED_TEST_P(HashValueIntTest, BasicUsage) {
+  EXPECT_TRUE((is_hashable<TypeParam>::value));
+
+  TypeParam n = 42;
+  EXPECT_EQ(SpyHash(n), SpyHash(TypeParam{42}));
+  EXPECT_NE(SpyHash(n), SpyHash(TypeParam{0}));
+  EXPECT_NE(SpyHash(std::numeric_limits<TypeParam>::max()),
+            SpyHash(std::numeric_limits<TypeParam>::min()));
+}
+
+TYPED_TEST_P(HashValueIntTest, FastPath) {
+  // Test the fast-path to make sure the values are the same.
+  TypeParam n = 42;
+  EXPECT_EQ(absl::Hash<TypeParam>{}(n),
+            absl::Hash<std::tuple<TypeParam>>{}(std::tuple<TypeParam>(n)));
+}
+
+REGISTER_TYPED_TEST_CASE_P(HashValueIntTest, BasicUsage, FastPath);
+using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t, uint32_t,
+                                uint64_t, size_t>;
+INSTANTIATE_TYPED_TEST_CASE_P(My, HashValueIntTest, IntTypes);
+
+template <typename T, typename = void>
+struct IsHashCallble : std::false_type {};
+
+template <typename T>
+struct IsHashCallble<T, absl::void_t<decltype(std::declval<absl::Hash<T>>()(
+                            std::declval<const T&>()))>> : std::true_type {};
+
+template <typename T, typename = void>
+struct IsAggregateInitializable : std::false_type {};
+
+template <typename T>
+struct IsAggregateInitializable<T, absl::void_t<decltype(T{})>>
+    : std::true_type {};
+
+TEST(IsHashableTest, ValidHash) {
+  EXPECT_TRUE((is_hashable<int>::value));
+  EXPECT_TRUE(std::is_default_constructible<absl::Hash<int>>::value);
+  EXPECT_TRUE(std::is_copy_constructible<absl::Hash<int>>::value);
+  EXPECT_TRUE(std::is_move_constructible<absl::Hash<int>>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<absl::Hash<int>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<absl::Hash<int>>::value);
+  EXPECT_TRUE(IsHashCallble<int>::value);
+  EXPECT_TRUE(IsAggregateInitializable<absl::Hash<int>>::value);
+}
+#if ABSL_HASH_INTERNAL_CAN_POISON_ && !defined(__APPLE__)
+TEST(IsHashableTest, PoisonHash) {
+  struct X {};
+  EXPECT_FALSE((is_hashable<X>::value));
+  EXPECT_FALSE(std::is_default_constructible<absl::Hash<X>>::value);
+  EXPECT_FALSE(std::is_copy_constructible<absl::Hash<X>>::value);
+  EXPECT_FALSE(std::is_move_constructible<absl::Hash<X>>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<absl::Hash<X>>::value);
+  EXPECT_FALSE(absl::is_move_assignable<absl::Hash<X>>::value);
+  EXPECT_FALSE(IsHashCallble<X>::value);
+  EXPECT_FALSE(IsAggregateInitializable<absl::Hash<X>>::value);
+}
+#endif  // ABSL_HASH_INTERNAL_CAN_POISON_
+
+// Hashable types
+//
+// These types exist simply to exercise various AbslHashValue behaviors, so
+// they are named by what their AbslHashValue overload does.
+struct NoOp {
+  template <typename HashCode>
+  friend HashCode AbslHashValue(HashCode h, NoOp n) {
+    return std::move(h);
+  }
+};
+
+struct EmptyCombine {
+  template <typename HashCode>
+  friend HashCode AbslHashValue(HashCode h, EmptyCombine e) {
+    return HashCode::combine(std::move(h));
+  }
+};
+
+template <typename Int>
+struct CombineIterative {
+  template <typename HashCode>
+  friend HashCode AbslHashValue(HashCode h, CombineIterative c) {
+    for (int i = 0; i < 5; ++i) {
+      h = HashCode::combine(std::move(h), Int(i));
+    }
+    return h;
+  }
+};
+
+template <typename Int>
+struct CombineVariadic {
+  template <typename HashCode>
+  friend HashCode AbslHashValue(HashCode h, CombineVariadic c) {
+    return HashCode::combine(std::move(h), Int(0), Int(1), Int(2), Int(3),
+                             Int(4));
+  }
+};
+
+using InvokeTag = absl::hash_internal::InvokeHashTag;
+template <InvokeTag T>
+using InvokeTagConstant = std::integral_constant<InvokeTag, T>;
+
+template <InvokeTag... Tags>
+struct MinTag;
+
+template <InvokeTag a, InvokeTag b, InvokeTag... Tags>
+struct MinTag<a, b, Tags...> : MinTag<(a < b ? a : b), Tags...> {};
+
+template <InvokeTag a>
+struct MinTag<a> : InvokeTagConstant<a> {};
+
+template <InvokeTag... Tags>
+struct CustomHashType {
+  size_t value;
+};
+
+template <InvokeTag allowed, InvokeTag... tags>
+struct EnableIfContained
+    : std::enable_if<absl::disjunction<
+          std::integral_constant<bool, allowed == tags>...>::value> {};
+
+template <
+    typename H, InvokeTag... Tags,
+    typename = typename EnableIfContained<InvokeTag::kHashValue, Tags...>::type>
+H AbslHashValue(H state, CustomHashType<Tags...> t) {
+  static_assert(MinTag<Tags...>::value == InvokeTag::kHashValue, "");
+  return H::combine(std::move(state),
+                    t.value + static_cast<int>(InvokeTag::kHashValue));
+}
+
+}  // namespace
+
+namespace absl {
+namespace hash_internal {
+template <InvokeTag... Tags>
+struct is_uniquely_represented<
+    CustomHashType<Tags...>,
+    typename EnableIfContained<InvokeTag::kUniquelyRepresented, Tags...>::type>
+    : std::true_type {};
+}  // namespace hash_internal
+}  // namespace absl
+
+#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE {
+template <InvokeTag... Tags>
+struct hash<CustomHashType<Tags...>> {
+  template <InvokeTag... TagsIn, typename = typename EnableIfContained<
+                                     InvokeTag::kLegacyHash, TagsIn...>::type>
+  size_t operator()(CustomHashType<TagsIn...> t) const {
+    static_assert(MinTag<Tags...>::value == InvokeTag::kLegacyHash, "");
+    return t.value + static_cast<int>(InvokeTag::kLegacyHash);
+  }
+};
+}  // namespace ABSL_INTERNAL_LEGACY_HASH_NAMESPACE
+#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+
+namespace std {
+template <InvokeTag... Tags>  // NOLINT
+struct hash<CustomHashType<Tags...>> {
+  template <InvokeTag... TagsIn, typename = typename EnableIfContained<
+                                     InvokeTag::kStdHash, TagsIn...>::type>
+  size_t operator()(CustomHashType<TagsIn...> t) const {
+    static_assert(MinTag<Tags...>::value == InvokeTag::kStdHash, "");
+    return t.value + static_cast<int>(InvokeTag::kStdHash);
+  }
+};
+}  // namespace std
+
+namespace {
+
+template <typename... T>
+void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>, T...) {
+  using type = CustomHashType<T::value...>;
+  SCOPED_TRACE(testing::PrintToString(std::vector<InvokeTag>{T::value...}));
+  EXPECT_TRUE(is_hashable<type>());
+  EXPECT_TRUE(is_hashable<const type>());
+  EXPECT_TRUE(is_hashable<const type&>());
+
+  const size_t offset = static_cast<int>(std::min({T::value...}));
+  EXPECT_EQ(SpyHash(type{7}), SpyHash(size_t{7 + offset}));
+}
+
+void TestCustomHashType(InvokeTagConstant<InvokeTag::kNone>) {
+#if ABSL_HASH_INTERNAL_CAN_POISON_
+  // is_hashable is false if we don't support any of the hooks.
+  using type = CustomHashType<>;
+  EXPECT_FALSE(is_hashable<type>());
+  EXPECT_FALSE(is_hashable<const type>());
+  EXPECT_FALSE(is_hashable<const type&>());
+#endif  // ABSL_HASH_INTERNAL_CAN_POISON_
+}
+
+template <InvokeTag Tag, typename... T>
+void TestCustomHashType(InvokeTagConstant<Tag> tag, T... t) {
+  constexpr auto next = static_cast<InvokeTag>(static_cast<int>(Tag) + 1);
+  TestCustomHashType(InvokeTagConstant<next>(), tag, t...);
+  TestCustomHashType(InvokeTagConstant<next>(), t...);
+}
+
+TEST(HashTest, CustomHashType) {
+  TestCustomHashType(InvokeTagConstant<InvokeTag{}>());
+}
+
+TEST(HashTest, NoOpsAreEquivalent) {
+  EXPECT_EQ(Hash<NoOp>()({}), Hash<NoOp>()({}));
+  EXPECT_EQ(Hash<NoOp>()({}), Hash<EmptyCombine>()({}));
+}
+
+template <typename T>
+class HashIntTest : public testing::Test {
+};
+TYPED_TEST_CASE_P(HashIntTest);
+
+TYPED_TEST_P(HashIntTest, BasicUsage) {
+  EXPECT_NE(Hash<NoOp>()({}), Hash<TypeParam>()(0));
+  EXPECT_NE(Hash<NoOp>()({}),
+            Hash<TypeParam>()(std::numeric_limits<TypeParam>::max()));
+  if (std::numeric_limits<TypeParam>::min() != 0) {
+    EXPECT_NE(Hash<NoOp>()({}),
+              Hash<TypeParam>()(std::numeric_limits<TypeParam>::min()));
+  }
+
+  EXPECT_EQ(Hash<CombineIterative<TypeParam>>()({}),
+            Hash<CombineVariadic<TypeParam>>()({}));
+}
+
+REGISTER_TYPED_TEST_CASE_P(HashIntTest, BasicUsage);
+using IntTypes = testing::Types<unsigned char, char, int, int32_t, int64_t, uint32_t,
+                                uint64_t, size_t>;
+INSTANTIATE_TYPED_TEST_CASE_P(My, HashIntTest, IntTypes);
+
+struct StructWithPadding {
+  char c;
+  int i;
+
+  template <typename H>
+  friend H AbslHashValue(H hash_state, const StructWithPadding& s) {
+    return H::combine(std::move(hash_state), s.c, s.i);
+  }
+};
+
+static_assert(sizeof(StructWithPadding) > sizeof(char) + sizeof(int),
+              "StructWithPadding doesn't have padding");
+static_assert(std::is_standard_layout<StructWithPadding>::value, "");
+
+// This check has to be disabled because libstdc++ doesn't support it.
+// static_assert(std::is_trivially_constructible<StructWithPadding>::value, "");
+
+template <typename T>
+struct ArraySlice {
+  T* begin;
+  T* end;
+
+  template <typename H>
+  friend H AbslHashValue(H hash_state, const ArraySlice& slice) {
+    for (auto t = slice.begin; t != slice.end; ++t) {
+      hash_state = H::combine(std::move(hash_state), *t);
+    }
+    return hash_state;
+  }
+};
+
+TEST(HashTest, HashNonUniquelyRepresentedType) {
+  // Create equal StructWithPadding objects that are known to have non-equal
+  // padding bytes.
+  static const size_t kNumStructs = 10;
+  unsigned char buffer1[kNumStructs * sizeof(StructWithPadding)];
+  std::memset(buffer1, 0, sizeof(buffer1));
+  auto* s1 = reinterpret_cast<StructWithPadding*>(buffer1);
+
+  unsigned char buffer2[kNumStructs * sizeof(StructWithPadding)];
+  std::memset(buffer2, 255, sizeof(buffer2));
+  auto* s2 = reinterpret_cast<StructWithPadding*>(buffer2);
+  for (int i = 0; i < kNumStructs; ++i) {
+    SCOPED_TRACE(i);
+    s1[i].c = s2[i].c = '0' + i;
+    s1[i].i = s2[i].i = i;
+    ASSERT_FALSE(memcmp(buffer1 + i * sizeof(StructWithPadding),
+                        buffer2 + i * sizeof(StructWithPadding),
+                        sizeof(StructWithPadding)) == 0)
+        << "Bug in test code: objects do not have unequal"
+        << " object representations";
+  }
+
+  EXPECT_EQ(Hash<StructWithPadding>()(s1[0]), Hash<StructWithPadding>()(s2[0]));
+  EXPECT_EQ(Hash<ArraySlice<StructWithPadding>>()({s1, s1 + kNumStructs}),
+            Hash<ArraySlice<StructWithPadding>>()({s2, s2 + kNumStructs}));
+}
+
+TEST(HashTest, StandardHashContainerUsage) {
+  std::unordered_map<int, std::string, Hash<int>> map = {{0, "foo"}, { 42, "bar" }};
+
+  EXPECT_NE(map.find(0), map.end());
+  EXPECT_EQ(map.find(1), map.end());
+  EXPECT_NE(map.find(0u), map.end());
+}
+
+struct ConvertibleFromNoOp {
+  ConvertibleFromNoOp(NoOp) {}  // NOLINT(runtime/explicit)
+
+  template <typename H>
+  friend H AbslHashValue(H hash_state, ConvertibleFromNoOp) {
+    return H::combine(std::move(hash_state), 1);
+  }
+};
+
+TEST(HashTest, HeterogeneousCall) {
+  EXPECT_NE(Hash<ConvertibleFromNoOp>()(NoOp()),
+            Hash<NoOp>()(NoOp()));
+}
+
+TEST(IsUniquelyRepresentedTest, SanityTest) {
+  using absl::hash_internal::is_uniquely_represented;
+
+  EXPECT_TRUE(is_uniquely_represented<unsigned char>::value);
+  EXPECT_TRUE(is_uniquely_represented<int>::value);
+  EXPECT_FALSE(is_uniquely_represented<bool>::value);
+  EXPECT_FALSE(is_uniquely_represented<int*>::value);
+}
+
+struct IntAndString {
+  int i;
+  std::string s;
+
+  template <typename H>
+  friend H AbslHashValue(H hash_state, IntAndString int_and_string) {
+    return H::combine(std::move(hash_state), int_and_string.s,
+                      int_and_string.i);
+  }
+};
+
+TEST(HashTest, SmallValueOn64ByteBoundary) {
+  Hash<IntAndString>()(IntAndString{0, std::string(63, '0')});
+}
+
+struct TypeErased {
+  size_t n;
+
+  template <typename H>
+  friend H AbslHashValue(H hash_state, const TypeErased& v) {
+    v.HashValue(absl::HashState::Create(&hash_state));
+    return hash_state;
+  }
+
+  void HashValue(absl::HashState state) const {
+    absl::HashState::combine(std::move(state), n);
+  }
+};
+
+TEST(HashTest, TypeErased) {
+  EXPECT_TRUE((is_hashable<TypeErased>::value));
+  EXPECT_TRUE((is_hashable<std::pair<TypeErased, int>>::value));
+
+  EXPECT_EQ(SpyHash(TypeErased{7}), SpyHash(size_t{7}));
+  EXPECT_NE(SpyHash(TypeErased{7}), SpyHash(size_t{13}));
+
+  EXPECT_EQ(SpyHash(std::make_pair(TypeErased{7}, 17)),
+            SpyHash(std::make_pair(size_t{7}, 17)));
+}
+
+}  // namespace
diff --git a/absl/hash/hash_testing.h b/absl/hash/hash_testing.h
new file mode 100644
index 0000000..1e3cda6
--- /dev/null
+++ b/absl/hash/hash_testing.h
@@ -0,0 +1,372 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_HASH_HASH_TESTING_H_
+#define ABSL_HASH_HASH_TESTING_H_
+
+#include <initializer_list>
+#include <tuple>
+#include <type_traits>
+#include <vector>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/hash/internal/spy_hash_state.h"
+#include "absl/meta/type_traits.h"
+#include "absl/strings/str_cat.h"
+#include "absl/types/variant.h"
+
+namespace absl {
+
+// Run the absl::Hash algorithm over all the elements passed in and verify that
+// their hash expansion is congruent with their `==` operator.
+//
+// It is used in conjunction with EXPECT_TRUE. Failures will output information
+// on what requirement failed and on which objects.
+//
+// Users should pass a collection of types as either an initializer list or a
+// container of cases.
+//
+//   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
+//       {v1, v2, ..., vN}));
+//
+//   std::vector<MyType> cases;
+//   // Fill cases...
+//   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(cases));
+//
+// Users can pass a variety of types for testing heterogeneous lookup with
+// `std::make_tuple`:
+//
+//   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
+//       std::make_tuple(v1, v2, ..., vN)));
+//
+//
+// Ideally, the values passed should provide enough coverage of the `==`
+// operator and the AbslHashValue implementations.
+// For dynamically sized types, the empty state should usually be included in
+// the values.
+//
+// The function accepts an optional comparator function, in case that `==` is
+// not enough for the values provided.
+//
+// Usage:
+//
+//   EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(
+//       std::make_tuple(v1, v2, ..., vN), MyCustomEq{}));
+//
+// It checks the following requirements:
+//   1. The expansion for a value is deterministic.
+//   2. For any two objects `a` and `b` in the sequence, if `a == b` evaluates
+//      to true, then their hash expansion must be equal.
+//   3. If `a == b` evaluates to false their hash expansion must be unequal.
+//   4. If `a == b` evaluates to false neither hash expansion can be a
+//      suffix of the other.
+//   5. AbslHashValue overloads should not be called by the user. They are only
+//      meant to be called by the framework. Users should call H::combine() and
+//      H::combine_contiguous().
+//   6. No moved-from instance of the hash state is used in the implementation
+//      of AbslHashValue.
+//
+// The values do not have to have the same type. This can be useful for
+// equivalent types that support heterogeneous lookup.
+//
+// A possible reason for breaking (2) is combining state in the hash expansion
+// that was not used in `==`.
+// For example:
+//
+// struct Bad2 {
+//   int a, b;
+//   template <typename H>
+//   friend H AbslHashValue(H state, Bad2 x) {
+//     // Uses a and b.
+//     return H::combine(x.a, x.b);
+//   }
+//   friend bool operator==(Bad2 x, Bad2 y) {
+//     // Only uses a.
+//     return x.a == y.a;
+//   }
+// };
+//
+// As for (3), breaking this usually means that there is state being passed to
+// the `==` operator that is not used in the hash expansion.
+// For example:
+//
+// struct Bad3 {
+//   int a, b;
+//   template <typename H>
+//   friend H AbslHashValue(H state, Bad3 x) {
+//     // Only uses a.
+//     return H::combine(x.a);
+//   }
+//   friend bool operator==(Bad3 x, Bad3 y) {
+//     // Uses a and b.
+//     return x.a == y.a && x.b == y.b;
+//   }
+// };
+//
+// Finally, a common way to break 4 is by combining dynamic ranges without
+// combining the size of the range.
+// For example:
+//
+// struct Bad4 {
+//   int *p, size;
+//   template <typename H>
+//   friend H AbslHashValue(H state, Bad4 x) {
+//     return H::combine_range(x.p, x.p + x.size);
+//   }
+//   friend bool operator==(Bad4 x, Bad4 y) {
+//     return std::equal(x.p, x.p + x.size, y.p, y.p + y.size);
+//   }
+// };
+//
+// An easy solution to this is to combine the size after combining the range,
+// like so:
+//   template <typename H>
+//   friend H AbslHashValue(H state, Bad4 x) {
+//     return H::combine(H::combine_range(x.p, x.p + x.size), x.size);
+//   }
+//
+template <int&... ExplicitBarrier, typename Container>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(const Container& values);
+
+template <int&... ExplicitBarrier, typename Container, typename Eq>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals);
+
+template <int&..., typename T>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values);
+
+template <int&..., typename T, typename Eq>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values,
+                                      Eq equals);
+
+namespace hash_internal {
+
+struct PrintVisitor {
+  size_t index;
+  template <typename T>
+  std::string operator()(const T* value) const {
+    return absl::StrCat("#", index, "(", testing::PrintToString(*value), ")");
+  }
+};
+
+template <typename Eq>
+struct EqVisitor {
+  Eq eq;
+  template <typename T, typename U>
+  bool operator()(const T* t, const U* u) const {
+    return eq(*t, *u);
+  }
+};
+
+struct ExpandVisitor {
+  template <typename T>
+  SpyHashState operator()(const T* value) const {
+    return SpyHashState::combine(SpyHashState(), *value);
+  }
+};
+
+template <typename Container, typename Eq>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals) {
+  using V = typename Container::value_type;
+
+  struct Info {
+    const V& value;
+    size_t index;
+    std::string ToString() const { return absl::visit(PrintVisitor{index}, value); }
+    SpyHashState expand() const { return absl::visit(ExpandVisitor{}, value); }
+  };
+
+  using EqClass = std::vector<Info>;
+  std::vector<EqClass> classes;
+
+  // Gather the values in equivalence classes.
+  size_t i = 0;
+  for (const auto& value : values) {
+    EqClass* c = nullptr;
+    for (auto& eqclass : classes) {
+      if (absl::visit(EqVisitor<Eq>{equals}, value, eqclass[0].value)) {
+        c = &eqclass;
+        break;
+      }
+    }
+    if (c == nullptr) {
+      classes.emplace_back();
+      c = &classes.back();
+    }
+    c->push_back({value, i});
+    ++i;
+
+    // Verify potential errors captured by SpyHashState.
+    if (auto error = c->back().expand().error()) {
+      return testing::AssertionFailure() << *error;
+    }
+  }
+
+  if (classes.size() < 2) {
+    return testing::AssertionFailure()
+           << "At least two equivalence classes are expected.";
+  }
+
+  // We assume that equality is correctly implemented.
+  // Now we verify that AbslHashValue is also correctly implemented.
+
+  for (const auto& c : classes) {
+    // All elements of the equivalence class must have the same hash expansion.
+    const SpyHashState expected = c[0].expand();
+    for (const Info& v : c) {
+      if (v.expand() != v.expand()) {
+        return testing::AssertionFailure()
+               << "Hash expansion for " << v.ToString()
+               << " is non-deterministic.";
+      }
+      if (v.expand() != expected) {
+        return testing::AssertionFailure()
+               << "Values " << c[0].ToString() << " and " << v.ToString()
+               << " evaluate as equal but have an unequal hash expansion.";
+      }
+    }
+
+    // Elements from other classes must have different hash expansion.
+    for (const auto& c2 : classes) {
+      if (&c == &c2) continue;
+      const SpyHashState c2_hash = c2[0].expand();
+      switch (SpyHashState::Compare(expected, c2_hash)) {
+        case SpyHashState::CompareResult::kEqual:
+          return testing::AssertionFailure()
+                 << "Values " << c[0].ToString() << " and " << c2[0].ToString()
+                 << " evaluate as unequal but have an equal hash expansion.";
+        case SpyHashState::CompareResult::kBSuffixA:
+          return testing::AssertionFailure()
+                 << "Hash expansion of " << c2[0].ToString()
+                 << " is a suffix of the hash expansion of " << c[0].ToString()
+                 << ".";
+        case SpyHashState::CompareResult::kASuffixB:
+          return testing::AssertionFailure()
+                 << "Hash expansion of " << c[0].ToString()
+                 << " is a suffix of the hash expansion of " << c2[0].ToString()
+                 << ".";
+        case SpyHashState::CompareResult::kUnequal:
+          break;
+      }
+    }
+  }
+  return testing::AssertionSuccess();
+}
+
+template <typename... T>
+struct TypeSet {
+  template <typename U, bool = disjunction<std::is_same<T, U>...>::value>
+  struct Insert {
+    using type = TypeSet<U, T...>;
+  };
+  template <typename U>
+  struct Insert<U, true> {
+    using type = TypeSet;
+  };
+
+  template <template <typename...> class C>
+  using apply = C<T...>;
+};
+
+template <typename... T>
+struct MakeTypeSet : TypeSet<>{};
+template <typename T, typename... Ts>
+struct MakeTypeSet<T, Ts...> : MakeTypeSet<Ts...>::template Insert<T>::type {};
+
+template <typename... T>
+using VariantForTypes = typename MakeTypeSet<
+    const typename std::decay<T>::type*...>::template apply<absl::variant>;
+
+template <typename Container>
+struct ContainerAsVector {
+  using V = absl::variant<const typename Container::value_type*>;
+  using Out = std::vector<V>;
+
+  static Out Do(const Container& values) {
+    Out out;
+    for (const auto& v : values) out.push_back(&v);
+    return out;
+  }
+};
+
+template <typename... T>
+struct ContainerAsVector<std::tuple<T...>> {
+  using V = VariantForTypes<T...>;
+  using Out = std::vector<V>;
+
+  template <size_t... I>
+  static Out DoImpl(const std::tuple<T...>& tuple, absl::index_sequence<I...>) {
+    return Out{&std::get<I>(tuple)...};
+  }
+
+  static Out Do(const std::tuple<T...>& values) {
+    return DoImpl(values, absl::index_sequence_for<T...>());
+  }
+};
+
+template <>
+struct ContainerAsVector<std::tuple<>> {
+  static std::vector<VariantForTypes<int>> Do(std::tuple<>) { return {}; }
+};
+
+struct DefaultEquals {
+  template <typename T, typename U>
+  bool operator()(const T& t, const U& u) const {
+    return t == u;
+  }
+};
+
+}  // namespace hash_internal
+
+template <int&..., typename Container>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(const Container& values) {
+  return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
+      hash_internal::ContainerAsVector<Container>::Do(values),
+      hash_internal::DefaultEquals{});
+}
+
+template <int&..., typename Container, typename Eq>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(const Container& values, Eq equals) {
+  return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
+      hash_internal::ContainerAsVector<Container>::Do(values),
+      equals);
+}
+
+template <int&..., typename T>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values) {
+  return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
+      hash_internal::ContainerAsVector<std::initializer_list<T>>::Do(values),
+      hash_internal::DefaultEquals{});
+}
+
+template <int&..., typename T, typename Eq>
+ABSL_MUST_USE_RESULT testing::AssertionResult
+VerifyTypeImplementsAbslHashCorrectly(std::initializer_list<T> values,
+                                      Eq equals) {
+  return hash_internal::VerifyTypeImplementsAbslHashCorrectly(
+      hash_internal::ContainerAsVector<std::initializer_list<T>>::Do(values),
+      equals);
+}
+
+}  // namespace absl
+
+#endif  // ABSL_HASH_HASH_TESTING_H_
diff --git a/absl/hash/internal/city.cc b/absl/hash/internal/city.cc
new file mode 100644
index 0000000..8f72dd1
--- /dev/null
+++ b/absl/hash/internal/city.cc
@@ -0,0 +1,590 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// This file provides CityHash64() and related functions.
+//
+// It's probably possible to create even faster hash functions by
+// writing a program that systematically explores some of the space of
+// possible hash functions, by using SIMD instructions, or by
+// compromising on hash quality.
+
+#include "absl/hash/internal/city.h"
+
+#include <string.h>  // for memcpy and memset
+#include <algorithm>
+
+#include "absl/base/config.h"
+#include "absl/base/internal/endian.h"
+#include "absl/base/internal/unaligned_access.h"
+#include "absl/base/optimization.h"
+
+namespace absl {
+namespace hash_internal {
+
+#ifdef ABSL_IS_BIG_ENDIAN
+#define uint32_in_expected_order(x) (absl::gbswap_32(x))
+#define uint64_in_expected_order(x) (absl::gbswap_64(x))
+#else
+#define uint32_in_expected_order(x) (x)
+#define uint64_in_expected_order(x) (x)
+#endif
+
+static uint64_t Fetch64(const char *p) {
+  return uint64_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
+}
+
+static uint32_t Fetch32(const char *p) {
+  return uint32_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
+}
+
+// Some primes between 2^63 and 2^64 for various uses.
+static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
+static const uint64_t k1 = 0xb492b66fbe98f273ULL;
+static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
+
+// Magic numbers for 32-bit hashing.  Copied from Murmur3.
+static const uint32_t c1 = 0xcc9e2d51;
+static const uint32_t c2 = 0x1b873593;
+
+// A 32-bit to 32-bit integer hash copied from Murmur3.
+static uint32_t fmix(uint32_t h) {
+  h ^= h >> 16;
+  h *= 0x85ebca6b;
+  h ^= h >> 13;
+  h *= 0xc2b2ae35;
+  h ^= h >> 16;
+  return h;
+}
+
+static uint32_t Rotate32(uint32_t val, int shift) {
+  // Avoid shifting by 32: doing so yields an undefined result.
+  return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
+}
+
+#undef PERMUTE3
+#define PERMUTE3(a, b, c) \
+  do {                    \
+    std::swap(a, b);      \
+    std::swap(a, c);      \
+  } while (0)
+
+static uint32_t Mur(uint32_t a, uint32_t h) {
+  // Helper from Murmur3 for combining two 32-bit values.
+  a *= c1;
+  a = Rotate32(a, 17);
+  a *= c2;
+  h ^= a;
+  h = Rotate32(h, 19);
+  return h * 5 + 0xe6546b64;
+}
+
+static uint32_t Hash32Len13to24(const char *s, size_t len) {
+  uint32_t a = Fetch32(s - 4 + (len >> 1));
+  uint32_t b = Fetch32(s + 4);
+  uint32_t c = Fetch32(s + len - 8);
+  uint32_t d = Fetch32(s + (len >> 1));
+  uint32_t e = Fetch32(s);
+  uint32_t f = Fetch32(s + len - 4);
+  uint32_t h = len;
+
+  return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
+}
+
+static uint32_t Hash32Len0to4(const char *s, size_t len) {
+  uint32_t b = 0;
+  uint32_t c = 9;
+  for (size_t i = 0; i < len; i++) {
+    signed char v = s[i];
+    b = b * c1 + v;
+    c ^= b;
+  }
+  return fmix(Mur(b, Mur(len, c)));
+}
+
+static uint32_t Hash32Len5to12(const char *s, size_t len) {
+  uint32_t a = len, b = len * 5, c = 9, d = b;
+  a += Fetch32(s);
+  b += Fetch32(s + len - 4);
+  c += Fetch32(s + ((len >> 1) & 4));
+  return fmix(Mur(c, Mur(b, Mur(a, d))));
+}
+
+uint32_t CityHash32(const char *s, size_t len) {
+  if (len <= 24) {
+    return len <= 12
+               ? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
+               : Hash32Len13to24(s, len);
+  }
+
+  // len > 24
+  uint32_t h = len, g = c1 * len, f = g;
+
+  uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
+  uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
+  uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
+  uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
+  uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
+  h ^= a0;
+  h = Rotate32(h, 19);
+  h = h * 5 + 0xe6546b64;
+  h ^= a2;
+  h = Rotate32(h, 19);
+  h = h * 5 + 0xe6546b64;
+  g ^= a1;
+  g = Rotate32(g, 19);
+  g = g * 5 + 0xe6546b64;
+  g ^= a3;
+  g = Rotate32(g, 19);
+  g = g * 5 + 0xe6546b64;
+  f += a4;
+  f = Rotate32(f, 19);
+  f = f * 5 + 0xe6546b64;
+  size_t iters = (len - 1) / 20;
+  do {
+    uint32_t b0 = Rotate32(Fetch32(s) * c1, 17) * c2;
+    uint32_t b1 = Fetch32(s + 4);
+    uint32_t b2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
+    uint32_t b3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
+    uint32_t b4 = Fetch32(s + 16);
+    h ^= b0;
+    h = Rotate32(h, 18);
+    h = h * 5 + 0xe6546b64;
+    f += b1;
+    f = Rotate32(f, 19);
+    f = f * c1;
+    g += b2;
+    g = Rotate32(g, 18);
+    g = g * 5 + 0xe6546b64;
+    h ^= b3 + b1;
+    h = Rotate32(h, 19);
+    h = h * 5 + 0xe6546b64;
+    g ^= b4;
+    g = absl::gbswap_32(g) * 5;
+    h += b4 * 5;
+    h = absl::gbswap_32(h);
+    f += b0;
+    PERMUTE3(f, h, g);
+    s += 20;
+  } while (--iters != 0);
+  g = Rotate32(g, 11) * c1;
+  g = Rotate32(g, 17) * c1;
+  f = Rotate32(f, 11) * c1;
+  f = Rotate32(f, 17) * c1;
+  h = Rotate32(h + g, 19);
+  h = h * 5 + 0xe6546b64;
+  h = Rotate32(h, 17) * c1;
+  h = Rotate32(h + f, 19);
+  h = h * 5 + 0xe6546b64;
+  h = Rotate32(h, 17) * c1;
+  return h;
+}
+
+// Bitwise right rotate.  Normally this will compile to a single
+// instruction, especially if the shift is a manifest constant.
+static uint64_t Rotate(uint64_t val, int shift) {
+  // Avoid shifting by 64: doing so yields an undefined result.
+  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
+}
+
+static uint64_t ShiftMix(uint64_t val) { return val ^ (val >> 47); }
+
+static uint64_t HashLen16(uint64_t u, uint64_t v) {
+  return Hash128to64(uint128(u, v));
+}
+
+static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
+  // Murmur-inspired hashing.
+  uint64_t a = (u ^ v) * mul;
+  a ^= (a >> 47);
+  uint64_t b = (v ^ a) * mul;
+  b ^= (b >> 47);
+  b *= mul;
+  return b;
+}
+
+static uint64_t HashLen0to16(const char *s, size_t len) {
+  if (len >= 8) {
+    uint64_t mul = k2 + len * 2;
+    uint64_t a = Fetch64(s) + k2;
+    uint64_t b = Fetch64(s + len - 8);
+    uint64_t c = Rotate(b, 37) * mul + a;
+    uint64_t d = (Rotate(a, 25) + b) * mul;
+    return HashLen16(c, d, mul);
+  }
+  if (len >= 4) {
+    uint64_t mul = k2 + len * 2;
+    uint64_t a = Fetch32(s);
+    return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
+  }
+  if (len > 0) {
+    uint8_t a = s[0];
+    uint8_t b = s[len >> 1];
+    uint8_t c = s[len - 1];
+    uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
+    uint32_t z = len + (static_cast<uint32_t>(c) << 2);
+    return ShiftMix(y * k2 ^ z * k0) * k2;
+  }
+  return k2;
+}
+
+// This probably works well for 16-byte strings as well, but it may be overkill
+// in that case.
+static uint64_t HashLen17to32(const char *s, size_t len) {
+  uint64_t mul = k2 + len * 2;
+  uint64_t a = Fetch64(s) * k1;
+  uint64_t b = Fetch64(s + 8);
+  uint64_t c = Fetch64(s + len - 8) * mul;
+  uint64_t d = Fetch64(s + len - 16) * k2;
+  return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
+                   a + Rotate(b + k2, 18) + c, mul);
+}
+
+// Return a 16-byte hash for 48 bytes.  Quick and dirty.
+// Callers do best to use "random-looking" values for a and b.
+static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(uint64_t w, uint64_t x,
+                                                        uint64_t y, uint64_t z,
+                                                        uint64_t a, uint64_t b) {
+  a += w;
+  b = Rotate(b + a + z, 21);
+  uint64_t c = a;
+  a += x;
+  a += y;
+  b += Rotate(a, 44);
+  return std::make_pair(a + z, b + c);
+}
+
+// Return a 16-byte hash for s[0] ... s[31], a, and b.  Quick and dirty.
+static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char *s, uint64_t a,
+                                                        uint64_t b) {
+  return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16),
+                                Fetch64(s + 24), a, b);
+}
+
+// Return an 8-byte hash for 33 to 64 bytes.
+static uint64_t HashLen33to64(const char *s, size_t len) {
+  uint64_t mul = k2 + len * 2;
+  uint64_t a = Fetch64(s) * k2;
+  uint64_t b = Fetch64(s + 8);
+  uint64_t c = Fetch64(s + len - 24);
+  uint64_t d = Fetch64(s + len - 32);
+  uint64_t e = Fetch64(s + 16) * k2;
+  uint64_t f = Fetch64(s + 24) * 9;
+  uint64_t g = Fetch64(s + len - 8);
+  uint64_t h = Fetch64(s + len - 16) * mul;
+  uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
+  uint64_t v = ((a + g) ^ d) + f + 1;
+  uint64_t w = absl::gbswap_64((u + v) * mul) + h;
+  uint64_t x = Rotate(e + f, 42) + c;
+  uint64_t y = (absl::gbswap_64((v + w) * mul) + g) * mul;
+  uint64_t z = e + f + c;
+  a = absl::gbswap_64((x + z) * mul + y) + b;
+  b = ShiftMix((z + a) * mul + d + h) * mul;
+  return b + x;
+}
+
+uint64_t CityHash64(const char *s, size_t len) {
+  if (len <= 32) {
+    if (len <= 16) {
+      return HashLen0to16(s, len);
+    } else {
+      return HashLen17to32(s, len);
+    }
+  } else if (len <= 64) {
+    return HashLen33to64(s, len);
+  }
+
+  // For strings over 64 bytes we hash the end first, and then as we
+  // loop we keep 56 bytes of state: v, w, x, y, and z.
+  uint64_t x = Fetch64(s + len - 40);
+  uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
+  uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
+  std::pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
+  std::pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
+  x = x * k1 + Fetch64(s);
+
+  // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
+  len = (len - 1) & ~static_cast<size_t>(63);
+  do {
+    x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
+    y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
+    x ^= w.second;
+    y += v.first + Fetch64(s + 40);
+    z = Rotate(z + w.first, 33) * k1;
+    v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
+    w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
+    std::swap(z, x);
+    s += 64;
+    len -= 64;
+  } while (len != 0);
+  return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
+                   HashLen16(v.second, w.second) + x);
+}
+
+uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
+  return CityHash64WithSeeds(s, len, k2, seed);
+}
+
+uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
+                           uint64_t seed1) {
+  return HashLen16(CityHash64(s, len) - seed0, seed1);
+}
+
+// A subroutine for CityHash128().  Returns a decent 128-bit hash for strings
+// of any length representable in signed long.  Based on City and Murmur.
+static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
+  uint64_t a = Uint128Low64(seed);
+  uint64_t b = Uint128High64(seed);
+  uint64_t c = 0;
+  uint64_t d = 0;
+  int64_t l = len - 16;
+  if (l <= 0) {  // len <= 16
+    a = ShiftMix(a * k1) * k1;
+    c = b * k1 + HashLen0to16(s, len);
+    d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
+  } else {  // len > 16
+    c = HashLen16(Fetch64(s + len - 8) + k1, a);
+    d = HashLen16(b + len, c + Fetch64(s + len - 16));
+    a += d;
+    do {
+      a ^= ShiftMix(Fetch64(s) * k1) * k1;
+      a *= k1;
+      b ^= a;
+      c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
+      c *= k1;
+      d ^= c;
+      s += 16;
+      l -= 16;
+    } while (l > 0);
+  }
+  a = HashLen16(a, c);
+  b = HashLen16(d, b);
+  return uint128(a ^ b, HashLen16(b, a));
+}
+
+uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
+  if (len < 128) {
+    return CityMurmur(s, len, seed);
+  }
+
+  // We expect len >= 128 to be the common case.  Keep 56 bytes of state:
+  // v, w, x, y, and z.
+  std::pair<uint64_t, uint64_t> v, w;
+  uint64_t x = Uint128Low64(seed);
+  uint64_t y = Uint128High64(seed);
+  uint64_t z = len * k1;
+  v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
+  v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
+  w.first = Rotate(y + z, 35) * k1 + x;
+  w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
+
+  // This is the same inner loop as CityHash64(), manually unrolled.
+  do {
+    x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
+    y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
+    x ^= w.second;
+    y += v.first + Fetch64(s + 40);
+    z = Rotate(z + w.first, 33) * k1;
+    v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
+    w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
+    std::swap(z, x);
+    s += 64;
+    x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
+    y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
+    x ^= w.second;
+    y += v.first + Fetch64(s + 40);
+    z = Rotate(z + w.first, 33) * k1;
+    v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
+    w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
+    std::swap(z, x);
+    s += 64;
+    len -= 128;
+  } while (ABSL_PREDICT_TRUE(len >= 128));
+  x += Rotate(v.first + z, 49) * k0;
+  y = y * k0 + Rotate(w.second, 37);
+  z = z * k0 + Rotate(w.first, 27);
+  w.first *= 9;
+  v.first *= k0;
+  // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
+  for (size_t tail_done = 0; tail_done < len;) {
+    tail_done += 32;
+    y = Rotate(x + y, 42) * k0 + v.second;
+    w.first += Fetch64(s + len - tail_done + 16);
+    x = x * k0 + w.first;
+    z += w.second + Fetch64(s + len - tail_done);
+    w.second += v.first;
+    v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
+    v.first *= k0;
+  }
+  // At this point our 56 bytes of state should contain more than
+  // enough information for a strong 128-bit hash.  We use two
+  // different 56-byte-to-8-byte hashes to get a 16-byte final result.
+  x = HashLen16(x, v.first);
+  y = HashLen16(y + z, w.first);
+  return uint128(HashLen16(x + v.second, w.second) + y,
+                 HashLen16(x + w.second, y + v.second));
+}
+
+uint128 CityHash128(const char *s, size_t len) {
+  return len >= 16
+             ? CityHash128WithSeed(s + 16, len - 16,
+                                   uint128(Fetch64(s), Fetch64(s + 8) + k0))
+             : CityHash128WithSeed(s, len, uint128(k0, k1));
+}
+}  // namespace hash_internal
+}  // namespace absl
+
+#ifdef __SSE4_2__
+#include <nmmintrin.h>
+#include "absl/hash/internal/city_crc.h"
+
+namespace absl {
+namespace hash_internal {
+
+// Requires len >= 240.
+static void CityHashCrc256Long(const char *s, size_t len, uint32_t seed,
+                               uint64_t *result) {
+  uint64_t a = Fetch64(s + 56) + k0;
+  uint64_t b = Fetch64(s + 96) + k0;
+  uint64_t c = result[0] = HashLen16(b, len);
+  uint64_t d = result[1] = Fetch64(s + 120) * k0 + len;
+  uint64_t e = Fetch64(s + 184) + seed;
+  uint64_t f = 0;
+  uint64_t g = 0;
+  uint64_t h = c + d;
+  uint64_t x = seed;
+  uint64_t y = 0;
+  uint64_t z = 0;
+
+  // 240 bytes of input per iter.
+  size_t iters = len / 240;
+  len -= iters * 240;
+  do {
+#undef CHUNK
+#define CHUNK(r)               \
+  PERMUTE3(x, z, y);           \
+  b += Fetch64(s);             \
+  c += Fetch64(s + 8);         \
+  d += Fetch64(s + 16);        \
+  e += Fetch64(s + 24);        \
+  f += Fetch64(s + 32);        \
+  a += b;                      \
+  h += f;                      \
+  b += c;                      \
+  f += d;                      \
+  g += e;                      \
+  e += z;                      \
+  g += x;                      \
+  z = _mm_crc32_u64(z, b + g); \
+  y = _mm_crc32_u64(y, e + h); \
+  x = _mm_crc32_u64(x, f + a); \
+  e = Rotate(e, r);            \
+  c += e;                      \
+  s += 40
+
+    CHUNK(0);
+    PERMUTE3(a, h, c);
+    CHUNK(33);
+    PERMUTE3(a, h, f);
+    CHUNK(0);
+    PERMUTE3(b, h, f);
+    CHUNK(42);
+    PERMUTE3(b, h, d);
+    CHUNK(0);
+    PERMUTE3(b, h, e);
+    CHUNK(33);
+    PERMUTE3(a, h, e);
+  } while (--iters > 0);
+
+  while (len >= 40) {
+    CHUNK(29);
+    e ^= Rotate(a, 20);
+    h += Rotate(b, 30);
+    g ^= Rotate(c, 40);
+    f += Rotate(d, 34);
+    PERMUTE3(c, h, g);
+    len -= 40;
+  }
+  if (len > 0) {
+    s = s + len - 40;
+    CHUNK(33);
+    e ^= Rotate(a, 43);
+    h += Rotate(b, 42);
+    g ^= Rotate(c, 41);
+    f += Rotate(d, 40);
+  }
+  result[0] ^= h;
+  result[1] ^= g;
+  g += h;
+  a = HashLen16(a, g + z);
+  x += y << 32;
+  b += x;
+  c = HashLen16(c, z) + h;
+  d = HashLen16(d, e + result[0]);
+  g += e;
+  h += HashLen16(x, f);
+  e = HashLen16(a, d) + g;
+  z = HashLen16(b, c) + a;
+  y = HashLen16(g, h) + c;
+  result[0] = e + z + y + x;
+  a = ShiftMix((a + y) * k0) * k0 + b;
+  result[1] += a + result[0];
+  a = ShiftMix(a * k0) * k0 + c;
+  result[2] = a + result[1];
+  a = ShiftMix((a + e) * k0) * k0;
+  result[3] = a + result[2];
+}
+
+// Requires len < 240.
+static void CityHashCrc256Short(const char *s, size_t len, uint64_t *result) {
+  char buf[240];
+  memcpy(buf, s, len);
+  memset(buf + len, 0, 240 - len);
+  CityHashCrc256Long(buf, 240, ~static_cast<uint32_t>(len), result);
+}
+
+void CityHashCrc256(const char *s, size_t len, uint64_t *result) {
+  if (ABSL_PREDICT_TRUE(len >= 240)) {
+    CityHashCrc256Long(s, len, 0, result);
+  } else {
+    CityHashCrc256Short(s, len, result);
+  }
+}
+
+uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
+  if (len <= 900) {
+    return CityHash128WithSeed(s, len, seed);
+  } else {
+    uint64_t result[4];
+    CityHashCrc256(s, len, result);
+    uint64_t u = Uint128High64(seed) + result[0];
+    uint64_t v = Uint128Low64(seed) + result[1];
+    return uint128(HashLen16(u, v + result[2]),
+                   HashLen16(Rotate(v, 32), u * k0 + result[3]));
+  }
+}
+
+uint128 CityHashCrc128(const char *s, size_t len) {
+  if (len <= 900) {
+    return CityHash128(s, len);
+  } else {
+    uint64_t result[4];
+    CityHashCrc256(s, len, result);
+    return uint128(result[2], result[3]);
+  }
+}
+
+}  // namespace hash_internal
+}  // namespace absl
+
+#endif
diff --git a/absl/hash/internal/city.h b/absl/hash/internal/city.h
new file mode 100644
index 0000000..55b37b8
--- /dev/null
+++ b/absl/hash/internal/city.h
@@ -0,0 +1,108 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// http://code.google.com/p/cityhash/
+//
+// This file provides a few functions for hashing strings.  All of them are
+// high-quality functions in the sense that they pass standard tests such
+// as Austin Appleby's SMHasher.  They are also fast.
+//
+// For 64-bit x86 code, on short strings, we don't know of anything faster than
+// CityHash64 that is of comparable quality.  We believe our nearest competitor
+// is Murmur3.  For 64-bit x86 code, CityHash64 is an excellent choice for hash
+// tables and most other hashing (excluding cryptography).
+//
+// For 64-bit x86 code, on long strings, the picture is more complicated.
+// On many recent Intel CPUs, such as Nehalem, Westmere, Sandy Bridge, etc.,
+// CityHashCrc128 appears to be faster than all competitors of comparable
+// quality.  CityHash128 is also good but not quite as fast.  We believe our
+// nearest competitor is Bob Jenkins' Spooky.  We don't have great data for
+// other 64-bit CPUs, but for long strings we know that Spooky is slightly
+// faster than CityHash on some relatively recent AMD x86-64 CPUs, for example.
+// Note that CityHashCrc128 is declared in citycrc.h.
+//
+// For 32-bit x86 code, we don't know of anything faster than CityHash32 that
+// is of comparable quality.  We believe our nearest competitor is Murmur3A.
+// (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
+//
+// Functions in the CityHash family are not suitable for cryptography.
+//
+// Please see CityHash's README file for more details on our performance
+// measurements and so on.
+//
+// WARNING: This code has been only lightly tested on big-endian platforms!
+// It is known to work well on little-endian platforms that have a small penalty
+// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
+// It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
+// bug reports are welcome.
+//
+// By the way, for some hash functions, given strings a and b, the hash
+// of a+b is easily derived from the hashes of a and b.  This property
+// doesn't hold for any hash functions in this file.
+
+#ifndef ABSL_HASH_INTERNAL_CITY_H_
+#define ABSL_HASH_INTERNAL_CITY_H_
+
+#include <stdint.h>
+#include <stdlib.h>  // for size_t.
+#include <utility>
+
+
+namespace absl {
+namespace hash_internal {
+
+typedef std::pair<uint64_t, uint64_t> uint128;
+
+inline uint64_t Uint128Low64(const uint128 &x) { return x.first; }
+inline uint64_t Uint128High64(const uint128 &x) { return x.second; }
+
+// Hash function for a byte array.
+uint64_t CityHash64(const char *s, size_t len);
+
+// Hash function for a byte array.  For convenience, a 64-bit seed is also
+// hashed into the result.
+uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed);
+
+// Hash function for a byte array.  For convenience, two seeds are also
+// hashed into the result.
+uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
+                           uint64_t seed1);
+
+// Hash function for a byte array.
+uint128 CityHash128(const char *s, size_t len);
+
+// Hash function for a byte array.  For convenience, a 128-bit seed is also
+// hashed into the result.
+uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed);
+
+// Hash function for a byte array.  Most useful in 32-bit binaries.
+uint32_t CityHash32(const char *s, size_t len);
+
+// Hash 128 input bits down to 64 bits of output.
+// This is intended to be a reasonably good hash function.
+inline uint64_t Hash128to64(const uint128 &x) {
+  // Murmur-inspired hashing.
+  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
+  uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;
+  a ^= (a >> 47);
+  uint64_t b = (Uint128High64(x) ^ a) * kMul;
+  b ^= (b >> 47);
+  b *= kMul;
+  return b;
+}
+
+}  // namespace hash_internal
+}  // namespace absl
+
+#endif  // ABSL_HASH_INTERNAL_CITY_H_
diff --git a/absl/hash/internal/city_crc.h b/absl/hash/internal/city_crc.h
new file mode 100644
index 0000000..6be6557
--- /dev/null
+++ b/absl/hash/internal/city_crc.h
@@ -0,0 +1,41 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// This file declares the subset of the CityHash functions that require
+// _mm_crc32_u64().  See the CityHash README for details.
+//
+// Functions in the CityHash family are not suitable for cryptography.
+
+#ifndef ABSL_HASH_INTERNAL_CITY_CRC_H_
+#define ABSL_HASH_INTERNAL_CITY_CRC_H_
+
+#include "absl/hash/internal/city.h"
+
+namespace absl {
+namespace hash_internal {
+
+// Hash function for a byte array.
+uint128 CityHashCrc128(const char *s, size_t len);
+
+// Hash function for a byte array.  For convenience, a 128-bit seed is also
+// hashed into the result.
+uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed);
+
+// Hash function for a byte array.  Sets result[0] ... result[3].
+void CityHashCrc256(const char *s, size_t len, uint64_t *result);
+
+}  // namespace hash_internal
+}  // namespace absl
+
+#endif  // ABSL_HASH_INTERNAL_CITY_CRC_H_
diff --git a/absl/hash/internal/city_test.cc b/absl/hash/internal/city_test.cc
new file mode 100644
index 0000000..678da53
--- /dev/null
+++ b/absl/hash/internal/city_test.cc
@@ -0,0 +1,1812 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/hash/internal/city.h"
+
+#include <string.h>
+#include <cstdio>
+#include <iostream>
+#include "gtest/gtest.h"
+#ifdef __SSE4_2__
+#include "absl/hash/internal/city_crc.h"
+#endif
+
+namespace absl {
+namespace hash_internal {
+
+static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
+static const uint64_t kSeed0 = 1234567;
+static const uint64_t kSeed1 = k0;
+static const uint128 kSeed128(kSeed0, kSeed1);
+static const int kDataSize = 1 << 20;
+static const int kTestSize = 300;
+
+static char data[kDataSize];
+
+// Initialize data to pseudorandom values.
+void setup() {
+  uint64_t a = 9;
+  uint64_t b = 777;
+  for (int i = 0; i < kDataSize; i++) {
+    a += b;
+    b += a;
+    a = (a ^ (a >> 41)) * k0;
+    b = (b ^ (b >> 41)) * k0 + i;
+    uint8_t u = b >> 37;
+    memcpy(data + i, &u, 1);  // uint8_t -> char
+  }
+}
+
+#define C(x) 0x##x##ULL
+static const uint64_t testdata[kTestSize][16] = {
+    {C(9ae16a3b2f90404f), C(75106db890237a4a), C(3feac5f636039766),
+     C(3df09dfc64c09a2b), C(3cb540c392e51e29), C(6b56343feac0663),
+     C(5b7bc50fd8e8ad92), C(3df09dfc64c09a2b), C(3cb540c392e51e29),
+     C(6b56343feac0663), C(5b7bc50fd8e8ad92), C(95162f24e6a5f930),
+     C(6808bdf4f1eb06e0), C(b3b1f3a67b624d82), C(c9a62f12bd4cd80b),
+     C(dc56d17a)},
+    {C(541150e87f415e96), C(1aef0d24b3148a1a), C(bacc300e1e82345a),
+     C(c3cdc41e1df33513), C(2c138ff2596d42f6), C(f58e9082aed3055f),
+     C(162e192b2957163d), C(c3cdc41e1df33513), C(2c138ff2596d42f6),
+     C(f58e9082aed3055f), C(162e192b2957163d), C(fb99e85e0d16f90c),
+     C(608462c15bdf27e8), C(e7d2c5c943572b62), C(1baaa9327642798c),
+     C(99929334)},
+    {C(f3786a4b25827c1), C(34ee1a2bf767bd1c), C(2f15ca2ebfb631f2),
+     C(3149ba1dac77270d), C(70e2e076e30703c), C(59bcc9659bc5296),
+     C(9ecbc8132ae2f1d7), C(3149ba1dac77270d), C(70e2e076e30703c),
+     C(59bcc9659bc5296), C(9ecbc8132ae2f1d7), C(a01d30789bad7cf2),
+     C(ae03fe371981a0e0), C(127e3883b8788934), C(d0ac3d4c0a6fca32),
+     C(4252edb7)},
+    {C(ef923a7a1af78eab), C(79163b1e1e9a9b18), C(df3b2aca6e1e4a30),
+     C(2193fb7620cbf23b), C(8b6a8ff06cda8302), C(1a44469afd3e091f),
+     C(8b0449376612506), C(2193fb7620cbf23b), C(8b6a8ff06cda8302),
+     C(1a44469afd3e091f), C(8b0449376612506), C(e9d9d41c32ad91d1),
+     C(b44ab09f58e3c608), C(19e9175f9fcf784), C(839b3c9581b4a480), C(ebc34f3c)},
+    {C(11df592596f41d88), C(843ec0bce9042f9c), C(cce2ea1e08b1eb30),
+     C(4d09e42f09cc3495), C(666236631b9f253b), C(d28b3763cd02b6a3),
+     C(43b249e57c4d0c1b), C(4d09e42f09cc3495), C(666236631b9f253b),
+     C(d28b3763cd02b6a3), C(43b249e57c4d0c1b), C(3887101c8adea101),
+     C(8a9355d4efc91df0), C(3e610944cc9fecfd), C(5bf9eb60b08ac0ce),
+     C(26f2b463)},
+    {C(831f448bdc5600b3), C(62a24be3120a6919), C(1b44098a41e010da),
+     C(dc07df53b949c6b), C(d2b11b2081aeb002), C(d212b02c1b13f772),
+     C(c0bed297b4be1912), C(dc07df53b949c6b), C(d2b11b2081aeb002),
+     C(d212b02c1b13f772), C(c0bed297b4be1912), C(682d3d2ad304e4af),
+     C(40e9112a655437a1), C(268b09f7ee09843f), C(6b9698d43859ca47),
+     C(b042c047)},
+    {C(3eca803e70304894), C(d80de767e4a920a), C(a51cfbb292efd53d),
+     C(d183dcda5f73edfa), C(3a93cbf40f30128c), C(1a92544d0b41dbda),
+     C(aec2c4bee81975e1), C(d183dcda5f73edfa), C(3a93cbf40f30128c),
+     C(1a92544d0b41dbda), C(aec2c4bee81975e1), C(5f91814d1126ba4b),
+     C(f8ac57eee87fcf1f), C(c55c644a5d0023cd), C(adb761e827825ff2),
+     C(e73bb0a8)},
+    {C(1b5a063fb4c7f9f1), C(318dbc24af66dee9), C(10ef7b32d5c719af),
+     C(b140a02ef5c97712), C(b7d00ef065b51b33), C(635121d532897d98),
+     C(532daf21b312a6d6), C(b140a02ef5c97712), C(b7d00ef065b51b33),
+     C(635121d532897d98), C(532daf21b312a6d6), C(c0b09b75d943910),
+     C(8c84dfb5ef2a8e96), C(e5c06034b0353433), C(3170faf1c33a45dd),
+     C(91dfdd75)},
+    {C(a0f10149a0e538d6), C(69d008c20f87419f), C(41b36376185b3e9e),
+     C(26b6689960ccf81d), C(55f23b27bb9efd94), C(3a17f6166dd765db),
+     C(c891a8a62931e782), C(26b6689960ccf81d), C(55f23b27bb9efd94),
+     C(3a17f6166dd765db), C(c891a8a62931e782), C(23852dc37ddd2607),
+     C(8b7f1b1ec897829e), C(d1d69452a54eed8a), C(56431f2bd766ec24),
+     C(c87f95de)},
+    {C(fb8d9c70660b910b), C(a45b0cc3476bff1b), C(b28d1996144f0207),
+     C(98ec31113e5e35d2), C(5e4aeb853f1b9aa7), C(bcf5c8fe4465b7c8),
+     C(b1ea3a8243996f15), C(98ec31113e5e35d2), C(5e4aeb853f1b9aa7),
+     C(bcf5c8fe4465b7c8), C(b1ea3a8243996f15), C(cabbccedb6407571),
+     C(d1e40a84c445ec3a), C(33302aa908cf4039), C(9f15f79211b5cdf8),
+     C(3f5538ef)},
+    {C(236827beae282a46), C(e43970221139c946), C(4f3ac6faa837a3aa),
+     C(71fec0f972248915), C(2170ec2061f24574), C(9eb346b6caa36e82),
+     C(2908f0fdbca48e73), C(71fec0f972248915), C(2170ec2061f24574),
+     C(9eb346b6caa36e82), C(2908f0fdbca48e73), C(8101c99f07c64abb),
+     C(b9f4b02b1b6a96a7), C(583a2b10cd222f88), C(199dae4cf9db24c), C(70eb1a1f)},
+    {C(c385e435136ecf7c), C(d9d17368ff6c4a08), C(1b31eed4e5251a67),
+     C(df01a322c43a6200), C(298b65a1714b5a7e), C(933b83f0aedf23c),
+     C(157bcb44d63f765a), C(df01a322c43a6200), C(298b65a1714b5a7e),
+     C(933b83f0aedf23c), C(157bcb44d63f765a), C(d6e9fc7a272d8b51),
+     C(3ee5073ef1a9b777), C(63149e31fac02c59), C(2f7979ff636ba1d8),
+     C(cfd63b83)},
+    {C(e3f6828b6017086d), C(21b4d1900554b3b0), C(bef38be1809e24f1),
+     C(d93251758985ee6c), C(32a9e9f82ba2a932), C(3822aacaa95f3329),
+     C(db349b2f90a490d8), C(d93251758985ee6c), C(32a9e9f82ba2a932),
+     C(3822aacaa95f3329), C(db349b2f90a490d8), C(8d49194a894a19ca),
+     C(79a78b06e42738e6), C(7e0f1eda3d390c66), C(1c291d7e641100a5),
+     C(894a52ef)},
+    {C(851fff285561dca0), C(4d1277d73cdf416f), C(28ccffa61010ebe2),
+     C(77a4ccacd131d9ee), C(e1d08eeb2f0e29aa), C(70b9e3051383fa45),
+     C(582d0120425caba), C(77a4ccacd131d9ee), C(e1d08eeb2f0e29aa),
+     C(70b9e3051383fa45), C(582d0120425caba), C(a740eef1846e4564),
+     C(572dddb74ac3ae00), C(fdb5ca9579163bbd), C(a649b9b799c615d2),
+     C(9cde6a54)},
+    {C(61152a63595a96d9), C(d1a3a91ef3a7ba45), C(443b6bb4a493ad0c),
+     C(a154296d11362d06), C(d0f0bf1f1cb02fc1), C(ccb87e09309f90d1),
+     C(b24a8e4881911101), C(a154296d11362d06), C(d0f0bf1f1cb02fc1),
+     C(ccb87e09309f90d1), C(b24a8e4881911101), C(1a481b4528559f58),
+     C(bf837a3150896995), C(4989ef6b941a3757), C(2e725ab72d0b2948),
+     C(6c4898d5)},
+    {C(44473e03be306c88), C(30097761f872472a), C(9fd1b669bfad82d7),
+     C(3bab18b164396783), C(47e385ff9d4c06f), C(18062081bf558df),
+     C(63416eb68f104a36), C(3bab18b164396783), C(47e385ff9d4c06f),
+     C(18062081bf558df), C(63416eb68f104a36), C(4abda1560c47ac80),
+     C(1ea0e63dc6587aee), C(33ec79d92ebc1de), C(94f9dccef771e048), C(13e1978e)},
+    {C(3ead5f21d344056), C(fb6420393cfb05c3), C(407932394cbbd303),
+     C(ac059617f5906673), C(94d50d3dcd3069a7), C(2b26c3b92dea0f0),
+     C(99b7374cc78fc3fb), C(ac059617f5906673), C(94d50d3dcd3069a7),
+     C(2b26c3b92dea0f0), C(99b7374cc78fc3fb), C(1a8e3c73cdd40ee8),
+     C(cbb5fca06747f45b), C(ceec44238b291841), C(28bf35cce9c90a25), C(51b4ba8)},
+    {C(6abbfde37ee03b5b), C(83febf188d2cc113), C(cda7b62d94d5b8ee),
+     C(a4375590b8ae7c82), C(168fd42f9ecae4ff), C(23bbde43de2cb214),
+     C(a8c333112a243c8c), C(a4375590b8ae7c82), C(168fd42f9ecae4ff),
+     C(23bbde43de2cb214), C(a8c333112a243c8c), C(10ac012e8c518b49),
+     C(64a44605d8b29458), C(a67e701d2a679075), C(3a3a20f43ec92303),
+     C(b6b06e40)},
+    {C(943e7ed63b3c080), C(1ef207e9444ef7f8), C(ef4a9f9f8c6f9b4a),
+     C(6b54fc38d6a84108), C(32f4212a47a4665), C(6b5a9a8f64ee1da6),
+     C(9f74e86c6da69421), C(6b54fc38d6a84108), C(32f4212a47a4665),
+     C(6b5a9a8f64ee1da6), C(9f74e86c6da69421), C(946dd0cb30c1a08e),
+     C(fdf376956907eaaa), C(a59074c6eec03028), C(b1a3abcf283f34ac), C(240a2f2)},
+    {C(d72ce05171ef8a1a), C(c6bd6bd869203894), C(c760e6396455d23a),
+     C(f86af0b40dcce7b), C(8d3c15d613394d3c), C(491e400491cd4ece),
+     C(7c19d3530ea3547f), C(f86af0b40dcce7b), C(8d3c15d613394d3c),
+     C(491e400491cd4ece), C(7c19d3530ea3547f), C(1362963a1dc32af9),
+     C(fb9bc11762e1385c), C(9e164ef1f5376083), C(6c15819b5e828a7e),
+     C(5dcefc30)},
+    {C(4182832b52d63735), C(337097e123eea414), C(b5a72ca0456df910),
+     C(7ebc034235bc122f), C(d9a7783d4edd8049), C(5f8b04a15ae42361),
+     C(fc193363336453dd), C(7ebc034235bc122f), C(d9a7783d4edd8049),
+     C(5f8b04a15ae42361), C(fc193363336453dd), C(9b6c50224ef8c4f8),
+     C(ba225c7942d16c3f), C(6f6d55226a73c412), C(abca061fe072152a),
+     C(7a48b105)},
+    {C(d6cdae892584a2cb), C(58de0fa4eca17dcd), C(43df30b8f5f1cb00),
+     C(9e4ea5a4941e097d), C(547e048d5a9daaba), C(eb6ecbb0b831d185),
+     C(e0168df5fad0c670), C(9e4ea5a4941e097d), C(547e048d5a9daaba),
+     C(eb6ecbb0b831d185), C(e0168df5fad0c670), C(afa9705f98c2c96a),
+     C(749436f48137a96b), C(759c041fc21df486), C(b23bf400107aa2ec),
+     C(fd55007b)},
+    {C(5c8e90bc267c5ee4), C(e9ae044075d992d9), C(f234cbfd1f0a1e59),
+     C(ce2744521944f14c), C(104f8032f99dc152), C(4e7f425bfac67ca7),
+     C(9461b911a1c6d589), C(ce2744521944f14c), C(104f8032f99dc152),
+     C(4e7f425bfac67ca7), C(9461b911a1c6d589), C(5e5ecc726db8b60d),
+     C(cce68b0586083b51), C(8a7f8e54a9cba0fc), C(42f010181d16f049),
+     C(6b95894c)},
+    {C(bbd7f30ac310a6f3), C(b23b570d2666685f), C(fb13fb08c9814fe7),
+     C(4ee107042e512374), C(1e2c8c0d16097e13), C(210c7500995aa0e6),
+     C(6c13190557106457), C(4ee107042e512374), C(1e2c8c0d16097e13),
+     C(210c7500995aa0e6), C(6c13190557106457), C(a99b31c96777f381),
+     C(8312ae8301d386c0), C(ed5042b2a4fa96a3), C(d71d1bb23907fe97),
+     C(3360e827)},
+    {C(36a097aa49519d97), C(8204380a73c4065), C(77c2004bdd9e276a),
+     C(6ee1f817ce0b7aee), C(e9dcb3507f0596ca), C(6bc63c666b5100e2),
+     C(e0b056f1821752af), C(6ee1f817ce0b7aee), C(e9dcb3507f0596ca),
+     C(6bc63c666b5100e2), C(e0b056f1821752af), C(8ea1114e60292678),
+     C(904b80b46becc77), C(46cd9bb6e9dff52f), C(4c91e3b698355540), C(45177e0b)},
+    {C(dc78cb032c49217), C(112464083f83e03a), C(96ae53e28170c0f5),
+     C(d367ff54952a958), C(cdad930657371147), C(aa24dc2a9573d5fe),
+     C(eb136daa89da5110), C(d367ff54952a958), C(cdad930657371147),
+     C(aa24dc2a9573d5fe), C(eb136daa89da5110), C(de623005f6d46057),
+     C(b50c0c92b95e9b7f), C(a8aa54050b81c978), C(573fb5c7895af9b5),
+     C(7c6fffe4)},
+    {C(441593e0da922dfe), C(936ef46061469b32), C(204a1921197ddd87),
+     C(50d8a70e7a8d8f56), C(256d150ae75dab76), C(e81f4c4a1989036a),
+     C(d0f8db365f9d7e00), C(50d8a70e7a8d8f56), C(256d150ae75dab76),
+     C(e81f4c4a1989036a), C(d0f8db365f9d7e00), C(753d686677b14522),
+     C(9f76e0cb6f2d0a66), C(ab14f95988ec0d39), C(97621d9da9c9812f),
+     C(bbc78da4)},
+    {C(2ba3883d71cc2133), C(72f2bbb32bed1a3c), C(27e1bd96d4843251),
+     C(a90f761e8db1543a), C(c339e23c09703cd8), C(f0c6624c4b098fd3),
+     C(1bae2053e41fa4d9), C(a90f761e8db1543a), C(c339e23c09703cd8),
+     C(f0c6624c4b098fd3), C(1bae2053e41fa4d9), C(3589e273c22ba059),
+     C(63798246e5911a0b), C(18e710ec268fc5dc), C(714a122de1d074f3),
+     C(c5c25d39)},
+    {C(f2b6d2adf8423600), C(7514e2f016a48722), C(43045743a50396ba),
+     C(23dacb811652ad4f), C(c982da480e0d4c7d), C(3a9c8ed5a399d0a9),
+     C(951b8d084691d4e4), C(23dacb811652ad4f), C(c982da480e0d4c7d),
+     C(3a9c8ed5a399d0a9), C(951b8d084691d4e4), C(d9f87b4988cff2f7),
+     C(217a191d986aa3bc), C(6ad23c56b480350), C(dd78673938ceb2e7), C(b6e5d06e)},
+    {C(38fffe7f3680d63c), C(d513325255a7a6d1), C(31ed47790f6ca62f),
+     C(c801faaa0a2e331f), C(491dbc58279c7f88), C(9c0178848321c97a),
+     C(9d934f814f4d6a3c), C(c801faaa0a2e331f), C(491dbc58279c7f88),
+     C(9c0178848321c97a), C(9d934f814f4d6a3c), C(606a3e4fc8763192),
+     C(bc15cb36a677ee84), C(52d5904157e1fe71), C(1588dd8b1145b79b),
+     C(6178504e)},
+    {C(b7477bf0b9ce37c6), C(63b1c580a7fd02a4), C(f6433b9f10a5dac),
+     C(68dd76db9d64eca7), C(36297682b64b67), C(42b192d71f414b7a),
+     C(79692cef44fa0206), C(68dd76db9d64eca7), C(36297682b64b67),
+     C(42b192d71f414b7a), C(79692cef44fa0206), C(f0979252f4776d07),
+     C(4b87cd4f1c9bbf52), C(51b84bbc6312c710), C(150720fbf85428a7),
+     C(bd4c3637)},
+    {C(55bdb0e71e3edebd), C(c7ab562bcf0568bc), C(43166332f9ee684f),
+     C(b2e25964cd409117), C(a010599d6287c412), C(fa5d6461e768dda2),
+     C(cb3ce74e8ec4f906), C(b2e25964cd409117), C(a010599d6287c412),
+     C(fa5d6461e768dda2), C(cb3ce74e8ec4f906), C(6120abfd541a2610),
+     C(aa88b148cc95794d), C(2686ca35df6590e3), C(c6b02d18616ce94d),
+     C(6e7ac474)},
+    {C(782fa1b08b475e7), C(fb7138951c61b23b), C(9829105e234fb11e),
+     C(9a8c431f500ef06e), C(d848581a580b6c12), C(fecfe11e13a2bdb4),
+     C(6c4fa0273d7db08c), C(9a8c431f500ef06e), C(d848581a580b6c12),
+     C(fecfe11e13a2bdb4), C(6c4fa0273d7db08c), C(482f43bf5ae59fcb),
+     C(f651fbca105d79e6), C(f09f78695d865817), C(7a99d0092085cf47),
+     C(1fb4b518)},
+    {C(c5dc19b876d37a80), C(15ffcff666cfd710), C(e8c30c72003103e2),
+     C(7870765b470b2c5d), C(78a9103ff960d82), C(7bb50ffc9fac74b3),
+     C(477e70ab2b347db2), C(7870765b470b2c5d), C(78a9103ff960d82),
+     C(7bb50ffc9fac74b3), C(477e70ab2b347db2), C(a625238bdf7c07cf),
+     C(1128d515174809f5), C(b0f1647e82f45873), C(17792d1c4f222c39),
+     C(31d13d6d)},
+    {C(5e1141711d2d6706), C(b537f6dee8de6933), C(3af0a1fbbe027c54),
+     C(ea349dbc16c2e441), C(38a7455b6a877547), C(5f97b9750e365411),
+     C(e8cde7f93af49a3), C(ea349dbc16c2e441), C(38a7455b6a877547),
+     C(5f97b9750e365411), C(e8cde7f93af49a3), C(ba101925ec1f7e26),
+     C(d5e84cab8192c71e), C(e256427726fdd633), C(a4f38e2c6116890d),
+     C(26fa72e3)},
+    {C(782edf6da001234f), C(f48cbd5c66c48f3), C(808754d1e64e2a32),
+     C(5d9dde77353b1a6d), C(11f58c54581fa8b1), C(da90fa7c28c37478),
+     C(5e9a2eafc670a88a), C(5d9dde77353b1a6d), C(11f58c54581fa8b1),
+     C(da90fa7c28c37478), C(5e9a2eafc670a88a), C(e35e1bc172e011ef),
+     C(bf9255a4450ae7fe), C(55f85194e26bc55f), C(4f327873e14d0e54),
+     C(6a7433bf)},
+    {C(d26285842ff04d44), C(8f38d71341eacca9), C(5ca436f4db7a883c),
+     C(bf41e5376b9f0eec), C(2252d21eb7e1c0e9), C(f4b70a971855e732),
+     C(40c7695aa3662afd), C(bf41e5376b9f0eec), C(2252d21eb7e1c0e9),
+     C(f4b70a971855e732), C(40c7695aa3662afd), C(770fe19e16ab73bb),
+     C(d603ebda6393d749), C(e58c62439aa50dbd), C(96d51e5a02d2d7cf),
+     C(4e6df758)},
+    {C(c6ab830865a6bae6), C(6aa8e8dd4b98815c), C(efe3846713c371e5),
+     C(a1924cbf0b5f9222), C(7f4872369c2b4258), C(cd6da30530f3ea89),
+     C(b7f8b9a704e6cea1), C(a1924cbf0b5f9222), C(7f4872369c2b4258),
+     C(cd6da30530f3ea89), C(b7f8b9a704e6cea1), C(fa06ff40433fd535),
+     C(fb1c36fe8f0737f1), C(bb7050561171f80), C(b1bc23235935d897), C(d57f63ea)},
+    {C(44b3a1929232892), C(61dca0e914fc217), C(a607cc142096b964),
+     C(f7dbc8433c89b274), C(2f5f70581c9b7d32), C(39bf5e5fec82dcca),
+     C(8ade56388901a619), C(f7dbc8433c89b274), C(2f5f70581c9b7d32),
+     C(39bf5e5fec82dcca), C(8ade56388901a619), C(c1c6a725caab3ea9),
+     C(c1c7906c2f80b898), C(9c3871a04cc884e6), C(df01813cbbdf217f),
+     C(52ef73b3)},
+    {C(4b603d7932a8de4f), C(fae64c464b8a8f45), C(8fafab75661d602a),
+     C(8ffe870ef4adc087), C(65bea2be41f55b54), C(82f3503f636aef1),
+     C(5f78a282378b6bb0), C(8ffe870ef4adc087), C(65bea2be41f55b54),
+     C(82f3503f636aef1), C(5f78a282378b6bb0), C(7bf2422c0beceddb),
+     C(9d238d4780114bd), C(7ad198311906597f), C(ec8f892c0422aca3), C(3cb36c3)},
+    {C(4ec0b54cf1566aff), C(30d2c7269b206bf4), C(77c22e82295e1061),
+     C(3df9b04434771542), C(feddce785ccb661f), C(a644aff716928297),
+     C(dd46aee73824b4ed), C(3df9b04434771542), C(feddce785ccb661f),
+     C(a644aff716928297), C(dd46aee73824b4ed), C(bf8d71879da29b02),
+     C(fc82dccbfc8022a0), C(31bfcd0d9f48d1d3), C(c64ee24d0e7b5f8b),
+     C(72c39bea)},
+    {C(ed8b7a4b34954ff7), C(56432de31f4ee757), C(85bd3abaa572b155),
+     C(7d2c38a926dc1b88), C(5245b9eb4cd6791d), C(fb53ab03b9ad0855),
+     C(3664026c8fc669d7), C(7d2c38a926dc1b88), C(5245b9eb4cd6791d),
+     C(fb53ab03b9ad0855), C(3664026c8fc669d7), C(45024d5080bc196),
+     C(b236ebec2cc2740), C(27231ad0e3443be4), C(145780b63f809250), C(a65aa25c)},
+    {C(5d28b43694176c26), C(714cc8bc12d060ae), C(3437726273a83fe6),
+     C(864b1b28ec16ea86), C(6a78a5a4039ec2b9), C(8e959533e35a766),
+     C(347b7c22b75ae65f), C(864b1b28ec16ea86), C(6a78a5a4039ec2b9),
+     C(8e959533e35a766), C(347b7c22b75ae65f), C(5005892bb61e647c),
+     C(fe646519b4a1894d), C(cd801026f74a8a53), C(8713463e9a1ab9ce),
+     C(74740539)},
+    {C(6a1ef3639e1d202e), C(919bc1bd145ad928), C(30f3f7e48c28a773),
+     C(2e8c49d7c7aaa527), C(5e2328fc8701db7c), C(89ef1afca81f7de8),
+     C(b1857db11985d296), C(2e8c49d7c7aaa527), C(5e2328fc8701db7c),
+     C(89ef1afca81f7de8), C(b1857db11985d296), C(17763d695f616115),
+     C(b8f7bf1fcdc8322c), C(cf0c61938ab07a27), C(1122d3e6edb4e866),
+     C(c3ae3c26)},
+    {C(159f4d9e0307b111), C(3e17914a5675a0c), C(af849bd425047b51),
+     C(3b69edadf357432b), C(3a2e311c121e6bf2), C(380fad1e288d57e5),
+     C(bf7c7e8ef0e3b83a), C(3b69edadf357432b), C(3a2e311c121e6bf2),
+     C(380fad1e288d57e5), C(bf7c7e8ef0e3b83a), C(92966d5f4356ae9b),
+     C(2a03fc66c4d6c036), C(2516d8bddb0d5259), C(b3ffe9737ff5090), C(f29db8a2)},
+    {C(cc0a840725a7e25b), C(57c69454396e193a), C(976eaf7eee0b4540),
+     C(cd7a46850b95e901), C(c57f7d060dda246f), C(6b9406ead64079bf),
+     C(11b28e20a573b7bd), C(cd7a46850b95e901), C(c57f7d060dda246f),
+     C(6b9406ead64079bf), C(11b28e20a573b7bd), C(2d6db356e9369ace),
+     C(dc0afe10fba193), C(5cdb10885dbbfce), C(5c700e205782e35a), C(1ef4cbf4)},
+    {C(a2b27ee22f63c3f1), C(9ebde0ce1b3976b2), C(2fe6a92a257af308),
+     C(8c1df927a930af59), C(a462f4423c9e384e), C(236542255b2ad8d9),
+     C(595d201a2c19d5bc), C(8c1df927a930af59), C(a462f4423c9e384e),
+     C(236542255b2ad8d9), C(595d201a2c19d5bc), C(22c87d4604a67f3),
+     C(585a06eb4bc44c4f), C(b4175a7ac7eabcd8), C(a457d3eeba14ab8c),
+     C(a9be6c41)},
+    {C(d8f2f234899bcab3), C(b10b037297c3a168), C(debea2c510ceda7f),
+     C(9498fefb890287ce), C(ae68c2be5b1a69a6), C(6189dfba34ed656c),
+     C(91658f95836e5206), C(9498fefb890287ce), C(ae68c2be5b1a69a6),
+     C(6189dfba34ed656c), C(91658f95836e5206), C(c0bb4fff32aecd4d),
+     C(94125f505a50eef9), C(6ac406e7cfbce5bb), C(344a4b1dcdb7f5d8), C(fa31801)},
+    {C(584f28543864844f), C(d7cee9fc2d46f20d), C(a38dca5657387205),
+     C(7a0b6dbab9a14e69), C(c6d0a9d6b0e31ac4), C(a674d85812c7cf6),
+     C(63538c0351049940), C(7a0b6dbab9a14e69), C(c6d0a9d6b0e31ac4),
+     C(a674d85812c7cf6), C(63538c0351049940), C(9710e5f0bc93d1d),
+     C(c2bea5bd7c54ddd4), C(48739af2bed0d32d), C(ba2c4e09e21fba85),
+     C(8331c5d8)},
+    {C(a94be46dd9aa41af), C(a57e5b7723d3f9bd), C(34bf845a52fd2f),
+     C(843b58463c8df0ae), C(74b258324e916045), C(bdd7353230eb2b38),
+     C(fad31fced7abade5), C(843b58463c8df0ae), C(74b258324e916045),
+     C(bdd7353230eb2b38), C(fad31fced7abade5), C(2436aeafb0046f85),
+     C(65bc9af9e5e33161), C(92733b1b3ae90628), C(f48143eaf78a7a89),
+     C(e9876db8)},
+    {C(9a87bea227491d20), C(a468657e2b9c43e7), C(af9ba60db8d89ef7),
+     C(cc76f429ea7a12bb), C(5f30eaf2bb14870a), C(434e824cb3e0cd11),
+     C(431a4d382e39d16e), C(cc76f429ea7a12bb), C(5f30eaf2bb14870a),
+     C(434e824cb3e0cd11), C(431a4d382e39d16e), C(9e51f913c4773a8),
+     C(32ab1925823d0add), C(99c61b54c1d8f69d), C(38cfb80f02b43b1f),
+     C(27b0604e)},
+    {C(27688c24958d1a5c), C(e3b4a1c9429cf253), C(48a95811f70d64bc),
+     C(328063229db22884), C(67e9c95f8ba96028), C(7c6bf01c60436075),
+     C(fa55161e7d9030b2), C(328063229db22884), C(67e9c95f8ba96028),
+     C(7c6bf01c60436075), C(fa55161e7d9030b2), C(dadbc2f0dab91681),
+     C(da39d7a4934ca11), C(162e845d24c1b45c), C(eb5b9dcd8c6ed31b), C(dcec07f2)},
+    {C(5d1d37790a1873ad), C(ed9cd4bcc5fa1090), C(ce51cde05d8cd96a),
+     C(f72c26e624407e66), C(a0eb541bdbc6d409), C(c3f40a2f40b3b213),
+     C(6a784de68794492d), C(f72c26e624407e66), C(a0eb541bdbc6d409),
+     C(c3f40a2f40b3b213), C(6a784de68794492d), C(10a38a23dbef7937),
+     C(6a5560f853252278), C(c3387bbf3c7b82ba), C(fbee7c12eb072805),
+     C(cff0a82a)},
+    {C(1f03fd18b711eea9), C(566d89b1946d381a), C(6e96e83fc92563ab),
+     C(405f66cf8cae1a32), C(d7261740d8f18ce6), C(fea3af64a413d0b2),
+     C(d64d1810e83520fe), C(405f66cf8cae1a32), C(d7261740d8f18ce6),
+     C(fea3af64a413d0b2), C(d64d1810e83520fe), C(e1334a00a580c6e8),
+     C(454049e1b52c15f), C(8895d823d9778247), C(efa7f2e88b826618), C(fec83621)},
+    {C(f0316f286cf527b6), C(f84c29538de1aa5a), C(7612ed3c923d4a71),
+     C(d4eccebe9393ee8a), C(2eb7867c2318cc59), C(1ce621fd700fe396),
+     C(686450d7a346878a), C(d4eccebe9393ee8a), C(2eb7867c2318cc59),
+     C(1ce621fd700fe396), C(686450d7a346878a), C(75a5f37579f8b4cb),
+     C(500cc16eb6541dc7), C(b7b02317b539d9a6), C(3519ddff5bc20a29), C(743d8dc)},
+    {C(297008bcb3e3401d), C(61a8e407f82b0c69), C(a4a35bff0524fa0e),
+     C(7a61d8f552a53442), C(821d1d8d8cfacf35), C(7cc06361b86d0559),
+     C(119b617a8c2be199), C(7a61d8f552a53442), C(821d1d8d8cfacf35),
+     C(7cc06361b86d0559), C(119b617a8c2be199), C(2996487da6721759),
+     C(61a901376070b91d), C(d88dee12ae9c9b3c), C(5665491be1fa53a7),
+     C(64d41d26)},
+    {C(43c6252411ee3be), C(b4ca1b8077777168), C(2746dc3f7da1737f),
+     C(2247a4b2058d1c50), C(1b3fa184b1d7bcc0), C(deb85613995c06ed),
+     C(cbe1d957485a3ccd), C(2247a4b2058d1c50), C(1b3fa184b1d7bcc0),
+     C(deb85613995c06ed), C(cbe1d957485a3ccd), C(dfe241f8f33c96b6),
+     C(6597eb05019c2109), C(da344b2a63a219cf), C(79b8e3887612378a),
+     C(acd90c81)},
+    {C(ce38a9a54fad6599), C(6d6f4a90b9e8755e), C(c3ecc79ff105de3f),
+     C(e8b9ee96efa2d0e), C(90122905c4ab5358), C(84f80c832d71979c),
+     C(229310f3ffbbf4c6), C(e8b9ee96efa2d0e), C(90122905c4ab5358),
+     C(84f80c832d71979c), C(229310f3ffbbf4c6), C(cc9eb42100cd63a7),
+     C(7a283f2f3da7b9f), C(359b061d314e7a72), C(d0d959720028862), C(7c746a4b)},
+    {C(270a9305fef70cf), C(600193999d884f3a), C(f4d49eae09ed8a1),
+     C(2e091b85660f1298), C(bfe37fae1cdd64c9), C(8dddfbab930f6494),
+     C(2ccf4b08f5d417a), C(2e091b85660f1298), C(bfe37fae1cdd64c9),
+     C(8dddfbab930f6494), C(2ccf4b08f5d417a), C(365c2ee85582fe6),
+     C(dee027bcd36db62a), C(b150994d3c7e5838), C(fdfd1a0e692e436d),
+     C(b1047e99)},
+    {C(e71be7c28e84d119), C(eb6ace59932736e6), C(70c4397807ba12c5),
+     C(7a9d77781ac53509), C(4489c3ccfda3b39c), C(fa722d4f243b4964),
+     C(25f15800bffdd122), C(7a9d77781ac53509), C(4489c3ccfda3b39c),
+     C(fa722d4f243b4964), C(25f15800bffdd122), C(ed85e4157fbd3297),
+     C(aab1967227d59efd), C(2199631212eb3839), C(3e4c19359aae1cc2),
+     C(d1fd1068)},
+    {C(b5b58c24b53aaa19), C(d2a6ab0773dd897f), C(ef762fe01ecb5b97),
+     C(9deefbcfa4cab1f1), C(b58f5943cd2492ba), C(a96dcc4d1f4782a7),
+     C(102b62a82309dde5), C(9deefbcfa4cab1f1), C(b58f5943cd2492ba),
+     C(a96dcc4d1f4782a7), C(102b62a82309dde5), C(35fe52684763b338),
+     C(afe2616651eaad1f), C(43e38715bdfa05e7), C(83c9ba83b5ec4a40),
+     C(56486077)},
+    {C(44dd59bd301995cf), C(3ccabd76493ada1a), C(540db4c87d55ef23),
+     C(cfc6d7adda35797), C(14c7d1f32332cf03), C(2d553ffbff3be99d),
+     C(c91c4ee0cb563182), C(cfc6d7adda35797), C(14c7d1f32332cf03),
+     C(2d553ffbff3be99d), C(c91c4ee0cb563182), C(9aa5e507f49136f0),
+     C(760c5dd1a82c4888), C(beea7e974a1cfb5c), C(640b247774fe4bf7),
+     C(6069be80)},
+    {C(b4d4789eb6f2630b), C(bf6973263ce8ef0e), C(d1c75c50844b9d3),
+     C(bce905900c1ec6ea), C(c30f304f4045487d), C(a5c550166b3a142b),
+     C(2f482b4e35327287), C(bce905900c1ec6ea), C(c30f304f4045487d),
+     C(a5c550166b3a142b), C(2f482b4e35327287), C(15b21ddddf355438),
+     C(496471fa3006bab), C(2a8fd458d06c1a32), C(db91e8ae812f0b8d), C(2078359b)},
+    {C(12807833c463737c), C(58e927ea3b3776b4), C(72dd20ef1c2f8ad0),
+     C(910b610de7a967bf), C(801bc862120f6bf5), C(9653efeed5897681),
+     C(f5367ff83e9ebbb3), C(910b610de7a967bf), C(801bc862120f6bf5),
+     C(9653efeed5897681), C(f5367ff83e9ebbb3), C(cf56d489afd1b0bf),
+     C(c7c793715cae3de8), C(631f91d64abae47c), C(5f1f42fb14a444a2),
+     C(9ea21004)},
+    {C(e88419922b87176f), C(bcf32f41a7ddbf6f), C(d6ebefd8085c1a0f),
+     C(d1d44fe99451ef72), C(ec951ba8e51e3545), C(c0ca86b360746e96),
+     C(aa679cc066a8040b), C(d1d44fe99451ef72), C(ec951ba8e51e3545),
+     C(c0ca86b360746e96), C(aa679cc066a8040b), C(51065861ece6ffc1),
+     C(76777368a2997e11), C(87f278f46731100c), C(bbaa4140bdba4527),
+     C(9c9cfe88)},
+    {C(105191e0ec8f7f60), C(5918dbfcca971e79), C(6b285c8a944767b9),
+     C(d3e86ac4f5eccfa4), C(e5399df2b106ca1), C(814aadfacd217f1d),
+     C(2754e3def1c405a9), C(d3e86ac4f5eccfa4), C(e5399df2b106ca1),
+     C(814aadfacd217f1d), C(2754e3def1c405a9), C(99290323b9f06e74),
+     C(a9782e043f271461), C(13c8b3b8c275a860), C(6038d620e581e9e7),
+     C(b70a6ddd)},
+    {C(a5b88bf7399a9f07), C(fca3ddfd96461cc4), C(ebe738fdc0282fc6),
+     C(69afbc800606d0fb), C(6104b97a9db12df7), C(fcc09198bb90bf9f),
+     C(c5e077e41a65ba91), C(69afbc800606d0fb), C(6104b97a9db12df7),
+     C(fcc09198bb90bf9f), C(c5e077e41a65ba91), C(db261835ee8aa08e),
+     C(db0ee662e5796dc9), C(fc1880ecec499e5f), C(648866fbe1502034),
+     C(dea37298)},
+    {C(d08c3f5747d84f50), C(4e708b27d1b6f8ac), C(70f70fd734888606),
+     C(909ae019d761d019), C(368bf4aab1b86ef9), C(308bd616d5460239),
+     C(4fd33269f76783ea), C(909ae019d761d019), C(368bf4aab1b86ef9),
+     C(308bd616d5460239), C(4fd33269f76783ea), C(7d53b37c19713eab),
+     C(6bba6eabda58a897), C(91abb50efc116047), C(4e902f347e0e0e35),
+     C(8f480819)},
+    {C(2f72d12a40044b4b), C(889689352fec53de), C(f03e6ad87eb2f36),
+     C(ef79f28d874b9e2d), C(b512089e8e63b76c), C(24dc06833bf193a9),
+     C(3c23308ba8e99d7e), C(ef79f28d874b9e2d), C(b512089e8e63b76c),
+     C(24dc06833bf193a9), C(3c23308ba8e99d7e), C(5ceff7b85cacefb7),
+     C(ef390338898cd73), C(b12967d7d2254f54), C(de874cbd8aef7b75), C(30b3b16)},
+    {C(aa1f61fdc5c2e11e), C(c2c56cd11277ab27), C(a1e73069fdf1f94f),
+     C(8184bab36bb79df0), C(c81929ce8655b940), C(301b11bf8a4d8ce8),
+     C(73126fd45ab75de9), C(8184bab36bb79df0), C(c81929ce8655b940),
+     C(301b11bf8a4d8ce8), C(73126fd45ab75de9), C(4bd6f76e4888229a),
+     C(9aae355b54a756d5), C(ca3de9726f6e99d5), C(83f80cac5bc36852),
+     C(f31bc4e8)},
+    {C(9489b36fe2246244), C(3355367033be74b8), C(5f57c2277cbce516),
+     C(bc61414f9802ecaf), C(8edd1e7a50562924), C(48f4ab74a35e95f2),
+     C(cc1afcfd99a180e7), C(bc61414f9802ecaf), C(8edd1e7a50562924),
+     C(48f4ab74a35e95f2), C(cc1afcfd99a180e7), C(517dd5e3acf66110),
+     C(7dd3ad9e8978b30d), C(1f6d5dfc70de812b), C(947daaba6441aaf3),
+     C(419f953b)},
+    {C(358d7c0476a044cd), C(e0b7b47bcbd8854f), C(ffb42ec696705519),
+     C(d45e44c263e95c38), C(df61db53923ae3b1), C(f2bc948cc4fc027c),
+     C(8a8000c6066772a3), C(d45e44c263e95c38), C(df61db53923ae3b1),
+     C(f2bc948cc4fc027c), C(8a8000c6066772a3), C(9fd93c942d31fa17),
+     C(d7651ecebe09cbd3), C(68682cefb6a6f165), C(541eb99a2dcee40e),
+     C(20e9e76d)},
+    {C(b0c48df14275265a), C(9da4448975905efa), C(d716618e414ceb6d),
+     C(30e888af70df1e56), C(4bee54bd47274f69), C(178b4059e1a0afe5),
+     C(6e2c96b7f58e5178), C(30e888af70df1e56), C(4bee54bd47274f69),
+     C(178b4059e1a0afe5), C(6e2c96b7f58e5178), C(bb429d3b9275e9bc),
+     C(c198013f09cafdc6), C(ec0a6ee4fb5de348), C(744e1e8ed2eb1eb0),
+     C(646f0ff8)},
+    {C(daa70bb300956588), C(410ea6883a240c6d), C(f5c8239fb5673eb3),
+     C(8b1d7bb4903c105f), C(cfb1c322b73891d4), C(5f3b792b22f07297),
+     C(fd64061f8be86811), C(8b1d7bb4903c105f), C(cfb1c322b73891d4),
+     C(5f3b792b22f07297), C(fd64061f8be86811), C(1d2db712921cfc2b),
+     C(cd1b2b2f2cee18ae), C(6b6f8790dc7feb09), C(46c179efa3f0f518),
+     C(eeb7eca8)},
+    {C(4ec97a20b6c4c7c2), C(5913b1cd454f29fd), C(a9629f9daf06d685),
+     C(852c9499156a8f3), C(3a180a6abfb79016), C(9fc3c4764037c3c9),
+     C(2890c42fc0d972cf), C(852c9499156a8f3), C(3a180a6abfb79016),
+     C(9fc3c4764037c3c9), C(2890c42fc0d972cf), C(1f92231d4e537651),
+     C(fab8bb07aa54b7b9), C(e05d2d771c485ed4), C(d50b34bf808ca731), C(8112bb9)},
+    {C(5c3323628435a2e8), C(1bea45ce9e72a6e3), C(904f0a7027ddb52e),
+     C(939f31de14dcdc7b), C(a68fdf4379df068), C(f169e1f0b835279d),
+     C(7498e432f9619b27), C(939f31de14dcdc7b), C(a68fdf4379df068),
+     C(f169e1f0b835279d), C(7498e432f9619b27), C(1aa2a1f11088e785),
+     C(d6ad72f45729de78), C(9a63814157c80267), C(55538e35c648e435),
+     C(85a6d477)},
+    {C(c1ef26bea260abdb), C(6ee423f2137f9280), C(df2118b946ed0b43),
+     C(11b87fb1b900cc39), C(e33e59b90dd815b1), C(aa6cb5c4bafae741),
+     C(739699951ca8c713), C(11b87fb1b900cc39), C(e33e59b90dd815b1),
+     C(aa6cb5c4bafae741), C(739699951ca8c713), C(2b4389a967310077),
+     C(1d5382568a31c2c9), C(55d1e787fbe68991), C(277c254bc31301e7),
+     C(56f76c84)},
+    {C(6be7381b115d653a), C(ed046190758ea511), C(de6a45ffc3ed1159),
+     C(a64760e4041447d0), C(e3eac49f3e0c5109), C(dd86c4d4cb6258e2),
+     C(efa9857afd046c7f), C(a64760e4041447d0), C(e3eac49f3e0c5109),
+     C(dd86c4d4cb6258e2), C(efa9857afd046c7f), C(fab793dae8246f16),
+     C(c9e3b121b31d094c), C(a2a0f55858465226), C(dba6f0ff39436344),
+     C(9af45d55)},
+    {C(ae3eece1711b2105), C(14fd3f4027f81a4a), C(abb7e45177d151db),
+     C(501f3e9b18861e44), C(465201170074e7d8), C(96d5c91970f2cb12),
+     C(40fd28c43506c95d), C(501f3e9b18861e44), C(465201170074e7d8),
+     C(96d5c91970f2cb12), C(40fd28c43506c95d), C(e86c4b07802aaff3),
+     C(f317d14112372a70), C(641b13e587711650), C(4915421ab1090eaa),
+     C(d1c33760)},
+    {C(376c28588b8fb389), C(6b045e84d8491ed2), C(4e857effb7d4e7dc),
+     C(154dd79fd2f984b4), C(f11171775622c1c3), C(1fbe30982e78e6f0),
+     C(a460a15dcf327e44), C(154dd79fd2f984b4), C(f11171775622c1c3),
+     C(1fbe30982e78e6f0), C(a460a15dcf327e44), C(f359e0900cc3d582),
+     C(7e11070447976d00), C(324e6daf276ea4b5), C(7aa6e2df0cc94fa2),
+     C(c56bbf69)},
+    {C(58d943503bb6748f), C(419c6c8e88ac70f6), C(586760cbf3d3d368),
+     C(b7e164979d5ccfc1), C(12cb4230d26bf286), C(f1bf910d44bd84cb),
+     C(b32c24c6a40272), C(b7e164979d5ccfc1), C(12cb4230d26bf286),
+     C(f1bf910d44bd84cb), C(b32c24c6a40272), C(11ed12e34c48c039),
+     C(b0c2538e51d0a6ac), C(4269bb773e1d553a), C(e35a9dbabd34867), C(abecfb9b)},
+    {C(dfff5989f5cfd9a1), C(bcee2e7ea3a96f83), C(681c7874adb29017),
+     C(3ff6c8ac7c36b63a), C(48bc8831d849e326), C(30b078e76b0214e2),
+     C(42954e6ad721b920), C(3ff6c8ac7c36b63a), C(48bc8831d849e326),
+     C(30b078e76b0214e2), C(42954e6ad721b920), C(f9aeb33d164b4472),
+     C(7b353b110831dbdc), C(16f64c82f44ae17b), C(b71244cc164b3b2b),
+     C(8de13255)},
+    {C(7fb19eb1a496e8f5), C(d49e5dfdb5c0833f), C(c0d5d7b2f7c48dc7),
+     C(1a57313a32f22dde), C(30af46e49850bf8b), C(aa0fe8d12f808f83),
+     C(443e31d70873bb6b), C(1a57313a32f22dde), C(30af46e49850bf8b),
+     C(aa0fe8d12f808f83), C(443e31d70873bb6b), C(bbeb67c49c9fdc13),
+     C(18f1e2a88f59f9d5), C(fb1b05038e5def11), C(d0450b5ce4c39c52),
+     C(a98ee299)},
+    {C(5dba5b0dadccdbaa), C(4ba8da8ded87fcdc), C(f693fdd25badf2f0),
+     C(e9029e6364286587), C(ae69f49ecb46726c), C(18e002679217c405),
+     C(bd6d66e85332ae9f), C(e9029e6364286587), C(ae69f49ecb46726c),
+     C(18e002679217c405), C(bd6d66e85332ae9f), C(6bf330b1c353dd2a),
+     C(74e9f2e71e3a4152), C(3f85560b50f6c413), C(d33a52a47eaed2b4),
+     C(3015f556)},
+    {C(688bef4b135a6829), C(8d31d82abcd54e8e), C(f95f8a30d55036d7),
+     C(3d8c90e27aa2e147), C(2ec937ce0aa236b4), C(89b563996d3a0b78),
+     C(39b02413b23c3f08), C(3d8c90e27aa2e147), C(2ec937ce0aa236b4),
+     C(89b563996d3a0b78), C(39b02413b23c3f08), C(8d475a2e64faf2d2),
+     C(48567f7dca46ecaf), C(254cda08d5f87a6d), C(ec6ae9f729c47039),
+     C(5a430e29)},
+    {C(d8323be05433a412), C(8d48fa2b2b76141d), C(3d346f23978336a5),
+     C(4d50c7537562033f), C(57dc7625b61dfe89), C(9723a9f4c08ad93a),
+     C(5309596f48ab456b), C(4d50c7537562033f), C(57dc7625b61dfe89),
+     C(9723a9f4c08ad93a), C(5309596f48ab456b), C(7e453088019d220f),
+     C(8776067ba6ab9714), C(67e1d06bd195de39), C(74a1a32f8994b918),
+     C(2797add0)},
+    {C(3b5404278a55a7fc), C(23ca0b327c2d0a81), C(a6d65329571c892c),
+     C(45504801e0e6066b), C(86e6c6d6152a3d04), C(4f3db1c53eca2952),
+     C(d24d69b3e9ef10f3), C(45504801e0e6066b), C(86e6c6d6152a3d04),
+     C(4f3db1c53eca2952), C(d24d69b3e9ef10f3), C(93a0de2219e66a70),
+     C(8932c7115ccb1f8a), C(5ef503fdf2841a8c), C(38064dd9efa80a41),
+     C(27d55016)},
+    {C(2a96a3f96c5e9bbc), C(8caf8566e212dda8), C(904de559ca16e45e),
+     C(f13bc2d9c2fe222e), C(be4ccec9a6cdccfd), C(37b2cbdd973a3ac9),
+     C(7b3223cd9c9497be), C(f13bc2d9c2fe222e), C(be4ccec9a6cdccfd),
+     C(37b2cbdd973a3ac9), C(7b3223cd9c9497be), C(d5904440f376f889),
+     C(62b13187699c473c), C(4751b89251f26726), C(9500d84fa3a61ba8),
+     C(84945a82)},
+    {C(22bebfdcc26d18ff), C(4b4d8dcb10807ba1), C(40265eee30c6b896),
+     C(3752b423073b119a), C(377dc5eb7c662bdb), C(2b9f07f93a6c25b9),
+     C(96f24ede2bdc0718), C(3752b423073b119a), C(377dc5eb7c662bdb),
+     C(2b9f07f93a6c25b9), C(96f24ede2bdc0718), C(f7699b12c31417bd),
+     C(17b366f401c58b2), C(bf60188d5f437b37), C(484436e56df17f04), C(3ef7e224)},
+    {C(627a2249ec6bbcc2), C(c0578b462a46735a), C(4974b8ee1c2d4f1f),
+     C(ebdbb918eb6d837f), C(8fb5f218dd84147c), C(c77dd1f881df2c54),
+     C(62eac298ec226dc3), C(ebdbb918eb6d837f), C(8fb5f218dd84147c),
+     C(c77dd1f881df2c54), C(62eac298ec226dc3), C(43eded83c4b60bd0),
+     C(9a0a403b5487503b), C(25f305d9147f0bda), C(3ad417f511bc1e64),
+     C(35ed8dc8)},
+    {C(3abaf1667ba2f3e0), C(ee78476b5eeadc1), C(7e56ac0a6ca4f3f4),
+     C(f1b9b413df9d79ed), C(a7621b6fd02db503), C(d92f7ba9928a4ffe),
+     C(53f56babdcae96a6), C(f1b9b413df9d79ed), C(a7621b6fd02db503),
+     C(d92f7ba9928a4ffe), C(53f56babdcae96a6), C(5302b89fc48713ab),
+     C(d03e3b04dbe7a2f2), C(fa74ef8af6d376a7), C(103c8cdea1050ef2),
+     C(6a75e43d)},
+    {C(3931ac68c5f1b2c9), C(efe3892363ab0fb0), C(40b707268337cd36),
+     C(a53a6b64b1ac85c9), C(d50e7f86ee1b832b), C(7bab08fdd26ba0a4),
+     C(7587743c18fe2475), C(a53a6b64b1ac85c9), C(d50e7f86ee1b832b),
+     C(7bab08fdd26ba0a4), C(7587743c18fe2475), C(e3b5d5d490cf5761),
+     C(dfc053f7d065edd5), C(42ffd8d5fb70129f), C(599ca38677cccdc3),
+     C(235d9805)},
+    {C(b98fb0606f416754), C(46a6e5547ba99c1e), C(c909d82112a8ed2),
+     C(dbfaae9642b3205a), C(f676a1339402bcb9), C(f4f12a5b1ac11f29),
+     C(7db8bad81249dee4), C(dbfaae9642b3205a), C(f676a1339402bcb9),
+     C(f4f12a5b1ac11f29), C(7db8bad81249dee4), C(b26e46f2da95922e),
+     C(2aaedd5e12e3c611), C(a0e2d9082966074), C(c64da8a167add63d), C(f7d69572)},
+    {C(7f7729a33e58fcc4), C(2e4bc1e7a023ead4), C(e707008ea7ca6222),
+     C(47418a71800334a0), C(d10395d8fc64d8a4), C(8257a30062cb66f),
+     C(6786f9b2dc1ff18a), C(47418a71800334a0), C(d10395d8fc64d8a4),
+     C(8257a30062cb66f), C(6786f9b2dc1ff18a), C(5633f437bb2f180f),
+     C(e5a3a405737d22d6), C(ca0ff1ef6f7f0b74), C(d0ae600684b16df8),
+     C(bacd0199)},
+    {C(42a0aa9ce82848b3), C(57232730e6bee175), C(f89bb3f370782031),
+     C(caa33cf9b4f6619c), C(b2c8648ad49c209f), C(9e89ece0712db1c0),
+     C(101d8274a711a54b), C(caa33cf9b4f6619c), C(b2c8648ad49c209f),
+     C(9e89ece0712db1c0), C(101d8274a711a54b), C(538e79f1e70135cd),
+     C(e1f5a76f983c844e), C(653c082fd66088fc), C(1b9c9b464b654958),
+     C(e428f50e)},
+    {C(6b2c6d38408a4889), C(de3ef6f68fb25885), C(20754f456c203361),
+     C(941f5023c0c943f9), C(dfdeb9564fd66f24), C(2140cec706b9d406),
+     C(7b22429b131e9c72), C(941f5023c0c943f9), C(dfdeb9564fd66f24),
+     C(2140cec706b9d406), C(7b22429b131e9c72), C(94215c22eb940f45),
+     C(d28b9ed474f7249a), C(6f25e88f2fbf9f56), C(b6718f9e605b38ac),
+     C(81eaaad3)},
+    {C(930380a3741e862a), C(348d28638dc71658), C(89dedcfd1654ea0d),
+     C(7e7f61684080106), C(837ace9794582976), C(5ac8ca76a357eb1b),
+     C(32b58308625661fb), C(7e7f61684080106), C(837ace9794582976),
+     C(5ac8ca76a357eb1b), C(32b58308625661fb), C(c09705c4572025d9),
+     C(f9187f6af0291303), C(1c0edd8ee4b02538), C(e6cb105daa0578a), C(addbd3e3)},
+    {C(94808b5d2aa25f9a), C(cec72968128195e0), C(d9f4da2bdc1e130f),
+     C(272d8dd74f3006cc), C(ec6c2ad1ec03f554), C(4ad276b249a5d5dd),
+     C(549a22a17c0cde12), C(272d8dd74f3006cc), C(ec6c2ad1ec03f554),
+     C(4ad276b249a5d5dd), C(549a22a17c0cde12), C(602119cb824d7cde),
+     C(f4d3cef240ef35fa), C(e889895e01911bc7), C(785a7e5ac20e852b),
+     C(e66dbca0)},
+    {C(b31abb08ae6e3d38), C(9eb9a95cbd9e8223), C(8019e79b7ee94ea9),
+     C(7b2271a7a3248e22), C(3b4f700e5a0ba523), C(8ebc520c227206fe),
+     C(da3f861490f5d291), C(7b2271a7a3248e22), C(3b4f700e5a0ba523),
+     C(8ebc520c227206fe), C(da3f861490f5d291), C(d08a689f9f3aa60e),
+     C(547c1b97a068661f), C(4b15a67fa29172f0), C(eaf40c085191d80f),
+     C(afe11fd5)},
+    {C(dccb5534a893ea1a), C(ce71c398708c6131), C(fe2396315457c164),
+     C(3f1229f4d0fd96fb), C(33130aa5fa9d43f2), C(e42693d5b34e63ab),
+     C(2f4ef2be67f62104), C(3f1229f4d0fd96fb), C(33130aa5fa9d43f2),
+     C(e42693d5b34e63ab), C(2f4ef2be67f62104), C(372e5153516e37b9),
+     C(af9ec142ab12cc86), C(777920c09345e359), C(e7c4a383bef8adc6),
+     C(a71a406f)},
+    {C(6369163565814de6), C(8feb86fb38d08c2f), C(4976933485cc9a20),
+     C(7d3e82d5ba29a90d), C(d5983cc93a9d126a), C(37e9dfd950e7b692),
+     C(80673be6a7888b87), C(7d3e82d5ba29a90d), C(d5983cc93a9d126a),
+     C(37e9dfd950e7b692), C(80673be6a7888b87), C(57f732dc600808bc),
+     C(59477199802cc78b), C(f824810eb8f2c2de), C(c4a3437f05b3b61c),
+     C(9d90eaf5)},
+    {C(edee4ff253d9f9b3), C(96ef76fb279ef0ad), C(a4d204d179db2460),
+     C(1f3dcdfa513512d6), C(4dc7ec07283117e4), C(4438bae88ae28bf9),
+     C(aa7eae72c9244a0d), C(1f3dcdfa513512d6), C(4dc7ec07283117e4),
+     C(4438bae88ae28bf9), C(aa7eae72c9244a0d), C(b9aedc8d3ecc72df),
+     C(b75a8eb090a77d62), C(6b15677f9cd91507), C(51d8282cb3a9ddbf),
+     C(6665db10)},
+    {C(941993df6e633214), C(929bc1beca5b72c6), C(141fc52b8d55572d),
+     C(b3b782ad308f21ed), C(4f2676485041dee0), C(bfe279aed5cb4bc8),
+     C(2a62508a467a22ff), C(b3b782ad308f21ed), C(4f2676485041dee0),
+     C(bfe279aed5cb4bc8), C(2a62508a467a22ff), C(e74d29eab742385d),
+     C(56b05cd90ecfc293), C(c603728ea73f8844), C(8638fcd21bc692c4),
+     C(9c977cbf)},
+    {C(859838293f64cd4c), C(484403b39d44ad79), C(bf674e64d64b9339),
+     C(44d68afda9568f08), C(478568ed51ca1d65), C(679c204ad3d9e766),
+     C(b28e788878488dc1), C(44d68afda9568f08), C(478568ed51ca1d65),
+     C(679c204ad3d9e766), C(b28e788878488dc1), C(d001a84d3a84fae6),
+     C(d376958fe4cb913e), C(17435277e36c86f0), C(23657b263c347aa6),
+     C(ee83ddd4)},
+    {C(c19b5648e0d9f555), C(328e47b2b7562993), C(e756b92ba4bd6a51),
+     C(c3314e362764ddb8), C(6481c084ee9ec6b5), C(ede23fb9a251771),
+     C(bd617f2643324590), C(c3314e362764ddb8), C(6481c084ee9ec6b5),
+     C(ede23fb9a251771), C(bd617f2643324590), C(d2d30c9b95e030f5),
+     C(8a517312ffc5795e), C(8b1f325033bd535e), C(3ee6e867e03f2892), C(26519cc)},
+    {C(f963b63b9006c248), C(9e9bf727ffaa00bc), C(c73bacc75b917e3a),
+     C(2c6aa706129cc54c), C(17a706f59a49f086), C(c7c1eec455217145),
+     C(6adfdc6e07602d42), C(2c6aa706129cc54c), C(17a706f59a49f086),
+     C(c7c1eec455217145), C(6adfdc6e07602d42), C(fb75fca30d848dd2),
+     C(5228c9ed14653ed4), C(953958910153b1a2), C(a430103a24f42a5d),
+     C(a485a53f)},
+    {C(6a8aa0852a8c1f3b), C(c8f1e5e206a21016), C(2aa554aed1ebb524),
+     C(fc3e3c322cd5d89b), C(b7e3911dc2bd4ebb), C(fcd6da5e5fae833a),
+     C(51ed3c41f87f9118), C(fc3e3c322cd5d89b), C(b7e3911dc2bd4ebb),
+     C(fcd6da5e5fae833a), C(51ed3c41f87f9118), C(f31750cbc19c420a),
+     C(186dab1abada1d86), C(ca7f88cb894b3cd7), C(2859eeb1c373790c),
+     C(f62bc412)},
+    {C(740428b4d45e5fb8), C(4c95a4ce922cb0a5), C(e99c3ba78feae796),
+     C(914f1ea2fdcebf5c), C(9566453c07cd0601), C(9841bf66d0462cd),
+     C(79140c1c18536aeb), C(914f1ea2fdcebf5c), C(9566453c07cd0601),
+     C(9841bf66d0462cd), C(79140c1c18536aeb), C(a963b930b05820c2),
+     C(6a7d9fa0c8c45153), C(64214c40d07cf39b), C(7057daf1d806c014),
+     C(8975a436)},
+    {C(658b883b3a872b86), C(2f0e303f0f64827a), C(975337e23dc45e1),
+     C(99468a917986162b), C(7b31434aac6e0af0), C(f6915c1562c7d82f),
+     C(e4071d82a6dd71db), C(99468a917986162b), C(7b31434aac6e0af0),
+     C(f6915c1562c7d82f), C(e4071d82a6dd71db), C(5f5331f077b5d996),
+     C(7b314ba21b747a4f), C(5a73cb9521da17f5), C(12ed435fae286d86),
+     C(94ff7f41)},
+    {C(6df0a977da5d27d4), C(891dd0e7cb19508), C(fd65434a0b71e680),
+     C(8799e4740e573c50), C(9e739b52d0f341e8), C(cdfd34ba7d7b03eb),
+     C(5061812ce6c88499), C(8799e4740e573c50), C(9e739b52d0f341e8),
+     C(cdfd34ba7d7b03eb), C(5061812ce6c88499), C(612b8d8f2411dc5c),
+     C(878bd883d29c7787), C(47a846727182bb), C(ec4949508c8b3b9a), C(760aa031)},
+    {C(a900275464ae07ef), C(11f2cfda34beb4a3), C(9abf91e5a1c38e4),
+     C(8063d80ab26f3d6d), C(4177b4b9b4f0393f), C(6de42ba8672b9640),
+     C(d0bccdb72c51c18), C(8063d80ab26f3d6d), C(4177b4b9b4f0393f),
+     C(6de42ba8672b9640), C(d0bccdb72c51c18), C(af3f611b7f22cf12),
+     C(3863c41492645755), C(928c7a616a8f14f9), C(a82c78eb2eadc58b),
+     C(3bda76df)},
+    {C(810bc8aa0c40bcb0), C(448a019568d01441), C(f60ec52f60d3aeae),
+     C(52c44837aa6dfc77), C(15d8d8fccdd6dc5b), C(345b793ccfa93055),
+     C(932160fe802ca975), C(52c44837aa6dfc77), C(15d8d8fccdd6dc5b),
+     C(345b793ccfa93055), C(932160fe802ca975), C(a624b0dd93fc18cd),
+     C(d955b254c2037f1e), C(e540533d370a664c), C(2ba4ec12514e9d7), C(498e2e65)},
+    {C(22036327deb59ed7), C(adc05ceb97026a02), C(48bff0654262672b),
+     C(c791b313aba3f258), C(443c7757a4727bee), C(e30e4b2372171bdf),
+     C(f3db986c4156f3cb), C(c791b313aba3f258), C(443c7757a4727bee),
+     C(e30e4b2372171bdf), C(f3db986c4156f3cb), C(a939aefab97c6e15),
+     C(dbeb8acf1d5b0e6c), C(1e0eab667a795bba), C(80dd539902df4d50),
+     C(d38deb48)},
+    {C(7d14dfa9772b00c8), C(595735efc7eeaed7), C(29872854f94c3507),
+     C(bc241579d8348401), C(16dc832804d728f0), C(e9cc71ae64e3f09e),
+     C(bef634bc978bac31), C(bc241579d8348401), C(16dc832804d728f0),
+     C(e9cc71ae64e3f09e), C(bef634bc978bac31), C(7f64b1fa2a9129e),
+     C(71d831bd530ac7f3), C(c7ad0a8a6d5be6f1), C(82a7d3a815c7aaab),
+     C(82b3fb6b)},
+    {C(2d777cddb912675d), C(278d7b10722a13f9), C(f5c02bfb7cc078af),
+     C(4283001239888836), C(f44ca39a6f79db89), C(ed186122d71bcc9f),
+     C(8620017ab5f3ba3b), C(4283001239888836), C(f44ca39a6f79db89),
+     C(ed186122d71bcc9f), C(8620017ab5f3ba3b), C(e787472187f176c),
+     C(267e64c4728cf181), C(f1ba4b3007c15e30), C(8e3a75d5b02ecfc0),
+     C(e500e25f)},
+    {C(f2ec98824e8aa613), C(5eb7e3fb53fe3bed), C(12c22860466e1dd4),
+     C(374dd4288e0b72e5), C(ff8916db706c0df4), C(cb1a9e85de5e4b8d),
+     C(d4d12afb67a27659), C(374dd4288e0b72e5), C(ff8916db706c0df4),
+     C(cb1a9e85de5e4b8d), C(d4d12afb67a27659), C(feb69095d1ba175a),
+     C(e2003aab23a47fad), C(8163a3ecab894b49), C(46d356674ce041f6),
+     C(bd2bb07c)},
+    {C(5e763988e21f487f), C(24189de8065d8dc5), C(d1519d2403b62aa0),
+     C(9136456740119815), C(4d8ff7733b27eb83), C(ea3040bc0c717ef8),
+     C(7617ab400dfadbc), C(9136456740119815), C(4d8ff7733b27eb83),
+     C(ea3040bc0c717ef8), C(7617ab400dfadbc), C(fb336770c10b17a1),
+     C(6123b68b5b31f151), C(1e147d5f295eccf2), C(9ecbb1333556f977),
+     C(3a2b431d)},
+    {C(48949dc327bb96ad), C(e1fd21636c5c50b4), C(3f6eb7f13a8712b4),
+     C(14cf7f02dab0eee8), C(6d01750605e89445), C(4f1cf4006e613b78),
+     C(57c40c4db32bec3b), C(14cf7f02dab0eee8), C(6d01750605e89445),
+     C(4f1cf4006e613b78), C(57c40c4db32bec3b), C(1fde5a347f4a326e),
+     C(cb5a54308adb0e3f), C(14994b2ba447a23c), C(7067d0abb4257b68),
+     C(7322a83d)},
+    {C(b7c4209fb24a85c5), C(b35feb319c79ce10), C(f0d3de191833b922),
+     C(570d62758ddf6397), C(5e0204fb68a7b800), C(4383a9236f8b5a2b),
+     C(7bc1a64641d803a4), C(570d62758ddf6397), C(5e0204fb68a7b800),
+     C(4383a9236f8b5a2b), C(7bc1a64641d803a4), C(5434d61285099f7a),
+     C(d49449aacdd5dd67), C(97855ba0e9a7d75d), C(da67328062f3a62f),
+     C(a645ca1c)},
+    {C(9c9e5be0943d4b05), C(b73dc69e45201cbb), C(aab17180bfe5083d),
+     C(c738a77a9a55f0e2), C(705221addedd81df), C(fd9bd8d397abcfa3),
+     C(8ccf0004aa86b795), C(c738a77a9a55f0e2), C(705221addedd81df),
+     C(fd9bd8d397abcfa3), C(8ccf0004aa86b795), C(2bb5db2280068206),
+     C(8c22d29f307a01d), C(274a22de02f473c8), C(b8791870f4268182), C(8909a45a)},
+    {C(3898bca4dfd6638d), C(f911ff35efef0167), C(24bdf69e5091fc88),
+     C(9b82567ab6560796), C(891b69462b41c224), C(8eccc7e4f3af3b51),
+     C(381e54c3c8f1c7d0), C(9b82567ab6560796), C(891b69462b41c224),
+     C(8eccc7e4f3af3b51), C(381e54c3c8f1c7d0), C(c80fbc489a558a55),
+     C(1ba88e062a663af7), C(af7b1ef1c0116303), C(bd20e1a5a6b1a0cd),
+     C(bd30074c)},
+    {C(5b5d2557400e68e7), C(98d610033574cee), C(dfd08772ce385deb),
+     C(3c13e894365dc6c2), C(26fc7bbcda3f0ef), C(dbb71106cdbfea36),
+     C(785239a742c6d26d), C(3c13e894365dc6c2), C(26fc7bbcda3f0ef),
+     C(dbb71106cdbfea36), C(785239a742c6d26d), C(f810c415ae05b2f4),
+     C(bb9b9e7398526088), C(70128f1bf830a32b), C(bcc73f82b6410899),
+     C(c17cf001)},
+    {C(a927ed8b2bf09bb6), C(606e52f10ae94eca), C(71c2203feb35a9ee),
+     C(6e65ec14a8fb565), C(34bff6f2ee5a7f79), C(2e329a5be2c011b),
+     C(73161c93331b14f9), C(6e65ec14a8fb565), C(34bff6f2ee5a7f79),
+     C(2e329a5be2c011b), C(73161c93331b14f9), C(15d13f2408aecf88),
+     C(9f5b61b8a4b55b31), C(8fe25a43b296dba6), C(bdad03b7300f284e),
+     C(26ffd25a)},
+    {C(8d25746414aedf28), C(34b1629d28b33d3a), C(4d5394aea5f82d7b),
+     C(379f76458a3c8957), C(79dd080f9843af77), C(c46f0a7847f60c1d),
+     C(af1579c5797703cc), C(379f76458a3c8957), C(79dd080f9843af77),
+     C(c46f0a7847f60c1d), C(af1579c5797703cc), C(8b7d31f338755c14),
+     C(2eff97679512aaa8), C(df07d68e075179ed), C(c8fa6c7a729e7f1f),
+     C(f1d8ce3c)},
+    {C(b5bbdb73458712f2), C(1ff887b3c2a35137), C(7f7231f702d0ace9),
+     C(1e6f0910c3d25bd8), C(ad9e250862102467), C(1c842a07abab30cd),
+     C(cd8124176bac01ac), C(1e6f0910c3d25bd8), C(ad9e250862102467),
+     C(1c842a07abab30cd), C(cd8124176bac01ac), C(ea6ebe7a79b67edc),
+     C(73f598ac9db26713), C(4f4e72d7460b8fc), C(365dc4b9fdf13f21), C(3ee8fb17)},
+    {C(3d32a26e3ab9d254), C(fc4070574dc30d3a), C(f02629579c2b27c9),
+     C(b1cf09b0184a4834), C(5c03db48eb6cc159), C(f18c7fcf34d1df47),
+     C(dfb043419ecf1fa9), C(b1cf09b0184a4834), C(5c03db48eb6cc159),
+     C(f18c7fcf34d1df47), C(dfb043419ecf1fa9), C(dcd78d13f9ca658f),
+     C(4355d408ffe8e49f), C(81eefee908b593b4), C(590c213c20e981a3),
+     C(a77acc2a)},
+    {C(9371d3c35fa5e9a5), C(42967cf4d01f30), C(652d1eeae704145c),
+     C(ceaf1a0d15234f15), C(1450a54e45ba9b9), C(65e9c1fd885aa932),
+     C(354d4bc034ba8cbe), C(ceaf1a0d15234f15), C(1450a54e45ba9b9),
+     C(65e9c1fd885aa932), C(354d4bc034ba8cbe), C(8fd4ff484c08fb4b),
+     C(bf46749866f69ba0), C(cf1c21ede82c9477), C(4217548c43da109), C(f4556dee)},
+    {C(cbaa3cb8f64f54e0), C(76c3b48ee5c08417), C(9f7d24e87e61ce9),
+     C(85b8e53f22e19507), C(bb57137739ca486b), C(c77f131cca38f761),
+     C(c56ac3cf275be121), C(85b8e53f22e19507), C(bb57137739ca486b),
+     C(c77f131cca38f761), C(c56ac3cf275be121), C(9ec1a6c9109d2685),
+     C(3dad0922e76afdb0), C(fd58cbf952958103), C(7b04c908e78639a1),
+     C(de287a64)},
+    {C(b2e23e8116c2ba9f), C(7e4d9c0060101151), C(3310da5e5028f367),
+     C(adc52dddb76f6e5e), C(4aad4e925a962b68), C(204b79b7f7168e64),
+     C(df29ed6671c36952), C(adc52dddb76f6e5e), C(4aad4e925a962b68),
+     C(204b79b7f7168e64), C(df29ed6671c36952), C(e02927cac396d210),
+     C(5d500e71742b638a), C(5c9998af7f27b124), C(3fba9a2573dc2f7), C(878e55b9)},
+    {C(8aa77f52d7868eb9), C(4d55bd587584e6e2), C(d2db37041f495f5),
+     C(ce030d15b5fe2f4), C(86b4a7a0780c2431), C(ee070a9ae5b51db7),
+     C(edc293d9595be5d8), C(ce030d15b5fe2f4), C(86b4a7a0780c2431),
+     C(ee070a9ae5b51db7), C(edc293d9595be5d8), C(3dfc5ec108260a2b),
+     C(8afe28c7123bf4e2), C(da82ef38023a7a5f), C(3e1f77b0174b77c3), C(7648486)},
+    {C(858fea922c7fe0c3), C(cfe8326bf733bc6f), C(4e5e2018cf8f7dfc),
+     C(64fd1bc011e5bab7), C(5c9e858728015568), C(97ac42c2b00b29b1),
+     C(7f89caf08c109aee), C(64fd1bc011e5bab7), C(5c9e858728015568),
+     C(97ac42c2b00b29b1), C(7f89caf08c109aee), C(9a8af34fd0e9dacf),
+     C(bbc54161aa1507e0), C(7cda723ccbbfe5ee), C(2c289d839fb93f58),
+     C(57ac0fb1)},
+    {C(46ef25fdec8392b1), C(e48d7b6d42a5cd35), C(56a6fe1c175299ca),
+     C(fdfa836b41dcef62), C(2f8db8030e847e1b), C(5ba0a49ac4f9b0f8),
+     C(dae897ed3e3fce44), C(fdfa836b41dcef62), C(2f8db8030e847e1b),
+     C(5ba0a49ac4f9b0f8), C(dae897ed3e3fce44), C(9c432e31aef626e7),
+     C(9a36e1c6cd6e3dd), C(5095a167c34d19d), C(a70005cfa6babbea), C(d01967ca)},
+    {C(8d078f726b2df464), C(b50ee71cdcabb299), C(f4af300106f9c7ba),
+     C(7d222caae025158a), C(cc028d5fd40241b9), C(dd42515b639e6f97),
+     C(e08e86531a58f87f), C(7d222caae025158a), C(cc028d5fd40241b9),
+     C(dd42515b639e6f97), C(e08e86531a58f87f), C(d93612c835b37d7b),
+     C(91dd61729b2fa7f4), C(ba765a1bdda09db7), C(55258b451b2b1297),
+     C(96ecdf74)},
+    {C(35ea86e6960ca950), C(34fe1fe234fc5c76), C(a00207a3dc2a72b7),
+     C(80395e48739e1a67), C(74a67d8f7f43c3d7), C(dd2bdd1d62246c6e),
+     C(a1f44298ba80acf6), C(80395e48739e1a67), C(74a67d8f7f43c3d7),
+     C(dd2bdd1d62246c6e), C(a1f44298ba80acf6), C(ad86d86c187bf38),
+     C(26feea1f2eee240d), C(ed7f1fd066b23897), C(a768cf1e0fbb502), C(779f5506)},
+    {C(8aee9edbc15dd011), C(51f5839dc8462695), C(b2213e17c37dca2d),
+     C(133b299a939745c5), C(796e2aac053f52b3), C(e8d9fe1521a4a222),
+     C(819a8863e5d1c290), C(133b299a939745c5), C(796e2aac053f52b3),
+     C(e8d9fe1521a4a222), C(819a8863e5d1c290), C(c0737f0fe34d36ad),
+     C(e6d6d4a267a5cc31), C(98300a7911674c23), C(bef189661c257098),
+     C(3c94c2de)},
+    {C(c3e142ba98432dda), C(911d060cab126188), C(b753fbfa8365b844),
+     C(fd1a9ba5e71b08a2), C(7ac0dc2ed7778533), C(b543161ff177188a),
+     C(492fc08a6186f3f4), C(fd1a9ba5e71b08a2), C(7ac0dc2ed7778533),
+     C(b543161ff177188a), C(492fc08a6186f3f4), C(fc4745f516afd3b6),
+     C(88c30370a53080e), C(65a1bb34abc465e2), C(abbd14662911c8b3), C(39f98faf)},
+    {C(123ba6b99c8cd8db), C(448e582672ee07c4), C(cebe379292db9e65),
+     C(938f5bbab544d3d6), C(d2a95f9f2d376d73), C(68b2f16149e81aa3),
+     C(ad7e32f82d86c79d), C(938f5bbab544d3d6), C(d2a95f9f2d376d73),
+     C(68b2f16149e81aa3), C(ad7e32f82d86c79d), C(4574015ae8626ce2),
+     C(455aa6137386a582), C(658ad2542e8ec20), C(e31d7be2ca35d00), C(7af31199)},
+    {C(ba87acef79d14f53), C(b3e0fcae63a11558), C(d5ac313a593a9f45),
+     C(eea5f5a9f74af591), C(578710bcc36fbea2), C(7a8393432188931d),
+     C(705cfc5ec7cc172), C(eea5f5a9f74af591), C(578710bcc36fbea2),
+     C(7a8393432188931d), C(705cfc5ec7cc172), C(da85ebe5fc427976),
+     C(bfa5c7a454df54c8), C(4632b72a81bf66d2), C(5dd72877db539ee2),
+     C(e341a9d6)},
+    {C(bcd3957d5717dc3), C(2da746741b03a007), C(873816f4b1ece472),
+     C(2b826f1a2c08c289), C(da50f56863b55e74), C(b18712f6b3eed83b),
+     C(bdc7cc05ab4c685f), C(2b826f1a2c08c289), C(da50f56863b55e74),
+     C(b18712f6b3eed83b), C(bdc7cc05ab4c685f), C(9e45fb833d1b0af),
+     C(d7213081db29d82e), C(d2a6b6c6a09ed55e), C(98a7686cba323ca9),
+     C(ca24aeeb)},
+    {C(61442ff55609168e), C(6447c5fc76e8c9cf), C(6a846de83ae15728),
+     C(effc2663cffc777f), C(93214f8f463afbed), C(a156ef06066f4e4e),
+     C(a407b6ed8769d51e), C(effc2663cffc777f), C(93214f8f463afbed),
+     C(a156ef06066f4e4e), C(a407b6ed8769d51e), C(bb2f9ed29745c02a),
+     C(981eecd435b36ad9), C(461a5a05fb9cdff4), C(bd6cb2a87b9f910c),
+     C(b2252b57)},
+    {C(dbe4b1b2d174757f), C(506512da18712656), C(6857f3e0b8dd95f),
+     C(5a4fc2728a9bb671), C(ebb971522ec38759), C(1a5a093e6cf1f72b),
+     C(729b057fe784f504), C(5a4fc2728a9bb671), C(ebb971522ec38759),
+     C(1a5a093e6cf1f72b), C(729b057fe784f504), C(71fcbf42a767f9cf),
+     C(114cfe772da6cdd), C(60cdf9cb629d9d7a), C(e270d10ad088b24e), C(72c81da1)},
+    {C(531e8e77b363161c), C(eece0b43e2dae030), C(8294b82c78f34ed1),
+     C(e777b1fd580582f2), C(7b880f58da112699), C(562c6b189a6333f4),
+     C(139d64f88a611d4), C(e777b1fd580582f2), C(7b880f58da112699),
+     C(562c6b189a6333f4), C(139d64f88a611d4), C(53d8ef17eda64fa4),
+     C(bf3eded14dc60a04), C(2b5c559cf5ec07c5), C(8895f7339d03a48a),
+     C(6b9fce95)},
+    {C(f71e9c926d711e2b), C(d77af2853a4ceaa1), C(9aa0d6d76a36fae7),
+     C(dd16cd0fbc08393), C(29a414a5d8c58962), C(72793d8d1022b5b2),
+     C(2e8e69cf7cbffdf0), C(dd16cd0fbc08393), C(29a414a5d8c58962),
+     C(72793d8d1022b5b2), C(2e8e69cf7cbffdf0), C(3721c0473aa99c9a),
+     C(1cff4ed9c31cd91c), C(4990735033cc482b), C(7fdf8c701c72f577),
+     C(19399857)},
+    {C(cb20ac28f52df368), C(e6705ee7880996de), C(9b665cc3ec6972f2),
+     C(4260e8c254e9924b), C(f197a6eb4591572d), C(8e867ff0fb7ab27c),
+     C(f95502fb503efaf3), C(4260e8c254e9924b), C(f197a6eb4591572d),
+     C(8e867ff0fb7ab27c), C(f95502fb503efaf3), C(30c41876b08e3e22),
+     C(958e2419e3cd22f4), C(f0f3aa1fe119a107), C(481662310a379100),
+     C(3c57a994)},
+    {C(e4a794b4acb94b55), C(89795358057b661b), C(9c4cdcec176d7a70),
+     C(4890a83ee435bc8b), C(d8c1c00fceb00914), C(9e7111ba234f900f),
+     C(eb8dbab364d8b604), C(4890a83ee435bc8b), C(d8c1c00fceb00914),
+     C(9e7111ba234f900f), C(eb8dbab364d8b604), C(b3261452963eebb),
+     C(6cf94b02792c4f95), C(d88fa815ef1e8fc), C(2d687af66604c73), C(c053e729)},
+    {C(cb942e91443e7208), C(e335de8125567c2a), C(d4d74d268b86df1f),
+     C(8ba0fdd2ffc8b239), C(f413b366c1ffe02f), C(c05b2717c59a8a28),
+     C(981188eab4fcc8fb), C(8ba0fdd2ffc8b239), C(f413b366c1ffe02f),
+     C(c05b2717c59a8a28), C(981188eab4fcc8fb), C(e563f49a1d9072ba),
+     C(3c6a3aa4a26367dc), C(ba0db13448653f34), C(31065d756074d7d6),
+     C(51cbbba7)},
+    {C(ecca7563c203f7ba), C(177ae2423ef34bb2), C(f60b7243400c5731),
+     C(cf1edbfe7330e94e), C(881945906bcb3cc6), C(4acf0293244855da),
+     C(65ae042c1c2a28c2), C(cf1edbfe7330e94e), C(881945906bcb3cc6),
+     C(4acf0293244855da), C(65ae042c1c2a28c2), C(b25fa0a1cab33559),
+     C(d98e8daa28124131), C(fce17f50b9c351b3), C(3f995ccf7386864b),
+     C(1acde79a)},
+    {C(1652cb940177c8b5), C(8c4fe7d85d2a6d6d), C(f6216ad097e54e72),
+     C(f6521b912b368ae6), C(a9fe4eff81d03e73), C(d6f623629f80d1a3),
+     C(2b9604f32cb7dc34), C(f6521b912b368ae6), C(a9fe4eff81d03e73),
+     C(d6f623629f80d1a3), C(2b9604f32cb7dc34), C(2a43d84dcf59c7e2),
+     C(d0a197c70c5dae0b), C(6e84d4bbc71d76a0), C(c7e94620378c6cb2),
+     C(2d160d13)},
+    {C(31fed0fc04c13ce8), C(3d5d03dbf7ff240a), C(727c5c9b51581203),
+     C(6b5ffc1f54fecb29), C(a8e8e7ad5b9a21d9), C(c4d5a32cd6aac22d),
+     C(d7e274ad22d4a79a), C(6b5ffc1f54fecb29), C(a8e8e7ad5b9a21d9),
+     C(c4d5a32cd6aac22d), C(d7e274ad22d4a79a), C(368841ea5731a112),
+     C(feaf7bc2e73ca48f), C(636fb272e9ea1f6), C(5d9cb7580c3f6207), C(787f5801)},
+    {C(e7b668947590b9b3), C(baa41ad32938d3fa), C(abcbc8d4ca4b39e4),
+     C(381ee1b7ea534f4e), C(da3759828e3de429), C(3e015d76729f9955),
+     C(cbbec51a6485fbde), C(381ee1b7ea534f4e), C(da3759828e3de429),
+     C(3e015d76729f9955), C(cbbec51a6485fbde), C(9b86605281f20727),
+     C(fc6fcf508676982a), C(3b135f7a813a1040), C(d3a4706bea1db9c9),
+     C(c9629828)},
+    {C(1de2119923e8ef3c), C(6ab27c096cf2fe14), C(8c3658edca958891),
+     C(4cc8ed3ada5f0f2), C(4a496b77c1f1c04e), C(9085b0a862084201),
+     C(a1894bde9e3dee21), C(4cc8ed3ada5f0f2), C(4a496b77c1f1c04e),
+     C(9085b0a862084201), C(a1894bde9e3dee21), C(367fb472dc5b277d),
+     C(7d39ccca16fc6745), C(763f988d70db9106), C(a8b66f7fecb70f02),
+     C(be139231)},
+    {C(1269df1e69e14fa7), C(992f9d58ac5041b7), C(e97fcf695a7cbbb4),
+     C(e5d0549802d15008), C(424c134ecd0db834), C(6fc44fd91be15c6c),
+     C(a1a5ef95d50e537d), C(e5d0549802d15008), C(424c134ecd0db834),
+     C(6fc44fd91be15c6c), C(a1a5ef95d50e537d), C(d1e3daf5d05f5308),
+     C(4c7f81600eaa1327), C(109d1b8d1f9d0d2b), C(871e8699e0aeb862),
+     C(7df699ef)},
+    {C(820826d7aba567ff), C(1f73d28e036a52f3), C(41c4c5a73f3b0893),
+     C(aa0d74d4a98db89b), C(36fd486d07c56e1d), C(d0ad23cbb6660d8a),
+     C(1264a84665b35e19), C(aa0d74d4a98db89b), C(36fd486d07c56e1d),
+     C(d0ad23cbb6660d8a), C(1264a84665b35e19), C(789682bf7d781b33),
+     C(6bfa6abd2fb5722d), C(6779cb3623d33900), C(435ca5214e1ee5f0),
+     C(8ce6b96d)},
+    {C(ffe0547e4923cef9), C(3534ed49b9da5b02), C(548a273700fba03d),
+     C(28ac84ca70958f7e), C(d8ae575a68faa731), C(2aaaee9b9dcffd4c),
+     C(6c7faab5c285c6da), C(28ac84ca70958f7e), C(d8ae575a68faa731),
+     C(2aaaee9b9dcffd4c), C(6c7faab5c285c6da), C(45d94235f99ba78f),
+     C(ab5ea16f39497f5b), C(fb4d6c86fccbdca3), C(8104e6310a5fd2c7),
+     C(6f9ed99c)},
+    {C(72da8d1b11d8bc8b), C(ba94b56b91b681c6), C(4e8cc51bd9b0fc8c),
+     C(43505ed133be672a), C(e8f2f9d973c2774e), C(677b9b9c7cad6d97),
+     C(4e1f5d56ef17b906), C(43505ed133be672a), C(e8f2f9d973c2774e),
+     C(677b9b9c7cad6d97), C(4e1f5d56ef17b906), C(eea3a6038f983767),
+     C(87109f077f86db01), C(ecc1ca41f74d61cc), C(34a87e86e83bed17),
+     C(e0244796)},
+    {C(d62ab4e3f88fc797), C(ea86c7aeb6283ae4), C(b5b93e09a7fe465),
+     C(4344a1a0134afe2), C(ff5c17f02b62341d), C(3214c6a587ce4644),
+     C(a905e7ed0629d05c), C(4344a1a0134afe2), C(ff5c17f02b62341d),
+     C(3214c6a587ce4644), C(a905e7ed0629d05c), C(b5c72690cd716e82),
+     C(7c6097649e6ebe7b), C(7ceee8c6e56a4dcd), C(80ca849dc53eb9e4),
+     C(4ccf7e75)},
+    {C(d0f06c28c7b36823), C(1008cb0874de4bb8), C(d6c7ff816c7a737b),
+     C(489b697fe30aa65f), C(4da0fb621fdc7817), C(dc43583b82c58107),
+     C(4b0261debdec3cd6), C(489b697fe30aa65f), C(4da0fb621fdc7817),
+     C(dc43583b82c58107), C(4b0261debdec3cd6), C(a9748d7b6c0e016c),
+     C(7e8828f7ba4b034b), C(da0fa54348a2512a), C(ebf9745c0962f9ad),
+     C(915cef86)},
+    {C(99b7042460d72ec6), C(2a53e5e2b8e795c2), C(53a78132d9e1b3e3),
+     C(c043e67e6fc64118), C(ff0abfe926d844d3), C(f2a9fe5db2e910fe),
+     C(ce352cdc84a964dd), C(c043e67e6fc64118), C(ff0abfe926d844d3),
+     C(f2a9fe5db2e910fe), C(ce352cdc84a964dd), C(b89bc028aa5e6063),
+     C(a354e7fdac04459c), C(68d6547e6e980189), C(c968dddfd573773e),
+     C(5cb59482)},
+    {C(4f4dfcfc0ec2bae5), C(841233148268a1b8), C(9248a76ab8be0d3),
+     C(334c5a25b5903a8c), C(4c94fef443122128), C(743e7d8454655c40),
+     C(1ab1e6d1452ae2cd), C(334c5a25b5903a8c), C(4c94fef443122128),
+     C(743e7d8454655c40), C(1ab1e6d1452ae2cd), C(fec766de4a8e476c),
+     C(cc0929da9567e71b), C(5f9ef5b5f150c35a), C(87659cabd649768f),
+     C(6ca3f532)},
+    {C(fe86bf9d4422b9ae), C(ebce89c90641ef9c), C(1c84e2292c0b5659),
+     C(8bde625a10a8c50d), C(eb8271ded1f79a0b), C(14dc6844f0de7a3c),
+     C(f85b2f9541e7e6da), C(8bde625a10a8c50d), C(eb8271ded1f79a0b),
+     C(14dc6844f0de7a3c), C(f85b2f9541e7e6da), C(2fe22cfd1683b961),
+     C(ea1d75c5b7aa01ca), C(9eef60a44876bb95), C(950c818e505c6f7f),
+     C(e24f3859)},
+    {C(a90d81060932dbb0), C(8acfaa88c5fbe92b), C(7c6f3447e90f7f3f),
+     C(dd52fc14c8dd3143), C(1bc7508516e40628), C(3059730266ade626),
+     C(ffa526822f391c2), C(dd52fc14c8dd3143), C(1bc7508516e40628),
+     C(3059730266ade626), C(ffa526822f391c2), C(e25232d7afc8a406),
+     C(d2b8a5a3f3b5f670), C(6630f33edb7dfe32), C(c71250ba68c4ea86),
+     C(adf5a9c7)},
+    {C(17938a1b0e7f5952), C(22cadd2f56f8a4be), C(84b0d1183d5ed7c1),
+     C(c1336b92fef91bf6), C(80332a3945f33fa9), C(a0f68b86f726ff92),
+     C(a3db5282cf5f4c0b), C(c1336b92fef91bf6), C(80332a3945f33fa9),
+     C(a0f68b86f726ff92), C(a3db5282cf5f4c0b), C(82640b6fc4916607),
+     C(2dc2a3aa1a894175), C(8b4c852bdee7cc9), C(10b9d0a08b55ff83), C(32264b75)},
+    {C(de9e0cb0e16f6e6d), C(238e6283aa4f6594), C(4fb9c914c2f0a13b),
+     C(497cb912b670f3b), C(d963a3f02ff4a5b6), C(4fccefae11b50391),
+     C(42ba47db3f7672f), C(497cb912b670f3b), C(d963a3f02ff4a5b6),
+     C(4fccefae11b50391), C(42ba47db3f7672f), C(1d6b655a1889feef),
+     C(5f319abf8fafa19f), C(715c2e49deb14620), C(8d9153082ecdcea4),
+     C(a64b3376)},
+    {C(6d4b876d9b146d1a), C(aab2d64ce8f26739), C(d315f93600e83fe5),
+     C(2fe9fabdbe7fdd4), C(755db249a2d81a69), C(f27929f360446d71),
+     C(79a1bf957c0c1b92), C(2fe9fabdbe7fdd4), C(755db249a2d81a69),
+     C(f27929f360446d71), C(79a1bf957c0c1b92), C(3c8a28d4c936c9cd),
+     C(df0d3d13b2c6a902), C(c76702dd97cd2edd), C(1aa220f7be16517), C(d33890e)},
+    {C(e698fa3f54e6ea22), C(bd28e20e7455358c), C(9ace161f6ea76e66),
+     C(d53fb7e3c93a9e4), C(737ae71b051bf108), C(7ac71feb84c2df42),
+     C(3d8075cd293a15b4), C(d53fb7e3c93a9e4), C(737ae71b051bf108),
+     C(7ac71feb84c2df42), C(3d8075cd293a15b4), C(bf8cee5e095d8a7c),
+     C(e7086b3c7608143a), C(e55b0c2fa938d70c), C(fffb5f58e643649c),
+     C(926d4b63)},
+    {C(7bc0deed4fb349f7), C(1771aff25dc722fa), C(19ff0644d9681917),
+     C(cf7d7f25bd70cd2c), C(9464ed9baeb41b4f), C(b9064f5c3cb11b71),
+     C(237e39229b012b20), C(cf7d7f25bd70cd2c), C(9464ed9baeb41b4f),
+     C(b9064f5c3cb11b71), C(237e39229b012b20), C(dd54d3f5d982dffe),
+     C(7fc7562dbfc81dbf), C(5b0dd1924f70945), C(f1760537d8261135), C(d51ba539)},
+    {C(db4b15e88533f622), C(256d6d2419b41ce9), C(9d7c5378396765d5),
+     C(9040e5b936b8661b), C(276e08fa53ac27fd), C(8c944d39c2bdd2cc),
+     C(e2514c9802a5743c), C(9040e5b936b8661b), C(276e08fa53ac27fd),
+     C(8c944d39c2bdd2cc), C(e2514c9802a5743c), C(e82107b11ac90386),
+     C(7d6a22bc35055e6), C(fd6ea9d1c438d8ae), C(be6015149e981553), C(7f37636d)},
+    {C(922834735e86ecb2), C(363382685b88328e), C(e9c92960d7144630),
+     C(8431b1bfd0a2379c), C(90383913aea283f9), C(a6163831eb4924d2),
+     C(5f3921b4f9084aee), C(8431b1bfd0a2379c), C(90383913aea283f9),
+     C(a6163831eb4924d2), C(5f3921b4f9084aee), C(7a70061a1473e579),
+     C(5b19d80dcd2c6331), C(6196b97931faad27), C(869bf6828e237c3f),
+     C(b98026c0)},
+    {C(30f1d72c812f1eb8), C(b567cd4a69cd8989), C(820b6c992a51f0bc),
+     C(c54677a80367125e), C(3204fbdba462e606), C(8563278afc9eae69),
+     C(262147dd4bf7e566), C(c54677a80367125e), C(3204fbdba462e606),
+     C(8563278afc9eae69), C(262147dd4bf7e566), C(2178b63e7ee2d230),
+     C(e9c61ad81f5bff26), C(9af7a81b3c501eca), C(44104a3859f0238f),
+     C(b877767e)},
+    {C(168884267f3817e9), C(5b376e050f637645), C(1c18314abd34497a),
+     C(9598f6ab0683fcc2), C(1c805abf7b80e1ee), C(dec9ac42ee0d0f32),
+     C(8cd72e3912d24663), C(9598f6ab0683fcc2), C(1c805abf7b80e1ee),
+     C(dec9ac42ee0d0f32), C(8cd72e3912d24663), C(1f025d405f1c1d87),
+     C(bf7b6221e1668f8f), C(52316f64e692dbb0), C(7bf43df61ec51b39), C(aefae77)},
+    {C(82e78596ee3e56a7), C(25697d9c87f30d98), C(7600a8342834924d),
+     C(6ba372f4b7ab268b), C(8c3237cf1fe243df), C(3833fc51012903df),
+     C(8e31310108c5683f), C(6ba372f4b7ab268b), C(8c3237cf1fe243df),
+     C(3833fc51012903df), C(8e31310108c5683f), C(126593715c2de429),
+     C(48ca8f35a3f54b90), C(b9322b632f4f8b0), C(926bb169b7337693), C(f686911)},
+    {C(aa2d6cf22e3cc252), C(9b4dec4f5e179f16), C(76fb0fba1d99a99a),
+     C(9a62af3dbba140da), C(27857ea044e9dfc1), C(33abce9da2272647),
+     C(b22a7993aaf32556), C(9a62af3dbba140da), C(27857ea044e9dfc1),
+     C(33abce9da2272647), C(b22a7993aaf32556), C(bf8f88f8019bedf0),
+     C(ed2d7f01fb273905), C(6b45f15901b481cd), C(f88ebb413ba6a8d5),
+     C(3deadf12)},
+    {C(7bf5ffd7f69385c7), C(fc077b1d8bc82879), C(9c04e36f9ed83a24),
+     C(82065c62e6582188), C(8ef787fd356f5e43), C(2922e53e36e17dfa),
+     C(9805f223d385010b), C(82065c62e6582188), C(8ef787fd356f5e43),
+     C(2922e53e36e17dfa), C(9805f223d385010b), C(692154f3491b787d),
+     C(e7e64700e414fbf), C(757d4d4ab65069a0), C(cd029446a8e348e2), C(ccf02a4e)},
+    {C(e89c8ff9f9c6e34b), C(f54c0f669a49f6c4), C(fc3e46f5d846adef),
+     C(22f2aa3df2221cc), C(f66fea90f5d62174), C(b75defaeaa1dd2a7),
+     C(9b994cd9a7214fd5), C(22f2aa3df2221cc), C(f66fea90f5d62174),
+     C(b75defaeaa1dd2a7), C(9b994cd9a7214fd5), C(fac675a31804b773),
+     C(98bcb3b820c50fc6), C(e14af64d28cf0885), C(27466fbd2b360eb5),
+     C(176c1722)},
+    {C(a18fbcdccd11e1f4), C(8248216751dfd65e), C(40c089f208d89d7c),
+     C(229b79ab69ae97d), C(a87aabc2ec26e582), C(be2b053721eb26d2),
+     C(10febd7f0c3d6fcb), C(229b79ab69ae97d), C(a87aabc2ec26e582),
+     C(be2b053721eb26d2), C(10febd7f0c3d6fcb), C(9cc5b9b2f6e3bf7b),
+     C(655d8495fe624a86), C(6381a9f3d1f2bd7e), C(79ebabbfc25c83e2), C(26f82ad)},
+    {C(2d54f40cc4088b17), C(59d15633b0cd1399), C(a8cc04bb1bffd15b),
+     C(d332cdb073d8dc46), C(272c56466868cb46), C(7e7fcbe35ca6c3f3),
+     C(ee8f51e5a70399d4), C(d332cdb073d8dc46), C(272c56466868cb46),
+     C(7e7fcbe35ca6c3f3), C(ee8f51e5a70399d4), C(16737a9c7581fe7b),
+     C(ed04bf52f4b75dcb), C(9707ffb36bd30c1a), C(1390f236fdc0de3e),
+     C(b5244f42)},
+    {C(69276946cb4e87c7), C(62bdbe6183be6fa9), C(3ba9773dac442a1a),
+     C(702e2afc7f5a1825), C(8c49b11ea8151fdc), C(caf3fef61f5a86fa),
+     C(ef0b2ee8649d7272), C(702e2afc7f5a1825), C(8c49b11ea8151fdc),
+     C(caf3fef61f5a86fa), C(ef0b2ee8649d7272), C(9e34a4e08d9441e1),
+     C(7bdc0cd64d5af533), C(a926b14d99e3d868), C(fca923a17788cce4),
+     C(49a689e5)},
+    {C(668174a3f443df1d), C(407299392da1ce86), C(c2a3f7d7f2c5be28),
+     C(a590b202a7a5807b), C(968d2593f7ccb54e), C(9dd8d669e3e95dec),
+     C(ee0cc5dd58b6e93a), C(a590b202a7a5807b), C(968d2593f7ccb54e),
+     C(9dd8d669e3e95dec), C(ee0cc5dd58b6e93a), C(ac65d5a9466fb483),
+     C(221be538b2c9d806), C(5cbe9441784f9fd9), C(d4c7d5d6e3c122b8), C(59fcdd3)},
+    {C(5e29be847bd5046), C(b561c7f19c8f80c3), C(5e5abd5021ccaeaf),
+     C(7432d63888e0c306), C(74bbceeed479cb71), C(6471586599575fdf),
+     C(6a859ad23365cba2), C(7432d63888e0c306), C(74bbceeed479cb71),
+     C(6471586599575fdf), C(6a859ad23365cba2), C(f9ceec84acd18dcc),
+     C(74a242ff1907437c), C(f70890194e1ee913), C(777dfcb4bb01f0ba),
+     C(4f4b04e9)},
+    {C(cd0d79f2164da014), C(4c386bb5c5d6ca0c), C(8e771b03647c3b63),
+     C(69db23875cb0b715), C(ada8dd91504ae37f), C(46bf18dbf045ed6a),
+     C(e1b5f67b0645ab63), C(69db23875cb0b715), C(ada8dd91504ae37f),
+     C(46bf18dbf045ed6a), C(e1b5f67b0645ab63), C(877be8f5dcddff4),
+     C(6d471b5f9ca2e2d1), C(802c86d6f495b9bb), C(a1f9b9b22b3be704),
+     C(8b00f891)},
+    {C(e0e6fc0b1628af1d), C(29be5fb4c27a2949), C(1c3f781a604d3630),
+     C(c4af7faf883033aa), C(9bd296c4e9453cac), C(ca45426c1f7e33f9),
+     C(a6bbdcf7074d40c5), C(c4af7faf883033aa), C(9bd296c4e9453cac),
+     C(ca45426c1f7e33f9), C(a6bbdcf7074d40c5), C(e13a005d7142733b),
+     C(c02b7925c5eeefaf), C(d39119a60441e2d5), C(3c24c710df8f4d43),
+     C(16e114f3)},
+    {C(2058927664adfd93), C(6e8f968c7963baa5), C(af3dced6fff7c394),
+     C(42e34cf3d53c7876), C(9cddbb26424dc5e), C(64f6340a6d8eddad),
+     C(2196e488eb2a3a4b), C(42e34cf3d53c7876), C(9cddbb26424dc5e),
+     C(64f6340a6d8eddad), C(2196e488eb2a3a4b), C(c9e9da25911a16fd),
+     C(e21b4683f3e196a8), C(cb80bf1a4c6fdbb4), C(53792e9b3c3e67f8),
+     C(d6b6dadc)},
+    {C(dc107285fd8e1af7), C(a8641a0609321f3f), C(db06e89ffdc54466),
+     C(bcc7a81ed5432429), C(b6d7bdc6ad2e81f1), C(93605ec471aa37db),
+     C(a2a73f8a85a8e397), C(bcc7a81ed5432429), C(b6d7bdc6ad2e81f1),
+     C(93605ec471aa37db), C(a2a73f8a85a8e397), C(10a012b8ca7ac24b),
+     C(aac5fd63351595cf), C(5bb4c648a226dea0), C(9d11ecb2b5c05c5f),
+     C(897e20ac)},
+    {C(fbba1afe2e3280f1), C(755a5f392f07fce), C(9e44a9a15402809a),
+     C(6226a32e25099848), C(ea895661ecf53004), C(4d7e0158db2228b9),
+     C(e5a7d82922f69842), C(6226a32e25099848), C(ea895661ecf53004),
+     C(4d7e0158db2228b9), C(e5a7d82922f69842), C(2cea7713b69840ca),
+     C(18de7b9ae938375b), C(f127cca08f3cc665), C(b1c22d727665ad2), C(f996e05d)},
+    {C(bfa10785ddc1011b), C(b6e1c4d2f670f7de), C(517d95604e4fcc1f),
+     C(ca6552a0dfb82c73), C(b024cdf09e34ba07), C(66cd8c5a95d7393b),
+     C(e3939acf790d4a74), C(ca6552a0dfb82c73), C(b024cdf09e34ba07),
+     C(66cd8c5a95d7393b), C(e3939acf790d4a74), C(97827541a1ef051e),
+     C(ac2fce47ebe6500c), C(b3f06d3bddf3bd6a), C(1d74afb25e1ce5fe),
+     C(c4306af6)},
+    {C(534cc35f0ee1eb4e), C(b703820f1f3b3dce), C(884aa164cf22363),
+     C(f14ef7f47d8a57a3), C(80d1f86f2e061d7c), C(401d6c2f151b5a62),
+     C(e988460224108944), C(f14ef7f47d8a57a3), C(80d1f86f2e061d7c),
+     C(401d6c2f151b5a62), C(e988460224108944), C(7804d4135f68cd19),
+     C(5487b4b39e69fe8e), C(8cc5999015358a27), C(8f3729b61c2d5601),
+     C(6dcad433)},
+    {C(7ca6e3933995dac), C(fd118c77daa8188), C(3aceb7b5e7da6545),
+     C(c8389799445480db), C(5389f5df8aacd50d), C(d136581f22fab5f),
+     C(c2f31f85991da417), C(c8389799445480db), C(5389f5df8aacd50d),
+     C(d136581f22fab5f), C(c2f31f85991da417), C(aefbf9ff84035a43),
+     C(8accbaf44adadd7c), C(e57f3657344b67f5), C(21490e5e8abdec51),
+     C(3c07374d)},
+    {C(f0d6044f6efd7598), C(e044d6ba4369856e), C(91968e4f8c8a1a4c),
+     C(70bd1968996bffc2), C(4c613de5d8ab32ac), C(fe1f4f97206f79d8),
+     C(ac0434f2c4e213a9), C(70bd1968996bffc2), C(4c613de5d8ab32ac),
+     C(fe1f4f97206f79d8), C(ac0434f2c4e213a9), C(7490e9d82cfe22ca),
+     C(5fbbf7f987454238), C(c39e0dc8368ce949), C(22201d3894676c71),
+     C(f0f4602c)},
+    {C(3d69e52049879d61), C(76610636ea9f74fe), C(e9bf5602f89310c0),
+     C(8eeb177a86053c11), C(e390122c345f34a2), C(1e30e47afbaaf8d6),
+     C(7b892f68e5f91732), C(8eeb177a86053c11), C(e390122c345f34a2),
+     C(1e30e47afbaaf8d6), C(7b892f68e5f91732), C(b87922525fa44158),
+     C(f440a1ee1a1a766b), C(ee8efad279d08c5c), C(421f910c5b60216e),
+     C(3e1ea071)},
+    {C(79da242a16acae31), C(183c5f438e29d40), C(6d351710ae92f3de),
+     C(27233b28b5b11e9b), C(c7dfe8988a942700), C(570ed11c4abad984),
+     C(4b4c04632f48311a), C(27233b28b5b11e9b), C(c7dfe8988a942700),
+     C(570ed11c4abad984), C(4b4c04632f48311a), C(12f33235442cbf9),
+     C(a35315ca0b5b8cdb), C(d8abde62ead5506b), C(fc0fcf8478ad5266),
+     C(67580f0c)},
+    {C(461c82656a74fb57), C(d84b491b275aa0f7), C(8f262cb29a6eb8b2),
+     C(49fa3070bc7b06d0), C(f12ed446bd0c0539), C(6d43ac5d1dd4b240),
+     C(7609524fe90bec93), C(49fa3070bc7b06d0), C(f12ed446bd0c0539),
+     C(6d43ac5d1dd4b240), C(7609524fe90bec93), C(391c2b2e076ec241),
+     C(f5e62deda7839f7b), C(3c7b3186a10d870f), C(77ef4f2cba4f1005),
+     C(4e109454)},
+    {C(53c1a66d0b13003), C(731f060e6fe797fc), C(daa56811791371e3),
+     C(57466046cf6896ed), C(8ac37e0e8b25b0c6), C(3e6074b52ad3cf18),
+     C(aa491ce7b45db297), C(57466046cf6896ed), C(8ac37e0e8b25b0c6),
+     C(3e6074b52ad3cf18), C(aa491ce7b45db297), C(f7a9227c5e5e22c3),
+     C(3d92e0841e29ce28), C(2d30da5b2859e59d), C(ff37fa1c9cbfafc2),
+     C(88a474a7)},
+    {C(d3a2efec0f047e9), C(1cabce58853e58ea), C(7a17b2eae3256be4),
+     C(c2dcc9758c910171), C(cb5cddaeff4ddb40), C(5d7cc5869baefef1),
+     C(9644c5853af9cfeb), C(c2dcc9758c910171), C(cb5cddaeff4ddb40),
+     C(5d7cc5869baefef1), C(9644c5853af9cfeb), C(255c968184694ee1),
+     C(4e4d726eda360927), C(7d27dd5b6d100377), C(9a300e2020ddea2c), C(5b5bedd)},
+    {C(43c64d7484f7f9b2), C(5da002b64aafaeb7), C(b576c1e45800a716),
+     C(3ee84d3d5b4ca00b), C(5cbc6d701894c3f9), C(d9e946f5ae1ca95),
+     C(24ca06e67f0b1833), C(3ee84d3d5b4ca00b), C(5cbc6d701894c3f9),
+     C(d9e946f5ae1ca95), C(24ca06e67f0b1833), C(3413d46b4152650e),
+     C(cbdfdbc2ab516f9c), C(2aad8acb739e0c6c), C(2bfc950d9f9fa977),
+     C(1aaddfa7)},
+    {C(a7dec6ad81cf7fa1), C(180c1ab708683063), C(95e0fd7008d67cff),
+     C(6b11c5073687208), C(7e0a57de0d453f3), C(e48c267d4f646867),
+     C(2168e9136375f9cb), C(6b11c5073687208), C(7e0a57de0d453f3),
+     C(e48c267d4f646867), C(2168e9136375f9cb), C(64da194aeeea7fdf),
+     C(a3b9f01fa5885678), C(c316f8ee2eb2bd17), C(a7e4d80f83e4427f),
+     C(5be07fd8)},
+    {C(5408a1df99d4aff), C(b9565e588740f6bd), C(abf241813b08006e),
+     C(7da9e81d89fda7ad), C(274157cabe71440d), C(2c22d9a480b331f7),
+     C(e835c8ac746472d5), C(7da9e81d89fda7ad), C(274157cabe71440d),
+     C(2c22d9a480b331f7), C(e835c8ac746472d5), C(2038ce817a201ae4),
+     C(46f3289dfe1c5e40), C(435578a42d4b7c56), C(f96d9f409fcf561), C(cbca8606)},
+    {C(a8b27a6bcaeeed4b), C(aec1eeded6a87e39), C(9daf246d6fed8326),
+     C(d45a938b79f54e8f), C(366b219d6d133e48), C(5b14be3c25c49405),
+     C(fdd791d48811a572), C(d45a938b79f54e8f), C(366b219d6d133e48),
+     C(5b14be3c25c49405), C(fdd791d48811a572), C(3de67b8d9e95d335),
+     C(903c01307cfbeed5), C(af7d65f32274f1d1), C(4dba141b5fc03c42),
+     C(bde64d01)},
+    {C(9a952a8246fdc269), C(d0dcfcac74ef278c), C(250f7139836f0f1f),
+     C(c83d3c5f4e5f0320), C(694e7adeb2bf32e5), C(7ad09538a3da27f5),
+     C(2b5c18f934aa5303), C(c83d3c5f4e5f0320), C(694e7adeb2bf32e5),
+     C(7ad09538a3da27f5), C(2b5c18f934aa5303), C(c4dad7703d34326e),
+     C(825569e2bcdc6a25), C(b83d267709ca900d), C(44ed05151f5d74e6),
+     C(ee90cf33)},
+    {C(c930841d1d88684f), C(5eb66eb18b7f9672), C(e455d413008a2546),
+     C(bc271bc0df14d647), C(b071100a9ff2edbb), C(2b1a4c1cc31a119a),
+     C(b5d7caa1bd946cef), C(bc271bc0df14d647), C(b071100a9ff2edbb),
+     C(2b1a4c1cc31a119a), C(b5d7caa1bd946cef), C(e02623ae10f4aadd),
+     C(d79f600389cd06fd), C(1e8da7965303e62b), C(86f50e10eeab0925),
+     C(4305c3ce)},
+    {C(94dc6971e3cf071a), C(994c7003b73b2b34), C(ea16e85978694e5),
+     C(336c1b59a1fc19f6), C(c173acaecc471305), C(db1267d24f3f3f36),
+     C(e9a5ee98627a6e78), C(336c1b59a1fc19f6), C(c173acaecc471305),
+     C(db1267d24f3f3f36), C(e9a5ee98627a6e78), C(718f334204305ae5),
+     C(e3b53c148f98d22c), C(a184012df848926), C(6e96386127d51183), C(4b3a1d76)},
+    {C(7fc98006e25cac9), C(77fee0484cda86a7), C(376ec3d447060456),
+     C(84064a6dcf916340), C(fbf55a26790e0ebb), C(2e7f84151c31a5c2),
+     C(9f7f6d76b950f9bf), C(84064a6dcf916340), C(fbf55a26790e0ebb),
+     C(2e7f84151c31a5c2), C(9f7f6d76b950f9bf), C(125e094fbee2b146),
+     C(5706aa72b2eef7c2), C(1c4a2daa905ee66e), C(83d48029b5451694),
+     C(a8bb6d80)},
+    {C(bd781c4454103f6), C(612197322f49c931), C(b9cf17fd7e5462d5),
+     C(e38e526cd3324364), C(85f2b63a5b5e840a), C(485d7cef5aaadd87),
+     C(d2b837a462f6db6d), C(e38e526cd3324364), C(85f2b63a5b5e840a),
+     C(485d7cef5aaadd87), C(d2b837a462f6db6d), C(3e41cef031520d9a),
+     C(82df73902d7f67e), C(3ba6fd54c15257cb), C(22f91f079be42d40), C(1f9fa607)},
+    {C(da60e6b14479f9df), C(3bdccf69ece16792), C(18ebf45c4fecfdc9),
+     C(16818ee9d38c6664), C(5519fa9a1e35a329), C(cbd0001e4b08ed8),
+     C(41a965e37a0c731b), C(16818ee9d38c6664), C(5519fa9a1e35a329),
+     C(cbd0001e4b08ed8), C(41a965e37a0c731b), C(66e7b5dcca1ca28f),
+     C(963b2d993614347d), C(9b6fc6f41d411106), C(aaaecaccf7848c0c),
+     C(8d0e4ed2)},
+    {C(4ca56a348b6c4d3), C(60618537c3872514), C(2fbb9f0e65871b09),
+     C(30278016830ddd43), C(f046646d9012e074), C(c62a5804f6e7c9da),
+     C(98d51f5830e2bc1e), C(30278016830ddd43), C(f046646d9012e074),
+     C(c62a5804f6e7c9da), C(98d51f5830e2bc1e), C(7b2cbe5d37e3f29e),
+     C(7b8c3ed50bda4aa0), C(3ea60cc24639e038), C(f7706de9fb0b5801),
+     C(1bf31347)},
+    {C(ebd22d4b70946401), C(6863602bf7139017), C(c0b1ac4e11b00666),
+     C(7d2782b82bd494b6), C(97159ba1c26b304b), C(42b3b0fd431b2ac2),
+     C(faa81f82691c830c), C(7d2782b82bd494b6), C(97159ba1c26b304b),
+     C(42b3b0fd431b2ac2), C(faa81f82691c830c), C(7cc6449234c7e185),
+     C(aeaa6fa643ca86a5), C(1412db1c0f2e0133), C(4df2fe3e4072934f),
+     C(1ae3fc5b)},
+    {C(3cc4693d6cbcb0c), C(501689ea1c70ffa), C(10a4353e9c89e364),
+     C(58c8aba7475e2d95), C(3e2f291698c9427a), C(e8710d19c9de9e41),
+     C(65dda22eb04cf953), C(58c8aba7475e2d95), C(3e2f291698c9427a),
+     C(e8710d19c9de9e41), C(65dda22eb04cf953), C(d7729c48c250cffa),
+     C(ef76162b2ddfba4b), C(52371e17f4d51f6d), C(ddd002112ff0c833),
+     C(459c3930)},
+    {C(38908e43f7ba5ef0), C(1ab035d4e7781e76), C(41d133e8c0a68ff7),
+     C(d1090893afaab8bc), C(96c4fe6922772807), C(4522426c2b4205eb),
+     C(efad99a1262e7e0d), C(d1090893afaab8bc), C(96c4fe6922772807),
+     C(4522426c2b4205eb), C(efad99a1262e7e0d), C(c7696029abdb465e),
+     C(4e18eaf03d517651), C(d006bced54c86ac8), C(4330326d1021860c),
+     C(e00c4184)},
+    {C(34983ccc6aa40205), C(21802cad34e72bc4), C(1943e8fb3c17bb8),
+     C(fc947167f69c0da5), C(ae79cfdb91b6f6c1), C(7b251d04c26cbda3),
+     C(128a33a79060d25e), C(fc947167f69c0da5), C(ae79cfdb91b6f6c1),
+     C(7b251d04c26cbda3), C(128a33a79060d25e), C(1eca842dbfe018dd),
+     C(50a4cd2ee0ba9c63), C(c2f5c97d8399682f), C(3f929fc7cbe8ecbb),
+     C(ffc7a781)},
+    {C(86215c45dcac9905), C(ea546afe851cae4b), C(d85b6457e489e374),
+     C(b7609c8e70386d66), C(36e6ccc278d1636d), C(2f873307c08e6a1c),
+     C(10f252a758505289), C(b7609c8e70386d66), C(36e6ccc278d1636d),
+     C(2f873307c08e6a1c), C(10f252a758505289), C(c8977646e81ab4b6),
+     C(8017b745cd80213b), C(960687db359bea0), C(ef4a470660799488), C(6a125480)},
+    {C(420fc255c38db175), C(d503cd0f3c1208d1), C(d4684e74c825a0bc),
+     C(4c10537443152f3d), C(720451d3c895e25d), C(aff60c4d11f513fd),
+     C(881e8d6d2d5fb953), C(4c10537443152f3d), C(720451d3c895e25d),
+     C(aff60c4d11f513fd), C(881e8d6d2d5fb953), C(9dec034a043f1f55),
+     C(e27a0c22e7bfb39d), C(2220b959128324), C(53240272152dbd8b), C(88a1512b)},
+    {C(1d7a31f5bc8fe2f9), C(4763991092dcf836), C(ed695f55b97416f4),
+     C(f265edb0c1c411d7), C(30e1e9ec5262b7e6), C(c2c3ba061ce7957a),
+     C(d975f93b89a16409), C(f265edb0c1c411d7), C(30e1e9ec5262b7e6),
+     C(c2c3ba061ce7957a), C(d975f93b89a16409), C(e9d703123f43450a),
+     C(41383fedfed67c82), C(6e9f43ecbbbd6004), C(c7ccd23a24e77b8), C(549bbbe5)},
+    {C(94129a84c376a26e), C(c245e859dc231933), C(1b8f74fecf917453),
+     C(e9369d2e9007e74b), C(b1375915d1136052), C(926c2021fe1d2351),
+     C(1d943addaaa2e7e6), C(e9369d2e9007e74b), C(b1375915d1136052),
+     C(926c2021fe1d2351), C(1d943addaaa2e7e6), C(f5f515869c246738),
+     C(7e309cd0e1c0f2a0), C(153c3c36cf523e3b), C(4931c66872ea6758),
+     C(c133d38c)},
+    {C(1d3a9809dab05c8d), C(adddeb4f71c93e8), C(ef342eb36631edb),
+     C(301d7a61c4b3dbca), C(861336c3f0552d61), C(12c6db947471300f),
+     C(a679ef0ed761deb9), C(301d7a61c4b3dbca), C(861336c3f0552d61),
+     C(12c6db947471300f), C(a679ef0ed761deb9), C(5f713b720efcd147),
+     C(37ac330a333aa6b), C(3309dc9ec1616eef), C(52301d7a908026b5), C(fcace348)},
+    {C(90fa3ccbd60848da), C(dfa6e0595b569e11), C(e585d067a1f5135d),
+     C(6cef866ec295abea), C(c486c0d9214beb2d), C(d6e490944d5fe100),
+     C(59df3175d72c9f38), C(6cef866ec295abea), C(c486c0d9214beb2d),
+     C(d6e490944d5fe100), C(59df3175d72c9f38), C(3f23aeb4c04d1443),
+     C(9bf0515cd8d24770), C(958554f60ccaade2), C(5182863c90132fe8),
+     C(ed7b6f9a)},
+    {C(2dbb4fc71b554514), C(9650e04b86be0f82), C(60f2304fba9274d3),
+     C(fcfb9443e997cab), C(f13310d96dec2772), C(709cad2045251af2),
+     C(afd0d30cc6376dad), C(fcfb9443e997cab), C(f13310d96dec2772),
+     C(709cad2045251af2), C(afd0d30cc6376dad), C(59d4bed30d550d0d),
+     C(58006d4e22d8aad1), C(eee12d2362d1f13b), C(35cf1d7faaf1d228),
+     C(6d907dda)},
+    {C(b98bf4274d18374a), C(1b669fd4c7f9a19a), C(b1f5972b88ba2b7a),
+     C(73119c99e6d508be), C(5d4036a187735385), C(8fa66e192fd83831),
+     C(2abf64b6b592ed57), C(73119c99e6d508be), C(5d4036a187735385),
+     C(8fa66e192fd83831), C(2abf64b6b592ed57), C(d4501f95dd84b08c),
+     C(bf1552439c8bea02), C(4f56fe753ba7e0ba), C(4ca8d35cc058cfcd),
+     C(7a4d48d5)},
+    {C(d6781d0b5e18eb68), C(b992913cae09b533), C(58f6021caaee3a40),
+     C(aafcb77497b5a20b), C(411819e5e79b77a3), C(bd779579c51c77ce),
+     C(58d11f5dcf5d075d), C(aafcb77497b5a20b), C(411819e5e79b77a3),
+     C(bd779579c51c77ce), C(58d11f5dcf5d075d), C(9eae76cde1cb4233),
+     C(32fe25a9bf657970), C(1c0c807948edb06a), C(b8f29a3dfaee254d),
+     C(e686f3db)},
+    {C(226651cf18f4884c), C(595052a874f0f51c), C(c9b75162b23bab42),
+     C(3f44f873be4812ec), C(427662c1dbfaa7b2), C(a207ff9638fb6558),
+     C(a738d919e45f550f), C(3f44f873be4812ec), C(427662c1dbfaa7b2),
+     C(a207ff9638fb6558), C(a738d919e45f550f), C(cb186ea05717e7d6),
+     C(1ca7d68a5871fdc1), C(5d4c119ea8ef3750), C(72b6a10fa2ff9406), C(cce7c55)},
+    {C(a734fb047d3162d6), C(e523170d240ba3a5), C(125a6972809730e8),
+     C(d396a297799c24a1), C(8fee992e3069bad5), C(2e3a01b0697ccf57),
+     C(ee9c7390bd901cfa), C(d396a297799c24a1), C(8fee992e3069bad5),
+     C(2e3a01b0697ccf57), C(ee9c7390bd901cfa), C(56f2d9da0af28af2),
+     C(3fdd37b2fe8437cb), C(3d13eeeb60d6aec0), C(2432ae62e800a5ce), C(f58b96b)},
+    {C(c6df6364a24f75a3), C(c294e2c84c4f5df8), C(a88df65c6a89313b),
+     C(895fe8443183da74), C(c7f2f6f895a67334), C(a0d6b6a506691d31),
+     C(24f51712b459a9f0), C(895fe8443183da74), C(c7f2f6f895a67334),
+     C(a0d6b6a506691d31), C(24f51712b459a9f0), C(173a699481b9e088),
+     C(1dee9b77bcbf45d3), C(32b98a646a8667d0), C(3adcd4ee28f42a0e),
+     C(1bbf6f60)},
+    {C(d8d1364c1fbcd10), C(2d7cc7f54832deaa), C(4e22c876a7c57625),
+     C(a3d5d1137d30c4bd), C(1e7d706a49bdfb9e), C(c63282b20ad86db2),
+     C(aec97fa07916bfd6), C(a3d5d1137d30c4bd), C(1e7d706a49bdfb9e),
+     C(c63282b20ad86db2), C(aec97fa07916bfd6), C(7c9ba3e52d44f73e),
+     C(af62fd245811185d), C(8a9d2dacd8737652), C(bd2cce277d5fbec0),
+     C(ce5e0cc2)},
+    {C(aae06f9146db885f), C(3598736441e280d9), C(fba339b117083e55),
+     C(b22bf08d9f8aecf7), C(c182730de337b922), C(2b9adc87a0450a46),
+     C(192c29a9cfc00aad), C(b22bf08d9f8aecf7), C(c182730de337b922),
+     C(2b9adc87a0450a46), C(192c29a9cfc00aad), C(9fd733f1d84a59d9),
+     C(d86bd5c9839ace15), C(af20b57303172876), C(9f63cb7161b5364c),
+     C(584cfd6f)},
+    {C(8955ef07631e3bcc), C(7d70965ea3926f83), C(39aed4134f8b2db6),
+     C(882efc2561715a9c), C(ef8132a18a540221), C(b20a3c87a8c257c1),
+     C(f541b8628fad6c23), C(882efc2561715a9c), C(ef8132a18a540221),
+     C(b20a3c87a8c257c1), C(f541b8628fad6c23), C(9552aed57a6e0467),
+     C(4d9fdd56867611a7), C(c330279bf23b9eab), C(44dbbaea2fcb8eba),
+     C(8f9bbc33)},
+    {C(ad611c609cfbe412), C(d3c00b18bf253877), C(90b2172e1f3d0bfd),
+     C(371a98b2cb084883), C(33a2886ee9f00663), C(be9568818ed6e6bd),
+     C(f244a0fa2673469a), C(371a98b2cb084883), C(33a2886ee9f00663),
+     C(be9568818ed6e6bd), C(f244a0fa2673469a), C(b447050bd3e559e9),
+     C(d3b695dae7a13383), C(ded0bb65be471188), C(ca3c7a2b78922cae),
+     C(d7640d95)},
+    {C(d5339adc295d5d69), C(b633cc1dcb8b586a), C(ee84184cf5b1aeaf),
+     C(89f3aab99afbd636), C(f420e004f8148b9a), C(6818073faa797c7c),
+     C(dd3b4e21cbbf42ca), C(89f3aab99afbd636), C(f420e004f8148b9a),
+     C(6818073faa797c7c), C(dd3b4e21cbbf42ca), C(6a2b7db261164844),
+     C(cbead63d1895852a), C(93d37e1eae05e2f9), C(5d06db2703fbc3ae), C(3d12a2b)},
+    {C(40d0aeff521375a8), C(77ba1ad7ecebd506), C(547c6f1a7d9df427),
+     C(21c2be098327f49b), C(7e035065ac7bbef5), C(6d7348e63023fb35),
+     C(9d427dc1b67c3830), C(21c2be098327f49b), C(7e035065ac7bbef5),
+     C(6d7348e63023fb35), C(9d427dc1b67c3830), C(4e3d018a43858341),
+     C(cf924bb44d6b43c5), C(4618b6a26e3446ae), C(54d3013fac3ed469),
+     C(aaeafed0)},
+    {C(8b2d54ae1a3df769), C(11e7adaee3216679), C(3483781efc563e03),
+     C(9d097dd3152ab107), C(51e21d24126e8563), C(cba56cac884a1354),
+     C(39abb1b595f0a977), C(9d097dd3152ab107), C(51e21d24126e8563),
+     C(cba56cac884a1354), C(39abb1b595f0a977), C(81e6dd1c1109848f),
+     C(1644b209826d7b15), C(6ac67e4e4b4812f0), C(b3a9f5622c935bf7),
+     C(95b9b814)},
+    {C(99c175819b4eae28), C(932e8ff9f7a40043), C(ec78dcab07ca9f7c),
+     C(c1a78b82ba815b74), C(458cbdfc82eb322a), C(17f4a192376ed8d7),
+     C(6f9e92968bc8ccef), C(c1a78b82ba815b74), C(458cbdfc82eb322a),
+     C(17f4a192376ed8d7), C(6f9e92968bc8ccef), C(93e098c333b39905),
+     C(d59b1cace44b7fdc), C(f7a64ed78c64c7c5), C(7c6eca5dd87ec1ce),
+     C(45fbe66e)},
+    {C(2a418335779b82fc), C(af0295987849a76b), C(c12bc5ff0213f46e),
+     C(5aeead8d6cb25bb9), C(739315f7743ec3ff), C(9ab48d27111d2dcc),
+     C(5b87bd35a975929b), C(5aeead8d6cb25bb9), C(739315f7743ec3ff),
+     C(9ab48d27111d2dcc), C(5b87bd35a975929b), C(c3dd8d6d95a46bb3),
+     C(7bf9093215a4f483), C(cb557d6ed84285bd), C(daf58422f261fdb5),
+     C(b4baa7a8)},
+    {C(3b1fc6a3d279e67d), C(70ea1e49c226396), C(25505adcf104697c),
+     C(ba1ffba29f0367aa), C(a20bec1dd15a8b6c), C(e9bf61d2dab0f774),
+     C(f4f35bf5870a049c), C(ba1ffba29f0367aa), C(a20bec1dd15a8b6c),
+     C(e9bf61d2dab0f774), C(f4f35bf5870a049c), C(26787efa5b92385),
+     C(3d9533590ce30b59), C(a4da3e40530a01d4), C(6395deaefb70067c),
+     C(83e962fe)},
+    {C(d97eacdf10f1c3c9), C(b54f4654043a36e0), C(b128f6eb09d1234),
+     C(d8ad7ec84a9c9aa2), C(e256cffed11f69e6), C(2cf65e4958ad5bda),
+     C(cfbf9b03245989a7), C(d8ad7ec84a9c9aa2), C(e256cffed11f69e6),
+     C(2cf65e4958ad5bda), C(cfbf9b03245989a7), C(9fa51e6686cf4444),
+     C(9425c117a34609d5), C(b25f7e2c6f30e96), C(ea5477c3f2b5afd1), C(aac3531c)},
+    {C(293a5c1c4e203cd4), C(6b3329f1c130cefe), C(f2e32f8ec76aac91),
+     C(361e0a62c8187bff), C(6089971bb84d7133), C(93df7741588dd50b),
+     C(c2a9b6abcd1d80b1), C(361e0a62c8187bff), C(6089971bb84d7133),
+     C(93df7741588dd50b), C(c2a9b6abcd1d80b1), C(4d2f86869d79bc59),
+     C(85cd24d8aa570ff), C(b0dcf6ef0e94bbb5), C(2037c69aa7a78421), C(2b1db7cc)},
+    {C(4290e018ffaedde7), C(a14948545418eb5e), C(72d851b202284636),
+     C(4ec02f3d2f2b23f2), C(ab3580708aa7c339), C(cdce066fbab3f65),
+     C(d8ed3ecf3c7647b9), C(4ec02f3d2f2b23f2), C(ab3580708aa7c339),
+     C(cdce066fbab3f65), C(d8ed3ecf3c7647b9), C(6d2204b3e31f344a),
+     C(61a4d87f80ee61d7), C(446c43dbed4b728f), C(73130ac94f58747e),
+     C(cf00cd31)},
+    {C(f919a59cbde8bf2f), C(a56d04203b2dc5a5), C(38b06753ac871e48),
+     C(c2c9fc637dbdfcfa), C(292ab8306d149d75), C(7f436b874b9ffc07),
+     C(a5b56b0129218b80), C(c2c9fc637dbdfcfa), C(292ab8306d149d75),
+     C(7f436b874b9ffc07), C(a5b56b0129218b80), C(9188f7bdc47ec050),
+     C(cfe9345d03a15ade), C(40b520fb2750c49e), C(c2e83d343968af2e),
+     C(7d3c43b8)},
+    {C(1d70a3f5521d7fa4), C(fb97b3fdc5891965), C(299d49bbbe3535af),
+     C(e1a8286a7d67946e), C(52bd956f047b298), C(cbd74332dd4204ac),
+     C(12b5be7752721976), C(e1a8286a7d67946e), C(52bd956f047b298),
+     C(cbd74332dd4204ac), C(12b5be7752721976), C(278426e27f6204b6),
+     C(932ca7a7cd610181), C(41647321f0a5914d), C(48f4aa61a0ae80db),
+     C(cbd5fac6)},
+    {C(6af98d7b656d0d7c), C(d2e99ae96d6b5c0c), C(f63bd1603ef80627),
+     C(bde51033ac0413f8), C(bc0272f691aec629), C(6204332651bebc44),
+     C(1cbf00de026ea9bd), C(bde51033ac0413f8), C(bc0272f691aec629),
+     C(6204332651bebc44), C(1cbf00de026ea9bd), C(b9c7ed6a75f3ff1e),
+     C(7e310b76a5808e4f), C(acbbd1aad5531885), C(fc245f2473adeb9c),
+     C(76d0fec4)},
+    {C(395b7a8adb96ab75), C(582df7165b20f4a), C(e52bd30e9ff657f9),
+     C(6c71064996cbec8b), C(352c535edeefcb89), C(ac7f0aba15cd5ecd),
+     C(3aba1ca8353e5c60), C(6c71064996cbec8b), C(352c535edeefcb89),
+     C(ac7f0aba15cd5ecd), C(3aba1ca8353e5c60), C(5c30a288a80ce646),
+     C(c2940488b6617674), C(925f8cc66b370575), C(aa65d1283b9bb0ef),
+     C(405e3402)},
+    {C(3822dd82c7df012f), C(b9029b40bd9f122b), C(fd25b988468266c4),
+     C(43e47bd5bab1e0ef), C(4a71f363421f282f), C(880b2f32a2b4e289),
+     C(1299d4eda9d3eadf), C(43e47bd5bab1e0ef), C(4a71f363421f282f),
+     C(880b2f32a2b4e289), C(1299d4eda9d3eadf), C(d713a40226f5564),
+     C(4d8d34fedc769406), C(a85001b29cd9cac3), C(cae92352a41fd2b0),
+     C(c732c481)},
+    {C(79f7efe4a80b951a), C(dd3a3fddfc6c9c41), C(ab4c812f9e27aa40),
+     C(832954ec9d0de333), C(94c390aa9bcb6b8a), C(f3b32afdc1f04f82),
+     C(d229c3b72e4b9a74), C(832954ec9d0de333), C(94c390aa9bcb6b8a),
+     C(f3b32afdc1f04f82), C(d229c3b72e4b9a74), C(1d11860d7ed624a6),
+     C(cadee20b3441b984), C(75307079bf306f7b), C(87902aa3b9753ba4),
+     C(a8d123c9)},
+    {C(ae6e59f5f055921a), C(e9d9b7bf68e82), C(5ce4e4a5b269cc59),
+     C(4960111789727567), C(149b8a37c7125ab6), C(78c7a13ab9749382),
+     C(1c61131260ca151a), C(4960111789727567), C(149b8a37c7125ab6),
+     C(78c7a13ab9749382), C(1c61131260ca151a), C(1e93276b35c309a0),
+     C(2618f56230acde58), C(af61130a18e4febf), C(7145deb18e89befe),
+     C(1e80ad7d)},
+    {C(8959dbbf07387d36), C(b4658afce48ea35d), C(8f3f82437d8cb8d6),
+     C(6566d74954986ba5), C(99d5235cc82519a7), C(257a23805c2d825),
+     C(ad75ccb968e93403), C(6566d74954986ba5), C(99d5235cc82519a7),
+     C(257a23805c2d825), C(ad75ccb968e93403), C(b45bd4cf78e11f7f),
+     C(80c5536bdc487983), C(a4fd76ecbf018c8a), C(3b9dac78a7a70d43),
+     C(52aeb863)},
+    {C(4739613234278a49), C(99ea5bcd340bf663), C(258640912e712b12),
+     C(c8a2827404991402), C(7ee5e78550f02675), C(2ec53952db5ac662),
+     C(1526405a9df6794b), C(c8a2827404991402), C(7ee5e78550f02675),
+     C(2ec53952db5ac662), C(1526405a9df6794b), C(eddc6271170c5e1f),
+     C(f5a85f986001d9d6), C(95427c677bf58d58), C(53ed666dfa85cb29),
+     C(ef7c0c18)},
+    {C(420e6c926bc54841), C(96dbbf6f4e7c75cd), C(d8d40fa70c3c67bb),
+     C(3edbc10e4bfee91b), C(f0d681304c28ef68), C(77ea602029aaaf9c),
+     C(90f070bd24c8483c), C(3edbc10e4bfee91b), C(f0d681304c28ef68),
+     C(77ea602029aaaf9c), C(90f070bd24c8483c), C(28bc8e41e08ceb86),
+     C(1eb56e48a65691ef), C(9fea5301c9202f0e), C(3fcb65091aa9f135),
+     C(b6ad4b68)},
+    {C(c8601bab561bc1b7), C(72b26272a0ff869a), C(56fdfc986d6bc3c4),
+     C(83707730cad725d4), C(c9ca88c3a779674a), C(e1c696fbbd9aa933),
+     C(723f3baab1c17a45), C(83707730cad725d4), C(c9ca88c3a779674a),
+     C(e1c696fbbd9aa933), C(723f3baab1c17a45), C(f82abc7a1d851682),
+     C(30683836818e857d), C(78bfa3e89a5ab23f), C(6928234482b31817),
+     C(c1e46b17)},
+    {C(b2d294931a0e20eb), C(284ffd9a0815bc38), C(1f8a103aac9bbe6),
+     C(1ef8e98e1ea57269), C(5971116272f45a8b), C(187ad68ce95d8eac),
+     C(e94e93ee4e8ecaa6), C(1ef8e98e1ea57269), C(5971116272f45a8b),
+     C(187ad68ce95d8eac), C(e94e93ee4e8ecaa6), C(a0ff2a58611838b5),
+     C(b01e03849bfbae6f), C(d081e202e28ea3ab), C(51836bcee762bf13),
+     C(57b8df25)},
+    {C(7966f53c37b6c6d7), C(8e6abcfb3aa2b88f), C(7f2e5e0724e5f345),
+     C(3eeb60c3f5f8143d), C(a25aec05c422a24f), C(b026b03ad3cca4db),
+     C(e6e030028cc02a02), C(3eeb60c3f5f8143d), C(a25aec05c422a24f),
+     C(b026b03ad3cca4db), C(e6e030028cc02a02), C(16fe679338b34bfc),
+     C(c1be385b5c8a9de4), C(65af5df6567530eb), C(ed3b303df4dc6335),
+     C(e9fa36d6)},
+    {C(be9bb0abd03b7368), C(13bca93a3031be55), C(e864f4f52b55b472),
+     C(36a8d13a2cbb0939), C(254ac73907413230), C(73520d1522315a70),
+     C(8c9fdb5cf1e1a507), C(36a8d13a2cbb0939), C(254ac73907413230),
+     C(73520d1522315a70), C(8c9fdb5cf1e1a507), C(b3640570b926886),
+     C(fba2344ee87f7bab), C(de57341ab448df05), C(385612ee094fa977),
+     C(8f8daefc)},
+    {C(a08d128c5f1649be), C(a8166c3dbbe19aad), C(cb9f914f829ec62c),
+     C(5b2b7ca856fad1c3), C(8093022d682e375d), C(ea5d163ba7ea231f),
+     C(d6181d012c0de641), C(5b2b7ca856fad1c3), C(8093022d682e375d),
+     C(ea5d163ba7ea231f), C(d6181d012c0de641), C(e7d40d0ab8b08159),
+     C(2e82320f51b3a67e), C(27c2e356ea0b63a3), C(58842d01a2b1d077), C(6e1bb7e)},
+    {C(7c386f0ffe0465ac), C(530419c9d843dbf3), C(7450e3a4f72b8d8c),
+     C(48b218e3b721810d), C(d3757ac8609bc7fc), C(111ba02a88aefc8),
+     C(e86343137d3bfc2a), C(48b218e3b721810d), C(d3757ac8609bc7fc),
+     C(111ba02a88aefc8), C(e86343137d3bfc2a), C(44ad26b51661b507),
+     C(db1268670274f51e), C(62a5e75beae875f3), C(e266e7a44c5f28c6),
+     C(fd0076f0)},
+    {C(bb362094e7ef4f8), C(ff3c2a48966f9725), C(55152803acd4a7fe),
+     C(15747d8c505ffd00), C(438a15f391312cd6), C(e46ca62c26d821f5),
+     C(be78d74c9f79cb44), C(15747d8c505ffd00), C(438a15f391312cd6),
+     C(e46ca62c26d821f5), C(be78d74c9f79cb44), C(a8aa19f3aa59f09a),
+     C(effb3cddab2c9267), C(d78e41ad97cb16a5), C(ace6821513527d32),
+     C(899b17b6)},
+    {C(cd80dea24321eea4), C(52b4fdc8130c2b15), C(f3ea100b154bfb82),
+     C(d9ccef1d4be46988), C(5ede0c4e383a5e66), C(da69683716a54d1e),
+     C(bfc3fdf02d242d24), C(d9ccef1d4be46988), C(5ede0c4e383a5e66),
+     C(da69683716a54d1e), C(bfc3fdf02d242d24), C(20ed30274651b3f5),
+     C(4c659824169e86c6), C(637226dae5b52a0e), C(7e050dbd1c71dc7f),
+     C(e3e84e31)},
+    {C(d599a04125372c3a), C(313136c56a56f363), C(1e993c3677625832),
+     C(2870a99c76a587a4), C(99f74cc0b182dda4), C(8a5e895b2f0ca7b6),
+     C(3d78882d5e0bb1dc), C(2870a99c76a587a4), C(99f74cc0b182dda4),
+     C(8a5e895b2f0ca7b6), C(3d78882d5e0bb1dc), C(f466123732a3e25e),
+     C(aca5e59716a40e50), C(261d2e7383d0e686), C(ce9362d6a42c15a7),
+     C(eef79b6b)},
+    {C(dbbf541e9dfda0a), C(1479fceb6db4f844), C(31ab576b59062534),
+     C(a3335c417687cf3a), C(92ff114ac45cda75), C(c3b8a627384f13b5),
+     C(c4f25de33de8b3f7), C(a3335c417687cf3a), C(92ff114ac45cda75),
+     C(c3b8a627384f13b5), C(c4f25de33de8b3f7), C(eacbf520578c5964),
+     C(4cb19c5ab24f3215), C(e7d8a6f67f0c6e7), C(325c2413eb770ada), C(868e3315)},
+    {C(c2ee3288be4fe2bf), C(c65d2f5ddf32b92), C(af6ecdf121ba5485),
+     C(c7cd48f7abf1fe59), C(ce600656ace6f53a), C(8a94a4381b108b34),
+     C(f9d1276c64bf59fb), C(c7cd48f7abf1fe59), C(ce600656ace6f53a),
+     C(8a94a4381b108b34), C(f9d1276c64bf59fb), C(219ce70ff5a112a5),
+     C(e6026c576e2d28d7), C(b8e467f25015e3a6), C(950cb904f37af710),
+     C(4639a426)},
+    {C(d86603ced1ed4730), C(f9de718aaada7709), C(db8b9755194c6535),
+     C(d803e1eead47604c), C(ad00f7611970a71b), C(bc50036b16ce71f5),
+     C(afba96210a2ca7d6), C(d803e1eead47604c), C(ad00f7611970a71b),
+     C(bc50036b16ce71f5), C(afba96210a2ca7d6), C(28f7a7be1d6765f0),
+     C(97bd888b93938c68), C(6ad41d1b407ded49), C(b9bfec098dc543e4),
+     C(f3213646)},
+    {C(915263c671b28809), C(a815378e7ad762fd), C(abec6dc9b669f559),
+     C(d17c928c5342477f), C(745130b795254ad5), C(8c5db926fe88f8ba),
+     C(742a95c953e6d974), C(d17c928c5342477f), C(745130b795254ad5),
+     C(8c5db926fe88f8ba), C(742a95c953e6d974), C(279db8057b5d3e96),
+     C(98168411565b4ec4), C(50a72c54fa1125fa), C(27766a635db73638),
+     C(17f148e9)},
+    {C(2b67cdd38c307a5e), C(cb1d45bb5c9fe1c), C(800baf2a02ec18ad),
+     C(6531c1fe32bcb417), C(8c970d8df8cdbeb4), C(917ba5fc67e72b40),
+     C(4b65e4e263e0a426), C(6531c1fe32bcb417), C(8c970d8df8cdbeb4),
+     C(917ba5fc67e72b40), C(4b65e4e263e0a426), C(e0de33ce88a8b3a9),
+     C(f8ef98a437e16b08), C(a5162c0c7c5f7b62), C(dbdac43361b2b881),
+     C(bfd94880)},
+    {C(2d107419073b9cd0), C(a96db0740cef8f54), C(ec41ee91b3ecdc1b),
+     C(ffe319654c8e7ebc), C(6a67b8f13ead5a72), C(6dd10a34f80d532f),
+     C(6e9cfaece9fbca4), C(ffe319654c8e7ebc), C(6a67b8f13ead5a72),
+     C(6dd10a34f80d532f), C(6e9cfaece9fbca4), C(b4468eb6a30aa7e9),
+     C(e87995bee483222a), C(d036c2c90c609391), C(853306e82fa32247),
+     C(bb1fa7f3)},
+    {C(f3e9487ec0e26dfc), C(1ab1f63224e837fa), C(119983bb5a8125d8),
+     C(8950cfcf4bdf622c), C(8847dca82efeef2f), C(646b75b026708169),
+     C(21cab4b1687bd8b), C(8950cfcf4bdf622c), C(8847dca82efeef2f),
+     C(646b75b026708169), C(21cab4b1687bd8b), C(243b489a9eae6231),
+     C(5f3e634c4b779876), C(ff8abd1548eaf646), C(c7962f5f0151914b), C(88816b1)},
+    {C(1160987c8fe86f7d), C(879e6db1481eb91b), C(d7dcb802bfe6885d),
+     C(14453b5cc3d82396), C(4ef700c33ed278bc), C(1639c72ffc00d12e),
+     C(fb140ee6155f700d), C(14453b5cc3d82396), C(4ef700c33ed278bc),
+     C(1639c72ffc00d12e), C(fb140ee6155f700d), C(2e6b5c96a6620862),
+     C(a1f136998cbe19c), C(74e058a3b6c5a712), C(93dcf6bd33928b17), C(5c2faeb3)},
+    {C(eab8112c560b967b), C(97f550b58e89dbae), C(846ed506d304051f),
+     C(276aa37744b5a028), C(8c10800ee90ea573), C(e6e57d2b33a1e0b7),
+     C(91f83563cd3b9dda), C(276aa37744b5a028), C(8c10800ee90ea573),
+     C(e6e57d2b33a1e0b7), C(91f83563cd3b9dda), C(afbb4739570738a1),
+     C(440ba98da5d8f69), C(fde4e9b0eda20350), C(e67dfa5a2138fa1), C(51b5fc6f)},
+    {C(1addcf0386d35351), C(b5f436561f8f1484), C(85d38e22181c9bb1),
+     C(ff5c03f003c1fefe), C(e1098670afe7ff6), C(ea445030cf86de19),
+     C(f155c68b5c2967f8), C(ff5c03f003c1fefe), C(e1098670afe7ff6),
+     C(ea445030cf86de19), C(f155c68b5c2967f8), C(95d31b145dbb2e9e),
+     C(914fe1ca3deb3265), C(6066020b1358ccc1), C(c74bb7e2dee15036),
+     C(33d94752)},
+    {C(d445ba84bf803e09), C(1216c2497038f804), C(2293216ea2237207),
+     C(e2164451c651adfb), C(b2534e65477f9823), C(4d70691a69671e34),
+     C(15be4963dbde8143), C(e2164451c651adfb), C(b2534e65477f9823),
+     C(4d70691a69671e34), C(15be4963dbde8143), C(762e75c406c5e9a3),
+     C(7b7579f7e0356841), C(480533eb066dfce5), C(90ae14ea6bfeb4ae),
+     C(b0c92948)},
+    {C(37235a096a8be435), C(d9b73130493589c2), C(3b1024f59378d3be),
+     C(ad159f542d81f04e), C(49626a97a946096), C(d8d3998bf09fd304),
+     C(d127a411eae69459), C(ad159f542d81f04e), C(49626a97a946096),
+     C(d8d3998bf09fd304), C(d127a411eae69459), C(8f3253c4eb785a7b),
+     C(4049062f37e62397), C(b9fa04d3b670e5c1), C(1211a7967ac9350f),
+     C(c7171590)},
+    {C(763ad6ea2fe1c99d), C(cf7af5368ac1e26b), C(4d5e451b3bb8d3d4),
+     C(3712eb913d04e2f2), C(2f9500d319c84d89), C(4ac6eb21a8cf06f9),
+     C(7d1917afcde42744), C(3712eb913d04e2f2), C(2f9500d319c84d89),
+     C(4ac6eb21a8cf06f9), C(7d1917afcde42744), C(6b58604b5dd10903),
+     C(c4288dfbc1e319fc), C(230f75ca96817c6e), C(8894cba3b763756c),
+     C(240a67fb)},
+    {C(ea627fc84cd1b857), C(85e372494520071f), C(69ec61800845780b),
+     C(a3c1c5ca1b0367), C(eb6933997272bb3d), C(76a72cb62692a655),
+     C(140bb5531edf756e), C(a3c1c5ca1b0367), C(eb6933997272bb3d),
+     C(76a72cb62692a655), C(140bb5531edf756e), C(8d0d8067d1c925f4),
+     C(7b3fa56d8d77a10c), C(2bd00287b0946d88), C(f08c8e4bd65b8970),
+     C(e1843cd5)},
+    {C(1f2ffd79f2cdc0c8), C(726a1bc31b337aaa), C(678b7f275ef96434),
+     C(5aa82bfaa99d3978), C(c18f96cade5ce18d), C(38404491f9e34c03),
+     C(891fb8926ba0418c), C(5aa82bfaa99d3978), C(c18f96cade5ce18d),
+     C(38404491f9e34c03), C(891fb8926ba0418c), C(e5f69a6398114c15),
+     C(7b8ded3623bc6b1d), C(2f3e5c5da5ff70e8), C(1ab142addea6a9ec),
+     C(fda1452b)},
+    {C(39a9e146ec4b3210), C(f63f75802a78b1ac), C(e2e22539c94741c3),
+     C(8b305d532e61226e), C(caeae80da2ea2e), C(88a6289a76ac684e),
+     C(8ce5b5f9df1cbd85), C(8b305d532e61226e), C(caeae80da2ea2e),
+     C(88a6289a76ac684e), C(8ce5b5f9df1cbd85), C(8ae1fc4798e00d57),
+     C(e7164b8fb364fc46), C(6a978c9bd3a66943), C(ef10d5ae4dd08dc), C(a2cad330)},
+    {C(74cba303e2dd9d6d), C(692699b83289fad1), C(dfb9aa7874678480),
+     C(751390a8a5c41bdc), C(6ee5fbf87605d34), C(6ca73f610f3a8f7c),
+     C(e898b3c996570ad), C(751390a8a5c41bdc), C(6ee5fbf87605d34),
+     C(6ca73f610f3a8f7c), C(e898b3c996570ad), C(98168a5858fc7110),
+     C(6f987fa27aa0daa2), C(f25e3e180d4b36a3), C(d0b03495aeb1be8a),
+     C(53467e16)},
+    {C(4cbc2b73a43071e0), C(56c5db4c4ca4e0b7), C(1b275a162f46bd3d),
+     C(b87a326e413604bf), C(d8f9a5fa214b03ab), C(8a8bb8265771cf88),
+     C(a655319054f6e70f), C(b87a326e413604bf), C(d8f9a5fa214b03ab),
+     C(8a8bb8265771cf88), C(a655319054f6e70f), C(b499cb8e65a9af44),
+     C(bee7fafcc8307491), C(5d2e55fa9b27cda2), C(63b120f5fb2d6ee5),
+     C(da14a8d0)},
+    {C(875638b9715d2221), C(d9ba0615c0c58740), C(616d4be2dfe825aa),
+     C(5df25f13ea7bc284), C(165edfaafd2598fb), C(af7215c5c718c696),
+     C(e9f2f9ca655e769), C(5df25f13ea7bc284), C(165edfaafd2598fb),
+     C(af7215c5c718c696), C(e9f2f9ca655e769), C(e459cfcb565d3d2d),
+     C(41d032631be2418a), C(c505db05fd946f60), C(54990394a714f5de),
+     C(67333551)},
+    {C(fb686b2782994a8d), C(edee60693756bb48), C(e6bc3cae0ded2ef5),
+     C(58eb4d03b2c3ddf5), C(6d2542995f9189f1), C(c0beec58a5f5fea2),
+     C(ed67436f42e2a78b), C(58eb4d03b2c3ddf5), C(6d2542995f9189f1),
+     C(c0beec58a5f5fea2), C(ed67436f42e2a78b), C(dfec763cdb2b5193),
+     C(724a8d5345bd2d6), C(94d4fd1b81457c23), C(28e87c50cdede453), C(a0ebd66e)},
+    {C(ab21d81a911e6723), C(4c31b07354852f59), C(835da384c9384744),
+     C(7f759dddc6e8549a), C(616dd0ca022c8735), C(94717ad4bc15ceb3),
+     C(f66c7be808ab36e), C(7f759dddc6e8549a), C(616dd0ca022c8735),
+     C(94717ad4bc15ceb3), C(f66c7be808ab36e), C(af8286b550b2f4b7),
+     C(745bd217d20a9f40), C(c73bfb9c5430f015), C(55e65922666e3fc2),
+     C(4b769593)},
+    {C(33d013cc0cd46ecf), C(3de726423aea122c), C(116af51117fe21a9),
+     C(f271ba474edc562d), C(e6596e67f9dd3ebd), C(c0a288edf808f383),
+     C(b3def70681c6babc), C(f271ba474edc562d), C(e6596e67f9dd3ebd),
+     C(c0a288edf808f383), C(b3def70681c6babc), C(7da7864e9989b095),
+     C(bf2f8718693cd8a1), C(264a9144166da776), C(61ad90676870beb6),
+     C(6aa75624)},
+    {C(8ca92c7cd39fae5d), C(317e620e1bf20f1), C(4f0b33bf2194b97f),
+     C(45744afcf131dbee), C(97222392c2559350), C(498a19b280c6d6ed),
+     C(83ac2c36acdb8d49), C(45744afcf131dbee), C(97222392c2559350),
+     C(498a19b280c6d6ed), C(83ac2c36acdb8d49), C(7a69645c294daa62),
+     C(abe9d2be8275b3d2), C(39542019de371085), C(7f4efac8488cd6ad),
+     C(602a3f96)},
+    {C(fdde3b03f018f43e), C(38f932946c78660), C(c84084ce946851ee),
+     C(b6dd09ba7851c7af), C(570de4e1bb13b133), C(c4e784eb97211642),
+     C(8285a7fcdcc7c58d), C(b6dd09ba7851c7af), C(570de4e1bb13b133),
+     C(c4e784eb97211642), C(8285a7fcdcc7c58d), C(d421f47990da899b),
+     C(8aed409c997eaa13), C(7a045929c2e29ccf), C(b373682a6202c86b),
+     C(cd183c4d)},
+    {C(9c8502050e9c9458), C(d6d2a1a69964beb9), C(1675766f480229b5),
+     C(216e1d6c86cb524c), C(d01cf6fd4f4065c0), C(fffa4ec5b482ea0f),
+     C(a0e20ee6a5404ac1), C(216e1d6c86cb524c), C(d01cf6fd4f4065c0),
+     C(fffa4ec5b482ea0f), C(a0e20ee6a5404ac1), C(c1b037e4eebaf85e),
+     C(634e3d7c3ebf89eb), C(bcda972358c67d1), C(fd1352181e5b8578), C(960a4d07)},
+    {C(348176ca2fa2fdd2), C(3a89c514cc360c2d), C(9f90b8afb318d6d0),
+     C(bceee07c11a9ac30), C(2e2d47dff8e77eb7), C(11a394cd7b6d614a),
+     C(1d7c41d54e15cb4a), C(bceee07c11a9ac30), C(2e2d47dff8e77eb7),
+     C(11a394cd7b6d614a), C(1d7c41d54e15cb4a), C(15baa5ae7312b0fc),
+     C(f398f596cc984635), C(8ab8fdf87a6788e8), C(b2b5c1234ab47e2), C(9ae998c4)},
+    {C(4a3d3dfbbaea130b), C(4e221c920f61ed01), C(553fd6cd1304531f),
+     C(bd2b31b5608143fe), C(ab717a10f2554853), C(293857f04d194d22),
+     C(d51be8fa86f254f0), C(bd2b31b5608143fe), C(ab717a10f2554853),
+     C(293857f04d194d22), C(d51be8fa86f254f0), C(1eee39e07686907e),
+     C(639039fe0e8d3052), C(d6ec1470cef97ff), C(370c82b860034f0f), C(74e2179d)},
+    {C(b371f768cdf4edb9), C(bdef2ace6d2de0f0), C(e05b4100f7f1baec),
+     C(b9e0d415b4ebd534), C(c97c2a27efaa33d7), C(591cdb35f84ef9da),
+     C(a57d02d0e8e3756c), C(b9e0d415b4ebd534), C(c97c2a27efaa33d7),
+     C(591cdb35f84ef9da), C(a57d02d0e8e3756c), C(23f55f12d7c5c87b),
+     C(4c7ca0fe23221101), C(dbc3020480334564), C(d985992f32c236b1),
+     C(ee9bae25)},
+    {C(7a1d2e96934f61f), C(eb1760ae6af7d961), C(887eb0da063005df),
+     C(2228d6725e31b8ab), C(9b98f7e4d0142e70), C(b6a8c2115b8e0fe7),
+     C(b591e2f5ab9b94b1), C(2228d6725e31b8ab), C(9b98f7e4d0142e70),
+     C(b6a8c2115b8e0fe7), C(b591e2f5ab9b94b1), C(6c1feaa8065318e0),
+     C(4e7e2ca21c2e81fb), C(e9fe5d8ce7993c45), C(ee411fa2f12cf8df),
+     C(b66edf10)},
+    {C(8be53d466d4728f2), C(86a5ac8e0d416640), C(984aa464cdb5c8bb),
+     C(87049e68f5d38e59), C(7d8ce44ec6bd7751), C(cc28d08ab414839c),
+     C(6c8f0bd34fe843e3), C(87049e68f5d38e59), C(7d8ce44ec6bd7751),
+     C(cc28d08ab414839c), C(6c8f0bd34fe843e3), C(b8496dcdc01f3e47),
+     C(2f03125c282ac26), C(82a8797ba3f5ef07), C(7c977a4d10bf52b8), C(d6209737)},
+    {C(829677eb03abf042), C(43cad004b6bc2c0), C(f2f224756803971a),
+     C(98d0dbf796480187), C(fbcb5f3e1bef5742), C(5af2a0463bf6e921),
+     C(ad9555bf0120b3a3), C(98d0dbf796480187), C(fbcb5f3e1bef5742),
+     C(5af2a0463bf6e921), C(ad9555bf0120b3a3), C(283e39b3dc99f447),
+     C(bedaa1a4a0250c28), C(9d50546624ff9a57), C(4abaf523d1c090f6), C(b994a88)},
+    {C(754435bae3496fc), C(5707fc006f094dcf), C(8951c86ab19d8e40),
+     C(57c5208e8f021a77), C(f7653fbb69cd9276), C(a484410af21d75cb),
+     C(f19b6844b3d627e8), C(57c5208e8f021a77), C(f7653fbb69cd9276),
+     C(a484410af21d75cb), C(f19b6844b3d627e8), C(f37400fc3ffd9514),
+     C(36ae0d821734edfd), C(5f37820af1f1f306), C(be637d40e6a5ad0), C(a05d43c0)},
+    {C(fda9877ea8e3805f), C(31e868b6ffd521b7), C(b08c90681fb6a0fd),
+     C(68110a7f83f5d3ff), C(6d77e045901b85a8), C(84ef681113036d8b),
+     C(3b9f8e3928f56160), C(68110a7f83f5d3ff), C(6d77e045901b85a8),
+     C(84ef681113036d8b), C(3b9f8e3928f56160), C(fc8b7f56c130835),
+     C(a11f3e800638e841), C(d9572267f5cf28c1), C(7897c8149803f2aa),
+     C(c79f73a8)},
+    {C(2e36f523ca8f5eb5), C(8b22932f89b27513), C(331cd6ecbfadc1bb),
+     C(d1bfe4df12b04cbf), C(f58c17243fd63842), C(3a453cdba80a60af),
+     C(5737b2ca7470ea95), C(d1bfe4df12b04cbf), C(f58c17243fd63842),
+     C(3a453cdba80a60af), C(5737b2ca7470ea95), C(54d44a3f4477030c),
+     C(8168e02d4869aa7f), C(77f383a17778559d), C(95e1737d77a268fc),
+     C(a490aff5)},
+    {C(21a378ef76828208), C(a5c13037fa841da2), C(506d22a53fbe9812),
+     C(61c9c95d91017da5), C(16f7c83ba68f5279), C(9c0619b0808d05f7),
+     C(83c117ce4e6b70a3), C(61c9c95d91017da5), C(16f7c83ba68f5279),
+     C(9c0619b0808d05f7), C(83c117ce4e6b70a3), C(cfb4c8af7fd01413),
+     C(fdef04e602e72296), C(ed6124d337889b1), C(4919c86707b830da), C(dfad65b4)},
+    {C(ccdd5600054b16ca), C(f78846e84204cb7b), C(1f9faec82c24eac9),
+     C(58634004c7b2d19a), C(24bb5f51ed3b9073), C(46409de018033d00),
+     C(4a9805eed5ac802e), C(58634004c7b2d19a), C(24bb5f51ed3b9073),
+     C(46409de018033d00), C(4a9805eed5ac802e), C(e18de8db306baf82),
+     C(46bbf75f1fa025ff), C(5faf2fb09be09487), C(3fbc62bd4e558fb3), C(1d07dfb)},
+    {C(7854468f4e0cabd0), C(3a3f6b4f098d0692), C(ae2423ec7799d30d),
+     C(29c3529eb165eeba), C(443de3703b657c35), C(66acbce31ae1bc8d),
+     C(1acc99effe1d547e), C(29c3529eb165eeba), C(443de3703b657c35),
+     C(66acbce31ae1bc8d), C(1acc99effe1d547e), C(cf07f8a57906573d),
+     C(31bafb0bbb9a86e7), C(40c69492702a9346), C(7df61fdaa0b858af),
+     C(416df9a0)},
+    {C(7f88db5346d8f997), C(88eac9aacc653798), C(68a4d0295f8eefa1),
+     C(ae59ca86f4c3323d), C(25906c09906d5c4c), C(8dd2aa0c0a6584ae),
+     C(232a7d96b38f40e9), C(ae59ca86f4c3323d), C(25906c09906d5c4c),
+     C(8dd2aa0c0a6584ae), C(232a7d96b38f40e9), C(8986ee00a2ed0042),
+     C(c49ae7e428c8a7d1), C(b7dd8280713ac9c2), C(e018720aed1ebc28),
+     C(1f8fb9cc)},
+    {C(bb3fb5fb01d60fcf), C(1b7cc0847a215eb6), C(1246c994437990a1),
+     C(d4edc954c07cd8f3), C(224f47e7c00a30ab), C(d5ad7ad7f41ef0c6),
+     C(59e089281d869fd7), C(d4edc954c07cd8f3), C(224f47e7c00a30ab),
+     C(d5ad7ad7f41ef0c6), C(59e089281d869fd7), C(f29340d07a14b6f1),
+     C(c87c5ef76d9c4ef3), C(463118794193a9a), C(2922dcb0540f0dbc), C(7abf48e3)},
+    {C(2e783e1761acd84d), C(39158042bac975a0), C(1cd21c5a8071188d),
+     C(b1b7ec44f9302176), C(5cb476450dc0c297), C(dc5ef652521ef6a2),
+     C(3cc79a9e334e1f84), C(b1b7ec44f9302176), C(5cb476450dc0c297),
+     C(dc5ef652521ef6a2), C(3cc79a9e334e1f84), C(769e2a283dbcc651),
+     C(9f24b105c8511d3f), C(c31c15575de2f27e), C(ecfecf32c3ae2d66),
+     C(dea4e3dd)},
+    {C(392058251cf22acc), C(944ec4475ead4620), C(b330a10b5cb94166),
+     C(54bc9bee7cbe1767), C(485820bdbe442431), C(54d6120ea2972e90),
+     C(f437a0341f29b72a), C(54bc9bee7cbe1767), C(485820bdbe442431),
+     C(54d6120ea2972e90), C(f437a0341f29b72a), C(8f30885c784d5704),
+     C(aa95376b16c7906a), C(e826928cfaf93dc3), C(20e8f54d1c16d7d8),
+     C(c6064f22)},
+    {C(adf5c1e5d6419947), C(2a9747bc659d28aa), C(95c5b8cb1f5d62c),
+     C(80973ea532b0f310), C(a471829aa9c17dd9), C(c2ff3479394804ab),
+     C(6bf44f8606753636), C(80973ea532b0f310), C(a471829aa9c17dd9),
+     C(c2ff3479394804ab), C(6bf44f8606753636), C(5184d2973e6dd827),
+     C(121b96369a332d9a), C(5c25d3475ab69e50), C(26d2961d62884168),
+     C(743bed9c)},
+    {C(6bc1db2c2bee5aba), C(e63b0ed635307398), C(7b2eca111f30dbbc),
+     C(230d2b3e47f09830), C(ec8624a821c1caf4), C(ea6ec411cdbf1cb1),
+     C(5f38ae82af364e27), C(230d2b3e47f09830), C(ec8624a821c1caf4),
+     C(ea6ec411cdbf1cb1), C(5f38ae82af364e27), C(a519ef515ea7187c),
+     C(6bad5efa7ebae05f), C(748abacb11a74a63), C(a28eef963d1396eb),
+     C(fce254d5)},
+    {C(b00f898229efa508), C(83b7590ad7f6985c), C(2780e70a0592e41d),
+     C(7122413bdbc94035), C(e7f90fae33bf7763), C(4b6bd0fb30b12387),
+     C(557359c0c44f48ca), C(7122413bdbc94035), C(e7f90fae33bf7763),
+     C(4b6bd0fb30b12387), C(557359c0c44f48ca), C(d5656c3d6bc5f0d),
+     C(983ff8e5e784da99), C(628479671b445bf), C(e179a1e27ce68f5d), C(e47ec9d1)},
+    {C(b56eb769ce0d9a8c), C(ce196117bfbcaf04), C(b26c3c3797d66165),
+     C(5ed12338f630ab76), C(fab19fcb319116d), C(167f5f42b521724b),
+     C(c4aa56c409568d74), C(5ed12338f630ab76), C(fab19fcb319116d),
+     C(167f5f42b521724b), C(c4aa56c409568d74), C(75fff4b42f8e9778),
+     C(94218f94710c1ea3), C(b7b05efb738b06a6), C(83fff2deabf9cd3), C(334a145c)},
+    {C(70c0637675b94150), C(259e1669305b0a15), C(46e1dd9fd387a58d),
+     C(fca4e5bc9292788e), C(cd509dc1facce41c), C(bbba575a59d82fe),
+     C(4e2e71c15b45d4d3), C(fca4e5bc9292788e), C(cd509dc1facce41c),
+     C(bbba575a59d82fe), C(4e2e71c15b45d4d3), C(5dc54582ead999c),
+     C(72612d1571963c6f), C(30318a9d2d3d1829), C(785dd00f4cc9c9a0),
+     C(adec1e3c)},
+    {C(74c0b8a6821faafe), C(abac39d7491370e7), C(faf0b2a48a4e6aed),
+     C(967e970df9673d2a), C(d465247cffa415c0), C(33a1df0ca1107722),
+     C(49fc2a10adce4a32), C(967e970df9673d2a), C(d465247cffa415c0),
+     C(33a1df0ca1107722), C(49fc2a10adce4a32), C(c5707e079a284308),
+     C(573028266635dda6), C(f786f5eee6127fa0), C(b30d79cebfb51266),
+     C(f6a9fbf8)},
+    {C(5fb5e48ac7b7fa4f), C(a96170f08f5acbc7), C(bbf5c63d4f52a1e5),
+     C(6cc09e60700563e9), C(d18f23221e964791), C(ffc23eeef7af26eb),
+     C(693a954a3622a315), C(815308a32a9b0daf), C(efb2ab27bf6fd0bd),
+     C(9f1ffc0986111118), C(f9a3aa1778ea3985), C(698fe54b2b93933b),
+     C(dacc2b28404d0f10), C(815308a32a9b0daf), C(efb2ab27bf6fd0bd),
+     C(5398210c)},
+};
+
+void TestUnchanging(const uint64_t* expected, int offset, int len) {
+  const uint128 u = CityHash128(data + offset, len);
+  const uint128 v = CityHash128WithSeed(data + offset, len, kSeed128);
+  EXPECT_EQ(expected[0], CityHash64(data + offset, len));
+  EXPECT_EQ(expected[15], CityHash32(data + offset, len));
+  EXPECT_EQ(expected[1], CityHash64WithSeed(data + offset, len, kSeed0));
+  EXPECT_EQ(expected[2],
+            CityHash64WithSeeds(data + offset, len, kSeed0, kSeed1));
+  EXPECT_EQ(expected[3], Uint128Low64(u));
+  EXPECT_EQ(expected[4], Uint128High64(u));
+  EXPECT_EQ(expected[5], Uint128Low64(v));
+  EXPECT_EQ(expected[6], Uint128High64(v));
+#ifdef __SSE4_2__
+  const uint128 y = CityHashCrc128(data + offset, len);
+  const uint128 z = CityHashCrc128WithSeed(data + offset, len, kSeed128);
+  uint64_t crc256_results[4];
+  CityHashCrc256(data + offset, len, crc256_results);
+  EXPECT_EQ(expected[7], Uint128Low64(y));
+  EXPECT_EQ(expected[8], Uint128High64(y));
+  EXPECT_EQ(expected[9], Uint128Low64(z));
+  EXPECT_EQ(expected[10], Uint128High64(z));
+  for (int i = 0; i < 4; i++) {
+    EXPECT_EQ(expected[11 + i], crc256_results[i]);
+  }
+#endif
+}
+
+TEST(CityHashTest, Unchanging) {
+  setup();
+  int i = 0;
+  for (; i < kTestSize - 1; i++) {
+    TestUnchanging(testdata[i], i * i, i);
+  }
+  TestUnchanging(testdata[i], 0, kDataSize);
+}
+
+}  // namespace hash_internal
+}  // namespace absl
diff --git a/absl/hash/internal/hash.cc b/absl/hash/internal/hash.cc
new file mode 100644
index 0000000..4bf6409
--- /dev/null
+++ b/absl/hash/internal/hash.cc
@@ -0,0 +1,23 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/hash/internal/hash.h"
+
+namespace absl {
+namespace hash_internal {
+
+ABSL_CONST_INIT const void* const CityHashState::kSeed = &kSeed;
+
+}  // namespace hash_internal
+}  // namespace absl
diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h
new file mode 100644
index 0000000..4543d67
--- /dev/null
+++ b/absl/hash/internal/hash.h
@@ -0,0 +1,885 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// -----------------------------------------------------------------------------
+// File: hash.h
+// -----------------------------------------------------------------------------
+//
+#ifndef ABSL_HASH_INTERNAL_HASH_H_
+#define ABSL_HASH_INTERNAL_HASH_H_
+
+#include <algorithm>
+#include <array>
+#include <cmath>
+#include <cstring>
+#include <deque>
+#include <forward_list>
+#include <functional>
+#include <iterator>
+#include <limits>
+#include <list>
+#include <map>
+#include <memory>
+#include <set>
+#include <string>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "absl/base/internal/endian.h"
+#include "absl/base/port.h"
+#include "absl/container/fixed_array.h"
+#include "absl/meta/type_traits.h"
+#include "absl/numeric/int128.h"
+#include "absl/strings/string_view.h"
+#include "absl/types/optional.h"
+#include "absl/types/variant.h"
+#include "absl/utility/utility.h"
+#include "absl/hash/internal/city.h"
+
+namespace absl {
+namespace hash_internal {
+
+// HashStateBase
+//
+// A hash state object represents an intermediate state in the computation
+// of an unspecified hash algorithm. `HashStateBase` provides a CRTP style
+// base class for hash state implementations. Developers adding type support
+// for `absl::Hash` should not rely on any parts of the state object other than
+// the following member functions:
+//
+//   * HashStateBase::combine()
+//   * HashStateBase::combine_contiguous()
+//
+// A derived hash state class of type `H` must provide a static member function
+// with a signature similar to the following:
+//
+//    `static H combine_contiguous(H state, const unsigned char*, size_t)`.
+//
+// `HashStateBase` will provide a complete implementations for a hash state
+// object in terms of this method.
+//
+// Example:
+//
+//   // Use CRTP to define your derived class.
+//   struct MyHashState : HashStateBase<MyHashState> {
+//       static H combine_contiguous(H state, const unsigned char*, size_t);
+//       using MyHashState::HashStateBase::combine;
+//       using MyHashState::HashStateBase::combine_contiguous;
+//   };
+template <typename H>
+class HashStateBase {
+ public:
+  // HashStateBase::combine()
+  //
+  // Combines an arbitrary number of values into a hash state, returning the
+  // updated state.
+  //
+  // Each of the value types `T` must be separately hashable by the Abseil
+  // hashing framework.
+  //
+  // NOTE:
+  //
+  //   state = H::combine(std::move(state), value1, value2, value3);
+  //
+  // is guaranteed to produce the same hash expansion as:
+  //
+  //   state = H::combine(std::move(state), value1);
+  //   state = H::combine(std::move(state), value2);
+  //   state = H::combine(std::move(state), value3);
+  template <typename T, typename... Ts>
+  static H combine(H state, const T& value, const Ts&... values);
+  static H combine(H state) { return state; }
+
+  // HashStateBase::combine_contiguous()
+  //
+  // Combines a contiguous array of `size` elements into a hash state, returning
+  // the updated state.
+  //
+  // NOTE:
+  //
+  //   state = H::combine_contiguous(std::move(state), data, size);
+  //
+  // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
+  // perform internal optimizations).  If you need this guarantee, use the
+  // for-loop instead.
+  template <typename T>
+  static H combine_contiguous(H state, const T* data, size_t size);
+};
+
+// is_uniquely_represented
+//
+// `is_uniquely_represented<T>` is a trait class that indicates whether `T`
+// is uniquely represented.
+//
+// A type is "uniquely represented" if two equal values of that type are
+// guaranteed to have the same bytes in their underlying storage. In other
+// words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
+// zero. This property cannot be detected automatically, so this trait is false
+// by default, but can be specialized by types that wish to assert that they are
+// uniquely represented. This makes them eligible for certain optimizations.
+//
+// If you have any doubt whatsoever, do not specialize this template.
+// The default is completely safe, and merely disables some optimizations
+// that will not matter for most types. Specializing this template,
+// on the other hand, can be very hazardous.
+//
+// To be uniquely represented, a type must not have multiple ways of
+// representing the same value; for example, float and double are not
+// uniquely represented, because they have distinct representations for
+// +0 and -0. Furthermore, the type's byte representation must consist
+// solely of user-controlled data, with no padding bits and no compiler-
+// controlled data such as vptrs or sanitizer metadata. This is usually
+// very difficult to guarantee, because in most cases the compiler can
+// insert data and padding bits at its own discretion.
+//
+// If you specialize this template for a type `T`, you must do so in the file
+// that defines that type (or in this file). If you define that specialization
+// anywhere else, `is_uniquely_represented<T>` could have different meanings
+// in different places.
+//
+// The Enable parameter is meaningless; it is provided as a convenience,
+// to support certain SFINAE techniques when defining specializations.
+template <typename T, typename Enable = void>
+struct is_uniquely_represented : std::false_type {};
+
+// is_uniquely_represented<unsigned char>
+//
+// unsigned char is a synonym for "byte", so it is guaranteed to be
+// uniquely represented.
+template <>
+struct is_uniquely_represented<unsigned char> : std::true_type {};
+
+// is_uniquely_represented for non-standard integral types
+//
+// Integral types other than bool should be uniquely represented on any
+// platform that this will plausibly be ported to.
+template <typename Integral>
+struct is_uniquely_represented<
+    Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
+    : std::true_type {};
+
+// is_uniquely_represented<bool>
+//
+//
+template <>
+struct is_uniquely_represented<bool> : std::false_type {};
+
+// hash_bytes()
+//
+// Convenience function that combines `hash_state` with the byte representation
+// of `value`.
+template <typename H, typename T>
+H hash_bytes(H hash_state, const T& value) {
+  const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
+  return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Basic Types
+// -----------------------------------------------------------------------------
+
+// Note: Default `AbslHashValue` implementations live in `hash_internal`. This
+// allows us to block lexical scope lookup when doing an unqualified call to
+// `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
+// only be found via ADL.
+
+// AbslHashValue() for hashing bool values
+//
+// We use SFINAE to ensure that this overload only accepts bool, not types that
+// are convertible to bool.
+template <typename H, typename B>
+typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
+    H hash_state, B value) {
+  return H::combine(std::move(hash_state),
+                    static_cast<unsigned char>(value ? 1 : 0));
+}
+
+// AbslHashValue() for hashing enum values
+template <typename H, typename Enum>
+typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
+    H hash_state, Enum e) {
+  // In practice, we could almost certainly just invoke hash_bytes directly,
+  // but it's possible that a sanitizer might one day want to
+  // store data in the unused bits of an enum. To avoid that risk, we
+  // convert to the underlying type before hashing. Hopefully this will get
+  // optimized away; if not, we can reopen discussion with c-toolchain-team.
+  return H::combine(std::move(hash_state),
+                    static_cast<typename std::underlying_type<Enum>::type>(e));
+}
+// AbslHashValue() for hashing floating-point values
+template <typename H, typename Float>
+typename std::enable_if<std::is_floating_point<Float>::value, H>::type
+AbslHashValue(H hash_state, Float value) {
+  return hash_internal::hash_bytes(std::move(hash_state),
+                                   value == 0 ? 0 : value);
+}
+
+// Long double has the property that it might have extra unused bytes in it.
+// For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
+// of it. This means we can't use hash_bytes on a long double and have to
+// convert it to something else first.
+template <typename H>
+H AbslHashValue(H hash_state, long double value) {
+  const int category = std::fpclassify(value);
+  switch (category) {
+    case FP_INFINITE:
+      // Add the sign bit to differentiate between +Inf and -Inf
+      hash_state = H::combine(std::move(hash_state), std::signbit(value));
+      break;
+
+    case FP_NAN:
+    case FP_ZERO:
+    default:
+      // Category is enough for these.
+      break;
+
+    case FP_NORMAL:
+    case FP_SUBNORMAL:
+      // We can't convert `value` directly to double because this would have
+      // undefined behavior if the value is out of range.
+      // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
+      // guaranteed to be in range for `double`. The truncation is
+      // implementation defined, but that works as long as it is deterministic.
+      int exp;
+      auto mantissa = static_cast<double>(std::frexp(value, &exp));
+      hash_state = H::combine(std::move(hash_state), mantissa, exp);
+  }
+
+  return H::combine(std::move(hash_state), category);
+}
+
+// AbslHashValue() for hashing pointers
+template <typename H, typename T>
+H AbslHashValue(H hash_state, T* ptr) {
+  return hash_internal::hash_bytes(std::move(hash_state), ptr);
+}
+
+// AbslHashValue() for hashing nullptr_t
+template <typename H>
+H AbslHashValue(H hash_state, std::nullptr_t) {
+  return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Composite Types
+// -----------------------------------------------------------------------------
+
+// is_hashable()
+//
+// Trait class which returns true if T is hashable by the absl::Hash framework.
+// Used for the AbslHashValue implementations for composite types below.
+template <typename T>
+struct is_hashable;
+
+// AbslHashValue() for hashing pairs
+template <typename H, typename T1, typename T2>
+typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
+                        H>::type
+AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
+  return H::combine(std::move(hash_state), p.first, p.second);
+}
+
+// hash_tuple()
+//
+// Helper function for hashing a tuple. The third argument should
+// be an index_sequence running from 0 to tuple_size<Tuple> - 1.
+template <typename H, typename Tuple, size_t... Is>
+H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
+  return H::combine(std::move(hash_state), std::get<Is>(t)...);
+}
+
+// AbslHashValue for hashing tuples
+template <typename H, typename... Ts>
+#if _MSC_VER
+// This SFINAE gets MSVC confused under some conditions. Let's just disable it
+// for now.
+H
+#else
+typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
+#endif
+AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
+  return hash_internal::hash_tuple(std::move(hash_state), t,
+                                   absl::make_index_sequence<sizeof...(Ts)>());
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Pointers
+// -----------------------------------------------------------------------------
+
+// AbslHashValue for hashing unique_ptr
+template <typename H, typename T, typename D>
+H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
+  return H::combine(std::move(hash_state), ptr.get());
+}
+
+// AbslHashValue for hashing shared_ptr
+template <typename H, typename T>
+H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
+  return H::combine(std::move(hash_state), ptr.get());
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for String-Like Types
+// -----------------------------------------------------------------------------
+
+// AbslHashValue for hashing strings
+//
+// All the string-like types supported here provide the same hash expansion for
+// the same character sequence. These types are:
+//
+//  - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
+//      any allocator A)
+//  - `absl::string_view` and `std::string_view`
+//
+// For simplicity, we currently support only `char` strings. This support may
+// be broadened, if necessary, but with some caution - this overload would
+// misbehave in cases where the traits' `eq()` member isn't equivalent to `==`
+// on the underlying character type.
+template <typename H>
+H AbslHashValue(H hash_state, absl::string_view str) {
+  return H::combine(
+      H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
+      str.size());
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Sequence Containers
+// -----------------------------------------------------------------------------
+
+// AbslHashValue for hashing std::array
+template <typename H, typename T, size_t N>
+typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
+    H hash_state, const std::array<T, N>& array) {
+  return H::combine_contiguous(std::move(hash_state), array.data(),
+                               array.size());
+}
+
+// AbslHashValue for hashing std::deque
+template <typename H, typename T, typename Allocator>
+typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
+    H hash_state, const std::deque<T, Allocator>& deque) {
+  // TODO(gromer): investigate a more efficient implementation taking
+  // advantage of the chunk structure.
+  for (const auto& t : deque) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), deque.size());
+}
+
+// AbslHashValue for hashing std::forward_list
+template <typename H, typename T, typename Allocator>
+typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
+    H hash_state, const std::forward_list<T, Allocator>& list) {
+  size_t size = 0;
+  for (const T& t : list) {
+    hash_state = H::combine(std::move(hash_state), t);
+    ++size;
+  }
+  return H::combine(std::move(hash_state), size);
+}
+
+// AbslHashValue for hashing std::list
+template <typename H, typename T, typename Allocator>
+typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
+    H hash_state, const std::list<T, Allocator>& list) {
+  for (const auto& t : list) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), list.size());
+}
+
+// AbslHashValue for hashing std::vector
+//
+// Do not use this for vector<bool>. It does not have a .data(), and a fallback
+// for std::hash<> is most likely faster.
+template <typename H, typename T, typename Allocator>
+typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
+                        H>::type
+AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
+  return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
+                                          vector.size()),
+                    vector.size());
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Ordered Associative Containers
+// -----------------------------------------------------------------------------
+
+// AbslHashValue for hashing std::map
+template <typename H, typename Key, typename T, typename Compare,
+          typename Allocator>
+typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
+                        H>::type
+AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
+  for (const auto& t : map) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), map.size());
+}
+
+// AbslHashValue for hashing std::multimap
+template <typename H, typename Key, typename T, typename Compare,
+          typename Allocator>
+typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
+                        H>::type
+AbslHashValue(H hash_state,
+              const std::multimap<Key, T, Compare, Allocator>& map) {
+  for (const auto& t : map) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), map.size());
+}
+
+// AbslHashValue for hashing std::set
+template <typename H, typename Key, typename Compare, typename Allocator>
+typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
+    H hash_state, const std::set<Key, Compare, Allocator>& set) {
+  for (const auto& t : set) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), set.size());
+}
+
+// AbslHashValue for hashing std::multiset
+template <typename H, typename Key, typename Compare, typename Allocator>
+typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
+    H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
+  for (const auto& t : set) {
+    hash_state = H::combine(std::move(hash_state), t);
+  }
+  return H::combine(std::move(hash_state), set.size());
+}
+
+// -----------------------------------------------------------------------------
+// AbslHashValue for Wrapper Types
+// -----------------------------------------------------------------------------
+
+// AbslHashValue for hashing absl::optional
+template <typename H, typename T>
+typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
+    H hash_state, const absl::optional<T>& opt) {
+  if (opt) hash_state = H::combine(std::move(hash_state), *opt);
+  return H::combine(std::move(hash_state), opt.has_value());
+}
+
+// VariantVisitor
+template <typename H>
+struct VariantVisitor {
+  H&& hash_state;
+  template <typename T>
+  H operator()(const T& t) const {
+    return H::combine(std::move(hash_state), t);
+  }
+};
+
+// AbslHashValue for hashing absl::variant
+template <typename H, typename... T>
+typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
+AbslHashValue(H hash_state, const absl::variant<T...>& v) {
+  if (!v.valueless_by_exception()) {
+    hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
+  }
+  return H::combine(std::move(hash_state), v.index());
+}
+// -----------------------------------------------------------------------------
+
+// hash_range_or_bytes()
+//
+// Mixes all values in the range [data, data+size) into the hash state.
+// This overload accepts only uniquely-represented types, and hashes them by
+// hashing the entire range of bytes.
+template <typename H, typename T>
+typename std::enable_if<is_uniquely_represented<T>::value, H>::type
+hash_range_or_bytes(H hash_state, const T* data, size_t size) {
+  const auto* bytes = reinterpret_cast<const unsigned char*>(data);
+  return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
+}
+
+// hash_range_or_bytes()
+template <typename H, typename T>
+typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
+hash_range_or_bytes(H hash_state, const T* data, size_t size) {
+  for (const auto end = data + size; data < end; ++data) {
+    hash_state = H::combine(std::move(hash_state), *data);
+  }
+  return hash_state;
+}
+
+// InvokeHashTag
+//
+// InvokeHash(H, const T&) invokes the appropriate hash implementation for a
+// hasher of type `H` and a value of type `T`. If `T` is not hashable, there
+// will be no matching overload of InvokeHash().
+// Note: Some platforms (eg MSVC) do not support the detect idiom on
+// std::hash. In those platforms the last fallback will be std::hash and
+// InvokeHash() will always have a valid overload even if std::hash<T> is not
+// valid.
+//
+// We try the following options in order:
+//   * If is_uniquely_represented, hash bytes directly.
+//   * ADL AbslHashValue(H, const T&) call.
+//   * std::hash<T>
+
+// In MSVC we can't probe std::hash or stdext::hash because it triggers a
+// static_assert instead of failing substitution.
+#if defined(_MSC_VER)
+#undef ABSL_HASH_INTERNAL_CAN_POISON_
+#else   // _MSC_VER
+#define ABSL_HASH_INTERNAL_CAN_POISON_ 1
+#endif  // _MSC_VER
+
+#if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
+    ABSL_HASH_INTERNAL_CAN_POISON_
+#define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
+#endif
+
+enum class InvokeHashTag {
+  kUniquelyRepresented,
+  kHashValue,
+#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+  kLegacyHash,
+#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+  kStdHash,
+  kNone
+};
+
+// HashSelect
+//
+// Type trait to select the appropriate hash implementation to use.
+// HashSelect<T>::value is an instance of InvokeHashTag that indicates the best
+// available hashing mechanism.
+// See `Note` above about MSVC.
+template <typename T>
+struct HashSelect {
+ private:
+  struct State : HashStateBase<State> {
+    static State combine_contiguous(State hash_state, const unsigned char*,
+                                    size_t);
+    using State::HashStateBase::combine_contiguous;
+  };
+
+  // `Probe<V, Tag>::value` evaluates to `V<T>::value` if it is a valid
+  // expression, and `false` otherwise.
+  // `Probe<V, Tag>::tag` always evaluates to `Tag`.
+  template <template <typename> class V, InvokeHashTag Tag>
+  struct Probe {
+   private:
+    template <typename U, typename std::enable_if<V<U>::value, int>::type = 0>
+    static std::true_type Test(int);
+    template <typename U>
+    static std::false_type Test(char);
+
+   public:
+    static constexpr InvokeHashTag kTag = Tag;
+    static constexpr bool value = decltype(
+        Test<absl::remove_const_t<absl::remove_reference_t<T>>>(0))::value;
+  };
+
+  template <typename U>
+  using ProbeUniquelyRepresented = is_uniquely_represented<U>;
+
+  template <typename U>
+  using ProbeHashValue =
+      std::is_same<State, decltype(AbslHashValue(std::declval<State>(),
+                                                 std::declval<const U&>()))>;
+
+#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+  template <typename U>
+  using ProbeLegacyHash =
+      std::is_convertible<decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<
+                                   U>()(std::declval<const U&>())),
+                          size_t>;
+#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+
+  template <typename U>
+  using ProbeStdHash =
+#if ABSL_HASH_INTERNAL_CAN_POISON_
+      std::is_convertible<decltype(std::hash<U>()(std::declval<const U&>())),
+                          size_t>;
+#else   // ABSL_HASH_INTERNAL_CAN_POISON_
+      std::true_type;
+#endif  // ABSL_HASH_INTERNAL_CAN_POISON_
+
+  template <typename U>
+  using ProbeNone = std::true_type;
+
+ public:
+  // Probe each implementation in order.
+  // disjunction provides short circuting wrt instantiation.
+  static constexpr InvokeHashTag value = absl::disjunction<
+      Probe<ProbeUniquelyRepresented, InvokeHashTag::kUniquelyRepresented>,
+      Probe<ProbeHashValue, InvokeHashTag::kHashValue>,
+#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+      Probe<ProbeLegacyHash, InvokeHashTag::kLegacyHash>,
+#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+      Probe<ProbeStdHash, InvokeHashTag::kStdHash>,
+      Probe<ProbeNone, InvokeHashTag::kNone>>::kTag;
+};
+
+template <typename T>
+struct is_hashable : std::integral_constant<bool, HashSelect<T>::value !=
+                                                      InvokeHashTag::kNone> {};
+
+template <typename H, typename T>
+absl::enable_if_t<HashSelect<T>::value == InvokeHashTag::kUniquelyRepresented,
+                  H>
+InvokeHash(H state, const T& value) {
+  return hash_internal::hash_bytes(std::move(state), value);
+}
+
+template <typename H, typename T>
+absl::enable_if_t<HashSelect<T>::value == InvokeHashTag::kHashValue, H>
+InvokeHash(H state, const T& value) {
+  return AbslHashValue(std::move(state), value);
+}
+
+#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+template <typename H, typename T>
+absl::enable_if_t<HashSelect<T>::value == InvokeHashTag::kLegacyHash, H>
+InvokeHash(H state, const T& value) {
+  return hash_internal::hash_bytes(
+      std::move(state), ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
+}
+#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
+
+template <typename H, typename T>
+absl::enable_if_t<HashSelect<T>::value == InvokeHashTag::kStdHash, H>
+InvokeHash(H state, const T& value) {
+  return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
+}
+
+// CityHashState
+class CityHashState : public HashStateBase<CityHashState> {
+  // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
+  // We use the intrinsic when available to improve performance.
+#ifdef ABSL_HAVE_INTRINSIC_INT128
+  using uint128 = __uint128_t;
+#else   // ABSL_HAVE_INTRINSIC_INT128
+  using uint128 = absl::uint128;
+#endif  // ABSL_HAVE_INTRINSIC_INT128
+
+  static constexpr uint64_t kMul =
+      sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51} : uint64_t{0x9ddfea08eb382d69};
+
+  template <typename T>
+  using IntegralFastPath =
+      conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
+
+ public:
+  // Move only
+  CityHashState(CityHashState&&) = default;
+  CityHashState& operator=(CityHashState&&) = default;
+
+  // CityHashState::combine_contiguous()
+  //
+  // Fundamental base case for hash recursion: mixes the given range of bytes
+  // into the hash state.
+  static CityHashState combine_contiguous(CityHashState hash_state,
+                                          const unsigned char* first,
+                                          size_t size) {
+    return CityHashState(
+        CombineContiguousImpl(hash_state.state_, first, size,
+                              std::integral_constant<int, sizeof(size_t)>{}));
+  }
+  using CityHashState::HashStateBase::combine_contiguous;
+
+  // CityHashState::hash()
+  //
+  // For performance reasons in non-opt mode, we specialize this for
+  // integral types.
+  // Otherwise we would be instantiating and calling dozens of functions for
+  // something that is just one multiplication and a couple xor's.
+  // The result should be the same as running the whole algorithm, but faster.
+  template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
+  static size_t hash(T value) {
+    return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
+  }
+
+  // Overload of CityHashState::hash()
+  template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
+  static size_t hash(const T& value) {
+    return static_cast<size_t>(combine(CityHashState{}, value).state_);
+  }
+
+ private:
+  // Invoked only once for a given argument; that plus the fact that this is
+  // move-only ensures that there is only one non-moved-from object.
+  CityHashState() : state_(Seed()) {}
+
+  // Workaround for MSVC bug.
+  // We make the type copyable to fix the calling convention, even though we
+  // never actually copy it. Keep it private to not affect the public API of the
+  // type.
+  CityHashState(const CityHashState&) = default;
+
+  explicit CityHashState(uint64_t state) : state_(state) {}
+
+  // Implementation of the base case for combine_contiguous where we actually
+  // mix the bytes into the state.
+  // Dispatch to different implementations of the combine_contiguous depending
+  // on the value of `sizeof(size_t)`.
+  static uint64_t CombineContiguousImpl(uint64_t state,
+                                        const unsigned char* first, size_t len,
+                                        std::integral_constant<int, 4>
+                                        /* sizeof_size_t */);
+  static uint64_t CombineContiguousImpl(uint64_t state,
+                                        const unsigned char* first, size_t len,
+                                        std::integral_constant<int, 8>
+                                        /* sizeof_size_t*/);
+
+  // Reads 9 to 16 bytes from p.
+  // The first 8 bytes are in .first, the rest (zero padded) bytes are in
+  // .second.
+  static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
+                                                 size_t len) {
+    uint64_t high = little_endian::Load64(p + len - 8);
+    return {little_endian::Load64(p), high >> (128 - len * 8)};
+  }
+
+  // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
+  static uint64_t Read4To8(const unsigned char* p, size_t len) {
+    return (static_cast<uint64_t>(little_endian::Load32(p + len - 4))
+            << (len - 4) * 8) |
+           little_endian::Load32(p);
+  }
+
+  // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
+  static uint32_t Read1To3(const unsigned char* p, size_t len) {
+    return static_cast<uint32_t>((p[0]) |                         //
+                                 (p[len / 2] << (len / 2 * 8)) |  //
+                                 (p[len - 1] << ((len - 1) * 8)));
+  }
+
+  ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
+    using MultType =
+        absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
+    // We do the addition in 64-bit space to make sure the 128-bit
+    // multiplication is fast. If we were to do it as MultType the compiler has
+    // to assume that the high word is non-zero and needs to perform 2
+    // multiplications instead of one.
+    MultType m = state + v;
+    m *= kMul;
+    return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
+  }
+
+  // Seed()
+  //
+  // A non-deterministic seed.
+  //
+  // The current purpose of this seed is to generate non-deterministic results
+  // and prevent having users depend on the particular hash values.
+  // It is not meant as a security feature right now, but it leaves the door
+  // open to upgrade it to a true per-process random seed. A true random seed
+  // costs more and we don't need to pay for that right now.
+  //
+  // On platforms with ASLR, we take advantage of it to make a per-process
+  // random value.
+  // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
+  //
+  // On other platforms this is still going to be non-deterministic but most
+  // probably per-build and not per-process.
+  ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
+    return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
+  }
+  static const void* const kSeed;
+
+  uint64_t state_;
+};
+
+// CityHashState::CombineContiguousImpl()
+inline uint64_t CityHashState::CombineContiguousImpl(
+    uint64_t state, const unsigned char* first, size_t len,
+    std::integral_constant<int, 4> /* sizeof_size_t */) {
+  // For large values we use CityHash, for small ones we just use a
+  // multiplicative hash.
+  uint64_t v;
+  if (len > 8) {
+    v = absl::hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
+  } else if (len >= 4) {
+    v = Read4To8(first, len);
+  } else if (len > 0) {
+    v = Read1To3(first, len);
+  } else {
+    // Empty ranges have no effect.
+    return state;
+  }
+  return Mix(state, v);
+}
+
+// Overload of CityHashState::CombineContiguousImpl()
+inline uint64_t CityHashState::CombineContiguousImpl(
+    uint64_t state, const unsigned char* first, size_t len,
+    std::integral_constant<int, 8> /* sizeof_size_t */) {
+  // For large values we use CityHash, for small ones we just use a
+  // multiplicative hash.
+  uint64_t v;
+  if (len > 16) {
+    v = absl::hash_internal::CityHash64(reinterpret_cast<const char*>(first), len);
+  } else if (len > 8) {
+    auto p = Read9To16(first, len);
+    state = Mix(state, p.first);
+    v = p.second;
+  } else if (len >= 4) {
+    v = Read4To8(first, len);
+  } else if (len > 0) {
+    v = Read1To3(first, len);
+  } else {
+    // Empty ranges have no effect.
+    return state;
+  }
+  return Mix(state, v);
+}
+
+
+struct AggregateBarrier {};
+
+// HashImpl
+
+// Add a private base class to make sure this type is not an aggregate.
+// Aggregates can be aggregate initialized even if the default constructor is
+// deleted.
+struct PoisonedHash : private AggregateBarrier {
+  PoisonedHash() = delete;
+  PoisonedHash(const PoisonedHash&) = delete;
+  PoisonedHash& operator=(const PoisonedHash&) = delete;
+};
+
+template <typename T>
+struct HashImpl {
+  size_t operator()(const T& value) const { return CityHashState::hash(value); }
+};
+
+template <typename T>
+struct Hash
+    : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
+
+template <typename H>
+template <typename T, typename... Ts>
+H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
+  return H::combine(hash_internal::InvokeHash(std::move(state), value),
+                    values...);
+}
+
+// HashStateBase::combine_contiguous()
+template <typename H>
+template <typename T>
+H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
+  return hash_internal::hash_range_or_bytes(std::move(state), data, size);
+}
+}  // namespace hash_internal
+}  // namespace absl
+
+#endif  // ABSL_HASH_INTERNAL_HASH_H_
diff --git a/absl/hash/internal/print_hash_of.cc b/absl/hash/internal/print_hash_of.cc
new file mode 100644
index 0000000..b6df31c
--- /dev/null
+++ b/absl/hash/internal/print_hash_of.cc
@@ -0,0 +1,23 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <cstdlib>
+
+#include "absl/hash/hash.h"
+
+// Prints the hash of argv[1].
+int main(int argc, char** argv) {
+  if (argc < 2) return 1;
+  printf("%zu\n", absl::Hash<int>{}(std::atoi(argv[1])));  // NOLINT
+}
diff --git a/absl/hash/internal/spy_hash_state.h b/absl/hash/internal/spy_hash_state.h
new file mode 100644
index 0000000..03d795b
--- /dev/null
+++ b/absl/hash/internal/spy_hash_state.h
@@ -0,0 +1,218 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef ABSL_HASH_INTERNAL_SPY_HASH_STATE_H_
+#define ABSL_HASH_INTERNAL_SPY_HASH_STATE_H_
+
+#include <ostream>
+#include <string>
+#include <vector>
+
+#include "absl/hash/hash.h"
+#include "absl/strings/match.h"
+#include "absl/strings/str_format.h"
+#include "absl/strings/str_join.h"
+
+namespace absl {
+namespace hash_internal {
+
+// SpyHashState is an implementation of the HashState API that simply
+// accumulates all input bytes in an internal buffer. This makes it useful
+// for testing AbslHashValue overloads (so long as they are templated on the
+// HashState parameter), since it can report the exact hash representation
+// that the AbslHashValue overload produces.
+//
+// Sample usage:
+// EXPECT_EQ(SpyHashState::combine(SpyHashState(), foo),
+//           SpyHashState::combine(SpyHashState(), bar));
+template <typename T>
+class SpyHashStateImpl : public HashStateBase<SpyHashStateImpl<T>> {
+ public:
+  SpyHashStateImpl()
+      : error_(std::make_shared<absl::optional<std::string>>()) {
+    static_assert(std::is_void<T>::value, "");
+  }
+
+  // Move-only
+  SpyHashStateImpl(const SpyHashStateImpl&) = delete;
+  SpyHashStateImpl& operator=(const SpyHashStateImpl&) = delete;
+
+  SpyHashStateImpl(SpyHashStateImpl&& other) noexcept {
+    *this = std::move(other);
+  }
+
+  SpyHashStateImpl& operator=(SpyHashStateImpl&& other) noexcept {
+    hash_representation_ = std::move(other.hash_representation_);
+    error_ = other.error_;
+    moved_from_ = other.moved_from_;
+    other.moved_from_ = true;
+    return *this;
+  }
+
+  template <typename U>
+  SpyHashStateImpl(SpyHashStateImpl<U>&& other) {  // NOLINT
+    hash_representation_ = std::move(other.hash_representation_);
+    error_ = other.error_;
+    moved_from_ = other.moved_from_;
+    other.moved_from_ = true;
+  }
+
+  template <typename A, typename... Args>
+  static SpyHashStateImpl combine(SpyHashStateImpl s, const A& a,
+                                  const Args&... args) {
+    // Pass an instance of SpyHashStateImpl<A> when trying to combine `A`. This
+    // allows us to test that the user only uses this instance for combine calls
+    // and does not call AbslHashValue directly.
+    // See AbslHashValue implementation at the bottom.
+    s = SpyHashStateImpl<A>::HashStateBase::combine(std::move(s), a);
+    return SpyHashStateImpl::combine(std::move(s), args...);
+  }
+  static SpyHashStateImpl combine(SpyHashStateImpl s) {
+    if (direct_absl_hash_value_error_) {
+      *s.error_ = "AbslHashValue should not be invoked directly.";
+    } else if (s.moved_from_) {
+      *s.error_ = "Used moved-from instance of the hash state object.";
+    }
+    return s;
+  }
+
+  static void SetDirectAbslHashValueError() {
+    direct_absl_hash_value_error_ = true;
+  }
+
+  // Two SpyHashStateImpl objects are equal if they hold equal hash
+  // representations.
+  friend bool operator==(const SpyHashStateImpl& lhs,
+                         const SpyHashStateImpl& rhs) {
+    return lhs.hash_representation_ == rhs.hash_representation_;
+  }
+
+  friend bool operator!=(const SpyHashStateImpl& lhs,
+                         const SpyHashStateImpl& rhs) {
+    return !(lhs == rhs);
+  }
+
+  enum class CompareResult {
+    kEqual,
+    kASuffixB,
+    kBSuffixA,
+    kUnequal,
+  };
+
+  static CompareResult Compare(const SpyHashStateImpl& a,
+                               const SpyHashStateImpl& b) {
+    const std::string a_flat = absl::StrJoin(a.hash_representation_, "");
+    const std::string b_flat = absl::StrJoin(b.hash_representation_, "");
+    if (a_flat == b_flat) return CompareResult::kEqual;
+    if (absl::EndsWith(a_flat, b_flat)) return CompareResult::kBSuffixA;
+    if (absl::EndsWith(b_flat, a_flat)) return CompareResult::kASuffixB;
+    return CompareResult::kUnequal;
+  }
+
+  // operator<< prints the hash representation as a hex and ASCII dump, to
+  // facilitate debugging.
+  friend std::ostream& operator<<(std::ostream& out,
+                                  const SpyHashStateImpl& hash_state) {
+    out << "[\n";
+    for (auto& s : hash_state.hash_representation_) {
+      size_t offset = 0;
+      for (char c : s) {
+        if (offset % 16 == 0) {
+          out << absl::StreamFormat("\n0x%04x: ", offset);
+        }
+        if (offset % 2 == 0) {
+          out << " ";
+        }
+        out << absl::StreamFormat("%02x", c);
+        ++offset;
+      }
+      out << "\n";
+    }
+    return out << "]";
+  }
+
+  // The base case of the combine recursion, which writes raw bytes into the
+  // internal buffer.
+  static SpyHashStateImpl combine_contiguous(SpyHashStateImpl hash_state,
+                                             const unsigned char* begin,
+                                             size_t size) {
+    hash_state.hash_representation_.emplace_back(
+        reinterpret_cast<const char*>(begin), size);
+    return hash_state;
+  }
+
+  using SpyHashStateImpl::HashStateBase::combine_contiguous;
+
+  absl::optional<std::string> error() const {
+    if (moved_from_) {
+      return "Returned a moved-from instance of the hash state object.";
+    }
+    return *error_;
+  }
+
+ private:
+  template <typename U>
+  friend class SpyHashStateImpl;
+
+  // This is true if SpyHashStateImpl<T> has been passed to a call of
+  // AbslHashValue with the wrong type. This detects that the user called
+  // AbslHashValue directly (because the hash state type does not match).
+  static bool direct_absl_hash_value_error_;
+
+
+  std::vector<std::string> hash_representation_;
+  // This is a shared_ptr because we want all instances of the particular
+  // SpyHashState run to share the field. This way we can set the error for
+  // use-after-move and all the copies will see it.
+  std::shared_ptr<absl::optional<std::string>> error_;
+  bool moved_from_ = false;
+};
+
+template <typename T>
+bool SpyHashStateImpl<T>::direct_absl_hash_value_error_;
+
+template <bool& B>
+struct OdrUse {
+  constexpr OdrUse() {}
+  bool& b = B;
+};
+
+template <void (*)()>
+struct RunOnStartup {
+  static bool run;
+  static constexpr OdrUse<run> kOdrUse{};
+};
+
+template <void (*f)()>
+bool RunOnStartup<f>::run = (f(), true);
+
+template <
+    typename T, typename U,
+    // Only trigger for when (T != U),
+    absl::enable_if_t<!std::is_same<T, U>::value, int> = 0,
+    // This statement works in two ways:
+    //  - First, it instantiates RunOnStartup and forces the initialization of
+    //    `run`, which set the global variable.
+    //  - Second, it triggers a SFINAE error disabling the overload to prevent
+    //    compile time errors. If we didn't disable the overload we would get
+    //    ambiguous overload errors, which we don't want.
+    int = RunOnStartup<SpyHashStateImpl<T>::SetDirectAbslHashValueError>::run>
+void AbslHashValue(SpyHashStateImpl<T>, const U&);
+
+using SpyHashState = SpyHashStateImpl<void>;
+
+}  // namespace hash_internal
+}  // namespace absl
+
+#endif  // ABSL_HASH_INTERNAL_SPY_HASH_STATE_H_
diff --git a/absl/memory/BUILD.bazel b/absl/memory/BUILD.bazel
index 46f47b1..89a312e 100644
--- a/absl/memory/BUILD.bazel
+++ b/absl/memory/BUILD.bazel
@@ -19,6 +19,7 @@
     "ABSL_DEFAULT_COPTS",
     "ABSL_TEST_COPTS",
     "ABSL_EXCEPTIONS_FLAG",
+    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
 )
 
 package(default_visibility = ["//visibility:public"])
@@ -53,6 +54,7 @@
         "memory_exception_safety_test.cc",
     ],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":memory",
         "//absl/base:exception_safety_testing",
diff --git a/absl/memory/memory.h b/absl/memory/memory.h
index c7caf8b..1eaec0f 100644
--- a/absl/memory/memory.h
+++ b/absl/memory/memory.h
@@ -39,16 +39,30 @@
 // Function Template: WrapUnique()
 // -----------------------------------------------------------------------------
 //
-//  Adopts ownership from a raw pointer and transfers it to the returned
-//  `std::unique_ptr`, whose type is deduced. DO NOT specify the template type T
-//  when calling WrapUnique.
+// Adopts ownership from a raw pointer and transfers it to the returned
+// `std::unique_ptr`, whose type is deduced. Because of this deduction, *do not*
+// specify the template type `T` when calling `WrapUnique`.
 //
 // Example:
 //   X* NewX(int, int);
 //   auto x = WrapUnique(NewX(1, 2));  // 'x' is std::unique_ptr<X>.
 //
-// `absl::WrapUnique` is useful for capturing the output of a raw pointer
-// factory. However, prefer 'absl::make_unique<T>(args...) over
+// The purpose of WrapUnique is to automatically deduce the pointer type. If you
+// wish to make the type explicit, for readability reasons or because you prefer
+// to use a base-class pointer rather than a derived one, just use
+// `std::unique_ptr` directly.
+//
+// Example:
+//   X* Factory(int, int);
+//   auto x = std::unique_ptr<X>(Factory(1, 2));
+//                  - or -
+//   std::unique_ptr<X> x(Factory(1, 2));
+//
+// This has the added advantage of working whether Factory returns a raw
+// pointer or a `std::unique_ptr`.
+//
+// While `absl::WrapUnique` is useful for capturing the output of a raw
+// pointer factory, prefer 'absl::make_unique<T>(args...)' over
 // 'absl::WrapUnique(new T(args...))'.
 //
 //   auto x = WrapUnique(new X(1, 2));  // works, but nonideal.
@@ -641,55 +655,59 @@
 #endif
 
 namespace memory_internal {
-#ifdef ABSL_HAVE_EXCEPTIONS
-template <typename Allocator, typename StorageElement, typename... Args>
-void ConstructStorage(Allocator* alloc, StorageElement* first,
-                      StorageElement* last, const Args&... args) {
-  for (StorageElement* cur = first; cur != last; ++cur) {
+#ifdef ABSL_HAVE_EXCEPTIONS  // ConstructRange
+template <typename Allocator, typename Iterator, typename... Args>
+void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
+                    const Args&... args) {
+  for (Iterator cur = first; cur != last; ++cur) {
     try {
-      std::allocator_traits<Allocator>::construct(*alloc, cur, args...);
+      std::allocator_traits<Allocator>::construct(alloc, cur, args...);
     } catch (...) {
       while (cur != first) {
         --cur;
-        std::allocator_traits<Allocator>::destroy(*alloc, cur);
+        std::allocator_traits<Allocator>::destroy(alloc, cur);
       }
       throw;
     }
   }
 }
-template <typename Allocator, typename StorageElement, typename Iterator>
-void CopyToStorageFromRange(Allocator* alloc, StorageElement* destination,
-                            Iterator first, Iterator last) {
-  for (StorageElement* cur = destination; first != last;
+#else   // ABSL_HAVE_EXCEPTIONS  // ConstructRange
+template <typename Allocator, typename Iterator, typename... Args>
+void ConstructRange(Allocator& alloc, Iterator first, Iterator last,
+                    const Args&... args) {
+  for (; first != last; ++first) {
+    std::allocator_traits<Allocator>::construct(alloc, first, args...);
+  }
+}
+#endif  // ABSL_HAVE_EXCEPTIONS  // ConstructRange
+
+#ifdef ABSL_HAVE_EXCEPTIONS  // CopyRange
+template <typename Allocator, typename Iterator, typename InputIterator>
+void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
+               InputIterator last) {
+  for (Iterator cur = destination; first != last;
        static_cast<void>(++cur), static_cast<void>(++first)) {
     try {
-      std::allocator_traits<Allocator>::construct(*alloc, cur, *first);
+      std::allocator_traits<Allocator>::construct(alloc, cur, *first);
     } catch (...) {
       while (cur != destination) {
         --cur;
-        std::allocator_traits<Allocator>::destroy(*alloc, cur);
+        std::allocator_traits<Allocator>::destroy(alloc, cur);
       }
       throw;
     }
   }
 }
-#else   // ABSL_HAVE_EXCEPTIONS
-template <typename Allocator, typename StorageElement, typename... Args>
-void ConstructStorage(Allocator* alloc, StorageElement* first,
-                      StorageElement* last, const Args&... args) {
-  for (; first != last; ++first) {
-    std::allocator_traits<Allocator>::construct(*alloc, first, args...);
-  }
-}
-template <typename Allocator, typename StorageElement, typename Iterator>
-void CopyToStorageFromRange(Allocator* alloc, StorageElement* destination,
-                            Iterator first, Iterator last) {
+#else   // ABSL_HAVE_EXCEPTIONS  // CopyRange
+template <typename Allocator, typename Iterator, typename InputIterator>
+void CopyRange(Allocator& alloc, Iterator destination, InputIterator first,
+               InputIterator last) {
   for (; first != last;
        static_cast<void>(++destination), static_cast<void>(++first)) {
-    std::allocator_traits<Allocator>::construct(*alloc, destination, *first);
+    std::allocator_traits<Allocator>::construct(alloc, destination, *first);
   }
 }
-#endif  // ABSL_HAVE_EXCEPTIONS
+#endif  // ABSL_HAVE_EXCEPTIONS  // CopyRange
 }  // namespace memory_internal
 }  // namespace absl
 
diff --git a/absl/memory/memory_exception_safety_test.cc b/absl/memory/memory_exception_safety_test.cc
index d1f6e84..00d2b19 100644
--- a/absl/memory/memory_exception_safety_test.cc
+++ b/absl/memory/memory_exception_safety_test.cc
@@ -32,7 +32,7 @@
                     .WithInitialValue(Thrower(kValue))
                     // Ensures make_unique does not modify the input. The real
                     // test, though, is ConstructorTracker checking for leaks.
-                    .WithInvariants(testing::strong_guarantee);
+                    .WithContracts(testing::strong_guarantee);
 
   EXPECT_TRUE(tester.Test([](Thrower* thrower) {
     static_cast<void>(absl::make_unique<Thrower>(*thrower));
diff --git a/absl/memory/memory_test.cc b/absl/memory/memory_test.cc
index dee9b48..2e453e2 100644
--- a/absl/memory/memory_test.cc
+++ b/absl/memory/memory_test.cc
@@ -149,7 +149,6 @@
 }
 
 #if 0
-// TODO(billydonahue): Make a proper NC test.
 // These tests shouldn't compile.
 TEST(MakeUniqueTestNC, AcceptMoveOnlyLvalue) {
   auto m = MoveOnly();
diff --git a/absl/meta/type_traits.h b/absl/meta/type_traits.h
index 457b890..23ebd6e 100644
--- a/absl/meta/type_traits.h
+++ b/absl/meta/type_traits.h
@@ -105,8 +105,25 @@
 struct is_detected_convertible
     : is_detected_convertible_impl<void, To, Op, Args...>::type {};
 
+template <typename T>
+using IsCopyAssignableImpl =
+    decltype(std::declval<T&>() = std::declval<const T&>());
+
+template <typename T>
+using IsMoveAssignableImpl = decltype(std::declval<T&>() = std::declval<T&&>());
+
 }  // namespace type_traits_internal
 
+template <typename T>
+struct is_copy_assignable : type_traits_internal::is_detected<
+                                type_traits_internal::IsCopyAssignableImpl, T> {
+};
+
+template <typename T>
+struct is_move_assignable : type_traits_internal::is_detected<
+                                type_traits_internal::IsMoveAssignableImpl, T> {
+};
+
 // void_t()
 //
 // Ignores the type of any its arguments and returns `void`. In general, this
@@ -309,7 +326,7 @@
 struct is_trivially_copy_assignable
     : std::integral_constant<
           bool, __has_trivial_assign(typename std::remove_reference<T>::type) &&
-                    std::is_copy_assignable<T>::value> {
+                    absl::is_copy_assignable<T>::value> {
 #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE
  private:
   static constexpr bool compliant =
@@ -409,11 +426,11 @@
     : absl::conjunction<std::is_default_constructible<std::hash<Key>>,
                         std::is_copy_constructible<std::hash<Key>>,
                         std::is_destructible<std::hash<Key>>,
-                        std::is_copy_assignable<std::hash<Key>>,
+                        absl::is_copy_assignable<std::hash<Key>>,
                         IsHashable<Key>> {};
+
 }  // namespace type_traits_internal
 
 }  // namespace absl
 
-
 #endif  // ABSL_META_TYPE_TRAITS_H_
diff --git a/absl/meta/type_traits_test.cc b/absl/meta/type_traits_test.cc
index 81b4bd3..f51f5de 100644
--- a/absl/meta/type_traits_test.cc
+++ b/absl/meta/type_traits_test.cc
@@ -877,4 +877,80 @@
   EXPECT_EQ(TypeEnum::D, GetTypeExt(Wrap<TypeD>()));
 }
 
+template <typename T>
+bool TestCopyAssign() {
+  return absl::is_copy_assignable<T>::value ==
+         std::is_copy_assignable<T>::value;
+}
+
+TEST(TypeTraitsTest, IsCopyAssignable) {
+  EXPECT_TRUE(TestCopyAssign<int>());
+  EXPECT_TRUE(TestCopyAssign<int&>());
+  EXPECT_TRUE(TestCopyAssign<int&&>());
+
+  struct S {};
+  EXPECT_TRUE(TestCopyAssign<S>());
+  EXPECT_TRUE(TestCopyAssign<S&>());
+  EXPECT_TRUE(TestCopyAssign<S&&>());
+
+  class C {
+   public:
+    explicit C(C* c) : c_(c) {}
+    ~C() { delete c_; }
+
+   private:
+    C* c_;
+  };
+  EXPECT_TRUE(TestCopyAssign<C>());
+  EXPECT_TRUE(TestCopyAssign<C&>());
+  EXPECT_TRUE(TestCopyAssign<C&&>());
+
+  // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases
+#ifndef _LIBCPP_VERSION
+  EXPECT_TRUE(TestCopyAssign<int()>());
+  EXPECT_TRUE(TestCopyAssign<int(int) const>());
+  EXPECT_TRUE(TestCopyAssign<int(...) volatile&>());
+  EXPECT_TRUE(TestCopyAssign<int(int, ...) const volatile&&>());
+#endif  // _LIBCPP_VERSION
+}
+
+template <typename T>
+bool TestMoveAssign() {
+  return absl::is_move_assignable<T>::value ==
+         std::is_move_assignable<T>::value;
+}
+
+TEST(TypeTraitsTest, IsMoveAssignable) {
+  EXPECT_TRUE(TestMoveAssign<int>());
+  EXPECT_TRUE(TestMoveAssign<int&>());
+  EXPECT_TRUE(TestMoveAssign<int&&>());
+
+  struct S {};
+  EXPECT_TRUE(TestMoveAssign<S>());
+  EXPECT_TRUE(TestMoveAssign<S&>());
+  EXPECT_TRUE(TestMoveAssign<S&&>());
+
+  class C {
+   public:
+    explicit C(C* c) : c_(c) {}
+    ~C() { delete c_; }
+    void operator=(const C&) = delete;
+    void operator=(C&&) = delete;
+
+   private:
+    C* c_;
+  };
+  EXPECT_TRUE(TestMoveAssign<C>());
+  EXPECT_TRUE(TestMoveAssign<C&>());
+  EXPECT_TRUE(TestMoveAssign<C&&>());
+
+  // Reason for ifndef: add_lvalue_reference<T> in libc++ breaks for these cases
+#ifndef _LIBCPP_VERSION
+  EXPECT_TRUE(TestMoveAssign<int()>());
+  EXPECT_TRUE(TestMoveAssign<int(int) const>());
+  EXPECT_TRUE(TestMoveAssign<int(...) volatile&>());
+  EXPECT_TRUE(TestMoveAssign<int(int, ...) const volatile&&>());
+#endif  // _LIBCPP_VERSION
+}
+
 }  // namespace
diff --git a/absl/numeric/BUILD.bazel b/absl/numeric/BUILD.bazel
index f49571e..324ce66 100644
--- a/absl/numeric/BUILD.bazel
+++ b/absl/numeric/BUILD.bazel
@@ -49,6 +49,7 @@
         ":int128",
         "//absl/base",
         "//absl/base:core_headers",
+        "//absl/hash:hash_testing",
         "//absl/meta:type_traits",
         "@com_google_googletest//:gtest_main",
     ],
diff --git a/absl/numeric/int128.h b/absl/numeric/int128.h
index 2d131b8..79b62a7 100644
--- a/absl/numeric/int128.h
+++ b/absl/numeric/int128.h
@@ -192,6 +192,12 @@
   // Returns the highest value for a 128-bit unsigned integer.
   friend constexpr uint128 Uint128Max();
 
+  // Support for absl::Hash.
+  template <typename H>
+  friend H AbslHashValue(H h, uint128 v) {
+    return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
+  }
+
  private:
   constexpr uint128(uint64_t high, uint64_t low);
 
diff --git a/absl/numeric/int128_test.cc b/absl/numeric/int128_test.cc
index 1eb3e0e..dfe3475 100644
--- a/absl/numeric/int128_test.cc
+++ b/absl/numeric/int128_test.cc
@@ -23,6 +23,7 @@
 
 #include "gtest/gtest.h"
 #include "absl/base/internal/cycleclock.h"
+#include "absl/hash/hash_testing.h"
 #include "absl/meta/type_traits.h"
 
 #if defined(_MSC_VER) && _MSC_VER == 1900
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel
index 3a5f133..6d7c261 100644
--- a/absl/strings/BUILD.bazel
+++ b/absl/strings/BUILD.bazel
@@ -19,6 +19,7 @@
     "ABSL_DEFAULT_COPTS",
     "ABSL_TEST_COPTS",
     "ABSL_EXCEPTIONS_FLAG",
+    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
 )
 
 package(
@@ -69,6 +70,7 @@
     deps = [
         ":internal",
         "//absl/base",
+        "//absl/base:bits",
         "//absl/base:config",
         "//absl/base:core_headers",
         "//absl/base:endian",
@@ -86,7 +88,6 @@
         "internal/utf8.cc",
     ],
     hdrs = [
-        "internal/bits.h",
         "internal/char_map.h",
         "internal/ostringstream.h",
         "internal/resize_uninitialized.h",
@@ -212,7 +213,6 @@
     visibility = ["//visibility:private"],
     deps = [
         ":internal",
-        ":strings",
         "//absl/base:core_headers",
         "@com_google_googletest//:gtest_main",
     ],
@@ -237,6 +237,7 @@
     size = "small",
     srcs = ["string_view_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     visibility = ["//visibility:private"],
     deps = [
         ":strings",
@@ -373,7 +374,6 @@
     visibility = ["//visibility:private"],
     deps = [
         ":strings",
-        "//absl/memory",
         "@com_github_google_benchmark//:benchmark_main",
     ],
 )
diff --git a/absl/strings/BUILD.gn b/absl/strings/BUILD.gn
index 9a8a10d..20af83c 100644
--- a/absl/strings/BUILD.gn
+++ b/absl/strings/BUILD.gn
@@ -59,6 +59,7 @@
   deps = [
     ":internal",
     "../base",
+    "../base:bits",
     "../base:config",
     "../base:core_headers",
     "../base:endian",
@@ -81,7 +82,6 @@
     "internal/utf8.cc",
   ]
   public = [
-    "internal/bits.h",
     "internal/char_map.h",
     "internal/ostringstream.h",
     "internal/resize_uninitialized.h",
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index cd12213..f3e4162 100644
--- a/absl/strings/CMakeLists.txt
+++ b/absl/strings/CMakeLists.txt
@@ -32,7 +32,6 @@
 
 
 list(APPEND STRINGS_INTERNAL_HEADERS
-  "internal/bits.h"
   "internal/char_map.h"
   "internal/charconv_bigint.h"
   "internal/charconv_parse.h"
diff --git a/absl/strings/ascii.h b/absl/strings/ascii.h
index 96a6454..48a9da2 100644
--- a/absl/strings/ascii.h
+++ b/absl/strings/ascii.h
@@ -165,7 +165,7 @@
 // Converts the characters in `s` to lowercase, changing the contents of `s`.
 void AsciiStrToLower(std::string* s);
 
-// Creates a lowercase std::string from a given absl::string_view.
+// Creates a lowercase string from a given absl::string_view.
 ABSL_MUST_USE_RESULT inline std::string AsciiStrToLower(absl::string_view s) {
   std::string result(s);
   absl::AsciiStrToLower(&result);
@@ -183,7 +183,7 @@
 // Converts the characters in `s` to uppercase, changing the contents of `s`.
 void AsciiStrToUpper(std::string* s);
 
-// Creates an uppercase std::string from a given absl::string_view.
+// Creates an uppercase string from a given absl::string_view.
 ABSL_MUST_USE_RESULT inline std::string AsciiStrToUpper(absl::string_view s) {
   std::string result(s);
   absl::AsciiStrToUpper(&result);
@@ -195,10 +195,10 @@
 ABSL_MUST_USE_RESULT inline absl::string_view StripLeadingAsciiWhitespace(
     absl::string_view str) {
   auto it = std::find_if_not(str.begin(), str.end(), absl::ascii_isspace);
-  return absl::string_view(it, str.end() - it);
+  return str.substr(it - str.begin());
 }
 
-// Strips in place whitespace from the beginning of the given std::string.
+// Strips in place whitespace from the beginning of the given string.
 inline void StripLeadingAsciiWhitespace(std::string* str) {
   auto it = std::find_if_not(str->begin(), str->end(), absl::ascii_isspace);
   str->erase(str->begin(), it);
@@ -209,10 +209,10 @@
 ABSL_MUST_USE_RESULT inline absl::string_view StripTrailingAsciiWhitespace(
     absl::string_view str) {
   auto it = std::find_if_not(str.rbegin(), str.rend(), absl::ascii_isspace);
-  return absl::string_view(str.begin(), str.rend() - it);
+  return str.substr(0, str.rend() - it);
 }
 
-// Strips in place whitespace from the end of the given std::string
+// Strips in place whitespace from the end of the given string
 inline void StripTrailingAsciiWhitespace(std::string* str) {
   auto it = std::find_if_not(str->rbegin(), str->rend(), absl::ascii_isspace);
   str->erase(str->rend() - it);
@@ -225,7 +225,7 @@
   return StripTrailingAsciiWhitespace(StripLeadingAsciiWhitespace(str));
 }
 
-// Strips in place whitespace from both ends of the given std::string
+// Strips in place whitespace from both ends of the given string
 inline void StripAsciiWhitespace(std::string* str) {
   StripTrailingAsciiWhitespace(str);
   StripLeadingAsciiWhitespace(str);
diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc
index 08c3947..c7b8c98 100644
--- a/absl/strings/charconv.cc
+++ b/absl/strings/charconv.cc
@@ -20,8 +20,8 @@
 #include <cstring>
 
 #include "absl/base/casts.h"
+#include "absl/base/internal/bits.h"
 #include "absl/numeric/int128.h"
-#include "absl/strings/internal/bits.h"
 #include "absl/strings/internal/charconv_bigint.h"
 #include "absl/strings/internal/charconv_parse.h"
 
@@ -243,9 +243,9 @@
 // minus the number of leading zero bits.)
 int BitWidth(uint128 value) {
   if (Uint128High64(value) == 0) {
-    return 64 - strings_internal::CountLeadingZeros64(Uint128Low64(value));
+    return 64 - base_internal::CountLeadingZeros64(Uint128Low64(value));
   }
-  return 128 - strings_internal::CountLeadingZeros64(Uint128High64(value));
+  return 128 - base_internal::CountLeadingZeros64(Uint128High64(value));
 }
 
 // Calculates how far to the right a mantissa needs to be shifted to create a
@@ -518,7 +518,7 @@
     const strings_internal::ParsedFloat& parsed_hex) {
   uint64_t mantissa = parsed_hex.mantissa;
   int exponent = parsed_hex.exponent;
-  int mantissa_width = 64 - strings_internal::CountLeadingZeros64(mantissa);
+  int mantissa_width = 64 - base_internal::CountLeadingZeros64(mantissa);
   const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
   bool result_exact;
   exponent += shift;
diff --git a/absl/strings/charconv.h b/absl/strings/charconv.h
index 3e31367..0735382 100644
--- a/absl/strings/charconv.h
+++ b/absl/strings/charconv.h
@@ -22,7 +22,7 @@
 // Workalike compatibilty version of std::chars_format from C++17.
 //
 // This is an bitfield enumerator which can be passed to absl::from_chars to
-// configure the std::string-to-float conversion.
+// configure the string-to-float conversion.
 enum class chars_format {
   scientific = 1,
   fixed = 2,
@@ -30,7 +30,7 @@
   general = fixed | scientific,
 };
 
-// The return result of a std::string-to-number conversion.
+// The return result of a string-to-number conversion.
 //
 // `ec` will be set to `invalid_argument` if a well-formed number was not found
 // at the start of the input range, `result_out_of_range` if a well-formed
@@ -67,7 +67,7 @@
 // If `fmt` is set, it must be one of the enumerator values of the chars_format.
 // (This is despite the fact that chars_format is a bitmask type.)  If set to
 // `scientific`, a matching number must contain an exponent.  If set to `fixed`,
-// then an exponent will never match.  (For example, the std::string "1e5" will be
+// then an exponent will never match.  (For example, the string "1e5" will be
 // parsed as "1".)  If set to `hex`, then a hexadecimal float is parsed in the
 // format that strtod() accepts, except that a "0x" prefix is NOT matched.
 // (In particular, in `hex` mode, the input "0xff" results in the largest
diff --git a/absl/strings/charconv_test.cc b/absl/strings/charconv_test.cc
index f8d71cc..89418fe 100644
--- a/absl/strings/charconv_test.cc
+++ b/absl/strings/charconv_test.cc
@@ -33,7 +33,7 @@
 
 #if ABSL_COMPILER_DOES_EXACT_ROUNDING
 
-// Tests that the given std::string is accepted by absl::from_chars, and that it
+// Tests that the given string is accepted by absl::from_chars, and that it
 // converts exactly equal to the given number.
 void TestDoubleParse(absl::string_view str, double expected_number) {
   SCOPED_TRACE(str);
@@ -250,7 +250,7 @@
   EXPECT_EQ(ToFloat("459926601011.e15"), ldexpf(12466336, 65));
 }
 
-// Common test logic for converting a std::string which lies exactly halfway between
+// Common test logic for converting a string which lies exactly halfway between
 // two target floats.
 //
 // mantissa and exponent represent the precise value between two floating point
@@ -655,7 +655,7 @@
 // is correct for in-bounds values, and that overflow and underflow are done
 // correctly for out-of-bounds values.
 //
-// input_generator maps from an integer index to a std::string to test.
+// input_generator maps from an integer index to a string to test.
 // expected_generator maps from an integer index to an expected Float value.
 // from_chars conversion of input_generator(i) should result in
 // expected_generator(i).
diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc
index fbc9f75..8d8b00b 100644
--- a/absl/strings/escaping.cc
+++ b/absl/strings/escaping.cc
@@ -89,7 +89,7 @@
 //
 //    Unescapes C escape sequences and is the reverse of CEscape().
 //
-//    If 'source' is valid, stores the unescaped std::string and its size in
+//    If 'source' is valid, stores the unescaped string and its size in
 //    'dest' and 'dest_len' respectively, and returns true. Otherwise
 //    returns false and optionally stores the error description in
 //    'error'. Set 'error' to nullptr to disable error reporting.
@@ -104,7 +104,7 @@
                        char* dest, ptrdiff_t* dest_len, std::string* error) {
   char* d = dest;
   const char* p = source.data();
-  const char* end = source.end();
+  const char* end = p + source.size();
   const char* last_byte = end - 1;
 
   // Small optimization for case where source = dest and there's no escaping
@@ -294,7 +294,7 @@
 // ----------------------------------------------------------------------
 // CUnescapeInternal()
 //
-//    Same as above but uses a C++ std::string for output. 'source' and 'dest'
+//    Same as above but uses a C++ string for output. 'source' and 'dest'
 //    may be the same.
 // ----------------------------------------------------------------------
 bool CUnescapeInternal(absl::string_view source, bool leave_nulls_escaped,
@@ -304,7 +304,7 @@
   ptrdiff_t dest_size;
   if (!CUnescapeInternal(source,
                          leave_nulls_escaped,
-                         const_cast<char*>(dest->data()),
+                         &(*dest)[0],
                          &dest_size,
                          error)) {
     return false;
@@ -684,7 +684,7 @@
 // The arrays below were generated by the following code
 // #include <sys/time.h>
 // #include <stdlib.h>
-// #include <std::string.h>
+// #include <string.h>
 // main()
 // {
 //   static const char Base64[] =
@@ -939,7 +939,8 @@
 constexpr char kWebSafeBase64Chars[] =
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
 
-void Base64EscapeInternal(const unsigned char* src, size_t szsrc, std::string* dest,
+template <typename String>
+void Base64EscapeInternal(const unsigned char* src, size_t szsrc, String* dest,
                           bool do_padding, const char* base64_chars) {
   const size_t calc_escaped_size =
       CalculateBase64EscapedLenInternal(szsrc, do_padding);
@@ -951,7 +952,8 @@
   dest->erase(escaped_len);
 }
 
-bool Base64UnescapeInternal(const char* src, size_t slen, std::string* dest,
+template <typename String>
+bool Base64UnescapeInternal(const char* src, size_t slen, String* dest,
                             const signed char* unbase64) {
   // Determine the size of the output std::string.  Base64 encodes every 3 bytes into
   // 4 characters.  any leftover chars are added directly for good measure.
@@ -999,7 +1001,7 @@
 /* clang-format on */
 
 // This is a templated function so that T can be either a char*
-// or a std::string.  This works because we use the [] operator to access
+// or a string.  This works because we use the [] operator to access
 // individual characters at a time.
 template <typename T>
 void HexStringToBytesInternal(const char* from, T to, ptrdiff_t num) {
@@ -1009,7 +1011,7 @@
   }
 }
 
-// This is a templated function so that T can be either a char* or a std::string.
+// This is a templated function so that T can be either a char* or a string.
 template <typename T>
 void BytesToHexStringInternal(const unsigned char* src, T dest, ptrdiff_t num) {
   auto dest_ptr = &dest[0];
diff --git a/absl/strings/escaping.h b/absl/strings/escaping.h
index 7f1ab96..2965973 100644
--- a/absl/strings/escaping.h
+++ b/absl/strings/escaping.h
@@ -17,7 +17,7 @@
 // File: escaping.h
 // -----------------------------------------------------------------------------
 //
-// This header file contains std::string utilities involved in escaping and
+// This header file contains string utilities involved in escaping and
 // unescaping strings in various ways.
 //
 
@@ -37,7 +37,7 @@
 
 // CUnescape()
 //
-// Unescapes a `source` std::string and copies it into `dest`, rewriting C-style
+// Unescapes a `source` string and copies it into `dest`, rewriting C-style
 // escape sequences (http://en.cppreference.com/w/cpp/language/escape) into
 // their proper code point equivalents, returning `true` if successful.
 //
@@ -57,9 +57,10 @@
 //     0x99).
 //
 //
-// If any errors are encountered, this function returns `false` and stores the
-// first encountered error in `error`. To disable error reporting, set `error`
-// to `nullptr` or use the overload with no error reporting below.
+// If any errors are encountered, this function returns `false`, leaving the
+// `dest` output parameter in an unspecified state, and stores the first
+// encountered error in `error`. To disable error reporting, set `error` to
+// `nullptr` or use the overload with no error reporting below.
 //
 // Example:
 //
@@ -78,7 +79,7 @@
 
 // CEscape()
 //
-// Escapes a 'src' std::string using C-style escapes sequences
+// Escapes a 'src' string using C-style escapes sequences
 // (http://en.cppreference.com/w/cpp/language/escape), escaping other
 // non-printable/non-whitespace bytes as octal sequences (e.g. "\377").
 //
@@ -91,7 +92,7 @@
 
 // CHexEscape()
 //
-// Escapes a 'src' std::string using C-style escape sequences, escaping
+// Escapes a 'src' string using C-style escape sequences, escaping
 // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g.
 // "\xFF").
 //
@@ -104,7 +105,7 @@
 
 // Utf8SafeCEscape()
 //
-// Escapes a 'src' std::string using C-style escape sequences, escaping bytes as
+// Escapes a 'src' string using C-style escape sequences, escaping bytes as
 // octal sequences, and passing through UTF-8 characters without conversion.
 // I.e., when encountering any bytes with their high bit set, this function
 // will not escape those values, whether or not they are valid UTF-8.
@@ -112,47 +113,47 @@
 
 // Utf8SafeCHexEscape()
 //
-// Escapes a 'src' std::string using C-style escape sequences, escaping bytes as
+// Escapes a 'src' string using C-style escape sequences, escaping bytes as
 // hexadecimal sequences, and passing through UTF-8 characters without
 // conversion.
 std::string Utf8SafeCHexEscape(absl::string_view src);
 
 // Base64Unescape()
 //
-// Converts a `src` std::string encoded in Base64 to its binary equivalent, writing
+// Converts a `src` string encoded in Base64 to its binary equivalent, writing
 // it to a `dest` buffer, returning `true` on success. If `src` contains invalid
 // characters, `dest` is cleared and returns `false`.
 bool Base64Unescape(absl::string_view src, std::string* dest);
 
-// WebSafeBase64Unescape(absl::string_view, std::string*)
+// WebSafeBase64Unescape()
 //
-// Converts a `src` std::string encoded in Base64 to its binary equivalent, writing
+// Converts a `src` string encoded in Base64 to its binary equivalent, writing
 // it to a `dest` buffer, but using '-' instead of '+', and '_' instead of '/'.
 // If `src` contains invalid characters, `dest` is cleared and returns `false`.
 bool WebSafeBase64Unescape(absl::string_view src, std::string* dest);
 
 // Base64Escape()
 //
-// Encodes a `src` std::string into a `dest` buffer using base64 encoding, with
+// Encodes a `src` string into a `dest` buffer using base64 encoding, with
 // padding characters. This function conforms with RFC 4648 section 4 (base64).
 void Base64Escape(absl::string_view src, std::string* dest);
 
 // WebSafeBase64Escape()
 //
-// Encodes a `src` std::string into a `dest` buffer using '-' instead of '+' and
+// Encodes a `src` string into a `dest` buffer using '-' instead of '+' and
 // '_' instead of '/', and without padding. This function conforms with RFC 4648
 // section 5 (base64url).
 void WebSafeBase64Escape(absl::string_view src, std::string* dest);
 
 // HexStringToBytes()
 //
-// Converts an ASCII hex std::string into bytes, returning binary data of length
+// Converts an ASCII hex string into bytes, returning binary data of length
 // `from.size()/2`.
 std::string HexStringToBytes(absl::string_view from);
 
 // BytesToHexString()
 //
-// Converts binary data into an ASCII text std::string, returning a std::string of size
+// Converts binary data into an ASCII text string, returning a string of size
 // `2*from.size()`.
 std::string BytesToHexString(absl::string_view from);
 
diff --git a/absl/strings/escaping_test.cc b/absl/strings/escaping_test.cc
index 3f65ec1..9dc27f3 100644
--- a/absl/strings/escaping_test.cc
+++ b/absl/strings/escaping_test.cc
@@ -543,18 +543,19 @@
     {"abcdefghijklmnopqrstuvwxyz", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo="},
 };
 
-TEST(Base64, EscapeAndUnescape) {
+template <typename StringType>
+void TestEscapeAndUnescape() {
   // Check the short strings; this tests the math (and boundaries)
   for (const auto& tc : base64_tests) {
-    std::string encoded("this junk should be ignored");
+    StringType encoded("this junk should be ignored");
     absl::Base64Escape(tc.plaintext, &encoded);
     EXPECT_EQ(encoded, tc.cyphertext);
 
-    std::string decoded("this junk should be ignored");
+    StringType decoded("this junk should be ignored");
     EXPECT_TRUE(absl::Base64Unescape(encoded, &decoded));
     EXPECT_EQ(decoded, tc.plaintext);
 
-    std::string websafe(tc.cyphertext);
+    StringType websafe(tc.cyphertext);
     for (int c = 0; c < websafe.size(); ++c) {
       if ('+' == websafe[c]) websafe[c] = '-';
       if ('/' == websafe[c]) websafe[c] = '_';
@@ -576,7 +577,7 @@
 
   // Now try the long strings, this tests the streaming
   for (const auto& tc : absl::strings_internal::base64_strings()) {
-    std::string buffer;
+    StringType buffer;
     absl::WebSafeBase64Escape(tc.plaintext, &buffer);
     EXPECT_EQ(tc.cyphertext, buffer);
   }
@@ -586,7 +587,7 @@
     absl::string_view data_set[] = {"ab-/", absl::string_view("\0bcd", 4),
                                     absl::string_view("abc.\0", 5)};
     for (absl::string_view bad_data : data_set) {
-      std::string buf;
+      StringType buf;
       EXPECT_FALSE(absl::Base64Unescape(bad_data, &buf));
       EXPECT_FALSE(absl::WebSafeBase64Unescape(bad_data, &buf));
       EXPECT_TRUE(buf.empty());
@@ -594,6 +595,10 @@
   }
 }
 
+TEST(Base64, EscapeAndUnescape) {
+  TestEscapeAndUnescape<std::string>();
+}
+
 TEST(Base64, DISABLED_HugeData) {
   const size_t kSize = size_t(3) * 1000 * 1000 * 1000;
   static_assert(kSize % 3 == 0, "kSize must be divisible by 3");
diff --git a/absl/strings/internal/bits.h b/absl/strings/internal/bits.h
deleted file mode 100644
index 901082c..0000000
--- a/absl/strings/internal/bits.h
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2018 The Abseil Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#ifndef ABSL_STRINGS_INTERNAL_BITS_H_
-#define ABSL_STRINGS_INTERNAL_BITS_H_
-
-#include <cstdint>
-
-#if defined(_MSC_VER) && defined(_M_X64)
-#include <intrin.h>
-#pragma intrinsic(_BitScanReverse64)
-#endif
-
-namespace absl {
-namespace strings_internal {
-
-// Returns the number of leading 0 bits in a 64-bit value.
-inline int CountLeadingZeros64(uint64_t n) {
-#if defined(__GNUC__)
-  static_assert(sizeof(unsigned long long) == sizeof(n),  // NOLINT(runtime/int)
-                "__builtin_clzll does not take 64bit arg");
-  return n == 0 ? 64 : __builtin_clzll(n);
-#elif defined(_MSC_VER) && defined(_M_X64)
-  unsigned long result;  // NOLINT(runtime/int)
-  if (_BitScanReverse64(&result, n)) {
-    return 63 - result;
-  }
-  return 64;
-#else
-  int zeroes = 60;
-  if (n >> 32) zeroes -= 32, n >>= 32;
-  if (n >> 16) zeroes -= 16, n >>= 16;
-  if (n >> 8) zeroes -= 8, n >>= 8;
-  if (n >> 4) zeroes -= 4, n >>= 4;
-  return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0\0"[n] + zeroes;
-#endif
-}
-
-}  // namespace strings_internal
-}  // namespace absl
-
-#endif  // ABSL_STRINGS_INTERNAL_BITS_H_
diff --git a/absl/strings/internal/charconv_parse.cc b/absl/strings/internal/charconv_parse.cc
index a04cc67..7e4dabc 100644
--- a/absl/strings/internal/charconv_parse.cc
+++ b/absl/strings/internal/charconv_parse.cc
@@ -91,7 +91,7 @@
 
 // To avoid incredibly large inputs causing integer overflow for our exponent,
 // we impose an arbitrary but very large limit on the number of significant
-// digits we will accept.  The implementation refuses to match a std::string with
+// digits we will accept.  The implementation refuses to match a string with
 // more consecutive significant mantissa digits than this.
 constexpr int kDecimalDigitLimit = 50000000;
 
diff --git a/absl/strings/internal/charconv_parse_test.cc b/absl/strings/internal/charconv_parse_test.cc
index 1ff8600..f48b9ae 100644
--- a/absl/strings/internal/charconv_parse_test.cc
+++ b/absl/strings/internal/charconv_parse_test.cc
@@ -29,16 +29,16 @@
 
 namespace {
 
-// Check that a given std::string input is parsed to the expected mantissa and
+// Check that a given string input is parsed to the expected mantissa and
 // exponent.
 //
-// Input std::string `s` must contain a '$' character.  It marks the end of the
+// Input string `s` must contain a '$' character.  It marks the end of the
 // characters that should be consumed by the match.  It is stripped from the
 // input to ParseFloat.
 //
-// If input std::string `s` contains '[' and ']' characters, these mark the region
+// If input string `s` contains '[' and ']' characters, these mark the region
 // of characters that should be marked as the "subrange".  For NaNs, this is
-// the location of the extended NaN std::string.  For numbers, this is the location
+// the location of the extended NaN string.  For numbers, this is the location
 // of the full, over-large mantissa.
 template <int base>
 void ExpectParsedFloat(std::string s, absl::chars_format format_flags,
@@ -92,10 +92,10 @@
   EXPECT_EQ(characters_matched, expected_characters_matched);
 }
 
-// Check that a given std::string input is parsed to the expected mantissa and
+// Check that a given string input is parsed to the expected mantissa and
 // exponent.
 //
-// Input std::string `s` must contain a '$' character.  It marks the end of the
+// Input string `s` must contain a '$' character.  It marks the end of the
 // characters that were consumed by the match.
 template <int base>
 void ExpectNumber(std::string s, absl::chars_format format_flags,
@@ -106,7 +106,7 @@
                           expected_literal_exponent);
 }
 
-// Check that a given std::string input is parsed to the given special value.
+// Check that a given string input is parsed to the given special value.
 //
 // This tests against both number bases, since infinities and NaNs have
 // identical representations in both modes.
@@ -116,7 +116,7 @@
   ExpectParsedFloat<16>(s, format_flags, type, 0, 0);
 }
 
-// Check that a given input std::string is not matched by Float.
+// Check that a given input string is not matched by Float.
 template <int base>
 void ExpectFailedParse(absl::string_view s, absl::chars_format format_flags) {
   ParsedFloat parsed =
diff --git a/absl/strings/internal/memutil.h b/absl/strings/internal/memutil.h
index a6f1c69..7de383b 100644
--- a/absl/strings/internal/memutil.h
+++ b/absl/strings/internal/memutil.h
@@ -14,7 +14,7 @@
 // limitations under the License.
 //
 
-// These routines provide mem versions of standard C std::string routines,
+// These routines provide mem versions of standard C string routines,
 // such as strpbrk.  They function exactly the same as the str versions,
 // so if you wonder what they are, replace the word "mem" by
 // "str" and check out the man page.  I could return void*, as the
@@ -22,14 +22,14 @@
 // since this is by far the most common way these functions are called.
 //
 // The difference between the mem and str versions is the mem version
-// takes a pointer and a length, rather than a '\0'-terminated std::string.
+// takes a pointer and a length, rather than a '\0'-terminated string.
 // The memcase* routines defined here assume the locale is "C"
 // (they use absl::ascii_tolower instead of tolower).
 //
 // These routines are based on the BSD library.
 //
-// Here's a list of routines from std::string.h, and their mem analogues.
-// Functions in lowercase are defined in std::string.h; those in UPPERCASE
+// Here's a list of routines from string.h, and their mem analogues.
+// Functions in lowercase are defined in string.h; those in UPPERCASE
 // are defined here:
 //
 // strlen                  --
diff --git a/absl/strings/internal/ostringstream.h b/absl/strings/internal/ostringstream.h
index 6e1325b..e81a89a 100644
--- a/absl/strings/internal/ostringstream.h
+++ b/absl/strings/internal/ostringstream.h
@@ -25,18 +25,18 @@
 namespace absl {
 namespace strings_internal {
 
-// The same as std::ostringstream but appends to a user-specified std::string,
+// The same as std::ostringstream but appends to a user-specified string,
 // and is faster. It is ~70% faster to create, ~50% faster to write to, and
-// completely free to extract the result std::string.
+// completely free to extract the result string.
 //
-//   std::string s;
+//   string s;
 //   OStringStream strm(&s);
 //   strm << 42 << ' ' << 3.14;  // appends to `s`
 //
 // The stream object doesn't have to be named. Starting from C++11 operator<<
 // works with rvalues of std::ostream.
 //
-//   std::string s;
+//   string s;
 //   OStringStream(&s) << 42 << ' ' << 3.14;  // appends to `s`
 //
 // OStringStream is faster to create than std::ostringstream but it's still
@@ -45,14 +45,14 @@
 //
 // Creates unnecessary instances of OStringStream: slow.
 //
-//   std::string s;
+//   string s;
 //   OStringStream(&s) << 42;
 //   OStringStream(&s) << ' ';
 //   OStringStream(&s) << 3.14;
 //
 // Creates a single instance of OStringStream and reuses it: fast.
 //
-//   std::string s;
+//   string s;
 //   OStringStream strm(&s);
 //   strm << 42;
 //   strm << ' ';
diff --git a/absl/strings/internal/resize_uninitialized.h b/absl/strings/internal/resize_uninitialized.h
index 0157ca0..a94e054 100644
--- a/absl/strings/internal/resize_uninitialized.h
+++ b/absl/strings/internal/resize_uninitialized.h
@@ -44,8 +44,8 @@
   s->resize(new_size);
 }
 
-// Returns true if the std::string implementation supports a resize where
-// the new characters added to the std::string are left untouched.
+// Returns true if the string implementation supports a resize where
+// the new characters added to the string are left untouched.
 //
 // (A better name might be "STLStringSupportsUninitializedResize", alluding to
 // the previous function.)
@@ -57,7 +57,7 @@
 // Like str->resize(new_size), except any new characters added to "*str" as a
 // result of resizing may be left uninitialized, rather than being filled with
 // '0' bytes. Typically used when code is then going to overwrite the backing
-// store of the std::string with known data. Uses a Google extension to std::string.
+// store of the string with known data. Uses a Google extension to ::string.
 template <typename string_type, typename = void>
 inline void STLStringResizeUninitialized(string_type* s, size_t new_size) {
   ResizeUninit(s, new_size, HasResizeUninitialized<string_type>());
diff --git a/absl/strings/internal/str_format/arg.cc b/absl/strings/internal/str_format/arg.cc
index eafb068..b40be8f 100644
--- a/absl/strings/internal/str_format/arg.cc
+++ b/absl/strings/internal/str_format/arg.cc
@@ -112,7 +112,7 @@
 // Note: 'o' conversions do not have a base indicator, it's just that
 // the '#' flag is specified to modify the precision for 'o' conversions.
 string_view BaseIndicator(const ConvertedIntInfo &info,
-                          const ConversionSpec &conv) {
+                          const ConversionSpec conv) {
   bool alt = conv.flags().alt;
   int radix = conv.conv().radix();
   if (conv.conv().id() == ConversionChar::p)
@@ -127,7 +127,7 @@
   return {};
 }
 
-string_view SignColumn(bool neg, const ConversionSpec &conv) {
+string_view SignColumn(bool neg, const ConversionSpec conv) {
   if (conv.conv().is_signed()) {
     if (neg) return "-";
     if (conv.flags().show_pos) return "+";
@@ -136,7 +136,7 @@
   return {};
 }
 
-bool ConvertCharImpl(unsigned char v, const ConversionSpec &conv,
+bool ConvertCharImpl(unsigned char v, const ConversionSpec conv,
                      FormatSinkImpl *sink) {
   size_t fill = 0;
   if (conv.width() >= 0) fill = conv.width();
@@ -148,7 +148,7 @@
 }
 
 bool ConvertIntImplInner(const ConvertedIntInfo &info,
-                         const ConversionSpec &conv, FormatSinkImpl *sink) {
+                         const ConversionSpec conv, FormatSinkImpl *sink) {
   // Print as a sequence of Substrings:
   //   [left_spaces][sign][base_indicator][zeroes][formatted][right_spaces]
   size_t fill = 0;
@@ -202,8 +202,7 @@
 }
 
 template <typename T>
-bool ConvertIntImplInner(T v, const ConversionSpec &conv,
-                         FormatSinkImpl *sink) {
+bool ConvertIntImplInner(T v, const ConversionSpec conv, FormatSinkImpl *sink) {
   ConvertedIntInfo info(v, conv.conv());
   if (conv.flags().basic && conv.conv().id() != ConversionChar::p) {
     if (info.is_neg()) sink->Append(1, '-');
@@ -218,7 +217,7 @@
 }
 
 template <typename T>
-bool ConvertIntArg(T v, const ConversionSpec &conv, FormatSinkImpl *sink) {
+bool ConvertIntArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) {
   if (conv.conv().is_float()) {
     return FormatConvertImpl(static_cast<double>(v), conv, sink).value;
   }
@@ -234,11 +233,11 @@
 }
 
 template <typename T>
-bool ConvertFloatArg(T v, const ConversionSpec &conv, FormatSinkImpl *sink) {
+bool ConvertFloatArg(T v, const ConversionSpec conv, FormatSinkImpl *sink) {
   return conv.conv().is_float() && ConvertFloatImpl(v, conv, sink);
 }
 
-inline bool ConvertStringArg(string_view v, const ConversionSpec &conv,
+inline bool ConvertStringArg(string_view v, const ConversionSpec conv,
                              FormatSinkImpl *sink) {
   if (conv.conv().id() != ConversionChar::s)
     return false;
@@ -254,19 +253,19 @@
 
 // ==================== Strings ====================
 ConvertResult<Conv::s> FormatConvertImpl(const std::string &v,
-                                         const ConversionSpec &conv,
+                                         const ConversionSpec conv,
                                          FormatSinkImpl *sink) {
   return {ConvertStringArg(v, conv, sink)};
 }
 
 ConvertResult<Conv::s> FormatConvertImpl(string_view v,
-                                         const ConversionSpec &conv,
+                                         const ConversionSpec conv,
                                          FormatSinkImpl *sink) {
   return {ConvertStringArg(v, conv, sink)};
 }
 
 ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char *v,
-                                                   const ConversionSpec &conv,
+                                                   const ConversionSpec conv,
                                                    FormatSinkImpl *sink) {
   if (conv.conv().id() == ConversionChar::p)
     return {FormatConvertImpl(VoidPtr(v), conv, sink).value};
@@ -283,7 +282,7 @@
 }
 
 // ==================== Raw pointers ====================
-ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, const ConversionSpec &conv,
+ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, const ConversionSpec conv,
                                          FormatSinkImpl *sink) {
   if (conv.conv().id() != ConversionChar::p)
     return {false};
@@ -295,104 +294,83 @@
 }
 
 // ==================== Floats ====================
-FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec &conv,
+FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertFloatArg(v, conv, sink)};
 }
-FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec &conv,
+FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertFloatArg(v, conv, sink)};
 }
 FloatingConvertResult FormatConvertImpl(long double v,
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertFloatArg(v, conv, sink)};
 }
 
 // ==================== Chars ====================
-IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec &conv,
+IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(signed char v,
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(unsigned char v,
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 
 // ==================== Ints ====================
 IntegralConvertResult FormatConvertImpl(short v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(unsigned short v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
-IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec &conv,
+IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
-IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec &conv,
+IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(long v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(unsigned long v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(long long v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(unsigned long long v,  // NOLINT
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 IntegralConvertResult FormatConvertImpl(absl::uint128 v,
-                                        const ConversionSpec &conv,
+                                        const ConversionSpec conv,
                                         FormatSinkImpl *sink) {
   return {ConvertIntArg(v, conv, sink)};
 }
 
-template struct FormatArgImpl::TypedVTable<str_format_internal::VoidPtr>;
+ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_();
 
-template struct FormatArgImpl::TypedVTable<bool>;
-template struct FormatArgImpl::TypedVTable<char>;
-template struct FormatArgImpl::TypedVTable<signed char>;
-template struct FormatArgImpl::TypedVTable<unsigned char>;
-template struct FormatArgImpl::TypedVTable<short>;           // NOLINT
-template struct FormatArgImpl::TypedVTable<unsigned short>;  // NOLINT
-template struct FormatArgImpl::TypedVTable<int>;
-template struct FormatArgImpl::TypedVTable<unsigned>;
-template struct FormatArgImpl::TypedVTable<long>;                // NOLINT
-template struct FormatArgImpl::TypedVTable<unsigned long>;       // NOLINT
-template struct FormatArgImpl::TypedVTable<long long>;           // NOLINT
-template struct FormatArgImpl::TypedVTable<unsigned long long>;  // NOLINT
-template struct FormatArgImpl::TypedVTable<absl::uint128>;
-
-template struct FormatArgImpl::TypedVTable<float>;
-template struct FormatArgImpl::TypedVTable<double>;
-template struct FormatArgImpl::TypedVTable<long double>;
-
-template struct FormatArgImpl::TypedVTable<const char *>;
-template struct FormatArgImpl::TypedVTable<std::string>;
-template struct FormatArgImpl::TypedVTable<string_view>;
 
 }  // namespace str_format_internal
 
diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h
index a956218..3376d48 100644
--- a/absl/strings/internal/str_format/arg.h
+++ b/absl/strings/internal/str_format/arg.h
@@ -33,7 +33,7 @@
 template <typename T>
 struct HasUserDefinedConvert<
     T, void_t<decltype(AbslFormatConvert(
-           std::declval<const T&>(), std::declval<const ConversionSpec&>(),
+           std::declval<const T&>(), std::declval<ConversionSpec>(),
            std::declval<FormatSink*>()))>> : std::true_type {};
 template <typename T>
 class StreamedWrapper;
@@ -50,25 +50,23 @@
       : value(ptr ? reinterpret_cast<uintptr_t>(ptr) : 0) {}
   uintptr_t value;
 };
-ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, const ConversionSpec& conv,
+ConvertResult<Conv::p> FormatConvertImpl(VoidPtr v, ConversionSpec conv,
                                          FormatSinkImpl* sink);
 
 // Strings.
-ConvertResult<Conv::s> FormatConvertImpl(const std::string& v,
-                                         const ConversionSpec& conv,
+ConvertResult<Conv::s> FormatConvertImpl(const std::string& v, ConversionSpec conv,
                                          FormatSinkImpl* sink);
-ConvertResult<Conv::s> FormatConvertImpl(string_view v,
-                                         const ConversionSpec& conv,
+ConvertResult<Conv::s> FormatConvertImpl(string_view v, ConversionSpec conv,
                                          FormatSinkImpl* sink);
 ConvertResult<Conv::s | Conv::p> FormatConvertImpl(const char* v,
-                                                   const ConversionSpec& conv,
+                                                   ConversionSpec conv,
                                                    FormatSinkImpl* sink);
 template <class AbslCord,
           typename std::enable_if<
               std::is_same<AbslCord, ::Cord>::value>::type* = nullptr,
           class AbslCordReader = ::CordReader>
 ConvertResult<Conv::s> FormatConvertImpl(const AbslCord& value,
-                                         const ConversionSpec& conv,
+                                         ConversionSpec conv,
                                          FormatSinkImpl* sink) {
   if (conv.conv().id() != ConversionChar::s) return {false};
 
@@ -104,51 +102,48 @@
 using FloatingConvertResult = ConvertResult<Conv::floating>;
 
 // Floats.
-FloatingConvertResult FormatConvertImpl(float v, const ConversionSpec& conv,
+FloatingConvertResult FormatConvertImpl(float v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
-FloatingConvertResult FormatConvertImpl(double v, const ConversionSpec& conv,
+FloatingConvertResult FormatConvertImpl(double v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
-FloatingConvertResult FormatConvertImpl(long double v,
-                                        const ConversionSpec& conv,
+FloatingConvertResult FormatConvertImpl(long double v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
 
 // Chars.
-IntegralConvertResult FormatConvertImpl(char v, const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(char v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
-IntegralConvertResult FormatConvertImpl(signed char v,
-                                        const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(signed char v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
-IntegralConvertResult FormatConvertImpl(unsigned char v,
-                                        const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(unsigned char v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
 
 // Ints.
 IntegralConvertResult FormatConvertImpl(short v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
 IntegralConvertResult FormatConvertImpl(unsigned short v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
-IntegralConvertResult FormatConvertImpl(int v, const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(int v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
-IntegralConvertResult FormatConvertImpl(unsigned v, const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(unsigned v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
 IntegralConvertResult FormatConvertImpl(long v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
 IntegralConvertResult FormatConvertImpl(unsigned long v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
 IntegralConvertResult FormatConvertImpl(long long v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
 IntegralConvertResult FormatConvertImpl(unsigned long long v,  // NOLINT
-                                        const ConversionSpec& conv,
+                                        ConversionSpec conv,
                                         FormatSinkImpl* sink);
-IntegralConvertResult FormatConvertImpl(uint128 v, const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(uint128 v, ConversionSpec conv,
                                         FormatSinkImpl* sink);
 template <typename T, enable_if_t<std::is_same<T, bool>::value, int> = 0>
-IntegralConvertResult FormatConvertImpl(T v, const ConversionSpec& conv,
+IntegralConvertResult FormatConvertImpl(T v, ConversionSpec conv,
                                         FormatSinkImpl* sink) {
   return FormatConvertImpl(static_cast<int>(v), conv, sink);
 }
@@ -159,11 +154,11 @@
 typename std::enable_if<std::is_enum<T>::value &&
                             !HasUserDefinedConvert<T>::value,
                         IntegralConvertResult>::type
-FormatConvertImpl(T v, const ConversionSpec& conv, FormatSinkImpl* sink);
+FormatConvertImpl(T v, ConversionSpec conv, FormatSinkImpl* sink);
 
 template <typename T>
 ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<T>& v,
-                                         const ConversionSpec& conv,
+                                         ConversionSpec conv,
                                          FormatSinkImpl* out) {
   std::ostringstream oss;
   oss << v.v_;
@@ -176,7 +171,7 @@
 struct FormatCountCaptureHelper {
   template <class T = int>
   static ConvertResult<Conv::n> ConvertHelper(const FormatCountCapture& v,
-                                              const ConversionSpec& conv,
+                                              ConversionSpec conv,
                                               FormatSinkImpl* sink) {
     const absl::enable_if_t<sizeof(T) != 0, FormatCountCapture>& v2 = v;
 
@@ -189,7 +184,7 @@
 
 template <class T = int>
 ConvertResult<Conv::n> FormatConvertImpl(const FormatCountCapture& v,
-                                         const ConversionSpec& conv,
+                                         ConversionSpec conv,
                                          FormatSinkImpl* sink) {
   return FormatCountCaptureHelper::ConvertHelper(v, conv, sink);
 }
@@ -199,20 +194,20 @@
 struct FormatArgImplFriend {
   template <typename Arg>
   static bool ToInt(Arg arg, int* out) {
-    if (!arg.vtbl_->to_int) return false;
-    *out = arg.vtbl_->to_int(arg.data_);
-    return true;
+    // A value initialized ConversionSpec has a `none` conv, which tells the
+    // dispatcher to run the `int` conversion.
+    return arg.dispatcher_(arg.data_, {}, out);
   }
 
   template <typename Arg>
-  static bool Convert(Arg arg, const str_format_internal::ConversionSpec& conv,
+  static bool Convert(Arg arg, str_format_internal::ConversionSpec conv,
                       FormatSinkImpl* out) {
-    return arg.vtbl_->convert(arg.data_, conv, out);
+    return arg.dispatcher_(arg.data_, conv, out);
   }
 
   template <typename Arg>
-  static const void* GetVTablePtrForTest(Arg arg) {
-    return arg.vtbl_;
+  static typename Arg::Dispatcher GetVTablePtrForTest(Arg arg) {
+    return arg.dispatcher_;
   }
 };
 
@@ -229,11 +224,7 @@
     char buf[kInlinedSpace];
   };
 
-  struct VTable {
-    bool (*convert)(Data, const str_format_internal::ConversionSpec& conv,
-                    FormatSinkImpl* out);
-    int (*to_int)(Data);
-  };
+  using Dispatcher = bool (*)(Data, ConversionSpec, void* out);
 
   template <typename T>
   struct store_by_value
@@ -253,10 +244,6 @@
                                                                 : ByPointer))> {
   };
 
-  // An instance of an FormatArgImpl::VTable suitable for 'T'.
-  template <typename T>
-  struct TypedVTable;
-
   // To reduce the number of vtables we will decay values before hand.
   // Anything with a user-defined Convert will get its own vtable.
   // For everything else:
@@ -338,7 +325,10 @@
   };
 
   template <typename T>
-  void Init(const T& value);
+  void Init(const T& value) {
+    data_ = Manager<T>::SetValue(value);
+    dispatcher_ = &Dispatch<T>;
+  }
 
   template <typename T>
   static int ToIntVal(const T& val) {
@@ -355,79 +345,75 @@
     return static_cast<int>(val);
   }
 
-  Data data_;
-  const VTable* vtbl_;
-};
+  template <typename T>
+  static bool ToInt(Data arg, int* out, std::true_type /* is_integral */,
+                    std::false_type) {
+    *out = ToIntVal(Manager<T>::Value(arg));
+    return true;
+  }
 
-template <typename T>
-struct FormatArgImpl::TypedVTable {
- private:
-  static bool ConvertImpl(Data arg,
-                          const str_format_internal::ConversionSpec& conv,
-                          FormatSinkImpl* out) {
-    return str_format_internal::FormatConvertImpl(Manager<T>::Value(arg), conv,
-                                                  out)
+  template <typename T>
+  static bool ToInt(Data arg, int* out, std::false_type,
+                    std::true_type /* is_enum */) {
+    *out = ToIntVal(static_cast<typename std::underlying_type<T>::type>(
+        Manager<T>::Value(arg)));
+    return true;
+  }
+
+  template <typename T>
+  static bool ToInt(Data, int*, std::false_type, std::false_type) {
+    return false;
+  }
+
+  template <typename T>
+  static bool Dispatch(Data arg, ConversionSpec spec, void* out) {
+    // A `none` conv indicates that we want the `int` conversion.
+    if (ABSL_PREDICT_FALSE(spec.conv().id() == ConversionChar::none)) {
+      return ToInt<T>(arg, static_cast<int*>(out), std::is_integral<T>(),
+                      std::is_enum<T>());
+    }
+
+    return str_format_internal::FormatConvertImpl(
+               Manager<T>::Value(arg), spec, static_cast<FormatSinkImpl*>(out))
         .value;
   }
 
-  template <typename U = T, typename = void>
-  struct ToIntImpl {
-    static constexpr int (*value)(Data) = nullptr;
-  };
-
-  template <typename U>
-  struct ToIntImpl<U,
-                   typename std::enable_if<std::is_integral<U>::value>::type> {
-    static int Invoke(Data arg) { return ToIntVal(Manager<T>::Value(arg)); }
-    static constexpr int (*value)(Data) = &Invoke;
-  };
-
-  template <typename U>
-  struct ToIntImpl<U, typename std::enable_if<std::is_enum<U>::value>::type> {
-    static int Invoke(Data arg) {
-      return ToIntVal(static_cast<typename std::underlying_type<T>::type>(
-          Manager<T>::Value(arg)));
-    }
-    static constexpr int (*value)(Data) = &Invoke;
-  };
-
- public:
-  static constexpr VTable value{&ConvertImpl, ToIntImpl<>::value};
+  Data data_;
+  Dispatcher dispatcher_;
 };
 
-template <typename T>
-constexpr FormatArgImpl::VTable FormatArgImpl::TypedVTable<T>::value;
+#define ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(T, E) \
+  E template bool FormatArgImpl::Dispatch<T>(Data, ConversionSpec, void*)
 
-template <typename T>
-void FormatArgImpl::Init(const T& value) {
-  data_ = Manager<T>::SetValue(value);
-  vtbl_ = &TypedVTable<T>::value;
-}
+#define ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(...)                   \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(str_format_internal::VoidPtr,     \
+                                             __VA_ARGS__);                     \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(bool, __VA_ARGS__);               \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(char, __VA_ARGS__);               \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(signed char, __VA_ARGS__);        \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned char, __VA_ARGS__);      \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(short, __VA_ARGS__); /* NOLINT */ \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned short,      /* NOLINT */ \
+                                             __VA_ARGS__);                     \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(int, __VA_ARGS__);                \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned int, __VA_ARGS__);       \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long, __VA_ARGS__); /* NOLINT */  \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long,      /* NOLINT */  \
+                                             __VA_ARGS__);                     \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long long, /* NOLINT */           \
+                                             __VA_ARGS__);                     \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(unsigned long long, /* NOLINT */  \
+                                             __VA_ARGS__);                     \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(uint128, __VA_ARGS__);            \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(float, __VA_ARGS__);              \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(double, __VA_ARGS__);             \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(long double, __VA_ARGS__);        \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(const char*, __VA_ARGS__);        \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(std::string, __VA_ARGS__);             \
+  ABSL_INTERNAL_FORMAT_DISPATCH_INSTANTIATE_(string_view, __VA_ARGS__)
 
-extern template struct FormatArgImpl::TypedVTable<str_format_internal::VoidPtr>;
+ABSL_INTERNAL_FORMAT_DISPATCH_OVERLOADS_EXPAND_(extern);
 
-extern template struct FormatArgImpl::TypedVTable<bool>;
-extern template struct FormatArgImpl::TypedVTable<char>;
-extern template struct FormatArgImpl::TypedVTable<signed char>;
-extern template struct FormatArgImpl::TypedVTable<unsigned char>;
-extern template struct FormatArgImpl::TypedVTable<short>;           // NOLINT
-extern template struct FormatArgImpl::TypedVTable<unsigned short>;  // NOLINT
-extern template struct FormatArgImpl::TypedVTable<int>;
-extern template struct FormatArgImpl::TypedVTable<unsigned>;
-extern template struct FormatArgImpl::TypedVTable<long>;           // NOLINT
-extern template struct FormatArgImpl::TypedVTable<unsigned long>;  // NOLINT
-extern template struct FormatArgImpl::TypedVTable<long long>;      // NOLINT
-extern template struct FormatArgImpl::TypedVTable<
-    unsigned long long>;  // NOLINT
-extern template struct FormatArgImpl::TypedVTable<uint128>;
-
-extern template struct FormatArgImpl::TypedVTable<float>;
-extern template struct FormatArgImpl::TypedVTable<double>;
-extern template struct FormatArgImpl::TypedVTable<long double>;
-
-extern template struct FormatArgImpl::TypedVTable<const char*>;
-extern template struct FormatArgImpl::TypedVTable<std::string>;
-extern template struct FormatArgImpl::TypedVTable<string_view>;
 }  // namespace str_format_internal
 }  // namespace absl
 
diff --git a/absl/strings/internal/str_format/bind.cc b/absl/strings/internal/str_format/bind.cc
index 33e8641..c4eddd1 100644
--- a/absl/strings/internal/str_format/bind.cc
+++ b/absl/strings/internal/str_format/bind.cc
@@ -103,15 +103,15 @@
 };
 
 template <typename Converter>
-bool ConvertAll(const UntypedFormatSpecImpl& format,
-                absl::Span<const FormatArgImpl> args,
-                const Converter& converter) {
-  const ParsedFormatBase* pc = format.parsed_conversion();
-  if (pc)
-    return pc->ProcessFormat(ConverterConsumer<Converter>(converter, args));
-
-  return ParseFormatString(format.str(),
-                           ConverterConsumer<Converter>(converter, args));
+bool ConvertAll(const UntypedFormatSpecImpl format,
+                absl::Span<const FormatArgImpl> args, Converter converter) {
+  if (format.has_parsed_conversion()) {
+    return format.parsed_conversion()->ProcessFormat(
+        ConverterConsumer<Converter>(converter, args));
+  } else {
+    return ParseFormatString(format.str(),
+                             ConverterConsumer<Converter>(converter, args));
+  }
 }
 
 class DefaultConverter {
@@ -158,7 +158,7 @@
   return ArgContext(pack).Bind(props, bound);
 }
 
-std::string Summarize(const UntypedFormatSpecImpl& format,
+std::string Summarize(const UntypedFormatSpecImpl format,
                  absl::Span<const FormatArgImpl> args) {
   typedef SummarizingConverter Converter;
   std::string out;
@@ -167,23 +167,18 @@
     // flush.
     FormatSinkImpl sink(&out);
     if (!ConvertAll(format, args, Converter(&sink))) {
-      sink.Flush();
-      out.clear();
+      return "";
     }
   }
   return out;
 }
 
 bool FormatUntyped(FormatRawSinkImpl raw_sink,
-                   const UntypedFormatSpecImpl& format,
+                   const UntypedFormatSpecImpl format,
                    absl::Span<const FormatArgImpl> args) {
   FormatSinkImpl sink(raw_sink);
   using Converter = DefaultConverter;
-  if (!ConvertAll(format, args, Converter(&sink))) {
-    sink.Flush();
-    return false;
-  }
-  return true;
+  return ConvertAll(format, args, Converter(&sink));
 }
 
 std::ostream& Streamable::Print(std::ostream& os) const {
@@ -191,14 +186,16 @@
   return os;
 }
 
-std::string& AppendPack(std::string* out, const UntypedFormatSpecImpl& format,
+std::string& AppendPack(std::string* out, const UntypedFormatSpecImpl format,
                    absl::Span<const FormatArgImpl> args) {
   size_t orig = out->size();
-  if (!FormatUntyped(out, format, args)) out->resize(orig);
+  if (ABSL_PREDICT_FALSE(!FormatUntyped(out, format, args))) {
+    out->erase(orig);
+  }
   return *out;
 }
 
-int FprintF(std::FILE* output, const UntypedFormatSpecImpl& format,
+int FprintF(std::FILE* output, const UntypedFormatSpecImpl format,
             absl::Span<const FormatArgImpl> args) {
   FILERawSink sink(output);
   if (!FormatUntyped(&sink, format, args)) {
@@ -216,7 +213,7 @@
   return static_cast<int>(sink.count());
 }
 
-int SnprintF(char* output, size_t size, const UntypedFormatSpecImpl& format,
+int SnprintF(char* output, size_t size, const UntypedFormatSpecImpl format,
              absl::Span<const FormatArgImpl> args) {
   BufferRawSink sink(output, size ? size - 1 : 0);
   if (!FormatUntyped(&sink, format, args)) {
diff --git a/absl/strings/internal/str_format/bind.h b/absl/strings/internal/str_format/bind.h
index 4008611..1b52df9 100644
--- a/absl/strings/internal/str_format/bind.h
+++ b/absl/strings/internal/str_format/bind.h
@@ -33,13 +33,21 @@
  public:
   UntypedFormatSpecImpl() = delete;
 
-  explicit UntypedFormatSpecImpl(string_view s) : str_(s), pc_() {}
+  explicit UntypedFormatSpecImpl(string_view s)
+      : data_(s.data()), size_(s.size()) {}
   explicit UntypedFormatSpecImpl(
       const str_format_internal::ParsedFormatBase* pc)
-      : pc_(pc) {}
-  string_view str() const { return str_; }
+      : data_(pc), size_(~size_t{}) {}
+
+  bool has_parsed_conversion() const { return size_ == ~size_t{}; }
+
+  string_view str() const {
+    assert(!has_parsed_conversion());
+    return string_view(static_cast<const char*>(data_), size_);
+  }
   const str_format_internal::ParsedFormatBase* parsed_conversion() const {
-    return pc_;
+    assert(has_parsed_conversion());
+    return static_cast<const str_format_internal::ParsedFormatBase*>(data_);
   }
 
   template <typename T>
@@ -48,8 +56,8 @@
   }
 
  private:
-  string_view str_;
-  const str_format_internal::ParsedFormatBase* pc_;
+  const void* data_;
+  size_t size_;
 };
 
 template <typename T, typename...>
@@ -144,31 +152,31 @@
 };
 
 // for testing
-std::string Summarize(const UntypedFormatSpecImpl& format,
+std::string Summarize(UntypedFormatSpecImpl format,
                  absl::Span<const FormatArgImpl> args);
 bool BindWithPack(const UnboundConversion* props,
                   absl::Span<const FormatArgImpl> pack, BoundConversion* bound);
 
 bool FormatUntyped(FormatRawSinkImpl raw_sink,
-                   const UntypedFormatSpecImpl& format,
+                   UntypedFormatSpecImpl format,
                    absl::Span<const FormatArgImpl> args);
 
-std::string& AppendPack(std::string* out, const UntypedFormatSpecImpl& format,
+std::string& AppendPack(std::string* out, UntypedFormatSpecImpl format,
                    absl::Span<const FormatArgImpl> args);
 
-inline std::string FormatPack(const UntypedFormatSpecImpl& format,
+inline std::string FormatPack(const UntypedFormatSpecImpl format,
                          absl::Span<const FormatArgImpl> args) {
   std::string out;
   AppendPack(&out, format, args);
   return out;
 }
 
-int FprintF(std::FILE* output, const UntypedFormatSpecImpl& format,
+int FprintF(std::FILE* output, UntypedFormatSpecImpl format,
             absl::Span<const FormatArgImpl> args);
-int SnprintF(char* output, size_t size, const UntypedFormatSpecImpl& format,
+int SnprintF(char* output, size_t size, UntypedFormatSpecImpl format,
              absl::Span<const FormatArgImpl> args);
 
-// Returned by Streamed(v). Converts via '%s' to the std::string created
+// Returned by Streamed(v). Converts via '%s' to the string created
 // by std::ostream << v.
 template <typename T>
 class StreamedWrapper {
@@ -178,7 +186,7 @@
  private:
   template <typename S>
   friend ConvertResult<Conv::s> FormatConvertImpl(const StreamedWrapper<S>& v,
-                                                  const ConversionSpec& conv,
+                                                  ConversionSpec conv,
                                                   FormatSinkImpl* out);
   const T& v_;
 };
diff --git a/absl/strings/internal/str_format/extension.h b/absl/strings/internal/str_format/extension.h
index 810330b..11b996a 100644
--- a/absl/strings/internal/str_format/extension.h
+++ b/absl/strings/internal/str_format/extension.h
@@ -18,6 +18,7 @@
 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_EXTENSION_H_
 
 #include <limits.h>
+#include <cstddef>
 #include <cstring>
 #include <ostream>
 
@@ -57,7 +58,7 @@
   void (*write_)(void*, string_view);
 };
 
-// An abstraction to which conversions write their std::string data.
+// An abstraction to which conversions write their string data.
 class FormatSinkImpl {
  public:
   explicit FormatSinkImpl(FormatRawSinkImpl raw) : raw_(raw) {}
@@ -307,7 +308,12 @@
  public:
   Flags flags() const { return flags_; }
   LengthMod length_mod() const { return length_mod_; }
-  ConversionChar conv() const { return conv_; }
+  ConversionChar conv() const {
+    // Keep this field first in the struct . It generates better code when
+    // accessing it when ConversionSpec is passed by value in registers.
+    static_assert(offsetof(ConversionSpec, conv_) == 0, "");
+    return conv_;
+  }
 
   // Returns the specified width. If width is unspecfied, it returns a negative
   // value.
@@ -324,9 +330,9 @@
   void set_left(bool b) { flags_.left = b; }
 
  private:
+  ConversionChar conv_;
   Flags flags_;
   LengthMod length_mod_;
-  ConversionChar conv_;
   int width_;
   int precision_;
 };
diff --git a/absl/strings/internal/str_format/output.h b/absl/strings/internal/str_format/output.h
index 3b0aa5e..12ecd99 100644
--- a/absl/strings/internal/str_format/output.h
+++ b/absl/strings/internal/str_format/output.h
@@ -69,10 +69,10 @@
 
 // Provide RawSink integration with common types from the STL.
 inline void AbslFormatFlush(std::string* out, string_view s) {
-  out->append(s.begin(), s.size());
+  out->append(s.data(), s.size());
 }
 inline void AbslFormatFlush(std::ostream* out, string_view s) {
-  out->write(s.begin(), s.size());
+  out->write(s.data(), s.size());
 }
 
 template <class AbslCord, typename = typename std::enable_if<
diff --git a/absl/strings/internal/str_format/parser.cc b/absl/strings/internal/str_format/parser.cc
index 10114f4..5e3d0d0 100644
--- a/absl/strings/internal/str_format/parser.cc
+++ b/absl/strings/internal/str_format/parser.cc
@@ -81,19 +81,27 @@
 template <bool is_positional>
 bool ConsumeConversion(string_view *src, UnboundConversion *conv,
                        int *next_arg) {
-  const char *pos = src->begin();
-  const char *const end = src->end();
+  const char *pos = src->data();
+  const char *const end = pos + src->size();
   char c;
-  // Read the next char into `c` and update `pos`. Reads '\0' if at end.
-  const auto get_char = [&] { c = pos == end ? '\0' : *pos++; };
+  // Read the next char into `c` and update `pos`. Returns false if there are
+  // no more chars to read.
+#define ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR()        \
+  do {                                                \
+    if (ABSL_PREDICT_FALSE(pos == end)) return false; \
+    c = *pos++;                                       \
+  } while (0)
 
   const auto parse_digits = [&] {
     int digits = c - '0';
-    // We do not want to overflow `digits` so we consume at most digits10-1
+    // We do not want to overflow `digits` so we consume at most digits10
     // digits. If there are more digits the parsing will fail later on when the
     // digit doesn't match the expected characters.
-    int num_digits = std::numeric_limits<int>::digits10 - 2;
-    for (get_char(); num_digits && std::isdigit(c); get_char()) {
+    int num_digits = std::numeric_limits<int>::digits10;
+    for (;;) {
+      if (ABSL_PREDICT_FALSE(pos == end || !num_digits)) break;
+      c = *pos++;
+      if (!std::isdigit(c)) break;
       --num_digits;
       digits = 10 * digits + c - '0';
     }
@@ -101,14 +109,14 @@
   };
 
   if (is_positional) {
-    get_char();
-    if (c < '1' || c > '9') return false;
+    ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
+    if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
     conv->arg_position = parse_digits();
     assert(conv->arg_position > 0);
-    if (c != '$') return false;
+    if (ABSL_PREDICT_FALSE(c != '$')) return false;
   }
 
-  get_char();
+  ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
 
   // We should start with the basic flag on.
   assert(conv->flags.basic);
@@ -119,32 +127,39 @@
   if (c < 'A') {
     conv->flags.basic = false;
 
-    for (; c <= '0'; get_char()) {
+    for (; c <= '0';) {
+      // FIXME: We might be able to speed this up reusing the kIds lookup table
+      // from above.
+      // It might require changing Flags to be a plain integer where we can |= a
+      // value.
       switch (c) {
         case '-':
           conv->flags.left = true;
-          continue;
+          break;
         case '+':
           conv->flags.show_pos = true;
-          continue;
+          break;
         case ' ':
           conv->flags.sign_col = true;
-          continue;
+          break;
         case '#':
           conv->flags.alt = true;
-          continue;
+          break;
         case '0':
           conv->flags.zero = true;
-          continue;
+          break;
+        default:
+          goto flags_done;
       }
-      break;
+      ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
     }
+flags_done:
 
     if (c <= '9') {
       if (c >= '0') {
         int maybe_width = parse_digits();
         if (!is_positional && c == '$') {
-          if (*next_arg != 0) return false;
+          if (ABSL_PREDICT_FALSE(*next_arg != 0)) return false;
           // Positional conversion.
           *next_arg = -1;
           conv->flags = Flags();
@@ -153,12 +168,12 @@
         }
         conv->width.set_value(maybe_width);
       } else if (c == '*') {
-        get_char();
+        ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
         if (is_positional) {
-          if (c < '1' || c > '9') return false;
+          if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
           conv->width.set_from_arg(parse_digits());
-          if (c != '$') return false;
-          get_char();
+          if (ABSL_PREDICT_FALSE(c != '$')) return false;
+          ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
         } else {
           conv->width.set_from_arg(++*next_arg);
         }
@@ -166,16 +181,16 @@
     }
 
     if (c == '.') {
-      get_char();
+      ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
       if (std::isdigit(c)) {
         conv->precision.set_value(parse_digits());
       } else if (c == '*') {
-        get_char();
+        ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
         if (is_positional) {
-          if (c < '1' || c > '9') return false;
+          if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
           conv->precision.set_from_arg(parse_digits());
           if (c != '$') return false;
-          get_char();
+          ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
         } else {
           conv->precision.set_from_arg(++*next_arg);
         }
@@ -188,23 +203,23 @@
   std::int8_t id = kIds[static_cast<unsigned char>(c)];
 
   if (id < 0) {
-    if (id == none) return false;
+    if (ABSL_PREDICT_FALSE(id == none)) return false;
 
     // It is a length modifier.
     using str_format_internal::LengthMod;
     LengthMod length_mod = LengthMod::FromId(static_cast<LM>(~id));
-    get_char();
+    ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
     if (c == 'h' && length_mod.id() == LengthMod::h) {
       conv->length_mod = LengthMod::FromId(LengthMod::hh);
-      get_char();
+      ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
     } else if (c == 'l' && length_mod.id() == LengthMod::l) {
       conv->length_mod = LengthMod::FromId(LengthMod::ll);
-      get_char();
+      ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
     } else {
       conv->length_mod = length_mod;
     }
     id = kIds[static_cast<unsigned char>(c)];
-    if (id < 0) return false;
+    if (ABSL_PREDICT_FALSE(id < 0)) return false;
   }
 
   assert(CheckFastPathSetting(*conv));
diff --git a/absl/strings/internal/str_format/parser.h b/absl/strings/internal/str_format/parser.h
index 5bebc95..1022f06 100644
--- a/absl/strings/internal/str_format/parser.h
+++ b/absl/strings/internal/str_format/parser.h
@@ -75,14 +75,14 @@
 bool ConsumeUnboundConversion(string_view* src, UnboundConversion* conv,
                               int* next_arg);
 
-// Parse the format std::string provided in 'src' and pass the identified items into
+// Parse the format string provided in 'src' and pass the identified items into
 // 'consumer'.
 // Text runs will be passed by calling
 //   Consumer::Append(string_view);
 // ConversionItems will be passed by calling
 //   Consumer::ConvertOne(UnboundConversion, string_view);
 // In the case of ConvertOne, the string_view that is passed is the
-// portion of the format std::string corresponding to the conversion, not including
+// portion of the format string corresponding to the conversion, not including
 // the leading %. On success, it returns true. On failure, it stops and returns
 // false.
 template <typename Consumer>
@@ -90,7 +90,7 @@
   int next_arg = 0;
   while (!src.empty()) {
     const char* percent =
-        static_cast<const char*>(memchr(src.begin(), '%', src.size()));
+        static_cast<const char*>(memchr(src.data(), '%', src.size()));
     if (!percent) {
       // We found the last substring.
       return consumer.Append(src);
@@ -98,7 +98,7 @@
     // We found a percent, so push the text run then process the percent.
     size_t percent_loc = percent - src.data();
     if (!consumer.Append(string_view(src.data(), percent_loc))) return false;
-    if (percent + 1 >= src.end()) return false;
+    if (percent + 1 >= src.data() + src.size()) return false;
 
     UnboundConversion conv;
 
@@ -178,7 +178,8 @@
     const char* const base = data_.get();
     string_view text(base, 0);
     for (const auto& item : items_) {
-      text = string_view(text.end(), (base + item.text_end) - text.end());
+      const char* const end = text.data() + text.size();
+      text = string_view(end, (base + item.text_end) - end);
       if (item.is_conversion) {
         if (!consumer.ConvertOne(item.conv, text)) return false;
       } else {
@@ -237,7 +238,7 @@
 // This class also supports runtime format checking with the ::New() and
 // ::NewAllowIgnored() factory functions.
 // This is the only API that allows the user to pass a runtime specified format
-// std::string. These factory functions will return NULL if the format does not match
+// string. These factory functions will return NULL if the format does not match
 // the conversions requested by the user.
 template <str_format_internal::Conv... C>
 class ExtendedParsedFormat : public str_format_internal::ParsedFormatBase {
diff --git a/absl/strings/internal/str_format/parser_test.cc b/absl/strings/internal/str_format/parser_test.cc
index e698020..ae40203 100644
--- a/absl/strings/internal/str_format/parser_test.cc
+++ b/absl/strings/internal/str_format/parser_test.cc
@@ -66,10 +66,10 @@
   typedef UnboundConversion Props;
   string_view Consume(string_view* src) {
     int next = 0;
-    const char* prev_begin = src->begin();
+    const char* prev_begin = src->data();
     o = UnboundConversion();  // refresh
     ConsumeUnboundConversion(src, &o, &next);
-    return {prev_begin, static_cast<size_t>(src->begin() - prev_begin)};
+    return {prev_begin, static_cast<size_t>(src->data() - prev_begin)};
   }
 
   bool Run(const char *fmt, bool force_positional = false) {
@@ -84,9 +84,9 @@
 TEST_F(ConsumeUnboundConversionTest, ConsumeSpecification) {
   struct Expectation {
     int line;
-    const char *src;
-    const char *out;
-    const char *src_post;
+    string_view src;
+    string_view out;
+    string_view src_post;
   };
   const Expectation kExpect[] = {
     {__LINE__, "",     "",     ""  },
@@ -236,6 +236,16 @@
   EXPECT_EQ(9, o.precision.get_from_arg());
 
   EXPECT_FALSE(Run(".*0$d")) << "no arg 0";
+
+  // Large values
+  EXPECT_TRUE(Run("999999999.999999999d"));
+  EXPECT_FALSE(o.width.is_from_arg());
+  EXPECT_EQ(999999999, o.width.value());
+  EXPECT_FALSE(o.precision.is_from_arg());
+  EXPECT_EQ(999999999, o.precision.value());
+
+  EXPECT_FALSE(Run("1000000000.999999999d"));
+  EXPECT_FALSE(Run("999999999.1000000000d"));
 }
 
 TEST_F(ConsumeUnboundConversionTest, Flags) {
diff --git a/absl/strings/internal/str_join_internal.h b/absl/strings/internal/str_join_internal.h
index a734758..0058fc8 100644
--- a/absl/strings/internal/str_join_internal.h
+++ b/absl/strings/internal/str_join_internal.h
@@ -189,7 +189,7 @@
 //
 
 // The main joining algorithm. This simply joins the elements in the given
-// iterator range, each separated by the given separator, into an output std::string,
+// iterator range, each separated by the given separator, into an output string,
 // and formats each element using the provided Formatter object.
 template <typename Iterator, typename Formatter>
 std::string JoinAlgorithm(Iterator start, Iterator end, absl::string_view s,
@@ -205,20 +205,20 @@
 }
 
 // A joining algorithm that's optimized for a forward iterator range of
-// std::string-like objects that do not need any additional formatting. This is to
-// optimize the common case of joining, say, a std::vector<std::string> or a
+// string-like objects that do not need any additional formatting. This is to
+// optimize the common case of joining, say, a std::vector<string> or a
 // std::vector<absl::string_view>.
 //
 // This is an overload of the previous JoinAlgorithm() function. Here the
 // Formatter argument is of type NoFormatter. Since NoFormatter is an internal
 // type, this overload is only invoked when strings::Join() is called with a
-// range of std::string-like objects (e.g., std::string, absl::string_view), and an
+// range of string-like objects (e.g., string, absl::string_view), and an
 // explicit Formatter argument was NOT specified.
 //
 // The optimization is that the needed space will be reserved in the output
-// std::string to avoid the need to resize while appending. To do this, the iterator
+// string to avoid the need to resize while appending. To do this, the iterator
 // range will be traversed twice: once to calculate the total needed size, and
-// then again to copy the elements and delimiters to the output std::string.
+// then again to copy the elements and delimiters to the output string.
 template <typename Iterator,
           typename = typename std::enable_if<std::is_convertible<
               typename std::iterator_traits<Iterator>::iterator_category,
diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h
index 9cf0833..81e8d55 100644
--- a/absl/strings/internal/str_split_internal.h
+++ b/absl/strings/internal/str_split_internal.h
@@ -51,7 +51,7 @@
 
 // This class is implicitly constructible from everything that absl::string_view
 // is implicitly constructible from. If it's constructed from a temporary
-// std::string, the data is moved into a data member so its lifetime matches that of
+// string, the data is moved into a data member so its lifetime matches that of
 // the ConvertibleToStringView instance.
 class ConvertibleToStringView {
  public:
@@ -102,7 +102,7 @@
   absl::string_view value_;
 };
 
-// An iterator that enumerates the parts of a std::string from a Splitter. The text
+// An iterator that enumerates the parts of a string from a Splitter. The text
 // to be split, the Delimiter, and the Predicate are all taken from the given
 // Splitter object. Iterators may only be compared if they refer to the same
 // Splitter instance.
@@ -159,7 +159,7 @@
       }
       const absl::string_view text = splitter_->text();
       const absl::string_view d = delimiter_.Find(text, pos_);
-      if (d.data() == text.end()) state_ = kLastState;
+      if (d.data() == text.data() + text.size()) state_ = kLastState;
       curr_ = text.substr(pos_, d.data() - (text.data() + pos_));
       pos_ += curr_.size() + d.size();
     } while (!predicate_(curr_));
diff --git a/absl/strings/match.cc b/absl/strings/match.cc
index 25bd7f0..3d10c57 100644
--- a/absl/strings/match.cc
+++ b/absl/strings/match.cc
@@ -27,6 +27,13 @@
 }
 }  // namespace
 
+bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2) {
+  return (piece1.size() == piece2.size() &&
+          0 == absl::strings_internal::memcasecmp(piece1.data(), piece2.data(),
+                                                  piece1.size()));
+  // memcasecmp uses absl::ascii_tolower().
+}
+
 bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix) {
   return (text.size() >= prefix.size()) &&
          CaseEqual(text.substr(0, prefix.size()), prefix);
diff --git a/absl/strings/match.h b/absl/strings/match.h
index 108b604..6e8ed10 100644
--- a/absl/strings/match.h
+++ b/absl/strings/match.h
@@ -17,7 +17,7 @@
 // File: match.h
 // -----------------------------------------------------------------------------
 //
-// This file contains simple utilities for performing std::string matching checks.
+// This file contains simple utilities for performing string matching checks.
 // All of these function parameters are specified as `absl::string_view`,
 // meaning that these functions can accept `std::string`, `absl::string_view` or
 // nul-terminated C-style strings.
@@ -41,14 +41,14 @@
 
 // StrContains()
 //
-// Returns whether a given std::string `haystack` contains the substring `needle`.
+// Returns whether a given string `haystack` contains the substring `needle`.
 inline bool StrContains(absl::string_view haystack, absl::string_view needle) {
   return haystack.find(needle, 0) != haystack.npos;
 }
 
 // StartsWith()
 //
-// Returns whether a given std::string `text` begins with `prefix`.
+// Returns whether a given string `text` begins with `prefix`.
 inline bool StartsWith(absl::string_view text, absl::string_view prefix) {
   return prefix.empty() ||
          (text.size() >= prefix.size() &&
@@ -57,7 +57,7 @@
 
 // EndsWith()
 //
-// Returns whether a given std::string `text` ends with `suffix`.
+// Returns whether a given string `text` ends with `suffix`.
 inline bool EndsWith(absl::string_view text, absl::string_view suffix) {
   return suffix.empty() ||
          (text.size() >= suffix.size() &&
@@ -66,16 +66,22 @@
          );
 }
 
+// EqualsIgnoreCase()
+//
+// Returns whether given ASCII strings `piece1` and `piece2` are equal, ignoring
+// case in the comparison.
+bool EqualsIgnoreCase(absl::string_view piece1, absl::string_view piece2);
+
 // StartsWithIgnoreCase()
 //
-// Returns whether a given std::string `text` starts with `starts_with`, ignoring
-// case in the comparison.
+// Returns whether a given ASCII string `text` starts with `starts_with`,
+// ignoring case in the comparison.
 bool StartsWithIgnoreCase(absl::string_view text, absl::string_view prefix);
 
 // EndsWithIgnoreCase()
 //
-// Returns whether a given std::string `text` ends with `ends_with`, ignoring case
-// in the comparison.
+// Returns whether a given ASCII string `text` ends with `ends_with`, ignoring
+// case in the comparison.
 bool EndsWithIgnoreCase(absl::string_view text, absl::string_view suffix);
 
 }  // namespace absl
diff --git a/absl/strings/match_test.cc b/absl/strings/match_test.cc
index d194f0e..c21e00b 100644
--- a/absl/strings/match_test.cc
+++ b/absl/strings/match_test.cc
@@ -80,6 +80,17 @@
   EXPECT_FALSE(absl::StrContains(cs, sv2));
 }
 
+TEST(MatchTest, EqualsIgnoreCase) {
+  std::string text = "the";
+  absl::string_view data(text);
+
+  EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The"));
+  EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE"));
+  EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the"));
+  EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick"));
+  EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then"));
+}
+
 TEST(MatchTest, StartsWithIgnoreCase) {
   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo"));
   EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo"));
diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc
index f842ed8..9309f9d 100644
--- a/absl/strings/numbers.cc
+++ b/absl/strings/numbers.cc
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// This file contains std::string processing functions related to
+// This file contains string processing functions related to
 // numeric values.
 
 #include "absl/strings/numbers.h"
@@ -30,10 +30,10 @@
 #include <memory>
 #include <utility>
 
+#include "absl/base/internal/bits.h"
 #include "absl/base/internal/raw_logging.h"
 #include "absl/strings/ascii.h"
 #include "absl/strings/charconv.h"
-#include "absl/strings/internal/bits.h"
 #include "absl/strings/internal/memutil.h"
 #include "absl/strings/str_cat.h"
 
@@ -161,8 +161,8 @@
 // their output to the beginning of the buffer.  The caller is responsible
 // for ensuring that the buffer has enough space to hold the output.
 //
-// Returns a pointer to the end of the std::string (i.e. the null character
-// terminating the std::string).
+// Returns a pointer to the end of the string (i.e. the null character
+// terminating the string).
 // ----------------------------------------------------------------------
 
 namespace {
@@ -344,7 +344,7 @@
   uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95);
   if (bits128_up == 0) return {bits64_127, bits0_63};
 
-  int shift = 64 - strings_internal::CountLeadingZeros64(bits128_up);
+  int shift = 64 - base_internal::CountLeadingZeros64(bits128_up);
   uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift));
   uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift));
   return {hi, lo};
@@ -375,7 +375,7 @@
       5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
       5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
   result = Mul32(result, powers_of_five[expfive & 15]);
-  int shift = strings_internal::CountLeadingZeros64(result.first);
+  int shift = base_internal::CountLeadingZeros64(result.first);
   if (shift != 0) {
     result.first = (result.first << shift) + (result.second >> (64 - shift));
     result.second = (result.second << shift);
diff --git a/absl/strings/numbers.h b/absl/strings/numbers.h
index cf3c597..f9b2cce 100644
--- a/absl/strings/numbers.h
+++ b/absl/strings/numbers.h
@@ -41,32 +41,32 @@
 
 // SimpleAtoi()
 //
-// Converts the given std::string into an integer value, returning `true` if
-// successful. The std::string must reflect a base-10 integer (optionally followed or
+// Converts the given string into an integer value, returning `true` if
+// successful. The string must reflect a base-10 integer (optionally followed or
 // preceded by ASCII whitespace) whose value falls within the range of the
-// integer type,
+// integer type.
 template <typename int_type>
 ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out);
 
 // SimpleAtof()
 //
-// Converts the given std::string (optionally followed or preceded by ASCII
+// Converts the given string (optionally followed or preceded by ASCII
 // whitespace) into a float, which may be rounded on overflow or underflow.
-// See http://en.cppreference.com/w/c/std::string/byte/strtof for details about the
+// See http://en.cppreference.com/w/c/string/byte/strtof for details about the
 // allowed formats for `str`.
 ABSL_MUST_USE_RESULT bool SimpleAtof(absl::string_view str, float* value);
 
 // SimpleAtod()
 //
-// Converts the given std::string (optionally followed or preceded by ASCII
+// Converts the given string (optionally followed or preceded by ASCII
 // whitespace) into a double, which may be rounded on overflow or underflow.
-// See http://en.cppreference.com/w/c/std::string/byte/strtof for details about the
+// See http://en.cppreference.com/w/c/string/byte/strtof for details about the
 // allowed formats for `str`.
 ABSL_MUST_USE_RESULT bool SimpleAtod(absl::string_view str, double* value);
 
 // SimpleAtob()
 //
-// Converts the given std::string into a boolean, returning `true` if successful.
+// Converts the given string into a boolean, returning `true` if successful.
 // The following case-insensitive strings are interpreted as boolean `true`:
 // "true", "t", "yes", "y", "1". The following case-insensitive strings
 // are interpreted as boolean `false`: "false", "f", "no", "n", "0".
@@ -169,9 +169,9 @@
 
 // SimpleAtoi()
 //
-// Converts a std::string to an integer, using `safe_strto?()` functions for actual
+// Converts a string to an integer, using `safe_strto?()` functions for actual
 // parsing, returning `true` if successful. The `safe_strto?()` functions apply
-// strict checking; the std::string must be a base-10 integer, optionally followed or
+// strict checking; the string must be a base-10 integer, optionally followed or
 // preceded by ASCII whitespace, with a value in the range of the corresponding
 // integer type.
 template <typename int_type>
diff --git a/absl/strings/numbers_benchmark.cc b/absl/strings/numbers_benchmark.cc
index 8ef650b..0570b75 100644
--- a/absl/strings/numbers_benchmark.cc
+++ b/absl/strings/numbers_benchmark.cc
@@ -158,7 +158,7 @@
     ->ArgPair(16, 10)
     ->ArgPair(16, 16);
 
-// Returns a vector of `num_strings` strings. Each std::string represents a
+// Returns a vector of `num_strings` strings. Each string represents a
 // floating point number with `num_digits` digits before the decimal point and
 // another `num_digits` digits after.
 std::vector<std::string> MakeFloatStrings(int num_strings, int num_digits) {
diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc
index 27cc047..36fc0d6 100644
--- a/absl/strings/numbers_test.cc
+++ b/absl/strings/numbers_test.cc
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// This file tests std::string processing functions related to numeric values.
+// This file tests string processing functions related to numeric values.
 
 #include "absl/strings/numbers.h"
 
diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc
index 3fe8c95..efa4fd7 100644
--- a/absl/strings/str_cat.cc
+++ b/absl/strings/str_cat.cc
@@ -79,7 +79,7 @@
 // ----------------------------------------------------------------------
 // StrCat()
 //    This merges the given strings or integers, with no delimiter.  This
-//    is designed to be the fastest possible way to construct a std::string out
+//    is designed to be the fastest possible way to construct a string out
 //    of a mix of raw C strings, StringPieces, strings, and integer values.
 // ----------------------------------------------------------------------
 
@@ -154,10 +154,10 @@
 }
 
 // It's possible to call StrAppend with an absl::string_view that is itself a
-// fragment of the std::string we're appending to.  However the results of this are
+// fragment of the string we're appending to.  However the results of this are
 // random. Therefore, check for this in debug mode.  Use unsigned math so we
 // only have to do one comparison. Note, there's an exception case: appending an
-// empty std::string is always allowed.
+// empty string is always allowed.
 #define ASSERT_NO_OVERLAP(dest, src) \
   assert(((src).size() == 0) ||      \
          (uintptr_t((src).data() - (dest).data()) > uintptr_t((dest).size())))
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index e5501a5..da9ed9a 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -23,7 +23,7 @@
 // designed to be used as a parameter type that efficiently manages conversion
 // to strings and avoids copies in the above operations.
 //
-// Any routine accepting either a std::string or a number may accept `AlphaNum`.
+// Any routine accepting either a string or a number may accept `AlphaNum`.
 // The basic idea is that by accepting a `const AlphaNum &` as an argument
 // to your function, your callers will automagically convert bools, integers,
 // and floating point values to strings for you.
@@ -65,7 +65,7 @@
 namespace absl {
 
 namespace strings_internal {
-// AlphaNumBuffer allows a way to pass a std::string to StrCat without having to do
+// AlphaNumBuffer allows a way to pass a string to StrCat without having to do
 // memory allocation.  It is simply a pair of a fixed-size character array, and
 // a size.  Please don't use outside of absl, yet.
 template <size_t max_size>
@@ -119,8 +119,8 @@
 // Hex
 // -----------------------------------------------------------------------------
 //
-// `Hex` stores a set of hexadecimal std::string conversion parameters for use
-// within `AlphaNum` std::string conversions.
+// `Hex` stores a set of hexadecimal string conversion parameters for use
+// within `AlphaNum` string conversions.
 struct Hex {
   uint64_t value;
   uint8_t width;
@@ -168,8 +168,8 @@
 // Dec
 // -----------------------------------------------------------------------------
 //
-// `Dec` stores a set of decimal std::string conversion parameters for use
-// within `AlphaNum` std::string conversions.  Dec is slower than the default
+// `Dec` stores a set of decimal string conversion parameters for use
+// within `AlphaNum` string conversions.  Dec is slower than the default
 // integer conversion, so use it only if you need padding.
 struct Dec {
   uint64_t value;
@@ -271,7 +271,7 @@
 //
 // Merges given strings or numbers, using no delimiter(s).
 //
-// `StrCat()` is designed to be the fastest possible way to construct a std::string
+// `StrCat()` is designed to be the fastest possible way to construct a string
 // out of a mix of raw C strings, string_views, strings, bool values,
 // and numeric values.
 //
@@ -279,7 +279,7 @@
 // works poorly on strings built up out of fragments.
 //
 // For clarity and performance, don't use `StrCat()` when appending to a
-// std::string. Use `StrAppend()` instead. In particular, avoid using any of these
+// string. Use `StrAppend()` instead. In particular, avoid using any of these
 // (anti-)patterns:
 //
 //   str.append(StrCat(...))
@@ -328,26 +328,26 @@
 // StrAppend()
 // -----------------------------------------------------------------------------
 //
-// Appends a std::string or set of strings to an existing std::string, in a similar
+// Appends a string or set of strings to an existing string, in a similar
 // fashion to `StrCat()`.
 //
 // WARNING: `StrAppend(&str, a, b, c, ...)` requires that none of the
 // a, b, c, parameters be a reference into str. For speed, `StrAppend()` does
 // not try to check each of its input arguments to be sure that they are not
-// a subset of the std::string being appended to. That is, while this will work:
+// a subset of the string being appended to. That is, while this will work:
 //
-//   std::string s = "foo";
+//   string s = "foo";
 //   s += s;
 //
 // This output is undefined:
 //
-//   std::string s = "foo";
+//   string s = "foo";
 //   StrAppend(&s, s);
 //
 // This output is undefined as well, since `absl::string_view` does not own its
 // data:
 //
-//   std::string s = "foobar";
+//   string s = "foobar";
 //   absl::string_view p = s;
 //   StrAppend(&s, p);
 
diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc
index e9d6769..555d8db 100644
--- a/absl/strings/str_cat_test.cc
+++ b/absl/strings/str_cat_test.cc
@@ -28,7 +28,8 @@
 #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
   EXPECT_DEBUG_DEATH(statement, ".*")
 #else
-#define ABSL_EXPECT_DEBUG_DEATH EXPECT_DEBUG_DEATH
+#define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
+  EXPECT_DEBUG_DEATH(statement, regex)
 #endif
 
 namespace {
diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h
index 70a811b..2d07725 100644
--- a/absl/strings/str_format.h
+++ b/absl/strings/str_format.h
@@ -18,20 +18,20 @@
 // -----------------------------------------------------------------------------
 //
 // The `str_format` library is a typesafe replacement for the family of
-// `printf()` std::string formatting routines within the `<cstdio>` standard library
+// `printf()` string formatting routines within the `<cstdio>` standard library
 // header. Like the `printf` family, the `str_format` uses a "format string" to
 // perform argument substitutions based on types.
 //
 // Example:
 //
-//   std::string s = absl::StrFormat("%s %s You have $%d!", "Hello", name, dollars);
+//   string s = absl::StrFormat("%s %s You have $%d!", "Hello", name, dollars);
 //
 // The library consists of the following basic utilities:
 //
 //   * `absl::StrFormat()`, a type-safe replacement for `std::sprintf()`, to
-//     write a format std::string to a `string` value.
-//   * `absl::StrAppendFormat()` to append a format std::string to a `string`
-//   * `absl::StreamFormat()` to more efficiently write a format std::string to a
+//     write a format string to a `string` value.
+//   * `absl::StrAppendFormat()` to append a format string to a `string`
+//   * `absl::StreamFormat()` to more efficiently write a format string to a
 //     stream, such as`std::cout`.
 //   * `absl::PrintF()`, `absl::FPrintF()` and `absl::SNPrintF()` as
 //     replacements for `std::printf()`, `std::fprintf()` and `std::snprintf()`.
@@ -39,15 +39,15 @@
 //     Note: a version of `std::sprintf()` is not supported as it is
 //     generally unsafe due to buffer overflows.
 //
-// Additionally, you can provide a format std::string (and its associated arguments)
+// Additionally, you can provide a format string (and its associated arguments)
 // using one of the following abstractions:
 //
-//   * A `FormatSpec` class template fully encapsulates a format std::string and its
+//   * A `FormatSpec` class template fully encapsulates a format string and its
 //     type arguments and is usually provided to `str_format` functions as a
 //     variadic argument of type `FormatSpec<Arg...>`. The `FormatSpec<Args...>`
 //     template is evaluated at compile-time, providing type safety.
 //   * A `ParsedFormat` instance, which encapsulates a specific, pre-compiled
-//     format std::string for a specific set of type(s), and which can be passed
+//     format string for a specific set of type(s), and which can be passed
 //     between API boundaries. (The `FormatSpec` type should not be used
 //     directly.)
 //
@@ -60,7 +60,7 @@
 //
 //   * A `FormatUntyped()` function that is similar to `Format()` except it is
 //     loosely typed. `FormatUntyped()` is not a template and does not perform
-//     any compile-time checking of the format std::string; instead, it returns a
+//     any compile-time checking of the format string; instead, it returns a
 //     boolean from a runtime check.
 //
 // In addition, the `str_format` library provides extension points for
@@ -89,7 +89,7 @@
 // Example:
 //
 //   absl::UntypedFormatSpec format("%d");
-//   std::string out;
+//   string out;
 //   CHECK(absl::FormatUntyped(&out, format, {absl::FormatArg(1)}));
 class UntypedFormatSpec {
  public:
@@ -135,7 +135,7 @@
 // Example:
 //
 //   int n = 0;
-//   std::string s = absl::StrFormat("%s%d%n", "hello", 123,
+//   string s = absl::StrFormat("%s%d%n", "hello", 123,
 //                   absl::FormatCountCapture(&n));
 //   EXPECT_EQ(8, n);
 class FormatCountCapture {
@@ -155,10 +155,10 @@
 
 // FormatSpec
 //
-// The `FormatSpec` type defines the makeup of a format std::string within the
+// The `FormatSpec` type defines the makeup of a format string within the
 // `str_format` library. You should not need to use or manipulate this type
 // directly. A `FormatSpec` is a variadic class template that is evaluated at
-// compile-time, according to the format std::string and arguments that are passed
+// compile-time, according to the format string and arguments that are passed
 // to it.
 //
 // For a `FormatSpec` to be valid at compile-time, it must be provided as
@@ -166,12 +166,12 @@
 //
 // * A `constexpr` literal or `absl::string_view`, which is how it most often
 //   used.
-// * A `ParsedFormat` instantiation, which ensures the format std::string is
+// * A `ParsedFormat` instantiation, which ensures the format string is
 //   valid before use. (See below.)
 //
 // Example:
 //
-//   // Provided as a std::string literal.
+//   // Provided as a string literal.
 //   absl::StrFormat("Welcome to %s, Number %d!", "The Village", 6);
 //
 //   // Provided as a constexpr absl::string_view.
@@ -183,7 +183,7 @@
 //   absl::ParsedFormat<'s', 'd'> formatString("Welcome to %s, Number %d!");
 //   absl::StrFormat(formatString, "TheVillage", 6);
 //
-// A format std::string generally follows the POSIX syntax as used within the POSIX
+// A format string generally follows the POSIX syntax as used within the POSIX
 // `printf` specification.
 //
 // (See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html.)
@@ -223,7 +223,7 @@
 //     "%p", *int               -> "0x7ffdeb6ad2a4"
 //
 //     int n = 0;
-//     std::string s = absl::StrFormat(
+//     string s = absl::StrFormat(
 //         "%s%d%n", "hello", 123, absl::FormatCountCapture(&n));
 //     EXPECT_EQ(8, n);
 //
@@ -236,7 +236,7 @@
 //
 // However, in the `str_format` library, a format conversion specifies a broader
 // C++ conceptual category instead of an exact type. For example, `%s` binds to
-// any std::string-like argument, so `std::string`, `absl::string_view`, and
+// any string-like argument, so `std::string`, `absl::string_view`, and
 // `const char*` are all accepted. Likewise, `%d` accepts any integer-like
 // argument, etc.
 
@@ -248,7 +248,7 @@
 //
 // A `ParsedFormat` is a class template representing a preparsed `FormatSpec`,
 // with template arguments specifying the conversion characters used within the
-// format std::string. Such characters must be valid format type specifiers, and
+// format string. Such characters must be valid format type specifiers, and
 // these type specifiers are checked at compile-time.
 //
 // Instances of `ParsedFormat` can be created, copied, and reused to speed up
@@ -275,26 +275,26 @@
 
 // StrFormat()
 //
-// Returns a `string` given a `printf()`-style format std::string and zero or more
+// Returns a `string` given a `printf()`-style format string and zero or more
 // additional arguments. Use it as you would `sprintf()`. `StrFormat()` is the
 // primary formatting function within the `str_format` library, and should be
 // used in most cases where you need type-safe conversion of types into
 // formatted strings.
 //
-// The format std::string generally consists of ordinary character data along with
+// The format string generally consists of ordinary character data along with
 // one or more format conversion specifiers (denoted by the `%` character).
-// Ordinary character data is returned unchanged into the result std::string, while
+// Ordinary character data is returned unchanged into the result string, while
 // each conversion specification performs a type substitution from
 // `StrFormat()`'s other arguments. See the comments for `FormatSpec` for full
-// information on the makeup of this format std::string.
+// information on the makeup of this format string.
 //
 // Example:
 //
-//   std::string s = absl::StrFormat(
+//   string s = absl::StrFormat(
 //       "Welcome to %s, Number %d!", "The Village", 6);
 //   EXPECT_EQ("Welcome to The Village, Number 6!", s);
 //
-// Returns an empty std::string in case of error.
+// Returns an empty string in case of error.
 template <typename... Args>
 ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec<Args...>& format,
                                       const Args&... args) {
@@ -305,13 +305,13 @@
 
 // StrAppendFormat()
 //
-// Appends to a `dst` std::string given a format std::string, and zero or more additional
+// Appends to a `dst` string given a format string, and zero or more additional
 // arguments, returning `*dst` as a convenience for chaining purposes. Appends
 // nothing in case of error (but possibly alters its capacity).
 //
 // Example:
 //
-//   std::string orig("For example PI is approximately ");
+//   string orig("For example PI is approximately ");
 //   std::cout << StrAppendFormat(&orig, "%12.6f", 3.14);
 template <typename... Args>
 std::string& StrAppendFormat(std::string* dst, const FormatSpec<Args...>& format,
@@ -323,7 +323,7 @@
 
 // StreamFormat()
 //
-// Writes to an output stream given a format std::string and zero or more arguments,
+// Writes to an output stream given a format string and zero or more arguments,
 // generally in a manner that is more efficient than streaming the result of
 // `absl:: StrFormat()`. The returned object must be streamed before the full
 // expression ends.
@@ -341,7 +341,7 @@
 
 // PrintF()
 //
-// Writes to stdout given a format std::string and zero or more arguments. This
+// Writes to stdout given a format string and zero or more arguments. This
 // function is functionally equivalent to `std::printf()` (and type-safe);
 // prefer `absl::PrintF()` over `std::printf()`.
 //
@@ -361,14 +361,14 @@
 
 // FPrintF()
 //
-// Writes to a file given a format std::string and zero or more arguments. This
+// Writes to a file given a format string and zero or more arguments. This
 // function is functionally equivalent to `std::fprintf()` (and type-safe);
 // prefer `absl::FPrintF()` over `std::fprintf()`.
 //
 // Example:
 //
 //   std::string_view s = "Ulaanbaatar";
-//   absl::FPrintF("The capital of Mongolia is %s", s);
+//   absl::FPrintF(stdout, "The capital of Mongolia is %s", s);
 //
 //   Outputs: "The capital of Mongolia is Ulaanbaatar"
 //
@@ -382,7 +382,7 @@
 
 // SNPrintF()
 //
-// Writes to a sized buffer given a format std::string and zero or more arguments.
+// Writes to a sized buffer given a format string and zero or more arguments.
 // This function is functionally equivalent to `std::snprintf()` (and
 // type-safe); prefer `absl::SNPrintF()` over `std::snprintf()`.
 //
@@ -430,14 +430,14 @@
 
 // Format()
 //
-// Writes a formatted std::string to an arbitrary sink object (implementing the
-// `absl::FormatRawSink` interface), using a format std::string and zero or more
+// Writes a formatted string to an arbitrary sink object (implementing the
+// `absl::FormatRawSink` interface), using a format string and zero or more
 // additional arguments.
 //
 // By default, `string` and `std::ostream` are supported as destination objects.
 //
 // `absl::Format()` is a generic version of `absl::StrFormat(), for custom
-// sinks. The format std::string, like format strings for `StrFormat()`, is checked
+// sinks. The format string, like format strings for `StrFormat()`, is checked
 // at compile-time.
 //
 // On failure, this function returns `false` and the state of the sink is
@@ -463,13 +463,13 @@
 
 // FormatUntyped()
 //
-// Writes a formatted std::string to an arbitrary sink object (implementing the
+// Writes a formatted string to an arbitrary sink object (implementing the
 // `absl::FormatRawSink` interface), using an `UntypedFormatSpec` and zero or
 // more additional arguments.
 //
 // This function acts as the most generic formatting function in the
 // `str_format` library. The caller provides a raw sink, an unchecked format
-// std::string, and (usually) a runtime specified list of arguments; no compile-time
+// string, and (usually) a runtime specified list of arguments; no compile-time
 // checking of formatting is performed within this function. As a result, a
 // caller should check the return value to verify that no error occurred.
 // On failure, this function returns `false` and the state of the sink is
@@ -483,9 +483,9 @@
 //
 // Example:
 //
-//   std::optional<std::string> FormatDynamic(const std::string& in_format,
-//                                       const vector<std::string>& in_args) {
-//     std::string out;
+//   std::optional<string> FormatDynamic(const string& in_format,
+//                                       const vector<string>& in_args) {
+//     string out;
 //     std::vector<absl::FormatArg> args;
 //     for (const auto& v : in_args) {
 //       // It is important that 'v' is a reference to the objects in in_args.
diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc
index fed75fa..aa14e21 100644
--- a/absl/strings/str_format_test.cc
+++ b/absl/strings/str_format_test.cc
@@ -391,7 +391,7 @@
 #endif
 
   // Other conversion
-  int64_t value = 0x7ffdeb6;
+  int64_t value = 0x7ffdeb4;
   auto ptr_value = static_cast<uintptr_t>(value);
   const int& something = *reinterpret_cast<const int*>(ptr_value);
   EXPECT_EQ(StrFormat("%p", &something), StrFormat("0x%x", ptr_value));
@@ -601,3 +601,39 @@
 
 }  // namespace
 }  // namespace absl
+
+// Some codegen thunks that we can use to easily dump the generated assembly for
+// different StrFormat calls.
+
+inline std::string CodegenAbslStrFormatInt(int i) {
+  return absl::StrFormat("%d", i);
+}
+
+inline std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s,
+                                                 int64_t i64) {
+  return absl::StrFormat("%d %s %d", i, s, i64);
+}
+
+inline void CodegenAbslStrAppendFormatInt(std::string* out, int i) {
+  absl::StrAppendFormat(out, "%d", i);
+}
+
+inline void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i,
+                                                     const std::string& s,
+                                                     int64_t i64) {
+  absl::StrAppendFormat(out, "%d %s %d", i, s, i64);
+}
+
+auto absl_internal_str_format_force_codegen_funcs = std::make_tuple(
+    CodegenAbslStrFormatInt, CodegenAbslStrFormatIntStringInt64,
+    CodegenAbslStrAppendFormatInt, CodegenAbslStrAppendFormatIntStringInt64);
+
+bool absl_internal_str_format_force_codegen_always_false;
+// Force the compiler to generate the functions by making it look like we
+// escape the function pointers.
+// It can't statically know that
+// absl_internal_str_format_force_codegen_always_false is not changed by someone
+// else.
+bool absl_internal_str_format_force_codegen =
+    absl_internal_str_format_force_codegen_always_false &&
+    printf("%p", &absl_internal_str_format_force_codegen_funcs) == 0;
diff --git a/absl/strings/str_join.h b/absl/strings/str_join.h
index bd4d0e1..f9611ad 100644
--- a/absl/strings/str_join.h
+++ b/absl/strings/str_join.h
@@ -18,10 +18,10 @@
 // -----------------------------------------------------------------------------
 //
 // This header file contains functions for joining a range of elements and
-// returning the result as a std::string. StrJoin operations are specified by passing
-// a range, a separator std::string to use between the elements joined, and an
+// returning the result as a string. StrJoin operations are specified by passing
+// a range, a separator string to use between the elements joined, and an
 // optional Formatter responsible for converting each argument in the range to a
-// std::string. If omitted, a default `AlphaNumFormatter()` is called on the elements
+// string. If omitted, a default `AlphaNumFormatter()` is called on the elements
 // to be joined, using the same formatting that `absl::StrCat()` uses. This
 // package defines a number of default formatters, and you can define your own
 // implementations.
@@ -29,7 +29,7 @@
 // Ranges are specified by passing a container with `std::begin()` and
 // `std::end()` iterators, container-specific `begin()` and `end()` iterators, a
 // brace-initialized `std::initializer_list`, or a `std::tuple` of heterogeneous
-// objects. The separator std::string is specified as an `absl::string_view`.
+// objects. The separator string is specified as an `absl::string_view`.
 //
 // Because the default formatter uses the `absl::AlphaNum` class,
 // `absl::StrJoin()`, like `absl::StrCat()`, will work out-of-the-box on
@@ -37,8 +37,8 @@
 //
 // Example:
 //
-//   std::vector<std::string> v = {"foo", "bar", "baz"};
-//   std::string s = absl::StrJoin(v, "-");
+//   std::vector<string> v = {"foo", "bar", "baz"};
+//   string s = absl::StrJoin(v, "-");
 //   EXPECT_EQ("foo-bar-baz", s);
 //
 // See comments on the `absl::StrJoin()` function for more examples.
@@ -52,6 +52,7 @@
 #include <iterator>
 #include <string>
 #include <tuple>
+#include <type_traits>
 #include <utility>
 
 #include "absl/base/macros.h"
@@ -65,16 +66,16 @@
 // -----------------------------------------------------------------------------
 //
 // A Formatter is a function object that is responsible for formatting its
-// argument as a std::string and appending it to a given output std::string. Formatters
+// argument as a string and appending it to a given output string. Formatters
 // may be implemented as function objects, lambdas, or normal functions. You may
 // provide your own Formatter to enable `absl::StrJoin()` to work with arbitrary
 // types.
 //
 // The following is an example of a custom Formatter that simply uses
-// `std::to_string()` to format an integer as a std::string.
+// `std::to_string()` to format an integer as a string.
 //
 //   struct MyFormatter {
-//     void operator()(std::string* out, int i) const {
+//     void operator()(string* out, int i) const {
 //       out->append(std::to_string(i));
 //     }
 //   };
@@ -83,7 +84,7 @@
 // argument to `absl::StrJoin()`:
 //
 //   std::vector<int> v = {1, 2, 3, 4};
-//   std::string s = absl::StrJoin(v, "-", MyFormatter());
+//   string s = absl::StrJoin(v, "-", MyFormatter());
 //   EXPECT_EQ("1-2-3-4", s);
 //
 // The following standard formatters are provided within this file:
@@ -155,10 +156,10 @@
 // StrJoin()
 // -----------------------------------------------------------------------------
 //
-// Joins a range of elements and returns the result as a std::string.
-// `absl::StrJoin()` takes a range, a separator std::string to use between the
+// Joins a range of elements and returns the result as a string.
+// `absl::StrJoin()` takes a range, a separator string to use between the
 // elements joined, and an optional Formatter responsible for converting each
-// argument in the range to a std::string.
+// argument in the range to a string.
 //
 // If omitted, the default `AlphaNumFormatter()` is called on the elements to be
 // joined.
@@ -166,22 +167,22 @@
 // Example 1:
 //   // Joins a collection of strings. This pattern also works with a collection
 //   // of `absl::string_view` or even `const char*`.
-//   std::vector<std::string> v = {"foo", "bar", "baz"};
-//   std::string s = absl::StrJoin(v, "-");
+//   std::vector<string> v = {"foo", "bar", "baz"};
+//   string s = absl::StrJoin(v, "-");
 //   EXPECT_EQ("foo-bar-baz", s);
 //
 // Example 2:
 //   // Joins the values in the given `std::initializer_list<>` specified using
 //   // brace initialization. This pattern also works with an initializer_list
 //   // of ints or `absl::string_view` -- any `AlphaNum`-compatible type.
-//   std::string s = absl::StrJoin({"foo", "bar", "baz"}, "-");
+//   string s = absl::StrJoin({"foo", "bar", "baz"}, "-");
 //   EXPECT_EQ("foo-bar-baz", s);
 //
 // Example 3:
 //   // Joins a collection of ints. This pattern also works with floats,
 //   // doubles, int64s -- any `StrCat()`-compatible type.
 //   std::vector<int> v = {1, 2, 3, -4};
-//   std::string s = absl::StrJoin(v, "-");
+//   string s = absl::StrJoin(v, "-");
 //   EXPECT_EQ("1-2-3--4", s);
 //
 // Example 4:
@@ -192,7 +193,7 @@
 //   // `std::vector<int*>`.
 //   int x = 1, y = 2, z = 3;
 //   std::vector<int*> v = {&x, &y, &z};
-//   std::string s = absl::StrJoin(v, "-");
+//   string s = absl::StrJoin(v, "-");
 //   EXPECT_EQ("1-2-3", s);
 //
 // Example 5:
@@ -201,42 +202,42 @@
 //   v.emplace_back(new int(1));
 //   v.emplace_back(new int(2));
 //   v.emplace_back(new int(3));
-//   std::string s = absl::StrJoin(v, "-");
+//   string s = absl::StrJoin(v, "-");
 //   EXPECT_EQ("1-2-3", s);
 //
 // Example 6:
 //   // Joins a `std::map`, with each key-value pair separated by an equals
 //   // sign. This pattern would also work with, say, a
 //   // `std::vector<std::pair<>>`.
-//   std::map<std::string, int> m = {
+//   std::map<string, int> m = {
 //       std::make_pair("a", 1),
 //       std::make_pair("b", 2),
 //       std::make_pair("c", 3)};
-//   std::string s = absl::StrJoin(m, ",", absl::PairFormatter("="));
+//   string s = absl::StrJoin(m, ",", absl::PairFormatter("="));
 //   EXPECT_EQ("a=1,b=2,c=3", s);
 //
 // Example 7:
 //   // These examples show how `absl::StrJoin()` handles a few common edge
 //   // cases:
-//   std::vector<std::string> v_empty;
+//   std::vector<string> v_empty;
 //   EXPECT_EQ("", absl::StrJoin(v_empty, "-"));
 //
-//   std::vector<std::string> v_one_item = {"foo"};
+//   std::vector<string> v_one_item = {"foo"};
 //   EXPECT_EQ("foo", absl::StrJoin(v_one_item, "-"));
 //
-//   std::vector<std::string> v_empty_string = {""};
+//   std::vector<string> v_empty_string = {""};
 //   EXPECT_EQ("", absl::StrJoin(v_empty_string, "-"));
 //
-//   std::vector<std::string> v_one_item_empty_string = {"a", ""};
+//   std::vector<string> v_one_item_empty_string = {"a", ""};
 //   EXPECT_EQ("a-", absl::StrJoin(v_one_item_empty_string, "-"));
 //
-//   std::vector<std::string> v_two_empty_string = {"", ""};
+//   std::vector<string> v_two_empty_string = {"", ""};
 //   EXPECT_EQ("-", absl::StrJoin(v_two_empty_string, "-"));
 //
 // Example 8:
 //   // Joins a `std::tuple<T...>` of heterogeneous types, converting each to
-//   // a std::string using the `absl::AlphaNum` class.
-//   std::string s = absl::StrJoin(std::make_tuple(123, "abc", 0.456), "-");
+//   // a string using the `absl::AlphaNum` class.
+//   string s = absl::StrJoin(std::make_tuple(123, "abc", 0.456), "-");
 //   EXPECT_EQ("123-abc-0.456", s);
 
 template <typename Iterator, typename Formatter>
diff --git a/absl/strings/str_replace.h b/absl/strings/str_replace.h
index f4d9bb9..3bfe4c6 100644
--- a/absl/strings/str_replace.h
+++ b/absl/strings/str_replace.h
@@ -17,19 +17,19 @@
 // File: str_replace.h
 // -----------------------------------------------------------------------------
 //
-// This file defines `absl::StrReplaceAll()`, a general-purpose std::string
+// This file defines `absl::StrReplaceAll()`, a general-purpose string
 // replacement function designed for large, arbitrary text substitutions,
 // especially on strings which you are receiving from some other system for
 // further processing (e.g. processing regular expressions, escaping HTML
-// entities, etc. `StrReplaceAll` is designed to be efficient even when only
+// entities, etc.). `StrReplaceAll` is designed to be efficient even when only
 // one substitution is being performed, or when substitution is rare.
 //
-// If the std::string being modified is known at compile-time, and the substitutions
+// If the string being modified is known at compile-time, and the substitutions
 // vary, `absl::Substitute()` may be a better choice.
 //
 // Example:
 //
-// std::string html_escaped = absl::StrReplaceAll(user_input, {
+// string html_escaped = absl::StrReplaceAll(user_input, {
 //                                           {"&", "&amp;"},
 //                                           {"<", "&lt;"},
 //                                           {">", "&gt;"},
@@ -49,16 +49,16 @@
 
 // StrReplaceAll()
 //
-// Replaces character sequences within a given std::string with replacements provided
+// Replaces character sequences within a given string with replacements provided
 // within an initializer list of key/value pairs. Candidate replacements are
-// considered in order as they occur within the std::string, with earlier matches
+// considered in order as they occur within the string, with earlier matches
 // taking precedence, and longer matches taking precedence for candidates
-// starting at the same position in the std::string. Once a substitution is made, the
+// starting at the same position in the string. Once a substitution is made, the
 // replaced text is not considered for any further substitutions.
 //
 // Example:
 //
-//   std::string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!",
+//   string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!",
 //                                  {{"$count", absl::StrCat(5)},
 //                                   {"$who", "Bob"},
 //                                   {"#Noun", "Apples"}});
@@ -78,28 +78,28 @@
 //   replacements["$who"] = "Bob";
 //   replacements["$count"] = "5";
 //   replacements["#Noun"] = "Apples";
-//   std::string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!",
+//   string s = absl::StrReplaceAll("$who bought $count #Noun. Thanks $who!",
 //                                  replacements);
 //   EXPECT_EQ("Bob bought 5 Apples. Thanks Bob!", s);
 //
 //   // A std::vector of std::pair elements can be more efficient.
-//   std::vector<std::pair<const absl::string_view, std::string>> replacements;
+//   std::vector<std::pair<const absl::string_view, string>> replacements;
 //   replacements.push_back({"&", "&amp;"});
 //   replacements.push_back({"<", "&lt;"});
 //   replacements.push_back({">", "&gt;"});
-//   std::string s = absl::StrReplaceAll("if (ptr < &foo)",
+//   string s = absl::StrReplaceAll("if (ptr < &foo)",
 //                                  replacements);
 //   EXPECT_EQ("if (ptr &lt; &amp;foo)", s);
 template <typename StrToStrMapping>
 std::string StrReplaceAll(absl::string_view s, const StrToStrMapping& replacements);
 
 // Overload of `StrReplaceAll()` to replace character sequences within a given
-// output std::string *in place* with replacements provided within an initializer
+// output string *in place* with replacements provided within an initializer
 // list of key/value pairs, returning the number of substitutions that occurred.
 //
 // Example:
 //
-//   std::string s = std::string("$who bought $count #Noun. Thanks $who!");
+//   string s = std::string("$who bought $count #Noun. Thanks $who!");
 //   int count;
 //   count = absl::StrReplaceAll({{"$count", absl::StrCat(5)},
 //                               {"$who", "Bob"},
@@ -112,12 +112,12 @@
     std::string* target);
 
 // Overload of `StrReplaceAll()` to replace patterns within a given output
-// std::string *in place* with replacements provided within a container of key/value
+// string *in place* with replacements provided within a container of key/value
 // pairs.
 //
 // Example:
 //
-//   std::string s = std::string("if (ptr < &foo)");
+//   string s = std::string("if (ptr < &foo)");
 //   int count = absl::StrReplaceAll({{"&", "&amp;"},
 //                                    {"<", "&lt;"},
 //                                    {">", "&gt;"}}, &s);
diff --git a/absl/strings/str_replace_benchmark.cc b/absl/strings/str_replace_benchmark.cc
index e608de8..8386f2e 100644
--- a/absl/strings/str_replace_benchmark.cc
+++ b/absl/strings/str_replace_benchmark.cc
@@ -38,16 +38,16 @@
     {"liquor", "shakes"},    //
 };
 
-// Here, we set up a std::string for use in global-replace benchmarks.
+// Here, we set up a string for use in global-replace benchmarks.
 // We started with a million blanks, and then deterministically insert
-// 10,000 copies each of two pangrams.  The result is a std::string that is
+// 10,000 copies each of two pangrams.  The result is a string that is
 // 40% blank space and 60% these words.  'the' occurs 18,247 times and
 // all the substitutions together occur 49,004 times.
 //
-// We then create "after_replacing_the" to be a std::string that is a result of
+// We then create "after_replacing_the" to be a string that is a result of
 // replacing "the" with "box" in big_string.
 //
-// And then we create "after_replacing_many" to be a std::string that is result
+// And then we create "after_replacing_many" to be a string that is result
 // of preferring several substitutions.
 void SetUpStrings() {
   if (big_string == nullptr) {
diff --git a/absl/strings/str_split.cc b/absl/strings/str_split.cc
index 0207213..0a68c52 100644
--- a/absl/strings/str_split.cc
+++ b/absl/strings/str_split.cc
@@ -43,10 +43,11 @@
   if (delimiter.empty() && text.length() > 0) {
     // Special case for empty std::string delimiters: always return a zero-length
     // absl::string_view referring to the item at position 1 past pos.
-    return absl::string_view(text.begin() + pos + 1, 0);
+    return absl::string_view(text.data() + pos + 1, 0);
   }
   size_t found_pos = absl::string_view::npos;
-  absl::string_view found(text.end(), 0);  // By default, not found
+  absl::string_view found(text.data() + text.size(),
+                          0);  // By default, not found
   found_pos = find_policy.Find(text, delimiter, pos);
   if (found_pos != absl::string_view::npos) {
     found = absl::string_view(text.data() + found_pos,
@@ -87,7 +88,7 @@
     // absl::string_view.
     size_t found_pos = text.find(delimiter_[0], pos);
     if (found_pos == absl::string_view::npos)
-      return absl::string_view(text.end(), 0);
+      return absl::string_view(text.data() + text.size(), 0);
     return text.substr(found_pos, 1);
   }
   return GenericFind(text, delimiter_, pos, LiteralPolicy());
@@ -100,7 +101,7 @@
 absl::string_view ByChar::Find(absl::string_view text, size_t pos) const {
   size_t found_pos = text.find(c_, pos);
   if (found_pos == absl::string_view::npos)
-    return absl::string_view(text.end(), 0);
+    return absl::string_view(text.data() + text.size(), 0);
   return text.substr(found_pos, 1);
 }
 
@@ -128,9 +129,9 @@
   // If the std::string is shorter than the chunk size we say we
   // "can't find the delimiter" so this will be the last chunk.
   if (substr.length() <= static_cast<size_t>(length_))
-    return absl::string_view(text.end(), 0);
+    return absl::string_view(text.data() + text.size(), 0);
 
-  return absl::string_view(substr.begin() + length_, 0);
+  return absl::string_view(substr.data() + length_, 0);
 }
 
 }  // namespace absl
diff --git a/absl/strings/str_split.h b/absl/strings/str_split.h
index 9a7be2b..c7eb280 100644
--- a/absl/strings/str_split.h
+++ b/absl/strings/str_split.h
@@ -19,13 +19,13 @@
 //
 // This file contains functions for splitting strings. It defines the main
 // `StrSplit()` function, several delimiters for determining the boundaries on
-// which to split the std::string, and predicates for filtering delimited results.
+// which to split the string, and predicates for filtering delimited results.
 // `StrSplit()` adapts the returned collection to the type specified by the
 // caller.
 //
 // Example:
 //
-//   // Splits the given std::string on commas. Returns the results in a
+//   // Splits the given string on commas. Returns the results in a
 //   // vector of strings.
 //   std::vector<std::string> v = absl::StrSplit("a,b,c", ',');
 //   // Can also use ","
@@ -55,7 +55,7 @@
 //------------------------------------------------------------------------------
 //
 // `StrSplit()` uses delimiters to define the boundaries between elements in the
-// provided input. Several `Delimiter` types are defined below. If a std::string
+// provided input. Several `Delimiter` types are defined below. If a string
 // (`const char*`, `std::string`, or `absl::string_view`) is passed in place of
 // an explicit `Delimiter` object, `StrSplit()` treats it the same way as if it
 // were passed a `ByString` delimiter.
@@ -65,7 +65,7 @@
 //
 // The following `Delimiter` types are available for use within `StrSplit()`:
 //
-//   - `ByString` (default for std::string arguments)
+//   - `ByString` (default for string arguments)
 //   - `ByChar` (default for a char argument)
 //   - `ByAnyChar`
 //   - `ByLength`
@@ -76,15 +76,15 @@
 // be split and the position to begin searching for the next delimiter in the
 // input text. The returned absl::string_view should refer to the next
 // occurrence (after pos) of the represented delimiter; this returned
-// absl::string_view represents the next location where the input std::string should
+// absl::string_view represents the next location where the input string should
 // be broken. The returned absl::string_view may be zero-length if the Delimiter
-// does not represent a part of the std::string (e.g., a fixed-length delimiter). If
+// does not represent a part of the string (e.g., a fixed-length delimiter). If
 // no delimiter is found in the given text, a zero-length absl::string_view
 // referring to text.end() should be returned (e.g.,
 // absl::string_view(text.end(), 0)). It is important that the returned
 // absl::string_view always be within the bounds of input text given as an
-// argument--it must not refer to a std::string that is physically located outside of
-// the given std::string.
+// argument--it must not refer to a string that is physically located outside of
+// the given string.
 //
 // The following example is a simple Delimiter object that is created with a
 // single char and will look for that char in the text passed to the Find()
@@ -104,13 +104,13 @@
 
 // ByString
 //
-// A sub-std::string delimiter. If `StrSplit()` is passed a std::string in place of a
-// `Delimiter` object, the std::string will be implicitly converted into a
+// A sub-string delimiter. If `StrSplit()` is passed a string in place of a
+// `Delimiter` object, the string will be implicitly converted into a
 // `ByString` delimiter.
 //
 // Example:
 //
-//   // Because a std::string literal is converted to an `absl::ByString`,
+//   // Because a string literal is converted to an `absl::ByString`,
 //   // the following two splits are equivalent.
 //
 //   std::vector<std::string> v1 = absl::StrSplit("a, b, c", ", ");
@@ -131,7 +131,7 @@
 // ByChar
 //
 // A single character delimiter. `ByChar` is functionally equivalent to a
-// 1-char std::string within a `ByString` delimiter, but slightly more
+// 1-char string within a `ByString` delimiter, but slightly more
 // efficient.
 //
 // Example:
@@ -164,9 +164,9 @@
 // ByAnyChar
 //
 // A delimiter that will match any of the given byte-sized characters within
-// its provided std::string.
+// its provided string.
 //
-// Note: this delimiter works with single-byte std::string data, but does not work
+// Note: this delimiter works with single-byte string data, but does not work
 // with variable-width encodings, such as UTF-8.
 //
 // Example:
@@ -175,8 +175,8 @@
 //   std::vector<std::string> v = absl::StrSplit("a,b=c", ByAnyChar(",="));
 //   // v[0] == "a", v[1] == "b", v[2] == "c"
 //
-// If `ByAnyChar` is given the empty std::string, it behaves exactly like
-// `ByString` and matches each individual character in the input std::string.
+// If `ByAnyChar` is given the empty string, it behaves exactly like
+// `ByString` and matches each individual character in the input string.
 //
 class ByAnyChar {
  public:
@@ -192,7 +192,7 @@
 // A delimiter for splitting into equal-length strings. The length argument to
 // the constructor must be greater than 0.
 //
-// Note: this delimiter works with single-byte std::string data, but does not work
+// Note: this delimiter works with single-byte string data, but does not work
 // with variable-width encodings, such as UTF-8.
 //
 // Example:
@@ -202,7 +202,7 @@
 
 //   // v[0] == "123", v[1] == "456", v[2] == "789"
 //
-// Note that the std::string does not have to be a multiple of the fixed split
+// Note that the string does not have to be a multiple of the fixed split
 // length. In such a case, the last substring will be shorter.
 //
 //   using absl::ByLength;
@@ -223,9 +223,9 @@
 // A traits-like metafunction for selecting the default Delimiter object type
 // for a particular Delimiter type. The base case simply exposes type Delimiter
 // itself as the delimiter's Type. However, there are specializations for
-// std::string-like objects that map them to the ByString delimiter object.
+// string-like objects that map them to the ByString delimiter object.
 // This allows functions like absl::StrSplit() and absl::MaxSplits() to accept
-// std::string-like objects (e.g., ',') as delimiter arguments but they will be
+// string-like objects (e.g., ',') as delimiter arguments but they will be
 // treated as if a ByString delimiter was given.
 template <typename Delimiter>
 struct SelectDelimiter {
@@ -261,7 +261,8 @@
       : delimiter_(delimiter), limit_(limit), count_(0) {}
   absl::string_view Find(absl::string_view text, size_t pos) {
     if (count_++ == limit_) {
-      return absl::string_view(text.end(), 0);  // No more matches.
+      return absl::string_view(text.data() + text.size(),
+                               0);  // No more matches.
     }
     return delimiter_.Find(text, pos);
   }
@@ -331,7 +332,7 @@
 // SkipEmpty()
 //
 // Returns `false` if the given `absl::string_view` is empty, indicating that
-// `StrSplit()` should omit the empty std::string.
+// `StrSplit()` should omit the empty string.
 //
 // Example:
 //
@@ -339,7 +340,7 @@
 //
 //   // v[0] == "a", v[1] == "b"
 //
-// Note: `SkipEmpty()` does not consider a std::string containing only whitespace
+// Note: `SkipEmpty()` does not consider a string containing only whitespace
 // to be empty. To skip such whitespace as well, use the `SkipWhitespace()`
 // predicate.
 struct SkipEmpty {
@@ -349,7 +350,7 @@
 // SkipWhitespace()
 //
 // Returns `false` if the given `absl::string_view` is empty *or* contains only
-// whitespace, indicating that `StrSplit()` should omit the std::string.
+// whitespace, indicating that `StrSplit()` should omit the string.
 //
 // Example:
 //
@@ -373,7 +374,7 @@
 
 // StrSplit()
 //
-// Splits a given std::string based on the provided `Delimiter` object, returning the
+// Splits a given string based on the provided `Delimiter` object, returning the
 // elements within the type specified by the caller. Optionally, you may pass a
 // `Predicate` to `StrSplit()` indicating whether to include or exclude the
 // resulting element within the final result set. (See the overviews for
@@ -412,7 +413,7 @@
 //
 // The `StrSplit()` function adapts the returned collection to the collection
 // specified by the caller (e.g. `std::vector` above). The returned collections
-// may contain `string`, `absl::string_view` (in which case the original std::string
+// may contain `string`, `absl::string_view` (in which case the original string
 // being split must ensure that it outlives the collection), or any object that
 // can be explicitly created from an `absl::string_view`. This behavior works
 // for:
@@ -424,7 +425,7 @@
 // Example:
 //
 //   // The results are returned as `absl::string_view` objects. Note that we
-//   // have to ensure that the input std::string outlives any results.
+//   // have to ensure that the input string outlives any results.
 //   std::vector<absl::string_view> v = absl::StrSplit("a,b,c", ',');
 //
 //   // Stores results in a std::set<std::string>, which also performs
@@ -444,7 +445,7 @@
 //   // is provided as a series of key/value pairs. For example, the 0th element
 //   // resulting from the split will be stored as a key to the 1st element. If
 //   // an odd number of elements are resolved, the last element is paired with
-//   // a default-constructed value (e.g., empty std::string).
+//   // a default-constructed value (e.g., empty string).
 //   std::map<std::string, std::string> m = absl::StrSplit("a,b,c", ',');
 //   // m["a"] == "b", m["c"] == ""     // last component value equals ""
 //
@@ -452,14 +453,14 @@
 // elements and is not a collection type. When splitting to a `std::pair` the
 // first two split strings become the `std::pair` `.first` and `.second`
 // members, respectively. The remaining split substrings are discarded. If there
-// are less than two split substrings, the empty std::string is used for the
+// are less than two split substrings, the empty string is used for the
 // corresponding
 // `std::pair` member.
 //
 // Example:
 //
 //   // Stores first two split strings as the members in a std::pair.
-//   std::pair<std::string, std::string> p = absl::StrSplit("a,b,c", ',');
+//   std::pair<string, string> p = absl::StrSplit("a,b,c", ',');
 //   // p.first == "a", p.second == "b"       // "c" is omitted.
 //
 // The `StrSplit()` function can be used multiple times to perform more
@@ -467,9 +468,9 @@
 //
 // Example:
 //
-//   // The input std::string "a=b=c,d=e,f=,g" becomes
+//   // The input string "a=b=c,d=e,f=,g" becomes
 //   // { "a" => "b=c", "d" => "e", "f" => "", "g" => "" }
-//   std::map<std::string, std::string> m;
+//   std::map<string, string> m;
 //   for (absl::string_view sp : absl::StrSplit("a=b=c,d=e,f=,g", ',')) {
 //     m.insert(absl::StrSplit(sp, absl::MaxSplits('=', 1)));
 //   }
diff --git a/absl/strings/str_split_test.cc b/absl/strings/str_split_test.cc
index 29a6804..413ad31 100644
--- a/absl/strings/str_split_test.cc
+++ b/absl/strings/str_split_test.cc
@@ -276,7 +276,7 @@
   EXPECT_EQ(it, end);
 }
 
-// Simple Predicate to skip a particular std::string.
+// Simple Predicate to skip a particular string.
 class Skip {
  public:
   explicit Skip(const std::string& s) : s_(s) {}
@@ -763,12 +763,12 @@
 static bool IsFoundAtStartingPos(absl::string_view text, Delimiter d,
                                  size_t starting_pos, int expected_pos) {
   absl::string_view found = d.Find(text, starting_pos);
-  return found.data() != text.end() &&
+  return found.data() != text.data() + text.size() &&
          expected_pos == found.data() - text.data();
 }
 
 // Helper function for testing Delimiter objects. Returns true if the given
-// Delimiter is found in the given std::string at the given position. This function
+// Delimiter is found in the given string at the given position. This function
 // tests two cases:
 //   1. The actual text given, staring at position 0
 //   2. The text given with leading padding that should be ignored
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index a7f9199..6bcd3c4 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -19,7 +19,7 @@
 //
 // This file contains the definition of the `absl::string_view` class. A
 // `string_view` points to a contiguous span of characters, often part or all of
-// another `std::string`, double-quoted std::string literal, character array, or even
+// another `std::string`, double-quoted string literal, character array, or even
 // another `string_view`.
 //
 // This `absl::string_view` abstraction is designed to be a drop-in
@@ -56,15 +56,15 @@
 
 // absl::string_view
 //
-// A `string_view` provides a lightweight view into the std::string data provided by
-// a `std::string`, double-quoted std::string literal, character array, or even
-// another `string_view`. A `string_view` does *not* own the std::string to which it
+// A `string_view` provides a lightweight view into the string data provided by
+// a `std::string`, double-quoted string literal, character array, or even
+// another `string_view`. A `string_view` does *not* own the string to which it
 // points, and that data cannot be modified through the view.
 //
 // You can use `string_view` as a function or method parameter anywhere a
-// parameter can receive a double-quoted std::string literal, `const char*`,
+// parameter can receive a double-quoted string literal, `const char*`,
 // `std::string`, or another `absl::string_view` argument with no need to copy
-// the std::string data. Systematic use of `string_view` within function arguments
+// the string data. Systematic use of `string_view` within function arguments
 // reduces data copies and `strlen()` calls.
 //
 // Because of its small size, prefer passing `string_view` by value:
@@ -97,8 +97,8 @@
 // `string_view` this way, it is your responsibility to ensure that the object
 // pointed to by the `string_view` outlives the `string_view`.
 //
-// A `string_view` may represent a whole std::string or just part of a std::string. For
-// example, when splitting a std::string, `std::vector<absl::string_view>` is a
+// A `string_view` may represent a whole string or just part of a string. For
+// example, when splitting a string, `std::vector<absl::string_view>` is a
 // natural data type for the output.
 //
 //
@@ -141,7 +141,7 @@
 // All empty `string_view` objects whether null or not, are equal:
 //
 //   absl::string_view() == absl::string_view("", 0)
-//   absl::string_view(nullptr, 0) == absl:: string_view("abcdef"+6, 0)
+//   absl::string_view(nullptr, 0) == absl::string_view("abcdef"+6, 0)
 class string_view {
  public:
   using traits_type = std::char_traits<char>;
@@ -173,8 +173,19 @@
   // Implicit constructor of a `string_view` from nul-terminated `str`. When
   // accepting possibly null strings, use `absl::NullSafeStringView(str)`
   // instead (see below).
+#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
+    (defined(__GNUC__) && !defined(__clang__))
+  // GCC has __builtin_strlen according to
+  // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
+  // ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
+  // __builtin_strlen is constexpr.
   constexpr string_view(const char* str)  // NOLINT(runtime/explicit)
-      : ptr_(str), length_(CheckLengthInternal(StrLenInternal(str))) {}
+      : ptr_(str),
+        length_(CheckLengthInternal(str ? __builtin_strlen(str) : 0)) {}
+#else
+  constexpr string_view(const char* str)  // NOLINT(runtime/explicit)
+      : ptr_(str), length_(CheckLengthInternal(str ? strlen(str) : 0)) {}
+#endif
 
   // Implicit constructor of a `string_view` from a `const char*` and length.
   constexpr string_view(const char* data, size_type len)
@@ -340,7 +351,7 @@
   //
   // Returns a "substring" of the `string_view` (at offset `pos` and length
   // `n`) as another string_view. This function throws `std::out_of_bounds` if
-  // `pos > size'.
+  // `pos > size`.
   string_view substr(size_type pos, size_type n = npos) const {
     if (ABSL_PREDICT_FALSE(pos > length_))
       base_internal::ThrowStdOutOfRange("absl::string_view::substr");
@@ -351,7 +362,7 @@
   // string_view::compare()
   //
   // Performs a lexicographical comparison between the `string_view` and
-  // another `absl::string_view), returning -1 if `this` is less than, 0 if
+  // another `absl::string_view`, returning -1 if `this` is less than, 0 if
   // `this` is equal to, and 1 if `this` is greater than the passed std::string
   // view. Note that in the case of data equality, a further comparison is made
   // on the respective sizes of the two `string_view`s to determine which is
@@ -481,22 +492,6 @@
   static constexpr size_type kMaxSize =
       std::numeric_limits<difference_type>::max();
 
-  // check whether __builtin_strlen is provided by the compiler.
-  // GCC doesn't have __has_builtin()
-  // (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66970),
-  // but has __builtin_strlen according to
-  // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html.
-#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
-    (defined(__GNUC__) && !defined(__clang__))
-  static constexpr size_type StrLenInternal(const char* str) {
-    return str ? __builtin_strlen(str) : 0;
-  }
-#else
-  static constexpr size_type StrLenInternal(const char* str) {
-    return str ? strlen(str) : 0;
-  }
-#endif
-
   static constexpr size_type CheckLengthInternal(size_type len) {
     return ABSL_ASSERT(len <= kMaxSize), len;
   }
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index b19d07c..a94e822 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -35,7 +35,8 @@
 #define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
   EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
 #else
-#define ABSL_EXPECT_DEATH_IF_SUPPORTED EXPECT_DEATH_IF_SUPPORTED
+#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
+  EXPECT_DEATH_IF_SUPPORTED(statement, regex)
 #endif
 
 namespace {
@@ -283,7 +284,7 @@
 }
 #undef COMPARE
 
-// Sadly, our users often confuse std::string::npos with absl::string_view::npos;
+// Sadly, our users often confuse string::npos with absl::string_view::npos;
 // So much so that we test here that they are the same.  They need to
 // both be unsigned, and both be the maximum-valued integer of their type.
 
@@ -811,15 +812,18 @@
 }
 
 // `std::string_view::string_view(const char*)` calls
-// `std::char_traits<char>::length(const char*)` to get the std::string length. In
+// `std::char_traits<char>::length(const char*)` to get the string length. In
 // libc++, it doesn't allow `nullptr` in the constexpr context, with the error
 // "read of dereferenced null pointer is not allowed in a constant expression".
 // At run time, the behavior of `std::char_traits::length()` on `nullptr` is
-// undefined by the standard and usually results in crash with libc++. This
-// conforms to the standard, but `absl::string_view` implements a different
+// undefined by the standard and usually results in crash with libc++.
+// In MSVC, creating a constexpr string_view from nullptr also triggers an
+// "unevaluable pointer value" error. This compiler implementation conforms
+// to the standard, but `absl::string_view` implements a different
 // behavior for historical reasons. We work around tests that construct
 // `string_view` from `nullptr` when using libc++.
-#if !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
+#if !defined(ABSL_HAVE_STD_STRING_VIEW) || \
+    (!defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
 #define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
 #endif  // !defined(ABSL_HAVE_STD_STRING_VIEW) || !defined(_LIBCPP_VERSION)
 
diff --git a/absl/strings/strip.h b/absl/strings/strip.h
index 2f8d21f..8d0d7c6 100644
--- a/absl/strings/strip.h
+++ b/absl/strings/strip.h
@@ -17,7 +17,7 @@
 // File: strip.h
 // -----------------------------------------------------------------------------
 //
-// This file contains various functions for stripping substrings from a std::string.
+// This file contains various functions for stripping substrings from a string.
 #ifndef ABSL_STRINGS_STRIP_H_
 #define ABSL_STRINGS_STRIP_H_
 
@@ -33,7 +33,7 @@
 
 // ConsumePrefix()
 //
-// Strips the `expected` prefix from the start of the given std::string, returning
+// Strips the `expected` prefix from the start of the given string, returning
 // `true` if the strip operation succeeded or false otherwise.
 //
 // Example:
@@ -48,7 +48,7 @@
 }
 // ConsumeSuffix()
 //
-// Strips the `expected` suffix from the end of the given std::string, returning
+// Strips the `expected` suffix from the end of the given string, returning
 // `true` if the strip operation succeeded or false otherwise.
 //
 // Example:
@@ -64,9 +64,9 @@
 
 // StripPrefix()
 //
-// Returns a view into the input std::string 'str' with the given 'prefix' removed,
-// but leaving the original std::string intact. If the prefix does not match at the
-// start of the std::string, returns the original std::string instead.
+// Returns a view into the input string 'str' with the given 'prefix' removed,
+// but leaving the original string intact. If the prefix does not match at the
+// start of the string, returns the original string instead.
 ABSL_MUST_USE_RESULT inline absl::string_view StripPrefix(
     absl::string_view str, absl::string_view prefix) {
   if (absl::StartsWith(str, prefix)) str.remove_prefix(prefix.size());
@@ -75,9 +75,9 @@
 
 // StripSuffix()
 //
-// Returns a view into the input std::string 'str' with the given 'suffix' removed,
-// but leaving the original std::string intact. If the suffix does not match at the
-// end of the std::string, returns the original std::string instead.
+// Returns a view into the input string 'str' with the given 'suffix' removed,
+// but leaving the original string intact. If the suffix does not match at the
+// end of the string, returns the original string instead.
 ABSL_MUST_USE_RESULT inline absl::string_view StripSuffix(
     absl::string_view str, absl::string_view suffix) {
   if (absl::EndsWith(str, suffix)) str.remove_suffix(suffix.size());
diff --git a/absl/strings/strip_test.cc b/absl/strings/strip_test.cc
index 205c160..40c4c60 100644
--- a/absl/strings/strip_test.cc
+++ b/absl/strings/strip_test.cc
@@ -12,8 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-// This file contains functions that remove a defined part from the std::string,
-// i.e., strip the std::string.
+// This file contains functions that remove a defined part from the string,
+// i.e., strip the string.
 
 #include "absl/strings/strip.h"
 
diff --git a/absl/strings/substitute.h b/absl/strings/substitute.h
index c4b25ba..4de7b4e 100644
--- a/absl/strings/substitute.h
+++ b/absl/strings/substitute.h
@@ -17,46 +17,46 @@
 // File: substitute.h
 // -----------------------------------------------------------------------------
 //
-// This package contains functions for efficiently performing std::string
-// substitutions using a format std::string with positional notation:
+// This package contains functions for efficiently performing string
+// substitutions using a format string with positional notation:
 // `Substitute()` and `SubstituteAndAppend()`.
 //
 // Unlike printf-style format specifiers, `Substitute()` functions do not need
 // to specify the type of the substitution arguments. Supported arguments
-// following the format std::string, such as strings, string_views, ints,
+// following the format string, such as strings, string_views, ints,
 // floats, and bools, are automatically converted to strings during the
 // substitution process. (See below for a full list of supported types.)
 //
 // `Substitute()` does not allow you to specify *how* to format a value, beyond
-// the default conversion to std::string. For example, you cannot format an integer
+// the default conversion to string. For example, you cannot format an integer
 // in hex.
 //
-// The format std::string uses positional identifiers indicated by a dollar sign ($)
+// The format string uses positional identifiers indicated by a dollar sign ($)
 // and single digit positional ids to indicate which substitution arguments to
-// use at that location within the format std::string.
+// use at that location within the format string.
 //
 // Example 1:
-//   std::string s = Substitute("$1 purchased $0 $2. Thanks $1!",
+//   string s = Substitute("$1 purchased $0 $2. Thanks $1!",
 //                         5, "Bob", "Apples");
 //   EXPECT_EQ("Bob purchased 5 Apples. Thanks Bob!", s);
 //
 // Example 2:
-//   std::string s = "Hi. ";
+//   string s = "Hi. ";
 //   SubstituteAndAppend(&s, "My name is $0 and I am $1 years old.", "Bob", 5);
 //   EXPECT_EQ("Hi. My name is Bob and I am 5 years old.", s);
 //
 //
 // Supported types:
-//   * absl::string_view, std::string, const char* (null is equivalent to "")
+//   * absl::string_view, string, const char* (null is equivalent to "")
 //   * int32_t, int64_t, uint32_t, uint64
 //   * float, double
 //   * bool (Printed as "true" or "false")
-//   * pointer types other than char* (Printed as "0x<lower case hex std::string>",
+//   * pointer types other than char* (Printed as "0x<lower case hex string>",
 //     except that null is printed as "NULL")
 //
-// If an invalid format std::string is provided, Substitute returns an empty std::string
-// and SubstituteAndAppend does not change the provided output std::string.
-// A format std::string is invalid if it:
+// If an invalid format string is provided, Substitute returns an empty string
+// and SubstituteAndAppend does not change the provided output string.
+// A format string is invalid if it:
 //   * ends in an unescaped $ character,
 //     e.g. "Hello $", or
 //   * calls for a position argument which is not provided,
@@ -88,7 +88,7 @@
 //
 // This class provides an argument type for `absl::Substitute()` and
 // `absl::SubstituteAndAppend()`. `Arg` handles implicit conversion of various
-// types to a std::string. (`Arg` is very similar to the `AlphaNum` class in
+// types to a string. (`Arg` is very similar to the `AlphaNum` class in
 // `StrCat()`.)
 //
 // This class has implicit constructors.
@@ -197,8 +197,8 @@
 
 // SubstituteAndAppend()
 //
-// Substitutes variables into a given format std::string and appends to a given
-// output std::string. See file comments above for usage.
+// Substitutes variables into a given format string and appends to a given
+// output string. See file comments above for usage.
 //
 // The declarations of `SubstituteAndAppend()` below consist of overloads
 // for passing 0 to 10 arguments, respectively.
@@ -444,7 +444,7 @@
 
 // Substitute()
 //
-// Substitutes variables into a given format std::string. See file comments above
+// Substitutes variables into a given format string. See file comments above
 // for usage.
 //
 // The declarations of `Substitute()` below consist of overloads for passing 0
@@ -456,7 +456,7 @@
 // Example:
 //  template <typename... Args>
 //  void VarMsg(absl::string_view format, const Args&... args) {
-//    std::string s = absl::Substitute(format, args...);
+//    string s = absl::Substitute(format, args...);
 
 ABSL_MUST_USE_RESULT inline std::string Substitute(absl::string_view format) {
   std::string result;
diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel
index 8d302e0..f52e9d4 100644
--- a/absl/synchronization/BUILD.bazel
+++ b/absl/synchronization/BUILD.bazel
@@ -88,6 +88,9 @@
     size = "small",
     srcs = ["barrier_test.cc"],
     copts = ABSL_TEST_COPTS,
+    tags = [
+        "no_test_wasm",
+    ],
     deps = [
         ":synchronization",
         "//absl/time",
@@ -100,6 +103,9 @@
     size = "small",
     srcs = ["blocking_counter_test.cc"],
     copts = ABSL_TEST_COPTS,
+    tags = [
+        "no_test_wasm",
+    ],
     deps = [
         ":synchronization",
         "//absl/time",
@@ -138,6 +144,9 @@
     name = "thread_pool",
     testonly = 1,
     hdrs = ["internal/thread_pool.h"],
+    visibility = [
+        "//absl:__subpackages__",
+    ],
     deps = [
         ":synchronization",
         "//absl/base:core_headers",
@@ -149,6 +158,7 @@
     size = "large",
     srcs = ["mutex_test.cc"],
     copts = ABSL_TEST_COPTS,
+    shard_count = 25,
     deps = [
         ":synchronization",
         ":thread_pool",
@@ -205,6 +215,7 @@
     name = "per_thread_sem_test",
     size = "medium",
     copts = ABSL_TEST_COPTS,
+    tags = ["no_test_wasm"],
     deps = [
         ":per_thread_sem_test_common",
         ":synchronization",
diff --git a/absl/synchronization/BUILD.gn b/absl/synchronization/BUILD.gn
index 3664aa1..e6f763a 100644
--- a/absl/synchronization/BUILD.gn
+++ b/absl/synchronization/BUILD.gn
@@ -50,8 +50,8 @@
     "internal/create_thread_identity.cc",
     "internal/per_thread_sem.cc",
     "internal/waiter.cc",
+    "mutex.cc",
     "notification.cc",
-    "mutex.cc"
   ]
   public = [
     "barrier.h",
@@ -93,6 +93,8 @@
     ":synchronization",
     "../base:core_headers",
   ]
+  visibility = []
+  visibility += [ "../*" ]
 }
 
 source_set("per_thread_sem_test_common") {
diff --git a/absl/synchronization/internal/graphcycles.cc b/absl/synchronization/internal/graphcycles.cc
index ab1f3f8..d3878de 100644
--- a/absl/synchronization/internal/graphcycles.cc
+++ b/absl/synchronization/internal/graphcycles.cc
@@ -204,8 +204,7 @@
   }
 
  private:
-  static const int32_t kEmpty;
-  static const int32_t kDel;
+  enum : int32_t { kEmpty = -1, kDel = -2 };
   Vec<int32_t> table_;
   uint32_t occupied_;     // Count of non-empty slots (includes deleted slots)
 
@@ -255,9 +254,6 @@
   NodeSet& operator=(const NodeSet&) = delete;
 };
 
-const int32_t NodeSet::kEmpty = -1;
-const int32_t NodeSet::kDel = -2;
-
 // We encode a node index and a node version in GraphId.  The version
 // number is incremented when the GraphId is freed which automatically
 // invalidates all copies of the GraphId.
diff --git a/absl/synchronization/internal/per_thread_sem_test.cc b/absl/synchronization/internal/per_thread_sem_test.cc
index 2b52ea7..c29d840 100644
--- a/absl/synchronization/internal/per_thread_sem_test.cc
+++ b/absl/synchronization/internal/per_thread_sem_test.cc
@@ -153,12 +153,15 @@
 
 TEST_F(PerThreadSemTest, Timeouts) {
   absl::Time timeout = absl::Now() + absl::Milliseconds(50);
+  // Allow for a slight early return, to account for quality of implementation
+  // issues on various platforms.
+  const absl::Duration slop = absl::Microseconds(200);
   EXPECT_FALSE(Wait(timeout));
-  EXPECT_LE(timeout, absl::Now());
+  EXPECT_LE(timeout, absl::Now() + slop);
 
   absl::Time negative_timeout = absl::UnixEpoch() - absl::Milliseconds(100);
   EXPECT_FALSE(Wait(negative_timeout));
-  EXPECT_LE(negative_timeout, absl::Now());  // trivially true :)
+  EXPECT_LE(negative_timeout, absl::Now() + slop);  // trivially true :)
 
   Post(GetOrCreateCurrentThreadIdentity());
   // The wait here has an expired timeout, but we have a wake to consume,
diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc
index 80f34f0..9d59a79 100644
--- a/absl/synchronization/mutex.cc
+++ b/absl/synchronization/mutex.cc
@@ -298,7 +298,7 @@
 // set "bits" in the word there (waiting until lockbit is clear before doing
 // so), and return a refcounted reference that will remain valid until
 // UnrefSynchEvent() is called.  If a new SynchEvent is allocated,
-// the std::string name is copied into it.
+// the string name is copied into it.
 // When used with a mutex, the caller should also ensure that kMuEvent
 // is set in the mutex word, and similarly for condition variables and kCVEvent.
 static SynchEvent *EnsureSynchEvent(std::atomic<intptr_t> *addr,
@@ -1827,8 +1827,8 @@
          cond == nullptr || EvalConditionAnnotated(cond, this, true, how);
 }
 
-// RAW_CHECK_FMT() takes a condition, a printf-style format std::string, and
-// the printf-style argument list.   The format std::string must be a literal.
+// RAW_CHECK_FMT() takes a condition, a printf-style format string, and
+// the printf-style argument list.   The format string must be a literal.
 // Arguments after the first are not evaluated unless the condition is true.
 #define RAW_CHECK_FMT(cond, ...)                                   \
   do {                                                             \
@@ -1975,7 +1975,7 @@
 // Unlock this mutex, which is held by the current thread.
 // If waitp is non-zero, it must be the wait parameters for the current thread
 // which holds the lock but is not runnable because its condition is false
-// or it n the process of blocking on a condition variable; it must requeue
+// or it is in the process of blocking on a condition variable; it must requeue
 // itself on the mutex/condvar to wait for its condition to become true.
 void Mutex::UnlockSlow(SynchWaitParams *waitp) {
   intptr_t v = mu_.load(std::memory_order_relaxed);
diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h
index 83c2148..a378190 100644
--- a/absl/synchronization/mutex.h
+++ b/absl/synchronization/mutex.h
@@ -962,7 +962,7 @@
 //
 // The function pointer registered here will be called here on various CondVar
 // events.  The callback is given an opaque handle to the CondVar object and
-// a std::string identifying the event.  This is thread-safe, but only a single
+// a string identifying the event.  This is thread-safe, but only a single
 // tracer can be registered.
 //
 // Events that can be sent are "Wait", "Unwait", "Signal wakeup", and
diff --git a/absl/synchronization/notification_test.cc b/absl/synchronization/notification_test.cc
index 9b3b6a5..d8708d5 100644
--- a/absl/synchronization/notification_test.cc
+++ b/absl/synchronization/notification_test.cc
@@ -71,10 +71,13 @@
       notification->WaitForNotificationWithTimeout(absl::Milliseconds(0)));
   EXPECT_FALSE(notification->WaitForNotificationWithDeadline(absl::Now()));
 
+  const absl::Duration delay = absl::Milliseconds(50);
+  // Allow for a slight early return, to account for quality of implementation
+  // issues on various platforms.
+  const absl::Duration slop = absl::Microseconds(200);
   absl::Time start = absl::Now();
-  EXPECT_FALSE(
-      notification->WaitForNotificationWithTimeout(absl::Milliseconds(50)));
-  EXPECT_LE(start + absl::Milliseconds(50), absl::Now());
+  EXPECT_FALSE(notification->WaitForNotificationWithTimeout(delay));
+  EXPECT_LE(start + delay, absl::Now() + slop);
 
   ThreadSafeCounter ready_counter;
   ThreadSafeCounter done_counter;
diff --git a/absl/time/BUILD.gn b/absl/time/BUILD.gn
index 9927af8..5758e9d 100644
--- a/absl/time/BUILD.gn
+++ b/absl/time/BUILD.gn
@@ -62,8 +62,9 @@
     ":time",
     "../base",
     "../time/internal/cctz:time_zone",
-    "//testing/gtest",
     "//testing/gmock",
+    "//testing/gtest",
+    "//third_party/googletest/:gmock",
   ]
   visibility = []
   visibility += [ "../time:*" ]
diff --git a/absl/time/duration.cc b/absl/time/duration.cc
index f402137..2950c7c 100644
--- a/absl/time/duration.cc
+++ b/absl/time/duration.cc
@@ -666,7 +666,7 @@
 }
 
 //
-// To/From std::string formatting.
+// To/From string formatting.
 //
 
 namespace {
@@ -744,7 +744,7 @@
 }  // namespace
 
 // From Go's doc at http://golang.org/pkg/time/#Duration.String
-//   [FormatDuration] returns a std::string representing the duration in the
+//   [FormatDuration] returns a string representing the duration in the
 //   form "72h3m0.5s".  Leading zero units are omitted.  As a special
 //   case, durations less than one second format use a smaller unit
 //   (milli-, micro-, or nanoseconds) to ensure that the leading digit
@@ -787,8 +787,8 @@
 namespace {
 
 // A helper for ParseDuration() that parses a leading number from the given
-// std::string and stores the result in *int_part/*frac_part/*frac_scale.  The
-// given std::string pointer is modified to point to the first unconsumed char.
+// string and stores the result in *int_part/*frac_part/*frac_scale.  The
+// given string pointer is modified to point to the first unconsumed char.
 bool ConsumeDurationNumber(const char** dpp, int64_t* int_part,
                            int64_t* frac_part, int64_t* frac_scale) {
   *int_part = 0;
@@ -816,8 +816,8 @@
 }
 
 // A helper for ParseDuration() that parses a leading unit designator (e.g.,
-// ns, us, ms, s, m, h) from the given std::string and stores the resulting unit
-// in "*unit".  The given std::string pointer is modified to point to the first
+// ns, us, ms, s, m, h) from the given string and stores the resulting unit
+// in "*unit".  The given string pointer is modified to point to the first
 // unconsumed char.
 bool ConsumeDurationUnit(const char** start, Duration* unit) {
   const char *s = *start;
@@ -850,7 +850,7 @@
 }  // namespace
 
 // From Go's doc at http://golang.org/pkg/time/#ParseDuration
-//   [ParseDuration] parses a duration std::string.  A duration std::string is
+//   [ParseDuration] parses a duration string.  A duration string is
 //   a possibly signed sequence of decimal numbers, each with optional
 //   fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
 //   Valid time units are "ns", "us" "ms", "s", "m", "h".
diff --git a/absl/time/format.cc b/absl/time/format.cc
index e98e60a..ee597e4 100644
--- a/absl/time/format.cc
+++ b/absl/time/format.cc
@@ -88,7 +88,7 @@
   return absl::ParseTime(format, input, absl::UTCTimeZone(), time, err);
 }
 
-// If the input std::string does not contain an explicit UTC offset, interpret
+// If the input string does not contain an explicit UTC offset, interpret
 // the fields with respect to the given TimeZone.
 bool ParseTime(const std::string& format, const std::string& input, absl::TimeZone tz,
                absl::Time* time, std::string* err) {
diff --git a/absl/time/internal/cctz/include/cctz/civil_time.h b/absl/time/internal/cctz/include/cctz/civil_time.h
index 898222b..0842fa4 100644
--- a/absl/time/internal/cctz/include/cctz/civil_time.h
+++ b/absl/time/internal/cctz/include/cctz/civil_time.h
@@ -59,7 +59,7 @@
 // inferior fields to their minimum valid value (as described above). The
 // following are examples of how each of the six types would align the fields
 // representing November 22, 2015 at 12:34:56 in the afternoon. (Note: the
-// std::string format used here is not important; it's just a shorthand way of
+// string format used here is not important; it's just a shorthand way of
 // showing the six YMDHMS fields.)
 //
 //   civil_second  2015-11-22 12:34:56
diff --git a/absl/time/internal/cctz/include/cctz/civil_time_detail.h b/absl/time/internal/cctz/include/cctz/civil_time_detail.h
index 2362a4f..d7f7271 100644
--- a/absl/time/internal/cctz/include/cctz/civil_time_detail.h
+++ b/absl/time/internal/cctz/include/cctz/civil_time_detail.h
@@ -355,11 +355,11 @@
       : civil_time(ct.f_) {}
 
   // Factories for the maximum/minimum representable civil_time.
-  static civil_time max() {
+  static CONSTEXPR_F civil_time max() {
     const auto max_year = std::numeric_limits<std::int_least64_t>::max();
     return civil_time(max_year, 12, 31, 23, 59, 59);
   }
-  static civil_time min() {
+  static CONSTEXPR_F civil_time min() {
     const auto min_year = std::numeric_limits<std::int_least64_t>::min();
     return civil_time(min_year, 1, 1, 0, 0, 0);
   }
@@ -416,6 +416,12 @@
     return difference(T{}, lhs.f_, rhs.f_);
   }
 
+  template <typename H>
+  friend H AbslHashValue(H h, civil_time a) {
+    return H::combine(std::move(h), a.f_.y, a.f_.m, a.f_.d,
+                                    a.f_.hh, a.f_.mm, a.f_.ss);
+  }
+
  private:
   // All instantiations of this template are allowed to call the following
   // private constructor and access the private fields member.
@@ -508,12 +514,8 @@
   CONSTEXPR_D int k_weekday_offsets[1 + 12] = {
       -1, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4,
   };
-  year_t wd = cd.year() - (cd.month() < 3);
-  if (wd >= 0) {
-    wd += wd / 4 - wd / 100 + wd / 400;
-  } else {
-    wd += (wd - 3) / 4 - (wd - 99) / 100 + (wd - 399) / 400;
-  }
+  year_t wd = 2400 + (cd.year() % 400) - (cd.month() < 3);
+  wd += wd / 4 - wd / 100 + wd / 400;
   wd += k_weekday_offsets[cd.month()] + cd.day();
   return k_weekday_by_sun_off[(wd % 7 + 7) % 7];
 }
diff --git a/absl/time/internal/cctz/include/cctz/time_zone.h b/absl/time/internal/cctz/include/cctz/time_zone.h
index 0b9764e..f28dad1 100644
--- a/absl/time/internal/cctz/include/cctz/time_zone.h
+++ b/absl/time/internal/cctz/include/cctz/time_zone.h
@@ -224,6 +224,11 @@
     return !(lhs == rhs);
   }
 
+  template <typename H>
+  friend H AbslHashValue(H h, time_zone tz) {
+    return H::combine(std::move(h), &tz.effective_impl());
+  }
+
   class Impl;
 
  private:
@@ -279,7 +284,7 @@
 }  // namespace detail
 
 // Formats the given time_point in the given cctz::time_zone according to
-// the provided format std::string. Uses strftime()-like formatting options,
+// the provided format string. Uses strftime()-like formatting options,
 // with the following extensions:
 //
 //   - %Ez  - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm)
@@ -298,7 +303,7 @@
 // more than four characters, just like %Y.
 //
 // Tip: Format strings should include the UTC offset (e.g., %z, %Ez, or %E*z)
-// so that the resulting std::string uniquely identifies an absolute time.
+// so that the resulting string uniquely identifies an absolute time.
 //
 // Example:
 //   cctz::time_zone lax;
@@ -314,7 +319,7 @@
   return detail::format(fmt, p.first, n, tz);
 }
 
-// Parses an input std::string according to the provided format std::string and
+// Parses an input string according to the provided format string and
 // returns the corresponding time_point. Uses strftime()-like formatting
 // options, with the same extensions as cctz::format(), but with the
 // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez
@@ -328,7 +333,7 @@
 //
 //   "1970-01-01 00:00:00.0 +0000"
 //
-// For example, parsing a std::string of "15:45" (%H:%M) will return a time_point
+// For example, parsing a string of "15:45" (%H:%M) will return a time_point
 // that represents "1970-01-01 15:45:00.0 +0000".
 //
 // Note that parse() returns time instants, so it makes most sense to parse
diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc
index c97df78..4498d7d 100644
--- a/absl/time/internal/cctz/src/cctz_benchmark.cc
+++ b/absl/time/internal/cctz/src/cctz_benchmark.cc
@@ -778,13 +778,13 @@
 }
 BENCHMARK(BM_Zone_UTCTimeZone);
 
-// In each "ToDateTime" benchmark we switch between two instants
-// separated by at least one transition in order to defeat any
-// internal caching of previous results (e.g., see local_time_hint_).
+// In each "ToCivil" benchmark we switch between two instants separated
+// by at least one transition in order to defeat any internal caching of
+// previous results (e.g., see local_time_hint_).
 //
 // The "UTC" variants use UTC instead of the Google/local time zone.
 
-void BM_Time_ToDateTime_CCTZ(benchmark::State& state) {
+void BM_Time_ToCivil_CCTZ(benchmark::State& state) {
   const cctz::time_zone tz = TestTimeZone();
   std::chrono::system_clock::time_point tp =
       std::chrono::system_clock::from_time_t(1384569027);
@@ -796,9 +796,9 @@
     benchmark::DoNotOptimize(cctz::convert(tp, tz));
   }
 }
-BENCHMARK(BM_Time_ToDateTime_CCTZ);
+BENCHMARK(BM_Time_ToCivil_CCTZ);
 
-void BM_Time_ToDateTime_Libc(benchmark::State& state) {
+void BM_Time_ToCivil_Libc(benchmark::State& state) {
   // No timezone support, so just use localtime.
   time_t t = 1384569027;
   time_t t2 = 1418962578;
@@ -813,9 +813,9 @@
 #endif
   }
 }
-BENCHMARK(BM_Time_ToDateTime_Libc);
+BENCHMARK(BM_Time_ToCivil_Libc);
 
-void BM_Time_ToDateTimeUTC_CCTZ(benchmark::State& state) {
+void BM_Time_ToCivilUTC_CCTZ(benchmark::State& state) {
   const cctz::time_zone tz = cctz::utc_time_zone();
   std::chrono::system_clock::time_point tp =
       std::chrono::system_clock::from_time_t(1384569027);
@@ -824,9 +824,9 @@
     benchmark::DoNotOptimize(cctz::convert(tp, tz));
   }
 }
-BENCHMARK(BM_Time_ToDateTimeUTC_CCTZ);
+BENCHMARK(BM_Time_ToCivilUTC_CCTZ);
 
-void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) {
+void BM_Time_ToCivilUTC_Libc(benchmark::State& state) {
   time_t t = 1384569027;
   struct tm tm;
   while (state.KeepRunning()) {
@@ -838,16 +838,16 @@
 #endif
   }
 }
-BENCHMARK(BM_Time_ToDateTimeUTC_Libc);
+BENCHMARK(BM_Time_ToCivilUTC_Libc);
 
-// In each "FromDateTime" benchmark we switch between two YMDhms
-// values separated by at least one transition in order to defeat any
-// internal caching of previous results (e.g., see time_local_hint_).
+// In each "FromCivil" benchmark we switch between two YMDhms values
+// separated by at least one transition in order to defeat any internal
+// caching of previous results (e.g., see time_local_hint_).
 //
 // The "UTC" variants use UTC instead of the Google/local time zone.
 // The "Day0" variants require normalization of the day of month.
 
-void BM_Time_FromDateTime_CCTZ(benchmark::State& state) {
+void BM_Time_FromCivil_CCTZ(benchmark::State& state) {
   const cctz::time_zone tz = TestTimeZone();
   int i = 0;
   while (state.KeepRunning()) {
@@ -860,9 +860,9 @@
     }
   }
 }
-BENCHMARK(BM_Time_FromDateTime_CCTZ);
+BENCHMARK(BM_Time_FromCivil_CCTZ);
 
-void BM_Time_FromDateTime_Libc(benchmark::State& state) {
+void BM_Time_FromCivil_Libc(benchmark::State& state) {
   // No timezone support, so just use localtime.
   int i = 0;
   while (state.KeepRunning()) {
@@ -886,20 +886,20 @@
     benchmark::DoNotOptimize(mktime(&tm));
   }
 }
-BENCHMARK(BM_Time_FromDateTime_Libc);
+BENCHMARK(BM_Time_FromCivil_Libc);
 
-void BM_Time_FromDateTimeUTC_CCTZ(benchmark::State& state) {
+void BM_Time_FromCivilUTC_CCTZ(benchmark::State& state) {
   const cctz::time_zone tz = cctz::utc_time_zone();
   while (state.KeepRunning()) {
     benchmark::DoNotOptimize(
         cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz));
   }
 }
-BENCHMARK(BM_Time_FromDateTimeUTC_CCTZ);
+BENCHMARK(BM_Time_FromCivilUTC_CCTZ);
 
-// There is no BM_Time_FromDateTimeUTC_Libc.
+// There is no BM_Time_FromCivilUTC_Libc.
 
-void BM_Time_FromDateTimeDay0_CCTZ(benchmark::State& state) {
+void BM_Time_FromCivilDay0_CCTZ(benchmark::State& state) {
   const cctz::time_zone tz = TestTimeZone();
   int i = 0;
   while (state.KeepRunning()) {
@@ -912,9 +912,9 @@
     }
   }
 }
-BENCHMARK(BM_Time_FromDateTimeDay0_CCTZ);
+BENCHMARK(BM_Time_FromCivilDay0_CCTZ);
 
-void BM_Time_FromDateTimeDay0_Libc(benchmark::State& state) {
+void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
   // No timezone support, so just use localtime.
   int i = 0;
   while (state.KeepRunning()) {
@@ -938,7 +938,7 @@
     benchmark::DoNotOptimize(mktime(&tm));
   }
 }
-BENCHMARK(BM_Time_FromDateTimeDay0_Libc);
+BENCHMARK(BM_Time_FromCivilDay0_Libc);
 
 const char* const kFormats[] = {
     RFC1123_full,         // 0
diff --git a/absl/time/internal/cctz/src/civil_time_test.cc b/absl/time/internal/cctz/src/civil_time_test.cc
index f6648c8..faffde4 100644
--- a/absl/time/internal/cctz/src/civil_time_test.cc
+++ b/absl/time/internal/cctz/src/civil_time_test.cc
@@ -620,7 +620,7 @@
   TEST_RELATIONAL(civil_second(2014, 1, 1, 1, 1, 0),
                   civil_second(2014, 1, 1, 1, 1, 1));
 
-  // Tests the relational operators of two different CivilTime types.
+  // Tests the relational operators of two different civil-time types.
   TEST_RELATIONAL(civil_day(2014, 1, 1), civil_minute(2014, 1, 1, 1, 1));
   TEST_RELATIONAL(civil_day(2014, 1, 1), civil_month(2014, 2));
 
diff --git a/absl/time/internal/cctz/src/time_zone_fixed.cc b/absl/time/internal/cctz/src/time_zone_fixed.cc
index 598b08f..db9a475 100644
--- a/absl/time/internal/cctz/src/time_zone_fixed.cc
+++ b/absl/time/internal/cctz/src/time_zone_fixed.cc
@@ -15,8 +15,8 @@
 #include "time_zone_fixed.h"
 
 #include <algorithm>
+#include <cassert>
 #include <chrono>
-#include <cstdio>
 #include <cstring>
 #include <string>
 
@@ -29,8 +29,15 @@
 // The prefix used for the internal names of fixed-offset zones.
 const char kFixedOffsetPrefix[] = "Fixed/UTC";
 
+const char kDigits[] = "0123456789";
+
+char* Format02d(char* p, int v) {
+  *p++ = kDigits[(v / 10) % 10];
+  *p++ = kDigits[v % 10];
+  return p;
+}
+
 int Parse02d(const char* p) {
-  static const char kDigits[] = "0123456789";
   if (const char* ap = std::strchr(kDigits, *p)) {
     int v = static_cast<int>(ap - kDigits);
     if (const char* bp = std::strchr(kDigits, *++p)) {
@@ -95,9 +102,17 @@
   }
   int hours = minutes / 60;
   minutes %= 60;
-  char buf[sizeof(kFixedOffsetPrefix) + sizeof("-24:00:00")];
-  snprintf(buf, sizeof(buf), "%s%c%02d:%02d:%02d",
-           kFixedOffsetPrefix, sign, hours, minutes, seconds);
+  char buf[sizeof(kFixedOffsetPrefix) - 1 + sizeof("-24:00:00")];
+  std::strcpy(buf, kFixedOffsetPrefix);
+  char* ep = buf + sizeof(kFixedOffsetPrefix) - 1;
+  *ep++ = sign;
+  ep = Format02d(ep, hours);
+  *ep++ = ':';
+  ep = Format02d(ep, minutes);
+  *ep++ = ':';
+  ep = Format02d(ep, seconds);
+  *ep++ = '\0';
+  assert(ep == buf + sizeof(buf));
   return buf;
 }
 
diff --git a/absl/time/internal/cctz/src/time_zone_format.cc b/absl/time/internal/cctz/src/time_zone_format.cc
index 1b02384..a02b1e3 100644
--- a/absl/time/internal/cctz/src/time_zone_format.cc
+++ b/absl/time/internal/cctz/src/time_zone_format.cc
@@ -533,7 +533,7 @@
   return dp;
 }
 
-// Parses a std::string into a std::tm using strptime(3).
+// Parses a string into a std::tm using strptime(3).
 const char* ParseTM(const char* dp, const char* fmt, std::tm* tm) {
   if (dp != nullptr) {
     dp = strptime(dp, fmt, tm);
@@ -743,7 +743,7 @@
     data = ParseTM(data, spec.c_str(), &tm);
 
     // If we successfully parsed %p we need to remember whether the result
-    // was AM or PM so that we can adjust tm_hour before ConvertDateTime().
+    // was AM or PM so that we can adjust tm_hour before time_zone::lookup().
     // So reparse the input with a known AM hour, and check if it is shifted
     // to a PM hour.
     if (spec == "%p" && data != nullptr) {
diff --git a/absl/time/internal/cctz/src/time_zone_format_test.cc b/absl/time/internal/cctz/src/time_zone_format_test.cc
index a90dda7..6b9928e 100644
--- a/absl/time/internal/cctz/src/time_zone_format_test.cc
+++ b/absl/time/internal/cctz/src/time_zone_format_test.cc
@@ -669,13 +669,13 @@
                     utc_time_zone(), &tp));
   ExpectTime(tp, tz, 2013, 6, 28, 19 - 8 - 7, 8, 9, -7 * 60 * 60, true, "PDT");
 
-  // Check a skipped time (a Spring DST transition).  parse() returns
-  // the preferred-offset result, as defined for ConvertDateTime().
+  // Check a skipped time (a Spring DST transition). parse() uses the
+  // pre-transition offset.
   EXPECT_TRUE(parse("%Y-%m-%d %H:%M:%S", "2011-03-13 02:15:00", tz, &tp));
   ExpectTime(tp, tz, 2011, 3, 13, 3, 15, 0, -7 * 60 * 60, true, "PDT");
 
-  // Check a repeated time (a Fall DST transition).  parse() returns
-  // the preferred-offset result, as defined for ConvertDateTime().
+  // Check a repeated time (a Fall DST transition).  parse() uses the
+  // pre-transition offset.
   EXPECT_TRUE(parse("%Y-%m-%d %H:%M:%S", "2011-11-06 01:15:00", tz, &tp));
   ExpectTime(tp, tz, 2011, 11, 6, 1, 15, 0, -7 * 60 * 60, true, "PDT");
 }
diff --git a/absl/time/internal/cctz/src/time_zone_info.cc b/absl/time/internal/cctz/src/time_zone_info.cc
index bf73635..2cb358d 100644
--- a/absl/time/internal/cctz/src/time_zone_info.cc
+++ b/absl/time/internal/cctz/src/time_zone_info.cc
@@ -286,7 +286,7 @@
   return true;
 }
 
-// Use the POSIX-TZ-environment-variable-style std::string to handle times
+// Use the POSIX-TZ-environment-variable-style string to handle times
 // in years after the last transition stored in the zoneinfo data.
 void TimeZoneInfo::ExtendTransitions(const std::string& name,
                                      const Header& hdr) {
diff --git a/absl/time/internal/cctz/src/time_zone_posix.h b/absl/time/internal/cctz/src/time_zone_posix.h
index 6619f27..9ccd4a8 100644
--- a/absl/time/internal/cctz/src/time_zone_posix.h
+++ b/absl/time/internal/cctz/src/time_zone_posix.h
@@ -89,7 +89,7 @@
   } time;
 };
 
-// The entirety of a POSIX-std::string specified time-zone rule. The standard
+// The entirety of a POSIX-string specified time-zone rule. The standard
 // abbreviation and offset are always given. If the time zone includes
 // daylight saving, then the daylight abbrevation is non-empty and the
 // remaining fields are also valid. Note that the start/end transitions
diff --git a/absl/time/time.h b/absl/time/time.h
index c41cb89..50bf971 100644
--- a/absl/time/time.h
+++ b/absl/time/time.h
@@ -25,11 +25,12 @@
 //  * `absl::TimeZone` defines geopolitical time zone regions (as collected
 //     within the IANA Time Zone database (https://www.iana.org/time-zones)).
 //
+//
 // Example:
 //
 //   absl::TimeZone nyc;
 //
-//   // LoadTimeZone may fail so it's always better to check for success.
+//   // LoadTimeZone() may fail so it's always better to check for success.
 //   if (!absl::LoadTimeZone("America/New_York", &nyc)) {
 //      // handle error case
 //   }
@@ -162,6 +163,11 @@
   Duration& operator*=(float r) { return *this *= static_cast<double>(r); }
   Duration& operator/=(float r) { return *this /= static_cast<double>(r); }
 
+  template <typename H>
+  friend H AbslHashValue(H h, Duration d) {
+    return H::combine(std::move(h), d.rep_hi_, d.rep_lo_);
+  }
+
  private:
   friend constexpr int64_t time_internal::GetRepHi(Duration d);
   friend constexpr uint32_t time_internal::GetRepLo(Duration d);
@@ -321,6 +327,9 @@
 //   0 == d / inf
 //   INT64_MAX == inf / d
 //
+//   d < inf
+//   -inf < d
+//
 //   // Division by zero returns infinity, or INT64_MIN/MAX where appropriate.
 //   inf == d / 0
 //   INT64_MAX == d / absl::ZeroDuration()
@@ -481,7 +490,7 @@
 
 // FormatDuration()
 //
-// Returns a std::string representing the duration in the form "72h3m0.5s".
+// Returns a string representing the duration in the form "72h3m0.5s".
 // Returns "inf" or "-inf" for +/- `InfiniteDuration()`.
 std::string FormatDuration(Duration d);
 
@@ -492,11 +501,11 @@
 
 // ParseDuration()
 //
-// Parses a duration std::string consisting of a possibly signed sequence of
+// Parses a duration string consisting of a possibly signed sequence of
 // decimal numbers, each with an optional fractional part and a unit
 // suffix.  The valid suffixes are "ns", "us" "ms", "s", "m", and "h".
 // Simple examples include "300ms", "-1.5h", and "2h45m".  Parses "0" as
-// `ZeroDuration()`.  Parses "inf" and "-inf" as +/- `InfiniteDuration()`.
+// `ZeroDuration()`. Parses "inf" and "-inf" as +/- `InfiniteDuration()`.
 bool ParseDuration(const std::string& dur_string, Duration* d);
 
 // Support for flag values of type Duration. Duration flags must be specified
@@ -607,6 +616,11 @@
   // Returns the breakdown of this instant in the given TimeZone.
   Breakdown In(TimeZone tz) const;
 
+  template <typename H>
+  friend H AbslHashValue(H h, Time t) {
+    return H::combine(std::move(h), t.rep_);
+  }
+
  private:
   friend constexpr Time time_internal::FromUnixDuration(Duration d);
   friend constexpr Duration time_internal::ToUnixDuration(Time t);
@@ -689,7 +703,9 @@
 // Examples:
 //
 //   absl::TimeZone lax;
-//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { ... }
+//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
+//     // handle error case
+//   }
 //
 //   // A unique civil time
 //   absl::TimeConversion jan01 =
@@ -759,7 +775,9 @@
 // Example:
 //
 //   absl::TimeZone seattle;
-//   if (!absl::LoadTimeZone("America/Los_Angeles", &seattle)) { ... }
+//   if (!absl::LoadTimeZone("America/Los_Angeles", &seattle)) {
+//     // handle error case
+//   }
 //   absl::Time t =  absl::FromDateTime(2017, 9, 26, 9, 30, 0, seattle);
 Time FromDateTime(int64_t year, int mon, int day, int hour, int min, int sec,
                   TimeZone tz);
@@ -871,8 +889,10 @@
 // FormatTime()/ParseTime() format specifiers for RFC3339 date/time strings,
 // with trailing zeros trimmed or with fractional seconds omitted altogether.
 //
-// Note that RFC3339_sec[] matches an ISO 8601 extended format for date
-// and time with UTC offset.
+// Note that RFC3339_sec[] matches an ISO 8601 extended format for date and
+// time with UTC offset.  Also note the use of "%Y": RFC3339 mandates that
+// years have exactly four digits, but we allow them to take their natural
+// width.
 extern const char RFC3339_full[];  // %Y-%m-%dT%H:%M:%E*S%Ez
 extern const char RFC3339_sec[];   // %Y-%m-%dT%H:%M:%S%Ez
 
@@ -886,7 +906,7 @@
 // FormatTime()
 //
 // Formats the given `absl::Time` in the `absl::TimeZone` according to the
-// provided format std::string. Uses strftime()-like formatting options, with
+// provided format string. Uses strftime()-like formatting options, with
 // the following extensions:
 //
 //   - %Ez  - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm)
@@ -910,16 +930,18 @@
 // Example:
 //
 //   absl::TimeZone lax;
-//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { ... }
+//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
+//     // handle error case
+//   }
 //   absl::Time t = absl::FromDateTime(2013, 1, 2, 3, 4, 5, lax);
 //
-//   std::string f = absl::FormatTime("%H:%M:%S", t, lax);  // "03:04:05"
+//   string f = absl::FormatTime("%H:%M:%S", t, lax);  // "03:04:05"
 //   f = absl::FormatTime("%H:%M:%E3S", t, lax);  // "03:04:05.000"
 //
 // Note: If the given `absl::Time` is `absl::InfiniteFuture()`, the returned
-// std::string will be exactly "infinite-future". If the given `absl::Time` is
-// `absl::InfinitePast()`, the returned std::string will be exactly "infinite-past".
-// In both cases the given format std::string and `absl::TimeZone` are ignored.
+// string will be exactly "infinite-future". If the given `absl::Time` is
+// `absl::InfinitePast()`, the returned string will be exactly "infinite-past".
+// In both cases the given format string and `absl::TimeZone` are ignored.
 //
 std::string FormatTime(const std::string& format, Time t, TimeZone tz);
 
@@ -936,7 +958,7 @@
 
 // ParseTime()
 //
-// Parses an input std::string according to the provided format std::string and
+// Parses an input string according to the provided format string and
 // returns the corresponding `absl::Time`. Uses strftime()-like formatting
 // options, with the same extensions as FormatTime(), but with the
 // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f.  %Ez
@@ -950,7 +972,7 @@
 //
 //   "1970-01-01 00:00:00.0 +0000"
 //
-// For example, parsing a std::string of "15:45" (%H:%M) will return an absl::Time
+// For example, parsing a string of "15:45" (%H:%M) will return an absl::Time
 // that represents "1970-01-01 15:45:00.0 +0000".
 //
 // Note that since ParseTime() returns time instants, it makes the most sense
@@ -977,15 +999,15 @@
 // Errors are indicated by returning false and assigning an error message
 // to the "err" out param if it is non-null.
 //
-// Note: If the input std::string is exactly "infinite-future", the returned
+// Note: If the input string is exactly "infinite-future", the returned
 // `absl::Time` will be `absl::InfiniteFuture()` and `true` will be returned.
-// If the input std::string is "infinite-past", the returned `absl::Time` will be
+// If the input string is "infinite-past", the returned `absl::Time` will be
 // `absl::InfinitePast()` and `true` will be returned.
 //
 bool ParseTime(const std::string& format, const std::string& input, Time* time,
                std::string* err);
 
-// Like ParseTime() above, but if the format std::string does not contain a UTC
+// Like ParseTime() above, but if the format string does not contain a UTC
 // offset specification (%z/%Ez/%E*z) then the input is interpreted in the
 // given TimeZone.  This means that the input, by itself, does not identify a
 // unique instant.  Being time-zone dependent, it also admits the possibility
@@ -1028,7 +1050,9 @@
 //   absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60);
 //   absl::TimeZone loc = absl::LocalTimeZone();
 //   absl::TimeZone lax;
-//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { ... }
+//   if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) {
+//     // handle error case
+//   }
 //
 // See also:
 // - https://github.com/google/cctz
@@ -1045,6 +1069,11 @@
 
   std::string name() const { return cz_.name(); }
 
+  template <typename H>
+  friend H AbslHashValue(H h, TimeZone tz) {
+    return H::combine(std::move(h), tz.cz_);
+  }
+
  private:
   friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; }
   friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; }
diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel
index 096c119..32f690c 100644
--- a/absl/types/BUILD.bazel
+++ b/absl/types/BUILD.bazel
@@ -19,6 +19,7 @@
     "ABSL_DEFAULT_COPTS",
     "ABSL_TEST_COPTS",
     "ABSL_EXCEPTIONS_FLAG",
+    "ABSL_EXCEPTIONS_FLAG_LINKOPTS",
 )
 
 package(default_visibility = ["//visibility:public"])
@@ -55,6 +56,7 @@
         "bad_any_cast.h",
     ],
     copts = ABSL_EXCEPTIONS_FLAG + ABSL_DEFAULT_COPTS,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     visibility = ["//visibility:private"],
     deps = [
         "//absl/base",
@@ -69,6 +71,7 @@
         "any_test.cc",
     ],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":any",
         "//absl/base",
@@ -100,6 +103,7 @@
     name = "any_exception_safety_test",
     srcs = ["any_exception_safety_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":any",
         "//absl/base:exception_safety_testing",
@@ -124,6 +128,7 @@
     size = "small",
     srcs = ["span_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":span",
         "//absl/base:config",
@@ -131,6 +136,7 @@
         "//absl/base:exception_testing",
         "//absl/container:fixed_array",
         "//absl/container:inlined_vector",
+        "//absl/hash:hash_testing",
         "//absl/strings",
         "@com_google_googletest//:gtest_main",
     ],
@@ -148,6 +154,7 @@
         "//absl/base:exception_testing",
         "//absl/container:fixed_array",
         "//absl/container:inlined_vector",
+        "//absl/hash:hash_testing",
         "//absl/strings",
         "@com_google_googletest//:gtest_main",
     ],
@@ -172,6 +179,7 @@
     srcs = ["bad_optional_access.cc"],
     hdrs = ["bad_optional_access.h"],
     copts = ABSL_DEFAULT_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         "//absl/base",
         "//absl/base:config",
@@ -183,6 +191,7 @@
     srcs = ["bad_variant_access.cc"],
     hdrs = ["bad_variant_access.h"],
     copts = ABSL_EXCEPTIONS_FLAG + ABSL_DEFAULT_COPTS,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         "//absl/base",
         "//absl/base:config",
@@ -196,6 +205,7 @@
         "optional_test.cc",
     ],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":optional",
         "//absl/base",
@@ -212,6 +222,7 @@
         "optional_exception_safety_test.cc",
     ],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":optional",
         "//absl/base:exception_safety_testing",
@@ -239,6 +250,7 @@
     size = "small",
     srcs = ["variant_test.cc"],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":variant",
         "//absl/base:config",
@@ -271,6 +283,7 @@
         "variant_exception_safety_test.cc",
     ],
     copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG,
+    linkopts = ABSL_EXCEPTIONS_FLAG_LINKOPTS,
     deps = [
         ":variant",
         "//absl/base:exception_safety_testing",
diff --git a/absl/types/BUILD.gn b/absl/types/BUILD.gn
index fd7e89d..bfdafb3 100644
--- a/absl/types/BUILD.gn
+++ b/absl/types/BUILD.gn
@@ -104,6 +104,7 @@
   deps = [
     ":bad_optional_access",
     "../base:config",
+    "../base:core_headers",
     "../memory",
     "../meta:type_traits",
     "../utility",
diff --git a/absl/types/any_exception_safety_test.cc b/absl/types/any_exception_safety_test.cc
index 36955f6..f9dd8c4 100644
--- a/absl/types/any_exception_safety_test.cc
+++ b/absl/types/any_exception_safety_test.cc
@@ -62,7 +62,7 @@
     static_cast<void>(unused);
     return AssertionFailure()
            << "A reset `any` should not be able to be any_cast";
-  } catch (absl::bad_any_cast) {
+  } catch (const absl::bad_any_cast&) {
   } catch (...) {
     return AssertionFailure()
            << "Unexpected exception thrown from absl::any_cast";
@@ -107,7 +107,7 @@
   };
   auto any_strong_tester = testing::MakeExceptionSafetyTester()
                                .WithInitialValue(original)
-                               .WithInvariants(AnyInvariants, any_is_strong);
+                               .WithContracts(AnyInvariants, any_is_strong);
 
   Thrower val(2);
   absl::any any_val(val);
@@ -129,7 +129,7 @@
   auto strong_empty_any_tester =
       testing::MakeExceptionSafetyTester()
           .WithInitialValue(absl::any{})
-          .WithInvariants(AnyInvariants, empty_any_is_strong);
+          .WithContracts(AnyInvariants, empty_any_is_strong);
 
   EXPECT_TRUE(strong_empty_any_tester.Test(assign_any));
   EXPECT_TRUE(strong_empty_any_tester.Test(assign_val));
@@ -142,7 +142,7 @@
       absl::any{absl::in_place_type_t<Thrower>(), 1, testing::nothrow_ctor};
   auto one_tester = testing::MakeExceptionSafetyTester()
                         .WithInitialValue(initial_val)
-                        .WithInvariants(AnyInvariants, AnyIsEmpty);
+                        .WithContracts(AnyInvariants, AnyIsEmpty);
 
   auto emp_thrower = [](absl::any* ap) { ap->emplace<Thrower>(2); };
   auto emp_throwervec = [](absl::any* ap) {
diff --git a/absl/types/internal/variant.h b/absl/types/internal/variant.h
index 7708e67..eff4fef 100644
--- a/absl/types/internal/variant.h
+++ b/absl/types/internal/variant.h
@@ -1,4 +1,4 @@
-// Copyright 2017 The Abseil Authors.
+// Copyright 2018 The Abseil Authors.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -37,6 +37,8 @@
 #include "absl/types/bad_variant_access.h"
 #include "absl/utility/utility.h"
 
+#if !defined(ABSL_HAVE_STD_VARIANT)
+
 namespace absl {
 
 template <class... Types>
@@ -908,6 +910,11 @@
   template <std::size_t... TupIs, std::size_t... Is>
   constexpr ReturnType Run(std::false_type /*has_valueless*/,
                            index_sequence<TupIs...>, SizeT<Is>...) const {
+    static_assert(
+        std::is_same<ReturnType,
+                     absl::result_of_t<Op(VariantAccessResult<
+                                          Is, QualifiedVariants>...)>>::value,
+        "All visitation overloads must have the same return type.");
     return absl::base_internal::Invoke(
         absl::forward<Op>(op),
         VariantCoreAccess::Access<Is>(
@@ -1227,23 +1234,23 @@
 // Base that is dependent on whether or not the move-assign can be trivial.
 template <class... T>
 using VariantMoveAssignBase = absl::conditional_t<
-    absl::disjunction<absl::conjunction<std::is_move_assignable<Union<T...>>,
+    absl::disjunction<absl::conjunction<absl::is_move_assignable<Union<T...>>,
                                         std::is_move_constructible<Union<T...>>,
                                         std::is_destructible<Union<T...>>>,
                       absl::negation<absl::conjunction<
                           std::is_move_constructible<T>...,
-                          std::is_move_assignable<T>...>>>::value,
+                          absl::is_move_assignable<T>...>>>::value,
     VariantCopyBase<T...>, VariantMoveAssignBaseNontrivial<T...>>;
 
 // Base that is dependent on whether or not the copy-assign can be trivial.
 template <class... T>
 using VariantCopyAssignBase = absl::conditional_t<
-    absl::disjunction<absl::conjunction<std::is_copy_assignable<Union<T...>>,
+    absl::disjunction<absl::conjunction<absl::is_copy_assignable<Union<T...>>,
                                         std::is_copy_constructible<Union<T...>>,
                                         std::is_destructible<Union<T...>>>,
                       absl::negation<absl::conjunction<
                           std::is_copy_constructible<T>...,
-                          std::is_copy_assignable<T>...>>>::value,
+                          absl::is_copy_assignable<T>...>>>::value,
     VariantMoveAssignBase<T...>, VariantCopyAssignBaseNontrivial<T...>>;
 
 template <class... T>
@@ -1612,4 +1619,5 @@
 }  // namespace variant_internal
 }  // namespace absl
 
+#endif  // !defined(ABSL_HAVE_STD_VARIANT)
 #endif  // ABSL_TYPES_variant_internal_H_
diff --git a/absl/types/optional.h b/absl/types/optional.h
index c837cdd..1421001 100644
--- a/absl/types/optional.h
+++ b/absl/types/optional.h
@@ -59,6 +59,7 @@
 #include <type_traits>
 #include <utility>
 
+#include "absl/base/attributes.h"
 #include "absl/memory/memory.h"
 #include "absl/meta/type_traits.h"
 #include "absl/types/bad_optional_access.h"
@@ -411,10 +412,10 @@
 
 template <typename T>
 constexpr copy_traits get_assign_copy_traits() {
-  return std::is_copy_assignable<T>::value &&
+  return absl::is_copy_assignable<T>::value &&
                  std::is_copy_constructible<T>::value
              ? copy_traits::copyable
-             : std::is_move_assignable<T>::value &&
+             : absl::is_move_assignable<T>::value &&
                        std::is_move_constructible<T>::value
                    ? copy_traits::movable
                    : copy_traits::non_movable;
@@ -700,7 +701,7 @@
   // optional::reset()
   //
   // Destroys the inner `T` value of an `absl::optional` if one is present.
-  void reset() noexcept { this->destruct(); }
+  ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept { this->destruct(); }
 
   // optional::emplace()
   //
diff --git a/absl/types/optional_exception_safety_test.cc b/absl/types/optional_exception_safety_test.cc
index d2ef04b..d117ee5 100644
--- a/absl/types/optional_exception_safety_test.cc
+++ b/absl/types/optional_exception_safety_test.cc
@@ -38,12 +38,12 @@
 template <typename OptionalT>
 bool ValueThrowsBadOptionalAccess(const OptionalT& optional) try {
   return (static_cast<void>(optional.value()), false);
-} catch (absl::bad_optional_access) {
+} catch (const absl::bad_optional_access&) {
   return true;
 }
 
 template <typename OptionalT>
-AssertionResult CheckInvariants(OptionalT* optional_ptr) {
+AssertionResult OptionalInvariants(OptionalT* optional_ptr) {
   // Check the current state post-throw for validity
   auto& optional = *optional_ptr;
 
@@ -123,8 +123,8 @@
 TEST(OptionalExceptionSafety, Emplace) {
   // Test the basic guarantee plus test the result of optional::has_value()
   // is false in all cases
-  auto disengaged_test = MakeExceptionSafetyTester().WithInvariants(
-      CheckInvariants<Optional>, CheckDisengaged<Optional>);
+  auto disengaged_test = MakeExceptionSafetyTester().WithContracts(
+      OptionalInvariants<Optional>, CheckDisengaged<Optional>);
   auto disengaged_test_empty = disengaged_test.WithInitialValue(Optional());
   auto disengaged_test_nonempty =
       disengaged_test.WithInitialValue(Optional(kInitialInteger));
@@ -147,11 +147,11 @@
   // Test the basic guarantee plus test the result of optional::has_value()
   // remains the same
   auto test =
-      MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
+      MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
   auto disengaged_test_empty = test.WithInitialValue(Optional())
-                                   .WithInvariants(CheckDisengaged<Optional>);
+                                   .WithContracts(CheckDisengaged<Optional>);
   auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
-                                   .WithInvariants(CheckEngaged<Optional>);
+                                   .WithContracts(CheckEngaged<Optional>);
 
   auto swap_empty = [](Optional* optional_ptr) {
     auto empty = Optional();
@@ -192,11 +192,11 @@
   // Test the basic guarantee plus test the result of optional::has_value()
   // remains the same
   auto test =
-      MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
+      MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
   auto disengaged_test_empty = test.WithInitialValue(Optional())
-                                   .WithInvariants(CheckDisengaged<Optional>);
+                                   .WithContracts(CheckDisengaged<Optional>);
   auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
-                                   .WithInvariants(CheckEngaged<Optional>);
+                                   .WithContracts(CheckEngaged<Optional>);
 
   auto copyassign_nonempty = [](Optional* optional_ptr) {
     auto nonempty =
@@ -218,11 +218,11 @@
   // Test the basic guarantee plus test the result of optional::has_value()
   // remains the same
   auto test =
-      MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
+      MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
   auto disengaged_test_empty = test.WithInitialValue(Optional())
-                                   .WithInvariants(CheckDisengaged<Optional>);
+                                   .WithContracts(CheckDisengaged<Optional>);
   auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
-                                   .WithInvariants(CheckEngaged<Optional>);
+                                   .WithContracts(CheckEngaged<Optional>);
 
   auto moveassign_empty = [](Optional* optional_ptr) {
     auto empty = Optional();
diff --git a/absl/types/optional_test.cc b/absl/types/optional_test.cc
index 179bfd6..d90db9f 100644
--- a/absl/types/optional_test.cc
+++ b/absl/types/optional_test.cc
@@ -607,11 +607,12 @@
   opt2_to_empty = empty;
   EXPECT_FALSE(opt2_to_empty);
 
-  EXPECT_FALSE(std::is_copy_assignable<absl::optional<const int>>::value);
-  EXPECT_TRUE(std::is_copy_assignable<absl::optional<Copyable>>::value);
-  EXPECT_FALSE(std::is_copy_assignable<absl::optional<MoveableThrow>>::value);
-  EXPECT_FALSE(std::is_copy_assignable<absl::optional<MoveableNoThrow>>::value);
-  EXPECT_FALSE(std::is_copy_assignable<absl::optional<NonMovable>>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<absl::optional<const int>>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<absl::optional<Copyable>>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<absl::optional<MoveableThrow>>::value);
+  EXPECT_FALSE(
+      absl::is_copy_assignable<absl::optional<MoveableNoThrow>>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<absl::optional<NonMovable>>::value);
 
   EXPECT_TRUE(absl::is_trivially_copy_assignable<int>::value);
   EXPECT_TRUE(absl::is_trivially_copy_assignable<volatile int>::value);
@@ -625,9 +626,9 @@
   };
 
   EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial>::value);
-  EXPECT_FALSE(std::is_copy_assignable<const Trivial>::value);
-  EXPECT_FALSE(std::is_copy_assignable<volatile Trivial>::value);
-  EXPECT_TRUE(std::is_copy_assignable<NonTrivial>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<const Trivial>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<volatile Trivial>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<NonTrivial>::value);
   EXPECT_FALSE(absl::is_trivially_copy_assignable<NonTrivial>::value);
 
   // std::optional doesn't support volatile nontrivial types.
@@ -695,11 +696,11 @@
     EXPECT_EQ(1, listener.volatile_move_assign);
   }
 #endif  // ABSL_HAVE_STD_OPTIONAL
-  EXPECT_FALSE(std::is_move_assignable<absl::optional<const int>>::value);
-  EXPECT_TRUE(std::is_move_assignable<absl::optional<Copyable>>::value);
-  EXPECT_TRUE(std::is_move_assignable<absl::optional<MoveableThrow>>::value);
-  EXPECT_TRUE(std::is_move_assignable<absl::optional<MoveableNoThrow>>::value);
-  EXPECT_FALSE(std::is_move_assignable<absl::optional<NonMovable>>::value);
+  EXPECT_FALSE(absl::is_move_assignable<absl::optional<const int>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<absl::optional<Copyable>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<absl::optional<MoveableThrow>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<absl::optional<MoveableNoThrow>>::value);
+  EXPECT_FALSE(absl::is_move_assignable<absl::optional<NonMovable>>::value);
 
   EXPECT_FALSE(
       std::is_nothrow_move_assignable<absl::optional<MoveableThrow>>::value);
@@ -1619,7 +1620,7 @@
   EXPECT_TRUE(
       (std::is_assignable<absl::optional<AnyLike>&, const AnyLike&>::value));
   EXPECT_TRUE(std::is_move_assignable<absl::optional<AnyLike>>::value);
-  EXPECT_TRUE(std::is_copy_assignable<absl::optional<AnyLike>>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<absl::optional<AnyLike>>::value);
 }
 
 }  // namespace
diff --git a/absl/types/span.h b/absl/types/span.h
index 76be819..911af0c 100644
--- a/absl/types/span.h
+++ b/absl/types/span.h
@@ -87,7 +87,7 @@
   return c.data();
 }
 
-// Before C++17, std::string::data returns a const char* in all cases.
+// Before C++17, string::data returns a const char* in all cases.
 inline char* GetDataImpl(std::string& s,  // NOLINT(runtime/references)
                          int) noexcept {
   return &s[0];
@@ -379,64 +379,70 @@
   //
   // Returns a reference to the i'th element of this span.
   constexpr reference at(size_type i) const {
-    return ABSL_PREDICT_TRUE(i < size())
-               ? ptr_[i]
+    return ABSL_PREDICT_TRUE(i < size())  //
+               ? *(data() + i)
                : (base_internal::ThrowStdOutOfRange(
                       "Span::at failed bounds check"),
-                  ptr_[i]);
+                  *(data() + i));
   }
 
   // Span::front()
   //
   // Returns a reference to the first element of this span.
-  reference front() const noexcept { return ABSL_ASSERT(size() > 0), ptr_[0]; }
+  constexpr reference front() const noexcept {
+    return ABSL_ASSERT(size() > 0), *data();
+  }
 
   // Span::back()
   //
   // Returns a reference to the last element of this span.
-  reference back() const noexcept {
-    return ABSL_ASSERT(size() > 0), ptr_[size() - 1];
+  constexpr reference back() const noexcept {
+    return ABSL_ASSERT(size() > 0), *(data() + size() - 1);
   }
 
   // Span::begin()
   //
   // Returns an iterator to the first element of this span.
-  constexpr iterator begin() const noexcept { return ptr_; }
+  constexpr iterator begin() const noexcept { return data(); }
 
   // Span::cbegin()
   //
   // Returns a const iterator to the first element of this span.
-  constexpr const_iterator cbegin() const noexcept { return ptr_; }
+  constexpr const_iterator cbegin() const noexcept { return begin(); }
 
   // Span::end()
   //
   // Returns an iterator to the last element of this span.
-  iterator end() const noexcept { return ptr_ + len_; }
+  constexpr iterator end() const noexcept { return data() + size(); }
 
   // Span::cend()
   //
   // Returns a const iterator to the last element of this span.
-  const_iterator cend() const noexcept { return end(); }
+  constexpr const_iterator cend() const noexcept { return end(); }
 
   // Span::rbegin()
   //
   // Returns a reverse iterator starting at the last element of this span.
-  reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
+  constexpr reverse_iterator rbegin() const noexcept {
+    return reverse_iterator(end());
+  }
 
   // Span::crbegin()
   //
   // Returns a reverse const iterator starting at the last element of this span.
-  const_reverse_iterator crbegin() const noexcept { return rbegin(); }
+  constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
 
   // Span::rend()
   //
   // Returns a reverse iterator starting at the first element of this span.
-  reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
+  constexpr reverse_iterator rend() const noexcept {
+    return reverse_iterator(begin());
+  }
 
   // Span::crend()
   //
   // Returns a reverse iterator starting at the first element of this span.
-  const_reverse_iterator crend() const noexcept { return rend(); }
+  constexpr const_reverse_iterator crend() const noexcept { return rend(); }
 
   // Span mutations
 
@@ -444,7 +450,7 @@
   //
   // Removes the first `n` elements from the span.
   void remove_prefix(size_type n) noexcept {
-    assert(len_ >= n);
+    assert(size() >= n);
     ptr_ += n;
     len_ -= n;
   }
@@ -453,7 +459,7 @@
   //
   // Removes the last `n` elements from the span.
   void remove_suffix(size_type n) noexcept {
-    assert(len_ >= n);
+    assert(size() >= n);
     len_ -= n;
   }
 
@@ -474,11 +480,18 @@
   //   absl::MakeSpan(vec).subspan(4);     // {}
   //   absl::MakeSpan(vec).subspan(5);     // throws std::out_of_range
   constexpr Span subspan(size_type pos = 0, size_type len = npos) const {
-    return (pos <= len_)
-               ? Span(ptr_ + pos, span_internal::Min(len_ - pos, len))
+    return (pos <= size())
+               ? Span(data() + pos, span_internal::Min(size() - pos, len))
                : (base_internal::ThrowStdOutOfRange("pos > size()"), Span());
   }
 
+  // Support for absl::Hash.
+  template <typename H>
+  friend H AbslHashValue(H h, Span v) {
+    return H::combine(H::combine_contiguous(std::move(h), v.data(), v.size()),
+                      v.size());
+  }
+
  private:
   pointer ptr_;
   size_type len_;
diff --git a/absl/types/span_test.cc b/absl/types/span_test.cc
index fbce7e8..bd739ff 100644
--- a/absl/types/span_test.cc
+++ b/absl/types/span_test.cc
@@ -29,6 +29,7 @@
 #include "absl/base/internal/exception_testing.h"
 #include "absl/container/fixed_array.h"
 #include "absl/container/inlined_vector.h"
+#include "absl/hash/hash_testing.h"
 #include "absl/strings/str_cat.h"
 
 namespace {
diff --git a/absl/types/variant.h b/absl/types/variant.h
index 17e0634..2f78722 100644
--- a/absl/types/variant.h
+++ b/absl/types/variant.h
@@ -414,9 +414,9 @@
 //   };
 //
 //   // Declare our variant, and call `absl::visit()` on it.
-//   std::variant<int, std::string> foo = std::string("foo");
+//   absl::variant<int, std::string> foo = std::string("foo");
 //   GetVariant visitor;
-//   std::visit(visitor, foo);  // Prints `The variant's value is: foo'
+//   absl::visit(visitor, foo);  // Prints `The variant's value is: foo'
 template <typename Visitor, typename... Variants>
 variant_internal::VisitResult<Visitor, Variants...> visit(Visitor&& vis,
                                                           Variants&&... vars) {
diff --git a/absl/types/variant_exception_safety_test.cc b/absl/types/variant_exception_safety_test.cc
index 27c0b96..58436f0 100644
--- a/absl/types/variant_exception_safety_test.cc
+++ b/absl/types/variant_exception_safety_test.cc
@@ -53,13 +53,13 @@
   try {
     v.emplace<Thrower>();
     v.emplace<Thrower>(ExceptionOnConversion<Thrower>());
-  } catch (ConversionException& /*e*/) {
+  } catch (const ConversionException&) {
     // This space intentionally left blank.
   }
 }
 
 // Check that variant is still in a usable state after an exception is thrown.
-testing::AssertionResult CheckInvariants(ThrowingVariant* v) {
+testing::AssertionResult VariantInvariants(ThrowingVariant* v) {
   using testing::AssertionFailure;
   using testing::AssertionSuccess;
 
@@ -100,7 +100,7 @@
     auto unused = absl::get<Thrower>(*v);
     static_cast<void>(unused);
     return AssertionFailure() << "Variant should not contain Thrower";
-  } catch (absl::bad_variant_access) {
+  } catch (const absl::bad_variant_access&) {
   } catch (...) {
     return AssertionFailure() << "Unexpected exception throw from absl::get";
   }
@@ -213,8 +213,8 @@
         MakeExceptionSafetyTester()
             .WithInitialValue(WithThrower())
             .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; });
-    EXPECT_TRUE(tester.WithInvariants(CheckInvariants).Test());
-    EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_TRUE(tester.WithContracts(VariantInvariants).Test());
+    EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test());
   }
   {
     const ThrowingVariant rhs(ExpectedThrowerVec());
@@ -222,8 +222,8 @@
         MakeExceptionSafetyTester()
             .WithInitialValue(WithThrowerVec())
             .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; });
-    EXPECT_TRUE(tester.WithInvariants(CheckInvariants).Test());
-    EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_TRUE(tester.WithContracts(VariantInvariants).Test());
+    EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test());
   }
   // libstdc++ std::variant has bugs on copy assignment regarding exception
   // safety.
@@ -251,12 +251,12 @@
             .WithInitialValue(WithCopyNoThrow())
             .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; });
     EXPECT_TRUE(tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return lhs->valueless_by_exception();
+                                   })
                     .Test());
-    EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test());
   }
 #endif  // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
   {
@@ -268,7 +268,7 @@
     const ThrowingVariant rhs(MoveNothrow{});
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithThrower())
-                    .WithInvariants(CheckInvariants, strong_guarantee)
+                    .WithContracts(VariantInvariants, strong_guarantee)
                     .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
   }
 }
@@ -304,11 +304,11 @@
                         *lhs = std::move(copy);
                       });
     EXPECT_TRUE(tester
-                    .WithInvariants(
-                        CheckInvariants,
+                    .WithContracts(
+                        VariantInvariants,
                         [&](ThrowingVariant* lhs) { return lhs->index() == j; })
                     .Test());
-    EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test());
   }
   {
     // - otherwise (index() != j), equivalent to
@@ -318,10 +318,10 @@
     ThrowingVariant rhs(CopyNothrow{});
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithThrower())
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return lhs->valueless_by_exception();
+                                   })
                     .Test([&](ThrowingVariant* lhs) {
                       auto copy = rhs;
                       *lhs = std::move(copy);
@@ -347,12 +347,12 @@
             .WithInitialValue(WithThrower())
             .WithOperation([rhs](ThrowingVariant* lhs) { *lhs = rhs; });
     EXPECT_TRUE(copy_tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return !lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return !lhs->valueless_by_exception();
+                                   })
                     .Test());
-    EXPECT_FALSE(copy_tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(copy_tester.WithContracts(strong_guarantee).Test());
     // move assign
     auto move_tester = MakeExceptionSafetyTester()
                            .WithInitialValue(WithThrower())
@@ -361,13 +361,13 @@
                              *lhs = std::move(copy);
                            });
     EXPECT_TRUE(move_tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return !lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return !lhs->valueless_by_exception();
+                                   })
                     .Test());
 
-    EXPECT_FALSE(move_tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(move_tester.WithContracts(strong_guarantee).Test());
   }
   // Otherwise (*this holds something else), if is_nothrow_constructible_v<Tj,
   // T> || !is_nothrow_move_constructible_v<Tj> is true, equivalent to
@@ -400,12 +400,12 @@
             .WithInitialValue(WithCopyNoThrow())
             .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; });
     EXPECT_TRUE(copy_tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return lhs->valueless_by_exception();
+                                   })
                     .Test());
-    EXPECT_FALSE(copy_tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(copy_tester.WithContracts(strong_guarantee).Test());
     // move
     auto move_tester = MakeExceptionSafetyTester()
                            .WithInitialValue(WithCopyNoThrow())
@@ -413,12 +413,12 @@
                              *lhs = ExpectedThrower(testing::nothrow_ctor);
                            });
     EXPECT_TRUE(move_tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* lhs) {
-                                      return lhs->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* lhs) {
+                                     return lhs->valueless_by_exception();
+                                   })
                     .Test());
-    EXPECT_FALSE(move_tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(move_tester.WithContracts(strong_guarantee).Test());
   }
   // Otherwise (if is_nothrow_constructible_v<Tj, T> == false &&
   // is_nothrow_move_constructible<Tj> == true),
@@ -432,7 +432,7 @@
     MoveNothrow rhs;
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithThrower())
-                    .WithInvariants(CheckInvariants, strong_guarantee)
+                    .WithContracts(VariantInvariants, strong_guarantee)
                     .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }));
   }
 #endif  // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__))
@@ -450,12 +450,12 @@
                         v->emplace<Thrower>(args);
                       });
     EXPECT_TRUE(tester
-                    .WithInvariants(CheckInvariants,
-                                    [](ThrowingVariant* v) {
-                                      return v->valueless_by_exception();
-                                    })
+                    .WithContracts(VariantInvariants,
+                                   [](ThrowingVariant* v) {
+                                     return v->valueless_by_exception();
+                                   })
                     .Test());
-    EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test());
+    EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test());
   }
 }
 
@@ -472,7 +472,7 @@
     ThrowingVariant rhs = ExpectedThrower();
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithThrower())
-                    .WithInvariants(CheckInvariants)
+                    .WithContracts(VariantInvariants)
                     .Test([&](ThrowingVariant* lhs) {
                       auto copy = rhs;
                       lhs->swap(copy);
@@ -486,7 +486,7 @@
     ThrowingVariant rhs = ExpectedThrower();
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithCopyNoThrow())
-                    .WithInvariants(CheckInvariants)
+                    .WithContracts(VariantInvariants)
                     .Test([&](ThrowingVariant* lhs) {
                       auto copy = rhs;
                       lhs->swap(copy);
@@ -496,7 +496,7 @@
     ThrowingVariant rhs = ExpectedThrower();
     EXPECT_TRUE(MakeExceptionSafetyTester()
                     .WithInitialValue(WithCopyNoThrow())
-                    .WithInvariants(CheckInvariants)
+                    .WithContracts(VariantInvariants)
                     .Test([&](ThrowingVariant* lhs) {
                       auto copy = rhs;
                       copy.swap(*lhs);
diff --git a/absl/types/variant_test.cc b/absl/types/variant_test.cc
index 262bd94..bfb8bd7 100644
--- a/absl/types/variant_test.cc
+++ b/absl/types/variant_test.cc
@@ -403,7 +403,7 @@
 
 template <class T>
 struct is_trivially_move_assignable
-    : std::is_move_assignable<SingleUnion<T>>::type {};
+    : absl::is_move_assignable<SingleUnion<T>>::type {};
 
 TEST(VariantTest, NothrowMoveConstructible) {
   // Verify that variant is nothrow move constructible iff its template
@@ -2439,14 +2439,14 @@
 
 TEST(VariantTest, TestCopyAndMoveTypeTraits) {
   EXPECT_TRUE(std::is_copy_constructible<variant<std::string>>::value);
-  EXPECT_TRUE(std::is_copy_assignable<variant<std::string>>::value);
+  EXPECT_TRUE(absl::is_copy_assignable<variant<std::string>>::value);
   EXPECT_TRUE(std::is_move_constructible<variant<std::string>>::value);
-  EXPECT_TRUE(std::is_move_assignable<variant<std::string>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<variant<std::string>>::value);
   EXPECT_TRUE(std::is_move_constructible<variant<std::unique_ptr<int>>>::value);
-  EXPECT_TRUE(std::is_move_assignable<variant<std::unique_ptr<int>>>::value);
+  EXPECT_TRUE(absl::is_move_assignable<variant<std::unique_ptr<int>>>::value);
   EXPECT_FALSE(
       std::is_copy_constructible<variant<std::unique_ptr<int>>>::value);
-  EXPECT_FALSE(std::is_copy_assignable<variant<std::unique_ptr<int>>>::value);
+  EXPECT_FALSE(absl::is_copy_assignable<variant<std::unique_ptr<int>>>::value);
 
   EXPECT_FALSE(
       absl::is_trivially_copy_constructible<variant<std::string>>::value);
diff --git a/absl/utility/utility.h b/absl/utility/utility.h
index d73602c..aef4baa 100644
--- a/absl/utility/utility.h
+++ b/absl/utility/utility.h
@@ -235,13 +235,13 @@
 // Example:
 //
 //   class Foo{void Bar(int);};
-//   void user_function(int, std::string);
+//   void user_function(int, string);
 //   void user_function(std::unique_ptr<Foo>);
 //
 //   int main()
 //   {
-//       std::tuple<int, std::string> tuple1(42, "bar");
-//       // Invokes the user function overload on int, std::string.
+//       std::tuple<int, string> tuple1(42, "bar");
+//       // Invokes the user function overload on int, string.
 //       absl::apply(&user_function, tuple1);
 //
 //       auto foo = absl::make_unique<Foo>();
diff --git a/api/audio/echo_canceller3_config.cc b/api/audio/echo_canceller3_config.cc
index 3b03d13..036ef9c 100644
--- a/api/audio/echo_canceller3_config.cc
+++ b/api/audio/echo_canceller3_config.cc
@@ -9,7 +9,34 @@
  */
 #include "api/audio/echo_canceller3_config.h"
 
+#include <algorithm>
+
+#include "rtc_base/logging.h"
+#include "rtc_base/numerics/safe_minmax.h"
+
 namespace webrtc {
+namespace {
+bool Limit(float* value, float min, float max) {
+  float clamped = rtc::SafeClamp(*value, min, max);
+  bool res = *value == clamped;
+  *value = clamped;
+  return res;
+}
+
+bool Limit(size_t* value, size_t min, size_t max) {
+  size_t clamped = rtc::SafeClamp(*value, min, max);
+  bool res = *value == clamped;
+  *value = clamped;
+  return res;
+}
+
+bool Limit(int* value, int min, int max) {
+  int clamped = rtc::SafeClamp(*value, min, max);
+  bool res = *value == clamped;
+  *value = clamped;
+  return res;
+}
+}  // namespace
 
 EchoCanceller3Config::EchoCanceller3Config() = default;
 EchoCanceller3Config::EchoCanceller3Config(const EchoCanceller3Config& e) =
@@ -51,4 +78,191 @@
 EchoCanceller3Config::Suppressor::Tuning::Tuning(
     const EchoCanceller3Config::Suppressor::Tuning& e) = default;
 
+bool EchoCanceller3Config::Validate(EchoCanceller3Config* config) {
+  RTC_DCHECK(config);
+  EchoCanceller3Config* c = config;
+  bool res = true;
+
+  if (c->delay.down_sampling_factor != 4 &&
+      c->delay.down_sampling_factor != 8) {
+    c->delay.down_sampling_factor = 4;
+    res = false;
+  }
+
+  if (c->delay.num_filters == 0) {
+    c->delay.num_filters = 1;
+    res = false;
+  }
+  if (c->delay.api_call_jitter_blocks == 0) {
+    c->delay.api_call_jitter_blocks = 1;
+    res = false;
+  }
+
+  if (c->delay.api_call_jitter_blocks == 0) {
+    c->delay.api_call_jitter_blocks = 1;
+    res = false;
+  }
+  if (c->delay.delay_headroom_blocks <= 1 &&
+      c->delay.hysteresis_limit_1_blocks == 1) {
+    c->delay.hysteresis_limit_1_blocks = 0;
+    res = false;
+  }
+  res = res && Limit(&c->delay.delay_estimate_smoothing, 0.f, 1.f);
+  res = res && Limit(&c->delay.delay_candidate_detection_threshold, 0.f, 1.f);
+  res = res && Limit(&c->delay.delay_selection_thresholds.initial, 1, 250);
+  res = res && Limit(&c->delay.delay_selection_thresholds.converged, 1, 250);
+
+  res = res && Limit(&c->filter.main.length_blocks, 1, 50);
+  res = res && Limit(&c->filter.main.leakage_converged, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main.leakage_diverged, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main.error_floor, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main.noise_gate, 0.f, 100000000.f);
+
+  res = res && Limit(&c->filter.main_initial.length_blocks, 1, 50);
+  res = res && Limit(&c->filter.main_initial.leakage_converged, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main_initial.leakage_diverged, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main_initial.error_floor, 0.f, 1000.f);
+  res = res && Limit(&c->filter.main_initial.noise_gate, 0.f, 100000000.f);
+
+  if (c->filter.main.length_blocks < c->filter.main_initial.length_blocks) {
+    c->filter.main_initial.length_blocks = c->filter.main.length_blocks;
+    res = false;
+  }
+
+  res = res && Limit(&c->filter.shadow.length_blocks, 1, 50);
+  res = res && Limit(&c->filter.shadow.rate, 0.f, 1.f);
+  res = res && Limit(&c->filter.shadow.noise_gate, 0.f, 100000000.f);
+
+  res = res && Limit(&c->filter.shadow_initial.length_blocks, 1, 50);
+  res = res && Limit(&c->filter.shadow_initial.rate, 0.f, 1.f);
+  res = res && Limit(&c->filter.shadow_initial.noise_gate, 0.f, 100000000.f);
+
+  if (c->filter.shadow.length_blocks < c->filter.shadow_initial.length_blocks) {
+    c->filter.shadow_initial.length_blocks = c->filter.shadow.length_blocks;
+    res = false;
+  }
+
+  res = res && Limit(&c->filter.config_change_duration_blocks, 0, 100000);
+  res = res && Limit(&c->filter.initial_state_seconds, 0.f, 100.f);
+
+  res = res && Limit(&c->erle.min, 1.f, 100000.f);
+  res = res && Limit(&c->erle.max_l, 1.f, 100000.f);
+  res = res && Limit(&c->erle.max_h, 1.f, 100000.f);
+  if (c->erle.min > c->erle.max_l || c->erle.min > c->erle.max_h) {
+    c->erle.min = std::min(c->erle.max_l, c->erle.max_h);
+    res = false;
+  }
+
+  res = res && Limit(&c->ep_strength.lf, 0.f, 1000000.f);
+  res = res && Limit(&c->ep_strength.mf, 0.f, 1000000.f);
+  res = res && Limit(&c->ep_strength.hf, 0.f, 1000000.f);
+  res = res && Limit(&c->ep_strength.default_len, 0.f, 1.f);
+
+  res = res && Limit(&c->gain_mask.m0, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m1, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m2, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m3, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m5, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m6, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m7, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m8, 0.f, 1.f);
+  res = res && Limit(&c->gain_mask.m9, 0.f, 1.f);
+
+  res = res && Limit(&c->gain_mask.gain_curve_offset, 0.f, 1000.f);
+  res = res && Limit(&c->gain_mask.gain_curve_slope, 0.f, 1000.f);
+  res = res && Limit(&c->gain_mask.temporal_masking_lf, 0.f, 1000.f);
+  res = res && Limit(&c->gain_mask.temporal_masking_hf, 0.f, 1000.f);
+  res = res && Limit(&c->gain_mask.temporal_masking_lf_bands, 0, 65);
+
+  res = res &&
+        Limit(&c->echo_audibility.low_render_limit, 0.f, 32768.f * 32768.f);
+  res = res &&
+        Limit(&c->echo_audibility.normal_render_limit, 0.f, 32768.f * 32768.f);
+  res = res && Limit(&c->echo_audibility.floor_power, 0.f, 32768.f * 32768.f);
+  res = res && Limit(&c->echo_audibility.audibility_threshold_lf, 0.f,
+                     32768.f * 32768.f);
+  res = res && Limit(&c->echo_audibility.audibility_threshold_mf, 0.f,
+                     32768.f * 32768.f);
+  res = res && Limit(&c->echo_audibility.audibility_threshold_hf, 0.f,
+                     32768.f * 32768.f);
+
+  res = res &&
+        Limit(&c->render_levels.active_render_limit, 0.f, 32768.f * 32768.f);
+  res = res && Limit(&c->render_levels.poor_excitation_render_limit, 0.f,
+                     32768.f * 32768.f);
+  res = res && Limit(&c->render_levels.poor_excitation_render_limit_ds8, 0.f,
+                     32768.f * 32768.f);
+
+  res =
+      res && Limit(&c->echo_removal_control.gain_rampup.initial_gain, 0.f, 1.f);
+  res = res && Limit(&c->echo_removal_control.gain_rampup.first_non_zero_gain,
+                     0.f, 1.f);
+  res = res && Limit(&c->echo_removal_control.gain_rampup.non_zero_gain_blocks,
+                     0, 100000);
+  res = res &&
+        Limit(&c->echo_removal_control.gain_rampup.full_gain_blocks, 0, 100000);
+
+  res = res && Limit(&c->echo_model.noise_floor_hold, 0, 1000);
+  res = res && Limit(&c->echo_model.min_noise_floor_power, 0, 2000000.f);
+  res = res && Limit(&c->echo_model.stationary_gate_slope, 0, 1000000.f);
+  res = res && Limit(&c->echo_model.noise_gate_power, 0, 1000000.f);
+  res = res && Limit(&c->echo_model.noise_gate_slope, 0, 1000000.f);
+  res = res && Limit(&c->echo_model.render_pre_window_size, 0, 100);
+  res = res && Limit(&c->echo_model.render_post_window_size, 0, 100);
+  res = res && Limit(&c->echo_model.render_pre_window_size_init, 0, 100);
+  res = res && Limit(&c->echo_model.render_post_window_size_init, 0, 100);
+  res = res && Limit(&c->echo_model.nonlinear_hold, 0, 100);
+  res = res && Limit(&c->echo_model.nonlinear_release, 0, 1.f);
+
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_lf.enr_transparent, 0.f, 100.f);
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_lf.enr_suppress, 0.f, 100.f);
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_lf.emr_transparent, 0.f, 100.f);
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_hf.enr_transparent, 0.f, 100.f);
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_hf.enr_suppress, 0.f, 100.f);
+  res = res &&
+        Limit(&c->suppressor.normal_tuning.mask_hf.emr_transparent, 0.f, 100.f);
+  res = res && Limit(&c->suppressor.normal_tuning.max_inc_factor, 0.f, 100.f);
+  res =
+      res && Limit(&c->suppressor.normal_tuning.max_dec_factor_lf, 0.f, 100.f);
+
+  res = res && Limit(&c->suppressor.nearend_tuning.mask_lf.enr_transparent, 0.f,
+                     100.f);
+  res = res &&
+        Limit(&c->suppressor.nearend_tuning.mask_lf.enr_suppress, 0.f, 100.f);
+  res = res && Limit(&c->suppressor.nearend_tuning.mask_lf.emr_transparent, 0.f,
+                     100.f);
+  res = res && Limit(&c->suppressor.nearend_tuning.mask_hf.enr_transparent, 0.f,
+                     100.f);
+  res = res &&
+        Limit(&c->suppressor.nearend_tuning.mask_hf.enr_suppress, 0.f, 100.f);
+  res = res && Limit(&c->suppressor.nearend_tuning.mask_hf.emr_transparent, 0.f,
+                     100.f);
+  res = res && Limit(&c->suppressor.nearend_tuning.max_inc_factor, 0.f, 100.f);
+  res =
+      res && Limit(&c->suppressor.nearend_tuning.max_dec_factor_lf, 0.f, 100.f);
+
+  res = res && Limit(&c->suppressor.dominant_nearend_detection.enr_threshold,
+                     0.f, 1000000.f);
+  res = res && Limit(&c->suppressor.dominant_nearend_detection.snr_threshold,
+                     0.f, 1000000.f);
+  res = res && Limit(&c->suppressor.dominant_nearend_detection.hold_duration, 0,
+                     10000);
+  res =
+      res && Limit(&c->suppressor.dominant_nearend_detection.trigger_threshold,
+                   0, 10000);
+
+  res = res && Limit(&c->suppressor.high_bands_suppression.enr_threshold, 0.f,
+                     1000000.f);
+  res = res && Limit(&c->suppressor.high_bands_suppression.max_gain_during_echo,
+                     0.f, 1.f);
+
+  res = res && Limit(&c->suppressor.floor_first_increase, 0.f, 1000000.f);
+
+  return res;
+}
 }  // namespace webrtc
diff --git a/api/audio/echo_canceller3_config.h b/api/audio/echo_canceller3_config.h
index f4ba8e9..59a84ba 100644
--- a/api/audio/echo_canceller3_config.h
+++ b/api/audio/echo_canceller3_config.h
@@ -13,18 +13,31 @@
 
 #include <stddef.h>  // size_t
 
+#include "rtc_base/system/rtc_export.h"
+
 namespace webrtc {
 
 // Configuration struct for EchoCanceller3
-struct EchoCanceller3Config {
+struct RTC_EXPORT EchoCanceller3Config {
+  // Checks and updates the parameters in a config to lie within reasonable
+  // ranges. Returns true if and only of the config did not need to be changed.
+  static bool Validate(EchoCanceller3Config* config);
+
   EchoCanceller3Config();
   EchoCanceller3Config(const EchoCanceller3Config& e);
+
+  struct Buffering {
+    bool use_new_render_buffering = true;
+    size_t excess_render_detection_interval_blocks = 250;
+    size_t max_allowed_excess_render_blocks = 8;
+  } buffering;
+
   struct Delay {
     Delay();
     Delay(const Delay& e);
     size_t default_delay = 5;
     size_t down_sampling_factor = 4;
-    size_t num_filters = 6;
+    size_t num_filters = 5;
     size_t api_call_jitter_blocks = 26;
     size_t min_echo_path_delay_blocks = 0;
     size_t delay_headroom_blocks = 2;
@@ -32,6 +45,12 @@
     size_t hysteresis_limit_2_blocks = 1;
     size_t skew_hysteresis_blocks = 3;
     size_t fixed_capture_delay_samples = 0;
+    float delay_estimate_smoothing = 0.7f;
+    float delay_candidate_detection_threshold = 0.2f;
+    struct DelaySelectionThresholds {
+      int initial;
+      int converged;
+    } delay_selection_thresholds = {5, 20};
   } delay;
 
   struct Filter {
@@ -40,6 +59,7 @@
       float leakage_converged;
       float leakage_diverged;
       float error_floor;
+      float error_ceil;
       float noise_gate;
     };
 
@@ -49,10 +69,11 @@
       float noise_gate;
     };
 
-    MainConfiguration main = {13, 0.00005f, 0.01f, 0.1f, 20075344.f};
+    MainConfiguration main = {13, 0.00005f, 0.05f, 0.001f, 2.f, 20075344.f};
     ShadowConfiguration shadow = {13, 0.7f, 20075344.f};
 
-    MainConfiguration main_initial = {12, 0.005f, 0.5f, 0.001f, 20075344.f};
+    MainConfiguration main_initial = {12,     0.005f, 0.5f,
+                                      0.001f, 2.f,    20075344.f};
     ShadowConfiguration shadow_initial = {12, 0.9f, 20075344.f};
 
     size_t config_change_duration_blocks = 250;
@@ -72,7 +93,7 @@
     float lf = 1.f;
     float mf = 1.f;
     float hf = 1.f;
-    float default_len = 0.88f;
+    float default_len = 0.83f;
     bool reverb_based_on_render = true;
     bool echo_can_saturate = true;
     bool bounded_erl = false;
@@ -105,7 +126,8 @@
     float audibility_threshold_lf = 10;
     float audibility_threshold_mf = 10;
     float audibility_threshold_hf = 10;
-    bool use_stationary_properties = true;
+    bool use_stationary_properties = false;
+    bool use_stationarity_properties_at_init = false;
   } echo_audibility;
 
   struct RenderLevels {
@@ -169,20 +191,22 @@
       float max_dec_factor_lf;
     };
 
-    Tuning normal_tuning = Tuning(MaskingThresholds(.2f, .3f, .3f),
+    Tuning normal_tuning = Tuning(MaskingThresholds(.3f, .4f, .3f),
                                   MaskingThresholds(.07f, .1f, .3f),
                                   2.0f,
                                   0.25f);
-    Tuning nearend_tuning = Tuning(MaskingThresholds(.2f, .3f, .3f),
-                                   MaskingThresholds(.07f, .1f, .3f),
+    Tuning nearend_tuning = Tuning(MaskingThresholds(1.09f, 1.1f, .3f),
+                                   MaskingThresholds(.1f, .3f, .3f),
                                    2.0f,
                                    0.25f);
 
     struct DominantNearendDetection {
-      float enr_threshold = 10.f;
-      float snr_threshold = 10.f;
-      int hold_duration = 25;
-      int trigger_threshold = 15;
+      float enr_threshold = 4.f;
+      float enr_exit_threshold = .1f;
+      float snr_threshold = 30.f;
+      int hold_duration = 50;
+      int trigger_threshold = 12;
+      bool use_during_initial_phase = true;
     } dominant_nearend_detection;
 
     struct HighBandsSuppression {
diff --git a/api/audio/echo_canceller3_config_json.cc b/api/audio/echo_canceller3_config_json.cc
new file mode 100644
index 0000000..a27f20c
--- /dev/null
+++ b/api/audio/echo_canceller3_config_json.cc
@@ -0,0 +1,592 @@
+/*
+ *  Copyright (c) 2018 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 "api/audio/echo_canceller3_config_json.h"
+
+#include <string>
+#include <vector>
+
+#include "rtc_base/logging.h"
+#include "rtc_base/strings/json.h"
+#include "rtc_base/strings/string_builder.h"
+
+namespace webrtc {
+namespace {
+void ReadParam(const Json::Value& root, std::string param_name, bool* param) {
+  RTC_DCHECK(param);
+  bool v;
+  if (rtc::GetBoolFromJsonObject(root, param_name, &v)) {
+    *param = v;
+  }
+}
+
+void ReadParam(const Json::Value& root, std::string param_name, size_t* param) {
+  RTC_DCHECK(param);
+  int v;
+  if (rtc::GetIntFromJsonObject(root, param_name, &v)) {
+    *param = v;
+  }
+}
+
+void ReadParam(const Json::Value& root, std::string param_name, int* param) {
+  RTC_DCHECK(param);
+  int v;
+  if (rtc::GetIntFromJsonObject(root, param_name, &v)) {
+    *param = v;
+  }
+}
+
+void ReadParam(const Json::Value& root, std::string param_name, float* param) {
+  RTC_DCHECK(param);
+  double v;
+  if (rtc::GetDoubleFromJsonObject(root, param_name, &v)) {
+    *param = static_cast<float>(v);
+  }
+}
+
+void ReadParam(const Json::Value& root,
+               std::string param_name,
+               EchoCanceller3Config::Filter::MainConfiguration* param) {
+  RTC_DCHECK(param);
+  Json::Value json_array;
+  if (rtc::GetValueFromJsonObject(root, param_name, &json_array)) {
+    std::vector<double> v;
+    rtc::JsonArrayToDoubleVector(json_array, &v);
+    if (v.size() != 6) {
+      RTC_LOG(LS_ERROR) << "Incorrect array size for " << param_name;
+      return;
+    }
+    param->length_blocks = static_cast<size_t>(v[0]);
+    param->leakage_converged = static_cast<float>(v[1]);
+    param->leakage_diverged = static_cast<float>(v[2]);
+    param->error_floor = static_cast<float>(v[3]);
+    param->error_ceil = static_cast<float>(v[4]);
+    param->noise_gate = static_cast<float>(v[5]);
+  }
+}
+
+void ReadParam(const Json::Value& root,
+               std::string param_name,
+               EchoCanceller3Config::Filter::ShadowConfiguration* param) {
+  RTC_DCHECK(param);
+  Json::Value json_array;
+  if (rtc::GetValueFromJsonObject(root, param_name, &json_array)) {
+    std::vector<double> v;
+    rtc::JsonArrayToDoubleVector(json_array, &v);
+    if (v.size() != 3) {
+      RTC_LOG(LS_ERROR) << "Incorrect array size for " << param_name;
+      return;
+    }
+    param->length_blocks = static_cast<size_t>(v[0]);
+    param->rate = static_cast<float>(v[1]);
+    param->noise_gate = static_cast<float>(v[2]);
+  }
+}
+
+void ReadParam(const Json::Value& root,
+               std::string param_name,
+               EchoCanceller3Config::Suppressor::MaskingThresholds* param) {
+  RTC_DCHECK(param);
+  Json::Value json_array;
+  if (rtc::GetValueFromJsonObject(root, param_name, &json_array)) {
+    std::vector<double> v;
+    rtc::JsonArrayToDoubleVector(json_array, &v);
+    if (v.size() != 3) {
+      RTC_LOG(LS_ERROR) << "Incorrect array size for " << param_name;
+      return;
+    }
+    param->enr_transparent = static_cast<float>(v[0]);
+    param->enr_suppress = static_cast<float>(v[1]);
+    param->emr_transparent = static_cast<float>(v[2]);
+  }
+}
+}  // namespace
+
+EchoCanceller3Config Aec3ConfigFromJsonString(absl::string_view json_string) {
+  EchoCanceller3Config cfg;
+
+  Json::Value root;
+  bool success = Json::Reader().parse(std::string(json_string), root);
+  if (!success) {
+    RTC_LOG(LS_ERROR) << "Incorrect JSON format: " << json_string;
+    return EchoCanceller3Config();
+  }
+
+  Json::Value aec3_root;
+  success = rtc::GetValueFromJsonObject(root, "aec3", &aec3_root);
+  if (!success) {
+    RTC_LOG(LS_ERROR) << "Missing AEC3 config field: " << json_string;
+    return EchoCanceller3Config();
+  }
+
+  Json::Value section;
+  if (rtc::GetValueFromJsonObject(root, "buffering", &section)) {
+    ReadParam(section, "use_new_render_buffering",
+              &cfg.buffering.use_new_render_buffering);
+    ReadParam(section, "excess_render_detection_interval_blocks",
+              &cfg.buffering.excess_render_detection_interval_blocks);
+    ReadParam(section, "max_allowed_excess_render_blocks",
+              &cfg.buffering.max_allowed_excess_render_blocks);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "delay", &section)) {
+    ReadParam(section, "default_delay", &cfg.delay.default_delay);
+    ReadParam(section, "down_sampling_factor", &cfg.delay.down_sampling_factor);
+    ReadParam(section, "num_filters", &cfg.delay.num_filters);
+    ReadParam(section, "api_call_jitter_blocks",
+              &cfg.delay.api_call_jitter_blocks);
+    ReadParam(section, "min_echo_path_delay_blocks",
+              &cfg.delay.min_echo_path_delay_blocks);
+    ReadParam(section, "delay_headroom_blocks",
+              &cfg.delay.delay_headroom_blocks);
+    ReadParam(section, "hysteresis_limit_1_blocks",
+              &cfg.delay.hysteresis_limit_1_blocks);
+    ReadParam(section, "hysteresis_limit_2_blocks",
+              &cfg.delay.hysteresis_limit_2_blocks);
+    ReadParam(section, "skew_hysteresis_blocks",
+              &cfg.delay.skew_hysteresis_blocks);
+    ReadParam(section, "fixed_capture_delay_samples",
+              &cfg.delay.fixed_capture_delay_samples);
+    ReadParam(section, "delay_estimate_smoothing",
+              &cfg.delay.delay_estimate_smoothing);
+    ReadParam(section, "delay_candidate_detection_threshold",
+              &cfg.delay.delay_candidate_detection_threshold);
+
+    Json::Value subsection;
+    if (rtc::GetValueFromJsonObject(section, "delay_selection_thresholds",
+                                    &subsection)) {
+      ReadParam(subsection, "initial",
+                &cfg.delay.delay_selection_thresholds.initial);
+      ReadParam(subsection, "converged",
+                &cfg.delay.delay_selection_thresholds.converged);
+    }
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "filter", &section)) {
+    ReadParam(section, "main", &cfg.filter.main);
+    ReadParam(section, "shadow", &cfg.filter.shadow);
+    ReadParam(section, "main_initial", &cfg.filter.main_initial);
+    ReadParam(section, "shadow_initial", &cfg.filter.shadow_initial);
+    ReadParam(section, "config_change_duration_blocks",
+              &cfg.filter.config_change_duration_blocks);
+    ReadParam(section, "initial_state_seconds",
+              &cfg.filter.initial_state_seconds);
+    ReadParam(section, "conservative_initial_phase",
+              &cfg.filter.conservative_initial_phase);
+    ReadParam(section, "enable_shadow_filter_output_usage",
+              &cfg.filter.enable_shadow_filter_output_usage);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "erle", &section)) {
+    ReadParam(section, "min", &cfg.erle.min);
+    ReadParam(section, "max_l", &cfg.erle.max_l);
+    ReadParam(section, "max_h", &cfg.erle.max_h);
+    ReadParam(section, "onset_detection", &cfg.erle.onset_detection);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "ep_strength", &section)) {
+    ReadParam(section, "lf", &cfg.ep_strength.lf);
+    ReadParam(section, "mf", &cfg.ep_strength.mf);
+    ReadParam(section, "hf", &cfg.ep_strength.hf);
+    ReadParam(section, "default_len", &cfg.ep_strength.default_len);
+    ReadParam(section, "reverb_based_on_render",
+              &cfg.ep_strength.reverb_based_on_render);
+    ReadParam(section, "echo_can_saturate", &cfg.ep_strength.echo_can_saturate);
+    ReadParam(section, "bounded_erl", &cfg.ep_strength.bounded_erl);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "gain_mask", &section)) {
+    ReadParam(section, "m1", &cfg.gain_mask.m1);
+    ReadParam(section, "m2", &cfg.gain_mask.m2);
+    ReadParam(section, "m3", &cfg.gain_mask.m3);
+    ReadParam(section, "m5", &cfg.gain_mask.m5);
+    ReadParam(section, "m6", &cfg.gain_mask.m6);
+    ReadParam(section, "m7", &cfg.gain_mask.m7);
+    ReadParam(section, "m8", &cfg.gain_mask.m8);
+    ReadParam(section, "m9", &cfg.gain_mask.m9);
+
+    ReadParam(section, "gain_curve_offset", &cfg.gain_mask.gain_curve_offset);
+    ReadParam(section, "gain_curve_slope", &cfg.gain_mask.gain_curve_slope);
+    ReadParam(section, "temporal_masking_lf",
+              &cfg.gain_mask.temporal_masking_lf);
+    ReadParam(section, "temporal_masking_hf",
+              &cfg.gain_mask.temporal_masking_hf);
+    ReadParam(section, "temporal_masking_lf_bands",
+              &cfg.gain_mask.temporal_masking_lf_bands);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "echo_audibility", &section)) {
+    ReadParam(section, "low_render_limit",
+              &cfg.echo_audibility.low_render_limit);
+    ReadParam(section, "normal_render_limit",
+              &cfg.echo_audibility.normal_render_limit);
+
+    ReadParam(section, "floor_power", &cfg.echo_audibility.floor_power);
+    ReadParam(section, "audibility_threshold_lf",
+              &cfg.echo_audibility.audibility_threshold_lf);
+    ReadParam(section, "audibility_threshold_mf",
+              &cfg.echo_audibility.audibility_threshold_mf);
+    ReadParam(section, "audibility_threshold_hf",
+              &cfg.echo_audibility.audibility_threshold_hf);
+    ReadParam(section, "use_stationary_properties",
+              &cfg.echo_audibility.use_stationary_properties);
+    ReadParam(section, "use_stationary_properties_at_init",
+              &cfg.echo_audibility.use_stationarity_properties_at_init);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "echo_removal_control",
+                                  &section)) {
+    Json::Value subsection;
+    if (rtc::GetValueFromJsonObject(section, "gain_rampup", &subsection)) {
+      ReadParam(subsection, "initial_gain",
+                &cfg.echo_removal_control.gain_rampup.initial_gain);
+      ReadParam(subsection, "first_non_zero_gain",
+                &cfg.echo_removal_control.gain_rampup.first_non_zero_gain);
+      ReadParam(subsection, "non_zero_gain_blocks",
+                &cfg.echo_removal_control.gain_rampup.non_zero_gain_blocks);
+      ReadParam(subsection, "full_gain_blocks",
+                &cfg.echo_removal_control.gain_rampup.full_gain_blocks);
+    }
+    ReadParam(section, "has_clock_drift",
+              &cfg.echo_removal_control.has_clock_drift);
+    ReadParam(section, "linear_and_stable_echo_path",
+              &cfg.echo_removal_control.linear_and_stable_echo_path);
+  }
+
+  if (rtc::GetValueFromJsonObject(aec3_root, "echo_model", &section)) {
+    Json::Value subsection;
+    ReadParam(section, "noise_floor_hold", &cfg.echo_model.noise_floor_hold);
+    ReadParam(section, "min_noise_floor_power",
+              &cfg.echo_model.min_noise_floor_power);
+    ReadParam(section, "stationary_gate_slope",
+              &cfg.echo_model.stationary_gate_slope);
+    ReadParam(section, "noise_gate_power", &cfg.echo_model.noise_gate_power);
+    ReadParam(section, "noise_gate_slope", &cfg.echo_model.noise_gate_slope);
+    ReadParam(section, "render_pre_window_size",
+              &cfg.echo_model.render_pre_window_size);
+    ReadParam(section, "render_post_window_size",
+              &cfg.echo_model.render_post_window_size);
+    ReadParam(section, "render_pre_window_size_init",
+              &cfg.echo_model.render_pre_window_size_init);
+    ReadParam(section, "render_post_window_size_init",
+              &cfg.echo_model.render_post_window_size_init);
+    ReadParam(section, "nonlinear_hold", &cfg.echo_model.nonlinear_hold);
+    ReadParam(section, "nonlinear_release", &cfg.echo_model.nonlinear_release);
+  }
+
+  Json::Value subsection;
+  if (rtc::GetValueFromJsonObject(aec3_root, "suppressor", &section)) {
+    ReadParam(section, "nearend_average_blocks",
+              &cfg.suppressor.nearend_average_blocks);
+
+    if (rtc::GetValueFromJsonObject(section, "normal_tuning", &subsection)) {
+      ReadParam(subsection, "mask_lf", &cfg.suppressor.normal_tuning.mask_lf);
+      ReadParam(subsection, "mask_hf", &cfg.suppressor.normal_tuning.mask_hf);
+      ReadParam(subsection, "max_inc_factor",
+                &cfg.suppressor.normal_tuning.max_inc_factor);
+      ReadParam(subsection, "max_dec_factor_lf",
+                &cfg.suppressor.normal_tuning.max_dec_factor_lf);
+    }
+
+    if (rtc::GetValueFromJsonObject(section, "nearend_tuning", &subsection)) {
+      ReadParam(subsection, "mask_lf", &cfg.suppressor.nearend_tuning.mask_lf);
+      ReadParam(subsection, "mask_hf", &cfg.suppressor.nearend_tuning.mask_hf);
+      ReadParam(subsection, "max_inc_factor",
+                &cfg.suppressor.nearend_tuning.max_inc_factor);
+      ReadParam(subsection, "max_dec_factor_lf",
+                &cfg.suppressor.nearend_tuning.max_dec_factor_lf);
+    }
+
+    if (rtc::GetValueFromJsonObject(section, "dominant_nearend_detection",
+                                    &subsection)) {
+      ReadParam(subsection, "enr_threshold",
+                &cfg.suppressor.dominant_nearend_detection.enr_threshold);
+      ReadParam(subsection, "enr_exit_threshold",
+                &cfg.suppressor.dominant_nearend_detection.enr_exit_threshold);
+      ReadParam(subsection, "snr_threshold",
+                &cfg.suppressor.dominant_nearend_detection.snr_threshold);
+      ReadParam(subsection, "hold_duration",
+                &cfg.suppressor.dominant_nearend_detection.hold_duration);
+      ReadParam(subsection, "trigger_threshold",
+                &cfg.suppressor.dominant_nearend_detection.trigger_threshold);
+    }
+
+    if (rtc::GetValueFromJsonObject(section, "high_bands_suppression",
+                                    &subsection)) {
+      ReadParam(subsection, "enr_threshold",
+                &cfg.suppressor.high_bands_suppression.enr_threshold);
+      ReadParam(subsection, "max_gain_during_echo",
+                &cfg.suppressor.high_bands_suppression.max_gain_during_echo);
+    }
+
+    ReadParam(section, "floor_first_increase",
+              &cfg.suppressor.floor_first_increase);
+    ReadParam(section, "enforce_transparent",
+              &cfg.suppressor.enforce_transparent);
+    ReadParam(section, "enforce_empty_higher_bands",
+              &cfg.suppressor.enforce_empty_higher_bands);
+  }
+  return cfg;
+}
+
+std::string Aec3ConfigToJsonString(const EchoCanceller3Config& config) {
+  rtc::StringBuilder ost;
+  ost << "{";
+  ost << "\"aec3\": {";
+  ost << "\"delay\": {";
+  ost << "\"default_delay\": " << config.delay.default_delay << ",";
+  ost << "\"down_sampling_factor\": " << config.delay.down_sampling_factor
+      << ",";
+  ost << "\"num_filters\": " << config.delay.num_filters << ",";
+  ost << "\"api_call_jitter_blocks\": " << config.delay.api_call_jitter_blocks
+      << ",";
+  ost << "\"min_echo_path_delay_blocks\": "
+      << config.delay.min_echo_path_delay_blocks << ",";
+  ost << "\"delay_headroom_blocks\": " << config.delay.delay_headroom_blocks
+      << ",";
+  ost << "\"hysteresis_limit_1_blocks\": "
+      << config.delay.hysteresis_limit_1_blocks << ",";
+  ost << "\"hysteresis_limit_2_blocks\": "
+      << config.delay.hysteresis_limit_2_blocks << ",";
+  ost << "\"skew_hysteresis_blocks\": " << config.delay.skew_hysteresis_blocks
+      << ",";
+  ost << "\"fixed_capture_delay_samples\": "
+      << config.delay.fixed_capture_delay_samples << ",";
+  ost << "\"delay_estimate_smoothing\": "
+      << config.delay.delay_estimate_smoothing << ",";
+  ost << "\"delay_candidate_detection_threshold\": "
+      << config.delay.delay_candidate_detection_threshold << ",";
+
+  ost << "\"delay_selection_thresholds\": {";
+  ost << "\"initial\": " << config.delay.delay_selection_thresholds.initial
+      << ",";
+  ost << "\"converged\": " << config.delay.delay_selection_thresholds.converged;
+  ost << "}";
+
+  ost << "},";
+
+  ost << "\"filter\": {";
+  ost << "\"main\": [";
+  ost << config.filter.main.length_blocks << ",";
+  ost << config.filter.main.leakage_converged << ",";
+  ost << config.filter.main.leakage_diverged << ",";
+  ost << config.filter.main.error_floor << ",";
+  ost << config.filter.main.error_ceil << ",";
+  ost << config.filter.main.noise_gate;
+  ost << "],";
+
+  ost << "\"shadow\": [";
+  ost << config.filter.shadow.length_blocks << ",";
+  ost << config.filter.shadow.rate << ",";
+  ost << config.filter.shadow.noise_gate;
+  ost << "],";
+
+  ost << "\"main_initial\": [";
+  ost << config.filter.main_initial.length_blocks << ",";
+  ost << config.filter.main_initial.leakage_converged << ",";
+  ost << config.filter.main_initial.leakage_diverged << ",";
+  ost << config.filter.main_initial.error_floor << ",";
+  ost << config.filter.main_initial.error_ceil << ",";
+  ost << config.filter.main_initial.noise_gate;
+  ost << "],";
+
+  ost << "\"shadow_initial\": [";
+  ost << config.filter.shadow_initial.length_blocks << ",";
+  ost << config.filter.shadow_initial.rate << ",";
+  ost << config.filter.shadow_initial.noise_gate;
+  ost << "],";
+
+  ost << "\"config_change_duration_blocks\": "
+      << config.filter.config_change_duration_blocks << ",";
+  ost << "\"initial_state_seconds\": " << config.filter.initial_state_seconds
+      << ",";
+  ost << "\"conservative_initial_phase\": "
+      << (config.filter.conservative_initial_phase ? "true" : "false") << ",";
+  ost << "\"enable_shadow_filter_output_usage\": "
+      << (config.filter.enable_shadow_filter_output_usage ? "true" : "false");
+
+  ost << "},";
+
+  ost << "\"erle\": {";
+  ost << "\"min\": " << config.erle.min << ",";
+  ost << "\"max_l\": " << config.erle.max_l << ",";
+  ost << "\"max_h\": " << config.erle.max_h << ",";
+  ost << "\"onset_detection\": "
+      << (config.erle.onset_detection ? "true" : "false");
+  ost << "},";
+
+  ost << "\"ep_strength\": {";
+  ost << "\"lf\": " << config.ep_strength.lf << ",";
+  ost << "\"mf\": " << config.ep_strength.mf << ",";
+  ost << "\"hf\": " << config.ep_strength.hf << ",";
+  ost << "\"default_len\": " << config.ep_strength.default_len << ",";
+  ost << "\"reverb_based_on_render\": "
+      << (config.ep_strength.reverb_based_on_render ? "true" : "false") << ",";
+  ost << "\"echo_can_saturate\": "
+      << (config.ep_strength.echo_can_saturate ? "true" : "false") << ",";
+  ost << "\"bounded_erl\": "
+      << (config.ep_strength.bounded_erl ? "true" : "false");
+
+  ost << "},";
+
+  ost << "\"gain_mask\": {";
+  ost << "\"m0\": " << config.gain_mask.m0 << ",";
+  ost << "\"m1\": " << config.gain_mask.m1 << ",";
+  ost << "\"m2\": " << config.gain_mask.m2 << ",";
+  ost << "\"m3\": " << config.gain_mask.m3 << ",";
+  ost << "\"m5\": " << config.gain_mask.m5 << ",";
+  ost << "\"m6\": " << config.gain_mask.m6 << ",";
+  ost << "\"m7\": " << config.gain_mask.m7 << ",";
+  ost << "\"m8\": " << config.gain_mask.m8 << ",";
+  ost << "\"m9\": " << config.gain_mask.m9 << ",";
+  ost << "\"gain_curve_offset\": " << config.gain_mask.gain_curve_offset << ",";
+  ost << "\"gain_curve_slope\": " << config.gain_mask.gain_curve_slope << ",";
+  ost << "\"temporal_masking_lf\": " << config.gain_mask.temporal_masking_lf
+      << ",";
+  ost << "\"temporal_masking_hf\": " << config.gain_mask.temporal_masking_hf
+      << ",";
+  ost << "\"temporal_masking_lf_bands\": "
+      << config.gain_mask.temporal_masking_lf_bands;
+  ost << "},";
+
+  ost << "\"echo_audibility\": {";
+  ost << "\"low_render_limit\": " << config.echo_audibility.low_render_limit
+      << ",";
+  ost << "\"normal_render_limit\": "
+      << config.echo_audibility.normal_render_limit << ",";
+  ost << "\"floor_power\": " << config.echo_audibility.floor_power << ",";
+  ost << "\"audibility_threshold_lf\": "
+      << config.echo_audibility.audibility_threshold_lf << ",";
+  ost << "\"audibility_threshold_mf\": "
+      << config.echo_audibility.audibility_threshold_mf << ",";
+  ost << "\"audibility_threshold_hf\": "
+      << config.echo_audibility.audibility_threshold_hf << ",";
+  ost << "\"use_stationary_properties\": "
+      << (config.echo_audibility.use_stationary_properties ? "true" : "false")
+      << ",";
+  ost << "\"use_stationarity_properties_at_init\": "
+      << (config.echo_audibility.use_stationarity_properties_at_init ? "true"
+                                                                     : "false");
+  ost << "},";
+
+  ost << "\"render_levels\": {";
+  ost << "\"active_render_limit\": " << config.render_levels.active_render_limit
+      << ",";
+  ost << "\"poor_excitation_render_limit\": "
+      << config.render_levels.poor_excitation_render_limit << ",";
+  ost << "\"poor_excitation_render_limit_ds8\": "
+      << config.render_levels.poor_excitation_render_limit_ds8;
+  ost << "},";
+
+  ost << "\"echo_removal_control\": {";
+  ost << "\"gain_rampup\": {";
+  ost << "\"initial_gain\": "
+      << config.echo_removal_control.gain_rampup.initial_gain << ",";
+  ost << "\"first_non_zero_gain\": "
+      << config.echo_removal_control.gain_rampup.first_non_zero_gain << ",";
+  ost << "\"non_zero_gain_blocks\": "
+      << config.echo_removal_control.gain_rampup.non_zero_gain_blocks << ",";
+  ost << "\"full_gain_blocks\": "
+      << config.echo_removal_control.gain_rampup.full_gain_blocks;
+  ost << "},";
+  ost << "\"has_clock_drift\": "
+      << (config.echo_removal_control.has_clock_drift ? "true" : "false")
+      << ",";
+  ost << "\"linear_and_stable_echo_path\": "
+      << (config.echo_removal_control.linear_and_stable_echo_path ? "true"
+                                                                  : "false");
+
+  ost << "},";
+
+  ost << "\"echo_model\": {";
+  ost << "\"noise_floor_hold\": " << config.echo_model.noise_floor_hold << ",";
+  ost << "\"min_noise_floor_power\": "
+      << config.echo_model.min_noise_floor_power << ",";
+  ost << "\"stationary_gate_slope\": "
+      << config.echo_model.stationary_gate_slope << ",";
+  ost << "\"noise_gate_power\": " << config.echo_model.noise_gate_power << ",";
+  ost << "\"noise_gate_slope\": " << config.echo_model.noise_gate_slope << ",";
+  ost << "\"render_pre_window_size\": "
+      << config.echo_model.render_pre_window_size << ",";
+  ost << "\"render_post_window_size\": "
+      << config.echo_model.render_post_window_size << ",";
+  ost << "\"render_pre_window_size_init\": "
+      << config.echo_model.render_pre_window_size_init << ",";
+  ost << "\"render_post_window_size_init\": "
+      << config.echo_model.render_post_window_size_init << ",";
+  ost << "\"nonlinear_hold\": " << config.echo_model.nonlinear_hold << ",";
+  ost << "\"nonlinear_release\": " << config.echo_model.nonlinear_release;
+  ost << "},";
+
+  ost << "\"suppressor\": {";
+  ost << "\"nearend_average_blocks\": "
+      << config.suppressor.nearend_average_blocks << ",";
+  ost << "\"normal_tuning\": {";
+  ost << "\"mask_lf\": [";
+  ost << config.suppressor.normal_tuning.mask_lf.enr_transparent << ",";
+  ost << config.suppressor.normal_tuning.mask_lf.enr_suppress << ",";
+  ost << config.suppressor.normal_tuning.mask_lf.emr_transparent;
+  ost << "],";
+  ost << "\"mask_hf\": [";
+  ost << config.suppressor.normal_tuning.mask_hf.enr_transparent << ",";
+  ost << config.suppressor.normal_tuning.mask_hf.enr_suppress << ",";
+  ost << config.suppressor.normal_tuning.mask_hf.emr_transparent;
+  ost << "],";
+  ost << "\"max_inc_factor\": "
+      << config.suppressor.normal_tuning.max_inc_factor << ",";
+  ost << "\"max_dec_factor_lf\": "
+      << config.suppressor.normal_tuning.max_dec_factor_lf;
+  ost << "},";
+  ost << "\"nearend_tuning\": {";
+  ost << "\"mask_lf\": [";
+  ost << config.suppressor.nearend_tuning.mask_lf.enr_transparent << ",";
+  ost << config.suppressor.nearend_tuning.mask_lf.enr_suppress << ",";
+  ost << config.suppressor.nearend_tuning.mask_lf.emr_transparent;
+  ost << "],";
+  ost << "\"mask_hf\": [";
+  ost << config.suppressor.nearend_tuning.mask_hf.enr_transparent << ",";
+  ost << config.suppressor.nearend_tuning.mask_hf.enr_suppress << ",";
+  ost << config.suppressor.nearend_tuning.mask_hf.emr_transparent;
+  ost << "],";
+  ost << "\"max_inc_factor\": "
+      << config.suppressor.nearend_tuning.max_inc_factor << ",";
+  ost << "\"max_dec_factor_lf\": "
+      << config.suppressor.nearend_tuning.max_dec_factor_lf;
+  ost << "},";
+  ost << "\"dominant_nearend_detection\": {";
+  ost << "\"enr_threshold\": "
+      << config.suppressor.dominant_nearend_detection.enr_threshold << ",";
+  ost << "\"enr_exit_threshold\": "
+      << config.suppressor.dominant_nearend_detection.enr_exit_threshold << ",";
+  ost << "\"snr_threshold\": "
+      << config.suppressor.dominant_nearend_detection.snr_threshold << ",";
+  ost << "\"hold_duration\": "
+      << config.suppressor.dominant_nearend_detection.hold_duration << ",";
+  ost << "\"trigger_threshold\": "
+      << config.suppressor.dominant_nearend_detection.trigger_threshold;
+  ost << "},";
+  ost << "\"high_bands_suppression\": {";
+  ost << "\"enr_threshold\": "
+      << config.suppressor.high_bands_suppression.enr_threshold << ",";
+  ost << "\"max_gain_during_echo\": "
+      << config.suppressor.high_bands_suppression.max_gain_during_echo;
+  ost << "},";
+  ost << "\"floor_first_increase\": " << config.suppressor.floor_first_increase
+      << ",";
+  ost << "\"enforce_transparent\": "
+      << (config.suppressor.enforce_transparent ? "true" : "false") << ",";
+  ost << "\"enforce_empty_higher_bands\": "
+      << (config.suppressor.enforce_empty_higher_bands ? "true" : "false");
+  ost << "}";
+  ost << "}";
+  ost << "}";
+
+  return ost.Release();
+}
+}  // namespace webrtc
diff --git a/api/audio/echo_canceller3_config_json.h b/api/audio/echo_canceller3_config_json.h
new file mode 100644
index 0000000..b315bf0
--- /dev/null
+++ b/api/audio/echo_canceller3_config_json.h
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2018 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 API_AUDIO_ECHO_CANCELLER3_CONFIG_JSON_H_
+#define API_AUDIO_ECHO_CANCELLER3_CONFIG_JSON_H_
+
+#include <string>
+
+#include "absl/strings/string_view.h"
+#include "api/audio/echo_canceller3_config.h"
+
+namespace webrtc {
+// Parses a JSON-encoded string into an Aec3 config. Fields corresponds to
+// substruct names, with the addition that there must be a top-level node
+// "aec3". Returns default config values for anything that cannot be parsed from
+// the string.
+EchoCanceller3Config Aec3ConfigFromJsonString(absl::string_view json_string);
+
+// Encodes an Aec3 config in JSON format. Fields corresponds to substruct names,
+// with the addition that the top-level node is named "aec3".
+std::string Aec3ConfigToJsonString(const EchoCanceller3Config& config);
+
+}  // namespace webrtc
+
+#endif  // API_AUDIO_ECHO_CANCELLER3_CONFIG_JSON_H_
diff --git a/api/audio/echo_canceller3_factory.h b/api/audio/echo_canceller3_factory.h
index f6db116..9052d99 100644
--- a/api/audio/echo_canceller3_factory.h
+++ b/api/audio/echo_canceller3_factory.h
@@ -15,10 +15,11 @@
 
 #include "api/audio/echo_canceller3_config.h"
 #include "api/audio/echo_control.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace webrtc {
 
-class EchoCanceller3Factory : public EchoControlFactory {
+class RTC_EXPORT EchoCanceller3Factory : public EchoControlFactory {
  public:
   // Factory producing EchoCanceller3 instances with the default configuration.
   EchoCanceller3Factory();
diff --git a/api/audio_options.cc b/api/audio_options.cc
index c196d7d..a4a37d2 100644
--- a/api/audio_options.cc
+++ b/api/audio_options.cc
@@ -10,9 +10,120 @@
 
 #include "api/audio_options.h"
 
+#include "rtc_base/strings/string_builder.h"
+
 namespace cricket {
+namespace {
+
+template <class T>
+void ToStringIfSet(rtc::SimpleStringBuilder* result,
+                   const char* key,
+                   const absl::optional<T>& val) {
+  if (val) {
+    (*result) << key << ": " << *val << ", ";
+  }
+}
+
+template <typename T>
+void SetFrom(absl::optional<T>* s, const absl::optional<T>& o) {
+  if (o) {
+    *s = o;
+  }
+}
+
+}  // namespace
 
 AudioOptions::AudioOptions() = default;
 AudioOptions::~AudioOptions() = default;
 
+void AudioOptions::SetAll(const AudioOptions& change) {
+  SetFrom(&echo_cancellation, change.echo_cancellation);
+#if defined(WEBRTC_IOS)
+  SetFrom(&ios_force_software_aec_HACK, change.ios_force_software_aec_HACK);
+#endif
+  SetFrom(&auto_gain_control, change.auto_gain_control);
+  SetFrom(&noise_suppression, change.noise_suppression);
+  SetFrom(&highpass_filter, change.highpass_filter);
+  SetFrom(&stereo_swapping, change.stereo_swapping);
+  SetFrom(&audio_jitter_buffer_max_packets,
+          change.audio_jitter_buffer_max_packets);
+  SetFrom(&audio_jitter_buffer_fast_accelerate,
+          change.audio_jitter_buffer_fast_accelerate);
+  SetFrom(&typing_detection, change.typing_detection);
+  SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
+  SetFrom(&experimental_agc, change.experimental_agc);
+  SetFrom(&extended_filter_aec, change.extended_filter_aec);
+  SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
+  SetFrom(&experimental_ns, change.experimental_ns);
+  SetFrom(&residual_echo_detector, change.residual_echo_detector);
+  SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
+  SetFrom(&tx_agc_digital_compression_gain,
+          change.tx_agc_digital_compression_gain);
+  SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
+  SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
+  SetFrom(&audio_network_adaptor, change.audio_network_adaptor);
+  SetFrom(&audio_network_adaptor_config, change.audio_network_adaptor_config);
+}
+
+bool AudioOptions::operator==(const AudioOptions& o) const {
+  return echo_cancellation == o.echo_cancellation &&
+#if defined(WEBRTC_IOS)
+         ios_force_software_aec_HACK == o.ios_force_software_aec_HACK &&
+#endif
+         auto_gain_control == o.auto_gain_control &&
+         noise_suppression == o.noise_suppression &&
+         highpass_filter == o.highpass_filter &&
+         stereo_swapping == o.stereo_swapping &&
+         audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets &&
+         audio_jitter_buffer_fast_accelerate ==
+             o.audio_jitter_buffer_fast_accelerate &&
+         typing_detection == o.typing_detection &&
+         aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
+         experimental_agc == o.experimental_agc &&
+         extended_filter_aec == o.extended_filter_aec &&
+         delay_agnostic_aec == o.delay_agnostic_aec &&
+         experimental_ns == o.experimental_ns &&
+         residual_echo_detector == o.residual_echo_detector &&
+         tx_agc_target_dbov == o.tx_agc_target_dbov &&
+         tx_agc_digital_compression_gain == o.tx_agc_digital_compression_gain &&
+         tx_agc_limiter == o.tx_agc_limiter &&
+         combined_audio_video_bwe == o.combined_audio_video_bwe &&
+         audio_network_adaptor == o.audio_network_adaptor &&
+         audio_network_adaptor_config == o.audio_network_adaptor_config;
+}
+
+std::string AudioOptions::ToString() const {
+  char buffer[1024];
+  rtc::SimpleStringBuilder result(buffer);
+  result << "AudioOptions {";
+  ToStringIfSet(&result, "aec", echo_cancellation);
+#if defined(WEBRTC_IOS)
+  ToStringIfSet(&result, "ios_force_software_aec_HACK",
+                ios_force_software_aec_HACK);
+#endif
+  ToStringIfSet(&result, "agc", auto_gain_control);
+  ToStringIfSet(&result, "ns", noise_suppression);
+  ToStringIfSet(&result, "hf", highpass_filter);
+  ToStringIfSet(&result, "swap", stereo_swapping);
+  ToStringIfSet(&result, "audio_jitter_buffer_max_packets",
+                audio_jitter_buffer_max_packets);
+  ToStringIfSet(&result, "audio_jitter_buffer_fast_accelerate",
+                audio_jitter_buffer_fast_accelerate);
+  ToStringIfSet(&result, "typing", typing_detection);
+  ToStringIfSet(&result, "comfort_noise", aecm_generate_comfort_noise);
+  ToStringIfSet(&result, "experimental_agc", experimental_agc);
+  ToStringIfSet(&result, "extended_filter_aec", extended_filter_aec);
+  ToStringIfSet(&result, "delay_agnostic_aec", delay_agnostic_aec);
+  ToStringIfSet(&result, "experimental_ns", experimental_ns);
+  ToStringIfSet(&result, "residual_echo_detector", residual_echo_detector);
+  ToStringIfSet(&result, "tx_agc_target_dbov", tx_agc_target_dbov);
+  ToStringIfSet(&result, "tx_agc_digital_compression_gain",
+                tx_agc_digital_compression_gain);
+  ToStringIfSet(&result, "tx_agc_limiter", tx_agc_limiter);
+  ToStringIfSet(&result, "combined_audio_video_bwe", combined_audio_video_bwe);
+  ToStringIfSet(&result, "audio_network_adaptor", audio_network_adaptor);
+  result << "}";
+  return result.str();
+}
+
 }  // namespace cricket
diff --git a/api/audio_options.h b/api/audio_options.h
index aefc7a1..aecff41 100644
--- a/api/audio_options.h
+++ b/api/audio_options.h
@@ -14,7 +14,6 @@
 #include <string>
 
 #include "absl/types/optional.h"
-#include "rtc_base/stringencode.h"
 
 namespace cricket {
 
@@ -25,101 +24,12 @@
 struct AudioOptions {
   AudioOptions();
   ~AudioOptions();
-  void SetAll(const AudioOptions& change) {
-    SetFrom(&echo_cancellation, change.echo_cancellation);
-#if defined(WEBRTC_IOS)
-    SetFrom(&ios_force_software_aec_HACK, change.ios_force_software_aec_HACK);
-#endif
-    SetFrom(&auto_gain_control, change.auto_gain_control);
-    SetFrom(&noise_suppression, change.noise_suppression);
-    SetFrom(&highpass_filter, change.highpass_filter);
-    SetFrom(&stereo_swapping, change.stereo_swapping);
-    SetFrom(&audio_jitter_buffer_max_packets,
-            change.audio_jitter_buffer_max_packets);
-    SetFrom(&audio_jitter_buffer_fast_accelerate,
-            change.audio_jitter_buffer_fast_accelerate);
-    SetFrom(&typing_detection, change.typing_detection);
-    SetFrom(&aecm_generate_comfort_noise, change.aecm_generate_comfort_noise);
-    SetFrom(&experimental_agc, change.experimental_agc);
-    SetFrom(&extended_filter_aec, change.extended_filter_aec);
-    SetFrom(&delay_agnostic_aec, change.delay_agnostic_aec);
-    SetFrom(&experimental_ns, change.experimental_ns);
-    SetFrom(&residual_echo_detector, change.residual_echo_detector);
-    SetFrom(&tx_agc_target_dbov, change.tx_agc_target_dbov);
-    SetFrom(&tx_agc_digital_compression_gain,
-            change.tx_agc_digital_compression_gain);
-    SetFrom(&tx_agc_limiter, change.tx_agc_limiter);
-    SetFrom(&combined_audio_video_bwe, change.combined_audio_video_bwe);
-    SetFrom(&audio_network_adaptor, change.audio_network_adaptor);
-    SetFrom(&audio_network_adaptor_config, change.audio_network_adaptor_config);
-  }
+  void SetAll(const AudioOptions& change);
 
-  bool operator==(const AudioOptions& o) const {
-    return echo_cancellation == o.echo_cancellation &&
-#if defined(WEBRTC_IOS)
-           ios_force_software_aec_HACK == o.ios_force_software_aec_HACK &&
-#endif
-           auto_gain_control == o.auto_gain_control &&
-           noise_suppression == o.noise_suppression &&
-           highpass_filter == o.highpass_filter &&
-           stereo_swapping == o.stereo_swapping &&
-           audio_jitter_buffer_max_packets ==
-               o.audio_jitter_buffer_max_packets &&
-           audio_jitter_buffer_fast_accelerate ==
-               o.audio_jitter_buffer_fast_accelerate &&
-           typing_detection == o.typing_detection &&
-           aecm_generate_comfort_noise == o.aecm_generate_comfort_noise &&
-           experimental_agc == o.experimental_agc &&
-           extended_filter_aec == o.extended_filter_aec &&
-           delay_agnostic_aec == o.delay_agnostic_aec &&
-           experimental_ns == o.experimental_ns &&
-           residual_echo_detector == o.residual_echo_detector &&
-           tx_agc_target_dbov == o.tx_agc_target_dbov &&
-           tx_agc_digital_compression_gain ==
-               o.tx_agc_digital_compression_gain &&
-           tx_agc_limiter == o.tx_agc_limiter &&
-           combined_audio_video_bwe == o.combined_audio_video_bwe &&
-           audio_network_adaptor == o.audio_network_adaptor &&
-           audio_network_adaptor_config == o.audio_network_adaptor_config;
-  }
+  bool operator==(const AudioOptions& o) const;
   bool operator!=(const AudioOptions& o) const { return !(*this == o); }
 
-  std::string ToString() const {
-    std::ostringstream ost;
-    ost << "AudioOptions {";
-    ost << ToStringIfSet("aec", echo_cancellation);
-#if defined(WEBRTC_IOS)
-    ost << ToStringIfSet("ios_force_software_aec_HACK",
-                         ios_force_software_aec_HACK);
-#endif
-    ost << ToStringIfSet("agc", auto_gain_control);
-    ost << ToStringIfSet("ns", noise_suppression);
-    ost << ToStringIfSet("hf", highpass_filter);
-    ost << ToStringIfSet("swap", stereo_swapping);
-    ost << ToStringIfSet("audio_jitter_buffer_max_packets",
-                         audio_jitter_buffer_max_packets);
-    ost << ToStringIfSet("audio_jitter_buffer_fast_accelerate",
-                         audio_jitter_buffer_fast_accelerate);
-    ost << ToStringIfSet("typing", typing_detection);
-    ost << ToStringIfSet("comfort_noise", aecm_generate_comfort_noise);
-    ost << ToStringIfSet("experimental_agc", experimental_agc);
-    ost << ToStringIfSet("extended_filter_aec", extended_filter_aec);
-    ost << ToStringIfSet("delay_agnostic_aec", delay_agnostic_aec);
-    ost << ToStringIfSet("experimental_ns", experimental_ns);
-    ost << ToStringIfSet("residual_echo_detector", residual_echo_detector);
-    ost << ToStringIfSet("tx_agc_target_dbov", tx_agc_target_dbov);
-    ost << ToStringIfSet("tx_agc_digital_compression_gain",
-                         tx_agc_digital_compression_gain);
-    ost << ToStringIfSet("tx_agc_limiter", tx_agc_limiter);
-    ost << ToStringIfSet("combined_audio_video_bwe", combined_audio_video_bwe);
-    ost << ToStringIfSet("audio_network_adaptor", audio_network_adaptor);
-    // The adaptor config is a serialized proto buffer and therefore not human
-    // readable. So we comment out the following line.
-    // ost << ToStringIfSet("audio_network_adaptor_config",
-    //     audio_network_adaptor_config);
-    ost << "}";
-    return ost.str();
-  }
+  std::string ToString() const;
 
   // Audio processing that attempts to filter away the output signal from
   // later inbound pickup.
@@ -164,27 +74,6 @@
   absl::optional<bool> audio_network_adaptor;
   // Config string for audio network adaptor.
   absl::optional<std::string> audio_network_adaptor_config;
-
- private:
-  template <class T>
-  static std::string ToStringIfSet(const char* key,
-                                   const absl::optional<T>& val) {
-    std::string str;
-    if (val) {
-      str = key;
-      str += ": ";
-      str += val ? rtc::ToString(*val) : "";
-      str += ", ";
-    }
-    return str;
-  }
-
-  template <typename T>
-  static void SetFrom(absl::optional<T>* s, const absl::optional<T>& o) {
-    if (o) {
-      *s = o;
-    }
-  }
 };
 
 }  // namespace cricket
diff --git a/api/candidate.cc b/api/candidate.cc
index d51fb84..10751ae 100644
--- a/api/candidate.cc
+++ b/api/candidate.cc
@@ -10,6 +10,9 @@
 
 #include "api/candidate.h"
 
+#include "rtc_base/helpers.h"
+#include "rtc_base/strings/string_builder.h"
+
 namespace cricket {
 
 Candidate::Candidate()
@@ -68,7 +71,7 @@
 }
 
 std::string Candidate::ToStringInternal(bool sensitive) const {
-  std::ostringstream ost;
+  rtc::StringBuilder ost;
   std::string address =
       sensitive ? address_.ToSensitiveString() : address_.ToString();
   ost << "Cand[" << transport_name_ << ":" << foundation_ << ":" << component_
@@ -76,7 +79,7 @@
       << ":" << related_address_.ToString() << ":" << username_ << ":"
       << password_ << ":" << network_id_ << ":" << network_cost_ << ":"
       << generation_ << "]";
-  return ost.str();
+  return ost.Release();
 }
 
 uint32_t Candidate::GetPriority(uint32_t type_preference,
diff --git a/api/candidate.h b/api/candidate.h
index 6e0547b..0a84591 100644
--- a/api/candidate.h
+++ b/api/candidate.h
@@ -18,7 +18,6 @@
 #include <string>
 
 #include "rtc_base/checks.h"
-#include "rtc_base/helpers.h"
 #include "rtc_base/network_constants.h"
 #include "rtc_base/socketaddress.h"
 
diff --git a/api/dtmfsenderinterface.h b/api/dtmfsenderinterface.h
index b79bb31..d8714f5 100644
--- a/api/dtmfsenderinterface.h
+++ b/api/dtmfsenderinterface.h
@@ -26,7 +26,14 @@
   // Triggered when DTMF |tone| is sent.
   // If |tone| is empty that means the DtmfSender has sent out all the given
   // tones.
-  virtual void OnToneChange(const std::string& tone) = 0;
+  // The callback includes the state of the tone buffer at the time when
+  // the tone finished playing.
+  virtual void OnToneChange(const std::string& tone,
+                            const std::string& tone_buffer) {}
+  // DEPRECATED: Older API without tone buffer.
+  // TODO(bugs.webrtc.org/9725): Remove old API and default implementation
+  // when old callers are gone.
+  virtual void OnToneChange(const std::string& tone) {}
 
  protected:
   virtual ~DtmfSenderObserverInterface() = default;
diff --git a/api/jsepicecandidate.h b/api/jsepicecandidate.h
index 50520fe..a57f861 100644
--- a/api/jsepicecandidate.h
+++ b/api/jsepicecandidate.h
@@ -15,7 +15,6 @@
 #define API_JSEPICECANDIDATE_H_
 
 #include <string>
-#include <utility>
 #include <vector>
 
 #include "api/candidate.h"
diff --git a/api/media_transport_interface.cc b/api/media_transport_interface.cc
new file mode 100644
index 0000000..fc97b0b
--- /dev/null
+++ b/api/media_transport_interface.cc
@@ -0,0 +1,76 @@
+/*
+ *  Copyright 2018 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.
+ */
+
+// This is EXPERIMENTAL interface for media transport.
+//
+// The goal is to refactor WebRTC code so that audio and video frames
+// are sent / received through the media transport interface. This will
+// enable different media transport implementations, including QUIC-based
+// media transport.
+
+#include "api/media_transport_interface.h"
+
+namespace webrtc {
+
+MediaTransportEncodedAudioFrame::~MediaTransportEncodedAudioFrame() {}
+
+MediaTransportEncodedAudioFrame::MediaTransportEncodedAudioFrame(
+    int sampling_rate_hz,
+    int starting_sample_index,
+    int samples_per_channel,
+    int sequence_number,
+    FrameType frame_type,
+    uint8_t payload_type,
+    std::vector<uint8_t> encoded_data)
+    : sampling_rate_hz_(sampling_rate_hz),
+      starting_sample_index_(starting_sample_index),
+      samples_per_channel_(samples_per_channel),
+      sequence_number_(sequence_number),
+      frame_type_(frame_type),
+      payload_type_(payload_type),
+      encoded_data_(std::move(encoded_data)) {}
+
+MediaTransportEncodedAudioFrame& MediaTransportEncodedAudioFrame::operator=(
+    const MediaTransportEncodedAudioFrame&) = default;
+
+MediaTransportEncodedAudioFrame& MediaTransportEncodedAudioFrame::operator=(
+    MediaTransportEncodedAudioFrame&&) = default;
+
+MediaTransportEncodedAudioFrame::MediaTransportEncodedAudioFrame(
+    const MediaTransportEncodedAudioFrame&) = default;
+
+MediaTransportEncodedAudioFrame::MediaTransportEncodedAudioFrame(
+    MediaTransportEncodedAudioFrame&&) = default;
+
+MediaTransportEncodedVideoFrame::~MediaTransportEncodedVideoFrame() {}
+
+MediaTransportEncodedVideoFrame::MediaTransportEncodedVideoFrame(
+    int64_t frame_id,
+    std::vector<int64_t> referenced_frame_ids,
+    VideoCodecType codec_type,
+    const webrtc::EncodedImage& encoded_image)
+    : codec_type_(codec_type),
+      encoded_image_(encoded_image),
+      frame_id_(frame_id),
+      referenced_frame_ids_(std::move(referenced_frame_ids)) {}
+
+MediaTransportEncodedVideoFrame& MediaTransportEncodedVideoFrame::operator=(
+    const MediaTransportEncodedVideoFrame&) = default;
+
+MediaTransportEncodedVideoFrame& MediaTransportEncodedVideoFrame::operator=(
+    MediaTransportEncodedVideoFrame&&) = default;
+
+MediaTransportEncodedVideoFrame::MediaTransportEncodedVideoFrame(
+    const MediaTransportEncodedVideoFrame&) = default;
+
+MediaTransportEncodedVideoFrame::MediaTransportEncodedVideoFrame(
+    MediaTransportEncodedVideoFrame&&) = default;
+
+}  // namespace webrtc
diff --git a/api/media_transport_interface.h b/api/media_transport_interface.h
new file mode 100644
index 0000000..33fa2ad
--- /dev/null
+++ b/api/media_transport_interface.h
@@ -0,0 +1,250 @@
+/* Copyright 2018 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.
+ */
+
+// This is EXPERIMENTAL interface for media transport.
+//
+// The goal is to refactor WebRTC code so that audio and video frames
+// are sent / received through the media transport interface. This will
+// enable different media transport implementations, including QUIC-based
+// media transport.
+
+#ifndef API_MEDIA_TRANSPORT_INTERFACE_H_
+#define API_MEDIA_TRANSPORT_INTERFACE_H_
+
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "api/rtcerror.h"
+#include "api/video/encoded_image.h"
+#include "common_types.h"  // NOLINT(build/include)
+
+namespace rtc {
+class PacketTransportInternal;
+class Thread;
+}  // namespace rtc
+
+namespace webrtc {
+
+// Represents encoded audio frame in any encoding (type of encoding is opaque).
+// To avoid copying of encoded data use move semantics when passing by value.
+class MediaTransportEncodedAudioFrame final {
+ public:
+  enum class FrameType {
+    // Normal audio frame (equivalent to webrtc::kAudioFrameSpeech).
+    kSpeech,
+
+    // DTX frame (equivalent to webrtc::kAudioFrameCN).
+    kDiscountinuousTransmission,
+  };
+
+  MediaTransportEncodedAudioFrame(
+      // Audio sampling rate, for example 48000.
+      int sampling_rate_hz,
+
+      // Starting sample index of the frame, i.e. how many audio samples were
+      // before this frame since the beginning of the call or beginning of time
+      // in one channel (the starting point should not matter for NetEq). In
+      // WebRTC it is used as a timestamp of the frame.
+      // TODO(sukhanov): Starting_sample_index is currently adjusted on the
+      // receiver side in RTP path. Non-RTP implementations should preserve it.
+      // For NetEq initial offset should not matter so we should consider fixing
+      // RTP path.
+      int starting_sample_index,
+
+      // Number of audio samples in audio frame in 1 channel.
+      int samples_per_channel,
+
+      // Sequence number of the frame in the order sent, it is currently
+      // required by NetEq, but we can fix NetEq, because starting_sample_index
+      // should be enough.
+      int sequence_number,
+
+      // If audio frame is a speech or discontinued transmission.
+      FrameType frame_type,
+
+      // Opaque payload type. In RTP codepath payload type is stored in RTP
+      // header. In other implementations it should be simply passed through the
+      // wire -- it's needed for decoder.
+      uint8_t payload_type,
+
+      // Vector with opaque encoded data.
+      std::vector<uint8_t> encoded_data);
+
+  ~MediaTransportEncodedAudioFrame();
+  MediaTransportEncodedAudioFrame(const MediaTransportEncodedAudioFrame&);
+  MediaTransportEncodedAudioFrame& operator=(
+      const MediaTransportEncodedAudioFrame& other);
+  MediaTransportEncodedAudioFrame& operator=(
+      MediaTransportEncodedAudioFrame&& other);
+  MediaTransportEncodedAudioFrame(MediaTransportEncodedAudioFrame&&);
+
+  // Getters.
+  int sampling_rate_hz() const { return sampling_rate_hz_; }
+  int starting_sample_index() const { return starting_sample_index_; }
+  int samples_per_channel() const { return samples_per_channel_; }
+  int sequence_number() const { return sequence_number_; }
+
+  uint8_t payload_type() const { return payload_type_; }
+  FrameType frame_type() const { return frame_type_; }
+
+  rtc::ArrayView<const uint8_t> encoded_data() const { return encoded_data_; }
+
+ private:
+  int sampling_rate_hz_;
+  int starting_sample_index_;
+  int samples_per_channel_;
+
+  // TODO(sukhanov): Refactor NetEq so we don't need sequence number.
+  // Having sample_index and samples_per_channel should be enough.
+  int sequence_number_;
+
+  FrameType frame_type_;
+
+  // TODO(sukhanov): Consider enumerating allowed encodings and store enum
+  // instead of uint payload_type.
+  uint8_t payload_type_;
+
+  std::vector<uint8_t> encoded_data_;
+};
+
+// Interface for receiving encoded audio frames from MediaTransportInterface
+// implementations.
+class MediaTransportAudioSinkInterface {
+ public:
+  virtual ~MediaTransportAudioSinkInterface() = default;
+
+  // Called when new encoded audio frame is received.
+  virtual void OnData(uint64_t channel_id,
+                      MediaTransportEncodedAudioFrame frame) = 0;
+};
+
+// Represents encoded video frame, along with the codec information.
+class MediaTransportEncodedVideoFrame final {
+ public:
+  MediaTransportEncodedVideoFrame(int64_t frame_id,
+                                  std::vector<int64_t> referenced_frame_ids,
+                                  VideoCodecType codec_type,
+                                  const webrtc::EncodedImage& encoded_image);
+  ~MediaTransportEncodedVideoFrame();
+  MediaTransportEncodedVideoFrame(const MediaTransportEncodedVideoFrame&);
+  MediaTransportEncodedVideoFrame& operator=(
+      const MediaTransportEncodedVideoFrame& other);
+  MediaTransportEncodedVideoFrame& operator=(
+      MediaTransportEncodedVideoFrame&& other);
+  MediaTransportEncodedVideoFrame(MediaTransportEncodedVideoFrame&&);
+
+  VideoCodecType codec_type() const { return codec_type_; }
+  const webrtc::EncodedImage& encoded_image() const { return encoded_image_; }
+
+  int64_t frame_id() const { return frame_id_; }
+  const std::vector<int64_t>& referenced_frame_ids() const {
+    return referenced_frame_ids_;
+  }
+
+ private:
+  VideoCodecType codec_type_;
+
+  // The buffer is not owned by the encoded image by default. On the sender it
+  // means that it will need to make a copy of it if it wants to deliver it
+  // asynchronously.
+  webrtc::EncodedImage encoded_image_;
+
+  // Frame id uniquely identifies a frame in a stream. It needs to be unique in
+  // a given time window (i.e. technically unique identifier for the lifetime of
+  // the connection is not needed, but you need to guarantee that remote side
+  // got rid of the previous frame_id if you plan to reuse it).
+  //
+  // It is required by a remote jitter buffer, and is the same as
+  // EncodedFrame::id::picture_id.
+  //
+  // This data must be opaque to the media transport, and media transport should
+  // itself not make any assumptions about what it is and its uniqueness.
+  int64_t frame_id_;
+
+  // A single frame might depend on other frames. This is set of identifiers on
+  // which the current frame depends.
+  std::vector<int64_t> referenced_frame_ids_;
+};
+
+// Interface for receiving encoded video frames from MediaTransportInterface
+// implementations.
+class MediaTransportVideoSinkInterface {
+ public:
+  virtual ~MediaTransportVideoSinkInterface() = default;
+
+  // Called when new encoded video frame is received.
+  virtual void OnData(uint64_t channel_id,
+                      MediaTransportEncodedVideoFrame frame) = 0;
+
+  // Called when the request for keyframe is received.
+  virtual void OnKeyFrameRequested(uint64_t channel_id) = 0;
+};
+
+// Media transport interface for sending / receiving encoded audio/video frames
+// and receiving bandwidth estimate update from congestion control.
+class MediaTransportInterface {
+ public:
+  virtual ~MediaTransportInterface() = default;
+
+  // Start asynchronous send of audio frame. The status returned by this method
+  // only pertains to the synchronous operations (e.g.
+  // serialization/packetization), not to the asynchronous operation.
+
+  virtual RTCError SendAudioFrame(uint64_t channel_id,
+                                  MediaTransportEncodedAudioFrame frame) = 0;
+
+  // Start asynchronous send of video frame. The status returned by this method
+  // only pertains to the synchronous operations (e.g.
+  // serialization/packetization), not to the asynchronous operation.
+  virtual RTCError SendVideoFrame(
+      uint64_t channel_id,
+      const MediaTransportEncodedVideoFrame& frame) = 0;
+
+  // Requests a keyframe for the particular channel (stream). The caller should
+  // check that the keyframe is not present in a jitter buffer already (i.e.
+  // don't request a keyframe if there is one that you will get from the jitter
+  // buffer in a moment).
+  virtual RTCError RequestKeyFrame(uint64_t channel_id) = 0;
+
+  // Sets audio sink. Sink must be unset by calling SetReceiveAudioSink(nullptr)
+  // before the media transport is destroyed or before new sink is set.
+  virtual void SetReceiveAudioSink(MediaTransportAudioSinkInterface* sink) = 0;
+
+  // Registers a video sink. Before destruction of media transport, you must
+  // pass a nullptr.
+  virtual void SetReceiveVideoSink(MediaTransportVideoSinkInterface* sink) = 0;
+
+  // TODO(sukhanov): RtcEventLogs.
+  // TODO(sukhanov): Bandwidth updates.
+};
+
+// If media transport factory is set in peer connection factory, it will be
+// used to create media transport for sending/receiving encoded frames and
+// this transport will be used instead of default RTP/SRTP transport.
+//
+// Currently Media Transport negotiation is not supported in SDP.
+// If application is using media transport, it must negotiate it before
+// setting media transport factory in peer connection.
+class MediaTransportFactory {
+ public:
+  virtual ~MediaTransportFactory() = default;
+
+  // Creates media transport.
+  // - Does not take ownership of packet_transport or network_thread.
+  // - Does not support group calls, in 1:1 call one side must set
+  //   is_caller = true and another is_caller = false.
+  virtual RTCErrorOr<std::unique_ptr<MediaTransportInterface>>
+  CreateMediaTransport(rtc::PacketTransportInternal* packet_transport,
+                       rtc::Thread* network_thread,
+                       bool is_caller) = 0;
+};
+
+}  // namespace webrtc
+#endif  // API_MEDIA_TRANSPORT_INTERFACE_H_
diff --git a/api/mediaconstraintsinterface.h b/api/mediaconstraintsinterface.h
index c6a914a..560fa4a 100644
--- a/api/mediaconstraintsinterface.h
+++ b/api/mediaconstraintsinterface.h
@@ -24,7 +24,6 @@
 #include <string>
 #include <vector>
 
-#include "absl/types/optional.h"
 #include "api/peerconnectioninterface.h"
 
 namespace webrtc {
diff --git a/api/mediastreaminterface.h b/api/mediastreaminterface.h
index b661351..2eb2f31 100644
--- a/api/mediastreaminterface.h
+++ b/api/mediastreaminterface.h
@@ -24,17 +24,11 @@
 
 #include "absl/types/optional.h"
 #include "api/video/video_frame.h"
-// TODO(zhihuang): Remove unrelated headers once downstream applications stop
-// relying on them; they were previously transitively included by
-// mediachannel.h, which is no longer a dependency of this file.
 #include "api/video/video_sink_interface.h"
 #include "api/video/video_source_interface.h"
 #include "modules/audio_processing/include/audio_processing_statistics.h"
-#include "rtc_base/ratetracker.h"
 #include "rtc_base/refcount.h"
 #include "rtc_base/scoped_ref_ptr.h"
-#include "rtc_base/thread.h"
-#include "rtc_base/timeutils.h"
 
 namespace webrtc {
 
diff --git a/api/peerconnectioninterface.cc b/api/peerconnectioninterface.cc
index 05aa53f..b4148d7 100644
--- a/api/peerconnectioninterface.cc
+++ b/api/peerconnectioninterface.cc
@@ -195,16 +195,6 @@
 rtc::scoped_refptr<PeerConnectionInterface>
 PeerConnectionFactoryInterface::CreatePeerConnection(
     const PeerConnectionInterface::RTCConfiguration& configuration,
-    const MediaConstraintsInterface* constraints,
-    std::unique_ptr<cricket::PortAllocator> allocator,
-    std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
-    PeerConnectionObserver* observer) {
-  return nullptr;
-}
-
-rtc::scoped_refptr<PeerConnectionInterface>
-PeerConnectionFactoryInterface::CreatePeerConnection(
-    const PeerConnectionInterface::RTCConfiguration& configuration,
     std::unique_ptr<cricket::PortAllocator> allocator,
     std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
     PeerConnectionObserver* observer) {
diff --git a/api/peerconnectioninterface.h b/api/peerconnectioninterface.h
index 2b94ee8..3d8a9c1 100644
--- a/api/peerconnectioninterface.h
+++ b/api/peerconnectioninterface.h
@@ -69,7 +69,6 @@
 
 #include <memory>
 #include <string>
-#include <utility>
 #include <vector>
 
 #include "api/asyncresolverfactory.h"
@@ -79,9 +78,9 @@
 #include "api/audio_options.h"
 #include "api/call/callfactoryinterface.h"
 #include "api/datachannelinterface.h"
-#include "api/dtmfsenderinterface.h"
 #include "api/fec_controller.h"
 #include "api/jsep.h"
+#include "api/media_transport_interface.h"
 #include "api/mediastreaminterface.h"
 #include "api/rtcerror.h"
 #include "api/rtceventlogoutput.h"
@@ -94,7 +93,6 @@
 #include "api/transport/bitrate_settings.h"
 #include "api/transport/network_control.h"
 #include "api/turncustomizer.h"
-#include "api/umametrics.h"
 #include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
 #include "media/base/mediaconfig.h"
 // TODO(bugs.webrtc.org/6353): cricket::VideoCapturer is deprecated and should
@@ -563,6 +561,12 @@
     // correctly. This flag will be deprecated soon. Do not rely on it.
     bool active_reset_srtp_params = false;
 
+    // If MediaTransportFactory is provided in PeerConnectionFactory, this flag
+    // informs PeerConnection that it should use the MediaTransportInterface.
+    // It's invalid to set it to |true| if the MediaTransportFactory wasn't
+    // provided.
+    bool use_media_transport = false;
+
     //
     // Don't forget to update operator== if adding something.
     //
@@ -1156,6 +1160,7 @@
   std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory;
   std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory;
   std::unique_ptr<NetworkControllerFactoryInterface> network_controller_factory;
+  std::unique_ptr<MediaTransportFactory> media_transport_factory;
 };
 
 // PeerConnectionFactoryInterface is the factory interface used for creating
@@ -1233,15 +1238,6 @@
       std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
       PeerConnectionObserver* observer);
 
-  // Deprecated; should use RTCConfiguration for everything that previously
-  // used constraints.
-  virtual rtc::scoped_refptr<PeerConnectionInterface> CreatePeerConnection(
-      const PeerConnectionInterface::RTCConfiguration& configuration,
-      const MediaConstraintsInterface* constraints,
-      std::unique_ptr<cricket::PortAllocator> allocator,
-      std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
-      PeerConnectionObserver* observer);
-
   // Returns the capabilities of an RTP sender of type |kind|.
   // If for some reason you pass in MEDIA_TYPE_DATA, returns an empty structure.
   // TODO(orphis): Make pure virtual when all subclasses implement it.
diff --git a/api/rtcerror.cc b/api/rtcerror.cc
index 55ac15e..039e7f3 100644
--- a/api/rtcerror.cc
+++ b/api/rtcerror.cc
@@ -36,32 +36,8 @@
 
 namespace webrtc {
 
-RTCError::RTCError(RTCError&& other)
-    : type_(other.type_), have_string_message_(other.have_string_message_) {
-  if (have_string_message_) {
-    new (&string_message_) std::string(std::move(other.string_message_));
-  } else {
-    static_message_ = other.static_message_;
-  }
-}
-
-RTCError& RTCError::operator=(RTCError&& other) {
-  type_ = other.type_;
-  if (other.have_string_message_) {
-    set_message(std::move(other.string_message_));
-  } else {
-    set_message(other.static_message_);
-  }
-  return *this;
-}
-
-RTCError::~RTCError() {
-  // If we hold a message string that was built, rather than a static string,
-  // we need to delete it.
-  if (have_string_message_) {
-    string_message_.~basic_string();
-  }
-}
+RTCError::RTCError(RTCError&& other) = default;
+RTCError& RTCError::operator=(RTCError&& other) = default;
 
 // static
 RTCError RTCError::OK() {
@@ -69,28 +45,11 @@
 }
 
 const char* RTCError::message() const {
-  if (have_string_message_) {
-    return string_message_.c_str();
-  } else {
-    return static_message_;
-  }
+  return message_.c_str();
 }
 
-void RTCError::set_message(const char* message) {
-  if (have_string_message_) {
-    string_message_.~basic_string();
-    have_string_message_ = false;
-  }
-  static_message_ = message;
-}
-
-void RTCError::set_message(std::string&& message) {
-  if (!have_string_message_) {
-    new (&string_message_) std::string(std::move(message));
-    have_string_message_ = true;
-  } else {
-    string_message_ = message;
-  }
+void RTCError::set_message(std::string message) {
+  message_ = std::move(message);
 }
 
 // TODO(jonasolsson): Change to use absl::string_view when it's available.
diff --git a/api/rtcerror.h b/api/rtcerror.h
index c87ce91..4910682 100644
--- a/api/rtcerror.h
+++ b/api/rtcerror.h
@@ -86,12 +86,9 @@
   // Creates a "no error" error.
   RTCError() {}
   explicit RTCError(RTCErrorType type) : type_(type) {}
-  // For performance, prefer using the constructor that takes a const char* if
-  // the message is a static string.
-  RTCError(RTCErrorType type, const char* message)
-      : type_(type), static_message_(message), have_string_message_(false) {}
-  RTCError(RTCErrorType type, std::string&& message)
-      : type_(type), string_message_(message), have_string_message_(true) {}
+
+  RTCError(RTCErrorType type, std::string message)
+      : type_(type), message_(std::move(message)) {}
 
   // Delete the copy constructor and assignment operator; there aren't any use
   // cases where you should need to copy an RTCError, as opposed to moving it.
@@ -103,8 +100,6 @@
   RTCError(RTCError&& other);
   RTCError& operator=(RTCError&& other);
 
-  ~RTCError();
-
   // Identical to default constructed error.
   //
   // Preferred over the default constructor for code readability.
@@ -118,10 +113,8 @@
   // anything but logging/diagnostics, since messages are not guaranteed to be
   // stable.
   const char* message() const;
-  // For performance, prefer using the method that takes a const char* if the
-  // message is a static string.
-  void set_message(const char* message);
-  void set_message(std::string&& message);
+
+  void set_message(std::string message);
 
   // Convenience method for situations where you only care whether or not an
   // error occurred.
@@ -129,16 +122,7 @@
 
  private:
   RTCErrorType type_ = RTCErrorType::NONE;
-  // For performance, we use static strings wherever possible. But in some
-  // cases the error string may need to be constructed, in which case an
-  // std::string is used.
-  union {
-    const char* static_message_ = "";
-    std::string string_message_;
-  };
-  // Whether or not |static_message_| or |string_message_| is being used in the
-  // above union.
-  bool have_string_message_ = false;
+  std::string message_;
 };
 
 // Outputs the error as a friendly string. Update this method when adding a new
diff --git a/api/rtcerror_unittest.cc b/api/rtcerror_unittest.cc
index 90593cf..a1ea83f 100644
--- a/api/rtcerror_unittest.cc
+++ b/api/rtcerror_unittest.cc
@@ -222,7 +222,8 @@
 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
 
 TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
-  EXPECT_DEATH(RTCErrorOr<int> err = RTCError::OK(), "");
+  RTCErrorOr<int> err;
+  EXPECT_DEATH(err = RTCError::OK(), "");
 }
 
 TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
diff --git a/api/rtp_headers.cc b/api/rtp_headers.cc
index a0b1a15..bf973b6 100644
--- a/api/rtp_headers.cc
+++ b/api/rtp_headers.cc
@@ -10,14 +10,6 @@
 
 #include "api/rtp_headers.h"
 
-#include <string.h>
-#include <algorithm>
-#include <limits>
-#include <type_traits>
-
-#include "rtc_base/checks.h"
-#include "rtc_base/stringutils.h"
-
 namespace webrtc {
 
 RTPHeaderExtension::RTPHeaderExtension()
@@ -34,7 +26,9 @@
       videoRotation(kVideoRotation_0),
       hasVideoContentType(false),
       videoContentType(VideoContentType::UNSPECIFIED),
-      has_video_timing(false) {}
+      has_video_timing(false),
+      has_frame_marking(false),
+      frame_marking({false, false, false, false, false, 0xFF, 0, 0}) {}
 
 RTPHeaderExtension::RTPHeaderExtension(const RTPHeaderExtension& other) =
     default;
diff --git a/api/rtp_headers.h b/api/rtp_headers.h
index c762534..799058d 100644
--- a/api/rtp_headers.h
+++ b/api/rtp_headers.h
@@ -13,18 +13,14 @@
 
 #include <stddef.h>
 #include <string.h>
-#include <string>
-#include <vector>
 
-#include "absl/types/optional.h"
 #include "api/array_view.h"
 #include "api/video/video_content_type.h"
+#include "api/video/video_frame_marking.h"
 #include "api/video/video_rotation.h"
 #include "api/video/video_timing.h"
 
 #include "common_types.h"  // NOLINT(build/include)
-#include "rtc_base/checks.h"
-#include "rtc_base/deprecation.h"
 
 namespace webrtc {
 
@@ -116,6 +112,9 @@
   bool has_video_timing;
   VideoSendTiming video_timing;
 
+  bool has_frame_marking;
+  FrameMarking frame_marking;
+
   PlayoutDelay playout_delay = {-1, -1};
 
   // For identification of a stream when ssrc is not signaled. See
diff --git a/api/rtpparameters.cc b/api/rtpparameters.cc
index 62ca3fc..e9f4d5d 100644
--- a/api/rtpparameters.cc
+++ b/api/rtpparameters.cc
@@ -12,7 +12,6 @@
 #include <algorithm>
 #include <string>
 
-#include "rtc_base/checks.h"
 #include "rtc_base/strings/string_builder.h"
 
 namespace webrtc {
@@ -129,11 +128,22 @@
 const char RtpExtension::kMidUri[] = "urn:ietf:params:rtp-hdrext:sdes:mid";
 const int RtpExtension::kMidDefaultId = 9;
 
+const char RtpExtension::kFrameMarkingUri[] =
+    "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07";
+const int RtpExtension::kFrameMarkingDefaultId = 10;
+
+const char RtpExtension::kGenericFrameDescriptorUri[] =
+    "http://www.webrtc.org/experiments/rtp-hdrext/generic-frame-descriptor-00";
+const int RtpExtension::kGenericFrameDescriptorDefaultId = 11;
+
 const char RtpExtension::kEncryptHeaderExtensionsUri[] =
     "urn:ietf:params:rtp-hdrext:encrypt";
 
-const int RtpExtension::kMinId = 1;
-const int RtpExtension::kMaxId = 14;
+constexpr int RtpExtension::kMinId;
+constexpr int RtpExtension::kMaxId;
+constexpr int RtpExtension::kMaxValueSize;
+constexpr int RtpExtension::kOneByteHeaderExtensionMaxId;
+constexpr int RtpExtension::kOneByteHeaderExtensionMaxValueSize;
 
 bool RtpExtension::IsSupportedForAudio(const std::string& uri) {
   return uri == webrtc::RtpExtension::kAudioLevelUri ||
@@ -149,7 +159,9 @@
          uri == webrtc::RtpExtension::kPlayoutDelayUri ||
          uri == webrtc::RtpExtension::kVideoContentTypeUri ||
          uri == webrtc::RtpExtension::kVideoTimingUri ||
-         uri == webrtc::RtpExtension::kMidUri;
+         uri == webrtc::RtpExtension::kMidUri ||
+         uri == webrtc::RtpExtension::kFrameMarkingUri ||
+         uri == webrtc::RtpExtension::kGenericFrameDescriptorUri;
 }
 
 bool RtpExtension::IsEncryptionSupported(const std::string& uri) {
diff --git a/api/rtpparameters.h b/api/rtpparameters.h
index 9a29c08..678ac02 100644
--- a/api/rtpparameters.h
+++ b/api/rtpparameters.h
@@ -201,7 +201,7 @@
 // redundant; if you call "RtpReceiver::GetCapabilities(MEDIA_TYPE_AUDIO)",
 // you know you're getting audio capabilities.
 struct RtpHeaderExtensionCapability {
-  // URI of this extension, as defined in RFC5285.
+  // URI of this extension, as defined in RFC8285.
   std::string uri;
 
   // Preferred value of ID that goes in the packet.
@@ -226,7 +226,7 @@
   }
 };
 
-// RTP header extension, see RFC 5285.
+// RTP header extension, see RFC8285.
 struct RtpExtension {
   RtpExtension();
   RtpExtension(const std::string& uri, int id);
@@ -282,6 +282,14 @@
   static const char kVideoTimingUri[];
   static const int kVideoTimingDefaultId;
 
+  // Header extension for video frame marking.
+  static const char kFrameMarkingUri[];
+  static const int kFrameMarkingDefaultId;
+
+  // Experimental codec agnostic frame descriptor.
+  static const char kGenericFrameDescriptorUri[];
+  static const int kGenericFrameDescriptorDefaultId;
+
   // Header extension for transport sequence number, see url for details:
   // http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions
   static const char kTransportSequenceNumberUri[];
@@ -299,9 +307,13 @@
   // https://tools.ietf.org/html/rfc6904
   static const char kEncryptHeaderExtensionsUri[];
 
-  // Inclusive min and max IDs for one-byte header extensions, per RFC5285.
-  static const int kMinId;
-  static const int kMaxId;
+  // Inclusive min and max IDs for two-byte header extensions and one-byte
+  // header extensions, per RFC8285 Section 4.2-4.3.
+  static constexpr int kMinId = 1;
+  static constexpr int kMaxId = 255;
+  static constexpr int kMaxValueSize = 255;
+  static constexpr int kOneByteHeaderExtensionMaxId = 14;
+  static constexpr int kOneByteHeaderExtensionMaxValueSize = 16;
 
   std::string uri;
   int id = 0;
@@ -417,9 +429,19 @@
   // TODO(asapersson): Not implemented for ORTC API.
   absl::optional<int> min_bitrate_bps;
 
-  // TODO(deadbeef): Not implemented.
+  // Specifies the maximum framerate in fps for video.
+  // TODO(asapersson): Different framerates are not supported per simulcast
+  // layer. If set, the maximum |max_framerate| is currently used.
+  // Not supported for screencast.
   absl::optional<int> max_framerate;
 
+  // Specifies the number of temporal layers for video (if the feature is
+  // supported by the codec implementation).
+  // TODO(asapersson): Different number of temporal layers are not supported
+  // per simulcast layer.
+  // Not supported for screencast.
+  absl::optional<int> num_temporal_layers;
+
   // For video, scale the resolution down by this factor.
   // TODO(deadbeef): Not implemented.
   absl::optional<double> scale_resolution_down_by;
@@ -451,7 +473,9 @@
            fec == o.fec && rtx == o.rtx && dtx == o.dtx &&
            bitrate_priority == o.bitrate_priority && ptime == o.ptime &&
            max_bitrate_bps == o.max_bitrate_bps &&
+           min_bitrate_bps == o.min_bitrate_bps &&
            max_framerate == o.max_framerate &&
+           num_temporal_layers == o.num_temporal_layers &&
            scale_resolution_down_by == o.scale_resolution_down_by &&
            scale_framerate_down_by == o.scale_framerate_down_by &&
            active == o.active && rid == o.rid &&
diff --git a/api/rtpsenderinterface.cc b/api/rtpsenderinterface.cc
index bbf3901..11c0a69 100644
--- a/api/rtpsenderinterface.cc
+++ b/api/rtpsenderinterface.cc
@@ -20,4 +20,9 @@
   return nullptr;
 }
 
+std::vector<RtpEncodingParameters> RtpSenderInterface::init_send_encodings()
+    const {
+  return {};
+}
+
 }  // namespace webrtc
diff --git a/api/rtpsenderinterface.h b/api/rtpsenderinterface.h
index 96c2669..7c94c21 100644
--- a/api/rtpsenderinterface.h
+++ b/api/rtpsenderinterface.h
@@ -24,7 +24,6 @@
 #include "api/proxy.h"
 #include "api/rtcerror.h"
 #include "api/rtpparameters.h"
-#include "rtc_base/deprecation.h"
 #include "rtc_base/refcount.h"
 #include "rtc_base/scoped_ref_ptr.h"
 
@@ -55,6 +54,12 @@
   // tracks.
   virtual std::vector<std::string> stream_ids() const = 0;
 
+  // Returns the list of encoding parameters that will be applied when the SDP
+  // local description is set. These initial encoding parameters can be set by
+  // PeerConnection::AddTransceiver, and later updated with Get/SetParameters.
+  // TODO(orphis): Make it pure virtual once Chrome has updated
+  virtual std::vector<RtpEncodingParameters> init_send_encodings() const;
+
   virtual RtpParameters GetParameters() = 0;
   // Note that only a subset of the parameters can currently be changed. See
   // rtpparameters.h
@@ -90,6 +95,7 @@
 PROXY_CONSTMETHOD0(cricket::MediaType, media_type)
 PROXY_CONSTMETHOD0(std::string, id)
 PROXY_CONSTMETHOD0(std::vector<std::string>, stream_ids)
+PROXY_CONSTMETHOD0(std::vector<RtpEncodingParameters>, init_send_encodings)
 PROXY_METHOD0(RtpParameters, GetParameters);
 PROXY_METHOD1(RTCError, SetParameters, const RtpParameters&)
 PROXY_CONSTMETHOD0(rtc::scoped_refptr<DtmfSenderInterface>, GetDtmfSender);
diff --git a/api/rtptransceiverinterface.cc b/api/rtptransceiverinterface.cc
index 065ac04..d833339 100644
--- a/api/rtptransceiverinterface.cc
+++ b/api/rtptransceiverinterface.cc
@@ -23,4 +23,9 @@
   return absl::nullopt;
 }
 
+void RtpTransceiverInterface::SetCodecPreferences(
+    rtc::ArrayView<RtpCodecCapability>) {
+  RTC_NOTREACHED() << "Not implemented";
+}
+
 }  // namespace webrtc
diff --git a/api/rtptransceiverinterface.h b/api/rtptransceiverinterface.h
index 4c22957..301a380 100644
--- a/api/rtptransceiverinterface.h
+++ b/api/rtptransceiverinterface.h
@@ -126,8 +126,7 @@
   // by WebRTC for this transceiver.
   // https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-setcodecpreferences
   // TODO(steveanton): Not implemented.
-  virtual void SetCodecPreferences(
-      rtc::ArrayView<RtpCodecCapability> codecs) = 0;
+  virtual void SetCodecPreferences(rtc::ArrayView<RtpCodecCapability> codecs);
 
  protected:
   ~RtpTransceiverInterface() override = default;
diff --git a/api/transport/enums.h b/api/transport/enums.h
new file mode 100644
index 0000000..b1d5770
--- /dev/null
+++ b/api/transport/enums.h
@@ -0,0 +1,32 @@
+/*
+ *  Copyright 2018 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 API_TRANSPORT_ENUMS_H_
+#define API_TRANSPORT_ENUMS_H_
+
+namespace webrtc {
+
+// See https://w3c.github.io/webrtc-pc/#rtcicetransportstate
+// Note that kFailed is currently not a terminal state, and a transport might
+// incorrectly be marked as failed while gathering candidates, see
+// bugs.webrtc.org/8833
+enum class IceTransportState {
+  kNew,
+  kChecking,
+  kConnected,
+  kCompleted,
+  kFailed,
+  kDisconnected,
+  kClosed,
+};
+
+}  // namespace webrtc
+
+#endif  // API_TRANSPORT_ENUMS_H_
diff --git a/api/transport/goog_cc_factory.cc b/api/transport/goog_cc_factory.cc
new file mode 100644
index 0000000..119e2dc
--- /dev/null
+++ b/api/transport/goog_cc_factory.cc
@@ -0,0 +1,43 @@
+/*
+ *  Copyright (c) 2018 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 "api/transport/goog_cc_factory.h"
+
+#include "absl/memory/memory.h"
+#include "modules/congestion_controller/goog_cc/goog_cc_network_control.h"
+namespace webrtc {
+GoogCcNetworkControllerFactory::GoogCcNetworkControllerFactory(
+    RtcEventLog* event_log)
+    : event_log_(event_log) {}
+
+std::unique_ptr<NetworkControllerInterface>
+GoogCcNetworkControllerFactory::Create(NetworkControllerConfig config) {
+  return absl::make_unique<GoogCcNetworkController>(event_log_, config, false);
+}
+
+TimeDelta GoogCcNetworkControllerFactory::GetProcessInterval() const {
+  const int64_t kUpdateIntervalMs = 25;
+  return TimeDelta::ms(kUpdateIntervalMs);
+}
+
+GoogCcFeedbackNetworkControllerFactory::GoogCcFeedbackNetworkControllerFactory(
+    RtcEventLog* event_log)
+    : event_log_(event_log) {}
+
+std::unique_ptr<NetworkControllerInterface>
+GoogCcFeedbackNetworkControllerFactory::Create(NetworkControllerConfig config) {
+  return absl::make_unique<GoogCcNetworkController>(event_log_, config, true);
+}
+
+TimeDelta GoogCcFeedbackNetworkControllerFactory::GetProcessInterval() const {
+  const int64_t kUpdateIntervalMs = 25;
+  return TimeDelta::ms(kUpdateIntervalMs);
+}
+}  // namespace webrtc
diff --git a/api/transport/goog_cc_factory.h b/api/transport/goog_cc_factory.h
new file mode 100644
index 0000000..2e3b317
--- /dev/null
+++ b/api/transport/goog_cc_factory.h
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2018 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 API_TRANSPORT_GOOG_CC_FACTORY_H_
+#define API_TRANSPORT_GOOG_CC_FACTORY_H_
+#include <memory>
+
+#include "api/transport/network_control.h"
+
+namespace webrtc {
+class RtcEventLog;
+
+class GoogCcNetworkControllerFactory
+    : public NetworkControllerFactoryInterface {
+ public:
+  explicit GoogCcNetworkControllerFactory(RtcEventLog*);
+  std::unique_ptr<NetworkControllerInterface> Create(
+      NetworkControllerConfig config) override;
+  TimeDelta GetProcessInterval() const override;
+
+ private:
+  RtcEventLog* const event_log_;
+};
+
+// Factory to create packet feedback only GoogCC, this can be used for
+// connections providing packet receive time feedback but no other reports.
+class GoogCcFeedbackNetworkControllerFactory
+    : public NetworkControllerFactoryInterface {
+ public:
+  explicit GoogCcFeedbackNetworkControllerFactory(RtcEventLog*);
+  std::unique_ptr<NetworkControllerInterface> Create(
+      NetworkControllerConfig config) override;
+  TimeDelta GetProcessInterval() const override;
+
+ private:
+  RtcEventLog* const event_log_;
+};
+}  // namespace webrtc
+
+#endif  // API_TRANSPORT_GOOG_CC_FACTORY_H_
diff --git a/api/transport/network_control.h b/api/transport/network_control.h
index abd945d..9086260 100644
--- a/api/transport/network_control.h
+++ b/api/transport/network_control.h
@@ -29,15 +29,13 @@
 // optional to use for a network controller implementation.
 struct NetworkControllerConfig {
   // The initial constraints to start with, these can be changed at any later
-  // time by calls to OnTargetRateConstraints.
+  // time by calls to OnTargetRateConstraints. Note that the starting rate
+  // has to be set initially to provide a starting state for the network
+  // controller, even though the field is marked as optional.
   TargetRateConstraints constraints;
   // Initial stream specific configuration, these are changed at any later time
   // by calls to OnStreamsConfig.
   StreamsConfig stream_based_config;
-  // The initial bandwidth estimate to base target rate on. This should be used
-  // as the basis for initial OnTargetTransferRate and OnPacerConfig callbacks.
-  // Note that starting rate is only provided on construction.
-  DataRate starting_bandwidth = DataRate::Infinity();
 };
 
 // NetworkControllerInterface is implemented by network controllers. A network
diff --git a/api/transport/network_types.cc b/api/transport/network_types.cc
index b4c09d4..48bdcab 100644
--- a/api/transport/network_types.cc
+++ b/api/transport/network_types.cc
@@ -48,7 +48,7 @@
 std::vector<PacketResult> TransportPacketsFeedback::LostWithSendInfo() const {
   std::vector<PacketResult> res;
   for (const PacketResult& fb : packet_feedbacks) {
-    if (fb.receive_time.IsInfinite() && fb.sent_packet.has_value()) {
+    if (fb.receive_time.IsPlusInfinity() && fb.sent_packet.has_value()) {
       res.push_back(fb);
     }
   }
diff --git a/api/transport/network_types.h b/api/transport/network_types.h
index 8e9526b..3a00efe 100644
--- a/api/transport/network_types.h
+++ b/api/transport/network_types.h
@@ -30,28 +30,32 @@
   StreamsConfig();
   StreamsConfig(const StreamsConfig&);
   ~StreamsConfig();
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   bool requests_alr_probing = false;
   absl::optional<double> pacing_factor;
   absl::optional<DataRate> min_pacing_rate;
   absl::optional<DataRate> max_padding_rate;
   absl::optional<DataRate> max_total_allocated_bitrate;
+  // The send rate of traffic for which feedback is not received.
+  DataRate unacknowledged_rate_allocation = DataRate::Zero();
 };
 
 struct TargetRateConstraints {
   TargetRateConstraints();
   TargetRateConstraints(const TargetRateConstraints&);
   ~TargetRateConstraints();
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   absl::optional<DataRate> min_data_rate;
   absl::optional<DataRate> max_data_rate;
+  // The initial bandwidth estimate to base target rate on. This should be used
+  // as the basis for initial OnTargetTransferRate and OnPacerConfig callbacks.
   absl::optional<DataRate> starting_rate;
 };
 
 // Send side information
 
 struct NetworkAvailability {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   bool network_available = false;
 };
 
@@ -59,7 +63,7 @@
   NetworkRouteChange();
   NetworkRouteChange(const NetworkRouteChange&);
   ~NetworkRouteChange();
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   // The TargetRateConstraints are set here so they can be changed synchronously
   // when network route changes.
   TargetRateConstraints constraints;
@@ -82,34 +86,35 @@
 };
 
 struct SentPacket {
-  Timestamp send_time = Timestamp::Infinity();
+  Timestamp send_time = Timestamp::PlusInfinity();
   DataSize size = DataSize::Zero();
+  DataSize prior_unacked_data = DataSize::Zero();
   PacedPacketInfo pacing_info;
   // Transport independent sequence number, any tracked packet should have a
   // sequence number that is unique over the whole call and increasing by 1 for
   // each packet.
   int64_t sequence_number;
-  // Data in flight when the packet was sent, including the packet.
+  // Tracked data in flight when the packet was sent, excluding unacked data.
   DataSize data_in_flight = DataSize::Zero();
 };
 
 // Transport level feedback
 
 struct RemoteBitrateReport {
-  Timestamp receive_time = Timestamp::Infinity();
+  Timestamp receive_time = Timestamp::PlusInfinity();
   DataRate bandwidth = DataRate::Infinity();
 };
 
 struct RoundTripTimeUpdate {
-  Timestamp receive_time = Timestamp::Infinity();
+  Timestamp receive_time = Timestamp::PlusInfinity();
   TimeDelta round_trip_time = TimeDelta::PlusInfinity();
   bool smoothed = false;
 };
 
 struct TransportLossReport {
-  Timestamp receive_time = Timestamp::Infinity();
-  Timestamp start_time = Timestamp::Infinity();
-  Timestamp end_time = Timestamp::Infinity();
+  Timestamp receive_time = Timestamp::PlusInfinity();
+  Timestamp start_time = Timestamp::PlusInfinity();
+  Timestamp end_time = Timestamp::PlusInfinity();
   uint64_t packets_lost_delta = 0;
   uint64_t packets_received_delta = 0;
 };
@@ -122,7 +127,7 @@
   ~PacketResult();
 
   absl::optional<SentPacket> sent_packet;
-  Timestamp receive_time = Timestamp::Infinity();
+  Timestamp receive_time = Timestamp::PlusInfinity();
 };
 
 struct TransportPacketsFeedback {
@@ -130,7 +135,7 @@
   TransportPacketsFeedback(const TransportPacketsFeedback& other);
   ~TransportPacketsFeedback();
 
-  Timestamp feedback_time = Timestamp::Infinity();
+  Timestamp feedback_time = Timestamp::PlusInfinity();
   DataSize data_in_flight = DataSize::Zero();
   DataSize prior_in_flight = DataSize::Zero();
   std::vector<PacketResult> packet_feedbacks;
@@ -143,7 +148,7 @@
 // Network estimation
 
 struct NetworkEstimate {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   DataRate bandwidth = DataRate::Infinity();
   TimeDelta round_trip_time = TimeDelta::PlusInfinity();
   TimeDelta bwe_period = TimeDelta::PlusInfinity();
@@ -154,7 +159,7 @@
 // Network control
 
 struct PacerConfig {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   // Pacer should send at most data_window data over time_window duration.
   DataSize data_window = DataSize::Infinity();
   TimeDelta time_window = TimeDelta::PlusInfinity();
@@ -165,14 +170,14 @@
 };
 
 struct ProbeClusterConfig {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   DataRate target_data_rate = DataRate::Zero();
   TimeDelta target_duration = TimeDelta::Zero();
   int32_t target_probe_count = 0;
 };
 
 struct TargetTransferRate {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
   // The estimate on which the target rate is based on.
   NetworkEstimate network_estimate;
   DataRate target_rate = DataRate::Zero();
@@ -193,7 +198,7 @@
 
 // Process control
 struct ProcessInterval {
-  Timestamp at_time = Timestamp::Infinity();
+  Timestamp at_time = Timestamp::PlusInfinity();
 };
 }  // namespace webrtc
 
diff --git a/api/units/data_rate.h b/api/units/data_rate.h
index 5c421d4..4bb988b 100644
--- a/api/units/data_rate.h
+++ b/api/units/data_rate.h
@@ -16,6 +16,7 @@
 #endif              // UNIT_TEST
 
 #include <stdint.h>
+#include <algorithm>
 #include <cmath>
 #include <limits>
 #include <string>
@@ -133,7 +134,26 @@
     return bits_per_sec_ == data_rate_impl::kPlusInfinityVal;
   }
   constexpr bool IsFinite() const { return !IsInfinite(); }
-
+  DataRate Clamped(DataRate min_rate, DataRate max_rate) const {
+    return std::max(min_rate, std::min(*this, max_rate));
+  }
+  void Clamp(DataRate min_rate, DataRate max_rate) {
+    *this = Clamped(min_rate, max_rate);
+  }
+  DataRate operator-(const DataRate& other) const {
+    return DataRate::bps(bps() - other.bps());
+  }
+  DataRate operator+(const DataRate& other) const {
+    return DataRate::bps(bps() + other.bps());
+  }
+  DataRate& operator-=(const DataRate& other) {
+    *this = *this - other;
+    return *this;
+  }
+  DataRate& operator+=(const DataRate& other) {
+    *this = *this + other;
+    return *this;
+  }
   constexpr double operator/(const DataRate& other) const {
     return bps<double>() / other.bps<double>();
   }
diff --git a/api/units/data_rate_unittest.cc b/api/units/data_rate_unittest.cc
index 9c91fd6..8e5b660 100644
--- a/api/units/data_rate_unittest.cc
+++ b/api/units/data_rate_unittest.cc
@@ -92,6 +92,26 @@
   EXPECT_TRUE(DataRate::bps(kInfinity).IsInfinite());
   EXPECT_TRUE(DataRate::kbps(kInfinity).IsInfinite());
 }
+TEST(DataRateTest, Clamping) {
+  const DataRate upper = DataRate::kbps(800);
+  const DataRate lower = DataRate::kbps(100);
+  const DataRate under = DataRate::kbps(100);
+  const DataRate inside = DataRate::kbps(500);
+  const DataRate over = DataRate::kbps(1000);
+  EXPECT_EQ(under.Clamped(lower, upper), lower);
+  EXPECT_EQ(inside.Clamped(lower, upper), inside);
+  EXPECT_EQ(over.Clamped(lower, upper), upper);
+
+  DataRate mutable_rate = lower;
+  mutable_rate.Clamp(lower, upper);
+  EXPECT_EQ(mutable_rate, lower);
+  mutable_rate = inside;
+  mutable_rate.Clamp(lower, upper);
+  EXPECT_EQ(mutable_rate, inside);
+  mutable_rate = over;
+  mutable_rate.Clamp(lower, upper);
+  EXPECT_EQ(mutable_rate, upper);
+}
 
 TEST(DataRateTest, MathOperations) {
   const int64_t kValueA = 450;
@@ -100,11 +120,21 @@
   const DataRate rate_b = DataRate::bps(kValueB);
   const int32_t kInt32Value = 123;
   const double kFloatValue = 123.0;
+
+  EXPECT_EQ((rate_a + rate_b).bps(), kValueA + kValueB);
+  EXPECT_EQ((rate_a - rate_b).bps(), kValueA - kValueB);
+
   EXPECT_EQ((rate_a * kValueB).bps(), kValueA * kValueB);
   EXPECT_EQ((rate_a * kInt32Value).bps(), kValueA * kInt32Value);
   EXPECT_EQ((rate_a * kFloatValue).bps(), kValueA * kFloatValue);
 
   EXPECT_EQ(rate_a / rate_b, static_cast<double>(kValueA) / kValueB);
+
+  DataRate mutable_rate = DataRate::bps(kValueA);
+  mutable_rate += rate_b;
+  EXPECT_EQ(mutable_rate.bps(), kValueA + kValueB);
+  mutable_rate -= rate_a;
+  EXPECT_EQ(mutable_rate.bps(), kValueB);
 }
 
 TEST(UnitConversionTest, DataRateAndDataSizeAndTimeDelta) {
diff --git a/api/units/data_size.h b/api/units/data_size.h
index ac61a6f..8958b24 100644
--- a/api/units/data_size.h
+++ b/api/units/data_size.h
@@ -95,11 +95,11 @@
     return DataSize::bytes(bytes() + other.bytes());
   }
   DataSize& operator-=(const DataSize& other) {
-    bytes_ -= other.bytes();
+    *this = *this - other;
     return *this;
   }
   DataSize& operator+=(const DataSize& other) {
-    bytes_ += other.bytes();
+    *this = *this + other;
     return *this;
   }
   constexpr double operator/(const DataSize& other) const {
diff --git a/api/units/time_delta.h b/api/units/time_delta.h
index b820472..ec36417 100644
--- a/api/units/time_delta.h
+++ b/api/units/time_delta.h
@@ -17,6 +17,7 @@
 
 #include <stdint.h>
 #include <cmath>
+#include <cstdlib>
 #include <limits>
 #include <string>
 
@@ -188,17 +189,35 @@
     return microseconds_ == timedelta_impl::kMinusInfinityVal;
   }
   TimeDelta operator+(const TimeDelta& other) const {
+    if (IsPlusInfinity() || other.IsPlusInfinity()) {
+      RTC_DCHECK(!IsMinusInfinity());
+      RTC_DCHECK(!other.IsMinusInfinity());
+      return PlusInfinity();
+    } else if (IsMinusInfinity() || other.IsMinusInfinity()) {
+      RTC_DCHECK(!IsPlusInfinity());
+      RTC_DCHECK(!other.IsPlusInfinity());
+      return MinusInfinity();
+    }
     return TimeDelta::us(us() + other.us());
   }
   TimeDelta operator-(const TimeDelta& other) const {
+    if (IsPlusInfinity() || other.IsMinusInfinity()) {
+      RTC_DCHECK(!IsMinusInfinity());
+      RTC_DCHECK(!other.IsPlusInfinity());
+      return PlusInfinity();
+    } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
+      RTC_DCHECK(!IsPlusInfinity());
+      RTC_DCHECK(!other.IsMinusInfinity());
+      return MinusInfinity();
+    }
     return TimeDelta::us(us() - other.us());
   }
   TimeDelta& operator-=(const TimeDelta& other) {
-    microseconds_ -= other.us();
+    *this = *this - other;
     return *this;
   }
   TimeDelta& operator+=(const TimeDelta& other) {
-    microseconds_ += other.us();
+    *this = *this + other;
     return *this;
   }
   constexpr double operator/(const TimeDelta& other) const {
diff --git a/api/units/time_delta_unittest.cc b/api/units/time_delta_unittest.cc
index 9eddee7..bf8bbce 100644
--- a/api/units/time_delta_unittest.cc
+++ b/api/units/time_delta_unittest.cc
@@ -74,6 +74,12 @@
   EXPECT_TRUE(TimeDelta::ms(-kValue).IsFinite());
   EXPECT_TRUE(TimeDelta::ms(kValue).IsFinite());
   EXPECT_TRUE(TimeDelta::Zero().IsFinite());
+
+  EXPECT_TRUE(TimeDelta::PlusInfinity().IsPlusInfinity());
+  EXPECT_FALSE(TimeDelta::MinusInfinity().IsPlusInfinity());
+
+  EXPECT_TRUE(TimeDelta::MinusInfinity().IsMinusInfinity());
+  EXPECT_FALSE(TimeDelta::PlusInfinity().IsMinusInfinity());
 }
 
 TEST(TimeDeltaTest, ComparisonOperators) {
@@ -164,6 +170,26 @@
 
   EXPECT_EQ(TimeDelta::us(-kValueA).Abs().us(), kValueA);
   EXPECT_EQ(TimeDelta::us(kValueA).Abs().us(), kValueA);
+
+  TimeDelta mutable_delta = TimeDelta::ms(kValueA);
+  mutable_delta += TimeDelta::ms(kValueB);
+  EXPECT_EQ(mutable_delta, TimeDelta::ms(kValueA + kValueB));
+  mutable_delta -= TimeDelta::ms(kValueB);
+  EXPECT_EQ(mutable_delta, TimeDelta::ms(kValueA));
+}
+
+TEST(TimeDeltaTest, InfinityOperations) {
+  const int64_t kValue = 267;
+  const TimeDelta finite = TimeDelta::ms(kValue);
+  EXPECT_TRUE((TimeDelta::PlusInfinity() + finite).IsPlusInfinity());
+  EXPECT_TRUE((TimeDelta::PlusInfinity() - finite).IsPlusInfinity());
+  EXPECT_TRUE((finite + TimeDelta::PlusInfinity()).IsPlusInfinity());
+  EXPECT_TRUE((finite - TimeDelta::MinusInfinity()).IsPlusInfinity());
+
+  EXPECT_TRUE((TimeDelta::MinusInfinity() + finite).IsMinusInfinity());
+  EXPECT_TRUE((TimeDelta::MinusInfinity() - finite).IsMinusInfinity());
+  EXPECT_TRUE((finite + TimeDelta::MinusInfinity()).IsMinusInfinity());
+  EXPECT_TRUE((finite - TimeDelta::PlusInfinity()).IsMinusInfinity());
 }
 }  // namespace test
 }  // namespace webrtc
diff --git a/api/units/timestamp.h b/api/units/timestamp.h
index 6e5e392..0298f5d 100644
--- a/api/units/timestamp.h
+++ b/api/units/timestamp.h
@@ -36,9 +36,12 @@
 class Timestamp {
  public:
   Timestamp() = delete;
-  static constexpr Timestamp Infinity() {
+  static constexpr Timestamp PlusInfinity() {
     return Timestamp(timestamp_impl::kPlusInfinityVal);
   }
+  static constexpr Timestamp MinusInfinity() {
+    return Timestamp(timestamp_impl::kMinusInfinityVal);
+  }
   template <int64_t seconds>
   static constexpr Timestamp Seconds() {
     static_assert(seconds >= 0, "");
@@ -103,7 +106,9 @@
                 nullptr>
   static Timestamp us(T microseconds) {
     if (microseconds == std::numeric_limits<double>::infinity()) {
-      return Infinity();
+      return PlusInfinity();
+    } else if (microseconds == -std::numeric_limits<double>::infinity()) {
+      return MinusInfinity();
     } else {
       RTC_DCHECK(!std::isnan(microseconds));
       RTC_DCHECK_GE(microseconds, 0);
@@ -141,7 +146,10 @@
   template <typename T>
   constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
   us() const {
-    return IsInfinite() ? std::numeric_limits<T>::infinity() : microseconds_;
+    return IsPlusInfinity()
+               ? std::numeric_limits<T>::infinity()
+               : IsMinusInfinity() ? -std::numeric_limits<T>::infinity()
+                                   : microseconds_;
   }
 
   constexpr int64_t seconds_or(int64_t fallback_value) const {
@@ -154,25 +162,59 @@
     return IsFinite() ? microseconds_ : fallback_value;
   }
 
-  constexpr bool IsInfinite() const {
-    return microseconds_ == timestamp_impl::kPlusInfinityVal;
-  }
   constexpr bool IsFinite() const { return !IsInfinite(); }
+  constexpr bool IsInfinite() const {
+    return microseconds_ == timedelta_impl::kPlusInfinityVal ||
+           microseconds_ == timedelta_impl::kMinusInfinityVal;
+  }
+  constexpr bool IsPlusInfinity() const {
+    return microseconds_ == timedelta_impl::kPlusInfinityVal;
+  }
+  constexpr bool IsMinusInfinity() const {
+    return microseconds_ == timedelta_impl::kMinusInfinityVal;
+  }
+  Timestamp operator+(const TimeDelta& other) const {
+    if (IsPlusInfinity() || other.IsPlusInfinity()) {
+      RTC_DCHECK(!IsMinusInfinity());
+      RTC_DCHECK(!other.IsMinusInfinity());
+      return PlusInfinity();
+    } else if (IsMinusInfinity() || other.IsMinusInfinity()) {
+      RTC_DCHECK(!IsPlusInfinity());
+      RTC_DCHECK(!other.IsPlusInfinity());
+      return MinusInfinity();
+    }
+    return Timestamp::us(us() + other.us());
+  }
+  Timestamp operator-(const TimeDelta& other) const {
+    if (IsPlusInfinity() || other.IsMinusInfinity()) {
+      RTC_DCHECK(!IsMinusInfinity());
+      RTC_DCHECK(!other.IsPlusInfinity());
+      return PlusInfinity();
+    } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
+      RTC_DCHECK(!IsPlusInfinity());
+      RTC_DCHECK(!other.IsMinusInfinity());
+      return MinusInfinity();
+    }
+    return Timestamp::us(us() - other.us());
+  }
   TimeDelta operator-(const Timestamp& other) const {
+    if (IsPlusInfinity() || other.IsMinusInfinity()) {
+      RTC_DCHECK(!IsMinusInfinity());
+      RTC_DCHECK(!other.IsPlusInfinity());
+      return TimeDelta::PlusInfinity();
+    } else if (IsMinusInfinity() || other.IsPlusInfinity()) {
+      RTC_DCHECK(!IsPlusInfinity());
+      RTC_DCHECK(!other.IsMinusInfinity());
+      return TimeDelta::MinusInfinity();
+    }
     return TimeDelta::us(us() - other.us());
   }
-  Timestamp operator-(const TimeDelta& delta) const {
-    return Timestamp::us(us() - delta.us());
-  }
-  Timestamp operator+(const TimeDelta& delta) const {
-    return Timestamp::us(us() + delta.us());
-  }
   Timestamp& operator-=(const TimeDelta& other) {
-    microseconds_ -= other.us();
+    *this = *this - other;
     return *this;
   }
   Timestamp& operator+=(const TimeDelta& other) {
-    microseconds_ += other.us();
+    *this = *this + other;
     return *this;
   }
   constexpr bool operator==(const Timestamp& other) const {
diff --git a/api/units/timestamp_unittest.cc b/api/units/timestamp_unittest.cc
index db894cc..6c2d1ee 100644
--- a/api/units/timestamp_unittest.cc
+++ b/api/units/timestamp_unittest.cc
@@ -15,7 +15,7 @@
 namespace test {
 TEST(TimestampTest, ConstExpr) {
   constexpr int64_t kValue = 12345;
-  constexpr Timestamp kTimestampInf = Timestamp::Infinity();
+  constexpr Timestamp kTimestampInf = Timestamp::PlusInfinity();
   static_assert(kTimestampInf.IsInfinite(), "");
   static_assert(kTimestampInf.ms_or(-1) == -1, "");
 
@@ -55,20 +55,28 @@
 TEST(TimestampTest, IdentityChecks) {
   const int64_t kValue = 3000;
 
-  EXPECT_TRUE(Timestamp::Infinity().IsInfinite());
+  EXPECT_TRUE(Timestamp::PlusInfinity().IsInfinite());
+  EXPECT_TRUE(Timestamp::MinusInfinity().IsInfinite());
   EXPECT_FALSE(Timestamp::ms(kValue).IsInfinite());
 
-  EXPECT_FALSE(Timestamp::Infinity().IsFinite());
+  EXPECT_FALSE(Timestamp::PlusInfinity().IsFinite());
+  EXPECT_FALSE(Timestamp::MinusInfinity().IsFinite());
   EXPECT_TRUE(Timestamp::ms(kValue).IsFinite());
+
+  EXPECT_TRUE(Timestamp::PlusInfinity().IsPlusInfinity());
+  EXPECT_FALSE(Timestamp::MinusInfinity().IsPlusInfinity());
+
+  EXPECT_TRUE(Timestamp::MinusInfinity().IsMinusInfinity());
+  EXPECT_FALSE(Timestamp::PlusInfinity().IsMinusInfinity());
 }
 
 TEST(TimestampTest, ComparisonOperators) {
   const int64_t kSmall = 450;
   const int64_t kLarge = 451;
 
-  EXPECT_EQ(Timestamp::Infinity(), Timestamp::Infinity());
-  EXPECT_GE(Timestamp::Infinity(), Timestamp::Infinity());
-  EXPECT_GT(Timestamp::Infinity(), Timestamp::ms(kLarge));
+  EXPECT_EQ(Timestamp::PlusInfinity(), Timestamp::PlusInfinity());
+  EXPECT_GE(Timestamp::PlusInfinity(), Timestamp::PlusInfinity());
+  EXPECT_GT(Timestamp::PlusInfinity(), Timestamp::ms(kLarge));
   EXPECT_EQ(Timestamp::ms(kSmall), Timestamp::ms(kSmall));
   EXPECT_LE(Timestamp::ms(kSmall), Timestamp::ms(kSmall));
   EXPECT_GE(Timestamp::ms(kSmall), Timestamp::ms(kSmall));
@@ -102,14 +110,21 @@
   EXPECT_EQ(Timestamp::us(kMicrosDouble).us(), kMicros);
 
   const double kPlusInfinity = std::numeric_limits<double>::infinity();
+  const double kMinusInfinity = -kPlusInfinity;
 
-  EXPECT_EQ(Timestamp::Infinity().seconds<double>(), kPlusInfinity);
-  EXPECT_EQ(Timestamp::Infinity().ms<double>(), kPlusInfinity);
-  EXPECT_EQ(Timestamp::Infinity().us<double>(), kPlusInfinity);
+  EXPECT_EQ(Timestamp::PlusInfinity().seconds<double>(), kPlusInfinity);
+  EXPECT_EQ(Timestamp::MinusInfinity().seconds<double>(), kMinusInfinity);
+  EXPECT_EQ(Timestamp::PlusInfinity().ms<double>(), kPlusInfinity);
+  EXPECT_EQ(Timestamp::MinusInfinity().ms<double>(), kMinusInfinity);
+  EXPECT_EQ(Timestamp::PlusInfinity().us<double>(), kPlusInfinity);
+  EXPECT_EQ(Timestamp::MinusInfinity().us<double>(), kMinusInfinity);
 
-  EXPECT_TRUE(Timestamp::seconds(kPlusInfinity).IsInfinite());
-  EXPECT_TRUE(Timestamp::ms(kPlusInfinity).IsInfinite());
-  EXPECT_TRUE(Timestamp::us(kPlusInfinity).IsInfinite());
+  EXPECT_TRUE(Timestamp::seconds(kPlusInfinity).IsPlusInfinity());
+  EXPECT_TRUE(Timestamp::seconds(kMinusInfinity).IsMinusInfinity());
+  EXPECT_TRUE(Timestamp::ms(kPlusInfinity).IsPlusInfinity());
+  EXPECT_TRUE(Timestamp::ms(kMinusInfinity).IsMinusInfinity());
+  EXPECT_TRUE(Timestamp::us(kPlusInfinity).IsPlusInfinity());
+  EXPECT_TRUE(Timestamp::us(kMinusInfinity).IsMinusInfinity());
 }
 
 TEST(UnitConversionTest, TimestampAndTimeDeltaMath) {
@@ -118,10 +133,27 @@
   const Timestamp time_a = Timestamp::ms(kValueA);
   const Timestamp time_b = Timestamp::ms(kValueB);
   const TimeDelta delta_a = TimeDelta::ms(kValueA);
+  const TimeDelta delta_b = TimeDelta::ms(kValueB);
 
   EXPECT_EQ((time_a - time_b), TimeDelta::ms(kValueA - kValueB));
   EXPECT_EQ((time_b - delta_a), Timestamp::ms(kValueB - kValueA));
   EXPECT_EQ((time_b + delta_a), Timestamp::ms(kValueB + kValueA));
+
+  Timestamp mutable_time = time_a;
+  mutable_time += delta_b;
+  EXPECT_EQ(mutable_time, time_a + delta_b);
+  mutable_time -= delta_b;
+  EXPECT_EQ(mutable_time, time_a);
+}
+
+TEST(UnitConversionTest, InfinityOperations) {
+  const int64_t kValue = 267;
+  const Timestamp finite_time = Timestamp::ms(kValue);
+  const TimeDelta finite_delta = TimeDelta::ms(kValue);
+  EXPECT_TRUE((Timestamp::PlusInfinity() + finite_delta).IsInfinite());
+  EXPECT_TRUE((Timestamp::PlusInfinity() - finite_delta).IsInfinite());
+  EXPECT_TRUE((finite_time + TimeDelta::PlusInfinity()).IsInfinite());
+  EXPECT_TRUE((finite_time - TimeDelta::MinusInfinity()).IsInfinite());
 }
 }  // namespace test
 }  // namespace webrtc
diff --git a/api/video/encoded_image.cc b/api/video/encoded_image.cc
new file mode 100644
index 0000000..5658dfc
--- /dev/null
+++ b/api/video/encoded_image.cc
@@ -0,0 +1,47 @@
+/*
+ *  Copyright (c) 2012 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 "api/video/encoded_image.h"
+
+#include <string.h>
+
+#include <algorithm>  // swap
+
+#include "rtc_base/bind.h"
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+// FFmpeg's decoder, used by H264DecoderImpl, requires up to 8 bytes padding due
+// to optimized bitstream readers. See avcodec_decode_video2.
+const size_t EncodedImage::kBufferPaddingBytesH264 = 8;
+
+size_t EncodedImage::GetBufferPaddingBytes(VideoCodecType codec_type) {
+  switch (codec_type) {
+    case kVideoCodecH264:
+      return kBufferPaddingBytesH264;
+    default:
+      return 0;
+  }
+}
+
+EncodedImage::EncodedImage() : EncodedImage(nullptr, 0, 0) {}
+
+EncodedImage::EncodedImage(const EncodedImage&) = default;
+
+EncodedImage::EncodedImage(uint8_t* buffer, size_t length, size_t size)
+    : _buffer(buffer), _length(length), _size(size) {}
+
+void EncodedImage::SetEncodeTime(int64_t encode_start_ms,
+                                 int64_t encode_finish_ms) {
+  timing_.encode_start_ms = encode_start_ms;
+  timing_.encode_finish_ms = encode_finish_ms;
+}
+}  // namespace webrtc
diff --git a/api/video/encoded_image.h b/api/video/encoded_image.h
new file mode 100644
index 0000000..ed58d96
--- /dev/null
+++ b/api/video/encoded_image.h
@@ -0,0 +1,97 @@
+/*
+ *  Copyright (c) 2014 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 API_VIDEO_ENCODED_IMAGE_H_
+#define API_VIDEO_ENCODED_IMAGE_H_
+
+#include "absl/types/optional.h"
+#include "api/video/video_content_type.h"
+#include "api/video/video_rotation.h"
+#include "api/video/video_timing.h"
+#include "common_types.h"  // NOLINT(build/include)
+
+namespace webrtc {
+
+// TODO(bug.webrtc.org/9378): This is a legacy api class, which is slowly being
+// cleaned up. Direct use of its members is strongly discouraged.
+class EncodedImage {
+ public:
+  static const size_t kBufferPaddingBytesH264;
+
+  // Some decoders require encoded image buffers to be padded with a small
+  // number of additional bytes (due to over-reading byte readers).
+  static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
+
+  EncodedImage();
+  EncodedImage(const EncodedImage&);
+  EncodedImage(uint8_t* buffer, size_t length, size_t size);
+
+  // TODO(nisse): Change style to timestamp(), set_timestamp(), for consistency
+  // with the VideoFrame class.
+  // Set frame timestamp (90kHz).
+  void SetTimestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
+
+  // Get frame timestamp (90kHz).
+  uint32_t Timestamp() const { return timestamp_rtp_; }
+
+  void SetEncodeTime(int64_t encode_start_ms, int64_t encode_finish_ms);
+
+  absl::optional<int> SpatialIndex() const {
+    if (spatial_index_ < 0)
+      return absl::nullopt;
+    return spatial_index_;
+  }
+  void SetSpatialIndex(absl::optional<int> spatial_index) {
+    RTC_DCHECK_GE(spatial_index.value_or(0), 0);
+    RTC_DCHECK_LT(spatial_index.value_or(0), kMaxSpatialLayers);
+    spatial_index_ = spatial_index.value_or(-1);
+  }
+
+  uint32_t _encodedWidth = 0;
+  uint32_t _encodedHeight = 0;
+  // NTP time of the capture time in local timebase in milliseconds.
+  int64_t ntp_time_ms_ = 0;
+  int64_t capture_time_ms_ = 0;
+  FrameType _frameType = kVideoFrameDelta;
+  uint8_t* _buffer;
+  size_t _length;
+  size_t _size;
+  VideoRotation rotation_ = kVideoRotation_0;
+  VideoContentType content_type_ = VideoContentType::UNSPECIFIED;
+  bool _completeFrame = false;
+  int qp_ = -1;  // Quantizer value.
+
+  // When an application indicates non-zero values here, it is taken as an
+  // indication that all future frames will be constrained with those limits
+  // until the application indicates a change again.
+  PlayoutDelay playout_delay_ = {-1, -1};
+
+  struct Timing {
+    uint8_t flags = VideoSendTiming::kInvalid;
+    int64_t encode_start_ms = 0;
+    int64_t encode_finish_ms = 0;
+    int64_t packetization_finish_ms = 0;
+    int64_t pacer_exit_ms = 0;
+    int64_t network_timestamp_ms = 0;
+    int64_t network2_timestamp_ms = 0;
+    int64_t receive_start_ms = 0;
+    int64_t receive_finish_ms = 0;
+  } timing_;
+
+ private:
+  uint32_t timestamp_rtp_ = 0;
+  // -1 means not set. Use a plain int rather than optional, to keep this class
+  // copyable with memcpy.
+  int spatial_index_ = -1;
+};
+
+}  // namespace webrtc
+
+#endif  // API_VIDEO_ENCODED_IMAGE_H_
diff --git a/api/video/video_bitrate_allocation.cc b/api/video/video_bitrate_allocation.cc
index 6c5ad1e..8922536 100644
--- a/api/video/video_bitrate_allocation.cc
+++ b/api/video/video_bitrate_allocation.cc
@@ -10,12 +10,9 @@
 
 #include "api/video/video_bitrate_allocation.h"
 
-#include <limits>
-
 #include "rtc_base/checks.h"
 #include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/strings/string_builder.h"
-#include "rtc_base/stringutils.h"
 
 namespace webrtc {
 
diff --git a/api/video/video_content_type.h b/api/video/video_content_type.h
index 8c64602..2d38a62 100644
--- a/api/video/video_content_type.h
+++ b/api/video/video_content_type.h
@@ -13,8 +13,6 @@
 
 #include <stdint.h>
 
-#include <string>
-
 namespace webrtc {
 
 enum class VideoContentType : uint8_t {
diff --git a/api/video/video_frame_marking.h b/api/video/video_frame_marking.h
new file mode 100644
index 0000000..2a34852
--- /dev/null
+++ b/api/video/video_frame_marking.h
@@ -0,0 +1,29 @@
+/*
+ *  Copyright (c) 2018 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 API_VIDEO_VIDEO_FRAME_MARKING_H_
+#define API_VIDEO_VIDEO_FRAME_MARKING_H_
+
+namespace webrtc {
+
+struct FrameMarking {
+  bool start_of_frame;
+  bool end_of_frame;
+  bool independent_frame;
+  bool discardable_frame;
+  bool base_layer_sync;
+  uint8_t temporal_id;
+  uint8_t layer_id;
+  uint8_t tl0_pic_idx;
+};
+
+}  // namespace webrtc
+
+#endif  // API_VIDEO_VIDEO_FRAME_MARKING_H_
diff --git a/audio/BUILD.gn b/audio/BUILD.gn
index ae4c67e..91cece9 100644
--- a/audio/BUILD.gn
+++ b/audio/BUILD.gn
@@ -24,10 +24,14 @@
     "audio_state.h",
     "audio_transport_impl.cc",
     "audio_transport_impl.h",
-    "channel.cc",
-    "channel.h",
-    "channel_proxy.cc",
-    "channel_proxy.h",
+    "channel_receive.cc",
+    "channel_receive.h",
+    "channel_receive_proxy.cc",
+    "channel_receive_proxy.h",
+    "channel_send.cc",
+    "channel_send.h",
+    "channel_send_proxy.cc",
+    "channel_send_proxy.h",
     "conversion.h",
     "null_audio_poller.cc",
     "null_audio_poller.h",
@@ -82,8 +86,8 @@
     "../rtc_base:safe_minmax",
     "../rtc_base:stringutils",
     "../system_wrappers",
-    "../system_wrappers:field_trial_api",
-    "../system_wrappers:metrics_api",
+    "../system_wrappers:field_trial",
+    "../system_wrappers:metrics",
     "utility:audio_frame_operations",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/types:optional",
diff --git a/audio/audio_receive_stream.cc b/audio/audio_receive_stream.cc
index b507afc..563e8a0 100644
--- a/audio/audio_receive_stream.cc
+++ b/audio/audio_receive_stream.cc
@@ -16,7 +16,7 @@
 #include "api/call/audio_sink.h"
 #include "audio/audio_send_stream.h"
 #include "audio/audio_state.h"
-#include "audio/channel_proxy.h"
+#include "audio/channel_receive_proxy.h"
 #include "audio/conversion.h"
 #include "call/rtp_stream_receiver_controller_interface.h"
 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
@@ -62,7 +62,7 @@
 
 namespace internal {
 namespace {
-std::unique_ptr<voe::ChannelProxy> CreateChannelAndProxy(
+std::unique_ptr<voe::ChannelReceiveProxy> CreateChannelAndProxy(
     webrtc::AudioState* audio_state,
     ProcessThread* module_process_thread,
     const webrtc::AudioReceiveStream::Config& config,
@@ -70,11 +70,13 @@
   RTC_DCHECK(audio_state);
   internal::AudioState* internal_audio_state =
       static_cast<internal::AudioState*>(audio_state);
-  return absl::make_unique<voe::ChannelProxy>(absl::make_unique<voe::Channel>(
-      module_process_thread, internal_audio_state->audio_device_module(),
-      nullptr /* RtcpRttStats */, event_log, config.rtp.remote_ssrc,
-      config.jitter_buffer_max_packets, config.jitter_buffer_fast_accelerate,
-      config.decoder_factory, config.codec_pair_id));
+  return absl::make_unique<voe::ChannelReceiveProxy>(
+      absl::make_unique<voe::ChannelReceive>(
+          module_process_thread, internal_audio_state->audio_device_module(),
+          config.rtcp_send_transport, event_log, config.rtp.remote_ssrc,
+          config.jitter_buffer_max_packets,
+          config.jitter_buffer_fast_accelerate, config.decoder_factory,
+          config.codec_pair_id, config.frame_decryptor));
 }
 }  // namespace
 
@@ -101,19 +103,18 @@
     const webrtc::AudioReceiveStream::Config& config,
     const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
     webrtc::RtcEventLog* event_log,
-    std::unique_ptr<voe::ChannelProxy> channel_proxy)
+    std::unique_ptr<voe::ChannelReceiveProxy> channel_proxy)
     : audio_state_(audio_state), channel_proxy_(std::move(channel_proxy)) {
   RTC_LOG(LS_INFO) << "AudioReceiveStream: " << config.rtp.remote_ssrc;
   RTC_DCHECK(receiver_controller);
   RTC_DCHECK(packet_router);
   RTC_DCHECK(config.decoder_factory);
+  RTC_DCHECK(config.rtcp_send_transport);
   RTC_DCHECK(audio_state_);
   RTC_DCHECK(channel_proxy_);
 
   module_process_thread_checker_.DetachFromThread();
 
-  channel_proxy_->RegisterTransport(config.rtcp_send_transport);
-
   // Configure bandwidth estimation.
   channel_proxy_->RegisterReceiverCongestionControlObjects(packet_router);
 
@@ -129,7 +130,6 @@
   RTC_LOG(LS_INFO) << "~AudioReceiveStream: " << config_.rtp.remote_ssrc;
   Stop();
   channel_proxy_->DisassociateSendChannel();
-  channel_proxy_->RegisterTransport(nullptr);
   channel_proxy_->ResetReceiverCongestionControlObjects();
 }
 
@@ -164,7 +164,8 @@
   webrtc::AudioReceiveStream::Stats stats;
   stats.remote_ssrc = config_.rtp.remote_ssrc;
 
-  webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
+  webrtc::CallReceiveStatistics call_stats =
+      channel_proxy_->GetRTCPStatistics();
   // TODO(solenberg): Don't return here if we can't get the codec - return the
   //                  stats we *can* get.
   webrtc::CodecInst codec_inst = {0};
diff --git a/audio/audio_receive_stream.h b/audio/audio_receive_stream.h
index 64552e3..e982b04 100644
--- a/audio/audio_receive_stream.h
+++ b/audio/audio_receive_stream.h
@@ -32,7 +32,7 @@
 class RtpStreamReceiverInterface;
 
 namespace voe {
-class ChannelProxy;
+class ChannelReceiveProxy;
 }  // namespace voe
 
 namespace internal {
@@ -54,7 +54,7 @@
                      const webrtc::AudioReceiveStream::Config& config,
                      const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
                      webrtc::RtcEventLog* event_log,
-                     std::unique_ptr<voe::ChannelProxy> channel_proxy);
+                     std::unique_ptr<voe::ChannelReceiveProxy> channel_proxy);
   ~AudioReceiveStream() override;
 
   // webrtc::AudioReceiveStream implementation.
@@ -101,7 +101,7 @@
   rtc::ThreadChecker module_process_thread_checker_;
   webrtc::AudioReceiveStream::Config config_;
   rtc::scoped_refptr<webrtc::AudioState> audio_state_;
-  std::unique_ptr<voe::ChannelProxy> channel_proxy_;
+  std::unique_ptr<voe::ChannelReceiveProxy> channel_proxy_;
   AudioSendStream* associated_send_stream_ = nullptr;
 
   bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false;
diff --git a/audio/audio_receive_stream_unittest.cc b/audio/audio_receive_stream_unittest.cc
index eb93bd0..97c42c4 100644
--- a/audio/audio_receive_stream_unittest.cc
+++ b/audio/audio_receive_stream_unittest.cc
@@ -25,6 +25,7 @@
 #include "modules/rtp_rtcp/source/byte_io.h"
 #include "test/gtest.h"
 #include "test/mock_audio_decoder_factory.h"
+#include "test/mock_transport.h"
 
 namespace webrtc {
 namespace test {
@@ -59,8 +60,8 @@
 const double kTotalOutputEnergy = 0.25;
 const double kTotalOutputDuration = 0.5;
 
-const CallStatistics kCallStats = {345,  678,  901, 234, -12,
-                                   3456, 7890, 567, 890, 123};
+const CallReceiveStatistics kCallStats = {345, 678, 901, 234,
+                                          -12, 567, 890, 123};
 const CodecInst kCodecInst = {123, "codec_name_recv", 96000, -187, 0, -103};
 const NetworkStatistics kNetworkStats = {
     123, 456, false, 789012, 3456, 123, 456, 0,  {}, 789, 12,
@@ -81,7 +82,7 @@
         new rtc::RefCountedObject<testing::NiceMock<MockAudioDeviceModule>>();
     audio_state_ = AudioState::Create(config);
 
-    channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
+    channel_proxy_ = new testing::StrictMock<MockChannelReceiveProxy>();
     EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kLocalSsrc)).Times(1);
     EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 15)).Times(1);
     EXPECT_CALL(*channel_proxy_,
@@ -89,7 +90,6 @@
         .Times(1);
     EXPECT_CALL(*channel_proxy_, ResetReceiverCongestionControlObjects())
         .Times(1);
-    EXPECT_CALL(*channel_proxy_, RegisterTransport(nullptr)).Times(2);
     EXPECT_CALL(*channel_proxy_, DisassociateSendChannel()).Times(1);
     EXPECT_CALL(*channel_proxy_, SetReceiveCodecs(_))
         .WillRepeatedly(Invoke([](const std::map<int, SdpAudioFormat>& codecs) {
@@ -103,6 +103,7 @@
         RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
     stream_config_.rtp.extensions.push_back(RtpExtension(
         RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
+    stream_config_.rtcp_send_transport = &rtcp_send_transport_;
     stream_config_.decoder_factory =
         new rtc::RefCountedObject<MockAudioDecoderFactory>;
   }
@@ -112,12 +113,12 @@
         new internal::AudioReceiveStream(
             &rtp_stream_receiver_controller_, &packet_router_, stream_config_,
             audio_state_, &event_log_,
-            std::unique_ptr<voe::ChannelProxy>(channel_proxy_)));
+            std::unique_ptr<voe::ChannelReceiveProxy>(channel_proxy_)));
   }
 
   AudioReceiveStream::Config& config() { return stream_config_; }
   rtc::scoped_refptr<MockAudioMixer> audio_mixer() { return audio_mixer_; }
-  MockVoEChannelProxy* channel_proxy() { return channel_proxy_; }
+  MockChannelReceiveProxy* channel_proxy() { return channel_proxy_; }
 
   void SetupMockForGetStats() {
     using testing::DoAll;
@@ -148,8 +149,9 @@
   rtc::scoped_refptr<AudioState> audio_state_;
   rtc::scoped_refptr<MockAudioMixer> audio_mixer_;
   AudioReceiveStream::Config stream_config_;
-  testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
+  testing::StrictMock<MockChannelReceiveProxy>* channel_proxy_ = nullptr;
   RtpStreamReceiverController rtp_stream_receiver_controller_;
+  MockTransport rtcp_send_transport_;
 };
 
 void BuildOneByteExtension(std::vector<uint8_t>::iterator it,
@@ -364,7 +366,7 @@
                    kTransportSequenceNumberId + 1));
   new_config.decoder_map.emplace(1, SdpAudioFormat("foo", 8000, 1));
 
-  MockVoEChannelProxy& channel_proxy = *helper.channel_proxy();
+  MockChannelReceiveProxy& channel_proxy = *helper.channel_proxy();
   EXPECT_CALL(channel_proxy, SetLocalSSRC(kLocalSsrc + 1)).Times(1);
   EXPECT_CALL(channel_proxy, SetNACKStatus(true, 15 + 1)).Times(1);
   EXPECT_CALL(channel_proxy, SetReceiveCodecs(new_config.decoder_map));
diff --git a/audio/audio_send_stream.cc b/audio/audio_send_stream.cc
index 0f725c4..49d933c 100644
--- a/audio/audio_send_stream.cc
+++ b/audio/audio_send_stream.cc
@@ -17,7 +17,7 @@
 #include "absl/memory/memory.h"
 
 #include "audio/audio_state.h"
-#include "audio/channel_proxy.h"
+#include "audio/channel_send_proxy.h"
 #include "audio/conversion.h"
 #include "call/rtp_transport_controller_send_interface.h"
 #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
@@ -38,7 +38,7 @@
 constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
 
-void CallEncoder(const std::unique_ptr<voe::ChannelProxy>& channel_proxy,
+void CallEncoder(const std::unique_ptr<voe::ChannelSendProxy>& channel_proxy,
                  rtc::FunctionView<void(AudioEncoder*)> lambda) {
   channel_proxy->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
     RTC_DCHECK(encoder_ptr);
@@ -46,18 +46,16 @@
   });
 }
 
-std::unique_ptr<voe::ChannelProxy> CreateChannelAndProxy(
-    webrtc::AudioState* audio_state,
+std::unique_ptr<voe::ChannelSendProxy> CreateChannelAndProxy(
     rtc::TaskQueue* worker_queue,
     ProcessThread* module_process_thread,
     RtcpRttStats* rtcp_rtt_stats,
-    RtcEventLog* event_log) {
-  RTC_DCHECK(audio_state);
-  internal::AudioState* internal_audio_state =
-      static_cast<internal::AudioState*>(audio_state);
-  return absl::make_unique<voe::ChannelProxy>(absl::make_unique<voe::Channel>(
-      worker_queue, module_process_thread,
-      internal_audio_state->audio_device_module(), rtcp_rtt_stats, event_log));
+    RtcEventLog* event_log,
+    FrameEncryptorInterface* frame_encryptor) {
+  return absl::make_unique<voe::ChannelSendProxy>(
+      absl::make_unique<voe::ChannelSend>(worker_queue, module_process_thread,
+                                          rtcp_rtt_stats, event_log,
+                                          frame_encryptor));
 }
 }  // namespace
 
@@ -104,11 +102,11 @@
                       rtcp_rtt_stats,
                       suspended_rtp_state,
                       overall_call_lifetime,
-                      CreateChannelAndProxy(audio_state.get(),
-                                            worker_queue,
+                      CreateChannelAndProxy(worker_queue,
                                             module_process_thread,
                                             rtcp_rtt_stats,
-                                            event_log)) {}
+                                            event_log,
+                                            config.frame_encryptor)) {}
 
 AudioSendStream::AudioSendStream(
     const webrtc::AudioSendStream::Config& config,
@@ -120,7 +118,7 @@
     RtcpRttStats* rtcp_rtt_stats,
     const absl::optional<RtpState>& suspended_rtp_state,
     TimeInterval* overall_call_lifetime,
-    std::unique_ptr<voe::ChannelProxy> channel_proxy)
+    std::unique_ptr<voe::ChannelSendProxy> channel_proxy)
     : worker_queue_(worker_queue),
       config_(Config(nullptr)),
       audio_state_(audio_state),
@@ -232,6 +230,11 @@
         stream->timed_send_transport_adapter_.get());
   }
 
+  // Enable the frame encryptor if a new frame encryptor has been provided.
+  if (first_time || new_config.frame_encryptor != old_config.frame_encryptor) {
+    channel_proxy->SetFrameEncryptor(new_config.frame_encryptor);
+  }
+
   const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
   const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
   // Audio level indication
@@ -298,9 +301,12 @@
        webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))) {
     // Audio BWE is enabled.
     transport_->packet_sender()->SetAccountForAudioPackets(true);
+    rtp_rtcp_module_->SetAsPartOfAllocation(true);
     ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps,
                              config_.bitrate_priority,
                              has_transport_sequence_number);
+  } else {
+    rtp_rtcp_module_->SetAsPartOfAllocation(false);
   }
   channel_proxy_->StartSend();
   sending_ = true;
@@ -350,7 +356,7 @@
   webrtc::AudioSendStream::Stats stats;
   stats.local_ssrc = config_.rtp.ssrc;
 
-  webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
+  webrtc::CallSendStatistics call_stats = channel_proxy_->GetRTCPStatistics();
   stats.bytes_sent = call_stats.bytesSent;
   stats.packets_sent = call_stats.packetsSent;
   // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
@@ -410,12 +416,6 @@
                                            uint8_t fraction_loss,
                                            int64_t rtt,
                                            int64_t bwe_period_ms) {
-  // Audio transport feedback will not be reported in this mode, instead update
-  // acknowledged bitrate estimator with the bitrate allocated for audio.
-  if (webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")) {
-    transport_->SetAllocatedBitrateWithoutFeedback(bitrate_bps);
-  }
-
   // A send stream may be allocated a bitrate of zero if the allocator decides
   // to disable it. For now we ignore this decision and keep sending on min
   // bitrate.
@@ -479,7 +479,7 @@
   return rtp_rtcp_module_->GetRtpState();
 }
 
-const voe::ChannelProxy& AudioSendStream::GetChannelProxy() const {
+const voe::ChannelSendProxy& AudioSendStream::GetChannelProxy() const {
   RTC_DCHECK(channel_proxy_.get());
   return *channel_proxy_.get();
 }
@@ -716,8 +716,10 @@
     stream->ConfigureBitrateObserver(
         new_config.min_bitrate_bps, new_config.max_bitrate_bps,
         new_config.bitrate_priority, has_transport_sequence_number);
+    stream->rtp_rtcp_module_->SetAsPartOfAllocation(true);
   } else {
     stream->RemoveBitrateObserver();
+    stream->rtp_rtcp_module_->SetAsPartOfAllocation(false);
   }
 }
 
diff --git a/audio/audio_send_stream.h b/audio/audio_send_stream.h
index ba87202..1ea676b 100644
--- a/audio/audio_send_stream.h
+++ b/audio/audio_send_stream.h
@@ -31,7 +31,7 @@
 class RtpTransportControllerSendInterface;
 
 namespace voe {
-class ChannelProxy;
+class ChannelSendProxy;
 }  // namespace voe
 
 namespace internal {
@@ -61,7 +61,7 @@
                   RtcpRttStats* rtcp_rtt_stats,
                   const absl::optional<RtpState>& suspended_rtp_state,
                   TimeInterval* overall_call_lifetime,
-                  std::unique_ptr<voe::ChannelProxy> channel_proxy);
+                  std::unique_ptr<voe::ChannelSendProxy> channel_proxy);
   ~AudioSendStream() override;
 
   // webrtc::AudioSendStream implementation.
@@ -96,7 +96,7 @@
   void SetTransportOverhead(int transport_overhead_per_packet);
 
   RtpState GetRtpState() const;
-  const voe::ChannelProxy& GetChannelProxy() const;
+  const voe::ChannelSendProxy& GetChannelProxy() const;
 
  private:
   class TimedTransport;
@@ -133,7 +133,7 @@
   rtc::TaskQueue* worker_queue_;
   webrtc::AudioSendStream::Config config_;
   rtc::scoped_refptr<webrtc::AudioState> audio_state_;
-  std::unique_ptr<voe::ChannelProxy> channel_proxy_;
+  std::unique_ptr<voe::ChannelSendProxy> channel_proxy_;
   RtcEventLog* const event_log_;
 
   int encoder_sample_rate_hz_ = 0;
diff --git a/audio/audio_send_stream_unittest.cc b/audio/audio_send_stream_unittest.cc
index f5d6796..0a954f8 100644
--- a/audio/audio_send_stream_unittest.cc
+++ b/audio/audio_send_stream_unittest.cc
@@ -56,8 +56,7 @@
 const double kEchoReturnLossEnhancement = 101;
 const double kResidualEchoLikelihood = -1.0f;
 const double kResidualEchoLikelihoodMax = 23.0f;
-const CallStatistics kCallStats = {1345,  1678,  1901, 1234,  112,
-                                   13456, 17890, 1567, -1890, -1123};
+const CallSendStatistics kCallStats = {112, 13456, 17890};
 const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
 const int kTelephoneEventPayloadType = 123;
 const int kTelephoneEventPayloadFrequency = 65432;
@@ -75,10 +74,11 @@
 
 class MockLimitObserver : public BitrateAllocator::LimitObserver {
  public:
-  MOCK_METHOD4(OnAllocationLimitsChanged,
+  MOCK_METHOD5(OnAllocationLimitsChanged,
                void(uint32_t min_send_bitrate_bps,
                     uint32_t max_padding_bitrate_bps,
                     uint32_t total_bitrate_bps,
+                    uint32_t allocated_without_feedback_bps,
                     bool has_packet_feedback));
 };
 
@@ -168,7 +168,7 @@
             stream_config_, audio_state_, &worker_queue_, &rtp_transport_,
             &bitrate_allocator_, &event_log_, &rtcp_rtt_stats_, absl::nullopt,
             &active_lifetime_,
-            std::unique_ptr<voe::ChannelProxy>(channel_proxy_)));
+            std::unique_ptr<voe::ChannelSendProxy>(channel_proxy_)));
   }
 
   AudioSendStream::Config& config() { return stream_config_; }
@@ -176,7 +176,7 @@
     return *static_cast<MockAudioEncoderFactory*>(
         stream_config_.encoder_factory.get());
   }
-  MockVoEChannelProxy* channel_proxy() { return channel_proxy_; }
+  MockChannelSendProxy* channel_proxy() { return channel_proxy_; }
   RtpTransportControllerSendInterface* transport() { return &rtp_transport_; }
   TimeInterval* active_lifetime() { return &active_lifetime_; }
 
@@ -188,7 +188,7 @@
 
   void SetupDefaultChannelProxy(bool audio_bwe_enabled) {
     EXPECT_TRUE(channel_proxy_ == nullptr);
-    channel_proxy_ = new testing::StrictMock<MockVoEChannelProxy>();
+    channel_proxy_ = new testing::StrictMock<MockChannelSendProxy>();
     EXPECT_CALL(*channel_proxy_, GetRtpRtcp()).WillRepeatedly(Invoke([this]() {
       return &this->rtp_rtcp_;
     }));
@@ -196,6 +196,7 @@
     EXPECT_CALL(*channel_proxy_, SetLocalSSRC(kSsrc)).Times(1);
     EXPECT_CALL(*channel_proxy_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
     EXPECT_CALL(*channel_proxy_, SetNACKStatus(true, 10)).Times(1);
+    EXPECT_CALL(*channel_proxy_, SetFrameEncryptor(nullptr)).Times(1);
     EXPECT_CALL(*channel_proxy_,
                 SetSendAudioLevelIndicationStatus(true, kAudioLevelId))
         .Times(1);
@@ -296,7 +297,7 @@
  private:
   rtc::scoped_refptr<AudioState> audio_state_;
   AudioSendStream::Config stream_config_;
-  testing::StrictMock<MockVoEChannelProxy>* channel_proxy_ = nullptr;
+  testing::StrictMock<MockChannelSendProxy>* channel_proxy_ = nullptr;
   rtc::scoped_refptr<MockAudioProcessing> audio_processing_;
   AudioProcessingStats audio_processing_stats_;
   TimeInterval active_lifetime_;
diff --git a/audio/audio_transport_impl.cc b/audio/audio_transport_impl.cc
index 44e95aa..94ef6cb 100644
--- a/audio/audio_transport_impl.cc
+++ b/audio/audio_transport_impl.cc
@@ -50,8 +50,6 @@
                          AudioFrame* audio_frame) {
   RTC_DCHECK(audio_processing);
   RTC_DCHECK(audio_frame);
-  RTC_DCHECK(
-      !audio_processing->echo_cancellation()->is_drift_compensation_enabled());
   audio_processing->set_stream_delay_ms(delay_ms);
   audio_processing->set_stream_key_pressed(key_pressed);
   int error = audio_processing->ProcessStream(audio_frame);
diff --git a/audio/channel.cc b/audio/channel.cc
deleted file mode 100644
index d884e03..0000000
--- a/audio/channel.cc
+++ /dev/null
@@ -1,1456 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "audio/channel.h"
-
-#include <algorithm>
-#include <map>
-#include <memory>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "absl/memory/memory.h"
-#include "api/array_view.h"
-#include "audio/utility/audio_frame_operations.h"
-#include "call/rtp_transport_controller_send_interface.h"
-#include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
-#include "logging/rtc_event_log/rtc_event_log.h"
-#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
-#include "modules/audio_coding/codecs/audio_format_conversion.h"
-#include "modules/audio_device/include/audio_device.h"
-#include "modules/audio_processing/include/audio_processing.h"
-#include "modules/pacing/packet_router.h"
-#include "modules/rtp_rtcp/include/receive_statistics.h"
-#include "modules/rtp_rtcp/source/rtp_packet_received.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-#include "modules/utility/include/process_thread.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/criticalsection.h"
-#include "rtc_base/format_macros.h"
-#include "rtc_base/location.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/rate_limiter.h"
-#include "rtc_base/task_queue.h"
-#include "rtc_base/thread_checker.h"
-#include "rtc_base/timeutils.h"
-#include "system_wrappers/include/field_trial.h"
-#include "system_wrappers/include/metrics.h"
-
-namespace webrtc {
-namespace voe {
-
-namespace {
-
-constexpr double kAudioSampleDurationSeconds = 0.01;
-constexpr int64_t kMaxRetransmissionWindowMs = 1000;
-constexpr int64_t kMinRetransmissionWindowMs = 30;
-
-// Video Sync.
-constexpr int kVoiceEngineMinMinPlayoutDelayMs = 0;
-constexpr int kVoiceEngineMaxMinPlayoutDelayMs = 10000;
-
-}  // namespace
-
-const int kTelephoneEventAttenuationdB = 10;
-
-class TransportFeedbackProxy : public TransportFeedbackObserver {
- public:
-  TransportFeedbackProxy() : feedback_observer_(nullptr) {
-    pacer_thread_.DetachFromThread();
-    network_thread_.DetachFromThread();
-  }
-
-  void SetTransportFeedbackObserver(
-      TransportFeedbackObserver* feedback_observer) {
-    RTC_DCHECK(thread_checker_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    feedback_observer_ = feedback_observer;
-  }
-
-  // Implements TransportFeedbackObserver.
-  void AddPacket(uint32_t ssrc,
-                 uint16_t sequence_number,
-                 size_t length,
-                 const PacedPacketInfo& pacing_info) override {
-    RTC_DCHECK(pacer_thread_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    if (feedback_observer_)
-      feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
-  }
-
-  void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
-    RTC_DCHECK(network_thread_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    if (feedback_observer_)
-      feedback_observer_->OnTransportFeedback(feedback);
-  }
-
- private:
-  rtc::CriticalSection crit_;
-  rtc::ThreadChecker thread_checker_;
-  rtc::ThreadChecker pacer_thread_;
-  rtc::ThreadChecker network_thread_;
-  TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
-};
-
-class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
- public:
-  TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
-    pacer_thread_.DetachFromThread();
-  }
-
-  void SetSequenceNumberAllocator(
-      TransportSequenceNumberAllocator* seq_num_allocator) {
-    RTC_DCHECK(thread_checker_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    seq_num_allocator_ = seq_num_allocator;
-  }
-
-  // Implements TransportSequenceNumberAllocator.
-  uint16_t AllocateSequenceNumber() override {
-    RTC_DCHECK(pacer_thread_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    if (!seq_num_allocator_)
-      return 0;
-    return seq_num_allocator_->AllocateSequenceNumber();
-  }
-
- private:
-  rtc::CriticalSection crit_;
-  rtc::ThreadChecker thread_checker_;
-  rtc::ThreadChecker pacer_thread_;
-  TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
-};
-
-class RtpPacketSenderProxy : public RtpPacketSender {
- public:
-  RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
-
-  void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
-    RTC_DCHECK(thread_checker_.CalledOnValidThread());
-    rtc::CritScope lock(&crit_);
-    rtp_packet_sender_ = rtp_packet_sender;
-  }
-
-  // Implements RtpPacketSender.
-  void InsertPacket(Priority priority,
-                    uint32_t ssrc,
-                    uint16_t sequence_number,
-                    int64_t capture_time_ms,
-                    size_t bytes,
-                    bool retransmission) override {
-    rtc::CritScope lock(&crit_);
-    if (rtp_packet_sender_) {
-      rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
-                                       capture_time_ms, bytes, retransmission);
-    }
-  }
-
-  void SetAccountForAudioPackets(bool account_for_audio) override {
-    RTC_NOTREACHED();
-  }
-
- private:
-  rtc::ThreadChecker thread_checker_;
-  rtc::CriticalSection crit_;
-  RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
-};
-
-class VoERtcpObserver : public RtcpBandwidthObserver {
- public:
-  explicit VoERtcpObserver(Channel* owner)
-      : owner_(owner), bandwidth_observer_(nullptr) {}
-  virtual ~VoERtcpObserver() {}
-
-  void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
-    rtc::CritScope lock(&crit_);
-    bandwidth_observer_ = bandwidth_observer;
-  }
-
-  void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
-    rtc::CritScope lock(&crit_);
-    if (bandwidth_observer_) {
-      bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
-    }
-  }
-
-  void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
-                                    int64_t rtt,
-                                    int64_t now_ms) override {
-    {
-      rtc::CritScope lock(&crit_);
-      if (bandwidth_observer_) {
-        bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
-                                                          now_ms);
-      }
-    }
-    // TODO(mflodman): Do we need to aggregate reports here or can we jut send
-    // what we get? I.e. do we ever get multiple reports bundled into one RTCP
-    // report for VoiceEngine?
-    if (report_blocks.empty())
-      return;
-
-    int fraction_lost_aggregate = 0;
-    int total_number_of_packets = 0;
-
-    // If receiving multiple report blocks, calculate the weighted average based
-    // on the number of packets a report refers to.
-    for (ReportBlockList::const_iterator block_it = report_blocks.begin();
-         block_it != report_blocks.end(); ++block_it) {
-      // Find the previous extended high sequence number for this remote SSRC,
-      // to calculate the number of RTP packets this report refers to. Ignore if
-      // we haven't seen this SSRC before.
-      std::map<uint32_t, uint32_t>::iterator seq_num_it =
-          extended_max_sequence_number_.find(block_it->source_ssrc);
-      int number_of_packets = 0;
-      if (seq_num_it != extended_max_sequence_number_.end()) {
-        number_of_packets =
-            block_it->extended_highest_sequence_number - seq_num_it->second;
-      }
-      fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
-      total_number_of_packets += number_of_packets;
-
-      extended_max_sequence_number_[block_it->source_ssrc] =
-          block_it->extended_highest_sequence_number;
-    }
-    int weighted_fraction_lost = 0;
-    if (total_number_of_packets > 0) {
-      weighted_fraction_lost =
-          (fraction_lost_aggregate + total_number_of_packets / 2) /
-          total_number_of_packets;
-    }
-    owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
-  }
-
- private:
-  Channel* owner_;
-  // Maps remote side ssrc to extended highest sequence number received.
-  std::map<uint32_t, uint32_t> extended_max_sequence_number_;
-  rtc::CriticalSection crit_;
-  RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
-};
-
-class Channel::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
- public:
-  ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
-                            Channel* channel)
-      : audio_frame_(std::move(audio_frame)), channel_(channel) {
-    RTC_DCHECK(channel_);
-  }
-
- private:
-  bool Run() override {
-    RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
-    channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
-    return true;
-  }
-
-  std::unique_ptr<AudioFrame> audio_frame_;
-  Channel* const channel_;
-};
-
-int32_t Channel::SendData(FrameType frameType,
-                          uint8_t payloadType,
-                          uint32_t timeStamp,
-                          const uint8_t* payloadData,
-                          size_t payloadSize,
-                          const RTPFragmentationHeader* fragmentation) {
-  RTC_DCHECK_RUN_ON(encoder_queue_);
-  if (_includeAudioLevelIndication) {
-    // Store current audio level in the RTP/RTCP module.
-    // The level will be used in combination with voice-activity state
-    // (frameType) to add an RTP header extension
-    _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
-  }
-
-  // Push data from ACM to RTP/RTCP-module to deliver audio frame for
-  // packetization.
-  // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
-  if (!_rtpRtcpModule->SendOutgoingData(
-          (FrameType&)frameType, payloadType, timeStamp,
-          // Leaving the time when this frame was
-          // received from the capture device as
-          // undefined for voice for now.
-          -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
-    RTC_DLOG(LS_ERROR)
-        << "Channel::SendData() failed to send data to RTP/RTCP module";
-    return -1;
-  }
-
-  return 0;
-}
-
-bool Channel::SendRtp(const uint8_t* data,
-                      size_t len,
-                      const PacketOptions& options) {
-  rtc::CritScope cs(&_callbackCritSect);
-
-  if (_transportPtr == NULL) {
-    RTC_DLOG(LS_ERROR)
-        << "Channel::SendPacket() failed to send RTP packet due to"
-        << " invalid transport object";
-    return false;
-  }
-
-  if (!_transportPtr->SendRtp(data, len, options)) {
-    RTC_DLOG(LS_ERROR) << "Channel::SendPacket() RTP transmission failed";
-    return false;
-  }
-  return true;
-}
-
-bool Channel::SendRtcp(const uint8_t* data, size_t len) {
-  rtc::CritScope cs(&_callbackCritSect);
-  if (_transportPtr == NULL) {
-    RTC_DLOG(LS_ERROR)
-        << "Channel::SendRtcp() failed to send RTCP packet due to"
-        << " invalid transport object";
-    return false;
-  }
-
-  int n = _transportPtr->SendRtcp(data, len);
-  if (n < 0) {
-    RTC_DLOG(LS_ERROR) << "Channel::SendRtcp() transmission failed";
-    return false;
-  }
-  return true;
-}
-
-int32_t Channel::OnReceivedPayloadData(const uint8_t* payloadData,
-                                       size_t payloadSize,
-                                       const WebRtcRTPHeader* rtpHeader) {
-  if (!channel_state_.Get().playing) {
-    // Avoid inserting into NetEQ when we are not playing. Count the
-    // packet as discarded.
-    return 0;
-  }
-
-  // Push the incoming payload (parsed and ready for decoding) into the ACM
-  if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
-      0) {
-    RTC_DLOG(LS_ERROR)
-        << "Channel::OnReceivedPayloadData() unable to push data to the ACM";
-    return -1;
-  }
-
-  int64_t round_trip_time = 0;
-  _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL);
-
-  std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
-  if (!nack_list.empty()) {
-    // Can't use nack_list.data() since it's not supported by all
-    // compilers.
-    ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
-  }
-  return 0;
-}
-
-AudioMixer::Source::AudioFrameInfo Channel::GetAudioFrameWithInfo(
-    int sample_rate_hz,
-    AudioFrame* audio_frame) {
-  audio_frame->sample_rate_hz_ = sample_rate_hz;
-
-  unsigned int ssrc;
-  RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
-  event_log_->Log(absl::make_unique<RtcEventAudioPlayout>(ssrc));
-  // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
-  bool muted;
-  if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame,
-                                     &muted) == -1) {
-    RTC_DLOG(LS_ERROR) << "Channel::GetAudioFrame() PlayoutData10Ms() failed!";
-    // In all likelihood, the audio in this frame is garbage. We return an
-    // error so that the audio mixer module doesn't add it to the mix. As
-    // a result, it won't be played out and the actions skipped here are
-    // irrelevant.
-    return AudioMixer::Source::AudioFrameInfo::kError;
-  }
-
-  if (muted) {
-    // TODO(henrik.lundin): We should be able to do better than this. But we
-    // will have to go through all the cases below where the audio samples may
-    // be used, and handle the muted case in some way.
-    AudioFrameOperations::Mute(audio_frame);
-  }
-
-  {
-    // Pass the audio buffers to an optional sink callback, before applying
-    // scaling/panning, as that applies to the mix operation.
-    // External recipients of the audio (e.g. via AudioTrack), will do their
-    // own mixing/dynamic processing.
-    rtc::CritScope cs(&_callbackCritSect);
-    if (audio_sink_) {
-      AudioSinkInterface::Data data(
-          audio_frame->data(), audio_frame->samples_per_channel_,
-          audio_frame->sample_rate_hz_, audio_frame->num_channels_,
-          audio_frame->timestamp_);
-      audio_sink_->OnData(data);
-    }
-  }
-
-  float output_gain = 1.0f;
-  {
-    rtc::CritScope cs(&volume_settings_critsect_);
-    output_gain = _outputGain;
-  }
-
-  // Output volume scaling
-  if (output_gain < 0.99f || output_gain > 1.01f) {
-    // TODO(solenberg): Combine with mute state - this can cause clicks!
-    AudioFrameOperations::ScaleWithSat(output_gain, audio_frame);
-  }
-
-  // Measure audio level (0-9)
-  // TODO(henrik.lundin) Use the |muted| information here too.
-  // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
-  // https://crbug.com/webrtc/7517).
-  _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds);
-
-  if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) {
-    // The first frame with a valid rtp timestamp.
-    capture_start_rtp_time_stamp_ = audio_frame->timestamp_;
-  }
-
-  if (capture_start_rtp_time_stamp_ >= 0) {
-    // audio_frame.timestamp_ should be valid from now on.
-
-    // Compute elapsed time.
-    int64_t unwrap_timestamp =
-        rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_);
-    audio_frame->elapsed_time_ms_ =
-        (unwrap_timestamp - capture_start_rtp_time_stamp_) /
-        (GetRtpTimestampRateHz() / 1000);
-
-    {
-      rtc::CritScope lock(&ts_stats_lock_);
-      // Compute ntp time.
-      audio_frame->ntp_time_ms_ =
-          ntp_estimator_.Estimate(audio_frame->timestamp_);
-      // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
-      if (audio_frame->ntp_time_ms_ > 0) {
-        // Compute |capture_start_ntp_time_ms_| so that
-        // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
-        capture_start_ntp_time_ms_ =
-            audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_;
-      }
-    }
-  }
-
-  {
-    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.TargetJitterBufferDelayMs",
-                              audio_coding_->TargetDelayMs());
-    const int jitter_buffer_delay = audio_coding_->FilteredCurrentDelayMs();
-    rtc::CritScope lock(&video_sync_lock_);
-    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDelayEstimateMs",
-                              jitter_buffer_delay + playout_delay_ms_);
-    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverJitterBufferDelayMs",
-                              jitter_buffer_delay);
-    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDeviceDelayMs",
-                              playout_delay_ms_);
-  }
-
-  return muted ? AudioMixer::Source::AudioFrameInfo::kMuted
-               : AudioMixer::Source::AudioFrameInfo::kNormal;
-}
-
-int Channel::PreferredSampleRate() const {
-  // Return the bigger of playout and receive frequency in the ACM.
-  return std::max(audio_coding_->ReceiveFrequency(),
-                  audio_coding_->PlayoutFrequency());
-}
-
-Channel::Channel(rtc::TaskQueue* encoder_queue,
-                 ProcessThread* module_process_thread,
-                 AudioDeviceModule* audio_device_module,
-                 RtcpRttStats* rtcp_rtt_stats,
-                 RtcEventLog* rtc_event_log)
-    : Channel(module_process_thread,
-              audio_device_module,
-              rtcp_rtt_stats,
-              rtc_event_log,
-              0,
-              0,
-              false,
-              rtc::scoped_refptr<AudioDecoderFactory>(),
-              absl::nullopt) {
-  RTC_DCHECK(encoder_queue);
-  encoder_queue_ = encoder_queue;
-}
-
-Channel::Channel(ProcessThread* module_process_thread,
-                 AudioDeviceModule* audio_device_module,
-                 RtcpRttStats* rtcp_rtt_stats,
-                 RtcEventLog* rtc_event_log,
-                 uint32_t remote_ssrc,
-                 size_t jitter_buffer_max_packets,
-                 bool jitter_buffer_fast_playout,
-                 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
-                 absl::optional<AudioCodecPairId> codec_pair_id)
-    : event_log_(rtc_event_log),
-      rtp_receive_statistics_(
-          ReceiveStatistics::Create(Clock::GetRealTimeClock())),
-      remote_ssrc_(remote_ssrc),
-      _outputAudioLevel(),
-      _timeStamp(0),  // This is just an offset, RTP module will add it's own
-                      // random offset
-      ntp_estimator_(Clock::GetRealTimeClock()),
-      playout_timestamp_rtp_(0),
-      playout_delay_ms_(0),
-      send_sequence_number_(0),
-      rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
-      capture_start_rtp_time_stamp_(-1),
-      capture_start_ntp_time_ms_(-1),
-      _moduleProcessThreadPtr(module_process_thread),
-      _audioDeviceModulePtr(audio_device_module),
-      _transportPtr(NULL),
-      input_mute_(false),
-      previous_frame_muted_(false),
-      _outputGain(1.0f),
-      _includeAudioLevelIndication(false),
-      transport_overhead_per_packet_(0),
-      rtp_overhead_per_packet_(0),
-      rtcp_observer_(new VoERtcpObserver(this)),
-      associated_send_channel_(nullptr),
-      feedback_observer_proxy_(new TransportFeedbackProxy()),
-      seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
-      rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
-      retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
-                                                   kMaxRetransmissionWindowMs)),
-      use_twcc_plr_for_ana_(
-          webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled") {
-  RTC_DCHECK(module_process_thread);
-  RTC_DCHECK(audio_device_module);
-  AudioCodingModule::Config acm_config;
-  acm_config.decoder_factory = decoder_factory;
-  acm_config.neteq_config.codec_pair_id = codec_pair_id;
-  acm_config.neteq_config.max_packets_in_buffer = jitter_buffer_max_packets;
-  acm_config.neteq_config.enable_fast_accelerate = jitter_buffer_fast_playout;
-  acm_config.neteq_config.enable_muted_state = true;
-  audio_coding_.reset(AudioCodingModule::Create(acm_config));
-
-  _outputAudioLevel.Clear();
-
-  rtp_receive_statistics_->EnableRetransmitDetection(remote_ssrc_, true);
-  RtpRtcp::Configuration configuration;
-  configuration.audio = true;
-  configuration.outgoing_transport = this;
-  configuration.overhead_observer = this;
-  configuration.receive_statistics = rtp_receive_statistics_.get();
-  configuration.bandwidth_callback = rtcp_observer_.get();
-  if (pacing_enabled_) {
-    configuration.paced_sender = rtp_packet_sender_proxy_.get();
-    configuration.transport_sequence_number_allocator =
-        seq_num_allocator_proxy_.get();
-    configuration.transport_feedback_callback = feedback_observer_proxy_.get();
-  }
-  configuration.event_log = event_log_;
-  configuration.rtt_stats = rtcp_rtt_stats;
-  configuration.retransmission_rate_limiter =
-      retransmission_rate_limiter_.get();
-
-  _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
-  _rtpRtcpModule->SetSendingMediaStatus(false);
-  _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_);
-  Init();
-}
-
-Channel::~Channel() {
-  Terminate();
-  RTC_DCHECK(!channel_state_.Get().sending);
-  RTC_DCHECK(!channel_state_.Get().playing);
-}
-
-void Channel::Init() {
-  channel_state_.Reset();
-
-  // --- Add modules to process thread (for periodic schedulation)
-  _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
-
-  // --- ACM initialization
-  int error = audio_coding_->InitializeReceiver();
-  RTC_DCHECK_EQ(0, error);
-
-  // --- RTP/RTCP module initialization
-
-  // Ensure that RTCP is enabled by default for the created channel.
-  // Note that, the module will keep generating RTCP until it is explicitly
-  // disabled by the user.
-  // After StopListen (when no sockets exists), RTCP packets will no longer
-  // be transmitted since the Transport object will then be invalid.
-  // RTCP is enabled by default.
-  _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
-
-  // --- Register all permanent callbacks
-  error = audio_coding_->RegisterTransportCallback(this);
-  RTC_DCHECK_EQ(0, error);
-}
-
-void Channel::Terminate() {
-  RTC_DCHECK(construction_thread_.CalledOnValidThread());
-  // Must be called on the same thread as Init().
-  rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
-
-  StopSend();
-  StopPlayout();
-
-  // The order to safely shutdown modules in a channel is:
-  // 1. De-register callbacks in modules
-  // 2. De-register modules in process thread
-  // 3. Destroy modules
-  int error = audio_coding_->RegisterTransportCallback(NULL);
-  RTC_DCHECK_EQ(0, error);
-
-  // De-register modules in process thread
-  if (_moduleProcessThreadPtr)
-    _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
-
-  // End of modules shutdown
-}
-
-void Channel::SetSink(AudioSinkInterface* sink) {
-  rtc::CritScope cs(&_callbackCritSect);
-  audio_sink_ = sink;
-}
-
-int32_t Channel::StartPlayout() {
-  if (channel_state_.Get().playing) {
-    return 0;
-  }
-
-  channel_state_.SetPlaying(true);
-
-  return 0;
-}
-
-int32_t Channel::StopPlayout() {
-  if (!channel_state_.Get().playing) {
-    return 0;
-  }
-
-  channel_state_.SetPlaying(false);
-  _outputAudioLevel.Clear();
-
-  return 0;
-}
-
-int32_t Channel::StartSend() {
-  if (channel_state_.Get().sending) {
-    return 0;
-  }
-  channel_state_.SetSending(true);
-
-  // Resume the previous sequence number which was reset by StopSend(). This
-  // needs to be done before |sending| is set to true on the RTP/RTCP module.
-  if (send_sequence_number_) {
-    _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
-  }
-  _rtpRtcpModule->SetSendingMediaStatus(true);
-  if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
-    RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending";
-    _rtpRtcpModule->SetSendingMediaStatus(false);
-    rtc::CritScope cs(&_callbackCritSect);
-    channel_state_.SetSending(false);
-    return -1;
-  }
-  {
-    // It is now OK to start posting tasks to the encoder task queue.
-    rtc::CritScope cs(&encoder_queue_lock_);
-    encoder_queue_is_active_ = true;
-  }
-  return 0;
-}
-
-void Channel::StopSend() {
-  if (!channel_state_.Get().sending) {
-    return;
-  }
-  channel_state_.SetSending(false);
-
-  // Post a task to the encoder thread which sets an event when the task is
-  // executed. We know that no more encoding tasks will be added to the task
-  // queue for this channel since sending is now deactivated. It means that,
-  // if we wait for the event to bet set, we know that no more pending tasks
-  // exists and it is therfore guaranteed that the task queue will never try
-  // to acccess and invalid channel object.
-  RTC_DCHECK(encoder_queue_);
-
-  rtc::Event flush(false, false);
-  {
-    // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
-    // than this final "flush task" to be posted on the queue.
-    rtc::CritScope cs(&encoder_queue_lock_);
-    encoder_queue_is_active_ = false;
-    encoder_queue_->PostTask([&flush]() { flush.Set(); });
-  }
-  flush.Wait(rtc::Event::kForever);
-
-  // Store the sequence number to be able to pick up the same sequence for
-  // the next StartSend(). This is needed for restarting device, otherwise
-  // it might cause libSRTP to complain about packets being replayed.
-  // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
-  // CL is landed. See issue
-  // https://code.google.com/p/webrtc/issues/detail?id=2111 .
-  send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
-
-  // Reset sending SSRC and sequence number and triggers direct transmission
-  // of RTCP BYE
-  if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
-    RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
-  }
-  _rtpRtcpModule->SetSendingMediaStatus(false);
-}
-
-bool Channel::SetEncoder(int payload_type,
-                         std::unique_ptr<AudioEncoder> encoder) {
-  RTC_DCHECK_GE(payload_type, 0);
-  RTC_DCHECK_LE(payload_type, 127);
-  // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
-  // one for for us to keep track of sample rate and number of channels, etc.
-
-  // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
-  // as well as some other things, so we collect this info and send it along.
-  CodecInst rtp_codec;
-  rtp_codec.pltype = payload_type;
-  strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
-  rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
-  // Seems unclear if it should be clock rate or sample rate. CodecInst
-  // supposedly carries the sample rate, but only clock rate seems sensible to
-  // send to the RTP/RTCP module.
-  rtp_codec.plfreq = encoder->RtpTimestampRateHz();
-  rtp_codec.pacsize = rtc::CheckedDivExact(
-      static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
-      100);
-  rtp_codec.channels = encoder->NumChannels();
-  rtp_codec.rate = 0;
-
-  if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
-    _rtpRtcpModule->DeRegisterSendPayload(payload_type);
-    if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
-      RTC_DLOG(LS_ERROR)
-          << "SetEncoder() failed to register codec to RTP/RTCP module";
-      return false;
-    }
-  }
-
-  audio_coding_->SetEncoder(std::move(encoder));
-  return true;
-}
-
-void Channel::ModifyEncoder(
-    rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
-  audio_coding_->ModifyEncoder(modifier);
-}
-
-int32_t Channel::GetRecCodec(CodecInst& codec) {
-  return (audio_coding_->ReceiveCodec(&codec));
-}
-
-void Channel::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->OnReceivedUplinkBandwidth(bitrate_bps, probing_interval_ms);
-    }
-  });
-  retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
-}
-
-void Channel::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
-  if (!use_twcc_plr_for_ana_)
-    return;
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
-    }
-  });
-}
-
-void Channel::OnRecoverableUplinkPacketLossRate(
-    float recoverable_packet_loss_rate) {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
-          recoverable_packet_loss_rate);
-    }
-  });
-}
-
-std::vector<webrtc::RtpSource> Channel::GetSources() const {
-  int64_t now_ms = rtc::TimeMillis();
-  std::vector<RtpSource> sources;
-  {
-    rtc::CritScope cs(&rtp_sources_lock_);
-    sources = contributing_sources_.GetSources(now_ms);
-    if (last_received_rtp_system_time_ms_ >=
-        now_ms - ContributingSources::kHistoryMs) {
-      sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_,
-                           RtpSourceType::SSRC);
-      sources.back().set_audio_level(last_received_rtp_audio_level_);
-    }
-  }
-  return sources;
-}
-
-void Channel::OnUplinkPacketLossRate(float packet_loss_rate) {
-  if (use_twcc_plr_for_ana_)
-    return;
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
-    }
-  });
-}
-
-void Channel::SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) {
-  for (const auto& kv : codecs) {
-    RTC_DCHECK_GE(kv.second.clockrate_hz, 1000);
-    payload_type_frequencies_[kv.first] = kv.second.clockrate_hz;
-  }
-  audio_coding_->SetReceiveCodecs(codecs);
-}
-
-bool Channel::EnableAudioNetworkAdaptor(const std::string& config_string) {
-  bool success = false;
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      success =
-          (*encoder)->EnableAudioNetworkAdaptor(config_string, event_log_);
-    }
-  });
-  return success;
-}
-
-void Channel::DisableAudioNetworkAdaptor() {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder)
-      (*encoder)->DisableAudioNetworkAdaptor();
-  });
-}
-
-void Channel::SetReceiverFrameLengthRange(int min_frame_length_ms,
-                                          int max_frame_length_ms) {
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
-                                              max_frame_length_ms);
-    }
-  });
-}
-
-void Channel::RegisterTransport(Transport* transport) {
-  rtc::CritScope cs(&_callbackCritSect);
-  _transportPtr = transport;
-}
-
-// TODO(nisse): Move receive logic up to AudioReceiveStream.
-void Channel::OnRtpPacket(const RtpPacketReceived& packet) {
-  int64_t now_ms = rtc::TimeMillis();
-  uint8_t audio_level;
-  bool voice_activity;
-  bool has_audio_level =
-      packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level);
-
-  {
-    rtc::CritScope cs(&rtp_sources_lock_);
-    last_received_rtp_timestamp_ = packet.Timestamp();
-    last_received_rtp_system_time_ms_ = now_ms;
-    if (has_audio_level)
-      last_received_rtp_audio_level_ = audio_level;
-    std::vector<uint32_t> csrcs = packet.Csrcs();
-    contributing_sources_.Update(now_ms, csrcs);
-  }
-
-  RTPHeader header;
-  packet.GetHeader(&header);
-
-  // Store playout timestamp for the received RTP packet
-  UpdatePlayoutTimestamp(false);
-
-  const auto& it = payload_type_frequencies_.find(header.payloadType);
-  if (it == payload_type_frequencies_.end())
-    return;
-  header.payload_type_frequency = it->second;
-
-  rtp_receive_statistics_->IncomingPacket(header, packet.size());
-
-  ReceivePacket(packet.data(), packet.size(), header);
-}
-
-bool Channel::ReceivePacket(const uint8_t* packet,
-                            size_t packet_length,
-                            const RTPHeader& header) {
-  const uint8_t* payload = packet + header.headerLength;
-  assert(packet_length >= header.headerLength);
-  size_t payload_length = packet_length - header.headerLength;
-  WebRtcRTPHeader webrtc_rtp_header = {};
-  webrtc_rtp_header.header = header;
-
-  const size_t payload_data_length = payload_length - header.paddingLength;
-  if (payload_data_length == 0) {
-    webrtc_rtp_header.frameType = kEmptyFrame;
-    return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header);
-  }
-  return OnReceivedPayloadData(payload, payload_data_length,
-                               &webrtc_rtp_header);
-}
-
-int32_t Channel::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
-  // Store playout timestamp for the received RTCP packet
-  UpdatePlayoutTimestamp(true);
-
-  // Deliver RTCP packet to RTP/RTCP module for parsing
-  _rtpRtcpModule->IncomingRtcpPacket(data, length);
-
-  int64_t rtt = GetRTT(true);
-  if (rtt == 0) {
-    // Waiting for valid RTT.
-    return 0;
-  }
-
-  int64_t nack_window_ms = rtt;
-  if (nack_window_ms < kMinRetransmissionWindowMs) {
-    nack_window_ms = kMinRetransmissionWindowMs;
-  } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
-    nack_window_ms = kMaxRetransmissionWindowMs;
-  }
-  retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
-
-  // Invoke audio encoders OnReceivedRtt().
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder)
-      (*encoder)->OnReceivedRtt(rtt);
-  });
-
-  uint32_t ntp_secs = 0;
-  uint32_t ntp_frac = 0;
-  uint32_t rtp_timestamp = 0;
-  if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
-                                     &rtp_timestamp)) {
-    // Waiting for RTCP.
-    return 0;
-  }
-
-  {
-    rtc::CritScope lock(&ts_stats_lock_);
-    ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
-  }
-  return 0;
-}
-
-int Channel::GetSpeechOutputLevelFullRange() const {
-  return _outputAudioLevel.LevelFullRange();
-}
-
-double Channel::GetTotalOutputEnergy() const {
-  return _outputAudioLevel.TotalEnergy();
-}
-
-double Channel::GetTotalOutputDuration() const {
-  return _outputAudioLevel.TotalDuration();
-}
-
-void Channel::SetInputMute(bool enable) {
-  rtc::CritScope cs(&volume_settings_critsect_);
-  input_mute_ = enable;
-}
-
-bool Channel::InputMute() const {
-  rtc::CritScope cs(&volume_settings_critsect_);
-  return input_mute_;
-}
-
-void Channel::SetChannelOutputVolumeScaling(float scaling) {
-  rtc::CritScope cs(&volume_settings_critsect_);
-  _outputGain = scaling;
-}
-
-int Channel::SendTelephoneEventOutband(int event, int duration_ms) {
-  RTC_DCHECK_LE(0, event);
-  RTC_DCHECK_GE(255, event);
-  RTC_DCHECK_LE(0, duration_ms);
-  RTC_DCHECK_GE(65535, duration_ms);
-  if (!Sending()) {
-    return -1;
-  }
-  if (_rtpRtcpModule->SendTelephoneEventOutband(
-          event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
-    RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
-    return -1;
-  }
-  return 0;
-}
-
-int Channel::SetSendTelephoneEventPayloadType(int payload_type,
-                                              int payload_frequency) {
-  RTC_DCHECK_LE(0, payload_type);
-  RTC_DCHECK_GE(127, payload_type);
-  CodecInst codec = {0};
-  codec.pltype = payload_type;
-  codec.plfreq = payload_frequency;
-  memcpy(codec.plname, "telephone-event", 16);
-  if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
-    _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
-    if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
-      RTC_DLOG(LS_ERROR)
-          << "SetSendTelephoneEventPayloadType() failed to register "
-             "send payload type";
-      return -1;
-    }
-  }
-  return 0;
-}
-
-int Channel::SetLocalSSRC(unsigned int ssrc) {
-  if (channel_state_.Get().sending) {
-    RTC_DLOG(LS_ERROR) << "SetLocalSSRC() already sending";
-    return -1;
-  }
-  _rtpRtcpModule->SetSSRC(ssrc);
-  return 0;
-}
-
-void Channel::SetMid(const std::string& mid, int extension_id) {
-  int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id);
-  RTC_DCHECK_EQ(0, ret);
-  _rtpRtcpModule->SetMid(mid);
-}
-
-// TODO(nisse): Pass ssrc in return value instead.
-int Channel::GetRemoteSSRC(unsigned int& ssrc) {
-  ssrc = remote_ssrc_;
-  return 0;
-}
-
-int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
-  _includeAudioLevelIndication = enable;
-  return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
-}
-
-void Channel::EnableSendTransportSequenceNumber(int id) {
-  int ret =
-      SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
-  RTC_DCHECK_EQ(0, ret);
-}
-
-void Channel::RegisterSenderCongestionControlObjects(
-    RtpTransportControllerSendInterface* transport,
-    RtcpBandwidthObserver* bandwidth_observer) {
-  RtpPacketSender* rtp_packet_sender = transport->packet_sender();
-  TransportFeedbackObserver* transport_feedback_observer =
-      transport->transport_feedback_observer();
-  PacketRouter* packet_router = transport->packet_router();
-
-  RTC_DCHECK(rtp_packet_sender);
-  RTC_DCHECK(transport_feedback_observer);
-  RTC_DCHECK(packet_router);
-  RTC_DCHECK(!packet_router_);
-  rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
-  feedback_observer_proxy_->SetTransportFeedbackObserver(
-      transport_feedback_observer);
-  seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
-  rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
-  _rtpRtcpModule->SetStorePacketsStatus(true, 600);
-  constexpr bool remb_candidate = false;
-  packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
-  packet_router_ = packet_router;
-}
-
-void Channel::RegisterReceiverCongestionControlObjects(
-    PacketRouter* packet_router) {
-  RTC_DCHECK(packet_router);
-  RTC_DCHECK(!packet_router_);
-  constexpr bool remb_candidate = false;
-  packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
-  packet_router_ = packet_router;
-}
-
-void Channel::ResetSenderCongestionControlObjects() {
-  RTC_DCHECK(packet_router_);
-  _rtpRtcpModule->SetStorePacketsStatus(false, 600);
-  rtcp_observer_->SetBandwidthObserver(nullptr);
-  feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
-  seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
-  packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
-  packet_router_ = nullptr;
-  rtp_packet_sender_proxy_->SetPacketSender(nullptr);
-}
-
-void Channel::ResetReceiverCongestionControlObjects() {
-  RTC_DCHECK(packet_router_);
-  packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
-  packet_router_ = nullptr;
-}
-
-void Channel::SetRTCPStatus(bool enable) {
-  _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
-}
-
-int Channel::SetRTCP_CNAME(const char cName[256]) {
-  if (_rtpRtcpModule->SetCNAME(cName) != 0) {
-    RTC_DLOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME";
-    return -1;
-  }
-  return 0;
-}
-
-int Channel::GetRemoteRTCPReportBlocks(
-    std::vector<ReportBlock>* report_blocks) {
-  if (report_blocks == NULL) {
-    RTC_DLOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks.";
-    return -1;
-  }
-
-  // Get the report blocks from the latest received RTCP Sender or Receiver
-  // Report. Each element in the vector contains the sender's SSRC and a
-  // report block according to RFC 3550.
-  std::vector<RTCPReportBlock> rtcp_report_blocks;
-  if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
-    return -1;
-  }
-
-  if (rtcp_report_blocks.empty())
-    return 0;
-
-  std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
-  for (; it != rtcp_report_blocks.end(); ++it) {
-    ReportBlock report_block;
-    report_block.sender_SSRC = it->sender_ssrc;
-    report_block.source_SSRC = it->source_ssrc;
-    report_block.fraction_lost = it->fraction_lost;
-    report_block.cumulative_num_packets_lost = it->packets_lost;
-    report_block.extended_highest_sequence_number =
-        it->extended_highest_sequence_number;
-    report_block.interarrival_jitter = it->jitter;
-    report_block.last_SR_timestamp = it->last_sender_report_timestamp;
-    report_block.delay_since_last_SR = it->delay_since_last_sender_report;
-    report_blocks->push_back(report_block);
-  }
-  return 0;
-}
-
-int Channel::GetRTPStatistics(CallStatistics& stats) {
-  // --- RtcpStatistics
-
-  // The jitter statistics is updated for each received RTP packet and is
-  // based on received packets.
-  RtcpStatistics statistics;
-  StreamStatistician* statistician =
-      rtp_receive_statistics_->GetStatistician(remote_ssrc_);
-  if (statistician) {
-    statistician->GetStatistics(&statistics,
-                                _rtpRtcpModule->RTCP() == RtcpMode::kOff);
-  }
-
-  stats.fractionLost = statistics.fraction_lost;
-  stats.cumulativeLost = statistics.packets_lost;
-  stats.extendedMax = statistics.extended_highest_sequence_number;
-  stats.jitterSamples = statistics.jitter;
-
-  // --- RTT
-  stats.rttMs = GetRTT(true);
-
-  // --- Data counters
-
-  size_t bytesSent(0);
-  uint32_t packetsSent(0);
-  size_t bytesReceived(0);
-  uint32_t packetsReceived(0);
-
-  if (statistician) {
-    statistician->GetDataCounters(&bytesReceived, &packetsReceived);
-  }
-
-  if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
-    RTC_DLOG(LS_WARNING)
-        << "GetRTPStatistics() failed to retrieve RTP datacounters"
-        << " => output will not be complete";
-  }
-
-  stats.bytesSent = bytesSent;
-  stats.packetsSent = packetsSent;
-  stats.bytesReceived = bytesReceived;
-  stats.packetsReceived = packetsReceived;
-
-  // --- Timestamps
-  {
-    rtc::CritScope lock(&ts_stats_lock_);
-    stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
-  }
-  return 0;
-}
-
-void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
-  // None of these functions can fail.
-  // If pacing is enabled we always store packets.
-  if (!pacing_enabled_)
-    _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
-  rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
-  if (enable)
-    audio_coding_->EnableNack(maxNumberOfPackets);
-  else
-    audio_coding_->DisableNack();
-}
-
-// Called when we are missing one or more packets.
-int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
-  return _rtpRtcpModule->SendNACK(sequence_numbers, length);
-}
-
-void Channel::ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) {
-  // Avoid posting any new tasks if sending was already stopped in StopSend().
-  rtc::CritScope cs(&encoder_queue_lock_);
-  if (!encoder_queue_is_active_) {
-    return;
-  }
-  // Profile time between when the audio frame is added to the task queue and
-  // when the task is actually executed.
-  audio_frame->UpdateProfileTimeStamp();
-  encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
-      new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
-}
-
-void Channel::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
-  RTC_DCHECK_RUN_ON(encoder_queue_);
-  RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
-  RTC_DCHECK_LE(audio_input->num_channels_, 2);
-
-  // Measure time between when the audio frame is added to the task queue and
-  // when the task is actually executed. Goal is to keep track of unwanted
-  // extra latency added by the task queue.
-  RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
-                             audio_input->ElapsedProfileTimeMs());
-
-  bool is_muted = InputMute();
-  AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
-
-  if (_includeAudioLevelIndication) {
-    size_t length =
-        audio_input->samples_per_channel_ * audio_input->num_channels_;
-    RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
-    if (is_muted && previous_frame_muted_) {
-      rms_level_.AnalyzeMuted(length);
-    } else {
-      rms_level_.Analyze(
-          rtc::ArrayView<const int16_t>(audio_input->data(), length));
-    }
-  }
-  previous_frame_muted_ = is_muted;
-
-  // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
-
-  // The ACM resamples internally.
-  audio_input->timestamp_ = _timeStamp;
-  // This call will trigger AudioPacketizationCallback::SendData if encoding
-  // is done and payload is ready for packetization and transmission.
-  // Otherwise, it will return without invoking the callback.
-  if (audio_coding_->Add10MsData(*audio_input) < 0) {
-    RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
-    return;
-  }
-
-  _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
-}
-
-void Channel::SetAssociatedSendChannel(Channel* channel) {
-  RTC_DCHECK_NE(this, channel);
-  rtc::CritScope lock(&assoc_send_channel_lock_);
-  associated_send_channel_ = channel;
-}
-
-void Channel::UpdateOverheadForEncoder() {
-  size_t overhead_per_packet =
-      transport_overhead_per_packet_ + rtp_overhead_per_packet_;
-  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
-    if (*encoder) {
-      (*encoder)->OnReceivedOverhead(overhead_per_packet);
-    }
-  });
-}
-
-void Channel::SetTransportOverhead(size_t transport_overhead_per_packet) {
-  rtc::CritScope cs(&overhead_per_packet_lock_);
-  transport_overhead_per_packet_ = transport_overhead_per_packet;
-  UpdateOverheadForEncoder();
-}
-
-// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
-void Channel::OnOverheadChanged(size_t overhead_bytes_per_packet) {
-  rtc::CritScope cs(&overhead_per_packet_lock_);
-  rtp_overhead_per_packet_ = overhead_bytes_per_packet;
-  UpdateOverheadForEncoder();
-}
-
-int Channel::GetNetworkStatistics(NetworkStatistics& stats) {
-  return audio_coding_->GetNetworkStatistics(&stats);
-}
-
-void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
-  audio_coding_->GetDecodingCallStatistics(stats);
-}
-
-ANAStats Channel::GetANAStatistics() const {
-  return audio_coding_->GetANAStats();
-}
-
-uint32_t Channel::GetDelayEstimate() const {
-  rtc::CritScope lock(&video_sync_lock_);
-  return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
-}
-
-int Channel::SetMinimumPlayoutDelay(int delayMs) {
-  if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
-      (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
-    RTC_DLOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay";
-    return -1;
-  }
-  if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
-    RTC_DLOG(LS_ERROR)
-        << "SetMinimumPlayoutDelay() failed to set min playout delay";
-    return -1;
-  }
-  return 0;
-}
-
-int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
-  uint32_t playout_timestamp_rtp = 0;
-  {
-    rtc::CritScope lock(&video_sync_lock_);
-    playout_timestamp_rtp = playout_timestamp_rtp_;
-  }
-  if (playout_timestamp_rtp == 0) {
-    RTC_DLOG(LS_ERROR) << "GetPlayoutTimestamp() failed to retrieve timestamp";
-    return -1;
-  }
-  timestamp = playout_timestamp_rtp;
-  return 0;
-}
-
-RtpRtcp* Channel::GetRtpRtcp() const {
-  return _rtpRtcpModule.get();
-}
-
-absl::optional<Syncable::Info> Channel::GetSyncInfo() const {
-  Syncable::Info info;
-  if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs,
-                                &info.capture_time_ntp_frac, nullptr, nullptr,
-                                &info.capture_time_source_clock) != 0) {
-    return absl::nullopt;
-  }
-  {
-    rtc::CritScope cs(&rtp_sources_lock_);
-    if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
-      return absl::nullopt;
-    }
-    info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
-    info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
-  }
-  return info;
-}
-
-void Channel::UpdatePlayoutTimestamp(bool rtcp) {
-  jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
-
-  if (!jitter_buffer_playout_timestamp_) {
-    // This can happen if this channel has not received any RTP packets. In
-    // this case, NetEq is not capable of computing a playout timestamp.
-    return;
-  }
-
-  uint16_t delay_ms = 0;
-  if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
-    RTC_DLOG(LS_WARNING) << "Channel::UpdatePlayoutTimestamp() failed to read"
-                         << " playout delay from the ADM";
-    return;
-  }
-
-  RTC_DCHECK(jitter_buffer_playout_timestamp_);
-  uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
-
-  // Remove the playout delay.
-  playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
-
-  {
-    rtc::CritScope lock(&video_sync_lock_);
-    if (!rtcp) {
-      playout_timestamp_rtp_ = playout_timestamp;
-    }
-    playout_delay_ms_ = delay_ms;
-  }
-}
-
-int Channel::SetSendRtpHeaderExtension(bool enable,
-                                       RTPExtensionType type,
-                                       unsigned char id) {
-  int error = 0;
-  _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
-  if (enable) {
-    error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
-  }
-  return error;
-}
-
-int Channel::GetRtpTimestampRateHz() const {
-  const auto format = audio_coding_->ReceiveFormat();
-  // Default to the playout frequency if we've not gotten any packets yet.
-  // TODO(ossu): Zero clockrate can only happen if we've added an external
-  // decoder for a format we don't support internally. Remove once that way of
-  // adding decoders is gone!
-  return (format && format->clockrate_hz != 0)
-             ? format->clockrate_hz
-             : audio_coding_->PlayoutFrequency();
-}
-
-int64_t Channel::GetRTT(bool allow_associate_channel) const {
-  RtcpMode method = _rtpRtcpModule->RTCP();
-  if (method == RtcpMode::kOff) {
-    return 0;
-  }
-  std::vector<RTCPReportBlock> report_blocks;
-  _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
-
-  int64_t rtt = 0;
-  if (report_blocks.empty()) {
-    if (allow_associate_channel) {
-      rtc::CritScope lock(&assoc_send_channel_lock_);
-      // Tries to get RTT from an associated channel. This is important for
-      // receive-only channels.
-      if (associated_send_channel_) {
-        // To prevent infinite recursion and deadlock, calling GetRTT of
-        // associate channel should always use "false" for argument:
-        // |allow_associate_channel|.
-        rtt = associated_send_channel_->GetRTT(false);
-      }
-    }
-    return rtt;
-  }
-
-  std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
-  for (; it != report_blocks.end(); ++it) {
-    if (it->sender_ssrc == remote_ssrc_)
-      break;
-  }
-
-  // If we have not received packets with SSRC matching the report blocks, use
-  // the SSRC of the first report block for calculating the RTT. This is very
-  // important for send-only channels where we don't know the SSRC of the other
-  // end.
-  uint32_t ssrc =
-      (it == report_blocks.end()) ? report_blocks[0].sender_ssrc : remote_ssrc_;
-
-  int64_t avg_rtt = 0;
-  int64_t max_rtt = 0;
-  int64_t min_rtt = 0;
-  if (_rtpRtcpModule->RTT(ssrc, &rtt, &avg_rtt, &min_rtt, &max_rtt) != 0) {
-    return 0;
-  }
-  return rtt;
-}
-
-}  // namespace voe
-}  // namespace webrtc
diff --git a/audio/channel.h b/audio/channel.h
deleted file mode 100644
index 562e79e..0000000
--- a/audio/channel.h
+++ /dev/null
@@ -1,423 +0,0 @@
-/*
- *  Copyright (c) 2012 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 AUDIO_CHANNEL_H_
-#define AUDIO_CHANNEL_H_
-
-#include <map>
-#include <memory>
-#include <string>
-#include <vector>
-
-#include "absl/types/optional.h"
-#include "api/audio/audio_mixer.h"
-#include "api/audio_codecs/audio_encoder.h"
-#include "api/call/audio_sink.h"
-#include "api/call/transport.h"
-#include "api/rtpreceiverinterface.h"
-#include "audio/audio_level.h"
-#include "call/syncable.h"
-#include "common_types.h"  // NOLINT(build/include)
-#include "modules/audio_coding/include/audio_coding_module.h"
-#include "modules/audio_processing/rms_level.h"
-#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
-#include "modules/rtp_rtcp/include/rtp_header_parser.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp.h"
-#include "modules/rtp_rtcp/source/contributing_sources.h"
-#include "rtc_base/criticalsection.h"
-#include "rtc_base/event.h"
-#include "rtc_base/task_queue.h"
-#include "rtc_base/thread_checker.h"
-
-// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
-// warnings about use of unsigned short, and non-const reference arguments.
-// These need cleanup, in a separate cl.
-
-namespace rtc {
-class TimestampWrapAroundHandler;
-}
-
-namespace webrtc {
-
-class AudioDeviceModule;
-class PacketRouter;
-class ProcessThread;
-class RateLimiter;
-class ReceiveStatistics;
-class RemoteNtpTimeEstimator;
-class RtcEventLog;
-class RtpPacketReceived;
-class RtpRtcp;
-class RtpTransportControllerSendInterface;
-
-struct SenderInfo;
-
-struct CallStatistics {
-  unsigned short fractionLost;  // NOLINT
-  unsigned int cumulativeLost;
-  unsigned int extendedMax;
-  unsigned int jitterSamples;
-  int64_t rttMs;
-  size_t bytesSent;
-  int packetsSent;
-  size_t bytesReceived;
-  int packetsReceived;
-  // The capture ntp time (in local timebase) of the first played out audio
-  // frame.
-  int64_t capture_start_ntp_time_ms_;
-};
-
-// See section 6.4.2 in http://www.ietf.org/rfc/rfc3550.txt for details.
-struct ReportBlock {
-  uint32_t sender_SSRC;  // SSRC of sender
-  uint32_t source_SSRC;
-  uint8_t fraction_lost;
-  int32_t cumulative_num_packets_lost;
-  uint32_t extended_highest_sequence_number;
-  uint32_t interarrival_jitter;
-  uint32_t last_SR_timestamp;
-  uint32_t delay_since_last_SR;
-};
-
-namespace voe {
-
-class RtpPacketSenderProxy;
-class TransportFeedbackProxy;
-class TransportSequenceNumberProxy;
-class VoERtcpObserver;
-
-// Helper class to simplify locking scheme for members that are accessed from
-// multiple threads.
-// Example: a member can be set on thread T1 and read by an internal audio
-// thread T2. Accessing the member via this class ensures that we are
-// safe and also avoid TSan v2 warnings.
-class ChannelState {
- public:
-  struct State {
-    bool playing = false;
-    bool sending = false;
-  };
-
-  ChannelState() {}
-  virtual ~ChannelState() {}
-
-  void Reset() {
-    rtc::CritScope lock(&lock_);
-    state_ = State();
-  }
-
-  State Get() const {
-    rtc::CritScope lock(&lock_);
-    return state_;
-  }
-
-  void SetPlaying(bool enable) {
-    rtc::CritScope lock(&lock_);
-    state_.playing = enable;
-  }
-
-  void SetSending(bool enable) {
-    rtc::CritScope lock(&lock_);
-    state_.sending = enable;
-  }
-
- private:
-  rtc::CriticalSection lock_;
-  State state_;
-};
-
-class Channel
-    : public RtpData,
-      public Transport,
-      public AudioPacketizationCallback,  // receive encoded packets from the
-                                          // ACM
-      public OverheadObserver {
- public:
-  friend class VoERtcpObserver;
-
-  enum { KNumSocketThreads = 1 };
-  enum { KNumberOfSocketBuffers = 8 };
-  // Used for send streams.
-  Channel(rtc::TaskQueue* encoder_queue,
-          ProcessThread* module_process_thread,
-          AudioDeviceModule* audio_device_module,
-          RtcpRttStats* rtcp_rtt_stats,
-          RtcEventLog* rtc_event_log);
-  // Used for receive streams.
-  Channel(ProcessThread* module_process_thread,
-          AudioDeviceModule* audio_device_module,
-          RtcpRttStats* rtcp_rtt_stats,
-          RtcEventLog* rtc_event_log,
-          uint32_t remote_ssrc,
-          size_t jitter_buffer_max_packets,
-          bool jitter_buffer_fast_playout,
-          rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
-          absl::optional<AudioCodecPairId> codec_pair_id);
-  virtual ~Channel();
-
-  void SetSink(AudioSinkInterface* sink);
-
-  void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
-
-  // Send using this encoder, with this payload type.
-  bool SetEncoder(int payload_type, std::unique_ptr<AudioEncoder> encoder);
-  void ModifyEncoder(
-      rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
-
-  // API methods
-
-  // VoEBase
-  int32_t StartPlayout();
-  int32_t StopPlayout();
-  int32_t StartSend();
-  void StopSend();
-
-  // Codecs
-  int32_t GetRecCodec(CodecInst& codec);  // NOLINT
-  void SetBitRate(int bitrate_bps, int64_t probing_interval_ms);
-  bool EnableAudioNetworkAdaptor(const std::string& config_string);
-  void DisableAudioNetworkAdaptor();
-  void SetReceiverFrameLengthRange(int min_frame_length_ms,
-                                   int max_frame_length_ms);
-
-  // Network
-  void RegisterTransport(Transport* transport);
-  // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
-  int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
-  void OnRtpPacket(const RtpPacketReceived& packet);
-
-  // Muting, Volume and Level.
-  void SetInputMute(bool enable);
-  void SetChannelOutputVolumeScaling(float scaling);
-  int GetSpeechOutputLevelFullRange() const;
-  // See description of "totalAudioEnergy" in the WebRTC stats spec:
-  // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
-  double GetTotalOutputEnergy() const;
-  double GetTotalOutputDuration() const;
-
-  // Stats.
-  int GetNetworkStatistics(NetworkStatistics& stats);  // NOLINT
-  void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
-  ANAStats GetANAStatistics() const;
-
-  // Audio+Video Sync.
-  uint32_t GetDelayEstimate() const;
-  int SetMinimumPlayoutDelay(int delayMs);
-  int GetPlayoutTimestamp(unsigned int& timestamp);  // NOLINT
-
-  // Used by AudioSendStream.
-  RtpRtcp* GetRtpRtcp() const;
-
-  // Produces the transport-related timestamps; current_delay_ms is left unset.
-  absl::optional<Syncable::Info> GetSyncInfo() const;
-  // DTMF.
-  int SendTelephoneEventOutband(int event, int duration_ms);
-  int SetSendTelephoneEventPayloadType(int payload_type, int payload_frequency);
-
-  // RTP+RTCP
-  int SetLocalSSRC(unsigned int ssrc);
-
-  void SetMid(const std::string& mid, int extension_id);
-  int SetSendAudioLevelIndicationStatus(bool enable, unsigned char id);
-  void EnableSendTransportSequenceNumber(int id);
-
-  void RegisterSenderCongestionControlObjects(
-      RtpTransportControllerSendInterface* transport,
-      RtcpBandwidthObserver* bandwidth_observer);
-  void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
-  void ResetSenderCongestionControlObjects();
-  void ResetReceiverCongestionControlObjects();
-  void SetRTCPStatus(bool enable);
-  int SetRTCP_CNAME(const char cName[256]);
-  int GetRemoteRTCPReportBlocks(std::vector<ReportBlock>* report_blocks);
-  int GetRTPStatistics(CallStatistics& stats);  // NOLINT
-  void SetNACKStatus(bool enable, int maxNumberOfPackets);
-
-  // From AudioPacketizationCallback in the ACM
-  int32_t SendData(FrameType frameType,
-                   uint8_t payloadType,
-                   uint32_t timeStamp,
-                   const uint8_t* payloadData,
-                   size_t payloadSize,
-                   const RTPFragmentationHeader* fragmentation) override;
-
-  // From RtpData in the RTP/RTCP module
-  int32_t OnReceivedPayloadData(const uint8_t* payloadData,
-                                size_t payloadSize,
-                                const WebRtcRTPHeader* rtpHeader) override;
-
-  // From Transport (called by the RTP/RTCP module)
-  bool SendRtp(const uint8_t* data,
-               size_t len,
-               const PacketOptions& packet_options) override;
-  bool SendRtcp(const uint8_t* data, size_t len) override;
-
-  // From AudioMixer::Source.
-  AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
-      int sample_rate_hz,
-      AudioFrame* audio_frame);
-
-  int PreferredSampleRate() const;
-
-  bool Playing() const { return channel_state_.Get().playing; }
-  bool Sending() const { return channel_state_.Get().sending; }
-  RtpRtcp* RtpRtcpModulePtr() const { return _rtpRtcpModule.get(); }
-
-  // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
-  // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
-  // the actual processing of the audio takes place. The processing mainly
-  // consists of encoding and preparing the result for sending by adding it to a
-  // send queue.
-  // The main reason for using a task queue here is to release the native,
-  // OS-specific, audio capture thread as soon as possible to ensure that it
-  // can go back to sleep and be prepared to deliver an new captured audio
-  // packet.
-  void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
-
-  // Associate to a send channel.
-  // Used for obtaining RTT for a receive-only channel.
-  void SetAssociatedSendChannel(Channel* channel);
-
-  void SetTransportOverhead(size_t transport_overhead_per_packet);
-
-  // From OverheadObserver in the RTP/RTCP module
-  void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
-
-  // The existence of this function alongside OnUplinkPacketLossRate is
-  // a compromise. We want the encoder to be agnostic of the PLR source, but
-  // we also don't want it to receive conflicting information from TWCC and
-  // from RTCP-XR.
-  void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
-
-  void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
-
-  std::vector<RtpSource> GetSources() const;
-
- private:
-  class ProcessAndEncodeAudioTask;
-
-  void Init();
-  void Terminate();
-
-  int GetRemoteSSRC(unsigned int& ssrc);  // NOLINT
-  void OnUplinkPacketLossRate(float packet_loss_rate);
-  bool InputMute() const;
-
-  bool ReceivePacket(const uint8_t* packet,
-                     size_t packet_length,
-                     const RTPHeader& header);
-  int ResendPackets(const uint16_t* sequence_numbers, int length);
-  void UpdatePlayoutTimestamp(bool rtcp);
-
-  int SetSendRtpHeaderExtension(bool enable,
-                                RTPExtensionType type,
-                                unsigned char id);
-
-  void UpdateOverheadForEncoder()
-      RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
-
-  int GetRtpTimestampRateHz() const;
-  int64_t GetRTT(bool allow_associate_channel) const;
-
-  // Called on the encoder task queue when a new input audio frame is ready
-  // for encoding.
-  void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input);
-
-  rtc::CriticalSection _callbackCritSect;
-  rtc::CriticalSection volume_settings_critsect_;
-
-  ChannelState channel_state_;
-
-  RtcEventLog* const event_log_;
-
-  // Indexed by payload type.
-  std::map<uint8_t, int> payload_type_frequencies_;
-
-  std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
-  std::unique_ptr<RtpRtcp> _rtpRtcpModule;
-  const uint32_t remote_ssrc_;
-
-  // Info for GetSources and GetSyncInfo is updated on network or worker thread,
-  // queried on the worker thread.
-  rtc::CriticalSection rtp_sources_lock_;
-  ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
-  absl::optional<uint32_t> last_received_rtp_timestamp_
-      RTC_GUARDED_BY(&rtp_sources_lock_);
-  absl::optional<int64_t> last_received_rtp_system_time_ms_
-      RTC_GUARDED_BY(&rtp_sources_lock_);
-  absl::optional<uint8_t> last_received_rtp_audio_level_
-      RTC_GUARDED_BY(&rtp_sources_lock_);
-
-  std::unique_ptr<AudioCodingModule> audio_coding_;
-  AudioSinkInterface* audio_sink_ = nullptr;
-  AudioLevel _outputAudioLevel;
-  uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
-
-  RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
-
-  // Timestamp of the audio pulled from NetEq.
-  absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
-
-  rtc::CriticalSection video_sync_lock_;
-  uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
-  uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
-  uint16_t send_sequence_number_;
-
-  rtc::CriticalSection ts_stats_lock_;
-
-  std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
-  // The rtp timestamp of the first played out audio frame.
-  int64_t capture_start_rtp_time_stamp_;
-  // The capture ntp time (in local timebase) of the first played out audio
-  // frame.
-  int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
-
-  // uses
-  ProcessThread* _moduleProcessThreadPtr;
-  AudioDeviceModule* _audioDeviceModulePtr;
-  Transport* _transportPtr;  // WebRtc socket or external transport
-  RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
-  bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
-  bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
-  float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
-  // VoeRTP_RTCP
-  // TODO(henrika): can today be accessed on the main thread and on the
-  // task queue; hence potential race.
-  bool _includeAudioLevelIndication;
-  size_t transport_overhead_per_packet_
-      RTC_GUARDED_BY(overhead_per_packet_lock_);
-  size_t rtp_overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_);
-  rtc::CriticalSection overhead_per_packet_lock_;
-  // RtcpBandwidthObserver
-  std::unique_ptr<VoERtcpObserver> rtcp_observer_;
-  // An associated send channel.
-  rtc::CriticalSection assoc_send_channel_lock_;
-  Channel* associated_send_channel_ RTC_GUARDED_BY(assoc_send_channel_lock_);
-
-  bool pacing_enabled_ = true;
-  PacketRouter* packet_router_ = nullptr;
-  std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
-  std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
-  std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
-  std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
-
-  rtc::ThreadChecker construction_thread_;
-
-  const bool use_twcc_plr_for_ana_;
-
-  rtc::CriticalSection encoder_queue_lock_;
-  bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_lock_) = false;
-  rtc::TaskQueue* encoder_queue_ = nullptr;
-};
-
-}  // namespace voe
-}  // namespace webrtc
-
-#endif  // AUDIO_CHANNEL_H_
diff --git a/audio/channel_proxy.cc b/audio/channel_proxy.cc
deleted file mode 100644
index 5e8181a..0000000
--- a/audio/channel_proxy.cc
+++ /dev/null
@@ -1,331 +0,0 @@
-/*
- *  Copyright (c) 2015 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 "audio/channel_proxy.h"
-
-#include <utility>
-
-#include "api/call/audio_sink.h"
-#include "call/rtp_transport_controller_send_interface.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/numerics/safe_minmax.h"
-
-namespace webrtc {
-namespace voe {
-ChannelProxy::ChannelProxy() {}
-
-ChannelProxy::ChannelProxy(std::unique_ptr<Channel> channel)
-    : channel_(std::move(channel)) {
-  RTC_DCHECK(channel_);
-  module_process_thread_checker_.DetachFromThread();
-}
-
-ChannelProxy::~ChannelProxy() {}
-
-bool ChannelProxy::SetEncoder(int payload_type,
-                              std::unique_ptr<AudioEncoder> encoder) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->SetEncoder(payload_type, std::move(encoder));
-}
-
-void ChannelProxy::ModifyEncoder(
-    rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->ModifyEncoder(modifier);
-}
-
-void ChannelProxy::SetRTCPStatus(bool enable) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetRTCPStatus(enable);
-}
-
-void ChannelProxy::SetLocalSSRC(uint32_t ssrc) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  int error = channel_->SetLocalSSRC(ssrc);
-  RTC_DCHECK_EQ(0, error);
-}
-
-void ChannelProxy::SetMid(const std::string& mid, int extension_id) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetMid(mid, extension_id);
-}
-
-void ChannelProxy::SetRTCP_CNAME(const std::string& c_name) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  // Note: VoERTP_RTCP::SetRTCP_CNAME() accepts a char[256] array.
-  std::string c_name_limited = c_name.substr(0, 255);
-  int error = channel_->SetRTCP_CNAME(c_name_limited.c_str());
-  RTC_DCHECK_EQ(0, error);
-}
-
-void ChannelProxy::SetNACKStatus(bool enable, int max_packets) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetNACKStatus(enable, max_packets);
-}
-
-void ChannelProxy::SetSendAudioLevelIndicationStatus(bool enable, int id) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  int error = channel_->SetSendAudioLevelIndicationStatus(enable, id);
-  RTC_DCHECK_EQ(0, error);
-}
-
-void ChannelProxy::EnableSendTransportSequenceNumber(int id) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->EnableSendTransportSequenceNumber(id);
-}
-
-void ChannelProxy::RegisterSenderCongestionControlObjects(
-    RtpTransportControllerSendInterface* transport,
-    RtcpBandwidthObserver* bandwidth_observer) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->RegisterSenderCongestionControlObjects(transport,
-                                                   bandwidth_observer);
-}
-
-void ChannelProxy::RegisterReceiverCongestionControlObjects(
-    PacketRouter* packet_router) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->RegisterReceiverCongestionControlObjects(packet_router);
-}
-
-void ChannelProxy::ResetSenderCongestionControlObjects() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->ResetSenderCongestionControlObjects();
-}
-
-void ChannelProxy::ResetReceiverCongestionControlObjects() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->ResetReceiverCongestionControlObjects();
-}
-
-CallStatistics ChannelProxy::GetRTCPStatistics() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  CallStatistics stats = {0};
-  int error = channel_->GetRTPStatistics(stats);
-  RTC_DCHECK_EQ(0, error);
-  return stats;
-}
-
-std::vector<ReportBlock> ChannelProxy::GetRemoteRTCPReportBlocks() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  std::vector<webrtc::ReportBlock> blocks;
-  int error = channel_->GetRemoteRTCPReportBlocks(&blocks);
-  RTC_DCHECK_EQ(0, error);
-  return blocks;
-}
-
-NetworkStatistics ChannelProxy::GetNetworkStatistics() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  NetworkStatistics stats = {0};
-  int error = channel_->GetNetworkStatistics(stats);
-  RTC_DCHECK_EQ(0, error);
-  return stats;
-}
-
-AudioDecodingCallStats ChannelProxy::GetDecodingCallStatistics() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  AudioDecodingCallStats stats;
-  channel_->GetDecodingCallStatistics(&stats);
-  return stats;
-}
-
-ANAStats ChannelProxy::GetANAStatistics() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetANAStatistics();
-}
-
-int ChannelProxy::GetSpeechOutputLevelFullRange() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetSpeechOutputLevelFullRange();
-}
-
-double ChannelProxy::GetTotalOutputEnergy() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetTotalOutputEnergy();
-}
-
-double ChannelProxy::GetTotalOutputDuration() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetTotalOutputDuration();
-}
-
-uint32_t ChannelProxy::GetDelayEstimate() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
-             module_process_thread_checker_.CalledOnValidThread());
-  return channel_->GetDelayEstimate();
-}
-
-bool ChannelProxy::SetSendTelephoneEventPayloadType(int payload_type,
-                                                    int payload_frequency) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->SetSendTelephoneEventPayloadType(payload_type,
-                                                    payload_frequency) == 0;
-}
-
-bool ChannelProxy::SendTelephoneEventOutband(int event, int duration_ms) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->SendTelephoneEventOutband(event, duration_ms) == 0;
-}
-
-void ChannelProxy::SetBitrate(int bitrate_bps, int64_t probing_interval_ms) {
-  // This method can be called on the worker thread, module process thread
-  // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged.
-  // TODO(solenberg): Figure out a good way to check this or enforce calling
-  // rules.
-  // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
-  //            module_process_thread_checker_.CalledOnValidThread());
-  channel_->SetBitRate(bitrate_bps, probing_interval_ms);
-}
-
-void ChannelProxy::SetReceiveCodecs(
-    const std::map<int, SdpAudioFormat>& codecs) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetReceiveCodecs(codecs);
-}
-
-void ChannelProxy::SetSink(AudioSinkInterface* sink) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetSink(sink);
-}
-
-void ChannelProxy::SetInputMute(bool muted) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetInputMute(muted);
-}
-
-void ChannelProxy::RegisterTransport(Transport* transport) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->RegisterTransport(transport);
-}
-
-void ChannelProxy::OnRtpPacket(const RtpPacketReceived& packet) {
-  // May be called on either worker thread or network thread.
-  channel_->OnRtpPacket(packet);
-}
-
-bool ChannelProxy::ReceivedRTCPPacket(const uint8_t* packet, size_t length) {
-  // May be called on either worker thread or network thread.
-  return channel_->ReceivedRTCPPacket(packet, length) == 0;
-}
-
-void ChannelProxy::SetChannelOutputVolumeScaling(float scaling) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetChannelOutputVolumeScaling(scaling);
-}
-
-AudioMixer::Source::AudioFrameInfo ChannelProxy::GetAudioFrameWithInfo(
-    int sample_rate_hz,
-    AudioFrame* audio_frame) {
-  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
-  return channel_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
-}
-
-int ChannelProxy::PreferredSampleRate() const {
-  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
-  return channel_->PreferredSampleRate();
-}
-
-void ChannelProxy::ProcessAndEncodeAudio(
-    std::unique_ptr<AudioFrame> audio_frame) {
-  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
-  return channel_->ProcessAndEncodeAudio(std::move(audio_frame));
-}
-
-void ChannelProxy::SetTransportOverhead(int transport_overhead_per_packet) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetTransportOverhead(transport_overhead_per_packet);
-}
-
-void ChannelProxy::AssociateSendChannel(
-    const ChannelProxy& send_channel_proxy) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetAssociatedSendChannel(send_channel_proxy.channel_.get());
-}
-
-void ChannelProxy::DisassociateSendChannel() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->SetAssociatedSendChannel(nullptr);
-}
-
-RtpRtcp* ChannelProxy::GetRtpRtcp() const {
-  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
-  return channel_->GetRtpRtcp();
-}
-
-absl::optional<Syncable::Info> ChannelProxy::GetSyncInfo() const {
-  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
-  return channel_->GetSyncInfo();
-}
-
-uint32_t ChannelProxy::GetPlayoutTimestamp() const {
-  RTC_DCHECK_RUNS_SERIALIZED(&video_capture_thread_race_checker_);
-  unsigned int timestamp = 0;
-  int error = channel_->GetPlayoutTimestamp(timestamp);
-  RTC_DCHECK(!error || timestamp == 0);
-  return timestamp;
-}
-
-void ChannelProxy::SetMinimumPlayoutDelay(int delay_ms) {
-  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
-  // Limit to range accepted by both VoE and ACM, so we're at least getting as
-  // close as possible, instead of failing.
-  delay_ms = rtc::SafeClamp(delay_ms, 0, 10000);
-  int error = channel_->SetMinimumPlayoutDelay(delay_ms);
-  if (0 != error) {
-    RTC_LOG(LS_WARNING) << "Error setting minimum playout delay.";
-  }
-}
-
-bool ChannelProxy::GetRecCodec(CodecInst* codec_inst) const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetRecCodec(*codec_inst) == 0;
-}
-
-void ChannelProxy::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->OnTwccBasedUplinkPacketLossRate(packet_loss_rate);
-}
-
-void ChannelProxy::OnRecoverableUplinkPacketLossRate(
-    float recoverable_packet_loss_rate) {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->OnRecoverableUplinkPacketLossRate(recoverable_packet_loss_rate);
-}
-
-std::vector<RtpSource> ChannelProxy::GetSources() const {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  return channel_->GetSources();
-}
-
-void ChannelProxy::StartSend() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  int error = channel_->StartSend();
-  RTC_DCHECK_EQ(0, error);
-}
-
-void ChannelProxy::StopSend() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  channel_->StopSend();
-}
-
-void ChannelProxy::StartPlayout() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  int error = channel_->StartPlayout();
-  RTC_DCHECK_EQ(0, error);
-}
-
-void ChannelProxy::StopPlayout() {
-  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
-  int error = channel_->StopPlayout();
-  RTC_DCHECK_EQ(0, error);
-}
-}  // namespace voe
-}  // namespace webrtc
diff --git a/audio/channel_receive.cc b/audio/channel_receive.cc
new file mode 100644
index 0000000..e9f7503
--- /dev/null
+++ b/audio/channel_receive.cc
@@ -0,0 +1,716 @@
+/*
+ *  Copyright (c) 2012 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 "audio/channel_receive.h"
+
+#include <algorithm>
+#include <map>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "absl/memory/memory.h"
+#include "audio/channel_send.h"
+#include "audio/utility/audio_frame_operations.h"
+#include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
+#include "logging/rtc_event_log/rtc_event_log.h"
+#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
+#include "modules/audio_device/include/audio_device.h"
+#include "modules/pacing/packet_router.h"
+#include "modules/rtp_rtcp/include/receive_statistics.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
+#include "modules/utility/include/process_thread.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/criticalsection.h"
+#include "rtc_base/format_macros.h"
+#include "rtc_base/location.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/thread_checker.h"
+#include "rtc_base/timeutils.h"
+#include "system_wrappers/include/metrics.h"
+
+namespace webrtc {
+namespace voe {
+
+namespace {
+
+constexpr double kAudioSampleDurationSeconds = 0.01;
+constexpr int64_t kMaxRetransmissionWindowMs = 1000;
+constexpr int64_t kMinRetransmissionWindowMs = 30;
+
+// Video Sync.
+constexpr int kVoiceEngineMinMinPlayoutDelayMs = 0;
+constexpr int kVoiceEngineMaxMinPlayoutDelayMs = 10000;
+
+}  // namespace
+
+int32_t ChannelReceive::OnReceivedPayloadData(
+    const uint8_t* payloadData,
+    size_t payloadSize,
+    const WebRtcRTPHeader* rtpHeader) {
+  if (!channel_state_.Get().playing) {
+    // Avoid inserting into NetEQ when we are not playing. Count the
+    // packet as discarded.
+    return 0;
+  }
+
+  // Push the incoming payload (parsed and ready for decoding) into the ACM
+  if (audio_coding_->IncomingPacket(payloadData, payloadSize, *rtpHeader) !=
+      0) {
+    RTC_DLOG(LS_ERROR) << "ChannelReceive::OnReceivedPayloadData() unable to "
+                          "push data to the ACM";
+    return -1;
+  }
+
+  int64_t round_trip_time = 0;
+  _rtpRtcpModule->RTT(remote_ssrc_, &round_trip_time, NULL, NULL, NULL);
+
+  std::vector<uint16_t> nack_list = audio_coding_->GetNackList(round_trip_time);
+  if (!nack_list.empty()) {
+    // Can't use nack_list.data() since it's not supported by all
+    // compilers.
+    ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
+  }
+  return 0;
+}
+
+AudioMixer::Source::AudioFrameInfo ChannelReceive::GetAudioFrameWithInfo(
+    int sample_rate_hz,
+    AudioFrame* audio_frame) {
+  audio_frame->sample_rate_hz_ = sample_rate_hz;
+
+  unsigned int ssrc;
+  RTC_CHECK_EQ(GetRemoteSSRC(ssrc), 0);
+  event_log_->Log(absl::make_unique<RtcEventAudioPlayout>(ssrc));
+  // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
+  bool muted;
+  if (audio_coding_->PlayoutData10Ms(audio_frame->sample_rate_hz_, audio_frame,
+                                     &muted) == -1) {
+    RTC_DLOG(LS_ERROR)
+        << "ChannelReceive::GetAudioFrame() PlayoutData10Ms() failed!";
+    // In all likelihood, the audio in this frame is garbage. We return an
+    // error so that the audio mixer module doesn't add it to the mix. As
+    // a result, it won't be played out and the actions skipped here are
+    // irrelevant.
+    return AudioMixer::Source::AudioFrameInfo::kError;
+  }
+
+  if (muted) {
+    // TODO(henrik.lundin): We should be able to do better than this. But we
+    // will have to go through all the cases below where the audio samples may
+    // be used, and handle the muted case in some way.
+    AudioFrameOperations::Mute(audio_frame);
+  }
+
+  {
+    // Pass the audio buffers to an optional sink callback, before applying
+    // scaling/panning, as that applies to the mix operation.
+    // External recipients of the audio (e.g. via AudioTrack), will do their
+    // own mixing/dynamic processing.
+    rtc::CritScope cs(&_callbackCritSect);
+    if (audio_sink_) {
+      AudioSinkInterface::Data data(
+          audio_frame->data(), audio_frame->samples_per_channel_,
+          audio_frame->sample_rate_hz_, audio_frame->num_channels_,
+          audio_frame->timestamp_);
+      audio_sink_->OnData(data);
+    }
+  }
+
+  float output_gain = 1.0f;
+  {
+    rtc::CritScope cs(&volume_settings_critsect_);
+    output_gain = _outputGain;
+  }
+
+  // Output volume scaling
+  if (output_gain < 0.99f || output_gain > 1.01f) {
+    // TODO(solenberg): Combine with mute state - this can cause clicks!
+    AudioFrameOperations::ScaleWithSat(output_gain, audio_frame);
+  }
+
+  // Measure audio level (0-9)
+  // TODO(henrik.lundin) Use the |muted| information here too.
+  // TODO(deadbeef): Use RmsLevel for |_outputAudioLevel| (see
+  // https://crbug.com/webrtc/7517).
+  _outputAudioLevel.ComputeLevel(*audio_frame, kAudioSampleDurationSeconds);
+
+  if (capture_start_rtp_time_stamp_ < 0 && audio_frame->timestamp_ != 0) {
+    // The first frame with a valid rtp timestamp.
+    capture_start_rtp_time_stamp_ = audio_frame->timestamp_;
+  }
+
+  if (capture_start_rtp_time_stamp_ >= 0) {
+    // audio_frame.timestamp_ should be valid from now on.
+
+    // Compute elapsed time.
+    int64_t unwrap_timestamp =
+        rtp_ts_wraparound_handler_->Unwrap(audio_frame->timestamp_);
+    audio_frame->elapsed_time_ms_ =
+        (unwrap_timestamp - capture_start_rtp_time_stamp_) /
+        (GetRtpTimestampRateHz() / 1000);
+
+    {
+      rtc::CritScope lock(&ts_stats_lock_);
+      // Compute ntp time.
+      audio_frame->ntp_time_ms_ =
+          ntp_estimator_.Estimate(audio_frame->timestamp_);
+      // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
+      if (audio_frame->ntp_time_ms_ > 0) {
+        // Compute |capture_start_ntp_time_ms_| so that
+        // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
+        capture_start_ntp_time_ms_ =
+            audio_frame->ntp_time_ms_ - audio_frame->elapsed_time_ms_;
+      }
+    }
+  }
+
+  {
+    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.TargetJitterBufferDelayMs",
+                              audio_coding_->TargetDelayMs());
+    const int jitter_buffer_delay = audio_coding_->FilteredCurrentDelayMs();
+    rtc::CritScope lock(&video_sync_lock_);
+    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDelayEstimateMs",
+                              jitter_buffer_delay + playout_delay_ms_);
+    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverJitterBufferDelayMs",
+                              jitter_buffer_delay);
+    RTC_HISTOGRAM_COUNTS_1000("WebRTC.Audio.ReceiverDeviceDelayMs",
+                              playout_delay_ms_);
+  }
+
+  return muted ? AudioMixer::Source::AudioFrameInfo::kMuted
+               : AudioMixer::Source::AudioFrameInfo::kNormal;
+}
+
+int ChannelReceive::PreferredSampleRate() const {
+  // Return the bigger of playout and receive frequency in the ACM.
+  return std::max(audio_coding_->ReceiveFrequency(),
+                  audio_coding_->PlayoutFrequency());
+}
+
+ChannelReceive::ChannelReceive(
+    ProcessThread* module_process_thread,
+    AudioDeviceModule* audio_device_module,
+    Transport* rtcp_send_transport,
+    RtcEventLog* rtc_event_log,
+    uint32_t remote_ssrc,
+    size_t jitter_buffer_max_packets,
+    bool jitter_buffer_fast_playout,
+    rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
+    absl::optional<AudioCodecPairId> codec_pair_id,
+    FrameDecryptorInterface* frame_decryptor)
+    : event_log_(rtc_event_log),
+      rtp_receive_statistics_(
+          ReceiveStatistics::Create(Clock::GetRealTimeClock())),
+      remote_ssrc_(remote_ssrc),
+      _outputAudioLevel(),
+      ntp_estimator_(Clock::GetRealTimeClock()),
+      playout_timestamp_rtp_(0),
+      playout_delay_ms_(0),
+      rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
+      capture_start_rtp_time_stamp_(-1),
+      capture_start_ntp_time_ms_(-1),
+      _moduleProcessThreadPtr(module_process_thread),
+      _audioDeviceModulePtr(audio_device_module),
+      _outputGain(1.0f),
+      associated_send_channel_(nullptr),
+      frame_decryptor_(frame_decryptor) {
+  RTC_DCHECK(module_process_thread);
+  RTC_DCHECK(audio_device_module);
+  AudioCodingModule::Config acm_config;
+  acm_config.decoder_factory = decoder_factory;
+  acm_config.neteq_config.codec_pair_id = codec_pair_id;
+  acm_config.neteq_config.max_packets_in_buffer = jitter_buffer_max_packets;
+  acm_config.neteq_config.enable_fast_accelerate = jitter_buffer_fast_playout;
+  acm_config.neteq_config.enable_muted_state = true;
+  audio_coding_.reset(AudioCodingModule::Create(acm_config));
+
+  _outputAudioLevel.Clear();
+
+  rtp_receive_statistics_->EnableRetransmitDetection(remote_ssrc_, true);
+  RtpRtcp::Configuration configuration;
+  configuration.audio = true;
+  // TODO(nisse): Also set receiver_only = true, but that seems to break RTT
+  // estimation, resulting in test failures for
+  // PeerConnectionIntegrationTest.GetCaptureStartNtpTimeWithOldStatsApi
+  configuration.outgoing_transport = rtcp_send_transport;
+  configuration.receive_statistics = rtp_receive_statistics_.get();
+
+  configuration.event_log = event_log_;
+
+  _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
+  _rtpRtcpModule->SetSendingMediaStatus(false);
+  _rtpRtcpModule->SetRemoteSSRC(remote_ssrc_);
+  Init();
+}
+
+ChannelReceive::~ChannelReceive() {
+  Terminate();
+  RTC_DCHECK(!channel_state_.Get().playing);
+}
+
+void ChannelReceive::Init() {
+  channel_state_.Reset();
+
+  // --- Add modules to process thread (for periodic schedulation)
+  _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
+
+  // --- ACM initialization
+  int error = audio_coding_->InitializeReceiver();
+  RTC_DCHECK_EQ(0, error);
+
+  // --- RTP/RTCP module initialization
+
+  // Ensure that RTCP is enabled by default for the created channel.
+  // Note that, the module will keep generating RTCP until it is explicitly
+  // disabled by the user.
+  // After StopListen (when no sockets exists), RTCP packets will no longer
+  // be transmitted since the Transport object will then be invalid.
+  // RTCP is enabled by default.
+  _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
+}
+
+void ChannelReceive::Terminate() {
+  RTC_DCHECK(construction_thread_.CalledOnValidThread());
+  // Must be called on the same thread as Init().
+  rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
+
+  StopPlayout();
+
+  // The order to safely shutdown modules in a channel is:
+  // 1. De-register callbacks in modules
+  // 2. De-register modules in process thread
+  // 3. Destroy modules
+  int error = audio_coding_->RegisterTransportCallback(NULL);
+  RTC_DCHECK_EQ(0, error);
+
+  // De-register modules in process thread
+  if (_moduleProcessThreadPtr)
+    _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
+
+  // End of modules shutdown
+}
+
+void ChannelReceive::SetSink(AudioSinkInterface* sink) {
+  rtc::CritScope cs(&_callbackCritSect);
+  audio_sink_ = sink;
+}
+
+int32_t ChannelReceive::StartPlayout() {
+  if (channel_state_.Get().playing) {
+    return 0;
+  }
+
+  channel_state_.SetPlaying(true);
+
+  return 0;
+}
+
+int32_t ChannelReceive::StopPlayout() {
+  if (!channel_state_.Get().playing) {
+    return 0;
+  }
+
+  channel_state_.SetPlaying(false);
+  _outputAudioLevel.Clear();
+
+  return 0;
+}
+
+int32_t ChannelReceive::GetRecCodec(CodecInst& codec) {
+  return (audio_coding_->ReceiveCodec(&codec));
+}
+
+std::vector<webrtc::RtpSource> ChannelReceive::GetSources() const {
+  int64_t now_ms = rtc::TimeMillis();
+  std::vector<RtpSource> sources;
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    sources = contributing_sources_.GetSources(now_ms);
+    if (last_received_rtp_system_time_ms_ >=
+        now_ms - ContributingSources::kHistoryMs) {
+      sources.emplace_back(*last_received_rtp_system_time_ms_, remote_ssrc_,
+                           RtpSourceType::SSRC);
+      sources.back().set_audio_level(last_received_rtp_audio_level_);
+    }
+  }
+  return sources;
+}
+
+void ChannelReceive::SetReceiveCodecs(
+    const std::map<int, SdpAudioFormat>& codecs) {
+  for (const auto& kv : codecs) {
+    RTC_DCHECK_GE(kv.second.clockrate_hz, 1000);
+    payload_type_frequencies_[kv.first] = kv.second.clockrate_hz;
+  }
+  audio_coding_->SetReceiveCodecs(codecs);
+}
+
+// TODO(nisse): Move receive logic up to AudioReceiveStream.
+void ChannelReceive::OnRtpPacket(const RtpPacketReceived& packet) {
+  int64_t now_ms = rtc::TimeMillis();
+  uint8_t audio_level;
+  bool voice_activity;
+  bool has_audio_level =
+      packet.GetExtension<::webrtc::AudioLevel>(&voice_activity, &audio_level);
+
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    last_received_rtp_timestamp_ = packet.Timestamp();
+    last_received_rtp_system_time_ms_ = now_ms;
+    if (has_audio_level)
+      last_received_rtp_audio_level_ = audio_level;
+    std::vector<uint32_t> csrcs = packet.Csrcs();
+    contributing_sources_.Update(now_ms, csrcs);
+  }
+
+  // Store playout timestamp for the received RTP packet
+  UpdatePlayoutTimestamp(false);
+
+  const auto& it = payload_type_frequencies_.find(packet.PayloadType());
+  if (it == payload_type_frequencies_.end())
+    return;
+  // TODO(nisse): Set payload_type_frequency earlier, when packet is parsed.
+  RtpPacketReceived packet_copy(packet);
+  packet_copy.set_payload_type_frequency(it->second);
+
+  rtp_receive_statistics_->OnRtpPacket(packet_copy);
+
+  RTPHeader header;
+  packet_copy.GetHeader(&header);
+
+  ReceivePacket(packet_copy.data(), packet_copy.size(), header);
+}
+
+bool ChannelReceive::ReceivePacket(const uint8_t* packet,
+                                   size_t packet_length,
+                                   const RTPHeader& header) {
+  const uint8_t* payload = packet + header.headerLength;
+  assert(packet_length >= header.headerLength);
+  size_t payload_length = packet_length - header.headerLength;
+  WebRtcRTPHeader webrtc_rtp_header = {};
+  webrtc_rtp_header.header = header;
+
+  size_t payload_data_length = payload_length - header.paddingLength;
+
+  // E2EE Custom Audio Frame Decryption (This is optional).
+  // Keep this buffer around for the lifetime of the OnReceivedPayloadData call.
+  rtc::Buffer decrypted_audio_payload;
+  if (frame_decryptor_ != nullptr) {
+    size_t max_plaintext_size = frame_decryptor_->GetMaxPlaintextByteSize(
+        cricket::MEDIA_TYPE_AUDIO, payload_length);
+    decrypted_audio_payload.SetSize(max_plaintext_size);
+
+    size_t bytes_written = 0;
+    std::vector<uint32_t> csrcs(header.arrOfCSRCs,
+                                header.arrOfCSRCs + header.numCSRCs);
+    int decrypt_status = frame_decryptor_->Decrypt(
+        cricket::MEDIA_TYPE_AUDIO, csrcs,
+        /*additional_data=*/nullptr,
+        rtc::ArrayView<const uint8_t>(payload, payload_data_length),
+        decrypted_audio_payload, &bytes_written);
+
+    // In this case just interpret the failure as a silent frame.
+    if (decrypt_status != 0) {
+      bytes_written = 0;
+    }
+
+    // Resize the decrypted audio payload to the number of bytes actually
+    // written.
+    decrypted_audio_payload.SetSize(bytes_written);
+    // Update the final payload.
+    payload = decrypted_audio_payload.data();
+    payload_data_length = decrypted_audio_payload.size();
+  }
+
+  if (payload_data_length == 0) {
+    webrtc_rtp_header.frameType = kEmptyFrame;
+    return OnReceivedPayloadData(nullptr, 0, &webrtc_rtp_header);
+  }
+  return OnReceivedPayloadData(payload, payload_data_length,
+                               &webrtc_rtp_header);
+}
+
+int32_t ChannelReceive::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
+  // Store playout timestamp for the received RTCP packet
+  UpdatePlayoutTimestamp(true);
+
+  // Deliver RTCP packet to RTP/RTCP module for parsing
+  _rtpRtcpModule->IncomingRtcpPacket(data, length);
+
+  int64_t rtt = GetRTT();
+  if (rtt == 0) {
+    // Waiting for valid RTT.
+    return 0;
+  }
+
+  int64_t nack_window_ms = rtt;
+  if (nack_window_ms < kMinRetransmissionWindowMs) {
+    nack_window_ms = kMinRetransmissionWindowMs;
+  } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
+    nack_window_ms = kMaxRetransmissionWindowMs;
+  }
+
+  uint32_t ntp_secs = 0;
+  uint32_t ntp_frac = 0;
+  uint32_t rtp_timestamp = 0;
+  if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
+                                     &rtp_timestamp)) {
+    // Waiting for RTCP.
+    return 0;
+  }
+
+  {
+    rtc::CritScope lock(&ts_stats_lock_);
+    ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
+  }
+  return 0;
+}
+
+int ChannelReceive::GetSpeechOutputLevelFullRange() const {
+  return _outputAudioLevel.LevelFullRange();
+}
+
+double ChannelReceive::GetTotalOutputEnergy() const {
+  return _outputAudioLevel.TotalEnergy();
+}
+
+double ChannelReceive::GetTotalOutputDuration() const {
+  return _outputAudioLevel.TotalDuration();
+}
+
+void ChannelReceive::SetChannelOutputVolumeScaling(float scaling) {
+  rtc::CritScope cs(&volume_settings_critsect_);
+  _outputGain = scaling;
+}
+
+int ChannelReceive::SetLocalSSRC(unsigned int ssrc) {
+  _rtpRtcpModule->SetSSRC(ssrc);
+  return 0;
+}
+
+// TODO(nisse): Pass ssrc in return value instead.
+int ChannelReceive::GetRemoteSSRC(unsigned int& ssrc) {
+  ssrc = remote_ssrc_;
+  return 0;
+}
+
+void ChannelReceive::RegisterReceiverCongestionControlObjects(
+    PacketRouter* packet_router) {
+  RTC_DCHECK(packet_router);
+  RTC_DCHECK(!packet_router_);
+  constexpr bool remb_candidate = false;
+  packet_router->AddReceiveRtpModule(_rtpRtcpModule.get(), remb_candidate);
+  packet_router_ = packet_router;
+}
+
+void ChannelReceive::ResetReceiverCongestionControlObjects() {
+  RTC_DCHECK(packet_router_);
+  packet_router_->RemoveReceiveRtpModule(_rtpRtcpModule.get());
+  packet_router_ = nullptr;
+}
+
+int ChannelReceive::GetRTPStatistics(CallReceiveStatistics& stats) {
+  // --- RtcpStatistics
+
+  // The jitter statistics is updated for each received RTP packet and is
+  // based on received packets.
+  RtcpStatistics statistics;
+  StreamStatistician* statistician =
+      rtp_receive_statistics_->GetStatistician(remote_ssrc_);
+  if (statistician) {
+    statistician->GetStatistics(&statistics,
+                                _rtpRtcpModule->RTCP() == RtcpMode::kOff);
+  }
+
+  stats.fractionLost = statistics.fraction_lost;
+  stats.cumulativeLost = statistics.packets_lost;
+  stats.extendedMax = statistics.extended_highest_sequence_number;
+  stats.jitterSamples = statistics.jitter;
+
+  // --- RTT
+  stats.rttMs = GetRTT();
+
+  // --- Data counters
+
+  size_t bytesReceived(0);
+  uint32_t packetsReceived(0);
+
+  if (statistician) {
+    statistician->GetDataCounters(&bytesReceived, &packetsReceived);
+  }
+
+  stats.bytesReceived = bytesReceived;
+  stats.packetsReceived = packetsReceived;
+
+  // --- Timestamps
+  {
+    rtc::CritScope lock(&ts_stats_lock_);
+    stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
+  }
+  return 0;
+}
+
+void ChannelReceive::SetNACKStatus(bool enable, int maxNumberOfPackets) {
+  // None of these functions can fail.
+  rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
+  if (enable)
+    audio_coding_->EnableNack(maxNumberOfPackets);
+  else
+    audio_coding_->DisableNack();
+}
+
+// Called when we are missing one or more packets.
+int ChannelReceive::ResendPackets(const uint16_t* sequence_numbers,
+                                  int length) {
+  return _rtpRtcpModule->SendNACK(sequence_numbers, length);
+}
+
+void ChannelReceive::SetAssociatedSendChannel(ChannelSend* channel) {
+  rtc::CritScope lock(&assoc_send_channel_lock_);
+  associated_send_channel_ = channel;
+}
+
+int ChannelReceive::GetNetworkStatistics(NetworkStatistics& stats) {
+  return audio_coding_->GetNetworkStatistics(&stats);
+}
+
+void ChannelReceive::GetDecodingCallStatistics(
+    AudioDecodingCallStats* stats) const {
+  audio_coding_->GetDecodingCallStatistics(stats);
+}
+
+uint32_t ChannelReceive::GetDelayEstimate() const {
+  rtc::CritScope lock(&video_sync_lock_);
+  return audio_coding_->FilteredCurrentDelayMs() + playout_delay_ms_;
+}
+
+int ChannelReceive::SetMinimumPlayoutDelay(int delayMs) {
+  if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
+      (delayMs > kVoiceEngineMaxMinPlayoutDelayMs)) {
+    RTC_DLOG(LS_ERROR) << "SetMinimumPlayoutDelay() invalid min delay";
+    return -1;
+  }
+  if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0) {
+    RTC_DLOG(LS_ERROR)
+        << "SetMinimumPlayoutDelay() failed to set min playout delay";
+    return -1;
+  }
+  return 0;
+}
+
+int ChannelReceive::GetPlayoutTimestamp(unsigned int& timestamp) {
+  uint32_t playout_timestamp_rtp = 0;
+  {
+    rtc::CritScope lock(&video_sync_lock_);
+    playout_timestamp_rtp = playout_timestamp_rtp_;
+  }
+  if (playout_timestamp_rtp == 0) {
+    RTC_DLOG(LS_ERROR) << "GetPlayoutTimestamp() failed to retrieve timestamp";
+    return -1;
+  }
+  timestamp = playout_timestamp_rtp;
+  return 0;
+}
+
+absl::optional<Syncable::Info> ChannelReceive::GetSyncInfo() const {
+  Syncable::Info info;
+  if (_rtpRtcpModule->RemoteNTP(&info.capture_time_ntp_secs,
+                                &info.capture_time_ntp_frac, nullptr, nullptr,
+                                &info.capture_time_source_clock) != 0) {
+    return absl::nullopt;
+  }
+  {
+    rtc::CritScope cs(&rtp_sources_lock_);
+    if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
+      return absl::nullopt;
+    }
+    info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
+    info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
+  }
+  return info;
+}
+
+void ChannelReceive::UpdatePlayoutTimestamp(bool rtcp) {
+  jitter_buffer_playout_timestamp_ = audio_coding_->PlayoutTimestamp();
+
+  if (!jitter_buffer_playout_timestamp_) {
+    // This can happen if this channel has not received any RTP packets. In
+    // this case, NetEq is not capable of computing a playout timestamp.
+    return;
+  }
+
+  uint16_t delay_ms = 0;
+  if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
+    RTC_DLOG(LS_WARNING)
+        << "ChannelReceive::UpdatePlayoutTimestamp() failed to read"
+        << " playout delay from the ADM";
+    return;
+  }
+
+  RTC_DCHECK(jitter_buffer_playout_timestamp_);
+  uint32_t playout_timestamp = *jitter_buffer_playout_timestamp_;
+
+  // Remove the playout delay.
+  playout_timestamp -= (delay_ms * (GetRtpTimestampRateHz() / 1000));
+
+  {
+    rtc::CritScope lock(&video_sync_lock_);
+    if (!rtcp) {
+      playout_timestamp_rtp_ = playout_timestamp;
+    }
+    playout_delay_ms_ = delay_ms;
+  }
+}
+
+int ChannelReceive::GetRtpTimestampRateHz() const {
+  const auto format = audio_coding_->ReceiveFormat();
+  // Default to the playout frequency if we've not gotten any packets yet.
+  // TODO(ossu): Zero clockrate can only happen if we've added an external
+  // decoder for a format we don't support internally. Remove once that way of
+  // adding decoders is gone!
+  return (format && format->clockrate_hz != 0)
+             ? format->clockrate_hz
+             : audio_coding_->PlayoutFrequency();
+}
+
+int64_t ChannelReceive::GetRTT() const {
+  RtcpMode method = _rtpRtcpModule->RTCP();
+  if (method == RtcpMode::kOff) {
+    return 0;
+  }
+  std::vector<RTCPReportBlock> report_blocks;
+  _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
+
+  // TODO(nisse): Could we check the return value from the ->RTT() call below,
+  // instead of checking if we have any report blocks?
+  if (report_blocks.empty()) {
+    rtc::CritScope lock(&assoc_send_channel_lock_);
+    // Tries to get RTT from an associated channel.
+    if (!associated_send_channel_) {
+      return 0;
+    }
+    return associated_send_channel_->GetRTT();
+  }
+
+  int64_t rtt = 0;
+  int64_t avg_rtt = 0;
+  int64_t max_rtt = 0;
+  int64_t min_rtt = 0;
+  if (_rtpRtcpModule->RTT(remote_ssrc_, &rtt, &avg_rtt, &min_rtt, &max_rtt) !=
+      0) {
+    return 0;
+  }
+  return rtt;
+}
+
+}  // namespace voe
+}  // namespace webrtc
diff --git a/audio/channel_receive.h b/audio/channel_receive.h
new file mode 100644
index 0000000..82eb4df
--- /dev/null
+++ b/audio/channel_receive.h
@@ -0,0 +1,267 @@
+/*
+ *  Copyright (c) 2012 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 AUDIO_CHANNEL_RECEIVE_H_
+#define AUDIO_CHANNEL_RECEIVE_H_
+
+#include <map>
+#include <memory>
+#include <vector>
+
+#include "absl/types/optional.h"
+#include "api/audio/audio_mixer.h"
+#include "api/call/audio_sink.h"
+#include "api/call/transport.h"
+#include "api/rtpreceiverinterface.h"
+#include "audio/audio_level.h"
+#include "call/syncable.h"
+#include "common_types.h"  // NOLINT(build/include)
+#include "modules/audio_coding/include/audio_coding_module.h"
+#include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
+#include "modules/rtp_rtcp/include/rtp_header_parser.h"
+#include "modules/rtp_rtcp/include/rtp_rtcp.h"
+#include "modules/rtp_rtcp/source/contributing_sources.h"
+#include "rtc_base/criticalsection.h"
+#include "rtc_base/thread_checker.h"
+
+// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
+// warnings about use of unsigned short, and non-const reference arguments.
+// These need cleanup, in a separate cl.
+
+namespace rtc {
+class TimestampWrapAroundHandler;
+}
+
+namespace webrtc {
+
+class AudioDeviceModule;
+class FrameDecryptorInterface;
+class PacketRouter;
+class ProcessThread;
+class RateLimiter;
+class ReceiveStatistics;
+class RtcEventLog;
+class RtpPacketReceived;
+class RtpRtcp;
+
+struct CallReceiveStatistics {
+  unsigned short fractionLost;  // NOLINT
+  unsigned int cumulativeLost;
+  unsigned int extendedMax;
+  unsigned int jitterSamples;
+  int64_t rttMs;
+  size_t bytesReceived;
+  int packetsReceived;
+  // The capture ntp time (in local timebase) of the first played out audio
+  // frame.
+  int64_t capture_start_ntp_time_ms_;
+};
+
+namespace voe {
+
+class ChannelSend;
+
+// Helper class to simplify locking scheme for members that are accessed from
+// multiple threads.
+// Example: a member can be set on thread T1 and read by an internal audio
+// thread T2. Accessing the member via this class ensures that we are
+// safe and also avoid TSan v2 warnings.
+class ChannelReceiveState {
+ public:
+  struct State {
+    bool playing = false;
+  };
+
+  ChannelReceiveState() {}
+  virtual ~ChannelReceiveState() {}
+
+  void Reset() {
+    rtc::CritScope lock(&lock_);
+    state_ = State();
+  }
+
+  State Get() const {
+    rtc::CritScope lock(&lock_);
+    return state_;
+  }
+
+  void SetPlaying(bool enable) {
+    rtc::CritScope lock(&lock_);
+    state_.playing = enable;
+  }
+
+ private:
+  rtc::CriticalSection lock_;
+  State state_;
+};
+
+class ChannelReceive : public RtpData {
+ public:
+  // Used for receive streams.
+  ChannelReceive(ProcessThread* module_process_thread,
+                 AudioDeviceModule* audio_device_module,
+                 Transport* rtcp_send_transport,
+                 RtcEventLog* rtc_event_log,
+                 uint32_t remote_ssrc,
+                 size_t jitter_buffer_max_packets,
+                 bool jitter_buffer_fast_playout,
+                 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
+                 absl::optional<AudioCodecPairId> codec_pair_id,
+                 FrameDecryptorInterface* frame_decryptor);
+  virtual ~ChannelReceive();
+
+  void SetSink(AudioSinkInterface* sink);
+
+  void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
+
+  // API methods
+
+  // VoEBase
+  int32_t StartPlayout();
+  int32_t StopPlayout();
+
+  // Codecs
+  int32_t GetRecCodec(CodecInst& codec);  // NOLINT
+
+  // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
+  int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
+  void OnRtpPacket(const RtpPacketReceived& packet);
+
+  // Muting, Volume and Level.
+  void SetChannelOutputVolumeScaling(float scaling);
+  int GetSpeechOutputLevelFullRange() const;
+  // See description of "totalAudioEnergy" in the WebRTC stats spec:
+  // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
+  double GetTotalOutputEnergy() const;
+  double GetTotalOutputDuration() const;
+
+  // Stats.
+  int GetNetworkStatistics(NetworkStatistics& stats);  // NOLINT
+  void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
+
+  // Audio+Video Sync.
+  uint32_t GetDelayEstimate() const;
+  int SetMinimumPlayoutDelay(int delayMs);
+  int GetPlayoutTimestamp(unsigned int& timestamp);  // NOLINT
+
+  // Produces the transport-related timestamps; current_delay_ms is left unset.
+  absl::optional<Syncable::Info> GetSyncInfo() const;
+
+  // RTP+RTCP
+  int SetLocalSSRC(unsigned int ssrc);
+
+  void RegisterReceiverCongestionControlObjects(PacketRouter* packet_router);
+  void ResetReceiverCongestionControlObjects();
+
+  int GetRTPStatistics(CallReceiveStatistics& stats);  // NOLINT
+  void SetNACKStatus(bool enable, int maxNumberOfPackets);
+
+  // From RtpData in the RTP/RTCP module
+  int32_t OnReceivedPayloadData(const uint8_t* payloadData,
+                                size_t payloadSize,
+                                const WebRtcRTPHeader* rtpHeader) override;
+
+  // From AudioMixer::Source.
+  AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
+      int sample_rate_hz,
+      AudioFrame* audio_frame);
+
+  int PreferredSampleRate() const;
+
+  // Associate to a send channel.
+  // Used for obtaining RTT for a receive-only channel.
+  void SetAssociatedSendChannel(ChannelSend* channel);
+
+  std::vector<RtpSource> GetSources() const;
+
+ private:
+  void Init();
+  void Terminate();
+
+  int GetRemoteSSRC(unsigned int& ssrc);  // NOLINT
+
+  bool ReceivePacket(const uint8_t* packet,
+                     size_t packet_length,
+                     const RTPHeader& header);
+  int ResendPackets(const uint16_t* sequence_numbers, int length);
+  void UpdatePlayoutTimestamp(bool rtcp);
+
+  int GetRtpTimestampRateHz() const;
+  int64_t GetRTT() const;
+
+  rtc::CriticalSection _callbackCritSect;
+  rtc::CriticalSection volume_settings_critsect_;
+
+  ChannelReceiveState channel_state_;
+
+  RtcEventLog* const event_log_;
+
+  // Indexed by payload type.
+  std::map<uint8_t, int> payload_type_frequencies_;
+
+  std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
+  std::unique_ptr<RtpRtcp> _rtpRtcpModule;
+  const uint32_t remote_ssrc_;
+
+  // Info for GetSources and GetSyncInfo is updated on network or worker thread,
+  // queried on the worker thread.
+  rtc::CriticalSection rtp_sources_lock_;
+  ContributingSources contributing_sources_ RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<uint32_t> last_received_rtp_timestamp_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<int64_t> last_received_rtp_system_time_ms_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+  absl::optional<uint8_t> last_received_rtp_audio_level_
+      RTC_GUARDED_BY(&rtp_sources_lock_);
+
+  std::unique_ptr<AudioCodingModule> audio_coding_;
+  AudioSinkInterface* audio_sink_ = nullptr;
+  AudioLevel _outputAudioLevel;
+
+  RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(ts_stats_lock_);
+
+  // Timestamp of the audio pulled from NetEq.
+  absl::optional<uint32_t> jitter_buffer_playout_timestamp_;
+
+  rtc::CriticalSection video_sync_lock_;
+  uint32_t playout_timestamp_rtp_ RTC_GUARDED_BY(video_sync_lock_);
+  uint32_t playout_delay_ms_ RTC_GUARDED_BY(video_sync_lock_);
+
+  rtc::CriticalSection ts_stats_lock_;
+
+  std::unique_ptr<rtc::TimestampWrapAroundHandler> rtp_ts_wraparound_handler_;
+  // The rtp timestamp of the first played out audio frame.
+  int64_t capture_start_rtp_time_stamp_;
+  // The capture ntp time (in local timebase) of the first played out audio
+  // frame.
+  int64_t capture_start_ntp_time_ms_ RTC_GUARDED_BY(ts_stats_lock_);
+
+  // uses
+  ProcessThread* _moduleProcessThreadPtr;
+  AudioDeviceModule* _audioDeviceModulePtr;
+  float _outputGain RTC_GUARDED_BY(volume_settings_critsect_);
+
+  // An associated send channel.
+  rtc::CriticalSection assoc_send_channel_lock_;
+  ChannelSend* associated_send_channel_
+      RTC_GUARDED_BY(assoc_send_channel_lock_);
+
+  PacketRouter* packet_router_ = nullptr;
+
+  rtc::ThreadChecker construction_thread_;
+
+  // E2EE Audio Frame Decryption
+  FrameDecryptorInterface* frame_decryptor_ = nullptr;
+};
+
+}  // namespace voe
+}  // namespace webrtc
+
+#endif  // AUDIO_CHANNEL_RECEIVE_H_
diff --git a/audio/channel_receive_proxy.cc b/audio/channel_receive_proxy.cc
new file mode 100644
index 0000000..b1c1c45
--- /dev/null
+++ b/audio/channel_receive_proxy.cc
@@ -0,0 +1,197 @@
+/*
+ *  Copyright (c) 2015 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 "audio/channel_receive_proxy.h"
+
+#include <utility>
+
+#include "api/call/audio_sink.h"
+#include "audio/channel_send_proxy.h"
+#include "call/rtp_transport_controller_send_interface.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/numerics/safe_minmax.h"
+
+namespace webrtc {
+namespace voe {
+ChannelReceiveProxy::ChannelReceiveProxy() {}
+
+ChannelReceiveProxy::ChannelReceiveProxy(
+    std::unique_ptr<ChannelReceive> channel)
+    : channel_(std::move(channel)) {
+  RTC_DCHECK(channel_);
+  module_process_thread_checker_.DetachFromThread();
+}
+
+ChannelReceiveProxy::~ChannelReceiveProxy() {}
+
+void ChannelReceiveProxy::SetLocalSSRC(uint32_t ssrc) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->SetLocalSSRC(ssrc);
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelReceiveProxy::SetNACKStatus(bool enable, int max_packets) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetNACKStatus(enable, max_packets);
+}
+
+CallReceiveStatistics ChannelReceiveProxy::GetRTCPStatistics() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  CallReceiveStatistics stats = {0};
+  int error = channel_->GetRTPStatistics(stats);
+  RTC_DCHECK_EQ(0, error);
+  return stats;
+}
+
+bool ChannelReceiveProxy::ReceivedRTCPPacket(const uint8_t* packet,
+                                             size_t length) {
+  // May be called on either worker thread or network thread.
+  return channel_->ReceivedRTCPPacket(packet, length) == 0;
+}
+
+void ChannelReceiveProxy::RegisterReceiverCongestionControlObjects(
+    PacketRouter* packet_router) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->RegisterReceiverCongestionControlObjects(packet_router);
+}
+
+void ChannelReceiveProxy::ResetReceiverCongestionControlObjects() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->ResetReceiverCongestionControlObjects();
+}
+
+NetworkStatistics ChannelReceiveProxy::GetNetworkStatistics() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  NetworkStatistics stats = {0};
+  int error = channel_->GetNetworkStatistics(stats);
+  RTC_DCHECK_EQ(0, error);
+  return stats;
+}
+
+AudioDecodingCallStats ChannelReceiveProxy::GetDecodingCallStatistics() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  AudioDecodingCallStats stats;
+  channel_->GetDecodingCallStatistics(&stats);
+  return stats;
+}
+
+int ChannelReceiveProxy::GetSpeechOutputLevelFullRange() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetSpeechOutputLevelFullRange();
+}
+
+double ChannelReceiveProxy::GetTotalOutputEnergy() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetTotalOutputEnergy();
+}
+
+double ChannelReceiveProxy::GetTotalOutputDuration() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetTotalOutputDuration();
+}
+
+uint32_t ChannelReceiveProxy::GetDelayEstimate() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
+             module_process_thread_checker_.CalledOnValidThread());
+  return channel_->GetDelayEstimate();
+}
+
+void ChannelReceiveProxy::SetReceiveCodecs(
+    const std::map<int, SdpAudioFormat>& codecs) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetReceiveCodecs(codecs);
+}
+
+void ChannelReceiveProxy::SetSink(AudioSinkInterface* sink) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetSink(sink);
+}
+
+void ChannelReceiveProxy::OnRtpPacket(const RtpPacketReceived& packet) {
+  // May be called on either worker thread or network thread.
+  channel_->OnRtpPacket(packet);
+}
+
+void ChannelReceiveProxy::SetChannelOutputVolumeScaling(float scaling) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetChannelOutputVolumeScaling(scaling);
+}
+
+AudioMixer::Source::AudioFrameInfo ChannelReceiveProxy::GetAudioFrameWithInfo(
+    int sample_rate_hz,
+    AudioFrame* audio_frame) {
+  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
+  return channel_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
+}
+
+int ChannelReceiveProxy::PreferredSampleRate() const {
+  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
+  return channel_->PreferredSampleRate();
+}
+
+void ChannelReceiveProxy::AssociateSendChannel(
+    const ChannelSendProxy& send_channel_proxy) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetAssociatedSendChannel(send_channel_proxy.GetChannel());
+}
+
+void ChannelReceiveProxy::DisassociateSendChannel() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetAssociatedSendChannel(nullptr);
+}
+
+absl::optional<Syncable::Info> ChannelReceiveProxy::GetSyncInfo() const {
+  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
+  return channel_->GetSyncInfo();
+}
+
+uint32_t ChannelReceiveProxy::GetPlayoutTimestamp() const {
+  RTC_DCHECK_RUNS_SERIALIZED(&video_capture_thread_race_checker_);
+  unsigned int timestamp = 0;
+  int error = channel_->GetPlayoutTimestamp(timestamp);
+  RTC_DCHECK(!error || timestamp == 0);
+  return timestamp;
+}
+
+void ChannelReceiveProxy::SetMinimumPlayoutDelay(int delay_ms) {
+  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
+  // Limit to range accepted by both VoE and ACM, so we're at least getting as
+  // close as possible, instead of failing.
+  delay_ms = rtc::SafeClamp(delay_ms, 0, 10000);
+  int error = channel_->SetMinimumPlayoutDelay(delay_ms);
+  if (0 != error) {
+    RTC_LOG(LS_WARNING) << "Error setting minimum playout delay.";
+  }
+}
+
+bool ChannelReceiveProxy::GetRecCodec(CodecInst* codec_inst) const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetRecCodec(*codec_inst) == 0;
+}
+
+std::vector<RtpSource> ChannelReceiveProxy::GetSources() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetSources();
+}
+
+void ChannelReceiveProxy::StartPlayout() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->StartPlayout();
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelReceiveProxy::StopPlayout() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->StopPlayout();
+  RTC_DCHECK_EQ(0, error);
+}
+}  // namespace voe
+}  // namespace webrtc
diff --git a/audio/channel_proxy.h b/audio/channel_receive_proxy.h
similarity index 60%
copy from audio/channel_proxy.h
copy to audio/channel_receive_proxy.h
index f82c1fd..8ebacc3 100644
--- a/audio/channel_proxy.h
+++ b/audio/channel_receive_proxy.h
@@ -8,18 +8,16 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#ifndef AUDIO_CHANNEL_PROXY_H_
-#define AUDIO_CHANNEL_PROXY_H_
+#ifndef AUDIO_CHANNEL_RECEIVE_PROXY_H_
+#define AUDIO_CHANNEL_RECEIVE_PROXY_H_
 
 #include <map>
 #include <memory>
-#include <string>
 #include <vector>
 
 #include "api/audio/audio_mixer.h"
-#include "api/audio_codecs/audio_encoder.h"
 #include "api/rtpreceiverinterface.h"
-#include "audio/channel.h"
+#include "audio/channel_receive.h"
 #include "call/rtp_packet_sink_interface.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/race_checker.h"
@@ -29,95 +27,62 @@
 
 class AudioSinkInterface;
 class PacketRouter;
-class RtcEventLog;
-class RtcpBandwidthObserver;
-class RtcpRttStats;
-class RtpPacketSender;
 class RtpPacketReceived;
-class RtpRtcp;
-class RtpTransportControllerSendInterface;
 class Transport;
-class TransportFeedbackObserver;
 
 namespace voe {
 
+class ChannelSendProxy;
+
 // This class provides the "view" of a voe::Channel that we need to implement
-// webrtc::AudioSendStream and webrtc::AudioReceiveStream. It serves two
-// purposes:
+// webrtc::AudioReceiveStream. It serves two purposes:
 //  1. Allow mocking just the interfaces used, instead of the entire
 //     voe::Channel class.
 //  2. Provide a refined interface for the stream classes, including assumptions
 //     on return values and input adaptation.
-class ChannelProxy : public RtpPacketSinkInterface {
+class ChannelReceiveProxy : public RtpPacketSinkInterface {
  public:
-  ChannelProxy();
-  explicit ChannelProxy(std::unique_ptr<Channel> channel);
-  virtual ~ChannelProxy();
+  ChannelReceiveProxy();
+  explicit ChannelReceiveProxy(std::unique_ptr<ChannelReceive> channel);
+  virtual ~ChannelReceiveProxy();
 
-  virtual bool SetEncoder(int payload_type,
-                          std::unique_ptr<AudioEncoder> encoder);
-  virtual void ModifyEncoder(
-      rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
-
-  virtual void SetRTCPStatus(bool enable);
+  // Shared with ChannelSendProxy
   virtual void SetLocalSSRC(uint32_t ssrc);
-  virtual void SetMid(const std::string& mid, int extension_id);
-  virtual void SetRTCP_CNAME(const std::string& c_name);
   virtual void SetNACKStatus(bool enable, int max_packets);
-  virtual void SetSendAudioLevelIndicationStatus(bool enable, int id);
-  virtual void EnableSendTransportSequenceNumber(int id);
-  virtual void RegisterSenderCongestionControlObjects(
-      RtpTransportControllerSendInterface* transport,
-      RtcpBandwidthObserver* bandwidth_observer);
+  virtual CallReceiveStatistics GetRTCPStatistics() const;
+  virtual bool ReceivedRTCPPacket(const uint8_t* packet, size_t length);
+
   virtual void RegisterReceiverCongestionControlObjects(
       PacketRouter* packet_router);
-  virtual void ResetSenderCongestionControlObjects();
   virtual void ResetReceiverCongestionControlObjects();
-  virtual CallStatistics GetRTCPStatistics() const;
-  virtual std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const;
   virtual NetworkStatistics GetNetworkStatistics() const;
   virtual AudioDecodingCallStats GetDecodingCallStatistics() const;
-  virtual ANAStats GetANAStatistics() const;
   virtual int GetSpeechOutputLevelFullRange() const;
   // See description of "totalAudioEnergy" in the WebRTC stats spec:
   // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
   virtual double GetTotalOutputEnergy() const;
   virtual double GetTotalOutputDuration() const;
   virtual uint32_t GetDelayEstimate() const;
-  virtual bool SetSendTelephoneEventPayloadType(int payload_type,
-                                                int payload_frequency);
-  virtual bool SendTelephoneEventOutband(int event, int duration_ms);
-  virtual void SetBitrate(int bitrate_bps, int64_t probing_interval_ms);
   virtual void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
   virtual void SetSink(AudioSinkInterface* sink);
-  virtual void SetInputMute(bool muted);
-  virtual void RegisterTransport(Transport* transport);
 
   // Implements RtpPacketSinkInterface
   void OnRtpPacket(const RtpPacketReceived& packet) override;
-  virtual bool ReceivedRTCPPacket(const uint8_t* packet, size_t length);
+
   virtual void SetChannelOutputVolumeScaling(float scaling);
   virtual AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
       int sample_rate_hz,
       AudioFrame* audio_frame);
   virtual int PreferredSampleRate() const;
-  virtual void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
-  virtual void SetTransportOverhead(int transport_overhead_per_packet);
-  virtual void AssociateSendChannel(const ChannelProxy& send_channel_proxy);
+  virtual void AssociateSendChannel(const ChannelSendProxy& send_channel_proxy);
   virtual void DisassociateSendChannel();
-  virtual RtpRtcp* GetRtpRtcp() const;
 
   // Produces the transport-related timestamps; current_delay_ms is left unset.
   absl::optional<Syncable::Info> GetSyncInfo() const;
   virtual uint32_t GetPlayoutTimestamp() const;
   virtual void SetMinimumPlayoutDelay(int delay_ms);
   virtual bool GetRecCodec(CodecInst* codec_inst) const;
-  virtual void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
-  virtual void OnRecoverableUplinkPacketLossRate(
-      float recoverable_packet_loss_rate);
   virtual std::vector<webrtc::RtpSource> GetSources() const;
-  virtual void StartSend();
-  virtual void StopSend();
   virtual void StartPlayout();
   virtual void StopPlayout();
 
@@ -134,11 +99,11 @@
   // audio thread to another, but access is still sequential.
   rtc::RaceChecker audio_thread_race_checker_;
   rtc::RaceChecker video_capture_thread_race_checker_;
-  std::unique_ptr<Channel> channel_;
+  std::unique_ptr<ChannelReceive> channel_;
 
-  RTC_DISALLOW_COPY_AND_ASSIGN(ChannelProxy);
+  RTC_DISALLOW_COPY_AND_ASSIGN(ChannelReceiveProxy);
 };
 }  // namespace voe
 }  // namespace webrtc
 
-#endif  // AUDIO_CHANNEL_PROXY_H_
+#endif  // AUDIO_CHANNEL_RECEIVE_PROXY_H_
diff --git a/audio/channel_send.cc b/audio/channel_send.cc
new file mode 100644
index 0000000..8639fbd
--- /dev/null
+++ b/audio/channel_send.cc
@@ -0,0 +1,997 @@
+/*
+ *  Copyright (c) 2012 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 "audio/channel_send.h"
+
+#include <algorithm>
+#include <map>
+#include <memory>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include "absl/memory/memory.h"
+#include "api/array_view.h"
+#include "api/crypto/frameencryptorinterface.h"
+#include "audio/utility/audio_frame_operations.h"
+#include "call/rtp_transport_controller_send_interface.h"
+#include "logging/rtc_event_log/events/rtc_event_audio_playout.h"
+#include "logging/rtc_event_log/rtc_event_log.h"
+#include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h"
+#include "modules/pacing/packet_router.h"
+#include "modules/utility/include/process_thread.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/criticalsection.h"
+#include "rtc_base/event.h"
+#include "rtc_base/format_macros.h"
+#include "rtc_base/location.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/rate_limiter.h"
+#include "rtc_base/task_queue.h"
+#include "rtc_base/thread_checker.h"
+#include "rtc_base/timeutils.h"
+#include "system_wrappers/include/field_trial.h"
+#include "system_wrappers/include/metrics.h"
+
+namespace webrtc {
+namespace voe {
+
+namespace {
+
+constexpr int64_t kMaxRetransmissionWindowMs = 1000;
+constexpr int64_t kMinRetransmissionWindowMs = 30;
+
+}  // namespace
+
+const int kTelephoneEventAttenuationdB = 10;
+
+class TransportFeedbackProxy : public TransportFeedbackObserver {
+ public:
+  TransportFeedbackProxy() : feedback_observer_(nullptr) {
+    pacer_thread_.DetachFromThread();
+    network_thread_.DetachFromThread();
+  }
+
+  void SetTransportFeedbackObserver(
+      TransportFeedbackObserver* feedback_observer) {
+    RTC_DCHECK(thread_checker_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    feedback_observer_ = feedback_observer;
+  }
+
+  // Implements TransportFeedbackObserver.
+  void AddPacket(uint32_t ssrc,
+                 uint16_t sequence_number,
+                 size_t length,
+                 const PacedPacketInfo& pacing_info) override {
+    RTC_DCHECK(pacer_thread_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    if (feedback_observer_)
+      feedback_observer_->AddPacket(ssrc, sequence_number, length, pacing_info);
+  }
+
+  void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override {
+    RTC_DCHECK(network_thread_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    if (feedback_observer_)
+      feedback_observer_->OnTransportFeedback(feedback);
+  }
+
+ private:
+  rtc::CriticalSection crit_;
+  rtc::ThreadChecker thread_checker_;
+  rtc::ThreadChecker pacer_thread_;
+  rtc::ThreadChecker network_thread_;
+  TransportFeedbackObserver* feedback_observer_ RTC_GUARDED_BY(&crit_);
+};
+
+class TransportSequenceNumberProxy : public TransportSequenceNumberAllocator {
+ public:
+  TransportSequenceNumberProxy() : seq_num_allocator_(nullptr) {
+    pacer_thread_.DetachFromThread();
+  }
+
+  void SetSequenceNumberAllocator(
+      TransportSequenceNumberAllocator* seq_num_allocator) {
+    RTC_DCHECK(thread_checker_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    seq_num_allocator_ = seq_num_allocator;
+  }
+
+  // Implements TransportSequenceNumberAllocator.
+  uint16_t AllocateSequenceNumber() override {
+    RTC_DCHECK(pacer_thread_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    if (!seq_num_allocator_)
+      return 0;
+    return seq_num_allocator_->AllocateSequenceNumber();
+  }
+
+ private:
+  rtc::CriticalSection crit_;
+  rtc::ThreadChecker thread_checker_;
+  rtc::ThreadChecker pacer_thread_;
+  TransportSequenceNumberAllocator* seq_num_allocator_ RTC_GUARDED_BY(&crit_);
+};
+
+class RtpPacketSenderProxy : public RtpPacketSender {
+ public:
+  RtpPacketSenderProxy() : rtp_packet_sender_(nullptr) {}
+
+  void SetPacketSender(RtpPacketSender* rtp_packet_sender) {
+    RTC_DCHECK(thread_checker_.CalledOnValidThread());
+    rtc::CritScope lock(&crit_);
+    rtp_packet_sender_ = rtp_packet_sender;
+  }
+
+  // Implements RtpPacketSender.
+  void InsertPacket(Priority priority,
+                    uint32_t ssrc,
+                    uint16_t sequence_number,
+                    int64_t capture_time_ms,
+                    size_t bytes,
+                    bool retransmission) override {
+    rtc::CritScope lock(&crit_);
+    if (rtp_packet_sender_) {
+      rtp_packet_sender_->InsertPacket(priority, ssrc, sequence_number,
+                                       capture_time_ms, bytes, retransmission);
+    }
+  }
+
+  void SetAccountForAudioPackets(bool account_for_audio) override {
+    RTC_NOTREACHED();
+  }
+
+ private:
+  rtc::ThreadChecker thread_checker_;
+  rtc::CriticalSection crit_;
+  RtpPacketSender* rtp_packet_sender_ RTC_GUARDED_BY(&crit_);
+};
+
+class VoERtcpObserver : public RtcpBandwidthObserver {
+ public:
+  explicit VoERtcpObserver(ChannelSend* owner)
+      : owner_(owner), bandwidth_observer_(nullptr) {}
+  virtual ~VoERtcpObserver() {}
+
+  void SetBandwidthObserver(RtcpBandwidthObserver* bandwidth_observer) {
+    rtc::CritScope lock(&crit_);
+    bandwidth_observer_ = bandwidth_observer;
+  }
+
+  void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
+    rtc::CritScope lock(&crit_);
+    if (bandwidth_observer_) {
+      bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate);
+    }
+  }
+
+  void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
+                                    int64_t rtt,
+                                    int64_t now_ms) override {
+    {
+      rtc::CritScope lock(&crit_);
+      if (bandwidth_observer_) {
+        bandwidth_observer_->OnReceivedRtcpReceiverReport(report_blocks, rtt,
+                                                          now_ms);
+      }
+    }
+    // TODO(mflodman): Do we need to aggregate reports here or can we jut send
+    // what we get? I.e. do we ever get multiple reports bundled into one RTCP
+    // report for VoiceEngine?
+    if (report_blocks.empty())
+      return;
+
+    int fraction_lost_aggregate = 0;
+    int total_number_of_packets = 0;
+
+    // If receiving multiple report blocks, calculate the weighted average based
+    // on the number of packets a report refers to.
+    for (ReportBlockList::const_iterator block_it = report_blocks.begin();
+         block_it != report_blocks.end(); ++block_it) {
+      // Find the previous extended high sequence number for this remote SSRC,
+      // to calculate the number of RTP packets this report refers to. Ignore if
+      // we haven't seen this SSRC before.
+      std::map<uint32_t, uint32_t>::iterator seq_num_it =
+          extended_max_sequence_number_.find(block_it->source_ssrc);
+      int number_of_packets = 0;
+      if (seq_num_it != extended_max_sequence_number_.end()) {
+        number_of_packets =
+            block_it->extended_highest_sequence_number - seq_num_it->second;
+      }
+      fraction_lost_aggregate += number_of_packets * block_it->fraction_lost;
+      total_number_of_packets += number_of_packets;
+
+      extended_max_sequence_number_[block_it->source_ssrc] =
+          block_it->extended_highest_sequence_number;
+    }
+    int weighted_fraction_lost = 0;
+    if (total_number_of_packets > 0) {
+      weighted_fraction_lost =
+          (fraction_lost_aggregate + total_number_of_packets / 2) /
+          total_number_of_packets;
+    }
+    owner_->OnUplinkPacketLossRate(weighted_fraction_lost / 255.0f);
+  }
+
+ private:
+  ChannelSend* owner_;
+  // Maps remote side ssrc to extended highest sequence number received.
+  std::map<uint32_t, uint32_t> extended_max_sequence_number_;
+  rtc::CriticalSection crit_;
+  RtcpBandwidthObserver* bandwidth_observer_ RTC_GUARDED_BY(crit_);
+};
+
+class ChannelSend::ProcessAndEncodeAudioTask : public rtc::QueuedTask {
+ public:
+  ProcessAndEncodeAudioTask(std::unique_ptr<AudioFrame> audio_frame,
+                            ChannelSend* channel)
+      : audio_frame_(std::move(audio_frame)), channel_(channel) {
+    RTC_DCHECK(channel_);
+  }
+
+ private:
+  bool Run() override {
+    RTC_DCHECK_RUN_ON(channel_->encoder_queue_);
+    channel_->ProcessAndEncodeAudioOnTaskQueue(audio_frame_.get());
+    return true;
+  }
+
+  std::unique_ptr<AudioFrame> audio_frame_;
+  ChannelSend* const channel_;
+};
+
+int32_t ChannelSend::SendData(FrameType frameType,
+                              uint8_t payloadType,
+                              uint32_t timeStamp,
+                              const uint8_t* payloadData,
+                              size_t payloadSize,
+                              const RTPFragmentationHeader* fragmentation) {
+  RTC_DCHECK_RUN_ON(encoder_queue_);
+  if (_includeAudioLevelIndication) {
+    // Store current audio level in the RTP/RTCP module.
+    // The level will be used in combination with voice-activity state
+    // (frameType) to add an RTP header extension
+    _rtpRtcpModule->SetAudioLevel(rms_level_.Average());
+  }
+
+  // E2EE Custom Audio Frame Encryption (This is optional).
+  // Keep this buffer around for the lifetime of the send call.
+  rtc::Buffer encrypted_audio_payload;
+  if (frame_encryptor_ != nullptr) {
+    // TODO(benwright@webrtc.org) - Allocate enough to always encrypt inline.
+    // Allocate a buffer to hold the maximum possible encrypted payload.
+    size_t max_ciphertext_size = frame_encryptor_->GetMaxCiphertextByteSize(
+        cricket::MEDIA_TYPE_AUDIO, payloadSize);
+    encrypted_audio_payload.SetSize(max_ciphertext_size);
+
+    // Encrypt the audio payload into the buffer.
+    size_t bytes_written = 0;
+    int encrypt_status = frame_encryptor_->Encrypt(
+        cricket::MEDIA_TYPE_AUDIO, _rtpRtcpModule->SSRC(),
+        /*additional_data=*/nullptr,
+        rtc::ArrayView<const uint8_t>(payloadData, payloadSize),
+        encrypted_audio_payload, &bytes_written);
+    if (encrypt_status != 0) {
+      RTC_DLOG(LS_ERROR) << "Channel::SendData() failed encrypt audio payload: "
+                         << encrypt_status;
+      return -1;
+    }
+    // Resize the buffer to the exact number of bytes actually used.
+    encrypted_audio_payload.SetSize(bytes_written);
+    // Rewrite the payloadData and size to the new encrypted payload.
+    payloadData = encrypted_audio_payload.data();
+    payloadSize = encrypted_audio_payload.size();
+  }
+
+  // Push data from ACM to RTP/RTCP-module to deliver audio frame for
+  // packetization.
+  // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
+  if (!_rtpRtcpModule->SendOutgoingData(
+          (FrameType&)frameType, payloadType, timeStamp,
+          // Leaving the time when this frame was
+          // received from the capture device as
+          // undefined for voice for now.
+          -1, payloadData, payloadSize, fragmentation, nullptr, nullptr)) {
+    RTC_DLOG(LS_ERROR)
+        << "ChannelSend::SendData() failed to send data to RTP/RTCP module";
+    return -1;
+  }
+
+  return 0;
+}
+
+bool ChannelSend::SendRtp(const uint8_t* data,
+                          size_t len,
+                          const PacketOptions& options) {
+  rtc::CritScope cs(&_callbackCritSect);
+
+  if (_transportPtr == NULL) {
+    RTC_DLOG(LS_ERROR)
+        << "ChannelSend::SendPacket() failed to send RTP packet due to"
+        << " invalid transport object";
+    return false;
+  }
+
+  if (!_transportPtr->SendRtp(data, len, options)) {
+    RTC_DLOG(LS_ERROR) << "ChannelSend::SendPacket() RTP transmission failed";
+    return false;
+  }
+  return true;
+}
+
+bool ChannelSend::SendRtcp(const uint8_t* data, size_t len) {
+  rtc::CritScope cs(&_callbackCritSect);
+  if (_transportPtr == NULL) {
+    RTC_DLOG(LS_ERROR)
+        << "ChannelSend::SendRtcp() failed to send RTCP packet due to"
+        << " invalid transport object";
+    return false;
+  }
+
+  int n = _transportPtr->SendRtcp(data, len);
+  if (n < 0) {
+    RTC_DLOG(LS_ERROR) << "ChannelSend::SendRtcp() transmission failed";
+    return false;
+  }
+  return true;
+}
+
+int ChannelSend::PreferredSampleRate() const {
+  // Return the bigger of playout and receive frequency in the ACM.
+  return std::max(audio_coding_->ReceiveFrequency(),
+                  audio_coding_->PlayoutFrequency());
+}
+
+ChannelSend::ChannelSend(rtc::TaskQueue* encoder_queue,
+                         ProcessThread* module_process_thread,
+                         RtcpRttStats* rtcp_rtt_stats,
+                         RtcEventLog* rtc_event_log,
+                         FrameEncryptorInterface* frame_encryptor)
+    : event_log_(rtc_event_log),
+      _timeStamp(0),  // This is just an offset, RTP module will add it's own
+                      // random offset
+      send_sequence_number_(0),
+      _moduleProcessThreadPtr(module_process_thread),
+      _transportPtr(NULL),
+      input_mute_(false),
+      previous_frame_muted_(false),
+      _includeAudioLevelIndication(false),
+      transport_overhead_per_packet_(0),
+      rtp_overhead_per_packet_(0),
+      rtcp_observer_(new VoERtcpObserver(this)),
+      feedback_observer_proxy_(new TransportFeedbackProxy()),
+      seq_num_allocator_proxy_(new TransportSequenceNumberProxy()),
+      rtp_packet_sender_proxy_(new RtpPacketSenderProxy()),
+      retransmission_rate_limiter_(new RateLimiter(Clock::GetRealTimeClock(),
+                                                   kMaxRetransmissionWindowMs)),
+      use_twcc_plr_for_ana_(
+          webrtc::field_trial::FindFullName("UseTwccPlrForAna") == "Enabled"),
+      encoder_queue_(encoder_queue),
+      frame_encryptor_(frame_encryptor) {
+  RTC_DCHECK(module_process_thread);
+  RTC_DCHECK(encoder_queue);
+  audio_coding_.reset(AudioCodingModule::Create(AudioCodingModule::Config()));
+
+  RtpRtcp::Configuration configuration;
+  configuration.audio = true;
+  configuration.outgoing_transport = this;
+  configuration.overhead_observer = this;
+  configuration.bandwidth_callback = rtcp_observer_.get();
+
+  configuration.paced_sender = rtp_packet_sender_proxy_.get();
+  configuration.transport_sequence_number_allocator =
+      seq_num_allocator_proxy_.get();
+  configuration.transport_feedback_callback = feedback_observer_proxy_.get();
+
+  configuration.event_log = event_log_;
+  configuration.rtt_stats = rtcp_rtt_stats;
+  configuration.retransmission_rate_limiter =
+      retransmission_rate_limiter_.get();
+
+  _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
+  _rtpRtcpModule->SetSendingMediaStatus(false);
+  Init();
+}
+
+ChannelSend::~ChannelSend() {
+  Terminate();
+  RTC_DCHECK(!channel_state_.Get().sending);
+}
+
+void ChannelSend::Init() {
+  channel_state_.Reset();
+
+  // --- Add modules to process thread (for periodic schedulation)
+  _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get(), RTC_FROM_HERE);
+
+  // --- ACM initialization
+  int error = audio_coding_->InitializeReceiver();
+  RTC_DCHECK_EQ(0, error);
+
+  // --- RTP/RTCP module initialization
+
+  // Ensure that RTCP is enabled by default for the created channel.
+  // Note that, the module will keep generating RTCP until it is explicitly
+  // disabled by the user.
+  // After StopListen (when no sockets exists), RTCP packets will no longer
+  // be transmitted since the Transport object will then be invalid.
+  // RTCP is enabled by default.
+  _rtpRtcpModule->SetRTCPStatus(RtcpMode::kCompound);
+
+  // --- Register all permanent callbacks
+  error = audio_coding_->RegisterTransportCallback(this);
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelSend::Terminate() {
+  RTC_DCHECK(construction_thread_.CalledOnValidThread());
+  // Must be called on the same thread as Init().
+
+  StopSend();
+
+  // The order to safely shutdown modules in a channel is:
+  // 1. De-register callbacks in modules
+  // 2. De-register modules in process thread
+  // 3. Destroy modules
+  int error = audio_coding_->RegisterTransportCallback(NULL);
+  RTC_DCHECK_EQ(0, error);
+
+  // De-register modules in process thread
+  if (_moduleProcessThreadPtr)
+    _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
+
+  // End of modules shutdown
+}
+
+int32_t ChannelSend::StartSend() {
+  if (channel_state_.Get().sending) {
+    return 0;
+  }
+  channel_state_.SetSending(true);
+
+  // Resume the previous sequence number which was reset by StopSend(). This
+  // needs to be done before |sending| is set to true on the RTP/RTCP module.
+  if (send_sequence_number_) {
+    _rtpRtcpModule->SetSequenceNumber(send_sequence_number_);
+  }
+  _rtpRtcpModule->SetSendingMediaStatus(true);
+  if (_rtpRtcpModule->SetSendingStatus(true) != 0) {
+    RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to start sending";
+    _rtpRtcpModule->SetSendingMediaStatus(false);
+    rtc::CritScope cs(&_callbackCritSect);
+    channel_state_.SetSending(false);
+    return -1;
+  }
+  {
+    // It is now OK to start posting tasks to the encoder task queue.
+    rtc::CritScope cs(&encoder_queue_lock_);
+    encoder_queue_is_active_ = true;
+  }
+  return 0;
+}
+
+void ChannelSend::StopSend() {
+  if (!channel_state_.Get().sending) {
+    return;
+  }
+  channel_state_.SetSending(false);
+
+  // Post a task to the encoder thread which sets an event when the task is
+  // executed. We know that no more encoding tasks will be added to the task
+  // queue for this channel since sending is now deactivated. It means that,
+  // if we wait for the event to bet set, we know that no more pending tasks
+  // exists and it is therfore guaranteed that the task queue will never try
+  // to acccess and invalid channel object.
+  RTC_DCHECK(encoder_queue_);
+
+  rtc::Event flush(false, false);
+  {
+    // Clear |encoder_queue_is_active_| under lock to prevent any other tasks
+    // than this final "flush task" to be posted on the queue.
+    rtc::CritScope cs(&encoder_queue_lock_);
+    encoder_queue_is_active_ = false;
+    encoder_queue_->PostTask([&flush]() { flush.Set(); });
+  }
+  flush.Wait(rtc::Event::kForever);
+
+  // Store the sequence number to be able to pick up the same sequence for
+  // the next StartSend(). This is needed for restarting device, otherwise
+  // it might cause libSRTP to complain about packets being replayed.
+  // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
+  // CL is landed. See issue
+  // https://code.google.com/p/webrtc/issues/detail?id=2111 .
+  send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
+
+  // Reset sending SSRC and sequence number and triggers direct transmission
+  // of RTCP BYE
+  if (_rtpRtcpModule->SetSendingStatus(false) == -1) {
+    RTC_DLOG(LS_ERROR) << "StartSend() RTP/RTCP failed to stop sending";
+  }
+  _rtpRtcpModule->SetSendingMediaStatus(false);
+}
+
+bool ChannelSend::SetEncoder(int payload_type,
+                             std::unique_ptr<AudioEncoder> encoder) {
+  RTC_DCHECK_GE(payload_type, 0);
+  RTC_DCHECK_LE(payload_type, 127);
+  // TODO(ossu): Make CodecInsts up, for now: one for the RTP/RTCP module and
+  // one for for us to keep track of sample rate and number of channels, etc.
+
+  // The RTP/RTCP module needs to know the RTP timestamp rate (i.e. clockrate)
+  // as well as some other things, so we collect this info and send it along.
+  CodecInst rtp_codec;
+  rtp_codec.pltype = payload_type;
+  strncpy(rtp_codec.plname, "audio", sizeof(rtp_codec.plname));
+  rtp_codec.plname[sizeof(rtp_codec.plname) - 1] = 0;
+  // Seems unclear if it should be clock rate or sample rate. CodecInst
+  // supposedly carries the sample rate, but only clock rate seems sensible to
+  // send to the RTP/RTCP module.
+  rtp_codec.plfreq = encoder->RtpTimestampRateHz();
+  rtp_codec.pacsize = rtc::CheckedDivExact(
+      static_cast<int>(encoder->Max10MsFramesInAPacket() * rtp_codec.plfreq),
+      100);
+  rtp_codec.channels = encoder->NumChannels();
+  rtp_codec.rate = 0;
+
+  if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
+    _rtpRtcpModule->DeRegisterSendPayload(payload_type);
+    if (_rtpRtcpModule->RegisterSendPayload(rtp_codec) != 0) {
+      RTC_DLOG(LS_ERROR)
+          << "SetEncoder() failed to register codec to RTP/RTCP module";
+      return false;
+    }
+  }
+
+  audio_coding_->SetEncoder(std::move(encoder));
+  return true;
+}
+
+void ChannelSend::ModifyEncoder(
+    rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
+  audio_coding_->ModifyEncoder(modifier);
+}
+
+void ChannelSend::SetBitRate(int bitrate_bps, int64_t probing_interval_ms) {
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->OnReceivedUplinkBandwidth(bitrate_bps, probing_interval_ms);
+    }
+  });
+  retransmission_rate_limiter_->SetMaxRate(bitrate_bps);
+}
+
+void ChannelSend::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
+  if (!use_twcc_plr_for_ana_)
+    return;
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
+    }
+  });
+}
+
+void ChannelSend::OnRecoverableUplinkPacketLossRate(
+    float recoverable_packet_loss_rate) {
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->OnReceivedUplinkRecoverablePacketLossFraction(
+          recoverable_packet_loss_rate);
+    }
+  });
+}
+
+void ChannelSend::OnUplinkPacketLossRate(float packet_loss_rate) {
+  if (use_twcc_plr_for_ana_)
+    return;
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->OnReceivedUplinkPacketLossFraction(packet_loss_rate);
+    }
+  });
+}
+
+bool ChannelSend::EnableAudioNetworkAdaptor(const std::string& config_string) {
+  bool success = false;
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      success =
+          (*encoder)->EnableAudioNetworkAdaptor(config_string, event_log_);
+    }
+  });
+  return success;
+}
+
+void ChannelSend::DisableAudioNetworkAdaptor() {
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder)
+      (*encoder)->DisableAudioNetworkAdaptor();
+  });
+}
+
+void ChannelSend::SetReceiverFrameLengthRange(int min_frame_length_ms,
+                                              int max_frame_length_ms) {
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->SetReceiverFrameLengthRange(min_frame_length_ms,
+                                              max_frame_length_ms);
+    }
+  });
+}
+
+void ChannelSend::RegisterTransport(Transport* transport) {
+  rtc::CritScope cs(&_callbackCritSect);
+  _transportPtr = transport;
+}
+
+int32_t ChannelSend::ReceivedRTCPPacket(const uint8_t* data, size_t length) {
+  // Deliver RTCP packet to RTP/RTCP module for parsing
+  _rtpRtcpModule->IncomingRtcpPacket(data, length);
+
+  int64_t rtt = GetRTT();
+  if (rtt == 0) {
+    // Waiting for valid RTT.
+    return 0;
+  }
+
+  int64_t nack_window_ms = rtt;
+  if (nack_window_ms < kMinRetransmissionWindowMs) {
+    nack_window_ms = kMinRetransmissionWindowMs;
+  } else if (nack_window_ms > kMaxRetransmissionWindowMs) {
+    nack_window_ms = kMaxRetransmissionWindowMs;
+  }
+  retransmission_rate_limiter_->SetWindowSize(nack_window_ms);
+
+  // Invoke audio encoders OnReceivedRtt().
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder)
+      (*encoder)->OnReceivedRtt(rtt);
+  });
+
+  return 0;
+}
+
+void ChannelSend::SetInputMute(bool enable) {
+  rtc::CritScope cs(&volume_settings_critsect_);
+  input_mute_ = enable;
+}
+
+bool ChannelSend::InputMute() const {
+  rtc::CritScope cs(&volume_settings_critsect_);
+  return input_mute_;
+}
+
+int ChannelSend::SendTelephoneEventOutband(int event, int duration_ms) {
+  RTC_DCHECK_LE(0, event);
+  RTC_DCHECK_GE(255, event);
+  RTC_DCHECK_LE(0, duration_ms);
+  RTC_DCHECK_GE(65535, duration_ms);
+  if (!Sending()) {
+    return -1;
+  }
+  if (_rtpRtcpModule->SendTelephoneEventOutband(
+          event, duration_ms, kTelephoneEventAttenuationdB) != 0) {
+    RTC_DLOG(LS_ERROR) << "SendTelephoneEventOutband() failed to send event";
+    return -1;
+  }
+  return 0;
+}
+
+int ChannelSend::SetSendTelephoneEventPayloadType(int payload_type,
+                                                  int payload_frequency) {
+  RTC_DCHECK_LE(0, payload_type);
+  RTC_DCHECK_GE(127, payload_type);
+  CodecInst codec = {0};
+  codec.pltype = payload_type;
+  codec.plfreq = payload_frequency;
+  memcpy(codec.plname, "telephone-event", 16);
+  if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
+    _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
+    if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
+      RTC_DLOG(LS_ERROR)
+          << "SetSendTelephoneEventPayloadType() failed to register "
+             "send payload type";
+      return -1;
+    }
+  }
+  return 0;
+}
+
+int ChannelSend::SetLocalSSRC(unsigned int ssrc) {
+  if (channel_state_.Get().sending) {
+    RTC_DLOG(LS_ERROR) << "SetLocalSSRC() already sending";
+    return -1;
+  }
+  _rtpRtcpModule->SetSSRC(ssrc);
+  return 0;
+}
+
+void ChannelSend::SetMid(const std::string& mid, int extension_id) {
+  int ret = SetSendRtpHeaderExtension(true, kRtpExtensionMid, extension_id);
+  RTC_DCHECK_EQ(0, ret);
+  _rtpRtcpModule->SetMid(mid);
+}
+
+int ChannelSend::SetSendAudioLevelIndicationStatus(bool enable,
+                                                   unsigned char id) {
+  _includeAudioLevelIndication = enable;
+  return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
+}
+
+void ChannelSend::EnableSendTransportSequenceNumber(int id) {
+  int ret =
+      SetSendRtpHeaderExtension(true, kRtpExtensionTransportSequenceNumber, id);
+  RTC_DCHECK_EQ(0, ret);
+}
+
+void ChannelSend::RegisterSenderCongestionControlObjects(
+    RtpTransportControllerSendInterface* transport,
+    RtcpBandwidthObserver* bandwidth_observer) {
+  RtpPacketSender* rtp_packet_sender = transport->packet_sender();
+  TransportFeedbackObserver* transport_feedback_observer =
+      transport->transport_feedback_observer();
+  PacketRouter* packet_router = transport->packet_router();
+
+  RTC_DCHECK(rtp_packet_sender);
+  RTC_DCHECK(transport_feedback_observer);
+  RTC_DCHECK(packet_router);
+  RTC_DCHECK(!packet_router_);
+  rtcp_observer_->SetBandwidthObserver(bandwidth_observer);
+  feedback_observer_proxy_->SetTransportFeedbackObserver(
+      transport_feedback_observer);
+  seq_num_allocator_proxy_->SetSequenceNumberAllocator(packet_router);
+  rtp_packet_sender_proxy_->SetPacketSender(rtp_packet_sender);
+  _rtpRtcpModule->SetStorePacketsStatus(true, 600);
+  constexpr bool remb_candidate = false;
+  packet_router->AddSendRtpModule(_rtpRtcpModule.get(), remb_candidate);
+  packet_router_ = packet_router;
+}
+
+void ChannelSend::ResetSenderCongestionControlObjects() {
+  RTC_DCHECK(packet_router_);
+  _rtpRtcpModule->SetStorePacketsStatus(false, 600);
+  rtcp_observer_->SetBandwidthObserver(nullptr);
+  feedback_observer_proxy_->SetTransportFeedbackObserver(nullptr);
+  seq_num_allocator_proxy_->SetSequenceNumberAllocator(nullptr);
+  packet_router_->RemoveSendRtpModule(_rtpRtcpModule.get());
+  packet_router_ = nullptr;
+  rtp_packet_sender_proxy_->SetPacketSender(nullptr);
+}
+
+void ChannelSend::SetRTCPStatus(bool enable) {
+  _rtpRtcpModule->SetRTCPStatus(enable ? RtcpMode::kCompound : RtcpMode::kOff);
+}
+
+int ChannelSend::SetRTCP_CNAME(const char cName[256]) {
+  if (_rtpRtcpModule->SetCNAME(cName) != 0) {
+    RTC_DLOG(LS_ERROR) << "SetRTCP_CNAME() failed to set RTCP CNAME";
+    return -1;
+  }
+  return 0;
+}
+
+int ChannelSend::GetRemoteRTCPReportBlocks(
+    std::vector<ReportBlock>* report_blocks) {
+  if (report_blocks == NULL) {
+    RTC_DLOG(LS_ERROR) << "GetRemoteRTCPReportBlock()s invalid report_blocks.";
+    return -1;
+  }
+
+  // Get the report blocks from the latest received RTCP Sender or Receiver
+  // Report. Each element in the vector contains the sender's SSRC and a
+  // report block according to RFC 3550.
+  std::vector<RTCPReportBlock> rtcp_report_blocks;
+  if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
+    return -1;
+  }
+
+  if (rtcp_report_blocks.empty())
+    return 0;
+
+  std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
+  for (; it != rtcp_report_blocks.end(); ++it) {
+    ReportBlock report_block;
+    report_block.sender_SSRC = it->sender_ssrc;
+    report_block.source_SSRC = it->source_ssrc;
+    report_block.fraction_lost = it->fraction_lost;
+    report_block.cumulative_num_packets_lost = it->packets_lost;
+    report_block.extended_highest_sequence_number =
+        it->extended_highest_sequence_number;
+    report_block.interarrival_jitter = it->jitter;
+    report_block.last_SR_timestamp = it->last_sender_report_timestamp;
+    report_block.delay_since_last_SR = it->delay_since_last_sender_report;
+    report_blocks->push_back(report_block);
+  }
+  return 0;
+}
+
+int ChannelSend::GetRTPStatistics(CallSendStatistics& stats) {
+  // --- RtcpStatistics
+
+  // --- RTT
+  stats.rttMs = GetRTT();
+
+  // --- Data counters
+
+  size_t bytesSent(0);
+  uint32_t packetsSent(0);
+
+  if (_rtpRtcpModule->DataCountersRTP(&bytesSent, &packetsSent) != 0) {
+    RTC_DLOG(LS_WARNING)
+        << "GetRTPStatistics() failed to retrieve RTP datacounters"
+        << " => output will not be complete";
+  }
+
+  stats.bytesSent = bytesSent;
+  stats.packetsSent = packetsSent;
+
+  return 0;
+}
+
+void ChannelSend::SetNACKStatus(bool enable, int maxNumberOfPackets) {
+  // None of these functions can fail.
+  if (enable)
+    audio_coding_->EnableNack(maxNumberOfPackets);
+  else
+    audio_coding_->DisableNack();
+}
+
+// Called when we are missing one or more packets.
+int ChannelSend::ResendPackets(const uint16_t* sequence_numbers, int length) {
+  return _rtpRtcpModule->SendNACK(sequence_numbers, length);
+}
+
+void ChannelSend::ProcessAndEncodeAudio(
+    std::unique_ptr<AudioFrame> audio_frame) {
+  // Avoid posting any new tasks if sending was already stopped in StopSend().
+  rtc::CritScope cs(&encoder_queue_lock_);
+  if (!encoder_queue_is_active_) {
+    return;
+  }
+  // Profile time between when the audio frame is added to the task queue and
+  // when the task is actually executed.
+  audio_frame->UpdateProfileTimeStamp();
+  encoder_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(
+      new ProcessAndEncodeAudioTask(std::move(audio_frame), this)));
+}
+
+void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
+  RTC_DCHECK_RUN_ON(encoder_queue_);
+  RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
+  RTC_DCHECK_LE(audio_input->num_channels_, 2);
+
+  // Measure time between when the audio frame is added to the task queue and
+  // when the task is actually executed. Goal is to keep track of unwanted
+  // extra latency added by the task queue.
+  RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
+                             audio_input->ElapsedProfileTimeMs());
+
+  bool is_muted = InputMute();
+  AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
+
+  if (_includeAudioLevelIndication) {
+    size_t length =
+        audio_input->samples_per_channel_ * audio_input->num_channels_;
+    RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
+    if (is_muted && previous_frame_muted_) {
+      rms_level_.AnalyzeMuted(length);
+    } else {
+      rms_level_.Analyze(
+          rtc::ArrayView<const int16_t>(audio_input->data(), length));
+    }
+  }
+  previous_frame_muted_ = is_muted;
+
+  // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
+
+  // The ACM resamples internally.
+  audio_input->timestamp_ = _timeStamp;
+  // This call will trigger AudioPacketizationCallback::SendData if encoding
+  // is done and payload is ready for packetization and transmission.
+  // Otherwise, it will return without invoking the callback.
+  if (audio_coding_->Add10MsData(*audio_input) < 0) {
+    RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
+    return;
+  }
+
+  _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
+}
+
+void ChannelSend::UpdateOverheadForEncoder() {
+  size_t overhead_per_packet =
+      transport_overhead_per_packet_ + rtp_overhead_per_packet_;
+  audio_coding_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder) {
+    if (*encoder) {
+      (*encoder)->OnReceivedOverhead(overhead_per_packet);
+    }
+  });
+}
+
+void ChannelSend::SetTransportOverhead(size_t transport_overhead_per_packet) {
+  rtc::CritScope cs(&overhead_per_packet_lock_);
+  transport_overhead_per_packet_ = transport_overhead_per_packet;
+  UpdateOverheadForEncoder();
+}
+
+// TODO(solenberg): Make AudioSendStream an OverheadObserver instead.
+void ChannelSend::OnOverheadChanged(size_t overhead_bytes_per_packet) {
+  rtc::CritScope cs(&overhead_per_packet_lock_);
+  rtp_overhead_per_packet_ = overhead_bytes_per_packet;
+  UpdateOverheadForEncoder();
+}
+
+ANAStats ChannelSend::GetANAStatistics() const {
+  return audio_coding_->GetANAStats();
+}
+
+RtpRtcp* ChannelSend::GetRtpRtcp() const {
+  return _rtpRtcpModule.get();
+}
+
+int ChannelSend::SetSendRtpHeaderExtension(bool enable,
+                                           RTPExtensionType type,
+                                           unsigned char id) {
+  int error = 0;
+  _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
+  if (enable) {
+    error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
+  }
+  return error;
+}
+
+int ChannelSend::GetRtpTimestampRateHz() const {
+  const auto format = audio_coding_->ReceiveFormat();
+  // Default to the playout frequency if we've not gotten any packets yet.
+  // TODO(ossu): Zero clockrate can only happen if we've added an external
+  // decoder for a format we don't support internally. Remove once that way of
+  // adding decoders is gone!
+  return (format && format->clockrate_hz != 0)
+             ? format->clockrate_hz
+             : audio_coding_->PlayoutFrequency();
+}
+
+int64_t ChannelSend::GetRTT() const {
+  RtcpMode method = _rtpRtcpModule->RTCP();
+  if (method == RtcpMode::kOff) {
+    return 0;
+  }
+  std::vector<RTCPReportBlock> report_blocks;
+  _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
+
+  if (report_blocks.empty()) {
+    return 0;
+  }
+
+  int64_t rtt = 0;
+  int64_t avg_rtt = 0;
+  int64_t max_rtt = 0;
+  int64_t min_rtt = 0;
+  // We don't know in advance the remote ssrc used by the other end's receiver
+  // reports, so use the SSRC of the first report block for calculating the RTT.
+  if (_rtpRtcpModule->RTT(report_blocks[0].sender_ssrc, &rtt, &avg_rtt,
+                          &min_rtt, &max_rtt) != 0) {
+    return 0;
+  }
+  return rtt;
+}
+
+void ChannelSend::SetFrameEncryptor(FrameEncryptorInterface* frame_encryptor) {
+  rtc::CritScope cs(&encoder_queue_lock_);
+  if (encoder_queue_is_active_) {
+    encoder_queue_->PostTask([this, frame_encryptor]() {
+      this->frame_encryptor_ = frame_encryptor;
+    });
+  } else {
+    frame_encryptor_ = frame_encryptor;
+  }
+}
+
+}  // namespace voe
+}  // namespace webrtc
diff --git a/audio/channel_send.h b/audio/channel_send.h
new file mode 100644
index 0000000..ef92f8e
--- /dev/null
+++ b/audio/channel_send.h
@@ -0,0 +1,306 @@
+/*
+ *  Copyright (c) 2012 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 AUDIO_CHANNEL_SEND_H_
+#define AUDIO_CHANNEL_SEND_H_
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "api/audio/audio_frame.h"
+#include "api/audio_codecs/audio_encoder.h"
+#include "api/call/transport.h"
+#include "common_types.h"  // NOLINT(build/include)
+#include "modules/audio_coding/include/audio_coding_module.h"
+#include "modules/audio_processing/rms_level.h"
+#include "modules/rtp_rtcp/include/rtp_rtcp.h"
+#include "rtc_base/criticalsection.h"
+#include "rtc_base/task_queue.h"
+#include "rtc_base/thread_checker.h"
+
+// TODO(solenberg, nisse): This file contains a few NOLINT marks, to silence
+// warnings about use of unsigned short, and non-const reference arguments.
+// These need cleanup, in a separate cl.
+
+namespace rtc {
+class TimestampWrapAroundHandler;
+}
+
+namespace webrtc {
+
+class FrameEncryptorInterface;
+class PacketRouter;
+class ProcessThread;
+class RateLimiter;
+class RtcEventLog;
+class RtpRtcp;
+class RtpTransportControllerSendInterface;
+
+struct SenderInfo;
+
+struct CallSendStatistics {
+  int64_t rttMs;
+  size_t bytesSent;
+  int packetsSent;
+};
+
+// See section 6.4.2 in http://www.ietf.org/rfc/rfc3550.txt for details.
+struct ReportBlock {
+  uint32_t sender_SSRC;  // SSRC of sender
+  uint32_t source_SSRC;
+  uint8_t fraction_lost;
+  int32_t cumulative_num_packets_lost;
+  uint32_t extended_highest_sequence_number;
+  uint32_t interarrival_jitter;
+  uint32_t last_SR_timestamp;
+  uint32_t delay_since_last_SR;
+};
+
+namespace voe {
+
+class RtpPacketSenderProxy;
+class TransportFeedbackProxy;
+class TransportSequenceNumberProxy;
+class VoERtcpObserver;
+
+// Helper class to simplify locking scheme for members that are accessed from
+// multiple threads.
+// Example: a member can be set on thread T1 and read by an internal audio
+// thread T2. Accessing the member via this class ensures that we are
+// safe and also avoid TSan v2 warnings.
+class ChannelSendState {
+ public:
+  struct State {
+    bool sending = false;
+  };
+
+  ChannelSendState() {}
+  virtual ~ChannelSendState() {}
+
+  void Reset() {
+    rtc::CritScope lock(&lock_);
+    state_ = State();
+  }
+
+  State Get() const {
+    rtc::CritScope lock(&lock_);
+    return state_;
+  }
+
+  void SetSending(bool enable) {
+    rtc::CritScope lock(&lock_);
+    state_.sending = enable;
+  }
+
+ private:
+  rtc::CriticalSection lock_;
+  State state_;
+};
+
+class ChannelSend
+    : public Transport,
+      public AudioPacketizationCallback,  // receive encoded packets from the
+                                          // ACM
+      public OverheadObserver {
+ public:
+  // TODO(nisse): Make OnUplinkPacketLossRate public, and delete friend
+  // declaration.
+  friend class VoERtcpObserver;
+
+  ChannelSend(rtc::TaskQueue* encoder_queue,
+              ProcessThread* module_process_thread,
+              RtcpRttStats* rtcp_rtt_stats,
+              RtcEventLog* rtc_event_log,
+              FrameEncryptorInterface* frame_encryptor);
+
+  virtual ~ChannelSend();
+
+  // Send using this encoder, with this payload type.
+  bool SetEncoder(int payload_type, std::unique_ptr<AudioEncoder> encoder);
+  void ModifyEncoder(
+      rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
+
+  // API methods
+
+  // VoEBase
+  int32_t StartSend();
+  void StopSend();
+
+  // Codecs
+  void SetBitRate(int bitrate_bps, int64_t probing_interval_ms);
+  bool EnableAudioNetworkAdaptor(const std::string& config_string);
+  void DisableAudioNetworkAdaptor();
+
+  // TODO(nisse): Modifies decoder, but not used?
+  void SetReceiverFrameLengthRange(int min_frame_length_ms,
+                                   int max_frame_length_ms);
+
+  // Network
+  void RegisterTransport(Transport* transport);
+  // TODO(nisse, solenberg): Delete when VoENetwork is deleted.
+  int32_t ReceivedRTCPPacket(const uint8_t* data, size_t length);
+
+  // Muting, Volume and Level.
+  void SetInputMute(bool enable);
+
+  // Stats.
+  ANAStats GetANAStatistics() const;
+
+  // Used by AudioSendStream.
+  RtpRtcp* GetRtpRtcp() const;
+
+  // DTMF.
+  int SendTelephoneEventOutband(int event, int duration_ms);
+  int SetSendTelephoneEventPayloadType(int payload_type, int payload_frequency);
+
+  // RTP+RTCP
+  int SetLocalSSRC(unsigned int ssrc);
+
+  void SetMid(const std::string& mid, int extension_id);
+  int SetSendAudioLevelIndicationStatus(bool enable, unsigned char id);
+  void EnableSendTransportSequenceNumber(int id);
+
+  void RegisterSenderCongestionControlObjects(
+      RtpTransportControllerSendInterface* transport,
+      RtcpBandwidthObserver* bandwidth_observer);
+  void ResetSenderCongestionControlObjects();
+  void SetRTCPStatus(bool enable);
+  int SetRTCP_CNAME(const char cName[256]);
+  int GetRemoteRTCPReportBlocks(std::vector<ReportBlock>* report_blocks);
+  int GetRTPStatistics(CallSendStatistics& stats);  // NOLINT
+  void SetNACKStatus(bool enable, int maxNumberOfPackets);
+
+  // From AudioPacketizationCallback in the ACM
+  int32_t SendData(FrameType frameType,
+                   uint8_t payloadType,
+                   uint32_t timeStamp,
+                   const uint8_t* payloadData,
+                   size_t payloadSize,
+                   const RTPFragmentationHeader* fragmentation) override;
+
+  // From Transport (called by the RTP/RTCP module)
+  bool SendRtp(const uint8_t* data,
+               size_t len,
+               const PacketOptions& packet_options) override;
+  bool SendRtcp(const uint8_t* data, size_t len) override;
+
+  int PreferredSampleRate() const;
+
+  bool Sending() const { return channel_state_.Get().sending; }
+  RtpRtcp* RtpRtcpModulePtr() const { return _rtpRtcpModule.get(); }
+
+  // ProcessAndEncodeAudio() posts a task on the shared encoder task queue,
+  // which in turn calls (on the queue) ProcessAndEncodeAudioOnTaskQueue() where
+  // the actual processing of the audio takes place. The processing mainly
+  // consists of encoding and preparing the result for sending by adding it to a
+  // send queue.
+  // The main reason for using a task queue here is to release the native,
+  // OS-specific, audio capture thread as soon as possible to ensure that it
+  // can go back to sleep and be prepared to deliver an new captured audio
+  // packet.
+  void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
+
+  void SetTransportOverhead(size_t transport_overhead_per_packet);
+
+  // From OverheadObserver in the RTP/RTCP module
+  void OnOverheadChanged(size_t overhead_bytes_per_packet) override;
+
+  // The existence of this function alongside OnUplinkPacketLossRate is
+  // a compromise. We want the encoder to be agnostic of the PLR source, but
+  // we also don't want it to receive conflicting information from TWCC and
+  // from RTCP-XR.
+  void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
+
+  void OnRecoverableUplinkPacketLossRate(float recoverable_packet_loss_rate);
+
+  int64_t GetRTT() const;
+
+  // E2EE Custom Audio Frame Encryption
+  void SetFrameEncryptor(FrameEncryptorInterface* frame_encryptor);
+
+ private:
+  class ProcessAndEncodeAudioTask;
+
+  void Init();
+  void Terminate();
+
+  void OnUplinkPacketLossRate(float packet_loss_rate);
+  bool InputMute() const;
+
+  int ResendPackets(const uint16_t* sequence_numbers, int length);
+
+  int SetSendRtpHeaderExtension(bool enable,
+                                RTPExtensionType type,
+                                unsigned char id);
+
+  void UpdateOverheadForEncoder()
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
+
+  int GetRtpTimestampRateHz() const;
+
+  // Called on the encoder task queue when a new input audio frame is ready
+  // for encoding.
+  void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input);
+
+  rtc::CriticalSection _callbackCritSect;
+  rtc::CriticalSection volume_settings_critsect_;
+
+  ChannelSendState channel_state_;
+
+  RtcEventLog* const event_log_;
+
+  std::unique_ptr<RtpRtcp> _rtpRtcpModule;
+
+  std::unique_ptr<AudioCodingModule> audio_coding_;
+  uint32_t _timeStamp RTC_GUARDED_BY(encoder_queue_);
+
+  uint16_t send_sequence_number_;
+
+  // uses
+  ProcessThread* _moduleProcessThreadPtr;
+  Transport* _transportPtr;  // WebRtc socket or external transport
+  RmsLevel rms_level_ RTC_GUARDED_BY(encoder_queue_);
+  bool input_mute_ RTC_GUARDED_BY(volume_settings_critsect_);
+  bool previous_frame_muted_ RTC_GUARDED_BY(encoder_queue_);
+  // VoeRTP_RTCP
+  // TODO(henrika): can today be accessed on the main thread and on the
+  // task queue; hence potential race.
+  bool _includeAudioLevelIndication;
+  size_t transport_overhead_per_packet_
+      RTC_GUARDED_BY(overhead_per_packet_lock_);
+  size_t rtp_overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_);
+  rtc::CriticalSection overhead_per_packet_lock_;
+  // RtcpBandwidthObserver
+  std::unique_ptr<VoERtcpObserver> rtcp_observer_;
+
+  PacketRouter* packet_router_ = nullptr;
+  std::unique_ptr<TransportFeedbackProxy> feedback_observer_proxy_;
+  std::unique_ptr<TransportSequenceNumberProxy> seq_num_allocator_proxy_;
+  std::unique_ptr<RtpPacketSenderProxy> rtp_packet_sender_proxy_;
+  std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
+
+  rtc::ThreadChecker construction_thread_;
+
+  const bool use_twcc_plr_for_ana_;
+
+  rtc::CriticalSection encoder_queue_lock_;
+  bool encoder_queue_is_active_ RTC_GUARDED_BY(encoder_queue_lock_) = false;
+  rtc::TaskQueue* encoder_queue_ = nullptr;
+
+  // E2EE Audio Frame Encryption
+  FrameEncryptorInterface* frame_encryptor_ = nullptr;
+};
+
+}  // namespace voe
+}  // namespace webrtc
+
+#endif  // AUDIO_CHANNEL_SEND_H_
diff --git a/audio/channel_send_proxy.cc b/audio/channel_send_proxy.cc
new file mode 100644
index 0000000..8091bdc
--- /dev/null
+++ b/audio/channel_send_proxy.cc
@@ -0,0 +1,207 @@
+/*
+ *  Copyright (c) 2015 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 "audio/channel_send_proxy.h"
+
+#include <utility>
+
+#include "api/call/audio_sink.h"
+#include "call/rtp_transport_controller_send_interface.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/numerics/safe_minmax.h"
+
+namespace webrtc {
+namespace voe {
+ChannelSendProxy::ChannelSendProxy() {}
+
+ChannelSendProxy::ChannelSendProxy(std::unique_ptr<ChannelSend> channel)
+    : channel_(std::move(channel)) {
+  RTC_DCHECK(channel_);
+  module_process_thread_checker_.DetachFromThread();
+}
+
+ChannelSendProxy::~ChannelSendProxy() {}
+
+void ChannelSendProxy::SetLocalSSRC(uint32_t ssrc) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->SetLocalSSRC(ssrc);
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelSendProxy::SetNACKStatus(bool enable, int max_packets) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetNACKStatus(enable, max_packets);
+}
+
+CallSendStatistics ChannelSendProxy::GetRTCPStatistics() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  CallSendStatistics stats = {0};
+  int error = channel_->GetRTPStatistics(stats);
+  RTC_DCHECK_EQ(0, error);
+  return stats;
+}
+
+void ChannelSendProxy::RegisterTransport(Transport* transport) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->RegisterTransport(transport);
+}
+
+bool ChannelSendProxy::ReceivedRTCPPacket(const uint8_t* packet,
+                                          size_t length) {
+  // May be called on either worker thread or network thread.
+  return channel_->ReceivedRTCPPacket(packet, length) == 0;
+}
+
+bool ChannelSendProxy::SetEncoder(int payload_type,
+                                  std::unique_ptr<AudioEncoder> encoder) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->SetEncoder(payload_type, std::move(encoder));
+}
+
+void ChannelSendProxy::ModifyEncoder(
+    rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->ModifyEncoder(modifier);
+}
+
+void ChannelSendProxy::SetRTCPStatus(bool enable) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetRTCPStatus(enable);
+}
+
+void ChannelSendProxy::SetMid(const std::string& mid, int extension_id) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetMid(mid, extension_id);
+}
+
+void ChannelSendProxy::SetRTCP_CNAME(const std::string& c_name) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  // Note: VoERTP_RTCP::SetRTCP_CNAME() accepts a char[256] array.
+  std::string c_name_limited = c_name.substr(0, 255);
+  int error = channel_->SetRTCP_CNAME(c_name_limited.c_str());
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelSendProxy::SetSendAudioLevelIndicationStatus(bool enable, int id) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->SetSendAudioLevelIndicationStatus(enable, id);
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelSendProxy::EnableSendTransportSequenceNumber(int id) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->EnableSendTransportSequenceNumber(id);
+}
+
+void ChannelSendProxy::RegisterSenderCongestionControlObjects(
+    RtpTransportControllerSendInterface* transport,
+    RtcpBandwidthObserver* bandwidth_observer) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->RegisterSenderCongestionControlObjects(transport,
+                                                   bandwidth_observer);
+}
+
+void ChannelSendProxy::ResetSenderCongestionControlObjects() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->ResetSenderCongestionControlObjects();
+}
+
+std::vector<ReportBlock> ChannelSendProxy::GetRemoteRTCPReportBlocks() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  std::vector<webrtc::ReportBlock> blocks;
+  int error = channel_->GetRemoteRTCPReportBlocks(&blocks);
+  RTC_DCHECK_EQ(0, error);
+  return blocks;
+}
+
+ANAStats ChannelSendProxy::GetANAStatistics() const {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->GetANAStatistics();
+}
+
+bool ChannelSendProxy::SetSendTelephoneEventPayloadType(int payload_type,
+                                                        int payload_frequency) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->SetSendTelephoneEventPayloadType(payload_type,
+                                                    payload_frequency) == 0;
+}
+
+bool ChannelSendProxy::SendTelephoneEventOutband(int event, int duration_ms) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  return channel_->SendTelephoneEventOutband(event, duration_ms) == 0;
+}
+
+void ChannelSendProxy::SetBitrate(int bitrate_bps,
+                                  int64_t probing_interval_ms) {
+  // This method can be called on the worker thread, module process thread
+  // or on a TaskQueue via VideoSendStreamImpl::OnEncoderConfigurationChanged.
+  // TODO(solenberg): Figure out a good way to check this or enforce calling
+  // rules.
+  // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread() ||
+  //            module_process_thread_checker_.CalledOnValidThread());
+  channel_->SetBitRate(bitrate_bps, probing_interval_ms);
+}
+
+void ChannelSendProxy::SetInputMute(bool muted) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetInputMute(muted);
+}
+
+void ChannelSendProxy::ProcessAndEncodeAudio(
+    std::unique_ptr<AudioFrame> audio_frame) {
+  RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
+  return channel_->ProcessAndEncodeAudio(std::move(audio_frame));
+}
+
+void ChannelSendProxy::SetTransportOverhead(int transport_overhead_per_packet) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetTransportOverhead(transport_overhead_per_packet);
+}
+
+RtpRtcp* ChannelSendProxy::GetRtpRtcp() const {
+  RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread());
+  return channel_->GetRtpRtcp();
+}
+
+void ChannelSendProxy::OnTwccBasedUplinkPacketLossRate(float packet_loss_rate) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->OnTwccBasedUplinkPacketLossRate(packet_loss_rate);
+}
+
+void ChannelSendProxy::OnRecoverableUplinkPacketLossRate(
+    float recoverable_packet_loss_rate) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->OnRecoverableUplinkPacketLossRate(recoverable_packet_loss_rate);
+}
+
+void ChannelSendProxy::StartSend() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  int error = channel_->StartSend();
+  RTC_DCHECK_EQ(0, error);
+}
+
+void ChannelSendProxy::StopSend() {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->StopSend();
+}
+
+ChannelSend* ChannelSendProxy::GetChannel() const {
+  return channel_.get();
+}
+
+void ChannelSendProxy::SetFrameEncryptor(
+    FrameEncryptorInterface* frame_encryptor) {
+  RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
+  channel_->SetFrameEncryptor(frame_encryptor);
+}
+
+}  // namespace voe
+}  // namespace webrtc
diff --git a/audio/channel_proxy.h b/audio/channel_send_proxy.h
similarity index 60%
rename from audio/channel_proxy.h
rename to audio/channel_send_proxy.h
index f82c1fd..1b8b4a0 100644
--- a/audio/channel_proxy.h
+++ b/audio/channel_send_proxy.h
@@ -8,51 +8,47 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#ifndef AUDIO_CHANNEL_PROXY_H_
-#define AUDIO_CHANNEL_PROXY_H_
+#ifndef AUDIO_CHANNEL_SEND_PROXY_H_
+#define AUDIO_CHANNEL_SEND_PROXY_H_
 
-#include <map>
 #include <memory>
 #include <string>
 #include <vector>
 
-#include "api/audio/audio_mixer.h"
 #include "api/audio_codecs/audio_encoder.h"
-#include "api/rtpreceiverinterface.h"
-#include "audio/channel.h"
-#include "call/rtp_packet_sink_interface.h"
+#include "audio/channel_send.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/race_checker.h"
 #include "rtc_base/thread_checker.h"
 
 namespace webrtc {
 
-class AudioSinkInterface;
-class PacketRouter;
-class RtcEventLog;
+class FrameEncryptorInterface;
 class RtcpBandwidthObserver;
-class RtcpRttStats;
-class RtpPacketSender;
-class RtpPacketReceived;
 class RtpRtcp;
 class RtpTransportControllerSendInterface;
 class Transport;
-class TransportFeedbackObserver;
 
 namespace voe {
 
 // This class provides the "view" of a voe::Channel that we need to implement
-// webrtc::AudioSendStream and webrtc::AudioReceiveStream. It serves two
-// purposes:
+// webrtc::AudioSendStream. It serves two purposes:
 //  1. Allow mocking just the interfaces used, instead of the entire
 //     voe::Channel class.
 //  2. Provide a refined interface for the stream classes, including assumptions
 //     on return values and input adaptation.
-class ChannelProxy : public RtpPacketSinkInterface {
+class ChannelSendProxy {
  public:
-  ChannelProxy();
-  explicit ChannelProxy(std::unique_ptr<Channel> channel);
-  virtual ~ChannelProxy();
+  ChannelSendProxy();
+  explicit ChannelSendProxy(std::unique_ptr<ChannelSend> channel);
+  virtual ~ChannelSendProxy();
+
+  // Shared with ChannelReceiveProxy
+  virtual void SetLocalSSRC(uint32_t ssrc);
+  virtual void SetNACKStatus(bool enable, int max_packets);
+  virtual CallSendStatistics GetRTCPStatistics() const;
+  virtual void RegisterTransport(Transport* transport);
+  virtual bool ReceivedRTCPPacket(const uint8_t* packet, size_t length);
 
   virtual bool SetEncoder(int payload_type,
                           std::unique_ptr<AudioEncoder> encoder);
@@ -60,66 +56,37 @@
       rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> modifier);
 
   virtual void SetRTCPStatus(bool enable);
-  virtual void SetLocalSSRC(uint32_t ssrc);
   virtual void SetMid(const std::string& mid, int extension_id);
   virtual void SetRTCP_CNAME(const std::string& c_name);
-  virtual void SetNACKStatus(bool enable, int max_packets);
   virtual void SetSendAudioLevelIndicationStatus(bool enable, int id);
   virtual void EnableSendTransportSequenceNumber(int id);
   virtual void RegisterSenderCongestionControlObjects(
       RtpTransportControllerSendInterface* transport,
       RtcpBandwidthObserver* bandwidth_observer);
-  virtual void RegisterReceiverCongestionControlObjects(
-      PacketRouter* packet_router);
   virtual void ResetSenderCongestionControlObjects();
-  virtual void ResetReceiverCongestionControlObjects();
-  virtual CallStatistics GetRTCPStatistics() const;
   virtual std::vector<ReportBlock> GetRemoteRTCPReportBlocks() const;
-  virtual NetworkStatistics GetNetworkStatistics() const;
-  virtual AudioDecodingCallStats GetDecodingCallStatistics() const;
   virtual ANAStats GetANAStatistics() const;
-  virtual int GetSpeechOutputLevelFullRange() const;
-  // See description of "totalAudioEnergy" in the WebRTC stats spec:
-  // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy
-  virtual double GetTotalOutputEnergy() const;
-  virtual double GetTotalOutputDuration() const;
-  virtual uint32_t GetDelayEstimate() const;
   virtual bool SetSendTelephoneEventPayloadType(int payload_type,
                                                 int payload_frequency);
   virtual bool SendTelephoneEventOutband(int event, int duration_ms);
   virtual void SetBitrate(int bitrate_bps, int64_t probing_interval_ms);
-  virtual void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
-  virtual void SetSink(AudioSinkInterface* sink);
   virtual void SetInputMute(bool muted);
-  virtual void RegisterTransport(Transport* transport);
 
-  // Implements RtpPacketSinkInterface
-  void OnRtpPacket(const RtpPacketReceived& packet) override;
-  virtual bool ReceivedRTCPPacket(const uint8_t* packet, size_t length);
-  virtual void SetChannelOutputVolumeScaling(float scaling);
-  virtual AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
-      int sample_rate_hz,
-      AudioFrame* audio_frame);
-  virtual int PreferredSampleRate() const;
   virtual void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame);
   virtual void SetTransportOverhead(int transport_overhead_per_packet);
-  virtual void AssociateSendChannel(const ChannelProxy& send_channel_proxy);
-  virtual void DisassociateSendChannel();
   virtual RtpRtcp* GetRtpRtcp() const;
 
-  // Produces the transport-related timestamps; current_delay_ms is left unset.
-  absl::optional<Syncable::Info> GetSyncInfo() const;
-  virtual uint32_t GetPlayoutTimestamp() const;
-  virtual void SetMinimumPlayoutDelay(int delay_ms);
-  virtual bool GetRecCodec(CodecInst* codec_inst) const;
   virtual void OnTwccBasedUplinkPacketLossRate(float packet_loss_rate);
   virtual void OnRecoverableUplinkPacketLossRate(
       float recoverable_packet_loss_rate);
-  virtual std::vector<webrtc::RtpSource> GetSources() const;
   virtual void StartSend();
   virtual void StopSend();
-  virtual void StartPlayout();
-  virtual void StopPlayout();
+
+  // Needed by ChannelReceiveProxy::AssociateSendChannel.
+  virtual ChannelSend* GetChannel() const;
+
+  // E2EE Custom Audio Frame Encryption (Optional)
+  virtual void SetFrameEncryptor(FrameEncryptorInterface* frame_encryptor);
 
  private:
   // Thread checkers document and lock usage of some methods on voe::Channel to
@@ -134,11 +101,11 @@
   // audio thread to another, but access is still sequential.
   rtc::RaceChecker audio_thread_race_checker_;
   rtc::RaceChecker video_capture_thread_race_checker_;
-  std::unique_ptr<Channel> channel_;
+  std::unique_ptr<ChannelSend> channel_;
 
-  RTC_DISALLOW_COPY_AND_ASSIGN(ChannelProxy);
+  RTC_DISALLOW_COPY_AND_ASSIGN(ChannelSendProxy);
 };
 }  // namespace voe
 }  // namespace webrtc
 
-#endif  // AUDIO_CHANNEL_PROXY_H_
+#endif  // AUDIO_CHANNEL_SEND_PROXY_H_
diff --git a/audio/mock_voe_channel_proxy.h b/audio/mock_voe_channel_proxy.h
index f6a2637..910858f 100644
--- a/audio/mock_voe_channel_proxy.h
+++ b/audio/mock_voe_channel_proxy.h
@@ -16,14 +16,50 @@
 #include <string>
 #include <vector>
 
-#include "audio/channel_proxy.h"
+#include "audio/channel_receive_proxy.h"
+#include "audio/channel_send_proxy.h"
 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "test/gmock.h"
 
 namespace webrtc {
 namespace test {
 
-class MockVoEChannelProxy : public voe::ChannelProxy {
+class MockChannelReceiveProxy : public voe::ChannelReceiveProxy {
+ public:
+  MOCK_METHOD1(SetLocalSSRC, void(uint32_t ssrc));
+  MOCK_METHOD2(SetNACKStatus, void(bool enable, int max_packets));
+  MOCK_METHOD1(RegisterReceiverCongestionControlObjects,
+               void(PacketRouter* packet_router));
+  MOCK_METHOD0(ResetReceiverCongestionControlObjects, void());
+  MOCK_CONST_METHOD0(GetRTCPStatistics, CallReceiveStatistics());
+  MOCK_CONST_METHOD0(GetNetworkStatistics, NetworkStatistics());
+  MOCK_CONST_METHOD0(GetDecodingCallStatistics, AudioDecodingCallStats());
+  MOCK_CONST_METHOD0(GetSpeechOutputLevelFullRange, int());
+  MOCK_CONST_METHOD0(GetTotalOutputEnergy, double());
+  MOCK_CONST_METHOD0(GetTotalOutputDuration, double());
+  MOCK_CONST_METHOD0(GetDelayEstimate, uint32_t());
+  MOCK_METHOD1(SetSink, void(AudioSinkInterface* sink));
+  MOCK_METHOD1(OnRtpPacket, void(const RtpPacketReceived& packet));
+  MOCK_METHOD2(ReceivedRTCPPacket, bool(const uint8_t* packet, size_t length));
+  MOCK_METHOD1(SetChannelOutputVolumeScaling, void(float scaling));
+  MOCK_METHOD2(GetAudioFrameWithInfo,
+               AudioMixer::Source::AudioFrameInfo(int sample_rate_hz,
+                                                  AudioFrame* audio_frame));
+  MOCK_CONST_METHOD0(PreferredSampleRate, int());
+  MOCK_METHOD1(AssociateSendChannel,
+               void(const voe::ChannelSendProxy& send_channel_proxy));
+  MOCK_METHOD0(DisassociateSendChannel, void());
+  MOCK_CONST_METHOD0(GetPlayoutTimestamp, uint32_t());
+  MOCK_METHOD1(SetMinimumPlayoutDelay, void(int delay_ms));
+  MOCK_CONST_METHOD1(GetRecCodec, bool(CodecInst* codec_inst));
+  MOCK_METHOD1(SetReceiveCodecs,
+               void(const std::map<int, SdpAudioFormat>& codecs));
+  MOCK_CONST_METHOD0(GetSources, std::vector<RtpSource>());
+  MOCK_METHOD0(StartPlayout, void());
+  MOCK_METHOD0(StopPlayout, void());
+};
+
+class MockChannelSendProxy : public voe::ChannelSendProxy {
  public:
   // GMock doesn't like move-only types, like std::unique_ptr.
   virtual bool SetEncoder(int payload_type,
@@ -44,33 +80,17 @@
   MOCK_METHOD2(RegisterSenderCongestionControlObjects,
                void(RtpTransportControllerSendInterface* transport,
                     RtcpBandwidthObserver* bandwidth_observer));
-  MOCK_METHOD1(RegisterReceiverCongestionControlObjects,
-               void(PacketRouter* packet_router));
   MOCK_METHOD0(ResetSenderCongestionControlObjects, void());
-  MOCK_METHOD0(ResetReceiverCongestionControlObjects, void());
-  MOCK_CONST_METHOD0(GetRTCPStatistics, CallStatistics());
+  MOCK_CONST_METHOD0(GetRTCPStatistics, CallSendStatistics());
   MOCK_CONST_METHOD0(GetRemoteRTCPReportBlocks, std::vector<ReportBlock>());
-  MOCK_CONST_METHOD0(GetNetworkStatistics, NetworkStatistics());
-  MOCK_CONST_METHOD0(GetDecodingCallStatistics, AudioDecodingCallStats());
   MOCK_CONST_METHOD0(GetANAStatistics, ANAStats());
-  MOCK_CONST_METHOD0(GetSpeechOutputLevelFullRange, int());
-  MOCK_CONST_METHOD0(GetTotalOutputEnergy, double());
-  MOCK_CONST_METHOD0(GetTotalOutputDuration, double());
-  MOCK_CONST_METHOD0(GetDelayEstimate, uint32_t());
   MOCK_METHOD2(SetSendTelephoneEventPayloadType,
                bool(int payload_type, int payload_frequency));
   MOCK_METHOD2(SendTelephoneEventOutband, bool(int event, int duration_ms));
   MOCK_METHOD2(SetBitrate, void(int bitrate_bps, int64_t probing_interval_ms));
-  MOCK_METHOD1(SetSink, void(AudioSinkInterface* sink));
   MOCK_METHOD1(SetInputMute, void(bool muted));
   MOCK_METHOD1(RegisterTransport, void(Transport* transport));
-  MOCK_METHOD1(OnRtpPacket, void(const RtpPacketReceived& packet));
   MOCK_METHOD2(ReceivedRTCPPacket, bool(const uint8_t* packet, size_t length));
-  MOCK_METHOD1(SetChannelOutputVolumeScaling, void(float scaling));
-  MOCK_METHOD2(GetAudioFrameWithInfo,
-               AudioMixer::Source::AudioFrameInfo(int sample_rate_hz,
-                                                  AudioFrame* audio_frame));
-  MOCK_CONST_METHOD0(PreferredSampleRate, int());
   // GMock doesn't like move-only types, like std::unique_ptr.
   virtual void ProcessAndEncodeAudio(std::unique_ptr<AudioFrame> audio_frame) {
     ProcessAndEncodeAudioForMock(&audio_frame);
@@ -78,23 +98,14 @@
   MOCK_METHOD1(ProcessAndEncodeAudioForMock,
                void(std::unique_ptr<AudioFrame>* audio_frame));
   MOCK_METHOD1(SetTransportOverhead, void(int transport_overhead_per_packet));
-  MOCK_METHOD1(AssociateSendChannel,
-               void(const ChannelProxy& send_channel_proxy));
-  MOCK_METHOD0(DisassociateSendChannel, void());
   MOCK_CONST_METHOD0(GetRtpRtcp, RtpRtcp*());
-  MOCK_CONST_METHOD0(GetPlayoutTimestamp, uint32_t());
-  MOCK_METHOD1(SetMinimumPlayoutDelay, void(int delay_ms));
-  MOCK_CONST_METHOD1(GetRecCodec, bool(CodecInst* codec_inst));
-  MOCK_METHOD1(SetReceiveCodecs,
-               void(const std::map<int, SdpAudioFormat>& codecs));
   MOCK_METHOD1(OnTwccBasedUplinkPacketLossRate, void(float packet_loss_rate));
   MOCK_METHOD1(OnRecoverableUplinkPacketLossRate,
                void(float recoverable_packet_loss_rate));
-  MOCK_CONST_METHOD0(GetSources, std::vector<RtpSource>());
   MOCK_METHOD0(StartSend, void());
   MOCK_METHOD0(StopSend, void());
-  MOCK_METHOD0(StartPlayout, void());
-  MOCK_METHOD0(StopPlayout, void());
+  MOCK_METHOD1(SetFrameEncryptor,
+               void(FrameEncryptorInterface* frame_encryptor));
 };
 }  // namespace test
 }  // namespace webrtc
diff --git a/audio/null_audio_poller.cc b/audio/null_audio_poller.cc
index c22b3d8..bd5317b 100644
--- a/audio/null_audio_poller.cc
+++ b/audio/null_audio_poller.cc
@@ -11,6 +11,7 @@
 #include "audio/null_audio_poller.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/thread.h"
+#include "rtc_base/timeutils.h"  // for TimeMillis
 
 namespace webrtc {
 namespace internal {
diff --git a/audio/transport_feedback_packet_loss_tracker_unittest.cc b/audio/transport_feedback_packet_loss_tracker_unittest.cc
index 2f9bf68..f522635 100644
--- a/audio/transport_feedback_packet_loss_tracker_unittest.cc
+++ b/audio/transport_feedback_packet_loss_tracker_unittest.cc
@@ -116,8 +116,6 @@
 
  private:
   int64_t time_ms_{0};
-
-  RTC_DISALLOW_COPY_AND_ASSIGN(TransportFeedbackPacketLossTrackerTest);
 };
 
 }  // namespace
diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn
index 50bd0f4..abcfe9a 100644
--- a/common_audio/BUILD.gn
+++ b/common_audio/BUILD.gn
@@ -15,16 +15,10 @@
   sources = [
     "audio_converter.cc",
     "audio_converter.h",
-    "audio_ring_buffer.cc",
-    "audio_ring_buffer.h",
     "audio_util.cc",
-    "blocker.cc",
-    "blocker.h",
     "channel_buffer.cc",
     "channel_buffer.h",
     "include/audio_util.h",
-    "lapped_transform.cc",
-    "lapped_transform.h",
     "real_fourier.cc",
     "real_fourier.h",
     "real_fourier_ooura.cc",
@@ -63,6 +57,8 @@
     "../system_wrappers",
     "../system_wrappers:cpu_features_api",
     "third_party/fft4g:fft4g",
+    "//third_party/abseil-cpp/absl/container:inlined_vector",
+    "//third_party/abseil-cpp/absl/memory:memory",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 
@@ -349,12 +345,9 @@
 
     sources = [
       "audio_converter_unittest.cc",
-      "audio_ring_buffer_unittest.cc",
       "audio_util_unittest.cc",
-      "blocker_unittest.cc",
       "channel_buffer_unittest.cc",
       "fir_filter_unittest.cc",
-      "lapped_transform_unittest.cc",
       "real_fourier_unittest.cc",
       "resampler/push_resampler_unittest.cc",
       "resampler/push_sinc_resampler_unittest.cc",
diff --git a/common_audio/audio_ring_buffer.cc b/common_audio/audio_ring_buffer.cc
deleted file mode 100644
index b3bdc25..0000000
--- a/common_audio/audio_ring_buffer.cc
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- *  Copyright (c) 2015 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 "common_audio/audio_ring_buffer.h"
-
-#include "common_audio/ring_buffer.h"
-#include "rtc_base/checks.h"
-
-// This is a simple multi-channel wrapper over the ring_buffer.h C interface.
-
-namespace webrtc {
-
-AudioRingBuffer::AudioRingBuffer(size_t channels, size_t max_frames) {
-  buffers_.reserve(channels);
-  for (size_t i = 0; i < channels; ++i)
-    buffers_.push_back(WebRtc_CreateBuffer(max_frames, sizeof(float)));
-}
-
-AudioRingBuffer::~AudioRingBuffer() {
-  for (auto* buf : buffers_)
-    WebRtc_FreeBuffer(buf);
-}
-
-void AudioRingBuffer::Write(const float* const* data,
-                            size_t channels,
-                            size_t frames) {
-  RTC_DCHECK_EQ(buffers_.size(), channels);
-  for (size_t i = 0; i < channels; ++i) {
-    const size_t written = WebRtc_WriteBuffer(buffers_[i], data[i], frames);
-    RTC_CHECK_EQ(written, frames);
-  }
-}
-
-void AudioRingBuffer::Read(float* const* data, size_t channels, size_t frames) {
-  RTC_DCHECK_EQ(buffers_.size(), channels);
-  for (size_t i = 0; i < channels; ++i) {
-    const size_t read =
-        WebRtc_ReadBuffer(buffers_[i], nullptr, data[i], frames);
-    RTC_CHECK_EQ(read, frames);
-  }
-}
-
-size_t AudioRingBuffer::ReadFramesAvailable() const {
-  // All buffers have the same amount available.
-  return WebRtc_available_read(buffers_[0]);
-}
-
-size_t AudioRingBuffer::WriteFramesAvailable() const {
-  // All buffers have the same amount available.
-  return WebRtc_available_write(buffers_[0]);
-}
-
-void AudioRingBuffer::MoveReadPositionForward(size_t frames) {
-  for (auto* buf : buffers_) {
-    const size_t moved =
-        static_cast<size_t>(WebRtc_MoveReadPtr(buf, static_cast<int>(frames)));
-    RTC_CHECK_EQ(moved, frames);
-  }
-}
-
-void AudioRingBuffer::MoveReadPositionBackward(size_t frames) {
-  for (auto* buf : buffers_) {
-    const size_t moved = static_cast<size_t>(
-        -WebRtc_MoveReadPtr(buf, -static_cast<int>(frames)));
-    RTC_CHECK_EQ(moved, frames);
-  }
-}
-
-}  // namespace webrtc
diff --git a/common_audio/audio_ring_buffer.h b/common_audio/audio_ring_buffer.h
deleted file mode 100644
index 67d24f0..0000000
--- a/common_audio/audio_ring_buffer.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- *  Copyright (c) 2015 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 COMMON_AUDIO_AUDIO_RING_BUFFER_H_
-#define COMMON_AUDIO_AUDIO_RING_BUFFER_H_
-
-#include <stddef.h>
-
-#include <memory>
-#include <vector>
-
-struct RingBuffer;
-
-namespace webrtc {
-
-// A ring buffer tailored for float deinterleaved audio. Any operation that
-// cannot be performed as requested will cause a crash (e.g. insufficient data
-// in the buffer to fulfill a read request.)
-class AudioRingBuffer final {
- public:
-  // Specify the number of channels and maximum number of frames the buffer will
-  // contain.
-  AudioRingBuffer(size_t channels, size_t max_frames);
-  ~AudioRingBuffer();
-
-  // Copies |data| to the buffer and advances the write pointer. |channels| must
-  // be the same as at creation time.
-  void Write(const float* const* data, size_t channels, size_t frames);
-
-  // Copies from the buffer to |data| and advances the read pointer. |channels|
-  // must be the same as at creation time.
-  void Read(float* const* data, size_t channels, size_t frames);
-
-  size_t ReadFramesAvailable() const;
-  size_t WriteFramesAvailable() const;
-
-  // Moves the read position. The forward version advances the read pointer
-  // towards the write pointer and the backward verison withdraws the read
-  // pointer away from the write pointer (i.e. flushing and stuffing the buffer
-  // respectively.)
-  void MoveReadPositionForward(size_t frames);
-  void MoveReadPositionBackward(size_t frames);
-
- private:
-  // TODO(kwiberg): Use std::vector<std::unique_ptr<RingBuffer>> instead.
-  std::vector<RingBuffer*> buffers_;
-};
-
-}  // namespace webrtc
-
-#endif  // COMMON_AUDIO_AUDIO_RING_BUFFER_H_
diff --git a/common_audio/audio_ring_buffer_unittest.cc b/common_audio/audio_ring_buffer_unittest.cc
deleted file mode 100644
index d411195..0000000
--- a/common_audio/audio_ring_buffer_unittest.cc
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- *  Copyright (c) 2015 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 <memory>
-
-#include "common_audio/audio_ring_buffer.h"
-
-#include "common_audio/channel_buffer.h"
-#include "test/gtest.h"
-
-namespace webrtc {
-
-class AudioRingBufferTest
-    : public ::testing::TestWithParam< ::testing::tuple<int, int, int, int> > {
-};
-
-void ReadAndWriteTest(const ChannelBuffer<float>& input,
-                      size_t num_write_chunk_frames,
-                      size_t num_read_chunk_frames,
-                      size_t buffer_frames,
-                      ChannelBuffer<float>* output) {
-  const size_t num_channels = input.num_channels();
-  const size_t total_frames = input.num_frames();
-  AudioRingBuffer buf(num_channels, buffer_frames);
-  std::unique_ptr<float* []> slice(new float*[num_channels]);
-
-  size_t input_pos = 0;
-  size_t output_pos = 0;
-  while (input_pos + buf.WriteFramesAvailable() < total_frames) {
-    // Write until the buffer is as full as possible.
-    while (buf.WriteFramesAvailable() >= num_write_chunk_frames) {
-      buf.Write(input.Slice(slice.get(), input_pos), num_channels,
-                num_write_chunk_frames);
-      input_pos += num_write_chunk_frames;
-    }
-    // Read until the buffer is as empty as possible.
-    while (buf.ReadFramesAvailable() >= num_read_chunk_frames) {
-      EXPECT_LT(output_pos, total_frames);
-      buf.Read(output->Slice(slice.get(), output_pos), num_channels,
-               num_read_chunk_frames);
-      output_pos += num_read_chunk_frames;
-    }
-  }
-
-  // Write and read the last bit.
-  if (input_pos < total_frames) {
-    buf.Write(input.Slice(slice.get(), input_pos), num_channels,
-              total_frames - input_pos);
-  }
-  if (buf.ReadFramesAvailable()) {
-    buf.Read(output->Slice(slice.get(), output_pos), num_channels,
-             buf.ReadFramesAvailable());
-  }
-  EXPECT_EQ(0u, buf.ReadFramesAvailable());
-}
-
-TEST_P(AudioRingBufferTest, ReadDataMatchesWrittenData) {
-  const size_t kFrames = 5000;
-  const size_t num_channels = ::testing::get<3>(GetParam());
-
-  // Initialize the input data to an increasing sequence.
-  ChannelBuffer<float> input(kFrames, static_cast<int>(num_channels));
-  for (size_t i = 0; i < num_channels; ++i)
-    for (size_t j = 0; j < kFrames; ++j)
-      input.channels()[i][j] = (i + 1) * (j + 1);
-
-  ChannelBuffer<float> output(kFrames, static_cast<int>(num_channels));
-  ReadAndWriteTest(input, ::testing::get<0>(GetParam()),
-                   ::testing::get<1>(GetParam()), ::testing::get<2>(GetParam()),
-                   &output);
-
-  // Verify the read data matches the input.
-  for (size_t i = 0; i < num_channels; ++i)
-    for (size_t j = 0; j < kFrames; ++j)
-      EXPECT_EQ(input.channels()[i][j], output.channels()[i][j]);
-}
-
-INSTANTIATE_TEST_CASE_P(
-    AudioRingBufferTest,
-    AudioRingBufferTest,
-    ::testing::Combine(::testing::Values(10, 20, 42),  // num_write_chunk_frames
-                       ::testing::Values(1, 10, 17),   // num_read_chunk_frames
-                       ::testing::Values(100, 256),    // buffer_frames
-                       ::testing::Values(1, 4)));      // num_channels
-
-TEST_F(AudioRingBufferTest, MoveReadPosition) {
-  const size_t kNumChannels = 1;
-  const float kInputArray[] = {1, 2, 3, 4};
-  const size_t kNumFrames = sizeof(kInputArray) / sizeof(*kInputArray);
-  ChannelBuffer<float> input(kNumFrames, kNumChannels);
-  input.SetDataForTesting(kInputArray, kNumFrames);
-  AudioRingBuffer buf(kNumChannels, kNumFrames);
-  buf.Write(input.channels(), kNumChannels, kNumFrames);
-
-  buf.MoveReadPositionForward(3);
-  ChannelBuffer<float> output(1, kNumChannels);
-  buf.Read(output.channels(), kNumChannels, 1);
-  EXPECT_EQ(4, output.channels()[0][0]);
-  buf.MoveReadPositionBackward(3);
-  buf.Read(output.channels(), kNumChannels, 1);
-  EXPECT_EQ(2, output.channels()[0][0]);
-}
-
-}  // namespace webrtc
diff --git a/common_audio/blocker.cc b/common_audio/blocker.cc
deleted file mode 100644
index 3dc8ed8..0000000
--- a/common_audio/blocker.cc
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- *  Copyright (c) 2014 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 "common_audio/blocker.h"
-
-#include <string.h>
-
-#include "rtc_base/checks.h"
-
-namespace {
-
-// Adds |a| and |b| frame by frame into |result| (basically matrix addition).
-void AddFrames(const float* const* a,
-               size_t a_start_index,
-               const float* const* b,
-               int b_start_index,
-               size_t num_frames,
-               size_t num_channels,
-               float* const* result,
-               size_t result_start_index) {
-  for (size_t i = 0; i < num_channels; ++i) {
-    for (size_t j = 0; j < num_frames; ++j) {
-      result[i][j + result_start_index] =
-          a[i][j + a_start_index] + b[i][j + b_start_index];
-    }
-  }
-}
-
-// Copies |src| into |dst| channel by channel.
-void CopyFrames(const float* const* src,
-                size_t src_start_index,
-                size_t num_frames,
-                size_t num_channels,
-                float* const* dst,
-                size_t dst_start_index) {
-  for (size_t i = 0; i < num_channels; ++i) {
-    memcpy(&dst[i][dst_start_index], &src[i][src_start_index],
-           num_frames * sizeof(dst[i][dst_start_index]));
-  }
-}
-
-// Moves |src| into |dst| channel by channel.
-void MoveFrames(const float* const* src,
-                size_t src_start_index,
-                size_t num_frames,
-                size_t num_channels,
-                float* const* dst,
-                size_t dst_start_index) {
-  for (size_t i = 0; i < num_channels; ++i) {
-    memmove(&dst[i][dst_start_index], &src[i][src_start_index],
-            num_frames * sizeof(dst[i][dst_start_index]));
-  }
-}
-
-void ZeroOut(float* const* buffer,
-             size_t starting_idx,
-             size_t num_frames,
-             size_t num_channels) {
-  for (size_t i = 0; i < num_channels; ++i) {
-    memset(&buffer[i][starting_idx], 0,
-           num_frames * sizeof(buffer[i][starting_idx]));
-  }
-}
-
-// Pointwise multiplies each channel of |frames| with |window|. Results are
-// stored in |frames|.
-void ApplyWindow(const float* window,
-                 size_t num_frames,
-                 size_t num_channels,
-                 float* const* frames) {
-  for (size_t i = 0; i < num_channels; ++i) {
-    for (size_t j = 0; j < num_frames; ++j) {
-      frames[i][j] = frames[i][j] * window[j];
-    }
-  }
-}
-
-size_t gcd(size_t a, size_t b) {
-  size_t tmp;
-  while (b) {
-    tmp = a;
-    a = b;
-    b = tmp % b;
-  }
-  return a;
-}
-
-}  // namespace
-
-namespace webrtc {
-
-Blocker::Blocker(size_t chunk_size,
-                 size_t block_size,
-                 size_t num_input_channels,
-                 size_t num_output_channels,
-                 const float* window,
-                 size_t shift_amount,
-                 BlockerCallback* callback)
-    : chunk_size_(chunk_size),
-      block_size_(block_size),
-      num_input_channels_(num_input_channels),
-      num_output_channels_(num_output_channels),
-      initial_delay_(block_size_ - gcd(chunk_size, shift_amount)),
-      frame_offset_(0),
-      input_buffer_(num_input_channels_, chunk_size_ + initial_delay_),
-      output_buffer_(chunk_size_ + initial_delay_, num_output_channels_),
-      input_block_(block_size_, num_input_channels_),
-      output_block_(block_size_, num_output_channels_),
-      window_(new float[block_size_]),
-      shift_amount_(shift_amount),
-      callback_(callback) {
-  RTC_CHECK_LE(num_output_channels_, num_input_channels_);
-  RTC_CHECK_LE(shift_amount_, block_size_);
-
-  memcpy(window_.get(), window, block_size_ * sizeof(*window_.get()));
-  input_buffer_.MoveReadPositionBackward(initial_delay_);
-}
-
-Blocker::~Blocker() = default;
-
-// When block_size < chunk_size the input and output buffers look like this:
-//
-//                      delay*             chunk_size    chunk_size + delay*
-//  buffer: <-------------|---------------------|---------------|>
-//                _a_              _b_                 _c_
-//
-// On each call to ProcessChunk():
-// 1. New input gets read into sections _b_ and _c_ of the input buffer.
-// 2. We block starting from frame_offset.
-// 3. We block until we reach a block |bl| that doesn't contain any frames
-//    from sections _a_ or _b_ of the input buffer.
-// 4. We window the current block, fire the callback for processing, window
-//    again, and overlap/add to the output buffer.
-// 5. We copy sections _a_ and _b_ of the output buffer into output.
-// 6. For both the input and the output buffers, we copy section _c_ into
-//    section _a_.
-// 7. We set the new frame_offset to be the difference between the first frame
-//    of |bl| and the border between sections _b_ and _c_.
-//
-// When block_size > chunk_size the input and output buffers look like this:
-//
-//                   chunk_size               delay*       chunk_size + delay*
-//  buffer: <-------------|---------------------|---------------|>
-//                _a_              _b_                 _c_
-//
-// On each call to ProcessChunk():
-// The procedure is the same as above, except for:
-// 1. New input gets read into section _c_ of the input buffer.
-// 3. We block until we reach a block |bl| that doesn't contain any frames
-//    from section _a_ of the input buffer.
-// 5. We copy section _a_ of the output buffer into output.
-// 6. For both the input and the output buffers, we copy sections _b_ and _c_
-//    into section _a_ and _b_.
-// 7. We set the new frame_offset to be the difference between the first frame
-//    of |bl| and the border between sections _a_ and _b_.
-//
-// * delay here refers to inintial_delay_
-//
-// TODO(claguna): Look at using ring buffers to eliminate some copies.
-void Blocker::ProcessChunk(const float* const* input,
-                           size_t chunk_size,
-                           size_t num_input_channels,
-                           size_t num_output_channels,
-                           float* const* output) {
-  RTC_CHECK_EQ(chunk_size, chunk_size_);
-  RTC_CHECK_EQ(num_input_channels, num_input_channels_);
-  RTC_CHECK_EQ(num_output_channels, num_output_channels_);
-
-  input_buffer_.Write(input, num_input_channels, chunk_size_);
-  size_t first_frame_in_block = frame_offset_;
-
-  // Loop through blocks.
-  while (first_frame_in_block < chunk_size_) {
-    input_buffer_.Read(input_block_.channels(), num_input_channels,
-                       block_size_);
-    input_buffer_.MoveReadPositionBackward(block_size_ - shift_amount_);
-
-    ApplyWindow(window_.get(), block_size_, num_input_channels_,
-                input_block_.channels());
-    callback_->ProcessBlock(input_block_.channels(), block_size_,
-                            num_input_channels_, num_output_channels_,
-                            output_block_.channels());
-    ApplyWindow(window_.get(), block_size_, num_output_channels_,
-                output_block_.channels());
-
-    AddFrames(output_buffer_.channels(), first_frame_in_block,
-              output_block_.channels(), 0, block_size_, num_output_channels_,
-              output_buffer_.channels(), first_frame_in_block);
-
-    first_frame_in_block += shift_amount_;
-  }
-
-  // Copy output buffer to output
-  CopyFrames(output_buffer_.channels(), 0, chunk_size_, num_output_channels_,
-             output, 0);
-
-  // Copy output buffer [chunk_size_, chunk_size_ + initial_delay]
-  // to output buffer [0, initial_delay], zero the rest.
-  MoveFrames(output_buffer_.channels(), chunk_size, initial_delay_,
-             num_output_channels_, output_buffer_.channels(), 0);
-  ZeroOut(output_buffer_.channels(), initial_delay_, chunk_size_,
-          num_output_channels_);
-
-  // Calculate new starting frames.
-  frame_offset_ = first_frame_in_block - chunk_size_;
-}
-
-}  // namespace webrtc
diff --git a/common_audio/blocker.h b/common_audio/blocker.h
deleted file mode 100644
index 9bce896..0000000
--- a/common_audio/blocker.h
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- *  Copyright (c) 2014 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 COMMON_AUDIO_BLOCKER_H_
-#define COMMON_AUDIO_BLOCKER_H_
-
-#include <memory>
-
-#include "common_audio/audio_ring_buffer.h"
-#include "common_audio/channel_buffer.h"
-
-namespace webrtc {
-
-// The callback function to process audio in the time domain. Input has already
-// been windowed, and output will be windowed. The number of input channels
-// must be >= the number of output channels.
-class BlockerCallback {
- public:
-  virtual ~BlockerCallback() {}
-
-  virtual void ProcessBlock(const float* const* input,
-                            size_t num_frames,
-                            size_t num_input_channels,
-                            size_t num_output_channels,
-                            float* const* output) = 0;
-};
-
-// The main purpose of Blocker is to abstract away the fact that often we
-// receive a different number of audio frames than our transform takes. For
-// example, most FFTs work best when the fft-size is a power of 2, but suppose
-// we receive 20ms of audio at a sample rate of 48000. That comes to 960 frames
-// of audio, which is not a power of 2. Blocker allows us to specify the
-// transform and all other necessary processing via the Process() callback
-// function without any constraints on the transform-size
-// (read: |block_size_|) or received-audio-size (read: |chunk_size_|).
-// We handle this for the multichannel audio case, allowing for different
-// numbers of input and output channels (for example, beamforming takes 2 or
-// more input channels and returns 1 output channel). Audio signals are
-// represented as deinterleaved floats in the range [-1, 1].
-//
-// Blocker is responsible for:
-// - blocking audio while handling potential discontinuities on the edges
-//   of chunks
-// - windowing blocks before sending them to Process()
-// - windowing processed blocks, and overlap-adding them together before
-//   sending back a processed chunk
-//
-// To use blocker:
-// 1. Impelment a BlockerCallback object |bc|.
-// 2. Instantiate a Blocker object |b|, passing in |bc|.
-// 3. As you receive audio, call b.ProcessChunk() to get processed audio.
-//
-// A small amount of delay is added to the first received chunk to deal with
-// the difference in chunk/block sizes. This delay is <= chunk_size.
-//
-// Ownership of window is retained by the caller.  That is, Blocker makes a
-// copy of window and does not attempt to delete it.
-class Blocker {
- public:
-  Blocker(size_t chunk_size,
-          size_t block_size,
-          size_t num_input_channels,
-          size_t num_output_channels,
-          const float* window,
-          size_t shift_amount,
-          BlockerCallback* callback);
-  ~Blocker();
-
-  void ProcessChunk(const float* const* input,
-                    size_t chunk_size,
-                    size_t num_input_channels,
-                    size_t num_output_channels,
-                    float* const* output);
-
-  size_t initial_delay() const { return initial_delay_; }
-
- private:
-  const size_t chunk_size_;
-  const size_t block_size_;
-  const size_t num_input_channels_;
-  const size_t num_output_channels_;
-
-  // The number of frames of delay to add at the beginning of the first chunk.
-  const size_t initial_delay_;
-
-  // The frame index into the input buffer where the first block should be read
-  // from. This is necessary because shift_amount_ is not necessarily a
-  // multiple of chunk_size_, so blocks won't line up at the start of the
-  // buffer.
-  size_t frame_offset_;
-
-  // Since blocks nearly always overlap, there are certain blocks that require
-  // frames from the end of one chunk and the beginning of the next chunk. The
-  // input and output buffers are responsible for saving those frames between
-  // calls to ProcessChunk().
-  //
-  // Both contain |initial delay| + |chunk_size| frames. The input is a fairly
-  // standard FIFO, but due to the overlap-add it's harder to use an
-  // AudioRingBuffer for the output.
-  AudioRingBuffer input_buffer_;
-  ChannelBuffer<float> output_buffer_;
-
-  // Space for the input block (can't wrap because of windowing).
-  ChannelBuffer<float> input_block_;
-
-  // Space for the output block (can't wrap because of overlap/add).
-  ChannelBuffer<float> output_block_;
-
-  std::unique_ptr<float[]> window_;
-
-  // The amount of frames between the start of contiguous blocks. For example,
-  // |shift_amount_| = |block_size_| / 2 for a Hann window.
-  size_t shift_amount_;
-
-  BlockerCallback* callback_;
-};
-
-}  // namespace webrtc
-
-#endif  // COMMON_AUDIO_BLOCKER_H_
diff --git a/common_audio/blocker_unittest.cc b/common_audio/blocker_unittest.cc
deleted file mode 100644
index 85a24f6..0000000
--- a/common_audio/blocker_unittest.cc
+++ /dev/null
@@ -1,293 +0,0 @@
-/*
- *  Copyright (c) 2014 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 <memory>
-
-#include "common_audio/blocker.h"
-
-#include "rtc_base/arraysize.h"
-#include "test/gtest.h"
-
-namespace {
-
-// Callback Function to add 3 to every sample in the signal.
-class PlusThreeBlockerCallback : public webrtc::BlockerCallback {
- public:
-  void ProcessBlock(const float* const* input,
-                    size_t num_frames,
-                    size_t num_input_channels,
-                    size_t num_output_channels,
-                    float* const* output) override {
-    for (size_t i = 0; i < num_output_channels; ++i) {
-      for (size_t j = 0; j < num_frames; ++j) {
-        output[i][j] = input[i][j] + 3;
-      }
-    }
-  }
-};
-
-// No-op Callback Function.
-class CopyBlockerCallback : public webrtc::BlockerCallback {
- public:
-  void ProcessBlock(const float* const* input,
-                    size_t num_frames,
-                    size_t num_input_channels,
-                    size_t num_output_channels,
-                    float* const* output) override {
-    for (size_t i = 0; i < num_output_channels; ++i) {
-      for (size_t j = 0; j < num_frames; ++j) {
-        output[i][j] = input[i][j];
-      }
-    }
-  }
-};
-
-}  // namespace
-
-namespace webrtc {
-
-// Tests blocking with a window that multiplies the signal by 2, a callback
-// that adds 3 to each sample in the signal, and different combinations of chunk
-// size, block size, and shift amount.
-class BlockerTest : public ::testing::Test {
- protected:
-  void RunTest(Blocker* blocker,
-               size_t chunk_size,
-               size_t num_frames,
-               const float* const* input,
-               float* const* input_chunk,
-               float* const* output,
-               float* const* output_chunk,
-               size_t num_input_channels,
-               size_t num_output_channels) {
-    size_t start = 0;
-    size_t end = chunk_size - 1;
-    while (end < num_frames) {
-      CopyTo(input_chunk, 0, start, num_input_channels, chunk_size, input);
-      blocker->ProcessChunk(input_chunk, chunk_size, num_input_channels,
-                            num_output_channels, output_chunk);
-      CopyTo(output, start, 0, num_output_channels, chunk_size, output_chunk);
-
-      start += chunk_size;
-      end += chunk_size;
-    }
-  }
-
-  void ValidateSignalEquality(const float* const* expected,
-                              const float* const* actual,
-                              size_t num_channels,
-                              size_t num_frames) {
-    for (size_t i = 0; i < num_channels; ++i) {
-      for (size_t j = 0; j < num_frames; ++j) {
-        EXPECT_FLOAT_EQ(expected[i][j], actual[i][j]);
-      }
-    }
-  }
-
-  void ValidateInitialDelay(const float* const* output,
-                            size_t num_channels,
-                            size_t num_frames,
-                            size_t initial_delay) {
-    for (size_t i = 0; i < num_channels; ++i) {
-      for (size_t j = 0; j < num_frames; ++j) {
-        if (j < initial_delay) {
-          EXPECT_FLOAT_EQ(output[i][j], 0.f);
-        } else {
-          EXPECT_GT(output[i][j], 0.f);
-        }
-      }
-    }
-  }
-
-  static void CopyTo(float* const* dst,
-                     size_t start_index_dst,
-                     size_t start_index_src,
-                     size_t num_channels,
-                     size_t num_frames,
-                     const float* const* src) {
-    for (size_t i = 0; i < num_channels; ++i) {
-      memcpy(&dst[i][start_index_dst], &src[i][start_index_src],
-             num_frames * sizeof(float));
-    }
-  }
-};
-
-TEST_F(BlockerTest, TestBlockerMutuallyPrimeChunkandBlockSize) {
-  const size_t kNumInputChannels = 3;
-  const size_t kNumOutputChannels = 2;
-  const size_t kNumFrames = 10;
-  const size_t kBlockSize = 4;
-  const size_t kChunkSize = 5;
-  const size_t kShiftAmount = 2;
-
-  const float kInput[kNumInputChannels][kNumFrames] = {
-      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
-      {2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
-      {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
-  ChannelBuffer<float> input_cb(kNumFrames, kNumInputChannels);
-  input_cb.SetDataForTesting(kInput[0], sizeof(kInput) / sizeof(**kInput));
-
-  const float kExpectedOutput[kNumInputChannels][kNumFrames] = {
-      {6, 6, 12, 20, 20, 20, 20, 20, 20, 20},
-      {6, 6, 12, 28, 28, 28, 28, 28, 28, 28}};
-  ChannelBuffer<float> expected_output_cb(kNumFrames, kNumInputChannels);
-  expected_output_cb.SetDataForTesting(
-      kExpectedOutput[0], sizeof(kExpectedOutput) / sizeof(**kExpectedOutput));
-
-  const float kWindow[kBlockSize] = {2.f, 2.f, 2.f, 2.f};
-
-  ChannelBuffer<float> actual_output_cb(kNumFrames, kNumOutputChannels);
-  ChannelBuffer<float> input_chunk_cb(kChunkSize, kNumInputChannels);
-  ChannelBuffer<float> output_chunk_cb(kChunkSize, kNumOutputChannels);
-
-  PlusThreeBlockerCallback callback;
-  Blocker blocker(kChunkSize, kBlockSize, kNumInputChannels, kNumOutputChannels,
-                  kWindow, kShiftAmount, &callback);
-
-  RunTest(&blocker, kChunkSize, kNumFrames, input_cb.channels(),
-          input_chunk_cb.channels(), actual_output_cb.channels(),
-          output_chunk_cb.channels(), kNumInputChannels, kNumOutputChannels);
-
-  ValidateSignalEquality(expected_output_cb.channels(),
-                         actual_output_cb.channels(), kNumOutputChannels,
-                         kNumFrames);
-}
-
-TEST_F(BlockerTest, TestBlockerMutuallyPrimeShiftAndBlockSize) {
-  const size_t kNumInputChannels = 3;
-  const size_t kNumOutputChannels = 2;
-  const size_t kNumFrames = 12;
-  const size_t kBlockSize = 4;
-  const size_t kChunkSize = 6;
-  const size_t kShiftAmount = 3;
-
-  const float kInput[kNumInputChannels][kNumFrames] = {
-      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
-      {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
-      {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
-  ChannelBuffer<float> input_cb(kNumFrames, kNumInputChannels);
-  input_cb.SetDataForTesting(kInput[0], sizeof(kInput) / sizeof(**kInput));
-
-  const float kExpectedOutput[kNumOutputChannels][kNumFrames] = {
-      {6, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10},
-      {6, 14, 14, 28, 14, 14, 28, 14, 14, 28, 14, 14}};
-  ChannelBuffer<float> expected_output_cb(kNumFrames, kNumOutputChannels);
-  expected_output_cb.SetDataForTesting(
-      kExpectedOutput[0], sizeof(kExpectedOutput) / sizeof(**kExpectedOutput));
-
-  const float kWindow[kBlockSize] = {2.f, 2.f, 2.f, 2.f};
-
-  ChannelBuffer<float> actual_output_cb(kNumFrames, kNumOutputChannels);
-  ChannelBuffer<float> input_chunk_cb(kChunkSize, kNumInputChannels);
-  ChannelBuffer<float> output_chunk_cb(kChunkSize, kNumOutputChannels);
-
-  PlusThreeBlockerCallback callback;
-  Blocker blocker(kChunkSize, kBlockSize, kNumInputChannels, kNumOutputChannels,
-                  kWindow, kShiftAmount, &callback);
-
-  RunTest(&blocker, kChunkSize, kNumFrames, input_cb.channels(),
-          input_chunk_cb.channels(), actual_output_cb.channels(),
-          output_chunk_cb.channels(), kNumInputChannels, kNumOutputChannels);
-
-  ValidateSignalEquality(expected_output_cb.channels(),
-                         actual_output_cb.channels(), kNumOutputChannels,
-                         kNumFrames);
-}
-
-TEST_F(BlockerTest, TestBlockerNoOverlap) {
-  const size_t kNumInputChannels = 3;
-  const size_t kNumOutputChannels = 2;
-  const size_t kNumFrames = 12;
-  const size_t kBlockSize = 4;
-  const size_t kChunkSize = 4;
-  const size_t kShiftAmount = 4;
-
-  const float kInput[kNumInputChannels][kNumFrames] = {
-      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
-      {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
-      {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}};
-  ChannelBuffer<float> input_cb(kNumFrames, kNumInputChannels);
-  input_cb.SetDataForTesting(kInput[0], sizeof(kInput) / sizeof(**kInput));
-
-  const float kExpectedOutput[kNumOutputChannels][kNumFrames] = {
-      {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
-      {14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14}};
-  ChannelBuffer<float> expected_output_cb(kNumFrames, kNumOutputChannels);
-  expected_output_cb.SetDataForTesting(
-      kExpectedOutput[0], sizeof(kExpectedOutput) / sizeof(**kExpectedOutput));
-
-  const float kWindow[kBlockSize] = {2.f, 2.f, 2.f, 2.f};
-
-  ChannelBuffer<float> actual_output_cb(kNumFrames, kNumOutputChannels);
-  ChannelBuffer<float> input_chunk_cb(kChunkSize, kNumInputChannels);
-  ChannelBuffer<float> output_chunk_cb(kChunkSize, kNumOutputChannels);
-
-  PlusThreeBlockerCallback callback;
-  Blocker blocker(kChunkSize, kBlockSize, kNumInputChannels, kNumOutputChannels,
-                  kWindow, kShiftAmount, &callback);
-
-  RunTest(&blocker, kChunkSize, kNumFrames, input_cb.channels(),
-          input_chunk_cb.channels(), actual_output_cb.channels(),
-          output_chunk_cb.channels(), kNumInputChannels, kNumOutputChannels);
-
-  ValidateSignalEquality(expected_output_cb.channels(),
-                         actual_output_cb.channels(), kNumOutputChannels,
-                         kNumFrames);
-}
-
-TEST_F(BlockerTest, InitialDelaysAreMinimum) {
-  const size_t kNumInputChannels = 3;
-  const size_t kNumOutputChannels = 2;
-  const size_t kNumFrames = 1280;
-  const size_t kChunkSize[] = {80,  80,  80,  80,  80,  80,
-                               160, 160, 160, 160, 160, 160};
-  const size_t kBlockSize[] = {64,  64,  64,  128, 128, 128,
-                               128, 128, 128, 256, 256, 256};
-  const size_t kShiftAmount[] = {16, 32, 64,  32, 64,  128,
-                                 32, 64, 128, 64, 128, 256};
-  const size_t kInitialDelay[] = {48, 48, 48, 112, 112, 112,
-                                  96, 96, 96, 224, 224, 224};
-
-  float input[kNumInputChannels][kNumFrames];
-  for (size_t i = 0; i < kNumInputChannels; ++i) {
-    for (size_t j = 0; j < kNumFrames; ++j) {
-      input[i][j] = i + 1;
-    }
-  }
-  ChannelBuffer<float> input_cb(kNumFrames, kNumInputChannels);
-  input_cb.SetDataForTesting(input[0], sizeof(input) / sizeof(**input));
-
-  ChannelBuffer<float> output_cb(kNumFrames, kNumOutputChannels);
-
-  CopyBlockerCallback callback;
-
-  for (size_t i = 0; i < arraysize(kChunkSize); ++i) {
-    std::unique_ptr<float[]> window(new float[kBlockSize[i]]);
-    for (size_t j = 0; j < kBlockSize[i]; ++j) {
-      window[j] = 1.f;
-    }
-
-    ChannelBuffer<float> input_chunk_cb(kChunkSize[i], kNumInputChannels);
-    ChannelBuffer<float> output_chunk_cb(kChunkSize[i], kNumOutputChannels);
-
-    Blocker blocker(kChunkSize[i], kBlockSize[i], kNumInputChannels,
-                    kNumOutputChannels, window.get(), kShiftAmount[i],
-                    &callback);
-
-    RunTest(&blocker, kChunkSize[i], kNumFrames, input_cb.channels(),
-            input_chunk_cb.channels(), output_cb.channels(),
-            output_chunk_cb.channels(), kNumInputChannels, kNumOutputChannels);
-
-    ValidateInitialDelay(output_cb.channels(), kNumOutputChannels, kNumFrames,
-                         kInitialDelay[i]);
-  }
-}
-
-}  // namespace webrtc
diff --git a/common_audio/lapped_transform.cc b/common_audio/lapped_transform.cc
deleted file mode 100644
index 72c2ad7..0000000
--- a/common_audio/lapped_transform.cc
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- *  Copyright (c) 2014 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 "common_audio/lapped_transform.h"
-
-#include <algorithm>
-#include <cstdlib>
-#include <cstring>
-
-#include "common_audio/real_fourier.h"
-#include "rtc_base/checks.h"
-
-namespace webrtc {
-
-void LappedTransform::BlockThunk::ProcessBlock(const float* const* input,
-                                               size_t num_frames,
-                                               size_t num_input_channels,
-                                               size_t num_output_channels,
-                                               float* const* output) {
-  RTC_CHECK_EQ(num_input_channels, parent_->num_in_channels_);
-  RTC_CHECK_EQ(num_output_channels, parent_->num_out_channels_);
-  RTC_CHECK_EQ(parent_->block_length_, num_frames);
-
-  for (size_t i = 0; i < num_input_channels; ++i) {
-    memcpy(parent_->real_buf_.Row(i), input[i], num_frames * sizeof(*input[0]));
-    parent_->fft_->Forward(parent_->real_buf_.Row(i),
-                           parent_->cplx_pre_.Row(i));
-  }
-
-  size_t block_length =
-      RealFourier::ComplexLength(RealFourier::FftOrder(num_frames));
-  RTC_CHECK_EQ(parent_->cplx_length_, block_length);
-  parent_->block_processor_->ProcessAudioBlock(
-      parent_->cplx_pre_.Array(), num_input_channels, parent_->cplx_length_,
-      num_output_channels, parent_->cplx_post_.Array());
-
-  for (size_t i = 0; i < num_output_channels; ++i) {
-    parent_->fft_->Inverse(parent_->cplx_post_.Row(i),
-                           parent_->real_buf_.Row(i));
-    memcpy(output[i], parent_->real_buf_.Row(i),
-           num_frames * sizeof(*input[0]));
-  }
-}
-
-LappedTransform::LappedTransform(size_t num_in_channels,
-                                 size_t num_out_channels,
-                                 size_t chunk_length,
-                                 const float* window,
-                                 size_t block_length,
-                                 size_t shift_amount,
-                                 Callback* callback)
-    : blocker_callback_(this),
-      num_in_channels_(num_in_channels),
-      num_out_channels_(num_out_channels),
-      block_length_(block_length),
-      chunk_length_(chunk_length),
-      block_processor_(callback),
-      blocker_(chunk_length_,
-               block_length_,
-               num_in_channels_,
-               num_out_channels_,
-               window,
-               shift_amount,
-               &blocker_callback_),
-      fft_(RealFourier::Create(RealFourier::FftOrder(block_length_))),
-      cplx_length_(RealFourier::ComplexLength(fft_->order())),
-      real_buf_(num_in_channels,
-                block_length_,
-                RealFourier::kFftBufferAlignment),
-      cplx_pre_(num_in_channels,
-                cplx_length_,
-                RealFourier::kFftBufferAlignment),
-      cplx_post_(num_out_channels,
-                 cplx_length_,
-                 RealFourier::kFftBufferAlignment) {
-  RTC_CHECK(num_in_channels_ > 0);
-  RTC_CHECK_GT(block_length_, 0);
-  RTC_CHECK_GT(chunk_length_, 0);
-  RTC_CHECK(block_processor_);
-
-  // block_length_ power of 2?
-  RTC_CHECK_EQ(0, block_length_ & (block_length_ - 1));
-}
-
-LappedTransform::~LappedTransform() = default;
-
-void LappedTransform::ProcessChunk(const float* const* in_chunk,
-                                   float* const* out_chunk) {
-  blocker_.ProcessChunk(in_chunk, chunk_length_, num_in_channels_,
-                        num_out_channels_, out_chunk);
-}
-
-}  // namespace webrtc
diff --git a/common_audio/lapped_transform.h b/common_audio/lapped_transform.h
deleted file mode 100644
index 1ab2a9f..0000000
--- a/common_audio/lapped_transform.h
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- *  Copyright (c) 2014 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 COMMON_AUDIO_LAPPED_TRANSFORM_H_
-#define COMMON_AUDIO_LAPPED_TRANSFORM_H_
-
-#include <complex>
-#include <memory>
-
-#include "common_audio/blocker.h"
-#include "common_audio/real_fourier.h"
-#include "rtc_base/memory/aligned_array.h"
-
-namespace webrtc {
-
-// Helper class for audio processing modules which operate on frequency domain
-// input derived from the windowed time domain audio stream.
-//
-// The input audio chunk is sliced into possibly overlapping blocks, multiplied
-// by a window and transformed with an FFT implementation. The transformed data
-// is supplied to the given callback for processing. The processed output is
-// then inverse transformed into the time domain and spliced back into a chunk
-// which constitutes the final output of this processing module.
-class LappedTransform {
- public:
-  class Callback {
-   public:
-    virtual ~Callback() {}
-
-    virtual void ProcessAudioBlock(const std::complex<float>* const* in_block,
-                                   size_t num_in_channels,
-                                   size_t frames,
-                                   size_t num_out_channels,
-                                   std::complex<float>* const* out_block) = 0;
-  };
-
-  // Construct a transform instance. |chunk_length| is the number of samples in
-  // each channel. |window| defines the window, owned by the caller (a copy is
-  // made internally); |window| should have length equal to |block_length|.
-  // |block_length| defines the length of a block, in samples.
-  // |shift_amount| is in samples. |callback| is the caller-owned audio
-  // processing function called for each block of the input chunk.
-  LappedTransform(size_t num_in_channels,
-                  size_t num_out_channels,
-                  size_t chunk_length,
-                  const float* window,
-                  size_t block_length,
-                  size_t shift_amount,
-                  Callback* callback);
-  ~LappedTransform();
-
-  // Main audio processing helper method. Internally slices |in_chunk| into
-  // blocks, transforms them to frequency domain, calls the callback for each
-  // block and returns a de-blocked time domain chunk of audio through
-  // |out_chunk|. Both buffers are caller-owned.
-  void ProcessChunk(const float* const* in_chunk, float* const* out_chunk);
-
-  // Get the chunk length.
-  //
-  // The chunk length is the number of samples per channel that must be passed
-  // to ProcessChunk via the parameter in_chunk.
-  //
-  // Returns the same chunk_length passed to the LappedTransform constructor.
-  size_t chunk_length() const { return chunk_length_; }
-
-  // Get the number of input channels.
-  //
-  // This is the number of arrays that must be passed to ProcessChunk via
-  // in_chunk.
-  //
-  // Returns the same num_in_channels passed to the LappedTransform constructor.
-  size_t num_in_channels() const { return num_in_channels_; }
-
-  // Get the number of output channels.
-  //
-  // This is the number of arrays that must be passed to ProcessChunk via
-  // out_chunk.
-  //
-  // Returns the same num_out_channels passed to the LappedTransform
-  // constructor.
-  size_t num_out_channels() const { return num_out_channels_; }
-
-  // Returns the initial delay.
-  //
-  // This is the delay introduced by the |blocker_| to be able to get and return
-  // chunks of |chunk_length|, but process blocks of |block_length|.
-  size_t initial_delay() const { return blocker_.initial_delay(); }
-
- private:
-  // Internal middleware callback, given to the blocker. Transforms each block
-  // and hands it over to the processing method given at construction time.
-  class BlockThunk : public BlockerCallback {
-   public:
-    explicit BlockThunk(LappedTransform* parent) : parent_(parent) {}
-
-    void ProcessBlock(const float* const* input,
-                      size_t num_frames,
-                      size_t num_input_channels,
-                      size_t num_output_channels,
-                      float* const* output) override;
-
-   private:
-    LappedTransform* const parent_;
-  } blocker_callback_;
-
-  const size_t num_in_channels_;
-  const size_t num_out_channels_;
-
-  const size_t block_length_;
-  const size_t chunk_length_;
-
-  Callback* const block_processor_;
-  Blocker blocker_;
-
-  std::unique_ptr<RealFourier> fft_;
-  const size_t cplx_length_;
-  AlignedArray<float> real_buf_;
-  AlignedArray<std::complex<float> > cplx_pre_;
-  AlignedArray<std::complex<float> > cplx_post_;
-};
-
-}  // namespace webrtc
-
-#endif  // COMMON_AUDIO_LAPPED_TRANSFORM_H_
diff --git a/common_audio/lapped_transform_unittest.cc b/common_audio/lapped_transform_unittest.cc
deleted file mode 100644
index 687df89..0000000
--- a/common_audio/lapped_transform_unittest.cc
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- *  Copyright (c) 2014 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 "common_audio/lapped_transform.h"
-
-#include <algorithm>
-#include <cmath>
-#include <cstring>
-
-#include "test/gtest.h"
-
-using std::complex;
-
-namespace {
-
-class NoopCallback : public webrtc::LappedTransform::Callback {
- public:
-  NoopCallback() : block_num_(0) {}
-
-  void ProcessAudioBlock(const complex<float>* const* in_block,
-                         size_t in_channels,
-                         size_t frames,
-                         size_t out_channels,
-                         complex<float>* const* out_block) override {
-    RTC_CHECK_EQ(in_channels, out_channels);
-    for (size_t i = 0; i < out_channels; ++i) {
-      memcpy(out_block[i], in_block[i], sizeof(**in_block) * frames);
-    }
-    ++block_num_;
-  }
-
-  size_t block_num() { return block_num_; }
-
- private:
-  size_t block_num_;
-};
-
-class FftCheckerCallback : public webrtc::LappedTransform::Callback {
- public:
-  FftCheckerCallback() : block_num_(0) {}
-
-  void ProcessAudioBlock(const complex<float>* const* in_block,
-                         size_t in_channels,
-                         size_t frames,
-                         size_t out_channels,
-                         complex<float>* const* out_block) override {
-    RTC_CHECK_EQ(in_channels, out_channels);
-
-    size_t full_length = (frames - 1) * 2;
-    ++block_num_;
-
-    if (block_num_ > 0) {
-      ASSERT_NEAR(in_block[0][0].real(), static_cast<float>(full_length),
-                  1e-5f);
-      ASSERT_NEAR(in_block[0][0].imag(), 0.0f, 1e-5f);
-      for (size_t i = 1; i < frames; ++i) {
-        ASSERT_NEAR(in_block[0][i].real(), 0.0f, 1e-5f);
-        ASSERT_NEAR(in_block[0][i].imag(), 0.0f, 1e-5f);
-      }
-    }
-  }
-
-  size_t block_num() { return block_num_; }
-
- private:
-  size_t block_num_;
-};
-
-void SetFloatArray(float value, int rows, int cols, float* const* array) {
-  for (int i = 0; i < rows; ++i) {
-    for (int j = 0; j < cols; ++j) {
-      array[i][j] = value;
-    }
-  }
-}
-
-}  // namespace
-
-namespace webrtc {
-
-TEST(LappedTransformTest, Windowless) {
-  const size_t kChannels = 3;
-  const size_t kChunkLength = 512;
-  const size_t kBlockLength = 64;
-  const size_t kShiftAmount = 64;
-  NoopCallback noop;
-
-  // Rectangular window.
-  float window[kBlockLength];
-  std::fill(window, &window[kBlockLength], 1.0f);
-
-  LappedTransform trans(kChannels, kChannels, kChunkLength, window,
-                        kBlockLength, kShiftAmount, &noop);
-  float in_buffer[kChannels][kChunkLength];
-  float* in_chunk[kChannels];
-  float out_buffer[kChannels][kChunkLength];
-  float* out_chunk[kChannels];
-
-  in_chunk[0] = in_buffer[0];
-  in_chunk[1] = in_buffer[1];
-  in_chunk[2] = in_buffer[2];
-  out_chunk[0] = out_buffer[0];
-  out_chunk[1] = out_buffer[1];
-  out_chunk[2] = out_buffer[2];
-  SetFloatArray(2.0f, kChannels, kChunkLength, in_chunk);
-  SetFloatArray(-1.0f, kChannels, kChunkLength, out_chunk);
-
-  trans.ProcessChunk(in_chunk, out_chunk);
-
-  for (size_t i = 0; i < kChannels; ++i) {
-    for (size_t j = 0; j < kChunkLength; ++j) {
-      ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f);
-    }
-  }
-
-  ASSERT_EQ(kChunkLength / kBlockLength, noop.block_num());
-}
-
-TEST(LappedTransformTest, IdentityProcessor) {
-  const size_t kChunkLength = 512;
-  const size_t kBlockLength = 64;
-  const size_t kShiftAmount = 32;
-  NoopCallback noop;
-
-  // Identity window for |overlap = block_size / 2|.
-  float window[kBlockLength];
-  std::fill(window, &window[kBlockLength], std::sqrt(0.5f));
-
-  LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kShiftAmount,
-                        &noop);
-  float in_buffer[kChunkLength];
-  float* in_chunk = in_buffer;
-  float out_buffer[kChunkLength];
-  float* out_chunk = out_buffer;
-
-  SetFloatArray(2.0f, 1, kChunkLength, &in_chunk);
-  SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk);
-
-  trans.ProcessChunk(&in_chunk, &out_chunk);
-
-  for (size_t i = 0; i < kChunkLength; ++i) {
-    ASSERT_NEAR(out_chunk[i], (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f,
-                1e-5f);
-  }
-
-  ASSERT_EQ(kChunkLength / kShiftAmount, noop.block_num());
-}
-
-TEST(LappedTransformTest, Callbacks) {
-  const size_t kChunkLength = 512;
-  const size_t kBlockLength = 64;
-  FftCheckerCallback call;
-
-  // Rectangular window.
-  float window[kBlockLength];
-  std::fill(window, &window[kBlockLength], 1.0f);
-
-  LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kBlockLength,
-                        &call);
-  float in_buffer[kChunkLength];
-  float* in_chunk = in_buffer;
-  float out_buffer[kChunkLength];
-  float* out_chunk = out_buffer;
-
-  SetFloatArray(1.0f, 1, kChunkLength, &in_chunk);
-  SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk);
-
-  trans.ProcessChunk(&in_chunk, &out_chunk);
-
-  ASSERT_EQ(kChunkLength / kBlockLength, call.block_num());
-}
-
-TEST(LappedTransformTest, chunk_length) {
-  const size_t kBlockLength = 64;
-  FftCheckerCallback call;
-  const float window[kBlockLength] = {};
-
-  // Make sure that chunk_length returns the same value passed to the
-  // LappedTransform constructor.
-  {
-    const size_t kExpectedChunkLength = 512;
-    const LappedTransform trans(1, 1, kExpectedChunkLength, window,
-                                kBlockLength, kBlockLength, &call);
-
-    EXPECT_EQ(kExpectedChunkLength, trans.chunk_length());
-  }
-  {
-    const size_t kExpectedChunkLength = 160;
-    const LappedTransform trans(1, 1, kExpectedChunkLength, window,
-                                kBlockLength, kBlockLength, &call);
-
-    EXPECT_EQ(kExpectedChunkLength, trans.chunk_length());
-  }
-}
-
-}  // namespace webrtc
diff --git a/common_audio/module.mk b/common_audio/module.mk
index dab66c1..dd650e0 100644
--- a/common_audio/module.mk
+++ b/common_audio/module.mk
@@ -6,11 +6,8 @@
 
 common_audio_CXX_OBJECTS = \
 	common_audio/audio_converter.o \
-	common_audio/audio_ring_buffer.o \
 	common_audio/audio_util.o \
-	common_audio/blocker.o \
 	common_audio/channel_buffer.o \
-	common_audio/lapped_transform.o \
 	common_audio/real_fourier.o \
 	common_audio/real_fourier_ooura.o \
 	common_audio/smoothing_filter.o \
diff --git a/common_audio/resampler/include/push_resampler.h b/common_audio/resampler/include/push_resampler.h
index 082cdc6..232ad2a 100644
--- a/common_audio/resampler/include/push_resampler.h
+++ b/common_audio/resampler/include/push_resampler.h
@@ -12,6 +12,7 @@
 #define COMMON_AUDIO_RESAMPLER_INCLUDE_PUSH_RESAMPLER_H_
 
 #include <memory>
+#include <vector>
 
 namespace webrtc {
 
@@ -36,17 +37,18 @@
   int Resample(const T* src, size_t src_length, T* dst, size_t dst_capacity);
 
  private:
-  std::unique_ptr<PushSincResampler> sinc_resampler_;
-  std::unique_ptr<PushSincResampler> sinc_resampler_right_;
   int src_sample_rate_hz_;
   int dst_sample_rate_hz_;
   size_t num_channels_;
-  std::unique_ptr<T[]> src_left_;
-  std::unique_ptr<T[]> src_right_;
-  std::unique_ptr<T[]> dst_left_;
-  std::unique_ptr<T[]> dst_right_;
-};
 
+  struct ChannelResampler {
+    std::unique_ptr<PushSincResampler> resampler;
+    std::vector<T> source;
+    std::vector<T> destination;
+  };
+
+  std::vector<ChannelResampler> channel_resamplers_;
+};
 }  // namespace webrtc
 
 #endif  // COMMON_AUDIO_RESAMPLER_INCLUDE_PUSH_RESAMPLER_H_
diff --git a/common_audio/resampler/push_resampler.cc b/common_audio/resampler/push_resampler.cc
index cc24c4b..318d97b 100644
--- a/common_audio/resampler/push_resampler.cc
+++ b/common_audio/resampler/push_resampler.cc
@@ -12,6 +12,8 @@
 
 #include <string.h>
 
+#include "absl/container/inlined_vector.h"
+#include "absl/memory/memory.h"
 #include "common_audio/include/audio_util.h"
 #include "common_audio/resampler/include/resampler.h"
 #include "common_audio/resampler/push_sinc_resampler.h"
@@ -34,7 +36,6 @@
   RTC_DCHECK_GT(src_sample_rate_hz, 0);
   RTC_DCHECK_GT(dst_sample_rate_hz, 0);
   RTC_DCHECK_GT(num_channels, 0);
-  RTC_DCHECK_LE(num_channels, 2);
 #endif
 }
 
@@ -76,8 +77,7 @@
     return 0;
   }
 
-  if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0 ||
-      num_channels > 2) {
+  if (src_sample_rate_hz <= 0 || dst_sample_rate_hz <= 0 || num_channels <= 0) {
     return -1;
   }
 
@@ -89,15 +89,14 @@
       static_cast<size_t>(src_sample_rate_hz / 100);
   const size_t dst_size_10ms_mono =
       static_cast<size_t>(dst_sample_rate_hz / 100);
-  sinc_resampler_.reset(
-      new PushSincResampler(src_size_10ms_mono, dst_size_10ms_mono));
-  if (num_channels_ == 2) {
-    src_left_.reset(new T[src_size_10ms_mono]);
-    src_right_.reset(new T[src_size_10ms_mono]);
-    dst_left_.reset(new T[dst_size_10ms_mono]);
-    dst_right_.reset(new T[dst_size_10ms_mono]);
-    sinc_resampler_right_.reset(
-        new PushSincResampler(src_size_10ms_mono, dst_size_10ms_mono));
+  channel_resamplers_.clear();
+  for (size_t i = 0; i < num_channels; ++i) {
+    channel_resamplers_.push_back(ChannelResampler());
+    auto channel_resampler = channel_resamplers_.rbegin();
+    channel_resampler->resampler = absl::make_unique<PushSincResampler>(
+        src_size_10ms_mono, dst_size_10ms_mono);
+    channel_resampler->source.resize(src_size_10ms_mono);
+    channel_resampler->destination.resize(dst_size_10ms_mono);
   }
 
   return 0;
@@ -117,25 +116,32 @@
     memcpy(dst, src, src_length * sizeof(T));
     return static_cast<int>(src_length);
   }
-  if (num_channels_ == 2) {
-    const size_t src_length_mono = src_length / num_channels_;
-    const size_t dst_capacity_mono = dst_capacity / num_channels_;
-    T* deinterleaved[] = {src_left_.get(), src_right_.get()};
-    Deinterleave(src, src_length_mono, num_channels_, deinterleaved);
 
-    size_t dst_length_mono = sinc_resampler_->Resample(
-        src_left_.get(), src_length_mono, dst_left_.get(), dst_capacity_mono);
-    sinc_resampler_right_->Resample(src_right_.get(), src_length_mono,
-                                    dst_right_.get(), dst_capacity_mono);
+  const size_t src_length_mono = src_length / num_channels_;
+  const size_t dst_capacity_mono = dst_capacity / num_channels_;
 
-    deinterleaved[0] = dst_left_.get();
-    deinterleaved[1] = dst_right_.get();
-    Interleave(deinterleaved, dst_length_mono, num_channels_, dst);
-    return static_cast<int>(dst_length_mono * num_channels_);
-  } else {
-    return static_cast<int>(
-        sinc_resampler_->Resample(src, src_length, dst, dst_capacity));
+  absl::InlinedVector<T*, 8> source_pointers;
+  for (auto& resampler : channel_resamplers_) {
+    source_pointers.push_back(resampler.source.data());
   }
+
+  Deinterleave(src, src_length_mono, num_channels_, source_pointers.data());
+
+  size_t dst_length_mono = 0;
+
+  for (auto& resampler : channel_resamplers_) {
+    dst_length_mono = resampler.resampler->Resample(
+        resampler.source.data(), src_length_mono, resampler.destination.data(),
+        dst_capacity_mono);
+  }
+
+  absl::InlinedVector<T*, 8> destination_pointers;
+  for (auto& resampler : channel_resamplers_) {
+    destination_pointers.push_back(resampler.destination.data());
+  }
+
+  Interleave(destination_pointers.data(), dst_length_mono, num_channels_, dst);
+  return static_cast<int>(dst_length_mono * num_channels_);
 }
 
 // Explictly generate required instantiations.
diff --git a/common_audio/resampler/push_resampler_unittest.cc b/common_audio/resampler/push_resampler_unittest.cc
index 6a0c60a..3a1d5c5 100644
--- a/common_audio/resampler/push_resampler_unittest.cc
+++ b/common_audio/resampler/push_resampler_unittest.cc
@@ -25,6 +25,7 @@
   PushResampler<int16_t> resampler;
   EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 1));
   EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 2));
+  EXPECT_EQ(0, resampler.InitializeIfNeeded(16000, 16000, 8));
 }
 
 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
@@ -45,10 +46,6 @@
   EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 0), "num_channels");
 }
 
-TEST(PushResamplerTest, VerifiesBadInputParameters4) {
-  PushResampler<int16_t> resampler;
-  EXPECT_DEATH(resampler.InitializeIfNeeded(16000, 16000, 3), "num_channels");
-}
 #endif
 #endif
 
diff --git a/common_audio/resampler/resampler_unittest.cc b/common_audio/resampler/resampler_unittest.cc
index fd636e2..61be040 100644
--- a/common_audio/resampler/resampler_unittest.cc
+++ b/common_audio/resampler/resampler_unittest.cc
@@ -11,6 +11,7 @@
 #include <array>
 
 #include "common_audio/resampler/include/resampler.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 // TODO(andrew): this is a work-in-progress. Many more tests are needed.
@@ -64,7 +65,7 @@
 void ResamplerTest::ResetIfNeededAndPush(int in_rate,
                                          int out_rate,
                                          int num_channels) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Input rate: " << in_rate << ", output rate: " << out_rate
      << ", channel count: " << num_channels;
   SCOPED_TRACE(ss.str());
@@ -90,7 +91,7 @@
   for (size_t i = 0; i < kRatesSize; ++i) {
     for (size_t j = 0; j < kRatesSize; ++j) {
       for (size_t k = 0; k < kNumChannelsSize; ++k) {
-        std::ostringstream ss;
+        rtc::StringBuilder ss;
         ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j]
            << ", channels: " << kNumChannels[k];
         SCOPED_TRACE(ss.str());
@@ -109,7 +110,7 @@
   const int kChannels = 1;
   for (size_t i = 0; i < kRatesSize; ++i) {
     for (size_t j = 0; j < kRatesSize; ++j) {
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
       SCOPED_TRACE(ss.str());
 
@@ -131,7 +132,7 @@
   const int kChannels = 2;
   for (size_t i = 0; i < kRatesSize; ++i) {
     for (size_t j = 0; j < kRatesSize; ++j) {
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Input rate: " << kRates[i] << ", output rate: " << kRates[j];
       SCOPED_TRACE(ss.str());
 
diff --git a/common_audio/ring_buffer.h b/common_audio/ring_buffer.h
index 0bbe879..bcc40e1 100644
--- a/common_audio/ring_buffer.h
+++ b/common_audio/ring_buffer.h
@@ -14,6 +14,8 @@
 #ifndef COMMON_AUDIO_RING_BUFFER_H_
 #define COMMON_AUDIO_RING_BUFFER_H_
 
+// TODO(alessiob): Used by AEC, AECm and AudioRingBuffer. Remove when possible.
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/common_audio/signal_processing/signal_processing_unittest.cc b/common_audio/signal_processing/signal_processing_unittest.cc
index 8e65ebd..aeaf97b 100644
--- a/common_audio/signal_processing/signal_processing_unittest.cc
+++ b/common_audio/signal_processing/signal_processing_unittest.cc
@@ -9,9 +9,9 @@
  */
 
 #include <algorithm>
-#include <sstream>
 
 #include "common_audio/signal_processing/include/signal_processing_library.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 static const size_t kVector16Size = 9;
@@ -134,7 +134,7 @@
           INT32_MIN, std::min<int64_t>(INT32_MAX, static_cast<int64_t>(a) + b));
       const int64_t diff = std::max<int64_t>(
           INT32_MIN, std::min<int64_t>(INT32_MAX, static_cast<int64_t>(a) - b));
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << a << " +/- " << b << ": sum " << sum << ", diff " << diff;
       SCOPED_TRACE(ss.str());
       EXPECT_EQ(sum, WebRtcSpl_AddSatW32(a, b));
diff --git a/common_types.h b/common_types.h
index 60d88b3..99c4064 100644
--- a/common_types.h
+++ b/common_types.h
@@ -342,10 +342,6 @@
   kVideoCodecMultiplex,
 };
 
-// Translates from name of codec to codec type and vice versa.
-const char* CodecTypeToPayloadString(VideoCodecType type);
-VideoCodecType PayloadStringToCodecType(const std::string& name);
-
 struct SpatialLayer {
   bool operator==(const SpatialLayer& other) const;
   bool operator!=(const SpatialLayer& other) const { return !(*this == other); }
diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn
index de2aeb7..5889018 100644
--- a/modules/audio_coding/BUILD.gn
+++ b/modules/audio_coding/BUILD.gn
@@ -58,7 +58,13 @@
 }
 
 rtc_static_library("rent_a_codec") {
-  visibility += webrtc_default_visibility
+  # Client code SHOULD NOT USE THIS TARGET, but for now it needs to be public
+  # because there exists client code that uses it.
+  # TODO(bugs.webrtc.org/9808): Move to private visibility as soon as that
+  # client code gets updated.
+  visibility += [ "*" ]
+  allow_poison = [ "audio_codecs" ]
+
   sources = [
     "acm2/acm_codec_database.cc",
     "acm2/acm_codec_database.h",
@@ -117,12 +123,12 @@
   }
 
   deps = audio_coding_deps + [
+           "../../system_wrappers:metrics",
            "../../api/audio:audio_frame_api",
            "..:module_api",
            "../../common_audio:common_audio_c",
            "../../rtc_base:deprecation",
            "../../rtc_base:checks",
-           "../../system_wrappers:metrics_api",
            "../../api:array_view",
            "../../api/audio_codecs:audio_codecs_api",
            ":audio_coding_module_typedefs",
@@ -821,7 +827,7 @@
     "../../rtc_base:rtc_base_approved",
     "../../rtc_base:rtc_numerics",
     "../../rtc_base:safe_minmax",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
@@ -938,7 +944,7 @@
     "../../rtc_base:rtc_base_approved",
     "../../rtc_base/system:file_wrapper",
     "../../system_wrappers",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
@@ -1046,6 +1052,7 @@
     ":neteq_decoder_enum",
     "..:module_api",
     "../..:webrtc_common",
+    "../../api:array_view",
     "../../api:libjingle_peerconnection_api",
     "../../api/audio:audio_frame_api",
     "../../api/audio_codecs:audio_codecs_api",
@@ -1058,8 +1065,8 @@
     "../../rtc_base:safe_minmax",
     "../../rtc_base:sanitizer",
     "../../rtc_base/system:fallthrough",
-    "../../system_wrappers:field_trial_api",
-    "../../system_wrappers:metrics_api",
+    "../../system_wrappers:field_trial",
+    "../../system_wrappers:metrics",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -1089,6 +1096,7 @@
     ":neteq",
     "../..:webrtc_common",
     "../../api:libjingle_peerconnection_api",
+    "../../api:neteq_simulator_api",
     "../../api/audio:audio_frame_api",
     "../../api/audio_codecs:audio_codecs_api",
     "../../api/audio_codecs:builtin_audio_decoder_factory",
@@ -1164,6 +1172,8 @@
     "neteq/tools/neteq_replacement_input.h",
     "neteq/tools/neteq_stats_getter.cc",
     "neteq/tools/neteq_stats_getter.h",
+    "neteq/tools/neteq_stats_plotter.cc",
+    "neteq/tools/neteq_stats_plotter.h",
   ]
 
   if (!build_with_chromium && is_clang) {
@@ -1180,6 +1190,7 @@
     "../../rtc_base:rtc_base_approved",
     "../rtp_rtcp",
     "../rtp_rtcp:rtp_rtcp_format",
+    "//third_party/abseil-cpp/absl/strings:strings",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 
@@ -1258,7 +1269,6 @@
       ":audio_codec_speed_tests",
       ":audio_decoder_unittests",
       ":audio_decoder_unittests",
-      ":delay_test",
       ":g711_test",
       ":g722_test",
       ":ilbc_test",
@@ -1288,7 +1298,6 @@
     visibility += webrtc_default_visibility
 
     sources = [
-      "test/ACMTest.h",
       "test/Channel.cc",
       "test/Channel.h",
       "test/EncodeDecodeTest.cc",
@@ -1322,12 +1331,28 @@
       ":audio_coding",
       ":audio_coding_module_typedefs",
       ":audio_format_conversion",
+      ":cng",
       ":pcm16b_c",
+      ":red",
       "..:module_api",
       "../..:webrtc_common",
       "../../api/audio:audio_frame_api",
+      "../../api/audio_codecs:audio_codecs_api",
       "../../api/audio_codecs:builtin_audio_decoder_factory",
       "../../api/audio_codecs:builtin_audio_encoder_factory",
+      "../../api/audio_codecs/L16:audio_decoder_L16",
+      "../../api/audio_codecs/L16:audio_encoder_L16",
+      "../../api/audio_codecs/g711:audio_decoder_g711",
+      "../../api/audio_codecs/g711:audio_encoder_g711",
+      "../../api/audio_codecs/g722:audio_decoder_g722",
+      "../../api/audio_codecs/g722:audio_encoder_g722",
+      "../../api/audio_codecs/ilbc:audio_decoder_ilbc",
+      "../../api/audio_codecs/ilbc:audio_encoder_ilbc",
+      "../../api/audio_codecs/isac:audio_decoder_isac_float",
+      "../../api/audio_codecs/isac:audio_encoder_isac_float",
+      "../../api/audio_codecs/opus:audio_decoder_opus",
+      "../../api/audio_codecs/opus:audio_encoder_opus",
+      "../../common_audio",
       "../../rtc_base:checks",
       "../../rtc_base:rtc_base_approved",
       "../../rtc_base/synchronization:rw_lock_wrapper",
@@ -1359,7 +1384,7 @@
       "../../rtc_base:protobuf_utils",
       "../../rtc_base:rtc_base_approved",
       "../../system_wrappers",
-      "../../system_wrappers:field_trial_api",
+      "../../system_wrappers:field_trial",
       "../../test:fileutils",
       "../../test:perf_test",
       "../../test:test_support",
@@ -1417,38 +1442,6 @@
            ]
   }
 
-  rtc_executable("delay_test") {
-    testonly = true
-    sources = [
-      "test/Channel.cc",
-      "test/Channel.h",
-      "test/PCMFile.cc",
-      "test/PCMFile.h",
-      "test/delay_test.cc",
-      "test/utility.cc",
-      "test/utility.h",
-    ]
-
-    deps = [
-      ":audio_coding",
-      ":audio_coding_module_typedefs",
-      ":audio_format_conversion",
-      "..:module_api",
-      "../../:webrtc_common",
-      "../../api/audio:audio_frame_api",
-      "../../api/audio_codecs:builtin_audio_decoder_factory",
-      "../../rtc_base:checks",
-      "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers",
-      "../../system_wrappers:system_wrappers_default",
-      "../../test:fileutils",
-      "../../test:test_support",
-      "../rtp_rtcp",
-      "//testing/gtest",
-      "//third_party/abseil-cpp/absl/types:optional",
-    ]
-  }  # delay_test
-
   audio_decoder_unittests_resources =
       [ "../../resources/audio_coding/testfile32kHz.pcm" ]
 
@@ -1505,16 +1498,17 @@
       proto_out_dir = "modules/audio_coding/neteq"
     }
 
-    rtc_test("neteq_rtpplay") {
+    rtc_source_set("neteq_test_factory") {
       testonly = true
+      visibility += webrtc_default_visibility
       defines = []
       deps = [
         "../../rtc_base:checks",
-        "../../test:field_trial",
         "../../test:fileutils",
       ]
       sources = [
-        "neteq/tools/neteq_rtpplay.cc",
+        "neteq/tools/neteq_test_factory.cc",
+        "neteq/tools/neteq_test_factory.h",
       ]
 
       if (!build_with_chromium && is_clang) {
@@ -1527,8 +1521,23 @@
         ":neteq_test_tools",
         "../..:webrtc_common",
         "../../rtc_base:rtc_base_approved",
-        "../../system_wrappers:system_wrappers_default",
         "../../test:test_support",
+        "//third_party/abseil-cpp/absl/memory",
+      ]
+    }
+
+    rtc_test("neteq_rtpplay") {
+      testonly = true
+      defines = []
+      deps = [
+        ":neteq_test_factory",
+        ":neteq_test_tools",
+        "../../rtc_base:rtc_base_approved",
+        "../../system_wrappers:field_trial",
+        "../../test:field_trial",
+      ]
+      sources = [
+        "neteq/tools/neteq_rtpplay.cc",
       ]
     }
   }
@@ -1584,9 +1593,6 @@
       "../..:webrtc_common",
       "../../api:libjingle_peerconnection_api",
       "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers:metrics_default",
-      "../../system_wrappers:system_wrappers_default",
-      "../../test:field_trial",
       "../../test:test_main",
       "../audio_processing",
       "//testing/gtest",
@@ -1661,7 +1667,6 @@
              "../../api/audio_codecs/L16:audio_encoder_L16",
              "../../api/audio_codecs/g722:audio_encoder_g722",
              "../../api/audio_codecs/ilbc:audio_encoder_ilbc",
-             "../../system_wrappers:system_wrappers_default",
              "../../api/audio_codecs/isac:audio_encoder_isac",
              "../../api/audio_codecs/opus:audio_encoder_opus",
              "../../rtc_base:rtc_base_approved",
@@ -1678,7 +1683,6 @@
     testonly = true
 
     deps = audio_coding_deps + [
-             "../../system_wrappers:system_wrappers_default",
              "../rtp_rtcp:rtp_rtcp_format",
              "../../api:array_view",
              "../../rtc_base:rtc_base_approved",
@@ -1701,7 +1705,6 @@
     deps = [
       "../../rtc_base:checks",
       "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers:system_wrappers_default",
       "../../test:rtp_test_utils",
       "//testing/gtest",
     ]
@@ -1719,7 +1722,6 @@
       ":neteq_test_tools",
       ":pcm16b",
       "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers:system_wrappers_default",
       "//testing/gtest",
     ]
   }
@@ -1754,8 +1756,6 @@
       ":neteq_test_support",
       "../..:webrtc_common",
       "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers:system_wrappers_default",
-      "../../test:fileutils",
       "../../test:test_support",
     ]
   }
@@ -1775,7 +1775,6 @@
       "../..:webrtc_common",
       "../../rtc_base:checks",
       "../../rtc_base:rtc_base_approved",
-      "../../system_wrappers:system_wrappers_default",
       "../../test:fileutils",
       "../../test:test_main",
       "//testing/gtest",
@@ -2023,6 +2022,7 @@
       "neteq/mock/mock_red_payload_splitter.h",
       "neteq/mock/mock_statistics_calculator.h",
       "neteq/nack_tracker_unittest.cc",
+      "neteq/neteq_decoder_plc_unittest.cc",
       "neteq/neteq_external_decoder_unittest.cc",
       "neteq/neteq_impl_unittest.cc",
       "neteq/neteq_network_stats_unittest.cc",
@@ -2093,6 +2093,8 @@
       "../../test:rtp_test_utils",
       "../../test:test_common",
       "../../test:test_support",
+      "codecs/opus/test",
+      "codecs/opus/test:test_unittest",
       "//testing/gtest",
       "//third_party/abseil-cpp/absl/memory",
     ]
diff --git a/modules/audio_coding/acm2/acm_codec_database.cc b/modules/audio_coding/acm2/acm_codec_database.cc
index a322c95..311af2d 100644
--- a/modules/audio_coding/acm2/acm_codec_database.cc
+++ b/modules/audio_coding/acm2/acm_codec_database.cc
@@ -17,8 +17,6 @@
 // references, where appropriate.
 #include "modules/audio_coding/acm2/acm_codec_database.h"
 
-#include <assert.h>
-
 #include "rtc_base/checks.h"
 
 #if ((defined WEBRTC_CODEC_ISAC) && (defined WEBRTC_CODEC_ISACFX))
diff --git a/modules/audio_coding/acm2/acm_receive_test.cc b/modules/audio_coding/acm2/acm_receive_test.cc
index ba8937e..2d7f296 100644
--- a/modules/audio_coding/acm2/acm_receive_test.cc
+++ b/modules/audio_coding/acm2/acm_receive_test.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/acm2/acm_receive_test.h"
 
-#include <assert.h>
 #include <stdio.h>
 
 #include <memory>
diff --git a/modules/audio_coding/acm2/acm_receiver.cc b/modules/audio_coding/acm2/acm_receiver.cc
index 892abe5..f631746 100644
--- a/modules/audio_coding/acm2/acm_receiver.cc
+++ b/modules/audio_coding/acm2/acm_receiver.cc
@@ -361,6 +361,12 @@
   }
 }
 
+absl::optional<SdpAudioFormat> AcmReceiver::DecoderByPayloadType(
+    int payload_type) const {
+  rtc::CritScope lock(&crit_sect_);
+  return neteq_->GetDecoderFormat(payload_type);
+}
+
 int AcmReceiver::EnableNack(size_t max_nack_list_size) {
   neteq_->EnableNack(max_nack_list_size);
   return 0;
diff --git a/modules/audio_coding/acm2/acm_receiver.h b/modules/audio_coding/acm2/acm_receiver.h
index 0731677..a2ae723 100644
--- a/modules/audio_coding/acm2/acm_receiver.h
+++ b/modules/audio_coding/acm2/acm_receiver.h
@@ -224,6 +224,7 @@
   //
   int DecoderByPayloadType(uint8_t payload_type,
                            CodecInst* codec) const;
+  absl::optional<SdpAudioFormat> DecoderByPayloadType(int payload_type) const;
 
   //
   // Enable NACK and set the maximum size of the NACK list. If NACK is already
diff --git a/modules/audio_coding/acm2/acm_receiver_unittest.cc b/modules/audio_coding/acm2/acm_receiver_unittest.cc
index 457ea1d..29f2a45 100644
--- a/modules/audio_coding/acm2/acm_receiver_unittest.cc
+++ b/modules/audio_coding/acm2/acm_receiver_unittest.cc
@@ -14,7 +14,9 @@
 #include <memory>
 
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
+#include "api/audio_codecs/builtin_audio_encoder_factory.h"
 #include "modules/audio_coding/acm2/rent_a_codec.h"
+#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
 #include "modules/audio_coding/include/audio_coding_module.h"
 #include "modules/audio_coding/neteq/tools/rtp_generator.h"
 #include "modules/include/module_common_types.h"
@@ -27,30 +29,6 @@
 namespace webrtc {
 
 namespace acm2 {
-namespace {
-
-bool CodecsEqual(const CodecInst& codec_a, const CodecInst& codec_b) {
-  if (strcmp(codec_a.plname, codec_b.plname) != 0 ||
-      codec_a.plfreq != codec_b.plfreq || codec_a.pltype != codec_b.pltype ||
-      codec_b.channels != codec_a.channels)
-    return false;
-  return true;
-}
-
-struct CodecIdInst {
-  explicit CodecIdInst(RentACodec::CodecId codec_id) {
-    const auto codec_ix = RentACodec::CodecIndexFromId(codec_id);
-    EXPECT_TRUE(codec_ix);
-    id = *codec_ix;
-    const auto codec_inst = RentACodec::CodecInstById(codec_id);
-    EXPECT_TRUE(codec_inst);
-    inst = *codec_inst;
-  }
-  int id;
-  CodecInst inst;
-};
-
-}  // namespace
 
 class AcmReceiverTestOldApi : public AudioPacketizationCallback,
                               public ::testing::Test {
@@ -60,7 +38,7 @@
         packet_sent_(false),
         last_packet_send_timestamp_(timestamp_),
         last_frame_type_(kEmptyFrame) {
-    config_.decoder_factory = CreateBuiltinAudioDecoderFactory();
+    config_.decoder_factory = decoder_factory_;
   }
 
   ~AcmReceiverTestOldApi() {}
@@ -70,8 +48,6 @@
     receiver_.reset(new AcmReceiver(config_));
     ASSERT_TRUE(receiver_.get() != NULL);
     ASSERT_TRUE(acm_.get() != NULL);
-    codecs_ = RentACodec::Database();
-
     acm_->InitializeReceiver();
     acm_->RegisterTransportCallback(this);
 
@@ -86,40 +62,54 @@
 
   void TearDown() override {}
 
-  void InsertOnePacketOfSilence(int codec_id) {
-    CodecInst codec =
-        *RentACodec::CodecInstById(*RentACodec::CodecIdFromIndex(codec_id));
-    if (timestamp_ == 0) {  // This is the first time inserting audio.
-      ASSERT_EQ(0, acm_->RegisterSendCodec(codec));
-    } else {
-      auto current_codec = acm_->SendCodec();
-      ASSERT_TRUE(current_codec);
-      if (!CodecsEqual(codec, *current_codec))
-        ASSERT_EQ(0, acm_->RegisterSendCodec(codec));
+  AudioCodecInfo SetEncoder(int payload_type,
+                            const SdpAudioFormat& format,
+                            const std::map<int, int> cng_payload_types = {}) {
+    // Create the speech encoder.
+    AudioCodecInfo info = encoder_factory_->QueryAudioEncoder(format).value();
+    std::unique_ptr<AudioEncoder> enc =
+        encoder_factory_->MakeAudioEncoder(payload_type, format, absl::nullopt);
+
+    // If we have a compatible CN specification, stack a CNG on top.
+    auto it = cng_payload_types.find(info.sample_rate_hz);
+    if (it != cng_payload_types.end()) {
+      AudioEncoderCng::Config config;
+      config.speech_encoder = std::move(enc);
+      config.num_channels = 1;
+      config.payload_type = it->second;
+      config.vad_mode = Vad::kVadNormal;
+      enc = absl::make_unique<AudioEncoderCng>(std::move(config));
     }
-    AudioFrame frame;
+
+    // Actually start using the new encoder.
+    acm_->SetEncoder(std::move(enc));
+    return info;
+  }
+
+  int InsertOnePacketOfSilence(const AudioCodecInfo& info) {
     // Frame setup according to the codec.
-    frame.sample_rate_hz_ = codec.plfreq;
-    frame.samples_per_channel_ = codec.plfreq / 100;  // 10 ms.
-    frame.num_channels_ = codec.channels;
+    AudioFrame frame;
+    frame.sample_rate_hz_ = info.sample_rate_hz;
+    frame.samples_per_channel_ = info.sample_rate_hz / 100;  // 10 ms.
+    frame.num_channels_ = info.num_channels;
     frame.Mute();
     packet_sent_ = false;
     last_packet_send_timestamp_ = timestamp_;
+    int num_10ms_frames = 0;
     while (!packet_sent_) {
       frame.timestamp_ = timestamp_;
       timestamp_ += rtc::checked_cast<uint32_t>(frame.samples_per_channel_);
-      ASSERT_GE(acm_->Add10MsData(frame), 0);
+      EXPECT_GE(acm_->Add10MsData(frame), 0);
+      ++num_10ms_frames;
     }
+    return num_10ms_frames;
   }
 
   template <size_t N>
-  void AddSetOfCodecs(const RentACodec::CodecId (&ids)[N]) {
-    for (auto id : ids) {
-      const auto i = RentACodec::CodecIndexFromId(id);
-      ASSERT_TRUE(i);
-      ASSERT_EQ(0, receiver_->AddCodec(*i, codecs_[*i].pltype,
-                                       codecs_[*i].channels, codecs_[*i].plfreq,
-                                       nullptr, codecs_[*i].plname));
+  void AddSetOfCodecs(rtc::ArrayView<SdpAudioFormat> formats) {
+    static int payload_type = 0;
+    for (const auto& format : formats) {
+      EXPECT_TRUE(receiver_->AddCodec(payload_type++, format));
     }
   }
 
@@ -149,9 +139,12 @@
     return 0;
   }
 
+  const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_ =
+      CreateBuiltinAudioEncoderFactory();
+  const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_ =
+      CreateBuiltinAudioDecoderFactory();
   AudioCodingModule::Config config_;
   std::unique_ptr<AcmReceiver> receiver_;
-  rtc::ArrayView<const CodecInst> codecs_;
   std::unique_ptr<AudioCodingModule> acm_;
   WebRtcRTPHeader rtp_header_;
   uint32_t timestamp_;
@@ -166,27 +159,26 @@
 #define MAYBE_AddCodecGetCodec AddCodecGetCodec
 #endif
 TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecGetCodec) {
+  const std::vector<AudioCodecSpec> codecs =
+      decoder_factory_->GetSupportedDecoders();
+
   // Add codec.
-  for (size_t n = 0; n < codecs_.size(); ++n) {
+  for (size_t n = 0; n < codecs.size(); ++n) {
     if (n & 0x1) {  // Just add codecs with odd index.
-      EXPECT_EQ(
-          0, receiver_->AddCodec(rtc::checked_cast<int>(n), codecs_[n].pltype,
-                                 codecs_[n].channels, codecs_[n].plfreq, NULL,
-                                 codecs_[n].plname));
+      const int payload_type = rtc::checked_cast<int>(n);
+      EXPECT_TRUE(receiver_->AddCodec(payload_type, codecs[n].format));
     }
   }
   // Get codec and compare.
-  for (size_t n = 0; n < codecs_.size(); ++n) {
-    CodecInst my_codec;
+  for (size_t n = 0; n < codecs.size(); ++n) {
+    const int payload_type = rtc::checked_cast<int>(n);
     if (n & 0x1) {
       // Codecs with odd index should match the reference.
-      EXPECT_EQ(0,
-                receiver_->DecoderByPayloadType(codecs_[n].pltype, &my_codec));
-      EXPECT_TRUE(CodecsEqual(codecs_[n], my_codec));
+      EXPECT_EQ(absl::make_optional(codecs[n].format),
+                receiver_->DecoderByPayloadType(payload_type));
     } else {
       // Codecs with even index are not registered.
-      EXPECT_EQ(-1,
-                receiver_->DecoderByPayloadType(codecs_[n].pltype, &my_codec));
+      EXPECT_EQ(absl::nullopt, receiver_->DecoderByPayloadType(payload_type));
     }
   }
 }
@@ -197,24 +189,15 @@
 #define MAYBE_AddCodecChangePayloadType AddCodecChangePayloadType
 #endif
 TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecChangePayloadType) {
-  const CodecIdInst codec1(RentACodec::CodecId::kPCMA);
-  CodecInst codec2 = codec1.inst;
-  ++codec2.pltype;
-  CodecInst test_codec;
+  const SdpAudioFormat format("giraffe", 8000, 1);
 
   // Register the same codec with different payloads.
-  EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype,
-                                   codec1.inst.channels, codec1.inst.plfreq,
-                                   nullptr, codec1.inst.plname));
-  EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec2.pltype, codec2.channels,
-                                   codec2.plfreq, NULL, codec2.plname));
+  EXPECT_EQ(true, receiver_->AddCodec(17, format));
+  EXPECT_EQ(true, receiver_->AddCodec(18, format));
 
   // Both payload types should exist.
-  EXPECT_EQ(0,
-            receiver_->DecoderByPayloadType(codec1.inst.pltype, &test_codec));
-  EXPECT_EQ(true, CodecsEqual(codec1.inst, test_codec));
-  EXPECT_EQ(0, receiver_->DecoderByPayloadType(codec2.pltype, &test_codec));
-  EXPECT_EQ(true, CodecsEqual(codec2, test_codec));
+  EXPECT_EQ(absl::make_optional(format), receiver_->DecoderByPayloadType(17));
+  EXPECT_EQ(absl::make_optional(format), receiver_->DecoderByPayloadType(18));
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -223,23 +206,15 @@
 #define MAYBE_AddCodecChangeCodecId AddCodecChangeCodecId
 #endif
 TEST_F(AcmReceiverTestOldApi, AddCodecChangeCodecId) {
-  const CodecIdInst codec1(RentACodec::CodecId::kPCMU);
-  CodecIdInst codec2(RentACodec::CodecId::kPCMA);
-  codec2.inst.pltype = codec1.inst.pltype;
-  CodecInst test_codec;
+  const SdpAudioFormat format1("giraffe", 8000, 1);
+  const SdpAudioFormat format2("gnu", 16000, 1);
 
   // Register the same payload type with different codec ID.
-  EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype,
-                                   codec1.inst.channels, codec1.inst.plfreq,
-                                   nullptr, codec1.inst.plname));
-  EXPECT_EQ(0, receiver_->AddCodec(codec2.id, codec2.inst.pltype,
-                                   codec2.inst.channels, codec2.inst.plfreq,
-                                   nullptr, codec2.inst.plname));
+  EXPECT_EQ(true, receiver_->AddCodec(17, format1));
+  EXPECT_EQ(true, receiver_->AddCodec(17, format2));
 
   // Make sure that the last codec is used.
-  EXPECT_EQ(0,
-            receiver_->DecoderByPayloadType(codec2.inst.pltype, &test_codec));
-  EXPECT_EQ(true, CodecsEqual(codec2.inst, test_codec));
+  EXPECT_EQ(absl::make_optional(format2), receiver_->DecoderByPayloadType(17));
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -248,21 +223,16 @@
 #define MAYBE_AddCodecRemoveCodec AddCodecRemoveCodec
 #endif
 TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecRemoveCodec) {
-  const CodecIdInst codec(RentACodec::CodecId::kPCMA);
-  const int payload_type = codec.inst.pltype;
-  EXPECT_EQ(
-      0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels,
-                             codec.inst.plfreq, nullptr, codec.inst.plname));
+  EXPECT_EQ(true, receiver_->AddCodec(17, SdpAudioFormat("giraffe", 8000, 1)));
 
   // Remove non-existing codec should not fail. ACM1 legacy.
-  EXPECT_EQ(0, receiver_->RemoveCodec(payload_type + 1));
+  EXPECT_EQ(0, receiver_->RemoveCodec(18));
 
   // Remove an existing codec.
-  EXPECT_EQ(0, receiver_->RemoveCodec(payload_type));
+  EXPECT_EQ(0, receiver_->RemoveCodec(17));
 
   // Ask for the removed codec, must fail.
-  CodecInst ci;
-  EXPECT_EQ(-1, receiver_->DecoderByPayloadType(payload_type, &ci));
+  EXPECT_EQ(absl::nullopt, receiver_->DecoderByPayloadType(17));
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -271,21 +241,25 @@
 #define MAYBE_SampleRate SampleRate
 #endif
 TEST_F(AcmReceiverTestOldApi, MAYBE_SampleRate) {
-  const RentACodec::CodecId kCodecId[] = {RentACodec::CodecId::kISAC,
-                                          RentACodec::CodecId::kISACSWB};
-  AddSetOfCodecs(kCodecId);
+  const std::vector<SdpAudioFormat> codecs = {{"ISAC", 16000, 1},
+                                              {"ISAC", 32000, 1}};
+  for (size_t i = 0; i < codecs.size(); ++i) {
+    const int payload_type = rtc::checked_cast<int>(i);
+    EXPECT_EQ(true, receiver_->AddCodec(payload_type, codecs[i]));
+  }
 
-  AudioFrame frame;
-  const int kOutSampleRateHz = 8000;  // Different than codec sample rate.
-  for (const auto codec_id : kCodecId) {
-    const CodecIdInst codec(codec_id);
-    const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100);
-    InsertOnePacketOfSilence(codec.id);
+  constexpr int kOutSampleRateHz = 8000;  // Different than codec sample rate.
+  for (size_t i = 0; i < codecs.size(); ++i) {
+    const int payload_type = rtc::checked_cast<int>(i);
+    const int num_10ms_frames =
+        InsertOnePacketOfSilence(SetEncoder(payload_type, codecs[i]));
     for (int k = 0; k < num_10ms_frames; ++k) {
+      AudioFrame frame;
       bool muted;
       EXPECT_EQ(0, receiver_->GetAudio(kOutSampleRateHz, &frame, &muted));
     }
-    EXPECT_EQ(codec.inst.plfreq, receiver_->last_output_sample_rate_hz());
+    EXPECT_EQ(encoder_factory_->QueryAudioEncoder(codecs[i])->sample_rate_hz,
+              receiver_->last_output_sample_rate_hz());
   }
 }
 
@@ -295,7 +269,7 @@
     config_.neteq_config.for_test_no_time_stretching = true;
   }
 
-  void RunVerifyAudioFrame(RentACodec::CodecId codec_id) {
+  void RunVerifyAudioFrame(const SdpAudioFormat& codec) {
     // Make sure "fax mode" is enabled. This will avoid delay changes unless the
     // packet-loss concealment is made. We do this in order to make the
     // timestamp increments predictable; in normal mode, NetEq may decide to do
@@ -303,16 +277,14 @@
     // the timestamp.
     EXPECT_TRUE(config_.neteq_config.for_test_no_time_stretching);
 
-    const RentACodec::CodecId kCodecId[] = {codec_id};
-    AddSetOfCodecs(kCodecId);
+    constexpr int payload_type = 17;
+    EXPECT_TRUE(receiver_->AddCodec(payload_type, codec));
 
-    const CodecIdInst codec(codec_id);
-    const int output_sample_rate_hz = codec.inst.plfreq;
-    const size_t output_channels = codec.inst.channels;
+    const AudioCodecInfo info = SetEncoder(payload_type, codec);
+    const int output_sample_rate_hz = info.sample_rate_hz;
+    const size_t output_channels = info.num_channels;
     const size_t samples_per_ms = rtc::checked_cast<size_t>(
         rtc::CheckedDivExact(output_sample_rate_hz, 1000));
-    const int num_10ms_frames = rtc::CheckedDivExact(
-        codec.inst.pacsize, rtc::checked_cast<int>(10 * samples_per_ms));
     const AudioFrame::VADActivity expected_vad_activity =
         output_sample_rate_hz > 16000 ? AudioFrame::kVadActive
                                       : AudioFrame::kVadPassive;
@@ -330,7 +302,7 @@
     // Expect timestamp = 0 before first packet is inserted.
     EXPECT_EQ(0u, frame.timestamp_);
     for (int i = 0; i < 5; ++i) {
-      InsertOnePacketOfSilence(codec.id);
+      const int num_10ms_frames = InsertOnePacketOfSilence(info);
       for (int k = 0; k < num_10ms_frames; ++k) {
         EXPECT_EQ(0,
                   receiver_->GetAudio(output_sample_rate_hz, &frame, &muted));
@@ -353,7 +325,7 @@
 #define MAYBE_VerifyAudioFramePCMU VerifyAudioFramePCMU
 #endif
 TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFramePCMU) {
-  RunVerifyAudioFrame(RentACodec::CodecId::kPCMU);
+  RunVerifyAudioFrame({"PCMU", 8000, 1});
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -362,7 +334,7 @@
 #define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC
 #endif
 TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) {
-  RunVerifyAudioFrame(RentACodec::CodecId::kISAC);
+  RunVerifyAudioFrame({"ISAC", 16000, 1});
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -371,7 +343,7 @@
 #define MAYBE_VerifyAudioFrameOpus VerifyAudioFrameOpus
 #endif
 TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameOpus) {
-  RunVerifyAudioFrame(RentACodec::CodecId::kOpus);
+  RunVerifyAudioFrame({"opus", 48000, 2});
 }
 
 #if defined(WEBRTC_ANDROID)
@@ -381,18 +353,17 @@
 #endif
 TEST_F(AcmReceiverTestOldApi, MAYBE_PostdecodingVad) {
   EXPECT_TRUE(config_.neteq_config.enable_post_decode_vad);
-  const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb);
-  ASSERT_EQ(
-      0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels,
-                             codec.inst.plfreq, nullptr, ""));
-  const int kNumPackets = 5;
-  const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100);
+  constexpr int payload_type = 34;
+  const SdpAudioFormat codec = {"L16", 16000, 1};
+  const AudioCodecInfo info = SetEncoder(payload_type, codec);
+  EXPECT_TRUE(receiver_->AddCodec(payload_type, codec));
+  constexpr int kNumPackets = 5;
   AudioFrame frame;
   for (int n = 0; n < kNumPackets; ++n) {
-    InsertOnePacketOfSilence(codec.id);
+    const int num_10ms_frames = InsertOnePacketOfSilence(info);
     for (int k = 0; k < num_10ms_frames; ++k) {
       bool muted;
-      ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted));
+      ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
     }
   }
   EXPECT_EQ(AudioFrame::kVadPassive, frame.vad_activity_);
@@ -412,18 +383,18 @@
 #endif
 TEST_F(AcmReceiverTestPostDecodeVadPassiveOldApi, MAYBE_PostdecodingVad) {
   EXPECT_FALSE(config_.neteq_config.enable_post_decode_vad);
-  const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb);
-  ASSERT_EQ(
-      0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels,
-                             codec.inst.plfreq, nullptr, ""));
+  constexpr int payload_type = 34;
+  const SdpAudioFormat codec = {"L16", 16000, 1};
+  const AudioCodecInfo info = SetEncoder(payload_type, codec);
+  encoder_factory_->QueryAudioEncoder(codec).value();
+  EXPECT_TRUE(receiver_->AddCodec(payload_type, codec));
   const int kNumPackets = 5;
-  const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100);
   AudioFrame frame;
   for (int n = 0; n < kNumPackets; ++n) {
-    InsertOnePacketOfSilence(codec.id);
+    const int num_10ms_frames = InsertOnePacketOfSilence(info);
     for (int k = 0; k < num_10ms_frames; ++k) {
       bool muted;
-      ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted));
+      ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
     }
   }
   EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_);
@@ -436,64 +407,64 @@
 #endif
 #if defined(WEBRTC_CODEC_ISAC)
 TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) {
-  const RentACodec::CodecId kCodecId[] = {
-      RentACodec::CodecId::kISAC, RentACodec::CodecId::kPCMA,
-      RentACodec::CodecId::kISACSWB, RentACodec::CodecId::kPCM16Bswb32kHz};
-  AddSetOfCodecs(kCodecId);
+  const std::vector<SdpAudioFormat> codecs = {{"ISAC", 16000, 1},
+                                              {"PCMA", 8000, 1},
+                                              {"ISAC", 32000, 1},
+                                              {"L16", 32000, 1}};
+  for (size_t i = 0; i < codecs.size(); ++i) {
+    const int payload_type = rtc::checked_cast<int>(i);
+    EXPECT_TRUE(receiver_->AddCodec(payload_type, codecs[i]));
+  }
 
-  const RentACodec::CodecId kCngId[] = {
-      // Not including full-band.
-      RentACodec::CodecId::kCNNB, RentACodec::CodecId::kCNWB,
-      RentACodec::CodecId::kCNSWB};
-  AddSetOfCodecs(kCngId);
+  const std::map<int, int> cng_payload_types = {
+      {8000, 100}, {16000, 101}, {32000, 102}};
+  for (const auto& x : cng_payload_types) {
+    const int sample_rate_hz = x.first;
+    const int payload_type = x.second;
+    EXPECT_TRUE(receiver_->AddCodec(payload_type, {"CN", sample_rate_hz, 1}));
+  }
 
-  // Register CNG at sender side.
-  for (auto id : kCngId)
-    ASSERT_EQ(0, acm_->RegisterSendCodec(CodecIdInst(id).inst));
-
-  CodecInst codec;
   // No audio payload is received.
-  EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec));
+  EXPECT_EQ(absl::nullopt, receiver_->LastAudioFormat());
 
   // Start with sending DTX.
-  ASSERT_EQ(0, acm_->SetVAD(true, true, VADVeryAggr));
   packet_sent_ = false;
-  InsertOnePacketOfSilence(CodecIdInst(kCodecId[0]).id);  // Enough to test
-                                                          // with one codec.
+  InsertOnePacketOfSilence(
+      SetEncoder(0, codecs[0], cng_payload_types));  // Enough to test
+                                                     // with one codec.
   ASSERT_TRUE(packet_sent_);
   EXPECT_EQ(kAudioFrameCN, last_frame_type_);
 
   // Has received, only, DTX. Last Audio codec is undefined.
-  EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec));
+  EXPECT_EQ(absl::nullopt, receiver_->LastAudioFormat());
   EXPECT_FALSE(receiver_->last_packet_sample_rate_hz());
 
-  for (auto id : kCodecId) {
-    const CodecIdInst c(id);
-
+  for (size_t i = 0; i < codecs.size(); ++i) {
     // Set DTX off to send audio payload.
-    acm_->SetVAD(false, false, VADAggr);
     packet_sent_ = false;
-    InsertOnePacketOfSilence(c.id);
+    const int payload_type = rtc::checked_cast<int>(i);
+    const AudioCodecInfo info_without_cng = SetEncoder(payload_type, codecs[i]);
+    InsertOnePacketOfSilence(info_without_cng);
 
     // Sanity check if Actually an audio payload received, and it should be
     // of type "speech."
     ASSERT_TRUE(packet_sent_);
     ASSERT_EQ(kAudioFrameSpeech, last_frame_type_);
-    EXPECT_EQ(c.inst.plfreq, receiver_->last_packet_sample_rate_hz());
+    EXPECT_EQ(info_without_cng.sample_rate_hz,
+              receiver_->last_packet_sample_rate_hz());
 
     // Set VAD on to send DTX. Then check if the "Last Audio codec" returns
-    // the expected codec.
-    acm_->SetVAD(true, true, VADAggr);
-
-    // Do as many encoding until a DTX is sent.
+    // the expected codec. Encode repeatedly until a DTX is sent.
+    const AudioCodecInfo info_with_cng =
+        SetEncoder(payload_type, codecs[i], cng_payload_types);
     while (last_frame_type_ != kAudioFrameCN) {
       packet_sent_ = false;
-      InsertOnePacketOfSilence(c.id);
+      InsertOnePacketOfSilence(info_with_cng);
       ASSERT_TRUE(packet_sent_);
     }
-    EXPECT_EQ(c.inst.plfreq, receiver_->last_packet_sample_rate_hz());
-    EXPECT_EQ(0, receiver_->LastAudioCodec(&codec));
-    EXPECT_TRUE(CodecsEqual(c.inst, codec));
+    EXPECT_EQ(info_with_cng.sample_rate_hz,
+              receiver_->last_packet_sample_rate_hz());
+    EXPECT_EQ(codecs[i], receiver_->LastAudioFormat());
   }
 }
 #endif
diff --git a/modules/audio_coding/acm2/audio_coding_module.cc b/modules/audio_coding/acm2/audio_coding_module.cc
index f0b35ca..60afeb6 100644
--- a/modules/audio_coding/acm2/audio_coding_module.cc
+++ b/modules/audio_coding/acm2/audio_coding_module.cc
@@ -167,8 +167,6 @@
 
   int GetNetworkStatistics(NetworkStatistics* statistics) override;
 
-  int SetOpusApplication(OpusApplicationMode application) override;
-
   // If current send codec is Opus, informs it about the maximum playback rate
   // the receiver will render.
   int SetOpusMaxPlaybackRate(int frequency_hz) override;
@@ -1097,26 +1095,6 @@
   return 0;
 }
 
-int AudioCodingModuleImpl::SetOpusApplication(OpusApplicationMode application) {
-  rtc::CritScope lock(&acm_crit_sect_);
-  if (!HaveValidEncoder("SetOpusApplication")) {
-    return -1;
-  }
-  AudioEncoder::Application app;
-  switch (application) {
-    case kVoip:
-      app = AudioEncoder::Application::kSpeech;
-      break;
-    case kAudio:
-      app = AudioEncoder::Application::kAudio;
-      break;
-    default:
-      FATAL();
-      return 0;
-  }
-  return encoder_stack_->SetApplication(app) ? 0 : -1;
-}
-
 // Informs Opus encoder of the maximum playback rate the receiver will render.
 int AudioCodingModuleImpl::SetOpusMaxPlaybackRate(int frequency_hz) {
   rtc::CritScope lock(&acm_crit_sect_);
diff --git a/modules/audio_coding/acm2/audio_coding_module_unittest.cc b/modules/audio_coding/acm2/audio_coding_module_unittest.cc
index 924a4a6..5ac6102 100644
--- a/modules/audio_coding/acm2/audio_coding_module_unittest.cc
+++ b/modules/audio_coding/acm2/audio_coding_module_unittest.cc
@@ -15,10 +15,12 @@
 
 #include "api/audio_codecs/audio_encoder.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
+#include "api/audio_codecs/builtin_audio_encoder_factory.h"
 #include "api/audio_codecs/opus/audio_encoder_opus.h"
 #include "modules/audio_coding/acm2/acm_receive_test.h"
 #include "modules/audio_coding/acm2/acm_send_test.h"
 #include "modules/audio_coding/codecs/audio_format_conversion.h"
+#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
 #include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
 #include "modules/audio_coding/codecs/g711/audio_encoder_pcm.h"
 #include "modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
@@ -194,7 +196,8 @@
 
   virtual void RegisterCodec() {
     EXPECT_EQ(true, acm_->RegisterReceiveCodec(kPayloadType, *audio_format_));
-    EXPECT_EQ(0, acm_->RegisterSendCodec(codec_));
+    acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
+        kPayloadType, *audio_format_, absl::nullopt));
   }
 
   virtual void InsertPacketAndPullAudio() {
@@ -341,6 +344,7 @@
 TEST_F(AudioCodingModuleTestOldApi, TransportCallbackIsInvokedForEachPacket) {
   const int k10MsBlocksPerPacket = 3;
   codec_.pacsize = k10MsBlocksPerPacket * kSampleRateHz / 100;
+  audio_format_->parameters["ptime"] = "30";
   RegisterCodec();
   const int kLoops = 10;
   for (int i = 0; i < kLoops; ++i) {
@@ -374,6 +378,7 @@
 
   // Change codec.
   ASSERT_EQ(0, AudioCodingModule::Codec("ISAC", &codec_, kSampleRateHz, 1));
+  audio_format_ = SdpAudioFormat("ISAC", kSampleRateHz, 1);
   RegisterCodec();
   blocks_per_packet = codec_.pacsize / (kSampleRateHz / 100);
   // Encode another 5 packets.
@@ -402,11 +407,14 @@
     EXPECT_EQ(true,
               acm_->RegisterReceiveCodec(
                   rtp_payload_type, SdpAudioFormat("cn", kSampleRateHz, 1)));
-
-    CodecInst codec;
-    EXPECT_EQ(0, AudioCodingModule::Codec("CN", &codec, kSampleRateHz, 1));
-    codec.pltype = rtp_payload_type;
-    EXPECT_EQ(0, acm_->RegisterSendCodec(codec));
+    acm_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
+      AudioEncoderCng::Config config;
+      config.speech_encoder = std::move(*enc);
+      config.num_channels = 1;
+      config.payload_type = rtp_payload_type;
+      config.vad_mode = Vad::kVadNormal;
+      *enc = absl::make_unique<AudioEncoderCng>(std::move(config));
+    });
   }
 
   void VerifyEncoding() override {
@@ -450,27 +458,14 @@
 // Checks that the transport callback is invoked once per frame period of the
 // underlying speech encoder, even when comfort noise is produced.
 // Also checks that the frame type is kAudioFrameCN or kEmptyFrame.
-// This test and the next check the same thing, but differ in the order of
-// speech codec and CNG registration.
 TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,
        TransportCallbackTestForComfortNoiseRegisterCngLast) {
   const int k10MsBlocksPerPacket = 3;
   codec_.pacsize = k10MsBlocksPerPacket * kSampleRateHz / 100;
+  audio_format_->parameters["ptime"] = "30";
   RegisterCodec();
   const int kCngPayloadType = 105;
   RegisterCngCodec(kCngPayloadType);
-  ASSERT_EQ(0, acm_->SetVAD(true, true));
-  DoTest(k10MsBlocksPerPacket, kCngPayloadType);
-}
-
-TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,
-       TransportCallbackTestForComfortNoiseRegisterCngFirst) {
-  const int k10MsBlocksPerPacket = 3;
-  codec_.pacsize = k10MsBlocksPerPacket * kSampleRateHz / 100;
-  const int kCngPayloadType = 105;
-  RegisterCngCodec(kCngPayloadType);
-  RegisterCodec();
-  ASSERT_EQ(0, acm_->SetVAD(true, true));
   DoTest(k10MsBlocksPerPacket, kCngPayloadType);
 }
 
@@ -662,7 +657,8 @@
     // Register iSAC codec in ACM, effectively unregistering the PCM16B codec
     // registered in AudioCodingModuleTestOldApi::SetUp();
     EXPECT_EQ(true, acm_->RegisterReceiveCodec(kPayloadType, *audio_format_));
-    EXPECT_EQ(0, acm_->RegisterSendCodec(codec_));
+    acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
+        kPayloadType, *audio_format_, absl::nullopt));
   }
 
   void InsertPacket() override {
@@ -855,7 +851,8 @@
     if (!codec_registered_ &&
         receive_packet_count_ > kRegisterAfterNumPackets) {
       // Register the iSAC encoder.
-      EXPECT_EQ(0, acm_->RegisterSendCodec(codec_));
+      acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
+          kPayloadType, *audio_format_, absl::nullopt));
       codec_registered_ = true;
     }
     if (codec_registered_ && receive_packet_count_ > kNumPackets) {
@@ -1478,32 +1475,13 @@
       50, test::AcmReceiveTestOldApi::kStereoOutput);
 }
 
-TEST_F(AcmSenderBitExactnessOldApi, Opus_stereo_20ms_voip) {
-  ASSERT_NO_FATAL_FAILURE(SetUpTest("opus", 48000, 2, 120, 960, 960));
-  // If not set, default will be kAudio in case of stereo.
-  EXPECT_EQ(0, send_test_->acm()->SetOpusApplication(kVoip));
-  Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
-          "b0325df4e8104f04e03af23c0b75800e",
-          "b0325df4e8104f04e03af23c0b75800e",
-          "1c81121f5d9286a5a865d01dbab22ce8",
-          "11d547f89142e9ef03f37d7ca7f32379",
-          "11d547f89142e9ef03f37d7ca7f32379"),
-      AcmReceiverBitExactnessOldApi::PlatformChecksum(
-          "4eab2259b6fe24c22dd242a113e0b3d9",
-          "4eab2259b6fe24c22dd242a113e0b3d9",
-          "839ea60399447268ee0f0262a50b75fd",
-          "1815fd5589cad0c6f6cf946c76b81aeb",
-          "1815fd5589cad0c6f6cf946c76b81aeb"),
-      50, test::AcmReceiveTestOldApi::kStereoOutput);
-}
-
 TEST_F(AcmSenderBitExactnessNewApi, OpusFromFormat_stereo_20ms_voip) {
-  const auto config = AudioEncoderOpus::SdpToConfig(
+  auto config = AudioEncoderOpus::SdpToConfig(
       SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}));
+  // If not set, default will be kAudio in case of stereo.
+  config->application = AudioEncoderOpusConfig::ApplicationMode::kVoip;
   ASSERT_NO_FATAL_FAILURE(SetUpTestExternalEncoder(
       AudioEncoderOpus::MakeAudioEncoder(*config, 120), 120));
-  // If not set, default will be kAudio in case of stereo.
-  EXPECT_EQ(0, send_test_->acm()->SetOpusApplication(kVoip));
   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
           "b0325df4e8104f04e03af23c0b75800e",
           "b0325df4e8104f04e03af23c0b75800e",
diff --git a/modules/audio_coding/acm2/rent_a_codec.cc b/modules/audio_coding/acm2/rent_a_codec.cc
index 264c2a8..0a9ce11 100644
--- a/modules/audio_coding/acm2/rent_a_codec.cc
+++ b/modules/audio_coding/acm2/rent_a_codec.cc
@@ -18,7 +18,7 @@
 #include "rtc_base/logging.h"
 #include "modules/audio_coding/codecs/g722/audio_encoder_g722.h"
 #ifdef WEBRTC_CODEC_ILBC
-#include "modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
+#include "modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"  // nogncheck
 #endif
 #ifdef WEBRTC_CODEC_ISACFX
 #include "modules/audio_coding/codecs/isac/fix/include/audio_decoder_isacfix.h"  // nogncheck
@@ -33,7 +33,7 @@
 #endif
 #include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
 #ifdef WEBRTC_CODEC_RED
-#include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
+#include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"  // nogncheck
 #endif
 #include "modules/audio_coding/acm2/acm_codec_database.h"
 
diff --git a/modules/audio_coding/codecs/isac/unittest.cc b/modules/audio_coding/codecs/isac/unittest.cc
index 4e76e0d..5855d56 100644
--- a/modules/audio_coding/codecs/isac/unittest.cc
+++ b/modules/audio_coding/codecs/isac/unittest.cc
@@ -10,7 +10,6 @@
 
 #include <algorithm>
 #include <numeric>
-#include <sstream>
 #include <vector>
 
 #include "modules/audio_coding/codecs/isac/fix/include/audio_encoder_isacfix.h"
@@ -18,6 +17,7 @@
 #include "modules/audio_coding/neteq/tools/input_audio_file.h"
 #include "rtc_base/buffer.h"
 #include "rtc_base/numerics/safe_conversions.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 #include "test/testsupport/fileutils.h"
 
@@ -142,7 +142,7 @@
 
   int elapsed_time_ms = 0;
   for (int i = 0; elapsed_time_ms < 10000; ++i) {
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << " i = " << i;
     SCOPED_TRACE(ss.str());
 
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
index e6240e6..c07abbe 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc
@@ -214,8 +214,80 @@
   return *config.bitrate_bps;
 }
 
+bool IsValidPacketLossRate(int value) {
+  return value >= 0 && value <= 100;
+}
+
+float ToFraction(int percent) {
+  return static_cast<float>(percent) / 100;
+}
+
+float GetMinPacketLossRate() {
+  constexpr char kPacketLossFieldTrial[] = "WebRTC-Audio-OpusMinPacketLossRate";
+  const bool use_opus_min_packet_loss_rate =
+      webrtc::field_trial::IsEnabled(kPacketLossFieldTrial);
+  if (use_opus_min_packet_loss_rate) {
+    const std::string field_trial_string =
+        webrtc::field_trial::FindFullName(kPacketLossFieldTrial);
+    constexpr int kDefaultMinPacketLossRate = 1;
+    int value = kDefaultMinPacketLossRate;
+    if (sscanf(field_trial_string.c_str(), "Enabled-%d", &value) == 1 &&
+        !IsValidPacketLossRate(value)) {
+      RTC_LOG(LS_WARNING) << "Invalid parameter for " << kPacketLossFieldTrial
+                          << ", using default value: "
+                          << kDefaultMinPacketLossRate;
+      value = kDefaultMinPacketLossRate;
+    }
+    return ToFraction(value);
+  }
+  return 0.0;
+}
+
+std::unique_ptr<AudioEncoderOpusImpl::NewPacketLossRateOptimizer>
+GetNewPacketLossRateOptimizer() {
+  constexpr char kPacketLossOptimizationName[] =
+      "WebRTC-Audio-NewOpusPacketLossRateOptimization";
+  const bool use_new_packet_loss_optimization =
+      webrtc::field_trial::IsEnabled(kPacketLossOptimizationName);
+  if (use_new_packet_loss_optimization) {
+    const std::string field_trial_string =
+        webrtc::field_trial::FindFullName(kPacketLossOptimizationName);
+    int min_rate;
+    int max_rate;
+    float slope;
+    if (sscanf(field_trial_string.c_str(), "Enabled-%d-%d-%f", &min_rate,
+               &max_rate, &slope) == 3 &&
+        IsValidPacketLossRate(min_rate) && IsValidPacketLossRate(max_rate)) {
+      return absl::make_unique<
+          AudioEncoderOpusImpl::NewPacketLossRateOptimizer>(
+          ToFraction(min_rate), ToFraction(max_rate), slope);
+    }
+    RTC_LOG(LS_WARNING) << "Invalid parameters for "
+                        << kPacketLossOptimizationName
+                        << ", using default values.";
+    return absl::make_unique<
+        AudioEncoderOpusImpl::NewPacketLossRateOptimizer>();
+  }
+  return nullptr;
+}
+
 }  // namespace
 
+AudioEncoderOpusImpl::NewPacketLossRateOptimizer::NewPacketLossRateOptimizer(
+    float min_packet_loss_rate,
+    float max_packet_loss_rate,
+    float slope)
+    : min_packet_loss_rate_(min_packet_loss_rate),
+      max_packet_loss_rate_(max_packet_loss_rate),
+      slope_(slope) {}
+
+float AudioEncoderOpusImpl::NewPacketLossRateOptimizer::OptimizePacketLossRate(
+    float packet_loss_rate) const {
+  packet_loss_rate = slope_ * packet_loss_rate;
+  return std::min(std::max(packet_loss_rate, min_packet_loss_rate_),
+                  max_packet_loss_rate_);
+}
+
 void AudioEncoderOpusImpl::AppendSupportedEncoders(
     std::vector<AudioCodecSpec>* specs) {
   const SdpAudioFormat fmt = {
@@ -402,6 +474,8 @@
           webrtc::field_trial::IsEnabled("WebRTC-AdjustOpusBandwidth")),
       bitrate_changed_(true),
       packet_loss_rate_(0.0),
+      min_packet_loss_rate_(GetMinPacketLossRate()),
+      new_packet_loss_optimizer_(GetNewPacketLossRateOptimizer()),
       inst_(nullptr),
       packet_loss_fraction_smoother_(new PacketLossFractionSmoother()),
       audio_network_adaptor_creator_(audio_network_adaptor_creator),
@@ -414,6 +488,7 @@
   RTC_CHECK(config.payload_type == -1 || config.payload_type == payload_type);
 
   RTC_CHECK(RecreateEncoderInstance(config));
+  SetProjectedPacketLossRate(packet_loss_rate_);
 }
 
 AudioEncoderOpusImpl::AudioEncoderOpusImpl(const CodecInst& codec_inst)
@@ -738,9 +813,14 @@
 }
 
 void AudioEncoderOpusImpl::SetProjectedPacketLossRate(float fraction) {
-  float opt_loss_rate = OptimizePacketLossRate(fraction, packet_loss_rate_);
-  if (packet_loss_rate_ != opt_loss_rate) {
-    packet_loss_rate_ = opt_loss_rate;
+  if (new_packet_loss_optimizer_) {
+    fraction = new_packet_loss_optimizer_->OptimizePacketLossRate(fraction);
+  } else {
+    fraction = OptimizePacketLossRate(fraction, packet_loss_rate_);
+    fraction = std::max(fraction, min_packet_loss_rate_);
+  }
+  if (packet_loss_rate_ != fraction) {
+    packet_loss_rate_ = fraction;
     RTC_CHECK_EQ(
         0, WebRtcOpus_SetPacketLossRate(
                inst_, static_cast<int32_t>(packet_loss_rate_ * 100 + .5)));
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.h b/modules/audio_coding/codecs/opus/audio_encoder_opus.h
index ea4b265..c26c6da 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus.h
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.h
@@ -34,6 +34,26 @@
 
 class AudioEncoderOpusImpl final : public AudioEncoder {
  public:
+  class NewPacketLossRateOptimizer {
+   public:
+    NewPacketLossRateOptimizer(float min_packet_loss_rate = 0.01,
+                               float max_packet_loss_rate = 0.2,
+                               float slope = 1.0);
+
+    float OptimizePacketLossRate(float packet_loss_rate) const;
+
+    // Getters for testing.
+    float min_packet_loss_rate() const { return min_packet_loss_rate_; };
+    float max_packet_loss_rate() const { return max_packet_loss_rate_; };
+    float slope() const { return slope_; };
+
+   private:
+    const float min_packet_loss_rate_;
+    const float max_packet_loss_rate_;
+    const float slope_;
+    RTC_DISALLOW_COPY_AND_ASSIGN(NewPacketLossRateOptimizer);
+  };
+
   static AudioEncoderOpusConfig CreateConfig(const CodecInst& codec_inst);
 
   // Returns empty if the current bitrate falls within the hysteresis window,
@@ -110,6 +130,9 @@
 
   // Getters for testing.
   float packet_loss_rate() const { return packet_loss_rate_; }
+  NewPacketLossRateOptimizer* new_packet_loss_optimizer() const {
+    return new_packet_loss_optimizer_.get();
+  }
   AudioEncoderOpusConfig::ApplicationMode application() const {
     return config_.application;
   }
@@ -158,6 +181,8 @@
   const bool adjust_bandwidth_;
   bool bitrate_changed_;
   float packet_loss_rate_;
+  const float min_packet_loss_rate_;
+  const std::unique_ptr<NewPacketLossRateOptimizer> new_packet_loss_optimizer_;
   std::vector<int16_t> input_buffer_;
   OpusEncInst* inst_;
   uint32_t first_timestamp_in_buffer_;
diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
index 7a6d5fd..d5c2c84 100644
--- a/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
+++ b/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc
@@ -272,6 +272,51 @@
   // clang-format on
 }
 
+TEST(AudioEncoderOpusTest, PacketLossRateLowerBounded) {
+  test::ScopedFieldTrials override_field_trials(
+      "WebRTC-Audio-OpusMinPacketLossRate/Enabled-5/");
+  auto states = CreateCodec(1);
+  auto I = [](float a, float b) { return IntervalSteps(a, b, 10); };
+  constexpr float eps = 1e-8f;
+
+  // clang-format off
+  TestSetPacketLossRate(states.get(), I(0.00f      , 0.01f - eps), 0.05f);
+  TestSetPacketLossRate(states.get(), I(0.01f + eps, 0.06f - eps), 0.05f);
+  TestSetPacketLossRate(states.get(), I(0.06f + eps, 0.11f - eps), 0.05f);
+  TestSetPacketLossRate(states.get(), I(0.11f + eps, 0.22f - eps), 0.10f);
+  TestSetPacketLossRate(states.get(), I(0.22f + eps, 1.00f      ), 0.20f);
+
+  TestSetPacketLossRate(states.get(), I(1.00f      , 0.18f + eps), 0.20f);
+  TestSetPacketLossRate(states.get(), I(0.18f - eps, 0.09f + eps), 0.10f);
+  TestSetPacketLossRate(states.get(), I(0.09f - eps, 0.04f + eps), 0.05f);
+  TestSetPacketLossRate(states.get(), I(0.04f - eps, 0.01f + eps), 0.05f);
+  TestSetPacketLossRate(states.get(), I(0.01f - eps, 0.00f      ), 0.05f);
+  // clang-format on
+}
+
+TEST(AudioEncoderOpusTest, NewPacketLossRateOptimization) {
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-NewOpusPacketLossRateOptimization/Enabled-5-15-0.5/");
+    auto states = CreateCodec(1);
+
+    TestSetPacketLossRate(states.get(), {0.00f}, 0.05f);
+    TestSetPacketLossRate(states.get(), {0.12f}, 0.06f);
+    TestSetPacketLossRate(states.get(), {0.22f}, 0.11f);
+    TestSetPacketLossRate(states.get(), {0.50f}, 0.15f);
+  }
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-NewOpusPacketLossRateOptimization/Enabled/");
+    auto states = CreateCodec(1);
+
+    TestSetPacketLossRate(states.get(), {0.00f}, 0.01f);
+    TestSetPacketLossRate(states.get(), {0.12f}, 0.12f);
+    TestSetPacketLossRate(states.get(), {0.22f}, 0.20f);
+    TestSetPacketLossRate(states.get(), {0.50f}, 0.20f);
+  }
+}
+
 TEST(AudioEncoderOpusTest, SetReceiverFrameLengthRange) {
   auto states = CreateCodec(2);
   // Before calling to |SetReceiverFrameLengthRange|,
@@ -446,6 +491,57 @@
   EXPECT_EQ(kMaxBitrateBps, states->encoder->GetTargetBitrate());
 }
 
+TEST(AudioEncoderOpusTest, MinPacketLossRate) {
+  constexpr float kDefaultMinPacketLossRate = 0.01;
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-OpusMinPacketLossRate/Enabled/");
+    auto states = CreateCodec(1);
+    EXPECT_EQ(kDefaultMinPacketLossRate, states->encoder->packet_loss_rate());
+  }
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-OpusMinPacketLossRate/Enabled-200/");
+    auto states = CreateCodec(1);
+    EXPECT_EQ(kDefaultMinPacketLossRate, states->encoder->packet_loss_rate());
+  }
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-OpusMinPacketLossRate/Enabled-50/");
+    constexpr float kMinPacketLossRate = 0.5;
+    auto states = CreateCodec(1);
+    EXPECT_EQ(kMinPacketLossRate, states->encoder->packet_loss_rate());
+  }
+}
+
+TEST(AudioEncoderOpusTest, NewPacketLossRateOptimizer) {
+  {
+    auto states = CreateCodec(1);
+    auto optimizer = states->encoder->new_packet_loss_optimizer();
+    EXPECT_EQ(nullptr, optimizer);
+  }
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-NewOpusPacketLossRateOptimization/Enabled/");
+    auto states = CreateCodec(1);
+    auto optimizer = states->encoder->new_packet_loss_optimizer();
+    ASSERT_NE(nullptr, optimizer);
+    EXPECT_FLOAT_EQ(0.01, optimizer->min_packet_loss_rate());
+    EXPECT_FLOAT_EQ(0.20, optimizer->max_packet_loss_rate());
+    EXPECT_FLOAT_EQ(1.00, optimizer->slope());
+  }
+  {
+    test::ScopedFieldTrials override_field_trials(
+        "WebRTC-Audio-NewOpusPacketLossRateOptimization/Enabled-2-50-0.7/");
+    auto states = CreateCodec(1);
+    auto optimizer = states->encoder->new_packet_loss_optimizer();
+    ASSERT_NE(nullptr, optimizer);
+    EXPECT_FLOAT_EQ(0.02, optimizer->min_packet_loss_rate());
+    EXPECT_FLOAT_EQ(0.50, optimizer->max_packet_loss_rate());
+    EXPECT_FLOAT_EQ(0.70, optimizer->slope());
+  }
+}
+
 // Verifies that the complexity adaptation in the config works as intended.
 TEST(AudioEncoderOpusTest, ConfigComplexityAdaptation) {
   AudioEncoderOpusConfig config;
diff --git a/modules/audio_coding/codecs/opus/opus_bandwidth_unittest.cc b/modules/audio_coding/codecs/opus/opus_bandwidth_unittest.cc
index 7f09c2a..7e6b626 100644
--- a/modules/audio_coding/codecs/opus/opus_bandwidth_unittest.cc
+++ b/modules/audio_coding/codecs/opus/opus_bandwidth_unittest.cc
@@ -11,8 +11,8 @@
 #include "api/audio_codecs/opus/audio_decoder_opus.h"
 #include "api/audio_codecs/opus/audio_encoder_opus.h"
 #include "common_audio/include/audio_util.h"
-#include "common_audio/lapped_transform.h"
 #include "common_audio/window_generator.h"
+#include "modules/audio_coding/codecs/opus/test/lapped_transform.h"
 #include "modules/audio_coding/neteq/tools/audio_loop.h"
 #include "test/field_trial.h"
 #include "test/gtest.h"
diff --git a/modules/audio_coding/include/audio_coding_module.h b/modules/audio_coding/include/audio_coding_module.h
index 840a719..b9f2228 100644
--- a/modules/audio_coding/include/audio_coding_module.h
+++ b/modules/audio_coding/include/audio_coding_module.h
@@ -626,22 +626,6 @@
   //
 
   ///////////////////////////////////////////////////////////////////////////
-  // int SetOpusApplication()
-  // Sets the intended application if current send codec is Opus. Opus uses this
-  // to optimize the encoding for applications like VOIP and music. Currently,
-  // two modes are supported: kVoip and kAudio.
-  //
-  // Input:
-  //   - application            : intended application.
-  //
-  // Return value:
-  //   -1 if current send codec is not Opus or error occurred in setting the
-  //      Opus application mode.
-  //    0 if the Opus application mode is successfully set.
-  //
-  virtual int SetOpusApplication(OpusApplicationMode application) = 0;
-
-  ///////////////////////////////////////////////////////////////////////////
   // int SetOpusMaxPlaybackRate()
   // If current send codec is Opus, informs it about maximum playback rate the
   // receiver will render. Opus can use this information to optimize the bit
diff --git a/modules/audio_coding/neteq/accelerate.cc b/modules/audio_coding/neteq/accelerate.cc
index 183ad7b..18350b0 100644
--- a/modules/audio_coding/neteq/accelerate.cc
+++ b/modules/audio_coding/neteq/accelerate.cc
@@ -25,7 +25,8 @@
       input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) {
     // Length of input data too short to do accelerate. Simply move all data
     // from input to output.
-    output->PushBackInterleaved(input, input_length);
+    output->PushBackInterleaved(
+        rtc::ArrayView<const int16_t>(input, input_length));
     return kError;
   }
   return TimeStretch::Process(input, input_length, fast_accelerate, output,
@@ -67,17 +68,18 @@
 
     assert(fs_mult_120 >= peak_index);  // Should be handled in Process().
     // Copy first part; 0 to 15 ms.
-    output->PushBackInterleaved(input, fs_mult_120 * num_channels_);
+    output->PushBackInterleaved(
+        rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
     // Copy the |peak_index| starting at 15 ms to |temp_vector|.
     AudioMultiVector temp_vector(num_channels_);
-    temp_vector.PushBackInterleaved(&input[fs_mult_120 * num_channels_],
-                                    peak_index * num_channels_);
+    temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
+        &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
     // Cross-fade |temp_vector| onto the end of |output|.
     output->CrossFade(temp_vector, peak_index);
     // Copy the last unmodified part, 15 ms + pitch period until the end.
-    output->PushBackInterleaved(
+    output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
         &input[(fs_mult_120 + peak_index) * num_channels_],
-        input_length - (fs_mult_120 + peak_index) * num_channels_);
+        input_length - (fs_mult_120 + peak_index) * num_channels_));
 
     if (active_speech) {
       return kSuccess;
@@ -86,7 +88,8 @@
     }
   } else {
     // Accelerate not allowed. Simply move all data from decoded to outData.
-    output->PushBackInterleaved(input, input_length);
+    output->PushBackInterleaved(
+        rtc::ArrayView<const int16_t>(input, input_length));
     return kNoStretch;
   }
 }
diff --git a/modules/audio_coding/neteq/accelerate.h b/modules/audio_coding/neteq/accelerate.h
index b0bab32..5609568 100644
--- a/modules/audio_coding/neteq/accelerate.h
+++ b/modules/audio_coding/neteq/accelerate.h
@@ -11,8 +11,6 @@
 #ifndef MODULES_AUDIO_CODING_NETEQ_ACCELERATE_H_
 #define MODULES_AUDIO_CODING_NETEQ_ACCELERATE_H_
 
-#include <assert.h>
-
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
 #include "modules/audio_coding/neteq/time_stretch.h"
 #include "rtc_base/constructormagic.h"
diff --git a/modules/audio_coding/neteq/audio_multi_vector.cc b/modules/audio_coding/neteq/audio_multi_vector.cc
index 874633f..349d75d 100644
--- a/modules/audio_coding/neteq/audio_multi_vector.cc
+++ b/modules/audio_coding/neteq/audio_multi_vector.cc
@@ -67,15 +67,15 @@
   }
 }
 
-void AudioMultiVector::PushBackInterleaved(const int16_t* append_this,
-                                           size_t length) {
-  assert(length % num_channels_ == 0);
+void AudioMultiVector::PushBackInterleaved(
+    rtc::ArrayView<const int16_t> append_this) {
+  RTC_DCHECK_EQ(append_this.size() % num_channels_, 0);
   if (num_channels_ == 1) {
     // Special case to avoid extra allocation and data shuffling.
-    channels_[0]->PushBack(append_this, length);
+    channels_[0]->PushBack(append_this.data(), append_this.size());
     return;
   }
-  size_t length_per_channel = length / num_channels_;
+  size_t length_per_channel = append_this.size() / num_channels_;
   int16_t* temp_array = new int16_t[length_per_channel];  // Temporary storage.
   for (size_t channel = 0; channel < num_channels_; ++channel) {
     // Copy elements to |temp_array|.
diff --git a/modules/audio_coding/neteq/audio_multi_vector.h b/modules/audio_coding/neteq/audio_multi_vector.h
index 4a9ed48..86f8282 100644
--- a/modules/audio_coding/neteq/audio_multi_vector.h
+++ b/modules/audio_coding/neteq/audio_multi_vector.h
@@ -15,6 +15,7 @@
 
 #include <vector>
 
+#include "api/array_view.h"
 #include "modules/audio_coding/neteq/audio_vector.h"
 #include "rtc_base/constructormagic.h"
 
@@ -44,12 +45,11 @@
   // number of channels.
   virtual void CopyTo(AudioMultiVector* copy_to) const;
 
-  // Appends the contents of array |append_this| to the end of this
-  // object. The array is assumed to be channel-interleaved. |length| must be
-  // an even multiple of this object's number of channels.
-  // The length of this object is increased with the |length| divided by the
-  // number of channels.
-  virtual void PushBackInterleaved(const int16_t* append_this, size_t length);
+  // Appends the contents of |append_this| to the end of this object. The array
+  // is assumed to be channel-interleaved. The length must be an even multiple
+  // of this object's number of channels. The length of this object is increased
+  // with the length of the array divided by the number of channels.
+  void PushBackInterleaved(rtc::ArrayView<const int16_t> append_this);
 
   // Appends the contents of AudioMultiVector |append_this| to this object. The
   // length of this object is increased with the length of |append_this|.
diff --git a/modules/audio_coding/neteq/audio_multi_vector_unittest.cc b/modules/audio_coding/neteq/audio_multi_vector_unittest.cc
index 3f3283e..5b2ec20 100644
--- a/modules/audio_coding/neteq/audio_multi_vector_unittest.cc
+++ b/modules/audio_coding/neteq/audio_multi_vector_unittest.cc
@@ -10,10 +10,10 @@
 
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
 
-#include <assert.h>
 #include <stdlib.h>
 
 #include <string>
+#include <vector>
 
 #include "rtc_base/numerics/safe_conversions.h"
 #include "test/gtest.h"
@@ -32,18 +32,16 @@
  protected:
   AudioMultiVectorTest()
       : num_channels_(GetParam()),  // Get the test parameter.
-        interleaved_length_(num_channels_ * array_length()) {
-    array_interleaved_ = new int16_t[num_channels_ * array_length()];
-  }
+        array_interleaved_(num_channels_ * array_length()) {}
 
-  ~AudioMultiVectorTest() { delete[] array_interleaved_; }
+  ~AudioMultiVectorTest() = default;
 
   virtual void SetUp() {
     // Populate test arrays.
     for (size_t i = 0; i < array_length(); ++i) {
       array_[i] = static_cast<int16_t>(i);
     }
-    int16_t* ptr = array_interleaved_;
+    int16_t* ptr = array_interleaved_.data();
     // Write 100, 101, 102, ... for first channel.
     // Write 200, 201, 202, ... for second channel.
     // And so on.
@@ -58,9 +56,8 @@
   size_t array_length() const { return sizeof(array_) / sizeof(array_[0]); }
 
   const size_t num_channels_;
-  size_t interleaved_length_;
   int16_t array_[10];
-  int16_t* array_interleaved_;
+  std::vector<int16_t> array_interleaved_;
 };
 
 // Create and destroy AudioMultiVector objects, both empty and with a predefined
@@ -95,7 +92,7 @@
 // method is also invoked.
 TEST_P(AudioMultiVectorTest, PushBackInterleavedAndCopy) {
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   AudioMultiVector vec_copy(num_channels_);
   vec.CopyTo(&vec_copy);  // Copy from |vec| to |vec_copy|.
   ASSERT_EQ(num_channels_, vec.Channels());
@@ -122,7 +119,7 @@
 TEST_P(AudioMultiVectorTest, CopyToNull) {
   AudioMultiVector vec(num_channels_);
   AudioMultiVector* vec_copy = NULL;
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   vec.CopyTo(vec_copy);
 }
 
@@ -154,7 +151,7 @@
 // Test the PushBackFromIndex method.
 TEST_P(AudioMultiVectorTest, PushBackFromIndex) {
   AudioMultiVector vec1(num_channels_);
-  vec1.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec1.PushBackInterleaved(array_interleaved_);
   AudioMultiVector vec2(num_channels_);
 
   // Append vec1 to the back of vec2 (which is empty). Read vec1 from the second
@@ -173,7 +170,7 @@
 // Starts with pushing some values to the vector, then test the Zeros method.
 TEST_P(AudioMultiVectorTest, Zeros) {
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   vec.Zeros(2 * array_length());
   ASSERT_EQ(num_channels_, vec.Channels());
   ASSERT_EQ(2u * array_length(), vec.Size());
@@ -187,20 +184,20 @@
 // Test the ReadInterleaved method
 TEST_P(AudioMultiVectorTest, ReadInterleaved) {
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
-  int16_t* output = new int16_t[interleaved_length_];
+  vec.PushBackInterleaved(array_interleaved_);
+  int16_t* output = new int16_t[array_interleaved_.size()];
   // Read 5 samples.
   size_t read_samples = 5;
   EXPECT_EQ(num_channels_ * read_samples,
             vec.ReadInterleaved(read_samples, output));
-  EXPECT_EQ(0,
-            memcmp(array_interleaved_, output, read_samples * sizeof(int16_t)));
+  EXPECT_EQ(0, memcmp(array_interleaved_.data(), output,
+                      read_samples * sizeof(int16_t)));
 
   // Read too many samples. Expect to get all samples from the vector.
-  EXPECT_EQ(interleaved_length_,
+  EXPECT_EQ(array_interleaved_.size(),
             vec.ReadInterleaved(array_length() + 1, output));
-  EXPECT_EQ(0,
-            memcmp(array_interleaved_, output, read_samples * sizeof(int16_t)));
+  EXPECT_EQ(0, memcmp(array_interleaved_.data(), output,
+                      read_samples * sizeof(int16_t)));
 
   delete[] output;
 }
@@ -208,7 +205,7 @@
 // Test the PopFront method.
 TEST_P(AudioMultiVectorTest, PopFront) {
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   vec.PopFront(1);  // Remove one element from each channel.
   ASSERT_EQ(array_length() - 1u, vec.Size());
   // Let |ptr| point to the second element of the first channel in the
@@ -227,12 +224,12 @@
 // Test the PopBack method.
 TEST_P(AudioMultiVectorTest, PopBack) {
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   vec.PopBack(1);  // Remove one element from each channel.
   ASSERT_EQ(array_length() - 1u, vec.Size());
   // Let |ptr| point to the first element of the first channel in the
   // interleaved array.
-  int16_t* ptr = array_interleaved_;
+  int16_t* ptr = array_interleaved_.data();
   for (size_t i = 0; i < array_length() - 1; ++i) {
     for (size_t channel = 0; channel < num_channels_; ++channel) {
       EXPECT_EQ(*ptr, vec[channel][i]);
@@ -265,7 +262,7 @@
 // Test the PushBack method with another AudioMultiVector as input argument.
 TEST_P(AudioMultiVectorTest, OverwriteAt) {
   AudioMultiVector vec1(num_channels_);
-  vec1.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec1.PushBackInterleaved(array_interleaved_);
   AudioMultiVector vec2(num_channels_);
   vec2.Zeros(3);  // 3 zeros in each channel.
   // Overwrite vec2 at position 5.
@@ -273,7 +270,7 @@
   // Verify result.
   // Length remains the same.
   ASSERT_EQ(array_length(), vec1.Size());
-  int16_t* ptr = array_interleaved_;
+  int16_t* ptr = array_interleaved_.data();
   for (size_t i = 0; i < array_length() - 1; ++i) {
     for (size_t channel = 0; channel < num_channels_; ++channel) {
       if (i >= 5 && i <= 7) {
@@ -294,7 +291,7 @@
     return;
 
   AudioMultiVector vec(num_channels_);
-  vec.PushBackInterleaved(array_interleaved_, interleaved_length_);
+  vec.PushBackInterleaved(array_interleaved_);
   // Create a reference copy.
   AudioMultiVector ref(num_channels_);
   ref.PushBack(vec);
diff --git a/modules/audio_coding/neteq/audio_vector_unittest.cc b/modules/audio_coding/neteq/audio_vector_unittest.cc
index 1a16bb3..e39774c 100644
--- a/modules/audio_coding/neteq/audio_vector_unittest.cc
+++ b/modules/audio_coding/neteq/audio_vector_unittest.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/neteq/audio_vector.h"
 
-#include <assert.h>
 #include <stdlib.h>
 
 #include <string>
diff --git a/modules/audio_coding/neteq/buffer_level_filter.cc b/modules/audio_coding/neteq/buffer_level_filter.cc
index 4d015b6..6e8da0a 100644
--- a/modules/audio_coding/neteq/buffer_level_filter.cc
+++ b/modules/audio_coding/neteq/buffer_level_filter.cc
@@ -12,6 +12,8 @@
 
 #include <algorithm>  // Provide access to std::max.
 
+#include "rtc_base/numerics/safe_conversions.h"
+
 namespace webrtc {
 
 BufferLevelFilter::BufferLevelFilter() {
@@ -33,7 +35,7 @@
   // |buffer_size_packets| is in Q0.
   filtered_current_level_ =
       ((level_factor_ * filtered_current_level_) >> 8) +
-      ((256 - level_factor_) * static_cast<int>(buffer_size_packets));
+      ((256 - level_factor_) * rtc::dchecked_cast<int>(buffer_size_packets));
 
   // Account for time-scale operations (accelerate and pre-emptive expand).
   if (time_stretched_samples && packet_len_samples > 0) {
@@ -41,9 +43,13 @@
     // value of |time_stretched_samples| from |filtered_current_level_| after
     // converting |time_stretched_samples| from samples to packets in Q8.
     // Make sure that the filtered value remains non-negative.
-    filtered_current_level_ = std::max(
-        0, filtered_current_level_ - (time_stretched_samples * (1 << 8)) /
-                                         static_cast<int>(packet_len_samples));
+
+    int64_t time_stretched_packets =
+        (int64_t{time_stretched_samples} * (1 << 8)) /
+        rtc::dchecked_cast<int64_t>(packet_len_samples);
+
+    filtered_current_level_ = rtc::saturated_cast<int>(
+        std::max<int64_t>(0, filtered_current_level_ - time_stretched_packets));
   }
 }
 
diff --git a/modules/audio_coding/neteq/buffer_level_filter_unittest.cc b/modules/audio_coding/neteq/buffer_level_filter_unittest.cc
index b6dcd2a..1f12e73 100644
--- a/modules/audio_coding/neteq/buffer_level_filter_unittest.cc
+++ b/modules/audio_coding/neteq/buffer_level_filter_unittest.cc
@@ -14,6 +14,7 @@
 
 #include <math.h>  // Access to pow function.
 
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -30,7 +31,7 @@
     for (int value = 100; value <= 200; value += 10) {
       filter.Reset();
       filter.SetTargetBufferLevel(1);  // Makes filter coefficient 251/256.
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "times = " << times << ", value = " << value;
       SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
       for (int i = 0; i < times; ++i) {
diff --git a/modules/audio_coding/neteq/decision_logic.cc b/modules/audio_coding/neteq/decision_logic.cc
index e24ca62..349fdab 100644
--- a/modules/audio_coding/neteq/decision_logic.cc
+++ b/modules/audio_coding/neteq/decision_logic.cc
@@ -21,8 +21,38 @@
 #include "modules/audio_coding/neteq/packet_buffer.h"
 #include "modules/audio_coding/neteq/sync_buffer.h"
 #include "modules/include/module_common_types.h"
+#include "rtc_base/logging.h"
 #include "system_wrappers/include/field_trial.h"
 
+namespace {
+constexpr char kPostponeDecodingFieldTrial[] =
+    "WebRTC-Audio-NetEqPostponeDecodingAfterExpand";
+
+int GetPostponeDecodingLevel() {
+  const bool enabled =
+      webrtc::field_trial::IsEnabled(kPostponeDecodingFieldTrial);
+  if (!enabled)
+    return 0;
+
+  constexpr int kDefaultPostponeDecodingLevel = 50;
+  const std::string field_trial_string =
+      webrtc::field_trial::FindFullName(kPostponeDecodingFieldTrial);
+  int value = -1;
+  if (sscanf(field_trial_string.c_str(), "Enabled-%d", &value) == 1) {
+    if (value >= 0 && value <= 100) {
+      return value;
+    } else {
+      RTC_LOG(LS_WARNING)
+          << "Wrong value (" << value
+          << ") for postpone decoding after expand, using default ("
+          << kDefaultPostponeDecodingLevel << ")";
+    }
+  }
+  return kDefaultPostponeDecodingLevel;
+}
+
+}  // namespace
+
 namespace webrtc {
 
 DecisionLogic* DecisionLogic::Create(int fs_hz,
@@ -59,8 +89,7 @@
       timescale_countdown_(
           tick_timer_->GetNewCountdown(kMinTimescaleInterval + 1)),
       num_consecutive_expands_(0),
-      postpone_decoding_after_expand_(field_trial::IsEnabled(
-          "WebRTC-Audio-NetEqPostponeDecodingAfterExpand")) {
+      postpone_decoding_level_(GetPostponeDecodingLevel()) {
   delay_manager_->set_streaming_mode(false);
   SetSampleRate(fs_hz, output_size_samples);
 }
@@ -164,11 +193,13 @@
   // if the mute factor is low enough (otherwise the expansion was short enough
   // to not be noticable).
   // Note that the MuteFactor is in Q14, so a value of 16384 corresponds to 1.
-  if (postpone_decoding_after_expand_ && prev_mode == kModeExpand &&
-      !packet_buffer_.ContainsDtxOrCngPacket(decoder_database_) &&
-      cur_size_samples<static_cast<size_t>(delay_manager_->TargetLevel() *
-                                           packet_length_samples_)>> 8 &&
-      expand.MuteFactor(0) < 16384 / 2) {
+  if ((prev_mode == kModeExpand || prev_mode == kModeCodecPlc) &&
+      expand.MuteFactor(0) < 16384 / 2 &&
+      cur_size_samples < static_cast<size_t>(
+              delay_manager_->TargetLevel() * packet_length_samples_ *
+              postpone_decoding_level_ / 100) >> 8 &&
+      !packet_buffer_.ContainsDtxOrCngPacket(decoder_database_)) {
+    RTC_DCHECK(webrtc::field_trial::IsEnabled(kPostponeDecodingFieldTrial));
     return kExpand;
   }
 
@@ -302,9 +333,9 @@
   // Check if we should continue with an ongoing expand because the new packet
   // is too far into the future.
   uint32_t timestamp_leap = available_timestamp - target_timestamp;
-  if ((prev_mode == kModeExpand) && !ReinitAfterExpands(timestamp_leap) &&
-      !MaxWaitForPacket() && PacketTooEarly(timestamp_leap) &&
-      UnderTargetLevel()) {
+  if ((prev_mode == kModeExpand || prev_mode == kModeCodecPlc) &&
+      !ReinitAfterExpands(timestamp_leap) && !MaxWaitForPacket() &&
+      PacketTooEarly(timestamp_leap) && UnderTargetLevel()) {
     if (play_dtmf) {
       // Still have DTMF to play, so do not do expand.
       return kDtmf;
@@ -314,6 +345,10 @@
     }
   }
 
+  if (prev_mode == kModeCodecPlc) {
+    return kNormal;
+  }
+
   const size_t samples_left =
       sync_buffer.FutureLength() - expand.overlap_length();
   const size_t cur_size_samples =
diff --git a/modules/audio_coding/neteq/decision_logic.h b/modules/audio_coding/neteq/decision_logic.h
index 00b8620..39761da 100644
--- a/modules/audio_coding/neteq/decision_logic.h
+++ b/modules/audio_coding/neteq/decision_logic.h
@@ -109,6 +109,10 @@
   }
   void set_prev_time_scale(bool value) { prev_time_scale_ = value; }
 
+  int postpone_decoding_level_for_test() const {
+    return postpone_decoding_level_;
+  }
+
  private:
   // The value 5 sets maximum time-stretch rate to about 100 ms/s.
   static const int kMinTimescaleInterval = 5;
@@ -181,7 +185,7 @@
   bool disallow_time_stretching_;
   std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
   int num_consecutive_expands_;
-  const bool postpone_decoding_after_expand_;
+  const int postpone_decoding_level_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
 };
diff --git a/modules/audio_coding/neteq/decision_logic_unittest.cc b/modules/audio_coding/neteq/decision_logic_unittest.cc
index 6929daa..08720d1 100644
--- a/modules/audio_coding/neteq/decision_logic_unittest.cc
+++ b/modules/audio_coding/neteq/decision_logic_unittest.cc
@@ -17,6 +17,7 @@
 #include "modules/audio_coding/neteq/delay_peak_detector.h"
 #include "modules/audio_coding/neteq/packet_buffer.h"
 #include "modules/audio_coding/neteq/tick_timer.h"
+#include "test/field_trial.h"
 #include "test/gtest.h"
 #include "test/mock_audio_decoder_factory.h"
 
@@ -38,6 +39,62 @@
   delete logic;
 }
 
+TEST(DecisionLogic, PostponeDecodingAfterExpansionSettings) {
+  constexpr int kDefaultPostponeDecodingLevel = 50;
+  constexpr int kFsHz = 8000;
+  constexpr int kOutputSizeSamples = kFsHz / 100;  // Samples per 10 ms.
+  DecoderDatabase decoder_database(
+      new rtc::RefCountedObject<MockAudioDecoderFactory>, absl::nullopt);
+  TickTimer tick_timer;
+  PacketBuffer packet_buffer(10, &tick_timer);
+  DelayPeakDetector delay_peak_detector(&tick_timer);
+  DelayManager delay_manager(240, &delay_peak_detector, &tick_timer);
+  BufferLevelFilter buffer_level_filter;
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqPostponeDecodingAfterExpand/Enabled/");
+    DecisionLogic logic(kFsHz, kOutputSizeSamples, false, &decoder_database,
+                        packet_buffer, &delay_manager, &buffer_level_filter,
+                        &tick_timer);
+    EXPECT_EQ(kDefaultPostponeDecodingLevel,
+              logic.postpone_decoding_level_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqPostponeDecodingAfterExpand/Enabled-65/");
+    DecisionLogic logic(kFsHz, kOutputSizeSamples, false, &decoder_database,
+                        packet_buffer, &delay_manager, &buffer_level_filter,
+                        &tick_timer);
+    EXPECT_EQ(65, logic.postpone_decoding_level_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqPostponeDecodingAfterExpand/Disabled/");
+    DecisionLogic logic(kFsHz, kOutputSizeSamples, false, &decoder_database,
+                        packet_buffer, &delay_manager, &buffer_level_filter,
+                        &tick_timer);
+    EXPECT_EQ(0, logic.postpone_decoding_level_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqPostponeDecodingAfterExpand/Enabled--1/");
+    DecisionLogic logic(kFsHz, kOutputSizeSamples, false, &decoder_database,
+                        packet_buffer, &delay_manager, &buffer_level_filter,
+                        &tick_timer);
+    EXPECT_EQ(kDefaultPostponeDecodingLevel,
+              logic.postpone_decoding_level_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqPostponeDecodingAfterExpand/Enabled-101/");
+    DecisionLogic logic(kFsHz, kOutputSizeSamples, false, &decoder_database,
+                        packet_buffer, &delay_manager, &buffer_level_filter,
+                        &tick_timer);
+    EXPECT_EQ(kDefaultPostponeDecodingLevel,
+              logic.postpone_decoding_level_for_test());
+  }
+}
+
 // TODO(hlundin): Write more tests.
 
 }  // namespace webrtc
diff --git a/modules/audio_coding/neteq/decoder_database_unittest.cc b/modules/audio_coding/neteq/decoder_database_unittest.cc
index 10043e0..3c49faa 100644
--- a/modules/audio_coding/neteq/decoder_database_unittest.cc
+++ b/modules/audio_coding/neteq/decoder_database_unittest.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/neteq/decoder_database.h"
 
-#include <assert.h>
 #include <stdlib.h>
 
 #include <string>
diff --git a/modules/audio_coding/neteq/defines.h b/modules/audio_coding/neteq/defines.h
index 768f0b9..46926fa 100644
--- a/modules/audio_coding/neteq/defines.h
+++ b/modules/audio_coding/neteq/defines.h
@@ -39,6 +39,7 @@
   kModePreemptiveExpandFail,
   kModeRfc3389Cng,
   kModeCodecInternalCng,
+  kModeCodecPlc,
   kModeDtmf,
   kModeError,
   kModeUndefined = -1
diff --git a/modules/audio_coding/neteq/delay_manager.cc b/modules/audio_coding/neteq/delay_manager.cc
index 84d457c..e5eb592 100644
--- a/modules/audio_coding/neteq/delay_manager.cc
+++ b/modules/audio_coding/neteq/delay_manager.cc
@@ -23,6 +23,41 @@
 #include "rtc_base/numerics/safe_conversions.h"
 #include "system_wrappers/include/field_trial.h"
 
+namespace {
+
+constexpr int kLimitProbability = 53687091;         // 1/20 in Q30.
+constexpr int kLimitProbabilityStreaming = 536871;  // 1/2000 in Q30.
+constexpr int kMaxStreamingPeakPeriodMs = 600000;   // 10 minutes in ms.
+constexpr int kCumulativeSumDrift = 2;  // Drift term for cumulative sum
+                                        // |iat_cumulative_sum_|.
+// Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
+constexpr int kIatFactor_ = 32745;
+constexpr int kMaxIat = 64;  // Max inter-arrival time to register.
+
+absl::optional<int> GetForcedLimitProbability() {
+  constexpr char kForceTargetDelayPercentileFieldTrial[] =
+      "WebRTC-Audio-NetEqForceTargetDelayPercentile";
+  const bool use_forced_target_delay_percentile =
+      webrtc::field_trial::IsEnabled(kForceTargetDelayPercentileFieldTrial);
+  if (use_forced_target_delay_percentile) {
+    const std::string field_trial_string = webrtc::field_trial::FindFullName(
+        kForceTargetDelayPercentileFieldTrial);
+    double percentile = -1.0;
+    if (sscanf(field_trial_string.c_str(), "Enabled-%lf", &percentile) == 1 &&
+        percentile >= 0.0 && percentile <= 100.0) {
+      return absl::make_optional<int>(static_cast<int>(
+          (1 << 30) * (100.0 - percentile) / 100.0 + 0.5));  // in Q30.
+    } else {
+      RTC_LOG(LS_WARNING) << "Invalid parameter for "
+                          << kForceTargetDelayPercentileFieldTrial
+                          << ", ignored.";
+    }
+  }
+  return absl::nullopt;
+}
+
+}  // namespace
+
 namespace webrtc {
 
 DelayManager::DelayManager(size_t max_packets_in_buffer,
@@ -46,8 +81,10 @@
       peak_detector_(*peak_detector),
       last_pack_cng_or_dtmf_(1),
       frame_length_change_experiment_(
-          field_trial::IsEnabled("WebRTC-Audio-NetEqFramelengthExperiment")) {
+          field_trial::IsEnabled("WebRTC-Audio-NetEqFramelengthExperiment")),
+      forced_limit_probability_(GetForcedLimitProbability()) {
   assert(peak_detector);  // Should never be NULL.
+
   Reset();
 }
 
@@ -253,7 +290,7 @@
 }
 
 int DelayManager::CalculateTargetLevel(int iat_packets) {
-  int limit_probability = kLimitProbability;
+  int limit_probability = forced_limit_probability_.value_or(kLimitProbability);
   if (streaming_mode_) {
     limit_probability = kLimitProbabilityStreaming;
   }
diff --git a/modules/audio_coding/neteq/delay_manager.h b/modules/audio_coding/neteq/delay_manager.h
index 00e39af..cd5fc09 100644
--- a/modules/audio_coding/neteq/delay_manager.h
+++ b/modules/audio_coding/neteq/delay_manager.h
@@ -16,6 +16,7 @@
 #include <memory>
 #include <vector>
 
+#include "absl/types/optional.h"
 #include "modules/audio_coding/neteq/tick_timer.h"
 #include "rtc_base/constructormagic.h"
 
@@ -114,16 +115,12 @@
   virtual int last_pack_cng_or_dtmf() const;
   virtual void set_last_pack_cng_or_dtmf(int value);
 
- private:
-  static const int kLimitProbability = 53687091;         // 1/20 in Q30.
-  static const int kLimitProbabilityStreaming = 536871;  // 1/2000 in Q30.
-  static const int kMaxStreamingPeakPeriodMs = 600000;   // 10 minutes in ms.
-  static const int kCumulativeSumDrift = 2;  // Drift term for cumulative sum
-                                             // |iat_cumulative_sum_|.
-  // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15.
-  static const int kIatFactor_ = 32745;
-  static const int kMaxIat = 64;  // Max inter-arrival time to register.
+  // This accessor is only intended for testing purposes.
+  const absl::optional<int>& forced_limit_probability_for_test() const {
+    return forced_limit_probability_;
+  }
 
+ private:
   // Sets |iat_vector_| to the default start distribution and sets the
   // |base_target_level_| and |target_level_| to the corresponding values.
   void ResetHistogram();
@@ -168,6 +165,7 @@
   DelayPeakDetector& peak_detector_;
   int last_pack_cng_or_dtmf_;
   const bool frame_length_change_experiment_;
+  const absl::optional<int> forced_limit_probability_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
 };
diff --git a/modules/audio_coding/neteq/delay_manager_unittest.cc b/modules/audio_coding/neteq/delay_manager_unittest.cc
index 6afed66..e4e865f 100644
--- a/modules/audio_coding/neteq/delay_manager_unittest.cc
+++ b/modules/audio_coding/neteq/delay_manager_unittest.cc
@@ -15,6 +15,7 @@
 #include <math.h>
 
 #include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
+#include "test/field_trial.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -34,11 +35,12 @@
   DelayManagerTest();
   virtual void SetUp();
   virtual void TearDown();
+  void RecreateDelayManager();
   void SetPacketAudioLength(int lengt_ms);
   void InsertNextPacket();
   void IncreaseTime(int inc_ms);
 
-  DelayManager* dm_;
+  std::unique_ptr<DelayManager> dm_;
   TickTimer tick_timer_;
   MockDelayPeakDetector detector_;
   uint16_t seq_no_;
@@ -46,11 +48,15 @@
 };
 
 DelayManagerTest::DelayManagerTest()
-    : dm_(NULL), detector_(&tick_timer_), seq_no_(0x1234), ts_(0x12345678) {}
+    : dm_(nullptr), detector_(&tick_timer_), seq_no_(0x1234), ts_(0x12345678) {}
 
 void DelayManagerTest::SetUp() {
+  RecreateDelayManager();
+}
+
+void DelayManagerTest::RecreateDelayManager() {
   EXPECT_CALL(detector_, Reset()).Times(1);
-  dm_ = new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_);
+  dm_.reset(new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_));
 }
 
 void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
@@ -71,7 +77,6 @@
 }
 void DelayManagerTest::TearDown() {
   EXPECT_CALL(detector_, Die());
-  delete dm_;
 }
 
 TEST_F(DelayManagerTest, CreateAndDestroy) {
@@ -326,6 +331,61 @@
   EXPECT_FALSE(dm_->SetMaximumDelay(60));
 }
 
+TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
+  test::ScopedFieldTrials field_trial(
+      "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
+  RecreateDelayManager();
+  EXPECT_EQ(absl::make_optional<int>(1 << 30),
+            dm_->forced_limit_probability_for_test());
+
+  SetPacketAudioLength(kFrameSizeMs);
+  // First packet arrival.
+  InsertNextPacket();
+  // Advance time by one frame size.
+  IncreaseTime(kFrameSizeMs);
+  // Second packet arrival.
+  // Expect detector update method to be called once with inter-arrival time
+  // equal to 1 packet.
+  EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
+  InsertNextPacket();
+  constexpr int kExpectedTarget = 1;
+  EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());  // In Q8.
+}
+
+TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
+    RecreateDelayManager();
+    EXPECT_EQ(absl::make_optional<int>(53687091),
+              dm_->forced_limit_probability_for_test());  // 1/20 in Q30
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
+    RecreateDelayManager();
+    EXPECT_EQ(absl::make_optional<int>(536871),
+              dm_->forced_limit_probability_for_test());  // 1/2000 in Q30
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
+    RecreateDelayManager();
+    EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
+    EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
+  }
+  {
+    test::ScopedFieldTrials field_trial(
+        "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
+    RecreateDelayManager();
+    EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
+  }
+}
+
 // Test if the histogram is stretched correctly if the packet size is decreased.
 TEST(DelayManagerIATScalingTest, StretchTest) {
   using IATVector = DelayManager::IATVector;
@@ -437,4 +497,5 @@
   scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
   EXPECT_EQ(scaled_iat, expected_result);
 }
+
 }  // namespace webrtc
diff --git a/modules/audio_coding/neteq/dtmf_buffer.cc b/modules/audio_coding/neteq/dtmf_buffer.cc
index 656cff9..f81036b 100644
--- a/modules/audio_coding/neteq/dtmf_buffer.cc
+++ b/modules/audio_coding/neteq/dtmf_buffer.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/neteq/dtmf_buffer.h"
 
-#include <assert.h>
 #include <algorithm>  // max
 
 #include "rtc_base/checks.h"
diff --git a/modules/audio_coding/neteq/dtmf_tone_generator_unittest.cc b/modules/audio_coding/neteq/dtmf_tone_generator_unittest.cc
index 11a0ac6..e843706 100644
--- a/modules/audio_coding/neteq/dtmf_tone_generator_unittest.cc
+++ b/modules/audio_coding/neteq/dtmf_tone_generator_unittest.cc
@@ -16,6 +16,7 @@
 
 #include "common_audio/include/audio_util.h"
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -33,7 +34,7 @@
     AudioMultiVector signal(channels);
 
     for (int event = 0; event <= 15; ++event) {
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Checking event " << event << " at sample rate " << fs_hz;
       SCOPED_TRACE(ss.str());
       const int kAttenuation = 0;
@@ -72,7 +73,7 @@
       EXPECT_EQ(kNumSamples, tone_gen_.Generate(kNumSamples, &ref_signal));
       // Test every 5 steps (to save time).
       for (int attenuation = 1; attenuation <= 63; attenuation += 5) {
-        std::ostringstream ss;
+        rtc::StringBuilder ss;
         ss << "Checking event " << event << " at sample rate " << fs_hz;
         ss << "; attenuation " << attenuation;
         SCOPED_TRACE(ss.str());
diff --git a/modules/audio_coding/neteq/include/neteq.h b/modules/audio_coding/neteq/include/neteq.h
index fdb73c5..530975f 100644
--- a/modules/audio_coding/neteq/include/neteq.h
+++ b/modules/audio_coding/neteq/include/neteq.h
@@ -21,6 +21,7 @@
 #include "api/audio_codecs/audio_decoder.h"
 #include "api/rtp_headers.h"
 #include "common_types.h"  // NOLINT(build/include)
+#include "modules/audio_coding/neteq/defines.h"
 #include "modules/audio_coding/neteq/neteq_decoder_enum.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/scoped_ref_ptr.h"
@@ -73,6 +74,27 @@
   uint64_t voice_concealed_samples = 0;
 };
 
+// Metrics that describe the operations performed in NetEq, and the internal
+// state.
+struct NetEqOperationsAndState {
+  // These sample counters are cumulative, and don't reset. As a reference, the
+  // total number of output samples can be found in
+  // NetEqLifetimeStatistics::total_samples_received.
+  uint64_t preemptive_samples = 0;
+  uint64_t accelerate_samples = 0;
+  // Count of the number of buffer flushes.
+  uint64_t packet_buffer_flushes = 0;
+  // The statistics below are not cumulative.
+  // The waiting time of the last decoded packet.
+  uint64_t last_waiting_time_ms = 0;
+  // The sum of the packet and jitter buffer size in ms.
+  uint64_t current_buffer_size_ms = 0;
+  // The current frame size in ms.
+  uint64_t current_frame_size_ms = 0;
+  // Flag to indicate that the next packet is available.
+  bool next_packet_available = false;
+};
+
 // This is the interface class for NetEq.
 class NetEq {
  public:
@@ -129,9 +151,14 @@
   // If muted state is enabled (through Config::enable_muted_state), |muted|
   // may be set to true after a prolonged expand period. When this happens, the
   // |data_| in |audio_frame| is not written, but should be interpreted as being
-  // all zeros.
+  // all zeros. For testing purposes, an override can be supplied in the
+  // |action_override| argument, which will cause NetEq to take this action
+  // next, instead of the action it would normally choose.
   // Returns kOK on success, or kFail in case of an error.
-  virtual int GetAudio(AudioFrame* audio_frame, bool* muted) = 0;
+  virtual int GetAudio(
+      AudioFrame* audio_frame,
+      bool* muted,
+      absl::optional<Operations> action_override = absl::nullopt) = 0;
 
   // Replaces the current set of decoders with the given one.
   virtual void SetCodecs(const std::map<int, SdpAudioFormat>& codecs) = 0;
@@ -200,6 +227,10 @@
   // never reset.
   virtual NetEqLifetimeStatistics GetLifetimeStatistics() const = 0;
 
+  // Returns statistics about the performed operations and internal state. These
+  // statistics are never reset.
+  virtual NetEqOperationsAndState GetOperationsAndState() const = 0;
+
   // Writes the current RTCP statistics to |stats|. The statistics are reset
   // and a new report period is started with the call.
   virtual void GetRtcpStatistics(RtcpStatistics* stats) = 0;
diff --git a/modules/audio_coding/neteq/merge.cc b/modules/audio_coding/neteq/merge.cc
index 3c9ad19..357ef8d 100644
--- a/modules/audio_coding/neteq/merge.cc
+++ b/modules/audio_coding/neteq/merge.cc
@@ -58,7 +58,8 @@
 
   // Transfer input signal to an AudioMultiVector.
   AudioMultiVector input_vector(num_channels_);
-  input_vector.PushBackInterleaved(input, input_length);
+  input_vector.PushBackInterleaved(
+      rtc::ArrayView<const int16_t>(input, input_length));
   size_t input_length_per_channel = input_vector.Size();
   assert(input_length_per_channel == input_length / num_channels_);
 
diff --git a/modules/audio_coding/neteq/merge.h b/modules/audio_coding/neteq/merge.h
index 235c07f..e7e0bf9 100644
--- a/modules/audio_coding/neteq/merge.h
+++ b/modules/audio_coding/neteq/merge.h
@@ -11,8 +11,6 @@
 #ifndef MODULES_AUDIO_CODING_NETEQ_MERGE_H_
 #define MODULES_AUDIO_CODING_NETEQ_MERGE_H_
 
-#include <assert.h>
-
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
 #include "rtc_base/constructormagic.h"
 
diff --git a/modules/audio_coding/neteq/neteq_decoder_plc_unittest.cc b/modules/audio_coding/neteq/neteq_decoder_plc_unittest.cc
new file mode 100644
index 0000000..8d0972c
--- /dev/null
+++ b/modules/audio_coding/neteq/neteq_decoder_plc_unittest.cc
@@ -0,0 +1,216 @@
+/*
+ *  Copyright (c) 2018 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.
+ */
+
+// Test to verify correct operation when using the decoder-internal PLC.
+
+#include <algorithm>
+#include <utility>
+#include <vector>
+
+#include "absl/types/optional.h"
+#include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
+#include "modules/audio_coding/neteq/tools/audio_checksum.h"
+#include "modules/audio_coding/neteq/tools/audio_sink.h"
+#include "modules/audio_coding/neteq/tools/encode_neteq_input.h"
+#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
+#include "modules/audio_coding/neteq/tools/input_audio_file.h"
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+#include "rtc_base/numerics/safe_conversions.h"
+#include "test/gtest.h"
+#include "test/testsupport/fileutils.h"
+
+namespace webrtc {
+namespace test {
+namespace {
+
+// This class implements a fake decoder. The decoder will read audio from a file
+// and present as output, both for regular decoding and for PLC.
+class AudioDecoderPlc : public AudioDecoder {
+ public:
+  AudioDecoderPlc(std::unique_ptr<InputAudioFile> input, int sample_rate_hz)
+      : input_(std::move(input)), sample_rate_hz_(sample_rate_hz) {}
+
+  void Reset() override {}
+  int SampleRateHz() const override { return sample_rate_hz_; }
+  size_t Channels() const override { return 1; }
+  int DecodeInternal(const uint8_t* /*encoded*/,
+                     size_t encoded_len,
+                     int sample_rate_hz,
+                     int16_t* decoded,
+                     SpeechType* speech_type) override {
+    RTC_CHECK_EQ(encoded_len / 2, 20 * sample_rate_hz_ / 1000);
+    RTC_CHECK_EQ(sample_rate_hz, sample_rate_hz_);
+    RTC_CHECK(decoded);
+    RTC_CHECK(speech_type);
+    RTC_CHECK(input_->Read(encoded_len / 2, decoded));
+    *speech_type = kSpeech;
+    last_was_plc_ = false;
+    return encoded_len / 2;
+  }
+
+  void GeneratePlc(size_t requested_samples_per_channel,
+                   rtc::BufferT<int16_t>* concealment_audio) override {
+    // Must keep a local copy of this since DecodeInternal sets it to false.
+    const bool last_was_plc = last_was_plc_;
+    SpeechType speech_type;
+    std::vector<int16_t> decoded(5760);
+    int dec_len = DecodeInternal(nullptr, 2 * 20 * sample_rate_hz_ / 1000,
+                                 sample_rate_hz_, decoded.data(), &speech_type);
+    // This fake decoder can only generate 20 ms of PLC data each time. Make
+    // sure the caller didn't ask for more.
+    RTC_CHECK_GE(dec_len, requested_samples_per_channel);
+    concealment_audio->AppendData(decoded.data(), dec_len);
+    concealed_samples_ += rtc::checked_cast<size_t>(dec_len);
+    if (!last_was_plc) {
+      ++concealment_events_;
+    }
+    last_was_plc_ = true;
+  }
+
+  size_t concealed_samples() { return concealed_samples_; }
+  size_t concealment_events() { return concealment_events_; }
+
+ private:
+  const std::unique_ptr<InputAudioFile> input_;
+  const int sample_rate_hz_;
+  size_t concealed_samples_ = 0;
+  size_t concealment_events_ = 0;
+  bool last_was_plc_ = false;
+};
+
+// An input sample generator which generates only zero-samples.
+class ZeroSampleGenerator : public EncodeNetEqInput::Generator {
+ public:
+  rtc::ArrayView<const int16_t> Generate(size_t num_samples) override {
+    vec.resize(num_samples, 0);
+    rtc::ArrayView<const int16_t> view(vec);
+    RTC_DCHECK_EQ(view.size(), num_samples);
+    return view;
+  }
+
+ private:
+  std::vector<int16_t> vec;
+};
+
+// A NetEqInput which connects to another NetEqInput, but drops a number of
+// packets on the way.
+class LossyInput : public NetEqInput {
+ public:
+  LossyInput(int loss_cadence, std::unique_ptr<NetEqInput> input)
+      : loss_cadence_(loss_cadence), input_(std::move(input)) {}
+
+  absl::optional<int64_t> NextPacketTime() const override {
+    return input_->NextPacketTime();
+  }
+
+  absl::optional<int64_t> NextOutputEventTime() const override {
+    return input_->NextOutputEventTime();
+  }
+
+  std::unique_ptr<PacketData> PopPacket() override {
+    if (loss_cadence_ != 0 && (++count_ % loss_cadence_) == 0) {
+      // Pop one extra packet to create the loss.
+      input_->PopPacket();
+    }
+    return input_->PopPacket();
+  }
+
+  void AdvanceOutputEvent() override { return input_->AdvanceOutputEvent(); }
+
+  bool ended() const override { return input_->ended(); }
+
+  absl::optional<RTPHeader> NextHeader() const override {
+    return input_->NextHeader();
+  }
+
+ private:
+  const int loss_cadence_;
+  int count_ = 0;
+  const std::unique_ptr<NetEqInput> input_;
+};
+
+class AudioChecksumWithOutput : public AudioChecksum {
+ public:
+  explicit AudioChecksumWithOutput(std::string* output_str)
+      : output_str_(*output_str) {}
+  ~AudioChecksumWithOutput() { output_str_ = Finish(); }
+
+ private:
+  std::string& output_str_;
+};
+
+NetEqNetworkStatistics RunTest(int loss_cadence, std::string* checksum) {
+  NetEq::Config config;
+  config.for_test_no_time_stretching = true;
+
+  // The input is mostly useless. It sends zero-samples to a PCM16b encoder,
+  // but the actual encoded samples will never be used by the decoder in the
+  // test. See below about the decoder.
+  auto generator = absl::make_unique<ZeroSampleGenerator>();
+  constexpr int kSampleRateHz = 32000;
+  constexpr int kPayloadType = 100;
+  AudioEncoderPcm16B::Config encoder_config;
+  encoder_config.sample_rate_hz = kSampleRateHz;
+  encoder_config.payload_type = kPayloadType;
+  auto encoder = absl::make_unique<AudioEncoderPcm16B>(encoder_config);
+  constexpr int kRunTimeMs = 10000;
+  auto input = absl::make_unique<EncodeNetEqInput>(
+      std::move(generator), std::move(encoder), kRunTimeMs);
+  // Wrap the input in a loss function.
+  auto lossy_input =
+      absl::make_unique<LossyInput>(loss_cadence, std::move(input));
+
+  // Settinng up decoders.
+  NetEqTest::DecoderMap decoders;
+  // Using a fake decoder which simply reads the output audio from a file.
+  auto input_file = absl::make_unique<InputAudioFile>(
+      webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"));
+  AudioDecoderPlc dec(std::move(input_file), kSampleRateHz);
+  // Masquerading as a PCM16b decoder.
+  NetEqTest::ExternalDecoderInfo dec_info = {
+      &dec, NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16b_PLC"};
+  NetEqTest::ExtDecoderMap external_decoders;
+  external_decoders.insert(std::make_pair(kPayloadType, dec_info));
+
+  // Output is simply a checksum calculator.
+  auto output = absl::make_unique<AudioChecksumWithOutput>(checksum);
+
+  // No callback objects.
+  NetEqTest::Callbacks callbacks;
+
+  NetEqTest neteq_test(config, decoders, external_decoders,
+                       std::move(lossy_input), std::move(output), callbacks);
+  EXPECT_LE(kRunTimeMs, neteq_test.Run());
+
+  auto lifetime_stats = neteq_test.LifetimeStats();
+  EXPECT_EQ(dec.concealed_samples(), lifetime_stats.concealed_samples);
+  EXPECT_EQ(dec.concealment_events(), lifetime_stats.concealment_events);
+
+  return neteq_test.SimulationStats();
+}
+}  // namespace
+
+TEST(NetEqDecoderPlc, Test) {
+  std::string checksum;
+  auto stats = RunTest(10, &checksum);
+
+  std::string checksum_no_loss;
+  auto stats_no_loss = RunTest(0, &checksum_no_loss);
+
+  EXPECT_EQ(checksum, checksum_no_loss);
+
+  EXPECT_EQ(stats.preemptive_rate, stats_no_loss.preemptive_rate);
+  EXPECT_EQ(stats.accelerate_rate, stats_no_loss.accelerate_rate);
+  EXPECT_EQ(0, stats_no_loss.expand_rate);
+  EXPECT_GT(stats.expand_rate, 0);
+}
+
+}  // namespace test
+}  // namespace webrtc
diff --git a/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc b/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
index 5c350bb..872829c 100644
--- a/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
+++ b/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
@@ -19,6 +19,7 @@
 #include "modules/audio_coding/neteq/tools/input_audio_file.h"
 #include "modules/audio_coding/neteq/tools/neteq_external_decoder_test.h"
 #include "modules/audio_coding/neteq/tools/rtp_generator.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gmock.h"
 #include "test/testsupport/fileutils.h"
 
@@ -122,7 +123,7 @@
         } while (Lost());  // If lost, immediately read the next packet.
       }
 
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Lap number " << k << ".";
       SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
       // Compare mono and multi-channel.
diff --git a/modules/audio_coding/neteq/neteq_impl.cc b/modules/audio_coding/neteq/neteq_impl.cc
index ddcd221..f428be1 100644
--- a/modules/audio_coding/neteq/neteq_impl.cc
+++ b/modules/audio_coding/neteq/neteq_impl.cc
@@ -199,10 +199,12 @@
 }
 }  // namespace
 
-int NetEqImpl::GetAudio(AudioFrame* audio_frame, bool* muted) {
+int NetEqImpl::GetAudio(AudioFrame* audio_frame,
+                        bool* muted,
+                        absl::optional<Operations> action_override) {
   TRACE_EVENT0("webrtc", "NetEqImpl::GetAudio");
   rtc::CritScope lock(&crit_sect_);
-  if (GetAudioInternal(audio_frame, muted) != 0) {
+  if (GetAudioInternal(audio_frame, muted, action_override) != 0) {
     return kFail;
   }
   RTC_DCHECK_EQ(
@@ -369,6 +371,20 @@
   return stats_.GetLifetimeStatistics();
 }
 
+NetEqOperationsAndState NetEqImpl::GetOperationsAndState() const {
+  rtc::CritScope lock(&crit_sect_);
+  auto result = stats_.GetOperationsAndState();
+  result.current_buffer_size_ms =
+      (packet_buffer_->NumSamplesInBuffer(decoder_frame_length_) +
+       sync_buffer_->FutureLength()) *
+      1000 / fs_hz_;
+  result.current_frame_size_ms = decoder_frame_length_ * 1000 / fs_hz_;
+  result.next_packet_available = packet_buffer_->PeekNextPacket() &&
+                                 packet_buffer_->PeekNextPacket()->timestamp ==
+                                     sync_buffer_->end_timestamp();
+  return result;
+}
+
 void NetEqImpl::GetRtcpStatistics(RtcpStatistics* stats) {
   rtc::CritScope lock(&crit_sect_);
   if (stats) {
@@ -798,7 +814,9 @@
   return 0;
 }
 
-int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame, bool* muted) {
+int NetEqImpl::GetAudioInternal(AudioFrame* audio_frame,
+                                bool* muted,
+                                absl::optional<Operations> action_override) {
   PacketList packet_list;
   DtmfEvent dtmf_event;
   Operations operation;
@@ -831,9 +849,8 @@
     *muted = true;
     return 0;
   }
-
-  int return_value =
-      GetDecision(&operation, &packet_list, &dtmf_event, &play_dtmf);
+  int return_value = GetDecision(&operation, &packet_list, &dtmf_event,
+                                 &play_dtmf, action_override);
   if (return_value != 0) {
     last_mode_ = kModeError;
     return return_value;
@@ -872,7 +889,12 @@
       break;
     }
     case kExpand: {
-      return_value = DoExpand(play_dtmf);
+      RTC_DCHECK_EQ(return_value, 0);
+      if (!current_rtp_payload_type_ || !DoCodecPlc()) {
+        return_value = DoExpand(play_dtmf);
+      }
+      RTC_DCHECK_GE(sync_buffer_->FutureLength() - expand_->overlap_length(),
+                    output_size_samples_);
       break;
     }
     case kAccelerate:
@@ -984,7 +1006,7 @@
     sync_buffer_->set_dtmf_index(sync_buffer_->Size());
   }
 
-  if (last_mode_ != kModeExpand) {
+  if (last_mode_ != kModeExpand && last_mode_ != kModeCodecPlc) {
     // If last operation was not expand, calculate the |playout_timestamp_| from
     // the |sync_buffer_|. However, do not update the |playout_timestamp_| if it
     // would be moved "backwards".
@@ -1009,7 +1031,7 @@
                 static_cast<uint32_t>(audio_frame->samples_per_channel_);
 
   if (!(last_mode_ == kModeRfc3389Cng || last_mode_ == kModeCodecInternalCng ||
-        last_mode_ == kModeExpand)) {
+        last_mode_ == kModeExpand || last_mode_ == kModeCodecPlc)) {
     generated_noise_stopwatch_.reset();
   }
 
@@ -1021,7 +1043,8 @@
 int NetEqImpl::GetDecision(Operations* operation,
                            PacketList* packet_list,
                            DtmfEvent* dtmf_event,
-                           bool* play_dtmf) {
+                           bool* play_dtmf,
+                           absl::optional<Operations> action_override) {
   // Initialize output variables.
   *play_dtmf = false;
   *operation = kUndefined;
@@ -1093,6 +1116,10 @@
       *sync_buffer_, *expand_, decoder_frame_length_, packet, last_mode_,
       *play_dtmf, generated_noise_samples, &reset_decoder_);
 
+  if (action_override) {
+    // Use the provided action instead of the decision NetEq decided on.
+    *operation = *action_override;
+  }
   // Check if we already have enough samples in the |sync_buffer_|. If so,
   // change decision to normal, unless the decision was merge, accelerate, or
   // preemptive expand.
@@ -1523,6 +1550,48 @@
   }
 }
 
+bool NetEqImpl::DoCodecPlc() {
+  AudioDecoder* decoder = decoder_database_->GetActiveDecoder();
+  if (!decoder) {
+    return false;
+  }
+  const size_t channels = algorithm_buffer_->Channels();
+  const size_t requested_samples_per_channel =
+      output_size_samples_ -
+      (sync_buffer_->FutureLength() - expand_->overlap_length());
+  concealment_audio_.Clear();
+  decoder->GeneratePlc(requested_samples_per_channel, &concealment_audio_);
+  if (concealment_audio_.empty()) {
+    // Nothing produced. Resort to regular expand.
+    return false;
+  }
+  RTC_CHECK_GE(concealment_audio_.size(),
+               requested_samples_per_channel * channels);
+  sync_buffer_->PushBackInterleaved(concealment_audio_);
+  RTC_DCHECK_NE(algorithm_buffer_->Channels(), 0);
+  const size_t concealed_samples_per_channel =
+      concealment_audio_.size() / channels;
+
+  // Update in-call and post-call statistics.
+  const bool is_new_concealment_event = (last_mode_ != kModeCodecPlc);
+  if (std::all_of(concealment_audio_.cbegin(), concealment_audio_.cend(),
+                  [](int16_t i) { return i == 0; })) {
+    // Expand operation generates only noise.
+    stats_.ExpandedNoiseSamples(concealed_samples_per_channel,
+                                is_new_concealment_event);
+  } else {
+    // Expand operation generates more than only noise.
+    stats_.ExpandedVoiceSamples(concealed_samples_per_channel,
+                                is_new_concealment_event);
+  }
+  last_mode_ = kModeCodecPlc;
+  if (!generated_noise_stopwatch_) {
+    // Start a new stopwatch since we may be covering for a lost CNG packet.
+    generated_noise_stopwatch_ = tick_timer_->GetNewStopwatch();
+  }
+  return true;
+}
+
 int NetEqImpl::DoExpand(bool play_dtmf) {
   while ((sync_buffer_->FutureLength() - expand_->overlap_length()) <
          output_size_samples_) {
diff --git a/modules/audio_coding/neteq/neteq_impl.h b/modules/audio_coding/neteq/neteq_impl.h
index 68fdf3c..8ef97ce 100644
--- a/modules/audio_coding/neteq/neteq_impl.h
+++ b/modules/audio_coding/neteq/neteq_impl.h
@@ -131,7 +131,10 @@
 
   void InsertEmptyPacket(const RTPHeader& rtp_header) override;
 
-  int GetAudio(AudioFrame* audio_frame, bool* muted) override;
+  int GetAudio(
+      AudioFrame* audio_frame,
+      bool* muted,
+      absl::optional<Operations> action_override = absl::nullopt) override;
 
   void SetCodecs(const std::map<int, SdpAudioFormat>& codecs) override;
 
@@ -173,6 +176,8 @@
 
   NetEqLifetimeStatistics GetLifetimeStatistics() const override;
 
+  NetEqOperationsAndState GetOperationsAndState() const override;
+
   // Same as RtcpStatistics(), but does not reset anything.
   void GetRtcpStatisticsNoReset(RtcpStatistics* stats) override;
 
@@ -230,7 +235,9 @@
 
   // Delivers 10 ms of audio data. The data is written to |audio_frame|.
   // Returns 0 on success, otherwise an error code.
-  int GetAudioInternal(AudioFrame* audio_frame, bool* muted)
+  int GetAudioInternal(AudioFrame* audio_frame,
+                       bool* muted,
+                       absl::optional<Operations> action_override)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
   // Provides a decision to the GetAudioInternal method. The decision what to
@@ -241,7 +248,9 @@
   int GetDecision(Operations* operation,
                   PacketList* packet_list,
                   DtmfEvent* dtmf_event,
-                  bool* play_dtmf) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+                  bool* play_dtmf,
+                  absl::optional<Operations> action_override)
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
   // Decodes the speech packets in |packet_list|, and writes the results to
   // |decoded_buffer|, which is allocated to hold |decoded_buffer_length|
@@ -281,6 +290,8 @@
                AudioDecoder::SpeechType speech_type,
                bool play_dtmf) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
+  bool DoCodecPlc() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
+
   // Sub-method which calls the Expand class to perform the expand operation.
   int DoExpand(bool play_dtmf) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
 
@@ -416,6 +427,7 @@
   ExpandUmaLogger expand_uma_logger_ RTC_GUARDED_BY(crit_sect_);
   ExpandUmaLogger speech_expand_uma_logger_ RTC_GUARDED_BY(crit_sect_);
   bool no_time_stretching_ RTC_GUARDED_BY(crit_sect_);  // Only used for test.
+  rtc::BufferT<int16_t> concealment_audio_ RTC_GUARDED_BY(crit_sect_);
 
  private:
   RTC_DISALLOW_COPY_AND_ASSIGN(NetEqImpl);
diff --git a/modules/audio_coding/neteq/neteq_stereo_unittest.cc b/modules/audio_coding/neteq/neteq_stereo_unittest.cc
index ef4c235..3f86944 100644
--- a/modules/audio_coding/neteq/neteq_stereo_unittest.cc
+++ b/modules/audio_coding/neteq/neteq_stereo_unittest.cc
@@ -22,6 +22,7 @@
 #include "modules/audio_coding/neteq/include/neteq.h"
 #include "modules/audio_coding/neteq/tools/input_audio_file.h"
 #include "modules/audio_coding/neteq/tools/rtp_generator.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 #include "test/testsupport/fileutils.h"
 
@@ -228,7 +229,7 @@
       EXPECT_EQ(num_channels_, output_multi_channel_.num_channels_);
       EXPECT_EQ(output_size_samples_,
                 output_multi_channel_.samples_per_channel_);
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Lap number " << k << ".";
       SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
       // Compare mono and multi-channel.
diff --git a/modules/audio_coding/neteq/neteq_unittest.cc b/modules/audio_coding/neteq/neteq_unittest.cc
index 96a392b..3ee0b5b 100644
--- a/modules/audio_coding/neteq/neteq_unittest.cc
+++ b/modules/audio_coding/neteq/neteq_unittest.cc
@@ -33,6 +33,7 @@
 #include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/protobuf_utils.h"
 #include "rtc_base/stringencode.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/arch.h"
 #include "test/field_trial.h"
 #include "test/gtest.h"
@@ -386,7 +387,7 @@
   uint64_t last_concealed_samples = 0;
   uint64_t last_total_samples_received = 0;
   while (packet_) {
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << "Lap number " << i++ << " in DecodeAndCompare while loop";
     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
     ASSERT_NO_FATAL_FAILURE(Process());
@@ -900,7 +901,7 @@
   static const int kExpectedOutputLength = 160;  // 10 ms at 16 kHz sample rate.
   const int16_t* const_out_frame_data = out_frame_.data();
   for (int i = 0; i < kExpectedOutputLength; ++i) {
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << "i = " << i;
     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
     EXPECT_EQ(0, const_out_frame_data[i]);
@@ -922,7 +923,7 @@
       kInitSampleRateHz / 100;  // 10 ms at initial sample rate.
   const int16_t* const_out_frame_data = out_frame_.data();
   for (int i = 0; i < kExpectedOutputLength; ++i) {
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << "i = " << i;
     SCOPED_TRACE(ss.str());  // Print out the parameter values on failure.
     EXPECT_EQ(0, const_out_frame_data[i]);
@@ -1532,7 +1533,7 @@
   AudioFrame out_frame1, out_frame2;
   bool muted;
   for (int i = 0; i < 1000; ++i) {
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << "i = " << i;
     SCOPED_TRACE(ss.str());  // Print out the loop iterator on failure.
     EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
@@ -1555,7 +1556,7 @@
   int counter = 0;
   while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) {
     ASSERT_LT(counter++, 1000) << "Test timed out";
-    std::ostringstream ss;
+    rtc::StringBuilder ss;
     ss << "counter = " << counter;
     SCOPED_TRACE(ss.str());  // Print out the loop iterator on failure.
     EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted));
diff --git a/modules/audio_coding/neteq/normal.cc b/modules/audio_coding/neteq/normal.cc
index 83f7616..713bfb6 100644
--- a/modules/audio_coding/neteq/normal.cc
+++ b/modules/audio_coding/neteq/normal.cc
@@ -41,7 +41,7 @@
     output->Clear();
     return 0;
   }
-  output->PushBackInterleaved(input, length);
+  output->PushBackInterleaved(rtc::ArrayView<const int16_t>(input, length));
 
   const int fs_mult = fs_hz_ / 8000;
   RTC_DCHECK_GT(fs_mult, 0);
diff --git a/modules/audio_coding/neteq/packet_buffer.cc b/modules/audio_coding/neteq/packet_buffer.cc
index c04534e..eba4d3e 100644
--- a/modules/audio_coding/neteq/packet_buffer.cc
+++ b/modules/audio_coding/neteq/packet_buffer.cc
@@ -91,6 +91,7 @@
   if (buffer_.size() >= max_number_of_packets_) {
     // Buffer is full. Flush it.
     Flush();
+    stats->FlushedPacketBuffer();
     RTC_LOG(LS_WARNING) << "Packet buffer flushed";
     return_val = kFlushed;
   }
diff --git a/modules/audio_coding/neteq/preemptive_expand.cc b/modules/audio_coding/neteq/preemptive_expand.cc
index 4702078..6159a9c 100644
--- a/modules/audio_coding/neteq/preemptive_expand.cc
+++ b/modules/audio_coding/neteq/preemptive_expand.cc
@@ -31,7 +31,8 @@
       old_data_length >= input_length / num_channels_ - overlap_samples_) {
     // Length of input data too short to do preemptive expand. Simply move all
     // data from input to output.
-    output->PushBackInterleaved(input, input_length);
+    output->PushBackInterleaved(
+        rtc::ArrayView<const int16_t>(input, input_length));
     return kError;
   }
   const bool kFastMode = false;  // Fast mode is not available for PE Expand.
@@ -75,19 +76,19 @@
     size_t unmodified_length =
         std::max(old_data_length_per_channel_, fs_mult_120);
     // Copy first part, including cross-fade region.
-    output->PushBackInterleaved(
-        input, (unmodified_length + peak_index) * num_channels_);
+    output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
+        input, (unmodified_length + peak_index) * num_channels_));
     // Copy the last |peak_index| samples up to 15 ms to |temp_vector|.
     AudioMultiVector temp_vector(num_channels_);
-    temp_vector.PushBackInterleaved(
+    temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
         &input[(unmodified_length - peak_index) * num_channels_],
-        peak_index * num_channels_);
+        peak_index * num_channels_));
     // Cross-fade |temp_vector| onto the end of |output|.
     output->CrossFade(temp_vector, peak_index);
     // Copy the last unmodified part, 15 ms + pitch period until the end.
-    output->PushBackInterleaved(
+    output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
         &input[unmodified_length * num_channels_],
-        input_length - unmodified_length * num_channels_);
+        input_length - unmodified_length * num_channels_));
 
     if (active_speech) {
       return kSuccess;
@@ -96,7 +97,8 @@
     }
   } else {
     // Accelerate not allowed. Simply move all data from decoded to outData.
-    output->PushBackInterleaved(input, input_length);
+    output->PushBackInterleaved(
+        rtc::ArrayView<const int16_t>(input, input_length));
     return kNoStretch;
   }
 }
diff --git a/modules/audio_coding/neteq/preemptive_expand.h b/modules/audio_coding/neteq/preemptive_expand.h
index 4d72616..ace648f 100644
--- a/modules/audio_coding/neteq/preemptive_expand.h
+++ b/modules/audio_coding/neteq/preemptive_expand.h
@@ -11,8 +11,6 @@
 #ifndef MODULES_AUDIO_CODING_NETEQ_PREEMPTIVE_EXPAND_H_
 #define MODULES_AUDIO_CODING_NETEQ_PREEMPTIVE_EXPAND_H_
 
-#include <assert.h>
-
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
 #include "modules/audio_coding/neteq/time_stretch.h"
 #include "rtc_base/constructormagic.h"
diff --git a/modules/audio_coding/neteq/statistics_calculator.cc b/modules/audio_coding/neteq/statistics_calculator.cc
index 3d5744c..807d7ee 100644
--- a/modules/audio_coding/neteq/statistics_calculator.cc
+++ b/modules/audio_coding/neteq/statistics_calculator.cc
@@ -126,7 +126,10 @@
           100),
       excess_buffer_delay_("WebRTC.Audio.AverageExcessBufferDelayMs",
                            60000,  // 60 seconds report interval.
-                           1000) {}
+                           1000),
+      buffer_full_counter_("WebRTC.Audio.JitterBufferFullPerMinute",
+                           60000,  // 60 seconds report interval.
+                           100) {}
 
 StatisticsCalculator::~StatisticsCalculator() = default;
 
@@ -200,10 +203,12 @@
 
 void StatisticsCalculator::PreemptiveExpandedSamples(size_t num_samples) {
   preemptive_samples_ += num_samples;
+  operations_and_state_.preemptive_samples += num_samples;
 }
 
 void StatisticsCalculator::AcceleratedSamples(size_t num_samples) {
   accelerate_samples_ += num_samples;
+  operations_and_state_.accelerate_samples += num_samples;
 }
 
 void StatisticsCalculator::AddZeros(size_t num_samples) {
@@ -227,6 +232,7 @@
       rtc::CheckedDivExact(static_cast<int>(1000 * num_samples), fs_hz);
   delayed_packet_outage_counter_.AdvanceClock(time_step_ms);
   excess_buffer_delay_.AdvanceClock(time_step_ms);
+  buffer_full_counter_.AdvanceClock(time_step_ms);
   timestamps_since_last_report_ += static_cast<uint32_t>(num_samples);
   if (timestamps_since_last_report_ >
       static_cast<uint32_t>(fs_hz * kMaxReportPeriod)) {
@@ -246,6 +252,11 @@
   secondary_decoded_samples_ += num_samples;
 }
 
+void StatisticsCalculator::FlushedPacketBuffer() {
+  operations_and_state_.packet_buffer_flushes++;
+  buffer_full_counter_.RegisterSample();
+}
+
 void StatisticsCalculator::LogDelayedPacketOutageEvent(int outage_duration_ms) {
   RTC_HISTOGRAM_COUNTS("WebRTC.Audio.DelayedPacketOutageEventMs",
                        outage_duration_ms, 1 /* min */, 2000 /* max */,
@@ -261,6 +272,7 @@
     waiting_times_.pop_front();
   }
   waiting_times_.push_back(waiting_time_ms);
+  operations_and_state_.last_waiting_time_ms = waiting_time_ms;
 }
 
 void StatisticsCalculator::GetNetworkStatistics(int fs_hz,
@@ -345,6 +357,10 @@
   return lifetime_stats_;
 }
 
+NetEqOperationsAndState StatisticsCalculator::GetOperationsAndState() const {
+  return operations_and_state_;
+}
+
 uint16_t StatisticsCalculator::CalculateQ14Ratio(size_t numerator,
                                                  uint32_t denominator) {
   if (numerator == 0) {
diff --git a/modules/audio_coding/neteq/statistics_calculator.h b/modules/audio_coding/neteq/statistics_calculator.h
index 5f7d06a..6a5f7f4 100644
--- a/modules/audio_coding/neteq/statistics_calculator.h
+++ b/modules/audio_coding/neteq/statistics_calculator.h
@@ -83,6 +83,9 @@
   // Reports that |num_samples| samples were decoded from secondary packets.
   void SecondaryDecodedSamples(int num_samples);
 
+  // Rerport that the packet buffer was flushed.
+  void FlushedPacketBuffer();
+
   // Logs a delayed packet outage event of |outage_duration_ms|. A delayed
   // packet outage event is defined as an expand period caused not by an actual
   // packet loss, but by a delayed packet.
@@ -111,6 +114,8 @@
   // never reset.
   NetEqLifetimeStatistics GetLifetimeStatistics() const;
 
+  NetEqOperationsAndState GetOperationsAndState() const;
+
  private:
   static const int kMaxReportPeriod = 60;  // Seconds before auto-reset.
   static const size_t kLenWaitingTimes = 100;
@@ -178,6 +183,7 @@
   static uint16_t CalculateQ14Ratio(size_t numerator, uint32_t denominator);
 
   NetEqLifetimeStatistics lifetime_stats_;
+  NetEqOperationsAndState operations_and_state_;
   size_t concealed_samples_correction_ = 0;
   size_t voice_concealed_samples_correction_ = 0;
   size_t preemptive_samples_;
@@ -193,6 +199,7 @@
   size_t discarded_secondary_packets_;
   PeriodicUmaCount delayed_packet_outage_counter_;
   PeriodicUmaAverage excess_buffer_delay_;
+  PeriodicUmaCount buffer_full_counter_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator);
 };
diff --git a/modules/audio_coding/neteq/sync_buffer.cc b/modules/audio_coding/neteq/sync_buffer.cc
index 82ca16f..fee18cc 100644
--- a/modules/audio_coding/neteq/sync_buffer.cc
+++ b/modules/audio_coding/neteq/sync_buffer.cc
@@ -36,6 +36,16 @@
   dtmf_index_ -= std::min(dtmf_index_, samples_added);
 }
 
+void SyncBuffer::PushBackInterleaved(const rtc::BufferT<int16_t>& append_this) {
+  const size_t size_before_adding = Size();
+  AudioMultiVector::PushBackInterleaved(append_this);
+  const size_t samples_added_per_channel = Size() - size_before_adding;
+  RTC_DCHECK_EQ(samples_added_per_channel * Channels(), append_this.size());
+  AudioMultiVector::PopFront(samples_added_per_channel);
+  next_index_ -= std::min(next_index_, samples_added_per_channel);
+  dtmf_index_ -= std::min(dtmf_index_, samples_added_per_channel);
+}
+
 void SyncBuffer::PushFrontZeros(size_t length) {
   InsertZerosAtIndex(length, 0);
 }
diff --git a/modules/audio_coding/neteq/sync_buffer.h b/modules/audio_coding/neteq/sync_buffer.h
index 3833cb2..72e320c 100644
--- a/modules/audio_coding/neteq/sync_buffer.h
+++ b/modules/audio_coding/neteq/sync_buffer.h
@@ -13,6 +13,7 @@
 
 #include "api/audio/audio_frame.h"
 #include "modules/audio_coding/neteq/audio_multi_vector.h"
+#include "rtc_base/buffer.h"
 #include "rtc_base/constructormagic.h"
 
 namespace webrtc {
@@ -34,6 +35,9 @@
   // the move of the beginning of "future" data.
   void PushBack(const AudioMultiVector& append_this) override;
 
+  // Like PushBack, but reads the samples channel-interleaved from the input.
+  void PushBackInterleaved(const rtc::BufferT<int16_t>& append_this);
+
   // Adds |length| zeros to the beginning of each channel. Removes
   // the same number of samples from the end of the SyncBuffer, to
   // maintain a constant buffer size. The |next_index_| is updated to reflect
diff --git a/modules/audio_coding/neteq/tools/audio_loop.cc b/modules/audio_coding/neteq/tools/audio_loop.cc
index 972921b..0045f06 100644
--- a/modules/audio_coding/neteq/tools/audio_loop.cc
+++ b/modules/audio_coding/neteq/tools/audio_loop.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/neteq/tools/audio_loop.h"
 
-#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
diff --git a/modules/audio_coding/neteq/tools/encode_neteq_input.cc b/modules/audio_coding/neteq/tools/encode_neteq_input.cc
index c576670..87b987d 100644
--- a/modules/audio_coding/neteq/tools/encode_neteq_input.cc
+++ b/modules/audio_coding/neteq/tools/encode_neteq_input.cc
@@ -53,7 +53,7 @@
 }
 
 bool EncodeNetEqInput::ended() const {
-  return next_output_event_ms_ <= input_duration_ms_;
+  return next_output_event_ms_ > input_duration_ms_;
 }
 
 absl::optional<RTPHeader> EncodeNetEqInput::NextHeader() const {
diff --git a/modules/audio_coding/neteq/tools/neteq_delay_analyzer.cc b/modules/audio_coding/neteq/tools/neteq_delay_analyzer.cc
index e5bd765..60e6902 100644
--- a/modules/audio_coding/neteq/tools/neteq_delay_analyzer.cc
+++ b/modules/audio_coding/neteq/tools/neteq_delay_analyzer.cc
@@ -17,18 +17,19 @@
 #include <limits>
 #include <utility>
 
+#include "absl/strings/string_view.h"
 #include "modules/include/module_common_types.h"
 #include "rtc_base/checks.h"
 
 namespace webrtc {
 namespace test {
 namespace {
-std::string kArrivalDelayX = "arrival_delay_x";
-std::string kArrivalDelayY = "arrival_delay_y";
-std::string kTargetDelayX = "target_delay_x";
-std::string kTargetDelayY = "target_delay_y";
-std::string kPlayoutDelayX = "playout_delay_x";
-std::string kPlayoutDelayY = "playout_delay_y";
+constexpr char kArrivalDelayX[] = "arrival_delay_x";
+constexpr char kArrivalDelayY[] = "arrival_delay_y";
+constexpr char kTargetDelayX[] = "target_delay_x";
+constexpr char kTargetDelayY[] = "target_delay_y";
+constexpr char kPlayoutDelayX[] = "playout_delay_x";
+constexpr char kPlayoutDelayY[] = "playout_delay_y";
 
 // Helper function for NetEqDelayAnalyzer::CreateGraphs. Returns the
 // interpolated value of a function at the point x. Vector x_vec contains the
@@ -64,8 +65,8 @@
 
 void PrintDelays(const NetEqDelayAnalyzer::Delays& delays,
                  int64_t ref_time_ms,
-                 const std::string& var_name_x,
-                 const std::string& var_name_y,
+                 absl::string_view var_name_x,
+                 absl::string_view var_name_y,
                  std::ofstream& output,
                  const std::string& terminator = "") {
   output << var_name_x << " = [ ";
diff --git a/modules/audio_coding/neteq/tools/neteq_input.cc b/modules/audio_coding/neteq/tools/neteq_input.cc
index bb2a01e..645894d 100644
--- a/modules/audio_coding/neteq/tools/neteq_input.cc
+++ b/modules/audio_coding/neteq/tools/neteq_input.cc
@@ -10,7 +10,7 @@
 
 #include "modules/audio_coding/neteq/tools/neteq_input.h"
 
-#include <sstream>
+#include "rtc_base/strings/string_builder.h"
 
 namespace webrtc {
 namespace test {
@@ -19,7 +19,7 @@
 NetEqInput::PacketData::~PacketData() = default;
 
 std::string NetEqInput::PacketData::ToString() const {
-  std::stringstream ss;
+  rtc::StringBuilder ss;
   ss << "{"
      << "time_ms: " << static_cast<int64_t>(time_ms) << ", "
      << "header: {"
@@ -28,7 +28,7 @@
      << "ts: " << header.timestamp << ", "
      << "ssrc: " << header.ssrc << "}, "
      << "payload bytes: " << payload.size() << "}";
-  return ss.str();
+  return ss.Release();
 }
 
 TimeLimitedNetEqInput::TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input,
diff --git a/modules/audio_coding/neteq/tools/neteq_replacement_input.cc b/modules/audio_coding/neteq/tools/neteq_replacement_input.cc
index 362eb89..ffd114a 100644
--- a/modules/audio_coding/neteq/tools/neteq_replacement_input.cc
+++ b/modules/audio_coding/neteq/tools/neteq_replacement_input.cc
@@ -42,7 +42,15 @@
 
 std::unique_ptr<NetEqInput::PacketData> NetEqReplacementInput::PopPacket() {
   std::unique_ptr<PacketData> to_return = std::move(packet_);
-  packet_ = source_->PopPacket();
+  while (true) {
+    packet_ = source_->PopPacket();
+    if (!packet_)
+      break;
+    if (packet_->payload.size() > packet_->header.paddingLength) {
+      // Not padding only. Good to go. Skip this packet otherwise.
+      break;
+    }
+  }
   ReplacePacket();
   return to_return;
 }
diff --git a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
index bf782bf..25e8cd8 100644
--- a/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
+++ b/modules/audio_coding/neteq/tools/neteq_rtpplay.cc
@@ -8,267 +8,29 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include <errno.h>
-#include <inttypes.h>
-#include <limits.h>  // For ULONG_MAX returned by strtoul.
-#include <stdio.h>
-#include <stdlib.h>  // For strtoul.
 #include <iostream>
-#include <memory>
 #include <string>
 
-#include "modules/audio_coding/neteq/include/neteq.h"
-#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
-#include "modules/audio_coding/neteq/tools/input_audio_file.h"
-#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
-#include "modules/audio_coding/neteq/tools/neteq_event_log_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
-#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
 #include "modules/audio_coding/neteq/tools/neteq_test.h"
-#include "modules/audio_coding/neteq/tools/output_audio_file.h"
-#include "modules/audio_coding/neteq/tools/output_wav_file.h"
-#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
-#include "rtc_base/checks.h"
+#include "modules/audio_coding/neteq/tools/neteq_test_factory.h"
 #include "rtc_base/flags.h"
+#include "system_wrappers/include/field_trial.h"
 #include "test/field_trial.h"
-#include "test/testsupport/fileutils.h"
 
-namespace webrtc {
-namespace test {
-namespace {
-
-// Parses the input string for a valid SSRC (at the start of the string). If a
-// valid SSRC is found, it is written to the output variable |ssrc|, and true is
-// returned. Otherwise, false is returned.
-bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
-  if (str.empty())
-    return true;
-  int base = 10;
-  // Look for "0x" or "0X" at the start and change base to 16 if found.
-  if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
-    base = 16;
-  errno = 0;
-  char* end_ptr;
-  unsigned long value = strtoul(str.c_str(), &end_ptr, base);
-  if (value == ULONG_MAX && errno == ERANGE)
-    return false;  // Value out of range for unsigned long.
-  if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)
-    return false;  // Value out of range for uint32_t.
-  if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
-    return false;  // Part of the string was not parsed.
-  *ssrc = static_cast<uint32_t>(value);
-  return true;
-}
-
-// Flag validators.
-bool ValidatePayloadType(int value) {
-  if (value >= 0 && value <= 127)  // Value is ok.
-    return true;
-  printf("Payload type must be between 0 and 127, not %d\n",
-         static_cast<int>(value));
-  return false;
-}
-
-bool ValidateSsrcValue(const std::string& str) {
-  uint32_t dummy_ssrc;
-  if (ParseSsrc(str, &dummy_ssrc))  // Value is ok.
-    return true;
-  printf("Invalid SSRC: %s\n", str.c_str());
-  return false;
-}
-
-static bool ValidateExtensionId(int value) {
-  if (value > 0 && value <= 255)  // Value is ok.
-    return true;
-  printf("Extension ID must be between 1 and 255, not %d\n",
-         static_cast<int>(value));
-  return false;
-}
-
-// Define command line flags.
-DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
-DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
-DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
-DEFINE_int(isac, 103, "RTP payload type for iSAC");
-DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
-DEFINE_int(opus, 111, "RTP payload type for Opus");
-DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
-DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
-DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
-DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
-DEFINE_int(g722, 9, "RTP payload type for G.722");
-DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
-DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
-DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
-DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
-DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
-DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
-DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
-DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
-DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
 DEFINE_bool(codec_map,
             false,
             "Prints the mapping between RTP payload type and "
             "codec");
-DEFINE_string(replacement_audio_file,
-              "",
-              "A PCM file that will be used to populate "
-              "dummy"
-              " RTP packets");
-DEFINE_string(ssrc,
-              "",
-              "Only use packets with this SSRC (decimal or hex, the latter "
-              "starting with 0x)");
-DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
-DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
-DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
-DEFINE_int(video_content_type, 7, "Extension ID for video content type");
-DEFINE_int(video_timing, 8, "Extension ID for video timing");
-DEFINE_bool(matlabplot,
-            false,
-            "Generates a matlab script for plotting the delay profile");
-DEFINE_bool(pythonplot,
-            false,
-            "Generates a python script for plotting the delay profile");
-DEFINE_bool(help, false, "Prints this message");
-DEFINE_bool(concealment_events, false, "Prints concealment events");
 DEFINE_string(
     force_fieldtrials,
     "",
     "Field trials control experimental feature code which can be forced. "
     "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
     " will assign the group Enable to field trial WebRTC-FooFeature.");
+DEFINE_bool(help, false, "Prints this message");
 
-// Maps a codec type to a printable name string.
-std::string CodecName(NetEqDecoder codec) {
-  switch (codec) {
-    case NetEqDecoder::kDecoderPCMu:
-      return "PCM-u";
-    case NetEqDecoder::kDecoderPCMa:
-      return "PCM-a";
-    case NetEqDecoder::kDecoderILBC:
-      return "iLBC";
-    case NetEqDecoder::kDecoderISAC:
-      return "iSAC";
-    case NetEqDecoder::kDecoderISACswb:
-      return "iSAC-swb (32 kHz)";
-    case NetEqDecoder::kDecoderOpus:
-      return "Opus";
-    case NetEqDecoder::kDecoderPCM16B:
-      return "PCM16b-nb (8 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bwb:
-      return "PCM16b-wb (16 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bswb32kHz:
-      return "PCM16b-swb32 (32 kHz)";
-    case NetEqDecoder::kDecoderPCM16Bswb48kHz:
-      return "PCM16b-swb48 (48 kHz)";
-    case NetEqDecoder::kDecoderG722:
-      return "G.722";
-    case NetEqDecoder::kDecoderRED:
-      return "redundant audio (RED)";
-    case NetEqDecoder::kDecoderAVT:
-      return "AVT/DTMF (8 kHz)";
-    case NetEqDecoder::kDecoderAVT16kHz:
-      return "AVT/DTMF (16 kHz)";
-    case NetEqDecoder::kDecoderAVT32kHz:
-      return "AVT/DTMF (32 kHz)";
-    case NetEqDecoder::kDecoderAVT48kHz:
-      return "AVT/DTMF (48 kHz)";
-    case NetEqDecoder::kDecoderCNGnb:
-      return "comfort noise (8 kHz)";
-    case NetEqDecoder::kDecoderCNGwb:
-      return "comfort noise (16 kHz)";
-    case NetEqDecoder::kDecoderCNGswb32kHz:
-      return "comfort noise (32 kHz)";
-    case NetEqDecoder::kDecoderCNGswb48kHz:
-      return "comfort noise (48 kHz)";
-    default:
-      FATAL();
-      return "undefined";
-  }
-}
-
-void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
-  std::cout << CodecName(codec) << ": " << flag << std::endl;
-}
-
-void PrintCodecMapping() {
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
-                         FLAG_pcm16b_swb32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
-                         FLAG_pcm16b_swb48);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
-  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
-}
-
-absl::optional<int> CodecSampleRate(uint8_t payload_type) {
-  if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
-      payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
-      payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
-    return 8000;
-  if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
-      payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
-      payload_type == FLAG_avt_16)
-    return 16000;
-  if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
-      payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
-    return 32000;
-  if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
-      payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
-    return 48000;
-  if (payload_type == FLAG_red)
-    return 0;
-  return absl::nullopt;
-}
-
-// A callback class which prints whenver the inserted packet stream changes
-// the SSRC.
-class SsrcSwitchDetector : public NetEqPostInsertPacket {
- public:
-  // Takes a pointer to another callback object, which will be invoked after
-  // this object finishes. This does not transfer ownership, and null is a
-  // valid value.
-  explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
-      : other_callback_(other_callback) {}
-
-  void AfterInsertPacket(const NetEqInput::PacketData& packet,
-                         NetEq* neteq) override {
-    if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
-      std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
-                << " to 0x" << std::hex << packet.header.ssrc << std::dec
-                << " (payload type "
-                << static_cast<int>(packet.header.payloadType) << ")"
-                << std::endl;
-    }
-    last_ssrc_ = packet.header.ssrc;
-    if (other_callback_) {
-      other_callback_->AfterInsertPacket(packet, neteq);
-    }
-  }
-
- private:
-  NetEqPostInsertPacket* other_callback_;
-  absl::optional<uint32_t> last_ssrc_;
-};
-
-int RunTest(int argc, char* argv[]) {
+int main(int argc, char* argv[]) {
+  webrtc::test::NetEqTestFactory factory;
   std::string program_name = argv[0];
   std::string usage =
       "Tool for decoding an RTP dump file using NetEq.\n"
@@ -278,274 +40,29 @@
       "Example usage:\n" +
       program_name + " input.rtp output.{pcm, wav}\n";
   if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
-    return 1;
+    exit(1);
   }
   if (FLAG_help) {
     std::cout << usage;
     rtc::FlagList::Print(nullptr, false);
-    return 0;
+    exit(0);
   }
-
   if (FLAG_codec_map) {
-    PrintCodecMapping();
+    factory.PrintCodecMap();
   }
-
   if (argc != 3) {
     if (FLAG_codec_map) {
       // We have already printed the codec map. Just end the program.
-      return 0;
+      exit(0);
     }
     // Print usage information.
     std::cout << usage;
-    return 0;
+    exit(0);
   }
-
-  ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
-  ScopedFieldTrials field_trials(FLAG_force_fieldtrials);
-
-  RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcma));
-  RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
-  RTC_CHECK(ValidatePayloadType(FLAG_isac));
-  RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
-  RTC_CHECK(ValidatePayloadType(FLAG_opus));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
-  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
-  RTC_CHECK(ValidatePayloadType(FLAG_g722));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
-  RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
-  RTC_CHECK(ValidatePayloadType(FLAG_red));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
-  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
-  RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
-  RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
-  RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
-  RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
-  RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
-  RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
-
-  // Gather RTP header extensions in a map.
-  NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
-      {FLAG_audio_level, kRtpExtensionAudioLevel},
-      {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
-      {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
-      {FLAG_video_content_type, kRtpExtensionVideoContentType},
-      {FLAG_video_timing, kRtpExtensionVideoTiming}};
-
-  const std::string input_file_name = argv[1];
-  std::unique_ptr<NetEqInput> input;
-  if (RtpFileSource::ValidRtpDump(input_file_name) ||
-      RtpFileSource::ValidPcap(input_file_name)) {
-    input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
-  } else {
-    input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
-  }
-
-  std::cout << "Input file: " << input_file_name << std::endl;
-  RTC_CHECK(input) << "Cannot open input file";
-  RTC_CHECK(!input->ended()) << "Input file is empty";
-
-  // Check if an SSRC value was provided.
-  if (strlen(FLAG_ssrc) > 0) {
-    uint32_t ssrc;
-    RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
-    static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
-  }
-
-  // Check the sample rate.
-  absl::optional<int> sample_rate_hz;
-  std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
-  while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
-    RTC_DCHECK(first_rtp_header);
-    sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
-    if (sample_rate_hz) {
-      std::cout << "Found valid packet with payload type "
-                << static_cast<int>(first_rtp_header->payloadType)
-                << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
-                << std::dec << std::endl;
-      break;
-    }
-    // Discard this packet and move to the next. Keep track of discarded payload
-    // types and SSRCs.
-    discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
-                                  first_rtp_header->ssrc);
-    input->PopPacket();
-  }
-  if (!discarded_pt_and_ssrc.empty()) {
-    std::cout << "Discarded initial packets with the following payload types "
-                 "and SSRCs:"
-              << std::endl;
-    for (const auto& d : discarded_pt_and_ssrc) {
-      std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
-                << static_cast<int>(d.second) << std::dec << std::endl;
-    }
-  }
-  if (!sample_rate_hz) {
-    std::cout << "Cannot find any packets with known payload types"
-              << std::endl;
-    RTC_NOTREACHED();
-  }
-
-  // Open the output file now that we know the sample rate. (Rate is only needed
-  // for wav files.)
-  const std::string output_file_name = argv[2];
-  std::unique_ptr<AudioSink> output;
-  if (output_file_name.size() >= 4 &&
-      output_file_name.substr(output_file_name.size() - 4) == ".wav") {
-    // Open a wav file.
-    output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
-  } else {
-    // Open a pcm file.
-    output.reset(new OutputAudioFile(output_file_name));
-  }
-
-  std::cout << "Output file: " << output_file_name << std::endl;
-
-  NetEqTest::DecoderMap codecs = {
-    {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
-    {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
-#ifdef WEBRTC_CODEC_ILBC
-    {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
-#endif
-    {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
-#if !defined(WEBRTC_ANDROID)
-    {FLAG_isac_swb, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
-#endif
-#ifdef WEBRTC_CODEC_OPUS
-    {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
-#endif
-    {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
-    {FLAG_pcm16b_wb,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
-    {FLAG_pcm16b_swb32,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
-    {FLAG_pcm16b_swb48,
-     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
-    {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
-    {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
-    {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
-    {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
-    {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
-    {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
-    {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
-    {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
-    {FLAG_cn_swb32,
-     std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
-    {FLAG_cn_swb48,
-     std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}
-  };
-
-  // Check if a replacement audio file was provided.
-  std::unique_ptr<AudioDecoder> replacement_decoder;
-  NetEqTest::ExtDecoderMap ext_codecs;
-  if (strlen(FLAG_replacement_audio_file) > 0) {
-    // Find largest unused payload type.
-    int replacement_pt = 127;
-    while (!(codecs.find(replacement_pt) == codecs.end() &&
-             ext_codecs.find(replacement_pt) == ext_codecs.end())) {
-      --replacement_pt;
-      RTC_CHECK_GE(replacement_pt, 0);
-    }
-
-    auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
-      std::set<uint8_t> b;
-      for (auto& x : a) {
-        b.insert(static_cast<uint8_t>(x));
-      }
-      return b;
-    };
-
-    std::set<uint8_t> cn_types = std_set_int32_to_uint8(
-        {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
-    std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
-        {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
-    input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
-                                          cn_types, forbidden_types));
-
-    replacement_decoder.reset(new FakeDecodeFromFile(
-        std::unique_ptr<InputAudioFile>(
-            new InputAudioFile(FLAG_replacement_audio_file)),
-        48000, false));
-    NetEqTest::ExternalDecoderInfo ext_dec_info = {
-        replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
-        "replacement codec"};
-    ext_codecs[replacement_pt] = ext_dec_info;
-  }
-
-  NetEqTest::Callbacks callbacks;
-  std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
-  if (FLAG_matlabplot || FLAG_pythonplot) {
-    delay_analyzer.reset(new NetEqDelayAnalyzer);
-  }
-
-  SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
-  callbacks.post_insert_packet = &ssrc_switch_detector;
-  NetEqStatsGetter stats_getter(std::move(delay_analyzer));
-  callbacks.get_audio_callback = &stats_getter;
-  NetEq::Config config;
-  config.sample_rate_hz = *sample_rate_hz;
-  NetEqTest test(config, codecs, ext_codecs, std::move(input),
-                 std::move(output), callbacks);
-
-  int64_t test_duration_ms = test.Run();
-
-  if (FLAG_matlabplot) {
-    auto matlab_script_name = output_file_name;
-    std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
-                 '_');
-    std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
-              << std::endl;
-    stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
-                                                      ".m");
-  }
-  if (FLAG_pythonplot) {
-    auto python_script_name = output_file_name;
-    std::replace(python_script_name.begin(), python_script_name.end(), '.',
-                 '_');
-    std::cout << "Creating Python plot script " << python_script_name + ".py"
-              << std::endl;
-    stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
-                                                      ".py");
-  }
-
-  printf("Simulation statistics:\n");
-  printf("  output duration: %" PRId64 " ms\n", test_duration_ms);
-  auto stats = stats_getter.AverageStats();
-  printf("  packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
-  printf("  expand_rate: %f %%\n", 100.0 * stats.expand_rate);
-  printf("  speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
-  printf("  preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
-  printf("  accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
-  printf("  secondary_decoded_rate: %f %%\n",
-         100.0 * stats.secondary_decoded_rate);
-  printf("  secondary_discarded_rate: %f %%\n",
-         100.0 * stats.secondary_discarded_rate);
-  printf("  clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
-  printf("  mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
-  printf("  median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
-  printf("  min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
-  printf("  max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
-  printf("  current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
-  printf("  preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
-  if (FLAG_concealment_events) {
-    std::cout << " concealment_events_ms:" << std::endl;
-    for (auto concealment_event : stats_getter.concealment_events())
-      std::cout << concealment_event.ToString() << std::endl;
-    std::cout << " end of concealment_events_ms" << std::endl;
-  }
+  webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
+  webrtc::field_trial::InitFieldTrialsFromString(FLAG_force_fieldtrials);
+  std::unique_ptr<webrtc::test::NetEqTest> test =
+      factory.InitializeTest(argv[1], argv[2]);
+  test->Run();
   return 0;
 }
-
-}  // namespace
-}  // namespace test
-}  // namespace webrtc
-
-int main(int argc, char* argv[]) {
-  return webrtc::test::RunTest(argc, argv);
-}
diff --git a/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc b/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc
new file mode 100644
index 0000000..3f86cfd
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_stats_plotter.cc
@@ -0,0 +1,81 @@
+/*
+ *  Copyright (c) 2018 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 "modules/audio_coding/neteq/tools/neteq_stats_plotter.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <utility>
+
+namespace webrtc {
+namespace test {
+
+NetEqStatsPlotter::NetEqStatsPlotter(bool make_matlab_plot,
+                                     bool make_python_plot,
+                                     bool show_concealment_events,
+                                     std::string base_file_name)
+    : make_matlab_plot_(make_matlab_plot),
+      make_python_plot_(make_python_plot),
+      show_concealment_events_(show_concealment_events),
+      base_file_name_(base_file_name) {
+  std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
+  if (make_matlab_plot || make_python_plot) {
+    delay_analyzer.reset(new NetEqDelayAnalyzer);
+  }
+  stats_getter_.reset(new NetEqStatsGetter(std::move(delay_analyzer)));
+}
+
+void NetEqStatsPlotter::SimulationEnded(int64_t simulation_time_ms) {
+  if (make_matlab_plot_) {
+    auto matlab_script_name = base_file_name_;
+    std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
+                 '_');
+    printf("Creating Matlab plot script %s.m\n", matlab_script_name.c_str());
+    stats_getter_->delay_analyzer()->CreateMatlabScript(matlab_script_name +
+                                                        ".m");
+  }
+  if (make_python_plot_) {
+    auto python_script_name = base_file_name_;
+    std::replace(python_script_name.begin(), python_script_name.end(), '.',
+                 '_');
+    printf("Creating Python plot script %s.py\n", python_script_name.c_str());
+    stats_getter_->delay_analyzer()->CreatePythonScript(python_script_name +
+                                                        ".py");
+  }
+
+  printf("Simulation statistics:\n");
+  printf("  output duration: %" PRId64 " ms\n", simulation_time_ms);
+  auto stats = stats_getter_->AverageStats();
+  printf("  packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
+  printf("  expand_rate: %f %%\n", 100.0 * stats.expand_rate);
+  printf("  speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
+  printf("  preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
+  printf("  accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
+  printf("  secondary_decoded_rate: %f %%\n",
+         100.0 * stats.secondary_decoded_rate);
+  printf("  secondary_discarded_rate: %f %%\n",
+         100.0 * stats.secondary_discarded_rate);
+  printf("  clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
+  printf("  mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
+  printf("  median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
+  printf("  min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
+  printf("  max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
+  printf("  current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
+  printf("  preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
+  if (show_concealment_events_) {
+    printf(" concealment_events_ms:\n");
+    for (auto concealment_event : stats_getter_->concealment_events())
+      printf("%s\n", concealment_event.ToString().c_str());
+    printf(" end of concealment_events_ms\n");
+  }
+}
+
+}  // namespace test
+}  // namespace webrtc
diff --git a/modules/audio_coding/neteq/tools/neteq_stats_plotter.h b/modules/audio_coding/neteq/tools/neteq_stats_plotter.h
new file mode 100644
index 0000000..c4df24e
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_stats_plotter.h
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
+#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
+
+#include <memory>
+#include <string>
+
+#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+
+namespace webrtc {
+namespace test {
+
+class NetEqStatsPlotter : public NetEqSimulationEndedCallback {
+ public:
+  NetEqStatsPlotter(bool make_matlab_plot,
+                    bool make_python_plot,
+                    bool show_concealment_events,
+                    std::string base_file_name);
+
+  void SimulationEnded(int64_t simulation_time_ms) override;
+
+  NetEqStatsGetter* stats_getter() { return stats_getter_.get(); }
+
+ private:
+  std::unique_ptr<NetEqStatsGetter> stats_getter_;
+  const bool make_matlab_plot_;
+  const bool make_python_plot_;
+  const bool show_concealment_events_;
+  const std::string base_file_name_;
+};
+
+}  // namespace test
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_PLOTTER_H_
diff --git a/modules/audio_coding/neteq/tools/neteq_test.cc b/modules/audio_coding/neteq/tools/neteq_test.cc
index 3448f4e..421f380 100644
--- a/modules/audio_coding/neteq/tools/neteq_test.cc
+++ b/modules/audio_coding/neteq/tools/neteq_test.cc
@@ -16,6 +16,26 @@
 
 namespace webrtc {
 namespace test {
+namespace {
+
+absl::optional<Operations> ActionToOperations(
+    absl::optional<NetEqSimulator::Action> a) {
+  if (!a) {
+    return absl::nullopt;
+  }
+  switch (*a) {
+    case NetEqSimulator::Action::kAccelerate:
+      return absl::make_optional(kAccelerate);
+    case NetEqSimulator::Action::kExpand:
+      return absl::make_optional(kExpand);
+    case NetEqSimulator::Action::kNormal:
+      return absl::make_optional(kNormal);
+    case NetEqSimulator::Action::kPreemptiveExpand:
+      return absl::make_optional(kPreemptiveExpand);
+  }
+}
+
+}  // namespace
 
 void DefaultNetEqTestErrorCallback::OnInsertPacketError(
     const NetEqInput::PacketData& packet) {
@@ -49,8 +69,23 @@
 NetEqTest::~NetEqTest() = default;
 
 int64_t NetEqTest::Run() {
+  int64_t simulation_time = 0;
+  SimulationStepResult step_result;
+  do {
+    step_result = RunToNextGetAudio();
+    simulation_time += step_result.simulation_step_ms;
+  } while (!step_result.is_simulation_finished);
+  if (callbacks_.simulation_ended_callback) {
+    callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
+  }
+  return simulation_time;
+}
+
+NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() {
+  SimulationStepResult result;
   const int64_t start_time_ms = *input_->NextEventTime();
   int64_t time_now_ms = start_time_ms;
+  current_state_.packet_iat_ms.clear();
 
   while (!input_->ended()) {
     // Advance time to next event.
@@ -60,17 +95,29 @@
     if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) {
       std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket();
       RTC_CHECK(packet_data);
-      int error = neteq_->InsertPacket(
-          packet_data->header,
-          rtc::ArrayView<const uint8_t>(packet_data->payload),
-          static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ / 1000));
-      if (error != NetEq::kOK && callbacks_.error_callback) {
-        callbacks_.error_callback->OnInsertPacketError(*packet_data);
+      const size_t payload_data_length =
+          packet_data->payload.size() - packet_data->header.paddingLength;
+      if (payload_data_length != 0) {
+        int error = neteq_->InsertPacket(
+            packet_data->header,
+            rtc::ArrayView<const uint8_t>(packet_data->payload),
+            static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ /
+                                  1000));
+        if (error != NetEq::kOK && callbacks_.error_callback) {
+          callbacks_.error_callback->OnInsertPacketError(*packet_data);
+        }
+        if (callbacks_.post_insert_packet) {
+          callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
+                                                           neteq_.get());
+        }
+      } else {
+        neteq_->InsertEmptyPacket(packet_data->header);
       }
-      if (callbacks_.post_insert_packet) {
-        callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
-                                                         neteq_.get());
+      if (last_packet_time_ms_) {
+        current_state_.packet_iat_ms.push_back(time_now_ms -
+                                               *last_packet_time_ms_);
       }
+      last_packet_time_ms_ = absl::make_optional<int>(time_now_ms);
     }
 
     // Check if it is time to get output audio.
@@ -81,7 +128,9 @@
       }
       AudioFrame out_frame;
       bool muted;
-      int error = neteq_->GetAudio(&out_frame, &muted);
+      int error = neteq_->GetAudio(&out_frame, &muted,
+                                   ActionToOperations(next_action_));
+      next_action_ = absl::nullopt;
       RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
       if (error != NetEq::kOK) {
         if (callbacks_.error_callback) {
@@ -102,9 +151,58 @@
       }
 
       input_->AdvanceOutputEvent();
+      result.simulation_step_ms =
+          input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
+      const auto operations_state = neteq_->GetOperationsAndState();
+      current_state_.current_delay_ms = operations_state.current_buffer_size_ms;
+      current_state_.packet_size_ms = operations_state.current_frame_size_ms;
+      current_state_.next_packet_available =
+          operations_state.next_packet_available;
+      current_state_.packet_buffer_flushed =
+          operations_state.packet_buffer_flushes >
+          prev_ops_state_.packet_buffer_flushes;
+      // TODO(ivoc): Add more accurate reporting by tracking the origin of
+      // samples in the sync buffer.
+      result.action_times_ms[Action::kExpand] = 0;
+      result.action_times_ms[Action::kAccelerate] = 0;
+      result.action_times_ms[Action::kPreemptiveExpand] = 0;
+      result.action_times_ms[Action::kNormal] = 0;
+
+      if (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC ||
+          out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG) {
+        // Consider the whole frame to be the result of expansion.
+        result.action_times_ms[Action::kExpand] = 10;
+      } else if (operations_state.accelerate_samples -
+                     prev_ops_state_.accelerate_samples >
+                 0) {
+        // Consider the whole frame to be the result of acceleration.
+        result.action_times_ms[Action::kAccelerate] = 10;
+      } else if (operations_state.preemptive_samples -
+                     prev_ops_state_.preemptive_samples >
+                 0) {
+        // Consider the whole frame to be the result of preemptive expansion.
+        result.action_times_ms[Action::kPreemptiveExpand] = 10;
+      } else {
+        // Consider the whole frame to be the result of normal playout.
+        result.action_times_ms[Action::kNormal] = 10;
+      }
+      result.is_simulation_finished = input_->ended();
+      prev_ops_state_ = operations_state;
+      return result;
     }
   }
-  return time_now_ms - start_time_ms;
+  result.simulation_step_ms =
+      input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
+  result.is_simulation_finished = true;
+  return result;
+}
+
+void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
+  next_action_ = absl::optional<Action>(next_operation);
+}
+
+NetEqTest::NetEqState NetEqTest::GetNetEqState() {
+  return current_state_;
 }
 
 NetEqNetworkStatistics NetEqTest::SimulationStats() {
diff --git a/modules/audio_coding/neteq/tools/neteq_test.h b/modules/audio_coding/neteq/tools/neteq_test.h
index ada6bab..23d7c22 100644
--- a/modules/audio_coding/neteq/tools/neteq_test.h
+++ b/modules/audio_coding/neteq/tools/neteq_test.h
@@ -16,6 +16,8 @@
 #include <string>
 #include <utility>
 
+#include "absl/types/optional.h"
+#include "api/test/neteq_simulator.h"
 #include "modules/audio_coding/neteq/include/neteq.h"
 #include "modules/audio_coding/neteq/tools/audio_sink.h"
 #include "modules/audio_coding/neteq/tools/neteq_input.h"
@@ -52,10 +54,16 @@
                              NetEq* neteq) = 0;
 };
 
+class NetEqSimulationEndedCallback {
+ public:
+  virtual ~NetEqSimulationEndedCallback() = default;
+  virtual void SimulationEnded(int64_t simulation_time_ms) = 0;
+};
+
 // Class that provides an input--output test for NetEq. The input (both packets
 // and output events) is provided by a NetEqInput object, while the output is
 // directed to an AudioSink object.
-class NetEqTest {
+class NetEqTest : public NetEqSimulator {
  public:
   using DecoderMap = std::map<int, std::pair<NetEqDecoder, std::string> >;
 
@@ -71,6 +79,7 @@
     NetEqTestErrorCallback* error_callback = nullptr;
     NetEqPostInsertPacket* post_insert_packet = nullptr;
     NetEqGetAudioCallback* get_audio_callback = nullptr;
+    NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
   };
 
   // Sets up the test with given configuration, codec mappings, input, ouput,
@@ -82,10 +91,17 @@
             std::unique_ptr<AudioSink> output,
             Callbacks callbacks);
 
-  ~NetEqTest();
+  ~NetEqTest() override;
 
   // Runs the test. Returns the duration of the produced audio in ms.
   int64_t Run();
+  // Runs the simulation until we hit the next GetAudio event. If the simulation
+  // is finished, is_simulation_finished will be set to true in the returned
+  // SimulationStepResult.
+  SimulationStepResult RunToNextGetAudio() override;
+
+  void SetNextAction(Action next_operation) override;
+  NetEqState GetNetEqState() override;
 
   // Returns the statistics from NetEq.
   NetEqNetworkStatistics SimulationStats();
@@ -96,12 +112,15 @@
  private:
   void RegisterDecoders(const DecoderMap& codecs);
   void RegisterExternalDecoders(const ExtDecoderMap& codecs);
-
+  absl::optional<Action> next_action_;
+  absl::optional<int> last_packet_time_ms_;
   std::unique_ptr<NetEq> neteq_;
   std::unique_ptr<NetEqInput> input_;
   std::unique_ptr<AudioSink> output_;
   Callbacks callbacks_;
   int sample_rate_hz_;
+  NetEqState current_state_;
+  NetEqOperationsAndState prev_ops_state_;
 };
 
 }  // namespace test
diff --git a/modules/audio_coding/neteq/tools/neteq_test_factory.cc b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
new file mode 100644
index 0000000..51077bc
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_test_factory.cc
@@ -0,0 +1,466 @@
+/*
+ *  Copyright (c) 2018 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 "modules/audio_coding/neteq/tools/neteq_test_factory.h"
+
+#include <errno.h>
+#include <limits.h>  // For ULONG_MAX returned by strtoul.
+#include <stdio.h>
+#include <stdlib.h>  // For strtoul.
+#include <iostream>
+#include <memory>
+#include <set>
+#include <string>
+#include <utility>
+
+#include "absl/memory/memory.h"
+#include "modules/audio_coding/neteq/include/neteq.h"
+#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
+#include "modules/audio_coding/neteq/tools/input_audio_file.h"
+#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
+#include "modules/audio_coding/neteq/tools/neteq_event_log_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
+#include "modules/audio_coding/neteq/tools/neteq_stats_plotter.h"
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+#include "modules/audio_coding/neteq/tools/output_audio_file.h"
+#include "modules/audio_coding/neteq/tools/output_wav_file.h"
+#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/flags.h"
+#include "test/testsupport/fileutils.h"
+
+namespace webrtc {
+namespace test {
+namespace {
+
+// Parses the input string for a valid SSRC (at the start of the string). If a
+// valid SSRC is found, it is written to the output variable |ssrc|, and true is
+// returned. Otherwise, false is returned.
+bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
+  if (str.empty())
+    return true;
+  int base = 10;
+  // Look for "0x" or "0X" at the start and change base to 16 if found.
+  if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
+    base = 16;
+  errno = 0;
+  char* end_ptr;
+  unsigned long value = strtoul(str.c_str(), &end_ptr, base);  // NOLINT
+  if (value == ULONG_MAX && errno == ERANGE)
+    return false;  // Value out of range for unsigned long.
+  if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)  // NOLINT
+    return false;  // Value out of range for uint32_t.
+  if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
+    return false;  // Part of the string was not parsed.
+  *ssrc = static_cast<uint32_t>(value);
+  return true;
+}
+
+// Flag validators.
+bool ValidatePayloadType(int value) {
+  if (value >= 0 && value <= 127)  // Value is ok.
+    return true;
+  printf("Payload type must be between 0 and 127, not %d\n",
+         static_cast<int>(value));
+  return false;
+}
+
+bool ValidateSsrcValue(const std::string& str) {
+  uint32_t dummy_ssrc;
+  if (ParseSsrc(str, &dummy_ssrc))  // Value is ok.
+    return true;
+  printf("Invalid SSRC: %s\n", str.c_str());
+  return false;
+}
+
+static bool ValidateExtensionId(int value) {
+  if (value > 0 && value <= 255)  // Value is ok.
+    return true;
+  printf("Extension ID must be between 1 and 255, not %d\n",
+         static_cast<int>(value));
+  return false;
+}
+
+// Define command line flags.
+DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
+DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
+DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
+DEFINE_int(isac, 103, "RTP payload type for iSAC");
+DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
+DEFINE_int(opus, 111, "RTP payload type for Opus");
+DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
+DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
+DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
+DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
+DEFINE_int(g722, 9, "RTP payload type for G.722");
+DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
+DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
+DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
+DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
+DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
+DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
+DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
+DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
+DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
+DEFINE_string(replacement_audio_file,
+              "",
+              "A PCM file that will be used to populate "
+              "dummy"
+              " RTP packets");
+DEFINE_string(ssrc,
+              "",
+              "Only use packets with this SSRC (decimal or hex, the latter "
+              "starting with 0x)");
+DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
+DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
+DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
+DEFINE_int(video_content_type, 7, "Extension ID for video content type");
+DEFINE_int(video_timing, 8, "Extension ID for video timing");
+DEFINE_bool(matlabplot,
+            false,
+            "Generates a matlab script for plotting the delay profile");
+DEFINE_bool(pythonplot,
+            false,
+            "Generates a python script for plotting the delay profile");
+DEFINE_bool(concealment_events, false, "Prints concealment events");
+
+// Maps a codec type to a printable name string.
+std::string CodecName(NetEqDecoder codec) {
+  switch (codec) {
+    case NetEqDecoder::kDecoderPCMu:
+      return "PCM-u";
+    case NetEqDecoder::kDecoderPCMa:
+      return "PCM-a";
+    case NetEqDecoder::kDecoderILBC:
+      return "iLBC";
+    case NetEqDecoder::kDecoderISAC:
+      return "iSAC";
+    case NetEqDecoder::kDecoderISACswb:
+      return "iSAC-swb (32 kHz)";
+    case NetEqDecoder::kDecoderOpus:
+      return "Opus";
+    case NetEqDecoder::kDecoderPCM16B:
+      return "PCM16b-nb (8 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bwb:
+      return "PCM16b-wb (16 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bswb32kHz:
+      return "PCM16b-swb32 (32 kHz)";
+    case NetEqDecoder::kDecoderPCM16Bswb48kHz:
+      return "PCM16b-swb48 (48 kHz)";
+    case NetEqDecoder::kDecoderG722:
+      return "G.722";
+    case NetEqDecoder::kDecoderRED:
+      return "redundant audio (RED)";
+    case NetEqDecoder::kDecoderAVT:
+      return "AVT/DTMF (8 kHz)";
+    case NetEqDecoder::kDecoderAVT16kHz:
+      return "AVT/DTMF (16 kHz)";
+    case NetEqDecoder::kDecoderAVT32kHz:
+      return "AVT/DTMF (32 kHz)";
+    case NetEqDecoder::kDecoderAVT48kHz:
+      return "AVT/DTMF (48 kHz)";
+    case NetEqDecoder::kDecoderCNGnb:
+      return "comfort noise (8 kHz)";
+    case NetEqDecoder::kDecoderCNGwb:
+      return "comfort noise (16 kHz)";
+    case NetEqDecoder::kDecoderCNGswb32kHz:
+      return "comfort noise (32 kHz)";
+    case NetEqDecoder::kDecoderCNGswb48kHz:
+      return "comfort noise (48 kHz)";
+    default:
+      FATAL();
+      return "undefined";
+  }
+}
+
+void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
+  std::cout << CodecName(codec) << ": " << flag << std::endl;
+}
+
+void PrintCodecMapping() {
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
+                         FLAG_pcm16b_swb32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
+                         FLAG_pcm16b_swb48);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
+  PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
+}
+
+absl::optional<int> CodecSampleRate(uint8_t payload_type) {
+  if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
+      payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
+      payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
+    return 8000;
+  if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
+      payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
+      payload_type == FLAG_avt_16)
+    return 16000;
+  if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
+      payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
+    return 32000;
+  if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
+      payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
+    return 48000;
+  if (payload_type == FLAG_red)
+    return 0;
+  return absl::nullopt;
+}
+
+}  // namespace
+
+// A callback class which prints whenver the inserted packet stream changes
+// the SSRC.
+class SsrcSwitchDetector : public NetEqPostInsertPacket {
+ public:
+  // Takes a pointer to another callback object, which will be invoked after
+  // this object finishes. This does not transfer ownership, and null is a
+  // valid value.
+  explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
+      : other_callback_(other_callback) {}
+
+  void AfterInsertPacket(const NetEqInput::PacketData& packet,
+                         NetEq* neteq) override {
+    if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
+      std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
+                << " to 0x" << std::hex << packet.header.ssrc << std::dec
+                << " (payload type "
+                << static_cast<int>(packet.header.payloadType) << ")"
+                << std::endl;
+    }
+    last_ssrc_ = packet.header.ssrc;
+    if (other_callback_) {
+      other_callback_->AfterInsertPacket(packet, neteq);
+    }
+  }
+
+ private:
+  NetEqPostInsertPacket* other_callback_;
+  absl::optional<uint32_t> last_ssrc_;
+};
+
+NetEqTestFactory::NetEqTestFactory() = default;
+
+NetEqTestFactory::~NetEqTestFactory() = default;
+
+void NetEqTestFactory::PrintCodecMap() {
+  PrintCodecMapping();
+}
+
+std::unique_ptr<NetEqTest> NetEqTestFactory::InitializeTest(
+    std::string input_file_name,
+    std::string output_file_name) {
+  RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcma));
+  RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
+  RTC_CHECK(ValidatePayloadType(FLAG_isac));
+  RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
+  RTC_CHECK(ValidatePayloadType(FLAG_opus));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
+  RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
+  RTC_CHECK(ValidatePayloadType(FLAG_g722));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
+  RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
+  RTC_CHECK(ValidatePayloadType(FLAG_red));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
+  RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
+  RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
+  RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
+  RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
+  RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
+  RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
+  RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
+
+  // Gather RTP header extensions in a map.
+  NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
+      {FLAG_audio_level, kRtpExtensionAudioLevel},
+      {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
+      {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
+      {FLAG_video_content_type, kRtpExtensionVideoContentType},
+      {FLAG_video_timing, kRtpExtensionVideoTiming}};
+
+  std::unique_ptr<NetEqInput> input;
+  if (RtpFileSource::ValidRtpDump(input_file_name) ||
+      RtpFileSource::ValidPcap(input_file_name)) {
+    input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
+  } else {
+    input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
+  }
+
+  std::cout << "Input file: " << input_file_name << std::endl;
+  RTC_CHECK(input) << "Cannot open input file";
+  RTC_CHECK(!input->ended()) << "Input file is empty";
+
+  // Check if an SSRC value was provided.
+  if (strlen(FLAG_ssrc) > 0) {
+    uint32_t ssrc;
+    RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
+    static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
+  }
+
+  // Check the sample rate.
+  absl::optional<int> sample_rate_hz;
+  std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
+  while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
+    RTC_DCHECK(first_rtp_header);
+    sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
+    if (sample_rate_hz) {
+      std::cout << "Found valid packet with payload type "
+                << static_cast<int>(first_rtp_header->payloadType)
+                << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
+                << std::dec << std::endl;
+      break;
+    }
+    // Discard this packet and move to the next. Keep track of discarded payload
+    // types and SSRCs.
+    discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
+                                  first_rtp_header->ssrc);
+    input->PopPacket();
+  }
+  if (!discarded_pt_and_ssrc.empty()) {
+    std::cout << "Discarded initial packets with the following payload types "
+                 "and SSRCs:"
+              << std::endl;
+    for (const auto& d : discarded_pt_and_ssrc) {
+      std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
+                << static_cast<int>(d.second) << std::dec << std::endl;
+    }
+  }
+  if (!sample_rate_hz) {
+    std::cout << "Cannot find any packets with known payload types"
+              << std::endl;
+    RTC_NOTREACHED();
+  }
+
+  // Open the output file now that we know the sample rate. (Rate is only needed
+  // for wav files.)
+  std::unique_ptr<AudioSink> output;
+  if (output_file_name.size() >= 4 &&
+      output_file_name.substr(output_file_name.size() - 4) == ".wav") {
+    // Open a wav file.
+    output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
+  } else {
+    // Open a pcm file.
+    output.reset(new OutputAudioFile(output_file_name));
+  }
+
+  std::cout << "Output file: " << output_file_name << std::endl;
+
+  NetEqTest::DecoderMap codecs = {
+    {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
+    {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
+#ifdef WEBRTC_CODEC_ILBC
+    {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
+#endif
+    {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
+#if !defined(WEBRTC_ANDROID)
+    {FLAG_isac_swb, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
+#endif
+#ifdef WEBRTC_CODEC_OPUS
+    {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
+#endif
+    {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
+    {FLAG_pcm16b_wb,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
+    {FLAG_pcm16b_swb32,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
+    {FLAG_pcm16b_swb48,
+     std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
+    {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
+    {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
+    {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
+    {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
+    {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
+    {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
+    {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
+    {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
+    {FLAG_cn_swb32,
+     std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
+    {FLAG_cn_swb48,
+     std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}
+  };
+
+  // Check if a replacement audio file was provided.
+  if (strlen(FLAG_replacement_audio_file) > 0) {
+    // Find largest unused payload type.
+    int replacement_pt = 127;
+    while (!(codecs.find(replacement_pt) == codecs.end() &&
+             ext_codecs_.find(replacement_pt) == ext_codecs_.end())) {
+      --replacement_pt;
+      RTC_CHECK_GE(replacement_pt, 0);
+    }
+
+    auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
+      std::set<uint8_t> b;
+      for (auto& x : a) {
+        b.insert(static_cast<uint8_t>(x));
+      }
+      return b;
+    };
+
+    std::set<uint8_t> cn_types = std_set_int32_to_uint8(
+        {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
+    std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
+        {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
+    input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
+                                          cn_types, forbidden_types));
+
+    replacement_decoder_.reset(new FakeDecodeFromFile(
+        std::unique_ptr<InputAudioFile>(
+            new InputAudioFile(FLAG_replacement_audio_file)),
+        48000, false));
+    NetEqTest::ExternalDecoderInfo ext_dec_info = {
+        replacement_decoder_.get(), NetEqDecoder::kDecoderArbitrary,
+        "replacement codec"};
+    ext_codecs_[replacement_pt] = ext_dec_info;
+  }
+
+  NetEqTest::Callbacks callbacks;
+  stats_plotter_.reset(new NetEqStatsPlotter(FLAG_matlabplot, FLAG_pythonplot,
+                                             FLAG_concealment_events,
+                                             output_file_name));
+
+  ssrc_switch_detector_.reset(
+      new SsrcSwitchDetector(stats_plotter_->stats_getter()->delay_analyzer()));
+  callbacks.post_insert_packet = ssrc_switch_detector_.get();
+  callbacks.get_audio_callback = stats_plotter_->stats_getter();
+  callbacks.simulation_ended_callback = stats_plotter_.get();
+  NetEq::Config config;
+  config.sample_rate_hz = *sample_rate_hz;
+  return absl::make_unique<NetEqTest>(config, codecs, ext_codecs_,
+                                      std::move(input), std::move(output),
+                                      callbacks);
+}
+
+}  // namespace test
+}  // namespace webrtc
diff --git a/modules/audio_coding/neteq/tools/neteq_test_factory.h b/modules/audio_coding/neteq/tools/neteq_test_factory.h
new file mode 100644
index 0000000..a249186
--- /dev/null
+++ b/modules/audio_coding/neteq/tools/neteq_test_factory.h
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_
+#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_
+
+#include <memory>
+#include <string>
+
+#include "modules/audio_coding/neteq/tools/neteq_test.h"
+
+namespace webrtc {
+namespace test {
+
+class SsrcSwitchDetector;
+class NetEqStatsGetter;
+class NetEqStatsPlotter;
+
+// Note that the NetEqTestFactory needs to be alive when the NetEqTest object is
+// used for a simulation.
+class NetEqTestFactory {
+ public:
+  NetEqTestFactory();
+  ~NetEqTestFactory();
+  void PrintCodecMap();
+  std::unique_ptr<NetEqTest> InitializeTest(std::string input_filename,
+                                            std::string output_filename);
+
+ private:
+  std::unique_ptr<AudioDecoder> replacement_decoder_;
+  NetEqTest::ExtDecoderMap ext_codecs_;
+  std::unique_ptr<SsrcSwitchDetector> ssrc_switch_detector_;
+  std::unique_ptr<NetEqStatsPlotter> stats_plotter_;
+};
+
+}  // namespace test
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_FACTORY_H_
diff --git a/modules/audio_coding/neteq/tools/rtc_event_log_source.cc b/modules/audio_coding/neteq/tools/rtc_event_log_source.cc
index 7c97824..9e435c7 100644
--- a/modules/audio_coding/neteq/tools/rtc_event_log_source.cc
+++ b/modules/audio_coding/neteq/tools/rtc_event_log_source.cc
@@ -10,7 +10,6 @@
 
 #include "modules/audio_coding/neteq/tools/rtc_event_log_source.h"
 
-#include <assert.h>
 #include <string.h>
 #include <iostream>
 #include <limits>
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index 052bb47..dd1569e 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -37,12 +37,8 @@
     "common.h",
     "echo_cancellation_impl.cc",
     "echo_cancellation_impl.h",
-    "echo_cancellation_proxy.cc",
-    "echo_cancellation_proxy.h",
     "echo_control_mobile_impl.cc",
     "echo_control_mobile_impl.h",
-    "echo_control_mobile_proxy.cc",
-    "echo_control_mobile_proxy.h",
     "echo_detector/circular_buffer.cc",
     "echo_detector/circular_buffer.h",
     "echo_detector/mean_variance_estimator.cc",
@@ -119,9 +115,10 @@
     "../../rtc_base:safe_minmax",
     "../../rtc_base:sanitizer",
     "../../rtc_base/system:arch",
+    "../../rtc_base/system:rtc_export",
     "../../system_wrappers:cpu_features_api",
-    "../../system_wrappers:field_trial_api",
-    "../../system_wrappers:metrics_api",
+    "../../system_wrappers:field_trial",
+    "../../system_wrappers:metrics",
     "aec:aec",
     "aec:aec_core",
     "aecm:aecm_core",
@@ -166,6 +163,7 @@
     "include/audio_processing_statistics.h",
   ]
   deps = [
+    "../../rtc_base/system:rtc_export",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -345,6 +343,7 @@
       "audio_frame_view_unittest.cc",
       "config_unittest.cc",
       "echo_cancellation_impl_unittest.cc",
+      "echo_control_mobile_unittest.cc",
       "gain_controller2_unittest.cc",
       "splitting_filter_unittest.cc",
       "test/fake_recording_device_unittest.cc",
@@ -419,6 +418,7 @@
         ":audioproc_protobuf_utils",
         ":audioproc_test_utils",
         ":audioproc_unittest_proto",
+        ":runtime_settings_protobuf_utils",
         "../../api/audio:audio_frame_api",
         "../../rtc_base:rtc_base_tests_utils",
         "../../rtc_base:rtc_task_queue",
@@ -430,7 +430,7 @@
         "audio_processing_impl_unittest.cc",
         "audio_processing_unittest.cc",
         "echo_cancellation_bit_exact_unittest.cc",
-        "echo_control_mobile_unittest.cc",
+        "echo_control_mobile_bit_exact_unittest.cc",
         "echo_detector/circular_buffer_unittest.cc",
         "echo_detector/mean_variance_estimator_unittest.cc",
         "echo_detector/moving_max_unittest.cc",
@@ -531,6 +531,8 @@
         ":audioproc_debug_proto",
         ":audioproc_protobuf_utils",
         ":audioproc_test_utils",
+        ":runtime_settings_protobuf_utils",
+        "../../api/audio:aec3_config_json",
         "../../api/audio:aec3_factory",
         "../../common_audio:common_audio",
         "../../rtc_base:checks",
@@ -540,7 +542,6 @@
         "../../rtc_base:rtc_task_queue",
         "../../rtc_base:stringutils",
         "../../system_wrappers",
-        "../../system_wrappers:system_wrappers_default",
         "../../test:test_support",
         "aec_dump",
         "aec_dump:aec_dump_impl",
@@ -610,7 +611,6 @@
       "../../rtc_base:rtc_base_approved",
       "../../rtc_base/system:file_wrapper",
       "../../system_wrappers",
-      "../../system_wrappers:metrics_default",
       "../../test:fileutils",
       "../../test:test_support",
       "agc:level_estimation",
@@ -630,7 +630,6 @@
       "../..:webrtc_common",
       "../../rtc_base/system:file_wrapper",
       "../../system_wrappers",
-      "../../system_wrappers:metrics_default",
     ]
   }
 
@@ -651,10 +650,26 @@
       deps = [
         ":audioproc_debug_proto",
         "../..:webrtc_common",
+        "../../rtc_base:checks",
         "../../rtc_base:protobuf_utils",
         "../../rtc_base:rtc_base_approved",
         "../../rtc_base/system:arch",
       ]
     }
+
+    rtc_static_library("runtime_settings_protobuf_utils") {
+      testonly = true
+      sources = [
+        "test/runtime_setting_util.cc",
+        "test/runtime_setting_util.h",
+      ]
+
+      deps = [
+        ":audio_processing",
+        ":audioproc_debug_proto",
+        ":audioproc_protobuf_utils",
+        "../../rtc_base:checks",
+      ]
+    }
   }
 }
diff --git a/modules/audio_processing/aec/BUILD.gn b/modules/audio_processing/aec/BUILD.gn
index 52441d7..294c43f 100644
--- a/modules/audio_processing/aec/BUILD.gn
+++ b/modules/audio_processing/aec/BUILD.gn
@@ -40,7 +40,7 @@
     "../../../rtc_base:rtc_base_approved",
     "../../../rtc_base/system:arch",
     "../../../system_wrappers:cpu_features_api",
-    "../../../system_wrappers:metrics_api",
+    "../../../system_wrappers:metrics",
     "../utility:block_mean_calculator",
     "../utility:legacy_delay_estimator",
     "../utility:ooura_fft",
diff --git a/modules/audio_processing/aec3/BUILD.gn b/modules/audio_processing/aec3/BUILD.gn
index 75ee372..d1f4595 100644
--- a/modules/audio_processing/aec3/BUILD.gn
+++ b/modules/audio_processing/aec3/BUILD.gn
@@ -26,6 +26,7 @@
     "block_framer.h",
     "block_processor.cc",
     "block_processor.h",
+    "block_processor2.cc",
     "block_processor_metrics.cc",
     "block_processor_metrics.h",
     "cascaded_biquad_filter.cc",
@@ -60,6 +61,8 @@
     "filter_analyzer.h",
     "frame_blocker.cc",
     "frame_blocker.h",
+    "fullband_erle_estimator.cc",
+    "fullband_erle_estimator.h",
     "main_filter_update_gain.cc",
     "main_filter_update_gain.h",
     "matched_filter.cc",
@@ -74,8 +77,10 @@
     "render_buffer.h",
     "render_delay_buffer.cc",
     "render_delay_buffer.h",
+    "render_delay_buffer2.cc",
     "render_delay_controller.cc",
     "render_delay_controller.h",
+    "render_delay_controller2.cc",
     "render_delay_controller_metrics.cc",
     "render_delay_controller_metrics.h",
     "render_signal_analyzer.cc",
@@ -98,6 +103,8 @@
     "skew_estimator.h",
     "stationarity_estimator.cc",
     "stationarity_estimator.h",
+    "subband_erle_estimator.cc",
+    "subband_erle_estimator.h",
     "subtractor.cc",
     "subtractor.h",
     "subtractor_output.cc",
@@ -133,8 +140,8 @@
     "../../../rtc_base:safe_minmax",
     "../../../rtc_base/system:arch",
     "../../../system_wrappers:cpu_features_api",
-    "../../../system_wrappers:field_trial_api",
-    "../../../system_wrappers:metrics_api",
+    "../../../system_wrappers:field_trial",
+    "../../../system_wrappers:metrics",
     "../utility:ooura_fft",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
diff --git a/modules/audio_processing/aec3/adaptive_fir_filter.cc b/modules/audio_processing/aec3/adaptive_fir_filter.cc
index 9a1e811..abe68f8 100644
--- a/modules/audio_processing/aec3/adaptive_fir_filter.cc
+++ b/modules/audio_processing/aec3/adaptive_fir_filter.cc
@@ -25,6 +25,7 @@
 #include "modules/audio_processing/aec3/fft_data.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
+#include "system_wrappers/include/field_trial.h"
 
 namespace webrtc {
 
@@ -417,12 +418,21 @@
 
 }  // namespace aec3
 
+namespace {
+
+bool EnablePartialFilterReset() {
+  return !field_trial::IsEnabled("WebRTC-Aec3PartialFilterResetKillSwitch");
+}
+
+}  // namespace
+
 AdaptiveFirFilter::AdaptiveFirFilter(size_t max_size_partitions,
                                      size_t initial_size_partitions,
                                      size_t size_change_duration_blocks,
                                      Aec3Optimization optimization,
                                      ApmDataDumper* data_dumper)
     : data_dumper_(data_dumper),
+      use_partial_filter_reset_(EnablePartialFilterReset()),
       fft_(),
       optimization_(optimization),
       max_size_partitions_(max_size_partitions),
@@ -455,20 +465,22 @@
 void AdaptiveFirFilter::HandleEchoPathChange() {
   size_t current_h_size = h_.size();
   h_.resize(GetTimeDomainLength(max_size_partitions_));
-  std::fill(h_.begin(), h_.end(), 0.f);
+  const size_t begin_coeffficient =
+      use_partial_filter_reset_ ? current_h_size : 0;
+  std::fill(h_.begin() + begin_coeffficient, h_.end(), 0.f);
   h_.resize(current_h_size);
 
   size_t current_size_partitions = H_.size();
   H_.resize(max_size_partitions_);
-  for (auto& H_j : H_) {
-    H_j.Clear();
+  H2_.resize(max_size_partitions_);
+
+  const size_t begin_partition =
+      use_partial_filter_reset_ ? current_size_partitions : 0;
+  for (size_t k = begin_partition; k < max_size_partitions_; ++k) {
+    H_[k].Clear();
+    H2_[k].fill(0.f);
   }
   H_.resize(current_size_partitions);
-
-  H2_.resize(max_size_partitions_);
-  for (auto& H2_k : H2_) {
-    H2_k.fill(0.f);
-  }
   H2_.resize(current_size_partitions);
 
   erl_.fill(0.f);
diff --git a/modules/audio_processing/aec3/adaptive_fir_filter.h b/modules/audio_processing/aec3/adaptive_fir_filter.h
index 5dfb466..f9d3f1a 100644
--- a/modules/audio_processing/aec3/adaptive_fir_filter.h
+++ b/modules/audio_processing/aec3/adaptive_fir_filter.h
@@ -164,6 +164,7 @@
   void UpdateSize();
 
   ApmDataDumper* const data_dumper_;
+  const bool use_partial_filter_reset_;
   const Aec3Fft fft_;
   const Aec3Optimization optimization_;
   const size_t max_size_partitions_;
diff --git a/modules/audio_processing/aec3/adaptive_fir_filter_unittest.cc b/modules/audio_processing/aec3/adaptive_fir_filter_unittest.cc
index 077586e..3d583e8 100644
--- a/modules/audio_processing/aec3/adaptive_fir_filter_unittest.cc
+++ b/modules/audio_processing/aec3/adaptive_fir_filter_unittest.cc
@@ -20,6 +20,7 @@
 #if defined(WEBRTC_ARCH_X86_FAMILY)
 #include <emmintrin.h>
 #endif
+
 #include "modules/audio_processing/aec3/aec3_fft.h"
 #include "modules/audio_processing/aec3/aec_state.h"
 #include "modules/audio_processing/aec3/cascaded_biquad_filter.h"
@@ -31,6 +32,7 @@
 #include "rtc_base/arraysize.h"
 #include "rtc_base/numerics/safe_minmax.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "system_wrappers/include/cpu_features_wrapper.h"
 #include "test/gtest.h"
 
@@ -39,9 +41,9 @@
 namespace {
 
 std::string ProduceDebugText(size_t delay) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << ", Delay: " << delay;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
diff --git a/modules/audio_processing/aec3/aec3_fft.cc b/modules/audio_processing/aec3/aec3_fft.cc
index b02f342..e29434d 100644
--- a/modules/audio_processing/aec3/aec3_fft.cc
+++ b/modules/audio_processing/aec3/aec3_fft.cc
@@ -11,6 +11,7 @@
 #include "modules/audio_processing/aec3/aec3_fft.h"
 
 #include <algorithm>
+#include <functional>
 
 #include "rtc_base/checks.h"
 
diff --git a/modules/audio_processing/aec3/aec_state.cc b/modules/audio_processing/aec3/aec_state.cc
index 62232ac..b9cd5ea 100644
--- a/modules/audio_processing/aec3/aec_state.cc
+++ b/modules/audio_processing/aec3/aec_state.cc
@@ -26,71 +26,20 @@
 namespace webrtc {
 namespace {
 
-bool EnableTransparentMode() {
-  return !field_trial::IsEnabled("WebRTC-Aec3TransparentModeKillSwitch");
+bool EnableErleResetsAtGainChanges() {
+  return !field_trial::IsEnabled("WebRTC-Aec3ResetErleAtGainChangesKillSwitch");
 }
 
-bool EnableStationaryRenderImprovements() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3StationaryRenderImprovementsKillSwitch");
+bool UseLegacyFilterQualityState() {
+  return field_trial::IsEnabled("WebRTC-Aec3FilterQualityStateKillSwitch");
 }
 
-bool EnableEnforcingDelayAfterRealignment() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3EnforceDelayAfterRealignmentKillSwitch");
+bool EnableLegacySaturationBehavior() {
+  return field_trial::IsEnabled("WebRTC-Aec3NewSaturationBehaviorKillSwitch");
 }
 
-bool EnableEarlyFilterUsage() {
-  return !field_trial::IsEnabled("WebRTC-Aec3EarlyLinearFilterUsageKillSwitch");
-}
-
-bool EnableShortInitialState() {
-  return !field_trial::IsEnabled("WebRTC-Aec3ShortInitialStateKillSwitch");
-}
-
-bool EnableUncertaintyUntilSufficientAdapted() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3ErleUncertaintyUntilSufficientlyAdaptedKillSwitch");
-}
-
-bool LowUncertaintyBeforeConvergence() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3LowUncertaintyBeforeConvergenceKillSwitch");
-}
-
-bool MediumUncertaintyBeforeConvergence() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3MediumUncertaintyBeforeConvergenceKillSwitch");
-}
-
-bool EarlyEntryToConvergedMode() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3EarlyEntryToConvergedModeKillSwitch");
-}
-
-bool UseEarlyLimiterDeactivation() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3EarlyLimiterDeactivationKillSwitch");
-}
-
-bool ResetErleAfterEchoPathChanges() {
-  return !field_trial::IsEnabled(
-      "WebRTC-Aec3ResetErleAfterEchoPathChangesKillSwitch");
-}
-
-float UncertaintyBeforeConvergence() {
-  if (LowUncertaintyBeforeConvergence()) {
-    return 1.f;
-  } else if (MediumUncertaintyBeforeConvergence()) {
-    return 4.f;
-  } else {
-    return 10.f;
-  }
-}
-
-float ComputeGainRampupIncrease(const EchoCanceller3Config& config) {
-  const auto& c = config.echo_removal_control.gain_rampup;
-  return powf(1.f / c.first_non_zero_gain, 1.f / c.non_zero_gain_blocks);
+bool UseSuppressionGainLimiter() {
+  return field_trial::IsEnabled("WebRTC-Aec3GainLimiterDeactivationKillSwitch");
 }
 
 constexpr size_t kBlocksSinceConvergencedFilterInit = 10000;
@@ -100,38 +49,65 @@
 
 int AecState::instance_count_ = 0;
 
+void AecState::GetResidualEchoScaling(
+    rtc::ArrayView<float> residual_scaling) const {
+  bool filter_has_had_time_to_converge;
+  if (config_.filter.conservative_initial_phase) {
+    filter_has_had_time_to_converge =
+        strong_not_saturated_render_blocks_ >= 1.5f * kNumBlocksPerSecond;
+  } else {
+    filter_has_had_time_to_converge =
+        strong_not_saturated_render_blocks_ >= 0.8f * kNumBlocksPerSecond;
+  }
+  echo_audibility_.GetResidualEchoScaling(filter_has_had_time_to_converge,
+                                          residual_scaling);
+}
+
+absl::optional<float> AecState::ErleUncertainty() const {
+  bool filter_has_had_time_to_converge;
+  if (config_.filter.conservative_initial_phase) {
+    filter_has_had_time_to_converge =
+        strong_not_saturated_render_blocks_ >= 1.5f * kNumBlocksPerSecond;
+  } else {
+    filter_has_had_time_to_converge =
+        strong_not_saturated_render_blocks_ >= 0.8f * kNumBlocksPerSecond;
+  }
+
+  if (!filter_has_had_time_to_converge) {
+    return 1.f;
+  }
+
+  if (SaturatedEcho() && use_legacy_saturation_behavior_) {
+    return 1.f;
+  }
+
+  return absl::nullopt;
+}
+
 AecState::AecState(const EchoCanceller3Config& config)
     : data_dumper_(
           new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
       config_(config),
-      allow_transparent_mode_(EnableTransparentMode()),
-      use_stationary_properties_(
-          EnableStationaryRenderImprovements() &&
-          config_.echo_audibility.use_stationary_properties),
-      enforce_delay_after_realignment_(EnableEnforcingDelayAfterRealignment()),
-      early_filter_usage_activated_(EnableEarlyFilterUsage() &&
-                                    !config.filter.conservative_initial_phase),
-      use_short_initial_state_(EnableShortInitialState() &&
-                               !config.filter.conservative_initial_phase),
-      convergence_trigger_linear_mode_(
-          !config.filter.conservative_initial_phase),
-      no_alignment_required_for_linear_mode_(
-          !config.filter.conservative_initial_phase),
-      use_uncertainty_until_sufficiently_adapted_(
-          EnableUncertaintyUntilSufficientAdapted()),
-      uncertainty_before_convergence_(UncertaintyBeforeConvergence()),
-      early_entry_to_converged_mode_(EarlyEntryToConvergedMode()),
-      early_limiter_deactivation_(UseEarlyLimiterDeactivation()),
-      reset_erle_after_echo_path_changes_(ResetErleAfterEchoPathChanges()),
-      erle_estimator_(config.erle.min, config.erle.max_l, config.erle.max_h),
-      max_render_(config_.filter.main.length_blocks, 0.f),
-      gain_rampup_increase_(ComputeGainRampupIncrease(config_)),
+      use_legacy_saturation_behavior_(EnableLegacySaturationBehavior()),
+      enable_erle_resets_at_gain_changes_(EnableErleResetsAtGainChanges()),
+      use_legacy_filter_quality_(UseLegacyFilterQualityState()),
+      use_suppressor_gain_limiter_(UseSuppressionGainLimiter()),
+      initial_state_(config_),
+      delay_state_(config_),
+      transparent_state_(config_),
+      filter_quality_state_(config_),
+      legacy_filter_quality_state_(config_),
+      legacy_saturation_detector_(config_),
+      erl_estimator_(2 * kNumBlocksPerSecond),
+      erle_estimator_(2 * kNumBlocksPerSecond,
+                      config_.erle.min,
+                      config_.erle.max_l,
+                      config_.erle.max_h),
       suppression_gain_limiter_(config_),
       filter_analyzer_(config_),
-      blocks_since_converged_filter_(kBlocksSinceConvergencedFilterInit),
-      active_blocks_since_consistent_filter_estimate_(
-          kBlocksSinceConsistentEstimateInit),
-      reverb_model_estimator_(config) {}
+      echo_audibility_(
+          config_.echo_audibility.use_stationarity_properties_at_init),
+      reverb_model_estimator_(config_) {}
 
 AecState::~AecState() = default;
 
@@ -139,22 +115,23 @@
     const EchoPathVariability& echo_path_variability) {
   const auto full_reset = [&]() {
     filter_analyzer_.Reset();
-    blocks_since_last_saturation_ = 0;
-    usable_linear_estimate_ = false;
     capture_signal_saturation_ = false;
-    echo_saturation_ = false;
-    std::fill(max_render_.begin(), max_render_.end(), 0.f);
-    blocks_with_proper_filter_adaptation_ = 0;
-    blocks_since_reset_ = 0;
-    filter_has_had_time_to_converge_ = false;
-    render_received_ = false;
+    strong_not_saturated_render_blocks_ = 0;
     blocks_with_active_render_ = 0;
-    initial_state_ = true;
-    suppression_gain_limiter_.Reset();
-    blocks_since_converged_filter_ = kBlocksSinceConvergencedFilterInit;
-    diverged_blocks_ = 0;
-    if (reset_erle_after_echo_path_changes_) {
-      erle_estimator_.Reset();
+    if (use_suppressor_gain_limiter_) {
+      suppression_gain_limiter_.Reset();
+    }
+    initial_state_.Reset();
+    transparent_state_.Reset();
+    if (use_legacy_saturation_behavior_) {
+      legacy_saturation_detector_.Reset();
+    }
+    erle_estimator_.Reset(true);
+    erl_estimator_.Reset();
+    if (use_legacy_filter_quality_) {
+      legacy_filter_quality_state_.Reset();
+    } else {
+      filter_quality_state_.Reset();
     }
   };
 
@@ -164,8 +141,10 @@
   if (echo_path_variability.delay_change !=
       EchoPathVariability::DelayAdjustment::kNone) {
     full_reset();
+  } else if (enable_erle_resets_at_gain_changes_ &&
+             echo_path_variability.gain_change) {
+    erle_estimator_.Reset(false);
   }
-
   subtractor_output_analyzer_.HandleEchoPathChange();
 }
 
@@ -182,256 +161,443 @@
   // Analyze the filter output.
   subtractor_output_analyzer_.Update(subtractor_output);
 
-  const bool converged_filter = subtractor_output_analyzer_.ConvergedFilter();
-  const bool diverged_filter = subtractor_output_analyzer_.DivergedFilter();
-
-  // Analyze the filter and compute the delays.
+  // Analyze the properties of the filter.
   filter_analyzer_.Update(adaptive_filter_impulse_response,
                           adaptive_filter_frequency_response, render_buffer);
-  filter_delay_blocks_ = filter_analyzer_.DelayBlocks();
-  if (enforce_delay_after_realignment_) {
-    if (external_delay &&
-        (!external_delay_ || external_delay_->delay != external_delay->delay)) {
-      frames_since_external_delay_change_ = 0;
-      external_delay_ = external_delay;
-    }
-    if (blocks_with_proper_filter_adaptation_ < 2 * kNumBlocksPerSecond &&
-        external_delay_) {
-      filter_delay_blocks_ = config_.delay.delay_headroom_blocks;
+
+  // Estimate the direct path delay of the filter.
+  delay_state_.Update(filter_analyzer_, external_delay,
+                      strong_not_saturated_render_blocks_);
+
+  const std::vector<float>& aligned_render_block =
+      render_buffer.Block(-delay_state_.DirectPathFilterDelay())[0];
+
+  // Update render counters.
+  const float render_energy = std::inner_product(
+      aligned_render_block.begin(), aligned_render_block.end(),
+      aligned_render_block.begin(), 0.f);
+  const bool active_render =
+      render_energy > (config_.render_levels.active_render_limit *
+                       config_.render_levels.active_render_limit) *
+                          kFftLengthBy2;
+  blocks_with_active_render_ += active_render ? 1 : 0;
+  strong_not_saturated_render_blocks_ +=
+      active_render && !SaturatedCapture() ? 1 : 0;
+
+  if (use_suppressor_gain_limiter_) {
+    // Update the limit on the echo suppression after an echo path change to
+    // avoid an initial echo burst.
+    suppression_gain_limiter_.Update(render_buffer.GetRenderActivity(),
+                                     TransparentMode());
+
+    if (subtractor_output_analyzer_.ConvergedFilter()) {
+      suppression_gain_limiter_.Deactivate();
     }
   }
 
-  if (filter_analyzer_.Consistent()) {
-    internal_delay_ = filter_analyzer_.DelayBlocks();
-  } else {
-    internal_delay_ = absl::nullopt;
-  }
-
-  external_delay_seen_ = external_delay_seen_ || external_delay;
-
-  const std::vector<float>& x = render_buffer.Block(-filter_delay_blocks_)[0];
-
-  // Update counters.
-  ++capture_block_counter_;
-  ++blocks_since_reset_;
-  const bool active_render_block = DetectActiveRender(x);
-  blocks_with_active_render_ += active_render_block ? 1 : 0;
-  blocks_with_proper_filter_adaptation_ +=
-      active_render_block && !SaturatedCapture() ? 1 : 0;
-
-  // Update the limit on the echo suppression after an echo path change to avoid
-  // an initial echo burst.
-  suppression_gain_limiter_.Update(render_buffer.GetRenderActivity(),
-                                   transparent_mode_);
-  if (converged_filter && early_limiter_deactivation_) {
-    suppression_gain_limiter_.Deactivate();
-  }
-
-  if (UseStationaryProperties()) {
+  if (config_.echo_audibility.use_stationary_properties) {
     // Update the echo audibility evaluator.
     echo_audibility_.Update(
-        render_buffer, FilterDelayBlocks(), external_delay_seen_,
+        render_buffer, delay_state_.DirectPathFilterDelay(),
+        delay_state_.ExternalDelayReported(),
         config_.ep_strength.reverb_based_on_render ? ReverbDecay() : 0.f);
   }
 
   // Update the ERL and ERLE measures.
-  if (reset_erle_after_echo_path_changes_ && transition_triggered_) {
-    erle_estimator_.Reset();
+  if (initial_state_.TransitionTriggered()) {
+    erle_estimator_.Reset(false);
   }
-  if (blocks_since_reset_ >= 2 * kNumBlocksPerSecond) {
-    const auto& X2 = render_buffer.Spectrum(filter_delay_blocks_);
-    erle_estimator_.Update(X2, Y2, E2_main, converged_filter,
-                           config_.erle.onset_detection);
-    if (converged_filter) {
-      erl_estimator_.Update(X2, Y2);
-    }
-  }
+  const auto& X2 = render_buffer.Spectrum(delay_state_.DirectPathFilterDelay());
+  erle_estimator_.Update(X2, Y2, E2_main,
+                         subtractor_output_analyzer_.ConvergedFilter(),
+                         config_.erle.onset_detection);
+  erl_estimator_.Update(subtractor_output_analyzer_.ConvergedFilter(), X2, Y2);
 
   // Detect and flag echo saturation.
-  if (config_.ep_strength.echo_can_saturate) {
-    echo_saturation_ = DetectEchoSaturation(x, EchoPathGain());
-  }
-
-  if (early_filter_usage_activated_) {
-    filter_has_had_time_to_converge_ =
-        blocks_with_proper_filter_adaptation_ >= 0.8f * kNumBlocksPerSecond;
+  if (use_legacy_saturation_behavior_) {
+    legacy_saturation_detector_.Update(aligned_render_block, SaturatedCapture(),
+                                       EchoPathGain());
   } else {
-    filter_has_had_time_to_converge_ =
-        blocks_with_proper_filter_adaptation_ >= 1.5f * kNumBlocksPerSecond;
+    saturation_detector_.Update(aligned_render_block, SaturatedCapture(),
+                                UsableLinearEstimate(), subtractor_output,
+                                EchoPathGain());
   }
 
-  if (converged_filter && early_entry_to_converged_mode_) {
-    filter_has_had_time_to_converge_ = true;
-  }
+  // Update the decision on whether to use the initial state parameter set.
+  initial_state_.Update(active_render, SaturatedCapture());
 
-  if (!filter_should_have_converged_) {
-    filter_should_have_converged_ =
-        blocks_with_proper_filter_adaptation_ > 6 * kNumBlocksPerSecond;
-  }
+  // Detect whether the transparent mode should be activated.
+  transparent_state_.Update(delay_state_.DirectPathFilterDelay(),
+                            filter_analyzer_.Consistent(),
+                            subtractor_output_analyzer_.ConvergedFilter(),
+                            subtractor_output_analyzer_.DivergedFilter(),
+                            active_render, SaturatedCapture());
 
-  // Flag whether the initial state is still active.
-  bool prev_initial_state = initial_state_;
-  if (use_short_initial_state_) {
-    initial_state_ = blocks_with_proper_filter_adaptation_ <
-                     config_.filter.initial_state_seconds * kNumBlocksPerSecond;
+  // Analyze the quality of the filter.
+  if (use_legacy_filter_quality_) {
+    legacy_filter_quality_state_.Update(
+        SaturatedEcho(), active_render, SaturatedCapture(), TransparentMode(),
+        external_delay, subtractor_output_analyzer_.ConvergedFilter(),
+        subtractor_output_analyzer_.DivergedFilter());
   } else {
-    initial_state_ =
-        blocks_with_proper_filter_adaptation_ < 5 * kNumBlocksPerSecond;
-  }
-  transition_triggered_ = !initial_state_ && prev_initial_state;
-
-  // Update counters for the filter divergence and convergence.
-  diverged_blocks_ = diverged_filter ? diverged_blocks_ + 1 : 0;
-  if (diverged_blocks_ >= 60) {
-    blocks_since_converged_filter_ = kBlocksSinceConvergencedFilterInit;
-  } else {
-    blocks_since_converged_filter_ =
-        converged_filter ? 0 : blocks_since_converged_filter_ + 1;
-  }
-  if (converged_filter) {
-    active_blocks_since_converged_filter_ = 0;
-  } else if (active_render_block) {
-    ++active_blocks_since_converged_filter_;
+    filter_quality_state_.Update(active_render, TransparentMode(),
+                                 SaturatedCapture(),
+                                 filter_analyzer_.Consistent(), external_delay,
+                                 subtractor_output_analyzer_.ConvergedFilter());
   }
 
-  bool recently_converged_filter =
-      blocks_since_converged_filter_ < 60 * kNumBlocksPerSecond;
-
-  if (blocks_since_converged_filter_ > 20 * kNumBlocksPerSecond) {
-    converged_filter_count_ = 0;
-  } else if (converged_filter) {
-    ++converged_filter_count_;
-  }
-  if (converged_filter_count_ > 50) {
-    finite_erl_ = true;
-  }
-
-  if (filter_analyzer_.Consistent() && filter_delay_blocks_ < 5) {
-    consistent_filter_seen_ = true;
-    active_blocks_since_consistent_filter_estimate_ = 0;
-  } else if (active_render_block) {
-    ++active_blocks_since_consistent_filter_estimate_;
-  }
-
-  bool consistent_filter_estimate_not_seen;
-  if (!consistent_filter_seen_) {
-    consistent_filter_estimate_not_seen =
-        capture_block_counter_ > 5 * kNumBlocksPerSecond;
-  } else {
-    consistent_filter_estimate_not_seen =
-        active_blocks_since_consistent_filter_estimate_ >
-        30 * kNumBlocksPerSecond;
-  }
-
-  converged_filter_seen_ = converged_filter_seen_ || converged_filter;
-
-  // If no filter convergence is seen for a long time, reset the estimated
-  // properties of the echo path.
-  if (active_blocks_since_converged_filter_ > 60 * kNumBlocksPerSecond) {
-    converged_filter_seen_ = false;
-    finite_erl_ = false;
-  }
-
-  // After an amount of active render samples for which an echo should have been
-  // detected in the capture signal if the ERL was not infinite, flag that a
-  // transparent mode should be entered.
-  transparent_mode_ = !config_.ep_strength.bounded_erl && !finite_erl_;
-  transparent_mode_ =
-      transparent_mode_ &&
-      (consistent_filter_estimate_not_seen || !converged_filter_seen_);
-  transparent_mode_ = transparent_mode_ && filter_should_have_converged_;
-  transparent_mode_ = transparent_mode_ && allow_transparent_mode_;
-
-  usable_linear_estimate_ = !echo_saturation_;
-
-  if (convergence_trigger_linear_mode_) {
-    usable_linear_estimate_ =
-        usable_linear_estimate_ &&
-        ((filter_has_had_time_to_converge_ && external_delay) ||
-         converged_filter_seen_);
-  } else {
-    usable_linear_estimate_ =
-        usable_linear_estimate_ && filter_has_had_time_to_converge_;
-  }
-
-  if (!no_alignment_required_for_linear_mode_) {
-    usable_linear_estimate_ = usable_linear_estimate_ && external_delay;
-  }
-
-  if (!config_.echo_removal_control.linear_and_stable_echo_path) {
-    usable_linear_estimate_ =
-        usable_linear_estimate_ && recently_converged_filter;
-  }
-  usable_linear_estimate_ = usable_linear_estimate_ && !TransparentMode();
-
-  use_linear_filter_output_ = usable_linear_estimate_ && !TransparentMode();
-
+  // Update the reverb estimate.
   const bool stationary_block =
-      use_stationary_properties_ && echo_audibility_.IsBlockStationary();
+      config_.echo_audibility.use_stationary_properties &&
+      echo_audibility_.IsBlockStationary();
 
-  reverb_model_estimator_.Update(
-      filter_analyzer_.GetAdjustedFilter(), adaptive_filter_frequency_response,
-      erle_estimator_.GetInstLinearQualityEstimate(), filter_delay_blocks_,
-      usable_linear_estimate_, stationary_block);
+  reverb_model_estimator_.Update(filter_analyzer_.GetAdjustedFilter(),
+                                 adaptive_filter_frequency_response,
+                                 erle_estimator_.GetInstLinearQualityEstimate(),
+                                 delay_state_.DirectPathFilterDelay(),
+                                 UsableLinearEstimate(), stationary_block);
 
   erle_estimator_.Dump(data_dumper_);
   reverb_model_estimator_.Dump(data_dumper_.get());
   data_dumper_->DumpRaw("aec3_erl", Erl());
   data_dumper_->DumpRaw("aec3_erl_time_domain", ErlTimeDomain());
   data_dumper_->DumpRaw("aec3_usable_linear_estimate", UsableLinearEstimate());
-  data_dumper_->DumpRaw("aec3_transparent_mode", transparent_mode_);
-  data_dumper_->DumpRaw("aec3_state_internal_delay",
-                        internal_delay_ ? *internal_delay_ : -1);
+  data_dumper_->DumpRaw("aec3_transparent_mode", TransparentMode());
   data_dumper_->DumpRaw("aec3_filter_delay", filter_analyzer_.DelayBlocks());
 
   data_dumper_->DumpRaw("aec3_consistent_filter",
                         filter_analyzer_.Consistent());
   data_dumper_->DumpRaw("aec3_suppression_gain_limit", SuppressionGainLimit());
-  data_dumper_->DumpRaw("aec3_initial_state", initial_state_);
+  data_dumper_->DumpRaw("aec3_initial_state",
+                        initial_state_.InitialStateActive());
   data_dumper_->DumpRaw("aec3_capture_saturation", SaturatedCapture());
-  data_dumper_->DumpRaw("aec3_echo_saturation", echo_saturation_);
-  data_dumper_->DumpRaw("aec3_converged_filter", converged_filter);
-  data_dumper_->DumpRaw("aec3_diverged_filter", diverged_filter);
+  data_dumper_->DumpRaw("aec3_echo_saturation", SaturatedEcho());
+  data_dumper_->DumpRaw("aec3_converged_filter",
+                        subtractor_output_analyzer_.ConvergedFilter());
+  data_dumper_->DumpRaw("aec3_diverged_filter",
+                        subtractor_output_analyzer_.DivergedFilter());
 
   data_dumper_->DumpRaw("aec3_external_delay_avaliable",
                         external_delay ? 1 : 0);
-  data_dumper_->DumpRaw("aec3_consistent_filter_estimate_not_seen",
-                        consistent_filter_estimate_not_seen);
-  data_dumper_->DumpRaw("aec3_filter_should_have_converged",
-                        filter_should_have_converged_);
-  data_dumper_->DumpRaw("aec3_filter_has_had_time_to_converge",
-                        filter_has_had_time_to_converge_);
-  data_dumper_->DumpRaw("aec3_recently_converged_filter",
-                        recently_converged_filter);
   data_dumper_->DumpRaw("aec3_suppresion_gain_limiter_running",
                         IsSuppressionGainLimitActive());
   data_dumper_->DumpRaw("aec3_filter_tail_freq_resp_est",
                         GetReverbFrequencyResponse());
 }
 
-bool AecState::DetectActiveRender(rtc::ArrayView<const float> x) const {
-  const float x_energy = std::inner_product(x.begin(), x.end(), x.begin(), 0.f);
-  return x_energy > (config_.render_levels.active_render_limit *
-                     config_.render_levels.active_render_limit) *
-                        kFftLengthBy2;
+AecState::InitialState::InitialState(const EchoCanceller3Config& config)
+    : conservative_initial_phase_(config.filter.conservative_initial_phase),
+      initial_state_seconds_(config.filter.initial_state_seconds) {
+  Reset();
 }
+void AecState::InitialState::InitialState::Reset() {
+  initial_state_ = true;
+  strong_not_saturated_render_blocks_ = 0;
+}
+void AecState::InitialState::InitialState::Update(bool active_render,
+                                                  bool saturated_capture) {
+  strong_not_saturated_render_blocks_ +=
+      active_render && !saturated_capture ? 1 : 0;
 
-bool AecState::DetectEchoSaturation(rtc::ArrayView<const float> x,
-                                    float echo_path_gain) {
-  RTC_DCHECK_LT(0, x.size());
-  const float max_sample = fabs(*std::max_element(
-      x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
-
-  // Set flag for potential presence of saturated echo
-  const float kMargin = 10.f;
-  float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
-  if (SaturatedCapture() && peak_echo_amplitude > 32000) {
-    blocks_since_last_saturation_ = 0;
+  // Flag whether the initial state is still active.
+  bool prev_initial_state = initial_state_;
+  if (conservative_initial_phase_) {
+    initial_state_ =
+        strong_not_saturated_render_blocks_ < 5 * kNumBlocksPerSecond;
   } else {
-    ++blocks_since_last_saturation_;
+    initial_state_ = strong_not_saturated_render_blocks_ <
+                     initial_state_seconds_ * kNumBlocksPerSecond;
   }
 
-  return blocks_since_last_saturation_ < 5;
+  // Flag whether the transition from the initial state has started.
+  transition_triggered_ = !initial_state_ && prev_initial_state;
+}
+
+AecState::FilterDelay::FilterDelay(const EchoCanceller3Config& config)
+    : delay_headroom_blocks_(config.delay.delay_headroom_blocks) {}
+
+void AecState::FilterDelay::Update(
+    const FilterAnalyzer& filter_analyzer,
+    const absl::optional<DelayEstimate>& external_delay,
+    size_t blocks_with_proper_filter_adaptation) {
+  // Update the delay based on the external delay.
+  if (external_delay &&
+      (!external_delay_ || external_delay_->delay != external_delay->delay)) {
+    external_delay_ = external_delay;
+    external_delay_reported_ = true;
+  }
+
+  // Override the estimated delay if it is not certain that the filter has had
+  // time to converge.
+  const bool delay_estimator_may_not_have_converged =
+      blocks_with_proper_filter_adaptation < 2 * kNumBlocksPerSecond;
+  if (delay_estimator_may_not_have_converged && external_delay_) {
+    filter_delay_blocks_ = delay_headroom_blocks_;
+  } else {
+    filter_delay_blocks_ = filter_analyzer.DelayBlocks();
+  }
+}
+
+AecState::TransparentMode::TransparentMode(const EchoCanceller3Config& config)
+    : bounded_erl_(config.ep_strength.bounded_erl),
+      linear_and_stable_echo_path_(
+          config.echo_removal_control.linear_and_stable_echo_path),
+      active_blocks_since_sane_filter_(kBlocksSinceConsistentEstimateInit),
+      non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {}
+
+void AecState::TransparentMode::Reset() {
+  non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
+  diverged_sequence_size_ = 0;
+  strong_not_saturated_render_blocks_ = 0;
+  if (linear_and_stable_echo_path_) {
+    recent_convergence_during_activity_ = false;
+  }
+}
+
+void AecState::TransparentMode::Update(int filter_delay_blocks,
+                                       bool consistent_filter,
+                                       bool converged_filter,
+                                       bool diverged_filter,
+                                       bool active_render,
+                                       bool saturated_capture) {
+  ++capture_block_counter_;
+  strong_not_saturated_render_blocks_ +=
+      active_render && !saturated_capture ? 1 : 0;
+
+  if (consistent_filter && filter_delay_blocks < 5) {
+    sane_filter_observed_ = true;
+    active_blocks_since_sane_filter_ = 0;
+  } else if (active_render) {
+    ++active_blocks_since_sane_filter_;
+  }
+
+  bool sane_filter_recently_seen;
+  if (!sane_filter_observed_) {
+    sane_filter_recently_seen =
+        capture_block_counter_ <= 5 * kNumBlocksPerSecond;
+  } else {
+    sane_filter_recently_seen =
+        active_blocks_since_sane_filter_ <= 30 * kNumBlocksPerSecond;
+  }
+
+  if (converged_filter) {
+    recent_convergence_during_activity_ = true;
+    active_non_converged_sequence_size_ = 0;
+    non_converged_sequence_size_ = 0;
+    ++num_converged_blocks_;
+  } else {
+    if (++non_converged_sequence_size_ > 20 * kNumBlocksPerSecond) {
+      num_converged_blocks_ = 0;
+    }
+
+    if (active_render &&
+        ++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
+      recent_convergence_during_activity_ = false;
+    }
+  }
+
+  if (!diverged_filter) {
+    diverged_sequence_size_ = 0;
+  } else if (++diverged_sequence_size_ >= 60) {
+    // TODO(peah): Change these lines to ensure proper triggering of usable
+    // filter.
+    non_converged_sequence_size_ = kBlocksSinceConvergencedFilterInit;
+  }
+
+  if (active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
+    finite_erl_recently_detected_ = false;
+  }
+  if (num_converged_blocks_ > 50) {
+    finite_erl_recently_detected_ = true;
+  }
+
+  if (bounded_erl_) {
+    transparency_activated_ = false;
+  } else if (finite_erl_recently_detected_) {
+    transparency_activated_ = false;
+  } else if (sane_filter_recently_seen && recent_convergence_during_activity_) {
+    transparency_activated_ = false;
+  } else {
+    const bool filter_should_have_converged =
+        strong_not_saturated_render_blocks_ > 6 * kNumBlocksPerSecond;
+    transparency_activated_ = filter_should_have_converged;
+  }
+}
+
+AecState::FilteringQualityAnalyzer::FilteringQualityAnalyzer(
+    const EchoCanceller3Config& config) {}
+
+void AecState::FilteringQualityAnalyzer::Reset() {
+  usable_linear_estimate_ = false;
+  filter_update_blocks_since_reset_ = 0;
+}
+
+void AecState::FilteringQualityAnalyzer::Update(
+    bool active_render,
+    bool transparent_mode,
+    bool saturated_capture,
+    bool consistent_estimate_,
+    const absl::optional<DelayEstimate>& external_delay,
+    bool converged_filter) {
+  // Update blocks counter.
+  const bool filter_update = active_render && !saturated_capture;
+  filter_update_blocks_since_reset_ += filter_update ? 1 : 0;
+  filter_update_blocks_since_start_ += filter_update ? 1 : 0;
+
+  // Store convergence flag when observed.
+  convergence_seen_ = convergence_seen_ || converged_filter;
+
+  // Verify requirements for achieving a decent filter. The requirements for
+  // filter adaptation at call startup are more restrictive than after an
+  // in-call reset.
+  const bool sufficient_data_to_converge_at_startup =
+      filter_update_blocks_since_start_ > kNumBlocksPerSecond * 0.4f;
+  const bool sufficient_data_to_converge_at_reset =
+      sufficient_data_to_converge_at_startup &&
+      filter_update_blocks_since_reset_ > kNumBlocksPerSecond * 0.2f;
+
+  // The linear filter can only be used it has had time to converge.
+  usable_linear_estimate_ = sufficient_data_to_converge_at_startup &&
+                            sufficient_data_to_converge_at_reset;
+
+  // The linear filter can only be used if an external delay or convergence have
+  // been identified
+  usable_linear_estimate_ =
+      usable_linear_estimate_ && (external_delay || convergence_seen_);
+
+  // If transparent mode is on, deactivate usign the linear filter.
+  usable_linear_estimate_ = usable_linear_estimate_ && !transparent_mode;
+}
+
+AecState::LegacyFilteringQualityAnalyzer::LegacyFilteringQualityAnalyzer(
+    const EchoCanceller3Config& config)
+    : conservative_initial_phase_(config.filter.conservative_initial_phase),
+      required_blocks_for_convergence_(
+          kNumBlocksPerSecond * (conservative_initial_phase_ ? 1.5f : 0.8f)),
+      linear_and_stable_echo_path_(
+          config.echo_removal_control.linear_and_stable_echo_path),
+      non_converged_sequence_size_(kBlocksSinceConvergencedFilterInit) {}
+
+void AecState::LegacyFilteringQualityAnalyzer::Reset() {
+  usable_linear_estimate_ = false;
+  strong_not_saturated_render_blocks_ = 0;
+  if (linear_and_stable_echo_path_) {
+    recent_convergence_during_activity_ = false;
+  }
+  diverged_sequence_size_ = 0;
+  // TODO(peah): Change to ensure proper triggering of usable filter.
+  non_converged_sequence_size_ = 10000;
+  recent_convergence_ = true;
+}
+
+void AecState::LegacyFilteringQualityAnalyzer::Update(
+    bool saturated_echo,
+    bool active_render,
+    bool saturated_capture,
+    bool transparent_mode,
+    const absl::optional<DelayEstimate>& external_delay,
+    bool converged_filter,
+    bool diverged_filter) {
+  diverged_sequence_size_ = diverged_filter ? diverged_sequence_size_ + 1 : 0;
+  if (diverged_sequence_size_ >= 60) {
+    // TODO(peah): Change these lines to ensure proper triggering of usable
+    // filter.
+    non_converged_sequence_size_ = 10000;
+    recent_convergence_ = true;
+  }
+
+  if (converged_filter) {
+    non_converged_sequence_size_ = 0;
+    recent_convergence_ = true;
+    active_non_converged_sequence_size_ = 0;
+    recent_convergence_during_activity_ = true;
+  } else {
+    if (++non_converged_sequence_size_ >= 60 * kNumBlocksPerSecond) {
+      recent_convergence_ = false;
+    }
+
+    if (active_render &&
+        ++active_non_converged_sequence_size_ > 60 * kNumBlocksPerSecond) {
+      recent_convergence_during_activity_ = false;
+    }
+  }
+
+  strong_not_saturated_render_blocks_ +=
+      active_render && !saturated_capture ? 1 : 0;
+  const bool filter_has_had_time_to_converge =
+      strong_not_saturated_render_blocks_ > required_blocks_for_convergence_;
+
+  usable_linear_estimate_ = filter_has_had_time_to_converge && external_delay;
+
+  if (!conservative_initial_phase_ && recent_convergence_during_activity_) {
+    usable_linear_estimate_ = true;
+  }
+
+  if (!linear_and_stable_echo_path_ && !recent_convergence_) {
+    usable_linear_estimate_ = false;
+  }
+
+  if (saturated_echo || transparent_mode) {
+    usable_linear_estimate_ = false;
+  }
+}
+
+void AecState::SaturationDetector::Update(
+    rtc::ArrayView<const float> x,
+    bool saturated_capture,
+    bool usable_linear_estimate,
+    const SubtractorOutput& subtractor_output,
+    float echo_path_gain) {
+  saturated_echo_ = saturated_capture;
+  if (usable_linear_estimate) {
+    constexpr float kSaturationThreshold = 20000.f;
+    saturated_echo_ =
+        saturated_echo_ &&
+        (subtractor_output.s_main_max_abs > kSaturationThreshold ||
+         subtractor_output.s_shadow_max_abs > kSaturationThreshold);
+  } else {
+    const float max_sample = fabs(*std::max_element(
+        x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
+
+    const float kMargin = 10.f;
+    float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
+    saturated_echo_ = saturated_echo_ && peak_echo_amplitude > 32000;
+  }
+}
+
+AecState::LegacySaturationDetector::LegacySaturationDetector(
+    const EchoCanceller3Config& config)
+    : echo_can_saturate_(config.ep_strength.echo_can_saturate),
+      not_saturated_sequence_size_(1000) {}
+
+void AecState::LegacySaturationDetector::Reset() {
+  not_saturated_sequence_size_ = 0;
+}
+
+void AecState::LegacySaturationDetector::Update(rtc::ArrayView<const float> x,
+                                                bool saturated_capture,
+                                                float echo_path_gain) {
+  if (!echo_can_saturate_) {
+    saturated_echo_ = false;
+    return;
+  }
+
+  RTC_DCHECK_LT(0, x.size());
+  if (saturated_capture) {
+    const float max_sample = fabs(*std::max_element(
+        x.begin(), x.end(), [](float a, float b) { return a * a < b * b; }));
+
+    // Set flag for potential presence of saturated echo
+    const float kMargin = 10.f;
+    float peak_echo_amplitude = max_sample * echo_path_gain * kMargin;
+    if (peak_echo_amplitude > 32000) {
+      not_saturated_sequence_size_ = 0;
+      saturated_echo_ = true;
+      return;
+    }
+  }
+
+  saturated_echo_ = ++not_saturated_sequence_size_ < 5;
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/aec_state.h b/modules/audio_processing/aec3/aec_state.h
index ea30daf..9bb8624 100644
--- a/modules/audio_processing/aec3/aec_state.h
+++ b/modules/audio_processing/aec3/aec_state.h
@@ -32,7 +32,6 @@
 #include "modules/audio_processing/aec3/subtractor_output.h"
 #include "modules/audio_processing/aec3/subtractor_output_analyzer.h"
 #include "modules/audio_processing/aec3/suppression_gain_limiter.h"
-#include "rtc_base/constructormagic.h"
 
 namespace webrtc {
 
@@ -46,10 +45,20 @@
 
   // Returns whether the echo subtractor can be used to determine the residual
   // echo.
-  bool UsableLinearEstimate() const { return usable_linear_estimate_; }
+  bool UsableLinearEstimate() const {
+    if (use_legacy_filter_quality_) {
+      return legacy_filter_quality_state_.LinearFilterUsable();
+    }
+    return filter_quality_state_.LinearFilterUsable();
+  }
 
   // Returns whether the echo subtractor output should be used as output.
-  bool UseLinearFilterOutput() const { return use_linear_filter_output_; }
+  bool UseLinearFilterOutput() const {
+    if (use_legacy_filter_quality_) {
+      return legacy_filter_quality_state_.LinearFilterUsable();
+    }
+    return filter_quality_state_.LinearFilterUsable();
+  }
 
   // Returns the estimated echo path gain.
   float EchoPathGain() const { return filter_analyzer_.Gain(); }
@@ -59,32 +68,27 @@
 
   // Returns the appropriate scaling of the residual echo to match the
   // audibility.
-  void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
-    echo_audibility_.GetResidualEchoScaling(residual_scaling);
-  }
+  void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const;
 
   // Returns whether the stationary properties of the signals are used in the
   // aec.
-  bool UseStationaryProperties() const { return use_stationary_properties_; }
+  bool UseStationaryProperties() const {
+    return config_.echo_audibility.use_stationary_properties;
+  }
 
   // Returns the ERLE.
   const std::array<float, kFftLengthBy2Plus1>& Erle() const {
     return erle_estimator_.Erle();
   }
 
-  // Returns any uncertainty in the ERLE estimate.
-  absl::optional<float> ErleUncertainty() const {
-    if (!filter_has_had_time_to_converge_ &&
-        use_uncertainty_until_sufficiently_adapted_) {
-      return uncertainty_before_convergence_;
-    }
-    return absl::nullopt;
-  }
+  // Returns an offset to apply to the estimation of the residual echo
+  // computation. Returning nullopt means that no offset should be used, while
+  // any other value will be applied as a multiplier to the estimated residual
+  // echo.
+  absl::optional<float> ErleUncertainty() const;
 
-  // Returns the time-domain ERLE in log2 units.
-  float ErleTimeDomainLog2() const {
-    return erle_estimator_.ErleTimeDomainLog2();
-  }
+  // Returns the fullband ERLE estimate in log2 units.
+  float FullBandErleLog2() const { return erle_estimator_.FullbandErleLog2(); }
 
   // Returns the ERL.
   const std::array<float, kFftLengthBy2Plus1>& Erl() const {
@@ -95,16 +99,17 @@
   float ErlTimeDomain() const { return erl_estimator_.ErlTimeDomain(); }
 
   // Returns the delay estimate based on the linear filter.
-  int FilterDelayBlocks() const { return filter_delay_blocks_; }
-
-  // Returns the internal delay estimate based on the linear filter.
-  absl::optional<int> InternalDelay() const { return internal_delay_; }
+  int FilterDelayBlocks() const { return delay_state_.DirectPathFilterDelay(); }
 
   // Returns whether the capture signal is saturated.
   bool SaturatedCapture() const { return capture_signal_saturation_; }
 
   // Returns whether the echo signal is saturated.
-  bool SaturatedEcho() const { return echo_saturation_; }
+  bool SaturatedEcho() const {
+    return use_legacy_saturation_behavior_
+               ? legacy_saturation_detector_.SaturatedEcho()
+               : saturation_detector_.SaturatedEcho();
+  }
 
   // Updates the capture signal saturation.
   void UpdateCaptureSaturation(bool capture_signal_saturation) {
@@ -112,7 +117,7 @@
   }
 
   // Returns whether the transparent mode is active
-  bool TransparentMode() const { return transparent_mode_; }
+  bool TransparentMode() const { return transparent_state_.Active(); }
 
   // Takes appropriate action at an echo path change.
   void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
@@ -127,7 +132,11 @@
 
   // Returns the upper limit for the echo suppression gain.
   float SuppressionGainLimit() const {
-    return suppression_gain_limiter_.Limit();
+    if (use_suppressor_gain_limiter_) {
+      return suppression_gain_limiter_.Limit();
+    } else {
+      return 1.f;
+    }
   }
 
   // Returns whether the suppression gain limiter is active.
@@ -135,14 +144,11 @@
     return suppression_gain_limiter_.IsActive();
   }
 
-  // Returns whether the linear filter should have been able to properly adapt.
-  bool FilterHasHadTimeToConverge() const {
-    return filter_has_had_time_to_converge_;
-  }
-
   // Returns whether the transition for going out of the initial stated has
   // been triggered.
-  bool TransitionTriggered() const { return transition_triggered_; }
+  bool TransitionTriggered() const {
+    return initial_state_.TransitionTriggered();
+  }
 
   // Updates the aec state.
   void Update(const absl::optional<DelayEstimate>& external_delay,
@@ -161,65 +167,223 @@
   }
 
  private:
-  bool DetectActiveRender(rtc::ArrayView<const float> x) const;
-  void UpdateSuppressorGainLimit(bool render_activity);
-  bool DetectEchoSaturation(rtc::ArrayView<const float> x,
-                            float echo_path_gain);
-
   static int instance_count_;
   std::unique_ptr<ApmDataDumper> data_dumper_;
   const EchoCanceller3Config config_;
-  const bool allow_transparent_mode_;
-  const bool use_stationary_properties_;
-  const bool enforce_delay_after_realignment_;
-  const bool early_filter_usage_activated_;
-  const bool use_short_initial_state_;
-  const bool convergence_trigger_linear_mode_;
-  const bool no_alignment_required_for_linear_mode_;
-  const bool use_uncertainty_until_sufficiently_adapted_;
-  const float uncertainty_before_convergence_;
-  const bool early_entry_to_converged_mode_;
-  const bool early_limiter_deactivation_;
-  const bool reset_erle_after_echo_path_changes_;
+  const bool use_legacy_saturation_behavior_;
+  const bool enable_erle_resets_at_gain_changes_;
+  const bool use_legacy_filter_quality_;
+  const bool use_suppressor_gain_limiter_;
+
+  // Class for controlling the transition from the intial state, which in turn
+  // controls when the filter parameters for the initial state should be used.
+  class InitialState {
+   public:
+    explicit InitialState(const EchoCanceller3Config& config);
+    // Resets the state to again begin in the initial state.
+    void Reset();
+
+    // Updates the state based on new data.
+    void Update(bool active_render, bool saturated_capture);
+
+    // Returns whether the initial state is active or not.
+    bool InitialStateActive() const { return initial_state_; }
+
+    // Returns that the transition from the initial state has was started.
+    bool TransitionTriggered() const { return transition_triggered_; }
+
+   private:
+    const bool conservative_initial_phase_;
+    const float initial_state_seconds_;
+    bool transition_triggered_ = false;
+    bool initial_state_ = true;
+    size_t strong_not_saturated_render_blocks_ = 0;
+  } initial_state_;
+
+  // Class for choosing the direct-path delay relative to the beginning of the
+  // filter, as well as any other data related to the delay used within
+  // AecState.
+  class FilterDelay {
+   public:
+    explicit FilterDelay(const EchoCanceller3Config& config);
+
+    // Returns whether an external delay has been reported to the AecState (from
+    // the delay estimator).
+    bool ExternalDelayReported() const { return external_delay_reported_; }
+
+    // Returns the delay in blocks relative to the beginning of the filter that
+    // corresponds to the direct path of the echo.
+    int DirectPathFilterDelay() const { return filter_delay_blocks_; }
+
+    // Updates the delay estimates based on new data.
+    void Update(const FilterAnalyzer& filter_analyzer,
+                const absl::optional<DelayEstimate>& external_delay,
+                size_t blocks_with_proper_filter_adaptation);
+
+   private:
+    const int delay_headroom_blocks_;
+    bool external_delay_reported_ = false;
+    int filter_delay_blocks_ = 0;
+    absl::optional<DelayEstimate> external_delay_;
+  } delay_state_;
+
+  // Class for detecting and toggling the transparent mode which causes the
+  // suppressor to apply no suppression.
+  class TransparentMode {
+   public:
+    explicit TransparentMode(const EchoCanceller3Config& config);
+
+    // Returns whether the transparent mode should be active.
+    bool Active() const { return transparency_activated_; }
+
+    // Resets the state of the detector.
+    void Reset();
+
+    // Updates the detection deciscion based on new data.
+    void Update(int filter_delay_blocks,
+                bool consistent_filter,
+                bool converged_filter,
+                bool diverged_filter,
+                bool active_render,
+                bool saturated_capture);
+
+   private:
+    const bool bounded_erl_;
+    const bool linear_and_stable_echo_path_;
+    size_t capture_block_counter_ = 0;
+    bool transparency_activated_ = false;
+    size_t active_blocks_since_sane_filter_;
+    bool sane_filter_observed_ = false;
+    bool finite_erl_recently_detected_ = false;
+    size_t non_converged_sequence_size_;
+    size_t diverged_sequence_size_ = 0;
+    size_t active_non_converged_sequence_size_ = 0;
+    size_t num_converged_blocks_ = 0;
+    bool recent_convergence_during_activity_ = false;
+    size_t strong_not_saturated_render_blocks_ = 0;
+  } transparent_state_;
+
+  // Class for analyzing how well the linear filter is, and can be expected to,
+  // perform on the current signals. The purpose of this is for using to
+  // select the echo suppression functionality as well as the input to the echo
+  // suppressor.
+  class FilteringQualityAnalyzer {
+   public:
+    FilteringQualityAnalyzer(const EchoCanceller3Config& config);
+
+    // Returns whether the the linear filter can be used for the echo
+    // canceller output.
+    bool LinearFilterUsable() const { return usable_linear_estimate_; }
+
+    // Resets the state of the analyzer.
+    void Reset();
+
+    // Updates the analysis based on new data.
+    void Update(bool active_render,
+                bool transparent_mode,
+                bool saturated_capture,
+                bool consistent_estimate_,
+                const absl::optional<DelayEstimate>& external_delay,
+                bool converged_filter);
+
+   private:
+    bool usable_linear_estimate_ = false;
+    size_t filter_update_blocks_since_reset_ = 0;
+    size_t filter_update_blocks_since_start_ = 0;
+    bool convergence_seen_ = false;
+  } filter_quality_state_;
+
+  // Class containing the legacy functionality for analyzing how well the linear
+  // filter is, and can be expected to perform on the current signals. The
+  // purpose of this is for using to select the echo suppression functionality
+  // as well as the input to the echo suppressor.
+  class LegacyFilteringQualityAnalyzer {
+   public:
+    explicit LegacyFilteringQualityAnalyzer(const EchoCanceller3Config& config);
+
+    // Returns whether the the linear filter is can be used for the echo
+    // canceller output.
+    bool LinearFilterUsable() const { return usable_linear_estimate_; }
+
+    // Resets the state of the analyzer.
+    void Reset();
+
+    // Updates the analysis based on new data.
+    void Update(bool saturated_echo,
+                bool active_render,
+                bool saturated_capture,
+                bool transparent_mode,
+                const absl::optional<DelayEstimate>& external_delay,
+                bool converged_filter,
+                bool diverged_filter);
+
+   private:
+    const bool conservative_initial_phase_;
+    const float required_blocks_for_convergence_;
+    const bool linear_and_stable_echo_path_;
+    bool usable_linear_estimate_ = false;
+    size_t strong_not_saturated_render_blocks_ = 0;
+    size_t non_converged_sequence_size_;
+    size_t diverged_sequence_size_ = 0;
+    size_t active_non_converged_sequence_size_ = 0;
+    bool recent_convergence_during_activity_ = false;
+    bool recent_convergence_ = false;
+  } legacy_filter_quality_state_;
+
+  // Class for detecting whether the echo is to be considered to be
+  // saturated.
+  class SaturationDetector {
+   public:
+    // Returns whether the echo is to be considered saturated.
+    bool SaturatedEcho() const { return saturated_echo_; };
+
+    // Updates the detection decision based on new data.
+    void Update(rtc::ArrayView<const float> x,
+                bool saturated_capture,
+                bool usable_linear_estimate,
+                const SubtractorOutput& subtractor_output,
+                float echo_path_gain);
+
+   private:
+    bool saturated_echo_ = false;
+  } saturation_detector_;
+
+  // Legacy class for detecting whether the echo is to be considered to be
+  // saturated. This is kept as a fallback solution to use instead of the class
+  // SaturationDetector,
+  class LegacySaturationDetector {
+   public:
+    explicit LegacySaturationDetector(const EchoCanceller3Config& config);
+
+    // Returns whether the echo is to be considered saturated.
+    bool SaturatedEcho() const { return saturated_echo_; };
+
+    // Resets the state of the detector.
+    void Reset();
+
+    // Updates the detection decision based on new data.
+    void Update(rtc::ArrayView<const float> x,
+                bool saturated_capture,
+                float echo_path_gain);
+
+   private:
+    const bool echo_can_saturate_;
+    size_t not_saturated_sequence_size_;
+    bool saturated_echo_ = false;
+  } legacy_saturation_detector_;
+
   ErlEstimator erl_estimator_;
   ErleEstimator erle_estimator_;
-  size_t capture_block_counter_ = 0;
-  size_t blocks_since_reset_ = 0;
-  size_t blocks_with_proper_filter_adaptation_ = 0;
+  size_t strong_not_saturated_render_blocks_ = 0;
   size_t blocks_with_active_render_ = 0;
-  bool usable_linear_estimate_ = false;
   bool capture_signal_saturation_ = false;
-  bool echo_saturation_ = false;
-  bool transparent_mode_ = false;
-  bool render_received_ = false;
-  int filter_delay_blocks_ = 0;
-  size_t blocks_since_last_saturation_ = 1000;
 
-  std::vector<float> max_render_;
-  bool filter_has_had_time_to_converge_ = false;
-  bool initial_state_ = true;
-  bool transition_triggered_ = false;
-  const float gain_rampup_increase_;
   SuppressionGainUpperLimiter suppression_gain_limiter_;
   FilterAnalyzer filter_analyzer_;
-  bool use_linear_filter_output_ = false;
-  absl::optional<int> internal_delay_;
-  size_t diverged_blocks_ = 0;
-  bool filter_should_have_converged_ = false;
-  size_t blocks_since_converged_filter_;
-  size_t active_blocks_since_consistent_filter_estimate_;
-  bool converged_filter_seen_ = false;
-  bool consistent_filter_seen_ = false;
-  bool external_delay_seen_ = false;
   absl::optional<DelayEstimate> external_delay_;
-  size_t frames_since_external_delay_change_ = 0;
-  size_t converged_filter_count_ = 0;
-  bool finite_erl_ = false;
-  size_t active_blocks_since_converged_filter_ = 0;
   EchoAudibility echo_audibility_;
   ReverbModelEstimator reverb_model_estimator_;
   SubtractorOutputAnalyzer subtractor_output_analyzer_;
-  RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
 };
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/aec_state_unittest.cc b/modules/audio_processing/aec3/aec_state_unittest.cc
index 50b97f4..a331006 100644
--- a/modules/audio_processing/aec3/aec_state_unittest.cc
+++ b/modules/audio_processing/aec3/aec_state_unittest.cc
@@ -25,7 +25,7 @@
   absl::optional<DelayEstimate> delay_estimate =
       DelayEstimate(DelayEstimate::Quality::kRefined, 10);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   std::array<float, kFftLengthBy2Plus1> E2_main = {};
   std::array<float, kFftLengthBy2Plus1> Y2 = {};
   std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
@@ -56,7 +56,7 @@
   std::fill(x[0].begin(), x[0].end(), 101.f);
   for (int k = 0; k < 3000; ++k) {
     render_delay_buffer->Insert(x);
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, converged_filter_frequency_response,
                  impulse_response, *render_delay_buffer->GetRenderBuffer(),
                  E2_main, Y2, output, y);
@@ -65,7 +65,7 @@
 
   // Verify that linear AEC usability becomes false after an echo path change is
   // reported
-  output.UpdatePowers(y);
+  output.ComputeMetrics(y);
   state.HandleEchoPathChange(EchoPathVariability(
       false, EchoPathVariability::DelayAdjustment::kBufferReadjustment, false));
   state.Update(delay_estimate, converged_filter_frequency_response,
@@ -76,7 +76,7 @@
   // Verify that the active render detection works as intended.
   std::fill(x[0].begin(), x[0].end(), 101.f);
   render_delay_buffer->Insert(x);
-  output.UpdatePowers(y);
+  output.ComputeMetrics(y);
   state.HandleEchoPathChange(EchoPathVariability(
       true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay, false));
   state.Update(delay_estimate, converged_filter_frequency_response,
@@ -86,7 +86,7 @@
 
   for (int k = 0; k < 1000; ++k) {
     render_delay_buffer->Insert(x);
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, converged_filter_frequency_response,
                  impulse_response, *render_delay_buffer->GetRenderBuffer(),
                  E2_main, Y2, output, y);
@@ -110,7 +110,7 @@
 
   Y2.fill(10.f * 10000.f * 10000.f);
   for (size_t k = 0; k < 1000; ++k) {
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, converged_filter_frequency_response,
                  impulse_response, *render_delay_buffer->GetRenderBuffer(),
                  E2_main, Y2, output, y);
@@ -128,21 +128,23 @@
   E2_main.fill(1.f * 10000.f * 10000.f);
   Y2.fill(10.f * E2_main[0]);
   for (size_t k = 0; k < 1000; ++k) {
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, converged_filter_frequency_response,
                  impulse_response, *render_delay_buffer->GetRenderBuffer(),
                  E2_main, Y2, output, y);
   }
   ASSERT_TRUE(state.UsableLinearEstimate());
   {
+    // Note that the render spectrum is built so it does not have energy in the
+    // odd bands but just in the even bands.
     const auto& erle = state.Erle();
     EXPECT_EQ(erle[0], erle[1]);
     constexpr size_t kLowFrequencyLimit = 32;
-    for (size_t k = 1; k < kLowFrequencyLimit; ++k) {
-      EXPECT_NEAR(k % 2 == 0 ? 4.f : 1.f, erle[k], 0.1);
+    for (size_t k = 2; k < kLowFrequencyLimit; k = k + 2) {
+      EXPECT_NEAR(4.f, erle[k], 0.1);
     }
-    for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; ++k) {
-      EXPECT_NEAR(k % 2 == 0 ? 1.5f : 1.f, erle[k], 0.1);
+    for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; k = k + 2) {
+      EXPECT_NEAR(1.5f, erle[k], 0.1);
     }
     EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
   }
@@ -150,7 +152,7 @@
   E2_main.fill(1.f * 10000.f * 10000.f);
   Y2.fill(5.f * E2_main[0]);
   for (size_t k = 0; k < 1000; ++k) {
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, converged_filter_frequency_response,
                  impulse_response, *render_delay_buffer->GetRenderBuffer(),
                  E2_main, Y2, output, y);
@@ -177,7 +179,7 @@
   EchoCanceller3Config config;
   AecState state(config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   absl::optional<DelayEstimate> delay_estimate;
   std::array<float, kFftLengthBy2Plus1> E2_main;
   std::array<float, kFftLengthBy2Plus1> Y2;
@@ -206,7 +208,7 @@
     impulse_response[k * kBlockSize + 1] = 1.f;
 
     state.HandleEchoPathChange(echo_path_variability);
-    output.UpdatePowers(y);
+    output.ComputeMetrics(y);
     state.Update(delay_estimate, frequency_response, impulse_response,
                  *render_delay_buffer->GetRenderBuffer(), E2_main, Y2, output,
                  y);
diff --git a/modules/audio_processing/aec3/block_framer_unittest.cc b/modules/audio_processing/aec3/block_framer_unittest.cc
index 16d3944..9baade9 100644
--- a/modules/audio_processing/aec3/block_framer_unittest.cc
+++ b/modules/audio_processing/aec3/block_framer_unittest.cc
@@ -10,11 +10,11 @@
 
 #include "modules/audio_processing/aec3/block_framer.h"
 
-#include <sstream>
 #include <string>
 #include <vector>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -159,9 +159,9 @@
 #endif
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -229,7 +229,7 @@
 TEST(BlockFramer, WrongNumberOfPreceedingApiCallsForInsertBlock) {
   for (auto rate : {8000, 16000, 32000, 48000}) {
     for (size_t num_calls = 0; num_calls < 4; ++num_calls) {
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Sample rate: " << rate;
       ss << ", Num preceeding InsertBlockAndExtractSubFrame calls: "
          << num_calls;
diff --git a/modules/audio_processing/aec3/block_processor.cc b/modules/audio_processing/aec3/block_processor.cc
index 1bb6958..4f4020e 100644
--- a/modules/audio_processing/aec3/block_processor.cc
+++ b/modules/audio_processing/aec3/block_processor.cc
@@ -104,7 +104,7 @@
     if (!capture_properly_started_) {
       capture_properly_started_ = true;
       render_buffer_->Reset();
-      delay_controller_->Reset();
+      delay_controller_->Reset(true);
     }
   } else {
     // If no render data has yet arrived, do not process the capture signal.
@@ -119,7 +119,7 @@
       render_properly_started_) {
     echo_path_variability.delay_change =
         EchoPathVariability::DelayAdjustment::kBufferFlush;
-    delay_controller_->Reset();
+    delay_controller_->Reset(true);
     RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block  "
                         << capture_call_counter_;
   }
@@ -135,11 +135,11 @@
         estimated_delay_->quality == DelayEstimate::Quality::kRefined) {
       echo_path_variability.delay_change =
           EchoPathVariability::DelayAdjustment::kDelayReset;
-      delay_controller_->Reset();
+      delay_controller_->Reset(true);
       capture_properly_started_ = false;
       render_properly_started_ = false;
 
-      RTC_LOG(LS_WARNING) << "Reset due to render buffer underrrun at block "
+      RTC_LOG(LS_WARNING) << "Reset due to render buffer underrun at block "
                           << capture_call_counter_;
     }
   } else if (render_event_ == RenderDelayBuffer::BufferingEvent::kApiCallSkew) {
@@ -147,7 +147,7 @@
     // echo.
     echo_path_variability.delay_change =
         EchoPathVariability::DelayAdjustment::kDelayReset;
-    delay_controller_->Reset();
+    delay_controller_->Reset(true);
     capture_properly_started_ = false;
     render_properly_started_ = false;
     RTC_LOG(LS_WARNING) << "Reset due to render buffer api skew at block "
@@ -180,7 +180,7 @@
       if (estimated_delay_->quality == DelayEstimate::Quality::kRefined) {
         echo_path_variability.delay_change =
             EchoPathVariability::DelayAdjustment::kDelayReset;
-        delay_controller_->Reset();
+        delay_controller_->Reset(true);
         render_buffer_->Reset();
         capture_properly_started_ = false;
         render_properly_started_ = false;
diff --git a/modules/audio_processing/aec3/block_processor.h b/modules/audio_processing/aec3/block_processor.h
index a3967ea..8793a03 100644
--- a/modules/audio_processing/aec3/block_processor.h
+++ b/modules/audio_processing/aec3/block_processor.h
@@ -23,19 +23,33 @@
 // Class for performing echo cancellation on 64 sample blocks of audio data.
 class BlockProcessor {
  public:
+  // Create a block processor with the legacy render buffering.
   static BlockProcessor* Create(const EchoCanceller3Config& config,
                                 int sample_rate_hz);
+  // Create a block processor with the new render buffering.
+  static BlockProcessor* Create2(const EchoCanceller3Config& config,
+                                 int sample_rate_hz);
   // Only used for testing purposes.
   static BlockProcessor* Create(
       const EchoCanceller3Config& config,
       int sample_rate_hz,
       std::unique_ptr<RenderDelayBuffer> render_buffer);
+  static BlockProcessor* Create2(
+      const EchoCanceller3Config& config,
+      int sample_rate_hz,
+      std::unique_ptr<RenderDelayBuffer> render_buffer);
   static BlockProcessor* Create(
       const EchoCanceller3Config& config,
       int sample_rate_hz,
       std::unique_ptr<RenderDelayBuffer> render_buffer,
       std::unique_ptr<RenderDelayController> delay_controller,
       std::unique_ptr<EchoRemover> echo_remover);
+  static BlockProcessor* Create2(
+      const EchoCanceller3Config& config,
+      int sample_rate_hz,
+      std::unique_ptr<RenderDelayBuffer> render_buffer,
+      std::unique_ptr<RenderDelayController> delay_controller,
+      std::unique_ptr<EchoRemover> echo_remover);
 
   virtual ~BlockProcessor() = default;
 
diff --git a/modules/audio_processing/aec3/block_processor2.cc b/modules/audio_processing/aec3/block_processor2.cc
new file mode 100644
index 0000000..3616427
--- /dev/null
+++ b/modules/audio_processing/aec3/block_processor2.cc
@@ -0,0 +1,254 @@
+/*
+ *  Copyright (c) 2018 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 <stddef.h>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include "absl/types/optional.h"
+#include "api/audio/echo_canceller3_config.h"
+#include "api/audio/echo_control.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/aec3/block_processor.h"
+#include "modules/audio_processing/aec3/block_processor_metrics.h"
+#include "modules/audio_processing/aec3/delay_estimate.h"
+#include "modules/audio_processing/aec3/echo_path_variability.h"
+#include "modules/audio_processing/aec3/echo_remover.h"
+#include "modules/audio_processing/aec3/render_delay_buffer.h"
+#include "modules/audio_processing/aec3/render_delay_controller.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "rtc_base/atomicops.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+
+namespace webrtc {
+namespace {
+
+enum class BlockProcessorApiCall { kCapture, kRender };
+
+class BlockProcessorImpl2 final : public BlockProcessor {
+ public:
+  BlockProcessorImpl2(const EchoCanceller3Config& config,
+                      int sample_rate_hz,
+                      std::unique_ptr<RenderDelayBuffer> render_buffer,
+                      std::unique_ptr<RenderDelayController> delay_controller,
+                      std::unique_ptr<EchoRemover> echo_remover);
+
+  BlockProcessorImpl2() = delete;
+
+  ~BlockProcessorImpl2() override;
+
+  void ProcessCapture(bool echo_path_gain_change,
+                      bool capture_signal_saturation,
+                      std::vector<std::vector<float>>* capture_block) override;
+
+  void BufferRender(const std::vector<std::vector<float>>& block) override;
+
+  void UpdateEchoLeakageStatus(bool leakage_detected) override;
+
+  void GetMetrics(EchoControl::Metrics* metrics) const override;
+
+  void SetAudioBufferDelay(size_t delay_ms) override;
+
+ private:
+  static int instance_count_;
+  std::unique_ptr<ApmDataDumper> data_dumper_;
+  const EchoCanceller3Config config_;
+  bool capture_properly_started_ = false;
+  bool render_properly_started_ = false;
+  const size_t sample_rate_hz_;
+  std::unique_ptr<RenderDelayBuffer> render_buffer_;
+  std::unique_ptr<RenderDelayController> delay_controller_;
+  std::unique_ptr<EchoRemover> echo_remover_;
+  BlockProcessorMetrics metrics_;
+  RenderDelayBuffer::BufferingEvent render_event_;
+  size_t capture_call_counter_ = 0;
+  absl::optional<DelayEstimate> estimated_delay_;
+  absl::optional<int> echo_remover_delay_;
+};
+
+int BlockProcessorImpl2::instance_count_ = 0;
+
+BlockProcessorImpl2::BlockProcessorImpl2(
+    const EchoCanceller3Config& config,
+    int sample_rate_hz,
+    std::unique_ptr<RenderDelayBuffer> render_buffer,
+    std::unique_ptr<RenderDelayController> delay_controller,
+    std::unique_ptr<EchoRemover> echo_remover)
+    : data_dumper_(
+          new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
+      config_(config),
+      sample_rate_hz_(sample_rate_hz),
+      render_buffer_(std::move(render_buffer)),
+      delay_controller_(std::move(delay_controller)),
+      echo_remover_(std::move(echo_remover)),
+      render_event_(RenderDelayBuffer::BufferingEvent::kNone) {
+  RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
+}
+
+BlockProcessorImpl2::~BlockProcessorImpl2() = default;
+
+void BlockProcessorImpl2::ProcessCapture(
+    bool echo_path_gain_change,
+    bool capture_signal_saturation,
+    std::vector<std::vector<float>>* capture_block) {
+  RTC_DCHECK(capture_block);
+  RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size());
+  RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size());
+
+  capture_call_counter_++;
+
+  data_dumper_->DumpRaw("aec3_processblock_call_order",
+                        static_cast<int>(BlockProcessorApiCall::kCapture));
+  data_dumper_->DumpWav("aec3_processblock_capture_input", kBlockSize,
+                        &(*capture_block)[0][0],
+                        LowestBandRate(sample_rate_hz_), 1);
+
+  if (render_properly_started_) {
+    if (!capture_properly_started_) {
+      capture_properly_started_ = true;
+      render_buffer_->Reset();
+      delay_controller_->Reset(true);
+    }
+  } else {
+    // If no render data has yet arrived, do not process the capture signal.
+    return;
+  }
+
+  EchoPathVariability echo_path_variability(
+      echo_path_gain_change, EchoPathVariability::DelayAdjustment::kNone,
+      false);
+
+  if (render_event_ == RenderDelayBuffer::BufferingEvent::kRenderOverrun &&
+      render_properly_started_) {
+    echo_path_variability.delay_change =
+        EchoPathVariability::DelayAdjustment::kBufferFlush;
+    delay_controller_->Reset(true);
+    RTC_LOG(LS_WARNING) << "Reset due to render buffer overrun at block  "
+                        << capture_call_counter_;
+  }
+  render_event_ = RenderDelayBuffer::BufferingEvent::kNone;
+
+  // Update the render buffers with any newly arrived render blocks and prepare
+  // the render buffers for reading the render data corresponding to the current
+  // capture block.
+  RenderDelayBuffer::BufferingEvent buffer_event =
+      render_buffer_->PrepareCaptureProcessing();
+  // Reset the delay controller at render buffer underrun.
+  if (buffer_event == RenderDelayBuffer::BufferingEvent::kRenderUnderrun) {
+    delay_controller_->Reset(false);
+  }
+
+  data_dumper_->DumpWav("aec3_processblock_capture_input2", kBlockSize,
+                        &(*capture_block)[0][0],
+                        LowestBandRate(sample_rate_hz_), 1);
+
+  // Compute and and apply the render delay required to achieve proper signal
+  // alignment.
+  estimated_delay_ = delay_controller_->GetDelay(
+      render_buffer_->GetDownsampledRenderBuffer(), render_buffer_->Delay(),
+      echo_remover_delay_, (*capture_block)[0]);
+
+  if (estimated_delay_) {
+    bool delay_change = render_buffer_->SetDelay(estimated_delay_->delay);
+    if (delay_change) {
+      RTC_LOG(LS_WARNING) << "Delay changed to " << estimated_delay_->delay
+                          << " at block " << capture_call_counter_;
+      echo_path_variability.delay_change =
+          EchoPathVariability::DelayAdjustment::kNewDetectedDelay;
+    }
+  }
+
+  // Remove the echo from the capture signal.
+  echo_remover_->ProcessCapture(
+      echo_path_variability, capture_signal_saturation, estimated_delay_,
+      render_buffer_->GetRenderBuffer(), capture_block);
+
+  // Check to see if a refined delay estimate has been obtained from the echo
+  // remover.
+  echo_remover_delay_ = echo_remover_->Delay();
+
+  // Update the metrics.
+  metrics_.UpdateCapture(false);
+}
+
+void BlockProcessorImpl2::BufferRender(
+    const std::vector<std::vector<float>>& block) {
+  RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block.size());
+  RTC_DCHECK_EQ(kBlockSize, block[0].size());
+  data_dumper_->DumpRaw("aec3_processblock_call_order",
+                        static_cast<int>(BlockProcessorApiCall::kRender));
+  data_dumper_->DumpWav("aec3_processblock_render_input", kBlockSize,
+                        &block[0][0], LowestBandRate(sample_rate_hz_), 1);
+  data_dumper_->DumpWav("aec3_processblock_render_input2", kBlockSize,
+                        &block[0][0], LowestBandRate(sample_rate_hz_), 1);
+
+  render_event_ = render_buffer_->Insert(block);
+
+  metrics_.UpdateRender(render_event_ !=
+                        RenderDelayBuffer::BufferingEvent::kNone);
+
+  render_properly_started_ = true;
+  delay_controller_->LogRenderCall();
+}
+
+void BlockProcessorImpl2::UpdateEchoLeakageStatus(bool leakage_detected) {
+  echo_remover_->UpdateEchoLeakageStatus(leakage_detected);
+}
+
+void BlockProcessorImpl2::GetMetrics(EchoControl::Metrics* metrics) const {
+  echo_remover_->GetMetrics(metrics);
+  const int block_size_ms = sample_rate_hz_ == 8000 ? 8 : 4;
+  absl::optional<size_t> delay = render_buffer_->Delay();
+  metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
+}
+
+void BlockProcessorImpl2::SetAudioBufferDelay(size_t delay_ms) {
+  render_buffer_->SetAudioBufferDelay(delay_ms);
+}
+
+}  // namespace
+
+BlockProcessor* BlockProcessor::Create2(const EchoCanceller3Config& config,
+                                        int sample_rate_hz) {
+  std::unique_ptr<RenderDelayBuffer> render_buffer(
+      RenderDelayBuffer::Create2(config, NumBandsForRate(sample_rate_hz)));
+  std::unique_ptr<RenderDelayController> delay_controller(
+      RenderDelayController::Create2(config, sample_rate_hz));
+  std::unique_ptr<EchoRemover> echo_remover(
+      EchoRemover::Create(config, sample_rate_hz));
+  return Create2(config, sample_rate_hz, std::move(render_buffer),
+                 std::move(delay_controller), std::move(echo_remover));
+}
+
+BlockProcessor* BlockProcessor::Create2(
+    const EchoCanceller3Config& config,
+    int sample_rate_hz,
+    std::unique_ptr<RenderDelayBuffer> render_buffer) {
+  std::unique_ptr<RenderDelayController> delay_controller(
+      RenderDelayController::Create2(config, sample_rate_hz));
+  std::unique_ptr<EchoRemover> echo_remover(
+      EchoRemover::Create(config, sample_rate_hz));
+  return Create2(config, sample_rate_hz, std::move(render_buffer),
+                 std::move(delay_controller), std::move(echo_remover));
+}
+
+BlockProcessor* BlockProcessor::Create2(
+    const EchoCanceller3Config& config,
+    int sample_rate_hz,
+    std::unique_ptr<RenderDelayBuffer> render_buffer,
+    std::unique_ptr<RenderDelayController> delay_controller,
+    std::unique_ptr<EchoRemover> echo_remover) {
+  return new BlockProcessorImpl2(
+      config, sample_rate_hz, std::move(render_buffer),
+      std::move(delay_controller), std::move(echo_remover));
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/aec3/block_processor_unittest.cc b/modules/audio_processing/aec3/block_processor_unittest.cc
index 5906018..8aba5b5 100644
--- a/modules/audio_processing/aec3/block_processor_unittest.cc
+++ b/modules/audio_processing/aec3/block_processor_unittest.cc
@@ -11,7 +11,6 @@
 #include "modules/audio_processing/aec3/block_processor.h"
 
 #include <memory>
-#include <sstream>
 #include <string>
 #include <vector>
 
@@ -22,6 +21,7 @@
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -37,7 +37,7 @@
 // methods are callable.
 void RunBasicSetupAndApiCallTest(int sample_rate_hz, int num_iterations) {
   std::unique_ptr<BlockProcessor> block_processor(
-      BlockProcessor::Create(EchoCanceller3Config(), sample_rate_hz));
+      BlockProcessor::Create2(EchoCanceller3Config(), sample_rate_hz));
   std::vector<std::vector<float>> block(NumBandsForRate(sample_rate_hz),
                                         std::vector<float>(kBlockSize, 1000.f));
 
@@ -51,7 +51,7 @@
 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
 void RunRenderBlockSizeVerificationTest(int sample_rate_hz) {
   std::unique_ptr<BlockProcessor> block_processor(
-      BlockProcessor::Create(EchoCanceller3Config(), sample_rate_hz));
+      BlockProcessor::Create2(EchoCanceller3Config(), sample_rate_hz));
   std::vector<std::vector<float>> block(
       NumBandsForRate(sample_rate_hz), std::vector<float>(kBlockSize - 1, 0.f));
 
@@ -60,7 +60,7 @@
 
 void RunCaptureBlockSizeVerificationTest(int sample_rate_hz) {
   std::unique_ptr<BlockProcessor> block_processor(
-      BlockProcessor::Create(EchoCanceller3Config(), sample_rate_hz));
+      BlockProcessor::Create2(EchoCanceller3Config(), sample_rate_hz));
   std::vector<std::vector<float>> block(
       NumBandsForRate(sample_rate_hz), std::vector<float>(kBlockSize - 1, 0.f));
 
@@ -72,7 +72,7 @@
                                      ? NumBandsForRate(sample_rate_hz) + 1
                                      : 1;
   std::unique_ptr<BlockProcessor> block_processor(
-      BlockProcessor::Create(EchoCanceller3Config(), sample_rate_hz));
+      BlockProcessor::Create2(EchoCanceller3Config(), sample_rate_hz));
   std::vector<std::vector<float>> block(wrong_num_bands,
                                         std::vector<float>(kBlockSize, 0.f));
 
@@ -84,7 +84,7 @@
                                      ? NumBandsForRate(sample_rate_hz) + 1
                                      : 1;
   std::unique_ptr<BlockProcessor> block_processor(
-      BlockProcessor::Create(EchoCanceller3Config(), sample_rate_hz));
+      BlockProcessor::Create2(EchoCanceller3Config(), sample_rate_hz));
   std::vector<std::vector<float>> block(wrong_num_bands,
                                         std::vector<float>(kBlockSize, 0.f));
 
@@ -93,9 +93,9 @@
 #endif
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -124,7 +124,7 @@
     EXPECT_CALL(*render_delay_buffer_mock, Delay())
         .Times(kNumBlocks + 1)
         .WillRepeatedly(Return(0));
-    std::unique_ptr<BlockProcessor> block_processor(BlockProcessor::Create(
+    std::unique_ptr<BlockProcessor> block_processor(BlockProcessor::Create2(
         EchoCanceller3Config(), rate, std::move(render_delay_buffer_mock)));
 
     std::vector<std::vector<float>> render_block(
@@ -173,7 +173,7 @@
     EXPECT_CALL(*echo_remover_mock, UpdateEchoLeakageStatus(_))
         .Times(kNumBlocks);
 
-    std::unique_ptr<BlockProcessor> block_processor(BlockProcessor::Create(
+    std::unique_ptr<BlockProcessor> block_processor(BlockProcessor::Create2(
         EchoCanceller3Config(), rate, std::move(render_delay_buffer_mock),
         std::move(render_delay_controller_mock), std::move(echo_remover_mock)));
 
@@ -239,7 +239,7 @@
 // Verifiers that the verification for null ProcessCapture input works.
 TEST(BlockProcessor, NullProcessCaptureParameter) {
   EXPECT_DEATH(std::unique_ptr<BlockProcessor>(
-                   BlockProcessor::Create(EchoCanceller3Config(), 8000))
+                   BlockProcessor::Create2(EchoCanceller3Config(), 8000))
                    ->ProcessCapture(false, false, nullptr),
                "");
 }
@@ -249,7 +249,7 @@
 // tests on test bots has been fixed.
 TEST(BlockProcessor, DISABLED_WrongSampleRate) {
   EXPECT_DEATH(std::unique_ptr<BlockProcessor>(
-                   BlockProcessor::Create(EchoCanceller3Config(), 8001)),
+                   BlockProcessor::Create2(EchoCanceller3Config(), 8001)),
                "");
 }
 
diff --git a/modules/audio_processing/aec3/comfort_noise_generator.cc b/modules/audio_processing/aec3/comfort_noise_generator.cc
index 55faf4b..a37c67a 100644
--- a/modules/audio_processing/aec3/comfort_noise_generator.cc
+++ b/modules/audio_processing/aec3/comfort_noise_generator.cc
@@ -23,6 +23,8 @@
 #include <numeric>
 
 #include "common_audio/signal_processing/include/signal_processing_library.h"
+#include "modules/audio_processing/aec3/vector_math.h"
+#include "rtc_base/checks.h"
 
 namespace webrtc {
 
@@ -38,64 +40,10 @@
 
 }  // namespace
 
-namespace aec3 {
+namespace {
 
-#if defined(WEBRTC_ARCH_X86_FAMILY)
-
-void EstimateComfortNoise_SSE2(const std::array<float, kFftLengthBy2Plus1>& N2,
-                               uint32_t* seed,
-                               FftData* lower_band_noise,
-                               FftData* upper_band_noise) {
-  FftData* N_low = lower_band_noise;
-  FftData* N_high = upper_band_noise;
-
-  // Compute square root spectrum.
-  std::array<float, kFftLengthBy2Plus1> N;
-  for (size_t k = 0; k < kFftLengthBy2; k += 4) {
-    __m128 v = _mm_loadu_ps(&N2[k]);
-    v = _mm_sqrt_ps(v);
-    _mm_storeu_ps(&N[k], v);
-  }
-
-  N[kFftLengthBy2] = sqrtf(N2[kFftLengthBy2]);
-
-  // Compute the noise level for the upper bands.
-  constexpr float kOneByNumBands = 1.f / (kFftLengthBy2Plus1 / 2 + 1);
-  constexpr int kFftLengthBy2Plus1By2 = kFftLengthBy2Plus1 / 2;
-  const float high_band_noise_level =
-      std::accumulate(N.begin() + kFftLengthBy2Plus1By2, N.end(), 0.f) *
-      kOneByNumBands;
-
-  // Generate complex noise.
-  std::array<int16_t, kFftLengthBy2 - 1> random_values_int;
-  TableRandomValue(random_values_int.data(), random_values_int.size(), seed);
-
-  std::array<float, kFftLengthBy2 - 1> sin;
-  std::array<float, kFftLengthBy2 - 1> cos;
-  constexpr float kScale = 6.28318530717959f / 32768.0f;
-  std::transform(random_values_int.begin(), random_values_int.end(),
-                 sin.begin(), [&](int16_t a) { return -sinf(kScale * a); });
-  std::transform(random_values_int.begin(), random_values_int.end(),
-                 cos.begin(), [&](int16_t a) { return cosf(kScale * a); });
-
-  // Form low-frequency noise via spectral shaping.
-  N_low->re[0] = N_low->re[kFftLengthBy2] = N_high->re[0] =
-      N_high->re[kFftLengthBy2] = 0.f;
-  std::transform(cos.begin(), cos.end(), N.begin() + 1, N_low->re.begin() + 1,
-                 std::multiplies<float>());
-  std::transform(sin.begin(), sin.end(), N.begin() + 1, N_low->im.begin() + 1,
-                 std::multiplies<float>());
-
-  // Form the high-frequency noise via simple levelling.
-  std::transform(cos.begin(), cos.end(), N_high->re.begin() + 1,
-                 [&](float a) { return high_band_noise_level * a; });
-  std::transform(sin.begin(), sin.end(), N_high->im.begin() + 1,
-                 [&](float a) { return high_band_noise_level * a; });
-}
-
-#endif
-
-void EstimateComfortNoise(const std::array<float, kFftLengthBy2Plus1>& N2,
+void GenerateComfortNoise(Aec3Optimization optimization,
+                          const std::array<float, kFftLengthBy2Plus1>& N2,
                           uint32_t* seed,
                           FftData* lower_band_noise,
                           FftData* upper_band_noise) {
@@ -104,8 +52,8 @@
 
   // Compute square root spectrum.
   std::array<float, kFftLengthBy2Plus1> N;
-  std::transform(N2.begin(), N2.end(), N.begin(),
-                 [](float a) { return sqrtf(a); });
+  std::copy(N2.begin(), N2.end(), N.begin());
+  aec3::VectorMath(optimization).Sqrt(N);
 
   // Compute the noise level for the upper bands.
   constexpr float kOneByNumBands = 1.f / (kFftLengthBy2Plus1 / 2 + 1);
@@ -118,13 +66,21 @@
   std::array<int16_t, kFftLengthBy2 - 1> random_values_int;
   TableRandomValue(random_values_int.data(), random_values_int.size(), seed);
 
+  // The analysis and synthesis windowing cause loss of power when
+  // cross-fading the noise where frames are completely uncorrelated
+  // (generated with random phase), hence the factor sqrt(2).
+  // This is not the case for the speech signal where the input is overlapping
+  // (strong correlation).
   std::array<float, kFftLengthBy2 - 1> sin;
   std::array<float, kFftLengthBy2 - 1> cos;
   constexpr float kScale = 6.28318530717959f / 32768.0f;
+  constexpr float kSqrt2 = 1.4142135623f;
   std::transform(random_values_int.begin(), random_values_int.end(),
-                 sin.begin(), [&](int16_t a) { return -sinf(kScale * a); });
+                 sin.begin(),
+                 [&](int16_t a) { return -sinf(kScale * a) * kSqrt2; });
   std::transform(random_values_int.begin(), random_values_int.end(),
-                 cos.begin(), [&](int16_t a) { return cosf(kScale * a); });
+                 cos.begin(),
+                 [&](int16_t a) { return cosf(kScale * a) * kSqrt2; });
 
   // Form low-frequency noise via spectral shaping.
   N_low->re[0] = N_low->re[kFftLengthBy2] = N_high->re[0] =
@@ -141,7 +97,7 @@
                  [&](float a) { return high_band_noise_level * a; });
 }
 
-}  // namespace aec3
+}  // namespace
 
 ComfortNoiseGenerator::ComfortNoiseGenerator(Aec3Optimization optimization)
     : optimization_(optimization),
@@ -205,17 +161,8 @@
   const std::array<float, kFftLengthBy2Plus1>& N2 =
       N2_initial_ ? *N2_initial_ : N2_;
 
-  switch (optimization_) {
-#if defined(WEBRTC_ARCH_X86_FAMILY)
-    case Aec3Optimization::kSse2:
-      aec3::EstimateComfortNoise_SSE2(N2, &seed_, lower_band_noise,
-                                      upper_band_noise);
-      break;
-#endif
-    default:
-      aec3::EstimateComfortNoise(N2, &seed_, lower_band_noise,
-                                 upper_band_noise);
-  }
+  GenerateComfortNoise(optimization_, N2, &seed_, lower_band_noise,
+                       upper_band_noise);
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/comfort_noise_generator_unittest.cc b/modules/audio_processing/aec3/comfort_noise_generator_unittest.cc
index 77a09ed..10ba696 100644
--- a/modules/audio_processing/aec3/comfort_noise_generator_unittest.cc
+++ b/modules/audio_processing/aec3/comfort_noise_generator_unittest.cc
@@ -52,45 +52,6 @@
 
 #endif
 
-#if defined(WEBRTC_ARCH_X86_FAMILY)
-// Verifies that the optimized methods are bitexact to their reference
-// counterparts.
-TEST(ComfortNoiseGenerator, TestOptimizations) {
-  if (WebRtc_GetCPUInfo(kSSE2) != 0) {
-    Random random_generator(42U);
-    uint32_t seed = 42;
-    uint32_t seed_SSE2 = 42;
-    std::array<float, kFftLengthBy2Plus1> N2;
-    FftData lower_band_noise;
-    FftData upper_band_noise;
-    FftData lower_band_noise_SSE2;
-    FftData upper_band_noise_SSE2;
-    for (int k = 0; k < 10; ++k) {
-      for (size_t j = 0; j < N2.size(); ++j) {
-        N2[j] = random_generator.Rand<float>() * 1000.f;
-      }
-
-      EstimateComfortNoise(N2, &seed, &lower_band_noise, &upper_band_noise);
-      EstimateComfortNoise_SSE2(N2, &seed_SSE2, &lower_band_noise_SSE2,
-                                &upper_band_noise_SSE2);
-      for (size_t j = 0; j < lower_band_noise.re.size(); ++j) {
-        EXPECT_NEAR(lower_band_noise.re[j], lower_band_noise_SSE2.re[j],
-                    0.00001f);
-        EXPECT_NEAR(upper_band_noise.re[j], upper_band_noise_SSE2.re[j],
-                    0.00001f);
-      }
-      for (size_t j = 1; j < lower_band_noise.re.size() - 1; ++j) {
-        EXPECT_NEAR(lower_band_noise.im[j], lower_band_noise_SSE2.im[j],
-                    0.00001f);
-        EXPECT_NEAR(upper_band_noise.im[j], upper_band_noise_SSE2.im[j],
-                    0.00001f);
-      }
-    }
-  }
-}
-
-#endif
-
 TEST(ComfortNoiseGenerator, CorrectLevel) {
   ComfortNoiseGenerator cng(DetectOptimization());
   AecState aec_state(EchoCanceller3Config{});
@@ -113,8 +74,8 @@
   for (int k = 0; k < 10000; ++k) {
     cng.Compute(aec_state, N2, &n_lower, &n_upper);
   }
-  EXPECT_NEAR(N2[0], Power(n_lower), N2[0] / 10.f);
-  EXPECT_NEAR(N2[0], Power(n_upper), N2[0] / 10.f);
+  EXPECT_NEAR(2.f * N2[0], Power(n_lower), N2[0] / 10.f);
+  EXPECT_NEAR(2.f * N2[0], Power(n_upper), N2[0] / 10.f);
 }
 
 }  // namespace aec3
diff --git a/modules/audio_processing/aec3/decimator_unittest.cc b/modules/audio_processing/aec3/decimator_unittest.cc
index e9ab21b..08dc428 100644
--- a/modules/audio_processing/aec3/decimator_unittest.cc
+++ b/modules/audio_processing/aec3/decimator_unittest.cc
@@ -14,11 +14,11 @@
 #include <algorithm>
 #include <array>
 #include <numeric>
-#include <sstream>
 #include <string>
 #include <vector>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -26,9 +26,9 @@
 namespace {
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 constexpr size_t kDownSamplingFactors[] = {2, 4, 8};
diff --git a/modules/audio_processing/aec3/echo_audibility.cc b/modules/audio_processing/aec3/echo_audibility.cc
index 567873b..fa123de 100644
--- a/modules/audio_processing/aec3/echo_audibility.cc
+++ b/modules/audio_processing/aec3/echo_audibility.cc
@@ -20,7 +20,8 @@
 
 namespace webrtc {
 
-EchoAudibility::EchoAudibility() {
+EchoAudibility::EchoAudibility(bool use_render_stationarity_at_init)
+    : use_render_stationarity_at_init_(use_render_stationarity_at_init) {
   Reset();
 }
 
@@ -34,7 +35,7 @@
                              render_buffer.GetBlockBuffer(),
                              external_delay_seen);
 
-  if (external_delay_seen) {
+  if (external_delay_seen || use_render_stationarity_at_init_) {
     UpdateRenderStationarityFlags(render_buffer, delay_blocks, reverb_decay);
   }
 }
diff --git a/modules/audio_processing/aec3/echo_audibility.h b/modules/audio_processing/aec3/echo_audibility.h
index 4650fa5..3b252f3 100644
--- a/modules/audio_processing/aec3/echo_audibility.h
+++ b/modules/audio_processing/aec3/echo_audibility.h
@@ -31,7 +31,7 @@
 
 class EchoAudibility {
  public:
-  EchoAudibility();
+  explicit EchoAudibility(bool use_render_stationarity_at_init);
   ~EchoAudibility();
 
   // Feed new render data to the echo audibility estimator.
@@ -39,11 +39,12 @@
               int delay_blocks,
               bool external_delay_seen,
               float reverb_decay);
-
   // Get the residual echo scaling.
-  void GetResidualEchoScaling(rtc::ArrayView<float> residual_scaling) const {
+  void GetResidualEchoScaling(bool filter_has_had_time_to_converge,
+                              rtc::ArrayView<float> residual_scaling) const {
     for (size_t band = 0; band < residual_scaling.size(); ++band) {
-      if (render_stationarity_.IsBandStationary(band)) {
+      if (render_stationarity_.IsBandStationary(band) &&
+          filter_has_had_time_to_converge) {
         residual_scaling[band] = 0.f;
       } else {
         residual_scaling[band] = 1.0f;
@@ -78,6 +79,7 @@
   absl::optional<int> render_spectrum_write_prev_;
   int render_block_write_prev_;
   bool non_zero_render_seen_;
+  const bool use_render_stationarity_at_init_;
   StationarityEstimator render_stationarity_;
   RTC_DISALLOW_COPY_AND_ASSIGN(EchoAudibility);
 };
diff --git a/modules/audio_processing/aec3/echo_canceller3.cc b/modules/audio_processing/aec3/echo_canceller3.cc
index e5219c7..857587a 100644
--- a/modules/audio_processing/aec3/echo_canceller3.cc
+++ b/modules/audio_processing/aec3/echo_canceller3.cc
@@ -62,6 +62,38 @@
   return field_trial::IsEnabled("WebRTC-Aec3EnableUnityNonZeroRampupGain");
 }
 
+bool EnableLongReverb() {
+  return field_trial::IsEnabled("WebRTC-Aec3ShortReverbKillSwitch");
+}
+
+bool EnableNewFilterParams() {
+  return !field_trial::IsEnabled("WebRTC-Aec3NewFilterParamsKillSwitch");
+}
+
+bool EnableLegacyDominantNearend() {
+  return field_trial::IsEnabled("WebRTC-Aec3EnableLegacyDominantNearend");
+}
+
+bool UseLegacyNormalSuppressorTuning() {
+  return field_trial::IsEnabled("WebRTC-Aec3UseLegacyNormalSuppressorTuning");
+}
+
+bool ActivateStationarityProperties() {
+  return field_trial::IsEnabled("WebRTC-Aec3UseStationarityProperties");
+}
+
+bool ActivateStationarityPropertiesAtInit() {
+  return field_trial::IsEnabled("WebRTC-Aec3UseStationarityPropertiesAtInit");
+}
+
+bool UseEarlyDelayDetection() {
+  return !field_trial::IsEnabled("WebRTC-Aec3EarlyDelayDetectionKillSwitch");
+}
+
+bool EnableNewRenderBuffering() {
+  return !field_trial::IsEnabled("WebRTC-Aec3NewRenderBufferingKillSwitch");
+}
+
 // Method for adjusting config parameter dependencies..
 EchoCanceller3Config AdjustConfig(const EchoCanceller3Config& config) {
   EchoCanceller3Config adjusted_cfg = config;
@@ -71,42 +103,18 @@
     adjusted_cfg.ep_strength.default_len = 0.f;
   }
 
-  // Use customized parameters when the system has clock-drift.
-  if (config.echo_removal_control.has_clock_drift) {
-    RTC_LOG(LS_WARNING)
-        << "Customizing parameters to work well for the clock-drift case.";
-    if (config.ep_strength.bounded_erl) {
-      adjusted_cfg.ep_strength.default_len = 0.85f;
-      adjusted_cfg.ep_strength.lf = 0.01f;
-      adjusted_cfg.ep_strength.mf = 0.01f;
-      adjusted_cfg.ep_strength.hf = 0.01f;
-      adjusted_cfg.echo_model.render_pre_window_size = 1;
-      adjusted_cfg.echo_model.render_post_window_size = 1;
-      adjusted_cfg.echo_model.nonlinear_hold = 3;
-      adjusted_cfg.echo_model.nonlinear_release = 0.001f;
-    } else {
-      adjusted_cfg.ep_strength.bounded_erl = true;
-      adjusted_cfg.delay.down_sampling_factor = 2;
-      adjusted_cfg.ep_strength.default_len = 0.8f;
-      adjusted_cfg.ep_strength.lf = 0.01f;
-      adjusted_cfg.ep_strength.mf = 0.01f;
-      adjusted_cfg.ep_strength.hf = 0.01f;
-      adjusted_cfg.filter.main = {30, 0.1f, 0.8f, 0.001f, 20075344.f};
-      adjusted_cfg.filter.shadow = {30, 0.7f, 20075344.f};
-      adjusted_cfg.filter.main_initial = {30, 0.1f, 1.5f, 0.001f, 20075344.f};
-      adjusted_cfg.filter.shadow_initial = {30, 0.9f, 20075344.f};
-      adjusted_cfg.echo_model.render_pre_window_size = 2;
-      adjusted_cfg.echo_model.render_post_window_size = 2;
-      adjusted_cfg.echo_model.nonlinear_hold = 3;
-      adjusted_cfg.echo_model.nonlinear_release = 0.6f;
-    }
-  }
-
   if (UseShortDelayEstimatorWindow()) {
     adjusted_cfg.delay.num_filters =
         std::min(adjusted_cfg.delay.num_filters, static_cast<size_t>(5));
   }
 
+  bool use_new_render_buffering =
+      EnableNewRenderBuffering() && config.buffering.use_new_render_buffering;
+  // Old render buffering needs one more filter to cover the same delay.
+  if (!use_new_render_buffering) {
+    adjusted_cfg.delay.num_filters += 1;
+  }
+
   if (EnableReverbBasedOnRender() == false) {
     adjusted_cfg.ep_strength.reverb_based_on_render = false;
   }
@@ -135,6 +143,13 @@
     adjusted_cfg.filter.main.error_floor = 0.001f;
   }
 
+  if (!EnableNewFilterParams()) {
+    adjusted_cfg.filter.main.leakage_diverged = 0.01f;
+    adjusted_cfg.filter.main.error_floor = 0.1f;
+    adjusted_cfg.filter.main.error_ceil = 1E10f;
+    adjusted_cfg.filter.main_initial.error_ceil = 1E10f;
+  }
+
   if (EnableUnityInitialRampupGain() &&
       adjusted_cfg.echo_removal_control.gain_rampup.initial_gain ==
           default_cfg.echo_removal_control.gain_rampup.initial_gain) {
@@ -147,6 +162,42 @@
     adjusted_cfg.echo_removal_control.gain_rampup.first_non_zero_gain = 1.f;
   }
 
+  if (EnableLongReverb()) {
+    adjusted_cfg.ep_strength.default_len = 0.88f;
+  }
+
+  if (EnableLegacyDominantNearend()) {
+    adjusted_cfg.suppressor.nearend_tuning =
+        EchoCanceller3Config::Suppressor::Tuning(
+            EchoCanceller3Config::Suppressor::MaskingThresholds(.2f, .3f, .3f),
+            EchoCanceller3Config::Suppressor::MaskingThresholds(.07f, .1f, .3f),
+            2.0f, 0.25f);
+  }
+
+  if (UseLegacyNormalSuppressorTuning()) {
+    adjusted_cfg.suppressor.normal_tuning =
+        EchoCanceller3Config::Suppressor::Tuning(
+            EchoCanceller3Config::Suppressor::MaskingThresholds(.2f, .3f, .3f),
+            EchoCanceller3Config::Suppressor::MaskingThresholds(.07f, .1f, .3f),
+            2.0f, 0.25f);
+
+    adjusted_cfg.suppressor.dominant_nearend_detection.enr_threshold = 10.f;
+    adjusted_cfg.suppressor.dominant_nearend_detection.snr_threshold = 10.f;
+    adjusted_cfg.suppressor.dominant_nearend_detection.hold_duration = 25;
+  }
+
+  if (ActivateStationarityProperties()) {
+    adjusted_cfg.echo_audibility.use_stationary_properties = true;
+  }
+
+  if (ActivateStationarityPropertiesAtInit()) {
+    adjusted_cfg.echo_audibility.use_stationarity_properties_at_init = true;
+  }
+
+  if (!UseEarlyDelayDetection()) {
+    adjusted_cfg.delay.delay_selection_thresholds = {25, 25};
+  }
+
   return adjusted_cfg;
 }
 
@@ -330,12 +381,16 @@
 EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
                                int sample_rate_hz,
                                bool use_highpass_filter)
-    : EchoCanceller3(
-          AdjustConfig(config),
-          sample_rate_hz,
-          use_highpass_filter,
-          std::unique_ptr<BlockProcessor>(
-              BlockProcessor::Create(AdjustConfig(config), sample_rate_hz))) {}
+    : EchoCanceller3(AdjustConfig(config),
+                     sample_rate_hz,
+                     use_highpass_filter,
+                     std::unique_ptr<BlockProcessor>(
+                         EnableNewRenderBuffering() &&
+                                 config.buffering.use_new_render_buffering
+                             ? BlockProcessor::Create2(AdjustConfig(config),
+                                                       sample_rate_hz)
+                             : BlockProcessor::Create(AdjustConfig(config),
+                                                      sample_rate_hz))) {}
 EchoCanceller3::EchoCanceller3(const EchoCanceller3Config& config,
                                int sample_rate_hz,
                                bool use_highpass_filter,
diff --git a/modules/audio_processing/aec3/echo_canceller3_unittest.cc b/modules/audio_processing/aec3/echo_canceller3_unittest.cc
index f652642..3f1e059 100644
--- a/modules/audio_processing/aec3/echo_canceller3_unittest.cc
+++ b/modules/audio_processing/aec3/echo_canceller3_unittest.cc
@@ -12,7 +12,6 @@
 
 #include <deque>
 #include <memory>
-#include <sstream>
 #include <string>
 #include <utility>
 #include <vector>
@@ -22,6 +21,7 @@
 #include "modules/audio_processing/aec3/frame_blocker.h"
 #include "modules/audio_processing/aec3/mock/mock_block_processor.h"
 #include "modules/audio_processing/audio_buffer.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
 
@@ -604,15 +604,15 @@
 };
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 std::string ProduceDebugText(int sample_rate_hz, int variant) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz << ", variant: " << variant;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
diff --git a/modules/audio_processing/aec3/echo_path_delay_estimator.cc b/modules/audio_processing/aec3/echo_path_delay_estimator.cc
index 4cae277..891f15e 100644
--- a/modules/audio_processing/aec3/echo_path_delay_estimator.cc
+++ b/modules/audio_processing/aec3/echo_path_delay_estimator.cc
@@ -47,22 +47,20 @@
           kMatchedFilterAlignmentShiftSizeSubBlocks,
           GetDownSamplingFactor(config) == 8
               ? config.render_levels.poor_excitation_render_limit_ds8
-              : config.render_levels.poor_excitation_render_limit),
+              : config.render_levels.poor_excitation_render_limit,
+          config.delay.delay_estimate_smoothing,
+          config.delay.delay_candidate_detection_threshold),
       matched_filter_lag_aggregator_(data_dumper_,
-                                     matched_filter_.GetMaxFilterLag()) {
+                                     matched_filter_.GetMaxFilterLag(),
+                                     config.delay.delay_selection_thresholds) {
   RTC_DCHECK(data_dumper);
   RTC_DCHECK(down_sampling_factor_ > 0);
 }
 
 EchoPathDelayEstimator::~EchoPathDelayEstimator() = default;
 
-void EchoPathDelayEstimator::Reset(bool soft_reset) {
-  if (!soft_reset) {
-    matched_filter_lag_aggregator_.Reset();
-  }
-  matched_filter_.Reset();
-  old_aggregated_lag_ = absl::nullopt;
-  consistent_estimate_counter_ = 0;
+void EchoPathDelayEstimator::Reset(bool reset_delay_confidence) {
+  Reset(true, reset_delay_confidence);
 }
 
 absl::optional<DelayEstimate> EchoPathDelayEstimator::EstimateDelay(
@@ -109,10 +107,20 @@
   old_aggregated_lag_ = aggregated_matched_filter_lag;
   constexpr size_t kNumBlocksPerSecondBy2 = kNumBlocksPerSecond / 2;
   if (consistent_estimate_counter_ > kNumBlocksPerSecondBy2) {
-    Reset(true);
+    Reset(false, false);
   }
 
   return aggregated_matched_filter_lag;
 }
 
+void EchoPathDelayEstimator::Reset(bool reset_lag_aggregator,
+                                   bool reset_delay_confidence) {
+  if (reset_lag_aggregator) {
+    matched_filter_lag_aggregator_.Reset(reset_delay_confidence);
+  }
+  matched_filter_.Reset();
+  old_aggregated_lag_ = absl::nullopt;
+  consistent_estimate_counter_ = 0;
+}
+
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/echo_path_delay_estimator.h b/modules/audio_processing/aec3/echo_path_delay_estimator.h
index cea9abc..55d82be 100644
--- a/modules/audio_processing/aec3/echo_path_delay_estimator.h
+++ b/modules/audio_processing/aec3/echo_path_delay_estimator.h
@@ -33,9 +33,9 @@
                          const EchoCanceller3Config& config);
   ~EchoPathDelayEstimator();
 
-  // Resets the estimation. If the soft-reset is specified, only  the matched
-  // filters are reset.
-  void Reset(bool soft_reset);
+  // Resets the estimation. If the delay confidence is reset, the reset behavior
+  // is as if the call is restarted.
+  void Reset(bool reset_delay_confidence);
 
   // Produce a delay estimate if such is avaliable.
   absl::optional<DelayEstimate> EstimateDelay(
@@ -58,6 +58,9 @@
   absl::optional<DelayEstimate> old_aggregated_lag_;
   size_t consistent_estimate_counter_ = 0;
 
+  // Internal reset method with more granularity.
+  void Reset(bool reset_lag_aggregator, bool reset_delay_confidence);
+
   RTC_DISALLOW_COPY_AND_ASSIGN(EchoPathDelayEstimator);
 };
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc b/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
index 0e237a5..a4e3133 100644
--- a/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
+++ b/modules/audio_processing/aec3/echo_path_delay_estimator_unittest.cc
@@ -11,7 +11,6 @@
 #include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
 
 #include <algorithm>
-#include <sstream>
 #include <string>
 
 #include "api/audio/echo_canceller3_config.h"
@@ -20,16 +19,17 @@
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
 namespace {
 
 std::string ProduceDebugText(size_t delay, size_t down_sampling_factor) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Delay: " << delay;
   ss << ", Down sampling factor: " << down_sampling_factor;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -39,7 +39,7 @@
   ApmDataDumper data_dumper(0);
   EchoCanceller3Config config;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   EchoPathDelayEstimator estimator(&data_dumper, config);
   std::vector<std::vector<float>> render(3, std::vector<float>(kBlockSize));
   std::vector<float> capture(kBlockSize);
@@ -64,12 +64,9 @@
     config.delay.num_filters = 10;
     for (size_t delay_samples : {30, 64, 150, 200, 800, 4000}) {
       SCOPED_TRACE(ProduceDebugText(delay_samples, down_sampling_factor));
-
-      config.delay.api_call_jitter_blocks = 5;
       std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-          RenderDelayBuffer::Create(config, 3));
-      DelayBuffer<float> signal_delay_buffer(
-          delay_samples + 2 * config.delay.api_call_jitter_blocks * 64);
+          RenderDelayBuffer::Create2(config, 3));
+      DelayBuffer<float> signal_delay_buffer(delay_samples);
       EchoPathDelayEstimator estimator(&data_dumper, config);
 
       absl::optional<DelayEstimate> estimated_delay_samples;
@@ -97,9 +94,7 @@
         // domain.
         size_t delay_ds = delay_samples / down_sampling_factor;
         size_t estimated_delay_ds =
-            (estimated_delay_samples->delay -
-             (config.delay.api_call_jitter_blocks + 1) * 64) /
-            down_sampling_factor;
+            estimated_delay_samples->delay / down_sampling_factor;
         EXPECT_NEAR(delay_ds, estimated_delay_ds, 1);
       } else {
         ADD_FAILURE();
@@ -118,7 +113,7 @@
   ApmDataDumper data_dumper(0);
   EchoPathDelayEstimator estimator(&data_dumper, config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+      RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
   for (size_t k = 0; k < 100; ++k) {
     RandomizeSampleVector(&random_generator, render[0]);
     for (auto& render_k : render[0]) {
@@ -142,7 +137,7 @@
   EchoCanceller3Config config;
   EchoPathDelayEstimator estimator(&data_dumper, config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   std::vector<float> capture(kBlockSize);
   EXPECT_DEATH(estimator.EstimateDelay(
                    render_delay_buffer->GetDownsampledRenderBuffer(), capture),
@@ -157,7 +152,7 @@
   EchoCanceller3Config config;
   EchoPathDelayEstimator estimator(&data_dumper, config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   std::vector<float> capture(std::vector<float>(kBlockSize - 1));
   EXPECT_DEATH(estimator.EstimateDelay(
                    render_delay_buffer->GetDownsampledRenderBuffer(), capture),
diff --git a/modules/audio_processing/aec3/echo_remover.cc b/modules/audio_processing/aec3/echo_remover.cc
index 9b6677e..063c476 100644
--- a/modules/audio_processing/aec3/echo_remover.cc
+++ b/modules/audio_processing/aec3/echo_remover.cc
@@ -48,6 +48,10 @@
       "WebRTC-Aec3SmoothSignalTransitionsKillSwitch");
 }
 
+bool EnableBoundedNearend() {
+  return !field_trial::IsEnabled("WebRTC-Aec3BoundedNearendKillSwitch");
+}
+
 void LinearEchoPower(const FftData& E,
                      const FftData& Y,
                      std::array<float, kFftLengthBy2Plus1>* S2) {
@@ -62,15 +66,15 @@
                       rtc::ArrayView<const float> to,
                       rtc::ArrayView<float> out) {
   constexpr size_t kTransitionSize = 30;
-  constexpr float kOneByTransitionSize = 1.f / kTransitionSize;
+  constexpr float kOneByTransitionSizePlusOne = 1.f / (kTransitionSize + 1);
 
   RTC_DCHECK_EQ(from.size(), to.size());
   RTC_DCHECK_EQ(from.size(), out.size());
   RTC_DCHECK_LE(kTransitionSize, out.size());
 
   for (size_t k = 0; k < kTransitionSize; ++k) {
-    out[k] = k * kOneByTransitionSize * to[k];
-    out[k] += (kTransitionSize - k) * kOneByTransitionSize * to[k];
+    float a = (k + 1) * kOneByTransitionSizePlusOne;
+    out[k] = a * to[k] + (1.f - a) * from[k];
   }
 
   std::copy(to.begin() + kTransitionSize, to.end(),
@@ -132,6 +136,7 @@
   const int sample_rate_hz_;
   const bool use_shadow_filter_output_;
   const bool use_smooth_signal_transitions_;
+  const bool enable_bounded_nearend_;
   Subtractor subtractor_;
   SuppressionGain suppression_gain_;
   ComfortNoiseGenerator cng_;
@@ -166,10 +171,11 @@
           UseShadowFilterOutput() &&
           config_.filter.enable_shadow_filter_output_usage),
       use_smooth_signal_transitions_(UseSmoothSignalTransitions()),
+      enable_bounded_nearend_(EnableBoundedNearend()),
       subtractor_(config, data_dumper_.get(), optimization_),
       suppression_gain_(config_, optimization_, sample_rate_hz),
       cng_(optimization_),
-      suppression_filter_(sample_rate_hz_),
+      suppression_filter_(optimization_, sample_rate_hz_),
       render_signal_analyzer_(config_),
       residual_echo_estimator_(config_),
       aec_state_(config_) {
@@ -185,7 +191,7 @@
   // Echo return loss (ERL) is inverted to go from gain to attenuation.
   metrics->echo_return_loss = -10.0 * log10(aec_state_.ErlTimeDomain());
   metrics->echo_return_loss_enhancement =
-      Log2TodB(aec_state_.ErleTimeDomainLog2());
+      Log2TodB(aec_state_.FullBandErleLog2());
 }
 
 void EchoRemoverImpl::ProcessCapture(
@@ -311,9 +317,18 @@
   // Compute and apply the suppression gain.
   const auto& echo_spectrum =
       aec_state_.UsableLinearEstimate() ? S2_linear : R2;
-  suppression_gain_.GetGain(E2, echo_spectrum, R2, cng_.NoiseSpectrum(), E, Y,
-                            render_signal_analyzer_, aec_state_, x,
-                            &high_bands_gain, &G);
+
+  std::array<float, kFftLengthBy2Plus1> E2_bounded;
+  if (enable_bounded_nearend_) {
+    std::transform(E2.begin(), E2.end(), Y2.begin(), E2_bounded.begin(),
+                   [](float a, float b) { return std::min(a, b); });
+  } else {
+    std::copy(E2.begin(), E2.end(), E2_bounded.begin());
+  }
+
+  suppression_gain_.GetGain(E2, E2_bounded, echo_spectrum, R2,
+                            cng_.NoiseSpectrum(), E, Y, render_signal_analyzer_,
+                            aec_state_, x, &high_bands_gain, &G);
 
   suppression_filter_.ApplyGain(comfort_noise, high_band_comfort_noise, G,
                                 high_bands_gain, Y_fft, y);
diff --git a/modules/audio_processing/aec3/echo_remover_metrics.cc b/modules/audio_processing/aec3/echo_remover_metrics.cc
index 8592a93..a04026b 100644
--- a/modules/audio_processing/aec3/echo_remover_metrics.cc
+++ b/modules/audio_processing/aec3/echo_remover_metrics.cc
@@ -67,7 +67,7 @@
     aec3::UpdateDbMetric(aec_state.Erl(), &erl_);
     erl_time_domain_.UpdateInstant(aec_state.ErlTimeDomain());
     aec3::UpdateDbMetric(aec_state.Erle(), &erle_);
-    erle_time_domain_.UpdateInstant(aec_state.ErleTimeDomainLog2());
+    erle_time_domain_.UpdateInstant(aec_state.FullBandErleLog2());
     aec3::UpdateDbMetric(comfort_noise_spectrum, &comfort_noise_);
     aec3::UpdateDbMetric(suppressor_gain, &suppressor_gain_);
     active_render_count_ += (aec_state.ActiveRender() ? 1 : 0);
diff --git a/modules/audio_processing/aec3/echo_remover_unittest.cc b/modules/audio_processing/aec3/echo_remover_unittest.cc
index 04a2727..8bf76c4 100644
--- a/modules/audio_processing/aec3/echo_remover_unittest.cc
+++ b/modules/audio_processing/aec3/echo_remover_unittest.cc
@@ -13,7 +13,6 @@
 #include <algorithm>
 #include <memory>
 #include <numeric>
-#include <sstream>
 #include <string>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
@@ -22,21 +21,22 @@
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
 namespace {
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 std::string ProduceDebugText(int sample_rate_hz, int delay) {
-  std::ostringstream ss(ProduceDebugText(sample_rate_hz));
+  rtc::StringBuilder ss(ProduceDebugText(sample_rate_hz));
   ss << ", Delay: " << delay;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -48,7 +48,7 @@
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<EchoRemover> remover(
         EchoRemover::Create(EchoCanceller3Config(), rate));
-    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
+    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create2(
         EchoCanceller3Config(), NumBandsForRate(rate)));
 
     std::vector<std::vector<float>> render(NumBandsForRate(rate),
@@ -89,7 +89,7 @@
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<EchoRemover> remover(
         EchoRemover::Create(EchoCanceller3Config(), rate));
-    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
+    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create2(
         EchoCanceller3Config(), NumBandsForRate(rate)));
     std::vector<std::vector<float>> capture(
         NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f));
@@ -111,7 +111,7 @@
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<EchoRemover> remover(
         EchoRemover::Create(EchoCanceller3Config(), rate));
-    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
+    std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create2(
         EchoCanceller3Config(), NumBandsForRate(rate)));
     std::vector<std::vector<float>> capture(
         NumBandsForRate(rate == 48000 ? 16000 : rate + 16000),
@@ -131,7 +131,7 @@
   std::unique_ptr<EchoRemover> remover(
       EchoRemover::Create(EchoCanceller3Config(), 8000));
   std::unique_ptr<RenderDelayBuffer> render_buffer(
-      RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+      RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
   EchoPathVariability echo_path_variability(
       false, EchoPathVariability::DelayAdjustment::kNone, false);
   EXPECT_DEATH(
@@ -161,7 +161,7 @@
       config.delay.min_echo_path_delay_blocks = 0;
       std::unique_ptr<EchoRemover> remover(EchoRemover::Create(config, rate));
       std::unique_ptr<RenderDelayBuffer> render_buffer(
-          RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+          RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
       render_buffer->SetDelay(delay_samples / kBlockSize);
 
       std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size());
diff --git a/modules/audio_processing/aec3/erl_estimator.cc b/modules/audio_processing/aec3/erl_estimator.cc
index b2849db..2da9cd8 100644
--- a/modules/audio_processing/aec3/erl_estimator.cc
+++ b/modules/audio_processing/aec3/erl_estimator.cc
@@ -22,7 +22,8 @@
 
 }  // namespace
 
-ErlEstimator::ErlEstimator() {
+ErlEstimator::ErlEstimator(size_t startup_phase_length_blocks_)
+    : startup_phase_length_blocks__(startup_phase_length_blocks_) {
   erl_.fill(kMaxErl);
   hold_counters_.fill(0);
   erl_time_domain_ = kMaxErl;
@@ -31,7 +32,12 @@
 
 ErlEstimator::~ErlEstimator() = default;
 
-void ErlEstimator::Update(rtc::ArrayView<const float> render_spectrum,
+void ErlEstimator::Reset() {
+  blocks_since_reset_ = 0;
+}
+
+void ErlEstimator::Update(bool converged_filter,
+                          rtc::ArrayView<const float> render_spectrum,
                           rtc::ArrayView<const float> capture_spectrum) {
   RTC_DCHECK_EQ(kFftLengthBy2Plus1, render_spectrum.size());
   RTC_DCHECK_EQ(kFftLengthBy2Plus1, capture_spectrum.size());
@@ -41,6 +47,11 @@
   // Corresponds to WGN of power -46 dBFS.
   constexpr float kX2Min = 44015068.0f;
 
+  if (++blocks_since_reset_ < startup_phase_length_blocks__ ||
+      !converged_filter) {
+    return;
+  }
+
   // Update the estimates in a maximum statistics manner.
   for (size_t k = 1; k < kFftLengthBy2; ++k) {
     if (X2[k] > kX2Min) {
diff --git a/modules/audio_processing/aec3/erl_estimator.h b/modules/audio_processing/aec3/erl_estimator.h
index 215c22e..dacd546 100644
--- a/modules/audio_processing/aec3/erl_estimator.h
+++ b/modules/audio_processing/aec3/erl_estimator.h
@@ -22,11 +22,15 @@
 // Estimates the echo return loss based on the signal spectra.
 class ErlEstimator {
  public:
-  ErlEstimator();
+  explicit ErlEstimator(size_t startup_phase_length_blocks_);
   ~ErlEstimator();
 
+  // Resets the ERL estimation.
+  void Reset();
+
   // Updates the ERL estimate.
-  void Update(rtc::ArrayView<const float> render_spectrum,
+  void Update(bool converged_filter,
+              rtc::ArrayView<const float> render_spectrum,
               rtc::ArrayView<const float> capture_spectrum);
 
   // Returns the most recent ERL estimate.
@@ -34,11 +38,12 @@
   float ErlTimeDomain() const { return erl_time_domain_; }
 
  private:
+  const size_t startup_phase_length_blocks__;
   std::array<float, kFftLengthBy2Plus1> erl_;
   std::array<int, kFftLengthBy2Minus1> hold_counters_;
   float erl_time_domain_;
   int hold_counter_time_domain_;
-
+  size_t blocks_since_reset_ = 0;
   RTC_DISALLOW_COPY_AND_ASSIGN(ErlEstimator);
 };
 
diff --git a/modules/audio_processing/aec3/erl_estimator_unittest.cc b/modules/audio_processing/aec3/erl_estimator_unittest.cc
index a406581..1b965d0 100644
--- a/modules/audio_processing/aec3/erl_estimator_unittest.cc
+++ b/modules/audio_processing/aec3/erl_estimator_unittest.cc
@@ -31,13 +31,13 @@
   std::array<float, kFftLengthBy2Plus1> X2;
   std::array<float, kFftLengthBy2Plus1> Y2;
 
-  ErlEstimator estimator;
+  ErlEstimator estimator(0);
 
   // Verifies that the ERL estimate is properly reduced to lower values.
   X2.fill(500 * 1000.f * 1000.f);
   Y2.fill(10 * X2[0]);
   for (size_t k = 0; k < 200; ++k) {
-    estimator.Update(X2, Y2);
+    estimator.Update(true, X2, Y2);
   }
   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
 
@@ -45,18 +45,18 @@
   // increases.
   Y2.fill(10000 * X2[0]);
   for (size_t k = 0; k < 998; ++k) {
-    estimator.Update(X2, Y2);
+    estimator.Update(true, X2, Y2);
   }
   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 10.f);
 
   // Verifies that the rate of increase is 3 dB.
-  estimator.Update(X2, Y2);
+  estimator.Update(true, X2, Y2);
   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 20.f);
 
   // Verifies that the maximum ERL is achieved when there are no low RLE
   // estimates.
   for (size_t k = 0; k < 1000; ++k) {
-    estimator.Update(X2, Y2);
+    estimator.Update(true, X2, Y2);
   }
   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
 
@@ -64,7 +64,7 @@
   X2.fill(1000.f * 1000.f);
   Y2.fill(10 * X2[0]);
   for (size_t k = 0; k < 200; ++k) {
-    estimator.Update(X2, Y2);
+    estimator.Update(true, X2, Y2);
   }
   VerifyErl(estimator.Erl(), estimator.ErlTimeDomain(), 1000.f);
 }
diff --git a/modules/audio_processing/aec3/erle_estimator.cc b/modules/audio_processing/aec3/erle_estimator.cc
index b02245c..711085f 100644
--- a/modules/audio_processing/aec3/erle_estimator.cc
+++ b/modules/audio_processing/aec3/erle_estimator.cc
@@ -10,163 +10,30 @@
 
 #include "modules/audio_processing/aec3/erle_estimator.h"
 
-#include <algorithm>
-#include <numeric>
-
-#include "absl/types/optional.h"
+#include "api/array_view.h"
 #include "modules/audio_processing/aec3/aec3_common.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
-#include "rtc_base/numerics/safe_minmax.h"
 
 namespace webrtc {
 
-namespace {
-constexpr int kPointsToAccumulate = 6;
-constexpr float kEpsilon = 1e-3f;
-}  // namespace
-
-ErleEstimator::ErleEstimator(float min_erle,
+ErleEstimator::ErleEstimator(size_t startup_phase_length_blocks_,
+                             float min_erle,
                              float max_erle_lf,
                              float max_erle_hf)
-    : min_erle_(min_erle),
-      min_erle_log2_(FastApproxLog2f(min_erle_ + kEpsilon)),
-      max_erle_lf_(max_erle_lf),
-      max_erle_lf_log2(FastApproxLog2f(max_erle_lf_ + kEpsilon)),
-      max_erle_hf_(max_erle_hf),
-      erle_freq_inst_(kPointsToAccumulate),
-      erle_time_inst_(kPointsToAccumulate) {
-  Reset();
+    : startup_phase_length_blocks__(startup_phase_length_blocks_),
+      fullband_erle_estimator_(min_erle, max_erle_lf),
+      subband_erle_estimator_(min_erle, max_erle_lf, max_erle_hf) {
+  Reset(true);
 }
 
 ErleEstimator::~ErleEstimator() = default;
 
-void ErleEstimator::Reset() {
-  erle_time_inst_.Reset();
-  erle_.fill(min_erle_);
-  erle_onsets_.fill(min_erle_);
-  hold_counters_.fill(0);
-  coming_onset_.fill(true);
-  erle_time_domain_log2_ = min_erle_log2_;
-  hold_counter_time_domain_ = 0;
-}
-
-ErleEstimator::ErleTimeInstantaneous::ErleTimeInstantaneous(
-    int points_to_accumulate)
-    : points_to_accumulate_(points_to_accumulate) {
-  Reset();
-}
-ErleEstimator::ErleTimeInstantaneous::~ErleTimeInstantaneous() = default;
-
-bool ErleEstimator::ErleTimeInstantaneous::Update(const float Y2_sum,
-                                                  const float E2_sum) {
-  bool ret = false;
-  E2_acum_ += E2_sum;
-  Y2_acum_ += Y2_sum;
-  num_points_++;
-  if (num_points_ == points_to_accumulate_) {
-    if (E2_acum_ > 0.f) {
-      ret = true;
-      erle_log2_ = FastApproxLog2f(Y2_acum_ / E2_acum_ + kEpsilon);
-    }
-    num_points_ = 0;
-    E2_acum_ = 0.f;
-    Y2_acum_ = 0.f;
+void ErleEstimator::Reset(bool delay_change) {
+  fullband_erle_estimator_.Reset();
+  subband_erle_estimator_.Reset();
+  if (delay_change) {
+    blocks_since_reset_ = 0;
   }
-
-  if (ret) {
-    UpdateMaxMin();
-    UpdateQualityEstimate();
-  }
-  return ret;
-}
-
-void ErleEstimator::ErleTimeInstantaneous::Reset() {
-  ResetAccumulators();
-  max_erle_log2_ = -10.f;  // -30 dB.
-  min_erle_log2_ = 33.f;   // 100 dB.
-  inst_quality_estimate_ = 0.f;
-}
-
-void ErleEstimator::ErleTimeInstantaneous::ResetAccumulators() {
-  erle_log2_ = absl::nullopt;
-  inst_quality_estimate_ = 0.f;
-  num_points_ = 0;
-  E2_acum_ = 0.f;
-  Y2_acum_ = 0.f;
-}
-
-void ErleEstimator::ErleTimeInstantaneous::Dump(
-    const std::unique_ptr<ApmDataDumper>& data_dumper) {
-  data_dumper->DumpRaw("aec3_erle_time_inst_log2",
-                       erle_log2_ ? *erle_log2_ : -10.f);
-  data_dumper->DumpRaw(
-      "aec3_erle_time_quality",
-      GetInstQualityEstimate() ? GetInstQualityEstimate().value() : 0.f);
-  data_dumper->DumpRaw("aec3_erle_time_max_log2", max_erle_log2_);
-  data_dumper->DumpRaw("aec3_erle_time_min_log2", min_erle_log2_);
-}
-
-void ErleEstimator::ErleTimeInstantaneous::UpdateMaxMin() {
-  RTC_DCHECK(erle_log2_);
-  if (erle_log2_.value() > max_erle_log2_) {
-    max_erle_log2_ = erle_log2_.value();
-  } else {
-    max_erle_log2_ -= 0.0004;  // Forget factor, approx 1dB every 3 sec.
-  }
-
-  if (erle_log2_.value() < min_erle_log2_) {
-    min_erle_log2_ = erle_log2_.value();
-  } else {
-    min_erle_log2_ += 0.0004;  // Forget factor, approx 1dB every 3 sec.
-  }
-}
-
-void ErleEstimator::ErleTimeInstantaneous::UpdateQualityEstimate() {
-  const float alpha = 0.07f;
-  float quality_estimate = 0.f;
-  RTC_DCHECK(erle_log2_);
-  if (max_erle_log2_ > min_erle_log2_) {
-    quality_estimate = (erle_log2_.value() - min_erle_log2_) /
-                       (max_erle_log2_ - min_erle_log2_);
-  }
-  if (quality_estimate > inst_quality_estimate_) {
-    inst_quality_estimate_ = quality_estimate;
-  } else {
-    inst_quality_estimate_ +=
-        alpha * (quality_estimate - inst_quality_estimate_);
-  }
-}
-
-ErleEstimator::ErleFreqInstantaneous::ErleFreqInstantaneous(
-    int points_to_accumulate)
-    : points_to_accumulate_(points_to_accumulate) {
-  Reset();
-}
-
-ErleEstimator::ErleFreqInstantaneous::~ErleFreqInstantaneous() = default;
-
-absl::optional<float>
-ErleEstimator::ErleFreqInstantaneous::Update(float Y2, float E2, size_t band) {
-  absl::optional<float> ret = absl::nullopt;
-  RTC_DCHECK_LT(band, kFftLengthBy2Plus1);
-  Y2_acum_[band] += Y2;
-  E2_acum_[band] += E2;
-  if (++num_points_[band] == points_to_accumulate_) {
-    if (E2_acum_[band]) {
-      ret = Y2_acum_[band] / E2_acum_[band];
-    }
-    num_points_[band] = 0;
-    Y2_acum_[band] = 0.f;
-    E2_acum_[band] = 0.f;
-  }
-
-  return ret;
-}
-
-void ErleEstimator::ErleFreqInstantaneous::Reset() {
-  Y2_acum_.fill(0.f);
-  E2_acum_.fill(0.f);
-  num_points_.fill(0);
 }
 
 void ErleEstimator::Update(rtc::ArrayView<const float> render_spectrum,
@@ -181,104 +48,18 @@
   const auto& Y2 = capture_spectrum;
   const auto& E2 = subtractor_spectrum;
 
-  // Corresponds of WGN of power -46 dBFS.
-  constexpr float kX2Min = 44015068.0f;
-
-  constexpr int kErleHold = 100;
-  constexpr int kBlocksForOnsetDetection = kErleHold + 150;
-
-  auto erle_band_update = [](float erle_band, float new_erle, float alpha_inc,
-                             float alpha_dec, float min_erle, float max_erle) {
-    float alpha = new_erle > erle_band ? alpha_inc : alpha_dec;
-    float erle_band_out = erle_band;
-    erle_band_out = erle_band + alpha * (new_erle - erle_band);
-    erle_band_out = rtc::SafeClamp(erle_band_out, min_erle, max_erle);
-    return erle_band_out;
-  };
-
-  // Update the estimates in a clamped minimum statistics manner.
-  auto erle_update = [&](size_t start, size_t stop, float max_erle,
-                         bool onset_detection) {
-    for (size_t k = start; k < stop; ++k) {
-      if (X2[k] > kX2Min) {
-        absl::optional<float> new_erle =
-            erle_freq_inst_.Update(Y2[k], E2[k], k);
-        if (new_erle) {
-          if (onset_detection) {
-            if (coming_onset_[k]) {
-              coming_onset_[k] = false;
-              erle_onsets_[k] =
-                  erle_band_update(erle_onsets_[k], new_erle.value(), 0.15f,
-                                   0.3f, min_erle_, max_erle);
-            }
-            hold_counters_[k] = kBlocksForOnsetDetection;
-          }
-          erle_[k] = erle_band_update(erle_[k], new_erle.value(), 0.05f, 0.1f,
-                                      min_erle_, max_erle);
-        }
-      }
-    }
-  };
-
-  if (converged_filter) {
-    // Note that the use of the converged_filter flag already imposed
-    // a minimum of the erle that can be estimated as that flag would
-    // be false if the filter is performing poorly.
-    constexpr size_t kFftLengthBy4 = kFftLengthBy2 / 2;
-    erle_update(1, kFftLengthBy4, max_erle_lf_, onset_detection);
-    erle_update(kFftLengthBy4, kFftLengthBy2, max_erle_hf_, onset_detection);
+  if (++blocks_since_reset_ < startup_phase_length_blocks__) {
+    return;
   }
 
-  if (onset_detection) {
-    for (size_t k = 1; k < kFftLengthBy2; ++k) {
-      hold_counters_[k]--;
-      if (hold_counters_[k] <= (kBlocksForOnsetDetection - kErleHold)) {
-        if (erle_[k] > erle_onsets_[k]) {
-          erle_[k] = std::max(erle_onsets_[k], 0.97f * erle_[k]);
-          RTC_DCHECK_LE(min_erle_, erle_[k]);
-        }
-        if (hold_counters_[k] <= 0) {
-          coming_onset_[k] = true;
-          hold_counters_[k] = 0;
-        }
-      }
-    }
-  }
-
-  erle_[0] = erle_[1];
-  erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1];
-
-  if (converged_filter) {
-    // Compute ERLE over all frequency bins.
-    const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
-    if (X2_sum > kX2Min * X2.size()) {
-      const float Y2_sum = std::accumulate(Y2.begin(), Y2.end(), 0.0f);
-      const float E2_sum = std::accumulate(E2.begin(), E2.end(), 0.0f);
-      if (erle_time_inst_.Update(Y2_sum, E2_sum)) {
-        hold_counter_time_domain_ = kErleHold;
-        erle_time_domain_log2_ +=
-            0.1f * ((erle_time_inst_.GetInstErle_log2().value()) -
-                    erle_time_domain_log2_);
-        erle_time_domain_log2_ = rtc::SafeClamp(
-            erle_time_domain_log2_, min_erle_log2_, max_erle_lf_log2);
-      }
-    }
-  }
-  --hold_counter_time_domain_;
-  if (hold_counter_time_domain_ <= 0) {
-    erle_time_domain_log2_ =
-        std::max(min_erle_log2_, erle_time_domain_log2_ - 0.044f);
-  }
-  if (hold_counter_time_domain_ == 0) {
-    erle_time_inst_.ResetAccumulators();
-  }
+  subband_erle_estimator_.Update(X2, Y2, E2, converged_filter, onset_detection);
+  fullband_erle_estimator_.Update(X2, Y2, E2, converged_filter);
 }
 
-void ErleEstimator::Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) {
-  data_dumper->DumpRaw("aec3_erle", Erle());
-  data_dumper->DumpRaw("aec3_erle_onset", ErleOnsets());
-  data_dumper->DumpRaw("aec3_erle_time_domain_log2", ErleTimeDomainLog2());
-  erle_time_inst_.Dump(data_dumper);
+void ErleEstimator::Dump(
+    const std::unique_ptr<ApmDataDumper>& data_dumper) const {
+  fullband_erle_estimator_.Dump(data_dumper);
+  subband_erle_estimator_.Dump(data_dumper);
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/erle_estimator.h b/modules/audio_processing/aec3/erle_estimator.h
index 8160dbe..8ca605f 100644
--- a/modules/audio_processing/aec3/erle_estimator.h
+++ b/modules/audio_processing/aec3/erle_estimator.h
@@ -12,112 +12,67 @@
 #define MODULES_AUDIO_PROCESSING_AEC3_ERLE_ESTIMATOR_H_
 
 #include <array>
+#include <memory>
 
 #include "absl/types/optional.h"
 #include "api/array_view.h"
 #include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/aec3/fullband_erle_estimator.h"
+#include "modules/audio_processing/aec3/subband_erle_estimator.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
-#include "rtc_base/constructormagic.h"
 
 namespace webrtc {
 
-// Estimates the echo return loss enhancement based on the signal spectra.
+// Estimates the echo return loss enhancement. One estimate is done per subband
+// and another one is done using the aggreation of energy over all the subbands.
 class ErleEstimator {
  public:
-  ErleEstimator(float min_erle, float max_erle_lf, float max_erle_hf);
+  ErleEstimator(size_t startup_phase_length_blocks_,
+                float min_erle,
+                float max_erle_lf,
+                float max_erle_hf);
   ~ErleEstimator();
 
-  // Reset the ERLE estimator.
-  void Reset();
+  // Resets the fullband ERLE estimator and the subbands ERLE estimators.
+  void Reset(bool delay_change);
 
-  // Updates the ERLE estimate.
+  // Updates the ERLE estimates.
   void Update(rtc::ArrayView<const float> render_spectrum,
               rtc::ArrayView<const float> capture_spectrum,
               rtc::ArrayView<const float> subtractor_spectrum,
               bool converged_filter,
               bool onset_detection);
 
-  // Returns the most recent ERLE estimate.
-  const std::array<float, kFftLengthBy2Plus1>& Erle() const { return erle_; }
-  // Returns the ERLE that is estimated during onsets. Use for logging/testing.
+  // Returns the most recent subband ERLE estimates.
+  const std::array<float, kFftLengthBy2Plus1>& Erle() const {
+    return subband_erle_estimator_.Erle();
+  }
+  // Returns the subband ERLE that are estimated during onsets. Used
+  // for logging/testing.
   const std::array<float, kFftLengthBy2Plus1>& ErleOnsets() const {
-    return erle_onsets_;
+    return subband_erle_estimator_.ErleOnsets();
   }
-  float ErleTimeDomainLog2() const { return erle_time_domain_log2_; }
 
+  // Returns the fullband ERLE estimate.
+  float FullbandErleLog2() const {
+    return fullband_erle_estimator_.FullbandErleLog2();
+  }
+
+  // Returns an estimation of the current linear filter quality based on the
+  // current and past fullband ERLE estimates. The returned value is a float
+  // between 0 and 1 where 1 indicates that, at this current time instant, the
+  // linear filter is reaching its maximum subtraction performance.
   absl::optional<float> GetInstLinearQualityEstimate() const {
-    return erle_time_inst_.GetInstQualityEstimate();
+    return fullband_erle_estimator_.GetInstLinearQualityEstimate();
   }
 
-  void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper);
-
-  class ErleTimeInstantaneous {
-   public:
-    ErleTimeInstantaneous(int points_to_accumulate);
-    ~ErleTimeInstantaneous();
-    // Update the estimator with a new point, returns true
-    // if the instantaneous erle was updated due to having enough
-    // points for performing the estimate.
-    bool Update(const float Y2_sum, const float E2_sum);
-    // Reset all the members of the class.
-    void Reset();
-    // Reset the members realated with an instantaneous estimate.
-    void ResetAccumulators();
-    // Returns the instantaneous ERLE in log2 units.
-    absl::optional<float> GetInstErle_log2() const { return erle_log2_; }
-    // Get an indication between 0 and 1 of the performance of the linear filter
-    // for the current time instant.
-    absl::optional<float> GetInstQualityEstimate() const {
-      return erle_log2_ ? absl::optional<float>(inst_quality_estimate_)
-                        : absl::nullopt;
-    }
-    void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper);
-
-   private:
-    void UpdateMaxMin();
-    void UpdateQualityEstimate();
-    absl::optional<float> erle_log2_;
-    float inst_quality_estimate_;
-    float max_erle_log2_;
-    float min_erle_log2_;
-    float Y2_acum_;
-    float E2_acum_;
-    int num_points_;
-    const int points_to_accumulate_;
-  };
-
-  class ErleFreqInstantaneous {
-   public:
-    ErleFreqInstantaneous(int points_to_accumulate);
-    ~ErleFreqInstantaneous();
-    // Updates the ERLE for a band with a new block. Returns absl::nullopt
-    // if not enough points were accuulated for doing the estimation.
-    absl::optional<float> Update(float Y2, float E2, size_t band);
-    // Reset all the member of the class.
-    void Reset();
-
-   private:
-    std::array<float, kFftLengthBy2Plus1> Y2_acum_;
-    std::array<float, kFftLengthBy2Plus1> E2_acum_;
-    std::array<int, kFftLengthBy2Plus1> num_points_;
-    const int points_to_accumulate_;
-  };
+  void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
 
  private:
-  std::array<float, kFftLengthBy2Plus1> erle_;
-  std::array<float, kFftLengthBy2Plus1> erle_onsets_;
-  std::array<bool, kFftLengthBy2Plus1> coming_onset_;
-  std::array<int, kFftLengthBy2Plus1> hold_counters_;
-  int hold_counter_time_domain_;
-  float erle_time_domain_log2_;
-  const float min_erle_;
-  const float min_erle_log2_;
-  const float max_erle_lf_;
-  const float max_erle_lf_log2;
-  const float max_erle_hf_;
-  ErleFreqInstantaneous erle_freq_inst_;
-  ErleTimeInstantaneous erle_time_inst_;
-  RTC_DISALLOW_COPY_AND_ASSIGN(ErleEstimator);
+  const size_t startup_phase_length_blocks__;
+  FullBandErleEstimator fullband_erle_estimator_;
+  SubbandErleEstimator subband_erle_estimator_;
+  size_t blocks_since_reset_ = 0;
 };
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/erle_estimator_unittest.cc b/modules/audio_processing/aec3/erle_estimator_unittest.cc
index 1687568..2cb050a 100644
--- a/modules/audio_processing/aec3/erle_estimator_unittest.cc
+++ b/modules/audio_processing/aec3/erle_estimator_unittest.cc
@@ -68,7 +68,7 @@
   std::array<float, kFftLengthBy2Plus1> E2;
   std::array<float, kFftLengthBy2Plus1> Y2;
 
-  ErleEstimator estimator(kMinErle, kMaxErleLf, kMaxErleHf);
+  ErleEstimator estimator(0, kMinErle, kMaxErleLf, kMaxErleHf);
 
   // Verifies that the ERLE estimate is properly increased to higher values.
   FormFarendFrame(&X2, &E2, &Y2, kTrueErle);
@@ -76,7 +76,7 @@
   for (size_t k = 0; k < 200; ++k) {
     estimator.Update(X2, Y2, E2, true, true);
   }
-  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.ErleTimeDomainLog2()),
+  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.FullbandErleLog2()),
              kMaxErleLf, kMaxErleHf);
 
   FormNearendFrame(&X2, &E2, &Y2);
@@ -85,7 +85,7 @@
   for (size_t k = 0; k < 50; ++k) {
     estimator.Update(X2, Y2, E2, true, true);
   }
-  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.ErleTimeDomainLog2()),
+  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.FullbandErleLog2()),
              kMaxErleLf, kMaxErleHf);
 }
 
@@ -94,7 +94,7 @@
   std::array<float, kFftLengthBy2Plus1> E2;
   std::array<float, kFftLengthBy2Plus1> Y2;
 
-  ErleEstimator estimator(kMinErle, kMaxErleLf, kMaxErleHf);
+  ErleEstimator estimator(0, kMinErle, kMaxErleLf, kMaxErleHf);
 
   for (size_t burst = 0; burst < 20; ++burst) {
     FormFarendFrame(&X2, &E2, &Y2, kTrueErleOnsets);
@@ -116,24 +116,7 @@
     estimator.Update(X2, Y2, E2, true, true);
   }
   // Verifies that during ne activity, Erle converges to the Erle for onsets.
-  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.ErleTimeDomainLog2()),
-             kMinErle, kMinErle);
-}
-
-TEST(ErleEstimator, VerifyNoErleUpdateDuringLowActivity) {
-  std::array<float, kFftLengthBy2Plus1> X2;
-  std::array<float, kFftLengthBy2Plus1> E2;
-  std::array<float, kFftLengthBy2Plus1> Y2;
-  ErleEstimator estimator(kMinErle, kMaxErleLf, kMaxErleHf);
-
-  // Verifies that the ERLE estimate is is not updated for low-level render
-  // signals.
-  X2.fill(1000.f * 1000.f);
-  Y2.fill(10 * E2[0]);
-  for (size_t k = 0; k < 200; ++k) {
-    estimator.Update(X2, Y2, E2, true, true);
-  }
-  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.ErleTimeDomainLog2()),
+  VerifyErle(estimator.Erle(), std::pow(2.f, estimator.FullbandErleLog2()),
              kMinErle, kMinErle);
 }
 
diff --git a/modules/audio_processing/aec3/frame_blocker_unittest.cc b/modules/audio_processing/aec3/frame_blocker_unittest.cc
index 6e73d4b..3ec74cc 100644
--- a/modules/audio_processing/aec3/frame_blocker_unittest.cc
+++ b/modules/audio_processing/aec3/frame_blocker_unittest.cc
@@ -10,12 +10,12 @@
 
 #include "modules/audio_processing/aec3/frame_blocker.h"
 
-#include <sstream>
 #include <string>
 #include <vector>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
 #include "modules/audio_processing/aec3/block_framer.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -228,9 +228,9 @@
 #endif
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -300,7 +300,7 @@
 TEST(FrameBlocker, WrongNumberOfPreceedingApiCallsForExtractBlock) {
   for (auto rate : {8000, 16000, 32000, 48000}) {
     for (size_t num_calls = 0; num_calls < 4; ++num_calls) {
-      std::ostringstream ss;
+      rtc::StringBuilder ss;
       ss << "Sample rate: " << rate;
       ss << ", Num preceeding InsertSubFrameAndExtractBlock calls: "
          << num_calls;
diff --git a/modules/audio_processing/aec3/fullband_erle_estimator.cc b/modules/audio_processing/aec3/fullband_erle_estimator.cc
new file mode 100644
index 0000000..db9be7c
--- /dev/null
+++ b/modules/audio_processing/aec3/fullband_erle_estimator.cc
@@ -0,0 +1,168 @@
+/*
+ *  Copyright (c) 2018 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 "modules/audio_processing/aec3/fullband_erle_estimator.h"
+
+#include <algorithm>
+#include <memory>
+#include <numeric>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "rtc_base/numerics/safe_minmax.h"
+
+namespace webrtc {
+
+namespace {
+constexpr float kEpsilon = 1e-3f;
+constexpr float kX2BandEnergyThreshold = 44015068.0f;
+constexpr int kErleHold = 100;
+constexpr int kPointsToAccumulate = 6;
+}  // namespace
+
+FullBandErleEstimator::FullBandErleEstimator(float min_erle, float max_erle_lf)
+    : min_erle_log2_(FastApproxLog2f(min_erle + kEpsilon)),
+      max_erle_lf_log2(FastApproxLog2f(max_erle_lf + kEpsilon)) {
+  Reset();
+}
+
+FullBandErleEstimator::~FullBandErleEstimator() = default;
+
+void FullBandErleEstimator::Reset() {
+  instantaneous_erle_.Reset();
+  erle_time_domain_log2_ = min_erle_log2_;
+  hold_counter_time_domain_ = 0;
+}
+
+void FullBandErleEstimator::Update(rtc::ArrayView<const float> X2,
+                                   rtc::ArrayView<const float> Y2,
+                                   rtc::ArrayView<const float> E2,
+                                   bool converged_filter) {
+  if (converged_filter) {
+    // Computes the fullband ERLE.
+    const float X2_sum = std::accumulate(X2.begin(), X2.end(), 0.0f);
+    if (X2_sum > kX2BandEnergyThreshold * X2.size()) {
+      const float Y2_sum = std::accumulate(Y2.begin(), Y2.end(), 0.0f);
+      const float E2_sum = std::accumulate(E2.begin(), E2.end(), 0.0f);
+      if (instantaneous_erle_.Update(Y2_sum, E2_sum)) {
+        hold_counter_time_domain_ = kErleHold;
+        erle_time_domain_log2_ +=
+            0.1f * ((instantaneous_erle_.GetInstErleLog2().value()) -
+                    erle_time_domain_log2_);
+        erle_time_domain_log2_ = rtc::SafeClamp(
+            erle_time_domain_log2_, min_erle_log2_, max_erle_lf_log2);
+      }
+    }
+  }
+  --hold_counter_time_domain_;
+  if (hold_counter_time_domain_ <= 0) {
+    erle_time_domain_log2_ =
+        std::max(min_erle_log2_, erle_time_domain_log2_ - 0.044f);
+  }
+  if (hold_counter_time_domain_ == 0) {
+    instantaneous_erle_.ResetAccumulators();
+  }
+}
+
+void FullBandErleEstimator::Dump(
+    const std::unique_ptr<ApmDataDumper>& data_dumper) const {
+  data_dumper->DumpRaw("aec3_fullband_erle_log2", FullbandErleLog2());
+  instantaneous_erle_.Dump(data_dumper);
+}
+
+FullBandErleEstimator::ErleInstantaneous::ErleInstantaneous() {
+  Reset();
+}
+
+FullBandErleEstimator::ErleInstantaneous::~ErleInstantaneous() = default;
+
+bool FullBandErleEstimator::ErleInstantaneous::Update(const float Y2_sum,
+                                                      const float E2_sum) {
+  bool update_estimates = false;
+  E2_acum_ += E2_sum;
+  Y2_acum_ += Y2_sum;
+  num_points_++;
+  if (num_points_ == kPointsToAccumulate) {
+    if (E2_acum_ > 0.f) {
+      update_estimates = true;
+      erle_log2_ = FastApproxLog2f(Y2_acum_ / E2_acum_ + kEpsilon);
+    }
+    num_points_ = 0;
+    E2_acum_ = 0.f;
+    Y2_acum_ = 0.f;
+  }
+
+  if (update_estimates) {
+    UpdateMaxMin();
+    UpdateQualityEstimate();
+  }
+  return update_estimates;
+}
+
+void FullBandErleEstimator::ErleInstantaneous::Reset() {
+  ResetAccumulators();
+  max_erle_log2_ = -10.f;  // -30 dB.
+  min_erle_log2_ = 33.f;   // 100 dB.
+  inst_quality_estimate_ = 0.f;
+}
+
+void FullBandErleEstimator::ErleInstantaneous::ResetAccumulators() {
+  erle_log2_ = absl::nullopt;
+  inst_quality_estimate_ = 0.f;
+  num_points_ = 0;
+  E2_acum_ = 0.f;
+  Y2_acum_ = 0.f;
+}
+
+void FullBandErleEstimator::ErleInstantaneous::Dump(
+    const std::unique_ptr<ApmDataDumper>& data_dumper) const {
+  data_dumper->DumpRaw("aec3_fullband_erle_inst_log2",
+                       erle_log2_ ? *erle_log2_ : -10.f);
+  data_dumper->DumpRaw(
+      "aec3_erle_instantaneous_quality",
+      GetQualityEstimate() ? GetQualityEstimate().value() : 0.f);
+  data_dumper->DumpRaw("aec3_fullband_erle_max_log2", max_erle_log2_);
+  data_dumper->DumpRaw("aec3_fullband_erle_min_log2", min_erle_log2_);
+}
+
+void FullBandErleEstimator::ErleInstantaneous::UpdateMaxMin() {
+  RTC_DCHECK(erle_log2_);
+  if (erle_log2_.value() > max_erle_log2_) {
+    max_erle_log2_ = erle_log2_.value();
+  } else {
+    max_erle_log2_ -= 0.0004;  // Forget factor, approx 1dB every 3 sec.
+  }
+
+  if (erle_log2_.value() < min_erle_log2_) {
+    min_erle_log2_ = erle_log2_.value();
+  } else {
+    min_erle_log2_ += 0.0004;  // Forget factor, approx 1dB every 3 sec.
+  }
+}
+
+void FullBandErleEstimator::ErleInstantaneous::UpdateQualityEstimate() {
+  const float alpha = 0.07f;
+  float quality_estimate = 0.f;
+  RTC_DCHECK(erle_log2_);
+  if (max_erle_log2_ > min_erle_log2_) {
+    quality_estimate = (erle_log2_.value() - min_erle_log2_) /
+                       (max_erle_log2_ - min_erle_log2_);
+  }
+  if (quality_estimate > inst_quality_estimate_) {
+    inst_quality_estimate_ = quality_estimate;
+  } else {
+    inst_quality_estimate_ +=
+        alpha * (quality_estimate - inst_quality_estimate_);
+  }
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/aec3/fullband_erle_estimator.h b/modules/audio_processing/aec3/fullband_erle_estimator.h
new file mode 100644
index 0000000..175db55
--- /dev/null
+++ b/modules/audio_processing/aec3/fullband_erle_estimator.h
@@ -0,0 +1,93 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_
+#define MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_
+
+#include <memory>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+
+namespace webrtc {
+
+// Estimates the echo return loss enhancement using the energy of all the
+// freuquency bands.
+class FullBandErleEstimator {
+ public:
+  FullBandErleEstimator(float min_erle, float max_erle_lf);
+  ~FullBandErleEstimator();
+  // Resets the ERLE estimator.
+  void Reset();
+
+  // Updates the ERLE estimator.
+  void Update(rtc::ArrayView<const float> X2,
+              rtc::ArrayView<const float> Y2,
+              rtc::ArrayView<const float> E2,
+              bool converged_filter);
+
+  // Returns the fullband ERLE estimates in log2 units.
+  float FullbandErleLog2() const { return erle_time_domain_log2_; }
+
+  // Returns an estimation of the current linear filter quality. It returns a
+  // float number between 0 and 1 mapping 1 to the highest possible quality.
+  absl::optional<float> GetInstLinearQualityEstimate() const {
+    return instantaneous_erle_.GetQualityEstimate();
+  }
+
+  void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
+
+ private:
+  class ErleInstantaneous {
+   public:
+    ErleInstantaneous();
+    ~ErleInstantaneous();
+
+    // Updates the estimator with a new point, returns true
+    // if the instantaneous ERLE was updated due to having enough
+    // points for performing the estimate.
+    bool Update(const float Y2_sum, const float E2_sum);
+    // Resets the instantaneous ERLE estimator to its initial state.
+    void Reset();
+    // Resets the members related with an instantaneous estimate.
+    void ResetAccumulators();
+    // Returns the instantaneous ERLE in log2 units.
+    absl::optional<float> GetInstErleLog2() const { return erle_log2_; }
+    // Gets an indication between 0 and 1 of the performance of the linear
+    // filter for the current time instant.
+    absl::optional<float> GetQualityEstimate() const {
+      return erle_log2_ ? absl::optional<float>(inst_quality_estimate_)
+                        : absl::nullopt;
+    }
+    void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
+
+   private:
+    void UpdateMaxMin();
+    void UpdateQualityEstimate();
+    absl::optional<float> erle_log2_;
+    float inst_quality_estimate_;
+    float max_erle_log2_;
+    float min_erle_log2_;
+    float Y2_acum_;
+    float E2_acum_;
+    int num_points_;
+  };
+
+  int hold_counter_time_domain_;
+  float erle_time_domain_log2_;
+  const float min_erle_log2_;
+  const float max_erle_lf_log2;
+  ErleInstantaneous instantaneous_erle_;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_PROCESSING_AEC3_FULLBAND_ERLE_ESTIMATOR_H_
diff --git a/modules/audio_processing/aec3/main_filter_update_gain.cc b/modules/audio_processing/aec3/main_filter_update_gain.cc
index 4f48cd4..ec5347e 100644
--- a/modules/audio_processing/aec3/main_filter_update_gain.cc
+++ b/modules/audio_processing/aec3/main_filter_update_gain.cc
@@ -127,7 +127,10 @@
                  H_error_increase.begin(), std::multiplies<float>());
   std::transform(H_error_.begin(), H_error_.end(), H_error_increase.begin(),
                  H_error_.begin(), [&](float a, float b) {
-                   return std::max(a + b, current_config_.error_floor);
+                   float error = a + b;
+                   error = std::max(error, current_config_.error_floor);
+                   error = std::min(error, current_config_.error_ceil);
+                   return error;
                  });
 
   data_dumper_->DumpRaw("aec3_main_gain_H_error", H_error_);
@@ -153,6 +156,9 @@
       current_config_.error_floor =
           average(old_target_config_.error_floor, target_config_.error_floor,
                   change_factor);
+      current_config_.error_ceil =
+          average(old_target_config_.error_ceil, target_config_.error_ceil,
+                  change_factor);
       current_config_.noise_gate =
           average(old_target_config_.noise_gate, target_config_.noise_gate,
                   change_factor);
diff --git a/modules/audio_processing/aec3/main_filter_update_gain_unittest.cc b/modules/audio_processing/aec3/main_filter_update_gain_unittest.cc
index bed148a..093b194 100644
--- a/modules/audio_processing/aec3/main_filter_update_gain_unittest.cc
+++ b/modules/audio_processing/aec3/main_filter_update_gain_unittest.cc
@@ -24,6 +24,7 @@
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/numerics/safe_minmax.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -65,7 +66,7 @@
   config.delay.min_echo_path_delay_blocks = 0;
   config.delay.default_delay = 1;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   AecState aec_state(config);
   RenderSignalAnalyzer render_signal_analyzer(config);
   absl::optional<DelayEstimate> delay_estimate;
@@ -174,16 +175,16 @@
 }
 
 std::string ProduceDebugText(int filter_length_blocks) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Length: " << filter_length_blocks;
-  return ss.str();
+  return ss.Release();
 }
 
 std::string ProduceDebugText(size_t delay, int filter_length_blocks) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Delay: " << delay << ", ";
   ss << ProduceDebugText(filter_length_blocks);
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
diff --git a/modules/audio_processing/aec3/matched_filter.cc b/modules/audio_processing/aec3/matched_filter.cc
index 7486ddf..6744bd5 100644
--- a/modules/audio_processing/aec3/matched_filter.cc
+++ b/modules/audio_processing/aec3/matched_filter.cc
@@ -32,6 +32,7 @@
 
 void MatchedFilterCore_NEON(size_t x_start_index,
                             float x2_sum_threshold,
+                            float smoothing,
                             rtc::ArrayView<const float> x,
                             rtc::ArrayView<const float> y,
                             rtc::ArrayView<float> h,
@@ -92,20 +93,16 @@
 
     // Compute the matched filter error.
     float e = y[i] - s;
-    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f ||
-                            s >= 32000.f || s <= -32000.f || e >= 32000.f ||
-                            e <= -32000.f;
-
-    e = std::min(32767.f, std::max(-32768.f, e));
+    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f;
     (*error_sum) += e * e;
 
     // Update the matched filter estimate in an NLMS manner.
     if (x2_sum > x2_sum_threshold && !saturation) {
       RTC_DCHECK_LT(0.f, x2_sum);
-      const float alpha = 0.7f * e / x2_sum;
+      const float alpha = smoothing * e / x2_sum;
       const float32x4_t alpha_128 = vmovq_n_f32(alpha);
 
-      // filter = filter + 0.7 * (y - filter * x) / x * x.
+      // filter = filter + smoothing * (y - filter * x) * x / x * x.
       float* h_p = &h[0];
       x_p = &x[x_start_index];
 
@@ -145,6 +142,7 @@
 
 void MatchedFilterCore_SSE2(size_t x_start_index,
                             float x2_sum_threshold,
+                            float smoothing,
                             rtc::ArrayView<const float> x,
                             rtc::ArrayView<const float> y,
                             rtc::ArrayView<float> h,
@@ -207,20 +205,16 @@
 
     // Compute the matched filter error.
     float e = y[i] - s;
-    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f ||
-                            s >= 32000.f || s <= -32000.f || e >= 32000.f ||
-                            e <= -32000.f;
-
-    e = std::min(32767.f, std::max(-32768.f, e));
+    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f;
     (*error_sum) += e * e;
 
     // Update the matched filter estimate in an NLMS manner.
     if (x2_sum > x2_sum_threshold && !saturation) {
       RTC_DCHECK_LT(0.f, x2_sum);
-      const float alpha = 0.7f * e / x2_sum;
+      const float alpha = smoothing * e / x2_sum;
       const __m128 alpha_128 = _mm_set1_ps(alpha);
 
-      // filter = filter + 0.7 * (y - filter * x) / x * x.
+      // filter = filter + smoothing * (y - filter * x) * x / x * x.
       float* h_p = &h[0];
       x_p = &x[x_start_index];
 
@@ -259,6 +253,7 @@
 
 void MatchedFilterCore(size_t x_start_index,
                        float x2_sum_threshold,
+                       float smoothing,
                        rtc::ArrayView<const float> x,
                        rtc::ArrayView<const float> y,
                        rtc::ArrayView<float> h,
@@ -278,19 +273,15 @@
 
     // Compute the matched filter error.
     float e = y[i] - s;
-    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f ||
-                            s >= 32000.f || s <= -32000.f || e >= 32000.f ||
-                            e <= -32000.f;
-
-    e = std::min(32767.f, std::max(-32768.f, e));
+    const bool saturation = y[i] >= 32000.f || y[i] <= -32000.f;
     (*error_sum) += e * e;
 
     // Update the matched filter estimate in an NLMS manner.
     if (x2_sum > x2_sum_threshold && !saturation) {
       RTC_DCHECK_LT(0.f, x2_sum);
-      const float alpha = 0.7f * e / x2_sum;
+      const float alpha = smoothing * e / x2_sum;
 
-      // filter = filter + 0.7 * (y - filter * x) / x * x.
+      // filter = filter + smoothing * (y - filter * x) * x / x * x.
       size_t x_index = x_start_index;
       for (size_t k = 0; k < h.size(); ++k) {
         h[k] += alpha * x[x_index];
@@ -311,7 +302,9 @@
                              size_t window_size_sub_blocks,
                              int num_matched_filters,
                              size_t alignment_shift_sub_blocks,
-                             float excitation_limit)
+                             float excitation_limit,
+                             float smoothing,
+                             float matching_filter_threshold)
     : data_dumper_(data_dumper),
       optimization_(optimization),
       sub_block_size_(sub_block_size),
@@ -321,7 +314,9 @@
           std::vector<float>(window_size_sub_blocks * sub_block_size_, 0.f)),
       lag_estimates_(num_matched_filters),
       filters_offsets_(num_matched_filters, 0),
-      excitation_limit_(excitation_limit) {
+      excitation_limit_(excitation_limit),
+      smoothing_(smoothing),
+      matching_filter_threshold_(matching_filter_threshold) {
   RTC_DCHECK(data_dumper);
   RTC_DCHECK_LT(0, window_size_sub_blocks);
   RTC_DCHECK((kBlockSize % sub_block_size) == 0);
@@ -362,19 +357,19 @@
 #if defined(WEBRTC_ARCH_X86_FAMILY)
       case Aec3Optimization::kSse2:
         aec3::MatchedFilterCore_SSE2(x_start_index, x2_sum_threshold,
-                                     render_buffer.buffer, y, filters_[n],
-                                     &filters_updated, &error_sum);
+                                     smoothing_, render_buffer.buffer, y,
+                                     filters_[n], &filters_updated, &error_sum);
         break;
 #endif
 #if defined(WEBRTC_HAS_NEON)
       case Aec3Optimization::kNeon:
         aec3::MatchedFilterCore_NEON(x_start_index, x2_sum_threshold,
-                                     render_buffer.buffer, y, filters_[n],
-                                     &filters_updated, &error_sum);
+                                     smoothing_, render_buffer.buffer, y,
+                                     filters_[n], &filters_updated, &error_sum);
         break;
 #endif
       default:
-        aec3::MatchedFilterCore(x_start_index, x2_sum_threshold,
+        aec3::MatchedFilterCore(x_start_index, x2_sum_threshold, smoothing_,
                                 render_buffer.buffer, y, filters_[n],
                                 &filters_updated, &error_sum);
     }
@@ -393,11 +388,10 @@
             [](float a, float b) -> bool { return a * a < b * b; }));
 
     // Update the lag estimates for the matched filter.
-    const float kMatchingFilterThreshold = 0.2f;
     lag_estimates_[n] = LagEstimate(
         error_sum_anchor - error_sum,
         (lag_estimate > 2 && lag_estimate < (filters_[n].size() - 10) &&
-         error_sum < kMatchingFilterThreshold * error_sum_anchor),
+         error_sum < matching_filter_threshold_ * error_sum_anchor),
         lag_estimate + alignment_shift, filters_updated);
 
     RTC_DCHECK_GE(10, filters_.size());
diff --git a/modules/audio_processing/aec3/matched_filter.h b/modules/audio_processing/aec3/matched_filter.h
index 1c06b5e..2ef4828 100644
--- a/modules/audio_processing/aec3/matched_filter.h
+++ b/modules/audio_processing/aec3/matched_filter.h
@@ -30,6 +30,7 @@
 // Filter core for the matched filter that is optimized for NEON.
 void MatchedFilterCore_NEON(size_t x_start_index,
                             float x2_sum_threshold,
+                            float smoothing,
                             rtc::ArrayView<const float> x,
                             rtc::ArrayView<const float> y,
                             rtc::ArrayView<float> h,
@@ -43,6 +44,7 @@
 // Filter core for the matched filter that is optimized for SSE2.
 void MatchedFilterCore_SSE2(size_t x_start_index,
                             float x2_sum_threshold,
+                            float smoothing,
                             rtc::ArrayView<const float> x,
                             rtc::ArrayView<const float> y,
                             rtc::ArrayView<float> h,
@@ -54,6 +56,7 @@
 // Filter core for the matched filter.
 void MatchedFilterCore(size_t x_start_index,
                        float x2_sum_threshold,
+                       float smoothing,
                        rtc::ArrayView<const float> x,
                        rtc::ArrayView<const float> y,
                        rtc::ArrayView<float> h,
@@ -87,7 +90,9 @@
                 size_t window_size_sub_blocks,
                 int num_matched_filters,
                 size_t alignment_shift_sub_blocks,
-                float excitation_limit);
+                float excitation_limit,
+                float smoothing,
+                float matching_filter_threshold);
 
   ~MatchedFilter();
 
@@ -122,6 +127,8 @@
   std::vector<LagEstimate> lag_estimates_;
   std::vector<size_t> filters_offsets_;
   const float excitation_limit_;
+  const float smoothing_;
+  const float matching_filter_threshold_;
 
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MatchedFilter);
 };
diff --git a/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc b/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
index 9cd2eb3..0a73768 100644
--- a/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
+++ b/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
@@ -15,19 +15,25 @@
 
 MatchedFilterLagAggregator::MatchedFilterLagAggregator(
     ApmDataDumper* data_dumper,
-    size_t max_filter_lag)
-    : data_dumper_(data_dumper), histogram_(max_filter_lag + 1, 0) {
+    size_t max_filter_lag,
+    const EchoCanceller3Config::Delay::DelaySelectionThresholds& thresholds)
+    : data_dumper_(data_dumper),
+      histogram_(max_filter_lag + 1, 0),
+      thresholds_(thresholds) {
   RTC_DCHECK(data_dumper);
+  RTC_DCHECK_LE(thresholds_.initial, thresholds_.converged);
   histogram_data_.fill(0);
 }
 
 MatchedFilterLagAggregator::~MatchedFilterLagAggregator() = default;
 
-void MatchedFilterLagAggregator::Reset() {
+void MatchedFilterLagAggregator::Reset(bool hard_reset) {
   std::fill(histogram_.begin(), histogram_.end(), 0);
   histogram_data_.fill(0);
   histogram_data_index_ = 0;
-  significant_candidate_found_ = false;
+  if (hard_reset) {
+    significant_candidate_found_ = false;
+  }
 }
 
 absl::optional<DelayEstimate> MatchedFilterLagAggregator::Aggregate(
@@ -67,11 +73,19 @@
         std::distance(histogram_.begin(),
                       std::max_element(histogram_.begin(), histogram_.end()));
 
-    if (histogram_[candidate] > 25) {
-      significant_candidate_found_ = true;
-      return DelayEstimate(DelayEstimate::Quality::kRefined, candidate);
+    significant_candidate_found_ =
+        significant_candidate_found_ ||
+        histogram_[candidate] > thresholds_.converged;
+    if (histogram_[candidate] > thresholds_.converged ||
+        (histogram_[candidate] > thresholds_.initial &&
+         !significant_candidate_found_)) {
+      DelayEstimate::Quality quality = significant_candidate_found_
+                                           ? DelayEstimate::Quality::kRefined
+                                           : DelayEstimate::Quality::kCoarse;
+      return DelayEstimate(quality, candidate);
     }
   }
+
   return absl::nullopt;
 }
 
diff --git a/modules/audio_processing/aec3/matched_filter_lag_aggregator.h b/modules/audio_processing/aec3/matched_filter_lag_aggregator.h
index fddcfbf..d7f34ae 100644
--- a/modules/audio_processing/aec3/matched_filter_lag_aggregator.h
+++ b/modules/audio_processing/aec3/matched_filter_lag_aggregator.h
@@ -14,6 +14,7 @@
 #include <vector>
 
 #include "absl/types/optional.h"
+#include "api/audio/echo_canceller3_config.h"
 #include "modules/audio_processing/aec3/delay_estimate.h"
 #include "modules/audio_processing/aec3/matched_filter.h"
 #include "rtc_base/constructormagic.h"
@@ -26,11 +27,14 @@
 // reliable combined lag estimate.
 class MatchedFilterLagAggregator {
  public:
-  MatchedFilterLagAggregator(ApmDataDumper* data_dumper, size_t max_filter_lag);
+  MatchedFilterLagAggregator(
+      ApmDataDumper* data_dumper,
+      size_t max_filter_lag,
+      const EchoCanceller3Config::Delay::DelaySelectionThresholds& thresholds);
   ~MatchedFilterLagAggregator();
 
   // Resets the aggregator.
-  void Reset();
+  void Reset(bool hard_reset);
 
   // Aggregates the provided lag estimates.
   absl::optional<DelayEstimate> Aggregate(
@@ -42,6 +46,7 @@
   std::array<int, 250> histogram_data_;
   int histogram_data_index_ = 0;
   bool significant_candidate_found_ = false;
+  const EchoCanceller3Config::Delay::DelaySelectionThresholds thresholds_;
 
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(MatchedFilterLagAggregator);
 };
diff --git a/modules/audio_processing/aec3/matched_filter_lag_aggregator_unittest.cc b/modules/audio_processing/aec3/matched_filter_lag_aggregator_unittest.cc
index cea5f13..e136c89 100644
--- a/modules/audio_processing/aec3/matched_filter_lag_aggregator_unittest.cc
+++ b/modules/audio_processing/aec3/matched_filter_lag_aggregator_unittest.cc
@@ -15,6 +15,7 @@
 #include <vector>
 
 #include "api/array_view.h"
+#include "api/audio/echo_canceller3_config.h"
 #include "modules/audio_processing/aec3/aec3_common.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "test/gtest.h"
@@ -31,8 +32,11 @@
   constexpr size_t kLag1 = 5;
   constexpr size_t kLag2 = 10;
   ApmDataDumper data_dumper(0);
+  EchoCanceller3Config config;
   std::vector<MatchedFilter::LagEstimate> lag_estimates(2);
-  MatchedFilterLagAggregator aggregator(&data_dumper, std::max(kLag1, kLag2));
+  MatchedFilterLagAggregator aggregator(
+      &data_dumper, std::max(kLag1, kLag2),
+      config.delay.delay_selection_thresholds);
   lag_estimates[0] = MatchedFilter::LagEstimate(1.f, true, kLag1, true);
   lag_estimates[1] = MatchedFilter::LagEstimate(0.5f, true, kLag2, true);
 
@@ -65,8 +69,10 @@
 TEST(MatchedFilterLagAggregator,
      LagEstimateInvarianceRequiredForAggregatedLag) {
   ApmDataDumper data_dumper(0);
+  EchoCanceller3Config config;
   std::vector<MatchedFilter::LagEstimate> lag_estimates(1);
-  MatchedFilterLagAggregator aggregator(&data_dumper, 100);
+  MatchedFilterLagAggregator aggregator(
+      &data_dumper, 100, config.delay.delay_selection_thresholds);
 
   absl::optional<DelayEstimate> aggregated_lag;
   for (size_t k = 0; k < kNumLagsBeforeDetection; ++k) {
@@ -94,8 +100,10 @@
      DISABLED_LagEstimateUpdatesRequiredForAggregatedLag) {
   constexpr size_t kLag = 5;
   ApmDataDumper data_dumper(0);
+  EchoCanceller3Config config;
   std::vector<MatchedFilter::LagEstimate> lag_estimates(1);
-  MatchedFilterLagAggregator aggregator(&data_dumper, kLag);
+  MatchedFilterLagAggregator aggregator(
+      &data_dumper, kLag, config.delay.delay_selection_thresholds);
   for (size_t k = 0; k < kNumLagsBeforeDetection * 10; ++k) {
     lag_estimates[0] = MatchedFilter::LagEstimate(1.f, true, kLag, false);
     absl::optional<DelayEstimate> aggregated_lag =
@@ -112,8 +120,11 @@
   constexpr size_t kLag1 = 5;
   constexpr size_t kLag2 = 10;
   ApmDataDumper data_dumper(0);
+  EchoCanceller3Config config;
   std::vector<MatchedFilter::LagEstimate> lag_estimates(1);
-  MatchedFilterLagAggregator aggregator(&data_dumper, std::max(kLag1, kLag2));
+  MatchedFilterLagAggregator aggregator(
+      &data_dumper, std::max(kLag1, kLag2),
+      config.delay.delay_selection_thresholds);
   absl::optional<DelayEstimate> aggregated_lag;
   for (size_t k = 0; k < kNumLagsBeforeDetection; ++k) {
     lag_estimates[0] = MatchedFilter::LagEstimate(1.f, true, kLag1, true);
@@ -134,7 +145,10 @@
 
 // Verifies the check for non-null data dumper.
 TEST(MatchedFilterLagAggregator, NullDataDumper) {
-  EXPECT_DEATH(MatchedFilterLagAggregator(nullptr, 10), "");
+  EchoCanceller3Config config;
+  EXPECT_DEATH(MatchedFilterLagAggregator(
+                   nullptr, 10, config.delay.delay_selection_thresholds),
+               "");
 }
 
 #endif
diff --git a/modules/audio_processing/aec3/matched_filter_unittest.cc b/modules/audio_processing/aec3/matched_filter_unittest.cc
index c7dc211..0c17118 100644
--- a/modules/audio_processing/aec3/matched_filter_unittest.cc
+++ b/modules/audio_processing/aec3/matched_filter_unittest.cc
@@ -17,7 +17,6 @@
 #include <emmintrin.h>
 #endif
 #include <algorithm>
-#include <sstream>
 #include <string>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
@@ -26,6 +25,7 @@
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "system_wrappers/include/cpu_features_wrapper.h"
 #include "test/gtest.h"
 
@@ -34,10 +34,10 @@
 namespace {
 
 std::string ProduceDebugText(size_t delay, size_t down_sampling_factor) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Delay: " << delay;
   ss << ", Down sampling factor: " << down_sampling_factor;
-  return ss.str();
+  return ss.Release();
 }
 
 constexpr size_t kNumMatchedFilters = 10;
@@ -52,6 +52,7 @@
 // counterparts.
 TEST(MatchedFilter, TestNeonOptimizations) {
   Random random_generator(42U);
+  constexpr float kSmoothing = 0.7f;
   for (auto down_sampling_factor : kDownSamplingFactors) {
     const size_t sub_block_size = kBlockSize / down_sampling_factor;
 
@@ -69,10 +70,10 @@
       bool filters_updated_NEON = false;
       float error_sum_NEON = 0.f;
 
-      MatchedFilterCore_NEON(x_index, h.size() * 150.f * 150.f, x, y, h_NEON,
-                             &filters_updated_NEON, &error_sum_NEON);
+      MatchedFilterCore_NEON(x_index, h.size() * 150.f * 150.f, kSmoothing, x,
+                             y, h_NEON, &filters_updated_NEON, &error_sum_NEON);
 
-      MatchedFilterCore(x_index, h.size() * 150.f * 150.f, x, y, h,
+      MatchedFilterCore(x_index, h.size() * 150.f * 150.f, kSmoothing, x, y, h,
                         &filters_updated, &error_sum);
 
       EXPECT_EQ(filters_updated, filters_updated_NEON);
@@ -95,6 +96,7 @@
   bool use_sse2 = (WebRtc_GetCPUInfo(kSSE2) != 0);
   if (use_sse2) {
     Random random_generator(42U);
+    constexpr float kSmoothing = 0.7f;
     for (auto down_sampling_factor : kDownSamplingFactors) {
       const size_t sub_block_size = kBlockSize / down_sampling_factor;
       std::vector<float> x(2000);
@@ -111,11 +113,12 @@
         bool filters_updated_SSE2 = false;
         float error_sum_SSE2 = 0.f;
 
-        MatchedFilterCore_SSE2(x_index, h.size() * 150.f * 150.f, x, y, h_SSE2,
-                               &filters_updated_SSE2, &error_sum_SSE2);
+        MatchedFilterCore_SSE2(x_index, h.size() * 150.f * 150.f, kSmoothing, x,
+                               y, h_SSE2, &filters_updated_SSE2,
+                               &error_sum_SSE2);
 
-        MatchedFilterCore(x_index, h.size() * 150.f * 150.f, x, y, h,
-                          &filters_updated, &error_sum);
+        MatchedFilterCore(x_index, h.size() * 150.f * 150.f, kSmoothing, x, y,
+                          h, &filters_updated, &error_sum);
 
         EXPECT_EQ(filters_updated, filters_updated_SSE2);
         EXPECT_NEAR(error_sum, error_sum_SSE2, error_sum / 100000.f);
@@ -157,10 +160,12 @@
                                              delay_samples);
       MatchedFilter filter(&data_dumper, DetectOptimization(), sub_block_size,
                            kWindowSizeSubBlocks, kNumMatchedFilters,
-                           kAlignmentShiftSubBlocks, 150);
+                           kAlignmentShiftSubBlocks, 150,
+                           config.delay.delay_estimate_smoothing,
+                           config.delay.delay_candidate_detection_threshold);
 
       std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-          RenderDelayBuffer::Create(config, 3));
+          RenderDelayBuffer::Create2(config, 3));
 
       // Analyze the correlation between render and capture.
       for (size_t k = 0; k < (600 + delay_samples / sub_block_size); ++k) {
@@ -256,10 +261,12 @@
     std::fill(capture.begin(), capture.end(), 0.f);
     ApmDataDumper data_dumper(0);
     std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-        RenderDelayBuffer::Create(config, 3));
+        RenderDelayBuffer::Create2(config, 3));
     MatchedFilter filter(&data_dumper, DetectOptimization(), sub_block_size,
                          kWindowSizeSubBlocks, kNumMatchedFilters,
-                         kAlignmentShiftSubBlocks, 150);
+                         kAlignmentShiftSubBlocks, 150,
+                         config.delay.delay_estimate_smoothing,
+                         config.delay.delay_candidate_detection_threshold);
 
     // Analyze the correlation between render and capture.
     for (size_t k = 0; k < 100; ++k) {
@@ -292,11 +299,14 @@
     std::array<float, kBlockSize> capture;
     capture.fill(0.f);
     ApmDataDumper data_dumper(0);
+    EchoCanceller3Config config;
     MatchedFilter filter(&data_dumper, DetectOptimization(), sub_block_size,
                          kWindowSizeSubBlocks, kNumMatchedFilters,
-                         kAlignmentShiftSubBlocks, 150);
+                         kAlignmentShiftSubBlocks, 150,
+                         config.delay.delay_estimate_smoothing,
+                         config.delay.delay_candidate_detection_threshold);
     std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-        RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+        RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
     Decimator capture_decimator(down_sampling_factor);
 
     // Analyze the correlation between render and capture.
@@ -331,12 +341,15 @@
 // number of alignment shifts.
 TEST(MatchedFilter, NumberOfLagEstimates) {
   ApmDataDumper data_dumper(0);
+  EchoCanceller3Config config;
   for (auto down_sampling_factor : kDownSamplingFactors) {
     const size_t sub_block_size = kBlockSize / down_sampling_factor;
     for (size_t num_matched_filters = 0; num_matched_filters < 10;
          ++num_matched_filters) {
       MatchedFilter filter(&data_dumper, DetectOptimization(), sub_block_size,
-                           32, num_matched_filters, 1, 150);
+                           32, num_matched_filters, 1, 150,
+                           config.delay.delay_estimate_smoothing,
+                           config.delay.delay_candidate_detection_threshold);
       EXPECT_EQ(num_matched_filters, filter.GetLagEstimates().size());
     }
   }
@@ -347,13 +360,19 @@
 // Verifies the check for non-zero windows size.
 TEST(MatchedFilter, ZeroWindowSize) {
   ApmDataDumper data_dumper(0);
-  EXPECT_DEATH(
-      MatchedFilter(&data_dumper, DetectOptimization(), 16, 0, 1, 1, 150), "");
+  EchoCanceller3Config config;
+  EXPECT_DEATH(MatchedFilter(&data_dumper, DetectOptimization(), 16, 0, 1, 1,
+                             150, config.delay.delay_estimate_smoothing,
+                             config.delay.delay_candidate_detection_threshold),
+               "");
 }
 
 // Verifies the check for non-null data dumper.
 TEST(MatchedFilter, NullDataDumper) {
-  EXPECT_DEATH(MatchedFilter(nullptr, DetectOptimization(), 16, 1, 1, 1, 150),
+  EchoCanceller3Config config;
+  EXPECT_DEATH(MatchedFilter(nullptr, DetectOptimization(), 16, 1, 1, 1, 150,
+                             config.delay.delay_estimate_smoothing,
+                             config.delay.delay_candidate_detection_threshold),
                "");
 }
 
@@ -361,8 +380,11 @@
 // TODO(peah): Activate the unittest once the required code has been landed.
 TEST(MatchedFilter, DISABLED_BlockSizeMultipleOf4) {
   ApmDataDumper data_dumper(0);
-  EXPECT_DEATH(
-      MatchedFilter(&data_dumper, DetectOptimization(), 15, 1, 1, 1, 150), "");
+  EchoCanceller3Config config;
+  EXPECT_DEATH(MatchedFilter(&data_dumper, DetectOptimization(), 15, 1, 1, 1,
+                             150, config.delay.delay_estimate_smoothing,
+                             config.delay.delay_candidate_detection_threshold),
+               "");
 }
 
 // Verifies the check for that there is an integer number of sub blocks that add
@@ -370,8 +392,11 @@
 // TODO(peah): Activate the unittest once the required code has been landed.
 TEST(MatchedFilter, DISABLED_SubBlockSizeAddsUpToBlockSize) {
   ApmDataDumper data_dumper(0);
-  EXPECT_DEATH(
-      MatchedFilter(&data_dumper, DetectOptimization(), 12, 1, 1, 1, 150), "");
+  EchoCanceller3Config config;
+  EXPECT_DEATH(MatchedFilter(&data_dumper, DetectOptimization(), 12, 1, 1, 1,
+                             150, config.delay.delay_estimate_smoothing,
+                             config.delay.delay_candidate_detection_threshold),
+               "");
 }
 
 #endif
diff --git a/modules/audio_processing/aec3/mock/mock_render_delay_controller.h b/modules/audio_processing/aec3/mock/mock_render_delay_controller.h
index ed5971c..5520f76 100644
--- a/modules/audio_processing/aec3/mock/mock_render_delay_controller.h
+++ b/modules/audio_processing/aec3/mock/mock_render_delay_controller.h
@@ -25,7 +25,7 @@
   MockRenderDelayController();
   virtual ~MockRenderDelayController();
 
-  MOCK_METHOD0(Reset, void());
+  MOCK_METHOD1(Reset, void(bool reset_delay_statistics));
   MOCK_METHOD0(LogRenderCall, void());
   MOCK_METHOD4(GetDelay,
                absl::optional<DelayEstimate>(
diff --git a/modules/audio_processing/aec3/render_buffer.cc b/modules/audio_processing/aec3/render_buffer.cc
index 235e3e3..f6ffa04 100644
--- a/modules/audio_processing/aec3/render_buffer.cc
+++ b/modules/audio_processing/aec3/render_buffer.cc
@@ -11,6 +11,7 @@
 #include "modules/audio_processing/aec3/render_buffer.h"
 
 #include <algorithm>
+#include <functional>
 
 #include "modules/audio_processing/aec3/aec3_common.h"
 #include "rtc_base/checks.h"
diff --git a/modules/audio_processing/aec3/render_delay_buffer.h b/modules/audio_processing/aec3/render_delay_buffer.h
index a6d6874..8c5667e 100644
--- a/modules/audio_processing/aec3/render_delay_buffer.h
+++ b/modules/audio_processing/aec3/render_delay_buffer.h
@@ -33,12 +33,13 @@
     kNone,
     kRenderUnderrun,
     kRenderOverrun,
-    kApiCallSkew,
-    kRenderDataLost
+    kApiCallSkew
   };
 
   static RenderDelayBuffer* Create(const EchoCanceller3Config& config,
                                    size_t num_bands);
+  static RenderDelayBuffer* Create2(const EchoCanceller3Config& config,
+                                    size_t num_bands);
   virtual ~RenderDelayBuffer() = default;
 
   // Resets the buffer alignment.
diff --git a/modules/audio_processing/aec3/render_delay_buffer2.cc b/modules/audio_processing/aec3/render_delay_buffer2.cc
new file mode 100644
index 0000000..6992c5b
--- /dev/null
+++ b/modules/audio_processing/aec3/render_delay_buffer2.cc
@@ -0,0 +1,453 @@
+/*
+ *  Copyright (c) 2018 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 <string.h>
+#include <algorithm>
+#include <memory>
+#include <numeric>
+#include <vector>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "api/audio/echo_canceller3_config.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/aec3/aec3_fft.h"
+#include "modules/audio_processing/aec3/decimator.h"
+#include "modules/audio_processing/aec3/downsampled_render_buffer.h"
+#include "modules/audio_processing/aec3/fft_buffer.h"
+#include "modules/audio_processing/aec3/fft_data.h"
+#include "modules/audio_processing/aec3/matrix_buffer.h"
+#include "modules/audio_processing/aec3/render_buffer.h"
+#include "modules/audio_processing/aec3/render_delay_buffer.h"
+#include "modules/audio_processing/aec3/vector_buffer.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "rtc_base/atomicops.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+
+namespace webrtc {
+namespace {
+
+class RenderDelayBufferImpl2 final : public RenderDelayBuffer {
+ public:
+  RenderDelayBufferImpl2(const EchoCanceller3Config& config, size_t num_bands);
+  RenderDelayBufferImpl2() = delete;
+  ~RenderDelayBufferImpl2() override;
+
+  void Reset() override;
+  BufferingEvent Insert(const std::vector<std::vector<float>>& block) override;
+  BufferingEvent PrepareCaptureProcessing() override;
+  bool SetDelay(size_t delay) override;
+  size_t Delay() const override { return ComputeDelay(); }
+  size_t MaxDelay() const override {
+    return blocks_.buffer.size() - 1 - buffer_headroom_;
+  }
+  RenderBuffer* GetRenderBuffer() override { return &echo_remover_buffer_; }
+
+  const DownsampledRenderBuffer& GetDownsampledRenderBuffer() const override {
+    return low_rate_;
+  }
+
+  int BufferLatency() const;
+  bool CausalDelay(size_t delay) const override;
+  void SetAudioBufferDelay(size_t delay_ms) override;
+
+ private:
+  static int instance_count_;
+  std::unique_ptr<ApmDataDumper> data_dumper_;
+  const Aec3Optimization optimization_;
+  const EchoCanceller3Config config_;
+  size_t down_sampling_factor_;
+  const int sub_block_size_;
+  MatrixBuffer blocks_;
+  VectorBuffer spectra_;
+  FftBuffer ffts_;
+  absl::optional<size_t> delay_;
+  RenderBuffer echo_remover_buffer_;
+  DownsampledRenderBuffer low_rate_;
+  Decimator render_decimator_;
+  const Aec3Fft fft_;
+  std::vector<float> render_ds_;
+  const int buffer_headroom_;
+  bool last_call_was_render_ = false;
+  int num_api_calls_in_a_row_ = 0;
+  int max_observed_jitter_ = 1;
+  size_t capture_call_counter_ = 0;
+  size_t render_call_counter_ = 0;
+  bool render_activity_ = false;
+  size_t render_activity_counter_ = 0;
+  absl::optional<size_t> external_audio_buffer_delay_;
+  bool external_audio_buffer_delay_verified_after_reset_ = false;
+  size_t min_latency_blocks_ = 0;
+  size_t excess_render_detection_counter_ = 0;
+  size_t num_bands_;
+
+  int MapDelayToTotalDelay(size_t delay) const;
+  int ComputeDelay() const;
+  void ApplyTotalDelay(int delay);
+  void InsertBlock(const std::vector<std::vector<float>>& block,
+                   int previous_write);
+  bool DetectActiveRender(rtc::ArrayView<const float> x) const;
+  bool DetectExcessRenderBlocks();
+  void IncrementWriteIndices();
+  void IncrementLowRateReadIndices();
+  void IncrementReadIndices();
+  bool RenderOverrun();
+  bool RenderUnderrun();
+};
+
+int RenderDelayBufferImpl2::instance_count_ = 0;
+
+RenderDelayBufferImpl2::RenderDelayBufferImpl2(
+    const EchoCanceller3Config& config,
+    size_t num_bands)
+    : data_dumper_(
+          new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
+      optimization_(DetectOptimization()),
+      config_(config),
+      down_sampling_factor_(config.delay.down_sampling_factor),
+      sub_block_size_(static_cast<int>(down_sampling_factor_ > 0
+                                           ? kBlockSize / down_sampling_factor_
+                                           : kBlockSize)),
+      blocks_(GetRenderDelayBufferSize(down_sampling_factor_,
+                                       config.delay.num_filters,
+                                       config.filter.main.length_blocks),
+              num_bands,
+              kBlockSize),
+      spectra_(blocks_.buffer.size(), kFftLengthBy2Plus1),
+      ffts_(blocks_.buffer.size()),
+      delay_(config_.delay.default_delay),
+      echo_remover_buffer_(&blocks_, &spectra_, &ffts_),
+      low_rate_(GetDownSampledBufferSize(down_sampling_factor_,
+                                         config.delay.num_filters)),
+      render_decimator_(down_sampling_factor_),
+      fft_(),
+      render_ds_(sub_block_size_, 0.f),
+      buffer_headroom_(config.filter.main.length_blocks),
+      num_bands_(num_bands) {
+  RTC_DCHECK_EQ(blocks_.buffer.size(), ffts_.buffer.size());
+  RTC_DCHECK_EQ(spectra_.buffer.size(), ffts_.buffer.size());
+
+  Reset();
+}
+
+RenderDelayBufferImpl2::~RenderDelayBufferImpl2() = default;
+
+// Resets the buffer delays and clears the reported delays.
+void RenderDelayBufferImpl2::Reset() {
+  last_call_was_render_ = false;
+  num_api_calls_in_a_row_ = 1;
+  min_latency_blocks_ = 0;
+  excess_render_detection_counter_ = 0;
+
+  // Initialize the read index to one sub-block before the write index.
+  low_rate_.read = low_rate_.OffsetIndex(low_rate_.write, sub_block_size_);
+
+  // Check for any external audio buffer delay and whether it is feasible.
+  if (external_audio_buffer_delay_) {
+    const size_t headroom = 2;
+    size_t audio_buffer_delay_to_set;
+    // Minimum delay is 1 (like the low-rate render buffer).
+    if (*external_audio_buffer_delay_ <= headroom) {
+      audio_buffer_delay_to_set = 1;
+    } else {
+      audio_buffer_delay_to_set = *external_audio_buffer_delay_ - headroom;
+    }
+
+    audio_buffer_delay_to_set = std::min(audio_buffer_delay_to_set, MaxDelay());
+
+    // When an external delay estimate is available, use that delay as the
+    // initial render buffer delay.
+    ApplyTotalDelay(audio_buffer_delay_to_set);
+    delay_ = ComputeDelay();
+
+    external_audio_buffer_delay_verified_after_reset_ = false;
+  } else {
+    // If an external delay estimate is not available, use that delay as the
+    // initial delay. Set the render buffer delays to the default delay.
+    ApplyTotalDelay(config_.delay.default_delay);
+
+    // Unset the delays which are set by SetDelay.
+    delay_ = absl::nullopt;
+  }
+}
+
+// Inserts a new block into the render buffers.
+RenderDelayBuffer::BufferingEvent RenderDelayBufferImpl2::Insert(
+    const std::vector<std::vector<float>>& block) {
+  ++render_call_counter_;
+  if (delay_) {
+    if (!last_call_was_render_) {
+      last_call_was_render_ = true;
+      num_api_calls_in_a_row_ = 1;
+    } else {
+      if (++num_api_calls_in_a_row_ > max_observed_jitter_) {
+        max_observed_jitter_ = num_api_calls_in_a_row_;
+        RTC_LOG(LS_WARNING)
+            << "New max number api jitter observed at render block "
+            << render_call_counter_ << ":  " << num_api_calls_in_a_row_
+            << " blocks";
+      }
+    }
+  }
+
+  // Increase the write indices to where the new blocks should be written.
+  const int previous_write = blocks_.write;
+  IncrementWriteIndices();
+
+  // Allow overrun and do a reset when render overrun occurrs due to more render
+  // data being inserted than capture data is received.
+  BufferingEvent event =
+      RenderOverrun() ? BufferingEvent::kRenderOverrun : BufferingEvent::kNone;
+
+  // Detect and update render activity.
+  if (!render_activity_) {
+    render_activity_counter_ += DetectActiveRender(block[0]) ? 1 : 0;
+    render_activity_ = render_activity_counter_ >= 20;
+  }
+
+  // Insert the new render block into the specified position.
+  InsertBlock(block, previous_write);
+
+  if (event != BufferingEvent::kNone) {
+    Reset();
+  }
+
+  return event;
+}
+
+// Prepares the render buffers for processing another capture block.
+RenderDelayBuffer::BufferingEvent
+RenderDelayBufferImpl2::PrepareCaptureProcessing() {
+  RenderDelayBuffer::BufferingEvent event = BufferingEvent::kNone;
+  ++capture_call_counter_;
+
+  if (delay_) {
+    if (last_call_was_render_) {
+      last_call_was_render_ = false;
+      num_api_calls_in_a_row_ = 1;
+    } else {
+      if (++num_api_calls_in_a_row_ > max_observed_jitter_) {
+        max_observed_jitter_ = num_api_calls_in_a_row_;
+        RTC_LOG(LS_WARNING)
+            << "New max number api jitter observed at capture block "
+            << capture_call_counter_ << ":  " << num_api_calls_in_a_row_
+            << " blocks";
+      }
+    }
+  }
+
+  if (DetectExcessRenderBlocks()) {
+    // Too many render blocks compared to capture blocks. Risk of delay ending
+    // up before the filter used by the delay estimator.
+    RTC_LOG(LS_WARNING) << "Excess render blocks detected at block "
+                        << capture_call_counter_;
+    Reset();
+    event = BufferingEvent::kRenderOverrun;
+  } else if (RenderUnderrun()) {
+    // Don't increment the read indices of the low rate buffer if there is a
+    // render underrun.
+    RTC_LOG(LS_WARNING) << "Render buffer underrun detected at block "
+                        << capture_call_counter_;
+    IncrementReadIndices();
+    // Incrementing the buffer index without increasing the low rate buffer
+    // index means that the delay is reduced by one.
+    if (delay_ && *delay_ > 0)
+      delay_ = *delay_ - 1;
+    event = BufferingEvent::kRenderUnderrun;
+  } else {
+    // Increment the read indices in the render buffers to point to the most
+    // recent block to use in the capture processing.
+    IncrementLowRateReadIndices();
+    IncrementReadIndices();
+  }
+
+  echo_remover_buffer_.SetRenderActivity(render_activity_);
+  if (render_activity_) {
+    render_activity_counter_ = 0;
+    render_activity_ = false;
+  }
+
+  return event;
+}
+
+// Sets the delay and returns a bool indicating whether the delay was changed.
+bool RenderDelayBufferImpl2::SetDelay(size_t delay) {
+  if (!external_audio_buffer_delay_verified_after_reset_ &&
+      external_audio_buffer_delay_ && delay_) {
+    int difference = static_cast<int>(delay) - static_cast<int>(*delay_);
+    RTC_LOG(LS_WARNING) << "Mismatch between first estimated delay after reset "
+                           "and externally reported audio buffer delay: "
+                        << difference << " blocks";
+    external_audio_buffer_delay_verified_after_reset_ = true;
+  }
+  if (delay_ && *delay_ == delay) {
+    return false;
+  }
+  delay_ = delay;
+
+  // Compute the total delay and limit the delay to the allowed range.
+  int total_delay = MapDelayToTotalDelay(*delay_);
+  total_delay =
+      std::min(MaxDelay(), static_cast<size_t>(std::max(total_delay, 0)));
+
+  // Apply the delay to the buffers.
+  ApplyTotalDelay(total_delay);
+  return true;
+}
+
+// Returns whether the specified delay is causal.
+bool RenderDelayBufferImpl2::CausalDelay(size_t delay) const {
+  // TODO(gustaf): Remove this from RenderDelayBuffer.
+  return true;
+}
+
+void RenderDelayBufferImpl2::SetAudioBufferDelay(size_t delay_ms) {
+  if (!external_audio_buffer_delay_) {
+    RTC_LOG(LS_WARNING)
+        << "Receiving a first externally reported audio buffer delay of "
+        << delay_ms << " ms.";
+  }
+
+  // Convert delay from milliseconds to blocks (rounded down).
+  external_audio_buffer_delay_ = delay_ms >> ((num_bands_ == 1) ? 1 : 2);
+}
+
+// Maps the externally computed delay to the delay used internally.
+int RenderDelayBufferImpl2::MapDelayToTotalDelay(
+    size_t external_delay_blocks) const {
+  const int latency_blocks = BufferLatency();
+  return latency_blocks + static_cast<int>(external_delay_blocks);
+}
+
+// Returns the delay (not including call jitter).
+int RenderDelayBufferImpl2::ComputeDelay() const {
+  const int latency_blocks = BufferLatency();
+  int internal_delay = spectra_.read >= spectra_.write
+                           ? spectra_.read - spectra_.write
+                           : spectra_.size + spectra_.read - spectra_.write;
+
+  return internal_delay - latency_blocks;
+}
+
+// Set the read indices according to the delay.
+void RenderDelayBufferImpl2::ApplyTotalDelay(int delay) {
+  RTC_LOG(LS_WARNING) << "Applying total delay of " << delay << " blocks.";
+  blocks_.read = blocks_.OffsetIndex(blocks_.write, -delay);
+  spectra_.read = spectra_.OffsetIndex(spectra_.write, delay);
+  ffts_.read = ffts_.OffsetIndex(ffts_.write, delay);
+}
+
+// Inserts a block into the render buffers.
+void RenderDelayBufferImpl2::InsertBlock(
+    const std::vector<std::vector<float>>& block,
+    int previous_write) {
+  auto& b = blocks_;
+  auto& lr = low_rate_;
+  auto& ds = render_ds_;
+  auto& f = ffts_;
+  auto& s = spectra_;
+  RTC_DCHECK_EQ(block.size(), b.buffer[b.write].size());
+  for (size_t k = 0; k < block.size(); ++k) {
+    RTC_DCHECK_EQ(block[k].size(), b.buffer[b.write][k].size());
+    std::copy(block[k].begin(), block[k].end(), b.buffer[b.write][k].begin());
+  }
+
+  data_dumper_->DumpWav("aec3_render_decimator_input", block[0].size(),
+                        block[0].data(), 16000, 1);
+  render_decimator_.Decimate(block[0], ds);
+  data_dumper_->DumpWav("aec3_render_decimator_output", ds.size(), ds.data(),
+                        16000 / down_sampling_factor_, 1);
+  std::copy(ds.rbegin(), ds.rend(), lr.buffer.begin() + lr.write);
+  fft_.PaddedFft(block[0], b.buffer[previous_write][0], &f.buffer[f.write]);
+  f.buffer[f.write].Spectrum(optimization_, s.buffer[s.write]);
+}
+
+bool RenderDelayBufferImpl2::DetectActiveRender(
+    rtc::ArrayView<const float> x) const {
+  const float x_energy = std::inner_product(x.begin(), x.end(), x.begin(), 0.f);
+  return x_energy > (config_.render_levels.active_render_limit *
+                     config_.render_levels.active_render_limit) *
+                        kFftLengthBy2;
+}
+
+bool RenderDelayBufferImpl2::DetectExcessRenderBlocks() {
+  bool excess_render_detected = false;
+  const size_t latency_blocks = static_cast<size_t>(BufferLatency());
+  // The recently seen minimum latency in blocks. Should be close to 0.
+  min_latency_blocks_ = std::min(min_latency_blocks_, latency_blocks);
+  // After processing a configurable number of blocks the minimum latency is
+  // checked.
+  if (++excess_render_detection_counter_ >=
+      config_.buffering.excess_render_detection_interval_blocks) {
+    // If the minimum latency is not lower than the threshold there have been
+    // more render than capture frames.
+    excess_render_detected = min_latency_blocks_ >
+                             config_.buffering.max_allowed_excess_render_blocks;
+    // Reset the counter and let the minimum latency be the current latency.
+    min_latency_blocks_ = latency_blocks;
+    excess_render_detection_counter_ = 0;
+  }
+
+  data_dumper_->DumpRaw("aec3_latency_blocks", latency_blocks);
+  data_dumper_->DumpRaw("aec3_min_latency_blocks", min_latency_blocks_);
+  data_dumper_->DumpRaw("aec3_excess_render_detected", excess_render_detected);
+  return excess_render_detected;
+}
+
+// Computes the latency in the buffer (the number of unread sub-blocks).
+int RenderDelayBufferImpl2::BufferLatency() const {
+  const DownsampledRenderBuffer& l = low_rate_;
+  int latency_samples = (l.buffer.size() + l.read - l.write) % l.buffer.size();
+  int latency_blocks = latency_samples / sub_block_size_;
+  return latency_blocks;
+}
+
+// Increments the write indices for the render buffers.
+void RenderDelayBufferImpl2::IncrementWriteIndices() {
+  low_rate_.UpdateWriteIndex(-sub_block_size_);
+  blocks_.IncWriteIndex();
+  spectra_.DecWriteIndex();
+  ffts_.DecWriteIndex();
+}
+
+// Increments the read indices of the low rate render buffers.
+void RenderDelayBufferImpl2::IncrementLowRateReadIndices() {
+  low_rate_.UpdateReadIndex(-sub_block_size_);
+}
+
+// Increments the read indices for the render buffers.
+void RenderDelayBufferImpl2::IncrementReadIndices() {
+  if (blocks_.read != blocks_.write) {
+    blocks_.IncReadIndex();
+    spectra_.DecReadIndex();
+    ffts_.DecReadIndex();
+  }
+}
+
+// Checks for a render buffer overrun.
+bool RenderDelayBufferImpl2::RenderOverrun() {
+  return low_rate_.read == low_rate_.write || blocks_.read == blocks_.write;
+}
+
+// Checks for a render buffer underrun.
+bool RenderDelayBufferImpl2::RenderUnderrun() {
+  return low_rate_.read == low_rate_.write;
+}
+
+}  // namespace
+
+RenderDelayBuffer* RenderDelayBuffer::Create2(
+    const EchoCanceller3Config& config,
+    size_t num_bands) {
+  return new RenderDelayBufferImpl2(config, num_bands);
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/aec3/render_delay_buffer_unittest.cc b/modules/audio_processing/aec3/render_delay_buffer_unittest.cc
index 78f0b5a..d1530c6 100644
--- a/modules/audio_processing/aec3/render_delay_buffer_unittest.cc
+++ b/modules/audio_processing/aec3/render_delay_buffer_unittest.cc
@@ -11,7 +11,6 @@
 #include "modules/audio_processing/aec3/render_delay_buffer.h"
 
 #include <memory>
-#include <sstream>
 #include <string>
 #include <vector>
 
@@ -19,15 +18,16 @@
 #include "modules/audio_processing/aec3/aec3_common.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
 namespace {
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -38,7 +38,7 @@
   for (auto rate : {8000, 16000, 32000, 48000}) {
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<RenderDelayBuffer> delay_buffer(
-        RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+        RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
     std::vector<std::vector<float>> block_to_insert(
         NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f));
     for (size_t k = 0; k < 10; ++k) {
@@ -62,7 +62,7 @@
 TEST(RenderDelayBuffer, AvailableBlock) {
   constexpr size_t kNumBands = 1;
   std::unique_ptr<RenderDelayBuffer> delay_buffer(
-      RenderDelayBuffer::Create(EchoCanceller3Config(), kNumBands));
+      RenderDelayBuffer::Create2(EchoCanceller3Config(), kNumBands));
   std::vector<std::vector<float>> input_block(
       kNumBands, std::vector<float>(kBlockSize, 1.f));
   EXPECT_EQ(RenderDelayBuffer::BufferingEvent::kNone,
@@ -74,7 +74,7 @@
 TEST(RenderDelayBuffer, SetDelay) {
   EchoCanceller3Config config;
   std::unique_ptr<RenderDelayBuffer> delay_buffer(
-      RenderDelayBuffer::Create(config, 1));
+      RenderDelayBuffer::Create2(config, 1));
   ASSERT_TRUE(delay_buffer->Delay());
   delay_buffer->Reset();
   size_t initial_internal_delay = config.delay.min_echo_path_delay_blocks +
@@ -93,7 +93,7 @@
 // tests on test bots has been fixed.
 TEST(RenderDelayBuffer, DISABLED_WrongDelay) {
   std::unique_ptr<RenderDelayBuffer> delay_buffer(
-      RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+      RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
   EXPECT_DEATH(delay_buffer->SetDelay(21), "");
 }
 
@@ -101,7 +101,7 @@
 TEST(RenderDelayBuffer, WrongNumberOfBands) {
   for (auto rate : {16000, 32000, 48000}) {
     SCOPED_TRACE(ProduceDebugText(rate));
-    std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create(
+    std::unique_ptr<RenderDelayBuffer> delay_buffer(RenderDelayBuffer::Create2(
         EchoCanceller3Config(), NumBandsForRate(rate)));
     std::vector<std::vector<float>> block_to_insert(
         NumBandsForRate(rate < 48000 ? rate + 16000 : 16000),
@@ -115,7 +115,7 @@
   for (auto rate : {8000, 16000, 32000, 48000}) {
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<RenderDelayBuffer> delay_buffer(
-        RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+        RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
     std::vector<std::vector<float>> block_to_insert(
         NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f));
     EXPECT_DEATH(delay_buffer->Insert(block_to_insert), "");
diff --git a/modules/audio_processing/aec3/render_delay_controller.cc b/modules/audio_processing/aec3/render_delay_controller.cc
index 8adf5f5..646acde 100644
--- a/modules/audio_processing/aec3/render_delay_controller.cc
+++ b/modules/audio_processing/aec3/render_delay_controller.cc
@@ -40,6 +40,14 @@
   return static_cast<int>(config.delay.skew_hysteresis_blocks);
 }
 
+bool UseOffsetBlocks() {
+  return field_trial::IsEnabled("WebRTC-Aec3UseOffsetBlocks");
+}
+
+bool UseEarlyDelayDetection() {
+  return !field_trial::IsEnabled("WebRTC-Aec3EarlyDelayDetectionKillSwitch");
+}
+
 constexpr int kSkewHistorySizeLog2 = 8;
 
 class RenderDelayControllerImpl final : public RenderDelayController {
@@ -48,7 +56,7 @@
                             int non_causal_offset,
                             int sample_rate_hz);
   ~RenderDelayControllerImpl() override;
-  void Reset() override;
+  void Reset(bool reset_delay_confidence) override;
   void LogRenderCall() override;
   absl::optional<DelayEstimate> GetDelay(
       const DownsampledRenderBuffer& render_buffer,
@@ -59,10 +67,12 @@
  private:
   static int instance_count_;
   std::unique_ptr<ApmDataDumper> data_dumper_;
+  const bool use_early_delay_detection_;
   const int delay_headroom_blocks_;
   const int hysteresis_limit_1_blocks_;
   const int hysteresis_limit_2_blocks_;
   const int skew_hysteresis_blocks_;
+  const bool use_offset_blocks_;
   absl::optional<DelayEstimate> delay_;
   EchoPathDelayEstimator delay_estimator_;
   std::vector<float> delay_buf_;
@@ -76,6 +86,7 @@
   size_t capture_call_counter_ = 0;
   int delay_change_counter_ = 0;
   size_t soft_reset_counter_ = 0;
+  DelayEstimate::Quality last_delay_estimate_quality_;
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl);
 };
 
@@ -124,6 +135,7 @@
     int sample_rate_hz)
     : data_dumper_(
           new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
+      use_early_delay_detection_(UseEarlyDelayDetection()),
       delay_headroom_blocks_(
           static_cast<int>(config.delay.delay_headroom_blocks)),
       hysteresis_limit_1_blocks_(
@@ -131,9 +143,11 @@
       hysteresis_limit_2_blocks_(
           static_cast<int>(config.delay.hysteresis_limit_2_blocks)),
       skew_hysteresis_blocks_(GetSkewHysteresis(config)),
+      use_offset_blocks_(UseOffsetBlocks()),
       delay_estimator_(data_dumper_.get(), config),
       delay_buf_(kBlockSize * non_causal_offset, 0.f),
-      skew_estimator_(kSkewHistorySizeLog2) {
+      skew_estimator_(kSkewHistorySizeLog2),
+      last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
   RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
   delay_estimator_.LogDelayEstimationProperties(sample_rate_hz,
                                                 delay_buf_.size());
@@ -141,16 +155,19 @@
 
 RenderDelayControllerImpl::~RenderDelayControllerImpl() = default;
 
-void RenderDelayControllerImpl::Reset() {
+void RenderDelayControllerImpl::Reset(bool reset_delay_confidence) {
   delay_ = absl::nullopt;
   delay_samples_ = absl::nullopt;
   skew_ = absl::nullopt;
   previous_offset_blocks_ = 0;
   std::fill(delay_buf_.begin(), delay_buf_.end(), 0.f);
-  delay_estimator_.Reset(false);
+  delay_estimator_.Reset(reset_delay_confidence);
   skew_estimator_.Reset();
   delay_change_counter_ = 0;
   soft_reset_counter_ = 0;
+  if (reset_delay_confidence) {
+    last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
+  }
 }
 
 void RenderDelayControllerImpl::LogRenderCall() {
@@ -188,9 +205,6 @@
   absl::optional<int> skew = skew_estimator_.GetSkewFromCapture();
 
   if (delay_samples) {
-    // TODO(peah): Refactor the rest of the code to assume a kRefined estimate
-    // quality.
-    RTC_DCHECK(DelayEstimate::Quality::kRefined == delay_samples->quality);
     if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
       delay_change_counter_ = 0;
     }
@@ -233,10 +247,12 @@
     } else if (soft_reset_counter_ > 10 * kNumBlocksPerSecond) {
       // Soft reset the delay estimator if there is a significant offset
       // detected.
-      delay_estimator_.Reset(true);
+      delay_estimator_.Reset(false);
       soft_reset_counter_ = 0;
     }
   }
+  if (!use_offset_blocks_)
+    offset_blocks = 0;
 
   // Log any changes in the skew.
   skew_shift_reporting_counter_ =
@@ -256,9 +272,14 @@
 
   if (delay_samples_) {
     // Compute the render delay buffer delay.
-    delay_ = ComputeBufferDelay(
-        delay_, delay_headroom_blocks_, hysteresis_limit_1_blocks_,
-        hysteresis_limit_2_blocks_, offset_blocks, *delay_samples_);
+    const bool use_hysteresis =
+        last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
+        delay_samples_->quality == DelayEstimate::Quality::kRefined;
+    delay_ = ComputeBufferDelay(delay_, delay_headroom_blocks_,
+                                use_hysteresis ? hysteresis_limit_1_blocks_ : 0,
+                                use_hysteresis ? hysteresis_limit_2_blocks_ : 0,
+                                offset_blocks, *delay_samples_);
+    last_delay_estimate_quality_ = delay_samples_->quality;
   }
 
   metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
diff --git a/modules/audio_processing/aec3/render_delay_controller.h b/modules/audio_processing/aec3/render_delay_controller.h
index ddd9548..41ba422 100644
--- a/modules/audio_processing/aec3/render_delay_controller.h
+++ b/modules/audio_processing/aec3/render_delay_controller.h
@@ -27,10 +27,13 @@
   static RenderDelayController* Create(const EchoCanceller3Config& config,
                                        int non_causal_offset,
                                        int sample_rate_hz);
+  static RenderDelayController* Create2(const EchoCanceller3Config& config,
+                                        int sample_rate_hz);
   virtual ~RenderDelayController() = default;
 
-  // Resets the delay controller.
-  virtual void Reset() = 0;
+  // Resets the delay controller. If the delay confidence is reset, the reset
+  // behavior is as if the call is restarted.
+  virtual void Reset(bool reset_delay_confidence) = 0;
 
   // Logs a render call.
   virtual void LogRenderCall() = 0;
diff --git a/modules/audio_processing/aec3/render_delay_controller2.cc b/modules/audio_processing/aec3/render_delay_controller2.cc
new file mode 100644
index 0000000..e27d5f3
--- /dev/null
+++ b/modules/audio_processing/aec3/render_delay_controller2.cc
@@ -0,0 +1,213 @@
+/*
+ *  Copyright (c) 2018 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 <stddef.h>
+#include <algorithm>
+#include <memory>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "api/audio/echo_canceller3_config.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/aec3/delay_estimate.h"
+#include "modules/audio_processing/aec3/downsampled_render_buffer.h"
+#include "modules/audio_processing/aec3/echo_path_delay_estimator.h"
+#include "modules/audio_processing/aec3/render_delay_controller.h"
+#include "modules/audio_processing/aec3/render_delay_controller_metrics.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "rtc_base/atomicops.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/constructormagic.h"
+#include "system_wrappers/include/field_trial.h"
+
+namespace webrtc {
+
+namespace {
+
+bool UseEarlyDelayDetection() {
+  return !field_trial::IsEnabled("WebRTC-Aec3EarlyDelayDetectionKillSwitch");
+}
+
+class RenderDelayControllerImpl2 final : public RenderDelayController {
+ public:
+  RenderDelayControllerImpl2(const EchoCanceller3Config& config,
+                             int sample_rate_hz);
+  ~RenderDelayControllerImpl2() override;
+  void Reset(bool reset_delay_confidence) override;
+  void LogRenderCall() override;
+  absl::optional<DelayEstimate> GetDelay(
+      const DownsampledRenderBuffer& render_buffer,
+      size_t render_delay_buffer_delay,
+      const absl::optional<int>& echo_remover_delay,
+      rtc::ArrayView<const float> capture) override;
+
+ private:
+  static int instance_count_;
+  std::unique_ptr<ApmDataDumper> data_dumper_;
+  const bool use_early_delay_detection_;
+  const int delay_headroom_blocks_;
+  const int hysteresis_limit_1_blocks_;
+  const int hysteresis_limit_2_blocks_;
+  absl::optional<DelayEstimate> delay_;
+  EchoPathDelayEstimator delay_estimator_;
+  RenderDelayControllerMetrics metrics_;
+  absl::optional<DelayEstimate> delay_samples_;
+  size_t capture_call_counter_ = 0;
+  int delay_change_counter_ = 0;
+  DelayEstimate::Quality last_delay_estimate_quality_;
+  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RenderDelayControllerImpl2);
+};
+
+DelayEstimate ComputeBufferDelay(
+    const absl::optional<DelayEstimate>& current_delay,
+    int delay_headroom_blocks,
+    int hysteresis_limit_1_blocks,
+    int hysteresis_limit_2_blocks,
+    DelayEstimate estimated_delay) {
+  // The below division is not exact and the truncation is intended.
+  const int echo_path_delay_blocks = estimated_delay.delay >> kBlockSizeLog2;
+
+  // Compute the buffer delay increase required to achieve the desired latency.
+  size_t new_delay_blocks =
+      std::max(echo_path_delay_blocks - delay_headroom_blocks, 0);
+
+  // Add hysteresis.
+  if (current_delay) {
+    size_t current_delay_blocks = current_delay->delay;
+    if (new_delay_blocks > current_delay_blocks) {
+      if (new_delay_blocks <=
+          current_delay_blocks + hysteresis_limit_1_blocks) {
+        new_delay_blocks = current_delay_blocks;
+      }
+    } else if (new_delay_blocks < current_delay_blocks) {
+      size_t hysteresis_limit = std::max(
+          static_cast<int>(current_delay_blocks) - hysteresis_limit_2_blocks,
+          0);
+      if (new_delay_blocks >= hysteresis_limit) {
+        new_delay_blocks = current_delay_blocks;
+      }
+    }
+  }
+
+  DelayEstimate new_delay = estimated_delay;
+  new_delay.delay = new_delay_blocks;
+  return new_delay;
+}
+
+int RenderDelayControllerImpl2::instance_count_ = 0;
+
+RenderDelayControllerImpl2::RenderDelayControllerImpl2(
+    const EchoCanceller3Config& config,
+    int sample_rate_hz)
+    : data_dumper_(
+          new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
+      use_early_delay_detection_(UseEarlyDelayDetection()),
+      delay_headroom_blocks_(
+          static_cast<int>(config.delay.delay_headroom_blocks)),
+      hysteresis_limit_1_blocks_(
+          static_cast<int>(config.delay.hysteresis_limit_1_blocks)),
+      hysteresis_limit_2_blocks_(
+          static_cast<int>(config.delay.hysteresis_limit_2_blocks)),
+      delay_estimator_(data_dumper_.get(), config),
+      last_delay_estimate_quality_(DelayEstimate::Quality::kCoarse) {
+  RTC_DCHECK(ValidFullBandRate(sample_rate_hz));
+  delay_estimator_.LogDelayEstimationProperties(sample_rate_hz, 0);
+}
+
+RenderDelayControllerImpl2::~RenderDelayControllerImpl2() = default;
+
+void RenderDelayControllerImpl2::Reset(bool reset_delay_confidence) {
+  delay_ = absl::nullopt;
+  delay_samples_ = absl::nullopt;
+  delay_estimator_.Reset(reset_delay_confidence);
+  delay_change_counter_ = 0;
+  if (reset_delay_confidence) {
+    last_delay_estimate_quality_ = DelayEstimate::Quality::kCoarse;
+  }
+}
+
+void RenderDelayControllerImpl2::LogRenderCall() {}
+
+absl::optional<DelayEstimate> RenderDelayControllerImpl2::GetDelay(
+    const DownsampledRenderBuffer& render_buffer,
+    size_t render_delay_buffer_delay,
+    const absl::optional<int>& echo_remover_delay,
+    rtc::ArrayView<const float> capture) {
+  RTC_DCHECK_EQ(kBlockSize, capture.size());
+  ++capture_call_counter_;
+
+  auto delay_samples = delay_estimator_.EstimateDelay(render_buffer, capture);
+
+  // Overrule the delay estimator delay if the echo remover reports a delay.
+  if (echo_remover_delay) {
+    int total_echo_remover_delay_samples =
+        (render_delay_buffer_delay + *echo_remover_delay) * kBlockSize;
+    delay_samples = DelayEstimate(DelayEstimate::Quality::kRefined,
+                                  total_echo_remover_delay_samples);
+  }
+
+  if (delay_samples) {
+    if (!delay_samples_ || delay_samples->delay != delay_samples_->delay) {
+      delay_change_counter_ = 0;
+    }
+    if (delay_samples_) {
+      delay_samples_->blocks_since_last_change =
+          delay_samples_->delay == delay_samples->delay
+              ? delay_samples_->blocks_since_last_change + 1
+              : 0;
+      delay_samples_->blocks_since_last_update = 0;
+      delay_samples_->delay = delay_samples->delay;
+      delay_samples_->quality = delay_samples->quality;
+    } else {
+      delay_samples_ = delay_samples;
+    }
+  } else {
+    if (delay_samples_) {
+      ++delay_samples_->blocks_since_last_change;
+      ++delay_samples_->blocks_since_last_update;
+    }
+  }
+
+  if (delay_change_counter_ < 2 * kNumBlocksPerSecond) {
+    ++delay_change_counter_;
+  }
+
+  if (delay_samples_) {
+    // Compute the render delay buffer delay.
+    const bool use_hysteresis =
+        last_delay_estimate_quality_ == DelayEstimate::Quality::kRefined &&
+        delay_samples_->quality == DelayEstimate::Quality::kRefined;
+    delay_ = ComputeBufferDelay(delay_, delay_headroom_blocks_,
+                                use_hysteresis ? hysteresis_limit_1_blocks_ : 0,
+                                use_hysteresis ? hysteresis_limit_2_blocks_ : 0,
+                                *delay_samples_);
+    last_delay_estimate_quality_ = delay_samples_->quality;
+  }
+
+  metrics_.Update(delay_samples_ ? absl::optional<size_t>(delay_samples_->delay)
+                                 : absl::nullopt,
+                  delay_ ? delay_->delay : 0, 0);
+
+  data_dumper_->DumpRaw("aec3_render_delay_controller_delay",
+                        delay_samples ? delay_samples->delay : 0);
+  data_dumper_->DumpRaw("aec3_render_delay_controller_buffer_delay",
+                        delay_ ? delay_->delay : 0);
+
+  return delay_;
+}
+
+}  // namespace
+
+RenderDelayController* RenderDelayController::Create2(
+    const EchoCanceller3Config& config,
+    int sample_rate_hz) {
+  return new RenderDelayControllerImpl2(config, sample_rate_hz);
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/aec3/render_delay_controller_unittest.cc b/modules/audio_processing/aec3/render_delay_controller_unittest.cc
index 98d5b25..e9f02d3 100644
--- a/modules/audio_processing/aec3/render_delay_controller_unittest.cc
+++ b/modules/audio_processing/aec3/render_delay_controller_unittest.cc
@@ -12,7 +12,6 @@
 
 #include <algorithm>
 #include <memory>
-#include <sstream>
 #include <string>
 #include <vector>
 
@@ -23,21 +22,22 @@
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
 namespace {
 
 std::string ProduceDebugText(int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Sample rate: " << sample_rate_hz;
-  return ss.str();
+  return ss.Release();
 }
 
 std::string ProduceDebugText(int sample_rate_hz, size_t delay) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << ProduceDebugText(sample_rate_hz) << ", Delay: " << delay;
-  return ss.str();
+  return ss.Release();
 }
 
 constexpr size_t kDownSamplingFactors[] = {2, 4, 8};
@@ -57,10 +57,9 @@
       for (auto rate : {8000, 16000, 32000, 48000}) {
         SCOPED_TRACE(ProduceDebugText(rate));
         std::unique_ptr<RenderDelayBuffer> delay_buffer(
-            RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+            RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
         std::unique_ptr<RenderDelayController> delay_controller(
-            RenderDelayController::Create(
-                config, RenderDelayBuffer::DelayEstimatorOffset(config), rate));
+            RenderDelayController::Create2(config, rate));
         for (size_t k = 0; k < 100; ++k) {
           auto delay = delay_controller->GetDelay(
               delay_buffer->GetDownsampledRenderBuffer(), delay_buffer->Delay(),
@@ -87,11 +86,9 @@
         std::vector<std::vector<float>> render_block(
             NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f));
         std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-            RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+            RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
         std::unique_ptr<RenderDelayController> delay_controller(
-            RenderDelayController::Create(
-                EchoCanceller3Config(),
-                RenderDelayBuffer::DelayEstimatorOffset(config), rate));
+            RenderDelayController::Create2(EchoCanceller3Config(), rate));
         for (size_t k = 0; k < 10; ++k) {
           render_delay_buffer->Insert(render_block);
           render_delay_buffer->PrepareCaptureProcessing();
@@ -128,11 +125,9 @@
           absl::optional<DelayEstimate> delay_blocks;
           SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
           std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-              RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+              RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
           std::unique_ptr<RenderDelayController> delay_controller(
-              RenderDelayController::Create(
-                  config, RenderDelayBuffer::DelayEstimatorOffset(config),
-                  rate));
+              RenderDelayController::Create2(config, rate));
           DelayBuffer<float> signal_delay_buffer(delay_samples);
           for (size_t k = 0; k < (400 + delay_samples / kBlockSize); ++k) {
             RandomizeSampleVector(&random_generator, render_block[0]);
@@ -179,11 +174,9 @@
           absl::optional<DelayEstimate> delay_blocks;
           SCOPED_TRACE(ProduceDebugText(rate, -delay_samples));
           std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-              RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+              RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
           std::unique_ptr<RenderDelayController> delay_controller(
-              RenderDelayController::Create(
-                  EchoCanceller3Config(),
-                  RenderDelayBuffer::DelayEstimatorOffset(config), rate));
+              RenderDelayController::Create2(EchoCanceller3Config(), rate));
           DelayBuffer<float> signal_delay_buffer(-delay_samples);
           for (int k = 0;
                k < (400 - delay_samples / static_cast<int>(kBlockSize)); ++k) {
@@ -223,11 +216,9 @@
           absl::optional<DelayEstimate> delay_blocks;
           SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
           std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-              RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+              RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
           std::unique_ptr<RenderDelayController> delay_controller(
-              RenderDelayController::Create(
-                  config, RenderDelayBuffer::DelayEstimatorOffset(config),
-                  rate));
+              RenderDelayController::Create2(config, rate));
           DelayBuffer<float> signal_delay_buffer(delay_samples);
           for (size_t j = 0; j < (1000 + delay_samples / kBlockSize) /
                                          config.delay.api_call_jitter_blocks +
@@ -280,11 +271,10 @@
       for (auto rate : {8000, 16000, 32000, 48000}) {
         SCOPED_TRACE(ProduceDebugText(rate));
         std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-            RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+            RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
 
         std::unique_ptr<RenderDelayController> delay_controller(
-            RenderDelayController::Create(
-                config, RenderDelayBuffer::DelayEstimatorOffset(config), rate));
+            RenderDelayController::Create2(config, rate));
       }
     }
   }
@@ -300,12 +290,10 @@
   for (auto rate : {8000, 16000, 32000, 48000}) {
     SCOPED_TRACE(ProduceDebugText(rate));
     std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-        RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+        RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
     EXPECT_DEATH(
         std::unique_ptr<RenderDelayController>(
-            RenderDelayController::Create(
-                EchoCanceller3Config(),
-                RenderDelayBuffer::DelayEstimatorOffset(config), rate))
+            RenderDelayController::Create2(EchoCanceller3Config(), rate))
             ->GetDelay(render_delay_buffer->GetDownsampledRenderBuffer(),
                        render_delay_buffer->Delay(), echo_remover_delay, block),
         "");
@@ -320,11 +308,10 @@
     SCOPED_TRACE(ProduceDebugText(rate));
     EchoCanceller3Config config;
     std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-        RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
+        RenderDelayBuffer::Create2(config, NumBandsForRate(rate)));
     EXPECT_DEATH(
-        std::unique_ptr<RenderDelayController>(RenderDelayController::Create(
-            EchoCanceller3Config(),
-            RenderDelayBuffer::DelayEstimatorOffset(config), rate)),
+        std::unique_ptr<RenderDelayController>(
+            RenderDelayController::Create2(EchoCanceller3Config(), rate)),
         "");
   }
 }
diff --git a/modules/audio_processing/aec3/render_signal_analyzer_unittest.cc b/modules/audio_processing/aec3/render_signal_analyzer_unittest.cc
index f9b1955..a993f8f 100644
--- a/modules/audio_processing/aec3/render_signal_analyzer_unittest.cc
+++ b/modules/audio_processing/aec3/render_signal_analyzer_unittest.cc
@@ -59,7 +59,7 @@
   std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
   std::array<float, kBlockSize> x_old;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
+      RenderDelayBuffer::Create2(EchoCanceller3Config(), 3));
   std::array<float, kFftLengthBy2Plus1> mask;
   x_old.fill(0.f);
 
@@ -93,7 +93,7 @@
   EchoCanceller3Config config;
   config.delay.min_echo_path_delay_blocks = 0;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
 
   std::array<float, kFftLengthBy2Plus1> mask;
   x_old.fill(0.f);
diff --git a/modules/audio_processing/aec3/residual_echo_estimator.cc b/modules/audio_processing/aec3/residual_echo_estimator.cc
index 2e3ad9f..7b063c1 100644
--- a/modules/audio_processing/aec3/residual_echo_estimator.cc
+++ b/modules/audio_processing/aec3/residual_echo_estimator.cc
@@ -105,9 +105,15 @@
 
   // Estimate the residual echo power.
   if (aec_state.UsableLinearEstimate()) {
-    RTC_DCHECK(!aec_state.SaturatedEcho());
     LinearEstimate(S2_linear, aec_state.Erle(), aec_state.ErleUncertainty(),
                    R2);
+
+    // When there is saturated echo, assume the same spectral content as is
+    // present in the micropone signal.
+    if (aec_state.SaturatedEcho()) {
+      std::copy(Y2.begin(), Y2.end(), R2->begin());
+    }
+
     // Adds the estimated unmodelled echo power to the residual echo power
     // estimate.
     if (echo_reverb_) {
@@ -151,10 +157,10 @@
     }
     NonLinearEstimate(echo_path_gain, X2, Y2, R2);
 
-    // If the echo is saturated, estimate the echo power as the maximum echo
-    // power with a leakage factor.
+    // When there is saturated echo, assume the same spectral content as is
+    // present in the micropone signal.
     if (aec_state.SaturatedEcho()) {
-      R2->fill((*std::max_element(R2->begin(), R2->end())) * 100.f);
+      std::copy(Y2.begin(), Y2.end(), R2->begin());
     }
 
     if (!(aec_state.TransparentMode() && soft_transparent_mode_)) {
diff --git a/modules/audio_processing/aec3/residual_echo_estimator_unittest.cc b/modules/audio_processing/aec3/residual_echo_estimator_unittest.cc
index 832d8ca..2e73a7e 100644
--- a/modules/audio_processing/aec3/residual_echo_estimator_unittest.cc
+++ b/modules/audio_processing/aec3/residual_echo_estimator_unittest.cc
@@ -27,7 +27,7 @@
   EchoCanceller3Config config;
   AecState aec_state(config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   std::vector<std::array<float, kFftLengthBy2Plus1>> H2;
   std::array<float, kFftLengthBy2Plus1> S2_linear;
   std::array<float, kFftLengthBy2Plus1> Y2;
@@ -48,7 +48,7 @@
   ResidualEchoEstimator estimator(config);
   AecState aec_state(config);
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
 
   std::array<float, kFftLengthBy2Plus1> E2_main;
   std::array<float, kFftLengthBy2Plus1> E2_shadow;
diff --git a/modules/audio_processing/aec3/shadow_filter_update_gain_unittest.cc b/modules/audio_processing/aec3/shadow_filter_update_gain_unittest.cc
index d77da33..017c679 100644
--- a/modules/audio_processing/aec3/shadow_filter_update_gain_unittest.cc
+++ b/modules/audio_processing/aec3/shadow_filter_update_gain_unittest.cc
@@ -22,6 +22,7 @@
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/numerics/safe_minmax.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -52,7 +53,7 @@
   config.delay.min_echo_path_delay_blocks = 0;
   config.delay.default_delay = 1;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
 
   std::array<float, kBlockSize> x_old;
   x_old.fill(0.f);
@@ -115,16 +116,16 @@
 }
 
 std::string ProduceDebugText(int filter_length_blocks) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Length: " << filter_length_blocks;
-  return ss.str();
+  return ss.Release();
 }
 
 std::string ProduceDebugText(size_t delay, int filter_length_blocks) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Delay: " << delay << ", ";
   ss << ProduceDebugText(filter_length_blocks);
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
diff --git a/modules/audio_processing/aec3/subband_erle_estimator.cc b/modules/audio_processing/aec3/subband_erle_estimator.cc
new file mode 100644
index 0000000..d8cb7a7
--- /dev/null
+++ b/modules/audio_processing/aec3/subband_erle_estimator.cc
@@ -0,0 +1,187 @@
+/*
+ *  Copyright (c) 2018 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 "modules/audio_processing/aec3/subband_erle_estimator.h"
+
+#include <algorithm>
+#include <memory>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "rtc_base/numerics/safe_minmax.h"
+#include "system_wrappers/include/field_trial.h"
+
+namespace webrtc {
+
+namespace {
+constexpr int kPointsToAccumulate = 6;
+constexpr float kX2BandEnergyThreshold = 44015068.0f;
+constexpr int kErleHold = 100;
+constexpr int kBlocksForOnsetDetection = kErleHold + 150;
+
+bool EnableAdaptErleOnLowRender() {
+  return !field_trial::IsEnabled("WebRTC-Aec3AdaptErleOnLowRenderKillSwitch");
+}
+
+}  // namespace
+
+SubbandErleEstimator::SubbandErleEstimator(float min_erle,
+                                           float max_erle_lf,
+                                           float max_erle_hf)
+    : min_erle_(min_erle),
+      max_erle_lf_(max_erle_lf),
+      max_erle_hf_(max_erle_hf),
+      adapt_on_low_render_(EnableAdaptErleOnLowRender()) {
+  Reset();
+}
+
+SubbandErleEstimator::~SubbandErleEstimator() = default;
+
+void SubbandErleEstimator::Reset() {
+  erle_.fill(min_erle_);
+  erle_onsets_.fill(min_erle_);
+  hold_counters_.fill(0);
+  coming_onset_.fill(true);
+}
+
+void SubbandErleEstimator::Update(rtc::ArrayView<const float> X2,
+                                  rtc::ArrayView<const float> Y2,
+                                  rtc::ArrayView<const float> E2,
+                                  bool converged_filter,
+                                  bool onset_detection) {
+  if (converged_filter) {
+    // Note that the use of the converged_filter flag already imposed
+    // a minimum of the erle that can be estimated as that flag would
+    // be false if the filter is performing poorly.
+    constexpr size_t kFftLengthBy4 = kFftLengthBy2 / 2;
+    UpdateBands(X2, Y2, E2, 1, kFftLengthBy4, max_erle_lf_, onset_detection);
+    UpdateBands(X2, Y2, E2, kFftLengthBy4, kFftLengthBy2, max_erle_hf_,
+                onset_detection);
+  }
+
+  if (onset_detection) {
+    DecreaseErlePerBandForLowRenderSignals();
+  }
+
+  erle_[0] = erle_[1];
+  erle_[kFftLengthBy2] = erle_[kFftLengthBy2 - 1];
+}
+
+void SubbandErleEstimator::Dump(
+    const std::unique_ptr<ApmDataDumper>& data_dumper) const {
+  data_dumper->DumpRaw("aec3_erle", Erle());
+  data_dumper->DumpRaw("aec3_erle_onset", ErleOnsets());
+}
+
+void SubbandErleEstimator::UpdateBands(rtc::ArrayView<const float> X2,
+                                       rtc::ArrayView<const float> Y2,
+                                       rtc::ArrayView<const float> E2,
+                                       size_t start,
+                                       size_t stop,
+                                       float max_erle,
+                                       bool onset_detection) {
+  auto erle_band_update = [](float erle_band, float new_erle,
+                             bool low_render_energy, float alpha_inc,
+                             float alpha_dec, float min_erle, float max_erle) {
+    if (new_erle < erle_band && low_render_energy) {
+      // Decreases are not allowed if low render energy signals were used for
+      // the erle computation.
+      return erle_band;
+    }
+    float alpha = new_erle > erle_band ? alpha_inc : alpha_dec;
+    float erle_band_out = erle_band;
+    erle_band_out = erle_band + alpha * (new_erle - erle_band);
+    erle_band_out = rtc::SafeClamp(erle_band_out, min_erle, max_erle);
+    return erle_band_out;
+  };
+
+  for (size_t k = start; k < stop; ++k) {
+    if (adapt_on_low_render_ || X2[k] > kX2BandEnergyThreshold) {
+      bool low_render_energy = false;
+      absl::optional<float> new_erle = instantaneous_erle_.Update(
+          X2[k], Y2[k], E2[k], k, &low_render_energy);
+      if (new_erle) {
+        RTC_DCHECK(adapt_on_low_render_ || !low_render_energy);
+        if (onset_detection && !low_render_energy) {
+          if (coming_onset_[k]) {
+            coming_onset_[k] = false;
+            erle_onsets_[k] = erle_band_update(
+                erle_onsets_[k], new_erle.value(), low_render_energy, 0.15f,
+                0.3f, min_erle_, max_erle);
+          }
+          hold_counters_[k] = kBlocksForOnsetDetection;
+        }
+
+        erle_[k] =
+            erle_band_update(erle_[k], new_erle.value(), low_render_energy,
+                             0.05f, 0.1f, min_erle_, max_erle);
+      }
+    }
+  }
+}
+
+void SubbandErleEstimator::DecreaseErlePerBandForLowRenderSignals() {
+  for (size_t k = 1; k < kFftLengthBy2; ++k) {
+    hold_counters_[k]--;
+    if (hold_counters_[k] <= (kBlocksForOnsetDetection - kErleHold)) {
+      if (erle_[k] > erle_onsets_[k]) {
+        erle_[k] = std::max(erle_onsets_[k], 0.97f * erle_[k]);
+        RTC_DCHECK_LE(min_erle_, erle_[k]);
+      }
+      if (hold_counters_[k] <= 0) {
+        coming_onset_[k] = true;
+        hold_counters_[k] = 0;
+      }
+    }
+  }
+}
+
+SubbandErleEstimator::ErleInstantaneous::ErleInstantaneous() {
+  Reset();
+}
+
+SubbandErleEstimator::ErleInstantaneous::~ErleInstantaneous() = default;
+
+absl::optional<float> SubbandErleEstimator::ErleInstantaneous::Update(
+    float X2,
+    float Y2,
+    float E2,
+    size_t band,
+    bool* low_render_energy) {
+  absl::optional<float> erle_instantaneous = absl::nullopt;
+  RTC_DCHECK_LT(band, kFftLengthBy2Plus1);
+  Y2_acum_[band] += Y2;
+  E2_acum_[band] += E2;
+  low_render_energy_[band] =
+      low_render_energy_[band] || X2 < kX2BandEnergyThreshold;
+  if (++num_points_[band] == kPointsToAccumulate) {
+    if (E2_acum_[band]) {
+      erle_instantaneous = Y2_acum_[band] / E2_acum_[band];
+    }
+    *low_render_energy = low_render_energy_[band];
+    num_points_[band] = 0;
+    Y2_acum_[band] = 0.f;
+    E2_acum_[band] = 0.f;
+    low_render_energy_[band] = false;
+  }
+
+  return erle_instantaneous;
+}
+
+void SubbandErleEstimator::ErleInstantaneous::Reset() {
+  Y2_acum_.fill(0.f);
+  E2_acum_.fill(0.f);
+  low_render_energy_.fill(false);
+  num_points_.fill(0);
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/aec3/subband_erle_estimator.h b/modules/audio_processing/aec3/subband_erle_estimator.h
new file mode 100644
index 0000000..aa5e5cc
--- /dev/null
+++ b/modules/audio_processing/aec3/subband_erle_estimator.h
@@ -0,0 +1,97 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
+#define MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
+
+#include <array>
+#include <memory>
+
+#include "absl/types/optional.h"
+#include "api/array_view.h"
+#include "modules/audio_processing/aec3/aec3_common.h"
+#include "modules/audio_processing/logging/apm_data_dumper.h"
+
+namespace webrtc {
+
+// Estimates the echo return loss enhancement for each frequency subband.
+class SubbandErleEstimator {
+ public:
+  SubbandErleEstimator(float min_erle, float max_erle_lf, float max_erle_hf);
+  ~SubbandErleEstimator();
+
+  // Resets the ERLE estimator.
+  void Reset();
+
+  // Updates the ERLE estimate.
+  void Update(rtc::ArrayView<const float> X2,
+              rtc::ArrayView<const float> Y2,
+              rtc::ArrayView<const float> E2,
+              bool converged_filter,
+              bool onset_detection);
+
+  // Returns the ERLE estimate.
+  const std::array<float, kFftLengthBy2Plus1>& Erle() const { return erle_; }
+
+  // Returns the ERLE estimate at onsets.
+  const std::array<float, kFftLengthBy2Plus1>& ErleOnsets() const {
+    return erle_onsets_;
+  }
+
+  void Dump(const std::unique_ptr<ApmDataDumper>& data_dumper) const;
+
+ private:
+  void UpdateBands(rtc::ArrayView<const float> X2,
+                   rtc::ArrayView<const float> Y2,
+                   rtc::ArrayView<const float> E2,
+                   size_t start,
+                   size_t stop,
+                   float max_erle,
+                   bool onset_detection);
+  void DecreaseErlePerBandForLowRenderSignals();
+
+  class ErleInstantaneous {
+   public:
+    ErleInstantaneous();
+    ~ErleInstantaneous();
+    // Updates the ERLE for a band with a new block. Returns absl::nullopt
+    // if not enough points were accumulated for doing the estimation,
+    // otherwise, it returns the ERLE. When the ERLE is returned, the
+    // low_render_energy flag contains information on whether the estimation was
+    // done using low level render signals.
+    absl::optional<float> Update(float X2,
+                                 float Y2,
+                                 float E2,
+                                 size_t band,
+                                 bool* low_render_energy);
+    // Resets the ERLE estimator to its initial state.
+    void Reset();
+
+   private:
+    std::array<float, kFftLengthBy2Plus1> Y2_acum_;
+    std::array<float, kFftLengthBy2Plus1> E2_acum_;
+    std::array<bool, kFftLengthBy2Plus1> low_render_energy_;
+    std::array<int, kFftLengthBy2Plus1> num_points_;
+  };
+
+  ErleInstantaneous instantaneous_erle_;
+  std::array<float, kFftLengthBy2Plus1> erle_;
+  std::array<float, kFftLengthBy2Plus1> erle_onsets_;
+  std::array<bool, kFftLengthBy2Plus1> coming_onset_;
+  std::array<int, kFftLengthBy2Plus1> hold_counters_;
+  const float min_erle_;
+  const float max_erle_lf_;
+  const float max_erle_hf_;
+  const bool adapt_on_low_render_;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_AUDIO_PROCESSING_AEC3_SUBBAND_ERLE_ESTIMATOR_H_
diff --git a/modules/audio_processing/aec3/subtractor.cc b/modules/audio_processing/aec3/subtractor.cc
index 609e8ac..9856a74 100644
--- a/modules/audio_processing/aec3/subtractor.cc
+++ b/modules/audio_processing/aec3/subtractor.cc
@@ -191,7 +191,7 @@
                   adaptation_during_saturation_, &shadow_saturation);
 
   // Compute the signal powers in the subtractor output.
-  output->UpdatePowers(y);
+  output->ComputeMetrics(y);
 
   // Adjust the filter if needed.
   bool main_filter_adjusted = false;
diff --git a/modules/audio_processing/aec3/subtractor_output.cc b/modules/audio_processing/aec3/subtractor_output.cc
index affa4a3..922cc3d 100644
--- a/modules/audio_processing/aec3/subtractor_output.cc
+++ b/modules/audio_processing/aec3/subtractor_output.cc
@@ -33,7 +33,7 @@
   y2 = 0.f;
 }
 
-void SubtractorOutput::UpdatePowers(rtc::ArrayView<const float> y) {
+void SubtractorOutput::ComputeMetrics(rtc::ArrayView<const float> y) {
   const auto sum_of_squares = [](float a, float b) { return a + b * b; };
   y2 = std::accumulate(y.begin(), y.end(), 0.f, sum_of_squares);
   e2_main = std::accumulate(e_main.begin(), e_main.end(), 0.f, sum_of_squares);
@@ -42,6 +42,14 @@
   s2_main = std::accumulate(s_main.begin(), s_main.end(), 0.f, sum_of_squares);
   s2_shadow =
       std::accumulate(s_shadow.begin(), s_shadow.end(), 0.f, sum_of_squares);
+
+  s_main_max_abs = *std::max_element(s_main.begin(), s_main.end());
+  s_main_max_abs = std::max(s_main_max_abs,
+                            -(*std::min_element(s_main.begin(), s_main.end())));
+
+  s_shadow_max_abs = *std::max_element(s_shadow.begin(), s_shadow.end());
+  s_shadow_max_abs = std::max(
+      s_shadow_max_abs, -(*std::min_element(s_shadow.begin(), s_shadow.end())));
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/subtractor_output.h b/modules/audio_processing/aec3/subtractor_output.h
index 89727e9..5f6fd3e 100644
--- a/modules/audio_processing/aec3/subtractor_output.h
+++ b/modules/audio_processing/aec3/subtractor_output.h
@@ -36,12 +36,14 @@
   float e2_main = 0.f;
   float e2_shadow = 0.f;
   float y2 = 0.f;
+  float s_main_max_abs = 0.f;
+  float s_shadow_max_abs = 0.f;
 
   // Reset the struct content.
   void Reset();
 
   // Updates the powers of the signals.
-  void UpdatePowers(rtc::ArrayView<const float> y);
+  void ComputeMetrics(rtc::ArrayView<const float> y);
 };
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/subtractor_unittest.cc b/modules/audio_processing/aec3/subtractor_unittest.cc
index 7791805..8d14cc1 100644
--- a/modules/audio_processing/aec3/subtractor_unittest.cc
+++ b/modules/audio_processing/aec3/subtractor_unittest.cc
@@ -18,6 +18,7 @@
 #include "modules/audio_processing/aec3/render_delay_buffer.h"
 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
 #include "rtc_base/random.h"
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -43,7 +44,7 @@
   config.delay.min_echo_path_delay_blocks = 0;
   config.delay.default_delay = 1;
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   RenderSignalAnalyzer render_signal_analyzer(config);
   Random random_generator(42U);
   Aec3Fft fft;
@@ -102,10 +103,10 @@
 }
 
 std::string ProduceDebugText(size_t delay, int filter_length_blocks) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << "Delay: " << delay << ", ";
   ss << "Length: " << filter_length_blocks;
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace
@@ -126,7 +127,7 @@
   EchoCanceller3Config config;
   Subtractor subtractor(config, &data_dumper, DetectOptimization());
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   RenderSignalAnalyzer render_signal_analyzer(config);
   std::vector<float> y(kBlockSize, 0.f);
 
@@ -142,7 +143,7 @@
   EchoCanceller3Config config;
   Subtractor subtractor(config, &data_dumper, DetectOptimization());
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   RenderSignalAnalyzer render_signal_analyzer(config);
   std::vector<float> y(kBlockSize - 1, 0.f);
   SubtractorOutput output;
@@ -209,21 +210,4 @@
   }
 }
 
-// Verifies that the subtractor is properly reset when there is an echo path
-// change.
-TEST(Subtractor, EchoPathChangeReset) {
-  std::vector<int> blocks_with_echo_path_changes;
-  blocks_with_echo_path_changes.push_back(99);
-  for (size_t filter_length_blocks : {12, 20, 30}) {
-    for (size_t delay_samples : {0, 64, 150, 200, 301}) {
-      SCOPED_TRACE(ProduceDebugText(delay_samples, filter_length_blocks));
-
-      float echo_to_nearend_power = RunSubtractorTest(
-          100, delay_samples, filter_length_blocks, filter_length_blocks, false,
-          blocks_with_echo_path_changes);
-      EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
-    }
-  }
-}
-
 }  // namespace webrtc
diff --git a/modules/audio_processing/aec3/suppression_filter.cc b/modules/audio_processing/aec3/suppression_filter.cc
index 87e3008..35b7b8c 100644
--- a/modules/audio_processing/aec3/suppression_filter.cc
+++ b/modules/audio_processing/aec3/suppression_filter.cc
@@ -12,11 +12,14 @@
 
 #include <math.h>
 #include <algorithm>
+#include <cmath>
 #include <cstring>
 #include <functional>
 #include <numeric>
 
+#include "modules/audio_processing/aec3/vector_math.h"
 #include "modules/audio_processing/utility/ooura_fft.h"
+#include "rtc_base/checks.h"
 #include "rtc_base/numerics/safe_minmax.h"
 
 namespace webrtc {
@@ -59,8 +62,10 @@
 
 }  // namespace
 
-SuppressionFilter::SuppressionFilter(int sample_rate_hz)
-    : sample_rate_hz_(sample_rate_hz),
+SuppressionFilter::SuppressionFilter(Aec3Optimization optimization,
+                                     int sample_rate_hz)
+    : optimization_(optimization),
+      sample_rate_hz_(sample_rate_hz),
       fft_(),
       e_output_old_(NumBandsForRate(sample_rate_hz_)) {
   RTC_DCHECK(ValidFullBandRate(sample_rate_hz_));
@@ -90,18 +95,17 @@
   std::transform(suppression_gain.begin(), suppression_gain.end(), E.im.begin(),
                  E.im.begin(), std::multiplies<float>());
 
-  // Compute and add the comfort noise.
-  std::array<float, kFftLengthBy2Plus1> scaled_comfort_noise;
+  // Comfort noise gain is sqrt(1-g^2), where g is the suppression gain.
+  std::array<float, kFftLengthBy2Plus1> noise_gain;
   std::transform(suppression_gain.begin(), suppression_gain.end(),
-                 comfort_noise.re.begin(), scaled_comfort_noise.begin(),
-                 [](float a, float b) { return std::max(1.f - a, 0.f) * b; });
-  std::transform(scaled_comfort_noise.begin(), scaled_comfort_noise.end(),
-                 E.re.begin(), E.re.begin(), std::plus<float>());
-  std::transform(suppression_gain.begin(), suppression_gain.end(),
-                 comfort_noise.im.begin(), scaled_comfort_noise.begin(),
-                 [](float a, float b) { return std::max(1.f - a, 0.f) * b; });
-  std::transform(scaled_comfort_noise.begin(), scaled_comfort_noise.end(),
-                 E.im.begin(), E.im.begin(), std::plus<float>());
+                 noise_gain.begin(), [](float g) { return 1.f - g * g; });
+  aec3::VectorMath(optimization_).Sqrt(noise_gain);
+
+  // Scale and add the comfort noise.
+  for (size_t k = 0; k < kFftLengthBy2Plus1; k++) {
+    E.re[k] += noise_gain[k] * comfort_noise.re[k];
+    E.im[k] += noise_gain[k] * comfort_noise.im[k];
+  }
 
   // Synthesis filterbank.
   std::array<float, kFftLength> e_extended;
@@ -135,7 +139,7 @@
 
     // Scale and apply the noise to the signals.
     const float high_bands_noise_scaling =
-        0.4f * std::max(1.f - high_bands_gain, 0.f);
+        0.4f * std::sqrt(1.f - high_bands_gain * high_bands_gain);
 
     std::transform(
         (*e)[1].begin(), (*e)[1].end(), time_domain_high_band_noise.begin(),
diff --git a/modules/audio_processing/aec3/suppression_filter.h b/modules/audio_processing/aec3/suppression_filter.h
index 237408d..1628e3e 100644
--- a/modules/audio_processing/aec3/suppression_filter.h
+++ b/modules/audio_processing/aec3/suppression_filter.h
@@ -22,7 +22,7 @@
 
 class SuppressionFilter {
  public:
-  explicit SuppressionFilter(int sample_rate_hz);
+  SuppressionFilter(Aec3Optimization optimization, int sample_rate_hz);
   ~SuppressionFilter();
   void ApplyGain(const FftData& comfort_noise,
                  const FftData& comfort_noise_high_bands,
@@ -32,6 +32,7 @@
                  std::vector<std::vector<float>>* e);
 
  private:
+  const Aec3Optimization optimization_;
   const int sample_rate_hz_;
   const OouraFft ooura_fft_;
   const Aec3Fft fft_;
diff --git a/modules/audio_processing/aec3/suppression_filter_unittest.cc b/modules/audio_processing/aec3/suppression_filter_unittest.cc
index eaa608e..9e4ff7c 100644
--- a/modules/audio_processing/aec3/suppression_filter_unittest.cc
+++ b/modules/audio_processing/aec3/suppression_filter_unittest.cc
@@ -45,21 +45,21 @@
   FftData E;
   std::array<float, kFftLengthBy2Plus1> gain;
 
-  EXPECT_DEATH(SuppressionFilter(16000).ApplyGain(cn, cn_high_bands, gain, 1.0f,
-                                                  E, nullptr),
+  EXPECT_DEATH(SuppressionFilter(Aec3Optimization::kNone, 16000)
+                   .ApplyGain(cn, cn_high_bands, gain, 1.0f, E, nullptr),
                "");
 }
 
 // Verifies the check for allowed sample rate.
 TEST(SuppressionFilter, ProperSampleRate) {
-  EXPECT_DEATH(SuppressionFilter(16001), "");
+  EXPECT_DEATH(SuppressionFilter(Aec3Optimization::kNone, 16001), "");
 }
 
 #endif
 
 // Verifies that no comfort noise is added when the gain is 1.
 TEST(SuppressionFilter, ComfortNoiseInUnityGain) {
-  SuppressionFilter filter(48000);
+  SuppressionFilter filter(Aec3Optimization::kNone, 48000);
   FftData cn;
   FftData cn_high_bands;
   std::array<float, kFftLengthBy2Plus1> gain;
@@ -89,7 +89,7 @@
 
 // Verifies that the suppressor is able to suppress a signal.
 TEST(SuppressionFilter, SignalSuppression) {
-  SuppressionFilter filter(48000);
+  SuppressionFilter filter(Aec3Optimization::kNone, 48000);
   FftData cn;
   FftData cn_high_bands;
   std::array<float, kFftLengthBy2> e_old_;
@@ -131,7 +131,7 @@
 // Verifies that the suppressor is able to pass through a desired signal while
 // applying suppressing for some frequencies.
 TEST(SuppressionFilter, SignalTransparency) {
-  SuppressionFilter filter(48000);
+  SuppressionFilter filter(Aec3Optimization::kNone, 48000);
   FftData cn;
   std::array<float, kFftLengthBy2> e_old_;
   Aec3Fft fft;
@@ -171,7 +171,7 @@
 
 // Verifies that the suppressor delay.
 TEST(SuppressionFilter, Delay) {
-  SuppressionFilter filter(48000);
+  SuppressionFilter filter(Aec3Optimization::kNone, 48000);
   FftData cn;
   FftData cn_high_bands;
   std::array<float, kFftLengthBy2> e_old_;
diff --git a/modules/audio_processing/aec3/suppression_gain.cc b/modules/audio_processing/aec3/suppression_gain.cc
index c389a6a..ce7ea33 100644
--- a/modules/audio_processing/aec3/suppression_gain.cc
+++ b/modules/audio_processing/aec3/suppression_gain.cc
@@ -50,16 +50,13 @@
 // Scales the echo according to assessed audibility at the other end.
 void WeightEchoForAudibility(const EchoCanceller3Config& config,
                              rtc::ArrayView<const float> echo,
-                             rtc::ArrayView<float> weighted_echo,
-                             rtc::ArrayView<float> one_by_weighted_echo) {
+                             rtc::ArrayView<float> weighted_echo) {
   RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size());
   RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size());
-  RTC_DCHECK_EQ(kFftLengthBy2Plus1, one_by_weighted_echo.size());
 
   auto weigh = [](float threshold, float normalizer, size_t begin, size_t end,
                   rtc::ArrayView<const float> echo,
-                  rtc::ArrayView<float> weighted_echo,
-                  rtc::ArrayView<float> one_by_weighted_echo) {
+                  rtc::ArrayView<float> weighted_echo) {
     for (size_t k = begin; k < end; ++k) {
       if (echo[k] < threshold) {
         float tmp = (threshold - echo[k]) * normalizer;
@@ -67,26 +64,23 @@
       } else {
         weighted_echo[k] = echo[k];
       }
-      one_by_weighted_echo[k] =
-          weighted_echo[k] > 0.f ? 1.f / weighted_echo[k] : 1.f;
     }
   };
 
   float threshold = config.echo_audibility.floor_power *
                     config.echo_audibility.audibility_threshold_lf;
   float normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
-  weigh(threshold, normalizer, 0, 3, echo, weighted_echo, one_by_weighted_echo);
+  weigh(threshold, normalizer, 0, 3, echo, weighted_echo);
 
   threshold = config.echo_audibility.floor_power *
               config.echo_audibility.audibility_threshold_mf;
   normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
-  weigh(threshold, normalizer, 3, 7, echo, weighted_echo, one_by_weighted_echo);
+  weigh(threshold, normalizer, 3, 7, echo, weighted_echo);
 
   threshold = config.echo_audibility.floor_power *
               config.echo_audibility.audibility_threshold_hf;
   normalizer = 1.f / (threshold - config.echo_audibility.floor_power);
-  weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo,
-        one_by_weighted_echo);
+  weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo);
 }
 
 // Computes the gain to reduce the echo to a non audible level.
@@ -256,75 +250,98 @@
   }
 }
 
-// TODO(peah): Add further optimizations, in particular for the divisions.
-void SuppressionGain::LowerBandGain(
+// Compute the minimum gain as the attenuating gain to put the signal just
+// above the zero sample values.
+void SuppressionGain::GetMinGain(
+    rtc::ArrayView<const float> suppressor_input,
+    rtc::ArrayView<const float> weighted_residual_echo,
     bool low_noise_render,
-    const AecState& aec_state,
-    const std::array<float, kFftLengthBy2Plus1>& nearend,
-    const std::array<float, kFftLengthBy2Plus1>& echo,
-    const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
-    std::array<float, kFftLengthBy2Plus1>* gain) {
-  const bool saturated_echo = aec_state.SaturatedEcho();
-  const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
-  const auto& params = dominant_nearend_detector_.IsNearendState()
-                           ? nearend_params_
-                           : normal_params_;
-
-  // Weight echo power in terms of audibility. // Precompute 1/weighted echo
-  // (note that when the echo is zero, the precomputed value is never used).
-  std::array<float, kFftLengthBy2Plus1> weighted_echo;
-  std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
-  WeightEchoForAudibility(config_, echo, weighted_echo, one_by_weighted_echo);
-
-  // Compute the minimum gain as the attenuating gain to put the signal just
-  // above the zero sample values.
-  std::array<float, kFftLengthBy2Plus1> min_gain;
-  const float min_echo_power =
-      low_noise_render ? config_.echo_audibility.low_render_limit
-                       : config_.echo_audibility.normal_render_limit;
+    bool saturated_echo,
+    rtc::ArrayView<float> min_gain) const {
   if (!saturated_echo) {
-    for (size_t k = 0; k < nearend.size(); ++k) {
-      const float denom = std::min(nearend[k], weighted_echo[k]);
+    const float min_echo_power =
+        low_noise_render ? config_.echo_audibility.low_render_limit
+                         : config_.echo_audibility.normal_render_limit;
+
+    for (size_t k = 0; k < suppressor_input.size(); ++k) {
+      const float denom =
+          std::min(suppressor_input[k], weighted_residual_echo[k]);
       min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f;
       min_gain[k] = std::min(min_gain[k], 1.f);
     }
     for (size_t k = 0; k < 6; ++k) {
+      const auto& dec = dominant_nearend_detector_.IsNearendState()
+                            ? nearend_params_.max_dec_factor_lf
+                            : normal_params_.max_dec_factor_lf;
+
       // Make sure the gains of the low frequencies do not decrease too
       // quickly after strong nearend.
       if (last_nearend_[k] > last_echo_[k]) {
-        min_gain[k] =
-            std::max(min_gain[k], last_gain_[k] * params.max_dec_factor_lf);
+        min_gain[k] = std::max(min_gain[k], last_gain_[k] * dec);
         min_gain[k] = std::min(min_gain[k], 1.f);
       }
     }
   } else {
-    min_gain.fill(0.f);
+    std::fill(min_gain.begin(), min_gain.end(), 0.f);
   }
+}
 
-  // Compute the maximum gain by limiting the gain increase from the previous
-  // gain.
-  std::array<float, kFftLengthBy2Plus1> max_gain;
-  for (size_t k = 0; k < gain->size(); ++k) {
-    max_gain[k] = std::min(std::max(last_gain_[k] * params.max_inc_factor,
-                                    config_.suppressor.floor_first_increase),
-                           1.f);
+// Compute the maximum gain by limiting the gain increase from the previous
+// gain.
+void SuppressionGain::GetMaxGain(rtc::ArrayView<float> max_gain) const {
+  const auto& inc = dominant_nearend_detector_.IsNearendState()
+                        ? nearend_params_.max_inc_factor
+                        : normal_params_.max_inc_factor;
+  const auto& floor = config_.suppressor.floor_first_increase;
+  for (size_t k = 0; k < max_gain.size(); ++k) {
+    max_gain[k] = std::min(std::max(last_gain_[k] * inc, floor), 1.f);
   }
+}
+
+// TODO(peah): Add further optimizations, in particular for the divisions.
+void SuppressionGain::LowerBandGain(
+    bool low_noise_render,
+    const AecState& aec_state,
+    const std::array<float, kFftLengthBy2Plus1>& suppressor_input,
+    const std::array<float, kFftLengthBy2Plus1>& nearend,
+    const std::array<float, kFftLengthBy2Plus1>& residual_echo,
+    const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
+    std::array<float, kFftLengthBy2Plus1>* gain) {
+  const bool saturated_echo = aec_state.SaturatedEcho();
+
+  // Weight echo power in terms of audibility. // Precompute 1/weighted echo
+  // (note that when the echo is zero, the precomputed value is never used).
+  std::array<float, kFftLengthBy2Plus1> weighted_residual_echo;
+  WeightEchoForAudibility(config_, residual_echo, weighted_residual_echo);
+
+  std::array<float, kFftLengthBy2Plus1> min_gain;
+  GetMinGain(suppressor_input, weighted_residual_echo, low_noise_render,
+             saturated_echo, min_gain);
+
+  std::array<float, kFftLengthBy2Plus1> max_gain;
+  GetMaxGain(max_gain);
 
   // Iteratively compute the gain required to attenuate the echo to a non
   // noticeable level.
-  std::array<float, kFftLengthBy2Plus1> masker;
+
   if (enable_new_suppression_) {
-    GainToNoAudibleEcho(nearend, weighted_echo, comfort_noise, min_gain,
-                        max_gain, gain);
+    GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise,
+                        min_gain, max_gain, gain);
     AdjustForExternalFilters(gain);
   } else {
+    const bool linear_echo_estimate = aec_state.UsableLinearEstimate();
+    std::array<float, kFftLengthBy2Plus1> masker;
+    std::array<float, kFftLengthBy2Plus1> one_by_weighted_echo;
+    std::transform(weighted_residual_echo.begin(), weighted_residual_echo.end(),
+                   one_by_weighted_echo.begin(),
+                   [](float e) { return e > 0.f ? 1.f / e : 1.f; });
     gain->fill(0.f);
     for (int k = 0; k < 2; ++k) {
       std::copy(comfort_noise.begin(), comfort_noise.end(), masker.begin());
       GainToNoAudibleEchoFallback(config_, low_noise_render, saturated_echo,
-                                  linear_echo_estimate, nearend, weighted_echo,
-                                  masker, min_gain, max_gain,
-                                  one_by_weighted_echo, gain);
+                                  linear_echo_estimate, nearend,
+                                  weighted_residual_echo, masker, min_gain,
+                                  max_gain, one_by_weighted_echo, gain);
       AdjustForExternalFilters(gain);
     }
   }
@@ -334,14 +351,16 @@
 
   // Store data required for the gain computation of the next block.
   std::copy(nearend.begin(), nearend.end(), last_nearend_.begin());
-  std::copy(weighted_echo.begin(), weighted_echo.end(), last_echo_.begin());
+  std::copy(weighted_residual_echo.begin(), weighted_residual_echo.end(),
+            last_echo_.begin());
   std::copy(gain->begin(), gain->end(), last_gain_.begin());
   aec3::VectorMath(optimization_).Sqrt(*gain);
 
   // Debug outputs for the purpose of development and analysis.
   data_dumper_->DumpRaw("aec3_suppressor_min_gain", min_gain);
   data_dumper_->DumpRaw("aec3_suppressor_max_gain", max_gain);
-  data_dumper_->DumpRaw("aec3_suppressor_masker", masker);
+  data_dumper_->DumpRaw("aec3_dominant_nearend",
+                        dominant_nearend_detector_.IsNearendState());
 }
 
 SuppressionGain::SuppressionGain(const EchoCanceller3Config& config,
@@ -370,6 +389,7 @@
 SuppressionGain::~SuppressionGain() = default;
 
 void SuppressionGain::GetGain(
+    const std::array<float, kFftLengthBy2Plus1>& suppressor_input_spectrum,
     const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
     const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
     const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
@@ -396,14 +416,13 @@
 
   // Update the state selection.
   dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum,
-                                    comfort_noise_spectrum);
+                                    comfort_noise_spectrum, initial_state_);
 
   // Compute gain for the lower band.
   bool low_noise_render = low_render_detector_.Detect(render);
-  const absl::optional<int> narrow_peak_band =
-      render_signal_analyzer.NarrowPeakBand();
-  LowerBandGain(low_noise_render, aec_state, nearend_average,
-                residual_echo_spectrum, comfort_noise_spectrum, low_band_gain);
+  LowerBandGain(low_noise_render, aec_state, suppressor_input_spectrum,
+                nearend_average, residual_echo_spectrum, comfort_noise_spectrum,
+                low_band_gain);
 
   // Limit the gain of the lower bands during start up and after resets.
   const float gain_upper_bound = aec_state.SuppressionGainLimit();
@@ -414,6 +433,9 @@
   }
 
   // Compute the gain for the upper bands.
+  const absl::optional<int> narrow_peak_band =
+      render_signal_analyzer.NarrowPeakBand();
+
   *high_bands_gain =
       UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band,
                      aec_state.SaturatedEcho(), render, *low_band_gain);
@@ -453,14 +475,17 @@
 SuppressionGain::DominantNearendDetector::DominantNearendDetector(
     const EchoCanceller3Config::Suppressor::DominantNearendDetection config)
     : enr_threshold_(config.enr_threshold),
+      enr_exit_threshold_(config.enr_exit_threshold),
       snr_threshold_(config.snr_threshold),
       hold_duration_(config.hold_duration),
-      trigger_threshold_(config.trigger_threshold) {}
+      trigger_threshold_(config.trigger_threshold),
+      use_during_initial_phase_(config.use_during_initial_phase) {}
 
 void SuppressionGain::DominantNearendDetector::Update(
     rtc::ArrayView<const float> nearend_spectrum,
     rtc::ArrayView<const float> residual_echo_spectrum,
-    rtc::ArrayView<const float> comfort_noise_spectrum) {
+    rtc::ArrayView<const float> comfort_noise_spectrum,
+    bool initial_state) {
   auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) {
     RTC_DCHECK_LE(16, spectrum.size());
     return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f);
@@ -471,7 +496,8 @@
 
   // Detect strong active nearend if the nearend is sufficiently stronger than
   // the echo and the nearend noise.
-  if (ne_sum > enr_threshold_ * echo_sum &&
+  if ((!initial_state || use_during_initial_phase_) &&
+      ne_sum > enr_threshold_ * echo_sum &&
       ne_sum > snr_threshold_ * noise_sum) {
     if (++trigger_counter_ >= trigger_threshold_) {
       // After a period of strong active nearend activity, flag nearend mode.
@@ -483,6 +509,12 @@
     trigger_counter_ = std::max(0, trigger_counter_ - 1);
   }
 
+  // Exit nearend-state early at strong echo.
+  if (ne_sum < enr_exit_threshold_ * echo_sum &&
+      echo_sum > snr_threshold_ * noise_sum) {
+    hold_counter_ = 0;
+  }
+
   // Remain in any nearend mode for a certain duration.
   hold_counter_ = std::max(0, hold_counter_ - 1);
   nearend_state_ = hold_counter_ > 0;
diff --git a/modules/audio_processing/aec3/suppression_gain.h b/modules/audio_processing/aec3/suppression_gain.h
index b851930..836ec51 100644
--- a/modules/audio_processing/aec3/suppression_gain.h
+++ b/modules/audio_processing/aec3/suppression_gain.h
@@ -30,6 +30,7 @@
                   int sample_rate_hz);
   ~SuppressionGain();
   void GetGain(
+      const std::array<float, kFftLengthBy2Plus1>& suppressor_input_spectrum,
       const std::array<float, kFftLengthBy2Plus1>& nearend_spectrum,
       const std::array<float, kFftLengthBy2Plus1>& echo_spectrum,
       const std::array<float, kFftLengthBy2Plus1>& residual_echo_spectrum,
@@ -63,12 +64,22 @@
       const std::array<float, kFftLengthBy2Plus1>& max_gain,
       std::array<float, kFftLengthBy2Plus1>* gain) const;
 
-  void LowerBandGain(bool stationary_with_low_power,
-                     const AecState& aec_state,
-                     const std::array<float, kFftLengthBy2Plus1>& nearend,
-                     const std::array<float, kFftLengthBy2Plus1>& echo,
-                     const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
-                     std::array<float, kFftLengthBy2Plus1>* gain);
+  void LowerBandGain(
+      bool stationary_with_low_power,
+      const AecState& aec_state,
+      const std::array<float, kFftLengthBy2Plus1>& suppressor_input,
+      const std::array<float, kFftLengthBy2Plus1>& nearend,
+      const std::array<float, kFftLengthBy2Plus1>& residual_echo,
+      const std::array<float, kFftLengthBy2Plus1>& comfort_noise,
+      std::array<float, kFftLengthBy2Plus1>* gain);
+
+  void GetMinGain(rtc::ArrayView<const float> suppressor_input,
+                  rtc::ArrayView<const float> weighted_residual_echo,
+                  bool low_noise_render,
+                  bool saturated_echo,
+                  rtc::ArrayView<float> min_gain) const;
+
+  void GetMaxGain(rtc::ArrayView<float> max_gain) const;
 
   class LowNoiseRenderDetector {
    public:
@@ -91,13 +102,16 @@
     // Updates the state selection based on latest spectral estimates.
     void Update(rtc::ArrayView<const float> nearend_spectrum,
                 rtc::ArrayView<const float> residual_echo_spectrum,
-                rtc::ArrayView<const float> comfort_noise_spectrum);
+                rtc::ArrayView<const float> comfort_noise_spectrum,
+                bool initial_state);
 
    private:
     const float enr_threshold_;
+    const float enr_exit_threshold_;
     const float snr_threshold_;
     const int hold_duration_;
     const int trigger_threshold_;
+    const bool use_during_initial_phase_;
 
     bool nearend_state_ = false;
     int trigger_counter_ = 0;
diff --git a/modules/audio_processing/aec3/suppression_gain_unittest.cc b/modules/audio_processing/aec3/suppression_gain_unittest.cc
index ef31371..651fd36 100644
--- a/modules/audio_processing/aec3/suppression_gain_unittest.cc
+++ b/modules/audio_processing/aec3/suppression_gain_unittest.cc
@@ -45,7 +45,7 @@
   AecState aec_state(EchoCanceller3Config{});
   EXPECT_DEATH(
       SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000)
-          .GetGain(E2, S2, R2, N2, E, Y,
+          .GetGain(E2, E2, S2, R2, N2, E, Y,
                    RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state,
                    std::vector<std::vector<float>>(
                        3, std::vector<float>(kBlockSize, 0.f)),
@@ -77,7 +77,7 @@
   ApmDataDumper data_dumper(42);
   Subtractor subtractor(config, &data_dumper, DetectOptimization());
   std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
-      RenderDelayBuffer::Create(config, 3));
+      RenderDelayBuffer::Create2(config, 3));
   absl::optional<DelayEstimate> delay_estimate;
 
   // Ensure that a strong noise is detected to mask any echoes.
@@ -106,7 +106,7 @@
                      subtractor.FilterImpulseResponse(),
                      *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
                      y);
-    suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
+    suppression_gain.GetGain(E2, E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
                              &high_bands_gain, &g);
   }
   std::for_each(g.begin(), g.end(),
@@ -126,7 +126,7 @@
                      subtractor.FilterImpulseResponse(),
                      *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
                      y);
-    suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
+    suppression_gain.GetGain(E2, E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
                              &high_bands_gain, &g);
   }
   std::for_each(g.begin(), g.end(),
@@ -138,7 +138,7 @@
   E.re.fill(sqrtf(E2[0]));
 
   for (int k = 0; k < 10; ++k) {
-    suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
+    suppression_gain.GetGain(E2, E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
                              &high_bands_gain, &g);
   }
   std::for_each(g.begin(), g.end(),
diff --git a/modules/audio_processing/aec_dump/BUILD.gn b/modules/audio_processing/aec_dump/BUILD.gn
index e5fee3e..5b55526 100644
--- a/modules/audio_processing/aec_dump/BUILD.gn
+++ b/modules/audio_processing/aec_dump/BUILD.gn
@@ -17,6 +17,7 @@
   deps = [
     "../",
     "../../../rtc_base:rtc_base_approved",
+    "../../../rtc_base/system:rtc_export",
   ]
 }
 
diff --git a/modules/audio_processing/aec_dump/aec_dump_factory.h b/modules/audio_processing/aec_dump/aec_dump_factory.h
index e3f00f6..1e55d59 100644
--- a/modules/audio_processing/aec_dump/aec_dump_factory.h
+++ b/modules/audio_processing/aec_dump/aec_dump_factory.h
@@ -16,6 +16,7 @@
 
 #include "modules/audio_processing/include/aec_dump.h"
 #include "rtc_base/platform_file.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace rtc {
 class TaskQueue;
@@ -23,7 +24,7 @@
 
 namespace webrtc {
 
-class AecDumpFactory {
+class RTC_EXPORT AecDumpFactory {
  public:
   // The |worker_queue| may not be null and must outlive the created
   // AecDump instance. |max_log_size_bytes == -1| means the log size
diff --git a/modules/audio_processing/aec_dump/aec_dump_impl.cc b/modules/audio_processing/aec_dump/aec_dump_impl.cc
index 9e07367..2732934 100644
--- a/modules/audio_processing/aec_dump/aec_dump_impl.cc
+++ b/modules/audio_processing/aec_dump/aec_dump_impl.cc
@@ -167,6 +167,34 @@
   worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task)));
 }
 
+void AecDumpImpl::WriteRuntimeSetting(
+    const AudioProcessing::RuntimeSetting& runtime_setting) {
+  RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
+  auto task = CreateWriteToFileTask();
+  auto* event = task->GetEvent();
+  event->set_type(audioproc::Event::RUNTIME_SETTING);
+  audioproc::RuntimeSetting* setting = event->mutable_runtime_setting();
+  switch (runtime_setting.type()) {
+    case AudioProcessing::RuntimeSetting::Type::kCapturePreGain: {
+      float x;
+      runtime_setting.GetFloat(&x);
+      setting->set_capture_pre_gain(x);
+      break;
+    }
+    case AudioProcessing::RuntimeSetting::Type::
+        kCustomRenderProcessingRuntimeSetting: {
+      float x;
+      runtime_setting.GetFloat(&x);
+      setting->set_custom_render_processing_setting(x);
+      break;
+    }
+    case AudioProcessing::RuntimeSetting::Type::kNotSpecified:
+      RTC_NOTREACHED();
+      break;
+  }
+  worker_queue_->PostTask(std::unique_ptr<rtc::QueuedTask>(std::move(task)));
+}
+
 std::unique_ptr<WriteToFileTask> AecDumpImpl::CreateWriteToFileTask() {
   return absl::make_unique<WriteToFileTask>(debug_file_.get(),
                                             &num_bytes_left_for_log_);
diff --git a/modules/audio_processing/aec_dump/aec_dump_impl.h b/modules/audio_processing/aec_dump/aec_dump_impl.h
index a5416a0..df949ca 100644
--- a/modules/audio_processing/aec_dump/aec_dump_impl.h
+++ b/modules/audio_processing/aec_dump/aec_dump_impl.h
@@ -67,6 +67,9 @@
 
   void WriteConfig(const InternalAPMConfig& config) override;
 
+  void WriteRuntimeSetting(
+      const AudioProcessing::RuntimeSetting& runtime_setting) override;
+
  private:
   std::unique_ptr<WriteToFileTask> CreateWriteToFileTask();
 
diff --git a/modules/audio_processing/aec_dump/mock_aec_dump.h b/modules/audio_processing/aec_dump/mock_aec_dump.h
index c01de51..8910b42 100644
--- a/modules/audio_processing/aec_dump/mock_aec_dump.h
+++ b/modules/audio_processing/aec_dump/mock_aec_dump.h
@@ -43,6 +43,9 @@
                void(const AudioFrameView<const float>& src));
 
   MOCK_METHOD1(WriteConfig, void(const InternalAPMConfig& config));
+
+  MOCK_METHOD1(WriteRuntimeSetting,
+               void(const AudioProcessing::RuntimeSetting& config));
 };
 
 }  // namespace test
diff --git a/modules/audio_processing/agc/BUILD.gn b/modules/audio_processing/agc/BUILD.gn
index 65653a0..a5c15ec 100644
--- a/modules/audio_processing/agc/BUILD.gn
+++ b/modules/audio_processing/agc/BUILD.gn
@@ -25,7 +25,7 @@
     "../../../rtc_base:logging",
     "../../../rtc_base:macromagic",
     "../../../rtc_base:safe_minmax",
-    "../../../system_wrappers:metrics_api",
+    "../../../system_wrappers:metrics",
     "../agc2:level_estimation_agc",
     "../vad",
   ]
diff --git a/modules/audio_processing/agc2/BUILD.gn b/modules/audio_processing/agc2/BUILD.gn
index 45ef968..1865fde 100644
--- a/modules/audio_processing/agc2/BUILD.gn
+++ b/modules/audio_processing/agc2/BUILD.gn
@@ -65,7 +65,7 @@
     "../../../rtc_base:checks",
     "../../../rtc_base:rtc_base_approved",
     "../../../rtc_base:safe_minmax",
-    "../../../system_wrappers:metrics_api",
+    "../../../system_wrappers:metrics",
   ]
 }
 
@@ -83,10 +83,12 @@
 
 rtc_source_set("common") {
   sources = [
+    "agc2_common.cc",
     "agc2_common.h",
   ]
   deps = [
     "../../../rtc_base:rtc_base_approved",
+    "../../../system_wrappers:field_trial",
   ]
 }
 
@@ -114,7 +116,7 @@
     "../../../rtc_base:gtest_prod",
     "../../../rtc_base:rtc_base_approved",
     "../../../rtc_base:safe_minmax",
-    "../../../system_wrappers:metrics_api",
+    "../../../system_wrappers:metrics",
   ]
 }
 
@@ -232,7 +234,7 @@
     "../../../rtc_base:checks",
     "../../../rtc_base:rtc_base_approved",
     "../../../rtc_base:rtc_base_tests_utils",
-    "../../../system_wrappers:metrics_default",
+    "../../../system_wrappers:metrics",
     "//third_party/abseil-cpp/absl/memory",
   ]
 }
diff --git a/modules/audio_processing/agc2/adaptive_agc.cc b/modules/audio_processing/agc2/adaptive_agc.cc
index 805be0c..c7346c6 100644
--- a/modules/audio_processing/agc2/adaptive_agc.cc
+++ b/modules/audio_processing/agc2/adaptive_agc.cc
@@ -29,25 +29,39 @@
 
 AdaptiveAgc::~AdaptiveAgc() = default;
 
-void AdaptiveAgc::Process(AudioFrameView<float> float_frame) {
-  const VadWithLevel::LevelAndProbability vad_result =
-      vad_.AnalyzeFrame(float_frame);
+void AdaptiveAgc::Process(AudioFrameView<float> float_frame,
+                          float last_audio_level) {
+  auto signal_with_levels = SignalWithLevels(float_frame);
+  signal_with_levels.vad_result = vad_.AnalyzeFrame(float_frame);
   apm_data_dumper_->DumpRaw("agc2_vad_probability",
-                            vad_result.speech_probability);
-  apm_data_dumper_->DumpRaw("agc2_vad_rms_dbfs", vad_result.speech_rms_dbfs);
+                            signal_with_levels.vad_result.speech_probability);
+  apm_data_dumper_->DumpRaw("agc2_vad_rms_dbfs",
+                            signal_with_levels.vad_result.speech_rms_dbfs);
 
-  apm_data_dumper_->DumpRaw("agc2_vad_peak_dbfs", vad_result.speech_peak_dbfs);
-  speech_level_estimator_.UpdateEstimation(vad_result);
+  apm_data_dumper_->DumpRaw("agc2_vad_peak_dbfs",
+                            signal_with_levels.vad_result.speech_peak_dbfs);
+  speech_level_estimator_.UpdateEstimation(signal_with_levels.vad_result);
 
-  const float speech_level_dbfs = speech_level_estimator_.LatestLevelEstimate();
+  signal_with_levels.input_level_dbfs =
+      speech_level_estimator_.LatestLevelEstimate();
 
-  const float noise_level_dbfs = noise_level_estimator_.Analyze(float_frame);
+  signal_with_levels.input_noise_level_dbfs =
+      noise_level_estimator_.Analyze(float_frame);
 
-  apm_data_dumper_->DumpRaw("agc2_noise_estimate_dbfs", noise_level_dbfs);
+  apm_data_dumper_->DumpRaw("agc2_noise_estimate_dbfs",
+                            signal_with_levels.input_noise_level_dbfs);
+
+  signal_with_levels.limiter_audio_level_dbfs =
+      last_audio_level > 0 ? FloatS16ToDbfs(last_audio_level) : -90.f;
+  apm_data_dumper_->DumpRaw("agc2_last_limiter_audio_level",
+                            signal_with_levels.limiter_audio_level_dbfs);
+
+  signal_with_levels.estimate_is_confident =
+      speech_level_estimator_.LevelEstimationIsConfident();
 
   // The gain applier applies the gain.
-  gain_applier_.Process(speech_level_dbfs, noise_level_dbfs, vad_result,
-                        float_frame);
+  gain_applier_.Process(signal_with_levels);
+  ;
 }
 
 void AdaptiveAgc::Reset() {
diff --git a/modules/audio_processing/agc2/adaptive_agc.h b/modules/audio_processing/agc2/adaptive_agc.h
index 8f5efec..792b2bc 100644
--- a/modules/audio_processing/agc2/adaptive_agc.h
+++ b/modules/audio_processing/agc2/adaptive_agc.h
@@ -27,7 +27,7 @@
   explicit AdaptiveAgc(ApmDataDumper* apm_data_dumper);
   ~AdaptiveAgc();
 
-  void Process(AudioFrameView<float> float_frame);
+  void Process(AudioFrameView<float> float_frame, float last_audio_level);
   void Reset();
 
  private:
diff --git a/modules/audio_processing/agc2/adaptive_digital_gain_applier.cc b/modules/audio_processing/agc2/adaptive_digital_gain_applier.cc
index f5342df..d4560ca 100644
--- a/modules/audio_processing/agc2/adaptive_digital_gain_applier.cc
+++ b/modules/audio_processing/agc2/adaptive_digital_gain_applier.cc
@@ -52,6 +52,23 @@
   return std::min(target_gain, std::max(noise_headroom_db, 0.f));
 }
 
+float LimitGainByLowConfidence(float target_gain,
+                               float last_gain,
+                               float limiter_audio_level_dbfs,
+                               bool estimate_is_confident) {
+  if (estimate_is_confident ||
+      limiter_audio_level_dbfs <= kLimiterThresholdForAgcGainDbfs) {
+    return target_gain;
+  }
+  const float limiter_level_before_gain = limiter_audio_level_dbfs - last_gain;
+
+  // Compute a new gain so that limiter_level_before_gain + new_gain <=
+  // kLimiterThreshold.
+  const float new_target_gain = std::max(
+      kLimiterThresholdForAgcGainDbfs - limiter_level_before_gain, 0.f);
+  return std::min(new_target_gain, target_gain);
+}
+
 // Computes how the gain should change during this frame.
 // Return the gain difference in db to 'last_gain_db'.
 float ComputeGainChangeThisFrameDb(float target_gain_db,
@@ -67,38 +84,43 @@
 }
 }  // namespace
 
+SignalWithLevels::SignalWithLevels(AudioFrameView<float> float_frame)
+    : float_frame(float_frame) {}
+SignalWithLevels::SignalWithLevels(const SignalWithLevels&) = default;
+
 AdaptiveDigitalGainApplier::AdaptiveDigitalGainApplier(
     ApmDataDumper* apm_data_dumper)
     : gain_applier_(false, DbToRatio(last_gain_db_)),
       apm_data_dumper_(apm_data_dumper) {}
 
-void AdaptiveDigitalGainApplier::Process(
-    float input_level_dbfs,
-    float input_noise_level_dbfs,
-    const VadWithLevel::LevelAndProbability vad_result,
-    AudioFrameView<float> float_frame) {
+void AdaptiveDigitalGainApplier::Process(SignalWithLevels signal_with_levels) {
   calls_since_last_gain_log_++;
   if (calls_since_last_gain_log_ == 100) {
     calls_since_last_gain_log_ = 0;
     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc2.DigitalGainApplied",
                                 last_gain_db_, 0, kMaxGainDb, kMaxGainDb + 1);
     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc2.EstimatedNoiseLevel",
-                                input_noise_level_dbfs, 0, 100, 101);
+                                -signal_with_levels.input_noise_level_dbfs, 0,
+                                100, 101);
   }
 
-  input_level_dbfs = std::min(input_level_dbfs, 0.f);
+  signal_with_levels.input_level_dbfs =
+      std::min(signal_with_levels.input_level_dbfs, 0.f);
 
-  RTC_DCHECK_GE(input_level_dbfs, -150.f);
-  RTC_DCHECK_GE(float_frame.num_channels(), 1);
-  RTC_DCHECK_GE(float_frame.samples_per_channel(), 1);
+  RTC_DCHECK_GE(signal_with_levels.input_level_dbfs, -150.f);
+  RTC_DCHECK_GE(signal_with_levels.float_frame.num_channels(), 1);
+  RTC_DCHECK_GE(signal_with_levels.float_frame.samples_per_channel(), 1);
 
-  const float target_gain_db =
-      LimitGainByNoise(ComputeGainDb(input_level_dbfs), input_noise_level_dbfs,
-                       apm_data_dumper_);
+  const float target_gain_db = LimitGainByLowConfidence(
+      LimitGainByNoise(ComputeGainDb(signal_with_levels.input_level_dbfs),
+                       signal_with_levels.input_noise_level_dbfs,
+                       apm_data_dumper_),
+      last_gain_db_, signal_with_levels.limiter_audio_level_dbfs,
+      signal_with_levels.estimate_is_confident);
 
   // Forbid increasing the gain when there is no speech.
-  gain_increase_allowed_ =
-      vad_result.speech_probability > kVadConfidenceThreshold;
+  gain_increase_allowed_ = signal_with_levels.vad_result.speech_probability >
+                           kVadConfidenceThreshold;
 
   const float gain_change_this_frame_db = ComputeGainChangeThisFrameDb(
       target_gain_db, last_gain_db_, gain_increase_allowed_);
@@ -114,7 +136,7 @@
     gain_applier_.SetGainFactor(
         DbToRatio(last_gain_db_ + gain_change_this_frame_db));
   }
-  gain_applier_.ApplyGain(float_frame);
+  gain_applier_.ApplyGain(signal_with_levels.float_frame);
 
   // Remember that the gain has changed for the next iteration.
   last_gain_db_ = last_gain_db_ + gain_change_this_frame_db;
diff --git a/modules/audio_processing/agc2/adaptive_digital_gain_applier.h b/modules/audio_processing/agc2/adaptive_digital_gain_applier.h
index a3a1ff5..e7f07fc 100644
--- a/modules/audio_processing/agc2/adaptive_digital_gain_applier.h
+++ b/modules/audio_processing/agc2/adaptive_digital_gain_applier.h
@@ -20,14 +20,23 @@
 
 class ApmDataDumper;
 
+struct SignalWithLevels {
+  SignalWithLevels(AudioFrameView<float> float_frame);
+  SignalWithLevels(const SignalWithLevels&);
+
+  float input_level_dbfs = -1.f;
+  float input_noise_level_dbfs = -1.f;
+  VadWithLevel::LevelAndProbability vad_result;
+  float limiter_audio_level_dbfs = -1.f;
+  bool estimate_is_confident = false;
+  AudioFrameView<float> float_frame;
+};
+
 class AdaptiveDigitalGainApplier {
  public:
   explicit AdaptiveDigitalGainApplier(ApmDataDumper* apm_data_dumper);
   // Decide what gain to apply.
-  void Process(float input_level_dbfs,
-               float input_noise_level_dbfs,
-               const VadWithLevel::LevelAndProbability vad_result,
-               AudioFrameView<float> float_frame);
+  void Process(SignalWithLevels signal_with_levels);
 
  private:
   float last_gain_db_ = kInitialAdaptiveDigitalGainDb;
diff --git a/modules/audio_processing/agc2/adaptive_digital_gain_applier_unittest.cc b/modules/audio_processing/agc2/adaptive_digital_gain_applier_unittest.cc
index ea9e5c7..6e77cda 100644
--- a/modules/audio_processing/agc2/adaptive_digital_gain_applier_unittest.cc
+++ b/modules/audio_processing/agc2/adaptive_digital_gain_applier_unittest.cc
@@ -23,6 +23,7 @@
 // Constants used in place of estimated noise levels.
 constexpr float kNoNoiseDbfs = -90.f;
 constexpr float kWithNoiseDbfs = -20.f;
+constexpr VadWithLevel::LevelAndProbability kVadSpeech(1.f, -20.f, 0.f);
 
 // Runs gain applier and returns the applied gain in linear scale.
 float RunOnConstantLevel(int num_iterations,
@@ -33,14 +34,30 @@
 
   for (int i = 0; i < num_iterations; ++i) {
     VectorFloatFrame fake_audio(1, 1, 1.f);
-    gain_applier->Process(input_level_dbfs, kNoNoiseDbfs, vad_data,
-                          fake_audio.float_frame_view());
+    SignalWithLevels signal_with_levels(fake_audio.float_frame_view());
+    signal_with_levels.input_level_dbfs = input_level_dbfs;
+    signal_with_levels.input_noise_level_dbfs = kNoNoiseDbfs;
+    signal_with_levels.vad_result = vad_data;
+    signal_with_levels.limiter_audio_level_dbfs = -2.f;
+    signal_with_levels.estimate_is_confident = true;
+    gain_applier->Process(signal_with_levels);
     gain_linear = fake_audio.float_frame_view().channel(0)[0];
   }
   return gain_linear;
 }
 
-constexpr VadWithLevel::LevelAndProbability kVadSpeech(1.f, -20.f, 0.f);
+// Returns 'SignalWithLevels' for typical GainApplier behavior. Voice on, no
+// noise, low limiter, confident level.
+SignalWithLevels TestSignalWithLevel(AudioFrameView<float> float_frame) {
+  SignalWithLevels result(float_frame);
+  result.input_level_dbfs = -1;
+  result.input_noise_level_dbfs = kNoNoiseDbfs;
+  result.vad_result = kVadSpeech;
+  result.estimate_is_confident = true;
+  result.limiter_audio_level_dbfs = -2.f;
+  return result;
+}
+
 }  // namespace
 
 TEST(AutomaticGainController2AdaptiveGainApplier, GainApplierShouldNotCrash) {
@@ -52,8 +69,9 @@
 
   // Make one call with reasonable audio level values and settings.
   VectorFloatFrame fake_audio(2, 480, 10000.f);
-  gain_applier.Process(-5.0, kNoNoiseDbfs, kVadSpeech,
-                       fake_audio.float_frame_view());
+  auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+  signal_with_level.input_level_dbfs = -5.0;
+  gain_applier.Process(signal_with_level);
 }
 
 // Check that the output is -kHeadroom dBFS.
@@ -103,8 +121,9 @@
   for (int i = 0; i < kNumFramesToAdapt; ++i) {
     SCOPED_TRACE(i);
     VectorFloatFrame fake_audio(1, 1, 1.f);
-    gain_applier.Process(initial_level_dbfs, kNoNoiseDbfs, kVadSpeech,
-                         fake_audio.float_frame_view());
+    auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+    signal_with_level.input_level_dbfs = initial_level_dbfs;
+    gain_applier.Process(signal_with_level);
     float current_gain_linear = fake_audio.float_frame_view().channel(0)[0];
     EXPECT_LE(std::abs(current_gain_linear - last_gain_linear),
               kMaxChangePerFrameLinear);
@@ -115,8 +134,9 @@
   for (int i = 0; i < kNumFramesToAdapt; ++i) {
     SCOPED_TRACE(i);
     VectorFloatFrame fake_audio(1, 1, 1.f);
-    gain_applier.Process(0.f, kNoNoiseDbfs, kVadSpeech,
-                         fake_audio.float_frame_view());
+    auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+    signal_with_level.input_level_dbfs = 0.f;
+    gain_applier.Process(signal_with_level);
     float current_gain_linear = fake_audio.float_frame_view().channel(0)[0];
     EXPECT_LE(std::abs(current_gain_linear - last_gain_linear),
               kMaxChangePerFrameLinear);
@@ -132,8 +152,9 @@
   constexpr int num_samples = 480;
 
   VectorFloatFrame fake_audio(1, num_samples, 1.f);
-  gain_applier.Process(initial_level_dbfs, kNoNoiseDbfs, kVadSpeech,
-                       fake_audio.float_frame_view());
+  auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+  signal_with_level.input_level_dbfs = initial_level_dbfs;
+  gain_applier.Process(signal_with_level);
   float maximal_difference = 0.f;
   float current_value = 1.f * DbToRatio(kInitialAdaptiveDigitalGainDb);
   for (const auto& x : fake_audio.float_frame_view().channel(0)) {
@@ -162,8 +183,10 @@
 
   for (int i = 0; i < num_initial_frames + num_frames; ++i) {
     VectorFloatFrame fake_audio(1, num_samples, 1.f);
-    gain_applier.Process(initial_level_dbfs, kWithNoiseDbfs, kVadSpeech,
-                         fake_audio.float_frame_view());
+    auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+    signal_with_level.input_level_dbfs = initial_level_dbfs;
+    signal_with_level.input_noise_level_dbfs = kWithNoiseDbfs;
+    gain_applier.Process(signal_with_level);
 
     // Wait so that the adaptive gain applier has time to lower the gain.
     if (i > num_initial_frames) {
@@ -182,7 +205,39 @@
 
   // Make one call with positive audio level values and settings.
   VectorFloatFrame fake_audio(2, 480, 10000.f);
-  gain_applier.Process(5.0f, kNoNoiseDbfs, kVadSpeech,
-                       fake_audio.float_frame_view());
+  auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+  signal_with_level.input_level_dbfs = 5.0f;
+  gain_applier.Process(signal_with_level);
+}
+
+TEST(AutomaticGainController2GainApplier, AudioLevelLimitsGain) {
+  ApmDataDumper apm_data_dumper(0);
+  AdaptiveDigitalGainApplier gain_applier(&apm_data_dumper);
+
+  constexpr float initial_level_dbfs = -25.f;
+  constexpr int num_samples = 480;
+  constexpr int num_initial_frames =
+      kInitialAdaptiveDigitalGainDb / kMaxGainChangePerFrameDb;
+  constexpr int num_frames = 50;
+
+  ASSERT_GT(kWithNoiseDbfs, kMaxNoiseLevelDbfs) << "kWithNoiseDbfs is too low";
+
+  for (int i = 0; i < num_initial_frames + num_frames; ++i) {
+    VectorFloatFrame fake_audio(1, num_samples, 1.f);
+    auto signal_with_level = TestSignalWithLevel(fake_audio.float_frame_view());
+    signal_with_level.input_level_dbfs = initial_level_dbfs;
+    signal_with_level.limiter_audio_level_dbfs = 1.f;
+    signal_with_level.estimate_is_confident = false;
+    gain_applier.Process(signal_with_level);
+
+    // Wait so that the adaptive gain applier has time to lower the gain.
+    if (i > num_initial_frames) {
+      const float maximal_ratio =
+          *std::max_element(fake_audio.float_frame_view().channel(0).begin(),
+                            fake_audio.float_frame_view().channel(0).end());
+
+      EXPECT_NEAR(maximal_ratio, 1.f, 0.001f);
+    }
+  }
 }
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/adaptive_mode_level_estimator.h b/modules/audio_processing/agc2/adaptive_mode_level_estimator.h
index 5ca7c55..4d4180c 100644
--- a/modules/audio_processing/agc2/adaptive_mode_level_estimator.h
+++ b/modules/audio_processing/agc2/adaptive_mode_level_estimator.h
@@ -23,6 +23,9 @@
   void UpdateEstimation(const VadWithLevel::LevelAndProbability& vad_data);
   float LatestLevelEstimate() const;
   void Reset();
+  bool LevelEstimationIsConfident() const {
+    return buffer_size_ms_ >= kFullBufferSizeMs;
+  }
 
  private:
   void DebugDumpEstimate();
diff --git a/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.cc b/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.cc
index b0922be..4cee963 100644
--- a/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.cc
+++ b/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.cc
@@ -17,7 +17,7 @@
 AdaptiveModeLevelEstimatorAgc::AdaptiveModeLevelEstimatorAgc(
     ApmDataDumper* apm_data_dumper)
     : level_estimator_(apm_data_dumper) {
-  set_target_level_dbfs(kDefaultLevelDbfs);
+  set_target_level_dbfs(kDefaultAgc2LevelHeadroomDbfs);
 }
 
 // |audio| must be mono; in a multi-channel stream, provide the first (usually
diff --git a/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h b/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h
index df01dd9..484b128 100644
--- a/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h
+++ b/modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h
@@ -38,7 +38,7 @@
 
  private:
   static constexpr int kTimeUntilConfidentMs = 700;
-  static constexpr int kDefaultLevelDbfs = 0;
+  static constexpr int kDefaultAgc2LevelHeadroomDbfs = -1;
   int32_t time_in_ms_since_last_estimate_ = 0;
   AdaptiveModeLevelEstimator level_estimator_;
   VadWithLevel agc2_vad_;
diff --git a/modules/audio_processing/agc2/adaptive_mode_level_estimator_unittest.cc b/modules/audio_processing/agc2/adaptive_mode_level_estimator_unittest.cc
index 1915ce2..d935416 100644
--- a/modules/audio_processing/agc2/adaptive_mode_level_estimator_unittest.cc
+++ b/modules/audio_processing/agc2/adaptive_mode_level_estimator_unittest.cc
@@ -39,14 +39,16 @@
   ApmDataDumper apm_data_dumper(0);
   AdaptiveModeLevelEstimator level_estimator(&apm_data_dumper);
 
-  constexpr float kSpeechRmsDbfs = -15.f;
-  RunOnConstantLevel(
-      100,
-      VadWithLevel::LevelAndProbability(
-          1.f, kSpeechRmsDbfs - kInitialSaturationMarginDb, kSpeechRmsDbfs),
-      &level_estimator);
+  constexpr float kSpeechPeakDbfs = -15.f;
+  RunOnConstantLevel(100,
+                     VadWithLevel::LevelAndProbability(
+                         1.f, kSpeechPeakDbfs - GetInitialSaturationMarginDb(),
+                         kSpeechPeakDbfs),
+                     &level_estimator);
 
-  EXPECT_NEAR(level_estimator.LatestLevelEstimate(), kSpeechRmsDbfs, 0.1f);
+  EXPECT_NEAR(level_estimator.LatestLevelEstimate() -
+                  GetExtraSaturationMarginOffsetDb(),
+              kSpeechPeakDbfs, 0.1f);
 }
 
 TEST(AutomaticGainController2AdaptiveModeLevelEstimator,
@@ -59,7 +61,7 @@
   RunOnConstantLevel(
       100,
       VadWithLevel::LevelAndProbability(
-          1.f, kSpeechRmsDbfs - kInitialSaturationMarginDb, kSpeechRmsDbfs),
+          1.f, kSpeechRmsDbfs - GetInitialSaturationMarginDb(), kSpeechRmsDbfs),
       &level_estimator);
 
   // Run for one more second, but mark as not speech.
@@ -69,7 +71,9 @@
       &level_estimator);
 
   // Level should not have changed.
-  EXPECT_NEAR(level_estimator.LatestLevelEstimate(), kSpeechRmsDbfs, 0.1f);
+  EXPECT_NEAR(level_estimator.LatestLevelEstimate() -
+                  GetExtraSaturationMarginOffsetDb(),
+              kSpeechRmsDbfs, 0.1f);
 }
 
 TEST(AutomaticGainController2AdaptiveModeLevelEstimator, TimeToAdapt) {
@@ -81,7 +85,7 @@
   RunOnConstantLevel(
       kFullBufferSizeMs / kFrameDurationMs,
       VadWithLevel::LevelAndProbability(
-          1.f, kInitialSpeechRmsDbfs - kInitialSaturationMarginDb,
+          1.f, kInitialSpeechRmsDbfs - GetInitialSaturationMarginDb(),
           kInitialSpeechRmsDbfs),
       &level_estimator);
 
@@ -94,7 +98,7 @@
   RunOnConstantLevel(
       static_cast<int>(kFullBufferSizeMs / kFrameDurationMs / 2),
       VadWithLevel::LevelAndProbability(
-          1.f, kDifferentSpeechRmsDbfs - kInitialSaturationMarginDb,
+          1.f, kDifferentSpeechRmsDbfs - GetInitialSaturationMarginDb(),
           kDifferentSpeechRmsDbfs),
       &level_estimator);
   EXPECT_GT(
@@ -105,11 +109,12 @@
   RunOnConstantLevel(
       static_cast<int>(3 * kFullBufferSizeMs / kFrameDurationMs),
       VadWithLevel::LevelAndProbability(
-          1.f, kDifferentSpeechRmsDbfs - kInitialSaturationMarginDb,
+          1.f, kDifferentSpeechRmsDbfs - GetInitialSaturationMarginDb(),
           kDifferentSpeechRmsDbfs),
       &level_estimator);
-  EXPECT_NEAR(level_estimator.LatestLevelEstimate(), kDifferentSpeechRmsDbfs,
-              kMaxDifferenceDb * 0.5f);
+  EXPECT_NEAR(level_estimator.LatestLevelEstimate() -
+                  GetExtraSaturationMarginOffsetDb(),
+              kDifferentSpeechRmsDbfs, kMaxDifferenceDb * 0.5f);
 }
 
 TEST(AutomaticGainController2AdaptiveModeLevelEstimator,
@@ -123,7 +128,7 @@
   RunOnConstantLevel(
       kFullBufferSizeMs / kFrameDurationMs,
       VadWithLevel::LevelAndProbability(
-          1.f, kInitialSpeechRmsDbfs - kInitialSaturationMarginDb,
+          1.f, kInitialSpeechRmsDbfs - GetInitialSaturationMarginDb(),
           kInitialSpeechRmsDbfs),
       &level_estimator);
 
@@ -134,16 +139,17 @@
   RunOnConstantLevel(
       kFullBufferSizeMs / kFrameDurationMs / 2,
       VadWithLevel::LevelAndProbability(
-          1.f, kDifferentSpeechRmsDbfs - kInitialSaturationMarginDb,
+          1.f, kDifferentSpeechRmsDbfs - GetInitialSaturationMarginDb(),
           kDifferentSpeechRmsDbfs),
       &level_estimator);
 
   // The level should be close to 'kDifferentSpeechRmsDbfs'.
   const float kMaxDifferenceDb =
       0.1f * std::abs(kDifferentSpeechRmsDbfs - kInitialSpeechRmsDbfs);
-  EXPECT_LT(
-      std::abs(kDifferentSpeechRmsDbfs - level_estimator.LatestLevelEstimate()),
-      kMaxDifferenceDb);
+  EXPECT_LT(std::abs(kDifferentSpeechRmsDbfs -
+                     (level_estimator.LatestLevelEstimate() -
+                      GetExtraSaturationMarginOffsetDb())),
+            kMaxDifferenceDb);
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/agc2_common.cc b/modules/audio_processing/agc2/agc2_common.cc
new file mode 100644
index 0000000..5da353f
--- /dev/null
+++ b/modules/audio_processing/agc2/agc2_common.cc
@@ -0,0 +1,56 @@
+/*
+ *  Copyright (c) 2018 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 "modules/audio_processing/agc2/agc2_common.h"
+
+#include <string>
+
+#include "system_wrappers/include/field_trial.h"
+
+namespace webrtc {
+
+float GetInitialSaturationMarginDb() {
+  constexpr char kForceInitialSaturationMarginFieldTrial[] =
+      "WebRTC-Audio-Agc2ForceInitialSaturationMargin";
+
+  const bool use_forced_initial_saturation_margin =
+      webrtc::field_trial::IsEnabled(kForceInitialSaturationMarginFieldTrial);
+  if (use_forced_initial_saturation_margin) {
+    const std::string field_trial_string = webrtc::field_trial::FindFullName(
+        kForceInitialSaturationMarginFieldTrial);
+    float margin_db = -1;
+    if (sscanf(field_trial_string.c_str(), "Enabled-%f", &margin_db) == 1 &&
+        margin_db >= 12.f && margin_db <= 25.f) {
+      return margin_db;
+    }
+  }
+  constexpr float kDefaultInitialSaturationMarginDb = 20.f;
+  return kDefaultInitialSaturationMarginDb;
+}
+
+float GetExtraSaturationMarginOffsetDb() {
+  constexpr char kForceExtraSaturationMarginFieldTrial[] =
+      "WebRTC-Audio-Agc2ForceExtraSaturationMargin";
+
+  const bool use_forced_extra_saturation_margin =
+      webrtc::field_trial::IsEnabled(kForceExtraSaturationMarginFieldTrial);
+  if (use_forced_extra_saturation_margin) {
+    const std::string field_trial_string = webrtc::field_trial::FindFullName(
+        kForceExtraSaturationMarginFieldTrial);
+    float margin_db = -1;
+    if (sscanf(field_trial_string.c_str(), "Enabled-%f", &margin_db) == 1 &&
+        margin_db >= 0.f && margin_db <= 10.f) {
+      return margin_db;
+    }
+  }
+  constexpr float kDefaultExtraSaturationMarginDb = 2.f;
+  return kDefaultExtraSaturationMarginDb;
+}
+};  // namespace webrtc
diff --git a/modules/audio_processing/agc2/agc2_common.h b/modules/audio_processing/agc2/agc2_common.h
index 35e8f58..71d33e5 100644
--- a/modules/audio_processing/agc2/agc2_common.h
+++ b/modules/audio_processing/agc2/agc2_common.h
@@ -34,6 +34,8 @@
 constexpr float kHeadroomDbfs = 1.f;
 constexpr float kMaxGainDb = 30.f;
 constexpr float kInitialAdaptiveDigitalGainDb = 8.f;
+// At what limiter levels should we start decreasing the adaptive digital gain.
+constexpr float kLimiterThresholdForAgcGainDbfs = -kHeadroomDbfs;
 
 // This parameter must be tuned together with the noise estimator.
 constexpr float kMaxNoiseLevelDbfs = -50.f;
@@ -50,7 +52,8 @@
 constexpr float kInitialSpeechLevelEstimateDbfs = -30.f;
 
 // Saturation Protector settings.
-constexpr float kInitialSaturationMarginDb = 17.f;
+float GetInitialSaturationMarginDb();
+float GetExtraSaturationMarginOffsetDb();
 
 constexpr size_t kPeakEnveloperSuperFrameLengthMs = 400;
 static_assert(kFullBufferSizeMs % kPeakEnveloperSuperFrameLengthMs == 0,
diff --git a/modules/audio_processing/agc2/fixed_digital_level_estimator.h b/modules/audio_processing/agc2/fixed_digital_level_estimator.h
index 4907ec7..84429d3 100644
--- a/modules/audio_processing/agc2/fixed_digital_level_estimator.h
+++ b/modules/audio_processing/agc2/fixed_digital_level_estimator.h
@@ -48,6 +48,8 @@
   // Resets the level estimator internal state.
   void Reset();
 
+  float LastAudioLevel() const { return filter_state_level_; }
+
  private:
   void CheckParameterCombination();
 
diff --git a/modules/audio_processing/agc2/fixed_gain_controller.cc b/modules/audio_processing/agc2/fixed_gain_controller.cc
index d49d181..0d7e3a6 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller.cc
+++ b/modules/audio_processing/agc2/fixed_gain_controller.cc
@@ -98,4 +98,8 @@
     }
   }
 }
+
+float FixedGainController::LastAudioLevel() const {
+  return gain_curve_applier_.LastAudioLevel();
+}
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/fixed_gain_controller.h b/modules/audio_processing/agc2/fixed_gain_controller.h
index a41a13f..ff6ab81 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller.h
+++ b/modules/audio_processing/agc2/fixed_gain_controller.h
@@ -29,6 +29,7 @@
   // with any other method call).
   void SetGain(float gain_to_apply_db);
   void SetSampleRate(size_t sample_rate_hz);
+  float LastAudioLevel() const;
 
  private:
   float gain_to_apply_ = 1.f;
diff --git a/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc b/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
index bd7c356..db1732a 100644
--- a/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
+++ b/modules/audio_processing/agc2/fixed_gain_controller_unittest.cc
@@ -16,7 +16,7 @@
 #include "modules/audio_processing/agc2/vector_float_frame.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "rtc_base/gunit.h"
-#include "system_wrappers/include/metrics_default.h"
+#include "system_wrappers/include/metrics.h"
 
 namespace webrtc {
 namespace {
diff --git a/modules/audio_processing/agc2/gain_applier.h b/modules/audio_processing/agc2/gain_applier.h
index a4f56ce..e2567b1 100644
--- a/modules/audio_processing/agc2/gain_applier.h
+++ b/modules/audio_processing/agc2/gain_applier.h
@@ -20,6 +20,7 @@
 
   void ApplyGain(AudioFrameView<float> signal);
   void SetGainFactor(float gain_factor);
+  float GetGainFactor() const { return current_gain_factor_; };
 
  private:
   void Initialize(size_t samples_per_channel);
diff --git a/modules/audio_processing/agc2/gain_curve_applier.cc b/modules/audio_processing/agc2/gain_curve_applier.cc
index b52e0d7..1eca21b 100644
--- a/modules/audio_processing/agc2/gain_curve_applier.cc
+++ b/modules/audio_processing/agc2/gain_curve_applier.cc
@@ -134,4 +134,8 @@
   level_estimator_.Reset();
 }
 
+float GainCurveApplier::LastAudioLevel() const {
+  return level_estimator_.LastAudioLevel();
+}
+
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/gain_curve_applier.h b/modules/audio_processing/agc2/gain_curve_applier.h
index a7ffa36..e0be19e 100644
--- a/modules/audio_processing/agc2/gain_curve_applier.h
+++ b/modules/audio_processing/agc2/gain_curve_applier.h
@@ -42,6 +42,8 @@
   // Resets the internal state.
   void Reset();
 
+  float LastAudioLevel() const;
+
  private:
   const InterpolatedGainCurve interp_gain_curve_;
   FixedDigitalLevelEstimator level_estimator_;
diff --git a/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.cc b/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.cc
index 4cbe48a..9f4e218 100644
--- a/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.cc
+++ b/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.cc
@@ -20,7 +20,10 @@
 namespace {
 
 // DCT scaling factor.
-const float kDctScalingFactor = std::sqrt(2.f / kNumBands);
+static_assert(
+    kNumBands == 22,
+    "kNumBands changed! Please update the value of kDctScalingFactor");
+constexpr float kDctScalingFactor = 0.301511345f;  // sqrt(2 / kNumBands)
 
 }  // namespace
 
diff --git a/modules/audio_processing/agc2/saturation_protector.cc b/modules/audio_processing/agc2/saturation_protector.cc
index dc9be47..0895583 100644
--- a/modules/audio_processing/agc2/saturation_protector.cc
+++ b/modules/audio_processing/agc2/saturation_protector.cc
@@ -56,7 +56,9 @@
 }
 
 SaturationProtector::SaturationProtector(ApmDataDumper* apm_data_dumper)
-    : apm_data_dumper_(apm_data_dumper) {}
+    : apm_data_dumper_(apm_data_dumper),
+      last_margin_(GetInitialSaturationMarginDb()),
+      extra_saturation_margin_db_(GetExtraSaturationMarginOffsetDb()) {}
 
 void SaturationProtector::UpdateMargin(
     const VadWithLevel::LevelAndProbability& vad_data,
@@ -77,7 +79,7 @@
 }
 
 float SaturationProtector::LastMargin() const {
-  return last_margin_;
+  return last_margin_ + extra_saturation_margin_db_;
 }
 
 void SaturationProtector::Reset() {
diff --git a/modules/audio_processing/agc2/saturation_protector.h b/modules/audio_processing/agc2/saturation_protector.h
index 3f207da..1705f6a 100644
--- a/modules/audio_processing/agc2/saturation_protector.h
+++ b/modules/audio_processing/agc2/saturation_protector.h
@@ -58,8 +58,9 @@
 
   ApmDataDumper* apm_data_dumper_;
 
-  float last_margin_ = kInitialSaturationMarginDb;
+  float last_margin_;
   PeakEnveloper peak_enveloper_;
+  float extra_saturation_margin_db_;
 };
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/agc2/saturation_protector_unittest.cc b/modules/audio_processing/agc2/saturation_protector_unittest.cc
index 6013e13..e767644 100644
--- a/modules/audio_processing/agc2/saturation_protector_unittest.cc
+++ b/modules/audio_processing/agc2/saturation_protector_unittest.cc
@@ -54,16 +54,18 @@
   SaturationProtector saturation_protector(&apm_data_dumper);
 
   constexpr float kPeakLevel = -20.f;
-  constexpr float kCrestFactor = kInitialSaturationMarginDb + 1.f;
-  constexpr float kSpeechLevel = kPeakLevel - kCrestFactor;
+  const float kCrestFactor = GetInitialSaturationMarginDb() + 1.f;
+  const float kSpeechLevel = kPeakLevel - kCrestFactor;
   const float kMaxDifference =
-      0.5 * std::abs(kInitialSaturationMarginDb - kCrestFactor);
+      0.5 * std::abs(GetInitialSaturationMarginDb() - kCrestFactor);
 
   static_cast<void>(RunOnConstantLevel(
       2000, VadWithLevel::LevelAndProbability(1.f, -90.f, kPeakLevel),
       kSpeechLevel, &saturation_protector));
 
-  EXPECT_NEAR(saturation_protector.LastMargin(), kCrestFactor, kMaxDifference);
+  EXPECT_NEAR(
+      saturation_protector.LastMargin() - GetExtraSaturationMarginOffsetDb(),
+      kCrestFactor, kMaxDifference);
 }
 
 TEST(AutomaticGainController2SaturationProtector, ProtectorChangesSlowly) {
@@ -71,10 +73,10 @@
   SaturationProtector saturation_protector(&apm_data_dumper);
 
   constexpr float kPeakLevel = -20.f;
-  constexpr float kCrestFactor = kInitialSaturationMarginDb - 5.f;
-  constexpr float kOtherCrestFactor = kInitialSaturationMarginDb;
-  constexpr float kSpeechLevel = kPeakLevel - kCrestFactor;
-  constexpr float kOtherSpeechLevel = kPeakLevel - kOtherCrestFactor;
+  const float kCrestFactor = GetInitialSaturationMarginDb() - 5.f;
+  const float kOtherCrestFactor = GetInitialSaturationMarginDb();
+  const float kSpeechLevel = kPeakLevel - kCrestFactor;
+  const float kOtherSpeechLevel = kPeakLevel - kOtherCrestFactor;
 
   constexpr int kNumIterations = 1000;
   float max_difference = RunOnConstantLevel(
@@ -107,26 +109,28 @@
   float max_difference = RunOnConstantLevel(
       kDelayIterations,
       VadWithLevel::LevelAndProbability(
-          1.f, -90.f, kInitialSpeechLevelDbfs + kInitialSaturationMarginDb),
+          1.f, -90.f, kInitialSpeechLevelDbfs + GetInitialSaturationMarginDb()),
       kInitialSpeechLevelDbfs, &saturation_protector);
 
   // Then peak changes, but not RMS.
-  max_difference = std::max(
-      RunOnConstantLevel(
-          kDelayIterations,
-          VadWithLevel::LevelAndProbability(
-              1.f, -90.f, kLaterSpeechLevelDbfs + kInitialSaturationMarginDb),
-          kInitialSpeechLevelDbfs, &saturation_protector),
-      max_difference);
+  max_difference =
+      std::max(RunOnConstantLevel(
+                   kDelayIterations,
+                   VadWithLevel::LevelAndProbability(
+                       1.f, -90.f,
+                       kLaterSpeechLevelDbfs + GetInitialSaturationMarginDb()),
+                   kInitialSpeechLevelDbfs, &saturation_protector),
+               max_difference);
 
   // Then both change.
-  max_difference = std::max(
-      RunOnConstantLevel(
-          kDelayIterations,
-          VadWithLevel::LevelAndProbability(
-              1.f, -90.f, kLaterSpeechLevelDbfs + kInitialSaturationMarginDb),
-          kLaterSpeechLevelDbfs, &saturation_protector),
-      max_difference);
+  max_difference =
+      std::max(RunOnConstantLevel(
+                   kDelayIterations,
+                   VadWithLevel::LevelAndProbability(
+                       1.f, -90.f,
+                       kLaterSpeechLevelDbfs + GetInitialSaturationMarginDb()),
+                   kLaterSpeechLevelDbfs, &saturation_protector),
+               max_difference);
 
   // The saturation protector expects that the RMS changes roughly
   // 'kFullBufferSizeMs' after peaks change. This is to account for
@@ -134,8 +138,9 @@
   // above is 'normal' and 'expected', and shouldn't influence the
   // margin by much.
 
-  const float total_difference =
-      std::abs(saturation_protector.LastMargin() - kInitialSaturationMarginDb);
+  const float total_difference = std::abs(saturation_protector.LastMargin() -
+                                          GetExtraSaturationMarginOffsetDb() -
+                                          GetInitialSaturationMarginDb());
 
   EXPECT_LE(total_difference, 0.05f);
   EXPECT_LE(max_difference, 0.01f);
diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc
index 8848b73..b9153fa 100644
--- a/modules/audio_processing/audio_processing_impl.cc
+++ b/modules/audio_processing/audio_processing_impl.cc
@@ -24,13 +24,18 @@
 #include "modules/audio_processing/audio_buffer.h"
 #include "modules/audio_processing/common.h"
 #include "modules/audio_processing/echo_cancellation_impl.h"
-#include "modules/audio_processing/echo_cancellation_proxy.h"
 #include "modules/audio_processing/echo_control_mobile_impl.h"
-#include "modules/audio_processing/echo_control_mobile_proxy.h"
 #include "modules/audio_processing/gain_control_for_experimental_agc.h"
 #include "modules/audio_processing/gain_control_impl.h"
 #include "modules/audio_processing/gain_controller2.h"
+#include "modules/audio_processing/level_estimator_impl.h"
 #include "modules/audio_processing/logging/apm_data_dumper.h"
+#include "modules/audio_processing/low_cut_filter.h"
+#include "modules/audio_processing/noise_suppression_impl.h"
+#include "modules/audio_processing/residual_echo_detector.h"
+#include "modules/audio_processing/transient/transient_suppressor.h"
+#include "modules/audio_processing/voice_detection_impl.h"
+#include "rtc_base/atomicops.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/platform_file.h"
@@ -38,13 +43,6 @@
 #include "rtc_base/system/arch.h"
 #include "rtc_base/timeutils.h"
 #include "rtc_base/trace_event.h"
-#include "modules/audio_processing/level_estimator_impl.h"
-#include "modules/audio_processing/low_cut_filter.h"
-#include "modules/audio_processing/noise_suppression_impl.h"
-#include "modules/audio_processing/residual_echo_detector.h"
-#include "modules/audio_processing/transient/transient_suppressor.h"
-#include "modules/audio_processing/voice_detection_impl.h"
-#include "rtc_base/atomicops.h"
 #include "system_wrappers/include/metrics.h"
 
 #define RETURN_ON_ERR(expr) \
@@ -154,7 +152,7 @@
       capture_analyzer_enabled_(capture_analyzer_enabled) {}
 
 bool AudioProcessingImpl::ApmSubmoduleStates::Update(
-    bool low_cut_filter_enabled,
+    bool high_pass_filter_enabled,
     bool echo_canceller_enabled,
     bool mobile_echo_controller_enabled,
     bool residual_echo_detector_enabled,
@@ -167,7 +165,7 @@
     bool level_estimator_enabled,
     bool transient_suppressor_enabled) {
   bool changed = false;
-  changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
+  changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
   changed |= (echo_canceller_enabled != echo_canceller_enabled_);
   changed |=
       (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
@@ -185,7 +183,7 @@
       (voice_activity_detector_enabled != voice_activity_detector_enabled_);
   changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
   if (changed) {
-    low_cut_filter_enabled_ = low_cut_filter_enabled;
+    high_pass_filter_enabled_ = high_pass_filter_enabled;
     echo_canceller_enabled_ = echo_canceller_enabled;
     mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
     residual_echo_detector_enabled_ = residual_echo_detector_enabled;
@@ -211,7 +209,7 @@
 
 bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
     const {
-  return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
+  return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
          mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
          adaptive_gain_controller_enabled_ || echo_controller_enabled_;
 }
@@ -243,13 +241,16 @@
   return false;
 }
 
+bool AudioProcessingImpl::ApmSubmoduleStates::LowCutFilteringRequired() const {
+  return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
+         mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
+}
+
 struct AudioProcessingImpl::ApmPublicSubmodules {
   ApmPublicSubmodules() {}
   // Accessed externally of APM without any lock acquired.
   std::unique_ptr<EchoCancellationImpl> echo_cancellation;
   std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
-  std::unique_ptr<EchoCancellationProxy> echo_cancellation_proxy;
-  std::unique_ptr<EchoControlMobileProxy> echo_control_mobile_proxy;
   std::unique_ptr<GainControlImpl> gain_control;
   std::unique_ptr<LevelEstimatorImpl> level_estimator;
   std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
@@ -365,13 +366,15 @@
       constants_(config.Get<ExperimentalAgc>().startup_min_volume,
                  config.Get<ExperimentalAgc>().clipped_level_min,
 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
-                 false,
-                 false,
-                 false),
+                 /* enabled= */ false,
+                 /* enabled_agc2_level_estimator= */ false,
+                 /* digital_adaptive_disabled= */ false,
+                 /* analyze_before_aec= */ false),
 #else
                  config.Get<ExperimentalAgc>().enabled,
                  config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
-                 config.Get<ExperimentalAgc>().digital_adaptive_disabled),
+                 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
+                 config.Get<ExperimentalAgc>().analyze_before_aec),
 #endif
 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
       capture_(false),
@@ -391,11 +394,6 @@
         new EchoCancellationImpl(&crit_render_, &crit_capture_));
     public_submodules_->echo_control_mobile.reset(
         new EchoControlMobileImpl(&crit_render_, &crit_capture_));
-    public_submodules_->echo_cancellation_proxy.reset(new EchoCancellationProxy(
-        this, public_submodules_->echo_cancellation.get()));
-    public_submodules_->echo_control_mobile_proxy.reset(
-        new EchoControlMobileProxy(
-            this, public_submodules_->echo_control_mobile.get()));
     public_submodules_->gain_control.reset(
         new GainControlImpl(&crit_render_, &crit_capture_));
     public_submodules_->level_estimator.reset(
@@ -670,12 +668,15 @@
   rtc::CritScope cs_render(&crit_render_);
   rtc::CritScope cs_capture(&crit_capture_);
 
-  static_cast<EchoCancellation*>(public_submodules_->echo_cancellation.get())
-      ->Enable(config_.echo_canceller.enabled &&
-               !config_.echo_canceller.mobile_mode);
-  static_cast<EchoControlMobile*>(public_submodules_->echo_control_mobile.get())
-      ->Enable(config_.echo_canceller.enabled &&
-               config_.echo_canceller.mobile_mode);
+  public_submodules_->echo_cancellation->Enable(
+      config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode);
+  public_submodules_->echo_control_mobile->Enable(
+      config_.echo_canceller.enabled && config_.echo_canceller.mobile_mode);
+
+  public_submodules_->echo_cancellation->set_suppression_level(
+      config.echo_canceller.legacy_moderate_suppression_level
+          ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
+          : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
 
   InitializeLowCutFilter();
 
@@ -876,6 +877,9 @@
 void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
   RuntimeSetting setting;
   while (capture_runtime_settings_.Remove(&setting)) {
+    if (aec_dump_) {
+      aec_dump_->WriteRuntimeSetting(setting);
+    }
     switch (setting.type()) {
       case RuntimeSetting::Type::kCapturePreGain:
         if (config_.pre_amplifier.enabled) {
@@ -898,6 +902,9 @@
 void AudioProcessingImpl::HandleRenderRuntimeSettings() {
   RuntimeSetting setting;
   while (render_runtime_settings_.Remove(&setting)) {
+    if (aec_dump_) {
+      aec_dump_->WriteRuntimeSetting(setting);
+    }
     switch (setting.type()) {
       case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
         if (private_submodules_->render_pre_processor) {
@@ -1207,6 +1214,15 @@
         capture_.prev_analog_mic_level != -1;
     capture_.prev_analog_mic_level = analog_mic_level;
 
+    // Detect and flag any change in the pre-amplifier gain.
+    if (private_submodules_->pre_amplifier) {
+      float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
+      capture_.echo_path_gain_change =
+          capture_.echo_path_gain_change ||
+          (capture_.prev_pre_amp_gain != pre_amp_gain &&
+           capture_.prev_pre_amp_gain >= 0.f);
+      capture_.prev_pre_amp_gain = pre_amp_gain;
+    }
     private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
   }
 
@@ -1215,6 +1231,13 @@
     private_submodules_->agc_manager->AnalyzePreProcess(
         capture_buffer->channels()[0], capture_buffer->num_channels(),
         capture_nonlocked_.capture_processing_format.num_frames());
+
+    if (constants_.use_experimental_agc_process_before_aec) {
+      private_submodules_->agc_manager->Process(
+          capture_buffer->channels()[0],
+          capture_nonlocked_.capture_processing_format.num_frames(),
+          capture_nonlocked_.capture_processing_format.sample_rate_hz());
+    }
   }
 
   if (submodule_states_.CaptureMultiBandSubModulesActive() &&
@@ -1284,13 +1307,15 @@
   public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
 
   if (constants_.use_experimental_agc &&
-      public_submodules_->gain_control->is_enabled()) {
+      public_submodules_->gain_control->is_enabled() &&
+      !constants_.use_experimental_agc_process_before_aec) {
     private_submodules_->agc_manager->Process(
         capture_buffer->split_bands_const(0)[kBand0To8kHz],
         capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
   }
   RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
-      capture_buffer, echo_cancellation()->stream_has_echo()));
+      capture_buffer,
+      public_submodules_->echo_cancellation->stream_has_echo()));
 
   if (submodule_states_.CaptureMultiBandProcessingActive() &&
       SampleRateSupportsMultiBand(
@@ -1616,7 +1641,7 @@
 AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
     const {
   AudioProcessingStatistics stats;
-  EchoCancellation::Metrics metrics;
+  EchoCancellationImpl::Metrics metrics;
   if (private_submodules_->echo_controller) {
     rtc::CritScope cs_capture(&crit_capture_);
     auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
@@ -1652,7 +1677,7 @@
     bool has_remote_tracks) const {
   AudioProcessingStats stats;
   if (has_remote_tracks) {
-    EchoCancellation::Metrics metrics;
+    EchoCancellationImpl::Metrics metrics;
     if (private_submodules_->echo_controller) {
       rtc::CritScope cs_capture(&crit_capture_);
       auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
@@ -1699,14 +1724,6 @@
   return stats;
 }
 
-EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
-  return public_submodules_->echo_cancellation_proxy.get();
-}
-
-EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
-  return public_submodules_->echo_control_mobile_proxy.get();
-}
-
 GainControl* AudioProcessingImpl::gain_control() const {
   if (constants_.use_experimental_agc) {
     return public_submodules_->gain_control_for_experimental_agc.get();
@@ -1759,7 +1776,6 @@
       capture_.transient_suppressor_enabled);
 }
 
-
 void AudioProcessingImpl::InitializeTransient() {
   if (capture_.transient_suppressor_enabled) {
     if (!public_submodules_->transient_suppressor.get()) {
@@ -1772,7 +1788,7 @@
 }
 
 void AudioProcessingImpl::InitializeLowCutFilter() {
-  if (config_.high_pass_filter.enabled) {
+  if (submodule_states_.LowCutFilteringRequired()) {
     private_submodules_->low_cut_filter.reset(
         new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
   } else {
@@ -1836,15 +1852,15 @@
 void AudioProcessingImpl::MaybeUpdateHistograms() {
   static const int kMinDiffDelayMs = 60;
 
-  if (echo_cancellation()->is_enabled()) {
+  if (public_submodules_->echo_cancellation->is_enabled()) {
     // Activate delay_jumps_ counters if we know echo_cancellation is running.
     // If a stream has echo we know that the echo_cancellation is in process.
     if (capture_.stream_delay_jumps == -1 &&
-        echo_cancellation()->stream_has_echo()) {
+        public_submodules_->echo_cancellation->stream_has_echo()) {
       capture_.stream_delay_jumps = 0;
     }
     if (capture_.aec_system_delay_jumps == -1 &&
-        echo_cancellation()->stream_has_echo()) {
+        public_submodules_->echo_cancellation->stream_has_echo()) {
       capture_.aec_system_delay_jumps = 0;
     }
 
@@ -2036,7 +2052,8 @@
       capture_processing_format(kSampleRate16kHz),
       split_rate(kSampleRate16kHz),
       echo_path_gain_change(false),
-      prev_analog_mic_level(-1) {}
+      prev_analog_mic_level(-1),
+      prev_pre_amp_gain(-1.f) {}
 
 AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
 
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index a95e150..50cb82b 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -117,8 +117,6 @@
   // would offer no protection (the submodules are
   // created only once in a single-treaded manner
   // during APM creation).
-  EchoCancellation* echo_cancellation() const override;
-  EchoControlMobile* echo_control_mobile() const override;
   GainControl* gain_control() const override;
   // TODO(peah): Deprecate this API call.
   HighPassFilter* high_pass_filter() const override;
@@ -178,7 +176,7 @@
                        bool render_pre_processor_enabled,
                        bool capture_analyzer_enabled);
     // Updates the submodule state and returns true if it has changed.
-    bool Update(bool low_cut_filter_enabled,
+    bool Update(bool high_pass_filter_enabled,
                 bool echo_canceller_enabled,
                 bool mobile_echo_controller_enabled,
                 bool residual_echo_detector_enabled,
@@ -197,12 +195,13 @@
     bool RenderMultiBandSubModulesActive() const;
     bool RenderFullBandProcessingActive() const;
     bool RenderMultiBandProcessingActive() const;
+    bool LowCutFilteringRequired() const;
 
    private:
     const bool capture_post_processor_enabled_ = false;
     const bool render_pre_processor_enabled_ = false;
     const bool capture_analyzer_enabled_ = false;
-    bool low_cut_filter_enabled_ = false;
+    bool high_pass_filter_enabled_ = false;
     bool echo_canceller_enabled_ = false;
     bool mobile_echo_controller_enabled_ = false;
     bool residual_echo_detector_enabled_ = false;
@@ -355,7 +354,8 @@
                  int agc_clipped_level_min,
                  bool use_experimental_agc,
                  bool use_experimental_agc_agc2_level_estimation,
-                 bool use_experimental_agc_agc2_digital_adaptive)
+                 bool use_experimental_agc_agc2_digital_adaptive,
+                 bool use_experimental_agc_process_before_aec)
         :  // Format of processing streams at input/output call sites.
           agc_startup_min_volume(agc_startup_min_volume),
           agc_clipped_level_min(agc_clipped_level_min),
@@ -363,12 +363,15 @@
           use_experimental_agc_agc2_level_estimation(
               use_experimental_agc_agc2_level_estimation),
           use_experimental_agc_agc2_digital_adaptive(
-              use_experimental_agc_agc2_digital_adaptive) {}
+              use_experimental_agc_agc2_digital_adaptive),
+          use_experimental_agc_process_before_aec(
+              use_experimental_agc_process_before_aec) {}
     int agc_startup_min_volume;
     int agc_clipped_level_min;
     bool use_experimental_agc;
     bool use_experimental_agc_agc2_level_estimation;
     bool use_experimental_agc_agc2_digital_adaptive;
+    bool use_experimental_agc_process_before_aec;
 
   } constants_;
 
@@ -392,6 +395,7 @@
     int split_rate;
     bool echo_path_gain_change;
     int prev_analog_mic_level;
+    float prev_pre_amp_gain;
   } capture_ RTC_GUARDED_BY(crit_capture_);
 
   struct ApmCaptureNonLockedState {
diff --git a/modules/audio_processing/audio_processing_impl_locking_unittest.cc b/modules/audio_processing/audio_processing_impl_locking_unittest.cc
index b770fef..0e0a33f 100644
--- a/modules/audio_processing/audio_processing_impl_locking_unittest.cc
+++ b/modules/audio_processing/audio_processing_impl_locking_unittest.cc
@@ -540,31 +540,23 @@
   ASSERT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
   ASSERT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
 
+  AudioProcessing::Config apm_config;
+  apm_config.echo_canceller.enabled =
+      (test_config_.aec_type != AecType::AecTurnedOff);
+  apm_config.echo_canceller.mobile_mode =
+      (test_config_.aec_type == AecType::BasicWebRtcAecSettingsWithAecMobile);
+  apm_->ApplyConfig(apm_config);
+
   Config config;
-  if (test_config_.aec_type == AecType::AecTurnedOff) {
-    ASSERT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
-    ASSERT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
-  } else if (test_config_.aec_type ==
-             AecType::BasicWebRtcAecSettingsWithAecMobile) {
-    ASSERT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
-    ASSERT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
-  } else {
-    ASSERT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
-    ASSERT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-    ASSERT_EQ(apm_->kNoError, apm_->echo_cancellation()->enable_metrics(true));
-    ASSERT_EQ(apm_->kNoError,
-              apm_->echo_cancellation()->enable_delay_logging(true));
+  config.Set<ExtendedFilter>(
+      new ExtendedFilter(test_config_.aec_type ==
+                         AecType::BasicWebRtcAecSettingsWithExtentedFilter));
 
-    config.Set<ExtendedFilter>(
-        new ExtendedFilter(test_config_.aec_type ==
-                           AecType::BasicWebRtcAecSettingsWithExtentedFilter));
+  config.Set<DelayAgnostic>(
+      new DelayAgnostic(test_config_.aec_type ==
+                        AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec));
 
-    config.Set<DelayAgnostic>(
-        new DelayAgnostic(test_config_.aec_type ==
-                          AecType::BasicWebRtcAecSettingsWithDelayAgnosticAec));
-
-    apm_->SetExtraOptions(config);
-  }
+  apm_->SetExtraOptions(config);
 }
 
 void AudioProcessingImplLockTest::TearDown() {
@@ -585,15 +577,15 @@
 bool StatsProcessor::Process() {
   SleepRandomMs(100, rand_gen_);
 
-  EXPECT_EQ(apm_->echo_cancellation()->is_enabled(),
-            ((test_config_->aec_type != AecType::AecTurnedOff) &&
-             (test_config_->aec_type !=
-              AecType::BasicWebRtcAecSettingsWithAecMobile)));
-  apm_->echo_cancellation()->stream_drift_samples();
-  EXPECT_EQ(apm_->echo_control_mobile()->is_enabled(),
-            (test_config_->aec_type != AecType::AecTurnedOff) &&
-                (test_config_->aec_type ==
-                 AecType::BasicWebRtcAecSettingsWithAecMobile));
+  AudioProcessing::Config apm_config = apm_->GetConfig();
+  if (test_config_->aec_type != AecType::AecTurnedOff) {
+    EXPECT_TRUE(apm_config.echo_canceller.enabled);
+    EXPECT_EQ(apm_config.echo_canceller.mobile_mode,
+              (test_config_->aec_type ==
+               AecType::BasicWebRtcAecSettingsWithAecMobile));
+  } else {
+    EXPECT_FALSE(apm_config.echo_canceller.enabled);
+  }
   EXPECT_TRUE(apm_->gain_control()->is_enabled());
   EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
 
diff --git a/modules/audio_processing/audio_processing_performance_unittest.cc b/modules/audio_processing/audio_processing_performance_unittest.cc
index df8d5fe..95f736c 100644
--- a/modules/audio_processing/audio_processing_performance_unittest.cc
+++ b/modules/audio_processing/audio_processing_performance_unittest.cc
@@ -452,11 +452,10 @@
       ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true));
       ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true));
       ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true));
-      ASSERT_EQ(apm->kNoError, apm->echo_control_mobile()->Enable(false));
-      ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(true));
-      ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->enable_metrics(true));
-      ASSERT_EQ(apm->kNoError,
-                apm->echo_cancellation()->enable_delay_logging(true));
+      AudioProcessing::Config apm_config = apm->GetConfig();
+      apm_config.echo_canceller.enabled = true;
+      apm_config.echo_canceller.mobile_mode = false;
+      apm->ApplyConfig(apm_config);
     };
 
     // Lambda function for setting the default APM runtime settings for mobile.
@@ -468,8 +467,10 @@
       ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(true));
       ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(true));
       ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(true));
-      ASSERT_EQ(apm->kNoError, apm->echo_control_mobile()->Enable(true));
-      ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(false));
+      AudioProcessing::Config apm_config = apm->GetConfig();
+      apm_config.echo_canceller.enabled = true;
+      apm_config.echo_canceller.mobile_mode = true;
+      apm->ApplyConfig(apm_config);
     };
 
     // Lambda function for turning off all of the APM runtime settings
@@ -482,11 +483,9 @@
       ASSERT_EQ(apm->kNoError, apm->gain_control()->Enable(false));
       ASSERT_EQ(apm->kNoError, apm->noise_suppression()->Enable(false));
       ASSERT_EQ(apm->kNoError, apm->voice_detection()->Enable(false));
-      ASSERT_EQ(apm->kNoError, apm->echo_control_mobile()->Enable(false));
-      ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->Enable(false));
-      ASSERT_EQ(apm->kNoError, apm->echo_cancellation()->enable_metrics(false));
-      ASSERT_EQ(apm->kNoError,
-                apm->echo_cancellation()->enable_delay_logging(false));
+      AudioProcessing::Config apm_config = apm->GetConfig();
+      apm_config.echo_canceller.enabled = false;
+      apm->ApplyConfig(apm_config);
     };
 
     // Lambda function for adding default desktop APM settings to a config.
diff --git a/modules/audio_processing/audio_processing_unittest.cc b/modules/audio_processing/audio_processing_unittest.cc
index b04f89a..3f0decc 100644
--- a/modules/audio_processing/audio_processing_unittest.cc
+++ b/modules/audio_processing/audio_processing_unittest.cc
@@ -35,6 +35,7 @@
 #include "rtc_base/numerics/safe_minmax.h"
 #include "rtc_base/protobuf_utils.h"
 #include "rtc_base/refcountedobject.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/swap_queue.h"
 #include "rtc_base/system/arch.h"
 #include "rtc_base/task_queue.h"
@@ -187,11 +188,7 @@
   EXPECT_NOERR(ap->gain_control()->Enable(true));
 #elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
   apm_config.echo_canceller.mobile_mode = false;
-  EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
-  EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
-  EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
-  EXPECT_NOERR(ap->echo_cancellation()->set_suppression_level(
-      EchoCancellation::SuppressionLevel::kModerateSuppression));
+  apm_config.echo_canceller.legacy_moderate_suppression_level = true;
 
   EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
   EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
@@ -224,24 +221,6 @@
   return max_data;
 }
 
-#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
-void TestStats(const AudioProcessing::Statistic& test,
-               const audioproc::Test::Statistic& reference) {
-  EXPECT_EQ(reference.instant(), test.instant);
-  EXPECT_EQ(reference.average(), test.average);
-  EXPECT_EQ(reference.maximum(), test.maximum);
-  EXPECT_EQ(reference.minimum(), test.minimum);
-}
-
-void WriteStatsMessage(const AudioProcessing::Statistic& output,
-                       audioproc::Test::Statistic* msg) {
-  msg->set_instant(output.instant);
-  msg->set_average(output.average);
-  msg->set_maximum(output.maximum);
-  msg->set_minimum(output.minimum);
-}
-#endif
-
 void OpenFileAndWriteMessage(const std::string& filename,
                              const MessageLite& msg) {
   FILE* file = fopen(filename.c_str(), "wb");
@@ -259,7 +238,7 @@
 }
 
 std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   // Resource files are all stereo.
   ss << name << sample_rate_hz / 1000 << "_stereo";
   return test::ResourcePath(ss.str(), "pcm");
@@ -280,7 +259,7 @@
                            size_t num_reverse_input_channels,
                            size_t num_reverse_output_channels,
                            StreamDirection file_direction) {
-  std::ostringstream ss;
+  rtc::StringBuilder ss;
   ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
      << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
   if (num_output_channels == 1) {
@@ -597,7 +576,6 @@
 
 void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
   EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
   EXPECT_EQ(apm_->kNoError,
       apm_->gain_control()->set_stream_analog_level(127));
   EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
@@ -682,13 +660,8 @@
     delete frame;
 
     if (frame_count == 250) {
-      int median;
-      int std;
-      float poor_fraction;
       // Discard the first delay metrics to avoid convergence effects.
-      EXPECT_EQ(apm_->kNoError,
-                apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
-                                                           &poor_fraction));
+      static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
     }
   }
 
@@ -711,12 +684,10 @@
       expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
       delay_max);
   // Verify delay metrics.
-  int median;
-  int std;
-  float poor_fraction;
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
-                                                       &poor_fraction));
+  AudioProcessingStats stats =
+      apm_->GetStatistics(true /* has_remote_tracks */);
+  ASSERT_TRUE(stats.delay_median_ms.has_value());
+  int32_t median = *stats.delay_median_ms;
   EXPECT_GE(expected_median_high, median);
   EXPECT_LE(expected_median_low, median);
 }
@@ -738,19 +709,16 @@
             ProcessStreamChooser(format));
 
   // Other stream parameters set correctly.
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(true));
+  AudioProcessing::Config apm_config = apm_->GetConfig();
+  apm_config.echo_canceller.enabled = true;
+  apm_config.echo_canceller.mobile_mode = false;
+  apm_->ApplyConfig(apm_config);
   EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
   EXPECT_EQ(apm_->kStreamParameterNotSetError,
             ProcessStreamChooser(format));
   EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(false));
 
   // -- Missing delay --
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
   EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
   EXPECT_EQ(apm_->kStreamParameterNotSetError,
             ProcessStreamChooser(format));
@@ -764,33 +732,11 @@
   // Other stream parameters set correctly.
   EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
   EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(true));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
-  EXPECT_EQ(apm_->kNoError,
             apm_->gain_control()->set_stream_analog_level(127));
   EXPECT_EQ(apm_->kStreamParameterNotSetError,
             ProcessStreamChooser(format));
   EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
 
-  // -- Missing drift --
-  EXPECT_EQ(apm_->kStreamParameterNotSetError,
-            ProcessStreamChooser(format));
-
-  // Resets after successful ProcessStream().
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
-  EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
-  EXPECT_EQ(apm_->kStreamParameterNotSetError,
-            ProcessStreamChooser(format));
-
-  // Other stream parameters set correctly.
-  EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->gain_control()->set_stream_analog_level(127));
-  EXPECT_EQ(apm_->kStreamParameterNotSetError,
-            ProcessStreamChooser(format));
-
   // -- No stream parameters --
   EXPECT_EQ(apm_->kNoError,
             AnalyzeReverseStreamChooser(format));
@@ -799,7 +745,6 @@
 
   // -- All there --
   EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
   EXPECT_EQ(apm_->kNoError,
             apm_->gain_control()->set_stream_analog_level(127));
   EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
@@ -923,79 +868,13 @@
   }
 }
 
-TEST_F(ApmTest, EchoCancellation) {
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
-
-  EchoCancellation::SuppressionLevel level[] = {
-    EchoCancellation::kLowSuppression,
-    EchoCancellation::kModerateSuppression,
-    EchoCancellation::kHighSuppression,
-  };
-  for (size_t i = 0; i < arraysize(level); i++) {
-    EXPECT_EQ(apm_->kNoError,
-        apm_->echo_cancellation()->set_suppression_level(level[i]));
-    EXPECT_EQ(level[i],
-        apm_->echo_cancellation()->suppression_level());
-  }
-
-  EchoCancellation::Metrics metrics;
-  EXPECT_EQ(apm_->kNotEnabledError,
-            apm_->echo_cancellation()->GetMetrics(&metrics));
-
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
-
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_metrics(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_metrics(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
-
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_delay_logging(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_delay_logging(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
-
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
-
-  int median = 0;
-  int std = 0;
-  float poor_fraction = 0;
-  EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
-                                        &median, &std, &poor_fraction));
-
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
-
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-  EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
-  EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
-  EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
-  EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
-}
-
 TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
   // TODO(bjornv): Fix this test to work with DA-AEC.
   // Enable AEC only.
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_drift_compensation(false));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_metrics(false));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_cancellation()->enable_delay_logging(true));
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
+  AudioProcessing::Config apm_config = apm_->GetConfig();
+  apm_config.echo_canceller.enabled = true;
+  apm_config.echo_canceller.mobile_mode = false;
+  apm_->ApplyConfig(apm_config);
   Config config;
   config.Set<DelayAgnostic>(new DelayAgnostic(false));
   apm_->SetExtraOptions(config);
@@ -1064,74 +943,6 @@
   }
 }
 
-TEST_F(ApmTest, EchoControlMobile) {
-  // Turn AECM on (and AEC off)
-  Init(16000, 16000, 16000, 2, 2, 2, false);
-  EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
-  EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
-
-  // Toggle routing modes
-  EchoControlMobile::RoutingMode mode[] = {
-      EchoControlMobile::kQuietEarpieceOrHeadset,
-      EchoControlMobile::kEarpiece,
-      EchoControlMobile::kLoudEarpiece,
-      EchoControlMobile::kSpeakerphone,
-      EchoControlMobile::kLoudSpeakerphone,
-  };
-  for (size_t i = 0; i < arraysize(mode); i++) {
-    EXPECT_EQ(apm_->kNoError,
-        apm_->echo_control_mobile()->set_routing_mode(mode[i]));
-    EXPECT_EQ(mode[i],
-        apm_->echo_control_mobile()->routing_mode());
-  }
-  // Turn comfort noise off/on
-  EXPECT_EQ(apm_->kNoError,
-      apm_->echo_control_mobile()->enable_comfort_noise(false));
-  EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
-  EXPECT_EQ(apm_->kNoError,
-      apm_->echo_control_mobile()->enable_comfort_noise(true));
-  EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
-  // Set and get echo path
-  const size_t echo_path_size =
-      apm_->echo_control_mobile()->echo_path_size_bytes();
-  std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
-  std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
-  EXPECT_EQ(apm_->kNullPointerError,
-            apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
-  EXPECT_EQ(apm_->kNullPointerError,
-            apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
-  EXPECT_EQ(apm_->kBadParameterError,
-            apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
-                                                     echo_path_size));
-  for (size_t i = 0; i < echo_path_size; i++) {
-    echo_path_in[i] = echo_path_out[i] + 1;
-  }
-  EXPECT_EQ(apm_->kBadParameterError,
-            apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
-                                                     echo_path_size));
-  EXPECT_EQ(apm_->kNoError,
-            apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
-                                                     echo_path_size));
-  for (size_t i = 0; i < echo_path_size; i++) {
-    EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
-  }
-
-  // Process a few frames with NS in the default disabled state. This exercises
-  // a different codepath than with it enabled.
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
-
-  // Turn AECM off
-  EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
-  EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
-}
-
 TEST_F(ApmTest, GainControl) {
   // Testing gain modes
   EXPECT_EQ(apm_->kNoError,
@@ -1463,8 +1274,9 @@
 }
 
 TEST_F(ApmTest, AllProcessingDisabledByDefault) {
-  EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
-  EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
+  AudioProcessing::Config config = apm_->GetConfig();
+  EXPECT_FALSE(config.echo_canceller.enabled);
+  EXPECT_FALSE(config.high_pass_filter.enabled);
   EXPECT_FALSE(apm_->gain_control()->is_enabled());
   EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
   EXPECT_FALSE(apm_->level_estimator()->is_enabled());
@@ -1547,7 +1359,6 @@
       frame_->vad_activity_ = AudioFrame::kVadUnknown;
 
       ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
-      apm_->echo_cancellation()->set_stream_drift_samples(0);
       ASSERT_EQ(kNoErr,
           apm_->gain_control()->set_stream_analog_level(analog_level));
       ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
@@ -1599,39 +1410,18 @@
   EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
   EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
 
-  // 5. Not using super-wb.
-  frame_->samples_per_channel_ = 160;
-  frame_->num_channels_ = 2;
-  frame_->sample_rate_hz_ = 16000;
-  // Enable AEC, which would require the filter in super-wb. We rely on the
-  // first few frames of data being unaffected by the AEC.
-  // TODO(andrew): This test, and the one below, rely rather tenuously on the
-  // behavior of the AEC. Think of something more robust.
-  EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
-  // Make sure we have extended filter enabled. This makes sure nothing is
-  // touched until we have a farend frame.
-  Config config;
-  config.Set<ExtendedFilter>(new ExtendedFilter(true));
-  apm_->SetExtraOptions(config);
-  SetFrameTo(frame_, 1000);
-  frame_copy.CopyFrom(*frame_);
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
-  EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
-  EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
-  EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
-  EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
-
   // Check the test is valid. We should have distortion from the filter
   // when AEC is enabled (which won't affect the audio).
+  AudioProcessing::Config apm_config = apm_->GetConfig();
+  apm_config.echo_canceller.enabled = true;
+  apm_config.echo_canceller.mobile_mode = false;
+  apm_->ApplyConfig(apm_config);
   frame_->samples_per_channel_ = 320;
   frame_->num_channels_ = 2;
   frame_->sample_rate_hz_ = 32000;
   SetFrameTo(frame_, 1000);
   frame_copy.CopyFrom(*frame_);
   EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-  apm_->echo_cancellation()->set_stream_drift_samples(0);
   EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
   EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
 }
@@ -1703,7 +1493,6 @@
 
       EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
       EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
-      apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
       if (msg.has_keypress()) {
         apm_->set_stream_key_pressed(msg.keypress());
       } else {
@@ -1924,8 +1713,6 @@
 
       EXPECT_NOERR(apm_->set_stream_delay_ms(0));
       EXPECT_NOERR(fapm->set_stream_delay_ms(0));
-      apm_->echo_cancellation()->set_stream_drift_samples(0);
-      fapm->echo_cancellation()->set_stream_drift_samples(0);
       EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
       EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
 
@@ -1962,8 +1749,6 @@
       analog_level = fapm->gain_control()->stream_analog_level();
       EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
                 fapm->gain_control()->stream_analog_level());
-      EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
-                fapm->echo_cancellation()->stream_has_echo());
       EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
                   fapm->noise_suppression()->speech_probability(),
                   0.01);
@@ -2046,7 +1831,6 @@
          true);
 
     int frame_count = 0;
-    int has_echo_count = 0;
     int has_voice_count = 0;
     int is_saturated_count = 0;
     int analog_level = 127;
@@ -2063,7 +1847,6 @@
       frame_->vad_activity_ = AudioFrame::kVadUnknown;
 
       EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
-      apm_->echo_cancellation()->set_stream_drift_samples(0);
       EXPECT_EQ(apm_->kNoError,
           apm_->gain_control()->set_stream_analog_level(analog_level));
 
@@ -2075,10 +1858,6 @@
 
       max_output_average += MaxAudioFrame(*frame_);
 
-      if (apm_->echo_cancellation()->stream_has_echo()) {
-        has_echo_count++;
-      }
-
       analog_level = apm_->gain_control()->stream_analog_level();
       analog_level_average += analog_level;
       if (apm_->gain_control()->stream_is_saturated()) {
@@ -2107,18 +1886,25 @@
 #if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
       const int kStatsAggregationFrameNum = 100;  // 1 second.
       if (frame_count % kStatsAggregationFrameNum == 0) {
-        // Get echo metrics.
-        EchoCancellation::Metrics echo_metrics;
-        EXPECT_EQ(apm_->kNoError,
-                  apm_->echo_cancellation()->GetMetrics(&echo_metrics));
+        // Get echo and delay metrics.
+        AudioProcessingStats stats =
+            apm_->GetStatistics(true /* has_remote_tracks */);
 
-        // Get delay metrics.
-        int median = 0;
-        int std = 0;
-        float fraction_poor_delays = 0;
-        EXPECT_EQ(apm_->kNoError,
-                  apm_->echo_cancellation()->GetDelayMetrics(
-                      &median, &std, &fraction_poor_delays));
+        // Echo metrics.
+        const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
+        const float echo_return_loss_enhancement =
+            stats.echo_return_loss_enhancement.value_or(-1.0f);
+        const float divergent_filter_fraction =
+            stats.divergent_filter_fraction.value_or(-1.0f);
+        const float residual_echo_likelihood =
+            stats.residual_echo_likelihood.value_or(-1.0f);
+        const float residual_echo_likelihood_recent_max =
+            stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
+
+        // Delay metrics.
+        const int32_t delay_median_ms = stats.delay_median_ms.value_or(-1.0);
+        const int32_t delay_standard_deviation_ms =
+            stats.delay_standard_deviation_ms.value_or(-1.0);
 
         // Get RMS.
         int rms_level = apm_->level_estimator()->RMS();
@@ -2128,46 +1914,40 @@
         if (!write_ref_data) {
           const audioproc::Test::EchoMetrics& reference =
               test->echo_metrics(stats_index);
-          TestStats(echo_metrics.residual_echo_return_loss,
-                    reference.residual_echo_return_loss());
-          TestStats(echo_metrics.echo_return_loss,
-                    reference.echo_return_loss());
-          TestStats(echo_metrics.echo_return_loss_enhancement,
-                    reference.echo_return_loss_enhancement());
-          TestStats(echo_metrics.a_nlp,
-                    reference.a_nlp());
-          EXPECT_EQ(echo_metrics.divergent_filter_fraction,
-                    reference.divergent_filter_fraction());
+          constexpr float kEpsilon = 0.01;
+          EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
+          EXPECT_NEAR(echo_return_loss_enhancement,
+                      reference.echo_return_loss_enhancement(), kEpsilon);
+          EXPECT_NEAR(divergent_filter_fraction,
+                      reference.divergent_filter_fraction(), kEpsilon);
+          EXPECT_NEAR(residual_echo_likelihood,
+                      reference.residual_echo_likelihood(), kEpsilon);
+          EXPECT_NEAR(residual_echo_likelihood_recent_max,
+                      reference.residual_echo_likelihood_recent_max(),
+                      kEpsilon);
 
           const audioproc::Test::DelayMetrics& reference_delay =
               test->delay_metrics(stats_index);
-          EXPECT_EQ(reference_delay.median(), median);
-          EXPECT_EQ(reference_delay.std(), std);
-          EXPECT_EQ(reference_delay.fraction_poor_delays(),
-                    fraction_poor_delays);
+          EXPECT_EQ(reference_delay.median(), delay_median_ms);
+          EXPECT_EQ(reference_delay.std(), delay_standard_deviation_ms);
 
           EXPECT_EQ(test->rms_level(stats_index), rms_level);
 
           ++stats_index;
         } else {
-          audioproc::Test::EchoMetrics* message =
-              test->add_echo_metrics();
-          WriteStatsMessage(echo_metrics.residual_echo_return_loss,
-                            message->mutable_residual_echo_return_loss());
-          WriteStatsMessage(echo_metrics.echo_return_loss,
-                            message->mutable_echo_return_loss());
-          WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
-                            message->mutable_echo_return_loss_enhancement());
-          WriteStatsMessage(echo_metrics.a_nlp,
-                            message->mutable_a_nlp());
-          message->set_divergent_filter_fraction(
-              echo_metrics.divergent_filter_fraction);
-
+          audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
+          message_echo->set_echo_return_loss(echo_return_loss);
+          message_echo->set_echo_return_loss_enhancement(
+              echo_return_loss_enhancement);
+          message_echo->set_divergent_filter_fraction(
+              divergent_filter_fraction);
+          message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
+          message_echo->set_residual_echo_likelihood_recent_max(
+              residual_echo_likelihood_recent_max);
           audioproc::Test::DelayMetrics* message_delay =
               test->add_delay_metrics();
-          message_delay->set_median(median);
-          message_delay->set_std(std);
-          message_delay->set_fraction_poor_delays(fraction_poor_delays);
+          message_delay->set_median(delay_median_ms);
+          message_delay->set_std(delay_standard_deviation_ms);
 
           test->add_rms_level(rms_level);
         }
@@ -2186,7 +1966,7 @@
       // TODO(bjornv): If we start getting more of these offsets on Android we
       // should consider a different approach. Either using one slack for all,
       // or generate a separate android reference.
-#if defined(WEBRTC_ANDROID)
+#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
       const int kHasVoiceCountOffset = 3;
       const int kHasVoiceCountNear = 8;
       const int kMaxOutputAverageOffset = 9;
@@ -2197,7 +1977,6 @@
       const int kMaxOutputAverageOffset = 0;
       const int kMaxOutputAverageNear = kIntNear;
 #endif
-      EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
       EXPECT_NEAR(test->has_voice_count(),
                   has_voice_count - kHasVoiceCountOffset,
                   kHasVoiceCountNear);
@@ -2214,7 +1993,6 @@
                   kFloatNear);
 #endif
     } else {
-      test->set_has_echo_count(has_echo_count);
       test->set_has_voice_count(has_voice_count);
       test->set_is_saturated_count(is_saturated_count);
 
@@ -2434,7 +2212,6 @@
           processing_config.reverse_output_stream(), rev_out_cb.channels()));
 
       EXPECT_NOERR(ap->set_stream_delay_ms(0));
-      ap->echo_cancellation()->set_stream_drift_samples(0);
       EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
 
       EXPECT_NOERR(ap->ProcessStream(
@@ -2893,25 +2670,17 @@
   }
 
   // Disable all components except for an AEC and the residual echo detector.
-  AudioProcessing::Config config;
-  config.residual_echo_detector.enabled = true;
-  config.high_pass_filter.enabled = false;
-  config.gain_controller2.enabled = false;
-  apm->ApplyConfig(config);
+  AudioProcessing::Config apm_config;
+  apm_config.residual_echo_detector.enabled = true;
+  apm_config.high_pass_filter.enabled = false;
+  apm_config.gain_controller2.enabled = false;
+  apm_config.echo_canceller.enabled = true;
+  apm_config.echo_canceller.mobile_mode = !use_AEC2;
+  apm->ApplyConfig(apm_config);
   EXPECT_EQ(apm->gain_control()->Enable(false), 0);
   EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
   EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
   EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
-
-  if (use_AEC2) {
-    EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
-    EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
-    EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
-    EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
-  } else {
-    EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
-    EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
-  }
   return apm;
 }
 
diff --git a/modules/audio_processing/debug.proto b/modules/audio_processing/debug.proto
index b19f7fe..40bd5d5 100644
--- a/modules/audio_processing/debug.proto
+++ b/modules/audio_processing/debug.proto
@@ -55,8 +55,8 @@
   optional int32 aec_suppression_level = 5;
   // Mobile AEC.
   optional bool aecm_enabled = 6;
-  optional bool aecm_comfort_noise_enabled = 7;
-  optional int32 aecm_routing_mode = 8;
+  optional bool aecm_comfort_noise_enabled = 7 [deprecated = true];
+  optional int32 aecm_routing_mode = 8 [deprecated = true];
   // Automatic gain controller.
   optional bool agc_enabled = 9;
   optional int32 agc_mode = 10;
@@ -80,6 +80,11 @@
   // Next field number 21.
 }
 
+message RuntimeSetting {
+  optional float capture_pre_gain = 1;
+  optional float custom_render_processing_setting = 2;
+}
+
 message Event {
   enum Type {
     INIT = 0;
@@ -87,6 +92,7 @@
     STREAM = 2;
     CONFIG = 3;
     UNKNOWN_EVENT = 4;
+    RUNTIME_SETTING = 5;
   }
 
   required Type type = 1;
@@ -95,4 +101,5 @@
   optional ReverseStream reverse_stream = 3;
   optional Stream stream = 4;
   optional Config config = 5;
+  optional RuntimeSetting runtime_setting = 6;
 }
diff --git a/modules/audio_processing/echo_cancellation_bit_exact_unittest.cc b/modules/audio_processing/echo_cancellation_bit_exact_unittest.cc
index 857cb1c..7f60c4b 100644
--- a/modules/audio_processing/echo_cancellation_bit_exact_unittest.cc
+++ b/modules/audio_processing/echo_cancellation_bit_exact_unittest.cc
@@ -22,14 +22,13 @@
 const int kNumFramesToProcess = 100;
 
 void SetupComponent(int sample_rate_hz,
-                    EchoCancellation::SuppressionLevel suppression_level,
+                    EchoCancellationImpl::SuppressionLevel suppression_level,
                     bool drift_compensation_enabled,
                     EchoCancellationImpl* echo_canceller) {
   echo_canceller->Initialize(sample_rate_hz, 1, 1, 1);
-  EchoCancellation* ec = static_cast<EchoCancellation*>(echo_canceller);
-  ec->Enable(true);
-  ec->set_suppression_level(suppression_level);
-  ec->enable_drift_compensation(drift_compensation_enabled);
+  echo_canceller->Enable(true);
+  echo_canceller->set_suppression_level(suppression_level);
+  echo_canceller->enable_drift_compensation(drift_compensation_enabled);
 
   Config config;
   config.Set<DelayAgnostic>(new DelayAgnostic(true));
@@ -56,8 +55,7 @@
   echo_canceller->ProcessRenderAudio(render_audio);
 
   if (drift_compensation_enabled) {
-    static_cast<EchoCancellation*>(echo_canceller)
-        ->set_stream_drift_samples(stream_drift_samples);
+    echo_canceller->set_stream_drift_samples(stream_drift_samples);
   }
 
   echo_canceller->ProcessCaptureAudio(capture_audio_buffer, stream_delay_ms);
@@ -67,14 +65,15 @@
   }
 }
 
-void RunBitexactnessTest(int sample_rate_hz,
-                         size_t num_channels,
-                         int stream_delay_ms,
-                         bool drift_compensation_enabled,
-                         int stream_drift_samples,
-                         EchoCancellation::SuppressionLevel suppression_level,
-                         bool stream_has_echo_reference,
-                         const rtc::ArrayView<const float>& output_reference) {
+void RunBitexactnessTest(
+    int sample_rate_hz,
+    size_t num_channels,
+    int stream_delay_ms,
+    bool drift_compensation_enabled,
+    int stream_drift_samples,
+    EchoCancellationImpl::SuppressionLevel suppression_level,
+    bool stream_has_echo_reference,
+    const rtc::ArrayView<const float>& output_reference) {
   rtc::CriticalSection crit_render;
   rtc::CriticalSection crit_capture;
   EchoCancellationImpl echo_canceller(&crit_render, &crit_capture);
@@ -118,8 +117,7 @@
   test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
                                      &capture_output);
 
-  EXPECT_EQ(stream_has_echo_reference,
-            static_cast<EchoCancellation*>(&echo_canceller)->stream_has_echo());
+  EXPECT_EQ(stream_has_echo_reference, echo_canceller.stream_has_echo());
 
   // Compare the output with the reference. Only the first values of the output
   // from last frame processed are compared in order not having to specify all
@@ -150,7 +148,7 @@
 #endif
   const float kOutputReference[] = {-0.000646f, -0.001525f, 0.002688f};
   RunBitexactnessTest(8000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -164,7 +162,7 @@
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
   RunBitexactnessTest(16000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -178,7 +176,7 @@
 #endif
   const float kOutputReference[] = {-0.000671f, 0.000061f, -0.000031f};
   RunBitexactnessTest(32000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -192,7 +190,7 @@
 #endif
   const float kOutputReference[] = {-0.001403f, -0.001411f, -0.000755f};
   RunBitexactnessTest(48000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -210,7 +208,7 @@
   const float kOutputReference[] = {-0.000009f, 0.000363f, 0.001094f};
 #endif
   RunBitexactnessTest(16000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kLowSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kLowSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -223,9 +221,10 @@
      DISABLED_Mono16kHz_ModerateLevel_NoDrift_StreamDelay0) {
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
-  RunBitexactnessTest(16000, 1, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kModerateSuppression,
-                      kStreamHasEchoReference, kOutputReference);
+  RunBitexactnessTest(
+      16000, 1, 0, false, 0,
+      EchoCancellationImpl::SuppressionLevel::kModerateSuppression,
+      kStreamHasEchoReference, kOutputReference);
 }
 
 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
@@ -238,7 +237,7 @@
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
   RunBitexactnessTest(16000, 1, 10, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -252,7 +251,7 @@
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
   RunBitexactnessTest(16000, 1, 20, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -266,7 +265,7 @@
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
   RunBitexactnessTest(16000, 1, 0, true, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -280,7 +279,7 @@
 #endif
   const float kOutputReference[] = {0.000055f, 0.000421f, 0.001149f};
   RunBitexactnessTest(16000, 1, 0, true, 5,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -300,7 +299,7 @@
                                     -0.000464f, -0.001525f, 0.002933f};
 #endif
   RunBitexactnessTest(8000, 2, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -315,7 +314,7 @@
   const float kOutputReference[] = {0.000166f, 0.000735f, 0.000841f,
                                     0.000166f, 0.000735f, 0.000841f};
   RunBitexactnessTest(16000, 2, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -335,7 +334,7 @@
                                     -0.000427f, 0.000183f, 0.000183f};
 #endif
   RunBitexactnessTest(32000, 2, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
@@ -350,7 +349,7 @@
   const float kOutputReference[] = {-0.001101f, -0.001101f, -0.000449f,
                                     -0.001101f, -0.001101f, -0.000449f};
   RunBitexactnessTest(48000, 2, 0, false, 0,
-                      EchoCancellation::SuppressionLevel::kHighSuppression,
+                      EchoCancellationImpl::SuppressionLevel::kHighSuppression,
                       kStreamHasEchoReference, kOutputReference);
 }
 
diff --git a/modules/audio_processing/echo_cancellation_impl.cc b/modules/audio_processing/echo_cancellation_impl.cc
index 9feaea7..e6fe769 100644
--- a/modules/audio_processing/echo_cancellation_impl.cc
+++ b/modules/audio_processing/echo_cancellation_impl.cc
@@ -21,13 +21,13 @@
 namespace webrtc {
 
 namespace {
-int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
+int16_t MapSetting(EchoCancellationImpl::SuppressionLevel level) {
   switch (level) {
-    case EchoCancellation::kLowSuppression:
+    case EchoCancellationImpl::kLowSuppression:
       return kAecNlpConservative;
-    case EchoCancellation::kModerateSuppression:
+    case EchoCancellationImpl::kModerateSuppression:
       return kAecNlpModerate;
-    case EchoCancellation::kHighSuppression:
+    case EchoCancellationImpl::kHighSuppression:
       return kAecNlpAggressive;
   }
   RTC_NOTREACHED();
@@ -108,12 +108,12 @@
     : crit_render_(crit_render),
       crit_capture_(crit_capture),
       drift_compensation_enabled_(false),
-      metrics_enabled_(false),
+      metrics_enabled_(true),
       suppression_level_(kHighSuppression),
       stream_drift_samples_(0),
       was_stream_drift_set_(false),
       stream_has_echo_(false),
-      delay_logging_enabled_(false),
+      delay_logging_enabled_(true),
       extended_filter_enabled_(false),
       delay_agnostic_enabled_(false),
       enforce_zero_stream_delay_(EnforceZeroStreamDelay()) {
@@ -242,7 +242,7 @@
   return Configure();
 }
 
-EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level()
+EchoCancellationImpl::SuppressionLevel EchoCancellationImpl::suppression_level()
     const {
   rtc::CritScope cs(crit_capture_);
   return suppression_level_;
diff --git a/modules/audio_processing/echo_cancellation_impl.h b/modules/audio_processing/echo_cancellation_impl.h
index 6700249..091a7b5 100644
--- a/modules/audio_processing/echo_cancellation_impl.h
+++ b/modules/audio_processing/echo_cancellation_impl.h
@@ -22,20 +22,106 @@
 
 class AudioBuffer;
 
-class EchoCancellationImpl : public EchoCancellation {
+// The acoustic echo cancellation (AEC) component provides better performance
+// than AECM but also requires more processing power and is dependent on delay
+// stability and reporting accuracy. As such it is well-suited and recommended
+// for PC and IP phone applications.
+class EchoCancellationImpl {
  public:
   EchoCancellationImpl(rtc::CriticalSection* crit_render,
                        rtc::CriticalSection* crit_capture);
-  ~EchoCancellationImpl() override;
+  ~EchoCancellationImpl();
 
   void ProcessRenderAudio(rtc::ArrayView<const float> packed_render_audio);
   int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
 
-  // EchoCancellation implementation.
-  bool is_enabled() const override;
-  int stream_drift_samples() const override;
-  SuppressionLevel suppression_level() const override;
-  bool is_drift_compensation_enabled() const override;
+  int Enable(bool enable);
+  bool is_enabled() const;
+
+  // Differences in clock speed on the primary and reverse streams can impact
+  // the AEC performance. On the client-side, this could be seen when different
+  // render and capture devices are used, particularly with webcams.
+  //
+  // This enables a compensation mechanism, and requires that
+  // set_stream_drift_samples() be called.
+  int enable_drift_compensation(bool enable);
+  bool is_drift_compensation_enabled() const;
+
+  // Sets the difference between the number of samples rendered and captured by
+  // the audio devices since the last call to |ProcessStream()|. Must be called
+  // if drift compensation is enabled, prior to |ProcessStream()|.
+  void set_stream_drift_samples(int drift);
+  int stream_drift_samples() const;
+
+  enum SuppressionLevel {
+    kLowSuppression,
+    kModerateSuppression,
+    kHighSuppression
+  };
+
+  // Sets the aggressiveness of the suppressor. A higher level trades off
+  // double-talk performance for increased echo suppression.
+  int set_suppression_level(SuppressionLevel level);
+  SuppressionLevel suppression_level() const;
+
+  // Returns false if the current frame almost certainly contains no echo
+  // and true if it _might_ contain echo.
+  bool stream_has_echo() const;
+
+  // Enables the computation of various echo metrics. These are obtained
+  // through |GetMetrics()|.
+  int enable_metrics(bool enable);
+  bool are_metrics_enabled() const;
+
+  // Each statistic is reported in dB.
+  // P_far:  Far-end (render) signal power.
+  // P_echo: Near-end (capture) echo signal power.
+  // P_out:  Signal power at the output of the AEC.
+  // P_a:    Internal signal power at the point before the AEC's non-linear
+  //         processor.
+  struct Metrics {
+    // RERL = ERL + ERLE
+    AudioProcessing::Statistic residual_echo_return_loss;
+
+    // ERL = 10log_10(P_far / P_echo)
+    AudioProcessing::Statistic echo_return_loss;
+
+    // ERLE = 10log_10(P_echo / P_out)
+    AudioProcessing::Statistic echo_return_loss_enhancement;
+
+    // (Pre non-linear processing suppression) A_NLP = 10log_10(P_echo / P_a)
+    AudioProcessing::Statistic a_nlp;
+
+    // Fraction of time that the AEC linear filter is divergent, in a 1-second
+    // non-overlapped aggregation window.
+    float divergent_filter_fraction;
+  };
+
+  // Provides various statistics about the AEC.
+  int GetMetrics(Metrics* metrics);
+
+  // Enables computation and logging of delay values. Statistics are obtained
+  // through |GetDelayMetrics()|.
+  int enable_delay_logging(bool enable);
+  bool is_delay_logging_enabled() const;
+
+  // Provides delay metrics.
+  // The delay metrics consists of the delay |median| and the delay standard
+  // deviation |std|. It also consists of the fraction of delay estimates
+  // |fraction_poor_delays| that can make the echo cancellation perform poorly.
+  // The values are aggregated until the first call to |GetDelayMetrics()| and
+  // afterwards aggregated and updated every second.
+  // Note that if there are several clients pulling metrics from
+  // |GetDelayMetrics()| during a session the first call from any of them will
+  // change to one second aggregation window for all.
+  int GetDelayMetrics(int* median, int* std);
+  int GetDelayMetrics(int* median, int* std, float* fraction_poor_delays);
+
+  // Returns a pointer to the low level AEC component.  In case of multiple
+  // channels, the pointer to the first one is returned.  A NULL pointer is
+  // returned when the AEC component is disabled or has not been initialized
+  // successfully.
+  struct AecCore* aec_core() const;
 
   void Initialize(int sample_rate_hz,
                   size_t num_reverse_channels_,
@@ -57,36 +143,10 @@
   static size_t NumCancellersRequired(size_t num_output_channels,
                                       size_t num_reverse_channels);
 
-  // Enable logging of various AEC statistics.
-  int enable_metrics(bool enable) override;
-
-  // Provides various statistics about the AEC.
-  int GetMetrics(Metrics* metrics) override;
-
-  // Enable logging of delay metrics.
-  int enable_delay_logging(bool enable) override;
-
-  // Provides delay metrics.
-  int GetDelayMetrics(int* median,
-                      int* std,
-                      float* fraction_poor_delays) override;
-
  private:
   class Canceller;
   struct StreamProperties;
 
-  // EchoCancellation implementation.
-  int Enable(bool enable) override;
-  int enable_drift_compensation(bool enable) override;
-  void set_stream_drift_samples(int drift) override;
-  int set_suppression_level(SuppressionLevel level) override;
-  bool are_metrics_enabled() const override;
-  bool stream_has_echo() const override;
-  bool is_delay_logging_enabled() const override;
-  int GetDelayMetrics(int* median, int* std) override;
-
-  struct AecCore* aec_core() const override;
-
   void AllocateRenderQueue();
   int Configure();
 
diff --git a/modules/audio_processing/echo_cancellation_impl_unittest.cc b/modules/audio_processing/echo_cancellation_impl_unittest.cc
index ec30abc..c50c52f 100644
--- a/modules/audio_processing/echo_cancellation_impl_unittest.cc
+++ b/modules/audio_processing/echo_cancellation_impl_unittest.cc
@@ -11,69 +11,137 @@
 #include <memory>
 
 #include "modules/audio_processing/aec/aec_core.h"
+#include "modules/audio_processing/echo_cancellation_impl.h"
 #include "modules/audio_processing/include/audio_processing.h"
+#include "rtc_base/criticalsection.h"
 #include "test/gtest.h"
 
 namespace webrtc {
-
 TEST(EchoCancellationInternalTest, ExtendedFilter) {
-  std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
-  EXPECT_TRUE(ap->echo_cancellation()->aec_core() == NULL);
+  rtc::CriticalSection crit_render;
+  rtc::CriticalSection crit_capture;
+  EchoCancellationImpl echo_canceller(&crit_render, &crit_capture);
+  echo_canceller.Initialize(AudioProcessing::kSampleRate32kHz, 2, 2, 2);
 
-  EXPECT_EQ(ap->kNoError, ap->echo_cancellation()->Enable(true));
-  EXPECT_TRUE(ap->echo_cancellation()->is_enabled());
+  EXPECT_TRUE(echo_canceller.aec_core() == nullptr);
 
-  AecCore* aec_core = ap->echo_cancellation()->aec_core();
+  echo_canceller.Enable(true);
+
+  AecCore* aec_core = echo_canceller.aec_core();
   ASSERT_TRUE(aec_core != NULL);
   // Disabled by default.
   EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(aec_core));
 
   Config config;
   config.Set<ExtendedFilter>(new ExtendedFilter(true));
-  ap->SetExtraOptions(config);
+  echo_canceller.SetExtraOptions(config);
   EXPECT_EQ(1, WebRtcAec_extended_filter_enabled(aec_core));
 
   // Retains setting after initialization.
-  EXPECT_EQ(ap->kNoError, ap->Initialize());
+  echo_canceller.Initialize(AudioProcessing::kSampleRate16kHz, 2, 2, 2);
   EXPECT_EQ(1, WebRtcAec_extended_filter_enabled(aec_core));
 
   config.Set<ExtendedFilter>(new ExtendedFilter(false));
-  ap->SetExtraOptions(config);
+  echo_canceller.SetExtraOptions(config);
   EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(aec_core));
 
   // Retains setting after initialization.
-  EXPECT_EQ(ap->kNoError, ap->Initialize());
+  echo_canceller.Initialize(AudioProcessing::kSampleRate16kHz, 1, 1, 1);
   EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(aec_core));
 }
 
 TEST(EchoCancellationInternalTest, DelayAgnostic) {
-  std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
-  EXPECT_TRUE(ap->echo_cancellation()->aec_core() == NULL);
+  rtc::CriticalSection crit_render;
+  rtc::CriticalSection crit_capture;
+  EchoCancellationImpl echo_canceller(&crit_render, &crit_capture);
+  echo_canceller.Initialize(AudioProcessing::kSampleRate32kHz, 1, 1, 1);
 
-  EXPECT_EQ(ap->kNoError, ap->echo_cancellation()->Enable(true));
-  EXPECT_TRUE(ap->echo_cancellation()->is_enabled());
+  EXPECT_TRUE(echo_canceller.aec_core() == NULL);
 
-  AecCore* aec_core = ap->echo_cancellation()->aec_core();
+  EXPECT_EQ(0, echo_canceller.Enable(true));
+  EXPECT_TRUE(echo_canceller.is_enabled());
+
+  AecCore* aec_core = echo_canceller.aec_core();
   ASSERT_TRUE(aec_core != NULL);
   // Enabled by default.
   EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(aec_core));
 
   Config config;
   config.Set<DelayAgnostic>(new DelayAgnostic(true));
-  ap->SetExtraOptions(config);
+  echo_canceller.SetExtraOptions(config);
   EXPECT_EQ(1, WebRtcAec_delay_agnostic_enabled(aec_core));
 
   // Retains setting after initialization.
-  EXPECT_EQ(ap->kNoError, ap->Initialize());
+  echo_canceller.Initialize(AudioProcessing::kSampleRate32kHz, 2, 2, 2);
   EXPECT_EQ(1, WebRtcAec_delay_agnostic_enabled(aec_core));
 
   config.Set<DelayAgnostic>(new DelayAgnostic(false));
-  ap->SetExtraOptions(config);
+  echo_canceller.SetExtraOptions(config);
   EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(aec_core));
 
   // Retains setting after initialization.
-  EXPECT_EQ(ap->kNoError, ap->Initialize());
+  echo_canceller.Initialize(AudioProcessing::kSampleRate16kHz, 2, 2, 2);
   EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(aec_core));
 }
 
+TEST(EchoCancellationInternalTest, InterfaceConfiguration) {
+  rtc::CriticalSection crit_render;
+  rtc::CriticalSection crit_capture;
+  EchoCancellationImpl echo_canceller(&crit_render, &crit_capture);
+  echo_canceller.Initialize(AudioProcessing::kSampleRate16kHz, 1, 1, 1);
+
+  EXPECT_EQ(0, echo_canceller.enable_drift_compensation(true));
+  EXPECT_TRUE(echo_canceller.is_drift_compensation_enabled());
+  EXPECT_EQ(0, echo_canceller.enable_drift_compensation(false));
+  EXPECT_FALSE(echo_canceller.is_drift_compensation_enabled());
+
+  EchoCancellationImpl::SuppressionLevel level[] = {
+      EchoCancellationImpl::kLowSuppression,
+      EchoCancellationImpl::kModerateSuppression,
+      EchoCancellationImpl::kHighSuppression,
+  };
+  for (size_t i = 0; i < arraysize(level); i++) {
+    EXPECT_EQ(0, echo_canceller.set_suppression_level(level[i]));
+    EXPECT_EQ(level[i], echo_canceller.suppression_level());
+  }
+
+  EchoCancellationImpl::Metrics metrics;
+  EXPECT_EQ(AudioProcessing::kNotEnabledError,
+            echo_canceller.GetMetrics(&metrics));
+
+  EXPECT_EQ(0, echo_canceller.Enable(true));
+  EXPECT_TRUE(echo_canceller.is_enabled());
+
+  EXPECT_EQ(0, echo_canceller.enable_metrics(true));
+  EXPECT_TRUE(echo_canceller.are_metrics_enabled());
+  EXPECT_EQ(0, echo_canceller.enable_metrics(false));
+  EXPECT_FALSE(echo_canceller.are_metrics_enabled());
+
+  EXPECT_EQ(0, echo_canceller.enable_delay_logging(true));
+  EXPECT_TRUE(echo_canceller.is_delay_logging_enabled());
+  EXPECT_EQ(0, echo_canceller.enable_delay_logging(false));
+  EXPECT_FALSE(echo_canceller.is_delay_logging_enabled());
+
+  EXPECT_EQ(0, echo_canceller.Enable(false));
+  EXPECT_FALSE(echo_canceller.is_enabled());
+
+  int median = 0;
+  int std = 0;
+  float poor_fraction = 0;
+  EXPECT_EQ(AudioProcessing::kNotEnabledError,
+            echo_canceller.GetDelayMetrics(&median, &std, &poor_fraction));
+
+  EXPECT_EQ(0, echo_canceller.Enable(true));
+  EXPECT_TRUE(echo_canceller.is_enabled());
+  EXPECT_EQ(0, echo_canceller.Enable(false));
+  EXPECT_FALSE(echo_canceller.is_enabled());
+
+  EXPECT_EQ(0, echo_canceller.Enable(true));
+  EXPECT_TRUE(echo_canceller.is_enabled());
+  EXPECT_TRUE(echo_canceller.aec_core() != NULL);
+  EXPECT_EQ(0, echo_canceller.Enable(false));
+  EXPECT_FALSE(echo_canceller.is_enabled());
+  EXPECT_FALSE(echo_canceller.aec_core() != NULL);
+}
+
 }  // namespace webrtc
diff --git a/modules/audio_processing/echo_cancellation_proxy.cc b/modules/audio_processing/echo_cancellation_proxy.cc
deleted file mode 100644
index e8b1fbd..0000000
--- a/modules/audio_processing/echo_cancellation_proxy.cc
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- *  Copyright (c) 2018 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 "modules/audio_processing/echo_cancellation_proxy.h"
-
-namespace webrtc {
-
-EchoCancellationProxy::EchoCancellationProxy(
-    AudioProcessing* audio_processing,
-    EchoCancellation* echo_cancellation)
-    : audio_processing_(audio_processing),
-      echo_cancellation_(echo_cancellation) {}
-
-EchoCancellationProxy::~EchoCancellationProxy() = default;
-
-int EchoCancellationProxy::Enable(bool enable) {
-  // Change the config in APM to mirror the applied settings.
-  // TODO(bugs.webrtc.org/9535): Remove the call to EchoCancellation::Enable
-  // when APM starts taking the config into account.
-  AudioProcessing::Config apm_config = audio_processing_->GetConfig();
-  bool aec2_enabled = apm_config.echo_canceller.enabled &&
-                      !apm_config.echo_canceller.mobile_mode;
-  if ((aec2_enabled && !enable) || (!aec2_enabled && enable)) {
-    apm_config.echo_canceller.enabled = enable;
-    apm_config.echo_canceller.mobile_mode = false;
-    audio_processing_->ApplyConfig(apm_config);
-  }
-  echo_cancellation_->Enable(enable);
-  return AudioProcessing::kNoError;
-}
-
-bool EchoCancellationProxy::is_enabled() const {
-  return echo_cancellation_->is_enabled();
-}
-
-int EchoCancellationProxy::enable_drift_compensation(bool enable) {
-  return echo_cancellation_->enable_drift_compensation(enable);
-}
-
-bool EchoCancellationProxy::is_drift_compensation_enabled() const {
-  return echo_cancellation_->is_drift_compensation_enabled();
-}
-
-void EchoCancellationProxy::set_stream_drift_samples(int drift) {
-  echo_cancellation_->set_stream_drift_samples(drift);
-}
-
-int EchoCancellationProxy::stream_drift_samples() const {
-  return echo_cancellation_->stream_drift_samples();
-}
-
-int EchoCancellationProxy::set_suppression_level(
-    EchoCancellation::SuppressionLevel level) {
-  return echo_cancellation_->set_suppression_level(level);
-}
-
-EchoCancellation::SuppressionLevel EchoCancellationProxy::suppression_level()
-    const {
-  return echo_cancellation_->suppression_level();
-}
-
-bool EchoCancellationProxy::stream_has_echo() const {
-  return echo_cancellation_->stream_has_echo();
-}
-int EchoCancellationProxy::enable_metrics(bool enable) {
-  return echo_cancellation_->enable_metrics(enable);
-}
-bool EchoCancellationProxy::are_metrics_enabled() const {
-  return echo_cancellation_->are_metrics_enabled();
-}
-int EchoCancellationProxy::GetMetrics(Metrics* metrics) {
-  return echo_cancellation_->GetMetrics(metrics);
-}
-int EchoCancellationProxy::enable_delay_logging(bool enable) {
-  return echo_cancellation_->enable_delay_logging(enable);
-}
-bool EchoCancellationProxy::is_delay_logging_enabled() const {
-  return echo_cancellation_->is_delay_logging_enabled();
-}
-int EchoCancellationProxy::GetDelayMetrics(int* median, int* std) {
-  return echo_cancellation_->GetDelayMetrics(median, std);
-}
-int EchoCancellationProxy::GetDelayMetrics(int* median,
-                                           int* std,
-                                           float* fraction_poor_delays) {
-  return echo_cancellation_->GetDelayMetrics(median, std, fraction_poor_delays);
-}
-
-struct AecCore* EchoCancellationProxy::aec_core() const {
-  return echo_cancellation_->aec_core();
-}
-
-}  // namespace webrtc
diff --git a/modules/audio_processing/echo_cancellation_proxy.h b/modules/audio_processing/echo_cancellation_proxy.h
deleted file mode 100644
index f2d3017..0000000
--- a/modules/audio_processing/echo_cancellation_proxy.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- *  Copyright (c) 2018 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 MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_PROXY_H_
-#define MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_PROXY_H_
-
-#include "modules/audio_processing/include/audio_processing.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/scoped_ref_ptr.h"
-
-namespace webrtc {
-
-// Class for temporarily redirecting AEC2 configuration to a new API.
-class EchoCancellationProxy : public EchoCancellation {
- public:
-  EchoCancellationProxy(AudioProcessing* audio_processing,
-                        EchoCancellation* echo_cancellation);
-  ~EchoCancellationProxy() override;
-
-  int Enable(bool enable) override;
-  bool is_enabled() const override;
-  int enable_drift_compensation(bool enable) override;
-  bool is_drift_compensation_enabled() const override;
-  void set_stream_drift_samples(int drift) override;
-  int stream_drift_samples() const override;
-  int set_suppression_level(SuppressionLevel level) override;
-  SuppressionLevel suppression_level() const override;
-  bool stream_has_echo() const override;
-  int enable_metrics(bool enable) override;
-  bool are_metrics_enabled() const override;
-  int GetMetrics(Metrics* metrics) override;
-  int enable_delay_logging(bool enable) override;
-  bool is_delay_logging_enabled() const override;
-  int GetDelayMetrics(int* median, int* std) override;
-  int GetDelayMetrics(int* median,
-                      int* std,
-                      float* fraction_poor_delays) override;
-  struct AecCore* aec_core() const override;
-
- private:
-  AudioProcessing* audio_processing_;
-  EchoCancellation* echo_cancellation_;
-
-  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCancellationProxy);
-};
-}  // namespace webrtc
-
-#endif  // MODULES_AUDIO_PROCESSING_ECHO_CANCELLATION_PROXY_H_
diff --git a/modules/audio_processing/echo_control_mobile_bit_exact_unittest.cc b/modules/audio_processing/echo_control_mobile_bit_exact_unittest.cc
new file mode 100644
index 0000000..8b83692
--- /dev/null
+++ b/modules/audio_processing/echo_control_mobile_bit_exact_unittest.cc
@@ -0,0 +1,224 @@
+/*
+ *  Copyright (c) 2018 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 <vector>
+
+#include "api/array_view.h"
+#include "modules/audio_processing/audio_buffer.h"
+#include "modules/audio_processing/echo_control_mobile_impl.h"
+#include "modules/audio_processing/test/audio_buffer_tools.h"
+#include "modules/audio_processing/test/bitexactness_tools.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace {
+
+// TODO(peah): Increase the number of frames to proces when the issue of
+// non repeatable test results have been found.
+const int kNumFramesToProcess = 200;
+
+void SetupComponent(int sample_rate_hz,
+                    EchoControlMobileImpl::RoutingMode routing_mode,
+                    bool comfort_noise_enabled,
+                    EchoControlMobileImpl* echo_control_mobile) {
+  echo_control_mobile->Initialize(
+      sample_rate_hz > 16000 ? 16000 : sample_rate_hz, 1, 1);
+  echo_control_mobile->Enable(true);
+  echo_control_mobile->set_routing_mode(routing_mode);
+  echo_control_mobile->enable_comfort_noise(comfort_noise_enabled);
+}
+
+void ProcessOneFrame(int sample_rate_hz,
+                     int stream_delay_ms,
+                     AudioBuffer* render_audio_buffer,
+                     AudioBuffer* capture_audio_buffer,
+                     EchoControlMobileImpl* echo_control_mobile) {
+  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
+    render_audio_buffer->SplitIntoFrequencyBands();
+    capture_audio_buffer->SplitIntoFrequencyBands();
+  }
+
+  std::vector<int16_t> render_audio;
+  EchoControlMobileImpl::PackRenderAudioBuffer(
+      render_audio_buffer, 1, render_audio_buffer->num_channels(),
+      &render_audio);
+  echo_control_mobile->ProcessRenderAudio(render_audio);
+
+  echo_control_mobile->ProcessCaptureAudio(capture_audio_buffer,
+                                           stream_delay_ms);
+
+  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
+    capture_audio_buffer->MergeFrequencyBands();
+  }
+}
+
+void RunBitexactnessTest(int sample_rate_hz,
+                         size_t num_channels,
+                         int stream_delay_ms,
+                         EchoControlMobileImpl::RoutingMode routing_mode,
+                         bool comfort_noise_enabled,
+                         const rtc::ArrayView<const float>& output_reference) {
+  rtc::CriticalSection crit_render;
+  rtc::CriticalSection crit_capture;
+  EchoControlMobileImpl echo_control_mobile(&crit_render, &crit_capture);
+  SetupComponent(sample_rate_hz, routing_mode, comfort_noise_enabled,
+                 &echo_control_mobile);
+
+  const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
+  const StreamConfig render_config(sample_rate_hz, num_channels, false);
+  AudioBuffer render_buffer(
+      render_config.num_frames(), render_config.num_channels(),
+      render_config.num_frames(), 1, render_config.num_frames());
+  test::InputAudioFile render_file(
+      test::GetApmRenderTestVectorFileName(sample_rate_hz));
+  std::vector<float> render_input(samples_per_channel * num_channels);
+
+  const StreamConfig capture_config(sample_rate_hz, num_channels, false);
+  AudioBuffer capture_buffer(
+      capture_config.num_frames(), capture_config.num_channels(),
+      capture_config.num_frames(), 1, capture_config.num_frames());
+  test::InputAudioFile capture_file(
+      test::GetApmCaptureTestVectorFileName(sample_rate_hz));
+  std::vector<float> capture_input(samples_per_channel * num_channels);
+
+  for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
+    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
+                                   &render_file, render_input);
+    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
+                                   &capture_file, capture_input);
+
+    test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer);
+    test::CopyVectorToAudioBuffer(capture_config, capture_input,
+                                  &capture_buffer);
+
+    ProcessOneFrame(sample_rate_hz, stream_delay_ms, &render_buffer,
+                    &capture_buffer, &echo_control_mobile);
+  }
+
+  // Extract and verify the test results.
+  std::vector<float> capture_output;
+  test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
+                                     &capture_output);
+
+  // Compare the output with the reference. Only the first values of the output
+  // from last frame processed are compared in order not having to specify all
+  // preceeding frames as testvectors. As the algorithm being tested has a
+  // memory, testing only the last frame implicitly also tests the preceeding
+  // frames.
+  const float kElementErrorBound = 1.0f / 32768.0f;
+  EXPECT_TRUE(test::VerifyDeinterleavedArray(
+      capture_config.num_frames(), capture_config.num_channels(),
+      output_reference, capture_output, kElementErrorBound));
+}
+
+}  // namespace
+
+// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
+// has been solved.
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono8kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.005280f, 0.002380f, -0.000427f};
+
+  RunBitexactnessTest(8000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.003601f, 0.002991f, 0.001923f};
+  RunBitexactnessTest(16000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono32kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.002258f, 0.002899f, 0.003906f};
+
+  RunBitexactnessTest(32000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono48kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {-0.000046f, 0.000041f, 0.000249f};
+
+  RunBitexactnessTest(48000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOff_StreamDelay0) {
+  const float kOutputReference[] = {0.000000f, 0.000000f, 0.000000f};
+
+  RunBitexactnessTest(16000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      false, kOutputReference);
+}
+
+// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
+// has been solved.
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay5) {
+  const float kOutputReference[] = {0.003693f, 0.002930f, 0.001801f};
+
+  RunBitexactnessTest(16000, 1, 5,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay10) {
+  const float kOutputReference[] = {-0.002380f, -0.002533f, -0.002563f};
+
+  RunBitexactnessTest(16000, 1, 10,
+                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
+                      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_QuietEarpieceOrHeadset_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.000397f, 0.000000f, -0.000305f};
+
+  RunBitexactnessTest(
+      16000, 1, 0, EchoControlMobileImpl::RoutingMode::kQuietEarpieceOrHeadset,
+      true, kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_Earpiece_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.002167f, 0.001617f, 0.001038f};
+
+  RunBitexactnessTest(16000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kEarpiece, true,
+                      kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_LoudEarpiece_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.003540f, 0.002899f, 0.001862f};
+
+  RunBitexactnessTest(16000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kLoudEarpiece, true,
+                      kOutputReference);
+}
+
+TEST(EchoControlMobileBitExactnessTest,
+     DISABLED_Mono16kHz_SpeakerPhone_CngOn_StreamDelay0) {
+  const float kOutputReference[] = {0.003632f, 0.003052f, 0.001984f};
+
+  RunBitexactnessTest(16000, 1, 0,
+                      EchoControlMobileImpl::RoutingMode::kSpeakerphone, true,
+                      kOutputReference);
+}
+
+}  // namespace webrtc
diff --git a/modules/audio_processing/echo_control_mobile_impl.cc b/modules/audio_processing/echo_control_mobile_impl.cc
index 272da7a..bd125c6 100644
--- a/modules/audio_processing/echo_control_mobile_impl.cc
+++ b/modules/audio_processing/echo_control_mobile_impl.cc
@@ -20,17 +20,17 @@
 namespace webrtc {
 
 namespace {
-int16_t MapSetting(EchoControlMobile::RoutingMode mode) {
+int16_t MapSetting(EchoControlMobileImpl::RoutingMode mode) {
   switch (mode) {
-    case EchoControlMobile::kQuietEarpieceOrHeadset:
+    case EchoControlMobileImpl::kQuietEarpieceOrHeadset:
       return 0;
-    case EchoControlMobile::kEarpiece:
+    case EchoControlMobileImpl::kEarpiece:
       return 1;
-    case EchoControlMobile::kLoudEarpiece:
+    case EchoControlMobileImpl::kLoudEarpiece:
       return 2;
-    case EchoControlMobile::kSpeakerphone:
+    case EchoControlMobileImpl::kSpeakerphone:
       return 3;
-    case EchoControlMobile::kLoudSpeakerphone:
+    case EchoControlMobileImpl::kLoudSpeakerphone:
       return 4;
   }
   RTC_NOTREACHED();
@@ -55,7 +55,7 @@
 }
 }  // namespace
 
-size_t EchoControlMobile::echo_path_size_bytes() {
+size_t EchoControlMobileImpl::echo_path_size_bytes() {
   return WebRtcAecm_echo_path_size_bytes();
 }
 
@@ -268,7 +268,7 @@
   return Configure();
 }
 
-EchoControlMobile::RoutingMode EchoControlMobileImpl::routing_mode() const {
+EchoControlMobileImpl::RoutingMode EchoControlMobileImpl::routing_mode() const {
   rtc::CritScope cs(crit_capture_);
   return routing_mode_;
 }
diff --git a/modules/audio_processing/echo_control_mobile_impl.h b/modules/audio_processing/echo_control_mobile_impl.h
index a03ab4d..a5b66c8 100644
--- a/modules/audio_processing/echo_control_mobile_impl.h
+++ b/modules/audio_processing/echo_control_mobile_impl.h
@@ -24,21 +24,62 @@
 
 class AudioBuffer;
 
-class EchoControlMobileImpl : public EchoControlMobile {
+// The acoustic echo control for mobile (AECM) component is a low complexity
+// robust option intended for use on mobile devices.
+class EchoControlMobileImpl {
  public:
   EchoControlMobileImpl(rtc::CriticalSection* crit_render,
                         rtc::CriticalSection* crit_capture);
 
-  ~EchoControlMobileImpl() override;
+  ~EchoControlMobileImpl();
+
+  int Enable(bool enable);
+  bool is_enabled() const;
+
+  // Recommended settings for particular audio routes. In general, the louder
+  // the echo is expected to be, the higher this value should be set. The
+  // preferred setting may vary from device to device.
+  enum RoutingMode {
+    kQuietEarpieceOrHeadset,
+    kEarpiece,
+    kLoudEarpiece,
+    kSpeakerphone,
+    kLoudSpeakerphone
+  };
+
+  // Sets echo control appropriate for the audio routing |mode| on the device.
+  // It can and should be updated during a call if the audio routing changes.
+  int set_routing_mode(RoutingMode mode);
+  RoutingMode routing_mode() const;
+
+  // Comfort noise replaces suppressed background noise to maintain a
+  // consistent signal level.
+  int enable_comfort_noise(bool enable);
+  bool is_comfort_noise_enabled() const;
+
+  // A typical use case is to initialize the component with an echo path from a
+  // previous call. The echo path is retrieved using |GetEchoPath()|, typically
+  // at the end of a call. The data can then be stored for later use as an
+  // initializer before the next call, using |SetEchoPath()|.
+  //
+  // Controlling the echo path this way requires the data |size_bytes| to match
+  // the internal echo path size. This size can be acquired using
+  // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
+  // noting if it is to be called during an ongoing call.
+  //
+  // It is possible that version incompatibilities may result in a stored echo
+  // path of the incorrect size. In this case, the stored path should be
+  // discarded.
+  int SetEchoPath(const void* echo_path, size_t size_bytes);
+  int GetEchoPath(void* echo_path, size_t size_bytes) const;
+
+  // The returned path size is guaranteed not to change for the lifetime of
+  // the application.
+  static size_t echo_path_size_bytes();
 
   void ProcessRenderAudio(rtc::ArrayView<const int16_t> packed_render_audio);
   int ProcessCaptureAudio(AudioBuffer* audio, int stream_delay_ms);
 
-  // EchoControlMobile implementation.
-  bool is_enabled() const override;
-  RoutingMode routing_mode() const override;
-  bool is_comfort_noise_enabled() const override;
-
   void Initialize(int sample_rate_hz,
                   size_t num_reverse_channels,
                   size_t num_output_channels);
@@ -55,13 +96,6 @@
   class Canceller;
   struct StreamProperties;
 
-  // EchoControlMobile implementation.
-  int Enable(bool enable) override;
-  int set_routing_mode(RoutingMode mode) override;
-  int enable_comfort_noise(bool enable) override;
-  int SetEchoPath(const void* echo_path, size_t size_bytes) override;
-  int GetEchoPath(void* echo_path, size_t size_bytes) const override;
-
   int Configure();
 
   rtc::CriticalSection* const crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
diff --git a/modules/audio_processing/echo_control_mobile_proxy.cc b/modules/audio_processing/echo_control_mobile_proxy.cc
deleted file mode 100644
index fd29834..0000000
--- a/modules/audio_processing/echo_control_mobile_proxy.cc
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- *  Copyright (c) 2018 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 "modules/audio_processing/echo_control_mobile_proxy.h"
-
-namespace webrtc {
-
-EchoControlMobileProxy::EchoControlMobileProxy(
-    AudioProcessing* audio_processing,
-    EchoControlMobile* echo_control_mobile)
-    : audio_processing_(audio_processing),
-      echo_control_mobile_(echo_control_mobile) {}
-
-EchoControlMobileProxy::~EchoControlMobileProxy() = default;
-
-int EchoControlMobileProxy::Enable(bool enable) {
-  // Change the config in APM to mirror the applied settings.
-  // TODO(bugs.webrtc.org/9535): Remove the call to EchoControlMobile::Enable
-  // when APM starts taking the config into account.
-  AudioProcessing::Config apm_config = audio_processing_->GetConfig();
-  bool aecm_enabled = apm_config.echo_canceller.enabled &&
-                      apm_config.echo_canceller.mobile_mode;
-  if ((aecm_enabled && !enable) || (!aecm_enabled && enable)) {
-    apm_config.echo_canceller.enabled = enable;
-    apm_config.echo_canceller.mobile_mode = true;
-    audio_processing_->ApplyConfig(apm_config);
-  }
-  echo_control_mobile_->Enable(enable);
-  return AudioProcessing::kNoError;
-}
-
-bool EchoControlMobileProxy::is_enabled() const {
-  return echo_control_mobile_->is_enabled();
-}
-
-int EchoControlMobileProxy::set_routing_mode(RoutingMode mode) {
-  return echo_control_mobile_->set_routing_mode(mode);
-}
-
-EchoControlMobile::RoutingMode EchoControlMobileProxy::routing_mode() const {
-  return echo_control_mobile_->routing_mode();
-}
-
-int EchoControlMobileProxy::enable_comfort_noise(bool enable) {
-  return echo_control_mobile_->enable_comfort_noise(enable);
-}
-
-bool EchoControlMobileProxy::is_comfort_noise_enabled() const {
-  return echo_control_mobile_->is_comfort_noise_enabled();
-}
-
-int EchoControlMobileProxy::SetEchoPath(const void* echo_path,
-                                        size_t size_bytes) {
-  return echo_control_mobile_->SetEchoPath(echo_path, size_bytes);
-}
-
-int EchoControlMobileProxy::GetEchoPath(void* echo_path,
-                                        size_t size_bytes) const {
-  return echo_control_mobile_->GetEchoPath(echo_path, size_bytes);
-}
-
-}  // namespace webrtc
diff --git a/modules/audio_processing/echo_control_mobile_proxy.h b/modules/audio_processing/echo_control_mobile_proxy.h
deleted file mode 100644
index eb5908a..0000000
--- a/modules/audio_processing/echo_control_mobile_proxy.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- *  Copyright (c) 2018 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 MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_PROXY_H_
-#define MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_PROXY_H_
-
-#include "modules/audio_processing/include/audio_processing.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/scoped_ref_ptr.h"
-
-namespace webrtc {
-
-// Class for temporarily redirecting AECM configuration to a new API.
-class EchoControlMobileProxy : public EchoControlMobile {
- public:
-  EchoControlMobileProxy(AudioProcessing* audio_processing,
-                         EchoControlMobile* echo_control_mobile);
-  ~EchoControlMobileProxy() override;
-
-  bool is_enabled() const override;
-  RoutingMode routing_mode() const override;
-  bool is_comfort_noise_enabled() const override;
-  int Enable(bool enable) override;
-  int set_routing_mode(RoutingMode mode) override;
-  int enable_comfort_noise(bool enable) override;
-  int SetEchoPath(const void* echo_path, size_t size_bytes) override;
-  int GetEchoPath(void* echo_path, size_t size_bytes) const override;
-
- private:
-  AudioProcessing* audio_processing_;
-  EchoControlMobile* echo_control_mobile_;
-
-  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoControlMobileProxy);
-};
-}  // namespace webrtc
-
-#endif  // MODULES_AUDIO_PROCESSING_ECHO_CONTROL_MOBILE_PROXY_H_
diff --git a/modules/audio_processing/echo_control_mobile_unittest.cc b/modules/audio_processing/echo_control_mobile_unittest.cc
index fb58a5b..d7e470c 100644
--- a/modules/audio_processing/echo_control_mobile_unittest.cc
+++ b/modules/audio_processing/echo_control_mobile_unittest.cc
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
+ *  Copyright (c) 2018 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
@@ -7,218 +7,70 @@
  *  in the file PATENTS.  All contributing project authors may
  *  be found in the AUTHORS file in the root of the source tree.
  */
+
+#include <array>
 #include <vector>
 
-#include "api/array_view.h"
-#include "modules/audio_processing/audio_buffer.h"
 #include "modules/audio_processing/echo_control_mobile_impl.h"
-#include "modules/audio_processing/test/audio_buffer_tools.h"
-#include "modules/audio_processing/test/bitexactness_tools.h"
+#include "modules/audio_processing/include/audio_processing.h"
+#include "rtc_base/criticalsection.h"
 #include "test/gtest.h"
 
 namespace webrtc {
-namespace {
-
-// TODO(peah): Increase the number of frames to proces when the issue of
-// non repeatable test results have been found.
-const int kNumFramesToProcess = 200;
-
-void SetupComponent(int sample_rate_hz,
-                    EchoControlMobile::RoutingMode routing_mode,
-                    bool comfort_noise_enabled,
-                    EchoControlMobileImpl* echo_control_mobile) {
-  echo_control_mobile->Initialize(
-      sample_rate_hz > 16000 ? 16000 : sample_rate_hz, 1, 1);
-  EchoControlMobile* ec = static_cast<EchoControlMobile*>(echo_control_mobile);
-  ec->Enable(true);
-  ec->set_routing_mode(routing_mode);
-  ec->enable_comfort_noise(comfort_noise_enabled);
-}
-
-void ProcessOneFrame(int sample_rate_hz,
-                     int stream_delay_ms,
-                     AudioBuffer* render_audio_buffer,
-                     AudioBuffer* capture_audio_buffer,
-                     EchoControlMobileImpl* echo_control_mobile) {
-  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
-    render_audio_buffer->SplitIntoFrequencyBands();
-    capture_audio_buffer->SplitIntoFrequencyBands();
-  }
-
-  std::vector<int16_t> render_audio;
-  EchoControlMobileImpl::PackRenderAudioBuffer(
-      render_audio_buffer, 1, render_audio_buffer->num_channels(),
-      &render_audio);
-  echo_control_mobile->ProcessRenderAudio(render_audio);
-
-  echo_control_mobile->ProcessCaptureAudio(capture_audio_buffer,
-                                           stream_delay_ms);
-
-  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
-    capture_audio_buffer->MergeFrequencyBands();
-  }
-}
-
-void RunBitexactnessTest(int sample_rate_hz,
-                         size_t num_channels,
-                         int stream_delay_ms,
-                         EchoControlMobile::RoutingMode routing_mode,
-                         bool comfort_noise_enabled,
-                         const rtc::ArrayView<const float>& output_reference) {
+TEST(EchoControlMobileTest, InterfaceConfiguration) {
   rtc::CriticalSection crit_render;
   rtc::CriticalSection crit_capture;
-  EchoControlMobileImpl echo_control_mobile(&crit_render, &crit_capture);
-  SetupComponent(sample_rate_hz, routing_mode, comfort_noise_enabled,
-                 &echo_control_mobile);
+  EchoControlMobileImpl aecm(&crit_render, &crit_capture);
+  aecm.Initialize(AudioProcessing::kSampleRate16kHz, 2, 2);
 
-  const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
-  const StreamConfig render_config(sample_rate_hz, num_channels, false);
-  AudioBuffer render_buffer(
-      render_config.num_frames(), render_config.num_channels(),
-      render_config.num_frames(), 1, render_config.num_frames());
-  test::InputAudioFile render_file(
-      test::GetApmRenderTestVectorFileName(sample_rate_hz));
-  std::vector<float> render_input(samples_per_channel * num_channels);
+  // Turn AECM on
+  EXPECT_EQ(0, aecm.Enable(true));
+  EXPECT_TRUE(aecm.is_enabled());
 
-  const StreamConfig capture_config(sample_rate_hz, num_channels, false);
-  AudioBuffer capture_buffer(
-      capture_config.num_frames(), capture_config.num_channels(),
-      capture_config.num_frames(), 1, capture_config.num_frames());
-  test::InputAudioFile capture_file(
-      test::GetApmCaptureTestVectorFileName(sample_rate_hz));
-  std::vector<float> capture_input(samples_per_channel * num_channels);
-
-  for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
-    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
-                                   &render_file, render_input);
-    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
-                                   &capture_file, capture_input);
-
-    test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer);
-    test::CopyVectorToAudioBuffer(capture_config, capture_input,
-                                  &capture_buffer);
-
-    ProcessOneFrame(sample_rate_hz, stream_delay_ms, &render_buffer,
-                    &capture_buffer, &echo_control_mobile);
+  // Toggle routing modes
+  std::array<EchoControlMobileImpl::RoutingMode, 5> routing_modes = {
+      EchoControlMobileImpl::kQuietEarpieceOrHeadset,
+      EchoControlMobileImpl::kEarpiece,
+      EchoControlMobileImpl::kLoudEarpiece,
+      EchoControlMobileImpl::kSpeakerphone,
+      EchoControlMobileImpl::kLoudSpeakerphone,
+  };
+  for (auto mode : routing_modes) {
+    EXPECT_EQ(0, aecm.set_routing_mode(mode));
+    EXPECT_EQ(mode, aecm.routing_mode());
   }
 
-  // Extract and verify the test results.
-  std::vector<float> capture_output;
-  test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
-                                     &capture_output);
+  // Turn comfort noise off/on
+  EXPECT_EQ(0, aecm.enable_comfort_noise(false));
+  EXPECT_FALSE(aecm.is_comfort_noise_enabled());
+  EXPECT_EQ(0, aecm.enable_comfort_noise(true));
+  EXPECT_TRUE(aecm.is_comfort_noise_enabled());
 
-  // Compare the output with the reference. Only the first values of the output
-  // from last frame processed are compared in order not having to specify all
-  // preceeding frames as testvectors. As the algorithm being tested has a
-  // memory, testing only the last frame implicitly also tests the preceeding
-  // frames.
-  const float kElementErrorBound = 1.0f / 32768.0f;
-  EXPECT_TRUE(test::VerifyDeinterleavedArray(
-      capture_config.num_frames(), capture_config.num_channels(),
-      output_reference, capture_output, kElementErrorBound));
-}
+  // Set and get echo path
+  const size_t echo_path_size = aecm.echo_path_size_bytes();
+  std::vector<uint8_t> echo_path_in(echo_path_size);
+  std::vector<uint8_t> echo_path_out(echo_path_size);
+  EXPECT_EQ(AudioProcessing::kNullPointerError,
+            aecm.SetEchoPath(nullptr, echo_path_size));
+  EXPECT_EQ(AudioProcessing::kNullPointerError,
+            aecm.GetEchoPath(nullptr, echo_path_size));
+  EXPECT_EQ(AudioProcessing::kBadParameterError,
+            aecm.GetEchoPath(echo_path_out.data(), 1));
+  EXPECT_EQ(0, aecm.GetEchoPath(echo_path_out.data(), echo_path_size));
+  for (size_t i = 0; i < echo_path_size; i++) {
+    echo_path_in[i] = echo_path_out[i] + 1;
+  }
+  EXPECT_EQ(AudioProcessing::kBadParameterError,
+            aecm.SetEchoPath(echo_path_in.data(), 1));
+  EXPECT_EQ(0, aecm.SetEchoPath(echo_path_in.data(), echo_path_size));
+  EXPECT_EQ(0, aecm.GetEchoPath(echo_path_out.data(), echo_path_size));
+  for (size_t i = 0; i < echo_path_size; i++) {
+    EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
+  }
 
-}  // namespace
-
-// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
-// has been solved.
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono8kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.005280f, 0.002380f, -0.000427f};
-
-  RunBitexactnessTest(8000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.003601f, 0.002991f, 0.001923f};
-  RunBitexactnessTest(16000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono32kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.002258f, 0.002899f, 0.003906f};
-
-  RunBitexactnessTest(32000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono48kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {-0.000046f, 0.000041f, 0.000249f};
-
-  RunBitexactnessTest(48000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOff_StreamDelay0) {
-  const float kOutputReference[] = {0.000000f, 0.000000f, 0.000000f};
-
-  RunBitexactnessTest(16000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, false,
-                      kOutputReference);
-}
-
-// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
-// has been solved.
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay5) {
-  const float kOutputReference[] = {0.003693f, 0.002930f, 0.001801f};
-
-  RunBitexactnessTest(16000, 1, 5,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay10) {
-  const float kOutputReference[] = {-0.002380f, -0.002533f, -0.002563f};
-
-  RunBitexactnessTest(16000, 1, 10,
-                      EchoControlMobile::RoutingMode::kLoudSpeakerphone, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_QuietEarpieceOrHeadset_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.000397f, 0.000000f, -0.000305f};
-
-  RunBitexactnessTest(16000, 1, 0,
-                      EchoControlMobile::RoutingMode::kQuietEarpieceOrHeadset,
-                      true, kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_Earpiece_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.002167f, 0.001617f, 0.001038f};
-
-  RunBitexactnessTest(16000, 1, 0, EchoControlMobile::RoutingMode::kEarpiece,
-                      true, kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_LoudEarpiece_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.003540f, 0.002899f, 0.001862f};
-
-  RunBitexactnessTest(16000, 1, 0,
-                      EchoControlMobile::RoutingMode::kLoudEarpiece, true,
-                      kOutputReference);
-}
-
-TEST(EchoControlMobileBitExactnessTest,
-     DISABLED_Mono16kHz_SpeakerPhone_CngOn_StreamDelay0) {
-  const float kOutputReference[] = {0.003632f, 0.003052f, 0.001984f};
-
-  RunBitexactnessTest(16000, 1, 0,
-                      EchoControlMobile::RoutingMode::kSpeakerphone, true,
-                      kOutputReference);
+  // Turn AECM off
+  EXPECT_EQ(0, aecm.Enable(false));
+  EXPECT_FALSE(aecm.is_enabled());
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/gain_controller2.cc b/modules/audio_processing/gain_controller2.cc
index fd4b411..29af962 100644
--- a/modules/audio_processing/gain_controller2.cc
+++ b/modules/audio_processing/gain_controller2.cc
@@ -15,6 +15,7 @@
 #include "modules/audio_processing/logging/apm_data_dumper.h"
 #include "rtc_base/atomicops.h"
 #include "rtc_base/checks.h"
+#include "rtc_base/strings/string_builder.h"
 
 namespace webrtc {
 
@@ -42,7 +43,7 @@
   AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(),
                                     audio->num_frames());
   if (adaptive_digital_mode_) {
-    adaptive_agc_.Process(float_frame);
+    adaptive_agc_.Process(float_frame, fixed_gain_controller_.LastAudioLevel());
   }
   fixed_gain_controller_.Process(float_frame);
 }
@@ -69,10 +70,10 @@
 
 std::string GainController2::ToString(
     const AudioProcessing::Config::GainController2& config) {
-  std::stringstream ss;
+  rtc::StringBuilder ss;
   ss << "{enabled: " << (config.enabled ? "true" : "false") << ", "
      << "fixed_gain_dB: " << config.fixed_gain_db << "}";
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace webrtc
diff --git a/modules/audio_processing/include/aec_dump.h b/modules/audio_processing/include/aec_dump.h
index e32fa67..313e9d7 100644
--- a/modules/audio_processing/include/aec_dump.h
+++ b/modules/audio_processing/include/aec_dump.h
@@ -98,6 +98,9 @@
   virtual void WriteRenderStreamMessage(
       const AudioFrameView<const float>& src) = 0;
 
+  virtual void WriteRuntimeSetting(
+      const AudioProcessing::RuntimeSetting& runtime_setting) = 0;
+
   // Logs Event::Type CONFIG message.
   virtual void WriteConfig(const InternalAPMConfig& config) = 0;
 };
diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h
index f05d7b6..8af0cb2 100644
--- a/modules/audio_processing/include/audio_processing.h
+++ b/modules/audio_processing/include/audio_processing.h
@@ -34,6 +34,7 @@
 #include "rtc_base/platform_file.h"
 #include "rtc_base/refcount.h"
 #include "rtc_base/scoped_ref_ptr.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace webrtc {
 
@@ -46,8 +47,6 @@
 class StreamConfig;
 class ProcessingConfig;
 
-class EchoCancellation;
-class EchoControlMobile;
 class EchoDetector;
 class GainControl;
 class HighPassFilter;
@@ -82,9 +81,8 @@
 };
 
 // Enables the refined linear filter adaptation in the echo canceller.
-// This configuration only applies to EchoCancellation and not
-// EchoControlMobile. It can be set in the constructor
-// or using AudioProcessing::SetExtraOptions().
+// This configuration only applies to non-mobile echo cancellation.
+// It can be set in the constructor or using AudioProcessing::SetExtraOptions().
 struct RefinedAdaptiveFilter {
   RefinedAdaptiveFilter() : enabled(false) {}
   explicit RefinedAdaptiveFilter(bool enabled) : enabled(enabled) {}
@@ -95,9 +93,9 @@
 
 // Enables delay-agnostic echo cancellation. This feature relies on internally
 // estimated delays between the process and reverse streams, thus not relying
-// on reported system delays. This configuration only applies to
-// EchoCancellation and not EchoControlMobile. It can be set in the constructor
-// or using AudioProcessing::SetExtraOptions().
+// on reported system delays. This configuration only applies to non-mobile echo
+// cancellation. It can be set in the constructor or using
+// AudioProcessing::SetExtraOptions().
 struct DelayAgnostic {
   DelayAgnostic() : enabled(false) {}
   explicit DelayAgnostic(bool enabled) : enabled(enabled) {}
@@ -122,10 +120,12 @@
   explicit ExperimentalAgc(bool enabled) : enabled(enabled) {}
   ExperimentalAgc(bool enabled,
                   bool enabled_agc2_level_estimator,
-                  bool digital_adaptive_disabled)
+                  bool digital_adaptive_disabled,
+                  bool analyze_before_aec)
       : enabled(enabled),
         enabled_agc2_level_estimator(enabled_agc2_level_estimator),
-        digital_adaptive_disabled(digital_adaptive_disabled) {}
+        digital_adaptive_disabled(digital_adaptive_disabled),
+        analyze_before_aec(analyze_before_aec) {}
 
   ExperimentalAgc(bool enabled, int startup_min_volume)
       : enabled(enabled), startup_min_volume(startup_min_volume) {}
@@ -140,6 +140,9 @@
   int clipped_level_min = kClippedLevelMin;
   bool enabled_agc2_level_estimator = false;
   bool digital_adaptive_disabled = false;
+  // 'analyze_before_aec' is an experimental flag. It is intended to be removed
+  // at some point.
+  bool analyze_before_aec = false;
 };
 
 // Use to enable experimental noise suppression. It can be set in the
@@ -188,13 +191,12 @@
 // AudioProcessing* apm = AudioProcessingBuilder().Create();
 //
 // AudioProcessing::Config config;
+// config.echo_canceller.enabled = true;
+// config.echo_canceller.mobile_mode = false;
 // config.high_pass_filter.enabled = true;
 // config.gain_controller2.enabled = true;
 // apm->ApplyConfig(config)
 //
-// apm->echo_cancellation()->enable_drift_compensation(false);
-// apm->echo_cancellation()->Enable(true);
-//
 // apm->noise_reduction()->set_level(kHighSuppression);
 // apm->noise_reduction()->Enable(true);
 //
@@ -550,7 +552,7 @@
     float minimum_ = 0.0f;  // Long-term minimum.
   };
 
-  struct AudioProcessingStatistics {
+  struct RTC_EXPORT AudioProcessingStatistics {
     AudioProcessingStatistics();
     AudioProcessingStatistics(const AudioProcessingStatistics& other);
     ~AudioProcessingStatistics();
@@ -595,8 +597,6 @@
   // These provide access to the component interfaces and should never return
   // NULL. The pointers will be valid for the lifetime of the APM instance.
   // The memory for these objects is entirely managed internally.
-  virtual EchoCancellation* echo_cancellation() const = 0;
-  virtual EchoControlMobile* echo_control_mobile() const = 0;
   virtual GainControl* gain_control() const = 0;
   // TODO(peah): Deprecate this API call.
   virtual HighPassFilter* high_pass_filter() const = 0;
@@ -649,7 +649,7 @@
   static const int kChunkSizeMs = 10;
 };
 
-class AudioProcessingBuilder {
+class RTC_EXPORT AudioProcessingBuilder {
  public:
   AudioProcessingBuilder();
   ~AudioProcessingBuilder();
@@ -789,168 +789,6 @@
   StreamConfig streams[StreamName::kNumStreamNames];
 };
 
-// The acoustic echo cancellation (AEC) component provides better performance
-// than AECM but also requires more processing power and is dependent on delay
-// stability and reporting accuracy. As such it is well-suited and recommended
-// for PC and IP phone applications.
-//
-// Not recommended to be enabled on the server-side.
-class EchoCancellation {
- public:
-  // EchoCancellation and EchoControlMobile may not be enabled simultaneously.
-  // Enabling one will disable the other.
-  virtual int Enable(bool enable) = 0;
-  virtual bool is_enabled() const = 0;
-
-  // Differences in clock speed on the primary and reverse streams can impact
-  // the AEC performance. On the client-side, this could be seen when different
-  // render and capture devices are used, particularly with webcams.
-  //
-  // This enables a compensation mechanism, and requires that
-  // set_stream_drift_samples() be called.
-  virtual int enable_drift_compensation(bool enable) = 0;
-  virtual bool is_drift_compensation_enabled() const = 0;
-
-  // Sets the difference between the number of samples rendered and captured by
-  // the audio devices since the last call to |ProcessStream()|. Must be called
-  // if drift compensation is enabled, prior to |ProcessStream()|.
-  virtual void set_stream_drift_samples(int drift) = 0;
-  virtual int stream_drift_samples() const = 0;
-
-  enum SuppressionLevel {
-    kLowSuppression,
-    kModerateSuppression,
-    kHighSuppression
-  };
-
-  // Sets the aggressiveness of the suppressor. A higher level trades off
-  // double-talk performance for increased echo suppression.
-  virtual int set_suppression_level(SuppressionLevel level) = 0;
-  virtual SuppressionLevel suppression_level() const = 0;
-
-  // Returns false if the current frame almost certainly contains no echo
-  // and true if it _might_ contain echo.
-  virtual bool stream_has_echo() const = 0;
-
-  // Enables the computation of various echo metrics. These are obtained
-  // through |GetMetrics()|.
-  virtual int enable_metrics(bool enable) = 0;
-  virtual bool are_metrics_enabled() const = 0;
-
-  // Each statistic is reported in dB.
-  // P_far:  Far-end (render) signal power.
-  // P_echo: Near-end (capture) echo signal power.
-  // P_out:  Signal power at the output of the AEC.
-  // P_a:    Internal signal power at the point before the AEC's non-linear
-  //         processor.
-  struct Metrics {
-    // RERL = ERL + ERLE
-    AudioProcessing::Statistic residual_echo_return_loss;
-
-    // ERL = 10log_10(P_far / P_echo)
-    AudioProcessing::Statistic echo_return_loss;
-
-    // ERLE = 10log_10(P_echo / P_out)
-    AudioProcessing::Statistic echo_return_loss_enhancement;
-
-    // (Pre non-linear processing suppression) A_NLP = 10log_10(P_echo / P_a)
-    AudioProcessing::Statistic a_nlp;
-
-    // Fraction of time that the AEC linear filter is divergent, in a 1-second
-    // non-overlapped aggregation window.
-    float divergent_filter_fraction;
-  };
-
-  // Deprecated. Use GetStatistics on the AudioProcessing interface instead.
-  // TODO(ajm): discuss the metrics update period.
-  virtual int GetMetrics(Metrics* metrics) = 0;
-
-  // Enables computation and logging of delay values. Statistics are obtained
-  // through |GetDelayMetrics()|.
-  virtual int enable_delay_logging(bool enable) = 0;
-  virtual bool is_delay_logging_enabled() const = 0;
-
-  // The delay metrics consists of the delay |median| and the delay standard
-  // deviation |std|. It also consists of the fraction of delay estimates
-  // |fraction_poor_delays| that can make the echo cancellation perform poorly.
-  // The values are aggregated until the first call to |GetDelayMetrics()| and
-  // afterwards aggregated and updated every second.
-  // Note that if there are several clients pulling metrics from
-  // |GetDelayMetrics()| during a session the first call from any of them will
-  // change to one second aggregation window for all.
-  // Deprecated. Use GetStatistics on the AudioProcessing interface instead.
-  virtual int GetDelayMetrics(int* median, int* std) = 0;
-  // Deprecated. Use GetStatistics on the AudioProcessing interface instead.
-  virtual int GetDelayMetrics(int* median,
-                              int* std,
-                              float* fraction_poor_delays) = 0;
-
-  // Returns a pointer to the low level AEC component.  In case of multiple
-  // channels, the pointer to the first one is returned.  A NULL pointer is
-  // returned when the AEC component is disabled or has not been initialized
-  // successfully.
-  virtual struct AecCore* aec_core() const = 0;
-
- protected:
-  virtual ~EchoCancellation() {}
-};
-
-// The acoustic echo control for mobile (AECM) component is a low complexity
-// robust option intended for use on mobile devices.
-//
-// Not recommended to be enabled on the server-side.
-class EchoControlMobile {
- public:
-  // EchoCancellation and EchoControlMobile may not be enabled simultaneously.
-  // Enabling one will disable the other.
-  virtual int Enable(bool enable) = 0;
-  virtual bool is_enabled() const = 0;
-
-  // Recommended settings for particular audio routes. In general, the louder
-  // the echo is expected to be, the higher this value should be set. The
-  // preferred setting may vary from device to device.
-  enum RoutingMode {
-    kQuietEarpieceOrHeadset,
-    kEarpiece,
-    kLoudEarpiece,
-    kSpeakerphone,
-    kLoudSpeakerphone
-  };
-
-  // Sets echo control appropriate for the audio routing |mode| on the device.
-  // It can and should be updated during a call if the audio routing changes.
-  virtual int set_routing_mode(RoutingMode mode) = 0;
-  virtual RoutingMode routing_mode() const = 0;
-
-  // Comfort noise replaces suppressed background noise to maintain a
-  // consistent signal level.
-  virtual int enable_comfort_noise(bool enable) = 0;
-  virtual bool is_comfort_noise_enabled() const = 0;
-
-  // A typical use case is to initialize the component with an echo path from a
-  // previous call. The echo path is retrieved using |GetEchoPath()|, typically
-  // at the end of a call. The data can then be stored for later use as an
-  // initializer before the next call, using |SetEchoPath()|.
-  //
-  // Controlling the echo path this way requires the data |size_bytes| to match
-  // the internal echo path size. This size can be acquired using
-  // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
-  // noting if it is to be called during an ongoing call.
-  //
-  // It is possible that version incompatibilities may result in a stored echo
-  // path of the incorrect size. In this case, the stored path should be
-  // discarded.
-  virtual int SetEchoPath(const void* echo_path, size_t size_bytes) = 0;
-  virtual int GetEchoPath(void* echo_path, size_t size_bytes) const = 0;
-
-  // The returned path size is guaranteed not to change for the lifetime of
-  // the application.
-  static size_t echo_path_size_bytes();
-
- protected:
-  virtual ~EchoControlMobile() {}
-};
-
 // TODO(peah): Remove this interface.
 // A filtering component which removes DC offset and low-frequency noise.
 // Recommended to be enabled on the client-side.
diff --git a/modules/audio_processing/include/audio_processing_statistics.h b/modules/audio_processing/include/audio_processing_statistics.h
index 237d23c..2318bad 100644
--- a/modules/audio_processing/include/audio_processing_statistics.h
+++ b/modules/audio_processing/include/audio_processing_statistics.h
@@ -12,11 +12,12 @@
 #define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_STATISTICS_H_
 
 #include "absl/types/optional.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace webrtc {
 // This version of the stats uses Optionals, it will replace the regular
 // AudioProcessingStatistics struct.
-struct AudioProcessingStats {
+struct RTC_EXPORT AudioProcessingStats {
   AudioProcessingStats();
   AudioProcessingStats(const AudioProcessingStats& other);
   ~AudioProcessingStats();
diff --git a/modules/audio_processing/include/config.h b/modules/audio_processing/include/config.h
index 398aab6..e77d3fd 100644
--- a/modules/audio_processing/include/config.h
+++ b/modules/audio_processing/include/config.h
@@ -14,6 +14,7 @@
 #include <map>
 
 #include "rtc_base/constructormagic.h"
+#include "rtc_base/system/rtc_export.h"
 
 namespace webrtc {
 
@@ -57,7 +58,7 @@
 //    config.Set<Algo1_CostFunction>(new SqrCost());
 //
 // Note: This class is thread-compatible (like STL containers).
-class Config {
+class RTC_EXPORT Config {
  public:
   // Returns the option if set or a default constructed one.
   // Callers that access options too often are encouraged to cache the result.
diff --git a/modules/audio_processing/include/mock_audio_processing.h b/modules/audio_processing/include/mock_audio_processing.h
index 2864e48..4a1fe62 100644
--- a/modules/audio_processing/include/mock_audio_processing.h
+++ b/modules/audio_processing/include/mock_audio_processing.h
@@ -21,43 +21,6 @@
 namespace webrtc {
 
 namespace test {
-
-class MockEchoCancellation : public EchoCancellation {
- public:
-  virtual ~MockEchoCancellation() {}
-  MOCK_METHOD1(Enable, int(bool enable));
-  MOCK_CONST_METHOD0(is_enabled, bool());
-  MOCK_METHOD1(enable_drift_compensation, int(bool enable));
-  MOCK_CONST_METHOD0(is_drift_compensation_enabled, bool());
-  MOCK_METHOD1(set_stream_drift_samples, void(int drift));
-  MOCK_CONST_METHOD0(stream_drift_samples, int());
-  MOCK_METHOD1(set_suppression_level, int(SuppressionLevel level));
-  MOCK_CONST_METHOD0(suppression_level, SuppressionLevel());
-  MOCK_CONST_METHOD0(stream_has_echo, bool());
-  MOCK_METHOD1(enable_metrics, int(bool enable));
-  MOCK_CONST_METHOD0(are_metrics_enabled, bool());
-  MOCK_METHOD1(GetMetrics, int(Metrics* metrics));
-  MOCK_METHOD1(enable_delay_logging, int(bool enable));
-  MOCK_CONST_METHOD0(is_delay_logging_enabled, bool());
-  MOCK_METHOD2(GetDelayMetrics, int(int* median, int* std));
-  MOCK_METHOD3(GetDelayMetrics,
-               int(int* median, int* std, float* fraction_poor_delays));
-  MOCK_CONST_METHOD0(aec_core, struct AecCore*());
-};
-
-class MockEchoControlMobile : public EchoControlMobile {
- public:
-  virtual ~MockEchoControlMobile() {}
-  MOCK_METHOD1(Enable, int(bool enable));
-  MOCK_CONST_METHOD0(is_enabled, bool());
-  MOCK_METHOD1(set_routing_mode, int(RoutingMode mode));
-  MOCK_CONST_METHOD0(routing_mode, RoutingMode());
-  MOCK_METHOD1(enable_comfort_noise, int(bool enable));
-  MOCK_CONST_METHOD0(is_comfort_noise_enabled, bool());
-  MOCK_METHOD2(SetEchoPath, int(const void* echo_path, size_t size_bytes));
-  MOCK_CONST_METHOD2(GetEchoPath, int(void* echo_path, size_t size_bytes));
-};
-
 class MockGainControl : public GainControl {
  public:
   virtual ~MockGainControl() {}
@@ -150,9 +113,7 @@
 class MockAudioProcessing : public testing::NiceMock<AudioProcessing> {
  public:
   MockAudioProcessing()
-      : echo_cancellation_(new testing::NiceMock<MockEchoCancellation>()),
-        echo_control_mobile_(new testing::NiceMock<MockEchoControlMobile>()),
-        gain_control_(new testing::NiceMock<MockGainControl>()),
+      : gain_control_(new testing::NiceMock<MockGainControl>()),
         high_pass_filter_(new testing::NiceMock<MockHighPassFilter>()),
         level_estimator_(new testing::NiceMock<MockLevelEstimator>()),
         noise_suppression_(new testing::NiceMock<MockNoiseSuppression>()),
@@ -221,12 +182,6 @@
   MOCK_METHOD0(UpdateHistogramsOnCallEnd, void());
   MOCK_CONST_METHOD0(GetStatistics, AudioProcessingStatistics());
   MOCK_CONST_METHOD1(GetStatistics, AudioProcessingStats(bool));
-  virtual MockEchoCancellation* echo_cancellation() const {
-    return echo_cancellation_.get();
-  }
-  virtual MockEchoControlMobile* echo_control_mobile() const {
-    return echo_control_mobile_.get();
-  }
   virtual MockGainControl* gain_control() const { return gain_control_.get(); }
   virtual MockHighPassFilter* high_pass_filter() const {
     return high_pass_filter_.get();
@@ -244,8 +199,6 @@
   MOCK_CONST_METHOD0(GetConfig, AudioProcessing::Config());
 
  private:
-  std::unique_ptr<MockEchoCancellation> echo_cancellation_;
-  std::unique_ptr<MockEchoControlMobile> echo_control_mobile_;
   std::unique_ptr<MockGainControl> gain_control_;
   std::unique_ptr<MockHighPassFilter> high_pass_filter_;
   std::unique_ptr<MockLevelEstimator> level_estimator_;
diff --git a/modules/audio_processing/module.mk b/modules/audio_processing/module.mk
index 7d8b360..e7beb84 100644
--- a/modules/audio_processing/module.mk
+++ b/modules/audio_processing/module.mk
@@ -14,9 +14,7 @@
 	modules/audio_processing/audio_buffer.o \
 	modules/audio_processing/audio_processing_impl.o \
 	modules/audio_processing/echo_cancellation_impl.o \
-	modules/audio_processing/echo_cancellation_proxy.o \
 	modules/audio_processing/echo_control_mobile_impl.o \
-	modules/audio_processing/echo_control_mobile_proxy.o \
 	modules/audio_processing/echo_detector/circular_buffer.o \
 	modules/audio_processing/echo_detector/mean_variance_estimator.o \
 	modules/audio_processing/echo_detector/moving_max.o \
@@ -95,6 +93,7 @@
 	modules/audio_processing/aec3/block_delay_buffer.o \
 	modules/audio_processing/aec3/block_framer.o \
 	modules/audio_processing/aec3/block_processor.o \
+	modules/audio_processing/aec3/block_processor2.o \
 	modules/audio_processing/aec3/block_processor_metrics.o \
 	modules/audio_processing/aec3/cascaded_biquad_filter.o \
 	modules/audio_processing/aec3/comfort_noise_generator.o \
@@ -111,6 +110,7 @@
 	modules/audio_processing/aec3/fft_buffer.o \
 	modules/audio_processing/aec3/filter_analyzer.o \
 	modules/audio_processing/aec3/frame_blocker.o \
+	modules/audio_processing/aec3/fullband_erle_estimator.o \
 	modules/audio_processing/aec3/main_filter_update_gain.o \
 	modules/audio_processing/aec3/matched_filter.o \
 	modules/audio_processing/aec3/matched_filter_lag_aggregator.o \
@@ -118,7 +118,9 @@
 	modules/audio_processing/aec3/moving_average.o \
 	modules/audio_processing/aec3/render_buffer.o \
 	modules/audio_processing/aec3/render_delay_buffer.o \
+	modules/audio_processing/aec3/render_delay_buffer2.o \
 	modules/audio_processing/aec3/render_delay_controller.o \
+	modules/audio_processing/aec3/render_delay_controller2.o \
 	modules/audio_processing/aec3/render_delay_controller_metrics.o \
 	modules/audio_processing/aec3/render_signal_analyzer.o \
 	modules/audio_processing/aec3/residual_echo_estimator.o \
@@ -130,6 +132,7 @@
 	modules/audio_processing/aec3/shadow_filter_update_gain.o \
 	modules/audio_processing/aec3/skew_estimator.o \
 	modules/audio_processing/aec3/stationarity_estimator.o \
+	modules/audio_processing/aec3/subband_erle_estimator.o \
 	modules/audio_processing/aec3/subtractor.o \
 	modules/audio_processing/aec3/subtractor_output.o \
 	modules/audio_processing/aec3/subtractor_output_analyzer.o \
@@ -149,6 +152,7 @@
 	modules/audio_processing/agc2/adaptive_digital_gain_applier.o \
 	modules/audio_processing/agc2/adaptive_mode_level_estimator.o \
 	modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.o \
+	modules/audio_processing/agc2/agc2_common.o \
 	modules/audio_processing/agc2/biquad_filter.o \
 	modules/audio_processing/agc2/compute_interpolated_gain_curve.o \
 	modules/audio_processing/agc2/down_sampler.o \
diff --git a/modules/audio_processing/transient/transient_detector_unittest.cc b/modules/audio_processing/transient/transient_detector_unittest.cc
index d1eb7af..091a573 100644
--- a/modules/audio_processing/transient/transient_detector_unittest.cc
+++ b/modules/audio_processing/transient/transient_detector_unittest.cc
@@ -11,11 +11,11 @@
 #include "modules/audio_processing/transient/transient_detector.h"
 
 #include <memory>
-#include <sstream>
 #include <string>
 
 #include "modules/audio_processing/transient/common.h"
 #include "modules/audio_processing/transient/file_utils.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/file_wrapper.h"
 #include "test/gtest.h"
 #include "test/testsupport/fileutils.h"
@@ -43,7 +43,7 @@
     int sample_rate_hz = kSampleRatesHz[i];
 
     // Prepare detect file.
-    std::stringstream detect_file_name;
+    rtc::StringBuilder detect_file_name;
     detect_file_name << "audio_processing/transient/detect"
                      << (sample_rate_hz / 1000) << "kHz";
 
@@ -58,7 +58,7 @@
                              << detect_file_name.str().c_str();
 
     // Prepare audio file.
-    std::stringstream audio_file_name;
+    rtc::StringBuilder audio_file_name;
     audio_file_name << "audio_processing/transient/audio"
                     << (sample_rate_hz / 1000) << "kHz";
 
diff --git a/modules/audio_processing/transient/wpd_tree_unittest.cc b/modules/audio_processing/transient/wpd_tree_unittest.cc
index 88f0739..830a5df 100644
--- a/modules/audio_processing/transient/wpd_tree_unittest.cc
+++ b/modules/audio_processing/transient/wpd_tree_unittest.cc
@@ -11,11 +11,11 @@
 #include "modules/audio_processing/transient/wpd_tree.h"
 
 #include <memory>
-#include <sstream>
 #include <string>
 
 #include "modules/audio_processing/transient/daubechies_8_wavelet_coeffs.h"
 #include "modules/audio_processing/transient/file_utils.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/file_wrapper.h"
 #include "test/gtest.h"
 #include "test/testsupport/fileutils.h"
@@ -87,7 +87,7 @@
     // Matlab files.
     matlab_files_data[i].reset(FileWrapper::Create());
 
-    std::ostringstream matlab_stream;
+    rtc::StringBuilder matlab_stream;
     matlab_stream << "audio_processing/transient/wpd" << i;
     std::string matlab_string = test::ResourcePath(matlab_stream.str(), "dat");
     matlab_files_data[i]->OpenFile(matlab_string.c_str(), true);  // Read only.
@@ -98,7 +98,7 @@
     // Out files.
     out_files_data[i].reset(FileWrapper::Create());
 
-    std::ostringstream out_stream;
+    rtc::StringBuilder out_stream;
     out_stream << test::OutputPath() << "wpd_" << i << ".out";
     std::string out_string = out_stream.str();
 
diff --git a/modules/audio_processing/typing_detection.h b/modules/audio_processing/typing_detection.h
index 70fd903..d8fb359 100644
--- a/modules/audio_processing/typing_detection.h
+++ b/modules/audio_processing/typing_detection.h
@@ -11,9 +11,11 @@
 #ifndef MODULES_AUDIO_PROCESSING_TYPING_DETECTION_H_
 #define MODULES_AUDIO_PROCESSING_TYPING_DETECTION_H_
 
+#include "rtc_base/system/rtc_export.h"
+
 namespace webrtc {
 
-class TypingDetection {
+class RTC_EXPORT TypingDetection {
  public:
   TypingDetection();
   virtual ~TypingDetection();
diff --git a/modules/include/module_common_types.cc b/modules/include/module_common_types.cc
new file mode 100644
index 0000000..4ad5d14
--- /dev/null
+++ b/modules/include/module_common_types.cc
@@ -0,0 +1,159 @@
+/*
+ *  Copyright (c) 2018 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 "modules/include/module_common_types.h"
+
+#include <string.h>
+#include <utility>
+
+#include "rtc_base/numerics/safe_conversions.h"
+
+namespace webrtc {
+
+RTPFragmentationHeader::RTPFragmentationHeader()
+    : fragmentationVectorSize(0),
+      fragmentationOffset(nullptr),
+      fragmentationLength(nullptr),
+      fragmentationTimeDiff(nullptr),
+      fragmentationPlType(nullptr) {}
+
+RTPFragmentationHeader::RTPFragmentationHeader(RTPFragmentationHeader&& other)
+    : RTPFragmentationHeader() {
+  swap(*this, other);
+}
+
+RTPFragmentationHeader& RTPFragmentationHeader::operator=(
+    RTPFragmentationHeader&& other) {
+  swap(*this, other);
+  return *this;
+}
+
+RTPFragmentationHeader::~RTPFragmentationHeader() {
+  delete[] fragmentationOffset;
+  delete[] fragmentationLength;
+  delete[] fragmentationTimeDiff;
+  delete[] fragmentationPlType;
+}
+
+void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b) {
+  using std::swap;
+  swap(a.fragmentationVectorSize, b.fragmentationVectorSize);
+  swap(a.fragmentationOffset, b.fragmentationOffset);
+  swap(a.fragmentationLength, b.fragmentationLength);
+  swap(a.fragmentationTimeDiff, b.fragmentationTimeDiff);
+  swap(a.fragmentationPlType, b.fragmentationPlType);
+}
+
+void RTPFragmentationHeader::CopyFrom(const RTPFragmentationHeader& src) {
+  if (this == &src) {
+    return;
+  }
+
+  if (src.fragmentationVectorSize != fragmentationVectorSize) {
+    // new size of vectors
+
+    // delete old
+    delete[] fragmentationOffset;
+    fragmentationOffset = nullptr;
+    delete[] fragmentationLength;
+    fragmentationLength = nullptr;
+    delete[] fragmentationTimeDiff;
+    fragmentationTimeDiff = nullptr;
+    delete[] fragmentationPlType;
+    fragmentationPlType = nullptr;
+
+    if (src.fragmentationVectorSize > 0) {
+      // allocate new
+      if (src.fragmentationOffset) {
+        fragmentationOffset = new size_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationLength) {
+        fragmentationLength = new size_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationTimeDiff) {
+        fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationPlType) {
+        fragmentationPlType = new uint8_t[src.fragmentationVectorSize];
+      }
+    }
+    // set new size
+    fragmentationVectorSize = src.fragmentationVectorSize;
+  }
+
+  if (src.fragmentationVectorSize > 0) {
+    // copy values
+    if (src.fragmentationOffset) {
+      memcpy(fragmentationOffset, src.fragmentationOffset,
+             src.fragmentationVectorSize * sizeof(size_t));
+    }
+    if (src.fragmentationLength) {
+      memcpy(fragmentationLength, src.fragmentationLength,
+             src.fragmentationVectorSize * sizeof(size_t));
+    }
+    if (src.fragmentationTimeDiff) {
+      memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff,
+             src.fragmentationVectorSize * sizeof(uint16_t));
+    }
+    if (src.fragmentationPlType) {
+      memcpy(fragmentationPlType, src.fragmentationPlType,
+             src.fragmentationVectorSize * sizeof(uint8_t));
+    }
+  }
+}
+
+void RTPFragmentationHeader::Resize(size_t size) {
+  const uint16_t size16 = rtc::dchecked_cast<uint16_t>(size);
+  if (fragmentationVectorSize < size16) {
+    uint16_t oldVectorSize = fragmentationVectorSize;
+    {
+      // offset
+      size_t* oldOffsets = fragmentationOffset;
+      fragmentationOffset = new size_t[size16];
+      memset(fragmentationOffset + oldVectorSize, 0,
+             sizeof(size_t) * (size16 - oldVectorSize));
+      // copy old values
+      memcpy(fragmentationOffset, oldOffsets, sizeof(size_t) * oldVectorSize);
+      delete[] oldOffsets;
+    }
+    // length
+    {
+      size_t* oldLengths = fragmentationLength;
+      fragmentationLength = new size_t[size16];
+      memset(fragmentationLength + oldVectorSize, 0,
+             sizeof(size_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationLength, oldLengths, sizeof(size_t) * oldVectorSize);
+      delete[] oldLengths;
+    }
+    // time diff
+    {
+      uint16_t* oldTimeDiffs = fragmentationTimeDiff;
+      fragmentationTimeDiff = new uint16_t[size16];
+      memset(fragmentationTimeDiff + oldVectorSize, 0,
+             sizeof(uint16_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationTimeDiff, oldTimeDiffs,
+             sizeof(uint16_t) * oldVectorSize);
+      delete[] oldTimeDiffs;
+    }
+    // payload type
+    {
+      uint8_t* oldTimePlTypes = fragmentationPlType;
+      fragmentationPlType = new uint8_t[size16];
+      memset(fragmentationPlType + oldVectorSize, 0,
+             sizeof(uint8_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationPlType, oldTimePlTypes,
+             sizeof(uint8_t) * oldVectorSize);
+      delete[] oldTimePlTypes;
+    }
+    fragmentationVectorSize = size16;
+  }
+}
+
+}  // namespace webrtc
diff --git a/modules/include/module_common_types.h b/modules/include/module_common_types.h
index 8ee2369..98ff767 100644
--- a/modules/include/module_common_types.h
+++ b/modules/include/module_common_types.h
@@ -11,24 +11,14 @@
 #ifndef MODULES_INCLUDE_MODULE_COMMON_TYPES_H_
 #define MODULES_INCLUDE_MODULE_COMMON_TYPES_H_
 
-#include <assert.h>
-#include <string.h>  // memcpy
+#include <stddef.h>
+#include <stdint.h>
 
-#include <algorithm>
-#include <limits>
-
-#include "absl/types/optional.h"
 #include "api/rtp_headers.h"
-#include "api/transport/network_types.h"
-#include "api/video/video_rotation.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/include/module_common_types_public.h"
 #include "modules/include/module_fec_types.h"
 #include "modules/rtp_rtcp/source/rtp_video_header.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/deprecation.h"
-#include "rtc_base/numerics/safe_conversions.h"
-#include "rtc_base/timeutils.h"
 
 namespace webrtc {
 
@@ -45,142 +35,29 @@
 
 class RTPFragmentationHeader {
  public:
-  RTPFragmentationHeader()
-      : fragmentationVectorSize(0),
-        fragmentationOffset(NULL),
-        fragmentationLength(NULL),
-        fragmentationTimeDiff(NULL),
-        fragmentationPlType(NULL) {}
+  RTPFragmentationHeader();
+  RTPFragmentationHeader(const RTPFragmentationHeader&) = delete;
+  RTPFragmentationHeader(RTPFragmentationHeader&& other);
+  RTPFragmentationHeader& operator=(const RTPFragmentationHeader& other) =
+      delete;
+  RTPFragmentationHeader& operator=(RTPFragmentationHeader&& other);
+  ~RTPFragmentationHeader();
 
-  RTPFragmentationHeader(RTPFragmentationHeader&& other)
-      : RTPFragmentationHeader() {
-    std::swap(*this, other);
-  }
+  friend void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b);
 
-  ~RTPFragmentationHeader() {
-    delete[] fragmentationOffset;
-    delete[] fragmentationLength;
-    delete[] fragmentationTimeDiff;
-    delete[] fragmentationPlType;
-  }
+  void CopyFrom(const RTPFragmentationHeader& src);
+  void VerifyAndAllocateFragmentationHeader(size_t size) { Resize(size); }
 
-  void operator=(RTPFragmentationHeader&& other) { std::swap(*this, other); }
+  void Resize(size_t size);
+  size_t Size() const { return fragmentationVectorSize; }
 
-  friend void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b) {
-    using std::swap;
-    swap(a.fragmentationVectorSize, b.fragmentationVectorSize);
-    swap(a.fragmentationOffset, b.fragmentationOffset);
-    swap(a.fragmentationLength, b.fragmentationLength);
-    swap(a.fragmentationTimeDiff, b.fragmentationTimeDiff);
-    swap(a.fragmentationPlType, b.fragmentationPlType);
-  }
+  size_t Offset(size_t index) const { return fragmentationOffset[index]; }
+  size_t Length(size_t index) const { return fragmentationLength[index]; }
+  uint16_t TimeDiff(size_t index) const { return fragmentationTimeDiff[index]; }
+  int PayloadType(size_t index) const { return fragmentationPlType[index]; }
 
-  void CopyFrom(const RTPFragmentationHeader& src) {
-    if (this == &src) {
-      return;
-    }
-
-    if (src.fragmentationVectorSize != fragmentationVectorSize) {
-      // new size of vectors
-
-      // delete old
-      delete[] fragmentationOffset;
-      fragmentationOffset = NULL;
-      delete[] fragmentationLength;
-      fragmentationLength = NULL;
-      delete[] fragmentationTimeDiff;
-      fragmentationTimeDiff = NULL;
-      delete[] fragmentationPlType;
-      fragmentationPlType = NULL;
-
-      if (src.fragmentationVectorSize > 0) {
-        // allocate new
-        if (src.fragmentationOffset) {
-          fragmentationOffset = new size_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationLength) {
-          fragmentationLength = new size_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationTimeDiff) {
-          fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationPlType) {
-          fragmentationPlType = new uint8_t[src.fragmentationVectorSize];
-        }
-      }
-      // set new size
-      fragmentationVectorSize = src.fragmentationVectorSize;
-    }
-
-    if (src.fragmentationVectorSize > 0) {
-      // copy values
-      if (src.fragmentationOffset) {
-        memcpy(fragmentationOffset, src.fragmentationOffset,
-               src.fragmentationVectorSize * sizeof(size_t));
-      }
-      if (src.fragmentationLength) {
-        memcpy(fragmentationLength, src.fragmentationLength,
-               src.fragmentationVectorSize * sizeof(size_t));
-      }
-      if (src.fragmentationTimeDiff) {
-        memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff,
-               src.fragmentationVectorSize * sizeof(uint16_t));
-      }
-      if (src.fragmentationPlType) {
-        memcpy(fragmentationPlType, src.fragmentationPlType,
-               src.fragmentationVectorSize * sizeof(uint8_t));
-      }
-    }
-  }
-
-  void VerifyAndAllocateFragmentationHeader(const size_t size) {
-    assert(size <= std::numeric_limits<uint16_t>::max());
-    const uint16_t size16 = static_cast<uint16_t>(size);
-    if (fragmentationVectorSize < size16) {
-      uint16_t oldVectorSize = fragmentationVectorSize;
-      {
-        // offset
-        size_t* oldOffsets = fragmentationOffset;
-        fragmentationOffset = new size_t[size16];
-        memset(fragmentationOffset + oldVectorSize, 0,
-               sizeof(size_t) * (size16 - oldVectorSize));
-        // copy old values
-        memcpy(fragmentationOffset, oldOffsets, sizeof(size_t) * oldVectorSize);
-        delete[] oldOffsets;
-      }
-      // length
-      {
-        size_t* oldLengths = fragmentationLength;
-        fragmentationLength = new size_t[size16];
-        memset(fragmentationLength + oldVectorSize, 0,
-               sizeof(size_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationLength, oldLengths, sizeof(size_t) * oldVectorSize);
-        delete[] oldLengths;
-      }
-      // time diff
-      {
-        uint16_t* oldTimeDiffs = fragmentationTimeDiff;
-        fragmentationTimeDiff = new uint16_t[size16];
-        memset(fragmentationTimeDiff + oldVectorSize, 0,
-               sizeof(uint16_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationTimeDiff, oldTimeDiffs,
-               sizeof(uint16_t) * oldVectorSize);
-        delete[] oldTimeDiffs;
-      }
-      // payload type
-      {
-        uint8_t* oldTimePlTypes = fragmentationPlType;
-        fragmentationPlType = new uint8_t[size16];
-        memset(fragmentationPlType + oldVectorSize, 0,
-               sizeof(uint8_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationPlType, oldTimePlTypes,
-               sizeof(uint8_t) * oldVectorSize);
-        delete[] oldTimePlTypes;
-      }
-      fragmentationVectorSize = size16;
-    }
-  }
-
+  // TODO(danilchap): Move all members to private section,
+  // simplify by replacing 4 raw arrays with single std::vector<Fragment>
   uint16_t fragmentationVectorSize;  // Number of fragmentations
   size_t* fragmentationOffset;       // Offset of pointer to data for each
                                      // fragmentation
@@ -188,33 +65,6 @@
   uint16_t* fragmentationTimeDiff;   // Timestamp difference relative "now" for
                                      // each fragmentation
   uint8_t* fragmentationPlType;      // Payload type of each fragmentation
-
- private:
-  RTC_DISALLOW_COPY_AND_ASSIGN(RTPFragmentationHeader);
-};
-
-struct RTCPVoIPMetric {
-  // RFC 3611 4.7
-  uint8_t lossRate;
-  uint8_t discardRate;
-  uint8_t burstDensity;
-  uint8_t gapDensity;
-  uint16_t burstDuration;
-  uint16_t gapDuration;
-  uint16_t roundTripDelay;
-  uint16_t endSystemDelay;
-  uint8_t signalLevel;
-  uint8_t noiseLevel;
-  uint8_t RERL;
-  uint8_t Gmin;
-  uint8_t Rfactor;
-  uint8_t extRfactor;
-  uint8_t MOSLQ;
-  uint8_t MOSCQ;
-  uint8_t RXconfig;
-  uint16_t JBnominal;
-  uint16_t JBmax;
-  uint16_t JBabsMax;
 };
 
 // Interface used by the CallStats class to distribute call statistics.
diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn
index 055d712..a4774d8 100644
--- a/modules/rtp_rtcp/BUILD.gn
+++ b/modules/rtp_rtcp/BUILD.gn
@@ -9,6 +9,7 @@
 import("../../webrtc.gni")
 
 rtc_source_set("rtp_rtcp_format") {
+  visibility = [ "*" ]
   public = [
     "include/rtp_cvo.h",
     "include/rtp_header_extension_map.h",
@@ -39,7 +40,6 @@
     "source/rtcp_packet/tmmbn.h",
     "source/rtcp_packet/tmmbr.h",
     "source/rtcp_packet/transport_feedback.h",
-    "source/rtcp_packet/voip_metric.h",
     "source/rtp_generic_frame_descriptor.h",
     "source/rtp_generic_frame_descriptor_extension.h",
     "source/rtp_header_extensions.h",
@@ -74,7 +74,6 @@
     "source/rtcp_packet/tmmbn.cc",
     "source/rtcp_packet/tmmbr.cc",
     "source/rtcp_packet/transport_feedback.cc",
-    "source/rtcp_packet/voip_metric.cc",
     "source/rtp_generic_frame_descriptor.cc",
     "source/rtp_generic_frame_descriptor_extension.cc",
     "source/rtp_header_extension_map.cc",
@@ -90,6 +89,7 @@
     "../../api:array_view",
     "../../api:libjingle_peerconnection_api",
     "../../api/audio_codecs:audio_codecs_api",
+    "../../api/transport:network_control",
     "../../api/video:video_frame",
     "../../common_video",
     "../../rtc_base:checks",
@@ -109,8 +109,6 @@
     "include/receive_statistics.h",
     "include/remote_ntp_time_estimator.h",
     "include/rtp_header_parser.h",
-    "include/rtp_payload_registry.h",
-    "include/rtp_receiver.h",
     "include/rtp_rtcp.h",
     "include/ulpfec_receiver.h",
     "source/contributing_sources.cc",
@@ -152,18 +150,11 @@
     "source/rtp_format_vp8.h",
     "source/rtp_format_vp9.cc",
     "source/rtp_format_vp9.h",
+    "source/rtp_header_extension_size.cc",
+    "source/rtp_header_extension_size.h",
     "source/rtp_header_parser.cc",
     "source/rtp_packet_history.cc",
     "source/rtp_packet_history.h",
-    "source/rtp_payload_registry.cc",
-    "source/rtp_receiver_audio.cc",
-    "source/rtp_receiver_audio.h",
-    "source/rtp_receiver_impl.cc",
-    "source/rtp_receiver_impl.h",
-    "source/rtp_receiver_strategy.cc",
-    "source/rtp_receiver_strategy.h",
-    "source/rtp_receiver_video.cc",
-    "source/rtp_receiver_video.h",
     "source/rtp_rtcp_config.h",
     "source/rtp_rtcp_impl.cc",
     "source/rtp_rtcp_impl.h",
@@ -203,8 +194,8 @@
     "../../api/audio_codecs:audio_codecs_api",
     "../../api/video:video_bitrate_allocation",
     "../../api/video:video_bitrate_allocator",
-    "../../api/video:video_frame",
     "../../api/video_codecs:video_codecs_api",
+    "../../call:rtp_interfaces",
     "../../common_video",
     "../../logging:rtc_event_audio",
     "../../logging:rtc_event_log_api",
@@ -221,10 +212,11 @@
     "../../rtc_base/system:fallthrough",
     "../../rtc_base/time:timestamp_extrapolator",
     "../../system_wrappers",
-    "../../system_wrappers:field_trial_api",
-    "../../system_wrappers:metrics_api",
+    "../../system_wrappers:field_trial",
+    "../../system_wrappers:metrics",
     "../audio_coding:audio_format_conversion",
     "../remote_bitrate_estimator",
+    "//third_party/abseil-cpp/absl/container:inlined_vector",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
@@ -252,8 +244,8 @@
     "../../api/video:video_bitrate_allocation",
     "../../rtc_base:checks",
     "../../rtc_base:rtc_base_approved",
+    "../../rtc_base:rtc_cancelable_task",
     "../../rtc_base:rtc_task_queue",
-    "../../rtc_base:weak_ptr",
     "../../system_wrappers",
     "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/types:optional",
@@ -385,7 +377,6 @@
       "source/rtcp_packet/tmmbn_unittest.cc",
       "source/rtcp_packet/tmmbr_unittest.cc",
       "source/rtcp_packet/transport_feedback_unittest.cc",
-      "source/rtcp_packet/voip_metric_unittest.cc",
       "source/rtcp_packet_unittest.cc",
       "source/rtcp_receiver_unittest.cc",
       "source/rtcp_sender_unittest.cc",
@@ -393,6 +384,7 @@
       "source/rtcp_transceiver_unittest.cc",
       "source/rtp_fec_unittest.cc",
       "source/rtp_format_h264_unittest.cc",
+      "source/rtp_format_unittest.cc",
       "source/rtp_format_video_generic_unittest.cc",
       "source/rtp_format_vp8_test_helper.cc",
       "source/rtp_format_vp8_test_helper.h",
@@ -400,10 +392,9 @@
       "source/rtp_format_vp9_unittest.cc",
       "source/rtp_generic_frame_descriptor_extension_unittest.cc",
       "source/rtp_header_extension_map_unittest.cc",
+      "source/rtp_header_extension_size_unittest.cc",
       "source/rtp_packet_history_unittest.cc",
       "source/rtp_packet_unittest.cc",
-      "source/rtp_payload_registry_unittest.cc",
-      "source/rtp_receiver_unittest.cc",
       "source/rtp_rtcp_impl_unittest.cc",
       "source/rtp_sender_unittest.cc",
       "source/rtp_utility_unittest.cc",
@@ -411,11 +402,6 @@
       "source/ulpfec_generator_unittest.cc",
       "source/ulpfec_header_reader_writer_unittest.cc",
       "source/ulpfec_receiver_unittest.cc",
-      "test/testAPI/test_api.cc",
-      "test/testAPI/test_api.h",
-      "test/testAPI/test_api_audio.cc",
-      "test/testAPI/test_api_rtcp.cc",
-      "test/testAPI/test_api_video.cc",
     ]
     deps = [
       ":fec_test_helper",
@@ -447,6 +433,7 @@
       "../../test:test_common",
       "../../test:test_support",
       "../audio_coding:audio_format_conversion",
+      "../video_coding:codec_globals_headers",
       "//third_party/abseil-cpp/absl/memory",
       "//third_party/abseil-cpp/absl/types:optional",
     ]
diff --git a/modules/rtp_rtcp/include/flexfec_sender.h b/modules/rtp_rtcp/include/flexfec_sender.h
index 57ba841..12277e1 100644
--- a/modules/rtp_rtcp/include/flexfec_sender.h
+++ b/modules/rtp_rtcp/include/flexfec_sender.h
@@ -21,6 +21,7 @@
 #include "modules/rtp_rtcp/include/flexfec_sender.h"
 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
+#include "modules/rtp_rtcp/source/rtp_header_extension_size.h"
 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
 #include "modules/rtp_rtcp/source/ulpfec_generator.h"
 #include "rtc_base/random.h"
diff --git a/modules/rtp_rtcp/include/receive_statistics.h b/modules/rtp_rtcp/include/receive_statistics.h
index fe72ada..24d6f81 100644
--- a/modules/rtp_rtcp/include/receive_statistics.h
+++ b/modules/rtp_rtcp/include/receive_statistics.h
@@ -14,6 +14,7 @@
 #include <map>
 #include <vector>
 
+#include "call/rtp_packet_sink_interface.h"
 #include "modules/include/module.h"
 #include "modules/include/module_common_types.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
@@ -48,28 +49,22 @@
   virtual uint32_t BitrateReceived() const = 0;
 };
 
-class ReceiveStatistics : public ReceiveStatisticsProvider {
+class ReceiveStatistics : public ReceiveStatisticsProvider,
+                          public RtpPacketSinkInterface {
  public:
   ~ReceiveStatistics() override = default;
 
   static ReceiveStatistics* Create(Clock* clock);
 
   // Updates the receive statistics with this packet.
+  // TODO(bugs.webrtc.org/8016): Deprecated. Delete as soon as
+  // downstream code is updated to use OnRtpPacket.
+  RTC_DEPRECATED
   virtual void IncomingPacket(const RTPHeader& rtp_header,
                               size_t packet_length) = 0;
 
-  // TODO(nisse): Wrapper for backwards compatibility. Delete as soon as
-  // downstream callers are updated.
-  RTC_DEPRECATED
-  void IncomingPacket(const RTPHeader& rtp_header,
-                      size_t packet_length,
-                      bool retransmitted) {
-    IncomingPacket(rtp_header, packet_length);
-  }
-
   // Increment counter for number of FEC packets received.
-  virtual void FecPacketReceived(const RTPHeader& header,
-                                 size_t packet_length) = 0;
+  virtual void FecPacketReceived(const RtpPacketReceived& packet) = 0;
 
   // Returns a pointer to the statistician of an ssrc.
   virtual StreamStatistician* GetStatistician(uint32_t ssrc) const = 0;
diff --git a/modules/rtp_rtcp/include/rtp_header_extension_map.h b/modules/rtp_rtcp/include/rtp_header_extension_map.h
index 89a0ed5..b8f27a1 100644
--- a/modules/rtp_rtcp/include/rtp_header_extension_map.h
+++ b/modules/rtp_rtcp/include/rtp_header_extension_map.h
@@ -20,11 +20,6 @@
 
 namespace webrtc {
 
-struct RtpExtensionSize {
-  RTPExtensionType type;
-  uint8_t value_size;
-};
-
 class RtpHeaderExtensionMap {
  public:
   static constexpr RTPExtensionType kInvalidType = kRtpExtensionNone;
@@ -44,11 +39,7 @@
     return GetId(type) != kInvalidId;
   }
   // Return kInvalidType if not found.
-  RTPExtensionType GetType(int id) const {
-    RTC_DCHECK_GE(id, kMinId);
-    RTC_DCHECK_LE(id, kMaxId);
-    return types_[id];
-  }
+  RTPExtensionType GetType(int id) const;
   // Return kInvalidId if not found.
   uint8_t GetId(RTPExtensionType type) const {
     RTC_DCHECK_GT(type, kRtpExtensionNone);
@@ -56,22 +47,24 @@
     return ids_[type];
   }
 
-  size_t GetTotalLengthInBytes(
-      rtc::ArrayView<const RtpExtensionSize> extensions) const;
-
   // TODO(danilchap): Remove use of the functions below.
   int32_t Register(RTPExtensionType type, int id) {
     return RegisterByType(id, type) ? 0 : -1;
   }
   int32_t Deregister(RTPExtensionType type);
 
+  bool IsMixedOneTwoByteHeaderSupported() const {
+    return mixed_one_two_byte_header_supported_;
+  }
+  void SetMixedOneTwoByteHeaderSupported(bool supported) {
+    mixed_one_two_byte_header_supported_ = supported;
+  }
+
  private:
-  static constexpr int kMinId = 1;
-  static constexpr int kMaxId = 14;
   bool Register(int id, RTPExtensionType type, const char* uri);
 
-  RTPExtensionType types_[kMaxId + 1];
   uint8_t ids_[kRtpExtensionNumberOfExtensions];
+  bool mixed_one_two_byte_header_supported_;
 };
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/include/rtp_payload_registry.h b/modules/rtp_rtcp/include/rtp_payload_registry.h
deleted file mode 100644
index 229c71b..0000000
--- a/modules/rtp_rtcp/include/rtp_payload_registry.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- *  Copyright (c) 2013 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 MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_
-#define MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_
-
-#include <map>
-#include <set>
-
-#include "absl/types/optional.h"
-#include "api/audio_codecs/audio_format.h"
-#include "api/video_codecs/video_codec.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "rtc_base/criticalsection.h"
-
-namespace webrtc {
-
-class RTPPayloadRegistry {
- public:
-  RTPPayloadRegistry();
-  ~RTPPayloadRegistry();
-
-  // TODO(magjed): Split RTPPayloadRegistry into separate Audio and Video class
-  // and simplify the code. http://crbug/webrtc/6743.
-
-  // Replace all audio receive payload types with the given map.
-  void SetAudioReceivePayloads(std::map<int, SdpAudioFormat> codecs);
-
-  int32_t RegisterReceivePayload(int payload_type,
-                                 const SdpAudioFormat& audio_format,
-                                 bool* created_new_payload_type);
-  int32_t RegisterReceivePayload(const VideoCodec& video_codec);
-
-  int32_t DeRegisterReceivePayload(int8_t payload_type);
-
-  int GetPayloadTypeFrequency(uint8_t payload_type) const;
-
-  absl::optional<RtpUtility::Payload> PayloadTypeToPayload(
-      uint8_t payload_type) const;
-
- private:
-  // Prunes the payload type map of the specific payload type, if it exists.
-  void DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
-      const SdpAudioFormat& audio_format);
-
-  rtc::CriticalSection crit_sect_;
-  std::map<int, RtpUtility::Payload> payload_type_map_;
-
-// As a first step in splitting this class up in separate cases for audio and
-// video, DCHECK that no instance is used for both audio and video.
-#if RTC_DCHECK_IS_ON
-  bool used_for_audio_ RTC_GUARDED_BY(crit_sect_) = false;
-  bool used_for_video_ RTC_GUARDED_BY(crit_sect_) = false;
-#endif
-};
-
-}  // namespace webrtc
-
-#endif  // MODULES_RTP_RTCP_INCLUDE_RTP_PAYLOAD_REGISTRY_H_
diff --git a/modules/rtp_rtcp/include/rtp_receiver.h b/modules/rtp_rtcp/include/rtp_receiver.h
deleted file mode 100644
index d93d656..0000000
--- a/modules/rtp_rtcp/include/rtp_receiver.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- *  Copyright (c) 2012 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 MODULES_RTP_RTCP_INCLUDE_RTP_RECEIVER_H_
-#define MODULES_RTP_RTCP_INCLUDE_RTP_RECEIVER_H_
-
-#include <vector>
-
-#include "api/rtpreceiverinterface.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-
-namespace webrtc {
-
-class RTPPayloadRegistry;
-class VideoCodec;
-
-class RtpReceiver {
- public:
-  // Creates a video-enabled RTP receiver.
-  static RtpReceiver* CreateVideoReceiver(
-      Clock* clock,
-      RtpData* incoming_payload_callback,
-      RTPPayloadRegistry* rtp_payload_registry);
-
-  // Creates an audio-enabled RTP receiver.
-  static RtpReceiver* CreateAudioReceiver(
-      Clock* clock,
-      RtpData* incoming_payload_callback,
-      RTPPayloadRegistry* rtp_payload_registry);
-
-  virtual ~RtpReceiver() {}
-
-  // Registers a receive payload in the payload registry and notifies the media
-  // receiver strategy.
-  virtual int32_t RegisterReceivePayload(
-      int payload_type,
-      const SdpAudioFormat& audio_format) = 0;
-
-  // Deprecated version of the above.
-  int32_t RegisterReceivePayload(const CodecInst& audio_codec);
-
-  // Registers a receive payload in the payload registry.
-  virtual int32_t RegisterReceivePayload(const VideoCodec& video_codec) = 0;
-
-  // De-registers |payload_type| from the payload registry.
-  virtual int32_t DeRegisterReceivePayload(const int8_t payload_type) = 0;
-
-  // Parses the media specific parts of an RTP packet and updates the receiver
-  // state. This for instance means that any changes in SSRC and payload type is
-  // detected and acted upon.
-  virtual bool IncomingRtpPacket(const RTPHeader& rtp_header,
-                                 const uint8_t* payload,
-                                 size_t payload_length,
-                                 PayloadUnion payload_specific) = 0;
-  // TODO(nisse): Deprecated version, delete as soon as downstream
-  // applications are updated.
-  bool IncomingRtpPacket(const RTPHeader& rtp_header,
-                         const uint8_t* payload,
-                         size_t payload_length,
-                         PayloadUnion payload_specific,
-                         bool in_order /* Ignored */) {
-    return IncomingRtpPacket(rtp_header, payload, payload_length,
-                             payload_specific);
-  }
-
-  // Gets the RTP timestamp and the corresponding monotonic system
-  // time for the most recent in-order packet. Returns true on
-  // success, false if no packet has been received.
-  virtual bool GetLatestTimestamps(uint32_t* timestamp,
-                                   int64_t* receive_time_ms) const = 0;
-
-  // Returns the remote SSRC of the currently received RTP stream.
-  virtual uint32_t SSRC() const = 0;
-
-  virtual std::vector<RtpSource> GetSources() const = 0;
-};
-}  // namespace webrtc
-
-#endif  // MODULES_RTP_RTCP_INCLUDE_RTP_RECEIVER_H_
diff --git a/modules/rtp_rtcp/include/rtp_rtcp.h b/modules/rtp_rtcp/include/rtp_rtcp.h
index f982a9b..a99544c 100644
--- a/modules/rtp_rtcp/include/rtp_rtcp.h
+++ b/modules/rtp_rtcp/include/rtp_rtcp.h
@@ -36,8 +36,6 @@
 class Transport;
 class VideoBitrateAllocationObserver;
 
-RTPExtensionType StringToRtpExtensionType(const std::string& extension);
-
 namespace rtcp {
 class TransportFeedback;
 }
@@ -142,6 +140,8 @@
   // Returns -1 on failure else 0.
   virtual int32_t RegisterSendRtpHeaderExtension(RTPExtensionType type,
                                                  uint8_t id) = 0;
+  // Register extension by uri, returns false on failure.
+  virtual bool RegisterRtpHeaderExtension(const std::string& uri, int id) = 0;
 
   virtual int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) = 0;
 
@@ -213,6 +213,10 @@
   // Returns current media sending status.
   virtual bool SendingMedia() const = 0;
 
+  // Indicate that the packets sent by this module should be counted towards the
+  // bitrate estimate since the stream participates in the bitrate allocation.
+  virtual void SetAsPartOfAllocation(bool part_of_allocation) = 0;
+
   // Returns current bitrate in Kbit/s.
   virtual void BitrateSent(uint32_t* total_rate,
                            uint32_t* video_rate,
@@ -335,10 +339,6 @@
                                                  uint32_t name,
                                                  const uint8_t* data,
                                                  uint16_t length) = 0;
-  // (XR) Sets VOIP metric.
-  // Returns -1 on failure else 0.
-  virtual int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) = 0;
-
   // (XR) Sets Receiver Reference Time Report (RTTR) status.
   virtual void SetRtcpXrRrtrStatus(bool enable) = 0;
 
diff --git a/modules/rtp_rtcp/include/rtp_rtcp_defines.cc b/modules/rtp_rtcp/include/rtp_rtcp_defines.cc
index f86b238..e1dca33 100644
--- a/modules/rtp_rtcp/include/rtp_rtcp_defines.cc
+++ b/modules/rtp_rtcp/include/rtp_rtcp_defines.cc
@@ -54,4 +54,73 @@
 PayloadUnion& PayloadUnion::operator=(const PayloadUnion&) = default;
 PayloadUnion& PayloadUnion::operator=(PayloadUnion&&) = default;
 
+PacketFeedback::PacketFeedback(int64_t arrival_time_ms,
+                               uint16_t sequence_number)
+    : PacketFeedback(-1,
+                     arrival_time_ms,
+                     kNoSendTime,
+                     sequence_number,
+                     0,
+                     0,
+                     0,
+                     PacedPacketInfo()) {}
+
+PacketFeedback::PacketFeedback(int64_t arrival_time_ms,
+                               int64_t send_time_ms,
+                               uint16_t sequence_number,
+                               size_t payload_size,
+                               const PacedPacketInfo& pacing_info)
+    : PacketFeedback(-1,
+                     arrival_time_ms,
+                     send_time_ms,
+                     sequence_number,
+                     payload_size,
+                     0,
+                     0,
+                     pacing_info) {}
+
+PacketFeedback::PacketFeedback(int64_t creation_time_ms,
+                               uint16_t sequence_number,
+                               size_t payload_size,
+                               uint16_t local_net_id,
+                               uint16_t remote_net_id,
+                               const PacedPacketInfo& pacing_info)
+    : PacketFeedback(creation_time_ms,
+                     kNotReceived,
+                     kNoSendTime,
+                     sequence_number,
+                     payload_size,
+                     local_net_id,
+                     remote_net_id,
+                     pacing_info) {}
+
+PacketFeedback::PacketFeedback(int64_t creation_time_ms,
+                               int64_t arrival_time_ms,
+                               int64_t send_time_ms,
+                               uint16_t sequence_number,
+                               size_t payload_size,
+                               uint16_t local_net_id,
+                               uint16_t remote_net_id,
+                               const PacedPacketInfo& pacing_info)
+    : creation_time_ms(creation_time_ms),
+      arrival_time_ms(arrival_time_ms),
+      send_time_ms(send_time_ms),
+      sequence_number(sequence_number),
+      payload_size(payload_size),
+      unacknowledged_data(0),
+      local_net_id(local_net_id),
+      remote_net_id(remote_net_id),
+      pacing_info(pacing_info) {}
+
+PacketFeedback::PacketFeedback(const PacketFeedback&) = default;
+PacketFeedback& PacketFeedback::operator=(const PacketFeedback&) = default;
+PacketFeedback::~PacketFeedback() = default;
+
+bool PacketFeedback::operator==(const PacketFeedback& rhs) const {
+  return arrival_time_ms == rhs.arrival_time_ms &&
+         send_time_ms == rhs.send_time_ms &&
+         sequence_number == rhs.sequence_number &&
+         payload_size == rhs.payload_size && pacing_info == rhs.pacing_info;
+}
+
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/include/rtp_rtcp_defines.h b/modules/rtp_rtcp/include/rtp_rtcp_defines.h
index 34c7428..e587313 100644
--- a/modules/rtp_rtcp/include/rtp_rtcp_defines.h
+++ b/modules/rtp_rtcp/include/rtp_rtcp_defines.h
@@ -18,6 +18,7 @@
 #include "absl/types/variant.h"
 #include "api/audio_codecs/audio_format.h"
 #include "api/rtp_headers.h"
+#include "api/transport/network_types.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/include/module_common_types.h"
 #include "system_wrappers/include/clock.h"
@@ -93,7 +94,10 @@
 
 enum StorageType { kDontRetransmit, kAllowRetransmission };
 
-enum RTPExtensionType {
+// This enum must not have any gaps, i.e., all integers between
+// kRtpExtensionNone and kRtpExtensionNumberOfExtensions must be valid enum
+// entries.
+enum RTPExtensionType : int {
   kRtpExtensionNone,
   kRtpExtensionTransmissionTimeOffset,
   kRtpExtensionAudioLevel,
@@ -103,6 +107,7 @@
   kRtpExtensionPlayoutDelay,
   kRtpExtensionVideoContentType,
   kRtpExtensionVideoTiming,
+  kRtpExtensionFrameMarking,
   kRtpExtensionRtpStreamId,
   kRtpExtensionRepairedRtpStreamId,
   kRtpExtensionMid,
@@ -125,7 +130,6 @@
   kRtcpTmmbr = 0x0100,
   kRtcpTmmbn = 0x0200,
   kRtcpSrReq = 0x0400,
-  kRtcpXrVoipMetric = 0x0800,
   kRtcpApp = 0x1000,
   kRtcpRemb = 0x10000,
   kRtcpTransmissionTimeOffset = 0x20000,
@@ -259,44 +263,20 @@
 };
 
 struct PacketFeedback {
-  PacketFeedback(int64_t arrival_time_ms, uint16_t sequence_number)
-      : PacketFeedback(-1,
-                       arrival_time_ms,
-                       kNoSendTime,
-                       sequence_number,
-                       0,
-                       0,
-                       0,
-                       PacedPacketInfo()) {}
+  PacketFeedback(int64_t arrival_time_ms, uint16_t sequence_number);
 
   PacketFeedback(int64_t arrival_time_ms,
                  int64_t send_time_ms,
                  uint16_t sequence_number,
                  size_t payload_size,
-                 const PacedPacketInfo& pacing_info)
-      : PacketFeedback(-1,
-                       arrival_time_ms,
-                       send_time_ms,
-                       sequence_number,
-                       payload_size,
-                       0,
-                       0,
-                       pacing_info) {}
+                 const PacedPacketInfo& pacing_info);
 
   PacketFeedback(int64_t creation_time_ms,
                  uint16_t sequence_number,
                  size_t payload_size,
                  uint16_t local_net_id,
                  uint16_t remote_net_id,
-                 const PacedPacketInfo& pacing_info)
-      : PacketFeedback(creation_time_ms,
-                       kNotReceived,
-                       kNoSendTime,
-                       sequence_number,
-                       payload_size,
-                       local_net_id,
-                       remote_net_id,
-                       pacing_info) {}
+                 const PacedPacketInfo& pacing_info);
 
   PacketFeedback(int64_t creation_time_ms,
                  int64_t arrival_time_ms,
@@ -305,15 +285,10 @@
                  size_t payload_size,
                  uint16_t local_net_id,
                  uint16_t remote_net_id,
-                 const PacedPacketInfo& pacing_info)
-      : creation_time_ms(creation_time_ms),
-        arrival_time_ms(arrival_time_ms),
-        send_time_ms(send_time_ms),
-        sequence_number(sequence_number),
-        payload_size(payload_size),
-        local_net_id(local_net_id),
-        remote_net_id(remote_net_id),
-        pacing_info(pacing_info) {}
+                 const PacedPacketInfo& pacing_info);
+  PacketFeedback(const PacketFeedback&);
+  PacketFeedback& operator=(const PacketFeedback&);
+  ~PacketFeedback();
 
   static constexpr int kNotAProbe = -1;
   static constexpr int64_t kNotReceived = -1;
@@ -324,12 +299,7 @@
   //       for book-keeping, and is of no interest outside that class.
   // TODO(philipel): Remove |creation_time_ms| from PacketFeedback when cleaning
   //                 up SendTimeHistory.
-  bool operator==(const PacketFeedback& rhs) const {
-    return arrival_time_ms == rhs.arrival_time_ms &&
-           send_time_ms == rhs.send_time_ms &&
-           sequence_number == rhs.sequence_number &&
-           payload_size == rhs.payload_size && pacing_info == rhs.pacing_info;
-  }
+  bool operator==(const PacketFeedback& rhs) const;
 
   // Time corresponding to when this object was created.
   int64_t creation_time_ms;
@@ -348,6 +318,8 @@
   int64_t long_sequence_number;
   // Size of the packet excluding RTP headers.
   size_t payload_size;
+  // Size of preceeding packets that are not part of feedback.
+  size_t unacknowledged_data;
   // The network route ids that this packet is associated with.
   uint16_t local_net_id;
   uint16_t remote_net_id;
diff --git a/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h b/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
index d176930..e95bdd3 100644
--- a/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
+++ b/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h
@@ -54,6 +54,8 @@
   MOCK_METHOD1(DeRegisterSendPayload, int32_t(int8_t payload_type));
   MOCK_METHOD2(RegisterSendRtpHeaderExtension,
                int32_t(RTPExtensionType type, uint8_t id));
+  MOCK_METHOD2(RegisterRtpHeaderExtension,
+               bool(const std::string& uri, int id));
   MOCK_METHOD1(DeregisterSendRtpHeaderExtension,
                int32_t(RTPExtensionType type));
   MOCK_CONST_METHOD0(HasBweExtensions, bool());
@@ -81,6 +83,7 @@
   MOCK_CONST_METHOD0(Sending, bool());
   MOCK_METHOD1(SetSendingMediaStatus, void(bool sending));
   MOCK_CONST_METHOD0(SendingMedia, bool());
+  MOCK_METHOD1(SetAsPartOfAllocation, void(bool));
   MOCK_CONST_METHOD4(BitrateSent,
                      void(uint32_t* total_rate,
                           uint32_t* video_rate,
@@ -146,7 +149,6 @@
                        uint32_t name,
                        const uint8_t* data,
                        uint16_t length));
-  MOCK_METHOD1(SetRTCPVoIPMetrics, int32_t(const RTCPVoIPMetric* voip_metric));
   MOCK_METHOD1(SetRtcpXrRrtrStatus, void(bool enable));
   MOCK_CONST_METHOD0(RtcpXrRrtrStatus, bool());
   MOCK_METHOD2(SetRemb, void(int64_t bitrate, std::vector<uint32_t> ssrcs));
diff --git a/modules/rtp_rtcp/source/contributing_sources.h b/modules/rtp_rtcp/source/contributing_sources.h
index 1a4a572..b6201ce 100644
--- a/modules/rtp_rtcp/source/contributing_sources.h
+++ b/modules/rtp_rtcp/source/contributing_sources.h
@@ -18,7 +18,8 @@
 
 #include "absl/types/optional.h"
 #include "api/array_view.h"
-#include "api/rtpreceiverinterface.h"
+#include "api/rtpreceiverinterface.h"  // For RtpSource
+#include "rtc_base/timeutils.h"        // For kNumMillisecsPerSec
 
 namespace webrtc {
 
diff --git a/modules/rtp_rtcp/source/flexfec_sender.cc b/modules/rtp_rtcp/source/flexfec_sender.cc
index 9704096..286f47c 100644
--- a/modules/rtp_rtcp/source/flexfec_sender.cc
+++ b/modules/rtp_rtcp/source/flexfec_sender.cc
@@ -90,7 +90,7 @@
       rtp_header_extension_map_(
           RegisterSupportedExtensions(rtp_header_extensions)),
       header_extensions_size_(
-          rtp_header_extension_map_.GetTotalLengthInBytes(extension_sizes)) {
+          RtpHeaderExtensionSize(extension_sizes, rtp_header_extension_map_)) {
   // This object should not have been instantiated if FlexFEC is disabled.
   RTC_DCHECK_GE(payload_type, 0);
   RTC_DCHECK_LE(payload_type, 127);
diff --git a/modules/rtp_rtcp/source/receive_statistics_impl.cc b/modules/rtp_rtcp/source/receive_statistics_impl.cc
index 80b8c9b..abe028d 100644
--- a/modules/rtp_rtcp/source/receive_statistics_impl.cc
+++ b/modules/rtp_rtcp/source/receive_statistics_impl.cc
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
 #include "modules/rtp_rtcp/source/time_util.h"
 #include "rtc_base/logging.h"
@@ -372,6 +373,12 @@
   }
 }
 
+void ReceiveStatisticsImpl::OnRtpPacket(const RtpPacketReceived& packet) {
+  RTPHeader header;
+  packet.GetHeader(&header);
+  IncomingPacket(header, packet.size());
+}
+
 void ReceiveStatisticsImpl::IncomingPacket(const RTPHeader& header,
                                            size_t packet_length) {
   StreamStatisticianImpl* impl;
@@ -394,18 +401,19 @@
   impl->IncomingPacket(header, packet_length);
 }
 
-void ReceiveStatisticsImpl::FecPacketReceived(const RTPHeader& header,
-                                              size_t packet_length) {
+void ReceiveStatisticsImpl::FecPacketReceived(const RtpPacketReceived& packet) {
   StreamStatisticianImpl* impl;
   {
     rtc::CritScope cs(&receive_statistics_lock_);
-    auto it = statisticians_.find(header.ssrc);
+    auto it = statisticians_.find(packet.Ssrc());
     // Ignore FEC if it is the first packet.
     if (it == statisticians_.end())
       return;
     impl = it->second;
   }
-  impl->FecPacketReceived(header, packet_length);
+  RTPHeader header;
+  packet.GetHeader(&header);
+  impl->FecPacketReceived(header, packet.size());
 }
 
 StreamStatistician* ReceiveStatisticsImpl::GetStatistician(
diff --git a/modules/rtp_rtcp/source/receive_statistics_impl.h b/modules/rtp_rtcp/source/receive_statistics_impl.h
index f94256a..56bfd2b 100644
--- a/modules/rtp_rtcp/source/receive_statistics_impl.h
+++ b/modules/rtp_rtcp/source/receive_statistics_impl.h
@@ -105,10 +105,12 @@
   // Implement ReceiveStatisticsProvider.
   std::vector<rtcp::ReportBlock> RtcpReportBlocks(size_t max_blocks) override;
 
+  // Implement RtpPacketSinkInterface
+  void OnRtpPacket(const RtpPacketReceived& packet) override;
+
   // Implement ReceiveStatistics.
   void IncomingPacket(const RTPHeader& header, size_t packet_length) override;
-  void FecPacketReceived(const RTPHeader& header,
-                         size_t packet_length) override;
+  void FecPacketReceived(const RtpPacketReceived& packet) override;
   StreamStatistician* GetStatistician(uint32_t ssrc) const override;
   void SetMaxReorderingThreshold(int max_reordering_threshold) override;
   void EnableRetransmitDetection(uint32_t ssrc, bool enable) override;
diff --git a/modules/rtp_rtcp/source/receive_statistics_unittest.cc b/modules/rtp_rtcp/source/receive_statistics_unittest.cc
index b721b03..578d81f 100644
--- a/modules/rtp_rtcp/source/receive_statistics_unittest.cc
+++ b/modules/rtp_rtcp/source/receive_statistics_unittest.cc
@@ -12,6 +12,8 @@
 #include <vector>
 
 #include "modules/rtp_rtcp/include/receive_statistics.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
+#include "rtc_base/random.h"
 #include "system_wrappers/include/clock.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
@@ -29,40 +31,68 @@
 const uint32_t kSsrc3 = 203;
 const uint32_t kSsrc4 = 304;
 
-RTPHeader CreateRtpHeader(uint32_t ssrc) {
-  RTPHeader header;
-  memset(&header, 0, sizeof(header));
-  header.ssrc = ssrc;
-  header.sequenceNumber = 100;
-  header.payload_type_frequency = 90000;
-  return header;
+RtpPacketReceived CreateRtpPacket(uint32_t ssrc,
+                                  size_t header_size,
+                                  size_t payload_size,
+                                  size_t padding_size) {
+  RtpPacketReceived packet;
+  packet.SetSsrc(ssrc);
+  packet.SetSequenceNumber(100);
+  packet.set_payload_type_frequency(90000);
+  RTC_CHECK_GE(header_size, 12);
+  RTC_CHECK_EQ(header_size % 4, 0);
+  if (header_size > 12) {
+    // Insert csrcs to increase header size.
+    const int num_csrcs = (header_size - 12) / 4;
+    std::vector<uint32_t> csrcs(num_csrcs);
+    packet.SetCsrcs(csrcs);
+  }
+  packet.SetPayloadSize(payload_size);
+  packet.SetPadding(padding_size);
+  return packet;
+}
+
+RtpPacketReceived CreateRtpPacket(uint32_t ssrc, size_t packet_size) {
+  return CreateRtpPacket(ssrc, 12, packet_size - 12, 0);
+}
+
+void IncrementSequenceNumber(RtpPacketReceived* packet, uint16_t incr) {
+  packet->SetSequenceNumber(packet->SequenceNumber() + incr);
+}
+
+void IncrementSequenceNumber(RtpPacketReceived* packet) {
+  IncrementSequenceNumber(packet, 1);
+}
+
+void IncrementTimestamp(RtpPacketReceived* packet, uint32_t incr) {
+  packet->SetTimestamp(packet->Timestamp() + incr);
 }
 
 class ReceiveStatisticsTest : public ::testing::Test {
  public:
   ReceiveStatisticsTest()
       : clock_(0), receive_statistics_(ReceiveStatistics::Create(&clock_)) {
-    header1_ = CreateRtpHeader(kSsrc1);
-    header2_ = CreateRtpHeader(kSsrc2);
+    packet1_ = CreateRtpPacket(kSsrc1, kPacketSize1);
+    packet2_ = CreateRtpPacket(kSsrc2, kPacketSize2);
   }
 
  protected:
   SimulatedClock clock_;
   std::unique_ptr<ReceiveStatistics> receive_statistics_;
-  RTPHeader header1_;
-  RTPHeader header2_;
+  RtpPacketReceived packet1_;
+  RtpPacketReceived packet2_;
 };
 
 TEST_F(ReceiveStatisticsTest, TwoIncomingSsrcs) {
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
-  receive_statistics_->IncomingPacket(header2_, kPacketSize2);
-  ++header2_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
+  receive_statistics_->OnRtpPacket(packet2_);
+  IncrementSequenceNumber(&packet2_);
   clock_.AdvanceTimeMilliseconds(100);
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
-  receive_statistics_->IncomingPacket(header2_, kPacketSize2);
-  ++header2_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
+  receive_statistics_->OnRtpPacket(packet2_);
+  IncrementSequenceNumber(&packet2_);
 
   StreamStatistician* statistician =
       receive_statistics_->GetStatistician(kSsrc1);
@@ -84,10 +114,10 @@
   EXPECT_EQ(2u, receive_statistics_->RtcpReportBlocks(3).size());
   // Add more incoming packets and verify that they are registered in both
   // access methods.
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
-  receive_statistics_->IncomingPacket(header2_, kPacketSize2);
-  ++header2_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
+  receive_statistics_->OnRtpPacket(packet2_);
+  IncrementSequenceNumber(&packet2_);
 
   receive_statistics_->GetStatistician(kSsrc1)->GetDataCounters(
       &bytes_received, &packets_received);
@@ -101,12 +131,12 @@
 
 TEST_F(ReceiveStatisticsTest,
        RtcpReportBlocksReturnsMaxBlocksWhenThereAreMoreStatisticians) {
-  RTPHeader header1 = CreateRtpHeader(kSsrc1);
-  RTPHeader header2 = CreateRtpHeader(kSsrc2);
-  RTPHeader header3 = CreateRtpHeader(kSsrc3);
-  receive_statistics_->IncomingPacket(header1, kPacketSize1);
-  receive_statistics_->IncomingPacket(header2, kPacketSize1);
-  receive_statistics_->IncomingPacket(header3, kPacketSize1);
+  RtpPacketReceived packet1 = CreateRtpPacket(kSsrc1, kPacketSize1);
+  RtpPacketReceived packet2 = CreateRtpPacket(kSsrc2, kPacketSize1);
+  RtpPacketReceived packet3 = CreateRtpPacket(kSsrc3, kPacketSize1);
+  receive_statistics_->OnRtpPacket(packet1);
+  receive_statistics_->OnRtpPacket(packet2);
+  receive_statistics_->OnRtpPacket(packet3);
 
   EXPECT_THAT(receive_statistics_->RtcpReportBlocks(2), SizeIs(2));
   EXPECT_THAT(receive_statistics_->RtcpReportBlocks(2), SizeIs(2));
@@ -115,14 +145,14 @@
 
 TEST_F(ReceiveStatisticsTest,
        RtcpReportBlocksReturnsAllObservedSsrcsWithMultipleCalls) {
-  RTPHeader header1 = CreateRtpHeader(kSsrc1);
-  RTPHeader header2 = CreateRtpHeader(kSsrc2);
-  RTPHeader header3 = CreateRtpHeader(kSsrc3);
-  RTPHeader header4 = CreateRtpHeader(kSsrc4);
-  receive_statistics_->IncomingPacket(header1, kPacketSize1);
-  receive_statistics_->IncomingPacket(header2, kPacketSize1);
-  receive_statistics_->IncomingPacket(header3, kPacketSize1);
-  receive_statistics_->IncomingPacket(header4, kPacketSize1);
+  RtpPacketReceived packet1 = CreateRtpPacket(kSsrc1, kPacketSize1);
+  RtpPacketReceived packet2 = CreateRtpPacket(kSsrc2, kPacketSize1);
+  RtpPacketReceived packet3 = CreateRtpPacket(kSsrc3, kPacketSize1);
+  RtpPacketReceived packet4 = CreateRtpPacket(kSsrc4, kPacketSize1);
+  receive_statistics_->OnRtpPacket(packet1);
+  receive_statistics_->OnRtpPacket(packet2);
+  receive_statistics_->OnRtpPacket(packet3);
+  receive_statistics_->OnRtpPacket(packet4);
 
   std::vector<uint32_t> observed_ssrcs;
   std::vector<rtcp::ReportBlock> report_blocks =
@@ -141,11 +171,11 @@
 }
 
 TEST_F(ReceiveStatisticsTest, ActiveStatisticians) {
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
   clock_.AdvanceTimeMilliseconds(1000);
-  receive_statistics_->IncomingPacket(header2_, kPacketSize2);
-  ++header2_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet2_);
+  IncrementSequenceNumber(&packet2_);
   // Nothing should time out since only 1000 ms has passed since the first
   // packet came in.
   EXPECT_EQ(2u, receive_statistics_->RtcpReportBlocks(3).size());
@@ -158,8 +188,8 @@
   // kSsrc2 should have timed out.
   EXPECT_EQ(0u, receive_statistics_->RtcpReportBlocks(3).size());
 
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
   // kSsrc1 should be active again and the data counters should have survived.
   EXPECT_EQ(1u, receive_statistics_->RtcpReportBlocks(3).size());
   StreamStatistician* statistician =
@@ -179,12 +209,12 @@
   EXPECT_TRUE(receive_statistics_->GetStatistician(kSsrc1) != nullptr);
   EXPECT_EQ(0u, receive_statistics_->RtcpReportBlocks(3).size());
   // Receive first packet
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
+  receive_statistics_->OnRtpPacket(packet1_);
   EXPECT_EQ(1u, receive_statistics_->RtcpReportBlocks(3).size());
 }
 
 TEST_F(ReceiveStatisticsTest, GetReceiveStreamDataCounters) {
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
+  receive_statistics_->OnRtpPacket(packet1_);
   StreamStatistician* statistician =
       receive_statistics_->GetStatistician(kSsrc1);
   ASSERT_TRUE(statistician != NULL);
@@ -194,7 +224,7 @@
   EXPECT_GT(counters.first_packet_time_ms, -1);
   EXPECT_EQ(1u, counters.transmitted.packets);
 
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
+  receive_statistics_->OnRtpPacket(packet1_);
   statistician->GetReceiveStreamDataCounters(&counters);
   EXPECT_GT(counters.first_packet_time_ms, -1);
   EXPECT_EQ(2u, counters.transmitted.packets);
@@ -225,23 +255,23 @@
   receive_statistics_->EnableRetransmitDetection(kSsrc1, true);
 
   // Add some arbitrary data, with loss and jitter.
-  header1_.sequenceNumber = 1;
+  packet1_.SetSequenceNumber(1);
   clock_.AdvanceTimeMilliseconds(7);
-  header1_.timestamp += 3;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  header1_.sequenceNumber += 2;
+  IncrementTimestamp(&packet1_, 3);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, 2);
   clock_.AdvanceTimeMilliseconds(9);
-  header1_.timestamp += 9;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  --header1_.sequenceNumber;
+  IncrementTimestamp(&packet1_, 9);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, -1);
   clock_.AdvanceTimeMilliseconds(13);
-  header1_.timestamp += 47;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  header1_.sequenceNumber += 3;
+  IncrementTimestamp(&packet1_, 47);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, 3);
   clock_.AdvanceTimeMilliseconds(11);
-  header1_.timestamp += 17;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
+  IncrementTimestamp(&packet1_, 17);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
 
   EXPECT_EQ(0u, callback.num_calls_);
 
@@ -265,23 +295,23 @@
   receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
 
   // Add some more data.
-  header1_.sequenceNumber = 1;
+  packet1_.SetSequenceNumber(1);
   clock_.AdvanceTimeMilliseconds(7);
-  header1_.timestamp += 3;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  header1_.sequenceNumber += 2;
+  IncrementTimestamp(&packet1_, 3);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, 2);
   clock_.AdvanceTimeMilliseconds(9);
-  header1_.timestamp += 9;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  --header1_.sequenceNumber;
+  IncrementTimestamp(&packet1_, 9);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, -1);
   clock_.AdvanceTimeMilliseconds(13);
-  header1_.timestamp += 47;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  header1_.sequenceNumber += 3;
+  IncrementTimestamp(&packet1_, 47);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_, 3);
   clock_.AdvanceTimeMilliseconds(11);
-  header1_.timestamp += 17;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1);
-  ++header1_.sequenceNumber;
+  IncrementTimestamp(&packet1_, 17);
+  receive_statistics_->OnRtpPacket(packet1_);
+  IncrementSequenceNumber(&packet1_);
 
   receive_statistics_->GetStatistician(kSsrc1)->GetStatistics(&statistics,
                                                               true);
@@ -334,9 +364,10 @@
   const size_t kHeaderLength = 20;
   const size_t kPaddingLength = 9;
 
-  // One packet of size kPacketSize1.
-  header1_.headerLength = kHeaderLength;
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1 + kHeaderLength);
+  // One packet with payload size kPacketSize1.
+  RtpPacketReceived packet1 =
+      CreateRtpPacket(kSsrc1, kHeaderLength, kPacketSize1, 0);
+  receive_statistics_->OnRtpPacket(packet1);
   StreamDataCounters expected;
   expected.transmitted.payload_bytes = kPacketSize1;
   expected.transmitted.header_bytes = kHeaderLength;
@@ -349,12 +380,12 @@
   expected.fec.packets = 0;
   callback.Matches(1, kSsrc1, expected);
 
-  ++header1_.sequenceNumber;
-  clock_.AdvanceTimeMilliseconds(5);
-  header1_.paddingLength = 9;
   // Another packet of size kPacketSize1 with 9 bytes padding.
-  receive_statistics_->IncomingPacket(
-      header1_, kPacketSize1 + kHeaderLength + kPaddingLength);
+  RtpPacketReceived packet2 =
+      CreateRtpPacket(kSsrc1, kHeaderLength, kPacketSize1, 9);
+  packet2.SetSequenceNumber(packet1.SequenceNumber() + 1);
+  clock_.AdvanceTimeMilliseconds(5);
+  receive_statistics_->OnRtpPacket(packet2);
   expected.transmitted.payload_bytes = kPacketSize1 * 2;
   expected.transmitted.header_bytes = kHeaderLength * 2;
   expected.transmitted.padding_bytes = kPaddingLength;
@@ -363,8 +394,7 @@
 
   clock_.AdvanceTimeMilliseconds(5);
   // Retransmit last packet.
-  receive_statistics_->IncomingPacket(
-      header1_, kPacketSize1 + kHeaderLength + kPaddingLength);
+  receive_statistics_->OnRtpPacket(packet2);
   expected.transmitted.payload_bytes = kPacketSize1 * 3;
   expected.transmitted.header_bytes = kHeaderLength * 3;
   expected.transmitted.padding_bytes = kPaddingLength * 2;
@@ -375,13 +405,11 @@
   expected.retransmitted.packets = 1;
   callback.Matches(3, kSsrc1, expected);
 
-  header1_.paddingLength = 0;
-  ++header1_.sequenceNumber;
-  clock_.AdvanceTimeMilliseconds(5);
   // One FEC packet.
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1 + kHeaderLength);
-  receive_statistics_->FecPacketReceived(header1_,
-                                         kPacketSize1 + kHeaderLength);
+  packet1.SetSequenceNumber(packet2.SequenceNumber() + 1);
+  clock_.AdvanceTimeMilliseconds(5);
+  receive_statistics_->OnRtpPacket(packet1);
+  receive_statistics_->FecPacketReceived(packet1);
   expected.transmitted.payload_bytes = kPacketSize1 * 4;
   expected.transmitted.header_bytes = kHeaderLength * 4;
   expected.transmitted.packets = 4;
@@ -393,9 +421,9 @@
   receive_statistics_->RegisterRtpStatisticsCallback(NULL);
 
   // New stats, but callback should not be called.
-  ++header1_.sequenceNumber;
+  IncrementSequenceNumber(&packet1);
   clock_.AdvanceTimeMilliseconds(5);
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1 + kHeaderLength);
+  receive_statistics_->OnRtpPacket(packet1);
   callback.Matches(5, kSsrc1, expected);
 }
 
@@ -404,14 +432,13 @@
   receive_statistics_->RegisterRtpStatisticsCallback(&callback);
 
   const uint32_t kHeaderLength = 20;
-  header1_.headerLength = kHeaderLength;
-
+  RtpPacketReceived packet =
+      CreateRtpPacket(kSsrc1, kHeaderLength, kPacketSize1, 0);
   // If first packet is FEC, ignore it.
-  receive_statistics_->FecPacketReceived(header1_,
-                                         kPacketSize1 + kHeaderLength);
+  receive_statistics_->FecPacketReceived(packet);
   EXPECT_EQ(0u, callback.num_calls_);
 
-  receive_statistics_->IncomingPacket(header1_, kPacketSize1 + kHeaderLength);
+  receive_statistics_->OnRtpPacket(packet);
   StreamDataCounters expected;
   expected.transmitted.payload_bytes = kPacketSize1;
   expected.transmitted.header_bytes = kHeaderLength;
@@ -420,8 +447,7 @@
   expected.fec.packets = 0;
   callback.Matches(1, kSsrc1, expected);
 
-  receive_statistics_->FecPacketReceived(header1_,
-                                         kPacketSize1 + kHeaderLength);
+  receive_statistics_->FecPacketReceived(packet);
   expected.fec.payload_bytes = kPacketSize1;
   expected.fec.header_bytes = kHeaderLength;
   expected.fec.packets = 1;
diff --git a/modules/rtp_rtcp/source/rtcp_packet/extended_reports.cc b/modules/rtp_rtcp/source/rtcp_packet/extended_reports.cc
index 06472ef..5513f37 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/extended_reports.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/extended_reports.cc
@@ -56,7 +56,6 @@
   sender_ssrc_ = ByteReader<uint32_t>::ReadBigEndian(packet.payload());
   rrtr_block_.reset();
   dlrr_block_.ClearItems();
-  voip_metric_block_.reset();
   target_bitrate_ = absl::nullopt;
 
   const uint8_t* current_block = packet.payload() + kXrBaseLength;
@@ -81,9 +80,6 @@
       case Dlrr::kBlockType:
         ParseDlrrBlock(current_block, block_length);
         break;
-      case VoipMetric::kBlockType:
-        ParseVoipMetricBlock(current_block, block_length);
-        break;
       case TargetBitrate::kBlockType:
         ParseTargetBitrateBlock(current_block, block_length);
         break;
@@ -114,12 +110,6 @@
   return true;
 }
 
-void ExtendedReports::SetVoipMetric(const VoipMetric& voip_metric) {
-  if (voip_metric_block_)
-    RTC_LOG(LS_WARNING) << "Voip metric already set, overwriting.";
-  voip_metric_block_.emplace(voip_metric);
-}
-
 void ExtendedReports::SetTargetBitrate(const TargetBitrate& bitrate) {
   if (target_bitrate_)
     RTC_LOG(LS_WARNING) << "TargetBitrate already set, overwriting.";
@@ -129,7 +119,7 @@
 
 size_t ExtendedReports::BlockLength() const {
   return kHeaderLength + kXrBaseLength + RrtrLength() + DlrrLength() +
-         VoipMetricLength() + TargetBitrateLength();
+         TargetBitrateLength();
 }
 
 bool ExtendedReports::Create(uint8_t* packet,
@@ -153,10 +143,6 @@
     dlrr_block_.Create(packet + *index);
     *index += dlrr_block_.BlockLength();
   }
-  if (voip_metric_block_) {
-    voip_metric_block_->Create(packet + *index);
-    *index += VoipMetric::kLength;
-  }
   if (target_bitrate_) {
     target_bitrate_->Create(packet + *index);
     *index += target_bitrate_->BlockLength();
@@ -197,22 +183,6 @@
   dlrr_block_.Parse(block, block_length);
 }
 
-void ExtendedReports::ParseVoipMetricBlock(const uint8_t* block,
-                                           uint16_t block_length) {
-  if (block_length != VoipMetric::kBlockLength) {
-    RTC_LOG(LS_WARNING) << "Incorrect voip metric block size " << block_length
-                        << " Should be " << VoipMetric::kBlockLength;
-    return;
-  }
-  if (voip_metric_block_) {
-    RTC_LOG(LS_WARNING)
-        << "Two Voip Metric blocks found in same Extended Report packet";
-    return;
-  }
-  voip_metric_block_.emplace();
-  voip_metric_block_->Parse(block);
-}
-
 void ExtendedReports::ParseTargetBitrateBlock(const uint8_t* block,
                                               uint16_t block_length) {
   target_bitrate_.emplace();
diff --git a/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h b/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h
index 7855502..fd96769 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h
+++ b/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h
@@ -18,7 +18,6 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/dlrr.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/rrtr.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/target_bitrate.h"
-#include "modules/rtp_rtcp/source/rtcp_packet/voip_metric.h"
 
 namespace webrtc {
 namespace rtcp {
@@ -40,15 +39,11 @@
 
   void SetRrtr(const Rrtr& rrtr);
   bool AddDlrrItem(const ReceiveTimeInfo& time_info);
-  void SetVoipMetric(const VoipMetric& voip_metric);
   void SetTargetBitrate(const TargetBitrate& target_bitrate);
 
   uint32_t sender_ssrc() const { return sender_ssrc_; }
   const absl::optional<Rrtr>& rrtr() const { return rrtr_block_; }
   const Dlrr& dlrr() const { return dlrr_block_; }
-  const absl::optional<VoipMetric>& voip_metric() const {
-    return voip_metric_block_;
-  }
   const absl::optional<TargetBitrate>& target_bitrate() const {
     return target_bitrate_;
   }
@@ -65,9 +60,6 @@
 
   size_t RrtrLength() const { return rrtr_block_ ? Rrtr::kLength : 0; }
   size_t DlrrLength() const { return dlrr_block_.BlockLength(); }
-  size_t VoipMetricLength() const {
-    return voip_metric_block_ ? VoipMetric::kLength : 0;
-  }
   size_t TargetBitrateLength() const;
 
   void ParseRrtrBlock(const uint8_t* block, uint16_t block_length);
@@ -78,7 +70,6 @@
   uint32_t sender_ssrc_;
   absl::optional<Rrtr> rrtr_block_;
   Dlrr dlrr_block_;  // Dlrr without items treated same as no dlrr block.
-  absl::optional<VoipMetric> voip_metric_block_;
   absl::optional<TargetBitrate> target_bitrate_;
 };
 }  // namespace rtcp
diff --git a/modules/rtp_rtcp/source/rtcp_packet/extended_reports_unittest.cc b/modules/rtp_rtcp/source/rtcp_packet/extended_reports_unittest.cc
index c37f77e..7052787 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/extended_reports_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/extended_reports_unittest.cc
@@ -23,31 +23,10 @@
 using webrtc::rtcp::ExtendedReports;
 using webrtc::rtcp::ReceiveTimeInfo;
 using webrtc::rtcp::Rrtr;
-using webrtc::rtcp::VoipMetric;
 
 namespace webrtc {
 // Define comparision operators that shouldn't be needed in production,
 // but make testing matches more clear.
-bool operator==(const RTCPVoIPMetric& metric1, const RTCPVoIPMetric& metric2) {
-  return metric1.lossRate == metric2.lossRate &&
-         metric1.discardRate == metric2.discardRate &&
-         metric1.burstDensity == metric2.burstDensity &&
-         metric1.gapDensity == metric2.gapDensity &&
-         metric1.burstDuration == metric2.burstDuration &&
-         metric1.gapDuration == metric2.gapDuration &&
-         metric1.roundTripDelay == metric2.roundTripDelay &&
-         metric1.endSystemDelay == metric2.endSystemDelay &&
-         metric1.signalLevel == metric2.signalLevel &&
-         metric1.noiseLevel == metric2.noiseLevel &&
-         metric1.RERL == metric2.RERL && metric1.Gmin == metric2.Gmin &&
-         metric1.Rfactor == metric2.Rfactor &&
-         metric1.extRfactor == metric2.extRfactor &&
-         metric1.MOSLQ == metric2.MOSLQ && metric1.MOSCQ == metric2.MOSCQ &&
-         metric1.RXconfig == metric2.RXconfig &&
-         metric1.JBnominal == metric2.JBnominal &&
-         metric1.JBmax == metric2.JBmax && metric1.JBabsMax == metric2.JBabsMax;
-}
-
 namespace rtcp {
 bool operator==(const Rrtr& rrtr1, const Rrtr& rrtr2) {
   return rrtr1.ntp() == rrtr2.ntp();
@@ -57,11 +36,6 @@
   return time1.ssrc == time2.ssrc && time1.last_rr == time2.last_rr &&
          time1.delay_since_last_rr == time2.delay_since_last_rr;
 }
-
-bool operator==(const VoipMetric& metric1, const VoipMetric& metric2) {
-  return metric1.ssrc() == metric2.ssrc() &&
-         metric1.voip_metric() == metric2.voip_metric();
-}
 }  // namespace rtcp
 
 namespace {
@@ -106,40 +80,6 @@
   return rrtr;
 }
 
-template <>
-RTCPVoIPMetric RtcpPacketExtendedReportsTest::Rand<RTCPVoIPMetric>() {
-  RTCPVoIPMetric metric;
-  metric.lossRate = Rand<uint8_t>();
-  metric.discardRate = Rand<uint8_t>();
-  metric.burstDensity = Rand<uint8_t>();
-  metric.gapDensity = Rand<uint8_t>();
-  metric.burstDuration = Rand<uint16_t>();
-  metric.gapDuration = Rand<uint16_t>();
-  metric.roundTripDelay = Rand<uint16_t>();
-  metric.endSystemDelay = Rand<uint16_t>();
-  metric.signalLevel = Rand<uint8_t>();
-  metric.noiseLevel = Rand<uint8_t>();
-  metric.RERL = Rand<uint8_t>();
-  metric.Gmin = Rand<uint8_t>();
-  metric.Rfactor = Rand<uint8_t>();
-  metric.extRfactor = Rand<uint8_t>();
-  metric.MOSLQ = Rand<uint8_t>();
-  metric.MOSCQ = Rand<uint8_t>();
-  metric.RXconfig = Rand<uint8_t>();
-  metric.JBnominal = Rand<uint16_t>();
-  metric.JBmax = Rand<uint16_t>();
-  metric.JBabsMax = Rand<uint16_t>();
-  return metric;
-}
-
-template <>
-VoipMetric RtcpPacketExtendedReportsTest::Rand<VoipMetric>() {
-  VoipMetric voip_metric;
-  voip_metric.SetMediaSsrc(Rand<uint32_t>());
-  voip_metric.SetVoipMetric(Rand<RTCPVoIPMetric>());
-  return voip_metric;
-}
-
 TEST_F(RtcpPacketExtendedReportsTest, CreateWithoutReportBlocks) {
   ExtendedReports xr;
   xr.SetSenderSsrc(kSenderSsrc);
@@ -156,7 +96,6 @@
   EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
   EXPECT_FALSE(parsed.rrtr());
   EXPECT_FALSE(parsed.dlrr());
-  EXPECT_FALSE(parsed.voip_metric());
 }
 
 TEST_F(RtcpPacketExtendedReportsTest, CreateAndParseWithRrtrBlock) {
@@ -220,33 +159,14 @@
               SizeIs(ExtendedReports::kMaxNumberOfDlrrItems));
 }
 
-TEST_F(RtcpPacketExtendedReportsTest, CreateAndParseWithVoipMetric) {
-  const VoipMetric kVoipMetric = Rand<VoipMetric>();
-
-  ExtendedReports xr;
-  xr.SetSenderSsrc(kSenderSsrc);
-  xr.SetVoipMetric(kVoipMetric);
-
-  rtc::Buffer packet = xr.Build();
-
-  ExtendedReports mparsed;
-  EXPECT_TRUE(test::ParseSinglePacket(packet, &mparsed));
-  const ExtendedReports& parsed = mparsed;
-
-  EXPECT_EQ(kSenderSsrc, parsed.sender_ssrc());
-  EXPECT_EQ(kVoipMetric, parsed.voip_metric());
-}
-
 TEST_F(RtcpPacketExtendedReportsTest, CreateAndParseWithMaximumReportBlocks) {
   const Rrtr kRrtr = Rand<Rrtr>();
-  const VoipMetric kVoipMetric = Rand<VoipMetric>();
 
   ExtendedReports xr;
   xr.SetSenderSsrc(kSenderSsrc);
   xr.SetRrtr(kRrtr);
   for (size_t i = 0; i < ExtendedReports::kMaxNumberOfDlrrItems; ++i)
     xr.AddDlrrItem(Rand<ReceiveTimeInfo>());
-  xr.SetVoipMetric(kVoipMetric);
 
   rtc::Buffer packet = xr.Build();
 
@@ -258,7 +178,6 @@
   EXPECT_EQ(kRrtr, parsed.rrtr());
   EXPECT_THAT(parsed.dlrr().sub_blocks(),
               ElementsAreArray(xr.dlrr().sub_blocks()));
-  EXPECT_EQ(kVoipMetric, parsed.voip_metric());
 }
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_packet/nack.cc b/modules/rtp_rtcp/source/rtcp_packet/nack.cc
index f83e5c0..6a4a0bd 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/nack.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/nack.cc
@@ -46,8 +46,9 @@
 //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 //   |            PID                |             BLP               |
 //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-Nack::Nack() {}
-Nack::~Nack() {}
+Nack::Nack() = default;
+Nack::Nack(const Nack& rhs) = default;
+Nack::~Nack() = default;
 
 bool Nack::Parse(const CommonHeader& packet) {
   RTC_DCHECK_EQ(packet.type(), kPacketType);
diff --git a/modules/rtp_rtcp/source/rtcp_packet/nack.h b/modules/rtp_rtcp/source/rtcp_packet/nack.h
index 0e462f9..9153733 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/nack.h
+++ b/modules/rtp_rtcp/source/rtcp_packet/nack.h
@@ -23,6 +23,7 @@
  public:
   static constexpr uint8_t kFeedbackMessageType = 1;
   Nack();
+  Nack(const Nack&);
   ~Nack() override;
 
   // Parse assumes header is already parsed and validated.
diff --git a/modules/rtp_rtcp/source/rtcp_packet/receiver_report.cc b/modules/rtp_rtcp/source/rtcp_packet/receiver_report.cc
index 5677db2..569a66d 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/receiver_report.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/receiver_report.cc
@@ -35,6 +35,8 @@
 
 ReceiverReport::ReceiverReport() : sender_ssrc_(0) {}
 
+ReceiverReport::ReceiverReport(const ReceiverReport& rhs) = default;
+
 ReceiverReport::~ReceiverReport() = default;
 
 bool ReceiverReport::Parse(const CommonHeader& packet) {
diff --git a/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h b/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h
index 3dc7920..8f143da 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h
+++ b/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h
@@ -26,6 +26,7 @@
   static constexpr size_t kMaxNumberOfReportBlocks = 0x1f;
 
   ReceiverReport();
+  ReceiverReport(const ReceiverReport&);
   ~ReceiverReport() override;
 
   // Parse assumes header is already parsed and validated.
diff --git a/modules/rtp_rtcp/source/rtcp_packet/remb.cc b/modules/rtp_rtcp/source/rtcp_packet/remb.cc
index 2b492af..0240611 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/remb.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/remb.cc
@@ -41,6 +41,8 @@
 
 Remb::Remb() : bitrate_bps_(0) {}
 
+Remb::Remb(const Remb& rhs) = default;
+
 Remb::~Remb() = default;
 
 bool Remb::Parse(const CommonHeader& packet) {
diff --git a/modules/rtp_rtcp/source/rtcp_packet/remb.h b/modules/rtp_rtcp/source/rtcp_packet/remb.h
index 5f4aef8..6570e59 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/remb.h
+++ b/modules/rtp_rtcp/source/rtcp_packet/remb.h
@@ -26,6 +26,7 @@
   static constexpr size_t kMaxNumberOfSsrcs = 0xff;
 
   Remb();
+  Remb(const Remb&);
   ~Remb() override;
 
   // Parse assumes header is already parsed and validated.
diff --git a/modules/rtp_rtcp/source/rtcp_packet/sdes_unittest.cc b/modules/rtp_rtcp/source/rtcp_packet/sdes_unittest.cc
index b903a4e..15a39ef 100644
--- a/modules/rtp_rtcp/source/rtcp_packet/sdes_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_packet/sdes_unittest.cc
@@ -10,6 +10,7 @@
 
 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
 
+#include "rtc_base/strings/string_builder.h"
 #include "test/gtest.h"
 #include "test/rtcp_packet_parser.h"
 
@@ -74,7 +75,7 @@
   Sdes sdes;
   for (size_t i = 0; i < kMaxChunks; ++i) {
     uint32_t ssrc = kSenderSsrc + i;
-    std::ostringstream oss;
+    rtc::StringBuilder oss;
     oss << "cname" << i;
     EXPECT_TRUE(sdes.AddCName(ssrc, oss.str()));
   }
diff --git a/modules/rtp_rtcp/source/rtcp_packet/voip_metric.cc b/modules/rtp_rtcp/source/rtcp_packet/voip_metric.cc
deleted file mode 100644
index b715b3d..0000000
--- a/modules/rtp_rtcp/source/rtcp_packet/voip_metric.cc
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- *  Copyright (c) 2015 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 "modules/rtp_rtcp/source/rtcp_packet/voip_metric.h"
-
-#include "modules/rtp_rtcp/source/byte_io.h"
-#include "rtc_base/checks.h"
-
-namespace webrtc {
-namespace rtcp {
-// VoIP Metrics Report Block (RFC 3611).
-//
-//     0                   1                   2                   3
-//     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  0 |     BT=7      |   reserved    |       block length = 8        |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  4 |                        SSRC of source                         |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-//  8 |   loss rate   | discard rate  | burst density |  gap density  |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 12 |       burst duration          |         gap duration          |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 16 |     round trip delay          |       end system delay        |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 20 | signal level  |  noise level  |     RERL      |     Gmin      |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 24 |   R factor    | ext. R factor |    MOS-LQ     |    MOS-CQ     |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 28 |   RX config   |   reserved    |          JB nominal           |
-//    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-// 32 |          JB maximum           |          JB abs max           |
-// 36 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-VoipMetric::VoipMetric() : ssrc_(0) {
-  memset(&voip_metric_, 0, sizeof(voip_metric_));
-}
-
-void VoipMetric::Parse(const uint8_t* buffer) {
-  RTC_DCHECK(buffer[0] == kBlockType);
-  // reserved = buffer[1];
-  RTC_DCHECK(ByteReader<uint16_t>::ReadBigEndian(&buffer[2]) == kBlockLength);
-  ssrc_ = ByteReader<uint32_t>::ReadBigEndian(&buffer[4]);
-  voip_metric_.lossRate = buffer[8];
-  voip_metric_.discardRate = buffer[9];
-  voip_metric_.burstDensity = buffer[10];
-  voip_metric_.gapDensity = buffer[11];
-  voip_metric_.burstDuration = ByteReader<uint16_t>::ReadBigEndian(&buffer[12]);
-  voip_metric_.gapDuration = ByteReader<uint16_t>::ReadBigEndian(&buffer[14]);
-  voip_metric_.roundTripDelay =
-      ByteReader<uint16_t>::ReadBigEndian(&buffer[16]);
-  voip_metric_.endSystemDelay =
-      ByteReader<uint16_t>::ReadBigEndian(&buffer[18]);
-  voip_metric_.signalLevel = buffer[20];
-  voip_metric_.noiseLevel = buffer[21];
-  voip_metric_.RERL = buffer[22];
-  voip_metric_.Gmin = buffer[23];
-  voip_metric_.Rfactor = buffer[24];
-  voip_metric_.extRfactor = buffer[25];
-  voip_metric_.MOSLQ = buffer[26];
-  voip_metric_.MOSCQ = buffer[27];
-  voip_metric_.RXconfig = buffer[28];
-  // reserved = buffer[29];
-  voip_metric_.JBnominal = ByteReader<uint16_t>::ReadBigEndian(&buffer[30]);
-  voip_metric_.JBmax = ByteReader<uint16_t>::ReadBigEndian(&buffer[32]);
-  voip_metric_.JBabsMax = ByteReader<uint16_t>::ReadBigEndian(&buffer[34]);
-}
-
-void VoipMetric::Create(uint8_t* buffer) const {
-  const uint8_t kReserved = 0;
-  buffer[0] = kBlockType;
-  buffer[1] = kReserved;
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[2], kBlockLength);
-  ByteWriter<uint32_t>::WriteBigEndian(&buffer[4], ssrc_);
-  buffer[8] = voip_metric_.lossRate;
-  buffer[9] = voip_metric_.discardRate;
-  buffer[10] = voip_metric_.burstDensity;
-  buffer[11] = voip_metric_.gapDensity;
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[12], voip_metric_.burstDuration);
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[14], voip_metric_.gapDuration);
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[16],
-                                       voip_metric_.roundTripDelay);
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[18],
-                                       voip_metric_.endSystemDelay);
-  buffer[20] = voip_metric_.signalLevel;
-  buffer[21] = voip_metric_.noiseLevel;
-  buffer[22] = voip_metric_.RERL;
-  buffer[23] = voip_metric_.Gmin;
-  buffer[24] = voip_metric_.Rfactor;
-  buffer[25] = voip_metric_.extRfactor;
-  buffer[26] = voip_metric_.MOSLQ;
-  buffer[27] = voip_metric_.MOSCQ;
-  buffer[28] = voip_metric_.RXconfig;
-  buffer[29] = kReserved;
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[30], voip_metric_.JBnominal);
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[32], voip_metric_.JBmax);
-  ByteWriter<uint16_t>::WriteBigEndian(&buffer[34], voip_metric_.JBabsMax);
-}
-
-}  // namespace rtcp
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_packet/voip_metric.h b/modules/rtp_rtcp/source/rtcp_packet/voip_metric.h
deleted file mode 100644
index c5588a4..0000000
--- a/modules/rtp_rtcp/source/rtcp_packet/voip_metric.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- *  Copyright (c) 2015 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 MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_VOIP_METRIC_H_
-#define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_VOIP_METRIC_H_
-
-#include "modules/include/module_common_types.h"
-
-namespace webrtc {
-namespace rtcp {
-
-class VoipMetric {
- public:
-  static const uint8_t kBlockType = 7;
-  static const uint16_t kBlockLength = 8;
-  static const size_t kLength = 4 * (kBlockLength + 1);  // 36
-  VoipMetric();
-  VoipMetric(const VoipMetric&) = default;
-  ~VoipMetric() {}
-
-  VoipMetric& operator=(const VoipMetric&) = default;
-
-  void Parse(const uint8_t* buffer);
-
-  // Fills buffer with the VoipMetric.
-  // Consumes VoipMetric::kLength bytes.
-  void Create(uint8_t* buffer) const;
-
-  void SetMediaSsrc(uint32_t ssrc) { ssrc_ = ssrc; }
-  void SetVoipMetric(const RTCPVoIPMetric& voip_metric) {
-    voip_metric_ = voip_metric;
-  }
-
-  uint32_t ssrc() const { return ssrc_; }
-  const RTCPVoIPMetric& voip_metric() const { return voip_metric_; }
-
- private:
-  uint32_t ssrc_;
-  RTCPVoIPMetric voip_metric_;
-};
-
-}  // namespace rtcp
-}  // namespace webrtc
-#endif  // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_VOIP_METRIC_H_
diff --git a/modules/rtp_rtcp/source/rtcp_packet/voip_metric_unittest.cc b/modules/rtp_rtcp/source/rtcp_packet/voip_metric_unittest.cc
deleted file mode 100644
index 598b8c6..0000000
--- a/modules/rtp_rtcp/source/rtcp_packet/voip_metric_unittest.cc
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- *  Copyright (c) 2015 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 "modules/rtp_rtcp/source/rtcp_packet/voip_metric.h"
-
-#include "test/gtest.h"
-
-namespace webrtc {
-namespace rtcp {
-namespace {
-
-const uint32_t kRemoteSsrc = 0x23456789;
-const uint8_t kBlock[] = {0x07, 0x00, 0x00, 0x08, 0x23, 0x45, 0x67, 0x89, 0x01,
-                          0x02, 0x03, 0x04, 0x11, 0x12, 0x22, 0x23, 0x33, 0x34,
-                          0x44, 0x45, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
-                          0x0c, 0x0d, 0x00, 0x55, 0x56, 0x66, 0x67, 0x77, 0x78};
-const size_t kBlockSizeBytes = sizeof(kBlock);
-static_assert(
-    kBlockSizeBytes == VoipMetric::kLength,
-    "Size of manually created Voip Metric block should match class constant");
-
-TEST(RtcpPacketVoipMetricTest, Create) {
-  uint8_t buffer[VoipMetric::kLength];
-  RTCPVoIPMetric metric;
-  metric.lossRate = 1;
-  metric.discardRate = 2;
-  metric.burstDensity = 3;
-  metric.gapDensity = 4;
-  metric.burstDuration = 0x1112;
-  metric.gapDuration = 0x2223;
-  metric.roundTripDelay = 0x3334;
-  metric.endSystemDelay = 0x4445;
-  metric.signalLevel = 5;
-  metric.noiseLevel = 6;
-  metric.RERL = 7;
-  metric.Gmin = 8;
-  metric.Rfactor = 9;
-  metric.extRfactor = 10;
-  metric.MOSLQ = 11;
-  metric.MOSCQ = 12;
-  metric.RXconfig = 13;
-  metric.JBnominal = 0x5556;
-  metric.JBmax = 0x6667;
-  metric.JBabsMax = 0x7778;
-  VoipMetric metric_block;
-  metric_block.SetMediaSsrc(kRemoteSsrc);
-  metric_block.SetVoipMetric(metric);
-
-  metric_block.Create(buffer);
-  EXPECT_EQ(0, memcmp(buffer, kBlock, kBlockSizeBytes));
-}
-
-TEST(RtcpPacketVoipMetricTest, Parse) {
-  VoipMetric read_metric;
-  read_metric.Parse(kBlock);
-
-  // Run checks on const object to ensure all accessors have const modifier.
-  const VoipMetric& parsed = read_metric;
-
-  EXPECT_EQ(kRemoteSsrc, parsed.ssrc());
-  EXPECT_EQ(1, parsed.voip_metric().lossRate);
-  EXPECT_EQ(2, parsed.voip_metric().discardRate);
-  EXPECT_EQ(3, parsed.voip_metric().burstDensity);
-  EXPECT_EQ(4, parsed.voip_metric().gapDensity);
-  EXPECT_EQ(0x1112, parsed.voip_metric().burstDuration);
-  EXPECT_EQ(0x2223, parsed.voip_metric().gapDuration);
-  EXPECT_EQ(0x3334, parsed.voip_metric().roundTripDelay);
-  EXPECT_EQ(0x4445, parsed.voip_metric().endSystemDelay);
-  EXPECT_EQ(5, parsed.voip_metric().signalLevel);
-  EXPECT_EQ(6, parsed.voip_metric().noiseLevel);
-  EXPECT_EQ(7, parsed.voip_metric().RERL);
-  EXPECT_EQ(8, parsed.voip_metric().Gmin);
-  EXPECT_EQ(9, parsed.voip_metric().Rfactor);
-  EXPECT_EQ(10, parsed.voip_metric().extRfactor);
-  EXPECT_EQ(11, parsed.voip_metric().MOSLQ);
-  EXPECT_EQ(12, parsed.voip_metric().MOSCQ);
-  EXPECT_EQ(13, parsed.voip_metric().RXconfig);
-  EXPECT_EQ(0x5556, parsed.voip_metric().JBnominal);
-  EXPECT_EQ(0x6667, parsed.voip_metric().JBmax);
-  EXPECT_EQ(0x7778, parsed.voip_metric().JBabsMax);
-}
-
-}  // namespace
-}  // namespace rtcp
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc b/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
index 6cd953c..c7708f0 100644
--- a/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
@@ -689,31 +689,6 @@
   InjectRtcpPacket(xr);
 }
 
-// VOiP reports are ignored.
-TEST_F(RtcpReceiverTest, InjectExtendedReportsVoipPacket) {
-  const uint8_t kLossRate = 123;
-  rtcp::VoipMetric voip_metric;
-  voip_metric.SetMediaSsrc(kReceiverMainSsrc);
-  RTCPVoIPMetric metric;
-  metric.lossRate = kLossRate;
-  voip_metric.SetVoipMetric(metric);
-  rtcp::ExtendedReports xr;
-  xr.SetSenderSsrc(kSenderSsrc);
-  xr.SetVoipMetric(voip_metric);
-
-  InjectRtcpPacket(xr);
-}
-
-TEST_F(RtcpReceiverTest, ExtendedReportsVoipPacketNotToUsIgnored) {
-  rtcp::VoipMetric voip_metric;
-  voip_metric.SetMediaSsrc(kNotToUsSsrc);
-  rtcp::ExtendedReports xr;
-  xr.SetSenderSsrc(kSenderSsrc);
-  xr.SetVoipMetric(voip_metric);
-
-  InjectRtcpPacket(xr);
-}
-
 TEST_F(RtcpReceiverTest, InjectExtendedReportsReceiverReferenceTimePacket) {
   const NtpTime kNtp(0x10203, 0x40506);
   rtcp::Rrtr rrtr;
@@ -792,13 +767,10 @@
   rtcp_receiver_.SetRtcpXrRrtrStatus(true);
 
   rtcp::Rrtr rrtr;
-  rtcp::VoipMetric metric;
-  metric.SetMediaSsrc(kReceiverMainSsrc);
   rtcp::ExtendedReports xr;
   xr.SetSenderSsrc(kSenderSsrc);
   xr.SetRrtr(rrtr);
   xr.AddDlrrItem(ReceiveTimeInfo(kReceiverMainSsrc, 0x12345, 0x67890));
-  xr.SetVoipMetric(metric);
 
   InjectRtcpPacket(xr);
 
@@ -813,13 +785,10 @@
   rtcp_receiver_.SetRtcpXrRrtrStatus(true);
 
   rtcp::Rrtr rrtr;
-  rtcp::VoipMetric metric;
-  metric.SetMediaSsrc(kReceiverMainSsrc);
   rtcp::ExtendedReports xr;
   xr.SetSenderSsrc(kSenderSsrc);
   xr.SetRrtr(rrtr);
   xr.AddDlrrItem(ReceiveTimeInfo(kReceiverMainSsrc, 0x12345, 0x67890));
-  xr.SetVoipMetric(metric);
 
   rtc::Buffer packet = xr.Build();
   // Modify the DLRR block to have an unsupported block type, from 5 to 6.
diff --git a/modules/rtp_rtcp/source/rtcp_sender.cc b/modules/rtp_rtcp/source/rtcp_sender.cc
index 55f545c..bfddc422 100644
--- a/modules/rtp_rtcp/source/rtcp_sender.cc
+++ b/modules/rtp_rtcp/source/rtcp_sender.cc
@@ -44,9 +44,9 @@
 namespace webrtc {
 
 namespace {
-const uint32_t kRtcpAnyExtendedReports =
-    kRtcpXrVoipMetric | kRtcpXrReceiverReferenceTime | kRtcpXrDlrrReportBlock |
-    kRtcpXrTargetBitrate;
+const uint32_t kRtcpAnyExtendedReports = kRtcpXrReceiverReferenceTime |
+                                         kRtcpXrDlrrReportBlock |
+                                         kRtcpXrTargetBitrate;
 }  // namespace
 
 RTCPSender::FeedbackState::FeedbackState()
@@ -150,7 +150,8 @@
       app_length_(0),
 
       xr_send_receiver_reference_time_enabled_(false),
-      packet_type_counter_observer_(packet_type_counter_observer) {
+      packet_type_counter_observer_(packet_type_counter_observer),
+      send_video_bitrate_allocation_(false) {
   RTC_DCHECK(transport_ != nullptr);
 
   builders_[kRtcpSr] = &RTCPSender::BuildSR;
@@ -607,29 +608,20 @@
     xr->AddDlrrItem(rti);
   }
 
-  if (video_bitrate_allocation_) {
+  if (send_video_bitrate_allocation_) {
     rtcp::TargetBitrate target_bitrate;
 
     for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
       for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
-        if (video_bitrate_allocation_->HasBitrate(sl, tl)) {
+        if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
           target_bitrate.AddTargetBitrate(
-              sl, tl, video_bitrate_allocation_->GetBitrate(sl, tl) / 1000);
+              sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
         }
       }
     }
 
     xr->SetTargetBitrate(target_bitrate);
-    video_bitrate_allocation_.reset();
-  }
-
-  if (xr_voip_metric_) {
-    rtcp::VoipMetric voip;
-    voip.SetMediaSsrc(remote_ssrc_);
-    voip.SetVoipMetric(*xr_voip_metric_);
-    xr_voip_metric_.reset();
-
-    xr->SetVoipMetric(voip);
+    send_video_bitrate_allocation_ = false;
   }
 
   return std::move(xr);
@@ -750,7 +742,8 @@
 
   if (generate_report) {
     if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
-        !feedback_state.last_xr_rtis.empty() || video_bitrate_allocation_) {
+        !feedback_state.last_xr_rtis.empty() ||
+        send_video_bitrate_allocation_) {
       SetFlag(kRtcpAnyExtendedReports, true);
     }
 
@@ -841,15 +834,6 @@
   return 0;
 }
 
-// TODO(sprang): Remove support for VoIP metrics? (Not used in receiver.)
-int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
-  rtc::CritScope lock(&critical_section_rtcp_sender_);
-  xr_voip_metric_.emplace(*VoIPMetric);
-
-  SetFlag(kRtcpAnyExtendedReports, true);
-  return 0;
-}
-
 void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
   rtc::CritScope lock(&critical_section_rtcp_sender_);
   xr_send_receiver_reference_time_enabled_ = enable;
@@ -904,10 +888,45 @@
 void RTCPSender::SetVideoBitrateAllocation(
     const VideoBitrateAllocation& bitrate) {
   rtc::CritScope lock(&critical_section_rtcp_sender_);
-  video_bitrate_allocation_.emplace(bitrate);
+  // Check if this allocation is first ever, or has a different set of
+  // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
+  // as soon as possible.
+  absl::optional<VideoBitrateAllocation> new_bitrate =
+      CheckAndUpdateLayerStructure(bitrate);
+  if (new_bitrate) {
+    video_bitrate_allocation_ = *new_bitrate;
+    next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
+  } else {
+    video_bitrate_allocation_ = bitrate;
+  }
+
+  send_video_bitrate_allocation_ = true;
   SetFlag(kRtcpAnyExtendedReports, true);
 }
 
+absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
+    const VideoBitrateAllocation& bitrate) const {
+  absl::optional<VideoBitrateAllocation> updated_bitrate;
+  for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
+    for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
+      if (!updated_bitrate &&
+          (bitrate.HasBitrate(si, ti) !=
+               video_bitrate_allocation_.HasBitrate(si, ti) ||
+           (bitrate.GetBitrate(si, ti) == 0) !=
+               (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
+        updated_bitrate = bitrate;
+      }
+      if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
+          bitrate.GetBitrate(si, ti) == 0) {
+        // Make sure this stream disabling is explicitly signaled.
+        updated_bitrate->SetBitrate(si, ti, 0);
+      }
+    }
+  }
+
+  return updated_bitrate;
+}
+
 bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
   size_t max_packet_size;
   {
diff --git a/modules/rtp_rtcp/source/rtcp_sender.h b/modules/rtp_rtcp/source/rtcp_sender.h
index 133bf68..c9220dd 100644
--- a/modules/rtp_rtcp/source/rtcp_sender.h
+++ b/modules/rtp_rtcp/source/rtcp_sender.h
@@ -124,7 +124,6 @@
                                      uint32_t name,
                                      const uint8_t* data,
                                      uint16_t length);
-  int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
 
   void SendRtcpXrReceiverReferenceTime(bool enable);
 
@@ -235,18 +234,19 @@
   bool xr_send_receiver_reference_time_enabled_
       RTC_GUARDED_BY(critical_section_rtcp_sender_);
 
-  // XR VoIP metric
-  absl::optional<RTCPVoIPMetric> xr_voip_metric_
-      RTC_GUARDED_BY(critical_section_rtcp_sender_);
-
   RtcpPacketTypeCounterObserver* const packet_type_counter_observer_;
   RtcpPacketTypeCounter packet_type_counter_
       RTC_GUARDED_BY(critical_section_rtcp_sender_);
 
   RtcpNackStats nack_stats_ RTC_GUARDED_BY(critical_section_rtcp_sender_);
 
-  absl::optional<VideoBitrateAllocation> video_bitrate_allocation_
+  VideoBitrateAllocation video_bitrate_allocation_
       RTC_GUARDED_BY(critical_section_rtcp_sender_);
+  bool send_video_bitrate_allocation_
+      RTC_GUARDED_BY(critical_section_rtcp_sender_);
+  absl::optional<VideoBitrateAllocation> CheckAndUpdateLayerStructure(
+      const VideoBitrateAllocation& bitrate) const
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
 
   void SetFlag(uint32_t type, bool is_volatile)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(critical_section_rtcp_sender_);
diff --git a/modules/rtp_rtcp/source/rtcp_sender_unittest.cc b/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
index bccf96d..bcffc81 100644
--- a/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
@@ -14,6 +14,7 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
 #include "modules/rtp_rtcp/source/rtcp_sender.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
 #include "rtc_base/rate_limiter.h"
 #include "test/gmock.h"
@@ -93,13 +94,12 @@
   }
 
   void InsertIncomingPacket(uint32_t remote_ssrc, uint16_t seq_num) {
-    RTPHeader header;
-    header.ssrc = remote_ssrc;
-    header.sequenceNumber = seq_num;
-    header.timestamp = 12345;
-    header.headerLength = 12;
-    size_t kPacketLength = 100;
-    receive_statistics_->IncomingPacket(header, kPacketLength);
+    RtpPacketReceived packet;
+    packet.SetSsrc(remote_ssrc);
+    packet.SetSequenceNumber(seq_num);
+    packet.SetTimestamp(12345);
+    packet.SetPayloadSize(100 - 12);
+    receive_statistics_->OnRtpPacket(packet);
   }
 
   test::RtcpPacketParser* parser() { return &test_transport_.parser_; }
@@ -399,58 +399,6 @@
   EXPECT_EQ(2, parser()->remb()->num_packets());
 }
 
-TEST_F(RtcpSenderTest, SendXrWithVoipMetric) {
-  rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
-  RTCPVoIPMetric metric;
-  metric.lossRate = 1;
-  metric.discardRate = 2;
-  metric.burstDensity = 3;
-  metric.gapDensity = 4;
-  metric.burstDuration = 0x1111;
-  metric.gapDuration = 0x2222;
-  metric.roundTripDelay = 0x3333;
-  metric.endSystemDelay = 0x4444;
-  metric.signalLevel = 5;
-  metric.noiseLevel = 6;
-  metric.RERL = 7;
-  metric.Gmin = 8;
-  metric.Rfactor = 9;
-  metric.extRfactor = 10;
-  metric.MOSLQ = 11;
-  metric.MOSCQ = 12;
-  metric.RXconfig = 13;
-  metric.JBnominal = 0x5555;
-  metric.JBmax = 0x6666;
-  metric.JBabsMax = 0x7777;
-  EXPECT_EQ(0, rtcp_sender_->SetRTCPVoIPMetrics(&metric));
-  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpXrVoipMetric));
-  EXPECT_EQ(1, parser()->xr()->num_packets());
-  EXPECT_EQ(kSenderSsrc, parser()->xr()->sender_ssrc());
-  ASSERT_TRUE(parser()->xr()->voip_metric());
-  EXPECT_EQ(kRemoteSsrc, parser()->xr()->voip_metric()->ssrc());
-  const auto& parsed_metric = parser()->xr()->voip_metric()->voip_metric();
-  EXPECT_EQ(metric.lossRate, parsed_metric.lossRate);
-  EXPECT_EQ(metric.discardRate, parsed_metric.discardRate);
-  EXPECT_EQ(metric.burstDensity, parsed_metric.burstDensity);
-  EXPECT_EQ(metric.gapDensity, parsed_metric.gapDensity);
-  EXPECT_EQ(metric.burstDuration, parsed_metric.burstDuration);
-  EXPECT_EQ(metric.gapDuration, parsed_metric.gapDuration);
-  EXPECT_EQ(metric.roundTripDelay, parsed_metric.roundTripDelay);
-  EXPECT_EQ(metric.endSystemDelay, parsed_metric.endSystemDelay);
-  EXPECT_EQ(metric.signalLevel, parsed_metric.signalLevel);
-  EXPECT_EQ(metric.noiseLevel, parsed_metric.noiseLevel);
-  EXPECT_EQ(metric.RERL, parsed_metric.RERL);
-  EXPECT_EQ(metric.Gmin, parsed_metric.Gmin);
-  EXPECT_EQ(metric.Rfactor, parsed_metric.Rfactor);
-  EXPECT_EQ(metric.extRfactor, parsed_metric.extRfactor);
-  EXPECT_EQ(metric.MOSLQ, parsed_metric.MOSLQ);
-  EXPECT_EQ(metric.MOSCQ, parsed_metric.MOSCQ);
-  EXPECT_EQ(metric.RXconfig, parsed_metric.RXconfig);
-  EXPECT_EQ(metric.JBnominal, parsed_metric.JBnominal);
-  EXPECT_EQ(metric.JBmax, parsed_metric.JBmax);
-  EXPECT_EQ(metric.JBabsMax, parsed_metric.JBabsMax);
-}
-
 TEST_F(RtcpSenderTest, SendXrWithDlrr) {
   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
   RTCPSender::FeedbackState feedback_state = rtp_rtcp_impl_->GetFeedbackState();
@@ -504,7 +452,6 @@
   EXPECT_EQ(1, parser()->xr()->num_packets());
   EXPECT_EQ(kSenderSsrc, parser()->xr()->sender_ssrc());
   EXPECT_FALSE(parser()->xr()->dlrr());
-  EXPECT_FALSE(parser()->xr()->voip_metric());
   ASSERT_TRUE(parser()->xr()->rrtr());
   EXPECT_EQ(ntp, parser()->xr()->rrtr()->ntp());
 }
@@ -658,10 +605,9 @@
   rtcp_sender_->SetTimestampOffset(kStartRtpTimestamp);
   rtcp_sender_->SetLastRtpTime(kRtpTimestamp, clock_.TimeInMilliseconds());
 
-  // Set up XR VoIP metric to be included with BYE
+  // Set up REMB info to be included with BYE.
   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
-  RTCPVoIPMetric metric;
-  EXPECT_EQ(0, rtcp_sender_->SetRTCPVoIPMetrics(&metric));
+  rtcp_sender_->SetRemb(1234, {});
   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpBye));
 }
 
@@ -700,4 +646,76 @@
   }
 }
 
+TEST_F(RtcpSenderTest, SendImmediateXrWithTargetBitrate) {
+  // Initialize. Send a first report right away.
+  rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
+  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
+  clock_.AdvanceTimeMilliseconds(5);
+
+  // Video bitrate allocation generated, save until next time we send a report.
+  VideoBitrateAllocation allocation;
+  allocation.SetBitrate(0, 0, 100000);
+  rtcp_sender_->SetVideoBitrateAllocation(allocation);
+  // First seen instance will be sent immediately.
+  EXPECT_TRUE(rtcp_sender_->TimeToSendRTCPReport(false));
+  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
+  clock_.AdvanceTimeMilliseconds(5);
+
+  // Update bitrate of existing layer, does not quality for immediate sending.
+  allocation.SetBitrate(0, 0, 150000);
+  rtcp_sender_->SetVideoBitrateAllocation(allocation);
+  EXPECT_FALSE(rtcp_sender_->TimeToSendRTCPReport(false));
+
+  // A new spatial layer enabled, signal this as soon as possible.
+  allocation.SetBitrate(1, 0, 200000);
+  rtcp_sender_->SetVideoBitrateAllocation(allocation);
+  EXPECT_TRUE(rtcp_sender_->TimeToSendRTCPReport(false));
+  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
+  clock_.AdvanceTimeMilliseconds(5);
+
+  // Explicitly disable top layer. The same set of layers now has a bitrate
+  // defined, but the explicit 0 indicates shutdown. Signal immediately.
+  allocation.SetBitrate(1, 0, 0);
+  EXPECT_FALSE(rtcp_sender_->TimeToSendRTCPReport(false));
+  rtcp_sender_->SetVideoBitrateAllocation(allocation);
+  EXPECT_TRUE(rtcp_sender_->TimeToSendRTCPReport(false));
+}
+
+TEST_F(RtcpSenderTest, SendTargetBitrateExplicitZeroOnStreamRemoval) {
+  // Set up and send a bitrate allocation with two layers.
+
+  rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
+  VideoBitrateAllocation allocation;
+  allocation.SetBitrate(0, 0, 100000);
+  allocation.SetBitrate(1, 0, 200000);
+  rtcp_sender_->SetVideoBitrateAllocation(allocation);
+  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
+  absl::optional<rtcp::TargetBitrate> target_bitrate =
+      parser()->xr()->target_bitrate();
+  ASSERT_TRUE(target_bitrate);
+  std::vector<rtcp::TargetBitrate::BitrateItem> bitrates =
+      target_bitrate->GetTargetBitrates();
+  ASSERT_EQ(2u, bitrates.size());
+  EXPECT_EQ(bitrates[0].target_bitrate_kbps,
+            allocation.GetBitrate(0, 0) / 1000);
+  EXPECT_EQ(bitrates[1].target_bitrate_kbps,
+            allocation.GetBitrate(1, 0) / 1000);
+
+  // Create a new allocation, where the second stream is no longer available.
+  VideoBitrateAllocation new_allocation;
+  new_allocation.SetBitrate(0, 0, 150000);
+  rtcp_sender_->SetVideoBitrateAllocation(new_allocation);
+  EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
+  target_bitrate = parser()->xr()->target_bitrate();
+  ASSERT_TRUE(target_bitrate);
+  bitrates = target_bitrate->GetTargetBitrates();
+
+  // Two bitrates should still be set, with an explicit entry indicating the
+  // removed stream is gone.
+  ASSERT_EQ(2u, bitrates.size());
+  EXPECT_EQ(bitrates[0].target_bitrate_kbps,
+            new_allocation.GetBitrate(0, 0) / 1000);
+  EXPECT_EQ(bitrates[1].target_bitrate_kbps, 0u);
+}
+
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver.cc b/modules/rtp_rtcp/source/rtcp_transceiver.cc
index 9a17929..77b10ca 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver.cc
@@ -19,45 +19,40 @@
 #include "rtc_base/timeutils.h"
 
 namespace webrtc {
+namespace {
+struct Destructor {
+  void operator()() { rtcp_transceiver = nullptr; }
+  std::unique_ptr<RtcpTransceiverImpl> rtcp_transceiver;
+};
+}  // namespace
 
 RtcpTransceiver::RtcpTransceiver(const RtcpTransceiverConfig& config)
     : task_queue_(config.task_queue),
-      rtcp_transceiver_(absl::make_unique<RtcpTransceiverImpl>(config)),
-      ptr_factory_(rtcp_transceiver_.get()),
-      // Creating first weak ptr can be done on any thread, but is not
-      // thread-safe, thus do it at construction. Creating second (e.g. making a
-      // copy) is thread-safe.
-      ptr_(ptr_factory_.GetWeakPtr()) {
+      rtcp_transceiver_(absl::make_unique<RtcpTransceiverImpl>(config)) {
   RTC_DCHECK(task_queue_);
 }
 
 RtcpTransceiver::~RtcpTransceiver() {
-  if (task_queue_->IsCurrent())
+  if (!rtcp_transceiver_)
     return;
+  task_queue_->PostTask(Destructor{std::move(rtcp_transceiver_)});
+  RTC_DCHECK(!rtcp_transceiver_);
+}
 
-  rtc::Event done(false, false);
-  // TODO(danilchap): Merge cleanup into main closure when task queue does not
-  // silently drop tasks.
-  task_queue_->PostTask(rtc::NewClosure(
-      [this] {
-        // Destructor steps that has to run on the task_queue_.
-        ptr_factory_.InvalidateWeakPtrs();
-        rtcp_transceiver_.reset();
-      },
-      /*cleanup=*/[&done] { done.Set(); }));
-  // Wait until destruction is complete to be sure weak pointers invalidated and
-  // rtcp_transceiver destroyed on the queue while |this| still valid.
-  done.Wait(rtc::Event::kForever);
-  RTC_CHECK(!rtcp_transceiver_) << "Task queue is too busy to handle rtcp";
+void RtcpTransceiver::Stop(std::unique_ptr<rtc::QueuedTask> on_destroyed) {
+  RTC_DCHECK(rtcp_transceiver_);
+  task_queue_->PostTaskAndReply(Destructor{std::move(rtcp_transceiver_)},
+                                std::move(on_destroyed));
+  RTC_DCHECK(!rtcp_transceiver_);
 }
 
 void RtcpTransceiver::AddMediaReceiverRtcpObserver(
     uint32_t remote_ssrc,
     MediaReceiverRtcpObserver* observer) {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   task_queue_->PostTask([ptr, remote_ssrc, observer] {
-    if (ptr)
-      ptr->AddMediaReceiverRtcpObserver(remote_ssrc, observer);
+    ptr->AddMediaReceiverRtcpObserver(remote_ssrc, observer);
   });
 }
 
@@ -65,59 +60,60 @@
     uint32_t remote_ssrc,
     MediaReceiverRtcpObserver* observer,
     std::unique_ptr<rtc::QueuedTask> on_removed) {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   auto remove = [ptr, remote_ssrc, observer] {
-    if (ptr)
-      ptr->RemoveMediaReceiverRtcpObserver(remote_ssrc, observer);
+    ptr->RemoveMediaReceiverRtcpObserver(remote_ssrc, observer);
   };
   task_queue_->PostTaskAndReply(std::move(remove), std::move(on_removed));
 }
 
 void RtcpTransceiver::SetReadyToSend(bool ready) {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   task_queue_->PostTask([ptr, ready] {
-    if (ptr)
       ptr->SetReadyToSend(ready);
   });
 }
 
 void RtcpTransceiver::ReceivePacket(rtc::CopyOnWriteBuffer packet) {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   int64_t now_us = rtc::TimeMicros();
   task_queue_->PostTask([ptr, packet, now_us] {
-    if (ptr)
       ptr->ReceivePacket(packet, now_us);
   });
 }
 
 void RtcpTransceiver::SendCompoundPacket() {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   task_queue_->PostTask([ptr] {
-    if (ptr)
       ptr->SendCompoundPacket();
   });
 }
 
 void RtcpTransceiver::SetRemb(int64_t bitrate_bps,
                               std::vector<uint32_t> ssrcs) {
+  RTC_CHECK(rtcp_transceiver_);
   // TODO(danilchap): Replace with lambda with move capture when available.
   struct SetRembClosure {
     void operator()() {
-      if (ptr)
         ptr->SetRemb(bitrate_bps, std::move(ssrcs));
     }
 
-    rtc::WeakPtr<RtcpTransceiverImpl> ptr;
+    RtcpTransceiverImpl* ptr;
     int64_t bitrate_bps;
     std::vector<uint32_t> ssrcs;
   };
-  task_queue_->PostTask(SetRembClosure{ptr_, bitrate_bps, std::move(ssrcs)});
+  task_queue_->PostTask(
+      SetRembClosure{rtcp_transceiver_.get(), bitrate_bps, std::move(ssrcs)});
 }
 
 void RtcpTransceiver::UnsetRemb() {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
   task_queue_->PostTask([ptr] {
-    if (ptr)
       ptr->UnsetRemb();
   });
 }
@@ -128,54 +124,53 @@
 
 bool RtcpTransceiver::SendFeedbackPacket(
     const rtcp::TransportFeedback& packet) {
+  RTC_CHECK(rtcp_transceiver_);
   struct Closure {
     void operator()() {
-      if (ptr)
         ptr->SendRawPacket(raw_packet);
     }
-    rtc::WeakPtr<RtcpTransceiverImpl> ptr;
+    RtcpTransceiverImpl* ptr;
     rtc::Buffer raw_packet;
   };
-  task_queue_->PostTask(Closure{ptr_, packet.Build()});
+  task_queue_->PostTask(Closure{rtcp_transceiver_.get(), packet.Build()});
   return true;
 }
 
 void RtcpTransceiver::SendNack(uint32_t ssrc,
                                std::vector<uint16_t> sequence_numbers) {
+  RTC_CHECK(rtcp_transceiver_);
   // TODO(danilchap): Replace with lambda with move capture when available.
   struct Closure {
     void operator()() {
-      if (ptr)
         ptr->SendNack(ssrc, std::move(sequence_numbers));
     }
 
-    rtc::WeakPtr<RtcpTransceiverImpl> ptr;
+    RtcpTransceiverImpl* ptr;
     uint32_t ssrc;
     std::vector<uint16_t> sequence_numbers;
   };
-  task_queue_->PostTask(Closure{ptr_, ssrc, std::move(sequence_numbers)});
+  task_queue_->PostTask(
+      Closure{rtcp_transceiver_.get(), ssrc, std::move(sequence_numbers)});
 }
 
 void RtcpTransceiver::SendPictureLossIndication(uint32_t ssrc) {
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr = ptr_;
-  task_queue_->PostTask([ptr, ssrc] {
-    if (ptr)
-      ptr->SendPictureLossIndication(ssrc);
-  });
+  RTC_CHECK(rtcp_transceiver_);
+  RtcpTransceiverImpl* ptr = rtcp_transceiver_.get();
+  task_queue_->PostTask([ptr, ssrc] { ptr->SendPictureLossIndication(ssrc); });
 }
 
 void RtcpTransceiver::SendFullIntraRequest(std::vector<uint32_t> ssrcs) {
+  RTC_CHECK(rtcp_transceiver_);
   // TODO(danilchap): Replace with lambda with move capture when available.
   struct Closure {
     void operator()() {
-      if (ptr)
         ptr->SendFullIntraRequest(ssrcs);
     }
 
-    rtc::WeakPtr<RtcpTransceiverImpl> ptr;
+    RtcpTransceiverImpl* ptr;
     std::vector<uint32_t> ssrcs;
   };
-  task_queue_->PostTask(Closure{ptr_, std::move(ssrcs)});
+  task_queue_->PostTask(Closure{rtcp_transceiver_.get(), std::move(ssrcs)});
 }
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver.h b/modules/rtp_rtcp/source/rtcp_transceiver.h
index 9125781..fc9488c 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver.h
+++ b/modules/rtp_rtcp/source/rtcp_transceiver.h
@@ -17,10 +17,8 @@
 
 #include "modules/rtp_rtcp/source/rtcp_transceiver_config.h"
 #include "modules/rtp_rtcp/source/rtcp_transceiver_impl.h"
-#include "rtc_base/constructormagic.h"
 #include "rtc_base/copyonwritebuffer.h"
 #include "rtc_base/task_queue.h"
-#include "rtc_base/weak_ptr.h"
 
 namespace webrtc {
 //
@@ -30,8 +28,22 @@
 class RtcpTransceiver : public RtcpFeedbackSenderInterface {
  public:
   explicit RtcpTransceiver(const RtcpTransceiverConfig& config);
+  RtcpTransceiver(const RtcpTransceiver&) = delete;
+  RtcpTransceiver& operator=(const RtcpTransceiver&) = delete;
+  // Note that interfaces provided in constructor still might be used after the
+  // destructor. However they can only be used on the confic.task_queue.
+  // Use Stop function to get notified when they are no longer used or
+  // ensure those objects outlive the task queue.
   ~RtcpTransceiver() override;
 
+  // Start asynchronious destruction of the RtcpTransceiver.
+  // It is safe to call destructor right after Stop exits.
+  // No other methods can be called.
+  // Note that interfaces provided in constructor or registered with AddObserver
+  // still might be used by the transceiver on the task queue
+  // until |on_destroyed| runs.
+  void Stop(std::unique_ptr<rtc::QueuedTask> on_destroyed);
+
   // Registers observer to be notified about incoming rtcp packets.
   // Calls to observer will be done on the |config.task_queue|.
   void AddMediaReceiverRtcpObserver(uint32_t remote_ssrc,
@@ -80,14 +92,6 @@
  private:
   rtc::TaskQueue* const task_queue_;
   std::unique_ptr<RtcpTransceiverImpl> rtcp_transceiver_;
-  rtc::WeakPtrFactory<RtcpTransceiverImpl> ptr_factory_;
-  // TaskQueue, and thus tasks posted to it, may outlive this.
-  // Thus when Posting task class always pass copy of the weak_ptr to access
-  // the RtcpTransceiver and never guarantee it still will be alive when task
-  // runs.
-  rtc::WeakPtr<RtcpTransceiverImpl> ptr_;
-
-  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcpTransceiver);
 };
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc b/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
index a4da63a..2c6c3ac 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_impl.cc
@@ -29,6 +29,7 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
 #include "modules/rtp_rtcp/source/time_util.h"
+#include "rtc_base/cancelable_periodic_task.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/task_queue.h"
@@ -87,15 +88,19 @@
 };
 
 RtcpTransceiverImpl::RtcpTransceiverImpl(const RtcpTransceiverConfig& config)
-    : config_(config),
-      ready_to_send_(config.initial_ready_to_send),
-      ptr_factory_(this) {
+    : config_(config), ready_to_send_(config.initial_ready_to_send) {
   RTC_CHECK(config_.Validate());
   if (ready_to_send_ && config_.schedule_periodic_compound_packets)
     SchedulePeriodicCompoundPackets(config_.initial_report_delay_ms);
 }
 
-RtcpTransceiverImpl::~RtcpTransceiverImpl() = default;
+RtcpTransceiverImpl::~RtcpTransceiverImpl() {
+  // If RtcpTransceiverImpl is destroyed off task queue, assume it is destroyed
+  // after TaskQueue. In that case there is no need to Cancel periodic task.
+  if (config_.task_queue == rtc::TaskQueue::Current()) {
+    periodic_task_handle_.Cancel();
+  }
+}
 
 void RtcpTransceiverImpl::AddMediaReceiverRtcpObserver(
     uint32_t remote_ssrc,
@@ -120,8 +125,8 @@
 
 void RtcpTransceiverImpl::SetReadyToSend(bool ready) {
   if (config_.schedule_periodic_compound_packets) {
-    if (ready_to_send_ && !ready)  // Stop existent send task.
-      ptr_factory_.InvalidateWeakPtrs();
+    if (ready_to_send_ && !ready)
+      periodic_task_handle_.Cancel();
 
     if (!ready_to_send_ && ready)  // Restart periodic sending.
       SchedulePeriodicCompoundPackets(config_.report_period_ms / 2);
@@ -318,36 +323,20 @@
 void RtcpTransceiverImpl::ReschedulePeriodicCompoundPackets() {
   if (!config_.schedule_periodic_compound_packets)
     return;
-  // Stop existent send task.
-  ptr_factory_.InvalidateWeakPtrs();
+  periodic_task_handle_.Cancel();
+  RTC_DCHECK(ready_to_send_);
   SchedulePeriodicCompoundPackets(config_.report_period_ms);
 }
 
 void RtcpTransceiverImpl::SchedulePeriodicCompoundPackets(int64_t delay_ms) {
-  class SendPeriodicCompoundPacketTask : public rtc::QueuedTask {
-   public:
-    SendPeriodicCompoundPacketTask(rtc::TaskQueue* task_queue,
-                                   rtc::WeakPtr<RtcpTransceiverImpl> ptr)
-        : task_queue_(task_queue), ptr_(std::move(ptr)) {}
-    bool Run() override {
-      RTC_DCHECK(task_queue_->IsCurrent());
-      if (!ptr_)
-        return true;
-      ptr_->SendPeriodicCompoundPacket();
-      task_queue_->PostDelayedTask(absl::WrapUnique(this),
-                                   ptr_->config_.report_period_ms);
-      return false;
-    }
+  auto task = rtc::CreateCancelablePeriodicTask([this] {
+    RTC_DCHECK(config_.schedule_periodic_compound_packets);
+    RTC_DCHECK(ready_to_send_);
+    SendPeriodicCompoundPacket();
+    return config_.report_period_ms;
+  });
+  periodic_task_handle_ = task->GetCancellationHandle();
 
-   private:
-    rtc::TaskQueue* const task_queue_;
-    const rtc::WeakPtr<RtcpTransceiverImpl> ptr_;
-  };
-
-  RTC_DCHECK(config_.schedule_periodic_compound_packets);
-
-  auto task = absl::make_unique<SendPeriodicCompoundPacketTask>(
-      config_.task_queue, ptr_factory_.GetWeakPtr());
   if (delay_ms > 0)
     config_.task_queue->PostDelayedTask(std::move(task), delay_ms);
   else
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_impl.h b/modules/rtp_rtcp/source/rtcp_transceiver_impl.h
index 7bcd16f..eb9086f 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_impl.h
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_impl.h
@@ -24,8 +24,7 @@
 #include "modules/rtp_rtcp/source/rtcp_packet/report_block.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/target_bitrate.h"
 #include "modules/rtp_rtcp/source/rtcp_transceiver_config.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/weak_ptr.h"
+#include "rtc_base/cancelable_task_handle.h"
 #include "system_wrappers/include/ntp_time.h"
 
 namespace webrtc {
@@ -36,6 +35,8 @@
 class RtcpTransceiverImpl {
  public:
   explicit RtcpTransceiverImpl(const RtcpTransceiverConfig& config);
+  RtcpTransceiverImpl(const RtcpTransceiverImpl&) = delete;
+  RtcpTransceiverImpl& operator=(const RtcpTransceiverImpl&) = delete;
   ~RtcpTransceiverImpl();
 
   void AddMediaReceiverRtcpObserver(uint32_t remote_ssrc,
@@ -95,9 +96,7 @@
   // TODO(danilchap): Remove entries from remote_senders_ that are no longer
   // needed.
   std::map<uint32_t, RemoteSenderState> remote_senders_;
-  rtc::WeakPtrFactory<RtcpTransceiverImpl> ptr_factory_;
-
-  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtcpTransceiverImpl);
+  rtc::CancelableTaskHandle periodic_task_handle_;
 };
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc b/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
index 1675b66..e5db086 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_impl_unittest.cc
@@ -137,6 +137,40 @@
   return config;
 }
 
+TEST(RtcpTransceiverImplTest, CanDestroyOnTaskQueue) {
+  FakeRtcpTransport transport;
+  rtc::TaskQueue queue("rtcp");
+  RtcpTransceiverConfig config = DefaultTestConfig();
+  config.task_queue = &queue;
+  config.schedule_periodic_compound_packets = true;
+  config.outgoing_transport = &transport;
+  auto* rtcp_transceiver = new RtcpTransceiverImpl(config);
+  // Wait for a periodic packet.
+  EXPECT_TRUE(transport.WaitPacket());
+
+  rtc::Event done(false, false);
+  queue.PostTask([rtcp_transceiver, &done] {
+    delete rtcp_transceiver;
+    done.Set();
+  });
+  ASSERT_TRUE(done.Wait(/*milliseconds=*/1000));
+}
+
+TEST(RtcpTransceiverImplTest, CanDestroyAfterTaskQueue) {
+  FakeRtcpTransport transport;
+  auto* queue = new rtc::TaskQueue("rtcp");
+  RtcpTransceiverConfig config = DefaultTestConfig();
+  config.task_queue = queue;
+  config.schedule_periodic_compound_packets = true;
+  config.outgoing_transport = &transport;
+  auto* rtcp_transceiver = new RtcpTransceiverImpl(config);
+  // Wait for a periodic packet.
+  EXPECT_TRUE(transport.WaitPacket());
+
+  delete queue;
+  delete rtcp_transceiver;
+}
+
 TEST(RtcpTransceiverImplTest, DelaysSendingFirstCompondPacket) {
   rtc::TaskQueue queue("rtcp");
   FakeRtcpTransport transport;
@@ -290,7 +324,7 @@
   absl::optional<RtcpTransceiverImpl> rtcp_transceiver;
   rtcp_transceiver.emplace(config);
 
-  rtcp_transceiver->SetReadyToSend(true);
+  queue.PostTask([&] { rtcp_transceiver->SetReadyToSend(true); });
 
   EXPECT_TRUE(transport.WaitPacket());
 
diff --git a/modules/rtp_rtcp/source/rtcp_transceiver_unittest.cc b/modules/rtp_rtcp/source/rtcp_transceiver_unittest.cc
index 1305d9a..2ea5bc9 100644
--- a/modules/rtp_rtcp/source/rtcp_transceiver_unittest.cc
+++ b/modules/rtp_rtcp/source/rtcp_transceiver_unittest.cc
@@ -47,8 +47,8 @@
 }
 
 TEST(RtcpTransceiverTest, SendsRtcpOnTaskQueueWhenCreatedOffTaskQueue) {
-  rtc::TaskQueue queue("rtcp");
   MockTransport outgoing_transport;
+  rtc::TaskQueue queue("rtcp");
   RtcpTransceiverConfig config;
   config.outgoing_transport = &outgoing_transport;
   config.task_queue = &queue;
@@ -64,8 +64,8 @@
 }
 
 TEST(RtcpTransceiverTest, SendsRtcpOnTaskQueueWhenCreatedOnTaskQueue) {
-  rtc::TaskQueue queue("rtcp");
   MockTransport outgoing_transport;
+  rtc::TaskQueue queue("rtcp");
   RtcpTransceiverConfig config;
   config.outgoing_transport = &outgoing_transport;
   config.task_queue = &queue;
@@ -83,15 +83,59 @@
   WaitPostedTasks(&queue);
 }
 
-TEST(RtcpTransceiverTest, CanBeDestoryedOnTaskQueue) {
-  rtc::TaskQueue queue("rtcp");
+TEST(RtcpTransceiverTest, CanBeDestroyedOnTaskQueue) {
   NiceMock<MockTransport> outgoing_transport;
+  rtc::TaskQueue queue("rtcp");
   RtcpTransceiverConfig config;
   config.outgoing_transport = &outgoing_transport;
   config.task_queue = &queue;
   auto rtcp_transceiver = absl::make_unique<RtcpTransceiver>(config);
 
-  queue.PostTask([&] { rtcp_transceiver.reset(); });
+  queue.PostTask([&] {
+    // Insert a packet just before destruction to test for races.
+    rtcp_transceiver->SendCompoundPacket();
+    rtcp_transceiver.reset();
+  });
+  WaitPostedTasks(&queue);
+}
+
+TEST(RtcpTransceiverTest, CanBeDestroyedWithoutBlocking) {
+  rtc::TaskQueue queue("rtcp");
+  NiceMock<MockTransport> outgoing_transport;
+  RtcpTransceiverConfig config;
+  config.outgoing_transport = &outgoing_transport;
+  config.task_queue = &queue;
+  auto* rtcp_transceiver = new RtcpTransceiver(config);
+  rtcp_transceiver->SendCompoundPacket();
+
+  rtc::Event done(false, false);
+  rtc::Event heavy_task(false, false);
+  queue.PostTask([&] {
+    EXPECT_TRUE(heavy_task.Wait(kTimeoutMs));
+    done.Set();
+  });
+  delete rtcp_transceiver;
+
+  heavy_task.Set();
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(RtcpTransceiverTest, MaySendPacketsAfterDestructor) {  // i.e. Be careful!
+  NiceMock<MockTransport> outgoing_transport;  // Must outlive queue below.
+  rtc::TaskQueue queue("rtcp");
+  RtcpTransceiverConfig config;
+  config.outgoing_transport = &outgoing_transport;
+  config.task_queue = &queue;
+  auto* rtcp_transceiver = new RtcpTransceiver(config);
+
+  rtc::Event heavy_task(false, false);
+  queue.PostTask([&] { EXPECT_TRUE(heavy_task.Wait(kTimeoutMs)); });
+  rtcp_transceiver->SendCompoundPacket();
+  delete rtcp_transceiver;
+
+  EXPECT_CALL(outgoing_transport, SendRtcp);
+  heavy_task.Set();
+
   WaitPostedTasks(&queue);
 }
 
@@ -189,26 +233,23 @@
   WaitPostedTasks(&queue);
 }
 
-TEST(RtcpTransceiverTest, DoesntSendPacketsAfterDestruction) {
-  MockTransport outgoing_transport;
+TEST(RtcpTransceiverTest, DoesntSendPacketsAfterStopCallback) {
+  NiceMock<MockTransport> outgoing_transport;
   rtc::TaskQueue queue("rtcp");
   RtcpTransceiverConfig config;
   config.outgoing_transport = &outgoing_transport;
   config.task_queue = &queue;
-  config.schedule_periodic_compound_packets = false;
-
-  EXPECT_CALL(outgoing_transport, SendRtcp(_, _)).Times(0);
+  config.schedule_periodic_compound_packets = true;
 
   auto rtcp_transceiver = absl::make_unique<RtcpTransceiver>(config);
-  rtc::Event pause(false, false);
-  queue.PostTask([&] {
-    pause.Wait(rtc::Event::kForever);
-    rtcp_transceiver.reset();
-  });
+  rtc::Event done(false, false);
   rtcp_transceiver->SendCompoundPacket();
-  pause.Set();
-  WaitPostedTasks(&queue);
-  EXPECT_FALSE(rtcp_transceiver);
+  rtcp_transceiver->Stop(rtc::NewClosure([&] {
+    EXPECT_CALL(outgoing_transport, SendRtcp).Times(0);
+    done.Set();
+  }));
+  rtcp_transceiver = nullptr;
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
 }
 
 TEST(RtcpTransceiverTest, SendsTransportFeedbackOnTaskQueue) {
diff --git a/modules/rtp_rtcp/source/rtp_format.cc b/modules/rtp_rtcp/source/rtp_format.cc
index 72beb17..13ec0af 100644
--- a/modules/rtp_rtcp/source/rtp_format.cc
+++ b/modules/rtp_rtcp/source/rtp_format.cc
@@ -17,6 +17,7 @@
 #include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
 #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
 #include "modules/rtp_rtcp/source/rtp_format_vp9.h"
+#include "rtc_base/checks.h"
 
 namespace webrtc {
 
@@ -30,13 +31,11 @@
     const RTPFragmentationHeader* fragmentation) {
   switch (type) {
     case kVideoCodecH264: {
+      RTC_CHECK(fragmentation);
       const auto& h264 =
           absl::get<RTPVideoHeaderH264>(rtp_video_header.video_type_header);
-      auto packetizer = absl::make_unique<RtpPacketizerH264>(
-          limits.max_payload_len, limits.last_packet_reduction_len,
-          h264.packetization_mode);
-      packetizer->SetPayloadData(payload.data(), payload.size(), fragmentation);
-      return std::move(packetizer);
+      return absl::make_unique<RtpPacketizerH264>(
+          payload, limits, h264.packetization_mode, *fragmentation);
     }
     case kVideoCodecVP8: {
       const auto& vp8 =
@@ -46,21 +45,83 @@
     case kVideoCodecVP9: {
       const auto& vp9 =
           absl::get<RTPVideoHeaderVP9>(rtp_video_header.video_type_header);
-      auto packetizer = absl::make_unique<RtpPacketizerVp9>(
-          vp9, limits.max_payload_len, limits.last_packet_reduction_len);
-      packetizer->SetPayloadData(payload.data(), payload.size(), nullptr);
-      return std::move(packetizer);
+      return absl::make_unique<RtpPacketizerVp9>(payload, limits, vp9);
     }
     default: {
-      auto packetizer = absl::make_unique<RtpPacketizerGeneric>(
-          rtp_video_header, frame_type, limits.max_payload_len,
-          limits.last_packet_reduction_len);
-      packetizer->SetPayloadData(payload.data(), payload.size(), nullptr);
-      return std::move(packetizer);
+      return absl::make_unique<RtpPacketizerGeneric>(
+          payload, limits, rtp_video_header, frame_type);
     }
   }
 }
 
+std::vector<int> RtpPacketizer::SplitAboutEqually(
+    int payload_len,
+    const PayloadSizeLimits& limits) {
+  RTC_DCHECK_GT(payload_len, 0);
+  // First or last packet larger than normal are unsupported.
+  RTC_DCHECK_GE(limits.first_packet_reduction_len, 0);
+  RTC_DCHECK_GE(limits.last_packet_reduction_len, 0);
+
+  std::vector<int> result;
+  if (limits.max_payload_len - limits.first_packet_reduction_len < 1 ||
+      limits.max_payload_len - limits.last_packet_reduction_len < 1) {
+    // Capacity is not enough to put a single byte into one of the packets.
+    return result;
+  }
+  // First and last packet of the frame can be smaller. Pretend that it's
+  // the same size, but we must write more payload to it.
+  // Assume frame fits in single packet if packet has extra space for sum
+  // of first and last packets reductions.
+  int total_bytes = payload_len + limits.first_packet_reduction_len +
+                    limits.last_packet_reduction_len;
+  // Integer divisions with rounding up.
+  int num_packets_left =
+      (total_bytes + limits.max_payload_len - 1) / limits.max_payload_len;
+
+  if (payload_len < num_packets_left) {
+    // Edge case where limits force to have more packets than there are payload
+    // bytes. This may happen when there is single byte of payload that can't be
+    // put into single packet if
+    // first_packet_reduction + last_packet_reduction >= max_payload_len.
+    return result;
+  }
+
+  int bytes_per_packet = total_bytes / num_packets_left;
+  int num_larger_packets = total_bytes % num_packets_left;
+  int remaining_data = payload_len;
+
+  result.reserve(num_packets_left);
+  bool first_packet = true;
+  while (remaining_data > 0) {
+    // Last num_larger_packets are 1 byte wider than the rest. Increase
+    // per-packet payload size when needed.
+    if (num_packets_left == num_larger_packets)
+      ++bytes_per_packet;
+    int current_packet_bytes = bytes_per_packet;
+    if (first_packet) {
+      if (current_packet_bytes > limits.first_packet_reduction_len + 1)
+        current_packet_bytes -= limits.first_packet_reduction_len;
+      else
+        current_packet_bytes = 1;
+    }
+    if (current_packet_bytes > remaining_data) {
+      current_packet_bytes = remaining_data;
+    }
+    // This is not the last packet in the whole payload, but there's no data
+    // left for the last packet. Leave at least one byte for the last packet.
+    if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
+      --current_packet_bytes;
+    }
+    result.push_back(current_packet_bytes);
+
+    remaining_data -= current_packet_bytes;
+    --num_packets_left;
+    first_packet = false;
+  }
+
+  return result;
+}
+
 RtpDepacketizer* RtpDepacketizer::Create(VideoCodecType type) {
   switch (type) {
     case kVideoCodecH264:
diff --git a/modules/rtp_rtcp/source/rtp_format.h b/modules/rtp_rtcp/source/rtp_format.h
index 007ddbc..9fad4cf 100644
--- a/modules/rtp_rtcp/source/rtp_format.h
+++ b/modules/rtp_rtcp/source/rtp_format.h
@@ -13,6 +13,7 @@
 
 #include <memory>
 #include <string>
+#include <vector>
 
 #include "api/array_view.h"
 #include "common_types.h"  // NOLINT(build/include)
@@ -26,8 +27,9 @@
 class RtpPacketizer {
  public:
   struct PayloadSizeLimits {
-    size_t max_payload_len = 1200;
-    size_t last_packet_reduction_len = 0;
+    int max_payload_len = 1200;
+    int first_packet_reduction_len = 0;
+    int last_packet_reduction_len = 0;
   };
   static std::unique_ptr<RtpPacketizer> Create(
       VideoCodecType type,
@@ -47,6 +49,11 @@
   // Write payload and set marker bit of the |packet|.
   // Returns true on success, false otherwise.
   virtual bool NextPacket(RtpPacketToSend* packet) = 0;
+
+  // Split payload_len into sum of integers with respect to |limits|.
+  // Returns empty vector on failure.
+  static std::vector<int> SplitAboutEqually(int payload_len,
+                                            const PayloadSizeLimits& limits);
 };
 
 // TODO(sprang): Update the depacketizer to return a std::unqie_ptr with a copy
diff --git a/modules/rtp_rtcp/source/rtp_format_h264.cc b/modules/rtp_rtcp/source/rtp_format_h264.cc
index 373cce9..9793eba 100644
--- a/modules/rtp_rtcp/source/rtp_format_h264.cc
+++ b/modules/rtp_rtcp/source/rtp_format_h264.cc
@@ -79,39 +79,20 @@
 
 }  // namespace
 
-RtpPacketizerH264::RtpPacketizerH264(size_t max_payload_len,
-                                     size_t last_packet_reduction_len,
-                                     H264PacketizationMode packetization_mode)
-    : max_payload_len_(max_payload_len),
-      last_packet_reduction_len_(last_packet_reduction_len),
-      num_packets_left_(0),
-      packetization_mode_(packetization_mode) {
+RtpPacketizerH264::RtpPacketizerH264(
+    rtc::ArrayView<const uint8_t> payload,
+    PayloadSizeLimits limits,
+    H264PacketizationMode packetization_mode,
+    const RTPFragmentationHeader& fragmentation)
+    : limits_(limits), num_packets_left_(0) {
   // Guard against uninitialized memory in packetization_mode.
   RTC_CHECK(packetization_mode == H264PacketizationMode::NonInterleaved ||
             packetization_mode == H264PacketizationMode::SingleNalUnit);
-  RTC_CHECK_GT(max_payload_len, last_packet_reduction_len);
-}
 
-RtpPacketizerH264::~RtpPacketizerH264() {}
-
-RtpPacketizerH264::Fragment::~Fragment() = default;
-
-RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length)
-    : buffer(buffer), length(length) {}
-RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment)
-    : buffer(fragment.buffer), length(fragment.length) {}
-
-size_t RtpPacketizerH264::SetPayloadData(
-    const uint8_t* payload_data,
-    size_t payload_size,
-    const RTPFragmentationHeader* fragmentation) {
-  RTC_DCHECK(packets_.empty());
-  RTC_DCHECK(input_fragments_.empty());
-  RTC_DCHECK(fragmentation);
-  for (int i = 0; i < fragmentation->fragmentationVectorSize; ++i) {
+  for (int i = 0; i < fragmentation.fragmentationVectorSize; ++i) {
     const uint8_t* buffer =
-        &payload_data[fragmentation->fragmentationOffset[i]];
-    size_t length = fragmentation->fragmentationLength[i];
+        payload.data() + fragmentation.fragmentationOffset[i];
+    size_t length = fragmentation.fragmentationLength[i];
 
     bool updated_sps = false;
     H264::NaluType nalu_type = H264::ParseNaluType(buffer[0]);
@@ -169,7 +150,7 @@
     if (!updated_sps)
       input_fragments_.push_back(Fragment(buffer, length));
   }
-  if (!GeneratePackets()) {
+  if (!GeneratePackets(packetization_mode)) {
     // If failed to generate all the packets, discard already generated
     // packets in case the caller would ignore return value and still try to
     // call NextPacket().
@@ -177,32 +158,42 @@
     while (!packets_.empty()) {
       packets_.pop();
     }
-    return 0;
   }
-  return num_packets_left_;
 }
 
+RtpPacketizerH264::~RtpPacketizerH264() = default;
+
+RtpPacketizerH264::Fragment::~Fragment() = default;
+
+RtpPacketizerH264::Fragment::Fragment(const uint8_t* buffer, size_t length)
+    : buffer(buffer), length(length) {}
+RtpPacketizerH264::Fragment::Fragment(const Fragment& fragment)
+    : buffer(fragment.buffer), length(fragment.length) {}
+
 size_t RtpPacketizerH264::NumPackets() const {
   return num_packets_left_;
 }
 
-bool RtpPacketizerH264::GeneratePackets() {
+bool RtpPacketizerH264::GeneratePackets(
+    H264PacketizationMode packetization_mode) {
   for (size_t i = 0; i < input_fragments_.size();) {
-    switch (packetization_mode_) {
+    switch (packetization_mode) {
       case H264PacketizationMode::SingleNalUnit:
         if (!PacketizeSingleNalu(i))
           return false;
         ++i;
         break;
       case H264PacketizationMode::NonInterleaved:
-        size_t fragment_len = input_fragments_[i].length;
-        if (i + 1 == input_fragments_.size()) {
-          // Pretend that last fragment is larger instead of making last packet
-          // smaller.
-          fragment_len += last_packet_reduction_len_;
-        }
-        if (fragment_len > max_payload_len_) {
-          PacketizeFuA(i);
+        int fragment_len = input_fragments_[i].length;
+        int single_packet_capacity = limits_.max_payload_len;
+        if (i == 0)
+          single_packet_capacity -= limits_.first_packet_reduction_len;
+        if (i + 1 == input_fragments_.size())
+          single_packet_capacity -= limits_.last_packet_reduction_len;
+
+        if (fragment_len > single_packet_capacity) {
+          if (!PacketizeFuA(i))
+            return false;
           ++i;
         } else {
           i = PacketizeStapA(i);
@@ -213,63 +204,47 @@
   return true;
 }
 
-void RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
+bool RtpPacketizerH264::PacketizeFuA(size_t fragment_index) {
   // Fragment payload into packets (FU-A).
-  // Strip out the original header and leave room for the FU-A header.
   const Fragment& fragment = input_fragments_[fragment_index];
-  bool is_last_fragment = fragment_index + 1 == input_fragments_.size();
+
+  PayloadSizeLimits limits = limits_;
+  // Leave room for the FU-A header.
+  limits.max_payload_len -= kFuAHeaderSize;
+  // Ignore first/last packet reductions unless it is first/last fragment.
+  if (fragment_index != 0)
+    limits.first_packet_reduction_len = 0;
+  if (fragment_index != input_fragments_.size() - 1)
+    limits.last_packet_reduction_len = 0;
+
+  // Strip out the original header.
   size_t payload_left = fragment.length - kNalHeaderSize;
-  size_t offset = kNalHeaderSize;
-  size_t per_packet_capacity = max_payload_len_ - kFuAHeaderSize;
+  int offset = kNalHeaderSize;
 
-  // Instead of making the last packet smaller we pretend that all packets are
-  // of the same size but we write additional virtual payload to the last
-  // packet.
-  size_t extra_len = is_last_fragment ? last_packet_reduction_len_ : 0;
+  std::vector<int> payload_sizes = SplitAboutEqually(payload_left, limits);
+  if (payload_sizes.empty())
+    return false;
 
-  // Integer divisions with rounding up. Minimal number of packets to fit all
-  // payload and virtual payload.
-  size_t num_packets = (payload_left + extra_len + (per_packet_capacity - 1)) /
-                       per_packet_capacity;
-  // Bytes per packet. Average rounded down.
-  size_t payload_per_packet = (payload_left + extra_len) / num_packets;
-  // We make several first packets to be 1 bytes smaller than the rest.
-  // i.e 14 bytes splitted in 4 packets would be 3+3+4+4.
-  size_t num_larger_packets = (payload_left + extra_len) % num_packets;
-
-  num_packets_left_ += num_packets;
-  while (payload_left > 0) {
-    // Increase payload per packet at the right time.
-    if (num_packets == num_larger_packets)
-      ++payload_per_packet;
-    size_t packet_length = payload_per_packet;
-    if (payload_left <= packet_length) {  // Last portion of the payload
-      packet_length = payload_left;
-      // One additional packet may be used for extensions in the last packet.
-      // Together with last payload packet there may be at most 2 of them.
-      RTC_DCHECK_LE(num_packets, 2);
-      if (num_packets == 2) {
-        // Whole payload fits in the first num_packets-1 packets but extra
-        // packet is used for virtual payload. Leave at least one byte of data
-        // for the last packet.
-        --packet_length;
-      }
-    }
+  for (size_t i = 0; i < payload_sizes.size(); ++i) {
+    int packet_length = payload_sizes[i];
     RTC_CHECK_GT(packet_length, 0);
     packets_.push(PacketUnit(Fragment(fragment.buffer + offset, packet_length),
-                             offset - kNalHeaderSize == 0,
-                             payload_left == packet_length, false,
-                             fragment.buffer[0]));
+                             /*first_fragment=*/i == 0,
+                             /*last_fragment=*/i == payload_sizes.size() - 1,
+                             false, fragment.buffer[0]));
     offset += packet_length;
     payload_left -= packet_length;
-    --num_packets;
   }
+  num_packets_left_ += payload_sizes.size();
   RTC_CHECK_EQ(0, payload_left);
+  return true;
 }
 
 size_t RtpPacketizerH264::PacketizeStapA(size_t fragment_index) {
   // Aggregate fragments into one packet (STAP-A).
-  size_t payload_size_left = max_payload_len_;
+  size_t payload_size_left = limits_.max_payload_len;
+  if (fragment_index == 0)
+    payload_size_left -= limits_.first_packet_reduction_len;
   int aggregated_fragments = 0;
   size_t fragment_headers_length = 0;
   const Fragment* fragment = &input_fragments_[fragment_index];
@@ -278,7 +253,7 @@
   while (payload_size_left >= fragment->length + fragment_headers_length &&
          (fragment_index + 1 < input_fragments_.size() ||
           payload_size_left >= fragment->length + fragment_headers_length +
-                                   last_packet_reduction_len_)) {
+                                   limits_.last_packet_reduction_len)) {
     RTC_CHECK_GT(fragment->length, 0);
     packets_.push(PacketUnit(*fragment, aggregated_fragments == 0, false, true,
                              fragment->buffer[0]));
@@ -306,16 +281,18 @@
 
 bool RtpPacketizerH264::PacketizeSingleNalu(size_t fragment_index) {
   // Add a single NALU to the queue, no aggregation.
-  size_t payload_size_left = max_payload_len_;
+  size_t payload_size_left = limits_.max_payload_len;
+  if (fragment_index == 0)
+    payload_size_left -= limits_.first_packet_reduction_len;
   if (fragment_index + 1 == input_fragments_.size())
-    payload_size_left -= last_packet_reduction_len_;
+    payload_size_left -= limits_.last_packet_reduction_len;
   const Fragment* fragment = &input_fragments_[fragment_index];
   if (payload_size_left < fragment->length) {
     RTC_LOG(LS_ERROR) << "Failed to fit a fragment to packet in SingleNalu "
                          "packetization mode. Payload size left "
                       << payload_size_left << ", fragment length "
                       << fragment->length << ", packet capacity "
-                      << max_payload_len_;
+                      << limits_.max_payload_len;
     return false;
   }
   RTC_CHECK_GT(fragment->length, 0u);
@@ -340,27 +317,20 @@
     packets_.pop();
     input_fragments_.pop_front();
   } else if (packet.aggregated) {
-    RTC_CHECK(H264PacketizationMode::NonInterleaved == packetization_mode_);
-    bool is_last_packet = num_packets_left_ == 1;
-    NextAggregatePacket(rtp_packet, is_last_packet);
+    NextAggregatePacket(rtp_packet);
   } else {
-    RTC_CHECK(H264PacketizationMode::NonInterleaved == packetization_mode_);
     NextFragmentPacket(rtp_packet);
   }
-  RTC_DCHECK_LE(rtp_packet->payload_size(), max_payload_len_);
-  if (packets_.empty()) {
-    RTC_DCHECK_LE(rtp_packet->payload_size(),
-                  max_payload_len_ - last_packet_reduction_len_);
-  }
   rtp_packet->SetMarker(packets_.empty());
   --num_packets_left_;
   return true;
 }
 
-void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet,
-                                            bool last) {
-  uint8_t* buffer = rtp_packet->AllocatePayload(
-      last ? max_payload_len_ - last_packet_reduction_len_ : max_payload_len_);
+void RtpPacketizerH264::NextAggregatePacket(RtpPacketToSend* rtp_packet) {
+  // Reserve maximum available payload, set actual payload size later.
+  size_t payload_capacity = rtp_packet->FreeCapacity();
+  RTC_CHECK_GE(payload_capacity, kNalHeaderSize);
+  uint8_t* buffer = rtp_packet->AllocatePayload(payload_capacity);
   RTC_DCHECK(buffer);
   PacketUnit* packet = &packets_.front();
   RTC_CHECK(packet->first_fragment);
@@ -370,6 +340,7 @@
   bool is_last_fragment = packet->last_fragment;
   while (packet->aggregated) {
     const Fragment& fragment = packet->source_fragment;
+    RTC_CHECK_LE(index + kLengthFieldSize + fragment.length, payload_capacity);
     // Add NAL unit length field.
     ByteWriter<uint16_t>::WriteBigEndian(&buffer[index], fragment.length);
     index += kLengthFieldSize;
diff --git a/modules/rtp_rtcp/source/rtp_format_h264.h b/modules/rtp_rtcp/source/rtp_format_h264.h
index 1f6702a..73e4087 100644
--- a/modules/rtp_rtcp/source/rtp_format_h264.h
+++ b/modules/rtp_rtcp/source/rtp_format_h264.h
@@ -26,16 +26,13 @@
  public:
   // Initialize with payload from encoder.
   // The payload_data must be exactly one encoded H264 frame.
-  RtpPacketizerH264(size_t max_payload_len,
-                    size_t last_packet_reduction_len,
-                    H264PacketizationMode packetization_mode);
+  RtpPacketizerH264(rtc::ArrayView<const uint8_t> payload,
+                    PayloadSizeLimits limits,
+                    H264PacketizationMode packetization_mode,
+                    const RTPFragmentationHeader& fragmentation);
 
   ~RtpPacketizerH264() override;
 
-  size_t SetPayloadData(const uint8_t* payload_data,
-                        size_t payload_size,
-                        const RTPFragmentationHeader* fragmentation);
-
   size_t NumPackets() const override;
 
   // Get the next payload with H264 payload header.
@@ -80,17 +77,16 @@
     uint8_t header;
   };
 
-  bool GeneratePackets();
-  void PacketizeFuA(size_t fragment_index);
+  bool GeneratePackets(H264PacketizationMode packetization_mode);
+  bool PacketizeFuA(size_t fragment_index);
   size_t PacketizeStapA(size_t fragment_index);
   bool PacketizeSingleNalu(size_t fragment_index);
-  void NextAggregatePacket(RtpPacketToSend* rtp_packet, bool last);
+
+  void NextAggregatePacket(RtpPacketToSend* rtp_packet);
   void NextFragmentPacket(RtpPacketToSend* rtp_packet);
 
-  const size_t max_payload_len_;
-  const size_t last_packet_reduction_len_;
+  const PayloadSizeLimits limits_;
   size_t num_packets_left_;
-  const H264PacketizationMode packetization_mode_;
   std::deque<Fragment> input_fragments_;
   std::queue<PacketUnit> packets_;
 
diff --git a/modules/rtp_rtcp/source/rtp_format_h264_unittest.cc b/modules/rtp_rtcp/source/rtp_format_h264_unittest.cc
index 0183a6a..edf907a 100644
--- a/modules/rtp_rtcp/source/rtp_format_h264_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_format_h264_unittest.cc
@@ -24,17 +24,17 @@
 namespace webrtc {
 namespace {
 
+using ::testing::Each;
+using ::testing::ElementsAre;
 using ::testing::ElementsAreArray;
-
-struct H264ParsedPayload : public RtpDepacketizer::ParsedPayload {
-  RTPVideoHeaderH264& h264() {
-    return absl::get<RTPVideoHeaderH264>(video.video_type_header);
-  }
-};
+using ::testing::Eq;
+using ::testing::IsEmpty;
+using ::testing::SizeIs;
 
 constexpr RtpPacketToSend::ExtensionManager* kNoExtensions = nullptr;
-const size_t kMaxPayloadSize = 1200;
-const size_t kLengthFieldLength = 2;
+constexpr size_t kMaxPayloadSize = 1200;
+constexpr size_t kLengthFieldLength = 2;
+constexpr RtpPacketizer::PayloadSizeLimits kNoLimits;
 
 enum Nalu {
   kSlice = 1,
@@ -55,181 +55,151 @@
 // Bit masks for FU (A and B) headers.
 enum FuDefs { kSBit = 0x80, kEBit = 0x40, kRBit = 0x20 };
 
-void CreateThreeFragments(RTPFragmentationHeader* fragmentation,
-                          size_t frameSize,
-                          size_t payloadOffset) {
-  fragmentation->VerifyAndAllocateFragmentationHeader(3);
-  fragmentation->fragmentationOffset[0] = 0;
-  fragmentation->fragmentationLength[0] = 2;
-  fragmentation->fragmentationOffset[1] = 2;
-  fragmentation->fragmentationLength[1] = 2;
-  fragmentation->fragmentationOffset[2] = 4;
-  fragmentation->fragmentationLength[2] =
-      kNalHeaderSize + frameSize - payloadOffset;
-}
-
-std::unique_ptr<RtpPacketizerH264> CreateH264Packetizer(
-    H264PacketizationMode mode,
-    size_t max_payload_size,
-    size_t last_packet_reduction) {
-  return absl::make_unique<RtpPacketizerH264>(max_payload_size,
-                                              last_packet_reduction, mode);
-}
-
-void VerifyFua(size_t fua_index,
-               const uint8_t* expected_payload,
-               int offset,
-               rtc::ArrayView<const uint8_t> packet,
-               const std::vector<size_t>& expected_sizes) {
-  ASSERT_EQ(expected_sizes[fua_index] + kFuAHeaderSize, packet.size())
-      << "FUA index: " << fua_index;
-  const uint8_t kFuIndicator = 0x1C;  // F=0, NRI=0, Type=28.
-  EXPECT_EQ(kFuIndicator, packet[0]) << "FUA index: " << fua_index;
-  bool should_be_last_fua = (fua_index == expected_sizes.size() - 1);
-  uint8_t fu_header = 0;
-  if (fua_index == 0)
-    fu_header = 0x85;  // S=1, E=0, R=0, Type=5.
-  else if (should_be_last_fua)
-    fu_header = 0x45;  // S=0, E=1, R=0, Type=5.
-  else
-    fu_header = 0x05;  // S=0, E=0, R=0, Type=5.
-  EXPECT_EQ(fu_header, packet[1]) << "FUA index: " << fua_index;
-  std::vector<uint8_t> expected_packet_payload(
-      &expected_payload[offset],
-      &expected_payload[offset + expected_sizes[fua_index]]);
-  EXPECT_THAT(expected_packet_payload,
-              ElementsAreArray(&packet[2], expected_sizes[fua_index]))
-      << "FUA index: " << fua_index;
-}
-
-void TestFua(size_t frame_size,
-             size_t max_payload_size,
-             size_t last_packet_reduction,
-             const std::vector<size_t>& expected_sizes) {
-  std::unique_ptr<uint8_t[]> frame;
-  frame.reset(new uint8_t[frame_size]);
-  frame[0] = 0x05;  // F=0, NRI=0, Type=5.
-  for (size_t i = 0; i < frame_size - kNalHeaderSize; ++i) {
-    frame[i + kNalHeaderSize] = i;
-  }
+RTPFragmentationHeader CreateFragmentation(rtc::ArrayView<const size_t> sizes) {
   RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(1);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = frame_size;
-  std::unique_ptr<RtpPacketizerH264> packetizer(
-      CreateH264Packetizer(H264PacketizationMode::NonInterleaved,
-                           max_payload_size, last_packet_reduction));
-  EXPECT_EQ(
-      expected_sizes.size(),
-      packetizer->SetPayloadData(frame.get(), frame_size, &fragmentation));
+  fragmentation.VerifyAndAllocateFragmentationHeader(sizes.size());
+  size_t offset = 0;
+  for (size_t i = 0; i < sizes.size(); ++i) {
+    fragmentation.fragmentationOffset[i] = offset;
+    fragmentation.fragmentationLength[i] = sizes[i];
+    offset += sizes[i];
+  }
+  return fragmentation;
+}
 
+// Create fragmentation with single fragment of same size as |frame|
+RTPFragmentationHeader NoFragmentation(rtc::ArrayView<const uint8_t> frame) {
+  size_t frame_size[] = {frame.size()};
+  return CreateFragmentation(frame_size);
+}
+
+// Create frame of given size.
+rtc::Buffer CreateFrame(size_t frame_size) {
+  rtc::Buffer frame(frame_size);
+  // Set some valid header.
+  frame[0] = 0x01;
+  // Generate payload to detect when shifted payload was put into a packet.
+  for (size_t i = 1; i < frame_size; ++i)
+    frame[i] = static_cast<uint8_t>(i);
+  return frame;
+}
+
+// Create frame with size deduced from fragmentation.
+rtc::Buffer CreateFrame(const RTPFragmentationHeader& fragmentation) {
+  size_t last_frame_index = fragmentation.fragmentationVectorSize - 1;
+  size_t frame_size = fragmentation.fragmentationOffset[last_frame_index] +
+                      fragmentation.fragmentationLength[last_frame_index];
+  rtc::Buffer frame = CreateFrame(frame_size);
+  // Set some headers.
+  // Tests can expect those are valid but shouln't rely on actual values.
+  for (size_t i = 0; i <= last_frame_index; ++i) {
+    frame[fragmentation.fragmentationOffset[i]] = i + 1;
+  }
+  return frame;
+}
+
+std::vector<RtpPacketToSend> FetchAllPackets(RtpPacketizerH264* packetizer) {
+  std::vector<RtpPacketToSend> result;
+  size_t num_packets = packetizer->NumPackets();
+  result.reserve(num_packets);
   RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(max_payload_size, packet.FreeCapacity());
-  size_t offset = kNalHeaderSize;
-  for (size_t i = 0; i < expected_sizes.size(); ++i) {
-    ASSERT_TRUE(packetizer->NextPacket(&packet));
-    VerifyFua(i, frame.get(), offset, packet.payload(), expected_sizes);
-    offset += expected_sizes[i];
+  while (packetizer->NextPacket(&packet)) {
+    result.push_back(packet);
   }
-
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+  EXPECT_THAT(result, SizeIs(num_packets));
+  return result;
 }
 
-size_t GetExpectedNaluOffset(const RTPFragmentationHeader& fragmentation,
-                             size_t start_index,
-                             size_t nalu_index) {
-  assert(nalu_index < fragmentation.fragmentationVectorSize);
-  size_t expected_nalu_offset = kNalHeaderSize;  // STAP-A header.
-  for (size_t i = start_index; i < nalu_index; ++i) {
-    expected_nalu_offset +=
-        kLengthFieldLength + fragmentation.fragmentationLength[i];
-  }
-  return expected_nalu_offset;
-}
-
-void VerifyStapAPayload(const RTPFragmentationHeader& fragmentation,
-                        size_t first_stapa_index,
-                        size_t nalu_index,
-                        rtc::ArrayView<const uint8_t> frame,
-                        rtc::ArrayView<const uint8_t> packet) {
-  size_t expected_payload_offset =
-      GetExpectedNaluOffset(fragmentation, first_stapa_index, nalu_index) +
-      kLengthFieldLength;
-  size_t offset = fragmentation.fragmentationOffset[nalu_index];
-  const uint8_t* expected_payload = &frame[offset];
-  size_t expected_payload_length =
-      fragmentation.fragmentationLength[nalu_index];
-  ASSERT_LE(offset + expected_payload_length, frame.size());
-  ASSERT_LE(expected_payload_offset + expected_payload_length, packet.size());
-  std::vector<uint8_t> expected_payload_vector(
-      expected_payload, &expected_payload[expected_payload_length]);
-  EXPECT_THAT(expected_payload_vector,
-              ElementsAreArray(&packet[expected_payload_offset],
-                               expected_payload_length));
-}
-
-void VerifySingleNaluPayload(const RTPFragmentationHeader& fragmentation,
-                             size_t nalu_index,
-                             rtc::ArrayView<const uint8_t> frame,
-                             rtc::ArrayView<const uint8_t> packet) {
-  auto fragment = frame.subview(fragmentation.fragmentationOffset[nalu_index],
-                                fragmentation.fragmentationLength[nalu_index]);
-  EXPECT_THAT(packet, ElementsAreArray(fragment.begin(), fragment.end()));
-}
-}  // namespace
-
 // Tests that should work with both packetization mode 0 and
 // packetization mode 1.
 class RtpPacketizerH264ModeTest
     : public ::testing::TestWithParam<H264PacketizationMode> {};
 
-TEST_P(RtpPacketizerH264ModeTest, TestSingleNalu) {
-  const uint8_t frame[2] = {0x05, 0xFF};  // F=0, NRI=0, Type=5.
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(1);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = sizeof(frame);
-  std::unique_ptr<RtpPacketizerH264> packetizer(
-      CreateH264Packetizer(GetParam(), kMaxPayloadSize, 0));
-  ASSERT_EQ(1u,
-            packetizer->SetPayloadData(frame, sizeof(frame), &fragmentation));
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kMaxPayloadSize, packet.FreeCapacity());
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  EXPECT_EQ(2u, packet.payload_size());
-  VerifySingleNaluPayload(fragmentation, 0, frame, packet.payload());
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+TEST_P(RtpPacketizerH264ModeTest, SingleNalu) {
+  const uint8_t frame[2] = {kIdr, 0xFF};
+
+  RtpPacketizerH264 packetizer(frame, kNoLimits, GetParam(),
+                               NoFragmentation(frame));
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(1));
+  EXPECT_THAT(packets[0].payload(), ElementsAreArray(frame));
 }
 
-TEST_P(RtpPacketizerH264ModeTest, TestSingleNaluTwoPackets) {
-  const size_t kFrameSize = kMaxPayloadSize + 100;
-  uint8_t frame[kFrameSize] = {0};
-  for (size_t i = 0; i < kFrameSize; ++i)
-    frame[i] = i;
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(2);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = kMaxPayloadSize;
-  fragmentation.fragmentationOffset[1] = kMaxPayloadSize;
-  fragmentation.fragmentationLength[1] = 100;
-  // Set NAL headers.
-  frame[fragmentation.fragmentationOffset[0]] = 0x01;
-  frame[fragmentation.fragmentationOffset[1]] = 0x01;
+TEST_P(RtpPacketizerH264ModeTest, SingleNaluTwoPackets) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = kMaxPayloadSize;
+  const size_t fragment_sizes[] = {kMaxPayloadSize, 100};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragment_sizes);
+  rtc::Buffer frame = CreateFrame(fragmentation);
 
-  std::unique_ptr<RtpPacketizerH264> packetizer(
-      CreateH264Packetizer(GetParam(), kMaxPayloadSize, 0));
-  ASSERT_EQ(2u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
+  RtpPacketizerH264 packetizer(frame, limits, GetParam(), fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
 
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  ASSERT_EQ(fragmentation.fragmentationOffset[1], packet.payload_size());
-  VerifySingleNaluPayload(fragmentation, 0, frame, packet.payload());
+  ASSERT_THAT(packets, SizeIs(2));
+  EXPECT_THAT(packets[0].payload(),
+              ElementsAreArray(frame.data(), kMaxPayloadSize));
+  EXPECT_THAT(packets[1].payload(),
+              ElementsAreArray(frame.data() + kMaxPayloadSize, 100));
+}
 
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  ASSERT_EQ(fragmentation.fragmentationLength[1], packet.payload_size());
-  VerifySingleNaluPayload(fragmentation, 1, frame, packet.payload());
+TEST_P(RtpPacketizerH264ModeTest,
+       SingleNaluFirstPacketReductionAppliesOnlyToFirstFragment) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 200;
+  limits.first_packet_reduction_len = 5;
+  const size_t fragments[] = {195, 200, 200};
 
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
+
+  RtpPacketizerH264 packetizer(frame, limits, GetParam(), fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(3));
+  const uint8_t* next_fragment = frame.data();
+  EXPECT_THAT(packets[0].payload(), ElementsAreArray(next_fragment, 195));
+  next_fragment += 195;
+  EXPECT_THAT(packets[1].payload(), ElementsAreArray(next_fragment, 200));
+  next_fragment += 200;
+  EXPECT_THAT(packets[2].payload(), ElementsAreArray(next_fragment, 200));
+}
+
+TEST_P(RtpPacketizerH264ModeTest,
+       SingleNaluLastPacketReductionAppliesOnlyToLastFragment) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 200;
+  limits.last_packet_reduction_len = 5;
+  const size_t fragments[] = {200, 200, 195};
+
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
+
+  RtpPacketizerH264 packetizer(frame, limits, GetParam(), fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(3));
+  const uint8_t* next_fragment = frame.data();
+  EXPECT_THAT(packets[0].payload(), ElementsAreArray(next_fragment, 200));
+  next_fragment += 200;
+  EXPECT_THAT(packets[1].payload(), ElementsAreArray(next_fragment, 200));
+  next_fragment += 200;
+  EXPECT_THAT(packets[2].payload(), ElementsAreArray(next_fragment, 195));
+}
+
+TEST_P(RtpPacketizerH264ModeTest,
+       SingleNaluFirstAndLastPacketReductionSumsForSinglePacket) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 200;
+  limits.first_packet_reduction_len = 20;
+  limits.last_packet_reduction_len = 30;
+  rtc::Buffer frame = CreateFrame(150);
+
+  RtpPacketizerH264 packetizer(frame, limits, GetParam(),
+                               NoFragmentation(frame));
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  EXPECT_THAT(packets, SizeIs(1));
 }
 
 INSTANTIATE_TEST_CASE_P(
@@ -238,258 +208,272 @@
     ::testing::Values(H264PacketizationMode::SingleNalUnit,
                       H264PacketizationMode::NonInterleaved));
 
-TEST(RtpPacketizerH264Test, TestStapA) {
-  const size_t kFrameSize =
-      kMaxPayloadSize - 3 * kLengthFieldLength - kNalHeaderSize;
-  uint8_t frame[kFrameSize] = {0x07, 0xFF,  // F=0, NRI=0, Type=7 (SPS).
-                               0x08, 0xFF,  // F=0, NRI=0, Type=8 (PPS).
-                               0x05};       // F=0, NRI=0, Type=5 (IDR).
-  const size_t kPayloadOffset = 5;
-  for (size_t i = 0; i < kFrameSize - kPayloadOffset; ++i)
-    frame[i + kPayloadOffset] = i;
-  RTPFragmentationHeader fragmentation;
-  CreateThreeFragments(&fragmentation, kFrameSize, kPayloadOffset);
-  std::unique_ptr<RtpPacketizerH264> packetizer(CreateH264Packetizer(
-      H264PacketizationMode::NonInterleaved, kMaxPayloadSize, 0));
-  ASSERT_EQ(1u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
+// Aggregation tests.
+TEST(RtpPacketizerH264Test, StapA) {
+  size_t fragments[] = {2, 2, 0x123};
 
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kMaxPayloadSize, packet.FreeCapacity());
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  size_t expected_packet_size =
-      kNalHeaderSize + 3 * kLengthFieldLength + kFrameSize;
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
 
-  for (size_t i = 0; i < fragmentation.fragmentationVectorSize; ++i)
-    VerifyStapAPayload(fragmentation, 0, i, frame, packet.payload());
+  RtpPacketizerH264 packetizer(
+      frame, kNoLimits, H264PacketizationMode::NonInterleaved, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
 
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+  ASSERT_THAT(packets, SizeIs(1));
+  auto payload = packets[0].payload();
+  EXPECT_EQ(payload.size(),
+            kNalHeaderSize + 3 * kLengthFieldLength + frame.size());
+
+  EXPECT_EQ(payload[0], kStapA);
+  payload = payload.subview(kNalHeaderSize);
+  // 1st fragment.
+  EXPECT_THAT(payload.subview(0, kLengthFieldLength),
+              ElementsAre(0, 2));  // Size.
+  EXPECT_THAT(payload.subview(kLengthFieldLength, 2),
+              ElementsAreArray(frame.data(), 2));
+  payload = payload.subview(kLengthFieldLength + 2);
+  // 2nd fragment.
+  EXPECT_THAT(payload.subview(0, kLengthFieldLength),
+              ElementsAre(0, 2));  // Size.
+  EXPECT_THAT(payload.subview(kLengthFieldLength, 2),
+              ElementsAreArray(frame.data() + 2, 2));
+  payload = payload.subview(kLengthFieldLength + 2);
+  // 3rd fragment.
+  EXPECT_THAT(payload.subview(0, kLengthFieldLength),
+              ElementsAre(0x1, 0x23));  // Size.
+  EXPECT_THAT(payload.subview(kLengthFieldLength),
+              ElementsAreArray(frame.data() + 4, 0x123));
 }
 
-TEST(RtpPacketizerH264Test, TestStapARespectsPacketReduction) {
-  const size_t kLastPacketReduction = 100;
-  const size_t kFrameSize = kMaxPayloadSize - 1 - kLastPacketReduction;
-  uint8_t frame[kFrameSize] = {0x07, 0xFF,  // F=0, NRI=0, Type=7.
-                               0x08, 0xFF,  // F=0, NRI=0, Type=8.
-                               0x05};       // F=0, NRI=0, Type=5.
-  const size_t kPayloadOffset = 5;
-  for (size_t i = 0; i < kFrameSize - kPayloadOffset; ++i)
-    frame[i + kPayloadOffset] = i;
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(3);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = 2;
-  fragmentation.fragmentationOffset[1] = 2;
-  fragmentation.fragmentationLength[1] = 2;
-  fragmentation.fragmentationOffset[2] = 4;
-  fragmentation.fragmentationLength[2] =
-      kNalHeaderSize + kFrameSize - kPayloadOffset;
-  std::unique_ptr<RtpPacketizerH264> packetizer(
-      CreateH264Packetizer(H264PacketizationMode::NonInterleaved,
-                           kMaxPayloadSize, kLastPacketReduction));
-  ASSERT_EQ(2u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
+TEST(RtpPacketizerH264Test, SingleNalUnitModeHasNoStapA) {
+  // This is the same setup as for the StapA test.
+  size_t fragments[] = {2, 2, 0x123};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
 
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kMaxPayloadSize, packet.FreeCapacity());
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  size_t expected_packet_size = kNalHeaderSize;
-  for (size_t i = 0; i < 2; ++i) {
-    expected_packet_size +=
-        kLengthFieldLength + fragmentation.fragmentationLength[i];
-  }
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
-  for (size_t i = 0; i < 2; ++i)
-    VerifyStapAPayload(fragmentation, 0, i, frame, packet.payload());
+  RtpPacketizerH264 packetizer(
+      frame, kNoLimits, H264PacketizationMode::SingleNalUnit, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
 
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  expected_packet_size = fragmentation.fragmentationLength[2];
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
-  VerifySingleNaluPayload(fragmentation, 2, frame, packet.payload());
-
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
-}
-
-TEST(RtpPacketizerH264Test, TestSingleNalUnitModeHasNoStapA) {
-  // This is the same setup as for the TestStapA test.
-  const size_t kFrameSize =
-      kMaxPayloadSize - 3 * kLengthFieldLength - kNalHeaderSize;
-  uint8_t frame[kFrameSize] = {0x07, 0xFF,  // F=0, NRI=0, Type=7 (SPS).
-                               0x08, 0xFF,  // F=0, NRI=0, Type=8 (PPS).
-                               0x05};       // F=0, NRI=0, Type=5 (IDR).
-  const size_t kPayloadOffset = 5;
-  for (size_t i = 0; i < kFrameSize - kPayloadOffset; ++i)
-    frame[i + kPayloadOffset] = i;
-  RTPFragmentationHeader fragmentation;
-  CreateThreeFragments(&fragmentation, kFrameSize, kPayloadOffset);
-  std::unique_ptr<RtpPacketizerH264> packetizer(CreateH264Packetizer(
-      H264PacketizationMode::SingleNalUnit, kMaxPayloadSize, 0));
-  packetizer->SetPayloadData(frame, kFrameSize, &fragmentation);
-
-  RtpPacketToSend packet(kNoExtensions);
   // The three fragments should be returned as three packets.
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+  ASSERT_THAT(packets, SizeIs(3));
+  EXPECT_EQ(packets[0].payload_size(), 2u);
+  EXPECT_EQ(packets[1].payload_size(), 2u);
+  EXPECT_EQ(packets[2].payload_size(), 0x123u);
 }
 
-TEST(RtpPacketizerH264Test, TestTooSmallForStapAHeaders) {
-  const size_t kFrameSize = kMaxPayloadSize - 1;
-  uint8_t frame[kFrameSize] = {0x07, 0xFF,  // F=0, NRI=0, Type=7.
-                               0x08, 0xFF,  // F=0, NRI=0, Type=8.
-                               0x05};       // F=0, NRI=0, Type=5.
-  const size_t kPayloadOffset = 5;
-  for (size_t i = 0; i < kFrameSize - kPayloadOffset; ++i)
-    frame[i + kPayloadOffset] = i;
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(3);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = 2;
-  fragmentation.fragmentationOffset[1] = 2;
-  fragmentation.fragmentationLength[1] = 2;
-  fragmentation.fragmentationOffset[2] = 4;
-  fragmentation.fragmentationLength[2] =
-      kNalHeaderSize + kFrameSize - kPayloadOffset;
-  std::unique_ptr<RtpPacketizerH264> packetizer(CreateH264Packetizer(
-      H264PacketizationMode::NonInterleaved, kMaxPayloadSize, 0));
-  ASSERT_EQ(2u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
+TEST(RtpPacketizerH264Test, StapARespectsFirstPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1000;
+  limits.first_packet_reduction_len = 100;
+  const size_t kFirstFragmentSize =
+      limits.max_payload_len - limits.first_packet_reduction_len;
+  size_t fragments[] = {kFirstFragmentSize, 2, 2};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
 
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kMaxPayloadSize, packet.FreeCapacity());
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  size_t expected_packet_size = kNalHeaderSize;
-  for (size_t i = 0; i < 2; ++i) {
-    expected_packet_size +=
-        kLengthFieldLength + fragmentation.fragmentationLength[i];
+  RtpPacketizerH264 packetizer(
+      frame, limits, H264PacketizationMode::NonInterleaved, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(2));
+  // Expect 1st packet is single nalu.
+  EXPECT_THAT(packets[0].payload(),
+              ElementsAreArray(frame.data(), kFirstFragmentSize));
+  // Expect 2nd packet is aggregate of last two fragments.
+  const uint8_t* tail = frame.data() + kFirstFragmentSize;
+  EXPECT_THAT(packets[1].payload(), ElementsAre(kStapA,                  //
+                                                0, 2, tail[0], tail[1],  //
+                                                0, 2, tail[2], tail[3]));
+}
+
+TEST(RtpPacketizerH264Test, StapARespectsLastPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1000;
+  limits.last_packet_reduction_len = 100;
+  const size_t kLastFragmentSize =
+      limits.max_payload_len - limits.last_packet_reduction_len;
+  size_t fragments[] = {2, 2, kLastFragmentSize};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
+
+  RtpPacketizerH264 packetizer(
+      frame, limits, H264PacketizationMode::NonInterleaved, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(2));
+  // Expect 1st packet is aggregate of 1st two fragments.
+  EXPECT_THAT(packets[0].payload(), ElementsAre(kStapA,                    //
+                                                0, 2, frame[0], frame[1],  //
+                                                0, 2, frame[2], frame[3]));
+  // Expect 2nd packet is single nalu.
+  EXPECT_THAT(packets[1].payload(),
+              ElementsAreArray(frame.data() + 4, kLastFragmentSize));
+}
+
+TEST(RtpPacketizerH264Test, TooSmallForStapAHeaders) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1000;
+  const size_t kLastFragmentSize =
+      limits.max_payload_len - 3 * kLengthFieldLength - 4;
+  size_t fragments[] = {2, 2, kLastFragmentSize};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
+
+  RtpPacketizerH264 packetizer(
+      frame, limits, H264PacketizationMode::NonInterleaved, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(2));
+  // Expect 1st packet is aggregate of 1st two fragments.
+  EXPECT_THAT(packets[0].payload(), ElementsAre(kStapA,                    //
+                                                0, 2, frame[0], frame[1],  //
+                                                0, 2, frame[2], frame[3]));
+  // Expect 2nd packet is single nalu.
+  EXPECT_THAT(packets[1].payload(),
+              ElementsAreArray(frame.data() + 4, kLastFragmentSize));
+}
+
+// Fragmentation + aggregation.
+TEST(RtpPacketizerH264Test, MixedStapAFUA) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 100;
+  const size_t kFuaPayloadSize = 70;
+  const size_t kFuaNaluSize = kNalHeaderSize + 2 * kFuaPayloadSize;
+  const size_t kStapANaluSize = 20;
+  size_t fragments[] = {kFuaNaluSize, kStapANaluSize, kStapANaluSize};
+  RTPFragmentationHeader fragmentation = CreateFragmentation(fragments);
+  rtc::Buffer frame = CreateFrame(fragmentation);
+
+  RtpPacketizerH264 packetizer(
+      frame, limits, H264PacketizationMode::NonInterleaved, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  ASSERT_THAT(packets, SizeIs(3));
+  const uint8_t* next_fragment = frame.data() + kNalHeaderSize;
+  // First expect two FU-A packets.
+  EXPECT_THAT(packets[0].payload().subview(0, kFuAHeaderSize),
+              ElementsAre(kFuA, FuDefs::kSBit | frame[0]));
+  EXPECT_THAT(packets[0].payload().subview(kFuAHeaderSize),
+              ElementsAreArray(next_fragment, kFuaPayloadSize));
+  next_fragment += kFuaPayloadSize;
+
+  EXPECT_THAT(packets[1].payload().subview(0, kFuAHeaderSize),
+              ElementsAre(kFuA, FuDefs::kEBit | frame[0]));
+  EXPECT_THAT(packets[1].payload().subview(kFuAHeaderSize),
+              ElementsAreArray(next_fragment, kFuaPayloadSize));
+  next_fragment += kFuaPayloadSize;
+
+  // Then expect one STAP-A packet with two nal units.
+  EXPECT_THAT(packets[2].payload()[0], kStapA);
+  auto payload = packets[2].payload().subview(kNalHeaderSize);
+  EXPECT_THAT(payload.subview(0, kLengthFieldLength),
+              ElementsAre(0, kStapANaluSize));
+  EXPECT_THAT(payload.subview(kLengthFieldLength, kStapANaluSize),
+              ElementsAreArray(next_fragment, kStapANaluSize));
+  payload = payload.subview(kLengthFieldLength + kStapANaluSize);
+  next_fragment += kStapANaluSize;
+  EXPECT_THAT(payload.subview(0, kLengthFieldLength),
+              ElementsAre(0, kStapANaluSize));
+  EXPECT_THAT(payload.subview(kLengthFieldLength),
+              ElementsAreArray(next_fragment, kStapANaluSize));
+}
+
+// Splits frame with payload size |frame_payload_size| without fragmentation,
+// Returns sizes of the payloads excluding fua headers.
+std::vector<int> TestFua(size_t frame_payload_size,
+                         const RtpPacketizer::PayloadSizeLimits& limits) {
+  rtc::Buffer frame = CreateFrame(kNalHeaderSize + frame_payload_size);
+
+  RtpPacketizerH264 packetizer(frame, limits,
+                               H264PacketizationMode::NonInterleaved,
+                               NoFragmentation(frame));
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  RTC_CHECK_GE(packets.size(), 2);  // Single packet indicates it is not FuA.
+  std::vector<uint16_t> fua_header;
+  std::vector<int> payload_sizes;
+
+  for (const RtpPacketToSend& packet : packets) {
+    auto payload = packet.payload();
+    EXPECT_GT(payload.size(), kFuAHeaderSize);
+    fua_header.push_back((payload[0] << 8) | payload[1]);
+    payload_sizes.push_back(payload.size() - kFuAHeaderSize);
   }
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
-  for (size_t i = 0; i < 2; ++i)
-    VerifyStapAPayload(fragmentation, 0, i, frame, packet.payload());
 
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  expected_packet_size = fragmentation.fragmentationLength[2];
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
-  VerifySingleNaluPayload(fragmentation, 2, frame, packet.payload());
+  EXPECT_TRUE(fua_header.front() & FuDefs::kSBit);
+  EXPECT_TRUE(fua_header.back() & FuDefs::kEBit);
+  // Clear S and E bits before testing all are duplicating same original header.
+  fua_header.front() &= ~FuDefs::kSBit;
+  fua_header.back() &= ~FuDefs::kEBit;
+  EXPECT_THAT(fua_header, Each(Eq((kFuA << 8) | frame[0])));
 
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+  return payload_sizes;
 }
 
-TEST(RtpPacketizerH264Test, TestMixedStapA_FUA) {
-  const size_t kFuaNaluSize = 2 * (kMaxPayloadSize - 100);
-  const size_t kStapANaluSize = 100;
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(3);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = kFuaNaluSize;
-  fragmentation.fragmentationOffset[1] = kFuaNaluSize;
-  fragmentation.fragmentationLength[1] = kStapANaluSize;
-  fragmentation.fragmentationOffset[2] = kFuaNaluSize + kStapANaluSize;
-  fragmentation.fragmentationLength[2] = kStapANaluSize;
-  const size_t kFrameSize = kFuaNaluSize + 2 * kStapANaluSize;
-  uint8_t frame[kFrameSize];
-  size_t nalu_offset = 0;
-  for (size_t i = 0; i < fragmentation.fragmentationVectorSize; ++i) {
-    nalu_offset = fragmentation.fragmentationOffset[i];
-    frame[nalu_offset] = 0x05;  // F=0, NRI=0, Type=5.
-    for (size_t j = 1; j < fragmentation.fragmentationLength[i]; ++j) {
-      frame[nalu_offset + j] = i + j;
-    }
-  }
-  std::unique_ptr<RtpPacketizerH264> packetizer(CreateH264Packetizer(
-      H264PacketizationMode::NonInterleaved, kMaxPayloadSize, 0));
-  ASSERT_EQ(3u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
-
-  // First expecting two FU-A packets.
-  std::vector<size_t> fua_sizes;
-  fua_sizes.push_back(1099);
-  fua_sizes.push_back(1100);
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kMaxPayloadSize, packet.FreeCapacity());
-  int fua_offset = kNalHeaderSize;
-  for (size_t i = 0; i < 2; ++i) {
-    ASSERT_TRUE(packetizer->NextPacket(&packet));
-    VerifyFua(i, frame, fua_offset, packet.payload(), fua_sizes);
-    fua_offset += fua_sizes[i];
-  }
-  // Then expecting one STAP-A packet with two nal units.
-  ASSERT_TRUE(packetizer->NextPacket(&packet));
-  size_t expected_packet_size =
-      kNalHeaderSize + 2 * kLengthFieldLength + 2 * kStapANaluSize;
-  ASSERT_EQ(expected_packet_size, packet.payload_size());
-  for (size_t i = 1; i < fragmentation.fragmentationVectorSize; ++i)
-    VerifyStapAPayload(fragmentation, 1, i, frame, packet.payload());
-
-  EXPECT_FALSE(packetizer->NextPacket(&packet));
+// Fragmentation tests.
+TEST(RtpPacketizerH264Test, FUAOddSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1200;
+  EXPECT_THAT(TestFua(1200, limits), ElementsAre(600, 600));
 }
 
-TEST(RtpPacketizerH264Test, TestFUAOddSize) {
-  const size_t kExpectedPayloadSizes[2] = {600, 600};
-  TestFua(
-      kMaxPayloadSize + 1, kMaxPayloadSize, 0,
-      std::vector<size_t>(kExpectedPayloadSizes,
-                          kExpectedPayloadSizes +
-                              sizeof(kExpectedPayloadSizes) / sizeof(size_t)));
+TEST(RtpPacketizerH264Test, FUAWithFirstPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1200;
+  limits.first_packet_reduction_len = 4;
+  EXPECT_THAT(TestFua(1198, limits), ElementsAre(597, 601));
 }
 
-TEST(RtpPacketizerH264Test, TestFUAWithLastPacketReduction) {
-  const size_t kExpectedPayloadSizes[2] = {601, 597};
-  TestFua(
-      kMaxPayloadSize - 1, kMaxPayloadSize, 4,
-      std::vector<size_t>(kExpectedPayloadSizes,
-                          kExpectedPayloadSizes +
-                              sizeof(kExpectedPayloadSizes) / sizeof(size_t)));
+TEST(RtpPacketizerH264Test, FUAWithLastPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1200;
+  limits.last_packet_reduction_len = 4;
+  EXPECT_THAT(TestFua(1198, limits), ElementsAre(601, 597));
 }
 
-TEST(RtpPacketizerH264Test, TestFUAEvenSize) {
-  const size_t kExpectedPayloadSizes[2] = {600, 601};
-  TestFua(
-      kMaxPayloadSize + 2, kMaxPayloadSize, 0,
-      std::vector<size_t>(kExpectedPayloadSizes,
-                          kExpectedPayloadSizes +
-                              sizeof(kExpectedPayloadSizes) / sizeof(size_t)));
+TEST(RtpPacketizerH264Test, FUAWithFirstAndLastPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1199;
+  limits.first_packet_reduction_len = 100;
+  limits.last_packet_reduction_len = 100;
+  EXPECT_THAT(TestFua(1000, limits), ElementsAre(500, 500));
 }
 
-TEST(RtpPacketizerH264Test, TestFUARounding) {
-  const size_t kExpectedPayloadSizes[8] = {1265, 1265, 1265, 1265,
-                                           1265, 1266, 1266, 1266};
-  TestFua(
-      10124, 1448, 0,
-      std::vector<size_t>(kExpectedPayloadSizes,
-                          kExpectedPayloadSizes +
-                              sizeof(kExpectedPayloadSizes) / sizeof(size_t)));
+TEST(RtpPacketizerH264Test, FUAEvenSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1200;
+  EXPECT_THAT(TestFua(1201, limits), ElementsAre(600, 601));
 }
 
-TEST(RtpPacketizerH264Test, TestFUABig) {
-  const size_t kExpectedPayloadSizes[10] = {1198, 1198, 1198, 1198, 1198,
-                                            1198, 1198, 1198, 1198, 1198};
-  // Generate 10 full sized packets, leave room for FU-A headers minus the NALU
-  // header.
-  TestFua(
-      10 * (kMaxPayloadSize - kFuAHeaderSize) + kNalHeaderSize, kMaxPayloadSize,
-      0,
-      std::vector<size_t>(kExpectedPayloadSizes,
-                          kExpectedPayloadSizes +
-                              sizeof(kExpectedPayloadSizes) / sizeof(size_t)));
+TEST(RtpPacketizerH264Test, FUARounding) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1448;
+  EXPECT_THAT(TestFua(10123, limits),
+              ElementsAre(1265, 1265, 1265, 1265, 1265, 1266, 1266, 1266));
 }
 
-TEST(RtpPacketizerH264Test, SendOverlongDataInPacketizationMode0) {
-  const size_t kFrameSize = kMaxPayloadSize + 1;
-  uint8_t frame[kFrameSize] = {0};
-  for (size_t i = 0; i < kFrameSize; ++i)
-    frame[i] = i;
-  RTPFragmentationHeader fragmentation;
-  fragmentation.VerifyAndAllocateFragmentationHeader(1);
-  fragmentation.fragmentationOffset[0] = 0;
-  fragmentation.fragmentationLength[0] = kFrameSize;
-  // Set NAL headers.
-  frame[fragmentation.fragmentationOffset[0]] = 0x01;
-
-  std::unique_ptr<RtpPacketizerH264> packetizer(CreateH264Packetizer(
-      H264PacketizationMode::SingleNalUnit, kMaxPayloadSize, 0));
-  EXPECT_EQ(0u, packetizer->SetPayloadData(frame, kFrameSize, &fragmentation));
+TEST(RtpPacketizerH264Test, FUABig) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 1200;
+  // Generate 10 full sized packets, leave room for FU-A headers.
+  EXPECT_THAT(
+      TestFua(10 * (1200 - kFuAHeaderSize), limits),
+      ElementsAre(1198, 1198, 1198, 1198, 1198, 1198, 1198, 1198, 1198, 1198));
 }
 
-namespace {
+TEST(RtpPacketizerH264Test, RejectsOverlongDataInPacketizationMode0) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  rtc::Buffer frame = CreateFrame(kMaxPayloadSize + 1);
+  RTPFragmentationHeader fragmentation = NoFragmentation(frame);
+
+  RtpPacketizerH264 packetizer(
+      frame, limits, H264PacketizationMode::SingleNalUnit, fragmentation);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
+
+  EXPECT_THAT(packets, IsEmpty());
+}
+
 const uint8_t kStartSequence[] = {0x00, 0x00, 0x00, 0x01};
 const uint8_t kOriginalSps[] = {kSps, 0x00, 0x00, 0x03, 0x03,
                                 0xF4, 0x05, 0x03, 0xC7, 0xC0};
@@ -497,7 +481,6 @@
                                  0xC7, 0xE0, 0x1B, 0x41, 0x10, 0x8D, 0x00};
 const uint8_t kIdrOne[] = {kIdr, 0xFF, 0x00, 0x00, 0x04};
 const uint8_t kIdrTwo[] = {kIdr, 0xFF, 0x00, 0x11};
-}  // namespace
 
 class RtpPacketizerH264TestSpsRewriting : public ::testing::Test {
  public:
@@ -522,33 +505,27 @@
  protected:
   rtc::Buffer in_buffer_;
   RTPFragmentationHeader fragmentation_header_;
-  std::unique_ptr<RtpPacketizerH264> packetizer_;
 };
 
 TEST_F(RtpPacketizerH264TestSpsRewriting, FuASps) {
   const size_t kHeaderOverhead = kFuAHeaderSize + 1;
 
   // Set size to fragment SPS into two FU-A packets.
-  packetizer_ =
-      CreateH264Packetizer(H264PacketizationMode::NonInterleaved,
-                           sizeof(kOriginalSps) - 2 + kHeaderOverhead, 0);
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = sizeof(kOriginalSps) - 2 + kHeaderOverhead;
+  RtpPacketizerH264 packetizer(in_buffer_, limits,
+                               H264PacketizationMode::NonInterleaved,
+                               fragmentation_header_);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
 
-  packetizer_->SetPayloadData(in_buffer_.data(), in_buffer_.size(),
-                              &fragmentation_header_);
-
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(sizeof(kOriginalSps) + kHeaderOverhead, packet.FreeCapacity());
-
-  EXPECT_TRUE(packetizer_->NextPacket(&packet));
   size_t offset = H264::kNaluTypeSize;
-  size_t length = packet.payload_size() - kFuAHeaderSize;
-  EXPECT_THAT(packet.payload().subview(kFuAHeaderSize),
+  size_t length = packets[0].payload_size() - kFuAHeaderSize;
+  EXPECT_THAT(packets[0].payload().subview(kFuAHeaderSize),
               ElementsAreArray(&kRewrittenSps[offset], length));
   offset += length;
 
-  EXPECT_TRUE(packetizer_->NextPacket(&packet));
-  length = packet.payload_size() - kFuAHeaderSize;
-  EXPECT_THAT(packet.payload().subview(kFuAHeaderSize),
+  length = packets[1].payload_size() - kFuAHeaderSize;
+  EXPECT_THAT(packets[1].payload().subview(kFuAHeaderSize),
               ElementsAreArray(&kRewrittenSps[offset], length));
   offset += length;
 
@@ -562,22 +539,28 @@
                                     sizeof(kIdrTwo) + (kLengthFieldLength * 3);
 
   // Set size to include SPS and the rest of the packets in a Stap-A package.
-  packetizer_ = CreateH264Packetizer(H264PacketizationMode::NonInterleaved,
-                                     kExpectedTotalSize + kHeaderOverhead, 0);
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = kExpectedTotalSize + kHeaderOverhead;
 
-  packetizer_->SetPayloadData(in_buffer_.data(), in_buffer_.size(),
-                              &fragmentation_header_);
+  RtpPacketizerH264 packetizer(in_buffer_, limits,
+                               H264PacketizationMode::NonInterleaved,
+                               fragmentation_header_);
+  std::vector<RtpPacketToSend> packets = FetchAllPackets(&packetizer);
 
-  RtpPacketToSend packet(kNoExtensions);
-  ASSERT_LE(kExpectedTotalSize + kHeaderOverhead, packet.FreeCapacity());
-
-  EXPECT_TRUE(packetizer_->NextPacket(&packet));
-  EXPECT_EQ(kExpectedTotalSize, packet.payload_size());
-  EXPECT_THAT(packet.payload().subview(H264::kNaluTypeSize + kLengthFieldLength,
-                                       sizeof(kRewrittenSps)),
-              ElementsAreArray(kRewrittenSps));
+  ASSERT_THAT(packets, SizeIs(1));
+  EXPECT_EQ(packets[0].payload_size(), kExpectedTotalSize);
+  EXPECT_THAT(
+      packets[0].payload().subview(H264::kNaluTypeSize + kLengthFieldLength,
+                                   sizeof(kRewrittenSps)),
+      ElementsAreArray(kRewrittenSps));
 }
 
+struct H264ParsedPayload : public RtpDepacketizer::ParsedPayload {
+  RTPVideoHeaderH264& h264() {
+    return absl::get<RTPVideoHeaderH264>(video.video_type_header);
+  }
+};
+
 class RtpDepacketizerH264Test : public ::testing::Test {
  protected:
   RtpDepacketizerH264Test()
@@ -939,4 +922,5 @@
   EXPECT_EQ(-1, h264.nalus[0].pps_id);
 }
 
+}  // namespace
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_format_unittest.cc b/modules/rtp_rtcp/source/rtp_format_unittest.cc
new file mode 100644
index 0000000..a79c434
--- /dev/null
+++ b/modules/rtp_rtcp/source/rtp_format_unittest.cc
@@ -0,0 +1,274 @@
+/*
+ *  Copyright (c) 2018 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 "modules/rtp_rtcp/source/rtp_format.h"
+
+#include <memory>
+#include <numeric>
+
+#include "test/gmock.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace {
+
+using ::testing::ElementsAre;
+using ::testing::Le;
+using ::testing::Gt;
+using ::testing::Each;
+using ::testing::IsEmpty;
+using ::testing::Not;
+using ::testing::SizeIs;
+
+// Calculate difference between largest and smallest packets respecting sizes
+// adjustement provided by limits,
+// i.e. last packet expected to be smaller than 'average' by reduction_len.
+int EffectivePacketsSizeDifference(
+    std::vector<int> sizes,
+    const RtpPacketizer::PayloadSizeLimits& limits) {
+  // Account for larger last packet header.
+  sizes.back() += limits.last_packet_reduction_len;
+
+  auto minmax = std::minmax_element(sizes.begin(), sizes.end());
+  // MAX-MIN
+  return *minmax.second - *minmax.first;
+}
+
+int Sum(const std::vector<int>& sizes) {
+  return std::accumulate(sizes.begin(), sizes.end(), 0);
+}
+
+TEST(RtpPacketizerSplitAboutEqually, AllPacketsAreEqualSumToPayloadLen) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+
+  EXPECT_THAT(Sum(payload_sizes), 13);
+}
+
+TEST(RtpPacketizerSplitAboutEqually, AllPacketsAreEqualRespectsMaxPayloadSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+
+  EXPECT_THAT(payload_sizes, Each(Le(limits.max_payload_len)));
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     AllPacketsAreEqualRespectsFirstPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.first_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+
+  ASSERT_THAT(payload_sizes, Not(IsEmpty()));
+  EXPECT_EQ(payload_sizes.front() + limits.first_packet_reduction_len,
+            limits.max_payload_len);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     AllPacketsAreEqualRespectsLastPacketReductionLength) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+
+  ASSERT_THAT(payload_sizes, Not(IsEmpty()));
+  EXPECT_LE(payload_sizes.back() + limits.last_packet_reduction_len,
+            limits.max_payload_len);
+}
+
+TEST(RtpPacketizerSplitAboutEqually, AllPacketsAreEqualInSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+
+  EXPECT_EQ(EffectivePacketsSizeDifference(payload_sizes, limits), 0);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     AllPacketsAreEqualGeneratesMinimumNumberOfPackets) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 2;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(13, limits);
+  // Computed by hand. 3 packets would have exactly capacity 3*5-2=13
+  // (max length - for each packet minus last packet reduction).
+  EXPECT_THAT(payload_sizes, SizeIs(3));
+}
+
+TEST(RtpPacketizerSplitAboutEqually, SomePacketsAreSmallerSumToPayloadLen) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.last_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(28, limits);
+
+  EXPECT_THAT(Sum(payload_sizes), 28);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     SomePacketsAreSmallerRespectsMaxPayloadSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.last_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(28, limits);
+
+  EXPECT_THAT(payload_sizes, Each(Le(limits.max_payload_len)));
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     SomePacketsAreSmallerRespectsFirstPacketReduction) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.first_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(28, limits);
+
+  EXPECT_LE(payload_sizes.front() + limits.first_packet_reduction_len,
+            limits.max_payload_len);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     SomePacketsAreSmallerRespectsLastPacketReductionLength) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.last_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(28, limits);
+
+  EXPECT_LE(payload_sizes.back(),
+            limits.max_payload_len - limits.last_packet_reduction_len);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     SomePacketsAreSmallerPacketsAlmostEqualInSize) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.last_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(28, limits);
+
+  EXPECT_LE(EffectivePacketsSizeDifference(payload_sizes, limits), 1);
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     SomePacketsAreSmallerGeneratesMinimumNumberOfPackets) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 7;
+  limits.last_packet_reduction_len = 5;
+
+  std::vector<int> payload_sizes = RtpPacketizer::SplitAboutEqually(24, limits);
+  // Computed by hand. 4 packets would have capacity 4*7-5=23 (max length -
+  // for each packet minus last packet reduction).
+  // 5 packets is enough for kPayloadSize.
+  EXPECT_THAT(payload_sizes, SizeIs(5));
+}
+
+TEST(RtpPacketizerSplitAboutEqually, GivesNonZeroPayloadLengthEachPacket) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 600;
+  limits.first_packet_reduction_len = 500;
+  limits.last_packet_reduction_len = 550;
+
+  // Naive implementation would split 1450 payload + 1050 reduction bytes into 5
+  // packets 500 bytes each, thus leaving first packet zero bytes and even less
+  // to last packet.
+  std::vector<int> payload_sizes =
+      RtpPacketizer::SplitAboutEqually(1450, limits);
+
+  EXPECT_EQ(Sum(payload_sizes), 1450);
+  EXPECT_THAT(payload_sizes, Each(Gt(0)));
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     OnePacketWhenExtraSpaceIsEnoughForSumOfFirstAndLastPacketReductions) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 30;
+  limits.first_packet_reduction_len = 6;
+  limits.last_packet_reduction_len = 4;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(20, limits), ElementsAre(20));
+}
+
+TEST(RtpPacketizerSplitAboutEqually,
+     TwoPacketsWhenExtraSpaceIsTooSmallForSumOfFirstAndLastPacketReductions) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 29;
+  limits.first_packet_reduction_len = 6;
+  limits.last_packet_reduction_len = 4;
+
+  // First packet needs two more extra bytes compared to last one,
+  // so should have two less payload bytes.
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(20, limits), ElementsAre(9, 11));
+}
+
+TEST(RtpPacketizerSplitAboutEqually, RejectsZeroMaxPayloadLen) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 0;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(20, limits), IsEmpty());
+}
+
+TEST(RtpPacketizerSplitAboutEqually, RejectsZeroFirstPacketLen) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.first_packet_reduction_len = 5;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(20, limits), IsEmpty());
+}
+
+TEST(RtpPacketizerSplitAboutEqually, RejectsZeroLastPacketLen) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 5;
+  limits.last_packet_reduction_len = 5;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(20, limits), IsEmpty());
+}
+
+TEST(RtpPacketizerSplitAboutEqually, CantPutSinglePayloadByteInTwoPackets) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 10;
+  limits.first_packet_reduction_len = 6;
+  limits.last_packet_reduction_len = 4;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(1, limits), IsEmpty());
+}
+
+TEST(RtpPacketizerSplitAboutEqually, CanPutTwoPayloadBytesInTwoPackets) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 10;
+  limits.first_packet_reduction_len = 6;
+  limits.last_packet_reduction_len = 4;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(2, limits), ElementsAre(1, 1));
+}
+
+TEST(RtpPacketizerSplitAboutEqually, CanPutSinglePayloadByteInOnePacket) {
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 11;
+  limits.first_packet_reduction_len = 6;
+  limits.last_packet_reduction_len = 4;
+
+  EXPECT_THAT(RtpPacketizer::SplitAboutEqually(1, limits), ElementsAre(1));
+}
+
+}  // namespace
+}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_format_video_generic.cc b/modules/rtp_rtcp/source/rtp_format_video_generic.cc
index 1731237..edd1e3c 100644
--- a/modules/rtp_rtcp/source/rtp_format_video_generic.cc
+++ b/modules/rtp_rtcp/source/rtp_format_video_generic.cc
@@ -21,121 +21,70 @@
 static const size_t kExtendedHeaderLength = 2;
 
 RtpPacketizerGeneric::RtpPacketizerGeneric(
+    rtc::ArrayView<const uint8_t> payload,
+    PayloadSizeLimits limits,
     const RTPVideoHeader& rtp_video_header,
-    FrameType frame_type,
-    size_t max_payload_len,
-    size_t last_packet_reduction_len)
-    : picture_id_(rtp_video_header.generic
-                      ? absl::optional<uint16_t>(
-                            rtp_video_header.generic->frame_id & 0x7FFF)
-                      : absl::nullopt),
-      payload_data_(nullptr),
-      payload_size_(0),
-      max_payload_len_(max_payload_len - kGenericHeaderLength -
-                       (picture_id_.has_value() ? kExtendedHeaderLength : 0)),
-      last_packet_reduction_len_(last_packet_reduction_len),
-      frame_type_(frame_type),
-      num_packets_left_(0),
-      num_larger_packets_(0) {}
+    FrameType frame_type)
+    : remaining_payload_(payload) {
+  BuildHeader(rtp_video_header, frame_type);
 
-RtpPacketizerGeneric::~RtpPacketizerGeneric() {}
-
-size_t RtpPacketizerGeneric::SetPayloadData(
-    const uint8_t* payload_data,
-    size_t payload_size,
-    const RTPFragmentationHeader* fragmentation) {
-  payload_data_ = payload_data;
-  payload_size_ = payload_size;
-
-  // Fragment packets such that they are almost the same size, even accounting
-  // for larger header in the last packet.
-  // Since we are given how much extra space is occupied by the longer header
-  // in the last packet, we can pretend that RTP headers are the same, but
-  // there's last_packet_reduction_len_ virtual payload, to be put at the end of
-  // the last packet.
-  //
-  size_t total_bytes = payload_size_ + last_packet_reduction_len_;
-
-  // Minimum needed number of packets to fit payload and virtual payload in the
-  // last packet.
-  num_packets_left_ = (total_bytes + max_payload_len_ - 1) / max_payload_len_;
-  // Given number of packets, calculate average size rounded down.
-  payload_len_per_packet_ = total_bytes / num_packets_left_;
-  // If we can't divide everything perfectly evenly, we put 1 extra byte in some
-  // last packets: 14 bytes in 4 packets would be split as 3+3+4+4.
-  num_larger_packets_ = total_bytes % num_packets_left_;
-  RTC_DCHECK_LE(payload_len_per_packet_, max_payload_len_);
-
-  generic_header_ = RtpFormatVideoGeneric::kFirstPacketBit;
-  if (frame_type_ == kVideoFrameKey) {
-    generic_header_ |= RtpFormatVideoGeneric::kKeyFrameBit;
-  }
-  if (picture_id_.has_value()) {
-    generic_header_ |= RtpFormatVideoGeneric::kExtendedHeaderBit;
-  }
-
-  return num_packets_left_;
+  limits.max_payload_len -= header_size_;
+  payload_sizes_ = SplitAboutEqually(payload.size(), limits);
+  current_packet_ = payload_sizes_.begin();
 }
 
+RtpPacketizerGeneric::~RtpPacketizerGeneric() = default;
+
 size_t RtpPacketizerGeneric::NumPackets() const {
-  return num_packets_left_;
+  return payload_sizes_.end() - current_packet_;
 }
 
 bool RtpPacketizerGeneric::NextPacket(RtpPacketToSend* packet) {
   RTC_DCHECK(packet);
-  if (num_packets_left_ == 0)
+  if (current_packet_ == payload_sizes_.end())
     return false;
-  // Last larger_packets_ packets are 1 byte larger than previous packets.
-  // Increase per packet payload once needed.
-  if (num_packets_left_ == num_larger_packets_)
-    ++payload_len_per_packet_;
-  size_t next_packet_payload_len = payload_len_per_packet_;
-  if (payload_size_ <= next_packet_payload_len) {
-    // Whole payload fits into this packet.
-    next_packet_payload_len = payload_size_;
-    if (num_packets_left_ == 2) {
-      // This is the penultimate packet. Leave at least 1 payload byte for the
-      // last packet.
-      --next_packet_payload_len;
-      RTC_DCHECK_GT(next_packet_payload_len, 0);
-    }
-  }
-  RTC_DCHECK_LE(next_packet_payload_len, max_payload_len_);
 
-  size_t total_length = next_packet_payload_len + kGenericHeaderLength +
-                        (picture_id_.has_value() ? kExtendedHeaderLength : 0);
-  uint8_t* out_ptr = packet->AllocatePayload(total_length);
+  size_t next_packet_payload_len = *current_packet_;
 
-  // Put generic header in packet.
-  out_ptr[0] = generic_header_;
-  out_ptr += kGenericHeaderLength;
+  uint8_t* out_ptr =
+      packet->AllocatePayload(header_size_ + next_packet_payload_len);
+  RTC_CHECK(out_ptr);
 
-  if (picture_id_.has_value()) {
-    WriteExtendedHeader(out_ptr);
-    out_ptr += kExtendedHeaderLength;
-  }
+  memcpy(out_ptr, header_, header_size_);
+  memcpy(out_ptr + header_size_, remaining_payload_.data(),
+         next_packet_payload_len);
 
   // Remove first-packet bit, following packets are intermediate.
-  generic_header_ &= ~RtpFormatVideoGeneric::kFirstPacketBit;
+  header_[0] &= ~RtpFormatVideoGeneric::kFirstPacketBit;
 
-  // Put payload in packet.
-  memcpy(out_ptr, payload_data_, next_packet_payload_len);
-  payload_data_ += next_packet_payload_len;
-  payload_size_ -= next_packet_payload_len;
-  --num_packets_left_;
+  remaining_payload_ = remaining_payload_.subview(next_packet_payload_len);
+
+  ++current_packet_;
+
   // Packets left to produce and data left to split should end at the same time.
-  RTC_DCHECK_EQ(num_packets_left_ == 0, payload_size_ == 0);
+  RTC_DCHECK_EQ(current_packet_ == payload_sizes_.end(),
+                remaining_payload_.empty());
 
-  packet->SetMarker(payload_size_ == 0);
-
+  packet->SetMarker(remaining_payload_.empty());
   return true;
 }
 
-void RtpPacketizerGeneric::WriteExtendedHeader(uint8_t* out_ptr) {
-  // Store bottom 15 bits of the the sequence number. Only 15 bits are used for
-  // compatibility with other packetizer implemenetations that also use 15 bits.
-  out_ptr[0] = (*picture_id_ >> 8) & 0x7F;
-  out_ptr[1] = *picture_id_ & 0xFF;
+void RtpPacketizerGeneric::BuildHeader(const RTPVideoHeader& rtp_video_header,
+                                       FrameType frame_type) {
+  header_size_ = kGenericHeaderLength;
+  header_[0] = RtpFormatVideoGeneric::kFirstPacketBit;
+  if (frame_type == kVideoFrameKey) {
+    header_[0] |= RtpFormatVideoGeneric::kKeyFrameBit;
+  }
+  if (rtp_video_header.generic.has_value()) {
+    // Store bottom 15 bits of the the picture id. Only 15 bits are used for
+    // compatibility with other packetizer implemenetations.
+    uint16_t picture_id = rtp_video_header.generic->frame_id & 0x7FFF;
+    header_[0] |= RtpFormatVideoGeneric::kExtendedHeaderBit;
+    header_[1] = (picture_id >> 8) & 0x7F;
+    header_[2] = picture_id & 0xFF;
+    header_size_ += kExtendedHeaderLength;
+  }
 }
 
 RtpDepacketizerGeneric::~RtpDepacketizerGeneric() = default;
diff --git a/modules/rtp_rtcp/source/rtp_format_video_generic.h b/modules/rtp_rtcp/source/rtp_format_video_generic.h
index 293b6e9..03509f5 100644
--- a/modules/rtp_rtcp/source/rtp_format_video_generic.h
+++ b/modules/rtp_rtcp/source/rtp_format_video_generic.h
@@ -10,8 +10,9 @@
 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VIDEO_GENERIC_H_
 #define MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VIDEO_GENERIC_H_
 
-#include <string>
+#include <vector>
 
+#include "api/array_view.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/rtp_rtcp/source/rtp_format.h"
 #include "rtc_base/constructormagic.h"
@@ -29,18 +30,13 @@
  public:
   // Initialize with payload from encoder.
   // The payload_data must be exactly one encoded generic frame.
-  RtpPacketizerGeneric(const RTPVideoHeader& rtp_video_header,
-                       FrameType frametype,
-                       size_t max_payload_len,
-                       size_t last_packet_reduction_len);
+  RtpPacketizerGeneric(rtc::ArrayView<const uint8_t> payload,
+                       PayloadSizeLimits limits,
+                       const RTPVideoHeader& rtp_video_header,
+                       FrameType frametype);
 
   ~RtpPacketizerGeneric() override;
 
-  // Returns total number of packets to be generated.
-  size_t SetPayloadData(const uint8_t* payload_data,
-                        size_t payload_size,
-                        const RTPFragmentationHeader* fragmentation);
-
   size_t NumPackets() const override;
 
   // Get the next payload with generic payload header.
@@ -49,20 +45,15 @@
   bool NextPacket(RtpPacketToSend* packet) override;
 
  private:
-  const absl::optional<uint16_t> picture_id_;
-  const uint8_t* payload_data_;
-  size_t payload_size_;
-  const size_t max_payload_len_;
-  const size_t last_packet_reduction_len_;
-  FrameType frame_type_;
-  size_t payload_len_per_packet_;
-  uint8_t generic_header_;
-  // Number of packets yet to be retrieved by NextPacket() call.
-  size_t num_packets_left_;
-  // Number of packets, which will be 1 byte more than the rest.
-  size_t num_larger_packets_;
+  // Fills header_ and header_size_ members.
+  void BuildHeader(const RTPVideoHeader& rtp_video_header,
+                   FrameType frame_type);
 
-  void WriteExtendedHeader(uint8_t* out_ptr);
+  uint8_t header_[3];
+  size_t header_size_;
+  rtc::ArrayView<const uint8_t> remaining_payload_;
+  std::vector<int> payload_sizes_;
+  std::vector<int>::const_iterator current_packet_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(RtpPacketizerGeneric);
 };
diff --git a/modules/rtp_rtcp/source/rtp_format_video_generic_unittest.cc b/modules/rtp_rtcp/source/rtp_format_video_generic_unittest.cc
index e77dabf..aa2829b 100644
--- a/modules/rtp_rtcp/source/rtp_format_video_generic_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_format_video_generic_unittest.cc
@@ -29,200 +29,113 @@
 using ::testing::ElementsAreArray;
 using ::testing::Le;
 using ::testing::SizeIs;
+using ::testing::Contains;
 
-const size_t kMaxPayloadSize = 1200;
+constexpr RtpPacketizer::PayloadSizeLimits kNoSizeLimits;
 
-uint8_t kTestPayload[kMaxPayloadSize];
-
-std::vector<size_t> NextPacketFillPayloadSizes(
-    RtpPacketizerGeneric* packetizer) {
+std::vector<int> NextPacketFillPayloadSizes(RtpPacketizerGeneric* packetizer) {
   RtpPacketToSend packet(nullptr);
-  std::vector<size_t> result;
+  std::vector<int> result;
   while (packetizer->NextPacket(&packet)) {
     result.push_back(packet.payload_size());
   }
   return result;
 }
 
-size_t GetEffectivePacketsSizeDifference(std::vector<size_t>* payload_sizes,
-                                         size_t last_packet_reduction_len) {
-  // Account for larger last packet header.
-  payload_sizes->back() += last_packet_reduction_len;
-  auto minmax =
-      std::minmax_element(payload_sizes->begin(), payload_sizes->end());
-  // MAX-MIN
-  size_t difference = *minmax.second - *minmax.first;
-  // Revert temporary changes.
-  payload_sizes->back() -= last_packet_reduction_len;
-  return difference;
+TEST(RtpPacketizerVideoGeneric, RespectsMaxPayloadSize) {
+  const size_t kPayloadSize = 50;
+  const uint8_t kPayload[kPayloadSize] = {};
+
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 6;
+  RtpPacketizerGeneric packetizer(kPayload, limits, RTPVideoHeader(),
+                                  kVideoFrameKey);
+
+  std::vector<int> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
+
+  EXPECT_THAT(payload_sizes, Each(Le(limits.max_payload_len)));
 }
 
-}  // namespace
+TEST(RtpPacketizerVideoGeneric, UsesMaxPayloadSize) {
+  const size_t kPayloadSize = 50;
+  const uint8_t kPayload[kPayloadSize] = {};
 
-TEST(RtpPacketizerVideoGeneric, AllPacketsMayBeEqualAndRespectMaxPayloadSize) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 6;
+  RtpPacketizerGeneric packetizer(kPayload, limits, RTPVideoHeader(),
+                                  kVideoFrameKey);
+
+  std::vector<int> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
+
+  // With kPayloadSize > max_payload_len^2, there should be packets that use
+  // all the payload, otherwise it is possible to use less packets.
+  EXPECT_THAT(payload_sizes, Contains(limits.max_payload_len));
+}
+
+TEST(RtpPacketizerVideoGeneric, WritesExtendedHeaderWhenPictureIdIsSet) {
   const size_t kPayloadSize = 13;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_THAT(payload_sizes, Each(Le(kMaxPayloadLen)));
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     AllPacketsMayBeEqual_RespectsLastPacketReductionLength) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
-  const size_t kPayloadSize = 13;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_LE(payload_sizes.back(), kMaxPayloadLen - kLastPacketReductionLen);
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     AllPacketsMayBeEqual_MakesPacketsAlmostEqualInSize) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
-  const size_t kPayloadSize = 13;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  size_t sizes_difference = GetEffectivePacketsSizeDifference(
-      &payload_sizes, kLastPacketReductionLen);
-  EXPECT_LE(sizes_difference, 1u);
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     AllPacketsMayBeEqual_GeneratesMinimumNumberOfPackets) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 3;
-  const size_t kPayloadSize = 13;
-  // Computed by hand. 3 packets would have capacity 3*(6-1)-3=12 (max length -
-  // generic header lengh for each packet minus last packet reduction).
-  // 4 packets is enough for kPayloadSize.
-  const size_t kMinNumPackets = 4;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_EQ(num_packets, kMinNumPackets);
-}
-
-TEST(RtpPacketizerVideoGeneric, SomePacketsAreSmaller_RespectsMaxPayloadSize) {
-  const size_t kMaxPayloadLen = 8;
-  const size_t kLastPacketReductionLen = 5;
-  const size_t kPayloadSize = 28;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_THAT(payload_sizes, Each(Le(kMaxPayloadLen)));
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     SomePacketsAreSmaller_RespectsLastPacketReductionLength) {
-  const size_t kMaxPayloadLen = 8;
-  const size_t kLastPacketReductionLen = 5;
-  const size_t kPayloadSize = 28;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_LE(payload_sizes.back(), kMaxPayloadLen - kLastPacketReductionLen);
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     SomePacketsAreSmaller_MakesPacketsAlmostEqualInSize) {
-  const size_t kMaxPayloadLen = 8;
-  const size_t kLastPacketReductionLen = 5;
-  const size_t kPayloadSize = 28;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  size_t sizes_difference = GetEffectivePacketsSizeDifference(
-      &payload_sizes, kLastPacketReductionLen);
-  EXPECT_LE(sizes_difference, 1u);
-}
-
-TEST(RtpPacketizerVideoGeneric,
-     SomePacketsAreSmaller_GeneratesMinimumNumberOfPackets) {
-  const size_t kMaxPayloadLen = 8;
-  const size_t kLastPacketReductionLen = 5;
-  const size_t kPayloadSize = 28;
-  // Computed by hand. 4 packets would have capacity 4*(8-1)-5=23 (max length -
-  // generic header lengh for each packet minus last packet reduction).
-  // 5 packets is enough for kPayloadSize.
-  const size_t kMinNumPackets = 5;
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  size_t num_packets =
-      packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
-  std::vector<size_t> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
-  EXPECT_THAT(payload_sizes, SizeIs(num_packets));
-
-  EXPECT_EQ(num_packets, kMinNumPackets);
-}
-
-TEST(RtpPacketizerVideoGeneric, HasFrameIdWritesExtendedHeader) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
-  const size_t kPayloadSize = 13;
+  const uint8_t kPayload[kPayloadSize] = {};
 
   RTPVideoHeader rtp_video_header;
   rtp_video_header.generic.emplace().frame_id = 37;
-  RtpPacketizerGeneric packetizer(rtp_video_header, kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
+  RtpPacketizerGeneric packetizer(kPayload, kNoSizeLimits, rtp_video_header,
+                                  kVideoFrameKey);
 
   RtpPacketToSend packet(nullptr);
-  packetizer.NextPacket(&packet);
+  ASSERT_TRUE(packetizer.NextPacket(&packet));
 
   rtc::ArrayView<const uint8_t> payload = packet.payload();
+  EXPECT_EQ(payload.size(), 3 + kPayloadSize);
   EXPECT_TRUE(payload[0] & 0x04);  // Extended header bit is set.
   // Frame id is 37.
   EXPECT_EQ(0u, payload[1]);
   EXPECT_EQ(37u, payload[2]);
 }
 
+TEST(RtpPacketizerVideoGeneric, RespectsMaxPayloadSizeWithExtendedHeader) {
+  const int kPayloadSize = 50;
+  const uint8_t kPayload[kPayloadSize] = {};
+
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 6;
+  RTPVideoHeader rtp_video_header;
+  rtp_video_header.generic.emplace().frame_id = 37;
+  RtpPacketizerGeneric packetizer(kPayload, limits, rtp_video_header,
+                                  kVideoFrameKey);
+
+  std::vector<int> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
+
+  EXPECT_THAT(payload_sizes, Each(Le(limits.max_payload_len)));
+}
+
+TEST(RtpPacketizerVideoGeneric, UsesMaxPayloadSizeWithExtendedHeader) {
+  const int kPayloadSize = 50;
+  const uint8_t kPayload[kPayloadSize] = {};
+
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 6;
+  RTPVideoHeader rtp_video_header;
+  rtp_video_header.generic.emplace().frame_id = 37;
+  RtpPacketizerGeneric packetizer(kPayload, limits, rtp_video_header,
+                                  kVideoFrameKey);
+  std::vector<int> payload_sizes = NextPacketFillPayloadSizes(&packetizer);
+
+  // With kPayloadSize > max_payload_len^2, there should be packets that use
+  // all the payload, otherwise it is possible to use less packets.
+  EXPECT_THAT(payload_sizes, Contains(limits.max_payload_len));
+}
+
 TEST(RtpPacketizerVideoGeneric, FrameIdOver15bitsWrapsAround) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
-  const size_t kPayloadSize = 13;
+  const int kPayloadSize = 13;
+  const uint8_t kPayload[kPayloadSize] = {};
 
   RTPVideoHeader rtp_video_header;
   rtp_video_header.generic.emplace().frame_id = 0x8137;
-  RtpPacketizerGeneric packetizer(rtp_video_header, kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
+  RtpPacketizerGeneric packetizer(kPayload, kNoSizeLimits, rtp_video_header,
+                                  kVideoFrameKey);
 
   RtpPacketToSend packet(nullptr);
-  packetizer.NextPacket(&packet);
+  ASSERT_TRUE(packetizer.NextPacket(&packet));
 
   rtc::ArrayView<const uint8_t> payload = packet.payload();
   EXPECT_TRUE(payload[0] & 0x04);  // Extended header bit is set.
@@ -232,16 +145,14 @@
 }
 
 TEST(RtpPacketizerVideoGeneric, NoFrameIdDoesNotWriteExtendedHeader) {
-  const size_t kMaxPayloadLen = 6;
-  const size_t kLastPacketReductionLen = 2;
-  const size_t kPayloadSize = 13;
+  const int kPayloadSize = 13;
+  const uint8_t kPayload[kPayloadSize] = {};
 
-  RtpPacketizerGeneric packetizer(RTPVideoHeader(), kVideoFrameKey,
-                                  kMaxPayloadLen, kLastPacketReductionLen);
-  packetizer.SetPayloadData(kTestPayload, kPayloadSize, nullptr);
+  RtpPacketizerGeneric packetizer(kPayload, kNoSizeLimits, RTPVideoHeader(),
+                                  kVideoFrameKey);
 
   RtpPacketToSend packet(nullptr);
-  packetizer.NextPacket(&packet);
+  ASSERT_TRUE(packetizer.NextPacket(&packet));
 
   rtc::ArrayView<const uint8_t> payload = packet.payload();
   EXPECT_FALSE(payload[0] & 0x04);
@@ -270,4 +181,5 @@
   EXPECT_EQ(0x1337, parsed_payload.video_header().generic->frame_id);
 }
 
+}  // namespace
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_format_vp8.cc b/modules/rtp_rtcp/source/rtp_format_vp8.cc
index 4a511fc..f40434e 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp8.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp8.cc
@@ -23,8 +23,15 @@
 namespace webrtc {
 namespace {
 
-// Length of VP8 payload descriptors' fixed part.
-constexpr int kVp8FixedPayloadDescriptorSize = 1;
+constexpr int kXBit = 0x80;
+constexpr int kNBit = 0x20;
+constexpr int kSBit = 0x10;
+constexpr int kKeyIdxField = 0x1F;
+constexpr int kIBit = 0x80;
+constexpr int kLBit = 0x40;
+constexpr int kTBit = 0x20;
+constexpr int kKBit = 0x10;
+constexpr int kYBit = 0x20;
 
 int ParseVP8PictureID(RTPVideoHeaderVP8* vp8,
                       const uint8_t** data,
@@ -165,259 +172,106 @@
 RtpPacketizerVp8::RtpPacketizerVp8(rtc::ArrayView<const uint8_t> payload,
                                    PayloadSizeLimits limits,
                                    const RTPVideoHeaderVP8& hdr_info)
-    : payload_data_(payload.data()), hdr_info_(hdr_info), limits_(limits) {
-  RTC_DCHECK(ValidateHeader(hdr_info));
-  GeneratePackets(payload.size());
+    : hdr_(BuildHeader(hdr_info)), remaining_payload_(payload) {
+  limits.max_payload_len -= hdr_.size();
+  payload_sizes_ = SplitAboutEqually(payload.size(), limits);
+  current_packet_ = payload_sizes_.begin();
 }
 
 RtpPacketizerVp8::~RtpPacketizerVp8() = default;
 
 size_t RtpPacketizerVp8::NumPackets() const {
-  return packets_.size();
+  return payload_sizes_.end() - current_packet_;
 }
 
 bool RtpPacketizerVp8::NextPacket(RtpPacketToSend* packet) {
   RTC_DCHECK(packet);
-  if (packets_.empty()) {
+  if (current_packet_ == payload_sizes_.end()) {
     return false;
   }
-  InfoStruct packet_info = packets_.front();
-  packets_.pop();
 
-  size_t packet_payload_len =
-      packets_.empty()
-          ? limits_.max_payload_len - limits_.last_packet_reduction_len
-          : limits_.max_payload_len;
-  uint8_t* buffer = packet->AllocatePayload(packet_payload_len);
-  int bytes = WriteHeaderAndPayload(packet_info, buffer, packet_payload_len);
-  if (bytes < 0) {
-    return false;
-  }
-  packet->SetPayloadSize(bytes);
-  packet->SetMarker(packets_.empty());
+  size_t packet_payload_len = *current_packet_;
+  ++current_packet_;
+
+  uint8_t* buffer = packet->AllocatePayload(hdr_.size() + packet_payload_len);
+  RTC_CHECK(buffer);
+
+  memcpy(buffer, hdr_.data(), hdr_.size());
+  memcpy(buffer + hdr_.size(), remaining_payload_.data(), packet_payload_len);
+
+  remaining_payload_ = remaining_payload_.subview(packet_payload_len);
+  hdr_[0] &= (~kSBit);  //  Clear 'Start of partition' bit.
+  packet->SetMarker(current_packet_ == payload_sizes_.end());
   return true;
 }
 
-void RtpPacketizerVp8::GeneratePackets(size_t payload_len) {
-  if (limits_.max_payload_len - limits_.last_packet_reduction_len <
-      kVp8FixedPayloadDescriptorSize + PayloadDescriptorExtraLength() + 1) {
-    // The provided payload length is not long enough for the payload
-    // descriptor and one payload byte in the last packet.
-    return;
+// Write the VP8 payload descriptor.
+//       0
+//       0 1 2 3 4 5 6 7 8
+//      +-+-+-+-+-+-+-+-+-+
+//      |X| |N|S| PART_ID |
+//      +-+-+-+-+-+-+-+-+-+
+// X:   |I|L|T|K|         | (mandatory if any of the below are used)
+//      +-+-+-+-+-+-+-+-+-+
+// I:   |PictureID   (16b)| (optional)
+//      +-+-+-+-+-+-+-+-+-+
+// L:   |   TL0PIC_IDX    | (optional)
+//      +-+-+-+-+-+-+-+-+-+
+// T/K: |TID:Y|  KEYIDX   | (optional)
+//      +-+-+-+-+-+-+-+-+-+
+RtpPacketizerVp8::RawHeader RtpPacketizerVp8::BuildHeader(
+    const RTPVideoHeaderVP8& header) {
+  RTC_DCHECK(ValidateHeader(header));
+
+  RawHeader result;
+  bool tid_present = header.temporalIdx != kNoTemporalIdx;
+  bool keyid_present = header.keyIdx != kNoKeyIdx;
+  bool tl0_pid_present = header.tl0PicIdx != kNoTl0PicIdx;
+  bool pid_present = header.pictureId != kNoPictureId;
+  uint8_t x_field = 0;
+  if (pid_present)
+    x_field |= kIBit;
+  if (tl0_pid_present)
+    x_field |= kLBit;
+  if (tid_present)
+    x_field |= kTBit;
+  if (keyid_present)
+    x_field |= kKBit;
+
+  uint8_t flags = 0;
+  if (x_field != 0)
+    flags |= kXBit;
+  if (header.nonReference)
+    flags |= kNBit;
+  // Create header as first packet in the frame. NextPacket() will clear it
+  // after first use.
+  flags |= kSBit;
+  result.push_back(flags);
+  if (x_field == 0) {
+    return result;
   }
-
-  size_t capacity = limits_.max_payload_len - (kVp8FixedPayloadDescriptorSize +
-                                               PayloadDescriptorExtraLength());
-
-  // Last packet of the last partition is smaller. Pretend that it's the same
-  // size, but we must write more payload to it.
-  size_t total_bytes = payload_len + limits_.last_packet_reduction_len;
-  // Integer divisions with rounding up.
-  size_t num_packets_left = (total_bytes + capacity - 1) / capacity;
-  size_t bytes_per_packet = total_bytes / num_packets_left;
-  size_t num_larger_packets = total_bytes % num_packets_left;
-  size_t remaining_data = payload_len;
-  while (remaining_data > 0) {
-    // Last num_larger_packets are 1 byte wider than the rest. Increase
-    // per-packet payload size when needed.
-    if (num_packets_left == num_larger_packets)
-      ++bytes_per_packet;
-    size_t current_packet_bytes = bytes_per_packet;
-    if (current_packet_bytes > remaining_data) {
-      current_packet_bytes = remaining_data;
+  result.push_back(x_field);
+  if (pid_present) {
+    const uint16_t pic_id = static_cast<uint16_t>(header.pictureId);
+    result.push_back(0x80 | ((pic_id >> 8) & 0x7F));
+    result.push_back(pic_id & 0xFF);
+  }
+  if (tl0_pid_present) {
+    result.push_back(header.tl0PicIdx);
+  }
+  if (tid_present || keyid_present) {
+    uint8_t data_field = 0;
+    if (tid_present) {
+      data_field |= header.temporalIdx << 6;
+      if (header.layerSync)
+        data_field |= kYBit;
     }
-    // This is not the last packet in the whole payload, but there's no data
-    // left for the last packet. Leave at least one byte for the last packet.
-    if (num_packets_left == 2 && current_packet_bytes == remaining_data) {
-      --current_packet_bytes;
+    if (keyid_present) {
+      data_field |= (header.keyIdx & kKeyIdxField);
     }
-    QueuePacket(payload_len - remaining_data, current_packet_bytes,
-                /*first_packet=*/remaining_data == payload_len);
-    remaining_data -= current_packet_bytes;
-    --num_packets_left;
+    result.push_back(data_field);
   }
-}
-
-void RtpPacketizerVp8::QueuePacket(size_t start_pos,
-                                   size_t packet_size,
-                                   bool first_packet) {
-  // Write info to packet info struct and store in packet info queue.
-  InfoStruct packet_info;
-  packet_info.payload_start_pos = start_pos;
-  packet_info.size = packet_size;
-  packet_info.first_packet = first_packet;
-  packets_.push(packet_info);
-}
-
-int RtpPacketizerVp8::WriteHeaderAndPayload(const InfoStruct& packet_info,
-                                            uint8_t* buffer,
-                                            size_t buffer_length) const {
-  // Write the VP8 payload descriptor.
-  //       0
-  //       0 1 2 3 4 5 6 7 8
-  //      +-+-+-+-+-+-+-+-+-+
-  //      |X| |N|S| PART_ID |
-  //      +-+-+-+-+-+-+-+-+-+
-  // X:   |I|L|T|K|         | (mandatory if any of the below are used)
-  //      +-+-+-+-+-+-+-+-+-+
-  // I:   |PictureID (8/16b)| (optional)
-  //      +-+-+-+-+-+-+-+-+-+
-  // L:   |   TL0PIC_IDX    | (optional)
-  //      +-+-+-+-+-+-+-+-+-+
-  // T/K: |TID:Y|  KEYIDX   | (optional)
-  //      +-+-+-+-+-+-+-+-+-+
-
-  RTC_DCHECK_GT(packet_info.size, 0);
-  buffer[0] = 0;
-  if (XFieldPresent())
-    buffer[0] |= kXBit;
-  if (hdr_info_.nonReference)
-    buffer[0] |= kNBit;
-  if (packet_info.first_packet)
-    buffer[0] |= kSBit;
-
-  const int extension_length = WriteExtensionFields(buffer, buffer_length);
-  if (extension_length < 0)
-    return -1;
-
-  memcpy(&buffer[kVp8FixedPayloadDescriptorSize + extension_length],
-         &payload_data_[packet_info.payload_start_pos], packet_info.size);
-
-  // Return total length of written data.
-  return packet_info.size + kVp8FixedPayloadDescriptorSize + extension_length;
-}
-
-int RtpPacketizerVp8::WriteExtensionFields(uint8_t* buffer,
-                                           size_t buffer_length) const {
-  size_t extension_length = 0;
-  if (XFieldPresent()) {
-    uint8_t* x_field = buffer + kVp8FixedPayloadDescriptorSize;
-    *x_field = 0;
-    extension_length = 1;  // One octet for the X field.
-    if (PictureIdPresent()) {
-      if (WritePictureIDFields(x_field, buffer, buffer_length,
-                               &extension_length) < 0) {
-        return -1;
-      }
-    }
-    if (TL0PicIdxFieldPresent()) {
-      if (WriteTl0PicIdxFields(x_field, buffer, buffer_length,
-                               &extension_length) < 0) {
-        return -1;
-      }
-    }
-    if (TIDFieldPresent() || KeyIdxFieldPresent()) {
-      if (WriteTIDAndKeyIdxFields(x_field, buffer, buffer_length,
-                                  &extension_length) < 0) {
-        return -1;
-      }
-    }
-    RTC_DCHECK_EQ(extension_length, PayloadDescriptorExtraLength());
-  }
-  return static_cast<int>(extension_length);
-}
-
-int RtpPacketizerVp8::WritePictureIDFields(uint8_t* x_field,
-                                           uint8_t* buffer,
-                                           size_t buffer_length,
-                                           size_t* extension_length) const {
-  *x_field |= kIBit;
-  RTC_DCHECK_GE(buffer_length,
-                kVp8FixedPayloadDescriptorSize + *extension_length);
-  const int pic_id_length = WritePictureID(
-      buffer + kVp8FixedPayloadDescriptorSize + *extension_length,
-      buffer_length - kVp8FixedPayloadDescriptorSize - *extension_length);
-  if (pic_id_length < 0)
-    return -1;
-  *extension_length += pic_id_length;
-  return 0;
-}
-
-int RtpPacketizerVp8::WritePictureID(uint8_t* buffer,
-                                     size_t buffer_length) const {
-  const uint16_t pic_id = static_cast<uint16_t>(hdr_info_.pictureId);
-  size_t picture_id_len = PictureIdLength();
-  if (picture_id_len > buffer_length)
-    return -1;
-  if (picture_id_len == 2) {
-    buffer[0] = 0x80 | ((pic_id >> 8) & 0x7F);
-    buffer[1] = pic_id & 0xFF;
-  } else if (picture_id_len == 1) {
-    buffer[0] = pic_id & 0x7F;
-  }
-  return static_cast<int>(picture_id_len);
-}
-
-int RtpPacketizerVp8::WriteTl0PicIdxFields(uint8_t* x_field,
-                                           uint8_t* buffer,
-                                           size_t buffer_length,
-                                           size_t* extension_length) const {
-  if (buffer_length < kVp8FixedPayloadDescriptorSize + *extension_length + 1) {
-    return -1;
-  }
-  *x_field |= kLBit;
-  buffer[kVp8FixedPayloadDescriptorSize + *extension_length] =
-      hdr_info_.tl0PicIdx;
-  ++*extension_length;
-  return 0;
-}
-
-int RtpPacketizerVp8::WriteTIDAndKeyIdxFields(uint8_t* x_field,
-                                              uint8_t* buffer,
-                                              size_t buffer_length,
-                                              size_t* extension_length) const {
-  if (buffer_length < kVp8FixedPayloadDescriptorSize + *extension_length + 1) {
-    return -1;
-  }
-  uint8_t* data_field =
-      &buffer[kVp8FixedPayloadDescriptorSize + *extension_length];
-  *data_field = 0;
-  if (TIDFieldPresent()) {
-    *x_field |= kTBit;
-    *data_field |= hdr_info_.temporalIdx << 6;
-    *data_field |= hdr_info_.layerSync ? kYBit : 0;
-  }
-  if (KeyIdxFieldPresent()) {
-    *x_field |= kKBit;
-    *data_field |= (hdr_info_.keyIdx & kKeyIdxField);
-  }
-  ++*extension_length;
-  return 0;
-}
-
-size_t RtpPacketizerVp8::PayloadDescriptorExtraLength() const {
-  size_t length_bytes = PictureIdLength();
-  if (TL0PicIdxFieldPresent())
-    ++length_bytes;
-  if (TIDFieldPresent() || KeyIdxFieldPresent())
-    ++length_bytes;
-  if (length_bytes > 0)
-    ++length_bytes;  // Include the extension field.
-  return length_bytes;
-}
-
-size_t RtpPacketizerVp8::PictureIdLength() const {
-  if (hdr_info_.pictureId == kNoPictureId) {
-    return 0;
-  }
-  return 2;
-}
-
-bool RtpPacketizerVp8::XFieldPresent() const {
-  return (TIDFieldPresent() || TL0PicIdxFieldPresent() || PictureIdPresent() ||
-          KeyIdxFieldPresent());
-}
-
-bool RtpPacketizerVp8::TIDFieldPresent() const {
-  return (hdr_info_.temporalIdx != kNoTemporalIdx);
-}
-
-bool RtpPacketizerVp8::KeyIdxFieldPresent() const {
-  return (hdr_info_.keyIdx != kNoKeyIdx);
-}
-
-bool RtpPacketizerVp8::TL0PicIdxFieldPresent() const {
-  return (hdr_info_.tl0PicIdx != kNoTl0PicIdx);
+  return result;
 }
 
 //
@@ -464,16 +318,16 @@
       beginning_of_partition && (partition_id == 0);
   parsed_payload->video_header().simulcastIdx = 0;
   parsed_payload->video_header().codec = kVideoCodecVP8;
-  parsed_payload->video_header().vp8().nonReference =
-      (*payload_data & 0x20) ? true : false;  // N bit
-  parsed_payload->video_header().vp8().partitionId = partition_id;
-  parsed_payload->video_header().vp8().beginningOfPartition =
-      beginning_of_partition;
-  parsed_payload->video_header().vp8().pictureId = kNoPictureId;
-  parsed_payload->video_header().vp8().tl0PicIdx = kNoTl0PicIdx;
-  parsed_payload->video_header().vp8().temporalIdx = kNoTemporalIdx;
-  parsed_payload->video_header().vp8().layerSync = false;
-  parsed_payload->video_header().vp8().keyIdx = kNoKeyIdx;
+  auto& vp8_header = parsed_payload->video_header()
+                         .video_type_header.emplace<RTPVideoHeaderVP8>();
+  vp8_header.nonReference = (*payload_data & 0x20) ? true : false;  // N bit
+  vp8_header.partitionId = partition_id;
+  vp8_header.beginningOfPartition = beginning_of_partition;
+  vp8_header.pictureId = kNoPictureId;
+  vp8_header.tl0PicIdx = kNoTl0PicIdx;
+  vp8_header.temporalIdx = kNoTemporalIdx;
+  vp8_header.layerSync = false;
+  vp8_header.keyIdx = kNoKeyIdx;
 
   if (partition_id > 8) {
     // Weak check for corrupt payload_data: PartID MUST NOT be larger than 8.
@@ -490,8 +344,7 @@
 
   if (extension) {
     const int parsed_bytes =
-        ParseVP8Extension(&parsed_payload->video_header().vp8(), payload_data,
-                          payload_data_length);
+        ParseVP8Extension(&vp8_header, payload_data, payload_data_length);
     if (parsed_bytes < 0)
       return false;
     payload_data += parsed_bytes;
diff --git a/modules/rtp_rtcp/source/rtp_format_vp8.h b/modules/rtp_rtcp/source/rtp_format_vp8.h
index dd66762..e4bc36e 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp8.h
+++ b/modules/rtp_rtcp/source/rtp_format_vp8.h
@@ -25,10 +25,11 @@
 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
 #define MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_H_
 
-#include <queue>
 #include <string>
 #include <vector>
 
+#include "absl/container/inlined_vector.h"
+#include "api/array_view.h"
 #include "modules/include/module_common_types.h"
 #include "modules/rtp_rtcp/source/rtp_format.h"
 #include "rtc_base/constructormagic.h"
@@ -54,88 +55,14 @@
   bool NextPacket(RtpPacketToSend* packet) override;
 
  private:
-  typedef struct {
-    size_t payload_start_pos;
-    size_t size;
-    bool first_packet;
-  } InfoStruct;
-  typedef std::queue<InfoStruct> InfoQueue;
+  // VP8 header can use up to 6 bytes.
+  using RawHeader = absl::InlinedVector<uint8_t, 6>;
+  static RawHeader BuildHeader(const RTPVideoHeaderVP8& header);
 
-  static const int kXBit = 0x80;
-  static const int kNBit = 0x20;
-  static const int kSBit = 0x10;
-  static const int kPartIdField = 0x0F;
-  static const int kKeyIdxField = 0x1F;
-  static const int kIBit = 0x80;
-  static const int kLBit = 0x40;
-  static const int kTBit = 0x20;
-  static const int kKBit = 0x10;
-  static const int kYBit = 0x20;
-
-  // Calculate all packet sizes and load to packet info queue.
-  void GeneratePackets(size_t payload_len);
-
-  // Insert packet into packet queue.
-  void QueuePacket(size_t start_pos, size_t packet_size, bool first_packet);
-
-  // Write the payload header and copy the payload to the buffer.
-  // The info in packet_info determines which part of the payload is written
-  // and what to write in the header fields.
-  int WriteHeaderAndPayload(const InfoStruct& packet_info,
-                            uint8_t* buffer,
-                            size_t buffer_length) const;
-
-  // Write the X field and the appropriate extension fields to buffer.
-  // The function returns the extension length (including X field), or -1
-  // on error.
-  int WriteExtensionFields(uint8_t* buffer, size_t buffer_length) const;
-
-  // Set the I bit in the x_field, and write PictureID to the appropriate
-  // position in buffer. The function returns 0 on success, -1 otherwise.
-  int WritePictureIDFields(uint8_t* x_field,
-                           uint8_t* buffer,
-                           size_t buffer_length,
-                           size_t* extension_length) const;
-
-  // Set the L bit in the x_field, and write Tl0PicIdx to the appropriate
-  // position in buffer. The function returns 0 on success, -1 otherwise.
-  int WriteTl0PicIdxFields(uint8_t* x_field,
-                           uint8_t* buffer,
-                           size_t buffer_length,
-                           size_t* extension_length) const;
-
-  // Set the T and K bits in the x_field, and write TID, Y and KeyIdx to the
-  // appropriate position in buffer. The function returns 0 on success,
-  // -1 otherwise.
-  int WriteTIDAndKeyIdxFields(uint8_t* x_field,
-                              uint8_t* buffer,
-                              size_t buffer_length,
-                              size_t* extension_length) const;
-
-  // Write the PictureID from codec_specific_info_ to buffer. One or two
-  // bytes are written, depending on magnitude of PictureID. The function
-  // returns the number of bytes written.
-  int WritePictureID(uint8_t* buffer, size_t buffer_length) const;
-
-  // Calculate and return length (octets) of the variable header fields in
-  // the next header (i.e., header length in addition to vp8_header_bytes_).
-  size_t PayloadDescriptorExtraLength() const;
-
-  // Calculate and return length (octets) of PictureID field in the next
-  // header. Can be 0, 1, or 2.
-  size_t PictureIdLength() const;
-
-  // Check whether each of the optional fields will be included in the header.
-  bool XFieldPresent() const;
-  bool TIDFieldPresent() const;
-  bool KeyIdxFieldPresent() const;
-  bool TL0PicIdxFieldPresent() const;
-  bool PictureIdPresent() const { return (PictureIdLength() > 0); }
-
-  const uint8_t* payload_data_;
-  const RTPVideoHeaderVP8 hdr_info_;
-  const PayloadSizeLimits limits_;
-  InfoQueue packets_;
+  RawHeader hdr_;
+  rtc::ArrayView<const uint8_t> remaining_payload_;
+  std::vector<int> payload_sizes_;
+  std::vector<int>::const_iterator current_packet_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(RtpPacketizerVp8);
 };
diff --git a/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.cc b/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.cc
index abc0c89..174e24c 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.cc
@@ -10,68 +10,17 @@
 
 #include "modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h"
 
+#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
+#include "test/gmock.h"
 #include "test/gtest.h"
 
 namespace webrtc {
+namespace {
 
-namespace test {
+using ::testing::ElementsAreArray;
 
 constexpr RtpPacketToSend::ExtensionManager* kNoExtensions = nullptr;
 
-RtpFormatVp8TestHelper::RtpFormatVp8TestHelper(const RTPVideoHeaderVP8* hdr)
-    : packet_(kNoExtensions),
-      payload_data_(NULL),
-      data_ptr_(NULL),
-      hdr_info_(hdr),
-      payload_start_(0),
-      payload_size_(0),
-      sloppy_partitioning_(false),
-      inited_(false) {}
-
-RtpFormatVp8TestHelper::~RtpFormatVp8TestHelper() {
-  delete[] payload_data_;
-}
-
-bool RtpFormatVp8TestHelper::Init(const size_t* partition_sizes,
-                                  size_t num_partitions) {
-  if (inited_)
-    return false;
-  payload_size_ = 0;
-  // Calculate sum payload size.
-  for (size_t p = 0; p < num_partitions; ++p) {
-    payload_size_ += partition_sizes[p];
-  }
-  payload_data_ = new uint8_t[payload_size_];
-  size_t j = 0;
-  // Loop through the partitions again.
-  for (size_t p = 0; p < num_partitions; ++p) {
-    for (size_t i = 0; i < partition_sizes[p]; ++i) {
-      assert(j < payload_size_);
-      payload_data_[j++] = p;  // Set the payload value to the partition index.
-    }
-  }
-  data_ptr_ = payload_data_;
-  inited_ = true;
-  return true;
-}
-
-void RtpFormatVp8TestHelper::GetAllPacketsAndCheck(
-    RtpPacketizerVp8* packetizer,
-    const size_t* expected_sizes,
-    const int* expected_part,
-    const bool* expected_frag_start,
-    size_t expected_num_packets) {
-  ASSERT_TRUE(inited_);
-  for (size_t i = 0; i < expected_num_packets; ++i) {
-    std::ostringstream ss;
-    ss << "Checking packet " << i;
-    SCOPED_TRACE(ss.str());
-    EXPECT_TRUE(packetizer->NextPacket(&packet_));
-    CheckPacket(expected_sizes[i], i + 1 == expected_num_packets,
-                expected_frag_start[i]);
-  }
-}
-
 // Payload descriptor
 //       0 1 2 3 4 5 6 7
 //      +-+-+-+-+-+-+-+-+
@@ -79,159 +28,145 @@
 //      +-+-+-+-+-+-+-+-+
 // X:   |I|L|T|K|  RSV  | (OPTIONAL)
 //      +-+-+-+-+-+-+-+-+
-// I:   |   PictureID   | (OPTIONAL)
+//      |M| PictureID   |
+// I:   +-+-+-+-+-+-+-+-+ (OPTIONAL)
+//      |   PictureID   |
 //      +-+-+-+-+-+-+-+-+
 // L:   |   TL0PICIDX   | (OPTIONAL)
 //      +-+-+-+-+-+-+-+-+
-// T/K: | TID | KEYIDX  | (OPTIONAL)
+// T/K: |TID|Y| KEYIDX  | (OPTIONAL)
 //      +-+-+-+-+-+-+-+-+
 
-// First octet tests.
-#define EXPECT_BIT_EQ(x, n, a) EXPECT_EQ((((x) >> (n)) & 0x1), a)
+int Bit(uint8_t byte, int position) {
+  return (byte >> position) & 0x01;
+}
 
-#define EXPECT_RSV_ZERO(x) EXPECT_EQ(((x)&0xE0), 0)
+}  // namespace
 
-#define EXPECT_BIT_X_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
+RtpFormatVp8TestHelper::RtpFormatVp8TestHelper(const RTPVideoHeaderVP8* hdr,
+                                               size_t payload_len)
+    : hdr_info_(hdr), payload_(payload_len) {
+  for (size_t i = 0; i < payload_.size(); ++i) {
+    payload_[i] = i;
+  }
+}
 
-#define EXPECT_BIT_N_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
+RtpFormatVp8TestHelper::~RtpFormatVp8TestHelper() = default;
 
-#define EXPECT_BIT_S_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
+void RtpFormatVp8TestHelper::GetAllPacketsAndCheck(
+    RtpPacketizerVp8* packetizer,
+    rtc::ArrayView<const size_t> expected_sizes) {
+  EXPECT_EQ(packetizer->NumPackets(), expected_sizes.size());
+  const uint8_t* data_ptr = payload_.begin();
+  RtpPacketToSend packet(kNoExtensions);
+  for (size_t i = 0; i < expected_sizes.size(); ++i) {
+    EXPECT_TRUE(packetizer->NextPacket(&packet));
+    auto rtp_payload = packet.payload();
+    EXPECT_EQ(rtp_payload.size(), expected_sizes[i]);
 
-#define EXPECT_PART_ID_EQ(x, a) EXPECT_EQ(((x)&0x0F), a)
+    int payload_offset = CheckHeader(rtp_payload, /*first=*/i == 0);
+    // Verify that the payload (i.e., after the headers) of the packet is
+    // identical to the expected (as found in data_ptr).
+    auto vp8_payload = rtp_payload.subview(payload_offset);
+    ASSERT_GE(payload_.end() - data_ptr, static_cast<int>(vp8_payload.size()));
+    EXPECT_THAT(vp8_payload, ElementsAreArray(data_ptr, vp8_payload.size()));
+    data_ptr += vp8_payload.size();
+  }
+  EXPECT_EQ(payload_.end() - data_ptr, 0);
+}
 
-// Extension fields tests
-#define EXPECT_BIT_I_EQ(x, a) EXPECT_BIT_EQ(x, 7, a)
+int RtpFormatVp8TestHelper::CheckHeader(rtc::ArrayView<const uint8_t> buffer,
+                                        bool first) {
+  int x_bit = Bit(buffer[0], 7);
+  EXPECT_EQ(Bit(buffer[0], 6), 0);  // Reserved.
+  EXPECT_EQ(Bit(buffer[0], 5), hdr_info_->nonReference ? 1 : 0);
+  EXPECT_EQ(Bit(buffer[0], 4), first ? 1 : 0);
+  EXPECT_EQ(buffer[0] & 0x0f, 0);  // RtpPacketizerVp8 always uses partition 0.
 
-#define EXPECT_BIT_L_EQ(x, a) EXPECT_BIT_EQ(x, 6, a)
-
-#define EXPECT_BIT_T_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
-
-#define EXPECT_BIT_K_EQ(x, a) EXPECT_BIT_EQ(x, 4, a)
-
-#define EXPECT_TID_EQ(x, a) EXPECT_EQ((((x)&0xC0) >> 6), a)
-
-#define EXPECT_BIT_Y_EQ(x, a) EXPECT_BIT_EQ(x, 5, a)
-
-#define EXPECT_KEYIDX_EQ(x, a) EXPECT_EQ(((x)&0x1F), a)
-
-void RtpFormatVp8TestHelper::CheckHeader(bool frag_start) {
-  payload_start_ = 1;
-  rtc::ArrayView<const uint8_t> buffer = packet_.payload();
-  EXPECT_BIT_EQ(buffer[0], 6, 0);  // Check reserved bit.
-  EXPECT_PART_ID_EQ(buffer[0], 0);  // In equal size mode, PartID is always 0.
-
+  int payload_offset = 1;
   if (hdr_info_->pictureId != kNoPictureId ||
       hdr_info_->temporalIdx != kNoTemporalIdx ||
       hdr_info_->tl0PicIdx != kNoTl0PicIdx || hdr_info_->keyIdx != kNoKeyIdx) {
-    EXPECT_BIT_X_EQ(buffer[0], 1);
-    ++payload_start_;
-    CheckPictureID();
-    CheckTl0PicIdx();
-    CheckTIDAndKeyIdx();
+    EXPECT_EQ(x_bit, 1);
+    ++payload_offset;
+    CheckPictureID(buffer, &payload_offset);
+    CheckTl0PicIdx(buffer, &payload_offset);
+    CheckTIDAndKeyIdx(buffer, &payload_offset);
+    EXPECT_EQ(buffer[1] & 0x07, 0);  // Reserved.
   } else {
-    EXPECT_BIT_X_EQ(buffer[0], 0);
+    EXPECT_EQ(x_bit, 0);
   }
 
-  EXPECT_BIT_N_EQ(buffer[0], hdr_info_->nonReference ? 1 : 0);
-  EXPECT_BIT_S_EQ(buffer[0], frag_start ? 1 : 0);
-
-  // Check partition index.
-  if (!sloppy_partitioning_) {
-    // The test payload data is constructed such that the payload value is the
-    // same as the partition index.
-    EXPECT_EQ(buffer[0] & 0x0F, buffer[payload_start_]);
-  } else {
-    // Partition should be set to 0.
-    EXPECT_EQ(buffer[0] & 0x0F, 0);
-  }
+  return payload_offset;
 }
 
 // Verify that the I bit and the PictureID field are both set in accordance
 // with the information in hdr_info_->pictureId.
-void RtpFormatVp8TestHelper::CheckPictureID() {
-  auto buffer = packet_.payload();
+void RtpFormatVp8TestHelper::CheckPictureID(
+    rtc::ArrayView<const uint8_t> buffer,
+    int* offset) {
+  int i_bit = Bit(buffer[1], 7);
   if (hdr_info_->pictureId != kNoPictureId) {
-    EXPECT_BIT_I_EQ(buffer[1], 1);
-    EXPECT_BIT_EQ(buffer[payload_start_], 7, 1);
-    EXPECT_EQ(buffer[payload_start_] & 0x7F,
-              (hdr_info_->pictureId >> 8) & 0x7F);
-    EXPECT_EQ(buffer[payload_start_ + 1], hdr_info_->pictureId & 0xFF);
-    payload_start_ += 2;
+    EXPECT_EQ(i_bit, 1);
+    int two_byte_picture_id = Bit(buffer[*offset], 7);
+    EXPECT_EQ(two_byte_picture_id, 1);
+    EXPECT_EQ(buffer[*offset] & 0x7F, (hdr_info_->pictureId >> 8) & 0x7F);
+    EXPECT_EQ(buffer[(*offset) + 1], hdr_info_->pictureId & 0xFF);
+    (*offset) += 2;
   } else {
-    EXPECT_BIT_I_EQ(buffer[1], 0);
+    EXPECT_EQ(i_bit, 0);
   }
 }
 
 // Verify that the L bit and the TL0PICIDX field are both set in accordance
 // with the information in hdr_info_->tl0PicIdx.
-void RtpFormatVp8TestHelper::CheckTl0PicIdx() {
-  auto buffer = packet_.payload();
+void RtpFormatVp8TestHelper::CheckTl0PicIdx(
+    rtc::ArrayView<const uint8_t> buffer,
+    int* offset) {
+  int l_bit = Bit(buffer[1], 6);
   if (hdr_info_->tl0PicIdx != kNoTl0PicIdx) {
-    EXPECT_BIT_L_EQ(buffer[1], 1);
-    EXPECT_EQ(buffer[payload_start_], hdr_info_->tl0PicIdx);
-    ++payload_start_;
+    EXPECT_EQ(l_bit, 1);
+    EXPECT_EQ(buffer[*offset], hdr_info_->tl0PicIdx);
+    ++*offset;
   } else {
-    EXPECT_BIT_L_EQ(buffer[1], 0);
+    EXPECT_EQ(l_bit, 0);
   }
 }
 
 // Verify that the T bit and the TL0PICIDX field, and the K bit and KEYIDX
 // field are all set in accordance with the information in
 // hdr_info_->temporalIdx and hdr_info_->keyIdx, respectively.
-void RtpFormatVp8TestHelper::CheckTIDAndKeyIdx() {
-  auto buffer = packet_.payload();
+void RtpFormatVp8TestHelper::CheckTIDAndKeyIdx(
+    rtc::ArrayView<const uint8_t> buffer,
+    int* offset) {
+  int t_bit = Bit(buffer[1], 5);
+  int k_bit = Bit(buffer[1], 4);
   if (hdr_info_->temporalIdx == kNoTemporalIdx &&
       hdr_info_->keyIdx == kNoKeyIdx) {
-    EXPECT_BIT_T_EQ(buffer[1], 0);
-    EXPECT_BIT_K_EQ(buffer[1], 0);
+    EXPECT_EQ(t_bit, 0);
+    EXPECT_EQ(k_bit, 0);
     return;
   }
+  int temporal_id = (buffer[*offset] & 0xC0) >> 6;
+  int y_bit = Bit(buffer[*offset], 5);
+  int key_idx = buffer[*offset] & 0x1f;
   if (hdr_info_->temporalIdx != kNoTemporalIdx) {
-    EXPECT_BIT_T_EQ(buffer[1], 1);
-    EXPECT_TID_EQ(buffer[payload_start_], hdr_info_->temporalIdx);
-    EXPECT_BIT_Y_EQ(buffer[payload_start_], hdr_info_->layerSync ? 1 : 0);
+    EXPECT_EQ(t_bit, 1);
+    EXPECT_EQ(temporal_id, hdr_info_->temporalIdx);
+    EXPECT_EQ(y_bit, hdr_info_->layerSync ? 1 : 0);
   } else {
-    EXPECT_BIT_T_EQ(buffer[1], 0);
-    EXPECT_TID_EQ(buffer[payload_start_], 0);
-    EXPECT_BIT_Y_EQ(buffer[payload_start_], 0);
+    EXPECT_EQ(t_bit, 0);
+    EXPECT_EQ(temporal_id, 0);
+    EXPECT_EQ(y_bit, 0);
   }
   if (hdr_info_->keyIdx != kNoKeyIdx) {
-    EXPECT_BIT_K_EQ(buffer[1], 1);
-    EXPECT_KEYIDX_EQ(buffer[payload_start_], hdr_info_->keyIdx);
+    EXPECT_EQ(k_bit, 1);
+    EXPECT_EQ(key_idx, hdr_info_->keyIdx);
   } else {
-    EXPECT_BIT_K_EQ(buffer[1], 0);
-    EXPECT_KEYIDX_EQ(buffer[payload_start_], 0);
+    EXPECT_EQ(k_bit, 0);
+    EXPECT_EQ(key_idx, 0);
   }
-  ++payload_start_;
+  ++*offset;
 }
 
-// Verify that the payload (i.e., after the headers) of the packet stored in
-// buffer_ is identical to the expected (as found in data_ptr_).
-void RtpFormatVp8TestHelper::CheckPayload() {
-  auto buffer = packet_.payload();
-  size_t payload_end = buffer.size();
-  for (size_t i = payload_start_; i < payload_end; ++i, ++data_ptr_)
-    EXPECT_EQ(buffer[i], *data_ptr_);
-}
-
-// Verify that the input variable "last" agrees with the position of data_ptr_.
-// If data_ptr_ has advanced payload_size_ bytes from the start (payload_data_)
-// we are at the end and last should be true. Otherwise, it should be false.
-void RtpFormatVp8TestHelper::CheckLast(bool last) const {
-  EXPECT_EQ(last, data_ptr_ == payload_data_ + payload_size_);
-}
-
-// Verify the contents of a packet. Check the length versus expected_bytes,
-// the header, payload, and "last" flag.
-void RtpFormatVp8TestHelper::CheckPacket(size_t expect_bytes,
-                                         bool last,
-                                         bool frag_start) {
-  EXPECT_EQ(expect_bytes, packet_.payload_size());
-  CheckHeader(frag_start);
-  CheckPayload();
-  CheckLast(last);
-}
-
-}  // namespace test
-
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h b/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h
index 7d5a090..07f5f64 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h
+++ b/modules/rtp_rtcp/source/rtp_format_vp8_test_helper.h
@@ -18,59 +18,39 @@
 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_TEST_HELPER_H_
 #define MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_TEST_HELPER_H_
 
-#include "modules/include/module_common_types.h"
+#include "api/array_view.h"
 #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
-#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
+#include "modules/video_coding/codecs/vp8/include/vp8_globals.h"
+#include "rtc_base/buffer.h"
 #include "rtc_base/constructormagic.h"
 
 namespace webrtc {
 
-namespace test {
-
 class RtpFormatVp8TestHelper {
  public:
-  explicit RtpFormatVp8TestHelper(const RTPVideoHeaderVP8* hdr);
+  RtpFormatVp8TestHelper(const RTPVideoHeaderVP8* hdr, size_t payload_len);
   ~RtpFormatVp8TestHelper();
-  bool Init(const size_t* partition_sizes, size_t num_partitions);
   void GetAllPacketsAndCheck(RtpPacketizerVp8* packetizer,
-                             const size_t* expected_sizes,
-                             const int* expected_part,
-                             const bool* expected_frag_start,
-                             size_t expected_num_packets);
+                             rtc::ArrayView<const size_t> expected_sizes);
 
-  rtc::ArrayView<const uint8_t> payload() const {
-    return rtc::ArrayView<const uint8_t>(payload_data_, payload_size_);
-  }
-  size_t payload_size() const { return payload_size_; }
-  size_t buffer_size() const {
-    static constexpr size_t kVp8PayloadDescriptorMaxSize = 6;
-    return payload_size_ + kVp8PayloadDescriptorMaxSize;
-  }
-  void set_sloppy_partitioning(bool value) { sloppy_partitioning_ = value; }
+  rtc::ArrayView<const uint8_t> payload() const { return payload_; }
+  size_t payload_size() const { return payload_.size(); }
 
  private:
-  void CheckHeader(bool frag_start);
-  void CheckPictureID();
-  void CheckTl0PicIdx();
-  void CheckTIDAndKeyIdx();
-  void CheckPayload();
-  void CheckLast(bool last) const;
-  void CheckPacket(size_t expect_bytes, bool last, bool frag_start);
+  // Returns header size, i.e. payload offset.
+  int CheckHeader(rtc::ArrayView<const uint8_t> rtp_payload, bool first);
+  void CheckPictureID(rtc::ArrayView<const uint8_t> rtp_payload, int* offset);
+  void CheckTl0PicIdx(rtc::ArrayView<const uint8_t> rtp_payload, int* offset);
+  void CheckTIDAndKeyIdx(rtc::ArrayView<const uint8_t> rtp_payload,
+                         int* offset);
+  void CheckPayload(const uint8_t* data_ptr);
 
-  RtpPacketToSend packet_;
-  uint8_t* payload_data_;
-  uint8_t* data_ptr_;
-  const RTPVideoHeaderVP8* hdr_info_;
-  int payload_start_;
-  size_t payload_size_;
-  bool sloppy_partitioning_;
-  bool inited_;
+  const RTPVideoHeaderVP8* const hdr_info_;
+  rtc::Buffer payload_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(RtpFormatVp8TestHelper);
 };
 
-}  // namespace test
-
 }  // namespace webrtc
 
 #endif  // MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP8_TEST_HELPER_H_
diff --git a/modules/rtp_rtcp/source/rtp_format_vp8_unittest.cc b/modules/rtp_rtcp/source/rtp_format_vp8_unittest.cc
index b1096d8..f9cc48c 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp8_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp8_unittest.cc
@@ -16,10 +16,6 @@
 #include "test/gmock.h"
 #include "test/gtest.h"
 
-#define CHECK_ARRAY_SIZE(expected_size, array)                     \
-  static_assert(expected_size == sizeof(array) / sizeof(array[0]), \
-                "check array size");
-
 namespace webrtc {
 namespace {
 
@@ -27,6 +23,7 @@
 using ::testing::make_tuple;
 
 constexpr RtpPacketToSend::ExtensionManager* kNoExtensions = nullptr;
+constexpr RtpPacketizer::PayloadSizeLimits kNoSizeLimits;
 // Payload descriptor
 //       0 1 2 3 4 5 6 7
 //      +-+-+-+-+-+-+-+-+
@@ -60,9 +57,11 @@
 //      +-+-+-+-+-+-+-+-+
 void VerifyBasicHeader(RTPVideoHeader* header, bool N, bool S, int part_id) {
   ASSERT_TRUE(header != NULL);
-  EXPECT_EQ(N, header->vp8().nonReference);
-  EXPECT_EQ(S, header->vp8().beginningOfPartition);
-  EXPECT_EQ(part_id, header->vp8().partitionId);
+  const auto& vp8_header =
+      absl::get<RTPVideoHeaderVP8>(header->video_type_header);
+  EXPECT_EQ(N, vp8_header.nonReference);
+  EXPECT_EQ(S, vp8_header.beginningOfPartition);
+  EXPECT_EQ(part_id, vp8_header.partitionId);
 }
 
 void VerifyExtensions(RTPVideoHeader* header,
@@ -71,72 +70,40 @@
                       uint8_t temporal_idx, /* T */
                       int key_idx /* K */) {
   ASSERT_TRUE(header != NULL);
-  EXPECT_EQ(picture_id, header->vp8().pictureId);
-  EXPECT_EQ(tl0_pic_idx, header->vp8().tl0PicIdx);
-  EXPECT_EQ(temporal_idx, header->vp8().temporalIdx);
-  EXPECT_EQ(key_idx, header->vp8().keyIdx);
+  const auto& vp8_header =
+      absl::get<RTPVideoHeaderVP8>(header->video_type_header);
+  EXPECT_EQ(picture_id, vp8_header.pictureId);
+  EXPECT_EQ(tl0_pic_idx, vp8_header.tl0PicIdx);
+  EXPECT_EQ(temporal_idx, vp8_header.temporalIdx);
+  EXPECT_EQ(key_idx, vp8_header.keyIdx);
 }
+
 }  // namespace
 
-class RtpPacketizerVp8Test : public ::testing::Test {
- protected:
-  RtpPacketizerVp8Test() : helper_(NULL) {}
-  void TearDown() override { delete helper_; }
-  bool Init(const size_t* partition_sizes, size_t num_partitions) {
-    hdr_info_.pictureId = kNoPictureId;
-    hdr_info_.nonReference = false;
-    hdr_info_.temporalIdx = kNoTemporalIdx;
-    hdr_info_.layerSync = false;
-    hdr_info_.tl0PicIdx = kNoTl0PicIdx;
-    hdr_info_.keyIdx = kNoKeyIdx;
-    if (helper_ != NULL)
-      return false;
-    helper_ = new test::RtpFormatVp8TestHelper(&hdr_info_);
-    return helper_->Init(partition_sizes, num_partitions);
-  }
+TEST(RtpPacketizerVp8Test, ResultPacketsAreAlmostEqualSize) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.pictureId = 200;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/30);
 
-  RTPVideoHeaderVP8 hdr_info_;
-  test::RtpFormatVp8TestHelper* helper_;
-};
-
-// Verify that EqualSize mode is forced if fragmentation info is missing.
-TEST_F(RtpPacketizerVp8Test, TestEqualSizeModeFallback) {
-  const size_t kSizeVector[] = {10, 10, 10};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
-
-  hdr_info_.pictureId = 200;          // > 0x7F should produce 2-byte PictureID
   RtpPacketizer::PayloadSizeLimits limits;
   limits.max_payload_len = 12;  // Small enough to produce 4 packets.
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), limits, hdr_info);
 
-  // Expecting three full packets, and one with the remainder.
   const size_t kExpectedSizes[] = {11, 11, 12, 12};
-  const int kExpectedPart[] = {0, 0, 0, 0};  // Always 0 for equal size mode.
-  // Frag start only true for first packet in equal size mode.
-  const bool kExpectedFragStart[] = {true, false, false, false};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->set_sloppy_partitioning(true);
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
-TEST_F(RtpPacketizerVp8Test, TestEqualSizeWithLastPacketReduction) {
-  const size_t kSizeVector[] = {30, 10, 3};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
+TEST(RtpPacketizerVp8Test, EqualSizeWithLastPacketReduction) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.pictureId = 200;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/43);
 
-  hdr_info_.pictureId = 200;
   RtpPacketizer::PayloadSizeLimits limits;
   limits.max_payload_len = 15;  // Small enough to produce 5 packets.
   limits.last_packet_reduction_len = 5;
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), limits, hdr_info);
 
   // Calculated by hand. VP8 payload descriptors are 4 byte each. 5 packets is
   // minimum possible to fit 43 payload bytes into packets with capacity of
@@ -144,125 +111,63 @@
   // almost equal in size, even last packet if counted with free space (which
   // will be filled up the stack by extra long RTP header).
   const size_t kExpectedSizes[] = {13, 13, 14, 14, 9};
-  const int kExpectedPart[] = {0, 0, 0, 0, 0};  // Always 0 for equal size mode.
-  // Frag start only true for first packet in equal size mode.
-  const bool kExpectedFragStart[] = {true, false, false, false, false};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->set_sloppy_partitioning(true);
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
-// Verify that non-reference bit is set. EqualSize mode fallback is expected.
-TEST_F(RtpPacketizerVp8Test, TestNonReferenceBit) {
-  const size_t kSizeVector[] = {10, 10, 10};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
+// Verify that non-reference bit is set.
+TEST(RtpPacketizerVp8Test, NonReferenceBit) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.nonReference = true;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/30);
 
-  hdr_info_.nonReference = true;
   RtpPacketizer::PayloadSizeLimits limits;
   limits.max_payload_len = 25;  // Small enough to produce two packets.
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), limits, hdr_info);
 
-  // EqualSize mode => First packet full; other not.
   const size_t kExpectedSizes[] = {16, 16};
-  const int kExpectedPart[] = {0, 0};  // Always 0 for equal size mode.
-  // Frag start only true for first packet in equal size mode.
-  const bool kExpectedFragStart[] = {true, false};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->set_sloppy_partitioning(true);
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
 // Verify Tl0PicIdx and TID fields, and layerSync bit.
-TEST_F(RtpPacketizerVp8Test, TestTl0PicIdxAndTID) {
-  const size_t kSizeVector[] = {10, 10, 10};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
+TEST(RtpPacketizerVp8Test, Tl0PicIdxAndTID) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.tl0PicIdx = 117;
+  hdr_info.temporalIdx = 2;
+  hdr_info.layerSync = true;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/30);
 
-  hdr_info_.tl0PicIdx = 117;
-  hdr_info_.temporalIdx = 2;
-  hdr_info_.layerSync = true;
-  RtpPacketizer::PayloadSizeLimits limits;
-  // max_payload_len is only limited by allocated buffer size.
-  limits.max_payload_len = helper_->buffer_size();
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), kNoSizeLimits, hdr_info);
 
-  // Expect one single packet of payload_size() + 4 bytes header.
-  const size_t kExpectedSizes[1] = {helper_->payload_size() + 4};
-  const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
-  const bool kExpectedFragStart[1] = {true};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  const size_t kExpectedSizes[1] = {helper.payload_size() + 4};
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
-// Verify KeyIdx field.
-TEST_F(RtpPacketizerVp8Test, TestKeyIdx) {
-  const size_t kSizeVector[] = {10, 10, 10};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
+TEST(RtpPacketizerVp8Test, KeyIdx) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.keyIdx = 17;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/30);
 
-  hdr_info_.keyIdx = 17;
-  RtpPacketizer::PayloadSizeLimits limits;
-  // max payload len is only limited by allocated buffer size.
-  limits.max_payload_len = helper_->buffer_size();
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), kNoSizeLimits, hdr_info);
 
-  // Expect one single packet of payload_size() + 3 bytes header.
-  const size_t kExpectedSizes[1] = {helper_->payload_size() + 3};
-  const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
-  const bool kExpectedFragStart[1] = {true};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  const size_t kExpectedSizes[1] = {helper.payload_size() + 3};
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
 // Verify TID field and KeyIdx field in combination.
-TEST_F(RtpPacketizerVp8Test, TestTIDAndKeyIdx) {
-  const size_t kSizeVector[] = {10, 10, 10};
-  const size_t kNumPartitions = GTEST_ARRAY_SIZE_(kSizeVector);
-  ASSERT_TRUE(Init(kSizeVector, kNumPartitions));
+TEST(RtpPacketizerVp8Test, TIDAndKeyIdx) {
+  RTPVideoHeaderVP8 hdr_info;
+  hdr_info.InitRTPVideoHeaderVP8();
+  hdr_info.temporalIdx = 1;
+  hdr_info.keyIdx = 5;
+  RtpFormatVp8TestHelper helper(&hdr_info, /*payload_len=*/30);
 
-  hdr_info_.temporalIdx = 1;
-  hdr_info_.keyIdx = 5;
-  RtpPacketizer::PayloadSizeLimits limits;
-  // max_payload_len is only limited by allocated buffer size.
-  limits.max_payload_len = helper_->buffer_size();
-  RtpPacketizerVp8 packetizer(helper_->payload(), limits, hdr_info_);
-  size_t num_packets = packetizer.NumPackets();
+  RtpPacketizerVp8 packetizer(helper.payload(), kNoSizeLimits, hdr_info);
 
-  // Expect one single packet of payload_size() + 3 bytes header.
-  const size_t kExpectedSizes[1] = {helper_->payload_size() + 3};
-  const int kExpectedPart[1] = {0};  // Packet starts with partition 0.
-  const bool kExpectedFragStart[1] = {true};
-  const size_t kExpectedNum = GTEST_ARRAY_SIZE_(kExpectedSizes);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedPart);
-  CHECK_ARRAY_SIZE(kExpectedNum, kExpectedFragStart);
-  ASSERT_EQ(num_packets, kExpectedNum);
-
-  helper_->GetAllPacketsAndCheck(&packetizer, kExpectedSizes, kExpectedPart,
-                                 kExpectedFragStart, kExpectedNum);
+  const size_t kExpectedSizes[1] = {helper.payload_size() + 3};
+  helper.GetAllPacketsAndCheck(&packetizer, kExpectedSizes);
 }
 
 class RtpDepacketizerVp8Test : public ::testing::Test {
@@ -274,10 +179,9 @@
                     const uint8_t* data,
                     size_t length) {
     ASSERT_TRUE(parsed_payload != NULL);
-    EXPECT_THAT(std::vector<uint8_t>(
-                    parsed_payload->payload,
-                    parsed_payload->payload + parsed_payload->payload_length),
-                ::testing::ElementsAreArray(data, length));
+    EXPECT_THAT(
+        make_tuple(parsed_payload->payload, parsed_payload->payload_length),
+        ElementsAreArray(data, length));
   }
 
   std::unique_ptr<RtpDepacketizer> depacketizer_;
@@ -368,7 +272,9 @@
   VerifyBasicHeader(&payload.video_header(), 0, 0, 8);
   VerifyExtensions(&payload.video_header(), kNoPictureId, kNoTl0PicIdx, 2,
                    kNoKeyIdx);
-  EXPECT_FALSE(payload.video_header().vp8().layerSync);
+  EXPECT_FALSE(
+      absl::get<RTPVideoHeaderVP8>(payload.video_header().video_type_header)
+          .layerSync);
 }
 
 TEST_F(RtpDepacketizerVp8Test, KeyIdx) {
@@ -451,7 +357,10 @@
   VerifyExtensions(&payload.video_header(), input_header.pictureId,
                    input_header.tl0PicIdx, input_header.temporalIdx,
                    input_header.keyIdx);
-  EXPECT_EQ(payload.video_header().vp8().layerSync, input_header.layerSync);
+  EXPECT_EQ(
+      absl::get<RTPVideoHeaderVP8>(payload.video_header().video_type_header)
+          .layerSync,
+      input_header.layerSync);
 }
 
 TEST_F(RtpDepacketizerVp8Test, TestEmptyPayload) {
diff --git a/modules/rtp_rtcp/source/rtp_format_vp9.cc b/modules/rtp_rtcp/source/rtp_format_vp9.cc
index f800ff8..4419d1a 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp9.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp9.cc
@@ -10,7 +10,6 @@
 
 #include "modules/rtp_rtcp/source/rtp_format_vp9.h"
 
-#include <assert.h>
 #include <string.h>
 
 #include <cmath>
@@ -147,23 +146,6 @@
          LayerInfoLength(hdr) + RefIndicesLength(hdr);
 }
 
-size_t PayloadDescriptorLength(const RTPVideoHeaderVP9& hdr) {
-  return PayloadDescriptorLengthMinusSsData(hdr) + SsDataLength(hdr);
-}
-
-void QueuePacket(size_t start_pos,
-                 size_t size,
-                 bool layer_begin,
-                 bool layer_end,
-                 RtpPacketizerVp9::PacketInfoQueue* packets) {
-  RtpPacketizerVp9::PacketInfo packet_info;
-  packet_info.payload_start_pos = start_pos;
-  packet_info.size = size;
-  packet_info.layer_begin = layer_begin;
-  packet_info.layer_end = layer_end;
-  packets->push(packet_info);
-}
-
 // Picture ID:
 //
 //      +-+-+-+-+-+-+-+-+
@@ -460,128 +442,57 @@
 }
 }  // namespace
 
-RtpPacketizerVp9::RtpPacketizerVp9(const RTPVideoHeaderVP9& hdr,
-                                   size_t max_payload_length,
-                                   size_t last_packet_reduction_len)
+RtpPacketizerVp9::RtpPacketizerVp9(rtc::ArrayView<const uint8_t> payload,
+                                   PayloadSizeLimits limits,
+                                   const RTPVideoHeaderVP9& hdr)
     : hdr_(hdr),
-      max_payload_length_(max_payload_length),
-      payload_(nullptr),
-      payload_size_(0),
-      last_packet_reduction_len_(last_packet_reduction_len) {}
+      header_size_(PayloadDescriptorLengthMinusSsData(hdr_)),
+      first_packet_extra_header_size_(SsDataLength(hdr_)),
+      remaining_payload_(payload) {
+  limits.max_payload_len -= header_size_;
+  limits.first_packet_reduction_len += first_packet_extra_header_size_;
 
-RtpPacketizerVp9::~RtpPacketizerVp9() {}
-
-size_t RtpPacketizerVp9::SetPayloadData(
-    const uint8_t* payload,
-    size_t payload_size,
-    const RTPFragmentationHeader* fragmentation) {
-  payload_ = payload;
-  payload_size_ = payload_size;
-  GeneratePackets();
-  return packets_.size();
+  payload_sizes_ = SplitAboutEqually(payload.size(), limits);
+  current_packet_ = payload_sizes_.begin();
 }
 
+RtpPacketizerVp9::~RtpPacketizerVp9() = default;
+
 size_t RtpPacketizerVp9::NumPackets() const {
-  return packets_.size();
-}
-
-// Splits payload in minimal number of roughly equal in size packets.
-void RtpPacketizerVp9::GeneratePackets() {
-  if (max_payload_length_ < PayloadDescriptorLength(hdr_) + 1) {
-    RTC_LOG(LS_ERROR) << "Payload header and one payload byte won't fit in the "
-                         "first packet.";
-    return;
-  }
-  if (max_payload_length_ < PayloadDescriptorLengthMinusSsData(hdr_) + 1 +
-                                last_packet_reduction_len_) {
-    RTC_LOG(LS_ERROR)
-        << "Payload header and one payload byte won't fit in the last"
-           " packet.";
-    return;
-  }
-  if (payload_size_ == 1 &&
-      max_payload_length_ <
-          PayloadDescriptorLength(hdr_) + 1 + last_packet_reduction_len_) {
-    RTC_LOG(LS_ERROR) << "Can't fit header and payload into single packet, but "
-                         "payload size is one: no way to generate packets with "
-                         "nonzero payload.";
-    return;
-  }
-
-  // Instead of making last packet smaller, we pretend that we must write
-  // additional data into it. We account for this virtual payload while
-  // calculating packets number and sizes. We also pretend that all packets
-  // headers are the same length and extra SS header data in the fits packet
-  // is also treated as a payload here.
-
-  size_t ss_data_len = SsDataLength(hdr_);
-  // Payload, virtual payload and SS hdr data in the first packet together.
-  size_t total_bytes = ss_data_len + payload_size_ + last_packet_reduction_len_;
-  // Now all packets will have the same lenght of vp9 headers.
-  size_t per_packet_capacity =
-      max_payload_length_ - PayloadDescriptorLengthMinusSsData(hdr_);
-  // Integer division rounding up.
-  size_t num_packets =
-      (total_bytes + per_packet_capacity - 1) / per_packet_capacity;
-  // Average rounded down.
-  size_t per_packet_bytes = total_bytes / num_packets;
-  // Several last packets are 1 byte larger than the rest.
-  // i.e. if 14 bytes were split between 4 packets, it would be 3+3+4+4.
-  size_t num_larger_packets = total_bytes % num_packets;
-  size_t bytes_processed = 0;
-  size_t num_packets_left = num_packets;
-  while (bytes_processed < payload_size_) {
-    if (num_packets_left == num_larger_packets)
-      ++per_packet_bytes;
-    size_t packet_bytes = per_packet_bytes;
-    // First packet also has SS hdr data.
-    if (bytes_processed == 0) {
-      // Must write at least one byte of the real payload to the packet.
-      if (packet_bytes > ss_data_len) {
-        packet_bytes -= ss_data_len;
-      } else {
-        packet_bytes = 1;
-      }
-    }
-    size_t rem_bytes = payload_size_ - bytes_processed;
-    if (packet_bytes >= rem_bytes) {
-      // All remaining payload fits into this packet.
-      packet_bytes = rem_bytes;
-      // If this is the penultimate packet, leave at least 1 byte of payload for
-      // the last packet.
-      if (num_packets_left == 2)
-        --packet_bytes;
-    }
-    QueuePacket(bytes_processed, packet_bytes, bytes_processed == 0,
-                rem_bytes == packet_bytes, &packets_);
-    --num_packets_left;
-    bytes_processed += packet_bytes;
-    // Last packet should be smaller
-    RTC_DCHECK(num_packets_left > 0 ||
-               per_packet_capacity >=
-                   packet_bytes + last_packet_reduction_len_);
-  }
-  RTC_CHECK_EQ(bytes_processed, payload_size_);
+  return payload_sizes_.end() - current_packet_;
 }
 
 bool RtpPacketizerVp9::NextPacket(RtpPacketToSend* packet) {
   RTC_DCHECK(packet);
-  if (packets_.empty()) {
+  if (current_packet_ == payload_sizes_.end()) {
     return false;
   }
-  PacketInfo packet_info = packets_.front();
-  packets_.pop();
 
-  if (!WriteHeaderAndPayload(packet_info, packet, packets_.empty())) {
+  bool layer_begin = current_packet_ == payload_sizes_.begin();
+  int packet_payload_len = *current_packet_;
+  ++current_packet_;
+  bool layer_end = current_packet_ == payload_sizes_.end();
+
+  int header_size = header_size_;
+  if (layer_begin)
+    header_size += first_packet_extra_header_size_;
+
+  uint8_t* buffer = packet->AllocatePayload(header_size + packet_payload_len);
+  RTC_CHECK(buffer);
+
+  if (!WriteHeader(layer_begin, layer_end,
+                   rtc::MakeArrayView(buffer, header_size)))
     return false;
-  }
+
+  memcpy(buffer + header_size, remaining_payload_.data(), packet_payload_len);
+  remaining_payload_ = remaining_payload_.subview(packet_payload_len);
 
   // Ensure end_of_picture is always set on top spatial layer when it is not
   // dropped.
   RTC_DCHECK(hdr_.spatial_idx < hdr_.num_spatial_layers - 1 ||
              hdr_.end_of_picture);
 
-  packet->SetMarker(packets_.empty() && hdr_.end_of_picture);
+  packet->SetMarker(layer_end && hdr_.end_of_picture);
   return true;
 }
 
@@ -620,40 +531,20 @@
 // V:   | SS            |
 //      | ..            |
 //      +-+-+-+-+-+-+-+-+
-
-bool RtpPacketizerVp9::WriteHeaderAndPayload(const PacketInfo& packet_info,
-                                             RtpPacketToSend* packet,
-                                             bool last) const {
-  uint8_t* buffer = packet->AllocatePayload(
-      last ? max_payload_length_ - last_packet_reduction_len_
-           : max_payload_length_);
-  RTC_DCHECK(buffer);
-  size_t header_length;
-  if (!WriteHeader(packet_info, buffer, &header_length))
-    return false;
-
-  // Copy payload data.
-  memcpy(&buffer[header_length], &payload_[packet_info.payload_start_pos],
-         packet_info.size);
-
-  packet->SetPayloadSize(header_length + packet_info.size);
-  return true;
-}
-
-bool RtpPacketizerVp9::WriteHeader(const PacketInfo& packet_info,
-                                   uint8_t* buffer,
-                                   size_t* header_length) const {
+bool RtpPacketizerVp9::WriteHeader(bool layer_begin,
+                                   bool layer_end,
+                                   rtc::ArrayView<uint8_t> buffer) const {
   // Required payload descriptor byte.
   bool i_bit = PictureIdPresent(hdr_);
   bool p_bit = hdr_.inter_pic_predicted;
   bool l_bit = LayerInfoPresent(hdr_);
   bool f_bit = hdr_.flexible_mode;
-  bool b_bit = packet_info.layer_begin;
-  bool e_bit = packet_info.layer_end;
+  bool b_bit = layer_begin;
+  bool e_bit = layer_end;
   bool v_bit = hdr_.ss_data_available && b_bit;
   bool z_bit = hdr_.non_ref_for_inter_layer_pred;
 
-  rtc::BitBufferWriter writer(buffer, max_payload_length_);
+  rtc::BitBufferWriter writer(buffer.data(), buffer.size());
   RETURN_FALSE_ON_ERROR(writer.WriteBits(i_bit ? 1 : 0, 1));
   RETURN_FALSE_ON_ERROR(writer.WriteBits(p_bit ? 1 : 0, 1));
   RETURN_FALSE_ON_ERROR(writer.WriteBits(l_bit ? 1 : 0, 1));
@@ -684,16 +575,15 @@
   size_t offset_bytes = 0;
   size_t offset_bits = 0;
   writer.GetCurrentOffset(&offset_bytes, &offset_bits);
-  assert(offset_bits == 0);
-
-  *header_length = offset_bytes;
+  RTC_DCHECK_EQ(offset_bits, 0);
+  RTC_DCHECK_EQ(offset_bytes, buffer.size());
   return true;
 }
 
 bool RtpDepacketizerVp9::Parse(ParsedPayload* parsed_payload,
                                const uint8_t* payload,
                                size_t payload_length) {
-  assert(parsed_payload != nullptr);
+  RTC_DCHECK(parsed_payload != nullptr);
   if (payload_length == 0) {
     RTC_LOG(LS_ERROR) << "Payload length is zero.";
     return false;
@@ -757,7 +647,7 @@
       b_bit && (!l_bit || !vp9_header.inter_layer_predicted);
 
   uint64_t rem_bits = parser.RemainingBitCount();
-  assert(rem_bits % 8 == 0);
+  RTC_DCHECK_EQ(rem_bits % 8, 0);
   parsed_payload->payload_length = rem_bits / 8;
   if (parsed_payload->payload_length == 0) {
     RTC_LOG(LS_ERROR) << "Failed parsing VP9 payload data.";
diff --git a/modules/rtp_rtcp/source/rtp_format_vp9.h b/modules/rtp_rtcp/source/rtp_format_vp9.h
index 61f04d4..8a2a2a6 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp9.h
+++ b/modules/rtp_rtcp/source/rtp_format_vp9.h
@@ -21,28 +21,25 @@
 #ifndef MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP9_H_
 #define MODULES_RTP_RTCP_SOURCE_RTP_FORMAT_VP9_H_
 
-#include <queue>
-#include <string>
+#include <vector>
 
+#include "api/array_view.h"
 #include "modules/include/module_common_types.h"
 #include "modules/rtp_rtcp/source/rtp_format.h"
+#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
 #include "rtc_base/constructormagic.h"
 
 namespace webrtc {
 
 class RtpPacketizerVp9 : public RtpPacketizer {
  public:
-  RtpPacketizerVp9(const RTPVideoHeaderVP9& hdr,
-                   size_t max_payload_length,
-                   size_t last_packet_reduction_len);
+  // The |payload| must be one encoded VP9 layer frame.
+  RtpPacketizerVp9(rtc::ArrayView<const uint8_t> payload,
+                   PayloadSizeLimits limits,
+                   const RTPVideoHeaderVP9& hdr);
 
   ~RtpPacketizerVp9() override;
 
-  // The payload data must be one encoded VP9 layer frame.
-  size_t SetPayloadData(const uint8_t* payload,
-                        size_t payload_size,
-                        const RTPFragmentationHeader* fragmentation);
-
   size_t NumPackets() const override;
 
   // Gets the next payload with VP9 payload header.
@@ -50,38 +47,20 @@
   // Returns true on success, false otherwise.
   bool NextPacket(RtpPacketToSend* packet) override;
 
-  typedef struct {
-    size_t payload_start_pos;
-    size_t size;
-    bool layer_begin;
-    bool layer_end;
-  } PacketInfo;
-  typedef std::queue<PacketInfo> PacketInfoQueue;
-
  private:
-  // Calculates all packet sizes and loads info to packet queue.
-  void GeneratePackets();
-
-  // Writes the payload descriptor header and copies payload to the |buffer|.
-  // |packet_info| determines which part of the payload to write.
-  // |last| indicates if the packet is the last packet in the frame.
-  // Returns true on success, false otherwise.
-  bool WriteHeaderAndPayload(const PacketInfo& packet_info,
-                             RtpPacketToSend* packet,
-                             bool last) const;
-
-  // Writes payload descriptor header to |buffer|.
-  // Returns true on success, false otherwise.
-  bool WriteHeader(const PacketInfo& packet_info,
-                   uint8_t* buffer,
-                   size_t* header_length) const;
+  // Writes the payload descriptor header.
+  // |layer_begin| and |layer_end| indicates the postision of the packet in
+  // the layer frame. Returns false on failure.
+  bool WriteHeader(bool layer_begin,
+                   bool layer_end,
+                   rtc::ArrayView<uint8_t> rtp_payload) const;
 
   const RTPVideoHeaderVP9 hdr_;
-  const size_t max_payload_length_;  // The max length in bytes of one packet.
-  const uint8_t* payload_;           // The payload data to be packetized.
-  size_t payload_size_;              // The size in bytes of the payload data.
-  const size_t last_packet_reduction_len_;
-  PacketInfoQueue packets_;
+  const int header_size_;
+  const int first_packet_extra_header_size_;
+  rtc::ArrayView<const uint8_t> remaining_payload_;
+  std::vector<int> payload_sizes_;
+  std::vector<int>::const_iterator current_packet_;
 
   RTC_DISALLOW_COPY_AND_ASSIGN(RtpPacketizerVp9);
 };
diff --git a/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc b/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
index 66c6091..f63d053 100644
--- a/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_format_vp9_unittest.cc
@@ -133,22 +133,19 @@
   void SetUp() override { expected_.InitRTPVideoHeaderVP9(); }
 
   RtpPacketToSend packet_;
-  std::unique_ptr<uint8_t[]> payload_;
-  size_t payload_size_;
+  std::vector<uint8_t> payload_;
   size_t payload_pos_;
   RTPVideoHeaderVP9 expected_;
   std::unique_ptr<RtpPacketizerVp9> packetizer_;
   size_t num_packets_;
 
   void Init(size_t payload_size, size_t packet_size) {
-    payload_.reset(new uint8_t[payload_size]);
-    memset(payload_.get(), 7, payload_size);
-    payload_size_ = payload_size;
+    payload_.assign(payload_size, 7);
     payload_pos_ = 0;
-    packetizer_.reset(new RtpPacketizerVp9(expected_, packet_size,
-                                           /*last_packet_reduction_len=*/0));
-    num_packets_ =
-        packetizer_->SetPayloadData(payload_.get(), payload_size_, nullptr);
+    RtpPacketizer::PayloadSizeLimits limits;
+    limits.max_payload_len = packet_size;
+    packetizer_.reset(new RtpPacketizerVp9(payload_, limits, expected_));
+    num_packets_ = packetizer_->NumPackets();
   }
 
   void CheckPayload(const uint8_t* packet,
@@ -158,7 +155,7 @@
     for (size_t i = start_pos; i < end_pos; ++i) {
       EXPECT_EQ(packet[i], payload_[payload_pos_++]);
     }
-    EXPECT_EQ(last, payload_pos_ == payload_size_);
+    EXPECT_EQ(last, payload_pos_ == payload_.size());
   }
 
   void CreateParseAndCheckPackets(const size_t* expected_hdr_sizes,
@@ -483,10 +480,9 @@
 
 TEST_F(RtpPacketizerVp9Test, EndOfPictureSetsSetMarker) {
   const size_t kFrameSize = 10;
-  const size_t kPacketSize = 8;
-  const size_t kLastPacketReductionLen = 0;
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 8;
   const uint8_t kFrame[kFrameSize] = {7};
-  const RTPFragmentationHeader* kNoFragmentation = nullptr;
 
   RTPVideoHeaderVP9 vp9_header;
   vp9_header.InitRTPVideoHeaderVP9();
@@ -502,9 +498,7 @@
         spatial_idx + 1 == vp9_header.num_spatial_layers - 1;
     vp9_header.spatial_idx = spatial_idx;
     vp9_header.end_of_picture = end_of_picture;
-    RtpPacketizerVp9 packetizer(vp9_header, kPacketSize,
-                                kLastPacketReductionLen);
-    packetizer.SetPayloadData(kFrame, sizeof(kFrame), kNoFragmentation);
+    RtpPacketizerVp9 packetizer(kFrame, limits, vp9_header);
     ASSERT_TRUE(packetizer.NextPacket(&packet));
     EXPECT_FALSE(packet.Marker());
     ASSERT_TRUE(packetizer.NextPacket(&packet));
@@ -514,23 +508,21 @@
 
 TEST_F(RtpPacketizerVp9Test, TestGeneratesMinimumNumberOfPackets) {
   const size_t kFrameSize = 10;
-  const size_t kPacketSize = 8;
-  const size_t kLastPacketReductionLen = 0;
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 8;
   // Calculated by hand. One packet can contain
   // |kPacketSize| - |kVp9MinDiscriptorSize| = 6 bytes of the frame payload,
   // thus to fit 10 bytes two packets are required.
   const size_t kMinNumberOfPackets = 2;
   const uint8_t kFrame[kFrameSize] = {7};
-  const RTPFragmentationHeader* kNoFragmentation = nullptr;
 
   RTPVideoHeaderVP9 vp9_header;
   vp9_header.InitRTPVideoHeaderVP9();
 
   RtpPacketToSend packet(kNoExtensions);
 
-  RtpPacketizerVp9 packetizer(vp9_header, kPacketSize, kLastPacketReductionLen);
-  EXPECT_EQ(kMinNumberOfPackets, packetizer.SetPayloadData(
-                                     kFrame, sizeof(kFrame), kNoFragmentation));
+  RtpPacketizerVp9 packetizer(kFrame, limits, vp9_header);
+  EXPECT_EQ(packetizer.NumPackets(), kMinNumberOfPackets);
   ASSERT_TRUE(packetizer.NextPacket(&packet));
   EXPECT_FALSE(packet.Marker());
   ASSERT_TRUE(packetizer.NextPacket(&packet));
@@ -539,8 +531,9 @@
 
 TEST_F(RtpPacketizerVp9Test, TestRespectsLastPacketReductionLen) {
   const size_t kFrameSize = 10;
-  const size_t kPacketSize = 8;
-  const size_t kLastPacketReductionLen = 5;
+  RtpPacketizer::PayloadSizeLimits limits;
+  limits.max_payload_len = 8;
+  limits.last_packet_reduction_len = 5;
   // Calculated by hand. VP9 payload descriptor is 2 bytes. Like in the test
   // above, 1 packet is not enough. 2 packets can contain
   // 2*(|kPacketSize| - |kVp9MinDiscriptorSize|) - |kLastPacketReductionLen| = 7
@@ -548,7 +541,6 @@
   // bytes.
   const size_t kMinNumberOfPackets = 3;
   const uint8_t kFrame[kFrameSize] = {7};
-  const RTPFragmentationHeader* kNoFragmentation = nullptr;
 
   RTPVideoHeaderVP9 vp9_header;
   vp9_header.InitRTPVideoHeaderVP9();
@@ -556,11 +548,8 @@
 
   RtpPacketToSend packet(kNoExtensions);
 
-  RtpPacketizerVp9 packetizer0(vp9_header, kPacketSize,
-                               kLastPacketReductionLen);
-  EXPECT_EQ(
-      packetizer0.SetPayloadData(kFrame, sizeof(kFrame), kNoFragmentation),
-      kMinNumberOfPackets);
+  RtpPacketizerVp9 packetizer0(kFrame, limits, vp9_header);
+  EXPECT_EQ(packetizer0.NumPackets(), kMinNumberOfPackets);
   ASSERT_TRUE(packetizer0.NextPacket(&packet));
   EXPECT_FALSE(packet.Marker());
   ASSERT_TRUE(packetizer0.NextPacket(&packet));
diff --git a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.cc b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.cc
index b5b8ce5..1b93132 100644
--- a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.cc
+++ b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.cc
@@ -19,6 +19,9 @@
 constexpr int RtpGenericFrameDescriptor::kMaxSpatialLayers;
 
 RtpGenericFrameDescriptor::RtpGenericFrameDescriptor() = default;
+RtpGenericFrameDescriptor::RtpGenericFrameDescriptor(
+    const RtpGenericFrameDescriptor&) = default;
+RtpGenericFrameDescriptor::~RtpGenericFrameDescriptor() = default;
 
 int RtpGenericFrameDescriptor::TemporalLayer() const {
   RTC_DCHECK(FirstPacketInSubFrame());
@@ -31,6 +34,17 @@
   temporal_layer_ = temporal_layer;
 }
 
+int RtpGenericFrameDescriptor::SpatialLayer() const {
+  RTC_DCHECK(FirstPacketInSubFrame());
+  int layer = 0;
+  uint8_t spatial_layers = spatial_layers_;
+  while (spatial_layers_ != 0 && !(spatial_layers & 1)) {
+    spatial_layers >>= 1;
+    layer++;
+  }
+  return layer;
+}
+
 uint8_t RtpGenericFrameDescriptor::SpatialLayersBitmask() const {
   RTC_DCHECK(FirstPacketInSubFrame());
   return spatial_layers_;
@@ -71,4 +85,15 @@
   return true;
 }
 
+void RtpGenericFrameDescriptor::SetByteRepresentation(
+    rtc::ArrayView<const uint8_t> byte_representation) {
+  byte_representation_.assign(byte_representation.begin(),
+                              byte_representation.end());
+}
+
+rtc::ArrayView<const uint8_t>
+RtpGenericFrameDescriptor::GetByteRepresentation() {
+  return byte_representation_;
+}
+
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h
index e4b775e..1bde4fa 100644
--- a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h
+++ b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h
@@ -12,6 +12,7 @@
 
 #include <stddef.h>
 #include <stdint.h>
+#include <vector>
 
 #include "api/array_view.h"
 
@@ -25,6 +26,8 @@
   static constexpr int kMaxSpatialLayers = 8;
 
   RtpGenericFrameDescriptor();
+  RtpGenericFrameDescriptor(const RtpGenericFrameDescriptor&);
+  ~RtpGenericFrameDescriptor();
 
   bool FirstPacketInSubFrame() const { return beginning_of_subframe_; }
   void SetFirstPacketInSubFrame(bool first) { beginning_of_subframe_ = first; }
@@ -41,8 +44,9 @@
   int TemporalLayer() const;
   void SetTemporalLayer(int temporal_layer);
 
-  // Frame might by used, possible indrectly, for spatial layer sid iff
+  // Frame might by used, possible indirectly, for spatial layer sid iff
   // (bitmask & (1 << sid)) != 0
+  int SpatialLayer() const;
   uint8_t SpatialLayersBitmask() const;
   void SetSpatialLayersBitmask(uint8_t spatial_layers);
 
@@ -54,6 +58,9 @@
   // Returns false on failure, i.e. number of dependencies is too large.
   bool AddFrameDependencyDiff(uint16_t fdiff);
 
+  void SetByteRepresentation(rtc::ArrayView<const uint8_t> representation);
+  rtc::ArrayView<const uint8_t> GetByteRepresentation();
+
  private:
   bool beginning_of_subframe_ = false;
   bool end_of_subframe_ = false;
@@ -65,6 +72,7 @@
   uint8_t temporal_layer_ = 0;
   size_t num_frame_deps_ = 0;
   uint16_t frame_deps_id_diffs_[kMaxNumFrameDependencies];
+  std::vector<uint8_t> byte_representation_;
 };
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h
index d6acbe5..2dd9dd4 100644
--- a/modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h
+++ b/modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h
@@ -23,7 +23,9 @@
  public:
   static constexpr RTPExtensionType kId = kRtpExtensionGenericFrameDescriptor;
   static constexpr char kUri[] =
-      "http://www.webrtc.org/experiments/rtp-hdrext/generic-frame-descriptor";
+      "http://www.webrtc.org/experiments/rtp-hdrext/"
+      "generic-frame-descriptor-00";
+  static constexpr int kMaxSizeBytes = 16;
 
   static bool Parse(rtc::ArrayView<const uint8_t> data,
                     RtpGenericFrameDescriptor* descriptor);
diff --git a/modules/rtp_rtcp/source/rtp_header_extension_map.cc b/modules/rtp_rtcp/source/rtp_header_extension_map.cc
index 9fa66c0..168665a 100644
--- a/modules/rtp_rtcp/source/rtp_header_extension_map.cc
+++ b/modules/rtp_rtcp/source/rtp_header_extension_map.cc
@@ -38,6 +38,7 @@
     CreateExtensionInfo<PlayoutDelayLimits>(),
     CreateExtensionInfo<VideoContentTypeExtension>(),
     CreateExtensionInfo<VideoTimingExtension>(),
+    CreateExtensionInfo<FrameMarkingExtension>(),
     CreateExtensionInfo<RtpStreamId>(),
     CreateExtensionInfo<RepairedRtpStreamId>(),
     CreateExtensionInfo<RtpMid>(),
@@ -54,12 +55,9 @@
 
 constexpr RTPExtensionType RtpHeaderExtensionMap::kInvalidType;
 constexpr int RtpHeaderExtensionMap::kInvalidId;
-constexpr int RtpHeaderExtensionMap::kMinId;
-constexpr int RtpHeaderExtensionMap::kMaxId;
 
-RtpHeaderExtensionMap::RtpHeaderExtensionMap() {
-  for (auto& type : types_)
-    type = kInvalidType;
+RtpHeaderExtensionMap::RtpHeaderExtensionMap()
+    : mixed_one_two_byte_header_supported_(false) {
   for (auto& id : ids_)
     id = kInvalidId;
 }
@@ -88,29 +86,20 @@
   return false;
 }
 
-size_t RtpHeaderExtensionMap::GetTotalLengthInBytes(
-    rtc::ArrayView<const RtpExtensionSize> extensions) const {
-  // Header size of the extension block, see RFC3550 Section 5.3.1
-  static constexpr size_t kRtpOneByteHeaderLength = 4;
-  // Header size of each individual extension, see RFC5285 Section 4.2
-  static constexpr size_t kExtensionHeaderLength = 1;
-  size_t values_size = 0;
-  for (const RtpExtensionSize& extension : extensions) {
-    if (IsRegistered(extension.type))
-      values_size += extension.value_size + kExtensionHeaderLength;
+RTPExtensionType RtpHeaderExtensionMap::GetType(int id) const {
+  RTC_DCHECK_GE(id, RtpExtension::kMinId);
+  RTC_DCHECK_LE(id, RtpExtension::kMaxId);
+  for (int type = kRtpExtensionNone + 1; type < kRtpExtensionNumberOfExtensions;
+       ++type) {
+    if (ids_[type] == id) {
+      return static_cast<RTPExtensionType>(type);
+    }
   }
-  if (values_size == 0)
-    return 0;
-  size_t size = kRtpOneByteHeaderLength + values_size;
-  // Round up to the nearest size that is a multiple of 4.
-  // Which is same as round down (size + 3).
-  return size + 3 - (size + 3) % 4;
+  return kInvalidType;
 }
 
 int32_t RtpHeaderExtensionMap::Deregister(RTPExtensionType type) {
   if (IsRegistered(type)) {
-    uint8_t id = GetId(type);
-    types_[id] = kInvalidType;
     ids_[type] = kInvalidId;
   }
   return 0;
@@ -122,28 +111,29 @@
   RTC_DCHECK_GT(type, kRtpExtensionNone);
   RTC_DCHECK_LT(type, kRtpExtensionNumberOfExtensions);
 
-  if (id < kMinId || id > kMaxId) {
+  if (id < RtpExtension::kMinId || id > RtpExtension::kMaxId) {
     RTC_LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
                         << "' with invalid id:" << id << ".";
     return false;
   }
 
-  if (GetType(id) == type) {  // Same type/id pair already registered.
+  RTPExtensionType registered_type = GetType(id);
+  if (registered_type == type) {  // Same type/id pair already registered.
     RTC_LOG(LS_VERBOSE) << "Reregistering extension uri:'" << uri
                         << "', id:" << id;
     return true;
   }
 
-  if (GetType(id) != kInvalidType) {  // |id| used by another extension type.
+  if (registered_type !=
+      kInvalidType) {  // |id| used by another extension type.
     RTC_LOG(LS_WARNING) << "Failed to register extension uri:'" << uri
                         << "', id:" << id
                         << ". Id already in use by extension type "
-                        << static_cast<int>(GetType(id));
+                        << static_cast<int>(registered_type);
     return false;
   }
   RTC_DCHECK(!IsRegistered(type));
 
-  types_[id] = type;
   // There is a run-time check above id fits into uint8_t.
   ids_[type] = static_cast<uint8_t>(id);
   return true;
diff --git a/modules/rtp_rtcp/source/rtp_header_extension_map_unittest.cc b/modules/rtp_rtcp/source/rtp_header_extension_map_unittest.cc
index ad9f79c..f5bb1bb 100644
--- a/modules/rtp_rtcp/source/rtp_header_extension_map_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_header_extension_map_unittest.cc
@@ -57,11 +57,18 @@
   EXPECT_EQ(3, map.GetId(AbsoluteSendTime::kId));
 }
 
+TEST(RtpHeaderExtensionTest, RegisterTwoByteHeaderExtensions) {
+  RtpHeaderExtensionMap map;
+  // Two-byte header extension needed for id: [15-255].
+  EXPECT_TRUE(map.Register<TransmissionOffset>(18));
+  EXPECT_TRUE(map.Register<AbsoluteSendTime>(255));
+}
+
 TEST(RtpHeaderExtensionTest, RegisterIllegalArg) {
   RtpHeaderExtensionMap map;
-  // Valid range for id: [1-14].
+  // Valid range for id: [1-255].
   EXPECT_FALSE(map.Register<TransmissionOffset>(0));
-  EXPECT_FALSE(map.Register<TransmissionOffset>(15));
+  EXPECT_FALSE(map.Register<TransmissionOffset>(256));
 }
 
 TEST(RtpHeaderExtensionTest, Idempotent) {
@@ -82,17 +89,6 @@
   EXPECT_TRUE(map.Register<AudioLevel>(4));
 }
 
-TEST(RtpHeaderExtensionTest, GetTotalLength) {
-  RtpHeaderExtensionMap map;
-  constexpr RtpExtensionSize kExtensionSizes[] = {
-      {TransmissionOffset::kId, TransmissionOffset::kValueSizeBytes}};
-  EXPECT_EQ(0u, map.GetTotalLengthInBytes(kExtensionSizes));
-  EXPECT_TRUE(map.Register<TransmissionOffset>(3));
-  static constexpr size_t kRtpOneByteHeaderLength = 4;
-  EXPECT_EQ(kRtpOneByteHeaderLength + (TransmissionOffset::kValueSizeBytes + 1),
-            map.GetTotalLengthInBytes(kExtensionSizes));
-}
-
 TEST(RtpHeaderExtensionTest, GetType) {
   RtpHeaderExtensionMap map;
   EXPECT_EQ(RtpHeaderExtensionMap::kInvalidType, map.GetType(3));
diff --git a/modules/rtp_rtcp/source/rtp_header_extension_size.cc b/modules/rtp_rtcp/source/rtp_header_extension_size.cc
new file mode 100644
index 0000000..f20ca00
--- /dev/null
+++ b/modules/rtp_rtcp/source/rtp_header_extension_size.cc
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2018 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 "modules/rtp_rtcp/source/rtp_header_extension_size.h"
+
+#include "api/rtpparameters.h"
+
+namespace webrtc {
+
+int RtpHeaderExtensionSize(rtc::ArrayView<const RtpExtensionSize> extensions,
+                           const RtpHeaderExtensionMap& registered_extensions) {
+  // RFC3550 Section 5.3.1
+  static constexpr int kExtensionBlockHeaderSize = 4;
+
+  int values_size = 0;
+  int num_extensions = 0;
+  int each_extension_header_size = 1;
+  for (const RtpExtensionSize& extension : extensions) {
+    int id = registered_extensions.GetId(extension.type);
+    if (id == RtpHeaderExtensionMap::kInvalidId)
+      continue;
+    // All extensions should use same size header. Check if the |extension|
+    // forces to switch to two byte header that allows larger id and value size.
+    if (id > RtpExtension::kOneByteHeaderExtensionMaxId ||
+        extension.value_size >
+            RtpExtension::kOneByteHeaderExtensionMaxValueSize) {
+      each_extension_header_size = 2;
+    }
+    values_size += extension.value_size;
+    num_extensions++;
+  }
+  if (values_size == 0)
+    return 0;
+  int size = kExtensionBlockHeaderSize +
+             each_extension_header_size * num_extensions + values_size;
+  // Extension size specified in 32bit words,
+  // so result must be multiple of 4 bytes. Round up.
+  return size + 3 - (size + 3) % 4;
+}
+
+}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_header_extension_size.h b/modules/rtp_rtcp/source/rtp_header_extension_size.h
new file mode 100644
index 0000000..8047fcc
--- /dev/null
+++ b/modules/rtp_rtcp/source/rtp_header_extension_size.h
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSION_SIZE_H_
+#define MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSION_SIZE_H_
+
+#include "api/array_view.h"
+#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
+#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
+
+namespace webrtc {
+
+struct RtpExtensionSize {
+  RTPExtensionType type;
+  int value_size;
+};
+
+// Calculates rtp header extension size in bytes assuming packet contain
+// all |extensions| with provided |value_size|.
+// Counts only extensions present among |registered_extensions|.
+int RtpHeaderExtensionSize(rtc::ArrayView<const RtpExtensionSize> extensions,
+                           const RtpHeaderExtensionMap& registered_extensions);
+
+}  // namespace webrtc
+
+#endif  // MODULES_RTP_RTCP_SOURCE_RTP_HEADER_EXTENSION_SIZE_H_
diff --git a/modules/rtp_rtcp/source/rtp_header_extension_size_unittest.cc b/modules/rtp_rtcp/source/rtp_header_extension_size_unittest.cc
new file mode 100644
index 0000000..5cc26bc
--- /dev/null
+++ b/modules/rtp_rtcp/source/rtp_header_extension_size_unittest.cc
@@ -0,0 +1,92 @@
+/*
+ *  Copyright (c) 2018 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 "modules/rtp_rtcp/source/rtp_header_extension_size.h"
+
+#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
+#include "test/gtest.h"
+
+namespace {
+
+using ::webrtc::RtpExtensionSize;
+using ::webrtc::RtpHeaderExtensionMap;
+using ::webrtc::RtpHeaderExtensionSize;
+using ::webrtc::RtpMid;
+using ::webrtc::RtpStreamId;
+
+// id for 1-byte header extension. actual value is irrelevant for these tests.
+constexpr int kId = 1;
+// id that forces to use 2-byte header extension.
+constexpr int kIdForceTwoByteHeader = 15;
+
+TEST(RtpHeaderExtensionSizeTest, ReturnsZeroIfNoExtensionsAreRegistered) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 3}};
+  // Register different extension than ask size for.
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpStreamId>(kId);
+
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 0);
+}
+
+TEST(RtpHeaderExtensionSizeTest, IncludesSizeOfExtensionHeaders) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 3}};
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpMid>(kId);
+
+  // 4 bytes for extension block header + 1 byte for individual extension header
+  // + 3 bytes for the value.
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 8);
+}
+
+TEST(RtpHeaderExtensionSizeTest, RoundsUpTo32bitAlignmant) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 5}};
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpMid>(kId);
+
+  // 10 bytes of data including headers + 2 bytes of padding.
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 12);
+}
+
+TEST(RtpHeaderExtensionSizeTest, SumsSeveralExtensions) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 16},
+                                                  {RtpStreamId::kId, 2}};
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpMid>(kId);
+  registered.Register<RtpStreamId>(14);
+
+  // 4 bytes for extension block header + 18 bytes of value +
+  // 2 bytes for two headers
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 24);
+}
+
+TEST(RtpHeaderExtensionSizeTest, LargeIdForce2BytesHeader) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 3},
+                                                  {RtpStreamId::kId, 2}};
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpMid>(kId);
+  registered.Register<RtpStreamId>(kIdForceTwoByteHeader);
+
+  // 4 bytes for extension block header + 5 bytes of value +
+  // 2*2 bytes for two headers + 3 bytes of padding.
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 16);
+}
+
+TEST(RtpHeaderExtensionSizeTest, LargeValueForce2BytesHeader) {
+  constexpr RtpExtensionSize kExtensionSizes[] = {{RtpMid::kId, 17},
+                                                  {RtpStreamId::kId, 4}};
+  RtpHeaderExtensionMap registered;
+  registered.Register<RtpMid>(1);
+  registered.Register<RtpStreamId>(2);
+
+  // 4 bytes for extension block header + 21 bytes of value +
+  // 2*2 bytes for two headers + 3 byte of padding.
+  EXPECT_EQ(RtpHeaderExtensionSize(kExtensionSizes, registered), 32);
+}
+
+}  // namespace
diff --git a/modules/rtp_rtcp/source/rtp_header_extensions.cc b/modules/rtp_rtcp/source/rtp_header_extensions.cc
index 2dba4d7..fc4b7ce 100644
--- a/modules/rtp_rtcp/source/rtp_header_extensions.cc
+++ b/modules/rtp_rtcp/source/rtp_header_extensions.cc
@@ -19,7 +19,7 @@
 // Absolute send time in RTP streams.
 //
 // The absolute send time is signaled to the receiver in-band using the
-// general mechanism for RTP header extensions [RFC5285]. The payload
+// general mechanism for RTP header extensions [RFC8285]. The payload
 // of this extension (the transmitted value) is a 24-bit unsigned integer
 // containing the sender's current time in seconds as a fixed point number
 // with 18 bits fractional part.
@@ -89,7 +89,7 @@
 // From RFC 5450: Transmission Time Offsets in RTP Streams.
 //
 // The transmission time is signaled to the receiver in-band using the
-// general mechanism for RTP header extensions [RFC5285]. The payload
+// general mechanism for RTP header extensions [RFC8285]. The payload
 // of this extension (the transmitted value) is a 24-bit signed integer.
 // When added to the RTP timestamp of the packet, it represents the
 // "effective" RTP transmission time of the packet, on the RTP
@@ -350,6 +350,86 @@
   return true;
 }
 
+// Frame Marking.
+//
+// Meta-information about an RTP stream outside the encrypted media payload,
+// useful for an RTP switch to do codec-agnostic selective forwarding
+// without decrypting the payload.
+//
+// For non-scalable streams:
+//    0                   1
+//    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//   |  ID   | L = 0 |S|E|I|D|0 0 0 0|
+//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//
+// For scalable streams:
+//    0                   1                   2                   3
+//    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+//   |  ID   | L = 2 |S|E|I|D|B| TID |      LID      |   TL0PICIDX   |
+//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+constexpr RTPExtensionType FrameMarkingExtension::kId;
+constexpr const char FrameMarkingExtension::kUri[];
+
+bool FrameMarkingExtension::IsScalable(uint8_t temporal_id, uint8_t layer_id) {
+  return temporal_id != kNoTemporalIdx || layer_id != kNoSpatialIdx;
+}
+
+bool FrameMarkingExtension::Parse(rtc::ArrayView<const uint8_t> data,
+                                  FrameMarking* frame_marking) {
+  RTC_DCHECK(frame_marking);
+
+  if (data.size() != 1 && data.size() != 3)
+    return false;
+
+  frame_marking->start_of_frame = (data[0] & 0x80) != 0;
+  frame_marking->end_of_frame = (data[0] & 0x40) != 0;
+  frame_marking->independent_frame = (data[0] & 0x20) != 0;
+  frame_marking->discardable_frame = (data[0] & 0x10) != 0;
+
+  if (data.size() == 3) {
+    frame_marking->base_layer_sync = (data[0] & 0x08) != 0;
+    frame_marking->temporal_id = data[0] & 0x7;
+    frame_marking->layer_id = data[1];
+    frame_marking->tl0_pic_idx = data[2];
+  } else {
+    // non-scalable
+    frame_marking->base_layer_sync = false;
+    frame_marking->temporal_id = kNoTemporalIdx;
+    frame_marking->layer_id = kNoSpatialIdx;
+    frame_marking->tl0_pic_idx = 0;
+  }
+  return true;
+}
+
+size_t FrameMarkingExtension::ValueSize(const FrameMarking& frame_marking) {
+  if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id))
+    return 3;
+  else
+    return 1;
+}
+
+bool FrameMarkingExtension::Write(rtc::ArrayView<uint8_t> data,
+                                  const FrameMarking& frame_marking) {
+  RTC_DCHECK_GE(data.size(), 1);
+  RTC_CHECK_LE(frame_marking.temporal_id, 0x07);
+  data[0] = frame_marking.start_of_frame ? 0x80 : 0x00;
+  data[0] |= frame_marking.end_of_frame ? 0x40 : 0x00;
+  data[0] |= frame_marking.independent_frame ? 0x20 : 0x00;
+  data[0] |= frame_marking.discardable_frame ? 0x10 : 0x00;
+
+  if (IsScalable(frame_marking.temporal_id, frame_marking.layer_id)) {
+    RTC_DCHECK_EQ(data.size(), 3);
+    data[0] |= frame_marking.base_layer_sync ? 0x08 : 0x00;
+    data[0] |= frame_marking.temporal_id & 0x07;
+    data[1] = frame_marking.layer_id;
+    data[2] = frame_marking.tl0_pic_idx;
+  }
+  return true;
+}
+
 bool BaseRtpStringExtension::Parse(rtc::ArrayView<const uint8_t> data,
                                    StringRtpHeaderExtension* str) {
   if (data.empty() || data[0] == 0)  // Valid string extension can't be empty.
@@ -382,9 +462,11 @@
 
 bool BaseRtpStringExtension::Write(rtc::ArrayView<uint8_t> data,
                                    const std::string& str) {
+  if (str.size() > StringRtpHeaderExtension::kMaxSize) {
+    return false;
+  }
   RTC_DCHECK_EQ(data.size(), str.size());
   RTC_DCHECK_GE(str.size(), 1);
-  RTC_DCHECK_LE(str.size(), StringRtpHeaderExtension::kMaxSize);
   memcpy(data.data(), str.data(), str.size());
   return true;
 }
diff --git a/modules/rtp_rtcp/source/rtp_header_extensions.h b/modules/rtp_rtcp/source/rtp_header_extensions.h
index 4e1afc1..84e9831 100644
--- a/modules/rtp_rtcp/source/rtp_header_extensions.h
+++ b/modules/rtp_rtcp/source/rtp_header_extensions.h
@@ -153,6 +153,22 @@
                     uint8_t idx);
 };
 
+class FrameMarkingExtension {
+ public:
+  static constexpr RTPExtensionType kId = kRtpExtensionFrameMarking;
+  static constexpr const char kUri[] =
+      "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07";
+
+  static bool Parse(rtc::ArrayView<const uint8_t> data,
+                    FrameMarking* frame_marking);
+  static size_t ValueSize(const FrameMarking& frame_marking);
+  static bool Write(rtc::ArrayView<uint8_t> data,
+                    const FrameMarking& frame_marking);
+
+ private:
+  static bool IsScalable(uint8_t temporal_id, uint8_t layer_id);
+};
+
 // Base extension class for RTP header extensions which are strings.
 // Subclasses must defined kId and kUri static constexpr members.
 class BaseRtpStringExtension {
diff --git a/modules/rtp_rtcp/source/rtp_packet.cc b/modules/rtp_rtcp/source/rtp_packet.cc
index 4387fe7..8cb8fd8 100644
--- a/modules/rtp_rtcp/source/rtp_packet.cc
+++ b/modules/rtp_rtcp/source/rtp_packet.cc
@@ -13,27 +13,24 @@
 #include <cstring>
 #include <utility>
 
+#include "api/rtpparameters.h"
 #include "common_types.h"  // NOLINT(build/include)
-#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
 #include "modules/rtp_rtcp/source/byte_io.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
-#include "rtc_base/random.h"
 
 namespace webrtc {
 namespace {
 constexpr size_t kFixedHeaderSize = 12;
 constexpr uint8_t kRtpVersion = 2;
-constexpr uint16_t kOneByteExtensionId = 0xBEDE;
-constexpr size_t kOneByteHeaderSize = 1;
+constexpr uint16_t kOneByteExtensionProfileId = 0xBEDE;
+constexpr uint16_t kTwoByteExtensionProfileId = 0x1000;
+constexpr size_t kOneByteExtensionHeaderLength = 1;
+constexpr size_t kTwoByteExtensionHeaderLength = 2;
 constexpr size_t kDefaultPacketSize = 1500;
 }  // namespace
 
-constexpr int RtpPacket::kMaxExtensionHeaders;
-constexpr int RtpPacket::kMinExtensionId;
-constexpr int RtpPacket::kMaxExtensionId;
-
 //  0                   1                   2                   3
 //  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -46,7 +43,7 @@
 // |            Contributing source (CSRC) identifiers             |
 // |                             ....                              |
 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
-// |One-byte eXtensions id = 0xbede|       length in 32bits        |
+// |  header eXtension profile id  |       length in 32bits        |
 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 // |                          Extensions                           |
 // |                             ....                              |
@@ -68,18 +65,14 @@
   RTC_DCHECK_GE(capacity, kFixedHeaderSize);
   Clear();
   if (extensions) {
-    IdentifyExtensions(*extensions);
-  } else {
-    for (size_t i = 0; i < kMaxExtensionHeaders; ++i)
-      extension_entries_[i].type = ExtensionManager::kInvalidType;
+    extensions_ = *extensions;
   }
 }
 
 RtpPacket::~RtpPacket() {}
 
 void RtpPacket::IdentifyExtensions(const ExtensionManager& extensions) {
-  for (int i = 0; i < kMaxExtensionHeaders; ++i)
-    extension_entries_[i].type = extensions.GetType(i + 1);
+  extensions_ = extensions;
 }
 
 bool RtpPacket::Parse(const uint8_t* buffer, size_t buffer_size) {
@@ -127,9 +120,8 @@
   timestamp_ = packet.timestamp_;
   ssrc_ = packet.ssrc_;
   payload_offset_ = packet.payload_offset_;
-  for (size_t i = 0; i < kMaxExtensionHeaders; ++i) {
-    extension_entries_[i] = packet.extension_entries_[i];
-  }
+  extensions_ = packet.extensions_;
+  extension_entries_ = packet.extension_entries_;
   extensions_size_ = packet.extensions_size_;
   buffer_.SetData(packet.data(), packet.headers_size());
   // Reset payload and padding.
@@ -184,19 +176,17 @@
 }
 
 rtc::ArrayView<uint8_t> RtpPacket::AllocateRawExtension(int id, size_t length) {
-  RTC_DCHECK_GE(id, kMinExtensionId);
-  RTC_DCHECK_LE(id, kMaxExtensionId);
+  RTC_DCHECK_GE(id, RtpExtension::kMinId);
+  RTC_DCHECK_LE(id, RtpExtension::kMaxId);
   RTC_DCHECK_GE(length, 1);
-  RTC_DCHECK_LE(length, 16);
-
-  ExtensionInfo* extension_entry = &extension_entries_[id - 1];
-  if (extension_entry->offset != 0) {
+  RTC_DCHECK_LE(length, RtpExtension::kMaxValueSize);
+  const ExtensionInfo* extension_entry = FindExtensionInfo(id);
+  if (extension_entry != nullptr) {
     // Extension already reserved. Check if same length is used.
     if (extension_entry->length == length)
       return rtc::MakeArrayView(WriteAt(extension_entry->offset), length);
 
-    RTC_LOG(LS_ERROR) << "Length mismatch for extension id " << id << " type "
-                      << static_cast<int>(extension_entry->type)
+    RTC_LOG(LS_ERROR) << "Length mismatch for extension id " << id
                       << ": expected "
                       << static_cast<int>(extension_entry->length)
                       << ". received " << length;
@@ -213,9 +203,51 @@
     return nullptr;
   }
 
-  size_t num_csrc = data()[0] & 0x0F;
-  size_t extensions_offset = kFixedHeaderSize + (num_csrc * 4) + 4;
-  size_t new_extensions_size = extensions_size_ + kOneByteHeaderSize + length;
+  const size_t num_csrc = data()[0] & 0x0F;
+  const size_t extensions_offset = kFixedHeaderSize + (num_csrc * 4) + 4;
+  // Determine if two-byte header is required for the extension based on id and
+  // length. Please note that a length of 0 also requires two-byte header
+  // extension. See RFC8285 Section 4.2-4.3.
+  const bool two_byte_header_required =
+      id > RtpExtension::kOneByteHeaderExtensionMaxId ||
+      length > RtpExtension::kOneByteHeaderExtensionMaxValueSize || length == 0;
+  RTC_CHECK(!two_byte_header_required ||
+            extensions_.IsMixedOneTwoByteHeaderSupported());
+
+  uint16_t profile_id;
+  if (extensions_size_ > 0) {
+    profile_id =
+        ByteReader<uint16_t>::ReadBigEndian(data() + extensions_offset - 4);
+    if (profile_id == kOneByteExtensionProfileId && two_byte_header_required) {
+      // Is buffer size big enough to fit promotion and new data field?
+      // The header extension will grow with one byte per already allocated
+      // extension + the size of the extension that is about to be allocated.
+      size_t expected_new_extensions_size =
+          extensions_size_ + extension_entries_.size() +
+          kTwoByteExtensionHeaderLength + length;
+      if (extensions_offset + expected_new_extensions_size > capacity()) {
+        RTC_LOG(LS_ERROR)
+            << "Extension cannot be registered: Not enough space left in "
+               "buffer to change to two-byte header extension and add new "
+               "extension.";
+        return nullptr;
+      }
+      // Promote already written data to two-byte header format.
+      PromoteToTwoByteHeaderExtension();
+      profile_id = kTwoByteExtensionProfileId;
+    }
+  } else {
+    // Profile specific ID, set to OneByteExtensionHeader unless
+    // TwoByteExtensionHeader is required.
+    profile_id = two_byte_header_required ? kTwoByteExtensionProfileId
+                                          : kOneByteExtensionProfileId;
+  }
+
+  const size_t extension_header_size = profile_id == kOneByteExtensionProfileId
+                                           ? kOneByteExtensionHeaderLength
+                                           : kTwoByteExtensionHeaderLength;
+  size_t new_extensions_size =
+      extensions_size_ + extension_header_size + length;
   if (extensions_offset + new_extensions_size > capacity()) {
     RTC_LOG(LS_ERROR)
         << "Extension cannot be registered: Not enough space left in buffer.";
@@ -226,20 +258,76 @@
   if (extensions_size_ == 0) {
     RTC_DCHECK_EQ(payload_offset_, kFixedHeaderSize + (num_csrc * 4));
     WriteAt(0, data()[0] | 0x10);  // Set extension bit.
-    // Profile specific ID always set to OneByteExtensionHeader.
     ByteWriter<uint16_t>::WriteBigEndian(WriteAt(extensions_offset - 4),
-                                         kOneByteExtensionId);
+                                         profile_id);
   }
 
-  uint8_t one_byte_header = rtc::dchecked_cast<uint8_t>(id) << 4;
-  one_byte_header |= rtc::dchecked_cast<uint8_t>(length - 1);
-  WriteAt(extensions_offset + extensions_size_, one_byte_header);
+  if (profile_id == kOneByteExtensionProfileId) {
+    uint8_t one_byte_header = rtc::dchecked_cast<uint8_t>(id) << 4;
+    one_byte_header |= rtc::dchecked_cast<uint8_t>(length - 1);
+    WriteAt(extensions_offset + extensions_size_, one_byte_header);
+  } else {
+    // TwoByteHeaderExtension.
+    uint8_t extension_id = rtc::dchecked_cast<uint8_t>(id);
+    WriteAt(extensions_offset + extensions_size_, extension_id);
+    uint8_t extension_length = rtc::dchecked_cast<uint8_t>(length);
+    WriteAt(extensions_offset + extensions_size_ + 1, extension_length);
+  }
 
-  extension_entry->offset = rtc::dchecked_cast<uint16_t>(
-      extensions_offset + extensions_size_ + kOneByteHeaderSize);
-  extension_entry->length = rtc::dchecked_cast<uint8_t>(length);
+  const uint16_t extension_info_offset = rtc::dchecked_cast<uint16_t>(
+      extensions_offset + extensions_size_ + extension_header_size);
+  const uint8_t extension_info_length = rtc::dchecked_cast<uint8_t>(length);
+  extension_entries_.emplace_back(id, extension_info_length,
+                                  extension_info_offset);
+
   extensions_size_ = new_extensions_size;
 
+  uint16_t extensions_size_padded =
+      SetExtensionLengthMaybeAddZeroPadding(extensions_offset);
+  payload_offset_ = extensions_offset + extensions_size_padded;
+  buffer_.SetSize(payload_offset_);
+  return rtc::MakeArrayView(WriteAt(extension_info_offset),
+                            extension_info_length);
+}
+
+void RtpPacket::PromoteToTwoByteHeaderExtension() {
+  size_t num_csrc = data()[0] & 0x0F;
+  size_t extensions_offset = kFixedHeaderSize + (num_csrc * 4) + 4;
+
+  RTC_CHECK_GT(extension_entries_.size(), 0);
+  RTC_CHECK_EQ(payload_size_, 0);
+  RTC_CHECK_EQ(kOneByteExtensionProfileId, ByteReader<uint16_t>::ReadBigEndian(
+                                               data() + extensions_offset - 4));
+  // Rewrite data.
+  // Each extension adds one to the offset. The write-read delta for the last
+  // extension is therefore the same as the number of extension entries.
+  size_t write_read_delta = extension_entries_.size();
+  for (auto extension_entry = extension_entries_.rbegin();
+       extension_entry != extension_entries_.rend(); ++extension_entry) {
+    size_t read_index = extension_entry->offset;
+    size_t write_index = read_index + write_read_delta;
+    // Update offset.
+    extension_entry->offset = rtc::dchecked_cast<uint16_t>(write_index);
+    // Copy data. Use memmove since read/write regions may overlap.
+    memmove(WriteAt(write_index), data() + read_index, extension_entry->length);
+    // Rewrite id and length.
+    WriteAt(--write_index, extension_entry->length);
+    WriteAt(--write_index, extension_entry->id);
+    --write_read_delta;
+  }
+
+  // Update profile header, extensions length, and zero padding.
+  ByteWriter<uint16_t>::WriteBigEndian(WriteAt(extensions_offset - 4),
+                                       kTwoByteExtensionProfileId);
+  extensions_size_ += extension_entries_.size();
+  uint16_t extensions_size_padded =
+      SetExtensionLengthMaybeAddZeroPadding(extensions_offset);
+  payload_offset_ = extensions_offset + extensions_size_padded;
+  buffer_.SetSize(payload_offset_);
+}
+
+uint16_t RtpPacket::SetExtensionLengthMaybeAddZeroPadding(
+    size_t extensions_offset) {
   // Update header length field.
   uint16_t extensions_words = rtc::dchecked_cast<uint16_t>(
       (extensions_size_ + 3) / 4);  // Wrap up to 32bit.
@@ -249,9 +337,7 @@
   size_t extension_padding_size = 4 * extensions_words - extensions_size_;
   memset(WriteAt(extensions_offset + extensions_size_), 0,
          extension_padding_size);
-  payload_offset_ = extensions_offset + 4 * extensions_words;
-  buffer_.SetSize(payload_offset_);
-  return rtc::MakeArrayView(WriteAt(extension_entry->offset), length);
+  return 4 * extensions_words;
 }
 
 uint8_t* RtpPacket::AllocatePayload(size_t size_bytes) {
@@ -272,22 +358,20 @@
   return WriteAt(payload_offset_);
 }
 
-bool RtpPacket::SetPadding(uint8_t size_bytes, Random* random) {
-  RTC_DCHECK(random);
-  if (payload_offset_ + payload_size_ + size_bytes > capacity()) {
-    RTC_LOG(LS_WARNING) << "Cannot set padding size " << size_bytes << ", only "
+bool RtpPacket::SetPadding(size_t padding_bytes) {
+  if (payload_offset_ + payload_size_ + padding_bytes > capacity()) {
+    RTC_LOG(LS_WARNING) << "Cannot set padding size " << padding_bytes
+                        << ", only "
                         << (capacity() - payload_offset_ - payload_size_)
                         << " bytes left in buffer.";
     return false;
   }
-  padding_size_ = size_bytes;
+  padding_size_ = rtc::dchecked_cast<uint8_t>(padding_bytes);
   buffer_.SetSize(payload_offset_ + payload_size_ + padding_size_);
   if (padding_size_ > 0) {
     size_t padding_offset = payload_offset_ + payload_size_;
     size_t padding_end = padding_offset + padding_size_;
-    for (size_t offset = padding_offset; offset < padding_end - 1; ++offset) {
-      WriteAt(offset, random->Rand<uint8_t>());
-    }
+    memset(WriteAt(padding_offset), 0, padding_size_ - 1);
     WriteAt(padding_end - 1, padding_size_);
     WriteAt(0, data()[0] | 0x20);  // Set padding bit.
   } else {
@@ -306,10 +390,7 @@
   payload_size_ = 0;
   padding_size_ = 0;
   extensions_size_ = 0;
-  for (ExtensionInfo& location : extension_entries_) {
-    location.offset = 0;
-    location.length = 0;
-  }
+  extension_entries_.clear();
 
   memset(WriteAt(0), 0, kFixedHeaderSize);
   buffer_.SetSize(kFixedHeaderSize);
@@ -349,10 +430,7 @@
   }
 
   extensions_size_ = 0;
-  for (ExtensionInfo& location : extension_entries_) {
-    location.offset = 0;
-    location.length = 0;
-  }
+  extension_entries_.clear();
   if (has_extension) {
     /* RTP header extension, RFC 3550.
      0                   1                   2                   3
@@ -375,42 +453,56 @@
     if (extension_offset + extensions_capacity > size) {
       return false;
     }
-    if (profile != kOneByteExtensionId) {
+    if (profile != kOneByteExtensionProfileId &&
+        profile != kTwoByteExtensionProfileId) {
       RTC_LOG(LS_WARNING) << "Unsupported rtp extension " << profile;
     } else {
+      size_t extension_header_length = profile == kOneByteExtensionProfileId
+                                           ? kOneByteExtensionHeaderLength
+                                           : kTwoByteExtensionHeaderLength;
+      constexpr uint8_t kPaddingByte = 0;
       constexpr uint8_t kPaddingId = 0;
-      constexpr uint8_t kReservedId = 15;
-      while (extensions_size_ + kOneByteHeaderSize < extensions_capacity) {
-        int id = buffer[extension_offset + extensions_size_] >> 4;
-        if (id == kReservedId) {
-          break;
-        } else if (id == kPaddingId) {
+      constexpr uint8_t kOneByteHeaderExtensionReservedId = 15;
+      while (extensions_size_ + extension_header_length < extensions_capacity) {
+        if (buffer[extension_offset + extensions_size_] == kPaddingByte) {
           extensions_size_++;
           continue;
         }
-        uint8_t length =
-            1 + (buffer[extension_offset + extensions_size_] & 0xf);
-        if (extensions_size_ + kOneByteHeaderSize + length >
+        int id;
+        uint8_t length;
+        if (profile == kOneByteExtensionProfileId) {
+          id = buffer[extension_offset + extensions_size_] >> 4;
+          length = 1 + (buffer[extension_offset + extensions_size_] & 0xf);
+          if (id == kOneByteHeaderExtensionReservedId ||
+              (id == kPaddingId && length != 1)) {
+            break;
+          }
+        } else {
+          id = buffer[extension_offset + extensions_size_];
+          length = buffer[extension_offset + extensions_size_ + 1];
+        }
+
+        if (extensions_size_ + extension_header_length + length >
             extensions_capacity) {
           RTC_LOG(LS_WARNING) << "Oversized rtp header extension.";
           break;
         }
 
-        size_t idx = id - 1;
-        if (extension_entries_[idx].length != 0) {
+        ExtensionInfo& extension_info = FindOrCreateExtensionInfo(id);
+        if (extension_info.length != 0) {
           RTC_LOG(LS_VERBOSE)
               << "Duplicate rtp header extension id " << id << ". Overwriting.";
         }
 
         size_t offset =
-            extension_offset + extensions_size_ + kOneByteHeaderSize;
+            extension_offset + extensions_size_ + extension_header_length;
         if (!rtc::IsValueInRangeForNumericType<uint16_t>(offset)) {
           RTC_DLOG(LS_WARNING) << "Oversized rtp header extension.";
           break;
         }
-        extension_entries_[idx].offset = static_cast<uint16_t>(offset);
-        extension_entries_[idx].length = length;
-        extensions_size_ += kOneByteHeaderSize + length;
+        extension_info.offset = static_cast<uint16_t>(offset);
+        extension_info.length = length;
+        extensions_size_ += extension_header_length + length;
       }
     }
     payload_offset_ = extension_offset + extensions_capacity;
@@ -423,30 +515,59 @@
   return true;
 }
 
-rtc::ArrayView<const uint8_t> RtpPacket::FindExtension(
-    ExtensionType type) const {
+const RtpPacket::ExtensionInfo* RtpPacket::FindExtensionInfo(int id) const {
   for (const ExtensionInfo& extension : extension_entries_) {
-    if (extension.type == type) {
-      if (extension.length == 0) {
-        // Extension is registered but not set.
-        return nullptr;
-      }
-      return rtc::MakeArrayView(data() + extension.offset, extension.length);
+    if (extension.id == id) {
+      return &extension;
     }
   }
   return nullptr;
 }
 
-rtc::ArrayView<uint8_t> RtpPacket::AllocateExtension(ExtensionType type,
-                                                     size_t length) {
-  for (int i = 0; i < kMaxExtensionHeaders; ++i) {
-    if (extension_entries_[i].type == type) {
-      int extension_id = i + 1;
-      return AllocateRawExtension(extension_id, length);
+RtpPacket::ExtensionInfo& RtpPacket::FindOrCreateExtensionInfo(int id) {
+  for (ExtensionInfo& extension : extension_entries_) {
+    if (extension.id == id) {
+      return extension;
     }
   }
-  // Extension not registered.
-  return nullptr;
+  extension_entries_.emplace_back(id);
+  return extension_entries_.back();
+}
+
+rtc::ArrayView<const uint8_t> RtpPacket::FindExtension(
+    ExtensionType type) const {
+  uint8_t id = extensions_.GetId(type);
+  if (id == ExtensionManager::kInvalidId) {
+    // Extension not registered.
+    return nullptr;
+  }
+  ExtensionInfo const* extension_info = FindExtensionInfo(id);
+  if (extension_info == nullptr) {
+    return nullptr;
+  }
+  return rtc::MakeArrayView(data() + extension_info->offset,
+                            extension_info->length);
+}
+
+rtc::ArrayView<uint8_t> RtpPacket::AllocateExtension(ExtensionType type,
+                                                     size_t length) {
+  // TODO(webrtc:7990): Add support for empty extensions (length==0).
+  if (length == 0 || length > RtpExtension::kMaxValueSize ||
+      (!extensions_.IsMixedOneTwoByteHeaderSupported() &&
+       length > RtpExtension::kOneByteHeaderExtensionMaxValueSize)) {
+    return nullptr;
+  }
+
+  uint8_t id = extensions_.GetId(type);
+  if (id == ExtensionManager::kInvalidId) {
+    // Extension not registered.
+    return nullptr;
+  }
+  if (!extensions_.IsMixedOneTwoByteHeaderSupported() &&
+      id > RtpExtension::kOneByteHeaderExtensionMaxId) {
+    return nullptr;
+  }
+  return AllocateRawExtension(id, length);
 }
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_packet.h b/modules/rtp_rtcp/source/rtp_packet.h
index 21adf62..3d7d90c 100644
--- a/modules/rtp_rtcp/source/rtp_packet.h
+++ b/modules/rtp_rtcp/source/rtp_packet.h
@@ -13,20 +13,18 @@
 #include <vector>
 
 #include "api/array_view.h"
+#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "rtc_base/copyonwritebuffer.h"
+#include "rtc_base/deprecation.h"
 
 namespace webrtc {
-class RtpHeaderExtensionMap;
 class Random;
 
 class RtpPacket {
  public:
   using ExtensionType = RTPExtensionType;
   using ExtensionManager = RtpHeaderExtensionMap;
-  static constexpr int kMaxExtensionHeaders = 14;
-  static constexpr int kMinExtensionId = 1;
-  static constexpr int kMaxExtensionId = 14;
 
   // |extensions| required for SetExtension/ReserveExtension functions during
   // packet creating and used if available in Parse function.
@@ -100,6 +98,10 @@
   template <typename Extension, typename... Values>
   bool GetExtension(Values...) const;
 
+  // Returns view of the raw extension or empty view on failure.
+  template <typename Extension>
+  rtc::ArrayView<const uint8_t> GetRawExtension() const;
+
   template <typename Extension, typename... Values>
   bool SetExtension(Values...);
 
@@ -110,19 +112,35 @@
   uint8_t* SetPayloadSize(size_t size_bytes);
   // Same as SetPayloadSize but doesn't guarantee to keep current payload.
   uint8_t* AllocatePayload(size_t size_bytes);
-  bool SetPadding(uint8_t size_bytes, Random* random);
+  RTC_DEPRECATED
+  bool SetPadding(uint8_t size_bytes, Random* random) {
+    return SetPadding(size_bytes);
+  }
+
+  bool SetPadding(size_t padding_size);
 
  private:
   struct ExtensionInfo {
-    ExtensionType type;
-    uint16_t offset;
+    explicit ExtensionInfo(uint8_t id) : ExtensionInfo(id, 0, 0) {}
+    ExtensionInfo(uint8_t id, uint8_t length, uint16_t offset)
+        : id(id), length(length), offset(offset) {}
+    uint8_t id;
     uint8_t length;
+    uint16_t offset;
   };
 
   // Helper function for Parse. Fill header fields using data in given buffer,
   // but does not touch packet own buffer, leaving packet in invalid state.
   bool ParseBuffer(const uint8_t* buffer, size_t size);
 
+  // Returns pointer to extension info for a given id. Returns nullptr if not
+  // found.
+  const ExtensionInfo* FindExtensionInfo(int id) const;
+
+  // Returns reference to extension info for a given id. Creates a new entry
+  // with the specified id if not found.
+  ExtensionInfo& FindOrCreateExtensionInfo(int id);
+
   // Find an extension |type|.
   // Returns view of the raw extension or empty view on failure.
   rtc::ArrayView<const uint8_t> FindExtension(ExtensionType type) const;
@@ -131,6 +149,12 @@
   // Returns empty arrayview on failure.
   rtc::ArrayView<uint8_t> AllocateRawExtension(int id, size_t length);
 
+  // Promotes existing one-byte header extensions to two-byte header extensions
+  // by rewriting the data and updates the corresponding extension offsets.
+  void PromoteToTwoByteHeaderExtension();
+
+  uint16_t SetExtensionLengthMaybeAddZeroPadding(size_t extensions_offset);
+
   // Find or allocate an extension |type|. Returns view of size |length|
   // to write raw extension to or an empty view on failure.
   rtc::ArrayView<uint8_t> AllocateExtension(ExtensionType type, size_t length);
@@ -148,7 +172,8 @@
   size_t payload_offset_;  // Match header size with csrcs and extensions.
   size_t payload_size_;
 
-  ExtensionInfo extension_entries_[kMaxExtensionHeaders];
+  ExtensionManager extensions_;
+  std::vector<ExtensionInfo> extension_entries_;
   size_t extensions_size_ = 0;  // Unaligned.
   rtc::CopyOnWriteBuffer buffer_;
 };
@@ -166,11 +191,14 @@
   return Extension::Parse(raw, values...);
 }
 
+template <typename Extension>
+rtc::ArrayView<const uint8_t> RtpPacket::GetRawExtension() const {
+  return FindExtension(Extension::kId);
+}
+
 template <typename Extension, typename... Values>
 bool RtpPacket::SetExtension(Values... values) {
   const size_t value_size = Extension::ValueSize(values...);
-  if (value_size == 0 || value_size > 16)
-    return false;
   auto buffer = AllocateExtension(Extension::kId, value_size);
   if (buffer.empty())
     return false;
diff --git a/modules/rtp_rtcp/source/rtp_packet_history.cc b/modules/rtp_rtcp/source/rtp_packet_history.cc
index 62f1c82..211077b 100644
--- a/modules/rtp_rtcp/source/rtp_packet_history.cc
+++ b/modules/rtp_rtcp/source/rtp_packet_history.cc
@@ -27,8 +27,7 @@
 
 // Utility function to get the absolute difference in size between the provided
 // target size and the size of packet.
-size_t SizeDiff(const std::unique_ptr<RtpPacketToSend>& packet, size_t size) {
-  size_t packet_size = packet->size();
+size_t SizeDiff(size_t packet_size, size_t size) {
   if (packet_size > size) {
     return packet_size - size;
   }
@@ -98,6 +97,15 @@
   const uint16_t rtp_seq_no = packet->SequenceNumber();
   StoredPacket& stored_packet = packet_history_[rtp_seq_no];
   RTC_DCHECK(stored_packet.packet == nullptr);
+  if (stored_packet.packet) {
+    // It is an error if this happen. But it can happen if the sequence numbers
+    // for some reason restart without that the history has been reset.
+    auto size_iterator = packet_size_.find(stored_packet.packet->size());
+    if (size_iterator != packet_size_.end() &&
+        size_iterator->second == stored_packet.packet->SequenceNumber()) {
+      packet_size_.erase(size_iterator);
+    }
+  }
   stored_packet.packet = std::move(packet);
 
   if (stored_packet.packet->capture_time_ms() <= 0) {
@@ -110,6 +118,10 @@
   if (!start_seqno_) {
     start_seqno_ = rtp_seq_no;
   }
+  // Store the sequence number of the last send packet with this size.
+  if (type != StorageType::kDontRetransmit) {
+    packet_size_[stored_packet.packet->size()] = rtp_seq_no;
+  }
 }
 
 std::unique_ptr<RtpPacketToSend> RtpPacketHistory::GetPacketAndSetSendTime(
@@ -186,28 +198,45 @@
     size_t packet_length) const {
   // TODO(sprang): Make this smarter, taking retransmit count etc into account.
   rtc::CritScope cs(&lock_);
-  if (packet_length < kMinPacketRequestBytes || packet_history_.empty()) {
+  if (packet_length < kMinPacketRequestBytes || packet_size_.empty()) {
     return nullptr;
   }
 
-  size_t min_diff = std::numeric_limits<size_t>::max();
-  RtpPacketToSend* best_packet = nullptr;
-  for (auto& it : packet_history_) {
-    size_t diff = SizeDiff(it.second.packet, packet_length);
-    if (!min_diff || diff < min_diff) {
-      min_diff = diff;
-      best_packet = it.second.packet.get();
-      if (diff == 0) {
-        break;
-      }
-    }
+  auto size_iter_upper = packet_size_.upper_bound(packet_length);
+  auto size_iter_lower = size_iter_upper;
+  if (size_iter_upper == packet_size_.end()) {
+    --size_iter_upper;
   }
+  if (size_iter_lower != packet_size_.begin()) {
+    --size_iter_lower;
+  }
+  const size_t upper_bound_diff =
+      SizeDiff(size_iter_upper->first, packet_length);
+  const size_t lower_bound_diff =
+      SizeDiff(size_iter_lower->first, packet_length);
 
+  const uint16_t seq_no = upper_bound_diff < lower_bound_diff
+                              ? size_iter_upper->second
+                              : size_iter_lower->second;
+  auto history_it = packet_history_.find(seq_no);
+  if (history_it == packet_history_.end()) {
+    RTC_LOG(LS_ERROR) << "Can't find packet in history with seq_no" << seq_no;
+    RTC_DCHECK(false);
+    return nullptr;
+  }
+  if (!history_it->second.packet) {
+    RTC_LOG(LS_ERROR) << "Packet pointer is null in history for seq_no"
+                      << seq_no;
+    RTC_DCHECK(false);
+    return nullptr;
+  }
+  RtpPacketToSend* best_packet = history_it->second.packet.get();
   return absl::make_unique<RtpPacketToSend>(*best_packet);
 }
 
 void RtpPacketHistory::Reset() {
   packet_history_.clear();
+  packet_size_.clear();
   start_seqno_.reset();
 }
 
@@ -272,6 +301,12 @@
     start_seqno_.reset();
   }
 
+  auto size_iterator = packet_size_.find(rtp_packet->size());
+  if (size_iterator != packet_size_.end() &&
+      size_iterator->second == rtp_packet->SequenceNumber()) {
+    packet_size_.erase(size_iterator);
+  }
+
   return rtp_packet;
 }
 
diff --git a/modules/rtp_rtcp/source/rtp_packet_history.h b/modules/rtp_rtcp/source/rtp_packet_history.h
index 433da0e..1646ba7 100644
--- a/modules/rtp_rtcp/source/rtp_packet_history.h
+++ b/modules/rtp_rtcp/source/rtp_packet_history.h
@@ -136,6 +136,7 @@
 
   // Map from rtp sequence numbers to stored packet.
   std::map<uint16_t, StoredPacket> packet_history_ RTC_GUARDED_BY(lock_);
+  std::map<size_t, uint16_t> packet_size_ RTC_GUARDED_BY(lock_);
 
   // The earliest packet in the history. This might not be the lowest sequence
   // number, in case there is a wraparound.
diff --git a/modules/rtp_rtcp/source/rtp_packet_history_unittest.cc b/modules/rtp_rtcp/source/rtp_packet_history_unittest.cc
index ab5aeb0..85e9eb2 100644
--- a/modules/rtp_rtcp/source/rtp_packet_history_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_packet_history_unittest.cc
@@ -16,6 +16,7 @@
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
 #include "system_wrappers/include/clock.h"
+#include "test/gmock.h"
 #include "test/gtest.h"
 
 namespace webrtc {
@@ -488,4 +489,92 @@
   EXPECT_EQ(target_packet_size,
             hist_.GetBestFittingPacket(target_packet_size)->size());
 }
+
+TEST_F(RtpPacketHistoryTest,
+       GetBestFittingPacketReturnsNextPacketWhenBestPacketHasBeenCulled) {
+  hist_.SetStorePacketsStatus(StorageMode::kStoreAndCull, 10);
+  std::unique_ptr<RtpPacketToSend> packet = CreateRtpPacket(kStartSeqNum);
+  packet->SetPayloadSize(50);
+  const size_t target_packet_size = packet->size();
+  hist_.PutRtpPacket(std::move(packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+
+  packet = hist_.GetBestFittingPacket(target_packet_size + 2);
+  ASSERT_THAT(packet, ::testing::NotNull());
+
+  // Send the packet and advance time past where packet expires.
+  ASSERT_THAT(hist_.GetPacketAndSetSendTime(kStartSeqNum, false),
+              ::testing::NotNull());
+  fake_clock_.AdvanceTimeMilliseconds(
+      RtpPacketHistory::kPacketCullingDelayFactor *
+      RtpPacketHistory::kMinPacketDurationMs);
+
+  packet = CreateRtpPacket(kStartSeqNum + 1);
+  packet->SetPayloadSize(100);
+  hist_.PutRtpPacket(std::move(packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+  ASSERT_FALSE(hist_.GetPacketState(kStartSeqNum, false));
+
+  auto best_packet = hist_.GetBestFittingPacket(target_packet_size + 2);
+  ASSERT_THAT(best_packet, ::testing::NotNull());
+  EXPECT_EQ(best_packet->SequenceNumber(), kStartSeqNum + 1);
+}
+
+TEST_F(RtpPacketHistoryTest, GetBestFittingPacketReturnLastPacketWhenSameSize) {
+  const size_t kTargetSize = 500;
+  hist_.SetStorePacketsStatus(StorageMode::kStore, 10);
+
+  // Add two packets of same size.
+  std::unique_ptr<RtpPacketToSend> packet = CreateRtpPacket(kStartSeqNum);
+  packet->SetPayloadSize(kTargetSize);
+  hist_.PutRtpPacket(std::move(packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+  packet = CreateRtpPacket(kStartSeqNum + 1);
+  packet->SetPayloadSize(kTargetSize);
+  hist_.PutRtpPacket(std::move(packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+
+  auto best_packet = hist_.GetBestFittingPacket(123);
+  ASSERT_THAT(best_packet, ::testing::NotNull());
+  EXPECT_EQ(best_packet->SequenceNumber(), kStartSeqNum + 1);
+}
+
+TEST_F(RtpPacketHistoryTest,
+       GetBestFittingPacketReturnsPacketWithSmallestDiff) {
+  const size_t kTargetSize = 500;
+  hist_.SetStorePacketsStatus(StorageMode::kStore, 10);
+
+  // Add two packets of very different size.
+  std::unique_ptr<RtpPacketToSend> small_packet = CreateRtpPacket(kStartSeqNum);
+  small_packet->SetPayloadSize(kTargetSize);
+  hist_.PutRtpPacket(std::move(small_packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+
+  auto large_packet = CreateRtpPacket(kStartSeqNum + 1);
+  large_packet->SetPayloadSize(kTargetSize * 2);
+  hist_.PutRtpPacket(std::move(large_packet), kAllowRetransmission,
+                     fake_clock_.TimeInMilliseconds());
+
+  ASSERT_THAT(hist_.GetBestFittingPacket(kTargetSize), ::testing::NotNull());
+  EXPECT_EQ(hist_.GetBestFittingPacket(kTargetSize)->SequenceNumber(),
+            kStartSeqNum);
+
+  ASSERT_THAT(hist_.GetBestFittingPacket(kTargetSize * 2),
+              ::testing::NotNull());
+  EXPECT_EQ(hist_.GetBestFittingPacket(kTargetSize * 2)->SequenceNumber(),
+            kStartSeqNum + 1);
+}
+
+TEST_F(RtpPacketHistoryTest,
+       GetBestFittingPacketIgnoresNoneRetransmitablePackets) {
+  hist_.SetStorePacketsStatus(StorageMode::kStoreAndCull, 10);
+  std::unique_ptr<RtpPacketToSend> packet = CreateRtpPacket(kStartSeqNum);
+  packet->SetPayloadSize(50);
+  hist_.PutRtpPacket(std::move(packet), kDontRetransmit,
+                     fake_clock_.TimeInMilliseconds());
+  EXPECT_THAT(hist_.GetBestFittingPacket(50), ::testing::IsNull());
+  EXPECT_THAT(hist_.GetPacketAndSetSendTime(kStartSeqNum, false),
+              ::testing::NotNull());
+}
+
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_packet_received.cc b/modules/rtp_rtcp/source/rtp_packet_received.cc
index 6acc9b5..c8deb99 100644
--- a/modules/rtp_rtcp/source/rtp_packet_received.cc
+++ b/modules/rtp_rtcp/source/rtp_packet_received.cc
@@ -61,6 +61,8 @@
           &header->extension.videoContentType);
   header->extension.has_video_timing =
       GetExtension<VideoTimingExtension>(&header->extension.video_timing);
+  header->extension.has_frame_marking =
+      GetExtension<FrameMarkingExtension>(&header->extension.frame_marking);
   GetExtension<RtpStreamId>(&header->extension.stream_id);
   GetExtension<RepairedRtpStreamId>(&header->extension.repaired_stream_id);
   GetExtension<RtpMid>(&header->extension.mid);
diff --git a/modules/rtp_rtcp/source/rtp_packet_unittest.cc b/modules/rtp_rtcp/source/rtp_packet_unittest.cc
index e19f191..648ddc3 100644
--- a/modules/rtp_rtcp/source/rtp_packet_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_packet_unittest.cc
@@ -18,6 +18,9 @@
 
 namespace webrtc {
 namespace {
+
+using ::testing::Each;
+using ::testing::ElementsAre;
 using ::testing::ElementsAreArray;
 using ::testing::IsEmpty;
 using ::testing::make_tuple;
@@ -33,11 +36,13 @@
 constexpr uint8_t kRtpStreamIdExtensionId = 0xa;
 constexpr uint8_t kRtpMidExtensionId = 0xb;
 constexpr uint8_t kVideoTimingExtensionId = 0xc;
+constexpr uint8_t kTwoByteExtensionId = 0xf0;
 constexpr int32_t kTimeOffset = 0x56ce;
 constexpr bool kVoiceActive = true;
 constexpr uint8_t kAudioLevel = 0x5a;
 constexpr char kStreamId[] = "streamid";
 constexpr char kMid[] = "mid";
+constexpr char kLongMid[] = "extra-long string to test two-byte header";
 constexpr size_t kMaxPaddingSize = 224u;
 // clang-format off
 constexpr uint8_t kMinimumPacket[] = {
@@ -60,6 +65,45 @@
     0x12, 0x00, 0x56, 0xce,
     0x90, 0x80|kAudioLevel, 0x00, 0x00};
 
+constexpr uint8_t kPacketWithTwoByteExtensionIdLast[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0x10, 0x00, 0x00, 0x04,
+    0x01, 0x03, 0x00, 0x56,
+    0xce, 0x09, 0x01, 0x80|kAudioLevel,
+    kTwoByteExtensionId, 0x03, 0x00, 0x30,  // => 0x00 0x30 0x22
+    0x22, 0x00, 0x00, 0x00};                // => Playout delay.min_ms = 3*10
+                                            // => Playout delay.max_ms = 34*10
+
+constexpr uint8_t kPacketWithTwoByteExtensionIdFirst[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0x10, 0x00, 0x00, 0x04,
+    kTwoByteExtensionId, 0x03, 0x00, 0x30,  // => 0x00 0x30 0x22
+    0x22, 0x01, 0x03, 0x00,                 // => Playout delay.min_ms = 3*10
+    0x56, 0xce, 0x09, 0x01,                 // => Playout delay.max_ms = 34*10
+    0x80|kAudioLevel, 0x00, 0x00, 0x00};
+
+constexpr uint8_t kPacketWithTOAndALInvalidPadding[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0xbe, 0xde, 0x00, 0x03,
+    0x12, 0x00, 0x56, 0xce,
+    0x00, 0x02, 0x00, 0x00,  // 0x02 is invalid padding, parsing should stop.
+    0x90, 0x80|kAudioLevel, 0x00, 0x00};
+
+constexpr uint8_t kPacketWithTOAndALReservedExtensionId[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0xbe, 0xde, 0x00, 0x03,
+    0x12, 0x00, 0x56, 0xce,
+    0x00, 0xF0, 0x00, 0x00,  // F is a reserved id, parsing should stop.
+    0x90, 0x80|kAudioLevel, 0x00, 0x00};
+
 constexpr uint8_t kPacketWithRsid[] = {
     0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
     0x65, 0x43, 0x12, 0x78,
@@ -90,6 +134,35 @@
     'p', 'a', 'y', 'l', 'o', 'a', 'd',
     'p', 'a', 'd', 'd', 'i', 'n', 'g', kPacketPaddingSize};
 
+constexpr uint8_t kPacketWithTwoByteHeaderExtension[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0x10, 0x00, 0x00, 0x02,  // Two-byte header extension profile id + length.
+    kTwoByteExtensionId, 0x03, 0x00, 0x56,
+    0xce, 0x00, 0x00, 0x00};
+
+constexpr uint8_t kPacketWithLongTwoByteHeaderExtension[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0x10, 0x00, 0x00, 0x0B,  // Two-byte header extension profile id + length.
+    kTwoByteExtensionId, 0x29, 'e', 'x',
+    't', 'r', 'a', '-', 'l', 'o', 'n', 'g',
+    ' ', 's', 't', 'r', 'i', 'n', 'g', ' ',
+    't', 'o', ' ', 't', 'e', 's', 't', ' ',
+    't', 'w', 'o', '-', 'b', 'y', 't', 'e',
+    ' ', 'h', 'e', 'a', 'd', 'e', 'r', 0x00};
+
+constexpr uint8_t kPacketWithTwoByteHeaderExtensionWithPadding[] = {
+    0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
+    0x65, 0x43, 0x12, 0x78,
+    0x12, 0x34, 0x56, 0x78,
+    0x10, 0x00, 0x00, 0x03,  // Two-byte header extension profile id + length.
+    kTwoByteExtensionId, 0x03, 0x00, 0x56,
+    0xce, 0x00, 0x00, 0x00,  // Three padding bytes.
+    kAudioLevelExtensionId, 0x01, 0x80|kAudioLevel, 0x00};
+
 constexpr uint8_t kPacketWithInvalidExtension[] = {
     0x90, kPayloadType, kSeqNumFirstByte, kSeqNumSecondByte,
     0x65, 0x43, 0x12, 0x78,  // kTimestamp.
@@ -152,6 +225,51 @@
               ElementsAreArray(packet.data(), packet.size()));
 }
 
+TEST(RtpPacketTest, CreateWithTwoByteHeaderExtensionFirst) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.SetMixedOneTwoByteHeaderSupported(true);
+  extensions.Register(kRtpExtensionTransmissionTimeOffset,
+                      kTransmissionOffsetExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  extensions.Register(kRtpExtensionPlayoutDelay, kTwoByteExtensionId);
+  RtpPacketToSend packet(&extensions);
+  packet.SetPayloadType(kPayloadType);
+  packet.SetSequenceNumber(kSeqNum);
+  packet.SetTimestamp(kTimestamp);
+  packet.SetSsrc(kSsrc);
+  // Set extension that requires two-byte header.
+  PlayoutDelay playoutDelay = {30, 340};
+  ASSERT_TRUE(packet.SetExtension<PlayoutDelayLimits>(playoutDelay));
+  packet.SetExtension<TransmissionOffset>(kTimeOffset);
+  packet.SetExtension<AudioLevel>(kVoiceActive, kAudioLevel);
+  EXPECT_THAT(kPacketWithTwoByteExtensionIdFirst,
+              ElementsAreArray(packet.data(), packet.size()));
+}
+
+TEST(RtpPacketTest, CreateWithTwoByteHeaderExtensionLast) {
+  // This test will trigger RtpPacket::PromoteToTwoByteHeaderExtension().
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.SetMixedOneTwoByteHeaderSupported(true);
+  extensions.Register(kRtpExtensionTransmissionTimeOffset,
+                      kTransmissionOffsetExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  extensions.Register(kRtpExtensionPlayoutDelay, kTwoByteExtensionId);
+  RtpPacketToSend packet(&extensions);
+  packet.SetPayloadType(kPayloadType);
+  packet.SetSequenceNumber(kSeqNum);
+  packet.SetTimestamp(kTimestamp);
+  packet.SetSsrc(kSsrc);
+  packet.SetExtension<TransmissionOffset>(kTimeOffset);
+  packet.SetExtension<AudioLevel>(kVoiceActive, kAudioLevel);
+  EXPECT_THAT(kPacketWithTOAndAL,
+              ElementsAreArray(packet.data(), packet.size()));
+  // Set extension that requires two-byte header.
+  PlayoutDelay playoutDelay = {30, 340};
+  ASSERT_TRUE(packet.SetExtension<PlayoutDelayLimits>(playoutDelay));
+  EXPECT_THAT(kPacketWithTwoByteExtensionIdLast,
+              ElementsAreArray(packet.data(), packet.size()));
+}
+
 TEST(RtpPacketTest, CreateWithDynamicSizedExtensions) {
   RtpPacketToSend::ExtensionManager extensions;
   extensions.Register<RtpStreamId>(kRtpStreamIdExtensionId);
@@ -196,6 +314,14 @@
   EXPECT_FALSE(packet.SetExtension<RtpMid>(kLongMid));
 }
 
+TEST(RtpPacketTest, TryToCreateTwoByteHeaderNotSupported) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionAudioLevel, kTwoByteExtensionId);
+  RtpPacketToSend packet(&extensions);
+  // Set extension that requires two-byte header.
+  EXPECT_FALSE(packet.SetExtension<AudioLevel>(kVoiceActive, kAudioLevel));
+}
+
 TEST(RtpPacketTest, CreateWithMaxSizeHeaderExtension) {
   const std::string kValue = "123456789abcdef";
   RtpPacket::ExtensionManager extensions;
@@ -242,11 +368,10 @@
   packet.SetSequenceNumber(kSeqNum);
   packet.SetTimestamp(kTimestamp);
   packet.SetSsrc(kSsrc);
-  Random random(0x123456789);
 
   EXPECT_LT(packet.size(), packet.capacity());
-  EXPECT_FALSE(packet.SetPadding(kPaddingSize + 1, &random));
-  EXPECT_TRUE(packet.SetPadding(kPaddingSize, &random));
+  EXPECT_FALSE(packet.SetPadding(kPaddingSize + 1));
+  EXPECT_TRUE(packet.SetPadding(kPaddingSize));
   EXPECT_EQ(packet.size(), packet.capacity());
 }
 
@@ -258,13 +383,48 @@
   packet.SetTimestamp(kTimestamp);
   packet.SetSsrc(kSsrc);
   packet.SetPayloadSize(kPayloadSize);
-  Random r(0x123456789);
 
   EXPECT_LT(packet.size(), packet.capacity());
-  EXPECT_TRUE(packet.SetPadding(kMaxPaddingSize, &r));
+  EXPECT_TRUE(packet.SetPadding(kMaxPaddingSize));
   EXPECT_EQ(packet.size(), packet.capacity());
 }
 
+TEST(RtpPacketTest, WritesPaddingSizeToLastByte) {
+  const size_t kPaddingSize = 5;
+  RtpPacket packet;
+
+  EXPECT_TRUE(packet.SetPadding(kPaddingSize));
+  EXPECT_EQ(packet.data()[packet.size() - 1], kPaddingSize);
+}
+
+TEST(RtpPacketTest, UsesZerosForPadding) {
+  const size_t kPaddingSize = 5;
+  RtpPacket packet;
+
+  EXPECT_TRUE(packet.SetPadding(kPaddingSize));
+  EXPECT_THAT(rtc::MakeArrayView(packet.data() + 12, kPaddingSize - 1),
+              Each(0));
+}
+
+TEST(RtpPacketTest, CreateOneBytePadding) {
+  size_t kPayloadSize = 123;
+  RtpPacket packet(nullptr, 12 + kPayloadSize + 1);
+  packet.SetPayloadSize(kPayloadSize);
+
+  EXPECT_TRUE(packet.SetPadding(1));
+
+  EXPECT_EQ(packet.size(), 12 + kPayloadSize + 1);
+  EXPECT_EQ(packet.padding_size(), 1u);
+}
+
+TEST(RtpPacketTest, FailsToAddPaddingWithoutCapacity) {
+  size_t kPayloadSize = 123;
+  RtpPacket packet(nullptr, 12 + kPayloadSize);
+  packet.SetPayloadSize(kPayloadSize);
+
+  EXPECT_FALSE(packet.SetPadding(1));
+}
+
 TEST(RtpPacketTest, ParseMinimum) {
   RtpPacketReceived packet;
   EXPECT_TRUE(packet.Parse(kMinimumPacket, sizeof(kMinimumPacket)));
@@ -308,6 +468,38 @@
   EXPECT_EQ(0u, packet.padding_size());
 }
 
+TEST(RtpPacketTest, GetRawExtensionWhenPresent) {
+  constexpr uint8_t kRawPacket[] = {
+      // comment for clang-format to align kRawPacket nicer.
+      0x90, 100,  0x5e, 0x04,  //
+      0x65, 0x43, 0x12, 0x78,  // Timestamp.
+      0x12, 0x34, 0x56, 0x78,  // Ssrc
+      0xbe, 0xde, 0x00, 0x01,  // Extension header
+      0x12, 'm',  'i',  'd',   // 3-byte extension with id=1.
+      'p',  'a',  'y',  'l',  'o', 'a', 'd'};
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register<RtpMid>(1);
+  RtpPacket packet(&extensions);
+  ASSERT_TRUE(packet.Parse(kRawPacket, sizeof(kRawPacket)));
+  EXPECT_THAT(packet.GetRawExtension<RtpMid>(), ElementsAre('m', 'i', 'd'));
+}
+
+TEST(RtpPacketTest, GetRawExtensionWhenAbsent) {
+  constexpr uint8_t kRawPacket[] = {
+      // comment for clang-format to align kRawPacket nicer.
+      0x90, 100,  0x5e, 0x04,  //
+      0x65, 0x43, 0x12, 0x78,  // Timestamp.
+      0x12, 0x34, 0x56, 0x78,  // Ssrc
+      0xbe, 0xde, 0x00, 0x01,  // Extension header
+      0x12, 'm',  'i',  'd',   // 3-byte extension with id=1.
+      'p',  'a',  'y',  'l',  'o', 'a', 'd'};
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register<RtpMid>(2);
+  RtpPacket packet(&extensions);
+  ASSERT_TRUE(packet.Parse(kRawPacket, sizeof(kRawPacket)));
+  EXPECT_THAT(packet.GetRawExtension<RtpMid>(), IsEmpty());
+}
+
 TEST(RtpPacketTest, ParseWithInvalidSizedExtension) {
   RtpPacketToSend::ExtensionManager extensions;
   extensions.Register(kRtpExtensionTransmissionTimeOffset,
@@ -365,6 +557,54 @@
   EXPECT_EQ(kAudioLevel, audio_level);
 }
 
+TEST(RtpPacketTest, ParseSecondPacketWithFewerExtensions) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionTransmissionTimeOffset,
+                      kTransmissionOffsetExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(packet.Parse(kPacketWithTOAndAL, sizeof(kPacketWithTOAndAL)));
+  EXPECT_TRUE(packet.HasExtension<TransmissionOffset>());
+  EXPECT_TRUE(packet.HasExtension<AudioLevel>());
+
+  // Second packet without audio level.
+  EXPECT_TRUE(packet.Parse(kPacketWithTO, sizeof(kPacketWithTO)));
+  EXPECT_TRUE(packet.HasExtension<TransmissionOffset>());
+  EXPECT_FALSE(packet.HasExtension<AudioLevel>());
+}
+
+TEST(RtpPacketTest, ParseWith2ExtensionsInvalidPadding) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionTransmissionTimeOffset,
+                      kTransmissionOffsetExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(packet.Parse(kPacketWithTOAndALInvalidPadding,
+                           sizeof(kPacketWithTOAndALInvalidPadding)));
+  int32_t time_offset;
+  EXPECT_TRUE(packet.GetExtension<TransmissionOffset>(&time_offset));
+  EXPECT_EQ(kTimeOffset, time_offset);
+  bool voice_active;
+  uint8_t audio_level;
+  EXPECT_FALSE(packet.GetExtension<AudioLevel>(&voice_active, &audio_level));
+}
+
+TEST(RtpPacketTest, ParseWith2ExtensionsReservedExtensionId) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionTransmissionTimeOffset,
+                      kTransmissionOffsetExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(packet.Parse(kPacketWithTOAndALReservedExtensionId,
+                           sizeof(kPacketWithTOAndALReservedExtensionId)));
+  int32_t time_offset;
+  EXPECT_TRUE(packet.GetExtension<TransmissionOffset>(&time_offset));
+  EXPECT_EQ(kTimeOffset, time_offset);
+  bool voice_active;
+  uint8_t audio_level;
+  EXPECT_FALSE(packet.GetExtension<AudioLevel>(&voice_active, &audio_level));
+}
+
 TEST(RtpPacketTest, ParseWithAllFeatures) {
   RtpPacketToSend::ExtensionManager extensions;
   extensions.Register(kRtpExtensionTransmissionTimeOffset,
@@ -382,6 +622,46 @@
   EXPECT_TRUE(packet.GetExtension<TransmissionOffset>(&time_offset));
 }
 
+TEST(RtpPacketTest, ParseTwoByteHeaderExtension) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionTransmissionTimeOffset, kTwoByteExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(packet.Parse(kPacketWithTwoByteHeaderExtension,
+                           sizeof(kPacketWithTwoByteHeaderExtension)));
+  int32_t time_offset;
+  EXPECT_TRUE(packet.GetExtension<TransmissionOffset>(&time_offset));
+  EXPECT_EQ(kTimeOffset, time_offset);
+}
+
+TEST(RtpPacketTest, ParseLongTwoByteHeaderExtension) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionMid, kTwoByteExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(packet.Parse(kPacketWithLongTwoByteHeaderExtension,
+                           sizeof(kPacketWithLongTwoByteHeaderExtension)));
+  std::string long_rtp_mid;
+  EXPECT_TRUE(packet.GetExtension<RtpMid>(&long_rtp_mid));
+  EXPECT_EQ(kLongMid, long_rtp_mid);
+}
+
+TEST(RtpPacketTest, ParseTwoByteHeaderExtensionWithPadding) {
+  RtpPacketToSend::ExtensionManager extensions;
+  extensions.Register(kRtpExtensionTransmissionTimeOffset, kTwoByteExtensionId);
+  extensions.Register(kRtpExtensionAudioLevel, kAudioLevelExtensionId);
+  RtpPacketReceived packet(&extensions);
+  EXPECT_TRUE(
+      packet.Parse(kPacketWithTwoByteHeaderExtensionWithPadding,
+                   sizeof(kPacketWithTwoByteHeaderExtensionWithPadding)));
+  int32_t time_offset;
+  EXPECT_TRUE(packet.GetExtension<TransmissionOffset>(&time_offset));
+  EXPECT_EQ(kTimeOffset, time_offset);
+  bool voice_active;
+  uint8_t audio_level;
+  EXPECT_TRUE(packet.GetExtension<AudioLevel>(&voice_active, &audio_level));
+  EXPECT_EQ(kVoiceActive, voice_active);
+  EXPECT_EQ(kAudioLevel, audio_level);
+}
+
 TEST(RtpPacketTest, ParseWithExtensionDelayed) {
   RtpPacketReceived packet;
   EXPECT_TRUE(packet.Parse(kPacketWithTO, sizeof(kPacketWithTO)));
diff --git a/modules/rtp_rtcp/source/rtp_payload_registry.cc b/modules/rtp_rtcp/source/rtp_payload_registry.cc
deleted file mode 100644
index 4bbb03a..0000000
--- a/modules/rtp_rtcp/source/rtp_payload_registry.cc
+++ /dev/null
@@ -1,218 +0,0 @@
-/*
- *  Copyright (c) 2013 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 "modules/rtp_rtcp/include/rtp_payload_registry.h"
-
-#include <algorithm>
-
-#include "modules/audio_coding/codecs/audio_format_conversion.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/stringutils.h"
-
-namespace webrtc {
-
-namespace {
-
-bool PayloadIsCompatible(const RtpUtility::Payload& payload,
-                         const SdpAudioFormat& audio_format) {
-  return payload.typeSpecific.is_audio() &&
-         audio_format.Matches(payload.typeSpecific.audio_payload().format);
-}
-
-bool PayloadIsCompatible(const RtpUtility::Payload& payload,
-                         const VideoCodec& video_codec) {
-  if (!payload.typeSpecific.is_video() ||
-      _stricmp(payload.name, CodecTypeToPayloadString(video_codec.codecType)) !=
-          0)
-    return false;
-  // For H264, profiles must match as well.
-  if (video_codec.codecType == kVideoCodecH264) {
-    return video_codec.H264().profile ==
-           payload.typeSpecific.video_payload().h264_profile;
-  }
-  return true;
-}
-
-RtpUtility::Payload CreatePayloadType(const SdpAudioFormat& audio_format) {
-  RTC_DCHECK_GE(audio_format.clockrate_hz, 1000);
-  return {audio_format.name.c_str(),
-          PayloadUnion(AudioPayload{audio_format, 0})};
-}
-
-RtpUtility::Payload CreatePayloadType(const VideoCodec& video_codec) {
-  VideoPayload p;
-  p.videoCodecType = video_codec.codecType;
-  if (video_codec.codecType == kVideoCodecH264)
-    p.h264_profile = video_codec.H264().profile;
-  return {CodecTypeToPayloadString(video_codec.codecType), PayloadUnion(p)};
-}
-
-bool IsPayloadTypeValid(int8_t payload_type) {
-  assert(payload_type >= 0);
-
-  // Sanity check.
-  switch (payload_type) {
-    // Reserved payload types to avoid RTCP conflicts when marker bit is set.
-    case 64:  //  192 Full INTRA-frame request.
-    case 72:  //  200 Sender report.
-    case 73:  //  201 Receiver report.
-    case 74:  //  202 Source description.
-    case 75:  //  203 Goodbye.
-    case 76:  //  204 Application-defined.
-    case 77:  //  205 Transport layer FB message.
-    case 78:  //  206 Payload-specific FB message.
-    case 79:  //  207 Extended report.
-      RTC_LOG(LS_ERROR) << "Can't register invalid receiver payload type: "
-                        << payload_type;
-      return false;
-    default:
-      return true;
-  }
-}
-
-}  // namespace
-
-RTPPayloadRegistry::RTPPayloadRegistry() = default;
-
-RTPPayloadRegistry::~RTPPayloadRegistry() = default;
-
-void RTPPayloadRegistry::SetAudioReceivePayloads(
-    std::map<int, SdpAudioFormat> codecs) {
-  rtc::CritScope cs(&crit_sect_);
-
-#if RTC_DCHECK_IS_ON
-  RTC_DCHECK(!used_for_video_);
-  used_for_audio_ = true;
-#endif
-
-  payload_type_map_.clear();
-  for (const auto& kv : codecs) {
-    const int& rtp_payload_type = kv.first;
-    const SdpAudioFormat& audio_format = kv.second;
-    RTC_DCHECK(IsPayloadTypeValid(rtp_payload_type));
-    payload_type_map_.emplace(rtp_payload_type,
-                              CreatePayloadType(audio_format));
-  }
-}
-
-int32_t RTPPayloadRegistry::RegisterReceivePayload(
-    int payload_type,
-    const SdpAudioFormat& audio_format,
-    bool* created_new_payload) {
-  rtc::CritScope cs(&crit_sect_);
-
-#if RTC_DCHECK_IS_ON
-  RTC_DCHECK(!used_for_video_);
-  used_for_audio_ = true;
-#endif
-
-  *created_new_payload = false;
-  if (!IsPayloadTypeValid(payload_type))
-    return -1;
-
-  const auto it = payload_type_map_.find(payload_type);
-  if (it != payload_type_map_.end()) {
-    // We already use this payload type. Check if it's the same as we already
-    // have. If same, ignore sending an error.
-    if (PayloadIsCompatible(it->second, audio_format)) {
-      it->second.typeSpecific.audio_payload().rate = 0;
-      return 0;
-    }
-    RTC_LOG(LS_ERROR) << "Payload type already registered: " << payload_type;
-    return -1;
-  }
-
-  // Audio codecs must be unique.
-  DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(audio_format);
-
-  const auto insert_status =
-      payload_type_map_.emplace(payload_type, CreatePayloadType(audio_format));
-  RTC_DCHECK(insert_status.second);  // Insertion succeeded.
-  *created_new_payload = true;
-
-  // Successful set of payload type.
-  return 0;
-}
-
-int32_t RTPPayloadRegistry::RegisterReceivePayload(
-    const VideoCodec& video_codec) {
-  rtc::CritScope cs(&crit_sect_);
-
-#if RTC_DCHECK_IS_ON
-  RTC_DCHECK(!used_for_audio_);
-  used_for_video_ = true;
-#endif
-
-  if (!IsPayloadTypeValid(video_codec.plType))
-    return -1;
-
-  auto it = payload_type_map_.find(video_codec.plType);
-  if (it != payload_type_map_.end()) {
-    // We already use this payload type. Check if it's the same as we already
-    // have. If same, ignore sending an error.
-    if (PayloadIsCompatible(it->second, video_codec))
-      return 0;
-    RTC_LOG(LS_ERROR) << "Payload type already registered: "
-                      << static_cast<int>(video_codec.plType);
-    return -1;
-  }
-
-  const auto insert_status = payload_type_map_.emplace(
-      video_codec.plType, CreatePayloadType(video_codec));
-  RTC_DCHECK(insert_status.second);  // Insertion succeeded.
-
-  // Successful set of payload type.
-  return 0;
-}
-
-int32_t RTPPayloadRegistry::DeRegisterReceivePayload(
-    const int8_t payload_type) {
-  rtc::CritScope cs(&crit_sect_);
-  payload_type_map_.erase(payload_type);
-  return 0;
-}
-
-// There can't be several codecs with the same rate, frequency and channels
-// for audio codecs, but there can for video.
-// Always called from within a critical section.
-void RTPPayloadRegistry::DeregisterAudioCodecOrRedTypeRegardlessOfPayloadType(
-    const SdpAudioFormat& audio_format) {
-  for (auto iterator = payload_type_map_.begin();
-       iterator != payload_type_map_.end(); ++iterator) {
-    if (PayloadIsCompatible(iterator->second, audio_format)) {
-      // Remove old setting.
-      payload_type_map_.erase(iterator);
-      break;
-    }
-  }
-}
-
-int RTPPayloadRegistry::GetPayloadTypeFrequency(uint8_t payload_type) const {
-  const auto payload = PayloadTypeToPayload(payload_type);
-  if (!payload) {
-    return -1;
-  }
-  rtc::CritScope cs(&crit_sect_);
-  return payload->typeSpecific.is_audio()
-             ? payload->typeSpecific.audio_payload().format.clockrate_hz
-             : kVideoPayloadTypeFrequency;
-}
-
-absl::optional<RtpUtility::Payload> RTPPayloadRegistry::PayloadTypeToPayload(
-    uint8_t payload_type) const {
-  rtc::CritScope cs(&crit_sect_);
-  const auto it = payload_type_map_.find(payload_type);
-  return it == payload_type_map_.end()
-             ? absl::nullopt
-             : absl::optional<RtpUtility::Payload>(it->second);
-}
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_payload_registry_unittest.cc b/modules/rtp_rtcp/source/rtp_payload_registry_unittest.cc
deleted file mode 100644
index 9ae6aef..0000000
--- a/modules/rtp_rtcp/source/rtp_payload_registry_unittest.cc
+++ /dev/null
@@ -1,186 +0,0 @@
-/*
- *  Copyright (c) 2013 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 <memory>
-
-#include "common_types.h"  // NOLINT(build/include)
-#include "modules/rtp_rtcp/include/rtp_header_parser.h"
-#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "test/gmock.h"
-#include "test/gtest.h"
-
-namespace webrtc {
-
-using ::testing::Eq;
-using ::testing::Return;
-using ::testing::StrEq;
-using ::testing::_;
-
-TEST(RtpPayloadRegistryTest,
-     RegistersAndRemembersVideoPayloadsUntilDeregistered) {
-  RTPPayloadRegistry rtp_payload_registry;
-  const uint8_t payload_type = 97;
-  VideoCodec video_codec;
-  video_codec.codecType = kVideoCodecVP8;
-  video_codec.plType = payload_type;
-
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(video_codec));
-
-  const auto retrieved_payload =
-      rtp_payload_registry.PayloadTypeToPayload(payload_type);
-  EXPECT_TRUE(retrieved_payload);
-
-  // We should get back the corresponding payload that we registered.
-  EXPECT_STREQ("VP8", retrieved_payload->name);
-  EXPECT_TRUE(retrieved_payload->typeSpecific.is_video());
-  EXPECT_EQ(kVideoCodecVP8,
-            retrieved_payload->typeSpecific.video_payload().videoCodecType);
-
-  // Now forget about it and verify it's gone.
-  EXPECT_EQ(0, rtp_payload_registry.DeRegisterReceivePayload(payload_type));
-  EXPECT_FALSE(rtp_payload_registry.PayloadTypeToPayload(payload_type));
-}
-
-TEST(RtpPayloadRegistryTest,
-     RegistersAndRemembersAudioPayloadsUntilDeregistered) {
-  RTPPayloadRegistry rtp_payload_registry;
-  constexpr int payload_type = 97;
-  const SdpAudioFormat audio_format("name", 44000, 1);
-  bool new_payload_created = false;
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type, audio_format, &new_payload_created));
-
-  EXPECT_TRUE(new_payload_created) << "A new payload WAS created.";
-
-  const auto retrieved_payload =
-      rtp_payload_registry.PayloadTypeToPayload(payload_type);
-  EXPECT_TRUE(retrieved_payload);
-
-  // We should get back the corresponding payload that we registered.
-  EXPECT_STREQ("name", retrieved_payload->name);
-  EXPECT_TRUE(retrieved_payload->typeSpecific.is_audio());
-  EXPECT_EQ(audio_format,
-            retrieved_payload->typeSpecific.audio_payload().format);
-
-  // Now forget about it and verify it's gone.
-  EXPECT_EQ(0, rtp_payload_registry.DeRegisterReceivePayload(payload_type));
-  EXPECT_FALSE(rtp_payload_registry.PayloadTypeToPayload(payload_type));
-}
-
-TEST(RtpPayloadRegistryTest,
-     DoesNotAcceptSamePayloadTypeTwiceExceptIfPayloadIsCompatible) {
-  constexpr int payload_type = 97;
-  RTPPayloadRegistry rtp_payload_registry;
-
-  bool ignored = false;
-  const SdpAudioFormat audio_format("name", 44000, 1);
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type, audio_format, &ignored));
-
-  const SdpAudioFormat audio_format_2("name", 44001, 1);  // Not compatible.
-  EXPECT_EQ(-1, rtp_payload_registry.RegisterReceivePayload(
-                    payload_type, audio_format_2, &ignored))
-      << "Adding incompatible codec with same payload type = bad.";
-
-  // Change payload type.
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type - 1, audio_format_2, &ignored))
-      << "With a different payload type is fine though.";
-
-  // Ensure both payloads are preserved.
-  const auto retrieved_payload1 =
-      rtp_payload_registry.PayloadTypeToPayload(payload_type);
-  EXPECT_TRUE(retrieved_payload1);
-  EXPECT_STREQ("name", retrieved_payload1->name);
-  EXPECT_TRUE(retrieved_payload1->typeSpecific.is_audio());
-  EXPECT_EQ(audio_format,
-            retrieved_payload1->typeSpecific.audio_payload().format);
-
-  const auto retrieved_payload2 =
-      rtp_payload_registry.PayloadTypeToPayload(payload_type - 1);
-  EXPECT_TRUE(retrieved_payload2);
-  EXPECT_STREQ("name", retrieved_payload2->name);
-  EXPECT_TRUE(retrieved_payload2->typeSpecific.is_audio());
-  EXPECT_EQ(audio_format_2,
-            retrieved_payload2->typeSpecific.audio_payload().format);
-
-  // Ok, update the rate for one of the codecs. If either the incoming rate or
-  // the stored rate is zero it's not really an error to register the same
-  // codec twice, and in that case roughly the following happens.
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type, audio_format, &ignored));
-}
-
-TEST(RtpPayloadRegistryTest,
-     RemovesCompatibleCodecsOnRegistryIfCodecsMustBeUnique) {
-  constexpr int payload_type = 97;
-  RTPPayloadRegistry rtp_payload_registry;
-
-  bool ignored = false;
-  const SdpAudioFormat audio_format("name", 44000, 1);
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type, audio_format, &ignored));
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type - 1, audio_format, &ignored));
-
-  EXPECT_FALSE(rtp_payload_registry.PayloadTypeToPayload(payload_type))
-      << "The first payload should be "
-         "deregistered because the only thing that differs is payload type.";
-  EXPECT_TRUE(rtp_payload_registry.PayloadTypeToPayload(payload_type - 1))
-      << "The second payload should still be registered though.";
-
-  // Now ensure non-compatible codecs aren't removed. Make |audio_format_2|
-  // incompatible by changing the frequency.
-  const SdpAudioFormat audio_format_2("name", 44001, 1);
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type + 1, audio_format_2, &ignored));
-
-  EXPECT_TRUE(rtp_payload_registry.PayloadTypeToPayload(payload_type - 1))
-      << "Not compatible; both payloads should be kept.";
-  EXPECT_TRUE(rtp_payload_registry.PayloadTypeToPayload(payload_type + 1))
-      << "Not compatible; both payloads should be kept.";
-}
-
-class ParameterizedRtpPayloadRegistryTest
-    : public ::testing::TestWithParam<int> {};
-
-TEST_P(ParameterizedRtpPayloadRegistryTest,
-       FailsToRegisterKnownPayloadsWeAreNotInterestedIn) {
-  RTPPayloadRegistry rtp_payload_registry;
-
-  bool ignored;
-  const int payload_type = GetParam();
-  const SdpAudioFormat audio_format("whatever", 1900, 1);
-  EXPECT_EQ(-1, rtp_payload_registry.RegisterReceivePayload(
-                    payload_type, audio_format, &ignored));
-}
-
-INSTANTIATE_TEST_CASE_P(TestKnownBadPayloadTypes,
-                        ParameterizedRtpPayloadRegistryTest,
-                        testing::Values(64, 72, 73, 74, 75, 76, 77, 78, 79));
-
-class RtpPayloadRegistryGenericTest : public ::testing::TestWithParam<int> {};
-
-TEST_P(RtpPayloadRegistryGenericTest, RegisterGenericReceivePayloadType) {
-  RTPPayloadRegistry rtp_payload_registry;
-
-  bool ignored;
-  const int payload_type = GetParam();
-  const SdpAudioFormat audio_format("generic-codec", 1900, 1);  // Dummy values.
-  EXPECT_EQ(0, rtp_payload_registry.RegisterReceivePayload(
-                   payload_type, audio_format, &ignored));
-}
-
-INSTANTIATE_TEST_CASE_P(TestDynamicRange,
-                        RtpPayloadRegistryGenericTest,
-                        testing::Range(96, 127 + 1));
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_audio.cc b/modules/rtp_rtcp/source/rtp_receiver_audio.cc
deleted file mode 100644
index 030c79f..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_audio.cc
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "modules/rtp_rtcp/source/rtp_receiver_audio.h"
-
-#include <assert.h>  // assert
-#include <math.h>    // pow()
-#include <string.h>  // memcpy()
-
-#include "common_types.h"  // NOLINT(build/include)
-#include "rtc_base/logging.h"
-#include "rtc_base/trace_event.h"
-
-namespace webrtc {
-RTPReceiverStrategy* RTPReceiverStrategy::CreateAudioStrategy(
-    RtpData* data_callback) {
-  return new RTPReceiverAudio(data_callback);
-}
-
-RTPReceiverAudio::RTPReceiverAudio(RtpData* data_callback)
-    : RTPReceiverStrategy(data_callback) {}
-
-RTPReceiverAudio::~RTPReceiverAudio() = default;
-
-// -   Sample based or frame based codecs based on RFC 3551
-// -
-// -   NOTE! There is one error in the RFC, stating G.722 uses 8 bits/samples.
-// -   The correct rate is 4 bits/sample.
-// -
-// -   name of                              sampling              default
-// -   encoding  sample/frame  bits/sample      rate  ms/frame  ms/packet
-// -
-// -   Sample based audio codecs
-// -   DVI4      sample        4                var.                   20
-// -   G722      sample        4              16,000                   20
-// -   G726-40   sample        5               8,000                   20
-// -   G726-32   sample        4               8,000                   20
-// -   G726-24   sample        3               8,000                   20
-// -   G726-16   sample        2               8,000                   20
-// -   L8        sample        8                var.                   20
-// -   L16       sample        16               var.                   20
-// -   PCMA      sample        8                var.                   20
-// -   PCMU      sample        8                var.                   20
-// -
-// -   Frame based audio codecs
-// -   G723      frame         N/A             8,000        30         30
-// -   G728      frame         N/A             8,000       2.5         20
-// -   G729      frame         N/A             8,000        10         20
-// -   G729D     frame         N/A             8,000        10         20
-// -   G729E     frame         N/A             8,000        10         20
-// -   GSM       frame         N/A             8,000        20         20
-// -   GSM-EFR   frame         N/A             8,000        20         20
-// -   LPC       frame         N/A             8,000        20         20
-// -   MPA       frame         N/A              var.      var.
-// -
-// -   G7221     frame         N/A
-
-int32_t RTPReceiverAudio::ParseRtpPacket(WebRtcRTPHeader* rtp_header,
-                                         const PayloadUnion& specific_payload,
-                                         const uint8_t* payload,
-                                         size_t payload_length,
-                                         int64_t timestamp_ms) {
-  if (first_packet_received_()) {
-    RTC_LOG(LS_INFO) << "Received first audio RTP packet";
-  }
-
-  return ParseAudioCodecSpecific(rtp_header, payload, payload_length,
-                                 specific_payload.audio_payload());
-}
-
-// We are not allowed to have any critsects when calling data_callback.
-int32_t RTPReceiverAudio::ParseAudioCodecSpecific(
-    WebRtcRTPHeader* rtp_header,
-    const uint8_t* payload_data,
-    size_t payload_length,
-    const AudioPayload& audio_specific) {
-  RTC_DCHECK_GE(payload_length, rtp_header->header.paddingLength);
-  const size_t payload_data_length =
-      payload_length - rtp_header->header.paddingLength;
-  if (payload_data_length == 0) {
-    rtp_header->frameType = kEmptyFrame;
-    return data_callback_->OnReceivedPayloadData(nullptr, 0, rtp_header);
-  }
-
-  return data_callback_->OnReceivedPayloadData(payload_data,
-                                               payload_data_length, rtp_header);
-}
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_audio.h b/modules/rtp_rtcp/source/rtp_receiver_audio.h
deleted file mode 100644
index 5d97a1f..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_audio.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- *  Copyright (c) 2012 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 MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
-#define MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
-
-#include <set>
-
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "rtc_base/onetimeevent.h"
-
-namespace webrtc {
-
-// Handles audio RTP packets. This class is thread-safe.
-class RTPReceiverAudio : public RTPReceiverStrategy {
- public:
-  explicit RTPReceiverAudio(RtpData* data_callback);
-  ~RTPReceiverAudio() override;
-
-  int32_t ParseRtpPacket(WebRtcRTPHeader* rtp_header,
-                         const PayloadUnion& specific_payload,
-                         const uint8_t* packet,
-                         size_t payload_length,
-                         int64_t timestamp_ms) override;
-
- private:
-  int32_t ParseAudioCodecSpecific(WebRtcRTPHeader* rtp_header,
-                                  const uint8_t* payload_data,
-                                  size_t payload_length,
-                                  const AudioPayload& audio_specific);
-
-  ThreadUnsafeOneTimeEvent first_packet_received_;
-};
-}  // namespace webrtc
-
-#endif  // MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_AUDIO_H_
diff --git a/modules/rtp_rtcp/source/rtp_receiver_impl.cc b/modules/rtp_rtcp/source/rtp_receiver_impl.cc
deleted file mode 100644
index ea63e5e..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_impl.cc
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "modules/rtp_rtcp/source/rtp_receiver_impl.h"
-
-#include <assert.h>
-#include <math.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <set>
-#include <vector>
-
-#include "common_types.h"  // NOLINT(build/include)
-#include "modules/audio_coding/codecs/audio_format_conversion.h"
-#include "modules/include/module_common_types.h"
-#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-#include "rtc_base/logging.h"
-
-namespace webrtc {
-
-namespace {
-bool InOrderPacket(absl::optional<uint16_t> latest_sequence_number,
-                   uint16_t current_sequence_number) {
-  if (!latest_sequence_number)
-    return true;
-
-  // We need to distinguish between a late or retransmitted packet,
-  // and a sequence number discontinuity.
-  if (IsNewerSequenceNumber(current_sequence_number, *latest_sequence_number)) {
-    return true;
-  } else {
-    // If we have a restart of the remote side this packet is still in order.
-    return !IsNewerSequenceNumber(
-        current_sequence_number,
-        *latest_sequence_number - kDefaultMaxReorderingThreshold);
-  }
-}
-
-}  // namespace
-
-using RtpUtility::Payload;
-
-// Only return the sources in the last 10 seconds.
-const int64_t kGetSourcesTimeoutMs = 10000;
-
-RtpReceiver* RtpReceiver::CreateVideoReceiver(
-    Clock* clock,
-    RtpData* incoming_payload_callback,
-    RTPPayloadRegistry* rtp_payload_registry) {
-  RTC_DCHECK(incoming_payload_callback != nullptr);
-  return new RtpReceiverImpl(
-      clock, rtp_payload_registry,
-      RTPReceiverStrategy::CreateVideoStrategy(incoming_payload_callback));
-}
-
-RtpReceiver* RtpReceiver::CreateAudioReceiver(
-    Clock* clock,
-    RtpData* incoming_payload_callback,
-    RTPPayloadRegistry* rtp_payload_registry) {
-  RTC_DCHECK(incoming_payload_callback != nullptr);
-  return new RtpReceiverImpl(
-      clock, rtp_payload_registry,
-      RTPReceiverStrategy::CreateAudioStrategy(incoming_payload_callback));
-}
-
-int32_t RtpReceiver::RegisterReceivePayload(const CodecInst& audio_codec) {
-  return RegisterReceivePayload(audio_codec.pltype,
-                                CodecInstToSdp(audio_codec));
-}
-
-RtpReceiverImpl::RtpReceiverImpl(Clock* clock,
-                                 RTPPayloadRegistry* rtp_payload_registry,
-                                 RTPReceiverStrategy* rtp_media_receiver)
-    : clock_(clock),
-      rtp_payload_registry_(rtp_payload_registry),
-      rtp_media_receiver_(rtp_media_receiver),
-      ssrc_(0),
-      last_received_timestamp_(0),
-      last_received_frame_time_ms_(-1) {}
-
-RtpReceiverImpl::~RtpReceiverImpl() {}
-
-int32_t RtpReceiverImpl::RegisterReceivePayload(
-    int payload_type,
-    const SdpAudioFormat& audio_format) {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-
-  // TODO(phoglund): Try to streamline handling of the RED codec and some other
-  // cases which makes it necessary to keep track of whether we created a
-  // payload or not.
-  bool created_new_payload = false;
-  int32_t result = rtp_payload_registry_->RegisterReceivePayload(
-      payload_type, audio_format, &created_new_payload);
-  return result;
-}
-
-int32_t RtpReceiverImpl::RegisterReceivePayload(const VideoCodec& video_codec) {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  return rtp_payload_registry_->RegisterReceivePayload(video_codec);
-}
-
-int32_t RtpReceiverImpl::DeRegisterReceivePayload(const int8_t payload_type) {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  return rtp_payload_registry_->DeRegisterReceivePayload(payload_type);
-}
-
-uint32_t RtpReceiverImpl::SSRC() const {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  return ssrc_;
-}
-
-bool RtpReceiverImpl::IncomingRtpPacket(const RTPHeader& rtp_header,
-                                        const uint8_t* payload,
-                                        size_t payload_length,
-                                        PayloadUnion payload_specific) {
-  // Trigger our callbacks.
-  CheckSSRCChanged(rtp_header);
-
-  if (payload_length == 0) {
-    // OK, keep-alive packet.
-    return true;
-  }
-  int64_t now_ms = clock_->TimeInMilliseconds();
-
-  {
-    rtc::CritScope lock(&critical_section_rtp_receiver_);
-
-    csrcs_.Update(
-        now_ms, rtc::MakeArrayView(rtp_header.arrOfCSRCs, rtp_header.numCSRCs));
-  }
-
-  WebRtcRTPHeader webrtc_rtp_header{};
-  webrtc_rtp_header.header = rtp_header;
-
-  auto audio_level =
-      rtp_header.extension.hasAudioLevel
-          ? absl::optional<uint8_t>(rtp_header.extension.audioLevel)
-          : absl::nullopt;
-  UpdateSources(audio_level);
-
-  int32_t ret_val = rtp_media_receiver_->ParseRtpPacket(
-      &webrtc_rtp_header, payload_specific, payload, payload_length, now_ms);
-
-  if (ret_val < 0) {
-    return false;
-  }
-
-  {
-    rtc::CritScope lock(&critical_section_rtp_receiver_);
-
-    // TODO(nisse): Do not rely on InOrderPacket for recovered packets, when
-    // packet is passed as RtpPacketReceived and that information is available.
-    // We should ideally never record timestamps for retransmitted or recovered
-    // packets.
-    if (InOrderPacket(last_received_sequence_number_,
-                      rtp_header.sequenceNumber)) {
-      last_received_sequence_number_.emplace(rtp_header.sequenceNumber);
-      last_received_timestamp_ = rtp_header.timestamp;
-      last_received_frame_time_ms_ = clock_->TimeInMilliseconds();
-    }
-  }
-
-  return true;
-}
-
-std::vector<RtpSource> RtpReceiverImpl::GetSources() const {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-
-  int64_t now_ms = clock_->TimeInMilliseconds();
-  RTC_DCHECK(std::is_sorted(ssrc_sources_.begin(), ssrc_sources_.end(),
-                            [](const RtpSource& lhs, const RtpSource& rhs) {
-                              return lhs.timestamp_ms() < rhs.timestamp_ms();
-                            }));
-  std::vector<RtpSource> sources = csrcs_.GetSources(now_ms);
-
-  std::set<uint32_t> selected_ssrcs;
-  for (auto rit = ssrc_sources_.rbegin(); rit != ssrc_sources_.rend(); ++rit) {
-    if ((now_ms - rit->timestamp_ms()) > kGetSourcesTimeoutMs) {
-      break;
-    }
-    if (selected_ssrcs.insert(rit->source_id()).second) {
-      sources.push_back(*rit);
-    }
-  }
-  return sources;
-}
-
-bool RtpReceiverImpl::GetLatestTimestamps(uint32_t* timestamp,
-                                          int64_t* receive_time_ms) const {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  if (!last_received_sequence_number_)
-    return false;
-
-  *timestamp = last_received_timestamp_;
-  *receive_time_ms = last_received_frame_time_ms_;
-
-  return true;
-}
-
-// TODO(nisse): Delete.
-// Implementation note: must not hold critsect when called.
-void RtpReceiverImpl::CheckSSRCChanged(const RTPHeader& rtp_header) {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  ssrc_ = rtp_header.ssrc;
-}
-
-void RtpReceiverImpl::UpdateSources(
-    const absl::optional<uint8_t>& ssrc_audio_level) {
-  rtc::CritScope lock(&critical_section_rtp_receiver_);
-  int64_t now_ms = clock_->TimeInMilliseconds();
-
-  // If this is the first packet or the SSRC is changed, insert a new
-  // contributing source that uses the SSRC.
-  if (ssrc_sources_.empty() || ssrc_sources_.rbegin()->source_id() != ssrc_) {
-    ssrc_sources_.emplace_back(now_ms, ssrc_, RtpSourceType::SSRC);
-  } else {
-    ssrc_sources_.rbegin()->update_timestamp_ms(now_ms);
-  }
-
-  ssrc_sources_.back().set_audio_level(ssrc_audio_level);
-
-  RemoveOutdatedSources(now_ms);
-}
-
-void RtpReceiverImpl::RemoveOutdatedSources(int64_t now_ms) {
-  std::vector<RtpSource>::iterator vec_it;
-  for (vec_it = ssrc_sources_.begin(); vec_it != ssrc_sources_.end();
-       ++vec_it) {
-    if ((now_ms - vec_it->timestamp_ms()) <= kGetSourcesTimeoutMs) {
-      break;
-    }
-  }
-  ssrc_sources_.erase(ssrc_sources_.begin(), vec_it);
-}
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_impl.h b/modules/rtp_rtcp/source/rtp_receiver_impl.h
deleted file mode 100644
index 66265f5..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_impl.h
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- *  Copyright (c) 2013 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 MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_IMPL_H_
-#define MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_IMPL_H_
-
-#include <memory>
-#include <unordered_map>
-#include <vector>
-
-#include "absl/types/optional.h"
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/source/contributing_sources.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-#include "rtc_base/criticalsection.h"
-
-namespace webrtc {
-
-class RtpReceiverImpl : public RtpReceiver {
- public:
-  // Callbacks passed in here may not be NULL (use Null Object callbacks if you
-  // want callbacks to do nothing). This class takes ownership of the media
-  // receiver but nothing else.
-  RtpReceiverImpl(Clock* clock,
-                  RTPPayloadRegistry* rtp_payload_registry,
-                  RTPReceiverStrategy* rtp_media_receiver);
-
-  ~RtpReceiverImpl() override;
-
-  int32_t RegisterReceivePayload(int payload_type,
-                                 const SdpAudioFormat& audio_format) override;
-  int32_t RegisterReceivePayload(const VideoCodec& video_codec) override;
-
-  int32_t DeRegisterReceivePayload(const int8_t payload_type) override;
-
-  bool IncomingRtpPacket(const RTPHeader& rtp_header,
-                         const uint8_t* payload,
-                         size_t payload_length,
-                         PayloadUnion payload_specific) override;
-
-  bool GetLatestTimestamps(uint32_t* timestamp,
-                           int64_t* receive_time_ms) const override;
-
-  uint32_t SSRC() const override;
-
-  std::vector<RtpSource> GetSources() const override;
-
- private:
-  void CheckSSRCChanged(const RTPHeader& rtp_header);
-
-  void UpdateSources(const absl::optional<uint8_t>& ssrc_audio_level);
-  void RemoveOutdatedSources(int64_t now_ms);
-
-  Clock* clock_;
-  rtc::CriticalSection critical_section_rtp_receiver_;
-
-  RTPPayloadRegistry* const rtp_payload_registry_
-      RTC_PT_GUARDED_BY(critical_section_rtp_receiver_);
-  const std::unique_ptr<RTPReceiverStrategy> rtp_media_receiver_;
-
-  // SSRCs.
-  uint32_t ssrc_ RTC_GUARDED_BY(critical_section_rtp_receiver_);
-
-  ContributingSources csrcs_ RTC_GUARDED_BY(critical_section_rtp_receiver_);
-
-  // Sequence number and timestamps for the latest in-order packet.
-  absl::optional<uint16_t> last_received_sequence_number_
-      RTC_GUARDED_BY(critical_section_rtp_receiver_);
-  uint32_t last_received_timestamp_
-      RTC_GUARDED_BY(critical_section_rtp_receiver_);
-  int64_t last_received_frame_time_ms_
-      RTC_GUARDED_BY(critical_section_rtp_receiver_);
-
-  // The RtpSource objects are sorted chronologically.
-  std::vector<RtpSource> ssrc_sources_;
-};
-}  // namespace webrtc
-#endif  // MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_IMPL_H_
diff --git a/modules/rtp_rtcp/source/rtp_receiver_strategy.cc b/modules/rtp_rtcp/source/rtp_receiver_strategy.cc
deleted file mode 100644
index 647ecea..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_strategy.cc
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-
-#include <stdlib.h>
-
-namespace webrtc {
-
-RTPReceiverStrategy::RTPReceiverStrategy(RtpData* data_callback)
-    : data_callback_(data_callback) {}
-
-RTPReceiverStrategy::~RTPReceiverStrategy() = default;
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_strategy.h b/modules/rtp_rtcp/source/rtp_receiver_strategy.h
deleted file mode 100644
index 987af7c..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_strategy.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- *  Copyright (c) 2012 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 MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_STRATEGY_H_
-#define MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_STRATEGY_H_
-
-#include "modules/rtp_rtcp/include/rtp_rtcp.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "rtc_base/criticalsection.h"
-
-namespace webrtc {
-
-struct CodecInst;
-
-// This strategy deals with media-specific RTP packet processing.
-// This class is not thread-safe and must be protected by its caller.
-class RTPReceiverStrategy {
- public:
-  static RTPReceiverStrategy* CreateVideoStrategy(RtpData* data_callback);
-  static RTPReceiverStrategy* CreateAudioStrategy(RtpData* data_callback);
-
-  virtual ~RTPReceiverStrategy();
-
-  // Parses the RTP packet and calls the data callback with the payload data.
-  // Implementations are encouraged to use the provided packet buffer and RTP
-  // header as arguments to the callback; implementations are also allowed to
-  // make changes in the data as necessary. The specific_payload argument
-  // provides audio or video-specific data.
-  virtual int32_t ParseRtpPacket(WebRtcRTPHeader* rtp_header,
-                                 const PayloadUnion& specific_payload,
-                                 const uint8_t* payload,
-                                 size_t payload_length,
-                                 int64_t timestamp_ms) = 0;
-
- protected:
-  // The data callback is where we should send received payload data.
-  // See ParseRtpPacket. This class does not claim ownership of the callback.
-  // Implementations must NOT hold any critical sections while calling the
-  // callback.
-  //
-  // Note: Implementations may call the callback for other reasons than calls
-  // to ParseRtpPacket, for instance if the implementation somehow recovers a
-  // packet.
-  explicit RTPReceiverStrategy(RtpData* data_callback);
-
-  rtc::CriticalSection crit_sect_;
-  RtpData* data_callback_;
-};
-}  // namespace webrtc
-
-#endif  // MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_STRATEGY_H_
diff --git a/modules/rtp_rtcp/source/rtp_receiver_unittest.cc b/modules/rtp_rtcp/source/rtp_receiver_unittest.cc
deleted file mode 100644
index 1400288..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_unittest.cc
+++ /dev/null
@@ -1,461 +0,0 @@
-/*
- *  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 <memory>
-
-#include "common_types.h"  // NOLINT(build/include)
-#include "modules/rtp_rtcp/include/rtp_header_parser.h"
-#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
-#include "modules/rtp_rtcp/include/rtp_receiver.h"
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
-#include "test/gmock.h"
-#include "test/gtest.h"
-
-namespace webrtc {
-namespace {
-
-using ::testing::NiceMock;
-using ::testing::UnorderedElementsAre;
-
-const uint32_t kTestRate = 64000u;
-const uint8_t kTestPayload[] = {'t', 'e', 's', 't'};
-const uint8_t kPcmuPayloadType = 96;
-const int64_t kGetSourcesTimeoutMs = 10000;
-const uint32_t kSsrc1 = 123;
-const uint32_t kSsrc2 = 124;
-const uint32_t kCsrc1 = 111;
-const uint32_t kCsrc2 = 222;
-
-static uint32_t rtp_timestamp(int64_t time_ms) {
-  return static_cast<uint32_t>(time_ms * kTestRate / 1000);
-}
-
-}  // namespace
-
-class RtpReceiverTest : public ::testing::Test {
- protected:
-  RtpReceiverTest()
-      : fake_clock_(123456),
-        rtp_receiver_(
-            RtpReceiver::CreateAudioReceiver(&fake_clock_,
-                                             &mock_rtp_data_,
-                                             &rtp_payload_registry_)) {
-    rtp_receiver_->RegisterReceivePayload(kPcmuPayloadType,
-                                          SdpAudioFormat("PCMU", 8000, 1));
-  }
-  ~RtpReceiverTest() {}
-
-  bool FindSourceByIdAndType(const std::vector<RtpSource>& sources,
-                             uint32_t source_id,
-                             RtpSourceType type,
-                             RtpSource* source) {
-    for (size_t i = 0; i < sources.size(); ++i) {
-      if (sources[i].source_id() == source_id &&
-          sources[i].source_type() == type) {
-        (*source) = sources[i];
-        return true;
-      }
-    }
-    return false;
-  }
-
-  SimulatedClock fake_clock_;
-  NiceMock<MockRtpData> mock_rtp_data_;
-  RTPPayloadRegistry rtp_payload_registry_;
-  std::unique_ptr<RtpReceiver> rtp_receiver_;
-};
-
-TEST_F(RtpReceiverTest, GetSources) {
-  int64_t now_ms = fake_clock_.TimeInMilliseconds();
-
-  RTPHeader header;
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(now_ms);
-  header.numCSRCs = 2;
-  header.arrOfCSRCs[0] = kCsrc1;
-  header.arrOfCSRCs[1] = kCsrc2;
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  auto sources = rtp_receiver_->GetSources();
-  // One SSRC source and two CSRC sources.
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
-                           RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
-                           RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
-
-  // Advance the fake clock and the method is expected to return the
-  // contributing source object with same source id and updated timestamp.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  now_ms = fake_clock_.TimeInMilliseconds();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
-                           RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC),
-                           RtpSource(now_ms, kCsrc2, RtpSourceType::CSRC)));
-
-  // Test the edge case that the sources are still there just before the
-  // timeout.
-  int64_t prev_time_ms = fake_clock_.TimeInMilliseconds();
-  fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources,
-              UnorderedElementsAre(
-                  RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
-                  RtpSource(prev_time_ms, kCsrc1, RtpSourceType::CSRC),
-                  RtpSource(prev_time_ms, kCsrc2, RtpSourceType::CSRC)));
-
-  // Time out.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  sources = rtp_receiver_->GetSources();
-  // All the sources should be out of date.
-  ASSERT_EQ(0u, sources.size());
-}
-
-// Test the case that the SSRC is changed.
-TEST_F(RtpReceiverTest, GetSourcesChangeSSRC) {
-  int64_t prev_time_ms = -1;
-  int64_t now_ms = fake_clock_.TimeInMilliseconds();
-
-  RTPHeader header;
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(now_ms);
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  auto sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
-
-  // The SSRC is changed and the old SSRC is expected to be returned.
-  fake_clock_.AdvanceTimeMilliseconds(100);
-  prev_time_ms = now_ms;
-  now_ms = fake_clock_.TimeInMilliseconds();
-  header.ssrc = kSsrc2;
-  header.timestamp = rtp_timestamp(now_ms);
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(prev_time_ms, kSsrc1, RtpSourceType::SSRC),
-                           RtpSource(now_ms, kSsrc2, RtpSourceType::SSRC)));
-
-  // The SSRC is changed again and happen to be changed back to 1. No
-  // duplication is expected.
-  fake_clock_.AdvanceTimeMilliseconds(100);
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(now_ms);
-  prev_time_ms = now_ms;
-  now_ms = fake_clock_.TimeInMilliseconds();
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(prev_time_ms, kSsrc2, RtpSourceType::SSRC),
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
-
-  // Old SSRC source timeout.
-  fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
-  now_ms = fake_clock_.TimeInMilliseconds();
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC)));
-}
-
-TEST_F(RtpReceiverTest, GetSourcesRemoveOutdatedSource) {
-  int64_t now_ms = fake_clock_.TimeInMilliseconds();
-
-  RTPHeader header;
-  header.payloadType = kPcmuPayloadType;
-  header.timestamp = rtp_timestamp(now_ms);
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-  header.numCSRCs = 1;
-  size_t kSourceListSize = 20;
-
-  for (size_t i = 0; i < kSourceListSize; ++i) {
-    header.ssrc = i;
-    header.arrOfCSRCs[0] = (i + 1);
-    EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-        header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  }
-
-  RtpSource source(0, 0, RtpSourceType::SSRC);
-  auto sources = rtp_receiver_->GetSources();
-  // Expect |kSourceListSize| SSRC sources and |kSourceListSize| CSRC sources.
-  ASSERT_EQ(2 * kSourceListSize, sources.size());
-  for (size_t i = 0; i < kSourceListSize; ++i) {
-    // The SSRC source IDs are expected to be 19, 18, 17 ... 0
-    ASSERT_TRUE(
-        FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
-    EXPECT_EQ(now_ms, source.timestamp_ms());
-
-    // The CSRC source IDs are expected to be 20, 19, 18 ... 1
-    ASSERT_TRUE(
-        FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
-    EXPECT_EQ(now_ms, source.timestamp_ms());
-  }
-
-  fake_clock_.AdvanceTimeMilliseconds(kGetSourcesTimeoutMs);
-  for (size_t i = 0; i < kSourceListSize; ++i) {
-    // The SSRC source IDs are expected to be 19, 18, 17 ... 0
-    ASSERT_TRUE(
-        FindSourceByIdAndType(sources, i, RtpSourceType::SSRC, &source));
-    EXPECT_EQ(now_ms, source.timestamp_ms());
-
-    // The CSRC source IDs are expected to be 20, 19, 18 ... 1
-    ASSERT_TRUE(
-        FindSourceByIdAndType(sources, (i + 1), RtpSourceType::CSRC, &source));
-    EXPECT_EQ(now_ms, source.timestamp_ms());
-  }
-
-  // Timeout. All the existing objects are out of date and are expected to be
-  // removed.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  header.ssrc = kSsrc1;
-  header.arrOfCSRCs[0] = kCsrc1;
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  now_ms = fake_clock_.TimeInMilliseconds();
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(now_ms, kSsrc1, RtpSourceType::SSRC),
-                           RtpSource(now_ms, kCsrc1, RtpSourceType::CSRC)));
-}
-
-// The audio level from the RTPHeader extension should be stored in the
-// RtpSource with the matching SSRC.
-TEST_F(RtpReceiverTest, GetSourcesContainsAudioLevelExtension) {
-  RTPHeader header;
-  int64_t time1_ms = fake_clock_.TimeInMilliseconds();
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(time1_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 10;
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  auto sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
-                           time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
-
-  // Receive a packet from a different SSRC with a different level and check
-  // that they are both remembered.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  int64_t time2_ms = fake_clock_.TimeInMilliseconds();
-  header.ssrc = kSsrc2;
-  header.timestamp = rtp_timestamp(time2_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 20;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources,
-              UnorderedElementsAre(
-                  RtpSource(time1_ms, kSsrc1, RtpSourceType::SSRC, 10),
-                  RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
-
-  // Receive a packet from the first SSRC again and check that the level is
-  // updated.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  int64_t time3_ms = fake_clock_.TimeInMilliseconds();
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(time3_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 30;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources,
-              UnorderedElementsAre(
-                  RtpSource(time3_ms, kSsrc1, RtpSourceType::SSRC, 30),
-                  RtpSource(time2_ms, kSsrc2, RtpSourceType::SSRC, 20)));
-}
-
-TEST_F(RtpReceiverTest,
-       MissingAudioLevelHeaderExtensionClearsRtpSourceAudioLevel) {
-  RTPHeader header;
-  int64_t time1_ms = fake_clock_.TimeInMilliseconds();
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(time1_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 10;
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  auto sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(RtpSource(
-                           time1_ms, kSsrc1, RtpSourceType::SSRC, 10)));
-
-  // Receive a second packet without the audio level header extension and check
-  // that the audio level is cleared.
-  fake_clock_.AdvanceTimeMilliseconds(1);
-  int64_t time2_ms = fake_clock_.TimeInMilliseconds();
-  header.timestamp = rtp_timestamp(time2_ms);
-  header.extension.hasAudioLevel = false;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  sources = rtp_receiver_->GetSources();
-  EXPECT_THAT(sources, UnorderedElementsAre(
-                           RtpSource(time2_ms, kSsrc1, RtpSourceType::SSRC)));
-}
-
-TEST_F(RtpReceiverTest, UpdatesTimestampsIfAndOnlyIfPacketArrivesInOrder) {
-  RTPHeader header;
-  int64_t time1_ms = fake_clock_.TimeInMilliseconds();
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(time1_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 10;
-  header.sequenceNumber = 0xfff0;
-
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-  uint32_t latest_timestamp;
-  int64_t latest_receive_time_ms;
-
-  // No packet received yet.
-  EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                  &latest_receive_time_ms));
-  // Initial packet
-  const uint32_t timestamp_1 = header.timestamp;
-  const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds();
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_1);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_1);
-
-  // Late packet, timestamp not recorded.
-  fake_clock_.AdvanceTimeMilliseconds(10);
-  header.timestamp -= 900;
-  header.sequenceNumber -= 2;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_1);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_1);
-
-  // New packet, still late, no wraparound.
-  fake_clock_.AdvanceTimeMilliseconds(10);
-  header.timestamp += 1800;
-  header.sequenceNumber += 1;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_1);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_1);
-
-  // New packet, new timestamp recorded
-  fake_clock_.AdvanceTimeMilliseconds(10);
-  header.timestamp += 900;
-  header.sequenceNumber += 2;
-  const uint32_t timestamp_2 = header.timestamp;
-  const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds();
-  const uint16_t seqno_2 = header.sequenceNumber;
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_2);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_2);
-
-  // New packet, timestamp wraps around
-  fake_clock_.AdvanceTimeMilliseconds(10);
-  header.timestamp += 900;
-  header.sequenceNumber += 20;
-  const uint32_t timestamp_3 = header.timestamp;
-  const int64_t receive_time_3 = fake_clock_.TimeInMilliseconds();
-  EXPECT_LT(header.sequenceNumber, seqno_2);  // Wrap-around
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_3);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_3);
-}
-
-TEST_F(RtpReceiverTest, UpdatesTimestampsWhenStreamResets) {
-  RTPHeader header;
-  int64_t time1_ms = fake_clock_.TimeInMilliseconds();
-  header.payloadType = kPcmuPayloadType;
-  header.ssrc = kSsrc1;
-  header.timestamp = rtp_timestamp(time1_ms);
-  header.extension.hasAudioLevel = true;
-  header.extension.audioLevel = 10;
-  header.sequenceNumber = 0xfff0;
-
-  const PayloadUnion payload_specific{
-      AudioPayload{SdpAudioFormat("foo", 8000, 1), 0}};
-  uint32_t latest_timestamp;
-  int64_t latest_receive_time_ms;
-
-  // No packet received yet.
-  EXPECT_FALSE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                  &latest_receive_time_ms));
-  // Initial packet
-  const uint32_t timestamp_1 = header.timestamp;
-  const int64_t receive_time_1 = fake_clock_.TimeInMilliseconds();
-  const uint16_t seqno_1 = header.sequenceNumber;
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_1);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_1);
-
-  // Packet with far in the past seqno, but unlikely to be a wrap-around.
-  // Treated as a seqno discontinuity, and timestamp is recorded.
-  fake_clock_.AdvanceTimeMilliseconds(10);
-  header.timestamp += 900;
-  header.sequenceNumber = 0x9000;
-
-  const uint32_t timestamp_2 = header.timestamp;
-  const int64_t receive_time_2 = fake_clock_.TimeInMilliseconds();
-  const uint16_t seqno_2 = header.sequenceNumber;
-  EXPECT_LT(seqno_1 - seqno_2, 0x8000);  // In the past.
-
-  EXPECT_TRUE(rtp_receiver_->IncomingRtpPacket(
-      header, kTestPayload, sizeof(kTestPayload), payload_specific));
-  EXPECT_TRUE(rtp_receiver_->GetLatestTimestamps(&latest_timestamp,
-                                                 &latest_receive_time_ms));
-  EXPECT_EQ(latest_timestamp, timestamp_2);
-  EXPECT_EQ(latest_receive_time_ms, receive_time_2);
-}
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_video.cc b/modules/rtp_rtcp/source/rtp_receiver_video.cc
deleted file mode 100644
index 1101bec..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_video.cc
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "modules/rtp_rtcp/source/rtp_receiver_video.h"
-
-#include <assert.h>
-#include <string.h>
-
-#include <memory>
-
-#include "modules/rtp_rtcp/include/rtp_cvo.h"
-#include "modules/rtp_rtcp/include/rtp_payload_registry.h"
-#include "modules/rtp_rtcp/source/rtp_format.h"
-#include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/trace_event.h"
-
-namespace webrtc {
-
-RTPReceiverStrategy* RTPReceiverStrategy::CreateVideoStrategy(
-    RtpData* data_callback) {
-  return new RTPReceiverVideo(data_callback);
-}
-
-RTPReceiverVideo::RTPReceiverVideo(RtpData* data_callback)
-    : RTPReceiverStrategy(data_callback) {}
-
-RTPReceiverVideo::~RTPReceiverVideo() {}
-
-int32_t RTPReceiverVideo::ParseRtpPacket(WebRtcRTPHeader* rtp_header,
-                                         const PayloadUnion& specific_payload,
-                                         const uint8_t* payload,
-                                         size_t payload_length,
-                                         int64_t timestamp_ms) {
-  rtp_header->video_header().codec =
-      specific_payload.video_payload().videoCodecType;
-
-  RTC_DCHECK_GE(payload_length, rtp_header->header.paddingLength);
-  const size_t payload_data_length =
-      payload_length - rtp_header->header.paddingLength;
-
-  if (payload == NULL || payload_data_length == 0) {
-    return data_callback_->OnReceivedPayloadData(NULL, 0, rtp_header) == 0 ? 0
-                                                                           : -1;
-  }
-
-  if (first_packet_received_()) {
-    RTC_LOG(LS_INFO) << "Received first video RTP packet";
-  }
-
-  // We are not allowed to hold a critical section when calling below functions.
-  std::unique_ptr<RtpDepacketizer> depacketizer(
-      RtpDepacketizer::Create(rtp_header->video_header().codec));
-  if (depacketizer.get() == NULL) {
-    RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
-    return -1;
-  }
-
-  RtpDepacketizer::ParsedPayload parsed_payload;
-  if (!depacketizer->Parse(&parsed_payload, payload, payload_data_length))
-    return -1;
-
-  rtp_header->frameType = parsed_payload.frame_type;
-  rtp_header->video_header() = parsed_payload.video_header();
-  rtp_header->video_header().rotation = kVideoRotation_0;
-  rtp_header->video_header().content_type = VideoContentType::UNSPECIFIED;
-  rtp_header->video_header().video_timing.flags = VideoSendTiming::kInvalid;
-
-  // Retrieve the video rotation information.
-  if (rtp_header->header.extension.hasVideoRotation) {
-    rtp_header->video_header().rotation =
-        rtp_header->header.extension.videoRotation;
-  }
-
-  if (rtp_header->header.extension.hasVideoContentType) {
-    rtp_header->video_header().content_type =
-        rtp_header->header.extension.videoContentType;
-  }
-
-  if (rtp_header->header.extension.has_video_timing) {
-    rtp_header->video_header().video_timing =
-        rtp_header->header.extension.video_timing;
-  }
-
-  rtp_header->video_header().playout_delay =
-      rtp_header->header.extension.playout_delay;
-
-  return data_callback_->OnReceivedPayloadData(parsed_payload.payload,
-                                               parsed_payload.payload_length,
-                                               rtp_header) == 0
-             ? 0
-             : -1;
-}
-
-}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_receiver_video.h b/modules/rtp_rtcp/source/rtp_receiver_video.h
deleted file mode 100644
index 46b97f5..0000000
--- a/modules/rtp_rtcp/source/rtp_receiver_video.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- *  Copyright (c) 2012 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 MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_VIDEO_H_
-#define MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_VIDEO_H_
-
-#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_strategy.h"
-#include "modules/rtp_rtcp/source/rtp_utility.h"
-#include "rtc_base/onetimeevent.h"
-
-namespace webrtc {
-
-class RTPReceiverVideo : public RTPReceiverStrategy {
- public:
-  explicit RTPReceiverVideo(RtpData* data_callback);
-
-  ~RTPReceiverVideo() override;
-
-  int32_t ParseRtpPacket(WebRtcRTPHeader* rtp_header,
-                         const PayloadUnion& specific_payload,
-                         const uint8_t* packet,
-                         size_t packet_length,
-                         int64_t timestamp) override;
-
-  void SetPacketOverHead(uint16_t packet_over_head);
-
- private:
-  OneTimeEvent first_packet_received_;
-};
-}  // namespace webrtc
-
-#endif  // MODULES_RTP_RTCP_SOURCE_RTP_RECEIVER_VIDEO_H_
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl.cc b/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
index fe8dbf3..392b91d 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
@@ -33,29 +33,6 @@
 const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
 }  // namespace
 
-RTPExtensionType StringToRtpExtensionType(const std::string& extension) {
-  if (extension == RtpExtension::kTimestampOffsetUri)
-    return kRtpExtensionTransmissionTimeOffset;
-  if (extension == RtpExtension::kAudioLevelUri)
-    return kRtpExtensionAudioLevel;
-  if (extension == RtpExtension::kAbsSendTimeUri)
-    return kRtpExtensionAbsoluteSendTime;
-  if (extension == RtpExtension::kVideoRotationUri)
-    return kRtpExtensionVideoRotation;
-  if (extension == RtpExtension::kTransportSequenceNumberUri)
-    return kRtpExtensionTransportSequenceNumber;
-  if (extension == RtpExtension::kPlayoutDelayUri)
-    return kRtpExtensionPlayoutDelay;
-  if (extension == RtpExtension::kVideoContentTypeUri)
-    return kRtpExtensionVideoContentType;
-  if (extension == RtpExtension::kVideoTimingUri)
-    return kRtpExtensionVideoTiming;
-  if (extension == RtpExtension::kMidUri)
-    return kRtpExtensionMid;
-  RTC_NOTREACHED() << "Looking up unsupported RTP extension.";
-  return kRtpExtensionNone;
-}
-
 RtpRtcp::Configuration::Configuration() = default;
 
 RtpRtcp* RtpRtcp::CreateRtpRtcp(const RtpRtcp::Configuration& configuration) {
@@ -103,8 +80,7 @@
                          kRtpRtcpMaxIdleTimeProcessMs),
       next_keepalive_time_(-1),
       packet_overhead_(28),  // IPV4 UDP.
-      nack_last_time_sent_full_(0),
-      nack_last_time_sent_full_prev_(0),
+      nack_last_time_sent_full_ms_(0),
       nack_last_seq_number_sent_(0),
       key_frame_req_method_(kKeyFrameReqPliRtcp),
       remote_bitrate_(configuration.remote_bitrate_estimator),
@@ -416,6 +392,11 @@
   return rtp_sender_ ? rtp_sender_->SendingMedia() : false;
 }
 
+void ModuleRtpRtcpImpl::SetAsPartOfAllocation(bool part_of_allocation) {
+  RTC_CHECK(rtp_sender_);
+  rtp_sender_->SetAsPartOfAllocation(part_of_allocation);
+}
+
 bool ModuleRtpRtcpImpl::SendOutgoingData(
     FrameType frame_type,
     int8_t payload_type,
@@ -550,12 +531,6 @@
   return rtcp_sender_.SetApplicationSpecificData(sub_type, name, data, length);
 }
 
-// (XR) VOIP metric.
-int32_t ModuleRtpRtcpImpl::SetRTCPVoIPMetrics(
-    const RTCPVoIPMetric* voip_metric) {
-  return rtcp_sender_.SetRTCPVoIPMetrics(voip_metric);
-}
-
 void ModuleRtpRtcpImpl::SetRtcpXrRrtrStatus(bool enable) {
   rtcp_receiver_.SetRtcpXrRrtrStatus(enable);
   rtcp_sender_.SendRtcpXrReceiverReferenceTime(enable);
@@ -640,6 +615,11 @@
   return rtp_sender_->RegisterRtpHeaderExtension(type, id);
 }
 
+bool ModuleRtpRtcpImpl::RegisterRtpHeaderExtension(const std::string& uri,
+                                                   int id) {
+  return rtp_sender_->RegisterRtpHeaderExtension(uri, id);
+}
+
 int32_t ModuleRtpRtcpImpl::DeregisterSendRtpHeaderExtension(
     const RTPExtensionType type) {
   return rtp_sender_->DeregisterRtpHeaderExtension(type);
@@ -686,10 +666,9 @@
   }
   uint16_t nack_length = size;
   uint16_t start_id = 0;
-  int64_t now = clock_->TimeInMilliseconds();
-  if (TimeToSendFullNackList(now)) {
-    nack_last_time_sent_full_ = now;
-    nack_last_time_sent_full_prev_ = now;
+  int64_t now_ms = clock_->TimeInMilliseconds();
+  if (TimeToSendFullNackList(now_ms)) {
+    nack_last_time_sent_full_ms_ = now_ms;
   } else {
     // Only send extended list.
     if (nack_last_seq_number_sent_ == nack_list[size - 1]) {
@@ -737,10 +716,7 @@
   }
 
   // Send a full NACK list once within every |wait_time|.
-  if (rtt_stats_) {
-    return now - nack_last_time_sent_full_ > wait_time;
-  }
-  return now - nack_last_time_sent_full_prev_ > wait_time;
+  return now - nack_last_time_sent_full_ms_ > wait_time;
 }
 
 // Store the sent packets, needed to answer to Negative acknowledgment requests.
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl.h b/modules/rtp_rtcp/source/rtp_rtcp_impl.h
index 9f53416..9c2b085 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl.h
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl.h
@@ -62,6 +62,7 @@
   // Register RTP header extension.
   int32_t RegisterSendRtpHeaderExtension(RTPExtensionType type,
                                          uint8_t id) override;
+  bool RegisterRtpHeaderExtension(const std::string& uri, int id) override;
 
   int32_t DeregisterSendRtpHeaderExtension(RTPExtensionType type) override;
 
@@ -114,6 +115,8 @@
 
   bool SendingMedia() const override;
 
+  void SetAsPartOfAllocation(bool part_of_allocation) override;
+
   // Used by the codec module to deliver a video or audio frame for
   // packetization.
   bool SendOutgoingData(FrameType frame_type,
@@ -239,9 +242,6 @@
                                          const uint8_t* data,
                                          uint16_t length) override;
 
-  // (XR) VOIP metric.
-  int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) override;
-
   // (XR) Receiver reference time report.
   void SetRtcpXrRrtrStatus(bool enable) override;
 
@@ -337,8 +337,7 @@
   uint16_t packet_overhead_;
 
   // Send side
-  int64_t nack_last_time_sent_full_;
-  uint32_t nack_last_time_sent_full_prev_;
+  int64_t nack_last_time_sent_full_ms_;
   uint16_t nack_last_seq_number_sent_;
 
   KeyFrameRequestMethod key_frame_req_method_;
diff --git a/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc b/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
index 2de6175..5160a64 100644
--- a/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_rtcp_impl_unittest.cc
@@ -17,6 +17,7 @@
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "modules/rtp_rtcp/source/rtcp_packet.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
+#include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
 #include "rtc_base/rate_limiter.h"
 #include "test/gmock.h"
@@ -37,7 +38,6 @@
 const uint8_t kBaseLayerTid = 0;
 const uint8_t kHigherLayerTid = 1;
 const uint16_t kSequenceNumber = 100;
-const int64_t kMaxRttMs = 1000;
 
 class RtcpRttStatsTestImpl : public RtcpRttStats {
  public:
@@ -116,7 +116,6 @@
   explicit RtpRtcpModule(SimulatedClock* clock)
       : receive_statistics_(ReceiveStatistics::Create(clock)),
         remote_ssrc_(0),
-        retransmission_rate_limiter_(clock, kMaxRttMs),
         clock_(clock) {
     CreateModuleImpl();
     transport_.SimulateNetworkDelay(kOneWayNetworkDelayMs, clock);
@@ -129,7 +128,6 @@
   RtcpRttStatsTestImpl rtt_stats_;
   std::unique_ptr<ModuleRtpRtcpImpl> impl_;
   uint32_t remote_ssrc_;
-  RateLimiter retransmission_rate_limiter_;
   RtpKeepAliveConfig keepalive_config_;
   RtcpIntervalConfig rtcp_interval_config_;
 
@@ -180,7 +178,6 @@
     config.receive_statistics = receive_statistics_.get();
     config.rtcp_packet_type_counter_observer = this;
     config.rtt_stats = &rtt_stats_;
-    config.retransmission_rate_limiter = &retransmission_rate_limiter_;
     config.keepalive_config = keepalive_config_;
     config.rtcp_interval_config = rtcp_interval_config_;
 
@@ -240,7 +237,7 @@
     rtp_video_header.is_first_packet_in_frame = true;
     rtp_video_header.simulcastIdx = 0;
     rtp_video_header.codec = kVideoCodecVP8;
-    rtp_video_header.vp8() = vp8_header;
+    rtp_video_header.video_type_header = vp8_header;
     rtp_video_header.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false};
 
     const uint8_t payload[100] = {0};
@@ -322,12 +319,12 @@
 }
 
 TEST_F(RtpRtcpImplTest, Rtt) {
-  RTPHeader header;
-  header.timestamp = 1;
-  header.sequenceNumber = 123;
-  header.ssrc = kSenderSsrc;
-  header.headerLength = 12;
-  receiver_.receive_statistics_->IncomingPacket(header, 100);
+  RtpPacketReceived packet;
+  packet.SetTimestamp(1);
+  packet.SetSequenceNumber(123);
+  packet.SetSsrc(kSenderSsrc);
+  packet.AllocatePayload(100 - 12);
+  receiver_.receive_statistics_->OnRtpPacket(packet);
 
   // Send Frame before sending an SR.
   SendFrame(&sender_, kBaseLayerTid);
diff --git a/modules/rtp_rtcp/source/rtp_sender.cc b/modules/rtp_rtcp/source/rtp_sender.cc
index ba04b5c..1678a03 100644
--- a/modules/rtp_rtcp/source/rtp_sender.cc
+++ b/modules/rtp_rtcp/source/rtp_sender.cc
@@ -11,6 +11,7 @@
 #include "modules/rtp_rtcp/source/rtp_sender.h"
 
 #include <algorithm>
+#include <limits>
 #include <string>
 #include <utility>
 
@@ -21,6 +22,7 @@
 #include "modules/rtp_rtcp/include/rtp_cvo.h"
 #include "modules/rtp_rtcp/source/byte_io.h"
 #include "modules/rtp_rtcp/source/playout_delay_oracle.h"
+#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
 #include "modules/rtp_rtcp/source/rtp_sender_audio.h"
@@ -73,6 +75,8 @@
     CreateExtensionSize<VideoContentTypeExtension>(),
     CreateExtensionSize<VideoTimingExtension>(),
     {RtpMid::kId, RtpMid::kMaxValueSizeBytes},
+    {RtpGenericFrameDescriptorExtension::kId,
+     RtpGenericFrameDescriptorExtension::kMaxSizeBytes},
 };
 
 const char* FrameTypeToString(FrameType frame_type) {
@@ -128,7 +132,8 @@
       transport_feedback_observer_(transport_feedback_observer),
       last_capture_time_ms_sent_(0),
       transport_(transport),
-      sending_media_(true),                   // Default to sending media.
+      sending_media_(true),  // Default to sending media.
+      force_part_of_allocation_(false),
       max_packet_size_(IP_PACKET_SIZE - 28),  // Default is IP-v4/UDP.
       last_payload_type_(-1),
       payload_type_map_(),
@@ -136,6 +141,9 @@
       packet_history_(clock),
       flexfec_packet_history_(clock),
       // Statistics
+      send_delays_(),
+      max_delay_it_(send_delays_.end()),
+      sum_delays_ms_(0),
       rtp_stats_callback_(nullptr),
       total_bitrate_sent_(kBitrateStatisticsWindowMs,
                           RateStatistics::kBpsScale),
@@ -160,9 +168,7 @@
       overhead_observer_(overhead_observer),
       populate_network2_timestamp_(populate_network2_timestamp),
       send_side_bwe_with_overhead_(
-          webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
-      unlimited_retransmission_experiment_(
-          field_trial::IsEnabled("WebRTC-UnlimitedScreenshareRetransmission")) {
+          webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")) {
   // This random initialization is not intended to be cryptographic strong.
   timestamp_offset_ = random_.Rand<uint32_t>();
   // Random start, 16 bits. Can't be 0.
@@ -238,6 +244,11 @@
   return rtp_header_extension_map_.RegisterByType(id, type) ? 0 : -1;
 }
 
+bool RTPSender::RegisterRtpHeaderExtension(const std::string& uri, int id) {
+  rtc::CritScope lock(&send_critsect_);
+  return rtp_header_extension_map_.RegisterByUri(id, uri);
+}
+
 bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) const {
   rtc::CritScope lock(&send_critsect_);
   return rtp_header_extension_map_.IsRegistered(type);
@@ -414,11 +425,6 @@
       *transport_frame_id_out = rtp_timestamp;
     if (!sending_media_)
       return true;
-
-    // Cache video content type.
-    if (!audio_configured_ && rtp_header) {
-      video_content_type_ = rtp_header->content_type;
-    }
   }
   VideoCodecType video_type = kVideoCodecGeneric;
   if (CheckPayloadType(payload_type, &video_type) != 0) {
@@ -607,10 +613,16 @@
     PacketOptions options;
     // Padding packets are never retransmissions.
     options.is_retransmit = false;
-    bool has_transport_seq_num =
-        UpdateTransportSequenceNumber(&padding_packet, &options.packet_id);
-    padding_packet.SetPadding(padding_bytes_in_packet, &random_);
-
+    bool has_transport_seq_num;
+    {
+      rtc::CritScope lock(&send_critsect_);
+      has_transport_seq_num =
+          UpdateTransportSequenceNumber(&padding_packet, &options.packet_id);
+      options.included_in_allocation =
+          has_transport_seq_num || force_part_of_allocation_;
+      options.included_in_feedback = has_transport_seq_num;
+    }
+    padding_packet.SetPadding(padding_bytes_in_packet);
     if (has_transport_seq_num) {
       AddPacketToTransportFeedback(options.packet_id, padding_packet,
                                    pacing_info);
@@ -650,22 +662,13 @@
 
   const int32_t packet_size = static_cast<int32_t>(stored_packet->payload_size);
 
-  // Skip retransmission rate check if sending screenshare and the experiment
-  // is on.
-  bool skip_retransmission_rate_limit;
-  {
-    rtc::CritScope lock(&send_critsect_);
-    skip_retransmission_rate_limit =
-        unlimited_retransmission_experiment_ && video_content_type_ &&
-        videocontenttypehelpers::IsScreenshare(*video_content_type_);
-  }
-
-  RTC_DCHECK(retransmission_rate_limiter_);
-  // Check if we're overusing retransmission bitrate.
-  // TODO(sprang): Add histograms for nack success or failure reasons.
-  if (!skip_retransmission_rate_limit &&
-      !retransmission_rate_limiter_->TryUseRate(packet_size)) {
-    return -1;
+  // Skip retransmission rate check if not configured.
+  if (retransmission_rate_limiter_) {
+    // Check if we're overusing retransmission bitrate.
+    // TODO(sprang): Add histograms for nack success or failure reasons.
+    if (!retransmission_rate_limiter_->TryUseRate(packet_size)) {
+      return -1;
+    }
   }
 
   if (paced_sender_) {
@@ -824,7 +827,16 @@
   // E.g. RTPSender::TrySendRedundantPayloads calls PrepareAndSendPacket with
   // send_over_rtx = true but is_retransmit = false.
   options.is_retransmit = is_retransmit || send_over_rtx;
-  if (UpdateTransportSequenceNumber(packet_to_send, &options.packet_id)) {
+  bool has_transport_seq_num;
+  {
+    rtc::CritScope lock(&send_critsect_);
+    has_transport_seq_num =
+        UpdateTransportSequenceNumber(packet_to_send, &options.packet_id);
+    options.included_in_allocation =
+        has_transport_seq_num || force_part_of_allocation_;
+    options.included_in_feedback = has_transport_seq_num;
+  }
+  if (has_transport_seq_num) {
     AddPacketToTransportFeedback(options.packet_id, *packet_to_send,
                                  pacing_info);
   }
@@ -957,7 +969,17 @@
 
   PacketOptions options;
   options.is_retransmit = false;
-  if (UpdateTransportSequenceNumber(packet.get(), &options.packet_id)) {
+
+  bool has_transport_seq_num;
+  {
+    rtc::CritScope lock(&send_critsect_);
+    has_transport_seq_num =
+        UpdateTransportSequenceNumber(packet.get(), &options.packet_id);
+    options.included_in_allocation =
+        has_transport_seq_num || force_part_of_allocation_;
+    options.included_in_feedback = has_transport_seq_num;
+  }
+  if (has_transport_seq_num) {
     AddPacketToTransportFeedback(options.packet_id, *packet.get(),
                                  PacedPacketInfo());
   }
@@ -988,12 +1010,21 @@
   return sent;
 }
 
+void RTPSender::RecomputeMaxSendDelay() {
+  max_delay_it_ = send_delays_.begin();
+  for (auto it = send_delays_.begin(); it != send_delays_.end(); ++it) {
+    if (it->second >= max_delay_it_->second) {
+      max_delay_it_ = it;
+    }
+  }
+}
+
 void RTPSender::UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms) {
   if (!send_side_delay_observer_ || capture_time_ms <= 0)
     return;
 
   uint32_t ssrc;
-  int64_t avg_delay_ms = 0;
+  int avg_delay_ms = 0;
   int max_delay_ms = 0;
   {
     rtc::CritScope lock(&send_critsect_);
@@ -1003,24 +1034,68 @@
   }
   {
     rtc::CritScope cs(&statistics_crit_);
-    // TODO(holmer): Compute this iteratively instead.
-    send_delays_[now_ms] = now_ms - capture_time_ms;
-    send_delays_.erase(
-        send_delays_.begin(),
-        send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs));
-    int num_delays = 0;
-    for (auto it = send_delays_.upper_bound(now_ms - kSendSideDelayWindowMs);
-         it != send_delays_.end(); ++it) {
-      max_delay_ms = std::max(max_delay_ms, it->second);
-      avg_delay_ms += it->second;
-      ++num_delays;
+    // Compute the max and average of the recent capture-to-send delays.
+    // The time complexity of the current approach depends on the distribution
+    // of the delay values. This could be done more efficiently.
+
+    // Remove elements older than kSendSideDelayWindowMs.
+    auto lower_bound =
+        send_delays_.lower_bound(now_ms - kSendSideDelayWindowMs);
+    for (auto it = send_delays_.begin(); it != lower_bound; ++it) {
+      if (max_delay_it_ == it) {
+        max_delay_it_ = send_delays_.end();
+      }
+      sum_delays_ms_ -= it->second;
     }
-    if (num_delays == 0)
-      return;
-    avg_delay_ms = (avg_delay_ms + num_delays / 2) / num_delays;
+    send_delays_.erase(send_delays_.begin(), lower_bound);
+    if (max_delay_it_ == send_delays_.end()) {
+      // Removed the previous max. Need to recompute.
+      RecomputeMaxSendDelay();
+    }
+
+    // Add the new element.
+    RTC_DCHECK_GE(now_ms, static_cast<int64_t>(0));
+    RTC_DCHECK_LE(now_ms, std::numeric_limits<int64_t>::max() / 2);
+    RTC_DCHECK_GE(capture_time_ms, static_cast<int64_t>(0));
+    RTC_DCHECK_LE(capture_time_ms, std::numeric_limits<int64_t>::max() / 2);
+    int64_t diff_ms = now_ms - capture_time_ms;
+    RTC_DCHECK_GE(diff_ms, static_cast<int64_t>(0));
+    RTC_DCHECK_LE(diff_ms,
+                  static_cast<int64_t>(std::numeric_limits<int>::max()));
+    int new_send_delay = rtc::dchecked_cast<int>(now_ms - capture_time_ms);
+    SendDelayMap::iterator it;
+    bool inserted;
+    std::tie(it, inserted) =
+        send_delays_.insert(std::make_pair(now_ms, new_send_delay));
+    if (!inserted) {
+      // TODO(terelius): If we have multiple delay measurements during the same
+      // millisecond then we keep the most recent one. It is not clear that this
+      // is the right decision, but it preserves an earlier behavior.
+      int previous_send_delay = it->second;
+      sum_delays_ms_ -= previous_send_delay;
+      it->second = new_send_delay;
+      if (max_delay_it_ == it && new_send_delay < previous_send_delay) {
+        RecomputeMaxSendDelay();
+      }
+    }
+    if (max_delay_it_ == send_delays_.end() ||
+        it->second >= max_delay_it_->second) {
+      max_delay_it_ = it;
+    }
+    sum_delays_ms_ += new_send_delay;
+
+    size_t num_delays = send_delays_.size();
+    RTC_DCHECK(max_delay_it_ != send_delays_.end());
+    max_delay_ms = rtc::dchecked_cast<int>(max_delay_it_->second);
+    int64_t avg_ms = (sum_delays_ms_ + num_delays / 2) / num_delays;
+    RTC_DCHECK_GE(avg_ms, static_cast<int64_t>(0));
+    RTC_DCHECK_LE(avg_ms,
+                  static_cast<int64_t>(std::numeric_limits<int>::max()));
+    avg_delay_ms =
+        rtc::dchecked_cast<int>((sum_delays_ms_ + num_delays / 2) / num_delays);
   }
-  send_side_delay_observer_->SendSideDelayUpdated(
-      rtc::dchecked_cast<int>(avg_delay_ms), max_delay_ms, ssrc);
+  send_side_delay_observer_->SendSideDelayUpdated(avg_delay_ms, max_delay_ms,
+                                                  ssrc);
 }
 
 void RTPSender::UpdateOnSendPacket(int packet_id,
@@ -1053,8 +1128,8 @@
   rtc::CritScope lock(&send_critsect_);
   size_t rtp_header_length = kRtpHeaderLength;
   rtp_header_length += sizeof(uint32_t) * csrcs_.size();
-  rtp_header_length += rtp_header_extension_map_.GetTotalLengthInBytes(
-      kFecOrPaddingExtensionSizes);
+  rtp_header_length += RtpHeaderExtensionSize(kFecOrPaddingExtensionSizes,
+                                              rtp_header_extension_map_);
   return rtp_header_length;
 }
 
@@ -1114,10 +1189,9 @@
 }
 
 bool RTPSender::UpdateTransportSequenceNumber(RtpPacketToSend* packet,
-                                              int* packet_id) const {
+                                              int* packet_id) {
   RTC_DCHECK(packet);
   RTC_DCHECK(packet_id);
-  rtc::CritScope lock(&send_critsect_);
   if (!rtp_header_extension_map_.IsRegistered(TransportSequenceNumber::kId))
     return false;
 
@@ -1142,6 +1216,11 @@
   return sending_media_;
 }
 
+void RTPSender::SetAsPartOfAllocation(bool part_of_allocation) {
+  rtc::CritScope lock(&send_critsect_);
+  force_part_of_allocation_ = part_of_allocation;
+}
+
 void RTPSender::SetTimestampOffset(uint32_t timestamp) {
   rtc::CritScope lock(&send_critsect_);
   timestamp_offset_ = timestamp;
diff --git a/modules/rtp_rtcp/source/rtp_sender.h b/modules/rtp_rtcp/source/rtp_sender.h
index 7f66d24..e9095d1 100644
--- a/modules/rtp_rtcp/source/rtp_sender.h
+++ b/modules/rtp_rtcp/source/rtp_sender.h
@@ -20,7 +20,6 @@
 #include "absl/types/optional.h"
 #include "api/array_view.h"
 #include "api/call/transport.h"
-#include "api/video/video_content_type.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/rtp_rtcp/include/flexfec_sender.h"
 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
@@ -86,6 +85,8 @@
   void SetSendingMediaStatus(bool enabled);
   bool SendingMedia() const;
 
+  void SetAsPartOfAllocation(bool part_of_allocation);
+
   void GetDataCounters(StreamDataCounters* rtp_stats,
                        StreamDataCounters* rtx_stats) const;
 
@@ -116,6 +117,7 @@
 
   // RTP header extension
   int32_t RegisterRtpHeaderExtension(RTPExtensionType type, uint8_t id);
+  bool RegisterRtpHeaderExtension(const std::string& uri, int id);
   bool IsRtpHeaderExtensionRegistered(RTPExtensionType type) const;
   int32_t DeregisterRtpHeaderExtension(RTPExtensionType type);
 
@@ -240,13 +242,14 @@
                            const PacketOptions& options,
                            const PacedPacketInfo& pacing_info);
 
+  void RecomputeMaxSendDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(statistics_crit_);
   void UpdateDelayStatistics(int64_t capture_time_ms, int64_t now_ms);
   void UpdateOnSendPacket(int packet_id,
                           int64_t capture_time_ms,
                           uint32_t ssrc);
 
-  bool UpdateTransportSequenceNumber(RtpPacketToSend* packet,
-                                     int* packet_id) const;
+  bool UpdateTransportSequenceNumber(RtpPacketToSend* packet, int* packet_id)
+      RTC_EXCLUSIVE_LOCKS_REQUIRED(send_critsect_);
 
   void UpdateRtpStats(const RtpPacketToSend& packet,
                       bool is_rtx,
@@ -275,7 +278,7 @@
 
   Transport* transport_;
   bool sending_media_ RTC_GUARDED_BY(send_critsect_);
-
+  bool force_part_of_allocation_ RTC_GUARDED_BY(send_critsect_);
   size_t max_packet_size_;
 
   int8_t last_payload_type_ RTC_GUARDED_BY(send_critsect_);
@@ -297,6 +300,8 @@
   // Statistics
   rtc::CriticalSection statistics_crit_;
   SendDelayMap send_delays_ RTC_GUARDED_BY(statistics_crit_);
+  SendDelayMap::const_iterator max_delay_it_ RTC_GUARDED_BY(statistics_crit_);
+  int64_t sum_delays_ms_ RTC_GUARDED_BY(statistics_crit_);
   FrameCounts frame_counts_ RTC_GUARDED_BY(statistics_crit_);
   StreamDataCounters rtp_stats_ RTC_GUARDED_BY(statistics_crit_);
   StreamDataCounters rtx_rtp_stats_ RTC_GUARDED_BY(statistics_crit_);
@@ -339,11 +344,6 @@
 
   const bool send_side_bwe_with_overhead_;
 
-  const bool unlimited_retransmission_experiment_;
-
-  absl::optional<VideoContentType> video_content_type_
-      RTC_GUARDED_BY(send_critsect_);
-
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RTPSender);
 };
 
diff --git a/modules/rtp_rtcp/source/rtp_sender_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
index d143c87..1b7e992 100644
--- a/modules/rtp_rtcp/source/rtp_sender_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
@@ -21,6 +21,8 @@
 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
 #include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
+#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h"
+#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
@@ -59,10 +61,13 @@
 const size_t kGenericHeaderLength = 1;
 const uint8_t kPayloadData[] = {47, 11, 32, 93, 89};
 const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
+const int kGenericDescriptorId = 10;
 
 using ::testing::_;
+using ::testing::ElementsAre;
 using ::testing::ElementsAreArray;
 using ::testing::Invoke;
+using ::testing::SizeIs;
 
 uint64_t ConvertMsToAbsSendTime(int64_t time_ms) {
   return (((time_ms << 18) + 500) / 1000) & 0x00ffffff;
@@ -84,6 +89,8 @@
     receivers_extensions_.Register(kRtpExtensionVideoTiming,
                                    kVideoTimingExtensionId);
     receivers_extensions_.Register(kRtpExtensionMid, kMidExtensionId);
+    receivers_extensions_.Register(kRtpExtensionGenericFrameDescriptor,
+                                   kGenericDescriptorId);
   }
 
   bool SendRtp(const uint8_t* data,
@@ -133,6 +140,11 @@
   MOCK_METHOD0(AllocateSequenceNumber, uint16_t());
 };
 
+class MockSendSideDelayObserver : public SendSideDelayObserver {
+ public:
+  MOCK_METHOD3(SendSideDelayUpdated, void(int, int, uint32_t));
+};
+
 class MockSendPacketObserver : public SendPacketObserver {
  public:
   MOCK_METHOD3(OnSendPacket, void(uint16_t, int64_t, uint32_t));
@@ -467,6 +479,7 @@
   ASSERT_TRUE(packet.GetExtension<TransportSequenceNumber>(&transport_seq_no));
   EXPECT_EQ(kTransportSequenceNumber, transport_seq_no);
   EXPECT_EQ(transport_.last_options_.packet_id, transport_seq_no);
+  EXPECT_TRUE(transport_.last_options_.included_in_allocation);
 }
 
 TEST_P(RtpSenderTestWithoutPacer, PacketOptionsNoRetransmission) {
@@ -481,8 +494,110 @@
   EXPECT_FALSE(transport_.last_options_.is_retransmit);
 }
 
-TEST_P(RtpSenderTestWithoutPacer, NoAllocationIfNotRegistered) {
+TEST_P(RtpSenderTestWithoutPacer,
+       SetsIncludedInFeedbackWhenTransportSequenceNumberExtensionIsRegistered) {
+  SetUpRtpSender(false, false);
+  rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber,
+                                          kTransportSequenceNumberExtensionId);
+  EXPECT_CALL(seq_num_allocator_, AllocateSequenceNumber())
+      .WillOnce(testing::Return(kTransportSequenceNumber));
+  EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
   SendGenericPayload();
+  EXPECT_TRUE(transport_.last_options_.included_in_feedback);
+}
+
+TEST_P(
+    RtpSenderTestWithoutPacer,
+    SetsIncludedInAllocationWhenTransportSequenceNumberExtensionIsRegistered) {
+  SetUpRtpSender(false, false);
+  rtp_sender_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber,
+                                          kTransportSequenceNumberExtensionId);
+  EXPECT_CALL(seq_num_allocator_, AllocateSequenceNumber())
+      .WillOnce(testing::Return(kTransportSequenceNumber));
+  EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
+  SendGenericPayload();
+  EXPECT_TRUE(transport_.last_options_.included_in_allocation);
+}
+
+TEST_P(RtpSenderTestWithoutPacer,
+       SetsIncludedInAllocationWhenForcedAsPartOfAllocation) {
+  SetUpRtpSender(false, false);
+  rtp_sender_->SetAsPartOfAllocation(true);
+  SendGenericPayload();
+  EXPECT_FALSE(transport_.last_options_.included_in_feedback);
+  EXPECT_TRUE(transport_.last_options_.included_in_allocation);
+}
+
+TEST_P(RtpSenderTestWithoutPacer, DoesnSetIncludedInAllocationByDefault) {
+  SetUpRtpSender(false, false);
+  SendGenericPayload();
+  EXPECT_FALSE(transport_.last_options_.included_in_feedback);
+  EXPECT_FALSE(transport_.last_options_.included_in_allocation);
+}
+
+TEST_P(RtpSenderTestWithoutPacer, OnSendSideDelayUpdated) {
+  testing::StrictMock<MockSendSideDelayObserver> send_side_delay_observer_;
+  rtp_sender_.reset(
+      new RTPSender(false, &fake_clock_, &transport_, nullptr, nullptr, nullptr,
+                    nullptr, nullptr, nullptr, &send_side_delay_observer_,
+                    &mock_rtc_event_log_, nullptr, nullptr, nullptr, false));
+  rtp_sender_->SetSSRC(kSsrc);
+
+  const uint8_t kPayloadType = 127;
+  const uint32_t kCaptureTimeMsToRtpTimestamp = 90;  // 90 kHz clock
+  char payload_name[RTP_PAYLOAD_NAME_SIZE] = "GENERIC";
+  RTPVideoHeader video_header;
+  EXPECT_EQ(0, rtp_sender_->RegisterPayload(payload_name, kPayloadType,
+                                            1000 * kCaptureTimeMsToRtpTimestamp,
+                                            0, 1500));
+
+  // Send packet with 10 ms send-side delay. The average and max should be 10
+  // ms.
+  EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(10, 10, kSsrc))
+      .Times(1);
+  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
+  fake_clock_.AdvanceTimeMilliseconds(10);
+  EXPECT_TRUE(rtp_sender_->SendOutgoingData(
+      kVideoFrameKey, kPayloadType,
+      capture_time_ms * kCaptureTimeMsToRtpTimestamp, capture_time_ms,
+      kPayloadData, sizeof(kPayloadData), nullptr, &video_header, nullptr,
+      kDefaultExpectedRetransmissionTimeMs));
+
+  // Send another packet with 20 ms delay. The average
+  // and max should be 15 and 20 ms respectively.
+  EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(15, 20, kSsrc))
+      .Times(1);
+  fake_clock_.AdvanceTimeMilliseconds(10);
+  EXPECT_TRUE(rtp_sender_->SendOutgoingData(
+      kVideoFrameKey, kPayloadType,
+      capture_time_ms * kCaptureTimeMsToRtpTimestamp, capture_time_ms,
+      kPayloadData, sizeof(kPayloadData), nullptr, &video_header, nullptr,
+      kDefaultExpectedRetransmissionTimeMs));
+
+  // Send another packet at the same time, which replaces the last packet.
+  // Since this packet has 0 ms delay, the average is now 5 ms and max is 10 ms.
+  // TODO(terelius): Is is not clear that this is the right behavior.
+  EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(5, 10, kSsrc))
+      .Times(1);
+  capture_time_ms = fake_clock_.TimeInMilliseconds();
+  EXPECT_TRUE(rtp_sender_->SendOutgoingData(
+      kVideoFrameKey, kPayloadType,
+      capture_time_ms * kCaptureTimeMsToRtpTimestamp, capture_time_ms,
+      kPayloadData, sizeof(kPayloadData), nullptr, &video_header, nullptr,
+      kDefaultExpectedRetransmissionTimeMs));
+
+  // Send a packet 1 second later. The earlier packets should have timed
+  // out, so both max and average should be the delay of this packet.
+  fake_clock_.AdvanceTimeMilliseconds(1000);
+  capture_time_ms = fake_clock_.TimeInMilliseconds();
+  fake_clock_.AdvanceTimeMilliseconds(1);
+  EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(1, 1, kSsrc))
+      .Times(1);
+  EXPECT_TRUE(rtp_sender_->SendOutgoingData(
+      kVideoFrameKey, kPayloadType,
+      capture_time_ms * kCaptureTimeMsToRtpTimestamp, capture_time_ms,
+      kPayloadData, sizeof(kPayloadData), nullptr, &video_header, nullptr,
+      kDefaultExpectedRetransmissionTimeMs));
 }
 
 TEST_P(RtpSenderTestWithoutPacer, OnSendPacketUpdated) {
@@ -1842,7 +1957,8 @@
 TEST_P(RtpSenderVideoTest, RetransmissionTypesVP8BaseLayer) {
   RTPVideoHeader header;
   header.codec = kVideoCodecVP8;
-  header.vp8().temporalIdx = 0;
+  auto& vp8_header = header.video_type_header.emplace<RTPVideoHeaderVP8>();
+  vp8_header.temporalIdx = 0;
 
   EXPECT_EQ(kDontRetransmit,
             rtp_sender_video_->GetStorageType(
@@ -1874,8 +1990,9 @@
   RTPVideoHeader header;
   header.codec = kVideoCodecVP8;
 
+  auto& vp8_header = header.video_type_header.emplace<RTPVideoHeaderVP8>();
   for (int tid = 1; tid <= kMaxTemporalStreams; ++tid) {
-    header.vp8().temporalIdx = tid;
+    vp8_header.temporalIdx = tid;
 
     EXPECT_EQ(kDontRetransmit, rtp_sender_video_->GetStorageType(
                                    header, kRetransmitOff,
@@ -1938,8 +2055,9 @@
       (RTPSenderVideo::kTLRateWindowSizeMs + (kFrameIntervalMs / 2)) /
       kFrameIntervalMs;
   constexpr int kPattern[] = {0, 2, 1, 2};
+  auto& vp8_header = header.video_type_header.emplace<RTPVideoHeaderVP8>();
   for (size_t i = 0; i < arraysize(kPattern) * kNumRepetitions; ++i) {
-    header.vp8().temporalIdx = kPattern[i % arraysize(kPattern)];
+    vp8_header.temporalIdx = kPattern[i % arraysize(kPattern)];
     rtp_sender_video_->GetStorageType(header, kSettings, kRttMs);
     fake_clock_.AdvanceTimeMilliseconds(kFrameIntervalMs);
   }
@@ -1948,7 +2066,7 @@
   // right now. We will wait at most one expected retransmission time before
   // acknowledging that it did not arrive, which means this frame and the next
   // will not be retransmitted.
-  header.vp8().temporalIdx = 1;
+  vp8_header.temporalIdx = 1;
   EXPECT_EQ(StorageType::kDontRetransmit,
             rtp_sender_video_->GetStorageType(header, kSettings, kRttMs));
   fake_clock_.AdvanceTimeMilliseconds(kFrameIntervalMs);
@@ -1964,7 +2082,7 @@
   // Insert a frame for TL2. We just had frame in TL1, so the next one there is
   // in three frames away. TL0 is still too far in the past. So, allow
   // retransmission.
-  header.vp8().temporalIdx = 2;
+  vp8_header.temporalIdx = 2;
   EXPECT_EQ(StorageType::kAllowRetransmission,
             rtp_sender_video_->GetStorageType(header, kSettings, kRttMs));
   fake_clock_.AdvanceTimeMilliseconds(kFrameIntervalMs);
@@ -1995,8 +2113,9 @@
       (RTPSenderVideo::kTLRateWindowSizeMs + (kFrameIntervalMs / 2)) /
       kFrameIntervalMs;
   constexpr int kPattern[] = {0, 2, 2, 2};
+  auto& vp8_header = header.video_type_header.emplace<RTPVideoHeaderVP8>();
   for (size_t i = 0; i < arraysize(kPattern) * kNumRepetitions; ++i) {
-    header.vp8().temporalIdx = kPattern[i % arraysize(kPattern)];
+    vp8_header.temporalIdx = kPattern[i % arraysize(kPattern)];
 
     rtp_sender_video_->GetStorageType(header, kSettings, kRttMs);
     fake_clock_.AdvanceTimeMilliseconds(kFrameIntervalMs);
@@ -2007,11 +2126,67 @@
   // we don't store for retransmission because we expect a frame in a lower
   // layer, but that last frame in TL1 was a long time ago in absolute terms,
   // so allow retransmission anyway.
-  header.vp8().temporalIdx = 1;
+  vp8_header.temporalIdx = 1;
   EXPECT_EQ(StorageType::kAllowRetransmission,
             rtp_sender_video_->GetStorageType(header, kSettings, kRttMs));
 }
 
+TEST_P(RtpSenderVideoTest, PopulateGenericFrameDescriptor) {
+  const int64_t kFrameId = 100000;
+  uint8_t kFrame[100];
+  EXPECT_EQ(0, rtp_sender_->RegisterRtpHeaderExtension(
+                   kRtpExtensionGenericFrameDescriptor, kGenericDescriptorId));
+
+  RTPVideoHeader hdr;
+  RTPVideoHeader::GenericDescriptorInfo& generic = hdr.generic.emplace();
+  generic.frame_id = kFrameId;
+  generic.temporal_index = 3;
+  generic.spatial_index = 2;
+  generic.higher_spatial_layers.push_back(4);
+  generic.dependencies.push_back(kFrameId - 1);
+  generic.dependencies.push_back(kFrameId - 500);
+  rtp_sender_video_->SendVideo(kVideoCodecGeneric, kVideoFrameDelta, kPayload,
+                               kTimestamp, 0, kFrame, sizeof(kFrame), nullptr,
+                               &hdr, kDefaultExpectedRetransmissionTimeMs);
+
+  RtpGenericFrameDescriptor descriptor_wire;
+  EXPECT_EQ(1U, transport_.sent_packets_.size());
+  EXPECT_TRUE(
+      transport_.last_sent_packet()
+          .GetExtension<RtpGenericFrameDescriptorExtension>(&descriptor_wire));
+  EXPECT_EQ(static_cast<uint16_t>(generic.frame_id), descriptor_wire.FrameId());
+  EXPECT_EQ(generic.temporal_index, descriptor_wire.TemporalLayer());
+  EXPECT_THAT(descriptor_wire.FrameDependenciesDiffs(), ElementsAre(1, 500));
+  uint8_t spatial_bitmask = 0x14;
+  EXPECT_EQ(spatial_bitmask, descriptor_wire.SpatialLayersBitmask());
+}
+
+TEST_P(RtpSenderVideoTest,
+       UsesMinimalVp8DescriptorWhenGenericFrameDescriptorExtensionIsUsed) {
+  const int64_t kFrameId = 100000;
+  const size_t kFrameSize = 100;
+  uint8_t kFrame[kFrameSize];
+  ASSERT_TRUE(rtp_sender_->RegisterRtpHeaderExtension(
+      RtpGenericFrameDescriptorExtension::kUri, kGenericDescriptorId));
+
+  RTPVideoHeader hdr;
+  hdr.codec = kVideoCodecVP8;
+  RTPVideoHeaderVP8& vp8 = hdr.video_type_header.emplace<RTPVideoHeaderVP8>();
+  vp8.pictureId = kFrameId % 0X7FFF;
+  vp8.tl0PicIdx = 13;
+  vp8.temporalIdx = 1;
+  vp8.keyIdx = 2;
+  RTPVideoHeader::GenericDescriptorInfo& generic = hdr.generic.emplace();
+  generic.frame_id = kFrameId;
+  rtp_sender_video_->SendVideo(kVideoCodecVP8, kVideoFrameDelta, kPayload,
+                               kTimestamp, 0, kFrame, sizeof(kFrame), nullptr,
+                               &hdr, kDefaultExpectedRetransmissionTimeMs);
+
+  ASSERT_THAT(transport_.sent_packets_, SizeIs(1));
+  // Expect only minimal 1-byte vp8 descriptor was generated.
+  EXPECT_THAT(transport_.sent_packets_[0].payload_size(), 1 + kFrameSize);
+}
+
 TEST_P(RtpSenderTest, OnOverheadChanged) {
   MockOverheadObserver mock_overhead_observer;
   rtp_sender_.reset(new RTPSender(
diff --git a/modules/rtp_rtcp/source/rtp_sender_video.cc b/modules/rtp_rtcp/source/rtp_sender_video.cc
index bf8150d..e8f0ea5 100644
--- a/modules/rtp_rtcp/source/rtp_sender_video.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_video.cc
@@ -24,6 +24,7 @@
 #include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
 #include "modules/rtp_rtcp/source/rtp_format_vp8.h"
 #include "modules/rtp_rtcp/source/rtp_format_vp9.h"
+#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
 #include "rtc_base/checks.h"
@@ -47,6 +48,69 @@
   memcpy(&red_payload[kRedForFecHeaderLength], media_payload.data(),
          media_payload.size());
 }
+
+void AddRtpHeaderExtensions(const RTPVideoHeader& video_header,
+                            FrameType frame_type,
+                            bool set_video_rotation,
+                            bool first_packet,
+                            bool last_packet,
+                            RtpPacketToSend* packet) {
+  if (last_packet && set_video_rotation)
+    packet->SetExtension<VideoOrientation>(video_header.rotation);
+
+  // Report content type only for key frames.
+  if (last_packet && frame_type == kVideoFrameKey &&
+      video_header.content_type != VideoContentType::UNSPECIFIED)
+    packet->SetExtension<VideoContentTypeExtension>(video_header.content_type);
+
+  if (last_packet &&
+      video_header.video_timing.flags != VideoSendTiming::kInvalid)
+    packet->SetExtension<VideoTimingExtension>(video_header.video_timing);
+
+  if (video_header.generic) {
+    RtpGenericFrameDescriptor generic_descriptor;
+    generic_descriptor.SetFirstPacketInSubFrame(first_packet);
+    generic_descriptor.SetLastPacketInSubFrame(last_packet);
+    generic_descriptor.SetFirstSubFrameInFrame(true);
+    generic_descriptor.SetLastSubFrameInFrame(true);
+
+    if (first_packet) {
+      generic_descriptor.SetFrameId(
+          static_cast<uint16_t>(video_header.generic->frame_id));
+      for (int64_t dep : video_header.generic->dependencies) {
+        generic_descriptor.AddFrameDependencyDiff(
+            video_header.generic->frame_id - dep);
+      }
+
+      uint8_t spatial_bimask = 1 << video_header.generic->spatial_index;
+      for (int layer : video_header.generic->higher_spatial_layers) {
+        RTC_DCHECK_GT(layer, video_header.generic->spatial_index);
+        RTC_DCHECK_LT(layer, 8);
+        spatial_bimask |= 1 << layer;
+      }
+      generic_descriptor.SetSpatialLayersBitmask(spatial_bimask);
+
+      generic_descriptor.SetTemporalLayer(video_header.generic->temporal_index);
+    }
+    packet->SetExtension<RtpGenericFrameDescriptorExtension>(
+        generic_descriptor);
+  }
+}
+
+bool MinimizeDescriptor(const RTPVideoHeader& full, RTPVideoHeader* minimized) {
+  if (full.codec == VideoCodecType::kVideoCodecVP8) {
+    minimized->codec = VideoCodecType::kVideoCodecVP8;
+    const auto& vp8 = absl::get<RTPVideoHeaderVP8>(full.video_type_header);
+    // Set minimum fields the RtpPacketizer is using to create vp8 packets.
+    auto& min_vp8 = minimized->video_type_header.emplace<RTPVideoHeaderVP8>();
+    min_vp8.InitRTPVideoHeaderVP8();
+    min_vp8.nonReference = vp8.nonReference;
+    return true;
+  }
+  // TODO(danilchap): Reduce vp9 codec specific descriptor too.
+  return false;
+}
+
 }  // namespace
 
 RTPSenderVideo::RTPSenderVideo(Clock* clock,
@@ -282,16 +346,10 @@
     return false;
   RTC_CHECK(video_header);
 
-  // Create header that will be reused in all packets.
-  std::unique_ptr<RtpPacketToSend> rtp_header = rtp_sender_->AllocatePacket();
-  rtp_header->SetPayloadType(payload_type);
-  rtp_header->SetTimestamp(rtp_timestamp);
-  rtp_header->set_capture_time_ms(capture_time_ms);
-  auto last_packet = absl::make_unique<RtpPacketToSend>(*rtp_header);
-
   size_t fec_packet_overhead;
   bool red_enabled;
   int32_t retransmission_settings;
+  bool set_video_rotation;
   {
     rtc::CritScope cs(&crit_);
     // According to
@@ -306,21 +364,10 @@
     // value sent.
     // Set rotation when key frame or when changed (to follow standard).
     // Or when different from 0 (to follow current receiver implementation).
-    VideoRotation current_rotation = video_header->rotation;
-    if (frame_type == kVideoFrameKey || current_rotation != last_rotation_ ||
-        current_rotation != kVideoRotation_0)
-      last_packet->SetExtension<VideoOrientation>(current_rotation);
-    last_rotation_ = current_rotation;
-    // Report content type only for key frames.
-    if (frame_type == kVideoFrameKey &&
-        video_header->content_type != VideoContentType::UNSPECIFIED) {
-      last_packet->SetExtension<VideoContentTypeExtension>(
-          video_header->content_type);
-    }
-    if (video_header->video_timing.flags != VideoSendTiming::kInvalid) {
-      last_packet->SetExtension<VideoTimingExtension>(
-          video_header->video_timing);
-    }
+    set_video_rotation = frame_type == kVideoFrameKey ||
+                         video_header->rotation != last_rotation_ ||
+                         video_header->rotation != kVideoRotation_0;
+    last_rotation_ = video_header->rotation;
 
     // FEC settings.
     const FecProtectionParams& fec_params =
@@ -335,23 +382,56 @@
     retransmission_settings = retransmission_settings_;
   }
 
-  size_t packet_capacity = rtp_sender_->MaxRtpPacketSize() -
-                           fec_packet_overhead -
-                           (rtp_sender_->RtxStatus() ? kRtxHeaderSize : 0);
-  RTC_DCHECK_LE(packet_capacity, rtp_header->capacity());
-  RTC_DCHECK_GT(packet_capacity, rtp_header->headers_size());
-  RTC_DCHECK_GT(packet_capacity, last_packet->headers_size());
-  size_t max_data_payload_length = packet_capacity - rtp_header->headers_size();
-  RTC_DCHECK_GE(last_packet->headers_size(), rtp_header->headers_size());
-  size_t last_packet_reduction_len =
-      last_packet->headers_size() - rtp_header->headers_size();
+  // Maximum size of packet including rtp headers.
+  // Extra space left in case packet will be resent using fec or rtx.
+  int packet_capacity = rtp_sender_->MaxRtpPacketSize() - fec_packet_overhead -
+                        (rtp_sender_->RtxStatus() ? kRtxHeaderSize : 0);
 
+  auto create_packet = [&] {
+    std::unique_ptr<RtpPacketToSend> rtp_packet = rtp_sender_->AllocatePacket();
+    RTC_DCHECK_LE(packet_capacity, rtp_packet->capacity());
+
+    rtp_packet->SetPayloadType(payload_type);
+    rtp_packet->SetTimestamp(rtp_timestamp);
+    rtp_packet->set_capture_time_ms(capture_time_ms);
+    return rtp_packet;
+  };
+
+  auto first_packet = create_packet();
+  auto middle_packet = absl::make_unique<RtpPacketToSend>(*first_packet);
+  auto last_packet = absl::make_unique<RtpPacketToSend>(*first_packet);
+  // Simplest way to estimate how much extensions would occupy is to set them.
+  AddRtpHeaderExtensions(*video_header, frame_type, set_video_rotation,
+                         /*first=*/true, /*last=*/false, first_packet.get());
+  AddRtpHeaderExtensions(*video_header, frame_type, set_video_rotation,
+                         /*first=*/false, /*last=*/false, middle_packet.get());
+  AddRtpHeaderExtensions(*video_header, frame_type, set_video_rotation,
+                         /*first=*/false, /*last=*/true, last_packet.get());
+
+  RTC_DCHECK_GT(packet_capacity, first_packet->headers_size());
+  RTC_DCHECK_GT(packet_capacity, middle_packet->headers_size());
+  RTC_DCHECK_GT(packet_capacity, last_packet->headers_size());
   RtpPacketizer::PayloadSizeLimits limits;
-  limits.max_payload_len = max_data_payload_length;
-  limits.last_packet_reduction_len = last_packet_reduction_len;
+  limits.max_payload_len = packet_capacity - middle_packet->headers_size();
+
+  RTC_DCHECK_GE(first_packet->headers_size(), middle_packet->headers_size());
+  limits.first_packet_reduction_len =
+      first_packet->headers_size() - middle_packet->headers_size();
+
+  RTC_DCHECK_GE(last_packet->headers_size(), middle_packet->headers_size());
+  limits.last_packet_reduction_len =
+      last_packet->headers_size() - middle_packet->headers_size();
+
+  RTPVideoHeader minimized_video_header;
+  const RTPVideoHeader* packetize_video_header = video_header;
+  if (first_packet->HasExtension<RtpGenericFrameDescriptorExtension>() &&
+      MinimizeDescriptor(*video_header, &minimized_video_header)) {
+    packetize_video_header = &minimized_video_header;
+  }
+
   std::unique_ptr<RtpPacketizer> packetizer = RtpPacketizer::Create(
       video_type, rtc::MakeArrayView(payload_data, payload_size), limits,
-      *video_header, frame_type, fragmentation);
+      *packetize_video_header, frame_type, fragmentation);
 
   const uint8_t temporal_id = GetTemporalId(*video_header);
   StorageType storage = GetStorageType(temporal_id, retransmission_settings,
@@ -363,14 +443,36 @@
 
   bool first_frame = first_frame_sent_();
   for (size_t i = 0; i < num_packets; ++i) {
-    bool last = (i + 1) == num_packets;
-    auto packet = last ? std::move(last_packet)
-                       : absl::make_unique<RtpPacketToSend>(*rtp_header);
+    std::unique_ptr<RtpPacketToSend> packet;
+    int expected_payload_capacity;
+    // Choose right packet template:
+    if (num_packets == 1) {
+      // No prepared template, create a new packet.
+      packet = create_packet();
+      AddRtpHeaderExtensions(*video_header, frame_type, set_video_rotation,
+                             /*first=*/true, /*last=*/true, packet.get());
+      // TODO(bugs.webrtc.org/7990): Revisit this case when two byte header
+      // extension are implemented because then single packet might need more
+      // space for extensions than sum of first and last packet reductions.
+      expected_payload_capacity = limits.max_payload_len -
+                                  limits.first_packet_reduction_len -
+                                  limits.last_packet_reduction_len;
+    } else if (i == 0) {
+      packet = std::move(first_packet);
+      expected_payload_capacity =
+          limits.max_payload_len - limits.first_packet_reduction_len;
+    } else if (i == num_packets - 1) {
+      packet = std::move(last_packet);
+      expected_payload_capacity =
+          limits.max_payload_len - limits.last_packet_reduction_len;
+    } else {
+      packet = absl::make_unique<RtpPacketToSend>(*middle_packet);
+      expected_payload_capacity = limits.max_payload_len;
+    }
+
     if (!packetizer->NextPacket(packet.get()))
       return false;
-    RTC_DCHECK_LE(packet->payload_size(),
-                  last ? max_data_payload_length - last_packet_reduction_len
-                       : max_data_payload_length);
+    RTC_DCHECK_LE(packet->payload_size(), expected_payload_capacity);
     if (!rtp_sender_->AssignSequenceNumber(packet.get()))
       return false;
 
@@ -405,7 +507,7 @@
         RTC_LOG(LS_INFO)
             << "Sent first RTP packet of the first video frame (pre-pacer)";
       }
-      if (last) {
+      if (i == num_packets - 1) {
         RTC_LOG(LS_INFO)
             << "Sent last RTP packet of the first video frame (pre-pacer)";
       }
@@ -467,15 +569,15 @@
 }
 
 uint8_t RTPSenderVideo::GetTemporalId(const RTPVideoHeader& header) {
-  switch (header.codec) {
-    case kVideoCodecVP8:
-      return header.vp8().temporalIdx;
-    case kVideoCodecVP9:
-      return absl::get<RTPVideoHeaderVP9>(header.video_type_header)
-          .temporal_idx;
-    default:
-      return kNoTemporalIdx;
-  }
+  struct TemporalIdGetter {
+    uint8_t operator()(const RTPVideoHeaderVP8& vp8) { return vp8.temporalIdx; }
+    uint8_t operator()(const RTPVideoHeaderVP9& vp9) {
+      return vp9.temporal_idx;
+    }
+    uint8_t operator()(const RTPVideoHeaderH264&) { return kNoTemporalIdx; }
+    uint8_t operator()(const absl::monostate&) { return kNoTemporalIdx; }
+  };
+  return absl::visit(TemporalIdGetter(), header.video_type_header);
 }
 
 bool RTPSenderVideo::UpdateConditionalRetransmit(
diff --git a/modules/rtp_rtcp/source/rtp_utility.cc b/modules/rtp_rtcp/source/rtp_utility.cc
index d150ce2..228572f 100644
--- a/modules/rtp_rtcp/source/rtp_utility.cc
+++ b/modules/rtp_rtcp/source/rtp_utility.cc
@@ -197,7 +197,9 @@
   header->timestamp = RTPTimestamp;
   header->ssrc = SSRC;
   header->numCSRCs = CC;
-  header->paddingLength = P ? *(_ptrRTPDataEnd - 1) : 0;
+  if (!P) {
+    header->paddingLength = 0;
+  }
 
   for (uint8_t i = 0; i < CC; ++i) {
     uint32_t CSRC = ByteReader<uint32_t>::ReadBigEndian(ptr);
@@ -236,6 +238,10 @@
   header->extension.has_video_timing = false;
   header->extension.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false};
 
+  header->extension.has_frame_marking = false;
+  header->extension.frame_marking = {false, false, false, false, false,
+                                     kNoTemporalIdx, 0, 0};
+
   if (X) {
     /* RTP header extension, RFC 3550.
      0                   1                   2                   3
@@ -272,6 +278,21 @@
     }
     header->headerLength += XLen;
   }
+  if (header->headerLength > static_cast<size_t>(length))
+    return false;
+
+  if (P) {
+    // Packet has padding.
+    if (header->headerLength != static_cast<size_t>(length)) {
+      // Packet is not header only. We can parse padding length now.
+      header->paddingLength = *(_ptrRTPDataEnd - 1);
+    } else {
+      RTC_LOG(LS_WARNING) << "Cannot parse padding length.";
+      // Packet is header only. We have no clue of the padding length.
+      return false;
+    }
+  }
+
   if (header->headerLength + header->paddingLength >
       static_cast<size_t>(length))
     return false;
@@ -454,6 +475,15 @@
                                       &header->extension.video_timing);
           break;
         }
+        case kRtpExtensionFrameMarking: {
+          if (!FrameMarkingExtension::Parse(rtc::MakeArrayView(ptr, len + 1),
+              &header->extension.frame_marking)) {
+            RTC_LOG(LS_WARNING) << "Incorrect frame marking len: " << len;
+            return;
+          }
+          header->extension.has_frame_marking = true;
+          break;
+        }
         case kRtpExtensionRtpStreamId: {
           header->extension.stream_id.Set(rtc::MakeArrayView(ptr, len + 1));
           break;
diff --git a/modules/rtp_rtcp/source/rtp_video_header.h b/modules/rtp_rtcp/source/rtp_video_header.h
index 520f4d4..288b7d0 100644
--- a/modules/rtp_rtcp/source/rtp_video_header.h
+++ b/modules/rtp_rtcp/source/rtp_video_header.h
@@ -13,6 +13,7 @@
 #include "absl/container/inlined_vector.h"
 #include "absl/types/variant.h"
 #include "api/video/video_content_type.h"
+#include "api/video/video_frame_marking.h"
 #include "api/video/video_rotation.h"
 #include "api/video/video_timing.h"
 #include "common_types.h"  // NOLINT(build/include)
@@ -21,8 +22,10 @@
 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
 
 namespace webrtc {
-using RTPVideoTypeHeader =
-    absl::variant<RTPVideoHeaderVP8, RTPVideoHeaderVP9, RTPVideoHeaderH264>;
+using RTPVideoTypeHeader = absl::variant<absl::monostate,
+                                         RTPVideoHeaderVP8,
+                                         RTPVideoHeaderVP9,
+                                         RTPVideoHeaderH264>;
 
 struct RTPVideoHeader {
   struct GenericDescriptorInfo {
@@ -42,21 +45,6 @@
 
   ~RTPVideoHeader();
 
-  // TODO(philipel): Remove when downstream projects have been updated.
-  RTPVideoHeaderVP8& vp8() {
-    if (!absl::holds_alternative<RTPVideoHeaderVP8>(video_type_header))
-      video_type_header.emplace<RTPVideoHeaderVP8>();
-
-    return absl::get<RTPVideoHeaderVP8>(video_type_header);
-  }
-  // TODO(philipel): Remove when downstream projects have been updated.
-  const RTPVideoHeaderVP8& vp8() const {
-    if (!absl::holds_alternative<RTPVideoHeaderVP8>(video_type_header))
-      video_type_header.emplace<RTPVideoHeaderVP8>();
-
-    return absl::get<RTPVideoHeaderVP8>(video_type_header);
-  }
-
   absl::optional<GenericDescriptorInfo> generic;
 
   uint16_t width = 0;
@@ -64,13 +52,14 @@
   VideoRotation rotation = VideoRotation::kVideoRotation_0;
   VideoContentType content_type = VideoContentType::UNSPECIFIED;
   bool is_first_packet_in_frame = false;
+  bool is_last_packet_in_frame = false;
   uint8_t simulcastIdx = 0;
   VideoCodecType codec = VideoCodecType::kVideoCodecGeneric;
 
   PlayoutDelay playout_delay;
   VideoSendTiming video_timing;
-  // TODO(philipel): remove mutable when downstream projects have been updated.
-  mutable RTPVideoTypeHeader video_type_header;
+  FrameMarking frame_marking;
+  RTPVideoTypeHeader video_type_header;
 };
 
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/ulpfec_receiver_impl.cc b/modules/rtp_rtcp/source/ulpfec_receiver_impl.cc
index 3afd612..eb09c95 100644
--- a/modules/rtp_rtcp/source/ulpfec_receiver_impl.cc
+++ b/modules/rtp_rtcp/source/ulpfec_receiver_impl.cc
@@ -14,7 +14,6 @@
 #include <utility>
 
 #include "modules/rtp_rtcp/source/byte_io.h"
-#include "modules/rtp_rtcp/source/rtp_receiver_video.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "system_wrappers/include/clock.h"
diff --git a/modules/video_coding/codecs/vp8/include/temporal_layers_checker.h b/modules/video_coding/codecs/vp8/include/temporal_layers_checker.h
new file mode 100644
index 0000000..9878ac9
--- /dev/null
+++ b/modules/video_coding/codecs/vp8/include/temporal_layers_checker.h
@@ -0,0 +1,63 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_TEMPORAL_LAYERS_CHECKER_H_
+#define MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_TEMPORAL_LAYERS_CHECKER_H_
+
+#include <stdint.h>
+
+#include <memory>
+
+#include "modules/video_coding/codecs/vp8/include/vp8_temporal_layers.h"
+
+namespace webrtc {
+
+// Interface for a class that verifies correctness of temporal layer
+// configurations (dependencies, sync flag, etc).
+// Intended to be used in tests as well as with real apps in debug mode.
+class TemporalLayersChecker {
+ public:
+  explicit TemporalLayersChecker(int num_temporal_layers);
+  virtual ~TemporalLayersChecker() {}
+
+  virtual bool CheckTemporalConfig(
+      bool frame_is_keyframe,
+      const TemporalLayers::FrameConfig& frame_config);
+
+  static std::unique_ptr<TemporalLayersChecker> CreateTemporalLayersChecker(
+      TemporalLayersType type,
+      int num_temporal_layers);
+
+ private:
+  struct BufferState {
+    BufferState() : is_keyframe(true), temporal_layer(0), sequence_number(0) {}
+    bool is_keyframe;
+    uint8_t temporal_layer;
+    uint32_t sequence_number;
+  };
+  bool CheckAndUpdateBufferState(BufferState* state,
+                                 bool* need_sync,
+                                 bool frame_is_keyframe,
+                                 uint8_t temporal_layer,
+                                 webrtc::TemporalLayers::BufferFlags flags,
+                                 uint32_t sequence_number,
+                                 uint32_t* lowest_sequence_referenced);
+  BufferState last_;
+  BufferState arf_;
+  BufferState golden_;
+  int num_temporal_layers_;
+  uint32_t sequence_number_;
+  uint32_t last_sync_sequence_number_;
+  uint32_t last_tl0_sequence_number_;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_TEMPORAL_LAYERS_CHECKER_H_
diff --git a/modules/video_coding/codecs/vp8/include/vp8.h b/modules/video_coding/codecs/vp8/include/vp8.h
index 57be521..5a6f1e0 100644
--- a/modules/video_coding/codecs/vp8/include/vp8.h
+++ b/modules/video_coding/codecs/vp8/include/vp8.h
@@ -19,18 +19,14 @@
 
 namespace webrtc {
 
-class VP8Encoder : public VideoEncoder {
+class VP8Encoder {
  public:
-  static std::unique_ptr<VP8Encoder> Create();
-
-  ~VP8Encoder() override {}
+  static std::unique_ptr<VideoEncoder> Create();
 };  // end of VP8Encoder class
 
-class VP8Decoder : public VideoDecoder {
+class VP8Decoder {
  public:
-  static std::unique_ptr<VP8Decoder> Create();
-
-  ~VP8Decoder() override {}
+  static std::unique_ptr<VideoDecoder> Create();
 };  // end of VP8Decoder class
 }  // namespace webrtc
 
diff --git a/modules/video_coding/codecs/vp8/include/vp8_temporal_layers.h b/modules/video_coding/codecs/vp8/include/vp8_temporal_layers.h
new file mode 100644
index 0000000..b5dbc4e
--- /dev/null
+++ b/modules/video_coding/codecs/vp8/include/vp8_temporal_layers.h
@@ -0,0 +1,198 @@
+/*
+ *  Copyright (c) 2018 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 MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_VP8_TEMPORAL_LAYERS_H_
+#define MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_VP8_TEMPORAL_LAYERS_H_
+
+#include <memory>
+#include <vector>
+
+namespace webrtc {
+
+// Some notes on the prerequisites of the TemporalLayers interface.
+// * Implementations of TemporalLayers may not contain internal synchronization
+//   so caller must make sure doing so thread safe.
+// * The encoder is assumed to encode all frames in order, and callbacks to
+//   PopulateCodecSpecific() / FrameEncoded() must happen in the same order.
+//
+// This means that in the case of pipelining encoders, it is OK to have a chain
+// of calls such as this:
+// - UpdateLayerConfig(timestampA)
+// - UpdateLayerConfig(timestampB)
+// - PopulateCodecSpecific(timestampA, ...)
+// - UpdateLayerConfig(timestampC)
+// - OnEncodeDone(timestampA, 1234, ...)
+// - UpdateLayerConfig(timestampC)
+// - OnEncodeDone(timestampB, 0, ...)
+// - OnEncodeDone(timestampC, 1234, ...)
+// Note that UpdateLayerConfig() for a new frame can happen before
+// FrameEncoded() for a previous one, but calls themselves must be both
+// synchronized (e.g. run on a task queue) and in order (per type).
+
+enum class TemporalLayersType { kFixedPattern, kBitrateDynamic };
+
+struct CodecSpecificInfoVP8;
+enum class Vp8BufferReference : uint8_t {
+  kNone = 0,
+  kLast = 1,
+  kGolden = 2,
+  kAltref = 4
+};
+
+struct Vp8EncoderConfig {
+  static constexpr size_t kMaxPeriodicity = 16;
+  static constexpr size_t kMaxLayers = 5;
+
+  // Number of active temporal layers. Set to 0 if not used.
+  uint32_t ts_number_layers;
+  // Arrays of length |ts_number_layers|, indicating (cumulative) target bitrate
+  // and rate decimator (e.g. 4 if every 4th frame is in the given layer) for
+  // each active temporal layer, starting with temporal id 0.
+  uint32_t ts_target_bitrate[kMaxLayers];
+  uint32_t ts_rate_decimator[kMaxLayers];
+
+  // The periodicity of the temporal pattern. Set to 0 if not used.
+  uint32_t ts_periodicity;
+  // Array of length |ts_periodicity| indicating the sequence of temporal id's
+  // to assign to incoming frames.
+  uint32_t ts_layer_id[kMaxPeriodicity];
+
+  // Target bitrate, in bps.
+  uint32_t rc_target_bitrate;
+
+  // Clamp QP to min/max. Use 0 to disable clamping.
+  uint32_t rc_min_quantizer;
+  uint32_t rc_max_quantizer;
+};
+
+// This interface defines a way of getting the encoder settings needed to
+// realize a temporal layer structure of predefined size.
+class TemporalLayers {
+ public:
+  enum BufferFlags : int {
+    kNone = 0,
+    kReference = 1,
+    kUpdate = 2,
+    kReferenceAndUpdate = kReference | kUpdate,
+  };
+  enum FreezeEntropy { kFreezeEntropy };
+
+  struct FrameConfig {
+    FrameConfig();
+
+    FrameConfig(BufferFlags last, BufferFlags golden, BufferFlags arf);
+    FrameConfig(BufferFlags last,
+                BufferFlags golden,
+                BufferFlags arf,
+                FreezeEntropy);
+
+    bool drop_frame;
+    BufferFlags last_buffer_flags;
+    BufferFlags golden_buffer_flags;
+    BufferFlags arf_buffer_flags;
+
+    // The encoder layer ID is used to utilize the correct bitrate allocator
+    // inside the encoder. It does not control references nor determine which
+    // "actual" temporal layer this is. The packetizer temporal index determines
+    // which layer the encoded frame should be packetized into.
+    // Normally these are the same, but current temporal-layer strategies for
+    // screenshare use one bitrate allocator for all layers, but attempt to
+    // packetize / utilize references to split a stream into multiple layers,
+    // with different quantizer settings, to hit target bitrate.
+    // TODO(pbos): Screenshare layers are being reconsidered at the time of
+    // writing, we might be able to remove this distinction, and have a temporal
+    // layer imply both (the normal case).
+    int encoder_layer_id;
+    int packetizer_temporal_idx;
+
+    bool layer_sync;
+
+    bool freeze_entropy;
+
+    // Indicates in which order the encoder should search the reference buffers
+    // when doing motion prediction. Set to kNone to use unspecified order. Any
+    // buffer indicated here must not have the corresponding no_ref bit set.
+    // If all three buffers can be reference, the one not listed here should be
+    // searched last.
+    Vp8BufferReference first_reference;
+    Vp8BufferReference second_reference;
+
+    bool operator==(const FrameConfig& o) const;
+    bool operator!=(const FrameConfig& o) const { return !(*this == o); }
+
+   private:
+    FrameConfig(BufferFlags last,
+                BufferFlags golden,
+                BufferFlags arf,
+                bool freeze_entropy);
+  };
+
+  // Factory for TemporalLayer strategy. Default behavior is a fixed pattern
+  // of temporal layers. See default_temporal_layers.cc
+  static std::unique_ptr<TemporalLayers> CreateTemporalLayers(
+      TemporalLayersType type,
+      int num_temporal_layers);
+
+  virtual ~TemporalLayers() = default;
+
+  // If this method returns true, the encoder is free to drop frames for
+  // instance in an effort to uphold encoding bitrate.
+  // If this return false, the encoder must not drop any frames unless:
+  //  1. Requested to do so via FrameConfig.drop_frame
+  //  2. The frame to be encoded is requested to be a keyframe
+  //  3. The encoded detected a large overshoot and decided to drop and then
+  //     re-encode the image at a low bitrate. In this case the encoder should
+  //     call OnEncodeDone() once with size = 0 to indicate drop, and then call
+  //     OnEncodeDone() again when the frame has actually been encoded.
+  virtual bool SupportsEncoderFrameDropping() const = 0;
+
+  // New target bitrate, per temporal layer.
+  virtual void OnRatesUpdated(const std::vector<uint32_t>& bitrates_bps,
+                              int framerate_fps) = 0;
+
+  // Called by the encoder before encoding a frame. |cfg| contains the current
+  // configuration. If the TemporalLayers instance wishes any part of that
+  // to be changed before the encode step, |cfg| should be changed and then
+  // return true. If false is returned, the encoder will proceed without
+  // updating the configuration.
+  virtual bool UpdateConfiguration(Vp8EncoderConfig* cfg) = 0;
+
+  // Returns the recommended VP8 encode flags needed, and moves the temporal
+  // pattern to the next frame.
+  // The timestamp may be used as both a time and a unique identifier, and so
+  // the caller must make sure no two frames use the same timestamp.
+  // The timestamp uses a 90kHz RTP clock.
+  // After calling this method, first call the actual encoder with the provided
+  // frame configuration, and then OnEncodeDone() below.
+  virtual FrameConfig UpdateLayerConfig(uint32_t rtp_timestamp) = 0;
+
+  // Called after the encode step is done. |rtp_timestamp| must match the
+  // parameter use in the UpdateLayerConfig() call.
+  // |is_keyframe| must be true iff the encoder decided to encode this frame as
+  // a keyframe.
+  // If the encoder decided to drop this frame, |size_bytes| must be set to 0,
+  // otherwise it should indicate the size in bytes of the encoded frame.
+  // If |size_bytes| > 0, and |vp8_info| is not null, the TemporalLayers
+  // instance my update |vp8_info| with codec specific data such as temporal id.
+  // Some fields of this struct may have already been populated by the encoder,
+  // check before overwriting.
+  // If |size_bytes| > 0, |qp| should indicate the frame-level QP this frame was
+  // encoded at. If the encoder does not support extracting this, |qp| should be
+  // set to 0.
+  virtual void OnEncodeDone(uint32_t rtp_timestamp,
+                            size_t size_bytes,
+                            bool is_keyframe,
+                            int qp,
+                            CodecSpecificInfoVP8* vp8_info) = 0;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_VIDEO_CODING_CODECS_VP8_INCLUDE_VP8_TEMPORAL_LAYERS_H_
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 2ef79fa..f7f5bb8 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -67,9 +67,102 @@
   ]
 }
 
+# The subset of rtc_base approved for use outside of libjingle.
+# TODO(bugs.webrtc.org/9838): Create small and focues build targets and remove
+# the old concept of rtc_base and rtc_base_approved.
 rtc_source_set("rtc_base_approved") {
   visibility = [ "*" ]
-  public_deps = [
+  deps = [
+    ":checks",
+    ":rtc_task_queue",
+    ":safe_compare",
+    ":type_traits",
+    "..:webrtc_common",
+    "../api:array_view",
+    "system:arch",
+    "system:unused",
+    "third_party/base64",
+    "//third_party/abseil-cpp/absl/memory:memory",
+    "//third_party/abseil-cpp/absl/types:optional",
+  ]
+
+  sources = [
+    "bind.h",
+    "bitbuffer.cc",
+    "bitbuffer.h",
+    "bitrateallocationstrategy.cc",
+    "bitrateallocationstrategy.h",
+    "buffer.h",
+    "bufferqueue.cc",
+    "bufferqueue.h",
+    "bytebuffer.cc",
+    "bytebuffer.h",
+    "byteorder.h",
+    "copyonwritebuffer.cc",
+    "copyonwritebuffer.h",
+    "event_tracer.cc",
+    "event_tracer.h",
+    "file.cc",
+    "file.h",
+    "flags.cc",
+    "flags.h",
+    "function_view.h",
+    "ignore_wundef.h",
+    "location.cc",
+    "location.h",
+    "message_buffer_reader.h",
+    "numerics/histogram_percentile_counter.cc",
+    "numerics/histogram_percentile_counter.h",
+    "numerics/mod_ops.h",
+    "numerics/moving_max_counter.h",
+    "numerics/sample_counter.cc",
+    "numerics/sample_counter.h",
+    "onetimeevent.h",
+    "pathutils.cc",
+    "pathutils.h",
+    "platform_file.cc",
+    "platform_file.h",
+    "race_checker.cc",
+    "race_checker.h",
+    "random.cc",
+    "random.h",
+    "rate_statistics.cc",
+    "rate_statistics.h",
+    "ratetracker.cc",
+    "ratetracker.h",
+    "swap_queue.h",
+    "template_util.h",
+    "timestampaligner.cc",
+    "timestampaligner.h",
+    "trace_event.h",
+    "zero_memory.cc",
+    "zero_memory.h",
+  ]
+
+  if (is_posix || is_fuchsia) {
+    sources += [ "file_posix.cc" ]
+  }
+
+  if (is_win) {
+    sources += [
+      "file_win.cc",
+      "win/windows_version.cc",
+      "win/windows_version.h",
+    ]
+    data_deps = [
+      "//build/win:runtime_libs",
+    ]
+  }
+
+  if (is_nacl) {
+    deps += [ "//native_client_sdk/src/libraries/nacl_io" ]
+  }
+
+  if (is_android) {
+    libs = [ "log" ]
+  }
+
+  public_deps = [  # no-presubmit-check TODO(webrtc:8603)
     ":atomicops",
     ":criticalsection",
     ":logging",
@@ -78,16 +171,12 @@
     ":platform_thread_types",
     ":ptr_util",
     ":refcount",
-    ":rtc_base_approved_generic",
     ":rtc_event",
     ":safe_conversions",
     ":stringutils",
     ":thread_checker",
     ":timeutils",
   ]
-  if (is_mac && !build_with_chromium) {
-    public_deps += [ ":rtc_base_approved_objc" ]
-  }
 }
 
 rtc_source_set("macromagic") {
@@ -149,7 +238,6 @@
 rtc_source_set("platform_thread") {
   visibility = [
     ":rtc_base_approved",
-    ":rtc_base_approved_generic",
     ":rtc_task_queue_libevent",
     ":rtc_task_queue_win",
     ":sequenced_task_checker",
@@ -192,12 +280,14 @@
 
 rtc_source_set("logging") {
   visibility = [ "*" ]
+  libs = []
   deps = [
     ":criticalsection",
     ":macromagic",
     ":platform_thread_types",
     ":stringutils",
     ":timeutils",
+    "//third_party/abseil-cpp/absl/strings",
   ]
 
   if (build_with_chromium) {
@@ -208,15 +298,27 @@
       "../../webrtc_overrides/rtc_base/logging.h",
     ]
   } else {
+    configs += [
+      "..:no_exit_time_destructors",
+      "..:no_global_constructors",
+    ]
     sources = [
       "logging.cc",
       "logging.h",
     ]
     deps += [ "system:inline" ]
 
+    if (is_mac) {
+      deps += [ ":logging_mac" ]
+    }
+
     # logging.h needs the deprecation header while downstream projects are
     # removing code that depends on logging implementation details.
     deps += [ ":deprecation" ]
+
+    if (is_android) {
+      libs += [ "log" ]
+    }
   }
 }
 
@@ -243,6 +345,7 @@
 rtc_source_set("checks") {
   # TODO(bugs.webrtc.org/9607): This should not be public.
   visibility = [ "*" ]
+  libs = []
   sources = [
     "checks.cc",
     "checks.h",
@@ -251,6 +354,9 @@
     ":safe_compare",
     "system:inline",
   ]
+  if (is_android) {
+    libs += [ "log" ]
+  }
 }
 
 rtc_source_set("rate_limiter") {
@@ -309,6 +415,7 @@
   deps = [
     ":checks",
     ":safe_conversions",
+    ":stringutils",
   ]
 }
 
@@ -328,6 +435,7 @@
     ":macromagic",
     ":safe_minmax",
     "../api:array_view",
+    "//third_party/abseil-cpp/absl/strings:strings",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -355,137 +463,14 @@
   ]
 }
 
-# The subset of rtc_base approved for use outside of libjingle.
-rtc_source_set("rtc_base_approved_generic") {
-  visibility = [
-    ":rtc_base_approved",
-    ":weak_ptr_unittests",
-  ]
-
-  cflags = []
-  defines = []
-  libs = []
-  data_deps = []
-  deps = [
-    ":atomicops",
-    ":checks",
-    ":criticalsection",
-    ":logging",
-    ":macromagic",
-    ":platform_thread",
-    ":platform_thread_types",
-    ":ptr_util",
-    ":refcount",
-    ":rtc_event",
-    ":rtc_task_queue",
-    ":safe_compare",
-    ":safe_conversions",
-    ":stringutils",
-    ":thread_checker",
-    ":timeutils",
-    ":type_traits",
-    "system:arch",
-    "system:unused",
-    "third_party/base64",
-  ]
-
-  sources = [
-    "bind.h",
-    "bitbuffer.cc",
-    "bitbuffer.h",
-    "bitrateallocationstrategy.cc",
-    "bitrateallocationstrategy.h",
-    "buffer.h",
-    "bufferqueue.cc",
-    "bufferqueue.h",
-    "bytebuffer.cc",
-    "bytebuffer.h",
-    "byteorder.h",
-    "copyonwritebuffer.cc",
-    "copyonwritebuffer.h",
-    "event_tracer.cc",
-    "event_tracer.h",
-    "file.cc",
-    "file.h",
-    "flags.cc",
-    "flags.h",
-    "function_view.h",
-    "ignore_wundef.h",
-    "location.cc",
-    "location.h",
-    "message_buffer_reader.h",
-    "numerics/histogram_percentile_counter.cc",
-    "numerics/histogram_percentile_counter.h",
-    "numerics/mod_ops.h",
-    "numerics/moving_max_counter.h",
-    "numerics/sample_counter.cc",
-    "numerics/sample_counter.h",
-    "onetimeevent.h",
-    "pathutils.cc",
-    "pathutils.h",
-    "platform_file.cc",
-    "platform_file.h",
-    "race_checker.cc",
-    "race_checker.h",
-    "random.cc",
-    "random.h",
-    "rate_statistics.cc",
-    "rate_statistics.h",
-    "ratetracker.cc",
-    "ratetracker.h",
-    "swap_queue.h",
-    "template_util.h",
-    "timestampaligner.cc",
-    "timestampaligner.h",
-    "trace_event.h",
-    "zero_memory.cc",
-    "zero_memory.h",
-  ]
-
-  deps += [
-    "..:webrtc_common",
-    "../api:array_view",
-    "//third_party/abseil-cpp/absl/memory:memory",
-    "//third_party/abseil-cpp/absl/types:optional",
-  ]
-
-  if (is_android) {
-    libs += [ "log" ]
-  }
-
-  if (is_posix || is_fuchsia) {
-    sources += [ "file_posix.cc" ]
-  }
-
-  if (is_win) {
-    sources += [
-      "file_win.cc",
-      "win/windows_version.cc",
-      "win/windows_version.h",
-    ]
-    data_deps += [ "//build/win:runtime_libs" ]
-  }
-
-  if (is_nacl) {
-    deps += [ "//native_client_sdk/src/libraries/nacl_io" ]
-  }
-}
-
 if (is_mac && !build_with_chromium) {
-  config("rtc_base_approved_objc_all_dependent_config") {
-    visibility = [ ":rtc_base_approved_objc" ]
-    libs = [ "Foundation.framework" ]  # needed for logging_mac.mm
-  }
-
-  rtc_source_set("rtc_base_approved_objc") {
-    visibility = [ ":rtc_base_approved" ]
-    all_dependent_configs = [ ":rtc_base_approved_objc_all_dependent_config" ]
+  rtc_source_set("logging_mac") {
+    visibility = [ ":logging" ]
+    libs = [ "Foundation.framework" ]
     sources = [
+      "logging_mac.h",
       "logging_mac.mm",
     ]
-    deps = [
-      ":logging",
-    ]
   }
 }
 
@@ -522,6 +507,27 @@
   deps = [
     ":macromagic",
     ":ptr_util",
+    "system:rtc_export",
+    "//third_party/abseil-cpp/absl/memory",
+  ]
+}
+
+rtc_source_set("rtc_cancelable_task") {
+  sources = [
+    "cancelable_periodic_task.h",
+    "cancelable_task_handle.cc",
+    "cancelable_task_handle.h",
+  ]
+  deps = [
+    ":checks",
+    ":logging",
+    ":macromagic",
+    ":ptr_util",
+    ":refcount",
+    ":rtc_task_queue",
+    ":safe_conversions",
+    ":sequenced_task_checker",
+    ":thread_checker",
     "//third_party/abseil-cpp/absl/memory",
   ]
 }
@@ -538,7 +544,9 @@
       ":checks",
       ":criticalsection",
       ":logging",
+      ":macromagic",
       ":platform_thread",
+      ":platform_thread_types",
       ":ptr_util",
       ":refcount",
       ":rtc_task_queue_api",
@@ -656,11 +664,13 @@
 }
 
 rtc_source_set("rtc_json") {
-  testonly = true
   defines = []
   sources = [
-    "json.cc",
-    "json.h",
+    "strings/json.cc",
+    "strings/json.h",
+  ]
+  deps = [
+    ":stringutils",
   ]
   all_dependent_configs = [ "//third_party/jsoncpp:jsoncpp_config" ]
   if (rtc_build_json) {
@@ -757,12 +767,12 @@
     "gunit_prod.h",
     "helpers.cc",
     "helpers.h",
-    "httpcommon-inl.h",
     "httpcommon.cc",
     "httpcommon.h",
     "ipaddress.cc",
     "ipaddress.h",
     "keep_ref_until_done.h",
+    "mdns_responder_interface.h",
     "messagedigest.cc",
     "messagedigest.h",
     "messagehandler.cc",
@@ -841,9 +851,6 @@
   ]
 
   if (build_with_chromium) {
-    if (is_win) {
-      sources += [ "../../webrtc_overrides/rtc_base/win32socketinit.cc" ]
-    }
     include_dirs = [ "../../boringssl/src/include" ]
     public_configs += [ ":rtc_base_chromium_config" ]
   } else {
@@ -860,7 +867,6 @@
 
     if (is_win) {
       sources += [
-        "win32socketinit.cc",
         "win32socketinit.h",
         "win32socketserver.cc",
         "win32socketserver.h",
@@ -968,6 +974,7 @@
     # included by multiple targets below.
     "cpu_time.cc",
     "cpu_time.h",
+    "fake_mdns_responder.h",
     "fakeclock.cc",
     "fakeclock.h",
     "fakenetwork.h",
@@ -977,10 +984,6 @@
     "firewallsocketserver.h",
     "gunit.cc",
     "gunit.h",
-    "httpbase.cc",
-    "httpbase.h",
-    "httpserver.cc",
-    "httpserver.h",
     "memory_usage.cc",
     "memory_usage.h",
     "natserver.cc",
@@ -1009,7 +1012,6 @@
     ":rtc_base",
     ":stringutils",
     "../api/units:time_delta",
-    "../test:field_trial",
     "../test:test_support",
     "system:fallthrough",
     "third_party/sigslot",
@@ -1057,10 +1059,9 @@
       ":rtc_base",
       ":rtc_base_approved",
       ":rtc_base_tests_utils",
-      "../system_wrappers:field_trial_default",
-      "../system_wrappers:metrics_default",
+      "../system_wrappers:field_trial",
+      "../system_wrappers:metrics",
       "../test:field_trial",
-      "../test:fileutils",
       "../test:test_support",
     ]
 
@@ -1178,12 +1179,14 @@
     testonly = true
 
     sources = [
+      "cancelable_periodic_task_unittest.cc",
       "task_queue_unittest.cc",
     ]
     deps = [
       ":rtc_base_approved",
       ":rtc_base_tests_main",
       ":rtc_base_tests_utils",
+      ":rtc_cancelable_task",
       ":rtc_task_queue",
       ":rtc_task_queue_for_test",
       "../test:test_support",
@@ -1213,7 +1216,7 @@
       "weak_ptr_unittest.cc",
     ]
     deps = [
-      ":rtc_base_approved_generic",
+      ":rtc_base_approved",
       ":rtc_base_tests_main",
       ":rtc_base_tests_utils",
       ":rtc_event",
@@ -1240,6 +1243,20 @@
     ]
   }
 
+  rtc_source_set("rtc_json_unittests") {
+    testonly = true
+
+    sources = [
+      "strings/json_unittest.cc",
+    ]
+    deps = [
+      ":rtc_base_tests_main",
+      ":rtc_base_tests_utils",
+      ":rtc_json",
+      "../test:test_support",
+    ]
+  }
+
   rtc_source_set("rtc_base_unittests") {
     testonly = true
     defines = []
@@ -1249,9 +1266,6 @@
       "crc32_unittest.cc",
       "data_rate_limiter_unittest.cc",
       "helpers_unittest.cc",
-      "httpbase_unittest.cc",
-      "httpcommon_unittest.cc",
-      "httpserver_unittest.cc",
       "ipaddress_unittest.cc",
       "memory_usage_unittest.cc",
       "messagedigest_unittest.cc",
diff --git a/rtc_base/asynctcpsocket.cc b/rtc_base/asynctcpsocket.cc
index 3d68a2a..087a98e 100644
--- a/rtc_base/asynctcpsocket.cc
+++ b/rtc_base/asynctcpsocket.cc
@@ -18,6 +18,7 @@
 #include "rtc_base/byteorder.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/timeutils.h"  // for TimeMillis
 
 #if defined(WEBRTC_POSIX)
 #include <errno.h>
diff --git a/rtc_base/asynctcpsocket.h b/rtc_base/asynctcpsocket.h
index 943e010..c145d6f 100644
--- a/rtc_base/asynctcpsocket.h
+++ b/rtc_base/asynctcpsocket.h
@@ -13,10 +13,12 @@
 
 #include <memory>
 
-#include "rtc_base/asyncpacketsocket.h"
-#include "rtc_base/buffer.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/socketfactory.h"
+#include "rtc_base/asyncpacketsocket.h"  // for PacketOptions, AsyncPacketSo...
+#include "rtc_base/asyncsocket.h"        // for AsyncSocket
+#include "rtc_base/buffer.h"             // for Buffer
+#include "rtc_base/constructormagic.h"   // for RTC_DISALLOW_COPY_AND_ASSIGN
+#include "rtc_base/socket.h"             // for Socket, Socket::Option
+#include "rtc_base/socketaddress.h"      // for SocketAddress
 
 namespace rtc {
 
diff --git a/rtc_base/bitbuffer.cc b/rtc_base/bitbuffer.cc
index 025cfe1..be72d55 100644
--- a/rtc_base/bitbuffer.cc
+++ b/rtc_base/bitbuffer.cc
@@ -108,6 +108,10 @@
 }
 
 bool BitBuffer::PeekBits(uint32_t* val, size_t bit_count) {
+  // TODO(nisse): Could allow bit_count == 0 and always return success. But
+  // current code reads one byte beyond end of buffer in the case that
+  // RemainingBitCount() == 0 and bit_count == 0.
+  RTC_DCHECK(bit_count > 0);
   if (!val || bit_count > RemainingBitCount() || bit_count > 32) {
     return false;
   }
diff --git a/rtc_base/bitbuffer.h b/rtc_base/bitbuffer.h
index 146577a..ba96a12 100644
--- a/rtc_base/bitbuffer.h
+++ b/rtc_base/bitbuffer.h
@@ -43,7 +43,7 @@
   bool ReadUInt32(uint32_t* val);
 
   // Reads bit-sized values from the buffer. Returns false if there isn't enough
-  // data left for the specified bit count..
+  // data left for the specified bit count.
   bool ReadBits(uint32_t* val, size_t bit_count);
 
   // Peeks bit-sized values from the buffer. Returns false if there isn't enough
diff --git a/rtc_base/bitrateallocationstrategy.cc b/rtc_base/bitrateallocationstrategy.cc
index d2a06cd..7c65c12 100644
--- a/rtc_base/bitrateallocationstrategy.cc
+++ b/rtc_base/bitrateallocationstrategy.cc
@@ -10,6 +10,7 @@
 
 #include "rtc_base/bitrateallocationstrategy.h"
 #include <algorithm>
+#include <map>
 #include <utility>
 
 namespace rtc {
diff --git a/rtc_base/bitrateallocationstrategy.h b/rtc_base/bitrateallocationstrategy.h
index f711d1f..a4a17f8 100644
--- a/rtc_base/bitrateallocationstrategy.h
+++ b/rtc_base/bitrateallocationstrategy.h
@@ -11,12 +11,9 @@
 #ifndef RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_
 #define RTC_BASE_BITRATEALLOCATIONSTRATEGY_H_
 
-#include <map>
-#include <memory>
 #include <string>
 #include <vector>
 #include "api/array_view.h"
-#include "rtc_base/checks.h"
 
 namespace rtc {
 
diff --git a/rtc_base/buffer.h b/rtc_base/buffer.h
index 22f8937..9bf96cd 100644
--- a/rtc_base/buffer.h
+++ b/rtc_base/buffer.h
@@ -149,7 +149,6 @@
   }
 
   BufferT& operator=(BufferT&& buf) {
-    RTC_DCHECK(IsConsistent());
     RTC_DCHECK(buf.IsConsistent());
     size_ = buf.size_;
     capacity_ = buf.capacity_;
@@ -389,7 +388,7 @@
     ExplicitZeroMemory(data_.get() + size_, count * sizeof(T));
   }
 
-  // Precondition for all methods except Clear and the destructor.
+  // Precondition for all methods except Clear, operator= and the destructor.
   // Postcondition for all methods except move construction and move
   // assignment, which leave the moved-from object in a possibly inconsistent
   // state.
@@ -401,14 +400,14 @@
   // can mutate the state slightly to help subsequent sanity checks catch bugs.
   void OnMovedFrom() {
 #if RTC_DCHECK_IS_ON
+    // Ensure that *this is always inconsistent, to provoke bugs.
+    size_ = 1;
+    capacity_ = 0;
+#else
     // Make *this consistent and empty. Shouldn't be necessary, but better safe
     // than sorry.
     size_ = 0;
     capacity_ = 0;
-#else
-    // Ensure that *this is always inconsistent, to provoke bugs.
-    size_ = 1;
-    capacity_ = 0;
 #endif
   }
 
diff --git a/rtc_base/buffer_unittest.cc b/rtc_base/buffer_unittest.cc
index ae976f1..1c3abfd 100644
--- a/rtc_base/buffer_unittest.cc
+++ b/rtc_base/buffer_unittest.cc
@@ -434,6 +434,19 @@
   EXPECT_EQ(kObsidian, buf[2].stone);
 }
 
+TEST(BufferTest, DieOnUseAfterMove) {
+  Buffer buf(17);
+  Buffer buf2 = std::move(buf);
+  EXPECT_EQ(buf2.size(), 17u);
+#if RTC_DCHECK_IS_ON
+#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
+  EXPECT_DEATH(buf.empty(), "");
+#endif
+#else
+  EXPECT_TRUE(buf.empty());
+#endif
+}
+
 TEST(ZeroOnFreeBufferTest, TestZeroOnSetData) {
   ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7);
   const uint8_t* old_data = buf.data();
diff --git a/rtc_base/bytebuffer.cc b/rtc_base/bytebuffer.cc
index 94fc6ac..f8ce1a2 100644
--- a/rtc_base/bytebuffer.cc
+++ b/rtc_base/bytebuffer.cc
@@ -12,8 +12,6 @@
 
 #include <string.h>
 
-#include <algorithm>
-
 namespace rtc {
 
 ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
diff --git a/rtc_base/cancelable_periodic_task.h b/rtc_base/cancelable_periodic_task.h
new file mode 100644
index 0000000..d113015
--- /dev/null
+++ b/rtc_base/cancelable_periodic_task.h
@@ -0,0 +1,83 @@
+/*
+ *  Copyright 2018 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 RTC_BASE_CANCELABLE_PERIODIC_TASK_H_
+#define RTC_BASE_CANCELABLE_PERIODIC_TASK_H_
+
+#include <memory>
+#include <type_traits>
+#include <utility>
+
+#include "absl/memory/memory.h"
+#include "rtc_base/cancelable_task_handle.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
+#include "rtc_base/numerics/safe_conversions.h"
+
+namespace rtc {
+namespace cancelable_periodic_task_internal {
+// CancelablePeriodicTask runs a closure multiple times with delay decided
+// by the return value of the closure itself.
+// The task can be canceled with the handle returned by GetCancelationHandle().
+// Note that the task can only be canceled on the task queue where it runs.
+template <typename Closure>
+class CancelablePeriodicTask final : public BaseCancelableTask {
+ public:
+  // |closure| should return time in ms until next run.
+  explicit CancelablePeriodicTask(Closure&& closure)
+      : closure_(std::forward<Closure>(closure)) {}
+  CancelablePeriodicTask(const CancelablePeriodicTask&) = delete;
+  CancelablePeriodicTask& operator=(const CancelablePeriodicTask&) = delete;
+  ~CancelablePeriodicTask() override = default;
+
+ private:
+  bool Run() override {
+    // See QueuedTask::Run documentaion for return values meaning.
+    if (BaseCancelableTask::Canceled())
+      return true;  // Caller retains ownership of `this`, and will destroy it.
+    // Run the actual task.
+    auto delay = closure_();
+    // Convert closure_() return type into uint32_t.
+    uint32_t delay_ms = 0;
+    if (rtc::IsValueInRangeForNumericType<uint32_t>(delay)) {
+      delay_ms = static_cast<uint32_t>(delay);
+    } else {
+      // Log and recover in production.
+      RTC_LOG(LS_ERROR) << "Invalid delay until next run: " << delay;
+      delay_ms = rtc::saturated_cast<uint32_t>(delay);
+      // But crash in debug.
+      RTC_DCHECK(false);
+    }
+    // Reschedule.
+    auto owned_task = absl::WrapUnique(this);
+    if (delay_ms == 0)
+      TaskQueue::Current()->PostTask(std::move(owned_task));
+    else
+      TaskQueue::Current()->PostDelayedTask(std::move(owned_task), delay_ms);
+    return false;  // Caller will release ownership of `this`.
+  }
+
+  Closure closure_;
+};
+}  // namespace cancelable_periodic_task_internal
+
+template <typename Closure>
+std::unique_ptr<BaseCancelableTask> CreateCancelablePeriodicTask(
+    Closure&& closure) {
+  using CleanedClosure = typename std::remove_cv<
+      typename std::remove_reference<Closure>::type>::type;
+  return absl::make_unique<cancelable_periodic_task_internal::
+                               CancelablePeriodicTask<CleanedClosure>>(
+      std::forward<CleanedClosure>(closure));
+}
+
+}  // namespace rtc
+
+#endif  // RTC_BASE_CANCELABLE_PERIODIC_TASK_H_
diff --git a/rtc_base/cancelable_periodic_task_unittest.cc b/rtc_base/cancelable_periodic_task_unittest.cc
new file mode 100644
index 0000000..fe27ea7
--- /dev/null
+++ b/rtc_base/cancelable_periodic_task_unittest.cc
@@ -0,0 +1,228 @@
+/*
+ *  Copyright 2018 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 "rtc_base/cancelable_periodic_task.h"
+
+#include "rtc_base/event.h"
+#include "test/gmock.h"
+
+namespace {
+
+using ::testing::AtLeast;
+using ::testing::Invoke;
+using ::testing::NiceMock;
+using ::testing::MockFunction;
+using ::testing::Return;
+
+constexpr int kTimeoutMs = 1000;
+
+class MockClosure {
+ public:
+  MOCK_METHOD0(Call, int());
+  MOCK_METHOD0(Delete, void());
+};
+
+class MoveOnlyClosure {
+ public:
+  explicit MoveOnlyClosure(MockClosure* mock) : mock_(mock) {}
+  MoveOnlyClosure(const MoveOnlyClosure&) = delete;
+  MoveOnlyClosure(MoveOnlyClosure&& other) : mock_(other.mock_) {
+    other.mock_ = nullptr;
+  }
+  ~MoveOnlyClosure() {
+    if (mock_)
+      mock_->Delete();
+  }
+  int operator()() { return mock_->Call(); }
+
+ private:
+  MockClosure* mock_;
+};
+
+class CopyableClosure {
+ public:
+  explicit CopyableClosure(MockClosure* mock) : mock_(mock) {}
+  CopyableClosure(const CopyableClosure& other) : mock_(other.mock_) {}
+  ~CopyableClosure() {
+    if (mock_) {
+      mock_->Delete();
+      mock_ = nullptr;
+    }
+  }
+  int operator()() { return mock_->Call(); }
+
+ private:
+  MockClosure* mock_;
+};
+
+TEST(CancelablePeriodicTaskTest, CanCallCancelOnEmptyHandle) {
+  rtc::CancelableTaskHandle handle;
+  handle.Cancel();
+}
+
+TEST(CancelablePeriodicTaskTest, CancelTaskBeforeItRuns) {
+  rtc::Event done(false, false);
+  MockClosure mock;
+  EXPECT_CALL(mock, Call).Times(0);
+  EXPECT_CALL(mock, Delete).WillOnce(Invoke([&done] { done.Set(); }));
+
+  rtc::TaskQueue task_queue("queue");
+
+  auto task = rtc::CreateCancelablePeriodicTask(MoveOnlyClosure(&mock));
+  rtc::CancelableTaskHandle handle = task->GetCancellationHandle();
+  task_queue.PostTask([handle] { handle.Cancel(); });
+  task_queue.PostTask(std::move(task));
+
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(CancelablePeriodicTaskTest, CancelDelayedTaskBeforeItRuns) {
+  rtc::Event done(false, false);
+  MockClosure mock;
+  EXPECT_CALL(mock, Call).Times(0);
+  EXPECT_CALL(mock, Delete).WillOnce(Invoke([&done] { done.Set(); }));
+
+  rtc::TaskQueue task_queue("queue");
+
+  auto task = rtc::CreateCancelablePeriodicTask(MoveOnlyClosure(&mock));
+  rtc::CancelableTaskHandle handle = task->GetCancellationHandle();
+  task_queue.PostDelayedTask(std::move(task), 100);
+  task_queue.PostTask([handle] { handle.Cancel(); });
+
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(CancelablePeriodicTaskTest, CancelTaskAfterItRuns) {
+  rtc::Event done(false, false);
+  MockClosure mock;
+  EXPECT_CALL(mock, Call).WillOnce(Return(100));
+  EXPECT_CALL(mock, Delete).WillOnce(Invoke([&done] { done.Set(); }));
+
+  rtc::TaskQueue task_queue("queue");
+
+  auto task = rtc::CreateCancelablePeriodicTask(MoveOnlyClosure(&mock));
+  rtc::CancelableTaskHandle handle = task->GetCancellationHandle();
+  task_queue.PostTask(std::move(task));
+  task_queue.PostTask([handle] { handle.Cancel(); });
+
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(CancelablePeriodicTaskTest, ZeroReturnValueRepostsTheTask) {
+  NiceMock<MockClosure> closure;
+  rtc::Event done(false, false);
+  EXPECT_CALL(closure, Call()).WillOnce(Return(0)).WillOnce(Invoke([&done] {
+    done.Set();
+    return kTimeoutMs;
+  }));
+  rtc::TaskQueue task_queue("queue");
+  task_queue.PostTask(
+      rtc::CreateCancelablePeriodicTask(MoveOnlyClosure(&closure)));
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(CancelablePeriodicTaskTest, StartPeriodicTask) {
+  MockFunction<int()> closure;
+  rtc::Event done(false, false);
+  EXPECT_CALL(closure, Call())
+      .WillOnce(Return(20))
+      .WillOnce(Return(20))
+      .WillOnce(Invoke([&done] {
+        done.Set();
+        return kTimeoutMs;
+      }));
+  rtc::TaskQueue task_queue("queue");
+  task_queue.PostTask(
+      rtc::CreateCancelablePeriodicTask(closure.AsStdFunction()));
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+// Validates perfect forwarding doesn't keep reference to deleted copy.
+TEST(CancelablePeriodicTaskTest, CreateWithCopyOfAClosure) {
+  rtc::Event done(false, false);
+  MockClosure mock;
+  EXPECT_CALL(mock, Call).WillOnce(Invoke([&done] {
+    done.Set();
+    return kTimeoutMs;
+  }));
+  EXPECT_CALL(mock, Delete).Times(AtLeast(2));
+  CopyableClosure closure(&mock);
+  std::unique_ptr<rtc::BaseCancelableTask> task;
+  {
+    CopyableClosure copy = closure;
+    task = rtc::CreateCancelablePeriodicTask(copy);
+  }
+
+  rtc::TaskQueue task_queue("queue");
+  task_queue.PostTask(std::move(task));
+  EXPECT_TRUE(done.Wait(kTimeoutMs));
+}
+
+TEST(CancelablePeriodicTaskTest, DeletingHandleDoesntStopTheTask) {
+  rtc::Event run(false, false);
+  rtc::TaskQueue task_queue("queue");
+  auto task = rtc::CreateCancelablePeriodicTask(([&] {
+    run.Set();
+    return kTimeoutMs;
+  }));
+  rtc::CancelableTaskHandle handle = task->GetCancellationHandle();
+  handle = {};  // delete the handle.
+  task_queue.PostTask(std::move(task));
+  EXPECT_TRUE(run.Wait(kTimeoutMs));
+}
+
+// Example to test there are no thread races and use after free for suggested
+// typical usage of the CancelablePeriodicTask
+TEST(CancelablePeriodicTaskTest, Example) {
+  class ObjectOnTaskQueue {
+   public:
+    void DoPeriodicTask() {}
+    int TimeUntilNextRunMs() { return 100; }
+
+    rtc::CancelableTaskHandle StartPeriodicTask(rtc::TaskQueue* task_queue) {
+      auto periodic_task = rtc::CreateCancelablePeriodicTask([this] {
+        DoPeriodicTask();
+        return TimeUntilNextRunMs();
+      });
+      rtc::CancelableTaskHandle handle = periodic_task->GetCancellationHandle();
+      task_queue->PostTask(std::move(periodic_task));
+      return handle;
+    }
+  };
+
+  rtc::TaskQueue task_queue("queue");
+
+  auto object = absl::make_unique<ObjectOnTaskQueue>();
+  // Create and start the periodic task.
+  rtc::CancelableTaskHandle handle = object->StartPeriodicTask(&task_queue);
+
+  // Restart the task
+  task_queue.PostTask([handle] { handle.Cancel(); });
+  handle = object->StartPeriodicTask(&task_queue);
+
+  // Stop the task and destroy the object.
+  struct Destructor {
+    void operator()() {
+      // Cancel must be run on the task_queue, but if task failed to start
+      // because of task queue destruction, there is no need to run Cancel.
+      handle.Cancel();
+    }
+    // Destruction will happen either on the task queue or because task
+    // queue is destroyed.
+
+    std::unique_ptr<ObjectOnTaskQueue> object;
+    rtc::CancelableTaskHandle handle;
+  };
+  task_queue.PostTask(Destructor{std::move(object), std::move(handle)});
+  // Do not wait for the Destructor closure in order to create a race between
+  // task queue destruction and running the Desctructor closure.
+}
+
+}  // namespace
diff --git a/rtc_base/cancelable_task_handle.cc b/rtc_base/cancelable_task_handle.cc
new file mode 100644
index 0000000..372e766
--- /dev/null
+++ b/rtc_base/cancelable_task_handle.cc
@@ -0,0 +1,85 @@
+/*
+ *  Copyright 2018 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 "rtc_base/cancelable_task_handle.h"
+
+#include <utility>
+
+#include "rtc_base/refcounter.h"
+#include "rtc_base/sequenced_task_checker.h"
+#include "rtc_base/thread_annotations.h"
+#include "rtc_base/thread_checker.h"
+
+namespace rtc {
+
+class CancelableTaskHandle::CancellationToken {
+ public:
+  CancellationToken() : canceled_(false), ref_count_(0) { checker_.Detach(); }
+  CancellationToken(const CancellationToken&) = delete;
+  CancellationToken& operator=(const CancellationToken&) = delete;
+
+  void Cancel() {
+    RTC_DCHECK_RUN_ON(&checker_);
+    canceled_ = true;
+  }
+
+  bool Canceled() {
+    RTC_DCHECK_RUN_ON(&checker_);
+    return canceled_;
+  }
+
+  void AddRef() { ref_count_.IncRef(); }
+
+  void Release() {
+    if (ref_count_.DecRef() == rtc::RefCountReleaseStatus::kDroppedLastRef)
+      delete this;
+  }
+
+ private:
+  ~CancellationToken() = default;
+
+  rtc::SequencedTaskChecker checker_;
+  bool canceled_ RTC_GUARDED_BY(checker_);
+  webrtc::webrtc_impl::RefCounter ref_count_;
+};
+
+CancelableTaskHandle::CancelableTaskHandle() = default;
+CancelableTaskHandle::CancelableTaskHandle(const CancelableTaskHandle&) =
+    default;
+CancelableTaskHandle::CancelableTaskHandle(CancelableTaskHandle&&) = default;
+CancelableTaskHandle& CancelableTaskHandle::operator=(
+    const CancelableTaskHandle&) = default;
+CancelableTaskHandle& CancelableTaskHandle::operator=(CancelableTaskHandle&&) =
+    default;
+CancelableTaskHandle::~CancelableTaskHandle() = default;
+
+void CancelableTaskHandle::Cancel() const {
+  if (cancellation_token_.get() != nullptr)
+    cancellation_token_->Cancel();
+}
+
+CancelableTaskHandle::CancelableTaskHandle(
+    rtc::scoped_refptr<CancellationToken> cancellation_token)
+    : cancellation_token_(std::move(cancellation_token)) {}
+
+BaseCancelableTask::~BaseCancelableTask() = default;
+
+CancelableTaskHandle BaseCancelableTask::GetCancellationHandle() const {
+  return CancelableTaskHandle(cancellation_token_);
+}
+
+BaseCancelableTask::BaseCancelableTask()
+    : cancellation_token_(new CancelableTaskHandle::CancellationToken) {}
+
+bool BaseCancelableTask::Canceled() const {
+  return cancellation_token_->Canceled();
+}
+
+}  // namespace rtc
diff --git a/rtc_base/cancelable_task_handle.h b/rtc_base/cancelable_task_handle.h
new file mode 100644
index 0000000..3b1f0d5
--- /dev/null
+++ b/rtc_base/cancelable_task_handle.h
@@ -0,0 +1,65 @@
+/*
+ *  Copyright 2018 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 RTC_BASE_CANCELABLE_TASK_HANDLE_H_
+#define RTC_BASE_CANCELABLE_TASK_HANDLE_H_
+
+#include "rtc_base/scoped_ref_ptr.h"
+#include "rtc_base/task_queue.h"
+
+namespace rtc {
+
+class BaseCancelableTask;
+
+// Allows to cancel a cancelable task. Non-empty handle can be acquired by
+// calling GetCancellationHandle() on a cancelable task.
+class CancelableTaskHandle {
+ public:
+  // This class is copyable and cheaply movable.
+  CancelableTaskHandle();
+  CancelableTaskHandle(const CancelableTaskHandle&);
+  CancelableTaskHandle(CancelableTaskHandle&&);
+  CancelableTaskHandle& operator=(const CancelableTaskHandle&);
+  CancelableTaskHandle& operator=(CancelableTaskHandle&&);
+  // Deleting the handler doesn't Cancel the task.
+  ~CancelableTaskHandle();
+
+  // Prevents the cancelable task to run.
+  // Must be executed on the same task queue as the task itself.
+  void Cancel() const;
+
+ private:
+  friend class BaseCancelableTask;
+  class CancellationToken;
+  explicit CancelableTaskHandle(
+      rtc::scoped_refptr<CancellationToken> cancelation_token);
+
+  rtc::scoped_refptr<CancellationToken> cancellation_token_;
+};
+
+class BaseCancelableTask : public QueuedTask {
+ public:
+  ~BaseCancelableTask() override;
+
+  CancelableTaskHandle GetCancellationHandle() const;
+
+ protected:
+  BaseCancelableTask();
+
+  bool Canceled() const;
+
+ private:
+  rtc::scoped_refptr<CancelableTaskHandle::CancellationToken>
+      cancellation_token_;
+};
+
+}  // namespace rtc
+
+#endif  // RTC_BASE_CANCELABLE_TASK_HANDLE_H_
diff --git a/rtc_base/checks.cc b/rtc_base/checks.cc
index 32ac8d6..b8b5c9d 100644
--- a/rtc_base/checks.cc
+++ b/rtc_base/checks.cc
@@ -35,53 +35,72 @@
 
 #include "rtc_base/checks.h"
 
+namespace {
+#if defined(__GNUC__)
+__attribute__((__format__(__printf__, 2, 3)))
+#endif
+  void AppendFormat(std::string* s, const char* fmt, ...) {
+  va_list args, copy;
+  va_start(args, fmt);
+  va_copy(copy, args);
+  const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
+  va_end(copy);
+
+  if (predicted_length > 0) {
+    const size_t size = s->size();
+    s->resize(size + predicted_length);
+    // Pass "+ 1" to vsnprintf to include space for the '\0'.
+    std::vsnprintf(&((*s)[size]), predicted_length + 1, fmt, args);
+  }
+  va_end(args);
+}
+}
+
 namespace rtc {
 namespace webrtc_checks_impl {
 
 // Reads one argument from args, appends it to s and advances fmt.
 // Returns true iff an argument was sucessfully parsed.
-bool ParseArg(va_list* args,
-              const CheckArgType** fmt,
-              std::ostream& s) {  // no-presubmit-check TODO(webrtc:8982)
+bool ParseArg(va_list* args, const CheckArgType** fmt, std::string* s) {
   if (**fmt == CheckArgType::kEnd)
     return false;
 
   switch (**fmt) {
     case CheckArgType::kInt:
-      s << va_arg(*args, int);
+      AppendFormat(s, "%d", va_arg(*args, int));
       break;
     case CheckArgType::kLong:
-      s << va_arg(*args, long);
+      AppendFormat(s, "%ld", va_arg(*args, long));
       break;
     case CheckArgType::kLongLong:
-      s << va_arg(*args, long long);
+      AppendFormat(s, "%lld", va_arg(*args, long long));
       break;
     case CheckArgType::kUInt:
-      s << va_arg(*args, unsigned);
+      AppendFormat(s, "%u", va_arg(*args, unsigned));
       break;
     case CheckArgType::kULong:
-      s << va_arg(*args, unsigned long);
+      AppendFormat(s, "%lu", va_arg(*args, unsigned long));
       break;
     case CheckArgType::kULongLong:
-      s << va_arg(*args, unsigned long long);
+      AppendFormat(s, "%llu", va_arg(*args, unsigned long long));
       break;
     case CheckArgType::kDouble:
-      s << va_arg(*args, double);
+      AppendFormat(s, "%g", va_arg(*args, double));
       break;
     case CheckArgType::kLongDouble:
-      s << va_arg(*args, long double);
+      AppendFormat(s, "%Lg", va_arg(*args, long double));
       break;
     case CheckArgType::kCharP:
-      s << va_arg(*args, const char*);
+      s->append(va_arg(*args, const char*));
       break;
     case CheckArgType::kStdString:
-      s << *va_arg(*args, const std::string*);
+      s->append(*va_arg(*args, const std::string*));
       break;
     case CheckArgType::kVoidP:
-      s << reinterpret_cast<std::uintptr_t>(va_arg(*args, const void*));
+      AppendFormat(s, "%p", va_arg(*args, const void*));
       break;
     default:
-      s << "[Invalid CheckArgType:" << static_cast<int8_t>(**fmt) << "]";
+      s->append("[Invalid CheckArgType]");
       return false;
   }
   (*fmt)++;
@@ -96,9 +115,14 @@
   va_list args;
   va_start(args, fmt);
 
-  std::ostringstream ss;  // no-presubmit-check TODO(webrtc:8982)
-  ss << "\n\n#\n# Fatal error in: " << file << ", line " << line
-     << "\n# last system error: " << LAST_SYSTEM_ERROR << "\n# Check failed: ";
+  std::string s;
+  AppendFormat(&s,
+               "\n\n"
+               "#\n"
+               "# Fatal error in: %s, line %d\n"
+               "# last system error: %u\n"
+               "# Check failed: %s",
+               file, line, LAST_SYSTEM_ERROR, message);
 
   if (*fmt == CheckArgType::kCheckOp) {
     // This log message was generated by RTC_CHECK_OP, so we have to complete
@@ -106,20 +130,19 @@
     // two arguments.
     fmt++;
 
-    std::ostringstream s1, s2;  // no-presubmit-check TODO(webrtc:8982)
-    if (ParseArg(&args, &fmt, s1) && ParseArg(&args, &fmt, s2))
-      ss << message << " (" << s1.str() << " vs. " << s2.str() << ")\n# ";
+    std::string s1, s2;
+    if (ParseArg(&args, &fmt, &s1) && ParseArg(&args, &fmt, &s2))
+      AppendFormat(&s, " (%s vs. %s)\n# ", s1.c_str(), s2.c_str());
   } else {
-    ss << message << "\n# ";
+    s.append("\n# ");
   }
 
   // Append all the user-supplied arguments to the message.
-  while (ParseArg(&args, &fmt, ss))
+  while (ParseArg(&args, &fmt, &s))
     ;
 
   va_end(args);
 
-  std::string s = ss.str();
   const char* output = s.c_str();
 
 #if defined(WEBRTC_ANDROID)
diff --git a/rtc_base/checks.h b/rtc_base/checks.h
index a65088d..9de6d47 100644
--- a/rtc_base/checks.h
+++ b/rtc_base/checks.h
@@ -40,7 +40,6 @@
 #ifdef __cplusplus
 // C++ version.
 
-#include <sstream>  // no-presubmit-check TODO(webrtc:8982)
 #include <string>
 
 #include "rtc_base/numerics/safe_compare.h"
diff --git a/rtc_base/constructormagic.h b/rtc_base/constructormagic.h
index 646a058..6b6e83c 100644
--- a/rtc_base/constructormagic.h
+++ b/rtc_base/constructormagic.h
@@ -12,7 +12,8 @@
 #define RTC_BASE_CONSTRUCTORMAGIC_H_
 
 // Put this in the declarations for a class to be unassignable.
-#define RTC_DISALLOW_ASSIGN(TypeName) void operator=(const TypeName&) = delete
+#define RTC_DISALLOW_ASSIGN(TypeName) \
+  TypeName& operator=(const TypeName&) = delete
 
 // A macro to disallow the copy constructor and operator= functions. This should
 // be used in the declarations for a class.
diff --git a/rtc_base/copyonwritebuffer.h b/rtc_base/copyonwritebuffer.h
index 0514e2f..177e38f 100644
--- a/rtc_base/copyonwritebuffer.h
+++ b/rtc_base/copyonwritebuffer.h
@@ -16,7 +16,6 @@
 
 #include "rtc_base/buffer.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/refcount.h"
 #include "rtc_base/refcountedobject.h"
 #include "rtc_base/scoped_ref_ptr.h"
 
diff --git a/rtc_base/cpu_time.cc b/rtc_base/cpu_time.cc
index f25b506..de4a6bd 100644
--- a/rtc_base/cpu_time.cc
+++ b/rtc_base/cpu_time.cc
@@ -16,6 +16,7 @@
 #include <time.h>
 #elif defined(WEBRTC_MAC)
 #include <mach/mach_init.h>
+#include <mach/mach_port.h>
 #include <mach/thread_act.h>
 #include <mach/thread_info.h>
 #include <sys/resource.h>
@@ -81,10 +82,13 @@
     RTC_LOG_ERR(LS_ERROR) << "clock_gettime() failed.";
   }
 #elif defined(WEBRTC_MAC)
+  mach_port_t thread_port = mach_thread_self();
   thread_basic_info_data_t info;
   mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
-  if (thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&info,
-                  &count) == KERN_SUCCESS) {
+  kern_return_t kr =
+      thread_info(thread_port, THREAD_BASIC_INFO, (thread_info_t)&info, &count);
+  mach_port_deallocate(mach_task_self(), thread_port);
+  if (kr == KERN_SUCCESS) {
     return info.user_time.seconds * kNumNanosecsPerSec +
            info.user_time.microseconds * kNumNanosecsPerMicrosec;
   } else {
diff --git a/rtc_base/experiments/BUILD.gn b/rtc_base/experiments/BUILD.gn
index c0e1049..044db80 100644
--- a/rtc_base/experiments/BUILD.gn
+++ b/rtc_base/experiments/BUILD.gn
@@ -15,7 +15,7 @@
   ]
   deps = [
     "../:rtc_base_approved",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -32,7 +32,7 @@
     "../../api/units:data_rate",
     "../../api/units:data_size",
     "../../api/units:time_delta",
-    "../../system_wrappers:field_trial_api",
+    "../../rtc_base:checks",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -44,7 +44,7 @@
   ]
   deps = [
     "../:rtc_base_approved",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -58,7 +58,7 @@
     "../:rtc_base_approved",
     "../..:webrtc_common",
     "../../api/video_codecs:video_codecs_api",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
@@ -70,7 +70,19 @@
   ]
   deps = [
     "../:rtc_base_approved",
-    "../../system_wrappers:field_trial_api",
+    "../../system_wrappers:field_trial",
+  ]
+}
+
+rtc_static_library("jitter_upper_bound_experiment") {
+  sources = [
+    "jitter_upper_bound_experiment.cc",
+    "jitter_upper_bound_experiment.h",
+  ]
+  deps = [
+    "../:rtc_base_approved",
+    "../../system_wrappers:field_trial",
+    "//third_party/abseil-cpp/absl/types:optional",
   ]
 }
 
@@ -92,7 +104,7 @@
       ":rtt_mult_experiment",
       "../:rtc_base_tests_main",
       "../:rtc_base_tests_utils",
-      "../../system_wrappers:field_trial_api",
+      "../../system_wrappers:field_trial",
       "../../test:field_trial",
     ]
   }
diff --git a/rtc_base/experiments/field_trial_parser.cc b/rtc_base/experiments/field_trial_parser.cc
index 55299ad..a2d7f97 100644
--- a/rtc_base/experiments/field_trial_parser.cc
+++ b/rtc_base/experiments/field_trial_parser.cc
@@ -14,6 +14,7 @@
 #include <type_traits>
 #include <utility>
 
+#include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 
 namespace webrtc {
@@ -28,7 +29,10 @@
 
 FieldTrialParameterInterface::FieldTrialParameterInterface(std::string key)
     : key_(key) {}
-FieldTrialParameterInterface::~FieldTrialParameterInterface() = default;
+FieldTrialParameterInterface::~FieldTrialParameterInterface() {
+  RTC_DCHECK(used_) << "Field trial parameter with key: '" << key_
+                    << "' never used.";
+}
 std::string FieldTrialParameterInterface::Key() const {
   return key_;
 }
@@ -38,6 +42,7 @@
     std::string trial_string) {
   std::map<std::string, FieldTrialParameterInterface*> field_map;
   for (FieldTrialParameterInterface* field : fields) {
+    field->MarkAsUsed();
     field_map[field->Key()] = field;
   }
   size_t i = 0;
@@ -77,7 +82,10 @@
 template <>
 absl::optional<double> ParseTypedParameter<double>(std::string str) {
   double value;
-  if (sscanf(str.c_str(), "%lf", &value) == 1) {
+  char unit[2]{0, 0};
+  if (sscanf(str.c_str(), "%lf%1s", &value, unit) >= 1) {
+    if (unit[0] == '%')
+      return value / 100;
     return value;
   } else {
     return absl::nullopt;
@@ -108,6 +116,10 @@
   return value_;
 }
 
+webrtc::FieldTrialFlag::operator bool() const {
+  return value_;
+}
+
 bool FieldTrialFlag::Parse(absl::optional<std::string> str_value) {
   // Only set the flag if there is no argument provided.
   if (str_value) {
diff --git a/rtc_base/experiments/field_trial_parser.h b/rtc_base/experiments/field_trial_parser.h
index a385ccf..22a8889 100644
--- a/rtc_base/experiments/field_trial_parser.h
+++ b/rtc_base/experiments/field_trial_parser.h
@@ -37,15 +37,21 @@
   virtual ~FieldTrialParameterInterface();
 
  protected:
+  // Protected to allow implementations to provide assignment and copy.
+  FieldTrialParameterInterface(const FieldTrialParameterInterface&) = default;
+  FieldTrialParameterInterface& operator=(const FieldTrialParameterInterface&) =
+      default;
   explicit FieldTrialParameterInterface(std::string key);
   friend void ParseFieldTrial(
       std::initializer_list<FieldTrialParameterInterface*> fields,
       std::string raw_string);
+  void MarkAsUsed() { used_ = true; }
   virtual bool Parse(absl::optional<std::string> str_value) = 0;
   std::string Key() const;
 
  private:
-  const std::string key_;
+  std::string key_;
+  bool used_ = false;
 };
 
 // ParseFieldTrial function parses the given string and fills the given fields
@@ -68,6 +74,7 @@
       : FieldTrialParameterInterface(key), value_(default_value) {}
   T Get() const { return value_; }
   operator T() const { return Get(); }
+  const T* operator->() const { return &value_; }
 
  protected:
   bool Parse(absl::optional<std::string> str_value) override {
@@ -135,7 +142,11 @@
       : FieldTrialParameterInterface(key) {}
   FieldTrialOptional(std::string key, absl::optional<T> default_value)
       : FieldTrialParameterInterface(key), value_(default_value) {}
-  absl::optional<T> Get() const { return value_; }
+  absl::optional<T> GetOptional() const { return value_; }
+  const T& Value() const { return value_.value(); }
+  const T& operator*() const { return value_.value(); }
+  const T* operator->() const { return &value_.value(); }
+  operator bool() const { return value_.has_value(); }
 
  protected:
   bool Parse(absl::optional<std::string> str_value) override {
@@ -162,6 +173,7 @@
   explicit FieldTrialFlag(std::string key);
   FieldTrialFlag(std::string key, bool default_value);
   bool Get() const;
+  operator bool() const;
 
  protected:
   bool Parse(absl::optional<std::string> str_value) override;
diff --git a/rtc_base/experiments/field_trial_parser_unittest.cc b/rtc_base/experiments/field_trial_parser_unittest.cc
index 69f35bd..de977ec 100644
--- a/rtc_base/experiments/field_trial_parser_unittest.cc
+++ b/rtc_base/experiments/field_trial_parser_unittest.cc
@@ -76,6 +76,15 @@
   EXPECT_EQ(exp.ping.Get(), true);
   EXPECT_EQ(exp.hash.Get(), "");
 }
+TEST(FieldTrialParserTest, ParsesDoubleParameter) {
+  FieldTrialParameter<double> double_param("f", 0.0);
+  ParseFieldTrial({&double_param}, "f:45%");
+  EXPECT_EQ(double_param.Get(), 0.45);
+  ParseFieldTrial({&double_param}, "f:34 %");
+  EXPECT_EQ(double_param.Get(), 0.34);
+  ParseFieldTrial({&double_param}, "f:0.67");
+  EXPECT_EQ(double_param.Get(), 0.67);
+}
 TEST(FieldTrialParserTest, IgnoresNewKey) {
   DummyExperiment exp("Disabled,r:-11,foo");
   EXPECT_FALSE(exp.enabled.Get());
@@ -93,20 +102,20 @@
 TEST(FieldTrialParserTest, ParsesOptionalParameters) {
   FieldTrialOptional<int> max_count("c", absl::nullopt);
   ParseFieldTrial({&max_count}, "");
-  EXPECT_FALSE(max_count.Get().has_value());
+  EXPECT_FALSE(max_count.GetOptional().has_value());
   ParseFieldTrial({&max_count}, "c:10");
-  EXPECT_EQ(max_count.Get().value(), 10);
+  EXPECT_EQ(max_count.GetOptional().value(), 10);
   ParseFieldTrial({&max_count}, "c");
-  EXPECT_FALSE(max_count.Get().has_value());
+  EXPECT_FALSE(max_count.GetOptional().has_value());
   ParseFieldTrial({&max_count}, "c:20");
-  EXPECT_EQ(max_count.Get().value(), 20);
+  EXPECT_EQ(max_count.GetOptional().value(), 20);
   ParseFieldTrial({&max_count}, "c:");
-  EXPECT_EQ(max_count.Get().value(), 20);
+  EXPECT_EQ(max_count.GetOptional().value(), 20);
   FieldTrialOptional<std::string> optional_string("s", std::string("ab"));
   ParseFieldTrial({&optional_string}, "s:");
-  EXPECT_EQ(optional_string.Get().value(), "");
+  EXPECT_EQ(optional_string.GetOptional().value(), "");
   ParseFieldTrial({&optional_string}, "s");
-  EXPECT_FALSE(optional_string.Get().has_value());
+  EXPECT_FALSE(optional_string.GetOptional().has_value());
 }
 TEST(FieldTrialParserTest, ParsesCustomEnumParameter) {
   FieldTrialEnum<CustomEnum> my_enum("e", CustomEnum::kDefault,
diff --git a/rtc_base/experiments/field_trial_units_unittest.cc b/rtc_base/experiments/field_trial_units_unittest.cc
index 1a2b75d..80771d9 100644
--- a/rtc_base/experiments/field_trial_units_unittest.cc
+++ b/rtc_base/experiments/field_trial_units_unittest.cc
@@ -32,19 +32,19 @@
 TEST(FieldTrialParserUnitsTest, FallsBackToDefaults) {
   DummyExperiment exp("");
   EXPECT_EQ(exp.target_rate.Get(), DataRate::kbps(100));
-  EXPECT_FALSE(exp.max_buffer.Get().has_value());
+  EXPECT_FALSE(exp.max_buffer.GetOptional().has_value());
   EXPECT_EQ(exp.period.Get(), TimeDelta::ms(100));
 }
 TEST(FieldTrialParserUnitsTest, ParsesUnitParameters) {
   DummyExperiment exp("t:300kbps,b:5bytes,p:300ms");
   EXPECT_EQ(exp.target_rate.Get(), DataRate::kbps(300));
-  EXPECT_EQ(*exp.max_buffer.Get(), DataSize::bytes(5));
+  EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::bytes(5));
   EXPECT_EQ(exp.period.Get(), TimeDelta::ms(300));
 }
 TEST(FieldTrialParserUnitsTest, ParsesDefaultUnitParameters) {
   DummyExperiment exp("t:300,b:5,p:300");
   EXPECT_EQ(exp.target_rate.Get(), DataRate::kbps(300));
-  EXPECT_EQ(*exp.max_buffer.Get(), DataSize::bytes(5));
+  EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::bytes(5));
   EXPECT_EQ(exp.period.Get(), TimeDelta::ms(300));
 }
 TEST(FieldTrialParserUnitsTest, ParsesInfinityParameter) {
@@ -55,7 +55,7 @@
 TEST(FieldTrialParserUnitsTest, ParsesOtherUnitParameters) {
   DummyExperiment exp("t:300bps,p:0.3 seconds,b:8 bytes");
   EXPECT_EQ(exp.target_rate.Get(), DataRate::bps(300));
-  EXPECT_EQ(*exp.max_buffer.Get(), DataSize::bytes(8));
+  EXPECT_EQ(*exp.max_buffer.GetOptional(), DataSize::bytes(8));
   EXPECT_EQ(exp.period.Get(), TimeDelta::ms(300));
 }
 
diff --git a/rtc_base/experiments/jitter_upper_bound_experiment.cc b/rtc_base/experiments/jitter_upper_bound_experiment.cc
new file mode 100644
index 0000000..b3e9230
--- /dev/null
+++ b/rtc_base/experiments/jitter_upper_bound_experiment.cc
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2018 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 "rtc_base/experiments/jitter_upper_bound_experiment.h"
+
+#include <algorithm>
+#include <string>
+
+#include "rtc_base/logging.h"
+#include "system_wrappers/include/field_trial.h"
+
+namespace webrtc {
+
+const char JitterUpperBoundExperiment::kJitterUpperBoundExperimentName[] =
+    "WebRTC-JitterUpperBound";
+
+absl::optional<double> JitterUpperBoundExperiment::GetUpperBoundSigmas() {
+  if (!field_trial::IsEnabled(kJitterUpperBoundExperimentName)) {
+    return absl::nullopt;
+  }
+  const std::string group =
+      webrtc::field_trial::FindFullName(kJitterUpperBoundExperimentName);
+
+  double upper_bound_sigmas;
+  if (sscanf(group.c_str(), "Enabled-%lf", &upper_bound_sigmas) != 1) {
+    RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
+    return absl::nullopt;
+  }
+
+  if (upper_bound_sigmas < 0) {
+    RTC_LOG(LS_WARNING) << "Invalid jitter upper bound sigmas, must be >= 0.0: "
+                        << upper_bound_sigmas;
+    return absl::nullopt;
+  }
+
+  return upper_bound_sigmas;
+}
+
+}  // namespace webrtc
diff --git a/rtc_base/experiments/jitter_upper_bound_experiment.h b/rtc_base/experiments/jitter_upper_bound_experiment.h
new file mode 100644
index 0000000..262cd79
--- /dev/null
+++ b/rtc_base/experiments/jitter_upper_bound_experiment.h
@@ -0,0 +1,31 @@
+/*
+ *  Copyright (c) 2018 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 RTC_BASE_EXPERIMENTS_JITTER_UPPER_BOUND_EXPERIMENT_H_
+#define RTC_BASE_EXPERIMENTS_JITTER_UPPER_BOUND_EXPERIMENT_H_
+
+#include "absl/types/optional.h"
+
+namespace webrtc {
+
+class JitterUpperBoundExperiment {
+ public:
+  // Returns nullopt if experiment is not on, otherwise returns the configured
+  // upper bound for frame delay delta used in jitter estimation, expressed as
+  // number of standard deviations of the current deviation from the expected
+  // delay.
+  static absl::optional<double> GetUpperBoundSigmas();
+
+  static const char kJitterUpperBoundExperimentName[];
+};
+
+}  // namespace webrtc
+
+#endif  // RTC_BASE_EXPERIMENTS_JITTER_UPPER_BOUND_EXPERIMENT_H_
diff --git a/rtc_base/fake_mdns_responder.h b/rtc_base/fake_mdns_responder.h
new file mode 100644
index 0000000..32d69ba
--- /dev/null
+++ b/rtc_base/fake_mdns_responder.h
@@ -0,0 +1,56 @@
+/*
+ *  Copyright 2018 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 RTC_BASE_FAKE_MDNS_RESPONDER_H_
+#define RTC_BASE_FAKE_MDNS_RESPONDER_H_
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include "rtc_base/mdns_responder_interface.h"
+
+#include "rtc_base/helpers.h"
+
+namespace webrtc {
+
+class FakeMDnsResponder : public MDnsResponderInterface {
+ public:
+  FakeMDnsResponder() = default;
+  ~FakeMDnsResponder() = default;
+
+  void CreateNameForAddress(const rtc::IPAddress& addr,
+                            NameCreatedCallback callback) override {
+    std::string name;
+    if (addr_name_map_.find(addr) != addr_name_map_.end()) {
+      name = addr_name_map_[addr];
+    } else {
+      name = std::to_string(next_available_id_++) + ".local";
+      addr_name_map_[addr] = name;
+    }
+    callback(addr, name);
+  }
+  void RemoveNameForAddress(const rtc::IPAddress& addr,
+                            NameRemovedCallback callback) override {
+    auto it = addr_name_map_.find(addr);
+    if (it != addr_name_map_.end()) {
+      addr_name_map_.erase(it);
+    }
+    callback(it != addr_name_map_.end());
+  }
+
+ private:
+  uint32_t next_available_id_ = 0;
+  std::map<rtc::IPAddress, std::string> addr_name_map_;
+};
+
+}  // namespace webrtc
+
+#endif  // RTC_BASE_FAKE_MDNS_RESPONDER_H_
diff --git a/rtc_base/fakeclock.cc b/rtc_base/fakeclock.cc
index ade9208..f63b85c 100644
--- a/rtc_base/fakeclock.cc
+++ b/rtc_base/fakeclock.cc
@@ -28,7 +28,7 @@
   }
   // If message queues are waiting in a socket select() with a timeout provided
   // by the OS, they should wake up and dispatch all messages that are ready.
-  MessageQueueManager::ProcessAllMessageQueues();
+  MessageQueueManager::ProcessAllMessageQueuesForTesting();
 }
 
 void FakeClock::AdvanceTime(webrtc::TimeDelta delta) {
@@ -36,7 +36,7 @@
     CritScope cs(&lock_);
     time_ += delta.ns();
   }
-  MessageQueueManager::ProcessAllMessageQueues();
+  MessageQueueManager::ProcessAllMessageQueuesForTesting();
 }
 
 ScopedFakeClock::ScopedFakeClock() {
diff --git a/rtc_base/fakenetwork.h b/rtc_base/fakenetwork.h
index 8ed1164..d5426a3 100644
--- a/rtc_base/fakenetwork.h
+++ b/rtc_base/fakenetwork.h
@@ -16,6 +16,9 @@
 #include <utility>
 #include <vector>
 
+#include "absl/memory/memory.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/fake_mdns_responder.h"
 #include "rtc_base/messagehandler.h"
 #include "rtc_base/network.h"
 #include "rtc_base/socketaddress.h"
@@ -79,9 +82,19 @@
   // MessageHandler interface.
   virtual void OnMessage(Message* msg) { DoUpdateNetworks(); }
 
+  void CreateMDnsResponder() {
+    if (mdns_responder_ == nullptr) {
+      mdns_responder_ = absl::make_unique<webrtc::FakeMDnsResponder>();
+    }
+  }
+
   using NetworkManagerBase::set_enumeration_permission;
   using NetworkManagerBase::set_default_local_addresses;
 
+  webrtc::MDnsResponderInterface* GetMDnsResponder() const override {
+    return mdns_responder_.get();
+  }
+
  private:
   void DoUpdateNetworks() {
     if (start_count_ == 0)
@@ -117,6 +130,8 @@
 
   IPAddress default_local_ipv4_address_;
   IPAddress default_local_ipv6_address_;
+
+  std::unique_ptr<webrtc::FakeMDnsResponder> mdns_responder_;
 };
 
 }  // namespace rtc
diff --git a/rtc_base/file.cc b/rtc_base/file.cc
index 6202411..a793500 100644
--- a/rtc_base/file.cc
+++ b/rtc_base/file.cc
@@ -10,8 +10,6 @@
 
 #include "rtc_base/file.h"
 
-#include <utility>
-
 namespace rtc {
 
 File::File(PlatformFile file) : file_(file) {}
diff --git a/rtc_base/filerotatingstream.cc b/rtc_base/filerotatingstream.cc
index c9a663a..31b0051 100644
--- a/rtc_base/filerotatingstream.cc
+++ b/rtc_base/filerotatingstream.cc
@@ -16,6 +16,7 @@
 
 #include "rtc_base/checks.h"
 #include "rtc_base/fileutils.h"
+#include "rtc_base/logging.h"
 #include "rtc_base/pathutils.h"
 #include "rtc_base/strings/string_builder.h"
 
diff --git a/rtc_base/fileutils.cc b/rtc_base/fileutils.cc
index 7d83f97..0adbbac 100644
--- a/rtc_base/fileutils.cc
+++ b/rtc_base/fileutils.cc
@@ -10,12 +10,11 @@
 
 #include "rtc_base/fileutils.h"
 
-#include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/pathutils.h"
-#include "rtc_base/stringutils.h"
 
 #if defined(WEBRTC_WIN)
+#include "rtc_base/stringutils.h"  // for ToUtf16
 #include "rtc_base/win32filesystem.h"
 #else
 #include "rtc_base/unixfilesystem.h"
diff --git a/rtc_base/fileutils.h b/rtc_base/fileutils.h
index 132fd88..f7afaf9 100644
--- a/rtc_base/fileutils.h
+++ b/rtc_base/fileutils.h
@@ -23,9 +23,7 @@
 #include <unistd.h>
 #endif  // WEBRTC_WIN
 
-#include "rtc_base/checks.h"
 #include "rtc_base/constructormagic.h"
-#include "rtc_base/platform_file.h"
 
 namespace rtc {
 
diff --git a/rtc_base/flags.cc b/rtc_base/flags.cc
index 5b28794..bcce0da 100644
--- a/rtc_base/flags.cc
+++ b/rtc_base/flags.cc
@@ -15,7 +15,6 @@
 #include <string.h>
 
 #include "rtc_base/checks.h"
-#include "rtc_base/stringutils.h"
 
 #if defined(WEBRTC_WIN)
 // clang-format off
@@ -23,6 +22,8 @@
 #include <windows.h>
 #include <shellapi.h> // must come after windows.h
 // clang-format on
+
+#include "rtc_base/stringutils.h"  // For ToUtf8
 #endif
 
 namespace {
diff --git a/rtc_base/flags.h b/rtc_base/flags.h
index 33f6e5b..1c476d8 100644
--- a/rtc_base/flags.h
+++ b/rtc_base/flags.h
@@ -23,7 +23,10 @@
 #define RTC_BASE_FLAGS_H_
 
 #include "rtc_base/checks.h"
+
+#if defined(WEBRTC_WIN)
 #include "rtc_base/constructormagic.h"
+#endif
 
 namespace rtc {
 
diff --git a/rtc_base/httpbase.cc b/rtc_base/httpbase.cc
deleted file mode 100644
index 8f2869d..0000000
--- a/rtc_base/httpbase.cc
+++ /dev/null
@@ -1,713 +0,0 @@
-/*
- *  Copyright 2004 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 <memory>
-
-#if defined(WEBRTC_WIN)
-#include "rtc_base/win32.h"
-#else  // !WEBRTC_WIN
-#define SEC_E_CERT_EXPIRED (-2146893016)
-#endif  // !WEBRTC_WIN
-
-#include "rtc_base/checks.h"
-#include "rtc_base/httpbase.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/socket.h"
-#include "rtc_base/stringutils.h"
-#include "rtc_base/system/fallthrough.h"
-#include "rtc_base/thread.h"
-
-namespace rtc {
-
-//////////////////////////////////////////////////////////////////////
-// Helpers
-//////////////////////////////////////////////////////////////////////
-
-bool MatchHeader(const char* str, size_t len, HttpHeader header) {
-  const char* const header_str = ToString(header);
-  const size_t header_len = strlen(header_str);
-  return (len == header_len) && (_strnicmp(str, header_str, header_len) == 0);
-}
-
-enum { MSG_READ };
-
-//////////////////////////////////////////////////////////////////////
-// HttpParser
-//////////////////////////////////////////////////////////////////////
-
-HttpParser::HttpParser() {
-  reset();
-}
-
-HttpParser::~HttpParser() {}
-
-void HttpParser::reset() {
-  state_ = ST_LEADER;
-  chunked_ = false;
-  data_size_ = SIZE_UNKNOWN;
-}
-
-HttpParser::ProcessResult HttpParser::Process(const char* buffer,
-                                              size_t len,
-                                              size_t* processed,
-                                              HttpError* error) {
-  *processed = 0;
-  *error = HE_NONE;
-
-  if (state_ >= ST_COMPLETE) {
-    RTC_NOTREACHED();
-    return PR_COMPLETE;
-  }
-
-  while (true) {
-    if (state_ < ST_DATA) {
-      size_t pos = *processed;
-      while ((pos < len) && (buffer[pos] != '\n')) {
-        pos += 1;
-      }
-      if (pos >= len) {
-        break;  // don't have a full header
-      }
-      const char* line = buffer + *processed;
-      size_t len = (pos - *processed);
-      *processed = pos + 1;
-      while ((len > 0) && isspace(static_cast<unsigned char>(line[len - 1]))) {
-        len -= 1;
-      }
-      ProcessResult result = ProcessLine(line, len, error);
-      RTC_LOG(LS_VERBOSE) << "Processed line, result=" << result;
-
-      if (PR_CONTINUE != result) {
-        return result;
-      }
-    } else if (data_size_ == 0) {
-      if (chunked_) {
-        state_ = ST_CHUNKTERM;
-      } else {
-        return PR_COMPLETE;
-      }
-    } else {
-      size_t available = len - *processed;
-      if (available <= 0) {
-        break;  // no more data
-      }
-      if ((data_size_ != SIZE_UNKNOWN) && (available > data_size_)) {
-        available = data_size_;
-      }
-      size_t read = 0;
-      ProcessResult result =
-          ProcessData(buffer + *processed, available, read, error);
-      RTC_LOG(LS_VERBOSE) << "Processed data, result: " << result
-                          << " read: " << read << " err: " << error;
-
-      if (PR_CONTINUE != result) {
-        return result;
-      }
-      *processed += read;
-      if (data_size_ != SIZE_UNKNOWN) {
-        data_size_ -= read;
-      }
-    }
-  }
-
-  return PR_CONTINUE;
-}
-
-HttpParser::ProcessResult HttpParser::ProcessLine(const char* line,
-                                                  size_t len,
-                                                  HttpError* error) {
-  RTC_LOG_F(LS_VERBOSE) << " state: " << state_
-                        << " line: " << std::string(line, len)
-                        << " len: " << len << " err: " << error;
-
-  switch (state_) {
-    case ST_LEADER:
-      state_ = ST_HEADERS;
-      return ProcessLeader(line, len, error);
-
-    case ST_HEADERS:
-      if (len > 0) {
-        const char* value = strchrn(line, len, ':');
-        if (!value) {
-          *error = HE_PROTOCOL;
-          return PR_COMPLETE;
-        }
-        size_t nlen = (value - line);
-        const char* eol = line + len;
-        do {
-          value += 1;
-        } while ((value < eol) && isspace(static_cast<unsigned char>(*value)));
-        size_t vlen = eol - value;
-        if (MatchHeader(line, nlen, HH_CONTENT_LENGTH)) {
-          // sscanf isn't safe with strings that aren't null-terminated, and
-          // there is no guarantee that |value| is. Create a local copy that is
-          // null-terminated.
-          std::string value_str(value, vlen);
-          unsigned int temp_size;
-          if (sscanf(value_str.c_str(), "%u", &temp_size) != 1) {
-            *error = HE_PROTOCOL;
-            return PR_COMPLETE;
-          }
-          data_size_ = static_cast<size_t>(temp_size);
-        } else if (MatchHeader(line, nlen, HH_TRANSFER_ENCODING)) {
-          if ((vlen == 7) && (_strnicmp(value, "chunked", 7) == 0)) {
-            chunked_ = true;
-          } else if ((vlen == 8) && (_strnicmp(value, "identity", 8) == 0)) {
-            chunked_ = false;
-          } else {
-            *error = HE_PROTOCOL;
-            return PR_COMPLETE;
-          }
-        }
-        return ProcessHeader(line, nlen, value, vlen, error);
-      } else {
-        state_ = chunked_ ? ST_CHUNKSIZE : ST_DATA;
-        return ProcessHeaderComplete(chunked_, data_size_, error);
-      }
-      break;
-
-    case ST_CHUNKSIZE:
-      if (len > 0) {
-        char* ptr = nullptr;
-        data_size_ = strtoul(line, &ptr, 16);
-        if (ptr != line + len) {
-          *error = HE_PROTOCOL;
-          return PR_COMPLETE;
-        }
-        state_ = (data_size_ == 0) ? ST_TRAILERS : ST_DATA;
-      } else {
-        *error = HE_PROTOCOL;
-        return PR_COMPLETE;
-      }
-      break;
-
-    case ST_CHUNKTERM:
-      if (len > 0) {
-        *error = HE_PROTOCOL;
-        return PR_COMPLETE;
-      } else {
-        state_ = chunked_ ? ST_CHUNKSIZE : ST_DATA;
-      }
-      break;
-
-    case ST_TRAILERS:
-      if (len == 0) {
-        return PR_COMPLETE;
-      }
-      // *error = onHttpRecvTrailer();
-      break;
-
-    default:
-      RTC_NOTREACHED();
-      break;
-  }
-
-  return PR_CONTINUE;
-}
-
-bool HttpParser::is_valid_end_of_input() const {
-  return (state_ == ST_DATA) && (data_size_ == SIZE_UNKNOWN);
-}
-
-void HttpParser::complete(HttpError error) {
-  if (state_ < ST_COMPLETE) {
-    state_ = ST_COMPLETE;
-    OnComplete(error);
-  }
-}
-
-//////////////////////////////////////////////////////////////////////
-// HttpBase
-//////////////////////////////////////////////////////////////////////
-
-HttpBase::HttpBase()
-    : mode_(HM_NONE), data_(nullptr), notify_(nullptr), http_stream_(nullptr) {}
-
-HttpBase::~HttpBase() {
-  RTC_DCHECK(HM_NONE == mode_);
-}
-
-bool HttpBase::isConnected() const {
-  return (http_stream_ != nullptr) && (http_stream_->GetState() == SS_OPEN);
-}
-
-bool HttpBase::attach(StreamInterface* stream) {
-  if ((mode_ != HM_NONE) || (http_stream_ != nullptr) || (stream == nullptr)) {
-    RTC_NOTREACHED();
-    return false;
-  }
-  http_stream_ = stream;
-  http_stream_->SignalEvent.connect(this, &HttpBase::OnHttpStreamEvent);
-  mode_ = (http_stream_->GetState() == SS_OPENING) ? HM_CONNECT : HM_NONE;
-  return true;
-}
-
-StreamInterface* HttpBase::detach() {
-  RTC_DCHECK(HM_NONE == mode_);
-  if (mode_ != HM_NONE) {
-    return nullptr;
-  }
-  StreamInterface* stream = http_stream_;
-  http_stream_ = nullptr;
-  if (stream) {
-    stream->SignalEvent.disconnect(this);
-  }
-  return stream;
-}
-
-void HttpBase::send(HttpData* data) {
-  RTC_DCHECK(HM_NONE == mode_);
-  if (mode_ != HM_NONE) {
-    return;
-  } else if (!isConnected()) {
-    OnHttpStreamEvent(http_stream_, SE_CLOSE, HE_DISCONNECTED);
-    return;
-  }
-
-  mode_ = HM_SEND;
-  data_ = data;
-  len_ = 0;
-  ignore_data_ = chunk_data_ = false;
-
-  if (data_->document) {
-    data_->document->SignalEvent.connect(this, &HttpBase::OnDocumentEvent);
-  }
-
-  std::string encoding;
-  if (data_->hasHeader(HH_TRANSFER_ENCODING, &encoding) &&
-      (encoding == "chunked")) {
-    chunk_data_ = true;
-  }
-
-  len_ = data_->formatLeader(buffer_, sizeof(buffer_));
-  len_ += strcpyn(buffer_ + len_, sizeof(buffer_) - len_, "\r\n");
-
-  header_ = data_->begin();
-  if (header_ == data_->end()) {
-    // We must call this at least once, in the case where there are no headers.
-    queue_headers();
-  }
-
-  flush_data();
-}
-
-void HttpBase::recv(HttpData* data) {
-  RTC_DCHECK(HM_NONE == mode_);
-  if (mode_ != HM_NONE) {
-    return;
-  } else if (!isConnected()) {
-    OnHttpStreamEvent(http_stream_, SE_CLOSE, HE_DISCONNECTED);
-    return;
-  }
-
-  mode_ = HM_RECV;
-  data_ = data;
-  len_ = 0;
-  ignore_data_ = chunk_data_ = false;
-
-  reset();
-  read_and_process_data();
-}
-
-void HttpBase::abort(HttpError err) {
-  if (mode_ != HM_NONE) {
-    if (http_stream_ != nullptr) {
-      http_stream_->Close();
-    }
-    do_complete(err);
-  }
-}
-
-HttpError HttpBase::HandleStreamClose(int error) {
-  if (http_stream_ != nullptr) {
-    http_stream_->Close();
-  }
-  if (error == 0) {
-    if ((mode_ == HM_RECV) && is_valid_end_of_input()) {
-      return HE_NONE;
-    } else {
-      return HE_DISCONNECTED;
-    }
-  } else if (error == SOCKET_EACCES) {
-    return HE_AUTH;
-  } else if (error == SEC_E_CERT_EXPIRED) {
-    return HE_CERTIFICATE_EXPIRED;
-  }
-  RTC_LOG_F(LS_ERROR) << "(" << error << ")";
-  return (HM_CONNECT == mode_) ? HE_CONNECT_FAILED : HE_SOCKET_ERROR;
-}
-
-bool HttpBase::DoReceiveLoop(HttpError* error) {
-  RTC_DCHECK(HM_RECV == mode_);
-  RTC_DCHECK(nullptr != error);
-
-  // Do to the latency between receiving read notifications from
-  // pseudotcpchannel, we rely on repeated calls to read in order to acheive
-  // ideal throughput.  The number of reads is limited to prevent starving
-  // the caller.
-
-  size_t loop_count = 0;
-  const size_t kMaxReadCount = 20;
-  bool process_requires_more_data = false;
-  do {
-    // The most frequent use of this function is response to new data available
-    // on http_stream_.  Therefore, we optimize by attempting to read from the
-    // network first (as opposed to processing existing data first).
-
-    if (len_ < sizeof(buffer_)) {
-      // Attempt to buffer more data.
-      size_t read;
-      int read_error;
-      StreamResult read_result = http_stream_->Read(
-          buffer_ + len_, sizeof(buffer_) - len_, &read, &read_error);
-      switch (read_result) {
-        case SR_SUCCESS:
-          RTC_DCHECK(len_ + read <= sizeof(buffer_));
-          len_ += read;
-          break;
-        case SR_BLOCK:
-          if (process_requires_more_data) {
-            // We're can't make progress until more data is available.
-            return false;
-          }
-          // Attempt to process the data already in our buffer.
-          break;
-        case SR_EOS:
-          // Clean close, with no error.
-          read_error = 0;
-          RTC_FALLTHROUGH();  // Fall through to HandleStreamClose.
-        case SR_ERROR:
-          *error = HandleStreamClose(read_error);
-          return true;
-      }
-    } else if (process_requires_more_data) {
-      // We have too much unprocessed data in our buffer.  This should only
-      // occur when a single HTTP header is longer than the buffer size (32K).
-      // Anything longer than that is almost certainly an error.
-      *error = HE_OVERFLOW;
-      return true;
-    }
-
-    // Process data in our buffer.  Process is not guaranteed to process all
-    // the buffered data.  In particular, it will wait until a complete
-    // protocol element (such as http header, or chunk size) is available,
-    // before processing it in its entirety.  Also, it is valid and sometimes
-    // necessary to call Process with an empty buffer, since the state machine
-    // may have interrupted state transitions to complete.
-    size_t processed;
-    ProcessResult process_result = Process(buffer_, len_, &processed, error);
-    RTC_DCHECK(processed <= len_);
-    len_ -= processed;
-    memmove(buffer_, buffer_ + processed, len_);
-    switch (process_result) {
-      case PR_CONTINUE:
-        // We need more data to make progress.
-        process_requires_more_data = true;
-        break;
-      case PR_BLOCK:
-        // We're stalled on writing the processed data.
-        return false;
-      case PR_COMPLETE:
-        // *error already contains the correct code.
-        return true;
-    }
-  } while (++loop_count <= kMaxReadCount);
-
-  RTC_LOG_F(LS_WARNING) << "danger of starvation";
-  return false;
-}
-
-void HttpBase::read_and_process_data() {
-  HttpError error;
-  if (DoReceiveLoop(&error)) {
-    complete(error);
-  }
-}
-
-void HttpBase::flush_data() {
-  RTC_DCHECK(HM_SEND == mode_);
-
-  // When send_required is true, no more buffering can occur without a network
-  // write.
-  bool send_required = (len_ >= sizeof(buffer_));
-
-  while (true) {
-    RTC_DCHECK(len_ <= sizeof(buffer_));
-
-    // HTTP is inherently sensitive to round trip latency, since a frequent use
-    // case is for small requests and responses to be sent back and forth, and
-    // the lack of pipelining forces a single request to take a minimum of the
-    // round trip time.  As a result, it is to our benefit to pack as much data
-    // into each packet as possible.  Thus, we defer network writes until we've
-    // buffered as much data as possible.
-
-    if (!send_required && (header_ != data_->end())) {
-      // First, attempt to queue more header data.
-      send_required = queue_headers();
-    }
-
-    if (!send_required && data_->document) {
-      // Next, attempt to queue document data.
-
-      const size_t kChunkDigits = 8;
-      size_t offset, reserve;
-      if (chunk_data_) {
-        // Reserve characters at the start for X-byte hex value and \r\n
-        offset = len_ + kChunkDigits + 2;
-        // ... and 2 characters at the end for \r\n
-        reserve = offset + 2;
-      } else {
-        offset = len_;
-        reserve = offset;
-      }
-
-      if (reserve >= sizeof(buffer_)) {
-        send_required = true;
-      } else {
-        size_t read;
-        int error;
-        StreamResult result = data_->document->Read(
-            buffer_ + offset, sizeof(buffer_) - reserve, &read, &error);
-        if (result == SR_SUCCESS) {
-          RTC_DCHECK(reserve + read <= sizeof(buffer_));
-          if (chunk_data_) {
-            // Prepend the chunk length in hex.
-            // Note: sprintfn appends a null terminator, which is why we can't
-            // combine it with the line terminator.
-            sprintfn(buffer_ + len_, kChunkDigits + 1, "%.*x", kChunkDigits,
-                     read);
-            // Add line terminator to the chunk length.
-            memcpy(buffer_ + len_ + kChunkDigits, "\r\n", 2);
-            // Add line terminator to the end of the chunk.
-            memcpy(buffer_ + offset + read, "\r\n", 2);
-          }
-          len_ = reserve + read;
-        } else if (result == SR_BLOCK) {
-          // Nothing to do but flush data to the network.
-          send_required = true;
-        } else if (result == SR_EOS) {
-          if (chunk_data_) {
-            // Append the empty chunk and empty trailers, then turn off
-            // chunking.
-            RTC_DCHECK(len_ + 5 <= sizeof(buffer_));
-            memcpy(buffer_ + len_, "0\r\n\r\n", 5);
-            len_ += 5;
-            chunk_data_ = false;
-          } else if (0 == len_) {
-            // No more data to read, and no more data to write.
-            do_complete();
-            return;
-          }
-          // Although we are done reading data, there is still data which needs
-          // to be flushed to the network.
-          send_required = true;
-        } else {
-          RTC_LOG_F(LS_ERROR) << "Read error: " << error;
-          do_complete(HE_STREAM);
-          return;
-        }
-      }
-    }
-
-    if (0 == len_) {
-      // No data currently available to send.
-      if (!data_->document) {
-        // If there is no source document, that means we're done.
-        do_complete();
-      }
-      return;
-    }
-
-    size_t written;
-    int error;
-    StreamResult result = http_stream_->Write(buffer_, len_, &written, &error);
-    if (result == SR_SUCCESS) {
-      RTC_DCHECK(written <= len_);
-      len_ -= written;
-      memmove(buffer_, buffer_ + written, len_);
-      send_required = false;
-    } else if (result == SR_BLOCK) {
-      if (send_required) {
-        // Nothing more we can do until network is writeable.
-        return;
-      }
-    } else {
-      RTC_DCHECK(result == SR_ERROR);
-      RTC_LOG_F(LS_ERROR) << "error";
-      OnHttpStreamEvent(http_stream_, SE_CLOSE, error);
-      return;
-    }
-  }
-
-  RTC_NOTREACHED();
-}
-
-bool HttpBase::queue_headers() {
-  RTC_DCHECK(HM_SEND == mode_);
-  while (header_ != data_->end()) {
-    size_t len =
-        sprintfn(buffer_ + len_, sizeof(buffer_) - len_, "%.*s: %.*s\r\n",
-                 header_->first.size(), header_->first.data(),
-                 header_->second.size(), header_->second.data());
-    if (len_ + len < sizeof(buffer_) - 3) {
-      len_ += len;
-      ++header_;
-    } else if (len_ == 0) {
-      RTC_LOG(WARNING) << "discarding header that is too long: "
-                       << header_->first;
-      ++header_;
-    } else {
-      // Not enough room for the next header, write to network first.
-      return true;
-    }
-  }
-  // End of headers
-  len_ += strcpyn(buffer_ + len_, sizeof(buffer_) - len_, "\r\n");
-  return false;
-}
-
-void HttpBase::do_complete(HttpError err) {
-  RTC_DCHECK(mode_ != HM_NONE);
-  HttpMode mode = mode_;
-  mode_ = HM_NONE;
-  if (data_ && data_->document) {
-    data_->document->SignalEvent.disconnect(this);
-  }
-  data_ = nullptr;
-  if (notify_) {
-    notify_->onHttpComplete(mode, err);
-  }
-}
-
-//
-// Stream Signals
-//
-
-void HttpBase::OnHttpStreamEvent(StreamInterface* stream,
-                                 int events,
-                                 int error) {
-  RTC_DCHECK(stream == http_stream_);
-  if ((events & SE_OPEN) && (mode_ == HM_CONNECT)) {
-    do_complete();
-    return;
-  }
-
-  if ((events & SE_WRITE) && (mode_ == HM_SEND)) {
-    flush_data();
-    return;
-  }
-
-  if ((events & SE_READ) && (mode_ == HM_RECV)) {
-    read_and_process_data();
-    return;
-  }
-
-  if ((events & SE_CLOSE) == 0)
-    return;
-
-  HttpError http_error = HandleStreamClose(error);
-  if (mode_ == HM_RECV) {
-    complete(http_error);
-  } else if (mode_ != HM_NONE) {
-    do_complete(http_error);
-  } else if (notify_) {
-    notify_->onHttpClosed(http_error);
-  }
-}
-
-void HttpBase::OnDocumentEvent(StreamInterface* stream, int events, int error) {
-  RTC_DCHECK(stream == data_->document.get());
-  if ((events & SE_WRITE) && (mode_ == HM_RECV)) {
-    read_and_process_data();
-    return;
-  }
-
-  if ((events & SE_READ) && (mode_ == HM_SEND)) {
-    flush_data();
-    return;
-  }
-
-  if (events & SE_CLOSE) {
-    RTC_LOG_F(LS_ERROR) << "Read error: " << error;
-    do_complete(HE_STREAM);
-    return;
-  }
-}
-
-//
-// HttpParser Implementation
-//
-
-HttpParser::ProcessResult HttpBase::ProcessLeader(const char* line,
-                                                  size_t len,
-                                                  HttpError* error) {
-  *error = data_->parseLeader(line, len);
-  return (HE_NONE == *error) ? PR_CONTINUE : PR_COMPLETE;
-}
-
-HttpParser::ProcessResult HttpBase::ProcessHeader(const char* name,
-                                                  size_t nlen,
-                                                  const char* value,
-                                                  size_t vlen,
-                                                  HttpError* error) {
-  std::string sname(name, nlen), svalue(value, vlen);
-  data_->addHeader(sname, svalue);
-  return PR_CONTINUE;
-}
-
-HttpParser::ProcessResult HttpBase::ProcessHeaderComplete(bool chunked,
-                                                          size_t& data_size,
-                                                          HttpError* error) {
-  if (notify_) {
-    *error = notify_->onHttpHeaderComplete(chunked, data_size);
-    // The request must not be aborted as a result of this callback.
-    RTC_DCHECK(nullptr != data_);
-  }
-  if ((HE_NONE == *error) && data_->document) {
-    data_->document->SignalEvent.connect(this, &HttpBase::OnDocumentEvent);
-  }
-  if (HE_NONE != *error) {
-    return PR_COMPLETE;
-  }
-  return PR_CONTINUE;
-}
-
-HttpParser::ProcessResult HttpBase::ProcessData(const char* data,
-                                                size_t len,
-                                                size_t& read,
-                                                HttpError* error) {
-  if (ignore_data_ || !data_->document) {
-    read = len;
-    return PR_CONTINUE;
-  }
-  int write_error = 0;
-  switch (data_->document->Write(data, len, &read, &write_error)) {
-    case SR_SUCCESS:
-      return PR_CONTINUE;
-    case SR_BLOCK:
-      return PR_BLOCK;
-    case SR_EOS:
-      RTC_LOG_F(LS_ERROR) << "Unexpected EOS";
-      *error = HE_STREAM;
-      return PR_COMPLETE;
-    case SR_ERROR:
-    default:
-      RTC_LOG_F(LS_ERROR) << "Write error: " << write_error;
-      *error = HE_STREAM;
-      return PR_COMPLETE;
-  }
-}
-
-void HttpBase::OnComplete(HttpError err) {
-  RTC_LOG_F(LS_VERBOSE);
-  do_complete(err);
-}
-
-}  // namespace rtc
diff --git a/rtc_base/httpbase.h b/rtc_base/httpbase.h
deleted file mode 100644
index b0e2425..0000000
--- a/rtc_base/httpbase.h
+++ /dev/null
@@ -1,189 +0,0 @@
-/*
- *  Copyright 2004 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 RTC_BASE_HTTPBASE_H_
-#define RTC_BASE_HTTPBASE_H_
-
-#include "rtc_base/httpcommon.h"
-
-namespace rtc {
-
-class StreamInterface;
-
-///////////////////////////////////////////////////////////////////////////////
-// HttpParser - Parses an HTTP stream provided via Process and end_of_input, and
-// generates events for:
-//  Structural Elements: Leader, Headers, Document Data
-//  Events: End of Headers, End of Document, Errors
-///////////////////////////////////////////////////////////////////////////////
-
-class HttpParser {
- public:
-  enum ProcessResult { PR_CONTINUE, PR_BLOCK, PR_COMPLETE };
-  HttpParser();
-  virtual ~HttpParser();
-
-  void reset();
-  ProcessResult Process(const char* buffer,
-                        size_t len,
-                        size_t* processed,
-                        HttpError* error);
-  bool is_valid_end_of_input() const;
-  void complete(HttpError err);
-
-  size_t GetDataRemaining() const { return data_size_; }
-
- protected:
-  ProcessResult ProcessLine(const char* line, size_t len, HttpError* error);
-
-  // HttpParser Interface
-  virtual ProcessResult ProcessLeader(const char* line,
-                                      size_t len,
-                                      HttpError* error) = 0;
-  virtual ProcessResult ProcessHeader(const char* name,
-                                      size_t nlen,
-                                      const char* value,
-                                      size_t vlen,
-                                      HttpError* error) = 0;
-  virtual ProcessResult ProcessHeaderComplete(bool chunked,
-                                              size_t& data_size,
-                                              HttpError* error) = 0;
-  virtual ProcessResult ProcessData(const char* data,
-                                    size_t len,
-                                    size_t& read,
-                                    HttpError* error) = 0;
-  virtual void OnComplete(HttpError err) = 0;
-
- private:
-  enum State {
-    ST_LEADER,
-    ST_HEADERS,
-    ST_CHUNKSIZE,
-    ST_CHUNKTERM,
-    ST_TRAILERS,
-    ST_DATA,
-    ST_COMPLETE
-  } state_;
-  bool chunked_;
-  size_t data_size_;
-};
-
-///////////////////////////////////////////////////////////////////////////////
-// IHttpNotify
-///////////////////////////////////////////////////////////////////////////////
-
-enum HttpMode { HM_NONE, HM_CONNECT, HM_RECV, HM_SEND };
-
-class IHttpNotify {
- public:
-  virtual ~IHttpNotify() {}
-  virtual HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) = 0;
-  virtual void onHttpComplete(HttpMode mode, HttpError err) = 0;
-  virtual void onHttpClosed(HttpError err) = 0;
-};
-
-///////////////////////////////////////////////////////////////////////////////
-// HttpBase - Provides a state machine for implementing HTTP-based components.
-// Attach HttpBase to a StreamInterface which represents a bidirectional HTTP
-// stream, and then call send() or recv() to initiate sending or receiving one
-// side of an HTTP transaction.  By default, HttpBase operates as an I/O pump,
-// moving data from the HTTP stream to the HttpData object and vice versa.
-// However, it can also operate in stream mode, in which case the user of the
-// stream interface drives I/O via calls to Read().
-///////////////////////////////////////////////////////////////////////////////
-
-class HttpBase : private HttpParser, public sigslot::has_slots<> {
- public:
-  HttpBase();
-  ~HttpBase() override;
-
-  void notify(IHttpNotify* notify) { notify_ = notify; }
-  bool attach(StreamInterface* stream);
-  StreamInterface* stream() { return http_stream_; }
-  StreamInterface* detach();
-  bool isConnected() const;
-
-  void send(HttpData* data);
-  void recv(HttpData* data);
-  void abort(HttpError err);
-
-  HttpMode mode() const { return mode_; }
-
-  void set_ignore_data(bool ignore) { ignore_data_ = ignore; }
-  bool ignore_data() const { return ignore_data_; }
-
- protected:
-  // Do cleanup when the http stream closes (error may be 0 for a clean
-  // shutdown), and return the error code to signal.
-  HttpError HandleStreamClose(int error);
-
-  // DoReceiveLoop acts as a data pump, pulling data from the http stream,
-  // pushing it through the HttpParser, and then populating the HttpData object
-  // based on the callbacks from the parser.  One of the most interesting
-  // callbacks is ProcessData, which provides the actual http document body.
-  // This data is then written to the HttpData::document.  As a result, data
-  // flows from the network to the document, with some incidental protocol
-  // parsing in between.
-  // Ideally, we would pass in the document* to DoReceiveLoop, to more easily
-  // support GetDocumentStream().  However, since the HttpParser is callback
-  // driven, we are forced to store the pointer somewhere until the callback
-  // is triggered.
-  // Returns true if the received document has finished, and
-  // HttpParser::complete should be called.
-  bool DoReceiveLoop(HttpError* err);
-
-  void read_and_process_data();
-  void flush_data();
-  bool queue_headers();
-  void do_complete(HttpError err = HE_NONE);
-
-  void OnHttpStreamEvent(StreamInterface* stream, int events, int error);
-  void OnDocumentEvent(StreamInterface* stream, int events, int error);
-
-  // HttpParser Interface
-  ProcessResult ProcessLeader(const char* line,
-                              size_t len,
-                              HttpError* error) override;
-  ProcessResult ProcessHeader(const char* name,
-                              size_t nlen,
-                              const char* value,
-                              size_t vlen,
-                              HttpError* error) override;
-  ProcessResult ProcessHeaderComplete(bool chunked,
-                                      size_t& data_size,
-                                      HttpError* error) override;
-  ProcessResult ProcessData(const char* data,
-                            size_t len,
-                            size_t& read,
-                            HttpError* error) override;
-  void OnComplete(HttpError err) override;
-
- private:
-  class DocumentStream;
-  friend class DocumentStream;
-
-  enum { kBufferSize = 32 * 1024 };
-
-  HttpMode mode_;
-  HttpData* data_;
-  IHttpNotify* notify_;
-  StreamInterface* http_stream_;
-  char buffer_[kBufferSize];
-  size_t len_;
-
-  bool ignore_data_, chunk_data_;
-  HttpData::const_iterator header_;
-};
-
-///////////////////////////////////////////////////////////////////////////////
-
-}  // namespace rtc
-
-#endif  // RTC_BASE_HTTPBASE_H_
diff --git a/rtc_base/httpbase_unittest.cc b/rtc_base/httpbase_unittest.cc
deleted file mode 100644
index 35321da..0000000
--- a/rtc_base/httpbase_unittest.cc
+++ /dev/null
@@ -1,358 +0,0 @@
-/*
- *  Copyright 2004 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 <algorithm>
-
-#include "rtc_base/gunit.h"
-#include "rtc_base/httpbase.h"
-#include "rtc_base/testutils.h"
-
-namespace rtc {
-
-const char* const kHttpResponse =
-    "HTTP/1.1 200\r\n"
-    "Connection: Keep-Alive\r\n"
-    "Content-Type: text/plain\r\n"
-    "Proxy-Authorization: 42\r\n"
-    "Transfer-Encoding: chunked\r\n"
-    "\r\n"
-    "00000008\r\n"
-    "Goodbye!\r\n"
-    "0\r\n\r\n";
-
-const char* const kHttpEmptyResponse =
-    "HTTP/1.1 200\r\n"
-    "Connection: Keep-Alive\r\n"
-    "Content-Length: 0\r\n"
-    "Proxy-Authorization: 42\r\n"
-    "\r\n";
-
-class HttpBaseTest : public testing::Test, public IHttpNotify {
- public:
-  enum EventType { E_HEADER_COMPLETE, E_COMPLETE, E_CLOSED };
-  struct Event {
-    EventType event;
-    bool chunked;
-    size_t data_size;
-    HttpMode mode;
-    HttpError err;
-  };
-  HttpBaseTest() : mem(nullptr), http_stream(nullptr) {}
-
-  void TearDown() override {
-    delete http_stream;
-    // Avoid an ASSERT, in case a test doesn't clean up properly
-    base.abort(HE_NONE);
-  }
-
-  HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) override {
-    RTC_LOG_F(LS_VERBOSE) << "chunked: " << chunked << " size: " << data_size;
-    Event e = {E_HEADER_COMPLETE, chunked, data_size, HM_NONE, HE_NONE};
-    events.push_back(e);
-    return HE_NONE;
-  }
-  void onHttpComplete(HttpMode mode, HttpError err) override {
-    RTC_LOG_F(LS_VERBOSE) << "mode: " << mode << " err: " << err;
-    Event e = {E_COMPLETE, false, 0, mode, err};
-    events.push_back(e);
-  }
-  void onHttpClosed(HttpError err) override {
-    RTC_LOG_F(LS_VERBOSE) << "err: " << err;
-    Event e = {E_CLOSED, false, 0, HM_NONE, err};
-    events.push_back(e);
-  }
-
-  void SetupSource(const char* response);
-
-  void VerifyHeaderComplete(size_t event_count, bool empty_doc);
-  void VerifyDocumentContents(const char* expected_data,
-                              size_t expected_length = SIZE_UNKNOWN);
-
-  void VerifyDocumentStreamIsOpening();
-  void VerifyDocumentStreamOpenEvent();
-  void ReadDocumentStreamData(const char* expected_data);
-  void VerifyDocumentStreamIsEOS();
-
-  void SetupDocument(const char* response);
-  void VerifySourceContents(const char* expected_data,
-                            size_t expected_length = SIZE_UNKNOWN);
-
-  void VerifyTransferComplete(HttpMode mode, HttpError error);
-
-  HttpBase base;
-  MemoryStream* mem;
-  HttpResponseData data;
-
-  // The source of http data, and source events
-  webrtc::testing::StreamSource src;
-  std::vector<Event> events;
-
-  // Stream events
-  StreamInterface* http_stream;
-  webrtc::testing::StreamSink sink;
-};
-
-void HttpBaseTest::SetupSource(const char* http_data) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  src.SetState(SS_OPENING);
-  src.QueueString(http_data);
-
-  base.notify(this);
-  base.attach(&src);
-  EXPECT_TRUE(events.empty());
-
-  src.SetState(SS_OPEN);
-  ASSERT_EQ(1U, events.size());
-  EXPECT_EQ(E_COMPLETE, events[0].event);
-  EXPECT_EQ(HM_CONNECT, events[0].mode);
-  EXPECT_EQ(HE_NONE, events[0].err);
-  events.clear();
-
-  mem = new MemoryStream;
-  data.document.reset(mem);
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyHeaderComplete(size_t event_count, bool empty_doc) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  ASSERT_EQ(event_count, events.size());
-  EXPECT_EQ(E_HEADER_COMPLETE, events[0].event);
-
-  std::string header;
-  EXPECT_EQ(HVER_1_1, data.version);
-  EXPECT_EQ(static_cast<uint32_t>(HC_OK), data.scode);
-  EXPECT_TRUE(data.hasHeader(HH_PROXY_AUTHORIZATION, &header));
-  EXPECT_EQ("42", header);
-  EXPECT_TRUE(data.hasHeader(HH_CONNECTION, &header));
-  EXPECT_EQ("Keep-Alive", header);
-
-  if (empty_doc) {
-    EXPECT_FALSE(events[0].chunked);
-    EXPECT_EQ(0U, events[0].data_size);
-
-    EXPECT_TRUE(data.hasHeader(HH_CONTENT_LENGTH, &header));
-    EXPECT_EQ("0", header);
-  } else {
-    EXPECT_TRUE(events[0].chunked);
-    EXPECT_EQ(SIZE_UNKNOWN, events[0].data_size);
-
-    EXPECT_TRUE(data.hasHeader(HH_CONTENT_TYPE, &header));
-    EXPECT_EQ("text/plain", header);
-    EXPECT_TRUE(data.hasHeader(HH_TRANSFER_ENCODING, &header));
-    EXPECT_EQ("chunked", header);
-  }
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyDocumentContents(const char* expected_data,
-                                          size_t expected_length) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  if (SIZE_UNKNOWN == expected_length) {
-    expected_length = strlen(expected_data);
-  }
-  EXPECT_EQ(mem, data.document.get());
-
-  size_t length;
-  mem->GetSize(&length);
-  EXPECT_EQ(expected_length, length);
-  EXPECT_TRUE(0 == memcmp(expected_data, mem->GetBuffer(), length));
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyDocumentStreamIsOpening() {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-  ASSERT_TRUE(nullptr != http_stream);
-  EXPECT_EQ(0, sink.Events(http_stream));
-  EXPECT_EQ(SS_OPENING, http_stream->GetState());
-
-  size_t read = 0;
-  char buffer[5] = {0};
-  EXPECT_EQ(SR_BLOCK,
-            http_stream->Read(buffer, sizeof(buffer), &read, nullptr));
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyDocumentStreamOpenEvent() {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  ASSERT_TRUE(nullptr != http_stream);
-  EXPECT_EQ(SE_OPEN | SE_READ, sink.Events(http_stream));
-  EXPECT_EQ(SS_OPEN, http_stream->GetState());
-
-  // HTTP headers haven't arrived yet
-  EXPECT_EQ(0U, events.size());
-  EXPECT_EQ(static_cast<uint32_t>(HC_INTERNAL_SERVER_ERROR), data.scode);
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::ReadDocumentStreamData(const char* expected_data) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  ASSERT_TRUE(nullptr != http_stream);
-  EXPECT_EQ(SS_OPEN, http_stream->GetState());
-
-  // Pump the HTTP I/O using Read, and verify the results.
-  size_t verified_length = 0;
-  const size_t expected_length = strlen(expected_data);
-  while (verified_length < expected_length) {
-    size_t read = 0;
-    char buffer[5] = {0};
-    size_t amt_to_read =
-        std::min(expected_length - verified_length, sizeof(buffer));
-    EXPECT_EQ(SR_SUCCESS,
-              http_stream->Read(buffer, amt_to_read, &read, nullptr));
-    EXPECT_EQ(amt_to_read, read);
-    EXPECT_TRUE(0 == memcmp(expected_data + verified_length, buffer, read));
-    verified_length += read;
-  }
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyDocumentStreamIsEOS() {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-
-  ASSERT_TRUE(nullptr != http_stream);
-  size_t read = 0;
-  char buffer[5] = {0};
-  EXPECT_EQ(SR_EOS, http_stream->Read(buffer, sizeof(buffer), &read, nullptr));
-  EXPECT_EQ(SS_CLOSED, http_stream->GetState());
-
-  // When EOS is caused by Read, we don't expect SE_CLOSE
-  EXPECT_EQ(0, sink.Events(http_stream));
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::SetupDocument(const char* document_data) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-  src.SetState(SS_OPEN);
-
-  base.notify(this);
-  base.attach(&src);
-  EXPECT_TRUE(events.empty());
-
-  if (document_data) {
-    // Note: we could just call data.set_success("text/plain", mem), but that
-    // won't allow us to use the chunked transfer encoding.
-    mem = new MemoryStream(document_data);
-    data.document.reset(mem);
-    data.setHeader(HH_CONTENT_TYPE, "text/plain");
-    data.setHeader(HH_TRANSFER_ENCODING, "chunked");
-  } else {
-    data.setHeader(HH_CONTENT_LENGTH, "0");
-  }
-  data.scode = HC_OK;
-  data.setHeader(HH_PROXY_AUTHORIZATION, "42");
-  data.setHeader(HH_CONNECTION, "Keep-Alive");
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifySourceContents(const char* expected_data,
-                                        size_t expected_length) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-  if (SIZE_UNKNOWN == expected_length) {
-    expected_length = strlen(expected_data);
-  }
-  std::string contents = src.ReadData();
-  EXPECT_EQ(expected_length, contents.length());
-  EXPECT_TRUE(0 == memcmp(expected_data, contents.data(), expected_length));
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-void HttpBaseTest::VerifyTransferComplete(HttpMode mode, HttpError error) {
-  RTC_LOG_F(LS_VERBOSE) << "Enter";
-  // Verify that http operation has completed
-  ASSERT_TRUE(events.size() > 0);
-  size_t last_event = events.size() - 1;
-  EXPECT_EQ(E_COMPLETE, events[last_event].event);
-  EXPECT_EQ(mode, events[last_event].mode);
-  EXPECT_EQ(error, events[last_event].err);
-  RTC_LOG_F(LS_VERBOSE) << "Exit";
-}
-
-//
-// Tests
-//
-
-TEST_F(HttpBaseTest, SupportsSend) {
-  // Queue response document
-  SetupDocument("Goodbye!");
-
-  // Begin send
-  base.send(&data);
-
-  // Send completed successfully
-  VerifyTransferComplete(HM_SEND, HE_NONE);
-  VerifySourceContents(kHttpResponse);
-}
-
-TEST_F(HttpBaseTest, SupportsSendNoDocument) {
-  // Queue response document
-  SetupDocument(nullptr);
-
-  // Begin send
-  base.send(&data);
-
-  // Send completed successfully
-  VerifyTransferComplete(HM_SEND, HE_NONE);
-  VerifySourceContents(kHttpEmptyResponse);
-}
-
-TEST_F(HttpBaseTest, SignalsCompleteOnInterruptedSend) {
-  // This test is attempting to expose a bug that occurs when a particular
-  // base objects is used for receiving, and then used for sending.  In
-  // particular, the HttpParser state is different after receiving.  Simulate
-  // that here.
-  SetupSource(kHttpResponse);
-  base.recv(&data);
-  VerifyTransferComplete(HM_RECV, HE_NONE);
-
-  src.Clear();
-  data.clear(true);
-  events.clear();
-  base.detach();
-
-  // Queue response document
-  SetupDocument("Goodbye!");
-
-  // Prevent entire response from being sent
-  const size_t kInterruptedLength = strlen(kHttpResponse) - 1;
-  src.SetWriteBlock(kInterruptedLength);
-
-  // Begin send
-  base.send(&data);
-
-  // Document is mostly complete, but no completion signal yet.
-  EXPECT_TRUE(events.empty());
-  VerifySourceContents(kHttpResponse, kInterruptedLength);
-
-  src.SetState(SS_CLOSED);
-
-  // Send completed with disconnect error, and no additional data.
-  VerifyTransferComplete(HM_SEND, HE_DISCONNECTED);
-  EXPECT_TRUE(src.ReadData().empty());
-}
-
-TEST_F(HttpBaseTest, SupportsReceiveViaDocumentPush) {
-  // Queue response document
-  SetupSource(kHttpResponse);
-
-  // Begin receive
-  base.recv(&data);
-
-  // Document completed successfully
-  VerifyHeaderComplete(2, false);
-  VerifyTransferComplete(HM_RECV, HE_NONE);
-  VerifyDocumentContents("Goodbye!");
-}
-
-}  // namespace rtc
diff --git a/rtc_base/httpcommon-inl.h b/rtc_base/httpcommon-inl.h
deleted file mode 100644
index bb72357..0000000
--- a/rtc_base/httpcommon-inl.h
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- *  Copyright 2004 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 RTC_BASE_HTTPCOMMON_INL_H_
-#define RTC_BASE_HTTPCOMMON_INL_H_
-
-#include "rtc_base/arraysize.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/httpcommon.h"
-
-namespace rtc {
-
-///////////////////////////////////////////////////////////////////////////////
-// Url
-///////////////////////////////////////////////////////////////////////////////
-
-template <class CTYPE>
-void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) {
-  if (ascnicmp(val, "http://", 7) == 0) {
-    val += 7;
-    len -= 7;
-    secure_ = false;
-  } else if (ascnicmp(val, "https://", 8) == 0) {
-    val += 8;
-    len -= 8;
-    secure_ = true;
-  } else {
-    clear();
-    return;
-  }
-  const CTYPE* path = strchrn(val, len, static_cast<CTYPE>('/'));
-  if (!path) {
-    path = val + len;
-  }
-  size_t address_length = (path - val);
-  do_set_address(val, address_length);
-  do_set_full_path(path, len - address_length);
-}
-
-template <class CTYPE>
-void Url<CTYPE>::do_set_address(const CTYPE* val, size_t len) {
-  if (const CTYPE* at = strchrn(val, len, static_cast<CTYPE>('@'))) {
-    // Everything before the @ is a user:password combo, so skip it.
-    len -= at - val + 1;
-    val = at + 1;
-  }
-  if (const CTYPE* colon = strchrn(val, len, static_cast<CTYPE>(':'))) {
-    host_.assign(val, colon - val);
-    // Note: In every case, we're guaranteed that colon is followed by a null,
-    // or non-numeric character.
-    port_ = static_cast<uint16_t>(::strtoul(colon + 1, nullptr, 10));
-    // TODO: Consider checking for invalid data following port number.
-  } else {
-    host_.assign(val, len);
-    port_ = HttpDefaultPort(secure_);
-  }
-}
-
-template <class CTYPE>
-void Url<CTYPE>::do_set_full_path(const CTYPE* val, size_t len) {
-  const CTYPE* query = strchrn(val, len, static_cast<CTYPE>('?'));
-  if (!query) {
-    query = val + len;
-  }
-  size_t path_length = (query - val);
-  if (0 == path_length) {
-    // TODO: consider failing in this case.
-    path_.assign(1, static_cast<CTYPE>('/'));
-  } else {
-    RTC_DCHECK(val[0] == static_cast<CTYPE>('/'));
-    path_.assign(val, path_length);
-  }
-  query_.assign(query, len - path_length);
-}
-
-template <class CTYPE>
-void Url<CTYPE>::do_get_url(string* val) const {
-  CTYPE protocol[9];
-  asccpyn(protocol, arraysize(protocol), secure_ ? "https://" : "http://");
-  val->append(protocol);
-  do_get_address(val);
-  do_get_full_path(val);
-}
-
-template <class CTYPE>
-void Url<CTYPE>::do_get_address(string* val) const {
-  val->append(host_);
-  if (port_ != HttpDefaultPort(secure_)) {
-    CTYPE format[5], port[32];
-    asccpyn(format, arraysize(format), ":%hu");
-    sprintfn(port, arraysize(port), format, port_);
-    val->append(port);
-  }
-}
-
-template <class CTYPE>
-void Url<CTYPE>::do_get_full_path(string* val) const {
-  val->append(path_);
-  val->append(query_);
-}
-
-template <class CTYPE>
-bool Url<CTYPE>::get_attribute(const string& name, string* value) const {
-  if (query_.empty())
-    return false;
-
-  std::string::size_type pos = query_.find(name, 1);
-  if (std::string::npos == pos)
-    return false;
-
-  pos += name.length() + 1;
-  if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos - 1]))
-    return false;
-
-  std::string::size_type end = query_.find(static_cast<CTYPE>('&'), pos);
-  if (std::string::npos == end) {
-    end = query_.length();
-  }
-  value->assign(query_.substr(pos, end - pos));
-  return true;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-}  // namespace rtc
-
-#endif  // RTC_BASE_HTTPCOMMON_INL_H_
diff --git a/rtc_base/httpcommon.cc b/rtc_base/httpcommon.cc
index 4ecb393..43831b7 100644
--- a/rtc_base/httpcommon.cc
+++ b/rtc_base/httpcommon.cc
@@ -18,17 +18,23 @@
 #include <security.h>
 #endif
 
+#include <ctype.h>  // for isspace
+#include <stdio.h>  // for sprintf
 #include <algorithm>
+#include <utility>  // for pair
+#include <vector>
 
 #include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
-#include "rtc_base/cryptstring.h"
-#include "rtc_base/httpcommon-inl.h"
+#include "rtc_base/cryptstring.h"  // for CryptString
 #include "rtc_base/httpcommon.h"
+#include "rtc_base/logging.h"
 #include "rtc_base/messagedigest.h"
 #include "rtc_base/socketaddress.h"
-#include "rtc_base/third_party/base64/base64.h"
-#include "rtc_base/zero_memory.h"
+#include "rtc_base/strings/string_builder.h"
+#include "rtc_base/stringutils.h"                // for strcpyn, _stricmp
+#include "rtc_base/third_party/base64/base64.h"  // for Base64
+#include "rtc_base/zero_memory.h"                // for ExplicitZeroMemory
 
 namespace rtc {
 namespace {
@@ -108,154 +114,9 @@
 #undef KLABEL
 #undef LASTLABEL
 #endif  // defined(WEBRTC_WIN)
-}  // namespace
 
-//////////////////////////////////////////////////////////////////////
-// Enum - TODO: expose globally later?
-//////////////////////////////////////////////////////////////////////
-
-bool find_string(size_t& index,
-                 const std::string& needle,
-                 const char* const haystack[],
-                 size_t max_index) {
-  for (index = 0; index < max_index; ++index) {
-    if (_stricmp(needle.c_str(), haystack[index]) == 0) {
-      return true;
-    }
-  }
-  return false;
-}
-
-template <class E>
-struct Enum {
-  static const char** Names;
-  static size_t Size;
-
-  static inline const char* Name(E val) { return Names[val]; }
-  static inline bool Parse(E& val, const std::string& name) {
-    size_t index;
-    if (!find_string(index, name, Names, Size))
-      return false;
-    val = static_cast<E>(index);
-    return true;
-  }
-
-  E val;
-
-  inline operator E&() { return val; }
-  inline Enum& operator=(E rhs) {
-    val = rhs;
-    return *this;
-  }
-
-  inline const char* name() const { return Name(val); }
-  inline bool assign(const std::string& name) { return Parse(val, name); }
-  inline Enum& operator=(const std::string& rhs) {
-    assign(rhs);
-    return *this;
-  }
-};
-
-#define ENUM(e, n)                 \
-  template <>                      \
-  const char** Enum<e>::Names = n; \
-  template <>                      \
-  size_t Enum<e>::Size = sizeof(n) / sizeof(n[0])
-
-//////////////////////////////////////////////////////////////////////
-// HttpCommon
-//////////////////////////////////////////////////////////////////////
-
-static const char* kHttpVersions[HVER_LAST + 1] = {"1.0", "1.1", "Unknown"};
-ENUM(HttpVersion, kHttpVersions);
-
-static const char* kHttpHeaders[HH_LAST + 1] = {
-    "Age",
-    "Cache-Control",
-    "Connection",
-    "Content-Disposition",
-    "Content-Length",
-    "Content-Range",
-    "Content-Type",
-    "Cookie",
-    "Date",
-    "ETag",
-    "Expires",
-    "Host",
-    "If-Modified-Since",
-    "If-None-Match",
-    "Keep-Alive",
-    "Last-Modified",
-    "Location",
-    "Proxy-Authenticate",
-    "Proxy-Authorization",
-    "Proxy-Connection",
-    "Range",
-    "Set-Cookie",
-    "TE",
-    "Trailers",
-    "Transfer-Encoding",
-    "Upgrade",
-    "User-Agent",
-    "WWW-Authenticate",
-};
-ENUM(HttpHeader, kHttpHeaders);
-
-const char* ToString(HttpVersion version) {
-  return Enum<HttpVersion>::Name(version);
-}
-
-bool FromString(HttpVersion& version, const std::string& str) {
-  return Enum<HttpVersion>::Parse(version, str);
-}
-
-const char* ToString(HttpHeader header) {
-  return Enum<HttpHeader>::Name(header);
-}
-
-bool FromString(HttpHeader& header, const std::string& str) {
-  return Enum<HttpHeader>::Parse(header, str);
-}
-
-bool HttpHeaderIsEndToEnd(HttpHeader header) {
-  switch (header) {
-    case HH_CONNECTION:
-    case HH_KEEP_ALIVE:
-    case HH_PROXY_AUTHENTICATE:
-    case HH_PROXY_AUTHORIZATION:
-    case HH_PROXY_CONNECTION:  // Note part of RFC... this is non-standard
-                               // header
-    case HH_TE:
-    case HH_TRAILERS:
-    case HH_TRANSFER_ENCODING:
-    case HH_UPGRADE:
-      return false;
-    default:
-      return true;
-  }
-}
-
-bool HttpHeaderIsCollapsible(HttpHeader header) {
-  switch (header) {
-    case HH_SET_COOKIE:
-    case HH_PROXY_AUTHENTICATE:
-    case HH_WWW_AUTHENTICATE:
-      return false;
-    default:
-      return true;
-  }
-}
-
-bool HttpShouldKeepAlive(const HttpData& data) {
-  std::string connection;
-  if ((data.hasHeader(HH_PROXY_CONNECTION, &connection) ||
-       data.hasHeader(HH_CONNECTION, &connection))) {
-    return (_stricmp(connection.c_str(), "Keep-Alive") == 0);
-  }
-  return (data.version >= HVER_1_1);
-}
-
-namespace {
+typedef std::pair<std::string, std::string> HttpAttribute;
+typedef std::vector<HttpAttribute> HttpAttributeList;
 
 inline bool IsEndOfAttributeName(size_t pos, size_t len, const char* data) {
   if (pos >= len)
@@ -272,8 +133,6 @@
   return false;
 }
 
-}  // anonymous namespace
-
 void HttpParseAttributes(const char* data,
                          size_t len,
                          HttpAttributeList& attributes) {
@@ -354,325 +213,6 @@
   return true;
 }
 
-bool HttpDateToSeconds(const std::string& date, time_t* seconds) {
-  const char* const kTimeZones[] = {
-      "UT",  "GMT", "EST", "EDT", "CST", "CDT", "MST", "MDT", "PST",
-      "PDT", "A",   "B",   "C",   "D",   "E",   "F",   "G",   "H",
-      "I",   "K",   "L",   "M",   "N",   "O",   "P",   "Q",   "R",
-      "S",   "T",   "U",   "V",   "W",   "X",   "Y"};
-  const int kTimeZoneOffsets[] = {
-      0,  0,  -5,  -4,  -6,  -5, -7, -6, -8, -7, -1, -2, -3, -4, -5, -6, -7,
-      -8, -9, -10, -11, -12, 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12};
-
-  RTC_DCHECK(nullptr != seconds);
-  struct tm tval;
-  memset(&tval, 0, sizeof(tval));
-  char month[4], zone[6];
-  memset(month, 0, sizeof(month));
-  memset(zone, 0, sizeof(zone));
-
-  if (7 != sscanf(date.c_str(), "%*3s, %d %3s %d %d:%d:%d %5c", &tval.tm_mday,
-                  month, &tval.tm_year, &tval.tm_hour, &tval.tm_min,
-                  &tval.tm_sec, zone)) {
-    return false;
-  }
-  switch (toupper(month[2])) {
-    case 'N':
-      tval.tm_mon = (month[1] == 'A') ? 0 : 5;
-      break;
-    case 'B':
-      tval.tm_mon = 1;
-      break;
-    case 'R':
-      tval.tm_mon = (month[0] == 'M') ? 2 : 3;
-      break;
-    case 'Y':
-      tval.tm_mon = 4;
-      break;
-    case 'L':
-      tval.tm_mon = 6;
-      break;
-    case 'G':
-      tval.tm_mon = 7;
-      break;
-    case 'P':
-      tval.tm_mon = 8;
-      break;
-    case 'T':
-      tval.tm_mon = 9;
-      break;
-    case 'V':
-      tval.tm_mon = 10;
-      break;
-    case 'C':
-      tval.tm_mon = 11;
-      break;
-  }
-  tval.tm_year -= 1900;
-  time_t gmt, non_gmt = mktime(&tval);
-  if ((zone[0] == '+') || (zone[0] == '-')) {
-    if (!isdigit(zone[1]) || !isdigit(zone[2]) || !isdigit(zone[3]) ||
-        !isdigit(zone[4])) {
-      return false;
-    }
-    int hours = (zone[1] - '0') * 10 + (zone[2] - '0');
-    int minutes = (zone[3] - '0') * 10 + (zone[4] - '0');
-    int offset = (hours * 60 + minutes) * 60;
-    gmt = non_gmt + ((zone[0] == '+') ? offset : -offset);
-  } else {
-    size_t zindex;
-    if (!find_string(zindex, zone, kTimeZones, arraysize(kTimeZones))) {
-      return false;
-    }
-    gmt = non_gmt + kTimeZoneOffsets[zindex] * 60 * 60;
-  }
-// TODO: Android should support timezone, see b/2441195
-#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID) || \
-    defined(BSD)
-  tm* tm_for_timezone = localtime(&gmt);
-  *seconds = gmt + tm_for_timezone->tm_gmtoff;
-#else
-#if defined(_MSC_VER) && _MSC_VER >= 1900
-  long timezone = 0;
-  _get_timezone(&timezone);
-#endif
-  *seconds = gmt - timezone;
-#endif
-  return true;
-}
-
-std::string HttpAddress(const SocketAddress& address, bool secure) {
-  return (address.port() == HttpDefaultPort(secure)) ? address.hostname()
-                                                     : address.ToString();
-}
-
-//////////////////////////////////////////////////////////////////////
-// HttpData
-//////////////////////////////////////////////////////////////////////
-
-HttpData::HttpData() : version(HVER_1_1) {}
-
-HttpData::~HttpData() = default;
-
-void HttpData::clear(bool release_document) {
-  // Clear headers first, since releasing a document may have far-reaching
-  // effects.
-  headers_.clear();
-  if (release_document) {
-    document.reset();
-  }
-}
-
-void HttpData::changeHeader(const std::string& name,
-                            const std::string& value,
-                            HeaderCombine combine) {
-  if (combine == HC_AUTO) {
-    HttpHeader header;
-    // Unrecognized headers are collapsible
-    combine = !FromString(header, name) || HttpHeaderIsCollapsible(header)
-                  ? HC_YES
-                  : HC_NO;
-  } else if (combine == HC_REPLACE) {
-    headers_.erase(name);
-    combine = HC_NO;
-  }
-  // At this point, combine is one of (YES, NO, NEW)
-  if (combine != HC_NO) {
-    HeaderMap::iterator it = headers_.find(name);
-    if (it != headers_.end()) {
-      if (combine == HC_YES) {
-        it->second.append(",");
-        it->second.append(value);
-      }
-      return;
-    }
-  }
-  headers_.insert(HeaderMap::value_type(name, value));
-}
-
-size_t HttpData::clearHeader(const std::string& name) {
-  return headers_.erase(name);
-}
-
-HttpData::iterator HttpData::clearHeader(iterator header) {
-  iterator deprecated = header++;
-  headers_.erase(deprecated);
-  return header;
-}
-
-bool HttpData::hasHeader(const std::string& name, std::string* value) const {
-  HeaderMap::const_iterator it = headers_.find(name);
-  if (it == headers_.end()) {
-    return false;
-  } else if (value) {
-    *value = it->second;
-  }
-  return true;
-}
-
-void HttpData::setContent(const std::string& content_type,
-                          StreamInterface* document) {
-  setHeader(HH_CONTENT_TYPE, content_type);
-  setDocumentAndLength(document);
-}
-
-void HttpData::setDocumentAndLength(StreamInterface* document) {
-  // TODO: Consider calling Rewind() here?
-  RTC_DCHECK(!hasHeader(HH_CONTENT_LENGTH, nullptr));
-  RTC_DCHECK(!hasHeader(HH_TRANSFER_ENCODING, nullptr));
-  RTC_DCHECK(document != nullptr);
-  this->document.reset(document);
-  size_t content_length = 0;
-  if (this->document->GetAvailable(&content_length)) {
-    char buffer[32];
-    sprintfn(buffer, sizeof(buffer), "%d", content_length);
-    setHeader(HH_CONTENT_LENGTH, buffer);
-  } else {
-    setHeader(HH_TRANSFER_ENCODING, "chunked");
-  }
-}
-
-//
-// HttpRequestData
-//
-
-void HttpRequestData::clear(bool release_document) {
-  path.clear();
-  HttpData::clear(release_document);
-}
-
-size_t HttpRequestData::formatLeader(char* buffer, size_t size) const {
-  RTC_DCHECK(path.find(' ') == std::string::npos);
-  return sprintfn(buffer, size, "GET %.*s HTTP/%s", path.size(), path.data(),
-                  ToString(version));
-}
-
-HttpError HttpRequestData::parseLeader(const char* line, size_t len) {
-  unsigned int vmajor, vminor;
-  int vend, dstart, dend;
-  // sscanf isn't safe with strings that aren't null-terminated, and there is
-  // no guarantee that |line| is. Create a local copy that is null-terminated.
-  std::string line_str(line, len);
-  line = line_str.c_str();
-  if ((sscanf(line, "%*s%n %n%*s%n HTTP/%u.%u", &vend, &dstart, &dend, &vmajor,
-              &vminor) != 2) ||
-      (vmajor != 1)) {
-    return HE_PROTOCOL;
-  }
-  if (vminor == 0) {
-    version = HVER_1_0;
-  } else if (vminor == 1) {
-    version = HVER_1_1;
-  } else {
-    return HE_PROTOCOL;
-  }
-  if (vend != 3 || memcmp(line, "GET", 3)) {
-    return HE_PROTOCOL;  // !?! HC_METHOD_NOT_SUPPORTED?
-  }
-  path.assign(line + dstart, line + dend);
-  return HE_NONE;
-}
-
-bool HttpRequestData::getAbsoluteUri(std::string* uri) const {
-  Url<char> url(path);
-  if (url.valid()) {
-    uri->assign(path);
-    return true;
-  }
-  std::string host;
-  if (!hasHeader(HH_HOST, &host))
-    return false;
-  url.set_address(host);
-  url.set_full_path(path);
-  uri->assign(url.url());
-  return url.valid();
-}
-
-bool HttpRequestData::getRelativeUri(std::string* host,
-                                     std::string* path) const {
-  Url<char> url(this->path);
-  if (url.valid()) {
-    host->assign(url.address());
-    path->assign(url.full_path());
-    return true;
-  }
-  if (!hasHeader(HH_HOST, host))
-    return false;
-  path->assign(this->path);
-  return true;
-}
-
-//
-// HttpResponseData
-//
-
-void HttpResponseData::clear(bool release_document) {
-  scode = HC_INTERNAL_SERVER_ERROR;
-  message.clear();
-  HttpData::clear(release_document);
-}
-
-void HttpResponseData::set_success(uint32_t scode) {
-  this->scode = scode;
-  message.clear();
-  setHeader(HH_CONTENT_LENGTH, "0", false);
-}
-
-void HttpResponseData::set_error(uint32_t scode) {
-  this->scode = scode;
-  message.clear();
-  setHeader(HH_CONTENT_LENGTH, "0", false);
-}
-
-size_t HttpResponseData::formatLeader(char* buffer, size_t size) const {
-  size_t len = sprintfn(buffer, size, "HTTP/%s %lu", ToString(version), scode);
-  if (!message.empty()) {
-    len += sprintfn(buffer + len, size - len, " %.*s", message.size(),
-                    message.data());
-  }
-  return len;
-}
-
-HttpError HttpResponseData::parseLeader(const char* line, size_t len) {
-  size_t pos = 0;
-  unsigned int vmajor, vminor, temp_scode;
-  int temp_pos;
-  // sscanf isn't safe with strings that aren't null-terminated, and there is
-  // no guarantee that |line| is. Create a local copy that is null-terminated.
-  std::string line_str(line, len);
-  line = line_str.c_str();
-  if (sscanf(line, "HTTP %u%n", &temp_scode, &temp_pos) == 1) {
-    // This server's response has no version. :( NOTE: This happens for every
-    // response to requests made from Chrome plugins, regardless of the server's
-    // behaviour.
-    RTC_LOG(LS_VERBOSE) << "HTTP version missing from response";
-    version = HVER_UNKNOWN;
-  } else if ((sscanf(line, "HTTP/%u.%u %u%n", &vmajor, &vminor, &temp_scode,
-                     &temp_pos) == 3) &&
-             (vmajor == 1)) {
-    // This server's response does have a version.
-    if (vminor == 0) {
-      version = HVER_1_0;
-    } else if (vminor == 1) {
-      version = HVER_1_1;
-    } else {
-      return HE_PROTOCOL;
-    }
-  } else {
-    return HE_PROTOCOL;
-  }
-  scode = temp_scode;
-  pos = static_cast<size_t>(temp_pos);
-  while ((pos < len) && isspace(static_cast<unsigned char>(line[pos])))
-    ++pos;
-  message.assign(line + pos, len - pos);
-  return HE_NONE;
-}
-
-//////////////////////////////////////////////////////////////////////
-// Http Authentication
-//////////////////////////////////////////////////////////////////////
-
 std::string quote(const std::string& str) {
   std::string result;
   result.push_back('"');
@@ -706,6 +246,8 @@
 };
 #endif  // WEBRTC_WIN
 
+}  // anonymous namespace
+
 HttpAuthResult HttpAuthenticate(const char* challenge,
                                 size_t len,
                                 const SocketAddress& server,
@@ -798,7 +340,7 @@
     std::string HA2 = MD5(A2);
     std::string dig_response = MD5(HA1 + ":" + middle + ":" + HA2);
 
-    std::stringstream ss;
+    rtc::StringBuilder ss;
     ss << auth_method;
     ss << " username=" << quote(username);
     ss << ", realm=" << quote(realm);
diff --git a/rtc_base/httpcommon.h b/rtc_base/httpcommon.h
index 11c01ca..fbad280 100644
--- a/rtc_base/httpcommon.h
+++ b/rtc_base/httpcommon.h
@@ -11,14 +11,7 @@
 #ifndef RTC_BASE_HTTPCOMMON_H_
 #define RTC_BASE_HTTPCOMMON_H_
 
-#include <map>
-#include <memory>
 #include <string>
-#include <vector>
-
-#include "rtc_base/checks.h"
-#include "rtc_base/stream.h"
-#include "rtc_base/stringutils.h"
 
 namespace rtc {
 
@@ -26,347 +19,6 @@
 class SocketAddress;
 
 //////////////////////////////////////////////////////////////////////
-// Constants
-//////////////////////////////////////////////////////////////////////
-
-enum HttpCode {
-  HC_OK = 200,
-  HC_INTERNAL_SERVER_ERROR = 500,
-};
-
-enum HttpVersion { HVER_1_0, HVER_1_1, HVER_UNKNOWN, HVER_LAST = HVER_UNKNOWN };
-
-enum HttpError {
-  HE_NONE,
-  HE_PROTOCOL,             // Received non-valid HTTP data
-  HE_DISCONNECTED,         // Connection closed unexpectedly
-  HE_OVERFLOW,             // Received too much data for internal buffers
-  HE_CONNECT_FAILED,       // The socket failed to connect.
-  HE_SOCKET_ERROR,         // An error occurred on a connected socket
-  HE_SHUTDOWN,             // Http object is being destroyed
-  HE_OPERATION_CANCELLED,  // Connection aborted locally
-  HE_AUTH,                 // Proxy Authentication Required
-  HE_CERTIFICATE_EXPIRED,  // During SSL negotiation
-  HE_STREAM,               // Problem reading or writing to the document
-  HE_CACHE,                // Problem reading from cache
-  HE_DEFAULT
-};
-
-enum HttpHeader {
-  HH_AGE,
-  HH_CACHE_CONTROL,
-  HH_CONNECTION,
-  HH_CONTENT_DISPOSITION,
-  HH_CONTENT_LENGTH,
-  HH_CONTENT_RANGE,
-  HH_CONTENT_TYPE,
-  HH_COOKIE,
-  HH_DATE,
-  HH_ETAG,
-  HH_EXPIRES,
-  HH_HOST,
-  HH_IF_MODIFIED_SINCE,
-  HH_IF_NONE_MATCH,
-  HH_KEEP_ALIVE,
-  HH_LAST_MODIFIED,
-  HH_LOCATION,
-  HH_PROXY_AUTHENTICATE,
-  HH_PROXY_AUTHORIZATION,
-  HH_PROXY_CONNECTION,
-  HH_RANGE,
-  HH_SET_COOKIE,
-  HH_TE,
-  HH_TRAILERS,
-  HH_TRANSFER_ENCODING,
-  HH_UPGRADE,
-  HH_USER_AGENT,
-  HH_WWW_AUTHENTICATE,
-  HH_LAST = HH_WWW_AUTHENTICATE
-};
-
-const uint16_t HTTP_DEFAULT_PORT = 80;
-const uint16_t HTTP_SECURE_PORT = 443;
-
-//////////////////////////////////////////////////////////////////////
-// Utility Functions
-//////////////////////////////////////////////////////////////////////
-
-const char* ToString(HttpVersion version);
-bool FromString(HttpVersion& version, const std::string& str);
-
-const char* ToString(HttpHeader header);
-bool FromString(HttpHeader& header, const std::string& str);
-
-bool HttpHeaderIsEndToEnd(HttpHeader header);
-bool HttpHeaderIsCollapsible(HttpHeader header);
-
-struct HttpData;
-bool HttpShouldKeepAlive(const HttpData& data);
-
-typedef std::pair<std::string, std::string> HttpAttribute;
-typedef std::vector<HttpAttribute> HttpAttributeList;
-void HttpParseAttributes(const char* data,
-                         size_t len,
-                         HttpAttributeList& attributes);
-bool HttpHasAttribute(const HttpAttributeList& attributes,
-                      const std::string& name,
-                      std::string* value);
-bool HttpHasNthAttribute(HttpAttributeList& attributes,
-                         size_t index,
-                         std::string* name,
-                         std::string* value);
-
-// Convert RFC1123 date (DoW, DD Mon YYYY HH:MM:SS TZ) to unix timestamp
-bool HttpDateToSeconds(const std::string& date, time_t* seconds);
-
-inline uint16_t HttpDefaultPort(bool secure) {
-  return secure ? HTTP_SECURE_PORT : HTTP_DEFAULT_PORT;
-}
-
-// Returns the http server notation for a given address
-std::string HttpAddress(const SocketAddress& address, bool secure);
-
-// functional for insensitive std::string compare
-struct iless {
-  bool operator()(const std::string& lhs, const std::string& rhs) const {
-    return (::_stricmp(lhs.c_str(), rhs.c_str()) < 0);
-  }
-};
-
-// put quotes around a string and escape any quotes inside it
-std::string quote(const std::string& str);
-
-//////////////////////////////////////////////////////////////////////
-// Url
-//////////////////////////////////////////////////////////////////////
-
-template <class CTYPE>
-class Url {
- public:
-  typedef typename Traits<CTYPE>::string string;
-
-  // TODO: Implement Encode/Decode
-  static int Encode(const CTYPE* source, CTYPE* destination, size_t len);
-  static int Encode(const string& source, string& destination);
-  static int Decode(const CTYPE* source, CTYPE* destination, size_t len);
-  static int Decode(const string& source, string& destination);
-
-  Url(const string& url) { do_set_url(url.c_str(), url.size()); }
-  Url(const string& path, const string& host, uint16_t port = HTTP_DEFAULT_PORT)
-      : host_(host), port_(port), secure_(HTTP_SECURE_PORT == port) {
-    set_full_path(path);
-  }
-
-  bool valid() const { return !host_.empty(); }
-  void clear() {
-    host_.clear();
-    port_ = HTTP_DEFAULT_PORT;
-    secure_ = false;
-    path_.assign(1, static_cast<CTYPE>('/'));
-    query_.clear();
-  }
-
-  void set_url(const string& val) { do_set_url(val.c_str(), val.size()); }
-  string url() const {
-    string val;
-    do_get_url(&val);
-    return val;
-  }
-
-  void set_address(const string& val) {
-    do_set_address(val.c_str(), val.size());
-  }
-  string address() const {
-    string val;
-    do_get_address(&val);
-    return val;
-  }
-
-  void set_full_path(const string& val) {
-    do_set_full_path(val.c_str(), val.size());
-  }
-  string full_path() const {
-    string val;
-    do_get_full_path(&val);
-    return val;
-  }
-
-  void set_host(const string& val) { host_ = val; }
-  const string& host() const { return host_; }
-
-  void set_port(uint16_t val) { port_ = val; }
-  uint16_t port() const { return port_; }
-
-  void set_secure(bool val) { secure_ = val; }
-  bool secure() const { return secure_; }
-
-  void set_path(const string& val) {
-    if (val.empty()) {
-      path_.assign(1, static_cast<CTYPE>('/'));
-    } else {
-      RTC_DCHECK(val[0] == static_cast<CTYPE>('/'));
-      path_ = val;
-    }
-  }
-  const string& path() const { return path_; }
-
-  void set_query(const string& val) {
-    RTC_DCHECK(val.empty() || (val[0] == static_cast<CTYPE>('?')));
-    query_ = val;
-  }
-  const string& query() const { return query_; }
-
-  bool get_attribute(const string& name, string* value) const;
-
- private:
-  void do_set_url(const CTYPE* val, size_t len);
-  void do_set_address(const CTYPE* val, size_t len);
-  void do_set_full_path(const CTYPE* val, size_t len);
-
-  void do_get_url(string* val) const;
-  void do_get_address(string* val) const;
-  void do_get_full_path(string* val) const;
-
-  string host_, path_, query_;
-  uint16_t port_;
-  bool secure_;
-};
-
-//////////////////////////////////////////////////////////////////////
-// HttpData
-//////////////////////////////////////////////////////////////////////
-
-struct HttpData {
-  typedef std::multimap<std::string, std::string, iless> HeaderMap;
-  typedef HeaderMap::const_iterator const_iterator;
-  typedef HeaderMap::iterator iterator;
-
-  HttpVersion version;
-  std::unique_ptr<StreamInterface> document;
-
-  HttpData();
-
-  enum HeaderCombine { HC_YES, HC_NO, HC_AUTO, HC_REPLACE, HC_NEW };
-  void changeHeader(const std::string& name,
-                    const std::string& value,
-                    HeaderCombine combine);
-  inline void addHeader(const std::string& name,
-                        const std::string& value,
-                        bool append = true) {
-    changeHeader(name, value, append ? HC_AUTO : HC_NO);
-  }
-  inline void setHeader(const std::string& name,
-                        const std::string& value,
-                        bool overwrite = true) {
-    changeHeader(name, value, overwrite ? HC_REPLACE : HC_NEW);
-  }
-  // Returns count of erased headers
-  size_t clearHeader(const std::string& name);
-  // Returns iterator to next header
-  iterator clearHeader(iterator header);
-
-  // keep in mind, this may not do what you want in the face of multiple headers
-  bool hasHeader(const std::string& name, std::string* value) const;
-
-  inline const_iterator begin() const { return headers_.begin(); }
-  inline const_iterator end() const { return headers_.end(); }
-  inline iterator begin() { return headers_.begin(); }
-  inline iterator end() { return headers_.end(); }
-  inline const_iterator begin(const std::string& name) const {
-    return headers_.lower_bound(name);
-  }
-  inline const_iterator end(const std::string& name) const {
-    return headers_.upper_bound(name);
-  }
-  inline iterator begin(const std::string& name) {
-    return headers_.lower_bound(name);
-  }
-  inline iterator end(const std::string& name) {
-    return headers_.upper_bound(name);
-  }
-
-  // Convenience methods using HttpHeader
-  inline void changeHeader(HttpHeader header,
-                           const std::string& value,
-                           HeaderCombine combine) {
-    changeHeader(ToString(header), value, combine);
-  }
-  inline void addHeader(HttpHeader header,
-                        const std::string& value,
-                        bool append = true) {
-    addHeader(ToString(header), value, append);
-  }
-  inline void setHeader(HttpHeader header,
-                        const std::string& value,
-                        bool overwrite = true) {
-    setHeader(ToString(header), value, overwrite);
-  }
-  inline void clearHeader(HttpHeader header) { clearHeader(ToString(header)); }
-  inline bool hasHeader(HttpHeader header, std::string* value) const {
-    return hasHeader(ToString(header), value);
-  }
-  inline const_iterator begin(HttpHeader header) const {
-    return headers_.lower_bound(ToString(header));
-  }
-  inline const_iterator end(HttpHeader header) const {
-    return headers_.upper_bound(ToString(header));
-  }
-  inline iterator begin(HttpHeader header) {
-    return headers_.lower_bound(ToString(header));
-  }
-  inline iterator end(HttpHeader header) {
-    return headers_.upper_bound(ToString(header));
-  }
-
-  void setContent(const std::string& content_type, StreamInterface* document);
-  void setDocumentAndLength(StreamInterface* document);
-
-  virtual size_t formatLeader(char* buffer, size_t size) const = 0;
-  virtual HttpError parseLeader(const char* line, size_t len) = 0;
-
- protected:
-  virtual ~HttpData();
-  void clear(bool release_document);
-
- private:
-  HeaderMap headers_;
-};
-
-struct HttpRequestData : public HttpData {
-  std::string path;
-
-  HttpRequestData() {}
-
-  void clear(bool release_document);
-
-  size_t formatLeader(char* buffer, size_t size) const override;
-  HttpError parseLeader(const char* line, size_t len) override;
-
-  bool getAbsoluteUri(std::string* uri) const;
-  bool getRelativeUri(std::string* host, std::string* path) const;
-};
-
-struct HttpResponseData : public HttpData {
-  uint32_t scode;
-  std::string message;
-
-  HttpResponseData() : scode(HC_INTERNAL_SERVER_ERROR) {}
-  void clear(bool release_document);
-
-  // Convenience methods
-  void set_success(uint32_t scode = HC_OK);
-  void set_error(uint32_t scode);
-
-  size_t formatLeader(char* buffer, size_t size) const override;
-  HttpError parseLeader(const char* line, size_t len) override;
-};
-
-struct HttpTransaction {
-  HttpRequestData request;
-  HttpResponseData response;
-};
-
-//////////////////////////////////////////////////////////////////////
 // Http Authentication
 //////////////////////////////////////////////////////////////////////
 
diff --git a/rtc_base/httpcommon_unittest.cc b/rtc_base/httpcommon_unittest.cc
deleted file mode 100644
index ed7f242..0000000
--- a/rtc_base/httpcommon_unittest.cc
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- *  Copyright 2004 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 "rtc_base/httpcommon.h"
-#include "rtc_base/gunit.h"
-#include "rtc_base/httpcommon-inl.h"
-
-namespace rtc {
-
-#define TEST_PROTOCOL "http://"
-#define TEST_HOST "www.google.com"
-#define TEST_PATH "/folder/file.html"
-#define TEST_QUERY "?query=x&attr=y"
-#define TEST_URL TEST_PROTOCOL TEST_HOST TEST_PATH TEST_QUERY
-
-TEST(Url, DecomposesUrls) {
-  Url<char> url(TEST_URL);
-  EXPECT_TRUE(url.valid());
-  EXPECT_FALSE(url.secure());
-  EXPECT_STREQ(TEST_HOST, url.host().c_str());
-  EXPECT_EQ(80, url.port());
-  EXPECT_STREQ(TEST_PATH, url.path().c_str());
-  EXPECT_STREQ(TEST_QUERY, url.query().c_str());
-  EXPECT_STREQ(TEST_HOST, url.address().c_str());
-  EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
-  EXPECT_STREQ(TEST_URL, url.url().c_str());
-}
-
-TEST(Url, ComposesUrls) {
-  // Set in constructor
-  Url<char> url(TEST_PATH TEST_QUERY, TEST_HOST, 80);
-  EXPECT_TRUE(url.valid());
-  EXPECT_FALSE(url.secure());
-  EXPECT_STREQ(TEST_HOST, url.host().c_str());
-  EXPECT_EQ(80, url.port());
-  EXPECT_STREQ(TEST_PATH, url.path().c_str());
-  EXPECT_STREQ(TEST_QUERY, url.query().c_str());
-  EXPECT_STREQ(TEST_HOST, url.address().c_str());
-  EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
-  EXPECT_STREQ(TEST_URL, url.url().c_str());
-
-  url.clear();
-  EXPECT_FALSE(url.valid());
-  EXPECT_FALSE(url.secure());
-  EXPECT_STREQ("", url.host().c_str());
-  EXPECT_EQ(80, url.port());
-  EXPECT_STREQ("/", url.path().c_str());
-  EXPECT_STREQ("", url.query().c_str());
-
-  // Set component-wise
-  url.set_host(TEST_HOST);
-  url.set_port(80);
-  url.set_path(TEST_PATH);
-  url.set_query(TEST_QUERY);
-  EXPECT_TRUE(url.valid());
-  EXPECT_FALSE(url.secure());
-  EXPECT_STREQ(TEST_HOST, url.host().c_str());
-  EXPECT_EQ(80, url.port());
-  EXPECT_STREQ(TEST_PATH, url.path().c_str());
-  EXPECT_STREQ(TEST_QUERY, url.query().c_str());
-  EXPECT_STREQ(TEST_HOST, url.address().c_str());
-  EXPECT_STREQ(TEST_PATH TEST_QUERY, url.full_path().c_str());
-  EXPECT_STREQ(TEST_URL, url.url().c_str());
-}
-
-TEST(Url, EnsuresNonEmptyPath) {
-  Url<char> url(TEST_PROTOCOL TEST_HOST);
-  EXPECT_TRUE(url.valid());
-  EXPECT_STREQ("/", url.path().c_str());
-
-  url.clear();
-  EXPECT_STREQ("/", url.path().c_str());
-  url.set_path("");
-  EXPECT_STREQ("/", url.path().c_str());
-
-  url.clear();
-  EXPECT_STREQ("/", url.path().c_str());
-  url.set_full_path("");
-  EXPECT_STREQ("/", url.path().c_str());
-}
-
-TEST(Url, GetQueryAttributes) {
-  Url<char> url(TEST_URL);
-  std::string value;
-  EXPECT_TRUE(url.get_attribute("query", &value));
-  EXPECT_STREQ("x", value.c_str());
-  value.clear();
-  EXPECT_TRUE(url.get_attribute("attr", &value));
-  EXPECT_STREQ("y", value.c_str());
-  value.clear();
-  EXPECT_FALSE(url.get_attribute("Query", &value));
-  EXPECT_TRUE(value.empty());
-}
-
-TEST(Url, SkipsUserAndPassword) {
-  Url<char> url("https://mail.google.com:pwd@badsite.com:12345/asdf");
-  EXPECT_TRUE(url.valid());
-  EXPECT_TRUE(url.secure());
-  EXPECT_STREQ("badsite.com", url.host().c_str());
-  EXPECT_EQ(12345, url.port());
-  EXPECT_STREQ("/asdf", url.path().c_str());
-  EXPECT_STREQ("badsite.com:12345", url.address().c_str());
-}
-
-TEST(Url, SkipsUser) {
-  Url<char> url("https://mail.google.com@badsite.com:12345/asdf");
-  EXPECT_TRUE(url.valid());
-  EXPECT_TRUE(url.secure());
-  EXPECT_STREQ("badsite.com", url.host().c_str());
-  EXPECT_EQ(12345, url.port());
-  EXPECT_STREQ("/asdf", url.path().c_str());
-  EXPECT_STREQ("badsite.com:12345", url.address().c_str());
-}
-
-TEST(HttpResponseData, parseLeaderHttp1_0) {
-  static const char kResponseString[] = "HTTP/1.0 200 OK";
-  HttpResponseData response;
-  EXPECT_EQ(HE_NONE,
-            response.parseLeader(kResponseString, sizeof(kResponseString) - 1));
-  EXPECT_EQ(HVER_1_0, response.version);
-  EXPECT_EQ(200U, response.scode);
-}
-
-TEST(HttpResponseData, parseLeaderHttp1_1) {
-  static const char kResponseString[] = "HTTP/1.1 200 OK";
-  HttpResponseData response;
-  EXPECT_EQ(HE_NONE,
-            response.parseLeader(kResponseString, sizeof(kResponseString) - 1));
-  EXPECT_EQ(HVER_1_1, response.version);
-  EXPECT_EQ(200U, response.scode);
-}
-
-TEST(HttpResponseData, parseLeaderHttpUnknown) {
-  static const char kResponseString[] = "HTTP 200 OK";
-  HttpResponseData response;
-  EXPECT_EQ(HE_NONE,
-            response.parseLeader(kResponseString, sizeof(kResponseString) - 1));
-  EXPECT_EQ(HVER_UNKNOWN, response.version);
-  EXPECT_EQ(200U, response.scode);
-}
-
-TEST(HttpResponseData, parseLeaderHttpFailure) {
-  static const char kResponseString[] = "HTTP/1.1 503 Service Unavailable";
-  HttpResponseData response;
-  EXPECT_EQ(HE_NONE,
-            response.parseLeader(kResponseString, sizeof(kResponseString) - 1));
-  EXPECT_EQ(HVER_1_1, response.version);
-  EXPECT_EQ(503U, response.scode);
-}
-
-TEST(HttpResponseData, parseLeaderHttpInvalid) {
-  static const char kResponseString[] = "Durrrrr, what's HTTP?";
-  HttpResponseData response;
-  EXPECT_EQ(HE_PROTOCOL,
-            response.parseLeader(kResponseString, sizeof(kResponseString) - 1));
-}
-
-}  // namespace rtc
diff --git a/rtc_base/httpserver.cc b/rtc_base/httpserver.cc
deleted file mode 100644
index 0e536c3..0000000
--- a/rtc_base/httpserver.cc
+++ /dev/null
@@ -1,270 +0,0 @@
-/*
- *  Copyright 2004 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 <algorithm>
-
-#include "rtc_base/httpcommon-inl.h"
-
-#include "rtc_base/asyncsocket.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/httpserver.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/socketstream.h"
-#include "rtc_base/thread.h"
-
-namespace rtc {
-
-///////////////////////////////////////////////////////////////////////////////
-// HttpServer
-///////////////////////////////////////////////////////////////////////////////
-
-HttpServer::HttpServer() : next_connection_id_(1), closing_(false) {}
-
-HttpServer::~HttpServer() {
-  if (closing_) {
-    RTC_LOG(LS_WARNING) << "HttpServer::CloseAll has not completed";
-  }
-  for (ConnectionMap::iterator it = connections_.begin();
-       it != connections_.end(); ++it) {
-    StreamInterface* stream = it->second->EndProcess();
-    delete stream;
-    delete it->second;
-  }
-}
-
-int HttpServer::HandleConnection(StreamInterface* stream) {
-  int connection_id = next_connection_id_++;
-  RTC_DCHECK(connection_id != HTTP_INVALID_CONNECTION_ID);
-  Connection* connection = new Connection(connection_id, this);
-  connections_.insert(ConnectionMap::value_type(connection_id, connection));
-  connection->BeginProcess(stream);
-  return connection_id;
-}
-
-void HttpServer::Respond(HttpServerTransaction* transaction) {
-  int connection_id = transaction->connection_id();
-  if (Connection* connection = Find(connection_id)) {
-    connection->Respond(transaction);
-  } else {
-    delete transaction;
-    // We may be tempted to SignalHttpComplete, but that implies that a
-    // connection still exists.
-  }
-}
-
-void HttpServer::Close(int connection_id, bool force) {
-  if (Connection* connection = Find(connection_id)) {
-    connection->InitiateClose(force);
-  }
-}
-
-void HttpServer::CloseAll(bool force) {
-  if (connections_.empty()) {
-    SignalCloseAllComplete(this);
-    return;
-  }
-  closing_ = true;
-  std::list<Connection*> connections;
-  for (ConnectionMap::const_iterator it = connections_.begin();
-       it != connections_.end(); ++it) {
-    connections.push_back(it->second);
-  }
-  for (std::list<Connection*>::const_iterator it = connections.begin();
-       it != connections.end(); ++it) {
-    (*it)->InitiateClose(force);
-  }
-}
-
-HttpServer::Connection* HttpServer::Find(int connection_id) {
-  ConnectionMap::iterator it = connections_.find(connection_id);
-  if (it == connections_.end())
-    return nullptr;
-  return it->second;
-}
-
-void HttpServer::Remove(int connection_id) {
-  ConnectionMap::iterator it = connections_.find(connection_id);
-  if (it == connections_.end()) {
-    RTC_NOTREACHED();
-    return;
-  }
-  Connection* connection = it->second;
-  connections_.erase(it);
-  SignalConnectionClosed(this, connection_id, connection->EndProcess());
-  delete connection;
-  if (closing_ && connections_.empty()) {
-    closing_ = false;
-    SignalCloseAllComplete(this);
-  }
-}
-
-///////////////////////////////////////////////////////////////////////////////
-// HttpServer::Connection
-///////////////////////////////////////////////////////////////////////////////
-
-HttpServer::Connection::Connection(int connection_id, HttpServer* server)
-    : connection_id_(connection_id),
-      server_(server),
-      current_(nullptr),
-      signalling_(false),
-      close_(false) {}
-
-HttpServer::Connection::~Connection() {
-  // It's possible that an object hosted inside this transaction signalled
-  // an event which caused the connection to close.
-  Thread::Current()->Dispose(current_);
-}
-
-void HttpServer::Connection::BeginProcess(StreamInterface* stream) {
-  base_.notify(this);
-  base_.attach(stream);
-  current_ = new HttpServerTransaction(connection_id_);
-  if (base_.mode() != HM_CONNECT)
-    base_.recv(&current_->request);
-}
-
-StreamInterface* HttpServer::Connection::EndProcess() {
-  base_.notify(nullptr);
-  base_.abort(HE_DISCONNECTED);
-  return base_.detach();
-}
-
-void HttpServer::Connection::Respond(HttpServerTransaction* transaction) {
-  RTC_DCHECK(current_ == nullptr);
-  current_ = transaction;
-  if (current_->response.begin() == current_->response.end()) {
-    current_->response.set_error(HC_INTERNAL_SERVER_ERROR);
-  }
-  bool keep_alive = HttpShouldKeepAlive(current_->request);
-  current_->response.setHeader(HH_CONNECTION,
-                               keep_alive ? "Keep-Alive" : "Close", false);
-  close_ = !HttpShouldKeepAlive(current_->response);
-  base_.send(&current_->response);
-}
-
-void HttpServer::Connection::InitiateClose(bool force) {
-  bool request_in_progress = (HM_SEND == base_.mode()) || (nullptr == current_);
-  if (!signalling_ && (force || !request_in_progress)) {
-    server_->Remove(connection_id_);
-  } else {
-    close_ = true;
-  }
-}
-
-//
-// IHttpNotify Implementation
-//
-
-HttpError HttpServer::Connection::onHttpHeaderComplete(bool chunked,
-                                                       size_t& data_size) {
-  if (data_size == SIZE_UNKNOWN) {
-    data_size = 0;
-  }
-  RTC_DCHECK(current_ != nullptr);
-  bool custom_document = false;
-  server_->SignalHttpRequestHeader(server_, current_, &custom_document);
-  if (!custom_document) {
-    current_->request.document.reset(new MemoryStream);
-  }
-  return HE_NONE;
-}
-
-void HttpServer::Connection::onHttpComplete(HttpMode mode, HttpError err) {
-  if (mode == HM_SEND) {
-    RTC_DCHECK(current_ != nullptr);
-    signalling_ = true;
-    server_->SignalHttpRequestComplete(server_, current_, err);
-    signalling_ = false;
-    if (close_) {
-      // Force a close
-      err = HE_DISCONNECTED;
-    }
-  }
-  if (err != HE_NONE) {
-    server_->Remove(connection_id_);
-  } else if (mode == HM_CONNECT) {
-    base_.recv(&current_->request);
-  } else if (mode == HM_RECV) {
-    RTC_DCHECK(current_ != nullptr);
-    // TODO: do we need this?
-    // request_.document_->rewind();
-    HttpServerTransaction* transaction = current_;
-    current_ = nullptr;
-    server_->SignalHttpRequest(server_, transaction);
-  } else if (mode == HM_SEND) {
-    Thread::Current()->Dispose(current_->response.document.release());
-    current_->request.clear(true);
-    current_->response.clear(true);
-    base_.recv(&current_->request);
-  } else {
-    RTC_NOTREACHED();
-  }
-}
-
-void HttpServer::Connection::onHttpClosed(HttpError err) {
-  server_->Remove(connection_id_);
-}
-
-///////////////////////////////////////////////////////////////////////////////
-// HttpListenServer
-///////////////////////////////////////////////////////////////////////////////
-
-HttpListenServer::HttpListenServer() {
-  SignalConnectionClosed.connect(this, &HttpListenServer::OnConnectionClosed);
-}
-
-HttpListenServer::~HttpListenServer() {}
-
-int HttpListenServer::Listen(const SocketAddress& address) {
-  AsyncSocket* sock = Thread::Current()->socketserver()->CreateAsyncSocket(
-      address.family(), SOCK_STREAM);
-  if (!sock) {
-    return SOCKET_ERROR;
-  }
-  listener_.reset(sock);
-  listener_->SignalReadEvent.connect(this, &HttpListenServer::OnReadEvent);
-  if ((listener_->Bind(address) != SOCKET_ERROR) &&
-      (listener_->Listen(5) != SOCKET_ERROR))
-    return 0;
-  return listener_->GetError();
-}
-
-bool HttpListenServer::GetAddress(SocketAddress* address) const {
-  if (!listener_) {
-    return false;
-  }
-  *address = listener_->GetLocalAddress();
-  return !address->IsNil();
-}
-
-void HttpListenServer::StopListening() {
-  if (listener_) {
-    listener_->Close();
-  }
-}
-
-void HttpListenServer::OnReadEvent(AsyncSocket* socket) {
-  RTC_DCHECK(socket == listener_.get());
-  AsyncSocket* incoming = listener_->Accept(nullptr);
-  if (incoming) {
-    StreamInterface* stream = new SocketStream(incoming);
-    HandleConnection(stream);
-  }
-}
-
-void HttpListenServer::OnConnectionClosed(HttpServer* server,
-                                          int connection_id,
-                                          StreamInterface* stream) {
-  Thread::Current()->Dispose(stream);
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-}  // namespace rtc
diff --git a/rtc_base/httpserver.h b/rtc_base/httpserver.h
deleted file mode 100644
index e4d9444..0000000
--- a/rtc_base/httpserver.h
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- *  Copyright 2004 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 RTC_BASE_HTTPSERVER_H_
-#define RTC_BASE_HTTPSERVER_H_
-
-#include <map>
-#include <memory>
-
-#include "rtc_base/httpbase.h"
-
-namespace rtc {
-
-class AsyncSocket;
-class HttpServer;
-class SocketAddress;
-
-//////////////////////////////////////////////////////////////////////
-// HttpServer
-//////////////////////////////////////////////////////////////////////
-
-const int HTTP_INVALID_CONNECTION_ID = 0;
-
-struct HttpServerTransaction : public HttpTransaction {
- public:
-  HttpServerTransaction(int id) : connection_id_(id) {}
-  int connection_id() const { return connection_id_; }
-
- private:
-  int connection_id_;
-};
-
-class HttpServer {
- public:
-  HttpServer();
-  virtual ~HttpServer();
-
-  int HandleConnection(StreamInterface* stream);
-  // Due to sigslot issues, we can't destroy some streams at an arbitrary time.
-  sigslot::signal3<HttpServer*, int, StreamInterface*> SignalConnectionClosed;
-
-  // This signal occurs when the HTTP request headers have been received, but
-  // before the request body is written to the request document.  By default,
-  // the request document is a MemoryStream.  By handling this signal, the
-  // document can be overridden, in which case the third signal argument should
-  // be set to true.  In the case where the request body should be ignored,
-  // the document can be set to null.  Note that the transaction object is still
-  // owened by the HttpServer at this point.
-  sigslot::signal3<HttpServer*, HttpServerTransaction*, bool*>
-      SignalHttpRequestHeader;
-
-  // An HTTP request has been made, and is available in the transaction object.
-  // Populate the transaction's response, and then return the object via the
-  // Respond method.  Note that during this time, ownership of the transaction
-  // object is transferred, so it may be passed between threads, although
-  // respond must be called on the server's active thread.
-  sigslot::signal2<HttpServer*, HttpServerTransaction*> SignalHttpRequest;
-  void Respond(HttpServerTransaction* transaction);
-
-  // If you want to know when a request completes, listen to this event.
-  sigslot::signal3<HttpServer*, HttpServerTransaction*, int>
-      SignalHttpRequestComplete;
-
-  // Stop processing the connection indicated by connection_id.
-  // Unless force is true, the server will complete sending a response that is
-  // in progress.
-  void Close(int connection_id, bool force);
-  void CloseAll(bool force);
-
-  // After calling CloseAll, this event is signalled to indicate that all
-  // outstanding connections have closed.
-  sigslot::signal1<HttpServer*> SignalCloseAllComplete;
-
- private:
-  class Connection : private IHttpNotify {
-   public:
-    Connection(int connection_id, HttpServer* server);
-    ~Connection() override;
-
-    void BeginProcess(StreamInterface* stream);
-    StreamInterface* EndProcess();
-
-    void Respond(HttpServerTransaction* transaction);
-    void InitiateClose(bool force);
-
-    // IHttpNotify Interface
-    HttpError onHttpHeaderComplete(bool chunked, size_t& data_size) override;
-    void onHttpComplete(HttpMode mode, HttpError err) override;
-    void onHttpClosed(HttpError err) override;
-
-    int connection_id_;
-    HttpServer* server_;
-    HttpBase base_;
-    HttpServerTransaction* current_;
-    bool signalling_, close_;
-  };
-
-  Connection* Find(int connection_id);
-  void Remove(int connection_id);
-
-  friend class Connection;
-  typedef std::map<int, Connection*> ConnectionMap;
-
-  ConnectionMap connections_;
-  int next_connection_id_;
-  bool closing_;
-};
-
-//////////////////////////////////////////////////////////////////////
-
-class HttpListenServer : public HttpServer, public sigslot::has_slots<> {
- public:
-  HttpListenServer();
-  ~HttpListenServer() override;
-
-  int Listen(const SocketAddress& address);
-  bool GetAddress(SocketAddress* address) const;
-  void StopListening();
-
- private:
-  void OnReadEvent(AsyncSocket* socket);
-  void OnConnectionClosed(HttpServer* server,
-                          int connection_id,
-                          StreamInterface* stream);
-
-  std::unique_ptr<AsyncSocket> listener_;
-};
-
-//////////////////////////////////////////////////////////////////////
-
-}  // namespace rtc
-
-#endif  // RTC_BASE_HTTPSERVER_H_
diff --git a/rtc_base/httpserver_unittest.cc b/rtc_base/httpserver_unittest.cc
deleted file mode 100644
index 0dde6c5..0000000
--- a/rtc_base/httpserver_unittest.cc
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- *  Copyright 2007 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 "rtc_base/httpserver.h"
-#include "rtc_base/gunit.h"
-#include "rtc_base/testutils.h"
-
-using namespace webrtc::testing;
-
-namespace rtc {
-
-namespace {
-const char* const kRequest =
-    "GET /index.html HTTP/1.1\r\n"
-    "Host: localhost\r\n"
-    "\r\n";
-
-struct HttpServerMonitor : public sigslot::has_slots<> {
-  HttpServerTransaction* transaction;
-  bool server_closed, connection_closed;
-
-  HttpServerMonitor(HttpServer* server)
-      : transaction(nullptr), server_closed(false), connection_closed(false) {
-    server->SignalCloseAllComplete.connect(this, &HttpServerMonitor::OnClosed);
-    server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest);
-    server->SignalHttpRequestComplete.connect(
-        this, &HttpServerMonitor::OnRequestComplete);
-    server->SignalConnectionClosed.connect(
-        this, &HttpServerMonitor::OnConnectionClosed);
-  }
-  void OnRequest(HttpServer*, HttpServerTransaction* t) {
-    ASSERT_FALSE(transaction);
-    transaction = t;
-    transaction->response.set_success();
-    transaction->response.setHeader(HH_CONNECTION, "Close");
-  }
-  void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) {
-    ASSERT_EQ(transaction, t);
-    transaction = nullptr;
-  }
-  void OnClosed(HttpServer*) { server_closed = true; }
-  void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) {
-    connection_closed = true;
-    delete stream;
-  }
-};
-
-void CreateClientConnection(HttpServer& server,
-                            HttpServerMonitor& monitor,
-                            bool send_request) {
-  StreamSource* client = new StreamSource;
-  client->SetState(SS_OPEN);
-  server.HandleConnection(client);
-  EXPECT_FALSE(monitor.server_closed);
-  EXPECT_FALSE(monitor.transaction);
-
-  if (send_request) {
-    // Simulate a request
-    client->QueueString(kRequest);
-    EXPECT_FALSE(monitor.server_closed);
-  }
-}
-}  // anonymous namespace
-
-TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) {
-  HttpServer server;
-  HttpServerMonitor monitor(&server);
-  // Add an active client connection
-  CreateClientConnection(server, monitor, true);
-  // Simulate a response
-  ASSERT_TRUE(nullptr != monitor.transaction);
-  server.Respond(monitor.transaction);
-  EXPECT_FALSE(monitor.transaction);
-  // Connection has closed, but no server close signal
-  EXPECT_FALSE(monitor.server_closed);
-  EXPECT_TRUE(monitor.connection_closed);
-}
-
-TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) {
-  HttpServer server;
-  HttpServerMonitor monitor(&server);
-  // Add an idle client connection
-  CreateClientConnection(server, monitor, false);
-  // Perform graceful close
-  server.CloseAll(false);
-  // Connections have all closed
-  EXPECT_TRUE(monitor.server_closed);
-  EXPECT_TRUE(monitor.connection_closed);
-}
-
-TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) {
-  HttpServer server;
-  HttpServerMonitor monitor(&server);
-  // Add an active client connection
-  CreateClientConnection(server, monitor, true);
-  // Initiate a graceful close
-  server.CloseAll(false);
-  EXPECT_FALSE(monitor.server_closed);
-  // Simulate a response
-  ASSERT_TRUE(nullptr != monitor.transaction);
-  server.Respond(monitor.transaction);
-  EXPECT_FALSE(monitor.transaction);
-  // Connections have all closed
-  EXPECT_TRUE(monitor.server_closed);
-  EXPECT_TRUE(monitor.connection_closed);
-}
-
-TEST(HttpServer, SignalsCloseAfterForcedCloseAll) {
-  HttpServer server;
-  HttpServerMonitor monitor(&server);
-  // Add an active client connection
-  CreateClientConnection(server, monitor, true);
-  // Initiate a forceful close
-  server.CloseAll(true);
-  // Connections have all closed
-  EXPECT_TRUE(monitor.server_closed);
-  EXPECT_TRUE(monitor.connection_closed);
-}
-
-}  // namespace rtc
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc
index c386600..53a1ed8 100644
--- a/rtc_base/logging.cc
+++ b/rtc_base/logging.cc
@@ -26,13 +26,10 @@
 static const int kMaxLogLineSize = 1024 - 60;
 #endif  // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
 
-#include <limits.h>
 #include <time.h>
 
 #include <algorithm>
 #include <cstdarg>
-#include <iomanip>
-#include <ostream>
 #include <vector>
 
 #include "rtc_base/criticalsection.h"
@@ -64,16 +61,6 @@
     return (end1 > end2) ? end1 + 1 : end2 + 1;
 }
 
-std::ostream& GetNoopStream() {
-  class NoopStreamBuf : public std::streambuf {
-   public:
-    int overflow(int c) override { return c; }
-  };
-  static NoopStreamBuf noop_buffer;
-  static std::ostream noop_stream(&noop_buffer);
-  return noop_stream;
-}
-
 // Global lock for log subsystem, only needed to serialize access to streams_.
 CriticalSection g_log_crit;
 }  // namespace
@@ -108,11 +95,7 @@
                        LoggingSeverity sev,
                        LogErrorContext err_ctx,
                        int err)
-    : severity_(sev), is_noop_(IsNoop(sev)) {
-  // If there's no need to do any work, let's not :)
-  if (is_noop_)
-    return;
-
+    : severity_(sev) {
   if (timestamp_) {
     // Use SystemTimeMillis so that even if tests use fake clocks, the timestamp
     // in log messages represents the real system time.
@@ -120,14 +103,14 @@
     // Also ensure WallClockStartTime is initialized, so that it matches
     // LogStartTime.
     WallClockStartTime();
-    print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
-                  << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
+    print_stream_ << "[" << rtc::LeftPad('0', 3, rtc::ToString(time / 1000))
+                  << ":" << rtc::LeftPad('0', 3, rtc::ToString(time % 1000))
                   << "] ";
   }
 
   if (thread_) {
     PlatformThreadId id = CurrentThreadId();
-    print_stream_ << "[" << std::dec << id << "] ";
+    print_stream_ << "[" << id << "] ";
   }
 
   if (file != nullptr) {
@@ -184,10 +167,8 @@
                        LoggingSeverity sev,
                        const char* tag)
     : LogMessage(file, line, sev, ERRCTX_NONE, 0 /* err */) {
-  if (!is_noop_) {
-    tag_ = tag;
-    print_stream_ << tag << ": ";
-  }
+  tag_ = tag;
+  print_stream_ << tag << ": ";
 }
 #endif
 
@@ -199,21 +180,13 @@
                        LoggingSeverity sev,
                        const std::string& tag)
     : LogMessage(file, line, sev) {
-  if (!is_noop_)
-    print_stream_ << tag << ": ";
+  print_stream_ << tag << ": ";
 }
 
 LogMessage::~LogMessage() {
-  if (is_noop_)
-    return;
-
   FinishPrintStream();
 
-  // TODO(tommi): Unfortunately |ostringstream::str()| always returns a copy
-  // of the constructed string. This means that we always end up creating
-  // two copies here (one owned by the stream, one by the return value of
-  // |str()|). It would be nice to switch to something else.
-  const std::string str = print_stream_.str();
+  const std::string str = print_stream_.Release();
 
   if (severity_ >= g_dbg_sev) {
 #if defined(WEBRTC_ANDROID)
@@ -237,18 +210,12 @@
 
 void LogMessage::AddTag(const char* tag) {
 #ifdef WEBRTC_ANDROID
-  if (!is_noop_) {
-    tag_ = tag;
-  }
+  tag_ = tag;
 #endif
 }
 
-std::ostream& LogMessage::stream() {
-  return is_noop_ ? GetNoopStream() : print_stream_;
-}
-
-bool LogMessage::Loggable(LoggingSeverity sev) {
-  return sev >= g_min_sev;
+rtc::StringBuilder& LogMessage::stream() {
+  return print_stream_;
 }
 
 int LogMessage::GetMinLogSeverity() {
@@ -476,22 +443,23 @@
 
 // static
 bool LogMessage::IsNoop(LoggingSeverity severity) {
-  if (severity >= g_dbg_sev)
+  if (severity >= g_dbg_sev || severity >= g_min_sev)
     return false;
 
   // TODO(tommi): We're grabbing this lock for every LogMessage instance that
   // is going to be logged. This introduces unnecessary synchronization for
   // a feature that's mostly used for testing.
   CritScope cs(&g_log_crit);
-  return streams_.size() == 0;
+  if (streams_.size() > 0)
+    return false;
+
+  return true;
 }
 
 void LogMessage::FinishPrintStream() {
-  if (is_noop_)
-    return;
   if (!extra_.empty())
     print_stream_ << " : " << extra_;
-  print_stream_ << std::endl;
+  print_stream_ << "\n";
 }
 
 namespace webrtc_logging_impl {
@@ -525,6 +493,12 @@
       return;
     }
   }
+
+  if (LogMessage::IsNoop(meta.meta.Severity())) {
+    va_end(args);
+    return;
+  }
+
   LogMessage log_message(meta.meta.File(), meta.meta.Line(),
                          meta.meta.Severity(), meta.err_ctx, meta.err);
   if (tag) {
@@ -563,8 +537,12 @@
       case LogArgType::kStdString:
         log_message.stream() << *va_arg(args, const std::string*);
         break;
+      case LogArgType::kStringView:
+        log_message.stream() << *va_arg(args, const absl::string_view*);
+        break;
       case LogArgType::kVoidP:
-        log_message.stream() << va_arg(args, const void*);
+        log_message.stream() << rtc::ToHex(
+            reinterpret_cast<uintptr_t>(va_arg(args, const void*)));
         break;
       default:
         RTC_NOTREACHED();
diff --git a/rtc_base/logging.h b/rtc_base/logging.h
index b5af959..4292971 100644
--- a/rtc_base/logging.h
+++ b/rtc_base/logging.h
@@ -47,18 +47,19 @@
 #include <errno.h>
 
 #include <list>
-#include <sstream>
+#include <sstream>  // no-presubmit-check TODO(webrtc:8982)
 #include <string>
 #include <utility>
 
-#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
-#include <CoreServices/CoreServices.h>
-#endif
-
+#include "absl/strings/string_view.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/deprecation.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/system/inline.h"
-#include "rtc_base/thread_annotations.h"
+
+#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
+#include "rtc_base/logging_mac.h"
+#endif  // WEBRTC_MAC
 
 #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
 #define RTC_DLOG_IS_ON 1
@@ -68,11 +69,6 @@
 
 namespace rtc {
 
-#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
-// Returns a UTF8 description from an OS X Status error.
-std::string DescriptionFromOSStatus(OSStatus err);
-#endif
-
 //////////////////////////////////////////////////////////////////////
 
 // Note that the non-standard LoggingSeverity aliases exist because they are
@@ -173,7 +169,7 @@
   kLongDouble,
   kCharP,
   kStdString,
-  // TODO(kwiberg): Add absl::StringView.
+  kStringView,
   kVoidP,
   kLogMetadata,
   kLogMetadataErr,
@@ -235,7 +231,10 @@
     const std::string& x) {
   return {&x};
 }
-// TODO(kwiberg): Add absl::string_view
+inline Val<LogArgType::kStringView, const absl::string_view*> MakeVal(
+    const absl::string_view& x) {
+  return {&x};
+}
 
 inline Val<LogArgType::kVoidP, const void*> MakeVal(const void* x) {
   return {x};
@@ -407,18 +406,7 @@
 
   void AddTag(const char* tag);
 
-  static bool Loggable(LoggingSeverity sev);
-
-  // Same as the above, but using a template argument instead of a function
-  // argument. (When the logging severity is statically known, passing it as a
-  // template argument instead of as a function argument saves space at the
-  // call site.)
-  template <LoggingSeverity S>
-  RTC_NO_INLINE static bool Loggable() {
-    return Loggable(S);
-  }
-
-  std::ostream& stream();
+  rtc::StringBuilder& stream();
 
   // Returns the time at which this function was called for the first time.
   // The time will be used as the logging start time.
@@ -464,6 +452,12 @@
   // Useful for configuring logging from the command line.
   static void ConfigureLogging(const char* params);
 
+  // Checks the current global debug severity and if the |streams_| collection
+  // is empty. If |severity| is smaller than the global severity and if the
+  // |streams_| collection is empty, the LogMessage will be considered a noop
+  // LogMessage.
+  static bool IsNoop(LoggingSeverity severity);
+
  private:
   friend class LogMessageForTesting;
   typedef std::pair<LogSink*, LoggingSeverity> StreamAndSeverity;
@@ -481,18 +475,12 @@
   static void OutputToDebug(const std::string& msg, LoggingSeverity severity);
 #endif
 
-  // Checks the current global debug severity and if the |streams_| collection
-  // is empty. If |severity| is smaller than the global severity and if the
-  // |streams_| collection is empty, the LogMessage will be considered a noop
-  // LogMessage.
-  static bool IsNoop(LoggingSeverity severity);
-
   // Called from the dtor (or from a test) to append optional extra error
   // information to the log stream and a newline character.
   void FinishPrintStream();
 
-  // The ostream that buffers the formatted message before output
-  std::ostringstream print_stream_;
+  // The stringbuilder that buffers the formatted message before output
+  rtc::StringBuilder print_stream_;
 
   // The severity level of this message
   LoggingSeverity severity_;
@@ -506,8 +494,6 @@
   // the message before output.
   std::string extra_;
 
-  const bool is_noop_;
-
   // The output streams and their associated severities
   static StreamList streams_;
 
@@ -527,24 +513,19 @@
 // DEPRECATED.
 // TODO(bugs.webrtc.org/9278): Remove once there are no more users.
 #define RTC_LOG_SEVERITY_PRECONDITION(sev) \
-  !(rtc::LogMessage::Loggable(sev))        \
+  (rtc::LogMessage::IsNoop(sev))           \
       ? static_cast<void>(0)               \
       : rtc::webrtc_logging_impl::LogMessageVoidify()&
 
-#define RTC_LOG(sev)                                                   \
-  for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log;    \
-       do_log = false)                                                 \
-  rtc::webrtc_logging_impl::LogCall() &                                \
-      rtc::webrtc_logging_impl::LogStreamer<>()                        \
-          << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, \
-                                                   rtc::sev)
+#define RTC_LOG_FILE_LINE(sev, file, line)                                     \
+  rtc::webrtc_logging_impl::LogCall() &                                        \
+      rtc::webrtc_logging_impl::LogStreamer<>()                                \
+          << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
+
+#define RTC_LOG(sev) RTC_LOG_FILE_LINE(rtc::sev, __FILE__, __LINE__)
 
 // The _V version is for when a variable is passed in.
-#define RTC_LOG_V(sev)                                                       \
-  for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
-  rtc::webrtc_logging_impl::LogCall() &                                      \
-      rtc::webrtc_logging_impl::LogStreamer<>()                              \
-          << rtc::webrtc_logging_impl::LogMetadata(__FILE__, __LINE__, sev)
+#define RTC_LOG_V(sev) RTC_LOG_FILE_LINE(sev, __FILE__, __LINE__)
 
 // The _F version prefixes the message with the current function name.
 #if (defined(__GNUC__) && !defined(NDEBUG)) || defined(WANT_PRETTY_LOG_F)
@@ -564,8 +545,6 @@
 }
 
 #define RTC_LOG_E(sev, ctx, err)                                    \
-  for (bool do_log = rtc::LogMessage::Loggable<rtc::sev>(); do_log; \
-       do_log = false)                                              \
     rtc::webrtc_logging_impl::LogCall() &                           \
         rtc::webrtc_logging_impl::LogStreamer<>()                   \
             << rtc::webrtc_logging_impl::LogMetadataErr {           \
@@ -603,7 +582,6 @@
 }  // namespace webrtc_logging_impl
 
 #define RTC_LOG_TAG(sev, tag)                                                \
-  for (bool do_log = rtc::LogMessage::Loggable(sev); do_log; do_log = false) \
     rtc::webrtc_logging_impl::LogCall() &                                    \
         rtc::webrtc_logging_impl::LogStreamer<>()                            \
             << rtc::webrtc_logging_impl::LogMetadataTag {                    \
diff --git a/rtc_base/logging_mac.h b/rtc_base/logging_mac.h
new file mode 100644
index 0000000..e65db56
--- /dev/null
+++ b/rtc_base/logging_mac.h
@@ -0,0 +1,28 @@
+/*
+ *  Copyright 2018 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 RTC_BASE_LOGGING_MAC_H_
+#define RTC_BASE_LOGGING_MAC_H_
+
+#if !defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
+#error "Only include this header in macOS builds"
+#endif
+
+#include <CoreServices/CoreServices.h>
+
+#include <string>
+
+namespace rtc {
+
+// Returns a UTF8 description from an OS X Status error.
+std::string DescriptionFromOSStatus(OSStatus err);
+
+}  // namespace rtc
+
+#endif  // RTC_BASE_LOGGING_MAC_H_
diff --git a/rtc_base/logging_mac.mm b/rtc_base/logging_mac.mm
index 378cfbf..bd5f2b9 100644
--- a/rtc_base/logging_mac.mm
+++ b/rtc_base/logging_mac.mm
@@ -8,15 +8,15 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/logging.h"
+#include "rtc_base/logging_mac.h"
 
 #import <Foundation/Foundation.h>
 
-
 namespace rtc {
 std::string DescriptionFromOSStatus(OSStatus err) {
   NSError* error =
       [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
   return error.description.UTF8String;
 }
+
 }  // namespace rtc
diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc
index af51212..a475e52 100644
--- a/rtc_base/logging_unittest.cc
+++ b/rtc_base/logging_unittest.cc
@@ -39,7 +39,6 @@
   bool SetPosition(size_t position) override;
   bool GetPosition(size_t* position) const override;
   bool GetSize(size_t* size) const override;
-  bool GetAvailable(size_t* size) const override;
   bool ReserveSize(size_t size) override;
 
  private:
@@ -110,12 +109,6 @@
   return true;
 }
 
-bool StringStream::GetAvailable(size_t* size) const {
-  if (size)
-    *size = str_.size() - read_pos_;
-  return true;
-}
-
 bool StringStream::ReserveSize(size_t size) {
   if (read_only_)
     return false;
@@ -150,7 +143,6 @@
       : LogMessage(file, line, sev, err_ctx, err) {}
 
   const std::string& get_extra() const { return extra_; }
-  bool is_noop() const { return is_noop_; }
 #if defined(WEBRTC_ANDROID)
   const char* get_tag() const { return tag_; }
 #endif
@@ -163,10 +155,7 @@
     RTC_DCHECK(!is_finished_);
     is_finished_ = true;
     FinishPrintStream();
-    std::string ret = print_stream_.str();
-    // Just to make an error even more clear if the stream gets used after this.
-    print_stream_.clear();
-    return ret;
+    return print_stream_.Release();
   }
 
  private:
@@ -188,9 +177,45 @@
   EXPECT_NE(std::string::npos, str.find("INFO"));
   EXPECT_EQ(std::string::npos, str.find("VERBOSE"));
 
+  int i = 1;
+  long l = 2l;
+  long long ll = 3ll;
+
+  unsigned int u = 4u;
+  unsigned long ul = 5ul;
+  unsigned long long ull = 6ull;
+
+  std::string s1 = "char*";
+  std::string s2 = "std::string";
+  std::string s3 = "absl::stringview";
+
+  void* p = reinterpret_cast<void*>(0xabcd);
+
+  // Log all suported types(except doubles/floats) as a sanity-check.
+  RTC_LOG(LS_INFO) << "|" << i << "|" << l << "|" << ll << "|" << u << "|" << ul
+                   << "|" << ull << "|" << s1.c_str() << "|" << s2 << "|"
+                   << absl::string_view(s3) << "|" << p << "|";
+
+  // Signed integers
+  EXPECT_NE(std::string::npos, str.find("|1|"));
+  EXPECT_NE(std::string::npos, str.find("|2|"));
+  EXPECT_NE(std::string::npos, str.find("|3|"));
+
+  // Unsigned integers
+  EXPECT_NE(std::string::npos, str.find("|4|"));
+  EXPECT_NE(std::string::npos, str.find("|5|"));
+  EXPECT_NE(std::string::npos, str.find("|6|"));
+
+  // Strings
+  EXPECT_NE(std::string::npos, str.find("|char*|"));
+  EXPECT_NE(std::string::npos, str.find("|std::string|"));
+  EXPECT_NE(std::string::npos, str.find("|absl::stringview|"));
+
+  // void*
+  EXPECT_NE(std::string::npos, str.find("|abcd|"));
+
   LogMessage::RemoveLogToStream(&stream);
   EXPECT_EQ(LS_NONE, LogMessage::GetLogToStream(&stream));
-
   EXPECT_EQ(sev, LogMessage::GetLogToStream(nullptr));
 }
 
@@ -303,7 +328,6 @@
 TEST(LogTest, CheckExtraErrorField) {
   LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_WARNING,
                                ERRCTX_ERRNO, 0xD);
-  ASSERT_FALSE(log_msg.is_noop());
   log_msg.stream() << "This gets added at dtor time";
 
   const std::string& extra = log_msg.get_extra();
@@ -314,7 +338,6 @@
 
 TEST(LogTest, CheckFilePathParsed) {
   LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_INFO);
-  ASSERT_FALSE(log_msg.is_noop());
   log_msg.stream() << "<- Does this look right?";
 
   const std::string stream = log_msg.GetPrintStream();
@@ -340,21 +363,6 @@
 }
 #endif
 
-TEST(LogTest, CheckNoopLogEntry) {
-  if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) {
-    printf("CheckNoopLogEntry: skipping. Global severity is being overridden.");
-    return;
-  }
-
-  // Logging at LS_SENSITIVE severity, is by default turned off, so this should
-  // be treated as a noop message.
-  LogMessageForTesting log_msg("some/path/myfile.cc", 100, LS_SENSITIVE);
-  log_msg.stream() << "Should be logged to nowhere.";
-  EXPECT_TRUE(log_msg.is_noop());
-  const std::string stream = log_msg.GetPrintStream();
-  EXPECT_TRUE(stream.empty());
-}
-
 // Test the time required to write 1000 80-character logs to a string.
 TEST(LogTest, Perf) {
   std::string str;
@@ -363,10 +371,7 @@
 
   const std::string message(80, 'X');
   {
-    // Just to be sure that we're not measuring the performance of logging
-    // noop log messages.
     LogMessageForTesting sanity_check_msg(__FILE__, __LINE__, LS_SENSITIVE);
-    ASSERT_FALSE(sanity_check_msg.is_noop());
   }
 
   // We now know how many bytes the logging framework will tag onto every msg.
diff --git a/rtc_base/mdns_responder_interface.h b/rtc_base/mdns_responder_interface.h
new file mode 100644
index 0000000..9dbaf56
--- /dev/null
+++ b/rtc_base/mdns_responder_interface.h
@@ -0,0 +1,51 @@
+/*
+ *  Copyright 2018 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 RTC_BASE_MDNS_RESPONDER_INTERFACE_H_
+#define RTC_BASE_MDNS_RESPONDER_INTERFACE_H_
+
+#include <functional>
+#include <map>
+#include <memory>
+#include <set>
+#include <string>
+
+#include "rtc_base/ipaddress.h"
+#include "rtc_base/socketaddress.h"
+
+namespace webrtc {
+
+// Defines an mDNS responder that can be used in ICE candidate gathering, where
+// the local IP addresses of host candidates are obfuscated by mDNS hostnames.
+class MDnsResponderInterface {
+ public:
+  using NameCreatedCallback =
+      std::function<void(const rtc::IPAddress&, const std::string&)>;
+  using NameRemovedCallback = std::function<void(bool)>;
+
+  MDnsResponderInterface() = default;
+  virtual ~MDnsResponderInterface() = default;
+
+  // Asynchronously creates a type-4 UUID hostname for an IP address. The
+  // created name should be given to |callback| with the address that it
+  // represents.
+  virtual void CreateNameForAddress(const rtc::IPAddress& addr,
+                                    NameCreatedCallback callback) = 0;
+  // Removes the name mapped to the given address if there is such an
+  // name-address mapping previously created via CreateNameForAddress. The
+  // result of whether an associated name-address mapping is removed should be
+  // given to |callback|.
+  virtual void RemoveNameForAddress(const rtc::IPAddress& addr,
+                                    NameRemovedCallback callback) = 0;
+};
+
+}  // namespace webrtc
+
+#endif  // RTC_BASE_MDNS_RESPONDER_INTERFACE_H_
diff --git a/rtc_base/memory/aligned_malloc.cc b/rtc_base/memory/aligned_malloc.cc
index 2943a64..c893c96 100644
--- a/rtc_base/memory/aligned_malloc.cc
+++ b/rtc_base/memory/aligned_malloc.cc
@@ -10,8 +10,8 @@
 
 #include "rtc_base/memory/aligned_malloc.h"
 
-#include <memory.h>
-#include <stdlib.h>
+#include <stdlib.h>  // for free, malloc
+#include <string.h>  // for memcpy
 
 #ifdef _WIN32
 #include <windows.h>
diff --git a/rtc_base/messagehandler.h b/rtc_base/messagehandler.h
index df2d1ad..0c40853 100644
--- a/rtc_base/messagehandler.h
+++ b/rtc_base/messagehandler.h
@@ -11,7 +11,6 @@
 #ifndef RTC_BASE_MESSAGEHANDLER_H_
 #define RTC_BASE_MESSAGEHANDLER_H_
 
-#include <memory>
 #include <utility>
 
 #include "rtc_base/constructormagic.h"
diff --git a/rtc_base/messagequeue.cc b/rtc_base/messagequeue.cc
index 035ff07..84d3a96 100644
--- a/rtc_base/messagequeue.cc
+++ b/rtc_base/messagequeue.cc
@@ -8,13 +8,14 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 #include <algorithm>
+#include <utility>  // for move
 
 #include "rtc_base/atomicops.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/messagequeue.h"
-#include "rtc_base/stringencode.h"
 #include "rtc_base/thread.h"
+#include "rtc_base/timeutils.h"  // for TimeMillis, TimeDiff, TimeUntil
 #include "rtc_base/trace_event.h"
 
 namespace rtc {
@@ -48,18 +49,9 @@
 //------------------------------------------------------------------
 // MessageQueueManager
 
-MessageQueueManager* MessageQueueManager::instance_ = nullptr;
-
 MessageQueueManager* MessageQueueManager::Instance() {
-  // Note: This is not thread safe, but it is first called before threads are
-  // spawned.
-  if (!instance_)
-    instance_ = new MessageQueueManager;
-  return instance_;
-}
-
-bool MessageQueueManager::IsInitialized() {
-  return instance_ != nullptr;
+  static MessageQueueManager* const instance = new MessageQueueManager;
+  return instance;
 }
 
 MessageQueueManager::MessageQueueManager() : processing_(0) {}
@@ -77,18 +69,9 @@
 }
 
 void MessageQueueManager::Remove(MessageQueue* message_queue) {
-  // If there isn't a message queue manager instance, then there isn't a queue
-  // to remove.
-  if (!instance_)
-    return;
   return Instance()->RemoveInternal(message_queue);
 }
 void MessageQueueManager::RemoveInternal(MessageQueue* message_queue) {
-  // If this is the last MessageQueue, destroy the manager as well so that
-  // we don't leak this object at program shutdown. As mentioned above, this is
-  // not thread-safe, but this should only happen at program termination (when
-  // the ThreadManager is destroyed, and threads are no longer active).
-  bool destroy = false;
   {
     CritScope cs(&crit_);
     // Prevent changes while the list of message queues is processed.
@@ -99,19 +82,10 @@
     if (iter != message_queues_.end()) {
       message_queues_.erase(iter);
     }
-    destroy = message_queues_.empty();
-  }
-  if (destroy) {
-    instance_ = nullptr;
-    delete this;
   }
 }
 
 void MessageQueueManager::Clear(MessageHandler* handler) {
-  // If there isn't a message queue manager instance, then there aren't any
-  // queues to remove this handler from.
-  if (!instance_)
-    return;
   return Instance()->ClearInternal(handler);
 }
 void MessageQueueManager::ClearInternal(MessageHandler* handler) {
@@ -124,10 +98,7 @@
   }
 }
 
-void MessageQueueManager::ProcessAllMessageQueues() {
-  if (!instance_) {
-    return;
-  }
+void MessageQueueManager::ProcessAllMessageQueuesForTesting() {
   return Instance()->ProcessAllMessageQueuesInternal();
 }
 
@@ -153,7 +124,7 @@
   {
     MarkProcessingCritScope cs(&crit_, &processing_);
     for (MessageQueue* queue : message_queues_) {
-      if (!queue->IsProcessingMessages()) {
+      if (!queue->IsProcessingMessagesForTesting()) {
         // If the queue is not processing messages, it can
         // be ignored. If we tried to post a message to it, it would be dropped
         // or ignored.
@@ -163,11 +134,15 @@
                          new ScopedIncrement(&queues_not_done));
     }
   }
-  // Note: One of the message queues may have been on this thread, which is why
-  // we can't synchronously wait for queues_not_done to go to 0; we need to
-  // process messages as well.
+
+  rtc::Thread* current = rtc::Thread::Current();
+  // Note: One of the message queues may have been on this thread, which is
+  // why we can't synchronously wait for queues_not_done to go to 0; we need
+  // to process messages as well.
   while (AtomicOps::AcquireLoad(&queues_not_done) > 0) {
-    rtc::Thread::Current()->ProcessMessages(0);
+    if (current) {
+      current->ProcessMessages(0);
+    }
   }
 }
 
@@ -221,7 +196,7 @@
   // is going away.
   SignalQueueDestroyed();
   MessageQueueManager::Remove(this);
-  Clear(nullptr);
+  ClearInternal(nullptr, MQID_ANY, nullptr);
 
   if (ss_) {
     ss_->SetMessageQueue(nullptr);
@@ -245,7 +220,7 @@
   return AtomicOps::AcquireLoad(&stop_) != 0;
 }
 
-bool MessageQueue::IsProcessingMessages() {
+bool MessageQueue::IsProcessingMessagesForTesting() {
   return !IsQuitting();
 }
 
@@ -476,7 +451,12 @@
                          uint32_t id,
                          MessageList* removed) {
   CritScope cs(&crit_);
+  ClearInternal(phandler, id, removed);
+}
 
+void MessageQueue::ClearInternal(MessageHandler* phandler,
+                                 uint32_t id,
+                                 MessageList* removed) {
   // Remove messages with phandler
 
   if (fPeekKeep_ && msgPeek_.Match(phandler, id)) {
diff --git a/rtc_base/messagequeue.h b/rtc_base/messagequeue.h
index fe64c9c..c1b9b5a 100644
--- a/rtc_base/messagequeue.h
+++ b/rtc_base/messagequeue.h
@@ -17,7 +17,6 @@
 #include <list>
 #include <memory>
 #include <queue>
-#include <utility>
 #include <vector>
 
 #include "rtc_base/constructormagic.h"
@@ -28,7 +27,6 @@
 #include "rtc_base/socketserver.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread_annotations.h"
-#include "rtc_base/timeutils.h"
 
 namespace rtc {
 
@@ -43,16 +41,13 @@
   static void Remove(MessageQueue* message_queue);
   static void Clear(MessageHandler* handler);
 
-  // For testing purposes, we expose whether or not the MessageQueueManager
-  // instance has been initialized. It has no other use relative to the rest of
-  // the functions of this class, which auto-initialize the underlying
-  // MessageQueueManager instance when necessary.
-  static bool IsInitialized();
+  // TODO(nisse): Delete alias, as soon as downstream code is updated.
+  static void ProcessAllMessageQueues() { ProcessAllMessageQueuesForTesting(); }
 
-  // Mainly for testing purposes, for use with a simulated clock.
+  // For testing purposes, for use with a simulated clock.
   // Ensures that all message queues have processed delayed messages
   // up until the current point in time.
-  static void ProcessAllMessageQueues();
+  static void ProcessAllMessageQueuesForTesting();
 
  private:
   static MessageQueueManager* Instance();
@@ -65,7 +60,6 @@
   void ClearInternal(MessageHandler* handler);
   void ProcessAllMessageQueuesInternal();
 
-  static MessageQueueManager* instance_;
   // This list contains all live MessageQueues.
   std::vector<MessageQueue*> message_queues_ RTC_GUARDED_BY(crit_);
 
@@ -226,7 +220,7 @@
   // Not all message queues actually process messages (such as SignalThread).
   // In those cases, it's important to know, before posting, that it won't be
   // Processed.  Normally, this would be true until IsQuitting() is true.
-  virtual bool IsProcessingMessages();
+  virtual bool IsProcessingMessagesForTesting();
 
   // Get() will process I/O until:
   //  1) A message is available (returns true)
@@ -302,9 +296,15 @@
   // if false was passed as init_queue to the MessageQueue constructor.
   void DoInit();
 
-  // Perform cleanup, subclasses that override Clear must call this from the
-  // destructor.
-  void DoDestroy();
+  // Does not take any lock. Must be called either while holding crit_, or by
+  // the destructor (by definition, the latter has exclusive access).
+  void ClearInternal(MessageHandler* phandler,
+                     uint32_t id,
+                     MessageList* removed) RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_);
+
+  // Perform cleanup; subclasses must call this from the destructor,
+  // and are not expected to actually hold the lock.
+  void DoDestroy() RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_);
 
   void WakeUpSocketServer();
 
diff --git a/rtc_base/messagequeue_unittest.cc b/rtc_base/messagequeue_unittest.cc
index 1018a62..d8e8b11 100644
--- a/rtc_base/messagequeue_unittest.cc
+++ b/rtc_base/messagequeue_unittest.cc
@@ -134,22 +134,6 @@
   bool rewrap_;
 };
 
-TEST(MessageQueueManager, Clear) {
-  UnwrapMainThreadScope s;
-  if (MessageQueueManager::IsInitialized()) {
-    RTC_LOG(LS_INFO)
-        << "Unable to run MessageQueueManager::Clear test, since the "
-        << "MessageQueueManager was already initialized by some "
-        << "other test in this run.";
-    return;
-  }
-  bool deleted = false;
-  DeletedMessageHandler* handler = new DeletedMessageHandler(&deleted);
-  delete handler;
-  EXPECT_TRUE(deleted);
-  EXPECT_FALSE(MessageQueueManager::IsInitialized());
-}
-
 // Ensure that ProcessAllMessageQueues does its essential function; process
 // all messages (both delayed and non delayed) up until the current time, on
 // all registered message queues.
@@ -182,7 +166,7 @@
   b->PostDelayed(RTC_FROM_HERE, 0, &incrementer);
   rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler);
 
-  MessageQueueManager::ProcessAllMessageQueues();
+  MessageQueueManager::ProcessAllMessageQueuesForTesting();
   EXPECT_EQ(4, AtomicOps::AcquireLoad(&messages_processed));
 }
 
@@ -191,7 +175,7 @@
   auto t = Thread::CreateWithSocketServer();
   t->Start();
   t->Quit();
-  MessageQueueManager::ProcessAllMessageQueues();
+  MessageQueueManager::ProcessAllMessageQueuesForTesting();
 }
 
 // Test that ProcessAllMessageQueues doesn't hang if a queue clears its
@@ -218,7 +202,7 @@
   // Post messages (both delayed and non delayed) to both threads.
   t->Post(RTC_FROM_HERE, &clearer);
   rtc::Thread::Current()->Post(RTC_FROM_HERE, &event_signaler);
-  MessageQueueManager::ProcessAllMessageQueues();
+  MessageQueueManager::ProcessAllMessageQueuesForTesting();
 }
 
 class RefCountedHandler : public MessageHandler, public rtc::RefCountInterface {
diff --git a/rtc_base/nethelper.h b/rtc_base/nethelper.h
index e86d126..f956138 100644
--- a/rtc_base/nethelper.h
+++ b/rtc_base/nethelper.h
@@ -10,7 +10,6 @@
 #ifndef RTC_BASE_NETHELPER_H_
 #define RTC_BASE_NETHELPER_H_
 
-#include <cstdlib>
 #include <string>
 
 // This header contains helper functions and constants used by different types
diff --git a/rtc_base/nethelpers.cc b/rtc_base/nethelpers.cc
index b1221c3..81cd1af 100644
--- a/rtc_base/nethelpers.cc
+++ b/rtc_base/nethelpers.cc
@@ -25,10 +25,9 @@
 #endif
 #endif  // defined(WEBRTC_POSIX) && !defined(__native_client__)
 
-#include "rtc_base/byteorder.h"
-#include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/signalthread.h"
+#include "rtc_base/third_party/sigslot/sigslot.h"  // for signal_with_thread...
 
 namespace rtc {
 
diff --git a/rtc_base/nethelpers.h b/rtc_base/nethelpers.h
index f9d188f..429f0c0 100644
--- a/rtc_base/nethelpers.h
+++ b/rtc_base/nethelpers.h
@@ -18,12 +18,12 @@
 #include <winsock2.h>  // NOLINT
 #endif
 
-#include <list>
+#include <vector>
 
 #include "rtc_base/asyncresolverinterface.h"
+#include "rtc_base/ipaddress.h"  // for IPAddress
 #include "rtc_base/signalthread.h"
 #include "rtc_base/socketaddress.h"
-#include "rtc_base/third_party/sigslot/sigslot.h"
 
 namespace rtc {
 
diff --git a/rtc_base/network.cc b/rtc_base/network.cc
index 34da828..67888ed 100644
--- a/rtc_base/network.cc
+++ b/rtc_base/network.cc
@@ -40,6 +40,7 @@
 #include "rtc_base/socket.h"  // includes something that makes windows happy
 #include "rtc_base/stream.h"
 #include "rtc_base/stringencode.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/stringutils.h"
 #include "rtc_base/thread.h"
 
@@ -181,9 +182,9 @@
 std::string MakeNetworkKey(const std::string& name,
                            const IPAddress& prefix,
                            int prefix_length) {
-  std::ostringstream ost;
+  rtc::StringBuilder ost;
   ost << name << "%" << prefix.ToString() << "/" << prefix_length;
-  return ost.str();
+  return ost.Release();
 }
 // Test if the network name matches the type<number> pattern, e.g. eth0. The
 // matching is case-sensitive.
@@ -259,6 +260,10 @@
   return false;
 }
 
+webrtc::MDnsResponderInterface* NetworkManager::GetMDnsResponder() const {
+  return nullptr;
+}
+
 NetworkManagerBase::NetworkManagerBase()
     : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED),
       ipv6_enabled_(true) {}
@@ -281,6 +286,7 @@
         new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY));
     ipv4_any_address_network_->set_default_local_address_provider(this);
     ipv4_any_address_network_->AddIP(ipv4_any_address);
+    ipv4_any_address_network_->SetMDnsResponder(GetMDnsResponder());
   }
   networks->push_back(ipv4_any_address_network_.get());
 
@@ -291,6 +297,7 @@
           "any", "any", ipv6_any_address, 0, ADAPTER_TYPE_ANY));
       ipv6_any_address_network_->set_default_local_address_provider(this);
       ipv6_any_address_network_->AddIP(ipv6_any_address);
+      ipv6_any_address_network_->SetMDnsResponder(GetMDnsResponder());
     }
     networks->push_back(ipv6_any_address_network_.get());
   }
@@ -380,6 +387,7 @@
         delete net;
       }
     }
+    networks_map_[key]->SetMDnsResponder(GetMDnsResponder());
   }
   // It may still happen that the merged list is a subset of |networks_|.
   // To detect this change, we compare their sizes.
@@ -1057,7 +1065,7 @@
 }
 
 std::string Network::ToString() const {
-  std::stringstream ss;
+  rtc::StringBuilder ss;
   // Print out the first space-terminated token of the network desc, plus
   // the IP address.
   ss << "Net[" << description_.substr(0, description_.find(' ')) << ":"
@@ -1067,7 +1075,7 @@
     ss << "/" << AdapterTypeToString(underlying_type_for_vpn_);
   }
   ss << ":id=" << id_ << "]";
-  return ss.str();
+  return ss.Release();
 }
 
 }  // namespace rtc
diff --git a/rtc_base/network.h b/rtc_base/network.h
index ad932d0..601e39f 100644
--- a/rtc_base/network.h
+++ b/rtc_base/network.h
@@ -20,6 +20,7 @@
 #include <vector>
 
 #include "rtc_base/ipaddress.h"
+#include "rtc_base/mdns_responder_interface.h"
 #include "rtc_base/messagehandler.h"
 #include "rtc_base/networkmonitor.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
@@ -111,7 +112,7 @@
   // include ignored networks.
   virtual void GetNetworks(NetworkList* networks) const = 0;
 
-  // return the current permission state of GetNetworks()
+  // Returns the current permission state of GetNetworks().
   virtual EnumerationPermission enumeration_permission() const;
 
   // "AnyAddressNetwork" is a network which only contains single "any address"
@@ -137,6 +138,10 @@
       ipv6_network_count = 0;
     }
   };
+
+  // Returns the mDNS responder that can be used to obfuscate the local IP
+  // addresses of ICE host candidates by mDNS hostnames.
+  virtual webrtc::MDnsResponderInterface* GetMDnsResponder() const;
 };
 
 // Base class for NetworkManager implementations.
@@ -356,6 +361,19 @@
   const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
   // Clear the network's list of addresses.
   void ClearIPs() { ips_.clear(); }
+  // Sets the mDNS responder that can be used to obfuscate the local IP
+  // addresses of host candidates by mDNS names in ICE gathering. After a
+  // name-address mapping is created by the mDNS responder, queries for the
+  // created name will be resolved by the responder.
+  //
+  // The mDNS responder, if not null, should outlive this rtc::Network.
+  void SetMDnsResponder(webrtc::MDnsResponderInterface* mdns_responder) {
+    mdns_responder_ = mdns_responder;
+  }
+  // Returns the mDNS responder, which is null by default.
+  webrtc::MDnsResponderInterface* GetMDnsResponder() const {
+    return mdns_responder_;
+  }
 
   // Returns the scope-id of the network's address.
   // Should only be relevant for link-local IPv6 addresses.
@@ -428,6 +446,7 @@
   int prefix_length_;
   std::string key_;
   std::vector<InterfaceAddress> ips_;
+  webrtc::MDnsResponderInterface* mdns_responder_ = nullptr;
   int scope_id_;
   bool ignored_;
   AdapterType type_;
diff --git a/rtc_base/networkmonitor.cc b/rtc_base/networkmonitor.cc
index ad6805a..e3b2efd 100644
--- a/rtc_base/networkmonitor.cc
+++ b/rtc_base/networkmonitor.cc
@@ -11,6 +11,7 @@
 #include "rtc_base/networkmonitor.h"
 
 #include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
 
 namespace {
 const uint32_t UPDATE_NETWORKS_MESSAGE = 1;
diff --git a/rtc_base/networkmonitor.h b/rtc_base/networkmonitor.h
index a84a30a..1ad7663 100644
--- a/rtc_base/networkmonitor.h
+++ b/rtc_base/networkmonitor.h
@@ -11,7 +11,6 @@
 #ifndef RTC_BASE_NETWORKMONITOR_H_
 #define RTC_BASE_NETWORKMONITOR_H_
 
-#include "rtc_base/logging.h"
 #include "rtc_base/network_constants.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread.h"
diff --git a/rtc_base/numerics/sample_counter.cc b/rtc_base/numerics/sample_counter.cc
index d9244c3..bab47f1 100644
--- a/rtc_base/numerics/sample_counter.cc
+++ b/rtc_base/numerics/sample_counter.cc
@@ -26,11 +26,6 @@
     RTC_DCHECK_GE(sample, std::numeric_limits<int64_t>::min() - sum_);
   }
   sum_ += sample;
-  // Prevent overflow in squaring.
-  RTC_DCHECK_GT(sample, std::numeric_limits<int32_t>::min());
-  RTC_DCHECK_LE(int64_t{sample} * sample,
-                std::numeric_limits<int64_t>::max() - sum_squared_);
-  sum_squared_ += int64_t{sample} * sample;
   ++num_samples_;
   if (!max_ || sample > *max_) {
     max_ = sample;
@@ -44,9 +39,6 @@
     RTC_DCHECK_GE(other.sum_, std::numeric_limits<int64_t>::min() - sum_);
   }
   sum_ += other.sum_;
-  RTC_DCHECK_LE(other.sum_squared_,
-                std::numeric_limits<int64_t>::max() - sum_squared_);
-  sum_squared_ += other.sum_squared_;
   RTC_DCHECK_LE(other.num_samples_,
                 std::numeric_limits<int64_t>::max() - num_samples_);
   num_samples_ += other.num_samples_;
@@ -61,7 +53,22 @@
   return rtc::dchecked_cast<int>(sum_ / num_samples_);
 }
 
-absl::optional<int64_t> SampleCounter::Variance(
+absl::optional<int> SampleCounter::Max() const {
+  return max_;
+}
+
+int64_t SampleCounter::NumSamples() const {
+  return num_samples_;
+}
+
+void SampleCounter::Reset() {
+  *this = {};
+}
+
+SampleCounterWithVariance::SampleCounterWithVariance() = default;
+SampleCounterWithVariance::~SampleCounterWithVariance() = default;
+
+absl::optional<int64_t> SampleCounterWithVariance::Variance(
     int64_t min_required_samples) const {
   RTC_DCHECK_GT(min_required_samples, 0);
   if (num_samples_ < min_required_samples)
@@ -71,11 +78,23 @@
   return sum_squared_ / num_samples_ - mean * mean;
 }
 
-absl::optional<int> SampleCounter::Max() const {
-  return max_;
+void SampleCounterWithVariance::Add(int sample) {
+  SampleCounter::Add(sample);
+  // Prevent overflow in squaring.
+  RTC_DCHECK_GT(sample, std::numeric_limits<int32_t>::min());
+  RTC_DCHECK_LE(int64_t{sample} * sample,
+                std::numeric_limits<int64_t>::max() - sum_squared_);
+  sum_squared_ += int64_t{sample} * sample;
 }
 
-void SampleCounter::Reset() {
+void SampleCounterWithVariance::Add(const SampleCounterWithVariance& other) {
+  SampleCounter::Add(other);
+  RTC_DCHECK_LE(other.sum_squared_,
+                std::numeric_limits<int64_t>::max() - sum_squared_);
+  sum_squared_ += other.sum_squared_;
+}
+
+void SampleCounterWithVariance::Reset() {
   *this = {};
 }
 
diff --git a/rtc_base/numerics/sample_counter.h b/rtc_base/numerics/sample_counter.h
index 643754e..4fe71d1 100644
--- a/rtc_base/numerics/sample_counter.h
+++ b/rtc_base/numerics/sample_counter.h
@@ -23,19 +23,33 @@
   ~SampleCounter();
   void Add(int sample);
   absl::optional<int> Avg(int64_t min_required_samples) const;
-  absl::optional<int64_t> Variance(int64_t min_required_samples) const;
   absl::optional<int> Max() const;
+  int64_t NumSamples() const;
   void Reset();
   // Adds all the samples from the |other| SampleCounter as if they were all
   // individually added using |Add(int)| method.
   void Add(const SampleCounter& other);
 
- private:
+ protected:
   int64_t sum_ = 0;
-  int64_t sum_squared_ = 0;
   int64_t num_samples_ = 0;
   absl::optional<int> max_;
 };
 
+class SampleCounterWithVariance : public SampleCounter {
+ public:
+  SampleCounterWithVariance();
+  ~SampleCounterWithVariance();
+  void Add(int sample);
+  absl::optional<int64_t> Variance(int64_t min_required_samples) const;
+  void Reset();
+  // Adds all the samples from the |other| SampleCounter as if they were all
+  // individually added using |Add(int)| method.
+  void Add(const SampleCounterWithVariance& other);
+
+ private:
+  int64_t sum_squared_ = 0;
+};
+
 }  // namespace rtc
 #endif  // RTC_BASE_NUMERICS_SAMPLE_COUNTER_H_
diff --git a/rtc_base/numerics/sample_counter_unittest.cc b/rtc_base/numerics/sample_counter_unittest.cc
index 898f00c..4085370 100644
--- a/rtc_base/numerics/sample_counter_unittest.cc
+++ b/rtc_base/numerics/sample_counter_unittest.cc
@@ -24,7 +24,6 @@
   constexpr int kMinSamples = 1;
   SampleCounter counter;
   EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
-  EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
   EXPECT_THAT(counter.Max(), Eq(absl::nullopt));
 }
 
@@ -35,7 +34,6 @@
     counter.Add(value);
   }
   EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
-  EXPECT_THAT(counter.Avg(kMinSamples), Eq(absl::nullopt));
   EXPECT_THAT(counter.Max(), Eq(5));
 }
 
@@ -46,8 +44,36 @@
     counter.Add(value);
   }
   EXPECT_THAT(counter.Avg(kMinSamples), Eq(3));
-  EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
   EXPECT_THAT(counter.Max(), Eq(5));
 }
 
+TEST(SampleCounterTest, ComputesVariance) {
+  constexpr int kMinSamples = 5;
+  SampleCounterWithVariance counter;
+  for (int value : {1, 2, 3, 4, 5}) {
+    counter.Add(value);
+  }
+  EXPECT_THAT(counter.Variance(kMinSamples), Eq(2));
+}
+
+TEST(SampleCounterTest, AggregatesTwoCounters) {
+  constexpr int kMinSamples = 5;
+  SampleCounterWithVariance counter1;
+  for (int value : {1, 2, 3}) {
+    counter1.Add(value);
+  }
+  SampleCounterWithVariance counter2;
+  for (int value : {4, 5}) {
+    counter2.Add(value);
+  }
+  // Before aggregation there is not enough samples.
+  EXPECT_THAT(counter1.Avg(kMinSamples), Eq(absl::nullopt));
+  EXPECT_THAT(counter1.Variance(kMinSamples), Eq(absl::nullopt));
+  // Aggregate counter2 in counter1.
+  counter1.Add(counter2);
+  EXPECT_THAT(counter1.Avg(kMinSamples), Eq(3));
+  EXPECT_THAT(counter1.Max(), Eq(5));
+  EXPECT_THAT(counter1.Variance(kMinSamples), Eq(2));
+}
+
 }  // namespace rtc
diff --git a/rtc_base/openssladapter.cc b/rtc_base/openssladapter.cc
index 50284a6..b589195 100644
--- a/rtc_base/openssladapter.cc
+++ b/rtc_base/openssladapter.cc
@@ -23,6 +23,7 @@
 #include <openssl/x509v3.h>
 #include "rtc_base/openssl.h"
 
+#include "absl/memory/memory.h"  // for make_unique
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
diff --git a/rtc_base/openssladapter.h b/rtc_base/openssladapter.h
index 50a7c08..45ffc6f 100644
--- a/rtc_base/openssladapter.h
+++ b/rtc_base/openssladapter.h
@@ -13,19 +13,22 @@
 
 #include <openssl/ossl_typ.h>
 
-#include <map>
 #include <memory>
 #include <string>
 #include <vector>
 
-#include "absl/memory/memory.h"
-#include "rtc_base/buffer.h"
-#include "rtc_base/messagehandler.h"
-#include "rtc_base/messagequeue.h"
-#include "rtc_base/opensslcertificate.h"
-#include "rtc_base/opensslidentity.h"
-#include "rtc_base/opensslsessioncache.h"
-#include "rtc_base/ssladapter.h"
+#include "rtc_base/asyncsocket.h"          // for AsyncSocket
+#include "rtc_base/buffer.h"               // for Buffer
+#include "rtc_base/messagehandler.h"       // for MessageHandler
+#include "rtc_base/messagequeue.h"         // for Message
+#include "rtc_base/opensslidentity.h"      // for SSL_CTX, OpenSSLIdentity
+#include "rtc_base/opensslsessioncache.h"  // for OpenSSLSessionCache
+#include "rtc_base/socket.h"               // for Socket::ConnState
+#include "rtc_base/socketaddress.h"        // for SocketAddress
+#include "rtc_base/ssladapter.h"           // for SSLAdapter, SSLAdapterFactory
+#include "rtc_base/sslcertificate.h"       // for SSLCertificateVerifier
+#include "rtc_base/sslidentity.h"          // for SSLIdentity
+#include "rtc_base/sslstreamadapter.h"     // for SSLMode, SSLRole, SSL_MODE...
 
 namespace rtc {
 
diff --git a/rtc_base/opensslcertificate.h b/rtc_base/opensslcertificate.h
index c730ffd..b7ecc3b 100644
--- a/rtc_base/opensslcertificate.h
+++ b/rtc_base/opensslcertificate.h
@@ -11,16 +11,16 @@
 #ifndef RTC_BASE_OPENSSLCERTIFICATE_H_
 #define RTC_BASE_OPENSSLCERTIFICATE_H_
 
-#include <openssl/evp.h>
-#include <openssl/x509.h>
+#include <openssl/base.h>  // for X509, ssl_ctx_st
 
-#include <memory>
+#include <stddef.h>  // for size_t
+#include <stdint.h>  // for int64_t
 #include <string>
 
-#include "rtc_base/checks.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/sslcertificate.h"
-#include "rtc_base/sslidentity.h"
+#include "rtc_base/buffer.h"            // for Buffer
+#include "rtc_base/constructormagic.h"  // for RTC_DISALLOW_COPY_AND_ASSIGN
+#include "rtc_base/sslcertificate.h"    // for SSLCertificate
+#include "rtc_base/sslidentity.h"       // for SSLIdentityParams
 
 typedef struct ssl_ctx_st SSL_CTX;
 
diff --git a/rtc_base/openssldigest.h b/rtc_base/openssldigest.h
index c4cd1e0..1427a94 100644
--- a/rtc_base/openssldigest.h
+++ b/rtc_base/openssldigest.h
@@ -11,7 +11,8 @@
 #ifndef RTC_BASE_OPENSSLDIGEST_H_
 #define RTC_BASE_OPENSSLDIGEST_H_
 
-#include <openssl/evp.h>
+#include <openssl/base.h>  // for EVP_MD, EVP_MD_CTX
+#include <string>
 
 #include "rtc_base/messagedigest.h"
 
diff --git a/rtc_base/opensslidentity.h b/rtc_base/opensslidentity.h
index 3404427..b72a4c2 100644
--- a/rtc_base/opensslidentity.h
+++ b/rtc_base/opensslidentity.h
@@ -11,16 +11,17 @@
 #ifndef RTC_BASE_OPENSSLIDENTITY_H_
 #define RTC_BASE_OPENSSLIDENTITY_H_
 
-#include <openssl/evp.h>
-#include <openssl/x509.h>
+#include <openssl/base.h>  // for EVP_PKEY, ssl_ctx_st
 
-#include <memory>
+#include <ctime>   // for time_t
+#include <memory>  // for unique_ptr
 #include <string>
 
-#include "rtc_base/checks.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/opensslcertificate.h"
-#include "rtc_base/sslidentity.h"
+#include "rtc_base/checks.h"              // for RTC_DCHECK
+#include "rtc_base/constructormagic.h"    // for RTC_DISALLOW_COPY_AND_ASSIGN
+#include "rtc_base/opensslcertificate.h"  // for OpenSSLCertificate
+#include "rtc_base/sslcertificate.h"      // for SSLCertChain
+#include "rtc_base/sslidentity.h"         // for SSLIdentity, KeyParams, SSL...
 
 typedef struct ssl_ctx_st SSL_CTX;
 
diff --git a/rtc_base/opensslutility.h b/rtc_base/opensslutility.h
index 7cb38b5..77ed0b1 100644
--- a/rtc_base/opensslutility.h
+++ b/rtc_base/opensslutility.h
@@ -13,7 +13,6 @@
 
 #include <openssl/ossl_typ.h>
 #include <string>
-#include "rtc_base/sslcertificate.h"
 
 namespace rtc {
 // The openssl namespace holds static helper methods. All methods related
diff --git a/rtc_base/pathutils.cc b/rtc_base/pathutils.cc
index 594deb7..0764671 100644
--- a/rtc_base/pathutils.cc
+++ b/rtc_base/pathutils.cc
@@ -15,10 +15,9 @@
 #include <tchar.h>
 #endif  // WEBRTC_WIN
 
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
+#include <string.h>  // for strchr
+
 #include "rtc_base/pathutils.h"
-#include "rtc_base/stringutils.h"
 
 namespace rtc {
 
diff --git a/rtc_base/pathutils.h b/rtc_base/pathutils.h
index 9543be0..59f2a4a 100644
--- a/rtc_base/pathutils.h
+++ b/rtc_base/pathutils.h
@@ -13,8 +13,6 @@
 
 #include <string>
 
-#include "rtc_base/checks.h"
-
 namespace rtc {
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/rtc_base/physicalsocketserver.cc b/rtc_base/physicalsocketserver.cc
index ca78499..4ad2857 100644
--- a/rtc_base/physicalsocketserver.cc
+++ b/rtc_base/physicalsocketserver.cc
@@ -50,7 +50,6 @@
 #include "rtc_base/networkmonitor.h"
 #include "rtc_base/nullsocketserver.h"
 #include "rtc_base/timeutils.h"
-#include "rtc_base/win32socketinit.h"
 
 #if defined(WEBRTC_WIN)
 #define LAST_SYSTEM_ERROR (::GetLastError())
@@ -117,14 +116,6 @@
       error_(0),
       state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED),
       resolver_(nullptr) {
-#if defined(WEBRTC_WIN)
-  // EnsureWinsockInit() ensures that winsock is initialized. The default
-  // version of this function doesn't do anything because winsock is
-  // initialized by constructor of a static object. If neccessary libjingle
-  // users can link it with a different version of this function by replacing
-  // win32socketinit.cc. See win32socketinit.cc for more details.
-  EnsureWinsockInit();
-#endif
   if (s_ != INVALID_SOCKET) {
     SetEnabledEvents(DE_READ | DE_WRITE);
 
@@ -1390,21 +1381,15 @@
 
   struct timeval* ptvWait = nullptr;
   struct timeval tvWait;
-  struct timeval tvStop;
+  int64_t stop_us;
   if (cmsWait != kForever) {
     // Calculate wait timeval
     tvWait.tv_sec = cmsWait / 1000;
     tvWait.tv_usec = (cmsWait % 1000) * 1000;
     ptvWait = &tvWait;
 
-    // Calculate when to return in a timeval
-    gettimeofday(&tvStop, nullptr);
-    tvStop.tv_sec += tvWait.tv_sec;
-    tvStop.tv_usec += tvWait.tv_usec;
-    if (tvStop.tv_usec >= 1000000) {
-      tvStop.tv_usec -= 1000000;
-      tvStop.tv_sec += 1;
-    }
+    // Calculate when to return
+    stop_us = rtc::TimeMicros() + cmsWait * 1000;
   }
 
   // Zero all fd_sets. Don't need to do this inside the loop since
@@ -1501,17 +1486,10 @@
     if (ptvWait) {
       ptvWait->tv_sec = 0;
       ptvWait->tv_usec = 0;
-      struct timeval tvT;
-      gettimeofday(&tvT, nullptr);
-      if ((tvStop.tv_sec > tvT.tv_sec) ||
-          ((tvStop.tv_sec == tvT.tv_sec) && (tvStop.tv_usec > tvT.tv_usec))) {
-        ptvWait->tv_sec = tvStop.tv_sec - tvT.tv_sec;
-        ptvWait->tv_usec = tvStop.tv_usec - tvT.tv_usec;
-        if (ptvWait->tv_usec < 0) {
-          RTC_DCHECK(ptvWait->tv_sec > 0);
-          ptvWait->tv_usec += 1000000;
-          ptvWait->tv_sec -= 1;
-        }
+      int64_t time_left_us = stop_us - rtc::TimeMicros();
+      if (time_left_us > 0) {
+        ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec;
+        ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec;
       }
     }
   }
diff --git a/rtc_base/platform_file.cc b/rtc_base/platform_file.cc
index a4c906a..d74acdd 100644
--- a/rtc_base/platform_file.cc
+++ b/rtc_base/platform_file.cc
@@ -10,10 +10,9 @@
 
 #include "rtc_base/platform_file.h"
 
-#include "rtc_base/stringutils.h"
-
 #if defined(WEBRTC_WIN)
 #include <io.h>
+#include "rtc_base/stringutils.h"  // For ToUtf16
 #else
 #include <fcntl.h>
 #include <sys/stat.h>
diff --git a/rtc_base/platform_thread.h b/rtc_base/platform_thread.h
index 33921c2..ea67aca 100644
--- a/rtc_base/platform_thread.h
+++ b/rtc_base/platform_thread.h
@@ -14,7 +14,6 @@
 #include <string>
 
 #include "rtc_base/constructormagic.h"
-#include "rtc_base/event.h"
 #include "rtc_base/platform_thread_types.h"
 #include "rtc_base/thread_checker.h"
 
diff --git a/rtc_base/proxy_unittest.cc b/rtc_base/proxy_unittest.cc
index d81b312..f42039f 100644
--- a/rtc_base/proxy_unittest.cc
+++ b/rtc_base/proxy_unittest.cc
@@ -11,7 +11,6 @@
 #include <memory>
 #include <string>
 #include "rtc_base/gunit.h"
-#include "rtc_base/httpserver.h"
 #include "rtc_base/proxyserver.h"
 #include "rtc_base/socketadapters.h"
 #include "rtc_base/testclient.h"
@@ -24,18 +23,14 @@
 
 static const SocketAddress kSocksProxyIntAddr("1.2.3.4", 1080);
 static const SocketAddress kSocksProxyExtAddr("1.2.3.5", 0);
-static const SocketAddress kHttpsProxyIntAddr("1.2.3.4", 443);
-static const SocketAddress kHttpsProxyExtAddr("1.2.3.5", 0);
 static const SocketAddress kBogusProxyIntAddr("1.2.3.4", 999);
 
-// Sets up a virtual socket server and HTTPS/SOCKS5 proxy servers.
+// Sets up a virtual socket server and a SOCKS5 proxy server.
 class ProxyTest : public testing::Test {
  public:
   ProxyTest() : ss_(new rtc::VirtualSocketServer()), thread_(ss_.get()) {
     socks_.reset(new rtc::SocksProxyServer(ss_.get(), kSocksProxyIntAddr,
                                            ss_.get(), kSocksProxyExtAddr));
-    https_.reset(new rtc::HttpListenServer());
-    https_->Listen(kHttpsProxyIntAddr);
   }
 
   rtc::SocketServer* ss() { return ss_.get(); }
@@ -44,8 +39,6 @@
   std::unique_ptr<rtc::SocketServer> ss_;
   rtc::AutoSocketServerThread thread_;
   std::unique_ptr<rtc::SocksProxyServer> socks_;
-  // TODO: Make this a real HTTPS proxy server.
-  std::unique_ptr<rtc::HttpListenServer> https_;
 };
 
 // Tests whether we can use a SOCKS5 proxy to connect to a server.
diff --git a/rtc_base/proxyserver.cc b/rtc_base/proxyserver.cc
index 55cab80..71c4879 100644
--- a/rtc_base/proxyserver.cc
+++ b/rtc_base/proxyserver.cc
@@ -13,6 +13,7 @@
 #include <algorithm>
 
 #include "rtc_base/checks.h"
+#include "rtc_base/logging.h"
 #include "rtc_base/socketfactory.h"
 
 namespace rtc {
diff --git a/rtc_base/race_checker.h b/rtc_base/race_checker.h
index 73567e9..d6eba08 100644
--- a/rtc_base/race_checker.h
+++ b/rtc_base/race_checker.h
@@ -12,7 +12,7 @@
 #define RTC_BASE_RACE_CHECKER_H_
 
 #include "rtc_base/checks.h"
-#include "rtc_base/platform_thread.h"
+#include "rtc_base/platform_thread_types.h"  // for PlatformThreadRef
 #include "rtc_base/thread_annotations.h"
 
 namespace rtc {
diff --git a/rtc_base/ratetracker.cc b/rtc_base/ratetracker.cc
index e31d266..7c96ca9 100644
--- a/rtc_base/ratetracker.cc
+++ b/rtc_base/ratetracker.cc
@@ -10,8 +10,6 @@
 
 #include "rtc_base/ratetracker.h"
 
-#include <stddef.h>
-
 #include <algorithm>
 
 #include "rtc_base/checks.h"
diff --git a/rtc_base/rtccertificate.cc b/rtc_base/rtccertificate.cc
index 7f027ba..786333f 100644
--- a/rtc_base/rtccertificate.cc
+++ b/rtc_base/rtccertificate.cc
@@ -14,6 +14,7 @@
 
 #include "rtc_base/checks.h"
 #include "rtc_base/refcountedobject.h"
+#include "rtc_base/timeutils.h"
 
 namespace rtc {
 
diff --git a/rtc_base/sequenced_task_checker_unittest.cc b/rtc_base/sequenced_task_checker_unittest.cc
index 96e655b..83fb14f 100644
--- a/rtc_base/sequenced_task_checker_unittest.cc
+++ b/rtc_base/sequenced_task_checker_unittest.cc
@@ -12,6 +12,7 @@
 
 #include "rtc_base/checks.h"
 #include "rtc_base/constructormagic.h"
+#include "rtc_base/event.h"
 #include "rtc_base/platform_thread.h"
 #include "rtc_base/task_queue.h"
 #include "rtc_base/thread_checker.h"
diff --git a/rtc_base/signalthread.cc b/rtc_base/signalthread.cc
index 58f8761..2e0fa0c 100644
--- a/rtc_base/signalthread.cc
+++ b/rtc_base/signalthread.cc
@@ -12,6 +12,7 @@
 
 #include "absl/memory/memory.h"
 #include "rtc_base/checks.h"
+#include "rtc_base/nullsocketserver.h"
 
 namespace rtc {
 
@@ -151,7 +152,7 @@
   main_ = nullptr;
 }
 
-bool SignalThread::Worker::IsProcessingMessages() {
+bool SignalThread::Worker::IsProcessingMessagesForTesting() {
   return false;
 }
 
diff --git a/rtc_base/signalthread.h b/rtc_base/signalthread.h
index be54d9c..448b289 100644
--- a/rtc_base/signalthread.h
+++ b/rtc_base/signalthread.h
@@ -14,10 +14,13 @@
 #include <string>
 
 #include "rtc_base/checks.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/nullsocketserver.h"
-#include "rtc_base/third_party/sigslot/sigslot.h"
-#include "rtc_base/thread.h"
+#include "rtc_base/constructormagic.h"             // for RTC_DISALLOW_IMPLI...
+#include "rtc_base/criticalsection.h"              // for CriticalSection
+#include "rtc_base/messagehandler.h"               // for MessageHandler
+#include "rtc_base/messagequeue.h"                 // for Message
+#include "rtc_base/third_party/sigslot/sigslot.h"  // for has_slots, signal_...
+#include "rtc_base/thread.h"                       // for Thread
+#include "rtc_base/thread_annotations.h"           // for RTC_EXCLUSIVE_LOCK...
 
 namespace rtc {
 
@@ -105,7 +108,7 @@
     explicit Worker(SignalThread* parent);
     ~Worker() override;
     void Run() override;
-    bool IsProcessingMessages() override;
+    bool IsProcessingMessagesForTesting() override;
 
    private:
     SignalThread* parent_;
diff --git a/rtc_base/socket.h b/rtc_base/socket.h
index b8290bb..2a3d61d 100644
--- a/rtc_base/socket.h
+++ b/rtc_base/socket.h
@@ -145,6 +145,8 @@
   PacketInfo(const PacketInfo& info);
   ~PacketInfo();
 
+  bool included_in_feedback;
+  bool included_in_allocation;
   PacketType packet_type = PacketType::kUnknown;
   PacketInfoProtocolType protocol = PacketInfoProtocolType::kUnknown;
   // A unique id assigned by the network manager, and absl::nullopt if not set.
diff --git a/rtc_base/socketadapters.cc b/rtc_base/socketadapters.cc
index 8095894..98be868 100644
--- a/rtc_base/socketadapters.cc
+++ b/rtc_base/socketadapters.cc
@@ -30,7 +30,7 @@
 #include "rtc_base/httpcommon.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/socketadapters.h"
-#include "rtc_base/stringencode.h"
+#include "rtc_base/strings/string_builder.h"
 #include "rtc_base/stringutils.h"
 #include "rtc_base/zero_memory.h"
 
@@ -369,7 +369,7 @@
 }
 
 void AsyncHttpsProxySocket::SendRequest() {
-  std::stringstream ss;
+  rtc::StringBuilder ss;
   ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n";
   ss << "User-Agent: " << agent_ << "\r\n";
   ss << "Host: " << dest_.HostAsURIString() << "\r\n";
diff --git a/rtc_base/socketadapters.h b/rtc_base/socketadapters.h
index ad88fe6..062f75c 100644
--- a/rtc_base/socketadapters.h
+++ b/rtc_base/socketadapters.h
@@ -11,13 +11,11 @@
 #ifndef RTC_BASE_SOCKETADAPTERS_H_
 #define RTC_BASE_SOCKETADAPTERS_H_
 
-#include <map>
 #include <string>
 
 #include "rtc_base/asyncsocket.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/cryptstring.h"
-#include "rtc_base/logging.h"
 
 namespace rtc {
 
diff --git a/rtc_base/socketaddress.h b/rtc_base/socketaddress.h
index 7095be1..b1a52b9 100644
--- a/rtc_base/socketaddress.h
+++ b/rtc_base/socketaddress.h
@@ -11,12 +11,10 @@
 #ifndef RTC_BASE_SOCKETADDRESS_H_
 #define RTC_BASE_SOCKETADDRESS_H_
 
-#include <iosfwd>
 #include <string>
 #ifdef UNIT_TEST
 #include <ostream>  // no-presubmit-check TODO(webrtc:8982)
 #endif              // UNIT_TEST
-#include <vector>
 #include "rtc_base/ipaddress.h"
 
 #undef SetPort
diff --git a/rtc_base/ssladapter_unittest.cc b/rtc_base/ssladapter_unittest.cc
index ec532b1..8ed460f 100644
--- a/rtc_base/ssladapter_unittest.cc
+++ b/rtc_base/ssladapter_unittest.cc
@@ -15,6 +15,7 @@
 #include "absl/memory/memory.h"
 #include "rtc_base/gunit.h"
 #include "rtc_base/ipaddress.h"
+#include "rtc_base/messagedigest.h"
 #include "rtc_base/socketstream.h"
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/sslidentity.h"
diff --git a/rtc_base/sslcertificate.cc b/rtc_base/sslcertificate.cc
index 9a38fc0..e40feec 100644
--- a/rtc_base/sslcertificate.cc
+++ b/rtc_base/sslcertificate.cc
@@ -10,16 +10,15 @@
 
 #include "rtc_base/sslcertificate.h"
 
-#include <ctime>
+#include <algorithm>  // for transform
 #include <string>
 #include <utility>
 
-#include "absl/memory/memory.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/opensslcertificate.h"
-#include "rtc_base/sslfingerprint.h"
-#include "rtc_base/third_party/base64/base64.h"
+#include "absl/memory/memory.h"                  // for WrapUnique, make_unique
+#include "rtc_base/checks.h"                     // for FatalLogCall, RTC_DC...
+#include "rtc_base/opensslcertificate.h"         // for OpenSSLCertificate
+#include "rtc_base/sslfingerprint.h"             // for SSLFingerprint
+#include "rtc_base/third_party/base64/base64.h"  // for Base64
 
 namespace rtc {
 
diff --git a/rtc_base/sslcertificate.h b/rtc_base/sslcertificate.h
index 29c4db5..029404c 100644
--- a/rtc_base/sslcertificate.h
+++ b/rtc_base/sslcertificate.h
@@ -15,15 +15,12 @@
 #ifndef RTC_BASE_SSLCERTIFICATE_H_
 #define RTC_BASE_SSLCERTIFICATE_H_
 
-#include <algorithm>
 #include <memory>
 #include <string>
 #include <vector>
 
 #include "rtc_base/buffer.h"
 #include "rtc_base/constructormagic.h"
-#include "rtc_base/messagedigest.h"
-#include "rtc_base/timeutils.h"
 
 namespace rtc {
 
diff --git a/rtc_base/sslfingerprint.cc b/rtc_base/sslfingerprint.cc
index b651a3d..4f1ae8f 100644
--- a/rtc_base/sslfingerprint.cc
+++ b/rtc_base/sslfingerprint.cc
@@ -13,7 +13,6 @@
 #include <ctype.h>
 #include <string>
 
-#include "rtc_base/helpers.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/messagedigest.h"
 #include "rtc_base/stringencode.h"
diff --git a/rtc_base/sslidentity.cc b/rtc_base/sslidentity.cc
index f286884..1d136d7 100644
--- a/rtc_base/sslidentity.cc
+++ b/rtc_base/sslidentity.cc
@@ -11,16 +11,15 @@
 // Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
 #include "rtc_base/sslidentity.h"
 
+#include <string.h>  // for strspn
 #include <ctime>
 #include <string>
-#include <utility>
 
-#include "absl/memory/memory.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
-#include "rtc_base/opensslidentity.h"
-#include "rtc_base/sslfingerprint.h"
-#include "rtc_base/third_party/base64/base64.h"
+#include "rtc_base/checks.h"                     // for FatalLogCall, RTC_DC...
+#include "rtc_base/opensslidentity.h"            // for OpenSSLIdentity
+#include "rtc_base/strings/string_builder.h"     // for StringBuilder
+#include "rtc_base/third_party/base64/base64.h"  // for Base64, Base64::DO_P...
+#include "rtc_base/timeutils.h"                  // for TmToSeconds
 
 namespace rtc {
 
@@ -116,7 +115,7 @@
 std::string SSLIdentity::DerToPem(const std::string& pem_type,
                                   const unsigned char* data,
                                   size_t length) {
-  std::stringstream result;
+  rtc::StringBuilder result;
 
   result << "-----BEGIN " << pem_type << "-----\n";
 
@@ -135,7 +134,7 @@
 
   result << "-----END " << pem_type << "-----\n";
 
-  return result.str();
+  return result.Release();
 }
 
 // static
diff --git a/rtc_base/sslidentity.h b/rtc_base/sslidentity.h
index 1379d73..d17d38b 100644
--- a/rtc_base/sslidentity.h
+++ b/rtc_base/sslidentity.h
@@ -13,16 +13,10 @@
 #ifndef RTC_BASE_SSLIDENTITY_H_
 #define RTC_BASE_SSLIDENTITY_H_
 
-#include <algorithm>
-#include <memory>
+#include <ctime>
 #include <string>
-#include <vector>
 
-#include "rtc_base/buffer.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/messagedigest.h"
 #include "rtc_base/sslcertificate.h"
-#include "rtc_base/timeutils.h"
 
 namespace rtc {
 
diff --git a/rtc_base/sslidentity_unittest.cc b/rtc_base/sslidentity_unittest.cc
index 132e240..68b5828 100644
--- a/rtc_base/sslidentity_unittest.cc
+++ b/rtc_base/sslidentity_unittest.cc
@@ -14,6 +14,7 @@
 #include "rtc_base/fakesslidentity.h"
 #include "rtc_base/gunit.h"
 #include "rtc_base/helpers.h"
+#include "rtc_base/messagedigest.h"
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/sslfingerprint.h"
 #include "rtc_base/sslidentity.h"
diff --git a/rtc_base/sslstreamadapter_unittest.cc b/rtc_base/sslstreamadapter_unittest.cc
index 3403bdb..389b0ea 100644
--- a/rtc_base/sslstreamadapter_unittest.cc
+++ b/rtc_base/sslstreamadapter_unittest.cc
@@ -17,6 +17,7 @@
 #include "rtc_base/checks.h"
 #include "rtc_base/gunit.h"
 #include "rtc_base/helpers.h"
+#include "rtc_base/messagedigest.h"
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/sslidentity.h"
 #include "rtc_base/sslstreamadapter.h"
diff --git a/rtc_base/stream.cc b/rtc_base/stream.cc
index 3f4d3dc..ea2e47a 100644
--- a/rtc_base/stream.cc
+++ b/rtc_base/stream.cc
@@ -15,21 +15,20 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#include <string.h>  // for memcpy, memmove, strlen
 #include <algorithm>
 #include <string>
 
 #include "rtc_base/checks.h"
-#include "rtc_base/logging.h"
+#include "rtc_base/location.h"  // for RTC_FROM_HERE
 #include "rtc_base/messagequeue.h"
 #include "rtc_base/stream.h"
-#include "rtc_base/stringencode.h"
-#include "rtc_base/stringutils.h"
 #include "rtc_base/thread.h"
-#include "rtc_base/timeutils.h"
 
 #if defined(WEBRTC_WIN)
 #include <windows.h>
 #define fileno _fileno
+#include "rtc_base/stringutils.h"  // for ToUtf16
 #endif
 
 namespace rtc {
@@ -124,10 +123,6 @@
   return false;
 }
 
-bool StreamInterface::GetAvailable(size_t* size) const {
-  return false;
-}
-
 bool StreamInterface::GetWriteRemaining(size_t* size) const {
   return false;
 }
@@ -192,10 +187,6 @@
   return stream_->GetSize(size);
 }
 
-bool StreamAdapterInterface::GetAvailable(size_t* size) const {
-  return stream_->GetAvailable(size);
-}
-
 bool StreamAdapterInterface::GetWriteRemaining(size_t* size) const {
   return stream_->GetWriteRemaining(size);
 }
@@ -379,18 +370,6 @@
   return true;
 }
 
-bool FileStream::GetAvailable(size_t* size) const {
-  RTC_DCHECK(nullptr != size);
-  if (!GetSize(size))
-    return false;
-  long result = ftell(file_);
-  if (result < 0)
-    return false;
-  if (size)
-    *size -= result;
-  return true;
-}
-
 bool FileStream::ReserveSize(size_t size) {
   // TODO: extend the file to the proper length
   return true;
@@ -496,12 +475,6 @@
   return true;
 }
 
-bool MemoryStreamBase::GetAvailable(size_t* size) const {
-  if (size)
-    *size = data_length_ - seek_position_;
-  return true;
-}
-
 bool MemoryStreamBase::ReserveSize(size_t size) {
   return (SR_SUCCESS == DoReserve(size, nullptr));
 }
diff --git a/rtc_base/stream.h b/rtc_base/stream.h
index ac8193b..7c6e618 100644
--- a/rtc_base/stream.h
+++ b/rtc_base/stream.h
@@ -18,7 +18,6 @@
 #include "rtc_base/buffer.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/criticalsection.h"
-#include "rtc_base/logging.h"
 #include "rtc_base/messagehandler.h"
 #include "rtc_base/messagequeue.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
@@ -118,12 +117,8 @@
   // be used.
   //
   // Even though several of these operations are related, you should
-  // always use whichever operation is most relevant.  For example, you may
-  // be tempted to use GetSize() and GetPosition() to deduce the result of
-  // GetAvailable().  However, a stream which is read-once may support the
-  // latter operation but not the former.
+  // always use whichever operation is most relevant.
   //
-
   // The following four methods are used to avoid copying data multiple times.
 
   // GetReadData returns a pointer to a buffer which is owned by the stream.
@@ -178,10 +173,6 @@
   // is not known.
   virtual bool GetSize(size_t* size) const;
 
-  // Return the number of Read()-able bytes remaining before end-of-stream.
-  // Returns false if not known.
-  virtual bool GetAvailable(size_t* size) const;
-
   // Return the number of Write()-able bytes remaining before end-of-stream.
   // Returns false if not known.
   virtual bool GetWriteRemaining(size_t* size) const;
@@ -293,7 +284,6 @@
   bool SetPosition(size_t position) override;
   bool GetPosition(size_t* position) const override;
   bool GetSize(size_t* size) const override;
-  bool GetAvailable(size_t* size) const override;
   bool GetWriteRemaining(size_t* size) const override;
   bool ReserveSize(size_t size) override;
   bool Flush() override;
@@ -349,7 +339,6 @@
   bool SetPosition(size_t position) override;
   bool GetPosition(size_t* position) const override;
   bool GetSize(size_t* size) const override;
-  bool GetAvailable(size_t* size) const override;
   bool ReserveSize(size_t size) override;
 
   bool Flush() override;
@@ -385,7 +374,6 @@
   bool SetPosition(size_t position) override;
   bool GetPosition(size_t* position) const override;
   bool GetSize(size_t* size) const override;
-  bool GetAvailable(size_t* size) const override;
   bool ReserveSize(size_t size) override;
 
   char* GetBuffer() { return buffer_; }
diff --git a/rtc_base/stream_unittest.cc b/rtc_base/stream_unittest.cc
index 11e0be0..616783c 100644
--- a/rtc_base/stream_unittest.cc
+++ b/rtc_base/stream_unittest.cc
@@ -60,8 +60,6 @@
 
   bool GetSize(size_t* size) const override { return false; }
 
-  bool GetAvailable(size_t* size) const override { return false; }
-
  private:
   size_t pos_;
 };
diff --git a/rtc_base/stringencode.cc b/rtc_base/stringencode.cc
index b410477..6a065cb 100644
--- a/rtc_base/stringencode.cc
+++ b/rtc_base/stringencode.cc
@@ -10,9 +10,6 @@
 
 #include "rtc_base/stringencode.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/stringutils.h"
@@ -478,6 +475,13 @@
   return std::string(&buf[0], len);
 }
 
+std::string ToString(const long double d) {
+  char buf[32];
+  const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
+  RTC_DCHECK_LE(len, arraysize(buf));
+  return std::string(&buf[0], len);
+}
+
 std::string ToString(const void* const p) {
   char buf[32];
   const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
diff --git a/rtc_base/stringencode.h b/rtc_base/stringencode.h
index 830ea77..5d436df 100644
--- a/rtc_base/stringencode.h
+++ b/rtc_base/stringencode.h
@@ -11,7 +11,6 @@
 #ifndef RTC_BASE_STRINGENCODE_H_
 #define RTC_BASE_STRINGENCODE_H_
 
-#include <sstream>
 #include <string>
 #include <vector>
 
@@ -164,6 +163,7 @@
 std::string ToString(unsigned long long int s);
 
 std::string ToString(double t);
+std::string ToString(long double t);
 
 std::string ToString(const void* p);
 
diff --git a/rtc_base/json.cc b/rtc_base/strings/json.cc
similarity index 96%
rename from rtc_base/json.cc
rename to rtc_base/strings/json.cc
index cc40bd6..efcb97a 100644
--- a/rtc_base/json.cc
+++ b/rtc_base/strings/json.cc
@@ -8,31 +8,29 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/json.h"
+#include "rtc_base/strings/json.h"
 
 #include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
 
-#include <sstream>
+#include "rtc_base/stringencode.h"
 
 namespace rtc {
 
 bool GetStringFromJson(const Json::Value& in, std::string* out) {
   if (!in.isString()) {
-    std::ostringstream s;
     if (in.isBool()) {
-      s << std::boolalpha << in.asBool();
+      *out = rtc::ToString(in.asBool());
     } else if (in.isInt()) {
-      s << in.asInt();
+      *out = rtc::ToString(in.asInt());
     } else if (in.isUInt()) {
-      s << in.asUInt();
+      *out = rtc::ToString(in.asUInt());
     } else if (in.isDouble()) {
-      s << in.asDouble();
+      *out = rtc::ToString(in.asDouble());
     } else {
       return false;
     }
-    *out = s.str();
   } else {
     *out = in.asString();
   }
diff --git a/rtc_base/json.h b/rtc_base/strings/json.h
similarity index 96%
rename from rtc_base/json.h
rename to rtc_base/strings/json.h
index b8e6d95..0cb9542 100644
--- a/rtc_base/json.h
+++ b/rtc_base/strings/json.h
@@ -8,8 +8,8 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#ifndef RTC_BASE_JSON_H_
-#define RTC_BASE_JSON_H_
+#ifndef RTC_BASE_STRINGS_JSON_H_
+#define RTC_BASE_STRINGS_JSON_H_
 
 #include <string>
 #include <vector>
@@ -85,4 +85,4 @@
 
 }  // namespace rtc
 
-#endif  // RTC_BASE_JSON_H_
+#endif  // RTC_BASE_STRINGS_JSON_H_
diff --git a/rtc_base/json_unittest.cc b/rtc_base/strings/json_unittest.cc
similarity index 98%
rename from rtc_base/json_unittest.cc
rename to rtc_base/strings/json_unittest.cc
index 17b126a..2215769 100644
--- a/rtc_base/json_unittest.cc
+++ b/rtc_base/strings/json_unittest.cc
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "rtc_base/json.h"
+#include "rtc_base/strings/json.h"
 
 #include <vector>
 
@@ -88,7 +88,7 @@
   EXPECT_TRUE(GetUIntFromJson(big_u, &out));
   EXPECT_EQ(0xFFFFFFFFU, out);
   EXPECT_FALSE(GetUIntFromJson(in_s, &out));
-  // TODO: Fail reading negative strings.
+  // TODO(bugs.webrtc.org/9804): Fail reading negative strings.
   // EXPECT_FALSE(GetUIntFromJson(in_si, &out));
   EXPECT_FALSE(GetUIntFromJson(in_i, &out));
   EXPECT_FALSE(GetUIntFromJson(big_sn, &out));
diff --git a/rtc_base/strings/string_builder.cc b/rtc_base/strings/string_builder.cc
index 70a14da..0dca938 100644
--- a/rtc_base/strings/string_builder.cc
+++ b/rtc_base/strings/string_builder.cc
@@ -10,6 +10,12 @@
 
 #include "rtc_base/strings/string_builder.h"
 
+#include <stdarg.h>  // for va_end, va_list, va_start
+#include <cstring>   // for strlen
+
+#include "rtc_base/checks.h"                // for FatalLogCall, RTC_DCHECK
+#include "rtc_base/numerics/safe_minmax.h"  // for SafeMin
+
 namespace rtc {
 
 SimpleStringBuilder::SimpleStringBuilder(rtc::ArrayView<char> buffer)
@@ -109,4 +115,24 @@
   return *this;
 }
 
+StringBuilder& StringBuilder::AppendFormat(const char* fmt, ...) {
+  va_list args, copy;
+  va_start(args, fmt);
+  va_copy(copy, args);
+  const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
+  va_end(copy);
+
+  RTC_DCHECK_GE(predicted_length, 0);
+  if (predicted_length > 0) {
+    const size_t size = str_.size();
+    str_.resize(size + predicted_length);
+    // Pass "+ 1" to vsnprintf to include space for the '\0'.
+    const int actual_length =
+        std::vsnprintf(&str_[size], predicted_length + 1, fmt, args);
+    RTC_DCHECK_GE(actual_length, 0);
+  }
+  va_end(args);
+  return *this;
+}
+
 }  // namespace rtc
diff --git a/rtc_base/strings/string_builder.h b/rtc_base/strings/string_builder.h
index 093954b..27001d1 100644
--- a/rtc_base/strings/string_builder.h
+++ b/rtc_base/strings/string_builder.h
@@ -12,12 +12,12 @@
 #define RTC_BASE_STRINGS_STRING_BUILDER_H_
 
 #include <cstdio>
-#include <cstring>
 #include <string>
+#include <utility>
 
+#include "absl/strings/string_view.h"
 #include "api/array_view.h"
-#include "rtc_base/checks.h"
-#include "rtc_base/numerics/safe_minmax.h"
+#include "rtc_base/stringencode.h"
 #include "rtc_base/stringutils.h"
 
 namespace rtc {
@@ -82,6 +82,95 @@
   size_t size_ = 0;
 };
 
+// A string builder that supports dynamic resizing while building a string.
+// The class is based around an instance of std::string and allows moving
+// ownership out of the class once the string has been built.
+// Note that this class uses the heap for allocations, so SimpleStringBuilder
+// might be more efficient for some use cases.
+class StringBuilder {
+ public:
+  StringBuilder() {}
+  explicit StringBuilder(absl::string_view s) : str_(s) {}
+
+  // TODO(tommi): Support construction from StringBuilder?
+  StringBuilder(const StringBuilder&) = delete;
+  StringBuilder& operator=(const StringBuilder&) = delete;
+
+  StringBuilder& operator<<(const absl::string_view str) {
+    str_.append(str.data(), str.length());
+    return *this;
+  }
+
+  StringBuilder& operator<<(char c) = delete;
+
+  StringBuilder& operator<<(int i) {
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(unsigned i) {
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(long i) {  // NOLINT
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(long long i) {  // NOLINT
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(unsigned long i) {  // NOLINT
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(unsigned long long i) {  // NOLINT
+    str_ += rtc::ToString(i);
+    return *this;
+  }
+
+  StringBuilder& operator<<(float f) {
+    str_ += rtc::ToString(f);
+    return *this;
+  }
+
+  StringBuilder& operator<<(double f) {
+    str_ += rtc::ToString(f);
+    return *this;
+  }
+
+  StringBuilder& operator<<(long double f) {
+    str_ += rtc::ToString(f);
+    return *this;
+  }
+
+  const std::string& str() const { return str_; }
+
+  void Clear() { str_.clear(); }
+
+  size_t size() const { return str_.size(); }
+
+  std::string Release() {
+    std::string ret = std::move(str_);
+    str_.clear();
+    return ret;
+  }
+
+  // Allows appending a printf style formatted string.
+  StringBuilder& AppendFormat(const char* fmt, ...)
+#if defined(__GNUC__)
+      __attribute__((__format__(__printf__, 2, 3)))
+#endif
+      ;
+
+ private:
+  std::string str_;
+};
+
 }  // namespace rtc
 
 #endif  // RTC_BASE_STRINGS_STRING_BUILDER_H_
diff --git a/rtc_base/strings/string_builder_unittest.cc b/rtc_base/strings/string_builder_unittest.cc
index aa570eb..878e128 100644
--- a/rtc_base/strings/string_builder_unittest.cc
+++ b/rtc_base/strings/string_builder_unittest.cc
@@ -140,4 +140,62 @@
 
 #endif
 
+////////////////////////////////////////////////////////////////////////////////
+// StringBuilder.
+
+TEST(StringBuilder, Limit) {
+  StringBuilder sb;
+  EXPECT_EQ(0u, sb.str().size());
+
+  sb << "012345678";
+  EXPECT_EQ(sb.str(), "012345678");
+}
+
+TEST(StringBuilder, NumbersAndChars) {
+  StringBuilder sb;
+  sb << 1 << ":" << 2.1 << ":" << 2.2f << ":" << 78187493520ll << ":"
+     << 78187493520ul;
+  EXPECT_THAT(sb.str(),
+              testing::MatchesRegex("1:2.10*:2.20*:78187493520:78187493520"));
+}
+
+TEST(StringBuilder, Format) {
+  StringBuilder sb;
+  sb << "Here we go - ";
+  sb.AppendFormat("This is a hex formatted value: 0x%08llx", 3735928559ULL);
+  EXPECT_EQ(sb.str(), "Here we go - This is a hex formatted value: 0xdeadbeef");
+}
+
+TEST(StringBuilder, StdString) {
+  StringBuilder sb;
+  std::string str = "does this work?";
+  sb << str;
+  EXPECT_EQ(str, sb.str());
+}
+
+TEST(StringBuilder, Release) {
+  StringBuilder sb;
+  std::string str =
+      "This string has to be of a moderate length, or we might "
+      "run into problems with small object optimizations.";
+  EXPECT_LT(sizeof(str), str.size());
+  sb << str;
+  EXPECT_EQ(str, sb.str());
+  const char* original_buffer = sb.str().c_str();
+  std::string moved = sb.Release();
+  EXPECT_TRUE(sb.str().empty());
+  EXPECT_EQ(str, moved);
+  EXPECT_EQ(original_buffer, moved.c_str());
+}
+
+TEST(StringBuilder, Reset) {
+  StringBuilder sb("abc");
+  sb << "def";
+  EXPECT_EQ("abcdef", sb.str());
+  sb.Clear();
+  EXPECT_TRUE(sb.str().empty());
+  sb << 123 << "!";
+  EXPECT_EQ("123!", sb.str());
+}
+
 }  // namespace rtc
diff --git a/rtc_base/stringutils.cc b/rtc_base/stringutils.cc
index bef128c..35153ab 100644
--- a/rtc_base/stringutils.cc
+++ b/rtc_base/stringutils.cc
@@ -7,11 +7,10 @@
  *  in the file PATENTS.  All contributing project authors may
  *  be found in the AUTHORS file in the root of the source tree.
  */
-#include <algorithm>
-#include <cstdio>
+
+#include "rtc_base/stringutils.h"
 
 #include "rtc_base/checks.h"
-#include "rtc_base/stringutils.h"
 
 namespace rtc {
 
@@ -146,4 +145,10 @@
   return std::string(buffer);
 }
 
+std::string LeftPad(char padding, unsigned length, std::string s) {
+  if (s.length() >= length)
+    return s;
+  return std::string(length - s.length(), padding) + s;
+}
+
 }  // namespace rtc
diff --git a/rtc_base/stringutils.h b/rtc_base/stringutils.h
index 7073df1..5739d21 100644
--- a/rtc_base/stringutils.h
+++ b/rtc_base/stringutils.h
@@ -329,6 +329,9 @@
 
 // TODO(jonasolsson): replace with absl::Hex when that becomes available.
 std::string ToHex(const int i);
+
+std::string LeftPad(char padding, unsigned length, std::string s);
+
 }  // namespace rtc
 
 #endif  // RTC_BASE_STRINGUTILS_H_
diff --git a/rtc_base/synchronization/rw_lock_wrapper.cc b/rtc_base/synchronization/rw_lock_wrapper.cc
index c8cd17e..fb46419 100644
--- a/rtc_base/synchronization/rw_lock_wrapper.cc
+++ b/rtc_base/synchronization/rw_lock_wrapper.cc
@@ -10,8 +10,6 @@
 
 #include "rtc_base/synchronization/rw_lock_wrapper.h"
 
-#include <assert.h>
-
 #if defined(_WIN32)
 #include "rtc_base/synchronization/rw_lock_win.h"
 #else
diff --git a/rtc_base/system/BUILD.gn b/rtc_base/system/BUILD.gn
index 64df84f..f22efe1 100644
--- a/rtc_base/system/BUILD.gn
+++ b/rtc_base/system/BUILD.gn
@@ -59,3 +59,9 @@
     "unused.h",
   ]
 }
+
+rtc_source_set("rtc_export") {
+  sources = [
+    "rtc_export.h",
+  ]
+}
diff --git a/rtc_base/system/rtc_export.h b/rtc_base/system/rtc_export.h
new file mode 100644
index 0000000..d1eb60a
--- /dev/null
+++ b/rtc_base/system/rtc_export.h
@@ -0,0 +1,43 @@
+/*
+ *  Copyright (c) 2018 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 RTC_BASE_SYSTEM_RTC_EXPORT_H_
+#define RTC_BASE_SYSTEM_RTC_EXPORT_H_
+
+// RTC_EXPORT is used to mark symbols as exported or imported when WebRTC is
+// built or used as a shared library.
+// When WebRTC is built as a static library the RTC_EXPORT macro expands to
+// nothing.
+
+#ifdef WEBRTC_ENABLE_SYMBOL_EXPORT
+
+#ifdef WEBRTC_WIN
+
+#ifdef WEBRTC_LIBRARY_IMPL
+#define RTC_EXPORT __declspec(dllexport)
+#else
+#define RTC_EXPORT __declspec(dllimport)
+#endif
+
+#else  // WEBRTC_WIN
+
+#if __has_attribute(visibility) && defined(WEBRTC_LIBRARY_IMPL)
+#define RTC_EXPORT __attribute__((visibility("default")))
+#endif
+
+#endif  // WEBRTC_WIN
+
+#endif  // WEBRTC_ENABLE_SYMBOL_EXPORT
+
+#ifndef RTC_EXPORT
+#define RTC_EXPORT
+#endif
+
+#endif  // RTC_BASE_SYSTEM_RTC_EXPORT_H_
diff --git a/rtc_base/task_queue.h b/rtc_base/task_queue.h
index b8b307c..888e203 100644
--- a/rtc_base/task_queue.h
+++ b/rtc_base/task_queue.h
@@ -18,6 +18,7 @@
 #include "absl/memory/memory.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/scoped_ref_ptr.h"
+#include "rtc_base/system/rtc_export.h"
 #include "rtc_base/thread_annotations.h"
 
 namespace rtc {
@@ -150,7 +151,7 @@
 // TaskQueue itself has been deleted or it may happen synchronously while the
 // TaskQueue instance is being deleted.  This may vary from one OS to the next
 // so assumptions about lifetimes of pending tasks should not be made.
-class RTC_LOCKABLE TaskQueue {
+class RTC_LOCKABLE RTC_EXPORT TaskQueue {
  public:
   // TaskQueue priority levels. On some platforms these will map to thread
   // priorities, on others such as Mac and iOS, GCD queue priorities.
diff --git a/rtc_base/task_queue_libevent.cc b/rtc_base/task_queue_libevent.cc
index 8dc9b15..000f463 100644
--- a/rtc_base/task_queue_libevent.cc
+++ b/rtc_base/task_queue_libevent.cc
@@ -10,11 +10,17 @@
 
 #include "rtc_base/task_queue.h"
 
+#include <errno.h>  // for EAGAIN, errno
 #include <fcntl.h>
+#include <pthread.h>  // for pthread_getspecific
 #include <signal.h>
-#include <string.h>
+#include <stdint.h>  // for uint32_t
+#include <time.h>    // for nanosleep, timespec
 #include <unistd.h>
 #include <list>
+#include <memory>       // for unique_ptr, allocator
+#include <type_traits>  // for remove_reference<>::...
+#include <utility>      // for move
 
 #include <event.h>
 #include "rtc_base/checks.h"
@@ -22,11 +28,13 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/safe_conversions.h"
 #include "rtc_base/platform_thread.h"
+#include "rtc_base/platform_thread_types.h"  // for CurrentThreadRef
 #include "rtc_base/refcount.h"
 #include "rtc_base/refcountedobject.h"
+#include "rtc_base/scoped_ref_ptr.h"  // for scoped_refptr
 #include "rtc_base/system/unused.h"
-#include "rtc_base/task_queue.h"
 #include "rtc_base/task_queue_posix.h"
+#include "rtc_base/thread_annotations.h"  // for RTC_GUARDED_BY
 #include "rtc_base/timeutils.h"
 
 namespace rtc {
diff --git a/rtc_base/task_queue_unittest.cc b/rtc_base/task_queue_unittest.cc
index cdf0d59..cedf68e 100644
--- a/rtc_base/task_queue_unittest.cc
+++ b/rtc_base/task_queue_unittest.cc
@@ -175,12 +175,17 @@
 
 TEST(TaskQueueTest, PostDelayedAfterDestruct) {
   static const char kQueueName[] = "PostDelayedAfterDestruct";
-  Event event(false, false);
+  Event run(false, false);
+  Event deleted(false, false);
   {
     TaskQueue queue(kQueueName);
-    queue.PostDelayedTask(Bind(&CheckCurrent, &event, &queue), 100);
+    queue.PostDelayedTask(
+        rtc::NewClosure([&run] { run.Set(); }, [&deleted] { deleted.Set(); }),
+        100);
   }
-  EXPECT_FALSE(event.Wait(200));  // Task should not run.
+  // Task might outlive the TaskQueue, but still should be deleted.
+  EXPECT_TRUE(deleted.Wait(200));
+  EXPECT_FALSE(run.Wait(0));  // and should not run.
 }
 
 TEST(TaskQueueTest, PostAndReply) {
@@ -371,6 +376,32 @@
   EXPECT_TRUE(event.Wait(1000));
 }
 
+// http://bugs.webrtc.org/9728
+#if defined(WEBRTC_WIN)
+#define MAYBE_DeleteTaskQueueAfterPostAndReply \
+  DISABLED_DeleteTaskQueueAfterPostAndReply
+#else
+#define MAYBE_DeleteTaskQueueAfterPostAndReply DeleteTaskQueueAfterPostAndReply
+#endif
+TEST(TaskQueueTest, MAYBE_DeleteTaskQueueAfterPostAndReply) {
+  Event task_deleted(false, false);
+  Event reply_deleted(false, false);
+  auto* task_queue = new TaskQueue("Queue");
+
+  task_queue->PostTaskAndReply(
+      /*task=*/rtc::NewClosure(
+          /*closure=*/[] {},
+          /*cleanup=*/[&task_deleted] { task_deleted.Set(); }),
+      /*reply=*/rtc::NewClosure(
+          /*closure=*/[] {},
+          /*cleanup=*/[&reply_deleted] { reply_deleted.Set(); }));
+
+  delete task_queue;
+
+  EXPECT_TRUE(task_deleted.Wait(1000));
+  EXPECT_TRUE(reply_deleted.Wait(1000));
+}
+
 void TestPostTaskAndReply(TaskQueue* work_queue, Event* event) {
   ASSERT_FALSE(work_queue->IsCurrent());
   work_queue->PostTaskAndReply(Bind(&CheckCurrent, nullptr, work_queue),
diff --git a/rtc_base/third_party/sigslot/sigslot.h b/rtc_base/third_party/sigslot/sigslot.h
index c77e4e6..8bd1c70 100644
--- a/rtc_base/third_party/sigslot/sigslot.h
+++ b/rtc_base/third_party/sigslot/sigslot.h
@@ -96,7 +96,6 @@
 #ifndef RTC_BASE_THIRD_PARTY_SIGSLOT_SIGSLOT_H_
 #define RTC_BASE_THIRD_PARTY_SIGSLOT_SIGSLOT_H_
 
-#include <stdlib.h>
 #include <cstring>
 #include <list>
 #include <set>
diff --git a/rtc_base/thread.cc b/rtc_base/thread.cc
index 373fa39..911ac16 100644
--- a/rtc_base/thread.cc
+++ b/rtc_base/thread.cc
@@ -24,10 +24,11 @@
 #pragma warning(disable : 4722)
 #endif
 
+#include <utility>  // for move
+
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/nullsocketserver.h"
-#include "rtc_base/platform_thread.h"
 #include "rtc_base/stringutils.h"
 #include "rtc_base/timeutils.h"
 #include "rtc_base/trace_event.h"
@@ -451,6 +452,11 @@
   Send(posted_from, handler);
 }
 
+bool Thread::IsProcessingMessagesForTesting() {
+  return (owned_ || IsCurrent()) &&
+         MessageQueue::IsProcessingMessagesForTesting();
+}
+
 void Thread::Clear(MessageHandler* phandler,
                    uint32_t id,
                    MessageList* removed) {
@@ -477,7 +483,7 @@
     ++iter;
   }
 
-  MessageQueue::Clear(phandler, id, removed);
+  ClearInternal(phandler, id, removed);
 }
 
 #if !defined(WEBRTC_MAC)
diff --git a/rtc_base/thread.h b/rtc_base/thread.h
index 0408a0d..fde5e8b 100644
--- a/rtc_base/thread.h
+++ b/rtc_base/thread.h
@@ -11,12 +11,9 @@
 #ifndef RTC_BASE_THREAD_H_
 #define RTC_BASE_THREAD_H_
 
-#include <algorithm>
 #include <list>
 #include <memory>
 #include <string>
-#include <utility>
-#include <vector>
 
 #if defined(WEBRTC_POSIX)
 #include <pthread.h>
@@ -189,6 +186,7 @@
   }
 
   // From MessageQueue
+  bool IsProcessingMessagesForTesting() override;
   void Clear(MessageHandler* phandler,
              uint32_t id = MQID_ANY,
              MessageList* removed = nullptr) override;
diff --git a/rtc_base/thread_darwin.mm b/rtc_base/thread_darwin.mm
index a404849..e64d6eb 100644
--- a/rtc_base/thread_darwin.mm
+++ b/rtc_base/thread_darwin.mm
@@ -13,6 +13,7 @@
 #import <Foundation/Foundation.h>
 
 #include "rtc_base/platform_thread.h"
+#include "rtc_base/timeutils.h"  // for TimeAfter, TimeUntil
 
 /*
  * This file contains platform-specific implementations for several
diff --git a/rtc_base/timeutils.h b/rtc_base/timeutils.h
index 20b1dac..3a412a4 100644
--- a/rtc_base/timeutils.h
+++ b/rtc_base/timeutils.h
@@ -18,6 +18,7 @@
 #include <string>
 
 #include "rtc_base/checks.h"
+#include "rtc_base/strings/string_builder.h"
 
 namespace rtc {
 
@@ -142,9 +143,9 @@
   int max() const { return max_; }
 
   std::string ToString() const {
-    std::stringstream ss;
+    rtc::StringBuilder ss;
     ss << "[" << min_ << "," << max_ << "]";
-    return ss.str();
+    return ss.Release();
   }
 
   bool operator==(const IntervalRange& o) const {
diff --git a/rtc_base/unittest_main.cc b/rtc_base/unittest_main.cc
index 0b5a39d..12543a3 100644
--- a/rtc_base/unittest_main.cc
+++ b/rtc_base/unittest_main.cc
@@ -19,10 +19,13 @@
 #include "rtc_base/logging.h"
 #include "rtc_base/ssladapter.h"
 #include "rtc_base/sslstreamadapter.h"
-#include "system_wrappers/include/field_trial_default.h"
-#include "system_wrappers/include/metrics_default.h"
+#include "system_wrappers/include/field_trial.h"
+#include "system_wrappers/include/metrics.h"
 #include "test/field_trial.h"
-#include "test/testsupport/fileutils.h"
+
+#if defined(WEBRTC_WIN)
+#include "rtc_base/win32socketinit.h"
+#endif
 
 #if defined(WEBRTC_IOS)
 #include "test/ios/test_support.h"
@@ -77,7 +80,6 @@
     return 0;
   }
 
-  webrtc::test::SetExecutablePath(argv[0]);
   webrtc::test::ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
   // InitFieldTrialsFromString stores the char*, so the char array must outlive
   // the application.
@@ -85,6 +87,8 @@
   webrtc::metrics::Enable();
 
 #if defined(WEBRTC_WIN)
+  rtc::WinsockInitializer winsock_init;
+
   if (!FLAG_default_error_handlers) {
     // Make sure any errors don't throw dialogs hanging the test run.
     _set_invalid_parameter_handler(TestInvalidParameterHandler);
diff --git a/rtc_base/unixfilesystem.cc b/rtc_base/unixfilesystem.cc
index 023c34c..818cb8a 100644
--- a/rtc_base/unixfilesystem.cc
+++ b/rtc_base/unixfilesystem.cc
@@ -47,6 +47,7 @@
 #include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/fileutils.h"
+#include "rtc_base/logging.h"
 #include "rtc_base/pathutils.h"
 #include "rtc_base/stream.h"
 #include "rtc_base/stringutils.h"
diff --git a/rtc_base/win/windows_version.cc b/rtc_base/win/windows_version.cc
index 9ccde4c..f10e42c 100644
--- a/rtc_base/win/windows_version.cc
+++ b/rtc_base/win/windows_version.cc
@@ -261,6 +261,12 @@
       architecture_(OTHER_ARCHITECTURE),
       wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
   OSVERSIONINFOEX version_info = {sizeof version_info};
+  // Applications not manifested for Windows 8.1 or Windows 10 will return the
+  // Windows 8 OS version value (6.2). Once an application is manifested for a
+  // given operating system version, GetVersionEx() will always return the
+  // version that the application is manifested for in future releases.
+  // https://docs.microsoft.com/en-us/windows/desktop/SysInfo/targeting-your-application-at-windows-8-1.
+  // https://www.codeproject.com/Articles/678606/Part-Overcoming-Windows-s-deprecation-of-GetVe.
   ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
   version_number_.major = version_info.dwMajorVersion;
   version_number_.minor = version_info.dwMinorVersion;
diff --git a/rtc_base/win32filesystem.cc b/rtc_base/win32filesystem.cc
index b500e5e..cd43966 100644
--- a/rtc_base/win32filesystem.cc
+++ b/rtc_base/win32filesystem.cc
@@ -20,6 +20,7 @@
 #include "rtc_base/arraysize.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/fileutils.h"
+#include "rtc_base/logging.h"
 #include "rtc_base/pathutils.h"
 #include "rtc_base/stream.h"
 #include "rtc_base/stringutils.h"
diff --git a/rtc_base/win32socketinit.cc b/rtc_base/win32socketinit.cc
deleted file mode 100644
index ea0aae6..0000000
--- a/rtc_base/win32socketinit.cc
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *  Copyright 2009 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 "rtc_base/win32socketinit.h"
-
-#include "rtc_base/win32.h"
-
-namespace rtc {
-
-// Please don't remove this function.
-void EnsureWinsockInit() {
-  // The default implementation uses a global initializer, so WSAStartup
-  // happens at module load time.  Thus we don't need to do anything here.
-  // The hook is provided so that a client that statically links with
-  // libjingle can override it, to provide its own initialization.
-}
-
-#if defined(WEBRTC_WIN)
-class WinsockInitializer {
- public:
-  WinsockInitializer() {
-    WSADATA wsaData;
-    WORD wVersionRequested = MAKEWORD(1, 0);
-    err_ = WSAStartup(wVersionRequested, &wsaData);
-  }
-  ~WinsockInitializer() {
-    if (!err_)
-      WSACleanup();
-  }
-  int error() { return err_; }
-
- private:
-  int err_;
-};
-WinsockInitializer g_winsockinit;
-#endif
-
-}  // namespace rtc
diff --git a/rtc_base/win32socketinit.h b/rtc_base/win32socketinit.h
index ea74809..7f9bdcc 100644
--- a/rtc_base/win32socketinit.h
+++ b/rtc_base/win32socketinit.h
@@ -11,9 +11,30 @@
 #ifndef RTC_BASE_WIN32SOCKETINIT_H_
 #define RTC_BASE_WIN32SOCKETINIT_H_
 
+#ifndef WEBRTC_WIN
+#error "Only #include this header in Windows builds"
+#endif
+
+#include "rtc_base/win32.h"
+
 namespace rtc {
 
-void EnsureWinsockInit();
+class WinsockInitializer {
+ public:
+  WinsockInitializer() {
+    WSADATA wsaData;
+    WORD wVersionRequested = MAKEWORD(1, 0);
+    err_ = WSAStartup(wVersionRequested, &wsaData);
+  }
+  ~WinsockInitializer() {
+    if (!err_)
+      WSACleanup();
+  }
+  int error() { return err_; }
+
+ private:
+  int err_;
+};
 
 }  // namespace rtc
 
diff --git a/rtc_base/win32socketserver.cc b/rtc_base/win32socketserver.cc
index cab751a..230f3ed 100644
--- a/rtc_base/win32socketserver.cc
+++ b/rtc_base/win32socketserver.cc
@@ -16,6 +16,7 @@
 #include "rtc_base/byteorder.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/timeutils.h"  // For Time, TimeSince
 #include "rtc_base/win32window.h"
 
 namespace rtc {
diff --git a/system_wrappers/BUILD.gn b/system_wrappers/BUILD.gn
index 106c63b..eb60052 100644
--- a/system_wrappers/BUILD.gn
+++ b/system_wrappers/BUILD.gn
@@ -25,8 +25,6 @@
     "source/cpu_features.cc",
     "source/cpu_info.cc",
     "source/event.cc",
-    "source/event_timer_win.cc",
-    "source/event_timer_win.h",
     "source/rtp_to_ntp_estimator.cc",
     "source/sleep.cc",
   ]
@@ -35,9 +33,6 @@
   libs = []
   deps = [
     ":cpu_features_api",
-    ":field_trial_api",
-    ":metrics_api",
-    ":runtime_enabled_features_api",
     "..:webrtc_common",
     "../modules:module_api_public",
     "../rtc_base:checks",
@@ -46,13 +41,6 @@
     "//third_party/abseil-cpp/absl/types:optional",
   ]
 
-  if (is_posix || is_fuchsia) {
-    sources += [
-      "source/event_timer_posix.cc",
-      "source/event_timer_posix.h",
-    ]
-  }
-
   if (is_android) {
     defines += [ "WEBRTC_THREAD_RR" ]
 
@@ -106,24 +94,30 @@
   ]
 }
 
-rtc_source_set("field_trial_api") {
-  sources = [
+rtc_source_set("field_trial") {
+  visibility = [ "*" ]
+  public = [
     "include/field_trial.h",
   ]
-}
-
-rtc_source_set("runtime_enabled_features_api") {
-  visibility = [ "*" ]
   sources = [
-    "include/runtime_enabled_features.h",
+    "source/field_trial.cc",
   ]
+  if (rtc_exclude_field_trial_default) {
+    defines = [ "WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT" ]
+  }
 }
 
-rtc_source_set("metrics_api") {
+rtc_source_set("metrics") {
   visibility = [ "*" ]
-  sources = [
+  public = [
     "include/metrics.h",
   ]
+  sources = [
+    "source/metrics.cc",
+  ]
+  if (rtc_exclude_metrics_default) {
+    defines = [ "WEBRTC_EXCLUDE_METRICS_DEFAULT" ]
+  }
   deps = [
     "..:webrtc_common",
     "../rtc_base:checks",
@@ -131,49 +125,6 @@
   ]
 }
 
-rtc_source_set("field_trial_default") {
-  visibility = [ "*" ]
-  sources = [
-    "include/field_trial_default.h",
-    "source/field_trial_default.cc",
-  ]
-  deps = [
-    ":field_trial_api",
-  ]
-}
-
-rtc_source_set("runtime_enabled_features_default") {
-  visibility = [ "*" ]
-  sources = [
-    "source/runtime_enabled_features_default.cc",
-  ]
-  deps = [
-    ":runtime_enabled_features_api",
-    "../rtc_base:rtc_base_approved",
-  ]
-}
-
-rtc_source_set("metrics_default") {
-  visibility = [ "*" ]
-  sources = [
-    "include/metrics_default.h",
-    "source/metrics_default.cc",
-  ]
-  deps = [
-    ":metrics_api",
-    "../rtc_base:rtc_base_approved",
-  ]
-}
-
-group("system_wrappers_default") {
-  deps = [
-    ":field_trial_default",
-    ":metrics_default",
-    ":runtime_enabled_features_default",
-    ":system_wrappers",
-  ]
-}
-
 if (is_android && !build_with_mozilla) {
   rtc_static_library("cpu_features_android") {
     sources = [
@@ -209,13 +160,8 @@
       "source/rtp_to_ntp_estimator_unittest.cc",
     ]
 
-    if (is_posix || is_fuchsia) {
-      sources += [ "source/event_timer_posix_unittest.cc" ]
-    }
-
     deps = [
-      ":metrics_api",
-      ":metrics_default",
+      ":metrics",
       ":system_wrappers",
       "..:webrtc_common",
       "../rtc_base:rtc_base_approved",
diff --git a/system_wrappers/include/event_wrapper.h b/system_wrappers/include/event_wrapper.h
index 0c29138..0531ddb 100644
--- a/system_wrappers/include/event_wrapper.h
+++ b/system_wrappers/include/event_wrapper.h
@@ -20,8 +20,6 @@
 
 #define WEBRTC_EVENT_INFINITE 0xffffffff
 
-class EventTimerWrapper;
-
 class EventWrapper {
  public:
   // Factory method. Constructor disabled.
@@ -50,20 +48,6 @@
   virtual EventTypeWrapper Wait(unsigned long max_time) = 0;
 };
 
-class EventTimerWrapper : public EventWrapper {
- public:
-  static EventTimerWrapper* Create();
-
-  // Starts a timer that will call a non-sticky version of Set() either once
-  // or periodically. If the timer is periodic it ensures that there is no
-  // drift over time relative to the system clock.
-  //
-  // |time| is in milliseconds.
-  virtual bool StartTimer(bool periodic, unsigned long time) = 0;
-
-  virtual bool StopTimer() = 0;
-};
-
 }  // namespace webrtc
 
 #endif  // SYSTEM_WRAPPERS_INCLUDE_EVENT_WRAPPER_H_
diff --git a/system_wrappers/include/field_trial.h b/system_wrappers/include/field_trial.h
index c6a6223..41bea9b 100644
--- a/system_wrappers/include/field_trial.h
+++ b/system_wrappers/include/field_trial.h
@@ -16,14 +16,15 @@
 // Field trials allow webrtc clients (such as Chrome) to turn on feature code
 // in binaries out in the field and gather information with that.
 //
-// WebRTC clients MUST provide an implementation of:
+// By default WebRTC provides an implementaion of field trials that can be
+// found in system_wrappers/source/field_trial.cc. If clients want to provide
+// a custom version, they will have to:
 //
-//   std::string webrtc::field_trial::FindFullName(const std::string& trial).
-//
-// Or link with a default one provided in:
-//
-//   system_wrappers/system_wrappers.gyp:field_trial_default
-//
+// 1. Compile WebRTC defining the preprocessor macro
+//    WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT (if GN is used this can be achieved
+//    by setting the GN arg rtc_exclude_field_trial_default to true).
+// 2. Provide an implementation of:
+//    std::string webrtc::field_trial::FindFullName(const std::string& trial).
 //
 // They are designed to wire up directly to chrome field trials and to speed up
 // developers by reducing the need to wire APIs to control whether a feature is
@@ -75,6 +76,14 @@
   return FindFullName(name).find("Disabled") == 0;
 }
 
+// Optionally initialize field trial from a string.
+// This method can be called at most once before any other call into webrtc.
+// E.g. before the peer connection factory is constructed.
+// Note: trials_string must never be destroyed.
+void InitFieldTrialsFromString(const char* trials_string);
+
+const char* GetFieldTrialString();
+
 }  // namespace field_trial
 }  // namespace webrtc
 
diff --git a/system_wrappers/include/field_trial_default.h b/system_wrappers/include/field_trial_default.h
deleted file mode 100644
index 1774c2d..0000000
--- a/system_wrappers/include/field_trial_default.h
+++ /dev/null
@@ -1,28 +0,0 @@
-//
-// Copyright (c) 2015 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 SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_DEFAULT_H_
-#define SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_DEFAULT_H_
-
-namespace webrtc {
-namespace field_trial {
-
-// Optionally initialize field trial from a string.
-// This method can be called at most once before any other call into webrtc.
-// E.g. before the peer connection factory is constructed.
-// Note: trials_string must never be destroyed.
-void InitFieldTrialsFromString(const char* trials_string);
-
-const char* GetFieldTrialString();
-
-}  // namespace field_trial
-}  // namespace webrtc
-
-#endif  // SYSTEM_WRAPPERS_INCLUDE_FIELD_TRIAL_DEFAULT_H_
diff --git a/system_wrappers/include/metrics.h b/system_wrappers/include/metrics.h
index 99b8194..2a2cda0 100644
--- a/system_wrappers/include/metrics.h
+++ b/system_wrappers/include/metrics.h
@@ -11,6 +11,8 @@
 #ifndef SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
 #define SYSTEM_WRAPPERS_INCLUDE_METRICS_H_
 
+#include <map>
+#include <memory>
 #include <string>
 
 #include "common_types.h"  // NOLINT(build/include)
@@ -31,20 +33,21 @@
 // The macros use the methods HistogramFactoryGetCounts,
 // HistogramFactoryGetEnumeration and HistogramAdd.
 //
-// Therefore, WebRTC clients must either:
+// By default WebRTC provides implementations of the aforementioned methods
+// that can be found in system_wrappers/source/metrics.cc. If clients want to
+// provide a custom version, they will have to:
 //
-// - provide implementations of
-//   Histogram* webrtc::metrics::HistogramFactoryGetCounts(
-//       const std::string& name, int sample, int min, int max,
-//       int bucket_count);
-//   Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
-//       const std::string& name, int sample, int boundary);
-//   void webrtc::metrics::HistogramAdd(
-//       Histogram* histogram_pointer, const std::string& name, int sample);
-//
-// - or link with the default implementations (i.e.
-//   system_wrappers:metrics_default).
-//
+// 1. Compile WebRTC defining the preprocessor macro
+//    WEBRTC_EXCLUDE_METRICS_DEFAULT (if GN is used this can be achieved
+//    by setting the GN arg rtc_exclude_metrics_default to true).
+// 2. Provide implementations of:
+//    Histogram* webrtc::metrics::HistogramFactoryGetCounts(
+//        const std::string& name, int sample, int min, int max,
+//        int bucket_count);
+//    Histogram* webrtc::metrics::HistogramFactoryGetEnumeration(
+//        const std::string& name, int sample, int boundary);
+//    void webrtc::metrics::HistogramAdd(
+//        Histogram* histogram_pointer, const std::string& name, int sample);
 //
 // Example usage:
 //
@@ -274,6 +277,39 @@
 // Function for adding a |sample| to a histogram.
 void HistogramAdd(Histogram* histogram_pointer, int sample);
 
+struct SampleInfo {
+  SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
+  ~SampleInfo();
+
+  const std::string name;
+  const int min;
+  const int max;
+  const size_t bucket_count;
+  std::map<int, int> samples;  // <value, # of events>
+};
+
+// Enables collection of samples.
+// This method should be called before any other call into webrtc.
+void Enable();
+
+// Gets histograms and clears all samples.
+void GetAndReset(
+    std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
+
+// Functions below are mainly for testing.
+
+// Clears all samples.
+void Reset();
+
+// Returns the number of times the |sample| has been added to the histogram.
+int NumEvents(const std::string& name, int sample);
+
+// Returns the total number of added samples to the histogram.
+int NumSamples(const std::string& name);
+
+// Returns the minimum sample value (or -1 if the histogram has no samples).
+int MinSample(const std::string& name);
+
 }  // namespace metrics
 }  // namespace webrtc
 
diff --git a/system_wrappers/include/metrics_default.h b/system_wrappers/include/metrics_default.h
deleted file mode 100644
index 5ce3582..0000000
--- a/system_wrappers/include/metrics_default.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- *  Copyright (c) 2016 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 SYSTEM_WRAPPERS_INCLUDE_METRICS_DEFAULT_H_
-#define SYSTEM_WRAPPERS_INCLUDE_METRICS_DEFAULT_H_
-
-#include <map>
-#include <memory>
-#include <string>
-
-namespace webrtc {
-namespace metrics {
-
-// This class does not actually exist. It is casted to an implementation defined
-// pointer inside the functions.
-class Histogram;
-
-struct SampleInfo {
-  SampleInfo(const std::string& name, int min, int max, size_t bucket_count);
-  ~SampleInfo();
-
-  const std::string name;
-  const int min;
-  const int max;
-  const size_t bucket_count;
-  std::map<int, int> samples;  // <value, # of events>
-};
-
-// Enables collection of samples.
-// This method should be called before any other call into webrtc.
-void Enable();
-
-// Gets histograms and clears all samples.
-void GetAndReset(
-    std::map<std::string, std::unique_ptr<SampleInfo>>* histograms);
-
-// Functions below are mainly for testing.
-
-// Clears all samples.
-void Reset();
-
-// Returns the number of times the |sample| has been added to the histogram.
-int NumEvents(const std::string& name, int sample);
-
-// Returns the total number of added samples to the histogram.
-int NumSamples(const std::string& name);
-
-// Returns the minimum sample value (or -1 if the histogram has no samples).
-int MinSample(const std::string& name);
-
-}  // namespace metrics
-}  // namespace webrtc
-
-#endif  // SYSTEM_WRAPPERS_INCLUDE_METRICS_DEFAULT_H_
diff --git a/system_wrappers/include/runtime_enabled_features.h b/system_wrappers/include/runtime_enabled_features.h
deleted file mode 100644
index 9ccbedc..0000000
--- a/system_wrappers/include/runtime_enabled_features.h
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- *  Copyright (c) 2018 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 SYSTEM_WRAPPERS_INCLUDE_RUNTIME_ENABLED_FEATURES_H_
-#define SYSTEM_WRAPPERS_INCLUDE_RUNTIME_ENABLED_FEATURES_H_
-
-#include <string>
-
-// These functions for querying enabled runtime features must be implemented
-// by all webrtc clients (such as Chrome).
-// Default implementation is provided in:
-//
-//    system_wrappers/system_wrappers:runtime_enabled_features_default
-
-// TODO(ilnik): Find a more flexible way to use Chrome features.
-// This interface requires manual translation from feature name to
-// Chrome feature class in third_party/webrtc_overrides.
-
-namespace webrtc {
-namespace runtime_enabled_features {
-
-const char kDualStreamModeFeatureName[] = "WebRtcDualStreamMode";
-
-bool IsFeatureEnabled(std::string feature_name);
-
-}  // namespace runtime_enabled_features
-}  // namespace webrtc
-
-#endif  // SYSTEM_WRAPPERS_INCLUDE_RUNTIME_ENABLED_FEATURES_H_
diff --git a/system_wrappers/source/clock.cc b/system_wrappers/source/clock.cc
index c9940fb..35ab5f0 100644
--- a/system_wrappers/source/clock.cc
+++ b/system_wrappers/source/clock.cc
@@ -200,32 +200,15 @@
 };
 #endif  // defined(WEBRTC_POSIX)
 
-#if defined(WEBRTC_WIN)
-static WindowsRealTimeClock* volatile g_shared_clock = nullptr;
-#endif  // defined(WEBRTC_WIN)
-
 Clock* Clock::GetRealTimeClock() {
 #if defined(WEBRTC_WIN)
-  // This read relies on volatile read being atomic-load-acquire. This is
-  // true in MSVC since at least 2005:
-  // "A read of a volatile object (volatile read) has Acquire semantics"
-  if (g_shared_clock != nullptr)
-    return g_shared_clock;
-  WindowsRealTimeClock* clock = new WindowsRealTimeClock;
-  if (InterlockedCompareExchangePointer(
-          reinterpret_cast<void* volatile*>(&g_shared_clock), clock, nullptr) !=
-      nullptr) {
-    // g_shared_clock was assigned while we constructed/tried to assign our
-    // instance, delete our instance and use the existing one.
-    delete clock;
-  }
-  return g_shared_clock;
+  static Clock* const clock = new WindowsRealTimeClock();
 #elif defined(WEBRTC_POSIX)
-  static UnixRealTimeClock clock;
-  return &clock;
-#else   // defined(WEBRTC_POSIX)
-  return nullptr;
-#endif  // !defined(WEBRTC_WIN) || defined(WEBRTC_POSIX)
+  static Clock* const clock = new UnixRealTimeClock();
+#else
+  static Clock* const clock = nullptr;
+#endif
+  return clock;
 }
 
 SimulatedClock::SimulatedClock(int64_t initial_time_us)
diff --git a/system_wrappers/source/event.cc b/system_wrappers/source/event.cc
index aac69f6..d1d2cda 100644
--- a/system_wrappers/source/event.cc
+++ b/system_wrappers/source/event.cc
@@ -12,14 +12,11 @@
 
 #if defined(_WIN32)
 #include <windows.h>
-#include "system_wrappers/source/event_timer_win.h"
 #elif defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
 #include <ApplicationServices/ApplicationServices.h>
 #include <pthread.h>
-#include "system_wrappers/source/event_timer_posix.h"
 #else
 #include <pthread.h>
-#include "system_wrappers/source/event_timer_posix.h"
 #endif
 
 #include "rtc_base/event.h"
diff --git a/system_wrappers/source/event_timer_posix.cc b/system_wrappers/source/event_timer_posix.cc
deleted file mode 100644
index e79aa99..0000000
--- a/system_wrappers/source/event_timer_posix.cc
+++ /dev/null
@@ -1,275 +0,0 @@
-/*
- *  Copyright (c) 2011 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 "system_wrappers/source/event_timer_posix.h"
-
-#if defined(WEBRTC_ANDROID)
-#include <android/api-level.h>
-#endif
-
-#include <errno.h>
-#include <pthread.h>
-#include <signal.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/time.h>
-#include <unistd.h>
-
-#include "rtc_base/checks.h"
-
-#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
-// Chromium build is always defining this macro if __ANDROID_API__ < 20.
-#undef HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
-#endif
-
-#if defined(WEBRTC_ANDROID) && defined(__ANDROID_API__)
-#define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC (__ANDROID_API__ < 21)
-#else
-#define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC 0
-#endif
-
-namespace webrtc {
-
-// static
-EventTimerWrapper* EventTimerWrapper::Create() {
-  return new EventTimerPosix();
-}
-
-const int64_t kNanosecondsPerMillisecond = 1000000;
-const int64_t kNanosecondsPerSecond = 1000000000;
-
-EventTimerPosix::EventTimerPosix()
-    : event_set_(false),
-      timer_thread_(nullptr),
-      created_at_(),
-      periodic_(false),
-      time_ms_(0),
-      count_(0),
-      is_stopping_(false) {
-  pthread_mutexattr_t attr;
-  pthread_mutexattr_init(&attr);
-  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-  pthread_mutex_init(&mutex_, &attr);
-  pthread_condattr_t cond_attr;
-  pthread_condattr_init(&cond_attr);
-// TODO(sprang): Remove HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC special case once
-// all supported Android platforms support pthread_condattr_setclock.
-// TODO(sprang): Add support for monotonic clock on Apple platforms.
-#if !(defined(WEBRTC_MAC) || defined(WEBRTC_IOS)) && \
-    !(defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
-  pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
-#endif
-  pthread_cond_init(&cond_, &cond_attr);
-  pthread_condattr_destroy(&cond_attr);
-}
-
-EventTimerPosix::~EventTimerPosix() {
-  StopTimer();
-  pthread_cond_destroy(&cond_);
-  pthread_mutex_destroy(&mutex_);
-}
-
-// TODO(pbos): Make this void.
-bool EventTimerPosix::Set() {
-  RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_));
-  event_set_ = true;
-  pthread_cond_signal(&cond_);
-  pthread_mutex_unlock(&mutex_);
-  return true;
-}
-
-EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout_ms) {
-  int ret_val = 0;
-  RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_));
-
-  if (!event_set_) {
-    if (WEBRTC_EVENT_INFINITE != timeout_ms) {
-      timespec end_at;
-#ifndef WEBRTC_MAC
-      clock_gettime(CLOCK_MONOTONIC, &end_at);
-#else
-      timeval value;
-      struct timezone time_zone;
-      time_zone.tz_minuteswest = 0;
-      time_zone.tz_dsttime = 0;
-      gettimeofday(&value, &time_zone);
-      TIMEVAL_TO_TIMESPEC(&value, &end_at);
-#endif
-      end_at.tv_sec += timeout_ms / 1000;
-      end_at.tv_nsec += (timeout_ms % 1000) * kNanosecondsPerMillisecond;
-
-      if (end_at.tv_nsec >= kNanosecondsPerSecond) {
-        end_at.tv_sec++;
-        end_at.tv_nsec -= kNanosecondsPerSecond;
-      }
-      while (ret_val == 0 && !event_set_) {
-#if defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
-        ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, &end_at);
-#else
-        ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at);
-#endif  // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
-      }
-    } else {
-      while (ret_val == 0 && !event_set_)
-        ret_val = pthread_cond_wait(&cond_, &mutex_);
-    }
-  }
-
-  RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT);
-
-  // Reset and signal if set, regardless of why the thread woke up.
-  if (event_set_) {
-    ret_val = 0;
-    event_set_ = false;
-  }
-  pthread_mutex_unlock(&mutex_);
-
-  return ret_val == 0 ? kEventSignaled : kEventTimeout;
-}
-
-EventTypeWrapper EventTimerPosix::Wait(timespec* end_at, bool reset_event) {
-  int ret_val = 0;
-  RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_));
-  if (reset_event) {
-    // Only wake for new events or timeouts.
-    event_set_ = false;
-  }
-
-  while (ret_val == 0 && !event_set_) {
-#if defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
-    ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, end_at);
-#else
-    ret_val = pthread_cond_timedwait(&cond_, &mutex_, end_at);
-#endif  // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC
-  }
-
-  RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT);
-
-  // Reset and signal if set, regardless of why the thread woke up.
-  if (event_set_) {
-    ret_val = 0;
-    event_set_ = false;
-  }
-  pthread_mutex_unlock(&mutex_);
-
-  return ret_val == 0 ? kEventSignaled : kEventTimeout;
-}
-
-rtc::PlatformThread* EventTimerPosix::CreateThread() {
-  const char* kThreadName = "WebRtc_event_timer_thread";
-  return new rtc::PlatformThread(Run, this, kThreadName);
-}
-
-bool EventTimerPosix::StartTimer(bool periodic, unsigned long time_ms) {
-  pthread_mutex_lock(&mutex_);
-  if (timer_thread_) {
-    if (periodic_) {
-      // Timer already started.
-      pthread_mutex_unlock(&mutex_);
-      return false;
-    } else {
-      // New one shot timer.
-      time_ms_ = time_ms;
-      created_at_.tv_sec = 0;
-      timer_event_->Set();
-      pthread_mutex_unlock(&mutex_);
-      return true;
-    }
-  }
-
-  // Start the timer thread.
-  timer_event_.reset(new EventTimerPosix());
-  timer_thread_.reset(CreateThread());
-  periodic_ = periodic;
-  time_ms_ = time_ms;
-  timer_thread_->Start();
-  timer_thread_->SetPriority(rtc::kRealtimePriority);
-  pthread_mutex_unlock(&mutex_);
-
-  return true;
-}
-
-bool EventTimerPosix::Run(void* obj) {
-  return static_cast<EventTimerPosix*>(obj)->Process();
-}
-
-bool EventTimerPosix::Process() {
-  pthread_mutex_lock(&mutex_);
-  if (is_stopping_) {
-    pthread_mutex_unlock(&mutex_);
-    return false;
-  }
-  if (created_at_.tv_sec == 0) {
-#ifndef WEBRTC_MAC
-    RTC_CHECK_EQ(0, clock_gettime(CLOCK_MONOTONIC, &created_at_));
-#else
-    timeval value;
-    struct timezone time_zone;
-    time_zone.tz_minuteswest = 0;
-    time_zone.tz_dsttime = 0;
-    gettimeofday(&value, &time_zone);
-    TIMEVAL_TO_TIMESPEC(&value, &created_at_);
-#endif
-    count_ = 0;
-  }
-
-  timespec end_at;
-  unsigned long long total_delta_ms = time_ms_ * ++count_;
-  if (!periodic_ && count_ >= 1) {
-    // No need to wake up often if we're not going to signal waiting threads.
-    total_delta_ms =
-        std::min<uint64_t>(total_delta_ms, 60 * kNanosecondsPerSecond);
-  }
-
-  end_at.tv_sec = created_at_.tv_sec + total_delta_ms / 1000;
-  end_at.tv_nsec = created_at_.tv_nsec +
-                   (total_delta_ms % 1000) * kNanosecondsPerMillisecond;
-
-  if (end_at.tv_nsec >= kNanosecondsPerSecond) {
-    end_at.tv_sec++;
-    end_at.tv_nsec -= kNanosecondsPerSecond;
-  }
-
-  pthread_mutex_unlock(&mutex_);
-  // Reset event on first call so that we don't immediately return here if this
-  // thread was not blocked on timer_event_->Wait when the StartTimer() call
-  // was made.
-  if (timer_event_->Wait(&end_at, count_ == 1) == kEventSignaled)
-    return true;
-
-  pthread_mutex_lock(&mutex_);
-  if (periodic_ || count_ == 1)
-    Set();
-  pthread_mutex_unlock(&mutex_);
-
-  return true;
-}
-
-bool EventTimerPosix::StopTimer() {
-  pthread_mutex_lock(&mutex_);
-  is_stopping_ = true;
-  pthread_mutex_unlock(&mutex_);
-
-  if (timer_event_)
-    timer_event_->Set();
-
-  if (timer_thread_) {
-    timer_thread_->Stop();
-    timer_thread_.reset();
-  }
-  timer_event_.reset();
-
-  // Set time to zero to force new reference time for the timer.
-  memset(&created_at_, 0, sizeof(created_at_));
-  count_ = 0;
-  return true;
-}
-
-}  // namespace webrtc
diff --git a/system_wrappers/source/event_timer_posix.h b/system_wrappers/source/event_timer_posix.h
deleted file mode 100644
index 72d6753..0000000
--- a/system_wrappers/source/event_timer_posix.h
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- *  Copyright (c) 2011 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 SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
-#define SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
-
-#include "system_wrappers/include/event_wrapper.h"
-
-#include <memory>
-
-#include <pthread.h>
-#include <time.h>
-
-#include "rtc_base/platform_thread.h"
-
-namespace webrtc {
-
-enum State { kUp = 1, kDown = 2 };
-
-class EventTimerPosix : public EventTimerWrapper {
- public:
-  EventTimerPosix();
-  ~EventTimerPosix() override;
-
-  EventTypeWrapper Wait(unsigned long max_time) override;
-  bool Set() override;
-
-  bool StartTimer(bool periodic, unsigned long time) override;
-  bool StopTimer() override;
-
- private:
-  friend class EventTimerPosixTest;
-
-  static bool Run(void* obj);
-  bool Process();
-  EventTypeWrapper Wait(timespec* end_at, bool reset_state);
-
-  virtual rtc::PlatformThread* CreateThread();
-
-  pthread_cond_t cond_;
-  pthread_mutex_t mutex_;
-  bool event_set_;
-
-  // TODO(pbos): Remove unique_ptr and use PlatformThread directly.
-  std::unique_ptr<rtc::PlatformThread> timer_thread_;
-  std::unique_ptr<EventTimerPosix> timer_event_;
-  timespec created_at_;
-
-  bool periodic_;
-  unsigned long time_ms_;
-  unsigned long count_;
-  bool is_stopping_;
-};
-
-}  // namespace webrtc
-
-#endif  // SYSTEM_WRAPPERS_SOURCE_EVENT_POSIX_H_
diff --git a/system_wrappers/source/event_timer_posix_unittest.cc b/system_wrappers/source/event_timer_posix_unittest.cc
deleted file mode 100644
index 88fd90a..0000000
--- a/system_wrappers/source/event_timer_posix_unittest.cc
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- *  Copyright (c) 2016 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 "system_wrappers/source/event_timer_posix.h"
-
-#include "rtc_base/criticalsection.h"
-#include "rtc_base/event.h"
-#include "test/gtest.h"
-
-namespace webrtc {
-
-enum class ThreadState {
-  kNotStarted,
-  kWaiting,
-  kRequestProcessCall,
-  kCallingProcess,
-  kProcessDone,
-  kContinue,
-  kExiting,
-  kDead
-};
-
-class EventTimerPosixTest : public testing::Test, public EventTimerPosix {
- public:
-  EventTimerPosixTest()
-      : thread_state_(ThreadState::kNotStarted),
-        process_event_(false, true),
-        main_event_(false, true),
-        process_thread_id_(0),
-        process_thread_(nullptr) {}
-  ~EventTimerPosixTest() override {}
-
-  rtc::PlatformThread* CreateThread() override {
-    EXPECT_TRUE(process_thread_ == nullptr);
-    process_thread_ =
-        new rtc::PlatformThread(Run, this, "EventTimerPosixTestThread");
-    return process_thread_;
-  }
-
-  static bool Run(void* obj) {
-    return static_cast<EventTimerPosixTest*>(obj)->Process();
-  }
-
-  bool Process() {
-    bool res = ProcessInternal();
-    if (!res) {
-      rtc::CritScope cs(&lock_);
-      thread_state_ = ThreadState::kDead;
-      main_event_.Set();
-    }
-    return res;
-  }
-
-  bool ProcessInternal() {
-    {
-      rtc::CritScope cs(&lock_);
-      if (thread_state_ == ThreadState::kNotStarted) {
-        if (!ChangeThreadState(ThreadState::kNotStarted,
-                               ThreadState::kContinue)) {
-          ADD_FAILURE() << "Unable to start process thread";
-          return false;
-        }
-        process_thread_id_ = rtc::CurrentThreadId();
-      }
-    }
-
-    if (!ChangeThreadState(ThreadState::kContinue, ThreadState::kWaiting))
-      return false;
-
-    if (!AwaitThreadState(ThreadState::kRequestProcessCall,
-                          rtc::Event::kForever))
-      return false;
-
-    if (!ChangeThreadState(ThreadState::kRequestProcessCall,
-                           ThreadState::kCallingProcess))
-      return false;
-
-    EventTimerPosix::Process();
-
-    if (!ChangeThreadState(ThreadState::kCallingProcess,
-                           ThreadState::kProcessDone))
-      return false;
-
-    if (!AwaitThreadState(ThreadState::kContinue, rtc::Event::kForever))
-      return false;
-
-    return true;
-  }
-
-  bool IsProcessThread() {
-    rtc::CritScope cs(&lock_);
-    return process_thread_id_ == rtc::CurrentThreadId();
-  }
-
-  bool ChangeThreadState(ThreadState prev_state, ThreadState new_state) {
-    rtc::CritScope cs(&lock_);
-    if (thread_state_ != prev_state)
-      return false;
-    thread_state_ = new_state;
-    if (IsProcessThread()) {
-      main_event_.Set();
-    } else {
-      process_event_.Set();
-    }
-    return true;
-  }
-
-  bool AwaitThreadState(ThreadState state, int timeout) {
-    rtc::Event* event = IsProcessThread() ? &process_event_ : &main_event_;
-    do {
-      rtc::CritScope cs(&lock_);
-      if (state != ThreadState::kDead && thread_state_ == ThreadState::kExiting)
-        return false;
-      if (thread_state_ == state)
-        return true;
-    } while (event->Wait(timeout));
-    return false;
-  }
-
-  bool CallProcess(int timeout_ms) {
-    return AwaitThreadState(ThreadState::kWaiting, timeout_ms) &&
-           ChangeThreadState(ThreadState::kWaiting,
-                             ThreadState::kRequestProcessCall);
-  }
-
-  bool AwaitProcessDone(int timeout_ms) {
-    return AwaitThreadState(ThreadState::kProcessDone, timeout_ms) &&
-           ChangeThreadState(ThreadState::kProcessDone, ThreadState::kContinue);
-  }
-
-  void TearDown() override {
-    if (process_thread_) {
-      {
-        rtc::CritScope cs(&lock_);
-        if (thread_state_ != ThreadState::kDead) {
-          thread_state_ = ThreadState::kExiting;
-          process_event_.Set();
-        }
-      }
-      ASSERT_TRUE(AwaitThreadState(ThreadState::kDead, 5000));
-    }
-  }
-
-  ThreadState thread_state_;
-  rtc::CriticalSection lock_;
-  rtc::Event process_event_;
-  rtc::Event main_event_;
-  rtc::PlatformThreadId process_thread_id_;
-  rtc::PlatformThread* process_thread_;
-};
-
-TEST_F(EventTimerPosixTest, WaiterBlocksUntilTimeout) {
-  const int kTimerIntervalMs = 100;
-  const int kTimeoutMs = 5000;
-  ASSERT_TRUE(StartTimer(false, kTimerIntervalMs));
-  ASSERT_TRUE(CallProcess(kTimeoutMs));
-  EventTypeWrapper res = Wait(kTimeoutMs);
-  EXPECT_EQ(kEventSignaled, res);
-  ASSERT_TRUE(AwaitProcessDone(kTimeoutMs));
-}
-
-TEST_F(EventTimerPosixTest, WaiterWakesImmediatelyAfterTimeout) {
-  const int kTimerIntervalMs = 100;
-  const int kTimeoutMs = 5000;
-  ASSERT_TRUE(StartTimer(false, kTimerIntervalMs));
-  ASSERT_TRUE(CallProcess(kTimeoutMs));
-  ASSERT_TRUE(AwaitProcessDone(kTimeoutMs));
-  EventTypeWrapper res = Wait(0);
-  EXPECT_EQ(kEventSignaled, res);
-}
-
-TEST_F(EventTimerPosixTest, WaiterBlocksUntilTimeoutProcessInactiveOnStart) {
-  const int kTimerIntervalMs = 100;
-  const int kTimeoutMs = 5000;
-  // First call to StartTimer initializes thread.
-  ASSERT_TRUE(StartTimer(false, kTimerIntervalMs));
-
-  // Process thread currently _not_ blocking on Process() call.
-  ASSERT_TRUE(AwaitThreadState(ThreadState::kWaiting, kTimeoutMs));
-
-  // Start new one-off timer, then call Process().
-  ASSERT_TRUE(StartTimer(false, kTimerIntervalMs));
-  ASSERT_TRUE(CallProcess(kTimeoutMs));
-
-  EventTypeWrapper res = Wait(kTimeoutMs);
-  EXPECT_EQ(kEventSignaled, res);
-
-  ASSERT_TRUE(AwaitProcessDone(kTimeoutMs));
-}
-
-}  // namespace webrtc
diff --git a/system_wrappers/source/event_timer_win.cc b/system_wrappers/source/event_timer_win.cc
deleted file mode 100644
index c2ad9f3..0000000
--- a/system_wrappers/source/event_timer_win.cc
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- *  Copyright (c) 2012 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 "system_wrappers/source/event_timer_win.h"
-
-#include "mmsystem.h"
-
-namespace webrtc {
-
-// static
-EventTimerWrapper* EventTimerWrapper::Create() {
-  return new EventTimerWin();
-}
-
-EventTimerWin::EventTimerWin()
-    : event_(::CreateEvent(NULL,    // security attributes
-                           FALSE,   // manual reset
-                           FALSE,   // initial state
-                           NULL)),  // name of event
-      timerID_(NULL) {}
-
-EventTimerWin::~EventTimerWin() {
-  StopTimer();
-  CloseHandle(event_);
-}
-
-bool EventTimerWin::Set() {
-  // Note: setting an event that is already set has no effect.
-  return SetEvent(event_) == 1;
-}
-
-EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
-  unsigned long res = WaitForSingleObject(event_, max_time);
-  switch (res) {
-    case WAIT_OBJECT_0:
-      return kEventSignaled;
-    case WAIT_TIMEOUT:
-      return kEventTimeout;
-    default:
-      return kEventError;
-  }
-}
-
-bool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
-  if (timerID_ != NULL) {
-    timeKillEvent(timerID_);
-    timerID_ = NULL;
-  }
-
-  if (periodic) {
-    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
-                            TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
-  } else {
-    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
-                            TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
-  }
-
-  return timerID_ != NULL;
-}
-
-bool EventTimerWin::StopTimer() {
-  if (timerID_ != NULL) {
-    timeKillEvent(timerID_);
-    timerID_ = NULL;
-  }
-
-  return true;
-}
-
-}  // namespace webrtc
diff --git a/system_wrappers/source/event_timer_win.h b/system_wrappers/source/event_timer_win.h
deleted file mode 100644
index c99f8c6..0000000
--- a/system_wrappers/source/event_timer_win.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- *  Copyright (c) 2011 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 SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
-#define SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
-
-#include <stdint.h>
-
-#include <windows.h>
-
-#include "system_wrappers/include/event_wrapper.h"
-
-namespace webrtc {
-
-class EventTimerWin : public EventTimerWrapper {
- public:
-  EventTimerWin();
-  ~EventTimerWin() override;
-
-  EventTypeWrapper Wait(unsigned long max_time) override;
-  bool Set() override;
-
-  bool StartTimer(bool periodic, unsigned long time) override;
-  bool StopTimer() override;
-
- private:
-  HANDLE event_;
-  uint32_t timerID_;
-};
-
-}  // namespace webrtc
-
-#endif  // SYSTEM_WRAPPERS_SOURCE_EVENT_WIN_H_
diff --git a/system_wrappers/source/field_trial_default.cc b/system_wrappers/source/field_trial.cc
similarity index 95%
rename from system_wrappers/source/field_trial_default.cc
rename to system_wrappers/source/field_trial.cc
index e8d8917..60158f4 100644
--- a/system_wrappers/source/field_trial_default.cc
+++ b/system_wrappers/source/field_trial.cc
@@ -7,7 +7,6 @@
 // be found in the AUTHORS file in the root of the source tree.
 //
 
-#include "system_wrappers/include/field_trial_default.h"
 #include "system_wrappers/include/field_trial.h"
 
 #include <string>
@@ -19,6 +18,7 @@
 
 static const char* trials_init_string = NULL;
 
+#ifndef WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
 std::string FindFullName(const std::string& name) {
   if (trials_init_string == NULL)
     return std::string();
@@ -51,6 +51,7 @@
   }
   return std::string();
 }
+#endif  // WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT
 
 // Optionally initialize field trial from a string.
 void InitFieldTrialsFromString(const char* trials_string) {
diff --git a/system_wrappers/source/metrics_default.cc b/system_wrappers/source/metrics.cc
similarity index 98%
rename from system_wrappers/source/metrics_default.cc
rename to system_wrappers/source/metrics.cc
index 7b62c81..4aa8770 100644
--- a/system_wrappers/source/metrics_default.cc
+++ b/system_wrappers/source/metrics.cc
@@ -7,13 +7,12 @@
 // be found in the AUTHORS file in the root of the source tree.
 //
 
-#include "system_wrappers/include/metrics_default.h"
+#include "system_wrappers/include/metrics.h"
 
 #include <algorithm>
 
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/thread_annotations.h"
-#include "system_wrappers/include/metrics.h"
 
 // Default implementation of histogram methods for WebRTC clients that do not
 // want to provide their own implementation.
@@ -203,6 +202,7 @@
 }
 }  // namespace
 
+#ifndef WEBRTC_EXCLUDE_METRICS_DEFAULT
 // Implementation of histogram methods in
 // webrtc/system_wrappers/interface/metrics.h.
 
@@ -259,6 +259,8 @@
   ptr->Add(sample);
 }
 
+#endif  // WEBRTC_EXCLUDE_METRICS_DEFAULT
+
 SampleInfo::SampleInfo(const std::string& name,
                        int min,
                        int max,
@@ -267,7 +269,7 @@
 
 SampleInfo::~SampleInfo() {}
 
-// Implementation of global functions in metrics_default.h.
+// Implementation of global functions in metrics.h.
 void Enable() {
   RTC_DCHECK(g_rtc_histogram_map == nullptr);
 #if RTC_DCHECK_IS_ON
diff --git a/system_wrappers/source/metrics_default_unittest.cc b/system_wrappers/source/metrics_default_unittest.cc
index 7491a2f..b2d2023 100644
--- a/system_wrappers/source/metrics_default_unittest.cc
+++ b/system_wrappers/source/metrics_default_unittest.cc
@@ -8,7 +8,6 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "system_wrappers/include/metrics_default.h"
 #include "system_wrappers/include/metrics.h"
 #include "test/gtest.h"
 
diff --git a/system_wrappers/source/metrics_unittest.cc b/system_wrappers/source/metrics_unittest.cc
index 4218ad7..dac8177 100644
--- a/system_wrappers/source/metrics_unittest.cc
+++ b/system_wrappers/source/metrics_unittest.cc
@@ -9,7 +9,6 @@
  */
 
 #include "system_wrappers/include/metrics.h"
-#include "system_wrappers/include/metrics_default.h"
 #include "test/gtest.h"
 
 namespace webrtc {
diff --git a/system_wrappers/source/module.mk b/system_wrappers/source/module.mk
index 15ea48a..b4a038a 100644
--- a/system_wrappers/source/module.mk
+++ b/system_wrappers/source/module.mk
@@ -9,14 +9,13 @@
 	system_wrappers/source/cpu_features.o \
 	system_wrappers/source/cpu_info.o \
 	system_wrappers/source/event.o \
-	system_wrappers/source/event_timer_posix.o \
-	system_wrappers/source/field_trial_default.o \
+	system_wrappers/source/field_trial.o \
 	system_wrappers/source/rtp_to_ntp_estimator.o \
 	system_wrappers/source/sleep.o
 
 CXX_STATIC_LIBRARY(system_wrappers/source/libsystem_wrappers.pic.a): \
 	$(system_wrappers_source_CXX_OBJECTS) \
-	system_wrappers/source/metrics_default.o
+	system_wrappers/source/metrics.o
 
 system_wrappers/source/libsystem_wrappers: \
 	CXX_STATIC_LIBRARY(system_wrappers/source/libsystem_wrappers.pic.a)
diff --git a/system_wrappers/source/runtime_enabled_features_default.cc b/system_wrappers/source/runtime_enabled_features_default.cc
deleted file mode 100644
index 1d040a9..0000000
--- a/system_wrappers/source/runtime_enabled_features_default.cc
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- *  Copyright (c) 2018 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 "system_wrappers/include/runtime_enabled_features.h"
-
-#include "rtc_base/flags.h"
-
-namespace flags {
-DEFINE_bool(enable_dual_stream_mode, false, "Enables dual video stream mode.");
-}
-
-namespace webrtc {
-namespace runtime_enabled_features {
-
-bool IsFeatureEnabled(std::string feature_name) {
-  if (feature_name == kDualStreamModeFeatureName)
-    return flags::FLAG_enable_dual_stream_mode;
-  return false;
-}
-
-}  // namespace runtime_enabled_features
-}  // namespace webrtc
diff --git a/webrtc_apm.cc b/webrtc_apm.cc
index 0b9e0cf..dbc35aa 100644
--- a/webrtc_apm.cc
+++ b/webrtc_apm.cc
@@ -11,6 +11,8 @@
 #include "rtc_base/task_queue.h"
 
 extern "C" {
+#include <errno.h>
+
 #include "webrtc_apm.h"
 
 int convert_to_aec3_config(
@@ -434,12 +436,4 @@
 	return 0;
 }
 
-int webrtc_apm_has_echo(webrtc_apm ptr)
-{
-	webrtc::AudioProcessing *apm;
-
-	apm = reinterpret_cast<webrtc::AudioProcessing *>(ptr);
-	return apm->echo_cancellation()->stream_has_echo();
-}
-
 } // extern "C"
diff --git a/webrtc_apm.h b/webrtc_apm.h
index d32060b..81c1784 100644
--- a/webrtc_apm.h
+++ b/webrtc_apm.h
@@ -283,9 +283,6 @@
  */
 WEBRTC_APM_API int webrtc_apm_set_stream_delay(webrtc_apm ptr, int delay_ms);
 
-/* Checks if apm detects echo in forward stream. */
-WEBRTC_APM_API int webrtc_apm_has_echo(webrtc_apm ptr);
-
 /* Dump aec debug info to a file.
  * Args:
  *    ptr - Pointer to the webrtc_apm instance.