Esempio n. 1
0
// Most code from http://www.geekpage.jp/en/programming/directshow/vmr9.php
void	Video::Init( int _DeviceIndex )
{
	HRESULT	hr;

	// Create FilterGraph
	ASSERT( SUCCEEDED( hr = CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (LPVOID*) &m_pGraphBuilder) ), "Failed to create FilterGraph instance!" );

	// Retrieve media source
	m_pSourceDevice = QueryMediaSourceDevice( _DeviceIndex );
	ASSERT( SUCCEEDED( hr = m_pGraphBuilder->AddFilter( m_pSourceDevice, L"WebCam" ) ), "Failed to add webcam device as source filter!" );

	// Prepare VMR9 filter
	ASSERT( SUCCEEDED( hr = CoCreateInstance( CLSID_VideoMixingRenderer9, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (LPVOID*) &m_pVMR9 ) ), "Failed to create VMR9 instance!" );
	ConfigureVMR9();

	// Add VMR9 filter to Graph
	ASSERT( SUCCEEDED( hr = m_pGraphBuilder->AddFilter( m_pVMR9, L"VMR9" ) ), "Failed to add VMR9 as rendering filter!" );

	// Create GraphBuilder, CaptureGraphBuilder2
	ASSERT( SUCCEEDED( hr = CoCreateInstance( CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (LPVOID*) &m_pCaptureGraphBuilder2 ) ), "Failed to create the CaptureGraphBuilder instance!" );

	// Set FilterGraph for CaptureGraphBuilder2
	ASSERT( SUCCEEDED( hr = m_pCaptureGraphBuilder2->SetFiltergraph( m_pGraphBuilder ) ), "Failed to set filter graph!" );

	// Build Graph
	ASSERT( SUCCEEDED( hr = m_pCaptureGraphBuilder2->RenderStream( 0, 0, m_pSourceDevice, 0, m_pVMR9 ) ), "Failed to setup render stream!" );

	// Get MediaControl Interface
	ASSERT( SUCCEEDED( hr = m_pGraphBuilder->QueryInterface( IID_IMediaControl, (LPVOID*) &m_pMediaControl ) ), "Failed to retrieve MediaControl instance!" );
}
HRESULT StartGraph(HWND window)
{
    HRESULT hr = S_OK;

    const DWORD BUFFER_SIZE = MAX_PATH * MAX_VIDEO_STREAMS;

    MultiSelectFileList<BUFFER_SIZE> selectList;

    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize         = sizeof(OPENFILENAME);
    ofn.hwndOwner           = g_hWnd;
    ofn.lpstrFilter         = TEXT("Video Files (.AVI, .MPG, .MPEG, .VOB, .WMV)\0*.AVI;*.MPG;*.MPEG;*.VOB;*.WMV\0") 
                              TEXT("All Files (*.*)\0*.*\0\0");
    ofn.nFilterIndex        = 1;
    ofn.lpstrFile           = selectList.BufferPtr();
    ofn.nMaxFile            = selectList.BufferSizeCch();
    ofn.lpstrTitle          = TEXT("Select a video file to play...");
    ofn.Flags               = OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_EXPLORER;
    ofn.lpstrDefExt         = TEXT("AVI");
    
    // Launch the Open File dialog.
	DWORD result = GetOpenFileName(&ofn);

    // Check for errors.
    if (CommDlgExtendedError() != 0)
    {

        // NOTE: For mult-selection, CommDlgExtendedError can return FNERR_BUFFERTOOSMALL even when
        // GetOpenFileName returns TRUE.

        MessageBox(NULL, TEXT( "Could not open files." ), NULL, MB_OK | MB_ICONERROR);
        return E_FAIL;
    }
    else if (!result)
    {
        // The user cancelled. (No error occurred.)
        return S_OK;
    }

    FAIL_RET(hr = selectList.ParseBuffer());

    // Clear all DirectShow interfaces (COM smart pointers)
    g_compositor            = NULL;
    g_windowlessControl     = NULL;
    g_mediaControl          = NULL;
    g_filter                = NULL;
    g_graph                 = NULL;

    // Create the Filter Graph Manager.
    FAIL_RET( CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&g_graph) );

    // Configure the VMR-9.
    FAIL_RET( ConfigureVMR9(window) );

    // Render every file that the user selected.
    for (DWORD i = 0; i < MAX_VIDEO_STREAMS; i++)
    {
        TCHAR *pFileName = NULL;

        FAIL_RET(hr = selectList.Next(&pFileName));
        if (hr == S_FALSE)
        {
            hr = S_OK;
            break;
        }

		hr = g_graph->RenderFile( pFileName, NULL );

        CoTaskMemFree(pFileName);

        FAIL_RET(hr);

	}

    // Run the graph.

    FAIL_RET( g_graph->QueryInterface(IID_IMediaControl, reinterpret_cast<void**>(&g_mediaControl)) );

    FAIL_RET( g_mediaControl->Run() );

    return hr;
}