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;
}
示例#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;
}