text: #include "StdAfx.h"
  #include <stdio.h>
  
  #include "AgoraObject.h"
  
  CAgoraObject *CAgoraObject::m_lpAgoraObject = NULL;
  IRtcEngine *CAgoraObject::m_lpAgoraEngine = NULL;
  CAGEngineEventHandler CAgoraObject::m_EngineEventHandler;
  
  CAgoraObject::CAgoraObject(void)
  	: m_dwEngineFlag(0)
  	, m_bVideoEnable(FALSE)
  	, m_bLocalAudioMuted(FALSE)
  {
  	m_strChannelName.Empty();
  	m_bLocalVideoMuted = FALSE;
  }
  
  CAgoraObject::~CAgoraObject(void)
  {
  }
  
  CString CAgoraObject::GetSDKVersion()
  {
  	int nBuildNumber = 0;
  	const char *lpszEngineVer = getAgoraRtcEngineVersion(&nBuildNumber);
  
  	CString strEngineVer;
  
  #ifdef UNICODE
  	::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpszEngineVer, -1, strEngineVer.GetBuffer(256), 256);
  	strEngineVer.ReleaseBuffer();
  #endif
  
  	return strEngineVer;
  }
  
  IRtcEngine *CAgoraObject::GetEngine()
  {
  	if(m_lpAgoraEngine == NULL)
  		m_lpAgoraEngine = (IRtcEngine *)createAgoraRtcEngine();
  
  	return m_lpAgoraEngine;
  }
  
  CAgoraObject *CAgoraObject::GetAgoraObject(LPCTSTR lpAppId)
  {
  	if (m_lpAgoraObject == NULL)
  		m_lpAgoraObject = new CAgoraObject();
  
  	if (m_lpAgoraEngine == NULL)
  		m_lpAgoraEngine = (IRtcEngine *)createAgoraRtcEngine();
  
  	if (lpAppId == NULL)
  		return m_lpAgoraObject;
  
  	RtcEngineContext ctx;
  
  	ctx.eventHandler = &m_EngineEventHandler;
  #ifdef UNICODE
  	char szAppId[128];
  
  	::WideCharToMultiByte(CP_ACP, 0, lpAppId, -1, szAppId, 128, NULL, NULL);
  	ctx.appId = szAppId;
  #else
  	ctx.appId = lpAppId;
  #endif
  
  	m_lpAgoraEngine->initialize(ctx);
  
  	return m_lpAgoraObject;
  }
  
  void CAgoraObject::CloseAgoraObject()
  {
  	if(m_lpAgoraEngine != NULL)
  		m_lpAgoraEngine->release();
  
  	if(m_lpAgoraObject != NULL)
  		delete m_lpAgoraObject;
  
  	m_lpAgoraEngine = NULL;
  	m_lpAgoraObject = NULL;
  }
  
  void CAgoraObject::SetMsgHandlerWnd(HWND hWnd)
  {
  	m_EngineEventHandler.SetMsgReceiver(hWnd);
  }
  
  HWND CAgoraObject::GetMsgHandlerWnd()
  {
  	return m_EngineEventHandler.GetMsgReceiver();
  }
  
  BOOL CAgoraObject::JoinChannel(LPCTSTR lpChannelName, UINT nUID,LPCTSTR lpToken)
  {
  	int nRet = 0;
  
  #ifdef UNICODE
  	CHAR szChannelName[128];
  	::WideCharToMultiByte(CP_UTF8, 0, lpChannelName, -1, szChannelName, 128, NULL, NULL);
  
  	char szToken[128];
  	::WideCharToMultiByte(CP_UTF8, 0, lpToken, -1, szToken, 128, NULL, NULL);
  
  	if(0 == _tcslen(lpToken))
  		nRet = m_lpAgoraEngine->joinChannel(NULL, szChannelName, NULL, nUID); 
  	else
  		nRet = m_lpAgoraEngine->joinChannel(szToken, szChannelName, NULL, nUID);
  #else
  	if(0 == _tcslen(lpToken))
  		nRet = m_lpAgoraEngine->joinChannel(NULL, lpChannelName, NULL, nUID);
  	else
  		nRet = m_lpAgoraEngine->joinChannel(lpToken, lpChannelName, NULL, nUID);
  #endif
  
  	if (nRet == 0)
  		m_strChannelName = lpChannelName;
  
  	return nRet == 0 ? TRUE : FALSE;
  }
  
  BOOL CAgoraObject::LeaveChannel()
  {
  	m_lpAgoraEngine->stopPreview();
  	int nRet = m_lpAgoraEngine->leaveChannel();
  
  	return nRet == 0 ? TRUE : FALSE;
  }
  
  CString CAgoraObject::GetChanelName()
  {
  	return m_strChannelName;
  }
  
  BOOL CAgoraObject::EnableVideo(BOOL bEnable)
  {
  	int nRet = 0;
  
  	if (bEnable)
  		nRet = m_lpAgoraEngine->enableVideo();
  	else
  		nRet = m_lpAgoraEngine->disableVideo();
  
  	if (nRet == 0)
  		m_bVideoEnable = bEnable;
  
  	return nRet == 0 ? TRUE : FALSE;
  }
  
  BOOL CAgoraObject::IsVideoEnabled()
  {
  	return m_bVideoEnable;
  }
  
  BOOL CAgoraObject::MuteLocalAudio(BOOL bMuted)
  {
  	ASSERT(m_lpAgoraEngine != NULL);
  
  	RtcEngineParameters rep(*m_lpAgoraEngine);
  
  	int ret = rep.muteLocalAudioStream((bool)bMuted);
  	if (ret == 0)
  		m_bLocalAudioMuted = bMuted;
  
  	return ret == 0 ? TRUE : FALSE;
  }
  
  BOOL CAgoraObject::IsLocalAudioMuted()
  {
  	return m_bLocalAudioMuted;
  }
  
  
  BOOL CAgoraObject::MuteLocalVideo(BOOL bMuted)
  {
  	ASSERT(m_lpAgoraEngine != NULL);
  
  	RtcEngineParameters rep(*m_lpAgoraEngine);
  
  	int ret = rep.muteLocalVideoStream((bool)bMuted);
  	if (ret == 0)
  		m_bLocalVideoMuted = bMuted;
  
  	return ret == 0 ? TRUE : FALSE;
  }
  
  BOOL CAgoraObject::IsLocalVideoMuted()
  {
  	return m_bLocalVideoMuted;
  }
  