我创建了 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,因此您将遇到问题。
我是一名优秀的程序员,十分优秀!