📄 pddvclas.cpp
字号:
}
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[pPDD->pstrStream->dwLastGoodBuff].pBuff + sizeof (USBVIDPAYLOADHDR);
*pdwFrameBytes = pPDD->pstrStream->pFrame[pPDD->pstrStream->dwLastGoodBuff].dwSize;
// Take it out of the list
pPDD->pstrStream->pFrame[pPDD->pstrStream->dwLastGoodBuff].pBuff = 0;
// Tell the caller how many frames have gone by since last call
if (pdwValidFrames)
*pdwValidFrames = pPDD->pstrStream->dwValidFrames;
pPDD->pstrStream->dwValidFrames = 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, TRUE, &pFrameBuff, &dwSize, 0, 0, 2000);
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, TRUE, 0, &dwSize, 0, pFrameBuff, 0);
}
}
__finally
{
// Leave the critical section
LeaveCriticalSection (&pPDD->csStill);
}
return rc;
}
//-----------------------------------------------------------------------
// 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.
//
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))
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;
}
//-----------------------------------------------------------------------
// FindFeatureInfo - Look up feature in the table to get parameter
// information.
//
PFEATURESTRUCT FindFeatureInfo (BYTE bFeatureID)
{
int i;
for (i = 0; i < dim (CamFeatures); i++)
{
if (bFeatureID == CamFeatures[i].bFeatureID)
return &CamFeatures[i];
}
for (i = 0; i < dim (ProcFeatures); i++)
{
if (bFeatureID == ProcFeatures[i].bFeatureID)
return &ProcFeatures[i];
}
return 0;
}
//-----------------------------------------------------------------------
// EnableSupportedFeatures - Given a bit array from the interface
// descriptor, fill the feature table with the necessary data.
//
int EnableSupportedFeatures (PBYTE pCtlBytes, int nCtlBytes, PFEATURESTRUCT pFeatArray,
int nFeatArraySize, BYTE bUnit)
{
BYTE bm;
// Loop through, Check the bit for each feature
for (BYTE index = 0; index < nFeatArraySize; index++)
{
// Do we need another byte?
if ((index % 8) == 0)
{
// if no more bytes, leave
if (nCtlBytes == 0) return 0;
bm = *pCtlBytes++;
nCtlBytes--;
}
if (bm & 0x01)
pFeatArray[index].bUnit = bUnit;
else
pFeatArray[index].bFeatureID = FEAT_UNSUPPORTED;
bm = bm >> 1;
}
return 0;
}
//-----------------------------------------------------------------------
// ParseFeatureParameters - Parses the device interface descriptor to
// see what features this camera supports.
//
int ParseFeatureParameters (PPDDCONTEXT pPDD)
{
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("GetFeatureParameters++\r\n")));
// Find the Control descriptors
PUSBVIDCTLIFDESCRIPTOR pHdr = (PUSBVIDCTLIFDESCRIPTOR)pPDD->usbctlIF.lpepExtDesc;
__try {
// Sanity check on header IDs
if ((pHdr->bType != 0x24) || (pHdr->bSubtype != 1))
{
DEBUGMSG (ZONE_ERROR, (DTAG TEXT("Bad Extended Stream Descriptor\r\n")));
return -1;
}
PBYTE pData = (PBYTE)pHdr;
PBYTE pEnd = (PBYTE)pHdr + 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 (ZONE_ERROR, (TEXT("Unexpected header type %xh\r\n"), pStd->bType));
break;
}
switch (pStd->bSubtype)
{
case USB_VIDEO_VC_PROCESSING_UNIT:
// Verify structure length
if (pStd->bLen >= 0x0b)
{
BYTE bCtlSize = *(pData+7);
PBYTE pCtls = pData+8;
BYTE bUnit = *(pData+3);
EnableSupportedFeatures (pCtls, bCtlSize, ProcFeatures,
dim(ProcFeatures), bUnit);
}
break;
case USB_VIDEO_VC_INPUT_TERMINAL:
{
WORD wType = *(LPWORD)(pData+4);
// Verify that it's the camera
if (wType == USB_VIDEO_ITT_CAMERA)
{
// Verify structure length
if (pStd->bLen >= 0x11)
{
BYTE bCtlSize = *(pData+14);
PBYTE pCtls = pData+15;
BYTE bTerm = *(pData+3);
EnableSupportedFeatures (pCtls, bCtlSize, CamFeatures,
dim(CamFeatures), bTerm);
}
}
}
break;
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
DEBUGMSG (1, (TEXT("Exception scanning extended control descriptor\r\n")));
return -2;
}
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("GetFeatureParameters--\r\n")));
return 0;
}
//-----------------------------------------------------------------------
// ProcessFrameFormats - Helper function that examines the frame information
// for each format and returns information on the given frame.
//
int ProcessFrameFormats (PPDDCONTEXT pPDD, PUSBVIDSTDDESCHDR pStd, BYTE bDescID,
BYTE bFormatIndex, BYTE bFrameIndex,
PFORMATPROPS pProps, BOOL *pfFound)
{
PUSBVIDSTREAMIF_FORMATDESCRIPTOR pFmt = (PUSBVIDSTREAMIF_FORMATDESCRIPTOR)pStd;
BYTE bCnt = pFmt->bNumFrameDescriptors;
*pfFound = FALSE;
DEBUGMSG (ZONE_FUNC, (DTAG TEXT("ProcessFrameFormats++\r\n")));
if (bFrameIndex > bCnt)
return -1;
// See if this is the format we want
if (pFmt->bFormatIndex != bFormatIndex)
return 0;
// Get the pointer to the first frame descriptor
PUSBVIDSTREAMIF_MJPEGFRAMEDESCRIPTOR pFrmMJPEG = (PUSBVIDSTREAMIF_MJPEGFRAMEDESCRIPTOR)( (PBYTE)pStd+pStd->bLen);
if (pFrmMJPEG->bSubtype != bDescID)
{
DEBUGMSG (ZONE_ERROR, (DTAG TEXT("ERROR: Expected Frame descriptor to follow format descriptor\r\n")));
return -2;
}
// Loop through Frame descriptors looking for matching index
for (int i = 0; i < bCnt; i++)
{
// See if this is the descriptor we want
if (pFrmMJPEG->bFrameIndex == bFrameIndex)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -