사용자 지정 할당자 제공

[이 페이지와 연결된 기능인 DirectShow는 레거시 기능입니다. MediaPlayer, IMFMediaEngine 및 Media Foundation의 오디오/비디오 캡처로 대체되었습니다. 이러한 기능은 Windows 10 및 Windows 11 최적화되었습니다. 가능한 경우 새 코드가 DirectShow 대신 Media Foundation에서 MediaPlayer, IMFMediaEngine오디오/비디오 캡처를 사용하는 것이 좋습니다. 가능한 경우 레거시 API를 사용하는 기존 코드를 다시 작성하여 새 API를 사용하도록 제안합니다.]

이 섹션에서는 필터에 대한 사용자 지정 할당자를 제공하는 방법을 설명합니다. IMemInputPin 연결만 설명되지만 IAsyncReader 연결에 대한 단계는 비슷합니다.

먼저 할당자에 대한 C++ 클래스를 정의합니다. 할당자는 표준 할당자 클래스 중 하나인 CBaseAllocator 또는 CMemAllocator에서 파생되거나 완전히 새로운 할당자 클래스를 만들 수 있습니다. 새 클래스를 만드는 경우 IMemAllocator 인터페이스를 노출해야 합니다.

나머지 단계는 할당자가 필터의 입력 핀 또는 출력 핀에 속하는지 여부에 따라 달라집니다. 입력 핀은 할당자 협상 단계에서 출력 핀과 다른 역할을 합니다. 출력 핀은 궁극적으로 할당자를 선택하기 때문입니다.

입력 핀에 대한 사용자 지정 할당자 제공

입력 핀에 할당자를 제공하려면 입력 핀의 CBaseInputPin::GetAllocator 메서드를 재정의합니다. 이 메서드 내에서 m_pAllocator 멤버 변수를 검사. 이 변수가 NULL이 아닌 경우 이 연결에 할당자가 이미 선택되어 있으므로 GetAllocator 메서드는 해당 할당자에 대한 포인터를 반환해야 합니다. m_pAllocatorNULL인 경우 할당자가 선택되지 않았으므로 GetAllocator 메서드는 입력 핀의 기본 할당자에 대한 포인터를 반환해야 합니다. 이 경우 사용자 지정 할당자의 instance 만들고 해당 IMemAllocator 포인터를 반환합니다. 다음 코드는 GetAllocator 메서드를 구현하는 방법을 보여줍니다.

STDMETHODIMP CMyInputPin::GetAllocator(IMemAllocator **ppAllocator)
{
    CheckPointer(ppAllocator, E_POINTER);
    if (m_pAllocator)  
    {
        // We already have an allocator, so return that one.
        *ppAllocator = m_pAllocator;
        (*ppAllocator)->AddRef();
        return S_OK;
    }

    // No allocator yet, so propose our custom allocator. The exact code
    // here will depend on your custom allocator class definition.
    HRESULT hr = S_OK;
    CMyAllocator *pAlloc = new CMyAllocator(&hr);
    if (!pAlloc)
    {
        return E_OUTOFMEMORY;
    }
    if (FAILED(hr))
    {
        delete pAlloc;
        return hr;
    }

    // Return the IMemAllocator interface to the caller.
    return pAlloc->QueryInterface(IID_IMemAllocator, (void**)ppAllocator);
}

업스트림 필터가 할당자를 선택하면 입력 핀의 IMemInputPin::NotifyAllocator 메서드를 호출합니다. CBaseInputPin::NotifyAllocator 메서드를 재정의하여 할당자 속성을 검사. 경우에 따라 입력 핀이 사용자 지정 할당자가 아닌 경우 할당자를 거부할 수 있지만 이로 인해 전체 핀 연결이 실패할 수 있습니다.

출력 핀에 대한 사용자 지정 할당자 제공

출력 핀에 할당자를 제공하려면 CBaseOutputPin::InitAllocator 메서드를 재정의하여 할당자의 instance 만듭니다.

HRESULT MyOutputPin::InitAllocator(IMemAllocator **ppAllocator)
{
    HRESULT hr = S_OK;
    CMyAllocator *pAlloc = new CMyAllocator(&hr);
    if (!pAlloc)
    {
        return E_OUTOFMEMORY;
    }

    if (FAILED(hr))
    {
        delete pAlloc;
        return hr;
    }

    // Return the IMemAllocator interface.
    return pAlloc->QueryInterface(IID_IMemAllocator, (void**)ppAllocator);
}

기본적으로 CBaseOutputPin 클래스는 먼저 입력 핀에서 할당자를 요청합니다. 해당 할당자가 적합하지 않은 경우 출력 핀은 자체 할당자를 만듭니다. 연결에서 사용자 지정 할당자를 사용하도록 하려면 CBaseOutputPin::D ecideAllocator 메서드를 재정의합니다. 그러나 다른 필터에는 자체 사용자 지정 할당자가 필요할 수도 있으므로 출력 핀이 특정 필터와 연결되지 않도록 방지할 수 있습니다. 세 번째 옵션은 순서를 전환하는 것입니다. 먼저 사용자 지정 할당자를 시도한 다음 다른 필터의 할당자로 대체합니다.

할당자 협상