//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: VideoCapture.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of VideoCapture.cpp //---------------------------------------------------------------------------- #include "stdafx.h" //#include "chicago.h" #include "VideoCapture.h" #include "VideoCaptureDevice.h" #include "CameraPTZ.h" //#include "VideoWnd.h" #include "SensorLGS.h" #include "..\..\LgLib\Version.h" #include "SensorArray.h" //#include "GenericArchive.h" #include "..\Chicago\Report.h" //#define WIDTHBYTES(bits) (((bits + 31) >> 5) << 2) #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif /* void RELEASE_TRACE( LPCTSTR pFormat, ... ) { TCHAR pBuffer[1024]; va_list pMarker; va_start( pMarker, pFormat ); _vsntprintf( pBuffer, 1024, pFormat, pMarker ); va_end( pMarker ); TCHAR pTimeBuffer[1024]; SYSTEMTIME st; ::GetLocalTime( &st ); _sntprintf( pTimeBuffer, 1024, _T("[%02d:%02d:%02d:%03d] %s"), st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, pBuffer ); ::OutputDebugString( pTimeBuffer ); } */ CVideoCapture::CVideoCapture() { m_pSensorArray = NULL; m_pReport = NULL; // m_pVideoCaptureDevice = NULL; ::ZeroMemory( &m_Render, sizeof( m_Render ) ); HWND hWnd = ::GetDesktopWindow(); HDC hDC = ::GetDC( hWnd ); m_Render.hDstDC = ::CreateCompatibleDC( hDC ); m_Render.hSrcDC = ::CreateCompatibleDC( hDC ); ::ReleaseDC( hWnd, hDC ); const cBitCount = 24; m_Render.nDstBmiSize = sizeof(BITMAPINFOHEADER) + ( ( cBitCount <= 8 ) ? sizeof(RGBQUAD) * ( 1 << cBitCount ) : 0 ); m_Render.pDstBmi = (LPBITMAPINFO) new BYTE[ m_Render.nDstBmiSize ]; m_Render.pDstBmi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); m_Render.pDstBmi->bmiHeader.biWidth = 320; m_Render.pDstBmi->bmiHeader.biHeight = 240; m_Render.pDstBmi->bmiHeader.biPlanes = 1; m_Render.pDstBmi->bmiHeader.biBitCount = cBitCount; m_Render.pDstBmi->bmiHeader.biCompression = 0; m_Render.pDstBmi->bmiHeader.biSizeImage = WIDTHBYTES( m_Render.pDstBmi->bmiHeader.biWidth * m_Render.pDstBmi->bmiHeader.biBitCount ) * m_Render.pDstBmi->bmiHeader.biHeight; m_Render.pDstBmi->bmiHeader.biClrImportant = 0; m_Render.pDstBmi->bmiHeader.biClrUsed = 0; m_Render.pDstBmi->bmiHeader.biXPelsPerMeter = 0; m_Render.pDstBmi->bmiHeader.biYPelsPerMeter = 0; /* for( int i = 0; i < 256; i++ ) { m_Render.pDstBmi->bmiColors[i].rgbRed = i; m_Render.pDstBmi->bmiColors[i].rgbGreen = i; m_Render.pDstBmi->bmiColors[i].rgbBlue = i; m_Render.pDstBmi->bmiColors[i].rgbReserved = 0; } */ m_Render.pDstBuffer = NULL; m_Render.hDstBitmap = ::CreateDIBSection( m_Render.hDstDC, m_Render.pDstBmi, DIB_RGB_COLORS, (void**)&m_Render.pDstBuffer, NULL, 0UL ); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CVideoCapture") ); } CVideoCapture::~CVideoCapture() { ::DeleteObject( m_Render.hDstBitmap ); delete [] (LPBYTE)m_Render.pDstBmi; ::DeleteDC( m_Render.hDstDC ); ::DeleteDC( m_Render.hSrcDC ); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::~CVideoCapture") ); } void CVideoCapture::Capture( CVideoCamera *pVideoCamera, LARGE_INTEGER liOffset ) { //find connected video input CVideoPin *pOutputPin = pVideoCamera->GetOutput(0); if( !pOutputPin ) { // camera already exist in capturing jobs list LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "camera %p has no output pin", pVideoCamera ); return; } // search for video input device while( pOutputPin ) { // get conneted pin CVideoPin *pInputPin = pOutputPin->GetCPin(); if( !pInputPin ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "camera %p has output pin that don't conneted to other pin", pVideoCamera ); return; } CVideoDevice *pVideoDevice = NULL; INT nInputPin = 0; pInputPin->GetVideoDevice( &pVideoDevice, &nInputPin ); if( !pVideoDevice ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::Capture", "input pin don't belong to any device" ); return; } // if it is video capture device for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ ) { CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice(i); if( pVideoDevice == pVideoCaptureDevice ) { // actual capturing LPBITMAPINFO pBmi = NULL; LPBYTE pBytes = NULL; HBITMAP hBitmap = NULL; pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap ); // image storing CAP_DATA cd; memset( &cd, 0, sizeof(cd) ); cd.liOffset = liOffset; cd.pCamera = pVideoCamera; StoreImage( pBmi, pBytes, cd ); // send image to main queue m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap ); // capture done LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p manual capture done", pVideoCamera ); return; } } // get associated pin pOutputPin = pInputPin->GetAPin(); } // capture done LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p manual capture can't be done", pVideoCamera ); } void CVideoCapture::Capture( CSensor* pSensor, CVideoCamera *pVideoCamera, BOOL bSnapShot, LARGE_INTEGER liOffset, double pan, double tilt, double zoom ) { // if there are any capture input and capture window if( m_pSensorArray->GetVideoCaptureDeviceCount() ) { if( bSnapShot ) { Capture( pVideoCamera, liOffset ); return; } if( pVideoCamera->GetType() == VC_PTZ ) { POSITION pos = m_List.GetHeadPosition(); while( pos ) { CAP_DATA cd = m_List.GetNext( pos ); if( cd.pCamera == pVideoCamera ) { // camera already exist in capturing jobs list LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "camera %p already busy with capture request", pVideoCamera ); return; } } } LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::Capture", "capture job for camera %p added to list", pVideoCamera ); CAP_DATA cd; cd.bSnapShot = bSnapShot; cd.pCamera = pVideoCamera; cd.pSensor = pSensor; // data for ptz camera cd.pan = pan; cd.tilt = tilt; cd.zoom = zoom; // data for store image cd.liOffset = liOffset; // start time cd.dwTime = ::GetTickCount(); // assume that we don't own camera yet cd.bOwn = FALSE; m_List.AddTail( cd ); } } void CVideoCapture::CheckState() { POSITION pos = m_List.GetHeadPosition(); while( pos ) { CAP_DATA cd = m_List.GetAt( pos ); DWORD dwTime = ::GetTickCount(); // check capture time limitation if( ( dwTime - cd.dwTime ) > MAX_CAPTURE_TIME ) { if( cd.bOwn ) { // release camera owning cd.pCamera->Unlock(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("Camera unlocked due remove capture job for camera %p by timeout"), cd.pCamera ); } // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("capture job for camera %p removed by timeout"), cd.pCamera ); POSITION temp = pos; // goto next capture job m_List.GetNext( pos ); // remove capture job m_List.RemoveAt( temp ); } else if( ( cd.pCamera->GetType() == VC_FIX ) && ( ( dwTime - cd.dwTime ) < ( cd.pSensor->GetImageCount() * 500UL / 2UL ) ) ) // MAX_FIXED_WAIT { // skip capturing for fixed camera, we will capture only after 2 sec upon request for capture appears LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "skip job for fixed camera" ); // goto next capture job m_List.GetNext( pos ); } else { CVideoCameraPTZ *pCameraPTZ; if( !cd.bOwn ) { if( !cd.pCamera->IsLock() ) { // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p locked", cd.pCamera ); // we need to prevent from using camera by someone else cd.pCamera->Lock(); // now we are owner of camera cd.bOwn = TRUE; // we MUST update data in list, because "cd" just a copy m_List.GetAt( pos ).bOwn = TRUE; // during snap shot camera already in position if( !cd.bSnapShot ) { switch( cd.pCamera->GetType() ) { case VC_FIX: break; case VC_PTZ: // LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CVideoCapture::CheckState"), _T("%0.0f,%0.0f,%0.0f"), cd.pan, cd.tilt, cd.zoom ); pCameraPTZ = static_cast(cd.pCamera); if( !pCameraPTZ->CmdSetPTZ( cd.pan, cd.tilt, cd.zoom ) ) LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p busy", cd.pCamera ); // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %p issued CmdSetPTZ", cd.pCamera ); break; default: // camera MUST has known type ASSERT( FALSE ); // log information LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::CheckState", "camera %p has unknown type %d", cd.pCamera, cd.pCamera->GetType() ); break; } } } } if( cd.bOwn ) { BOOL bCameraReady = TRUE; // during snap shot camera already in position if( !cd.bSnapShot ) { // camera is in position? switch( cd.pCamera->GetType() ) { case VC_FIX: break; case VC_PTZ: pCameraPTZ = static_cast(cd.pCamera); bCameraReady = pCameraPTZ->IsReadyPtz(); if( !bCameraReady ) { // pCameraPTZ->CmdGetPTZ(); // RELEASE_TRACE( "CAMERA NOT READY\n" ); } break; default: // camera MUST has known type ASSERT( bCameraReady = FALSE ); // log information LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CVideoCapture::CheckState", "camera %p has unknown type %d", cd.pCamera, cd.pCamera->GetType() ); break; } } if( bCameraReady ) { // find availible pin index for( INT i = 0; i < m_pSensorArray->GetVideoCaptureDeviceCount(); i++ ) { CVideoCaptureDevice *pVideoCaptureDevice = m_pSensorArray->GetVideoCaptureDevice(i); int nInputPin = pVideoCaptureDevice->GetAvailiblePin( cd.pCamera->GetOutput(0) ); if( nInputPin >= 0 ) { // force video connection if( cd.pCamera->Connect( pVideoCaptureDevice->GetInput( nInputPin ), FALSE ) ) { // assign current video camera to computer video input pVideoCaptureDevice->AssignVideoCamera( nInputPin, cd.pCamera ); LPBITMAPINFO pBmi = NULL; LPBYTE pBytes = NULL; HBITMAP hBitmap = NULL; if( cd.pCamera->GetType() == VC_FIX ) { for( INT nImage = cd.pSensor->GetImageCount() - 1; nImage >= 0; nImage-- ) { // actual capturing & image storing if( !pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap, nImage * 500UL ) ) continue; // store event StoreImage( pBmi, pBytes, cd ); // send image to main queue m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap ); } } else { // actual capturing & image storing if( pVideoCaptureDevice->Capture( nInputPin, &pBmi, &pBytes, &hBitmap ) ) { // store event StoreImage( pBmi, pBytes, cd ); // send image to main queue m_pSensorArray->GetEventQueue()->PostEventImageCapture( cd.pSensor, hBitmap ); } } // release camera owning cd.pCamera->Unlock(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "Camera %p unlocked after successefull capture", cd.pCamera ); // release video lines owning cd.pCamera->Disconnect(); // we need to update time in list, // so next time this node will be deleted m_List.GetAt( pos ).dwTime = dwTime - MAX_CAPTURE_TIME; // now we are not an owner of camera cd.bOwn = FALSE; // we MUST update data in list, because "cd" just a copy m_List.GetAt( pos ).bOwn = FALSE; // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "capture done for camera %d", cd.pCamera ); break; } else // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "camera %d could not connect to video input", cd.pCamera ); } else // log information LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CVideoCapture::CheckState", "video capture device % has no availible video inputs", pVideoCaptureDevice ); } } } // goto next capture job m_List.GetNext( pos ); } } } void CVideoCapture::StoreImage( LPBITMAPINFO pSrcBmi, LPBYTE pSrcBytes, CVideoCapture::CAP_DATA &cd ) { if( !pSrcBmi || !pSrcBytes ) { // log information LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CVideoCapture::StoreImage"), _T("can't store - bitmap properties is invalid (%p%p)"), pSrcBmi, pSrcBytes ); return; } ASSERT( m_pReport ); if( m_ImageJpeg.Create( pSrcBmi, pSrcBytes ) ) m_pReport->StoreImage( m_ImageJpeg, &cd.liOffset ); }