Msdn DirectShow learning-Chapter 4 Use DirectShow Section 1 use graphedit to simulate graph Creation

Source: Internet
Author: User
Document directory
  • 4.1.1 graphedit Overview
  • 4.1.2 use graphedit
  • 4.1.3 load a graph from an external process
  • 4.1.4 save a filter graph to the graphedit File
  • 4.1.5 load a programmable graphedit File
Chapter 4 Use DirectShow

(I am not good at English either. The first time I translated msdn, I hope to help beginners and learn faster. If there is anything inappropriate, please point it out and I will modify it as soon as possible .)

4.1 Use graphedit to simulate graph Creation

(Simulating graph building with graphedit)

DirectShow provides a public program called graphedit for debugging. You can use it to create and test the filter graphs.

4.1.1 graphedit Overview

Graphedit is a visualization tool that allows you to create filter graphs. Using graphedit, you can experiment with filter graph before writing program code. You can also load a filter graph created by your program to check whether your program has created the correct graph. If you develop a custom filter, graphedit provides a quick way to test it: simply load a graph with your custom filter and try to run it. If you are a newbie to DirectShow, graphedit is a good way to familiarize yourself with the filter graph and DirectShow architectures.

The received illustration shows how graphedit represents a simple filter graph.

 

Each filter is represented by a rectangle. A small square on the edge of the filter represents the pin. The input pin is on the left of the filter, and the output pin is on the right. Arrows represent links between pins.

Using graphedit, you can:

L you can use a visual drag-and-drop interface to create and modify a filter graph.

L simulate a program call to create a graph, such as igraphbuilder: renderfile.

L run, pause, stop, and search for a graph.

L check which filters have been registered on your computer and check the registration information of each filter.

L observe the page properties of the filter.

L observe the media type of the pin connection.

4.1.2 use graphedit

This chapter provides a brief introduction to what you can do using the graphedit tool. For more details about these features, see the help documentation of the graphedit program.

Create a file to play the Graph

Graphedit can create a filter graph for file playback. This feature works the same way as calling the igraphbuilder: renderfile method in a program. In the File menu, click render media file ]. Graphedit displays the open file dialog box. Select a multimedia file and click open ]. Graphedit creates a filter graph to play the file you selected.

You can also render a media file on the URL. From the File menu, click render URL. graphedit displays a dialog box where you can enter a URL.

Create a custom filter graph

GrapheditYou can create a custom filter graph and use some registered fiters in your system. From the graph menu, click Insert filter ]. A filter List dialog box is displayed based on the filter type. Graphedit creates this list based on your registry information. This dialog box is displayed.

 

Add a filter to graph, select the Filter Name, click Insert filters, or double-click the Filter Name. After you add a filter, you can connect two filters and drag and drop the output pin of one filter to the input pin of another filter. If the pin accepts the connection, graphedit draws an arrow to connect them.

 

Run Graph

Once you have created a filter graph in graphedit, you can run this graph to see if it can work as expected. The graph menu contains a menu for the playback, pause, and stop commands. These commands call the run, pause, and stop methods of imediacontrol respectively. The following buttons are available in the graphedit toolbar:

 

Note: The grhphedit Stop command stops graph and positions the time to 0 (assuming that graph can be located ). For file playback, this action resets the video window to the first frame. Then callImediacontrol: Stop.

If graph is located, you can drag the slider to locate it. The imediaseeking: setpositions method is called by dragging the slider.

View properties page

Some filters support custom property pages, which provide a user interface for setting properties. View a filter attribute page in graphedit, right-click filter, and select Properties in the pop-up window ]. Graphedit displays an Attribute Table that contains the filter definition. In addition, graphedit contains all the attribute tables of pin on the filter. The pin Attribute Table is defined by graphedit rather than filter. If the PIN is connected, the pin Attribute Table displays the connected media type. In addition, it lists the preferred media types of pin.

 

4.1.3 load a graph from an external process

Graphedit can load a filter graph created by an external program. With this feature, you can precisely see the filter graph created by your program, with only a small amount of additional code in your program.

Note: This feature requires Windows 2000, Windows XP, or an updated version.

Note: from Windows Vista, you must register proppage. DLL to use this function.

 

The program must register a filter graph instance in the running object table (ROT. Rot is a globally searchable table to keep running tracking. The object is registered with an alias in the rot. To link graph, graphedit finds the aliases in the rot that match the specific format:

! FiltergraphXPIDY

X is the address of the hex fiter graph manager, and Y is the process ID and hexadecimal.

When your program creates fiter graph for the first time, it calls the following function.

HRESULT AddToRot(IUnknown *pUnkGraph, DWORD *pdwRegister) 
{
    IMoniker * pMoniker = NULL;
    IRunningObjectTable *pROT = NULL;
 
    if (FAILED(GetRunningObjectTable(0, &pROT))) 
    {
        return E_FAIL;
    }
    
    const size_t STRING_LENGTH = 256;
 
    WCHAR wsz[STRING_LENGTH];
    StringCchPrintfW(wsz, STRING_LENGTH, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph, GetCurrentProcessId());
    
    HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
    if (SUCCEEDED(hr)) 
    {
        hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pUnkGraph,
            pMoniker, pdwRegister);
        pMoniker->Release();
    }
    pROT->Release();
    
    return hr;
}

 

This function creates an alias and a new rot entry. The first parameter is a pointer to the filter graph. The second parameter receives a value to identify the new rot entry. Before the program releases the filter graph, call the following function to remove the rot entry.PdwregisterThis parameter is the identifier returned by the addtorot function.

void RemoveFromRot(DWORD pdwRegister)
{
    IRunningObjectTable *pROT;
    if (SUCCEEDED(GetRunningObjectTable(0, &pROT))) {
        pROT->Revoke(pdwRegister);
        pROT->Release();
    }
}

 

 

The following code example shows how to call these functions. In this example, the code for adding and deleting rot entries is compiled with conditional conditions, so it only contains the debugging version.

IGraphBuilder *pGraph;
DWORD dwRegister;
    
// Create the filter graph manager.
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
                        IID_IGraphBuilder, (void **)&pGraph);
#ifdef _DEBUG
hr = AddToRot(pGraph, &dwRegister);
#endif
 
// Rest of the application (not shown).
 
#ifdef _DEBUG
RemoveFromRot(dwRegister);
#endif
pGraph->Release();

 

View the filter graph in graphedit and run your program and graphedit at the same time. In graphedit [file], click Connect to remote graph ...]. In the connect to graph dialog box, select the PID of your program and click OK ]. Graphedit loads and displays the filter graph. Do not use other graphedit functions in this graph. Unexpected results may be referenced. For example, do not add or delete a filter, or stop or start a graph. Disable graphedit before exiting the program.

Note: Your program may be interrupted when it exists. You can ignore them.

The connect to graph dialog box is displayed.

 

Graphedit loads a graph and runs it in the target program environment. Therefore, graphedit is restricted because it is waiting for a thread. For example, this may happen when debugging your code.

This function should be used only in the debug version program, not in the release version, because it may allow other programs to view or control the filter graph.

 

Connect to a remote graph from the command line

Graphedit supports the command line option to load a remote graph and start it automatically. Syntax:

Graphedt-Moniker

Moniker is previously created and described using the addtorot function.

 

4.1.4 save a filter graph to the graphedit File

The following code example shows how to save a filter graph into A. GRF file. This may be useful for debugging your program.

HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath) 
{
    const WCHAR wszStreamName[] = L"ActiveMovieGraph"; 
    HRESULT hr;
    
    IStorage *pStorage = NULL;
    hr = StgCreateDocfile(
        wszPath,
        STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
        0, &pStorage);
    if(FAILED(hr)) 
    {
        return hr;
    }
 
    IStream *pStream;
    hr = pStorage->CreateStream(
        wszStreamName,
        STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
        0, 0, &pStream);
    if (FAILED(hr)) 
    {
        pStorage->Release();    
        return hr;
    }
 
    IPersistStream *pPersist = NULL;
    pGraph->QueryInterface(IID_IPersistStream, (void**)&pPersist);
    hr = pPersist->Save(pStream, TRUE);
    pStream->Release();
    pPersist->Release();
    if (SUCCEEDED(hr)) 
    {
        hr = pStorage->Commit(STGC_DEFAULT);
    }
    pStorage->Release();
    return hr;
}

 

For example, the following code creates a playback graph and saves it as mygraph. GRF.

void __cdecl main(void)
{
    HRESULT hr;
    IGraphBuilder *pGraph;
    CoInitialize(NULL);
    
    // Create the Filter Graph Manager and render a file.
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
        IID_IGraphBuilder, reinterpret_cast<void**>(&pGraph));
    hr = pGraph->RenderFile(L"C:\\Video.avi", NULL);
 
    if (SUCCEEDED(hr))
    {
        hr = SaveGraphFile(pGraph, L"C:\\MyGraph.grf");
    }
    
    pGraph->Release();
    CoUninitialize();
}

 

For more information about the stgcreatedocfile function, see the SDK documentation.

 

4.1.5 load a programmable graphedit File

(Loading a graphedit file programmatically)

The program can use the ipersiststream interface to load a GRF file, using the following code:

HRESULT LoadGraphFile(IGraphBuilder *pGraph, const WCHAR* wszName)
{
    IStorage *pStorage = 0;
    if (S_OK != StgIsStorageFile(wszName))
    {
        return E_FAIL;
    }
    HRESULT hr = StgOpenStorage(wszName, 0, 
        STGM_TRANSACTED | STGM_READ | STGM_SHARE_DENY_WRITE, 
        0, 0, &pStorage);
    if (FAILED(hr))
    {
        return hr;
    }
    IPersistStream *pPersistStream = 0;
    hr = pGraph->QueryInterface(IID_IPersistStream,
             reinterpret_cast<void**>(&pPersistStream));
    if (SUCCEEDED(hr))
    {
        IStream *pStream = 0;
        hr = pStorage->OpenStream(L"ActiveMovieGraph", 0, 
            STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pStream);
        if(SUCCEEDED(hr))
        {
            hr = pPersistStream->Load(pStream);
            pStream->Release();
        }
        pPersistStream->Release();
    }
    pStorage->Release();
    return hr;
}

 

Note: In the previous codeIpersiststream: LoadThe function returns a DirectShow error or successful code. To view a series of return values, see error and success codes.

The graphedit file is only used for debugging and debugging. We don't plan to use it for end-user programs.

For more information about the stgisstoragefile and stgopenstorage functions, see the SDK documentation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.