写一个deepstream c++版本推流代码
时间: 2023-03-19 16:24:19 浏览: 109
以下是一个基本的 DeepStream C 版本推流代码,使用 NVIDIA NVENC 编码器和 RTSP 协议。
```
#include <gst/gst.h>
#include <gst/rtsp-server/rtsp-server.h>
#include <cuda_runtime_api.h>
#include <nvdsgstutils.h>
#include <nvcuviddec.h>
#include <nvbufsurface.h>
#include <nvbufsurftransform.h>
#include <nvdsinfer.h>
#include <nvdsinfer_context.h>
#include <nvdsinfer_custom_impl.h>
#include <nvdsinfer_tensorrt.h>
#define DEFAULT_BITRATE 4000000
#define DEFAULT_FRAMERATE 30
#define DEFAULT_WIDTH 1920
#define DEFAULT_HEIGHT 1080
#define DEFAULT_FORMAT "I420"
#define DEFAULT_CODEC "h264"
#define DEFAULT_PORT "8554"
typedef struct {
GstElement *pipeline;
GstElement *source;
GstElement *streammux;
GstElement *nvinfer;
GstElement *nvvidconv;
GstElement *nvenc;
GstElement *rtppay;
GstElement *sink;
} AppCtx;
static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
{
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
g_print("End of stream\n");
g_main_loop_quit((GMainLoop *)data);
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);
g_free(debug);
g_main_loop_quit((GMainLoop *)data);
break;
}
default:
break;
}
return TRUE;
}
static GstPadProbeReturn cb_have_data(GstPad *pad, GstPadProbeInfo *info, gpointer user_data)
{
GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER(info);
AppCtx *appctx = (AppCtx *)user_data;
GstMapInfo map;
gst_buffer_map(buf, &map, GST_MAP_READ);
CUdeviceptr in_device_ptr = 0;
CUdeviceptr out_device_ptr = 0;
NvBufSurface *surface = NULL;
NvBufSurfaceCreateParams create_params = {0};
NvBufSurfaceTransformParams transform_params = {0};
NvBufSurfaceParams surface_params = {0};
NvBufSurfaceMapParams map_params = {0};
NvDsBatchMeta *batch_meta = NULL;
create_params.gpuId = 0;
create_params.width = appctx->source_width;
create_params.height = appctx->source_height;
create_params.size = appctx->source_width * appctx->source_height * 3 / 2;
create_params.colorFormat = appctx->source_format == NvBufSurfaceColorFormat_I420 ? NvBufSurfaceColorFormat_I420 : NvBufSurfaceColorFormat_RGBA;
create_params.layout = NvBufSurfaceLayout_Pitch;
if (NvBufSurfaceCreate(&surface, 1, &create_params) != 0) {
g_print("Error: failed to create surface\n");
goto done;
}
surface_params.src_rect.width = appctx->source_width;
surface_params.src_rect.height = appctx->source_height;
surface_params.src_rect.top = 0;
surface_params.src_rect.left = 0;
surface_params.dst_rect.width = appctx->stream_width;
surface_params.dst_rect.height