Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

session-rtp.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *      This product includes software developed by the MASH Research
00017  *      Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
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         /* XXX We'll get to this later... */
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 /* XXX Should hash this... */
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 }

Generated on Tue Apr 20 12:14:32 2004 for NS2.26SourcesOriginal by doxygen 1.3.3