#!/bin/bash # # front-end to gstreamer for testing common video in/out scenarios # Print usage and exit # $1 if opt error print_usage() { [ "$1" ] && echo "Invalid option: -$1" echo "Pipes video from a source pattern to specified sink" echo -n "usage:" echo " $0 [OPTIONS]" echo " -h This help message" echo " -i Input device (/dev/video) (0, 1, 2, ...) Defaults to 0" echo " -o Output framebuffer (/dev/fb) (0, 1, 2, ...) Defaults to 0" echo " -p Which pattern from 0-20, defaults to 0" echo " -b Blank all other screens before video output" echo " -B Background Overlay 'pattern' to frame-buffer+1" echo " -c Executes gst-launch " echo " -I Set gstreamer src plugin, defaults to videotestsrc" echo " -O Set gstreamer sink plugin, defaults to mfw_v4lsink" echo " -u Leave frame-buffer unblanked" echo "" echo "Example Usage:" echo " $0 -o 2 # output colorbar test pattern to /dev/fb2" echo " $0 -i 0 -o 0 -B 18 # HDMI video in (/dev/video0) to HDMI video out (/dev/fb0) with overlay" echo " $0 -i 1 -o 0 -B 18 # Analog video in (/dev/video1) to HDMI video out (/dev/fb0) with overlay" echo " $0 -i 1 -o 2 -B 18 # Analog video in (/dev/video1) to Analog video out (/dev/fb2) with overlay" exit 1 } OUTFB=0 INDEV= PATTERN=0 VIDEOSRC= VIDEOSINK=mfw_v4lsink BLANK_ME= PIPELINE= UNBLANK= OVERLAY= PID_BG= PID_BLANK= while getopts :o:i:p:bc:B:I:O:u opt do case "$opt" in o) OUTFB=$OPTARG;; i) INDEV=$OPTARG;; p) PATTERN=$OPTARG;; b) BLANK_ME=/sys/class/graphics/fb*;; c) PIPELINE=$OPTARG;; B) OVERLAY=$OPTARG;; I) VIDEOSRC=$OPTARG;; O) VIDEOSINK=$OPTARG;; u) UNBLANK=1;; :) echo "-$OPTARG requires an argument" print_usage ;; *) print_usage;; esac done # sanity checks [ -c "/dev/fb$OUTFB" ] || { echo "invalid framebuffer" print_usage } [ $INDEV ] && { [ -c "/dev/video$INDEV" ] || { echo "invalid input device" print_usage } } # blank framebuffers for i in $BLANK_ME; do [ -r $i/blank ] && { echo "Unblanking $i" echo 1 > $i/blank } done hdmi_in() { local dir local state [ -d /sys/devices/platform/imx-i2c.2 ] && \ dir=/sys/devices/platform/imx-i2c.2/i2c-2/2-0048/ [ -d /sys/devices/soc0/soc.1/2100000.aips-bus/21a8000.i2c/ ] && \ dir=/sys/devices/soc0/soc.1/2100000.aips-bus/21a8000.i2c/i2c-2/2-0048 state="$(cat $dir/state)" [ "$state" = "locked" ] && { echo "$(cat $dir/vendor) $(cat $dir/product) locked: $(cat $dir/vidmode)" return 1 } } analog_in() { local state="$(v4l2-ctl -d /dev/video$INDEV --get-standard | head -n1 | awk '{print $4}')" case "$state" in 0x000000ff) echo "PAL Analog Input Detected" return 1 ;; 0x0000b000) echo "NTSC Analog Input Detected" return 1 ;; esac } cleanup() { echo "cleanup: killing $PID_BG $PID_BLANK..." [ $PID_BG ] && kill $PID_BG [ $PID_BLANK ] && kill $PID_BLANK exit 0 } # Trap sigint trap cleanup SIGINT SIGTERM # Wait for video input source to be detected # (Otherwise gstreamer can hang or error out) if [ "$INDEV" ]; then src=CVBS [ -e /sys/class/video4linux/video"$INDEV" ] && { src=$(cat /sys/class/video4linux/video"$INDEV"/fsl_slave_property) case "$src" in tda1997x*) src=HDMI [ "$VIDEOSRC" ] || VIDEOSRC=mfw_v4lsrc ;; adv* ) src=CVBS;; * ) echo "Defaulting to CVBS Video in..." esac } [ "$VIDEOSRC" ] || VIDEOSRC=tvsrc echo "Waiting to detect $src input signal..." #echo "uptime: $(awk '{print $1}' /proc/uptime)" input= while [ 1 ]; do case $src in HDMI) input=$(hdmi_in);; CVBS) input=$(analog_in);; esac [ "$input" ] && { #echo "uptime: $(awk '{print $1}' /proc/uptime)" echo "$input" break } sleep 1 done sleep 2 else [ "$VIDEOSRC" ] || VIDEOSRC=videotestsrc fi # Background process to keep fb's unblanked ( while [ 1 ]; do bgfb=$(($OUTFB+1)) echo 0 > /sys/class/graphics/fb$OUTFB/blank [ "$OVERLAY" -a -r /sys/class/graphics/fb${bgfb} ] && { echo 0 > /sys/class/graphics/fb${bgfb}/blank } sleep 1 done ) & PID_BLANK=$! # Background the overlay [ $OVERLAY ] && { echo "Launching pipeline for background display on /dev/video$((OUTFB+16+1))" (gst-launch -e videotestsrc pattern=$OVERLAY ! mfw_v4lsink device=/dev/video$((OUTFB+16+1))) & PID_BG=$! } # Create the Pipeline [ "$PIPELINE" ] || { VIDEOSINK="$VIDEOSINK device=/dev/video$((OUTFB+16))" if [ "$VIDEOSRC" = "videotestsrc" ]; then VIDEOSRC="$VIDEOSRC pattern=$PATTERN" else VIDEOSRC="$VIDEOSRC device=/dev/video$INDEV" fi PIPELINE="$VIDEOSRC ! $VIDEOSINK" } echo "Executing gst-launch $PIPELINE" gst-launch $PIPELINE [ "$UNBLANK" ] || { echo 1 > /sys/class/graphics/fb$OUTFB/blank } cleanup