AnsweredAssumed Answered

imx6dl中使用gstreamer录像疑问

Question asked by kun zhang on Dec 18, 2018
Latest reply on Dec 25, 2018 by kun zhang

项目需求:

开机后板子进入预览状态,当用户点击录像按钮,开始录像,点解停止,录像结束后进入预览页面,下面是我的代码:

#include <gst/gst.h>
#include <glib.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>

typedef struct Recorder {
GstElement *my_pipeline;
GstElement *camera_src;
GstElement *tee;
GstElement *queue0;
GstElement *queue1;
GstElement *encoder;
GstElement *mux;
GstElement *f_sink;
GstElement *x_sink;
} Recorder_t;

void R_init(Recorder_t *recorder)
{
assert(recorder);

recorder->camera_src = gst_element_factory_make ("imxv4l2src","camera-source");
recorder->tee = gst_element_factory_make ("tee","stream-tee");
recorder->queue0 = gst_element_factory_make ("queue","video-queue");
recorder->queue1 = gst_element_factory_make ("queue","record-queue");
recorder->encoder = gst_element_factory_make ("vpuenc_h264","video-encoder");
recorder->mux = gst_element_factory_make ("matroskamux","video_mux");
recorder->f_sink = gst_element_factory_make ("filesink","record-sink");
recorder->x_sink = gst_element_factory_make ("overlaysink","video-sink");

if (!recorder->camera_src || !recorder->tee || !recorder->queue0 || !recorder->queue1 || !recorder->encoder \
|| !recorder->mux || !recorder->f_sink || !recorder->x_sink){
g_printerr("One element could not be created!");
return;
}

recorder->my_pipeline = gst_pipeline_new ("stream-pipeline");
gst_bin_add_many(GST_BIN (recorder->my_pipeline), recorder->camera_src, recorder->tee, recorder->queue0, \
recorder->queue1, recorder->encoder, recorder->mux, recorder->f_sink, recorder->x_sink, NULL);

gst_element_link(recorder->camera_src, recorder->tee);
gst_element_link_many(recorder->tee, recorder->queue0, recorder->x_sink, NULL);
gst_element_link_many(recorder->tee, recorder->queue1, recorder->encoder, recorder->mux, recorder->f_sink, NULL);
}

void destroy(Recorder_t *recorder)
{
assert(recorder);

if(recorder->my_pipeline)
{
gst_element_set_state(recorder->my_pipeline, GST_STATE_NULL);
}
}

void preview(Recorder_t *recorder)
{
assert(recorder);

if(recorder->my_pipeline)
{
gst_element_set_state(recorder->my_pipeline, GST_STATE_NULL);
}

gst_element_unlink(recorder->my_pipeline, recorder->queue1);
gst_element_unlink(recorder->my_pipeline, recorder->encoder);
gst_element_unlink(recorder->my_pipeline, recorder->mux);
gst_element_unlink(recorder->my_pipeline, recorder->f_sink);

gst_bin_remove_many(GST_BIN(recorder->my_pipeline), recorder->queue1, recorder->encoder, recorder->mux, recorder->f_sink, NULL);

gst_element_set_state(recorder->my_pipeline, GST_STATE_PLAYING);
}

void record(Recorder_t *recorder)
{
assert(recorder);

if(recorder->my_pipeline)
{
gst_element_set_state(recorder->my_pipeline, GST_STATE_NULL);
}

g_object_set(G_OBJECT(recorder->f_sink), "location", "rec.mp4", NULL);

gst_bin_add_many(GST_BIN(recorder->my_pipeline), recorder->queue1, recorder->encoder, recorder->mux, recorder->f_sink, NULL);

gst_element_link_many(recorder->tee, recorder->queue1, recorder->encoder, recorder->mux, recorder->f_sink, NULL);

gst_element_set_state(recorder->my_pipeline, GST_STATE_PLAYING);
}

int main (int argc, char **argv)
{
Recorder_t my_recorder;

gst_init(&argc, &argv);

R_init(&my_recorder);
/*
preview(&my_recorder);

sleep(10);

record(&my_recorder);

sleep(10);

preview(&my_recorder);
*/
record(&my_recorder);

sleep(10);

preview(&my_recorder);

while(1);

return 0;
}

 

单独测试预览或者录像接口是可以的,但是组合起来就不行了,画面会卡死,请问是哪里的问题呢?

Outcomes