System Get Hang while running avi application in visual c++ 6.0

Aug 6 2010 2:07 AM
i m dhananjay from pune i m workin on
videostreaming application i wrote a whole code ,it run fine  on my
machine but when same code i try
to run on client machine the computer get hang means all i/o
operations get stuck n even task manager is also not open
plz help me if u have any solution
1) i wrote a application in visual c++ 6.0
2)i rad a data from cypress USB and disply it
3) i read a data in from of 512 packets
4)i use avi apli for windw
5) i used two thread one is for display and another tthread read a data from usb
please help me as early as possiable
/************************
herewith i copy paste my
code***************************************/
// Video_Streaming_Soft1.cpp : implementation file
//

#include "stdafx.h"
#include "high_frame_rate_board.h"
#include "Video_Streaming_Soft.h"
#include <string.h>
#include <vfw.h>
#include <time.h>
#include "CyApi.h"
#include "Wingdi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Video_Streaming_Soft dialog
//CCyUSBDevice *USBDevice = new CCyUSBDevice(NULL);
 extern class CCyUSBDevice *USBDevice;// Create an instance of CCyUSBDevice
unsigned long Exit_Thread_Code,Exit_Thread_Code2;
unsigned short* Input_Buff=new unsigned short[76801];
PUCHAR Bmp_Buff=new UCHAR[230401];
PUCHAR Bmp_Buff2=new UCHAR[230401];
PUCHAR Out_Buff1=new UCHAR[513];
bool Play_Stop=false;

unsigned int Store_Video1 = 0;
unsigned int Store_Video2 = 0;
unsigned int Display_Video = 0;
unsigned int Exit_All_Loops = 0;
time_t rawtime;
struct tm * timeinfo;

LONG inlen=153600,outlen=512;

IAVIFile *pfile = NULL;                         // created by CreateAvi
IAVIStream *ps = NULL;
IAVIStream *psCompressed = NULL;  // video stream, when first created



Video_Streaming_Soft::Video_Streaming_Soft(CWnd* pParent /*=NULL*/)
       : CDialog(Video_Streaming_Soft::IDD, pParent)
{
       //{{AFX_DATA_INIT(Video_Streaming_Soft)
       m_Save_Option = FALSE;
       //}}AFX_DATA_INIT
}


void Video_Streaming_Soft::DoDataExchange(CDataExchange* pDX)
{
       CDialog::DoDataExchange(pDX);
       //{{AFX_DATA_MAP(Video_Streaming_Soft)
       DDX_Control(pDX, IDC_SHOW_FRAME, m_Show_Frame);
       DDX_Check(pDX, IDC_SAVE_OPTION, m_Save_Option);
       //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Video_Streaming_Soft, CDialog)
       //{{AFX_MSG_MAP(Video_Streaming_Soft)
       ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
       //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Video_Streaming_Soft message handlers
BOOL Video_Streaming_Soft::OnInitDialog()
{
       CDialog::OnInitDialog();

       // Add "About..." menu item to system menu.

       // IDM_ABOUTBOX must be in the system command range.
       ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
       ASSERT(IDM_ABOUTBOX < 0xF000);

       CMenu* pSysMenu = GetSystemMenu(FALSE);
       if (pSysMenu != NULL)
       {
               CString strAboutMenu;
               strAboutMenu.LoadString(IDS_ABOUTBOX);
               if (!strAboutMenu.IsEmpty())
               {
                       pSysMenu->AppendMenu(MF_SEPARATOR);
                       pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX,
strAboutMenu);
               }
       }

       // Set the icon for this dialog.  The framework does this automatically
       //  when the application's main window is not a dialog
       SetIcon(m_hIcon, TRUE);                 // Set big icon
       SetIcon(m_hIcon, FALSE);                // Set small icon

       // TODO: Add extra initialization here
       SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
        //      Process
Priority
               //BITMAP bmpInfo;
       ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
       bmpInfo.bmiHeader.biPlanes              = 1;
       bmpInfo.bmiHeader.biWidth               = 320;
       bmpInfo.bmiHeader.biHeight              = 240;
       bmpInfo.bmiHeader.biCompression = BI_RGB;
       bmpInfo.bmiHeader.biBitCount    = 24;
       bmpInfo.bmiHeader.biSize                = sizeof(BITMAPINFOHEADER);
       bmpInfo.bmiHeader.biSizeImage   = (320*240*3);

       return TRUE;  // return TRUE  unless you set the focus to a control
}

void Video_Streaming_Soft::OnSysCommand(UINT nID, LPARAM lParam)
{
       if ((nID & 0xFFF0) == IDM_ABOUTBOX)
       {
       //      CAboutDlg dlgAbout;
               //dlgAbout.DoModal();
       }
       else
       {
               CDialog::OnSysCommand(nID, lParam);
       }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void Video_Streaming_Soft::OnPaint()
{
       if (IsIconic())
       {
               CPaintDC dc(this); // device context for painting

               SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

               // Center icon in client rectangle
               int cxIcon = GetSystemMetrics(SM_CXICON);
               int cyIcon = GetSystemMetrics(SM_CYICON);
               CRect rect;
               GetClientRect(&rect);
               int x = (rect.Width() - cxIcon + 1) / 2;
               int y = (rect.Height() - cyIcon + 1) / 2;

               // Draw the icon
               dc.DrawIcon(x, y, m_hIcon);
       }
       else
       {
               CDialog::OnPaint();
       }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR Video_Streaming_Soft::OnQueryDragIcon()
{
       return (HCURSOR) m_hIcon;
}

struct ThreadParam_Cypress
{
   HWND mDlg;    // Note: A handle.
       CWnd* pParent;
       Video_Streaming_Soft* mDlg_Handle;
};

struct ThreadParam_Display
{
   HWND mDlg;    // Note: A handle.
       CWnd* pParent;
       Video_Streaming_Soft* mDlg_Handle;
};


UINT Video_Stream(LPVOID param)  //<----for multithreaded Mouse
{
       ThreadParam_Cypress* thread_param =
static_cast<ThreadParam_Cypress*> (param);

       HWND hWnd =
::GetDlgItem(thread_param->mDlg_Handle->m_hWnd,IDC_PLAY_STOP);

       bool Success_In = false;

       register unsigned int min_val=0xFFFF,max_val=0;
       register unsigned int loop=0;
       register unsigned int step_factor=0,diff_val=0;
       register unsigned int bmp_ptr = 0;

       SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
//      SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
       Out_Buff1[0]=0xcd;
       Out_Buff1[1]=0xab;

       bool Success_Out=USBDevice->BulkOutEndPt->XferData(Out_Buff1,outlen);;
       if(!Success_Out)
       {
               Play_Stop = false;
               ::SetWindowText(hWnd,"Play");
               AfxMessageBox("Failed to Write to USB  ???",MB_ICONERROR);
               return 0;
       }

       HDC bDC_frame = ::GetDC(thread_param->mDlg_Handle->m_Show_Frame.m_hWnd);

       while(1)
       {
               inlen=153600;
               Success_In = USBDevice->BulkInEndPt->XferData((unsigned
char*)Input_Buff,inlen);
               if(!Success_In)
               {
                       Play_Stop = false;
                       AVIStreamRelease(ps);
                       AVIStreamRelease(psCompressed);
                       AVIFileRelease(pfile);

                       AVIFileExit();

                       ::SetWindowText(hWnd,"Play");
                       AfxMessageBox("Failed to Read From USB
???",MB_ICONERROR);
                       return 0;
               }

/*              min_val = Input_Buff[76799];
               max_val = Input_Buff[76798];

               step_factor = max_val - min_val;
               step_factor >>= 8;*/

               Display_Video = 1;
               Store_Video1 = 0;
               for(loop=0,bmp_ptr=0;loop<76800;loop++,bmp_ptr++)
               {
                       diff_val = (Input_Buff[loop]);
               //      Bmp_Buff[bmp_ptr] = diff_val;
               //      diff_val = (Input_Buff[loop]-min_val)/step_factor;
                       Bmp_Buff[bmp_ptr] = (diff_val & 0x001F)<<3;
                       Bmp_Buff[++bmp_ptr] =(diff_val & 0x03E0)>>2;
                       Bmp_Buff[++bmp_ptr]   =(diff_val & 0x7C00)>>7;

               }

               Store_Video1 = 1;
               Display_Video = 1;

               Sleep(8);

/////////////                                   Read 2nd Time
                                 ///////////

               inlen=153600;
               Success_In = USBDevice->BulkInEndPt->XferData((unsigned
char*)Input_Buff,inlen);
               if(!Success_In)
               {
                       Play_Stop = false;
                       AVIStreamRelease(ps);
                       AVIStreamRelease(psCompressed);
                       AVIFileRelease(pfile);

                       AVIFileExit();

                       ::SetWindowText(hWnd,"Play");
                       AfxMessageBox("Failed to Read From USB
???",MB_ICONERROR);
                       return 0;
               }

/*              min_val=Input_Buff[76799];
               max_val=Input_Buff[76798];

               step_factor = max_val - min_val;
               step_factor >>= 8;*/
               Store_Video2 = 0;
               for(loop=0,bmp_ptr=0;loop<76800;loop++,bmp_ptr++)
               {
               //      diff_val = (Input_Buff[loop]-min_val)/step_factor;
                       diff_val = (Input_Buff[loop]);
               //      Bmp_Buff2[bmp_ptr] = diff_val;
                       Bmp_Buff[bmp_ptr] = (diff_val & 0x001F)<<3;
                       Bmp_Buff[++bmp_ptr] = (diff_val & 0x03E0)>>2;
                       Bmp_Buff[++bmp_ptr]   =(diff_val & 0x7C00)>>7;

               }
               Store_Video2 = 1;
               Sleep(8);
       }
       return 0;
}

UINT Display_Save_Stream(LPVOID param)  //<----for multithreaded Mouse
{
       ThreadParam_Cypress* thread_param =
static_cast<ThreadParam_Cypress*> (param);

       HWND hWnd =
::GetDlgItem(thread_param->mDlg_Handle->m_hWnd,IDC_PLAY_STOP);
       HWND hWnd_Save_Option =
::GetDlgItem(thread_param->mDlg_Handle->m_hWnd,IDC_SAVE_OPTION);

       SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
       HDC bDC_frame = ::GetDC(thread_param->mDlg_Handle->m_Show_Frame.m_hWnd);
       ::EnableWindow(hWnd_Save_Option,0);

       Store_Video1=0;
       Store_Video2=0;
       Display_Video=1;

       if(thread_param->mDlg_Handle->m_Save_Option)
       {
               unsigned long m_lSample = 0;
        // Keeps track of the current Frame Index
               while(1)
               {
                       if(Store_Video1)
                       {

psCompressed->Write(m_lSample++,1,Bmp_Buff,230400,0,NULL,NULL);
                               Store_Video1 = 0;
                       }
                       if(Store_Video2)
                       {

psCompressed->Write(m_lSample++,1,Bmp_Buff2,230400,0,NULL,NULL);
                               Store_Video2 = 0;
                       }
                       if(Display_Video)
                       {
                               Display_Video = 0;

SetDIBitsToDevice(bDC_frame,0,0,320,240,0,0,0,240,Bmp_Buff,&(thread_param->mDlg_Handle->bmpInfo),DIB_RGB_COLORS);
                       }
                       if(Exit_All_Loops)
                               break;
               }
       }
       else
       {
               while(1)
               {
                       if(Display_Video)
                       {
                               Display_Video = 1;

SetDIBitsToDevice(bDC_frame,0,0,320,240,0,0,0,240,Bmp_Buff,&(thread_param->mDlg_Handle->bmpInfo),DIB_RGB_COLORS);
                       }
                       if(Exit_All_Loops)
                               break;
               }
       }
       return 0;
}

void Video_Streaming_Soft::OnPlayStop()
{
       // TODO: Add your control notification handler code here
       UpdateData(TRUE);

       if(!Play_Stop)
       {
               if(m_Save_Option)               //              AVI
Store Function
               {
                       char ptr_file_name[40]="";

                       time ( &rawtime );
                       timeinfo = localtime ( &rawtime );


sprintf(ptr_file_name,"Out_Stream_%d_%d_%d_%d_%d.avi",timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_mday,timeinfo->tm_mon+1,(timeinfo->tm_year-100));

                       FILE* fp = fopen(ptr_file_name,"wb+");
                       fclose(fp);

                       AVIFileInit();
                                // Initialise AVI Function

                       HRESULT hr =
AVIFileOpen(&pfile,ptr_file_name,OF_CREATE,NULL);
                       if (hr != AVIERR_OK)
                       {
                               Play_Stop = false;
                               AVIFileExit();
                               AfxMessageBox("Failed to Open AVI File
Function         ???",MB_ICONERROR);
                               return;
                       }
                       else
                       {
                               Play_Stop = true;
                               AVISTREAMINFO strhdr;
                // create the stream, if it wasn't there before
                               ZeroMemory(&strhdr,sizeof(strhdr));
                               strhdr.fccType = streamtypeVIDEO;// stream type
                               strhdr.fccHandler = mmioFOURCC('D','I','V','X');
                               strhdr.dwScale = 1;
                               strhdr.wPriority=1;
                               strhdr.dwRate = 50;
                               strhdr.dwSuggestedBufferSize  = 320*240*3;
                               SetRect(&strhdr.rcFrame, 0, 0, 320, 240);
                               _tcscpy(strhdr.szName, _T("Video Stream"));

                               hr=AVIFileCreateStream(pfile,&ps,&strhdr);
                               if (hr!=AVIERR_OK)
                               {
                                       Play_Stop = false;
                                       AVIFileExit();
                                       AfxMessageBox("Failed in
AVIFileCreateStream Function           ???",MB_ICONERROR);
                                       return;
                               }
                               else
                               {
                                       Play_Stop = true;
                                       AVICOMPRESSOPTIONS opts;
                                       ZeroMemory(&opts,sizeof(opts));
                                       opts.fccHandler =
mmioFOURCC('D','I','V','X');

opts.dwFlags=AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;//|AVICOMPRESSF_DATARATE;
                                       opts.dwKeyFrameEvery=1;

                                       hr =
AVIMakeCompressedStream(&psCompressed,ps,&opts,NULL);
                                       if (hr != AVIERR_OK)
                                       {
                                               Play_Stop = false;
                                               AVIFileExit();
                                               AfxMessageBox("Failed
in AVIMakeCompressedStream
Function                ???",MB_ICONERROR);
                                               return;
                                       }
                                       else
                                       {
                                               Play_Stop = true;
                                               hr =
AVIStreamSetFormat(psCompressed,0,(LPVOID)&bmpInfo,bmpInfo.bmiHeader.biSize);
                                               if (hr!=AVIERR_OK)
                                               {
                                                       Play_Stop = false;
                                                       AVIFileExit();

AfxMessageBox("Failed in AVIStreamSetFormat Function
???",MB_ICONERROR);
                                                       return;
                                               }
                                               else
                                               {
                                                       Play_Stop = true;
                                               }

                                       }
                               }
                       }
               }

               else
               {
                       Exit_All_Loops=0;
                       Play_Stop = true;
                       ThreadParam_Cypress* param = new ThreadParam_Cypress;
                       param->mDlg = GetSafeHwnd();
                       param->mDlg_Handle = this;

                       ThreadParam_Display* param2 = new ThreadParam_Display;
                       param2->mDlg = GetSafeHwnd();
                       param2->mDlg_Handle=this;

                       hDevice = USBDevice->DeviceHandle();
                       if(hDevice==INVALID_HANDLE_VALUE)
                       {
                               Play_Stop = false;
                               AfxMessageBox("Error in Opening Handle
",MB_ICONERROR);
                               return;
                       }
                       else
                       {
                               Play_Stop = true;

ThreadID=AfxBeginThread(Video_Stream,param,THREAD_PRIORITY_HIGHEST);
                               ThreadID->m_bAutoDelete=TRUE;

if(!GetExitCodeThread(ThreadID->m_hThread,&Exit_Thread_Code))
                               {
                                       MessageBox("GetExitCodeThread
Failed  !!!","Thread ID",MB_ICONERROR);
                                       return;
                               }
                               else
                               {

ThreadID_Display=AfxBeginThread(Display_Save_Stream,param2,THREAD_PRIORITY_NORMAL);
                                       ThreadID_Display->m_bAutoDelete=TRUE;

if(!GetExitCodeThread(ThreadID_Display->m_hThread,&Exit_Thread_Code2))
                                       {

MessageBox("GetExitCodeThread Failed  !!!","Thread ID",MB_ICONERROR);
                                               return;
                                       }
                                       else
                                       {

GetDlgItem(IDC_PLAY_STOP)->SetWindowText("Stop");
                                               param=0;
                                               param2=0;
                                       }
                               }
                       }
               }
       }
       else
       {
               GetDlgItem(IDC_SAVE_OPTION)->EnableWindow(1);
               outlen=512;
               bool
Success_Out=USBDevice->BulkOutEndPt->XferData(Out_Buff1,outlen);
               if(!Success_Out)
               {
                       AfxMessageBox("Failed to Write to USB
???",MB_ICONERROR);
                       return;
               }
               else
               {
                       Exit_All_Loops=1;
                       Play_Stop = false;
                       GetDlgItem(IDC_PLAY_STOP)->SetWindowText("Play");

                       Sleep(100);

if(!(::TerminateThread(ThreadID->m_hThread,Exit_Thread_Code)))
                       {
                               MessageBox("Failed to Kill Video Stream
Thread ????","Thread
ID",MB_ICONERROR);
                       }
                       else
                       {

                               USBDevice->BulkOutEndPt->Abort();
                               USBDevice->BulkInEndPt->Abort();

                               if(m_Save_Option)
                               {
                                       AVIStreamRelease(ps);
                                       AVIStreamRelease(psCompressed);
                                       AVIFileRelease(pfile);
                                       AVIFileExit();
                               }
                               else
                               {
                               }
                       }
               }
       }

}