Click here to Skip to main content
15,885,027 members
Articles / Multimedia / Video

VMR9 Allocator Presenter in C# with Direct3D Video Rendering

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
10 Jul 2012CPOL5 min read 56.9K   3.3K   16  
Articles describes how to make pure C# rendering video on VMR9 with custom allocator presenter over Direct3D in .NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SlimDX.Direct3D9;
using VMR9;
using Sonic;
using SlimDX;
using System.Threading;

namespace VMR9Playback
{
    public partial class MainForm : Form
    {
        #region Variables

        private object m_csSceneLock = new object();

        private Scene m_Scene = null;

        private DSFilePlayback m_Playback = null;

        #endregion

        #region Constructor

        public MainForm()
        {
            InitializeComponent();
            lock (m_csSceneLock)
            {
                m_Scene = new Scene(this.pbView);
            }
        }

        #endregion

        #region Form Handlers

        private void MainForm_Load(object sender, EventArgs e)
        {
#if DEBUG
            tbFileName.Text = @"d:\test\media\1.avi";
#endif
            btnStart.Enabled = tbFileName.Text != "" && File.Exists(tbFileName.Text);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_Playback != null)
            {
                m_Playback.Dispose();
                m_Playback = null;
            }
            if (m_Scene != null)
            {
                m_Scene.Dispose();
                m_Scene = null;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog _dlg = new OpenFileDialog();
            _dlg.Title = "Select File for playback...";
            if (tbFileName.Text != "")
            {
                _dlg.FileName = Path.GetFileName(tbFileName.Text);
                _dlg.InitialDirectory = Path.GetDirectoryName(tbFileName.Text);
            }
            _dlg.CheckPathExists = true;
            _dlg.DefaultExt = "*.avi";
            _dlg.Multiselect = false;
            _dlg.Filter = "Video Files |*.avi;*.mpg;*.mpeg;*.mov;*.wmv;*.asf;*.mp4;*.vob| All Files (*.*)|*.*";
            _dlg.FilterIndex = 0;
            if (_dlg.ShowDialog(this) == DialogResult.OK)
            {
                tbFileName.Text = _dlg.FileName;
                btnStart.Enabled = tbFileName.Text != "" && File.Exists(tbFileName.Text);
            }
            _dlg.Dispose();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (m_Playback == null)
            {
                m_Playback = new DSFilePlaybackVMR9(m_Scene.Direct3DDevice);
                m_Playback.OnPlaybackStop += new EventHandler(btnStart_Click);
                ((DSFilePlaybackVMR9)m_Playback).OnSurfaceReady += new VMR9.SurfaceReadyHandler(m_Scene.OnSurfaceReady);
                m_Playback.FileName = this.tbFileName.Text;
                if (m_Playback.Start().Succeeded)
                {
                    btnStart.Text = "Stop";
                    btnBrowse.Enabled = false;
                }
                else
                {
                    btnStart_Click(sender, e);
                }
            }
            else
            {
                m_Playback.Dispose();
                m_Playback = null;
                btnStart.Text = "Start";
                btnBrowse.Enabled = true;
                this.pbView.Invalidate();
            }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Russian Federation Russian Federation
I'm a professional multimedia developer (more than 10 years) in any kind of applications and technologies related to it, such as DirectShow, Direct3D, WinMM, OpenGL, MediaFoundation, WASAPI, Windows Media and other including drivers development of Kernel Streaming, Audio/Video capture drivers and audio effects. Have experience in following languages: C, C++, C#, delphi, C++ builder, VB and VB.NET. Strong knowledge in math and networking.

Comments and Discussions