feat: 初步增加 Windows 支持

This commit is contained in:
mofeng-git
2026-05-18 22:43:28 +08:00
parent 0b9d94f53f
commit 935fa823f2
163 changed files with 11419 additions and 7581 deletions

View File

@@ -0,0 +1,51 @@
export interface VideoDeviceLabelSource {
name?: string | null
path: string
}
const WINDOWS_USB_ID_RE = /vid[_-]([0-9a-f]{4}).*pid[_-]([0-9a-f]{4})/i
const WINDOWS_SYMBOLIC_LINK_RE = /^\\\\\?\\/
const WINDOWS_DIRECTSHOW_RE = /^dshow:/i
function shortDevicePath(path: string): string {
const normalized = path.replace(/\\/g, '/')
const parts = normalized.split('/').filter(Boolean)
return parts[parts.length - 1] || path
}
function isWindowsSymbolicLink(path: string): boolean {
return WINDOWS_SYMBOLIC_LINK_RE.test(path)
}
function isWindowsDirectShowPath(path: string): boolean {
return WINDOWS_DIRECTSHOW_RE.test(path)
}
function cleanWindowsDirectShowLabel(value: string): string {
return value.replace(WINDOWS_DIRECTSHOW_RE, '').trim()
}
function fallbackDeviceName(path: string): string {
if (isWindowsDirectShowPath(path)) return cleanWindowsDirectShowLabel(path) || 'Windows Capture Device'
if (isWindowsSymbolicLink(path)) return 'Windows Capture Device'
return shortDevicePath(path)
}
export function formatVideoDeviceLabel(device: VideoDeviceLabelSource): string {
const path = device.path.trim()
const rawName = device.name?.trim() || fallbackDeviceName(path)
const name = isWindowsDirectShowPath(rawName) ? cleanWindowsDirectShowLabel(rawName) : rawName
const usbId = path.match(WINDOWS_USB_ID_RE)
if (usbId?.[1] && usbId[2]) {
return `${name} (${usbId[1].toLowerCase()}:${usbId[2].toLowerCase()})`
}
if (!path) return name
if (isWindowsDirectShowPath(path)) return cleanWindowsDirectShowLabel(name) || fallbackDeviceName(path)
if (isWindowsSymbolicLink(path)) return name
return `${name} (${shortDevicePath(path)})`
}