Project import generated by Copybara.

GitOrigin-RevId: d073f8e21be2fcc0e503cb97c6695078b6b75310
This commit is contained in:
MediaPipe Team
2021-02-27 03:30:05 -05:00
committed by chuoling
parent 39309bedba
commit 350fbb2100
755 changed files with 16391 additions and 11075 deletions
+3 -7
View File
@@ -269,12 +269,6 @@ cc_library(
"calculator_graph.h",
"scheduler.h",
],
defines = select({
"//conditions:default": [],
"//mediapipe/gpu:disable_gpu": [
"MEDIAPIPE_DISABLE_GPU",
],
}),
visibility = [
":mediapipe_internal",
],
@@ -460,6 +454,7 @@ cc_library(
":type_map",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/tool:tag_map",
"//mediapipe/framework/tool:tag_map_helper",
"//mediapipe/framework/tool:validate_name",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/memory",
@@ -917,7 +912,7 @@ cc_library(
"//conditions:default": [],
}) + select({
"//conditions:default": [],
"//mediapipe/gpu:disable_gpu": ["MEDIAPIPE_DISABLE_GPU"],
"//mediapipe/gpu:disable_gpu": ["MEDIAPIPE_DISABLE_GPU=1"],
}) + select({
"//conditions:default": [],
"//mediapipe/framework:disable_rtti_and_exceptions": [
@@ -928,6 +923,7 @@ cc_library(
"//mediapipe/calculators:__subpackages__",
"//mediapipe/framework:__subpackages__",
"//mediapipe/framework/port:__pkg__",
"//mediapipe/gpu:__pkg__",
"//mediapipe/util:__subpackages__",
],
)
+1 -5
View File
@@ -3,15 +3,10 @@ package(
features = ["-use_header_modules"],
)
# API2 is in preview mode. Internal clients are welcome and encouraged to try
# it out, but be aware that there may be more changes before release. Please
# add your package to this list and reach out to the MediaPipe team (use
# camillol@ as the CL reviewer).
package_group(
name = "preview_users",
packages = [
"//mediapipe/...",
"//video/content_analysis/...",
],
)
@@ -134,6 +129,7 @@ cc_library(
":tuple",
"//mediapipe/framework:packet",
"//mediapipe/framework/port:logging",
"@com_google_absl//absl/meta:type_traits",
],
)
+6 -7
View File
@@ -481,8 +481,7 @@ class Graph {
std::string TaggedName(const TagIndexLocation& loc, const std::string& name) {
if (loc.tag.empty()) {
// ParseTagIndexName does not allow using explicit indices without tags,
// while ParseTagIndex does. There is no explanation for this discrepancy
// in the CLs that introduced them (cl/143209019, cl/156499931).
// while ParseTagIndex does.
// TODO: decide whether we should just allow it.
return name;
} else {
@@ -494,8 +493,8 @@ class Graph {
}
}
mediapipe::Status UpdateNodeConfig(const NodeBase& node,
CalculatorGraphConfig::Node* config) {
absl::Status UpdateNodeConfig(const NodeBase& node,
CalculatorGraphConfig::Node* config) {
config->set_calculator(node.type_);
node.in_streams_.Visit(
[&](const TagIndexLocation& loc, const DestinationBase& endpoint) {
@@ -521,8 +520,8 @@ class Graph {
return {};
}
mediapipe::Status UpdateNodeConfig(const PacketGenerator& node,
PacketGeneratorConfig* config) {
absl::Status UpdateNodeConfig(const PacketGenerator& node,
PacketGeneratorConfig* config) {
config->set_packet_generator(node.type_);
node.in_sides_.Visit([&](const TagIndexLocation& loc,
const DestinationBase& endpoint) {
@@ -540,7 +539,7 @@ class Graph {
}
// For special boundary node.
mediapipe::Status UpdateBoundaryConfig(CalculatorGraphConfig* config) {
absl::Status UpdateBoundaryConfig(CalculatorGraphConfig* config) {
graph_boundary_.in_streams_.Visit(
[&](const TagIndexLocation& loc, const DestinationBase& endpoint) {
CHECK(endpoint.source != nullptr);
+22 -27
View File
@@ -26,7 +26,7 @@ class StreamHandler {
const const_str& name() { return name_; }
mediapipe::Status AddToContract(CalculatorContract* cc) const {
absl::Status AddToContract(CalculatorContract* cc) const {
cc->SetInputStreamHandler(name_.data());
return {};
}
@@ -47,7 +47,7 @@ class TimestampChange {
return TimestampChange(kUnset);
}
mediapipe::Status AddToContract(CalculatorContract* cc) const {
absl::Status AddToContract(CalculatorContract* cc) const {
if (offset_ != kUnset) cc->SetTimestampOffset(offset_);
return {};
}
@@ -71,10 +71,9 @@ struct HasProcessMethod : std::false_type {};
template <class T>
struct HasProcessMethod<
T, std::void_t<decltype(mediapipe::Status(
std::declval<std::decay_t<T>>().Process(
std::declval<mediapipe::CalculatorContext*>())))>>
: std::true_type {};
T,
std::void_t<decltype(absl::Status(std::declval<std::decay_t<T>>().Process(
std::declval<mediapipe::CalculatorContext*>())))>> : std::true_type {};
template <class T, class = void>
struct HasNestedItems : std::false_type {};
@@ -142,9 +141,9 @@ class Contract {
constexpr Contract(T&&... args)
: Contract(std::tuple<T...>{std::move(args)...}) {}
mediapipe::Status GetContract(mediapipe::CalculatorContract* cc) const {
std::vector<mediapipe::Status> statuses;
auto store_status = [&statuses](mediapipe::Status status) {
absl::Status GetContract(mediapipe::CalculatorContract* cc) const {
std::vector<absl::Status> statuses;
auto store_status = [&statuses](absl::Status status) {
if (!status.ok()) statuses.push_back(std::move(status));
};
internal::tuple_for_each(
@@ -209,7 +208,7 @@ class TaggedContract {
public:
constexpr TaggedContract() = default;
static mediapipe::Status GetContract(mediapipe::CalculatorContract* cc) {
static absl::Status GetContract(mediapipe::CalculatorContract* cc) {
return c2.GetContract(cc);
}
@@ -272,34 +271,32 @@ class OutputSender {
OutputSender(std::tuple<P...>&& args) : outputs_(args) {}
template <class R, std::enable_if_t<sizeof...(P) == 1, int> = 0>
mediapipe::Status operator()(CalculatorContext* cc,
mediapipe::StatusOr<R>&& result) {
absl::Status operator()(CalculatorContext* cc, absl::StatusOr<R>&& result) {
if (result.ok()) {
return this(cc, result.ValueOrDie());
return this(cc, result.value());
} else {
return result.status();
}
}
template <class R, std::enable_if_t<sizeof...(P) == 1, int> = 0>
mediapipe::Status operator()(CalculatorContext* cc, R&& result) {
absl::Status operator()(CalculatorContext* cc, R&& result) {
std::get<0>(outputs_)(cc).Send(std::forward<R>(result));
return {};
}
template <class... R>
mediapipe::Status operator()(CalculatorContext* cc,
mediapipe::StatusOr<std::tuple<R...>>&& result) {
absl::Status operator()(CalculatorContext* cc,
absl::StatusOr<std::tuple<R...>>&& result) {
if (result.ok()) {
return this(cc, result.ValueOrDie());
return this(cc, result.value());
} else {
return result.status();
}
}
template <class... R>
mediapipe::Status operator()(CalculatorContext* cc,
std::tuple<R...>&& result) {
absl::Status operator()(CalculatorContext* cc, std::tuple<R...>&& result) {
static_assert(sizeof...(P) == sizeof...(R), "");
internal::tuple_for_each(
[cc, &result](const auto& port, auto i_const) {
@@ -345,9 +342,9 @@ class FunCaller {
auto inputs() const { return internal::filter_tuple<IsInputPort>(args_); }
auto outputs() const { return internal::filter_tuple<IsOutputPort>(args_); }
mediapipe::Status AddToContract(CalculatorContract* cc) const { return {}; }
absl::Status AddToContract(CalculatorContract* cc) const { return {}; }
mediapipe::Status Process(CalculatorContext* cc) const { return (*this)(cc); }
absl::Status Process(CalculatorContext* cc) const { return (*this)(cc); }
constexpr std::tuple<P...> nested_items() const { return args_; }
@@ -359,16 +356,14 @@ class FunCaller {
// TODO: implement multiple callers for syncsets.
template <class... T>
mediapipe::Status ProcessFnCallers(CalculatorContext* cc,
std::tuple<T...> callers);
absl::Status ProcessFnCallers(CalculatorContext* cc, std::tuple<T...> callers);
inline mediapipe::Status ProcessFnCallers(CalculatorContext* cc, std::tuple<>) {
return mediapipe::InternalError("Process unimplemented");
inline absl::Status ProcessFnCallers(CalculatorContext* cc, std::tuple<>) {
return absl::InternalError("Process unimplemented");
}
template <class T>
mediapipe::Status ProcessFnCallers(CalculatorContext* cc,
std::tuple<T> callers) {
absl::Status ProcessFnCallers(CalculatorContext* cc, std::tuple<T> callers) {
return std::get<0>(callers).Process(cc);
}
+1 -1
View File
@@ -10,7 +10,7 @@ namespace api2 {
namespace {
struct ProcessItem {
mediapipe::Status Process(CalculatorContext* cc) { return {}; }
absl::Status Process(CalculatorContext* cc) { return {}; }
};
struct ItemWithNested {
+3 -3
View File
@@ -34,7 +34,7 @@ class CalculatorBaseFactoryFor<
typename std::enable_if<std::is_base_of<mediapipe::api2::Node, T>{}>::type>
: public CalculatorBaseFactory {
public:
mediapipe::Status GetContract(CalculatorContract* cc) final {
absl::Status GetContract(CalculatorContract* cc) final {
auto status = T::Contract::GetContract(cc);
if (status.ok()) {
status = UpdateContract<T>(cc);
@@ -54,7 +54,7 @@ class CalculatorBaseFactoryFor<
return U::UpdateContract(cc);
}
template <typename U>
mediapipe::Status UpdateContract(...) {
absl::Status UpdateContract(...) {
return {};
}
};
@@ -142,7 +142,7 @@ class RegisteredNode<void> : public Node {};
template <class Impl>
struct FunctionNode : public RegisteredNode<Impl> {
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
return internal::ProcessFnCallers(cc, Impl::kContract.process_items());
}
};
+55 -12
View File
@@ -12,6 +12,7 @@
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/port/status_macros.h"
#include "mediapipe/framework/port/status_matchers.h"
namespace mediapipe {
@@ -32,7 +33,7 @@ std::vector<T> PacketValues(const std::vector<mediapipe::Packet>& packets) {
class FooImpl : public NodeImpl<Foo, FooImpl> {
public:
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
float bias = kBias(cc).GetOr(0.0);
float scale = kScale(cc).GetOr(1.0);
kOut(cc).Send(*kBase(cc) * scale + bias);
@@ -80,7 +81,7 @@ class Foo5 : public FunctionNode<Foo5> {
class Foo2Impl : public NodeImpl<Foo2, Foo2Impl> {
public:
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
float bias = SideIn(MPP_TAG("BIAS"), cc).GetOr(0.0);
float scale = In(MPP_TAG("SCALE"), cc).GetOr(1.0);
Out(MPP_TAG("OUT"), cc).Send(*In(MPP_TAG("BASE"), cc) * scale + bias);
@@ -90,7 +91,7 @@ class Foo2Impl : public NodeImpl<Foo2, Foo2Impl> {
class BarImpl : public NodeImpl<Bar, BarImpl> {
public:
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
Packet p = kIn(cc);
kOut(cc).Send(p);
return {};
@@ -99,9 +100,9 @@ class BarImpl : public NodeImpl<Bar, BarImpl> {
class BazImpl : public NodeImpl<Baz> {
public:
static mediapipe::Status UpdateContract(CalculatorContract* cc) { return {}; }
static absl::Status UpdateContract(CalculatorContract* cc) { return {}; }
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
for (int i = 0; i < kData(cc).Count(); ++i) {
kDataOut(cc)[i].Send(kData(cc)[i]);
}
@@ -112,7 +113,7 @@ MEDIAPIPE_NODE_IMPLEMENTATION(BazImpl);
class IntForwarderImpl : public NodeImpl<IntForwarder, IntForwarderImpl> {
public:
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
kOut(cc).Send(*kIn(cc));
return {};
}
@@ -120,7 +121,7 @@ class IntForwarderImpl : public NodeImpl<IntForwarder, IntForwarderImpl> {
class ToFloatImpl : public NodeImpl<ToFloat, ToFloatImpl> {
public:
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
kIn(cc).Visit([cc](auto x) { kOut(cc).Send(x); });
return {};
}
@@ -315,7 +316,7 @@ struct SideFallback : public Node {
MEDIAPIPE_NODE_CONTRACT(kIn, kFactor, kOut);
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
kOut(cc).Send(kIn(cc).Get() * kFactor(cc).Get());
return {};
}
@@ -341,7 +342,7 @@ TEST(NodeTest, SideFallbackWithStream) {
MP_EXPECT_OK(
graph.ObserveOutputStream("out", [&outputs](const mediapipe::Packet& p) {
outputs.push_back(p.Get<int>());
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_EXPECT_OK(graph.StartRun({}));
MP_EXPECT_OK(graph.AddPacketToInputStream(
@@ -372,7 +373,7 @@ TEST(NodeTest, SideFallbackWithSide) {
MP_EXPECT_OK(
graph.ObserveOutputStream("out", [&outputs](const mediapipe::Packet& p) {
outputs.push_back(p.Get<int>());
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_EXPECT_OK(graph.StartRun({{"factor", mediapipe::MakePacket<int>(2)}}));
MP_EXPECT_OK(graph.AddPacketToInputStream(
@@ -451,7 +452,7 @@ struct DropEvenTimestamps : public Node {
MEDIAPIPE_NODE_CONTRACT(kIn, kOut);
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
if (cc->InputTimestamp().Value() % 2) {
kOut(cc).Send(kIn(cc));
}
@@ -466,7 +467,7 @@ struct ListIntPackets : public Node {
MEDIAPIPE_NODE_CONTRACT(kIn, kOut);
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
std::string result = absl::StrCat(cc->InputTimestamp().DebugString(), ":");
for (int i = 0; i < kIn(cc).Count(); ++i) {
if (kIn(cc)[i].IsEmpty()) {
@@ -522,6 +523,48 @@ TEST(NodeTest, DefaultTimestampChange0) {
MP_EXPECT_OK(graph.WaitUntilDone());
}
struct ConsumerNode : public Node {
static constexpr Input<int> kInt{"INT"};
static constexpr Input<AnyType> kGeneric{"ANY"};
static constexpr Input<OneOf<int, float>> kOneOf{"NUM"};
MEDIAPIPE_NODE_CONTRACT(kInt, kGeneric, kOneOf);
absl::Status Process(CalculatorContext* cc) override {
ASSIGN_OR_RETURN(auto maybe_int, kInt(cc).Consume());
ASSIGN_OR_RETURN(auto maybe_float, kGeneric(cc).Consume<float>());
ASSIGN_OR_RETURN(auto maybe_int2, kOneOf(cc).Consume<int>());
return {};
}
};
MEDIAPIPE_REGISTER_NODE(ConsumerNode);
TEST(NodeTest, ConsumeInputs) {
CalculatorGraphConfig config =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
input_stream: "int"
input_stream: "any"
input_stream: "num"
node {
calculator: "ConsumerNode"
input_stream: "INT:int"
input_stream: "ANY:any"
input_stream: "NUM:num"
}
)");
mediapipe::CalculatorGraph graph;
MP_EXPECT_OK(graph.Initialize(config, {}));
MP_EXPECT_OK(graph.StartRun({}));
MP_EXPECT_OK(graph.AddPacketToInputStream(
"int", mediapipe::MakePacket<int>(10).At(Timestamp(0))));
MP_EXPECT_OK(graph.AddPacketToInputStream(
"any", mediapipe::MakePacket<float>(10).At(Timestamp(0))));
MP_EXPECT_OK(graph.AddPacketToInputStream(
"num", mediapipe::MakePacket<int>(10).At(Timestamp(0))));
MP_EXPECT_OK(graph.CloseAllPacketSources());
MP_EXPECT_OK(graph.WaitUntilDone());
}
} // namespace test
} // namespace api2
} // namespace mediapipe
+10
View File
@@ -7,9 +7,19 @@ PacketBase FromOldPacket(const mediapipe::Packet& op) {
return PacketBase(packet_internal::GetHolderShared(op)).At(op.Timestamp());
}
PacketBase FromOldPacket(mediapipe::Packet&& op) {
Timestamp t = op.Timestamp();
return PacketBase(packet_internal::GetHolderShared(std::move(op))).At(t);
}
mediapipe::Packet ToOldPacket(const PacketBase& p) {
return mediapipe::packet_internal::Create(p.payload_, p.timestamp_);
}
mediapipe::Packet ToOldPacket(PacketBase&& p) {
return mediapipe::packet_internal::Create(std::move(p.payload_),
p.timestamp_);
}
} // namespace api2
} // namespace mediapipe
+115 -1
View File
@@ -13,6 +13,7 @@
#include <functional>
#include <type_traits>
#include "absl/meta/type_traits.h"
#include "mediapipe/framework/api2/tuple.h"
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/port/logging.h"
@@ -58,7 +59,22 @@ class PacketBase {
const T& Get() const;
// Conversion to old Packet type.
operator mediapipe::Packet() const { return ToOldPacket(*this); }
operator mediapipe::Packet() const& { return ToOldPacket(*this); }
operator mediapipe::Packet() && { return ToOldPacket(std::move(*this)); }
// Note: Consume is included for compatibility with the old Packet; however,
// it relies on shared_ptr.unique(), which is deprecated and is not guaranteed
// to give exact results.
template <typename T>
absl::StatusOr<std::unique_ptr<T>> Consume() {
// Using the implementation in the old Packet for now.
mediapipe::Packet old =
packet_internal::Create(std::move(payload_), timestamp_);
auto result = old.Consume<T>();
if (!result.ok())
payload_ = packet_internal::GetHolderShared(std::move(old));
return result;
}
protected:
explicit PacketBase(std::shared_ptr<HolderBase> payload)
@@ -70,11 +86,15 @@ class PacketBase {
template <typename T>
friend PacketBase PacketBaseAdopting(const T* ptr);
friend PacketBase FromOldPacket(const mediapipe::Packet& op);
friend PacketBase FromOldPacket(mediapipe::Packet&& op);
friend mediapipe::Packet ToOldPacket(const PacketBase& p);
friend mediapipe::Packet ToOldPacket(PacketBase&& p);
};
PacketBase FromOldPacket(const mediapipe::Packet& op);
PacketBase FromOldPacket(mediapipe::Packet&& op);
mediapipe::Packet ToOldPacket(const PacketBase& p);
mediapipe::Packet ToOldPacket(PacketBase&& p);
template <typename T>
inline const T& PacketBase::Get() const {
@@ -132,6 +152,16 @@ struct Generic {
Generic() = delete;
};
template <class V, class U>
struct IsCompatibleType : std::false_type {};
template <class V>
struct IsCompatibleType<V, V> : std::true_type {};
template <class V>
struct IsCompatibleType<V, internal::Generic> : std::true_type {};
template <class V, class... U>
struct IsCompatibleType<V, OneOf<U...>>
: std::integral_constant<bool, (std::is_same_v<V, U> || ...)> {};
}; // namespace internal
template <typename T>
@@ -191,6 +221,13 @@ class Packet : public Packet<internal::Generic> {
return IsEmpty() ? static_cast<T>(absl::forward<U>(v)) : **this;
}
// Note: Consume is included for compatibility with the old Packet; however,
// it relies on shared_ptr.unique(), which is deprecated and is not guaranteed
// to give exact results.
absl::StatusOr<std::unique_ptr<T>> Consume() {
return PacketBase::Consume<T>();
}
private:
explicit Packet(std::shared_ptr<HolderBase> payload)
: Packet<internal::Generic>(std::move(payload)) {}
@@ -216,6 +253,44 @@ template <class T, class... U>
struct First {
using type = T;
};
template <class T>
struct AddStatus {
using type = StatusOr<T>;
};
template <class T>
struct AddStatus<StatusOr<T>> {
using type = StatusOr<T>;
};
template <>
struct AddStatus<Status> {
using type = Status;
};
template <>
struct AddStatus<void> {
using type = Status;
};
template <class R, class F, class... A>
struct CallAndAddStatusImpl {
typename AddStatus<R>::type operator()(const F& f, A&&... a) {
return f(std::forward<A>(a)...);
}
};
template <class F, class... A>
struct CallAndAddStatusImpl<void, F, A...> {
Status operator()(const F& f, A&&... a) {
f(std::forward<A>(a)...);
return {};
}
};
template <class F, class... A>
auto CallAndAddStatus(const F& f, A&&... a) {
return CallAndAddStatusImpl<absl::result_of_t<F(A...)>, F, A...>()(
f, std::forward<A>(a)...);
}
} // namespace internal
template <class... T>
@@ -276,6 +351,30 @@ class Packet<OneOf<T...>> : public PacketBase {
return Invoke<decltype(f), T...>(f);
}
// Note: Consume is included for compatibility with the old Packet; however,
// it relies on shared_ptr.unique(), which is deprecated and is not guaranteed
// to give exact results.
template <class U, class = AllowedType<U>>
absl::StatusOr<std::unique_ptr<U>> Consume() {
return PacketBase::Consume<U>();
}
template <class... F>
auto ConsumeAndVisit(const F&... args) {
CHECK(payload_);
auto f = internal::Overload{args...};
using FirstT = typename internal::First<T...>::type;
using VisitorResultType =
absl::result_of_t<decltype(f)(std::unique_ptr<FirstT>)>;
static_assert(
(std::is_same_v<VisitorResultType,
absl::result_of_t<decltype(f)(std::unique_ptr<T>)>> &&
...),
"All visitor overloads must have the same return type");
using ResultType = typename internal::AddStatus<VisitorResultType>::type;
return InvokeConsuming<ResultType, decltype(f), T...>(f);
}
protected:
explicit Packet(std::shared_ptr<HolderBase> payload)
: PacketBase(std::move(payload)) {}
@@ -292,6 +391,21 @@ class Packet<OneOf<T...>> : public PacketBase {
auto Invoke(const F& f) const {
return Has<U>() ? f(Get<U>()) : Invoke<F, V, W...>(f);
}
template <class R, class F, class U>
auto InvokeConsuming(const F& f) -> R {
auto maybe_value = Consume<U>();
if (maybe_value.ok())
return internal::CallAndAddStatus(f, std::move(maybe_value).value());
else
return maybe_value.status();
}
template <class R, class F, class U, class V, class... W>
auto InvokeConsuming(const F& f) -> R {
return Has<U>() ? InvokeConsuming<R, F, U>(f)
: InvokeConsuming<R, F, V, W...>(f);
}
};
template <>
+52
View File
@@ -1,7 +1,9 @@
#include "mediapipe/framework/api2/packet.h"
#include "absl/strings/str_cat.h"
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/status_matchers.h"
namespace mediapipe {
namespace api2 {
@@ -168,12 +170,26 @@ TEST(PacketTest, FromOldPacket) {
mediapipe::Packet op = mediapipe::MakePacket<int>(7);
Packet<int> p = FromOldPacket(op).As<int>();
EXPECT_EQ(p.Get(), 7);
EXPECT_EQ(op.Get<int>(), 7);
}
TEST(PacketTest, FromOldPacketConsume) {
mediapipe::Packet op = mediapipe::MakePacket<int>(7);
Packet<int> p = FromOldPacket(std::move(op)).As<int>();
MP_EXPECT_OK(p.Consume());
}
TEST(PacketTest, ToOldPacket) {
auto p = MakePacket<int>(7);
mediapipe::Packet op = ToOldPacket(p);
EXPECT_EQ(op.Get<int>(), 7);
EXPECT_EQ(p.Get(), 7);
}
TEST(PacketTest, ToOldPacketConsume) {
auto p = MakePacket<int>(7);
mediapipe::Packet op = ToOldPacket(std::move(p));
MP_EXPECT_OK(op.Consume<int>());
}
TEST(PacketTest, OldRefCounting) {
@@ -190,6 +206,42 @@ TEST(PacketTest, OldRefCounting) {
EXPECT_FALSE(alive);
}
TEST(PacketTest, Consume) {
auto p = MakePacket<int>(7);
auto maybe_int = p.Consume();
EXPECT_TRUE(p.IsEmpty());
ASSERT_TRUE(maybe_int.ok());
EXPECT_EQ(*maybe_int.value(), 7);
p = MakePacket<int>(3);
auto p2 = p;
maybe_int = p.Consume();
EXPECT_FALSE(maybe_int.ok());
EXPECT_FALSE(p.IsEmpty());
EXPECT_FALSE(p2.IsEmpty());
}
TEST(PacketTest, OneOfConsume) {
Packet<OneOf<std::string, int>> p = MakePacket<std::string>("hi");
EXPECT_TRUE(p.Has<std::string>());
EXPECT_FALSE(p.Has<int>());
EXPECT_EQ(p.Get<std::string>(), "hi");
absl::StatusOr<std::string> out = p.ConsumeAndVisit(
[](std::unique_ptr<std::string> s) {
return absl::StrCat("string: ", *s);
},
[](std::unique_ptr<int> i) { return absl::StrCat("int: ", *i); });
MP_EXPECT_OK(out);
EXPECT_EQ(out.value(), "string: hi");
EXPECT_TRUE(p.IsEmpty());
p = MakePacket<int>(3);
absl::Status out2 = p.ConsumeAndVisit([](std::unique_ptr<std::string> s) {},
[](std::unique_ptr<int> i) {});
MP_EXPECT_OK(out2);
EXPECT_TRUE(p.IsEmpty());
}
} // namespace
} // namespace api2
} // namespace mediapipe
+93 -15
View File
@@ -179,7 +179,7 @@ inline void SetType(CalculatorContract* cc, PacketType& pt) {
template <typename ValueT>
InputShardAccess<ValueT> SinglePortAccess(mediapipe::CalculatorContext* cc,
const InputStreamShard* stream) {
InputStreamShard* stream) {
return InputShardAccess<ValueT>(*cc, stream);
}
@@ -203,7 +203,7 @@ OutputSidePacketAccess<ValueT> SinglePortAccess(
template <typename ValueT>
InputShardOrSideAccess<ValueT> SinglePortAccess(
mediapipe::CalculatorContext* cc, const InputStreamShard* stream,
mediapipe::CalculatorContext* cc, InputStreamShard* stream,
const mediapipe::Packet* packet) {
return InputShardOrSideAccess<ValueT>(*cc, stream, packet);
}
@@ -226,19 +226,50 @@ auto AccessPort(std::false_type, const PortT& port, CC* cc) {
template <typename ValueT, typename X, class CC>
class MultiplePortAccess {
public:
using AccessT = decltype(SinglePortAccess<ValueT>(std::declval<CC*>(),
std::declval<X*>()));
MultiplePortAccess(CC* cc, X* first, int count)
: cc_(cc), first_(first), count_(count) {}
// TODO: maybe this should be size(), like in a standard C++
// container?
int Count() { return count_; }
auto operator[](int pos) {
AccessT operator[](int pos) {
CHECK_GE(pos, 0);
CHECK_LT(pos, count_);
return SinglePortAccess<ValueT>(cc_, &first_[pos]);
}
// TODO: add begin/end.
class Iterator {
public:
using iterator_category = std::input_iterator_tag;
using value_type = AccessT;
using difference_type = std::ptrdiff_t;
using pointer = AccessT*;
using reference = AccessT; // allowed; see e.g. std::istreambuf_iterator
Iterator(CC* cc, X* p) : cc_(cc), p_(p) {}
Iterator& operator++() {
++p_;
return *this;
}
Iterator operator++(int) {
Iterator res = *this;
++(*this);
return res;
}
bool operator==(const Iterator& other) const { return p_ == other.p_; }
bool operator!=(const Iterator& other) const { return !(*this == other); }
AccessT operator*() const { return SinglePortAccess<ValueT>(cc_, p_); }
private:
CC* cc_;
X* p_;
};
Iterator begin() { return Iterator(cc_, first_); }
Iterator end() { return Iterator(cc_, first_ + count_); }
private:
CC* cc_;
@@ -307,7 +338,7 @@ class PortCommon : public Base {
}
private:
mediapipe::Status AddToContract(CalculatorContract* cc) const {
absl::Status AddToContract(CalculatorContract* cc) const {
if (kMultiple) {
AddMultiple(cc);
} else {
@@ -385,17 +416,17 @@ class SideFallbackT : public Base {
side_port(tag) {}
protected:
mediapipe::Status AddToContract(CalculatorContract* cc) const {
absl::Status AddToContract(CalculatorContract* cc) const {
stream_port.AddToContract(cc);
side_port.AddToContract(cc);
int connected_count =
stream_port(cc).IsConnected() + side_port(cc).IsConnected();
if (connected_count > 1)
return mediapipe::InvalidArgumentError(absl::StrCat(
return absl::InvalidArgumentError(absl::StrCat(
Tag(),
" can be connected as a stream or as a side packet, but not both"));
if (!IsOptionalV && connected_count == 0)
return mediapipe::InvalidArgumentError(
return absl::InvalidArgumentError(
absl::StrCat(Tag(), " must be connected"));
return {};
}
@@ -452,6 +483,14 @@ class OutputShardAccess : public OutputShardAccessBase {
void Send(const T& payload) { Send(payload, context_.InputTimestamp()); }
void Send(T&& payload, Timestamp time) {
Send(api2::MakePacket<T>(std::move(payload)).At(time));
}
void Send(T&& payload) {
Send(std::move(payload), context_.InputTimestamp());
}
void Send(std::unique_ptr<T> payload, Timestamp time) {
Send(api2::PacketAdopting(std::move(payload)).At(time));
}
@@ -501,6 +540,7 @@ class OutputSidePacketAccess {
}
void Set(const T& payload) { Set(MakePacket<T>(payload)); }
void Set(T&& payload) { Set(MakePacket<T>(std::move(payload))); }
private:
OutputSidePacketAccess(OutputSidePacket* output) : output_(output) {}
@@ -523,15 +563,54 @@ class InputShardAccess : public Packet<T> {
PacketBase Header() const { return FromOldPacket(stream_->Header()); }
// "Consume" requires exclusive ownership of the packet's payload. In the
// current interim implementation, InputShardAccess creates a new reference to
// the payload (as a Packet<T> instead of a type-erased Packet), which means
// the conditions for Consume would never be satisfied. This helper class
// defines wrappers for the Consume methods in Packet which temporarily erase
// the reference held by the underlying InputStreamShard.
// Note that we cannot simply take over the reference when InputShardAccess is
// created, because it is currently created as a temporary and we might create
// more than one instance for the same stream.
template <class U = T,
class = std::enable_if_t<std::is_same<U, T>{},
decltype(&Packet<U>::Consume)>>
absl::StatusOr<std::unique_ptr<U>> Consume() {
return WrapConsumeCall(&Packet<T>::Consume);
}
template <class V, class U = T,
std::enable_if_t<internal::IsCompatibleType<V, U>{}, int> = 0>
absl::StatusOr<std::unique_ptr<V>> Consume() {
return WrapConsumeCall(&Packet<T>::template Consume<V>);
}
template <class... F>
auto ConsumeAndVisit(F&&... args) {
auto f = &Packet<T>::template ConsumeAndVisit<F...>;
return WrapConsumeCall(f, std::forward<F>(args)...);
}
private:
InputShardAccess(const CalculatorContext&, const InputStreamShard* stream)
InputShardAccess(const CalculatorContext&, InputStreamShard* stream)
: Packet<T>(stream ? FromOldPacket(stream->Value()).template As<T>()
: Packet<T>()),
stream_(stream) {}
const InputStreamShard* stream_;
template <class F, class... A>
auto WrapConsumeCall(F f, A&&... args) {
stream_->Value() = {};
auto result = (this->*f)(std::forward<A>(args)...);
if (!result.ok()) {
stream_->Value() = ToOldPacket(*this);
}
return result;
}
InputStreamShard* stream_;
friend InputShardAccess<T> internal::SinglePortAccess<T>(
mediapipe::CalculatorContext*, const InputStreamShard*);
mediapipe::CalculatorContext*, InputStreamShard*);
};
template <typename T>
@@ -566,19 +645,18 @@ class InputShardOrSideAccess : public Packet<T> {
PacketBase Header() const { return FromOldPacket(stream_->Header()); }
private:
InputShardOrSideAccess(const CalculatorContext&,
const InputStreamShard* stream,
InputShardOrSideAccess(const CalculatorContext&, InputStreamShard* stream,
const mediapipe::Packet* packet)
: Packet<T>(stream ? FromOldPacket(stream->Value()).template As<T>()
: packet ? FromOldPacket(*packet).template As<T>()
: Packet<T>()),
stream_(stream),
connected_(stream_ != nullptr || packet != nullptr) {}
const InputStreamShard* stream_;
InputStreamShard* stream_;
bool connected_;
friend InputShardOrSideAccess<T> internal::SinglePortAccess<T>(
mediapipe::CalculatorContext*, const InputStreamShard*,
mediapipe::CalculatorContext*, InputStreamShard*,
const mediapipe::Packet*);
};
+4 -4
View File
@@ -17,7 +17,7 @@ namespace test {
class FooBarImpl1 : public SubgraphImpl<FooBar1, FooBarImpl1> {
public:
mediapipe::StatusOr<CalculatorGraphConfig> GetConfig(
absl::StatusOr<CalculatorGraphConfig> GetConfig(
const SubgraphOptions& /*options*/) {
builder::Graph graph;
auto& foo = graph.AddNode("Foo");
@@ -31,7 +31,7 @@ class FooBarImpl1 : public SubgraphImpl<FooBar1, FooBarImpl1> {
class FooBarImpl2 : public SubgraphImpl<FooBar2, FooBarImpl2> {
public:
mediapipe::StatusOr<CalculatorGraphConfig> GetConfig(
absl::StatusOr<CalculatorGraphConfig> GetConfig(
const SubgraphOptions& /*options*/) {
builder::Graph graph;
auto& foo = graph.AddNode<Foo>();
@@ -44,7 +44,7 @@ class FooBarImpl2 : public SubgraphImpl<FooBar2, FooBarImpl2> {
};
TEST(SubgraphTest, SubgraphConfig) {
CalculatorGraphConfig subgraph = FooBarImpl1().GetConfig({}).ValueOrDie();
CalculatorGraphConfig subgraph = FooBarImpl1().GetConfig({}).value();
const CalculatorGraphConfig expected_graph =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
input_stream: "IN:__stream_0"
@@ -64,7 +64,7 @@ TEST(SubgraphTest, SubgraphConfig) {
}
TEST(SubgraphTest, TypedSubgraphConfig) {
CalculatorGraphConfig subgraph = FooBarImpl2().GetConfig({}).ValueOrDie();
CalculatorGraphConfig subgraph = FooBarImpl2().GetConfig({}).value();
const CalculatorGraphConfig expected_graph =
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
input_stream: "IN:__stream_0"
@@ -9,6 +9,11 @@
mediapipe::type_map_internal::ReflectType<void(type*)>::Type, #type, \
nullptr, nullptr)
#define MEDIAPIPE_REGISTER_GENERIC_TYPE_WITH_NAME(type, name) \
MEDIAPIPE_REGISTER_TYPE( \
mediapipe::type_map_internal::ReflectType<void(type*)>::Type, name, \
nullptr, nullptr)
// Note: we cannot define a type which type hash id is already in the map.
// E.g. if tool::GetTypeHash<int>() == tool::GetTypeHash<int32>(), then only one
// can be registered.
@@ -26,3 +31,4 @@ MEDIAPIPE_REGISTER_GENERIC_TYPE(::std::vector<int>);
MEDIAPIPE_REGISTER_GENERIC_TYPE(::std::vector<int64>);
MEDIAPIPE_REGISTER_GENERIC_TYPE(::std::vector<std::string>);
MEDIAPIPE_REGISTER_GENERIC_TYPE(::std::vector<::std::vector<float>>);
MEDIAPIPE_REGISTER_GENERIC_TYPE_WITH_NAME(::std::string, "string");
+1 -1
View File
@@ -351,7 +351,7 @@ message CalculatorGraphConfig {
int32 num_threads = 8;
// Configs for StatusHandlers that will be called after each call to
// Run() on the graph. StatusHandlers take zero or more input side
// packets and the ::util::Status returned by a graph run. For example,
// packets and the absl::Status returned by a graph run. For example,
// a StatusHandler could store information about graph failures and
// their causes for later monitoring. Note that graph failures during
// initialization may cause required input side packets (created by a
+12 -16
View File
@@ -82,7 +82,7 @@ class CalculatorBase {
// this function is static the registration macro provides access to
// each subclass' GetContract function.
//
// static mediapipe::Status GetContract(CalculatorContract* cc);
// static absl::Status GetContract(CalculatorContract* cc);
//
// GetContract fills in the calculator's contract with the framework, such
// as its expectations of what packets it will receive. When this function
@@ -116,23 +116,21 @@ class CalculatorBase {
// Open is called before any Process() calls, on a freshly constructed
// calculator. Subclasses may override this method to perform necessary
// setup, and possibly output Packets and/or set output streams' headers.
// Must return mediapipe::OkStatus() to indicate success. On failure any
// Must return absl::OkStatus() to indicate success. On failure any
// other status code can be returned. If failure is returned then the
// framework will call neither Process() nor Close() on the calculator (so any
// necessary cleanup should be done before returning failure or in the
// destructor).
virtual mediapipe::Status Open(CalculatorContext* cc) {
return mediapipe::OkStatus();
}
virtual absl::Status Open(CalculatorContext* cc) { return absl::OkStatus(); }
// Processes the incoming inputs. May call the methods on cc to access
// inputs and produce outputs.
//
// Process() called on a non-source node must return
// mediapipe::OkStatus() to indicate that all went well, or any other
// absl::OkStatus() to indicate that all went well, or any other
// status code to signal an error.
// For example:
// mediapipe::UnknownError("Failure Message");
// absl::UnknownError("Failure Message");
// Notice the convenience functions in util/task/canonical_errors.h .
// If a non-source Calculator returns tool::StatusStop(), then this
// signals the graph is being cancelled early. In this case, all
@@ -140,23 +138,21 @@ class CalculatorBase {
// remaining Packets will propagate through the graph).
//
// A source node will continue to have Process() called on it as long
// as it returns mediapipe::OkStatus(). To indicate that there is
// as it returns absl::OkStatus(). To indicate that there is
// no more data to be generated return tool::StatusStop(). Any other
// status indicates an error has occurred.
virtual mediapipe::Status Process(CalculatorContext* cc) = 0;
virtual absl::Status Process(CalculatorContext* cc) = 0;
// Is called if Open() was called and succeeded. Is called either
// immediately after processing is complete or after a graph run has ended
// (if an error occurred in the graph). Must return mediapipe::OkStatus()
// (if an error occurred in the graph). Must return absl::OkStatus()
// to indicate success. On failure any other status code can be returned.
// Packets may be output during a call to Close(). However, output packets
// are silently discarded if Close() is called after a graph run has ended.
//
// NOTE: If Close() needs to perform an action only when processing is
// complete, Close() must check if cc->GraphStatus() is OK.
virtual mediapipe::Status Close(CalculatorContext* cc) {
return mediapipe::OkStatus();
}
virtual absl::Status Close(CalculatorContext* cc) { return absl::OkStatus(); }
// Returns a value according to which the framework selects
// the next source calculator to Process(); smaller value means
@@ -180,7 +176,7 @@ namespace internal {
class CalculatorBaseFactory {
public:
virtual ~CalculatorBaseFactory() {}
virtual mediapipe::Status GetContract(CalculatorContract* cc) = 0;
virtual absl::Status GetContract(CalculatorContract* cc) = 0;
virtual std::unique_ptr<CalculatorBase> CreateCalculator(
CalculatorContext* calculator_context) = 0;
virtual std::string ContractMethodName() { return "GetContract"; }
@@ -189,7 +185,7 @@ class CalculatorBaseFactory {
// Functions for checking that the calculator has the required GetContract.
template <class T>
constexpr bool CalculatorHasGetContract(decltype(&T::GetContract) /*unused*/) {
typedef mediapipe::Status (*GetContractType)(CalculatorContract * cc);
typedef absl::Status (*GetContractType)(CalculatorContract * cc);
return std::is_same<decltype(&T::GetContract), GetContractType>::value;
}
template <class T>
@@ -219,7 +215,7 @@ class CalculatorBaseFactoryFor<
// Provides access to the static function GetContract within a specific
// subclass of CalculatorBase.
mediapipe::Status GetContract(CalculatorContract* cc) final {
absl::Status GetContract(CalculatorContract* cc) final {
// CalculatorBaseSubclass must implement this function, since it is not
// implemented in the parent class.
return T::GetContract(cc);
+20 -26
View File
@@ -41,7 +41,7 @@ namespace test_ns {
// streams and input side packets.
class DeadEndCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).SetAny();
}
@@ -51,16 +51,14 @@ class DeadEndCalculator : public CalculatorBase {
for (int i = 0; i < cc->InputSidePackets().NumEntries(); ++i) {
cc->InputSidePackets().Index(i).SetAny();
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) override { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
if (cc->Inputs().NumEntries() > 0) {
return mediapipe::OkStatus();
return absl::OkStatus();
} else {
// This is a source calculator, but we don't produce any outputs.
return tool::StatusStop();
@@ -73,14 +71,12 @@ namespace whitelisted_ns {
class DeadCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
return mediapipe::OkStatus();
static absl::Status GetContract(CalculatorContract* cc) {
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
return mediapipe::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
return mediapipe::OkStatus();
absl::Status Open(CalculatorContext* cc) override { return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override {
return absl::OkStatus();
}
};
@@ -89,14 +85,12 @@ class DeadCalculator : public CalculatorBase {
class EndCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
return mediapipe::OkStatus();
static absl::Status GetContract(CalculatorContract* cc) {
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
return mediapipe::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
return mediapipe::OkStatus();
absl::Status Open(CalculatorContext* cc) override { return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override {
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(::mediapipe::EndCalculator);
@@ -105,7 +99,7 @@ namespace {
TEST(CalculatorTest, SourceProcessOrder) {
internal::Collection<OutputStreamManager> output_stream_managers(
tool::CreateTagMap(2).ValueOrDie());
tool::CreateTagMap(2).value());
PacketType output0_type;
PacketType output1_type;
@@ -117,7 +111,7 @@ TEST(CalculatorTest, SourceProcessOrder) {
MP_ASSERT_OK(
output_stream_managers.Index(1).Initialize("output1", &output1_type));
PacketSet input_side_packets(tool::CreateTagMap({}).ValueOrDie());
PacketSet input_side_packets(tool::CreateTagMap({}).value());
CalculatorState calculator_state("Node", /*node_id=*/0, "Calculator",
CalculatorGraphConfig::Node(), nullptr);
@@ -126,7 +120,7 @@ TEST(CalculatorTest, SourceProcessOrder) {
CalculatorContextManager calculator_context_manager;
CalculatorContext calculator_context(&calculator_state,
tool::CreateTagMap({}).ValueOrDie(),
tool::CreateTagMap({}).value(),
output_stream_managers.TagMap());
OutputStreamShardSet& output_set = calculator_context.Outputs();
output_set.Index(0).SetSpec(output_stream_managers.Index(0).Spec());
@@ -167,13 +161,13 @@ TEST(CalculatorTest, CreateByName) {
"mediapipe", "DeadEndCalculator")
.status()
.code(),
mediapipe::StatusCode::kNotFound);
absl::StatusCode::kNotFound);
EXPECT_EQ(CalculatorBaseRegistry::CreateByName( //
"DeadEndCalculator")
.status()
.code(),
mediapipe::StatusCode::kNotFound);
absl::StatusCode::kNotFound);
}
// Tests registration of a calculator within a whitelisted namespace.
@@ -41,6 +41,11 @@ Counter* CalculatorContext::GetCounter(const std::string& name) {
return calculator_state_->GetCounter(name);
}
CounterSet* CalculatorContext::GetCounterSet() {
CHECK(calculator_state_);
return calculator_state_->GetCounterSet();
}
const PacketSet& CalculatorContext::InputSidePackets() const {
return calculator_state_->InputSidePackets();
}
+7 -5
View File
@@ -74,6 +74,10 @@ class CalculatorContext {
// the calculator's type (if not).
Counter* GetCounter(const std::string& name);
// Returns the counter set, which can be used to create new counters.
// No prefix is added to counters created in this way.
CounterSet* GetCounterSet();
// Returns the current input timestamp, or Timestamp::Unset if there are
// no input packets.
Timestamp InputTimestamp() const {
@@ -103,7 +107,7 @@ class CalculatorContext {
// Returns the status of the graph run.
//
// NOTE: This method should only be called during CalculatorBase::Close().
mediapipe::Status GraphStatus() const { return graph_status_; }
absl::Status GraphStatus() const { return graph_status_; }
ProfilingContext* GetProfilingContext() const {
return calculator_state_->GetSharedProfilingContext().get();
@@ -148,9 +152,7 @@ class CalculatorContext {
input_timestamps_.pop();
}
void SetGraphStatus(const mediapipe::Status& status) {
graph_status_ = status;
}
void SetGraphStatus(const absl::Status& status) { graph_status_ = status; }
// Interface for the friend class Calculator.
const InputStreamSet& InputStreams() const;
@@ -171,7 +173,7 @@ class CalculatorContext {
std::queue<Timestamp> input_timestamps_;
// The status of the graph run. Only used when Close() is called.
mediapipe::Status graph_status_;
absl::Status graph_status_;
// Accesses CalculatorContext for setting input timestamp.
friend class CalculatorContextManager;
@@ -34,9 +34,8 @@ void CalculatorContextManager::Initialize(
calculator_run_in_parallel_ = calculator_run_in_parallel;
}
mediapipe::Status CalculatorContextManager::PrepareForRun(
std::function<mediapipe::Status(CalculatorContext*)>
setup_shards_callback) {
absl::Status CalculatorContextManager::PrepareForRun(
std::function<absl::Status(CalculatorContext*)> setup_shards_callback) {
setup_shards_callback_ = std::move(setup_shards_callback);
default_context_ = absl::make_unique<CalculatorContext>(
calculator_state_, input_tag_map_, output_tag_map_);
@@ -45,9 +45,8 @@ class CalculatorContextManager {
// Sets the callback that can setup the input and output stream shards in a
// newly constructed calculator context. Then, initializes the default
// calculator context.
mediapipe::Status PrepareForRun(
std::function<mediapipe::Status(CalculatorContext*)>
setup_shards_callback);
absl::Status PrepareForRun(
std::function<absl::Status(CalculatorContext*)> setup_shards_callback);
// Invoked by CalculatorNode::CleanupAfterRun().
void CleanupAfterRun() ABSL_LOCKS_EXCLUDED(contexts_mutex_);
@@ -108,7 +107,7 @@ class CalculatorContextManager {
}
void SetGraphStatusInContext(CalculatorContext* calculator_context,
const mediapipe::Status& status) {
const absl::Status& status) {
CHECK(calculator_context);
calculator_context->SetGraphStatus(status);
}
@@ -124,7 +123,7 @@ class CalculatorContextManager {
// NOTE: This callback invokes input/output stream handler methods.
// The callback is used to break the circular dependency between
// calculator context manager and input/output stream handlers.
std::function<mediapipe::Status(CalculatorContext*)> setup_shards_callback_;
std::function<absl::Status(CalculatorContext*)> setup_shards_callback_;
// The default calculator context that is always reused for sequential
// execution. It is also used by Open() and Close() method of a parallel
@@ -99,9 +99,9 @@ std::unique_ptr<CalculatorState> MakeCalculatorState(
std::unique_ptr<CalculatorContext> MakeCalculatorContext(
CalculatorState* calculator_state) {
return absl::make_unique<CalculatorContext>(
calculator_state, tool::CreateTagMap({}).ValueOrDie(),
tool::CreateTagMap({}).ValueOrDie());
return absl::make_unique<CalculatorContext>(calculator_state,
tool::CreateTagMap({}).value(),
tool::CreateTagMap({}).value());
}
TEST(CalculatorTest, NodeId) {
+16 -18
View File
@@ -24,9 +24,9 @@
namespace mediapipe {
mediapipe::Status CalculatorContract::Initialize(
absl::Status CalculatorContract::Initialize(
const CalculatorGraphConfig::Node& node) {
std::vector<mediapipe::Status> statuses;
std::vector<absl::Status> statuses;
auto input_stream_statusor = tool::TagMap::Create(node.input_stream());
if (!input_stream_statusor.ok()) {
@@ -64,19 +64,18 @@ mediapipe::Status CalculatorContract::Initialize(
options_.Initialize(*node_config_);
// Create the PacketTypeSets.
inputs_ = absl::make_unique<PacketTypeSet>(
std::move(input_stream_statusor).ValueOrDie());
std::move(input_stream_statusor).value());
outputs_ = absl::make_unique<PacketTypeSet>(
std::move(output_stream_statusor).ValueOrDie());
std::move(output_stream_statusor).value());
input_side_packets_ = absl::make_unique<PacketTypeSet>(
std::move(input_side_packet_statusor).ValueOrDie());
std::move(input_side_packet_statusor).value());
output_side_packets_ = absl::make_unique<PacketTypeSet>(
std::move(output_side_packet_statusor).ValueOrDie());
return mediapipe::OkStatus();
std::move(output_side_packet_statusor).value());
return absl::OkStatus();
}
mediapipe::Status CalculatorContract::Initialize(
const PacketGeneratorConfig& node) {
std::vector<mediapipe::Status> statuses;
absl::Status CalculatorContract::Initialize(const PacketGeneratorConfig& node) {
std::vector<absl::Status> statuses;
auto input_side_packet_statusor =
tool::TagMap::Create(node.input_side_packet());
@@ -103,15 +102,14 @@ mediapipe::Status CalculatorContract::Initialize(
}
input_side_packets_ = absl::make_unique<PacketTypeSet>(
std::move(input_side_packet_statusor).ValueOrDie());
std::move(input_side_packet_statusor).value());
output_side_packets_ = absl::make_unique<PacketTypeSet>(
std::move(output_side_packet_statusor).ValueOrDie());
return mediapipe::OkStatus();
std::move(output_side_packet_statusor).value());
return absl::OkStatus();
}
mediapipe::Status CalculatorContract::Initialize(
const StatusHandlerConfig& node) {
std::vector<mediapipe::Status> statuses;
absl::Status CalculatorContract::Initialize(const StatusHandlerConfig& node) {
std::vector<absl::Status> statuses;
auto input_side_packet_statusor =
tool::TagMap::Create(node.input_side_packet());
@@ -133,8 +131,8 @@ mediapipe::Status CalculatorContract::Initialize(
}
input_side_packets_ = absl::make_unique<PacketTypeSet>(
std::move(input_side_packet_statusor).ValueOrDie());
return mediapipe::OkStatus();
std::move(input_side_packet_statusor).value());
return absl::OkStatus();
}
} // namespace mediapipe
+3 -3
View File
@@ -47,9 +47,9 @@ namespace mediapipe {
//
class CalculatorContract {
public:
mediapipe::Status Initialize(const CalculatorGraphConfig::Node& node);
mediapipe::Status Initialize(const PacketGeneratorConfig& node);
mediapipe::Status Initialize(const StatusHandlerConfig& node);
absl::Status Initialize(const CalculatorGraphConfig::Node& node);
absl::Status Initialize(const PacketGeneratorConfig& node);
absl::Status Initialize(const StatusHandlerConfig& node);
void SetNodeName(const std::string& node_name) { node_name_ = node_name; }
// Returns the options given to this node.
+89 -90
View File
@@ -62,9 +62,9 @@
#include "mediapipe/framework/validated_graph_config.h"
#include "mediapipe/gpu/graph_support.h"
#include "mediapipe/util/cpu_util.h"
#ifndef MEDIAPIPE_DISABLE_GPU
#if !MEDIAPIPE_DISABLE_GPU
#include "mediapipe/gpu/gpu_shared_data_internal.h"
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
@@ -129,13 +129,13 @@ CalculatorGraph::CalculatorGraph(const CalculatorGraphConfig& config)
// instantiated.
CalculatorGraph::~CalculatorGraph() {
// Stop periodic profiler output to ublock Executor destructors.
mediapipe::Status status = profiler()->Stop();
absl::Status status = profiler()->Stop();
if (!status.ok()) {
LOG(ERROR) << "During graph destruction: " << status;
}
}
mediapipe::Status CalculatorGraph::InitializePacketGeneratorGraph(
absl::Status CalculatorGraph::InitializePacketGeneratorGraph(
const std::map<std::string, Packet>& side_packets) {
// Create and initialize the output side packets.
if (!validated_graph_->OutputSidePacketInfos().empty()) {
@@ -164,7 +164,7 @@ mediapipe::Status CalculatorGraph::InitializePacketGeneratorGraph(
default_executor, side_packets);
}
mediapipe::Status CalculatorGraph::InitializeStreams() {
absl::Status CalculatorGraph::InitializeStreams() {
any_packet_type_.SetAny();
// Create and initialize the input streams.
@@ -221,16 +221,16 @@ mediapipe::Status CalculatorGraph::InitializeStreams() {
graph_input_stream_add_mode_ = GraphInputStreamAddMode::WAIT_TILL_NOT_FULL;
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::InitializeCalculatorNodes() {
absl::Status CalculatorGraph::InitializeCalculatorNodes() {
// Check if the user has specified a maximum queue size for an input stream.
max_queue_size_ = validated_graph_->Config().max_queue_size();
max_queue_size_ = max_queue_size_ ? max_queue_size_ : 100;
// Use a local variable to avoid needing to lock errors_.
std::vector<mediapipe::Status> errors;
std::vector<absl::Status> errors;
// Create and initialize all the nodes in the graph.
nodes_ = absl::make_unique<absl::FixedArray<CalculatorNode>>(
@@ -240,7 +240,7 @@ mediapipe::Status CalculatorGraph::InitializeCalculatorNodes() {
// buffer_size_hint will be positive if one was specified in
// the graph proto.
int buffer_size_hint = 0;
const mediapipe::Status result = (*nodes_)[node_id].Initialize(
const absl::Status result = (*nodes_)[node_id].Initialize(
validated_graph_.get(), node_id, input_stream_managers_.get(),
output_stream_managers_.get(), output_side_packets_.get(),
&buffer_size_hint, profiler_);
@@ -259,15 +259,15 @@ mediapipe::Status CalculatorGraph::InitializeCalculatorNodes() {
VLOG(2) << "Maximum input stream queue size based on graph config: "
<< max_queue_size_;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::InitializeProfiler() {
absl::Status CalculatorGraph::InitializeProfiler() {
profiler_->Initialize(*validated_graph_);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::InitializeExecutors() {
absl::Status CalculatorGraph::InitializeExecutors() {
// If the ExecutorConfig for the default executor leaves the executor type
// unspecified, default_executor_options points to the
// ThreadPoolExecutorOptions in that ExecutorConfig. Otherwise,
@@ -324,10 +324,10 @@ mediapipe::Status CalculatorGraph::InitializeExecutors() {
use_application_thread));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::InitializeDefaultExecutor(
absl::Status CalculatorGraph::InitializeDefaultExecutor(
const ThreadPoolExecutorOptions* default_executor_options,
bool use_application_thread) {
#ifdef __EMSCRIPTEN__
@@ -340,7 +340,7 @@ mediapipe::Status CalculatorGraph::InitializeDefaultExecutor(
"", std::make_shared<internal::DelegatingExecutor>(
std::bind(&internal::Scheduler::AddApplicationThreadTask,
&scheduler_, std::placeholders::_1))));
return mediapipe::OkStatus();
return absl::OkStatus();
}
// Check the number of threads specified in the proto.
@@ -359,10 +359,10 @@ mediapipe::Status CalculatorGraph::InitializeDefaultExecutor(
}
MP_RETURN_IF_ERROR(
CreateDefaultThreadPool(default_executor_options, num_threads));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::Initialize(
absl::Status CalculatorGraph::Initialize(
std::unique_ptr<ValidatedGraphConfig> validated_graph,
const std::map<std::string, Packet>& side_packets) {
RET_CHECK(!initialized_).SetNoLogging()
@@ -380,15 +380,15 @@ mediapipe::Status CalculatorGraph::Initialize(
#endif
initialized_ = true;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::Initialize(
absl::Status CalculatorGraph::Initialize(
const CalculatorGraphConfig& input_config) {
return Initialize(input_config, {});
}
mediapipe::Status CalculatorGraph::Initialize(
absl::Status CalculatorGraph::Initialize(
const CalculatorGraphConfig& input_config,
const std::map<std::string, Packet>& side_packets) {
auto validated_graph = absl::make_unique<ValidatedGraphConfig>();
@@ -396,7 +396,7 @@ mediapipe::Status CalculatorGraph::Initialize(
return Initialize(std::move(validated_graph), side_packets);
}
mediapipe::Status CalculatorGraph::Initialize(
absl::Status CalculatorGraph::Initialize(
const std::vector<CalculatorGraphConfig>& input_configs,
const std::vector<CalculatorGraphTemplate>& input_templates,
const std::map<std::string, Packet>& side_packets,
@@ -407,9 +407,9 @@ mediapipe::Status CalculatorGraph::Initialize(
return Initialize(std::move(validated_graph), side_packets);
}
mediapipe::Status CalculatorGraph::ObserveOutputStream(
absl::Status CalculatorGraph::ObserveOutputStream(
const std::string& stream_name,
std::function<mediapipe::Status(const Packet&)> packet_callback) {
std::function<absl::Status(const Packet&)> packet_callback) {
RET_CHECK(initialized_).SetNoLogging()
<< "CalculatorGraph is not initialized.";
// TODO Allow output observers to be attached by graph level
@@ -425,10 +425,10 @@ mediapipe::Status CalculatorGraph::ObserveOutputStream(
stream_name, &any_packet_type_, std::move(packet_callback),
&output_stream_managers_[output_stream_index]));
graph_output_streams_.push_back(std::move(observer));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::StatusOr<OutputStreamPoller> CalculatorGraph::AddOutputStreamPoller(
absl::StatusOr<OutputStreamPoller> CalculatorGraph::AddOutputStreamPoller(
const std::string& stream_name) {
RET_CHECK(initialized_).SetNoLogging()
<< "CalculatorGraph is not initialized.";
@@ -449,7 +449,7 @@ mediapipe::StatusOr<OutputStreamPoller> CalculatorGraph::AddOutputStreamPoller(
return std::move(poller);
}
mediapipe::StatusOr<Packet> CalculatorGraph::GetOutputSidePacket(
absl::StatusOr<Packet> CalculatorGraph::GetOutputSidePacket(
const std::string& packet_name) {
int side_packet_index = validated_graph_->OutputSidePacketIndex(packet_name);
if (side_packet_index < 0) {
@@ -486,7 +486,7 @@ mediapipe::StatusOr<Packet> CalculatorGraph::GetOutputSidePacket(
return output_packet;
}
mediapipe::Status CalculatorGraph::Run(
absl::Status CalculatorGraph::Run(
const std::map<std::string, Packet>& extra_side_packets) {
RET_CHECK(graph_input_streams_.empty()).SetNoLogging()
<< "When using graph input streams, call StartRun() instead of Run() so "
@@ -495,7 +495,7 @@ mediapipe::Status CalculatorGraph::Run(
return WaitUntilDone();
}
mediapipe::Status CalculatorGraph::StartRun(
absl::Status CalculatorGraph::StartRun(
const std::map<std::string, Packet>& extra_side_packets,
const std::map<std::string, Packet>& stream_headers) {
RET_CHECK(initialized_).SetNoLogging()
@@ -503,18 +503,18 @@ mediapipe::Status CalculatorGraph::StartRun(
MP_RETURN_IF_ERROR(PrepareForRun(extra_side_packets, stream_headers));
MP_RETURN_IF_ERROR(profiler_->Start(executors_[""].get()));
scheduler_.Start();
return mediapipe::OkStatus();
return absl::OkStatus();
}
#ifndef MEDIAPIPE_DISABLE_GPU
mediapipe::Status CalculatorGraph::SetGpuResources(
#if !MEDIAPIPE_DISABLE_GPU
absl::Status CalculatorGraph::SetGpuResources(
std::shared_ptr<::mediapipe::GpuResources> resources) {
RET_CHECK(!ContainsKey(service_packets_, kGpuService.key))
<< "The GPU resources have already been configured.";
service_packets_[kGpuService.key] =
MakePacket<std::shared_ptr<::mediapipe::GpuResources>>(
std::move(resources));
return mediapipe::OkStatus();
return absl::OkStatus();
}
std::shared_ptr<::mediapipe::GpuResources> CalculatorGraph::GetGpuResources()
@@ -524,7 +524,7 @@ std::shared_ptr<::mediapipe::GpuResources> CalculatorGraph::GetGpuResources()
return service_iter->second.Get<std::shared_ptr<::mediapipe::GpuResources>>();
}
mediapipe::StatusOr<std::map<std::string, Packet>> CalculatorGraph::PrepareGpu(
absl::StatusOr<std::map<std::string, Packet>> CalculatorGraph::PrepareGpu(
const std::map<std::string, Packet>& side_packets) {
std::map<std::string, Packet> additional_side_packets;
bool update_sp = false;
@@ -588,9 +588,9 @@ mediapipe::StatusOr<std::map<std::string, Packet>> CalculatorGraph::PrepareGpu(
}
return additional_side_packets;
}
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
mediapipe::Status CalculatorGraph::PrepareForRun(
absl::Status CalculatorGraph::PrepareForRun(
const std::map<std::string, Packet>& extra_side_packets,
const std::map<std::string, Packet>& stream_headers) {
if (VLOG_IS_ON(1)) {
@@ -607,9 +607,9 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
num_closed_graph_input_streams_ = 0;
std::map<std::string, Packet> additional_side_packets;
#ifndef MEDIAPIPE_DISABLE_GPU
#if !MEDIAPIPE_DISABLE_GPU
ASSIGN_OR_RETURN(additional_side_packets, PrepareGpu(extra_side_packets));
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
const std::map<std::string, Packet>* input_side_packets;
if (!additional_side_packets.empty()) {
@@ -621,7 +621,7 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
}
current_run_side_packets_.clear();
mediapipe::Status generator_status = packet_generator_graph_.RunGraphSetup(
absl::Status generator_status = packet_generator_graph_.RunGraphSetup(
*input_side_packets, &current_run_side_packets_);
CallStatusHandlers(GraphRunState::PRE_RUN, generator_status);
@@ -632,7 +632,7 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
// If there was an error on the CallStatusHandlers (PRE_RUN), it was stored
// in the error list. We return immediately notifying this to the caller.
mediapipe::Status error_status;
absl::Status error_status;
if (has_error_) {
GetCombinedErrors(&error_status);
LOG(ERROR) << error_status;
@@ -682,7 +682,7 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
std::placeholders::_1, std::placeholders::_2);
node.SetQueueSizeCallbacks(queue_size_callback, queue_size_callback);
scheduler_.AssignNodeToSchedulerQueue(&node);
const mediapipe::Status result = node.PrepareForRun(
const absl::Status result = node.PrepareForRun(
current_run_side_packets_, service_packets_,
std::bind(&internal::Scheduler::ScheduleNodeForOpen, &scheduler_,
&node),
@@ -700,13 +700,13 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
for (auto& graph_output_stream : graph_output_streams_) {
graph_output_stream->PrepareForRun(
[&graph_output_stream, this] {
mediapipe::Status status = graph_output_stream->Notify();
absl::Status status = graph_output_stream->Notify();
if (!status.ok()) {
RecordError(status);
}
scheduler_.EmittedObservedOutput();
},
[this](mediapipe::Status status) { RecordError(status); });
[this](absl::Status status) { RecordError(status); });
}
if (GetCombinedErrors(&error_status)) {
@@ -759,20 +759,20 @@ mediapipe::Status CalculatorGraph::PrepareForRun(
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::WaitUntilIdle() {
absl::Status CalculatorGraph::WaitUntilIdle() {
MP_RETURN_IF_ERROR(scheduler_.WaitUntilIdle());
VLOG(2) << "Scheduler idle.";
mediapipe::Status status = mediapipe::OkStatus();
absl::Status status = absl::OkStatus();
if (GetCombinedErrors(&status)) {
LOG(ERROR) << status;
}
return status;
}
mediapipe::Status CalculatorGraph::WaitUntilDone() {
absl::Status CalculatorGraph::WaitUntilDone() {
VLOG(2) << "Waiting for scheduler to terminate...";
MP_RETURN_IF_ERROR(scheduler_.WaitUntilDone());
VLOG(2) << "Scheduler terminated.";
@@ -780,16 +780,16 @@ mediapipe::Status CalculatorGraph::WaitUntilDone() {
return FinishRun();
}
mediapipe::Status CalculatorGraph::WaitForObservedOutput() {
absl::Status CalculatorGraph::WaitForObservedOutput() {
return scheduler_.WaitForObservedOutput();
}
mediapipe::Status CalculatorGraph::AddPacketToInputStream(
absl::Status CalculatorGraph::AddPacketToInputStream(
const std::string& stream_name, const Packet& packet) {
return AddPacketToInputStreamInternal(stream_name, packet);
}
mediapipe::Status CalculatorGraph::AddPacketToInputStream(
absl::Status CalculatorGraph::AddPacketToInputStream(
const std::string& stream_name, Packet&& packet) {
return AddPacketToInputStreamInternal(stream_name, std::move(packet));
}
@@ -799,7 +799,7 @@ mediapipe::Status CalculatorGraph::AddPacketToInputStream(
// internal-only templated version. T&& is a forwarding reference here, so
// std::forward will deduce the correct type as we pass along packet.
template <typename T>
mediapipe::Status CalculatorGraph::AddPacketToInputStreamInternal(
absl::Status CalculatorGraph::AddPacketToInputStreamInternal(
const std::string& stream_name, T&& packet) {
std::unique_ptr<GraphInputStream>* stream =
mediapipe::FindOrNull(graph_input_streams_, stream_name);
@@ -814,7 +814,7 @@ mediapipe::Status CalculatorGraph::AddPacketToInputStreamInternal(
if (graph_input_stream_add_mode_ ==
GraphInputStreamAddMode::ADD_IF_NOT_FULL) {
if (has_error_) {
mediapipe::Status error_status;
absl::Status error_status;
GetCombinedErrors("Graph has errors: ", &error_status);
return error_status;
}
@@ -835,7 +835,7 @@ mediapipe::Status CalculatorGraph::AddPacketToInputStreamInternal(
&full_input_streams_mutex_);
}
if (has_error_) {
mediapipe::Status error_status;
absl::Status error_status;
GetCombinedErrors("Graph has errors: ", &error_status);
return error_status;
}
@@ -857,7 +857,7 @@ mediapipe::Status CalculatorGraph::AddPacketToInputStreamInternal(
// because we don't have the lock over the input stream.
(*stream)->AddPacket(std::forward<T>(packet));
if (has_error_) {
mediapipe::Status error_status;
absl::Status error_status;
GetCombinedErrors("Graph has errors: ", &error_status);
return error_status;
}
@@ -869,23 +869,22 @@ mediapipe::Status CalculatorGraph::AddPacketToInputStreamInternal(
// again if the graph is still idle. Unthrottling basically only lets in one
// packet at a time. TODO: add test.
scheduler_.AddedPacketToGraphInputStream();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::SetInputStreamMaxQueueSize(
absl::Status CalculatorGraph::SetInputStreamMaxQueueSize(
const std::string& stream_name, int max_queue_size) {
// graph_input_streams_ has not been filled in yet, so we'll check this when
// it is applied when the graph is started.
graph_input_stream_max_queue_size_[stream_name] = max_queue_size;
return mediapipe::OkStatus();
return absl::OkStatus();
}
bool CalculatorGraph::HasInputStream(const std::string& stream_name) {
return mediapipe::FindOrNull(graph_input_streams_, stream_name) != nullptr;
}
mediapipe::Status CalculatorGraph::CloseInputStream(
const std::string& stream_name) {
absl::Status CalculatorGraph::CloseInputStream(const std::string& stream_name) {
std::unique_ptr<GraphInputStream>* stream =
mediapipe::FindOrNull(graph_input_streams_, stream_name);
RET_CHECK(stream).SetNoLogging() << absl::Substitute(
@@ -896,7 +895,7 @@ mediapipe::Status CalculatorGraph::CloseInputStream(
// threads cannot call CloseInputStream() on the same stream_name at the same
// time.
if ((*stream)->IsClosed()) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
(*stream)->Close();
@@ -905,10 +904,10 @@ mediapipe::Status CalculatorGraph::CloseInputStream(
scheduler_.ClosedAllGraphInputStreams();
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::CloseAllInputStreams() {
absl::Status CalculatorGraph::CloseAllInputStreams() {
for (auto& item : graph_input_streams_) {
item.second->Close();
}
@@ -916,10 +915,10 @@ mediapipe::Status CalculatorGraph::CloseAllInputStreams() {
num_closed_graph_input_streams_ = graph_input_streams_.size();
scheduler_.ClosedAllGraphInputStreams();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::CloseAllPacketSources() {
absl::Status CalculatorGraph::CloseAllPacketSources() {
for (auto& item : graph_input_streams_) {
item.second->Close();
}
@@ -928,10 +927,10 @@ mediapipe::Status CalculatorGraph::CloseAllPacketSources() {
scheduler_.ClosedAllGraphInputStreams();
scheduler_.CloseAllSourceNodes();
return mediapipe::OkStatus();
return absl::OkStatus();
}
void CalculatorGraph::RecordError(const mediapipe::Status& error) {
void CalculatorGraph::RecordError(const absl::Status& error) {
VLOG(2) << "RecordError called with " << error;
{
absl::MutexLock lock(&error_mutex_);
@@ -942,7 +941,7 @@ void CalculatorGraph::RecordError(const mediapipe::Status& error) {
stream->NotifyError();
}
if (errors_.size() > kMaxNumAccumulatedErrors) {
for (const mediapipe::Status& error : errors_) {
for (const absl::Status& error : errors_) {
LOG(ERROR) << error;
}
LOG(FATAL) << "Forcefully aborting to prevent the framework running out "
@@ -951,13 +950,13 @@ void CalculatorGraph::RecordError(const mediapipe::Status& error) {
}
}
bool CalculatorGraph::GetCombinedErrors(mediapipe::Status* error_status) {
bool CalculatorGraph::GetCombinedErrors(absl::Status* error_status) {
return GetCombinedErrors("CalculatorGraph::Run() failed in Run: ",
error_status);
}
bool CalculatorGraph::GetCombinedErrors(const std::string& error_prefix,
mediapipe::Status* error_status) {
absl::Status* error_status) {
absl::MutexLock lock(&error_mutex_);
if (!errors_.empty()) {
*error_status = tool::CombinedStatus(error_prefix, errors_);
@@ -967,7 +966,7 @@ bool CalculatorGraph::GetCombinedErrors(const std::string& error_prefix,
}
void CalculatorGraph::CallStatusHandlers(GraphRunState graph_run_state,
const mediapipe::Status& status) {
const absl::Status& status) {
for (int status_handler_index = 0;
status_handler_index < validated_graph_->Config().status_handler_size();
++status_handler_index) {
@@ -979,7 +978,7 @@ void CalculatorGraph::CallStatusHandlers(GraphRunState graph_run_state,
validated_graph_->StatusHandlerInfos()[status_handler_index];
const PacketTypeSet& packet_type_set =
status_handler_info.InputSidePacketTypes();
mediapipe::StatusOr<std::unique_ptr<PacketSet>> packet_set_statusor =
absl::StatusOr<std::unique_ptr<PacketSet>> packet_set_statusor =
tool::FillPacketSet(packet_type_set, current_run_side_packets_,
nullptr);
if (!packet_set_statusor.ok()) {
@@ -989,18 +988,18 @@ void CalculatorGraph::CallStatusHandlers(GraphRunState graph_run_state,
<< "Skipping run of " << handler_type << ": ");
continue;
}
mediapipe::StatusOr<std::unique_ptr<internal::StaticAccessToStatusHandler>>
absl::StatusOr<std::unique_ptr<internal::StaticAccessToStatusHandler>>
static_access_statusor = internal::StaticAccessToStatusHandlerRegistry::
CreateByNameInNamespace(validated_graph_->Package(), handler_type);
CHECK(static_access_statusor.ok()) << handler_type << " is not registered.";
auto static_access = std::move(static_access_statusor).ValueOrDie();
mediapipe::Status handler_result;
auto static_access = std::move(static_access_statusor).value();
absl::Status handler_result;
if (graph_run_state == GraphRunState::PRE_RUN) {
handler_result = static_access->HandlePreRunStatus(
handler_config.options(), *packet_set_statusor.ValueOrDie(), status);
handler_config.options(), *packet_set_statusor.value(), status);
} else { // POST_RUN
handler_result = static_access->HandleStatus(
handler_config.options(), *packet_set_statusor.ValueOrDie(), status);
handler_config.options(), *packet_set_statusor.value(), status);
}
if (!handler_result.ok()) {
mediapipe::StatusBuilder builder(std::move(handler_result),
@@ -1134,7 +1133,7 @@ bool CalculatorGraph::UnthrottleSources() {
}
for (InputStreamManager* stream : full_streams) {
if (Config().report_deadlock()) {
RecordError(mediapipe::UnavailableError(absl::StrCat(
RecordError(absl::UnavailableError(absl::StrCat(
"Detected a deadlock due to input throttling for: \"", stream->Name(),
"\". All calculators are idle while packet sources remain active "
"and throttled. Consider adjusting \"max_queue_size\" or "
@@ -1163,7 +1162,7 @@ void CalculatorGraph::SetGraphInputStreamAddMode(GraphInputStreamAddMode mode) {
}
void CalculatorGraph::Cancel() {
// TODO This function should return mediapipe::Status.
// TODO This function should return absl::Status.
scheduler_.Cancel();
}
@@ -1171,11 +1170,11 @@ void CalculatorGraph::Pause() { scheduler_.Pause(); }
void CalculatorGraph::Resume() { scheduler_.Resume(); }
mediapipe::Status CalculatorGraph::SetServicePacket(
const GraphServiceBase& service, Packet p) {
absl::Status CalculatorGraph::SetServicePacket(const GraphServiceBase& service,
Packet p) {
// TODO: check that the graph has not been started!
service_packets_[service.key] = std::move(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}
Packet CalculatorGraph::GetServicePacket(const GraphServiceBase& service) {
@@ -1186,7 +1185,7 @@ Packet CalculatorGraph::GetServicePacket(const GraphServiceBase& service) {
return it->second;
}
mediapipe::Status CalculatorGraph::SetExecutorInternal(
absl::Status CalculatorGraph::SetExecutorInternal(
const std::string& name, std::shared_ptr<Executor> executor) {
if (!executors_.emplace(name, executor).second) {
return mediapipe::AlreadyExistsErrorBuilder(MEDIAPIPE_LOC)
@@ -1198,11 +1197,11 @@ mediapipe::Status CalculatorGraph::SetExecutorInternal(
} else {
MP_RETURN_IF_ERROR(scheduler_.SetNonDefaultExecutor(name, executor.get()));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorGraph::SetExecutor(
const std::string& name, std::shared_ptr<Executor> executor) {
absl::Status CalculatorGraph::SetExecutor(const std::string& name,
std::shared_ptr<Executor> executor) {
RET_CHECK(!initialized_)
<< "SetExecutor can only be called before Initialize()";
if (IsReservedExecutorName(name)) {
@@ -1212,7 +1211,7 @@ mediapipe::Status CalculatorGraph::SetExecutor(
return SetExecutorInternal(name, std::move(executor));
}
mediapipe::Status CalculatorGraph::CreateDefaultThreadPool(
absl::Status CalculatorGraph::CreateDefaultThreadPool(
const ThreadPoolExecutorOptions* default_executor_options,
int num_threads) {
MediaPipeOptions extendable_options;
@@ -1234,16 +1233,16 @@ bool CalculatorGraph::IsReservedExecutorName(const std::string& name) {
return ValidatedGraphConfig::IsReservedExecutorName(name);
}
mediapipe::Status CalculatorGraph::FinishRun() {
absl::Status CalculatorGraph::FinishRun() {
// Check for any errors that may have occurred.
mediapipe::Status status = mediapipe::OkStatus();
absl::Status status = absl::OkStatus();
MP_RETURN_IF_ERROR(profiler_->Stop());
GetCombinedErrors(&status);
CleanupAfterRun(&status);
return status;
}
void CalculatorGraph::CleanupAfterRun(mediapipe::Status* status) {
void CalculatorGraph::CleanupAfterRun(absl::Status* status) {
for (auto& item : graph_input_streams_) {
item.second->Close();
}
@@ -1310,7 +1309,7 @@ bool MetricElementComparator(const std::pair<std::string, int64>& e1,
}
} // namespace
mediapipe::Status CalculatorGraph::GetCalculatorProfiles(
absl::Status CalculatorGraph::GetCalculatorProfiles(
std::vector<CalculatorProfile>* profiles) const {
return profiler_->GetCalculatorProfiles(profiles);
}
+61 -68
View File
@@ -53,16 +53,16 @@
#include "mediapipe/framework/scheduler.h"
#include "mediapipe/framework/thread_pool_executor.pb.h"
#ifndef MEDIAPIPE_DISABLE_GPU
#if !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
class GpuResources;
struct GpuSharedData;
} // namespace mediapipe
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
typedef mediapipe::StatusOr<OutputStreamPoller> StatusOrPoller;
typedef absl::StatusOr<OutputStreamPoller> StatusOrPoller;
// The class representing a DAG of calculator nodes.
//
@@ -126,12 +126,11 @@ class CalculatorGraph {
// Initializes the graph from a its proto description.
// side_packets that are provided at this stage are common across all Run()
// invocations and could be used to execute PacketGenerators immediately.
mediapipe::Status Initialize(
const CalculatorGraphConfig& config,
const std::map<std::string, Packet>& side_packets);
absl::Status Initialize(const CalculatorGraphConfig& config,
const std::map<std::string, Packet>& side_packets);
// Convenience version which does not take side packets.
mediapipe::Status Initialize(const CalculatorGraphConfig& config);
absl::Status Initialize(const CalculatorGraphConfig& config);
// Initializes the CalculatorGraph from the specified graph and subgraph
// configs. Template graph and subgraph configs can be specified through
@@ -139,7 +138,7 @@ class CalculatorGraph {
// CalclatorGraphConfig.type. A subgraph can be instantiated directly by
// specifying its type in |graph_type|. A template graph can be instantiated
// directly by specifying its template arguments in |options|.
mediapipe::Status Initialize(
absl::Status Initialize(
const std::vector<CalculatorGraphConfig>& configs,
const std::vector<CalculatorGraphTemplate>& templates,
const std::map<std::string, Packet>& side_packets = {},
@@ -155,9 +154,9 @@ class CalculatorGraph {
// packet emitted by the output stream. Can only be called before Run() or
// StartRun().
// TODO: Rename to AddOutputStreamCallback.
mediapipe::Status ObserveOutputStream(
absl::Status ObserveOutputStream(
const std::string& stream_name,
std::function<mediapipe::Status(const Packet&)> packet_callback);
std::function<absl::Status(const Packet&)> packet_callback);
// Adds an OutputStreamPoller for a stream. This provides a synchronous,
// polling API for accessing a stream's output. Should only be called before
@@ -169,17 +168,16 @@ class CalculatorGraph {
// packets (generated by PacketGenerators) can be retrieved before
// graph is done. Returns error if the graph is still running (for non-base
// packets) or the output side packet is not found or empty.
mediapipe::StatusOr<Packet> GetOutputSidePacket(
const std::string& packet_name);
absl::StatusOr<Packet> GetOutputSidePacket(const std::string& packet_name);
// Runs the graph after adding the given extra input side packets. All
// arguments are forgotten after Run() returns.
// Run() is a blocking call and will return when all calculators are done.
virtual mediapipe::Status Run(
virtual absl::Status Run(
const std::map<std::string, Packet>& extra_side_packets);
// Run the graph without adding any input side packets.
mediapipe::Status Run() { return Run({}); }
absl::Status Run() { return Run({}); }
// Start a run of the graph. StartRun, WaitUntilDone, HasError,
// AddPacketToInputStream, and CloseInputStream allow more control over
@@ -199,7 +197,7 @@ class CalculatorGraph {
// MP_RETURN_IF_ERROR(graph.CloseInputStream(stream));
// }
// MP_RETURN_IF_ERROR(graph.WaitUntilDone());
mediapipe::Status StartRun(
absl::Status StartRun(
const std::map<std::string, Packet>& extra_side_packets) {
return StartRun(extra_side_packets, {});
}
@@ -208,28 +206,27 @@ class CalculatorGraph {
// stream header before running.
// Note: We highly discourage the use of stream headers, this is added for the
// compatibility of existing calculators that use headers during Open().
mediapipe::Status StartRun(
const std::map<std::string, Packet>& extra_side_packets,
const std::map<std::string, Packet>& stream_headers);
absl::Status StartRun(const std::map<std::string, Packet>& extra_side_packets,
const std::map<std::string, Packet>& stream_headers);
// Wait for the current run to finish (block the current thread
// until all source calculators have returned StatusStop(), all
// graph_input_streams_ have been closed, and no more calculators can
// be run). This function can be called only after StartRun().
mediapipe::Status WaitUntilDone();
absl::Status WaitUntilDone();
// Wait until the running graph is in the idle mode, which is when nothing can
// be scheduled and nothing is running in the worker threads. This function
// can be called only after StartRun().
// NOTE: The graph must not have any source nodes because source nodes prevent
// the running graph from becoming idle until the source nodes are done.
mediapipe::Status WaitUntilIdle();
absl::Status WaitUntilIdle();
// Wait until a packet is emitted on one of the observed output streams.
// Returns immediately if a packet has already been emitted since the last
// call to this function.
// Returns OutOfRangeError if the graph terminated while waiting.
mediapipe::Status WaitForObservedOutput();
absl::Status WaitForObservedOutput();
// Quick non-locking means of checking if the graph has encountered an error.
bool HasError() const { return has_error_; }
@@ -243,8 +240,8 @@ class CalculatorGraph {
// sizes of the queues in the graph. The input stream must have been specified
// in the configuration as a graph level input_stream. On error, nothing is
// added.
mediapipe::Status AddPacketToInputStream(const std::string& stream_name,
const Packet& packet);
absl::Status AddPacketToInputStream(const std::string& stream_name,
const Packet& packet);
// Same as the l-value version of this function by the same name, but moves
// the r-value referenced packet into the stream instead of copying it over.
@@ -253,12 +250,12 @@ class CalculatorGraph {
// packet may remain valid. In particular, when using the ADD_IF_NOT_FULL
// mode with a full queue, this will return StatusUnavailable and the caller
// may try adding the packet again later.
mediapipe::Status AddPacketToInputStream(const std::string& stream_name,
Packet&& packet);
absl::Status AddPacketToInputStream(const std::string& stream_name,
Packet&& packet);
// Sets the queue size of a graph input stream, overriding the graph default.
mediapipe::Status SetInputStreamMaxQueueSize(const std::string& stream_name,
int max_queue_size);
absl::Status SetInputStreamMaxQueueSize(const std::string& stream_name,
int max_queue_size);
// Check if an input stream exists in the graph
bool HasInputStream(const std::string& name);
@@ -268,14 +265,14 @@ class CalculatorGraph {
// been closed (and all packets propagate through the graph).
// Note that multiple threads cannot call CloseInputStream() on the same
// stream_name at the same time.
mediapipe::Status CloseInputStream(const std::string& stream_name);
absl::Status CloseInputStream(const std::string& stream_name);
// Closes all the graph input streams.
// TODO: deprecate this function in favor of CloseAllPacketSources.
mediapipe::Status CloseAllInputStreams();
absl::Status CloseAllInputStreams();
// Closes all the graph input streams and source calculator nodes.
mediapipe::Status CloseAllPacketSources();
absl::Status CloseAllPacketSources();
// Returns the pointer to the stream with the given name, or dies if none
// exists. The result remains owned by the CalculatorGraph.
@@ -290,8 +287,7 @@ class CalculatorGraph {
// calculator in the graph. May be called at any time after the graph has been
// initialized.
ABSL_DEPRECATED("Use profiler()->GetCalculatorProfiles() instead")
mediapipe::Status GetCalculatorProfiles(
std::vector<CalculatorProfile>*) const;
absl::Status GetCalculatorProfiles(std::vector<CalculatorProfile>*) const;
// Set the type of counter used in this graph.
void SetCounterFactory(CounterFactory* factory) {
@@ -301,15 +297,14 @@ class CalculatorGraph {
// Callback when an error is encountered.
// Adds the error to the vector of errors.
void RecordError(const mediapipe::Status& error)
ABSL_LOCKS_EXCLUDED(error_mutex_);
void RecordError(const absl::Status& error) ABSL_LOCKS_EXCLUDED(error_mutex_);
// Combines errors into a status. Returns true if the vector of errors is
// non-empty.
bool GetCombinedErrors(const std::string& error_prefix,
mediapipe::Status* error_status);
absl::Status* error_status);
// Convenience overload which specifies a default error prefix.
bool GetCombinedErrors(mediapipe::Status* error_status);
bool GetCombinedErrors(absl::Status* error_status);
// Returns the maximum input stream queue size.
int GetMaxInputStreamQueueSize();
@@ -338,8 +333,8 @@ class CalculatorGraph {
// Sets the executor that will run the nodes assigned to the executor
// named |name|. If |name| is empty, this sets the default executor. Must
// be called before the graph is initialized.
mediapipe::Status SetExecutor(const std::string& name,
std::shared_ptr<Executor> executor);
absl::Status SetExecutor(const std::string& name,
std::shared_ptr<Executor> executor);
// WARNING: the following public methods are exposed to Scheduler only.
@@ -365,23 +360,23 @@ class CalculatorGraph {
return scheduler_.GetSchedulerTimes();
}
#ifndef MEDIAPIPE_DISABLE_GPU
#if !MEDIAPIPE_DISABLE_GPU
// Returns a pointer to the GpuResources in use, if any.
// Only meant for internal use.
std::shared_ptr<::mediapipe::GpuResources> GetGpuResources() const;
mediapipe::Status SetGpuResources(
absl::Status SetGpuResources(
std::shared_ptr<::mediapipe::GpuResources> resources);
// Helper for PrepareForRun. If it returns a non-empty map, those packets
// must be added to the existing side packets, replacing existing values
// that have the same key.
mediapipe::StatusOr<std::map<std::string, Packet>> PrepareGpu(
absl::StatusOr<std::map<std::string, Packet>> PrepareGpu(
const std::map<std::string, Packet>& side_packets);
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
template <typename T>
mediapipe::Status SetServiceObject(const GraphService<T>& service,
std::shared_ptr<T> object) {
absl::Status SetServiceObject(const GraphService<T>& service,
std::shared_ptr<T> object) {
return SetServicePacket(service,
MakePacket<std::shared_ptr<T>>(std::move(object)));
}
@@ -394,7 +389,7 @@ class CalculatorGraph {
}
// Only the Java API should call this directly.
mediapipe::Status SetServicePacket(const GraphServiceBase& service, Packet p);
absl::Status SetServicePacket(const GraphServiceBase& service, Packet p);
private:
// GraphRunState is used as a parameter in the function CallStatusHandlers.
@@ -418,7 +413,7 @@ class CalculatorGraph {
shard_.SetSpec(manager_->Spec());
}
void PrepareForRun(std::function<void(mediapipe::Status)> error_callback) {
void PrepareForRun(std::function<void(absl::Status)> error_callback) {
manager_->PrepareForRun(std::move(error_callback));
}
@@ -446,36 +441,35 @@ class CalculatorGraph {
};
// Initializes the graph from a ValidatedGraphConfig object.
mediapipe::Status Initialize(
std::unique_ptr<ValidatedGraphConfig> validated_graph,
const std::map<std::string, Packet>& side_packets);
absl::Status Initialize(std::unique_ptr<ValidatedGraphConfig> validated_graph,
const std::map<std::string, Packet>& side_packets);
// AddPacketToInputStreamInternal template is called by either
// AddPacketToInputStream(Packet&& packet) or
// AddPacketToInputStream(const Packet& packet).
template <typename T>
mediapipe::Status AddPacketToInputStreamInternal(
const std::string& stream_name, T&& packet);
absl::Status AddPacketToInputStreamInternal(const std::string& stream_name,
T&& packet);
// Sets the executor that will run the nodes assigned to the executor
// named |name|. If |name| is empty, this sets the default executor.
// Does not check that the graph is uninitialized and |name| is not a
// reserved executor name.
mediapipe::Status SetExecutorInternal(const std::string& name,
std::shared_ptr<Executor> executor);
absl::Status SetExecutorInternal(const std::string& name,
std::shared_ptr<Executor> executor);
// If the num_threads field in default_executor_options is not specified,
// assigns a reasonable value based on system configuration and the graph.
// Then, creates the default thread pool if appropriate.
//
// Only called by InitializeExecutors().
mediapipe::Status InitializeDefaultExecutor(
absl::Status InitializeDefaultExecutor(
const ThreadPoolExecutorOptions* default_executor_options,
bool use_application_thread);
// Creates a thread pool as the default executor. The num_threads argument
// overrides the num_threads field in default_executor_options.
mediapipe::Status CreateDefaultThreadPool(
absl::Status CreateDefaultThreadPool(
const ThreadPoolExecutorOptions* default_executor_options,
int num_threads);
@@ -483,39 +477,38 @@ class CalculatorGraph {
static bool IsReservedExecutorName(const std::string& name);
// Helper functions for Initialize().
mediapipe::Status InitializeExecutors();
mediapipe::Status InitializePacketGeneratorGraph(
absl::Status InitializeExecutors();
absl::Status InitializePacketGeneratorGraph(
const std::map<std::string, Packet>& side_packets);
mediapipe::Status InitializeStreams();
mediapipe::Status InitializeProfiler();
mediapipe::Status InitializeCalculatorNodes();
absl::Status InitializeStreams();
absl::Status InitializeProfiler();
absl::Status InitializeCalculatorNodes();
// Iterates through all nodes and schedules any that can be opened.
void ScheduleAllOpenableNodes();
// Does the bulk of the work for StartRun but does not start the scheduler.
mediapipe::Status PrepareForRun(
absl::Status PrepareForRun(
const std::map<std::string, Packet>& extra_side_packets,
const std::map<std::string, Packet>& stream_headers);
// Cleans up any remaining state after the run and returns any errors that may
// have occurred during the run. Called after the scheduler has terminated.
mediapipe::Status FinishRun();
absl::Status FinishRun();
// Cleans up any remaining state after the run. All status handlers run here
// if their requested input side packets exist.
// The original |*status| is passed to all the status handlers. If any status
// handler fails, it appends its error to errors_, and CleanupAfterRun sets
// |*status| to the new combined errors on return.
void CleanupAfterRun(mediapipe::Status* status)
ABSL_LOCKS_EXCLUDED(error_mutex_);
void CleanupAfterRun(absl::Status* status) ABSL_LOCKS_EXCLUDED(error_mutex_);
// Calls HandlePreRunStatus or HandleStatus on the StatusHandlers. Which one
// is called depends on the GraphRunState parameter (PRE_RUN or POST_RUN).
// current_run_side_packets_ must be set before this function is called.
// On error, has_error_ will be set.
void CallStatusHandlers(GraphRunState graph_run_state,
const mediapipe::Status& status);
const absl::Status& status);
// Callback function to throttle or unthrottle source nodes when a stream
// becomes full or non-full. A node is throttled (i.e. prevented being
@@ -531,11 +524,11 @@ class CalculatorGraph {
void UpdateThrottledNodes(InputStreamManager* stream, bool* stream_was_full);
Packet GetServicePacket(const GraphServiceBase& service);
#ifndef MEDIAPIPE_DISABLE_GPU
#if !MEDIAPIPE_DISABLE_GPU
// Owns the legacy GpuSharedData if we need to create one for backwards
// compatibility.
std::unique_ptr<::mediapipe::GpuSharedData> legacy_gpu_shared_;
#endif // !defined(MEDIAPIPE_DISABLE_GPU)
#endif // !MEDIAPIPE_DISABLE_GPU
// True if the graph was initialized.
bool initialized_ = false;
@@ -609,7 +602,7 @@ class CalculatorGraph {
// Vector of errors encountered while running graph. Always use RecordError()
// to add an error to this vector.
std::vector<mediapipe::Status> errors_ ABSL_GUARDED_BY(error_mutex_);
std::vector<absl::Status> errors_ ABSL_GUARDED_BY(error_mutex_);
// True if the default executor uses the application thread.
bool use_application_thread_ = false;
@@ -30,7 +30,7 @@ namespace {
constexpr int kIntTestValue = 33;
typedef std::function<mediapipe::Status(CalculatorContext* cc)>
typedef std::function<absl::Status(CalculatorContext* cc)>
CalculatorContextFunction;
// Returns the contents of a set of Packets.
@@ -87,26 +87,24 @@ class CountingExecutor : public Executor {
// streams and outputs the sum to the output stream.
class IntAdderCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).Set<int>();
}
cc->Outputs().Index(0).Set<int>();
cc->SetTimestampOffset(TimestampDiff(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
int sum = 0;
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
sum += cc->Inputs().Index(i).Get<int>();
}
cc->Outputs().Index(0).Add(new int(sum), cc->InputTimestamp());
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(IntAdderCalculator);
@@ -114,13 +112,13 @@ REGISTER_CALCULATOR(IntAdderCalculator);
template <typename InputType>
class TypedSinkCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<InputType>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
return mediapipe::OkStatus();
absl::Status Process(CalculatorContext* cc) override {
return absl::OkStatus();
}
};
typedef TypedSinkCalculator<std::string> StringSinkCalculator;
@@ -132,13 +130,13 @@ REGISTER_CALCULATOR(IntSinkCalculator);
// integer.
class EvenIntFilterCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
int value = cc->Inputs().Index(0).Get<int>();
if (value % 2 == 0) {
cc->Outputs().Index(0).AddPacket(cc->Inputs().Index(0).Value());
@@ -146,7 +144,7 @@ class EvenIntFilterCalculator : public CalculatorBase {
cc->Outputs().Index(0).SetNextTimestampBound(
cc->InputTimestamp().NextAllowedInStream());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(EvenIntFilterCalculator);
@@ -156,19 +154,19 @@ REGISTER_CALCULATOR(EvenIntFilterCalculator);
// input stream carries the value true.
class ValveCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Inputs().Index(1).Set<bool>();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->Outputs().Index(0).SetHeader(cc->Inputs().Index(0).Header());
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
if (cc->Inputs().Index(1).Get<bool>()) {
cc->GetCounter("PassThrough")->Increment();
cc->Outputs().Index(0).AddPacket(cc->Inputs().Index(0).Value());
@@ -182,7 +180,7 @@ class ValveCalculator : public CalculatorBase {
cc->Outputs().Index(0).SetNextTimestampBound(
cc->InputTimestamp().NextAllowedInStream());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(ValveCalculator);
@@ -191,27 +189,27 @@ REGISTER_CALCULATOR(ValveCalculator);
// but shifts the timestamp.
class TimeShiftCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
cc->InputSidePackets().Index(0).Set<TimestampDiff>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
// Input: arbitrary Packets.
// Output: copy of the input.
cc->Outputs().Index(0).SetHeader(cc->Inputs().Index(0).Header());
shift_ = cc->InputSidePackets().Index(0).Get<TimestampDiff>();
cc->SetOffset(shift_);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
cc->GetCounter("PassThrough")->Increment();
cc->Outputs().Index(0).AddPacket(
cc->Inputs().Index(0).Value().At(cc->InputTimestamp() + shift_));
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -231,17 +229,17 @@ REGISTER_CALCULATOR(TimeShiftCalculator);
// T=2000 Output 100
class OutputAndBoundSourceCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Outputs().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
counter_ = 0;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
Timestamp timestamp(counter_);
if (counter_ % 20 == 0) {
cc->Outputs().Index(0).AddPacket(
@@ -253,7 +251,7 @@ class OutputAndBoundSourceCalculator : public CalculatorBase {
return tool::StatusStop();
}
counter_ += 10;
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -266,42 +264,40 @@ REGISTER_CALCULATOR(OutputAndBoundSourceCalculator);
// Process() method. The input stream and output stream have the integer type.
class Delay20Calculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
cc->SetTimestampOffset(TimestampDiff(20));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->Outputs().Index(0).AddPacket(MakePacket<int>(0).At(Timestamp(0)));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
const Packet& packet = cc->Inputs().Index(0).Value();
Timestamp timestamp = packet.Timestamp() + 20;
cc->Outputs().Index(0).AddPacket(packet.At(timestamp));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(Delay20Calculator);
class CustomBoundCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
cc->Outputs().Index(0).SetNextTimestampBound(cc->InputTimestamp() + 1);
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(CustomBoundCalculator);
@@ -613,7 +609,7 @@ TEST(CalculatorGraphBoundsTest, ImmediateHandlerBounds) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output", [&](const Packet& p) {
output_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -638,47 +634,41 @@ TEST(CalculatorGraphBoundsTest, ImmediateHandlerBounds) {
// A Calculator that only sets timestamp bound by SetOffset().
class OffsetBoundCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
cc->SetTimestampOffset(TimestampDiff(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Process(CalculatorContext* cc) final { return absl::OkStatus(); }
};
REGISTER_CALCULATOR(OffsetBoundCalculator);
// A Calculator that produces a packet for each call to Process.
class BoundToPacketCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).SetAny();
}
for (int i = 0; i < cc->Outputs().NumEntries(); ++i) {
cc->Outputs().Index(i).Set<Timestamp>();
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
for (int i = 0; i < cc->Outputs().NumEntries(); ++i) {
Timestamp t = cc->Inputs().Index(i).Value().Timestamp();
cc->Outputs().Index(i).AddPacket(
mediapipe::MakePacket<Timestamp>(t).At(cc->InputTimestamp()));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(BoundToPacketCalculator);
@@ -688,22 +678,20 @@ class FuturePacketCalculator : public CalculatorBase {
public:
static constexpr int64 kOutputFutureMicros = 3;
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
const Packet& packet = cc->Inputs().Index(0).Value();
Timestamp timestamp =
Timestamp(packet.Timestamp().Value() + kOutputFutureMicros);
cc->Outputs().Index(0).AddPacket(packet.At(timestamp));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(FuturePacketCalculator);
@@ -735,7 +723,7 @@ TEST(CalculatorGraphBoundsTest, OffsetBoundPropagation) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output", [&](const Packet& p) {
output_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -786,7 +774,7 @@ TEST(CalculatorGraphBoundsTest, BoundWithoutInputPackets) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output", [&](const Packet& p) {
output_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -860,13 +848,13 @@ TEST(CalculatorGraphBoundsTest, FixedSizeHandlerBounds) {
std::vector<Packet> outputs;
MP_ASSERT_OK(graph.ObserveOutputStream("output", [&](const Packet& p) {
outputs.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
std::vector<Packet> thinned_outputs;
MP_ASSERT_OK(
graph.ObserveOutputStream("thinned_output", [&](const Packet& p) {
thinned_outputs.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// The enter_semaphore is used to wait for LambdaCalculator::Process.
@@ -875,13 +863,13 @@ TEST(CalculatorGraphBoundsTest, FixedSizeHandlerBounds) {
AtomicSemaphore exit_semaphore(0);
CalculatorContextFunction open_fn = [&](CalculatorContext* cc) {
cc->SetOffset(0);
return mediapipe::OkStatus();
return absl::OkStatus();
};
CalculatorContextFunction process_fn = [&](CalculatorContext* cc) {
enter_semaphore.Release(1);
exit_semaphore.Acquire(1);
cc->Outputs().Index(0).AddPacket(cc->Inputs().Index(0).Value());
return mediapipe::OkStatus();
return absl::OkStatus();
};
MP_ASSERT_OK(graph.StartRun({
{"open_fn", Adopt(new auto(open_fn))},
@@ -935,22 +923,20 @@ TEST(CalculatorGraphBoundsTest, FixedSizeHandlerBounds) {
// A Calculator that outputs only the last packet from its input stream.
class LastPacketCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetAny();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) final {
cc->Outputs().Index(0).SetNextTimestampBound(cc->InputTimestamp());
last_packet_ = cc->Inputs().Index(0).Value();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Close(CalculatorContext* cc) final {
absl::Status Close(CalculatorContext* cc) final {
cc->Outputs().Index(0).AddPacket(last_packet_);
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -992,12 +978,12 @@ TEST(CalculatorGraphBoundsTest, LastPacketCheck) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output", [&](const Packet& p) {
output_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
std::vector<Packet> last_output_packets;
MP_ASSERT_OK(graph.ObserveOutputStream("last_output", [&](const Packet& p) {
last_output_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -1055,11 +1041,11 @@ void TestBoundsForEmptyInputs(std::string input_stream_handler) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("input_ts", [&](const Packet& p) {
input_ts_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.ObserveOutputStream("bounds_ts", [&](const Packet& p) {
bounds_ts_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -1129,7 +1115,7 @@ TEST(CalculatorGraphBoundsTest, BoundsForEmptyInputs_SyncSets) {
// A Calculator that produces a packet for each timestamp bounds update.
class ProcessBoundToPacketCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).SetAny();
}
@@ -1138,10 +1124,10 @@ class ProcessBoundToPacketCalculator : public CalculatorBase {
}
cc->SetInputStreamHandler("ImmediateInputStreamHandler");
cc->SetProcessTimestampBounds(true);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
for (int i = 0; i < cc->Outputs().NumEntries(); ++i) {
Timestamp t = cc->Inputs().Index(i).Value().Timestamp();
// Create a new packet for each input stream with a new timestamp bound,
@@ -1151,7 +1137,7 @@ class ProcessBoundToPacketCalculator : public CalculatorBase {
cc->Outputs().Index(i).Add(new auto(t), t);
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(ProcessBoundToPacketCalculator);
@@ -1159,7 +1145,7 @@ REGISTER_CALCULATOR(ProcessBoundToPacketCalculator);
// A Calculator that passes through each packet and timestamp immediately.
class ImmediatePassthroughCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).SetAny();
}
@@ -1168,10 +1154,10 @@ class ImmediatePassthroughCalculator : public CalculatorBase {
}
cc->SetInputStreamHandler("ImmediateInputStreamHandler");
cc->SetProcessTimestampBounds(true);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
for (int i = 0; i < cc->Outputs().NumEntries(); ++i) {
if (!cc->Inputs().Index(i).IsEmpty()) {
cc->Outputs().Index(i).AddPacket(cc->Inputs().Index(i).Value());
@@ -1185,7 +1171,7 @@ class ImmediatePassthroughCalculator : public CalculatorBase {
}
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(ImmediatePassthroughCalculator);
@@ -1224,7 +1210,7 @@ void TestProcessForEmptyInputs(const std::string& input_stream_handler) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("bounds_ts", [&](const Packet& p) {
bounds_ts_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -1324,11 +1310,11 @@ TEST(CalculatorGraphBoundsTest, ProcessTimestampBounds_Passthrough) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output_0", [&](const Packet& p) {
output_0_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.ObserveOutputStream("output_1", [&](const Packet& p) {
output_1_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -1378,20 +1364,20 @@ TEST(CalculatorGraphBoundsTest, ProcessTimestampBounds_Passthrough) {
// A Calculator that sends a timestamp bound for every other input.
class OccasionalBoundCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
absl::SleepFor(absl::Milliseconds(1));
if (cc->InputTimestamp().Value() % 20 == 0) {
Timestamp bound = cc->InputTimestamp().NextAllowedInStream();
cc->Outputs().Index(0).SetNextTimestampBound(
std::max(bound, cc->Outputs().Index(0).NextTimestampBound()));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OccasionalBoundCalculator);
@@ -1419,7 +1405,7 @@ TEST(CalculatorGraphBoundsTest, MaxInFlightWithOccasionalBound) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output_0", [&](const Packet& p) {
output_0_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -1443,20 +1429,18 @@ TEST(CalculatorGraphBoundsTest, MaxInFlightWithOccasionalBound) {
// A Calculator that uses both SetTimestampOffset and SetNextTimestampBound.
class OffsetAndBoundCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
cc->SetTimestampOffset(TimestampDiff(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
return mediapipe::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) final {
if (cc->InputTimestamp().Value() % 20 == 0) {
cc->Outputs().Index(0).SetNextTimestampBound(Timestamp(10000));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OffsetAndBoundCalculator);
@@ -1481,7 +1465,7 @@ TEST(CalculatorGraphBoundsTest, OffsetAndBound) {
MP_ASSERT_OK(graph.Initialize(config));
MP_ASSERT_OK(graph.ObserveOutputStream("output_0", [&](const Packet& p) {
output_0_packets.push_back(p);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
MP_ASSERT_OK(graph.WaitUntilIdle());
@@ -53,25 +53,25 @@ class CalculatorGraphEventLoopTest : public testing::Test {
// testing.
class BlockingPassThroughCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
cc->InputSidePackets().Index(0).Set<std::unique_ptr<absl::Mutex>>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
mutex_ = GetFromUniquePtr<absl::Mutex>(cc->InputSidePackets().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
mutex_->Lock();
cc->Outputs().Index(0).AddPacket(
cc->Inputs().Index(0).Value().At(cc->InputTimestamp()));
mutex_->Unlock();
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -87,15 +87,15 @@ struct SimpleHeader {
class UsingHeaderCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
if (cc->Inputs().Index(0).Header().IsEmpty()) {
return mediapipe::UnknownError("No stream header present.");
return absl::UnknownError("No stream header present.");
}
const SimpleHeader& header =
@@ -105,13 +105,13 @@ class UsingHeaderCalculator : public CalculatorBase {
output_header->height = header.height;
cc->Outputs().Index(0).SetHeader(Adopt(output_header.release()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
cc->Outputs().Index(0).AddPacket(
cc->Inputs().Index(0).Value().At(cc->InputTimestamp()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(UsingHeaderCalculator);
@@ -187,21 +187,20 @@ TEST_F(CalculatorGraphEventLoopTest, WellProvisionedEventLoop) {
// Pass-Through calculator that fails upon receiving the 10th packet.
class FailingPassThroughCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
Timestamp timestamp = cc->InputTimestamp();
if (timestamp.Value() == 9) {
return mediapipe::UnknownError(
"Meant to fail (magicstringincludedhere).");
return absl::UnknownError("Meant to fail (magicstringincludedhere).");
}
cc->Outputs().Index(0).AddPacket(
cc->Inputs().Index(0).Value().At(timestamp));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(FailingPassThroughCalculator);
@@ -231,7 +230,7 @@ TEST_F(CalculatorGraphEventLoopTest, FailingEventLoop) {
this, std::placeholders::_1))}}));
// Insert packets.
mediapipe::Status status;
absl::Status status;
for (int i = 0; true; ++i) {
status = graph.AddPacketToInputStream("input_numbers",
Adopt(new int(i)).At(Timestamp(i)));
@@ -315,10 +314,10 @@ TEST_F(CalculatorGraphEventLoopTest, SetStreamHeader) {
&CalculatorGraphEventLoopTest::AddThreadSafeVectorSink,
this, std::placeholders::_1))}}));
mediapipe::Status status = graph.WaitUntilIdle();
absl::Status status = graph.WaitUntilIdle();
// Expect to fail if header not set.
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kUnknown);
EXPECT_EQ(status.code(), absl::StatusCode::kUnknown);
EXPECT_THAT(status.message(),
testing::HasSubstr("No stream header present."));
@@ -387,7 +386,7 @@ TEST_F(CalculatorGraphEventLoopTest, TryToAddPacketToInputStream) {
// mechanism could be off by 1 at most due to the order of acquisition of
// locks.
for (int i = 0; i < kNumInputPackets; ++i) {
mediapipe::Status status = graph.AddPacketToInputStream(
absl::Status status = graph.AddPacketToInputStream(
"input_numbers", Adopt(new int(i)).At(Timestamp(i)));
if (!status.ok()) {
++fail_count;
@@ -472,7 +471,7 @@ TEST_F(CalculatorGraphEventLoopTest, ThrottleGraphInputStreamTwice) {
// Lock the mutex so that the BlockingPassThroughCalculator cannot read any
// of these packets.
mutex->Lock();
mediapipe::Status status = mediapipe::OkStatus();
absl::Status status = absl::OkStatus();
for (int i = 0; i < 10; ++i) {
status = graph.AddPacketToInputStream("input_numbers",
Adopt(new int(i)).At(Timestamp(i)));
@@ -482,7 +481,7 @@ TEST_F(CalculatorGraphEventLoopTest, ThrottleGraphInputStreamTwice) {
}
mutex->Unlock();
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kUnavailable);
EXPECT_EQ(status.code(), absl::StatusCode::kUnavailable);
EXPECT_THAT(status.message(), testing::HasSubstr("Graph is throttled."));
MP_ASSERT_OK(graph.CloseInputStream("input_numbers"));
MP_ASSERT_OK(graph.WaitUntilDone());
@@ -523,7 +522,7 @@ TEST_F(CalculatorGraphEventLoopTest, WaitToAddPacketToInputStream) {
// All of these packets should be accepted by the graph.
int fail_count = 0;
for (int i = 0; i < kNumInputPackets; ++i) {
mediapipe::Status status = graph.AddPacketToInputStream(
absl::Status status = graph.AddPacketToInputStream(
"input_numbers", Adopt(new int(i)).At(Timestamp(i)));
if (!status.ok()) {
++fail_count;
@@ -576,7 +575,7 @@ TEST_F(CalculatorGraphEventLoopTest, UnthrottleSources) {
CalculatorGraph::GraphInputStreamAddMode::ADD_IF_NOT_FULL);
auto poller_status = graph.AddOutputStreamPoller("output_numbers");
MP_ASSERT_OK(poller_status.status());
mediapipe::OutputStreamPoller& poller = poller_status.ValueOrDie();
mediapipe::OutputStreamPoller& poller = poller_status.value();
poller.SetMaxQueueSize(kQueueSize);
MP_ASSERT_OK(graph.StartRun({}));
@@ -38,16 +38,16 @@ namespace {
// output side packet.
class OutputSidePacketInProcessCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->OutputSidePackets().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
cc->OutputSidePackets().Index(0).Set(
cc->Inputs().Index(0).Value().At(Timestamp::Unset()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OutputSidePacketInProcessCalculator);
@@ -56,22 +56,22 @@ REGISTER_CALCULATOR(OutputSidePacketInProcessCalculator);
// receives. Outputs the total number of packets as a side packet in Close.
class CountAndOutputSummarySidePacketInCloseCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->OutputSidePackets().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
++count_;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Close(CalculatorContext* cc) final {
absl::Status Close(CalculatorContext* cc) final {
absl::SleepFor(absl::Milliseconds(300)); // For GetOutputSidePacket test.
cc->OutputSidePackets().Index(0).Set(
MakePacket<int>(count_).At(Timestamp::Unset()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
int count_ = 0;
@@ -82,15 +82,15 @@ REGISTER_CALCULATOR(CountAndOutputSummarySidePacketInCloseCalculator);
// output side packet. This triggers an error in the graph.
class OutputSidePacketWithTimestampCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->OutputSidePackets().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
cc->OutputSidePackets().Index(0).Set(cc->Inputs().Index(0).Value());
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OutputSidePacketWithTimestampCalculator);
@@ -98,19 +98,19 @@ REGISTER_CALCULATOR(OutputSidePacketWithTimestampCalculator);
// Generates an output side packet containing the integer 1.
class IntegerOutputSidePacketCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->OutputSidePackets().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->OutputSidePackets().Index(0).Set(MakePacket<int>(1));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
LOG(FATAL) << "Not reached.";
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(IntegerOutputSidePacketCalculator);
@@ -119,23 +119,23 @@ REGISTER_CALCULATOR(IntegerOutputSidePacketCalculator);
// side packets.
class SidePacketAdderCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->InputSidePackets().Index(0).Set<int>();
cc->InputSidePackets().Index(1).Set<int>();
cc->OutputSidePackets().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->OutputSidePackets().Index(0).Set(
MakePacket<int>(cc->InputSidePackets().Index(1).Get<int>() +
cc->InputSidePackets().Index(0).Get<int>()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
LOG(FATAL) << "Not reached.";
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(SidePacketAdderCalculator);
@@ -144,20 +144,20 @@ REGISTER_CALCULATOR(SidePacketAdderCalculator);
// input side packet.
class SidePacketToStreamPacketCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->InputSidePackets().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->InputSidePackets().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->Outputs().Index(0).AddPacket(
cc->InputSidePackets().Index(0).At(Timestamp::PostStream()));
cc->Outputs().Index(0).Close();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
return mediapipe::tool::StatusStop();
}
};
@@ -166,18 +166,18 @@ REGISTER_CALCULATOR(SidePacketToStreamPacketCalculator);
// Packet generator for an arbitrary unit64 packet.
class Uint64PacketGenerator : public PacketGenerator {
public:
static mediapipe::Status FillExpectations(
static absl::Status FillExpectations(
const PacketGeneratorOptions& extendable_options,
PacketTypeSet* input_side_packets, PacketTypeSet* output_side_packets) {
output_side_packets->Index(0).Set<uint64>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
static mediapipe::Status Generate(
const PacketGeneratorOptions& extendable_options,
const PacketSet& input_side_packets, PacketSet* output_side_packets) {
static absl::Status Generate(const PacketGeneratorOptions& extendable_options,
const PacketSet& input_side_packets,
PacketSet* output_side_packets) {
output_side_packets->Index(0) = Adopt(new uint64(15LL << 32 | 5));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_PACKET_GENERATOR(Uint64PacketGenerator);
@@ -204,7 +204,7 @@ TEST(CalculatorGraph, OutputSidePacketInProcess) {
MP_ASSERT_OK(graph.ObserveOutputStream(
"output", [&output_packets](const Packet& packet) {
output_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// Run the graph twice.
@@ -226,11 +226,11 @@ TEST(CalculatorGraph, OutputSidePacketInProcess) {
// also be ignored.
class PassThroughGenerator : public PacketGenerator {
public:
static mediapipe::Status FillExpectations(
static absl::Status FillExpectations(
const PacketGeneratorOptions& extendable_options, PacketTypeSet* inputs,
PacketTypeSet* outputs) {
if (!inputs->TagMap()->SameAs(*outputs->TagMap())) {
return mediapipe::InvalidArgumentError(
return absl::InvalidArgumentError(
"Input and outputs to PassThroughGenerator must use the same tags "
"and indexes.");
}
@@ -238,17 +238,17 @@ class PassThroughGenerator : public PacketGenerator {
inputs->Get(id).SetAny();
outputs->Get(id).SetSameAs(&inputs->Get(id));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
static mediapipe::Status Generate(
const PacketGeneratorOptions& extendable_options,
const PacketSet& input_side_packets, PacketSet* output_side_packets) {
static absl::Status Generate(const PacketGeneratorOptions& extendable_options,
const PacketSet& input_side_packets,
PacketSet* output_side_packets) {
for (CollectionItemId id = input_side_packets.BeginId();
id < input_side_packets.EndId(); ++id) {
output_side_packets->Get(id) = input_side_packets.Get(id);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_PACKET_GENERATOR(PassThroughGenerator);
@@ -402,8 +402,8 @@ TEST(CalculatorGraph, OutputSidePacketAlreadySet) {
"offset", MakePacket<TimestampDiff>(offset).At(Timestamp(1))));
MP_ASSERT_OK(graph.CloseInputStream("offset"));
mediapipe::Status status = graph.WaitUntilDone();
EXPECT_EQ(status.code(), mediapipe::StatusCode::kAlreadyExists);
absl::Status status = graph.WaitUntilDone();
EXPECT_EQ(status.code(), absl::StatusCode::kAlreadyExists);
EXPECT_THAT(status.message(), testing::HasSubstr("was already set."));
}
@@ -428,8 +428,8 @@ TEST(CalculatorGraph, OutputSidePacketWithTimestamp) {
MP_ASSERT_OK(graph.AddPacketToInputStream(
"offset", MakePacket<TimestampDiff>(offset).At(Timestamp(237))));
MP_ASSERT_OK(graph.CloseInputStream("offset"));
mediapipe::Status status = graph.WaitUntilDone();
EXPECT_EQ(status.code(), mediapipe::StatusCode::kInvalidArgument);
absl::Status status = graph.WaitUntilDone();
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument);
EXPECT_THAT(status.message(), testing::HasSubstr("has a timestamp 237."));
}
@@ -460,7 +460,7 @@ TEST(CalculatorGraph, OutputSidePacketConsumedBySourceNode) {
MP_ASSERT_OK(graph.ObserveOutputStream(
"output", [&output_packets](const Packet& packet) {
output_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
// Wait until the graph is idle so that
@@ -486,19 +486,19 @@ class FirstPacketFilterCalculator : public CalculatorBase {
FirstPacketFilterCalculator() {}
~FirstPacketFilterCalculator() override {}
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->Outputs().Index(0).SetSameAs(&cc->Inputs().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
if (!seen_first_packet_) {
cc->Outputs().Index(0).AddPacket(cc->Inputs().Index(0).Value());
cc->Outputs().Index(0).Close();
seen_first_packet_ = true;
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -568,8 +568,8 @@ TEST(CalculatorGraph, SourceLayerInversion) {
MP_ASSERT_OK(graph.Initialize(
config, {{"max_count", MakePacket<int>(max_count)},
{"initial_value1", MakePacket<int>(initial_value1)}}));
mediapipe::Status status = graph.Run();
EXPECT_EQ(status.code(), mediapipe::StatusCode::kUnknown);
absl::Status status = graph.Run();
EXPECT_EQ(status.code(), absl::StatusCode::kUnknown);
EXPECT_THAT(status.message(), testing::HasSubstr("deadlock"));
}
@@ -614,7 +614,7 @@ TEST(CalculatorGraph, PacketGeneratorLikeCalculators) {
MP_ASSERT_OK(graph.ObserveOutputStream(
"output", [&output_packets](const Packet& packet) {
output_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.Run());
ASSERT_EQ(1, output_packets.size());
@@ -643,7 +643,7 @@ TEST(CalculatorGraph, OutputSummarySidePacketInClose) {
MP_ASSERT_OK(graph.ObserveOutputStream(
"output", [&output_packets](const Packet& packet) {
output_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// Run the graph twice.
@@ -686,15 +686,14 @@ TEST(CalculatorGraph, GetOutputSidePacket) {
MP_ASSERT_OK(graph.Initialize(config));
// Check a packet generated by the PacketGenerator, which is available after
// graph initialization, can be fetched before graph starts.
mediapipe::StatusOr<Packet> status_or_packet =
absl::StatusOr<Packet> status_or_packet =
graph.GetOutputSidePacket("output_uint64");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
// IntSplitterPacketGenerator is missing its input side packet and we
// won't be able to get its output side packet now.
status_or_packet = graph.GetOutputSidePacket("output_uint32_pair");
EXPECT_EQ(mediapipe::StatusCode::kUnavailable,
status_or_packet.status().code());
EXPECT_EQ(absl::StatusCode::kUnavailable, status_or_packet.status().code());
// Run the graph twice.
int max_count = 100;
std::map<std::string, Packet> extra_side_packets;
@@ -703,7 +702,7 @@ TEST(CalculatorGraph, GetOutputSidePacket) {
MP_ASSERT_OK(graph.StartRun(extra_side_packets));
status_or_packet = graph.GetOutputSidePacket("output_uint32_pair");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
for (int i = 0; i < max_count; ++i) {
MP_ASSERT_OK(graph.AddPacketToInputStream(
"input_packets", MakePacket<int>(i).At(Timestamp(i))));
@@ -713,34 +712,32 @@ TEST(CalculatorGraph, GetOutputSidePacket) {
// Should return NOT_FOUND for invalid side packets.
status_or_packet = graph.GetOutputSidePacket("unknown");
EXPECT_FALSE(status_or_packet.ok());
EXPECT_EQ(mediapipe::StatusCode::kNotFound,
status_or_packet.status().code());
EXPECT_EQ(absl::StatusCode::kNotFound, status_or_packet.status().code());
// Should return UNAVAILABLE before graph is done for valid non-base
// packets.
status_or_packet = graph.GetOutputSidePacket("num_of_packets");
EXPECT_FALSE(status_or_packet.ok());
EXPECT_EQ(mediapipe::StatusCode::kUnavailable,
status_or_packet.status().code());
EXPECT_EQ(absl::StatusCode::kUnavailable, status_or_packet.status().code());
// Should stil return a base even before graph is done.
status_or_packet = graph.GetOutputSidePacket("output_uint64");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
MP_ASSERT_OK(graph.WaitUntilDone());
// Check packets are available after graph is done.
status_or_packet = graph.GetOutputSidePacket("num_of_packets");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(max_count, status_or_packet.ValueOrDie().Get<int>());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(max_count, status_or_packet.value().Get<int>());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
// Should still return a base packet after graph is done.
status_or_packet = graph.GetOutputSidePacket("output_uint64");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
// Should still return a non-base packet after graph is done.
status_or_packet = graph.GetOutputSidePacket("output_uint32_pair");
MP_ASSERT_OK(status_or_packet);
EXPECT_EQ(Timestamp::Unset(), status_or_packet.ValueOrDie().Timestamp());
EXPECT_EQ(Timestamp::Unset(), status_or_packet.value().Timestamp());
}
}
@@ -749,20 +746,20 @@ typedef std::string HugeModel;
// Generates an output-side-packet once for each calculator-graph.
class OutputSidePacketCachedCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->OutputSidePackets().Index(0).Set<HugeModel>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
cc->OutputSidePackets().Index(0).Set(MakePacket<HugeModel>(
R"(An expensive side-packet created only once per graph)"));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
LOG(FATAL) << "Not reached.";
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OutputSidePacketCachedCalculator);
@@ -791,7 +788,7 @@ TEST(CalculatorGraph, OutputSidePacketCached) {
MP_ASSERT_OK(graph.ObserveOutputStream(
"output", [&output_packets](const Packet& packet) {
output_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// Run the graph three times.
@@ -49,24 +49,24 @@ using mediapipe::Packet;
class InfiniteSequenceCalculator : public mediapipe::CalculatorBase {
public:
static mediapipe::Status GetContract(mediapipe::CalculatorContract* cc) {
static absl::Status GetContract(mediapipe::CalculatorContract* cc) {
cc->Outputs().Tag("OUT").Set<int>();
cc->Outputs().Tag("EVENT").Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
cc->Outputs().Tag("EVENT").AddPacket(MakePacket<int>(1).At(Timestamp(1)));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
cc->Outputs().Tag("OUT").AddPacket(
MakePacket<int>(count_).At(Timestamp(count_)));
count_++;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Close(CalculatorContext* cc) override {
absl::Status Close(CalculatorContext* cc) override {
cc->Outputs().Tag("EVENT").AddPacket(MakePacket<int>(2).At(Timestamp(2)));
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -76,30 +76,30 @@ REGISTER_CALCULATOR(::testing_ns::InfiniteSequenceCalculator);
class StoppingPassThroughCalculator : public mediapipe::CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(""); ++i) {
cc->Inputs().Get("", i).SetAny();
cc->Outputs().Get("", i).SetSameAs(&cc->Inputs().Get("", i));
}
cc->Outputs().Tag("EVENT").Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
cc->Outputs().Tag("EVENT").AddPacket(MakePacket<int>(1).At(Timestamp(1)));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
for (int i = 0; i < cc->Inputs().NumEntries(""); ++i) {
if (!cc->Inputs().Get("", i).IsEmpty()) {
cc->Outputs().Get("", i).AddPacket(cc->Inputs().Get("", i).Value());
}
}
return (++count_ <= max_count_) ? mediapipe::OkStatus()
return (++count_ <= max_count_) ? absl::OkStatus()
: mediapipe::tool::StatusStop();
}
mediapipe::Status Close(CalculatorContext* cc) override {
absl::Status Close(CalculatorContext* cc) override {
cc->Outputs().Tag("EVENT").AddPacket(MakePacket<int>(2).At(Timestamp(2)));
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -124,39 +124,39 @@ class AtomicSemaphore {
};
// A ProcessFunction that passes through all packets.
mediapipe::Status DoProcess(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs) {
absl::Status DoProcess(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs) {
for (int i = 0; i < inputs.NumEntries(); ++i) {
if (!inputs.Index(i).Value().IsEmpty()) {
outputs->Index(i).AddPacket(inputs.Index(i).Value());
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
typedef std::function<mediapipe::Status(const InputStreamShardSet&,
OutputStreamShardSet*)>
typedef std::function<absl::Status(const InputStreamShardSet&,
OutputStreamShardSet*)>
ProcessFunction;
// A Calculator that delegates its Process function to a callback function.
class ProcessCallbackCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (int i = 0; i < cc->Inputs().NumEntries(); ++i) {
cc->Inputs().Index(i).SetAny();
cc->Outputs().Index(i).SetSameAs(&cc->Inputs().Index(0));
}
cc->InputSidePackets().Index(0).Set<std::unique_ptr<ProcessFunction>>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) final {
absl::Status Open(CalculatorContext* cc) final {
callback_ =
*GetFromUniquePtr<ProcessFunction>(cc->InputSidePackets().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
return callback_(cc->Inputs(), &(cc->Outputs()));
}
@@ -202,22 +202,22 @@ TEST(CalculatorGraphStoppingTest, CloseAllPacketSources) {
if (out_packets.size() >= kNumPackets) {
MP_EXPECT_OK(graph.CloseAllPacketSources());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.ObserveOutputStream( //
"count_out", [&](const Packet& packet) {
count_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.ObserveOutputStream( //
"event", [&](const Packet& packet) {
event_packets.push_back(packet.Get<int>());
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.ObserveOutputStream( //
"event_out", [&](const Packet& packet) {
event_out_packets.push_back(packet.Get<int>());
return mediapipe::OkStatus();
return absl::OkStatus();
}));
MP_ASSERT_OK(graph.StartRun({}));
for (int i = 0; i < kNumPackets; ++i) {
@@ -261,7 +261,7 @@ TEST(CalculatorGraphStoppingTest, DeadlockReporting) {
MP_ASSERT_OK(
graph.ObserveOutputStream("out_1", [&out_packets](const Packet& packet) {
out_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// Lambda that waits for a local semaphore.
@@ -289,8 +289,8 @@ TEST(CalculatorGraphStoppingTest, DeadlockReporting) {
MP_EXPECT_OK(add_packet("in_1", 2));
EXPECT_FALSE(add_packet("in_1", 3).ok());
mediapipe::Status status = graph.WaitUntilIdle();
EXPECT_EQ(status.code(), mediapipe::StatusCode::kUnavailable);
absl::Status status = graph.WaitUntilIdle();
EXPECT_EQ(status.code(), absl::StatusCode::kUnavailable);
EXPECT_THAT(
status.message(),
testing::HasSubstr("Detected a deadlock due to input throttling"));
@@ -326,7 +326,7 @@ TEST(CalculatorGraphStoppingTest, DeadlockResolution) {
MP_ASSERT_OK(
graph.ObserveOutputStream("out_1", [&out_packets](const Packet& packet) {
out_packets.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
// Lambda that waits for a local semaphore.
File diff suppressed because it is too large Load Diff
+86 -38
View File
@@ -63,6 +63,52 @@ const PacketType* GetPacketType(const PacketTypeSet& packet_type_set,
return &packet_type_set.Get(id);
}
// Copies a TagMap omitting entries with certain names.
std::shared_ptr<tool::TagMap> RemoveNames(const tool::TagMap& tag_map,
std::set<std::string> names) {
auto tag_index_names = tag_map.CanonicalEntries();
for (auto id = tag_map.EndId() - 1; id >= tag_map.BeginId(); --id) {
std::string name = tag_map.Names()[id.value()];
if (names.count(name) > 0) {
tag_index_names.erase(tag_index_names.begin() + id.value());
}
}
return tool::TagMap::Create(tag_index_names).value();
}
// Copies matching entries from another Collection.
template <class CollectionType>
void CopyCollection(const CollectionType& other, CollectionType* result) {
auto tag_map = result->TagMap();
for (auto id = tag_map->BeginId(); id != tag_map->EndId(); ++id) {
auto tag_index = tag_map->TagAndIndexFromId(id);
auto other_id = other.GetId(tag_index.first, tag_index.second);
if (other_id.IsValid()) {
result->Get(id) = other.Get(other_id);
}
}
}
// Copies packet types omitting entries that are optional and not provided.
std::unique_ptr<PacketTypeSet> RemoveOmittedPacketTypes(
const PacketTypeSet& packet_types,
const std::map<std::string, Packet>& all_side_packets,
const ValidatedGraphConfig* validated_graph) {
std::set<std::string> omitted_names;
for (auto id = packet_types.BeginId(); id != packet_types.EndId(); ++id) {
std::string name = packet_types.TagMap()->Names()[id.value()];
if (packet_types.Get(id).IsOptional() &&
validated_graph->IsExternalSidePacket(name) &&
all_side_packets.count(name) == 0) {
omitted_names.insert(name);
}
}
auto tag_map = RemoveNames(*packet_types.TagMap(), omitted_names);
auto result = std::make_unique<PacketTypeSet>(tag_map);
CopyCollection(packet_types, result.get());
return result;
}
} // namespace
CalculatorNode::CalculatorNode() {}
@@ -72,7 +118,7 @@ Timestamp CalculatorNode::SourceProcessOrder(
return calculator_->SourceProcessOrder(cc);
}
mediapipe::Status CalculatorNode::Initialize(
absl::Status CalculatorNode::Initialize(
const ValidatedGraphConfig* validated_graph, int node_id,
InputStreamManager* input_stream_managers,
OutputStreamManager* output_stream_managers,
@@ -158,7 +204,7 @@ mediapipe::Status CalculatorNode::Initialize(
return InitializeInputStreams(input_stream_managers, output_stream_managers);
}
mediapipe::Status CalculatorNode::InitializeOutputSidePackets(
absl::Status CalculatorNode::InitializeOutputSidePackets(
const PacketTypeSet& output_side_packet_types,
OutputSidePacketImpl* output_side_packets) {
output_side_packets_ =
@@ -172,10 +218,10 @@ mediapipe::Status CalculatorNode::InitializeOutputSidePackets(
output_side_packets_->GetPtr(id) =
&output_side_packets[base_index + id.value()];
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorNode::InitializeInputSidePackets(
absl::Status CalculatorNode::InitializeInputSidePackets(
OutputSidePacketImpl* output_side_packets) {
const NodeTypeInfo& node_type_info =
validated_graph_->CalculatorInfos()[node_id_];
@@ -200,10 +246,10 @@ mediapipe::Status CalculatorNode::InitializeInputSidePackets(
<< output_side_packet_index;
origin_output_side_packet->AddMirror(&input_side_packet_handler_, id);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorNode::InitializeOutputStreams(
absl::Status CalculatorNode::InitializeOutputStreams(
OutputStreamManager* output_stream_managers) {
RET_CHECK(output_stream_managers) << "output_stream_managers is NULL";
const NodeTypeInfo& node_type_info =
@@ -215,7 +261,7 @@ mediapipe::Status CalculatorNode::InitializeOutputStreams(
current_output_stream_managers);
}
mediapipe::Status CalculatorNode::InitializeInputStreams(
absl::Status CalculatorNode::InitializeInputStreams(
InputStreamManager* input_stream_managers,
OutputStreamManager* output_stream_managers) {
RET_CHECK(input_stream_managers) << "input_stream_managers is NULL";
@@ -246,10 +292,10 @@ mediapipe::Status CalculatorNode::InitializeInputStreams(
<< output_stream_index;
origin_output_stream_manager->AddMirror(input_stream_handler_.get(), id);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorNode::InitializeInputStreamHandler(
absl::Status CalculatorNode::InitializeInputStreamHandler(
const InputStreamHandlerConfig& handler_config,
const PacketTypeSet& input_stream_types) {
const ProtoString& input_stream_handler_name =
@@ -264,10 +310,10 @@ mediapipe::Status CalculatorNode::InitializeInputStreamHandler(
_ << "\"" << input_stream_handler_name
<< "\" is not a registered input stream handler.");
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorNode::InitializeOutputStreamHandler(
absl::Status CalculatorNode::InitializeOutputStreamHandler(
const OutputStreamHandlerConfig& handler_config,
const PacketTypeSet& output_stream_types) {
const ProtoString& output_stream_handler_name =
@@ -281,10 +327,10 @@ mediapipe::Status CalculatorNode::InitializeOutputStreamHandler(
/*calculator_run_in_parallel=*/max_in_flight_ > 1),
_ << "\"" << output_stream_handler_name
<< "\" is not a registered output stream handler.");
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorNode::ConnectShardsToStreams(
absl::Status CalculatorNode::ConnectShardsToStreams(
CalculatorContext* calculator_context) {
RET_CHECK(calculator_context);
MP_RETURN_IF_ERROR(
@@ -324,13 +370,13 @@ void CalculatorNode::SetMaxInputStreamQueueSize(int max_queue_size) {
input_stream_handler_->SetMaxQueueSize(max_queue_size);
}
mediapipe::Status CalculatorNode::PrepareForRun(
absl::Status CalculatorNode::PrepareForRun(
const std::map<std::string, Packet>& all_side_packets,
const std::map<std::string, Packet>& service_packets,
std::function<void()> ready_for_open_callback,
std::function<void()> source_node_opened_callback,
std::function<void(CalculatorContext*)> schedule_callback,
std::function<void(mediapipe::Status)> error_callback,
std::function<void(absl::Status)> error_callback,
CounterFactory* counter_factory) {
RET_CHECK(ready_for_open_callback) << "ready_for_open_callback is NULL";
RET_CHECK(schedule_callback) << "schedule_callback is NULL";
@@ -345,10 +391,12 @@ mediapipe::Status CalculatorNode::PrepareForRun(
std::move(schedule_callback), error_callback);
output_stream_handler_->PrepareForRun(error_callback);
const PacketTypeSet* input_side_packet_types =
const PacketTypeSet* packet_types =
&validated_graph_->CalculatorInfos()[node_id_].InputSidePacketTypes();
input_side_packet_types_ = RemoveOmittedPacketTypes(
*packet_types, all_side_packets, validated_graph_);
MP_RETURN_IF_ERROR(input_side_packet_handler_.PrepareForRun(
input_side_packet_types, all_side_packets,
input_side_packet_types_.get(), all_side_packets,
[this]() { CalculatorNode::InputSidePacketsReady(); },
std::move(error_callback)));
calculator_state_->SetInputSidePackets(
@@ -394,7 +442,7 @@ mediapipe::Status CalculatorNode::PrepareForRun(
input_side_packets_ready_ =
(input_side_packet_handler_.MissingInputSidePacketCount() == 0);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
namespace {
@@ -406,7 +454,7 @@ const Packet GetPacket(const OutputSidePacket& out) {
}
// Resends the output-side-packets from the previous graph run.
mediapipe::Status ResendSidePackets(CalculatorContext* cc) {
absl::Status ResendSidePackets(CalculatorContext* cc) {
auto& outs = cc->OutputSidePackets();
for (CollectionItemId id = outs.BeginId(); id < outs.EndId(); ++id) {
Packet packet = GetPacket(outs.Get(id));
@@ -415,7 +463,7 @@ mediapipe::Status ResendSidePackets(CalculatorContext* cc) {
outs.Get(id).Set(packet);
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
} // namespace
@@ -429,7 +477,7 @@ bool CalculatorNode::OutputsAreConstant(CalculatorContext* cc) {
return true;
}
mediapipe::Status CalculatorNode::OpenNode() {
absl::Status CalculatorNode::OpenNode() {
VLOG(2) << "CalculatorNode::OpenNode() for " << DebugName();
CalculatorContext* default_context =
@@ -444,7 +492,7 @@ mediapipe::Status CalculatorNode::OpenNode() {
calculator_context_manager_.PushInputTimestampToContext(
default_context, Timestamp::Unstarted());
mediapipe::Status result;
absl::Status result;
if (OutputsAreConstant(default_context)) {
result = ResendSidePackets(default_context);
} else {
@@ -489,7 +537,7 @@ mediapipe::Status CalculatorNode::OpenNode() {
status_ = kStateOpened;
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
void CalculatorNode::ActivateNode() {
@@ -523,8 +571,8 @@ void CalculatorNode::CloseOutputStreams(OutputStreamShardSet* outputs) {
output_stream_handler_->Close(outputs);
}
mediapipe::Status CalculatorNode::CloseNode(
const mediapipe::Status& graph_status, bool graph_run_ended) {
absl::Status CalculatorNode::CloseNode(const absl::Status& graph_status,
bool graph_run_ended) {
{
absl::MutexLock status_lock(&status_mutex_);
RET_CHECK_NE(status_, kStateClosed)
@@ -544,11 +592,11 @@ mediapipe::Status CalculatorNode::CloseNode(
calculator_context_manager_.SetGraphStatusInContext(default_context,
graph_status);
mediapipe::Status result;
absl::Status result;
if (OutputsAreConstant(default_context)) {
// Do nothing.
result = mediapipe::OkStatus();
result = absl::OkStatus();
} else {
MEDIAPIPE_PROFILING(CLOSE, default_context);
LegacyCalculatorSupport::Scoped<CalculatorContext> s(default_context);
@@ -578,10 +626,10 @@ mediapipe::Status CalculatorNode::CloseNode(
"Calculator::Close() for node \"$0\" failed: ", DebugName());
VLOG(2) << "Closed node " << DebugName();
return mediapipe::OkStatus();
return absl::OkStatus();
}
void CalculatorNode::CleanupAfterRun(const mediapipe::Status& graph_status) {
void CalculatorNode::CleanupAfterRun(const absl::Status& graph_status) {
if (needs_to_close_) {
calculator_context_manager_.PushInputTimestampToContext(
calculator_context_manager_.GetDefaultCalculatorContext(),
@@ -750,12 +798,12 @@ std::string CalculatorNode::DebugName() const {
}
// TODO: Split this function.
mediapipe::Status CalculatorNode::ProcessNode(
absl::Status CalculatorNode::ProcessNode(
CalculatorContext* calculator_context) {
if (IsSource()) {
// This is a source Calculator.
if (Closed()) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
const Timestamp input_timestamp = calculator_context->InputTimestamp();
@@ -764,7 +812,7 @@ mediapipe::Status CalculatorNode::ProcessNode(
output_stream_handler_->PrepareOutputs(input_timestamp, outputs);
VLOG(2) << "Calling Calculator::Process() for node: " << DebugName();
mediapipe::Status result;
absl::Status result;
{
MEDIAPIPE_PROFILING(PROCESS, calculator_context);
@@ -787,15 +835,15 @@ mediapipe::Status CalculatorNode::ProcessNode(
output_stream_handler_->PostProcess(input_timestamp);
if (node_stopped) {
MP_RETURN_IF_ERROR(
CloseNode(mediapipe::OkStatus(), /*graph_run_ended=*/false));
CloseNode(absl::OkStatus(), /*graph_run_ended=*/false));
}
return mediapipe::OkStatus();
return absl::OkStatus();
} else {
// This is not a source Calculator.
InputStreamShardSet* const inputs = &calculator_context->Inputs();
OutputStreamShardSet* const outputs = &calculator_context->Outputs();
mediapipe::Status result =
mediapipe::InternalError("Calculator context has no input packets.");
absl::Status result =
absl::InternalError("Calculator context has no input packets.");
int num_invocations = calculator_context_manager_.NumberOfContextTimestamps(
*calculator_context);
@@ -814,7 +862,7 @@ mediapipe::Status CalculatorNode::ProcessNode(
if (OutputsAreConstant(calculator_context)) {
// Do nothing.
result = mediapipe::OkStatus();
result = absl::OkStatus();
} else {
MEDIAPIPE_PROFILING(PROCESS, calculator_context);
LegacyCalculatorSupport::Scoped<CalculatorContext> s(
@@ -851,7 +899,7 @@ mediapipe::Status CalculatorNode::ProcessNode(
CHECK_EQ(calculator_context_manager_.NumberOfContextTimestamps(
*calculator_context),
1);
return CloseNode(mediapipe::OkStatus(), /*graph_run_ended=*/false);
return CloseNode(absl::OkStatus(), /*graph_run_ended=*/false);
} else {
RET_CHECK_FAIL()
<< "Invalid input timestamp in ProcessNode(). timestamp: "
+23 -21
View File
@@ -95,7 +95,7 @@ class CalculatorNode {
void SetExecutor(const std::string& executor);
// Calls Process() on the Calculator corresponding to this node.
mediapipe::Status ProcessNode(CalculatorContext* calculator_context);
absl::Status ProcessNode(CalculatorContext* calculator_context);
// Initializes the node. The buffer_size_hint argument is
// set to the value specified in the graph proto for this field.
@@ -105,12 +105,13 @@ class CalculatorNode {
// output_side_packets is expected to point to a contiguous flat array with
// OutputSidePacketImpls corresponding to the output side packet indexes in
// validated_graph.
mediapipe::Status Initialize(
const ValidatedGraphConfig* validated_graph, int node_id,
InputStreamManager* input_stream_managers,
OutputStreamManager* output_stream_managers,
OutputSidePacketImpl* output_side_packets, int* buffer_size_hint,
std::shared_ptr<ProfilingContext> profiling_context);
absl::Status Initialize(const ValidatedGraphConfig* validated_graph,
int node_id,
InputStreamManager* input_stream_managers,
OutputStreamManager* output_stream_managers,
OutputSidePacketImpl* output_side_packets,
int* buffer_size_hint,
std::shared_ptr<ProfilingContext> profiling_context);
// Sets up the node at the beginning of CalculatorGraph::Run(). This
// method is executed before any OpenNode() calls to the nodes
@@ -121,22 +122,22 @@ class CalculatorNode {
// can be scheduled. source_node_opened_callback is called when a source
// node is opened. schedule_callback is passed to the InputStreamHandler
// and is called each time a new invocation can be scheduled.
mediapipe::Status PrepareForRun(
absl::Status PrepareForRun(
const std::map<std::string, Packet>& all_side_packets,
const std::map<std::string, Packet>& service_packets,
std::function<void()> ready_for_open_callback,
std::function<void()> source_node_opened_callback,
std::function<void(CalculatorContext*)> schedule_callback,
std::function<void(mediapipe::Status)> error_callback,
std::function<void(absl::Status)> error_callback,
CounterFactory* counter_factory) ABSL_LOCKS_EXCLUDED(status_mutex_);
// Opens the node.
mediapipe::Status OpenNode() ABSL_LOCKS_EXCLUDED(status_mutex_);
absl::Status OpenNode() ABSL_LOCKS_EXCLUDED(status_mutex_);
// Called when a source node's layer becomes active.
void ActivateNode() ABSL_LOCKS_EXCLUDED(status_mutex_);
// Cleans up the node after the CalculatorGraph has been run. Deletes
// the Calculator managed by this node. graph_status is the status of
// the graph run.
void CleanupAfterRun(const mediapipe::Status& graph_status)
void CleanupAfterRun(const absl::Status& graph_status)
ABSL_LOCKS_EXCLUDED(status_mutex_);
// Returns true iff PrepareForRun() has been called (and types verified).
@@ -218,8 +219,7 @@ class CalculatorNode {
// Closes the node's calculator and input and output streams.
// graph_status is the current status of the graph run. graph_run_ended
// indicates whether the graph run has ended.
mediapipe::Status CloseNode(const mediapipe::Status& graph_status,
bool graph_run_ended)
absl::Status CloseNode(const absl::Status& graph_status, bool graph_run_ended)
ABSL_LOCKS_EXCLUDED(status_mutex_);
// Returns a pointer to the default calculator context that is used for
@@ -235,35 +235,34 @@ class CalculatorNode {
private:
// Sets up the output side packets from the master flat array.
mediapipe::Status InitializeOutputSidePackets(
absl::Status InitializeOutputSidePackets(
const PacketTypeSet& output_side_packet_types,
OutputSidePacketImpl* output_side_packets);
// Connects the input side packets as mirrors on the output side packets.
// Output side packets are looked up in the master flat array which is
// provided.
mediapipe::Status InitializeInputSidePackets(
absl::Status InitializeInputSidePackets(
OutputSidePacketImpl* output_side_packets);
// Sets up the output streams from the master flat array.
mediapipe::Status InitializeOutputStreams(
absl::Status InitializeOutputStreams(
OutputStreamManager* output_stream_managers);
// Sets up the input streams and connects them as mirrors on the
// output streams. Both input streams and output streams are looked
// up in the master flat arrays which are provided.
mediapipe::Status InitializeInputStreams(
absl::Status InitializeInputStreams(
InputStreamManager* input_stream_managers,
OutputStreamManager* output_stream_managers);
mediapipe::Status InitializeInputStreamHandler(
absl::Status InitializeInputStreamHandler(
const InputStreamHandlerConfig& handler_config,
const PacketTypeSet& input_stream_types);
mediapipe::Status InitializeOutputStreamHandler(
absl::Status InitializeOutputStreamHandler(
const OutputStreamHandlerConfig& handler_config,
const PacketTypeSet& output_stream_types);
// Connects the input/output stream shards in the given calculator context to
// the input/output streams of the node.
mediapipe::Status ConnectShardsToStreams(
CalculatorContext* calculator_context);
absl::Status ConnectShardsToStreams(CalculatorContext* calculator_context);
// The general scheduling logic shared by EndScheduling() and
// CheckIfBecameReady().
@@ -351,6 +350,9 @@ class CalculatorNode {
// Mutex for node status.
mutable absl::Mutex status_mutex_;
// Describes the input side packets required to run this node.
std::unique_ptr<PacketTypeSet> input_side_packet_types_;
// Manages the set of input side packets.
InputSidePacketHandler input_side_packet_handler_;
+15 -15
View File
@@ -37,23 +37,23 @@ class CountCalculator : public CalculatorBase {
CountCalculator() { ++num_constructed_; }
~CountCalculator() override { ++num_destroyed_; }
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
++num_fill_expectations_;
cc->Inputs().Get(cc->Inputs().BeginId()).Set<int>();
cc->Outputs().Get(cc->Outputs().BeginId()).Set<int>();
cc->InputSidePackets().Get(cc->InputSidePackets().BeginId()).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
++num_open_;
// Simulate doing nontrivial work to ensure that the time spent in the
// method will register on streamz each time it is called.
usleep(100);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
++num_process_;
int input_stream_int = cc->Inputs().Get(cc->Inputs().BeginId()).Get<int>();
int side_packet_int =
@@ -65,15 +65,15 @@ class CountCalculator : public CalculatorBase {
// Simulate doing nontrivial work to ensure that the time spent in the
// method will register on streamz each time it is called.
usleep(100);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Close(CalculatorContext* cc) override {
absl::Status Close(CalculatorContext* cc) override {
++num_close_;
// Simulate doing nontrivial work to ensure that the time spent in the
// method will register on streamz each time it is called.
usleep(100);
return mediapipe::OkStatus();
return absl::OkStatus();
}
static int num_constructed_;
@@ -94,7 +94,7 @@ int CountCalculator::num_destroyed_ = 0;
void SourceNodeOpenedNoOp() {}
void CheckFail(const mediapipe::Status& status) {
void CheckFail(const absl::Status& status) {
LOG(FATAL) << "The test triggered the error callback with status: " << status;
}
@@ -165,7 +165,7 @@ class CalculatorNodeTest : public ::testing::Test {
&buffer_size_hint_, graph_profiler_));
}
mediapipe::Status PrepareNodeForRun() {
absl::Status PrepareNodeForRun() {
return node_->PrepareForRun( //
input_side_packets_, //
service_packets_, //
@@ -180,7 +180,7 @@ class CalculatorNodeTest : public ::testing::Test {
nullptr);
}
mediapipe::Status InitializeStreams() {
absl::Status InitializeStreams() {
// START OF: code is copied from
// CalculatorGraph::InitializePacketGeneratorGraph.
// Create and initialize the output side packets.
@@ -220,7 +220,7 @@ class CalculatorNodeTest : public ::testing::Test {
stream_a_manager_ = &output_stream_managers_[1];
stream_b_manager_ = &output_stream_managers_[2];
return mediapipe::OkStatus();
return absl::OkStatus();
}
virtual void SimulateParentOpenNode() { stream_a_manager_->LockIntroData(); }
@@ -482,7 +482,7 @@ TEST_F(CalculatorNodeTest, CleanupAfterRun) {
node_->EndScheduling();
// The max parallelism is already reached.
EXPECT_FALSE(node_->TryToBeginScheduling());
node_->CleanupAfterRun(mediapipe::OkStatus());
node_->CleanupAfterRun(absl::OkStatus());
EXPECT_FALSE(node_->Prepared());
EXPECT_FALSE(node_->Opened());
@@ -517,7 +517,7 @@ void CalculatorNodeTest::TestCleanupAfterRunTwice() {
EXPECT_TRUE(node_->TryToBeginScheduling());
MP_EXPECT_OK(node_->ProcessNode(cc_));
node_->EndScheduling();
node_->CleanupAfterRun(mediapipe::OkStatus());
node_->CleanupAfterRun(absl::OkStatus());
stream_a_manager_->PrepareForRun(nullptr);
@@ -543,7 +543,7 @@ void CalculatorNodeTest::TestCleanupAfterRunTwice() {
node_->EndScheduling();
// The max parallelism is already reached.
EXPECT_FALSE(node_->TryToBeginScheduling());
node_->CleanupAfterRun(mediapipe::OkStatus());
node_->CleanupAfterRun(absl::OkStatus());
EXPECT_FALSE(node_->Prepared());
EXPECT_FALSE(node_->Opened());
@@ -37,7 +37,6 @@ option java_outer_classname = "CalculatorOptionsProto";
message CalculatorOptions {
// If true, this proto specifies a subset of field values,
// which should override corresponding field values.
// Deprecated in cl/228195782.
optional bool merge_fields = 1 [deprecated = true];
extensions 20000 to max;
@@ -50,20 +50,20 @@ inline void BusySleep(absl::Duration duration) {
class SlowPlusOneCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Outputs().Index(0).Set<int>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
cc->SetOffset(mediapipe::TimestampDiff(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
if (cc->InputTimestamp().Value() % 4 == 0) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
RandomEngine random(testing::UnitTest::GetInstance()->random_seed());
@@ -71,7 +71,7 @@ class SlowPlusOneCalculator : public CalculatorBase {
BusySleep(absl::Milliseconds(90 + uniform_dist(random)));
cc->Outputs().Index(0).Add(new int(cc->Inputs().Index(0).Get<int>() + 1),
cc->InputTimestamp());
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
@@ -124,7 +124,7 @@ TEST_F(ParallelExecutionTest, SlowPlusOneCalculatorsTest) {
const int kTotalNums = 100;
int fail_count = 0;
for (int i = 0; i < kTotalNums; ++i) {
mediapipe::Status status = graph.AddPacketToInputStream(
absl::Status status = graph.AddPacketToInputStream(
"input", Adopt(new int(i)).At(Timestamp(i)));
if (!status.ok()) {
++fail_count;
+18 -18
View File
@@ -36,15 +36,15 @@ namespace {
// Input side packets: 1, pointing to CalculatorRunner::StreamContents.
class CalculatorRunnerSourceCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->InputSidePackets()
.Index(0)
.Set<const CalculatorRunner::StreamContents*>();
cc->Outputs().Index(0).SetAny();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
const auto* contents = cc->InputSidePackets()
.Index(0)
.Get<const CalculatorRunner::StreamContents*>();
@@ -53,9 +53,9 @@ class CalculatorRunnerSourceCalculator : public CalculatorBase {
for (const Packet& packet : contents->packets) {
cc->Outputs().Index(0).AddPacket(packet);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
return tool::StatusStop();
}
};
@@ -67,23 +67,23 @@ REGISTER_CALCULATOR(CalculatorRunnerSourceCalculator);
// Input side packets: 1, pointing to CalculatorRunner::StreamContents.
class CalculatorRunnerSinkCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).SetAny();
cc->InputSidePackets().Index(0).Set<CalculatorRunner::StreamContents*>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
contents_ = cc->InputSidePackets()
.Index(0)
.Get<CalculatorRunner::StreamContents*>();
contents_->header = cc->Inputs().Index(0).Header();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
contents_->packets.push_back(cc->Inputs().Index(0).Value());
return mediapipe::OkStatus();
return absl::OkStatus();
}
private:
@@ -98,7 +98,7 @@ CalculatorRunner::CalculatorRunner(
MEDIAPIPE_CHECK_OK(InitializeFromNodeConfig(node_config));
}
mediapipe::Status CalculatorRunner::InitializeFromNodeConfig(
absl::Status CalculatorRunner::InitializeFromNodeConfig(
const CalculatorGraphConfig::Node& node_config) {
node_config_ = node_config;
@@ -126,7 +126,7 @@ mediapipe::Status CalculatorRunner::InitializeFromNodeConfig(
tool::TagMap::Create(node_config_.output_side_packet()));
output_side_packets_ = absl::make_unique<PacketSet>(output_side_map);
return mediapipe::OkStatus();
return absl::OkStatus();
}
CalculatorRunner::CalculatorRunner(const std::string& calculator_type,
@@ -220,10 +220,10 @@ std::map<std::string, int64> CalculatorRunner::GetCountersValues() {
return graph_->GetCounterFactory()->GetCounterSet()->GetCountersValues();
}
mediapipe::Status CalculatorRunner::BuildGraph() {
absl::Status CalculatorRunner::BuildGraph() {
if (graph_ != nullptr) {
// The graph was already built.
return mediapipe::OkStatus();
return absl::OkStatus();
}
RET_CHECK(inputs_) << "The inputs were not initialized.";
RET_CHECK(outputs_) << "The outputs were not initialized.";
@@ -277,10 +277,10 @@ mediapipe::Status CalculatorRunner::BuildGraph() {
graph_ = absl::make_unique<CalculatorGraph>();
MP_RETURN_IF_ERROR(graph_->Initialize(config));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status CalculatorRunner::Run() {
absl::Status CalculatorRunner::Run() {
MP_RETURN_IF_ERROR(BuildGraph());
// Set the input side packets for the sources.
std::map<std::string, Packet> input_side_packets;
@@ -352,7 +352,7 @@ mediapipe::Status CalculatorRunner::Run() {
tag, (index == -1) ? ++positional_index : index);
ASSIGN_OR_RETURN(contents, graph_->GetOutputSidePacket(name));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
} // namespace mediapipe
+4 -4
View File
@@ -109,11 +109,11 @@ class CalculatorRunner {
// Runs the calculator, by calling Open(), Process() with the
// inputs provided via mutable_inputs(), and Close(). Returns the
// mediapipe::Status from CalculatorGraph::Run(). Internally, Run()
// absl::Status from CalculatorGraph::Run(). Internally, Run()
// constructs a CalculatorGraph in the first call, and calls
// CalculatorGraph::Run(). A single instance of CalculatorRunner
// uses the same instance of CalculatorGraph for all runs.
mediapipe::Status Run();
absl::Status Run();
// Returns the vector of contents of the output streams. The .header
// field contains the stream header and the .packets field contains
@@ -135,11 +135,11 @@ class CalculatorRunner {
static const char kSinkPrefix[];
// Initialize using a node config (does the constructor's work).
mediapipe::Status InitializeFromNodeConfig(
absl::Status InitializeFromNodeConfig(
const CalculatorGraphConfig::Node& node_config);
// Builds the graph if one does not already exist.
mediapipe::Status BuildGraph();
absl::Status BuildGraph();
CalculatorGraphConfig::Node node_config_;
+10 -10
View File
@@ -40,7 +40,7 @@ namespace {
// at InputTimestamp. The headers are strings.
class CalculatorRunnerTestCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->Inputs().Index(0).Set<int>();
cc->Inputs().Index(1).Set<int>();
cc->Outputs().Index(0).Set<int>();
@@ -50,10 +50,10 @@ class CalculatorRunnerTestCalculator : public CalculatorBase {
cc->OutputSidePackets()
.Tag("SIDE_OUTPUT")
.SetSameAs(&cc->InputSidePackets().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Open(CalculatorContext* cc) override {
absl::Status Open(CalculatorContext* cc) override {
std::string input_header_string =
absl::StrCat(cc->Inputs().Index(0).Header().Get<std::string>(),
cc->Inputs().Index(1).Header().Get<std::string>());
@@ -66,17 +66,17 @@ class CalculatorRunnerTestCalculator : public CalculatorBase {
cc->OutputSidePackets()
.Tag("SIDE_OUTPUT")
.Set(cc->InputSidePackets().Index(0));
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
for (int index = 0; index < 2; ++index) {
cc->Outputs().Index(index).Add(
new int(-cc->Inputs().Index(index).Get<int>()), cc->InputTimestamp());
}
cc->Outputs().Index(2).AddPacket(
cc->InputSidePackets().Index(0).At(cc->InputTimestamp()));
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(CalculatorRunnerTestCalculator);
@@ -87,7 +87,7 @@ REGISTER_CALCULATOR(CalculatorRunnerTestCalculator);
// with the same tag name (and any index).
class CalculatorRunnerMultiTagTestCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
for (const std::string& tag : cc->Inputs().GetTags()) {
for (CollectionItemId item_id = cc->Inputs().BeginId(tag);
item_id < cc->Inputs().EndId(tag); ++item_id) {
@@ -95,10 +95,10 @@ class CalculatorRunnerMultiTagTestCalculator : public CalculatorBase {
}
cc->Outputs().Get(tag, 0).Set<int>();
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) override {
absl::Status Process(CalculatorContext* cc) override {
for (const std::string& tag : cc->Inputs().GetTags()) {
auto sum = absl::make_unique<int>(0);
for (CollectionItemId item_id = cc->Inputs().BeginId(tag);
@@ -109,7 +109,7 @@ class CalculatorRunnerMultiTagTestCalculator : public CalculatorBase {
}
cc->Outputs().Get(tag, 0).Add(sum.release(), cc->InputTimestamp());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(CalculatorRunnerMultiTagTestCalculator);
+5
View File
@@ -61,6 +61,11 @@ Counter* CalculatorState::GetCounter(const std::string& name) {
return counter_factory_->GetCounter(absl::StrCat(NodeName(), "-", name));
}
CounterSet* CalculatorState::GetCounterSet() {
CHECK(counter_factory_);
return counter_factory_->GetCounterSet();
}
void CalculatorState::SetServicePacket(const std::string& key, Packet packet) {
service_packets_[key] = std::move(packet);
}
+5
View File
@@ -78,6 +78,11 @@ class CalculatorState {
// name is the passed-in name, prefixed by the calculator NodeName.
Counter* GetCounter(const std::string& name);
// Returns a counter set, which can be passed to other classes, to generate
// counters. NOTE: This differs from GetCounter, in that the counters
// created by this counter set do not have the NodeName prefix.
CounterSet* GetCounterSet();
std::shared_ptr<ProfilingContext> GetSharedProfilingContext() const {
return profiling_context_;
}
+7 -28
View File
@@ -29,6 +29,7 @@
#include "mediapipe/framework/collection_item_id.h"
#include "mediapipe/framework/port/logging.h"
#include "mediapipe/framework/tool/tag_map.h"
#include "mediapipe/framework/tool/tag_map_helper.h"
#include "mediapipe/framework/tool/validate_name.h"
#include "mediapipe/framework/type_map.h"
@@ -379,39 +380,17 @@ Collection<T, storage, ErrorHandler>::Collection(
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(
const tool::TagAndNameInfo& info) {
tag_map_ = std::move(tool::TagMap::Create(info).ValueOrDie());
if (tag_map_->NumEntries() != 0) {
data_ = absl::make_unique<stored_type[]>(tag_map_->NumEntries());
}
}
const tool::TagAndNameInfo& info)
: Collection(tool::TagMap::Create(info).value()) {}
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(const int num_entries) {
proto_ns::RepeatedPtrField<ProtoString> fields;
for (int i = 0; i < num_entries; ++i) {
*fields.Add() = absl::StrCat("name", i);
}
tag_map_ = std::move(tool::TagMap::Create(fields).ValueOrDie());
if (tag_map_->NumEntries() != 0) {
data_ = absl::make_unique<stored_type[]>(tag_map_->NumEntries());
}
}
Collection<T, storage, ErrorHandler>::Collection(const int num_entries)
: Collection(tool::CreateTagMap(num_entries).value()) {}
template <typename T, CollectionStorage storage, typename ErrorHandler>
Collection<T, storage, ErrorHandler>::Collection(
const std::initializer_list<std::string>& tag_names) {
proto_ns::RepeatedPtrField<ProtoString> fields;
int i = 0;
for (const std::string& name : tag_names) {
*fields.Add() = absl::StrCat(name, ":name", i);
++i;
}
tag_map_ = std::move(tool::TagMap::Create(fields).ValueOrDie());
if (tag_map_->NumEntries() != 0) {
data_ = absl::make_unique<stored_type[]>(tag_map_->NumEntries());
}
}
const std::initializer_list<std::string>& tag_names)
: Collection(tool::CreateTagMapFromTags(tag_names).value()) {}
template <typename T, CollectionStorage storage, typename ErrorHandler>
bool Collection<T, storage, ErrorHandler>::UsesTags() const {
+11 -11
View File
@@ -78,7 +78,7 @@ TEST(CollectionTest, MixedTagAndIndexUsage) {
"TAG_C:0:e", "TAG_A:1:f"});
MP_ASSERT_OK(tags_statusor);
internal::Collection<int> collection1(std::move(tags_statusor.ValueOrDie()));
internal::Collection<int> collection1(std::move(tags_statusor.value()));
collection1.Get("TAG_A", 0) = 100;
collection1.Get("TAG_A", 1) = 101;
collection1.Get("TAG_A", 2) = 102;
@@ -165,16 +165,16 @@ TEST(CollectionTest, StaticEmptyCollectionHeapCheck) {
// "new T[0]" returns a non-null pointer which the heap checker has
// issues in tracking. Additionally, allocating of empty arrays is
// also inefficient as it invokes heap management routines.
static auto* collection1 = new PacketSet(tool::CreateTagMap({}).ValueOrDie());
static auto* collection1 = new PacketSet(tool::CreateTagMap({}).value());
// Heap check issues are most triggered when zero length and non-zero
// length allocations are interleaved. Additionally, this heap check
// wasn't triggered by "char", so a more complex type (Packet) is used.
static auto* collection2 =
new PacketSet(tool::CreateTagMap({"TAG:name"}).ValueOrDie());
static auto* collection3 = new PacketSet(tool::CreateTagMap({}).ValueOrDie());
new PacketSet(tool::CreateTagMap({"TAG:name"}).value());
static auto* collection3 = new PacketSet(tool::CreateTagMap({}).value());
static auto* collection4 =
new PacketSet(tool::CreateTagMap({"TAG:name"}).ValueOrDie());
static auto* collection5 = new PacketSet(tool::CreateTagMap({}).ValueOrDie());
new PacketSet(tool::CreateTagMap({"TAG:name"}).value());
static auto* collection5 = new PacketSet(tool::CreateTagMap({}).value());
EXPECT_EQ(0, collection1->NumEntries());
EXPECT_EQ(1, collection2->NumEntries());
EXPECT_EQ(0, collection3->NumEntries());
@@ -183,12 +183,12 @@ TEST(CollectionTest, StaticEmptyCollectionHeapCheck) {
}
template <typename T>
mediapipe::Status TestCollectionWithPointers(
const std::vector<T>& original_values, const T& inject1, const T& inject2) {
absl::Status TestCollectionWithPointers(const std::vector<T>& original_values,
const T& inject1, const T& inject2) {
std::shared_ptr<tool::TagMap> tag_map =
tool::CreateTagMap({"TAG_A:a", "TAG_B:1:b", "TAG_A:2:c", "TAG_B:d",
"TAG_C:0:e", "TAG_A:1:f"})
.ValueOrDie();
.value();
{
// Test a regular collection.
@@ -451,7 +451,7 @@ mediapipe::Status TestCollectionWithPointers(
++i;
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
TEST(CollectionTest, TestCollectionWithPointersIntAndString) {
@@ -464,7 +464,7 @@ TEST(CollectionTest, TestIteratorFunctions) {
std::shared_ptr<tool::TagMap> tag_map =
tool::CreateTagMap({"TAG_A:a", "TAG_B:1:b", "TAG_A:2:c", "TAG_B:d",
"TAG_C:0:e", "TAG_A:1:f"})
.ValueOrDie();
.value();
std::vector<std::string> values = {"a0", "a1", "a2", "b0", "b1", "c0"};
internal::Collection<std::string, internal::CollectionStorage::kStorePointer>
+1 -29
View File
@@ -94,7 +94,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
":file_path",
":status",
"//mediapipe/framework/port:status",
"@com_google_absl//absl/strings",
],
)
@@ -257,22 +257,6 @@ cc_library(
],
)
cc_library(
name = "statusor",
srcs = ["statusor.cc"],
hdrs = [
"statusor.h",
"statusor_internals.h",
],
# Use this library through "mediapipe/framework/port:statusor".
visibility = ["//mediapipe/framework/port:__pkg__"],
deps = [
":status",
"//mediapipe/framework/port:logging",
"@com_google_absl//absl/base:core_headers",
],
)
cc_library(
name = "re2",
hdrs = [
@@ -429,18 +413,6 @@ cc_test(
],
)
cc_test(
name = "statusor_test",
size = "small",
srcs = ["statusor_test.cc"],
linkstatic = 1,
deps = [
":status",
":statusor",
"//mediapipe/framework/port:gtest_main",
],
)
cc_test(
name = "topologicalsorter_test",
srcs = ["topologicalsorter_test.cc"],
+28 -28
View File
@@ -22,60 +22,60 @@ namespace mediapipe {
// Each of the functions below creates a canonical error with the given
// message. The error code of the returned status object matches the name of
// the function.
inline mediapipe::Status AlreadyExistsError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kAlreadyExists, message);
inline absl::Status AlreadyExistsError(absl::string_view message) {
return absl::Status(absl::StatusCode::kAlreadyExists, message);
}
inline mediapipe::Status CancelledError() {
return mediapipe::Status(mediapipe::StatusCode::kCancelled, "");
inline absl::Status CancelledError() {
return absl::Status(absl::StatusCode::kCancelled, "");
}
inline mediapipe::Status CancelledError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kCancelled, message);
inline absl::Status CancelledError(absl::string_view message) {
return absl::Status(absl::StatusCode::kCancelled, message);
}
inline mediapipe::Status InternalError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kInternal, message);
inline absl::Status InternalError(absl::string_view message) {
return absl::Status(absl::StatusCode::kInternal, message);
}
inline mediapipe::Status InvalidArgumentError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kInvalidArgument, message);
inline absl::Status InvalidArgumentError(absl::string_view message) {
return absl::Status(absl::StatusCode::kInvalidArgument, message);
}
inline mediapipe::Status FailedPreconditionError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kFailedPrecondition, message);
inline absl::Status FailedPreconditionError(absl::string_view message) {
return absl::Status(absl::StatusCode::kFailedPrecondition, message);
}
inline mediapipe::Status NotFoundError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kNotFound, message);
inline absl::Status NotFoundError(absl::string_view message) {
return absl::Status(absl::StatusCode::kNotFound, message);
}
inline mediapipe::Status OutOfRangeError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kOutOfRange, message);
inline absl::Status OutOfRangeError(absl::string_view message) {
return absl::Status(absl::StatusCode::kOutOfRange, message);
}
inline mediapipe::Status PermissionDeniedError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kPermissionDenied, message);
inline absl::Status PermissionDeniedError(absl::string_view message) {
return absl::Status(absl::StatusCode::kPermissionDenied, message);
}
inline mediapipe::Status UnimplementedError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kUnimplemented, message);
inline absl::Status UnimplementedError(absl::string_view message) {
return absl::Status(absl::StatusCode::kUnimplemented, message);
}
inline mediapipe::Status UnknownError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kUnknown, message);
inline absl::Status UnknownError(absl::string_view message) {
return absl::Status(absl::StatusCode::kUnknown, message);
}
inline mediapipe::Status UnavailableError(absl::string_view message) {
return mediapipe::Status(mediapipe::StatusCode::kUnavailable, message);
inline absl::Status UnavailableError(absl::string_view message) {
return absl::Status(absl::StatusCode::kUnavailable, message);
}
inline bool IsCancelled(const mediapipe::Status& status) {
return status.code() == mediapipe::StatusCode::kCancelled;
inline bool IsCancelled(const absl::Status& status) {
return status.code() == absl::StatusCode::kCancelled;
}
inline bool IsNotFound(const mediapipe::Status& status) {
return status.code() == mediapipe::StatusCode::kNotFound;
inline bool IsNotFound(const absl::Status& status) {
return status.code() == absl::StatusCode::kNotFound;
}
} // namespace mediapipe
+25 -25
View File
@@ -26,11 +26,11 @@
#include <cerrno>
#include "mediapipe/framework/deps/canonical_errors.h"
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/deps/status_builder.h"
#include "mediapipe/framework/deps/status_macros.h"
#include "mediapipe/framework/port/canonical_errors.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/status_builder.h"
#include "mediapipe/framework/port/status_macros.h"
namespace mediapipe {
namespace file {
@@ -138,8 +138,8 @@ class DirectoryListing {
} // namespace
mediapipe::Status GetContents(absl::string_view file_name, std::string* output,
bool read_as_binary) {
absl::Status GetContents(absl::string_view file_name, std::string* output,
bool read_as_binary) {
FILE* fp = fopen(file_name.data(), read_as_binary ? "rb" : "r");
if (fp == NULL) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
@@ -157,11 +157,11 @@ mediapipe::Status GetContents(absl::string_view file_name, std::string* output,
output->append(std::string(buf, ret));
}
fclose(fp);
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content) {
absl::Status SetContents(absl::string_view file_name,
absl::string_view content) {
FILE* fp = fopen(file_name.data(), "w");
if (fp == NULL) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
@@ -175,12 +175,12 @@ mediapipe::Status SetContents(absl::string_view file_name,
<< "Error while writing file: " << file_name
<< ". Error message: " << strerror(write_error);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status MatchInTopSubdirectories(const std::string& parent_directory,
const std::string& file_name,
std::vector<std::string>* results) {
absl::Status MatchInTopSubdirectories(const std::string& parent_directory,
const std::string& file_name,
std::vector<std::string>* results) {
DirectoryListing parent_listing(parent_directory);
while (parent_listing.HasNextEntry()) {
@@ -194,12 +194,12 @@ mediapipe::Status MatchInTopSubdirectories(const std::string& parent_directory,
}
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status MatchFileTypeInDirectory(const std::string& directory,
const std::string& file_suffix,
std::vector<std::string>* results) {
absl::Status MatchFileTypeInDirectory(const std::string& directory,
const std::string& file_suffix,
std::vector<std::string>* results) {
DirectoryListing directory_listing(directory);
while (directory_listing.HasNextEntry()) {
@@ -209,21 +209,21 @@ mediapipe::Status MatchFileTypeInDirectory(const std::string& directory,
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Exists(absl::string_view file_name) {
absl::Status Exists(absl::string_view file_name) {
struct stat buffer;
int status;
status = stat(std::string(file_name).c_str(), &buffer);
if (status == 0) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
switch (errno) {
case EACCES:
return mediapipe::PermissionDeniedError("Insufficient permissions.");
default:
return mediapipe::NotFoundError("The path does not exist.");
return absl::NotFoundError("The path does not exist.");
}
}
@@ -235,9 +235,9 @@ int mkdir(std::string path) {
int mkdir(std::string path) { return _mkdir(path.c_str()); }
#endif
mediapipe::Status RecursivelyCreateDir(absl::string_view path) {
absl::Status RecursivelyCreateDir(absl::string_view path) {
if (path.empty() || Exists(path).ok()) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
auto split_path = file::SplitPath(path);
MP_RETURN_IF_ERROR(RecursivelyCreateDir(split_path.first));
@@ -246,10 +246,10 @@ mediapipe::Status RecursivelyCreateDir(absl::string_view path) {
case EACCES:
return mediapipe::PermissionDeniedError("Insufficient permissions.");
default:
return mediapipe::UnavailableError("Failed to create directory.");
return absl::UnavailableError("Failed to create directory.");
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
} // namespace file
+13 -13
View File
@@ -16,27 +16,27 @@
#define MEDIAPIPE_DEPS_FILE_HELPERS_H_
#include "absl/strings/match.h"
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/port/status.h"
namespace mediapipe {
namespace file {
mediapipe::Status GetContents(absl::string_view file_name, std::string* output,
bool read_as_binary = true);
absl::Status GetContents(absl::string_view file_name, std::string* output,
bool read_as_binary = true);
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content);
absl::Status SetContents(absl::string_view file_name,
absl::string_view content);
mediapipe::Status MatchInTopSubdirectories(const std::string& parent_directory,
const std::string& file_name,
std::vector<std::string>* results);
absl::Status MatchInTopSubdirectories(const std::string& parent_directory,
const std::string& file_name,
std::vector<std::string>* results);
mediapipe::Status MatchFileTypeInDirectory(const std::string& directory,
const std::string& file_suffix,
std::vector<std::string>* results);
absl::Status MatchFileTypeInDirectory(const std::string& directory,
const std::string& file_suffix,
std::vector<std::string>* results);
mediapipe::Status Exists(absl::string_view file_name);
absl::Status Exists(absl::string_view file_name);
mediapipe::Status RecursivelyCreateDir(absl::string_view path);
absl::Status RecursivelyCreateDir(absl::string_view path);
} // namespace file
} // namespace mediapipe
+10 -11
View File
@@ -67,7 +67,7 @@ namespace mediapipe {
// class Client {};
//
// using ClientRegistry =
// GlobalFactoryRegistry<mediapipe::StatusOr<unique_ptr<Client>>;
// GlobalFactoryRegistry<absl::StatusOr<unique_ptr<Client>>;
//
// class MyClient : public Client {
// public:
@@ -84,7 +84,7 @@ namespace mediapipe {
// ::my_ns::MyClient,
// []() {
// auto backend = absl::make_unique<Backend>("/path/to/backend");
// const mediapipe::Status status = backend->Init();
// const absl::Status status = backend->Init();
// if (!status.ok()) {
// return status;
// }
@@ -95,13 +95,13 @@ namespace mediapipe {
//
// === Using the registry to create instances ==============================
//
// // Registry will return mediapipe::StatusOr<Object>
// mediapipe::StatusOr<unique_ptr<Widget>> s_or_widget =
// // Registry will return absl::StatusOr<Object>
// absl::StatusOr<unique_ptr<Widget>> s_or_widget =
// WidgetRegistry::CreateByName(
// "my_ns.MyWidget", std::move(gadget), thing);
// // Registry will return NOT_FOUND if the name is unknown.
// if (!s_or_widget.ok()) ... // handle error
// DoStuffWithWidget(std::move(s_or_widget).ValueOrDie());
// DoStuffWithWidget(std::move(s_or_widget).value());
//
// // It's also possible to find an instance by name within a source namespace.
// auto s_or_widget = WidgetRegistry::CreateByNameInNamespace(
@@ -115,7 +115,7 @@ namespace mediapipe {
// // This might be useful if clients outside of your codebase are registering
// // plugins.
// for (const auto& name : WidgetRegistry::GetRegisteredNames()) {
// mediapipe::StatusOr<unique_ptr<Widget>> s_or_widget =
// absl::StatusOr<unique_ptr<Widget>> s_or_widget =
// WidgetRegistry::CreateByName(name, std::move(gadget), thing);
// ...
// }
@@ -134,13 +134,13 @@ constexpr char kNameSep[] = ".";
template <typename T>
struct WrapStatusOr {
using type = mediapipe::StatusOr<T>;
using type = absl::StatusOr<T>;
};
// Specialization to avoid double-wrapping types that are already StatusOrs.
template <typename T>
struct WrapStatusOr<mediapipe::StatusOr<T>> {
using type = mediapipe::StatusOr<T>;
struct WrapStatusOr<absl::StatusOr<T>> {
using type = absl::StatusOr<T>;
};
} // namespace registration_internal
@@ -196,8 +196,7 @@ class FunctionRegistry {
absl::ReaderMutexLock lock(&lock_);
auto it = functions_.find(name);
if (it == functions_.end()) {
return mediapipe::NotFoundError("No registered object with name: " +
name);
return absl::NotFoundError("No registered object with name: " + name);
}
function = it->second;
}
+1 -1
View File
@@ -31,7 +31,7 @@ mediapipe::StatusBuilder RetCheckFailSlowPath(
mediapipe::StatusBuilder RetCheckFailSlowPath(
mediapipe::source_location location, const char* condition,
const mediapipe::Status& status) {
const absl::Status& status) {
return mediapipe::RetCheckFailSlowPath(location)
<< condition << " returned " << status << " ";
}
+2 -2
View File
@@ -31,9 +31,9 @@ mediapipe::StatusBuilder RetCheckFailSlowPath(
// Returns a StatusBuilder that corresponds to a `RET_CHECK` failure.
mediapipe::StatusBuilder RetCheckFailSlowPath(
mediapipe::source_location location, const char* condition,
const mediapipe::Status& status);
const absl::Status& status);
inline StatusBuilder RetCheckImpl(const mediapipe::Status& status,
inline StatusBuilder RetCheckImpl(const absl::Status& status,
const char* condition,
mediapipe::source_location location) {
if (ABSL_PREDICT_TRUE(status.ok()))
+1 -1
View File
@@ -23,7 +23,7 @@ std::ostream& operator<<(std::ostream& os, const Status& x) {
return os;
}
std::string* MediaPipeCheckOpHelperOutOfLine(const mediapipe::Status& v,
std::string* MediaPipeCheckOpHelperOutOfLine(const absl::Status& v,
const char* msg) {
std::string r("Non-OK-status: ");
r += msg;
+9 -7
View File
@@ -20,22 +20,24 @@
#include <memory>
#include <string>
#include "absl/base/attributes.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "mediapipe/framework/port/logging.h"
namespace mediapipe {
using Status = absl::Status;
using StatusCode = absl::StatusCode;
using Status ABSL_DEPRECATED("Use absl::Status directly") = absl::Status;
using StatusCode ABSL_DEPRECATED("Use absl::StatusCode directly") =
absl::StatusCode;
inline mediapipe::Status OkStatus() { return absl::OkStatus(); }
ABSL_DEPRECATED("Use absl::OkStatus directly")
inline absl::Status OkStatus() { return absl::OkStatus(); }
extern std::string* MediaPipeCheckOpHelperOutOfLine(const mediapipe::Status& v,
extern std::string* MediaPipeCheckOpHelperOutOfLine(const absl::Status& v,
const char* msg);
inline std::string* MediaPipeCheckOpHelper(mediapipe::Status v,
const char* msg) {
inline std::string* MediaPipeCheckOpHelper(absl::Status v, const char* msg) {
if (v.ok()) return nullptr;
return MediaPipeCheckOpHelperOutOfLine(v, msg);
}
@@ -51,7 +53,7 @@ inline std::string* MediaPipeCheckOpHelper(mediapipe::Status v,
#define MEDIAPIPE_DCHECK_OK(val) MEDIAPIPE_CHECK_OK(val)
#else
#define MEDIAPIPE_DCHECK_OK(val) \
while (false && (mediapipe::OkStatus() == (val))) LOG(FATAL)
while (false && (absl::OkStatus() == (val))) LOG(FATAL)
#endif
#define CHECK_OK MEDIAPIPE_CHECK_OK
+1 -1
View File
@@ -68,7 +68,7 @@ StatusBuilder::operator Status() && {
return JoinMessageToStatus();
}
mediapipe::Status StatusBuilder::JoinMessageToStatus() {
absl::Status StatusBuilder::JoinMessageToStatus() {
std::string message;
if (join_style_ == MessageJoinStyle::kAnnotate) {
if (!status_.ok()) {
+14 -15
View File
@@ -30,14 +30,14 @@ class ABSL_MUST_USE_RESULT StatusBuilder {
// Creates a `StatusBuilder` based on an original status. If logging is
// enabled, it will use `location` as the location from which the log message
// occurs. A typical user will call this with `MEDIAPIPE_LOC`.
StatusBuilder(const mediapipe::Status& original_status,
StatusBuilder(const absl::Status& original_status,
mediapipe::source_location location)
: status_(original_status),
line_(location.line()),
file_(location.file_name()),
stream_(new std::ostringstream) {}
StatusBuilder(mediapipe::Status&& original_status,
StatusBuilder(absl::Status&& original_status,
mediapipe::source_location location)
: status_(std::move(original_status)),
line_(location.line()),
@@ -47,14 +47,13 @@ class ABSL_MUST_USE_RESULT StatusBuilder {
// Creates a `StatusBuilder` from a mediapipe status code. If logging is
// enabled, it will use `location` as the location from which the log message
// occurs. A typical user will call this with `MEDIAPIPE_LOC`.
StatusBuilder(mediapipe::StatusCode code, mediapipe::source_location location)
StatusBuilder(absl::StatusCode code, mediapipe::source_location location)
: status_(code, ""),
line_(location.line()),
file_(location.file_name()),
stream_(new std::ostringstream) {}
StatusBuilder(const mediapipe::Status& original_status, const char* file,
int line)
StatusBuilder(const absl::Status& original_status, const char* file, int line)
: status_(original_status),
line_(line),
file_(file),
@@ -78,7 +77,7 @@ class ABSL_MUST_USE_RESULT StatusBuilder {
operator Status() const&;
operator Status() &&;
mediapipe::Status JoinMessageToStatus();
absl::Status JoinMessageToStatus();
private:
// Specifies how to join the error message in the original status and any
@@ -90,7 +89,7 @@ class ABSL_MUST_USE_RESULT StatusBuilder {
};
// The status that the result will be based on.
mediapipe::Status status_;
absl::Status status_;
// The line to record if this file is logged.
int line_;
// Not-owned: The file to record if this status is logged.
@@ -104,39 +103,39 @@ class ABSL_MUST_USE_RESULT StatusBuilder {
inline StatusBuilder AlreadyExistsErrorBuilder(
mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kAlreadyExists, location);
return StatusBuilder(absl::StatusCode::kAlreadyExists, location);
}
inline StatusBuilder FailedPreconditionErrorBuilder(
mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kFailedPrecondition, location);
return StatusBuilder(absl::StatusCode::kFailedPrecondition, location);
}
inline StatusBuilder InternalErrorBuilder(mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kInternal, location);
return StatusBuilder(absl::StatusCode::kInternal, location);
}
inline StatusBuilder InvalidArgumentErrorBuilder(
mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kInvalidArgument, location);
return StatusBuilder(absl::StatusCode::kInvalidArgument, location);
}
inline StatusBuilder NotFoundErrorBuilder(mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kNotFound, location);
return StatusBuilder(absl::StatusCode::kNotFound, location);
}
inline StatusBuilder UnavailableErrorBuilder(
mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kUnavailable, location);
return StatusBuilder(absl::StatusCode::kUnavailable, location);
}
inline StatusBuilder UnimplementedErrorBuilder(
mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kUnimplemented, location);
return StatusBuilder(absl::StatusCode::kUnimplemented, location);
}
inline StatusBuilder UnknownErrorBuilder(mediapipe::source_location location) {
return StatusBuilder(mediapipe::StatusCode::kUnknown, location);
return StatusBuilder(absl::StatusCode::kUnknown, location);
}
} // namespace mediapipe
+23 -25
View File
@@ -19,54 +19,52 @@
namespace mediapipe {
TEST(StatusBuilder, AnnotateMode) {
mediapipe::Status status =
StatusBuilder(mediapipe::Status(mediapipe::StatusCode::kNotFound,
"original message"),
MEDIAPIPE_LOC)
<< "annotated message1 "
<< "annotated message2";
absl::Status status = StatusBuilder(absl::Status(absl::StatusCode::kNotFound,
"original message"),
MEDIAPIPE_LOC)
<< "annotated message1 "
<< "annotated message2";
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kNotFound);
EXPECT_EQ(status.code(), absl::StatusCode::kNotFound);
EXPECT_EQ(status.message(),
"original message; annotated message1 annotated message2");
}
TEST(StatusBuilder, PrependMode) {
mediapipe::Status status =
StatusBuilder(mediapipe::Status(mediapipe::StatusCode::kInvalidArgument,
"original message"),
MEDIAPIPE_LOC)
absl::Status status =
StatusBuilder(
absl::Status(absl::StatusCode::kInvalidArgument, "original message"),
MEDIAPIPE_LOC)
.SetPrepend()
<< "prepended message1 "
<< "prepended message2 ";
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kInvalidArgument);
EXPECT_EQ(status.code(), absl::StatusCode::kInvalidArgument);
EXPECT_EQ(status.message(),
"prepended message1 prepended message2 original message");
}
TEST(StatusBuilder, AppendMode) {
mediapipe::Status status =
StatusBuilder(mediapipe::Status(mediapipe::StatusCode::kInternal,
"original message"),
MEDIAPIPE_LOC)
.SetAppend()
<< " extra message1"
<< " extra message2";
absl::Status status = StatusBuilder(absl::Status(absl::StatusCode::kInternal,
"original message"),
MEDIAPIPE_LOC)
.SetAppend()
<< " extra message1"
<< " extra message2";
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kInternal);
EXPECT_EQ(status.code(), absl::StatusCode::kInternal);
EXPECT_EQ(status.message(), "original message extra message1 extra message2");
}
TEST(StatusBuilder, NoLoggingMode) {
mediapipe::Status status =
StatusBuilder(mediapipe::Status(mediapipe::StatusCode::kUnavailable,
"original message"),
MEDIAPIPE_LOC)
absl::Status status =
StatusBuilder(
absl::Status(absl::StatusCode::kUnavailable, "original message"),
MEDIAPIPE_LOC)
.SetNoLogging()
<< " extra message";
ASSERT_FALSE(status.ok());
EXPECT_EQ(status.code(), mediapipe::StatusCode::kUnavailable);
EXPECT_EQ(status.code(), absl::StatusCode::kUnavailable);
EXPECT_EQ(status.message(), "original message");
}
+11 -11
View File
@@ -13,7 +13,7 @@
// limitations under the License.
//
// Helper macros and methods to return and propagate errors with
// `mediapipe::Status`.
// `absl::Status`.
//
// The owners of mediapipe do not endorse use of these macros as a good
// programming practice, and would prefer that you write the equivalent C++
@@ -26,14 +26,14 @@
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/deps/status_builder.h"
// Evaluates an expression that produces a `mediapipe::Status`. If the status
// Evaluates an expression that produces a `absl::Status`. If the status
// is not ok, returns it from the current function.
//
// For example:
// mediapipe::Status MultiStepFunction() {
// absl::Status MultiStepFunction() {
// MP_RETURN_IF_ERROR(Function(args...));
// MP_RETURN_IF_ERROR(foo.Method(args...));
// return mediapipe::OkStatus();
// return absl::OkStatus();
// }
//
// The macro ends with a `mediapipe::StatusBuilder` which allows the returned
@@ -41,11 +41,11 @@
// macro will not be evaluated unless there is an error.
//
// For example:
// mediapipe::Status MultiStepFunction() {
// absl::Status MultiStepFunction() {
// MP_RETURN_IF_ERROR(Function(args...)) << "in MultiStepFunction";
// MP_RETURN_IF_ERROR(foo.Method(args...)).Log(base_logging::ERROR)
// << "while processing query: " << query.DebugString();
// return mediapipe::OkStatus();
// return absl::OkStatus();
// }
//
// `mediapipe::StatusBuilder` supports adapting the builder chain using a
@@ -74,12 +74,12 @@
//
// If using this macro inside a lambda, you need to annotate the return type
// to avoid confusion between a `mediapipe::StatusBuilder` and a
// `mediapipe::Status` type. E.g.
// `absl::Status` type. E.g.
//
// []() -> mediapipe::Status {
// []() -> absl::Status {
// MP_RETURN_IF_ERROR(Function(args...));
// MP_RETURN_IF_ERROR(foo.Method(args...));
// return mediapipe::OkStatus();
// return absl::OkStatus();
// }
#define MP_RETURN_IF_ERROR(expr) \
STATUS_MACROS_IMPL_ELSE_BLOCKER_ \
@@ -88,7 +88,7 @@
} else /* NOLINT */ \
return status_macro_internal_adaptor.Consume()
// Executes an expression `rexpr` that returns a `mediapipe::StatusOr<T>`. On
// Executes an expression `rexpr` that returns a `absl::StatusOr<T>`. On
// OK, extracts its value into the variable defined by `lhs`, otherwise returns
// from the current function. By default the error status is returned
// unchanged, but it may be modified by an `error_expression`. If there is an
@@ -165,7 +165,7 @@
(void)_; /* error_expression is allowed to not use this variable */ \
return (error_expression); \
} \
lhs = std::move(statusor).ValueOrDie()
lhs = std::move(statusor).value()
// Internal helper for concatenating macro values.
#define STATUS_MACROS_IMPL_CONCAT_INNER_(x, y) x##y
+2 -2
View File
@@ -50,8 +50,8 @@ inline IsOkMatcher IsOk() { return IsOkMatcher(); }
} // namespace mediapipe
// Macros for testing the results of functions that return mediapipe::Status or
// mediapipe::StatusOr<T> (for any type T).
// Macros for testing the results of functions that return absl::Status or
// absl::StatusOr<T> (for any type T).
#define MP_EXPECT_OK(expression) EXPECT_THAT(expression, mediapipe::IsOk())
#define MP_ASSERT_OK(expression) ASSERT_THAT(expression, mediapipe::IsOk())
+15 -15
View File
@@ -20,7 +20,7 @@
namespace mediapipe {
TEST(Status, OK) {
EXPECT_EQ(OkStatus().code(), mediapipe::StatusCode::kOk);
EXPECT_EQ(OkStatus().code(), absl::StatusCode::kOk);
EXPECT_EQ(OkStatus().message(), "");
MP_EXPECT_OK(OkStatus());
MP_ASSERT_OK(OkStatus());
@@ -30,25 +30,25 @@ TEST(Status, OK) {
}
TEST(DeathStatus, CheckOK) {
Status status(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status status(absl::StatusCode::kInvalidArgument, "Invalid");
ASSERT_DEATH(MEDIAPIPE_CHECK_OK(status), "Invalid");
}
TEST(Status, Set) {
Status status;
status = Status(mediapipe::StatusCode::kCancelled, "Error message");
EXPECT_EQ(status.code(), mediapipe::StatusCode::kCancelled);
status = Status(absl::StatusCode::kCancelled, "Error message");
EXPECT_EQ(status.code(), absl::StatusCode::kCancelled);
EXPECT_EQ(status.message(), "Error message");
}
TEST(Status, Copy) {
Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status a(absl::StatusCode::kInvalidArgument, "Invalid");
Status b(a);
ASSERT_EQ(a.ToString(), b.ToString());
}
TEST(Status, Assign) {
Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status a(absl::StatusCode::kInvalidArgument, "Invalid");
Status b;
b = a;
ASSERT_EQ(a.ToString(), b.ToString());
@@ -58,10 +58,10 @@ TEST(Status, Update) {
Status s;
s.Update(OkStatus());
ASSERT_TRUE(s.ok());
Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status a(absl::StatusCode::kInvalidArgument, "Invalid");
s.Update(a);
ASSERT_EQ(s.ToString(), a.ToString());
Status b(mediapipe::StatusCode::kInternal, "Invalid");
Status b(absl::StatusCode::kInternal, "Invalid");
s.Update(b);
ASSERT_EQ(s.ToString(), a.ToString());
s.Update(OkStatus());
@@ -72,26 +72,26 @@ TEST(Status, Update) {
TEST(Status, EqualsOK) { ASSERT_EQ(OkStatus(), Status()); }
TEST(Status, EqualsSame) {
Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status b(mediapipe::StatusCode::kInvalidArgument, "Invalid");
Status a(absl::StatusCode::kInvalidArgument, "Invalid");
Status b(absl::StatusCode::kInvalidArgument, "Invalid");
ASSERT_EQ(a, b);
}
TEST(Status, EqualsCopy) {
const Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
const Status a(absl::StatusCode::kInvalidArgument, "Invalid");
const Status b = a;
ASSERT_EQ(a, b);
}
TEST(Status, EqualsDifferentCode) {
const Status a(mediapipe::StatusCode::kInvalidArgument, "Invalid");
const Status b(mediapipe::StatusCode::kInternal, "Internal");
const Status a(absl::StatusCode::kInvalidArgument, "Invalid");
const Status b(absl::StatusCode::kInternal, "Internal");
ASSERT_NE(a, b);
}
TEST(Status, EqualsDifferentMessage) {
const Status a(mediapipe::StatusCode::kInvalidArgument, "message");
const Status b(mediapipe::StatusCode::kInvalidArgument, "another");
const Status a(absl::StatusCode::kInvalidArgument, "message");
const Status b(absl::StatusCode::kInvalidArgument, "another");
ASSERT_NE(a, b);
}
-38
View File
@@ -1,38 +0,0 @@
// Copyright 2019 The MediaPipe 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 "mediapipe/framework/deps/statusor.h"
#include "absl/base/attributes.h"
#include "mediapipe/framework/deps/canonical_errors.h"
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/port/logging.h"
namespace mediapipe {
namespace internal_statusor {
void Helper::HandleInvalidStatusCtorArg(mediapipe::Status* status) {
const char* kMessage =
"An OK status is not a valid constructor argument to StatusOr<T>";
LOG(ERROR) << kMessage;
*status = mediapipe::InternalError(kMessage);
}
void Helper::Crash(const mediapipe::Status& status) {
LOG(FATAL) << "Attempting to fetch value instead of handling error "
<< status;
}
} // namespace internal_statusor
} // namespace mediapipe
-331
View File
@@ -1,331 +0,0 @@
// Copyright 2019 The MediaPipe 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.
//
// StatusOr<T> is the union of a Status object and a T
// object. StatusOr models the concept of an object that is either a
// usable value, or an error Status explaining why such a value is
// not present. To this end, StatusOr<T> does not allow its Status
// value to be Status::OK. Furthermore, the value of a StatusOr<T*>
// must not be null. This is enforced by a debug check in most cases,
// but even when it is not, clients must not set the value to null.
//
// The primary use-case for StatusOr<T> is as the return value of a
// function which may fail.
//
// Example client usage for a StatusOr<T>, where T is not a pointer:
//
// mediapipe::StatusOr<float> result = DoBigCalculationThatCouldFail();
// if (result.ok()) {
// float answer = result.ValueOrDie();
// printf("Big calculation yielded: %f", answer);
// } else {
// LOG(ERROR) << result.status();
// }
//
// Example client usage for a StatusOr<T*>:
//
// mediapipe::StatusOr<Foo*> result = FooFactory::MakeNewFoo(arg);
// if (result.ok()) {
// std::unique_ptr<Foo> foo(result.ValueOrDie());
// foo->DoSomethingCool();
// } else {
// LOG(ERROR) << result.status();
// }
//
// Example client usage for a StatusOr<std::unique_ptr<T>>:
//
// mediapipe::StatusOr<std::unique_ptr<Foo>> result =
// FooFactory::MakeNewFoo(arg);
// if (result.ok()) {
// std::unique_ptr<Foo> foo = std::move(result.ValueOrDie());
// foo->DoSomethingCool();
// } else {
// LOG(ERROR) << result.status();
// }
//
// Example factory implementation returning StatusOr<T*>:
//
// mediapipe::StatusOr<Foo*> FooFactory::MakeNewFoo(int arg) {
// if (arg <= 0) {
// return mediapipe::InvalidArgumentError("Arg must be positive");
// } else {
// return new Foo(arg);
// }
// }
//
// Note that the assignment operators require that destroying the currently
// stored value cannot invalidate the argument; in other words, the argument
// cannot be an alias for the current value, or anything owned by the current
// value.
#ifndef MEDIAPIPE_DEPS_DEFAULT_STATUSOR_H_
#define MEDIAPIPE_DEPS_DEFAULT_STATUSOR_H_
#include "absl/base/attributes.h"
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/deps/status_builder.h"
#include "mediapipe/framework/deps/statusor_internals.h"
namespace mediapipe {
#if defined(__clang__)
// Only clang supports warn_unused_result as a type annotation.
template <typename T>
class ABSL_MUST_USE_RESULT StatusOr;
#endif
template <typename T>
class StatusOr : private internal_statusor::StatusOrData<T>,
private internal_statusor::TraitsBase<
std::is_copy_constructible<T>::value,
std::is_move_constructible<T>::value> {
template <typename U>
friend class StatusOr;
typedef internal_statusor::StatusOrData<T> Base;
public:
typedef T element_type;
// Constructs a new StatusOr with Status::UNKNOWN status. This is marked
// 'explicit' to try to catch cases like 'return {};', where people think
// StatusOr<std::vector<int>> will be initialized with an empty vector,
// instead of a Status::UNKNOWN status.
explicit StatusOr();
// StatusOr<T> will be copy constructible/assignable if T is copy
// constructible.
StatusOr(const StatusOr&) = default;
StatusOr& operator=(const StatusOr&) = default;
// StatusOr<T> will be move constructible/assignable if T is move
// constructible.
StatusOr(StatusOr&&) = default;
StatusOr& operator=(StatusOr&&) = default;
// Conversion copy/move constructor, T must be convertible from U.
// TODO: These should not participate in overload resolution if U
// is not convertible to T.
template <typename U>
StatusOr(const StatusOr<U>& other);
template <typename U>
StatusOr(StatusOr<U>&& other);
// Conversion copy/move assignment operator, T must be convertible from U.
template <typename U>
StatusOr& operator=(const StatusOr<U>& other);
template <typename U>
StatusOr& operator=(StatusOr<U>&& other);
// Constructs a new StatusOr with the given value. After calling this
// constructor, calls to ValueOrDie() will succeed, and calls to status() will
// return OK.
//
// NOTE: Not explicit - we want to use StatusOr<T> as a return type
// so it is convenient and sensible to be able to do 'return T()'
// when the return type is StatusOr<T>.
//
// REQUIRES: T is copy constructible.
StatusOr(const T& value);
// Constructs a new StatusOr with the given non-ok status. After calling
// this constructor, calls to ValueOrDie() will CHECK-fail.
//
// NOTE: Not explicit - we want to use StatusOr<T> as a return
// value, so it is convenient and sensible to be able to do 'return
// Status()' when the return type is StatusOr<T>.
//
// REQUIRES: !status.ok(). This requirement is DCHECKed.
// In optimized builds, passing Status::OK() here will have the effect
// of passing mediapipe::StatusCode::kInternal as a fallback.
StatusOr(const mediapipe::Status& status);
StatusOr& operator=(const mediapipe::Status& status);
StatusOr(const mediapipe::StatusBuilder& builder);
StatusOr& operator=(const mediapipe::StatusBuilder& builder);
// TODO: Add operator=(T) overloads.
// Similar to the `const T&` overload.
//
// REQUIRES: T is move constructible.
StatusOr(T&& value);
// RValue versions of the operations declared above.
StatusOr(mediapipe::Status&& status);
StatusOr& operator=(mediapipe::Status&& status);
StatusOr(mediapipe::StatusBuilder&& builder);
StatusOr& operator=(mediapipe::StatusBuilder&& builder);
// Returns this->status().ok()
bool ok() const { return this->status_.ok(); }
// Returns a reference to mediapipe status. If this contains a T, then
// returns Status::OK().
const mediapipe::Status& status() const&;
mediapipe::Status status() &&;
// Returns a reference to our current value, or CHECK-fails if !this->ok().
//
// Note: for value types that are cheap to copy, prefer simple code:
//
// T value = statusor.ValueOrDie();
//
// Otherwise, if the value type is expensive to copy, but can be left
// in the StatusOr, simply assign to a reference:
//
// T& value = statusor.ValueOrDie(); // or `const T&`
//
// Otherwise, if the value type supports an efficient move, it can be
// used as follows:
//
// T value = std::move(statusor).ValueOrDie();
//
// The std::move on statusor instead of on the whole expression enables
// warnings about possible uses of the statusor object after the move.
// C++ style guide waiver for ref-qualified overloads granted in cl/143176389
// See go/ref-qualifiers for more details on such overloads.
const T& ValueOrDie() const&;
T& ValueOrDie() &;
const T&& ValueOrDie() const&&;
T&& ValueOrDie() &&;
T ConsumeValueOrDie() { return std::move(ValueOrDie()); }
// Ignores any errors. This method does nothing except potentially suppress
// complaints from any tools that are checking that errors are not dropped on
// the floor.
void IgnoreError() const;
};
////////////////////////////////////////////////////////////////////////////////
// Implementation details for StatusOr<T>
template <typename T>
StatusOr<T>::StatusOr()
: Base(mediapipe::Status(mediapipe::StatusCode::kUnknown, "")) {}
template <typename T>
StatusOr<T>::StatusOr(const T& value) : Base(value) {}
template <typename T>
StatusOr<T>::StatusOr(const mediapipe::Status& status) : Base(status) {}
template <typename T>
StatusOr<T>::StatusOr(const mediapipe::StatusBuilder& builder)
: Base(builder) {}
template <typename T>
StatusOr<T>& StatusOr<T>::operator=(const mediapipe::Status& status) {
this->Assign(status);
return *this;
}
template <typename T>
StatusOr<T>& StatusOr<T>::operator=(const mediapipe::StatusBuilder& builder) {
return *this = static_cast<mediapipe::Status>(builder);
}
template <typename T>
StatusOr<T>::StatusOr(T&& value) : Base(std::move(value)) {}
template <typename T>
StatusOr<T>::StatusOr(mediapipe::Status&& status) : Base(std::move(status)) {}
template <typename T>
StatusOr<T>::StatusOr(mediapipe::StatusBuilder&& builder)
: Base(std::move(builder)) {}
template <typename T>
StatusOr<T>& StatusOr<T>::operator=(mediapipe::Status&& status) {
this->Assign(std::move(status));
return *this;
}
template <typename T>
StatusOr<T>& StatusOr<T>::operator=(mediapipe::StatusBuilder&& builder) {
return *this = static_cast<mediapipe::Status>(std::move(builder));
}
template <typename T>
template <typename U>
inline StatusOr<T>::StatusOr(const StatusOr<U>& other)
: Base(static_cast<const typename StatusOr<U>::Base&>(other)) {}
template <typename T>
template <typename U>
inline StatusOr<T>& StatusOr<T>::operator=(const StatusOr<U>& other) {
if (other.ok())
this->Assign(other.ValueOrDie());
else
this->Assign(other.status());
return *this;
}
template <typename T>
template <typename U>
inline StatusOr<T>::StatusOr(StatusOr<U>&& other)
: Base(static_cast<typename StatusOr<U>::Base&&>(other)) {}
template <typename T>
template <typename U>
inline StatusOr<T>& StatusOr<T>::operator=(StatusOr<U>&& other) {
if (other.ok()) {
this->Assign(std::move(other).ValueOrDie());
} else {
this->Assign(std::move(other).status());
}
return *this;
}
template <typename T>
const mediapipe::Status& StatusOr<T>::status() const& {
return this->status_;
}
template <typename T>
mediapipe::Status StatusOr<T>::status() && {
return ok() ? mediapipe::OkStatus() : std::move(this->status_);
}
template <typename T>
const T& StatusOr<T>::ValueOrDie() const& {
this->EnsureOk();
return this->data_;
}
template <typename T>
T& StatusOr<T>::ValueOrDie() & {
this->EnsureOk();
return this->data_;
}
template <typename T>
const T&& StatusOr<T>::ValueOrDie() const&& {
this->EnsureOk();
return std::move(this->data_);
}
template <typename T>
T&& StatusOr<T>::ValueOrDie() && {
this->EnsureOk();
return std::move(this->data_);
}
template <typename T>
void StatusOr<T>::IgnoreError() const {
// no-op
}
} // namespace mediapipe
#endif // MEDIAPIPE_DEPS_DEFAULT_STATUSOR_H_
@@ -1,245 +0,0 @@
// Copyright 2019 The MediaPipe 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 MEDIAPIPE_DEPS_STATUSOR_INTERNALS_H_
#define MEDIAPIPE_DEPS_STATUSOR_INTERNALS_H_
#include "absl/base/attributes.h"
#include "mediapipe/framework/deps/status.h"
namespace mediapipe {
namespace internal_statusor {
class Helper {
public:
// Move type-agnostic error handling to the .cc.
static void HandleInvalidStatusCtorArg(mediapipe::Status*);
ABSL_ATTRIBUTE_NORETURN static void Crash(const mediapipe::Status& status);
};
// Construct an instance of T in `p` through placement new, passing Args... to
// the constructor.
// This abstraction is here mostly for the gcc performance fix.
template <typename T, typename... Args>
void PlacementNew(void* p, Args&&... args) {
#if defined(__GNUC__) && !defined(__clang__)
// Teach gcc that 'p' cannot be null, fixing code size issues.
if (p == nullptr) __builtin_unreachable();
#endif
new (p) T(std::forward<Args>(args)...);
}
// Helper base class to hold the data and all operations.
// We move all this to a base class to allow mixing with the appropriate
// TraitsBase specialization.
template <typename T>
class StatusOrData {
template <typename U>
friend class StatusOrData;
public:
StatusOrData() = delete;
StatusOrData(const StatusOrData& other) {
if (other.ok()) {
MakeValue(other.data_);
MakeStatus();
} else {
MakeStatus(other.status_);
}
}
StatusOrData(StatusOrData&& other) noexcept {
if (other.ok()) {
MakeValue(std::move(other.data_));
MakeStatus();
} else {
MakeStatus(std::move(other.status_));
}
}
template <typename U>
StatusOrData(const StatusOrData<U>& other) {
if (other.ok()) {
MakeValue(other.data_);
MakeStatus();
} else {
MakeStatus(other.status_);
}
}
template <typename U>
StatusOrData(StatusOrData<U>&& other) {
if (other.ok()) {
MakeValue(std::move(other.data_));
MakeStatus();
} else {
MakeStatus(std::move(other.status_));
}
}
explicit StatusOrData(const T& value) : data_(value) { MakeStatus(); }
explicit StatusOrData(T&& value) : data_(std::move(value)) { MakeStatus(); }
explicit StatusOrData(const mediapipe::Status& status) : status_(status) {
EnsureNotOk();
}
explicit StatusOrData(mediapipe::Status&& status)
: status_(std::move(status)) {
EnsureNotOk();
}
StatusOrData& operator=(const StatusOrData& other) {
if (this == &other) return *this;
if (other.ok())
Assign(other.data_);
else
Assign(other.status_);
return *this;
}
StatusOrData& operator=(StatusOrData&& other) {
if (this == &other) return *this;
if (other.ok())
Assign(std::move(other.data_));
else
Assign(std::move(other.status_));
return *this;
}
~StatusOrData() {
if (ok()) {
status_.~Status();
data_.~T();
} else {
status_.~Status();
}
}
void Assign(const T& value) {
if (ok()) {
data_.~T();
MakeValue(value);
} else {
MakeValue(value);
status_ = mediapipe::OkStatus();
}
}
void Assign(T&& value) {
if (ok()) {
data_.~T();
MakeValue(std::move(value));
} else {
MakeValue(std::move(value));
status_ = mediapipe::OkStatus();
}
}
void Assign(const mediapipe::Status& status) {
Clear();
status_ = status;
EnsureNotOk();
}
void Assign(mediapipe::Status&& status) {
Clear();
status_ = std::move(status);
EnsureNotOk();
}
bool ok() const { return status_.ok(); }
protected:
// status_ will always be active after the constructor.
// We make it a union to be able to initialize exactly how we need without
// waste.
// Eg. in the copy constructor we use the default constructor of Status in
// the ok() path to avoid an extra Ref call.
union {
mediapipe::Status status_;
};
// data_ is active iff status_.ok()==true
struct Dummy {};
union {
// When T is const, we need some non-const object we can cast to void* for
// the placement new. dummy_ is that object.
Dummy dummy_;
T data_;
};
void Clear() {
if (ok()) data_.~T();
}
void EnsureOk() const {
if (!ok()) Helper::Crash(status_);
}
void EnsureNotOk() {
if (ok()) Helper::HandleInvalidStatusCtorArg(&status_);
}
// Construct the value (ie. data_) through placement new with the passed
// argument.
template <typename Arg>
void MakeValue(Arg&& arg) {
internal_statusor::PlacementNew<T>(&dummy_, std::forward<Arg>(arg));
}
// Construct the status (ie. status_) through placement new with the passed
// argument.
template <typename... Args>
void MakeStatus(Args&&... args) {
internal_statusor::PlacementNew<mediapipe::Status>(
&status_, std::forward<Args>(args)...);
}
};
// Helper base class to allow implicitly deleted constructors and assignment
// operations in StatusOr.
// TraitsBase will explicitly delete what it can't support and StatusOr will
// inherit that behavior implicitly.
template <bool Copy, bool Move>
struct TraitsBase {
TraitsBase() = default;
TraitsBase(const TraitsBase&) = default;
TraitsBase(TraitsBase&&) = default;
TraitsBase& operator=(const TraitsBase&) = default;
TraitsBase& operator=(TraitsBase&&) = default;
};
template <>
struct TraitsBase<false, true> {
TraitsBase() = default;
TraitsBase(const TraitsBase&) = delete;
TraitsBase(TraitsBase&&) = default;
TraitsBase& operator=(const TraitsBase&) = delete;
TraitsBase& operator=(TraitsBase&&) = default;
};
template <>
struct TraitsBase<false, false> {
TraitsBase() = default;
TraitsBase(const TraitsBase&) = delete;
TraitsBase(TraitsBase&&) = delete;
TraitsBase& operator=(const TraitsBase&) = delete;
TraitsBase& operator=(TraitsBase&&) = delete;
};
} // namespace internal_statusor
} // namespace mediapipe
#endif // MEDIAPIPE_DEPS_STATUSOR_INTERNALS_H_
-437
View File
@@ -1,437 +0,0 @@
// Copyright 2019 The MediaPipe 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.
// Unit tests for StatusOr
#include "mediapipe/framework/deps/statusor.h"
#include <memory>
#include <type_traits>
#include "mediapipe/framework/deps/canonical_errors.h"
#include "mediapipe/framework/deps/status.h"
#include "mediapipe/framework/port/gtest.h"
namespace mediapipe {
namespace {
class Base1 {
public:
virtual ~Base1() {}
int pad_;
};
class Base2 {
public:
virtual ~Base2() {}
int yetotherpad_;
};
class Derived : public Base1, public Base2 {
public:
~Derived() override {}
int evenmorepad_;
};
class CopyNoAssign {
public:
explicit CopyNoAssign(int value) : foo_(value) {}
CopyNoAssign(const CopyNoAssign& other) : foo_(other.foo_) {}
int foo_;
private:
const CopyNoAssign& operator=(const CopyNoAssign&);
};
class NoDefaultConstructor {
public:
explicit NoDefaultConstructor(int foo);
};
static_assert(!std::is_default_constructible<NoDefaultConstructor>(),
"Should not be default-constructible.");
StatusOr<std::unique_ptr<int>> ReturnUniquePtr() {
// Uses implicit constructor from T&&
return std::unique_ptr<int>(new int(0));
}
TEST(StatusOr, ElementType) {
static_assert(std::is_same<StatusOr<int>::element_type, int>(), "");
static_assert(std::is_same<StatusOr<char>::element_type, char>(), "");
}
TEST(StatusOr, TestNoDefaultConstructorInitialization) {
// Explicitly initialize it with an error code.
mediapipe::StatusOr<NoDefaultConstructor> statusor(
mediapipe::CancelledError(""));
EXPECT_FALSE(statusor.ok());
EXPECT_EQ(statusor.status().code(), mediapipe::StatusCode::kCancelled);
// Default construction of StatusOr initializes it with an UNKNOWN error code.
mediapipe::StatusOr<NoDefaultConstructor> statusor2;
EXPECT_FALSE(statusor2.ok());
EXPECT_EQ(statusor2.status().code(), mediapipe::StatusCode::kUnknown);
}
TEST(StatusOr, TestMoveOnlyInitialization) {
mediapipe::StatusOr<std::unique_ptr<int>> thing(ReturnUniquePtr());
ASSERT_TRUE(thing.ok());
EXPECT_EQ(0, *thing.ValueOrDie());
int* previous = thing.ValueOrDie().get();
thing = ReturnUniquePtr();
EXPECT_TRUE(thing.ok());
EXPECT_EQ(0, *thing.ValueOrDie());
EXPECT_NE(previous, thing.ValueOrDie().get());
}
TEST(StatusOr, TestMoveOnlyStatusCtr) {
mediapipe::StatusOr<std::unique_ptr<int>> thing(
mediapipe::CancelledError(""));
ASSERT_FALSE(thing.ok());
}
TEST(StatusOr, TestMoveOnlyValueExtraction) {
mediapipe::StatusOr<std::unique_ptr<int>> thing(ReturnUniquePtr());
ASSERT_TRUE(thing.ok());
std::unique_ptr<int> ptr = thing.ConsumeValueOrDie();
EXPECT_EQ(0, *ptr);
thing = std::move(ptr);
ptr = std::move(thing.ValueOrDie());
EXPECT_EQ(0, *ptr);
}
TEST(StatusOr, TestMoveOnlyConversion) {
mediapipe::StatusOr<std::unique_ptr<const int>> const_thing(
ReturnUniquePtr());
EXPECT_TRUE(const_thing.ok());
EXPECT_EQ(0, *const_thing.ValueOrDie());
// Test rvalue converting assignment
const int* const_previous = const_thing.ValueOrDie().get();
const_thing = ReturnUniquePtr();
EXPECT_TRUE(const_thing.ok());
EXPECT_EQ(0, *const_thing.ValueOrDie());
EXPECT_NE(const_previous, const_thing.ValueOrDie().get());
}
TEST(StatusOr, TestMoveOnlyVector) {
// Sanity check that mediapipe::StatusOr<MoveOnly> works in vector.
std::vector<mediapipe::StatusOr<std::unique_ptr<int>>> vec;
vec.push_back(ReturnUniquePtr());
vec.resize(2);
auto another_vec = std::move(vec);
EXPECT_EQ(0, *another_vec[0].ValueOrDie());
EXPECT_EQ(mediapipe::StatusCode::kUnknown, another_vec[1].status().code());
}
TEST(StatusOr, TestMoveWithValuesAndErrors) {
mediapipe::StatusOr<std::string> status_or(std::string(1000, '0'));
mediapipe::StatusOr<std::string> value1(std::string(1000, '1'));
mediapipe::StatusOr<std::string> value2(std::string(1000, '2'));
mediapipe::StatusOr<std::string> error1(
Status(mediapipe::StatusCode::kUnknown, "error1"));
mediapipe::StatusOr<std::string> error2(
Status(mediapipe::StatusCode::kUnknown, "error2"));
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '0'), status_or.ValueOrDie());
// Overwrite the value in status_or with another value.
status_or = std::move(value1);
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '1'), status_or.ValueOrDie());
// Overwrite the value in status_or with an error.
status_or = std::move(error1);
ASSERT_FALSE(status_or.ok());
EXPECT_EQ("error1", status_or.status().message());
// Overwrite the error in status_or with another error.
status_or = std::move(error2);
ASSERT_FALSE(status_or.ok());
EXPECT_EQ("error2", status_or.status().message());
// Overwrite the error with a value.
status_or = std::move(value2);
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '2'), status_or.ValueOrDie());
}
TEST(StatusOr, TestCopyWithValuesAndErrors) {
mediapipe::StatusOr<std::string> status_or(std::string(1000, '0'));
mediapipe::StatusOr<std::string> value1(std::string(1000, '1'));
mediapipe::StatusOr<std::string> value2(std::string(1000, '2'));
mediapipe::StatusOr<std::string> error1(
Status(mediapipe::StatusCode::kUnknown, "error1"));
mediapipe::StatusOr<std::string> error2(
Status(mediapipe::StatusCode::kUnknown, "error2"));
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '0'), status_or.ValueOrDie());
// Overwrite the value in status_or with another value.
status_or = value1;
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '1'), status_or.ValueOrDie());
// Overwrite the value in status_or with an error.
status_or = error1;
ASSERT_FALSE(status_or.ok());
EXPECT_EQ("error1", status_or.status().message());
// Overwrite the error in status_or with another error.
status_or = error2;
ASSERT_FALSE(status_or.ok());
EXPECT_EQ("error2", status_or.status().message());
// Overwrite the error with a value.
status_or = value2;
ASSERT_TRUE(status_or.ok());
EXPECT_EQ(std::string(1000, '2'), status_or.ValueOrDie());
// Verify original values unchanged.
EXPECT_EQ(std::string(1000, '1'), value1.ValueOrDie());
EXPECT_EQ("error1", error1.status().message());
EXPECT_EQ("error2", error2.status().message());
EXPECT_EQ(std::string(1000, '2'), value2.ValueOrDie());
}
TEST(StatusOr, TestDefaultCtor) {
mediapipe::StatusOr<int> thing;
EXPECT_FALSE(thing.ok());
EXPECT_EQ(thing.status().code(), mediapipe::StatusCode::kUnknown);
}
TEST(StatusOrDeathTest, TestDefaultCtorValue) {
mediapipe::StatusOr<int> thing;
EXPECT_DEATH(thing.ValueOrDie(), "");
const mediapipe::StatusOr<int> thing2;
EXPECT_DEATH(thing.ValueOrDie(), "");
}
TEST(StatusOr, TestStatusCtor) {
mediapipe::StatusOr<int> thing(
mediapipe::Status(mediapipe::StatusCode::kCancelled, ""));
EXPECT_FALSE(thing.ok());
EXPECT_EQ(thing.status().code(), mediapipe::StatusCode::kCancelled);
}
TEST(StatusOr, TestValueCtor) {
const int kI = 4;
const mediapipe::StatusOr<int> thing(kI);
EXPECT_TRUE(thing.ok());
EXPECT_EQ(kI, thing.ValueOrDie());
}
TEST(StatusOr, TestCopyCtorStatusOk) {
const int kI = 4;
const mediapipe::StatusOr<int> original(kI);
const mediapipe::StatusOr<int> copy(original);
EXPECT_EQ(copy.status(), original.status());
EXPECT_EQ(original.ValueOrDie(), copy.ValueOrDie());
}
TEST(StatusOr, TestCopyCtorStatusNotOk) {
mediapipe::StatusOr<int> original(
Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<int> copy(original);
EXPECT_EQ(copy.status(), original.status());
}
TEST(StatusOr, TestCopyCtorNonAssignable) {
const int kI = 4;
CopyNoAssign value(kI);
mediapipe::StatusOr<CopyNoAssign> original(value);
mediapipe::StatusOr<CopyNoAssign> copy(original);
EXPECT_EQ(copy.status(), original.status());
EXPECT_EQ(original.ValueOrDie().foo_, copy.ValueOrDie().foo_);
}
TEST(StatusOr, TestCopyCtorStatusOKConverting) {
const int kI = 4;
mediapipe::StatusOr<int> original(kI);
mediapipe::StatusOr<double> copy(original);
EXPECT_EQ(copy.status(), original.status());
EXPECT_DOUBLE_EQ(original.ValueOrDie(), copy.ValueOrDie());
}
TEST(StatusOr, TestCopyCtorStatusNotOkConverting) {
mediapipe::StatusOr<int> original(
Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<double> copy(original);
EXPECT_EQ(copy.status(), original.status());
}
TEST(StatusOr, TestAssignmentStatusOk) {
const int kI = 4;
mediapipe::StatusOr<int> source(kI);
mediapipe::StatusOr<int> target;
target = source;
EXPECT_EQ(target.status(), source.status());
EXPECT_EQ(source.ValueOrDie(), target.ValueOrDie());
}
TEST(StatusOr, TestAssignmentStatusNotOk) {
mediapipe::StatusOr<int> source(
Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<int> target;
target = source;
EXPECT_EQ(target.status(), source.status());
}
TEST(StatusOr, TestStatus) {
mediapipe::StatusOr<int> good(4);
EXPECT_TRUE(good.ok());
mediapipe::StatusOr<int> bad(Status(mediapipe::StatusCode::kCancelled, ""));
EXPECT_FALSE(bad.ok());
EXPECT_EQ(bad.status(), Status(mediapipe::StatusCode::kCancelled, ""));
}
TEST(StatusOr, TestValue) {
const int kI = 4;
mediapipe::StatusOr<int> thing(kI);
EXPECT_EQ(kI, thing.ValueOrDie());
}
TEST(StatusOr, TestValueConst) {
const int kI = 4;
const mediapipe::StatusOr<int> thing(kI);
EXPECT_EQ(kI, thing.ValueOrDie());
}
TEST(StatusOrDeathTest, TestValueNotOk) {
mediapipe::StatusOr<int> thing(
mediapipe::Status(mediapipe::StatusCode::kCancelled, "cancelled"));
EXPECT_DEATH(thing.ValueOrDie(), "cancelled");
}
TEST(StatusOrDeathTest, TestValueNotOkConst) {
const mediapipe::StatusOr<int> thing(
mediapipe::Status(mediapipe::StatusCode::kUnknown, ""));
EXPECT_DEATH(thing.ValueOrDie(), "");
}
TEST(StatusOr, TestPointerDefaultCtor) {
mediapipe::StatusOr<int*> thing;
EXPECT_FALSE(thing.ok());
EXPECT_EQ(thing.status().code(), mediapipe::StatusCode::kUnknown);
}
TEST(StatusOrDeathTest, TestPointerDefaultCtorValue) {
mediapipe::StatusOr<int*> thing;
EXPECT_DEATH(thing.ValueOrDie(), "");
}
TEST(StatusOr, TestPointerStatusCtor) {
mediapipe::StatusOr<int*> thing(
Status(mediapipe::StatusCode::kCancelled, ""));
EXPECT_FALSE(thing.ok());
EXPECT_EQ(thing.status(), Status(mediapipe::StatusCode::kCancelled, ""));
}
TEST(StatusOr, TestPointerValueCtor) {
const int kI = 4;
mediapipe::StatusOr<const int*> thing(&kI);
EXPECT_TRUE(thing.ok());
EXPECT_EQ(&kI, thing.ValueOrDie());
}
TEST(StatusOr, TestPointerCopyCtorStatusOk) {
const int kI = 0;
mediapipe::StatusOr<const int*> original(&kI);
mediapipe::StatusOr<const int*> copy(original);
EXPECT_EQ(copy.status(), original.status());
EXPECT_EQ(original.ValueOrDie(), copy.ValueOrDie());
}
TEST(StatusOr, TestPointerCopyCtorStatusNotOk) {
mediapipe::StatusOr<int*> original(
Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<int*> copy(original);
EXPECT_EQ(copy.status(), original.status());
}
TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) {
Derived derived;
mediapipe::StatusOr<Derived*> original(&derived);
mediapipe::StatusOr<Base2*> copy(original);
EXPECT_EQ(copy.status(), original.status());
EXPECT_EQ(static_cast<const Base2*>(original.ValueOrDie()),
copy.ValueOrDie());
}
TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) {
mediapipe::StatusOr<Derived*> original(
mediapipe::Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<Base2*> copy(original);
EXPECT_EQ(copy.status(), original.status());
}
TEST(StatusOr, TestPointerAssignmentStatusOk) {
const int kI = 0;
mediapipe::StatusOr<const int*> source(&kI);
mediapipe::StatusOr<const int*> target;
target = source;
EXPECT_EQ(target.status(), source.status());
EXPECT_EQ(source.ValueOrDie(), target.ValueOrDie());
}
TEST(StatusOr, TestPointerAssignmentStatusNotOk) {
mediapipe::StatusOr<int*> source(
mediapipe::Status(mediapipe::StatusCode::kCancelled, ""));
mediapipe::StatusOr<int*> target;
target = source;
EXPECT_EQ(target.status(), source.status());
}
TEST(StatusOr, TestPointerStatus) {
const int kI = 0;
mediapipe::StatusOr<const int*> good(&kI);
EXPECT_TRUE(good.ok());
mediapipe::StatusOr<const int*> bad(
mediapipe::Status(mediapipe::StatusCode::kCancelled, ""));
EXPECT_EQ(bad.status(),
mediapipe::Status(mediapipe::StatusCode::kCancelled, ""));
}
TEST(StatusOr, TestPointerValue) {
const int kI = 0;
mediapipe::StatusOr<const int*> thing(&kI);
EXPECT_EQ(&kI, thing.ValueOrDie());
}
TEST(StatusOr, TestPointerValueConst) {
const int kI = 0;
const mediapipe::StatusOr<const int*> thing(&kI);
EXPECT_EQ(&kI, thing.ValueOrDie());
}
TEST(StatusOrDeathTest, TestPointerValueNotOk) {
mediapipe::StatusOr<int*> thing(
mediapipe::Status(mediapipe::StatusCode::kCancelled, "cancelled"));
EXPECT_DEATH(thing.ValueOrDie(), "cancelled");
}
TEST(StatusOrDeathTest, TestPointerValueNotOkConst) {
const mediapipe::StatusOr<int*> thing(
mediapipe::Status(mediapipe::StatusCode::kCancelled, "cancelled"));
EXPECT_DEATH(thing.ValueOrDie(), "cancelled");
}
} // namespace
} // namespace mediapipe
+1 -1
View File
@@ -241,7 +241,7 @@ class BasicVector {
return out << "]";
}
// These are only public for technical reasons (see cl/121145822).
// These are only public for technical reasons.
template <typename K>
D MulScalarInternal(const K& k) const {
return Generate([k](const T& x) { return k * x; }, AsD());
+3 -4
View File
@@ -13,7 +13,6 @@
// limitations under the License.
// Executor class for the MediaPipe scheduler.
// Design doc: go/mediapipe-executor
#ifndef MEDIAPIPE_FRAMEWORK_EXECUTOR_H_
#define MEDIAPIPE_FRAMEWORK_EXECUTOR_H_
@@ -48,7 +47,7 @@ class Executor {
// A registered Executor subclass must implement the static factory method
// Create. The Executor subclass cannot be registered without it.
//
// static mediapipe::StatusOr<Executor*> Create(
// static absl::StatusOr<Executor*> Create(
// const MediaPipeOptions& extendable_options);
//
// Create validates extendable_options, then calls the constructor, and
@@ -65,8 +64,8 @@ class Executor {
virtual void Schedule(std::function<void()> task) = 0;
};
using ExecutorRegistry = GlobalFactoryRegistry<mediapipe::StatusOr<Executor*>,
const MediaPipeOptions&>;
using ExecutorRegistry =
GlobalFactoryRegistry<absl::StatusOr<Executor*>, const MediaPipeOptions&>;
// Macro for registering the executor.
#define REGISTER_EXECUTOR(name) \
+90 -2
View File
@@ -165,6 +165,7 @@ cc_library(
}),
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:protobuf",
"//mediapipe/framework/formats:location_data_cc_proto",
"//mediapipe/framework/formats/annotation:locus_cc_proto",
"@com_google_absl//absl/base:core_headers",
@@ -182,8 +183,6 @@ cc_library(
"//mediapipe/framework/port:statusor",
"//mediapipe/framework/formats/annotation:rasterization_cc_proto",
] + select({
"//conditions:default": ["@com_google_protobuf//:protobuf"],
}) + select({
"//conditions:default": [
"//mediapipe/framework/port:opencv_imgproc",
],
@@ -277,6 +276,95 @@ filegroup(
visibility = ["//mediapipe:__subpackages__"],
)
cc_library(
name = "image",
srcs = ["image.cc"],
hdrs = ["image.h"],
copts = select({
"//mediapipe:ios": [
"-x objective-c++",
"-fobjc-arc", # enable reference-counting
],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework/formats:image_frame",
"//mediapipe/framework/formats:image_format_cc_proto",
"@com_google_absl//absl/synchronization",
"//mediapipe/framework:port",
"//mediapipe/framework/port:logging",
] + select({
"//conditions:default": [
"//mediapipe/gpu:gpu_buffer",
"//mediapipe/gpu:gpu_buffer_format",
"//mediapipe/gpu:gl_texture_buffer",
],
"//mediapipe:ios": [
"//mediapipe/gpu:gpu_buffer",
"//mediapipe/gpu:gpu_buffer_format",
],
"//mediapipe/gpu:disable_gpu": [],
}) + select({
"//conditions:default": [],
"//mediapipe:apple": [
"//mediapipe/objc:CFHolder",
"//mediapipe/objc:util",
],
}),
)
cc_library(
name = "image_multi_pool",
srcs = ["image_multi_pool.cc"],
hdrs = ["image_multi_pool.h"],
visibility = ["//visibility:public"],
deps = [
":image",
"//mediapipe/framework/formats:image_frame_pool",
"//mediapipe/framework:port",
"//mediapipe/framework/port:logging",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/synchronization",
] + select({
"//conditions:default": [
"//mediapipe/gpu:gl_texture_buffer",
"//mediapipe/gpu:gl_texture_buffer_pool",
"//mediapipe/gpu:gl_base",
"//mediapipe/gpu:gpu_buffer",
],
"//mediapipe:ios": [
"//mediapipe/gpu:gl_base",
"//mediapipe/gpu:gpu_buffer",
],
"//mediapipe/gpu:disable_gpu": [],
}) + select({
"//conditions:default": [],
"//mediapipe:apple": [
"//mediapipe/gpu:pixel_buffer_pool_util",
"//mediapipe/objc:CFHolder",
],
}),
)
cc_library(
name = "image_opencv",
srcs = [
"image_opencv.cc",
],
hdrs = [
"image_opencv.h",
],
visibility = ["//visibility:public"],
deps = [
":image",
"//mediapipe/framework/formats:image_format_cc_proto",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:opencv_core",
"//mediapipe/framework/port:statusor",
],
)
cc_library(
name = "image_frame_pool",
srcs = ["image_frame_pool.cc"],
@@ -21,6 +21,8 @@ syntax = "proto2";
package mediapipe;
option objc_class_prefix = "MediaPipe";
option java_package = "com.google.mediapipe.formats.proto";
option java_outer_classname = "ClassificationProto";
message Classification {
// The index of the class in the corresponding label map.
@@ -29,7 +29,6 @@ option java_outer_classname = "DetectionProto";
message Detection {
// i-th label or label_id has a score encoded by the i-th element in score.
// Either string or integer labels must be used but not both at the same time.
repeated string label = 1;
repeated int32 label_id = 2 [packed = true];
repeated float score = 3 [packed = true];
+97
View File
@@ -0,0 +1,97 @@
// Copyright 2020 The MediaPipe 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 "mediapipe/framework/formats/image.h"
namespace mediapipe {
// TODO Refactor common code from GpuBufferToImageFrameCalculator
bool Image::ConvertToCpu() const {
if (!use_gpu_) return true; // Already on CPU.
#if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
image_frame_ = CreateImageFrameForCVPixelBuffer(GetCVPixelBufferRef());
#else
auto gl_texture = gpu_buffer_.GetGlTextureBufferSharedPtr();
if (!gl_texture->GetProducerContext()) return false;
gl_texture->GetProducerContext()->Run([this, &gl_texture]() {
gl_texture->WaitOnGpu();
const auto gpu_buf = mediapipe::GpuBuffer(GetGlTextureBufferSharedPtr());
#ifdef __ANDROID__
glBindFramebuffer(GL_FRAMEBUFFER, 0); // b/32091368
#endif
GLuint fb = 0;
glDisable(GL_DEPTH_TEST);
// TODO Re-use a shared framebuffer.
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glViewport(0, 0, gpu_buf.width(), gpu_buf.height());
glActiveTexture(GL_TEXTURE0);
glBindTexture(gl_texture->target(), gl_texture->name());
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
gl_texture->target(), gl_texture->name(), 0);
auto frame = std::make_shared<ImageFrame>(
mediapipe::ImageFormatForGpuBufferFormat(gpu_buf.format()),
gpu_buf.width(), gpu_buf.height(),
ImageFrame::kGlDefaultAlignmentBoundary);
const auto info = GlTextureInfoForGpuBufferFormat(
gpu_buf.format(), 0, gl_texture->GetProducerContext()->GetGlVersion());
glReadPixels(0, 0, gpu_buf.width(), gpu_buf.height(), info.gl_format,
info.gl_type, frame->MutablePixelData());
glDeleteFramebuffers(1, &fb);
// Cleanup
gl_texture->DidRead(gl_texture->GetProducerContext()->CreateSyncToken());
image_frame_ = frame;
});
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#endif // !MEDIAPIPE_DISABLE_GPU
use_gpu_ = false;
return true;
}
// TODO Refactor common code from ImageFrameToGpuBufferCalculator
bool Image::ConvertToGpu() const {
#if MEDIAPIPE_DISABLE_GPU
return false;
#else
if (use_gpu_) return true; // Already on GPU.
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
auto packet = MakePacket<ImageFrame>(std::move(*image_frame_));
image_frame_ = nullptr;
CFHolder<CVPixelBufferRef> buffer;
auto status = CreateCVPixelBufferForImageFramePacket(packet, true, &buffer);
CHECK_OK(status);
gpu_buffer_ = mediapipe::GpuBuffer(std::move(buffer));
#else
// GlCalculatorHelperImpl::MakeGlTextureBuffer (CreateSourceTexture)
auto buffer = mediapipe::GlTextureBuffer::Create(
image_frame_->Width(), image_frame_->Height(),
mediapipe::GpuBufferFormatForImageFormat(image_frame_->Format()),
image_frame_->PixelData());
glBindTexture(GL_TEXTURE_2D, buffer->name());
// See GlCalculatorHelperImpl::SetStandardTextureParams
glTexParameteri(buffer->target(), GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(buffer->target(), GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(buffer->target(), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(buffer->target(), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
gpu_buffer_ = mediapipe::GpuBuffer(std::move(buffer));
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
use_gpu_ = true;
return true;
#endif // MEDIAPIPE_DISABLE_GPU
}
} // namespace mediapipe
+318
View File
@@ -0,0 +1,318 @@
// Copyright 2020 The MediaPipe 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 MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_H_
#define MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_H_
#include <utility>
#include "absl/synchronization/mutex.h"
#include "mediapipe/framework/formats/image_format.pb.h"
#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/port/logging.h"
#if !MEDIAPIPE_DISABLE_GPU
#include "mediapipe/gpu/gpu_buffer.h"
#include "mediapipe/gpu/gpu_buffer_format.h"
#if defined(__APPLE__)
#include <CoreVideo/CoreVideo.h>
#include "mediapipe/objc/CFHolder.h"
#include "mediapipe/objc/util.h"
#if !TARGET_OS_OSX // iOS, use CVPixelBuffer.
#define MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER 1
#endif // TARGET_OS_OSX
#endif // defined(__APPLE__)
#if !MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER // OSX, use GL textures.
#include "mediapipe/gpu/gl_texture_buffer.h"
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#endif // !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
using ImageFrameSharedPtr = std::shared_ptr<ImageFrame>;
// This class wraps ImageFrame(CPU) & GpuBuffer(GPU) data.
// An instance of Image acts as an opaque reference to the underlying
// data objects. Image also maintains backwards compatability with GpuBuffer.
//
// Accessing GPU storage requires a valid OpenGL context active beforehand.
// i.e.: GetGlTextureBufferSharedPtr() & ConvertToGpu() & GetGpuBuffer()
// should be called inside an active GL context.
//
// Note: 'use_gpu_' flag is used to keep track of where data is (dirty bit).
//
// TODO Refactor Image to use 'Impl' class delegation system.
//
class Image {
public:
// Default constructor creates invalid object.
Image() = default;
// Copy and move constructors and assignment operators are supported.
Image(const Image& other) = default;
Image(Image&& other) = default;
Image& operator=(const Image& other) = default;
Image& operator=(Image&& other) = default;
// Creates an Image representing the same image content as the ImageFrame
// the input shared pointer points to, and retaining shared ownership.
explicit Image(ImageFrameSharedPtr frame_buffer)
: image_frame_(std::move(frame_buffer)) {
use_gpu_ = false;
pixel_mutex_ = std::make_shared<absl::Mutex>();
}
// Creates an Image representing the same image content as the input GPU
// buffer in platform-specific representations.
#if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
explicit Image(CFHolder<CVPixelBufferRef> pixel_buffer)
: Image(mediapipe::GpuBuffer(std::move(pixel_buffer))) {}
explicit Image(CVPixelBufferRef pixel_buffer)
: Image(mediapipe::GpuBuffer(pixel_buffer)) {}
#else
explicit Image(mediapipe::GlTextureBufferSharedPtr texture_buffer)
: Image(mediapipe::GpuBuffer(std::move(texture_buffer))) {}
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
explicit Image(mediapipe::GpuBuffer gpu_buffer) {
use_gpu_ = true;
gpu_buffer_ = gpu_buffer;
pixel_mutex_ = std::make_shared<absl::Mutex>();
}
#endif // !MEDIAPIPE_DISABLE_GPU
const ImageFrameSharedPtr& GetImageFrameSharedPtr() const {
if (use_gpu_ == true) ConvertToCpu();
return image_frame_;
}
#if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
CVPixelBufferRef GetCVPixelBufferRef() const {
if (use_gpu_ == false) ConvertToGpu();
return gpu_buffer_.GetCVPixelBufferRef();
}
#else
const mediapipe::GlTextureBufferSharedPtr& GetGlTextureBufferSharedPtr()
const {
if (use_gpu_ == false) ConvertToGpu();
return gpu_buffer_.GetGlTextureBufferSharedPtr();
}
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
// Get a GPU view. Automatically uploads from CPU if needed.
const mediapipe::GpuBuffer GetGpuBuffer() const {
if (use_gpu_ == false) ConvertToGpu();
return gpu_buffer_;
}
#endif // !MEDIAPIPE_DISABLE_GPU
// Returns image properties.
int width() const;
int height() const;
int channels() const;
int step() const; // Row size in bytes.
bool UsesGpu() const { return use_gpu_; }
ImageFormat::Format image_format() const;
#if !MEDIAPIPE_DISABLE_GPU
mediapipe::GpuBufferFormat format() const;
#endif // !MEDIAPIPE_DISABLE_GPU
// Converts to true iff valid.
explicit operator bool() const { return operator!=(nullptr); }
bool operator==(const Image& other) const;
bool operator!=(const Image& other) const { return !operator==(other); }
// Allow comparison with nullptr.
bool operator==(std::nullptr_t other) const;
bool operator!=(std::nullptr_t other) const { return !operator==(other); }
// Allow assignment from nullptr.
Image& operator=(std::nullptr_t other);
// Lock/Unlock pixel data.
// Should be used exclusively by the PixelLock helper class.
void LockPixels() const ABSL_EXCLUSIVE_LOCK_FUNCTION(pixel_mutex_);
void UnlockPixels() const ABSL_UNLOCK_FUNCTION(pixel_mutex_);
// Helper utility for GPU->CPU data transfer.
bool ConvertToCpu() const;
// Helper utility for CPU->GPU data transfer.
// *Requires a valid OpenGL context to be active before calling!*
bool ConvertToGpu() const;
private:
#if !MEDIAPIPE_DISABLE_GPU
mutable mediapipe::GpuBuffer gpu_buffer_;
#endif // !MEDIAPIPE_DISABLE_GPU
mutable ImageFrameSharedPtr image_frame_;
mutable bool use_gpu_ = false;
mutable std::shared_ptr<absl::Mutex> pixel_mutex_; // ImageFrame only.
};
inline int Image::width() const {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
return gpu_buffer_.width();
else
#endif // !MEDIAPIPE_DISABLE_GPU
return image_frame_->Width();
}
inline int Image::height() const {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
return gpu_buffer_.height();
else
#endif // !MEDIAPIPE_DISABLE_GPU
return image_frame_->Height();
}
inline ImageFormat::Format Image::image_format() const {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
return mediapipe::ImageFormatForGpuBufferFormat(gpu_buffer_.format());
else
#endif // !MEDIAPIPE_DISABLE_GPU
return image_frame_->Format();
}
#if !MEDIAPIPE_DISABLE_GPU
inline mediapipe::GpuBufferFormat Image::format() const {
if (use_gpu_)
return gpu_buffer_.format();
else
return mediapipe::GpuBufferFormatForImageFormat(image_frame_->Format());
}
#endif // !MEDIAPIPE_DISABLE_GPU
inline bool Image::operator==(std::nullptr_t other) const {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
return gpu_buffer_ == other;
else
#endif // !MEDIAPIPE_DISABLE_GPU
return image_frame_ == other;
}
inline bool Image::operator==(const Image& other) const {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
return gpu_buffer_ == other.gpu_buffer_;
else
#endif // !MEDIAPIPE_DISABLE_GPU
return image_frame_ == other.image_frame_;
}
inline Image& Image::operator=(std::nullptr_t other) {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu_)
gpu_buffer_ = other;
else
#endif // !MEDIAPIPE_DISABLE_GPU
image_frame_ = other;
return *this;
}
inline int Image::channels() const {
return ImageFrame::NumberOfChannelsForFormat(image_format());
}
inline int Image::step() const {
if (use_gpu_)
return width() * ImageFrame::ByteDepthForFormat(image_format());
else
return image_frame_->WidthStep();
}
inline void Image::LockPixels() const {
pixel_mutex_->Lock();
ConvertToCpu(); // Download data if necessary.
}
inline void Image::UnlockPixels() const { pixel_mutex_->Unlock(); }
// Helper class for getting access to Image CPU data,
// and handles automatically locking/unlocking CPU data access.
//
// Returns pointer to first pixel, or nullptr if invaild Image is provided
//
// Example use:
// Image buf = ...
// {
// PixelLock lock(&buf);
// uint8* buf_ptr = lock.Pixels();
// ... use buf_ptr to access pixel data ...
// ... lock released automatically at end of scope ...
// }
//
// Note: should be used in separate minimal scope where possible; see example^.
//
class PixelReadLock {
public:
explicit PixelReadLock(const Image& image) {
buffer_ = &image;
if (buffer_) buffer_->LockPixels();
}
~PixelReadLock() {
if (buffer_) buffer_->UnlockPixels();
}
PixelReadLock(const PixelReadLock&) = delete;
const uint8* Pixels() const {
if (buffer_ && !buffer_->UsesGpu()) {
ImageFrame* frame = buffer_->GetImageFrameSharedPtr().get();
if (frame) return frame->PixelData();
}
return nullptr;
}
PixelReadLock& operator=(const PixelReadLock&) = delete;
private:
const Image* buffer_ = nullptr;
};
class PixelWriteLock {
public:
explicit PixelWriteLock(Image* image) {
buffer_ = image;
if (buffer_) buffer_->LockPixels();
}
~PixelWriteLock() {
if (buffer_) buffer_->UnlockPixels();
}
PixelWriteLock(const PixelWriteLock&) = delete;
uint8* Pixels() {
if (buffer_ && !buffer_->UsesGpu()) {
ImageFrame* frame = buffer_->GetImageFrameSharedPtr().get();
if (frame) return frame->MutablePixelData();
}
return nullptr;
}
PixelWriteLock& operator=(const PixelWriteLock&) = delete;
private:
const Image* buffer_ = nullptr;
};
} // namespace mediapipe
#endif // MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_H_
@@ -0,0 +1,219 @@
// Copyright 2019 The MediaPipe 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 "mediapipe/framework/formats/image_multi_pool.h"
#include <tuple>
#include "absl/memory/memory.h"
#include "absl/synchronization/mutex.h"
#include "mediapipe/framework/port/logging.h"
#if !MEDIAPIPE_DISABLE_GPU
#ifdef __APPLE__
#include "mediapipe/objc/CFHolder.h"
#endif // __APPLE__
#endif // !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
// Keep this many buffers allocated for a given frame size.
static constexpr int kKeepCount = 2;
// The maximum size of the ImageMultiPool. When the limit is reached, the
// oldest IBufferSpec will be dropped.
static constexpr int kMaxPoolCount = 20;
#if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
ImageMultiPool::SimplePoolGpu ImageMultiPool::MakeSimplePoolGpu(
IBufferSpec spec) {
OSType cv_format = mediapipe::CVPixelFormatForGpuBufferFormat(
GpuBufferFormatForImageFormat(spec.format));
CHECK_NE(cv_format, -1) << "unsupported pixel format";
return MakeCFHolderAdopting(mediapipe::CreateCVPixelBufferPool(
spec.width, spec.height, cv_format, kKeepCount,
0.1 /* max age in seconds */));
}
Image ImageMultiPool::GetBufferFromSimplePool(
IBufferSpec spec, const ImageMultiPool::SimplePoolGpu& pool) {
#if TARGET_IPHONE_SIMULATOR
// On the simulator, syncing the texture with the pixelbuffer does not work,
// and we have to use glReadPixels. Since GL_UNPACK_ROW_LENGTH is not
// available in OpenGL ES 2, we should create the buffer so the pixels are
// contiguous.
//
// TODO: verify if we can use kIOSurfaceBytesPerRow to force the
// pool to give us contiguous data.
OSType cv_format = mediapipe::CVPixelFormatForGpuBufferFormat(
mediapipe::GpuBufferFormatForImageFormat(spec.format));
CHECK_NE(cv_format, -1) << "unsupported pixel format";
CVPixelBufferRef buffer;
CVReturn err = mediapipe::CreateCVPixelBufferWithoutPool(
spec.width, spec.height, cv_format, &buffer);
CHECK(!err) << "Error creating pixel buffer: " << err;
return Image(MakeCFHolderAdopting(buffer));
#else
CVPixelBufferRef buffer;
// TODO: allow the keepCount and the allocation threshold to be set
// by the application, and to be set independently.
static CFDictionaryRef auxAttributes =
mediapipe::CreateCVPixelBufferPoolAuxiliaryAttributesForThreshold(
kKeepCount);
CVReturn err = mediapipe::CreateCVPixelBufferWithPool(
*pool, auxAttributes,
[this]() {
absl::MutexLock lock(&mutex_gpu_);
for (const auto& cache : texture_caches_) {
#if TARGET_OS_OSX
CVOpenGLTextureCacheFlush(*cache, 0);
#else
CVOpenGLESTextureCacheFlush(*cache, 0);
#endif // TARGET_OS_OSX
}
},
&buffer);
CHECK(!err) << "Error creating pixel buffer: " << err;
return Image(MakeCFHolderAdopting(buffer));
#endif // TARGET_IPHONE_SIMULATOR
}
#else
ImageMultiPool::SimplePoolGpu ImageMultiPool::MakeSimplePoolGpu(
IBufferSpec spec) {
return mediapipe::GlTextureBufferPool::Create(
spec.width, spec.height, GpuBufferFormatForImageFormat(spec.format),
kKeepCount);
}
Image ImageMultiPool::GetBufferFromSimplePool(
IBufferSpec spec, const ImageMultiPool::SimplePoolGpu& pool) {
return Image(pool->GetBuffer());
}
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#endif // !MEDIAPIPE_DISABLE_GPU
ImageMultiPool::SimplePoolCpu ImageMultiPool::MakeSimplePoolCpu(
IBufferSpec spec) {
return ImageFramePool::Create(spec.width, spec.height, spec.format,
kKeepCount);
}
Image ImageMultiPool::GetBufferFromSimplePool(
IBufferSpec spec, const ImageMultiPool::SimplePoolCpu& pool) {
return Image(pool->GetBuffer());
}
Image ImageMultiPool::GetBuffer(int width, int height, bool use_gpu,
ImageFormat::Format format) {
#if !MEDIAPIPE_DISABLE_GPU
if (use_gpu) {
absl::MutexLock lock(&mutex_gpu_);
IBufferSpec key(width, height, format);
auto pool_it = pools_gpu_.find(key);
if (pool_it == pools_gpu_.end()) {
// Discard the least recently used pool in LRU cache.
if (pools_gpu_.size() >= kMaxPoolCount) {
auto old_spec = buffer_specs_gpu_.front(); // Front has LRU.
buffer_specs_gpu_.pop_front();
pools_gpu_.erase(old_spec);
}
buffer_specs_gpu_.push_back(key); // Push new spec to back.
std::tie(pool_it, std::ignore) = pools_gpu_.emplace(
std::piecewise_construct, std::forward_as_tuple(key),
std::forward_as_tuple(MakeSimplePoolGpu(key)));
} else {
// Find and move current 'key' spec to back, keeping others in same order.
auto specs_it = buffer_specs_gpu_.begin();
while (specs_it != buffer_specs_gpu_.end()) {
if (*specs_it == key) {
buffer_specs_gpu_.erase(specs_it);
break;
}
++specs_it;
}
buffer_specs_gpu_.push_back(key);
}
return GetBufferFromSimplePool(pool_it->first, pool_it->second);
} else // NOLINT(readability/braces)
#endif // !MEDIAPIPE_DISABLE_GPU
{
absl::MutexLock lock(&mutex_cpu_);
IBufferSpec key(width, height, format);
auto pool_it = pools_cpu_.find(key);
if (pool_it == pools_cpu_.end()) {
// Discard the least recently used pool in LRU cache.
if (pools_cpu_.size() >= kMaxPoolCount) {
auto old_spec = buffer_specs_cpu_.front(); // Front has LRU.
buffer_specs_cpu_.pop_front();
pools_cpu_.erase(old_spec);
}
buffer_specs_cpu_.push_back(key); // Push new spec to back.
std::tie(pool_it, std::ignore) = pools_cpu_.emplace(
std::piecewise_construct, std::forward_as_tuple(key),
std::forward_as_tuple(MakeSimplePoolCpu(key)));
} else {
// Find and move current 'key' spec to back, keeping others in same order.
auto specs_it = buffer_specs_cpu_.begin();
while (specs_it != buffer_specs_cpu_.end()) {
if (*specs_it == key) {
buffer_specs_cpu_.erase(specs_it);
break;
}
++specs_it;
}
buffer_specs_cpu_.push_back(key);
}
return GetBufferFromSimplePool(pool_it->first, pool_it->second);
}
}
ImageMultiPool::~ImageMultiPool() {
#if !MEDIAPIPE_DISABLE_GPU
#ifdef __APPLE__
CHECK_EQ(texture_caches_.size(), 0)
<< "Failed to unregister texture caches before deleting pool";
#endif // defined(__APPLE__)
#endif // !MEDIAPIPE_DISABLE_GPU
}
#if !MEDIAPIPE_DISABLE_GPU
#ifdef __APPLE__
void ImageMultiPool::RegisterTextureCache(mediapipe::CVTextureCacheType cache) {
absl::MutexLock lock(&mutex_gpu_);
CHECK(std::find(texture_caches_.begin(), texture_caches_.end(), cache) ==
texture_caches_.end())
<< "Attempting to register a texture cache twice";
texture_caches_.emplace_back(cache);
}
void ImageMultiPool::UnregisterTextureCache(
mediapipe::CVTextureCacheType cache) {
absl::MutexLock lock(&mutex_gpu_);
auto it = std::find(texture_caches_.begin(), texture_caches_.end(), cache);
CHECK(it != texture_caches_.end())
<< "Attempting to unregister an unknown texture cache";
texture_caches_.erase(it);
}
#endif // defined(__APPLE__)
#endif // !MEDIAPIPE_DISABLE_GPU
} // namespace mediapipe
@@ -0,0 +1,154 @@
// Copyright 2019 The MediaPipe 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 class lets calculators allocate GpuBuffers of various sizes, caching
// and reusing them as needed. It does so by automatically creating and using
// platform-specific buffer pools for the requested sizes.
//
// This class is not meant to be used directly by calculators, but is instead
// used by GlCalculatorHelper to allocate buffers.
#ifndef MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_MULTI_POOL_H_
#define MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_MULTI_POOL_H_
#include <deque>
#include <limits>
#include <unordered_map>
#include "absl/synchronization/mutex.h"
#include "mediapipe/framework/formats/image.h"
#include "mediapipe/framework/formats/image_frame_pool.h"
#if !MEDIAPIPE_DISABLE_GPU
#include "mediapipe/gpu/gpu_buffer.h"
#ifdef __APPLE__
#include "mediapipe/gpu/pixel_buffer_pool_util.h"
#endif // __APPLE__
#if !MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#include "mediapipe/gpu/gl_texture_buffer_pool.h"
#endif // !MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
#endif // !MEDIAPIPE_DISABLE_GPU
namespace mediapipe {
using ImageFrameSharedPtr = std::shared_ptr<ImageFrame>;
// TODO: Update to use new pool eviction policy.
class ImageMultiPool {
public:
ImageMultiPool() {}
explicit ImageMultiPool(void* ignored) {}
~ImageMultiPool();
// Obtains a buffer. May either be reused or created anew.
Image GetBuffer(int width, int height, bool use_gpu,
ImageFormat::Format format /*= ImageFormat::SRGBA*/);
#if !MEDIAPIPE_DISABLE_GPU
#ifdef __APPLE__
// TODO: add tests for the texture cache registration.
// Inform the pool of a cache that should be flushed when it is low on
// reusable buffers.
void RegisterTextureCache(mediapipe::CVTextureCacheType cache);
// Remove a texture cache from the list of caches to be flushed.
void UnregisterTextureCache(mediapipe::CVTextureCacheType cache);
#endif // defined(__APPLE__)
#endif // !MEDIAPIPE_DISABLE_GPU
static std::size_t RotateLeftN(std::size_t x, int n) {
return (x << n) | (x >> (std::numeric_limits<size_t>::digits - n));
}
struct IBufferSpec {
IBufferSpec(int w, int h, mediapipe::ImageFormat::Format f)
: width(w), height(h), format(f) {}
int width;
int height;
mediapipe::ImageFormat::Format format;
// Note: alignment should be added here if ImageFrameBufferPool is changed
// to allow for customizable alignment sizes (currently fixed at 4 for best
// compatability with OpenGL).
};
struct IBufferSpecHash {
std::size_t operator()(const IBufferSpec& spec) const {
// Width and height are expected to be smaller than half the width of
// size_t. We can combine them into a single integer using std::hash.
constexpr int kWidth = std::numeric_limits<size_t>::digits;
return std::hash<std::size_t>{}(
spec.width ^ RotateLeftN(spec.height, kWidth / 2) ^
RotateLeftN(static_cast<uint32_t>(spec.format), kWidth / 4));
// Note: alignment should be added here if ImageFrameBufferPool is changed
// to allow for customizable alignment sizes (currently fixed at 4 for
// best compatability with OpenGL).
}
};
private:
#if !MEDIAPIPE_DISABLE_GPU
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
typedef CFHolder<CVPixelBufferPoolRef> SimplePoolGpu;
#else
typedef std::shared_ptr<mediapipe::GlTextureBufferPool> SimplePoolGpu;
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
SimplePoolGpu MakeSimplePoolGpu(IBufferSpec spec);
Image GetBufferFromSimplePool(IBufferSpec spec, const SimplePoolGpu& pool);
absl::Mutex mutex_gpu_;
std::unordered_map<IBufferSpec, SimplePoolGpu, IBufferSpecHash> pools_gpu_
ABSL_GUARDED_BY(mutex_gpu_);
// A queue of IBufferSpecs to keep track of the age of each IBufferSpec added
// to the pool.
std::deque<IBufferSpec> buffer_specs_gpu_;
#endif // !MEDIAPIPE_DISABLE_GPU
typedef std::shared_ptr<ImageFramePool> SimplePoolCpu;
SimplePoolCpu MakeSimplePoolCpu(IBufferSpec spec);
Image GetBufferFromSimplePool(IBufferSpec spec, const SimplePoolCpu& pool);
absl::Mutex mutex_cpu_;
std::unordered_map<IBufferSpec, SimplePoolCpu, IBufferSpecHash> pools_cpu_
ABSL_GUARDED_BY(mutex_cpu_);
// A queue of IBufferSpecs to keep track of the age of each IBufferSpec added
// to the pool.
std::deque<IBufferSpec> buffer_specs_cpu_;
#if !MEDIAPIPE_DISABLE_GPU
#ifdef __APPLE__
// Texture caches used with this pool.
std::vector<CFHolder<mediapipe::CVTextureCacheType>> texture_caches_
GUARDED_BY(mutex_gpu_);
#endif // defined(__APPLE__)
#endif // !MEDIAPIPE_DISABLE_GPU
};
// IBufferSpec equality operators
inline bool operator==(const ImageMultiPool::IBufferSpec& lhs,
const ImageMultiPool::IBufferSpec& rhs) {
return lhs.width == rhs.width && lhs.height == rhs.height &&
lhs.format == rhs.format;
}
inline bool operator!=(const ImageMultiPool::IBufferSpec& lhs,
const ImageMultiPool::IBufferSpec& rhs) {
return !operator==(lhs, rhs);
}
} // namespace mediapipe
#endif // MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_MULTI_POOL_H_
+103
View File
@@ -0,0 +1,103 @@
// Copyright 2019 The MediaPipe 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 "mediapipe/framework/formats/image_opencv.h"
#include "mediapipe/framework/formats/image_format.pb.h"
#include "mediapipe/framework/port/logging.h"
namespace {
// Maps Image format to OpenCV Mat type.
// See mediapipe...image_format.proto and cv...opencv2/core/hal/interface.h
// for more details on respective formats.
int GetMatType(const mediapipe::ImageFormat::Format format) {
int type = 0;
switch (format) {
case mediapipe::ImageFormat::UNKNOWN:
// Invalid; Default to uchar.
type = CV_8U;
break;
case mediapipe::ImageFormat::SRGB:
type = CV_8U;
break;
case mediapipe::ImageFormat::SRGBA:
type = CV_8U;
break;
case mediapipe::ImageFormat::GRAY8:
type = CV_8U;
break;
case mediapipe::ImageFormat::GRAY16:
type = CV_16U;
break;
case mediapipe::ImageFormat::YCBCR420P:
// Invalid; Default to uchar.
type = CV_8U;
break;
case mediapipe::ImageFormat::YCBCR420P10:
// Invalid; Default to uint16.
type = CV_16U;
break;
case mediapipe::ImageFormat::SRGB48:
type = CV_16U;
break;
case mediapipe::ImageFormat::SRGBA64:
type = CV_16U;
break;
case mediapipe::ImageFormat::VEC32F1:
type = CV_32F;
break;
case mediapipe::ImageFormat::VEC32F2:
type = CV_32FC2;
break;
case mediapipe::ImageFormat::LAB8:
type = CV_8U;
break;
case mediapipe::ImageFormat::SBGRA:
type = CV_8U;
break;
default:
// Invalid or unknown; Default to uchar.
type = CV_8U;
break;
}
return type;
}
} // namespace
namespace mediapipe {
namespace formats {
cv::Mat MatView(const mediapipe::Image* image) {
const int dims = 2;
const int sizes[] = {image->height(), image->width()};
const int type =
CV_MAKETYPE(GetMatType(image->image_format()), image->channels());
const size_t steps[] = {static_cast<size_t>(image->step()),
static_cast<size_t>(ImageFrame::ByteDepthForFormat(
image->image_format()))};
mediapipe::PixelWriteLock dst_lock(const_cast<mediapipe::Image*>(image));
uint8* data_ptr = dst_lock.Pixels();
CHECK(data_ptr != nullptr);
// Use Image to initialize in-place. Image still owns memory.
if (steps[0] == sizes[1] * image->channels() *
ImageFrame::ByteDepthForFormat(image->image_format())) {
// Contiguous memory optimization. See b/78570764
return cv::Mat(dims, sizes, type, data_ptr);
} else {
// Custom width step.
return cv::Mat(dims, sizes, type, data_ptr, steps);
}
}
} // namespace formats
} // namespace mediapipe
@@ -0,0 +1,37 @@
// Copyright 2019-2020 The MediaPipe 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.
//
// Helper functions for working with ImageFrame and OpenCV.
#ifndef MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_OPENCV_H_
#define MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_OPENCV_H_
#include "mediapipe/framework/formats/image.h"
#include "mediapipe/framework/port/opencv_core_inc.h"
namespace mediapipe {
namespace formats {
// Image to OpenCV helper conversion function.
// A view into existing data is created (zero copy).
// The pixel data remains owned and maintained by mediapipe::Image.
// When converting a const Image into a cv::Mat,
// the const modifier is lost. The caller must be careful
// not to use the returned object to modify the data in a const Image,
// even though the returned data is mutable.
cv::Mat MatView(const mediapipe::Image* image);
} // namespace formats
} // namespace mediapipe
#endif // MEDIAPIPE_FRAMEWORK_FORMATS_IMAGE_FRAME_OPENCV_H_
+3 -3
View File
@@ -72,13 +72,13 @@ std::unique_ptr<cv::Mat> MaskToMat(const LocationData::BinaryMask& mask) {
}
return image;
}
mediapipe::StatusOr<std::unique_ptr<cv::Mat>> RectangleToMat(
absl::StatusOr<std::unique_ptr<cv::Mat>> RectangleToMat(
int image_width, int image_height, const Rectangle_i& rect) {
// These checks prevent undefined behavior caused when setting memory for
// rectangles whose edges lie outside image edges.
if (rect.ymin() < 0 || rect.xmin() < 0 || rect.xmax() > image_width ||
rect.ymax() > image_height) {
return mediapipe::InvalidArgumentError(absl::Substitute(
return absl::InvalidArgumentError(absl::Substitute(
"Rectangle must be bounded by image boundaries.\nImage Width: "
"$0\nImage Height: $1\nRectangle: [($2, $3), ($4, $5)]",
image_width, image_height, rect.xmin(), rect.ymin(), rect.xmax(),
@@ -643,7 +643,7 @@ std::unique_ptr<cv::Mat> Location::ConvertToCvMask(int image_width,
LOG(ERROR) << status_or_mat.status().message();
return nullptr;
}
return std::move(status_or_mat).ValueOrDie();
return std::move(status_or_mat).value();
}
case LocationData::MASK: {
return MaskToMat(location_data_.mask());
+2
View File
@@ -63,9 +63,11 @@ cc_library(
cc_test(
name = "optical_flow_field_test",
srcs = ["optical_flow_field_test.cc"],
linkstatic = 1,
deps = [
":optical_flow_field",
"//mediapipe/framework/deps:file_path",
"//mediapipe/framework/port:commandlineflags",
"//mediapipe/framework/port:file_helpers",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/port:integral_types",
@@ -19,6 +19,7 @@
#include <string>
#include "mediapipe/framework/deps/file_path.h"
#include "mediapipe/framework/port/commandlineflags.h"
#include "mediapipe/framework/port/file_helpers.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/integral_types.h"
+150 -95
View File
@@ -140,34 +140,29 @@ Tensor::OpenGlTexture2dView Tensor::GetOpenGlTexture2dReadView() const {
auto lock = absl::make_unique<absl::MutexLock>(&view_mutex_);
AllocateOpenGlTexture2d();
if (!(valid_ & kValidOpenGlTexture2d)) {
uint8_t* buffer;
std::unique_ptr<uint8_t[]> temp_buffer;
if (BhwcDepthFromShape(shape_) % 4 == 0) {
// No padding exists because number of channels are multiple of 4.
buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
} else {
const int padded_depth = (BhwcDepthFromShape(shape_) + 3) / 4 * 4;
const int padded_depth_size = padded_depth * element_size();
const int padded_size = BhwcBatchFromShape(shape_) *
BhwcHeightFromShape(shape_) *
BhwcWidthFromShape(shape_) * padded_depth_size;
temp_buffer = absl::make_unique<uint8_t[]>(padded_size);
buffer = temp_buffer.get();
uint8_t* src_buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
const int actual_depth_size = BhwcDepthFromShape(shape_) * element_size();
for (int e = 0;
e < BhwcBatchFromShape(shape_) * BhwcHeightFromShape(shape_) *
BhwcWidthFromShape(shape_);
e++) {
std::memcpy(buffer, src_buffer, actual_depth_size);
src_buffer += actual_depth_size;
buffer += padded_depth_size;
}
const int padded_size =
texture_height_ * texture_width_ * 4 * element_size();
auto temp_buffer = absl::make_unique<uint8_t[]>(padded_size);
uint8_t* dest_buffer = temp_buffer.get();
uint8_t* src_buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
const int num_elements = BhwcWidthFromShape(shape_) *
BhwcHeightFromShape(shape_) *
BhwcBatchFromShape(shape_);
const int actual_depth_size = BhwcDepthFromShape(shape_) * element_size();
const int padded_depth_size =
(BhwcDepthFromShape(shape_) + 3) / 4 * 4 * element_size();
for (int e = 0; e < num_elements; e++) {
std::memcpy(dest_buffer, src_buffer, actual_depth_size);
src_buffer += actual_depth_size;
dest_buffer += padded_depth_size;
}
// Transfer from CPU memory into GPU memory.
glBindTexture(GL_TEXTURE_2D, opengl_texture2d_);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, BhwcWidthFromShape(shape_),
BhwcHeightFromShape(shape_), GL_RGBA, GL_FLOAT, buffer);
// Set alignment for the proper value (default) to avoid address sanitizer
// error "out of boundary reading".
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture_width_, texture_height_,
GL_RGBA, GL_FLOAT, temp_buffer.get());
glBindTexture(GL_TEXTURE_2D, 0);
valid_ |= kValidOpenGlTexture2d;
}
@@ -181,6 +176,48 @@ Tensor::OpenGlTexture2dView Tensor::GetOpenGlTexture2dWriteView() const {
return {opengl_texture2d_, std::move(lock)};
}
Tensor::OpenGlTexture2dView::Layout
Tensor::OpenGlTexture2dView::GetLayoutDimensions(const Tensor::Shape& shape,
int* width, int* height) {
static int max_size = 0;
if (max_size == 0) {
int max_texture_size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
int max_renderbuffer_size;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &max_renderbuffer_size);
int max_viewport_dims[2];
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, max_viewport_dims);
max_size = std::min(std::min(max_texture_size, max_renderbuffer_size),
std::min(max_viewport_dims[0], max_viewport_dims[1]));
}
const int num_slices = (BhwcDepthFromShape(shape) + 3) / 4;
const int num_elements = BhwcBatchFromShape(shape) *
BhwcHeightFromShape(shape) *
BhwcWidthFromShape(shape);
const int num_pixels = num_slices * num_elements;
int w = BhwcWidthFromShape(shape) * num_slices;
if (w <= max_size) {
int h = (num_pixels + w - 1) / w;
if (h <= max_size) {
*width = w;
*height = h;
return Tensor::OpenGlTexture2dView::Layout::kAligned;
}
}
// The best performance of a compute shader can be achived with textures'
// width multiple of 256. Making minimum fixed width of 256 waste memory for
// small tensors. The optimal balance memory-vs-performance is power of 2.
// The texture width and height are choosen to be closer to square.
float power = std::log2(std::sqrt(static_cast<float>(num_pixels)));
w = 1 << static_cast<int>(power);
int h = (num_pixels + w - 1) / w;
LOG_IF(FATAL, w > max_size || h > max_size)
<< "The tensor can't fit into OpenGL Texture2D View.";
*width = w;
*height = h;
return Tensor::OpenGlTexture2dView::Layout::kLinearized;
}
void Tensor::AllocateOpenGlTexture2d() const {
if (opengl_texture2d_ == GL_INVALID_INDEX) {
gl_context_ = mediapipe::GlContext::GetCurrent();
@@ -192,12 +229,26 @@ void Tensor::AllocateOpenGlTexture2d() const {
// supported from floating point textures.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
const int pixels_per_depth = (BhwcDepthFromShape(shape_) + 3) / 4;
const int width = BhwcWidthFromShape(shape_) * pixels_per_depth;
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA32F, width,
BhwcHeightFromShape(shape_));
OpenGlTexture2dView::GetLayoutDimensions(shape_, &texture_width_,
&texture_height_);
if (gl_context_->GetGlVersion() != mediapipe::GlVersion::kGLES2) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA32F, texture_width_,
texture_height_);
} else {
// We assume all contexts will have the same extensions, so we only check
// once for OES_texture_float extension, to save time.
static bool has_oes_extension =
gl_context_->HasGlExtension("OES_texture_float");
LOG_IF(FATAL, !has_oes_extension)
<< "OES_texture_float extension required in order to use MP tensor "
<< "with GLES 2.0";
// Allocate the image data; note that it's no longer RGBA32F, so will be
// lower precision.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width_, texture_height_,
0, GL_RGBA, GL_FLOAT, 0 /* data */);
}
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(1, &frame_buffer_);
}
@@ -272,6 +323,8 @@ void Tensor::Move(Tensor* src) {
src->frame_buffer_ = GL_INVALID_INDEX;
opengl_texture2d_ = src->opengl_texture2d_;
src->opengl_texture2d_ = GL_INVALID_INDEX;
texture_width_ = src->texture_width_;
texture_height_ = src->texture_height_;
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
opengl_buffer_ = src->opengl_buffer_;
src->opengl_buffer_ = GL_INVALID_INDEX;
@@ -283,42 +336,54 @@ Tensor::Tensor(ElementType element_type, const Shape& shape)
: element_type_(element_type), shape_(shape) {}
void Tensor::Invalidate() {
absl::MutexLock lock(&view_mutex_);
#if MEDIAPIPE_METAL_ENABLED
// If memory is allocated and not owned by the metal buffer.
// TODO: Re-design cpu buffer memory management.
if (cpu_buffer_ && !metal_buffer_) {
DeallocateVirtualMemory(cpu_buffer_, AlignToPageSize(bytes()));
}
metal_buffer_ = nil;
#else
if (cpu_buffer_) {
free(cpu_buffer_);
}
#endif // MEDIAPIPE_METAL_ENABLED
cpu_buffer_ = nullptr;
// Don't need to wait for the resource to be deleted bacause if will be
// released on last reference deletion inside the OpenGL driver.
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
if (opengl_texture2d_ != GL_INVALID_INDEX) {
GLuint opengl_texture2d = opengl_texture2d_;
GLuint frame_buffer = frame_buffer_;
gl_context_->RunWithoutWaiting([opengl_texture2d, frame_buffer]() {
glDeleteTextures(1, &opengl_texture2d);
glDeleteFramebuffers(1, &frame_buffer);
});
opengl_texture2d_ = GL_INVALID_INDEX;
frame_buffer_ = GL_INVALID_INDEX;
}
GLuint cleanup_gl_tex = GL_INVALID_INDEX;
GLuint cleanup_gl_fb = GL_INVALID_INDEX;
GLuint cleanup_gl_buf = GL_INVALID_INDEX;
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
{
absl::MutexLock lock(&view_mutex_);
#if MEDIAPIPE_METAL_ENABLED
// If memory is allocated and not owned by the metal buffer.
// TODO: Re-design cpu buffer memory management.
if (cpu_buffer_ && !metal_buffer_) {
DeallocateVirtualMemory(cpu_buffer_, AlignToPageSize(bytes()));
}
metal_buffer_ = nil;
#else
if (cpu_buffer_) {
free(cpu_buffer_);
}
#endif // MEDIAPIPE_METAL_ENABLED
cpu_buffer_ = nullptr;
// Don't need to wait for the resource to be deleted bacause if will be
// released on last reference deletion inside the OpenGL driver.
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
std::swap(cleanup_gl_tex, opengl_texture2d_);
std::swap(cleanup_gl_fb, frame_buffer_);
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
if (opengl_buffer_ != GL_INVALID_INDEX) {
GLuint opengl_buffer = opengl_buffer_;
gl_context_->RunWithoutWaiting(
[opengl_buffer]() { glDeleteBuffers(1, &opengl_buffer); });
opengl_buffer_ = GL_INVALID_INDEX;
}
std::swap(cleanup_gl_buf, opengl_buffer_);
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
}
// Do not hold the view mutex while invoking GlContext::RunWithoutWaiting,
// since that method may acquire the context's own lock.
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
if (cleanup_gl_tex != GL_INVALID_INDEX || cleanup_gl_fb != GL_INVALID_INDEX ||
cleanup_gl_buf != GL_INVALID_INDEX)
gl_context_->RunWithoutWaiting([cleanup_gl_tex, cleanup_gl_fb
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
,
cleanup_gl_buf
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
]() {
glDeleteTextures(1, &cleanup_gl_tex);
glDeleteFramebuffers(1, &cleanup_gl_fb);
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
glDeleteBuffers(1, &cleanup_gl_buf);
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
});
#endif // MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
}
@@ -341,6 +406,8 @@ Tensor::CpuReadView Tensor::GetCpuReadView() const {
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_30
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
// TODO: we cannot just grab the GL context's lock while holding
// the view mutex here.
if (valid_ & kValidOpenGlBuffer) {
gl_context_->Run([this]() {
glBindBuffer(GL_SHADER_STORAGE_BUFFER, opengl_buffer_);
@@ -356,42 +423,30 @@ Tensor::CpuReadView Tensor::GetCpuReadView() const {
// yet.
if (valid_ & kValidOpenGlTexture2d) {
gl_context_->Run([this]() {
const int pixels_per_depth = (BhwcDepthFromShape(shape_) + 3) / 4;
const int width = BhwcWidthFromShape(shape_) * pixels_per_depth;
uint8_t* buffer;
std::unique_ptr<uint8_t[]> temp_buffer;
if (BhwcDepthFromShape(shape_) % 4 == 0) {
buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
} else {
const int padded_size = BhwcBatchFromShape(shape_) *
BhwcHeightFromShape(shape_) * width *
pixels_per_depth * 4 * element_size();
temp_buffer = absl::make_unique<uint8_t[]>(padded_size);
buffer = temp_buffer.get();
}
const int padded_size =
texture_height_ * texture_width_ * 4 * element_size();
auto temp_buffer = absl::make_unique<uint8_t[]>(padded_size);
uint8_t* buffer = temp_buffer.get();
glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer_);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, opengl_texture2d_, 0);
glPixelStorei(GL_PACK_ROW_LENGTH, width);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, BhwcHeightFromShape(shape_), GL_RGBA,
GL_FLOAT, buffer);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(0, 0, texture_width_, texture_height_, GL_RGBA, GL_FLOAT,
buffer);
if (BhwcDepthFromShape(shape_) % 4) {
uint8_t* dest_buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
const int actual_depth_size =
BhwcDepthFromShape(shape_) * element_size();
const int padded_depth_size = pixels_per_depth * 4 * element_size();
for (int e = 0;
e < BhwcBatchFromShape(shape_) * BhwcHeightFromShape(shape_) *
BhwcWidthFromShape(shape_);
e++) {
std::memcpy(dest_buffer, buffer, actual_depth_size);
dest_buffer += actual_depth_size;
buffer += padded_depth_size;
}
uint8_t* dest_buffer = reinterpret_cast<uint8_t*>(cpu_buffer_);
const int actual_depth_size =
BhwcDepthFromShape(shape_) * element_size();
const int num_slices = (BhwcDepthFromShape(shape_) + 3) / 4;
const int padded_depth_size = num_slices * 4 * element_size();
const int num_elements = BhwcWidthFromShape(shape_) *
BhwcHeightFromShape(shape_) *
BhwcBatchFromShape(shape_);
for (int e = 0; e < num_elements; e++) {
std::memcpy(dest_buffer, buffer, actual_depth_size);
dest_buffer += actual_depth_size;
buffer += padded_depth_size;
}
});
}
+12
View File
@@ -161,6 +161,16 @@ class Tensor {
: View(std::move(src)), name_(src.name_) {
src.name_ = GL_INVALID_INDEX;
}
// To fit a tensor into a texture two layouts are used:
// 1. Aligned. Width of the texture = tensor_width * num_slices, where slice
// is a group of 4 depth values. Tensor depth is padded to 4.
// 2. Linearized. If texture width or height with the layout 1. is greater
// than the GPU supports then all tensor values are packed into a texture
// with fixed width calculated by this method.
// Must be called with the valid GL context bound to the current thread.
enum class Layout { kAligned, kLinearized };
static Layout GetLayoutDimensions(const Tensor::Shape& shape, int* width,
int* height);
protected:
friend class Tensor;
@@ -254,6 +264,8 @@ class Tensor {
mutable std::shared_ptr<mediapipe::GlContext> gl_context_;
mutable GLuint opengl_texture2d_ = GL_INVALID_INDEX;
mutable GLuint frame_buffer_ = GL_INVALID_INDEX;
mutable int texture_width_;
mutable int texture_height_;
void AllocateOpenGlTexture2d() const;
#if MEDIAPIPE_OPENGL_ES_VERSION >= MEDIAPIPE_OPENGL_ES_31
mutable GLuint opengl_buffer_ = GL_INVALID_INDEX;
+1 -1
View File
@@ -2,7 +2,7 @@
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest.h"
#if !defined(MEDIAPIPE_DISABLE_GPU)
#if !MEDIAPIPE_DISABLE_GPU
#include "mediapipe/gpu/gl_calculator_helper.h"
#include "mediapipe/gpu/gpu_buffer_format.h"
#endif
+13 -13
View File
@@ -18,7 +18,7 @@ namespace mediapipe {
namespace internal {
mediapipe::Status GraphOutputStream::Initialize(
absl::Status GraphOutputStream::Initialize(
const std::string& stream_name, const PacketType* packet_type,
OutputStreamManager* output_stream_manager) {
RET_CHECK(output_stream_manager);
@@ -27,7 +27,7 @@ mediapipe::Status GraphOutputStream::Initialize(
proto_ns::RepeatedPtrField<ProtoString> input_stream_field;
input_stream_field.Add()->assign(stream_name);
std::shared_ptr<tool::TagMap> tag_map =
tool::TagMap::Create(input_stream_field).ValueOrDie();
tool::TagMap::Create(input_stream_field).value();
input_stream_handler_ = absl::make_unique<GraphOutputStreamHandler>(
tag_map, /*cc_manager=*/nullptr, MediaPipeOptions(),
/*calculator_run_in_parallel=*/false);
@@ -38,20 +38,20 @@ mediapipe::Status GraphOutputStream::Initialize(
MP_RETURN_IF_ERROR(input_stream_handler_->InitializeInputStreamManagers(
input_stream_.get()));
output_stream_manager->AddMirror(input_stream_handler_.get(), id);
return mediapipe::OkStatus();
return absl::OkStatus();
}
void GraphOutputStream::PrepareForRun(
std::function<void()> notification_callback,
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
input_stream_handler_->PrepareForRun(
/*headers_ready_callback=*/[] {}, std::move(notification_callback),
/*schedule_callback=*/nullptr, std::move(error_callback));
}
mediapipe::Status OutputStreamObserver::Initialize(
absl::Status OutputStreamObserver::Initialize(
const std::string& stream_name, const PacketType* packet_type,
std::function<mediapipe::Status(const Packet&)> packet_callback,
std::function<absl::Status(const Packet&)> packet_callback,
OutputStreamManager* output_stream_manager) {
RET_CHECK(output_stream_manager);
@@ -60,7 +60,7 @@ mediapipe::Status OutputStreamObserver::Initialize(
output_stream_manager);
}
mediapipe::Status OutputStreamObserver::Notify() {
absl::Status OutputStreamObserver::Notify() {
while (true) {
bool empty;
Timestamp min_timestamp = input_stream_->MinTimestampOrBound(&empty);
@@ -76,10 +76,10 @@ mediapipe::Status OutputStreamObserver::Notify() {
num_packets_dropped, input_stream_->Name());
MP_RETURN_IF_ERROR(packet_callback_(packet));
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status OutputStreamPollerImpl::Initialize(
absl::Status OutputStreamPollerImpl::Initialize(
const std::string& stream_name, const PacketType* packet_type,
std::function<void(InputStreamManager*, bool*)> queue_size_callback,
OutputStreamManager* output_stream_manager) {
@@ -87,12 +87,12 @@ mediapipe::Status OutputStreamPollerImpl::Initialize(
output_stream_manager));
input_stream_handler_->SetQueueSizeCallbacks(queue_size_callback,
queue_size_callback);
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputStreamPollerImpl::PrepareForRun(
std::function<void()> notification_callback,
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
input_stream_handler_->PrepareForRun(
/*headers_ready_callback=*/[] {}, std::move(notification_callback),
/*schedule_callback=*/nullptr, std::move(error_callback));
@@ -116,11 +116,11 @@ void OutputStreamPollerImpl::SetMaxQueueSize(int queue_size) {
int OutputStreamPollerImpl::QueueSize() { return input_stream_->QueueSize(); }
mediapipe::Status OutputStreamPollerImpl::Notify() {
absl::Status OutputStreamPollerImpl::Notify() {
mutex_.Lock();
handler_condvar_.Signal();
mutex_.Unlock();
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputStreamPollerImpl::NotifyError() {
+14 -16
View File
@@ -50,18 +50,17 @@ class GraphOutputStream {
// input stream and attaches the input stream to an output stream as
// the mirror for observation/polling. Ownership of output_stream_manager
// is not transferred to the graph output stream object.
mediapipe::Status Initialize(const std::string& stream_name,
const PacketType* packet_type,
OutputStreamManager* output_stream_manager);
absl::Status Initialize(const std::string& stream_name,
const PacketType* packet_type,
OutputStreamManager* output_stream_manager);
// Installs callbacks into its GraphOutputStreamHandler.
virtual void PrepareForRun(
std::function<void()> notification_callback,
std::function<void(mediapipe::Status)> error_callback);
virtual void PrepareForRun(std::function<void()> notification_callback,
std::function<void(absl::Status)> error_callback);
// Notifies the graph output stream of new packets emitted by the output
// stream.
virtual mediapipe::Status Notify() = 0;
virtual absl::Status Notify() = 0;
// Notifies the graph output stream of the errors in the calculator graph.
virtual void NotifyError() = 0;
@@ -110,21 +109,21 @@ class OutputStreamObserver : public GraphOutputStream {
public:
virtual ~OutputStreamObserver() {}
mediapipe::Status Initialize(
absl::Status Initialize(
const std::string& stream_name, const PacketType* packet_type,
std::function<mediapipe::Status(const Packet&)> packet_callback,
std::function<absl::Status(const Packet&)> packet_callback,
OutputStreamManager* output_stream_manager);
// Notifies the observer of new packets emitted by the observed
// output stream.
mediapipe::Status Notify() override;
absl::Status Notify() override;
// Notifies the observer of the errors in the calculator graph.
void NotifyError() override {}
private:
// Invoked on every packet emitted by the observed output stream.
std::function<mediapipe::Status(const Packet&)> packet_callback_;
std::function<absl::Status(const Packet&)> packet_callback_;
};
// OutputStreamPollerImpl that returns packets to the caller via
@@ -134,14 +133,13 @@ class OutputStreamPollerImpl : public GraphOutputStream {
virtual ~OutputStreamPollerImpl() {}
// Initializes an OutputStreamPollerImpl.
mediapipe::Status Initialize(
absl::Status Initialize(
const std::string& stream_name, const PacketType* packet_type,
std::function<void(InputStreamManager*, bool*)> queue_size_callback,
OutputStreamManager* output_stream_manager);
void PrepareForRun(
std::function<void()> notification_callback,
std::function<void(mediapipe::Status)> error_callback) override;
void PrepareForRun(std::function<void()> notification_callback,
std::function<void(absl::Status)> error_callback) override;
// Resets graph_has_error_ and cleans the internal packet queue.
void Reset();
@@ -152,7 +150,7 @@ class OutputStreamPollerImpl : public GraphOutputStream {
int QueueSize();
// Notifies the poller of new packets emitted by the output stream.
mediapipe::Status Notify() override;
absl::Status Notify() override;
// Notifies the poller of the errors in the calculator graph.
void NotifyError() override;
+1 -1
View File
@@ -60,7 +60,7 @@ class GraphServiceTest : public ::testing::Test {
MP_ASSERT_OK(
graph_.ObserveOutputStream("out", [this](const Packet& packet) {
output_packets_.push_back(packet);
return mediapipe::OkStatus();
return absl::OkStatus();
}));
}
+6 -7
View File
@@ -28,7 +28,7 @@ namespace mediapipe {
class GraphValidation {
public:
// Validates the specified CalculatorGraphConfig.
mediapipe::Status Validate(
absl::Status Validate(
const CalculatorGraphConfig& config,
const std::map<std::string, Packet>& side_packets = {}) {
return graph_.Initialize(config, side_packets);
@@ -40,12 +40,11 @@ class GraphValidation {
// CalclatorGraphConfig.type. A subgraph can be validated directly by
// specifying its type in |graph_type|. A template graph can be validated
// directly by specifying its template arguments in |arguments|.
mediapipe::Status Validate(
const std::vector<CalculatorGraphConfig>& configs,
const std::vector<CalculatorGraphTemplate>& templates,
const std::map<std::string, Packet>& side_packets = {},
const std::string& graph_type = "",
const Subgraph::SubgraphOptions* options = nullptr) {
absl::Status Validate(const std::vector<CalculatorGraphConfig>& configs,
const std::vector<CalculatorGraphTemplate>& templates,
const std::map<std::string, Packet>& side_packets = {},
const std::string& graph_type = "",
const Subgraph::SubgraphOptions* options = nullptr) {
return graph_.Initialize(configs, templates, side_packets, graph_type,
options);
}
+57 -9
View File
@@ -106,9 +106,8 @@ TEST(GraphValidationTest, InitializeGraphFromProtos) {
TEST(GraphValidationTest, InitializeGraphFromLinker) {
EXPECT_FALSE(SubgraphRegistry::IsRegistered("DubQuadTestSubgraph"));
ValidatedGraphConfig builder_1;
mediapipe::Status status_1 =
builder_1.Initialize({}, {}, "DubQuadTestSubgraph");
EXPECT_EQ(status_1.code(), mediapipe::StatusCode::kNotFound);
absl::Status status_1 = builder_1.Initialize({}, {}, "DubQuadTestSubgraph");
EXPECT_EQ(status_1.code(), absl::StatusCode::kNotFound);
EXPECT_THAT(status_1.message(),
testing::HasSubstr(
R"(No registered object with name: DubQuadTestSubgraph)"));
@@ -313,8 +312,8 @@ TEST(GraphValidationTest, OptionalSubgraphStreamsMismatched) {
)");
GraphValidation validation_1;
mediapipe::Status status = validation_1.Validate({config_1, config_2}, {});
ASSERT_EQ(status.code(), mediapipe::StatusCode::kInvalidArgument);
absl::Status status = validation_1.Validate({config_1, config_2}, {});
ASSERT_EQ(status.code(), absl::StatusCode::kInvalidArgument);
ASSERT_THAT(status.ToString(),
testing::HasSubstr(
"PassThroughCalculator must use matching tags and indexes"));
@@ -323,22 +322,22 @@ TEST(GraphValidationTest, OptionalSubgraphStreamsMismatched) {
// A calculator that optionally accepts an input-side-packet.
class OptionalSideInputTestCalculator : public CalculatorBase {
public:
static mediapipe::Status GetContract(CalculatorContract* cc) {
static absl::Status GetContract(CalculatorContract* cc) {
cc->InputSidePackets().Tag("SIDEINPUT").Set<std::string>().Optional();
cc->Inputs().Tag("SELECT").Set<int>().Optional();
cc->Inputs().Tag("ENABLE").Set<bool>().Optional();
cc->Outputs().Tag("OUTPUT").Set<std::string>();
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status Process(CalculatorContext* cc) final {
absl::Status Process(CalculatorContext* cc) final {
std::string value("default");
if (cc->InputSidePackets().HasTag("SIDEINPUT")) {
value = cc->InputSidePackets().Tag("SIDEINPUT").Get<std::string>();
}
cc->Outputs().Tag("OUTPUT").Add(new std::string(value),
cc->InputTimestamp());
return mediapipe::OkStatus();
return absl::OkStatus();
}
};
REGISTER_CALCULATOR(OptionalSideInputTestCalculator);
@@ -451,5 +450,54 @@ TEST(GraphValidationTest, MultipleOptionalInputsForSubgraph) {
MP_EXPECT_OK(graph_1.WaitUntilDone());
}
// Shows a calculator graph running with and without one optional side packet.
TEST(GraphValidationTest, OptionalInputsForGraph) {
// A subgraph defining one optional input-side-packet.
auto config_1 = ParseTextProtoOrDie<CalculatorGraphConfig>(R"(
type: "PassThroughGraph"
input_side_packet: "side_input_0"
input_stream: "stream_input_0"
input_stream: "stream_input_1"
output_stream: "OUTPUT:output_0"
node {
calculator: "OptionalSideInputTestCalculator"
input_side_packet: "SIDEINPUT:side_input_0"
input_stream: "SELECT:stream_input_0"
input_stream: "ENABLE:stream_input_1"
output_stream: "OUTPUT:output_0"
}
)");
GraphValidation validation_1;
MP_EXPECT_OK(validation_1.Validate({config_1}, {}));
CalculatorGraph graph_1;
MP_EXPECT_OK(graph_1.Initialize({config_1}, {}));
auto out_poller = graph_1.AddOutputStreamPoller("output_0");
MP_ASSERT_OK(out_poller);
// Run the graph specifying the optional side packet.
std::map<std::string, Packet> side_packets;
side_packets.insert({"side_input_0", MakePacket<std::string>("side_in")});
MP_EXPECT_OK(graph_1.StartRun(side_packets));
MP_EXPECT_OK(graph_1.AddPacketToInputStream(
"stream_input_0", MakePacket<int>(22).At(Timestamp(3000))));
MP_EXPECT_OK(graph_1.AddPacketToInputStream(
"stream_input_1", MakePacket<bool>(true).At(Timestamp(3000))));
Packet out_packet, options_packet;
EXPECT_TRUE(out_poller->Next(&out_packet));
EXPECT_EQ(out_packet.Get<std::string>(), "side_in");
MP_EXPECT_OK(graph_1.CloseAllPacketSources());
MP_EXPECT_OK(graph_1.WaitUntilDone());
// Run the graph omitting the optional inputs.
MP_EXPECT_OK(graph_1.StartRun({}));
MP_EXPECT_OK(graph_1.CloseInputStream("stream_input_1"));
MP_EXPECT_OK(graph_1.AddPacketToInputStream(
"stream_input_0", MakePacket<int>(22).At(Timestamp(3000))));
EXPECT_TRUE(out_poller->Next(&out_packet));
EXPECT_EQ(out_packet.Get<std::string>(), "default");
MP_EXPECT_OK(graph_1.CloseAllPacketSources());
MP_EXPECT_OK(graph_1.WaitUntilDone());
}
} // namespace
} // namespace mediapipe
@@ -21,11 +21,11 @@
namespace mediapipe {
mediapipe::Status InputSidePacketHandler::PrepareForRun(
absl::Status InputSidePacketHandler::PrepareForRun(
const PacketTypeSet* input_side_packet_types,
const std::map<std::string, Packet>& all_side_packets,
std::function<void()> input_side_packets_ready_callback,
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
int missing_input_side_packet_count;
prev_input_side_packets_ = std::move(input_side_packets_);
ASSIGN_OR_RETURN(
@@ -39,7 +39,7 @@ mediapipe::Status InputSidePacketHandler::PrepareForRun(
input_side_packets_ready_callback_ =
std::move(input_side_packets_ready_callback);
error_callback_ = std::move(error_callback);
return mediapipe::OkStatus();
return absl::OkStatus();
}
bool InputSidePacketHandler::InputSidePacketsChanged() {
@@ -49,14 +49,14 @@ bool InputSidePacketHandler::InputSidePacketsChanged() {
}
void InputSidePacketHandler::Set(CollectionItemId id, const Packet& packet) {
mediapipe::Status status = SetInternal(id, packet);
absl::Status status = SetInternal(id, packet);
if (!status.ok()) {
TriggerErrorCallback(status);
}
}
mediapipe::Status InputSidePacketHandler::SetInternal(CollectionItemId id,
const Packet& packet) {
absl::Status InputSidePacketHandler::SetInternal(CollectionItemId id,
const Packet& packet) {
RET_CHECK_GT(missing_input_side_packet_count_, 0);
Packet& side_packet = input_side_packets_->Get(id);
@@ -64,7 +64,7 @@ mediapipe::Status InputSidePacketHandler::SetInternal(CollectionItemId id,
return mediapipe::AlreadyExistsErrorBuilder(MEDIAPIPE_LOC)
<< "Input side packet with id " << id << " was already set.";
}
mediapipe::Status result = input_side_packet_types_->Get(id).Validate(packet);
absl::Status result = input_side_packet_types_->Get(id).Validate(packet);
if (!result.ok()) {
return mediapipe::StatusBuilder(result, MEDIAPIPE_LOC).SetPrepend()
<< absl::StrCat(
@@ -77,11 +77,11 @@ mediapipe::Status InputSidePacketHandler::SetInternal(CollectionItemId id,
1, std::memory_order_acq_rel) == 1) {
input_side_packets_ready_callback_();
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
void InputSidePacketHandler::TriggerErrorCallback(
const mediapipe::Status& status) const {
const absl::Status& status) const {
CHECK(error_callback_);
error_callback_(status);
}
@@ -41,11 +41,11 @@ class InputSidePacketHandler {
// Resets the input side packet handler and its underlying input side packets
// for another run of the graph.
mediapipe::Status PrepareForRun(
absl::Status PrepareForRun(
const PacketTypeSet* input_side_packet_types,
const std::map<std::string, Packet>& all_side_packets,
std::function<void()> input_side_packets_ready_callback,
std::function<void(mediapipe::Status)> error_callback);
std::function<void(absl::Status)> error_callback);
// Sets a particular input side packet.
void Set(CollectionItemId id, const Packet& packet);
@@ -63,11 +63,11 @@ class InputSidePacketHandler {
private:
// Called by Set().
mediapipe::Status SetInternal(CollectionItemId id, const Packet& packet);
absl::Status SetInternal(CollectionItemId id, const Packet& packet);
// Triggers the error callback with mediapipe::Status info when an error
// Triggers the error callback with absl::Status info when an error
// occurs.
void TriggerErrorCallback(const mediapipe::Status& status) const;
void TriggerErrorCallback(const absl::Status& status) const;
const PacketTypeSet* input_side_packet_types_;
@@ -77,7 +77,7 @@ class InputSidePacketHandler {
std::atomic<int> missing_input_side_packet_count_{0};
std::function<void()> input_side_packets_ready_callback_;
std::function<void(mediapipe::Status)> error_callback_;
std::function<void(absl::Status)> error_callback_;
};
} // namespace mediapipe
+9 -9
View File
@@ -24,13 +24,13 @@ namespace mediapipe {
using SyncSet = InputStreamHandler::SyncSet;
mediapipe::Status InputStreamHandler::InitializeInputStreamManagers(
absl::Status InputStreamHandler::InitializeInputStreamManagers(
InputStreamManager* flat_input_stream_managers) {
for (CollectionItemId id = input_stream_managers_.BeginId();
id < input_stream_managers_.EndId(); ++id) {
input_stream_managers_.Get(id) = &flat_input_stream_managers[id.value()];
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
InputStreamManager* InputStreamHandler::GetInputStreamManager(
@@ -38,7 +38,7 @@ InputStreamManager* InputStreamHandler::GetInputStreamManager(
return input_stream_managers_.Get(id);
}
mediapipe::Status InputStreamHandler::SetupInputShards(
absl::Status InputStreamHandler::SetupInputShards(
InputStreamShardSet* input_shards) {
RET_CHECK(input_shards);
for (CollectionItemId id = input_stream_managers_.BeginId();
@@ -48,7 +48,7 @@ mediapipe::Status InputStreamHandler::SetupInputShards(
input_shards->Get(id).SetName(&manager->Name());
input_shards->Get(id).SetHeader(manager->Header());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
std::vector<std::pair<std::string, int>>
@@ -68,7 +68,7 @@ void InputStreamHandler::PrepareForRun(
std::function<void()> headers_ready_callback,
std::function<void()> notification_callback,
std::function<void(CalculatorContext*)> schedule_callback,
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
headers_ready_callback_ = std::move(headers_ready_callback);
notification_ = std::move(notification_callback);
schedule_callback_ = std::move(schedule_callback);
@@ -94,7 +94,7 @@ void InputStreamHandler::SetQueueSizeCallbacks(
}
void InputStreamHandler::SetHeader(CollectionItemId id, const Packet& header) {
mediapipe::Status result = input_stream_managers_.Get(id)->SetHeader(header);
absl::Status result = input_stream_managers_.Get(id)->SetHeader(header);
if (!result.ok()) {
error_callback_(result);
return;
@@ -260,7 +260,7 @@ void InputStreamHandler::AddPackets(CollectionItemId id,
LogQueuedPackets(GetCalculatorContext(calculator_context_manager_),
input_stream_managers_.Get(id), packets.back());
bool notify = false;
mediapipe::Status result =
absl::Status result =
input_stream_managers_.Get(id)->AddPackets(packets, &notify);
if (!result.ok()) {
error_callback_(result);
@@ -275,7 +275,7 @@ void InputStreamHandler::MovePackets(CollectionItemId id,
LogQueuedPackets(GetCalculatorContext(calculator_context_manager_),
input_stream_managers_.Get(id), packets->back());
bool notify = false;
mediapipe::Status result =
absl::Status result =
input_stream_managers_.Get(id)->MovePackets(packets, &notify);
if (!result.ok()) {
error_callback_(result);
@@ -288,7 +288,7 @@ void InputStreamHandler::MovePackets(CollectionItemId id,
void InputStreamHandler::SetNextTimestampBound(CollectionItemId id,
Timestamp bound) {
bool notify = false;
mediapipe::Status result =
absl::Status result =
input_stream_managers_.Get(id)->SetNextTimestampBound(bound, &notify);
if (!result.ok()) {
error_callback_(result);
+4 -4
View File
@@ -84,13 +84,13 @@ class InputStreamHandler {
// InputStreamHandler::input_stream_managers_ (meaning it should point
// to somewhere in the middle of the master flat array of all input
// stream managers).
mediapipe::Status InitializeInputStreamManagers(
absl::Status InitializeInputStreamManagers(
InputStreamManager* flat_input_stream_managers);
InputStreamManager* GetInputStreamManager(CollectionItemId id);
// Sets up the InputStreamShardSet by propagating data from the managers.
mediapipe::Status SetupInputShards(InputStreamShardSet* input_shards);
absl::Status SetupInputShards(InputStreamShardSet* input_shards);
// Returns a vector of pairs of stream name and queue size for monitoring
// purpose.
@@ -106,7 +106,7 @@ class InputStreamHandler {
std::function<void()> headers_ready_callback,
std::function<void()> notification_callback,
std::function<void(CalculatorContext*)> schedule_callback,
std::function<void(mediapipe::Status)> error_callback);
std::function<void(absl::Status)> error_callback);
int NumInputStreams() const { return input_stream_managers_.NumEntries(); }
@@ -286,7 +286,7 @@ class InputStreamHandler {
std::function<void()> notification_;
// A callback to schedule the node with the prepared calculator context.
std::function<void(CalculatorContext*)> schedule_callback_;
std::function<void(mediapipe::Status)> error_callback_;
std::function<void(absl::Status)> error_callback_;
private:
// Indicates when to fill the input set. If true, every input set will be
+19 -19
View File
@@ -27,14 +27,14 @@
namespace mediapipe {
mediapipe::Status InputStreamManager::Initialize(const std::string& name,
const PacketType* packet_type,
bool back_edge) {
absl::Status InputStreamManager::Initialize(const std::string& name,
const PacketType* packet_type,
bool back_edge) {
name_ = name;
packet_type_ = packet_type;
back_edge_ = back_edge;
PrepareForRun();
return mediapipe::OkStatus();
return absl::OkStatus();
}
const std::string& InputStreamManager::Name() const { return name_; }
@@ -70,29 +70,29 @@ Packet InputStreamManager::QueueHead() const {
return queue_.front();
}
mediapipe::Status InputStreamManager::SetHeader(const Packet& header) {
absl::Status InputStreamManager::SetHeader(const Packet& header) {
if (header.Timestamp() != Timestamp::Unset()) {
return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC)
<< "Headers must not have a timestamp. Stream: \"" << name_
<< "\".";
}
header_ = header;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status InputStreamManager::AddPackets(
const std::list<Packet>& container, bool* notify) {
absl::Status InputStreamManager::AddPackets(const std::list<Packet>& container,
bool* notify) {
return AddOrMovePacketsInternal<const std::list<Packet>&>(container, notify);
}
mediapipe::Status InputStreamManager::MovePackets(std::list<Packet>* container,
bool* notify) {
absl::Status InputStreamManager::MovePackets(std::list<Packet>* container,
bool* notify) {
return AddOrMovePacketsInternal<std::list<Packet>&>(*container, notify);
}
template <typename Container>
mediapipe::Status InputStreamManager::AddOrMovePacketsInternal(
Container container, bool* notify) {
absl::Status InputStreamManager::AddOrMovePacketsInternal(Container container,
bool* notify) {
*notify = false;
bool queue_became_non_empty = false;
bool queue_became_full = false;
@@ -100,7 +100,7 @@ mediapipe::Status InputStreamManager::AddOrMovePacketsInternal(
// Scope to prevent locking the stream when notification is called.
absl::MutexLock stream_lock(&stream_mutex_);
if (closed_) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
// Check if the queue was full before packets came in.
bool was_queue_full =
@@ -108,7 +108,7 @@ mediapipe::Status InputStreamManager::AddOrMovePacketsInternal(
// Check if the queue becomes non-empty.
queue_became_non_empty = queue_.empty() && !container.empty();
for (auto& packet : container) {
mediapipe::Status result = packet_type_->Validate(packet);
absl::Status result = packet_type_->Validate(packet);
if (!result.ok()) {
return tool::AddStatusPrefix(
absl::StrCat(
@@ -177,17 +177,17 @@ mediapipe::Status InputStreamManager::AddOrMovePacketsInternal(
becomes_full_callback_(this, &last_reported_stream_full_);
}
*notify = queue_became_non_empty;
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status InputStreamManager::SetNextTimestampBound(
const Timestamp bound, bool* notify) {
absl::Status InputStreamManager::SetNextTimestampBound(const Timestamp bound,
bool* notify) {
*notify = false;
{
// Scope to prevent locking the stream when notification is called.
absl::MutexLock stream_lock(&stream_mutex_);
if (closed_) {
return mediapipe::OkStatus();
return absl::OkStatus();
}
if (enable_timestamps_ && bound < next_timestamp_bound_) {
@@ -211,7 +211,7 @@ mediapipe::Status InputStreamManager::SetNextTimestampBound(
}
}
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
void InputStreamManager::DisableTimestamps() { enable_timestamps_ = false; }
+7 -8
View File
@@ -57,8 +57,8 @@ class InputStreamManager {
InputStreamManager() = default;
// Initializes the InputStreamManager.
mediapipe::Status Initialize(const std::string& name,
const PacketType* packet_type, bool back_edge);
absl::Status Initialize(const std::string& name,
const PacketType* packet_type, bool back_edge);
// Returns the stream name.
const std::string& Name() const;
@@ -67,7 +67,7 @@ class InputStreamManager {
bool BackEdge() const { return back_edge_; }
// Sets the header Packet.
mediapipe::Status SetHeader(const Packet& header);
absl::Status SetHeader(const Packet& header);
const Packet& Header() const { return header_; }
@@ -87,13 +87,12 @@ class InputStreamManager {
// Timestamp::PostStream(), the packet must be the only packet in the
// stream.
// Violation of any of these conditions causes an error status.
mediapipe::Status AddPackets(const std::list<Packet>& container,
bool* notify);
absl::Status AddPackets(const std::list<Packet>& container, bool* notify);
// Move a list of timestamped packets. Sets "notify" to true if the queue
// becomes non-empty. Does nothing if the input stream is closed. After the
// move, all packets in the container must be empty.
mediapipe::Status MovePackets(std::list<Packet>* container, bool* notify);
absl::Status MovePackets(std::list<Packet>* container, bool* notify);
// Closes the input stream. This function can be called multiple times.
void Close() ABSL_LOCKS_EXCLUDED(stream_mutex_);
@@ -103,7 +102,7 @@ class InputStreamManager {
// empty. Returns an error status if this decreases the bound, unless
// DisableTimestamps() is called. Does nothing if the input stream is
// closed.
mediapipe::Status SetNextTimestampBound(Timestamp bound, bool* notify)
absl::Status SetNextTimestampBound(Timestamp bound, bool* notify)
ABSL_LOCKS_EXCLUDED(stream_mutex_);
// Returns the smallest timestamp at which we might see an input in
@@ -182,7 +181,7 @@ class InputStreamManager {
// Otherwise, the caller must be MovePackets() and Container should be
// non-const reference.
template <typename Container>
mediapipe::Status AddOrMovePacketsInternal(Container container, bool* notify)
absl::Status AddOrMovePacketsInternal(Container container, bool* notify)
ABSL_LOCKS_EXCLUDED(stream_mutex_);
// Returns true if the next timestamp bound reaches Timestamp::Done().
@@ -133,7 +133,7 @@ TEST_F(InputStreamManagerTest, AddPacketUnset) {
packets.push_back(MakePacket<std::string>("packet 1").At(Timestamp::Unset()));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(), testing::HasSubstr("Timestamp::Unset()"));
EXPECT_FALSE(notify_);
@@ -145,7 +145,7 @@ TEST_F(InputStreamManagerTest, AddPacketUnstarted) {
MakePacket<std::string>("packet 1").At(Timestamp::Unstarted()));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(), testing::HasSubstr("Timestamp::Unstarted()"));
EXPECT_FALSE(notify_);
@@ -157,7 +157,7 @@ TEST_F(InputStreamManagerTest, AddPacketOneOverPostStream) {
MakePacket<std::string>("packet 1").At(Timestamp::OneOverPostStream()));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(),
testing::HasSubstr("Timestamp::OneOverPostStream()"));
@@ -169,7 +169,7 @@ TEST_F(InputStreamManagerTest, AddPacketDone) {
packets.push_back(MakePacket<std::string>("packet 1").At(Timestamp::Done()));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(), testing::HasSubstr("Timestamp::Done()"));
EXPECT_FALSE(notify_);
@@ -196,7 +196,7 @@ TEST_F(InputStreamManagerTest, AddPacketsAfterPreStream) {
packets.push_back(MakePacket<std::string>("packet 2").At(Timestamp(10)));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(),
testing::HasSubstr("Timestamp::OneOverPostStream()"));
@@ -224,7 +224,7 @@ TEST_F(InputStreamManagerTest, AddPacketsBeforePostStream) {
MakePacket<std::string>("packet 2").At(Timestamp::PostStream()));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(), testing::HasSubstr("Timestamp::PostStream()"));
EXPECT_FALSE(notify_);
@@ -237,7 +237,7 @@ TEST_F(InputStreamManagerTest, AddPacketsReverseTimestamps) {
packets.push_back(MakePacket<std::string>("packet 3").At(Timestamp(30)));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(),
testing::HasSubstr(
@@ -398,7 +398,7 @@ TEST_F(InputStreamManagerTest, BadPacketType) {
packets.push_back(MakePacket<int>(10).At(Timestamp(10)));
EXPECT_TRUE(input_stream_manager_->IsEmpty());
mediapipe::Status result =
absl::Status result =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result.message(), testing::HasSubstr("Packet type mismatch"));
EXPECT_FALSE(notify_);
@@ -543,7 +543,7 @@ TEST_F(InputStreamManagerTest, BackwardsInTime) {
EXPECT_FALSE(notify_);
notify_ = false;
mediapipe::Status result = input_stream_manager_->SetNextTimestampBound(
absl::Status result = input_stream_manager_->SetNextTimestampBound(
Timestamp(40), &notify_); // Set Timestamp bound backwards in time.
ASSERT_THAT(result.message(), testing::HasSubstr("40"));
ASSERT_THAT(result.message(), testing::HasSubstr("50"));
@@ -554,7 +554,7 @@ TEST_F(InputStreamManagerTest, BackwardsInTime) {
packets.clear();
packets.push_back(MakePacket<std::string>("packet 3")
.At(Timestamp(30))); // Backwards in time
mediapipe::Status result2 =
absl::Status result2 =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result2.message(), testing::HasSubstr("50"));
ASSERT_THAT(result2.message(), testing::HasSubstr("30"));
@@ -585,7 +585,7 @@ TEST_F(InputStreamManagerTest, BackwardsInTime) {
packets.clear();
packets.push_back(MakePacket<std::string>("packet 5")
.At(Timestamp(130))); // Backwards in time.
mediapipe::Status result3 =
absl::Status result3 =
input_stream_manager_->AddPackets(packets, &notify_); // No notification
ASSERT_THAT(result3.message(), testing::HasSubstr("151"));
ASSERT_THAT(result3.message(), testing::HasSubstr("130"));
+30
View File
@@ -0,0 +1,30 @@
"""Macro for multi-platform C++ tests."""
DEFAULT_ADDITIONAL_TEST_DEPS = []
def mediapipe_cc_test(
name,
srcs = [],
data = [],
deps = [],
size = None,
timeout = None,
additional_deps = DEFAULT_ADDITIONAL_TEST_DEPS,
**kwargs):
# Note: additional_deps are MediaPipe-specific test support deps added by default.
# They are provided as a default argument so they can be disabled if desired.
native.cc_library(
name = name + "_lib",
testonly = 1,
srcs = srcs,
data = data,
deps = deps + additional_deps,
alwayslink = 1,
)
native.cc_test(
name = name,
size = size,
timeout = timeout,
deps = [":{}_lib".format(name)],
)
@@ -20,21 +20,21 @@
namespace mediapipe {
mediapipe::Status OutputSidePacketImpl::Initialize(
const std::string& name, const PacketType* packet_type) {
absl::Status OutputSidePacketImpl::Initialize(const std::string& name,
const PacketType* packet_type) {
name_ = name;
packet_type_ = packet_type;
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputSidePacketImpl::PrepareForRun(
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
error_callback_ = std::move(error_callback);
initialized_ = false;
}
void OutputSidePacketImpl::Set(const Packet& packet) {
mediapipe::Status status = SetInternal(packet);
absl::Status status = SetInternal(packet);
if (!status.ok()) {
TriggerErrorCallback(status);
}
@@ -46,7 +46,7 @@ void OutputSidePacketImpl::AddMirror(
mirrors_.emplace_back(input_side_packet_handler, id);
}
mediapipe::Status OutputSidePacketImpl::SetInternal(const Packet& packet) {
absl::Status OutputSidePacketImpl::SetInternal(const Packet& packet) {
if (initialized_) {
return mediapipe::AlreadyExistsErrorBuilder(MEDIAPIPE_LOC)
<< "Output side packet \"" << name_ << "\" was already set.";
@@ -63,7 +63,7 @@ mediapipe::Status OutputSidePacketImpl::SetInternal(const Packet& packet) {
<< packet.Timestamp().DebugString() << ".";
}
mediapipe::Status result = packet_type_->Validate(packet);
absl::Status result = packet_type_->Validate(packet);
if (!result.ok()) {
return mediapipe::StatusBuilder(result, MEDIAPIPE_LOC).SetPrepend()
<< absl::StrCat(
@@ -76,11 +76,11 @@ mediapipe::Status OutputSidePacketImpl::SetInternal(const Packet& packet) {
for (const auto& mirror : mirrors_) {
mirror.input_side_packet_handler->Set(mirror.id, packet_);
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputSidePacketImpl::TriggerErrorCallback(
const mediapipe::Status& status) const {
const absl::Status& status) const {
CHECK(error_callback_);
error_callback_(status);
}
@@ -35,13 +35,13 @@ class OutputSidePacketImpl : public OutputSidePacket {
~OutputSidePacketImpl() override = default;
// Initializes the OutputSidePacketImpl.
mediapipe::Status Initialize(const std::string& name,
const PacketType* packet_type);
absl::Status Initialize(const std::string& name,
const PacketType* packet_type);
// Prepares this for processing. If an error occurs in a user called function
// (such as Set()) then error_callback will be called before returning
// control to the user.
void PrepareForRun(std::function<void(mediapipe::Status)> error_callback);
void PrepareForRun(std::function<void(absl::Status)> error_callback);
// Gets the output side packet.
Packet GetPacket() const { return packet_; }
@@ -70,15 +70,15 @@ class OutputSidePacketImpl : public OutputSidePacket {
};
// Called by Set().
mediapipe::Status SetInternal(const Packet& packet);
absl::Status SetInternal(const Packet& packet);
// Triggers the error callback with mediapipe::Status info when an error
// Triggers the error callback with absl::Status info when an error
// occurs.
void TriggerErrorCallback(const mediapipe::Status& status) const;
void TriggerErrorCallback(const absl::Status& status) const;
std::string name_;
const PacketType* packet_type_;
std::function<void(mediapipe::Status)> error_callback_;
std::function<void(absl::Status)> error_callback_;
Packet packet_;
bool initialized_ = false;
+5 -5
View File
@@ -20,16 +20,16 @@
namespace mediapipe {
mediapipe::Status OutputStreamHandler::InitializeOutputStreamManagers(
absl::Status OutputStreamHandler::InitializeOutputStreamManagers(
OutputStreamManager* flat_output_stream_managers) {
for (CollectionItemId id = output_stream_managers_.BeginId();
id < output_stream_managers_.EndId(); ++id) {
output_stream_managers_.Get(id) = &flat_output_stream_managers[id.value()];
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
mediapipe::Status OutputStreamHandler::SetupOutputShards(
absl::Status OutputStreamHandler::SetupOutputShards(
OutputStreamShardSet* output_shards) {
CHECK(output_shards);
for (CollectionItemId id = output_stream_managers_.BeginId();
@@ -37,11 +37,11 @@ mediapipe::Status OutputStreamHandler::SetupOutputShards(
OutputStreamManager* manager = output_stream_managers_.Get(id);
output_shards->Get(id).SetSpec(manager->Spec());
}
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputStreamHandler::PrepareForRun(
const std::function<void(mediapipe::Status)>& error_callback) {
const std::function<void(absl::Status)>& error_callback) {
for (auto& manager : output_stream_managers_) {
manager->PrepareForRun(error_callback);
}
+3 -4
View File
@@ -76,11 +76,11 @@ class OutputStreamHandler {
// OutputStreamHandler::output_stream_managers_ (meaning it should
// point to somewhere in the middle of the master flat array of all
// output stream managers).
mediapipe::Status InitializeOutputStreamManagers(
absl::Status InitializeOutputStreamManagers(
OutputStreamManager* flat_output_stream_managers);
// Sets up output shards by connecting to the managers.
mediapipe::Status SetupOutputShards(OutputStreamShardSet* output_shards);
absl::Status SetupOutputShards(OutputStreamShardSet* output_shards);
int NumOutputStreams() const { return output_stream_managers_.NumEntries(); }
@@ -91,8 +91,7 @@ class OutputStreamHandler {
// Calls OutputStreamManager::PrepareForRun(error_callback) per stream, and
// resets data memebers.
void PrepareForRun(
const std::function<void(mediapipe::Status)>& error_callback)
void PrepareForRun(const std::function<void(absl::Status)>& error_callback)
ABSL_LOCKS_EXCLUDED(timestamp_mutex_);
// Marks the output streams as started and propagates any changes made in
+4 -5
View File
@@ -20,17 +20,17 @@
namespace mediapipe {
mediapipe::Status OutputStreamManager::Initialize(
const std::string& name, const PacketType* packet_type) {
absl::Status OutputStreamManager::Initialize(const std::string& name,
const PacketType* packet_type) {
output_stream_spec_.name = name;
output_stream_spec_.packet_type = packet_type;
output_stream_spec_.offset_enabled = false;
PrepareForRun(nullptr);
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputStreamManager::PrepareForRun(
std::function<void(mediapipe::Status)> error_callback) {
std::function<void(absl::Status)> error_callback) {
output_stream_spec_.error_callback = std::move(error_callback);
output_stream_spec_.locked_intro_data = false;
@@ -117,7 +117,6 @@ Timestamp OutputStreamManager::ComputeOutputTimestampBound(
// MaxOutputTimestamp(completed_timestamp) + 1)
// Note that "MaxOutputTimestamp()" must consider both output packet
// timetstamp and SetNextTimestampBound values.
// See the timestamp mapping section in go/mediapipe-bounds for details.
Timestamp input_bound;
if (output_stream_spec_.offset_enabled &&
input_timestamp != Timestamp::Unstarted()) {
+4 -5
View File
@@ -40,13 +40,13 @@ class OutputStreamManager {
OutputStreamManager() = default;
// Initializes the OutputStreamManager.
mediapipe::Status Initialize(const std::string& name,
const PacketType* packet_type);
absl::Status Initialize(const std::string& name,
const PacketType* packet_type);
// Prepares this for processing. If an error occurs in a user called function
// (such as AddPacket()) then error_callback will be called before returning
// control to the user.
void PrepareForRun(std::function<void(mediapipe::Status)> error_callback);
void PrepareForRun(std::function<void(absl::Status)> error_callback);
// Gets the stream name.
const std::string& Name() const { return output_stream_spec_.name; }
@@ -85,8 +85,7 @@ class OutputStreamManager {
// Computes the output timestamp bound based on the input timestamp, the
// timestamp of the last added packet, and the next timestamp bound from
// the OutputStreamShard. See the timestamp mapping section in
// go/mediapipe-bounds for details.
// the OutputStreamShard.
// The function is invoked by OutputStreamHandler after the calculator node
// finishes a call to Calculator::Process().
Timestamp ComputeOutputTimestampBound(
@@ -58,13 +58,13 @@ class OutputStreamManagerTest : public ::testing::Test {
output_stream_shard_.SetSpec(output_stream_manager_->Spec());
output_stream_manager_->ResetShard(&output_stream_shard_);
std::shared_ptr<tool::TagMap> tag_map = tool::CreateTagMap(1).ValueOrDie();
mediapipe::StatusOr<std::unique_ptr<mediapipe::InputStreamHandler>>
std::shared_ptr<tool::TagMap> tag_map = tool::CreateTagMap(1).value();
absl::StatusOr<std::unique_ptr<mediapipe::InputStreamHandler>>
status_or_handler = InputStreamHandlerRegistry::CreateByName(
"DefaultInputStreamHandler", tag_map, /*cc_manager=*/nullptr,
MediaPipeOptions(), /*calculator_run_in_parallel=*/false);
ASSERT_TRUE(status_or_handler.ok());
input_stream_handler_ = std::move(status_or_handler.ValueOrDie());
input_stream_handler_ = std::move(status_or_handler.value());
const CollectionItemId& id = tag_map->BeginId();
MP_ASSERT_OK(input_stream_manager_.Initialize("a_test", &packet_type_,
@@ -85,7 +85,7 @@ class OutputStreamManagerTest : public ::testing::Test {
void ScheduleNoOp(CalculatorContext* cc) {}
void RecordError(const mediapipe::Status& error) { errors_.push_back(error); }
void RecordError(const absl::Status& error) { errors_.push_back(error); }
void ReportQueueNoOp(InputStreamManager* stream, bool* stream_was_full) {}
@@ -104,7 +104,7 @@ class OutputStreamManagerTest : public ::testing::Test {
std::function<void()> headers_ready_callback_;
std::function<void()> notification_callback_;
std::function<void(CalculatorContext*)> schedule_callback_;
std::function<void(mediapipe::Status)> error_callback_;
std::function<void(absl::Status)> error_callback_;
InputStreamManager::QueueSizeCallback queue_full_callback_;
InputStreamManager::QueueSizeCallback queue_not_full_callback_;
@@ -114,7 +114,7 @@ class OutputStreamManagerTest : public ::testing::Test {
InputStreamManager input_stream_manager_;
// Vector of errors encountered while using the stream.
std::vector<mediapipe::Status> errors_;
std::vector<absl::Status> errors_;
};
TEST_F(OutputStreamManagerTest, Init) {}
+1 -1
View File
@@ -128,7 +128,7 @@ Status OutputStreamShard::AddPacketInternal(T&& packet) {
// TODO debug log?
return mediapipe::OkStatus();
return absl::OkStatus();
}
void OutputStreamShard::AddPacket(const Packet& packet) {
+4 -4
View File
@@ -31,16 +31,16 @@ class OutputStreamManager;
// The output stream spec shared across all output stream shards and their
// output stream manager.
struct OutputStreamSpec {
// Triggers the error callback with mediapipe::Status info when an error
// Triggers the error callback with absl::Status info when an error
// occurs.
void TriggerErrorCallback(const mediapipe::Status& status) const {
void TriggerErrorCallback(const absl::Status& status) const {
CHECK(error_callback);
error_callback(status);
}
std::string name;
const PacketType* packet_type;
std::function<void(mediapipe::Status)> error_callback;
std::function<void(absl::Status)> error_callback;
bool locked_intro_data;
// Those three variables are the intro data protected by locked_intro_data.
bool offset_enabled;
@@ -102,7 +102,7 @@ class OutputStreamShard : public OutputStream {
// AddPacketInternal template is called by either AddPacket(Packet&& packet)
// or AddPacket(const Packet& packet).
template <typename T>
mediapipe::Status AddPacketInternal(T&& packet);
absl::Status AddPacketInternal(T&& packet);
// Returns a pointer to the output queue.
std::list<Packet>* OutputQueue() { return &output_queue_; }

Some files were not shown because too many files have changed in this diff Show More