Jelajahi Sumber

优化FileTransferWriter,修复同一时段可能存在同名文件的情况,以及消息通知不该限制5条最大数量

felix 1 tahun lalu
induk
melakukan
4582a8a739

+ 54 - 10
FISSDK/FISSDK/FileTransferWriter.cs

@@ -66,11 +66,7 @@ namespace FISLib
                 }
                 lock (_lock)
                 {
-                    var now = DateTime.Now;
-                    var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
-                    var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
-                    tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
-                    normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
                     if (Directory.Exists(StorageFolderPath))
                     {
                         using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
@@ -108,11 +104,7 @@ namespace FISLib
                 }
                 lock (_lock)
                 {
-                    var now = DateTime.Now;
-                    var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
-                    var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
-                    tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
-                    normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
                     if (Directory.Exists(StorageFolderPath) && Directory.GetFiles(StorageFolderPath, "*.fis").Length < 5)
                     {
                         using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
@@ -135,6 +127,45 @@ namespace FISLib
             }
         }
 
+        /// <summary>
+        /// 用于一直写Message
+        /// </summary>
+        /// <param name="byteImage"></param>
+        public void WriteMessageToFileContinuous(byte[] byteImage)
+        {
+            string tempFilePath = null;
+            string normalFilePath = null;
+            try
+            {
+                if (_disposed)
+                {
+                    return;
+                }
+                lock (_lock)
+                {
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
+                    if (Directory.Exists(StorageFolderPath))
+                    {
+                        using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
+                        {
+                            using (BinaryWriter writer = new BinaryWriter(fileStream))
+                            {
+                                writer.Write(byteImage.Length);
+                                writer.Write(byteImage);
+                            }
+                        }
+                        File.Move(tempFilePath, normalFilePath);
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                LogMsgThrow?.Invoke(this, new FISLogEventArgs(FISDeviceLogCategory.Error, $"FileTransferWriter {Name} WriteToFileContinuous Error:{ex}"));
+                FileHelper.DeleteFile(tempFilePath);
+                FileHelper.DeleteFile(normalFilePath);
+            }
+        }
+
         private void ClearCache()
         {
             if (Directory.Exists(StorageFolderPath))
@@ -150,6 +181,19 @@ namespace FISLib
             }
         }
 
+        private void GetFilePath(DateTime now, out string tempFilePath, out string normalFilePath)
+        {
+            var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
+            var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
+            tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
+            normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+            if (File.Exists(normalFilePath))
+            {
+                now = now.AddMilliseconds(1);
+                GetFilePath(now, out tempFilePath, out normalFilePath);
+            }
+        }
+
         public void Dispose()
         {
             try

+ 8 - 8
Vinno.FIS.TRTCClient.Android/FISTRTCClient.cs

@@ -205,7 +205,7 @@ namespace Vinno.FIS.TRTCClient.Android
         {
             Logger.WriteLineInfo($"OnFirstFrameSend Invoke");
             var firstFrame = new TRTCMessage(EnumMessageType.FirstFrameReceived, new byte[0]);
-            _fileWriterFromTRTCToCommon.WriteToFileContinuous(firstFrame.ToBytes());
+            _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(firstFrame.ToBytes());
         }
 
         private static int GetResolution(int width, int height)
@@ -395,7 +395,7 @@ namespace Vinno.FIS.TRTCClient.Android
                 }
                 if (!_remoteVideoFileWriter.ContainsKey(e.UserId))
                 {
-                    StartNewRemoteVideoFileWrite(e.UserId, e.Width, e.Height);
+                    StartNewRemoteVideoFileWriter(e.UserId, e.Width, e.Height);
                 }
                 if (!_remoteImageList.ContainsKey(e.UserId))
                 {
@@ -404,7 +404,7 @@ namespace Vinno.FIS.TRTCClient.Android
                         Logger.WriteLineInfo($"RemoteImageSize Invoke,UserId:{e.UserId},Width:{e.Width},Height:{e.Height}");
                         var remoteImageSizeData = new TRTCRemoteImageSizeData(e.UserId, e.Width, e.Height);
                         var trtcMessage = new TRTCMessage(EnumMessageType.RemoteImageSizeChanged, remoteImageSizeData.ToBytes());
-                        _fileWriterFromTRTCToCommon.WriteToFileContinuous(trtcMessage.ToBytes());
+                        _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(trtcMessage.ToBytes());
                     }
                 }
                 _remoteVideoFileWriter[e.UserId].WriteToFileContinuous(e.Data);
@@ -415,7 +415,7 @@ namespace Vinno.FIS.TRTCClient.Android
             }
         }
 
-        private static void StartNewRemoteVideoFileWrite(string userId, int width, int height)
+        private static void StartNewRemoteVideoFileWriter(string userId, int width, int height)
         {
             var remoteVideoFileWriter = new FileTransferWriter($"{FISTRTCConsts.ImageTransferFolderForTRTCConsultationRemoteVideo}_{userId}", FISTRTCConsts.ImageTransferFolderForTRTCConsultation, _fisFolderPath);
             if (!_remoteVideoFileWriter.TryAdd(userId, remoteVideoFileWriter))
@@ -427,7 +427,7 @@ namespace Vinno.FIS.TRTCClient.Android
                 Logger.WriteLineInfo($"Start Remote Video File Writer Client :{remoteVideoFileWriter.Name},Width:{width},Height:{height}");
                 var remoteMessage = new TRTCRemoteImageSizeData(userId, width, height);
                 var fileTransferMessageData = new TRTCMessage(EnumMessageType.AddRemoteFileTransfer, remoteMessage.ToBytes());
-                _fileWriterFromTRTCToCommon.WriteToFileContinuous(fileTransferMessageData.ToBytes());
+                _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(fileTransferMessageData.ToBytes());
                 remoteVideoFileWriter.LogMsgThrow += OnLogMsgThrow;
             }
         }
@@ -435,21 +435,21 @@ namespace Vinno.FIS.TRTCClient.Android
         private static void OnEnterRoomError(object sender, EventArgs e)
         {
             var fileTransferMessageData = new TRTCMessage(EnumMessageType.EnterRoomError, null);
-            _fileWriterFromTRTCToCommon.WriteToFileContinuous(fileTransferMessageData.ToBytes());
+            _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(fileTransferMessageData.ToBytes());
             Logger.WriteLineInfo($"OnEnterRoomError Invoked");
         }
 
         private static void OnRemoteUserLeaveRoomArrived(object sender, TRTCRemoteUserLeaveRoomMessage e)
         {
             var fileTransferMessageData = new TRTCMessage(EnumMessageType.RemoteUserLeaveRoom, e.ToBytes());
-            _fileWriterFromTRTCToCommon.WriteToFileContinuous(fileTransferMessageData.ToBytes());
+            _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(fileTransferMessageData.ToBytes());
             Logger.WriteLineInfo($"OnRemoteUserLeaveRoomArrived Invoked,userId:{e.UserId},reason:{e.Reason}.");
         }
 
         private static void OnTryToReconnect(object sender, EventArgs e)
         {
             var fileTransferMessageData = new TRTCMessage(EnumMessageType.TryToReconnect, null);
-            _fileWriterFromTRTCToCommon.WriteToFileContinuous(fileTransferMessageData.ToBytes());
+            _fileWriterFromTRTCToCommon.WriteMessageToFileContinuous(fileTransferMessageData.ToBytes());
             Logger.WriteLineInfo("OnTryToReconnect Invoked.");
         }
     }

+ 54 - 10
Vinno.FIS.TRTCClient.Common/FileTransfer/FileTransferWriter.cs

@@ -66,11 +66,7 @@ namespace Vinno.FIS.TRTCClient.Common.FileTransfer
                 }
                 lock (_lock)
                 {
-                    var now = DateTime.Now;
-                    var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
-                    var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
-                    tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
-                    normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
                     if (Directory.Exists(StorageFolderPath))
                     {
                         using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
@@ -133,11 +129,7 @@ namespace Vinno.FIS.TRTCClient.Common.FileTransfer
                 }
                 lock (_lock)
                 {
-                    var now = DateTime.Now;
-                    var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
-                    var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
-                    tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
-                    normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
                     if (Directory.Exists(StorageFolderPath) && Directory.GetFiles(StorageFolderPath, "*.fis").Length < 5)
                     {
                         using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
@@ -160,6 +152,58 @@ namespace Vinno.FIS.TRTCClient.Common.FileTransfer
             }
         }
 
+        /// <summary>
+        /// 用于一直写
+        /// </summary>
+        /// <param name="byteImage"></param>
+        public void WriteMessageToFileContinuous(byte[] byteImage)
+        {
+            string tempFilePath = null;
+            string normalFilePath = null;
+            try
+            {
+                if (_disposed)
+                {
+                    return;
+                }
+                lock (_lock)
+                {
+                    GetFilePath(DateTime.Now, out tempFilePath, out normalFilePath);
+                    if (Directory.Exists(StorageFolderPath))
+                    {
+                        using (var fileStream = new FileStream(tempFilePath, FileMode.Create))
+                        {
+                            using (BinaryWriter writer = new BinaryWriter(fileStream))
+                            {
+                                writer.Write(byteImage.Length);
+                                writer.Write(byteImage);
+                            }
+                        }
+                        File.Move(tempFilePath, normalFilePath);
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                LogMsgThrow?.Invoke(this, new LogEventArgs(DeviceLogCategory.Error, $"FileTransferWriter {Name} WriteToFileContinuous Error:{ex}"));
+                DeleteFile(tempFilePath);
+                DeleteFile(normalFilePath);
+            }
+        }
+
+        private void GetFilePath(DateTime now, out string tempFilePath, out string normalFilePath)
+        {
+            var tempFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".tmp";
+            var normalFileName = now.ToString("yyyyMMddHHmmss") + now.Millisecond.ToString("D3") + ".fis";
+            tempFilePath = Path.Combine(StorageFolderPath, tempFileName);
+            normalFilePath = Path.Combine(StorageFolderPath, normalFileName);
+            if (File.Exists(normalFilePath))
+            {
+                now = now.AddMilliseconds(1);
+                GetFilePath(now, out tempFilePath, out normalFilePath);
+            }
+        }
+
         private void ClearCache()
         {
             if (Directory.Exists(StorageFolderPath))

+ 20 - 5
Vinno.vCloud.Common.FIS/Consultation/ConsultationClientV2.cs

@@ -1,10 +1,12 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Text.Json;
 using Vinno.IUS.Common.Log;
 using Vinno.vCloud.Common.FIS.Helper;
 using Vinno.vCloud.FIS.CrossPlatform.Common;
 using Vinno.vCloud.FIS.CrossPlatform.Common.Consultation;
+using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
 using Vinno.vCloud.Protocol.Infrastructures;
 using WingInterfaceLibrary.Interface;
 using WingInterfaceLibrary.LiveConsultation;
@@ -307,12 +309,25 @@ namespace Vinno.vCloud.Common.FIS.Consultation
                             }
                             strokePointLists = StrokeDict[whiteBoardDataDTO.UserId].ToList();
                         }
-                        var jsonData = CrossPlatformHelper.Instance.StrokeHelper.GetStrokeJson(strokePointLists, color);
-                        InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, jsonData));
-                        return new ResultInfoDTO
+                        if (CrossPlatformHelper.Instance.Platform == EnumPlatform.Windows)
                         {
-                            IsSuccess = true,
-                        };
+                            var jsonData = CrossPlatformHelper.Instance.StrokeHelper.GetStrokeJson(strokePointLists, color);
+                            InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, jsonData));
+                            return new ResultInfoDTO
+                            {
+                                IsSuccess = true,
+                            };
+                        }
+                        else
+                        {
+                            var strokePointsInfo = new StrokePointsInfo(color, strokePointLists);
+                            var jsonData = JsonSerializer.Serialize(strokePointsInfo);
+                            InteractiveBoardInfoArrived?.Invoke(this, new InteractiveBoardInfo(whiteBoardDataDTO.UserId, AccountId, null, jsonData));
+                            return new ResultInfoDTO
+                            {
+                                IsSuccess = true,
+                            };
+                        }
                     }
                     else
                     {

+ 5 - 4
Vinno.vCloud.Common.FIS/Consultation/ConsultationRecipientV2.cs

@@ -466,10 +466,7 @@ namespace Vinno.vCloud.Common.FIS.Consultation
                 {
                     ConsultationInfo.ConsultationMemberInfos.Add(consultationMemberChangeDTO.MemberInfo);
                 }
-                else
-                {
-                    memberInfo = consultationMemberChangeDTO.MemberInfo;
-                }
+                memberInfo = consultationMemberChangeDTO.MemberInfo;
             }
             else if (consultationMemberChangeDTO.MemberInfo.OperationType == ClientMessageOperationType.Delete)
             {
@@ -478,6 +475,10 @@ namespace Vinno.vCloud.Common.FIS.Consultation
                     ConsultationInfo?.ConsultationMemberInfos.Remove(memberInfo);
                 }
             }
+            if (memberInfo != null)
+            {
+                consultationMemberChangeDTO.MemberInfo.RoleType = memberInfo.RoleType;
+            }
             var meetingChangeMember = DTOConverter.ConvertConsultationMemberChangeDTOToConsultationMemberNotificaiton(consultationMemberChangeDTO);
             HandleConsultationMemberNotificationArrived(meetingChangeMember);
             return new ResultInfoDTO

+ 1 - 1
Vinno.vCloud.FIS.CrossPlatform.Android/Consultation/RTC/RtcRoom.cs

@@ -142,7 +142,7 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Android.Consultation.RTC
         private void SendMessage(TRTCMessage message)
         {
             CrossPlatformHelper.Instance.LogWriter?.WriteLineInfo($"RTCRoom SendMessage Invoke,{message.MessageType}");
-            FISAndroid.FileWrtierFromCommonToTRTCConsultation.WriteToFileContinuous(message.ToBytes());
+            FISAndroid.FileWrtierFromCommonToTRTCConsultation.WriteMessageToFileContinuous(message.ToBytes());
         }
 
         private void StartLocalVideoFileWriter()

+ 2 - 0
Vinno.vCloud.FIS.CrossPlatform.Android/FISAndroid.cs

@@ -11,6 +11,7 @@ using Vinno.vCloud.FIS.CrossPlatform.Android.Consultation.RTMP;
 using Vinno.vCloud.FIS.CrossPlatform.Android.Hardware;
 using Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo;
 using Vinno.vCloud.FIS.CrossPlatform.Common;
+using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
 using Vinno.vCloud.FIS.CrossPlatform.Common.Helper;
 
 namespace Vinno.vCloud.FIS.CrossPlatform.Android
@@ -50,6 +51,7 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Android
             DirectoryHelper.CreateDirectory(FISFolderPath);
             DirectoryHelper.CreateDirectory(FISLogPath);
             MobileInfo.Initialize();
+            CrossPlatformHelper.Instance.Platform = EnumPlatform.Android;
             CrossPlatformHelper.Instance.LiveVideoPusherCreator = new LiveVideoPusherCreator();
             CrossPlatformHelper.Instance.HardwareDetector = new HardwareDetector();
             CrossPlatformHelper.Instance.ImageHelperCreator = new ImageHelperCreator();

+ 16 - 1
Vinno.vCloud.FIS.CrossPlatform.Android/Hardware/HardwareDetector.cs

@@ -3,6 +3,7 @@ using Android.Content;
 using Android.Hardware.Camera2;
 using Android.Media;
 using Android.OS;
+using Java.Lang;
 using System;
 using System.Collections.Generic;
 using Vinno.FIS.TRTCClient.Common.Enum;
@@ -187,8 +188,22 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Android.Hardware
                     {
                         foreach (var cameraId in _cameraManager.GetCameraIdList())
                         {
+                            var characteristics = _cameraManager.GetCameraCharacteristics(cameraId);
+                            var facing = characteristics.Get(CameraCharacteristics.LensFacing);
                             //默认打开前置摄像头
-                            hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Camera_" + cameraId, new List<CameraCaptureCapability>(), 0, false));
+                            if (facing != null && facing == Integer.ValueOf((int)LensFacing.Back))
+                            {
+                                hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Back Camera", new List<CameraCaptureCapability>(), 0, false));
+                            }
+                            else if (facing != null && facing == Integer.ValueOf((int)LensFacing.Front))
+                            {
+                                hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "Front Camera", new List<CameraCaptureCapability>(), 0, false));
+                            }
+                            else if (facing != null && facing == Integer.ValueOf((int)LensFacing.External))
+                            {
+                                externalNumber++;
+                                hardwareList.Add(new HardwareInfo(EnumHardwareType.Camera, cameraId, new List<string>(), "External Camera" + externalNumber, new List<CameraCaptureCapability>(), 0, false));
+                            }
                         }
                     }
                     catch (Exception ex)

+ 1 - 1
Vinno.vCloud.FIS.CrossPlatform.Android/LiveVideo/RTC/RtcMultiPusherV2.cs

@@ -438,7 +438,7 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Android.LiveVideo.RTC
 
             private void SendMessage(byte[] bytes)
             {
-                FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteToFileContinuous(bytes);
+                FISAndroid.FileWrtierFromCommonToTRTCLiveVideo.WriteMessageToFileContinuous(bytes);
             }
 
             private void StartImageSender()

+ 13 - 0
Vinno.vCloud.FIS.CrossPlatform.Common/Consultation/StrokePoint.cs

@@ -22,4 +22,17 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Common.Consultation
         /// </summary>
         public List<StrokePoint> StrokePoints { get; set; }
     }
+
+    public class StrokePointsInfo
+    {
+        public uint Color { get; set; }
+
+        public List<StrokePointList> StrokePointLists { get; set; }
+
+        public StrokePointsInfo(uint color, List<StrokePointList> strokePointLists)
+        {
+            Color = color;
+            StrokePointLists = strokePointLists;
+        }
+    }
 }

+ 3 - 0
Vinno.vCloud.FIS.CrossPlatform.Common/CrossPlatformHelper.cs

@@ -1,4 +1,5 @@
 using Vinno.vCloud.FIS.CrossPlatform.Common.Consultation.Interface;
+using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
 using Vinno.vCloud.FIS.CrossPlatform.Common.Hardware.Interface;
 using Vinno.vCloud.FIS.CrossPlatform.Common.LiveVideo.Interface;
 using Vinno.vCloud.FIS.CrossPlatform.Common.Log.Interface;
@@ -14,6 +15,8 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Common
         /// </summary>
         public static CrossPlatformHelper Instance => _instance ?? (_instance = new CrossPlatformHelper());
 
+        public EnumPlatform Platform { get; set; }
+
         /// <summary>
         /// Gets or sets LiveVideoPusherCreator to create pusher for different platform
         /// </summary>

+ 9 - 0
Vinno.vCloud.FIS.CrossPlatform.Common/Enum/EnumPlatform.cs

@@ -0,0 +1,9 @@
+namespace Vinno.vCloud.FIS.CrossPlatform.Common.Enum
+{
+    public enum EnumPlatform
+    {
+        Windows,
+        Android,
+        IOS,
+    }
+}

+ 2 - 0
Vinno.vCloud.FIS.CrossPlatform.Windows/FISWin.cs

@@ -3,6 +3,7 @@ using ManageLiteAV;
 using System;
 using System.IO;
 using Vinno.vCloud.FIS.CrossPlatform.Common;
+using Vinno.vCloud.FIS.CrossPlatform.Common.Enum;
 using Vinno.vCloud.FIS.CrossPlatform.Common.Helper;
 using Vinno.vCloud.FIS.CrossPlatform.Windows.Consultation;
 using Vinno.vCloud.FIS.CrossPlatform.Windows.Consultation.RTC;
@@ -33,6 +34,7 @@ namespace Vinno.vCloud.FIS.CrossPlatform.Windows
             }
             DirectoryHelper.CreateDirectory(FISLogPath);
             CpuInfo.Initialize();// initialize cpu info
+            CrossPlatformHelper.Instance.Platform = EnumPlatform.Windows;
             CrossPlatformHelper.Instance.LiveVideoPusherCreator = new LiveVideoPusherCreator();
             CrossPlatformHelper.Instance.LiveVideoPusherCreatorForSonopost = new LiveVideoPusherCreatorForSonopost();
             CrossPlatformHelper.Instance.LiveVideoPusherCreatorV2 = new LiveVideoPusherCreatorV2();