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

cskiongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
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.
0
cskiongAuthor Commented:
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.
0
cskiongAuthor Commented:
Hi DanRollins,

I found out the issue causing the problem, it is due to my carelessness in releasing the wrong buffer.
According to MSDN, I should release pgpPicture instead of pstm.

Finally get the problem fixed.  Thank you!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanRollinsCommented:
Please see:
   How do I close a question?
   http://www.experts-exchange.com/help.jsp?hi=407
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.