From a183212a13df9f28c27b086fcf5244d8927b368a Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Mon, 14 Aug 2023 18:47:49 -0700 Subject: [PATCH] Header for callback_packet_calculator to allow dynamic registration for superusers PiperOrigin-RevId: 556977122 --- mediapipe/calculators/internal/BUILD | 2 + .../internal/callback_packet_calculator.cc | 97 +++++++++---------- .../internal/callback_packet_calculator.h | 39 ++++++++ 3 files changed, 86 insertions(+), 52 deletions(-) create mode 100644 mediapipe/calculators/internal/callback_packet_calculator.h diff --git a/mediapipe/calculators/internal/BUILD b/mediapipe/calculators/internal/BUILD index a92a2f252..a5d82e134 100644 --- a/mediapipe/calculators/internal/BUILD +++ b/mediapipe/calculators/internal/BUILD @@ -31,12 +31,14 @@ mediapipe_proto_library( cc_library( name = "callback_packet_calculator", srcs = ["callback_packet_calculator.cc"], + hdrs = ["callback_packet_calculator.h"], visibility = ["//mediapipe/framework:__subpackages__"], deps = [ ":callback_packet_calculator_cc_proto", "//mediapipe/framework:calculator_base", "//mediapipe/framework:calculator_registry", "//mediapipe/framework:output_side_packet", + "@com_google_absl//absl/status", ], alwayslink = 1, ) diff --git a/mediapipe/calculators/internal/callback_packet_calculator.cc b/mediapipe/calculators/internal/callback_packet_calculator.cc index cc153483e..aa86c0617 100644 --- a/mediapipe/calculators/internal/callback_packet_calculator.cc +++ b/mediapipe/calculators/internal/callback_packet_calculator.cc @@ -11,10 +11,12 @@ // 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/calculators/internal/callback_packet_calculator.h" #include #include +#include "absl/status/status.h" #include "mediapipe/calculators/internal/callback_packet_calculator.pb.h" // NOLINT #include "mediapipe/framework/calculator_base.h" #include "mediapipe/framework/calculator_registry.h" @@ -39,64 +41,55 @@ void DumpPostStreamPacket(Packet* post_stream_packet, const Packet& packet) { *post_stream_packet = packet; } } + } // namespace -// Creates a callback which takes a packet and stores it either in a -// vector of packets or stores only the packet at PostStream timestamp. -// The kind of callback is controlled by an option. The callback is -// a std::function and is directly usable by CallbackCalculator. -// Since the options for the packet generator include a serialized pointer -// value, the resulting callback is only valid on the original machine -// while that pointer is still alive. -class CallbackPacketCalculator : public CalculatorBase { - public: - static absl::Status GetContract(CalculatorContract* cc) { - const auto& options = cc->Options(); - switch (options.type()) { - case CallbackPacketCalculatorOptions::VECTOR_PACKET: - case CallbackPacketCalculatorOptions::POST_STREAM_PACKET: - cc->OutputSidePackets() - .Index(0) - .Set>(); - break; - default: - return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC) - << "Invalid type of callback to produce."; - } - return absl::OkStatus(); - } - - absl::Status Open(CalculatorContext* cc) override { - const auto& options = cc->Options(); - void* ptr; - if (sscanf(options.pointer().c_str(), "%p", &ptr) != 1) { +absl::Status CallbackPacketCalculator::GetContract(CalculatorContract* cc) { + const auto& options = cc->Options(); + switch (options.type()) { + case CallbackPacketCalculatorOptions::VECTOR_PACKET: + case CallbackPacketCalculatorOptions::POST_STREAM_PACKET: + cc->OutputSidePackets() + .Index(0) + .Set>(); + break; + default: return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC) - << "Stored pointer value in options is invalid."; - } - switch (options.type()) { - case CallbackPacketCalculatorOptions::VECTOR_PACKET: - cc->OutputSidePackets().Index(0).Set( - MakePacket>(std::bind( - &DumpToVector, reinterpret_cast*>(ptr), - std::placeholders::_1))); - break; - case CallbackPacketCalculatorOptions::POST_STREAM_PACKET: - cc->OutputSidePackets().Index(0).Set( - MakePacket>( - std::bind(&DumpPostStreamPacket, reinterpret_cast(ptr), - std::placeholders::_1))); - break; - default: - return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC) - << "Invalid type to dump into."; - } - return absl::OkStatus(); + << "Invalid type of callback to produce."; } + return absl::OkStatus(); +} - absl::Status Process(CalculatorContext* cc) override { - return absl::OkStatus(); +absl::Status CallbackPacketCalculator::Open(CalculatorContext* cc) { + const auto& options = cc->Options(); + void* ptr; + if (sscanf(options.pointer().c_str(), "%p", &ptr) != 1) { + return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC) + << "Stored pointer value in options is invalid."; } -}; + switch (options.type()) { + case CallbackPacketCalculatorOptions::VECTOR_PACKET: + cc->OutputSidePackets().Index(0).Set( + MakePacket>(std::bind( + &DumpToVector, reinterpret_cast*>(ptr), + std::placeholders::_1))); + break; + case CallbackPacketCalculatorOptions::POST_STREAM_PACKET: + cc->OutputSidePackets().Index(0).Set( + MakePacket>( + std::bind(&DumpPostStreamPacket, reinterpret_cast(ptr), + std::placeholders::_1))); + break; + default: + return mediapipe::InvalidArgumentErrorBuilder(MEDIAPIPE_LOC) + << "Invalid type to dump into."; + } + return absl::OkStatus(); +} + +absl::Status CallbackPacketCalculator::Process(CalculatorContext* cc) { + return absl::OkStatus(); +} REGISTER_CALCULATOR(CallbackPacketCalculator); diff --git a/mediapipe/calculators/internal/callback_packet_calculator.h b/mediapipe/calculators/internal/callback_packet_calculator.h new file mode 100644 index 000000000..e0b170e36 --- /dev/null +++ b/mediapipe/calculators/internal/callback_packet_calculator.h @@ -0,0 +1,39 @@ +// Copyright 2023 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_CALCULATORS_INTERNAL_CALLBACK_PACKET_CALCULATOR_H_ +#define MEDIAPIPE_CALCULATORS_INTERNAL_CALLBACK_PACKET_CALCULATOR_H_ + +#include "absl/status/status.h" +#include "mediapipe/framework/calculator_base.h" + +namespace mediapipe { + +// Creates a callback which takes a packet and stores it either in a +// vector of packets or stores only the packet at PostStream timestamp. +// The kind of callback is controlled by an option. The callback is +// a std::function and is directly usable by CallbackCalculator. +// Since the options for the packet generator include a serialized pointer +// value, the resulting callback is only valid on the original machine +// while that pointer is still alive. +class CallbackPacketCalculator : public CalculatorBase { + public: + static absl::Status GetContract(CalculatorContract* cc); + absl::Status Open(CalculatorContext* cc) override; + absl::Status Process(CalculatorContext* cc) override; +}; + +} // namespace mediapipe + +#endif // MEDIAPIPE_CALCULATORS_INTERNAL_CALLBACK_PACKET_CALCULATOR_H_