|
| 1 | +/** |
| 2 | + * Camera list parsing and scrcpy camera argument building. |
| 3 | + * |
| 4 | + * The camera list is printed by the scrcpy server on the device and forwarded |
| 5 | + * to the PC on stderr, so callers must pass the combined process output here. |
| 6 | + * This module is intentionally dependency-free so it can be unit tested with |
| 7 | + * `bun test` without booting Electron. |
| 8 | + */ |
| 9 | + |
| 10 | +export type CameraFacing = 'front' | 'back' | 'external' | 'unknown' |
| 11 | + |
| 12 | +export type CameraHighSpeedMode = { |
| 13 | + size: string |
| 14 | + fps: number[] |
| 15 | +} |
| 16 | + |
| 17 | +export type CameraInfo = { |
| 18 | + id: string |
| 19 | + facing: CameraFacing |
| 20 | + activeSize: string |
| 21 | + fps: number[] |
| 22 | + zoomRange: {min: number; max: number} | null |
| 23 | + sizes: string[] |
| 24 | + highSpeed: CameraHighSpeedMode[] |
| 25 | +} |
| 26 | + |
| 27 | +export type CameraWindowOptions = { |
| 28 | + cameraId: string |
| 29 | + cameraSize: string |
| 30 | + cameraFps: string |
| 31 | + orientation: string |
| 32 | + zoom: string |
| 33 | + torch: boolean |
| 34 | + bitRate: string |
| 35 | + alwaysOnTop: boolean |
| 36 | + fullscreen: boolean |
| 37 | + borderless: boolean |
| 38 | +} |
| 39 | + |
| 40 | +const parseFacing = (value: string): CameraFacing => { |
| 41 | + if (value === 'front' || value === 'back' || value === 'external') { |
| 42 | + return value |
| 43 | + } |
| 44 | + return 'unknown' |
| 45 | +} |
| 46 | + |
| 47 | +const parseNumberSet = (value: string): number[] => { |
| 48 | + const result: number[] = [] |
| 49 | + for (const part of value.split(',')) { |
| 50 | + const num = Number(part.trim()) |
| 51 | + if (Number.isFinite(num) && num > 0 && !result.includes(num)) { |
| 52 | + result.push(num) |
| 53 | + } |
| 54 | + } |
| 55 | + return result.sort((a, b) => b - a) |
| 56 | +} |
| 57 | + |
| 58 | +const parseZoomRange = (value: string): {min: number; max: number} | null => { |
| 59 | + const matched = value.match(/^\s*([\d.]+)\s*,\s*([\d.]+)\s*$/) |
| 60 | + if (!matched) return null |
| 61 | + const min = Number(matched[1]) |
| 62 | + const max = Number(matched[2]) |
| 63 | + if (!Number.isFinite(min) || !Number.isFinite(max) || max <= 0) return null |
| 64 | + return {min, max} |
| 65 | +} |
| 66 | + |
| 67 | +const CAMERA_LINE = /^\s*--camera-id=(\S+)\s*\((.*)\)\s*$/ |
| 68 | +const SIZE_LINE = /^\s*-\s+(\d+x\d+)(?:\s+\(fps=\{([^}]*)\}\))?/ |
| 69 | +const HIGH_SPEED_HEADING = /^\s*High speed capture\b/ |
| 70 | + |
| 71 | +/** |
| 72 | + * Parse the output of `scrcpy --list-cameras` / `--list-camera-sizes`. |
| 73 | + * |
| 74 | + * Example: |
| 75 | + * List of cameras: |
| 76 | + * --camera-id=0 (back, 4000x3000, fps={15, 30, 60}, zoom-range=[1, 10]) |
| 77 | + * - 4000x3000 |
| 78 | + * - 1920x1080 |
| 79 | + * High speed capture (--camera-high-speed): |
| 80 | + * - 1920x1080 (fps={120, 240}) |
| 81 | + * |
| 82 | + * Unrelated lines (warnings, the trailing `(none)` / `(access denied)`) are |
| 83 | + * ignored; an empty array means "no usable camera list". |
| 84 | + */ |
| 85 | +export const parseCameraList = (output: string): CameraInfo[] => { |
| 86 | + const cameras: CameraInfo[] = [] |
| 87 | + let current: CameraInfo | null = null |
| 88 | + let inHighSpeed = false |
| 89 | + |
| 90 | + for (const line of String(output || '').split(/\r?\n/)) { |
| 91 | + const cameraMatched = line.match(CAMERA_LINE) |
| 92 | + if (cameraMatched) { |
| 93 | + const id = cameraMatched[1] |
| 94 | + const detail = cameraMatched[2] |
| 95 | + const facingMatched = detail.match(/^\s*([a-z]+)\s*,/i) |
| 96 | + const sizeMatched = detail.match(/(\d+x\d+)/) |
| 97 | + const fpsMatched = detail.match(/fps=\{([^}]*)\}/) |
| 98 | + const zoomMatched = detail.match(/zoom-range=\[([^\]]*)\]/) |
| 99 | + current = { |
| 100 | + id, |
| 101 | + facing: parseFacing(facingMatched ? facingMatched[1].toLowerCase() : ''), |
| 102 | + activeSize: sizeMatched ? sizeMatched[1] : '', |
| 103 | + fps: fpsMatched ? parseNumberSet(fpsMatched[1]) : [], |
| 104 | + zoomRange: zoomMatched ? parseZoomRange(zoomMatched[1]) : null, |
| 105 | + sizes: [], |
| 106 | + highSpeed: [], |
| 107 | + } |
| 108 | + cameras.push(current) |
| 109 | + inHighSpeed = false |
| 110 | + continue |
| 111 | + } |
| 112 | + if (!current) continue |
| 113 | + if (HIGH_SPEED_HEADING.test(line)) { |
| 114 | + inHighSpeed = true |
| 115 | + continue |
| 116 | + } |
| 117 | + const sizeMatched = line.match(SIZE_LINE) |
| 118 | + if (sizeMatched) { |
| 119 | + const size = sizeMatched[1] |
| 120 | + const fps = sizeMatched[2] ? parseNumberSet(sizeMatched[2]) : [] |
| 121 | + if (inHighSpeed) { |
| 122 | + current.highSpeed.push({size, fps}) |
| 123 | + } else if (!current.sizes.includes(size)) { |
| 124 | + current.sizes.push(size) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return cameras |
| 130 | +} |
| 131 | + |
| 132 | +const normalizeSize = (value: string): string => { |
| 133 | + const matched = String(value || '').match(/^(\d+)x(\d+)$/) |
| 134 | + if (!matched) return '' |
| 135 | + const width = Number(matched[1]) |
| 136 | + const height = Number(matched[2]) |
| 137 | + if (!width || !height) return '' |
| 138 | + return `${width}x${height}` |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Build the scrcpy arguments for a camera mirror window. |
| 143 | + * |
| 144 | + * Camera mode has no touch control, so only video/window options are emitted. |
| 145 | + * `--orientation` accepts 0/90/180/270 and flip0/flip90/flip180/flip270; the |
| 146 | + * default "0" is omitted because it is scrcpy's default. |
| 147 | + */ |
| 148 | +export const buildCameraArgs = (option: CameraWindowOptions): string[] => { |
| 149 | + const args: string[] = ['--video-source', 'camera'] |
| 150 | + const cameraId = String(option.cameraId || '').trim() |
| 151 | + if (cameraId) { |
| 152 | + args.push('--camera-id', cameraId) |
| 153 | + } |
| 154 | + const cameraSize = normalizeSize(option.cameraSize) |
| 155 | + if (cameraSize) { |
| 156 | + args.push('--camera-size', cameraSize) |
| 157 | + } |
| 158 | + const cameraFps = String(option.cameraFps || '').trim() |
| 159 | + if (/^\d+$/.test(cameraFps) && Number(cameraFps) > 0) { |
| 160 | + args.push('--camera-fps', cameraFps) |
| 161 | + } |
| 162 | + const orientation = String(option.orientation || '').trim() |
| 163 | + if (orientation && orientation !== '0') { |
| 164 | + args.push('--orientation', orientation) |
| 165 | + } |
| 166 | + const zoom = String(option.zoom || '').trim() |
| 167 | + if (zoom && Number.isFinite(Number(zoom)) && Number(zoom) !== 1) { |
| 168 | + args.push('--camera-zoom', zoom) |
| 169 | + } |
| 170 | + if (option.torch) { |
| 171 | + args.push('--camera-torch') |
| 172 | + } |
| 173 | + const bitRate = String(option.bitRate || '').trim() |
| 174 | + if (bitRate) { |
| 175 | + args.push('--video-bit-rate', bitRate) |
| 176 | + } |
| 177 | + if (option.alwaysOnTop) { |
| 178 | + args.push('--always-on-top') |
| 179 | + } |
| 180 | + if (option.fullscreen) { |
| 181 | + args.push('--fullscreen') |
| 182 | + } |
| 183 | + if (option.borderless) { |
| 184 | + args.push('--window-borderless') |
| 185 | + } |
| 186 | + return args |
| 187 | +} |
| 188 | + |
| 189 | +export const cameraOrientationOptions = (): {value: string; labelKey: string}[] => [ |
| 190 | + {value: '0', labelKey: 'device.cameraOrientationNormal'}, |
| 191 | + {value: '90', labelKey: 'device.cameraOrientationLeft'}, |
| 192 | + {value: '270', labelKey: 'device.cameraOrientationRight'}, |
| 193 | + {value: '180', labelKey: 'device.cameraOrientationUpsideDown'}, |
| 194 | + {value: 'flip0', labelKey: 'device.cameraOrientationFlip'}, |
| 195 | + {value: 'flip180', labelKey: 'device.cameraOrientationFlipVertical'}, |
| 196 | + {value: 'flip90', labelKey: 'device.cameraOrientationFlipLeft'}, |
| 197 | + {value: 'flip270', labelKey: 'device.cameraOrientationFlipRight'}, |
| 198 | +] |
0 commit comments