观察者模式
2011年12月19日
class JPDecoder : public ISDD_Decoder
{
public:
JPDecoder();
virtual ~JPDecoder();
public:
//实现ISDD_Decoder接口
virtual bool Init();
virtual void Fini();
virtual void Release();
virtual void Reflesh();//刷新窗体
//流操作
virtual bool Open(void * argument, long size, long pool);
virtual void Close();
virtual bool InputData(void * buffer, unsigned long size, unsigned long type);
//控制
virtual bool StartPlay(HWND hwnd);
virtual bool StartPlay(HWND hwnd, PlaybackRecordCallback lpcbPlayback, char *pContext);
virtual void StopPlay();
virtual void Slow();
virtual void Fast();
virtual void Normal();
virtual void Pause();
virtual void Resume();
virtual void Jump(int post);
virtual void Snap(const char * pFilePath, PictureFormat ePictureFormat);
virtual int Vendor();
public:
//属性
void Vendor(int value); //设置厂商类型
void* GetFrameRatePara();//获取帧率回调参数
PlaybackRecordCallback m_pFrameProgress;//帧率回调
FrameRate* GetFrameStruct();
void SetFrameRateCallback(void *pPara);
static DWORD WINAPI GetTimeThread(void *pPara);
protected:
long m_jpNumber; //金鹏SDK函数里面的Number
HWND m_hPlayWnd; //播放窗口句柄
int m_vendor; //厂商类型
void *m_pContext;//设置帧率回调指针
FrameRate m_FrameRate;//帧率结构
friend class JPDecoderFactory;
};
DECLARE_DECODER_MODULE()
#endif
#include "jpdecoder.h"
#include "Ice_Entity.h"
#include
#include
// 金鹏DVRSDK头文件
#include "JPPlayApi.h"
/************************************************************************/
static std::vector *s_JPDecoderVec=NULL;//所有解码器对象
static bool s_bTimerRuning= false;//线程状态
static HANDLE s_hTimerThread= NULL;//时间线程
static DWORD s_dwThreadId= 0;//新线程Id
static CRITICAL_SECTION s_Section;//临界区对象
static BOOL s_bJPInit= FALSE; //是否已经初始化了
//金鹏SDK初始化操作
static BOOL JP_Init();
//金鹏SDK清理操作
static void JP_Fini();
//金鹏解码器创建操作
static ISDD_Decoder* JP_Create();
BEGIN_DECODER_MODULE()
DC_INIT(JP_Init)
DC_FINI(JP_Fini)
DC_CREATE(JP_Create)
END_DECODER_MODULE()
/************************************************************************/
static BOOL JP_Init()
{
s_bJPInit= TRUE;
s_bTimerRuning= true;
s_JPDecoderVec= new std::vector;
InitializeCriticalSection(&s_Section);
s_hTimerThread= CreateThread(NULL, 0, JPDecoder::GetTimeThread, NULL, 0, &s_dwThreadId);
return s_bJPInit;
}
static void JP_Fini()
{
//停止线程
s_bTimerRuning= false;
if(s_hTimerThread!=NULL)
{
WaitForSingleObject(s_hTimerThread, INFINITE);
CloseHandle(s_hTimerThread);
s_hTimerThread= NULL;
}
浪费大量CPU时间
//while(true)
//{
// DWORD dwExitCode= STILL_ACTIVE;
// // 判断线程的退出代码前要求线程对象还没有退出
// ::GetExitCodeThread(s_hTimerThread, &dwExitCode);
// if( dwExitCode != STILL_ACTIVE)
// {
// break;
// }
//}
DeleteCriticalSection(&s_Section);
//释放对象
for( std::vector::iterator it=s_JPDecoderVec->begin(); it!= s_JPDecoderVec->end(); ++it)
{
JPDecoder *pDecoder= dynamic_cast(*it);
if( pDecoder!=NULL)
{
pDecoder->m_pFrameProgress= NULL;
delete pDecoder;
pDecoder= NULL;
}
}
s_JPDecoderVec->clear();
delete s_JPDecoderVec;
s_JPDecoderVec= NULL;
}
static ISDD_Decoder* JP_Create()
{
JPDecoder *newDecoder= NULL;
if (!s_bJPInit)
{
return NULL;
}
newDecoder= new JPDecoder;
newDecoder->Vendor(cameraType_JP);
s_JPDecoderVec->push_back(newDecoder);
return newDecoder;
}
/************************************************************************/
JPDecoder::JPDecoder()
: m_hPlayWnd(NULL),
m_jpNumber(-1),
m_pFrameProgress(NULL),
m_pContext(NULL)
{
memset(&m_FrameRate, 0, sizeof(m_FrameRate));
}
JPDecoder::~JPDecoder()
{
}
void* JPDecoder::GetFrameRatePara()
{
return m_pContext;
}
FrameRate* JPDecoder::GetFrameStruct()
{
return &m_FrameRate;
}
bool JPDecoder::Init()
{
BOOL bRet= FALSE;
bRet= Player_InitialDirectDraw();
return (TRUE==bRet ) ;
}
void JPDecoder::Fini()
{
Player_ReleaseDirectDraw();
}
int JPDecoder::Vendor()
{
return m_vendor;
}
void JPDecoder::Vendor(int value)
{
m_vendor= value;
}
bool JPDecoder::Open(void * argument, long size, long pool)
{
if (TRUE != Player_OpenStreamEx(m_jpNumber, STREAMFILEMODE, 0))
{
return false;
}
// if (TRUE != Player_SetStreamOpenMode(m_jpNumber, STREAMFILEMODE))
// {
// return false;
// }
return true;
}
void JPDecoder::Close()
{
Player_CloseStream(m_jpNumber);
}
void JPDecoder::Release()
{
/*
由于换成了全局对象,要在Fini中释放
delete this;
*/
}
bool JPDecoder::InputData(void * buffer, unsigned long size, unsigned long type)
{
BOOL bRet= FALSE;
bRet= Player_InputData(m_jpNumber, (PBYTE)buffer, size) ;
return (TRUE==bRet);
}
bool JPDecoder::StartPlay(HWND hwnd)
{
BOOL bRet= FALSE;
bRet= Player_Play(m_jpNumber, hwnd) ;
if (bRet)
{
m_hPlayWnd= hwnd;
}
else
{
m_hPlayWnd= NULL;
}
return (TRUE==bRet);
}
bool JPDecoder::StartPlay(HWND hwnd, PlaybackRecordCallback lpcbPlayback, char *pContext)
{
//设置回调函数和回调函数参数
m_pFrameProgress= lpcbPlayback;
m_pContext= pContext;
BOOL bRet= FALSE;
bRet= Player_Play(m_jpNumber, hwnd);
if (bRet)
{
m_hPlayWnd= hwnd;
}
else
{
m_hPlayWnd= NULL;
}
return (TRUE==bRet);
}
void JPDecoder::StopPlay()
{
Player_Stop(m_jpNumber);
}
void JPDecoder::Reflesh()
{
Player_Refresh(m_jpNumber);
}
void JPDecoder::Slow()
{
Player_Slow(m_jpNumber);
}
void JPDecoder::Fast()
{
Player_Fast(m_jpNumber);
}
void JPDecoder::Normal()
{
if (m_hPlayWnd != NULL)
{
Player_Play(m_jpNumber, m_hPlayWnd);
}
}
void JPDecoder::Pause()
{
Player_Pause(m_jpNumber);
}
void JPDecoder::Resume()
{
Player_Play(m_jpNumber, m_hPlayWnd);
}
void JPDecoder::Jump(int pos)
{
}
void JPDecoder::Snap(const char * pFilePath, PictureFormat ePictureFormat)
{
char szFileName[256]= {0};
int nImgType= (int)ePictureFormat;
if( PF_JPG==nImgType )
{
sprintf(szFileName, "%s", pFilePath);
}
else if( PF_BMP==nImgType )
{
sprintf(szFileName, "%s", pFilePath);
}
Player_SnapPicture(m_jpNumber, szFileName );
}
/************************************************************************/
DWORD WINAPI JPDecoder::GetTimeThread(void *pPara)
{
while(s_bTimerRuning)
{
EnterCriticalSection(&s_Section);
Sleep(500);
for( std::vector::iterator it=s_JPDecoderVec->begin(); it!= s_JPDecoderVec->end(); ++it)
{
JPDecoder *pDecoder= dynamic_cast(*it);
if( pDecoder!=NULL)
{
pDecoder->SetFrameRateCallback(pDecoder);
}
}
LeaveCriticalSection(&s_Section);
}
return 0;
}
void JPDecoder::SetFrameRateCallback(void *pPara)
{
JPDecoder *pDecoder= (JPDecoder*)(pPara) ;
if(pDecoder==NULL)
{
return;
}
long lNumber= pDecoder->m_jpNumber;
static unsigned long lRenderTime= Player_GetPlayedTime(lNumber);
unsigned long lTimeStamp= Player_GetPlayedTime(lNumber)-lRenderTime;
unsigned long lFrameRate= Player_GetPlayedFrames(lNumber);
/*
static long lCircleCount= 0;
CString str;
lCircleCount++;
str.AppendFormat(L"%ld",ulFrameRate);
if( 0==(lCircleCount%20) )
OutputDebugString(str+ LPCTSTR("\n"));
else
OutputDebugString(str+ LPCTSTR(";"));
*/
if ( pDecoder->m_pFrameProgress!=NULL )
{
pDecoder->m_pFrameProgress( (char*)pDecoder->GetFrameRatePara(), lFrameRate ,lTimeStamp);
}
}
2011年12月19日
class JPDecoder : public ISDD_Decoder
{
public:
JPDecoder();
virtual ~JPDecoder();
public:
//实现ISDD_Decoder接口
virtual bool Init();
virtual void Fini();
virtual void Release();
virtual void Reflesh();//刷新窗体
//流操作
virtual bool Open(void * argument, long size, long pool);
virtual void Close();
virtual bool InputData(void * buffer, unsigned long size, unsigned long type);
//控制
virtual bool StartPlay(HWND hwnd);
virtual bool StartPlay(HWND hwnd, PlaybackRecordCallback lpcbPlayback, char *pContext);
virtual void StopPlay();
virtual void Slow();
virtual void Fast();
virtual void Normal();
virtual void Pause();
virtual void Resume();
virtual void Jump(int post);
virtual void Snap(const char * pFilePath, PictureFormat ePictureFormat);
virtual int Vendor();
public:
//属性
void Vendor(int value); //设置厂商类型
void* GetFrameRatePara();//获取帧率回调参数
PlaybackRecordCallback m_pFrameProgress;//帧率回调
FrameRate* GetFrameStruct();
void SetFrameRateCallback(void *pPara);
static DWORD WINAPI GetTimeThread(void *pPara);
protected:
long m_jpNumber; //金鹏SDK函数里面的Number
HWND m_hPlayWnd; //播放窗口句柄
int m_vendor; //厂商类型
void *m_pContext;//设置帧率回调指针
FrameRate m_FrameRate;//帧率结构
friend class JPDecoderFactory;
};
DECLARE_DECODER_MODULE()
#endif
#include "jpdecoder.h"
#include "Ice_Entity.h"
#include
#include
// 金鹏DVRSDK头文件
#include "JPPlayApi.h"
/************************************************************************/
static std::vector *s_JPDecoderVec=NULL;//所有解码器对象
static bool s_bTimerRuning= false;//线程状态
static HANDLE s_hTimerThread= NULL;//时间线程
static DWORD s_dwThreadId= 0;//新线程Id
static CRITICAL_SECTION s_Section;//临界区对象
static BOOL s_bJPInit= FALSE; //是否已经初始化了
//金鹏SDK初始化操作
static BOOL JP_Init();
//金鹏SDK清理操作
static void JP_Fini();
//金鹏解码器创建操作
static ISDD_Decoder* JP_Create();
BEGIN_DECODER_MODULE()
DC_INIT(JP_Init)
DC_FINI(JP_Fini)
DC_CREATE(JP_Create)
END_DECODER_MODULE()
/************************************************************************/
static BOOL JP_Init()
{
s_bJPInit= TRUE;
s_bTimerRuning= true;
s_JPDecoderVec= new std::vector;
InitializeCriticalSection(&s_Section);
s_hTimerThread= CreateThread(NULL, 0, JPDecoder::GetTimeThread, NULL, 0, &s_dwThreadId);
return s_bJPInit;
}
static void JP_Fini()
{
//停止线程
s_bTimerRuning= false;
if(s_hTimerThread!=NULL)
{
WaitForSingleObject(s_hTimerThread, INFINITE);
CloseHandle(s_hTimerThread);
s_hTimerThread= NULL;
}
浪费大量CPU时间
//while(true)
//{
// DWORD dwExitCode= STILL_ACTIVE;
// // 判断线程的退出代码前要求线程对象还没有退出
// ::GetExitCodeThread(s_hTimerThread, &dwExitCode);
// if( dwExitCode != STILL_ACTIVE)
// {
// break;
// }
//}
DeleteCriticalSection(&s_Section);
//释放对象
for( std::vector::iterator it=s_JPDecoderVec->begin(); it!= s_JPDecoderVec->end(); ++it)
{
JPDecoder *pDecoder= dynamic_cast(*it);
if( pDecoder!=NULL)
{
pDecoder->m_pFrameProgress= NULL;
delete pDecoder;
pDecoder= NULL;
}
}
s_JPDecoderVec->clear();
delete s_JPDecoderVec;
s_JPDecoderVec= NULL;
}
static ISDD_Decoder* JP_Create()
{
JPDecoder *newDecoder= NULL;
if (!s_bJPInit)
{
return NULL;
}
newDecoder= new JPDecoder;
newDecoder->Vendor(cameraType_JP);
s_JPDecoderVec->push_back(newDecoder);
return newDecoder;
}
/************************************************************************/
JPDecoder::JPDecoder()
: m_hPlayWnd(NULL),
m_jpNumber(-1),
m_pFrameProgress(NULL),
m_pContext(NULL)
{
memset(&m_FrameRate, 0, sizeof(m_FrameRate));
}
JPDecoder::~JPDecoder()
{
}
void* JPDecoder::GetFrameRatePara()
{
return m_pContext;
}
FrameRate* JPDecoder::GetFrameStruct()
{
return &m_FrameRate;
}
bool JPDecoder::Init()
{
BOOL bRet= FALSE;
bRet= Player_InitialDirectDraw();
return (TRUE==bRet ) ;
}
void JPDecoder::Fini()
{
Player_ReleaseDirectDraw();
}
int JPDecoder::Vendor()
{
return m_vendor;
}
void JPDecoder::Vendor(int value)
{
m_vendor= value;
}
bool JPDecoder::Open(void * argument, long size, long pool)
{
if (TRUE != Player_OpenStreamEx(m_jpNumber, STREAMFILEMODE, 0))
{
return false;
}
// if (TRUE != Player_SetStreamOpenMode(m_jpNumber, STREAMFILEMODE))
// {
// return false;
// }
return true;
}
void JPDecoder::Close()
{
Player_CloseStream(m_jpNumber);
}
void JPDecoder::Release()
{
/*
由于换成了全局对象,要在Fini中释放
delete this;
*/
}
bool JPDecoder::InputData(void * buffer, unsigned long size, unsigned long type)
{
BOOL bRet= FALSE;
bRet= Player_InputData(m_jpNumber, (PBYTE)buffer, size) ;
return (TRUE==bRet);
}
bool JPDecoder::StartPlay(HWND hwnd)
{
BOOL bRet= FALSE;
bRet= Player_Play(m_jpNumber, hwnd) ;
if (bRet)
{
m_hPlayWnd= hwnd;
}
else
{
m_hPlayWnd= NULL;
}
return (TRUE==bRet);
}
bool JPDecoder::StartPlay(HWND hwnd, PlaybackRecordCallback lpcbPlayback, char *pContext)
{
//设置回调函数和回调函数参数
m_pFrameProgress= lpcbPlayback;
m_pContext= pContext;
BOOL bRet= FALSE;
bRet= Player_Play(m_jpNumber, hwnd);
if (bRet)
{
m_hPlayWnd= hwnd;
}
else
{
m_hPlayWnd= NULL;
}
return (TRUE==bRet);
}
void JPDecoder::StopPlay()
{
Player_Stop(m_jpNumber);
}
void JPDecoder::Reflesh()
{
Player_Refresh(m_jpNumber);
}
void JPDecoder::Slow()
{
Player_Slow(m_jpNumber);
}
void JPDecoder::Fast()
{
Player_Fast(m_jpNumber);
}
void JPDecoder::Normal()
{
if (m_hPlayWnd != NULL)
{
Player_Play(m_jpNumber, m_hPlayWnd);
}
}
void JPDecoder::Pause()
{
Player_Pause(m_jpNumber);
}
void JPDecoder::Resume()
{
Player_Play(m_jpNumber, m_hPlayWnd);
}
void JPDecoder::Jump(int pos)
{
}
void JPDecoder::Snap(const char * pFilePath, PictureFormat ePictureFormat)
{
char szFileName[256]= {0};
int nImgType= (int)ePictureFormat;
if( PF_JPG==nImgType )
{
sprintf(szFileName, "%s", pFilePath);
}
else if( PF_BMP==nImgType )
{
sprintf(szFileName, "%s", pFilePath);
}
Player_SnapPicture(m_jpNumber, szFileName );
}
/************************************************************************/
DWORD WINAPI JPDecoder::GetTimeThread(void *pPara)
{
while(s_bTimerRuning)
{
EnterCriticalSection(&s_Section);
Sleep(500);
for( std::vector::iterator it=s_JPDecoderVec->begin(); it!= s_JPDecoderVec->end(); ++it)
{
JPDecoder *pDecoder= dynamic_cast(*it);
if( pDecoder!=NULL)
{
pDecoder->SetFrameRateCallback(pDecoder);
}
}
LeaveCriticalSection(&s_Section);
}
return 0;
}
void JPDecoder::SetFrameRateCallback(void *pPara)
{
JPDecoder *pDecoder= (JPDecoder*)(pPara) ;
if(pDecoder==NULL)
{
return;
}
long lNumber= pDecoder->m_jpNumber;
static unsigned long lRenderTime= Player_GetPlayedTime(lNumber);
unsigned long lTimeStamp= Player_GetPlayedTime(lNumber)-lRenderTime;
unsigned long lFrameRate= Player_GetPlayedFrames(lNumber);
/*
static long lCircleCount= 0;
CString str;
lCircleCount++;
str.AppendFormat(L"%ld",ulFrameRate);
if( 0==(lCircleCount%20) )
OutputDebugString(str+ LPCTSTR("\n"));
else
OutputDebugString(str+ LPCTSTR(";"));
*/
if ( pDecoder->m_pFrameProgress!=NULL )
{
pDecoder->m_pFrameProgress( (char*)pDecoder->GetFrameRatePara(), lFrameRate ,lTimeStamp);
}
}