C#에서 DirectShow를 이용한 미디어 재생
우선, DirectShow용 Managed 래퍼는 마이크로소프트가 제공하지 않습니다. 그래도 괜찮은 것이 ^^ 공개소스로 이미 DirectShow.NET이라는 라이브러리가 제공되고 있기 때문입니다.
DirectShow.NET
; http://directshownet.sourceforge.net/
(2021-01-08 업데이트: 현재 
NuGet에 올려진 DirectShowLib로 편리하게 참조 추가할 수 있습니다.)
Install-Package DirectShowLib
위의 다운로드에 포함된 "DirectShowLib-2005.dll" 어셈블리를 참조한 후 다음과 같이 기본 재생 코드를 만들 수 있습니다.
using DirectShowLib;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IGraphBuilder pGraphBuilder;
        IMediaControl pMediaControl;
        private void Form1_Load(object sender, EventArgs e)
        {
            pGraphBuilder = new FilterGraph() as IGraphBuilder;
            pMediaControl = pGraphBuilder as IMediaControl;
            string filePath = @"C:\temp\108_3.wmv";
            pGraphBuilder.RenderFile(filePath, null);
            pMediaControl.Run();
        }
        protected override void OnClosed(EventArgs e)
        {
            if (pGraphBuilder != null)
            {
                Marshal.ReleaseComObject(pGraphBuilder);
            }
            if (pMediaControl != null)
            {
                Marshal.ReleaseComObject(pMediaControl);
            }
            base.OnClosed(e);
        }
    }
}
그런데 위와 같이 하면 별도의 창이 떠서 동영상이 렌더링됩니다. MainWindow 내에서 렌더링하려면 다음과 같이 IVideoWindow를 이용해 MainWindow 내에 자식 컨트롤로 포함시켜 렌더링할 수 있습니다.
IVideoWindow pVideoWindow;
private void Form1_Load(object sender, EventArgs e)
{
    pGraphBuilder = new FilterGraph() as IGraphBuilder;
    pMediaControl = pGraphBuilder as IMediaControl;
    pVideoWindow = pGraphBuilder as IVideoWindow;
    string filePath = @"C:\temp\108_3.wmv";
    pGraphBuilder.RenderFile(filePath, null);
    pVideoWindow.put_Owner(this.Handle);
    pVideoWindow.put_WindowStyle(WindowStyle.Child | WindowStyle.ClipSiblings);
    pVideoWindow.SetWindowPosition(0, 0, this.Width, this.Height);
    pVideoWindow.put_MessageDrain(this.Handle);
    pVideoWindow.put_Visible(OABool.True);
    if (pMediaControl == null)
    {
        return;
    }
    pMediaControl.Run();
}
그럼 다음과 같이 MainWindow 하나에서 동영상이 재생됩니다. ^^
한 단계 더 나아가 볼까요? ^^ 재생되는 미디어의 필터그래프를 Graph Editor에서 확인할 수 있도록 
ROT(Running Object Table)에도 등록해 보겠습니다.
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);
[DllImport("ole32.dll")]
static extern int CreateItemMoniker([MarshalAs(UnmanagedType.LPWStr)] string
    lpszDelim, [MarshalAs(UnmanagedType.LPWStr)] string lpszItem,
    out IMoniker ppmk);
int _cookie;
private void Form1_Load(object sender, EventArgs e)
{
    pGraphBuilder = new FilterGraph() as IGraphBuilder;
    // ... [생략]...
    AddGraphToROT(pGraphBuilder, out _cookie);
    pMediaControl.Run();
}
private void AddGraphToROT(IGraphBuilder pGraphBuilder, out int cookie)
{
    IMoniker pMoniker;
    IRunningObjectTable pROT;
    cookie = 0;
    if (GetRunningObjectTable(0, out pROT) == 0)
    {
        string filterGraph = string.Format("FilterGraph {0:x} pid {1:x}", pGraphBuilder.GetHashCode(), Process.GetCurrentProcess().Id);
        int hr = CreateItemMoniker("!", filterGraph, out pMoniker);
        if (hr == 0)
        {
            cookie = pROT.Register(0, pGraphBuilder, pMoniker);
            Marshal.ReleaseComObject(pMoniker);
        }
        Marshal.ReleaseComObject(pROT);
    }
}
void RemoveGraphFromROT(int cookie)
{
    IRunningObjectTable pROT;
    if (GetRunningObjectTable(0, out pROT) != 0)
    {
        pROT.Revoke(cookie);
        Marshal.ReleaseComObject(pROT);
    }
}
protected override void OnClosed(EventArgs e)
{
    if (_cookie != 0)
    {
        RemoveGraphFromROT(_cookie);
    }
    // ...[생략]...
    base.OnClosed(e);
}
위와 같이 변경된 프로그램을 실행시키고 Graph Editor(graphedt.exe)를 통해 "File" / "Connect to Remote Graph..." 메뉴를 선택하면 아래와 같이 해당 프로그램에서 재생중인 미디어의 필터 그래프를 확인할 수 있습니다. (만약 목록에 나오지 않는다면 proppage.dll을 등록 후 다시 시도해 보세요.)
(
첨부 파일은 위의 테스트 코드를 포함합니다.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]