MovieStreamer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //--------------------------------------------
  2. // Movie Player
  3. // Copyright © 2014 SHUU Games
  4. //--------------------------------------------
  5. using UnityEngine;
  6. using System;
  7. using MP;
  8. using MP.Net;
  9. /// <summary>
  10. /// Movie streamer
  11. /// </summary>
  12. public class MovieStreamer : MoviePlayerBase
  13. {
  14. #region ----- public ------
  15. /// <summary>
  16. /// Movie source url
  17. /// </summary>
  18. public string sourceUrl;
  19. /// <summary>
  20. /// Movie load options. The Load() methods on this component will use
  21. /// this unless you're provinding your own.
  22. /// </summary>
  23. public LoadOptions loadOptions = LoadOptions.Default;
  24. /// <summary>
  25. /// Background thread status
  26. /// </summary>
  27. public string status;
  28. public long bytesReceived;
  29. public bool IsConnected
  30. {
  31. get {
  32. return movie==null || movie.demux==null ? false : ((Streamer)movie.demux).IsConnected;
  33. }
  34. }
  35. /// <summary>
  36. /// Connects to an URL for streaming.
  37. ///
  38. /// In case it fails, exception text is logged and FALSE is returned
  39. /// </summary>
  40. public bool Load (string srcUrl, LoadOptions loadOptions = null)
  41. {
  42. this.sourceUrl = srcUrl;
  43. if(loadOptions == null) {
  44. loadOptions = this.loadOptions;
  45. } else {
  46. this.loadOptions = loadOptions;
  47. }
  48. try {
  49. base.Load(new MovieSource() { url = srcUrl }, loadOptions);
  50. return true;
  51. }
  52. catch (Exception e) {
  53. Debug.LogError (e);
  54. //throw e;
  55. return false;
  56. }
  57. }
  58. [ContextMenu("Reconnect")]
  59. public bool ReConnect ()
  60. {
  61. bool success = true;
  62. if (!string.IsNullOrEmpty(sourceUrl)) {
  63. success = Load (sourceUrl, loadOptions);
  64. }
  65. return success;
  66. }
  67. #endregion ------ / public ------
  68. #region ----- private -----
  69. private int lastVideoFrame = -1;
  70. void Start ()
  71. {
  72. ReConnect ();
  73. }
  74. void OnGUI ()
  75. {
  76. if (!IsConnected || !movie.demux.hasVideo)
  77. return;
  78. // if we're playing the movie directly to screen, but don't
  79. // show it before we've received at least one frame
  80. if (drawToScreen && framebuffer != null && ((Streamer)movie.demux).VideoPosition > 0) {
  81. DrawFramebufferToScreen ();
  82. }
  83. }
  84. void Update ()
  85. {
  86. // get the thread status and write it here
  87. if(movie != null && movie.demux != null)
  88. {
  89. if(movie.demux is HttpMjpegStreamer) {
  90. status = ((HttpMjpegStreamer)movie.demux).Status;
  91. bytesReceived = ((HttpMjpegStreamer)movie.demux).BytesReceived;
  92. }
  93. }
  94. // if this.play changed, Play or Stop the movie
  95. HandlePlayStop ();
  96. // decode a frame when necessary
  97. if(play) {
  98. HandleFrameDecode ();
  99. }
  100. }
  101. protected void HandleFrameDecode ()
  102. {
  103. if (!IsConnected || !movie.demux.hasVideo || movie.videoDecoder == null)
  104. return;
  105. // decode a frame if there's a new one available
  106. if (movie.videoDecoder.Position != lastVideoFrame)
  107. {
  108. if(movie.videoDecoder.Position >= 0)
  109. {
  110. movie.videoDecoder.DecodeNext ();
  111. // update the aspect ration of the video
  112. movie.demux.videoStreamInfo.width = framebuffer.width;
  113. movie.demux.videoStreamInfo.height = framebuffer.height;
  114. }
  115. lastVideoFrame = movie.videoDecoder.Position;
  116. }
  117. }
  118. #endregion
  119. }