video - 如何自动关闭 GStreamer 应用程序?
问题描述
我是 GStreamer 的新手。我按照教程做了一些应用程序,并制作了自己的应用程序来编写视频。但是我的应用程序在成功写入视频并且摄像头仍然处于活动状态(摄像头指示灯仍然亮)后无法自动关闭。我必须做的额外命令是Ctrl + C
关闭。
那么,当完成并关闭我的应用程序时,我如何知道从视频写入器获得的标志或信号输出?
这是我的代码:
#include <gst/gst.h>
#include <stdio.h>
#include <stdlib.h>
//static GMainLoop *loop;
int main(int argc, char *argv[]) {
GstElement *pipeline, *source, *sink, *filesink, *enc;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Create the elements */
source = gst_element_factory_make ("v4l2src", "device=/dev/video0");
sink = gst_element_factory_make ("autovideosink", "sink");
// Elements for write video
filesink = gst_element_factory_make("filesink",NULL);
enc = gst_element_factory_make("x264enc",NULL);
// Set parameters for some elements
g_object_set(G_OBJECT(source), "num-buffers", 300, NULL);
g_object_set(G_OBJECT(filesink), "location", "video-GStreamer-1280x720.mp4", "async", 0, NULL);
/* Create the empty pipeline */
pipeline = gst_pipeline_new ("test-pipeline");
if (!pipeline || !source || !sink) {
g_printerr ("Not all elements could be created.\n");
return -1;
}
/* Build the pipeline */
gst_bin_add_many (GST_BIN (pipeline), source, filesink, enc, sink, NULL);
if (gst_element_link_many(source, enc, filesink, NULL) != TRUE){
g_error("Failed to link save elements!");
gst_object_unref (pipeline);
return -1;
}
/* Start playing */
ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (pipeline);
return -1;
}
/* Wait until error or EOS */
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Parse message */
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
/* Free resources */
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
//g_main_loop_unref(loop);
return 0;
}
解决方案
推荐阅读
- ios - 内部查询解析
- python - 如何将具有外键的另一个表中的多个字段添加到list_display?
- android - 如何在 Flutter 中使用 Native 应用程序 sdk
- c++ - valgrind:尝试调试在 Docker 容器内运行的 C++ 程序时,堆块 lo/hi 大小不匹配
- windows - 从android studio执行后如何停止Git for Windows终端关闭
- properties - 使用 powershell 将结果输出到 LABEL 控件中
- jsf - 动态创建的 p:datatable 中的 RowSelection 始终返回第 0 行
- c - 如何在 C 中初始化双指针?(指针数组)
- android - Flutter - 如何让 SnackBar 不向上推动 FloatingActionButton?
- c# - 基础提供程序在 Open EF6 iis 上失败