Skip to content

Add OTG support for Windows - #3011

Merged
rom1v merged 10 commits into
devfrom
otg-windows
Feb 20, 2022
Merged

rom1v merged 10 commits into
devfrom
otg-windows

Conversation

@rom1v

@rom1v rom1v commented Feb 10, 2022 •

Copy link
Copy Markdown
Collaborator

scrcpy v1.22 introduced a new OTG mode. It was based on HID keyboard introduced in v1.20 and HID mouse also introduced in v1.22. It was only supported on Linux.

On Windows, it is not possible to open a USB device if it is already open by another process (like the adb daemon). As a consequence, we can't send HID events if adb is running. Therefore, on Windows, --hid-keyboard and --hid-mouse while mirroring are not possible. These features are still limited to Linux.

However, in OTG mode (--otg), no adb is necessary, so it is possible to open the USB device to send HID events. This PR adds support for --otg on Windows.

Concretely:

  • download the unofficial release below
  • extract it
  • double-click on open_a_terminal_here.bat (extracted from the release)
  • plug an Android device over USB
  • execute scrcpy --otg

Fixes #2773


Here is an unofficial release with this branch, so that you can test and report issues:

Note that on my test computer with Windows 10 64bit, the win32 version does not work due to missing libgcc_s_sjlj-1.dll.
According to stackoverflow, a linker flag -static-libgcc must be added somewhere, but I think it is related to prebuilt libusb dll for win32 (since it works for win64).

EDIT: yes, it's a problem on the libusb prebuilt dll for win32: libusb/libusb#1049

@rom1v
rom1v changed the base branch from master to dev February 10, 2022 08:55
@rom1v rom1v mentioned this pull request Feb 17, 2022
rom1v added a commit that referenced this pull request Feb 20, 2022
Currently, there is an issue with the libusb prebuilt dll.

Refs #3011 <#3011>
Refs libusb/#1049 <libusb/libusb#1049>
rom1v added 10 commits February 20, 2022 17:38
USB device disconnection is detected via a hotplug callback when it is
supported.

In addition, report disconnection on libusb calls returning
LIBUSB_ERROR_NO_DEVICE or LIBUSB_ERROR_NOT_FOUND. This allows to detect
disconnection after a libusb call when hotplug is not available.

PR #3011 <#3011>
If it fails, the error is already logged by sc_usb_register_callback().

PR #3011 <#3011>
Convert uint16_t to unsigned to avoid using PRIx16, which may not exist
on Windows.

PR #3011 <#3011>
Add LIBUSB_CALL so that the callback has the correct signature on
Windows (including __attribute__((stdcall))).

PR #3011 <#3011>
The HID/OTG features are now available on all platforms.

PR #3011 <#3011>
All adb commands are executed with an "interruptor", so that they can be
interrupted on Ctrl+C.

Make this interruptor optional, so that we could call "adb kill-server"
in OTG mode. This command always returns almost immediately anyway.

Ideally, we should make all blocking calls interruptible (including
libusb calls, by using the asynchronous API), but it's a lot of work,
and in practice it works well enough.

PR #3011 <#3011>
On Windows, it is not possible to open a USB device from several
process, so HID events may only work if no adb daemon is running.

PR #3011 <#3011>
On Windows, if the adb daemon is running, opening the USB device will
necessarily fail, so HID input is not possible.

Refs #2773 <#2773>
PR #3011 <#3011>
Currently, there is an issue with the libusb prebuilt dll.

Refs libusb/#1049 <libusb/libusb#1049>
PR #3011 <#3011>
@rom1v

rom1v commented Feb 20, 2022

Copy link
Copy Markdown
Collaborator Author

I disabled the feature for win32 due to this prebuilt dll issue. On Win64, it works as expected. I merge. 🚀

@rom1v
rom1v merged commit d9bc508 into dev Feb 20, 2022
@rom1v rom1v mentioned this pull request Feb 21, 2022
2 tasks done
@quyleanh

quyleanh commented Feb 22, 2022 •

Copy link
Copy Markdown
Contributor

Thank you for your hard work.

Just a minor suggest so I don't want to throw new issue.

Do you have any plan to make a Github Action to build automatically for all platforms? For example when each PR is merged to dev branch, the test build will be built and others can test.

@rom1v

rom1v commented Feb 22, 2022

Copy link
Copy Markdown
Collaborator Author

There is a PR for GitHub Actions: #1709
But for some reason, gradle check didn't work and had to be removed for the CI.

I spend some time to figure out why, but didn't manage to make it work, then I worked on something else… I should retry (any help is welcome).

@sameerasw

sameerasw commented Feb 23, 2022 •

Copy link
Copy Markdown

DEBUG: Open USB device 04e8:6860: libusb error: Operation not supported or unimplemented on this platform

any workarounds? Running Android 11 on the device and also compatible with both OTG mouse and kb too. but not in scrcpy?
image

@rom1v

rom1v commented Feb 23, 2022 •

Copy link
Copy Markdown
Collaborator Author

Operation not supported or unimplemented on this platform

That might be an error on another USB device (that's why it's logged at debug level).

Is the device detected by adb (when USB debugging is enabled)?

PS: please post console output as text instead of screenshot.

@sameerasw

Copy link
Copy Markdown

Operation not supported or unimplemented on this platform

That might be an error on another USB device (that's why it's logged at debug level).

Is the device detected by adb (when USB debugging is enabled)?

PS: please post console output as text instead of screenshot.

Yes ,everything else works perfectly except just the OTG function and it says no USB devices. even if I connected multiple devices, the new error screen shows this as a USB device... could windows 11 be a problem?

@rom1v rom1v mentioned this pull request Feb 23, 2022
2 tasks done
@rom1v

rom1v commented Feb 23, 2022

Copy link
Copy Markdown
Collaborator Author

@54m33r4 #3050

rom1v added a commit that referenced this pull request Apr 22, 2022
Upgrade and enable libusb support for Windows 32-bit builds.

Refs #3011 <#3011>
PR #3206 <#3206>
rom1v added a commit that referenced this pull request Apr 22, 2022
Upgrade and enable libusb support for Windows 32-bit builds.

Refs #3011 <#3011>
Fixes #3204 <#3204>
PR #3206 <#3206>
Linux4 pushed a commit to Linux4/scrcpy that referenced this pull request Sep 5, 2025
scrcpy v1.23

Changes since v1.22:
 - Add HID/OTG support for Windows (--otg only) (Genymobile#2773, Genymobile#3011)
 - Add HID/OTG support for macOS (Genymobile#2774, Genymobile#3031)
 - Improve device selection (list devices, -d, -e) (Genymobile#3005)
 - Downscale and retry on MediaCodec error in more cases (Genymobile#2990, Genymobile#3043)
 - Add ZSH completion script (Genymobile#3012)
 - Add Bash completion script (Genymobile#2930, Genymobile#3048)
 - Add --no-cleanup option (Genymobile#1764, Genymobile#3020)
 - Add --printf-fps (Genymobile#468, Genymobile#3030)
 - Print both compiled and linked version of libs on --version
 - Fix FPS counter (broken in v1.22)
 - Various technical refactors and fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HID support on Windows

3 participants