2,543
社区成员
发帖
与我相关
我的任务
分享
UINT ReadMyFile(LPVOID lpParam)
{
// AfxMessageBox(L"Begin ReadMyFile");
CFile f;
PBYTE buf = new BYTE[32768];
int eof = 0;
f.Open(L"\\NAND\\QQ.mpg",CFile::modeRead);
while(1)//add buf
{
eof = f.Read(buf,32768);//get new data,you can change it,example used socket
if(eof!=32768)//if data finish
break;
Sleep(1);
while(Stream.AddBuf(buf)==-1)
{
Sleep(1);
}
}
f.Close();
delete [] buf;
return 0;
}
HANDLE hStartPlay = NULL;
HANDLE hProtectBuf = NULL;
#define PLAYBUFSIZE 32768*10
class CMemStream : public CAsyncStream
{
public:
CMemStream() :
m_llPosition(0),
m_BufSize(0),
m_dwKBPerSec(INFINITE)
{
hStartPlay = CreateEvent(NULL,TRUE,TRUE,NULL);
hProtectBuf = CreateMutex(NULL,FALSE,NULL);
m_PlayBuf = new BYTE[PLAYBUFSIZE];//自己定义的数据缓冲
}
HRESULT SetPointer(LONGLONG llPos)
{
return S_OK;
}
HRESULT Read(PBYTE pbBuffer,
DWORD dwBytesToRead,
BOOL bAlign,
LPDWORD pdwBytesRead)
{
CAutoLock lck(&m_csLock);
DWORD dwReadLength;
CString strDebug;
dwReadLength = dwBytesToRead;
while (32768>m_BufSize)
{
Sleep(1);
}
WaitForSingleObject(hProtectBuf,INFINITE);
CopyMemory((PVOID)pbBuffer,(PVOID)m_PlayBuf,dwReadLength);
strDebug.Format(L"m_BufSize:%d",m_BufSize);
OutputDebugString(strDebug);
m_BufSize -= dwReadLength; //未播放的数据大小减去刚刚播放的数据量dwReadLength
CopyMemory((PVOID)m_PlayBuf, (PVOID)(m_PlayBuf+dwReadLength),m_BufSize);
ReleaseMutex(hProtectBuf);
SetEvent(hStartPlay); //可以播放了
m_llPosition += dwReadLength;
*pdwBytesRead = dwReadLength;
return S_OK;
}
LONGLONG Size(LONGLONG *pSizeAvailable)
{
*pSizeAvailable = m_llLength;
return 0x7FFFFFFF;
}
DWORD Alignment()
{
return 1;
}
void Lock()
{
m_csLock.Lock();
}
void Unlock()
{
m_csLock.Unlock();
}
LONGLONG AddBuf(PBYTE buf)
{
if ((m_BufSize +32768)>32768*10)
{
return -1;
}
WaitForSingleObject(hProtectBuf,INFINITE);
CopyMemory((PVOID)(m_PlayBuf+m_BufSize),(PVOID)buf,32768);//把新的数据添加到缓冲中
m_BufSize += 32768;
ReleaseMutex(hProtectBuf);
return m_BufSize;
}
void FreeBuf()
{
delete [] m_PlayBuf;
}
private:
CCritSec m_csLock;
LONGLONG m_llLength;
LONGLONG m_llPosition;
DWORD m_dwKBPerSec;
DWORD m_dwTimeStart;
LONGLONG m_BufSize;
PBYTE m_PlayBuf;
};
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\0"), NULL, pStream, phr)
{
m_mt = *pmt;
}
};
class CMyStream
class CMySource :
public CSource,
public IFileSourceFilter
{
//..
static DWORD WINAPI RecvSampleThreadProc(LPVOID lpParameter); //
bool ProcSample(); //接收Sample后把Sample放进输出Pin的发送队列中
}
class CMyStream : public CSourceStream
{
// plots a ball into the supplied video frame
HRESULT FillBuffer(IMediaSample *pSamp); //重写这里进行sample的处理和传递
// Ask for buffers of the size appropriate to the agreed media type
HRESULT DecideBufferSize(IMemAllocator *pAlloc,
ALLOCATOR_PROPERTIES *pProperties);
// Resets the stream time to zero
HRESULT OnThreadCreate(void);
HRESULT OnThreadDestroy(void);
HRESULT OnThreadStartPlay(void);
void ResetTimestamps(IMediaSample *pSamp); //重设timestamps
}