一、官网地址
GStreamer 文档:GStreamer
GStreamer 入门教程:Basic tutorial 2: GStreamer concepts
二、Linux(Ubuntu或Debian)终端上命令
2.1、下载
apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
2.2、编译
basic-tutorial-1.c 文件地址:Basic tutorial 1: Hello world!
gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`
2.3、运行
./basic-tutorial-1
三、源代码下载安装
3.1、代码地址
下载地址:Index of /src
Gstreamer 所需的插件也在此路径下,根据需要下载。
3.2、解压
1、例如下载
gstreamer-1.14.4.tar.xz
2、进入目录
cd gstreamer-1.14.4
mkdir build
3.3、配置
执行命令配置makefile
../autogen.sh --host=aarch64-linux-gnu --prefix=./you/inttall --disable-static --enable-shared
或者
../configure --host=aarch64-linux-gnu --prefix=./you/inttall --disable-static --enable-shared
在配置执行前可能需要安装其他插件
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install gtk-doc-tools
sudo apt-get install orc-0.4 orc-0.4-dev
sudo apt-get install gstreamer1.0-libav gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly
3.4、编译
make -j$(nproc)
3.5、安装
make install
四、Gstreamer介绍
4.1、核心功能
- 媒体播放:支持多种格式的音视频文件播放。
- 媒体捕获:从摄像头、麦克风等设备捕获音视频流。
- 转码:将一种编码格式转换为另一种(例如,将 MP4 转换为 AVI)。
- 过滤与效果处理:应用各种音频和视频特效,如均衡器、混响、模糊、锐化等。
- 网络流传输:支持 RTSP、RTP、HTTP 等协议进行流媒体的发送和接收。
- 多媒体编辑:剪辑、合并音视频文件。
- 实时通信:支持 WebRTC 等技术实现点对点的音视频通话。
五、C语言 API开发过程
GStreamer 的 C API 开发应用程序,你需要遵循以下基本步骤:
5.1. 初始化 GStreamer
#include <gst/gst.h>
int main(int argc, char *argv[]) {
gst_init(&argc, &argv);
}
5.2. 创建管道(Pipeline)
GStreamer 中的一切都是基于“管道”概念来组织的。你可以创建一个管道,并向其中添加元素(Element)来构建你的多媒体流程。
GstElement *pipeline = gst_parse_launch("playbin uri=https://www.example.com/video.mp4", NULL);
5.3. 设置状态并运行
设置管道的状态为 GST_STATE_PLAYING 来开始播放媒体文件。
gst_element_set_state(pipeline, GST_STATE_PLAYING);
5.4. 处理消息
通过监听总线(Bus)上的消息来处理事件,如错误、结束信号等。
GstBus *bus = gst_element_get_bus(pipeline);
GstMessage *msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
// 根据 msg 类型处理相应的逻辑
5.5. 清理资源
当不再需要使用 GStreamer 资源时,确保释放它们以避免内存泄漏。
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
5.6、示例代码
这里有一个简单的例子,展示了如何使用 GStreamer 播放一个网络视频:
#include <gst/gst.h>
static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) {
GMainLoop *loop = (GMainLoop *)data;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
g_print("End of stream\n");
g_main_loop_quit(loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error(msg, &error, &debug);
g_printerr("Error: %s\n", error->message);
g_error_free(error);
if (debug) {
g_printerr("Debug details: %s\n", debug);
g_free(debug);
}
g_main_loop_quit(loop);
break;
}
default:
break;
}
return TRUE;
}
int main(int argc, char *argv[]) {
GstElement *pipeline;
GstBus *bus;
GMainLoop *loop;
gst_init(&argc, &argv);
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_parse_launch("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);
if (!pipeline) {
g_printerr("Failed to create pipeline.\n");
return -1;
}
bus = gst_element_get_bus(pipeline);
gst_bus_add_watch(bus, bus_call, loop);
gst_element_set_state(pipeline, GST_STATE_PLAYING);
g_main_loop_run(loop);
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
g_main_loop_unref(loop);
return 0;
}
5.7、makefile构建
CC := gcc
GSTREAMER_PREFIX := $(installl)/gstreamer-1.14.4
CFLAGS := -I$(GSTREAMER_PREFIX)/include/gstreamer-1.0
CFLAGS += -I./glib-2.0/include
LDFLAGS := -L$(GSTREAMER_PREFIX)/lib -lgstreamer-1.0
LDFLAGS += -L./glib-2.0/lib -lglib-2.0 -lgobject-2.0
LDFLAGS += -L./lib -lpcre -lffi -lgmodule-2.0
TARGET := gstreamer
SRCS := main.c
OBJS := $(SRCS:.c=.o)
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(TARGET)
.PHONY: all clean
修改makefile 中库(gstreamer glib-2.0 -lpcre -lffi -lgmodule-2.0)对应自己的路径
5.8、设备运行
./gstreamer
如果运行成功,显示器将会播放https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm