MoviePlayerBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //--------------------------------------------
  2. // Movie Player
  3. // Copyright © 2014 SHUU Games
  4. //--------------------------------------------
  5. using UnityEngine;
  6. using MP;
  7. public abstract class MoviePlayerBase : MonoBehaviour
  8. {
  9. #region ----- Public properties ------
  10. /// <summary>
  11. /// Video decoder target texture that is created when movie is loaded.
  12. /// </summary>
  13. public Texture2D framebuffer;
  14. /// <summary>
  15. /// Audio decoder target buffer that is used ONLY if there is no audioSource set.
  16. /// </summary>
  17. public AudioClip audiobuffer;
  18. /// <summary>
  19. /// If TRUE then the framebuffer is drawn directly
  20. /// onto the screen in OnGUI method.
  21. /// </summary>
  22. public bool drawToScreen;
  23. [System.Obsolete("Use property named material instead, it works exactly like otherMaterial did. In later version otherMaterial will be removed")]
  24. public Material otherMaterial;
  25. /// <summary>
  26. /// A material to assign the framebuffer to (optional).
  27. /// If using drawToScreen option, then this material is used.
  28. /// </summary>
  29. public Material material;
  30. /// <summary>
  31. /// Material texture property name
  32. /// </summary>
  33. public string texturePropertyName = "_MainTex";
  34. public enum ScreenMode
  35. {
  36. Crop,
  37. Fill,
  38. Stretch,
  39. CustomRect
  40. }
  41. /// <summary>
  42. /// How exactly should we draw the framebuffer onto the screen.
  43. /// </summary>
  44. public ScreenMode screenMode;
  45. /// <summary>
  46. /// GUI.depth for DrawTexture when drawing onto the screen.
  47. /// </summary>
  48. public int screenGuiDepth;
  49. /// <summary>
  50. /// If ScreenMode.CustomRect, then this is the rect.
  51. /// </summary>
  52. public Rect customScreenRect = new Rect(0, 0, 100, 100);
  53. /// <summary>
  54. /// Set to TRUE to play and FALSE for pause the movie
  55. /// </summary>
  56. public bool play;
  57. /// <summary>
  58. /// Currently loaded movie.
  59. /// </summary>
  60. public Movie movie;
  61. /// <summary>
  62. /// Frame count defining max lag between audio and video stream.
  63. /// If the audio and video streams are synchronized too often
  64. /// (usually clicking sounds in audio), then increasing this value may help.
  65. /// </summary>
  66. public int maxSyncErrorFrames = 2;
  67. public delegate void MovieEvent (MoviePlayerBase caller);
  68. /// <summary>
  69. /// Called right before starting the playback.
  70. /// The movie is loaded and ready.
  71. /// </summary>
  72. public event MovieEvent OnPlay;
  73. /// <summary>
  74. /// Called right after the movie has stopped.
  75. /// </summary>
  76. public event MovieEvent OnStop;
  77. /// <summary>
  78. /// Frame drop count.
  79. /// </summary>
  80. public int framesSkipped { get { return _framesDropped; } }
  81. /// <summary>
  82. /// Audio and video stream sync events
  83. /// </summary>
  84. /// <value>The sync events.</value>
  85. public int syncEvents { get { return _syncEvents; } }
  86. /// <summary>
  87. /// Gets the video stream framerate.
  88. ///
  89. /// See movie.demux.videoStreamInfo and movie.demux.audioStreamInfo for accessing all stream info.
  90. /// </summary>
  91. public float framerate {
  92. get {
  93. return (movie != null && movie.demux != null && movie.demux.videoStreamInfo != null)
  94. ? movie.demux.videoStreamInfo.framerate : 30;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the video stream length. Total playback time is always the video stream length even if
  99. /// audio stream in a movie is shorter or longer.
  100. ///
  101. /// See movie.demux.videoStreamInfo and movie.demux.audioStreamInfo for accessing all stream info.
  102. /// </summary>
  103. public float lengthSeconds {
  104. get {
  105. return (movie != null && movie.demux != null && movie.demux.videoStreamInfo != null)
  106. ? movie.demux.videoStreamInfo.lengthSeconds : 0;
  107. }
  108. }
  109. #endregion ------ /public properties ------
  110. #region ------ public methods ------
  111. /// <summary>
  112. /// Loads the movie.
  113. ///
  114. /// In case it fails, exception is thrown
  115. /// </summary>
  116. protected void Load (MovieSource source, LoadOptions loadOptions = null)
  117. {
  118. // use temporary vars when loading and then assign them to instance vars.
  119. // this way if the loading fails, the MoviePlayer is still in consistent state.
  120. AudioClip tmpAudioBuffer;
  121. Texture2D tmpFramebuffer;
  122. // Actually try to load the movie.
  123. // In case of failure, exception is thrown here and the currently playing movie keeps playing
  124. var loadedMovie = MoviePlayerUtil.Load (source, out tmpFramebuffer, out tmpAudioBuffer, loadOptions);
  125. // new movie loaded successfully. if there was a movie previously loaded, unload it
  126. if(movie != null) {
  127. MoviePlayerUtil.Unload(movie);
  128. }
  129. movie = loadedMovie;
  130. // reset stats
  131. _framesDropped = 0;
  132. _syncEvents = 0;
  133. // make the loaded movie visible and hearable
  134. audiobuffer = tmpAudioBuffer;
  135. framebuffer = tmpFramebuffer;
  136. Bind ();
  137. }
  138. /// <summary>
  139. /// Closes the movie source stream.
  140. ///
  141. /// Don't leave cleaning up to Garbage Colletor, at least
  142. /// if it is a file stream. Clean it up yourself.
  143. /// </summary>
  144. [ContextMenu("Unload (disconnect)")]
  145. public void Unload()
  146. {
  147. if(movie != null) {
  148. // stop the audio
  149. if(GetComponent<AudioSource>() != null) GetComponent<AudioSource>().Stop();
  150. // unload all other resources associated with the movie
  151. MoviePlayerUtil.Unload(movie);
  152. movie = null;
  153. }
  154. }
  155. #endregion ------ / public methods ------
  156. protected int _framesDropped;
  157. protected int _syncEvents;
  158. protected bool lastPlay;
  159. /// <summary>
  160. /// Binds or rebinds the output of audio and video decoder to framebuffer texture and audioClip.
  161. /// </summary>
  162. protected void Bind ()
  163. {
  164. // if we have a renderer, bind the framebuffer to its material (most convenient use case probably)
  165. var renderer = GetComponent<Renderer>();
  166. if(renderer != null) {
  167. renderer.sharedMaterial.SetTexture(texturePropertyName, framebuffer);
  168. #if MP_DEBUG
  169. Debug.Log("Framebuffer bound to renderer material. Property name: " + texturePropertyName);
  170. #endif
  171. }
  172. // bind the framebuffer to given material
  173. if(material != null) {
  174. material.SetTexture(texturePropertyName, framebuffer);
  175. #if MP_DEBUG
  176. Debug.Log("Framebuffer bound to " + material.name + " material. Property name: " + texturePropertyName);
  177. #endif
  178. }
  179. // if there is audio, bind it to the AudioSource component.
  180. // if AudioSource is not there, then it is added automatically
  181. if (audiobuffer != null) {
  182. if (GetComponent<AudioSource>() == null) {
  183. gameObject.AddComponent<AudioSource> ();
  184. }
  185. GetComponent<AudioSource>().clip = audiobuffer;
  186. GetComponent<AudioSource>().playOnAwake = false;
  187. #if MP_DEBUG
  188. Debug.Log("Audio buffer bound to GameObject's AudioSource");
  189. #endif
  190. }
  191. // if there's no audio in the movie, but there is AudioSource component, then stop whatever is playing there
  192. else if(GetComponent<AudioSource>() != null) {
  193. GetComponent<AudioSource>().Stop();
  194. GetComponent<AudioSource>().clip = null;
  195. #if MP_DEBUG
  196. Debug.Log("Removed audio buffer from GameObject's AudioSource");
  197. #endif
  198. }
  199. }
  200. protected void DrawFramebufferToScreen ()
  201. {
  202. Rect drawRect = new Rect (0, 0, Screen.width, Screen.height);
  203. if (screenMode == ScreenMode.CustomRect) {
  204. drawRect = customScreenRect;
  205. } else if (screenMode != ScreenMode.Stretch) {
  206. float movieAspect = (float)movie.demux.videoStreamInfo.width / (float)movie.demux.videoStreamInfo.height;
  207. float screenAspect = (float)Screen.width / (float)Screen.height;
  208. if (screenMode == ScreenMode.Crop) {
  209. if (screenAspect < movieAspect) { // black bars on top and bottom
  210. drawRect.height = Mathf.Round (drawRect.width / movieAspect);
  211. } else { // black bars on left and right
  212. drawRect.width = Mathf.Round (drawRect.height * movieAspect);
  213. }
  214. } else if (screenMode == ScreenMode.Fill) {
  215. if (screenAspect < movieAspect) { // lock top and bottom
  216. drawRect.width = Mathf.Round (Screen.height * movieAspect);
  217. } else { // lock left and right
  218. drawRect.height = Mathf.Round (Screen.width / movieAspect);
  219. }
  220. }
  221. drawRect.x = Mathf.Round ((Screen.width - drawRect.width) / 2f);
  222. drawRect.y = Mathf.Round ((Screen.height - drawRect.height) / 2f);
  223. }
  224. GUI.depth = screenGuiDepth;
  225. var e = Event.current;
  226. if(e == null || e.type == EventType.Repaint) {
  227. Graphics.DrawTexture(drawRect, framebuffer, material);
  228. }
  229. }
  230. protected void HandlePlayStop ()
  231. {
  232. if (play != lastPlay) {
  233. if (play) {
  234. #if MP_DEBUG
  235. Debug.Log ("PLAY");
  236. #endif
  237. if (OnPlay != null)
  238. OnPlay (this);
  239. if (GetComponent<AudioSource>() != null) {
  240. GetComponent<AudioSource>().Play ();
  241. if(this is MoviePlayer) {
  242. GetComponent<AudioSource>().time = ((MoviePlayer)this).videoTime;
  243. }
  244. }
  245. } else {
  246. #if MP_DEBUG
  247. Debug.Log ("STOP");
  248. #endif
  249. if (OnStop != null)
  250. OnStop (this);
  251. if (GetComponent<AudioSource>() != null) {
  252. GetComponent<AudioSource>().Stop ();
  253. }
  254. }
  255. lastPlay = play;
  256. }
  257. }
  258. }