Link to home
Start Free TrialLog in
Avatar of cskiong
cskiongFlag for Malaysia

asked on

Using OleLoadPicture to decode JPEG image stream from 2 web servers concurrently causing SLOW response???

Hi Experts,

I am using the following code to decode continuous JPEG image stream which I download from a web server.  It works fine when it is downloading the image stream from one web server.  However, the application runs slower and slower when I am downloading JPEG image stream from 2 web servers concurrently.

Sometimes the OleLoadPicture() will fail as well.  How should I improve the performance?

Your kind advice and suggesion are very much welcomed and appreciated.

Thank you!
void RefreshDisplay(BYTE* buf, DWORD len)
{
     LPPICTURE gpPicture = NULL;
     if (DecodeJPEG(buf, len, &gpPicture))
     {
           long hmWidth  = 0;
           long hmHeight = 0;
           gpPicture->get_Width(&hmWidth);
           gpPicture->get_Height(&hmHeight);
 
          CRect rectI = CRect(0, 0, 640, 480);
          rectI.NormalizeRect();
          int nWidth = rectI.Width();
          int nHeight= rectI.Height();
 
           // set the display base dimension
           CPoint ptCenter(width, height);
           RECT rc;
           GetClientRect(&rc);
           CDC* cdc = GetDC();
           HDC pDC = cdc->GetSafeHdc();
           gpPicture->Render(pDC,
                          0,
                          ptCenter.y,
                          nWidth,
                          -nHeight,
                          0,
                          0,
                          hmWidth,
                          hmHeight,
                          &rc);
 
           gpPicture->Release();
     }
}
 
BOOL CNWCameraViewer::DecodeJPEG(LPVOID buf, DWORD len, LPPICTURE* pgpPicture)
{
         HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len);
         size_t size = GlobalSize(m_hGlobal);
         if (size < len)
         {
               GlobalFree(m_hGlobal);
               m_hGlobal = GlobalAlloc(GMEM_MOVEABLE, len);
         }
 
         if (m_hGlobal != NULL)
         {
	LPVOID pvData = NULL;
	pvData = GlobalLock(m_hGlobal);
	memcpy(pvData, buf, len);
	GlobalUnlock(m_hGlobal);
 
	LPSTREAM pstm = NULL;
	HRESULT hr = CreateStreamOnHGlobal(m_hGlobal, TRUE, &pstm);
 
	if (!(SUCCEEDED(hr)))
	{
 	//AfxMessageBox (L"CreateStreamOnHGlobal() failed");
 
                                if (pstm != NULL)
	           {
		pstm->Release();
	           }
	            
		return FALSE;
	}
 
		// Create IPicture from image file
		if (*pgpPicture)
		{
			(*pgpPicture)->Release();
		}
 
		hr = ::OleLoadPicture(pstm,						  len,
			  FALSE,
			  IID_IPicture,
			  (LPVOID *)&(*pgpPicture));
 
		if (!(SUCCEEDED(hr)))
		{
    		         pstm->Release();
			//AfxMessageBox(L"Could not load image (hr failure)");
			return FALSE;
		}
 
		else if (*pgpPicture == NULL)
		{
    		         pstm->Release();
		         //AfxMessageBox(L"Could not load image (pgpPicture failure)");
			return FALSE;
		}
 
		pstm->Release();
		return TRUE;
	}
 
	return FALSE;
}

Open in new window

Avatar of DanRollins
DanRollins
Flag of United States of America image

My intuition is that the slowdown is not due to rendering limits, but to bandwidth limitations. Two continuous streams of jpg images can saturate many common Internet connections. Also, the server may throttle the output when it sees two connections from the same IP.  One way to check this is to use the Task Manager and watch the CPU meter. If it is pegging at 100, then the problem is that the CPU is overworked. Otherwise, it is probably a bandwidth issue.
I suggest that you test this by loading up several jpg files into memory and flipping through them as fast as possible. Now open another copy of the EXE and have it do the same thing. If the rendering is the issue, you will see a significant slowdown when the second (and third, etc.) copy of the program are running. If both (or multiple) copies of the testing program keep screaming along, then it is a bandwidth issue.
Avatar of cskiong

ASKER

Hi DanRollins,

Thanks for the suggestions and I have gone through the steps as you suggested.  So far this is what I found, when two JPEG streams are downloaded:
1. Sometimes the renderred images tend to be distorted.  When this happens, the CPU usage will shoot up to >75% from 15% (average).

2. Then the application will pause with error message access violation at Line 80 of the code attached with the question.

3. Once 1 and 2 happen, and I try to stop the debugging, the Visual Studio will response very slow.

My application is actually connecting two differenct web servers on the local network.  So I don't think it is the bandwidth issues.
ASKER CERTIFIED SOLUTION
Avatar of cskiong
cskiong
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please see:
   How do I close a question?
   http://www.experts-exchange.com/help.jsp?hi=407