Ejemplo n.º 1
0
        /// <summary>
        /// Updates a scaling policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">Scaling policy to update</param>
        public string UpdateScalingPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-SCALING-POLICY-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

            // Construct the request
            string policyName    = $"projects/{projectId}/locations/global/scalingPolicies/{policyId}";
            var    scalingPolicy = new ScalingPolicy
            {
                Name     = policyName,
                Priority = 10,
            };
            var fieldMask = new FieldMask
            {
                Paths = { "priority" }
            };

            // Call the API
            try
            {
                var updated = client.UpdateScalingPolicy(scalingPolicy, fieldMask);

                // Inspect the result
                return($"Scaling policy updated: {policyName} updated. Operation Id: {updated.Name}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"UpdateScalingPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
        /************************************************************************/

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="TransformParameters"/> class.
        /// </summary>
        /// <param name="targetSize">The target size.</param>
        /// <param name="pixelFormat">The pixel format. Only PixelFormats.Pbgra32, Bgr24, and Gray8 are supported.</param>
        /// <param name="scalePolicy">Scale policy.</param>
        /// <param name="scaleQuality">Scale quality.</param>
        /// <param name="region">Region of interest, or empty rectangle for all.</param>
        public TransformParameters(Size targetSize, PixelFormat pixelFormat, ScalingPolicy scalePolicy, ScalingQuality scaleQuality, Rectangle region)
        {
            TargetSize   = targetSize;
            PixelFormat  = pixelFormat;
            ScalePolicy  = scalePolicy;
            ScaleQuality = scaleQuality;
            Region       = region;
        }
Ejemplo n.º 3
0
 public TransformParameters(RectangleF regionOfInterest, Size targetFrameSize, ScalingPolicy scalePolicy,
                            PixelFormat targetFormat, ScalingQuality scaleQuality)
 {
     RegionOfInterest = regionOfInterest;
     TargetFrameSize  = targetFrameSize;
     TargetFormat     = targetFormat;
     ScaleQuality     = scaleQuality;
     ScalePolicy      = scalePolicy;
 }
        /// <summary>
        /// Create a new scaling policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">Id of the scaling policy</param>
        /// <param name="deploymentId">Id of the deployment to scale</param>
        public string CreateScalingPolicy(
            string projectId    = "YOUR-PROJECT-ID",
            string policyId     = "YOUR-SCALING-POLICY-ID",
            string deploymentId = "YOUR-DEPLOYMENT-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

            // Construct the request
            string parent         = $"projects/{projectId}/locations/global";
            string policyName     = $"{parent}/scalingPolicies/{policyId}";
            string deploymentName = $"{parent}/gameServerDeployments/{deploymentId}";

            var autoscalerSettings = new FleetAutoscalerSettings
            {
                BufferSizeAbsolute = 1,
                MinReplicas        = 1,
                MaxReplicas        = 2
            };
            var scalingPolicy = new ScalingPolicy
            {
                Name     = policyName,
                Priority = 1,
                FleetAutoscalerSettings = autoscalerSettings,
                GameServerDeployment    = deploymentName
            };
            var scalingPolicyRequest = new CreateScalingPolicyRequest
            {
                Parent          = parent,
                ScalingPolicyId = policyId,
                ScalingPolicy   = scalingPolicy
            };

            // Call the API
            try
            {
                var created = client.CreateScalingPolicy(scalingPolicyRequest);

                // Inspect the result
                return($"Scaling policy created for {policyName}. Operation Id {created.Name}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"CreateScalingPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <exception cref="DecoderException"></exception>
        public static FFmpegDecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParameters,
                                                      PostVideoDecodingParameters postVideoDecodingParameters)
        {
            if (decodedVideoFrameParameters == null)
            {
                throw new ArgumentNullException(nameof(decodedVideoFrameParameters));
            }
            if (postVideoDecodingParameters == null)
            {
                throw new ArgumentNullException(nameof(postVideoDecodingParameters));
            }

            int sourceLeft   = 0;
            int sourceTop    = 0;
            int sourceWidth  = decodedVideoFrameParameters.Width;
            int sourceHeight = decodedVideoFrameParameters.Height;
            int scaledWidth  = decodedVideoFrameParameters.Width;
            int scaledHeight = decodedVideoFrameParameters.Height;

            if (!postVideoDecodingParameters.RegionOfInterest.IsEmpty)
            {
                sourceLeft =
                    (int)(decodedVideoFrameParameters.Width * postVideoDecodingParameters.RegionOfInterest.Left);
                sourceTop =
                    (int)(decodedVideoFrameParameters.Height * postVideoDecodingParameters.RegionOfInterest.Top);
                sourceWidth =
                    (int)(decodedVideoFrameParameters.Width * postVideoDecodingParameters.RegionOfInterest.Width);
                sourceHeight =
                    (int)(decodedVideoFrameParameters.Height * postVideoDecodingParameters.RegionOfInterest.Height);
            }

            if (!postVideoDecodingParameters.TargetFrameSize.IsEmpty)
            {
                scaledWidth  = postVideoDecodingParameters.TargetFrameSize.Width;
                scaledHeight = postVideoDecodingParameters.TargetFrameSize.Height;

                ScalingPolicy scalingPolicy = postVideoDecodingParameters.ScalePolicy;

                float srcAspectRatio  = (float)sourceWidth / sourceHeight;
                float destAspectRatio = (float)scaledWidth / scaledHeight;

                if (scalingPolicy == ScalingPolicy.Auto)
                {
                    float relativeChange = Math.Abs(srcAspectRatio - destAspectRatio) / srcAspectRatio;

                    scalingPolicy = relativeChange > MaxAspectRatioError
                        ? ScalingPolicy.RespectAspectRatio
                        : ScalingPolicy.Stretch;
                }

                if (scalingPolicy == ScalingPolicy.RespectAspectRatio)
                {
                    if (destAspectRatio < srcAspectRatio)
                    {
                        scaledHeight = sourceHeight * scaledWidth / sourceWidth;
                    }
                    else
                    {
                        scaledWidth = sourceWidth * scaledHeight / sourceHeight;
                    }
                }
            }

            PixelFormat          scaledPixelFormat       = postVideoDecodingParameters.TargetFormat;
            FFmpegPixelFormat    scaledFFmpegPixelFormat = GetFFmpegPixelFormat(scaledPixelFormat);
            FFmpegScalingQuality scaleQuality            = GetFFmpegScaleQuality(postVideoDecodingParameters.ScaleQuality);

            int resultCode = FFmpegVideoPInvoke.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight,
                                                                  decodedVideoFrameParameters.PixelFormat,
                                                                  scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out var handle);

            if (resultCode != 0)
            {
                throw new DecoderException(@"An error is occurred while creating scaler, code: {resultCode}");
            }

            return(new FFmpegDecodedVideoScaler(handle, scaledWidth, scaledHeight, scaledPixelFormat));
        }
Ejemplo n.º 6
0
        /// <exception cref="DecoderException"></exception>
        public static DecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParms, TransformParameters transformParms)
        {
            if (decodedVideoFrameParms == null)
            {
                throw new ArgumentNullException(nameof(decodedVideoFrameParms));
            }
            if (transformParms == null)
            {
                throw new ArgumentNullException(nameof(transformParms));
            }

            int sourceLeft   = 0;
            int sourceTop    = 0;
            int sourceWidth  = decodedVideoFrameParms.Width;
            int sourceHeight = decodedVideoFrameParms.Height;
            int scaledWidth  = decodedVideoFrameParms.Width;
            int scaledHeight = decodedVideoFrameParms.Height;

            if (!transformParms.Region.IsEmpty)
            {
                sourceLeft   = transformParms.Region.Left;
                sourceTop    = transformParms.Region.Top;
                sourceWidth  = transformParms.Region.Width;
                sourceHeight = transformParms.Region.Height;
            }

            if (!transformParms.TargetSize.IsEmpty)
            {
                scaledWidth  = transformParms.TargetSize.Width;
                scaledHeight = transformParms.TargetSize.Height;

                ScalingPolicy scalingPolicy = transformParms.ScalePolicy;

                float srcAspectRatio  = (float)sourceWidth / sourceHeight;
                float destAspectRatio = (float)scaledWidth / scaledHeight;

                if (scalingPolicy == ScalingPolicy.Auto)
                {
                    float relativeChange = Math.Abs(srcAspectRatio - destAspectRatio) / srcAspectRatio;

                    scalingPolicy = relativeChange > MaxAspectRatioError
                        ? ScalingPolicy.RespectAspectRatio
                        : ScalingPolicy.Stretch;
                }

                if (scalingPolicy == ScalingPolicy.RespectAspectRatio)
                {
                    if (destAspectRatio < srcAspectRatio)
                    {
                        scaledHeight = sourceHeight * scaledWidth / sourceWidth;
                    }
                    else
                    {
                        scaledWidth = sourceWidth * scaledHeight / sourceHeight;
                    }
                }
            }

            FFmpegPixelFormat    pixelFormat  = ToFFmpegPixelFormat(transformParms.PixelFormat);
            FFmpegScalingQuality scaleQuality = ToFFmpegScaleQuality(transformParms.ScaleQuality);

            int resultCode = FFmpegVideoPInvoke.CreateVideoScaler
                             (
                sourceLeft, sourceTop, sourceWidth, sourceHeight,
                decodedVideoFrameParms.PixelFormat,
                scaledWidth, scaledHeight,
                pixelFormat, scaleQuality,
                out var handle
                             );

            if (resultCode != 0)
            {
                throw new DecoderException(@"An error occurred while creating scaler, code: {resultCode}");
            }

            return(new DecodedVideoScaler(handle, scaledWidth, scaledHeight, transformParms.PixelFormat));
        }
        /// <exception cref="DecoderException"></exception>
        public static FFmpegDecodedVideoScaler Create(DecodedVideoFrameParameters decodedVideoFrameParameters,
                                                      TransformParameters transformParameters)
        {
            if (decodedVideoFrameParameters == null)
            {
                throw new ArgumentNullException(nameof(decodedVideoFrameParameters));
            }
            if (transformParameters == null)
            {
                throw new ArgumentNullException(nameof(transformParameters));
            }

            int sourceLeft   = 0;
            int sourceTop    = 0;
            int sourceWidth  = decodedVideoFrameParameters.Width;
            int sourceHeight = decodedVideoFrameParameters.Height;
            int scaledWidth  = decodedVideoFrameParameters.Width;
            int scaledHeight = decodedVideoFrameParameters.Height;

            if (!transformParameters.RegionOfInterest.IsEmpty)
            {
                sourceLeft =
                    (int)(decodedVideoFrameParameters.Width * transformParameters.RegionOfInterest.Left);
                sourceTop =
                    (int)(decodedVideoFrameParameters.Height * transformParameters.RegionOfInterest.Top);
                sourceWidth =
                    (int)(decodedVideoFrameParameters.Width * transformParameters.RegionOfInterest.Width);
                sourceHeight =
                    (int)(decodedVideoFrameParameters.Height * transformParameters.RegionOfInterest.Height);
            }

            if (!transformParameters.TargetFrameSize.IsEmpty)
            {
                scaledWidth  = transformParameters.TargetFrameSize.Width;
                scaledHeight = transformParameters.TargetFrameSize.Height;

                ScalingPolicy scalingPolicy = transformParameters.ScalePolicy;

                float srcAspectRatio  = (float)sourceWidth / sourceHeight;
                float destAspectRatio = (float)scaledWidth / scaledHeight;

                if (scalingPolicy == ScalingPolicy.Auto)
                {
                    float relativeChange = Math.Abs(srcAspectRatio - destAspectRatio) / srcAspectRatio;

                    scalingPolicy = relativeChange > MaxAspectRatioError
                        ? ScalingPolicy.RespectAspectRatio
                        : ScalingPolicy.Stretch;
                }

                if (scalingPolicy == ScalingPolicy.RespectAspectRatio)
                {
                    if (destAspectRatio < srcAspectRatio)
                    {
                        scaledHeight = sourceHeight * scaledWidth / sourceWidth;
                    }
                    else
                    {
                        scaledWidth = sourceWidth * scaledHeight / sourceHeight;
                    }
                }
            }

            PixelFormat          scaledPixelFormat       = transformParameters.TargetFormat;
            FFmpegPixelFormat    scaledFFmpegPixelFormat = GetFFmpegPixelFormat(scaledPixelFormat);
            FFmpegScalingQuality scaleQuality            = GetFFmpegScaleQuality(transformParameters.ScaleQuality);
            int    resultCode;
            IntPtr handle;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                resultCode = FFmpegVideoPInvokeWin.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight,
                                                                     decodedVideoFrameParameters.PixelFormat,
                                                                     scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out handle);
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                resultCode = FFmpegVideoPInvokeLinux.CreateVideoScaler(sourceLeft, sourceTop, sourceWidth, sourceHeight,
                                                                       decodedVideoFrameParameters.PixelFormat,
                                                                       scaledWidth, scaledHeight, scaledFFmpegPixelFormat, scaleQuality, out handle);
            }
            else
            {
                throw new PlatformNotSupportedException();
            }


            if (resultCode != 0)
            {
                throw new DecoderException(@"An error occurred while creating scaler, code: {resultCode}");
            }

            return(new FFmpegDecodedVideoScaler(handle, scaledWidth, scaledHeight, scaledPixelFormat));
        }