UnityRemoteVideo.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace UnityEngine.XR.iOS
  6. {
  7. public class UnityRemoteVideo : MonoBehaviour
  8. {
  9. public ConnectToEditor connectToEditor;
  10. private UnityARSessionNativeInterface m_Session;
  11. private bool bTexturesInitialized;
  12. private int currentFrameIndex;
  13. private byte[] m_textureYBytes;
  14. private byte[] m_textureUVBytes;
  15. private byte[] m_textureYBytes2;
  16. private byte[] m_textureUVBytes2;
  17. private GCHandle m_pinnedYArray;
  18. private GCHandle m_pinnedUVArray;
  19. #if !UNITY_EDITOR
  20. public void Start()
  21. {
  22. m_Session = UnityARSessionNativeInterface.GetARSessionNativeInterface ();
  23. UnityARSessionNativeInterface.ARFrameUpdatedEvent += UpdateCamera;
  24. currentFrameIndex = 0;
  25. bTexturesInitialized = false;
  26. }
  27. void UpdateCamera(UnityARCamera camera)
  28. {
  29. if (!bTexturesInitialized) {
  30. InitializeTextures (camera);
  31. }
  32. UnityARSessionNativeInterface.ARFrameUpdatedEvent -= UpdateCamera;
  33. }
  34. void InitializeTextures(UnityARCamera camera)
  35. {
  36. int numYBytes = camera.videoParams.yWidth * camera.videoParams.yHeight;
  37. int numUVBytes = camera.videoParams.yWidth * camera.videoParams.yHeight / 2; //quarter resolution, but two bytes per pixel
  38. m_textureYBytes = new byte[numYBytes];
  39. m_textureUVBytes = new byte[numUVBytes];
  40. m_textureYBytes2 = new byte[numYBytes];
  41. m_textureUVBytes2 = new byte[numUVBytes];
  42. m_pinnedYArray = GCHandle.Alloc (m_textureYBytes);
  43. m_pinnedUVArray = GCHandle.Alloc (m_textureUVBytes);
  44. bTexturesInitialized = true;
  45. }
  46. IntPtr PinByteArray(ref GCHandle handle, byte[] array)
  47. {
  48. handle.Free ();
  49. handle = GCHandle.Alloc (array, GCHandleType.Pinned);
  50. return handle.AddrOfPinnedObject ();
  51. }
  52. byte [] ByteArrayForFrame(int frame, byte[] array0, byte[] array1)
  53. {
  54. return frame == 1 ? array1 : array0;
  55. }
  56. byte [] YByteArrayForFrame(int frame)
  57. {
  58. return ByteArrayForFrame (frame, m_textureYBytes, m_textureYBytes2);
  59. }
  60. byte [] UVByteArrayForFrame(int frame)
  61. {
  62. return ByteArrayForFrame (frame, m_textureUVBytes, m_textureUVBytes2);
  63. }
  64. void OnDestroy()
  65. {
  66. m_Session.SetCapturePixelData (false, IntPtr.Zero, IntPtr.Zero);
  67. m_pinnedYArray.Free ();
  68. m_pinnedUVArray.Free ();
  69. }
  70. public void OnPreRender()
  71. {
  72. ARTextureHandles handles = m_Session.GetARVideoTextureHandles();
  73. if (handles.textureY == System.IntPtr.Zero || handles.textureCbCr == System.IntPtr.Zero)
  74. {
  75. return;
  76. }
  77. if (!bTexturesInitialized)
  78. return;
  79. currentFrameIndex = (currentFrameIndex + 1) % 2;
  80. Resolution currentResolution = Screen.currentResolution;
  81. m_Session.SetCapturePixelData (true, PinByteArray(ref m_pinnedYArray,YByteArrayForFrame(currentFrameIndex)), PinByteArray(ref m_pinnedUVArray,UVByteArrayForFrame(currentFrameIndex)));
  82. connectToEditor.SendToEditor (ConnectionMessageIds.screenCaptureYMsgId, YByteArrayForFrame(1-currentFrameIndex));
  83. connectToEditor.SendToEditor (ConnectionMessageIds.screenCaptureUVMsgId, UVByteArrayForFrame(1-currentFrameIndex));
  84. }
  85. #endif
  86. }
  87. }