using Com.Tencent.Trtc; using System; using System.Collections.Concurrent; using System.Runtime.InteropServices; using Vinno.FIS.TRTCClient.Common.Log; using Vinno.FIS.TRTCClient.Common.Models; namespace Vinno.FIS.TRTCClient.Android { public class RemoteVideoRenderListener : Java.Lang.Object, TRTCCloudListener.ITRTCVideoRenderListener { private readonly Action _callBack; private ConcurrentDictionary _remoteFrameCaches; public RemoteVideoRenderListener(Action callBack) { _remoteFrameCaches = new ConcurrentDictionary(); _callBack = callBack; } public void OnRenderVideoFrame(string userId, int streamType, TRTCCloudDef.TRTCVideoFrame frame) { try { if (!_remoteFrameCaches.ContainsKey(userId)) { _remoteFrameCaches[userId] = new TRTCVideoFrameData(userId, frame.Width, frame.Height, new byte[frame.Height * frame.Width * 4]); } var remoteFrameCache = _remoteFrameCaches[userId]; var height = frame.Height; var width = frame.Width; if (width != remoteFrameCache.Width || height != remoteFrameCache.Height) { remoteFrameCache.Width = width; remoteFrameCache.Height = height; remoteFrameCache.Data = new byte[height * width * 4]; } Marshal.Copy(frame.Buffer.GetDirectBufferAddress(), remoteFrameCache.Data, 0, remoteFrameCache.Data.Length); _callBack?.Invoke(remoteFrameCache); } catch (Exception ex) { Logger.WriteLineError($"Receive Remote Image Error:{ex}"); } } } }