gpt4 book ai didi

c++ - 如何在 IMFByteStream 接口(interface)中实现 `BeginRead`

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:06 51 4
gpt4 key购买 nike

我创建了 ReadByteContainer 来存储当前数据,并创建了 ReadByteAsyncCallback 用于回调。是否有更好的替代方案?

        HRESULT MediaByteStream::BeginRead(
BYTE *pb,
ULONG cb,
IMFAsyncCallback *pCallback,
IUnknown *punkState)
{
HRESULT hr = S_OK;

// Create a new read byte container.
ReadByteContainer* readBytes = new ReadByteContainer(pb, cb);
ReadByteAsyncCallback* readCallback = new ReadByteAsyncCallback(this);

// If not created.
if (readBytes == NULL)
{
return E_OUTOFMEMORY;
}

// If not created.
if (readCallback == NULL)
{
return E_OUTOFMEMORY;
}

IMFAsyncResult *pResult = NULL;
readBytes->_readCallback = readCallback;

// Creates an asynchronous result object. Use this function if you are implementing an asynchronous method.
hr = MFCreateAsyncResult(readBytes, pCallback, punkState, &pResult);

if (SUCCEEDED(hr))
{
// Start a new work item thread.
hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, readCallback, pResult);
pResult->Release();
}

// Return the result.
return hr;
}

最佳答案

如果 pb 在 EndRead 发生之前保持有效,则可以避免复制(新的 ReadByteContainer),并保持 pb 不变。

通常你的 MediaByteStream 实现 IMFAsyncCallback 所以你应该像这样调用 MFPutWorkItem(避免新的 ReadByteAsyncCallback):

hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, this, pResult);

此外,我没有在 BeginRead 中看到一些锁定机制。如果你在多线程环境中使用它,你应该处理这个。当调用了 BeginRead 但未完成时,可以调用 close,因此您将遇到问题。

关于c++ - 如何在 IMFByteStream 接口(interface)中实现 `BeginRead`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311667/

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