STDMETHODIMP FWmfMediaByteStream::BeginRead(BYTE* pb, ULONG cb, IMFAsyncCallback* pCallback, IUnknown* punkState)
{
	if ((pCallback == NULL) || (pb == NULL))
	{
		return E_INVALIDARG;
	}

	TComPtr<FWmfMediaReadState> ReadState = new(std::nothrow) FWmfMediaReadState(pb, cb);

	if (ReadState == NULL)
	{
		return E_OUTOFMEMORY;
	}

	IMFAsyncResult* AsyncResult = NULL;
	HRESULT Result = ::MFCreateAsyncResult(ReadState, pCallback, punkState, &AsyncResult);

	if (SUCCEEDED(Result))
	{
		AsyncReadInProgress = true;
		Result = ::MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, this, AsyncResult);
		AsyncResult->Release();
	}

	return Result;
}
STDMETHODIMP FWmfMediaByteStream::Invoke(IMFAsyncResult* AsyncResult)
{
	// recover read state
	IUnknown* State = NULL;
	
	if (FAILED(AsyncResult->GetState(&State)))
	{
		return S_OK;
	}

	IMFAsyncResult* CallerResult = NULL;

	if (FAILED(State->QueryInterface(IID_PPV_ARGS(&CallerResult))))
	{
		return S_OK;
	}

	IUnknown* Unknown = NULL;
	
	if ((CallerResult != NULL) && FAILED(CallerResult->GetObject(&Unknown)))
	{
		return S_OK;
	}

	FWmfMediaReadState* ReadState = static_cast<FWmfMediaReadState*>(Unknown);

	// perform the read
	ULONG cbRead;
	Read(ReadState->GetReadBuffer(), ReadState->GetReadBufferSize() - ReadState->GetBytesRead(), &cbRead);
	ReadState->AddBytesRead(cbRead);

	// notify caller
	if (CallerResult != NULL)
	{
		CallerResult->SetStatus(S_OK);
		::MFInvokeCallback(CallerResult);
	}

	// clean up
	if (CallerResult != NULL)
	{
		CallerResult->Release();
	}

	if (Unknown != NULL)
	{
		Unknown->Release();
	}

	if (State != NULL)
	{
		State->Release();
	}

	return S_OK;
}
Exemplo n.º 3
0
			/// <summary>
			/// Begins an asynchronous read operation from the stream.
			/// </summary>
			/// <param name="pb">Pointer to a buffer that receives the data. The caller must allocate the buffer. [in]</param>
			/// <param name="cb">Size of the buffer in bytes. [in]</param>
			/// <param name="pCallback">Pointer to the IMFAsyncCallback interface of a callback object. The caller must implement this interface. [in]</param>
			/// <param name="punkState">Pointer to the IUnknown interface of a state object, defined by the caller. This parameter can be NULL. 
			/// You can use this object to hold state information. The object is returned to the caller when the callback is invoked. [in]</param>
			/// <returns>The result of the operation.</returns>
			HRESULT StoreByteStream::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;
			}
/** Asyncronous callback */
STDMETHODIMP FImfByteStream::Invoke( IMFAsyncResult* pResult )
{
	HRESULT HResult = S_OK;

	IUnknown* State = NULL;
	IUnknown* Unknown = NULL;
	IMFAsyncResult* CallerResult = NULL;

	AsyncReadState* Op = NULL;

	HResult = pResult->GetState( &State );
	check( SUCCEEDED( HResult ) );

	HResult = State->QueryInterface( IID_PPV_ARGS( &CallerResult ) );
	check( SUCCEEDED( HResult ) );

	HResult = CallerResult->GetObject( &Unknown );
	check( SUCCEEDED( HResult ) );

	Op = static_cast<AsyncReadState*>( Unknown );

	/* Do actual read */
	ULONG cbRead;
	Read( Op->GetBuffer( ), Op->GetCount( ) - Op->GetBytesRead( ), &cbRead );
	Op->SetBytesRead( cbRead + Op->GetBytesRead( ) );

	if( CallerResult )
	{
		CallerResult->SetStatus( HResult );
		MFInvokeCallback( CallerResult );
	}

	if( State != NULL ) { State->Release( ); }
	if( Unknown != NULL ) { Unknown->Release( ); }
	if( CallerResult != NULL ){ CallerResult->Release( ); }

	return S_OK;
}
/** Begins an asyncronous read */
STDMETHODIMP FImfByteStream::BeginRead( BYTE* pb, ULONG cb, IMFAsyncCallback* pCallback, IUnknown* punkState )
{
	HRESULT HResult = S_OK;

	if( !pCallback || !pb )
		return E_INVALIDARG;

	IMFAsyncResult* Result = NULL;
	AsyncReadState* State = new( std::nothrow ) AsyncReadState( pb, cb );
	if( State == NULL )
		return E_OUTOFMEMORY;

	HResult = MFCreateAsyncResult( State, pCallback, punkState, &Result );
	State->Release( );

	if( SUCCEEDED( HResult ) )
	{
		IsCurrentlyReading = true;
		HResult = MFPutWorkItem( MFASYNC_CALLBACK_QUEUE_STANDARD, this, Result );
		Result->Release( );
	}

	return HResult;
}
Exemplo n.º 6
0
			/// <summary>
			/// Called when an asynchronous operation is completed.
			/// </summary>
			/// <param name="pAsyncResult">Pointer to the IMFAsyncResult interface. Pass this pointer to the asynchronous End... method to complete the asynchronous call.</param>
			/// <returns>The result of the operation.</returns>
			HRESULT ReadByteAsyncCallback::Invoke(IMFAsyncResult* pAsyncResult)
			{
				// Enter critical section.
				EnterCriticalSection(&_critsec);
				HRESULT hr = S_OK;

				ULONG pcbRead;
				IUnknown *pState = NULL;
				IUnknown *pUnk = NULL;
				IMFAsyncResult *pCallerResult = NULL;
				ReadByteContainer *pReader = NULL;

				// Get the asynchronous result object for the application callback. 
				hr = pAsyncResult->GetState(&pState);
				if (FAILED(hr))
				{
					goto done;
				}

				// Get the caller result interface.
				hr = pState->QueryInterface(IID_PPV_ARGS(&pCallerResult));
				if (FAILED(hr))
				{
					goto done;
				}

				// Get the object that holds the state information for the asynchronous method.
				hr = pCallerResult->GetObject(&pUnk);
				if (FAILED(hr))
				{
					goto done;
				}

				// Get the reader byte container.
				pReader = static_cast<ReadByteContainer*>(pUnk);

				// Write the bytes.
				_byteStream->Read(pReader->_pb, pReader->_cb, &pcbRead);

				// Assign the number of bytes writen.
				pReader->_pcbRead = pcbRead;

			done:
				// Set the result.
				// Signal the application.
				if (pCallerResult)
				{
					pCallerResult->SetStatus(hr);
					MFInvokeCallback(pCallerResult);
				}

				// Clean-up.
				SafeRelease(&pState);
				SafeRelease(&pUnk);
				SafeRelease(&pCallerResult);

				// Leave critical section.
				LeaveCriticalSection(&_critsec);

				// Return the result.
				return hr;
			}