gpt4 book ai didi

c++ - 媒体基础 : Getting a MediaSink from a SinkWriter

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:58 31 4
gpt4 key购买 nike

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

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

如何从 MediaSinkWriter 中获取底层的 MediaSink

或者,我如何轻松地为任意拓扑创建 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;
}

最佳答案

当然是在写完问题后就想通了。在您调用 BeginWriting 之前,SinkWriter 没有 MediaSink

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));

(确保在使用 StreamSink 时不要让 SinkWriter 被释放)

关于c++ - 媒体基础 : Getting a MediaSink from a SinkWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459148/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com