//------------------------------------------------------------------------------
// File: AsyncIo.cpp
//
// Desc: DirectShow sample code - base library with I/O functionality.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#include "stdafx.h"
#include <streams.h>
#include "asyncio.h"
// --- CAsyncRequest ---
// implementation of CAsyncRequest representing a single
// outstanding request. All the i/o for this object is done
// in the Complete method.
// init the params for this request.
// Read is not issued until the complete call
HRESULT
CAsyncRequest::Request(
CAsyncIo *pIo,
CAsyncStream *pStream,
LONGLONG llPos,
LONG lLength,
BOOL bAligned,
BYTE* pBuffer,
LPVOID pContext, // filter's context
DWORD dwUser) // downstream filter's context
{
m_pIo = pIo;
m_pStream = pStream;
m_llPos = llPos;
m_lLength = lLength;
m_bAligned = bAligned;
m_pBuffer = pBuffer;
m_pContext = pContext;
m_dwUser = dwUser;
m_hr = VFW_E_TIMEOUT; // not done yet
return S_OK;
}
// issue the i/o if not overlapped, and block until i/o complete.
// returns error code of file i/o
//
//
HRESULT
CAsyncRequest::Complete()
{
m_pStream->Lock();
m_hr = m_pStream->SetPointer(m_llPos);
if(S_OK == m_hr)
{
DWORD dwActual;
m_hr = m_pStream->Read(m_pBuffer, m_lLength, m_bAligned, &dwActual);
if(m_hr == OLE_S_FIRST)
{
if(m_pContext)
{
IMediaSample *pSample = reinterpret_cast<IMediaSample *>(m_pContext);
pSample->SetDiscontinuity(TRUE);
m_hr = S_OK;
}
}
if(FAILED(m_hr))
{
}
else if(dwActual != (DWORD)m_lLength)
{
// tell caller size changed - probably because of EOF
m_lLength = (LONG) dwActual;
m_hr = S_FALSE;
}
else
{
m_hr = S_OK;
}
}
m_pStream->Unlock();
return m_hr;
}
// --- CAsyncIo ---
// note - all events created manual reset
CAsyncIo::CAsyncIo(CAsyncStream *pStream)
: m_hThread(NULL),
m_evWork(TRUE),
m_evDone(TRUE),
m_evStop(TRUE),
m_listWork(NAME("Work list")),
m_listDone(NAME("Done list")),
m_bFlushing(FALSE),
m_cItemsOut(0),
m_bWaiting(FALSE),
m_pStream(pStream)
{