当前位置:   article > 正文

HoloToolkit/unity远程实时传输视频_unity实时录像上传

unity实时录像上传

-1 holotoolkit 客户端与服务器架构的建立

0  微软Hololens开发包MixedRealityToolkit-unity

https://github.com/Microsoft/MixedRealityToolkit-Unity/releases

https://github.com/microsoft/MixedRealityToolkit-Unity.

0-1  Windows Mixed Reality documentation

 

1, 服务器

windows+R

cmd

把x.exe拖入到黑窗口中

再输入 -local

则服务器会运行起来

2 local客户端

a,导入 HoloToolkit.unitypackage

b,拖入预制体sharing

c 添加三个脚本

打开并显示相机  cameraView.cs

A  devices[0].name  或 devices[i].name

B 视频传输时编码和解码都用的是unity自带的功能

Texture2D viewTexture)

 cameraView.viewTexture.EncodeToJPG();

 viewTexture.LoadImage(currentFrame);

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class cameraView : MonoBehaviour {
  5. public GameObject cameraViewPlan;
  6. public static Texture2D viewTexture;
  7. public static WebCamTexture viewTextureCamera;
  8. // Use this for initialization
  9. void Start () {
  10. WebCamDevice[] devices = WebCamTexture.devices;
  11. viewTextureCamera = new WebCamTexture("Creative GestureCam", 1280, 720, 30);
  12. viewTexture = new Texture2D(viewTextureCamera.width, viewTextureCamera.height);
  13. viewTextureCamera.Play();
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. viewTexture.SetPixels(viewTextureCamera.GetPixels());
  18. viewTexture.Apply();
  19. GetComponent<Renderer>().material.mainTexture = viewTexture;
  20. }
  21. }

下面两个脚本只要修改这两个脚本就好

LocalCustomMessagesManger.cs

发送数据

  1. public void SendMessage()
  2. {
  3. // If we are connected to a session, broadcast our head info
  4. if (serverConnection != null && serverConnection.IsConnected())
  5. {
  6. // Create an outgoing network message to contain all the info we want to send
  7. NetworkOutMessage msg = CreateMessage((byte)TestMessageID.StringMessageID);
  8. //send video
  9. byte[] currentColorFrameByteArray = cameraView.viewTexture.EncodeToJPG();
  10. msg.Write(currentColorFrameByteArray.Length);
  11. msg.WriteArray(currentColorFrameByteArray, (uint)(currentColorFrameByteArray.Length));
  12. //AppendTransform();调用这个函数就可以继续传值了
  13. // Send the message as a broadcast, which will cause the server to forward it to all other users in the session.
  14. serverConnection.Broadcast(
  15. msg,
  16. MessagePriority.Immediate,
  17. MessageReliability.UnreliableSequenced,
  18. MessageChannel.Avatar);
  19. }
  20. }

 

d, 挂脚本

3 remote client

a, cameraView.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class cameraView : MonoBehaviour {
  5. public GameObject cameraViewPlan;
  6. public static byte[] currentFrame;
  7. private Texture2D viewTexture;
  8. // Use this for initialization
  9. void Start () {
  10. currentFrame = new byte[1280 * 720 * 4];
  11. viewTexture = new Texture2D(1280, 720);
  12. }
  13. // Update is called once per frame
  14. void Update () {
  15. viewTexture.LoadImage(currentFrame);
  16. viewTexture.Apply();
  17. GetComponent<Renderer>().material.mainTexture = viewTexture;
  18. }
  19. }

如果在本地端不显示则把这两句注释

viewTexture.Apply();

 GetComponent<Renderer>().material.mainTexture = viewTexture;

b,LocalCustomMessagesManager.cs

接受数据

  1. /// <summary>
  2. /// Called when a remote user sends a message.
  3. /// </summary>
  4. /// <param name="msg"></param>
  5. private void UpdateMessage(NetworkInMessage msg)
  6. {
  7. // Parse the message
  8. long userID = msg.ReadInt64();
  9. //接受视频
  10. int colorFrameLength = msg.ReadInt32();
  11. if (colorFrameLength > 1)
  12. {
  13. msg.ReadArray(cameraView.currentFrame, (uint)colorFrameLength);
  14. } ///后面继续写接受的,发送和接受的顺序要对应上不然会出错
  15. }

 

 

 

这个IP要把localhost修改为服务器的IP

 

HoloToolkit5.5.0 API详解

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号