MemFilter.h
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:4k
- //
- // MemFilter.h
- //
- #ifndef __MemFilter_h__
- #define __MemFilter_h__
- #include <streams.h>
- #include <stdio.h>
- #include "asyncio.h"
- #include "asyncrdr.h"
- #include "CMediaSocketClient.h"
- #include "CDataAdmin.h"
- #include "MyDef.h"
- //////////////////////////////////////////////////////////////////////
- // Define an internal filter that wraps the base CBaseReader stuff //
- //////////////////////////////////////////////////////////////////////
- class CMemStream : public CAsyncStream
- {
- private:
- CCritSec m_csLock;
- CDataAdmin * m_pDataList;
- ULONG m_ulPositionInPack;
- // Total length available
- LONGLONG m_llLength;
- // Bytes totally read
- DWORD m_dwTimeStart;
- public:
- CMemStream(CDataAdmin* inBuffer)
- {
- m_pDataList = inBuffer;
- m_ulPositionInPack = 0;
- m_llLength = 0;
- }
- HRESULT SetPointer(LONGLONG llPos)
- {
- return S_OK;
- }
- HRESULT Read(PBYTE pbBuffer,
- DWORD dwBytesToRead,
- BOOL bAlign,
- LPDWORD pdwBytesRead)
- {
- if (m_pDataList == NULL)
- return S_FALSE;
- CAutoLock lck(&m_csLock);
- DWORD dwHaveRead = 0;
- PMPEG1_PACK pPack = NULL;
- while (dwHaveRead < dwBytesToRead)
- {
- if (dwBytesToRead - dwHaveRead >= MPEG1_PACK_SIZE - m_ulPositionInPack)
- {
- // Just copy the whole pack data
- pPack = m_pDataList->GetDataBuffer();
- if (pPack != NULL)
- {
- CopyMemory((PVOID)(pbBuffer + dwHaveRead),
- (PVOID)((PBYTE)(pPack) + m_ulPositionInPack), (SIZE_T)MPEG1_PACK_SIZE - m_ulPositionInPack);
- m_pDataList->ReleaseDataBuffer(pPack);
- dwHaveRead += MPEG1_PACK_SIZE - m_ulPositionInPack;
- m_ulPositionInPack = 0;
- }
- else if (m_pDataList->IsFlushing())
- {
- // FILE * fp = fopen("c:\log.txt","a+");
- // fprintf(fp,"failed!n");
- // fclose(fp);
- return E_FAIL;
- }
- else
- {
- // FILE * fp = fopen("c:\log.txt","a+");
- // fprintf(fp,"SLEEP!n");
- // fclose(fp);
- Sleep(10);
- }
- }
- else
- {
- // Copy part of the pack data
- pPack = m_pDataList->PointToDataHead();
- if (pPack != NULL)
- {
- m_ulPositionInPack = dwBytesToRead - dwHaveRead;
- CopyMemory((PVOID)(pbBuffer + dwHaveRead),
- (PVOID)(pPack), (SIZE_T)m_ulPositionInPack);
- dwHaveRead += m_ulPositionInPack;
- }
- else if (m_pDataList->IsFlushing())
- {
- // FILE * fp = fopen("c:\log.txt","a+");
- // fprintf(fp,"failed!n");
- // fclose(fp);
- return E_FAIL;
- }
- else
- {
- // FILE * fp = fopen("c:\log.txt","a+");
- // fprintf(fp,"SLEEP!n");
- // fclose(fp);
- Sleep(10);
- }
- }
- }
- *pdwBytesRead = dwBytesToRead;
- return S_OK;
- }
- LONGLONG Size(LONGLONG *pSizeAvailable)
- {
- *pSizeAvailable = m_llLength;
- return 0x7fffffffff;
- }
- DWORD Alignment()
- {
- return 1;
- }
- void Lock()
- {
- m_csLock.Lock();
- }
- void Unlock()
- {
- m_csLock.Unlock();
- }
- void AddAvailableLength(LONGLONG inLength)
- {
- m_llLength += inLength;
- }
- };
- class CMemReader : public CAsyncReader
- {
- public:
- // We're not going to be CoCreate'd so we don't need registration
- // stuff etc
- STDMETHODIMP Register()
- {
- return S_OK;
- }
- STDMETHODIMP Unregister()
- {
- return S_OK;
- }
- CMemReader(CMemStream *pStream, CMediaType *pmt, HRESULT *phr) :
- CAsyncReader(NAME("Mem Reader"), NULL, pStream, phr)
- {
- m_mt = *pmt;
- }
- };
- #endif // __MemFilter_h__