using System.IO; using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; namespace UMP { public class MediaPlayerHelper { private const string UMP_FOLDER_NAME = "/UniversalMediaPlayer"; private const string LOCAL_FILE_ROOT = "file:///"; private delegate void ManageLogCallback(string msg, ManageLogLevel level); private ManageLogCallback _manageLogCallback; private static Regex _androidStorageRoot = new Regex("(^\\/.*)Android"); private enum ManageLogLevel { DEBUG = 0, WARNING = 1, ERROR = 2 }; private void DebugLogHandler(string msg, ManageLogLevel level) { Debug.LogError(msg); } /// /// Apply texture to Unity game objects that has 'RawImage' or 'MeshRenderer' component /// /// Texture to render video output /// Game objects where will be rendering video output /// public static void ApplyTextureToRenderingObjects(Texture2D texture, GameObject[] renderingObjects) { if (renderingObjects == null) return; foreach (var gameObject in renderingObjects) { if (gameObject == null) continue; var rawImage = gameObject.GetComponent(); if (rawImage != null) { rawImage.texture = texture; continue; } var meshRenderer = gameObject.GetComponent(); if (meshRenderer != null && meshRenderer.material != null) meshRenderer.material.mainTexture = texture; else Debug.LogError(gameObject.name + ": don't have 'RawImage' or 'MeshRenderer' component - ignored"); } } /// /// Generate correct texture for current runtime platform /// /// Width in pixels /// Height in pixels /// public static Texture2D GenVideoTexture(int width, int height) { if (UMPSettings.RuntimePlatform == UMPSettings.Platforms.Win || UMPSettings.RuntimePlatform == UMPSettings.Platforms.Mac || UMPSettings.RuntimePlatform == UMPSettings.Platforms.Linux) return new Texture2D(width, height, TextureFormat.BGRA32, false); return new Texture2D(width, height, TextureFormat.RGBA32, false); } /// /// Generate correct texture for native plugin /// /// Width in pixels /// Height in pixels /// internal static Texture2D GenPluginTexture(int width, int height) { if (UMPSettings.RuntimePlatform == UMPSettings.Platforms.Win || UMPSettings.RuntimePlatform == UMPSettings.Platforms.Mac || UMPSettings.RuntimePlatform == UMPSettings.Platforms.Linux) return new Texture2D(width, height, TextureFormat.BGRA32, false); return new Texture2D(width, height, TextureFormat.RGBA32, false); } /// /// Getting average color from frame buffer array /// /// public static Color GetAverageColor(byte[] frameBuffer) { if (frameBuffer == null) return Color.black; long redBucket = 0; long greenBucket = 0; long blueBucket = 0; long alphaBucket = 0; int pixelCount = frameBuffer.Length / 4; if (pixelCount <= 0 || pixelCount % 4 != 0) return Color.black; for (int x = 0; x < frameBuffer.Length; x+=4) { redBucket += frameBuffer[x]; greenBucket += frameBuffer[x + 1]; blueBucket += frameBuffer[x + 2]; alphaBucket += frameBuffer[x + 3]; } return new Color(redBucket / pixelCount, greenBucket / pixelCount, blueBucket / pixelCount, alphaBucket / pixelCount); } /// /// Getting colors from frame buffer array /// /// public static Color32[] GetFrameColors(byte[] frameBuffer) { var colorsArray = new Color32[frameBuffer.Length / 4]; for (var i = 0; i < frameBuffer.Length; i += 4) { var color = new Color32(frameBuffer[i + 2], frameBuffer[i + 1], frameBuffer[i + 0], frameBuffer[i + 3]); colorsArray[i / 4] = color; } return colorsArray; } /// /// Getting root storage menory for current platform /// Windows, Mac OS, Linux will return path to project folder /// Android, iOS will return root of internal/external memory root /// /// public static string GetDeviceRootPath() { Match match = _androidStorageRoot.Match(Application.persistentDataPath); if (match.Length > 1) return match.Groups[1].Value; return Application.persistentDataPath; } /// /// Check if file exists in 'StreamingAssets' folder /// /// File name or path to file in 'StreamingAssets' folder /// public static bool IsAssetsFile(string filePath) { if (filePath.StartsWith(LOCAL_FILE_ROOT)) filePath = filePath.Substring(LOCAL_FILE_ROOT.Length); if (!filePath.Contains(Application.streamingAssetsPath)) filePath = Path.Combine(Application.streamingAssetsPath, filePath); if (UMPSettings.RuntimePlatform != UMPSettings.Platforms.Android) { return File.Exists(filePath); } else { #if UNITY_2017_2_OR_NEWER var www = UnityWebRequest.Get(filePath); www.SendWebRequest(); while (!www.isDone && www.downloadProgress <= 0) { } #else var www = new WWW(filePath); while (!www.isDone && www.progress <= 0) { } #endif bool result = string.IsNullOrEmpty(www.error); www.Dispose(); return result; } } /// /// Get correct madia data source path for file path /// /// Relative path to the data file /// public static string GetDataSourcePath(string relativePath) { if (relativePath.StartsWith(LOCAL_FILE_ROOT)) { relativePath = relativePath.Substring(LOCAL_FILE_ROOT.Length); if (IsAssetsFile(relativePath)) relativePath = Path.Combine(Application.streamingAssetsPath, relativePath); } if (UMPSettings.RuntimePlatform == UMPSettings.Platforms.Android) { var pathDR = GetDeviceRootPath() + relativePath; if (File.Exists(pathDR)) relativePath = pathDR; } if (File.Exists(relativePath)) relativePath = LOCAL_FILE_ROOT + relativePath; return relativePath; } } }