这是老版本的代码 我需要1.22.3的代码
时间: 2024-03-10 17:45:51 浏览: 100
抱歉,上一个答案给出的示例代码是使用 GStreamer 1.0 C++ API 实现 RTSP 拉流的通用示例代码,可以在较新的 GStreamer 版本中使用。以下是使用 GStreamer 1.22.3 版本的 C++ 代码示例:
```cpp
#include <iostream>
#include <gst/gst.h>
int main(int argc, char *argv[]) {
GstElement *pipeline, *source, *sink;
GstBus *bus;
GstMessage *msg;
GstStateChangeReturn ret;
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Create the elements */
source = gst_element_factory_make("rtspsrc", "source");
sink = gst_element_factory_make("autovideosink", "sink");
/* Create the empty pipeline */
pipeline = gst_pipeline_new("test-pipeline");
if (!pipeline || !source || !sink) {
std::cerr << "Not all elements could be created." << std::endl;
return -1;
}
/* Build the pipeline */
gst_bin_add_many(GST_BIN(pipeline), source, sink, nullptr);
if (gst_element_link(source, sink) != TRUE) {
std::cerr << "Elements could not be linked." << std::endl;
gst_object_unref(pipeline);
return -1;
}
/* Set the URI to play */
g_object_set(source, "location", "rtsp://example.com/stream", nullptr);
/* Start playing */
ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
std::cerr << "Failed to start playback." << std::endl;
gst_object_unref(pipeline);
return -1;
}
/* Wait until error or EOS */
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
/* Parse message */
if (msg != nullptr) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &err, &debug_info);
std::cerr << "Error received from element " << GST_OBJECT_NAME(msg->src) << ": " << err->message << std::endl;
std::cerr << "Debugging information: " << (debug_info ? debug_info : "none") << std::endl;
g_clear_error(&err);
g_free(debug_info);
break;
case GST_MESSAGE_EOS:
std::cout << "End-Of-Stream reached." << std::endl;
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
std::cerr << "Unexpected message received." << std::endl;
break;
}
gst_message_unref(msg);
}
/* Free resources */
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}
```
这个示例代码与之前的示例代码类似,只是使用了 C++11 的新特性,例如 nullptr 和 std::cout/std::cerr,但是与使用 GStreamer 1.0 API 的示例代码的逻辑相同。如果需要使用其他 GStreamer 插件或更高级的功能,请参考 GStreamer 官方文档。
阅读全文