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

📄 pddvclas.cpp

📁 USB Webcam driver for Windows CE,修正前版本一些问题,支持CE6.0。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
int pdd_QueryFeature (PDRVCONTEXT pDrv, DWORD dwFeatureID, PFEATUREPROP pfp) 
{
	int rc;

	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_QueryFeature++  ID: %d\r\n"), dwFeatureID));

	// Look up the feature data from the ID
	PFEATURESTRUCT pfs = FindFeatureInfo ((BYTE)dwFeatureID);
	if (!pfs)
		return ERROR_NOT_SUPPORTED;

	pfp->dwFeatureID = dwFeatureID;
	pfp->dwFlags = 0;
	pfp->nMin = 0;
	pfp->nMax = 0;

	// Get min value
	rc = DoVendorTransfer (pDrv, USBVID_GET_MIN, pfs->bCmdID, pfs->bInterface, 
	                       pfs->bUnit, (PBYTE)&pfp->nMin, pfs->wLength);
	if (rc)
		DEBUGMSG (ZONE_ERROR | ZONE_FEATURE,
		          (DTAG TEXT("Error getting min value for feat %d. rc %d\r\n"),
				  dwFeatureID, rc));
	// Get max value
	rc = DoVendorTransfer (pDrv, USBVID_GET_MAX, pfs->bCmdID, pfs->bInterface, 
	                       pfs->bUnit, (PBYTE)&pfp->nMax, pfs->wLength);
	if (rc)
		DEBUGMSG (ZONE_ERROR | ZONE_FEATURE,
		          (DTAG TEXT("Error getting max value for feat %d. rc %d\r\n"),
				  dwFeatureID, rc));

	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_QueryFeature--  rc %d  min: %xh max %xh\r\n"), 
	                      rc, pfp->nMin, pfp->nMax));
	return rc;
}
//---------------------------------------------------------------------------------------
// pdd_GetParameter - Gets the current value of a camera parameter
//
int pdd_GetParameter (PDRVCONTEXT pDrv, DWORD dwFeatureID, PDWORD pData)
{
	int rc;

	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_GetParameter++  ID: %d\r\n"), dwFeatureID));

	// Look up the feature data from the ID
	PFEATURESTRUCT pfs = FindFeatureInfo ((BYTE)dwFeatureID);
	if (pfs)
	{
		*pData = 0;
		// Send packet to the device to get the parameter
		rc = DoVendorTransfer (pDrv, USBVID_GET_CUR, pfs->bCmdID, pfs->bInterface, 
							   pfs->bUnit, (PBYTE)pData, pfs->wLength);
	} 
	else
		rc = ERROR_NOT_SUPPORTED;

	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_GetParameter--  rc %d  val: %xh\r\n"), rc, *pData));
	return rc;
}
//---------------------------------------------------------------------------------------
// pdd_SetParameter - Sets a camera parameter
//
int pdd_SetParameter (PDRVCONTEXT pDrv, DWORD dwFeatureID, DWORD dwVal) 
{
	int rc;
	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_SetParameter++  ID: %d Val: %xh\r\n"), dwFeatureID, dwVal));

	// Look up the feature data from the ID
	PFEATURESTRUCT pfs = FindFeatureInfo ((BYTE)dwFeatureID);
	if (pfs)
	{
		// Send packet to the device to set the parameter
		rc = DoVendorTransfer (pDrv, USBVID_SET_CUR, pfs->bCmdID, pfs->bInterface, 
							   pfs->bUnit, (PBYTE)&dwVal, pfs->wLength);
	}
	else
		rc = ERROR_NOT_SUPPORTED;

	DEBUGMSG (ZONE_FUNC | ZONE_FEATURE, 
	          (DTAG TEXT("pdd_SetParameter--  rc %d\r\n"), rc));
	return rc;
}
//-----------------------------------------------------------------------
// GetFormatParameters - This routine parses the video class control 
// interface descriptor to find information on the requested format
// 
int pdd_GetFormatParameters (PDRVCONTEXT pDrv, BYTE bFormatIndex, BYTE bFrameIndex, 
							 BOOL fStill, PFORMATPROPS pProps) 
{
	int rc;
	BYTE bLastSubType = USB_VIDEO_VS_UNDEFINED;
	BOOL fFound = FALSE;

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetFormatParameters++ Fmt:%d  Frm:%d\r\n"), bFormatIndex, bFrameIndex));

	// Get our pdd specific contextlpex
	PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext; 

	// Find the Frame descriptors
	PUSBVIDSTREAMIFDESCRIPTOR pHdr = (PUSBVIDSTREAMIFDESCRIPTOR)pPDD->usbstrmIF->lpepExtDesc;
	__try {
		// Sanity check on header IDs
		if (!pHdr || (pHdr->bType != 0x24) || (pHdr->bSubtype != 1)) 
		{
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Bad Extended Stream Descriptor\r\n")));
			return -1;
		}
		PBYTE pData = (PBYTE)pPDD->usbstrmIF->lpepExtDesc;
		PBYTE pEnd = (PBYTE)pPDD->usbstrmIF->lpepExtDesc + pHdr->wTotalLen;
		PUSBVIDSTDDESCHDR pStd = (PUSBVIDSTDDESCHDR)pHdr;
		// Loop through all the descriptors
		while (pData + pStd->bLen < pEnd)
		{
			pData += pStd->bLen;
			pStd = (PUSBVIDSTDDESCHDR)pData;

			if (pStd->bType != USB_VIDEO_CS_INTERFACE)
			{
				DEBUGMSG (1, (TEXT("Unexpected header type %xh\r\n"), pStd->bType));
				break;
			}
			// Are we looking for still formats or stream?
			if (!fStill)
			{
				switch (pStd->bSubtype) 
				{
				//TODO:: Need to support other formats
				case USB_VIDEO_VS_FORMAT_MJPEG:
					rc = ProcessFrameFormats (pPDD, pStd, VIDFORMAT_MJPEG, 
					                          bFormatIndex, bFrameIndex, pProps, &fFound);
					break;
				case USB_VIDEO_VS_FORMAT_UNCOMPRESSED:
					rc = ProcessFrameFormats (pPDD, pStd, VIDFORMAT_UNCOMPRESSED, 
					                          bFormatIndex, bFrameIndex, pProps, &fFound);
					break;
				case USB_VIDEO_VS_FORMAT_FRAME_BASED:
					rc = ProcessFrameFormats (pPDD, pStd, USB_VIDEO_VS_FRAME_FRAME_BASED, 
					                          bFormatIndex, bFrameIndex, pProps, &fFound);
					break;
				case USB_VIDEO_VS_FORMAT_MPEG2TS:
					break;
				case USB_VIDEO_VS_FORMAT_DV:
					break;
				default:
					break;
				}
				if (fFound)
					return rc;
			}
			else
			{
				if (pStd->bSubtype == USB_VIDEO_VS_STILL_IMAGE_FRAME)
				{
					PUSBVIDSTREAMIF_STILLIMGDESCRIPTOR pFrmStill = (PUSBVIDSTREAMIF_STILLIMGDESCRIPTOR)pStd;
					// See if index is within range
					if (bFrameIndex < pFrmStill->bNumImageSizePatterns)
					{
						memset (pProps, 0, sizeof (FORMATPROPS));
						pProps->cbSize = sizeof (FORMATPROPS);
						pProps->wFormatType = bLastSubType;
						pProps->wFormatIndex = bFormatIndex;
						pProps->wFrameIndex = bFrameIndex;
						pProps->dwHeight = pFrmStill->sStillFmt[bFrameIndex].wHeight;
						pProps->dwWidth = pFrmStill->sStillFmt[bFrameIndex].wWidth;
						//Maxbuff below assumes worst case 32bpp
						pProps->dwMaxBuff = pProps->dwHeight * pProps->dwWidth * 4; 
						return 0;
					}
					return -4;
				}
			}
			// Save the last format type
			bLastSubType = pStd->bSubtype;
		}
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		DEBUGMSG (1, (TEXT("Exception scanning extended stream descriptor\r\n")));
		return -2;
	}

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetFormatParameters-- found:%d\r\n"), fFound));
	return -3;
}	

//-----------------------------------------------------------------------
// pdd_GetCurrentFormat - Returns the format of the current stream
// but only if the stream is not internal. 
// 
int pdd_GetCurrentFormat (PDRVCONTEXT pDrv, PFORMATPROPS pProps) 
{
	int rc = ERROR_VC_DISCONNECTED;

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetCurrentFormat++\r\n")));

	// Get our pdd specific context
	PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext; 

	// See if stream running
	if (pPDD->wReadThreadState != STREAMTHD_STOPPED)
	{
		// Make sure its not an internal stream for still capture
		if ((DWORD)pPDD->pstrStream != (DWORD)&pPDD->strStreamDefault)
		{
			rc = pdd_GetFormatParameters (pDrv, (BYTE)pPDD->wCurrFormatIndex,
										  (BYTE)pPDD->wCurrFrameIndex, FALSE, 
										  pProps);
			// Save the stream rate
			pProps->nNumInterval = 1;
			pProps->dwInterval[0] = pPDD->dwCurrValidInterval;
		}
	}
	if (rc == ERROR_VC_DISCONNECTED)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error No stream active\r\n")));

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetCurrentFormat-- rc %d\r\n"), rc));
	return rc;
}
//-----------------------------------------------------------------------
// pdd_StartVidStream - Set streaming video from camera
// 
int pdd_StartVidStream (PDRVCONTEXT pDrv, BYTE bFormatIndex, BYTE bFrameIndex, 
						PVIDSTREAMSTRUCT pstrStream, DWORD dwFrameInterval) 
{
	int i, rc = 0;
	BYTE bInterface = VID_IF_STREAM;
	BYTE bUnit = 0;
	DWORD dwValidInterval;
	int nIntIndex;

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_StartVidStream++  Fmt %d\r\n"), bFrameIndex));

	// Get our pdd specific context
	PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext; 

	//
	// If we're starting just for a still capture, if any streaming is going
	// on, we'll take it and return.
	//
	if ((bFormatIndex == 0xff) && (bFrameIndex == 0xff))
	{
		if (pPDD->pstrStream != 0)
		{
			DEBUGMSG (ZONE_STILL, (DTAG TEXT("StartVidStream already running.  Exiting\r\n")));
			return ERROR_STREAM_ALREADY_RUNNING;
		}
		bFormatIndex = 1;
		bFrameIndex = 1;
		dwFrameInterval = 0;
	}
	//
	// Query the parameters for the new format.  This validates format and frame values
	//
	FORMATPROPS Props;
	rc = pdd_GetFormatParameters (pDrv, bFormatIndex, bFrameIndex, FALSE, &Props);
	if (rc)
	{
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Bad format or frame fmt:%d frame:%d rc %d\r\n"),
		          bFormatIndex, bFrameIndex, rc));
		return ERROR_INVALID_PARAMETER;
	}

	//
	// Validate the frame interval
	//

	// Discrete intervals?
	if (Props.nNumInterval != 0)
	{
		// See if min
		if (dwFrameInterval == -1)
			nIntIndex = Props.nNumInterval-1;

		// If 0, set to fastest interval
		else if (dwFrameInterval == 0)
			nIntIndex = 0;

		// Else look up to see if valid
		else
		{
			// Loop through the allowed descrete intervals
			for (nIntIndex = 0; nIntIndex < Props.nNumInterval; nIntIndex++)
			{
				if (dwFrameInterval == Props.dwInterval[nIntIndex])
					break;
			}
			if (nIntIndex == Props.nNumInterval)
				rc = ERROR_INVALID_PARAMETER;
		}
	}
	else
	{
		// See if min
		if (dwFrameInterval == -1)
			dwValidInterval = Props.dwInterval[0]; //Min value

		// If 0, set to fastest interval
		else if (dwFrameInterval == 0)
			dwValidInterval = Props.dwInterval[1]; //Max value

		// Else see if in proper range
		else
			if ((dwFrameInterval >= Props.dwInterval[0]) && (dwFrameInterval <= Props.dwInterval[1]))
				dwValidInterval = dwFrameInterval;
			else
				rc = ERROR_INVALID_PARAMETER;
	}
	if (rc)
	{
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT ("Bad frame interval %d specified\r\n"), dwFrameInterval));
		return rc;
	}

	//
	// Do probe commit for video stream
	//
	// Discrete intervals?
	if (Props.nNumInterval != 0)
	{
		// Try all frame intervals for a given Format/Frame size setting
		for (i = nIntIndex; i < Props.nNumInterval; i++)
		{
			// See if any quality setting for these parameters can work
			rc = NegotiateQuality (pDrv, bFormatIndex, bFrameIndex, Props.dwInterval[i]);
			if (rc == 0)
				break;
		}
	}
	else
	{
		// Starting with specified interval increment until over max
		while (dwValidInterval <= Props.dwInterval[1])
		{
			// See if any quality setting for these parameters can work
			rc = NegotiateQuality (pDrv, bFormatIndex, bFrameIndex, dwValidInterval);
			if (rc == 0)
				break;
			// Increment by granularity
			dwValidInterval += Props.dwInterval[2]; // rate granularity
		}
	}
	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Probe/Commit failed.  Not enough bandwidth for Fmt: %d  Frm: %d\r\n"), 
		          bFormatIndex, bFrameIndex));

	//
	// Allocate the streaming buffers if an internal stream just for a still
	//
	if (rc == 0)
	{
		// See if we need to set up a default stream or if we're changing the dest buffers
		if (pstrStream == 0) 
		{
			// Allocate buffers internally for the stream
			rc = AllocateInternalStreamBuffers (pPDD, Props.dwMaxBuff);
			if (rc == 0)
			{
				pPDD->pstrStream = (PVIDSTREAMSTRUCT)&pPDD->strStreamDefault;
			} 
		}
		// Else, we have a stream strcture passed to us.  Use it.
		else
		{
			pPDD->pstrStream = pstrStream;
		}
	}

	//
	// Start the read thread to get the data
	//
	if (rc == 0)

⌨️ 快捷键说明

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