C ++ and DirectShow

I'm just starting out with DirectShow programming, and the C-type character of the DirectShow API does a scan of my skin. Endless return codes instead of exceptions, addref / release everywhere, functions with pointer to pointers ...

Are there any "C ++ - friendly" DirectShow programming shells that hide all the ugliness of COM below.?

+3
source share
4 answers

For 98% of DirectShow code, you will never see a call to AddRef or Release. Always use CComPtr <>. There are a few minor exceptions to this rule.

, , CCritSec CAutoLock; , CCritSec, .

, :

#include <dxerr9.h>

...

HRESULT hr = S_OK;

... something goes wrong ...

CString err(::DXGetErrorString9(hr));
err += ::DXGetErrorDescription9(hr);

, , DShow. DShow, , ( ).

, DShow. , , .

+5

CComPtr ,

, HRESULTS. ... , ... -.

+4

, DirectShow quartz.dll DirectShow qedit.dll.

" COM" Visual ++ DirectShow.

media_file ( cl/D_UNICODE playfile.cpp):

#include <iostream> 

#import <quartz.dll> rename_namespace("dshow") 
#import <qedit.dll> rename_namespace("dshow")

// Required for CLSID_FilterGraph
#include <uuids.h> 
#pragma comment(lib, "strmiids.lib")

int wmain(int argc, wchar_t* argv[]) 
{  
    using namespace dshow;
    using namespace std;

    if (argc != 2)
    {
        wcout << "Usage: play media_file" << endl;
        return 1;
    }

    struct ComInitializer
    {
        ComInitializer() 
        { 
            ::CoInitialize(0);
        }

        ~ComInitializer()
        {
            ::CoUninitialize();
        }
    } comInit;

    try 
    { 
        IGraphBuilderPtr graphBuilder; 
        graphBuilder.CreateInstance(CLSID_FilterGraph); 

        graphBuilder->RenderFile(argv[1], 0);

        IMediaControlPtr mediaControl = graphBuilder;
        mediaControl->Run();

        wcout << "Press Return to stop playback." << endl;
        wcin.get();

        mediaControl->Stop();
    } 
    catch (const _com_error& err)
    {
        wcout << L"Error code: 0x" << hex << err.Error() << endl;
    }
} 

DirectShow oggenc.

+2

, , B true, B throw! ( → ).

( COM, "" COM):

std::ostream& operator<<( std::ostream& stream, wchar_t const s[] )
{
    Size const  nBytes      = wcstombs( 0, s, 0 );
    (nBytes >= 0)
        || throwX( "wcstombs failed to deduce buffer size" );

    Size const              bufSize     = nBytes + 1;
    std::vector< char >     buf( bufSize );

    // The count of bytes written does not include terminating nullbyte.
    wcstombs( &buf[0], s, bufSize )
        >> Accept< IsNonNegative >()
        || throwX( "wcstombs failed to convert string" );

    return (stream << &buf[0]);
}

, , .., .

, AddRef Release COM. (, ATL/MFC "" Visual ++), , . , . boost::intrusive_ptr.

hth.,

-2
source

Source: https://habr.com/ru/post/1780308/


All Articles