MediaPlayerAudioFrameObserverNative.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  5. using AOT;
  6. #endif
  7. namespace Agora.Rtc
  8. {
  9. internal static class MediaPlayerAudioFrameObserverNative
  10. {
  11. internal static Dictionary<int, IMediaPlayerAudioFrameObserver> AudioFrameObserverDic = new Dictionary<int, IMediaPlayerAudioFrameObserver>();
  12. private static class LocalAudioPcmFrames
  13. {
  14. internal static readonly AudioPcmFrame AudioPcmFrame = new AudioPcmFrame();
  15. }
  16. #if UNITY_EDITOR_WIN || UNITY_EDITOR_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID
  17. [MonoPInvokeCallback(typeof(Func_AudioOnFrame_Native))]
  18. #endif
  19. internal static bool OnFrame(IntPtr audioFramePtr, int mediaPlayerId)
  20. {
  21. var audioPcmFrame = (IrisAudioPcmFrame) (Marshal.PtrToStructure(audioFramePtr, typeof(IrisAudioPcmFrame)) ??
  22. new IrisAudioPcmFrame());
  23. var localAudioPcmFrame = new AudioPcmFrame();
  24. // todo optimize
  25. localAudioPcmFrame = LocalAudioPcmFrames.AudioPcmFrame;
  26. localAudioPcmFrame.data_ = new Int16[3840];
  27. localAudioPcmFrame.data_ = audioPcmFrame.data_;
  28. localAudioPcmFrame.num_channels_ = audioPcmFrame.num_channels_;
  29. localAudioPcmFrame.capture_timestamp = audioPcmFrame.capture_timestamp;
  30. localAudioPcmFrame.sample_rate_hz_ = audioPcmFrame.sample_rate_hz_;
  31. localAudioPcmFrame.samples_per_channel_ = audioPcmFrame.samples_per_channel_;
  32. try
  33. {
  34. return AudioFrameObserverDic.ContainsKey(mediaPlayerId) ||
  35. AudioFrameObserverDic[mediaPlayerId].OnFrame(localAudioPcmFrame);
  36. }
  37. catch (Exception e)
  38. {
  39. AgoraLog.LogError("[Exception] IMediaPlayerAudioFrameObserver.OnFrame: " + e);
  40. return true;
  41. }
  42. }
  43. }
  44. }