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
00060 class YRTP_API RTPProcessor : public GenObject
00061 {
00062 friend class RTPGroup;
00063 friend class RTPTransport;
00064 friend class RTPSession;
00065 friend class RTPSender;
00066 friend class RTPReceiver;
00067
00068 public:
00072 RTPProcessor();
00073
00077 virtual ~RTPProcessor();
00078
00083 inline RTPGroup* group() const
00084 { return m_group; }
00085
00091 virtual void rtpData(const void* data, int len);
00092
00098 virtual void rtcpData(const void* data, int len);
00099
00100 protected:
00105 void group(RTPGroup* newgrp);
00106
00111 virtual void timerTick(const Time& when) = 0;
00112
00113 private:
00114 RTPGroup* m_group;
00115 };
00116
00122 class YRTP_API RTPGroup : public GenObject, public Mutex, public Thread
00123 {
00124 friend class RTPProcessor;
00125
00126 public:
00132 RTPGroup(int msec = 0, Priority prio = Normal);
00133
00137 virtual ~RTPGroup();
00138
00142 virtual void cleanup();
00143
00147 virtual void run();
00148
00153 static void setMinSleep(int msec);
00154
00155 protected:
00160 void join(RTPProcessor* proc);
00161
00166 void part(RTPProcessor* proc);
00167
00168 private:
00169 ObjList m_processors;
00170 bool m_listChanged;
00171 unsigned long m_sleep;
00172 };
00173
00178 class YRTP_API RTPTransport : public RTPProcessor
00179 {
00180 public:
00184 enum Activation {
00185 Inactive,
00186 Bound,
00187 Active
00188 };
00189
00193 RTPTransport();
00194
00198 virtual ~RTPTransport();
00199
00204 void setProcessor(RTPProcessor* processor = 0);
00205
00210 void setMonitor(RTPProcessor* monitor = 0);
00211
00216 inline const SocketAddr& localAddr() const
00217 { return m_localAddr; }
00218
00223 inline const SocketAddr& remoteAddr() const
00224 { return m_remoteAddr; }
00225
00232 bool localAddr(SocketAddr& addr, bool rtcp = true);
00233
00240 bool remoteAddr(SocketAddr& addr, bool sniff = false);
00241
00247 inline bool setTOS(int tos)
00248 { return m_rtpSock.setTOS(tos); }
00249
00254 inline Socket* rtpSock()
00255 { return &m_rtpSock; }
00256
00261 bool drillHole();
00262
00263 protected:
00268 virtual void timerTick(const Time& when);
00269
00275 virtual void rtpData(const void* data, int len);
00276
00282 virtual void rtcpData(const void* data, int len);
00283
00284 private:
00285 RTPProcessor* m_processor;
00286 RTPProcessor* m_monitor;
00287 Socket m_rtpSock;
00288 Socket m_rtcpSock;
00289 SocketAddr m_localAddr;
00290 SocketAddr m_remoteAddr;
00291 SocketAddr m_remoteRTCP;
00292 bool m_autoRemote;
00293 };
00294
00301 class YRTP_API RTPDejitter : public RTPProcessor
00302 {
00303 public:
00310 RTPDejitter(RTPReceiver* receiver, unsigned int mindelay, unsigned int maxdelay);
00311
00315 virtual ~RTPDejitter();
00316
00325 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00326 const void* data, int len);
00327
00328 protected:
00333 virtual void timerTick(const Time& when);
00334
00335 private:
00336 ObjList m_packets;
00337 RTPReceiver* m_receiver;
00338 unsigned int m_mindelay;
00339 unsigned int m_maxdelay;
00340 unsigned int m_headStamp;
00341 unsigned int m_tailStamp;
00342 u_int64_t m_headTime;
00343 u_int64_t m_tailTime;
00344 };
00345
00350 class YRTP_API RTPBaseIO
00351 {
00352 friend class RTPSession;
00353 public:
00357 inline RTPBaseIO(RTPSession* session = 0)
00358 : m_session(session), m_ssrcInit(true), m_ssrc(0), m_ts(0), m_seq(0),
00359 m_evTs(0), m_evNum(-1), m_evVol(-1),
00360 m_dataType(-1), m_eventType(-1), m_silenceType(-1)
00361 { }
00362
00366 virtual ~RTPBaseIO()
00367 { }
00368
00373 inline int dataPayload() const
00374 { return m_dataType; }
00375
00381 bool dataPayload(int type);
00382
00387 inline int eventPayload() const
00388 { return m_eventType; }
00389
00395 bool eventPayload(int type);
00396
00401 inline int silencePayload() const
00402 { return m_silenceType; }
00403
00410 bool silencePayload(int type);
00411
00416 unsigned int ssrcInit();
00417
00421 inline void reset()
00422 { m_ssrcInit = true; }
00423
00428 inline unsigned int ssrc() const
00429 { return m_ssrcInit ? 0 : m_ssrc; }
00430
00434 inline void ssrc(unsigned int src)
00435 { m_ssrc = src; m_ssrcInit = false; }
00436
00437 protected:
00442 virtual void timerTick(const Time& when) = 0;
00443
00444 RTPSession* m_session;
00445 bool m_ssrcInit;
00446 u_int32_t m_ssrc;
00447 u_int32_t m_ts;
00448 u_int16_t m_seq;
00449 u_int32_t m_evTs;
00450 int m_evNum;
00451 int m_evVol;
00452
00453 private:
00454 int m_dataType;
00455 int m_eventType;
00456 int m_silenceType;
00457 };
00458
00463 class YRTP_API RTPReceiver : public RTPBaseIO
00464 {
00465 friend class RTPSession;
00466 public:
00470 inline RTPReceiver(RTPSession* session = 0)
00471 : RTPBaseIO(session), m_dejitter(0), m_tsLast(0), m_warn(true)
00472 { }
00473
00477 virtual ~RTPReceiver();
00478
00483 void setDejitter(RTPDejitter* dejitter);
00484
00490 inline void setDejitter(unsigned int mindelay, unsigned int maxdelay)
00491 { setDejitter(new RTPDejitter(this,mindelay,maxdelay)); }
00492
00503 virtual bool rtpRecv(bool marker, int payload, unsigned int timestamp,
00504 const void* data, int len);
00505
00514 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00515 const void* data, int len);
00516
00526 virtual bool rtpRecvEvent(int event, char key, int duration,
00527 int volume, unsigned int timestamp);
00528
00536 virtual void rtpNewPayload(int payload, unsigned int timestamp);
00537
00545 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker);
00546
00547 protected:
00552 virtual void timerTick(const Time& when);
00553
00554 private:
00555 void rtpData(const void* data, int len);
00556 void rtcpData(const void* data, int len);
00557 bool decodeEvent(bool marker, unsigned int timestamp, const void* data, int len);
00558 bool decodeSilence(bool marker, unsigned int timestamp, const void* data, int len);
00559 void finishEvent(unsigned int timestamp);
00560 bool pushEvent(int event, int duration, int volume, unsigned int timestamp);
00561 RTPDejitter* m_dejitter;
00562 unsigned int m_tsLast;
00563 bool m_warn;
00564 };
00565
00570 class YRTP_API RTPSender : public RTPBaseIO
00571 {
00572 public:
00578 RTPSender(RTPSession* session = 0, bool randomTs = true);
00579
00583 virtual ~RTPSender()
00584 { }
00585
00595 bool rtpSend(bool marker, int payload, unsigned int timestamp,
00596 const void* data, int len);
00597
00606 bool rtpSendData(bool marker, unsigned int timestamp,
00607 const void* data, int len);
00608
00617 bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0);
00618
00627 bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0);
00628
00629
00634 inline int padding() const
00635 { return m_padding; }
00636
00642 bool padding(int chunk);
00643
00644 protected:
00649 virtual void timerTick(const Time& when);
00650
00651 private:
00652 int m_evTime;
00653 unsigned int m_tsLast;
00654 unsigned char m_padding;
00655 bool sendEventData(unsigned int timestamp);
00656 };
00657
00662 class YRTP_API RTPSession : public RTPProcessor
00663 {
00664 public:
00668 enum Direction {
00669 FullStop = 0,
00670 RecvOnly = 1,
00671 SendOnly = 2,
00672 SendRecv = 3
00673 };
00674
00678 RTPSession();
00679
00683 virtual ~RTPSession();
00684
00690 virtual void rtpData(const void* data, int len);
00691
00697 virtual void rtcpData(const void* data, int len);
00698
00707 virtual bool rtpRecvData(bool marker, unsigned int timestamp,
00708 const void* data, int len);
00709
00719 virtual bool rtpRecvEvent(int event, char key, int duration,
00720 int volume, unsigned int timestamp);
00721
00729 virtual void rtpNewPayload(int payload, unsigned int timestamp);
00730
00738 virtual void rtpNewSSRC(u_int32_t newSsrc, bool marker);
00739
00745 virtual RTPSender* createSender();
00746
00752 virtual RTPReceiver* createReceiver();
00753
00759 virtual RTPTransport* createTransport();
00760
00765 bool initTransport();
00766
00773 bool initGroup(int msec = 0, Thread::Priority prio = Thread::Normal);
00774
00784 inline bool rtpSend(bool marker, int payload, unsigned int timestamp,
00785 const void* data, int len)
00786 { return m_send && m_send->rtpSend(marker,payload,timestamp,data,len); }
00787
00796 inline bool rtpSendData(bool marker, unsigned int timestamp,
00797 const void* data, int len)
00798 { return m_send && m_send->rtpSendData(marker,timestamp,data,len); }
00799
00808 inline bool rtpSendEvent(int event, int duration, int volume = 0, unsigned int timestamp = 0)
00809 { return m_send && m_send->rtpSendEvent(event,duration,volume,timestamp); }
00810
00819 inline bool rtpSendKey(char key, int duration, int volume = 0, unsigned int timestamp = 0)
00820 { return m_send && m_send->rtpSendKey(key,duration,volume,timestamp); }
00821
00826 inline int padding() const
00827 { return m_send ? m_send->padding() : 0; }
00828
00834 inline bool padding(int chunk)
00835 { return m_send && m_send->padding(chunk); }
00836
00842 inline void setDejitter(unsigned int mindelay = 20, unsigned int maxdelay = 50)
00843 { if (m_recv) m_recv->setDejitter(mindelay,maxdelay); }
00844
00849 inline RTPTransport* transport() const
00850 { return m_transport; }
00851
00856 void transport(RTPTransport* trans);
00857
00862 inline RTPSender* sender() const
00863 { return m_send; }
00864
00869 void sender(RTPSender* send);
00870
00875 inline RTPReceiver* receiver() const
00876 { return m_recv; }
00877
00882 void receiver(RTPReceiver* recv);
00883
00888 inline Direction direction() const
00889 { return m_direction; }
00890
00897 bool direction(Direction dir);
00898
00905 inline bool addDirection(Direction dir)
00906 { return direction((Direction)(m_direction | dir)); }
00907
00914 inline bool delDirection(Direction dir)
00915 { return direction((Direction)(m_direction & ~dir)); }
00916
00922 bool dataPayload(int type);
00923
00929 bool eventPayload(int type);
00930
00936 bool silencePayload(int type);
00937
00944 inline bool localAddr(SocketAddr& addr, bool rtcp = true)
00945 { return m_transport && m_transport->localAddr(addr,rtcp); }
00946
00953 inline bool remoteAddr(SocketAddr& addr, bool sniff = false)
00954 { return m_transport && m_transport->remoteAddr(addr,sniff); }
00955
00961 inline bool setTOS(int tos)
00962 { return m_transport && m_transport->setTOS(tos); }
00963
00968 inline Socket* rtpSock()
00969 { return m_transport ? m_transport->rtpSock() : 0; }
00970
00975 inline bool drillHole()
00976 { return m_transport && m_transport->drillHole(); }
00977
00982 void setTimeout(int interval);
00983
00984 protected:
00989 virtual void timerTick(const Time& when);
00990
00995 virtual void timeout(bool initial);
00996
00997 private:
00998 RTPTransport* m_transport;
00999 Direction m_direction;
01000 RTPSender* m_send;
01001 RTPReceiver* m_recv;
01002 u_int64_t m_timeoutTime;
01003 u_int64_t m_timeoutInterval;
01004 };
01005
01006 }
01007
01008 #endif
01009
01010