OlaGraph 编译通过

This commit is contained in:
Wang.Renzhu 2022-07-20 10:46:56 +08:00
parent 723fc6e46e
commit 64ed559d94
8 changed files with 9376 additions and 992 deletions

View File

@ -2,7 +2,7 @@ cc_library(
name = "FaceMeshGPULibrary", name = "FaceMeshGPULibrary",
copts = ["-std=c++17"], copts = ["-std=c++17"],
srcs = [ srcs = [
"face_mesh_module.hpp", "face_mesh_module.h",
], ],
hdrs = ["face_mesh_module.cpp"], hdrs = ["face_mesh_module.cpp"],
@ -14,6 +14,7 @@ cc_library(
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//mediapipe/render/module/common:olamodule_common_library", "//mediapipe/render/module/common:olamodule_common_library",
"//mediapipe/render/core:core",
] + select({ ] + select({
"//mediapipe:ios_i386": [], "//mediapipe:ios_i386": [],
"//mediapipe:ios_x86_64": [], "//mediapipe:ios_x86_64": [],

View File

@ -1,4 +1,4 @@
#include "face_mesh_module.hpp" #include "face_mesh_module.h"
namespace Opipe { namespace Opipe {
FaceMeshModule::FaceMeshModule() { FaceMeshModule::FaceMeshModule() {

View File

@ -1,4 +1,4 @@
#include "OlaGraph.hpp"
namespace Opipe { namespace Opipe {
class FaceMeshModule { class FaceMeshModule {

View File

@ -15,13 +15,13 @@ cc_library(
"//mediapipe/framework/port:status", "//mediapipe/framework/port:status",
"//mediapipe/framework/port:statusor", "//mediapipe/framework/port:statusor",
"//mediapipe/framework/port:threadpool", "//mediapipe/framework/port:threadpool",
"//mediapipe/framework:calculator_graph",
"//mediapipe/gpu:gl_base", "//mediapipe/gpu:gl_base",
"//mediapipe/gpu:gpu_buffer", "//mediapipe/gpu:gpu_buffer",
"//mediapipe/gpu:gpu_shared_data_internal", "//mediapipe/gpu:gpu_shared_data_internal",
"//mediapipe/gpu:graph_support", "//mediapipe/gpu:graph_support",
"//mediapipe/gpu:pixel_buffer_pool_util", "//mediapipe/gpu:pixel_buffer_pool_util",
"//mediapipe/util:cpu_util", "//mediapipe/util:cpu_util",
"//mediapipe/render/core:core",
"@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/memory", "@com_google_absl//absl/memory",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
@ -34,4 +34,12 @@ cc_library(
"//mediapipe/objc:util", "//mediapipe/objc:util",
], ],
}), }),
copts = select({
"//mediapipe:apple": [
"-x objective-c++",
"-fobjc-arc", # enable reference-counting
],
"//conditions:default": [],
}),
alwayslink = True,
) )

View File

@ -32,8 +32,8 @@ OlaGraph::~OlaGraph() {
} }
mediapipe::ProfilingContext OlaGraph::getProfiler() { mediapipe::ProfilingContext* OlaGraph::getProfiler() {
return _graph->getProfiler(); return _graph->profiler();
} }
void OlaGraph::setHeaderPacket(const mediapipe::Packet &packet, std::string streamName) { void OlaGraph::setHeaderPacket(const mediapipe::Packet &packet, std::string streamName) {
@ -45,29 +45,61 @@ void OlaGraph::setSidePacket(const mediapipe::Packet &packet, std::string name)
} }
void OlaGraph::setServicePacket(mediapipe::Packet &packet,const mediapipe::GraphServiceBase &service) { void OlaGraph::setServicePacket(mediapipe::Packet &packet,const mediapipe::GraphServiceBase &service) {
_servicePackets[&service] = std::move(packet);
} }
void OlaGraph::addSidePackets(const std::map<std::string, mediapipe::Packet> &extraSidePackets) { void OlaGraph::addSidePackets(const std::map<std::string, mediapipe::Packet> &extraSidePackets) {
_inputSidePackets.insert(extraSidePackets.begin(), extraSidePackets.end());
} }
void OlaGraph::addFrameOutputStream(const std::string &outputStreamName, void OlaGraph::addFrameOutputStream(const std::string &outputStreamName,
MPPPacketType packetType) { MPPPacketType packetType) {
std::string callbackInputName;
mediapipe::tool::AddCallbackCalculator(outputStreamName, &_config, &callbackInputName,
/*use_std_function=*/true);
// No matter what ownership qualifiers are put on the pointer, NewPermanentCallback will
// still end up with a strong pointer to MPPGraph*. That is why we use void* instead.
void* wrapperVoid = this;
_inputSidePackets[callbackInputName] =
mediapipe::MakePacket<std::function<void(const mediapipe::Packet&)>>( [wrapperVoid, outputStreamName, packetType]
(const mediapipe::Packet& packet) {
CallFrameDelegate(wrapperVoid, outputStreamName, packetType, packet);
});
} }
bool OlaGraph::start() { bool OlaGraph::start() {
absl::Status status = performStart();
_started = true;
return false; return false;
} }
absl::Status OlaGraph::performStart() {
absl::Status status = _graph->Initialize(_config);
if (!status.ok()) {
return status;
}
for (const auto& service_packet : _servicePackets) {
status = _graph->SetServicePacket(*service_packet.first, service_packet.second);
if (!status.ok()) {
return status;
}
}
status = _graph->StartRun(_inputSidePackets, _streamHeaders);
if (!status.ok()) {
return status;
}
return status;
}
bool OlaGraph::sendPacket(const mediapipe::Packet &packet, bool OlaGraph::sendPacket(const mediapipe::Packet &packet,
const std::string &streamName) { const std::string &streamName) {
return false; absl::Status status = _graph->AddPacketToInputStream(streamName, packet);
return status.ok();
} }
bool OlaGraph::movePacket(mediapipe::Packet &&packet, const std::string &streamName) { bool OlaGraph::movePacket(mediapipe::Packet &&packet, const std::string &streamName) {
return false; absl::Status status = _graph->AddPacketToInputStream(streamName, std::move(packet));
return status.ok();
} }
/// Sets the maximum queue size for a stream. Experimental feature, currently /// Sets the maximum queue size for a stream. Experimental feature, currently
@ -75,21 +107,45 @@ bool OlaGraph::movePacket(mediapipe::Packet &&packet, const std::string &streamN
/// graph. /// graph.
bool OlaGraph::setMaxQueueSize(int maxQueueSize, bool OlaGraph::setMaxQueueSize(int maxQueueSize,
const std::string &streamName) { const std::string &streamName) {
absl::Status status = _graph->SetInputStreamMaxQueueSize(streamName, maxQueueSize);
return status.ok();
} }
#if defined(__APPLE__) #if defined(__APPLE__)
/// Creates a MediaPipe packet wrapping the given pixelBuffer; /// Creates a MediaPipe packet wrapping the given pixelBuffer;
mediapipe::Packet OlaGraph::packetWithPixelBuffer(CVPixelBufferRef pixelBuffer, mediapipe::Packet OlaGraph::packetWithPixelBuffer(CVPixelBufferRef pixelBuffer,
MPPPacketType packetType) { MPPPacketType packetType) {
return 0; mediapipe::Packet packet;
if (packetType == MPPPacketTypeImageFrame || packetType == MPPPacketTypeImageFrameBGRANoSwap) {
auto frame = CreateImageFrameForCVPixelBuffer(
pixelBuffer, /* canOverwrite = */ false,
/* bgrAsRgb = */ packetType == MPPPacketTypeImageFrameBGRANoSwap);
packet = mediapipe::Adopt(frame.release());
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
} else if (packetType == MPPPacketTypePixelBuffer) {
packet = mediapipe::MakePacket<mediapipe::GpuBuffer>(pixelBuffer);
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
} else if (packetType == MPPPacketTypeImage) {
#if MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
// GPU
packet = mediapipe::MakePacket<mediapipe::Image>(pixelBuffer);
#else
// CPU
auto frame = CreateImageFrameForCVPixelBuffer(imageBuffer, /* canOverwrite = */ false,
/* bgrAsRgb = */ false);
packet = mediapipe::MakePacket<mediapipe::Image>(std::move(frame));
#endif // MEDIAPIPE_GPU_BUFFER_USE_CV_PIXEL_BUFFER
} else {
assert(false);
}
return packet;
} }
/// Creates a MediaPipe packet of type Image, wrapping the given CVPixelBufferRef. /// Creates a MediaPipe packet of type Image, wrapping the given CVPixelBufferRef.
mediapipe::Packet OlaGraph::imagePacketWithPixelBuffer(CVPixelBufferRef pixelBuffer) { mediapipe::Packet OlaGraph::imagePacketWithPixelBuffer(CVPixelBufferRef pixelBuffer) {
return 0; return packetWithPixelBuffer(pixelBuffer, MPPPacketTypeImage);
} }
/// Sends a pixel buffer into a graph input stream, using the specified packet /// Sends a pixel buffer into a graph input stream, using the specified packet
@ -102,7 +158,17 @@ bool OlaGraph::sendPixelBuffer(CVPixelBufferRef imageBuffer,
MPPPacketType packetType, MPPPacketType packetType,
const mediapipe::Timestamp &timestamp, const mediapipe::Timestamp &timestamp,
bool allowOverwrite) { bool allowOverwrite) {
return false; if (_maxFramesInFlight && _framesInFlight >= _maxFramesInFlight) return false;
mediapipe::Packet packet = packetWithPixelBuffer(imageBuffer, packetType);
bool success;
if (allowOverwrite) {
packet = std::move(packet).At(timestamp);
success = movePacket(std::move(packet), inputName);
} else {
success = sendPacket(packet.At(timestamp), inputName);
}
if (success) _framesInFlight++;
return success;
} }
/// Sends a pixel buffer into a graph input stream, using the specified packet /// Sends a pixel buffer into a graph input stream, using the specified packet
@ -113,7 +179,7 @@ bool OlaGraph::sendPixelBuffer(CVPixelBufferRef imageBuffer,
const std::string & inputName, const std::string & inputName,
MPPPacketType packetType, MPPPacketType packetType,
const mediapipe::Timestamp &timestamp) { const mediapipe::Timestamp &timestamp) {
return false; return sendPixelBuffer(imageBuffer, inputName, packetType, timestamp, false);
} }
/// Sends a pixel buffer into a graph input stream, using the specified packet /// Sends a pixel buffer into a graph input stream, using the specified packet
@ -124,32 +190,39 @@ bool OlaGraph::sendPixelBuffer(CVPixelBufferRef imageBuffer,
bool OlaGraph::sendPixelBuffer(CVPixelBufferRef imageBuffer, bool OlaGraph::sendPixelBuffer(CVPixelBufferRef imageBuffer,
const std::string & inputName, const std::string & inputName,
MPPPacketType packetType) { MPPPacketType packetType) {
return false; if (_frameTimestamp < mediapipe::Timestamp::Min()) {
_frameTimestamp = mediapipe::Timestamp::Min();
} else {
_frameTimestamp++;
}
return sendPixelBuffer(imageBuffer, inputName, packetType, _frameTimestamp);
} }
#endif #endif
/// Cancels a graph run. You must still call waitUntilDoneWithError: after this. /// Cancels a graph run. You must still call waitUntilDoneWithError: after this.
void OlaGraph::cancel() { void OlaGraph::cancel() {
_graph->Cancel();
} }
/// Check if the graph contains this input stream /// Check if the graph contains this input stream
bool OlaGraph::hasInputStream(const std::string &inputName) { bool OlaGraph::hasInputStream(const std::string &inputName) {
return false; return _graph->HasInputStream(inputName);
} }
/// Closes an input stream. /// Closes an input stream.
/// You must close all graph input streams before stopping the graph. /// You must close all graph input streams before stopping the graph.
/// @return YES if successful. /// @return YES if successful.
bool OlaGraph::closeInputStream(const std::string &inputName) { bool OlaGraph::closeInputStream(const std::string &inputName) {
return false; absl::Status status = _graph->CloseInputStream(inputName);
return status.ok();
} }
/// Closes all graph input streams. /// Closes all graph input streams.
/// @return YES if successful. /// @return YES if successful.
bool OlaGraph::closeAllInputStreams() { bool OlaGraph::closeAllInputStreams() {
return false; absl::Status status = _graph->CloseAllInputStreams();
return status.ok();
} }
/// Stops running the graph. /// Stops running the graph.
@ -158,12 +231,15 @@ bool OlaGraph::closeAllInputStreams() {
/// thread. /// thread.
/// @return YES if successful. /// @return YES if successful.
bool OlaGraph::waitUntilDone() { bool OlaGraph::waitUntilDone() {
return false; absl::Status status = _graph->WaitUntilDone();
_started = false;
return status.ok();
} }
/// Waits for the graph to become idle. /// Waits for the graph to become idle.
bool OlaGraph::waitUntilIdle() { bool OlaGraph::waitUntilIdle() {
return false; absl::Status status = _graph->WaitUntilIdle();
return status.ok();
} }

View File

@ -58,7 +58,7 @@ public:
_delegate = delegate; _delegate = delegate;
} }
mediapipe::ProfilingContext *getProfiler(); mediapipe::ProfilingContext* getProfiler();
int maxFramesInFlight; int maxFramesInFlight;
@ -161,13 +161,15 @@ public:
bool waitUntilIdle(); bool waitUntilIdle();
private: private:
std::unique_ptr<mediapipe::CalculatorGraph> _graph; std::unique_ptr<mediapipe::CalculatorGraph> _graph;
mediapipe::CalculatorGraphConfig _config; mediapipe::CalculatorGraphConfig _config;
/// Input side packets that will be added to the graph when it is started. /// Input side packets that will be added to the graph when it is started.
std::map<std::string, mediapipe::Packet> _inputSidePackets; std::map<std::string, mediapipe::Packet> _inputSidePackets;
/// Packet headers that will be added to the graph when it is started. /// Packet headers that will be added to the graph when it is started.
std::map<s td::string, mediapipe::Packet> _streamHeaders; std::map<std::string, mediapipe::Packet> _streamHeaders;
/// Service packets to be added to the graph when it is started. /// Service packets to be added to the graph when it is started.
std::map<const mediapipe::GraphServiceBase*, mediapipe::Packet> _servicePackets; std::map<const mediapipe::GraphServiceBase*, mediapipe::Packet> _servicePackets;
@ -181,6 +183,10 @@ private:
bool _started; bool _started;
std::weak_ptr<MPPGraphDelegate> _delegate; std::weak_ptr<MPPGraphDelegate> _delegate;
absl::Status performStart();
int _maxFramesInFlight = 0;
}; };
} }

View File

@ -102,6 +102,24 @@ cc_library(
"OlaRender.h", "OlaRender.h",
"OlaRenderIMP.h", "OlaRenderIMP.h",
], ],
linkopts = select({
"//conditions:default": [
"-lGLESv2",
"-lEGL",
],
"//mediapipe:android": [
"-lGLESv2",
"-lEGL",
],
"//mediapipe:ios": [
"-framework OpenGLES",
"-framework CoreVideo",
],
"//mediapipe:macos": [
"-framework OpenGL",
"-framework CoreVideo",
],
}),
# Use -Dverbose=-1 to turn off zlib's trace logging. (#3280) # Use -Dverbose=-1 to turn off zlib's trace logging. (#3280)
# linkstatic = True, # linkstatic = True,
# linkopts = GL_BASE_LINK_OPTS_OSS, # linkopts = GL_BASE_LINK_OPTS_OSS,