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; 00046 00048 struct sr_extensions { 00050 u_int32_t srcid_; 00052 double eff_rtt_; 00053 u_int32_t& srcid() { return (srcid_); } 00054 double& eff_rtt() {return eff_rtt_;} 00055 sr_extensions* next; 00056 00057 00058 }; 00059 00061 struct sender_report { 00063 u_int32_t sender_srcid_; 00065 int pkts_sent_; 00067 int octets_sent_; 00068 /* per-field member functions */ 00069 u_int32_t& sender_srcid() { return (sender_srcid_); } 00071 int& pkts_sent() { return (pkts_sent_); } 00073 int& octets_sent() { return (octets_sent_); } 00074 sr_extensions* s_ext_; 00075 }; 00076 00078 struct receiver_report { 00080 int cum_pkts_lost_; 00082 double R_tcp_; 00084 double LSR_; 00086 double DLSR_; 00088 double jitter_; 00089 int bye_; 00090 /* per-field member functions */ 00092 int& cum_pkts_lost() { return (cum_pkts_lost_); } 00094 double& R_tcp() { return (R_tcp_); } 00096 double& LSR() { return (LSR_); } 00098 double& DLSR() { return (DLSR_); } 00100 double& jitter() { return (jitter_); } 00101 int& bye() { return (bye_); } 00102 00103 }; 00104 00106 struct hdr_rtp { 00108 u_int32_t srcid_; 00110 int seqno_; 00112 u_int16_t flags_; 00114 double timestamp_; 00115 static int offset_; 00116 inline static int& offset() { return offset_; } 00117 inline static hdr_rtp* access(const Packet* p) { 00118 return (hdr_rtp*) p->access(offset_); 00119 } 00120 /* per-field member functions */ 00122 u_int32_t& srcid() { return (srcid_); } 00124 int& seqno() { return (seqno_); } 00125 u_int16_t& flags() { return (flags_); } 00127 double& timestamp() { return (timestamp_); } 00128 sender_report* sr_; 00129 receiver_report* rr_; 00130 00131 00132 }; 00140 class RTPSource : public TclObject { 00141 public: 00142 00143 RTPSource* next; 00144 00146 RTPSource(u_int32_t srcid); 00148 inline u_int32_t srcid() { return (srcid_); } 00150 inline int np() { return (np_); } 00152 inline int snp() { return (snp_); } 00154 inline int ehsr() { return (ehsr_); } 00156 inline int nbytes() { return (nbytes_); } 00158 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00160 inline double LSR() { return (LSR_); } 00162 inline double SRT() { return (SRT_); } 00164 inline bool is_sender() { return (is_sender_); } 00166 inline int ps() { return (ps_); } 00168 inline double rate() { return (rate_); } 00170 inline double transit() { return (transit_); } 00172 inline double jitter() { return (jitter_); } 00173 00174 00176 inline void np(int n) { np_ += n; } 00178 inline void snp(int n) { snp_ = n; } 00180 inline void ehsr(int n) { ehsr_ = n; } 00182 inline void nbytes(int n) { nbytes_+= n; } 00184 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00186 inline void LSR(double n) { LSR_= n; } 00188 inline void SRT(double n) { SRT_= n; } 00190 inline void is_sender(bool n) { is_sender_= n; } 00192 inline void ps(int n) { ps_= n; } 00194 inline void rate(double n) { rate_= n; } 00196 inline void transit(double n) { transit_= n; } 00198 inline void jitter(double n) { jitter_= n; } 00199 protected: 00201 u_int32_t srcid_; 00203 int np_; 00205 int snp_; 00207 int ehsr_; 00209 int nbytes_; 00211 int cum_pkts_lost_; 00213 double LSR_; 00215 double SRT_; 00217 bool is_sender_; 00219 int ps_; 00221 double rate_; 00223 double transit_; 00225 double jitter_; 00226 00227 }; 00228 00232 class RTPReceiver : public TclObject { 00233 public: 00235 RTPReceiver* next; 00237 RTPReceiver(u_int32_t srcid); 00239 inline u_int32_t srcid() { return (srcid_); } 00241 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00243 inline double eff_rtt() { return (eff_rtt_); } 00245 inline double rate() { return (rate_); } 00246 00248 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00250 inline void eff_rtt(double n) { eff_rtt_ = n; } 00252 inline void rate(double n) { rate_ = n; } 00253 void receive_report(receiver_report*); 00254 00255 protected: 00257 u_int32_t srcid_; 00259 int cum_pkts_lost_; 00261 double eff_rtt_; 00263 double rate_; 00264 }; 00265 00266 00271 class RTPSession : public NsObject { 00272 public: 00273 RTPSession(); 00274 ~RTPSession(); 00275 /* the receive packet function */ 00276 virtual void recv(Packet* p, Handler*); 00277 /* the receive control packet function */ 00278 virtual void recv_ctrl(Packet* p); 00279 /* connection with TCL commands */ 00280 int command(int argc, const char*const* argv); 00281 /* returns the srcid of the local source */ 00282 inline u_int32_t srcid() { return (localsrc_->srcid()); } 00283 /* builds the RTCP SR and RR */ 00284 int build_report(int bye); 00285 /* icrements by one each time when a RTP packet is sent */ 00286 void localsrc_update(int); 00287 /* updates the trasnmission rate of the RTP sender */ 00288 void update_rate(); 00289 /* passes the Sender Report */ 00290 void pass_sr(sender_report*); 00291 /* increments by packet size (in bytes) each time when a RTP packet is sent */ 00292 void localsrc_update_nbytes(int n); 00293 /* calculates a smooth TCP-friendly rate */ 00294 void smooth_rate(double rate); 00295 RTPSource* allsrcs_; 00296 RTPSource* localsrc_; 00297 RTPReceiver* receivers_; 00299 int last_np_; 00300 /* we do not implement this function */ 00301 int build_sdes(); 00302 /* we do not implement this function */ 00303 int build_bye(); 00304 /* access the RTP header */ 00305 hdr_rtp* access_hdr_rtp(); 00306 /* for debugging */ 00307 void print_rcv(); 00309 int enableFlowControl_; 00311 double rx_recv_; 00313 double jitter_; 00315 double smooth_rate_; 00316 00317 00318 00319 protected: 00320 00321 RTPSource* lookup(u_int32_t); 00322 RTPReceiver* lookup_rcv(u_int32_t); 00324 list<double> lst; 00325 /* add this source */ 00326 void enter(RTPSource*); 00327 /* add this receiver */ 00328 void enter_rcv(RTPReceiver*); 00329 /* calculate RTT */ 00330 void calculate_RTT(); 00331 /* increase rate if packet losses have been not detected since last RR */ 00332 void increase_rate(int a); 00333 /* measure smooth packet loss ratio */ 00334 void measure_smooth_loss (double n); 00335 /* calculates TCP-friendly bandwidth share */ 00336 void calculateR_tcp(int a); 00337 /* the initial session rate */ 00338 void initial_rate(double a); 00340 double T_one_way_; 00342 hdr_rtp* rh_; 00343 /* removes the RTP receiver from the list */ 00344 void remove_receiver(RTPReceiver*); 00346 int we_sent; 00348 double RTT_; 00350 double weight[8]; 00352 double pkt_loss_history[8]; 00354 double smooth_loss_; 00356 int last_pkts_lost_; 00358 int last_ehsr_; 00360 double tx_rate_; 00362 double time_elapsed_; 00364 double last_time_report_; 00366 double alpha; 00367 /* calculates A */ 00368 void calculate_alpha(double value); 00369 /* core function for congestion control */ 00370 void evaluate_jitter(double value); 00372 double smooth_factor; 00374 list<double> jitter_lst; 00376 double z_score; 00377 void remove_sender(RTPSource*); 00378 00379 00380 00381 }; 00382 00383 class RTPAgent; 00388 class RTPTimer : public TimerHandler { 00389 public: 00390 RTPTimer(RTPAgent *a) : TimerHandler() { a_ = a; } 00391 protected: 00392 virtual void expire(Event *e); 00393 RTPAgent *a_; 00394 }; 00398 class RTPAgent : public Agent { 00399 public: 00400 RTPAgent(); 00401 virtual void timeout(int); 00402 virtual void recv(Packet* p, Handler*); 00403 virtual int command(int argc, const char*const* argv); 00404 void advanceby(int delta); 00405 virtual void sendmsg(int nbytes, const char *flags = 0); 00406 protected: 00407 virtual void sendpkt(); 00408 virtual void makepkt(Packet*); 00409 void rate_change(); 00410 virtual void start(); 00411 virtual void stop(); 00412 virtual void finish(); 00413 RTPSession* session_; 00414 double lastpkttime_; 00415 int seqno_; 00416 int running_; 00417 int random_; 00418 int maxpkts_; 00419 double interval_; 00420 RTPTimer rtp_timer_; 00421 double timestamp_; 00422 }; 00423 00424 #endif