前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux平台x86_64(麒麟|统信UOS)|aarch64(飞腾)如何实现摄像头|屏幕和麦克风|扬声器采集推送RTMP服务或轻量级RTSP服务

Linux平台x86_64(麒麟|统信UOS)|aarch64(飞腾)如何实现摄像头|屏幕和麦克风|扬声器采集推送RTMP服务或轻量级RTSP服务

原创
作者头像
音视频牛哥
发布2024-07-26 12:24:40
480
发布2024-07-26 12:24:40
举报
文章被收录于专栏:RTSP/RTMP直播相关

​技术背景

为什么要在Linux平台麒麟、统信、飞腾平台实现RTMP推送、RTMP|RTSP播放和轻量级RTSP服务?归根结底,还是提升信息安全与自主可控能力,促进经济发展与产业升级。以无纸化会议系统为例,由于无纸化系统涉及大量敏感信息和重要数据的传输与处理,国产化能够确保信息在传输、存储和处理过程中的安全性,降低信息泄露和被外部势力攻击的风险。通过国产化,国内企业能够掌握核心技术和关键部件的研发和生产,避免对国外技术的依赖,提升国家在关键领域的自主可控能力。

前几年,我们发布了Linux平台x86_64架构的RTMP推拉流模块和RTSP模块,随着aarch64架构产品的持续推进,arm架构的音视频技术诉求强烈,可用的方案好多存在短板,为此,我们发布了aach64架构的音视频解决方案。

好多开发者,可能不太了解x86_64和aarch64架构区别,x86_64(通常简称为x64)和aarch64是两种不同的CPU指令集架构,它们各有优缺点,这里做个简单的技术扫盲:

x86_64架构

优点:
  1. 高性能:x64架构支持更大的内存地址空间(最大可达16EB)和更多的处理器寄存器(如16个通用寄存器),这使得它能够处理更多的数据并提升计算性能。在处理大型数据库、复杂应用和高性能计算任务时表现出色。
  2. 广泛的兼容性:x64架构在保持对x86指令集兼容性的同时,增加了新的指令和寄存器。这意味着大多数旧的32位软件(通过兼容层或模拟器)和操作系统可以在x64架构上运行,从而保护了用户的投资。
  3. 丰富的生态系统:由于x64架构的长期流行,市场上存在大量的x64处理器、操作系统、应用软件和开发工具,形成了庞大的生态系统。
缺点:
  1. 成本和功耗:相比于一些其他架构(如aarch64),x64架构的处理器和内存价格可能较高,且功耗也可能较大。这对于成本敏感或能源受限的应用场景可能不是最佳选择。
  2. 对旧软件的依赖:尽管x64架构提供了对旧32位软件的兼容性,但这种兼容性可能需要额外的软件层或模拟器,可能会影响性能并增加复杂性。

aarch64架构

优点:
  1. 低功耗和高能效比:aarch64架构以其低功耗和高能效比而闻名,特别适用于移动设备、嵌入式系统和某些对功耗有严格要求的服务器场景。
  2. 高性能:aarch64通过引入更宽的寄存器和更丰富的指令集,大幅提高了处理器的运算能力。在处理大数据、运行复杂应用和游戏等方面具有显著优势。
  3. 兼容性增强:aarch64不仅支持原生64位应用,还通过向后兼容技术使得32位应用在aarch64平台上也能顺畅运行。
缺点:
  1. 生态系统相对较小:尽管aarch64架构在移动设备和嵌入式系统领域占据重要地位,但与x64架构相比,其生态系统(包括处理器、操作系统、应用软件和开发工具)可能相对较小。
  2. 软件兼容性挑战:尽管aarch64提供了对32位应用的兼容性,但某些特定于平台的软件或驱动可能需要重新编译或优化才能在aarch64架构上运行。

x86_64和aarch64架构各有其独特的优势和局限性。在选择合适的架构时,需要根据具体的应用场景、性能需求、成本预算和生态系统支持等因素进行综合考虑。

技术实现

大牛直播SDK在实现Linux平台x86_64架构和aarch64架构的音视频推拉流模块的时候,并没有太大的技术瓶颈,这块源于我们2015年起,就做了Windows、Android、iOS平台的RTMP|RTSP的音视频推拉流模块,当时设计的时候,就考虑到了跨平台问题。

Linux平台,摄像头采集,可采用V4L2相关接口,如需查看摄像头设备文件,使用(ls -l /dev/|grep video),打开设备使用open接口, 如:open("/dev/videoxx", flag)。

屏幕采集用X相关接口实现,如果是Wayland协议, 用PipeWire相关接口实现采集就好。 麦克风采集使用ALSA或者PulseAudio,采集播放音频用PulseAudio。

废话不多说,上代码,这里分别是启动了轻量级RTSP服务,并实现了RTMP的推送。

代码语言:cpp
复制
/*
 * publisherdemo.cpp
 * Author: daniusdk.com
 * WeChat: xinsheng120
 */
int main(int argc, char *argv[])
{
	struct sigaction act;
	sigemptyset(&act.sa_mask);
	act.sa_sigaction = OnSaSigaction;
	act.sa_flags = SA_SIGINFO;

	sigaction(SIGINT, &act, NULL);
	sigaction(SIGFPE, &act, NULL);

	XInitThreads(); // X支持多线程, 必须调用

	auto display = XOpenDisplay(nullptr);
	if (!display)
	{
		fprintf(stderr, "Cannot connect to X server\n");
		return 0;
	}

	auto screen = DefaultScreen(display);
	auto root = XRootWindow(display, screen);

	XWindowAttributes root_win_att;
	if (!XGetWindowAttributes(display, root, &root_win_att))
	{
		fprintf(stderr, "Get Root window attri failed\n");
		XCloseDisplay(display);
		return 0;
	}

	int main_w = root_win_att.width / 2, main_h = root_win_att.height / 2;

	auto black_pixel = BlackPixel(display, screen);
	auto white_pixel = WhitePixel(display, screen);

	auto main_wid = XCreateSimpleWindow(display, root, 0, 0, main_w, main_h, 0, white_pixel, black_pixel);
	if (!main_wid)
	{
		fprintf(stderr, "Cannot Create Main Window\n");
		XCloseDisplay(display);
		return 0;
	}

	XSelectInput(display, main_wid, StructureNotifyMask | KeyPressMask);

	auto sub_wid = CreateSubWindow(display, screen, main_wid);
	if (!sub_wid)
	{
		fprintf(stderr, "Cannot Create Render Window\n");
		XDestroyWindow(display, main_wid);
		XCloseDisplay(display);
		return 0;
	}

	XMapWindow(display, main_wid);
	XStoreName(display, main_wid, "Video Preview");
	XMapWindow(display, sub_wid);

	LogInit();

	NT_SmartPublisherSDKAPI push_api;
	if (!PushSDKInit(push_api))
	{
		XDestroyWindow(display, sub_wid);
		XDestroyWindow(display, main_wid);
		XCloseDisplay(display);

		return 0;
	}

	// auto rtsp_server_handle = start_rtsp_server(&push_api, 8554, "test", "12345");
	auto rtsp_server_handle = start_rtsp_server(&push_api, 8554, "", "");
	if (nullptr == rtsp_server_handle) {
		fprintf(stderr, "start_rtsp_server failed.\n");

		XDestroyWindow(display, sub_wid);
		XDestroyWindow(display, main_wid);
		XCloseDisplay(display);

		push_api.UnInit();

		return 0;
	}

	auto push_handle = open_config_instance(&push_api, 20);
	if (nullptr == push_handle) {
		fprintf(stderr, "open_config_instance failed.\n");

		XDestroyWindow(display, sub_wid);
		XDestroyWindow(display, main_wid);
		XCloseDisplay(display);

		stop_rtsp_server(&push_api, rtsp_server_handle);

		push_api.UnInit();

		return 0;
	}

	if (!start_rtsp_stream(&push_api, rtsp_server_handle, push_handle, "stream1")) {
		fprintf(stderr, "start_rtsp_stream failed.\n");
		goto _cleanup_;
	}

	if (!start_rtmp(&push_api, push_handle, "rtmp://192.168.0.107:1935/live/test1")) {
		fprintf(stderr, "start_rtmp failed.\n");
		goto _cleanup_;
	}

	// 开启预览,也可以不开启, 根据需求来
	push_api.SetPreviewXWindow(push_handle, "", sub_wid);
	push_api.StartPreview(push_handle, 0, nullptr);

	while (!g_is_exit)
	{
		while (MY_X11_Pending(display, 10))
		{
			XEvent xev;
			memset(&xev, 0, sizeof(xev));
			XNextEvent(display, &xev);

			if (xev.type == ConfigureNotify)
			{
				if (xev.xconfigure.window == main_wid)
				{
					if (xev.xconfigure.width != main_w || xev.xconfigure.height != main_h)
					{
						main_w = xev.xconfigure.width;
						main_h = xev.xconfigure.height;

						XMoveResizeWindow(display, sub_wid, 0, 0, main_w - 4, main_h - 4);
					}
				}
			}
			else if (xev.type == KeyPress)
			{
				if (xev.xkey.keycode == XKeysymToKeycode(display, XK_Escape))
				{
					fprintf(stdout, "ESC Key Press\n");
					g_is_exit = true;
				}
			}

			if (g_is_exit)
				break;
		}
	}

}

其中,PushSDKInit()实现如下:

代码语言:cpp
复制
	/*
     * publisherdemo.cpp
     * Author: daniusdk.com
     */
    bool PushSDKInit(NT_SmartPublisherSDKAPI& push_api)
	{
		memset(&push_api, 0, sizeof(push_api));
		NT_GetSmartPublisherSDKAPI(&push_api);

		auto ret = push_api.Init(0, nullptr);
		if (NT_ERC_OK != ret)
		{
			fprintf(stderr, "push_api.Init failed!\n");
			return false;
		}
		else
		{
			fprintf(stdout, "push_api.Init ok!\n");
		}

		return true;
	}

启动RTSP服务对应的代码如下:

代码语言:cpp
复制
	NT_HANDLE start_rtsp_server(NT_SmartPublisherSDKAPI* push_api, int port, std::string user_name, std::string password) {

		NT_HANDLE rtsp_server_handle = nullptr;
		if (NT_ERC_OK != push_api->OpenRtspServer(&rtsp_server_handle, 0)) {
			fprintf(stderr, "OpenRtspServer failed\n");
			return nullptr;
		}

		if (nullptr == rtsp_server_handle) {
			fprintf(stderr, "rtsp_server_handle is null\n");
			return nullptr;
		}

		if (NT_ERC_OK != push_api->SetRtspServerPort(rtsp_server_handle, port)) {
			push_api->CloseRtspServer(rtsp_server_handle);
			return nullptr;
		}

		if (!user_name.empty() && !password.empty())
			push_api->SetRtspServerUserNamePassword(rtsp_server_handle, user_name.c_str(), password.c_str());

		if (NT_ERC_OK == push_api->StartRtspServer(rtsp_server_handle, 0))
			return rtsp_server_handle;

		fprintf(stderr, "StartRtspServer failed\n");
		push_api->CloseRtspServer(rtsp_server_handle);
		
		return nullptr;
	}

open_config_instance()实现如下,可以获取摄像头或屏幕数据,并做基础的编码等参数配置,看似复杂,实际和Windows平台相差不大:

代码语言:cpp
复制
	NT_HANDLE open_config_instance(NT_SmartPublisherSDKAPI* push_api, int dst_fps) {
		NT_INT32 pulse_device_number = 0;
		if (NT_ERC_OK == push_api->GetAuidoInputDeviceNumber(2, &pulse_device_number))
		{
			fprintf(stdout, "[daniusdk.com]Pulse device num:%d\n", pulse_device_number);
			char device_name[512];

			for (auto i = 0; i < pulse_device_number; ++i)
			{
				if (NT_ERC_OK == push_api->GetAuidoInputDeviceName(2, i, device_name, 512))
				{
					fprintf(stdout, "[daniusdk.com]index:%d name:%s\n", i, device_name);
				}
			}
		}

		NT_INT32 alsa_device_number = 0;
		if (pulse_device_number < 1)
		{
			if (NT_ERC_OK == push_api->GetAuidoInputDeviceNumber(1, &alsa_device_number))
			{
				fprintf(stdout, "Alsa device num:%d\n", alsa_device_number);
				char device_name[512];
				for (auto i = 0; i < alsa_device_number; ++i)
				{
					if (NT_ERC_OK == push_api->GetAuidoInputDeviceName(1, i, device_name, 512))
					{
						fprintf(stdout, "[daniusdk.com]index:%d name:%s\n", i, device_name);
					}
				}
			}
		}

		NT_INT32 capture_speaker_flag = 0;
		if (NT_ERC_OK == push_api->IsCanCaptureSpeaker(2, &capture_speaker_flag))
		{
			if (capture_speaker_flag)
				fprintf(stdout, "[daniusdk.com]Support speaker capture\n");
			else
				fprintf(stdout, "[daniusdk.com]UnSupport speaker capture\n");
		}

		NT_INT32 is_support_window_capture = 0;
		if (NT_ERC_OK == push_api->IsCaptureXWindowSupported(NULL, &is_support_window_capture))
		{
			if (is_support_window_capture)
				fprintf(stdout, "[daniusdk.com]Support window capture\n");
			else
				fprintf(stdout, "[daniusdk.com]UnSupport window capture\n");
		}

		if (is_support_window_capture)
		{
			NT_INT32 win_count = 0;
			if (NT_ERC_OK == push_api->UpdateCaptureXWindowList(NULL, &win_count) && win_count > 0)
			{

				fprintf(stdout, "X Capture Winows list++\n");

				for (auto i = 0; i < win_count; ++i)
				{
					NT_UINT64 wid;
					char title[512];

					if (NT_ERC_OK == push_api->GetCaptureXWindowInfo(i, &wid, title, sizeof(title) / sizeof(char)))
					{
						x_win_list.push_back(wid);
						fprintf(stdout, "wid:%llu, title:%s\n", wid, title);
					}
				}

				fprintf(stdout, "[daniusdk.com]X Capture Winows list--\n");
			}
		}

		std::vector<CameraInfo> cameras;
		GetCameraInfo(push_api, cameras);

		if (!cameras.empty())
		{
			fprintf(stdout, "cameras count:%d\n", (int)cameras.size());

			for (const auto& c : cameras)
			{
				fprintf(stdout, "camera name:%s, id:%s, cap_num:%d\n", c.name_.c_str(), c.id_.c_str(), (int)c.capabilities_.size());

				for (const auto& i : c.capabilities_)
				{
					fprintf(stdout, "[daniusdk.com]cap w:%d, h:%d, fps:%d\n", i.width_, i.height_, i.max_frame_rate_);
				}
			}
		}

		NT_UINT32 auido_option = NT_PB_E_AUDIO_OPTION_NO_AUDIO;

		if (pulse_device_number > 0 || alsa_device_number > 0)
		{
			auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_MIC;
		}
		else if (capture_speaker_flag)
		{
			auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_SPEAKER;
		}

		//auido_option = NT_PB_E_AUDIO_OPTION_CAPTURE_MIC_SPEAKER_MIXER;

		NT_UINT32 video_option = NT_PB_E_VIDEO_OPTION_SCREEN;

		if (!cameras.empty())
		{
			video_option = NT_PB_E_VIDEO_OPTION_CAMERA;
		}
		else if (is_support_window_capture)
		{
			video_option = NT_PB_E_VIDEO_OPTION_WINDOW;
		}

		// video_option = NT_PB_E_VIDEO_OPTION_LAYER;

		//video_option = NT_PB_E_VIDEO_OPTION_NO_VIDEO;

		NT_HANDLE push_handle = nullptr;

		//if (NT_ERC_OK != push_api->Open(&push_handle, NT_PB_E_VIDEO_OPTION_LAYER, NT_PB_E_AUDIO_OPTION_CAPTURE_SPEAKER, 0, NULL))
		if (NT_ERC_OK != push_api->Open(&push_handle, video_option, auido_option, 0, NULL))
		{
			return nullptr;
		}

		push_api->SetEventCallBack(push_handle, nullptr, OnSDKEventHandle);

		//push_api->SetXDisplayName(push_handle, ":0");
		//push_api->SetXDisplayName(push_handle, NULL);

		// 视频层配置方式
		if (NT_PB_E_VIDEO_OPTION_LAYER == video_option)
		{
			std::vector<std::shared_ptr<nt_pb_sdk::layer_conf_wrapper_base> > layer_confs;

			auto index = 0;

			//// 第0层填充RGBA矩形, 目的是保证帧率, 颜色就填充全黑
			auto rgba_layer_c0 = std::make_shared<nt_pb_sdk::RGBARectangleLayerConfigWrapper>(index++, true, 0, 0, 1280, 720);

			rgba_layer_c0->conf_.red_ = 200;
			rgba_layer_c0->conf_.green_ = 200;
			rgba_layer_c0->conf_.blue_ = 200;
			rgba_layer_c0->conf_.alpha_ = 255;

			layer_confs.push_back(rgba_layer_c0);

			////// 第一层为桌面层
			//auto screen_layer_c1 = std::make_shared<nt_pb_sdk::ScreenLayerConfigWrapper>(index++, true, 0, 0, 1280, 720);

			//screen_layer_c1->conf_.scale_filter_mode_ = 3;

			//layer_confs.push_back(screen_layer_c1);

			//// 第一层为窗口
			if (!x_win_list.empty())
			{
				auto window_layer_c1 = std::make_shared<nt_pb_sdk::WindowLayerConfigWrapper>(index++, true, 0, 0, 640, 360);

				window_layer_c1->conf_.xwindow_ = x_win_list.back();

				layer_confs.push_back(window_layer_c1);
			}

			//// 摄像头层
			if (!cameras.empty())
			{
				auto camera_layer_c1 = std::make_shared<nt_pb_sdk::CameraLayerConfigWrapper>(index++, true,
					640, 0, 640, 360);

				strcpy(camera_layer_c1->conf_.device_unique_id_, cameras.front().id_.c_str());

				camera_layer_c1->conf_.is_flip_horizontal_ = 0;
				camera_layer_c1->conf_.is_flip_vertical_ = 0;
				camera_layer_c1->conf_.rotate_degress_ = 0;

				layer_confs.push_back(camera_layer_c1);

				if (cameras.size() > 1)
				{
					auto camera_layer_c2 = std::make_shared<nt_pb_sdk::CameraLayerConfigWrapper>(index++, true,
						640, 0, 320, 240);

					strcpy(camera_layer_c2->conf_.device_unique_id_, cameras.back().id_.c_str());

					camera_layer_c2->conf_.is_flip_horizontal_ = 0;
					camera_layer_c2->conf_.is_flip_vertical_ = 0;
					camera_layer_c2->conf_.rotate_degress_ = 0;

					layer_confs.push_back(camera_layer_c2);
				}
			}

			auto image_layer1 = std::make_shared<nt_pb_sdk::ImageLayerConfigWrapper>(index++, true, 650, 120, 324, 300);

			strcpy(image_layer1->conf_.file_name_utf8_, "./testpng/tca.png");

			layer_confs.push_back(image_layer1);

			auto image_layer2 = std::make_shared<nt_pb_sdk::ImageLayerConfigWrapper>(index++, true, 120, 380, 182, 138);

			strcpy(image_layer2->conf_.file_name_utf8_, "./testpng/t4.png");

			layer_confs.push_back(image_layer2);

			std::vector<const NT_PB_LayerBaseConfig* > layer_base_confs;

			for (const auto& i : layer_confs)
			{
				layer_base_confs.push_back(i->getBase());
			}

			if (NT_ERC_OK != push_api->SetLayersConfig(push_handle, 0, layer_base_confs.data(),
				layer_base_confs.size(), 0, nullptr))
			{
				push_api->Close(push_handle);
				push_handle = nullptr;
				return nullptr;
			}
		}

		// push_api->SetScreenClip(push_handle, 0, 0, 1280, 720);

		if (video_option == NT_PB_E_VIDEO_OPTION_CAMERA)
		{
			if (!cameras.empty())
			{
				push_api->SetVideoCaptureDeviceBaseParameter(push_handle, cameras.front().id_.c_str(),
					640, 480);

				//push_api->FlipVerticalCamera(push_handle, 1);
				//push_api->FlipHorizontalCamera(push_handle, 1);
				//push_api->RotateCamera(push_handle, 0);
			}
		}

		if (video_option == NT_PB_E_VIDEO_OPTION_WINDOW)
		{
			if (!x_win_list.empty())
			{
				//push_api->SetCaptureXWindow(push_handle, x_win_list[0]);
				push_api->SetCaptureXWindow(push_handle, x_win_list.back());
			}
		}

		push_api->SetFrameRate(push_handle, dst_fps); // 帧率设置

		push_api->SetVideoEncoder(push_handle, 0, 1, NT_MEDIA_CODEC_ID_H264, 0);

		push_api->SetVideoBitRate(push_handle, 2000);  // 平均码率2000kbps
		push_api->SetVideoQuality(push_handle, 26);
		push_api->SetVideoMaxBitRate(push_handle, 4000); // 最大码率4000kbps

		// openh264 配置特定参数
		push_api->SetVideoEncoderSpecialInt32Option(push_handle, "usage_type", 0); //0是摄像头编码, 1是屏幕编码
		push_api->SetVideoEncoderSpecialInt32Option(push_handle, "rc_mode", 1); // 0是质量模式, 1是码率模式
		push_api->SetVideoEncoderSpecialInt32Option(push_handle, "enable_frame_skip", 0); // 0是关闭跳帧, 1是打开跳帧

		push_api->SetVideoKeyFrameInterval(push_handle, dst_fps * 2); // 关键帧间隔
		push_api->SetVideoEncoderProfile(push_handle, 3); // H264 high
		push_api->SetVideoEncoderSpeed(push_handle, 3); // 编码速度设置到3

		if (pulse_device_number > 0)
		{
			push_api->SetAudioInputLayer(push_handle, 2);
			push_api->SetAuidoInputDeviceId(push_handle, 0);
		}
		else if (alsa_device_number > 0)
		{
			push_api->SetAudioInputLayer(push_handle, 1);
			push_api->SetAuidoInputDeviceId(push_handle, 0);
		}

		push_api->SetEchoCancellation(push_handle, 1, 0);
		push_api->SetNoiseSuppression(push_handle, 1);
		push_api->SetAGC(push_handle, 1);
		push_api->SetVAD(push_handle, 1);

		push_api->SetInputAudioVolume(push_handle, 0, 1.0);
		push_api->SetInputAudioVolume(push_handle, 1, 0.2);

		// 音频配置
		push_api->SetPublisherAudioCodecType(push_handle, 1);
		//push_api->SetMute(push_handle, 1);

		return push_handle;
	}

其中,push_api->Open(&push_handle, video_option, auido_option, 0, NULL))时,设置音视频采集类型,相关类型如下:

代码语言:cpp
复制
/*
 * nt_smart_publisher_define.h
 * Author: daniusdk.com
 */
/*定义Video源选项*/
typedef enum _NT_PB_E_VIDEO_OPTION
{
	NT_PB_E_VIDEO_OPTION_NO_VIDEO = 0x0,
	NT_PB_E_VIDEO_OPTION_SCREEN   = 0x1, // 采集屏幕
	NT_PB_E_VIDEO_OPTION_CAMERA	  = 0x2, // 摄像头采集
	NT_PB_E_VIDEO_OPTION_LAYER    = 0x3, // 视频合并,比如桌面叠加摄像头等
	NT_PB_E_VIDEO_OPTION_ENCODED_DATA = 0x4, // 已经编码的视频数据,目前支持H264
	NT_PB_E_VIDEO_OPTION_WINDOW   = 0x5, // 采集窗口
} NT_PB_E_VIDEO_OPTION;

/*定义Auido源选项*/
typedef enum _NT_PB_E_AUDIO_OPTION
{
	NT_PB_E_AUDIO_OPTION_NO_AUDIO					= 0x0,
	NT_PB_E_AUDIO_OPTION_CAPTURE_MIC				= 0x1, // 采集麦克风音频
	NT_PB_E_AUDIO_OPTION_CAPTURE_SPEAKER			= 0x2, // 采集扬声器
	NT_PB_E_AUDIO_OPTION_CAPTURE_MIC_SPEAKER_MIXER	= 0x3, // 麦克风扬声器混音
	NT_PB_E_AUDIO_OPTION_ENCODED_DATA				= 0x4, // 编码后的音频数据,目前支持AAC, speex宽带(wideband mode)
	NT_PB_E_AUDIO_OPTION_EXTERNAL_PCM_DATA			= 0x5, /*外部PCM数据*/
	NT_PB_E_AUDIO_OPTION_MIC_EXTERNAL_PCM_MIXER     = 0x6, /* 麦克风和外部PCM数据混音 当前只支持一路外部音频和内置麦克风混音*/
	NT_PB_E_AUDIO_OPTION_TWO_EXTERNAL_PCM_MIXER     = 0x7, /* 两路外部PCM数据混音*/
} NT_PB_E_AUDIO_OPTION;

发布RTSP流实现如下:

代码语言:cpp
复制
	bool start_rtsp_stream(NT_SmartPublisherSDKAPI* push_api, NT_HANDLE rtsp_server_handle, NT_HANDLE handle, const std::string stream_name) {

		push_api->SetRtspStreamName(handle, stream_name.c_str());

		push_api->ClearRtspStreamServer(handle);

		push_api->AddRtspStreamServer(handle, rtsp_server_handle, 0);
		
		if (NT_ERC_OK != push_api->StartRtspStream(handle, 0))
			return false;

		return true;
	}

如需推送RTMP流:

代码语言:cpp
复制
bool start_rtmp(NT_SmartPublisherSDKAPI* push_api, NT_HANDLE handle, const std::string& rtmp_url) {
	if (NT_ERC_OK != push_api->SetURL(handle, rtmp_url.c_str(), NULL))
		return false;

	if (NT_ERC_OK != push_api->StartPublisher(handle, NULL))
		return false;

	return true;
}

如果需要本地摄像头或者屏幕预览数据,调研预览接口即可:

代码语言:cpp
复制
	// 开启预览,也可以不开启, 根据需求来
	push_api.SetPreviewXWindow(push_handle, "", sub_wid);
	push_api.StartPreview(push_handle, 0, nullptr);

如需停止:

代码语言:cpp
复制
	fprintf(stdout, "Skip run loop, is_exit:%d\n", g_is_exit);

	fprintf(stdout, "StopRtspStream++\n");

	push_api.StopRtspStream(push_handle);
	
	fprintf(stdout, "StopRtspStream--\n");

	fprintf(stdout, "stop_rtsp_server++\n");

	stop_rtsp_server(&push_api, rtsp_server_handle);

	fprintf(stdout, "stop_rtsp_server--\n");

	push_api.StopPreview(push_handle);

	push_api.StopPublisher(push_handle);

	push_api.Close(push_handle);

	push_handle = nullptr;

	XDestroyWindow(display, sub_wid);
	XDestroyWindow(display, main_wid);
	XCloseDisplay(display);

	push_api.UnInit();

	fprintf(stdout, "SDK UnInit..\n");

	return 0;

总结

国产化操作系统的发展,减少了外部依赖,更符合国家安全标准,并可提升自主研发能力,促进产业链发展,满足定制开发能力,减少了外部技术封锁的风险,提高了国际竞争力,推动了产业升级。目前大牛直播SDK针对Linux平台x86_64架构和aarch64架构的RTMP推送模块和轻量级RTSP服务模块,主要设计实现的功能如下:

Linux平台x64_64架构|aarch64架构RTMP直播推送模块

  • 音频编码:AAC/SPEEX;
  • 视频编码:H.264;
  • 推流协议:RTMP;
  • [音视频]支持纯音频/纯视频/音视频推送;
  • 支持X11屏幕采集;
  • 支持部分V4L2摄像头设备采集;
  • [屏幕/V4L2摄像头]支持帧率、关键帧间隔(GOP)、码率(bit-rate)设置;
  • [V4L2摄像头]支持V4L2摄像头设备选择(设备文件名范围:[/dev/video0, /dev/video63])、分辨率设置、帧率设置;
  • [V4L2摄像头]支持水平反转、垂直反转、0° 90° 180° 270°旋转;
  • [音频]支持基于alsa-lib接口的音频采集;
  • [音频]支持基于libpulse接口采集本机PulseAudio服务音频;
  • [预览]支持推送端实时预览;
  • [对接服务器]支持自建标准RTMP服务器或CDN;
  • 支持断网自动重连、网络状态回调;
  • 屏幕和摄像头合成/多层合成;
  • 支持窗口采集(一般不建议使用);
  • 支持实时快照;
  • 支持降噪处理、自动增益控制、VAD端点检测;
  • 支持扬声器和麦克风混音;
  • 支持外部编码前音视频数据对接;
  • 支持外部编码后音视频数据对接;
  • 支持实时音量调节;
  • 支持扩展录像模块;
  • 支持Unity接口;
  • 支持H.264扩展SEI发送模块;
  • 支持x64_64架构、aarch64架构(需要glibc-2.21及以上版本的Linux系统, 需要libX11.so.6, 需要GLib–2.0, 需安装 libstdc++.so.6.0.21、GLIBCXX_3.4.21、 CXXABI_1.3.9);

Linux平台x64_64架构|aarch64架构轻量级RTSP服务

  • 音频编码:AAC;
  • 视频编码:H.264;
  • 协议:RTSP;
  • [音视频]支持纯音频/纯视频/音视频推送;
  • 支持X11屏幕采集;
  • 支持部分V4L2摄像头设备采集;
  • [屏幕/V4L2摄像头]支持帧率、关键帧间隔(GOP)、码率(bit-rate)设置;
  • [V4L2摄像头]支持V4L2摄像头设备选择(设备文件名范围:[/dev/video0, /dev/video63])、分辨率设置、帧率设置;
  • [V4L2摄像头]支持水平反转、垂直反转、0° 90° 180° 270°旋转;
  • [音频]支持基于alsa-lib接口的音频采集;
  • [音频]支持基于libpulse接口采集本机PulseAudio服务音频;
  • 支持窗口采集(一般不建议使用);
  • 支持实时快照;
  • 支持降噪处理、自动增益控制、VAD端点检测;
  • 支持扬声器和麦克风混音;
  • 支持外部编码前音视频数据对接;
  • 支持外部编码后音视频数据对接;
  • 支持实时音量调节;
  • 支持扩展录像模块;
  • 支持Unity接口;
  • 支持H.264扩展SEI发送模块;
  • [预览]支持实时预览; 支持RTSP端口设置;
  • 支持RTSP鉴权用户名、密码设置;
  • 支持获取当前RTSP服务会话连接数;
  • 支持x64_64架构、aarch64架构(需要glibc-2.21及以上版本的Linux系统, 需要libX11.so.6, 需要GLib–2.0, 需安装 libstdc++.so.6.0.21、GLIBCXX_3.4.21、 CXXABI_1.3.9)。

配合SmartPlayer RTMP|RTSP直播播放器,可轻松实现150-400ms低延迟体验,感兴趣的开发者,可以单独跟我沟通讨论。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ​技术背景
    • x86_64架构
      • aarch64架构
        • 技术实现
          • 总结
          相关产品与服务
          实时音视频
          实时音视频(Tencent RTC)基于腾讯21年来在网络与音视频技术上的深度积累,以多人音视频通话和低延时互动直播两大场景化方案,通过腾讯云服务向开发者开放,致力于帮助开发者快速搭建低成本、低延时、高品质的音视频互动解决方案。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档