gstreamer播放教程一:playbin——获取媒体的流信息、切换流。

以下程序:获取一个媒体文件中流的数目以及流的信息,并且可以切换音轨。

参考:https://blog.youkuaiyun.com/sakulafly/article/list/2

https://gstreamer.freedesktop.org/documentation/tutorials/playback/subtitle-management.html

可以理解为:使用playbin播放媒体,然后从playbin中获取以上信息

#include "pch.h"
#include<string.h>
#include<stdio.h>
#include <gst/gst.h>

typedef struct _CustomData{
	GstElement *playbin;

	gint n_video;
	gint n_audio;
	gint n_text;

	gint current_video;
	gint current_audio;
	gint current_text;

	GMainLoop *main_loop;
}CustomData;

typedef enum {
	GST_PLAY_FLAG_VIDEO = (1 << 0), //we want video output
	GST_PALY_FLAG_AUDIO = (1 << 1),
	GST_PALY_FLAG_TEXT = (1 << 2)
}GstPlayFlags;

static gboolean handle_message(GstBus *bus, GstMessage *msg, CustomData *data);
static gboolean handle_keyboard(GIOChannel *source, GIOCondition cond, CustomData *data);

int main(int argc, char *argv[]) {
	CustomData data;
	GstBus *bus;
	GstStateChangeReturn ret;
	gint flags;
	GIOChannel *io_stdin;

	gst_init(&argc, &argv);

	data.playbin = gst_element_factory_make("playbin", "playbin");
	if (!data.playbin) {
		g_printerr("could not create playbin\n");
		return -1;
	}
	//file:///C:\Users\lenovo\Desktop\testVideo\[PGS][Tintin-004][DTS-AC3][5PGS].mkv
	g_object_set(data.playbin, "uri", "file:///C:/Users/lenovo/Desktop/testVideo/[PGS][Tintin-004][DTS-AC3][5PGS].mkv", NULL);

	g_object_get(data.playbin, "flags", &flags, NULL);
	flags |= GST_PLAY_FLAG_VIDEO | GST_PALY_FLAG_AUDIO;
	flags &= ~GST_PALY_FLAG_TEXT;
	g_object_set(data.playbin, "flags", flags, NULL);

	//connection-speed设置网络的最大连接速度
	g_object_set(data.playbin, "connection-speed", 56, NULL);
	//  我们逐个的设置这些属性,但我们也可以仅调用g_object_set()一次,来设置uri,flags,connect-speed
	
	bus = gst_element_get_bus(data.playbin);
	gst_bus_add_watch(bus, (GstBusFunc)handle_message, &data);

	//这几行连接了一个标准输入(键盘)和一个回调函数。这里使用的机制是GLib的,并非是基于GStreamer的.
#ifdef G_OS_WIN32
	io_stdin = g_io_channel_win32_new_fd(_fileno(stdin));
#else
	io_stdin = g_io_channel_unix_new(fileno(stdin));
#endif
	g_io_add_watch(io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
	

	ret = gst_element_set_state(data.playbin, GST_STATE_PLAYING);
	if (ret == GST_STATE_CHANGE_FAILURE) {
		g_printerr("could not set sate to playing\n");
		gst_object_unref(data.playbin);
		return -1;
	}

	//为了交互,不再手动轮询gstreamer总线,我们创建main_loop,并且使用了g_main_loop_run函数让它运行起来。
	//,直到调用g_main_loop_quit()才被返回
	data.main_loop = g_main_loop_new(NULL,false);
	g_main_loop_run(data.main_loop);

	g_main_loop_unref(data.main_loop);
	g_io_channel_unref(io_stdin);
	gst_object_unref(bus);
	gst_element_set_state(data.playbin, GST_STATE_NULL);
	g_object_unref(data.playbin);
	return 0;
}

static void analyze_streams(CustomData *data) {
	gint i;
	GstTagList *tags;
	gchar *str;
	guint rate;

	g_object_get(data->playbin, "n-video", &data->n_video, NULL);
	g_object_get(data->playbin, "n-audio", &data->n_audio, NULL);
	g_obj
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值