当前位置:   article > 正文

Unity接收FFmpeg的UDP推流_unity 接收udp图片

unity 接收udp图片
  1. 将下方的脚本挂在Unity中的一个空物体上:
  1. // proof of concept, ffmpeg raw video into unity texture 2D using UDP streaming
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using UnityEngine;
  7. using Debug = UnityEngine.Debug;
  8. namespace UnityCoder.RawVideoUDP
  9. {
  10. public class RawVideoReceiver : MonoBehaviour
  11. {
  12. public Material targetMat;
  13. UdpClient client;
  14. int port = 8888;
  15. int receiveBufferSize = 1472 * 1000;
  16. IPEndPoint ipEndPoint;
  17. private object obj = null;
  18. private AsyncCallback AC;
  19. byte[] receivedBytes;
  20. Texture2D tex;
  21. public int size = 256;
  22. int imageSize = 0;
  23. byte[] dump;
  24. int bufferSize = 0;
  25. int bufferIndex = 0;
  26. int bufferFrameStart = 0;
  27. byte[] temp;
  28. bool frameReady = false;
  29. void Start()
  30. {
  31. //tex = new Texture2D(size, size, TextureFormat.RGB24, false, false);
  32. tex = new Texture2D(size, size, TextureFormat.RGBA32, false, false);
  33. tex.filterMode = FilterMode.Point;
  34. tex.wrapMode = TextureWrapMode.Clamp;
  35. imageSize = size * size * 4;
  36. temp = new byte[imageSize];
  37. // init pixels wit bright color
  38. for (int i = 0; i < imageSize; i += 4)
  39. {
  40. temp[i] = 255;
  41. temp[i + 1] = 0;
  42. temp[i + 2] = 255;
  43. }
  44. tex.LoadRawTextureData(temp);
  45. tex.Apply(false);
  46. bufferSize = imageSize * 100;
  47. dump = new byte[bufferSize];
  48. targetMat.mainTexture = tex;
  49. InitializeUDPClient();
  50. }
  51. Queue<int> frameIndex = new Queue<int>();
  52. int frameBufferCount = 0;
  53. void FixedUpdate()
  54. {
  55. // if we have frames, draw them to texture
  56. if (frameBufferCount > 0)
  57. {
  58. Buffer.BlockCopy(dump, frameIndex.Dequeue(), temp, 0, imageSize);
  59. frameBufferCount--;
  60. tex.LoadRawTextureData(temp);
  61. tex.Apply(false);
  62. }
  63. }
  64. void ReceivedUDPPacket(IAsyncResult result)
  65. {
  66. try
  67. {
  68. receivedBytes = client.EndReceive(result, ref ipEndPoint);
  69. var len = receivedBytes.Length;
  70. // we only use the buffer until the end, should wrap around
  71. if (bufferIndex + len > bufferSize)
  72. {
  73. Debug.LogError("Buffer finished, should fix this..");
  74. return;
  75. }
  76. Buffer.BlockCopy(receivedBytes, 0, dump, bufferIndex, len);
  77. bufferIndex += len;
  78. if (bufferIndex - bufferFrameStart >= imageSize)
  79. {
  80. frameIndex.Enqueue(bufferFrameStart);
  81. frameBufferCount++;
  82. bufferFrameStart += imageSize;
  83. }
  84. }
  85. catch (Exception e)
  86. {
  87. Debug.LogException(e);
  88. }
  89. client.BeginReceive(AC, obj);
  90. }
  91. public void InitializeUDPClient()
  92. {
  93. ipEndPoint = new IPEndPoint(IPAddress.Any, port);
  94. client = new UdpClient();
  95. client.Client.ReceiveBufferSize = receiveBufferSize;
  96. client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue: true);
  97. client.ExclusiveAddressUse = false;
  98. client.EnableBroadcast = true;
  99. client.Client.Bind(ipEndPoint);
  100. client.DontFragment = true;
  101. client.Client.ReceiveBufferSize = 1472 * 100000;
  102. AC = new AsyncCallback(ReceivedUDPPacket);
  103. client.BeginReceive(AC, obj);
  104. Debug.Log("Started UDP listener..");
  105. }
  106. private void OnDestroy()
  107. {
  108. if (client != null)
  109. {
  110. client.Close();
  111. }
  112. }
  113. }
  114. }
  1. 创建一个Plane或Quad,并选择一个Unlit Shader作为承载影像的材质使用Ulit/Texture挂载到该物体上;

  1. 将用在这个Plane或Quad的Unlit Shader材质挂载在第一步中创建的空物体Raw Video Player组件中的Target Mat上;

  1. 将相机调整到合适位置;

  1. 在本机上进行针对于桌面的ffmpeg推流,命令如下:
 

ffmpeg -f gdigrab -i desktop -pixel_format rgb8 -video_size 256x256 -vf scale=256:256 -framerate 5 -r 5 -f rawvideo udp://127.0.0.1:8888

  1. 点击运行Unity的场景,可以看到桌面推流的效果。


原文地址:Unity接收FFmpeg的UDP推流 - Ezharjan - 博客园

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/114166
推荐阅读
相关标签
  

闽ICP备14008679号