Click here to Skip to main content
12,073,837 members (67,678 online)
Click here to Skip to main content
Articles » Multimedia » DirectX » General » Downloads

Stats

348.9K views
9K downloads
93 bookmarked
Posted

Using the DirectShow Video Mixing Renderer 9 filter

, 1 Feb 2005
This article describes how to dynamically mix two video files (.mpeg, .mpg, .avi and .dat). Mixing involves alpha-blending and stretching/shrinking and positioning of the two video streams, individually, using DirectShow's VMR9 filter.
directshowvmr9_demo.zip
DirectShowVMR9.exe
directshowvmr9_src.zip
app.ico
#pragma once

#include <dshow.h>
#include <d3d9.h>
#include <vmr9.h>

#undef MessageBox
#define SAFE_RELEASE(i) {if (i) i->Release(); i = NULL;}

namespace DirectShowVMR9
{
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary> 
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::PictureBox *  pictureBoxVMR9;
	private: System::Windows::Forms::TrackBar *  trackBarX1;
	private: System::Windows::Forms::Label *  label1;
	private: System::Windows::Forms::TrackBar *  trackBarY1;
	private: System::Windows::Forms::Label *  label2;
	private: System::Windows::Forms::TrackBar *  trackBarW1;
	private: System::Windows::Forms::Label *  label3;
	private: System::Windows::Forms::TrackBar *  trackBarH1;
	private: System::Windows::Forms::Label *  label4;
	private: System::Windows::Forms::TrackBar *  trackBarA1;
	private: System::Windows::Forms::Label *  label5;
	private: System::Windows::Forms::GroupBox *  groupBox1;
	private: System::Windows::Forms::GroupBox *  groupBox2;
	private: System::Windows::Forms::TrackBar *  trackBarA2;
	private: System::Windows::Forms::Label *  label6;
	private: System::Windows::Forms::TrackBar *  trackBarH2;
	private: System::Windows::Forms::Label *  label7;
	private: System::Windows::Forms::TrackBar *  trackBarW2;
	private: System::Windows::Forms::Label *  label8;
	private: System::Windows::Forms::TrackBar *  trackBarY2;
	private: System::Windows::Forms::Label *  label9;
	private: System::Windows::Forms::TrackBar *  trackBarX2;
	private: System::Windows::Forms::Label *  label10;
	private: System::Windows::Forms::GroupBox *  groupBox3;
	private: System::Windows::Forms::Button *  buttonOpen;
	private: System::Windows::Forms::Button *  buttonPlay;
	private: System::Windows::Forms::Button *  buttonPause;
	private: System::Windows::Forms::Button *  buttonStop;
	private: System::Windows::Forms::Button *  buttonClose;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->pictureBoxVMR9 = new System::Windows::Forms::PictureBox();
			this->trackBarX1 = new System::Windows::Forms::TrackBar();
			this->label1 = new System::Windows::Forms::Label();
			this->trackBarY1 = new System::Windows::Forms::TrackBar();
			this->label2 = new System::Windows::Forms::Label();
			this->trackBarW1 = new System::Windows::Forms::TrackBar();
			this->label3 = new System::Windows::Forms::Label();
			this->trackBarH1 = new System::Windows::Forms::TrackBar();
			this->label4 = new System::Windows::Forms::Label();
			this->trackBarA1 = new System::Windows::Forms::TrackBar();
			this->label5 = new System::Windows::Forms::Label();
			this->groupBox1 = new System::Windows::Forms::GroupBox();
			this->groupBox2 = new System::Windows::Forms::GroupBox();
			this->buttonStop = new System::Windows::Forms::Button();
			this->buttonClose = new System::Windows::Forms::Button();
			this->buttonPlay = new System::Windows::Forms::Button();
			this->buttonOpen = new System::Windows::Forms::Button();
			this->buttonPause = new System::Windows::Forms::Button();
			this->trackBarA2 = new System::Windows::Forms::TrackBar();
			this->label6 = new System::Windows::Forms::Label();
			this->trackBarH2 = new System::Windows::Forms::TrackBar();
			this->label7 = new System::Windows::Forms::Label();
			this->trackBarW2 = new System::Windows::Forms::TrackBar();
			this->label8 = new System::Windows::Forms::Label();
			this->trackBarY2 = new System::Windows::Forms::TrackBar();
			this->label9 = new System::Windows::Forms::Label();
			this->trackBarX2 = new System::Windows::Forms::TrackBar();
			this->label10 = new System::Windows::Forms::Label();
			this->groupBox3 = new System::Windows::Forms::GroupBox();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarX1))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarY1))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarW1))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarH1))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarA1))->BeginInit();
			this->groupBox2->SuspendLayout();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarA2))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarH2))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarW2))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarY2))->BeginInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarX2))->BeginInit();
			this->SuspendLayout();
			// 
			// pictureBoxVMR9
			// 
			this->pictureBoxVMR9->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->pictureBoxVMR9->Location = System::Drawing::Point(168, 32);
			this->pictureBoxVMR9->Name = S"pictureBoxVMR9";
			this->pictureBoxVMR9->Size = System::Drawing::Size(352, 288);
			this->pictureBoxVMR9->TabIndex = 0;
			this->pictureBoxVMR9->TabStop = false;
			// 
			// trackBarX1
			// 
			this->trackBarX1->Enabled = false;
			this->trackBarX1->Location = System::Drawing::Point(32, 48);
			this->trackBarX1->Maximum = 100;
			this->trackBarX1->Name = S"trackBarX1";
			this->trackBarX1->TabIndex = 1;
			this->trackBarX1->TickFrequency = 10;
			this->trackBarX1->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarX1->Scroll += new System::EventHandler(this, trackBarX1_Scroll);
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(40, 32);
			this->label1->Name = S"label1";
			this->label1->Size = System::Drawing::Size(72, 23);
			this->label1->TabIndex = 2;
			this->label1->Text = S"X Position";
			// 
			// trackBarY1
			// 
			this->trackBarY1->Enabled = false;
			this->trackBarY1->Location = System::Drawing::Point(32, 112);
			this->trackBarY1->Maximum = 100;
			this->trackBarY1->Name = S"trackBarY1";
			this->trackBarY1->TabIndex = 3;
			this->trackBarY1->TickFrequency = 10;
			this->trackBarY1->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarY1->Scroll += new System::EventHandler(this, trackBarY1_Scroll);
			// 
			// label2
			// 
			this->label2->Location = System::Drawing::Point(40, 96);
			this->label2->Name = S"label2";
			this->label2->Size = System::Drawing::Size(72, 23);
			this->label2->TabIndex = 4;
			this->label2->Text = S"Y Position";
			// 
			// trackBarW1
			// 
			this->trackBarW1->Enabled = false;
			this->trackBarW1->Location = System::Drawing::Point(32, 176);
			this->trackBarW1->Maximum = 100;
			this->trackBarW1->Name = S"trackBarW1";
			this->trackBarW1->TabIndex = 5;
			this->trackBarW1->TickFrequency = 10;
			this->trackBarW1->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarW1->Value = 100;
			this->trackBarW1->Scroll += new System::EventHandler(this, trackBarW1_Scroll);
			// 
			// label3
			// 
			this->label3->Location = System::Drawing::Point(40, 160);
			this->label3->Name = S"label3";
			this->label3->Size = System::Drawing::Size(72, 23);
			this->label3->TabIndex = 6;
			this->label3->Text = S"Width";
			// 
			// trackBarH1
			// 
			this->trackBarH1->Enabled = false;
			this->trackBarH1->Location = System::Drawing::Point(32, 240);
			this->trackBarH1->Maximum = 100;
			this->trackBarH1->Name = S"trackBarH1";
			this->trackBarH1->TabIndex = 7;
			this->trackBarH1->TickFrequency = 10;
			this->trackBarH1->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarH1->Value = 100;
			this->trackBarH1->Scroll += new System::EventHandler(this, trackBarH1_Scroll);
			// 
			// label4
			// 
			this->label4->Location = System::Drawing::Point(40, 224);
			this->label4->Name = S"label4";
			this->label4->Size = System::Drawing::Size(72, 23);
			this->label4->TabIndex = 8;
			this->label4->Text = S"Height";
			// 
			// trackBarA1
			// 
			this->trackBarA1->Enabled = false;
			this->trackBarA1->Location = System::Drawing::Point(32, 304);
			this->trackBarA1->Maximum = 100;
			this->trackBarA1->Name = S"trackBarA1";
			this->trackBarA1->TabIndex = 9;
			this->trackBarA1->TickFrequency = 10;
			this->trackBarA1->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarA1->Value = 100;
			this->trackBarA1->Scroll += new System::EventHandler(this, trackBarA1_Scroll);
			// 
			// label5
			// 
			this->label5->Location = System::Drawing::Point(40, 288);
			this->label5->Name = S"label5";
			this->label5->Size = System::Drawing::Size(72, 23);
			this->label5->TabIndex = 10;
			this->label5->Text = S"Alpha";
			// 
			// groupBox1
			// 
			this->groupBox1->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->groupBox1->Location = System::Drawing::Point(16, 8);
			this->groupBox1->Name = S"groupBox1";
			this->groupBox1->Size = System::Drawing::Size(128, 352);
			this->groupBox1->TabIndex = 11;
			this->groupBox1->TabStop = false;
			this->groupBox1->Text = S"Video Stream 1";
			// 
			// groupBox2
			// 
			this->groupBox2->Controls->Add(this->buttonStop);
			this->groupBox2->Controls->Add(this->buttonClose);
			this->groupBox2->Controls->Add(this->buttonPlay);
			this->groupBox2->Controls->Add(this->buttonOpen);
			this->groupBox2->Controls->Add(this->buttonPause);
			this->groupBox2->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->groupBox2->Location = System::Drawing::Point(152, 8);
			this->groupBox2->Name = S"groupBox2";
			this->groupBox2->Size = System::Drawing::Size(384, 352);
			this->groupBox2->TabIndex = 12;
			this->groupBox2->TabStop = false;
			this->groupBox2->Text = S"Video Mixing Renderer 9";
			// 
			// buttonStop
			// 
			this->buttonStop->Enabled = false;
			this->buttonStop->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->buttonStop->Location = System::Drawing::Point(216, 320);
			this->buttonStop->Name = S"buttonStop";
			this->buttonStop->Size = System::Drawing::Size(48, 23);
			this->buttonStop->TabIndex = 25;
			this->buttonStop->Text = S"Stop";
			this->buttonStop->Click += new System::EventHandler(this, buttonStop_Click);
			// 
			// buttonClose
			// 
			this->buttonClose->Enabled = false;
			this->buttonClose->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->buttonClose->Location = System::Drawing::Point(280, 320);
			this->buttonClose->Name = S"buttonClose";
			this->buttonClose->Size = System::Drawing::Size(48, 23);
			this->buttonClose->TabIndex = 26;
			this->buttonClose->Text = S"Close";
			this->buttonClose->Click += new System::EventHandler(this, buttonClose_Click);
			// 
			// buttonPlay
			// 
			this->buttonPlay->Enabled = false;
			this->buttonPlay->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->buttonPlay->Location = System::Drawing::Point(152, 320);
			this->buttonPlay->Name = S"buttonPlay";
			this->buttonPlay->Size = System::Drawing::Size(48, 23);
			this->buttonPlay->TabIndex = 1;
			this->buttonPlay->Text = S"Play";
			this->buttonPlay->Click += new System::EventHandler(this, buttonPlay_Click);
			// 
			// buttonOpen
			// 
			this->buttonOpen->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->buttonOpen->Location = System::Drawing::Point(48, 320);
			this->buttonOpen->Name = S"buttonOpen";
			this->buttonOpen->Size = System::Drawing::Size(88, 24);
			this->buttonOpen->TabIndex = 0;
			this->buttonOpen->Text = S"Open Streams";
			this->buttonOpen->Click += new System::EventHandler(this, buttonOpen_Click);
			// 
			// buttonPause
			// 
			this->buttonPause->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->buttonPause->Location = System::Drawing::Point(152, 320);
			this->buttonPause->Name = S"buttonPause";
			this->buttonPause->Size = System::Drawing::Size(48, 23);
			this->buttonPause->TabIndex = 24;
			this->buttonPause->Text = S"Pause";
			this->buttonPause->Click += new System::EventHandler(this, buttonPause_Click);
			// 
			// trackBarA2
			// 
			this->trackBarA2->Enabled = false;
			this->trackBarA2->Location = System::Drawing::Point(560, 304);
			this->trackBarA2->Maximum = 100;
			this->trackBarA2->Name = S"trackBarA2";
			this->trackBarA2->TabIndex = 21;
			this->trackBarA2->TickFrequency = 10;
			this->trackBarA2->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarA2->Value = 50;
			this->trackBarA2->Scroll += new System::EventHandler(this, trackBarA2_Scroll);
			// 
			// label6
			// 
			this->label6->Location = System::Drawing::Point(568, 288);
			this->label6->Name = S"label6";
			this->label6->Size = System::Drawing::Size(72, 23);
			this->label6->TabIndex = 22;
			this->label6->Text = S"Alpha";
			// 
			// trackBarH2
			// 
			this->trackBarH2->Enabled = false;
			this->trackBarH2->Location = System::Drawing::Point(560, 240);
			this->trackBarH2->Maximum = 100;
			this->trackBarH2->Name = S"trackBarH2";
			this->trackBarH2->TabIndex = 19;
			this->trackBarH2->TickFrequency = 10;
			this->trackBarH2->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarH2->Value = 100;
			this->trackBarH2->Scroll += new System::EventHandler(this, trackBarH2_Scroll);
			// 
			// label7
			// 
			this->label7->Location = System::Drawing::Point(568, 224);
			this->label7->Name = S"label7";
			this->label7->Size = System::Drawing::Size(72, 23);
			this->label7->TabIndex = 20;
			this->label7->Text = S"Height";
			// 
			// trackBarW2
			// 
			this->trackBarW2->Enabled = false;
			this->trackBarW2->Location = System::Drawing::Point(560, 176);
			this->trackBarW2->Maximum = 100;
			this->trackBarW2->Name = S"trackBarW2";
			this->trackBarW2->TabIndex = 17;
			this->trackBarW2->TickFrequency = 10;
			this->trackBarW2->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarW2->Value = 100;
			this->trackBarW2->Scroll += new System::EventHandler(this, trackBarW2_Scroll);
			// 
			// label8
			// 
			this->label8->Location = System::Drawing::Point(568, 160);
			this->label8->Name = S"label8";
			this->label8->Size = System::Drawing::Size(72, 23);
			this->label8->TabIndex = 18;
			this->label8->Text = S"Width";
			// 
			// trackBarY2
			// 
			this->trackBarY2->Enabled = false;
			this->trackBarY2->Location = System::Drawing::Point(560, 112);
			this->trackBarY2->Maximum = 100;
			this->trackBarY2->Name = S"trackBarY2";
			this->trackBarY2->TabIndex = 15;
			this->trackBarY2->TickFrequency = 10;
			this->trackBarY2->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarY2->Scroll += new System::EventHandler(this, trackBarY2_Scroll);
			// 
			// label9
			// 
			this->label9->Location = System::Drawing::Point(568, 96);
			this->label9->Name = S"label9";
			this->label9->Size = System::Drawing::Size(72, 23);
			this->label9->TabIndex = 16;
			this->label9->Text = S"Y Position";
			// 
			// trackBarX2
			// 
			this->trackBarX2->Enabled = false;
			this->trackBarX2->Location = System::Drawing::Point(560, 48);
			this->trackBarX2->Maximum = 100;
			this->trackBarX2->Name = S"trackBarX2";
			this->trackBarX2->TabIndex = 13;
			this->trackBarX2->TickFrequency = 10;
			this->trackBarX2->TickStyle = System::Windows::Forms::TickStyle::Both;
			this->trackBarX2->Scroll += new System::EventHandler(this, trackBarX2_Scroll);
			// 
			// label10
			// 
			this->label10->Location = System::Drawing::Point(568, 32);
			this->label10->Name = S"label10";
			this->label10->Size = System::Drawing::Size(72, 23);
			this->label10->TabIndex = 14;
			this->label10->Text = S"X Position";
			// 
			// groupBox3
			// 
			this->groupBox3->Font = new System::Drawing::Font(S"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, (System::Byte)0);
			this->groupBox3->Location = System::Drawing::Point(544, 8);
			this->groupBox3->Name = S"groupBox3";
			this->groupBox3->Size = System::Drawing::Size(128, 352);
			this->groupBox3->TabIndex = 23;
			this->groupBox3->TabStop = false;
			this->groupBox3->Text = S"Video Stream 2";
			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
			this->ClientSize = System::Drawing::Size(686, 375);
			this->Controls->Add(this->trackBarA2);
			this->Controls->Add(this->label6);
			this->Controls->Add(this->trackBarH2);
			this->Controls->Add(this->label7);
			this->Controls->Add(this->trackBarW2);
			this->Controls->Add(this->label8);
			this->Controls->Add(this->trackBarY2);
			this->Controls->Add(this->label9);
			this->Controls->Add(this->trackBarX2);
			this->Controls->Add(this->label10);
			this->Controls->Add(this->groupBox3);
			this->Controls->Add(this->trackBarA1);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->trackBarH1);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->trackBarW1);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->trackBarY1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->trackBarX1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->groupBox1);
			this->Controls->Add(this->pictureBoxVMR9);
			this->Controls->Add(this->groupBox2);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedDialog;
			this->MaximizeBox = false;
			this->Name = S"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = S"DirectShow - Video Mixing Renderer 9";
			this->Load += new System::EventHandler(this, Form1_Load);
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarX1))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarY1))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarW1))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarH1))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarA1))->EndInit();
			this->groupBox2->ResumeLayout(false);
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarA2))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarH2))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarW2))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarY2))->EndInit();
			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->trackBarX2))->EndInit();
			this->ResumeLayout(false);

		}	

		__nogc class myVMR9
		{
            // Encapsulate VMR9 functionality in this class
			VMR9NormalizedRect *r;
			IVMRWindowlessControl9 *pWC;
			IVMRMixerControl9 *pMix;
			IGraphBuilder *pGB;
			IBaseFilter *pVmr;
			IVMRFilterConfig9 *pConfig;
			IMediaControl *pMC;
			IMediaSeeking *pMS;

		public: myVMR9(System::Drawing::Rectangle rect, HWND hwnd)
			{
                /*
				constructor receives PictureBox's coordinates
                along with its handler
			    */

				// initialize video coordinates with normal values
				r = new VMR9NormalizedRect;
				r->left = 0;
				r->top = 0;
				r->right = 1;
				r->bottom = 1;

				pWC = NULL;
				pMix = NULL;
				pGB = NULL;
				pVmr = NULL;
				pConfig = NULL;
				pMC = NULL;
				pMS = NULL;

				// create an instance of the Filter Graph Manager
				CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                IID_IGraphBuilder, (void **)&pGB);
				
				// create an instance of the VMR9 filter
				CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC,
				IID_IBaseFilter, (void**)&pVmr);

				// add the VMR9 filter to the Graph Manager
				pGB->AddFilter(pVmr, L"Video");

				// get a pointer to the IVMRFilterConfig9 interface 
				pVmr->QueryInterface(IID_IVMRFilterConfig9, (void**)&pConfig);
				// make sure VMR9 is in windowless mode
				pConfig->SetRenderingMode(VMR9Mode_Windowless);

				// get a pointer to the IVMRWindowlessControl9 interface 
				pVmr->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pWC);

				// explicitly convert System::Drawing::Rectangle type to RECT type
				RECT rcDest = {0};
				rcDest.bottom =  rect.Bottom;
				rcDest.left = rect.Left;
				rcDest.right = rect.Right;
				rcDest.top = rect.Top;

				// set destination rectangle for the video
				pWC->SetVideoPosition(NULL, &rcDest);
				// specify the container window that the video should be clipped to
				pWC->SetVideoClippingWindow(hwnd);

				// IVMRMixerControl manipulates video streams
				pVmr->QueryInterface(IID_IVMRMixerControl9, (void**)&pMix);
                // IMediaSeeking seeks to a position in the video stream
				pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS);
				// IMediaControl controls flow of data through the graph
				pGB->QueryInterface(IID_IMediaControl, (void **)&pMC);
			}

			HRESULT play()
			{
				pMC->Run();
				return S_OK;
			}

			HRESULT pause()
			{
				pMC->Pause();
				return S_OK;
			}

			HRESULT stop()
			{
				LONGLONG pos = 0;
				pMC->Stop();
				pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
				pMC->Pause();
				return S_OK;
			}

			HRESULT close()
			{
				SAFE_RELEASE(pWC);
				SAFE_RELEASE(pMix);
				SAFE_RELEASE(pGB);
				SAFE_RELEASE(pVmr);
				SAFE_RELEASE(pConfig);
				SAFE_RELEASE(pMC);
				SAFE_RELEASE(pMS);
				return S_OK;
			}

			HRESULT setAlpha(DWORD stream, float alpha)
			{
				pMix->SetAlpha(stream, alpha);
				return S_OK;
			}

			HRESULT setX(DWORD stream, float x)
			{
				// video displacement along x-axis
				r->right = x + (r->right - r->left);
				r->left = x;
				pMix->SetOutputRect(stream, r);
				return S_OK;
			}

			HRESULT setY(DWORD stream, float y)
			{
				// video displacement along y-axis
				r->bottom = y + (r->bottom - r->top);
				r->top = y;
				pMix->SetOutputRect(stream, r);
				return S_OK;
			}

			HRESULT setW(DWORD stream, float w)
			{
				// video stretching/shrinking along x-axis
				r->right = r->left + w;
				pMix->SetOutputRect(stream, r);
				return S_OK;
			}

			HRESULT setH(DWORD stream, float h)
			{
				// video stretching/shrinking along y-axis
				r->bottom = r->top + h;
				pMix->SetOutputRect(stream, r);
				return S_OK;
			}

			HRESULT renderFiles(String* file1, String* file2)
			{
				// convert String type to LPCSTR type and render the videos

				LPCTSTR lFile;
				lFile = static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAuto(file1))));
				pGB->RenderFile((LPCWSTR)lFile, NULL);

				lFile = static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAuto(file2))));
				pGB->RenderFile((LPCWSTR)lFile, NULL);
				
				System::Runtime::InteropServices::Marshal::FreeHGlobal(static_cast<IntPtr>(const_cast<void*>(static_cast<const void*>(lFile))));

				pMC->StopWhenReady();
				return S_OK;
			}
		};

		myVMR9 *myVmr;

	private: void initialize(void)
				{
                    HWND hwnd = (HWND)pictureBoxVMR9->get_Handle().ToPointer();
                    myVmr = new myVMR9(pictureBoxVMR9->get_ClientRectangle(), hwnd);
                    myVmr->setAlpha(0, (float)trackBarA1->Value/100);
                    myVmr->setAlpha(1, (float)trackBarA2->Value/100);
                    trackBarX1->Minimum = -100;
					trackBarY1->Minimum = -100;
					trackBarW1->Minimum = -100;
					trackBarH1->Minimum = -100;
					trackBarX2->Minimum = -100;
					trackBarY2->Minimum = -100;
					trackBarW2->Minimum = -100;
					trackBarH2->Minimum = -100;
					trackBarX1->Value = 0;
					trackBarY1->Value = 0;
					trackBarW1->Value = 100;
					trackBarH1->Value = 100;
					trackBarA1->Value = 100;
					trackBarX2->Value = 0;
					trackBarY2->Value = 0;
					trackBarW2->Value = 100;
					trackBarH2->Value = 100;
					trackBarA2->Value = 50;
				}

	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
			 {
				 initialize();
			 }

	private: System::Void buttonOpen_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 String *file1, *file2;
			     String* filterText = "Video Files (*.mpg; *.mpeg; *.m1v; *.avi)|*.mpg; *.mpeg; *.m1v; *.avi;| All Files (*.*)|*.*";
                 OpenFileDialog *ofd = new OpenFileDialog();
                 ofd->Filter = filterText;
                 ofd->Title = "Open Video File";
				 MessageBox::Show("Select first video");
				 ofd->ShowDialog();
				 file1 = ofd->FileName;
				 if ((file1 == NULL) || (file1 == String::Empty))
                     return;

				 ofd->FileName = NULL;
				 MessageBox::Show("Select second video");

				 ofd->ShowDialog();
				 file2 = ofd->FileName;
				 if ((file2 == NULL) || (file2 == String::Empty))
                     return;

                 myVmr->renderFiles(file1, file2);

                 buttonOpen->Enabled = false;
				 buttonPlay->Enabled = true;
				 buttonStop->Enabled = true;
                 buttonClose->Enabled = true;
				 trackBarA1->Enabled = true;
				 trackBarX1->Enabled = true;
				 trackBarY1->Enabled = true;
				 trackBarW1->Enabled = true;
				 trackBarH1->Enabled = true;
				 trackBarA2->Enabled = true;
				 trackBarX2->Enabled = true;
				 trackBarY2->Enabled = true;
				 trackBarW2->Enabled = true;
				 trackBarH2->Enabled = true;
			 }

private: System::Void buttonPlay_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 myVmr->play();
			 buttonPlay->Hide();
		 }

private: System::Void buttonPause_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 myVmr->pause();
			 buttonPlay->Show();
		 }

private: System::Void buttonStop_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 myVmr->stop();
			 buttonPlay->Show();
		 }

private: System::Void buttonClose_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 myVmr->close();
			 initialize();
			 buttonClose->Enabled = false;
			 buttonPlay->Show();
			 buttonStop->Show();
			 buttonPlay->Enabled = false;
			 buttonStop->Enabled = false;
			 buttonOpen->Enabled = true;
             trackBarA1->Enabled = false;
			 trackBarX1->Enabled = false;
			 trackBarY1->Enabled = false;
			 trackBarW1->Enabled = false;
			 trackBarH1->Enabled = false;
			 trackBarA2->Enabled = false;
			 trackBarX2->Enabled = false;
			 trackBarY2->Enabled = false;
			 trackBarW2->Enabled = false;
			 trackBarH2->Enabled = false;
		 }

private: System::Void trackBarX1_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setX(0, (float)trackBarX1->Value/100);
		 }

private: System::Void trackBarY1_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setY(0, (float)trackBarY1->Value/100);
		 }

private: System::Void trackBarW1_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setW(0, (float)trackBarW1->Value/100);
		 }

private: System::Void trackBarH1_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setH(0, (float)trackBarH1->Value/100);
		 }

private: System::Void trackBarA1_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setAlpha(0, (float)trackBarA1->Value/100);
		 }

private: System::Void trackBarX2_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setX(1, (float)trackBarX2->Value/100);
		 }

private: System::Void trackBarY2_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setY(1, (float)trackBarY2->Value/100);
		 }

private: System::Void trackBarW2_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setW(1, (float)trackBarW2->Value/100);
		 }

private: System::Void trackBarH2_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setH(1, (float)trackBarH2->Value/100);
		 }

private: System::Void trackBarA2_Scroll(System::Object *  sender, System::EventArgs *  e)
		 {
             myVmr->setAlpha(1, (float)trackBarA2->Value/100);
		 }

};
}


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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Sameer Ahmed
Software Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 1 Feb 2005
Article Copyright 2005 by Sameer Ahmed
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid