Header for callback_packet_calculator to allow dynamic registration for superusers
PiperOrigin-RevId: 556977122
This commit is contained in:
parent
9c5bdd2eb9
commit
a183212a13
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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 <functional>
|
||||
#include <string>
|
||||
|
||||
#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<CallbackPacketCalculatorOptions>();
|
||||
switch (options.type()) {
|
||||
case CallbackPacketCalculatorOptions::VECTOR_PACKET:
|
||||
case CallbackPacketCalculatorOptions::POST_STREAM_PACKET:
|
||||
cc->OutputSidePackets()
|
||||
.Index(0)
|
||||
.Set<std::function<void(const Packet&)>>();
|
||||
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<CallbackPacketCalculatorOptions>();
|
||||
void* ptr;
|
||||
if (sscanf(options.pointer().c_str(), "%p", &ptr) != 1) {
|
||||
absl::Status CallbackPacketCalculator::GetContract(CalculatorContract* cc) {
|
||||
const auto& options = cc->Options<CallbackPacketCalculatorOptions>();
|
||||
switch (options.type()) {
|
||||
case CallbackPacketCalculatorOptions::VECTOR_PACKET:
|
||||
case CallbackPacketCalculatorOptions::POST_STREAM_PACKET:
|
||||
cc->OutputSidePackets()
|
||||
.Index(0)
|
||||
.Set<std::function<void(const Packet&)>>();
|
||||
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::function<void(const Packet&)>>(std::bind(
|
||||
&DumpToVector, reinterpret_cast<std::vector<Packet>*>(ptr),
|
||||
std::placeholders::_1)));
|
||||
break;
|
||||
case CallbackPacketCalculatorOptions::POST_STREAM_PACKET:
|
||||
cc->OutputSidePackets().Index(0).Set(
|
||||
MakePacket<std::function<void(const Packet&)>>(
|
||||
std::bind(&DumpPostStreamPacket, reinterpret_cast<Packet*>(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<CallbackPacketCalculatorOptions>();
|
||||
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::function<void(const Packet&)>>(std::bind(
|
||||
&DumpToVector, reinterpret_cast<std::vector<Packet>*>(ptr),
|
||||
std::placeholders::_1)));
|
||||
break;
|
||||
case CallbackPacketCalculatorOptions::POST_STREAM_PACKET:
|
||||
cc->OutputSidePackets().Index(0).Set(
|
||||
MakePacket<std::function<void(const Packet&)>>(
|
||||
std::bind(&DumpPostStreamPacket, reinterpret_cast<Packet*>(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);
|
||||
|
||||
|
|
39
mediapipe/calculators/internal/callback_packet_calculator.h
Normal file
39
mediapipe/calculators/internal/callback_packet_calculator.h
Normal 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_
|
Loading…
Reference in New Issue
Block a user