2019-06-17 01:03:25 +02:00
|
|
|
// 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/input_side_packet_handler.h"
|
|
|
|
|
|
|
|
#include "mediapipe/framework/port/logging.h"
|
|
|
|
#include "mediapipe/framework/port/ret_check.h"
|
|
|
|
#include "mediapipe/framework/port/status_builder.h"
|
|
|
|
#include "mediapipe/framework/tool/fill_packet_set.h"
|
|
|
|
|
|
|
|
namespace mediapipe {
|
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
absl::Status InputSidePacketHandler::PrepareForRun(
|
2019-06-17 01:03:25 +02:00
|
|
|
const PacketTypeSet* input_side_packet_types,
|
|
|
|
const std::map<std::string, Packet>& all_side_packets,
|
|
|
|
std::function<void()> input_side_packets_ready_callback,
|
2021-02-27 09:21:16 +01:00
|
|
|
std::function<void(absl::Status)> error_callback) {
|
2019-06-17 01:03:25 +02:00
|
|
|
int missing_input_side_packet_count;
|
2019-10-25 23:12:58 +02:00
|
|
|
prev_input_side_packets_ = std::move(input_side_packets_);
|
2019-06-17 01:03:25 +02:00
|
|
|
ASSIGN_OR_RETURN(
|
|
|
|
input_side_packets_,
|
|
|
|
tool::FillPacketSet(*input_side_packet_types, all_side_packets,
|
|
|
|
&missing_input_side_packet_count));
|
|
|
|
|
|
|
|
input_side_packet_types_ = input_side_packet_types;
|
|
|
|
missing_input_side_packet_count_.store(missing_input_side_packet_count,
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
input_side_packets_ready_callback_ =
|
|
|
|
std::move(input_side_packets_ready_callback);
|
|
|
|
error_callback_ = std::move(error_callback);
|
2021-02-27 09:21:16 +01:00
|
|
|
return absl::OkStatus();
|
2019-06-17 01:03:25 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 23:12:58 +02:00
|
|
|
bool InputSidePacketHandler::InputSidePacketsChanged() {
|
|
|
|
return prev_input_side_packets_ == nullptr ||
|
|
|
|
input_side_packets_ == nullptr ||
|
|
|
|
*input_side_packets_ != *prev_input_side_packets_;
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:03:25 +02:00
|
|
|
void InputSidePacketHandler::Set(CollectionItemId id, const Packet& packet) {
|
2021-02-27 09:21:16 +01:00
|
|
|
absl::Status status = SetInternal(id, packet);
|
2019-06-17 01:03:25 +02:00
|
|
|
if (!status.ok()) {
|
|
|
|
TriggerErrorCallback(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-27 09:21:16 +01:00
|
|
|
absl::Status InputSidePacketHandler::SetInternal(CollectionItemId id,
|
|
|
|
const Packet& packet) {
|
2019-06-17 01:03:25 +02:00
|
|
|
RET_CHECK_GT(missing_input_side_packet_count_, 0);
|
|
|
|
Packet& side_packet = input_side_packets_->Get(id);
|
|
|
|
|
|
|
|
if (!side_packet.IsEmpty()) {
|
2020-12-10 04:13:05 +01:00
|
|
|
return mediapipe::AlreadyExistsErrorBuilder(MEDIAPIPE_LOC)
|
2019-06-17 01:03:25 +02:00
|
|
|
<< "Input side packet with id " << id << " was already set.";
|
|
|
|
}
|
2021-02-27 09:21:16 +01:00
|
|
|
absl::Status result = input_side_packet_types_->Get(id).Validate(packet);
|
2019-06-17 01:03:25 +02:00
|
|
|
if (!result.ok()) {
|
2020-12-10 04:13:05 +01:00
|
|
|
return mediapipe::StatusBuilder(result, MEDIAPIPE_LOC).SetPrepend()
|
2019-06-17 01:03:25 +02:00
|
|
|
<< absl::StrCat(
|
|
|
|
"Packet type mismatch on calculator input side packet with "
|
|
|
|
"id ",
|
|
|
|
id.value(), ": ");
|
|
|
|
}
|
|
|
|
side_packet = packet;
|
|
|
|
if (missing_input_side_packet_count_.fetch_sub(
|
|
|
|
1, std::memory_order_acq_rel) == 1) {
|
|
|
|
input_side_packets_ready_callback_();
|
|
|
|
}
|
2021-02-27 09:21:16 +01:00
|
|
|
return absl::OkStatus();
|
2019-06-17 01:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSidePacketHandler::TriggerErrorCallback(
|
2021-02-27 09:21:16 +01:00
|
|
|
const absl::Status& status) const {
|
2019-06-17 01:03:25 +02:00
|
|
|
CHECK(error_callback_);
|
|
|
|
error_callback_(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mediapipe
|