📄 camerafunc.cpp
字号:
BOOL bResult;
EnterCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraCapture+\r\n")));
#ifdef USE_PRP_PHYSICAL_ADDRESS
if(bUsePhyAddr) {
// Don't use the DirectShow filter, just return the physical memory for VPU using
BOOL bDirectMode = TRUE;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_SET_DIRECT_CAPTURE_MODE, // I/O control code
&bDirectMode, // in buffer
sizeof(bDirectMode), // in buffer size
NULL, // out buffer
0, // out buffer size
&dwBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL
if (!bResult) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("LoadCamDriver: set bDirectDisplayMode failed!\r\n")));
return FALSE;
}
}
#endif
IOCTLInput.Set = CSPROPSETID_Connection;
IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
state = CSSTATE_PAUSE;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_PROPERTY, // I/O control code
&IOCTLInput, // in buffer
sizeof(CSPROPERTY), // in buffer size
&state, // out buffer
sizeof(CSSTATE), // out buffer size
&dwBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraCapture: PAUSE capture pin failed!\r\n")));
return FALSE;
}
state = CSSTATE_RUN;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_PROPERTY, // I/O control code
&IOCTLInput, // in buffer
sizeof(CSPROPERTY), // in buffer size
&state, // out buffer
sizeof(CSSTATE), // number of bytes returned
&dwBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraCapture: RUN Capture pin failed!\r\n")));
return FALSE;
}
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StartCameraCapture-\r\n")));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: StopCameraCapture
//
// Stop the camera capture pin.
//
// Parameters:
// None
//
// Returns:
// TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL StopCameraCapture()
{
CSPROPERTY IOCTLInput;
CSSTATE state;
DWORD dwBytesTransferred;
EnterCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraCapture+\r\n")));
IOCTLInput.Set = CSPROPSETID_Connection;
IOCTLInput.Id = CSPROPERTY_CONNECTION_STATE;
IOCTLInput.Flags = CSPROPERTY_TYPE_SET;
state = CSSTATE_STOP;
BOOL bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_PROPERTY, // I/O control code
&IOCTLInput, // in buffer
sizeof(CSPROPERTY), // in buffer size
&state, // out buffer
sizeof(CSSTATE), // out buffer size
&dwBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraCapture: STOP Capture pin failed!\r\n")));
return FALSE;
}
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_DEBUG_MSG, (TEXT("StopCameraCapture-\r\n")));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: PerFrameCameraCapture
//
// Output one frame through capture pin from camera
//
// Parameters:
// pbyDstBuffer
// [in]the address of output data buffer
//
// Returns:
// TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL PerFrameCameraCapture(UINT8 * pbyDstBuffer)
{
DWORD dBytesTransferred;
//UINT16 displayHeight;
//UINT16 displayWidth;
CS_MSGQUEUE_BUFFER CsMsgQBuff;
DWORD size, flags;
UINT8 *pbySrcBuffer;
BOOL bResult = FALSE;
CSBUFFER_INFO csBufferInfo1;
EnterCriticalSection(&gcsCapture);
csBufferInfo1.dwCommand = CS_ENQUEUE;
csBufferInfo1.pStreamDescriptor = gpCaptureStreamDesc;
while(1) {
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_BUFFERS, // I/O control code
&csBufferInfo1, // in buffer
sizeof(csBufferInfo1), // in buffer size
gpCaptureStreamDesc, // out buffer
sizeof(CS_STREAM_DESCRIPTOR), // out buffer size
&dBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
if(GetLastError()!= ERROR_SERVICE_NOT_ACTIVE) { //
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraCapture: Reading frame failed!\r\n")));
return FALSE;
}
else
Sleep(500);
continue;
}
break;
}
if (WaitForSingleObject(ghCaptureMsgQ, INFINITE) != WAIT_OBJECT_0) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraCapture: WaitForSingleObject Timeout!\r\n")));
return FALSE;
}
if(!ReadMsgQueue(ghCaptureMsgQ, &CsMsgQBuff, sizeof(CS_MSGQUEUE_BUFFER), &size, 10, &flags)) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG,(TEXT("PerFrameCameraCapture: unable to read Capture message queue. Try again.\r\n")));
return FALSE;
}
pbySrcBuffer = reinterpret_cast<PUINT8>(CsMsgQBuff.pStreamDescriptor->CsStreamHeader.Data);
LeaveCriticalSection(&gcsCapture);
memcpy(pbyDstBuffer,pbySrcBuffer,(UINT32)(CH2_CAPTURE_WIDTH * CH2_CAPTURE_HEIGHT * 1.5));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: GetPhysicalAddrPerFrame
//
// Output one frame through capture pin from camera
//
// Parameters:
// pbyDstBuffer
// [Out]Pointer to the PRP_BUFFER to store the physical address of
// Prp captured YUV data buffer
//
// Returns:
// TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL GetPhysicalAddrPerFrame(PPRP_BUFFER pPrpBuf)
{
DWORD dBytesTransferred;
CS_MSGQUEUE_BUFFER CsMsgQBuff;
DWORD size, flags;
UINT8 *pbySrcBuffer;
BOOL bResult = FALSE;
EnterCriticalSection(&gcsCapture);
DWORD command = CS_ENQUEUE;
while(1) {
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_BUFFERS, // I/O control code
&command, // in buffer
sizeof(DWORD), // in buffer size
gpCaptureStreamDesc, // out buffer
sizeof(CS_STREAM_DESCRIPTOR), // out buffer size
&dBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
if(GetLastError()!= ERROR_SERVICE_NOT_ACTIVE) { //
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraCapture: Reading frame failed!\r\n")));
return FALSE;
}
else
Sleep(500);
continue;
}
break;
}
if (WaitForSingleObject(ghCaptureMsgQ, INFINITE) != WAIT_OBJECT_0) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("PerFrameCameraCapture: WaitForSingleObject Timeout!\r\n")));
return FALSE;
}
if(!ReadMsgQueue(ghCaptureMsgQ, &CsMsgQBuff, sizeof(CS_MSGQUEUE_BUFFER), &size, 10, &flags)) {
LeaveCriticalSection(&gcsCapture);
RETAILMSG(CodecDemo_ERROR_MSG,(TEXT("PerFrameCameraCapture: unable to read Capture message queue. Try again.\r\n")));
return FALSE;
}
pbySrcBuffer = reinterpret_cast<PUINT8>(CsMsgQBuff.pStreamDescriptor->CsStreamHeader.Data);
LeaveCriticalSection(&gcsCapture);
memcpy(pPrpBuf,pbySrcBuffer,sizeof(PRP_BUFFER));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: ReturnPhysicalAddrPerFrame
//
// Release the frame buffer returned from GetPhysicalAddrPerFrame before to camera
//
// Parameters:
// pbyDstBuffer
// [Out]Pointer to the PRP_BUFFER to store the physica address of the
// frame buffer of YUV data buffer
//
// Returns:
// TRUE if successful; FALSE if failed.
//
//----------------------------------------------------------------------------
BOOL ReturnPhysicalAddrPerFrame(PPRP_BUFFER pPrpBuf)
{
#ifdef USE_PRP_PHYSICAL_ADDRESS
DWORD dBytesTransferred;
PRP_BUFFER PrpBuffer;
BOOL bResult = FALSE;
memcpy(&PrpBuffer, pPrpBuf, sizeof(PRP_BUFFER));
gpCaptureStreamDesc->CsFrameInfo.pPhysAddr = pPrpBuf->pPhysAddr;
gpCaptureStreamDesc->CsFrameInfo.pVirtAddr= pPrpBuf->Reserved;
EnterCriticalSection(&gcsCapture);
DWORD command = CS_RECYCBUFF;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_BUFFERS, // I/O control code
&command, // in buffer
sizeof(DWORD), // in buffer size
gpCaptureStreamDesc, // out buffer
sizeof(CS_STREAM_DESCRIPTOR), // out buffer size
&dBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult) {
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("ReturnPhysicalAddrPerFrame: return frame failed!\r\n")));
LeaveCriticalSection(&gcsCapture);
return FALSE;
}
LeaveCriticalSection(&gcsCapture);
#endif
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: UnloadCamDriver
//
// Release the Camera driver.
//
// Parameters:
// None
//
// Returns:
// TRUE if successful; FALSE if failed.
//
//-----------------------------------------------------------------------------
BOOL UnloadCamDriver()
{
DWORD dBytesTransferred;
BOOL bResult = TRUE;
CSBUFFER_INFO csBufferInfo;
StopCameraView();
StopCameraCapture();
EnterCriticalSection(&gcsPreview);
#if 0
// allocate buffer for capture pin
csBufferInfo.dwCommand = CS_DEALLOCATE;
csBufferInfo.pStreamDescriptor = gpPreviewStreamDesc;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_BUFFERS, // I/O control code
&csBufferInfo, // in buffer
sizeof(csBufferInfo), // in buffer size
gpPreviewStreamDesc, // out buffer
sizeof(CS_STREAM_DESCRIPTOR), // out buffer size
&dBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult){
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("UnloadCamDriver: allocate buffer for preview pin failed!\r\n")));
bResult = FALSE;
}
#endif
free(gpPreviewStreamDesc);
LeaveCriticalSection(&gcsPreview);
EnterCriticalSection(&gcsCapture);
csBufferInfo.dwCommand = CS_DEALLOCATE;
csBufferInfo.pStreamDescriptor = gpCaptureStreamDesc;
bResult = DeviceIoControl(ghCapturePinDll, // file handle to the driver
IOCTL_CS_BUFFERS, // I/O control code
&csBufferInfo, // in buffer
sizeof(csBufferInfo), // in buffer size
gpCaptureStreamDesc, // out buffer
sizeof(CS_STREAM_DESCRIPTOR), // out buffer size
&dBytesTransferred, // number of bytes returned
NULL); // ignored (=NULL)
if (!bResult){
RETAILMSG(CodecDemo_ERROR_MSG, (TEXT("UnloadCamDriver: allocate buffer for capture pin failed!\r\n")));
bResult = FALSE;
}
free(gpCaptureStreamDesc);
LeaveCriticalSection(&gcsCapture);
CloseHandle(ghPreviewPinDll);
CloseHandle(ghCapturePinDll);
CloseHandle(ghCameraDll);
if (FALSE == CloseMsgQueue(ghPreviewMsgQ))
bResult = FALSE;
if (FALSE == CloseMsgQueue(ghCaptureMsgQ))
bResult = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -