Restore setting encoder speed for AV1 encoder wrapper

Also add simple unittests for the wrapper.

Bug: webrtc:11404
Change-Id: I41d185da9bce392297d1982194c059bddb7881ac
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/171481
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30867}
diff --git a/modules/video_coding/BUILD.gn b/modules/video_coding/BUILD.gn
index 4dd19d5..0183a3a 100644
--- a/modules/video_coding/BUILD.gn
+++ b/modules/video_coding/BUILD.gn
@@ -948,6 +948,7 @@
       "../../test/time_controller:time_controller",
       "../rtp_rtcp:rtp_rtcp_format",
       "../rtp_rtcp:rtp_video_header",
+      "codecs/av1:video_coding_codecs_av1_tests",
       "//third_party/abseil-cpp/absl/memory",
       "//third_party/abseil-cpp/absl/types:optional",
       "//third_party/abseil-cpp/absl/types:variant",
diff --git a/modules/video_coding/codecs/av1/BUILD.gn b/modules/video_coding/codecs/av1/BUILD.gn
index 7e25189..4faa6a6 100644
--- a/modules/video_coding/codecs/av1/BUILD.gn
+++ b/modules/video_coding/codecs/av1/BUILD.gn
@@ -63,3 +63,17 @@
     sources = [ "libaom_av1_encoder_absent.cc" ]
   }
 }
+
+rtc_library("video_coding_codecs_av1_tests") {
+  testonly = true
+
+  if (enable_libaom) {
+    sources = [ "libaom_av1_encoder_unittest.cc" ]
+    deps = [
+      ":libaom_av1_encoder",
+      "../..:video_codec_interface",
+      "../../../../api/video_codecs:video_codecs_api",
+      "../../../../test:test_support",
+    ]
+  }
+}
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
index 6fc5992..59ad127 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
@@ -36,9 +36,10 @@
 // Encoder configuration parameters
 constexpr int kQpMax = 56;
 constexpr int kQpMin = 10;
-constexpr int kUsageProfile = 1;  // 0 = good quality; 1 = real-time.
-constexpr int kMinQindex = 58;    // Min qindex threshold for QP scaling.
-constexpr int kMaxQindex = 180;   // Max qindex threshold for QP scaling.
+constexpr int kDefaultEncSpeed = 7;  // Use values 6, 7, or 8 for RTC.
+constexpr int kUsageProfile = 1;     // 0 = good quality; 1 = real-time.
+constexpr int kMinQindex = 58;       // Min qindex threshold for QP scaling.
+constexpr int kMaxQindex = 180;      // Max qindex threshold for QP scaling.
 constexpr int kBitDepth = 8;
 constexpr int kLagInFrames = 0;  // No look ahead.
 constexpr int kRtpTicksPerSecond = 90000;
@@ -179,6 +180,12 @@
   inited_ = true;
 
   // Set control parameters
+  ret = aom_codec_control(&ctx_, AOME_SET_CPUUSED, kDefaultEncSpeed);
+  if (ret != AOM_CODEC_OK) {
+    RTC_LOG(LS_WARNING) << "LibaomAv1Encoder::EncodeInit returned " << ret
+                        << " on control AV1E_SET_CPUUSED.";
+    return WEBRTC_VIDEO_CODEC_ERROR;
+  }
   ret = aom_codec_control(&ctx_, AV1E_SET_ENABLE_TPL_MODEL, 0);
   if (ret != AOM_CODEC_OK) {
     RTC_LOG(LS_WARNING) << "LibaomAv1Encoder::EncodeInit returned " << ret
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder_unittest.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder_unittest.cc
new file mode 100644
index 0000000..6d1d0bb
--- /dev/null
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder_unittest.cc
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (c) 2020 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/video_coding/codecs/av1/libaom_av1_encoder.h"
+
+#include <memory>
+
+#include "api/video_codecs/video_codec.h"
+#include "api/video_codecs/video_encoder.h"
+#include "modules/video_coding/include/video_error_codes.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace {
+
+TEST(LibaomAv1EncoderTest, CanCreate) {
+  std::unique_ptr<VideoEncoder> encoder = CreateLibaomAv1Encoder();
+  EXPECT_TRUE(encoder);
+}
+
+TEST(LibaomAv1EncoderTest, InitAndRelease) {
+  std::unique_ptr<VideoEncoder> encoder = CreateLibaomAv1Encoder();
+  ASSERT_TRUE(encoder);
+  VideoCodec codec_settings;
+  codec_settings.width = 1280;
+  codec_settings.height = 720;
+  codec_settings.maxFramerate = 30;
+  VideoEncoder::Capabilities capabilities(/*loss_notification=*/false);
+  VideoEncoder::Settings encoder_settings(capabilities, /*number_of_cores=*/1,
+                                          /*max_payload_size=*/1200);
+  EXPECT_EQ(encoder->InitEncode(&codec_settings, encoder_settings),
+            WEBRTC_VIDEO_CODEC_OK);
+  EXPECT_EQ(encoder->Release(), WEBRTC_VIDEO_CODEC_OK);
+}
+
+}  // namespace
+}  // namespace webrtc