ast_h323.cpp

来自「一个非常美妙的proxy。功能强大。基于sip的协议。如果还要的话」· C++ 代码 · 共 1,533 行 · 第 1/3 页

CPP
1,533
字号
				cout << "-- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;			}			break;		case H323Connection::EndedByUnreachable:			if (h323debug) {				cout << "-- " << remoteName << " could not be reached." << endl;			}			break;		case H323Connection::EndedByHostOffline:			if (h323debug) {				cout << "-- " << remoteName << " is not online." << endl;			}			break;		case H323Connection::EndedByNoEndPoint:			if (h323debug) {				cout << "-- No phone running for " << remoteName << endl;			}			break;		case H323Connection::EndedByConnectFail:			if (h323debug) {				cout << "-- Transport error calling " << remoteName << endl;			}			break;		default:			if (h323debug)				cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;	}	if (connection.IsEstablished()) {		if (h323debug) {			cout << "\t-- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;		}	}		/* Invoke the PBX application registered callback */	on_connection_cleared(connection.GetCallReference(), clearedCallToken);	return;}H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *o){	unsigned options = 0;	call_options_t *opts = (call_options_t *)o;	if (opts && opts->noFastStart) {		options |= H323Connection::FastStartOptionDisable;	} else {		options |= H323Connection::FastStartOptionEnable;	}	if (opts && opts->noH245Tunneling) {		options |= H323Connection::H245TunnelingOptionDisable;	} else {		options |= H323Connection::H245TunnelingOptionEnable;	}/* Disable until I can figure out the proper way to deal with this */#if 0	if (opts->noSilenceSuppression) {		options |= H323Connection::SilenceSuppresionOptionDisable;	} else {		options |= H323Connection::SilenceSUppressionOptionEnable;	}#endif	return new MyH323Connection(*this, callReference, options);}/* MyH323Connection Implementation */    MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,							unsigned options)	: H323Connection(ep, callReference, options){	cause = -1;	if (h323debug) {		cout << "	== New H.323 Connection created." << endl;	}	return;}MyH323Connection::~MyH323Connection(){	if (h323debug) {		cout << "	== H.323 Connection deleted." << endl;	}	return;}BOOL MyH323Connection::OnReceivedProgress(const H323SignalPDU &pdu){	BOOL isInband;	unsigned pi;	if (!H323Connection::OnReceivedProgress(pdu)) {		return FALSE;	}	if (!pdu.GetQ931().GetProgressIndicator(pi))		pi = 0;	if (h323debug) {		cout << "\t- Progress Indicator: " << pi << endl;	}		switch(pi) {	case Q931::ProgressNotEndToEndISDN:	case Q931::ProgressInbandInformationAvailable:		isInband = TRUE;		break;	default:		isInband = FALSE;	}	on_progress(GetCallReference(), (const char *)GetCallToken(), isInband);	return connectionState != ShuttingDownConnection;}H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,								  const H323SignalPDU & setupPDU,								  H323SignalPDU & /*connectPDU*/){	unsigned pi;	if (h323debug) {               cout << "\t=-= In OnAnswerCall for call " << GetCallReference() << endl;	}	if (connectionState == ShuttingDownConnection)		return H323Connection::AnswerCallDenied;	if (!setupPDU.GetQ931().GetProgressIndicator(pi)) {		pi = 0;	}	if (h323debug) {		cout << "\t\t- Progress Indicator: " << pi << endl;	}	if (progressAlert) {		pi = progressAlert;	} else if (pi == Q931::ProgressOriginNotISDN) {		pi = Q931::ProgressInbandInformationAvailable;	}	if (pi) {		alertingPDU->GetQ931().SetProgressIndicator(pi);	}	if (h323debug) {		cout << "\t\t- Inserting PI of " << pi << " into ALERTING message" << endl;	}	if (!on_answer_call(GetCallReference(), (const char *)GetCallToken())) {		return H323Connection::AnswerCallDenied;	}	/* The call will be answered later with "AnsweringCall()" function.	 */ 	return H323Connection::AnswerCallDeferredWithMedia;}BOOL MyH323Connection::OnAlerting(const H323SignalPDU & alertingPDU, const PString & username){	if (h323debug) {	        cout << "\t=-= In OnAlerting for call " << GetCallReference()	              << ": sessionId=" << sessionId << endl;                 cout << "\t-- Ringing phone for \"" << username << "\"" << endl;	}	if (on_progress) {		BOOL isInband;		unsigned alertingPI;		if (!alertingPDU.GetQ931().GetProgressIndicator(alertingPI)) {			alertingPI = 0;		}		if (h323debug) {			cout << "\t\t- Progress Indicator: " << alertingPI << endl;		}				switch(alertingPI) {		case Q931::ProgressNotEndToEndISDN:		case Q931::ProgressInbandInformationAvailable:			isInband = TRUE;			break;		default:			isInband = FALSE;		}		on_progress(GetCallReference(), (const char *)GetCallToken(), isInband);	}        on_chan_ringing(GetCallReference(), (const char *)GetCallToken() );        return connectionState != ShuttingDownConnection;}BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU){	call_details_t cd;	PString sourceE164;	PString destE164;	PString sourceName;	PString sourceAliases;		PString destAliases;	PIPSocket::Address Ip;	WORD sourcePort;	char *s, *s1; 	if (h323debug) {		cout << ("\t--Received SETUP message\n");	}	if (connectionState == ShuttingDownConnection)		return FALSE;	sourceAliases = setupPDU.GetSourceAliases();	destAliases = setupPDU.GetDestinationAlias();				sourceE164 = "";	setupPDU.GetSourceE164(sourceE164);	sourceName = "";	sourceName=setupPDU.GetQ931().GetDisplayName();	destE164 = "";	setupPDU.GetDestinationE164(destE164);	/* Convert complex strings */	//  FIXME: deal more than one source alias     	if ((s = strchr(sourceAliases, ' ')) != NULL) {                *s = '\0';	}    	if ((s = strchr(sourceAliases, '\t')) != NULL) {                *s = '\0';	} 	if ((s1 = strchr(destAliases, ' ')) != NULL) {         	*s1 = '\0';	}	if ((s1 = strchr(destAliases, '\t')) != NULL) {         	*s1 = '\0';	}	memset(&cd, 0, sizeof(cd));	cd.call_reference = GetCallReference();	cd.call_token = strdup((const char *)GetCallToken());	cd.call_source_aliases = strdup((const char *)sourceAliases);	cd.call_dest_alias = strdup((const char *)destAliases);	cd.call_source_e164 = strdup((const char *)sourceE164);	cd.call_dest_e164 = strdup((const char *)destE164);	cd.call_source_name = strdup((const char *)sourceName);	GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort); 	cd.sourceIp = strdup((const char *)Ip.AsString());	/* Notify Asterisk of the request */	call_options_t *res = on_incoming_call(&cd);	if (!res) {		if (h323debug) {			cout << "	-- Call Failed" << endl;		}		return FALSE;	}	progressSetup = res->progress_setup;	progressAlert = res->progress_alert;	dtmfCodec = (RTP_DataFrame::PayloadTypes)res->dtmfcodec;	return H323Connection::OnReceivedSignalSetup(setupPDU);}BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU){	call_details_t cd;	char *s, *s1;	if (h323debug) { 		cout << "	-- Sending SETUP message" << endl;	}	if (connectionState == ShuttingDownConnection)		return FALSE;	if (!ast_cid_num.IsEmpty()) {		setupPDU.GetQ931().SetCallingPartyNumber(ast_cid_num);	}	if (!ast_cid_name.IsEmpty()) {		setupPDU.GetQ931().SetDisplayName(ast_cid_name);	}	sourceAliases = setupPDU.GetSourceAliases();	destAliases = setupPDU.GetDestinationAlias();	sourceE164 = "";	setupPDU.GetSourceE164(sourceE164);	destE164 = "";	setupPDU.GetDestinationE164(destE164);	/* Convert complex strings */	//  FIXME: deal more than one source alias 	    	if ((s = strchr(sourceAliases, ' ')) != NULL) {                *s = '\0';	}    	if ((s = strchr(sourceAliases, '\t')) != NULL) {                *s = '\0';	}    	if ((s1 = strchr(destAliases, ' ')) != NULL) {        	 *s1 = '\0';	}	if ((s1 = strchr(destAliases, '\t')) != NULL) {         	*s1 = '\0';	}	memset(&cd, 0, sizeof(cd));	cd.call_reference = GetCallReference();	cd.call_token = strdup((const char *)GetCallToken());	cd.call_source_aliases = strdup((const char *)sourceAliases);	cd.call_dest_alias = strdup((const char *)destAliases);	cd.call_source_e164 = strdup((const char *)sourceE164);	cd.call_dest_e164 = strdup((const char *)destE164);	int res = on_outgoing_call(&cd);	if (!res) {		if (h323debug) {			cout << "\t-- Call Failed" << endl;		}		return FALSE;	}	if (progressSetup) {		setupPDU.GetQ931().SetProgressIndicator(progressSetup);	}	return H323Connection::OnSendSignalSetup(setupPDU);}BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU){	if (h323debug) {		cout << "\t-- Sending RELEASE COMPLETE" << endl;	}	if (cause > 0)		releaseCompletePDU.GetQ931().SetCause((Q931::CauseValues)cause);	return H323Connection::OnSendReleaseComplete(releaseCompletePDU);}BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu){	if (h323debug) {		cout << "\t-- Received Facility message... " << endl;	}		return H323Connection::OnReceivedFacility(pdu);}void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu){	if (h323debug) {		cout <<  "\t-- Received RELEASE COMPLETE message..." << endl;	}	if (on_hangup)		on_hangup(GetCallReference(), (const char *)GetCallToken(), pdu.GetQ931().GetCause());	return H323Connection::OnReceivedReleaseComplete(pdu);}BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel){	if (h323debug) {		cout << "\t-- Closing logical channel..." << endl;	}	return H323Connection::OnClosingLogicalChannel(channel);}void MyH323Connection::SendUserInputTone(char tone, unsigned duration){	if (h323debug) {		cout << "\t-- Sending user input tone (" << tone << ") to remote" << endl;	}	on_send_digit(GetCallReference(), tone, (const char *)GetCallToken());		H323Connection::SendUserInputTone(tone, duration);}void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp){	if (mode == H323_DTMF_INBAND) {		if (h323debug) {			cout << "\t-- Received user input tone (" << tone << ") from remote" << endl;		}		on_send_digit(GetCallReference(), tone, (const char *)GetCallToken());	}	H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);}void MyH323Connection::OnUserInputString(const PString &value){	if (mode == H323_DTMF_RFC2833) {		if (h323debug) {			cout <<  "\t-- Received user input string (" << value << ") from remote." << endl;		}		on_send_digit(GetCallReference(), value[0], (const char *)GetCallToken());	}	}void MyH323Connection::OnSendCapabilitySet(H245_TerminalCapabilitySet & pdu){	PINDEX i;	H323Connection::OnSendCapabilitySet(pdu);	H245_ArrayOf_CapabilityTableEntry & tables = pdu.m_capabilityTable;	for(i = 0; i < tables.GetSize(); i++)	{		H245_CapabilityTableEntry & entry = tables[i];		if (entry.HasOptionalField(H245_CapabilityTableEntry::e_capability)) {			H245_Capability & cap = entry.m_capability;			if (cap.GetTag() == H245_Capability::e_receiveRTPAudioTelephonyEventCapability) {				H245_AudioTelephonyEventCapability & atec = cap;				atec.m_dynamicRTPPayloadType = dtmfCodec;				on_set_rfc2833_payload(GetCallReference(), (const char *)GetCallToken(), (int)dtmfCodec);				if (h323debug) {					cout << "\t-- Transmitting RFC2833 on payload " <<						atec.m_dynamicRTPPayloadType << endl;				}			}		}	}}void MyH323Connection::OnSetLocalCapabilities(){	if (on_setcapabilities)		on_setcapabilities(GetCallReference(), (const char *)callToken);}BOOL MyH323Connection::OnReceivedCapabilitySet(const H323Capabilities & remoteCaps,					       const H245_MultiplexCapability * muxCap,					       H245_TerminalCapabilitySetReject & reject){	if (!H323Connection::OnReceivedCapabilitySet(remoteCaps, muxCap, reject)) {		return FALSE;	}	const H323Capability * cap = remoteCaps.FindCapability(H323_UserInputCapability::SubTypeNames[H323_UserInputCapability::SignalToneRFC2833]);	if (cap != NULL) {		RTP_DataFrame::PayloadTypes pt = ((H323_UserInputCapability*)cap)->GetPayloadType();		on_set_rfc2833_payload(GetCallReference(), (const char *)GetCallToken(), (int)pt);		if (h323debug) {			cout << "\t-- Inbound RFC2833 on payload " << pt << endl;		}	}	return TRUE;}H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,									   H323Channel::Directions dir,								   unsigned sessionID,		 					           const H245_H2250LogicalChannelParameters * /*param*/,								   RTP_QOS * /*param*/ ){	return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID);}/** This callback function is invoked once upon creation of each  * channel for an H323 session   */BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel){    	/* Increase the count of channels we have open */	channelsOpen++;	if (h323debug) {		cout << "\t-- Started logical channel: ";		cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));		cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;		cout <<  "\t\t-- channelsOpen = " << channelsOpen << endl;	}	return connectionState != ShuttingDownConnection;}void MyH323Connection::SetCapabilities(int cap, int dtmfMode){	int g711Frames = 20;//	int gsmFrames  = 4;	PINDEX lastcap = -1; /* last common capability index */#if 0	if (cap & AST_FORMAT_SPEEX) {		/* Not real sure if Asterisk acutally supports all		   of the various different bit rates so add them 		   all and figure it out later*/		localCapabilities.SetCapability(0, 0, new SpeexNarrow2AudioCapability());		localCapabilities.SetCapability(0, 0, new SpeexNarrow3AudioCapability());		localCapabilities.SetCapability(0, 0, new SpeexNarrow4AudioCapability());		localCapabilities.SetCapability(0, 0, new SpeexNarrow5AudioCapability());		localCapabilities.SetCapability(0, 0, new SpeexNarrow6AudioCapability());	}#endif 	if (cap & AST_FORMAT_G729A) {		AST_G729ACapability *g729aCap;		AST_G729Capability *g729Cap;		lastcap = localCapabilities.SetCapability(0, 0, g729aCap = new AST_G729ACapability);		lastcap = localCapabilities.SetCapability(0, 0, g729Cap = new AST_G729Capability);	}		if (cap & AST_FORMAT_G723_1) {		H323_G7231Capability *g7231Cap;		lastcap = localCapabilities.SetCapability(0, 0, g7231Cap = new H323_G7231Capability);	} #if 0	if (cap & AST_FORMAT_GSM) {		H323_GSM0610Capability *gsmCap;	    	lastcap = localCapabilities.SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);	    	gsmCap->SetTxFramesInPacket(gsmFrames);	} #endif	if (cap & AST_FORMAT_ULAW) {		H323_G711Capability *g711uCap;	    	lastcap = localCapabilities.SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));		g711uCap->SetTxFramesInPacket(g711Frames);	} 	if (cap & AST_FORMAT_ALAW) {

⌨️ 快捷键说明

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