天天看點

【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

        前面鋪墊了很多,從顯示,到壓縮,經過一段時間的學習,終于大緻清楚了gstreamer如何擴充應用,終于到現在,可以開始一開始就定好的目标,把USB相機轉成RTSP流的網絡相機。這在很多機器人本體應用還是很有必要,USB相機小巧,便宜,可以安裝很多個,為了降低視訊遠傳後顯示對帶寬的需求,我們肯定是需要将其壓縮,并且裝成RTSP流。

前面我們講了很多基礎的,非基礎的,這裡我們羅列一下:

  • 【GStreamer 】4-gstreamer 中在jeston TX1平台上的硬體編解碼加速介紹_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】3-6-2 gstreamer x264enc 、avdec_h264插件說明_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】3-6 gstreamer jpegenc 、jpegdec 插件說明_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】3-2 gstreamer實作USB相機采集圖檔和錄像_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】3-USB相機的各種顯示,播放常用指令_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】3-1 gstreamer插件之 videotestsrc 介紹_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】2-ubuntu v4l2-ctl 檢視USB 相機基本參數_機器人虎哥的部落格-CSDN部落格
  • 【GStreamer 】1-掃盲介紹_機器人虎哥的部落格-CSDN部落格

        通過這些鋪墊,會對後續工作開展有很多幫助。後續我們的測試和學習過程還是延續了一樣的硬體環境,及TX1核心子產品,USB 720P的相機,就不在重複交代,有需要前面随便翻一篇就有說明。

0. 安裝gstreamer-rtsp-server

git clone  -b 1.8 https://github.com/GStreamer/gst-rtsp-server.git  //下載下傳源碼
cd gst-rtsp-server      
./autogen.sh
sudo make
sudo make install
           
【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

問題1 解決:configure: error: You need to have gtk-doc >= 1.12 installed to build [GStreamer]

【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源
sudo apt install gtk-doc-tools
           

進入示例檢視代碼:

cd /home/nvidia/Downloads/gst-rtsp-server/examples
           

我們主要使用test-launch 這個測試檔案。

【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

1、測試元件test-launch源碼說明

#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
​
 /* 注釋:端口宏定義 */
#define DEFAULT_RTSP_PORT "8554"
​
static char *port = (char *) DEFAULT_RTSP_PORT;
​
static GOptionEntry entries[] = {
  {"port", 'p', 0, G_OPTION_ARG_STRING, &port,
      "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
  {NULL}
};
​
int main (int argc, char *argv[])
{
  GMainLoop *loop;
  GstRTSPServer *server;
  GstRTSPMountPoints *mounts;
  GstRTSPMediaFactory *factory;
  GOptionContext *optctx;
  GError *error = NULL;
​
  optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n"
      "Example: \"( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )\"");
  g_option_context_add_main_entries (optctx, entries, NULL);
  g_option_context_add_group (optctx, gst_init_get_option_group ());
  if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
    g_printerr ("Error parsing options: %s\n", error->message);
    g_option_context_free (optctx);
    g_clear_error (&error);
    return -1;
  }
  g_option_context_free (optctx);
​
  loop = g_main_loop_new (NULL, FALSE);
​
  /* create a server instance */
  server = gst_rtsp_server_new ();
  g_object_set (server, "service", port, NULL);
​
  /* get the mount points for this server, every server has a default object
   * that be used to map uri mount points to media factories */
  mounts = gst_rtsp_server_get_mount_points (server);
​
  /* make a media factory for a test stream. The default media factory can use
   * gst-launch syntax to create pipelines.
   * any launch line works as long as it contains elements named pay%d. Each
   * element with pay%d names will be a stream */
  factory = gst_rtsp_media_factory_new ();
  gst_rtsp_media_factory_set_launch (factory, argv[1]);
​
  /* attach the test factory to the /test url */
  gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
​
  /* don't need the ref to the mapper anymore */
  g_object_unref (mounts);
​
  /* attach the server to the default maincontext */
  gst_rtsp_server_attach (server, NULL);
​
  /* start serving */
  g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
  g_main_loop_run (loop);
​
  return 0;
}
           

編譯:

gcc test-launch.c -o test-launch $(pkg-config --cflags --libs gstreamer-rtsp-server-1.0 gstreamer-1.0)
           

測試:

./test-launch "( videotestsrc ! x264enc ! rtph264pay name=pay0 pt=96 )"
           
【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

播放rtsp流:需要顯示器支援

gst-launch-1.0 playbin uri=rtsp://127.0.0.1:8554/test
           

也可以使用VLC在WIN電腦上播放,隻要在同一個區域網路就可以:

rtsp://192.168.55.1:8554/test
           
【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

2、使用USB相機做為伺服器視訊源

./test-launch --gst-debug-level=3 "( v4l2src device=/dev/video0 ! videoconvert! videoscale ! video/x-raw, width=640, height=480, framerate=25/1 ! queue ! x264enc bitrate=2048 !  rtph264pay name=pay0 pt=96 )"
           

播放rtsp流:

rtsp://192.168.55.1:8554/test
           
【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

這個目前的延時有個10秒左右,有很大的優化空間。

伺服器還會列印很多資訊警告:

【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

同時,CPU占用也比較高

【GStreamer 】5-1 USB相機轉RTSP網絡視訊流 初次接觸0. 安裝gstreamer-rtsp-server1、測試元件test-launch源碼說明2、使用USB相機做為伺服器視訊源

後續我們的主要工作就是分兩點:

(1)詳細了解gstreamer-rtsp-server 中使用的一些控件細節

(2)優化CPU占用,進行硬體加速輔助

繼續閱讀