processor.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Inter Asterisk Exchange 2
00004  * 
00005  * The core routine which determines the processing of packets for one call.
00006  * 
00007  * Open Phone Abstraction Library (OPAL)
00008  *
00009  * Copyright (c) 2005 Indranet Technologies Ltd.
00010  *
00011  * The contents of this file are subject to the Mozilla Public License
00012  * Version 1.0 (the "License"); you may not use this file except in
00013  * compliance with the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS"
00017  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00018  * the License for the specific language governing rights and limitations
00019  * under the License.
00020  *
00021  * The Original Code is Open Phone Abstraction Library.
00022  *
00023  * The Initial Developer of the Original Code is Indranet Technologies Ltd.
00024  *
00025  * The author of this code is Derek J Smithies
00026  *
00027  *
00028  *  $Log: processor.h,v $
00029  *  Revision 1.7  2005/09/05 01:19:43  dereksmithies
00030  *  add patches from Adrian Sietsma to avoid multiple hangup packets at call end,
00031  *  and stop the sending of ping/lagrq packets at call end. Many thanks.
00032  *
00033  *  Revision 1.6  2005/08/26 03:07:38  dereksmithies
00034  *  Change naming convention, so all class names contain the string "IAX2"
00035  *
00036  *  Revision 1.5  2005/08/25 03:26:06  dereksmithies
00037  *  Add patch from Adrian Sietsma to correctly set the packet timestamps under windows.
00038  *  Many thanks.
00039  *
00040  *  Revision 1.4  2005/08/24 04:56:25  dereksmithies
00041  *  Add code from Adrian Sietsma to send FullFrameTexts and FullFrameDtmfs to
00042  *  the remote end.  Many Thanks.
00043  *
00044  *  Revision 1.3  2005/08/24 01:38:38  dereksmithies
00045  *  Add encryption, iax2 style. Numerous tidy ups. Use the label iax2, not iax
00046  *
00047  *  Revision 1.2  2005/08/04 08:14:17  rjongbloed
00048  *  Fixed Windows build under DevStudio 2003 of IAX2 code
00049  *
00050  *  Revision 1.1  2005/07/30 07:01:32  csoutheren
00051  *  Added implementation of IAX2 (Inter Asterisk Exchange 2) protocol
00052  *  Thanks to Derek Smithies of Indranet Technologies Ltd. for
00053  *  writing and contributing this code
00054  *
00055  *
00056  *
00057  *
00058  *
00059  *
00060  *
00061  */
00062 
00063 #ifndef PROCESSOR_H
00064 #define PROCESSOR_H
00065 
00066 #include <ptlib.h>
00067 #include <opal/connection.h>
00068 
00069 #include <iax2/frame.h>
00070 #include <iax2/iedata.h>
00071 #include <iax2/remote.h>
00072 #include <iax2/safestrings.h>
00073 #include <iax2/sound.h>
00074 
00075 class IAX2EndPoint;
00076 class IAX2Connection;
00077 
00078 
00079 
00080 
00082 
00099 class IAX2WaitingForAck : public PObject
00100 {
00101   PCLASSINFO(IAX2WaitingForAck, PObject);
00102 
00103  public:
00105   enum ResponseToAck {
00106     RingingAcked   = 0,  
00107     AcceptAcked    = 1,  
00108     AuthRepAcked   = 2,  
00109     AnswerAcked    = 3   
00110   };
00111   
00113   IAX2WaitingForAck();
00114   
00116   void Assign(IAX2FullFrame *f, ResponseToAck _response);
00117   
00119   BOOL MatchingAckPacket(IAX2FullFrame *f);
00120   
00122   ResponseToAck GetResponse() { return response; }
00123   
00125   PString GetResponseAsString() const;
00126   
00128   virtual void PrintOn(ostream & strm) const;
00129   
00131   void ZeroValues();
00132   
00133  private:
00135   DWORD timeStamp;
00136   
00138   PINDEX seqNo;
00139   
00141   ResponseToAck response;
00142 };
00143 
00145 
00149 class IAX2Processor : public PThread
00150 {
00151   PCLASSINFO(IAX2Processor, PThread);
00152   
00153  public:
00154   
00156   IAX2Processor(IAX2EndPoint & ep);
00157 
00159   ~IAX2Processor(); 
00160 
00162   void AssignConnection(IAX2Connection * _con);
00163  
00167   virtual void Main();
00168   
00170   void Terminate();
00171   
00174   void Activate();
00175 
00179   void PutSoundPacketToNetwork(PBYTEArray *sund);
00180 
00182   IAX2Remote & GetRemoteInfo() { return remote; }
00183   
00185   IAX2SequenceNumbers & GetSequenceInfo() { return sequence; }
00186   
00188   IAX2Encryption & GetEncryptionInfo() { return encryption; }
00189 
00191   const PTimeInterval & GetCallStartTick() { return callStartTick; }
00192 
00194   virtual void Release(OpalConnection::CallEndReason releaseReason = OpalConnection::EndedByLocalUser);
00195 
00197   void ClearCall(OpalConnection::CallEndReason releaseReason = OpalConnection::EndedByLocalUser);
00198 
00203   virtual void OnReleased();
00204   
00207   void SendDtmf(PString  dtmfs);
00208 
00211   void SendText(const PString & text);
00212 
00219   virtual BOOL SetUpConnection();
00220 
00222   void IncomingEthernetFrame (IAX2Frame *frame);
00223 
00230   IAX2Frame *GetSoundPacketFromNetwork();
00231 
00232 
00235   void SetSpecialPackets(BOOL newValue) { specialPackets = newValue; }
00236 
00239   BOOL IsStatusQueryEthernetFrame(IAX2Frame *frame);
00240 
00242   BOOL Matches(IAX2Frame *frame) { return remote == (frame->GetRemoteInfo()); }
00243   
00244   
00247   virtual void PrintOn(ostream & strm) const;
00248   
00251   void ReportStatistics();
00252   
00253 
00255   BOOL MatchingLocalCallNumber(PINDEX compare) { return (compare == remote.SourceCallNumber()); }
00256   
00257   
00259   unsigned short GetSelectedCodec() { return (unsigned short) selectedCodec; }
00260   
00262   void SetConnected(); 
00263   
00266   void AcceptIncomingCall();
00267 
00278   virtual BOOL SetAlerting(
00279                            const PString & calleeName,   
00280                            BOOL withMedia                
00281                            ) ;
00282   
00292   void SetCallToken(PString newToken);
00293 
00296   void SetEstablished(BOOL originator        
00297                       );
00298 
00300   PString GetCallToken();
00301 
00303   IAX2EndPoint & GetEndPoint() { return endpoint; }
00304 
00308   void Hangup(PString messageToSend);
00309 
00312   BOOL IsCallTerminating() { return callStatus & callTerminating; }
00313   
00314  protected:
00315   
00317   IAX2EndPoint      & endpoint;
00318   
00320   IAX2Connection * con;
00321 
00326   BOOL RemoteSelectedCodecOk();
00327  
00330   void CheckForHangupMessages();
00331  
00333   void SendAckFrame(IAX2FullFrame *inReplyTo);
00334   
00337   void TransmitFrameToRemoteEndpoint(IAX2Frame *src);
00338 
00344   void TransmitFrameToRemoteEndpoint(IAX2FullFrame *src,
00345                                      IAX2WaitingForAck::ResponseToAck response  
00346                                      );
00347 
00351   void TransmitFrameNow(IAX2Frame *src);
00352   
00354   void TransmitFrameToRemoteEndpoint(IAX2FullFrameProtocol *src);
00355   
00357   void ProcessNetworkFrame(IAX2Frame * src);
00358   
00360   void ProcessNetworkFrame(IAX2MiniFrame * src);
00361   
00363   void ProcessNetworkFrame(IAX2FullFrame * src);
00364   
00366   void ProcessNetworkFrame(IAX2FullFrameDtmf * src);
00367   
00369   void ProcessNetworkFrame(IAX2FullFrameVoice * src);
00370   
00372   void ProcessNetworkFrame(IAX2FullFrameVideo * src);
00373   
00375   void ProcessNetworkFrame(IAX2FullFrameSessionControl * src);
00376   
00378   void ProcessNetworkFrame(IAX2FullFrameNull * src);
00379   
00384   void ProcessNetworkFrame(IAX2FullFrameProtocol * src);
00385   
00387   void ProcessNetworkFrame(IAX2FullFrameText * src);
00388   
00390   void ProcessNetworkFrame(IAX2FullFrameImage * src);
00391   
00393   void ProcessNetworkFrame(IAX2FullFrameHtml * src);
00394   
00396   void ProcessNetworkFrame(IAX2FullFrameCng * src);
00397     
00399   void ProcessLists();
00400 
00404   BOOL ProcessOneIncomingEthernetFrame();
00405   
00407   void ConnectToRemoteNode(PString & destination);
00408   
00410   void SendDtmfMessage(char message);
00411   
00413   void SendTextMessage(PString & message);
00414 
00417   void SendSoundMessage(PBYTEArray *sound);
00418   
00420   void IncControlFramesSent() { ++controlFramesSent; }
00421   
00423   void IncControlFramesRcvd() { ++controlFramesRcvd; }
00424   
00426   void IncAudioFramesSent()   { ++audioFramesSent; }
00427   
00429   void IncAudioFramesRcvd()   { ++audioFramesRcvd; }
00430   
00432   void IncVideoFramesSent()   { ++videoFramesSent; }
00433   
00435   void IncVideoFramesRcvd()   { ++videoFramesRcvd; }
00436   
00439   void RemoteNodeHasAnswered();
00440   
00444   void CallStopSounds();
00445   
00448   void ReceivedHookFlash();
00449   
00452   void RemoteNodeIsBusy();
00453   
00455   void ProcessIncomingAudioFrame(IAX2Frame *newFrame);
00456   
00458   void ProcessIncomingVideoFrame(IAX2Frame *newFrame);
00459   
00461   void ProcessIaxCmdNew(IAX2FullFrameProtocol *src);
00462   
00464   void ProcessIaxCmdPing(IAX2FullFrameProtocol *src);
00465   
00467   void ProcessIaxCmdPong(IAX2FullFrameProtocol *src);
00468   
00470   void ProcessIaxCmdAck(IAX2FullFrameProtocol *src);
00471   
00473   void ProcessIaxCmdHangup(IAX2FullFrameProtocol *src);
00474   
00476   void ProcessIaxCmdReject(IAX2FullFrameProtocol *src);
00477   
00479   void ProcessIaxCmdAccept(IAX2FullFrameProtocol *src);
00480   
00482   void ProcessIaxCmdAuthReq(IAX2FullFrameProtocol *src);
00483   
00485   void ProcessIaxCmdAuthRep(IAX2FullFrameProtocol *src);
00486   
00488   void ProcessIaxCmdInval(IAX2FullFrameProtocol *src);
00489   
00491   void ProcessIaxCmdLagRq(IAX2FullFrameProtocol *src);
00492   
00494   void ProcessIaxCmdLagRp(IAX2FullFrameProtocol *src);
00495   
00497   void ProcessIaxCmdRegReq(IAX2FullFrameProtocol *src);
00498   
00500   void ProcessIaxCmdRegAuth(IAX2FullFrameProtocol *src);
00501   
00503   void ProcessIaxCmdRegAck(IAX2FullFrameProtocol *src);
00504   
00506   void ProcessIaxCmdRegRej(IAX2FullFrameProtocol *src);
00507   
00509   void ProcessIaxCmdRegRel(IAX2FullFrameProtocol *src);
00510   
00512   void ProcessIaxCmdVnak(IAX2FullFrameProtocol *src);
00513   
00515   void ProcessIaxCmdDpReq(IAX2FullFrameProtocol *src);
00516   
00518   void ProcessIaxCmdDpRep(IAX2FullFrameProtocol *src);
00519   
00521   void ProcessIaxCmdDial(IAX2FullFrameProtocol *src);
00522   
00524   void ProcessIaxCmdTxreq(IAX2FullFrameProtocol *src);
00525   
00527   void ProcessIaxCmdTxcnt(IAX2FullFrameProtocol *src);
00528   
00530   void ProcessIaxCmdTxacc(IAX2FullFrameProtocol *src);
00531   
00533   void ProcessIaxCmdTxready(IAX2FullFrameProtocol *src);
00534   
00536   void ProcessIaxCmdTxrel(IAX2FullFrameProtocol *src);
00537   
00539   void ProcessIaxCmdTxrej(IAX2FullFrameProtocol *src);
00540   
00542   void ProcessIaxCmdQuelch(IAX2FullFrameProtocol *src);
00543   
00545   void ProcessIaxCmdUnquelch(IAX2FullFrameProtocol *src);
00546   
00548   void ProcessIaxCmdPoke(IAX2FullFrameProtocol *src);
00549   
00551   void ProcessIaxCmdPage(IAX2FullFrameProtocol *src);
00552   
00554   void ProcessIaxCmdMwi(IAX2FullFrameProtocol *src);
00555   
00557   void ProcessIaxCmdUnsupport(IAX2FullFrameProtocol *src);
00558   
00560   void ProcessIaxCmdTransfer(IAX2FullFrameProtocol *src);
00561   
00563   void ProcessIaxCmdProvision(IAX2FullFrameProtocol *src);
00564   
00566   void ProcessIaxCmdFwDownl(IAX2FullFrameProtocol *src);
00567   
00569   void ProcessIaxCmdFwData(IAX2FullFrameProtocol *src);
00570   
00573   BOOL Authenticate(IAX2FullFrameProtocol *reply 
00574                     );
00575     
00579   PTimeInterval callStartTick;
00580   
00582   IAX2Remote remote;
00583   
00585   IAX2SequenceNumbers sequence;
00586   
00588   PAtomicInteger controlFramesSent;
00589   
00591   PAtomicInteger controlFramesRcvd;
00592   
00594   PAtomicInteger audioFramesSent;
00595   
00597   PAtomicInteger audioFramesRcvd;
00598   
00600   PAtomicInteger videoFramesSent;
00601   
00603   PAtomicInteger videoFramesRcvd;
00604   
00606   SafeString remotePhoneNumber;
00607   
00609   SafeStrings callList;
00610   
00613   SafeString dtmfText;
00614 
00616   SafeStrings textList;
00617 
00619   SafeStrings dtmfNetworkList;
00620 
00622   SafeStrings hangList;
00623       
00626   IAX2SoundList   soundWaitingForTransmission;
00627   
00630   IAX2FrameList   soundReadFromEthernet;
00631   
00633   IAX2FrameList frameList;
00634   
00637   PINDEX lastFullFrameTimeStamp;
00638     
00640   BOOL audioCanFlow;
00641 
00643   IAX2IeData ieData;
00644   
00646   unsigned int selectedCodec;
00647   
00649   enum CallStatus {
00650     callNewed      =  1 << 0,   
00651     callSentRinging = 1 << 1,   
00652     callRegistered =  1 << 2,   
00653     callAuthorised =  1 << 3,   
00654     callAccepted   =  1 << 4,   
00655     callRinging    =  1 << 5,   
00656     callAnswered   =  1 << 6,   
00657     callTerminating = 1 << 7    
00658   };
00659   
00661   enum DefinedNoResponseTimePeriod {
00662     NoResponseTimePeriod = 5000 
00663   };
00664   
00666   unsigned short callStatus;
00667   
00669   void SetCallSentRinging(BOOL newValue = TRUE) 
00670     { if (newValue) callStatus |= callSentRinging; else callStatus &= ~callSentRinging; }
00671   
00673   void SetCallNewed(BOOL newValue = TRUE) 
00674     { if (newValue) callStatus |= callNewed; else callStatus &= ~callNewed; }
00675   
00677   void SetCallRegistered(BOOL newValue = TRUE) 
00678     { if (newValue) callStatus |= callRegistered; else callStatus &= ~callRegistered; }
00679   
00681   void SetCallAuthorised(BOOL newValue = TRUE) 
00682     { if (newValue) callStatus |= callAuthorised; else callStatus &= ~callAuthorised; }
00683   
00685   void SetCallAccepted(BOOL newValue = TRUE) 
00686     { if (newValue) callStatus |= callAccepted; else callStatus &= ~callAccepted; }
00687   
00689   void SetCallRinging(BOOL newValue = TRUE) 
00690     { if (newValue) callStatus |= callRinging; else callStatus &= ~callRinging; }
00691   
00693   void SetCallAnswered(BOOL newValue = TRUE) 
00694     { if (newValue) callStatus |= callAnswered; else callStatus &= ~callAnswered; }
00695 
00697   void SetCallTerminating(BOOL newValue = TRUE) 
00698     { if (newValue) callStatus |= callTerminating; else callStatus &= ~callTerminating; }
00699   
00701   BOOL IsCallHappening() { return callStatus > 0; }
00702   
00704   BOOL IsCallNewed() { return callStatus & callNewed; }
00705   
00707   BOOL IsCallSentRinging() { return callStatus & callSentRinging; }
00708   
00710   BOOL IsCallRegistered() { return callStatus & callRegistered; }
00711   
00713   BOOL IsCallAuthorised() { return callStatus & callAuthorised; }
00714   
00716   BOOL IsCallAccepted() { return callStatus & callAccepted; }
00717   
00719   BOOL IsCallRinging() { return callStatus & callRinging; }
00720   
00722   BOOL IsCallAnswered() { return callStatus & callAnswered; }
00723 
00724   
00726   void SendAnswerMessageToRemoteNode();
00727 
00729   PTimer noResponseTimer;
00730   
00731 #ifdef DOC_PLUS_PLUS
00732 
00739   void OnNoResponseTimeout(PTimer &, INT);
00740 #else
00741   PDECLARE_NOTIFIER(PTimer, IAX2Processor, OnNoResponseTimeout);
00742 #endif
00743    
00746   void StartNoResponseTimer(PINDEX msToWait = 60000);
00747    
00749   void StopNoResponseTimer() { noResponseTimer.Stop(); }
00750    
00753   void StartStatusCheckTimer(PINDEX msToWait = 10000 );  
00754 
00755 #ifdef DOC_PLUS_PLUS
00756 
00762   void OnStatusCheck(PTimer &, INT);
00763 #else
00764   PDECLARE_NOTIFIER(PTimer, IAX2Processor, OnStatusCheck);
00765 #endif
00766   
00768   void DoStatusCheck();
00769 
00771   void CleanPendingLists() { activate.Signal(); }
00772   
00775   void RemoteNodeIsRinging();
00776 
00780   void RingingWasAcked();
00781 
00786   void AnswerWasAcked();
00787 
00790   IAX2WaitingForAck nextTask;
00791   
00793   PSyncPoint activate;    
00794   
00796   BOOL       endThread;         
00797 
00801   BOOL      IsHandlingSpecialPackets() { return specialPackets; };
00802 
00805   BOOL       specialPackets;
00806 
00808   SafeString callToken;
00809 
00813   BOOL firstMediaFrame;
00814 
00817   BOOL answerCallNow;
00818 
00823   BOOL statusCheckOtherEnd;
00824 
00826   PTimer statusCheckTimer;
00827 
00829   PINDEX lastSentAudioFrameTime;
00830 
00833   PINDEX audioFrameDuration;
00834 
00836   PINDEX audioCompressedBytes;
00837 
00841   BOOL audioFramesNotStarted;
00842 
00845   void CheckForRemoteCapabilities(IAX2FullFrameProtocol *src);
00846 
00848   IAX2Encryption encryption;
00849 };
00850 
00851 
00852 #endif // PROCESSOR_H

Generated on Mon Sep 25 16:20:10 2006 for OPAL by  doxygen 1.4.7