GStreamer基礎教程13 - 調試Pipeline

摘要

  在不少狀況下,咱們須要對GStreamer建立的Pipeline進行調試,來了解其運行機制以解決所遇到的問題。爲此,GStreamer提供了相應的調試機制,方便咱們快速定位問題。html

 

查看調試日誌

使用GST_DEBUG環境變量查看日誌

  GStreamer框架以及其插件提供了不一樣級別的日誌信息,日誌中包含時間戳,進程ID,線程ID,類型,源碼行數,函數名,Element信息以及相應的日誌消息。例如:ios

$ GST_DEBUG=2 gst-launch-1.0 playbin uri=file:///x.mp3
Setting pipeline to PAUSED ...
0:00:00.014898047 47333      0x2159d80 WARN                 filesrc gstfilesrc.c:530:gst_file_src_start:<source> error: No such file "/x.mp3"
...

  咱們能夠發現,只須要在運行時指定GST_DEBUG環境變量,並設置日誌級別,便可獲得相應的日誌。因爲GStreamer提供了豐富的日誌,若是咱們打開全部的日誌,一定會對程序的性能有所影響,因此咱們須要對日誌進行分級,GStreamer提供了8種級別,用於輸出不一樣類型的日誌。web

  • 級別0:不輸出任何日誌信息。
  • 級別1:ERROR信息。
  • 級別2:WARNING信息。
  • 級別3:FIXME信息。
  • 級別4:INFO信息。
  • 級別5:DEBUG信息
  • 級別6:LOG信息。
  • 級別7:TRACE信息。
  • 級別8:MEMDUMP信息,最高級別日誌。

  在使用時,咱們只需將GST_DEBUG設置爲相應級別,全部小於其級別的信息都會被輸出,例如:設置GST_DEBUG=2,咱們會獲得ERROR及WARNING級別的日誌。
  上面的例子中,全部模塊使用同一日誌級別,除此以外,咱們還能夠針對某個插件設定其獨有的日誌級別,例如:GST_DEBUG=2,audiotestsrc:6 只會將audiotestsrc的日誌級別設置爲6,其餘的模塊仍然使用級別2。
  這樣,GST_DEBUG的值是以逗號分隔的」模塊名:級別「的鍵值對,能夠在最開始增長其餘未指定模塊的默認日誌級別,多個模塊名可使用逗號隔開。同時,GST_DEBUG的值還支持」*「通配符。
  例如:GST_DEBUG=2,audio*:6會將模塊名以audio開始的模塊的日誌級別設置爲6,其餘的默認爲2。
  一樣,GST_DEBUG=*:2 會匹配全部的模塊,與GST_DEBUG=2等同。
  咱們能夠經過gst-launch-1.0 --gst-debug-help 列出當前所註冊的模塊名,模塊名由插件註冊。在安裝的插件改變時,此命令輸出結果也會變化。網絡

 

使用GST_DEBUG_FILE將日誌輸出到文件

  在實際中,咱們一般將日誌保存在文件中,便於後續分析。咱們可使用GST_DEBUG_FILE環境變量,指定日誌文件名,GStreamer會自動將日誌寫入文件中,因爲GStreamer日誌包含終端色彩代碼,咱們一般使用 GST_DEBUG_NO_COLOR=1 環境變量將其禁用,方便查看。使用方式以下:框架

$ GST_DEBUG_NO_COLOR=1 GST_DEBUG_FILE=pipeline.log GST_DEBUG=5 gst-launch-1.0 audiotestsrc ! autoaudiosink

 

使用自定義日誌接口

  在實際項目中,不一樣應用可能採用不一樣的日誌接口,爲此,GStreamer提供了相應的接口,應用程序能夠在初始化時,經過gst_debug_add_log_function()增長自定義日誌接口。相關接口以下:ide

//Add customized log function to GStreamer log system.
void gst_debug_add_log_function (GstLogFunction func,
                            gpointer user_data,
                            GDestroyNotify notify);

// Function prototype for a logging function that can be registered with
// gst_debug_add_log_function()
// Use G_GNUC_NO_INSTRUMENT on that function.
typedef void (*GstLogFunction) (GstDebugCategory * category,
                   GstDebugLevel level,
                   const gchar * file,
                   const gchar * function,
                   gint line,
                   GObject * object,
                   GstDebugMessage * message,
                   gpointer user_data);

// Enable log if set to true.
void gst_debug_set_active (gboolean active);
// Set the default log level.
void gst_debug_set_default_threshold (GstDebugLevel level);

  示例代碼以下:函數

#include <gst/gst.h>
#include <stdio.h>

/* declare log function with the required attribute */
void my_log_func(GstDebugCategory * category,
                 GstDebugLevel level,
                 const gchar * file,
                 const gchar * function,
                 gint line,
                 GObject * object,
                 GstDebugMessage * message,
                 gpointer user_data) G_GNUC_NO_INSTRUMENT;

void my_log_func(GstDebugCategory * category,
                 GstDebugLevel level,
                 const gchar * file,
                 const gchar * function,
                 gint line,
                 GObject * object,
                 GstDebugMessage * message,
                 gpointer user_data) {

    printf("MyLogFunc: [Level:%d] %s:%s:%d  %s\n",
            level, file, function, line,
            gst_debug_message_get(message));

}

int main(int argc, char *argv[]) {
  GstPipeline *pipeline = NULL;
  GMainLoop *main_loop = NULL;

  /* set log function and remove the default one */
  gst_debug_add_log_function(my_log_func, NULL, NULL);
  gst_debug_set_active(TRUE);
  gst_debug_set_default_threshold(GST_LEVEL_INFO);

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* default log function is added by gst_init, so we need remove it after that. */
  gst_debug_remove_log_function(gst_debug_log_default);


  pipeline = (GstPipeline *)gst_parse_launch("audiotestsrc ! autoaudiosink", NULL);

  /* Start playing */
  gst_element_set_state (GST_ELEMENT(pipeline), GST_STATE_PLAYING);

  main_loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (main_loop);

  /* Free resources */
  g_main_loop_unref (main_loop);
  gst_element_set_state (GST_ELEMENT(pipeline), GST_STATE_NULL);
  gst_object_unref (pipeline);

  return 0;
}

 

  編譯運行後,會獲得指定函數輸出的log。工具

$ gcc basic-tutorial-13a.c -o basic-tutorial-13a `pkg-config --cflags --libs gstreamer-1.0`

 

使用GStreamer日誌系統

  若是不想使用自定義接口,咱們一樣可使用Gstreamer提供的日誌系統來由Gstreamer框架統一管理日誌。
  使用GStreamer的日誌系統時,咱們須要首先定義咱們的category,並定義GST_CAT_DEFAULT 宏爲咱們的category:oop

GST_DEBUG_CATEGORY_STATIC (my_category);
#define GST_CAT_DEFAULT my_category

  而後在gst_init後初始化咱們的category:性能

GST_DEBUG_CATEGORY_INIT (my_category, "my category", 0, "This is my very own");

  最後使用GST_ERROR(), GST_WARNING(), GST_INFO(), GST_LOG() 或GST_DEBUG() 宏輸出日誌,這些宏所接受的參數類型與printf相同。
  示例代碼以下:

#include <gst/gst.h>
#include <stdio.h>

GST_DEBUG_CATEGORY_STATIC (my_category);
#define GST_CAT_DEFAULT my_category

int main(int argc, char *argv[]) {
  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  GST_DEBUG_CATEGORY_INIT (my_category, "my category", 0, "This is my very own");

  GST_ERROR("My msg: %d", 0);
  GST_WARNING("My msg: %d", 1);
  GST_INFO("My msg: %d", 2);
  GST_DEBUG("My msg: %d", 3);

  return 0;
}

  編譯後,設置相應的log等級便可看到咱們所添加的log。

$ gcc basic-tutorial-13b.c -o basic-tutorial-13b `pkg-config --cflags --libs gstreamer-1.0`
$ GST_DEBUG=5 ./basic-tutorial-13b
...
0:00:00.135957434  6189      0x21c4600 ERROR            my category basic-tutorial-13b.c:13:main: My msg: 0
0:00:00.135967528  6189      0x21c4600 WARN             my category basic-tutorial-13b.c:14:main: My msg: 1
0:00:00.135976899  6189      0x21c4600 INFO             my category basic-tutorial-13b.c:15:main: My msg: 2
0:00:00.135985622  6189      0x21c4600 DEBUG            my category basic-tutorial-13b.c:16:main: My msg: 3

 

獲取Pipeline運行時的Element關係圖

  在Pipeline變得很複雜時,咱們須要知道Pipeline是否按預期運行、使用到哪些Element,尤爲是使用playbin 或uridecodebin時。爲此,GStreamer提供了相應的功能,可以將Pipeline在當前狀態下全部的Elements及其關係輸出成dot文件,再經過 Graphviz等工具能夠將其轉換成圖片文件。
  爲了獲得.dot文件,咱們只需經過GST_DEBUG_DUMP_DOT_DIR 環境變量,指定輸出目錄便可,gst-launch-1.0會在各狀態分別生成一個.dot文件。 例如:經過下列命令,咱們能夠獲得使用playbin播放網絡文件時生成的Pipeline:

$ GST_DEBUG_DUMP_DOT_DIR=. gst-launch-1.0 playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm
$ ls *.dot
0.00.00.013715494-gst-launch.NULL_READY.dot    
0.00.00.170999259-gst-launch.PAUSED_PLAYING.dot  
0.00.07.642049256-gst-launch.PAUSED_READY.dot
0.00.00.162033239-gst-launch.READY_PAUSED.dot  
0.00.07.606477348-gst-launch.PLAYING_PAUSED.dot

$ dot 0.00.00.170999259-gst-launch.PAUSED_PLAYING.dot -Tpng -o play.png

 生成的play.png以下(結果會根據安裝的插件不一樣而不一樣):

 

須要注意的是,若是須要在本身的應用中加入此功能,那就須要在想要生成dot文件的時候顯式地在相應事件發生時調用GST_DEBUG_BIN_TO_DOT_FILE() 或GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS(),不然即便設置了GST_DEBUG_DUMP_DOT_DIR 環境變量也沒法生成dot文件。

 

總結

經過本文,咱們學習了:

  • 如何經過GST_DEBUG環境變量獲取GStreamer詳細的日誌信息。
  • 如何使用自定義GStreamer的日誌輸出函數。
  • 如何使用GStreamer日誌系統。
  • 如何得到GStreamer運行時的Element關係圖。

 

 

做者: John.Leng
本文版權歸做者全部,歡迎轉載。商業轉載請聯繫做者得到受權,非商業轉載請在文章頁面明顯位置給出原文鏈接.
相關文章
相關標籤/搜索