cmvr-es/cmvr-es/main.cpp

100 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <csignal>
#include <pthread.h>
#include "common/base/logging/logger.h"
#include "runtime/include/cmvr_runtime.h"
namespace {
bool blockShutdownSignals(sigset_t& shutdown_signals)
{
sigemptyset(&shutdown_signals);
sigaddset(&shutdown_signals, SIGINT);
sigaddset(&shutdown_signals, SIGTERM);
return pthread_sigmask(SIG_BLOCK, &shutdown_signals, nullptr) == 0;
}
} // namespace
namespace fs = std::filesystem;
static bool setupIntelMediaEnvironment()
{
char exe_path_buffer[PATH_MAX] = {};
const ssize_t length = readlink(
"/proc/self/exe",
exe_path_buffer,
sizeof(exe_path_buffer) - 1);
if (length <= 0) {
std::cerr << "Failed to resolve /proc/self/exe\n";
return false;
}
exe_path_buffer[length] = '\0';
const fs::path executable_path(exe_path_buffer);
const fs::path bin_directory = executable_path.parent_path();
const fs::path install_directory = bin_directory.parent_path();
const fs::path library_directory = install_directory / "lib";
const fs::path va_driver_directory = library_directory / "dri";
const fs::path ihd_driver = va_driver_directory / "iHD_drv_video.so";
if (!fs::exists(ihd_driver)) {
std::cerr << "Intel media driver not found: "
<< ihd_driver << '\n';
return false;
}
// overwrite=1:强制 cmvr-es 使用随项目发布的媒体运行时。
setenv("LIBVA_DRIVER_NAME", "iHD", 1);
setenv(
"LIBVA_DRIVERS_PATH",
va_driver_directory.c_str(),
1);
setenv(
"ONEVPL_SEARCH_PATH",
library_directory.c_str(),
1);
return true;
}
int main()
{
if (!setupIntelMediaEnvironment()) {
return EXIT_FAILURE;
}
sigset_t shutdown_signals;
if (!blockShutdownSignals(shutdown_signals)) {
return 1;
}
cmvr::Runtime runtime;
if (!runtime.init()) {
return 1;
}
if (!runtime.startTasks()) {
return 1;
}
if (auto viewer = runtime.mujocoViewer("mujoco_viewer")) {
CMVR_LOG(INFO) << "[Startup] Run MuJoCo viewer on main thread";
viewer->runOnMainThread();
runtime.shutdown();
return 0;
}
int received_signal = 0;
if (sigwait(&shutdown_signals, &received_signal) != 0) {
runtime.shutdown();
return 1;
}
runtime.shutdown();
return 0;
}