#!/bin/bash
#
#/******************************************************************************
#                                                                              #
#      MJPG-streamer allows to stream JPG frames from an input-plugin          #
#      to several output plugins                                               #
#                                                                              #
#      Copyright (C) 2007 Tom Stoeveken                                        #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation; version 2 of the License.                      #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; if not, write to the Free Software                  #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA    #
#                                                                              #
#******************************************************************************/

## This example shows how to invoke mjpg-streamer from the command line

#export LD_LIBRARY_PATH="$(pwd)"
#./mjpg_streamer -i "input_uvc.so --help"

#./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so -w ./www"

#./mjpg_streamer -i "./input_uvc.so" -o "./output_udp.so -p 2001"

## pwd echos the current path you are working at,
## the backticks open a subshell to execute the command pwd first
## the exported variable name configures ldopen() to search a certain
## folder for *.so modules
#export LD_LIBRARY_PATH=`pwd`

## this is the minimum command line to start mjpg-streamer with webpages
## for the input-plugin default parameters are used
#./mjpg_streamer -o "output_http.so -w `pwd`/www"

## to query help for the core:
# ./mjpg_streamer --help

## to query help for the input-plugin "input_uvc.so":
# ./mjpg_streamer --input "input_uvc.so --help"

## to query help for the output-plugin "output_file.so":
# ./mjpg_streamer --output "output_file.so --help"

## to query help for the output-plugin "output_http.so":
# ./mjpg_streamer --output "output_http.so --help"

## to specify a certain device, framerage and resolution for the input plugin:
# ./mjpg_streamer -i "input_uvc.so -d /dev/video2 -r 320x240 -f 10"

## to start both, the http-output-plugin and write to files every 15 second:
# mkdir pics
# ./mjpg_streamer -o "output_http.so -w `pwd`/www" -o "output_file.so -f pics -d 15000"

## to protect the webserver with a username and password (!! can easily get sniffed and decoded, it is just base64 encoded !!)
# ./mjpg-streamer -o "output_http.so -w ./www -c UsErNaMe:SeCrEt"

## If you want to track down errors, use this simple testpicture plugin as input source.
## to use the testpicture input plugin instead of a webcam or folder:
#./mjpg_streamer -i "input_testpicture.so -r 320x240 -d 500" -o "output_http.so -w www"

## The input_file.so plugin watches a folder for new files, it does not matter where
## the JPEG files orginate from. For instance it is possible to grab the desktop and 
## store the files to a folder:
# mkdir -p /tmp/input
# while true; do xwd -root | convert - -scale 640 /tmp/input/bla.jpg; sleep 0.5; done &
## Then the files can be read from the folder "/tmp/input" and served via HTTP
# ./mjpg_streamer -i "input_file.so -f /tmp/input -r" -o "output_http.so -w www"

## To upload files to a FTP server (edit the script first)
# ./mjpg_streamer -i input_testpicture.so -o "output_file.so --command plugins/output_file/examples/ftp_upload.sh"

## To create a control only interface useful for controlling the pan/tilt throug
## a webpage while another program streams video/audio, like skype.
#./mjpg_streamer -i "./input_control.so" -o "./output_http.so -w ./www"

########################
# Notes: README openSUSE
########################
#
# There have been some changes regarding the paths.
# The executable mjpg_streamer is found at /usr/bin and is within the
# normal user PATH.
#
# Plugins are stored in /usr/lib/mjpg-streamer/ for the i586 architecture
# and in /usr/lib64/mjpg-streamer/ for x86_64. Note that there is a hyphen
# and not an underscore in these pathnames.
#
# All sample HTML files found their place in /usr/share/mjpg-streamer/www/
# where they should not conflict with apache.
#
# This section is written by vodoo (a t) vakw (d o t) ch
#
# -----------------------------------------------------------------------
# Extensions to start.sh to make mstreamer accessible from the desktop
#
# Set to yes|no or say: "mstreamer start -b" to start as daemon
RUNASDAEMON="no"

# Get configuration parameters
if test -r "/etc/sysconfig/mjpg-streamer" ; then
	. /etc/sysconfig/mjpg-streamer
else
	# Set parameters here
	VIDEODEV=""
	#VIDEODEV="/dev/video0"
	IMGSIZE="640x480"
	FRAMERATE="5"
	TCP_PORT="8080"
	UDP_PORT="2001"
fi

if test -n "$VIDEODEV" ; then
	VIDEOOPT="-d $VIDEODEV"
else
	VIDEOOPT=""
fi

##################
# running with GUI
##################

if test "$1" == "gui" ; then
    if ! test -x "/usr/bin/kdialog" ; then
	# We do our best to give a message in the logs
	if test -x "/bin/logger" ; then
	    logger "/usr/bin/mstreamer Error: /usr/bin/kdialog not found"
	fi
	exit 1
    fi

    if ! test -x "/usr/bin/mjpg_streamer" ; then
	kdialog --title "mjpg-streamer" \
	--error "Executable /usr/bin/mjpg_streamer not found"
	exit 1
    fi

    if test -f "/usr/lib/mjpg-streamer/input_uvc.so" ; then
	LIBDIR="/usr/lib"
    elif test -f "/usr/lib64/mjpg-streamer/input_uvc.so" ; then
	LIBDIR="/usr/lib64"
    else
	kdialog --title "mjpg-streamer" \
		--error "plugin input_uvc.so not found;\n aborting"
	exit 1
    fi

    if ! test -f "$LIBDIR/mjpg-streamer/output_http.so" ; then
	kdialog --title "mjpg-streamer" \
		--error "plugin output_http.so not found;\n aborting"
	exit 1
    fi

    if ! test -f "$LIBDIR/mjpg-streamer/output_udp.so" ; then
    	kdialog --title "mjpg-streamer" \
		--error "plugin output_http.so not found;\n aborting"
    	exit 1
    fi

    # get status
    MPID=$(pgrep mjpg_streamer)
    if test -z "$MPID" ; then
	kdialog --title "mjpg-streamer" --yesno \
	"mjpg-streamer is currently not running\n do you want to start it now?"
	RETVAL="$?"
	if test "$RETVAL" == "0" ; then
	    kdialog --title "mjpg-streamer" \
		    --passivepopup "Starting mjpg-streamer, please wait ..." 2
	    export LD_LIBRARY_PATH="$LIBDIR/mjpg-streamer"
	    /usr/bin/mjpg_streamer -b \
		-i "input_uvc.so $VIDEOOPT -f $FRAMERATE -r $IMGSIZE" \
		-o "output_http.so -p $TCP_PORT -w /usr/share/mjpg-streamer/www" \
		-o "output_udp.so -p $UDP_PORT" \
		>/dev/null 2>&1
	    sleep 2
	    MPID=$(pgrep mjpg_streamer)
	    if test -n "$MPID" ; then
		kdialog --title "mjpg-streamer" \
			--msgbox "mjpg-streamer started\n The stream is available at\n http://localhost:$TCP_PORT/"
	    else
		kdialog --title "mjpg-streamer" \
			--error "Starting mjpg-streamer failed."
		exit 1
	    fi
	fi
    else
	kdialog --title "mjpg-streamer" --warningyesno \
	"mjpg-streamer is currently running\n with PID = $MPID\n do you want to stop it?"
	RETVAL="$?"
	if test "$RETVAL" = "0" ; then
	    kdialog --title "mjpg-streamer" \
		    --passivepopup "Shutting down mjpg-streamer, please wait ..." 3
	    pkill -SIGTERM mjpg_streamer
	    sleep 3
	    MPID=$(pgrep mjpg_streamer)
	    if test -z "$MPID" ; then
		kdialog --title "mjpg-streamer" \
			--msgbox "mjpg-streamer stopped"
	    else
		kdialog --title "mjpg-streamer" \
			--error "Stopping mjpg-streamer failed\n still runs with PID=$MPID"
		exit 1
	    fi
	fi
    fi
    exit 0
fi

#################
# run in terminal
#################

# Check arguments
if ! test "$1" == "start" -o "$1" == "stop" -o "$1" == "status"; then
    echo "Usage: mstreamer <start [-b]|stop|status>"
    echo "   -b  starts as daemon in the background"
    exit 0
fi

if test "$1" == "start" -a "$2" == "-b" ; then
    RUNASDAEMON="yes"
fi

# Status
if test "$1" == "status" ; then
    MPID=$(pgrep mjpg_streamer)
    if test -n "$MPID" ; then
	echo "mjpg_streamer is running"
    else
	echo "mjpg_streamer is stopped"
    fi
    exit 0
fi

# Stop running mjpg_streamer
if test "$1" == "stop" ; then
    MPID=$(pgrep mjpg_streamer)
    if test -n "$MPID" ; then
	echo "Shutting down mjpg_streamer"
	pkill -SIGINT mjpg_streamer
	sleep 3
    else
	echo "Can not stop; mjpg_streamer is not running"
	exit 1
    fi
    exit 0
fi

# Start mjpg_streamer
if test "$1" == "start" ; then
    MPID=$(pgrep mjpg_streamer)
    if test -n "$MPID" ; then
	echo "Can not start; mjpg_streamer is already running"
	exit 1
    else
	if ! test -x "/usr/bin/mjpg_streamer" ; then
	    echo "Executable /usr/bin/mjpg_streamer not found"
	    exit 1
	fi

	if test -f "/usr/lib/mjpg-streamer/input_uvc.so" ; then
	    LIBDIR="/usr/lib"
	elif test -f "/usr/lib64/mjpg-streamer/input_uvc.so" ; then
	    LIBDIR="/usr/lib64"
	else
	    echo "Plugin input_uvc.so not found; aborting"
	    exit 1
	fi

	if ! test -f "$LIBDIR/mjpg-streamer/output_http.so" ; then
	    echo "Plugin output_http.so not found; aborting"
	    exit 1
	fi

	if ! test -f "$LIBDIR/mjpg-streamer/output_udp.so" ; then
	    echo "Plugin output_udp.so not found; aborting"
	    exit 1
	fi

	export LD_LIBRARY_PATH="$LIBDIR/mjpg-streamer"
	echo "Info: to see the stream visit http://localhost:$TCP_PORT/"
	if test "$RUNASDAEMON" == "yes" ; then
	    # can use bash to enter daemon mode:
	    # /usr/bin/mjpg_streamer \
	    # -i "input_uvc.so $VIDEOOPT -f $FRAMERATE -r $IMGSIZE" \
	    # -o "output_http.so -w /usr/share/mjpg-streamer/www" \
	    # -o "output_udp.so -p 2001" </dev/null &
	    # sleep 2
	    # disown %1
	    /usr/bin/mjpg_streamer -b \
		-i "input_uvc.so $VIDEOOPT -f $FRAMERATE -r $IMGSIZE" \
		-o "output_http.so -p $TCP_PORT -w /usr/share/mjpg-streamer/www" \
		-o "output_udp.so -p $UDP_PORT"
	    sleep 2
	    MPID=$(pgrep mjpg_streamer)
	    if test -n "$MPID" ; then
		echo "mjpg_streamer started"
		echo "To stop streaming run 'mstreamer stop'"
	    else
		echo "Starting mjpg_streamer failed"
		exit 1
	    fi
	else
	    echo "      Stop streaming with <CTRL> C"
	    /usr/bin/mjpg_streamer \
		-i "input_uvc.so $VIDEOOPT -f $FRAMERATE -r $IMGSIZE" \
		-o "output_http.so -p $TCP_PORT -w /usr/share/mjpg-streamer/www" \
		-o "output_udp.so -p $UDP_PORT"
	fi
    fi
fi
exit 0

# Command to start or stop: mstreamer <start|stop|status>
# Then point your browser at http://localhost:8080/
# and/or take snapshots with MJPG-streamer UDP client.
# This should get you started.
#
# To stop the running mjpg_streamer run 'mstreamer stop'
# All running plugins will be gracefully stopped before
# mjpg_streamer exits.
