Header for callback_packet_calculator to allow dynamic registration for superusers

PiperOrigin-RevId: 556977122
This commit is contained in:
MediaPipe Team 2023-08-14 18:47:49 -07:00 committed by Copybara-Service
parent 9c5bdd2eb9
commit a183212a13
3 changed files with 86 additions and 52 deletions

View File

@ -31,12 +31,14 @@ mediapipe_proto_library(
cc_library( cc_library(
name = "callback_packet_calculator", name = "callback_packet_calculator",
srcs = ["callback_packet_calculator.cc"], srcs = ["callback_packet_calculator.cc"],
hdrs = ["callback_packet_calculator.h"],
visibility = ["//mediapipe/framework:__subpackages__"], visibility = ["//mediapipe/framework:__subpackages__"],
deps = [ deps = [
":callback_packet_calculator_cc_proto", ":callback_packet_calculator_cc_proto",
"//mediapipe/framework:calculator_base", "//mediapipe/framework:calculator_base",
"//mediapipe/framework:calculator_registry", "//mediapipe/framework:calculator_registry",
"//mediapipe/framework:output_side_packet", "//mediapipe/framework:output_side_packet",
"@com_google_absl//absl/status",
], ],
alwayslink = 1, alwayslink = 1,
) )

View File

@ -11,10 +11,12 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "mediapipe/calculators/internal/callback_packet_calculator.h"
#include <functional> #include <functional>
#include <string> #include <string>
#include "absl/status/status.h"
#include "mediapipe/calculators/internal/callback_packet_calculator.pb.h" // NOLINT #include "mediapipe/calculators/internal/callback_packet_calculator.pb.h" // NOLINT
#include "mediapipe/framework/calculator_base.h" #include "mediapipe/framework/calculator_base.h"
#include "mediapipe/framework/calculator_registry.h" #include "mediapipe/framework/calculator_registry.h"
@ -39,18 +41,10 @@ void DumpPostStreamPacket(Packet* post_stream_packet, const Packet& packet) {
*post_stream_packet = packet; *post_stream_packet = packet;
} }
} }
} // namespace } // namespace
// Creates a callback which takes a packet and stores it either in a absl::Status CallbackPacketCalculator::GetContract(CalculatorContract* cc) {
// 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<CallbackPacketCalculatorOptions>(); const auto& options = cc->Options<CallbackPacketCalculatorOptions>();
switch (options.type()) { switch (options.type()) {
case CallbackPacketCalculatorOptions::VECTOR_PACKET: case CallbackPacketCalculatorOptions::VECTOR_PACKET:
@ -66,7 +60,7 @@ class CallbackPacketCalculator : public CalculatorBase {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::Status Open(CalculatorContext* cc) override { absl::Status CallbackPacketCalculator::Open(CalculatorContext* cc) {
const auto& options = cc->Options<CallbackPacketCalculatorOptions>(); const auto& options = cc->Options<CallbackPacketCalculatorOptions>();
void* ptr; void* ptr;
if (sscanf(options.pointer().c_str(), "%p", &ptr) != 1) { if (sscanf(options.pointer().c_str(), "%p", &ptr) != 1) {
@ -93,10 +87,9 @@ class CallbackPacketCalculator : public CalculatorBase {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::Status Process(CalculatorContext* cc) override { absl::Status CallbackPacketCalculator::Process(CalculatorContext* cc) {
return absl::OkStatus(); return absl::OkStatus();
} }
};
REGISTER_CALCULATOR(CallbackPacketCalculator); REGISTER_CALCULATOR(CallbackPacketCalculator);

View File

@ -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_