MediaPlayer_OpenBuffer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. public partial class MediaPlayer : MonoBehaviour
  8. {
  9. public bool OpenMediaFromBuffer(byte[] buffer, bool autoPlay = true)
  10. {
  11. _mediaPath = new MediaPath("buffer", MediaPathType.AbsolutePathOrURL);
  12. _autoPlayOnStart = autoPlay;
  13. if (_controlInterface == null)
  14. {
  15. Initialise();
  16. }
  17. return OpenMediaFromBufferInternal(buffer);
  18. }
  19. public bool StartOpenChunkedMediaFromBuffer(ulong length, bool autoPlay = true)
  20. {
  21. _mediaPath = new MediaPath("buffer", MediaPathType.AbsolutePathOrURL);
  22. _autoPlayOnStart = autoPlay;
  23. if (_controlInterface == null)
  24. {
  25. Initialise();
  26. }
  27. return StartOpenMediaFromBufferInternal(length);
  28. }
  29. public bool AddChunkToVideoBuffer(byte[] chunk, ulong offset, ulong chunkSize)
  30. {
  31. return AddChunkToBufferInternal(chunk, offset, chunkSize);
  32. }
  33. public bool EndOpenChunkedVideoFromBuffer()
  34. {
  35. return EndOpenMediaFromBufferInternal();
  36. }
  37. private bool OpenMediaFromBufferInternal(byte[] buffer)
  38. {
  39. bool result = false;
  40. // Open the video file
  41. if (_controlInterface != null)
  42. {
  43. CloseMedia();
  44. _isMediaOpened = true;
  45. _autoPlayOnStartTriggered = !_autoPlayOnStart;
  46. Helper.LogInfo("Opening buffer of length " + buffer.Length, this);
  47. if (!_controlInterface.OpenMediaFromBuffer(buffer))
  48. {
  49. Debug.LogError("[AVProVideo] Failed to open buffer", this);
  50. if (GetCurrentPlatformOptions() != PlatformOptionsWindows || PlatformOptionsWindows.videoApi != Windows.VideoApi.DirectShow)
  51. {
  52. Debug.LogError("[AVProVideo] Loading from buffer is currently only supported in Windows when using the DirectShow API");
  53. }
  54. }
  55. else
  56. {
  57. SetPlaybackOptions();
  58. result = true;
  59. StartRenderCoroutine();
  60. }
  61. }
  62. return result;
  63. }
  64. private bool StartOpenMediaFromBufferInternal(ulong length)
  65. {
  66. bool result = false;
  67. // Open the video file
  68. if (_controlInterface != null)
  69. {
  70. CloseMedia();
  71. _isMediaOpened = true;
  72. _autoPlayOnStartTriggered = !_autoPlayOnStart;
  73. Helper.LogInfo("Starting Opening buffer of length " + length, this);
  74. if (!_controlInterface.StartOpenMediaFromBuffer(length))
  75. {
  76. Debug.LogError("[AVProVideo] Failed to start open video from buffer", this);
  77. if (GetCurrentPlatformOptions() != PlatformOptionsWindows || PlatformOptionsWindows.videoApi != Windows.VideoApi.DirectShow)
  78. {
  79. Debug.LogError("[AVProVideo] Loading from buffer is currently only supported in Windows when using the DirectShow API");
  80. }
  81. }
  82. else
  83. {
  84. SetPlaybackOptions();
  85. result = true;
  86. StartRenderCoroutine();
  87. }
  88. }
  89. return result;
  90. }
  91. private bool AddChunkToBufferInternal(byte[] chunk, ulong offset, ulong chunkSize)
  92. {
  93. if (Control != null)
  94. {
  95. return Control.AddChunkToMediaBuffer(chunk, offset, chunkSize);
  96. }
  97. return false;
  98. }
  99. private bool EndOpenMediaFromBufferInternal()
  100. {
  101. if (Control != null)
  102. {
  103. return Control.EndOpenMediaFromBuffer();
  104. }
  105. return false;
  106. }
  107. }
  108. }