⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tapi3eventnotification.cpp

📁 jtapi for telephone
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
	Copyright (c) 2005 Serban Iordache 
	
	All rights reserved. 
	
	Permission is hereby granted, free of charge, to any person obtaining a 
	copy of this software and associated documentation files (the 
	"Software"), to deal in the Software without restriction, including 
	without limitation the rights to use, copy, modify, merge, publish, 
	distribute, and/or sell copies of the Software, and to permit persons 
	to whom the Software is furnished to do so, provided that the above 
	copyright notice(s) and this permission notice appear in all copies of 
	the Software and that both the above copyright notice(s) and this 
	permission notice appear in supporting documentation. 
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 
	OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
	HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 
	INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 
	FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
	NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
	WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
	
	Except as contained in this notice, the name of a copyright holder 
	shall not be used in advertising or otherwise to promote the sale, use 
	or other dealings in this Software without prior written authorization 
	of the copyright holder.
*/
#include "stdafx.h"
#include <tapi3.h>
#include <control.h>
#include <strmif.h>

#include "TAPI3EventNotification.h"
#include "MSTapi3.h"
#include "Util.h"

TAPI3EventNotification::TAPI3EventNotification(MSTapi3* aMSTapi3, CallbackNotification aCallback) : 
		pTapi3(aMSTapi3), callback(aCallback) {
	logger = new Logger("TAPI3EventNotification");
}
// destructor
TAPI3EventNotification::~TAPI3EventNotification() {
	delete logger;
}
                
///////////////////////////////////////////////////////////////////
// CallEventNotification
//
// The only method in the ITCallEventNotification interface.  This gets
// called by TAPI 3.0 when there is a call event to report. This just
// posts the message to our UI thread, so that we do as little as
// possible on TAPI's callback thread.
//
///////////////////////////////////////////////////////////////////
HRESULT STDMETHODCALLTYPE TAPI3EventNotification::Event(TAPI_EVENT tapiEvent, IDispatch* pEvent) {
	try {
		if(pTapi3->isDown) {
			logger->warn("TAPI3EventNotification: TAPI already shut down.");
			return S_FALSE;
		}
		HRESULT hr;
		switch (tapiEvent) {
			case TE_CALLNOTIFICATION: {
				logger->debug("TE_CALLNOTIFICATION");
				// TE_CALLNOTIFICATION means that the application is being notified of a new call.
				ITCallNotificationEvent* pNotify;
				hr = pEvent->QueryInterface( IID_ITCallNotificationEvent, (void **)&pNotify );

				if (FAILED(hr)) {
					logger->error("Incoming call, but failed to get the interface");
				} else {
					logger->debug("Incoming call.");

					ITCallInfo* pCall;
					hr = pNotify->get_Call( &pCall );
					pNotify->Release();
					if (FAILED(hr)) {
						logger->error("Cannot retrieve call: hr=%08X.", hr);
						return hr;
					}
					// check to see if we own the call
					CALL_PRIVILEGE cp;
					hr = pCall->get_Privilege( &cp );
					if ( FAILED(hr) /* || (CP_OWNER != cp) */ ) {
						// just ignore it if we don't own it
						//logger->debug("Ignored event (we don't own it): hr=%08X.", hr);
						logger->debug("Ignored event (failed to get privilege): hr=%08X.", hr);
						pCall->Release();
						return S_OK;
					}

					CALL_STATE callState;
					hr = pCall->get_CallState(&callState);
					if(FAILED(hr)) {
						logger->error("Cannot retrieve call state: hr=%08X.", hr);
						pCall->Release();
						return hr;
					}
					logger->debug("Call state: %08X.", callState);

					wstring strAddress;
					hr = getAddress<ITCallInfo>(logger, pCall, strAddress);
					if(FAILED(hr)) {
						pCall->Release();
						return hr;
					}
					logger->info("CallNotification on address %S.", strAddress.c_str());						

					ITBasicCallControl* pCallControl;
					hr = pCall->QueryInterface( IID_ITBasicCallControl, (void**)&pCallControl );
					if(FAILED(hr)) {
						logger->error("Error retrieving CallControl: hr=%08X", hr);
						pCall->Release();
						return hr;
					}
					int callID = pTapi3->getOrCreateCallID(pCallControl);
					if(callID > 0) {
						pCallControl->Release();
					} else {
						callID = -callID;
					}
					wstring callInfo[4];
					bool hasCallInfo = getCallInfo(logger, pCall, callInfo);
					pCall->Release();
					int method = -1;
					switch(callState) {
						//case CS_IDLE: 
						//case CS_INPROGRESS: 
						case CS_CONNECTED: method = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_TERMINAL_CONNECTION_TALKING; break;
						//case CS_DISCONNECTED: 
						case CS_OFFERING: method = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_TERMINAL_CONNECTION_RINGING; break;
						case CS_HOLD: method = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_TERMINAL_CONNECTION_HELD; break;
						//case CS_QUEUED:
						default: break;
					}
					if(method == -1) {
						logger->warn("No callback for callState=%08X", callState);
					} else {
						logger->debug("Calling callback for method %d on %S with callID=%d", method, strAddress.c_str(), callID);
						callback(method, callID, strAddress, net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_JNI_CAUSE_NEW_CALL, 
							(hasCallInfo ? callInfo : NULL));
					}
				}
				break;
			}
        
			case TE_CALLSTATE: {
				logger->debug("TE_CALLSTATE");
				// TE_CALLSTATE is a call state event.  pEvent is an ITCallStateEvent
				ITCallStateEvent* pCallStateEvent;

				// Get the interface
				hr = pEvent->QueryInterface( IID_ITCallStateEvent, (void **)&pCallStateEvent );
				if (FAILED(hr)) {
					logger->error("Cannot get interface IID_ITCallStateEvent: hr=%08X", hr);
					return hr;
				}

				// get the CallState that we are being notified of.
				CALL_STATE cs;
				hr = pCallStateEvent->get_State(&cs);
				if (FAILED(hr)) {
					logger->error("get_State() failed: hr=%08X", hr);
					pCallStateEvent->Release();
					return hr;
				}

				ITCallInfo* pCall;
				hr = pCallStateEvent->get_Call(&pCall);
				if (FAILED(hr)) {
					logger->error("get_Call() failed: hr=%08X", hr);
					pCallStateEvent->Release();
					return hr;
				}

				wstring strAddress;
				hr = getAddress<ITCallInfo>(logger, pCall, strAddress);
				if(FAILED(hr)) {
					pCall->Release();
					pCallStateEvent->Release();
					return hr;
				}			
				logger->info("CallState on address %S.", strAddress.c_str());

				ITBasicCallControl* pCallControl;
				hr = pCall->QueryInterface(IID_ITBasicCallControl, (void**)&pCallControl);
				if (FAILED(hr)) {
					logger->error("Interface ITBasicCallControl not supported: hr=%08X.", hr);
					pCall->Release();
					pCallStateEvent->Release();
					return hr;
				}
				int callID = pTapi3->getCallID(pCallControl);
				pCallControl->Release();
				pCall->Release();
				pCallStateEvent->Release();
				logger->debug("CallState %d for callID %d.", cs, callID);
				int methodID = -1;
				switch(cs) {
					case CS_OFFERING: methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_CONNECTION_ALERTING; break;
					case CS_DISCONNECTED: pTapi3->ReleaseTheCall(callID); methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_CONNECTION_DISCONNECTED; break;
					//case CS_CONNECTED: methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_CONNECTION_CONNECTED; break;
					case CS_CONNECTED: methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_TERMINAL_CONNECTION_TALKING; break;
					// case CS_IDLE: logger->info("No callback for CS_IDLE."); break;
					case CS_INPROGRESS : methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_CONNECTION_IN_PROGRESS; break;
					case CS_HOLD: methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_TERMINAL_CONNECTION_HELD; break;
					// case CS_QUEUED: methodID = net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_METHOD_CONNECTION_???; break;
				}
				if(methodID == -1) {
					logger->warn("No callback for cs=%d.", cs);
				} else {
					// logger->debug("Calling callback for %d on %S with callID=%d", methodID, strAddress.c_str(), callID);
					callback(methodID, callID, strAddress, net_sourceforge_gjtapi_raw_tapi3_Tapi3Provider_JNI_CAUSE_NORMAL, NULL);
				}
				break;
			}
			case TE_ADDRESS: {
				logger->debug("TE_ADDRESS");
				ITAddressEvent* pAddressEvent;
				hr = pEvent->QueryInterface( IID_ITAddressEvent, (void **)&pAddressEvent );
				if (FAILED(hr)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -