在DirectShow中保持宽高比? (窗口)C ++

时间:2017-08-01 03:21:31

标签: c++ directshow

编辑:更新了代码,调用DLL的应用程序不再崩溃。

当show_video()的第四个参数设置为true时,我希望DirectShow播放的视频保持其宽高比。这是我的DLL的来源:

#include <windows.h>
#include <dshow.h>

#pragma comment (lib, "strmiids.lib")
#define DLL extern "C" _declspec(dllexport)

wchar_t *convertCharArrayToLPCWSTR(const char* charArray) {
    wchar_t* wString = new wchar_t[4096];

    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);

    return wString;
}

DLL void show_video(double window1, HWND window2, char *fname, double keep_aspect_ratio) {
    CoInitialize(NULL);

    HRESULT hr = S_OK;

    IGraphBuilder *pGraph = NULL;
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    hr = pGraph->RenderFile(convertCharArrayToLPCWSTR(fname), NULL);

    IBaseFilter *pVideoRenderer = NULL;
    hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pVideoRenderer);

    IVMRAspectRatioControl *pAspectRatio = NULL;
    hr = pVideoRenderer->QueryInterface(IID_IVMRAspectRatioControl, (void**)&pAspectRatio);

    if ((bool)keep_aspect_ratio == true) {
        hr = pAspectRatio->SetAspectRatioMode(VMR_ARMODE_LETTER_BOX);
    }
    else {
        hr = pAspectRatio->SetAspectRatioMode(VMR_ARMODE_NONE);
    }

    IVideoWindow *pVidWin = NULL;
    hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

    RECT rect;
    if ((HWND)(DWORD)window1 != NULL) {
        SetWindowLong((HWND)(DWORD)window1, GWL_STYLE, GetWindowLong((HWND)(DWORD)window1, GWL_STYLE) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
        hr = pVidWin->put_Owner((OAHWND)(HWND)(DWORD)window1);
        GetClientRect((HWND)(DWORD)window1, &rect);
    }
    else {
        SetWindowLong(window2, GWL_STYLE, GetWindowLong(window2, GWL_STYLE) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
        hr = pVidWin->put_Owner((OAHWND)window2);
        GetClientRect(window2, &rect);
    }

    hr = pVidWin->SetWindowPosition(0, 0, rect.right - rect.left, rect.bottom - rect.top);
    hr = pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
    hr = pVidWin->SetWindowForeground(OATRUE);
    hr = pVidWin->HideCursor(OATRUE);

    IMediaControl *pControl = NULL;
    hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
    hr = pControl->Run();

    IMediaEvent *pEvent = NULL;
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    long evCode;

    hr = pEvent->WaitForCompletion(INFINITE, &evCode);

    hr = pControl->Stop();
    hr = pVidWin->put_Visible(OAFALSE);
    hr = pVidWin->put_Owner(NULL);

    pEvent->Release();
    pControl->Release();
    pVidWin->Release();

    pAspectRatio->Release();
    pVideoRenderer->Release();
    pGraph->Release();

    CoUninitialize();
}

就目前而言,从我的应用程序调用DLL,我为第三个参数选择的视频播放正常,但视频不保持其原始宽高比。有谁知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

找到解决方案。初始化pVideoRenderer后,我需要添加以下行:

pGraph->FindFilterByName(L"Video Renderer", &pVideoRenderer);

因此生成的代码如下所示:

#include <windows.h>
#include <dshow.h>

#pragma comment (lib, "strmiids.lib")
#define DLL extern "C" _declspec(dllexport)

wchar_t *convertCharArrayToLPCWSTR(const char* charArray) {
    wchar_t* wString = new wchar_t[4096];

    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);

    return wString;
}

DLL void show_video(double window1, HWND window2, char *fname, double keep_aspect_ratio) {
    CoInitialize(NULL);

    HRESULT hr = S_OK;

    IGraphBuilder *pGraph = NULL;
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    hr = pGraph->RenderFile(convertCharArrayToLPCWSTR(fname), NULL);

    IBaseFilter *pVideoRenderer = NULL;
    hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pVideoRenderer);
    pGraph->FindFilterByName(L"Video Renderer", &pVideoRenderer);

    IVMRAspectRatioControl *pAspectRatio = NULL;
    hr = pVideoRenderer->QueryInterface(IID_IVMRAspectRatioControl, (void**)&pAspectRatio);

    if ((bool)keep_aspect_ratio == true) {
        hr = pAspectRatio->SetAspectRatioMode(VMR_ARMODE_LETTER_BOX);
    }
    else {
        hr = pAspectRatio->SetAspectRatioMode(VMR_ARMODE_NONE);
    }

    IVideoWindow *pVidWin = NULL;
    hr = pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);

    RECT rect;
    if ((HWND)(DWORD)window1 != NULL) {
        SetWindowLong((HWND)(DWORD)window1, GWL_STYLE, GetWindowLong((HWND)(DWORD)window1, GWL_STYLE) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
        hr = pVidWin->put_Owner((OAHWND)(HWND)(DWORD)window1);
        GetClientRect((HWND)(DWORD)window1, &rect);
    }
    else {
        SetWindowLong(window2, GWL_STYLE, GetWindowLong(window2, GWL_STYLE) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
        hr = pVidWin->put_Owner((OAHWND)window2);
        GetClientRect(window2, &rect);
    }

    hr = pVidWin->SetWindowPosition(0, 0, rect.right - rect.left, rect.bottom - rect.top);
    hr = pVidWin->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
    hr = pVidWin->SetWindowForeground(OATRUE);
    hr = pVidWin->HideCursor(OATRUE);

    IMediaControl *pControl = NULL;
    hr = pGraph->QueryInterface(IID_IMediaControl, (void**)&pControl);
    hr = pControl->Run();

    IMediaEvent *pEvent = NULL;
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    long evCode;

    hr = pEvent->WaitForCompletion(INFINITE, &evCode);

    hr = pControl->Stop();
    hr = pVidWin->put_Visible(OAFALSE);
    hr = pVidWin->put_Owner(NULL);

    pEvent->Release();
    pControl->Release();
    pVidWin->Release();

    pAspectRatio->Release();
    pVideoRenderer->Release();
    pGraph->Release();

    CoUninitialize();
}

问题解决了! :d

答案 1 :(得分:0)

  

...调用DLL的进程现在崩溃了。

进程崩溃假设您可以提供其他详细信息,例如崩溃点,异常详细信息等。

可能的原因是前E_NOINTERFACE次呼叫QueryInterface失败并且没有IVMRAspectRatioControl指针。

Filter Graph Manager不应该实现IVMRAspectRatioControl。如果您的过滤器图表中有一个视频混合渲染器过滤器,请直接QueryInterface

UPD。另一个答案的解决方案是不应该做的事情的一个例子。如果您对VR进行CoCreateInstance,则应将其添加到图表中并将其包含在渲染过程中。否则,您根本不需要CoCreateInstance,而您不应该按名称找到过滤器 - 而是枚举过滤器并通过查找实现相关接口的过滤器来识别您的VR。