CubieBoard 上安裝MJPG-Streamer 記錄

CubieBoard 上安裝MJPG-Streamer 記錄

環境準備 (Login as root)

安裝開發工具

apt-get install gcc autoconf make

安裝必要套件

apt-get install subversion libjpeg8-dev imagemagick

安裝其他套件(非必要), such as lsusb

apt-get install usbutils

編譯 MJPG-Streamer

下載MJPG-Streamer souce code

 cd ~
 svn checkout http://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code
 cd ~/mjpg-streamer-code/mjpg-streamer
 make
 #
 # Assume you want to install the mjpg-streamer in /mjpg-streamer directory
 #
 mkdir -p /mjpg-streamer/bin
 mkdir -p /mjpg-streamer/lib
 mkdir -p /mjpg-streamer/www
 make DESTDIR=/mjpg-streamer install

測試

插入支援UVC 的USB webcam, 使用lsusb 查看裝置

root@bsms:~/# lsusb
Bus 004 Device 002: ID 045e:0779 Microsoft Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

其中 Bus 004 Device 002: ID 045e:0779 Microsoft Corp 即是我的USB webcam.

而且在/dev 目錄下, 應該會產生一個video0 的裝置

root@bsms:~/# ls -al /dev/video?
crw-rw---T 1 root video 81, 0 10月 21 13:02 /dev/video0

執行mjpg-streamer

/mjpg-streamer/bin/mjpg_streamer -i "/mjpg-streamer/lib/input_uvc.so" -o "/mjpg-streamer/lib/output_http.so -w /mjpg-streamer/www"

然後使用Browser 連接 http://your_cubieboar_ip:8080 即可看到畫面

詳細執行指令參數可以參考source code 目錄中 start.sh

less ~/mjpg-streamer-code/mjpg-streamer/start.sh

開機自動執行

建立script setting file /etc/default/mjpg-streamer

root@bsms:~/# vi /etc/default/mjpg-streamer

# Configuration for /etc/init.d/mjpg-streamer

# The init.d script will only run if this variable non-empty.
MJPG_STREAMER_USER="root"

# Where mjpg-streamer is installed, this is important!
DAEMON_HOME=/mjpg-streamer
DAEMON_EXEC_DIR=$DAEMON_HOME/bin
DAEMON_LIB_DIR=$DAEMON_HOME/lib
DAEMON_WEB_DIR=$DAEMON_HOME/www

# What arguments to pass to mjpg-streamer, here is few examples; You can change them by commenting one and
# uncommenting another:

DAEMON_I_ARGS="$DAEMON_LIB_DIR/input_uvc.so -r 1280x720 -f 15"   # UVC-mode
#DAEMON_I_ARGS="$DAEMON_LIB_DIR/input_uvc.so -y -r 1280x720 -f 15"  # YUV-mode

DAEMON_O_ARGS="$DAEMON_LIB_DIR/output_http.so -w $DAEMON_WEB_DIR -c your_account:your_passowrd"

# Umask of files mjpg-streamer generates, Change this to 000 if running mjpg-streamer as its own, separate user
UMASK=022

# Should we run at startup?
RUN_AT_STARTUP="YES"

將檔案中的your_account:your_password 改成你想要的帳密

並且修改檔案屬性為600

root@bsms:~/# chmod 600 /etc/default/mjpg-streamer

建立init script file /etc/init.d/mjpg-streamer

root@bsms:~/# vi /etc/init.d/mjpg-streamer

#!/bin/sh
### BEGIN INIT INFO
# Provides:          mjpg-streamer
# Required-Start:    $local_fs networking
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mjpg-streamer daemon
# Description:       Starts the mjpg-streamer daemon with the user specified in
#                    /etc/default/mjpg-streamer.
### END INIT INFO

# Author: Sami Olmari
# Modified: Hao Tseng

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="mjpg-streamer Daemon"
NAME="mjpg-streamer"
DAEMON=mjpg_streamer
PIDFILE=/var/run/${NAME}.pid
PKGNAME=mjpg-streamer
SCRIPTNAME=/etc/init.d/$PKGNAME

# Read configuration variable file if it is present
[ -r /etc/default/$PKGNAME ] && . /etc/default/$PKGNAME

# Exit if the mjpg-streamer is not installed
[ -x "$DAEMON_EXEC_DIR/$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if [ -z "$RUN_AT_STARTUP" -o "$RUN_AT_STARTUP" != "YES" ]; then
   log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it."
   exit 0
fi

if [ -z "$MJPG_STREAMER_USER" ]; then

    log_warning_msg "Not starting $PKGNAME, MJPG_STREAMER_USER not set in /etc/default/$PKGNAME."
    exit 0
fi

#
# Function to verify if a pid is alive
#
is_alive()
{
   pid=`cat $1` > /dev/null 2>&1
   kill -0 $pid > /dev/null 2>&1
   return $?
}

#
# Function that starts the daemon/service
#
do_start()
{
   # Return
   #   0 if daemon has been started
   #   1 if daemon was already running
   #   2 if daemon could not be started

   is_alive $PIDFILE
   RETVAL="$?"

   if [ $RETVAL != 0 ]; then
       start-stop-daemon --start --background --quiet --pidfile $PIDFILE --make-pidfile \
       --exec $DAEMON_EXEC_DIR/$DAEMON --chuid $MJPG_STREAMER_USER --user $MJPG_STREAMER_USER --umask $UMASK -- -i "$DAEMON_I_ARGS" -o "$DAEMON_O_ARGS" 
       RETVAL="$?"
   fi
}

#
# Function that stops the daemon/service
#
do_stop()
{
   # Return
   #   0 if daemon has been stopped
   #   1 if daemon was already stopped
   #   2 if daemon could not be stopped
   #   other if a failure occurred

   start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $MJPG_STREAMER_USER --pidfile $PIDFILE
   RETVAL="$?"
   [ "$RETVAL" = "2" ] && return 2

   rm -f $PIDFILE

   [ "$RETVAL" = "0"  ] && return 0 || return 1
}

case "$1" in
  start)
   [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  stop)
   [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  restart)
   log_daemon_msg "Restarting $DESC" "$NAME"
   do_stop
   case "$?" in
     0|1)
      do_start
      case "$?" in
         0) log_end_msg 0 ;;
         1) log_end_msg 1 ;; # Old process is still running
         *) log_end_msg 1 ;; # Failed to start
      esac
      ;;
     *)
        # Failed to stop
      log_end_msg 1
      ;;
   esac
   ;;
  status)
   status_of_proc -p ${PIDFILE} $DAEMON_EXEC_DIR/$DAEMON $DAEMON && exit 0 || exit $?;
   ;;
  *)
   echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
   exit 3
   ;;
esac

:

修改檔案屬性為755

root@bsms:~/# chmod 755 /etc/init.d/mjpg-streamer

將/etc/init.d/mjpg-streamer 設成開機時執行

root@bsms:~/# update-rc.d mjpg-streamer defaults

使用下列指令測試

root@bsms:~/# /etc/init.d/mjpg-streamer start
[ ok ] Starting mjpg-streamer Daemon: mjpg-streamer.

已知問題

  1. 不知為何每次開機執行時, mjpg-streamer 啟動後, 網頁連的進去, 但是卻看不到畫面. 只要手動關掉deamon 再啟動即可.
  2. 長時間使用後, 有時camera 畫面會出不來, 但是網頁是ok的.

基於以上兩點, 所以我只好用crontab 來定時restart 這個deamon.

root@bsms:~/# crontab -l

*/10 * * * * /etc/init.d/mjpg-streamer restart

每10分鐘自動重起daemon


參考資料:

  1. http://sourceforge.net/projects/mjpg-streamer/
  2. Raspberry PI : Webcam + MJPG-streamer 變 Webcam Streamer
  3. Raspberry Pi 用 Mjpg-Streamer 監看二台 WebCam
  4. How To Install Mjpg-Streamer
  5. MJPG-Streamer init/default scripts by Sami Olmari
  6. Debian 6 如何增加系統啟動程式與指定順序 linux debian
This entry was posted in Linux, MultiMedia and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *