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

📄 consolesample.c

📁 一个在Linux下开发的IP摄像头的驱动程序及控制软件
💻 C
📖 第 1 页 / 共 5 页
字号:
		else if (nType == 7)
			OutputMoreOptionsForUpgrade(nType);
	}
}


int Connect_Channel(void)
{
	SCODE v;
	TDataBrokerConnectionOptions ConnectionOptions;

	memset(&ConnectionOptions, 0, sizeof(ConnectionOptions));
	ConnectionOptions.dwStatusContext=g_Channel_Info[g_iFocus].dwContext;
	ConnectionOptions.dwAVContext=g_Channel_Info[g_iFocus].dwContext;
	ConnectionOptions.pzIPAddr=g_Channel_Info[g_iFocus].chHostIP;
	ConnectionOptions.pzUID=g_Channel_Info[g_iFocus].chUserName;
	ConnectionOptions.pzPWD=g_Channel_Info[g_iFocus].chPassword;
	if ( g_Channel_Info[g_iFocus].dwProtocol == 1 )
	{
		ConnectionOptions.dwMediaType=emtVideo|emtAudio;
		if (g_Channel_Info[g_iFocus].chServer_Type[2] > '3' &&
			g_Channel_Info[g_iFocus].chServer_Type[2] < '7')
			ConnectionOptions.dwProtocolType=eptHTTP;
		else
			ConnectionOptions.dwProtocolType=eptTCP;
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 2 )
	{
		ConnectionOptions.dwMediaType=emtVideo|emtAudio;
		ConnectionOptions.dwProtocolType=eptUDP;
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 3 )
	{
		if (g_Channel_Info[g_iFocus].chServer_Type[2] > '3' &&
			g_Channel_Info[g_iFocus].chServer_Type[2] < '7')
			ConnectionOptions.dwMediaType=emtVideo|emtAudio;
		else
			ConnectionOptions.dwMediaType=emtVideo;
		ConnectionOptions.dwProtocolType=eptHTTP; // for test
	}
	// the following is for IP3135 module only
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 4 )
	{
		ConnectionOptions.dwMediaType=emtAudio;
		ConnectionOptions.dwProtocolType=eptHTTP; // for test
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 5 )
	{
		ConnectionOptions.dwMediaType=emtVideo|emtAudio;
		ConnectionOptions.dwProtocolType=eptHTTP; // for test
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 6 )
	{
		ConnectionOptions.dwMediaType=emtVideo;
		ConnectionOptions.dwProtocolType=eptUDP; // for test
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 7 )
	{
		ConnectionOptions.dwMediaType=emtAudio;
		ConnectionOptions.dwProtocolType=eptUDP; // for test
	}
	// for 7000 only
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 8 )
	{
		ConnectionOptions.dwMediaType=emtVideo;
		ConnectionOptions.dwProtocolType=eptTCP; // for test
	}
	else if ( g_Channel_Info[g_iFocus].dwProtocol == 9 )
	{
		ConnectionOptions.dwMediaType=emtAudio;
		ConnectionOptions.dwProtocolType=eptTCP; // for test
	}
	else
	{
		ConnectionOptions.dwMediaType=emtVideo;
		ConnectionOptions.dwProtocolType=eptHTTP; // for test
	}

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

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

	if (g_Channel_Info[g_iFocus].bNetPacketCallback)
	{
		DataBroker_SetConnectionNetPacketCallback(g_Channel_Info[g_iFocus].hConn,
			DataBrokerNetPacketCallback, g_Channel_Info[g_iFocus].dwContext, TRUE);
	}

	{
		v = DataBroker_SetConnectionOptions(g_Channel_Info[g_iFocus].hConn,&ConnectionOptions);
		if ( v != S_OK)
		{
			printf("DataBroker_SetConnectionOptions error %X\n", v);
			return -1;
		}
	}

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

	return 0;
}

SCODE __stdcall DataBrokerAVCallBack(DWORD dwContext,TMediaDataPacketInfo *pMediaDataPacket)
{
	int index;
	index = (int) dwContext;

	if ( g_Channel_Info[index].bNetPacketCallback)
		return S_OK;

	return DataBrokerInputAVCallBack(dwContext, pMediaDataPacket);
}


SCODE __stdcall DataBrokerInputAVCallBack(DWORD dwContext,TMediaDataPacketInfo *pMediaDataPacket)
{
	TMediaDataPacketInfoEx *ptPacketEx = (TMediaDataPacketInfoEx *) pMediaDataPacket;
	char szFileName[MAX_PATH];
	FILE *pFile;

	int index;

	index=(int) dwContext;

	if (g_Channel_Info[index].bFirstI == FALSE)
	{
		if (pMediaDataPacket->dwStreamType == mctH263 || pMediaDataPacket->dwStreamType == mctJPEG ||
			pMediaDataPacket->dwStreamType == mctMP4V)
		{
			if (pMediaDataPacket->tFrameType == MEDIADB_FRAME_INTRA)
			{
				time_t t;
				DWORD dwMilliSec;
				struct tm *ptm;
#ifndef _WIN32
				struct timeval tv;
				struct timezone tz;

				gettimeofday(&tv, &tz);

				t = tv.tv_sec;
				dwMilliSec = (DWORD)tv.tv_usec / 1000;
#else
				SYSTEMTIME stNow;
				time(&t);
				GetLocalTime(&stNow);
				dwMilliSec = stNow.wMilliseconds;
#endif // 
				ptm = localtime(&t);

				if (ptPacketEx->dwWidth != 0)
				{
					printf("%04d%02d%02d %02d%02d%02d.%03d I frame arrived\n"
						"    The image imformation: W = %d, H = %d, Top Pad = %d, Bottom Pad = %d\n",
						ptm->tm_year + 1900, ptm->tm_mon + 1,
						ptm->tm_mday, ptm->tm_hour,
						ptm->tm_min, ptm->tm_sec,
						dwMilliSec,
						ptPacketEx->dwWidth, ptPacketEx->dwHeight, ptPacketEx->dwHeightPadTop,
						ptPacketEx->dwHeightPadBottom);
				}
				else
				{
					printf("%04d%02d%02d %02d%02d%02d.%03d I Frame arrived\n",
						ptm->tm_year + 1900, ptm->tm_mon + 1,
						ptm->tm_mday, ptm->tm_hour,
						ptm->tm_min, ptm->tm_sec,
						dwMilliSec);
				}

				g_Channel_Info[index].bFirstI = TRUE;
			}
			else
				return S_OK;
		}
		else if (g_Channel_Info[index].dwMediaType != AVSYNCHRONIZER_MEDIATYPE_AUDIO_ONLY)
			return S_OK;
	}
	else
	{
		if (pMediaDataPacket->dwStreamType == mctH263 || pMediaDataPacket->dwStreamType == mctJPEG ||
			pMediaDataPacket->dwStreamType == mctMP4V)
		{
			if (pMediaDataPacket->tFrameType == MEDIADB_FRAME_INTRA)
			{
				time_t t;
				DWORD dwMilliSec;
				struct tm *ptm;
#ifndef _WIN32
				struct timeval tv;
				struct timezone tz;

				gettimeofday(&tv, &tz);

				t = tv.tv_sec;
				dwMilliSec = (DWORD)tv.tv_usec / 1000;
#else
				SYSTEMTIME stNow;
				time(&t);
				GetLocalTime(&stNow);
				dwMilliSec = stNow.wMilliseconds;
#endif // 
				ptm = localtime(&t);

				if (ptPacketEx->dwWidth != 0)
				{
					printf("%04d%02d%02d %02d%02d%02d.%03d I frame arrived\n"
						"    The image imformation: W = %d, H = %d, Top Pad = %d, Bottom Pad = %d\n",
						ptm->tm_year + 1900, ptm->tm_mon + 1,
						ptm->tm_mday, ptm->tm_hour,
						ptm->tm_min, ptm->tm_sec,
						dwMilliSec,
						ptPacketEx->dwWidth, ptPacketEx->dwHeight, ptPacketEx->dwHeightPadTop,
						ptPacketEx->dwHeightPadBottom);
				}
				else
				{
					printf("%04d%02d%02d %02d%02d%02d.%03d I Frame arrived\n",
						ptm->tm_year + 1900, ptm->tm_mon + 1,
						ptm->tm_mday, ptm->tm_hour,
						ptm->tm_min, ptm->tm_sec,
						dwMilliSec);
				}

				g_Channel_Info[index].nFileCount++;
				g_Channel_Info[index].nFileCount %= MAX_GOP_FILE_NUM;
			}
		}
	}

#ifdef _WIN32
	EnterCriticalSection(&g_CS[index]);
#else
	pthread_mutex_lock(&g_CS[index]);
#endif // _WIN32

#ifdef _AVSYNC
	if (g_Channel_Info[index].hDecoderChannel != NULL)
	{
		SCODE scRet = S_OK;
		TFRAMEINFO tFrameInfo;
		DWORD dwRealSize;

		if (pMediaDataPacket->dwStreamType == mctH263 || pMediaDataPacket->dwStreamType == mctJPEG ||
			pMediaDataPacket->dwStreamType == mctMP4V)
		{
			if (g_bCBDecoder)
			{
				scRet = AvSynchronizer_InputToBeDecodedMediaFrame(g_Channel_Info[index].hDecoderChannel, 
					pMediaDataPacket);
				if (scRet == S_OK)
				{
					printf("Decode Video 1 OK\n");
				}
				else
				{
					printf("Decode Video 1 error: %X\n", scRet);
				}
			}
			else
			{				
				// Note: you could have a global TVFRAMEINFO or TAFRAMEINFO to reduce the need
				// to allocate/free memory, but here is a simplified version that use local
				// variables to show the way to use the two function

				memset(&tFrameInfo.tVideoFrame, 0, sizeof(tFrameInfo.tVideoFrame));
				// 2004/08/16
				// the rule: max picture size is 704x576, and the possible decode video
				// types have max pixel of 4 (RGB32).. and the 1000 is for BMP header or other
				// overhead. If you have set SETCH_RESERVED_HEADER, the 1000 must include
				// the reserved size. 
				tFrameInfo.tVideoFrame.pbyHeader = g_Channel_Info[index].pbyVideoBuff;

				tFrameInfo.tVideoFrame.dwSize = 704 * 576 * 4 + 1000;
				if (tFrameInfo.tVideoFrame.pbyHeader != NULL)
				{
					scRet = AvSynchronizer_DecodeVideo(g_Channel_Info[index].hDecoderChannel, 
						pMediaDataPacket, &tFrameInfo.tVideoFrame, &dwRealSize);
					if (scRet == S_OK)
					{
						printf("Decode Video OK\n");
						tFrameInfo.tVideoFrame.dwSize = dwRealSize;
						// Note: the call to this function is just for demo
						AvSynchronizerDecodeCallback(dwContext, (TMediaType) VIDEO_FRAME, &tFrameInfo);
					}
					else
					{
						printf("Decode Video error: %X\n", scRet);
					}
				}				
			}
		}
		else
		{
			if (g_bCBDecoder)
			{
				scRet = AvSynchronizer_InputToBeDecodedMediaFrame(g_Channel_Info[index].hDecoderChannel, 
					pMediaDataPacket);
				if (scRet == S_OK)
				{
					printf("Decode Audio 1 OK\n");
				}
				else
				{
					printf("Decode Audio 1 error: %X\n", scRet);
				}
			}
			else
			{	
				memset(&tFrameInfo.tAudioFrame, 0, sizeof(tFrameInfo.tAudioFrame));
				// 2004/08/16
				// the rule: max possible audio frame number in a packet (which is 20 currently)
				// * max possible decode pcm frame size (which is 2048 for AAC)
				// if you are sure the possible audio codec the size for 2048 could
				// be reduced to 320 for G7221 and 160 for G729A
				tFrameInfo.tAudioFrame.dwSize = 20 * 2048;
				if (tFrameInfo.tAudioFrame.pbySound != NULL)
				{
					scRet= AvSynchronizer_DecodeAudio(g_Channel_Info[index].hDecoderChannel, 
						pMediaDataPacket, &tFrameInfo.tAudioFrame, &dwRealSize);
					if (scRet == S_OK)
					{
						printf("Decode Audio OK\n");
						tFrameInfo.tAudioFrame.dwSize = dwRealSize;
						// Note: the call to this function is just for demo
						// the non-callback funciton is a little different for callback
						// version because the audio frames are decoded and put together
						// in one tFrameInfo.tAudioFrame in non-callback model,
						// while in callback model, the callback is made one times
						// for one audio frame.
						AvSynchronizerDecodeCallback(dwContext, (TMediaType) AUDIO_FRAME, &tFrameInfo);
					}
					else
					{
						printf("Decode Audio error: %X\n", scRet);
					}					
				}
			}
		}

		if (scRet != S_OK)
		{
			if (pMediaDataPacket->dwStreamType == mctH263 || pMediaDataPacket->dwStreamType == mctJPEG ||
				pMediaDataPacket->dwStreamType == mctMP4V)
				printf("Decode video frame failed with error %X\n", scRet);
			else
				printf("Decode audio frame failed with error %X\n", scRet);
		}
	}

#ifdef _DO_FILE_IO
	sprintf(szFileName, "%s%ch263Gop%04d.bin", RECORD_ROOT_PATH, CHAR_PATH_SEP, g_Channel_Info[index].nFileCount);

	pFile = fopen(szFileName, "ab");
	if (pFile)
	{
		fwrite(pMediaDataPacket->pbyBuff + pMediaDataPacket->dwOffset,
			pMediaDataPacket->dwBitstreamSize, 1, pFile);
		fclose(pFile);
	}
#endif // _DO_FILE_IO

#endif // _AVSYNC

#ifdef _WIN32
	LeaveCriticalSection(&g_CS[index]);
#else
	pthread_mutex_unlock(&g_CS[index]);
#endif // _WIN32
	
	return S_OK;
}

long OnConnectError(long wParam, long lParam)
{
	switch (lParam)
	{
	case connErrAuth :
        printf("Channel %d Authenticated failed\n", wParam + 1);
		break;
	case connErrConnFailed :
        printf("Channel %d connected failed\n", wParam + 1);
		break;
	case connErrHttpRead :
        printf("Channel %d read HTML data failed\n", wParam + 1);
		break;
	case connErrOutMemory :
        printf("Channel %d connected failed because out of memory\n", wParam + 1);
		break;
	case connErrAccessName :
		printf("Channel %d connected failed because access name error\n", wParam + 1);
		break;
	default :
		return 0;
	}

	return 0;
}

long OnSignalChanged(long wParam, long lParam)
{
    printf("Channel %d Signal is changed to %s\n", wParam + 1,
		lParam==eSignalRestored?"ON":"OFF");

	return 0;
}


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

	switch ( tStatusType )
	{
		case eOnConnectionInfo:
			{
				PTDataBrokerConnInfo pConnInfo;
				pConnInfo=(PTDataBrokerConnInfo) pvParam1;

				printf("ConnInfo: Witdth = %d, Height = %d\n", pConnInfo->dwWidth,
					pConnInfo->dwHeight);
				g_Channel_Info[index].dwVideo_Height=pConnInfo->dwHeight;
				g_Channel_Info[index].dwVideo_Width=pConnInfo->dwWidth;
				g_Channel_Info[index].dwAudioCodec=pConnInfo->dwAudioCodec;
				g_Channel_Info[index].dwVideoCodec=pConnInfo->dwVideoCodec;

#ifdef _AVSYNC
				if ( pConnInfo->dwMediaType == (emtAudio|emtVideo) )
				{
					g_Channel_Info[index].dwMediaType=AVSYNCHRONIZER_MEDIATYPE_AUDIO_VIDEO;
				}
				else if ( pConnInfo->dwMediaType == emtAudio )
				{
					g_Channel_Info[index].dwMediaType=AVSYNCHRONIZER_MEDIATYPE_AUDIO_ONLY;
				}
				else
				{
					g_Channel_Info[index].dwMediaType=AVSYNCHRONIZER_MEDIATYPE_VIDEO_ONLY;
				}
#endif // _AVSYNC

				if (g_Channel_Info[index].chServer_Type[0] == 0)
					strcpy(g_Channel_Info[index].chServer_Type, pConnInfo->szServerType);

				if (g_Channel_Info[index].bNetPacketCallback)
				{
					TDataBrokerInputOptions tInputOpt;
					SCODE scRet;

					memset(&tInputOpt, 0, sizeof(tInputOpt));
					tInputOpt.pfStatus = DataBrokerInputStatusCallBack;
					tInputOpt.pfAV = DataBrokerInputAVCallBack;
					tInputOpt.dwStatusContext = g_Channel_Info[g_iFocus].dwContext;
					tInputOpt.dwAVContext = g_Channel_Info[g_iFocus].dwContext;
					tInputOpt.pzServerType = g_Channel_Info[g_iFocus].chServer_Type;
					tInputOpt.dwAudioCodec = pConnInfo->dwAudioCodec;
					tInputOpt.dwVideoCodec = pConnInfo->dwVideoCodec;
					tInputOpt.dwProtocolType = pConnInfo->dwProtocol;
					tInputOpt.zVSize = "0";

					scRet = DataBroker_SetInputOptions(g_Channel_Info[index].hDataBrokerInput,
						&tInputOpt);
					assert(scRet == S_OK);
				}

			}

			g_Channel_Info[index].nQueueFullTimes = 0;

			break;
		case eOnAuthFailed:

⌨️ 快捷键说明

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