网上得到一个nvidia encoder 的代码片断,自己把它补全. 可以测试网卡编码的个数限制.

 cat main.cpp
#include <stdio.h>
#include <cuda.h>
#include "cuda_runtime.h"
#include "nvEncodeAPI.h"
//#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
int main()
{
	cuInit(0);

	//获取cuDevice
	CUdevice cuDevice = 0;
	cuDeviceGet(&cuDevice, 0);

	//创建cuContext
	CUcontext cuContext = NULL;
	cuCtxCreate(&cuContext, CU_CTX_SCHED_BLOCKING_SYNC, cuDevice);

	//准备编码会话参数
	NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encodeSessionExParams = { NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER };
	encodeSessionExParams.device     = cuContext;
	encodeSessionExParams.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
	encodeSessionExParams.apiVersion = NVENCAPI_VERSION;

	//创建nvenc示例
	NV_ENCODE_API_FUNCTION_LIST nvenc = { NV_ENCODE_API_FUNCTION_LIST_VER };
	NVENCSTATUS nvStatus = NvEncodeAPICreateInstance(&nvenc);

	int loop=0;
	void *hEncoder;
	while(1)
	{
		hEncoder = NULL;
		nvStatus = nvenc.nvEncOpenEncodeSessionEx(&encodeSessionExParams, &hEncoder);
		if(nvStatus != NV_ENC_SUCCESS) break;
		printf("OpenEncodeSesionEx,loop:%d,status %d \n",++loop,nvStatus);
	}
	printf("OpenEncodeSesionEx,loop:%d,status %d, exit... \n",++loop,nvStatus);

	return 0;
}

代码挺难编译通过的,把工程也放上来吧.

TEMPLATE = app
TARGET = nvidia_enc
INCLUDEPATH += .
INCLUDEPATH += /usr/local/cuda/include /usr/local/include/ffnvcodec
# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

# Input
SOURCES += main.cpp
LIBS+= -lnvidia-encode -lcuda

需要安装cuda库及nvidia 卡驱动以及ffnvcodec, 只要折腾,总能搞过, 先在此留个纪念!

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐