NativePlugin.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0 && !UNITY_5_1)
  2. #define AVPRO_MOVIECAPTURE_ISSUEPLUGINEVENT_52
  3. #endif
  4. using UnityEngine;
  5. using System.Text;
  6. using System.Runtime.InteropServices;
  7. //-----------------------------------------------------------------------------
  8. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. namespace RenderHeads.Media.AVProMovieCapture
  11. {
  12. public enum StereoPacking
  13. {
  14. None,
  15. TopBottom,
  16. LeftRight,
  17. }
  18. public enum StopMode
  19. {
  20. None,
  21. FramesEncoded,
  22. SecondsEncoded,
  23. SecondsElapsed,
  24. }
  25. public class NativePlugin
  26. {
  27. public enum PixelFormat
  28. {
  29. RGBA32,
  30. BGRA32, // Note: This is the native format for Unity textures with red and blue swapped.
  31. YCbCr422_YUY2,
  32. YCbCr422_UYVY,
  33. YCbCr422_HDYC,
  34. }
  35. // Used by GL.IssuePluginEvent
  36. public const int PluginID = 0xFA30000;
  37. public enum PluginEvent
  38. {
  39. CaptureFrameBuffer = 0,
  40. FreeResources = 1,
  41. }
  42. public const string ScriptVersion = "3.3.1";
  43. public const string ExpectedPluginVersion = "3.30";
  44. public const int MaxRenderWidth = 16384;
  45. public const int MaxRenderHeight = 16384;
  46. #if AVPRO_MOVIECAPTURE_ISSUEPLUGINEVENT_52
  47. [DllImport("AVProMovieCapture")]
  48. public static extern System.IntPtr GetRenderEventFunc();
  49. [DllImport("AVProMovieCapture")]
  50. public static extern System.IntPtr GetFreeResourcesEventFunc();
  51. #endif
  52. //////////////////////////////////////////////////////////////////////////
  53. // Global Init/Deinit
  54. [DllImport("AVProMovieCapture")]
  55. public static extern bool Init();
  56. [DllImport("AVProMovieCapture")]
  57. public static extern void Deinit();
  58. [DllImport("AVProMovieCapture")]
  59. public static extern float GetPluginVersion();
  60. [DllImport("AVProMovieCapture")]
  61. public static extern bool IsTrialVersion();
  62. //////////////////////////////////////////////////////////////////////////
  63. // Video Codecs
  64. [DllImport("AVProMovieCapture")]
  65. public static extern int GetNumAVIVideoCodecs();
  66. [DllImport("AVProMovieCapture")]
  67. public static extern bool IsConfigureVideoCodecSupported(int index);
  68. [DllImport("AVProMovieCapture")]
  69. public static extern void ConfigureVideoCodec(int index);
  70. public static string GetAVIVideoCodecName(int index)
  71. {
  72. string result = "Invalid";
  73. StringBuilder nameBuffer = new StringBuilder(256);
  74. if (GetAVIVideoCodecName(index, nameBuffer, nameBuffer.Capacity))
  75. {
  76. result = nameBuffer.ToString();
  77. }
  78. return result;
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. // Audio Codecs
  82. [DllImport("AVProMovieCapture")]
  83. public static extern int GetNumAVIAudioCodecs();
  84. [DllImport("AVProMovieCapture")]
  85. public static extern bool IsConfigureAudioCodecSupported(int index);
  86. [DllImport("AVProMovieCapture")]
  87. public static extern void ConfigureAudioCodec(int index);
  88. public static string GetAVIAudioCodecName(int index)
  89. {
  90. string result = "Invalid";
  91. StringBuilder nameBuffer = new StringBuilder(256);
  92. if (GetAVIAudioCodecName(index, nameBuffer, nameBuffer.Capacity))
  93. {
  94. result = nameBuffer.ToString();
  95. }
  96. return result;
  97. }
  98. //////////////////////////////////////////////////////////////////////////
  99. // Audio Devices
  100. [DllImport("AVProMovieCapture")]
  101. public static extern int GetNumAVIAudioInputDevices();
  102. public static string GetAVIAudioInputDeviceName(int index)
  103. {
  104. string result = "Invalid";
  105. StringBuilder nameBuffer = new StringBuilder(256);
  106. if (GetAVIAudioInputDeviceName(index, nameBuffer, nameBuffer.Capacity))
  107. {
  108. result = nameBuffer.ToString();
  109. }
  110. return result;
  111. }
  112. //////////////////////////////////////////////////////////////////////////
  113. // Create the recorder
  114. [DllImport("AVProMovieCapture")]
  115. public static extern int CreateRecorderAVI([MarshalAs(UnmanagedType.LPWStr)] string filename, uint width, uint height, int frameRate, int format,
  116. bool isTopDown, int videoCodecIndex, bool hasAudio, int audioSampleRate, int audioChannelCount, int audioInputDeviceIndex, int audioCodecIndex, bool isRealTime, bool useMediaFoundationH264, bool supportAlpha);
  117. [DllImport("AVProMovieCapture")]
  118. public static extern int CreateRecorderPipe([MarshalAs(UnmanagedType.LPWStr)] string filename, uint width, uint height, int frameRate, int format, bool isTopDown, bool supportAlpha);
  119. //////////////////////////////////////////////////////////////////////////
  120. // Update recorder
  121. [DllImport("AVProMovieCapture")]
  122. public static extern bool Start(int handle);
  123. [DllImport("AVProMovieCapture")]
  124. public static extern bool IsNewFrameDue(int handle);
  125. [DllImport("AVProMovieCapture")]
  126. public static extern void EncodeFrame(int handle, System.IntPtr data);
  127. [DllImport("AVProMovieCapture")]
  128. public static extern void EncodeAudio(int handle, System.IntPtr data, uint length);
  129. [DllImport("AVProMovieCapture")]
  130. public static extern void EncodeFrameWithAudio(int handle, System.IntPtr videoData, System.IntPtr audioData, uint audioLength);
  131. [DllImport("AVProMovieCapture")]
  132. public static extern void Pause(int handle);
  133. [DllImport("AVProMovieCapture")]
  134. public static extern void Stop(int handle, bool skipPendingFrames);
  135. [DllImport("AVProMovieCapture")]
  136. public static extern void SetTexturePointer(int handle, System.IntPtr texture);
  137. //////////////////////////////////////////////////////////////////////////
  138. // Destroy recorder
  139. [DllImport("AVProMovieCapture")]
  140. public static extern void FreeRecorder(int handle);
  141. //////////////////////////////////////////////////////////////////////////
  142. // Debugging
  143. [DllImport("AVProMovieCapture")]
  144. public static extern uint GetNumDroppedFrames(int handle);
  145. [DllImport("AVProMovieCapture")]
  146. public static extern uint GetNumDroppedEncoderFrames(int handle);
  147. [DllImport("AVProMovieCapture")]
  148. public static extern uint GetNumEncodedFrames(int handle);
  149. [DllImport("AVProMovieCapture")]
  150. public static extern uint GetEncodedSeconds(int handle);
  151. //////////////////////////////////////////////////////////////////////////
  152. // Private internal functions
  153. [DllImport("AVProMovieCapture")]
  154. private static extern bool GetAVIVideoCodecName(int index, StringBuilder name, int nameBufferLength);
  155. [DllImport("AVProMovieCapture")]
  156. private static extern bool GetAVIAudioCodecName(int index, StringBuilder name, int nameBufferLength);
  157. [DllImport("AVProMovieCapture")]
  158. private static extern bool GetAVIAudioInputDeviceName(int index, StringBuilder name, int nameBufferLength);
  159. }
  160. }