Posts Projects //TODO -> Links /posts/Casting-Android-To-Raspberry-Pi$_

Casting Android to a Raspberry Pi

Recently I undertook a bit of a road trip, the kind that involves the better part of thousand kilometers. The kind that also includes three young children (read: five and under) in the back of the car. Clearly, entertainment was going to be needed. Our car actually has an after-party roof-mounted DVD player, which is great, but we only have a couple of DVDs that are in the correct region for it, and as a result I'm far better accquainted with both Aladdin and The Lion King than I'd really like to be. Naturally I set about finding a way to get other content onto that screen; It has composite input, which is a good start, but I didn't want to buy anything new, and seemed to find a lot of mixed reports around the various cables and adapter combinations on offer (typically Phone -> HDMI -> Composite). No problem, I thought, I've got a spare Raspberry Pi knockout about, surely I can do something with that? I could, but in 2021 it took quite a while to work out what the correct setup was, so I'm listing it here in the hope that it'll save somebody else a lot of time digging through older resources.

Step 1: Install scrcpy

scrcpy is a program that connects to your Android phone via adb, and streams the display to a window, allowing you to interact and control the phone with your keyboard and mouse. I found various options for dealing with this, but in short it boiled down to these easy steps when running the latest version of the Raspberry Pi OS.

First, install the Snap Store and ADB:

sudo apt update
sudo apt install snapd
sudo apt install adb

Reboot:

sudo reboot

And finally, install scrcpy:

sudo snap install --beta scrcpy

Step 1.5: Test scrcpy

Plug in your Android phone via a USB cable, start the adb server, and run scrcpy:

adb start-server
scrcpy

You'll likely see a confirmation message on your phone asking for permission to enable USB debugging. Accept it, you may find you need to run scrcpy again after doing so. When it works, you should see a bunch of messages fly by too fast to read, before a window pops up showing your screen.

Step 2: Enabling Hardware Acceleration

At this point my setup worked, but it was smashing the CPU of the little Pi and showing both a poor frame rate and a lot of lag. I'm using a Raspberry Pi 3B, and found a few different posts and the like for turning on hardware acceleration, but after a fair while trying out different options the solution turned out to be very simple (at least running scrcpy version 1.17). Run the Raspberry Pi configuration tool:

sudo raspi-config

Navigate to Advanced Options -> GL Driver and enable GL (Fake KMS). That's it. Run scrcpy again and you should see it responding much faster, and at a much higher framerate.

Step 3: Workout Your Command Line Options

scrcpy has a few different command line options you can specify, for my particular setup I wanted the video full-screen, horizontal, and didn't need a crazy resolution, so I simply used this:

scrcpy --rotation 0 -m 800 -f

where:

Step 4: Profit?

To make life easier in the car I configured the Raspberry PI desktop to start a terminal once X starts, which I did by editing /etc/xdg/lxsession/LXDE-pi/autostart and adding @lxterminal to it.

To facilitate start up (and restarting - I found that scrcpy would crash on occasion as well, seemingly related to some kind of rendering context changes on the phone when moving between apps and the launcher), I added a quick and dirty shell script called start to the system with the following content:

adb kill-server
adb start-server
scrcpy -m 720 --rotation 0 -f

Step 5: Save Your Filesystem From Corruption

Outside of a car you probably wouldn't need this, but I didn't want my SD card to get corrupted through the Pi being powered down suddenly without a proper shutdown procedure. There's a tonne of guides around on making the file system readonly, and I followed a few, again to find that most were out of date. The easiest option as of the time of writing is to again launch the Pi configuration tool (sudo raspi-config), navigate to 'Performance Options' -> 'Overlay File System' and turn it on. Job done!

Results

This setup worked wonders for our trip, it did crash once or twice but not nearly as much as when I was testing, I guess because the phone was simply running Netflix most of the time and wasn't moving between apps. I mention Netflix in particular because that worked well, some other streaming apps wouldn't work at all, menus etc. would be fine but the moment content started scrcpy would bail. It seemed to be related to the codecs used for the streams but with Netflix working well I didn't feel the need to spend more than ten fruitless minutes trying to work that out.