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 <asterisk/logger.h>
32 /* PWlib Required Components */
33 #define MAJOR_VERSION 1
34 #define MINOR_VERSION 0
35 #define BUILD_TYPE ReleaseCode
36 #define BUILD_NUMBER 0
38 /** Counter for the number of connections */
42 int mode = H323_DTMF_RFC2833;
44 /** Options for connections creation */
46 BOOL noH245Tunnelling;
47 BOOL noSilenceSuppression;
50 * We assume that only one endPoint should exist.
51 * The application cannot run the h323_end_point_create() more than once
52 * FIXME: Singleton this, for safety
54 MyH323EndPoint *endPoint = NULL;
56 /** PWLib entry point */
57 MyProcess *localProcess = NULL;
60 MyProcess::MyProcess(): PProcess("The NuFone Network's", "H.323 Channel Driver for Asterisk",
61 MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
66 void MyProcess::Main()
68 ast_verbose(" == Creating H.323 Endpoint\n");
69 endPoint = new MyH323EndPoint();
70 PTrace::Initialise(0, NULL, PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
73 #define H323_NAME OPAL_G7231_6k3"{sw}"
74 #define H323_G729 OPAL_G729 "{sw}"
75 #define H323_G729A OPAL_G729A"{sw}"
77 H323_REGISTER_CAPABILITY(H323_G7231Capability, H323_NAME);
78 H323_REGISTER_CAPABILITY(AST_G729Capability, H323_G729);
79 H323_REGISTER_CAPABILITY(AST_G729ACapability, H323_G729A);
81 H323_G7231Capability::H323_G7231Capability(BOOL annexA_)
82 : H323AudioCapability(7, 4)
87 PObject::Comparison H323_G7231Capability::Compare(const PObject & obj) const
89 Comparison result = H323AudioCapability::Compare(obj);
90 if (result != EqualTo)
93 PINDEX otherAnnexA = ((const H323_G7231Capability &)obj).annexA;
94 if (annexA < otherAnnexA)
96 if (annexA > otherAnnexA)
101 PObject * H323_G7231Capability::Clone() const
103 return new H323_G7231Capability(*this);
107 PString H323_G7231Capability::GetFormatName() const
113 unsigned H323_G7231Capability::GetSubType() const
115 return H245_AudioCapability::e_g7231;
119 BOOL H323_G7231Capability::OnSendingPDU(H245_AudioCapability & cap,
120 unsigned packetSize) const
122 cap.SetTag(H245_AudioCapability::e_g7231);
124 H245_AudioCapability_g7231 & g7231 = cap;
125 g7231.m_maxAl_sduAudioFrames = packetSize;
126 g7231.m_silenceSuppression = annexA;
132 BOOL H323_G7231Capability::OnReceivedPDU(const H245_AudioCapability & cap,
133 unsigned & packetSize)
135 if (cap.GetTag() != H245_AudioCapability::e_g7231)
138 const H245_AudioCapability_g7231 & g7231 = cap;
139 packetSize = g7231.m_maxAl_sduAudioFrames;
140 annexA = g7231.m_silenceSuppression;
146 H323Codec * H323_G7231Capability::CreateCodec(H323Codec::Direction direction) const
152 /////////////////////////////////////////////////////////////////////////////
154 AST_G729Capability::AST_G729Capability()
155 : H323AudioCapability(24, 6)
160 PObject * AST_G729Capability::Clone() const
162 return new AST_G729Capability(*this);
166 unsigned AST_G729Capability::GetSubType() const
168 return H245_AudioCapability::e_g729;
172 PString AST_G729Capability::GetFormatName() const
178 H323Codec * AST_G729Capability::CreateCodec(H323Codec::Direction direction) const
182 /////////////////////////////////////////////////////////////////////////////
184 AST_G729ACapability::AST_G729ACapability()
185 : H323AudioCapability(24, 6)
190 PObject * AST_G729ACapability::Clone() const
192 return new AST_G729ACapability(*this);
196 unsigned AST_G729ACapability::GetSubType() const
198 return H245_AudioCapability::e_g729AnnexA;
202 PString AST_G729ACapability::GetFormatName() const
208 H323Codec * AST_G729ACapability::CreateCodec(H323Codec::Direction direction) const
214 * The fullAddress parameter is used directly in the MakeCall method so
215 * the General form for the fullAddress argument is :
216 * [alias@][transport$]host[:port]
217 * default values: alias = the same value as host.
221 int MyH323EndPoint::MakeCall(const PString & dest, PString & token,
222 unsigned int *callReference, unsigned int port, char *callerid)
225 MyH323Connection * connection;
227 /* Determine whether we are using a gatekeeper or not. */
228 if (GetGatekeeper() != NULL) {
231 cout << " -- Making call to " << fullAddress << " using gatekeeper." << endl;
233 fullAddress = dest; /* host */
235 cout << " -- Making call to " << fullAddress << "." << endl;
238 if (!(connection = (MyH323Connection *)H323EndPoint::MakeCallLocked(fullAddress, token))) {
240 cout << "Error making call to \"" << fullAddress << '"' << endl;
244 *callReference = connection->GetCallReference();
247 connection->SetLocalPartyName(PString(callerid));
249 connection->Unlock();
252 cout << " -- " << GetLocalUserName() << " is calling host " << fullAddress << endl;
253 cout << " -- " << "Call token is " << (const char *)token << endl;
254 cout << " -- Call reference is " << *callReference << endl;
259 void MyH323EndPoint::SetEndpointTypeInfo( H225_EndpointType & info ) const
261 H323EndPoint::SetEndpointTypeInfo(info);
262 info.m_gateway.IncludeOptionalField(H225_GatewayInfo::e_protocol);
263 info.m_gateway.m_protocol.SetSize(1);
264 H225_SupportedProtocols &protocol=info.m_gateway.m_protocol[0];
265 protocol.SetTag(H225_SupportedProtocols::e_voice);
266 PINDEX as=SupportedPrefixes.GetSize();
267 ((H225_VoiceCaps &)protocol).m_supportedPrefixes.SetSize(as);
268 for (PINDEX p=0; p<as; p++) {
269 H323SetAliasAddress(SupportedPrefixes[p], ((H225_VoiceCaps &)protocol).m_supportedPrefixes[p].m_prefix);
273 void MyH323EndPoint::SetGateway(void)
275 terminalType = e_GatewayOnly;
278 H323Capabilities MyH323EndPoint::GetCapabilities(void)
283 BOOL MyH323EndPoint::ClearCall(const PString & token)
286 cout << " -- ClearCall: Request to clear call with token " << token << endl;
288 return H323EndPoint::ClearCall(token);
291 void MyH323EndPoint::SendUserTone(const PString &token, char tone)
293 H323Connection *connection = NULL;
295 connection = FindConnectionWithLock(token);
296 if (connection != NULL) {
297 connection->SendUserInputTone(tone, 500);
298 connection->Unlock();
302 void MyH323EndPoint::OnClosedLogicalChannel(H323Connection & connection, const H323Channel & channel)
306 cout << " channelsOpen = " << channelsOpen << endl;
307 H323EndPoint::OnClosedLogicalChannel(connection, channel);
310 BOOL MyH323EndPoint::OnConnectionForwarded(H323Connection & connection,
311 const PString & forwardParty,
312 const H323SignalPDU & pdu)
315 cout << " -- Call Forwarded to " << forwardParty << endl;
320 BOOL MyH323EndPoint::ForwardConnection(H323Connection & connection,
321 const PString & forwardParty,
322 const H323SignalPDU & pdu)
325 cout << " -- Forwarding call to " << forwardParty << endl;
327 return H323EndPoint::ForwardConnection(connection, forwardParty, pdu);
330 void MyH323EndPoint::OnConnectionEstablished(H323Connection & connection, const PString & estCallToken)
333 cout << " -- Connection Established with \"" << connection.GetRemotePartyName() << "\"" << endl;
335 on_connection_established(connection.GetCallReference());
338 /** OnConnectionCleared callback function is called upon the dropping of an established
341 void MyH323EndPoint::OnConnectionCleared(H323Connection & connection, const PString & clearedCallToken)
343 PString remoteName = connection.GetRemotePartyName();
347 cd.call_reference = connection.GetCallReference();
348 cd.call_token = (const char *)connection.GetCallToken();
349 cd.call_source_aliases = (const char *)connection.GetRemotePartyName();
351 /* Convert complex strings */
353 if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
356 /* Invoke the PBX application registered callback */
357 on_connection_cleared(cd);
359 switch (connection.GetCallEndReason()) {
360 case H323Connection::EndedByCallForwarded :
362 cout << " -- " << remoteName << " has forwarded the call" << endl;
364 case H323Connection::EndedByRemoteUser :
366 cout << " -- " << remoteName << " has cleared the call" << endl;
368 case H323Connection::EndedByCallerAbort :
370 cout << " -- " << remoteName << " has stopped calling" << endl;
372 case H323Connection::EndedByRefusal :
374 cout << " -- " << remoteName << " did not accept your call" << endl;
376 case H323Connection::EndedByRemoteBusy :
378 cout << " -- " << remoteName << " was busy" << endl;
380 case H323Connection::EndedByRemoteCongestion :
382 cout << " -- Congested link to " << remoteName << endl;
384 case H323Connection::EndedByNoAnswer :
386 cout << " -- " << remoteName << " did not answer your call" << endl;
388 case H323Connection::EndedByTransportFail :
390 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
392 case H323Connection::EndedByCapabilityExchange :
394 cout << " -- Could not find common codec with " << remoteName << endl;
396 case H323Connection::EndedByNoAccept :
398 cout << " -- Did not accept incoming call from " << remoteName << endl;
400 case H323Connection::EndedByAnswerDenied :
402 cout << " -- Refused incoming call from " << remoteName << endl;
404 case H323Connection::EndedByNoUser :
406 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
408 case H323Connection::EndedByNoBandwidth :
410 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
412 case H323Connection::EndedByUnreachable :
414 cout << " -- " << remoteName << " could not be reached." << endl;
416 case H323Connection::EndedByHostOffline :
418 cout << " -- " << remoteName << " is not online." << endl;
420 case H323Connection::EndedByNoEndPoint :
422 cout << " -- No phone running for " << remoteName << endl;
424 case H323Connection::EndedByConnectFail :
426 cout << " -- Transport error calling " << remoteName << endl;
430 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
434 if(connection.IsEstablished())
436 cout << " -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
440 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
442 unsigned options = 0;
445 options |= H323Connection::FastStartOptionDisable;
447 if (noH245Tunnelling)
448 options |= H323Connection::H245TunnelingOptionDisable;
450 return new MyH323Connection(*this, callReference, options);
453 /* MyH323Connection */
454 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
456 : H323Connection(ep, callReference, options)
458 remoteIpAddress = 0; // IP Address of remote endpoint
459 remotePort = 0; // remote endpoint Data port (control is dataPort+1)
462 cout << " == New H.323 Connection created." << endl;
467 MyH323Connection::~MyH323Connection()
470 cout << " == H.323 Connection deleted." << endl;
475 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
476 const H323SignalPDU & /*setupPDU*/,
477 H323SignalPDU & /*connectPDU*/)
479 /* The call will be answered later with "AnsweringCall()" function.
481 return H323Connection::AnswerCallAlertWithMedia;
484 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
488 cout << " -- Ringing phone for \"" << username << "\"" << endl;
493 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
497 ast_verbose(" -- Received SETUP message\n");
503 PString sourceAliases;
506 PIPSocket::Address Ip;
509 sourceAliases = setupPDU.GetSourceAliases();
510 destAliases = setupPDU.GetDestinationAlias();
513 setupPDU.GetSourceE164(sourceE164);
515 setupPDU.GetDestinationE164(destE164);
517 /* Convert complex strings */
518 // FIXME: deal more than one source alias
520 if ((s = strchr(sourceAliases, ' ')) != NULL)
522 if ((s = strchr(sourceAliases, '\t')) != NULL)
525 if ((s1 = strchr(destAliases, ' ')) != NULL)
527 if ((s1 = strchr(destAliases, '\t')) != NULL)
530 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
532 sourceIp = Ip.AsString();
534 cd.call_reference = GetCallReference();
535 cd.call_token = (const char *)GetCallToken();
536 cd.call_source_aliases = (const char *)sourceAliases;
537 cd.call_dest_alias = (const char *)destAliases;
538 cd.call_source_e164 = (const char *)sourceE164;
539 cd.call_dest_e164 = (const char *)destE164;
540 cd.sourceIp = (const char *)sourceIp;
542 /* Notify Asterisk of the request */
543 int res = on_incoming_call(cd);
547 cout << " -- Call Failed" << endl;
552 return H323Connection::OnReceivedSignalSetup(setupPDU);
555 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
560 cout << " -- Sending SETUP message" << endl;
562 sourceAliases = setupPDU.GetSourceAliases();
563 destAliases = setupPDU.GetDestinationAlias();
566 setupPDU.GetSourceE164(sourceE164);
568 setupPDU.GetDestinationE164(destE164);
570 /* Convert complex strings */
571 // FIXME: deal more than one source alias
573 if ((s = strchr(sourceAliases, ' ')) != NULL)
575 if ((s = strchr(sourceAliases, '\t')) != NULL)
578 if ((s1 = strchr(destAliases, ' ')) != NULL)
580 if ((s1 = strchr(destAliases, '\t')) != NULL)
583 cd.call_reference = GetCallReference();
584 cd.call_token = (const char *)GetCallToken();
585 cd.call_source_aliases = (const char *)sourceAliases;
586 cd.call_dest_alias = (const char *)destAliases;
587 cd.call_source_e164 = (const char *)sourceE164;
588 cd.call_dest_e164 = (const char *)destE164;
590 int res = on_outgoing_call(cd);
594 cout << " -- Call Failed" << endl;
599 return H323Connection::OnSendSignalSetup(setupPDU);
602 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
605 cout << " -- Sending RELEASE COMPLETE" << endl;
607 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
610 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
613 cout << " -- Received Facility message... " << endl;
615 return H323Connection::OnReceivedFacility(pdu);
618 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
621 cout << " -- Received RELEASE COMPLETE message..." << endl;
623 return H323Connection::OnReceivedReleaseComplete(pdu);
626 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
629 cout << " -- Closing logical channel..." << endl;
631 return H323Connection::OnClosingLogicalChannel(channel);
635 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
638 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
640 on_send_digit(GetCallReference(), tone);
641 H323Connection::SendUserInputTone(tone, duration);
644 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
646 if (mode == H323_DTMF_INBAND) {
648 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
650 on_send_digit(GetCallReference(), tone);
652 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
655 void MyH323Connection::OnUserInputString(const PString &value)
657 if (mode == H323_DTMF_RFC2833) {
659 cout << " -- Received user input string (" << value << ") from remote." << endl;
661 on_send_digit(GetCallReference(), value[0]);
665 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
666 H323Channel::Directions dir,
668 const H245_H2250LogicalChannelParameters * /*param*/)
670 struct rtp_info *info;
673 /* Determine the Local (A side) IP Address and port */
674 info = on_create_connection(GetCallReference());
680 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
681 externalPort = info->port;
684 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
685 cout << " -- externalIpAddress: " << externalIpAddress << endl;
686 cout << " -- externalPort: " << externalPort << endl;
687 cout << " -- SessionID: " << sessionID << endl;
688 cout << " -- Direction: " << dir << endl;
690 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
693 /** This callback function is invoked once upon creation of each
694 * channel for an H323 session
696 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
699 cout << " -- Started logical channel: ";
700 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
701 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
703 // adjust the count of channels we have open
706 cout << " -- channelsOpen = " << channelsOpen << endl;
708 H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
709 external.GetRemoteAddress(remoteIpAddress, remotePort);
712 cout << " -- remoteIpAddress: " << remoteIpAddress << endl;
713 cout << " -- remotePort: " << remotePort << endl;
714 cout << " -- ExternalIpAddress: " << externalIpAddress << endl;
715 cout << " -- ExternalPort: " << externalPort << endl;
717 /* Notify Asterisk of remote RTP information */
718 on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
723 /* MyH323_ExternalRTPChannel */
724 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
725 const H323Capability & capability,
726 Directions direction,
728 const PIPSocket::Address & ip,
730 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
733 cout << " == New H.323 ExternalRTPChannel created." << endl;
738 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
741 cout << " == H.323 ExternalRTPChannel deleted." << endl;
746 BOOL MyH323_ExternalRTPChannel::OnReceivedPDU(
747 const H245_H2250LogicalChannelParameters & param,
748 unsigned & errorCode)
751 cout << " MyH323_ExternalRTPChannel::OnReceivedPDU " << endl;
753 return H323_ExternalRTPChannel::OnReceivedPDU( param, errorCode );
756 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(
757 const H245_H2250LogicalChannelAckParameters & param)
760 PIPSocket::Address remoteIpAddress; // IP Address of remote endpoint
761 WORD remotePort; // remote endpoint Data port (control is dataPort+1)
764 cout << " MyH323_ExternalRTPChannel::OnReceivedAckPDU " << endl;
767 if (H323_ExternalRTPChannel::OnReceivedAckPDU( param )) {
768 GetRemoteAddress(remoteIpAddress, remotePort);
770 cout << " -- remoteIpAddress: " << remoteIpAddress << endl;
771 cout << " -- remotePort: " << remotePort << endl;
773 /* Notify Asterisk of remote RTP information */
774 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
781 /** IMPLEMENTATION OF C FUNCTIONS */
784 * The extern "C" directive takes care for
785 * the ANSI-C representation of linkable symbols
789 int h323_end_point_exist(void)
797 void h323_end_point_create(void)
800 localProcess = new MyProcess();
801 localProcess->Main();
804 void h323_gk_urq(void)
806 if (!h323_end_point_exist()) {
807 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
810 endPoint->RemoveGatekeeper();
813 void h323_end_process(void)
815 endPoint->ClearAllCalls();
816 endPoint->RemoveListener(NULL);
821 void h323_debug(int flag, unsigned level)
824 PTrace:: SetLevel(level);
826 PTrace:: SetLevel(0);
830 /** Installs the callback functions on behalf of the PBX application */
831 void h323_callback_register(setup_incoming_cb ifunc,
832 setup_outbound_cb sfunc,
833 on_connection_cb confunc,
834 start_logchan_cb lfunc,
836 con_established_cb efunc,
839 on_incoming_call = ifunc;
840 on_outgoing_call = sfunc;
841 on_create_connection = confunc;
842 on_start_logical_channel = lfunc;
843 on_connection_cleared = clfunc;
844 on_connection_established = efunc;
845 on_send_digit = dfunc;
849 * Add capability to the capability table of the end point.
851 int h323_set_capability(int cap, int dtmfMode)
853 H323Capabilities oldcaps;
857 if (!h323_end_point_exist()) {
858 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
862 /* clean up old capabilities list before changing */
863 oldcaps = endPoint->GetCapabilities();
867 if (dtmfMode == H323_DTMF_INBAND) {
868 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
870 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
872 if (cap & AST_FORMAT_SPEEX) {
873 /* Not real sure if Asterisk acutally supports all
874 of the various different bit rates so add them
875 all and figure it out later*/
877 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
878 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
879 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
880 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
881 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
884 if (cap & AST_FORMAT_G729A) {
885 AST_G729ACapability *g729aCap;
886 AST_G729Capability *g729Cap;
887 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
888 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
891 if (cap & AST_FORMAT_G723_1) {
892 H323_G7231Capability *g7231Cap;
893 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
896 if (cap & AST_FORMAT_GSM) {
897 H323_GSM0610Capability *gsmCap;
898 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
899 gsmCap->SetTxFramesInPacket(gsmFrames);
902 if (cap & AST_FORMAT_ULAW) {
903 H323_G711Capability *g711uCap;
904 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
905 g711uCap->SetTxFramesInPacket(g711Frames);
908 if (cap & AST_FORMAT_ALAW) {
909 H323_G711Capability *g711aCap;
910 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
911 g711aCap->SetTxFramesInPacket(g711Frames);
915 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
920 /** Start the H.323 listener */
921 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
924 if (!h323_end_point_exist()) {
925 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
929 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
935 /** H.323 listener */
936 H323ListenerTCP *tcpListener;
938 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
940 if (!endPoint->StartListener(tcpListener)) {
941 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
946 cout << " == H.323 listener started" << endl;
952 int h323_set_alias(struct oh323_alias *alias)
956 PString h323id(alias->name);
957 PString e164(alias->e164);
959 if (!h323_end_point_exist()) {
960 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
964 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
965 endPoint->AddAliasName(h323id);
966 endPoint->RemoveAliasName(localProcess->GetUserName());
968 if (!e164.IsEmpty()) {
969 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
970 endPoint->AddAliasName(e164);
972 if (strlen(alias->prefix)) {
974 num = strsep(&p, ",");
976 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
977 endPoint->SupportedPrefixes += PString(num);
978 endPoint->SetGateway();
979 num = strsep(&p, ",");
987 void h323_set_id(char *id)
992 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
996 endPoint->SetLocalUserName(h323id);
999 void h323_show_tokens(void)
1001 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1005 /** Establish Gatekeeper communiations, if so configured,
1006 * register aliases for the H.323 endpoint to respond to.
1008 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1010 PString gkName = PString(gatekeeper);
1011 PString pass = PString(secret);
1012 H323TransportUDP *rasChannel;
1014 if (!h323_end_point_exist()) {
1015 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1020 cout << "Error: Gatekeeper cannot be NULL" << endl;
1024 if (strlen(secret)) {
1025 endPoint->SetGatekeeperPassword(pass);
1028 if (gatekeeper_discover) {
1029 /* discover the gk using multicast */
1030 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1031 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1033 cout << " *** Could not find a gatekeeper." << endl;
1037 rasChannel = new H323TransportUDP(*endPoint);
1040 cout << " *** No RAS Channel, this is bad" << endl;
1043 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1044 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1046 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1048 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1056 /** Send a DTMF tone over the H323Connection with the
1059 void h323_send_tone(const char *call_token, char tone)
1061 if (!h323_end_point_exist()) {
1062 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1066 PString token = PString(call_token);
1067 endPoint->SendUserTone(token, tone);
1070 /** Make a call to the remote endpoint.
1072 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1077 if (!h323_end_point_exist()) {
1083 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid);
1084 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1089 int h323_clear_call(const char *call_token)
1091 if (!h323_end_point_exist()) {
1095 endPoint->ClearCall(PString(call_token));
1099 /** This function tells the h.323 stack to either
1100 answer or deny an incoming call */
1101 int h323_answering_call(const char *token, int busy)
1103 const PString currentToken(token);
1104 H323Connection * connection;
1106 connection = endPoint->FindConnectionWithLock(currentToken);
1108 if (connection == NULL) {
1109 cout << "No connection found for " << token << endl;
1114 connection->AnsweringCall(H323Connection::AnswerCallNow);
1115 connection->Unlock();
1118 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1119 connection->Unlock();
1126 int h323_show_codec(int fd, int argc, char *argv[])
1128 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1132 int h323_soft_hangup(const char *data)
1134 PString token(data);
1137 result = endPoint->ClearCall(token);
1141 /* alas, this doesn't work :( */
1142 void h323_native_bridge(const char *token, char *them, char *capability)
1144 H323Channel *channel;
1145 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1148 cout << "ERROR: No connection found, this is bad\n";
1152 cout << "Native Bridge: them [" << them << "]" << endl;
1154 channel = connection->FindChannel(connection->sessionId, TRUE);
1155 connection->bridging = TRUE;
1156 connection->CloseLogicalChannelNumber(channel->GetNumber());
1158 connection->Unlock();