AmRtpStream.h

Go to the documentation of this file.
00001 /*
00002  * $Id: AmRtpStream.h 950 2008-05-14 12:54:45Z sayer $
00003  *
00004  * Copyright (C) 2002-2003 Fhg Fokus
00005  *
00006  * This file is part of sems, a free SIP media server.
00007  *
00008  * sems is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * sems is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00028 #ifndef _RtpStream_h_
00029 #define _RtpStream_h_
00030 
00031 
00032 #include "AmSdp.h"
00033 #include "AmThread.h"
00034 #include "SampleArray.h"
00035 #include "AmRtpPacket.h"
00036 #include "AmEvent.h"
00037 
00038 #include <netinet/in.h>
00039 
00040 #include <string>
00041 #include <map>
00042 #include <memory>
00043 using std::string;
00044 using std::auto_ptr;
00045 
00046 // return values of AmRtpStream::receive
00047 #define RTP_EMPTY        0 // no rtp packet available
00048 #define RTP_PARSE_ERROR -1 // error while parsing rtp packet
00049 #define RTP_TIMEOUT     -2 // last received packet is too old
00050 #define RTP_DTMF        -3 // dtmf packet has been received
00051 #define RTP_BUFFER_SIZE -4 // buffer overrun
00052 #define RTP_UNKNOWN_PL  -5 // unknown payload
00053 
00054 
00055 struct amci_payload_t;
00056 
00057 class AmAudio;
00058 class AmSession;
00059 class SdpPayload;
00060 typedef std::map<unsigned int, AmRtpPacket*, ts_less> ReceiveBuffer;
00061 
00062 
00066 struct PacketMem {
00067 #define MAX_PACKETS 32
00068 
00069   AmRtpPacket packets[MAX_PACKETS];
00070   bool        used[MAX_PACKETS];
00071 
00072   PacketMem();
00073 
00074   inline AmRtpPacket* newPacket();
00075   inline void freePacket(AmRtpPacket* p);
00076   inline void clear();
00077 };
00078 
00079 
00085 class AmRtpStream 
00086 {
00087 protected:
00088   static int next_port;
00089   static AmMutex port_mut;
00090 
00091   static int getNextPort();
00092 
00097   int         payload;
00098   unsigned int   sequence;
00099 
00105   int         last_payload;
00106 
00107   string             r_host;
00108   unsigned short     r_port;
00109 #ifdef SUPPORT_IPV6
00110   struct sockaddr_storage r_saddr;
00111   struct sockaddr_storage l_saddr;
00112 #else
00113   struct sockaddr_in r_saddr;
00114   struct sockaddr_in l_saddr;
00115 #endif
00116   unsigned short     l_port;
00117   int                l_sd;
00118 
00119   struct timeval last_recv_time;
00120 
00121   unsigned int   l_ssrc;
00122   unsigned int   r_ssrc;
00123   bool           r_ssrc_i;
00124 
00126   bool              passive;      // passive mode ?
00127 
00129   auto_ptr<const SdpPayload> telephone_event_pt;
00130 
00131 
00132   PacketMem       mem;
00133   ReceiveBuffer   receive_buf;
00134   AmMutex         receive_mut;
00135 
00136 
00137   /* get next packet in buffer */
00138   int nextPacket(AmRtpPacket*& p);
00139   
00140   AmSession*         session;
00141 
00143   void setLocalPort();
00144 
00145 
00146 public:
00147 
00148   AmRtpPacket* newPacket();
00149   void freePacket(AmRtpPacket* p);
00150   
00152   bool mute;
00154   bool hold;
00156   bool begin_talk;
00158   bool monitor_rtp_timeout;
00159 
00160 
00162   bool receiving;
00163 
00164   int send( unsigned int ts,
00165             unsigned char* buffer,
00166             unsigned int   size );
00167 
00168   int send_raw( char* packet, unsigned int length );
00169 
00170   int receive( unsigned char* buffer, unsigned int size,
00171                unsigned int& ts, int& payload);
00172 
00174   int ping();
00175 
00177   AmRtpStream(AmSession* _s=0);
00179   virtual ~AmRtpStream();
00180 
00186   void setLocalIP(const string& ip);
00187             
00192   int getLocalPort();
00193 
00198   int getRPort();
00199     
00204   string getRHost();
00205 
00209   void setRAddr(const string& addr, unsigned short port);
00210 
00212   void setPassiveMode(bool p) { passive = p; }
00213   bool getPassiveMode() { return passive; }
00214 
00215   unsigned int get_ssrc() { return l_ssrc; }
00216 
00221   void setTelephoneEventPT(const SdpPayload *pt) {
00222     telephone_event_pt.reset(pt);
00223   }
00224 
00225   int getTelephoneEventRate();
00226 
00234   virtual void init(const vector<SdpPayload*>& sdp_payloads);
00235 
00239   void pause();
00240 
00244   void resume();
00245 
00247   void setOnHold(bool on_hold);
00248   
00250   bool getOnHold();
00251 
00253   void setMonitorRTPTimeout(bool m) { monitor_rtp_timeout = m; }
00255   bool getMonitorRTPTimeout() { return monitor_rtp_timeout; }
00256 
00260   void icmpError();
00261 
00266   void bufferPacket(AmRtpPacket* p);
00267 
00268   /*
00269    * clear RTP timeout at time recv_time 
00270    */
00271   void clearRTPTimeout(struct timeval* recv_time);
00272 
00273   virtual unsigned int bytes2samples(unsigned int) const = 0;
00274 };
00275 
00277 class AmRtpTimeoutEvent
00278   : public AmEvent
00279 {
00280         
00281 public:
00282   AmRtpTimeoutEvent() 
00283     : AmEvent(0) { }
00284   ~AmRtpTimeoutEvent() { }
00285 };
00286 
00287 #endif
00288 
00289 // Local Variables:
00290 // mode:C++
00291 // End:
00292 

Generated on Fri May 16 12:02:05 2008 for SEMS by  doxygen 1.5.1
Home |  Recent changes |  Search |  Glossary |  Sitemap |  Login