媒体基金会:从SinkWriter获取MediaSink

时间:2018-03-23 22:34:15

标签: c++ ms-media-foundation

我正在尝试将MP4文件接收器添加到拓扑中。当我的MediaSource已经是MP4时,我会使用MFCreateMPEG4MediaSinkMF_MPEG4SINK_SPSPPS_PASSTHROUGH。当我的MediaSource不是MP4(因此来自网络摄像头的原始YUV)时,我想使用MFCreateSinkWriterFromURL,这样我就不必弄清楚MP4标题和其他复杂内容。

根据the MSDN Docs,我应该可以使用GetServiceForStream来获取MediaSink,因为输入类型与输出类型不同。但是它始终返回MF_E_UNSUPPORTED_SERVICE

如何从MediaSink中获取基础MediaSinkWriter

或者,如何轻松地为任意拓扑创建MP4媒体接收器?

HRESULT CreateVideoFileSink(
    IMFStreamDescriptor *pSourceSD,     // Pointer to the stream descriptor.
    LPCWSTR pFilename,                  // Name of file to save to.
    IMFStreamSink **ppStream)           // Receives a pointer to the stream sink.
    HRESULT hr = S_OK;
    CComPtr<IMFAttributes> pAttr;
    CComPtr<IMFMediaTypeHandler> pHandler;
    CComPtr<IMFMediaType> pType;
    CComPtr<IMFMediaSink> pSink;
    CComPtr<IMFStreamSink> pStream;
    CComPtr<IMFSinkWriter> pSinkWriter;
    CComPtr<IMFByteStream> pByteStream;

    *ppStream = nullptr;

    // Get the media type handler for the stream.
    IFR(pSourceSD->GetMediaTypeHandler(&pHandler));

    // Get the major media type.
    GUID guidMajorType;
    IFR(pHandler->GetMajorType(&guidMajorType));

    IFR(MFCreateAttributes(&pAttr, 1));
    IFR(pAttr->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, TRUE));

    // Create an output file 
    if (MFMediaType_Video == guidMajorType)
    {
        GUID guidSubType;
        IFR(pHandler->GetCurrentMediaType(&pType));
        IFR(pType->GetGUID(MF_MT_SUBTYPE, &guidSubType));

        if (MFVideoFormat_H264 == guidSubType)
        {
            // ... use MFCreateMPEG4MediaSink
        }
        else
        {
            IFR(MFCreateSinkWriterFromURL(pFilename, nullptr, pAttr, &pSinkWriter));
            DWORD streamIdx;
            IFR(pSinkWriter->AddStream(pType, &streamIdx));
            IFR(pSinkWriter->GetServiceForStream(MF_SINK_WRITER_MEDIASINK, GUID_NULL, IID_PPV_ARGS(&pSink)));
            IFR(pSink->GetStreamSinkByIndex(streamIdx, &pStream));
        }
    }
    else
    {
        // Don't use this stream
        IFR(E_FAIL)
    }

    // Return IMFStreamSink pointer to caller.
    *ppStream = pStream.Detach();

    return S_OK;
}

1 个答案:

答案 0 :(得分:2)

在写完问题后立即想出来 - 当然。在您致电SinkWriter之前,MediaSink不会 {/ 1}}。

BeginWriting

(确保在您使用IFR(MFCreateSinkWriterFromURL(pFilename, nullptr, pAttr, &pSinkWriter)); DWORD streamIdx; IFR(pSinkWriter->AddStream(pType, &streamIdx)); IFR(pSinkWriter->BeginWriting()); // <<---- IFR(pSinkWriter->GetServiceForStream(MF_SINK_WRITER_MEDIASINK, GUID_NULL, IID_PPV_ARGS(&pSink))); IFR(pSink->GetStreamSinkByIndex(streamIdx, &pStream)); 时{0}}释放SinkWriter