添加g_unix_signal_add后不调用中断句柄

Interrupt handle is not called after added with g_unix_signal_add

我正在学习 gstreamer,我有一个简单的程序,我想在 Linux 中按 CTRL+C 后优雅地结束流。为此,我阅读了 gst-launch 的源代码,发现 g_unix_signal_add() 用于添加信号。在我的代码中,我添加了以下行:

signal_watch_intr_id = g_unix_signal_add (SIGINT,
                       (GSourceFunc) intr_handler, data.pipeline);

其中数据是包含管道的结构。我的处理函数是:

static gboolean
intr_handler (gpointer user_data)
{
  printf("handling interrupt.\n");
  GstElement *pipeline = (GstElement *) user_data;


  /* post an application specific message */
  gst_element_post_message (GST_ELEMENT (pipeline),
      gst_message_new_application (GST_OBJECT (pipeline),
          gst_structure_new ("GstLaunchInterrupt",
              "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));

  /* remove signal handler */
  signal_watch_intr_id = 0;
  return G_SOURCE_REMOVE;
}

我希望它将 "handling interrupt." 打印到控制台,使管道发送一个 GST_MESSAGE_APPLICATION 类型的消息,然后将处理该消息以停止管道。但是,应用程序现在在 SIGINT 之后什么都不做。因为它什么都不做,所以我知道它会更改默认句柄,但我不明白为什么它不调用处理函数。有人可以帮我做这个处理吗?

正如 Philip 的评论所暗示的那样,问题是我不是 运行 主循环并使用 gst_bus_timed_pop() 从总线获取消息。通过使用 GMainLoop 并添加一个带有总线回调函数的总线观察器(使用 gst_bus_add_watch),我设法捕获了我想要的中断。