scrcpy icon indicating copy to clipboard operation
scrcpy copied to clipboard

Start scrcpy 2.0 by NodeJS but cannot receive "Recording Started" in stdout pipe

Open Jellyswat opened this issue 2 years ago • 9 comments

  • [x] I have read the FAQ.
  • [x] I have searched in existing issues.

Environment

  • OS: macOS
  • scrcpy version: 2.0
  • installation method: brew
  • device model: All device
  • Android version: All version

Describe the bug Hi, I just start scrcpy 2.0 by NodeJs but cannot receive "Recording Started" in stdout pipe.

I tried scrcpy 1.24 Its ok. So i think this is a new bug from 1.45 to 2.0

More Information: I guess its something wrong between buffer area? Maybe print but not '\n' or cout but not "<< endl;" ? Just a guess

Jellyswat avatar Apr 20 '23 09:04 Jellyswat

I just start scrcpy 2.0 by NodeJs

What does it mean?

What is the full console output?

See commit a039124d5d21c3886aacb4463a747edd69da1a46.

rom1v avatar Apr 20 '23 10:04 rom1v

I use NodeJs to start scrcpy code like this

// start scrcpy by JS subprocess module
let subProcess = childProcess.spawn(command, { shell: true });

// listen scrcpy's stdout
subProcess.stdout.on('data', async res => { console.log(res); });

Command scrcpy --serial ${deviceId} --port 12345 --crop 100:100:0:0 --no-display --record ${videoPath} --no-audio

Console output

// this is the console i got at the first time
/opt/homebrew/Cellar/scrcpy/2.0_1/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 29.2 MB/s (52867 bytes in 0.002s)
[server] INFO: Device: Xiaomi M2011K2C (Android 12)

// When I kill the child process, I got the following console at the same time
 scrcpy 2.0 <https://github.com/Genymobile/scrcpy>
INFO: Recording started to mp4 file: /Users/xxx/cache/15883504-android-scrcpy-video-1681988923815.mp4
INFO: Recording complete to mp4 file: /Users/xxx/cache/15883504-android-scrcpy-video-1681988923815.mp4

Jellyswat avatar Apr 20 '23 11:04 Jellyswat

So you get the adb and server logs immediately, but the client logs when you exit. There is additional buffering somewhere…

rom1v avatar Apr 20 '23 11:04 rom1v

Yes. I want to got the console output like INFO: Recording started immediately~

When I use 1.24, it's normal, but 2.0 doesn't work anymore. Is there any suggestion?

Jellyswat avatar Apr 20 '23 11:04 Jellyswat

Maybe related: in 2.0, info logs are printed to stdout rather than stderr: 4a25f3e53bdac8c2510cc1c7f06e479a17a42e6b

rom1v avatar Apr 20 '23 11:04 rom1v

I listen to stderr like subProcess.stderr.on('data', async res => { console.log(res); });

But i got the same situation. Not fixed..

Jellyswat avatar Apr 20 '23 11:04 Jellyswat

@rom1v Hi ~ Do we have any suggestion?

Jellyswat avatar Apr 21 '23 06:04 Jellyswat

You have to disable stdout buffering in some way in your program (I don't know NodeJS).

Also see: https://unix.stackexchange.com/a/25378 And https://github.com/nodejs/node/issues/6379

rom1v avatar Apr 21 '23 06:04 rom1v

Well, Lets see is there any way to disable stdout buffering in NodeJS.

Thanks for your helping ~~

Jellyswat avatar Apr 21 '23 06:04 Jellyswat

@rom1v For a long time search in StackOverflow and 'Node' repo issue,

I found that stdout has a buffer area while stderr does not. Means, Stderr will print msg immediately.(That's why scrcpy 1.24 works.) But Stdout will wait for buffer fflush.

In my case, I use unbuffer scrcpy --serial xxx --port 12345 for a temporary plan. But its very difficult to use in Windows and MacOSX system. So i strongly suggest to add fflush(stdout) when we print something in Stdout.

Reference material:

  • https://stackoverflow.com/questions/54707695/nodejs-child-process-stdout-is-not-live-but-works-fine-when-the-command-is-execu
  • https://stackoverflow.com/questions/32598607/not-receiving-stdout-from-nodejs-spawned-process?rq=4
  • https://github.com/nodejs/node/issues/2926

Jellyswat avatar Feb 20 '24 08:02 Jellyswat