#include "stdio.h"
#include "..\..\CommonWrapper\C++\FilterGraphWrapper.h"
#include "MILEncodeWrapper.h"
#include "MILDecodeWrapper.h"
#include "..\..\CommonWrapper\C++\DirectShowHelper.h"
HRESULT EncodeFromGrab();
HRESULT DecodeFromFile();
int main(void)
{
HRESULT hr = S_OK;
MosPrintf(MIL_TEXT("Matrox Encode and Decode DirectShow filter example:\n"));
MosPrintf(MIL_TEXT("---------------------------------------------------\n"));
MosPrintf(MIL_TEXT("\n"));
MosPrintf(MIL_TEXT("Matrox Encode DirectShow filter section.\n"));
MosPrintf(MIL_TEXT("----------------------------------------\n"));
MosPrintf(MIL_TEXT("This section will try to add a video capture filter\n"));
MosPrintf(MIL_TEXT("and an audio capture filter to the graph.\n"));
MosPrintf(MIL_TEXT("It will encode the video data using \"Matrox Encode\" filter.\n"));
MosPrintf(MIL_TEXT("The compressed stream will be muxed with the audio input into an avi file.\n"));
MosPrintf(MIL_TEXT("Both sources will also be rendered using default renderer.\n"));
MosPrintf(MIL_TEXT("\n"));
hr = EncodeFromGrab();
if (hr == S_FALSE)
goto end;
MosPrintf(MIL_TEXT("\n"));
MosPrintf(MIL_TEXT("Matrox Decode DirectShow filter section.\n"));
MosPrintf(MIL_TEXT("----------------------------------------\n"));
MosPrintf(MIL_TEXT("This section will read the file encoded previously\n"));
MosPrintf(MIL_TEXT("and decode the video stream using \"Matrox Decode\" filter.\n"));
MosPrintf(MIL_TEXT("All stream from the avi file will use default renderer.\n"));
MosPrintf(MIL_TEXT("\n"));
hr = DecodeFromFile();
if (hr == S_FALSE)
goto end;
MosPrintf(MIL_TEXT("\nPress <Enter> to end."));
MosGetch();
end:
MosPrintf(MIL_TEXT("\n"));
return 0;
}
HRESULT EncodeFromGrab()
{
HRESULT hr = S_OK;
FilterGraphWrapper WrapperFilterGraph;
MilCompressionWrapper WrapperCompressionFilter;
WrapperFilterGraph.InitGraph();
BOOL bVideoCaptureFilterSelected = FALSE;
BOOL bAudioCaptureFilterSelected = FALSE;
if (WrapperFilterGraph.IsInitialized())
{
HRESULT hr = S_OK;
CComPtr<IBaseFilter> selectedVideoCaptureFilter = NULL;
CComPtr<IPin> selectedVideoCapturePin = NULL;
CComPtr<IBaseFilter> selectedAudioCaptureFilter = NULL;
CComPtr<IPin> selectedAudioCapturePin = NULL;
CComPtr<IPin> CompressionOutputPin = NULL;
CComPtr<IBaseFilter> AudioInfinitePinTeeFilter = NULL;
CComPtr<IBaseFilter> AviMuxFilter = NULL;
CComPtr<IPin> AviMuxOutputPin = NULL;
CComPtr<IBaseFilter> FileWriterFilter = NULL;
if (FilterFormCategoryCount(CLSID_VideoInputDeviceCategory) == 0)
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("No video capture filters are available.\n"));
MosPrintf(MIL_TEXT("Make sure the desired filter is properly installed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
LPCWSTR selectedCaptureFilterName = new WCHAR[1024];
MosPrintf(MIL_TEXT("Select a video capture source from the list:\n"));
bVideoCaptureFilterSelected = SelectFilterFromCategory(CLSID_VideoInputDeviceCategory, &selectedVideoCaptureFilter, selectedCaptureFilterName);
if (bVideoCaptureFilterSelected && !selectedVideoCaptureFilter)
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("The selected filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the desired filter is properly installed and configured.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
selectedVideoCaptureFilter = NULL;
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
if (bVideoCaptureFilterSelected && selectedVideoCaptureFilter)
{
WrapperFilterGraph.InsertFilter(selectedVideoCaptureFilter, selectedCaptureFilterName, &selectedVideoCapturePin);
}
if (selectedCaptureFilterName)
{
delete [] selectedCaptureFilterName;
selectedCaptureFilterName = NULL;
}
if (!bVideoCaptureFilterSelected)
{
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n"));
MosGetch();
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
if (FilterFormCategoryCount(CLSID_AudioInputDeviceCategory))
{
LPCWSTR selectedAudioCaptureFilterName = new WCHAR[1024];
MosPrintf(MIL_TEXT("Select an audio capture source from the list:\n"));
bAudioCaptureFilterSelected = SelectFilterFromCategory(CLSID_AudioInputDeviceCategory, &selectedAudioCaptureFilter, selectedAudioCaptureFilterName);
if (bAudioCaptureFilterSelected && !selectedAudioCaptureFilter)
{
MosPrintf(MIL_TEXT("\n"));
MosPrintf(MIL_TEXT("The selected audio filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the desired filter is properly installed and configured.\n"));
MosPrintf(MIL_TEXT("Operation will continue without audio.\n"));
}
if (bAudioCaptureFilterSelected && selectedAudioCaptureFilter)
{
WrapperFilterGraph.InsertFilter(selectedAudioCaptureFilter, selectedAudioCaptureFilterName, &selectedAudioCapturePin);
}
if (selectedAudioCaptureFilterName)
{
delete [] selectedAudioCaptureFilterName;
selectedAudioCaptureFilterName = NULL;
}
}
hr = WrapperCompressionFilter.AllocateFilter();
if (SUCCEEDED(hr))
{
hr = WrapperFilterGraph.InsertFilter(WrapperCompressionFilter.GetFilterInterface(), L"Matrox Encode", &CompressionOutputPin);
}
else
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("\"Matrox Encode\" filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the filter is properly registered in \n"));
MosPrintf(MIL_TEXT("MILConfig under \"Benchmarks and Utilities/DirectShow\"\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
selectedVideoCaptureFilter = NULL;
selectedVideoCapturePin = NULL;
selectedAudioCaptureFilter = NULL;
selectedAudioCapturePin = NULL;
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
if (selectedAudioCaptureFilter)
{
hr = CoCreateInstance(CLSID_InfTee,
NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter,
(void**)&AudioInfinitePinTeeFilter);
if (SUCCEEDED(hr))
{
WrapperFilterGraph.InsertFilter(AudioInfinitePinTeeFilter, L"Audio Infinite Pin Tee", NULL);
}
}
WrapperFilterGraph.Connect(selectedVideoCapturePin, WrapperCompressionFilter.GetFilterInterface());
WrapperFilterGraph.Connect(selectedAudioCapturePin, AudioInfinitePinTeeFilter);
WrapperFilterGraph.Render(selectedVideoCaptureFilter);
WrapperFilterGraph.Render(AudioInfinitePinTeeFilter);
hr = CoCreateInstance(CLSID_AviDest,
NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter,
(void**)&AviMuxFilter);
if (SUCCEEDED(hr))
{
WrapperFilterGraph.InsertFilter(AviMuxFilter, L"Avi Mux", &AviMuxOutputPin);
}
hr = CoCreateInstance(CLSID_FileWriter,
NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter,
(void**)&FileWriterFilter);
if (SUCCEEDED(hr))
{
hr = WrapperFilterGraph.InsertFilter(FileWriterFilter, L"MilDSFEncode.avi", NULL);
LPTSTR sTempPath = new TCHAR[MAX_PATH + 1];
LPTSTR sOutFile = new TCHAR[MAX_PATH + 1];
GetTempPath(MAX_PATH + 1, sTempPath);
MosSprintf(sOutFile, MAX_PATH + 1, MIL_TEXT("%s%s"), sTempPath, MIL_TEXT("MilDSFEncode.avi"));
if (sTempPath)
{
delete [] sTempPath;
sTempPath = NULL;
}
CComPtr<IFileSinkFilter> FileSinkFilter = NULL;
FileWriterFilter->QueryInterface(IID_IFileSinkFilter, (void**)&FileSinkFilter);
FileSinkFilter->SetFileName(sOutFile, NULL);
if (sOutFile)
{
delete [] sOutFile;
sOutFile = NULL;
}
LPTSTR sFileName = NULL;
FileSinkFilter->GetCurFile(&sFileName, NULL);
if (sFileName)
{
MosPrintf(MIL_TEXT("Output file path: %s\n\n"), sFileName);
CoTaskMemFree(sFileName);
}
FileSinkFilter = NULL;
}
WrapperFilterGraph.Connect(CompressionOutputPin, AviMuxFilter);
WrapperFilterGraph.Connect(AudioInfinitePinTeeFilter, AviMuxFilter);
WrapperFilterGraph.Connect(AviMuxOutputPin, FileWriterFilter);
selectedVideoCaptureFilter = NULL;
selectedVideoCapturePin = NULL;
selectedAudioCaptureFilter = NULL;
selectedAudioCapturePin = NULL;
AudioInfinitePinTeeFilter = NULL;
AviMuxFilter = NULL;
AviMuxOutputPin = NULL;
FileWriterFilter = NULL;
CompressionOutputPin = NULL;
}
if (WrapperFilterGraph.IsInitialized() && bVideoCaptureFilterSelected)
{
hr = WrapperFilterGraph.StartGraph();
if (hr == S_OK)
{
MosPrintf(MIL_TEXT("Encoding graph is running and saving to file.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to stop graph.\n"));
MosGetch();
}
else
{
MosPrintf(MIL_TEXT("Graph failed to start properly\n"));
}
WrapperFilterGraph.StopGraph();
}
WrapperCompressionFilter.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
return hr;
}
HRESULT DecodeFromFile()
{
HRESULT hr = S_OK;
FilterGraphWrapper WrapperFilterGraph;
MilDecompressionWrapper WrapperDecompressionFilter;
WrapperFilterGraph.InitGraph();
if (WrapperFilterGraph.IsInitialized())
{
CComPtr<IBaseFilter> sourceFileFilter = NULL;
hr = CoCreateInstance(CLSID_AsyncReader,
NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter,
(void**)&sourceFileFilter);
if (SUCCEEDED(hr))
{
WrapperFilterGraph.InsertFilter(sourceFileFilter, L"MilDSFEncode.avi", NULL);
LPTSTR sTempPath = new TCHAR[MAX_PATH + 1];
LPTSTR sInFile = new TCHAR[MAX_PATH + 1];
GetTempPath(MAX_PATH + 1, sTempPath);
MosSprintf(sInFile, MAX_PATH + 1, MIL_TEXT("%s%s"), sTempPath, MIL_TEXT("MilDSFEncode.avi"));
if (sTempPath)
{
delete [] sTempPath;
sTempPath = NULL;
}
CComPtr<IFileSourceFilter> FileSourceFilter = NULL;
sourceFileFilter->QueryInterface(IID_IFileSourceFilter, (void**)&FileSourceFilter);
hr = FileSourceFilter->Load(sInFile, NULL);
if (sInFile)
{
delete [] sInFile;
sInFile = NULL;
}
LPTSTR sFileName = NULL;
FileSourceFilter->GetCurFile(&sFileName, NULL);
if (sFileName)
{
MosPrintf(MIL_TEXT("Input file path: %s\n\n"), sFileName);
CoTaskMemFree(sFileName);
}
FileSourceFilter = NULL;
}
if (hr != S_OK)
{
sourceFileFilter = NULL;
MosPrintf(MIL_TEXT("\nFile could not be opened.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n"));
MosGetch();
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
hr = WrapperDecompressionFilter.AllocateFilter();
if (SUCCEEDED(hr))
{
hr = WrapperFilterGraph.InsertFilter(WrapperDecompressionFilter.GetFilterInterface(), L"Matrox Decode", NULL);
}
else
{
MosPrintf(MIL_TEXT("\nERROR\n\n"));
MosPrintf(MIL_TEXT("\"Matrox Decode\" filter creation failed.\n"));
MosPrintf(MIL_TEXT("Make sure the filter is properly registered in \n"));
MosPrintf(MIL_TEXT("MILConfig under \"Benchmarks and Utilities/DirectShow\"\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
sourceFileFilter = NULL;
WrapperFilterGraph.UninitGraph();
return S_FALSE;
}
WrapperFilterGraph.Render(sourceFileFilter);
sourceFileFilter = NULL;
}
if (WrapperFilterGraph.IsInitialized())
{
WrapperFilterGraph.StartGraph();
MosPrintf(MIL_TEXT("Decoding graph is running.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to stop graph.\n"));
MosGetch();
WrapperFilterGraph.StopGraph();
}
WrapperDecompressionFilter.ReleaseFilter();
WrapperFilterGraph.UninitGraph();
return hr;
}