Skip to content

Instantly share code, notes, and snippets.

@bansalnaman15
Last active January 16, 2025 18:32
Show Gist options
  • Save bansalnaman15/5fc93bfaec163bce65e817c40d0f4e9f to your computer and use it in GitHub Desktop.
Save bansalnaman15/5fc93bfaec163bce65e817c40d0f4e9f to your computer and use it in GitHub Desktop.
Setup RaspberryPI streaming listener

RTSP Stream Viewer Setup for Raspberry Pi

A complete guide for setting up an RTSP stream viewer in kiosk mode on Raspberry Pi.

Features

  • Fullscreen RTSP stream display
  • Automatic recovery from disconnections
  • Error message display when camera is unreachable
  • Kiosk mode for dedicated display
  • SSH accessible for management
  • Continuous operation without desktop interference

Prerequisites

  • Raspberry Pi with Raspberry Pi OS (with desktop)
  • Connected display
  • Network connection
  • RTSP camera stream available

Installation

1. Install Required Packages

# Update package list
sudo apt-get update

# Install required packages
sudo apt-get install ffmpeg unclutter x11-xserver-utils

2. Create Stream Script

sudo nano /usr/local/bin/start-stream.sh

Add this content:


# Set display
export DISPLAY=:0
export XAUTHORITY=/home/naman/.Xauthority

# Disable screen blanking and mouse
xset s off
xset s noblank
xset -dpms
unclutter -idle 0 -root &

# Function to display error message using ffplay
show_error_message() {
    export SDL_VIDEODRIVER=x11
    # Start error message without time limit
    ffplay -fs -noborder -alwaysontop -x 1920 -y 1080 -f lavfi -i "color=c=black:s=1920x1080,drawtext=text='Cannot connect to camera... Retrying':fontcolor=white:fontsize=64:x=(w-text_w)/2:y=(h-text_h)/2" &
    ERROR_PID=$!

    while true; do
        if ping -c 1 192.168.68.100 &> /dev/null; then
            kill $ERROR_PID 2>/dev/null
            return 0
        fi
        sleep 1
    done

    kill $ERROR_PID 2>/dev/null
    return 1
}

# Function to try connecting to camera
try_stream() {
    export SDL_VIDEODRIVER=x11
    ffplay -fs -noborder -alwaysontop -x 1920 -y 1080 -fflags nobuffer -flags low_delay -framedrop -rtsp_transport tcp -probesize 32 -analyzeduration 0 -buffer_size 16384 rtsp://aayushi:[email protected]:554/stream2 &
    STREAM_PID=$!

    while kill -0 $STREAM_PID 2>/dev/null; do
        if ! ping -c 1 192.168.68.100 &> /dev/null; then
            kill $STREAM_PID 2>/dev/null
            return 1
        fi
        sleep 1
    done

    return 1
}

# Main loop
while true; do
    if ping -c 1 192.168.68.100 &> /dev/null; then
        try_stream
        if [ $? -ne 0 ]; then
            show_error_message
        fi
    else
        show_error_message
    fi
    sleep 1
done

Make it executable:

sudo chmod +x /usr/local/bin/start-stream.sh

3. Setup Kiosk Mode

Create autostart file:

sudo nano /etc/xdg/autostart/kiosk.desktop

Add:

[Desktop Entry]
Type=Application
Name=Kiosk
Exec=/usr/local/bin/start-stream.sh
X-GNOME-Autostart-enabled=true

4. Create System Service

sudo nano /etc/systemd/system/stream.service

Add:

[Unit]
Description=Auto Start RTSP Stream
After=network.target graphical.target

[Service]
Type=simple
User=naman  # Change to your username
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/naman/.Xauthority  # Change 'naman' to your username
ExecStart=/usr/local/bin/start-stream.sh
Restart=always
RestartSec=3

[Install]
WantedBy=graphical.target

Enable and start service:

sudo systemctl enable stream.service
sudo systemctl start stream.service

Usage

Service Management

# Check status
sudo systemctl status stream.service

# Stop stream
sudo systemctl stop stream.service

# Start stream
sudo systemctl start stream.service

# Restart stream
sudo systemctl restart stream.service

# View logs
journalctl -u stream.service

Kiosk Mode Features

  • Runs in fullscreen without desktop environment visible
  • Auto-restarts on crashes
  • Prevents screen blanking
  • Hides cursor
  • Keeps stream always on top
  • Shows error message when camera is unreachable

Troubleshooting

No Display

  • Check DISPLAY environment variable
  • Verify monitor connection
  • Check Xauthority path matches username

Stream Issues

  • Verify camera IP is reachable
  • Test RTSP URL in VLC player
  • Check network connectivity
  • Review service logs

Desktop Showing

  • Verify kiosk mode setup
  • Check autostart configuration
  • Ensure proper permissions

Customization

You can modify:

  • Error message appearance
  • Stream parameters
  • Retry intervals
  • Screen resolution
  • RTSP URL and credentials

Remember to restart the service after changes:

sudo systemctl restart stream.service

Notes

  • SSH access recommended for management
  • Keep backup of working configuration
  • Test changes in non-production first
  • Monitor system resources periodically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment