VideoEncoder.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Record
  10. {
  11. using UnityEngine;
  12. using System;
  13. using AOT;
  14. using System.Runtime.InteropServices;
  15. /// <summary> A video encoder. </summary>
  16. public class VideoEncoder : IEncoder
  17. {
  18. #if !UNITY_EDITOR
  19. private const int STARTENCODEEVENT = 0x1001;
  20. private const int STOPENCODEEVENT = 0x1002;
  21. private NativeEncoder mNativeEncoder;
  22. private delegate void RenderEventDelegate(int eventID);
  23. private static RenderEventDelegate RenderThreadHandle = new RenderEventDelegate(RunOnRenderThread);
  24. private static IntPtr RenderThreadHandlePtr = Marshal.GetFunctionPointerForDelegate(RenderThreadHandle);
  25. #endif
  26. private AudioRecordTool m_AudioEncodeTool;
  27. public NativeEncodeConfig EncodeConfig;
  28. private IntPtr androidMediaProjection { get; set; }
  29. private IntPtr m_TexPtr = IntPtr.Zero;
  30. private byte[] m_AudioRawData;
  31. private bool m_IsStarted = false;
  32. /// <summary> Default constructor. </summary>
  33. public VideoEncoder()
  34. {
  35. #if !UNITY_EDITOR
  36. mNativeEncoder = NativeEncoder.GetInstance();
  37. mNativeEncoder.Register(this);
  38. #endif
  39. }
  40. #if !UNITY_EDITOR
  41. [MonoPInvokeCallback(typeof(RenderEventDelegate))]
  42. private static void RunOnRenderThread(int eventID)
  43. {
  44. if (eventID == STARTENCODEEVENT)
  45. {
  46. NativeEncoder.GetInstance().Start();
  47. }
  48. //if (eventID == STOPENCODEEVENT)
  49. //{
  50. // NativeEncoder.Stop();
  51. //}
  52. }
  53. #endif
  54. /// <summary> Configurations the given parameter. </summary>
  55. /// <param name="param"> The parameter.</param>
  56. public void Config(CameraParameters param)
  57. {
  58. EncodeConfig = new NativeEncodeConfig(param);
  59. androidMediaProjection = (param.mediaProjection != null) ? param.mediaProjection.GetRawObject() : IntPtr.Zero;
  60. }
  61. /// <summary> Adjust the volume of encoder.</summary>
  62. /// <param name="recordIdx"> Recorder index.</param>
  63. /// <param name="factor"> The factor of volume.</param>
  64. public void AdjustVolume(RecorderIndex recordIdx, float factor)
  65. {
  66. #if !UNITY_EDITOR
  67. mNativeEncoder.AdjustVolume(recordIdx, factor);
  68. #endif
  69. }
  70. /// <summary> Starts this object. </summary>
  71. public void Start()
  72. {
  73. if (m_IsStarted)
  74. {
  75. return;
  76. }
  77. NRDebugger.Info("[VideoEncoder] Start");
  78. // if (EncodeConfig.audioUseExternalData)
  79. // {
  80. // InitAudioEncodeTool();
  81. // m_AudioEncodeTool?.StartRecord();
  82. // }
  83. NRDebugger.Info("[VideoEncoder] Config {0}", EncodeConfig.ToString());
  84. #if !UNITY_EDITOR
  85. mNativeEncoder.SetConfigration(EncodeConfig, androidMediaProjection);
  86. //mNativeEncoder.Start();
  87. GL.IssuePluginEvent(RenderThreadHandlePtr, STARTENCODEEVENT);
  88. #endif
  89. m_IsStarted = true;
  90. }
  91. private void InitAudioEncodeTool()
  92. {
  93. AudioListener audioListener = null;
  94. if (NRSessionManager.Instance.NRHMDPoseTracker != null)
  95. {
  96. audioListener = NRSessionManager.Instance.NRHMDPoseTracker.centerCamera.gameObject.GetComponent<AudioListener>();
  97. }
  98. else if (GameObject.FindObjectOfType<AudioListener>() != null)
  99. {
  100. audioListener = GameObject.FindObjectOfType<AudioListener>();
  101. }
  102. if (audioListener != null)
  103. {
  104. m_AudioEncodeTool = audioListener.gameObject.GetComponent<AudioRecordTool>();
  105. if (m_AudioEncodeTool == null)
  106. {
  107. m_AudioEncodeTool = audioListener.gameObject.AddComponent<AudioRecordTool>();
  108. }
  109. }
  110. else
  111. {
  112. throw (new MissingComponentException("Can not find a 'AudioListener' in current scene."));
  113. }
  114. }
  115. /// <summary> Commits. </summary>
  116. /// <param name="rt"> The renderTexture.</param>
  117. /// <param name="timestamp"> The timestamp.</param>
  118. public void Commit(RenderTexture rt, UInt64 timestamp)
  119. {
  120. if (!m_IsStarted)
  121. {
  122. return;
  123. }
  124. if (m_TexPtr == IntPtr.Zero)
  125. {
  126. m_TexPtr = rt.GetNativeTexturePtr();
  127. }
  128. #if !UNITY_EDITOR
  129. mNativeEncoder.UpdateSurface(m_TexPtr, timestamp);
  130. if (m_AudioEncodeTool != null)
  131. {
  132. bool result = m_AudioEncodeTool.Flush(ref m_AudioRawData);
  133. if (result)
  134. {
  135. mNativeEncoder.UpdateAudioData(m_AudioRawData, m_AudioEncodeTool.SampleRate,2,1);
  136. }
  137. }
  138. #endif
  139. }
  140. /// <summary> Stops this object. </summary>
  141. public void Stop()
  142. {
  143. if (!m_IsStarted)
  144. {
  145. return;
  146. }
  147. NRDebugger.Info("[VideoEncoder] Stop");
  148. m_AudioEncodeTool?.StopRecord();
  149. #if !UNITY_EDITOR
  150. //GL.IssuePluginEvent(RenderThreadHandlePtr, STOPENCODEEVENT);
  151. mNativeEncoder.Stop();
  152. #endif
  153. m_IsStarted = false;
  154. }
  155. /// <summary> Releases this object. </summary>
  156. public void Release()
  157. {
  158. NRDebugger.Info("[VideoEncoder] Release...");
  159. #if !UNITY_EDITOR
  160. mNativeEncoder.UnRegister(this);
  161. mNativeEncoder.Destroy();
  162. #endif
  163. if (m_AudioEncodeTool != null)
  164. {
  165. GameObject.Destroy(m_AudioEncodeTool);
  166. m_AudioEncodeTool = null;
  167. }
  168. }
  169. }
  170. }