📄 t2ps.cpp
字号:
} // NewSegment
//
// CT2ps class
//
CT2ps::CT2ps(LPUNKNOWN pUnk, HRESULT *phr) :
CUnknown(NAME("CT2ps"), pUnk),
m_pFilter(NULL),
m_pPin(NULL),
m_pPosition(NULL),
m_hFile(INVALID_HANDLE_VALUE),
m_pFileName(0),
m_fWriteError(0)
{
ASSERT(phr);
m_pFilter = new CT2psFilter(this, GetOwner(), &m_Lock, phr);
if (m_pFilter == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
return;
}
m_pPin = new CT2psInputPin(this,GetOwner(),
m_pFilter,
&m_Lock,
&m_ReceiveLock,
phr);
if (m_pPin == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
return;
}
}
//
// SetFileName
//
// Implemented for IFileSinkFilter support
//
STDMETHODIMP CT2ps::SetFileName(LPCOLESTR pszFileName,const AM_MEDIA_TYPE *pmt)
{
// Is this a valid filename supplied
CheckPointer(pszFileName,E_POINTER);
if(wcslen(pszFileName) > MAX_PATH)
return ERROR_FILENAME_EXCED_RANGE;
// Take a copy of the filename
m_pFileName = new WCHAR[1+lstrlenW(pszFileName)];
if (m_pFileName == 0)
return E_OUTOFMEMORY;
lstrcpyW(m_pFileName,pszFileName);
// Clear the global 'write error' flag that would be set
// if we had encountered a problem writing the previous T2ps file.
// (eg. running out of disk space).
m_fWriteError = FALSE;
// Create the file then close it
HRESULT hr = OpenFile();
CloseFile();
return hr;
} // SetFileName
//
// GetCurFile
//
// Implemented for IFileSinkFilter support
//
STDMETHODIMP CT2ps::GetCurFile(LPOLESTR * ppszFileName,AM_MEDIA_TYPE *pmt)
{
CheckPointer(ppszFileName, E_POINTER);
*ppszFileName = NULL;
if (m_pFileName != NULL)
{
*ppszFileName = (LPOLESTR)
QzTaskMemAlloc(sizeof(WCHAR) * (1+lstrlenW(m_pFileName)));
if (*ppszFileName != NULL)
{
lstrcpyW(*ppszFileName, m_pFileName);
}
}
if(pmt)
{
ZeroMemory(pmt, sizeof(*pmt));
pmt->majortype = MEDIATYPE_NULL;
pmt->subtype = MEDIASUBTYPE_NULL;
}
return S_OK;
} // GetCurFile
// Destructor
CT2ps::~CT2ps()
{
CloseFile();
delete m_pPin;
delete m_pFilter;
delete m_pPosition;
delete m_pFileName;
}
//
// CreateInstance
//
// Provide the way for COM to create a T2ps filter
//
CUnknown * WINAPI CT2ps::CreateInstance(LPUNKNOWN punk, HRESULT *phr)
{
ASSERT(phr);
CT2ps *pNewObject = new CT2ps(punk, phr);
if (pNewObject == NULL) {
if (phr)
*phr = E_OUTOFMEMORY;
}
return pNewObject;
} // CreateInstance
//
// NonDelegatingQueryInterface
//
// Override this to say what interfaces we support where
//
STDMETHODIMP CT2ps::NonDelegatingQueryInterface(REFIID riid, void ** ppv)
{
CheckPointer(ppv,E_POINTER);
CAutoLock lock(&m_Lock);
// Do we have this interface
if (riid == IID_IFileSinkFilter) {
return GetInterface((IFileSinkFilter *) this, ppv);
}
else if (riid == IID_IBaseFilter || riid == IID_IMediaFilter || riid == IID_IPersist) {
return m_pFilter->NonDelegatingQueryInterface(riid, ppv);
}
else if (riid == IID_IMediaPosition || riid == IID_IMediaSeeking) {
if (m_pPosition == NULL)
{
HRESULT hr = S_OK;
m_pPosition = new CPosPassThru(NAME("T2ps Pass Through"),
(IUnknown *) GetOwner(),
(HRESULT *) &hr, m_pPin);
if (m_pPosition == NULL)
return E_OUTOFMEMORY;
if (FAILED(hr))
{
delete m_pPosition;
m_pPosition = NULL;
return hr;
}
}
return m_pPosition->NonDelegatingQueryInterface(riid, ppv);
}
return CUnknown::NonDelegatingQueryInterface(riid, ppv);
} // NonDelegatingQueryInterface
//
// OpenFile
//
// Opens the file ready for T2psing
//
HRESULT CT2ps::OpenFile()
{
TCHAR *pFileName = NULL;
// Is the file already opened
if (m_hFile != INVALID_HANDLE_VALUE) {
return NOERROR;
}
// Has a filename been set yet
if (m_pFileName == NULL) {
return ERROR_INVALID_NAME;
}
// Convert the UNICODE filename if necessary
#if defined(WIN32) && !defined(UNICODE)
char convert[MAX_PATH];
if(!WideCharToMultiByte(CP_ACP,0,m_pFileName,-1,convert,MAX_PATH,0,0))
return ERROR_INVALID_NAME;
pFileName = convert;
#else
pFileName = m_pFileName;
#endif
// Try to open the file
m_hFile = CreateFile((LPCTSTR) pFileName, // The filename
GENERIC_WRITE, // File access
FILE_SHARE_READ, // Share access
NULL, // Security
CREATE_ALWAYS, // Open flags
(DWORD) 0, // More flags
NULL); // Template
if (m_hFile == INVALID_HANDLE_VALUE)
{
DWORD dwErr = GetLastError();
return HRESULT_FROM_WIN32(dwErr);
}
return S_OK;
} // Open
//
// CloseFile
//
// Closes any T2ps file we have opened
//
HRESULT CT2ps::CloseFile()
{
// Must lock this section to prevent problems related to
// closing the file while still receiving data in Receive()
CAutoLock lock(&m_Lock);
if (m_hFile == INVALID_HANDLE_VALUE) {
return NOERROR;
}
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE; // Invalidate the file
return NOERROR;
} // Open
//
// Write
//
// Write raw data to the file
//
HRESULT CT2ps::Write(PBYTE pbData, LONG lDataLength)
{
DWORD dwWritten;
// If the file has already been closed, don't continue
if (m_hFile == INVALID_HANDLE_VALUE) {
return S_FALSE;
}
if (!WriteFile(m_hFile, (PVOID)pbData, (DWORD)lDataLength,
&dwWritten, NULL))
{
return (HandleWriteFailure());
}
return S_OK;
}
HRESULT CT2ps::HandleWriteFailure(void)
{
DWORD dwErr = GetLastError();
if (dwErr == ERROR_DISK_FULL)
{
// Close the T2ps file and stop the filter,
// which will prevent further write attempts
m_pFilter->Stop();
// Set a global flag to prevent accidental deletion of the T2ps file
m_fWriteError = TRUE;
// Display a message box to inform the developer of the write failure
TCHAR szMsg[MAX_PATH + 80];
wsprintf(szMsg, TEXT("The disk containing T2ps file has run out of space, ")
TEXT("so the T2ps filter has been stopped.\r\n\r\n")
TEXT("You must set a new T2ps file name or restart the graph ")
TEXT("to clear this filter error."));
MessageBox(NULL, szMsg, TEXT("T2ps Filter failure"), MB_ICONEXCLAMATION);
}
return HRESULT_FROM_WIN32(dwErr);
}
//
// WriteString
//
// Writes the given string into the file
//
void CT2ps::WriteString(TCHAR *pString)
{
ASSERT(pString);
// If the file has already been closed, don't continue
if (m_hFile == INVALID_HANDLE_VALUE) {
return;
}
BOOL bSuccess;
DWORD dwWritten = 0;
DWORD dwToWrite = lstrlen(pString);
// Write the requested data to the T2ps file
bSuccess = WriteFile((HANDLE) m_hFile,
(PVOID) pString, (DWORD) dwToWrite,
&dwWritten, NULL);
if (bSuccess == TRUE)
{
// Append a carriage-return and newline to the file
const TCHAR *pEndOfLine = TEXT("\r\n\0");
dwWritten = 0;
dwToWrite = lstrlen(pEndOfLine);
bSuccess = WriteFile((HANDLE) m_hFile,
(PVOID) pEndOfLine, (DWORD) dwToWrite,
&dwWritten, NULL);
}
// If either of the writes failed, stop receiving data
if (!bSuccess || (dwWritten < dwToWrite))
{
HandleWriteFailure();
}
} // WriteString
////////////////////////////////////////////////////////////////////////
//
// Exported entry points for registration and unregistration
// (in this case they only call through to default implementations).
//
////////////////////////////////////////////////////////////////////////
//
// DllRegisterSever
//
// Handle the registration of this filter
//
STDAPI DllRegisterServer()
{
return AMovieDllRegisterServer2( TRUE );
} // DllRegisterServer
//
// DllUnregisterServer
//
STDAPI DllUnregisterServer()
{
return AMovieDllRegisterServer2( FALSE );
} // DllUnregisterServer
//
// DllEntryPoint
//
extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID);
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD dwReason,
LPVOID lpReserved)
{
return DllEntryPoint((HINSTANCE)(hModule), dwReason, lpReserved);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -