Commit aa824ea0 authored by liberato@chromium.org's avatar liberato@chromium.org Committed by Commit Bot

Add mojo bindings for media learning.

Also adds a typemap for TrainingExample.

Bug: 897463
Change-Id: I66d47d01e56e0a8f250783225662f258646e4cfa
Reviewed-on: https://chromium-review.googlesource.com/c/1338223
Commit-Queue: Frank Liberato <liberato@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarXiaohan Wang <xhwang@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#610162}
parent b209311e
...@@ -10,6 +10,7 @@ source_set("unit_tests") { ...@@ -10,6 +10,7 @@ source_set("unit_tests") {
"//media:test_support", "//media:test_support",
"//media/learning/common:unit_tests", "//media/learning/common:unit_tests",
"//media/learning/impl:unit_tests", "//media/learning/impl:unit_tests",
"//media/learning/mojo:unit_tests",
"//testing/gtest", "//testing/gtest",
] ]
} }
...@@ -69,3 +69,6 @@ All of our models are supervised. ...@@ -69,3 +69,6 @@ All of our models are supervised.
* `common/` - public interfaces * `common/` - public interfaces
* `impl/` - learning algorithms, other implementation details * `impl/` - learning algorithms, other implementation details
* `mojo/` - mojo service-side implementations
* `mojo/public/cpp` - public headers / clients for mojo
* `mojo/public/mojom` - public mojom interfaces
...@@ -8,6 +8,9 @@ component("common") { ...@@ -8,6 +8,9 @@ component("common") {
"//media/learning", "//media/learning",
"//media/learning/impl:*", "//media/learning/impl:*",
"//media/learning/impl:test_support", "//media/learning/impl:test_support",
"//media/learning/mojo/public/cpp:*",
"//media/learning/mojo/public/mojom:mojom",
"//media/learning/mojo:*",
"//media/learning/common:unit_tests", "//media/learning/common:unit_tests",
] ]
......
# 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/buildflag_header.gni")
import("//testing/test.gni")
component("impl") {
output_name = "media_learning_mojo_impl"
sources = [
"mojo_learning_session_impl.cc",
"mojo_learning_session_impl.h",
]
defines = [ "IS_MEDIA_LEARNING_MOJO_IMPL" ]
deps = [
"//base",
"//media/learning/common",
# Since the mojom target generates a source set by default, make sure that
# everybody depends on it themselves.
"//media/learning/mojo/public/mojom",
"//mojo/public/cpp/bindings",
]
public_deps = [
"//media/learning/mojo/public/cpp",
]
}
# Unit Tests
source_set("unit_tests") {
testonly = true
sources = [
"mojo_learning_session_impl_unittest.cc",
]
deps = [
":impl",
"//base",
"//base/test:test_support",
"//media/learning/common",
"//media/learning/mojo/public/cpp:unit_tests",
"//media/learning/mojo/public/mojom",
"//testing/gmock",
"//testing/gtest",
]
}
test("media_learning_mojo_unittests") {
deps = [
":unit_tests",
"//mojo/core/test:run_all_unittests",
]
}
include_rules = [
"+mojo/public",
]
// 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.
#include "media/learning/mojo/mojo_learning_session_impl.h"
#include "media/learning/common/learning_session.h"
namespace media {
namespace learning {
MojoLearningSessionImpl::MojoLearningSessionImpl(
std::unique_ptr<::media::learning::LearningSession> impl)
: impl_(std::move(impl)) {}
MojoLearningSessionImpl::~MojoLearningSessionImpl() = default;
void MojoLearningSessionImpl::Bind(mojom::LearningSessionRequest request) {
self_bindings_.AddBinding(this, std::move(request));
}
void MojoLearningSessionImpl::AddExample(mojom::LearningTaskType task_type,
const TrainingExample& example) {
// TODO(liberato): Convert |task_type| into a task name.
std::string task_name("no_task");
impl_->AddExample(task_name, example);
}
} // namespace learning
} // namespace media
// 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.
#ifndef MEDIA_LEARNING_MOJO_MOJO_LEARNING_SESSION_IMPL_H_
#define MEDIA_LEARNING_MOJO_MOJO_LEARNING_SESSION_IMPL_H_
#include <memory>
#include "base/component_export.h"
#include "base/macros.h"
#include "media/learning/mojo/public/mojom/learning_session.mojom.h"
#include "mojo/public/cpp/bindings/binding_set.h"
namespace media {
namespace learning {
class LearningSession;
class MojoLearningSessionImplTest;
// Mojo service that talks to a local LearningSession.
class COMPONENT_EXPORT(MEDIA_LEARNING_MOJO) MojoLearningSessionImpl
: public mojom::LearningSession {
public:
~MojoLearningSessionImpl() override;
// Bind |request| to this instance.
void Bind(mojom::LearningSessionRequest request);
// mojom::LearningSession
void AddExample(mojom::LearningTaskType task_type,
const TrainingExample& example) override;
protected:
explicit MojoLearningSessionImpl(
std::unique_ptr<::media::learning::LearningSession> impl);
// Underlying session to which we proxy calls.
std::unique_ptr<::media::learning::LearningSession> impl_;
// We own our own bindings. If we're ever not a singleton, then this should
// move to our owner.
mojo::BindingSet<mojom::LearningSession> self_bindings_;
friend class MojoLearningSessionImplTest;
DISALLOW_COPY_AND_ASSIGN(MojoLearningSessionImpl);
};
} // namespace learning
} // namespace media
#endif // MEDIA_LEARNING_MOJO_MOJO_LEARNING_SESSION_IMPL_H_
// 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.
#include <memory>
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "media/learning/common/learning_session.h"
#include "media/learning/mojo/mojo_learning_session_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace learning {
class MojoLearningSessionImplTest : public ::testing::Test {
public:
class FakeLearningSession : public ::media::learning::LearningSession {
public:
void AddExample(const std::string& task_name,
const TrainingExample& example) override {
most_recent_task_name_ = task_name;
most_recent_example_ = example;
}
std::string most_recent_task_name_;
TrainingExample most_recent_example_;
};
public:
MojoLearningSessionImplTest() = default;
~MojoLearningSessionImplTest() override = default;
void SetUp() override {
// Create a mojo learner that forwards to a fake learner.
std::unique_ptr<FakeLearningSession> provider =
std::make_unique<FakeLearningSession>();
fake_learning_session_ = provider.get();
learning_session_impl_ = base::WrapUnique<MojoLearningSessionImpl>(
new MojoLearningSessionImpl(std::move(provider)));
}
FakeLearningSession* fake_learning_session_ = nullptr;
const mojom::LearningTaskType task_type_ =
mojom::LearningTaskType::kPlaceHolderTask;
// The learner provider under test.
std::unique_ptr<MojoLearningSessionImpl> learning_session_impl_;
};
TEST_F(MojoLearningSessionImplTest, FeaturesAndTargetValueAreCopied) {
mojom::TrainingExamplePtr example_ptr = mojom::TrainingExample::New();
const TrainingExample example = {{Value(123), Value(456), Value(890)},
TargetValue(1234)};
learning_session_impl_->AddExample(task_type_, example);
EXPECT_EQ(example, fake_learning_session_->most_recent_example_);
}
} // namespace learning
} // namespace media
# 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.
source_set("cpp") {
visibility = [
"//media/learning/mojo:impl",
"//media/learning/mojo/public/cpp:unit_tests",
]
sources = [
"mojo_learning_session.cc",
"mojo_learning_session.h",
]
defines = [ "IS_MEDIA_LEARNING_MOJO_IMPL" ]
deps = [
"//base",
"//media/learning/common",
"//media/learning/mojo/public/mojom",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"mojo_learning_session_unittest.cc",
]
deps = [
"//base",
"//base/test:test_support",
"//media/learning/common",
"//media/learning/mojo:impl",
"//media/mojo:test_support",
"//mojo/public/cpp/bindings",
"//testing/gmock",
"//testing/gtest",
]
}
per-file *_struct_traits*.*=set noparent
per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS
per-file *_mojom_traits*.*=set noparent
per-file *_mojom_traits*.*=file://ipc/SECURITY_OWNERS
// 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.
#include "media/learning/mojo/public/cpp/learning_mojom_traits.h"
namespace mojo {
// static
bool StructTraits<media::learning::mojom::TrainingExampleDataView,
media::learning::TrainingExample>::
Read(media::learning::mojom::TrainingExampleDataView data,
media::learning::TrainingExample* out_example) {
out_example->features.clear();
if (!data.ReadFeatures(&out_example->features))
return false;
if (!data.ReadTargetValue(&out_example->target_value))
return false;
return true;
}
// static
bool StructTraits<media::learning::mojom::FeatureValueDataView,
media::learning::FeatureValue>::
Read(media::learning::mojom::FeatureValueDataView data,
media::learning::FeatureValue* out_feature_value) {
*out_feature_value = media::learning::FeatureValue(data.value());
return true;
}
// static
bool StructTraits<media::learning::mojom::TargetValueDataView,
media::learning::TargetValue>::
Read(media::learning::mojom::TargetValueDataView data,
media::learning::TargetValue* out_target_value) {
*out_target_value = media::learning::TargetValue(data.value());
return true;
}
} // namespace mojo
// 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.
#ifndef MEDIA_LEARNING_MOJO_PUBLIC_CPP_LEARNING_MOJOM_TRAITS_H_
#define MEDIA_LEARNING_MOJO_PUBLIC_CPP_LEARNING_MOJOM_TRAITS_H_
#include <vector>
#include "media/learning/common/value.h"
#include "media/learning/mojo/public/mojom/learning_types.mojom.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
namespace mojo {
template <>
class StructTraits<media::learning::mojom::TrainingExampleDataView,
media::learning::TrainingExample> {
public:
static const std::vector<media::learning::FeatureValue>& features(
const media::learning::TrainingExample& e) {
return e.features;
}
static media::learning::TargetValue target_value(
const media::learning::TrainingExample& e) {
return e.target_value;
}
static bool Read(media::learning::mojom::TrainingExampleDataView data,
media::learning::TrainingExample* out_example);
};
template <>
class StructTraits<media::learning::mojom::FeatureValueDataView,
media::learning::FeatureValue> {
public:
static int64_t value(const media::learning::FeatureValue& e) {
return e.value();
}
static bool Read(media::learning::mojom::FeatureValueDataView data,
media::learning::FeatureValue* out_feature_value);
};
template <>
class StructTraits<media::learning::mojom::TargetValueDataView,
media::learning::TargetValue> {
public:
static int64_t value(const media::learning::TargetValue& e) {
return e.value();
}
static bool Read(media::learning::mojom::TargetValueDataView data,
media::learning::TargetValue* out_target_value);
};
} // namespace mojo
#endif // MEDIA_LEARNING_MOJO_PUBLIC_CPP_LEARNING_MOJOM_TRAITS_H_
// 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.
#include "media/learning/mojo/public/cpp/mojo_learning_session.h"
#include "mojo/public/cpp/bindings/binding.h"
namespace media {
namespace learning {
MojoLearningSession::MojoLearningSession(mojom::LearningSessionPtr session_ptr)
: session_ptr_(std::move(session_ptr)) {}
MojoLearningSession::~MojoLearningSession() = default;
void MojoLearningSession::AddExample(const std::string& task_name,
const TrainingExample& example) {
// TODO(liberato): Convert from |task_name| to a task type.
session_ptr_->AddExample(mojom::LearningTaskType::kPlaceHolderTask, example);
}
} // namespace learning
} // namespace media
// 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.
#ifndef MEDIA_LEARNING_MOJO_PUBLIC_CPP_MOJO_LEARNING_SESSION_H_
#define MEDIA_LEARNING_MOJO_PUBLIC_CPP_MOJO_LEARNING_SESSION_H_
#include "base/component_export.h"
#include "base/macros.h"
#include "media/learning/common/learning_session.h"
#include "media/learning/mojo/public/mojom/learning_session.mojom.h"
namespace media {
namespace learning {
// LearningSession implementation to forward to a remote impl via mojo.
class COMPONENT_EXPORT(MEDIA_LEARNING_MOJO) MojoLearningSession
: public LearningSession {
public:
explicit MojoLearningSession(mojom::LearningSessionPtr session_ptr);
~MojoLearningSession() override;
// LearningSession
void AddExample(const std::string& task_name,
const TrainingExample& example) override;
private:
mojom::LearningSessionPtr session_ptr_;
DISALLOW_COPY_AND_ASSIGN(MojoLearningSession);
};
} // namespace learning
} // namespace media
#endif // MEDIA_LEARNING_MOJO_PUBLIC_CPP_MOJO_LEARNING_SESSION_H_
// 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.
#include <memory>
#include "base/bind.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "media/learning/mojo/public/cpp/mojo_learning_session.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace learning {
class MojoLearningSessionTest : public ::testing::Test {
public:
// Impl of a mojom::LearningSession that remembers call arguments.
class FakeMojoLearningSessionImpl : public mojom::LearningSession {
public:
void AddExample(mojom::LearningTaskType task_type,
const TrainingExample& example) override {
task_type_ = std::move(task_type);
example_ = example;
}
mojom::LearningTaskType task_type_;
TrainingExample example_;
};
public:
MojoLearningSessionTest()
: learning_session_binding_(&fake_learning_session_impl_) {}
~MojoLearningSessionTest() override = default;
void SetUp() override {
// Create a fake learner provider mojo impl.
mojom::LearningSessionPtr learning_session_ptr;
learning_session_binding_.Bind(mojo::MakeRequest(&learning_session_ptr));
// Tell |learning_session_| to forward to the fake learner impl.
learning_session_ =
std::make_unique<MojoLearningSession>(std::move(learning_session_ptr));
}
// Mojo stuff.
base::test::ScopedTaskEnvironment scoped_task_environment_;
FakeMojoLearningSessionImpl fake_learning_session_impl_;
mojo::Binding<mojom::LearningSession> learning_session_binding_;
// The learner under test.
std::unique_ptr<MojoLearningSession> learning_session_;
};
TEST_F(MojoLearningSessionTest, ExampleIsCopied) {
TrainingExample example({FeatureValue(123), FeatureValue(456)},
TargetValue(1234));
learning_session_->AddExample("unused task id", example);
learning_session_binding_.FlushForTesting();
EXPECT_EQ(fake_learning_session_impl_.example_, example);
}
} // namespace learning
} // namespace media
typemaps = [ "//media/learning/mojo/public/mojom/learning_types.typemap" ]
# 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("//media/media_options.gni")
import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojom") {
sources = [
"learning_session.mojom",
"learning_types.mojom",
]
public_deps = [
"//mojo/public/mojom/base",
]
}
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
per-file *.typemap=set noparent
per-file *.typemap=file://ipc/SECURITY_OWNERS
// 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.
module media.learning.mojom;
import "media/learning/mojo/public/mojom/learning_types.mojom";
// Learning tasks, to prevent sending the task name string in AcquireLearner.
enum LearningTaskType {
// There are no tasks yet.
kPlaceHolderTask,
};
// media/learning/public/learning_session.h
interface LearningSession {
// Add |example| to |task_type|.
AddExample(LearningTaskType task_type, TrainingExample example);
};
// 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.
module media.learning.mojom;
// learning::FeatureValue (common/value.h)
struct FeatureValue {
int64 value;
};
// learning::TargetValue (common/value.h)
struct TargetValue {
int64 value;
};
// learning::TrainingExample (common/training_example.h)
struct TrainingExample {
array<FeatureValue> features;
TargetValue target_value;
};
mojom = "//media/learning/mojo/public/mojom/learning_types.mojom"
public_headers = [
"//media/learning/common/training_example.h",
"//media/learning/common/value.h",
]
traits_headers = [ "//media/learning/mojo/public/cpp/learning_mojom_traits.h" ]
sources = [
"//media/learning/mojo/public/cpp/learning_mojom_traits.cc",
"//media/learning/mojo/public/cpp/learning_mojom_traits.h",
]
public_deps = [
"//media/learning/common",
]
type_mappings = [
"media.learning.mojom.TrainingExample=media::learning::TrainingExample",
"media.learning.mojom.FeatureValue=media::learning::FeatureValue",
"media.learning.mojom.TargetValue=media::learning::TargetValue",
]
...@@ -80,6 +80,7 @@ source_set("unit_tests") { ...@@ -80,6 +80,7 @@ source_set("unit_tests") {
testonly = true testonly = true
deps = [ deps = [
"//media/learning/mojo:unit_tests",
"//media/mojo/clients:unit_tests", "//media/mojo/clients:unit_tests",
"//media/mojo/common:unit_tests", "//media/mojo/common:unit_tests",
"//media/mojo/interfaces:unit_tests", "//media/mojo/interfaces:unit_tests",
......
...@@ -24,6 +24,7 @@ _typemap_imports = [ ...@@ -24,6 +24,7 @@ _typemap_imports = [
"//gpu/ipc/common/typemaps.gni", "//gpu/ipc/common/typemaps.gni",
"//ipc/typemaps.gni", "//ipc/typemaps.gni",
"//media/capture/mojom/typemaps.gni", "//media/capture/mojom/typemaps.gni",
"//media/learning/mojo/public/cpp/typemaps.gni",
"//media/mojo/interfaces/typemaps.gni", "//media/mojo/interfaces/typemaps.gni",
"//mojo/public/cpp/base/typemaps.gni", "//mojo/public/cpp/base/typemaps.gni",
"//mojo/public/cpp/bindings/tests/chromium_typemaps.gni", "//mojo/public/cpp/bindings/tests/chromium_typemaps.gni",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment