Pinephone as a remote motion detection camera 1st iteration

How to use a pinephone as a remote camera with motion detection, test 1

April 19, 2022
sysadmin pinephone motioneye remote camera

Using a pinephone as a remote motion detection camera

So I have this pinephone coupled with the keyboard battery case that I have been using as a remote SDR relay for my radio nerdity. It works fine, but I wanted to add a new dimension to it, a camera I can use pretty much everywhere to relay video/audio.

First iteration today using v4l2loopback, ffmpeg and motioneye for motion detection. I will add sound later.

So starting from an archlinux install on the pinephone ( I do recommend you use yay as your package manager, so that you can easily get things from AUR, but YMMV based on your preferences ).

yay -S python-pip motioneye  python-babel python-boto3 linux-megi-headers
yay -S v4l2loopback-utils

That will get us the basics installed.

Now the fun part, the camera from the pinephone is not a standard v4l device. It is a bit more complicated, because two sensors are exposed on the same device ( front and back ) and you can’t just use /dev/videoX as a simple v4l camera.

So what we have to do is use media-ctl to set the parameters for the camera ( which sensors / resolution / pixel format ) and use ffmpeg to read the raw output and write it back to a loopback so that motioneye can consume it.

Here’s the script:

#!/bin/bash
# Copyright gled@remote-shell.net, credits please if using and modifying, thanks !

function cleanup () {
	kill %?ffmpeg
	sudo systemctl stop motioneye
	sudo rmmod v4l2loopback
	exit
}

trap cleanup SIGINT SIGTERM ERR EXIT

sudo modprobe v4l2loopback video_nr=3 card_label="FFmpeg Stream" exclusive_caps=1;

media-ctl -d /dev/media1 --set-v4l2 '"ov5640 5-004c":0[fmt:UYVY8_2X8/1280x720@1/30]'
ffmpeg -hide_banner -f rawvideo  -pix_fmt yuv420p  -s 1280x720  -f video4linux2  -i /dev/video0 -vcodec rawvideo -pix_fmt yuv420p -f v4l2 /dev/video3 -loglevel error &

sudo systemctl restart motioneye

#ips=`ip addr | grep inet | grep -v 'host lo' | grep -v '::1/128' | grep -v 'inet6 fe80:' | awk '{print $2}' | cut -d / -f 1 | awk '{print "http://"$1":8765"}' | xargs`
ips=`ip addr | grep inet | grep -v 'host lo' | grep -v 'inet6' | awk '{print $2}' | cut -d / -f 1 | awk '{print "http://"$1":8765"}' | xargs`
echo "Started motioneye and ffmpeg, ctrl+c or kill to exit"
echo "Ip addresses: $ips"

while true; do
	sleep 10;
done

Save it, chmod +x it and launch it ( in a screen or tmux maybe ? ). You will have the ip address to connect to motioneye written on the CLI. You will need to add the dummy webcam to motioneye ( easy, this is the one named FFmpeg stream ) when you add a v4l source.

Now, the script may not work because your camera chip may not be exposed on /dev/media1 but another /dev/media. You can check that by using media-ctl -p -d /dev/mediaX. the one you want is the one with the driver sun6i-csi.

Same thing, /dev/video0 used by ffmpeg as the raw input may not be valid, you may have to use /dev/video1 or else depending on your distro / kernel / whatever. Adapt to your needs.

Lastly, do you see the line containing “ov5640 5-004c” ? this is the line that sets the sensors parameters ( namely in this example yuv420 pixel format, 1280x720 capture size and 30 fps ). Adapt to your needs too, but the OG pinephone is not super powerful, so don’t expect full HD 60fps…

Anyway, there you go, remote camera with motion detection.

Next steps, use a sdcard to store the videos, since you do not want to use the emmc for that, which you configure on motioneye UI.

Now to work on a solution with proper audio and maybe lighter software stack ?

Happy to receive comments as usual on activitypub at gled@ap.remote-shell.net ! ( Use mastodon / or any other activitypub instance to send comments ! ).

Bonus script to stream audio via ssh:

#!/bin/bash
# Copyright gled@remote-shell.net, credits please if using and modifying, thanks !

function cleanup () {
	alsactl --file "$OLDAUDIOF" restore
	exit
}

OLDAUDIOF="$(mktemp)"
alsactl --file "$OLDAUDIOF" store

trap cleanup EXIT SIGINT SIGKILL

sxmo_megiaudioroute -m
arecord -D plug:dsnoop -f cd -c 1

You can use it as ssh user@phoneip “./streamaudioscript.sh” | aplay