AudioEncoder.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 System.Collections.Generic;
  14. using AOT;
  15. using System.Runtime.InteropServices;
  16. /// <summary> A video encoder. </summary>
  17. public class AudioEncoder : IEncoderBase
  18. {
  19. private NativeEncoder mNativeEncoder;
  20. public NativeEncodeConfig EncodeConfig;
  21. private IntPtr androidMediaProjection { get; set; }
  22. private bool m_IsStarted = false;
  23. private AudioDataCallBack mDataCallBack = null;
  24. private static List<AudioEncoder> gInactiveAudioEncoders = new List<AudioEncoder>();
  25. #if !UNITY_EDITOR
  26. private const int STARTENCODEEVENT = 0x2001;
  27. private const int STOPENCODEEVENT = 0x2002;
  28. private delegate void RenderEventDelegate(int eventID);
  29. private static RenderEventDelegate RenderThreadHandle = new RenderEventDelegate(RunOnRenderThread);
  30. private static IntPtr RenderThreadHandlePtr = Marshal.GetFunctionPointerForDelegate(RenderThreadHandle);
  31. #endif
  32. /// <summary> Default constructor. </summary>
  33. public AudioEncoder()
  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. lock (gInactiveAudioEncoders)
  47. {
  48. for (int i = 0; i < gInactiveAudioEncoders.Count; i++)
  49. {
  50. NativeEncoder.GetInstance().StartAudioRecorder(gInactiveAudioEncoders[i].mDataCallBack);
  51. }
  52. gInactiveAudioEncoders.Clear();
  53. }
  54. }
  55. }
  56. #endif
  57. /// <summary> Configurations the given parameter. </summary>
  58. /// <param name="param"> The parameter.</param>
  59. public void Config(CameraParameters param)
  60. {
  61. EncodeConfig = new NativeEncodeConfig(param);
  62. androidMediaProjection = (param.mediaProjection != null) ? param.mediaProjection.GetRawObject() : IntPtr.Zero;
  63. }
  64. /// <summary> Adjust the volume of encoder.</summary>
  65. /// <param name="recordIdx"> Recorder index.</param>
  66. /// <param name="factor"> The factor of volume.</param>
  67. public void AdjustVolume(RecorderIndex recordIdx, float factor)
  68. {
  69. #if !UNITY_EDITOR
  70. mNativeEncoder.AdjustVolume(recordIdx, factor);
  71. #endif
  72. }
  73. /// <summary> Starts this object. </summary>
  74. public void Start(AudioDataCallBack onAudioDataCallback = null)
  75. {
  76. if (m_IsStarted)
  77. {
  78. return;
  79. }
  80. NRDebugger.Info("[AudioEncoder] Start");
  81. NRDebugger.Info("[AudioEncoder] Config {0}", EncodeConfig.ToString());
  82. mDataCallBack = onAudioDataCallback;
  83. lock (gInactiveAudioEncoders)
  84. {
  85. gInactiveAudioEncoders.Add(this);
  86. }
  87. #if !UNITY_EDITOR
  88. mNativeEncoder.SetConfigration(EncodeConfig, androidMediaProjection);
  89. //mNativeEncoder.StartAudioRecorder(mDataCallBack);
  90. GL.IssuePluginEvent(RenderThreadHandlePtr, STARTENCODEEVENT);
  91. #endif
  92. m_IsStarted = true;
  93. }
  94. /// <summary> Stops this object. </summary>
  95. public void Stop()
  96. {
  97. if (!m_IsStarted)
  98. {
  99. return;
  100. }
  101. NRDebugger.Info("[AudioEncoder] Stop");
  102. lock (gInactiveAudioEncoders)
  103. {
  104. gInactiveAudioEncoders.Remove(this);
  105. }
  106. #if !UNITY_EDITOR
  107. mNativeEncoder.StopAudioRecorder(mDataCallBack);
  108. #endif
  109. m_IsStarted = false;
  110. }
  111. /// <summary> Releases this object. </summary>
  112. public void Release()
  113. {
  114. NRDebugger.Info("[AudioEncoder] Release...");
  115. #if !UNITY_EDITOR
  116. mNativeEncoder.UnRegister(this);
  117. mNativeEncoder.Destroy();
  118. #endif
  119. }
  120. }
  121. }