SCRCPY
SCRCPY en Raspberry Pi
SCRCPY on Raspberry PI
Now with Audio!!!(Android>11)
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:
VER=v2.4
wget -c https://github.com/Genymobile/scrcpy/releases/download/$ver/scrcpy-server-${VER}
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:
sudo apt-get -y install ffmpeg libsdl2-2.0-0 adb gcc git pkg-config meson ninja-build libavcodec-dev libavformat-dev libavutil-dev libsdl2-dev openjdk-17-jdk libavdevice-dev libusb-1.0-0-dev cmake
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:
cd
git clone https://github.com/Genymobile/scrcpy
cd scrcpy
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:
meson x --buildtype release --strip -Db_lto=true -Dprebuilt_server=$HOME/scrcpy-server-${VER}
Si todo salió bien, vas a ver 14 YES verdes.--If all goes fine we see 14 Green YES
Ejecutamos--Execute ninja:
ninja -Cx
Instalamos la compilación--Install the compiled:
sudo ninja -Cx install
Ahora podemos eliminar el archivo que descargamos con wget y el repositorio clonado--Now we can delete the cloned folder and the server downloaded:
cd ..
rm -rf scrcpy
rm scrcpy-server*
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:
scrcpy
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:
scrcpy
Grabado con--Record with scrcpy -r demo.mkv
SCRCPY a traves de WiFi requisitos--SCRCPY Wireless:
Smartphone con modo desarrollador activo--With ADB Activated(Ya lo tenemos si seguimos los pasos de arriba)
Cable USB
Smartphone y Raspberry Pi en la misma Red WiFi--Smartphone & Raspberry Pi on the same WiFi network.
Aplicación para manejar--App for manage adb tcpip connection on the phone en el celu:
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:
adb tcpip 5555
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:
adb connect 192.16.0.99
Una vez conectado podemos desconectar el cable USB y ejecutar SCRCPY--Once is connected we can unplug the USB cable and execute SCRCPY:
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:
scrcpy --tcpip=192.168.1.1 # default port is 5555
scrcpy --tcpip=192.168.1.1:5555
If the adb TCP/IP mode is disabled (or if you don't know the IP address), connect the device over USB, then run:
scrcpy --tcpip # without arguments
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:
adb kill-server
adb -a nodaemon server start
# keep this open
Then, from another terminal, run scrcpy:
export ADB_SERVER_SOCKET=tcp:192.168.1.2:5037
scrcpy --tunnel-host=192.168.1.2
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:
--always-on-top
Make scrcpy window always on top (above other windows).
--audio-bit-rate=value
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).
--audio-buffer=ms
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.
--audio-codec=name
Select an audio codec (opus, aac or raw).
Default is opus.
--audio-codec-options=key[:type]=value[,...]
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>
--audio-encoder=name
Use a specific MediaCodec audio encoder (depending on the codec provided by --audio-codec).
The available encoders can be listed by --list-encoders.
-b, --video-bit-rate=value
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=width:height:x:y
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.
-d, --select-usb
Use USB device (if there is exactly one, like adb -d).
Also see -e (--select-tcpip).
--disable-screensaver
Disable screensaver while scrcpy is running.
--display=id
Specify the device display id to mirror.
The available display ids can be listed by:
scrcpy --list-displays
Default is 0.
--display-buffer=ms
Add a buffering delay (in milliseconds) before displaying. This increases latency to compensate for jitter.
Default is 0 (no buffering).
-e, --select-tcpip
Use TCP/IP device (if there is exactly one, like adb -e).
Also see -d (--select-usb).
--force-adb-forward
Do not attempt to use "adb reverse" to connect to the the device.
--forward-all-clicks
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.
-f, --fullscreen
Start in fullscreen.
-K, --hid-keyboard
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.
-h, --help
Print this help.
--legacy-paste
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-displays
List device displays.
--list-encoders
List video and audio encoders available on the device.
--lock-video-orientation[=value]
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".
--max-fps=value
Limit the frame rate of screen capture (officially supported since Android 10, but may work on earlier versions).
-M, --hid-mouse
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.
-m, --max-size=value
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).
--no-audio
Disable audio forwarding.
--no-cleanup
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.
--no-clipboard-autosync
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.
--no-downsize-on-error
By default, on MediaCodec error, scrcpy automatically tries again with a lower definition.
This option disables this behavior.
-n, --no-control
Disable device control (mirror the device in read-only).
-N, --no-display
Do not display device (only when screen recording or V4L2 sink is enabled).
--no-key-repeat
Do not forward repeated key events when a key is held down.
--no-mipmaps
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.
--no-power-on
Do not power on the device on start.
--otg
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.
-p, --port=port[:port]
Set the TCP port (range) used by the client to listen.
Default is 27183:27199.
--power-off-on-close
Turn the device screen off when closing scrcpy.
--prefer-text
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).
--print-fps
Start FPS counter, to print framerate logs to the console. It can be started or stopped at any time with MOD+i.
--push-target=path
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/".
--raw-key-events
Inject key events for all input keys, and ignore text events.
-r, --record=file.mp4
Record screen to file. The format is determined by the --record-format option if set, or by the file extension (.mp4 or .mkv).
--record-format=format
Force recording format (either mp4 or mkv).
--render-driver=name
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>
--require-audio.
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.
--rotation=value
Set the initial display rotation. Possibles values are 0, 1, 2 and 3. Each increment adds a 90 degrees rotation counterclockwise.
-s, --serial=serial
The device serial number. Mandatory only if several devices are connected to adb.
--shortcut-mod=key[+...]][,...]
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).
-S, --turn-screen-off
Turn the device screen off immediately.
-t, --show-touches
Enable "show touches" on start, restore the initial value on exit. It only shows physical touches (not clicks from scrcpy).
--tcpip[=ip[:port]].
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.
--tunnel-host=ip.
Set the IP address of the adb tunnel to reach the scrcpy server. This option automatically enables --force-adb-forward.
Default is localhost.
--tunnel-port=port
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.
--v4l2-sink=/dev/videoN
Output to v4l2loopback device. It requires to lock the video orientation (see --lock-video-orientation).
--v4l2-buffer=ms
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.
-V, --verbosity=value
Set the log level (verbose, debug, info, warn or error).
Default is info.
-v, --version
Print the version of scrcpy.
--video-codec=name
Select a video codec (h264, h265 or av1).
Default is h264.
--video-codec-options=key[:type]=value[,...]
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>
--video-encoder=name.
Use a specific MediaCodec video encoder (depending on the codec provided by --video-codec).
The available encoders can be listed by --list-encoders.
-w, --stay-awake
Keep the device on while scrcpy is running, when the device is plugged in.
--window-borderless
Disable window decorations (display borderless window).
--window-title=text
Set a custom window title.
--window-x=value
Set the initial window horizontal position.
Default is "auto".
--window-y=value
Set the initial window vertical position.
Default is "auto".
--window-width=value
Set the initial window width.
Default is 0 (automatic).
--window-height=value
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).
MOD+f
Switch fullscreen mode
MOD+Left
Rotate display left
MOD+Right
Rotate display right
MOD+g
Resize window to 1:1 (pixel-perfect)
MOD+w
Double-click on black borders
Resize window to remove black borders
MOD+h
Middle-click
Click on HOME
MOD+b
MOD+Backspace
Right-click (when screen is on)
Click on BACK
MOD+s
4th-click
Click on APP_SWITCH
MOD+m
Click on MENU
MOD+Up
Click on VOLUME_UP
MOD+Down
Click on VOLUME_DOWN
MOD+p
Click on POWER (turn screen on/off)
Right-click (when screen is off)
Power on
MOD+o
Turn device screen off (keep mirroring)
MOD+Shift+o
Turn device screen on
MOD+r
Rotate device screen
MOD+n
Expand notification panel
MOD+Shift+n
Collapse notification panel
MOD+c
Copy to clipboard (inject COPY keycode, Android >= 7 only)
MOD+x
Cut to clipboard (inject CUT keycode, Android >= 7 only)
MOD+v
Copy computer clipboard to device, then paste (inject PASTE keycode, Android >= 7 only)
MOD+Shift+v
Inject computer clipboard text as a sequence of key events
MOD+i
Enable/disable FPS counter (print frames/second in logs)
Ctrl+click-and-move
Pinch-to-zoom from the center of the screen
Drag & drop APK file
Install APK from computer
Drag & drop non-APK file
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