IVideoCompositor 接口

定义

实现的用于创建自定义视频合成器的接口。

public interface class IVideoCompositor : IMediaExtension
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(2232464446, 16908, 16911, 150, 199, 124, 152, 187, 161, 252, 85)]
struct IVideoCompositor : IMediaExtension
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(2232464446, 16908, 16911, 150, 199, 124, 152, 187, 161, 252, 85)]
public interface IVideoCompositor : IMediaExtension
Public Interface IVideoCompositor
Implements IMediaExtension
属性
实现

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

using Microsoft.Graphics.Canvas;使用 Microsoft.Graphics.Canvas.Effects;使用 Windows.Foundation.Collections;使用 Windows.Graphics.DirectX.Direct3D11;使用 Windows.Media.Effects;使用 Windows.Media.MediaProperties;使用 Windows.UI;

namespace Effects
{
    /// <summary>
    /// Chroma key compositor
    /// <list type="Bullet">
    /// <listheader>Properties</listheader>
    /// <item>Color (Color) : Chroma key color (default is black)</item>
    /// <item>Feather (Boolean): true to soften edges of the output (default is false)</item>
    /// <item>Tolerance (float): Color tolerance 0-1 (default is 0.1)</item>
    /// <item>InvertAlpha (Boolean): invert the alpha value (default is false)</item>
    /// </list>
    /// </summary>
    public sealed class ChromaKeyVideoCompositor : IVideoCompositor
    {
        #region Fields
        private VideoEncodingProperties _backgroundProperties;
        private CanvasDevice _canvasDevice;
        #endregion

        #region Properties
        /// <summary>
        /// Gets the chroma-key color
        /// </summary>
        public Color Color { get; private set; } = Colors.Black;

        /// <summary>
        /// Gets a value indicating whether to feather the edges of the chroma key
        /// </summary>
        public bool Feather { get; private set; } = false;

        /// <summary>
        /// Gets the color tolerance 
        /// </summary>
        public float Tolerance { get; private set; } = 0.1f;

        /// <summary>
        /// Gets a value indicating whether to invert the alpha transparency
        /// </summary>
        public bool InvertAlpha { get; private set; } = false;

        /// <summary>
        /// Gets a value indicating whether the compositor is time-independent
        /// </summary>
        public bool TimeIndependent => true;
        #endregion

        #region Methods
        /// <summary>
        /// Sets the encoding properties
        /// </summary>
        /// <param name="backgroundProperties">the background properties</param>
        /// <param name="device">the Direct3D device</param>
        public void SetEncodingProperties(VideoEncodingProperties backgroundProperties, IDirect3DDevice device)
        {
            _backgroundProperties = backgroundProperties;

            _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device);
        }

        /// <summary>
        /// Composite the frame
        /// </summary>
        /// <param name="context">the composite frame context</param>
        public void CompositeFrame(CompositeVideoFrameContext context)
        {
            foreach (var surface in context.SurfacesToOverlay)
            {
                using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, surface))
                using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
                using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
                using (var chromaKeyEffect = new ChromaKeyEffect
                {
                    Source = inputBitmap,
                    Color = Color,
                    Feather = Feather,
                    Tolerance = Tolerance,
                    InvertAlpha = InvertAlpha
                })
                {
                    var overlay = context.GetOverlayForSurface(surface);

                    var destinationRectangle = overlay.Position;

                    var sourceRectangle = inputBitmap.Bounds;

                    var opacity = System.Convert.ToSingle(overlay.Opacity);

                    ds.DrawImage(chromaKeyEffect, destinationRectangle, sourceRectangle, opacity);
                }
            }
        }

        /// <summary>
        /// Close the compositor & dispose of the canvas device
        /// </summary>
        /// <param name="reason">the media effect closed reason</param>
        public void Close(MediaEffectClosedReason reason)
        {
            if (_canvasDevice != null)
            {
                _canvasDevice.Dispose();
                _canvasDevice = null;
            }
        }

        /// <summary>
        /// Discard of the queued frames
        /// </summary>
        /// <remarks>this does nothing</remarks>
        public void DiscardQueuedFrames()
        {
        }

        /// <summary>
        /// Sets the properties passed into the compositor
        /// </summary>
        /// <param name="configuration">the configuration</param>
        public void SetProperties(IPropertySet configuration)
        {
            if (configuration == null)
            {
                return;
            }

            object value;

            if (configuration.TryGetValue("Color", out value))
            {
                Color = (Color)value;
            }

            if (configuration.TryGetValue("Tolerance", out value))
            {
                Tolerance = (float)value;
            }

            if (configuration.TryGetValue("Feather", out value))
            {
                Feather = (bool)value;
            }

            if (configuration.TryGetValue("InvertAlpha", out value))
            {
                InvertAlpha = (bool)value;
            }
        }
        #endregion
    }
}

注解

  1. 在 Windows 运行时 组件项目中,从此接口派生公共密封类 (请参阅以下示例) 。

  2. MediaOverlayLayer 构造函数中使用完整的类名。

    var propertySet = new PropertySet { [“Feather”] = true, [“Tolerance”] = 0.2f, [“Color”] = Colors.Blue };

    var compositorDefinition = new VideoCompositorDefinition (“Effects.ChromaKeyVideoCompositor”, propertySet) ;

    var mediaOverlayLayer = new MediaOverlayLayer (compositorDefinition) ;

属性

TimeIndependent

获取一个值,该值指示自定义视频效果是否与时间无关。

方法

Close(MediaEffectClosedReason)

当视频合成器应关闭并清理分配的资源时调用。

CompositeFrame(CompositeVideoFrameContext)

当帧可由自定义视频合成器进行合成时调用。

DiscardQueuedFrames()

调用 以允许视频合成器实现选择性地放弃与已接收的帧相关的任何存储状态。

SetEncodingProperties(VideoEncodingProperties, IDirect3DDevice)

调用 以设置自定义视频合成器的编码属性。

SetProperties(IPropertySet)

设置注册媒体分析程序或编解码器时提供的配置属性。

(继承自 IMediaExtension)

适用于

另请参阅