| Index: extensions/browser/api/media_perception_private/conversion_utils.cc
|
| diff --git a/extensions/browser/api/media_perception_private/conversion_utils.cc b/extensions/browser/api/media_perception_private/conversion_utils.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a0c4de1f5fb7d39b6bd04b72320c15840851d298
|
| --- /dev/null
|
| +++ b/extensions/browser/api/media_perception_private/conversion_utils.cc
|
| @@ -0,0 +1,243 @@
|
| +// Copyright 2017 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.
|
| +
|
| +#include "extensions/browser/api/media_perception_private/conversion_utils.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace {
|
| +
|
| +std::unique_ptr<media_perception::Point> PointProtoToIdl(
|
| + const mri::Point& point) {
|
| + std::unique_ptr<media_perception::Point> p_result =
|
| + base::MakeUnique<media_perception::Point>();
|
| + if (point.has_x())
|
| + p_result->x = base::MakeUnique<double>(point.x());
|
| +
|
| + if (point.has_y())
|
| + p_result->y = base::MakeUnique<double>(point.y());
|
| +
|
| + return p_result;
|
| +}
|
| +
|
| +std::unique_ptr<media_perception::BoundingBox> BoundingBoxProtoToIdl(
|
| + const mri::BoundingBox& bounding_box) {
|
| + std::unique_ptr<media_perception::BoundingBox> bbox_result =
|
| + base::MakeUnique<media_perception::BoundingBox>();
|
| + if (bounding_box.has_normalized())
|
| + bbox_result->normalized = base::MakeUnique<bool>(bounding_box.normalized());
|
| +
|
| + if (bounding_box.has_top_left())
|
| + bbox_result->top_left = PointProtoToIdl(bounding_box.top_left());
|
| +
|
| + if (bounding_box.has_bottom_right())
|
| + bbox_result->bottom_right = PointProtoToIdl(bounding_box.bottom_right());
|
| +
|
| + return bbox_result;
|
| +}
|
| +
|
| +media_perception::EntityType EntityTypeProtoToIdl(const mri::Entity& entity) {
|
| + if (entity.has_type()) {
|
| + switch (entity.type()) {
|
| + case mri::Entity::FACE:
|
| + return media_perception::ENTITY_TYPE_FACE;
|
| + case mri::Entity::PERSON:
|
| + return media_perception::ENTITY_TYPE_PERSON;
|
| + case mri::Entity::UNSPECIFIED:
|
| + return media_perception::ENTITY_TYPE_UNSPECIFIED;
|
| + }
|
| + NOTREACHED() << "Unknown entity type: " << entity.type();
|
| + }
|
| + return media_perception::ENTITY_TYPE_UNSPECIFIED;
|
| +}
|
| +
|
| +media_perception::Entity EntityProtoToIdl(const mri::Entity& entity) {
|
| + media_perception::Entity e_result;
|
| + if (entity.has_id())
|
| + e_result.id = base::MakeUnique<int>(entity.id());
|
| +
|
| + e_result.type = EntityTypeProtoToIdl(entity);
|
| + if (entity.has_confidence())
|
| + e_result.confidence = base::MakeUnique<double>(entity.confidence());
|
| +
|
| + if (entity.has_bounding_box())
|
| + e_result.bounding_box = BoundingBoxProtoToIdl(entity.bounding_box());
|
| +
|
| + return e_result;
|
| +}
|
| +
|
| +media_perception::FramePerception FramePerceptionProtoToIdl(
|
| + const mri::FramePerception& frame_perception) {
|
| + media_perception::FramePerception fp_result;
|
| + if (frame_perception.has_frame_id())
|
| + fp_result.frame_id = base::MakeUnique<int>(frame_perception.frame_id());
|
| +
|
| + if (frame_perception.has_frame_width_in_px()) {
|
| + fp_result.frame_width_in_px =
|
| + base::MakeUnique<int>(frame_perception.frame_width_in_px());
|
| + }
|
| + if (frame_perception.has_frame_height_in_px()) {
|
| + fp_result.frame_height_in_px =
|
| + base::MakeUnique<int>(frame_perception.frame_height_in_px());
|
| + }
|
| + if (frame_perception.has_timestamp()) {
|
| + fp_result.timestamp =
|
| + base::MakeUnique<double>(frame_perception.timestamp());
|
| + }
|
| + if (frame_perception.entity_size() > 0) {
|
| + fp_result.entities =
|
| + base::MakeUnique<std::vector<media_perception::Entity>>();
|
| + for (const auto& entity : frame_perception.entity()) {
|
| + fp_result.entities->emplace_back(EntityProtoToIdl(entity));
|
| + }
|
| + }
|
| + return fp_result;
|
| +}
|
| +
|
| +media_perception::ImageFormat ImageFormatProtoToIdl(
|
| + const mri::ImageFrame& image_frame) {
|
| + if (image_frame.has_format()) {
|
| + switch (image_frame.format()) {
|
| + case mri::ImageFrame::RGB:
|
| + return media_perception::IMAGE_FORMAT_RGB;
|
| + case mri::ImageFrame::PNG:
|
| + return media_perception::IMAGE_FORMAT_PNG;
|
| + case mri::ImageFrame::JPEG:
|
| + return media_perception::IMAGE_FORMAT_JPEG;
|
| + case mri::ImageFrame::FORMAT_UNSPECIFIED:
|
| + return media_perception::IMAGE_FORMAT_UNSPECIFIED;
|
| + }
|
| + NOTREACHED() << "Unknown image format: " << image_frame.format();
|
| + }
|
| + return media_perception::IMAGE_FORMAT_UNSPECIFIED;
|
| +}
|
| +
|
| +media_perception::ImageFrame ImageFrameProtoToIdl(
|
| + const mri::ImageFrame& image_frame) {
|
| + media_perception::ImageFrame if_result;
|
| + if (image_frame.has_width())
|
| + if_result.width = base::MakeUnique<int>(image_frame.width());
|
| +
|
| + if (image_frame.has_height())
|
| + if_result.height = base::MakeUnique<int>(image_frame.height());
|
| +
|
| + if (image_frame.has_data_length())
|
| + if_result.data_length = base::MakeUnique<int>(image_frame.data_length());
|
| +
|
| + if (image_frame.has_pixel_data()) {
|
| + if_result.frame = base::MakeUnique<std::vector<char>>(
|
| + image_frame.pixel_data().begin(), image_frame.pixel_data().end());
|
| + }
|
| +
|
| + if_result.format = ImageFormatProtoToIdl(image_frame);
|
| + return if_result;
|
| +}
|
| +
|
| +media_perception::PerceptionSample PerceptionSampleProtoToIdl(
|
| + const mri::PerceptionSample& perception_sample) {
|
| + media_perception::PerceptionSample ps_result;
|
| + if (perception_sample.has_frame_perception()) {
|
| + ps_result.frame_perception =
|
| + base::MakeUnique<media_perception::FramePerception>(
|
| + FramePerceptionProtoToIdl(perception_sample.frame_perception()));
|
| + }
|
| + if (perception_sample.has_image_frame()) {
|
| + ps_result.image_frame = base::MakeUnique<media_perception::ImageFrame>(
|
| + ImageFrameProtoToIdl(perception_sample.image_frame()));
|
| + }
|
| + return ps_result;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +media_perception::State StateProtoToIdl(const mri::State& state) {
|
| + media_perception::State s_result;
|
| + if (state.has_status()) {
|
| + switch (state.status()) {
|
| + case mri::State::UNINITIALIZED:
|
| + s_result.status = media_perception::STATUS_UNINITIALIZED;
|
| + break;
|
| + case mri::State::STARTED:
|
| + s_result.status = media_perception::STATUS_STARTED;
|
| + break;
|
| + case mri::State::RUNNING:
|
| + s_result.status = media_perception::STATUS_RUNNING;
|
| + break;
|
| + case mri::State::SUSPENDED:
|
| + s_result.status = media_perception::STATUS_SUSPENDED;
|
| + break;
|
| + case mri::State::STATUS_UNSPECIFIED:
|
| + default:
|
| + NOTREACHED() << "Status not set.";
|
| + break;
|
| + }
|
| + }
|
| + if (state.has_device_context()) {
|
| + s_result.device_context =
|
| + base::MakeUnique<std::string>(state.device_context());
|
| + }
|
| + return s_result;
|
| +}
|
| +
|
| +mri::State StateIdlToProto(const media_perception::State& state) {
|
| + mri::State s_result;
|
| + switch (state.status) {
|
| + case media_perception::STATUS_UNINITIALIZED:
|
| + s_result.set_status(mri::State::UNINITIALIZED);
|
| + break;
|
| + case media_perception::STATUS_STARTED:
|
| + s_result.set_status(mri::State::STARTED);
|
| + break;
|
| + case media_perception::STATUS_RUNNING:
|
| + s_result.set_status(mri::State::RUNNING);
|
| + break;
|
| + case media_perception::STATUS_SUSPENDED:
|
| + s_result.set_status(mri::State::SUSPENDED);
|
| + break;
|
| + default:
|
| + NOTREACHED() << "Status not set.";
|
| + break;
|
| + }
|
| + if (state.device_context)
|
| + s_result.set_device_context(*state.device_context);
|
| +
|
| + return s_result;
|
| +}
|
| +
|
| +media_perception::MediaPerception MediaPerceptionProtoToIdl(
|
| + const mri::MediaPerception& media_perception) {
|
| + media_perception::MediaPerception mp_result;
|
| + if (media_perception.has_timestamp()) {
|
| + mp_result.timestamp =
|
| + base::MakeUnique<double>(media_perception.timestamp());
|
| + }
|
| +
|
| + if (media_perception.frame_perception_size() > 0) {
|
| + mp_result.frame_perceptions =
|
| + base::MakeUnique<std::vector<media_perception::FramePerception>>();
|
| + for (const auto& frame_perception : media_perception.frame_perception()) {
|
| + mp_result.frame_perceptions->emplace_back(
|
| + FramePerceptionProtoToIdl(frame_perception));
|
| + }
|
| + }
|
| + return mp_result;
|
| +}
|
| +
|
| +media_perception::Diagnostics DiagnosticsProtoToIdl(
|
| + const mri::Diagnostics& diagnostics) {
|
| + media_perception::Diagnostics d_result;
|
| + if (diagnostics.perception_sample_size() > 0) {
|
| + d_result.perception_samples =
|
| + base::MakeUnique<std::vector<media_perception::PerceptionSample>>();
|
| + for (const auto& perception_sample : diagnostics.perception_sample()) {
|
| + d_result.perception_samples->emplace_back(
|
| + PerceptionSampleProtoToIdl(perception_sample));
|
| + }
|
| + }
|
| + return d_result;
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|