00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef lint
00036 static const char rcsid[] =
00037 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/common/session-rtp.cc,v 1.13 2000/09/01 03:04:07 haoboy Exp $";
00038 #endif
00039
00040 #include <stdlib.h>
00041 #include "packet.h"
00042 #include "ip.h"
00043 #include "rtp.h"
00044
00045 static class RTPSourceClass : public TclClass {
00046 public:
00047 RTPSourceClass() : TclClass("RTPSource") {}
00048 TclObject* create(int argc, const char*const* argv) {
00049 if (argc >= 5)
00050 return (new RTPSource(atoi(argv[4])));
00051
00052 return 0;
00053 }
00054 } class_rtp_source;
00055
00056 static class RTPSessionClass : public TclClass {
00057 public:
00058 RTPSessionClass() : TclClass("Session/RTP") {}
00059 TclObject* create(int, const char*const*) {
00060 return (new RTPSession());
00061 }
00062 } class_rtp_session;
00063
00064 RTPSession::RTPSession()
00065 : allsrcs_(0), localsrc_(0), last_np_(0)
00066 {
00067 }
00068
00069 RTPSession::~RTPSession()
00070 {
00071 while (allsrcs_ != 0) {
00072 RTPSource* p = allsrcs_;
00073 allsrcs_ = allsrcs_->next;
00074 delete p;
00075 }
00076 delete localsrc_;
00077 }
00078
00079 void RTPSession::localsrc_update(int)
00080 {
00081 localsrc_->np(1);
00082 }
00083
00084 #define RTCP_HDRSIZE 8
00085 #define RTCP_SR_SIZE 20
00086 #define RTCP_RR_SIZE 48
00087
00088 int RTPSession::build_report(int bye)
00089 {
00090 int nsrc = 0;
00091 int nrr = 0;
00092 int len = RTCP_HDRSIZE;
00093 int we_sent = 0;
00094 if (localsrc_->np() != last_np_) {
00095 last_np_ = localsrc_->np();
00096 we_sent = 1;
00097 len += RTCP_SR_SIZE;
00098 }
00099 for (RTPSource* sp = allsrcs_; sp != 0; sp = sp->next) {
00100 ++nsrc;
00101 int received = sp->np() - sp->snp();
00102 if (received == 0) {
00103 continue;
00104 }
00105 sp->snp(sp->np());
00106 len += RTCP_RR_SIZE;
00107 if (++nrr >= 31)
00108 break;
00109 }
00110
00111 if (bye)
00112 len += build_bye();
00113 else
00114 len += build_sdes();
00115
00116 Tcl::instance().evalf("%s adapt-timer %d %d %d", name(),
00117 nsrc, nrr, we_sent);
00118 Tcl::instance().evalf("%s sample-size %d", name(), len);
00119
00120 return (len);
00121 }
00122
00123 int RTPSession::build_bye()
00124 {
00125 return (8);
00126 }
00127
00128 int RTPSession::build_sdes()
00129 {
00130
00131 return (20);
00132 }
00133
00134 void RTPSession::recv(Packet* p, Handler*)
00135 {
00136 hdr_rtp *rh = hdr_rtp::access(p);
00137 u_int32_t srcid = rh->srcid();
00138 RTPSource* s = lookup(srcid);
00139 if (s == 0) {
00140 Tcl& tcl = Tcl::instance();
00141 tcl.evalf("%s new-source %d", name(), srcid);
00142 s = (RTPSource*)TclObject::lookup(tcl.result());
00143 }
00144 s->np(1);
00145 s->ehsr(rh->seqno());
00146 Packet::free(p);
00147 }
00148
00149 void RTPSession::recv_ctrl(Packet* p)
00150 {
00151 hdr_cmn* ch = hdr_cmn::access(p);
00152 Tcl::instance().evalf("%s sample-size %d", name(), ch->size());
00153 Packet::free(p);
00154 }
00155
00156
00157 RTPSource* RTPSession::lookup(u_int32_t srcid)
00158 {
00159 RTPSource *p;
00160 for (p = allsrcs_; p != 0; p = p->next)
00161 if (p->srcid() == srcid)
00162 return (p);
00163
00164 return (0);
00165 }
00166
00167 void RTPSession::enter(RTPSource* s)
00168 {
00169 s->next = allsrcs_;
00170 allsrcs_ = s;
00171 }
00172
00173 int RTPSession::command(int argc, const char*const* argv)
00174 {
00175 if (argc == 3) {
00176 if (strcmp(argv[1], "enter") == 0) {
00177 RTPSource* s = (RTPSource*)TclObject::lookup(argv[2]);
00178 enter(s);
00179 return (TCL_OK);
00180 }
00181 if (strcmp(argv[1], "localsrc") == 0) {
00182 localsrc_ = (RTPSource*)TclObject::lookup(argv[2]);
00183 enter(localsrc_);
00184 return (TCL_OK);
00185 }
00186 }
00187 return (TclObject::command(argc, argv));
00188 }
00189
00190 RTPSource::RTPSource(u_int32_t srcid)
00191 : next(0), np_(0), snp_(0), ehsr_(-1)
00192 {
00193 bind("srcid_", (int*)&srcid_);
00194 srcid_ = srcid;
00195 }