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

📄 pddvclas.cpp

📁 USB Webcam driver for Windows CE,修正前版本一些问题,支持CE6.0。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	{
		// Start of read thread if not already started
		if (pPDD->wReadThreadState == STREAMTHD_STOPPED)
		{
			// Create the thread
			pPDD->wReadThreadState = 1;
			pPDD->hReadThread = CreateThread (NULL, 0, ReadIsocThread, (PVOID)pDrv, 0, NULL);
			if (pPDD->hReadThread == 0)
			{
				DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error creating read thread  ExtErr: %d\r\n"), GetLastError()));
				pPDD->wReadThreadState = 0;
				rc = -1;
			} else	
				Sleep (10);
		}
	}
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("StartVidStream--  rc %d\r\n"), rc));
	return rc;
}	
//---------------------------------------------------------------------------------------
// pdd_GetNextVideoFrame - Returns the latest valid frame in the frame buffer.
//
// Note: Entrypoint modified in 0.6 to change the way we request the type of frame.
//
// Note on frame structure:
//    pBuff == 0            -> Buffer 'held' by app
//    dwTicksAtCapture == 0 -> Buffer not 'valid' perhaps due to purge request by app
//
int pdd_GetNextVideoFrame (PDRVCONTEXT pDrv, PBYTE *ppFrameBuff, DWORD *pdwFrameBytes, 
						   DWORD *pdwValidFrames, PBYTE pFrameReturn, 
						   DWORD dwTimeout, DWORD dwFrameFlags)
{
	int rc = 0; 
	DWORD i;
	BOOL fLeaveCS = TRUE;
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetNextVideoFrame++\r\n")));

	PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext; 

	// Sync with read thread
	EnterCriticalSection (&pPDD->pstrStream->csBuffSync);
	__try 
	{
		//
		// See if there is a frame to return
		//
		if (pFrameReturn != 0)
		{
			// Trivial sanity check for returned buffer. Buffer addresses will always
			// be above the first entry in the buffer array.
			if ((DWORD)pFrameReturn < (DWORD)&pPDD->pstrStream->pFrame[0].pBuff)
			{
				rc = ERROR_INVALID_PARAMETER;
				DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Returned buffer pointer %08x not valid \r\n"), 
				          pFrameReturn));
				__leave;
			}
			// Find a free slot in the array
			for (i = 0; i < pPDD->pstrStream->dwNumBuffs; i++)
			{
				// If slot free, add the buffer back.  Correct for packet header offset
				if (pPDD->pstrStream->pFrame[i].pBuff == 0)
				{
					pPDD->pstrStream->pFrame[i].pBuff = pFrameReturn - sizeof (USBVIDPAYLOADHDR);
					pPDD->pstrStream->pFrame[i].dwTicksAtCapture = 0;
				}
			}
		}
		//
		// See if they want a new frame
		//
		if (ppFrameBuff != 0)
		{
			int nCnt = 0;
			int nFrameToGrab = -1;
			int nOldest = -1, nNewest = 0x7fffffff;
			DWORD dwOldestTicks = 0, dwNewestTicks = 0, dwCurTicks = GetTickCount();

			// See if we have enough buffers remaining for this task. We
			// need to leave at least 2 buffers in the list
			for (i = 0; (i < pPDD->pstrStream->dwNumBuffs); i++)
			{
				// If slot used, count it.
				if (pPDD->pstrStream->pFrame[i].pBuff != 0) 
				{
					nCnt++;
				    if (pPDD->pstrStream->pFrame[i].dwTicksAtCapture != 0)
					{
						// Find oldest valid frame.
						if (dwCurTicks - pPDD->pstrStream->pFrame[i].dwTicksAtCapture > dwOldestTicks)
						{
							dwOldestTicks = dwCurTicks - pPDD->pstrStream->pFrame[i].dwTicksAtCapture;
							nOldest = i;
						}
						if (dwCurTicks - pPDD->pstrStream->pFrame[i].dwTicksAtCapture < dwNewestTicks)
						{
							dwNewestTicks = dwCurTicks - pPDD->pstrStream->pFrame[i].dwTicksAtCapture;
							nNewest = i;
						}
					}
				}
			}
			if (nCnt < 2)
			{
				rc = ERROR_NOT_ENOUGH_MEMORY;
				DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Not enough free buffers to return one\r\n")));
				__leave;
			}
			// See if they want the oldest, newest avail, or they want to wait...
			if (dwFrameFlags & FRAME_OLDEST) 
			{
				// If there is an oldest frame
				if (nOldest != -1)
				{
					nFrameToGrab = nOldest;
				}
			} 
			else if (dwFrameFlags & FRAME_NEWESTAVAIL)
			{
				// If there is an newest frame
				if (nNewest != -1)
				{
					nFrameToGrab = nNewest;
				}
			}			
			// See if we need to wait for a frame either because they want to wait 
			// on the next frame or because there isn't an oldest frame.
			if (nFrameToGrab == -1)
			{
				// We need to leave the CS to allow the isoc thread to complete
				// a frame.
				LeaveCriticalSection (&pPDD->pstrStream->csBuffSync);
				fLeaveCS = FALSE;

				// Wait for next frame event
				rc = WaitForSingleObject ((HANDLE)pPDD->pstrStream->dwReserved, dwTimeout);
				if (rc == WAIT_OBJECT_0)
				{
					// Retake the CS so we can access the struct again
					EnterCriticalSection (&pPDD->pstrStream->csBuffSync);

					nFrameToGrab = pPDD->pstrStream->dwLastGoodBuff;
					fLeaveCS = TRUE;
				} 
				else
				{
					DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Next Frame event not signaled rc %d\r\n"), rc));
					__leave;
				}
			}

			// Get lastest buffer, and its size. Skip past the frame header
			*ppFrameBuff = pPDD->pstrStream->pFrame[nFrameToGrab].pBuff + sizeof (USBVIDPAYLOADHDR);
			*pdwFrameBytes = pPDD->pstrStream->pFrame[nFrameToGrab].dwSize;

			// Take it out of the list
			pPDD->pstrStream->pFrame[nFrameToGrab].pBuff = 0;

			// Tell the caller how many frames have gone by since last call
			if (pdwValidFrames)
				*pdwValidFrames = pPDD->pstrStream->dwValidFrames;
			pPDD->pstrStream->dwValidFrames = 0;

			// See if we need to purge the other frames.
			if (dwFrameFlags & FRAME_PURGEOLD)
			{
				for (i = 0; (i < pPDD->pstrStream->dwNumBuffs); i++)
					pPDD->pstrStream->pFrame[i].dwTicksAtCapture = 0;
			}
		}
	}
	__finally 
	{
		if (fLeaveCS)
			LeaveCriticalSection (&pPDD->pstrStream->csBuffSync);
	}

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetNextVideoFrame--  rc %d\r\n"), rc));
	return rc;
}
//-----------------------------------------------------------------------
// pdd_StopVidStream - Set streaming video from camera
// 
int pdd_StopVidStream (PDRVCONTEXT pDrv) 
{
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_StopVidStream++\r\n")));

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

	StopReadThread (pPDD);
	pPDD->wCurrFormatIndex = 0xffff;
	pPDD->wCurrFrameIndex = 0xffff;

	// Change to null interface
	SetStreamInterface (pDrv, 1, 0);  

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_StopVidStream--\r\n")));
	return 0;
}
//---------------------------------------------------------------------------------------
// pdd_GetStillImage - Captures a single frame of video.  
//
int pdd_GetStillImage (PDRVCONTEXT pDrv, int nFrameIndex, int nFormatIndex, PBYTE pOut, 
					   DWORD dwOut, PDWORD pdwBytesWritten)
{
	PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext; 
	int rc = 0;

	// If we can't enter the still cap CS, we're already capturing an image, so fail
	if (!TryEnterCriticalSection (&pPDD->csStill))
	{
		return ERROR_BUSY;
	}
	__try
	{
		// See if stream running, or if simply an internal stream
		if ((pPDD->wReadThreadState == STREAMTHD_STOPPED) ||
			((DWORD)pPDD->pstrStream != (DWORD)&pPDD->strStreamDefault))
		{
			// If current stream doesn't match, change it.
			if ((nFormatIndex != pPDD->wCurrFormatIndex) || (nFrameIndex != pPDD->wCurrFrameIndex))
			{
				// According to the Vid Class spec, the video stream should be running for a still capture
				rc = pdd_StartVidStream (pDrv, nFormatIndex, nFrameIndex, NULL, -1); 
				DEBUGMSG( 1, (TEXT("StartVidStream returned %d.\n"), rc)); 
				Sleep (500); // Let the camera settle a bit
			}
		}
		if (rc)
			__leave;

		PBYTE pFrameBuff;
		DWORD dwSize;
		// Get the next frame
		rc = pdd_GetNextVideoFrame (pDrv, &pFrameBuff, &dwSize, 0, 0, 2000, 0);
		if (rc == 0)
		{
			__try
			{
				// Copy the frame to the output buffer
				memcpy (pOut, pFrameBuff, dwSize);
				*pdwBytesWritten = dwSize;
			}
			__except (EXCEPTION_EXECUTE_HANDLER)
			{
				DEBUGMSG (ZONE_ERROR, (TEXT("Exception writing to output buffer\r\n")));
				rc = ERROR_INVALID_PARAMETER;
			}
			// Return the frame buffer pointer
			pdd_GetNextVideoFrame (pDrv, 0, &dwSize, 0, pFrameBuff, 0, 0);
		}
	}
	__finally
	{
		// Leave the critical section
		LeaveCriticalSection (&pPDD->csStill);
	}
	return rc;
}
//-----------------------------------------------------------------------
// pdd_WaitOnCameraEvent - Set streaming video from camera
// 
int pdd_WaitOnCameraEvent (PDRVCONTEXT pDrv, DWORD dwEventMask, DWORD dwTimeout, 
						   DWORD *pdwSignaledEvent)
{
	HANDLE hHandles[32];
	DWORD dwWaitFlags[32];  
	int rc, nCnt;

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

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

	// Add handles to the array
	nCnt = 0;
	if (dwEventMask & WAITCAM_NEWFRAME) 
	{
		hHandles[nCnt] = pPDD->hNewFrameEvent;
		dwWaitFlags[nCnt] = WAITCAM_NEWFRAME;
		nCnt++;
	}
	// Add new events here...

	// Now wait if at least one flag was set.
	*pdwSignaledEvent = 0;
	if (nCnt) 
	{
		// Time to wait
		rc = WaitForMultipleObjects (nCnt, hHandles, FALSE, dwTimeout);

		// Set which event was signaled.
		if ((rc >= WAIT_OBJECT_0) && (rc < WAIT_OBJECT_0 + nCnt))
		{
			*pdwSignaledEvent |= dwWaitFlags[rc - WAIT_OBJECT_0];
		}
		else if (rc == WAIT_TIMEOUT)
		{
			*pdwSignaledEvent = WAITCAM_WAITTIMEOUT;
		}
	}
	else
	{
		DEBUGMSG (ZONE_ERROR, (TEXT("No wait flags set.\r\n")));
		rc = ERROR_INVALID_PARAMETER;
	}
	
	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_WaitOnCameraEvent-- rc = %x\r\n"), rc));
	return 0;
}

//-----------------------------------------------------------------------
// GetPower - Gets the camera power 
// 
int GetPower (PDRVCONTEXT pDrv, BOOL *pbVal)
{
	int rc = 0;
	BYTE bInterface = VID_IF_CTL;
	BYTE bUnit = 0;
	BYTE bVal;

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

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

	rc = DoVendorTransfer (pDrv, USBVID_GET_CUR, USB_VIDEO_VC_CS_VIDEO_POWER_MODE_CONTROL,
	                       bInterface, bUnit, &bVal, 1);

	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error reading power setting from camera rc %d\r\n"), rc));
	if (bVal & 1)
		*pbVal = TRUE;
	else
		*pbVal = FALSE;

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("GetPower-- rc %d error code %d\r\n"), rc, bVal));
	return rc;
}

//-----------------------------------------------------------------------
// SetPower - Sets the camera power 
// 
int SetPower (PDRVCONTEXT pDrv, BOOL fOn)
{
	int rc = 0;
	BYTE buff[64];
	BYTE bInterface = VID_IF_CTL;
	BYTE bUnit = 0;

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

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

	if (fOn)
		buff[0] = 0x01;
	else
		buff[0] = 0x00;

	rc = DoVendorTransfer (pDrv, USBVID_SET_CUR, USB_VIDEO_VC_CS_VIDEO_POWER_MODE_CONTROL,
	                       bInterface, bUnit, (PBYTE)buff, 1);

	if (rc)
		DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Error set camera power rc %d\r\n"), rc));

	DEBUGMSG (ZONE_FUNC, (DTAG TEXT("SetPower-- rc %d error code %d\r\n"), rc, buff[0]));
	return rc;
}
//-----------------------------------------------------------------------
// DoVendorTransfer - Called to communicate with the camera.  Since I've
// noticed that the camera sometimes overruns the output buffer, this 
// function pipes the data into a fairly big buffer and then copies the
// data to the exact size buffer passed.

⌨️ 快捷键说明

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