#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gst/gst.h>
#define BOOL _Bool
#define FILE_LEN 100
#define URI_LEN 128
#define PIPELINE_LEN 255
#define URI_HEADER "file://"
#define PLAYBIN_HEADER "playbin uri="
BOOL GetInputParam(int argc, char **argv, char *fileName)
{
if (argc < 2) {
printf("input argc nums error\n");
return FALSE;
}
int len = strlen(argv[1]);
if (len > FILE_LEN) {
printf("the length of input file name is too long to set\n");
return FALSE;
}
memcpy(fileName, argv[1], strlen(argv[1]));
return TRUE;
}
int main(int argc, char *argv[])
{
char fileName[FILE_LEN] = {0};
char uri[URI_LEN] = {0};
char myPipeline[URI_LEN] = {0};
GstElement *pipeline;
GstBus *bus;
GstMessage *msg;
BOOL ret = GetInputParam(argc, argv, fileName);
if (ret != TRUE) {
printf("argc error please check, [eg: ./playbin_test /home/my/Videos/h264_320x480_universe.mp4]\n");
return FALSE;
}
sprintf(uri, "%s%s", URI_HEADER, fileName);
printf("uri: %s\n", uri);
sprintf(myPipeline, "%s%s", PLAYBIN_HEADER, uri);
printf("myPipeline: %s\n", myPipeline);
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Build the pipeline */
pipeline = gst_parse_launch(myPipeline, NULL);
/* Start playing */
gst_element_set_state(pipeline, GST_STATE_PLAYING);
/* 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);
/* Free resources */
if (msg != NULL)
gst_message_unref(msg);
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return TRUE;
}
编译环境:ubuntu 18.04以上
编译命令:
gcc playbin.c -o playbin pkg-config --cflags --libs gstreamer-1.0