MediaClip不会使用自定义IBasicVideoEffect呈现

时间:2016-02-08 11:56:36

标签: c# .net uwp video-processing video-effects

我正在尝试根据https://github.com/aarononeal/media-contrib示例应用IBasicVideoEffect实现的饱和度视频效果。

当我将MediaEffectDefinition添加到MediaClip时,会设置预览视频,但不会呈现任何内容。 (视频消失)

private void AddSaturationEffectButton_Click(object sender, RoutedEventArgs e)
{
    var clip = _composition.Clips[0];
    clip.VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(SaturationVideoEffect).FullName));

    SetupMediaStreamSource();
}

SaturationVideoEffect在独立的Windows运行时组件(通用Windows)项目中实现。

using System;
using System.Collections.Generic;
using Windows.Foundation.Collections;
using Windows.Graphics.DirectX.Direct3D11;
using Windows.Media.Effects;
using Windows.Media.MediaProperties;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;

namespace VideoEffectsLibrary
{
    public sealed class SaturationVideoEffect : IBasicVideoEffect
    {
        private VideoEncodingProperties _currentEncodingProperties;
        private CanvasDevice _canvasDevice;
        private IPropertySet _configuration;

        private float Saturation
        {
            get
            {
                if (_configuration != null && _configuration.ContainsKey("Saturation"))
                    return (float)_configuration["Saturation"];
                else
                    return 0.5f;
            }
            set
            {
                _configuration["Saturation"] = value;
            }
        }

        public void ProcessFrame(ProcessVideoFrameContext context)
        {
            using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface))
            using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface))
            using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession())
            {
                var saturation = new SaturationEffect()
                {
                    Source = inputBitmap,
                    Saturation = this.Saturation
                };
                ds.DrawImage(saturation);
            }
        }

        public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device)
        {
            _currentEncodingProperties = encodingProperties;
            _canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device);
            CanvasDevice.DebugLevel = CanvasDebugLevel.Error;
        }

        public void SetProperties(IPropertySet configuration)
        {
            _configuration = configuration;
        }

        public bool IsReadOnly { get { return false; } }
        public MediaMemoryTypes SupportedMemoryTypes { get { return MediaMemoryTypes.Gpu; } }
        public bool TimeIndependent { get { return false; } }

        public IReadOnlyList<VideoEncodingProperties> SupportedEncodingProperties
        {
            get
            {
                return new List<VideoEncodingProperties>()
                {
                    // NOTE: Specifying width and height is only necessary due to bug in media pipeline when
                    // effect is being used with Media Capture. 
                    // This can be changed to "0, 0" in a future release of FBL_IMPRESSIVE. 
                    VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Argb32, 800, 600)
                };
            }
        }

        public void Close(MediaEffectClosedReason reason)
        {
            // Clean up devices
            if (_canvasDevice != null)
                _canvasDevice.Dispose();
        }

        public void DiscardQueuedFrames()
        {
            // No cached frames to discard
        }
    }
}

如何使此效果正常运行?谢谢你的帮助。

0 个答案:

没有答案