Helper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProVideo
  8. {
  9. public static class Helper
  10. {
  11. public const string AVProVideoVersion = "2.6.6";
  12. public sealed class ExpectedPluginVersion
  13. {
  14. public const string Windows = "2.6.3";
  15. public const string WinRT = "2.6.3";
  16. public const string Android = "2.6.6";
  17. public const string Apple = "2.6.6";
  18. }
  19. public const string UnityBaseTextureName = "_MainTex";
  20. public const string UnityBaseTextureName_URP = "_BaseMap";
  21. public const string UnityBaseTextureName_HDRP = "_BaseColorMap";
  22. public static string GetPath(MediaPathType location)
  23. {
  24. string result = string.Empty;
  25. switch (location)
  26. {
  27. case MediaPathType.AbsolutePathOrURL:
  28. break;
  29. case MediaPathType.RelativeToDataFolder:
  30. result = Application.dataPath;
  31. break;
  32. case MediaPathType.RelativeToPersistentDataFolder:
  33. result = Application.persistentDataPath;
  34. break;
  35. case MediaPathType.RelativeToProjectFolder:
  36. #if !UNITY_WINRT_8_1
  37. string path = "..";
  38. #if UNITY_STANDALONE_OSX && !UNITY_EDITOR_OSX
  39. path += "/..";
  40. #endif
  41. result = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, path));
  42. result = result.Replace('\\', '/');
  43. #endif
  44. break;
  45. case MediaPathType.RelativeToStreamingAssetsFolder:
  46. result = Application.streamingAssetsPath;
  47. break;
  48. }
  49. return result;
  50. }
  51. public static string GetFilePath(string path, MediaPathType location)
  52. {
  53. string result = string.Empty;
  54. if (!string.IsNullOrEmpty(path))
  55. {
  56. switch (location)
  57. {
  58. case MediaPathType.AbsolutePathOrURL:
  59. result = path;
  60. break;
  61. case MediaPathType.RelativeToDataFolder:
  62. case MediaPathType.RelativeToPersistentDataFolder:
  63. case MediaPathType.RelativeToProjectFolder:
  64. case MediaPathType.RelativeToStreamingAssetsFolder:
  65. result = System.IO.Path.Combine(GetPath(location), path);
  66. break;
  67. }
  68. }
  69. return result;
  70. }
  71. public static string GetFriendlyResolutionName(int width, int height, float fps)
  72. {
  73. // List of common 16:9 resolutions
  74. int[] areas = { 0, 7680 * 4320, 3840 * 2160, 2560 * 1440, 1920 * 1080, 1280 * 720, 853 * 480, 640 * 360, 426 * 240, 256 * 144 };
  75. string[] names = { "Unknown", "8K", "4K", "1440p", "1080p", "720p", "480p", "360p", "240p", "144p" };
  76. Debug.Assert(areas.Length == names.Length);
  77. // Find the closest resolution
  78. int closestAreaIndex = 0;
  79. int area = width * height;
  80. int minDelta = int.MaxValue;
  81. for (int i = 0; i < areas.Length; i++)
  82. {
  83. int d = Mathf.Abs(areas[i] - area);
  84. // TODO: add a maximum threshold to ignore differences that are too high
  85. if (d < minDelta)
  86. {
  87. closestAreaIndex = i;
  88. minDelta = d;
  89. // If the exact mode is found, early out
  90. if (d == 0)
  91. {
  92. break;
  93. }
  94. }
  95. }
  96. string result = names[closestAreaIndex];
  97. // Append frame rate if valid
  98. if (fps > 0f && !float.IsNaN(fps))
  99. {
  100. result += fps.ToString("0.##");
  101. }
  102. return result;
  103. }
  104. public static string GetErrorMessage(ErrorCode code)
  105. {
  106. string result = string.Empty;
  107. switch (code)
  108. {
  109. case ErrorCode.None:
  110. result = "No Error";
  111. break;
  112. case ErrorCode.LoadFailed:
  113. result = "Loading failed. File not found, codec not supported, video resolution too high or insufficient system resources.";
  114. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
  115. // Add extra information for older Windows versions that don't have support for modern codecs
  116. if (SystemInfo.operatingSystem.StartsWith("Windows XP") ||
  117. SystemInfo.operatingSystem.StartsWith("Windows Vista"))
  118. {
  119. result += " 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.";
  120. }
  121. #endif
  122. break;
  123. case ErrorCode.DecodeFailed:
  124. result = "Decode failed. Possible codec not supported, video resolution/bit-depth too high, or insufficient system resources.";
  125. #if UNITY_ANDROID
  126. result += " On Android this is generally due to the hardware not having enough resources to decode the video. Most Android devices can only handle a maximum of one 4K video at once.";
  127. #endif
  128. break;
  129. }
  130. return result;
  131. }
  132. public static string GetPlatformName(Platform platform)
  133. {
  134. string result = "Unknown";
  135. switch (platform)
  136. {
  137. case Platform.WindowsUWP:
  138. result = "Windows UWP";
  139. break;
  140. case Platform.MacOSX:
  141. result = "macOS";
  142. break;
  143. default:
  144. result = platform.ToString();
  145. break;
  146. }
  147. return result;
  148. }
  149. public static string[] GetPlatformNames()
  150. {
  151. return new string[] {
  152. GetPlatformName(Platform.Windows),
  153. GetPlatformName(Platform.MacOSX),
  154. GetPlatformName(Platform.iOS),
  155. GetPlatformName(Platform.tvOS),
  156. GetPlatformName(Platform.Android),
  157. GetPlatformName(Platform.WindowsUWP),
  158. GetPlatformName(Platform.WebGL),
  159. };
  160. }
  161. #if AVPROVIDEO_DISABLE_LOGGING
  162. [System.Diagnostics.Conditional("ALWAYS_FALSE")]
  163. #endif
  164. public static void LogInfo(string message, Object context = null)
  165. {
  166. if (context == null)
  167. {
  168. Debug.Log("[AVProVideo] " + message);
  169. }
  170. else
  171. {
  172. Debug.Log("[AVProVideo] " + message, context);
  173. }
  174. }
  175. public static int GetUnityAudioSampleRate()
  176. {
  177. // For standalone builds (not in the editor):
  178. // In Unity 4.6, 5.0, 5.1 when audio is disabled there is no indication from the API.
  179. // But in 5.2.0 and above, it logs an error when trying to call
  180. // AudioSettings.GetDSPBufferSize() or AudioSettings.outputSampleRate
  181. // So to prevent the error, check if AudioSettings.GetConfiguration().sampleRate == 0
  182. return (AudioSettings.GetConfiguration().sampleRate == 0) ? 0 : AudioSettings.outputSampleRate;
  183. }
  184. public static int GetUnityAudioSpeakerCount()
  185. {
  186. switch (AudioSettings.GetConfiguration().speakerMode)
  187. {
  188. case AudioSpeakerMode.Mono: return 1;
  189. case AudioSpeakerMode.Stereo: return 2;
  190. case AudioSpeakerMode.Quad: return 4;
  191. case AudioSpeakerMode.Surround: return 5;
  192. case AudioSpeakerMode.Mode5point1: return 6;
  193. case AudioSpeakerMode.Mode7point1: return 8;
  194. case AudioSpeakerMode.Prologic: return 2;
  195. }
  196. return 0;
  197. }
  198. // Returns a valid range to use for a timeline display
  199. // Either it will return the range 0..duration, or
  200. // for live streams it will return first seekable..last seekable time
  201. public static TimeRange GetTimelineRange(double duration, TimeRanges seekable)
  202. {
  203. TimeRange result = new TimeRange();
  204. if (duration >= 0.0 && duration < 2e10)
  205. {
  206. // Duration is valid
  207. result.startTime = 0f;
  208. result.duration = duration;
  209. }
  210. else
  211. {
  212. // Duration is invalid, so it could be a live stream, so derive from seekable range
  213. result.startTime = seekable.MinTime;
  214. result.duration = seekable.Duration;
  215. }
  216. return result;
  217. }
  218. public const double SecondsToHNS = 10000000.0;
  219. public const double MilliSecondsToHNS = 10000.0;
  220. public static string GetTimeString(double timeSeconds, bool showMilliseconds = false)
  221. {
  222. float totalSeconds = (float)timeSeconds;
  223. int hours = Mathf.FloorToInt(totalSeconds / (60f * 60f));
  224. float usedSeconds = hours * 60f * 60f;
  225. int minutes = Mathf.FloorToInt((totalSeconds - usedSeconds) / 60f);
  226. usedSeconds += minutes * 60f;
  227. int seconds = Mathf.FloorToInt(totalSeconds - usedSeconds);
  228. string result;
  229. if (hours <= 0)
  230. {
  231. if (showMilliseconds)
  232. {
  233. int milliSeconds = (int)((totalSeconds - Mathf.Floor(totalSeconds)) * 1000f);
  234. result = string.Format("{0:00}:{1:00}:{2:000}", minutes, seconds, milliSeconds);
  235. }
  236. else
  237. {
  238. result = string.Format("{0:00}:{1:00}", minutes, seconds);
  239. }
  240. }
  241. else
  242. {
  243. if (showMilliseconds)
  244. {
  245. int milliSeconds = (int)((totalSeconds - Mathf.Floor(totalSeconds)) * 1000f);
  246. result = string.Format("{2}:{0:00}:{1:00}:{3:000}", minutes, seconds, hours, milliSeconds);
  247. }
  248. else
  249. {
  250. result = string.Format("{2}:{0:00}:{1:00}", minutes, seconds, hours);
  251. }
  252. }
  253. return result;
  254. }
  255. /// <summary>
  256. /// Convert texture transform matrix to an enum of orientation types
  257. /// </summary>
  258. public static Orientation GetOrientation(float[] t)
  259. {
  260. Orientation result = Orientation.Landscape;
  261. if (t != null)
  262. {
  263. // TODO: check that the Portrait and PortraitFlipped are the right way around
  264. if (t[0] == 0f && t[1]== 1f && t[2] == -1f && t[3] == 0f)
  265. {
  266. result = Orientation.Portrait;
  267. } else
  268. if (t[0] == 0f && t[1] == -1f && t[2] == 1f && t[3] == 0f)
  269. {
  270. result = Orientation.PortraitFlipped;
  271. } else
  272. if (t[0]== 1f && t[1] == 0f && t[2] == 0f && t[3] == 1f)
  273. {
  274. result = Orientation.Landscape;
  275. } else
  276. if (t[0] == -1f && t[1] == 0f && t[2] == 0f && t[3] == -1f)
  277. {
  278. result = Orientation.LandscapeFlipped;
  279. }
  280. else
  281. if (t[0] == 0f && t[1] == 1f && t[2] == 1f && t[3] == 0f)
  282. {
  283. result = Orientation.PortraitHorizontalMirror;
  284. }
  285. }
  286. return result;
  287. }
  288. private static Matrix4x4 PortraitMatrix = Matrix4x4.TRS(new Vector3(0f, 1f, 0f), Quaternion.Euler(0f, 0f, -90f), Vector3.one);
  289. private static Matrix4x4 PortraitFlippedMatrix = Matrix4x4.TRS(new Vector3(1f, 0f, 0f), Quaternion.Euler(0f, 0f, 90f), Vector3.one);
  290. private static Matrix4x4 LandscapeFlippedMatrix = Matrix4x4.TRS(new Vector3(0f, 1f, 0f), Quaternion.Euler(0f, 0f, -90f), Vector3.one);
  291. public static Matrix4x4 GetMatrixForOrientation(Orientation ori)
  292. {
  293. Matrix4x4 result;
  294. switch (ori)
  295. {
  296. case Orientation.Landscape:
  297. result = Matrix4x4.identity;
  298. break;
  299. case Orientation.LandscapeFlipped:
  300. result = LandscapeFlippedMatrix;
  301. break;
  302. case Orientation.Portrait:
  303. result = PortraitMatrix;
  304. break;
  305. case Orientation.PortraitFlipped:
  306. result = PortraitFlippedMatrix;
  307. break;
  308. case Orientation.PortraitHorizontalMirror:
  309. result = new Matrix4x4();
  310. result.SetColumn(0, new Vector4(0f, 1f, 0f, 0f));
  311. result.SetColumn(1, new Vector4(1f, 0f, 0f, 0f));
  312. result.SetColumn(2, new Vector4(0f, 0f, 1f, 0f));
  313. result.SetColumn(3, new Vector4(0f, 0f, 0f, 1f));
  314. break;
  315. default:
  316. throw new System.Exception("Unknown Orientation type");
  317. }
  318. return result;
  319. }
  320. public static int ConvertTimeSecondsToFrame(double seconds, float frameRate)
  321. {
  322. // NOTE: Generally you should use RountToInt when converting from time to frame number
  323. // but because we're adding a half frame offset (which seems to be the safer thing to do) we need to FloorToInt
  324. seconds = System.Math.Max(0.0, seconds);
  325. frameRate = Mathf.Max(0f, frameRate);
  326. return (int)System.Math.Floor(frameRate * seconds);
  327. }
  328. public static double ConvertFrameToTimeSeconds(int frame, float frameRate)
  329. {
  330. frame = Mathf.Max(0, frame);
  331. frameRate = Mathf.Max(0f, frameRate);
  332. double frameDurationSeconds = 1.0 / frameRate;
  333. return ((double)frame * frameDurationSeconds) + (frameDurationSeconds * 0.5); // Add half a frame we that the time lands in the middle of the frame range and not at the edges
  334. }
  335. public static double FindNextKeyFrameTimeSeconds(double seconds, float frameRate, int keyFrameInterval)
  336. {
  337. seconds = System.Math.Max(0.0, seconds);
  338. frameRate = Mathf.Max(0f, frameRate);
  339. keyFrameInterval = Mathf.Max(0, keyFrameInterval);
  340. int currentFrame = Helper.ConvertTimeSecondsToFrame(seconds, frameRate);
  341. // TODO: allow specifying a minimum number of frames so that if currentFrame is too close to nextKeyFrame, it will calculate the next-next keyframe
  342. int nextKeyFrame = keyFrameInterval * Mathf.CeilToInt((float)(currentFrame + 1) / (float)keyFrameInterval);
  343. return Helper.ConvertFrameToTimeSeconds(nextKeyFrame, frameRate);
  344. }
  345. public static System.DateTime ConvertSecondsSince1970ToDateTime(double secondsSince1970)
  346. {
  347. System.TimeSpan time = System.TimeSpan.FromSeconds(secondsSince1970);
  348. return new System.DateTime(1970, 1, 1).Add(time);
  349. }
  350. #if (UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN))
  351. [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError=true)]
  352. private static extern int GetShortPathName([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pathName,
  353. [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] System.Text.StringBuilder shortName,
  354. int cbShortName);
  355. // Handle very long file paths by converting to DOS 8.3 format
  356. internal static string ConvertLongPathToShortDOS83Path(string path)
  357. {
  358. const string pathToken = @"\\?\";
  359. string result = pathToken + path.Replace("/","\\");
  360. int length = GetShortPathName(result, null, 0);
  361. if (length > 0)
  362. {
  363. System.Text.StringBuilder sb = new System.Text.StringBuilder(length);
  364. if (0 != GetShortPathName(result, sb, length))
  365. {
  366. result = sb.ToString().Replace(pathToken, "");
  367. Debug.LogWarning("[AVProVideo] Long path detected. Changing to DOS 8.3 format");
  368. }
  369. }
  370. return result;
  371. }
  372. #endif
  373. // Converts a non-readable texture to a readable Texture2D.
  374. // "targetTexture" can be null or you can pass in an existing texture.
  375. // Remember to Destroy() the returned texture after finished with it
  376. public static Texture2D GetReadableTexture(Texture inputTexture, bool requiresVerticalFlip, Orientation ori, Texture2D targetTexture = null)
  377. {
  378. Texture2D resultTexture = targetTexture;
  379. RenderTexture prevRT = RenderTexture.active;
  380. int textureWidth = inputTexture.width;
  381. int textureHeight = inputTexture.height;
  382. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS
  383. if (ori == Orientation.Portrait || ori == Orientation.PortraitFlipped)
  384. {
  385. textureWidth = inputTexture.height;
  386. textureHeight = inputTexture.width;
  387. }
  388. #endif
  389. // Blit the texture to a temporary RenderTexture
  390. // This handles any format conversion that is required and allows us to use ReadPixels to copy texture from RT to readable texture
  391. RenderTexture tempRT = RenderTexture.GetTemporary(textureWidth, textureHeight, 0, RenderTextureFormat.ARGB32);
  392. if (ori == Orientation.Landscape)
  393. {
  394. if (!requiresVerticalFlip)
  395. {
  396. Graphics.Blit(inputTexture, tempRT);
  397. }
  398. else
  399. {
  400. // The above Blit can't flip unless using a material, so we use Graphics.DrawTexture instead
  401. GL.PushMatrix();
  402. RenderTexture.active = tempRT;
  403. GL.LoadPixelMatrix(0f, tempRT.width, 0f, tempRT.height);
  404. Rect sourceRect = new Rect(0f, 0f, 1f, 1f);
  405. // NOTE: not sure why we need to set y to -1, without this there is a 1px gap at the bottom
  406. Rect destRect = new Rect(0f, -1f, tempRT.width, tempRT.height);
  407. Graphics.DrawTexture(destRect, inputTexture, sourceRect, 0, 0, 0, 0);
  408. GL.PopMatrix();
  409. GL.InvalidateState();
  410. }
  411. }
  412. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IPHONE || UNITY_IOS || UNITY_TVOS
  413. else
  414. {
  415. Matrix4x4 m = Matrix4x4.identity;
  416. switch (ori)
  417. {
  418. case Orientation.Portrait:
  419. m = Matrix4x4.TRS(new Vector3(0f, inputTexture.width, 0f), Quaternion.Euler(0f, 0f, -90f), Vector3.one);
  420. break;
  421. case Orientation.PortraitFlipped:
  422. m = Matrix4x4.TRS(new Vector3(inputTexture.height, 0f, 0f), Quaternion.Euler(0f, 0f, 90f), Vector3.one);
  423. break;
  424. case Orientation.LandscapeFlipped:
  425. m = Matrix4x4.TRS(new Vector3(inputTexture.width, inputTexture.height, 0f), Quaternion.identity, new Vector3(-1f, -1f, 1f));
  426. break;
  427. }
  428. // The above Blit can't flip unless using a material, so we use Graphics.DrawTexture instead
  429. GL.InvalidateState();
  430. GL.PushMatrix();
  431. GL.Clear(false, true, Color.red);
  432. RenderTexture.active = tempRT;
  433. GL.LoadPixelMatrix(0f, tempRT.width, 0f, tempRT.height);
  434. Rect sourceRect = new Rect(0f, 0f, 1f, 1f);
  435. // NOTE: not sure why we need to set y to -1, without this there is a 1px gap at the bottom
  436. Rect destRect = new Rect(0f, -1f, inputTexture.width, inputTexture.height);
  437. GL.MultMatrix(m);
  438. Graphics.DrawTexture(destRect, inputTexture, sourceRect, 0, 0, 0, 0);
  439. GL.PopMatrix();
  440. GL.InvalidateState();
  441. }
  442. #endif
  443. if (resultTexture == null)
  444. {
  445. resultTexture = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false);
  446. }
  447. RenderTexture.active = tempRT;
  448. resultTexture.ReadPixels(new Rect(0f, 0f, textureWidth, textureHeight), 0, 0, false);
  449. resultTexture.Apply(false, false);
  450. RenderTexture.ReleaseTemporary(tempRT);
  451. RenderTexture.active = prevRT;
  452. return resultTexture;
  453. }
  454. // Converts a non-readable texture to a readable Texture2D.
  455. // "targetTexture" can be null or you can pass in an existing texture.
  456. // Remember to Destroy() the returned texture after finished with it
  457. public static Texture2D GetReadableTexture(RenderTexture inputTexture, Texture2D targetTexture = null)
  458. {
  459. if (targetTexture == null)
  460. {
  461. targetTexture = new Texture2D(inputTexture.width, inputTexture.height, TextureFormat.ARGB32, false);
  462. }
  463. RenderTexture prevRT = RenderTexture.active;
  464. RenderTexture.active = inputTexture;
  465. targetTexture.ReadPixels(new Rect(0f, 0f, inputTexture.width, inputTexture.height), 0, 0, false);
  466. targetTexture.Apply(false, false);
  467. RenderTexture.active = prevRT;
  468. return targetTexture;
  469. }
  470. }
  471. }