wpfdirectshowwindows-media-encoder

How do I preview a Windows Media Encoder session in WPF?


This code works in a windows forms application (it shows the preview) but not in a WPF application.

WMEncoder _encoder;
WMEncDataView _preview;
_encoder = new WMEncoder();

IWMEncSourceGroupCollection SrcGrpColl = _encoder.SourceGroupCollection;
IWMEncSourceGroup2 sourceGroup = (IWMEncSourceGroup2)SrcGrpColl.Add("SG_1");
IWMEncVideoSource2 videoDevice = (IWMEncVideoSource2)sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
videoDevice.SetInput("Default_Video_Device", "Device", "");
IWMEncAudioSource audioDevice = (IWMEncAudioSource)sourceGroup.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
audioDevice.SetInput("Default_Audio_Device", "Device", "");

IWMEncProfile2 profile = new WMEncProfile2();
profile.LoadFromFile("Recording.prx");
sourceGroup.set_Profile(profile);

_encoder.PrepareToEncode(true);

_preview = new WMEncDataView();
int lpreviewStream = videoDevice.PreviewCollection.Add(_preview);

_encoder.Start();

_preview.SetViewProperties(lpreviewStream, (int)windowsFormsHost1.Handle);
_preview.StartView(lpreviewStream);

I've tried to use the WindowsFormsHost control to get a handle to pass (as shown in the sample), but still no luck.


Solution

  • I've recently done something similar to integrate an existing DirectShow video component with a new WPF UI. There are a variety of ways to do it, but probably the easiest is to derive a new class from HwndHost. You then simply override a couple of methods, give the window handle to your preview stream and it should all just work. depending on your requirements you may need to handle a couple of Windows messages in your WndProc to handle display changes and redrawing when the video's not playing.

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;
    
    namespace SOQuestion
    {
        public class VideoHost : HwndHost
        {
            protected override HandleRef BuildWindowCore(HandleRef hwndParent)
            {
                IntPtr hwndHost = IntPtr.Zero;
                int hostHeight = (int) this.ActualHeight;
                int hostWidth = (int) this.ActualWidth;
    
                hwndHost = CreateWindowEx(0, "static", "",
                    WS_CHILD | WS_VISIBLE,
                    0, 0,
                    hostHeight, hostWidth,
                    hwndParent.Handle,
                    (IntPtr)HOST_ID,
                    IntPtr.Zero,
                    0);
    
                return new HandleRef(this, hwndHost);
            }
    
            protected override void DestroyWindowCore(HandleRef hwnd)
            {
                DestroyWindow(hwnd.Handle);
            }
    
            protected override void OnWindowPositionChanged(Rect rcBoundingBox)
            {
                base.OnWindowPositionChanged(rcBoundingBox);
    
                _preview.SetViewProperties(lpreviewStream, (int)this.Handle);
            }
    
            protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                // Handle a couple of windows messages if required - see MSDN for details
    
                return base.WndProc(hwnd, msg, wParam, lParam, ref handled);
            }
    
            [DllImport("user32.dll", EntryPoint = "CreateWindowEx", CharSet = CharSet.Auto)]
            internal static extern IntPtr CreateWindowEx(int dwExStyle,
            string lpszClassName,
            string lpszWindowName,
            int style,
            int x, int y,
            int width, int height,
            IntPtr hwndParent,
            IntPtr hMenu,
            IntPtr hInst,
            [MarshalAs(UnmanagedType.AsAny)] object pvParam);
    
            [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Auto)]
            internal static extern bool DestroyWindow(IntPtr hwnd); 
    
            internal const int WS_CHILD = 0x40000000;
            internal const int WS_VISIBLE = 0x10000000;
            internal const int HOST_ID = 0x00000002;
        }
    }