Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
110.42k stars 10.58k forks source link

High polling rate makes mouse(HID) movement sluggish #3088

Open nothingshiru opened 2 years ago

nothingshiru commented 2 years ago

Environment

Describe the bug The polling rate of my mouse can be set to 125hz, 500hz, 1000hz in driver. When it is set to 500hz or 1000hz, mouse(HID) movement is sluggish.

rom1v commented 2 years ago

Any relevant logs in the console output?

nothingshiru commented 2 years ago

Sorry. I'm not a programmer so that I might not understand you well. Where can I find the console output?

rom1v commented 2 years ago

Double-click on open_a_terminal_here.bat, type scrcpy -KM (K for HID keyboard, M for HID mouse) and press Enter. Some logs will be printed to this terminal while scrcpy is running.

nothingshiru commented 2 years ago

scrcpy -KM doesn't work. scrcpy --otg works.

And here is the log.

D:\Program Files\scrcpy-win64-v1.23>scrcpy -KM -s TFMNU20429107644
scrcpy 1.23 <https://github.com/Genymobile/scrcpy>
ERROR: On Windows, it is not possible to open a USB device already open by another process (like adb).
ERROR: Therefore, -K/--hid-keyboard and -M/--hid-mouse may only work in OTG mode (--otg).

D:\Program Files\scrcpy-win64-v1.23>scrcpy --otg -s TFMNU20429107644
scrcpy 1.23 <https://github.com/Genymobile/scrcpy>
INFO: Killing adb daemon (if any)...
INFO: USB device: TFMNU20429107644 (12d1:107e) HUAWEI BAH3-W09
WARN: On this platform, libusb does not have hotplug capability; device disconnection will not be detected properly
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
WARN: Could request HID event
nothingshiru commented 2 years ago

When the polling rate is set to 125hz, I don't see the WARN: Could request HID event

D:\Program Files\scrcpy-win64-v1.23>scrcpy --otg -s TFMNU20429107644
scrcpy 1.23 <https://github.com/Genymobile/scrcpy>
INFO: Killing adb daemon (if any)...
INFO: USB device: TFMNU20429107644 (12d1:107e) HUAWEI BAH3-W09
WARN: On this platform, libusb does not have hotplug capability; device disconnection will not be detected properly
rom1v commented 2 years ago

OK, that's what I suspected.

Scrcpy enqueues at most a fixed number of events (currently 64), because if there are too many events not consumed, we want to discard them (to avoid forwarding them with a high delay).

With a high polling rate, this limit might be too low, let's try to increase to 512:

diff --git a/app/src/usb/aoa_hid.h b/app/src/usb/aoa_hid.h
index d785a0e9..d1a95643 100644
--- a/app/src/usb/aoa_hid.h
+++ b/app/src/usb/aoa_hid.h
@@ -27,7 +27,7 @@ sc_hid_event_init(struct sc_hid_event *hid_event, uint16_t accessory_id,
 void
 sc_hid_event_destroy(struct sc_hid_event *hid_event);

-struct sc_hid_event_queue CBUF(struct sc_hid_event, 64);
+struct sc_hid_event_queue CBUF(struct sc_hid_event, 512);

 struct sc_aoa {
     struct sc_usb *usb;

Here is a binary to replace in your v1.23 release:

Please check if there are still warnings in the console (if you mouse the mouse for a while). If not, please check that there is no noticeable delay between your mouse motion and the cursor motion.

If there are still warnings, then the events could not be consumed/forwarded at the same rate as they are received. That would require event resampling/merging to accommodate the issue.

nothingshiru commented 2 years ago

I replaced the file. There are still warnings in the console and the delay is much worse than before.

rom1v commented 2 years ago

OK, thank you.

So an event resampling/merging mechanism will be necessary. It's not trivial and requires some work.

Unfortunately, I could not reproduce on my machine with my mouse (AFAIK I could not configure the polling rate).

nothingshiru commented 2 years ago

This is quite important as the polling rate of a gaming mouse is normally set to at least 500hz by default. I'm now ok with 125hz though. I will wait patiently.

rom1v commented 2 years ago

Does it work with another Android device?

nothingshiru commented 2 years ago

There are still warnings in the console and the delay is much worse than before.

I tested it on my phone. Still the same.

rom1v commented 2 years ago

OK.

And does it work correctly if you connect your mouse directly to the Android device?

rom1v commented 2 years ago

Also, do you reproduce the problem if you use the default mouse injection method instead of HID? (Since mouse motion events are not forwarded when nl mouse buttons are pressed, press the left button and move the mouse.)

nothingshiru commented 2 years ago

And does it work correctly if you connect your mouse directly to the Android device?

I connect my mouse directly to my Android devices via OTG cable. It works correctly.

Also, do you reproduce the problem if you use the default mouse injection method instead of HID? (Since mouse motion events are not forwarded when nl mouse buttons are pressed, press the left button and move the mouse.)

Sorry. I don't quite understand this. What is "default mouse injecton method"?

rom1v commented 2 years ago

Sorry. I don't quite understand this. What is "default mouse injecton method"?

Without --otg.

nothingshiru commented 2 years ago

Do you mean mirroring screen of Android device and controling it on pc screen? In this case, I don't see delay.

rom1v commented 2 years ago

Do you mean mirroring screen of Android device and controling it on pc screen?

Yes. In that case, when you move the mouse with left button pressed, are there any warnings in the console?

nothingshiru commented 2 years ago

No, there is no any warnings in the console.

DymondZ commented 2 years ago

I have the same issue on v1.24 (latest right now) using a Razer DeathAdder Essential which only can be set to 500 or 1000 Hz polling rate. I tested both Using 500 Hz, very tiny movements dont trigger the message and seem to result in a good latency. Moving a fraction more or faster and such triggering the message do result in a very noticable input lag. 1000 Hz does increase the input lag even more I would say you can feel it.

I cut the end of the Terminal output because its repeating:

PS C:\Programs\scrcpy-win64-v1.24> .\scrcpy.exe --otg
scrcpy 1.24 <https://github.com/Genymobile/scrcpy>
INFO: Killing adb daemon (if any)...
INFO: USB device: 227d5ca9 (18d1:4ee7) Xiaomi Redmi Note 10 Pro
WARN: On this platform, libusb does not have hotplug capability; device disconnection will not be detected properly
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)
WARN: Could not request HID event (mouse motion)

I know its asking a bit lot but the DeathAdder Essential is a very cheap gaming mouse by now. I bought it for 28€ 2 years ago not on discount. Cheapest price new I found 22€ just now. Maybe you could consider it for supporting mice that dont go under 500 Hz. I would even use 125 Hz as a workaround if it was available for me. And heck I would even do 10€ via PayPal or send you mine if needed.

iliqiliev commented 2 years ago

Everything described above applies to my 1000Hz mouse on Linux if that is any help

Tryanks commented 1 year ago

When I tried using AOAv2 to forward HID events in my own program, I noticed that each usb.Control command took longer than 1ms, sometimes up to 2ms. I think that's why the high polling rate mouse lags, because it reports faster than AOAv2 can consume.

Tryanks commented 1 year ago

For sending HID events, it is possible to combine multiple HID event data into one send. after AOAv2 receives the data and reports it to the kernel, the system splits and processes the received HID events one by one. this approach keeps the 1000Hz polling data intact and reduces the data interaction frequency to 125Hz. but it introduces a latency of about 8ms. But I think this is enough to solve the problem.