Browse Source

设备端上报设备画面尺寸信息接口调整

Jeremy 2 years ago
parent
commit
61a14f93fd
1 changed files with 141 additions and 12 deletions
  1. 141 12
      Service/DeviceService.cs

+ 141 - 12
Service/DeviceService.cs

@@ -1025,21 +1025,11 @@ namespace WingDeviceService.Service
             var deviceCode = await GetClientIdByTokenAsync(request.Token);
             var deviceDTO = await _deviceInfoDBServiceProxy.FindDeviceInfoByCodeAsync(deviceCode);
 
+            var screenAndCamera = request.VideoDeviceInfos.Any(x => x.VideoDeviceSourceType == VideoDeviceSourceTypeEnum.Camera);
             var videoDeviceInfos = new List<VideoDeviceDTO>();
             foreach (var video in request.VideoDeviceInfos)
             {
-                var videoDeviceInfo = new VideoDeviceDTO
-                {
-                    VideoDeviceId = video.VideoDeviceId,
-                    VideoDeviceSourceType = video.VideoDeviceSourceType,
-                    Width = video.Width,
-                    Height = video.Height,
-                    OutputWidth = video.Width,
-                    OutputHeight = video.Height,
-                    VideoFps = video.VideoFps,
-                    VideoBitrate = video.VideoBitrate,
-                    MinVideoBitrate = video.MinVideoBitrate
-                };
+                var videoDeviceInfo = InitOutputInfo(video, deviceDTO.MergedChannel, screenAndCamera, deviceDTO.MergedVideoOutputWidth, deviceDTO.MergedVideoOutputHeight);
                 videoDeviceInfos.Add(videoDeviceInfo);
             }
             deviceDTO.VideoDeviceInfos = videoDeviceInfos;
@@ -1056,6 +1046,145 @@ namespace WingDeviceService.Service
             return result;
         }
 
+        private VideoDeviceDTO InitOutputInfo(VideoDeviceInfo videoDeviceInfo, bool mergedChannel, bool screenAndCamera, int maxWidth, int maxHeight)
+        {
+            // 给到设备端的输出尺寸,一定需要保持上报设备分辨率的宽高比
+            // CPU好的输出尺寸不能超过1080P,CPU较差的输出尺寸不能超过720P
+
+            // 单路情况,主频画面小于推荐尺寸,保持不变;主屏画面大于推荐尺寸,保持上报设备分辨率宽高比缩小
+            // (CPU好的情况帧率20,码率3000,最小帧率设为0(SDK自由设置)
+            // (CPU较差的情况)帧率15,码率2000,最小帧率设为0(SDK自由设置)
+
+            // 多路情况:1080P降到1280*720,720P降到1280*540
+            // 多路分流,保持上报设备分辨率宽高比缩小
+            // 多路合流(摄像头):640*480(CPU好的情况);640*360(CPU较差的情况)
+            // (CPU好的情况帧率20,码率3000,最小帧率设为2000
+            // (CPU较差的情况)帧率15,码率2000,最小帧率设为1000
+
+            var bestCpu = maxWidth == 1920;
+            var outputInfo = new VideoDeviceDTO
+            {
+                VideoDeviceId = videoDeviceInfo.VideoDeviceId,
+                VideoDeviceSourceType = videoDeviceInfo.VideoDeviceSourceType,
+                Width = videoDeviceInfo.Width,
+                Height = videoDeviceInfo.Height,
+                OutputWidth = videoDeviceInfo.Width,
+                OutputHeight = videoDeviceInfo.Height,
+            };
+            //输出分辨率
+            var width = videoDeviceInfo.Width;
+            var height = Math.Max(videoDeviceInfo.Height, 1);
+            if (!screenAndCamera)
+            {
+                //单路
+                if (bestCpu)
+                {
+                    InitFpsAndBitrate(outputInfo, 20, 3000, 0);
+                    InitDesktopOutputSize(outputInfo, 1920, 1080);
+                }
+                else
+                {
+                    InitFpsAndBitrate(outputInfo, 15, 2000, 0);
+                    InitDesktopOutputSize(outputInfo, 1280, 720);
+                }
+            }
+            else
+            {
+                if (mergedChannel)
+                {
+                    //多路合流
+                    if (bestCpu)
+                    {
+                        InitFpsAndBitrate(outputInfo, 20, 3000, 2000);
+                        InitDesktopOutputSize(outputInfo, 1280, 720);
+                        InitCameraOutputSize(outputInfo, 640, 480);
+                    }
+                    else
+                    {
+                        InitFpsAndBitrate(outputInfo, 15, 2000, 1000);
+                        InitDesktopOutputSize(outputInfo, 1280, 540);
+                        InitCameraOutputSize(outputInfo, 640, 360);
+                    }
+                }
+                else
+                {
+                    //多路分流
+                    if (bestCpu)
+                    {
+                        InitFpsAndBitrate(outputInfo, 20, 3000, 2000);
+                        InitDesktopOutputSize(outputInfo, 1280, 720);
+                        InitCameraOutputSize(outputInfo, 1280, 720);
+                    }
+                    else
+                    {
+                        InitFpsAndBitrate(outputInfo, 15, 2000, 1000);
+                        InitDesktopOutputSize(outputInfo, 1280, 540);
+                        InitCameraOutputSize(outputInfo, 1280, 540);
+                    }
+                }
+            }
+            return outputInfo;
+        }
+
+        private void InitFpsAndBitrate(VideoDeviceDTO outputInfo, int videoFps, int videoBitrate, int minVideoBitrate)
+        {
+            outputInfo.VideoFps = videoFps;
+            outputInfo.VideoBitrate = videoBitrate;
+            outputInfo.MinVideoBitrate = minVideoBitrate;
+        }
+
+        private void InitDesktopOutputSize(VideoDeviceDTO outputInfo, int maxWidth, int maxHeight)
+        {
+            if (outputInfo.VideoDeviceSourceType == VideoDeviceSourceTypeEnum.Desktop)
+            {
+                if (outputInfo.Width <= maxWidth && outputInfo.Height <= maxHeight)
+                {
+                    outputInfo.OutputWidth = outputInfo.Width;
+                    outputInfo.OutputHeight = outputInfo.Height;
+                }
+                else
+                {
+                    var radio = (double)outputInfo.Width / Math.Max(outputInfo.Height, 1);
+                    if (outputInfo.Height > maxHeight)
+                    {
+                        outputInfo.OutputHeight = maxHeight;
+                        outputInfo.OutputWidth = (int)(maxHeight * radio);
+                    }
+                    if (outputInfo.OutputWidth > maxWidth)
+                    {
+                        outputInfo.OutputWidth = maxWidth;
+                        outputInfo.OutputHeight = (int)(maxWidth / radio);
+                    }
+                }
+            }
+        }
+
+        private void InitCameraOutputSize(VideoDeviceDTO outputInfo, int maxWidth, int maxHeight)
+        {
+            if (outputInfo.VideoDeviceSourceType == VideoDeviceSourceTypeEnum.Camera)
+            {
+                if (outputInfo.Width <= maxWidth && outputInfo.Height <= maxHeight)
+                {
+                    outputInfo.OutputWidth = outputInfo.Width;
+                    outputInfo.OutputHeight = outputInfo.Height;
+                }
+                else
+                {
+                    var radio = (double)outputInfo.Width / Math.Max(outputInfo.Height, 1);
+                    if (outputInfo.Height > maxHeight)
+                    {
+                        outputInfo.OutputHeight = maxHeight;
+                        outputInfo.OutputWidth = (int)(maxHeight * radio);
+                    }
+                    if (outputInfo.OutputWidth > maxWidth)
+                    {
+                        outputInfo.OutputWidth = maxWidth;
+                        outputInfo.OutputWidth = (int)(maxWidth / radio);
+                    }
+                }
+            }
+        }
+
         /// <summary>
         /// 查找有效设备总数量
         /// </summary>