2017-02-18 94 views
1

我已經創建了ReadByteContainer來存儲當前數據,ReadByteAsyncCallback用於回調。有替代品會更好嗎?如何在IMFByteStream接口中實現`BeginRead`

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

回答

0

如果直到EndRead發生PB仍然有效,可避免拷貝(新ReadByteContainer),並只保留PB爲是。

通常你MediaByteStream實現IMFAsyncCallback所以你應該叫MFPutWorkItem像這樣(避免新ReadByteAsyncCallback):

hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, this, pResult);

另外,我看不到裏面的BeginRead一些鎖機制。如果你在多線程環境中使用它,你應該處理這個問題。當BeginRead被調用並且沒有完成時關閉可以被調用,所以你將面臨問題。