Helper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. public static class Helper
  8. {
  9. public static string GetName(Platform platform)
  10. {
  11. return platform.ToString();
  12. }
  13. public static string GetErrorMessage(ErrorCode code)
  14. {
  15. string text = string.Empty;
  16. if (code != ErrorCode.None)
  17. {
  18. if (code != ErrorCode.LoadFailed)
  19. {
  20. if (code == ErrorCode.DecodeFailed)
  21. {
  22. text = "Decode failed. Possible codec not supported, video resolution too high or insufficient system resources.";
  23. }
  24. }
  25. else
  26. {
  27. text = "Loading failed. Codec not supported or video resolution too high or insufficient system resources.";
  28. if (SystemInfo.operatingSystem.StartsWith("Windows XP") || SystemInfo.operatingSystem.StartsWith("Windows Vista"))
  29. {
  30. text += " NOTE: Windows XP and Vista don't have native support for H.264 codec. Consider using an older codec such as DivX or installing 3rd party codecs such as LAV Filters.";
  31. }
  32. }
  33. }
  34. else
  35. {
  36. text = "No Error";
  37. }
  38. return text;
  39. }
  40. public static string[] GetPlatformNames()
  41. {
  42. return new string[]
  43. {
  44. Helper.GetName(Platform.Windows),
  45. Helper.GetName(Platform.MacOSX),
  46. Helper.GetName(Platform.iOS),
  47. Helper.GetName(Platform.tvOS),
  48. Helper.GetName(Platform.Android),
  49. Helper.GetName(Platform.WindowsPhone),
  50. Helper.GetName(Platform.WindowsUWP),
  51. Helper.GetName(Platform.WebGL),
  52. Helper.GetName(Platform.PS4)
  53. };
  54. }
  55. public static void LogInfo(string message, UnityEngine.Object context = null)
  56. {
  57. if (context == null)
  58. {
  59. Debug.Log("[AVProVideo] " + message);
  60. }
  61. else
  62. {
  63. Debug.Log("[AVProVideo] " + message, context);
  64. }
  65. }
  66. public static string GetTimeString(float totalSeconds, bool showMilliseconds = false)
  67. {
  68. int num = Mathf.FloorToInt(totalSeconds / 3600f);
  69. float num2 = (float)num * 60f * 60f;
  70. int num3 = Mathf.FloorToInt((totalSeconds - num2) / 60f);
  71. num2 += (float)num3 * 60f;
  72. int num4 = Mathf.FloorToInt(totalSeconds - num2);
  73. string result;
  74. if (num <= 0)
  75. {
  76. if (showMilliseconds)
  77. {
  78. int num5 = (int)((totalSeconds - Mathf.Floor(totalSeconds)) * 1000f);
  79. result = string.Format("{0:00}:{1:00}:{2:000}", num3, num4, num5);
  80. }
  81. else
  82. {
  83. result = string.Format("{0:00}:{1:00}", num3, num4);
  84. }
  85. }
  86. else if (showMilliseconds)
  87. {
  88. int num6 = (int)((totalSeconds - Mathf.Floor(totalSeconds)) * 1000f);
  89. result = string.Format("{2}:{0:00}:{1:00}:{3:000}", new object[]
  90. {
  91. num3,
  92. num4,
  93. num,
  94. num6
  95. });
  96. }
  97. else
  98. {
  99. result = string.Format("{2}:{0:00}:{1:00}", num3, num4, num);
  100. }
  101. return result;
  102. }
  103. public static Orientation GetOrientation(float[] t)
  104. {
  105. Orientation result = Orientation.Landscape;
  106. if (t[0] == 0f && t[1] == 1f && t[2] == -1f && t[3] == 0f)
  107. {
  108. result = Orientation.Portrait;
  109. }
  110. else if (t[0] == 0f && t[1] == -1f && t[2] == 1f && t[3] == 0f)
  111. {
  112. result = Orientation.PortraitFlipped;
  113. }
  114. else if (t[0] == 1f && t[1] == 0f && t[2] == 0f && t[3] == 1f)
  115. {
  116. result = Orientation.Landscape;
  117. }
  118. else if (t[0] == -1f && t[1] == 0f && t[2] == 0f && t[3] == -1f)
  119. {
  120. result = Orientation.LandscapeFlipped;
  121. }
  122. return result;
  123. }
  124. public static Matrix4x4 GetMatrixForOrientation(Orientation ori)
  125. {
  126. Matrix4x4 matrix4x = Matrix4x4.TRS(new Vector3(0f, 1f, 0f), Quaternion.Euler(0f, 0f, -90f), Vector3.one);
  127. Matrix4x4 matrix4x2 = Matrix4x4.TRS(new Vector3(1f, 0f, 0f), Quaternion.Euler(0f, 0f, 90f), Vector3.one);
  128. Matrix4x4 matrix4x3 = Matrix4x4.TRS(new Vector3(1f, 1f, 0f), Quaternion.identity, new Vector3(-1f, -1f, 1f));
  129. Matrix4x4 result = Matrix4x4.identity;
  130. switch (ori)
  131. {
  132. case Orientation.LandscapeFlipped:
  133. result = matrix4x3;
  134. break;
  135. case Orientation.Portrait:
  136. result = matrix4x;
  137. break;
  138. case Orientation.PortraitFlipped:
  139. result = matrix4x2;
  140. break;
  141. }
  142. return result;
  143. }
  144. public static void SetupStereoMaterial(Material material, StereoPacking packing, bool displayDebugTinting)
  145. {
  146. material.DisableKeyword("STEREO_CUSTOM_UV");
  147. material.DisableKeyword("STEREO_TOP_BOTTOM");
  148. material.DisableKeyword("STEREO_LEFT_RIGHT");
  149. material.DisableKeyword("MONOSCOPIC");
  150. switch (packing)
  151. {
  152. case StereoPacking.TopBottom:
  153. material.EnableKeyword("STEREO_TOP_BOTTOM");
  154. break;
  155. case StereoPacking.LeftRight:
  156. material.EnableKeyword("STEREO_LEFT_RIGHT");
  157. break;
  158. case StereoPacking.CustomUV:
  159. material.EnableKeyword("STEREO_CUSTOM_UV");
  160. break;
  161. }
  162. if (displayDebugTinting)
  163. {
  164. material.EnableKeyword("STEREO_DEBUG");
  165. }
  166. else
  167. {
  168. material.DisableKeyword("STEREO_DEBUG");
  169. }
  170. }
  171. public static void SetupAlphaPackedMaterial(Material material, AlphaPacking packing)
  172. {
  173. material.DisableKeyword("ALPHAPACK_TOP_BOTTOM");
  174. material.DisableKeyword("ALPHAPACK_LEFT_RIGHT");
  175. material.DisableKeyword("ALPHAPACK_NONE");
  176. if (packing != AlphaPacking.None)
  177. {
  178. if (packing != AlphaPacking.TopBottom)
  179. {
  180. if (packing == AlphaPacking.LeftRight)
  181. {
  182. material.EnableKeyword("ALPHAPACK_LEFT_RIGHT");
  183. }
  184. }
  185. else
  186. {
  187. material.EnableKeyword("ALPHAPACK_TOP_BOTTOM");
  188. }
  189. }
  190. }
  191. public static void SetupGammaMaterial(Material material, bool playerSupportsLinear)
  192. {
  193. if (QualitySettings.activeColorSpace == ColorSpace.Linear && !playerSupportsLinear)
  194. {
  195. material.EnableKeyword("APPLY_GAMMA");
  196. }
  197. else
  198. {
  199. material.DisableKeyword("APPLY_GAMMA");
  200. }
  201. }
  202. public static int ConvertTimeSecondsToFrame(float seconds, float frameRate)
  203. {
  204. return Mathf.FloorToInt(frameRate * seconds);
  205. }
  206. public static float ConvertFrameToTimeSeconds(int frame, float frameRate)
  207. {
  208. float num = 1f / frameRate;
  209. return (float)frame * num + num * 0.5f;
  210. }
  211. public static void DrawTexture(Rect screenRect, Texture texture, ScaleMode scaleMode, AlphaPacking alphaPacking, Material material)
  212. {
  213. if (Event.current.type == EventType.Repaint)
  214. {
  215. float num = (float)texture.width;
  216. float num2 = (float)texture.height;
  217. if (alphaPacking != AlphaPacking.LeftRight)
  218. {
  219. if (alphaPacking == AlphaPacking.TopBottom)
  220. {
  221. num2 *= 0.5f;
  222. }
  223. }
  224. else
  225. {
  226. num *= 0.5f;
  227. }
  228. float num3 = num / num2;
  229. Rect sourceRect = new Rect(0f, 0f, 1f, 1f);
  230. if (scaleMode != ScaleMode.ScaleAndCrop)
  231. {
  232. if (scaleMode != ScaleMode.ScaleToFit)
  233. {
  234. if (scaleMode != ScaleMode.StretchToFill)
  235. {
  236. }
  237. }
  238. else
  239. {
  240. float num4 = screenRect.width / screenRect.height;
  241. if (num4 > num3)
  242. {
  243. float num5 = num3 / num4;
  244. screenRect = new Rect(screenRect.xMin + screenRect.width * (1f - num5) * 0.5f, screenRect.yMin, num5 * screenRect.width, screenRect.height);
  245. }
  246. else
  247. {
  248. float num6 = num4 / num3;
  249. screenRect = new Rect(screenRect.xMin, screenRect.yMin + screenRect.height * (1f - num6) * 0.5f, screenRect.width, num6 * screenRect.height);
  250. }
  251. }
  252. }
  253. else
  254. {
  255. float num7 = screenRect.width / screenRect.height;
  256. if (num7 > num3)
  257. {
  258. float num8 = num3 / num7;
  259. sourceRect = new Rect(0f, (1f - num8) * 0.5f, 1f, num8);
  260. }
  261. else
  262. {
  263. float num9 = num7 / num3;
  264. sourceRect = new Rect(0.5f - num9 * 0.5f, 0f, num9, 1f);
  265. }
  266. }
  267. Graphics.DrawTexture(screenRect, texture, sourceRect, 0, 0, 0, 0, GUI.color, material);
  268. }
  269. }
  270. public static Texture2D GetReadableTexture(Texture inputTexture, bool requiresVerticalFlip, Orientation ori, Texture2D targetTexture)
  271. {
  272. Texture2D texture2D = targetTexture;
  273. RenderTexture active = RenderTexture.active;
  274. int width = inputTexture.width;
  275. int height = inputTexture.height;
  276. RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32);
  277. if (ori == Orientation.Landscape)
  278. {
  279. if (!requiresVerticalFlip)
  280. {
  281. Graphics.Blit(inputTexture, temporary);
  282. }
  283. else
  284. {
  285. GL.PushMatrix();
  286. RenderTexture.active = temporary;
  287. GL.LoadPixelMatrix(0f, (float)temporary.width, 0f, (float)temporary.height);
  288. Rect sourceRect = new Rect(0f, 0f, 1f, 1f);
  289. Rect screenRect = new Rect(0f, -1f, (float)temporary.width, (float)temporary.height);
  290. Graphics.DrawTexture(screenRect, inputTexture, sourceRect, 0, 0, 0, 0);
  291. GL.PopMatrix();
  292. GL.InvalidateState();
  293. }
  294. }
  295. if (texture2D == null)
  296. {
  297. texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
  298. }
  299. RenderTexture.active = temporary;
  300. texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0, false);
  301. texture2D.Apply(false, false);
  302. RenderTexture.ReleaseTemporary(temporary);
  303. RenderTexture.active = active;
  304. return texture2D;
  305. }
  306. private static int ParseTimeToMs(string text)
  307. {
  308. int result = 0;
  309. string[] array = text.Split(new char[]
  310. {
  311. ':',
  312. ','
  313. });
  314. if (array.Length == 4)
  315. {
  316. int num = int.Parse(array[0]);
  317. int num2 = int.Parse(array[1]);
  318. int num3 = int.Parse(array[2]);
  319. int num4 = int.Parse(array[3]);
  320. result = num4 + (num3 + (num2 + num * 60) * 60) * 1000;
  321. }
  322. return result;
  323. }
  324. public static List<Subtitle> LoadSubtitlesSRT(string data)
  325. {
  326. List<Subtitle> list = null;
  327. if (!string.IsNullOrEmpty(data))
  328. {
  329. data = data.Trim();
  330. Regex regex = new Regex("\n\r|\r\n|\n|\r");
  331. string[] array = regex.Split(data);
  332. if (array.Length >= 3)
  333. {
  334. list = new List<Subtitle>(256);
  335. int num = 0;
  336. int num2 = 0;
  337. Subtitle subtitle = null;
  338. for (int i = 0; i < array.Length; i++)
  339. {
  340. if (num2 == 0)
  341. {
  342. subtitle = new Subtitle();
  343. subtitle.index = num;
  344. }
  345. else if (num2 == 1)
  346. {
  347. string[] array2 = array[i].Split(new string[]
  348. {
  349. " --> "
  350. }, StringSplitOptions.RemoveEmptyEntries);
  351. if (array2.Length == 2)
  352. {
  353. subtitle.timeStartMs = Helper.ParseTimeToMs(array2[0]);
  354. subtitle.timeEndMs = Helper.ParseTimeToMs(array2[1]);
  355. }
  356. }
  357. else if (!string.IsNullOrEmpty(array[i]))
  358. {
  359. if (num2 == 2)
  360. {
  361. subtitle.text = array[i];
  362. }
  363. else
  364. {
  365. Subtitle subtitle2 = subtitle;
  366. subtitle2.text = subtitle2.text + "\n" + array[i];
  367. }
  368. }
  369. if (string.IsNullOrEmpty(array[i]) && num2 > 1)
  370. {
  371. list.Add(subtitle);
  372. num2 = 0;
  373. num++;
  374. subtitle = null;
  375. }
  376. else
  377. {
  378. num2++;
  379. }
  380. }
  381. if (subtitle != null)
  382. {
  383. list.Add(subtitle);
  384. }
  385. }
  386. else
  387. {
  388. Debug.LogWarning("[AVProVideo] SRT format doesn't appear to be valid");
  389. }
  390. }
  391. return list;
  392. }
  393. public const string ScriptVersion = "1.7.5";
  394. }
  395. }