1. 概要
Raspberry Pi上でGStreamerを使用したRTSPサーバーを用意し、MJPEGをRTSP配信する。
MJPEGしか出せないUVCカメラでRTSP配信を試そうとしたところVLC Playerで再生できずに困っていると知人に言われ、調べてみた結果をまとめた。
2. 使用機器
- Raspberry Pi 3B
- OSはBuster(Debian 10)
- UVCカメラ: Buffalo BSW500M
- 再生アプリケーション: VLC Player
3. 準備
Gstreamerには、gst-rtsp-serverというRTSPサーバーアプリがあるのでそれを利用する。
Gstreamerのインストールは別の記事でまとめたので割愛する。
インストール済みGStreamerのバージョンを調べる。1.14.4だった。
$ gst-launch-1.0 --version
gst-launch-1.0 version 1.14.4
GStreamer 1.14.4
http://packages.qa.debian.org/gstreamer1.0
autogen.shの実行で「You need gtk-doc to build this package」が出てエラーとなったので、「gtk-doc-tools」がインストールされていなければ予めインストールしておく方が良い。あとからでも問題はない。
$ sudo apt-get install gtk-doc-tools
gst-rtsp-serverをクローンする。バージョンごとにブランチが切られているので、取得したGStreamerのバージョンのブランチをチェックアウトする。
$ git clone git://anongit.freedesktop.org/gstreamer/gst-rtsp-server
$ cd gst-rtsp-server
$ git checkout 1.14.4
$ ./autogen.sh
$ make
$ sudo make install
4. 試す
gst-rtsp-server内のテストアプリのあるディレクトリに移動
$ cd examples/
カラーバーをH.264でRTSP出力
./test-launch '( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )'
VLC Playerのネットワークストリームを開くを選択、「rtsp://192.168.1.128:8554/test」と入力して、再生をクリックする。尚、「192.168.1.128」はRaspberry Pi 3BのIPアドレス。
4.1. YUVをH.264にソフトウェアエンコード
YUVをH.264に変換(ソフトウェアエンコード)してRTSPで配信した。
VLC Playerで再生できたが遅延は大きい。
./test-launch '( v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 )'
ただ、「x264 [error]: baseline profile doesn't support 4:2:2」とエラーが出ていた。
4.2. YUVをH.264にハードウェアエンコード
YUVをH.264に変換(ハードウェアエンコード)してRTSPで配信した。
VLC Playerで再生できた。遅延は少しマシになった。
./test-launch '( v4l2src device=/dev/video0 ! video/x-raw,width=640,height=480,framerate=30/1 ! videoconvert ! omxh264enc ! video/x-h264,profile=baseline ! h264parse ! rtph264pay name=pay0 pt=96 )'
4.3. MJPEGをH.264にハードウェアエンコード
MJPEGをH.264に変換(ハードウェアエンコード)してRTSPで配信した。
VLC Playerで再生できた。遅延はハードウェアエンコードのYUV->H.264と同じくらいか?
./test-launch '( v4l2src device=/dev/video0 ! image/jpeg,width=640,height=480,framerate=30/1 ! omxmjpegdec ! omxh264enc control-rate=1 target-bitrate=5120000 periodicty-idr=10 inline-header=FALSE ! h264parse config-interval=-1 ! video/x-h264,stream-format=byte-stream,alignment=au,profile=baseline ! rtph264pay name=pay0 pt=96 )'
5. まとめ
MJPEGのソースでもH.264にしてRTSP配信できます。
パラメータの組み合わせなどについて話題に上がったら調べて追記するつもり。
以上