MiracastManager.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using EZXR.Glass.Recording;
  5. using System.Threading;
  6. using System;
  7. using UnityEngine.UI;
  8. #if ARMiracast
  9. using Unity.RenderStreaming;
  10. using Unity.RenderStreaming.Signaling;
  11. #endif
  12. namespace EZXR.Glass.MiraCast
  13. {
  14. public class MiracastManager : MonoBehaviour
  15. {
  16. #region 单例
  17. private static MiracastManager instance;
  18. public static MiracastManager Instance
  19. {
  20. get
  21. {
  22. return instance;
  23. }
  24. }
  25. #endregion
  26. public bool isMiracasting;
  27. public int width = 1280;
  28. public int height = 960;
  29. #if ARMiracast
  30. public RenderStreaming renderStreaming;
  31. #endif
  32. private void Awake()
  33. {
  34. if (Application.isPlaying)
  35. {
  36. instance = this;
  37. DontDestroyOnLoad(gameObject);
  38. #if ARMiracast
  39. renderStreaming = GetComponent<RenderStreaming>();
  40. #endif
  41. ARRenderRGB.SetRGBResolution(width, height);
  42. }
  43. else
  44. {
  45. }
  46. }
  47. private void OnApplicationPause(bool pause)
  48. {
  49. if (pause)
  50. {
  51. Stop();
  52. }
  53. }
  54. public void Run(string serverIP)
  55. {
  56. ARRenderRGB.Instance.HandleARRenderOpen();
  57. #if ARMiracast
  58. renderStreaming.Run(true, CreateSignaling(typeof(WebSocketSignaling), "ws://" + serverIP, 5, SynchronizationContext.Current));
  59. #endif
  60. isMiracasting = true;
  61. }
  62. public void Stop()
  63. {
  64. Debug.Log("MiracastManager Stop 0");
  65. ARRenderRGB.Instance.HandleARRenderClose();
  66. #if ARMiracast
  67. Debug.Log("MiracastManager Stop 1");
  68. //renderStreaming.Stop();
  69. instance = null;
  70. Debug.Log("MiracastManager Stop 2");
  71. Destroy(gameObject);
  72. Debug.Log("MiracastManager Stop 3");
  73. #endif
  74. }
  75. #if ARMiracast
  76. static ISignaling CreateSignaling(Type type, string url, float interval, SynchronizationContext context)
  77. {
  78. Debug.Log(type);
  79. Type _type = type;
  80. if (_type == null)
  81. {
  82. throw new ArgumentException($"Signaling type is undefined. {type}");
  83. }
  84. object[] args = { url, interval, context };
  85. return (ISignaling)Activator.CreateInstance(_type, args);
  86. }
  87. #endif
  88. }
  89. }