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)
345 PIPSocket::Address Ip;
348 remoteName = connection.GetRemotePartyName();
350 cd.call_reference = connection.GetCallReference();
351 cd.call_token = (const char *)clearedCallToken;
352 cd.call_source_aliases = (const char *)connection.GetRemotePartyName();
354 connection.GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
355 cd.sourceIp = (const char *)Ip.AsString();
357 /* Convert complex strings */
359 if ((s = strchr(cd.call_source_aliases, ' ')) != NULL)
362 switch (connection.GetCallEndReason()) {
363 case H323Connection::EndedByCallForwarded :
365 cout << " -- " << remoteName << " has forwarded the call" << endl;
367 case H323Connection::EndedByRemoteUser :
369 cout << " -- " << remoteName << " has cleared the call" << endl;
371 case H323Connection::EndedByCallerAbort :
373 cout << " -- " << remoteName << " has stopped calling" << endl;
375 case H323Connection::EndedByRefusal :
377 cout << " -- " << remoteName << " did not accept your call" << endl;
379 case H323Connection::EndedByRemoteBusy :
381 cout << " -- " << remoteName << " was busy" << endl;
383 case H323Connection::EndedByRemoteCongestion :
385 cout << " -- Congested link to " << remoteName << endl;
387 case H323Connection::EndedByNoAnswer :
389 cout << " -- " << remoteName << " did not answer your call" << endl;
391 case H323Connection::EndedByTransportFail :
393 cout << " -- Call with " << remoteName << " ended abnormally" << endl;
395 case H323Connection::EndedByCapabilityExchange :
397 cout << " -- Could not find common codec with " << remoteName << endl;
399 case H323Connection::EndedByNoAccept :
401 cout << " -- Did not accept incoming call from " << remoteName << endl;
403 case H323Connection::EndedByAnswerDenied :
405 cout << " -- Refused incoming call from " << remoteName << endl;
407 case H323Connection::EndedByNoUser :
409 cout << " -- Remote endpoint could not find user: " << remoteName << endl;
411 case H323Connection::EndedByNoBandwidth :
413 cout << " -- Call to " << remoteName << " aborted, insufficient bandwidth." << endl;
415 case H323Connection::EndedByUnreachable :
417 cout << " -- " << remoteName << " could not be reached." << endl;
419 case H323Connection::EndedByHostOffline :
421 cout << " -- " << remoteName << " is not online." << endl;
423 case H323Connection::EndedByNoEndPoint :
425 cout << " -- No phone running for " << remoteName << endl;
427 case H323Connection::EndedByConnectFail :
429 cout << " -- Transport error calling " << remoteName << endl;
433 cout << " -- Call with " << remoteName << " completed (" << connection.GetCallEndReason() << ")" << endl;
437 if(connection.IsEstablished())
439 cout << " -- Call duration " << setprecision(0) << setw(5) << (PTime() - connection.GetConnectionStartTime()) << endl;
441 /* Invoke the PBX application registered callback */
442 on_connection_cleared(cd);
448 H323Connection * MyH323EndPoint::CreateConnection(unsigned callReference, void *outbound)
450 unsigned options = 0;
453 options |= H323Connection::FastStartOptionDisable;
455 if (noH245Tunnelling)
456 options |= H323Connection::H245TunnelingOptionDisable;
458 return new MyH323Connection(*this, callReference, options);
461 /* MyH323Connection */
462 MyH323Connection::MyH323Connection(MyH323EndPoint & ep, unsigned callReference,
464 : H323Connection(ep, callReference, options)
466 remoteIpAddress = 0; // IP Address of remote endpoint
467 remotePort = 0; // remote endpoint Data port (control is dataPort+1)
470 cout << " == New H.323 Connection created." << endl;
475 MyH323Connection::~MyH323Connection()
478 cout << " == H.323 Connection deleted." << endl;
483 H323Connection::AnswerCallResponse MyH323Connection::OnAnswerCall(const PString & caller,
484 const H323SignalPDU & /*setupPDU*/,
485 H323SignalPDU & /*connectPDU*/)
487 /* The call will be answered later with "AnsweringCall()" function.
489 return H323Connection::AnswerCallAlertWithMedia;
492 BOOL MyH323Connection::OnAlerting(const H323SignalPDU & /*alertingPDU*/, const PString & username)
496 cout << " -- Ringing phone for \"" << username << "\"" << endl;
501 BOOL MyH323Connection::OnReceivedSignalSetup(const H323SignalPDU & setupPDU)
505 ast_verbose(" -- Received SETUP message\n");
511 PString sourceAliases;
513 PIPSocket::Address Ip;
517 sourceAliases = setupPDU.GetSourceAliases();
518 destAliases = setupPDU.GetDestinationAlias();
521 setupPDU.GetSourceE164(sourceE164);
523 setupPDU.GetDestinationE164(destE164);
525 /* Convert complex strings */
526 // FIXME: deal more than one source alias
527 if ((s = strchr(sourceAliases, ' ')) != NULL)
529 if ((s = strchr(sourceAliases, '\t')) != NULL)
531 if ((s1 = strchr(destAliases, ' ')) != NULL)
533 if ((s1 = strchr(destAliases, '\t')) != NULL)
537 cd.call_reference = GetCallReference();
538 cd.call_token = (const char *)GetCallToken();
539 cd.call_source_aliases = (const char *)sourceAliases;
540 cd.call_dest_alias = (const char *)destAliases;
541 cd.call_source_e164 = (const char *)sourceE164;
542 cd.call_dest_e164 = (const char *)destE164;
544 GetSignallingChannel()->GetRemoteAddress().GetIpAndPort(Ip, sourcePort);
545 cd.sourceIp = (const char *)Ip.AsString();
547 /* Notify Asterisk of the request */
548 int res = on_incoming_call(cd);
552 cout << " -- Call Failed" << endl;
557 return H323Connection::OnReceivedSignalSetup(setupPDU);
560 BOOL MyH323Connection::OnSendSignalSetup(H323SignalPDU & setupPDU)
566 cout << " -- Sending SETUP message" << endl;
568 sourceAliases = setupPDU.GetSourceAliases();
569 destAliases = setupPDU.GetDestinationAlias();
572 setupPDU.GetSourceE164(sourceE164);
574 setupPDU.GetDestinationE164(destE164);
576 /* Convert complex strings */
577 // FIXME: deal more than one source alias
579 if ((s = strchr(sourceAliases, ' ')) != NULL)
581 if ((s = strchr(sourceAliases, '\t')) != NULL)
583 if ((s1 = strchr(destAliases, ' ')) != NULL)
585 if ((s1 = strchr(destAliases, '\t')) != NULL)
588 cd.call_reference = GetCallReference();
589 cd.call_token = (const char *)GetCallToken();
590 cd.call_source_aliases = (const char *)sourceAliases;
591 cd.call_dest_alias = (const char *)destAliases;
592 cd.call_source_e164 = (const char *)sourceE164;
593 cd.call_dest_e164 = (const char *)destE164;
595 int res = on_outgoing_call(cd);
599 cout << " -- Call Failed" << endl;
604 return H323Connection::OnSendSignalSetup(setupPDU);
607 BOOL MyH323Connection::OnSendReleaseComplete(H323SignalPDU & releaseCompletePDU)
610 cout << " -- Sending RELEASE COMPLETE" << endl;
612 return H323Connection::OnSendReleaseComplete(releaseCompletePDU);
615 BOOL MyH323Connection::OnReceivedFacility(const H323SignalPDU & pdu)
618 cout << " -- Received Facility message... " << endl;
620 return H323Connection::OnReceivedFacility(pdu);
623 void MyH323Connection::OnReceivedReleaseComplete(const H323SignalPDU & pdu)
626 cout << " -- Received RELEASE COMPLETE message..." << endl;
628 return H323Connection::OnReceivedReleaseComplete(pdu);
631 BOOL MyH323Connection::OnClosingLogicalChannel(H323Channel & channel)
634 cout << " -- Closing logical channel..." << endl;
636 return H323Connection::OnClosingLogicalChannel(channel);
640 void MyH323Connection::SendUserInputTone(char tone, unsigned duration)
643 cout << " -- Sending user input tone (" << tone << ") to remote" << endl;
645 on_send_digit(GetCallReference(), tone);
646 H323Connection::SendUserInputTone(tone, duration);
649 void MyH323Connection::OnUserInputTone(char tone, unsigned duration, unsigned logicalChannel, unsigned rtpTimestamp)
651 if (mode == H323_DTMF_INBAND) {
653 cout << " -- Received user input tone (" << tone << ") from remote" << endl;
655 on_send_digit(GetCallReference(), tone);
657 H323Connection::OnUserInputTone(tone, duration, logicalChannel, rtpTimestamp);
660 void MyH323Connection::OnUserInputString(const PString &value)
662 if (mode == H323_DTMF_RFC2833) {
664 cout << " -- Received user input string (" << value << ") from remote." << endl;
666 on_send_digit(GetCallReference(), value[0]);
670 H323Channel * MyH323Connection::CreateRealTimeLogicalChannel(const H323Capability & capability,
671 H323Channel::Directions dir,
673 const H245_H2250LogicalChannelParameters * /*param*/)
675 struct rtp_info *info;
678 /* Determine the Local (A side) IP Address and port */
679 info = on_create_connection(GetCallReference());
685 GetControlChannel().GetLocalAddress().GetIpAndPort(externalIpAddress, port);
686 externalPort = info->port;
689 cout << " =*= In CreateRealTimeLogicalChannel for call " << GetCallReference() << endl;
690 cout << " -- externalIpAddress: " << externalIpAddress << endl;
691 cout << " -- externalPort: " << externalPort << endl;
692 cout << " -- SessionID: " << sessionID << endl;
693 cout << " -- Direction: " << dir << endl;
695 return new MyH323_ExternalRTPChannel(*this, capability, dir, sessionID, externalIpAddress, externalPort);
698 /** This callback function is invoked once upon creation of each
699 * channel for an H323 session
701 BOOL MyH323Connection::OnStartLogicalChannel(H323Channel & channel)
704 cout << " -- Started logical channel: ";
705 cout << ((channel.GetDirection()==H323Channel::IsTransmitter)?"sending ":((channel.GetDirection()==H323Channel::IsReceiver)?"receiving ":" "));
706 cout << (const char *)(channel.GetCapability()).GetFormatName() << endl;
708 // adjust the count of channels we have open
711 cout << " -- channelsOpen = " << channelsOpen << endl;
713 H323_ExternalRTPChannel & external = (H323_ExternalRTPChannel &)channel;
714 external.GetRemoteAddress(remoteIpAddress, remotePort);
717 cout << " -- remoteIpAddress: " << remoteIpAddress << endl;
718 cout << " -- remotePort: " << remotePort << endl;
719 cout << " -- ExternalIpAddress: " << externalIpAddress << endl;
720 cout << " -- ExternalPort: " << externalPort << endl;
722 /* Notify Asterisk of remote RTP information */
723 on_start_logical_channel(GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
728 /* MyH323_ExternalRTPChannel */
729 MyH323_ExternalRTPChannel::MyH323_ExternalRTPChannel(MyH323Connection & connection,
730 const H323Capability & capability,
731 Directions direction,
733 const PIPSocket::Address & ip,
735 : H323_ExternalRTPChannel(connection, capability, direction, sessionID, ip, dataPort)
738 cout << " == New H.323 ExternalRTPChannel created." << endl;
743 MyH323_ExternalRTPChannel::~MyH323_ExternalRTPChannel()
746 cout << " == H.323 ExternalRTPChannel deleted." << endl;
751 BOOL MyH323_ExternalRTPChannel::OnReceivedPDU(
752 const H245_H2250LogicalChannelParameters & param,
753 unsigned & errorCode)
756 cout << " MyH323_ExternalRTPChannel::OnReceivedPDU " << endl;
758 return H323_ExternalRTPChannel::OnReceivedPDU( param, errorCode );
761 BOOL MyH323_ExternalRTPChannel::OnReceivedAckPDU(
762 const H245_H2250LogicalChannelAckParameters & param)
765 PIPSocket::Address remoteIpAddress; // IP Address of remote endpoint
766 WORD remotePort; // remote endpoint Data port (control is dataPort+1)
769 cout << " MyH323_ExternalRTPChannel::OnReceivedAckPDU " << endl;
772 if (H323_ExternalRTPChannel::OnReceivedAckPDU( param )) {
773 GetRemoteAddress(remoteIpAddress, remotePort);
775 cout << " -- remoteIpAddress: " << remoteIpAddress << endl;
776 cout << " -- remotePort: " << remotePort << endl;
778 /* Notify Asterisk of remote RTP information */
779 on_start_logical_channel(connection.GetCallReference(), (const char *)remoteIpAddress.AsString(), remotePort);
786 /** IMPLEMENTATION OF C FUNCTIONS */
789 * The extern "C" directive takes care for
790 * the ANSI-C representation of linkable symbols
794 int h323_end_point_exist(void)
802 void h323_end_point_create(void)
805 localProcess = new MyProcess();
806 localProcess->Main();
809 void h323_gk_urq(void)
811 if (!h323_end_point_exist()) {
812 cout << " ERROR: [h323_gk_urq] No Endpoint, this is bad" << endl;
815 endPoint->RemoveGatekeeper();
818 void h323_end_process(void)
820 endPoint->ClearAllCalls();
821 endPoint->RemoveListener(NULL);
826 void h323_debug(int flag, unsigned level)
829 PTrace:: SetLevel(level);
831 PTrace:: SetLevel(0);
835 /** Installs the callback functions on behalf of the PBX application */
836 void h323_callback_register(setup_incoming_cb ifunc,
837 setup_outbound_cb sfunc,
838 on_connection_cb confunc,
839 start_logchan_cb lfunc,
841 con_established_cb efunc,
844 on_incoming_call = ifunc;
845 on_outgoing_call = sfunc;
846 on_create_connection = confunc;
847 on_start_logical_channel = lfunc;
848 on_connection_cleared = clfunc;
849 on_connection_established = efunc;
850 on_send_digit = dfunc;
854 * Add capability to the capability table of the end point.
856 int h323_set_capability(int cap, int dtmfMode)
858 H323Capabilities oldcaps;
862 if (!h323_end_point_exist()) {
863 cout << " ERROR: [h323_set_capablity] No Endpoint, this is bad" << endl;
867 /* clean up old capabilities list before changing */
868 oldcaps = endPoint->GetCapabilities();
872 if (dtmfMode == H323_DTMF_INBAND) {
873 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsTone);
875 endPoint->SetSendUserInputMode(H323Connection::SendUserInputAsInlineRFC2833);
877 if (cap & AST_FORMAT_SPEEX) {
878 /* Not real sure if Asterisk acutally supports all
879 of the various different bit rates so add them
880 all and figure it out later*/
882 endPoint->SetCapability(0, 0, new SpeexNarrow2AudioCapability());
883 endPoint->SetCapability(0, 0, new SpeexNarrow3AudioCapability());
884 endPoint->SetCapability(0, 0, new SpeexNarrow4AudioCapability());
885 endPoint->SetCapability(0, 0, new SpeexNarrow5AudioCapability());
886 endPoint->SetCapability(0, 0, new SpeexNarrow6AudioCapability());
889 if (cap & AST_FORMAT_G729A) {
890 AST_G729ACapability *g729aCap;
891 AST_G729Capability *g729Cap;
892 endPoint->SetCapability(0, 0, g729aCap = new AST_G729ACapability);
893 endPoint->SetCapability(0, 0, g729Cap = new AST_G729Capability);
896 if (cap & AST_FORMAT_G723_1) {
897 H323_G7231Capability *g7231Cap;
898 endPoint->SetCapability(0, 0, g7231Cap = new H323_G7231Capability);
901 if (cap & AST_FORMAT_GSM) {
902 H323_GSM0610Capability *gsmCap;
903 endPoint->SetCapability(0, 0, gsmCap = new H323_GSM0610Capability);
904 gsmCap->SetTxFramesInPacket(gsmFrames);
907 if (cap & AST_FORMAT_ULAW) {
908 H323_G711Capability *g711uCap;
909 endPoint->SetCapability(0, 0, g711uCap = new H323_G711Capability(H323_G711Capability::muLaw));
910 g711uCap->SetTxFramesInPacket(g711Frames);
913 if (cap & AST_FORMAT_ALAW) {
914 H323_G711Capability *g711aCap;
915 endPoint->SetCapability(0, 0, g711aCap = new H323_G711Capability(H323_G711Capability::ALaw));
916 g711aCap->SetTxFramesInPacket(g711Frames);
920 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
925 /** Start the H.323 listener */
926 int h323_start_listener(int listenPort, struct sockaddr_in bindaddr)
929 if (!h323_end_point_exist()) {
930 cout << "ERROR: [h323_start_listener] No Endpoint, this is bad!" << endl;
934 PIPSocket::Address interfaceAddress(bindaddr.sin_addr);
940 /** H.323 listener */
941 H323ListenerTCP *tcpListener;
943 tcpListener = new H323ListenerTCP(*endPoint, interfaceAddress, (WORD)listenPort);
945 if (!endPoint->StartListener(tcpListener)) {
946 cout << "ERROR: Could not open H.323 listener port on " << ((H323ListenerTCP *) tcpListener)->GetListenerPort() << endl;
951 cout << " == H.323 listener started" << endl;
957 int h323_set_alias(struct oh323_alias *alias)
961 PString h323id(alias->name);
962 PString e164(alias->e164);
964 if (!h323_end_point_exist()) {
965 cout << "ERROR: [h323_set_alias] No Endpoint, this is bad!" << endl;
969 cout << " == Adding alias \"" << h323id << "\" to endpoint" << endl;
970 endPoint->AddAliasName(h323id);
971 endPoint->RemoveAliasName(localProcess->GetUserName());
973 if (!e164.IsEmpty()) {
974 cout << " == Adding E.164 \"" << e164 << "\" to endpoint" << endl;
975 endPoint->AddAliasName(e164);
977 if (strlen(alias->prefix)) {
979 num = strsep(&p, ",");
981 cout << " == Adding Prefix \"" << num << "\" to endpoint" << endl;
982 endPoint->SupportedPrefixes += PString(num);
983 endPoint->SetGateway();
984 num = strsep(&p, ",");
992 void h323_set_id(char *id)
997 cout << " == Using '" << h323id << "' as our H.323ID for this call" << endl;
1001 endPoint->SetLocalUserName(h323id);
1004 void h323_show_tokens(void)
1006 cout << "Current call tokens: " << setprecision(2) << endPoint->GetAllConnections() << endl;
1010 /** Establish Gatekeeper communiations, if so configured,
1011 * register aliases for the H.323 endpoint to respond to.
1013 int h323_set_gk(int gatekeeper_discover, char *gatekeeper, char *secret)
1015 PString gkName = PString(gatekeeper);
1016 PString pass = PString(secret);
1017 H323TransportUDP *rasChannel;
1019 if (!h323_end_point_exist()) {
1020 cout << "ERROR: [h323_set_gk] No Endpoint, this is bad!" << endl;
1025 cout << "Error: Gatekeeper cannot be NULL" << endl;
1029 if (strlen(secret)) {
1030 endPoint->SetGatekeeperPassword(pass);
1033 if (gatekeeper_discover) {
1034 /* discover the gk using multicast */
1035 if (endPoint->DiscoverGatekeeper(new H323TransportUDP(*endPoint))) {
1036 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1038 cout << " *** Could not find a gatekeeper." << endl;
1042 rasChannel = new H323TransportUDP(*endPoint);
1045 cout << " *** No RAS Channel, this is bad" << endl;
1048 if (endPoint->SetGatekeeper(gkName, rasChannel)) {
1049 cout << " == Using " << (endPoint->GetGatekeeper())->GetName() << " as our Gatekeeper." << endl;
1051 cout << " *** Error registering with gatekeeper \"" << gkName << "\". " << endl;
1053 /* XXX Maybe we should fire a new thread to attempt to re-register later and not kill asterisk here? */
1061 /** Send a DTMF tone over the H323Connection with the
1064 void h323_send_tone(const char *call_token, char tone)
1066 if (!h323_end_point_exist()) {
1067 cout << "ERROR: [h323_send_tone] No Endpoint, this is bad!" << endl;
1071 PString token = PString(call_token);
1072 endPoint->SendUserTone(token, tone);
1075 /** Make a call to the remote endpoint.
1077 int h323_make_call(char *host, call_details_t *cd, call_options_t call_options)
1082 if (!h323_end_point_exist()) {
1088 res = endPoint->MakeCall(dest, token, &cd->call_reference, call_options.port, call_options.callerid);
1089 memcpy((char *)(cd->call_token), (const unsigned char *)token, token.GetLength());
1094 int h323_clear_call(const char *call_token)
1096 if (!h323_end_point_exist()) {
1100 endPoint->ClearCall(PString(call_token));
1104 /** This function tells the h.323 stack to either
1105 answer or deny an incoming call */
1106 int h323_answering_call(const char *token, int busy)
1108 const PString currentToken(token);
1109 H323Connection * connection;
1111 connection = endPoint->FindConnectionWithLock(currentToken);
1113 if (connection == NULL) {
1114 cout << "No connection found for " << token << endl;
1119 connection->AnsweringCall(H323Connection::AnswerCallNow);
1120 connection->Unlock();
1123 connection->AnsweringCall(H323Connection::AnswerCallDenied);
1124 connection->Unlock();
1131 int h323_show_codec(int fd, int argc, char *argv[])
1133 cout << "Allowed Codecs:\n\t" << setprecision(2) << endPoint->GetCapabilities() << endl;
1137 int h323_soft_hangup(const char *data)
1139 PString token(data);
1142 result = endPoint->ClearCall(token);
1146 /* alas, this doesn't work :( */
1147 void h323_native_bridge(const char *token, char *them, char *capability)
1149 H323Channel *channel;
1150 MyH323Connection *connection = (MyH323Connection *)endPoint->FindConnectionWithLock(token);
1153 cout << "ERROR: No connection found, this is bad\n";
1157 cout << "Native Bridge: them [" << them << "]" << endl;
1159 channel = connection->FindChannel(connection->sessionId, TRUE);
1160 connection->bridging = TRUE;
1161 connection->CloseLogicalChannelNumber(channel->GetNumber());
1163 connection->Unlock();