当前位置: 首页 > article >正文

Android平台Unity3D下如何同时播放多路RTMP|RTSP流?

技术背景

好多开发者,提到希望在Unity的Android头显终端,播放2路以上RTMP或RTSP流,在设备性能一般的情况下,对Unity下的RTMP|RTSP播放器提出了更高的要求。实际上,我们在前几年发布Unity下直播播放模块的时候,就已经支持了Android多实例播放RTMP|RTSP,随着大家对这块的技术诉求和性能要求越来越高,我们需要持续考虑如何低资源占用的播放多实例流。

实现思路

目前,我们是通过大牛直播SDK原生的RTMP|RTSP播放器,设置回调解码后的YUV或RGB数据,然后投递到Unity层,在Unity层做渲染。

对于每一路RTMP或RTSP流,可以分别创建个播放实例,并启动播放。可以创建一个管理类,用于统一管理多个播放器实例,方便对多路流的播放状态进行监控和控制。

当从原生播放器中获取到视频流的数据后,需要将数据回调到 Unity 中进行渲染。可以使用 Unity 的纹理(Texture)来存储视频帧数据,并将其应用到相应的材质(Material)上,然后将材质应用到 3D 模型或UI元素上,以实现视频的播放显示。对于多路视频流,需要为每一路视频流创建独立的纹理和材质,并分别进行渲染。

具体实现如下:

开始播放:

/*
 * SmartPlayerAndroidMono.cs
 * Author: daniusdk.com
 * WeChat: xinsheng120
 */
 
public void Play(int sel)
{
	if (videoctrl[sel].is_running)
	{
		Debug.Log("已经在播放.. sel: " + sel);   
		return;
	}

	videoctrl[sel].player_handle_ = OpenPlayer();
	if (videoctrl[sel].player_handle_ == 0)
	{
		Debug.LogError("open fail sel: " + sel);
		return;
	}

	NT_U3D_Set_Game_Object(videoctrl[sel].player_handle_, game_object_);


	/* ++ 播放前参数配置可加在此处 ++ */
	int is_using_tcp = 0;        //TCP/UDP模式设置
	NT_U3D_SetRTSPTcpMode(videoctrl[sel].player_handle_, is_using_tcp);

	int is_report = 0;
	int report_interval = 1;
	NT_U3D_SetReportDownloadSpeed(videoctrl[sel].player_handle_, is_report, report_interval);  //下载速度回调

	int buffer_time = 0;
	NT_U3D_SetBuffer(videoctrl[sel].player_handle_, buffer_time);//设置buffer time

	NT_U3D_SetPlayerLowLatencyMode(videoctrl[sel].player_handle_, 0);//设置是否启用低延迟模式

	NT_U3D_SetMute(videoctrl[sel].player_handle_, 0);//是否启动播放的时候静音

	int cur_audio_volume = 100;         //默认播放音量
	NT_U3D_SetAudioVolume(videoctrl[sel].player_handle_, cur_audio_volume);             //设置播放音量

	Boolean is_hw_decode = true;
	NT_U3D_SetVideoDecoderMode(videoctrl[sel].player_handle_, is_hw_decode ? 1 : 0);          //设置H.264软硬解模式
	NT_U3D_SetVideoHevcDecoderMode(videoctrl[sel].player_handle_, is_hw_decode ? 1 : 0);          //设置H.265软硬解模式

	NT_U3D_SetImageReaderOutput(videoctrl[sel].player_handle_, is_output, disable_use_image_planes, is_supported_multiple_format, max_images, buffer_pool_max_size);

	int is_fast_startup = 1;
	NT_U3D_SetFastStartup(videoctrl[sel].player_handle_, is_fast_startup);                     //设置快速启动模式

	int rtsp_timeout = 10;
	NT_U3D_SetRTSPTimeout(videoctrl[sel].player_handle_, rtsp_timeout);                        //设置RTSP超时时间

	int is_auto_switch_tcp_udp = 1;
	NT_U3D_SetRTSPAutoSwitchTcpUdp(videoctrl[sel].player_handle_, is_auto_switch_tcp_udp);    //设置TCP/UDP模式自动切换
	int is_audiotrack = 1;
	NT_U3D_SetAudioOutputType(videoctrl[sel].player_handle_, is_audiotrack);                   //设置音频输出模式: if 0: 自动选择; if with 1: audiotrack模式
	NT_U3D_SetUrl(videoctrl[sel].player_handle_, videoctrl[sel].videoUrl);
	/* -- 播放前参数配置可加在此处 -- */

	int flag = NT_U3D_StartPlay(videoctrl[sel].player_handle_);

	if (flag  == DANIULIVE_RETURN_OK)
	{
		videoctrl[sel].is_need_get_frame_ = true;
		Debug.Log("播放成功 sel: " + sel);
	}
	else
	{
		videoctrl[sel].is_need_get_frame_ = false;
		Debug.LogError("播放失败 sel: " + sel);
	}

	videoctrl[sel].is_running = true;  
}

对应的OpenPlayer()实现如下:

private long OpenPlayer()
{
	if ( java_obj_cur_activity_ == null )
	{
		Debug.LogError("getApplicationContext is null");
		return 0;
	}
	
	long player_handle = 0;

	player_handle = NT_U3D_Open();

	if (player_handle != 0)
		Debug.Log("open success");
	else
		Debug.LogError("open fail");
		
	return player_handle;
}

停止播放:

private void ClosePlayer(int sel)
{
	videoctrl[sel].is_need_get_frame_ = false;
	videoctrl[sel].is_need_init_texture_ = false;

	int flag = NT_U3D_StopPlay(videoctrl[sel].player_handle_);
	if (flag == DANIULIVE_RETURN_OK)
	{
		Debug.Log("停止成功");
	}
	else
	{
		Debug.LogError("停止失败");
	}

	flag = NT_U3D_Close(videoctrl[sel].player_handle_);
	if (flag == DANIULIVE_RETURN_OK)
	{
		Debug.Log("关闭成功");
	}
	else
	{
		Debug.LogError("关闭失败");
	}

	videoctrl[sel].player_handle_ = 0;

	videoctrl[sel].video_format_ = VideoFrame.FORMAT_UNKNOWN;
	videoctrl[sel].video_width_ = 0;
	videoctrl[sel].video_height_ = 0;
	videoctrl[sel].is_running = false;
}

UpdateYUVTexture()实现如下:

private void UpdateYUVTexture(VideoFrame video_frame,int sel)
{
	if (video_frame == null)
		return;

	if (video_frame.java_frame_obj_ == null)
		return;

	if (video_frame.plane0_ == IntPtr.Zero || video_frame.plane1_ == IntPtr.Zero)
		return;

	if (video_frame.format_ == VideoFrame.FORMAT_I420) {
		if (video_frame.plane2_ptr_ == IntPtr.Zero)
			return;
	}
	if (videoctrl[sel].yTexture_ != null)
	{
		videoctrl[sel].yTexture_.LoadRawTextureData(video_frame.plane0_, video_frame.plane0_size_);
		videoctrl[sel].yTexture_.Apply();
	}

	if (videoctrl[sel].uTexture_ != null)
	{
		videoctrl[sel].uTexture_.LoadRawTextureData(video_frame.plane1_, video_frame.plane1_size_);
		videoctrl[sel].uTexture_.Apply();
	}

	if (video_frame.format_ == VideoFrame.FORMAT_I420)
	{
		if (videoctrl[sel].vTexture_ != null)
		{
			videoctrl[sel].vTexture_.LoadRawTextureData(video_frame.plane2_, video_frame.plane2_size_);
			videoctrl[sel].vTexture_.Apply();
		}
	}
}

总结

直接在Unity中播放RTMP|RTSP流可能并不简单,因为Unity没有内置对RTMP|RTSP的直接支持。你需要根据你的具体需求(如是否需要实时交互、流的来源、你的技术栈等)来选择最合适的解决方案。对于大多数应用场景,使用插件或服务器端转码可能是最简单有效的方法,但不是效率最高的办法,特别是对延迟要求比较高的场景,可以考虑使用大牛直播SDK这种专业的Unity RTMP|RTSP播放模块,无论是延迟还是稳定性,均可达到业内顶级的水准。以上是Unity下多路播放RTMP|RTSP的技术探讨,感兴趣的开发者,可以单独跟我沟通讨论。


http://www.kler.cn/news/319637.html

相关文章:

  • 什么是电商云手机?可以用来干什么?
  • 内容生态短缺,Rokid AR眼镜面临市场淘汰赛
  • 影刀RPA实战:网页爬虫之天猫商品数据
  • 在 Windows 上安装和配置 NVIDIA 驱动程序、CUDA、cuDNN 和 TensorRT
  • Vue2学习笔记(02条件渲染 、监视数据的原理)
  • JS面试真题 part6
  • 【C++】模拟实现list
  • WPF DataGrid 动态修改某一个单元格的样式
  • MyBatis 缓存机制
  • 【AI写作】解释区块链技术的应用场景和优势
  • select 函数简介
  • CentOS Linux教程(6)--CentOS目录
  • CSS的字体属性
  • 软件测试面试题(6)——二面(游戏测试)
  • 2024年_ChatGPT 及类似的人工智能技术带来的影响与改变 怎样利用 ChatGPT 提高学习效率
  • 在SpringBoot项目中利用Redission实现布隆过滤器(布隆过滤器的应用场景、布隆过滤器误判的情况、与位图相关的操作)
  • MICS:PythonJail沙箱逃逸(持续更新中)
  • Python数据分析与可视化:从基础到高级应用
  • vue3 实现图片预览组件
  • [ABC330E] Mex and Update
  • java-重启异常断掉的线程和监控线程状态
  • Android——Application
  • 网红挣钱太容易了
  • 路由器全局配置DHCP实验简述
  • MySQL篇(视图)(持续更新迭代)
  • CANopen通讯协议笔记
  • 制作一个能对话能跳舞的otto机器人
  • SentencePiece进行文本分类
  • 大数据-147 Apache Kudu 常用 Java API 增删改查
  • 二进制位运算题