WebGL实时视频(6) Unity里面显示视频
接着上一篇
将awrtc.js拷贝会原来的awrtc.jspre,
同时在awrtc_unity.js里面添加上相应的测试接口
var Unity_DebugTool={Unity_H5Stream_GetVideo: function(e){console.log("------- Unity_H5Stream_GetVideo",e);return awrtc.CAPI_H5Stream_GetVideo(e);},Unity_SetRtcSourceType: function(e){console.log("------- Unity_SetRtcSourceType",e);return awrtc.CAPI_SetRtcSourceType(e);},Unity_SetDebugShowElements: function(e){console.log("------- Unity_SetDebugShowElements",e);return awrtc.CAPI_SetDebugShowElements(e);}
};
创建Unity场景和脚本
为了不影响原来的例子,创建脚本WebRtcVideo和WebRtcVideoUI,内容先和CallApp以及CalAppUI一样,然后在CallAppUI中删除无关的界面元素和相关代码。
创建场景,并测试,结果
Start对应的WebRtcVideo的代码
public void StartPlay(){SetupCall();//mCall.Call("{'type': 'open'}");//不行有问题//mCall.Call("{\"type\": \"open\"}");//可以var cmd = new Cmd { type="open"};var json = JsonUtility.ToJson(cmd);//JsonUtility不能用匿名类,也不支持属性。Debug.Log("json:" + json);mCall.Call(json);//可以}private class Cmd{public string type;}
public virtual void SetupCall(){Append("Setting up ...");//hacks to turn off certain connection types. If both set to true only//turn servers are used. This helps simulating a NAT that doesn't support//opening ports.//hack to turn off direct connections//Byn.Awrtc.Native.InternalDataPeer.sDebugIgnoreTypHost = true;//Byn.Awrtc.Native.InternalDataPeer.sDebugIgnoreTypSrflx = true;NetworkConfig netConfig = CreateNetworkConfig();Debug.Log("Creating call using NetworkConfig:" + netConfig);mCall = CreateCall(netConfig);if (mCall == null){Append("Failed to create the call");return;}mCall.LocalFrameEvents = mLocalFrameEvents;string[] devices = UnityCallFactory.Instance.GetVideoDevices();if (devices == null || devices.Length == 0){Debug.Log("no device found or no device information available");}else{foreach (string s in devices)Debug.Log("device found: " + s + " IsFrontFacing: " + UnityCallFactory.Instance.IsFrontFacing(s));}Append("Call created!:" + mCall);mCall.CallEvent += Call_CallEvent;//make a deep clone to avoid confusion if settings are changed//at runtime. mMediaConfigInUse = mMediaConfig.DeepClone();//try to pick a good default video device if the user wants to send video but//didn't bother to pick a specific deviceif (mMediaConfigInUse.Video && string.IsNullOrEmpty(mMediaConfigInUse.VideoDeviceName)){mMediaConfigInUse.VideoDeviceName = UnityCallFactory.Instance.GetDefaultVideoDevice();}Debug.Log("Configure call using MediaConfig: " + mMediaConfigInUse);mCall.Configure(mMediaConfigInUse);mUi.SetGuiState(false);}
整个过程和上一篇的html里面测试的代码一样。应该说那个过程是我根据unity里面的CallApp写的。
1.创建config
2.创建Call
3.调用Call方法
4.在事件中显示显示并刷新图片
刷新图片用到了CallEventType.FrameUpdate,也就是和awrtc.js里面的FrameToCallEvent连接上了。
public static bool UpdateTexture(IFrame frame, ref Texture2D tex){var format = frame.Format;if (frame.Format == FramePixelFormat.ABGR || frame.Format == FramePixelFormat.YUY2){bool newTextureCreated = false;//texture exists but has the wrong height /width? -> destroy it and set the value to nullif (tex != null && (tex.width != frame.Width || tex.height != frame.Height)){Texture2D.Destroy(tex);tex = null;}//no texture? create a new one firstif (tex == null){newTextureCreated = true;Debug.Log("Creating new texture with resolution " + frame.Width + "x" + frame.Height + " Format:" + format);//so far only ABGR is really supported. this will change laterif (format == FramePixelFormat.ABGR){tex = new Texture2D(frame.Width, frame.Height, TextureFormat.RGBA32, false);}else{Debug.LogWarning("YUY2 texture is set. This is only for testing");tex = new Texture2D(frame.Width, frame.Height, TextureFormat.YUY2, false);}tex.wrapMode = TextureWrapMode.Clamp;}//copy image data into the texture and apply//Watch out the RawImage has the top pixels in the top row but//unity has the top pixels in the bottom row. Result is an image that is//flipped. Fixing this here would waste a lot of CPU power thus//the UI will simply set scale.Y of the UI element to -1 to reverse this.tex.LoadRawTextureData(frame.Buffer);tex.Apply();return newTextureCreated;}else if (frame.Format == FramePixelFormat.I420p && frame is IDirectMemoryFrame){//this one shouldn't be used. It squeezes all planes into a single texture var dframe = frame as IDirectMemoryFrame;int texHeight = (int)(frame.Height * 1.5f);bool newTextureCreated = EnsureTex(frame.Width, texHeight, TextureFormat.R8, ref tex);tex.LoadRawTextureData(dframe.GetIntPtr(), dframe.GetSize());dframe.Dispose();tex.Apply();return newTextureCreated;}else{Debug.LogError("Format not supported");return false;}}
从结果来看,可以显示在三维物体上。
------------------------------------------------------------------------------------------------------------
而awrtc.js里面的核心代码是context.drawImage和context.getImageData。
总之,视频还是由html5的video来获取,至于怎么获取,怎么解码我们不用管,从它上面获取图片就行了。
这么看来,用h5splayer.js获取也能够结合unity来获取到视频图片。
而且不一定用webrtc,用websocket也是可以的。
但是写出这么一大套的完整代码,那就不知道要多久了。
115美元,再加上2星期的阅读理解和修改,才到能够使用的水平。
一天的工资就不止115美元了....。
还是花钱效率高,可惜只找到这么一个插件。
接下来是和UMP结合,还要结合后端,根据摄像头获取相应的h5stream的ws地址。
另外还有音频的问题,不过,既然本质上是在网页中的video,音频是不是不用特别处理,也是能播放的。
----------------------------------------------------------------------------------------------------------------------------------------
注意到在播放视频后,三维里面旋转的物体的速度就变慢了,说明帧率下降了。
测试一下,既然播放视频是用Update->EvenCall来的,改成InvokeRepeating来调整刷新的频率。
先将GetFrame里面的打印语句去掉,打印语句有和没有还是有差别的,5帧左右。
1.播放视频前60,播放后30上下,25-35。
2.按时间播放:0.02,30左右;0.1,40左右;0.2,44左右,0.3,能到50了。再多看到的图像就不连续了,能够识别出区别了。
0.1左右比较合适,0.02就没必要了。
代码如下:
protected virtual void FixedUpdate()//0.02s,20ms{if (UpdateByFixed && IsPlayUpdating){CallUpdate();}}private void CallUpdate(){if (mCall != null){//update the call object. This will trigger all buffered events to be fired//to ensure it is done in the unity thread at a convenient time.mCall.Update();}}public bool IsPlayUpdating = false;public bool UpdateByFixed = true;public bool UpdateByInvoke = false;public float PlayUpdateInterval = 0.1f;public void StartPlayUpdate(){IsPlayUpdating = true;if (UpdateByInvoke){InvokeRepeating("CallUpdate", 0, PlayUpdateInterval);}}public void StopPlayUpdate(){IsPlayUpdating = false;if (UpdateByInvoke){CancelInvoke("CallUpdate");}}
protected virtual NetworkConfig CreateNetworkConfig(){if (mUi.ToggleIsByInterval.isOn){UpdateByFixed = false;UpdateByInvoke = true;PlayUpdateInterval = float.Parse(mUi.InputInterval.text);}else{UpdateByFixed = true;UpdateByInvoke = false;}。。。。。。。
}
WebGL实时视频(6) Unity里面显示视频相关推荐
- Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等
Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等 目录 Unity 基础 之 在 UGUI 上简单实现VideoPl ...
- Unity实现Image图片显示视频 、灵活配置控制视频播放(视频格式为MovieTexture)
一.编写(视频格式为MovieTexture)视频控制脚本 /*** * Title:" " 项目 * 主题:实现控制moveTexture格式的视频 * Description: ...
- java制作h5视频聊天_JAVA实现大华摄像头WEB方式实时显示视频,H5界面展示方式思路。...
JAVA实现大华摄像头WEB方式实时显示视频,H5界面展示方式思路. 2018-09-17 问题:大华IPC枪型摄像头需要在WEB中显示实时监控视频,官方提供的SDK只有C#的桌面程序访问方式. 解决 ...
- WebGL实时视频(5) awrtc.js理解并修改
继续上一篇的 在理解了WebRTC Video Chat的本质是js后,就是理解并修改成我需要的功能了. 一开始是修改后,用unity打包,进行测试.效率很低,一周也没进展多少.后来想到,能不能直接在 ...
- Unity 绿幕视频抠图算法原理与实现 -- 效果极好
码字不易,转载请注明出处喔 https://blog.csdn.net/newchenxf/article/details/119575690 分两件事来说,一个是Unity怎么播放视频,二是播放了, ...
- unity制作交互视频/互动视频
unity制作交互视频/互动视频 思路 1.先让unity播放视频 2. 视频播放结束后,视频暂停,按钮出现 3.按钮跳转下一个视频 视频播放 先创建UI-Raw Iamge 在Raw Iamge 挂 ...
- iNeuOS工业互联平台,WEB组态(iNeuView)集成rtmp和websocket视频元件,支持海康、大华等摄像头实时显示视频
目 录 1. 概述... 1 2. 平台演示... 2 3. 硬件摄像头... 2 4. 视频流协议转换管理... 2 5. 组态视频元件 ...
- VS+MFC+Opencv显示视频和图像。
读入图片: void CDrawImgVideoDlg::OnBnClickedShowImg() { // TODO: 在此添加控件通知处理程序代码 CDC *pDC = GetDl ...
- glide缩略图存储 android,Glide 显示视频缩略图及遇到的坑
Glide 显示视频缩略图及遇到的坑 实现原理 Glide支持视频格式的文件,但是在3.x里会有些欠缺.其底层是通过 MediaMetadataRetriever实现的. MediaMetadataR ...
最新文章
- 技术总监,被判刑12年!因公司未兑现股权,愤怒植入代码,牟利333万
- [拓扑排序/强联通分量] [NOIP201402] 信息传递
- [RN] React Native 错误 Module does not exist in the module map
- app启动页数秒加载 代码_干货 | App 自动化测试痛点(弹框及首页启动加载完成判断处理)
- [html] 实现一个页面锁屏的功能
- 【译】Asp.Net 导出 Excel 数据的9种方案
- php的auth权限类源码
- C中常量数组不能修改
- ShellExecuteEx and SHELLEXECUTEINFO and ShellExecute
- html设置某一块一直在屏幕上方,gogo体育在线-gogo体育在线
- 最新版Activit7从原理到项目,工作流精讲上线
- 斯坦福大学公开课:乔布斯演讲
- 枫叶永恒 服务器维护,3月8日服务器维护公告
- 在Windows 7中使用AppLocker限制对程序的访问
- 偏偏在面试的时候踏入一个大坑--360浏览器兼容模式
- oracle11g 客户端字符集,查看 Oracle11g 的字符集
- MySQL 5.7 服务端 错误码 (机翻)
- git具体作用_Git是什么
- 火焰焰心matlab,火焰心_刘德华_高音质在线试听_火焰心歌词|歌曲下载_酷狗音乐...
- html 合并单元格 步骤总结