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

📄 consolesample.c

📁 一个在Linux下开发的IP摄像头的驱动程序及控制软件
💻 C
📖 第 1 页 / 共 5 页
字号:
			if (g_Channel_Info[index].chStatus == channelConnecting)
			{
				OnConnectError(index, connErrAuth);
			}
			break;
		case eOnStartMediaChannel:
			break;
		case eOnChannelClosed:
			break;
		case eOnTimeout:
			if ((int) pvParam1 == eAudioChannel)
				printf("Timeout for audio\n");
			else
				printf("Timeout for video\n");
			return S_OK;
		case eOnProtocolChanged:
			break;
		case eOnPacketLoss:
			{
				DWORD type=(DWORD) pvParam2;

				printf("eOnPacketLoss for %s...\n", type==1?"video":"audio");
			}
			
			break;
		case eOnDiDo:
			{
				if (g_Channel_Info[index].chServer_Type[0])
				{
					TSrvDepResource_ServerInfo srvInfo;
					DWORD dwlParam = (DWORD) pvParam2;
					WORD wDIVal, wDOVal;
					int nFlag = 1, nCnt;
					char szBuf[256], szTemp[256];

					SrvDepResource_GetParamForServerByFriendlyName(g_Channel_Info[index].chServer_Type, &srvInfo);

					printf("DI/DO Val = %X(DI num = %d, DO num = %d)\n", dwlParam,
						srvInfo.dwDiNum, srvInfo.dwDoNum);
					szBuf[0] = 0;
					wDIVal = (WORD) (dwlParam & 0xFFFF);
					wDOVal = (WORD) (dwlParam >> 16);
					for (nCnt = 0; nCnt < (int) srvInfo.dwDiNum; nCnt++, nFlag <<= 1)
					{
						if (wDIVal & nFlag)
							strcpy(szTemp, "H");
						else
							strcpy(szTemp, "L");

						if (nCnt > 0)
							strcat(szBuf, ", ");
						strcat(szBuf, szTemp);
					}
					printf("DI(s): %s\n", szBuf);

					szBuf[0] = 0;
					nFlag = 1;
					for (nCnt = 0; nCnt < (int) srvInfo.dwDoNum; nCnt++, nFlag <<= 1)
					{
						if (wDOVal & nFlag)
							strcpy(szTemp, "1");
						else
							strcpy(szTemp, "0");

						if (nCnt > 0)
							strcat(szBuf, ", ");
						strcat(szBuf, szTemp);
					}

					printf("DO(s): %s\n", szBuf);
				}
				else
					printf("DI/DO changed\n");
			}
			break;
		case eOnLocationChanged:
			strcpy(g_Channel_Info[index].chLocation,(char *)pvParam1);
			printf("Location changed to %s\n", g_Channel_Info[index].chLocation);
			break;
		case eOnInputInfo:
			break;
		case eOnOtherError:
			if (g_Channel_Info[index].chStatus == channelConnecting)
			{
				if ((SCODE) pvParam1 == DATABROKER_E_HTTP_READ_ERROR)
					OnConnectError(index, connErrHttpRead);
				else if ((SCODE) pvParam1 == DATABROKER_E_HTTP_CONNECT_FAILED)
					OnConnectError(index, connErrConnFailed);
				else if ((SCODE) pvParam1 == DATABROKER_E_OUT_OF_MEMORY)
					OnConnectError(index, connErrOutMemory);
			}

			break;
		case eOnVideoSignalChange:
			OnSignalChanged(index, (long) pvParam1);
			break;
		case eOnStopped:
			printf("eOnStopped\n");
			g_Channel_Info[index].chStatus = channelStopped;

			if (g_Channel_Info[index].bExitBlock)
				//SetEvent(g_ExitWait[index]);
				g_bExitFlag[index] = TRUE;

			break;
		default:
			break;
	}

	return S_OK;
}

SCODE __stdcall DataBrokerInputStatusCallBack(DWORD dwContext, TDataBrokerStatusType tStatusType, PVOID pvParam1, PVOID pvParam2)
{
	
	int index;
	index=(int) dwContext;

	switch ( tStatusType )
	{
		case eOnPacketLoss:
			{
				DWORD type=(DWORD) pvParam2;

				printf("(Input) eOnPacketLoss for %s...\n", type==1?"video":"audio");
			}
			break;
		case eOnDiDo:
			{
				if (g_Channel_Info[index].chServer_Type[0])
				{
					TSrvDepResource_ServerInfo srvInfo;
					DWORD dwlParam = (DWORD) pvParam2;
					WORD wDIVal, wDOVal;
					int nFlag = 1, nCnt;
					char szBuf[256], szTemp[256];

					SrvDepResource_GetParamForServerByFriendlyName(g_Channel_Info[index].chServer_Type, &srvInfo);

					printf("DI/DO Val = %X(DI num = %d, DO num = %d)\n", dwlParam,
						srvInfo.dwDiNum, srvInfo.dwDoNum);
					szBuf[0] = 0;
					wDIVal = (WORD) (dwlParam & 0xFFFF);
					wDOVal = (WORD) (dwlParam >> 16);
					for (nCnt = 0; nCnt < (int) srvInfo.dwDiNum; nCnt++, nFlag <<= 1)
					{
						if (wDIVal & nFlag)
							strcpy(szTemp, "H");
						else
							strcpy(szTemp, "L");

						if (nCnt > 0)
							strcat(szBuf, ", ");
						strcat(szBuf, szTemp);
					}
					printf("DI(s): %s\n", szBuf);

					szBuf[0] = 0;
					nFlag = 1;
					for (nCnt = 0; nCnt < (int) srvInfo.dwDoNum; nCnt++, nFlag <<= 1)
					{
						if (wDOVal & nFlag)
							strcpy(szTemp, "1");
						else
							strcpy(szTemp, "0");

						if (nCnt > 0)
							strcat(szBuf, ", ");
						strcat(szBuf, szTemp);
					}

					printf("DO(s): %s\n", szBuf);
				}
				else
					printf("DI/DO changed\n");
			}
			break;
		case eOnLocationChanged:
			strcpy(g_Channel_Info[index].chLocation,(char *)pvParam1);
			printf("Location changed to %s\n", g_Channel_Info[index].chLocation);
			break;
		case eOnInputInfo:
			printf("eOnInputInfo\n");
			break;
		case eOnVideoSignalChange:
			{
				char szBuf[MAX_PATH];

				printf(szBuf, "Channel %d Signal is changed to %s", index + 1,
					(int) pvParam1==eSignalRestored?"ON":"OFF");
			}
			break;
		default:
			break;
	}
	return S_OK;
}


int Initial_Module(void)
{
	// step1. Initial DB
	SCODE sRet;
	int i;
	DWORD dwExtra = 0;
#ifndef _WIN32
	pthread_mutexattr_t muAttr;
#endif // _WIN32

	sRet = DataBroker_Initial(&g_hDataBroker, g_nMaxChannel * 2, DataBrokerStatusCallBack,DataBrokerAVCallBack,
		mctJPEG | mctH263 | mctMP4V | mctG7221 | mctG729A | mctAAC | mctGAMR, 0,
		DATABROKER_VERSION);
	if (IS_FAIL(sRet))
	{
		printf("DataBroker_Initial failed with error %X\n", sRet);
		return -1;
	}

#ifdef _AVSYNC
	dwExtra |= DECODER_CHANNEL_ONLY;
	sRet = AvSynchronizer_InitialEx(&g_hAvSync, NULL,
		NULL, NULL, AUDIOOUT_FOCUS_NORMAL, 0, AVSYNCHRONIZER_VERSION, 
		dwExtra, 0x0FFFFFF);
	if (IS_FAIL(sRet))
	{
		printf("AvSynchronizer_Initial failed with error %X", sRet);
		return -1;
	}
#endif // _AVSYNC


#ifndef _WIN32
    pthread_mutexattr_init(&muAttr);
    pthread_mutexattr_settype(&muAttr, PTHREAD_MUTEX_RECURSIVE_NP);
#endif // _WIN32

	for ( i=0 ; i < g_nMaxChannel ; i ++ )
	{
#ifdef _WIN32
		InitializeCriticalSection(&g_CS[i]);
#else
		pthread_mutex_init(&g_CS[i], &muAttr);
#endif // _WIN32
		sRet = DataBroker_CreateConnection(g_hDataBroker,&(g_Channel_Info[i].hConn));
		if (IS_FAIL(sRet))
		{
			printf("DataBroker_CreateConnection for channel %d failed with error %X\n", i + 1, sRet);
#ifndef _WIN32
		    pthread_mutexattr_destroy(&muAttr);
#endif // _WIN32
			return -1;
		}

		if (g_Channel_Info[i].bNetPacketCallback)
		{
			sRet = DataBroker_CreateInputEx(g_hDataBroker, &g_Channel_Info[i].hDataBrokerInput);
			if (IS_FAIL(sRet))
			{
				printf("DataBroker_CreateInputEx for channel %d failed with error %X", i + 1, sRet);
#ifndef _WIN32
			    pthread_mutexattr_destroy(&muAttr);
#endif // _WIN32
				return -1;
			}
		}

#ifdef _AVSYNC
		{
			TDECCHOPTION tDecChannelOptions ;

			memset(&tDecChannelOptions, 0, sizeof(tDecChannelOptions));
			tDecChannelOptions.pfDecodeFrame = AvSynchronizerDecodeCallback;
			tDecChannelOptions.dwAvDecodeContext = g_iFocus;
			tDecChannelOptions.dwRawDataFormat = PF_BMP24;
			sRet = AvSynchronizer_CreateDecoderChannel (g_hAvSync, 
				&g_Channel_Info[g_iFocus].hDecoderChannel, 
				&tDecChannelOptions);
			if (sRet != S_OK)
			{
				printf("AvSynchronizer_CreateDecoderChannel failed with error: %X", sRet);
				return -1;
			}

			g_Channel_Info[g_iFocus].dwVideoBuffSize = 704 * 576 * 4 + 1000;
			g_Channel_Info[g_iFocus].pbyVideoBuff = (BYTE *) malloc(g_Channel_Info[g_iFocus].dwVideoBuffSize);
			if (g_Channel_Info[g_iFocus].pbyVideoBuff == NULL)
			{
				printf("malloc failed\n");
				return -1;
			}

			g_Channel_Info[g_iFocus].dwAudioBuffSize = 20 * 2048;
			g_Channel_Info[g_iFocus].pbyAudioBuff = (BYTE *) malloc(g_Channel_Info[g_iFocus].dwAudioBuffSize);
			if (g_Channel_Info[g_iFocus].pbyAudioBuff == NULL)
			{
				printf("malloc failed\n");
				return -1;
			}

			AvSynchronizer_SetChannelOption(g_Channel_Info[g_iFocus].hDecoderChannel, 
				SETCH_DECODE_DATA_FORMAT, (DWORD) g_eAVRawType, 0);

#endif // _AVSYNC
		}

	}

#ifndef _WIN32
    pthread_mutexattr_destroy(&muAttr);
#endif // _WIN32

	return 0;
}

int Release_Module(void)
{
	// step1. delete connection of DataBroker & close location of DB
	int nOldFocus = g_iFocus;
	int i;
	BOOL bNo;

	for ( i=0 ; i < g_nMaxChannel ; i ++ )
	{
		//g_ExitWait[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
		g_bExitFlag[i] = FALSE;
		g_iFocus=i;
		if (g_Channel_Info[i].bConnecting == TRUE ||
			g_Channel_Info[i].chStatus == channelConnecting)
		{
			Disconnect_Channel(i, TRUE);
		}
		else
		{
			//SetEvent(g_ExitWait[i]);
			g_bExitFlag[i] = TRUE;
		}
		g_Channel_Info[i].bConnecting = FALSE;
	}

	//WaitForMultipleObjects(g_nMaxChannel, g_ExitWait, TRUE, INFINITE);
	while (1)
	{
		bNo = TRUE;
		for ( i=0 ; i < g_nMaxChannel ; i ++ )
		{
			if (g_bExitFlag[i] != TRUE)
				bNo = FALSE;
		}

		if (bNo)
			break;
#ifdef _WIN32
		Sleep(16);
#else
		sleep(1);
#endif // _WIN32
	}

	for ( i=0 ; i < g_nMaxChannel ; i ++ )
	{
//		CloseHandle(g_ExitWait[i]);
		g_iFocus=i;

		DataBroker_DeleteConnection(g_hDataBroker,&(g_Channel_Info[i].hConn));

		g_Channel_Info[i].hChannel=NULL;

#ifdef _AVSYNC
		if (g_Channel_Info[i].hDecoderChannel)
			AvSynchronizer_DeleteDecoderChannel(g_hAvSync, &g_Channel_Info[i].hDecoderChannel);
#endif // _AVSYNC

		if (g_Channel_Info[i].pFileMedia)
			fclose(g_Channel_Info[i].pFileMedia);
		if (g_Channel_Info[i].pbyTxData)
			free(g_Channel_Info[i].pbyTxData);

		if (g_Channel_Info[i].pbyVideoBuff)
			free(g_Channel_Info[i].pbyVideoBuff);
		if (g_Channel_Info[i].pbyAudioBuff)
			free(g_Channel_Info[i].pbyAudioBuff);

#ifdef _WIN32
		DeleteCriticalSection(&g_CS[i]);
#else
		pthread_mutex_destroy(&g_CS[i]);
#endif // _WIN32
		
	}
	g_iFocus = nOldFocus;
   
	DataBroker_Release(&g_hDataBroker);
#ifdef _AVSYNC
	AvSynchronizer_Release(&g_hAvSync);
#endif // _AVSYNC

	return 0;
}

int 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;	

	g_Channel_Info[g_iFocus].bTxConn = FALSE;

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

	g_Channel_Info[nRelIdx].chStatus = channelDisconnecting;


	DataBroker_Disconnect(g_Channel_Info[nRelIdx].hConn);

	return 0;
}

#ifdef _AVSYNC
SCODE __stdcall AvSynchronizerDecodeCallback(DWORD dwContext, TMediaType tFrameType, TFRAMEINFO * tframeinfo )
{
	int index = (int)dwContext;
	FILE *pBinFile;

	char szBuf[MAX_PATH];

	if (tFrameType == AVSYNCHRONIZER_MEDIATYPE_VIDEO_ONLY)
	{
		printf("Video width = %d, Height = %d\n", tframeinfo->tVideoFrame.dwWidth,
			tframeinfo->tVideoFrame.dwHeight);
		g_Channel_Info[index].dwDecFileIndex++;
		g_Channel_Info[index].dwDecFileIndex %= g_nMaxDecoderFileNum;
		sprintf(szBuf, "%s%cvideo%d-%d.%s", DECODER_ROOT_PATH, CHAR_PATH_SEP,
			index, g_Channel_Info[index].dwDecFileIndex, g_szExtName);
	}
	else
	{
		g_Channel_Info[index].dwAudioFileIndex++;
		g_Channel_Info[index].dwAudioFileIndex %= g_nMaxDecoderFileNum;
		sprintf(szBuf, "%s%caudio%d-%d.pcm", DECODER_ROOT_PATH, CHAR_PATH_SEP, index, 
			g_Channel_Info[index].dwAudioFileIndex);
	}

#ifdef _DO_FILE_IO
	pBinFile = fopen(szBuf, "wb");
	if (pBinFile)
	{
		if (tFrameType == AVSYNCHRONIZER_MEDIATYPE_VIDEO_ONLY)
			fwrite(tframeinfo->tVideoFrame.pbyPicture,1, tframeinfo->tVideoFrame.dwSize, pBinFile);
		else
			fwrite(tframeinfo->tAudioFrame.pbySound, 1, tframeinfo->tAudioFrame.dwSize, pBinFile);
		fclose(pBinFile);
	}
#endif // _DO_FILE_IO

	return S_OK;
}
#endif // _AVSYNC

SCODE __stdcall DataBrokerNetPacketCallback(DWORD dwContext, DWORD dwMediaType, DWORD dwLen, BYTE *pbyPacket)
{
	int index;
	SCODE scRet;

	index = (int) dwContext;

	assert( g_Channel_Info[index].bNetPacketCallback);

	scRet = DataBroker_InputPacketEx(g_Channel_Info[index].hDataBrokerInput, 
		(TsdrMediaType) dwMediaType, pbyPacket, dwLen);

	if (scRet != S_OK)
	{
		printf("Error = %X\n", scRet);
	}

	return S_OK;

}

⌨️ 快捷键说明

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