00001 00035 #ifndef ns_rtp_h 00036 #define ns_rtp_h 00037 00038 #include "config.h" 00039 #include "object.h" 00040 #include "agent.h" 00041 #include "timer-handler.h" 00042 #include <list> 00043 00044 #define RTP_M 0x0080 // marker for significant events 00045 class RTPReceiver; 00047 struct sender_report { 00049 u_int32_t sender_srcid_; 00051 int pkts_sent_; 00053 int octets_sent_; 00055 RTPReceiver* rcvrs_; 00056 /* per-field member functions */ 00057 u_int32_t& sender_srcid() { return (sender_srcid_); } 00059 int& pkts_sent() { return (pkts_sent_); } 00061 int& octets_sent() { return (octets_sent_); } 00062 }; 00063 00065 struct receiver_report { 00067 int cum_pkts_lost_; 00069 double R_tcp_; 00071 double LSR_; 00073 double DLSR_; 00075 double jitter_; 00076 int bye_; 00077 /* per-field member functions */ 00079 int& cum_pkts_lost() { return (cum_pkts_lost_); } 00081 double& R_tcp() { return (R_tcp_); } 00083 double& LSR() { return (LSR_); } 00085 double& DLSR() { return (DLSR_); } 00087 double& jitter() { return (jitter_); } 00088 int& bye() { return (bye_); } 00089 00090 }; 00091 00093 struct hdr_rtp { 00095 u_int32_t srcid_; 00097 int seqno_; 00099 u_int16_t flags_; 00101 double timestamp_; 00102 static int offset_; 00103 inline static int& offset() { return offset_; } 00104 inline static hdr_rtp* access(const Packet* p) { 00105 return (hdr_rtp*) p->access(offset_); 00106 } 00107 /* per-field member functions */ 00109 u_int32_t& srcid() { return (srcid_); } 00111 int& seqno() { return (seqno_); } 00112 u_int16_t& flags() { return (flags_); } 00114 double& timestamp() { return (timestamp_); } 00116 sender_report* sr_; 00118 receiver_report* rr_; 00119 00120 00121 }; 00128 class RTPSource : public TclObject { 00129 public: 00130 00131 RTPSource* next; 00132 00134 RTPSource(u_int32_t srcid); 00136 inline u_int32_t srcid() { return (srcid_); } 00138 inline int np() { return (np_); } 00140 inline int snp() { return (snp_); } 00142 inline int ehsr() { return (ehsr_); } 00144 inline int nbytes() { return (nbytes_); } 00146 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00148 inline double LSR() { return (LSR_); } 00150 inline double SRT() { return (SRT_); } 00152 inline bool is_sender() { return (is_sender_); } 00154 inline int ps() { return (ps_); } 00156 inline double rate() { return (rate_); } 00158 inline double transit() { return (transit_); } 00160 inline double jitter() { return (jitter_); } 00161 00163 inline void np(int n) { np_ += n; } 00165 inline void snp(int n) { snp_ = n; } 00167 inline void ehsr(int n) { ehsr_ = n; } 00169 inline void nbytes(int n) { nbytes_+= n; } 00171 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00173 inline void LSR(double n) { LSR_= n; } 00175 inline void SRT(double n) { SRT_= n; } 00177 inline void is_sender(bool n) { is_sender_= n; } 00179 inline void ps(int n) { ps_= n; } 00181 inline void rate(double n) { rate_= n; } 00183 inline void transit(double n) { transit_= n; } 00185 inline void jitter(double n) { jitter_= n; } 00186 protected: 00188 u_int32_t srcid_; 00190 int np_; 00192 int snp_; 00194 int ehsr_; 00196 int nbytes_; 00198 int cum_pkts_lost_; 00200 double LSR_; 00202 double SRT_; 00204 bool is_sender_; 00206 int ps_; 00208 double rate_; 00210 double transit_; 00212 double jitter_; 00213 00214 }; 00215 00219 class RTPReceiver : public TclObject { 00220 public: 00222 RTPReceiver* next; 00224 RTPReceiver(u_int32_t srcid); 00226 inline u_int32_t srcid() { return (srcid_); } 00228 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00230 inline double eff_rtt() { return (eff_rtt_); } 00232 inline double rate() { return (rate_); } 00234 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00236 inline void eff_rtt(double n) { eff_rtt_ = n; } 00238 inline void rate(double n) { rate_ = n; } 00239 void receive_report(receiver_report*); 00240 00241 protected: 00243 u_int32_t srcid_; 00245 int cum_pkts_lost_; 00247 double eff_rtt_; 00249 double rate_; 00250 }; 00251 00252 00257 class RTPSession : public NsObject { 00258 public: 00259 RTPSession(); 00260 ~RTPSession(); 00261 /* the receive packet function */ 00262 virtual void recv(Packet* p, Handler*); 00263 /* the receive control packet function */ 00264 virtual void recv_ctrl(Packet* p); 00265 /* connection with TCL commands */ 00266 int command(int argc, const char*const* argv); 00267 /* returns the srcid of the local source */ 00268 inline u_int32_t srcid() { return (localsrc_->srcid()); } 00269 /* builds the RTCP SR and RR */ 00270 int build_report(int bye); 00271 /* icrements by one each time when a RTP packet is sent */ 00272 void localsrc_update(int); 00273 /* updates the trasnmission rate of the RTP sender */ 00274 void update_rate(); 00275 /* passes the Sender Report */ 00276 void pass_sr(sender_report*); 00277 /* increments by packet size (in bytes) each time when a RTP packet is sent */ 00278 void localsrc_update_nbytes(int n); 00279 /* calculates a smooth TCP-friendly rate */ 00280 void smooth_rate(double rate); 00281 RTPSource* allsrcs_; 00282 RTPSource* localsrc_; 00283 RTPReceiver* receivers_; 00285 int last_np_; 00286 /* we do not implement this function */ 00287 int build_sdes(); 00288 /* future work */ 00289 int build_bye(); 00290 /* access the RTP header */ 00291 hdr_rtp* access_hdr_rtp(); 00292 /* for debugging */ 00293 void print_rcv(); 00295 int enableFlowControl_; 00297 double rx_recv_; 00299 double jitter_; 00300 00301 00302 protected: 00303 00304 RTPSource* lookup(u_int32_t); 00305 RTPReceiver* lookup_rcv(u_int32_t); 00307 list<double> lst; 00308 /* add this source */ 00309 void enter(RTPSource*); 00310 /* add this receiver */ 00311 void enter_rcv(RTPReceiver*); 00312 /* calculate RTT */ 00313 void calculate_RTT(); 00314 /* increase rate if packet losses have been not detected since last RR */ 00315 void increase_rate(int a); 00316 /* measure smooth packet loss ratio */ 00317 void measure_smooth_loss (double n); 00318 /* calculates TCP-friendly bandwidth share */ 00319 void calculateR_tcp(int a); 00320 /* the initial session rate */ 00321 void initial_rate(double a); 00323 double T_one_way_; 00325 hdr_rtp* rh_; 00326 /* removes the RTP receiver from the list */ 00327 void remove_receiver(RTPReceiver*); 00329 int we_sent; 00331 double RTT_; 00333 double weight[8]; 00335 double pkt_loss_history[8]; 00337 double smooth_loss_; 00339 int last_pkts_lost_; 00341 int last_ehsr_; 00343 double tx_rate_; 00345 double time_elapsed_; 00347 double last_time_report_; 00349 double alpha; 00350 /* calculates A */ 00351 void calculate_alpha(double value); 00352 00353 00354 00355 }; 00356 00357 class RTPAgent; 00362 class RTPTimer : public TimerHandler { 00363 public: 00364 RTPTimer(RTPAgent *a) : TimerHandler() { a_ = a; } 00365 protected: 00366 virtual void expire(Event *e); 00367 RTPAgent *a_; 00368 }; 00373 class RTPAgent : public Agent { 00374 public: 00375 RTPAgent(); 00376 virtual void timeout(int); 00377 virtual void recv(Packet* p, Handler*); 00378 virtual int command(int argc, const char*const* argv); 00379 void advanceby(int delta); 00380 virtual void sendmsg(int nbytes, const char *flags = 0); 00381 protected: 00382 virtual void sendpkt(); 00383 virtual void makepkt(Packet*); 00384 void rate_change(); 00385 virtual void start(); 00386 virtual void stop(); 00387 virtual void finish(); 00388 RTPSession* session_; 00389 double lastpkttime_; 00390 int seqno_; 00391 int running_; 00392 int random_; 00393 int maxpkts_; 00394 double interval_; 00395 RTPTimer rtp_timer_; 00396 double timestamp_; 00397 }; 00398 00399 #endif