WebVideoPlayer.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Nfynt.WVP
  5. {
  6. internal class WebVideoPlayer : MonoBehaviour, IVideoPlayer
  7. {
  8. private bool m_playerActive = false;
  9. private int m_playerIndx = -1;
  10. private RenderTexture m_targetTex;
  11. private Texture2D m_targetTex2D;
  12. private UnityEngine.Video.VideoAspectRatio m_videoAspectRatio;
  13. private Rect m_sampleRect = new Rect(0,0,1,1);
  14. public static string ValidVideoPath(string path)
  15. {
  16. if (path.Contains("://"))
  17. {
  18. //web url: https://; rtsp://; file:// etc
  19. //Todo implement accessibility test?
  20. return path;
  21. }
  22. #if UNITY_EDITOR
  23. if (System.IO.File.Exists(path)) return path;
  24. path = System.IO.Path.Combine(Application.streamingAssetsPath, path);
  25. if (System.IO.File.Exists(path))
  26. {
  27. path = "file://" + path;
  28. return path;
  29. }
  30. #elif UNITY_WEBGL
  31. path = System.IO.Path.Combine(Application.streamingAssetsPath, path);
  32. return path;
  33. #endif
  34. return "";
  35. }
  36. private void UpdateConfig(PlayerConfigs config, string url)
  37. {
  38. m_targetTex = config.VideoTextureTarget;
  39. m_videoAspectRatio = config.VideoAspectRatio;
  40. if (!string.IsNullOrEmpty(url))
  41. WVPNative.WVPSetDataSource(m_playerIndx, url);
  42. WVPNative.WVPSourceSetMute(m_playerIndx, config.MuteAsDefault);
  43. WVPNative.WVPSourceSetLoop(m_playerIndx, config.LoopPlayer);
  44. }
  45. public void InitializePlayer(PlayerConfigs config)
  46. {
  47. m_playerIndx = WVPNative.WVPInitialize(config.PlayOnAwake, config.LoopPlayer, config.MuteAsDefault);
  48. UpdateConfig(config, config.VideoSrcPath);
  49. Debug.Log("NVideoPlayer initialized. Indx: " + m_playerIndx);
  50. if (config.PlayOnAwake)
  51. {
  52. PlayStopVideo(config, true);
  53. }
  54. }
  55. public void ReleasePlayer()
  56. {
  57. Debug.Log("Releasing video player indx: " + m_playerIndx);
  58. WVPNative.WVPSourceRelease(m_playerIndx);
  59. }
  60. public bool PlayStopVideo(PlayerConfigs config, bool play)
  61. {
  62. m_playerActive = play;
  63. if (play)
  64. {
  65. UpdateConfig(config, ValidVideoPath(config.VideoSrcPath));
  66. WVPNative.WVPSourcePlay(m_playerIndx);
  67. StartCoroutine(TextureUpdateLoop());
  68. }
  69. else
  70. {
  71. StopCoroutine(TextureUpdateLoop());
  72. WVPNative.WVPSourceStop(m_playerIndx);
  73. }
  74. return true;
  75. }
  76. public bool PauseResumeVideo(bool pause)
  77. {
  78. if (pause)
  79. WVPNative.WVPSourcePause(m_playerIndx);
  80. else
  81. WVPNative.WVPSourcePlay(m_playerIndx);
  82. return true;
  83. }
  84. public bool MuteUnmuteVideo(bool mute)
  85. {
  86. WVPNative.WVPSourceSetMute(m_playerIndx, mute);
  87. return true;
  88. }
  89. public bool IsPlaying() => WVPNative.WVPSourceIsPlaying(m_playerIndx);
  90. public bool IsMuted() => WVPNative.WVPSourceIsMute(m_playerIndx);
  91. public double VideoDuration()
  92. {
  93. if(m_playerIndx<0) return 0;
  94. return WVPNative.WVPSourceDuration(m_playerIndx);
  95. }
  96. public double CurrFrameTime()
  97. {
  98. if(!m_playerActive) return 0;
  99. return WVPNative.WVPSourceFrameTime(m_playerIndx);
  100. }
  101. public bool SetFrameTime(double timeInSec)
  102. {
  103. if (m_playerIndx < 0) return false;
  104. WVPNative.WVPSourceSetFrameTime(m_playerIndx,timeInSec);
  105. return true;
  106. }
  107. public Vector2 FrameSize()
  108. {
  109. if(!m_playerActive) return Vector2.zero;
  110. return new Vector2(WVPNative.WVPSourceWidth(m_playerIndx),WVPNative.WVPSourceHeight(m_playerIndx));
  111. }
  112. private IEnumerator TextureUpdateLoop()
  113. {
  114. while (!WVPNative.WVPSourceIsReady(m_playerIndx))
  115. {
  116. yield return new WaitForEndOfFrame();
  117. Debug.Log("Loading video...");
  118. }
  119. int width = WVPNative.WVPSourceWidth(m_playerIndx);
  120. int height = WVPNative.WVPSourceHeight(m_playerIndx);
  121. Debug.Log("Vid Res: " + width + "x" + height);
  122. if (m_targetTex2D == null || m_targetTex2D.width != width || m_targetTex2D.height != height)
  123. {
  124. m_targetTex2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
  125. m_targetTex2D.filterMode = FilterMode.Bilinear;
  126. m_targetTex2D.Apply();
  127. }
  128. UpdateSampleRect(width, height, m_targetTex.width, m_targetTex.height, m_videoAspectRatio);
  129. while (m_playerActive)
  130. {
  131. yield return new WaitForEndOfFrame();
  132. if (IsPlaying())
  133. {
  134. WVPNative.WVPUpdateTexture(m_playerIndx, m_targetTex2D.GetNativeTexturePtr());
  135. RenderTexture at = RenderTexture.active;
  136. //RenderTexture.active = m_targetTex;
  137. //Graphics.Blit(m_targetTex2D, m_targetTex);
  138. NfyntGPUUpdateRTT(m_targetTex2D, m_targetTex);
  139. RenderTexture.active = at;
  140. }
  141. }
  142. }
  143. private void NfyntGPUUpdateRTT(Texture2D src, RenderTexture dstTex)
  144. {
  145. //Set the RTT for rendering
  146. Graphics.SetRenderTarget(dstTex);
  147. //RenderTexture.active = dstTex;
  148. GL.PushMatrix();
  149. GL.LoadPixelMatrix(0, 1, 1, 0);
  150. GL.Clear(true,true,new Color(0,0,0,0));
  151. Graphics.DrawTexture(m_sampleRect, src);
  152. GL.PopMatrix();
  153. }
  154. private void UpdateSampleRect(float srcWidth, float srcHeight, float dstWidth, float dstHeight, UnityEngine.Video.VideoAspectRatio arMode)
  155. {
  156. if (arMode == UnityEngine.Video.VideoAspectRatio.NoScaling)
  157. {
  158. m_sampleRect = new Rect(0, 0, srcWidth / dstWidth, srcHeight / dstHeight);
  159. }
  160. else if (arMode == UnityEngine.Video.VideoAspectRatio.FitVertically)
  161. {
  162. float srcAR = srcWidth / srcHeight;
  163. m_sampleRect = new Rect(0, 0, (dstHeight * srcAR) / dstWidth, 1);
  164. //Center the texture
  165. m_sampleRect = new Rect((1.0f - m_sampleRect.width) / 2.0f, (1.0f - m_sampleRect.height) / 2.0f, m_sampleRect.width, m_sampleRect.height);
  166. }
  167. else if (arMode == UnityEngine.Video.VideoAspectRatio.FitHorizontally)
  168. {
  169. float srcAR = srcWidth / srcHeight;
  170. m_sampleRect = new Rect(0, 0, 1, (dstWidth / srcAR) / dstHeight);
  171. //Center the texture
  172. m_sampleRect = new Rect((1.0f - m_sampleRect.width) / 2.0f, (1.0f - m_sampleRect.height) / 2.0f, m_sampleRect.width, m_sampleRect.height);
  173. }
  174. else if (arMode == UnityEngine.Video.VideoAspectRatio.FitInside)
  175. {
  176. if(srcWidth/dstWidth > srcHeight / dstHeight) //Fit horizontally
  177. {
  178. float srcAR = srcWidth / srcHeight;
  179. m_sampleRect = new Rect(0, 0, 1, (dstWidth / srcAR) / dstHeight);
  180. }
  181. else //Fit vertically
  182. {
  183. float srcAR = srcWidth / srcHeight;
  184. m_sampleRect = new Rect(0, 0, (dstHeight * srcAR) / dstWidth, 1);
  185. }
  186. //Center the texture
  187. m_sampleRect = new Rect((1.0f - m_sampleRect.width) / 2.0f, (1.0f - m_sampleRect.height) / 2.0f, m_sampleRect.width, m_sampleRect.height);
  188. }
  189. else if (arMode == UnityEngine.Video.VideoAspectRatio.FitOutside)
  190. {
  191. if (srcWidth / dstWidth > srcHeight / dstHeight) //Fit vertically so there's no border
  192. {
  193. float srcAR = srcWidth / srcHeight;
  194. m_sampleRect = new Rect(0, 0, (dstHeight * srcAR) / dstWidth, 1);
  195. }
  196. else //Fit horizontally so there's no border
  197. {
  198. float srcAR = srcWidth / srcHeight;
  199. m_sampleRect = new Rect(0, 0, 1, (dstWidth / srcAR) / dstHeight);
  200. }
  201. //Center the texture
  202. m_sampleRect = new Rect((1.0f - m_sampleRect.width) / 2.0f, (1.0f - m_sampleRect.height) / 2.0f, m_sampleRect.width, m_sampleRect.height);
  203. }
  204. else //stretch mode
  205. m_sampleRect = new Rect(0,0,1,1);
  206. //Debug.Log("Sample rect: " + m_sampleRect);
  207. }
  208. }
  209. }