Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 39100157 authored by Vadim Caen's avatar Vadim Caen
Browse files

Add sensor rotation option to the test camera

Add a option to set the sensor rotation for the test camera

Bug: N/A
Test: Manulally, creating a test camera
Change-Id: I931a54b492b519d016316ab0d09358b34e26fc11
parent 3d3eca64
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -76,6 +76,8 @@ Available commands:
       --camera_id=(ID) - override numerical ID for test camera instance
       --lens_facing=(front|back|external) - specifies lens facing for test camera instance
       --input_fps=(fps) - specify input fps for test camera, valid values are from 1 to 1000
       --sensor_orientation=(0|90|180|270) - Clockwise angle through which the output image 
           needs to be rotated to be upright on the device screen in its native orientation
 * disable_test_camera
)";
constexpr char kCreateVirtualDevicePermission[] =
@@ -439,6 +441,31 @@ binder_status_t VirtualCameraService::enableTestCameraCmd(
    }
  }

  std::optional<SensorOrientation> sensorOrientation;
  std::optional<int> sensorOrientationInt;
  it = options.find("sensor_orientation");
  if (it != options.end()) {
    sensorOrientationInt = parseInt(it->second);
    switch (sensorOrientationInt.value_or(0)) {
      case 0:
        sensorOrientation = SensorOrientation::ORIENTATION_0;
        break;
      case 90:
        sensorOrientation = SensorOrientation::ORIENTATION_90;
        break;
      case 180:
        sensorOrientation = SensorOrientation::ORIENTATION_180;
        break;
      case 270:
        sensorOrientation = SensorOrientation::ORIENTATION_270;
        break;
      default:
        dprintf(err, "Invalid sensor rotation: %s\n, must be 0, 90, 180 or 270.",
                it->second.c_str());
        return STATUS_BAD_VALUE;
    }
  }

  sp<BBinder> token = sp<BBinder>::make();
  mTestCameraToken.set(AIBinder_fromPlatformBinder(token));

@@ -449,6 +476,8 @@ binder_status_t VirtualCameraService::enableTestCameraCmd(
                                                  Format::RGBA_8888,
                                                  .maxFps = kMaxFps});
  configuration.lensFacing = lensFacing.value_or(LensFacing::EXTERNAL);
  configuration.sensorOrientation =
      sensorOrientation.value_or(SensorOrientation::ORIENTATION_0);
  configuration.virtualCameraCallback =
      ndk::SharedRefBase::make<VirtualCameraTestInstance>(
          inputFps.value_or(kTestCameraDefaultInputFps));
+29 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <cstdio>
#include <iterator>
#include <memory>
#include <optional>
#include <regex>

#include "VirtualCameraService.h"
@@ -191,6 +192,16 @@ class VirtualCameraServiceTest : public ::testing::Test {
    return getLensFacing(metadata);
  }

  std::optional<int32_t> getCameraSensorOrienation(const std::string& id) {
    std::shared_ptr<VirtualCameraDevice> camera = mCameraProvider->getCamera(id);
    if (camera == nullptr) {
      return std::nullopt;
    }
    CameraMetadata metadata;
    camera->getCameraCharacteristics(&metadata);
    return getSensorOrientation(metadata);
  }

 protected:
  std::shared_ptr<VirtualCameraService> mCameraService;
  std::shared_ptr<VirtualCameraProvider> mCameraProvider;
@@ -506,6 +517,24 @@ TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithInvalidInputFps) {
              Eq(STATUS_BAD_VALUE));
}

TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithSensorOrientation90) {
  EXPECT_THAT(
      execute_shell_command("enable_test_camera --sensor_orientation=90"),
      Eq(NO_ERROR));

  std::vector<std::string> cameraIds = getCameraIds();
  ASSERT_THAT(cameraIds, SizeIs(1));
  EXPECT_THAT(getCameraSensorOrienation(cameraIds[0]), Optional(Eq(90)));
}

TEST_F(VirtualCameraServiceTest, TestCameraShellCmdWithSensorOrientationNoArgs) {
  EXPECT_THAT(execute_shell_command("enable_test_camera"), Eq(NO_ERROR));

  std::vector<std::string> cameraIds = getCameraIds();
  ASSERT_THAT(cameraIds, SizeIs(1));
  EXPECT_THAT(getCameraSensorOrienation(cameraIds[0]), Optional(Eq(0)));
}

}  // namespace
}  // namespace virtualcamera
}  // namespace companion
+14 −0
Original line number Diff line number Diff line
@@ -961,6 +961,20 @@ std::optional<int32_t> getDeviceId(
  return static_cast<int32_t>(entry.data.i32[0]);
}

std::optional<int32_t> getSensorOrientation(
    const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata) {
  auto metadata =
      reinterpret_cast<const camera_metadata_t*>(cameraMetadata.metadata.data());

  camera_metadata_ro_entry_t entry;
  if (find_camera_metadata_ro_entry(metadata, ANDROID_SENSOR_ORIENTATION,
                                    &entry) != OK) {
    return std::nullopt;
  }

  return static_cast<int32_t>(entry.data.i32[0]);
}

}  // namespace virtualcamera
}  // namespace companion
}  // namespace android
+5 −0
Original line number Diff line number Diff line
@@ -488,6 +488,11 @@ getPrecaptureTrigger(
std::optional<int32_t> getDeviceId(
    const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata);

// Return the value of ANDROID_SENSOR_ORIENTATION or nullopt if the key is not
// present (which is equivalent to a orientation of 0).
std::optional<int32_t> getSensorOrientation(
    const aidl::android::hardware::camera::device::CameraMetadata& cameraMetadata);

}  // namespace virtualcamera
}  // namespace companion
}  // namespace android