www.pudn.com > DVBPlayer.rar > DvbReciver.cpp, change:2004-07-15,size:4676b


// DvbReciver.cpp: implementation of the CDvbReciver class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "app.h" 
#include "DvbReciver.h" 
#include "mutex.h" 
#include "misc.h" 
#include "Mainfrm.h" 
#include "dsm.h" 
 
#include "tunerevent.h" 
#include "tuners.h" 
#include "tunerinfoevent.h" 
 
 
#include "hal-hi.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
using namespace std ; 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CDvbReciver::CDvbReciver() 
{ 
	m_eventQueue = new Queue < Event * >() ;  
	m_ui = NULL ;  
 
	is_tunered = TRUE ;  
} 
 
CDvbReciver::~CDvbReciver() 
{ 
	// Delete CIOs 
    if(m_uiList) 
    { 
        vector<UiInterface *>::iterator i = m_uiList->begin(); 
		 
        //for (; i != m_uiList->end(); i++) 
        //    delete *i;  
        delete m_uiList; 
		 
        m_uiList = NULL; 
    } 
 
 
	 
	TYPICAL_DELETE(m_eventSem); 
	TYPICAL_DELETE( m_eventQueue ) ;  
	if ( m_service_thread ) 
	{ 
		m_service_thread ->Join() ;  
		delete m_service_thread ;  
		m_service_thread = NULL ;  
	} 
 
} 
 
BOOL CDvbReciver::Init(CWnd* pwnd) 
{ 
 
#ifndef TEST 
	if ( 0 != InitCard() ) 
		return FALSE ;  
#endif 
	 
 
	m_eventSem  = new Semaphore () ;  
    m_uiList = new vector < UiInterface * >; 
	 
	m_ui  = new CMainFrame () ;  
	ASSERT(m_ui) ;  
	m_ui ->SetTarget(this) ;  
	RegisterActiveUI( m_ui ) ;  
	 
 
 
	char buff [MAX_PARAM_LENGTH] ;  
	GetModuleFileName( NULL, buff, MAX_PARAM_LENGTH ) ;  
	char* p = strrchr ( buff, '\\' ) ;  
	if (p) p[1] = '\0' ;  
 
 
	if ( !InitAllData((BYTE*)buff) ) 
		return FALSE ;  
 
 
	if ( !m_ui ->init() ) 
		return FALSE ;  
	RegisterActiveUI( &((CMainFrame*) m_ui)->m_ui_scan_transponder ) ;  
	 
 
	is_run = TRUE ;  
 
	// start service  
	m_service_thread = Thread::CreateThread() ;  
	m_service_thread ->Create( ServiceThreadFunc, this ) ;  
	 
 
	 
	return TRUE ;  
} 
 
int CDvbReciver::MainHandle() 
{ 
 
	//check if have event to handle  
	Event    *pC; 
	if ( m_eventQueue ->Peek() ) 
	{ 
		pC = m_eventQueue ->Read() ;  
		if ( pC ) 
			ServiceEvent (pC)  ; 
		return 1 ;  
 
	} 
 
	//tuner  
	if (!is_tunered) 
	{ 
		LockTuner( &tuner_param ) ;  
		return 1 ;  
	} 
 	    
	return 0 ;  
} 
 
DWORD CDvbReciver::AcceptEvent(Event * pc) 
{ 
 
	m_eventQueue ->Write( pc ) ;  
	m_eventSem->Signal() ;  
 
	return 0 ;  
} 
 
CWnd* CDvbReciver::GetMainUiWnd() 
{ 
	if ( m_ui ) 
	{ 
		return m_ui ->GetMainWnd () ;  
	} 
	else 
		return 0 ;  
} 
 
void CDvbReciver::Exit() 
{ 
 
	is_run = FALSE ;  
	ExitCard() ;  
	ExitAllData () ;  
 
} 
 
void CDvbReciver::Tuner(TunerAttr *p) 
{ 
//	LockTuner ( p ) ;  
 
	TunerParams t ;  
 
	t  .b12V			= 0 ;  
	t  .b22K			= p ->b22K ;  
	t  .bDisEqc			= p ->bDisEqc ;  
	t  .bDual_LNB_Flag	= p ->bDual_LNB_Flag ;  
	t  .bTone			= p ->bToneBurst ;  
	t  .bVOrH			= p ->bVOrH ;  
	t  .dwBZFreHi		= p ->dwFreLOF ;  
	t  .dwBZFreLo		= p ->dwFreLOF1 ;  
	t  .dwRecFrequency	= p ->dwFrequency ;  
	t  .dwSymbolRate	= p ->dwSymbolRate ;  
 
	if ( tuner_param != t ) 
	{ 
		tuner_param  = t ;  
 
 
		is_tunered = FALSE ;  
	} 
} 
 
int CDvbReciver::ServiceEvent(Event *p) 
{ 
	if (!p) 
		return 0; 
 
	int ret = 0 ;  
 
	switch(p ->type()) 
	{ 
	case CMD_TUNER: 
		Tuner( &((CTunerEvent*) p) ->m_tuner_param .s ) ; 
		break; 
 
	case CMD_EXIT: 
		ret = 1 ;  
		break; 
		 
	default: 
		ASSERT(FALSE) ;  
	} 
 
 
	delete p ;  
	return ret ;  
} 
 
void CDvbReciver::ServiceThreadFunc( void* pvoid ) 
{ 
	 
	int ret = 0 ;  
	Event    *pC; 
	    
 
	CDvbReciver* p_dvb = (CDvbReciver*) pvoid ;  
 
	while (p_dvb ->is_run)  
	{ 
 
		//handle tuner  
		BYTE sq = GetSignalQuality() ; 
		int     i; 
		TunerInfoEvent  te (sq) ;  
		 
		for (i = 0; i < p_dvb ->m_uiList->size(); i++) 
		{ 
			(*p_dvb ->m_uiList)[i]->AcceptEvent(&te); 
		} 
		 
		if (!p_dvb ->is_tunered) 
		{ 
			LockTuner( &p_dvb ->tuner_param ) ;  
 
			if ( GetLockStatus () ) 
			{ 
				p_dvb ->is_tunered = TRUE ; 				 
			} 
//			continue   ;  
		} 
		else 
		{ 
			Sleep ( 500 ) ;  
		} 
		 
		 
		if ( !p_dvb ->m_eventQueue || p_dvb ->m_eventQueue ->Peek() == NULL ) 
#if 0 
			p_dvb ->m_eventSem ->Wait () ;  
#else 
			;  
#endif 
			else 
			{ 
			 
 
		pC = p_dvb ->m_eventQueue ->Read() ;  
		ret = p_dvb ->ServiceEvent(pC)  ; 
			} 
		 
	} 
 
 
	 
	return ;  
} 
 
 
DWORD CDvbReciver::RegisterActiveUI(UiInterface *ui) 
{ 
	if (m_uiList && ui) 
	{ 
		m_uiList->push_back(ui); 
		return 0; 
	} 
	else 
	{ 
		return 255; 
	} 
 
}