IWMEncDataView2::put_FrameRateBalance

Windows Media Encoder SDK banner art

The put_FrameRateBalance method specifies a Boolean value indicating whether the frame rate is balanced.

Syntax

HRESULT put_FrameRateBalance(
  DWORD  dwStreamCookie,
  VARIANT_BOOL  fFrameRateBalance
);

Parameters

dwStreamCookie

[in]  DWORD containing the stream cookie.

fFrameRateBalance

[in]  VARIANT_BOOL that indicates whether the frame rate is balanced.

Return Values

If the method succeeds, it returns S_OK. If it fails, it supports the IErrorInfo interface and returns an HRESULT error code.

Remarks

When the frame rate is balanced, the encoding process automatically detects the CPU usage and then adjusts the frame rate when displaying previews and postviews to optimize performance. This setting is not applied when transcoding files.

Example Code

#include <windows.h>
#include <atlbase.h>
#include "C:\WMSDK\WMEncSDK9\include\wmencode.h"
#include "C:\WMSDK\WMEncSDK9\include\wmencvu.h"
#include <conio.h> // for kbhit()

    // Declare variables.
    HRESULT hr;
    IWMEncoder* pEncoder;
    IWMEncDataView* pPreview;
    IWMEncDataView2* pPreview2;
    IWMEncDataViewCollection* pPreviewColl;
    IWMEncSourceGroupCollection* pSrcGrpColl;
    IWMEncSourceGroup* pSrcGrp;
    IWMEncSource* pSrcAud;
    IWMEncSource* pSrcVid;
    IWMEncProfileCollection* pProColl;
    IWMEncProfile* pPro;
    IWMEncBroadcast* pBrdCst;

    CComBSTR bstrName = NULL;

    // Initialize the COM library and retrieve a pointer
    // to an IWMEncoder interface.
    hr = CoInitialize(NULL);

    if ( SUCCEEDED( hr ) )
    {
        hr = CoCreateInstance(CLSID_WMEncoder,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IWMEncoder,
            (void**) &pEncoder);
    }

    // Retrieve a pointer to an IWMEncSourceGroupCollection interface.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->get_SourceGroupCollection(&pSrcGrpColl);
    }

    // Add a source group to the collection.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrpColl->Add(CComBSTR("SG_1"), &pSrcGrp);
    }

    // Add an audio source and a video source.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrp->AddSource(WMENC_VIDEO, &pSrcVid);
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrp->AddSource(WMENC_AUDIO, &pSrcAud);
    }

    // Specify the sources.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcVid->SetInput(CComBSTR("Device://default_video_device"));
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcAud->SetInput(CComBSTR("Device://default_audio_device"));
    }

    // Specify the output.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->get_Broadcast(&pBrdCst); 
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pBrdCst->put_PortNumber(WMENC_PROTOCOL_HTTP, 8080);
    }

    // Select the 5th profile from the collection and set it into the source group.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->get_ProfileCollection(&pProColl);
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pProColl->Item(4, &pPro); 
    }

    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcGrp->put_Profile(CComVariant(pPro));
    }

    // Retrieve a pointer to a preview object.
    if ( SUCCEEDED( hr ) )
    {
        hr = CoCreateInstance( CLSID_WMEncPreview,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IWMEncDataView,
            (void**)&pPreview);
    }

    // Retrieve the preview collection.
    if ( SUCCEEDED( hr ) )
    {
        hr = pSrcVid->get_PreviewCollection( &pPreviewColl );
    }

    // Add the postiew object to the data view collection. If you set the
    // cookie to -1, the encoder engine automatically generates a unique cookie.
    long lCookie = -1;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreviewColl->Add( pPreview, &lCookie);
    }

    // Continue configuring the encoder engine.
    // Initialize the encoder engine.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->PrepareToEncode(VARIANT_TRUE);
    }

    // Start encoding.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->Start();
    }

    // Start viewing the stream in a pop-up window.
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview->Start(lCookie);
    }

    // Retrieve a pointer to an IWMEncDataView2 interface.
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview->QueryInterface(IID_IWMEncDataView2, (void**)&pPreview2);
    }

    // Set the view to available. 
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview2->put_ViewAvailable(lCookie, VARIANT_TRUE);
    }

    // Determine whether the view is available. 
    VARIANT_BOOL bIsViewAvailable;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview2->get_ViewAvailable(lCookie, &bIsViewAvailable);
    }

    // Set the frame rate to balanced. 
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview2->put_FrameRateBalance(lCookie, VARIANT_TRUE);
    }

    // Determine whether the frame rate is balanced. 
    VARIANT_BOOL bIsBalanced;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview2->get_FrameRateBalance(lCookie, &bIsBalanced);
    }

    // Determine whether the view is running. 
    VARIANT_BOOL bIsRunning;
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview2->IsRunning(lCookie, &bIsRunning);
    }

    // Let the process run 10 seconds.
    int i = 0;
    while ( i < 100 )
    {
        MSG msg;
        Sleep ( 100 );

        while ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
        i++;
    }

    // When finished, stop the preview.
    if ( SUCCEEDED( hr ) )
    {
        hr = pPreview->Stop(lCookie);
    }

    // Stop the encoding process.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->Stop();
    }

    // Release pointers.
    if ( pPreview )
    {
        pPreview->Release();
        pPreview = NULL;
    }
    if ( pPreview2 )
    {
        pPreview2->Release();
        pPreview2 = NULL;
    }
    if ( pPreviewColl )
    {
        pPreviewColl->Release();
        pPreviewColl = NULL;
    }
    if ( pSrcGrpColl )
    {
        pSrcGrpColl->Release();
        pSrcGrpColl = NULL;
    }
    if ( pSrcGrp )
    {
        pSrcGrp->Release();
        pSrcGrp = NULL;
    }
    if ( pSrcAud )
    {
        pSrcAud->Release();
        pSrcAud = NULL;
    }
    if ( pSrcVid )
    {
        pSrcVid->Release();
        pSrcVid = NULL;
    }
    if ( pProColl )
    {
        pProColl->Release();
        pProColl = NULL;
    }
    if ( pPro )
    {
        pPro->Release();
        pPro = NULL;
    }
    if ( pEncoder )
    {
        pEncoder->Release();
        pEncoder = NULL;
    }

Requirements

Header: wmencvu.h

Library: wmprevu.dll

See Also