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

📄 pddvclas.cpp

📁 USB Webcam driver for Windows CE,修正前版本一些问题,支持CE6.0。
💻 CPP
📖 第 1 页 / 共 5 页
字号:

	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error setting original probe vals  rc %d\r\n"), rc));

	if (rc == 0)
	{
		// Get minimum quality value
		p->bFormatIndex = bFormatIndex;
		p->bFrameIndex = bFrameIndex;
		p->dwFrameInterval = dwFrameInterval;

		rc = DoVendorTransfer (pDrv, USBVID_GET_MIN, USB_VIDEO_VS_CS_PROBE_CTL,
		                       bInterface, bUnit, (PBYTE)p, wProbeLen);
		if (rc)
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error getting min value. rc %d\r\n"), rc));
		*pwCompMin = p->wCompQuality;
	}

	if (rc == 0)
	{
		// Get maximum quality value
		p->bFormatIndex = bFormatIndex;
		p->bFrameIndex = bFrameIndex;
		p->dwFrameInterval = dwFrameInterval;

		rc = DoVendorTransfer (pDrv, USBVID_GET_MAX, USB_VIDEO_VS_CS_PROBE_CTL,
		                       bInterface, bUnit, (PBYTE)p, wProbeLen);
		if (rc)
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error getting max value. rc %d\r\n"), rc));
		*pwCompMax = p->wCompQuality;
	}

	if (rc == 0)
	{
		// Get maximum quality value
		p->bFormatIndex = bFormatIndex;
		p->bFrameIndex = bFrameIndex;
		p->dwFrameInterval = dwFrameInterval;

		rc = DoVendorTransfer (pDrv, USBVID_GET_RES, USB_VIDEO_VS_CS_PROBE_CTL,
		                       bInterface, bUnit, (PBYTE)p, wProbeLen);
		if (rc)
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error getting inc value. rc %d\r\n"), rc));
		*pwCompInc = p->wCompQuality;
	}

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("GetMinMaxQuality-- rc %d\r\n"), rc));
	return rc;
}

//-----------------------------------------------------------------------
// ProbeCommitVidStream - Negotiates the streaming parameters 
// 
int ProbeCommitVidStream (PDRVCONTEXT pDrv, BYTE bFormatIndex, BYTE bFrameIndex, 
		                  DWORD *pdwFrameInterval, WORD wCompression, PDWORD pdwMaxBandwidth) 
{
	int rc = 0;
	BYTE bInterface = VID_IF_STREAM;
	BYTE bUnit = 0;
	WORD wProbeLen = sizeof (PSTREAM_PROBE_CONTROLSTRUCT);

	DEBUGMSG (ZONE_FUNC | ZONE_PROBE, 
	          (DTAG TEXT("ProbeCommitVidStream++ Fmt: %d  Frm: %d  Interval %d  Compression %d\r\n"), 
	          bFormatIndex, bFrameIndex, *pdwFrameInterval, wCompression));

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

	// Get the length of this struct, since it changes between cameras
	rc = GetVendorTransferLen (pDrv, USB_VIDEO_VS_CS_PROBE_CTL, bInterface, bUnit, &wProbeLen);

	// Clear the buffer.  We use this because the USB stack (or camera) sometimes returns more
	// data than the size of the STREAM_PROBE_CONTROLSTRUCT structure.

	STREAM_PROBE_CONTROLSTRUCT pSPC;
	memset (&pSPC, 0, sizeof (STREAM_PROBE_CONTROLSTRUCT));

	// Set probe values
	pSPC.bFormatIndex = bFormatIndex;
	pSPC.bFrameIndex = bFrameIndex;
	pSPC.dwFrameInterval = *pdwFrameInterval;
	pSPC.wCompQuality = wCompression;
	pSPC.bmHint = USB_VIDEO_PROBE_HINT_FRAMEINTERVAL;

	rc = DoVendorTransfer (pDrv, USBVID_SET_CUR, USB_VIDEO_VS_CS_PROBE_CTL,
		                   bInterface, bUnit, (PBYTE)&pSPC, wProbeLen);
	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error setting probe values. rc %d\r\n"), rc));

	// Get back probe values
	if (rc == 0)
	{
		rc = DoVendorTransfer (pDrv, USBVID_GET_CUR, USB_VIDEO_VS_CS_PROBE_CTL,
		                       bInterface, bUnit, (PBYTE)&pSPC, wProbeLen);
		if (rc)
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error getting final probe values. rc %d\r\n"), rc));
	}

	// Set the commit values
	if (rc == 0)
	{
		rc = DoVendorTransfer (pDrv, USBVID_SET_CUR, USB_VIDEO_VS_CS_COMMIT_CTL,
		                       bInterface, bUnit, (PBYTE)&pSPC, wProbeLen);
		if (rc)
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error setting probe values. rc %d\r\n"), rc));
	}
	if (rc == 0)
	{
		// Save the max payload value, we need this for choosing the proper interface
		*pdwMaxBandwidth = (pSPC.dwMaxVideoFrameSize * 10000) / pSPC.dwFrameInterval;
	}
	DEBUGMSG (ZONE_FUNC | ZONE_PROBE, 
	          (DTAG TEXT("ProbeCommitVidStream-- Fmt: %d  Frm: %d  Compression %d Interval %d  Transfer size: %d Frame size %d BW %d\r\n"), 
	          pSPC.bFormatIndex, pSPC.bFrameIndex, pSPC.wCompQuality, pSPC.dwFrameInterval, pSPC.dwMaxPayloadTransferSize, pSPC.dwMaxVideoFrameSize, *pdwMaxBandwidth));
	
	return rc;
}

//-----------------------------------------------------------------------
// AllocateInternalStreamBuffers - If a still is requested and no
// stream is currently running, this routine allocates streaming buffers
// internally.
// 
int AllocateInternalStreamBuffers (PPDDCONTEXT pPDD, DWORD dwMaxBuff) 
{
	int i, rc = 0;

	//
	// See if the new format is too small for the current default
	//
	if (pPDD->strStreamDefault.dwBuffSize < dwMaxBuff)
	{
		// We/re changing buffers, stop the current read thread 
		StopReadThread (pPDD);

		// Free old buffers
		for (i = 0; i < NUMDEFBUFFS; i++)
		{
			if (pPDD->strStreamDefault.pFrame[i].pBuff) LocalFree (pPDD->strStreamDefault.pFrame[i].pBuff);
			pPDD->strStreamDefault.pFrame[i].pBuff = 0;
		}

		// Allocate new buffers
		for (i = 0; i < NUMDEFBUFFS; i++)
		{
			pPDD->strStreamDefault.pFrame[i].pBuff = (PBYTE)LocalAlloc (LPTR, dwMaxBuff);

			// Check to see if the alloc is successful
			if (pPDD->strStreamDefault.pFrame[i].pBuff == 0)
			{
				// If we've already allocated some buffs, we need to free the ones we did
				if (i) 
				{
					// Yes, I'm reusing 'i', but I'm bailing out of this loop
					for (i = 0; i < NUMDEFBUFFS; i++)
					{
						if (pPDD->strStreamDefault.pFrame[i].pBuff) LocalFree (pPDD->strStreamDefault.pFrame[i].pBuff);
						pPDD->strStreamDefault.pFrame[i].pBuff = 0;
					}
				}
				DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Failure allocating stream buffers\r\n")));
				return ERROR_NOT_ENOUGH_MEMORY;
			}
		}

		// We've set up the default buffs, set the buff ptr.
		pPDD->strStreamDefault.dwBuffSize = dwMaxBuff;

		// Use the reserved field to store the event handle local to the driver
		if (pPDD->strStreamDefault.dwReserved == 0)
			pPDD->strStreamDefault.dwReserved = (DWORD)CreateEvent (NULL, FALSE, FALSE, NULL);
	} 
	return rc;
}
//-----------------------------------------------------------------------
// NegotiateQuality - Given a fixed set of format/frame/interval, see if a
// quality setting can be found that allows us to fit into the available
// bus bandwidth.
// 
int NegotiateQuality (PDRVCONTEXT pDrv, BYTE bFormatIndex, BYTE bFrameIndex, DWORD dwValidInterval)
{
	int rc;
	DWORD dwBandwidth;
	WORD wQuality, wQualityMin, wQualityMax, wQualityInc;

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

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

	// Get the range of valid compression values for this format/frame/rate
	rc = GetMinMaxQuality (pDrv, bFormatIndex, bFrameIndex, dwValidInterval,
							&wQualityMin, &wQualityMax, &wQualityInc);
	if (rc == 0)
	{
		DEBUGMSG (ZONE_PROBE, (DTAG TEXT("Probe/Commit trying interval rate %d  Comp: min %d max %d int %d\r\n"), 
				  dwValidInterval, wQualityMin, wQualityMax, wQualityInc));
		
		// Loop through each compression quality setting finding one that works.
		for (wQuality = wQualityMax; wQuality >= wQualityMin; wQuality -= wQualityInc)
		{
			// Send the the paramters to the camera, it'll tell us the bandwidth it needs
			rc = ProbeCommitVidStream (pDrv, bFormatIndex, bFrameIndex, &dwValidInterval, wQuality,
									   &dwBandwidth);
			if (rc == 0) 
			{
				// Save the format and stream rate
				pPDD->wCurrFormatIndex = bFormatIndex;
				pPDD->wCurrFrameIndex = bFrameIndex;
				pPDD->dwCurrValidInterval = dwValidInterval;

				// Try to find an interface that will support the bandwidth
				rc = SetStreamInterface (pDrv, 1, dwBandwidth);
				if (rc == 0)
				{
					//Probe commit worked and we have an interface.  Yea! we're done.
					DEBUGMSG (ZONE_PROBE, (DTAG TEXT("Probe/Commit complete. Fmt: %d  Frm: %d  rate %d Compression %d Bandwidth %d\r\n"), 
							  bFormatIndex, bFrameIndex, dwValidInterval, wQuality, dwBandwidth));
					break;
				}
			}
		}
	}
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("NegotiateQuality--  rc %d\r\n"), rc));
	return rc;
}
	
//-----------------------------------------------------------------------
// FindStreamInterface - Find an interface given packet size needed.  
// If fMin is true, it finds the slowest interface that meets the
// packet size.  If fMin is false, if finds the fastest interface
// under the specified packet size.
//
PUSBSTRMIF FindStreamInterface (PPDDCONTEXT pPDD, BYTE nInterface, 
								BOOL fMin, WORD wPacketSize)
{
	int i;
	PUSBSTRMIF lpFound = 0;

	// Find the interface
	for (i = 0; i < pPDD->nStreamInterfaces; i++)
	{

		if (pPDD->usbstrmIF[i].ifDesc.bInterfaceNumber == nInterface) 
		{
			// Does the interface even have an endpoint?
			if (pPDD->usbstrmIF[i].fEndpoint)
			{
				if (fMin)
				{
					// Find the IF with min packet size that meets the needs
					if (pPDD->usbstrmIF[i].epDesc.wMaxPacketSize >= wPacketSize)
					{ 
						if (lpFound == 0) lpFound = &pPDD->usbstrmIF[i];
						if(pPDD->usbstrmIF[i].epDesc.wMaxPacketSize < lpFound->epDesc.wMaxPacketSize)
						{
							lpFound = &pPDD->usbstrmIF[i];
						}
					}
				}
				else
				{
					// Find the IF with packet size requested
					if (pPDD->usbstrmIF[i].epDesc.wMaxPacketSize < wPacketSize) 
					{
						if (lpFound == 0) lpFound = &pPDD->usbstrmIF[i];
						if (pPDD->usbstrmIF[i].epDesc.wMaxPacketSize > lpFound->epDesc.wMaxPacketSize)
						{
							lpFound = &pPDD->usbstrmIF[i];
						}
					}
				}
			}
			// See if we want a pipe with no bandwidth
			else if (wPacketSize == 0)
				return &pPDD->usbstrmIF[i];
		}
	}
	if (lpFound)
		return lpFound;
	return 0;
}

//-----------------------------------------------------------------------
// SetStreamInterface - Sets the requested stream interface.  The
// different stream interfaces have different data bandwidths.
// 
BOOL SetStreamInterface (PDRVCONTEXT pDrv, BYTE nInterface, DWORD dwBandwidth) 
{
	USB_TRANSFER hTransfer;
	PUSBSTRMIF lpIF;
	int rc;

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

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("SetStreamInterface++  Interface:%d  pktsize:%d\r\n"), 
	          nInterface, dwBandwidth));

	// Close the currently opened pipe if any
	if (pPDD->pipeStream.hEvent != 0)
	{
		pDrv->lpUsbFuncs->lpClosePipe (pPDD->pipeStream.hPipe);
		CloseHandle (pPDD->pipeStream.hEvent);
		pPDD->pipeStream.hEvent = 0;
	}

	// For USB 1.x, 1 mS frame rate 
	WORD wPacketSize = (WORD)dwBandwidth;

	// Find an interface that has the packet size we need
	lpIF = FindStreamInterface (pPDD, nInterface, TRUE, wPacketSize);

	// See if interface not found
	if (lpIF == 0)
		return -1;

	hTransfer = pDrv->lpUsbFuncs->lpSetInterface (pDrv->hDevice, NULL, NULL, 0, nInterface, 
	                                              lpIF->ifDesc.bAlternateSetting);
	if (hTransfer == 0)
	{
		DEBUGMSG (ZONE_ERROR, (TEXT("Error calling SetInterface rc=%d\r\n"), GetLastError()));
		return GetLastError();
	}
	if (!CloseTransferHandle (pDrv->lpUsbFuncs, hTransfer))
		rc = GetLastError();

	rc = 0;
	// Open a pipe to the end point
	if (lpIF->fEndpoint)
	{

		// Open the pipe
		pPDD->pipeStream.hPipe = pDrv->lpUsbFuncs->lpOpenPipe (pDrv->hDevice, &lpIF->epDesc);
		// Check for sucessful open
		if (pPDD->pipeStream.hPipe)
		{
			// Copy some pipe attributes
			pPDD->pipeStream.ucAddr = lpIF->epDesc.bEndpointAddress;
			pPDD->pipeStream.wPacketSize = lpIF->epDesc.wMaxPacketSize;

			// First, create an event for handshaking
			pPDD->pipeStream.hEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
		} 
		else
		{
			rc = GetLastError();
			if (rc == 0) rc = ERROR_NOT_ENOUGH_MEMORY; // Sometimes, there isn't an extended error here
			DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error opening pipe  rc: %d\r\n"), rc));
		}
	}
	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error in SetStreamInterface  rc: %d  ExtErr: %d\r\n"), rc, GetLastError()));
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("SetStreamInterface--  rc %d\r\n"), rc));
	return rc;
}
//-----------------------------------------------------------------------
// ParseStreamInterfaces - Walks the camera's USB interface descriptors
// to determine the supported freatures of the camera.
//

#define USB_CONFIGURATION_SIZE_5  28
#define USB_CONFIGURATION_SIZE_6  32

BOOL ParseStreamInterfaces (PPDDCONTEXT pPDD, LPCUSB_DEVICE lpUsbDev, BYTE bIFCtl,
							BYTE bIFSubCtl, BYTE bIFStrm, BYTE bIFSubStrm)
{
    DEBUGMSG (ZONE_USBLOAD, (DTAG TEXT("ParseStreamInterfaces++\r\n")));

    DEBUGMSG (ZONE_USBLOAD, (DTAG TEXT("Looking for Ctl IF %d %d and Stream IF %d %d\r\n"), bIFCtl, bIFSubCtl, bIFStrm, bIFSubStrm));
	__try {
	
//(ce 6)		if (lpUsbDev->lpConfigs->dwCount != sizeof (USB_CONFIGURATION))

⌨️ 快捷键说明

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