MediaReferenceEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace RenderHeads.Media.AVProVideo.Editor
  6. {
  7. [CanEditMultipleObjects()]
  8. [CustomEditor(typeof(MediaReference))]
  9. public class MediaReferenceEditor : UnityEditor.Editor
  10. {
  11. internal const string SettingsPrefix = "AVProVideo-MediaReferenceEditor-";
  12. private SerializedProperty _propMediaPath;
  13. private SerializedProperty _propHints;
  14. private SerializedProperty _propPlatformMacOS;
  15. private SerializedProperty _propPlatformWindows;
  16. private SerializedProperty _propPlatformAndroid;
  17. private SerializedProperty _propPlatformIOS;
  18. private SerializedProperty _propPlatformTvOS;
  19. private SerializedProperty _propPlatformWindowsUWP;
  20. private SerializedProperty _propPlatformWebGL;
  21. //private SerializedProperty _propAlias;
  22. void OnEnable()
  23. {
  24. _propMediaPath = this.CheckFindProperty("_mediaPath");
  25. _propHints = this.CheckFindProperty("_hints");
  26. _propPlatformMacOS = this.CheckFindProperty("_macOS");
  27. _propPlatformWindows = this.CheckFindProperty("_windows");
  28. _propPlatformAndroid = this.CheckFindProperty("_android");
  29. _propPlatformIOS = this.CheckFindProperty("_iOS");
  30. _propPlatformTvOS = this.CheckFindProperty("_tvOS");
  31. _propPlatformWindowsUWP = this.CheckFindProperty("_windowsUWP");
  32. _propPlatformWebGL = this.CheckFindProperty("_webGL");
  33. //_propAlias = CheckFindProperty("_alias");
  34. _zoomToFill = EditorPrefs.GetBool(SettingsPrefix + "ZoomToFill", _zoomToFill);
  35. _thumbnailTime = EditorPrefs.GetFloat(SettingsPrefix + "ThumbnailTime", _thumbnailTime);
  36. }
  37. void OnDisable()
  38. {
  39. EndGenerateThumbnails(false);
  40. RemoveProgress();
  41. EditorPrefs.SetBool(SettingsPrefix + "ZoomToFill", _zoomToFill);
  42. EditorPrefs.SetFloat(SettingsPrefix + "ThumbnailTime", _thumbnailTime);
  43. }
  44. public override void OnInspectorGUI()
  45. {
  46. //MediaPlayer media = (this.target) as MediaPlayer;
  47. //this.DrawDefaultInspector();
  48. serializedObject.Update();
  49. GUILayout.Label("Media Reference");
  50. EditorGUILayout.Space();
  51. //EditorGUILayout.PropertyField(_propAlias);
  52. //EditorGUILayout.Space();
  53. {
  54. string mediaName = _propMediaPath.FindPropertyRelative("_path").stringValue;
  55. GUILayout.BeginVertical(GUI.skin.box);
  56. MediaPlayerEditor.OnInspectorGUI_CopyableFilename(mediaName);
  57. GUILayout.EndVertical();
  58. }
  59. EditorGUILayout.PropertyField(_propMediaPath);
  60. MediaPathDrawer.ShowBrowseButton(_propMediaPath);
  61. EditorGUILayout.Space();
  62. //GUILayout.Label("Media Hints", EditorStyles.boldLabel);
  63. EditorGUILayout.PropertyField(_propHints);
  64. EditorGUILayout.PropertyField(_propPlatformMacOS, new GUIContent("macOS"));
  65. EditorGUILayout.PropertyField(_propPlatformWindows, new GUIContent("Windows"));
  66. EditorGUILayout.PropertyField(_propPlatformAndroid, new GUIContent("Android"));
  67. EditorGUILayout.PropertyField(_propPlatformIOS, new GUIContent("iOS"));
  68. EditorGUILayout.PropertyField(_propPlatformTvOS, new GUIContent("tvOS"));
  69. EditorGUILayout.PropertyField(_propPlatformWindowsUWP, new GUIContent("UWP"));
  70. EditorGUILayout.PropertyField(_propPlatformWebGL, new GUIContent("WebGL"));
  71. EditorGUILayout.Space();
  72. EditorGUILayout.Space();
  73. serializedObject.ApplyModifiedProperties();
  74. bool beginGenerateThumbnails = false;
  75. GUILayout.FlexibleSpace();
  76. EditorGUI.BeginDisabledGroup(IsGeneratingThumbnails());
  77. GUILayout.BeginHorizontal();
  78. _thumbnailTime = GUILayout.HorizontalSlider(_thumbnailTime, 0f, 1f, GUILayout.ExpandWidth(true));
  79. _zoomToFill = GUILayout.Toggle(_zoomToFill, "Zoom And Crop", GUI.skin.button, GUILayout.ExpandWidth(false));
  80. GUILayout.EndHorizontal();
  81. if (GUILayout.Button("Generate Thumbnail"))
  82. {
  83. beginGenerateThumbnails = true;
  84. }
  85. EditorGUI.EndDisabledGroup();
  86. if (beginGenerateThumbnails)
  87. {
  88. BeginGenerateThumbnails();
  89. }
  90. if (IsGeneratingThumbnails())
  91. {
  92. ShowProgress();
  93. }
  94. if (!IsGeneratingThumbnails())
  95. {
  96. RemoveProgress();
  97. }
  98. }
  99. private void ShowProgress()
  100. {
  101. // Show cancellable progress
  102. float t = (float)_targetIndex / (float)this.targets.Length;
  103. t = 0.25f + t * 0.75f;
  104. MediaReference media = (this.targets[_targetIndex]) as MediaReference;
  105. #if UNITY_2020_1_OR_NEWER
  106. if (_progressId < 0)
  107. {
  108. //Progress.RegisterCancelCallback(_progressId...)
  109. _progressId = Progress.Start("[AVProVideo] Generating Thumbnails...", null, Progress.Options.Managed);
  110. }
  111. Progress.Report(_progressId, t, media.MediaPath.Path);
  112. #else
  113. if (EditorUtility.DisplayCancelableProgressBar("[AVProVideo] Generating Thumbnails...", media.MediaPath.Path, t))
  114. {
  115. EndGenerateThumbnails(false);
  116. }
  117. #endif
  118. }
  119. private void RemoveProgress()
  120. {
  121. #if UNITY_2020_1_OR_NEWER
  122. if (_progressId >= 0)
  123. {
  124. Progress.Remove(_progressId);
  125. _progressId = -1;
  126. }
  127. #else
  128. EditorUtility.ClearProgressBar();
  129. #endif
  130. }
  131. #if UNITY_2020_1_OR_NEWER
  132. private int _progressId = -1;
  133. #endif
  134. private float _thumbnailTime;
  135. private bool _zoomToFill = false;
  136. private int _lastFrame;
  137. private BaseMediaPlayer _thumbnailPlayer;
  138. private int _mediaFrame = -1;
  139. private int _targetIndex = 0;
  140. private float _timeoutTimer = 0f;
  141. private bool IsGeneratingThumbnails()
  142. {
  143. return (_thumbnailPlayer != null);
  144. }
  145. private void BeginGenerateThumbnails()
  146. {
  147. EditorApplication.update -= UpdateGenerateThumbnail;
  148. Debug.Assert(_thumbnailPlayer == null);
  149. #if UNITY_EDITOR_WIN
  150. if (WindowsMediaPlayer.InitialisePlatform())
  151. {
  152. MediaPlayer.OptionsWindows options = new MediaPlayer.OptionsWindows();
  153. _thumbnailPlayer = new WindowsMediaPlayer(options);
  154. }
  155. #elif UNITY_EDITOR_OSX
  156. {
  157. MediaPlayer.OptionsApple options = new MediaPlayer.OptionsApple(MediaPlayer.OptionsApple.TextureFormat.BGRA, MediaPlayer.OptionsApple.Flags.None);
  158. _thumbnailPlayer = new AppleMediaPlayer(options);
  159. }
  160. #endif
  161. if (_thumbnailPlayer != null)
  162. {
  163. _targetIndex = 0;
  164. BeginNextThumbnail(0);
  165. }
  166. else
  167. {
  168. EndGenerateThumbnails(false);
  169. }
  170. }
  171. private void BeginNextThumbnail(int index)
  172. {
  173. EditorApplication.update -= UpdateGenerateThumbnail;
  174. _mediaFrame = -1;
  175. _timeoutTimer = 0f;
  176. if (_thumbnailPlayer != null)
  177. {
  178. if (index < this.targets.Length)
  179. {
  180. _targetIndex = index;
  181. MediaReference media = (this.targets[_targetIndex]) as MediaReference;
  182. string path = media.MediaPath.GetResolvedFullPath();
  183. bool openedMedia = false;
  184. if (!string.IsNullOrEmpty(path))
  185. {
  186. if (_thumbnailPlayer.OpenMedia(path, 0, string.Empty, media.Hints, 0, false))
  187. {
  188. openedMedia = true;
  189. EditorApplication.update += UpdateGenerateThumbnail;
  190. }
  191. }
  192. if (!openedMedia)
  193. {
  194. // If the media failed to open, continue to the next one
  195. BeginNextThumbnail(_targetIndex + 1);
  196. }
  197. }
  198. else
  199. {
  200. EndGenerateThumbnails(true);
  201. }
  202. }
  203. }
  204. private void EndGenerateThumbnails(bool updateAssets)
  205. {
  206. EditorApplication.update -= UpdateGenerateThumbnail;
  207. if (_thumbnailPlayer != null)
  208. {
  209. _thumbnailPlayer.CloseMedia();
  210. _thumbnailPlayer.Dispose();
  211. _thumbnailPlayer = null;
  212. }
  213. _mediaFrame = -1;
  214. if (updateAssets)
  215. {
  216. // This forces the static preview to refresh
  217. foreach (Object o in this.targets)
  218. {
  219. EditorUtility.SetDirty(o);
  220. AssetPreview.GetAssetPreview(o);
  221. }
  222. AssetDatabase.SaveAssets();
  223. }
  224. }
  225. private void UpdateGenerateThumbnail()
  226. {
  227. if (Time.renderedFrameCount == _lastFrame)
  228. {
  229. // In at least Unity 5.6 we have to force refresh of the UI otherwise the render thread doesn't run to update the textures
  230. this.Repaint();
  231. UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
  232. return;
  233. }
  234. // Wait for a frame to be rendered
  235. Debug.Assert(_thumbnailPlayer != null);
  236. if (_thumbnailPlayer != null)
  237. {
  238. _timeoutTimer += Time.unscaledDeltaTime;
  239. bool nextVideo = false;
  240. _thumbnailPlayer.Update();
  241. _thumbnailPlayer.Render();
  242. if (_mediaFrame < 0 && _thumbnailPlayer.CanPlay())
  243. {
  244. _thumbnailPlayer.MuteAudio(true);
  245. _thumbnailPlayer.Play();
  246. _thumbnailPlayer.Seek(_thumbnailPlayer.GetDuration() * _thumbnailTime);
  247. _mediaFrame = _thumbnailPlayer.GetTextureFrameCount();
  248. }
  249. if (_thumbnailPlayer.GetTexture() != null)
  250. {
  251. if (_mediaFrame != _thumbnailPlayer.GetTextureFrameCount() && _thumbnailPlayer.GetTextureFrameCount() > 3)
  252. {
  253. bool prevSRGB = GL.sRGBWrite;
  254. GL.sRGBWrite = false;
  255. RenderTexture rt2 = null;
  256. // TODO: move this all into VideoRender as a resolve method
  257. {
  258. Material materialResolve = new Material(Shader.Find(VideoRender.Shader_Resolve));
  259. VideoRender.SetupVerticalFlipMaterial(materialResolve, _thumbnailPlayer.RequiresVerticalFlip());
  260. VideoRender.SetupAlphaPackedMaterial(materialResolve, _thumbnailPlayer.GetTextureAlphaPacking());
  261. VideoRender.SetupGammaMaterial(materialResolve, !_thumbnailPlayer.PlayerSupportsLinearColorSpace());
  262. RenderTexture prev = RenderTexture.active;
  263. // Scale to fit and downsample
  264. rt2 = RenderTexture.GetTemporary(128, 128, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
  265. RenderTexture.active = rt2;
  266. GL.Clear(false, true, new Color(0f, 0f, 0f, 0f));
  267. ScaleMode scaleMode = ScaleMode.ScaleToFit;
  268. if (_zoomToFill)
  269. {
  270. scaleMode = ScaleMode.ScaleAndCrop;
  271. }
  272. VideoRender.DrawTexture(new Rect(0f, 0f, 128f, 128f), _thumbnailPlayer.GetTexture(), scaleMode, _thumbnailPlayer.GetTextureAlphaPacking(), _thumbnailPlayer.GetTexturePixelAspectRatio(), materialResolve);
  273. RenderTexture.active = prev;
  274. Material.DestroyImmediate(materialResolve); materialResolve = null;
  275. }
  276. Texture2D readTexture = new Texture2D(128, 128, TextureFormat.RGBA32, true, false);
  277. Helper.GetReadableTexture(rt2, readTexture);
  278. MediaReference mediaRef = (this.targets[_targetIndex]) as MediaReference;
  279. mediaRef.GeneratePreview(readTexture);
  280. DestroyImmediate(readTexture); readTexture = null;
  281. RenderTexture.ReleaseTemporary(rt2);
  282. GL.sRGBWrite = prevSRGB;
  283. nextVideo = true;
  284. Debug.Log("Thumbnail Written");
  285. }
  286. }
  287. if (!nextVideo)
  288. {
  289. // If there is an error or it times out, then skip this media
  290. if (_timeoutTimer > 10f || _thumbnailPlayer.GetLastError() != ErrorCode.None)
  291. {
  292. MediaReference mediaRef = (this.targets[_targetIndex]) as MediaReference;
  293. mediaRef.GeneratePreview(null);
  294. nextVideo = true;
  295. }
  296. }
  297. if (nextVideo)
  298. {
  299. BeginNextThumbnail(_targetIndex + 1);
  300. }
  301. }
  302. _lastFrame = Time.renderedFrameCount;
  303. }
  304. public override bool HasPreviewGUI()
  305. {
  306. return true;
  307. }
  308. public override void OnPreviewGUI(Rect r, GUIStyle background)
  309. {
  310. Texture texture = RenderStaticPreview(string.Empty, null, 128, 128);
  311. if (texture)
  312. {
  313. GUI.DrawTexture(r, texture, ScaleMode.ScaleToFit, true);
  314. }
  315. }
  316. public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
  317. {
  318. MediaReference media = this.target as MediaReference;
  319. if (media)
  320. {
  321. bool isLinear = false;
  322. #if !UNITY_2018_1_OR_NEWER
  323. // NOTE: These older versions of Unity don't handle sRGB in the editor correctly so a workaround is to create texture as linear
  324. isLinear = true;
  325. #endif
  326. Texture2D result = new Texture2D(width, height, TextureFormat.RGBA32, true, isLinear);
  327. if (!media.GetPreview(result))
  328. {
  329. DestroyImmediate(result); result = null;
  330. }
  331. return result;
  332. }
  333. return null;
  334. }
  335. }
  336. }