Skip to content

启动 scrcpy 客户端时出错: AdbScrcpyExitedError: scrcpy server exited prematurely #1

Description

@luqihao

我用react照抄的时候测试只有一台手机可以正常投屏,其他手机都会报错启动 scrcpy 客户端时出错: AdbScrcpyExitedError: scrcpy server exited prematurely,想问问知道这是什么情况吗?

 const connect = async () => {
        const Manager = AdbDaemonWebUsbDeviceManager.BROWSER
        console.log('Manager', Manager)
        const device = await Manager?.requestDevice()
        console.log('device', device)
        if (device) {
            try {
                const connection = await device.connect()
                console.log('connection', connection)
                const CredentialStore = new AdbWebCredentialStore()
                const transport = await AdbDaemonTransport.authenticate({
                    serial: device.serial,
                    connection,
                    credentialStore: CredentialStore
                })
                console.log('transport', transport)
                const adb = new Adb(transport)
                console.log('adb', adb)
                adbInstance.current = adb
            } catch (e) {
                console.log(e)
                console.error('连接失败,当前设备已被其他应用程序占用!请关闭其他程序再尝试连接')
            }
        } else {
            console.error('请选择一台设备进行绑定连接')
            return
        }
    }

    const startScrcpy = () => {
        if (adbInstance.current) {
            scrcpyStart(renderRef, adbInstance.current)
        }
    }
    /**
     * 初始化 scrcpy 测试,推送服务端并启动 scrcpy 客户端
     */
    const scrcpyStart = async (renderRef: any, adb: Adb) => {
        try {
            console.log('Scrcpy 版本:', VERSION)
            renderContainer.current = renderRef.current

            if (!renderRef.current) {
                throw new Error('渲染容器未找到')
            }

            adbInstance.current = adb
            if (!adbInstance.current) {
                throw new Error('ADB 实例未初始化')
            }

            console.log('正在推送服务器...')
            await pushServerAndStartScrcpyClient(adbInstance.current, '/server.bin')
            console.log('服务端已推送')

            await startScrcpyClient(adb)
        } catch (error) {
            console.error('初始化 scrcpy 时出错:', error)
            if (error instanceof Error) {
                console.error('错误详情:', error.message)
                console.error('错误堆栈:', error.stack)
            }
            throw error
        }
    }

    /**
     * 启动 scrcpy 客户端并初始化视频流
     * @param  adb ADB 实例
     */
    const startScrcpyClient = async (adb: Adb) => {
        try {
            options = new AdbScrcpyOptionsLatest(
                new ScrcpyOptionsLatest({ videoCodec: 'h264' })
                // new ScrcpyOptionsLatest({
                //     videoBitRate: 1000000, // 降低比特率到 1Mbps
                //     videoCodec: 'h264', // 明确使用 H264
                //     lockVideoOrientation: ScrcpyVideoOrientation.Unlocked,
                //     displayId: 0,
                //     maxFps: 30, // 限制帧率
                //     maxSize: 1024, // 限制最大尺寸
                //     sendDeviceMeta: true,
                //     sendDummyByte: true,
                //     videoCodecOptions,
                //     tunnelForward: true,
                //     logLevel: ScrcpyLogLevel.Debug
                // })
            )

// 就是这一步引起的报错
            client.current = await AdbScrcpyClient.start(adb, DEFAULT_SERVER_PATH, VERSION, options)

            client.current.stdout
                .pipeTo(
                    new WritableStream({
                        write: line => {
                            console.log('stdout:', line)
                        }
                    }) as any
                )
                .then((r: any) => console.log('pipeTo', r))

            videoStream = await client.current.videoStream

            if (videoStream) {
                initializeVideoStream(videoStream)
                console.log('视频流已启动')
            } else {
                throw new Error('视频流初始化失败')
            }
        } catch (error) {
            console.error('启动 scrcpy 客户端时出错:', error)
            // 添加更详细的错误信息
            if (error instanceof Error) {
                console.error('错误详情:', error.message)
                console.error('错误堆栈:', error.stack)
            }
            // 尝试清理资源
            await destroyClient()
            throw error
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions