scrcpy icon indicating copy to clipboard operation
scrcpy copied to clipboard

Auto reconnect mode

Open Bluscream opened this issue 6 years ago • 16 comments

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

Bluscream avatar Aug 14 '19 01:08 Bluscream

Ref: #685

rom1v avatar Aug 14 '19 20:08 rom1v

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

georgiptr avatar Apr 23 '20 13:04 georgiptr

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.

rom1v avatar Apr 23 '20 14:04 rom1v

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?

georgiptr avatar Apr 23 '20 17:04 georgiptr

OK, I'm ok with that. :+1:

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 :wink:).

rom1v avatar Apr 23 '20 20:04 rom1v

https://github.com/QuestMods/QuestHome/blob/master/UI/Main.cs#L69

i hate it lol

Bluscream avatar Apr 23 '20 20:04 Bluscream

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. :)

georgiptr avatar Apr 24 '20 14:04 georgiptr

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

daniel11609 avatar Feb 22 '21 10:02 daniel11609

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

rom1v avatar Feb 22 '21 17:02 rom1v

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?

georgiptr avatar Mar 14 '21 16:03 georgiptr

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

rom1v avatar Mar 14 '21 17:03 rom1v

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

georgiptr avatar Mar 23 '21 14:03 georgiptr

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..

vinayadatta avatar Feb 18 '22 04:02 vinayadatta

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

georgiptr avatar Oct 10 '22 11:10 georgiptr

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

shodanx2 avatar Jun 01 '24 21:06 shodanx2

I would also really like to see this; my use case is that I have multiple phones from which I only plug in one at a time, but always need to rerun scrcpy once attached. For now I am using autoadb and it seems to work, but an integrated solution (or autoadb being a bit more maintained) would be great! :)

carstenhag avatar May 14 '25 11:05 carstenhag

yeah id love autoreconnect, even if it only works for tcpip devices

AJolly avatar Oct 22 '25 19:10 AJolly