// videowndchn.cpp : implementation file
//

#include "stdafx.h"
#include "videowndchn.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define		VIDEOWND_STATIC	4600
/////////////////////////////////////////////////////////////////////////////
// VideoWndChn

VideoWndChn::VideoWndChn()
{
	m_hVideoWnd = NULL;
	m_lHandle   = 0;
	m_nStreamInx = -1;
}

VideoWndChn::~VideoWndChn()
{
	if(m_hVideoWnd)
	{
		m_hVideoWnd->DestroyWindow();
		delete m_hVideoWnd;
		m_hVideoWnd = NULL;
	}
}

BEGIN_MESSAGE_MAP(VideoWndChn, CWnd)
	//{{AFX_MSG_MAP(VideoWndChn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// VideoWndChn message handlers
void VideoWndChn::SetWndSize(int bAutoScan)
{
	if(!m_hVideoWnd)
		return;
	CRect  rcBkSize;
	float  fHWScale;
	int    nX, nY, nNewW, nNewH;
	MRAVSTREAM_T stStream;
	rcBkSize = m_clientRect;
	if(!m_lHandle || !bAutoScan || m_nStreamInx < 0)
	{
		m_hVideoWnd->MoveWindow(&rcBkSize);
		return;
	}
	if(MRNSDK_Stream_GetStream(m_lHandle, m_nStreamInx, &stStream) || stStream.codec_type != AVMEDIA_TYPE_VIDEO)
	{
		m_hVideoWnd->MoveWindow(&rcBkSize);
		return;
	}
	
	fHWScale = (float)stStream.nHeight / (float)stStream.nWidth;
	nNewW    = rcBkSize.Width();
	nNewH    = (int)((float)nNewW * fHWScale);
	if(nNewH > rcBkSize.Height())
	{
		nNewH = rcBkSize.Height();
		nNewW = (int)((float)nNewH / fHWScale);
	}
	nX = (rcBkSize.Width() - nNewW) / 2;
	nY = (rcBkSize.Height() - nNewH) / 2;
	m_hVideoWnd->MoveWindow(nX, nY, nNewW, nNewH);
}

