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

📄 mstapi3.cpp

📁 jtapi for telephone
💻 CPP
📖 第 1 页 / 共 3 页
字号:
			logger->debug("Hold() returned %08X.", hr);
			if(hr == TAPI_E_NOTSUPPORTED) {
				logger->debug("Hold not supported. Trying to swap...");
				swapIt = true;
			}
		}
		if(swapIt) {
			bool found = false;
			CallMap::const_iterator it;
			for(it = calls.begin(); it != calls.end(); it++) {
				ITBasicCallControl* curCallControl = (*it).second;
				if(curCallControl == callControl) continue;

				ITCallInfo* pCallInfo;
				HRESULT hr1 = curCallControl->QueryInterface( IID_ITCallInfo, (void**)&pCallInfo );
				if(FAILED(hr1)) {
					logger->error("Getting callInfo failed. hr=%08X", hr1);
					continue;
				}
				CALL_STATE callState;
				hr1 = pCallInfo->get_CallState(&callState);
				pCallInfo->Release();
				if(FAILED(hr1)) {
					logger->error("Cannot retrieve call state: hr=%08X.", hr1);
					continue;
				}
				if(callState == CS_HOLD) {
					break;
				}
			}
			if(it == calls.end()) {
				logger->warn("Cannot swap. No call found in hold.");
			} else {
				logger->debug("Call %d found in hold. Swapping.", (*it).first);
				hr = callControl->SwapHold((*it).second);
			}
		}
        return hr;
    } else {
		logger->warn("Cannot hold. CallID %d not found.", callID);
		return E_FAIL;
	}
}

//////////////////////////////////////////////////////////////////////
// UnHoldTheCall
//
// Unholds the call
//////////////////////////////////////////////////////////////////////
HRESULT MSTapi3::UnHoldTheCall(int callID) {
	logger->debug("Unholding callID=%d...", callID);
	ITBasicCallControl* callControl = getCallControl(callID);
    if (NULL != callControl) {
		bool swapIt = swapOnHold;
		HRESULT hr;
		if(!swapIt) {
			hr = callControl->Hold(VARIANT_FALSE);
			logger->debug("UnHold() returned %08X.", hr);
			if(hr == TAPI_E_NOTSUPPORTED) {
				logger->debug("UnHold not supported. Trying to swap...");
				swapIt = true;
			}
		}
		if(swapIt) {
			bool found = false;
			CallMap::const_iterator it;
			for(it = calls.begin(); it != calls.end(); it++) {
				ITBasicCallControl* curCallControl = (*it).second;
				if(curCallControl == callControl) continue;

				ITCallInfo* pCallInfo;
				HRESULT hr1 = curCallControl->QueryInterface( IID_ITCallInfo, (void**)&pCallInfo );
				if(FAILED(hr1)) {
					logger->error("Getting callInfo failed. hr=%08X", hr1);
					continue;
				}
				CALL_STATE callState;
				hr1 = pCallInfo->get_CallState(&callState);
				pCallInfo->Release();
				if(FAILED(hr1)) {
					logger->error("Cannot retrieve call state: hr=%08X.", hr1);
					continue;
				}
				if(callState == CS_CONNECTED) {
					break;
				}
			}
			if(it == calls.end()) {
				logger->warn("Cannot swap. No connected call found.");
			} else {
				logger->debug("Call %d found connected. Swapping.", (*it).first);
				hr = (*it).second->SwapHold(callControl);
			}
		}
        return hr;
    } else {
		logger->warn("Cannot unhold. CallID %d not found.", callID);
		return E_FAIL;
	}
}


HRESULT MSTapi3::SendDigits(wstring terminal, wstring digits) {
	// using terminal as address !!!
	ITAddress* pAddr = getITAddress(terminal);
	if(pAddr == NULL) {
		return TAPI_E_NOITEMS;
	}
	IEnumCall* pCallEnum = NULL;
	HRESULT hr = pAddr->EnumerateCalls(&pCallEnum);
	if(pCallEnum == NULL) {
		logger->error("pCallEnum is NULL !!!");
		return TAPI_E_NOITEMS;
	}
	if(SUCCEEDED(hr)) {
		int count = 0;
		while(true) {
			ITCallInfo* callInfo;
			hr = pCallEnum->Next(1, &callInfo, NULL);
			if(FAILED(hr)) {
				logger->error("pCallEnum->Next() failed: hr=%08X.", hr);
				break;
			}
			if(hr == S_FALSE) {
				logger->debug("pCallEnum done.");
				hr = S_OK;
				break;
			}

			ITBasicCallControl* pCallControl;
			hr = callInfo->QueryInterface(IID_ITBasicCallControl, (void**)&pCallControl);
			if(SUCCEEDED(hr)) {
				if(pCallControl != NULL) {
					CALL_STATE callState;
					callInfo->get_CallState(&callState);
					if(callState == CS_CONNECTED) {
						ITLegacyCallMediaControl* pLegacy;
						hr = pCallControl->QueryInterface(IID_ITLegacyCallMediaControl, (void **) &pLegacy);
						if(FAILED(hr)) {
							logger->error("Cannot retrieve legacy: hr=%08X.", hr);
						} else {
							BSTR bstrDigits = SysAllocString(digits.c_str());
							hr = pLegacy->GenerateDigits(bstrDigits, LINEDIGITMODE_DTMF);
							SysFreeString(bstrDigits);
							pLegacy->Release();
							if(FAILED(hr)) {
								logger->error("GenerateDigits() failed: hr=%08X.", hr);
							} else {
								logger->debug("GenerateDigits() succeeded.");
							}
						}
					}
				} else {
					logger->debug("pCallControl is NULL.");
					hr = TAPI_E_NOITEMS;
				}
			}
			pCallControl->Release();
			callInfo->Release();
		}
	} else {
		logger->error("EnumerateCalls() failed with hr=%08X", hr);
	}
	return hr;
}

//////////////////////////////////////////////////////////////////////
// DisconnectTheCall
//
// Disconnects the call
//////////////////////////////////////////////////////////////////////
HRESULT MSTapi3::DisconnectTheCall(int callID) {
	logger->debug("Disconnecting callID=%d...", callID);
	ITBasicCallControl* callControl = getCallControl(callID);
    if (NULL != callControl) {

        HRESULT hr = callControl->Disconnect(DC_NORMAL);
		logger->debug("Disconnect(DC_NORMAL): hr=%08X", hr);
        // Do not release the call yet, as that would prevent
        // us from receiving the disconnected notification.
        return hr;
    } else {
		logger->warn("Cannot disconnect. CallID %d not found.", callID);
		return E_FAIL;
	}
}

//////////////////////////////////////////////////////////////////////
// ReleaseTheCall
//
// Releases the call
//////////////////////////////////////////////////////////////////////
void MSTapi3::ReleaseTheCall(int callID) {
	logger->debug("Releasing callID=%d...", callID);
	removeCallControl(callID);
}


/////////////////////////////////////
// PRIVATE METHODS
/////////////////////////////////////

void MSTapi3::ReleaseCalls(ITAddress* pAddr) {
	IEnumCall* pCallEnum = NULL;
	if(pAddr == NULL) {
		logger->error("pAddr is NULL !!!");
		return;
	}
	HRESULT hr = pAddr->EnumerateCalls(&pCallEnum);
	if(pCallEnum == NULL) {
		logger->error("pCallEnum is NULL !!!");
		return;
	}
	if(SUCCEEDED(hr)) {
		while(true) {
			ITCallInfo* callInfo;
			hr = pCallEnum->Next(1, &callInfo, NULL);
			if(FAILED(hr)) {
				logger->error("pCallEnum->Next() failed: hr=%08X.", hr);
				break;
			}
			if(hr == S_FALSE) {
				logger->debug("pCallEnum done.");
				break;
			}
			ITBasicCallControl* pCallControl;
			hr = callInfo->QueryInterface(IID_ITBasicCallControl, (void**)&pCallControl);
			if(SUCCEEDED(hr)) {
				if(pCallControl != NULL) {
					int callID = getCallID(pCallControl);
					if(callID >= 0) {
						calls.erase(callID);
					}
					logger->debug("Disconnecting pCallControl...");
					hr = pCallControl->Disconnect( DC_NORMAL );
					logger->debug("Disconnected pCallControl.");
					logger->debug("Releasing pCallControl...");
					pCallControl->Release();
					logger->debug("pCallControl released.");
				} else {
					logger->debug("pCallControl is NULL.");
				}
			}
			logger->debug("Releasing callInfo...");
			callInfo->Release();
			logger->debug("callInfo released.");
		}
	} else {
		logger->error("EnumerateCalls() failed with hr=%08X", hr);
	}
}

void MSTapi3::ReleaseAddresses() {
	for (AddressMap::iterator it = addresses.begin(); it != addresses.end(); ++it) {
		ITAddress* address = (*it).second;
		if(address != NULL) {
			logger->info("Releasing calls on %S", (*it).first.c_str());
			ReleaseCalls(address);
			address->Release();
		} else {
			logger->warn("ITAddress is NULL for %S.", (*it).first.c_str());
		}
	}
	addresses.clear();
}

void MSTapi3::UnregisterCallNotifications() {
	for (list<long>::iterator it = callNotificationRegisters.begin(); it != callNotificationRegisters.end(); ++it) {
		HRESULT hr = tapi->UnregisterNotifications(*it);
		if(FAILED(hr)) {
			logger->error("UnregisterNotifications(%ld) failed: hr=%08X", *it, hr);
		} else {
			logger->debug("UnregisterNotifications(%ld) succeeded", *it);
		}
	}
	callNotificationRegisters.clear();

}


///////////////////////////////////////////////////////////////////////////
// RegisterTapiEventInterface()
///////////////////////////////////////////////////////////////////////////
HRESULT MSTapi3::RegisterTapiEventInterface() {
    HRESULT                       hr = S_OK;
    IConnectionPointContainer   * pCPC;
    IConnectionPoint            * pCP;
    

    hr = tapi->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC);

    if ( FAILED(hr) ) {
        return hr;
    }

    hr = pCPC->FindConnectionPoint(IID_ITTAPIEventNotification, &pCP);
    pCPC->Release();
        
    if (FAILED(hr)) {
        return hr;
    }
    hr = pCP->Advise(tapi3EventNotification, &gulAdvise);
    pCP->Release();
    return hr;

}

///////////////////////////////////////////////////////////////////////////
// UnregisterTapiEventInterface()
///////////////////////////////////////////////////////////////////////////
HRESULT MSTapi3::UnregisterTapiEventInterface() {
    HRESULT                       hr = S_OK;
    IConnectionPointContainer   * pCPC;
    IConnectionPoint            * pCP;
    

    hr = tapi->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC);

    if ( FAILED(hr) ) {
        return hr;
    }

    hr = pCPC->FindConnectionPoint(IID_ITTAPIEventNotification, &pCP);
    pCPC->Release();
        
    if (FAILED(hr)) {
        return hr;
    }
    hr = pCP->Unadvise(gulAdvise);
    pCP->Release();
    return hr;

}


////////////////////////////////////////////////////////////////////////
// ListenOnAddresses
//
// This procedure will find all addresses that support audioin and audioout
// and will call ListenOnThisAddress to start listening on it.
////////////////////////////////////////////////////////////////////////
HRESULT MSTapi3::ListenOnAddresses()
{
    HRESULT hr = S_OK;
    IEnumAddress* pEnumAddress;
    ITAddress* pAddress;

⌨️ 快捷键说明

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