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();
569 cd.call_token = strdup((const char *)GetCallToken());
570 cd.call_source_aliases = strdup((const char *)sourceAliases);
571 cd.call_dest_alias = strdup((const char *)destAliases);
572 cd.call_source_e164 = strdup((const char *)sourceE164);
573 cd.call_dest_e164 = strdup((const char *)destE164);
574 cd.call_source_name = strdup((const char *)sourceName);
576 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
577 cd.sourceIp = strdup((const char *)Ip.AsString());
579 /* Notify Asterisk of the request */
580 int res = on_incoming_call(cd);
584 cout << " -- Call Failed" << endl;
589 return H323Connection::OnReceivedSignalSetup(setupPDU);
592 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
598 cout << " -- Sending SETUP message" << endl;
600 sourceAliases = setupPDU.GetSourceAliases();
601 destAliases = setupPDU.GetDestinationAlias();
604 setupPDU.GetSourceE164(sourceE164);
606 setupPDU.GetDestinationE164(destE164);
608 /* Convert complex strings */
609 // FIXME: deal more than one source alias
611 if ((s = strchr(sourceAliases, ' ')) != NULL)
613 if ((s = strchr(sourceAliases, '\t')) != NULL)
615 if ((s1 = strchr(destAliases, ' ')) != NULL)
617 if ((s1 = strchr(destAliases, '\t')) != NULL)
620 cd.call_reference = GetCallReference();
621 cd.call_token = strdup((const char *)GetCallToken());
622 cd.call_source_aliases = strdup((const char *)sourceAliases);
623 cd.call_dest_alias = strdup((const char *)destAliases);
624 cd.call_source_e164 = strdup((const char *)sourceE164);
625 cd.call_dest_e164 = strdup((const char *)destE164);
627 int res = on_outgoing_call(cd);
631 cout << " -- Call Failed" << endl;
636 return H323Connection::OnSendSignalSetup(setupPDU);
639 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
642 cout << " -- Sending RELEASE COMPLETE" << endl;
644 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
647 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
650 cout << " -- Received Facility message... " << endl;
652 return H323Connection::OnReceivedFacility(pdu);
655 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
658 cout << " -- Received RELEASE COMPLETE message..." << endl;
660 return H323Connection::OnReceivedReleaseComplete(pdu);
663 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
666 cout << " -- Closing logical channel..." << endl;
668 return H323Connection::OnClosingLogicalChannel(channel);
672 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
675 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
677 on_send_digit(GetCallReference(), tone);
678 H323Connection::SendUserInputTone(tone, duration);
681 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
683 if (mode == H323_DTMF_INBAND) {
685 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
687 on_send_digit(GetCallReference(), tone);
689 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
692 void MyH323Connection::OnUserInputString(const PString &value)
694 if (mode == H323_DTMF_RFC2833) {
696 cout << " -- Received user input string (" << value << ") from remote." << endl;
698 on_send_digit(GetCallReference(), value[0]);
702 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
703 H323Channel::Directions dir,
705 const H245_H2250LogicalChannelParameters * /*param*/)
707 struct rtp_info *info;
710 /* Determine the Local (A side) IP Address and port */
711 info = on_create_connection(GetCallReference());
717 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
718 externalPort = info->port;
721 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
722 cout << " -- externalIpAddress: " << externalIpAddress << endl;
723 cout << " -- externalPort: " << externalPort << endl;
724 cout << " -- SessionID: " << sessionID << endl;
725 cout << " -- Direction: " << dir << endl;
727 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
730 /** This callback function is invoked once upon creation of each
731 * channel for an H323 session
733 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
736 cout << " -- Started logical channel: ";
737 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
738 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
741 /* adjust the count of channels we have open */
745 cout << " -- channelsOpen = " << channelsOpen << endl;
751 /* MyH323_ExternalRTPChannel */
752 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
753 const H323Capability & capability,
754 Directions direction,
756 const PIPSocket::Address & ip,
758 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
762 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
763 const H323Capability & capability,
764 Directions direction,
766 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id)
770 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
771 const H323Capability & capability,
772 Directions direction,
774 const H323TransportAddress & data,
775 const H323TransportAddress & control)
776 : H323_ExternalRTPChannel::H323_ExternalRTPChannel(connection, capability, direction, id, data, control)
780 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
784 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(const H245_H2250LogicalChannelAckParameters & param)
786 PIPSocket::Address remoteIpAddress;
789 if (H323_ExternalRTPChannel::OnReceivedAckPDU(param)) {
790 H323_ExternalRTPChannel::GetRemoteAddress(remoteIpAddress, remotePort);
791 /* Notify Asterisk of remote RTP information */
792 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
798 /** IMPLEMENTATION OF C FUNCTIONS */
801 * The extern "C" directive takes care for
802 * the ANSI-C representation of linkable symbols
807 int h323_end_point_exist(void)
815 void h323_end_point_create(int no_fast_start, int no_h245_tunneling)
819 noFastStart = (BOOL)no_fast_start;
820 noH245Tunneling = (BOOL)no_h245_tunneling;
822 localProcess = new MyProcess();
823 localProcess->Main();
826 void h323_gk_urq(void)
828 if (!h323_end_point_exist()) {
829 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
832 endPoint->RemoveGatekeeper();
835 void h323_end_process(void)
837 endPoint->ClearAllCalls();
838 endPoint->RemoveListener(NULL);
843 void h323_debug(int flag, unsigned level)
846 PTrace:: SetLevel(level);
848 PTrace:: SetLevel(0);
852 /** Installs the callback functions on behalf of the PBX application */
853 void h323_callback_register(setup_incoming_cb ifunc,
854 setup_outbound_cb sfunc,
855 on_connection_cb confunc,
856 start_logchan_cb lfunc,
858 chan_ringing_cb rfunc,
859 con_established_cb efunc,
861 answer_call_cb acfunc)
863 on_incoming_call = ifunc;
864 on_outgoing_call = sfunc;
865 on_create_connection = confunc;
866 on_start_logical_channel = lfunc;
867 on_connection_cleared = clfunc;
868 on_chan_ringing = rfunc;
869 on_connection_established = efunc;
870 on_send_digit = dfunc;
871 on_answer_call = acfunc;
875 * Add capability to the capability table of the end point.
877 int h323_set_capability(int cap, int dtmfMode)
879 H323Capabilities oldcaps;
884 if (!h323_end_point_exist()) {
885 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
889 /* clean up old capabilities list before changing */
890 oldcaps = endPoint->GetCapabilities();
891 for (PINDEX i=0; i< oldcaps.GetSize(); i++) {
892 codecs.AppendString(oldcaps[i].GetFormatName());
894 endPoint->RemoveCapabilities(codecs);
897 if (dtmfMode == H323_DTMF_INBAND) {
898 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
900 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
902 if (cap & AST_FORMAT_SPEEX) {
903 /* Not real sure if Asterisk acutally supports all
904 of the various different bit rates so add them
905 all and figure it out later*/
907 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
908 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
909 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
910 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
911 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
914 if (cap & AST_FORMAT_G729A) {
915 AST_G729ACapability *g729aCap;
916 AST_G729Capability *g729Cap;
917 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
918 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
921 if (cap & AST_FORMAT_G723_1) {
922 H323_G7231Capability *g7231Cap;
923 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
926 if (cap & AST_FORMAT_GSM) {
927 H323_GSM0610Capability *gsmCap;
928 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
929 gsmCap->SetTxFramesInPacket(gsmFrames);
932 if (cap & AST_FORMAT_ULAW) {
933 H323_G711Capability *g711uCap;
934 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
935 g711uCap->SetTxFramesInPacket(g711Frames);
938 if (cap & AST_FORMAT_ALAW) {
939 H323_G711Capability *g711aCap;
940 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
941 g711aCap->SetTxFramesInPacket(g711Frames);
945 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
950 /** Start the H.323 listener */
951 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
954 if (!h323_end_point_exist()) {
955 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
959 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
965 /** H.323 listener */
966 H323ListenerTCP *tcpListener;
968 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
970 if (!endPoint->StartListener(tcpListener)) {
971 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
976 cout << " == H.323 listener started" << endl;
982 int h323_set_alias(struct oh323_alias *alias)
986 PString h323id(alias->name);
987 PString e164(alias->e164);
989 if (!h323_end_point_exist()) {
990 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
994 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
995 endPoint->AddAliasName(h323id);
996 endPoint->RemoveAliasName(localProcess->GetUserName());
998 if (!e164.IsEmpty()) {
999 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
1000 endPoint->AddAliasName(e164);
1002 if (strlen(alias->prefix)) {
1004 num = strsep(&p, ",");
1006 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
1007 endPoint->SupportedPrefixes += PString(num);
1008 endPoint->SetGateway();
1009 num = strsep(&p, ",");
1017 void h323_set_id(char *id)
1022 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
1026 endPoint->SetLocalUserName(h323id);
1029 void h323_show_tokens(void)
1031 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1035 /** Establish Gatekeeper communiations, if so configured,
1036 * register aliases for the H.323 endpoint to respond to.
1038 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1040 PString gkName = PString(gatekeeper);
1041 PString pass = PString(secret);
1042 H323TransportUDP *rasChannel;
1044 if (!h323_end_point_exist()) {
1045 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1050 cout << "Error: Gatekeeper cannot be NULL" << endl;
1054 if (strlen(secret)) {
1055 endPoint->SetGatekeeperPassword(pass);
1058 if (gatekeeper_discover) {
1059 /* discover the gk using multicast */
1060 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1061 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1063 cout << " *** Could not find a gatekeeper." << endl;
1067 rasChannel = new H323TransportUDP(*endPoint);
1070 cout << " *** No RAS Channel, this is bad" << endl;
1073 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1074 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1076 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1078 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1086 /** Send a DTMF tone over the H323Connection with the
1089 void h323_send_tone(const char *call_token, char tone)
1091 if (!h323_end_point_exist()) {
1092 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1096 PString token = PString(call_token);
1097 endPoint->SendUserTone(token, tone);
1100 /** Make a call to the remote endpoint.
1102 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1108 if (!h323_end_point_exist()) {
1112 noFastStart = call_options.noFastStart;
1113 noH245Tunneling = call_options.noH245Tunneling;
1115 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid, call_options.callername);
1116 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1121 int h323_clear_call(const char *call_token)
1123 if (!h323_end_point_exist()) {
1127 endPoint->ClearCall(PString(call_token));
1131 /* Send Alerting PDU to H.323 caller */
1132 int h323_send_alerting(const char *token)
1134 const PString currentToken(token);
1135 H323Connection * connection;
1137 connection = endPoint->FindConnectionWithLock(currentToken);
1140 cout << "No connection found for " << token << endl;
1144 connection->AnsweringCall(H323Connection::AnswerCallPending);
1145 connection->Unlock();
1151 /* Send Progress PDU to H.323 caller */
1152 int h323_send_progress(const char *token)
1154 const PString currentToken(token);
1155 H323Connection * connection;
1157 connection = endPoint->FindConnectionWithLock(currentToken);
1160 cout << "No connection found for " << token << endl;
1164 connection->AnsweringCall(H323Connection::AnswerCallDeferredWithMedia);
1165 connection->Unlock();
1170 /** This function tells the h.323 stack to either
1171 answer or deny an incoming call */
1172 int h323_answering_call(const char *token, int busy)
1174 const PString currentToken(token);
1175 H323Connection * connection;
1177 connection = endPoint->FindConnectionWithLock(currentToken);
1179 if (connection == NULL) {
1180 cout << "No connection found for " << token << endl;
1185 connection->AnsweringCall(H323Connection::AnswerCallNow);
1186 connection->Unlock();
1189 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1190 connection->Unlock();
1197 int h323_show_codec(int fd, int argc, char *argv[])
1199 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1203 int h323_soft_hangup(const char *data)
1205 PString token(data);
1208 result = endPoint->ClearCall(token);
1212 /* alas, this doesn't work :( */
1213 void h323_native_bridge(const char *token, char *them, char *capability)
1215 H323Channel *channel;
1216 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1219 cout << "ERROR: No connection found, this is bad\n";
1223 cout << "Native Bridge: them [" << them << "]" << endl;
1225 channel = connection->FindChannel(connection->sessionId, TRUE);
1226 connection->bridging = TRUE;
1227 connection->CloseLogicalChannelNumber(channel->GetNumber());
1229 connection->Unlock();