RemoteVideoRenderCallback.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Com.Tencent.Trtc;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Runtime.InteropServices;
  5. using Vinno.FIS.TRTCClient.Common.Log;
  6. using Vinno.FIS.TRTCClient.Common.Models;
  7. namespace Vinno.FIS.TRTCClient.Android
  8. {
  9. public class RemoteVideoRenderListener : Java.Lang.Object, TRTCCloudListener.ITRTCVideoRenderListener
  10. {
  11. private readonly Action<TRTCVideoFrameData> _callBack;
  12. private ConcurrentDictionary<string, TRTCVideoFrameData> _remoteFrameCaches;
  13. public RemoteVideoRenderListener(Action<TRTCVideoFrameData> callBack)
  14. {
  15. _remoteFrameCaches = new ConcurrentDictionary<string, TRTCVideoFrameData>();
  16. _callBack = callBack;
  17. }
  18. public void OnRenderVideoFrame(string userId, int streamType, TRTCCloudDef.TRTCVideoFrame frame)
  19. {
  20. try
  21. {
  22. if (!_remoteFrameCaches.ContainsKey(userId))
  23. {
  24. _remoteFrameCaches[userId] = new TRTCVideoFrameData(userId, frame.Width, frame.Height, new byte[frame.Height * frame.Width * 4]);
  25. }
  26. var remoteFrameCache = _remoteFrameCaches[userId];
  27. var height = frame.Height;
  28. var width = frame.Width;
  29. if (width != remoteFrameCache.Width || height != remoteFrameCache.Height)
  30. {
  31. remoteFrameCache.Width = width;
  32. remoteFrameCache.Height = height;
  33. remoteFrameCache.Data = new byte[height * width * 4];
  34. }
  35. Marshal.Copy(frame.Buffer.GetDirectBufferAddress(), remoteFrameCache.Data, 0, remoteFrameCache.Data.Length);
  36. _callBack?.Invoke(remoteFrameCache);
  37. }
  38. catch (Exception ex)
  39. {
  40. Logger.WriteLineError($"Receive Remote Image Error:{ex}");
  41. }
  42. }
  43. }
  44. }