麒麟系统桌面采集|麒麟同屏|Linux 屏幕采集和系统声音采集推送SDK

本文介绍了在Linux上实现桌面和系统声音采集,并通过rtmp协议推送的SDK。内容包括集成简单示例代码,展示在Ubuntu 14上的低延迟采集播放效果,适用于无纸化办公和电子教室场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      Windows上实现桌面采集资料多,方案也多. Linux这方面的资料较少,不过Linux上也可以实现桌面采集,调用XLib相关接口就可以.

     最近写了一个Linux上实现桌面和系统声音采集, 然后使用rtmp协议推出去的一个SDK. 集成调用非常简单,下面是集成调用代码:

namespace
{
	volatile bool g_is_exit = false;

	void OnSigIntHandler(int sig)
	{
		if (SIGINT == sig)
		{
			g_is_exit = true;
		}
	}
	
	void LogInit()
	{
		SmartLogAPI log_api;
		memset(&log_api, 0, sizeof(log_api));
		GetSmartLogAPI(&log_api);

		log_api.SetLevel(SL_INFO_LEVEL);
		log_api.SetPath((NT_PVOID)"./");
	}

	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;
	}

	NT_HANDLE StartPush(NT_SmartPublisherSDKAPI* push_api, const std::string& rtmp_url, int dst_fps)
	{
		NT_INT32 pulse_device_number = 0;
		if (NT_ERC_OK == push_api->GetAuidoInputDeviceNumber(2, &pulse_device_number))
		{
			fprintf(stdout, "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, "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, "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, "Support speaker capture\n");
			else
				fprintf(stdout, "UnSupport speaker capture\n");
		}

		NT_HANDLE push_handle = nullptr;

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

		push_api->SetFrameRate(push_handle, dst_fps); // 帧率设置
		
		push_api->SetVideoBitRate(push_handle, 2000);  // 平均码率2000kbps
		push_api->SetVideoQualityV2(push_handle, 26); 
		push_api->SetVideoMaxBitRate(push_handle, 4000); // 最大码率4000kbps
		push_api->SetVideoKeyFrameInterval(push_handle, dst_fps*2); // 关键帧间隔
		push_api->SetVideoEncoderProfile(push_handle, 3); // h264 baseline
		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->SetPublisherAudioCodecType(push_handle, 1);
		//push_api->SetMute(push_handle, 1);

		if ( NT_ERC_OK != push_api->SetURL(push_handle, rtmp_url.c_str(), NULL) )
		{
			push_api->Close(push_handle);
			push_handle = nullptr;
			return nullptr;
		}

		if ( NT_ERC_OK != push_api->StartPublisher(push_handle, NULL) )
		{
			push_api->Close(push_handle);
			push_handle = nullptr;
			return nullptr;
		}

		return push_handle;
	}
}

int main(int argc, char *argv[])
{
	signal(SIGINT, &OnSigIntHandler);

	LogInit();

	NT_SmartPublisherSDKAPI push_api;
	if (!PushSDKInit(push_api))
	{
		return 0;
	}

	auto push_handle = StartPush(&push_api, "rtmp://192.168.1.99:1935/live/test", 30);
	if (!push_handle)
	{
		fprintf(stderr, "start push failed.\n");
		push_api.UnInit();
		return 0;
	}

	while (!g_is_exit)
	{
		sleep(2);
	}

	fprintf(stdout, "Skip run loop, is_exit:%d\n", g_is_exit);

	push_api.StopPublisher(push_handle);

	push_api.Close(push_handle);

	push_handle = nullptr;
	
	push_api.UnInit();

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

   下面是采集Ubuntu 14屏幕,然后在Windows上的播放效果:

   上图可以看到,延时也就几十毫秒,这个延时做无纸化办公,电子教室等场景的采集和播放很理想.

  更多问题可以联系qq: 1130758427,   github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值