📄 pddvclas.cpp
字号:
{
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)
{
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetStillImage-- Frame %d Fmt %d\r\n"),
nFrameIndex, nFormatIndex));
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))
{
DEBUGMSG(ZONE_ERROR | ZONE_STILL, (TEXT("Still CS being held when geting a still!\n")));
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(ZONE_STILL, (TEXT("StartVidStream returned %d.\n"), rc));
Sleep (300); // 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
{
// If using internal stream for still, shut it down. Folks don't like it
// to run.
if ((DWORD)pPDD->pstrStream == (DWORD)&pPDD->strStreamDefault)
pdd_StopVidStream (pDrv);
// Leave the critical section
LeaveCriticalSection (&pPDD->csStill);
}
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("pdd_GetStillImage-- rc %d\r\n"), rc));
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] = 0x00; //"Full" power mode
else
buff[0] = 0x01; //"Vendor specific" (lower) power mode
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.
//
int DoVendorTransfer (PDRVCONTEXT pDrv, BYTE bRequest, BYTE bCmd, BYTE bInterface,
BYTE bUnit, PBYTE pVal, WORD wLen)
{
USB_DEVICE_REQUEST req;
DWORD dwBytes;
DWORD dw, dwErr;
BOOL fSet;
static BYTE bTBuff[512];
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("DoVendorTransfer++\r\n")));
if (bRequest == USBVID_SET_CUR)
fSet = TRUE;
else
fSet = FALSE;
// Get our pdd specific context
PPDDCONTEXT pPDD = (PPDDCONTEXT)pDrv->dwPddContext;
//
// Set transfer flags depending on read or write
//
if (fSet)
req.bmRequestType = USB_REQUEST_HOST_TO_DEVICE | USB_REQUEST_CLASS | USB_REQUEST_FOR_INTERFACE;
else
req.bmRequestType = USB_REQUEST_DEVICE_TO_HOST | USB_REQUEST_CLASS | USB_REQUEST_FOR_INTERFACE;
req.bRequest = bRequest;
req.wValue = MAKEWORD (0, bCmd);
req.wIndex = MAKEWORD (bInterface, bUnit);
req.wLength = wLen;
dwBytes = 0;
dwErr = USB_NO_ERROR;
dw = IssueVendorTransfer (pDrv->lpUsbFuncs, pDrv->hDevice,
DefaultTransferComplete, pPDD->hVendorEvent,
(fSet ? USB_OUT_TRANSFER : USB_IN_TRANSFER) | USB_SHORT_TRANSFER_OK,
&req, fSet && (req.wLength < sizeof (bTBuff)) ? pVal : bTBuff,
NULL, &dwBytes, 2000, &dwErr);
if (dw)
DEBUGMSG (ZONE_ERROR,
(DTAG TEXT("Error calling IssueVendorTransfer rc: %d ExtErr: %d\r\n"),
dw, GetLastError()));
// If no data transferred, stuff in an error
if (!dw && (dwBytes != wLen))
{
DEBUGMSG (ZONE_ERROR,
(DTAG TEXT("IssueVendorTransfer returned different length than requested len: %d expected: %d\r\n"),
dwBytes, wLen));
dw = ERROR_BAD_LENGTH;
}
// Copy data to output buffer
if (!dw && !fSet && (dwBytes <= req.wLength))
memcpy (pVal, bTBuff, dwBytes);
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("DoVendorTransfer-- rc %d\r\n"), dw));
return dw;
}
//-----------------------------------------------------------------------
// GetVendorTransferLen - Called to query the data length request
// for a particular command.
//
int GetVendorTransferLen (PDRVCONTEXT pDrv, BYTE bCmd, BYTE bInterface,
BYTE bUnit, WORD *pwLen)
{
USB_DEVICE_REQUEST req;
DWORD dwBytes;
DWORD dw, dwErr;
static BYTE bTBuff[512];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -