2023-04-10 21:01:29 +00:00
|
|
|
<?php
|
|
|
|
|
2023-04-14 23:03:22 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-04-14 23:02:13 +00:00
|
|
|
namespace OCA\Memories\Service;
|
|
|
|
|
|
|
|
use OCA\Memories\Util;
|
2023-04-10 21:01:29 +00:00
|
|
|
|
|
|
|
class BinExt
|
|
|
|
{
|
2023-05-05 10:49:39 +00:00
|
|
|
public const EXIFTOOL_VER = '12.60';
|
2023-08-31 03:09:58 +00:00
|
|
|
public const GOVOD_VER = '0.1.13';
|
2023-05-18 06:16:11 +00:00
|
|
|
public const NX_VER_MIN = '1.0';
|
2023-04-10 21:01:29 +00:00
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
/** Copy a binary to temp dir for execution */
|
|
|
|
public static function getTempBin(string $path, string $name, bool $copy = true): string
|
|
|
|
{
|
2023-04-21 08:13:15 +00:00
|
|
|
// Bust cache if the path changes
|
|
|
|
$suffix = hash('crc32', $path);
|
2023-04-11 02:45:42 +00:00
|
|
|
|
|
|
|
// Check target temp file
|
|
|
|
$target = sys_get_temp_dir().'/'.$name.'-'.$suffix;
|
2023-04-11 00:51:16 +00:00
|
|
|
if (file_exists($target)) {
|
|
|
|
if (!is_writable($target)) {
|
|
|
|
throw new \Exception("{$name} temp binary path is not writable: {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_executable($target) && !chmod($target, 0755)) {
|
|
|
|
throw new \Exception("failed to make {$name} temp binary executable: {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($copy) {
|
2023-04-24 19:42:54 +00:00
|
|
|
if (empty($path)) {
|
2023-04-24 19:48:18 +00:00
|
|
|
throw new \Exception('binary path is empty (run occ maintenance:repair or use system perl)');
|
2023-04-24 19:42:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
if (!copy($path, $target)) {
|
|
|
|
throw new \Exception("failed to copy {$name} binary from {$path} to {$target}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getTempBin($path, $name, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception("failed to find exiftool temp binary {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:52:12 +00:00
|
|
|
/** Get the name for a binary */
|
|
|
|
public static function getName(string $name, string $version = ''): string
|
|
|
|
{
|
|
|
|
$id = Util::getInstanceId();
|
|
|
|
|
|
|
|
return empty($version) ? "{$name}-{$id}" : "{$name}-{$id}-{$version}";
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:01:29 +00:00
|
|
|
/** Test configured exiftool binary */
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testExiftool(): string
|
2023-04-10 21:01:29 +00:00
|
|
|
{
|
|
|
|
$cmd = implode(' ', array_merge(self::getExiftool(), ['-ver']));
|
|
|
|
$out = shell_exec($cmd);
|
|
|
|
if (!$out) {
|
2023-04-11 02:38:07 +00:00
|
|
|
throw new \Exception("failed to run exiftool: {$cmd}");
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Check version
|
2023-04-10 21:01:29 +00:00
|
|
|
$version = trim($out);
|
|
|
|
$target = self::EXIFTOOL_VER;
|
|
|
|
if (!version_compare($version, $target, '=')) {
|
|
|
|
throw new \Exception("version does not match {$version} <==> {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-14 02:43:13 +00:00
|
|
|
// Test with actual file
|
2023-04-14 23:02:13 +00:00
|
|
|
$file = realpath(__DIR__.'/../../exiftest.jpg');
|
2023-04-14 02:43:13 +00:00
|
|
|
if (!$file) {
|
|
|
|
throw new \Exception('Could not find EXIF test file');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$exif = \OCA\Memories\Exif::getExifFromLocalPath($file);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception("Couldn't read Exif data from test file: ".$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$exif) {
|
|
|
|
throw new \Exception('Got no Exif data from test file');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($exp = '2004:08:31 19:52:58') !== ($got = $exif['DateTimeOriginal'])) {
|
|
|
|
throw new \Exception("Got wrong Exif data from test file {$exp} <==> {$got}");
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:02:34 +00:00
|
|
|
return $version;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
/** Get path to exiftool binary */
|
|
|
|
public static function getExiftoolPBin(): string
|
|
|
|
{
|
|
|
|
$path = Util::getSystemConfig('memories.exiftool');
|
|
|
|
|
2023-04-11 18:52:12 +00:00
|
|
|
return self::getTempBin($path, self::getName('exiftool', self::EXIFTOOL_VER));
|
2023-04-11 00:51:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 21:01:29 +00:00
|
|
|
/** Get path to exiftool binary for proc_open */
|
|
|
|
public static function getExiftool(): array
|
|
|
|
{
|
|
|
|
if (Util::getSystemConfig('memories.exiftool_no_local')) {
|
2023-04-14 23:02:13 +00:00
|
|
|
return ['perl', realpath(__DIR__.'/../../exiftool-bin/exiftool/exiftool')];
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
return [self::getExiftoolPBin()];
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Detect the exiftool binary to use */
|
2023-04-10 21:32:35 +00:00
|
|
|
public static function detectExiftool()
|
2023-04-10 21:01:29 +00:00
|
|
|
{
|
|
|
|
if (!empty($path = Util::getSystemConfig('memories.exiftool'))) {
|
2023-04-10 21:32:35 +00:00
|
|
|
return $path;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Util::getSystemConfig('memories.exiftool_no_local')) {
|
2023-04-10 21:32:35 +00:00
|
|
|
return implode(' ', self::getExiftool());
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect architecture
|
|
|
|
$arch = \OCA\Memories\Util::getArch();
|
|
|
|
$libc = \OCA\Memories\Util::getLibc();
|
|
|
|
|
|
|
|
// Get static binary if available
|
|
|
|
if ($arch && $libc) {
|
|
|
|
// get target file path
|
2023-04-14 23:02:13 +00:00
|
|
|
$path = realpath(__DIR__."/../../exiftool-bin/exiftool-{$arch}-{$libc}");
|
2023-04-10 21:01:29 +00:00
|
|
|
|
2023-04-11 00:51:16 +00:00
|
|
|
// make sure it exists
|
2023-04-25 05:54:53 +00:00
|
|
|
if ($path && file_exists($path)) {
|
|
|
|
Util::setSystemConfig('memories.exiftool', $path);
|
|
|
|
|
2023-04-10 21:32:35 +00:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Util::setSystemConfig('memories.exiftool_no_local', true);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
/**
|
|
|
|
* Get the upstream URL for a video.
|
|
|
|
*/
|
|
|
|
public static function getGoVodUrl(string $client, string $path, string $profile): string
|
|
|
|
{
|
|
|
|
$path = rawurlencode($path);
|
|
|
|
|
|
|
|
$bind = Util::getSystemConfig('memories.vod.bind');
|
|
|
|
$connect = Util::getSystemConfig('memories.vod.connect', $bind);
|
|
|
|
|
|
|
|
return "http://{$connect}/{$client}{$path}/{$profile}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getGoVodConfig($local = false)
|
|
|
|
{
|
|
|
|
// Get config from system values
|
|
|
|
$env = [
|
|
|
|
'vaapi' => Util::getSystemConfig('memories.vod.vaapi'),
|
|
|
|
'vaapiLowPower' => Util::getSystemConfig('memories.vod.vaapi.low_power'),
|
|
|
|
|
2023-09-03 16:26:54 +00:00
|
|
|
'nvenc' => Util::getSystemConfig('memories.vod.nvenc'),
|
2023-04-10 23:45:04 +00:00
|
|
|
'nvencTemporalAQ' => Util::getSystemConfig('memories.vod.nvenc.temporal_aq'),
|
|
|
|
'nvencScale' => Util::getSystemConfig('memories.vod.nvenc.scale'),
|
2023-08-03 20:13:17 +00:00
|
|
|
|
|
|
|
'useTranspose' => Util::getSystemConfig('memories.vod.use_transpose'),
|
2023-04-10 23:45:04 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (!$local) {
|
|
|
|
return $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get temp directory
|
|
|
|
$tmpPath = Util::getSystemConfig('memories.vod.tempdir', sys_get_temp_dir().'/go-vod/');
|
|
|
|
|
|
|
|
// Make sure path ends with slash
|
|
|
|
if ('/' !== substr($tmpPath, -1)) {
|
|
|
|
$tmpPath .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add instance ID to path
|
2023-04-11 18:52:12 +00:00
|
|
|
$tmpPath .= Util::getInstanceId();
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
return array_merge($env, [
|
|
|
|
'bind' => Util::getSystemConfig('memories.vod.bind'),
|
|
|
|
'ffmpeg' => Util::getSystemConfig('memories.vod.ffmpeg'),
|
|
|
|
'ffprobe' => Util::getSystemConfig('memories.vod.ffprobe'),
|
|
|
|
'tempdir' => $tmpPath,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-04-11 00:57:57 +00:00
|
|
|
/**
|
|
|
|
* Get temp binary for go-vod.
|
|
|
|
*/
|
|
|
|
public static function getGoVodBin()
|
|
|
|
{
|
2023-04-11 18:52:12 +00:00
|
|
|
$path = Util::getSystemConfig('memories.vod.path');
|
|
|
|
|
|
|
|
return self::getTempBin($path, self::getName('go-vod', self::GOVOD_VER));
|
2023-04-11 00:57:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
/**
|
|
|
|
* If local, restart the go-vod instance.
|
|
|
|
* If external, configure the go-vod instance.
|
|
|
|
*/
|
|
|
|
public static function startGoVod()
|
|
|
|
{
|
2023-04-11 18:52:12 +00:00
|
|
|
// Check if disabled
|
|
|
|
if (Util::getSystemConfig('memories.vod.disable')) {
|
|
|
|
// Make sure it's dead, in case the user just disabled it
|
|
|
|
Util::pkill(self::getName('go-vod'));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
// Check if external
|
|
|
|
if (Util::getSystemConfig('memories.vod.external')) {
|
|
|
|
self::configureGoVod();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get transcoder path
|
2023-04-11 00:57:57 +00:00
|
|
|
$transcoder = self::getGoVodBin();
|
2023-04-10 23:45:04 +00:00
|
|
|
if (empty($transcoder)) {
|
|
|
|
throw new \Exception('Transcoder not configured');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get local config
|
|
|
|
$env = self::getGoVodConfig(true);
|
|
|
|
$tmpPath = $env['tempdir'];
|
|
|
|
|
|
|
|
// (Re-)create temp dir
|
|
|
|
shell_exec("rm -rf '{$tmpPath}' && mkdir -p '{$tmpPath}' && chmod 755 '{$tmpPath}'");
|
|
|
|
|
|
|
|
// Check temp directory exists
|
|
|
|
if (!is_dir($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory could not be created ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check temp directory is writable
|
|
|
|
if (!is_writable($tmpPath)) {
|
|
|
|
throw new \Exception("Temp directory is not writable ({$tmpPath})");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write config to file
|
|
|
|
$logFile = $tmpPath.'.log';
|
|
|
|
$configFile = $tmpPath.'.json';
|
|
|
|
file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT));
|
|
|
|
|
|
|
|
// Kill the transcoder in case it's running
|
2023-04-11 18:52:12 +00:00
|
|
|
Util::pkill(self::getName('go-vod'));
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
// Start transcoder
|
|
|
|
shell_exec("nohup {$transcoder} {$configFile} >> '{$logFile}' 2>&1 & > /dev/null");
|
|
|
|
|
|
|
|
// wait for 500ms
|
|
|
|
usleep(500000);
|
|
|
|
|
|
|
|
return $logFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test go-vod and (re)-start if it is not external.
|
|
|
|
*/
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testStartGoVod(): string
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return self::testGoVod();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// silently try to restart
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to (re)start go-vod
|
|
|
|
// If it is external, this only attempts to reconfigure
|
|
|
|
self::startGoVod();
|
|
|
|
|
|
|
|
// Test again
|
|
|
|
return self::testGoVod();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Test the go-vod instance that is running */
|
2023-04-18 21:02:34 +00:00
|
|
|
public static function testGoVod(): string
|
2023-04-10 23:45:04 +00:00
|
|
|
{
|
2023-04-11 18:52:12 +00:00
|
|
|
// Check if disabled
|
|
|
|
if (Util::getSystemConfig('memories.vod.disable')) {
|
|
|
|
throw new \Exception('Transcoding is disabled');
|
|
|
|
}
|
|
|
|
|
2023-04-10 23:45:04 +00:00
|
|
|
// TODO: check data mount; ignoring the result of the file for now
|
2023-04-14 23:02:13 +00:00
|
|
|
$testfile = realpath(__DIR__.'/../../exiftest.jpg');
|
2023-04-10 23:45:04 +00:00
|
|
|
|
|
|
|
// Make request
|
|
|
|
$url = self::getGoVodUrl('test', $testfile, 'test');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
2023-04-11 02:17:16 +00:00
|
|
|
$res = $client->request('GET', $url, [
|
|
|
|
'timeout' => 1,
|
|
|
|
'connect_timeout' => 1,
|
|
|
|
]);
|
2023-04-10 23:45:04 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception('failed to connect to go-vod: '.$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse body
|
2023-04-14 23:08:58 +00:00
|
|
|
$json = json_decode((string) $res->getBody(), true);
|
2023-04-10 23:45:04 +00:00
|
|
|
if (!$json) {
|
|
|
|
throw new \Exception('failed to parse go-vod response');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check version
|
|
|
|
$version = $json['version'];
|
|
|
|
$target = self::GOVOD_VER;
|
|
|
|
if (!version_compare($version, $target, '=')) {
|
|
|
|
throw new \Exception("version does not match {$version} <==> {$target}");
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:02:34 +00:00
|
|
|
return $version;
|
2023-04-10 23:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** POST a new configuration to go-vod */
|
|
|
|
public static function configureGoVod()
|
|
|
|
{
|
|
|
|
// Get config
|
|
|
|
$config = self::getGoVodConfig();
|
|
|
|
|
|
|
|
// Make request
|
|
|
|
$url = self::getGoVodUrl('config', '/config', 'config');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$client->request('POST', $url, [
|
|
|
|
'json' => $config,
|
2023-04-11 02:17:16 +00:00
|
|
|
'timeout' => 1,
|
|
|
|
'connect_timeout' => 1,
|
2023-04-10 23:45:04 +00:00
|
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new \Exception('failed to connect to go-vod: '.$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:32:35 +00:00
|
|
|
/** Detect the go-vod binary to use */
|
|
|
|
public static function detectGoVod()
|
|
|
|
{
|
|
|
|
$goVodPath = Util::getSystemConfig('memories.vod.path');
|
|
|
|
|
|
|
|
if (empty($goVodPath) || !file_exists($goVodPath)) {
|
|
|
|
// Detect architecture
|
|
|
|
$arch = \OCA\Memories\Util::getArch();
|
2023-04-14 23:02:13 +00:00
|
|
|
$path = __DIR__."/../../exiftool-bin/go-vod-{$arch}";
|
2023-04-10 21:32:35 +00:00
|
|
|
$goVodPath = realpath($path);
|
|
|
|
|
|
|
|
if (!$goVodPath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set config
|
|
|
|
Util::setSystemConfig('memories.vod.path', $goVodPath);
|
|
|
|
|
|
|
|
// Make executable
|
|
|
|
if (!is_executable($goVodPath)) {
|
|
|
|
@chmod($goVodPath, 0755);
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-10 21:32:35 +00:00
|
|
|
|
|
|
|
return $goVodPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function detectFFmpeg()
|
|
|
|
{
|
|
|
|
$ffmpegPath = Util::getSystemConfig('memories.vod.ffmpeg');
|
|
|
|
$ffprobePath = Util::getSystemConfig('memories.vod.ffprobe');
|
|
|
|
|
|
|
|
if (empty($ffmpegPath) || !file_exists($ffmpegPath) || empty($ffprobePath) || !file_exists($ffprobePath)) {
|
|
|
|
// Use PATH
|
|
|
|
$ffmpegPath = shell_exec('which ffmpeg');
|
|
|
|
$ffprobePath = shell_exec('which ffprobe');
|
|
|
|
if (!$ffmpegPath || !$ffprobePath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim
|
|
|
|
$ffmpegPath = trim($ffmpegPath);
|
|
|
|
$ffprobePath = trim($ffprobePath);
|
|
|
|
|
|
|
|
// Set config
|
|
|
|
Util::setSystemConfig('memories.vod.ffmpeg', $ffmpegPath);
|
|
|
|
Util::setSystemConfig('memories.vod.ffprobe', $ffprobePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if executable
|
|
|
|
if (!is_executable($ffmpegPath) || !is_executable($ffprobePath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ffmpegPath;
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|
2023-04-18 21:02:34 +00:00
|
|
|
|
2023-04-29 17:56:43 +00:00
|
|
|
public static function testFFmpeg(string $path, string $name)
|
2023-04-18 21:02:34 +00:00
|
|
|
{
|
|
|
|
$version = shell_exec("{$path} -version");
|
2023-04-29 17:56:43 +00:00
|
|
|
if (!preg_match("/{$name} version \\S*/", $version, $matches)) {
|
2023-04-18 21:02:34 +00:00
|
|
|
throw new \Exception("failed to detect version, found {$version}");
|
|
|
|
}
|
|
|
|
|
2023-04-29 17:56:43 +00:00
|
|
|
return explode(' ', $matches[0])[2];
|
2023-04-18 21:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function testSystemPerl(string $path): string
|
|
|
|
{
|
|
|
|
if (($out = shell_exec("{$path} -e 'print \"OK\";'")) !== 'OK') {
|
|
|
|
throw new \Exception('Failed to run test perl script: '.$out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shell_exec("{$path} -e 'print $^V;'");
|
|
|
|
}
|
2023-04-10 21:01:29 +00:00
|
|
|
}
|