⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp_apdlg.cpp

📁 一个在Linux下开发的IP摄像头的驱动程序及控制软件
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	if (m_hServUtil)
		ServerUtl_Release(&m_hServUtil);

	ReleaseAudioCapture();
	AvSynchronizer_Release(&g_hAvSync);

	// step2. release DataBroker/AvSyhchronizer/DB
	for ( i = 0 ; i < g_nMaxChannel ; i ++ )
	{
		DataBroker_DeleteConnection(g_hDataBroker, &(g_Channel_Info[i].hConn));

		if (g_Channel_Info[i].hDataBrokerInput)
			DataBroker_DeleteInputEx(g_hDataBroker, &g_Channel_Info[i].hDataBrokerInput);
	}
	DataBroker_Release(&g_hDataBroker);

	return 0;
}

int CMP_APDlg::CreateAvSyncChannel(int nIndex)
{
	TCHANNELOPTION tChannelOptions;
	ZeroMemory(&tChannelOptions, sizeof(tChannelOptions));

	tChannelOptions.dwTopBorderSize = 20;
	tChannelOptions.dwBottomBorderSize = 0;
	tChannelOptions.dwLeftBorderSize = 0;
	tChannelOptions.dwRightBorderSize = 0;
	tChannelOptions.dwDisplayContext = g_Channel_Info[nIndex].dwContext;
	tChannelOptions.hDisplay = g_Channel_Info[nIndex].hDisplay;
	tChannelOptions.dwVolume = 50*g_Channel_Info[nIndex].dwVolume;
	tChannelOptions.bAudioOut = TRUE;
	tChannelOptions.bMotionAlert = TRUE;
	tChannelOptions.dwMotionRect = 0x80000000 | RGB(0, 255, 255);
	tChannelOptions.dwMotionRectAlert = 0;
	if (g_dwOrgPicCap != 0)
	{
		tChannelOptions.bOrgSizeCallback = TRUE;
		tChannelOptions.dwOrgSizeTopBorder = 20;
		tChannelOptions.dwOrgSizeBottomBorder = 20;
		tChannelOptions.dwOrgSizeLeftBorder = 10;
		tChannelOptions.dwOrgSizeRightBorder = 10;
	}
	tChannelOptions.hBMP = g_hBMP;
	tChannelOptions.dwFlags = CH_BITMAP|CH_MOTIONRECT | CH_MOTIONRECTALERT | CH_MOTION|CH_DISPLAYCONTEXT | CH_AUDIOOUT|CH_ALLBORDER | CH_VOLUME;

	char szBuf[MAX_PATH];
	
	SCODE sRet = AvSynchronizer_CreateChannel (g_hAvSync,
		&(g_Channel_Info[nIndex].hChannel),
		tChannelOptions );
	if (IS_FAIL(sRet))
	{
		sprintf(szBuf, "AvSynchronizer_CreateChannel failed with error code %X", sRet);
		MessageBox(szBuf, "Error", MB_OK);
		return -1;
	}

	AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_CLEAR_QUEUE, 2, 0);
	// set deblocking if any
	AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_DEBLOCKING, (DWORD) g_bDeblocking, 0);
	if (g_bDrawOnGraph)
		AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_FASTER_CAPTION_ONGRAPH, 
			TRUE, 0);

	if (g_nAVRawCCB != 0)
	{
		if (g_bAVRawCBOwnBuf)
		{
			// the max length for audio (AAC 1024 * 2 * 2)
			g_Channel_Info[nIndex].pbyAVCBAudioBuf = (BYTE *) malloc(4096);
			if (g_Channel_Info[nIndex].pbyAVCBAudioBuf == NULL)
			{
				MessageBox("Malloc error", "Error", MB_OK);
				return -1;
			}
			g_Channel_Info[nIndex].dwAVCBAudioBufSize = 4096;

			// the max length for video (1024 is for reserved header)
			g_Channel_Info[nIndex].pbyAVCBVideoBuf = (BYTE *) malloc(704 * 576 * 4 + 1024);
			if (g_Channel_Info[nIndex].pbyAVCBVideoBuf == NULL)
			{
				MessageBox("Malloc error", "Error", MB_OK);
				return -1;
			}
			g_Channel_Info[nIndex].dwAVCBVideoBufSize = 704 * 576 * 4 + 1024;
		}

		if (g_nAVRawCCB & 1)	// video
		{
			AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_OUTPUT_VIDEO_FRAME, (DWORD) TRUE, 0);
			// set the video format
			AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_DECODE_DATA_FORMAT, (DWORD) g_nAVRawDBAskType, 0);
			if (g_bAVRawCBOwnBuf)
				AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, 
					SETCH_DECODE_VIDEO_BUFFER, (DWORD) g_Channel_Info[nIndex].dwAVCBVideoBufSize, 
					(DWORD) g_Channel_Info[nIndex].pbyAVCBVideoBuf);
		}

		if (g_nAVRawCCB & 2)	// audio
		{
			AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_OUTPUT_AUDIO_FRAME, (DWORD) TRUE, 0);
			if (g_bAVRawCBOwnBuf)
				AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, 
					SETCH_DECODE_AUDIO_BUFFER, (DWORD) g_Channel_Info[nIndex].dwAVCBAudioBufSize, 
					(DWORD) g_Channel_Info[nIndex].pbyAVCBAudioBuf);
		}

		// set the callback function
		AvSynchronizer_SetChannelOption(g_Channel_Info[nIndex].hChannel, SETCH_DECODE_CB, 
			(DWORD) nIndex, (DWORD) AvSynchronizerAVCB);
	}

	return 0;
}

int CMP_APDlg::HandleAvSyncChannel(int nIndex)
{
	TUPDATECHANNELOPTION tUpdateChOpt;
	tUpdateChOpt.dwFlags = UPCH_MEDIATYPE;
	tUpdateChOpt.tMediaType = (EMEDIATYPE) g_Channel_Info[nIndex].dwMediaType;
	AvSynchronizer_UpdateChannelSettings(g_Channel_Info[nIndex].hChannel,
		tUpdateChOpt);

	g_Channel_Info[nIndex].chStatus = channelConnected;
	g_Channel_Info[nIndex].bConnecting = TRUE;
	g_Channel_Info[nIndex].nFrames = 0;
	g_Channel_Info[nIndex].dwLastFRTick = GetTickCount();

	return 0;
}
//=================================================================
//	Function   : Connect_Channel
//	Description: connect the focus channel (g_iFocus)
//				 
//	Parameters :                     
//	Return     : 
//=================================================================
int CMP_APDlg::Connect_Channel(int nChannel)
{
	static int nFirst = 1;
	char szBuf[MAX_PATH];
	// step1. create the connect_ok event
	if (g_Channel_Info[nChannel].chStatus != channelStopped)
		return -1;

    if (g_nAVRawCCB != 0)
	{
		mkdir("C:\\AVCBFile");
	}

	// step2. set connection options

	if (g_Channel_Info[nChannel].hConn == NULL)
	{
		SCODE sRet = DataBroker_CreateConnection(g_hDataBroker, &(g_Channel_Info[nChannel].hConn));
		if (IS_FAIL(sRet))
		{
			sprintf(szBuf, "DataBroker_CreateConnection for channel %d failed with error %X", g_iFocus + 1, sRet);
			MessageBox(szBuf, "Error", MB_OK);
			return -1;
		}
	}
	
	TDataBrokerConnectionOptions ConnectionOptions;
	ZeroMemory(&ConnectionOptions, sizeof(ConnectionOptions));
	ConnectionOptions.dwStatusContext = g_Channel_Info[nChannel].dwContext;
	ConnectionOptions.dwAVContext = g_Channel_Info[nChannel].dwContext;
	ConnectionOptions.pzIPAddr = g_Channel_Info[nChannel].chHostIP;
	ConnectionOptions.pzUID = g_Channel_Info[nChannel].chUserName;
	ConnectionOptions.pzPWD = g_Channel_Info[nChannel].chPassword;

	if (g_Channel_Info[nChannel].chServer_Type[2] == '3' &&
		strncmp(g_Channel_Info[nChannel].chServer_Type, "IP31x1-TLFN", 11) != 0)
	{
		if ( g_Channel_Info[nChannel].dwProtocol == 1 )
		{
			ConnectionOptions.dwProtocolType = eptTCP;
			ConnectionOptions.dwMediaType = emtAudio | emtVideo;
		}
		else if ( g_Channel_Info[nChannel].dwProtocol == 2 )
		{
			ConnectionOptions.dwProtocolType = eptUDP;
			ConnectionOptions.dwMediaType = emtAudio | emtVideo;
		}
		else if ( g_Channel_Info[nChannel].dwProtocol == 3 )
		{
			ConnectionOptions.dwProtocolType = eptHTTP;
			ConnectionOptions.dwMediaType = emtVideo;
		}
	}
	else if (g_Channel_Info[nChannel].chServer_Type[2] == '2')
	{
		ConnectionOptions.dwProtocolType = eptHTTP;
		ConnectionOptions.dwMediaType = emtVideo;
	}
	else
	{
		if ( g_Channel_Info[nChannel].dwWantMedia == 1 )
			ConnectionOptions.dwMediaType = emtVideo;
		else if ( g_Channel_Info[nChannel].dwWantMedia == 2 )
			ConnectionOptions.dwMediaType = emtAudio;
		else
			ConnectionOptions.dwMediaType = emtAudio | emtVideo;

		if ( g_Channel_Info[nChannel].dwProtocol == 1 )
		{
			ConnectionOptions.dwProtocolType = eptTCP;
		}
		else if ( g_Channel_Info[nChannel].dwProtocol == 2 )
		{
			ConnectionOptions.dwProtocolType = eptUDP;
		}
		else if ( g_Channel_Info[nChannel].dwProtocol == 3 )
		{
			ConnectionOptions.dwProtocolType = eptHTTP; // for test
		}
		else if ( g_Channel_Info[nChannel].dwProtocol == 10 ) 
		{
			ConnectionOptions.dwProtocolType = eptMULTICAST; // for test
		}
	}

	ConnectionOptions.wHttpPort = (WORD) g_Channel_Info[nChannel].dwHTTPPort;
	ConnectionOptions.wCam = (WORD) g_Channel_Info[nChannel].dwCamera;
	ConnectionOptions.pzServerType = g_Channel_Info[nChannel].chServer_Type;
	ConnectionOptions.dwFlags = eConOptProtocolAndMediaType | eConOptCam | eConOptHttpPort | eConOptStatusContext | eConOptAVContext;

	// test
	ConnectionOptions.dwFlags |= eConOptVSize|eConOptQuality;
	strcpy(ConnectionOptions.zVSize, "3");
	ConnectionOptions.dwQuality = 3;

	// for IP3135 only
	//if (!strncmp(g_Channel_Info[nChannel].chServer_Type, "PT6114", 6))
	if (g_Channel_Info[nChannel].chServer_Type[2] > '3' && g_Channel_Info[nChannel].chServer_Type[2] != '7')
	{
		ConnectionOptions.pfTx = DataBrokerTxCallBack;
		ConnectionOptions.dwTxContext = g_Channel_Info[nChannel].dwContext;
		ConnectionOptions.dwAudioEnc = mctG729A;
		ConnectionOptions.dwFlags |= eConOptTxCallback | eConOptTxContext | eConOptAudioEncCodec;
	}

	SCODE sRet = AvSynchronizer_StartChannel(g_Channel_Info[nChannel].hChannel, DRAW_CONNECTING);
	if (IS_FAIL(sRet))
	{
		sprintf(szBuf, "AvSynchronizer_StartChannel failed with error code %X", sRet);
		MessageBox(szBuf, "Error", MB_OK);
		return -1;
	}

	if (g_Channel_Info[nChannel].szUserUrl[0])
	{
		DataBroker_SetConnectionUrlsExtra(g_Channel_Info[nChannel].hConn,
			g_Channel_Info[nChannel].szUserUrl, g_Channel_Info[nChannel].szUserUrlExtra,
			NULL, NULL, NULL, NULL);
	}

	if (g_bAutoForceI)
	{
		DataBroker_SetConnectionExtraOption(g_Channel_Info[nChannel].hConn, eOptAutoForceI, TRUE,
			g_nForcePeriod);
	}


	SCODE v;
	{
		v = DataBroker_SetConnectionOptions(g_Channel_Info[nChannel].hConn, &ConnectionOptions);
		if ( v != S_OK)
		{
			char szBuf[MAX_PATH];
			sprintf(szBuf, "DataBroker_SetConnectionOptions error %X", v);
			MessageBox(szBuf);
			return -1;
		}
		DataBroker_SetConnectionExtraOption(g_Channel_Info[nChannel].hConn, eOptRtspCtrlPort, g_Channel_Info[nChannel].dwCtrlPort, 0);
		nFirst = 0;
	}

	// step3. Connect 
	g_Channel_Info[nChannel].chStatus = channelConnecting;
	v = DataBroker_Connect(g_Channel_Info[nChannel].hConn);
	if ( v != S_OK)
	{
		g_Channel_Info[nChannel].chStatus = channelStopped;
		MessageBox(" DataBroker_Connect error");
		return -1;
	}

	// for RTSP server
	g_Channel_Info[nChannel].dwMediaStart = 0;
	g_Channel_Info[nChannel].dwMediaEnd = 0;
	g_Channel_Info[nChannel].eRtspState = eRtspStNone;

	// step4. wait connect ok

	return 0;
}

void CMP_APDlg::ReleaseAvSyncChannel(int nIndex, BOOL bChangeToBMP)
{
	g_Channel_Info[nIndex].bConnecting = FALSE;
	if (g_Channel_Info[nIndex].hChannel)
	{
		AvSynchronizer_StopChannel(g_Channel_Info[nIndex].hChannel);

		if (bChangeToBMP)
		{
			TUPDATECHANNELOPTION tUpdateChOpt;
			tUpdateChOpt.dwFlags = UPCH_SHOW_BMP;
			AvSynchronizer_UpdateChannelSettings(g_Channel_Info[nIndex].hChannel,
				tUpdateChOpt);
		}
	}
	TRACE("AvSynchronizer_DeleteChannel -------------------->\n");

	g_Channel_Info[nIndex].nTimerDI = 0;
	g_Channel_Info[nIndex].chStatus = channelStopped;
	if (!m_bExit)
		PostMessage(WM_CHECK_UI, 0, 0);
}

//=================================================================
//	Function   : Disconnect_Channel
//	Description: disconnect the focus channel (nIndex)
//				 
//	Parameters :                     
//	Return     : 
//=================================================================
int CMP_APDlg::Disconnect_Channel(int nIndex, BOOL bBlock)
{	
	int nRelIdx = nIndex > -1 ? nIndex : g_iFocus;

	if (g_Channel_Info[nRelIdx].chStatus != channelConnected &&
		g_Channel_Info[nRelIdx].chStatus != channelConnecting)
		return -1;	

	TRACE("Disconnect_Channel go head %d\n", nIndex);

	g_Channel_Info[g_iFocus].nTxConn = 0;

	if (bBlock)
		g_Channel_Info[nRelIdx].bExitBlock = TRUE;
	else
		g_Channel_Info[nRelIdx].bExitBlock = FALSE;

	g_Channel_Info[nRelIdx].chStatus = channelDisconnecting;
	if (!m_bExit)
		PostMessage(WM_CHECK_UI, 0, 0);


	DataBroker_Disconnect(g_Channel_Info[nRelIdx].hConn);

	return 0;
}

int CMP_APDlg::StartRecord_Channel(void)
{	
	TMediaLocationOption tLocOption;
	
	memset(&tLocOption, 0, sizeof(tLocOption));
	tLocOption.pszLocation = g_Channel_Info[g_iFocus].chDBLocation;
	tLocOption.dwMaxFileSize = 1024 * 1024 * 300;
	tLocOption.dwMaxIndexNumber = 20240;
	tLocOption.dwPreBufferSecond = 7;
	tLocOption.dwDIMask = 1;
	g_Channel_Info[g_iFocus].hDBLocation = NULL;

	EnterCriticalSection(&g_CS[g_iFocus]);
	if (MediaDB_OpenLocation(g_hDatabase, &tLocOption, &(g_Channel_Info[g_iFocus].hDBLocation)) != S_OK)
		MediaDB_CreateLocation(g_hDatabase, &tLocOption, &(g_Channel_Info[g_iFocus].hDBLocation));	
	LeaveCriticalSection(&g_CS[g_iFocus]); 	
	g_Channel_Info[g_iFocus].bRecording = TRUE;
	
	return 0;
}

int CMP_APDlg::StopRecord_Channel(void)
{
	if ( (g_Channel_Info[g_iFocus].hDBLocation) != NULL )
	{
		MediaDB_CloseLocation(g_Channel_Info[g_iFocus].hDBLocation);
		g_Channel_Info[g_iFocus].hDBLocation = NULL;
	}

	g_Channel_Info[g_iFocus].bRecording = FALSE;	
	return 0;
}

int CMP_APDlg::Change_Status(void)
{
	//chStatus
	CString str1, str2, str3, str4, strTmp, str;
	char chTemp[8];
	str1.Format("%s%d\n%s",_T(" Focus Channel: "), g_iFocus + 1,_T(" Connecting Channels: ")); 
	str2.Format("\n%s",_T(" Recording Channels: ")); 

	m_nFRCount++;
	m_nFRCount %= 5;
	DWORD dwTickNow = GetTickCount();
	DWORD dwDiff;

	for ( int i = 0 ; i < g_nMaxChannel ; i ++ )
	{
		sprintf(chTemp,"%d ",i+1);
		if (g_Channel_Info[i].bConnecting == TRUE )
		{
			str1 += chTemp;
		}

		if (g_Channel_Info[i].bRecording == TRUE )
		{
			str2 += chTemp;
		}

		if (m_nFRCount == 0)
		{
			int nRate = 0;
			if (g_Channel_Info[i].bConnecting == TRUE)
			{
				dwDiff = dwTickNow - g_Channel_Info[i].dwLastFRTick;
				if (dwDiff > 0)
				{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -