sipcon.h

Go to the documentation of this file.
00001 /*
00002  * sipcon.h
00003  *
00004  * Session Initiation Protocol connection.
00005  *
00006  * Open Phone Abstraction Library (OPAL)
00007  * Formally known as the Open H323 project.
00008  *
00009  * Copyright (c) 2001 Equivalence Pty. 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 Equivalence Pty. Ltd.
00024  *
00025  * Contributor(s): ______________________________________.
00026  *
00027  * $Log: sipcon.h,v $
00028  * Revision 2.41.2.4  2007/05/23 20:52:32  dsandras
00029  * We should release the current session if no ACK is received after
00030  * an INVITE answer for a period of 64*T1. Don't trigger the ACK timer
00031  * when sending an ACK, only when not receiving one.
00032  *
00033  * Revision 2.41.2.3  2007/01/15 22:16:42  dsandras
00034  * Backported patches improving stability from HEAD to Phobos.
00035  *
00036  * Revision 2.41.2.2  2006/03/08 21:55:59  dsandras
00037  * Forgot to commit this file. Sorry.
00038  *
00039  * Revision 2.41.2.1  2006/02/11 13:32:24  csoutheren
00040  * Backported fixed from CVS head
00041  *
00042  * Revision 2.42  2006/02/10 23:44:03  csoutheren
00043  * Applied fix for SetConnection and RFC2833 startup
00044  *
00045  * Revision 2.41  2006/01/02 14:47:28  dsandras
00046  * More code cleanups.
00047  *
00048  * Revision 2.40  2005/11/30 13:35:26  csoutheren
00049  * Changed tags for Doxygen
00050  *
00051  * Revision 2.39  2005/10/22 12:16:05  dsandras
00052  * Moved mutex preventing media streams to be opened before they are completely closed to the SIPConnection class.
00053  *
00054  * Revision 2.38  2005/10/13 19:33:50  dsandras
00055  * Added GetDirection to get the default direction for a media stream. Modified OnSendMediaDescription to call BuildSDP if no reverse streams can be opened.
00056  *
00057  * Revision 2.37  2005/10/04 12:57:18  rjongbloed
00058  * Removed CanOpenSourceMediaStream/CanOpenSinkMediaStream functions and
00059  *   now use overides on OpenSourceMediaStream/OpenSinkMediaStream
00060  *
00061  * Revision 2.36  2005/09/27 16:03:46  dsandras
00062  * Removed SendResponseToInvite and added SendPDU function that will change the transport address right before sending the PDU. This operation is atomic.
00063  *
00064  * Revision 2.35  2005/09/15 17:08:36  dsandras
00065  * Added support for CanOpen[Source|Sink]MediaStream.
00066  *
00067  * Revision 2.34  2005/08/25 18:49:52  dsandras
00068  * Added SIP Video support. Changed API of BuildSDP to allow it to be called
00069  * for both audio and video.
00070  *
00071  * Revision 2.33  2005/07/11 01:52:24  csoutheren
00072  * Extended AnsweringCall to work for SIP as well as H.323
00073  * Fixed problems with external RTP connection in H.323
00074  * Added call to OnClosedMediaStream
00075  *
00076  * Revision 2.32  2005/05/06 07:37:06  csoutheren
00077  * Various changed while working with SIP carrier
00078  *   - remove assumption that authentication realm is a domain name.
00079  *   - stopped rewrite of "To" field when proxy being used
00080  *   - fix Contact field in REGISTER to match actual port used when Symmetric NATin use
00081  *   - lots of formatting changes and cleanups
00082  *
00083  * Revision 2.31  2005/04/28 20:22:53  dsandras
00084  * Applied big sanity patch for SIP thanks to Ted Szoczei <tszoczei@microtronix.ca>.
00085  * Thanks a lot!
00086  *
00087  * Revision 2.30  2005/04/10 20:59:42  dsandras
00088  * Added call hold support (local and remote).
00089  *
00090  * Revision 2.29  2005/04/10 20:58:21  dsandras
00091  * Added function to handle incoming transfer (REFER).
00092  *
00093  * Revision 2.28  2005/04/10 20:57:18  dsandras
00094  * Added support for Blind Transfer (transfering and being transfered)
00095  *
00096  * Revision 2.27  2005/04/10 20:54:35  dsandras
00097  * Added function that returns the "best guess" callback URL of a connection.
00098  *
00099  * Revision 2.26  2005/02/19 22:48:48  dsandras
00100  * Added the possibility to register to several registrars and be able to do authenticated calls to each of them. Added SUBSCRIBE/NOTIFY support for Message Waiting Indications.
00101  *
00102  * Revision 2.25  2005/01/16 11:28:05  csoutheren
00103  * Added GetIdentifier virtual function to OpalConnection, and changed H323
00104  * and SIP descendants to use this function. This allows an application to
00105  * obtain a GUID for any connection regardless of the protocol used
00106  *
00107  * Revision 2.24  2004/12/25 20:43:41  dsandras
00108  * Attach the RFC2833 handlers when we are in connected state to ensure
00109  * OpalMediaPatch exist. Fixes problem for DTMF sending.
00110  *
00111  * Revision 2.23  2004/12/22 18:53:18  dsandras
00112  * Added definition for ForwardCall.
00113  *
00114  * Revision 2.22  2004/08/20 12:13:31  rjongbloed
00115  * Added correct handling of SIP 180 response
00116  *
00117  * Revision 2.21  2004/08/14 07:56:30  rjongbloed
00118  * Major revision to utilise the PSafeCollection classes for the connections and calls.
00119  *
00120  * Revision 2.20  2004/04/26 05:40:38  rjongbloed
00121  * Added RTP statistics callback to SIP
00122  *
00123  * Revision 2.19  2004/03/14 10:09:53  rjongbloed
00124  * Moved transport on SIP top be constructed by endpoint as any transport created on
00125  *   an endpoint can receive data for any connection.
00126  *
00127  * Revision 2.18  2004/03/13 06:30:03  rjongbloed
00128  * Changed parameter in UDP write function to void * from PObject *.
00129  *
00130  * Revision 2.17  2004/02/24 11:33:46  rjongbloed
00131  * Normalised RTP session management across protocols
00132  * Added support for NAT (via STUN)
00133  *
00134  * Revision 2.16  2004/02/07 02:20:32  rjongbloed
00135  * Changed to allow opening of more than just audio streams.
00136  *
00137  * Revision 2.15  2003/12/20 12:21:18  rjongbloed
00138  * Applied more enhancements, thank you very much Ted Szoczei
00139  *
00140  * Revision 2.14  2003/03/17 10:26:59  robertj
00141  * Added video support.
00142  *
00143  * Revision 2.13  2003/03/06 03:57:47  robertj
00144  * IVR support (work in progress) requiring large changes everywhere.
00145  *
00146  * Revision 2.12  2002/09/16 02:52:35  robertj
00147  * Added #define so can select if #pragma interface/implementation is used on
00148  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00149  *
00150  * Revision 2.11  2002/04/16 07:53:15  robertj
00151  * Changes to support calls through proxies.
00152  *
00153  * Revision 2.10  2002/04/10 03:13:45  robertj
00154  * Moved code for handling media bypass address resolution into ancestor as
00155  *   now done ths same way in both SIP and H.323.
00156  * Major changes to RTP session management when initiating an INVITE.
00157  *
00158  * Revision 2.9  2002/04/09 01:02:14  robertj
00159  * Fixed problems with restarting INVITE on  authentication required response.
00160  *
00161  * Revision 2.8  2002/04/08 02:40:13  robertj
00162  * Fixed issues with using double originate call, eg from simple app command line.
00163  *
00164  * Revision 2.7  2002/04/05 10:42:04  robertj
00165  * Major changes to support transactions (UDP timeouts and retries).
00166  *
00167  * Revision 2.6  2002/03/15 10:55:28  robertj
00168  * Added ability to specify proxy username/password in URL.
00169  *
00170  * Revision 2.5  2002/03/08 06:28:19  craigs
00171  * Changed to allow Authorisation to be included in other PDUs
00172  *
00173  * Revision 2.4  2002/02/19 07:52:40  robertj
00174  * Restructured media bypass functions to fix problems with RFC2833.
00175  *
00176  * Revision 2.3  2002/02/13 04:55:59  craigs
00177  * Fixed problem with endless loop if proxy keeps failing authentication with 407
00178  *
00179  * Revision 2.2  2002/02/11 07:34:06  robertj
00180  * Changed SDP to use OpalTransport for hosts instead of IP addresses/ports
00181  * Added media bypass for streams between compatible protocols.
00182  *
00183  * Revision 2.1  2002/02/01 04:53:01  robertj
00184  * Added (very primitive!) SIP support.
00185  *
00186  */
00187 
00188 #ifndef __OPAL_SIPCON_H
00189 #define __OPAL_SIPCON_H
00190 
00191 #ifdef P_USE_PRAGMA
00192 #pragma interface
00193 #endif
00194 
00195 
00196 #include <opal/connection.h>
00197 #include <sip/sippdu.h>
00198 
00199 
00200 class OpalCall;
00201 class SIPEndPoint;
00202 
00203 
00205 
00208 class SIPConnection : public OpalConnection
00209 {
00210   PCLASSINFO(SIPConnection, OpalConnection);
00211   public:
00212 
00217     SIPConnection(
00218       OpalCall & call,            
00219       SIPEndPoint & endpoint,     
00220       const PString & token,      
00221       const SIPURL & address,     
00222       OpalTransport * transport   
00223     );
00224 
00227     ~SIPConnection();
00229 
00238     virtual BOOL SetUpConnection();
00239 
00243     virtual void TransferConnection(
00244       const PString & remoteParty,   
00245       const PString & callIdentity = PString::Empty()
00247     );
00248 
00251     virtual void HoldConnection();
00252 
00256     virtual void RetrieveConnection();
00257 
00260     virtual BOOL IsConnectionOnHold();
00261 
00272     virtual BOOL SetAlerting(
00273       const PString & calleeName,   
00274       BOOL withMedia
00275     );
00276 
00281     virtual BOOL SetConnected();
00282 
00285     virtual OpalMediaFormatList GetMediaFormats() const;
00286     
00289     virtual BOOL OpenSourceMediaStream(
00290       const OpalMediaFormatList & mediaFormats, 
00291       unsigned sessionID                   
00292     );
00293 
00296     virtual OpalMediaStream * OpenSinkMediaStream(
00297       OpalMediaStream & source    
00298     );
00299 
00314     virtual OpalMediaStream * CreateMediaStream(
00315       const OpalMediaFormat & mediaFormat, 
00316       unsigned sessionID,                  
00317       BOOL isSource                        
00318     );
00319 
00320 
00336     virtual OpalConnection::AnswerCallResponse OnAnswerCall(
00337       const PString & callerName      
00338     );
00339 
00350     void AnsweringCall(
00351       AnswerCallResponse response 
00352     );
00353 
00354 
00366     virtual void OnConnected();
00367 
00373     virtual BOOL IsMediaBypassPossible(
00374       unsigned sessionID                  
00375     ) const;
00376 
00394     virtual void OnReleased();
00396 
00401     virtual void OnTransactionFailed(
00402       SIPTransaction & transaction
00403     );
00404 
00407     virtual void OnReceivedPDU(SIP_PDU & pdu);
00408 
00411     virtual void OnReceivedINVITE(SIP_PDU & pdu);
00412 
00415     virtual void OnReceivedACK(SIP_PDU & pdu);
00416   
00419     virtual void OnReceivedOPTIONS(SIP_PDU & pdu);
00420 
00423     virtual void OnReceivedNOTIFY(SIP_PDU & pdu);
00424 
00427     virtual void OnReceivedREFER(SIP_PDU & pdu);
00428   
00431     virtual void OnReceivedBYE(SIP_PDU & pdu);
00432   
00435     virtual void OnReceivedCANCEL(SIP_PDU & pdu);
00436   
00439     virtual void OnReceivedResponse(
00440       SIPTransaction & transaction,
00441       SIP_PDU & response
00442     );
00443 
00446     virtual void OnReceivedTrying(SIP_PDU & pdu);
00447   
00450     virtual void OnReceivedRinging(SIP_PDU & pdu);
00451   
00454     virtual void OnReceivedSessionProgress(SIP_PDU & pdu);
00455   
00458     virtual void OnReceivedAuthenticationRequired(
00459       SIPTransaction & transaction,
00460       SIP_PDU & response
00461     );
00462   
00465     virtual void OnReceivedRedirection(SIP_PDU & pdu);
00466 
00470     virtual void OnReceivedOK(
00471       SIPTransaction & transaction,
00472       SIP_PDU & response
00473     );
00474   
00482     void QueuePDU(
00483       SIP_PDU * pdu
00484     );
00485 
00493     virtual void OnRTPStatistics(
00494       const RTP_Session & session         
00495     ) const;
00497 
00498 
00508     virtual BOOL ForwardCall(
00509       const PString & forwardParty   
00510     );
00511 
00512     virtual BOOL SendACK(SIPTransaction & invite, SIP_PDU & response);
00513 
00517     BOOL SendPDU(SIP_PDU &, const OpalTransportAddress &);
00518 
00519     unsigned GetNextCSeq() { PWaitAndSignal m(transactionsMutex); return ++lastSentCSeq; }
00520 
00521     BOOL BuildSDP(
00522       SDPSessionDescription * &,     
00523       RTP_SessionManager & rtpSessions,
00524       unsigned rtpSessionId
00525     );
00526 
00527     SIPTransaction * GetTransaction (const PString & transactionID) { PWaitAndSignal m(transactionsMutex); return transactions.GetAt(transactionID); }
00528 
00529     void AddTransaction(
00530       SIPTransaction * transaction
00531     ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), transaction); }
00532 
00533     void RemoveTransaction(
00534       SIPTransaction * transaction
00535     ) { PWaitAndSignal m(transactionsMutex); transactions.SetAt(transaction->GetTransactionID(), NULL); }
00536 
00537 
00538     OpalTransportAddress GetLocalAddress(WORD port = 0) const;
00539 
00540     OpalTransport & GetTransport() const { return *transport; }
00541 
00542     PString GetLocalPartyAddress() const { return localPartyAddress; }
00543 
00546     void SetLocalPartyAddress();
00547     void SetLocalPartyAddress(
00548       const PString & addr
00549     ) { localPartyAddress = addr; }
00550 
00555     const PString GetRemotePartyCallbackURL() const;
00556 
00557     PString GetTag() const { return GetIdentifier().AsString(); }
00558     SIPEndPoint & GetEndPoint() const { return endpoint; }
00559     const SIPURL & GetTargetAddress() const { return targetAddress; }
00560     const PStringList & GetRouteSet() const { return routeSet; }
00561     const SIPAuthentication & GetAuthenticator() const { return authentication; }
00562 
00563   protected:
00564     PDECLARE_NOTIFIER(PThread, SIPConnection, HandlePDUsThreadMain);
00565     PDECLARE_NOTIFIER(PThread, SIPConnection, OnAckTimeout);
00566 
00567     virtual RTP_UDP *OnUseRTPSession(
00568       const unsigned rtpSessionId,
00569       const OpalTransportAddress & mediaAddress,
00570       OpalTransportAddress & localAddress
00571     );
00572     virtual void OnReceivedSDP(SIP_PDU & pdu);
00573     virtual BOOL OnReceivedSDPMediaDescription(
00574       SDPSessionDescription & sdp,
00575       SDPMediaDescription::MediaType mediaType,
00576       unsigned sessionId
00577     );
00578     virtual BOOL OnSendSDPMediaDescription(
00579       const SDPSessionDescription & sdpIn,
00580       SDPMediaDescription::MediaType mediaType,
00581       unsigned sessionId,
00582       SDPSessionDescription & sdpOut
00583     );
00584     virtual BOOL OnOpenSourceMediaStreams(
00585       const OpalMediaFormatList & remoteFormatList,
00586       unsigned sessionId,
00587       SDPMediaDescription *localMedia
00588     );
00589     SDPMediaDescription::Direction GetDirection(unsigned sessionId);
00590     static BOOL WriteINVITE(OpalTransport & transport, void * param);
00591 
00592     void InitRFC2833Handler();
00593 
00594     SIPEndPoint         & endpoint;
00595     OpalTransport       * transport;
00596     OpalTransportAddress  lastTransportAddress;
00597 
00598     PMutex                transportMutex;
00599     PMutex                streamsMutex;
00600     BOOL                  local_hold;
00601     BOOL                  remote_hold;
00602     PString               localPartyAddress;
00603     PString               forwardParty;
00604     SIP_PDU             * originalInvite;
00605     SDPSessionDescription remoteSDP;
00606     PStringList           routeSet;
00607     SIPURL                targetAddress;
00608     SIPAuthentication     authentication;
00609 
00610     SIP_PDU_Queue pduQueue;
00611     PSemaphore    pduSemaphore;
00612     PThread     * pduHandler;
00613 
00614     PTimer             ackTimer;
00615     PMutex             transactionsMutex;
00616     SIPTransaction   * referTransaction;
00617     PMutex             invitationsMutex;
00618     SIPTransactionList invitations;
00619     SIPTransactionDict transactions;
00620     unsigned           lastSentCSeq;
00621 
00622     enum {
00623       ReleaseWithBYE,
00624       ReleaseWithCANCEL,
00625       ReleaseWithResponse,
00626       ReleaseWithNothing,
00627     } releaseMethod;
00628 
00629     OpalMediaFormatList remoteFormatList;
00630 };
00631 
00632 
00635 class SIP_RTP_Session : public RTP_UserData
00636 {
00637   PCLASSINFO(SIP_RTP_Session, RTP_UserData);
00638 
00643     SIP_RTP_Session(
00644       const SIPConnection & connection  
00645     );
00647 
00656     virtual void OnTxStatistics(
00657       const RTP_Session & session   
00658     ) const;
00659 
00666     virtual void OnRxStatistics(
00667       const RTP_Session & session   
00668     ) const;
00670 
00671 
00672   protected:
00673     const SIPConnection & connection; 
00674 };
00675 
00676 
00677 #endif // __OPAL_SIPCON_H
00678 
00679 
00680 // End of File ///////////////////////////////////////////////////////////////

Generated on Fri Jan 25 01:20:18 2008 for OPAL by  doxygen 1.5.4