SCRCPY

Apr 21, 2024

SCRCPY en Raspberry Pi

SCRCPY on Raspberry PI

Now with Audio!!!(Android>11)

Tutorial realizado en conjunto con Alvaro Vidal

A veces solemos necesitar ver la pantalla de nuestro Smartphone Android en un proyector, seas docente, brindes conferencias, o simplemente porque quieres ver tu pantalla en un proyector!. Para eso, que mejor que usar nuestra querida Raspberry Pi 5, zero 2w, 400, 4B, 3B+, 3B, y su distribución preferida Raspberry Pi OS!!!.

Para realizar este trabajo, necesitaremos compilar una espectacular aplicación o servicio llamado SCRCPY  desarrollado por Genymobile. Nuestro Smartphone debe tener un Sistema Operativo superior a Android 5.0(API 21.0). Además necesitaremos el servidor pre-compilado que lo descargaremos de github.

Sometimes we usually need to see the screen of our Android Smartphone on a projector, whether you are a teacher, give conferences, or simply because you want to see your screen on a projector! For that, what better than using our beloved Raspberry Pi  5, zero 2W, 400, 4B, 3B+, 3B, and your favorite distribution Raspberry Pi OS!!!.

To do this job, we'll need to build a cool application or service called SCRCPY developed by Genymobile. Our Smartphone must have an Operating System higher than Android 5.0 (API 21.0). We will also need the pre-compiled server that we will download from github.

Manos a la obra--Let's do it!:

Primero necesitaremos tener instalado--First we need install Rasperry Pi OS

Segundo en tú Smartphone busca como activar ADB debugging. La mayoría de las veces hay que activar el modo de desarrollador, yendo a Preferencias-> Acerca del teléfono y darle varios toques a el Número de compilación Versión del Kernel. En cada Smartphone es diferente, googlea esta info para tu teléfono.(Cómo activar Modo Desarrollador en mi celular MARCA).

Second, on your Smartphone, look for how to activate ADB debugging. Most of the time you have to activate the developer mode, by going to Preferences-> About phone and giving several taps to the Build Number Kernel Version. Each Smartphone is different, google this info for your phone. "How to activate Developer Mode on my BRAND cell phone".

Debemos bajarnos el servidor que enviaremos a nuestro smartphone al ejecutar SCRCPY, esto se hará por ADB. Para ello abrimos nuestra Terminal y tecleamos:

We must download the server that we will send to our smartphone when executing SCRCPY, this will be done by ADB. To do this we open our Terminal and type:

Declare the variable ver with the number version, keep Terminal open, if you close it, declare the variable again--Declaramos la variable ver asignándole el número de versión actual, mantén la Terminal abierta, si la cierras vuelve a declarar la variable:


Una vez descargado, nos quedará en la ruta $HOME/scrcpy-server-${VER} , debemos tener en cuenta ésta dirección para más adelante!

Ahora tenemos que instalar ciertas dependencias para que nuestra compilación sea exitosa, hay programas que ya se encuentran instalados, pero no nos fiamos y queremos que ustedes también disfruten de Scrcpy(Screen Copy):

Once downloaded, we will be in the $HOME/scrcpy-server-${VER} path, we must take this address into account for later!


Now we have to install certain dependencies for our compilation to be successful, there are programs that are already installed, but we don't trust them and we want you to enjoy Scrcpy(Screen Copy) too:


Ya tenemos las dependencias cumplidas!!!--We have all the necesaries dependencies!!!


Clonamos el proyecto  nuestra Raspberry Pi utilizando git para eso lo instalamos(aunque ya viene en Raspberry Pi OS) y nos movemos al nuevo directorio--Clone the repo to our system and move to the cloned folder:


Construimos nuestro propio scrcpy(Áca usamos la ruta de scrcpy-server) Sin sudo:

We build our own scrcpy (here we use the scrcpy-server path) without sudo:

Si todo salió bien, vas a ver 14 YES verdes.--If all goes fine we see 14 Green YES

Ejecutamos--Execute ninja:

Instalamos la compilación--Install the compiled:

Ahora podemos eliminar el archivo que descargamos con wget y el repositorio clonado--Now we can delete the cloned folder and the server downloaded:

Listo si seguiste todos los pasos, cumpliste las dependencias, clonaste, te moviste al directorio clonado, compilaste e instalaste, nada más que tu celu u tu USB puede fallar, nosotros replicamos las mismas líneas escritas aquí y las pegamos en la Terminal de la Pi4b, así que si algo te falla revisa cada pasito.

Llegó la hora de la verdad! Si lograste acceder al menú de desarrollador de tu teléfono y activar el Adb debugging, es hora de que lo enchufes al puerto USB de tu Raspberry Pi!

Permitir el acceso, a mí siempre me da un error de cache en MTP, pero puedo ver las carpetas de mi celu tranquilamente.

Ready if you followed all the steps, fulfilled the dependencies, cloned, moved to the cloned directory, compiled and installed, nothing more than your cell phone or your USB can fail, we replicate the same lines written here and paste them in the Pi4B Terminal , so if something fails you check each little step.

It's time for the truth! If you managed to access the developer menu of your phone and activate Adb debugging, it's time to plug it into the USB port of your Raspberry Pi!

I allowed access, it always gives me a cache error in MTP, but I can see my cell phone folders calmly.

Si ya cerraste la Terminal ábrela nuevamente y escribe--Open the Terminal and type:

Te va a saltar algo similar, tilda Permitir siempre y Aceptar--Accept

Y en la Terminal esto.--You will see something similar

Ejecutamos nuevamente y funciona(Videos funcionando)--Execute again and now will work:

Grabado con--Record with scrcpy -r demo.mkv

Scrcpy version 1.14
Instalación o actualización a Scrcpy v1.16
Lanzado con el comando:scrcpy --render-driver=opengles2 -b 10M

SCRCPY a traves de WiFi requisitos--SCRCPY Wireless:

En la Raspberry Pi conectaremos nuestro celular por USB, permitiendo todo permiso normal que nos solicite, abrimos La Terminal (CTRL+ALT+T) e introducimos lo siguiente:

In the Raspberry Pi we will connect our cell phone via USB, allowing all normal permission that it requests, we open The Terminal (CTRL+ALT+T) and enter the following:

Luego abrimos la app ADB Wireless que nos descargamos, sirve cualquiera. Presionamos el rectangulo Show Wifi IP, nos mostrara nuestra IP, que usaremos para realizar la conexión.

Then we open the ADB Wireless app that we downloaded, any one will do. Press the Show Wifi IP rectangle, it will show us our IP, which we will use to make the connection.

Volvemos a la Terminal y escribimos el comando con la IP mostrada en la app, ejemplo--Type on terrminal with the IP that give you Show Wifi IP on the app:

Una vez conectado podemos desconectar el cable USB y ejecutar SCRCPY--Once is connected we can unplug the USB cable and execute SCRCPY:

Tras lo cual debemos ver nuestro celular en la pantalla de la Raspberry Pi!--After this you will see your phone in the Raspberry Pi wireless!!

Nuevo Método Oficial--New Official method for wireless:

Simplify TCP/IP (wireless) configuration

Scrcpy uses adb to communicate with the device. To use scrcpy wirelessly, the users had to find the device IP address and connect via TCP/IP using adb directly.

For convenience, this version introduces a new option to configure TCP/IP connections automatically, with two variants.

If the device (accessible at 192.168.1.1 in this example) already listens on a port (typically 5555) for incoming adb connections, then run:

If the adb TCP/IP mode is disabled (or if you don't know the IP address), connect the device over USB, then run:

It will automatically find the device IP address (by parsing adb shell ip route), enable TCP/IP mode, then connects to the device before starting.


Configure tunneling

Two new options --tunnel-host and --tunnel-port (implemented by RipleyTom) allow to connect to a remote ADB server. This allows for example to mirror a device plugged on another computer easily.

To connect to a remote ADB server (for example 192.168.1.2), make the server listen on all interfaces:

Then, from another terminal, run scrcpy:

Note that all ADB communications between the server and the client are unencrypted.

To connect to a remote ADB server over the Internet, it is strongly recommended to use a SSH tunnel instead.

Comandos útiles de--Useful commands SCRCPY:

Uso--Use: scrcpy [opciones]

Opciones--Options:

        Make scrcpy window always on top (above other windows).

       Encode the audio at the given bit-rate, expressed in bits/s. Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).

       Default is 128K (128000).

        Configure the audio buffering delay (in milliseconds).

        Lower values decrease the latency, but increase the likelyhood of buffer underrun (causing audio glitches).

         Default is 50.

        Select an audio codec (opus, aac or raw).

        Default is opus.

        Set a list of comma-separated key:type=value options for the device audio encoder.

        The possible values for 'type' are 'int' (default), 'long', 'float' and 'string'.

        The list of possible codec options is available in the Android documentation:

        <https://d.android.com/reference/android/media/MediaFormat>

        Use a specific MediaCodec audio encoder (depending on the codec provided by --audio-codec).

        The available encoders can be listed by --list-encoders.

        Encode the video at the given bit-rate, expressed in bits/s.

        Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).

        Default is 8M (8000000).

        Crop the device screen on the server. The values are expressed in the device natural orientation (typically, portrait for a phone, landscape for a tablet).

        Any --max-size value is computed on the cropped size.

        Use USB device (if there is exactly one, like adb -d).

        Also see -e (--select-tcpip).

        Disable screensaver while scrcpy is running.

        Specify the device display id to mirror.

        The available display ids can be listed by:

            scrcpy --list-displays

       Default is 0.

        Add a buffering delay (in milliseconds) before displaying. This increases latency to compensate for jitter.

        Default is 0 (no buffering).

        Use TCP/IP device (if there is exactly one, like adb -e).

        Do not attempt to use "adb reverse" to connect to the the device.

        By default, right-click triggers BACK (or POWER on) and middle-click triggers HOME. This option disables these shortcuts and forward the clicks to the device instead.

        Start in fullscreen.

        Simulate a physical keyboard by using HID over AOAv2.

        It provides a better experience for IME users, and allows to generate non-ASCII characters, contrary to the default injection method.

        It may only work over USB.

        The keyboard layout must be configured (once and for all) on the device, via Settings -> System -> Languages and input -> Physical keyboard. This settings page can be started directly: 'adb shell am start -a android.settings.HARD_KEYBOARD_SETTINGS'.

        However, the option is only available when the HID keyboard is enabled (or a physical keyboard is connected).

        Also see --hid-mouse.

        Print this help.

        Inject computer clipboard text as a sequence of key events on Ctrl+v (like MOD+Shift+v). This is a workaround for some devices not behaving as expected when setting the device clipboard programmatically.

        List device displays.

        List video and audio encoders available on the device.

        Lock video orientation to value. Possible values are "unlocked", "initial" (locked to the initial orientation), 0, 1, 2 and 3. Natural device orientation is 0, and each increment adds a 90 degrees rotation counterclockwise.

        Default is "unlocked"

        Passing the option without argument is equivalent to passing "initial".

        Limit the frame rate of screen capture (officially supported since Android 10, but may work on earlier versions).

        Simulate a physical mouse by using HID over AOAv2.

        In this mode, the computer mouse is captured to control the device directly (relative mouse mode).

        LAlt, LSuper or RSuper toggle the capture mode, to give control of the mouse back to the computer.

        It may only work over USB.

        Also see --hid-keyboard.

        Limit both the width and height of the video to value. The other dimension is computed so that the device aspect-ratio is preserved.

        Default is 0 (unlimited).

        Disable audio forwarding.

        By default, scrcpy removes the server binary from the device and restores the device state (show touches, stay awake and power mode) on exit.

        This option disables this cleanup.

        By default, scrcpy automatically synchronizes the computer clipboard to the device clipboard before injecting

        Ctrl+v, and the device clipboard to the computer clipboard whenever it changes.

        This option disables this automatic synchronization.

        By default, on MediaCodec error, scrcpy automatically tries again with a lower definition.

        This option disables this behavior.

        Disable device control (mirror the device in read-only).

        Do not display device (only when screen recording or V4L2 sink is enabled).

        Do not forward repeated key events when a key is held down.

        If the renderer is OpenGL 3.0+ or OpenGL ES 2.0+, then mipmaps are automatically generated to improve downscaling quality. This option disables the generation of mipmaps.

        Do not power on the device on start.

        Run in OTG mode: simulate physical keyboard and mouse, as if the computer keyboard and mouse were plugged directly to the device via an OTG cable.

        In this mode, adb (USB debugging) is not necessary, and mirroring is disabled.

        LAlt, LSuper or RSuper toggle the mouse capture mode, to give control of the mouse back to the computer.

        If any of --hid-keyboard or --hid-mouse is set, only enable keyboard or mouse respectively, otherwise enable both.

        It may only work over USB.

        See --hid-keyboard and --hid-mouse.

        Set the TCP port (range) used by the client to listen.

        Default is 27183:27199.

        Turn the device screen off when closing scrcpy.

        Inject alpha characters and space as text events instead of key events. This avoids issues when combining multiple keys to enter a special character, but breaks the expected behavior of alpha keys in games (typically WASD).

        Start FPS counter, to print framerate logs to the console. It can be started or stopped at any time with MOD+i.

        Set the target directory for pushing files to the device by drag & drop. It is passed as-is to "adb push".

        Default is "/sdcard/Download/".

        Inject key events for all input keys, and ignore text events.

        Record screen to file. The format is determined by the --record-format option if set, or by the file extension (.mp4 or .mkv).

        Force recording format (either mp4 or mkv).

        Request SDL to use the given render driver (this is just a hint). Supported names are currently "direct3d", "opengl", "opengles2", "opengles", "metal" and "software". <https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>

        By default, scrcpy mirrors only the video when audio capture fails on the device. This option makes scrcpy fail if audio is enabled but does not work.

        Set the initial display rotation. Possibles values are 0, 1, 2 and 3. Each increment adds a 90 degrees rotation counterclockwise.

        The device serial number. Mandatory only if several devices are connected to adb.

        Specify the modifiers to use for scrcpy shortcuts. Possible keys are "lctrl", "rctrl", "lalt", "ralt", "lsuper" and "rsuper". 

        A shortcut can consist in several keys, separated by '+'. Several shortcuts can be specified, separated by ','.

        For example, to use either LCtrl+LAlt or LSuper for scrcpy shortcuts, pass "lctrl+lalt,lsuper".

        Default is "lalt,lsuper" (left-Alt or left-Super).

        Turn the device screen off immediately.

        Enable "show touches" on start, restore the initial value on exit. It only shows physical touches (not clicks from scrcpy).

        Configure and reconnect the device over TCP/IP.

        If a destination address is provided, then scrcpy connects to this address before starting. The device must listen on the given TCP port (default is 5555).

        If no destination address is provided, then scrcpy attempts to find the IP address of the current device (typically connected over USB), enables TCP/IP mode, then connects to this address before starting.

        Set the IP address of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.

        Default is localhost.

        Set the TCP port of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.

        Default is 0 (not forced): the local port used for establishing the tunnel will be used.

        Output to v4l2loopback device. It requires to lock the video orientation (see --lock-video-orientation).

        Add a buffering delay (in milliseconds) before pushing frames. This increases latency to compensate for jitter.

        This option is similar to --display-buffer, but specific to V4L2 sink.

        Default is 0 (no buffering). This option is only available on Linux.

        Set the log level (verbose, debug, info, warn or error).

        Default is info.

        Print the version of scrcpy.

        Select a video codec (h264, h265 or av1).

        Default is h264.

        Set a list of comma-separated key:type=value options for the device video encoder.

        The possible values for 'type' are 'int' (default), 'long', 'float' and 'string'.

        The list of possible codec options is available in the Android documentation: <https://d.android.com/reference/android/media/MediaFormat>

        Use a specific MediaCodec video encoder (depending on the codec provided by --video-codec).

        The available encoders can be listed by --list-encoders.

        Keep the device on while scrcpy is running, when the device is plugged in.

        Disable window decorations (display borderless window).

        Set a custom window title.

        Set the initial window horizontal position.

        Default is "auto".

        Set the initial window vertical position.

        Default is "auto".

        Set the initial window width.

        Default is 0 (automatic).

        Set the initial window height.

        Default is 0 (automatic).


Shortcuts:

In the following list, MOD is the shortcut modifier. By default, it's (left) Alt or (left) Super, but it can be configured by: --shortcut-mod (see above).


        Switch fullscreen mode

        Rotate display left

        Rotate display right

        Resize window to 1:1 (pixel-perfect)

       Double-click on black borders

        Resize window to remove black borders

        Middle-click

        Click on HOME

        Right-click (when screen is on)

        Click on BACK

        4th-click

 Click on APP_SWITCH

        Click on MENU

        Click on VOLUME_UP

        Click on VOLUME_DOWN

        Click on POWER (turn screen on/off)

        Power on

        Turn device screen off (keep mirroring)

        Turn device screen on

        Rotate device screen

        Expand notification panel

        Collapse notification panel

        Copy to clipboard (inject COPY keycode, Android >= 7 only)

        Cut to clipboard (inject CUT keycode, Android >= 7 only)

        Copy computer clipboard to device, then paste (inject PASTE keycode, Android >= 7 only)

        Inject computer clipboard text as a sequence of key events

        Enable/disable FPS counter (print frames/second in logs)

        Pinch-to-zoom from the center of the screen

        Install APK from computer

         Push file to device (see --push-target)

Environment variables:

    ADB

        Path to adb executable

    ANDROID_SERIAL

        Device serial to use if no selector (-s, -d, -e or --tcpip=<addr>) is specified

    SCRCPY_ICON_PATH

        Path to the program icon

    SCRCPY_SERVER_PATH

        Path to the server binary

Exit status:

      0  Normal program termination

      1  Start failure

      2  Device disconnected while running