2017-02-18 3 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)을 유지한다. 이 같은 MFPutWorkItem 전화 (새 ReadByteAsyncCallback을 피하기)해야하므로

일반적으로 당신의 MediaByteStream는 IMFAsyncCallback을 구현 또한

hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, this, pResult);

, 난 그러면 BeginRead 내부의 잠금 장치가 표시되지 않습니다. 멀티 스레딩 환경에서 사용한다면 이것을 처리해야합니다. Close는 BeginRead가 호출되고 완료되지 않았을 때 호출 할 수 있으므로 문제가 발생할 수 있습니다.