2023-11-02 08:08:19 +01:00
|
|
|
import util from 'node:util'
|
|
|
|
import { exec as _exec, spawn } from 'node:child_process'
|
2023-10-19 05:44:17 +02:00
|
|
|
import appStore from '@electron/helpers/store.js'
|
2023-10-17 09:27:08 +02:00
|
|
|
import { adbPath, scrcpyPath } from '@electron/configs/index.js'
|
2023-09-18 09:07:28 +02:00
|
|
|
|
2023-11-02 08:08:19 +01:00
|
|
|
const exec = util.promisify(_exec)
|
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
const shell = async (command, { stdout, stderr } = {}) => {
|
2023-10-26 12:11:52 +02:00
|
|
|
const spawnPath = appStore.get('common.scrcpyPath') || scrcpyPath
|
|
|
|
const ADB = appStore.get('common.adbPath') || adbPath
|
2023-10-17 11:54:29 +02:00
|
|
|
const args = command.split(' ')
|
2023-10-20 08:23:48 +02:00
|
|
|
|
2023-10-28 11:21:07 +02:00
|
|
|
console.log('scrcpy.shell.spawnPath', spawnPath)
|
|
|
|
console.log('scrcpy.shell.ADB', ADB)
|
2023-11-02 08:08:19 +01:00
|
|
|
console.log('scrcpy.shell.args', args)
|
2023-10-28 11:21:07 +02:00
|
|
|
|
2023-10-25 12:17:19 +02:00
|
|
|
const scrcpyProcess = spawn(`"${spawnPath}"`, args, {
|
2023-10-28 11:21:07 +02:00
|
|
|
env: { ...process.env, ADB },
|
2023-10-20 08:23:48 +02:00
|
|
|
shell: true,
|
2023-10-25 12:17:19 +02:00
|
|
|
encoding: 'utf8',
|
2023-10-20 08:23:48 +02:00
|
|
|
})
|
2023-09-18 09:07:28 +02:00
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
scrcpyProcess.stdout.on('data', (data) => {
|
|
|
|
const stringData = data.toString()
|
|
|
|
|
|
|
|
console.log('scrcpyProcess.stdout.data:', stringData)
|
|
|
|
|
|
|
|
if (stdout) {
|
2023-10-20 12:17:58 +02:00
|
|
|
stdout(stringData, scrcpyProcess)
|
2023-10-17 11:54:29 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scrcpyProcess.stderr.on('data', (data) => {
|
|
|
|
const stringData = data.toString()
|
|
|
|
|
|
|
|
console.error('scrcpyProcess.stderr.data:', stringData)
|
|
|
|
|
|
|
|
if (stderr) {
|
2023-10-20 12:17:58 +02:00
|
|
|
stderr(stringData, scrcpyProcess)
|
2023-10-17 11:54:29 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
scrcpyProcess.on('close', (code) => {
|
|
|
|
if (code === 0) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reject(new Error(`Command failed with code ${code}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scrcpyProcess.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-09-18 09:07:28 +02:00
|
|
|
|
2023-11-02 08:08:19 +01:00
|
|
|
const execShell = async (command) => {
|
|
|
|
const spawnPath = appStore.get('common.scrcpyPath') || scrcpyPath
|
|
|
|
const ADB = appStore.get('common.adbPath') || adbPath
|
|
|
|
|
|
|
|
console.log('scrcpy.execShell.spawnPath', spawnPath)
|
|
|
|
console.log('scrcpy.execShell.ADB', ADB)
|
|
|
|
console.log('scrcpy.shell.command', command)
|
|
|
|
|
|
|
|
const res = exec(`"${spawnPath}" ${command}`, {
|
|
|
|
env: { ...process.env, ADB },
|
|
|
|
shell: true,
|
|
|
|
encoding: 'utf8',
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
const getEncoders = async (serial) => {
|
|
|
|
const res = await execShell(`--serial="${serial}" --list-encoders`)
|
|
|
|
// console.log('getEncoders.res', res)
|
|
|
|
const stdout = res.stdout
|
|
|
|
|
|
|
|
// 提取视频编码器列表
|
|
|
|
const videoEncoderRegex
|
|
|
|
= /--video-codec=([\w-]+)\s+--video-encoder='([^']+)'/g
|
|
|
|
const videoEncoders = [...stdout.matchAll(videoEncoderRegex)].map(
|
|
|
|
([, codec, encoder]) => ({ decoder: codec, encoder }),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 提取音频编码器列表
|
|
|
|
const audioEncoderRegex
|
|
|
|
= /--audio-codec=([\w-]+)\s+--audio-encoder='([^']+)'/g
|
|
|
|
const audioEncoders = [...stdout.matchAll(audioEncoderRegex)].map(
|
|
|
|
([, codec, encoder]) => ({ decoder: codec, encoder }),
|
|
|
|
)
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
audio: audioEncoders,
|
|
|
|
video: videoEncoders,
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('getEncoders.value', value)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-09-18 09:07:28 +02:00
|
|
|
export default () => ({
|
|
|
|
shell,
|
2023-11-02 08:08:19 +01:00
|
|
|
execShell,
|
|
|
|
getEncoders,
|
2023-09-18 09:07:28 +02:00
|
|
|
})
|