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 00045 #define RTP_M 0x0080 // marker for significant events 00046 class RTPReceiver; 00047 class SimStream; 00048 00050 struct sr_extensions { 00052 u_int32_t srcid_; 00054 double eff_rtt_; 00055 u_int32_t& srcid() { return (srcid_); } 00056 double& eff_rtt() {return eff_rtt_;} 00057 sr_extensions* next; 00058 00059 00060 }; 00062 struct sender_report { 00064 u_int32_t sender_srcid_; 00066 int pkts_sent_; 00068 int octets_sent_; 00070 double low_tx_; 00072 double medium_tx_; 00074 double high_tx_; 00075 /* per-field member functions */ 00076 u_int32_t& sender_srcid() { return (sender_srcid_); } 00078 int& pkts_sent() { return (pkts_sent_); } 00080 int& octets_sent() { return (octets_sent_); } 00081 double& low_tx() {return low_tx_;} 00082 double& medium_tx() {return medium_tx_;} 00083 double& high_tx() {return high_tx_;} 00084 sr_extensions* s_ext_; 00085 }; 00086 00088 struct receiver_report { 00090 int cum_pkts_lost_; 00092 double R_tcp_; 00094 double LSR_; 00096 double DLSR_; 00098 double jitter_; 00100 int bye_; 00101 double rx_; 00102 /* per-field member functions */ 00104 int& cum_pkts_lost() { return (cum_pkts_lost_); } 00106 double& R_tcp() { return (R_tcp_); } 00108 double& LSR() { return (LSR_); } 00110 double& DLSR() { return (DLSR_); } 00112 double& jitter() { return (jitter_); } 00113 int& bye() { return (bye_); } 00114 double& rx() { return (rx_); } 00115 00116 }; 00117 00119 struct hdr_rtp { 00121 u_int32_t srcid_; 00123 int seqno_; 00125 u_int16_t flags_; 00127 double timestamp_; 00128 u_int32_t T_epoch_; 00129 static int offset_; 00130 inline static int& offset() { return offset_; } 00131 inline static hdr_rtp* access(const Packet* p) { 00132 return (hdr_rtp*) p->access(offset_); 00133 } 00134 /* per-field member functions */ 00136 u_int32_t& srcid() { return (srcid_); } 00138 int& seqno() { return (seqno_); } 00139 u_int16_t& flags() { return (flags_); } 00141 double& timestamp() { return (timestamp_); } 00142 u_int32_t& T_epoch() { return (T_epoch_); } 00143 sender_report* sr_; 00144 receiver_report* rr_; 00145 00146 00147 }; 00155 class RTPSource : public TclObject { 00156 public: 00157 00158 RTPSource* next; 00159 00161 RTPSource(u_int32_t srcid); 00163 inline u_int32_t srcid() { return (srcid_); } 00165 inline int np() { return (np_); } 00167 inline int snp() { return (snp_); } 00169 inline int ehsr() { return (ehsr_); } 00171 inline int nbytes() { return (nbytes_); } 00173 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00175 inline double LSR() { return (LSR_); } 00177 inline double SRT() { return (SRT_); } 00179 inline bool is_sender() { return (is_sender_); } 00181 inline int ps() { return (ps_); } 00183 inline double rate() { return (rate_); } 00185 inline double transit() { return (transit_); } 00187 inline double jitter() { return (jitter_); } 00188 00189 00191 inline void np(int n) { np_ += n; } 00193 inline void snp(int n) { snp_ = n; } 00195 inline void ehsr(int n) { ehsr_ = n; } 00197 inline void nbytes(int n) { nbytes_+= n; } 00199 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00201 inline void LSR(double n) { LSR_= n; } 00203 inline void SRT(double n) { SRT_= n; } 00205 inline void is_sender(bool n) { is_sender_= n; } 00207 inline void ps(int n) { ps_= n; } 00209 inline void rate(double n) { rate_= n; } 00211 inline void transit(double n) { transit_= n; } 00213 inline void jitter(double n) { jitter_= n; } 00214 protected: 00216 u_int32_t srcid_; 00218 int np_; 00220 int snp_; 00222 int ehsr_; 00224 int nbytes_; 00226 int cum_pkts_lost_; 00228 double LSR_; 00230 double SRT_; 00232 bool is_sender_; 00234 int ps_; 00236 double rate_; 00238 double transit_; 00240 double jitter_; 00241 00242 }; 00243 00247 class RTPReceiver : public TclObject { 00248 public: 00250 RTPReceiver* next; 00253 RTPReceiver(u_int32_t srcid); 00255 inline u_int32_t srcid() { return (srcid_); } 00257 inline int cum_pkts_lost() { return (cum_pkts_lost_); } 00259 inline double eff_rtt() { return (eff_rtt_); } 00261 inline double rate() { return (rate_); } 00262 inline bool sent_report() { return (sent_report_); } 00263 00265 inline void cum_pkts_lost(int n) { cum_pkts_lost_ += n; } 00267 inline void eff_rtt(double n) { eff_rtt_ = n; } 00269 inline void rate(double n) { rate_ = n; } 00270 void receive_report(receiver_report*); 00271 inline void sent_report(bool n) { sent_report_ = n; } 00272 00273 protected: 00275 u_int32_t srcid_; 00277 int cum_pkts_lost_; 00279 double eff_rtt_; 00281 double rate_; 00282 bool sent_report_; 00283 }; 00284 00285 class RTPSession; 00286 class SenderTimer : public TimerHandler { 00287 public: 00288 SenderTimer(RTPSession *a) : TimerHandler() { a_ = a; } 00289 protected: 00290 virtual void expire(Event *e); 00291 RTPSession *a_; 00292 }; 00293 00294 00295 00300 class RTPSession : public NsObject { 00301 public: 00302 RTPSession(); 00303 ~RTPSession(); 00304 RTPSession* next; 00305 virtual void timeout(int); 00306 /* the receive packet function */ 00307 virtual void recv(Packet* p, Handler*); 00308 /* the receive control packet function */ 00309 virtual void recv_ctrl(Packet* p); 00310 /* connection with TCL commands */ 00311 int command(int argc, const char*const* argv); 00312 /* returns the srcid of the local source */ 00313 inline u_int32_t srcid() { return (localsrc_->srcid()); } 00314 /* builds the RTCP SR and RR */ 00315 int build_report(int bye); 00316 /* icrements by one each time when a RTP packet is sent */ 00317 void localsrc_update(int); 00318 /* updates the trasnmission rate of the RTP sender */ 00319 void update_rate(); 00320 /* increments by packet size (in bytes) each time when a RTP packet is sent */ 00321 void localsrc_update_nbytes(int n); 00322 /* calculates a smooth TCP-friendly rate */ 00323 void smooth_rate(double rate); 00324 RTPSource* allsrcs_; 00325 RTPSource* localsrc_; 00326 RTPReceiver* receivers_; 00327 SimStream* stream_; 00329 int last_np_; 00330 /* we do not implement this function */ 00331 int build_sdes(); 00332 /* we do not implement this function */ 00333 int build_bye(); 00334 /* access the RTP header */ 00335 hdr_rtp* access_hdr_rtp(); 00336 /* for debugging */ 00337 void print_rcvs(); 00339 int enableFlowControl_; 00341 double rx_recv_; 00343 double jitter_; 00345 double smooth_rate_; 00346 void print_srcs(); 00347 SenderTimer sender_timer_; 00348 void start_timer(); 00349 void stop_timer(); 00350 00351 00352 00353 00354 protected: 00355 00356 RTPSource* lookup(u_int32_t); 00357 RTPReceiver* lookup_rcv(u_int32_t); 00359 list<double> lst; 00360 /* add this source */ 00361 void enter(RTPSource*); 00362 /* add this receiver */ 00363 void enter_rcv(RTPReceiver*); 00364 /* calculate RTT */ 00365 void calculate_RTT(); 00366 /* increase rate if packet losses have been not detected since last RR */ 00367 void increase_rate(int a); 00368 /* measure smooth packet loss ratio */ 00369 void measure_smooth_loss (double n); 00370 /* calculates TCP-friendly bandwidth share */ 00371 void calculateR_tcp(int a); 00372 /* the initial session rate */ 00373 void initial_rate(double a); 00375 double T_one_way_; 00377 hdr_rtp* rh_; 00378 /* removes the RTP receiver from the list */ 00379 void remove_receiver(RTPReceiver*); 00381 int we_sent; 00383 double RTT_; 00385 double weight[8]; 00387 double pkt_loss_history[8]; 00389 double smooth_loss_; 00391 int last_pkts_lost_; 00393 int last_ehsr_; 00395 double tx_rate_; 00397 double time_elapsed_; 00399 double last_time_report_; 00401 double alpha; 00402 /* calculates A */ 00403 void calculate_alpha(double value); 00404 /* core function for congestion control */ 00405 void evaluate_jitter(double value); 00407 double smooth_factor; 00409 list<double> jitter_lst; 00411 double z_score; 00412 double low_; 00413 double high_; 00414 void remove_sender(RTPSource*); 00415 int level_; 00416 double avg_tx_; 00417 void setInst_tx(double newValue); 00418 list<double> tx_lst; 00419 void average_tx(); 00420 int running_; 00421 void slow_start(); 00422 00423 00424 00425 00426 00427 00428 00429 }; 00430 00431 class RTPAgent; 00436 class RTPTimer : public TimerHandler { 00437 public: 00438 RTPTimer(RTPAgent *a) : TimerHandler() { a_ = a; } 00439 protected: 00440 virtual void expire(Event *e); 00441 RTPAgent *a_; 00442 }; 00443 00444 class JoinTimer : public TimerHandler { 00445 public: 00446 JoinTimer(RTPAgent *a) : TimerHandler() { a_ = a; } 00447 protected: 00448 virtual void expire(Event *e); 00449 RTPAgent *a_; 00450 }; 00451 00452 00453 00454 00458 class RTPAgent : public Agent { 00459 public: 00460 RTPAgent(); 00461 virtual void timeout(int); 00462 void join_timeout(int); 00463 virtual void recv(Packet* p, Handler*); 00464 virtual int command(int argc, const char*const* argv); 00465 void advanceby(int delta); 00466 virtual void sendmsg(int nbytes, const char *flags = 0); 00467 protected: 00468 virtual void sendpkt(); 00469 virtual void makepkt(Packet*); 00470 void rate_change(); 00471 virtual void start(); 00472 virtual void stop(); 00473 virtual void finish(); 00474 RTPSession* session_; 00475 double lastpkttime_; 00476 int seqno_; 00477 int running_; 00478 int random_; 00479 int maxpkts_; 00480 double interval_; 00481 RTPTimer rtp_timer_; 00482 double timestamp_; 00483 JoinTimer join_timer_; 00484 int join_flag_; 00485 00486 }; 00487 00488 #endif