cmvr-es/cmvr-es/main.cpp

100 lines
2.4 KiB
C++
Raw Normal View History

2026-06-24 15:45:54 +08:00
#include <csignal>
#include <pthread.h>
2026-06-24 15:45:54 +08:00
#include "common/base/logging/logger.h"
#include "runtime/include/cmvr_runtime.h"
2026-06-24 15:45:54 +08:00
2026-06-24 15:45:54 +08:00
namespace {
bool blockShutdownSignals(sigset_t& shutdown_signals)
2026-06-24 15:45:54 +08:00
{
sigemptyset(&shutdown_signals);
sigaddset(&shutdown_signals, SIGINT);
sigaddset(&shutdown_signals, SIGTERM);
return pthread_sigmask(SIG_BLOCK, &shutdown_signals, nullptr) == 0;
2026-06-24 15:45:54 +08:00
}
} // 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()
2026-06-24 15:45:54 +08:00
{
if (!setupIntelMediaEnvironment()) {
return EXIT_FAILURE;
}
2026-06-24 15:45:54 +08:00
sigset_t shutdown_signals;
if (!blockShutdownSignals(shutdown_signals)) {
2026-06-24 15:45:54 +08:00
return 1;
}
cmvr::Runtime runtime;
if (!runtime.init()) {
2026-06-24 15:45:54 +08:00
return 1;
}
if (!runtime.startTasks()) {
2026-06-24 15:45:54 +08:00
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;
2026-06-24 15:45:54 +08:00
}
int received_signal = 0;
if (sigwait(&shutdown_signals, &received_signal) != 0) {
runtime.shutdown();
2026-06-24 15:45:54 +08:00
return 1;
}
runtime.shutdown();
return 0;
}