Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto reconnect mode #721

Open
Bluscream opened this issue Aug 14, 2019 · 16 comments
Open

Auto reconnect mode #721

Bluscream opened this issue Aug 14, 2019 · 16 comments

Comments

@Bluscream
Copy link

It would be nice if the app would wait for the same device to reconnect to it automatically (in case of a restart etc)

@rom1v
Copy link
Collaborator

rom1v commented Aug 14, 2019

Ref: #685

@georgiptr
Copy link

@rom1v If I make a patch to enable auto-reconnect, would you accept this functionality?

@rom1v
Copy link
Collaborator

rom1v commented Apr 23, 2020

In theory, I'm not against. In practice, there are a lot of questions to be discussed.

For example (non exhaustive):

  • How do you detect that this is the same device? (for now scrcpy doesn't retrieve the serial if it's not given via -s)
  • Do you want to detect that this is the same device?
  • If you plug several devices, should it open several new instances?
  • How is recording impacted?

Just for info, I developed a separate tool to start scrcpy (or anything else) whenever a new device is connected: https://github.com/rom1v/autoadb
Not as convenient, but it exists.

@georgiptr
Copy link

georgiptr commented Apr 23, 2020

Your other project would solve my problem, but I would like to contribute to the core program and make auto-reconnect self-contained inside it. This will make this functionality easily available with just a command-line switch.

I propose that the auto-reconnect is enabled only for a short duration in order to "buffer" for intermittent disconnects. Or explicit short ones initiated by the user when the connection has stalled. Like 10 seconds by default, with an option to extend it.

Something like:
scrcpy --auto-reconnect (defaults to waiting up to 10 seconds)
scrcpy --auto-reconnect 60 (waits for up to 60 seconds)

I'm having short disconnections from time to time on my machine for an unknown reason. Probably other people are having this problem as well. Using USB2 instead of USB3 is much better, but they still happen like 5-6 times a day. I'm using good cables and rear USB ports.

I lack context in the program use cases.

If a device is already connected and a new device is connected, nothing changes from current behavior (whatever it is). However, if the device was just disconnected, we open a "reconnection window" of up to X seconds. If no serial was given before, accept any device that makes it into this window. If a serial was given, accept only a device with this serial.

I am not aware of the recording functionality. I'm open to suggestions here.

So how does this sound?

@rom1v
Copy link
Collaborator

rom1v commented Apr 23, 2020

OK, I'm ok with that. 👍

There is a preliminary change to make: currently, scrcpy handles the connection in the main thread, then starts the UI event loop once connected. To be able to reconnect, we need to handle the connection in a separate thread, and send an event to the event loop when the device is connected.

I could do that change (it's a refactor that could be done independently of the re connection feature) after the v1.13 release (btw, help wanted: #1307 😉).

@Bluscream
Copy link
Author

@georgiptr
Copy link

Very nice. When I have some time these days, I'll look into the application's code to understand what needs to be done and I'll comment on what I'm planning to do here. :)

@daniel11609
Copy link

would really enjoy to have some auto restart function. any update on this?

@rom1v
Copy link
Collaborator

rom1v commented Feb 22, 2021

Not yet. I have it in mind, but I have more refactors to do beforehand.

@georgiptr
Copy link

Oh, I completely forgot this one. I switched my project and I didn't need this functionality, so I forgot. My apologies. @rom1v can I take a look and try to implement it or you need to finish the refactoring before that?

@rom1v
Copy link
Collaborator

rom1v commented Mar 14, 2021

I have to move server to a separate thread first :)

@georgiptr
Copy link

Sure. Please let me know when this is finished and I'll try my best to implement the feature from this issue. :)

@vinayadatta
Copy link

SOLUTION..!!

I found a way to reconnect scrcpy with a simple workaround until the dev implements the auto-reconnect feature..

This can be performed by using a loop in a batch file..

:start
scrcpy.exe -s <your-adb-device-id> --max-size 1024 --bit-rate 2M --max-fps 30 --encoder 'c2.android.avc.encoder'
goto start

With this batch file, the reconnect can automatically happen as long as you have an adb connection ready even if there are hiccups in the stability of the connection.. Although it may not resume/reconnect with the same thread, it will start a new instance as soon as the previous instance closes..

How does it work? => This loop is basically an infinite loop that runs the scrcpy command again each time the command execution is completed, which means the command execution is completed when scrcpy exits for any reason..

Note:

  1. As this is an infinite loop, you may have to close the windows quickly before it restarts..
  2. The batch file can be placed in the same folder where scrcpy.exe exists or the actual file path of the scrcpy.exe can be mentioned in the batch file..

@georgiptr
Copy link

georgiptr commented Oct 10, 2022

I would add a "timeout /t 1" just after :start to wait for a second when there's no connection.
Also if you save this for example as "automatedscrcpy.cmd", then create another file like scrcpy-noconsolve.vbs that calls the CMD file. This will auto-connect without an annoying console. Just keep in mind stopping the process if you don't need scrcpy anymore as it won't be visible.

strCommand = "cmd /c automatedscrcpy.cmd"

For Each Arg In WScript.Arguments
    strCommand = strCommand & " """ & replace(Arg, """", """""""""") & """"
Next

CreateObject("Wscript.Shell").Run strCommand, 0, false

@shodanx2
Copy link

shodanx2 commented Jun 1, 2024

Hello friends,

Here is my pure batch solution for this issue

start FOR /L %L IN (0,0,1) DO @( scrcpy -s cam1.lan --window-title CAMFRONT )

This will autorestart forever

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

No branches or pull requests

7 participants
@rom1v @Bluscream @shodanx2 @georgiptr @daniel11609 @vinayadatta and others