ちょっと話題の記事

【小ネタ】Raspberry Pi と USB接続のWebカメラ でRTSPサーバを作って動画を配信する

2019.04.08

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

1 はじめに

こんにちは、AIソリューション部の平内(SIN)です。

Amazon Kinesis Video Streamsを色々試している中で、手軽に利用できるRTSPサーバが欲しいと言うことで、Raspberry Pi で作った記録です。

使用させて頂いたのは、v412rtspserverです。

https://github.com/mpromonet/v4l2rtspserver

作成したRTSPサーバで配信しているようすです。

2 要件

今回、利用した諸元等は以下のとおりです。

(1) 3 Model B+

$ cat /proc/cpuinfo | grep Revision
Revision : a020d3

(2) Raspbianイメージ

昨年11月の最新版(Raspbian GNU/Linux 9 (stretch)) 2018-11-13-raspbian-stretch.img を使用しました。

Linux OSのディストリとバージョン

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.4 (stretch)
Release: 9.4
Codename: stretch

カーネル

$ uname -a
Linux raspberrypi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux

(3) Webカメラ

BUFFALOの320万画素Webカメラ( BSW32KM01HBK)です。

http://buffalo.jp/product/multimedia/web-camera/bsw32km01h/

3 コンパイル等

作業は以下のとおりです。

(1) カメラの認識

カメラが /dev/video0 で認識できていることの確認

$ v4l2-ctl --list-devices
USB Camera: USB Camera (usb-3f980000.usb-1.1.3):
/dev/video0

/etc/modulesbcm2835-v4l2を追加して再起動する

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

i2c-dev
bcm2835-v4l2

(2) 必要モジュールのインストール

$ sudo apt-get install cmake
$ sudo apt-get install subversion
$ sudo apt-get install liblivemedia-dev

(3) コンパイル及び、インストール

$ git clone https://github.com/mpromonet/h264_v4l2_rtspserver.git
$ cd h264_v4l2_rtspserver
$ sudo cmake .
$ sudo make install

コンパイル及び、インストールが完了すると、/usr/local/bin/v4l2rtspserverが利用可能になります。

Install the project...
-- Install configuration: ""
-- Installing: /lib/systemd/system/v4l2rtspserver.service
-- Installing: /usr/local/bin/v4l2rtspserver
-- Installing: /usr/local/share/v4l2rtspserver/index.html
-- Installing: /usr/local/share/v4l2rtspserver/hls.js/dist/hls.light.min.js

4 配信

v4l2rtspserverを実行するだけで、デフォルト値でRTSPサーバとして動作します。(採用されたデフォルト値は、コンソールに表示されます。)

$ sudo v4l2rtspserver
Create V4L2 Source.../dev/video0
driver:uvcvideo capabilities:84200001 mandatory:4000001
/dev/video0 support capture
/dev/video0 support streaming
/dev/video0:YUYV size:640x480
Cannot set pixelformat to:H264 format is:YUYV
/dev/video0:MJPG size:640x480 bufferSize:614400
fps:1/30
nbBuffer:0
Device /dev/video0 nb buffer:10
Create Source .../dev/video0
begin thread

デフォルトで起動した場合、rtsp://<RaspberryPiのアドレス>:8554/unicastでアクセス出来ます。(デフォルトでポート8554、パスは/unicastとなっています)

パラメータでデフォルト値以外の指定も可能です。

./v4l2rtspserver [-v[v]] [-Q queueSize] [-O file] \
[-I interface] [-P RTSP port] [-p RTSP/HTTP port] [-m multicast url] [-u unicast url] [-M multicast addr] [-c] [-t timeout] \
[-r] [-s] [-W width] [-H height] [-F fps] [device1] [device2]
-v : verbose
-vv : very verbose
-Q length: Number of frame queue (default 10)
-O output: Copy captured frame to a file or a V4L2 device

RTSP options :
-I addr : RTSP interface (default autodetect)
-P port : RTSP port (defa-ult 8554)
-p port : RTSP over HTTP port (default 0)
-U user:password : RTSP user and password
-R realm : use md5 password 'md5(::')
-u url : unicast url (default unicast)
-m url : multicast url (default multicast)
-M addr : multicast group:port (default is random_address:20000)
-c : don't repeat config (default repeat config before IDR frame)
-t secs : RTCP expiration timeout (default 65)
-S[secs] : HTTP segment duration (enable HLS & MPEG-DASH)

V4L2 options :
-r : V4L2 capture using read interface (default use memory mapped buffers)
-w : V4L2 capture using write interface (default use memory mapped buffers)
-s : V4L2 capture using live555 mainloop (default use a separated reading thread)
-f : V4L2 capture using current capture format (-W,-H are ignored)
-fformat : V4L2 capture using format (-W,-H are used)
-W width : V4L2 capture width (default 640)
-H height: V4L2 capture height (default 480)
-F fps : V4L2 capture framerate (default 25, 0 disable setting framerate)
-G x[x] : V4L2 capture format (default 0x0x25)

ALSA options :
-A freq : ALSA capture frequency and channel (default 44100)
-C channels: ALSA capture channels (default 2)
-a fmt : ALSA capture audio format (default S16_LE)

device : V4L2 capture device and/or ALSA device (default /dev/video0)

5 自動起動

Raspberry Pi を起動しただけで、自動的に配信が開始するように、/etc/rc.local/usr/local/bin/v4l2rtspserver を書いておきます。

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

/usr/local/bin/v4l2rtspserver -W 1280 -H 960 -F 7

exit 0

6 最後に

v412rtspserverで、超簡単にRTSPサーバが作れます。これで、Kinesis Video Streamの動作確認も捗るはずです。