AudioEncodedFrameObserverNative.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Runtime.InteropServices;
  3. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  4. using AOT;
  5. #endif
  6. namespace Agora.Rtc
  7. {
  8. internal static class AudioEncodedFrameObserverNative
  9. {
  10. internal static IAudioEncodedFrameObserver AudioEncodedFrameObserver = null;
  11. internal static EncodedAudioFrameInfo IrisEncodedAudioFrameInfo2EncodedAudioFrameInfo(ref IrisEncodedAudioFrameInfo from)
  12. {
  13. EncodedAudioFrameInfo to = new EncodedAudioFrameInfo();
  14. to.codec = from.codec;
  15. to.sampleRateHz = from.sampleRateHz;
  16. to.samplesPerChannel = from.samplesPerChannel;
  17. to.numberOfChannels = from.numberOfChannels;
  18. to.advancedSettings = new EncodedAudioFrameAdvancedSettings();
  19. to.advancedSettings.speech = from.advancedSettings.speech;
  20. to.advancedSettings.sendEvenIfEmpty = from.advancedSettings.sendEvenIfEmpty;
  21. to.captureTimeMs = from.captureTimeMs;
  22. return to;
  23. }
  24. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  25. [MonoPInvokeCallback(typeof(Func_RecordAudioEncodedFrame_Native))]
  26. #endif
  27. internal static void OnRecordAudioEncodedFrame(IntPtr frame_buffer, int length, IntPtr encoded_audio_frame_info)
  28. {
  29. if (AudioEncodedFrameObserver == null) return;
  30. IrisEncodedAudioFrameInfo from = (IrisEncodedAudioFrameInfo)Marshal.PtrToStructure(encoded_audio_frame_info, typeof(IrisEncodedAudioFrameInfo));
  31. EncodedAudioFrameInfo to = IrisEncodedAudioFrameInfo2EncodedAudioFrameInfo(ref from);
  32. try
  33. {
  34. AudioEncodedFrameObserver.OnRecordAudioEncodedFrame(frame_buffer, length, to);
  35. }
  36. catch (Exception e)
  37. {
  38. AgoraLog.LogError("[Exception] IAudioEncodedFrameObserver.OnRecordAudioEncodedFrame: " + e);
  39. }
  40. }
  41. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  42. [MonoPInvokeCallback(typeof(Func_RecordAudioEncodedFrame_Native))]
  43. #endif
  44. internal static void OnPlaybackAudioEncodedFrame(IntPtr frame_buffer, int length, IntPtr encoded_audio_frame_info)
  45. {
  46. if (AudioEncodedFrameObserver == null) return;
  47. IrisEncodedAudioFrameInfo from = (IrisEncodedAudioFrameInfo)Marshal.PtrToStructure(encoded_audio_frame_info, typeof(IrisEncodedAudioFrameInfo));
  48. EncodedAudioFrameInfo to = IrisEncodedAudioFrameInfo2EncodedAudioFrameInfo(ref from);
  49. try
  50. {
  51. AudioEncodedFrameObserver.OnPlaybackAudioEncodedFrame(frame_buffer, length, to);
  52. }
  53. catch (Exception e)
  54. {
  55. AgoraLog.LogError("[Exception] IAudioEncodedFrameObserver.OnPlaybackAudioEncodedFrame: " + e);
  56. }
  57. }
  58. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  59. [MonoPInvokeCallback(typeof(Func_RecordAudioEncodedFrame_Native))]
  60. #endif
  61. internal static void OnMixedAudioEncodedFrame(IntPtr frame_buffer, int length, IntPtr encoded_audio_frame_info)
  62. {
  63. if (AudioEncodedFrameObserver == null) return;
  64. IrisEncodedAudioFrameInfo from = (IrisEncodedAudioFrameInfo)Marshal.PtrToStructure(encoded_audio_frame_info, typeof(IrisEncodedAudioFrameInfo));
  65. EncodedAudioFrameInfo to = IrisEncodedAudioFrameInfo2EncodedAudioFrameInfo(ref from);
  66. try
  67. {
  68. AudioEncodedFrameObserver.OnMixedAudioEncodedFrame(frame_buffer, length, to);
  69. }
  70. catch (Exception e)
  71. {
  72. AgoraLog.LogError("[Exception] IAudioEncodedFrameObserver.OnMixedAudioEncodedFrame: " + e);
  73. }
  74. }
  75. }
  76. }