📄 videocommdlg.cpp
字号:
m_bIsMulticast = FALSE;
//begin video communication
//initialization
m_pAudioReceiveChannel->Initialize(this);
m_pVideoReceiveChannel->Initialize(this);
m_pDataReceiveChannel->Initialize(this);
m_pCommandReceiveChannel->Initialize(this);
m_pFileReceiveChannel->Initialize(this);
//加通道
m_pMultiplex->AddAChannel(m_pAudioSendChannel);
m_pDemultiplex->AddAChannel(m_pAudioReceiveChannel);
m_pMultiplex->AddAChannel(m_pVideoSendChannel);
m_pDemultiplex->AddAChannel(m_pVideoReceiveChannel);
m_pMultiplex->AddAChannel(m_pCommandSendChannel);
m_pDemultiplex->AddAChannel(m_pDataReceiveChannel);
m_pMultiplex->AddAChannel(m_pDataSendChannel);
m_pDemultiplex->AddAChannel(m_pCommandReceiveChannel);
m_pMultiplex->AddAChannel(m_pFileSendChannel);
m_pDemultiplex->AddAChannel(m_pFileReceiveChannel);
m_pG723Decoder->Initialize();
m_pG723Encoder->Initialize();
//get port rate setting
UpdateData();
//Save the port rate to registry
HKEY hKey = NULL;
DWORD dwDisposition = 0;
long re0 = ::RegCreateKeyEx(HKEY_CURRENT_USER, data_Set, NULL,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisposition);
if(re0 != ERROR_SUCCESS)//Open or create fail
MessageBox("无法打开注册表!");
else
{
//Query data about Port rate, video quant and this teminal's name
DWORD dwLength = sizeof(DWORD);
BYTE cPortRate[4];
DWORD dwType_1 = REG_DWORD;
::CopyMemory(cPortRate, &m_nPortRate, sizeof(m_nPortRate));
long re1 = ::RegSetValueEx(hKey, "PortRate", NULL, dwType_1, cPortRate, dwLength);
if(re1 != ERROR_SUCCESS)
MessageBox("错误:无法写入注册表!");
::RegCloseKey(hKey);
}
//begin the board at a specific rate
switch(m_nPortRate)
{
case 0:
m_pInterfaceToBoard->Begin(RATE_16K);break;
case 1:
m_pInterfaceToBoard->Begin(RATE_32K);break;
case 2:
m_pInterfaceToBoard->Begin(RATE_64K);break;
default:
m_pInterfaceToBoard->Begin();//begin with the default rate
}
m_pVideoCapture->SetCapSize();//注意顺序
m_pVideoCapture->InitCap(GetDlgItem(IDC_LOCALSCREEN)->m_hWnd,this);
m_pVideoCapture->StartCap();
m_pWaveIn->Begin((DWORD)this);
m_pWaveOut->Begin((DWORD)this);
m_pDisplay->Begin(&m_RemoteScreen);
SetTimer(DISPLAY_STATUS,StatusInternal,NULL);
m_bBegin=TRUE;
SetDlgItemText(IDC_BEGIN,"结束");
EndWaitCursor();
}
void CVideoCommDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if(nSBCode==SB_ENDSCROLL)
{
int nQuant=((CSliderCtrl*)pScrollBar)->GetPos();
//Save the port rate to registry
HKEY hKey = NULL;
DWORD dwDisposition = 0;
long re0 = ::RegCreateKeyEx(HKEY_CURRENT_USER, data_Set, NULL,
NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisposition);
if(re0 != ERROR_SUCCESS)//Open or create fail
MessageBox("无法打开注册表!");
else
{
//Query data about Port rate, video quant and this teminal's name
DWORD dwLength = sizeof(DWORD);
BYTE cQuant[4];
::CopyMemory(cQuant, &nQuant, sizeof(nQuant));
long re1 = ::RegSetValueEx(hKey, "VideoQuant", NULL, REG_DWORD, cQuant, dwLength);
if(re1 != ERROR_SUCCESS)
MessageBox("错误:无法写入注册表!");
::RegCloseKey(hKey);
}
if(m_pCommandSendChannel)
{
BYTE byTemp=(BYTE)nQuant;
m_pCommandSendChannel->SendData(&byTemp,1);
byTemp=255;
m_pCommandSendChannel->SendData(&byTemp,1);
}
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CVideoCommDlg::OnMove(int x, int y)
{
CDialog::OnMove(x, y);
// TODO: Add your message handler code here
if(m_pVideoCapture)
::InvalidateRect(m_pVideoCapture->m_hWndCap,NULL,TRUE);
}
void CVideoCommDlg::ChannelToAudioDec(BYTE * pFrame, int nSize)
{
m_pAudioDecodeBuffer->AppendTail(pFrame,nSize);
}
void CVideoCommDlg::ChannelToVideoDec(BYTE * pFrame, int nSize)
{
if(m_bVideoCRC)
{
CCheckError CheckError;
if(!(CheckError.IsDataRigth(pFrame, nSize, pFrame, &nSize)))//not right crc
{
TRACE("Video crc error!\n");
return;
}
}
m_uFrameCount++;
m_pVideoDecodeBuffer->AddTail(CPacket(pFrame,nSize));
}
void CVideoCommDlg::ChannelToCommandDec(BYTE * pFrame, int nSize)
{
if(pFrame[0]>=1&&pFrame[0]<=31)
m_nQuant=pFrame[0];
}
void CVideoCommDlg::DataServiceToChannel(BYTE* pContent, UINT nLength)
{
if(m_pDataSendChannel)
m_pDataSendChannel->SendData(pContent, nLength);
}
int CVideoCommDlg::ChannelToDataService(BYTE* pFrame, int nLength)
{
if(m_pDataComm)
m_pDataComm->WriteCommBlock(pFrame, nLength);
//Output data
//char SendMsg[MAXSERIALDATALEN];
//VARIANT SendMsg;
//ByteToStr(pFrame, SendMsg, nLength);//pFrame->Cstring
///SendMsg.vt = VT_UI1 | VT_BYREF;
//CopyMemory(SendMsg.pbVal, pFrame, nLength);
//CByteArray cByte;
//for(int i=0; i<nLength; i++)
// cByte.Add(pFrame[i]);
//m_CommCtr.SetOutput(COleVariant(cByte));
return 0;
}
int CVideoCommDlg::AudioDecToWaveOut(char * pWave, int nSize)
{
int nLength;
if(m_pAudioDecodeBuffer->DetachHeader(m_pInputAudio,&nLength))
{
return m_pG723Decoder->Decode(m_pInputAudio,nLength,pWave);
}
else
return 0;
}
int CVideoCommDlg::WaveInToAudioEnc(char * pWave, int nSize)
{
extern BOOL bPause;
if(!m_bVideoCRC)
{
if(bPause)
{
m_pAudioSendChannel->ClearChannelData();
return 0;
}
}
int nLength=m_pG723Encoder->Encode(pWave,nSize,m_pOutputAudio);
if(m_pAudioSendChannel->SendData((BYTE*)m_pOutputAudio,nLength))
return nSize;
else
return 0;
}
void CVideoCommDlg::VideoToChannel(BYTE * pContent, UINT nLength)
{
static int nQuant=m_nQuant;
extern BOOL bPause;
if(!m_bVideoCRC)
{
if(bPause)
return;
}
if(!m_bBegin)
return;
if(m_bCreating)
return;
if(m_bDeleting)
return;
int nBufferSize=384;
if(nQuant!=m_nQuant)
{
m_pH263Encoder->SetQuant(m_nQuant);
nQuant=m_nQuant;
}
if(m_pVideoSendChannel->GetLength()>nBufferSize)
return;
int nEncodedLength=m_pH263Encoder->EncodeAFrame(pContent,nLength);
if(nEncodedLength)
{
if(m_bVideoCRC)
{
CCheckError CheckError;
CheckError.CaculateCRC(m_pH263Encoder->CompressData,nEncodedLength,m_pH263Encoder->CompressData,&nEncodedLength);
}
if(!m_pVideoSendChannel->SendData(m_pH263Encoder->CompressData,nEncodedLength))
{
m_pH263Encoder->ForceAIntraFrame();
return;
}
}
}
BOOL CVideoCommDlg::CreateObject()
{
m_bCreating=TRUE;
Sleep(500);
m_pAudioDecodeBuffer=new CLoopElementBuffer<26,10>;
m_pVideoDecodeBuffer=new CSafeList<CPacket>;
m_pAudioReceiveChannel=new CReceiveChannel(MT_AUDIO,8,24,AudioReceiveChannelCallBack);
m_pAudioSendChannel=new CSendChannel(MT_AUDIO,8,24);
m_pVideoReceiveChannel=new CReceiveChannel(MT_VIDEO,1,5120,VideoReceiveChannelCallBack);
m_pVideoSendChannel=new CSendChannel(MT_VIDEO,1,48);
m_pDataReceiveChannel=new CReceiveChannel(MT_DATA,1,256,DataReceiveChannelCallBack);
m_pDataSendChannel=new CSendChannel(MT_DATA,1,256);
m_pCommandReceiveChannel=new CReceiveChannel(MT_COMMAND,1,MAXCOMMANDFRAMELENGTH,CommandReceiveChannelCallBack);
m_pCommandSendChannel=new CSendChannel(MT_COMMAND,1,MAXCOMMANDFRAMELENGTH);
g_pCommandSendChannel = m_pCommandSendChannel;
m_pFileReceiveChannel=new CReceiveChannel(MT_FILEDATA,1,1024,FileReceiveChannelCallBack);
m_pFileSendChannel=new CSendChannel(MT_FILEDATA,1,1024);
/*
m_pAudioReceiveChannel=new CReceiveChannel(1,8,24,AudioReceiveChannelCallBack);
m_pAudioSendChannel=new CSendChannel(1,8,24);
m_pVideoReceiveChannel=new CReceiveChannel(0,1,5120,VideoReceiveChannelCallBack);
m_pVideoSendChannel=new CSendChannel(0,1,48);
m_pDataReceiveChannel=new CReceiveChannel(3,1,16,DataReceiveChannelCallBack);
m_pDataSendChannel=new CSendChannel(3,1,16);
m_pCommandReceiveChannel=new CReceiveChannel(2,1,8,CommandReceiveChannelCallBack);
m_pCommandSendChannel=new CSendChannel(2,1,8);
*/
m_pDemultiplex=new CDemultiplex(MAXFRAMELENGTH);
m_pMultiplex=new CMultiplex;
m_pWaveOut=new CWaveOut(WaveOutCallBack);
m_pWaveIn=new CWaveIn(WaveInCallBack);
m_pVideoCapture=new CCapture(VideoCapCallBack);
m_pInterfaceToBoard=new CTheInterfaceToBoard(m_pMultiplex,m_pDemultiplex);
m_pDisplay=new CH263Displayer(m_pVideoDecodeBuffer,SF_QCIF);
m_pDataComm = new CDataComm((DWORD)this, DataInCallBack);
m_pFileTransmit = new CFileTransmit((DWORD)this, FileDataSendCallBack);
m_pG723Decoder=new CG723Decoder;
m_pG723Encoder=new CG723Encoder;
m_pH263Encoder=new CH263Encoder;
//m_pcTPortStatus = new CPortStatus;
m_TPortStatus.m_bAddConference = FALSE;
m_TPortStatus.m_bOwnToken = FALSE;
m_TPortStatus.m_bRequestToken = FALSE;
m_TPortStatus.m_bRequestTokenRefused = FALSE;
m_TPortStatus.m_byThisPortWatched=0;
for(int j=0; j<MAXPORTNUMBER;j++)
m_TPortStatus.m_byWatchingThisPort[j]=0;
m_TPortStatus.m_bWantWatch=FALSE;
m_TPortStatus.m_byThisPortWatching=0;
m_TPortStatus.m_bWantWatchRefused=FALSE;
m_bCreating=FALSE;
return TRUE;
}
void CVideoCommDlg::DeleteObject()
{
m_bDeleting=TRUE;
Sleep(1500);
delete m_pAudioDecodeBuffer;
delete m_pVideoDecodeBuffer;
delete m_pAudioReceiveChannel;
delete m_pAudioSendChannel;
delete m_pVideoReceiveChannel;
delete m_pVideoSendChannel;
delete m_pDataReceiveChannel;
delete m_pDataSendChannel;
delete m_pCommandReceiveChannel;
delete m_pCommandSendChannel;
delete m_pFileReceiveChannel;
delete m_pFileSendChannel;
delete m_pDemultiplex;
delete m_pMultiplex;
delete m_pWaveOut;
delete m_pWaveIn;
delete m_pVideoCapture;
delete m_pInterfaceToBoard;
delete m_pDataComm;
delete m_pFileTransmit;
delete m_pDisplay;
delete m_pG723Decoder;
delete m_pG723Encoder;
delete m_pH263Encoder;
//delete m_pcTPortStatus;
m_pAudioDecodeBuffer=NULL;
m_pVideoDecodeBuffer=NULL;
m_pAudioReceiveChannel=NULL;
m_pAudioSendChannel=NULL;
m_pVideoReceiveChannel=NULL;
m_pVideoSendChannel=NULL;
m_pDataReceiveChannel=NULL;
m_pDataSendChannel=NULL;
m_pCommandReceiveChannel=NULL;
m_pCommandSendChannel=NULL;
m_pFileReceiveChannel=NULL;
m_pFileSendChannel=NULL;
m_pDemultiplex=NULL;
m_pMultiplex=NULL;
m_pWaveOut=NULL;
m_pWaveIn=NULL;
m_pVideoCapture=NULL;
m_pInterfaceToBoard=NULL;
m_pDataComm = NULL;
m_pFileTransmit = NULL;
m_pDisplay=NULL;
m_pG723Decoder=NULL;
m_pG723Encoder=NULL;
m_pH263Encoder=NULL;
//m_pcTPortStatus=NULL;
m_bDeleting=FALSE;
}
const BYTE TRequestAddConference=32;//Must same as MCU define
const BYTE TGetAddConferenceAck=33;
const BYTE TGetTerminalHaveAddedConference=34;
const BYTE TRequestToken=35;
const BYTE TOwnToken=36;
const BYTE TRequestTokenRefuse=37;
const BYTE TRequestReleaseToken=49;
const BYTE TGetReleaseTokenAck=39;
const BYTE TWantWatch=40;//40,T
const BYTE TGetWantWatchAck=41;
const BYTE TGetWantWatchRefuse=42;
const BYTE TStopWatch=43;
const BYTE TGetStopWatchAck=44;
const BYTE TRequestExitConference=45;
const BYTE TGetExitConferenceAck=46;
//Only broadcasting user can select want watch, so if he don't watch, we stop
const BYTE TSomeoneWantWatchMe=47;
const BYTE TSomeoneStopWatchMe=48;
const BYTE TWhoOwnToken=60;
const BYTE TLhfOwnToken=61;
const BYTE TLzgOwnToken=62;
const BYTE TWhoReleaseToken=63;
const BYTE TLhfReleaseToken=64;
const BYTE TLzgReleaseToken=65;
const BYTE TLzgExitConference=66;
const BYTE TLhfExitConference=67;
const BYTE TWhoExitConference=68;
/*
void CVideoCommDlg::OnCommandAdd() //Add conference
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
byFrame[0] = TRequestAddConference;//1
byFrame[1] = m_TPortStatus.GetPortNameLength();//1
CopyMemory(byFrame+2, m_TPortStatus.GetPortName(), byFrame[1]);
byCILength = 2 + byFrame[1];
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
//After add to conference, enable this button
GetDlgItem(IDC_COMMANDADD)->EnableWindow(FALSE);
GetDlgItem(IDC_COMMANDBROADCAST)->EnableWindow(TRUE);
GetDlgItem(IDC_COMMANDEXIT)->EnableWindow(TRUE);
}
void CVideoCommDlg::OnCommandBroadcast() //Request broadcast
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
byFrame[0] = TRequestToken;//1
byCILength = 1;
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
GetDlgItem(IDC_COMMANDBROADCAST)->EnableWindow(FALSE);
GetDlgItem(IDC_COMMANDSTOP)->EnableWindow(TRUE);
}
void CVideoCommDlg::OnCommandStop() //Release token
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
byFrame[0] = TRequestReleaseToken;//1
byCILength = 1;
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
GetDlgItem(IDC_COMMANDBROADCAST)->EnableWindow(TRUE);
GetDlgItem(IDC_COMMANDSTOP)->EnableWindow(FALSE);
}
void CVideoCommDlg::OnCommandExit() //Exit conference
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
byFrame[0] = TRequestExit;//1
byCILength = 1;
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
GetDlgItem(IDC_COMMANDADD)->EnableWindow(TRUE);
GetDlgItem(IDC_COMMANDBROADCAST)->EnableWindow(FALSE);
GetDlgItem(IDC_COMMANDEXIT)->EnableWindow(FALSE);
}
void CVideoCommDlg::OnCommandWatchOne() //This terminal want to watch some other one
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
int nUserSel = 0;
CString SelName;
BYTE byPortNumber = 0;
byFrame[0] = TWantWatch;//1
//想看的终端号, 所以终端必须能知道名称对应的端口号, 需要MCU传来
nUserSel = m_NameInConference.GetCurSel();
m_NameInConference.GetText(nUserSel, SelName);
//根据名称查找对应的端口号
for(int i=0; i<m_nTotalAddTerminalCount; i++)
{
if(NameNumber[i].Name == SelName)
{
byPortNumber = NameNumber[i].Number;
break;
}
}
byFrame[1] = byPortNumber;//1
byCILength = 2;
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
GetDlgItem(IDC_COMMANDWATCHONE)->EnableWindow(FALSE);
GetDlgItem(IDC_COMMANDSTOPWATCH)->EnableWindow(TRUE);
}
void CVideoCommDlg::OnCommandStopWatch()
{
// TODO: Add your control notification handler code here
BYTE byFrame[MAXCOMMANDFRAMELENGTH];
int byCILength = 0;
byFrame[0] = TStopWatch;//1
byCILength = 1;
CommandCallBack(byFrame, byCILength, FS_VALID);//Send
GetDlgItem(IDC_COMMANDWATCHONE)->EnableWindow(TRUE);
GetDlgItem(IDC_COMMANDSTOPWATCH)->EnableWindow(FALSE);
}
*/
//When receiving a new c&i, call this function
//pFrame-------C&I saved in here
//nLength------C&I's length
//bFrameState--This frame is valid
//LPVOID-------Some other data
//这一段程序可能需要加入同步程序
BOOL bPause = FALSE;
void CVideoCommDlg::CommandCallBack(BYTE* pFrame,int nLength,BOOL bFrameState, int nFromWhere)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -