00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00108 protected:
00113 void group(RTPGroup* newgrp);
00114
00119 virtual void timerTick(const Time& when) = 0;
00120
00121 private:
00122 RTPGroup* m_group;
00123 };
00124
00130 class YRTP_API RTPGroup : public GenObject, public Mutex, public Thread
00131 {
00132 friend class RTPProcessor;
00133
00134 public:
00140 RTPGroup(int msec = 0, Priority prio = Normal);
00141
00145 virtual ~RTPGroup();
00146
00150 virtual void cleanup();
00151
00155 virtual void run();
00156
00161 static void setMinSleep(int msec);
00162
00167 void join(RTPProcessor* proc);
00168
00173 void part(RTPProcessor* proc);
00174
00175 private:
00176 ObjList m_processors;
00177 bool m_listChanged;
00178 unsigned long m_sleep;
00179 };
00180
00185 class YRTP_API RTPTransport : public RTPProcessor
00186 {
00187 public:
00191 enum Activation {
00192 Inactive,
00193 Bound,
00194 Active
00195 };
00196
00200 enum Type {
00201 Unknown,
00202 RTP,
00203 UDPTL
00204 };
00205
00210 RTPTransport(Type type = RTP);
00211
00215 virtual ~RTPTransport();
00216
00221 void setProcessor(RTPProcessor* processor = 0);
00222
00227 void setMonitor(RTPProcessor* monitor = 0);
00228
00233 inline const SocketAddr& localAddr() const
00234 { return m_localAddr; }
00235
00240 inline const SocketAddr& remoteAddr() const
00241 { return m_remoteAddr; }
00242
00249 bool localAddr(SocketAddr& addr, bool rtcp = true);
00250
00257 bool remoteAddr(SocketAddr& addr, bool sniff = false);
00258
00264 inline bool setTOS(int tos)
00265 { return m_rtpSock.setTOS(tos); }
00266
00271 inline Socket* rtpSock()
00272 { return &m_rtpSock; }
00273
00278 bool drillHole();
00279
00280 protected:
00285 virtual void timerTick(const Time& when);
00286
00292 virtual void rtpData(const void* data, int len);
00293
00299 virtual void rtcpData(const void* data, int len);
00300
00301 private:
00302 Type m_type;
00303 RTPProcessor* m_processor;
00304 RTPProcessor* m_monitor;
00305 Socket m_rtpSock;
00306 Socket m_rtcpSock;
00307 SocketAddr m_localAddr;
00308 SocketAddr m_remoteAddr;
00309 SocketAddr m_remoteRTCP;
00310 bool m_autoRemote;
00311 };
00312
00319 class YRTP_API RTPDejitter : public RTPProcessor
00320 {
00321 public:
00328 RTPDejitter(RTPReceiver* receiver, unsigned int mindelay, unsigned int maxdelay);
00329
00333 virtual ~RTPDejitter();
00334
00343 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00344 const void* data, int len);
00345
00346 protected:
00351 virtual void timerTick(const Time& when);
00352
00353 private:
00354 ObjList m_packets;
00355 RTPReceiver* m_receiver;
00356 unsigned int m_mindelay;
00357 unsigned int m_maxdelay;
00358 unsigned int m_headStamp;
00359 unsigned int m_tailStamp;
00360 u_int64_t m_headTime;
00361 u_int64_t m_tailTime;
00362 };
00363
00368 class YRTP_API RTPBaseIO
00369 {
00370 friend class RTPSession;
00371 friend class RTPSecure;
00372 public:
00376 inline RTPBaseIO(RTPSession* session = 0)
00377 : m_session(session), m_secure(0),
00378 m_ssrcInit(true), m_ssrc(0), m_ts(0),
00379 m_seq(0), m_rollover(0), m_secLen(0), m_mkiLen(0),
00380 m_evTs(0), m_evNum(-1), m_evVol(-1),
00381 m_ioPackets(), m_ioOctets(0),
00382 m_dataType(-1), m_eventType(-1), m_silenceType(-1)
00383 { }
00384
00388 virtual ~RTPBaseIO();
00389
00394 inline int dataPayload() const
00395 { return m_dataType; }
00396
00402 bool dataPayload(int type);
00403
00408 inline int eventPayload() const
00409 { return m_eventType; }
00410
00416 bool eventPayload(int type);
00417
00422 inline int silencePayload() const
00423 { return m_silenceType; }
00424
00431 bool silencePayload(int type);
00432
00437 unsigned int ssrcInit();
00438
00442 inline void reset()
00443 { m_ssrcInit = true; }
00444
00449 inline unsigned int ssrc() const
00450 { return m_ssrcInit ? 0 : m_ssrc; }
00451
00455 inline void ssrc(unsigned int src)
00456 { m_ssrc = src; m_ssrcInit = false; }
00457
00462 inline u_int16_t seq() const
00463 { return m_seq; }
00464
00469 inline u_int32_t rollover() const
00470 { return m_rollover; }
00471
00476 inline u_int64_t fullSeq() const
00477 { return m_seq | (((u_int64_t)m_rollover) << 16); }
00478
00483 inline u_int32_t ioPackets() const
00484 { return m_ioPackets; }
00485
00490 inline u_int32_t ioOctets() const
00491 { return m_ioOctets; }
00492
00497 inline RTPSession* session() const
00498 { return m_session; }
00499
00504 inline RTPSecure* security() const
00505 { return m_secure; }
00506
00511 void security(RTPSecure* secure);
00512
00513 protected:
00518 virtual void timerTick(const Time& when) = 0;
00519
00525 inline void secLength(u_int32_t len, u_int32_t key = 0)
00526 { m_secLen = len; m_mkiLen = key; }
00527
00528 RTPSession* m_session;
00529 RTPSecure* m_secure;
00530 bool m_ssrcInit;
00531 u_int32_t m_ssrc;
00532 u_int32_t m_ts;
00533 u_int16_t m_seq;
00534 u_int32_t m_rollover;
00535 u_int16_t m_secLen;
00536 u_int16_t m_mkiLen;
00537 u_int32_t m_evTs;
00538 int m_evNum;
00539 int m_evVol;
00540 u_int32_t m_ioPackets;
00541 u_int32_t m_ioOctets;
00542
00543 private:
00544 int m_dataType;
00545 int m_eventType;
00546 int m_silenceType;
00547 };
00548
00553 class YRTP_API RTPReceiver : public RTPBaseIO
00554 {
00555 friend class RTPSession;
00556 public:
00560 inline RTPReceiver(RTPSession* session = 0)
00561 : RTPBaseIO(session),
00562 m_ioLostPkt(0), m_dejitter(0), m_tsLast(0),
00563 m_seqSync(0), m_seqCount(0), m_warn(true)
00564 { }
00565
00569 virtual ~RTPReceiver();
00570
00575 inline u_int32_t ioPacketsLost() const
00576 { return m_ioLostPkt; }
00577
00578
00583 void setDejitter(RTPDejitter* dejitter);
00584
00590 inline void setDejitter(unsigned int mindelay, unsigned int maxdelay)
00591 { setDejitter(new RTPDejitter(this,mindelay,maxdelay)); }
00592
00603 virtual bool rtpRecv(bool marker, int payload, unsigned int timestamp,
00604 const void* data, int len);
00605
00614 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00615 const void* data, int len);
00616
00626 virtual bool rtpRecvEvent(int event, char key, int duration,
00627 int volume, unsigned int timestamp);
00628
00636 virtual void rtpNewPayload(int payload, unsigned int timestamp);
00637
00645 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker);
00646
00647 protected:
00652 virtual void timerTick(const Time& when);
00653
00664 virtual bool rtpDecipher(unsigned char* data, int len, const void* secData, u_int32_t ssrc, u_int64_t seq);
00665
00676 virtual bool rtpCheckIntegrity(const unsigned char* data, int len, const void* authData, u_int32_t ssrc, u_int64_t seq);
00677
00678 u_int32_t m_ioLostPkt;
00679
00680 private:
00681 void rtpData(const void* data, int len);
00682 void rtcpData(const void* data, int len);
00683 bool decodeEvent(bool marker, unsigned int timestamp, const void* data, int len);
00684 bool decodeSilence(bool marker, unsigned int timestamp, const void* data, int len);
00685 void finishEvent(unsigned int timestamp);
00686 bool pushEvent(int event, int duration, int volume, unsigned int timestamp);
00687 RTPDejitter* m_dejitter;
00688 unsigned int m_tsLast;
00689 u_int16_t m_seqSync;
00690 u_int16_t m_seqCount;
00691 bool m_warn;
00692 };
00693
00698 class YRTP_API RTPSender : public RTPBaseIO
00699 {
00700 public:
00706 RTPSender(RTPSession* session = 0, bool randomTs = true);
00707
00711 virtual ~RTPSender()
00712 { }
00713
00723 bool rtpSend(bool marker, int payload, unsigned int timestamp,
00724 const void* data, int len);
00725
00734 bool rtpSendData(bool marker, unsigned int timestamp,
00735 const void* data, int len);
00736
00745 bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0);
00746
00755 bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0);
00756
00757
00762 inline int padding() const
00763 { return m_padding; }
00764
00770 bool padding(int chunk);
00771
00772 protected:
00777 virtual void timerTick(const Time& when);
00778
00785 virtual void rtpEncipher(unsigned char* data, int len);
00786
00794 virtual void rtpAddIntegrity(const unsigned char* data, int len, unsigned char* authData);
00795
00796
00797 private:
00798 int m_evTime;
00799 unsigned int m_tsLast;
00800 unsigned char m_padding;
00801 bool sendEventData(unsigned int timestamp);
00802 };
00803
00808 class YRTP_API UDPSession : public RTPProcessor
00809 {
00810 public:
00814 virtual ~UDPSession();
00815
00821 virtual RTPTransport* createTransport();
00822
00827 bool initTransport();
00828
00835 bool initGroup(int msec = 0, Thread::Priority prio = Thread::Normal);
00836
00843 inline bool remoteAddr(SocketAddr& addr, bool sniff = false)
00844 { return m_transport && m_transport->remoteAddr(addr,sniff); }
00845
00851 inline bool setTOS(int tos)
00852 { return m_transport && m_transport->setTOS(tos); }
00853
00858 inline Socket* rtpSock()
00859 { return m_transport ? m_transport->rtpSock() : 0; }
00860
00865 inline bool drillHole()
00866 { return m_transport && m_transport->drillHole(); }
00867
00872 void setTimeout(int interval);
00873
00878 inline RTPTransport* transport() const
00879 { return m_transport; }
00880
00885 virtual void transport(RTPTransport* trans);
00886
00887 protected:
00891 UDPSession();
00892
00897 virtual void timeout(bool initial);
00898
00899 RTPTransport* m_transport;
00900 u_int64_t m_timeoutTime;
00901 u_int64_t m_timeoutInterval;
00902 };
00903
00908 class YRTP_API RTPSession : public UDPSession
00909 {
00910 public:
00914 enum Direction {
00915 FullStop = 0,
00916 RecvOnly = 1,
00917 SendOnly = 2,
00918 SendRecv = 3
00919 };
00920
00924 RTPSession();
00925
00929 virtual ~RTPSession();
00930
00935 virtual void getStats(String& stats) const;
00936
00942 virtual void rtpData(const void* data, int len);
00943
00949 virtual void rtcpData(const void* data, int len);
00950
00959 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00960 const void* data, int len);
00961
00971 virtual bool rtpRecvEvent(int event, char key, int duration,
00972 int volume, unsigned int timestamp);
00973
00981 virtual void rtpNewPayload(int payload, unsigned int timestamp);
00982
00990 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker);
00991
00997 virtual RTPSender* createSender();
00998
01004 virtual RTPReceiver* createReceiver();
01005
01012 virtual Cipher* createCipher(const String& name, Cipher::Direction dir);
01013
01019 virtual bool checkCipher(const String& name);
01020
01030 inline bool rtpSend(bool marker, int payload, unsigned int timestamp,
01031 const void* data, int len)
01032 { return m_send && m_send->rtpSend(marker,payload,timestamp,data,len); }
01033
01042 inline bool rtpSendData(bool marker, unsigned int timestamp,
01043 const void* data, int len)
01044 { return m_send && m_send->rtpSendData(marker,timestamp,data,len); }
01045
01054 inline bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0)
01055 { return m_send && m_send->rtpSendEvent(event,duration,volume,timestamp); }
01056
01065 inline bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0)
01066 { return m_send && m_send->rtpSendKey(key,duration,volume,timestamp); }
01067
01072 inline int padding() const
01073 { return m_send ? m_send->padding() : 0; }
01074
01080 inline bool padding(int chunk)
01081 { return m_send && m_send->padding(chunk); }
01082
01088 inline void setDejitter(unsigned int mindelay = 20, unsigned int maxdelay = 50)
01089 { if (m_recv) m_recv->setDejitter(mindelay,maxdelay); }
01090
01095 virtual void transport(RTPTransport* trans);
01096
01101 inline RTPSender* sender() const
01102 { return m_send; }
01103
01108 void sender(RTPSender* send);
01109
01114 inline RTPReceiver* receiver() const
01115 { return m_recv; }
01116
01121 void receiver(RTPReceiver* recv);
01122
01127 inline Direction direction() const
01128 { return m_direction; }
01129
01136 bool direction(Direction dir);
01137
01144 inline bool addDirection(Direction dir)
01145 { return direction((Direction)(m_direction | dir)); }
01146
01153 inline bool delDirection(Direction dir)
01154 { return direction((Direction)(m_direction & ~dir)); }
01155
01161 bool dataPayload(int type);
01162
01168 bool eventPayload(int type);
01169
01175 bool silencePayload(int type);
01176
01183 inline bool localAddr(SocketAddr& addr, bool rtcp = true)
01184 { return m_transport && m_transport->localAddr(addr,rtcp); }
01185
01190 inline RTPSecure* security() const
01191 { return m_send ? m_send->security() : m_secure; }
01192
01197 void security(RTPSecure* secure);
01198
01199 protected:
01204 virtual void timerTick(const Time& when);
01205
01206 private:
01207 Direction m_direction;
01208 RTPSender* m_send;
01209 RTPReceiver* m_recv;
01210 RTPSecure* m_secure;
01211 };
01212
01217 class YRTP_API UDPTLSession : public UDPSession
01218 {
01219 public:
01223 ~UDPTLSession();
01224
01230 inline bool localAddr(SocketAddr& addr)
01231 { return m_transport && m_transport->localAddr(addr,false); }
01232
01237 inline u_int16_t maxLen() const
01238 { return m_maxLen; }
01239
01244 inline u_int8_t maxSec() const
01245 { return m_maxSec; }
01246
01252 virtual void rtpData(const void* data, int len);
01253
01261 bool udptlSend(const void* data, int len, u_int16_t seq);
01262
01263 protected:
01269 UDPTLSession(u_int16_t maxLen = 250, u_int8_t maxSec = 2);
01270
01275 virtual void timerTick(const Time& when);
01276
01282 virtual RTPTransport* createTransport();
01283
01291 virtual void udptlRecv(const void* data, int len, u_int16_t seq, bool recovered) = 0;
01292
01293 private:
01294 void recoverSec(const unsigned char* data, int len, u_int16_t seq, int nSec);
01295 u_int16_t m_rxSeq;
01296 u_int16_t m_txSeq;
01297 u_int16_t m_maxLen;
01298 u_int8_t m_maxSec;
01299 bool m_warn;
01300 ObjList m_txQueue;
01301 };
01302
01307 class YRTP_API RTPSecure : public GenObject
01308 {
01309 friend class RTPReceiver;
01310 friend class RTPSender;
01311 friend class RTPSession;
01312 public:
01316 RTPSecure();
01317
01322 RTPSecure(const String& suite);
01323
01328 RTPSecure(const RTPSecure& other);
01329
01333 virtual ~RTPSecure();
01334
01339 inline RTPBaseIO* owner() const
01340 { return m_owner; }
01341
01346 void owner(RTPBaseIO* newOwner);
01347
01352 inline Cipher* rtpCipher() const
01353 { return m_rtpCipher; }
01354
01360 virtual bool supported(RTPSession* session = 0) const;
01361
01369 virtual bool setup(const String& suite, const String& keyParams, const ObjList* paramList = 0);
01370
01378 virtual bool create(String& suite, String& keyParams, bool buildMaster = true);
01379
01380 protected:
01384 virtual void init();
01385
01391 virtual void rtpEncipher(unsigned char* data, int len);
01392
01399 virtual void rtpAddIntegrity(const unsigned char* data, int len, unsigned char* authData);
01400
01410 virtual bool rtpDecipher(unsigned char* data, int len, const void* secData, u_int32_t ssrc, u_int64_t seq);
01411
01421 virtual bool rtpCheckIntegrity(const unsigned char* data, int len, const void* authData, u_int32_t ssrc, u_int64_t seq);
01422
01432 bool deriveKey(Cipher& cipher, DataBlock& key, unsigned int len, unsigned char label, u_int64_t index = 0);
01433
01434 private:
01435 RTPBaseIO* m_owner;
01436 Cipher* m_rtpCipher;
01437 DataBlock m_masterKey;
01438 DataBlock m_masterSalt;
01439 DataBlock m_cipherKey;
01440 DataBlock m_cipherSalt;
01441 SHA1 m_authIpad;
01442 SHA1 m_authOpad;
01443 u_int32_t m_rtpAuthLen;
01444 bool m_rtpEncrypted;
01445 };
01446
01447 }
01448
01449 #endif
01450
01451