Files
appRobotVideoControls/programs/input.js

49 lines
1.2 KiB
JavaScript
Executable File

// programs/input.js
'use strict';
const fs = require('fs');
const path = require('path');
function byIdCaptureCandidates() {
const dir = '/dev/v4l/by-id';
try {
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir)
.filter(n => n.endsWith('-index0'))
.map(n => fs.realpathSync(path.join(dir, n)));
} catch {
return [];
}
}
function naiveVideoNodes() {
try {
return fs.readdirSync('/dev')
.filter(n => /^video\d+$/.test(n))
.map(n => path.join('/dev', n))
.sort((a, b) => Number(a.replace(/\D/g, '')) - Number(b.replace(/\D/g, '')));
} catch {
return ['/dev/video0', '/dev/video2'];
}
}
function pickDevices(env = process.env) {
const DEV0 = env.DEV0 || null;
const DEV1 = env.DEV1 || null;
if (DEV0 && DEV1) return [DEV0, DEV1];
const byId = byIdCaptureCandidates();
if (DEV0 || DEV1) {
const pool = byId.length ? byId : naiveVideoNodes();
const d0 = DEV0 || pool[0];
const d1 = DEV1 || pool.find(d => d !== d0) || pool[1];
return [d0, d1];
}
if (byId.length >= 2) return [byId[0], byId[1]];
const naive = naiveVideoNodes();
return [naive[0], naive[1]];
}
module.exports = { pickDevices };