4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * This code has been derived from code created by
9 * Michael Manousos and Mark Spencer
11 * This file is part of the chan_h323 driver for Asterisk
13 * chan_h323 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
19 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <arpa/inet.h>
43 #include <asterisk/logger.h>
48 #include "chan_h323.h"
51 /* PWlib Required Components */
52 #define MAJOR_VERSION 1
53 #define MINOR_VERSION 0
54 #define BUILD_TYPE ReleaseCode
55 #define BUILD_NUMBER 0
57 /** Counter for the number of connections */
61 int mode = H323_DTMF_RFC2833;
63 /** Options for connections creation */
64 BOOL noFastStart = TRUE;
66 BOOL noSilenceSuppression;
69 * We assume that only one endPoint should exist.
70 * The application cannot run the h323_end_point_create() more than once
71 * FIXME: Singleton this, for safety
73 MyH323EndPoint *endPoint = NULL;
75 /** PWLib entry point */
76 MyProcess *localProcess = NULL;
79 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
80 MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
85 void MyProcess::Main()
87 ast_verbose(" == Creating H.323 Endpoint\n");
88 endPoint = new MyH323EndPoint();
89 PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
92 #define H323_NAME OPAL_G7231_6k3"{sw}"
93 #define H323_G729 OPAL_G729 "{sw}"
94 #define H323_G729A OPAL_G729A"{sw}"
96 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
97 H323_REGISTER_CAPABILITY(AST_G729Capability, H323_G729);
98 H323_REGISTER_CAPABILITY(AST_G729ACapability, H323_G729A);
100 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
101 : H323AudioCapability(7, 4)
106 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
108 Comparison result = H323AudioCapability::Compare(obj);
109 if (result != EqualTo)
112 PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
113 if (annexA < otherAnnexA)
115 if (annexA > otherAnnexA)
120 PObject * H323_G7231Capability::Clone() const
122 return new H323_G7231Capability(*this);
126 PString H323_G7231Capability::GetFormatName() const
132 unsigned H323_G7231Capability::GetSubType() const
134 return H245_AudioCapability::e_g7231;
138 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
139 unsigned packetSize) const
141 cap.SetTag(H245_AudioCapability::e_g7231);
143 H245_AudioCapability_g7231 & g7231 = cap;
144 g7231.m_maxAl_sduAudioFrames = packetSize;
145 g7231.m_silenceSuppression = annexA;
151 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
152 unsigned & packetSize)
154 if (cap.GetTag() != H245_AudioCapability::e_g7231)
157 const H245_AudioCapability_g7231 & g7231 = cap;
158 packetSize = g7231.m_maxAl_sduAudioFrames;
159 annexA = g7231.m_silenceSuppression;
165 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
171 /////////////////////////////////////////////////////////////////////////////
173 AST_G729Capability::AST_G729Capability()
174 : H323AudioCapability(24, 6)
179 PObject * AST_G729Capability::Clone() const
181 return new AST_G729Capability(*this);
185 unsigned AST_G729Capability::GetSubType() const
187 return H245_AudioCapability::e_g729;
191 PString AST_G729Capability::GetFormatName() const
197 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
201 /////////////////////////////////////////////////////////////////////////////
203 AST_G729ACapability::AST_G729ACapability()
204 : H323AudioCapability(24, 6)
209 PObject * AST_G729ACapability::Clone() const
211 return new AST_G729ACapability(*this);
215 unsigned AST_G729ACapability::GetSubType() const
217 return H245_AudioCapability::e_g729AnnexA;
221 PString AST_G729ACapability::GetFormatName() const
227 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
233 * The fullAddress parameter is used directly in the MakeCall method so
234 * the General form for the fullAddress argument is :
235 * [alias@][transport$]host[:port]
236 * default values: alias = the same value as host.
240 int MyH323EndPoint::MakeCall(const PString & dest, PString & token, unsigned int *callReference, unsigned int port, char *callerid, char *callername)
243 MyH323Connection * connection;
245 /* Determine whether we are using a gatekeeper or not. */
246 if (GetGatekeeper() != NULL) {
249 cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
251 fullAddress = dest; /* host */
253 cout << " -- Making call to " << fullAddress << "." << endl;
256 if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
258 cout << "Error making call to \"" << fullAddress << '"' << endl;
262 *callReference = connection->GetCallReference();
265 connection->SetLocalPartyName(PString(callerid));
267 localAliasNames.RemoveAll();
268 connection->SetLocalPartyName(PString(callername));
270 localAliasNames.AppendString(PString(callerid));
271 } else if (callerid) {
272 localAliasNames.RemoveAll();
273 connection->SetLocalPartyName(PString(callerid));
276 connection->Unlock();
279 cout << " -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
280 cout << " -- " << "Call token is " << (const char *)token << endl;
281 cout << " -- Call reference is " << *callReference << endl;
286 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
288 H323EndPoint::SetEndpointTypeInfo(info);
289 info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
290 info.m_gateway.m_protocol.SetSize(1);
291 H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
292 protocol.SetTag(H225_SupportedProtocols::e_voice);
293 PINDEX as=SupportedPrefixes.GetSize();
294 ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
295 for (PINDEX p=0; p<as; p++) {
296 H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
300 void MyH323EndPoint::SetGateway(void)
302 terminalType = e_GatewayOnly;
305 H323Capabilities MyH323EndPoint::GetCapabilities(void)
310 BOOL MyH323EndPoint::ClearCall(const PString & token)
313 cout << " -- ClearCall: Request to clear call with token " << token << endl;
315 return H323EndPoint::ClearCall(token);
318 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
320 H323Connection *connection = NULL;
322 connection = FindConnectionWithLock(token);
323 if (connection != NULL) {
324 connection->SendUserInputTone(tone, 500);
325 connection->Unlock();
329 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
333 cout << " channelsOpen = " << channelsOpen << endl;
334 H323EndPoint::OnClosedLogicalChannel(connection, channel);
337 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
338 const PString & forwardParty,
339 const H323SignalPDU & pdu)
342 cout << " -- Call Forwarded to " << forwardParty << endl;
347 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
348 const PString & forwardParty,
349 const H323SignalPDU & pdu)
352 cout << " -- Forwarding call to " << forwardParty << endl;
354 return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
357 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
360 cout << " -- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
362 on_connection_established(connection.GetCallReference());
365 /** OnConnectionCleared callback function is called upon the dropping of an established
368 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
372 PIPSocket::Address Ip;
375 remoteName = connection.GetRemotePartyName();
377 cd.call_reference = connection.GetCallReference();
378 cd.call_token = strdup((const char *)clearedCallToken);
379 cd.call_source_aliases = strdup((const char *)connection.GetRemotePartyName());
381 connection.GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
382 cd.sourceIp = strdup((const char *)Ip.AsString());
384 /* Convert complex strings */
386 if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
389 switch (connection.GetCallEndReason()) {
390 case H323Connection::EndedByCallForwarded :
392 cout << " -- " << remoteName << " has forwarded the call" << endl;
394 case H323Connection::EndedByRemoteUser :
396 cout << " -- " << remoteName << " has cleared the call" << endl;
398 case H323Connection::EndedByCallerAbort :
400 cout << " -- " << remoteName << " has stopped calling" << endl;
402 case H323Connection::EndedByRefusal :
404 cout << " -- " << remoteName << " did not accept your call" << endl;
406 case H323Connection::EndedByRemoteBusy :
408 cout << " -- " << remoteName << " was busy" << endl;
410 case H323Connection::EndedByRemoteCongestion :
412 cout << " -- Congested link to " << remoteName << endl;
414 case H323Connection::EndedByNoAnswer :
416 cout << " -- " << remoteName << " did not answer your call" << endl;
418 case H323Connection::EndedByTransportFail :
420 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
422 case H323Connection::EndedByCapabilityExchange :
424 cout << " -- Could not find common codec with " << remoteName << endl;
426 case H323Connection::EndedByNoAccept :
428 cout << " -- Did not accept incoming call from " << remoteName << endl;
430 case H323Connection::EndedByAnswerDenied :
432 cout << " -- Refused incoming call from " << remoteName << endl;
434 case H323Connection::EndedByNoUser :
436 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
438 case H323Connection::EndedByNoBandwidth :
440 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
442 case H323Connection::EndedByUnreachable :
444 cout << " -- " << remoteName << " could not be reached." << endl;
446 case H323Connection::EndedByHostOffline :
448 cout << " -- " << remoteName << " is not online." << endl;
450 case H323Connection::EndedByNoEndPoint :
452 cout << " -- No phone running for " << remoteName << endl;
454 case H323Connection::EndedByConnectFail :
456 cout << " -- Transport error calling " << remoteName << endl;
460 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
464 if(connection.IsEstablished())
466 cout << " -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
468 /* Invoke the PBX application registered callback */
469 on_connection_cleared(cd);
475 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
477 unsigned options = 0;
480 options |= H323Connection::FastStartOptionDisable;
483 options |= H323Connection::H245TunnelingOptionDisable;
485 return new MyH323Connection(*this, callReference, options);
488 /* MyH323Connection */
489 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
491 : H323Connection(ep, callReference, options)
494 cout << " == New H.323 Connection created." << endl;
499 MyH323Connection::~MyH323Connection()
502 cout << " == H.323 Connection deleted." << endl;
507 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
508 const H323SignalPDU & /*setupPDU*/,
509 H323SignalPDU & /*connectPDU*/)
511 if (!on_answer_call(GetCallReference()))
512 return H323Connection::AnswerCallDenied;
514 /* The call will be answered later with "AnsweringCall()" function.
516 return H323Connection::AnswerCallDeferred;
519 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
523 cout << " -- Ringing phone for \"" << username << "\"" << endl;
525 on_chan_ringing(GetCallReference());
529 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
533 ast_verbose(" -- Received SETUP message\n");
540 PString sourceAliases;
542 PIPSocket::Address Ip;
546 sourceAliases = setupPDU.GetSourceAliases();
547 destAliases = setupPDU.GetDestinationAlias();
550 setupPDU.GetSourceE164(sourceE164);
552 sourceName=setupPDU.GetQ931().GetDisplayName();
554 setupPDU.GetDestinationE164(destE164);
556 /* Convert complex strings */
557 // FIXME: deal more than one source alias
558 if ((s = strchr(sourceAliases, ' ')) != NULL)
560 if ((s = strchr(sourceAliases, '\t')) != NULL)
562 if ((s1 = strchr(destAliases, ' ')) != NULL)
564 if ((s1 = strchr(destAliases, '\t')) != NULL)
568 cd.call_reference = GetCallReference();
570 cd.call_token = strdup((const char *)GetCallToken());
572 cd.call_source_aliases = strdup((const char *)sourceAliases);
573 cd.call_dest_alias = strdup((const char *)destAliases);
574 cd.call_source_e164 = strdup((const char *)sourceE164);
575 cd.call_dest_e164 = strdup((const char *)destE164);
576 cd.call_source_name = strdup((const char *)sourceName);
578 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
579 cd.sourceIp = strdup((const char *)Ip.AsString());
581 /* Notify Asterisk of the request */
582 int res = on_incoming_call(cd);
586 cout << " -- Call Failed" << endl;
591 return H323Connection::OnReceivedSignalSetup(setupPDU);
594 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
600 cout << " -- Sending SETUP message" << endl;
602 sourceAliases = setupPDU.GetSourceAliases();
603 destAliases = setupPDU.GetDestinationAlias();
606 setupPDU.GetSourceE164(sourceE164);
608 setupPDU.GetDestinationE164(destE164);
610 /* Convert complex strings */
611 // FIXME: deal more than one source alias
613 if ((s = strchr(sourceAliases, ' ')) != NULL)
615 if ((s = strchr(sourceAliases, '\t')) != NULL)
617 if ((s1 = strchr(destAliases, ' ')) != NULL)
619 if ((s1 = strchr(destAliases, '\t')) != NULL)
622 cd.call_reference = GetCallReference();
624 cd.call_token = strdup((const char *)GetCallToken());
626 cd.call_source_aliases = strdup((const char *)sourceAliases);
627 cd.call_dest_alias = strdup((const char *)destAliases);
628 cd.call_source_e164 = strdup((const char *)sourceE164);
629 cd.call_dest_e164 = strdup((const char *)destE164);
631 int res = on_outgoing_call(cd);
635 cout << " -- Call Failed" << endl;
640 return H323Connection::OnSendSignalSetup(setupPDU);
643 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
646 cout << " -- Sending RELEASE COMPLETE" << endl;
648 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
651 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
654 cout << " -- Received Facility message... " << endl;
656 return H323Connection::OnReceivedFacility(pdu);
659 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
662 cout << " -- Received RELEASE COMPLETE message..." << endl;
664 return H323Connection::OnReceivedReleaseComplete(pdu);
667 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
670 cout << " -- Closing logical channel..." << endl;
672 return H323Connection::OnClosingLogicalChannel(channel);
676 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
679 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
681 on_send_digit(GetCallReference(), tone);
682 H323Connection::SendUserInputTone(tone, duration);
685 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
687 if (mode == H323_DTMF_INBAND) {
689 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
691 on_send_digit(GetCallReference(), tone);
693 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
696 void MyH323Connection::OnUserInputString(const PString &value)
698 if (mode == H323_DTMF_RFC2833) {
700 cout << " -- Received user input string (" << value << ") from remote." << endl;
702 on_send_digit(GetCallReference(), value[0]);
706 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
707 H323Channel::Directions dir,
709 const H245_H2250LogicalChannelParameters * /*param*/)
711 struct rtp_info *info;
714 /* Determine the Local (A side) IP Address and port */
715 info = on_create_connection(GetCallReference());
721 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
722 externalPort = info->port;
725 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
726 cout << " -- externalIpAddress: " << externalIpAddress << endl;
727 cout << " -- externalPort: " << externalPort << endl;
728 cout << " -- SessionID: " << sessionID << endl;
729 cout << " -- Direction: " << dir << endl;
731 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
734 /** This callback function is invoked once upon creation of each
735 * channel for an H323 session
737 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
740 cout << " -- Started logical channel: ";
741 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
742 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
745 /* adjust the count of channels we have open */
749 cout << " -- channelsOpen = " << channelsOpen << endl;
755 /* MyH323_ExternalRTPChannel */
756 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
757 const H323Capability & capability,
758 Directions direction,
760 const PIPSocket::Address & ip,
762 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
766 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
767 const H323Capability & capability,
768 Directions direction,
770 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id)
774 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
775 const H323Capability & capability,
776 Directions direction,
778 const H323TransportAddress & data,
779 const H323TransportAddress & control)
780 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id, data, control)
784 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
788 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(const H245_H2250LogicalChannelAckParameters & param)
790 PIPSocket::Address remoteIpAddress;
793 if (H323_ExternalRTPChannel::OnReceivedAckPDU(param)) {
794 H323_ExternalRTPChannel::GetRemoteAddress(remoteIpAddress, remotePort);
795 /* Notify Asterisk of remote RTP information */
796 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
802 /** IMPLEMENTATION OF C FUNCTIONS */
805 * The extern "C" directive takes care for
806 * the ANSI-C representation of linkable symbols
811 int h323_end_point_exist(void)
819 void h323_end_point_create(int no_fast_start, int no_h245_tunneling)
823 noFastStart = (BOOL)no_fast_start;
824 noH245Tunneling = (BOOL)no_h245_tunneling;
826 localProcess = new MyProcess();
827 localProcess->Main();
830 void h323_gk_urq(void)
832 if (!h323_end_point_exist()) {
833 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
836 endPoint->RemoveGatekeeper();
839 void h323_end_process(void)
841 endPoint->ClearAllCalls();
842 endPoint->RemoveListener(NULL);
847 void h323_debug(int flag, unsigned level)
850 PTrace:: SetLevel(level);
852 PTrace:: SetLevel(0);
856 /** Installs the callback functions on behalf of the PBX application */
857 void h323_callback_register(setup_incoming_cb ifunc,
858 setup_outbound_cb sfunc,
859 on_connection_cb confunc,
860 start_logchan_cb lfunc,
862 chan_ringing_cb rfunc,
863 con_established_cb efunc,
865 answer_call_cb acfunc)
867 on_incoming_call = ifunc;
868 on_outgoing_call = sfunc;
869 on_create_connection = confunc;
870 on_start_logical_channel = lfunc;
871 on_connection_cleared = clfunc;
872 on_chan_ringing = rfunc;
873 on_connection_established = efunc;
874 on_send_digit = dfunc;
875 on_answer_call = acfunc;
879 * Add capability to the capability table of the end point.
881 int h323_set_capability(int cap, int dtmfMode)
883 H323Capabilities oldcaps;
888 if (!h323_end_point_exist()) {
889 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
893 /* clean up old capabilities list before changing */
894 oldcaps = endPoint->GetCapabilities();
895 for (PINDEX i=0; i< oldcaps.GetSize(); i++) {
896 codecs.AppendString(oldcaps[i].GetFormatName());
898 endPoint->RemoveCapabilities(codecs);
901 if (dtmfMode == H323_DTMF_INBAND) {
902 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
904 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
906 if (cap & AST_FORMAT_SPEEX) {
907 /* Not real sure if Asterisk acutally supports all
908 of the various different bit rates so add them
909 all and figure it out later*/
911 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
912 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
913 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
914 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
915 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
918 if (cap & AST_FORMAT_G729A) {
919 AST_G729ACapability *g729aCap;
920 AST_G729Capability *g729Cap;
921 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
922 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
925 if (cap & AST_FORMAT_G723_1) {
926 H323_G7231Capability *g7231Cap;
927 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
930 if (cap & AST_FORMAT_GSM) {
931 H323_GSM0610Capability *gsmCap;
932 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
933 gsmCap->SetTxFramesInPacket(gsmFrames);
936 if (cap & AST_FORMAT_ULAW) {
937 H323_G711Capability *g711uCap;
938 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
939 g711uCap->SetTxFramesInPacket(g711Frames);
942 if (cap & AST_FORMAT_ALAW) {
943 H323_G711Capability *g711aCap;
944 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
945 g711aCap->SetTxFramesInPacket(g711Frames);
949 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
954 /** Start the H.323 listener */
955 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
958 if (!h323_end_point_exist()) {
959 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
963 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
969 /** H.323 listener */
970 H323ListenerTCP *tcpListener;
972 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
974 if (!endPoint->StartListener(tcpListener)) {
975 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
980 cout << " == H.323 listener started" << endl;
986 int h323_set_alias(struct oh323_alias *alias)
990 PString h323id(alias->name);
991 PString e164(alias->e164);
993 if (!h323_end_point_exist()) {
994 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
998 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
999 endPoint->AddAliasName(h323id);
1000 endPoint->RemoveAliasName(localProcess->GetUserName());
1002 if (!e164.IsEmpty()) {
1003 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
1004 endPoint->AddAliasName(e164);
1006 if (strlen(alias->prefix)) {
1008 num = strsep(&p, ",");
1010 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
1011 endPoint->SupportedPrefixes += PString(num);
1012 endPoint->SetGateway();
1013 num = strsep(&p, ",");
1021 void h323_set_id(char *id)
1026 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
1030 endPoint->SetLocalUserName(h323id);
1033 void h323_show_tokens(void)
1035 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1039 /** Establish Gatekeeper communiations, if so configured,
1040 * register aliases for the H.323 endpoint to respond to.
1042 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1044 PString gkName = PString(gatekeeper);
1045 PString pass = PString(secret);
1046 H323TransportUDP *rasChannel;
1048 if (!h323_end_point_exist()) {
1049 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1054 cout << "Error: Gatekeeper cannot be NULL" << endl;
1058 if (strlen(secret)) {
1059 endPoint->SetGatekeeperPassword(pass);
1062 if (gatekeeper_discover) {
1063 /* discover the gk using multicast */
1064 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1065 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1067 cout << " *** Could not find a gatekeeper." << endl;
1071 rasChannel = new H323TransportUDP(*endPoint);
1074 cout << " *** No RAS Channel, this is bad" << endl;
1077 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1078 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1080 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1082 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1090 /** Send a DTMF tone over the H323Connection with the
1093 void h323_send_tone(const char *call_token, char tone)
1095 if (!h323_end_point_exist()) {
1096 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1100 PString token = PString(call_token);
1101 endPoint->SendUserTone(token, tone);
1104 /** Make a call to the remote endpoint.
1106 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1112 if (!h323_end_point_exist()) {
1116 noFastStart = call_options.noFastStart;
1117 noH245Tunneling = call_options.noH245Tunneling;
1119 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid, call_options.callername);
1120 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1125 int h323_clear_call(const char *call_token)
1127 if (!h323_end_point_exist()) {
1131 endPoint->ClearCall(PString(call_token));
1135 /* Send Alerting PDU to H.323 caller */
1136 int h323_send_alerting(const char *token)
1138 const PString currentToken(token);
1139 H323Connection * connection;
1141 connection = endPoint->FindConnectionWithLock(currentToken);
1144 cout << "No connection found for " << token << endl;
1148 connection->AnsweringCall(H323Connection::AnswerCallPending);
1149 connection->Unlock();
1155 /* Send Progress PDU to H.323 caller */
1156 int h323_send_progress(const char *token)
1158 const PString currentToken(token);
1159 H323Connection * connection;
1161 connection = endPoint->FindConnectionWithLock(currentToken);
1164 cout << "No connection found for " << token << endl;
1168 connection->AnsweringCall(H323Connection::AnswerCallDeferredWithMedia);
1169 connection->Unlock();
1174 /** This function tells the h.323 stack to either
1175 answer or deny an incoming call */
1176 int h323_answering_call(const char *token, int busy)
1178 const PString currentToken(token);
1179 H323Connection * connection;
1181 connection = endPoint->FindConnectionWithLock(currentToken);
1183 if (connection == NULL) {
1184 cout << "No connection found for " << token << endl;
1189 connection->AnsweringCall(H323Connection::AnswerCallNow);
1191 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1193 connection->Unlock();
1198 int h323_show_codec(int fd, int argc, char *argv[])
1200 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1204 int h323_soft_hangup(const char *data)
1206 PString token(data);
1209 result = endPoint->ClearCall(token);
1213 /* alas, this doesn't work :( */
1214 void h323_native_bridge(const char *token, char *them, char *capability)
1216 H323Channel *channel;
1217 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1220 cout << "ERROR: No connection found, this is bad\n";
1224 cout << "Native Bridge: them [" << them << "]" << endl;
1226 channel = connection->FindChannel(connection->sessionId, TRUE);
1227 connection->bridging = TRUE;
1228 connection->CloseLogicalChannelNumber(channel->GetNumber());
1230 connection->Unlock();