Yate
|
00001 /* 00002 * yatertp.h 00003 * Yet Another RTP Stack 00004 * This file is part of the YATE Project http://YATE.null.ro 00005 * 00006 * Yet Another Telephony Engine - a fully featured software PBX and IVR 00007 * Copyright (C) 2004-2006 Null Team 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #ifndef __YATERTP_H 00025 #define __YATERTP_H 00026 00027 #include <yateclass.h> 00028 00029 #ifdef _WINDOWS 00030 00031 #ifdef LIBYRTP_EXPORTS 00032 #define YRTP_API __declspec(dllexport) 00033 #else 00034 #ifndef LIBYRTP_STATIC 00035 #define YRTP_API __declspec(dllimport) 00036 #endif 00037 #endif 00038 00039 #endif /* _WINDOWS */ 00040 00041 #ifndef YRTP_API 00042 #define YRTP_API 00043 #endif 00044 00048 namespace TelEngine { 00049 00050 class RTPGroup; 00051 class RTPTransport; 00052 class RTPSession; 00053 class RTPSender; 00054 class RTPReceiver; 00055 class RTPSecure; 00056 00061 class YRTP_API RTPProcessor : public GenObject 00062 { 00063 friend class UDPSession; 00064 friend class UDPTLSession; 00065 friend class RTPGroup; 00066 friend class RTPTransport; 00067 friend class RTPSender; 00068 friend class RTPReceiver; 00069 00070 public: 00074 RTPProcessor(); 00075 00079 virtual ~RTPProcessor(); 00080 00085 inline RTPGroup* group() const 00086 { return m_group; } 00087 00093 virtual void rtpData(const void* data, int len); 00094 00100 virtual void rtcpData(const void* data, int len); 00101 00106 virtual void getStats(String& stats) const; 00107 00111 virtual inline void incWrongSrc() 00112 { } 00113 00118 inline unsigned int wrongSrc() 00119 { return m_wrongSrc; } 00120 00121 protected: 00126 void group(RTPGroup* newgrp); 00127 00132 virtual void timerTick(const Time& when) = 0; 00133 00134 unsigned int m_wrongSrc; 00135 00136 private: 00137 RTPGroup* m_group; 00138 }; 00139 00145 class YRTP_API RTPGroup : public GenObject, public Mutex, public Thread 00146 { 00147 friend class RTPProcessor; 00148 00149 public: 00155 RTPGroup(int msec = 0, Priority prio = Normal); 00156 00160 virtual ~RTPGroup(); 00161 00165 virtual void cleanup(); 00166 00170 virtual void run(); 00171 00176 static void setMinSleep(int msec); 00177 00182 void join(RTPProcessor* proc); 00183 00188 void part(RTPProcessor* proc); 00189 00190 private: 00191 ObjList m_processors; 00192 bool m_listChanged; 00193 unsigned long m_sleep; 00194 }; 00195 00200 class YRTP_API RTPTransport : public RTPProcessor 00201 { 00202 public: 00206 enum Activation { 00207 Inactive, 00208 Bound, 00209 Active 00210 }; 00211 00215 enum Type { 00216 Unknown, 00217 RTP, 00218 UDPTL 00219 }; 00220 00225 RTPTransport(Type type = RTP); 00226 00230 virtual ~RTPTransport(); 00231 00236 void setProcessor(RTPProcessor* processor = 0); 00237 00242 void setMonitor(RTPProcessor* monitor = 0); 00243 00248 inline const SocketAddr& localAddr() const 00249 { return m_localAddr; } 00250 00255 inline const SocketAddr& remoteAddr() const 00256 { return m_remoteAddr; } 00257 00264 bool localAddr(SocketAddr& addr, bool rtcp = true); 00265 00272 bool remoteAddr(SocketAddr& addr, bool sniff = false); 00273 00279 inline bool setTOS(int tos) 00280 { return m_rtpSock.setTOS(tos); } 00281 00286 inline Socket* rtpSock() 00287 { return &m_rtpSock; } 00288 00293 inline Socket* rtcpSock() 00294 { return &m_rtcpSock; } 00295 00300 bool drillHole(); 00301 00302 protected: 00307 virtual void timerTick(const Time& when); 00308 00314 virtual void rtpData(const void* data, int len); 00315 00321 virtual void rtcpData(const void* data, int len); 00322 00323 private: 00324 Type m_type; 00325 RTPProcessor* m_processor; 00326 RTPProcessor* m_monitor; 00327 Socket m_rtpSock; 00328 Socket m_rtcpSock; 00329 SocketAddr m_localAddr; 00330 SocketAddr m_remoteAddr; 00331 SocketAddr m_remoteRTCP; 00332 SocketAddr m_remotePref; 00333 SocketAddr m_rxAddrRTP; 00334 SocketAddr m_rxAddrRTCP; 00335 bool m_autoRemote; 00336 }; 00337 00344 class YRTP_API RTPDejitter : public RTPProcessor 00345 { 00346 public: 00353 RTPDejitter(RTPReceiver* receiver, unsigned int mindelay, unsigned int maxdelay); 00354 00358 virtual ~RTPDejitter(); 00359 00369 virtual bool rtpRecv(bool marker, int payload, unsigned int timestamp, 00370 const void* data, int len); 00371 00375 void clear(); 00376 00377 protected: 00382 virtual void timerTick(const Time& when); 00383 00384 private: 00385 ObjList m_packets; 00386 RTPReceiver* m_receiver; 00387 unsigned int m_minDelay; 00388 unsigned int m_maxDelay; 00389 unsigned int m_headStamp; 00390 unsigned int m_tailStamp; 00391 u_int64_t m_headTime; 00392 u_int64_t m_sampRate; 00393 unsigned char m_fastRate; 00394 }; 00395 00400 class YRTP_API RTPBaseIO 00401 { 00402 friend class RTPSession; 00403 friend class RTPSecure; 00404 public: 00408 inline RTPBaseIO(RTPSession* session = 0) 00409 : m_session(session), m_secure(0), 00410 m_ssrcInit(true), m_ssrc(0), m_ts(0), 00411 m_seq(0), m_rollover(0), m_secLen(0), m_mkiLen(0), 00412 m_evTs(0), m_evNum(-1), m_evVol(-1), 00413 m_ioPackets(), m_ioOctets(0), m_tsLast(0), 00414 m_dataType(-1), m_eventType(-1), m_silenceType(-1) 00415 { } 00416 00420 virtual ~RTPBaseIO(); 00421 00426 inline int dataPayload() const 00427 { return m_dataType; } 00428 00434 bool dataPayload(int type); 00435 00440 inline int eventPayload() const 00441 { return m_eventType; } 00442 00448 bool eventPayload(int type); 00449 00454 inline int silencePayload() const 00455 { return m_silenceType; } 00456 00463 bool silencePayload(int type); 00464 00469 unsigned int ssrcInit(); 00470 00474 inline void reset() 00475 { m_ssrcInit = true; } 00476 00481 inline unsigned int ssrc() const 00482 { return m_ssrcInit ? 0 : m_ssrc; } 00483 00487 inline void ssrc(unsigned int src) 00488 { m_ssrc = src; m_ssrcInit = false; } 00489 00494 inline u_int16_t seq() const 00495 { return m_seq; } 00496 00501 inline u_int32_t rollover() const 00502 { return m_rollover; } 00503 00508 inline u_int64_t fullSeq() const 00509 { return m_seq | (((u_int64_t)m_rollover) << 16); } 00510 00515 inline u_int32_t ioPackets() const 00516 { return m_ioPackets; } 00517 00522 inline u_int32_t ioOctets() const 00523 { return m_ioOctets; } 00524 00529 inline unsigned int tsLast() const 00530 { return m_ts + m_tsLast; } 00531 00536 inline RTPSession* session() const 00537 { return m_session; } 00538 00543 inline RTPSecure* security() const 00544 { return m_secure; } 00545 00550 void security(RTPSecure* secure); 00551 00552 protected: 00557 virtual void timerTick(const Time& when) = 0; 00558 00564 inline void secLength(u_int32_t len, u_int32_t key = 0) 00565 { m_secLen = len; m_mkiLen = key; } 00566 00567 RTPSession* m_session; 00568 RTPSecure* m_secure; 00569 bool m_ssrcInit; 00570 u_int32_t m_ssrc; 00571 u_int32_t m_ts; 00572 u_int16_t m_seq; 00573 u_int32_t m_rollover; 00574 u_int16_t m_secLen; 00575 u_int16_t m_mkiLen; 00576 u_int32_t m_evTs; 00577 int m_evNum; 00578 int m_evVol; 00579 u_int32_t m_ioPackets; 00580 u_int32_t m_ioOctets; 00581 unsigned int m_tsLast; 00582 00583 private: 00584 int m_dataType; 00585 int m_eventType; 00586 int m_silenceType; 00587 }; 00588 00593 class YRTP_API RTPReceiver : public RTPBaseIO 00594 { 00595 friend class RTPSession; 00596 friend class RTPDejitter; 00597 public: 00601 inline RTPReceiver(RTPSession* session = 0) 00602 : RTPBaseIO(session), 00603 m_ioLostPkt(0), m_dejitter(0), 00604 m_seqSync(0), m_seqCount(0), m_warn(true), 00605 m_seqLost(0), m_wrongSSRC(0), m_syncLost(0) 00606 { } 00607 00611 virtual ~RTPReceiver(); 00612 00617 inline u_int32_t ioPacketsLost() const 00618 { return m_ioLostPkt; } 00619 00620 00625 void setDejitter(RTPDejitter* dejitter); 00626 00632 inline void setDejitter(unsigned int mindelay, unsigned int maxdelay) 00633 { setDejitter(new RTPDejitter(this,mindelay,maxdelay)); } 00634 00645 virtual bool rtpRecv(bool marker, int payload, unsigned int timestamp, 00646 const void* data, int len); 00647 00656 virtual bool rtpRecvData(bool marker, unsigned int timestamp, 00657 const void* data, int len); 00658 00668 virtual bool rtpRecvEvent(int event, char key, int duration, 00669 int volume, unsigned int timestamp); 00670 00678 virtual void rtpNewPayload(int payload, unsigned int timestamp); 00679 00687 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker); 00688 00693 virtual void stats(NamedList& stat) const; 00694 00695 protected: 00700 virtual void timerTick(const Time& when); 00701 00712 virtual bool rtpDecipher(unsigned char* data, int len, const void* secData, u_int32_t ssrc, u_int64_t seq); 00713 00724 virtual bool rtpCheckIntegrity(const unsigned char* data, int len, const void* authData, u_int32_t ssrc, u_int64_t seq); 00725 00726 u_int32_t m_ioLostPkt; 00727 00728 private: 00729 void rtpData(const void* data, int len); 00730 void rtcpData(const void* data, int len); 00731 bool decodeEvent(bool marker, unsigned int timestamp, const void* data, int len); 00732 bool decodeSilence(bool marker, unsigned int timestamp, const void* data, int len); 00733 void finishEvent(unsigned int timestamp); 00734 bool pushEvent(int event, int duration, int volume, unsigned int timestamp); 00735 RTPDejitter* m_dejitter; 00736 u_int16_t m_seqSync; 00737 u_int16_t m_seqCount; 00738 bool m_warn; 00739 unsigned int m_seqLost; 00740 unsigned int m_wrongSSRC; 00741 unsigned int m_syncLost; 00742 }; 00743 00748 class YRTP_API RTPSender : public RTPBaseIO 00749 { 00750 public: 00756 RTPSender(RTPSession* session = 0, bool randomTs = true); 00757 00761 virtual ~RTPSender() 00762 { } 00763 00773 bool rtpSend(bool marker, int payload, unsigned int timestamp, 00774 const void* data, int len); 00775 00784 bool rtpSendData(bool marker, unsigned int timestamp, 00785 const void* data, int len); 00786 00795 bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0); 00796 00805 bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0); 00806 00807 00812 inline int padding() const 00813 { return m_padding; } 00814 00820 bool padding(int chunk); 00821 00826 virtual void stats(NamedList& stat) const; 00827 00828 protected: 00833 virtual void timerTick(const Time& when); 00834 00841 virtual void rtpEncipher(unsigned char* data, int len); 00842 00850 virtual void rtpAddIntegrity(const unsigned char* data, int len, unsigned char* authData); 00851 00852 00853 private: 00854 int m_evTime; 00855 unsigned char m_padding; 00856 DataBlock m_buffer; 00857 bool sendEventData(unsigned int timestamp); 00858 }; 00859 00864 class YRTP_API UDPSession : public RTPProcessor 00865 { 00866 public: 00870 virtual ~UDPSession(); 00871 00877 virtual RTPTransport* createTransport(); 00878 00883 bool initTransport(); 00884 00891 bool initGroup(int msec = 0, Thread::Priority prio = Thread::Normal); 00892 00899 inline bool remoteAddr(SocketAddr& addr, bool sniff = false) 00900 { return m_transport && m_transport->remoteAddr(addr,sniff); } 00901 00907 inline bool setTOS(int tos) 00908 { return m_transport && m_transport->setTOS(tos); } 00909 00914 inline Socket* rtpSock() 00915 { return m_transport ? m_transport->rtpSock() : 0; } 00916 00921 inline bool drillHole() 00922 { return m_transport && m_transport->drillHole(); } 00923 00928 void setTimeout(int interval); 00929 00934 inline RTPTransport* transport() const 00935 { return m_transport; } 00936 00941 virtual void transport(RTPTransport* trans); 00942 00943 protected: 00947 UDPSession(); 00948 00953 virtual void timeout(bool initial); 00954 00955 RTPTransport* m_transport; 00956 u_int64_t m_timeoutTime; 00957 u_int64_t m_timeoutInterval; 00958 }; 00959 00964 class YRTP_API RTPSession : public UDPSession, public Mutex 00965 { 00966 public: 00970 enum Direction { 00971 FullStop = 0, 00972 RecvOnly = 1, 00973 SendOnly = 2, 00974 SendRecv = 3 00975 }; 00976 00980 RTPSession(); 00981 00985 virtual ~RTPSession(); 00986 00991 virtual void getStats(String& stats) const; 00992 00998 virtual void rtpData(const void* data, int len); 00999 01005 virtual void rtcpData(const void* data, int len); 01006 01015 virtual bool rtpRecvData(bool marker, unsigned int timestamp, 01016 const void* data, int len); 01017 01027 virtual bool rtpRecvEvent(int event, char key, int duration, 01028 int volume, unsigned int timestamp); 01029 01037 virtual void rtpNewPayload(int payload, unsigned int timestamp); 01038 01046 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker); 01047 01053 virtual RTPSender* createSender(); 01054 01060 virtual RTPReceiver* createReceiver(); 01061 01068 virtual Cipher* createCipher(const String& name, Cipher::Direction dir); 01069 01075 virtual bool checkCipher(const String& name); 01076 01086 inline bool rtpSend(bool marker, int payload, unsigned int timestamp, 01087 const void* data, int len) 01088 { Lock lck(this); return m_send && m_send->rtpSend(marker,payload,timestamp,data,len); } 01089 01098 inline bool rtpSendData(bool marker, unsigned int timestamp, 01099 const void* data, int len) 01100 { Lock lck(this); return m_send && m_send->rtpSendData(marker,timestamp,data,len); } 01101 01110 inline bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0) 01111 { Lock lck(this); return m_send && m_send->rtpSendEvent(event,duration,volume,timestamp); } 01112 01121 inline bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0) 01122 { Lock lck(this); return m_send && m_send->rtpSendKey(key,duration,volume,timestamp); } 01123 01128 inline u_int32_t ioPacketsLost() const 01129 { return m_recv ? m_recv->ioPacketsLost() : 0; } 01130 01135 inline int padding() const 01136 { return m_send ? m_send->padding() : 0; } 01137 01143 inline bool padding(int chunk) 01144 { return m_send && m_send->padding(chunk); } 01145 01151 inline void setDejitter(unsigned int mindelay = 20, unsigned int maxdelay = 50) 01152 { if (m_recv) m_recv->setDejitter(mindelay,maxdelay); } 01153 01158 virtual void transport(RTPTransport* trans); 01159 01164 inline RTPSender* sender() const 01165 { return m_send; } 01166 01171 void sender(RTPSender* send); 01172 01177 inline RTPReceiver* receiver() const 01178 { return m_recv; } 01179 01184 void receiver(RTPReceiver* recv); 01185 01190 inline Direction direction() const 01191 { return m_direction; } 01192 01199 bool direction(Direction dir); 01200 01207 inline bool addDirection(Direction dir) 01208 { return direction((Direction)(m_direction | dir)); } 01209 01216 inline bool delDirection(Direction dir) 01217 { return direction((Direction)(m_direction & ~dir)); } 01218 01224 bool dataPayload(int type); 01225 01231 bool eventPayload(int type); 01232 01238 bool silencePayload(int type); 01239 01246 inline bool localAddr(SocketAddr& addr, bool rtcp = true) 01247 { Lock lck(this); return m_transport && m_transport->localAddr(addr,rtcp); } 01248 01253 inline RTPSecure* security() const 01254 { return m_send ? m_send->security() : m_secure; } 01255 01260 void security(RTPSecure* secure); 01261 01266 void setReports(int interval); 01267 01272 virtual void getStats(NamedList& stats) const; 01273 01277 virtual void incWrongSrc(); 01278 01279 protected: 01284 virtual void timerTick(const Time& when); 01285 01290 void sendRtcpReport(const Time& when); 01291 01295 void sendRtcpBye(); 01296 01297 private: 01298 Direction m_direction; 01299 RTPSender* m_send; 01300 RTPReceiver* m_recv; 01301 RTPSecure* m_secure; 01302 u_int64_t m_reportTime; 01303 u_int64_t m_reportInterval; 01304 }; 01305 01310 class YRTP_API UDPTLSession : public UDPSession, public Mutex 01311 { 01312 public: 01316 ~UDPTLSession(); 01317 01323 inline bool localAddr(SocketAddr& addr) 01324 { Lock lck(this); return m_transport && m_transport->localAddr(addr,false); } 01325 01330 inline u_int16_t maxLen() const 01331 { return m_maxLen; } 01332 01337 inline u_int8_t maxSec() const 01338 { return m_maxSec; } 01339 01345 virtual void rtpData(const void* data, int len); 01346 01354 bool udptlSend(const void* data, int len, u_int16_t seq); 01355 01356 protected: 01362 UDPTLSession(u_int16_t maxLen = 250, u_int8_t maxSec = 2); 01363 01368 virtual void timerTick(const Time& when); 01369 01375 virtual RTPTransport* createTransport(); 01376 01384 virtual void udptlRecv(const void* data, int len, u_int16_t seq, bool recovered) = 0; 01385 01386 private: 01387 void recoverSec(const unsigned char* data, int len, u_int16_t seq, int nSec); 01388 u_int16_t m_rxSeq; 01389 u_int16_t m_txSeq; 01390 u_int16_t m_maxLen; 01391 u_int8_t m_maxSec; 01392 bool m_warn; 01393 ObjList m_txQueue; 01394 }; 01395 01400 class YRTP_API RTPSecure : public GenObject 01401 { 01402 friend class RTPReceiver; 01403 friend class RTPSender; 01404 friend class RTPSession; 01405 public: 01409 RTPSecure(); 01410 01415 RTPSecure(const String& suite); 01416 01421 RTPSecure(const RTPSecure& other); 01422 01426 virtual ~RTPSecure(); 01427 01432 inline RTPBaseIO* owner() const 01433 { return m_owner; } 01434 01439 void owner(RTPBaseIO* newOwner); 01440 01445 inline Cipher* rtpCipher() const 01446 { return m_rtpCipher; } 01447 01453 virtual bool supported(RTPSession* session = 0) const; 01454 01462 virtual bool setup(const String& suite, const String& keyParams, const ObjList* paramList = 0); 01463 01471 virtual bool create(String& suite, String& keyParams, bool buildMaster = true); 01472 01473 protected: 01477 virtual void init(); 01478 01484 virtual void rtpEncipher(unsigned char* data, int len); 01485 01492 virtual void rtpAddIntegrity(const unsigned char* data, int len, unsigned char* authData); 01493 01503 virtual bool rtpDecipher(unsigned char* data, int len, const void* secData, u_int32_t ssrc, u_int64_t seq); 01504 01514 virtual bool rtpCheckIntegrity(const unsigned char* data, int len, const void* authData, u_int32_t ssrc, u_int64_t seq); 01515 01525 bool deriveKey(Cipher& cipher, DataBlock& key, unsigned int len, unsigned char label, u_int64_t index = 0); 01526 01527 private: 01528 RTPBaseIO* m_owner; 01529 Cipher* m_rtpCipher; 01530 DataBlock m_masterKey; 01531 DataBlock m_masterSalt; 01532 DataBlock m_cipherKey; 01533 DataBlock m_cipherSalt; 01534 SHA1 m_authIpad; 01535 SHA1 m_authOpad; 01536 u_int32_t m_rtpAuthLen; 01537 bool m_rtpEncrypted; 01538 }; 01539 01540 } 01541 01542 #endif /* __YATERTP_H */ 01543 01544 /* vi: set ts=8 sw=4 sts=4 noet: */