HRESULT CPlayer::AddBranchToPartialTopology(IMFTopology *pTopology, IMFPresentationDescriptor *pSourcePD, DWORD iStream)
{
    TRACE((L"CPlayer::AddBranchToPartialTopology\n"));

    assert(pTopology != NULL);

    IMFStreamDescriptor* pSourceSD = NULL;
    IMFTopologyNode* pSourceNode = NULL;
    IMFTopologyNode* pOutputNode = NULL;
    BOOL fSelected = FALSE;

    HRESULT hr = S_OK;

    // Get the stream descriptor for this stream.
    CHECK_HR(hr = pSourcePD->GetStreamDescriptorByIndex(iStream, &fSelected, &pSourceSD));

    // Create the topology branch only if the stream is selected.
    // Otherwise, do nothing.
    if (fSelected)
    {
        // Create a source node for this stream.
        CHECK_HR(hr = CreateSourceStreamNode(m_pSource, pSourcePD, pSourceSD, &pSourceNode));

        // Create the output node for the renderer.
        CHECK_HR(hr = CreateOutputNode(pSourceSD, m_hwndVideo, &pOutputNode));

        // Add both nodes to the topology.
        CHECK_HR(hr = pTopology->AddNode(pSourceNode));

        CHECK_HR(hr = pTopology->AddNode(pOutputNode));

        // Connect the source node to the output node.
        CHECK_HR(hr = pSourceNode->ConnectOutput(0, pOutputNode, 0));
    }

done:
    // Clean up.
    SAFE_RELEASE(pSourceSD);
    SAFE_RELEASE(pSourceNode);
    SAFE_RELEASE(pOutputNode);
    return hr;
}
HRESULT CreateTopologyBranch(
    IMFTopology *pTopology,
    IMFMediaSource *pSource,          // Media source.
    IMFPresentationDescriptor *pPD,   // Presentation descriptor.
    IMFStreamDescriptor *pSD,         // Stream descriptor.
    IMFMediaSink *pSink
    )
{

    CComPtr<IMFTopologyNode> pSourceNode;
    CComPtr<IMFTopologyNode> pOutputNode;

    HRESULT hr = S_OK;

    hr = CreateSourceNode(pSource, pPD, pSD, &pSourceNode);

    if (SUCCEEDED(hr))
    {
        hr = CreateOutputNode(pSink, 0, &pOutputNode);
    }

    if (SUCCEEDED(hr))
    {
        hr = pTopology->AddNode(pSourceNode);
    }

    if (SUCCEEDED(hr))
    {
        hr = pTopology->AddNode(pOutputNode);
    }

    if (SUCCEEDED(hr))
    {
        hr = pSourceNode->ConnectOutput(0, pOutputNode, 0);
    }

    return hr;
}