【GStreamer】基于gst和gtk的简单videoplayer
在ubuntu上做了一个简单的video player的demo,参考了很多其他人的代码,修修改改调试了一下,能够从web端或者本地读取视频,按下play video按钮就可以实现播放/暂停的效果,不过工具栏还没有实现
编译命令
gcc gst_gtk_videoplayer.c -o gst_gtk_videoplayer $(pkg-config --cflags --libs gstreamer-1.0 gstreamer-video-1.0 gtk+-3.0)
终端输出
代码主要使用了gtk的UI设置,以及如何与gst的输出链接起来,gst使用了简单的uridecodebin和glimagesink模块,整体比较简单,放在了download
两个较为重要的地方,一个是按钮的回调,另一个是glimagesink与gtk widget的连接
// 按钮的回调函数
static void on_play_button_clicked(GtkButton *button, gpointer user_data) {
if (pipeline) {
// Get the input from the GtkEntry
const gchar *input = gtk_entry_get_text(GTK_ENTRY(url_entry));
// Convert the input to a URI
gchar *uri = file_path_to_uri(input);
if (!uri) {
g_printerr("Invalid file path or URL.\n");
return;
}
// Set the URI for the uridecodebin element
GstElement *src = gst_bin_get_by_name(GST_BIN(pipeline), "source");
if (src) {
g_object_set(src, "uri", uri, NULL);
gst_object_unref(src);
}
g_free(uri); // Free the URI string
// 点击开始播放,再点击暂停
if (play_status == STREAM_PAUSED) {
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_print("Start playing...\n");
play_status = STREAM_PLAYING;
} else {
gst_element_set_state(pipeline, GST_STATE_PAUSED);
g_print("Paused...\n");
play_status = STREAM_PAUSED;
}
}
}
// 将video_sink和video_widget绑定,解析的视频会输出到gtk window之中
GstElement *video_sink = gst_bin_get_by_interface(GST_BIN(pipeline), GST_TYPE_VIDEO_OVERLAY);
if (video_sink) {
// Ensure the video widget is realized before setting the window handle
gtk_widget_realize(video_widget);
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(video_sink), GDK_WINDOW_XID(gtk_widget_get_window(video_widget)));
gst_object_unref(video_sink);
}