User Manual, Developers Guide and API Documentation

VideoTelephony.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <APPLICATIONS/session/server/wimax/VideoTelephony.hpp>
00029 
00030 using namespace applications::session::server::wimax;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR(applications::session::server::wimax::VideoTelephony,
00033                      applications::session::Session,
00034                      "server.WiMAXVideoTelephony", wns::PyConfigViewCreator);
00035 
00036 VideoTelephony::VideoTelephony(const wns::pyconfig::View& _pyco) :
00037   Session(_pyco),
00038   stateTransitionDistribution(NULL),
00039   iFramePacketSizeDistribution(NULL),
00040   bFramePacketSizeDistribution(NULL),
00041   pFramePacketSizeDistribution(NULL),
00042   bFrameCounter(1),
00043   gopCounter(1),
00044   newGOP(true),
00045   cnCounter(0)
00046 {
00047   wns::pyconfig::View sTVConfig(_pyco, "stateTransition");
00048   std::string sTVName = sTVConfig.get<std::string>("__plugin__");
00049   stateTransitionDistribution = wns::distribution::DistributionFactory::creator(sTVName)->create(sTVConfig);
00050 
00051   wns::pyconfig::View iPSConfig(_pyco, "iFramePacketSize");
00052   std::string iPSName = iPSConfig.get<std::string>("__plugin__");
00053   iFramePacketSizeDistribution = wns::distribution::DistributionFactory::creator(iPSName)->create(iPSConfig);
00054 
00055   wns::pyconfig::View bPSConfig(_pyco, "bFramePacketSize");
00056   std::string bPSName = bPSConfig.get<std::string>("__plugin__");
00057   bFramePacketSizeDistribution = wns::distribution::DistributionFactory::creator(bPSName)->create(bPSConfig);
00058 
00059   wns::pyconfig::View pPSConfig(_pyco, "pFramePacketSize");
00060   std::string pPSName = pPSConfig.get<std::string>("__plugin__");
00061   pFramePacketSizeDistribution = wns::distribution::DistributionFactory::creator(pPSName)->create(pPSConfig);
00062 
00063   settlingTime = _pyco.get<wns::simulator::Time>("settlingTime");
00064 
00065   voicePacketIat = _pyco.get<wns::simulator::Time>("voicePacketIat");
00066   voicePacketSize = _pyco.get<Bit>("voicePacketSize");
00067   /* Packet size includes 12 bytes RTP header. */
00068   voicePacketSize = voicePacketSize + 96;
00069 
00070   comfortNoisePacketSize = _pyco.get<Bit>("comfortNoisePacketSize");
00071   /* Packet size includes 12 bytes RTP header. */
00072   comfortNoisePacketSize = comfortNoisePacketSize + 96;
00073 
00074   comfortNoise = _pyco.get<bool>("comfortNoise");
00075 
00076   MESSAGE_BEGIN(NORMAL, logger, m, "APPL: Starting new session with voiceparameters: ");
00077   m << "voicePacketSize = " << voicePacketSize;
00078   m << ", voicePacketIat = " << voicePacketIat;
00079   m << ", comfortNoisePacketSize = " << comfortNoisePacketSize;
00080   MESSAGE_END();
00081 
00082   videoFrameRate = _pyco.get<int>("frameRate");
00083   videoPacketIat = 1 / double(videoFrameRate);
00084   shiftI = _pyco.get<double>("shiftI");
00085 
00086   MESSAGE_BEGIN(NORMAL, logger, m, "APPL: Starting new session with videoparameters: ");
00087   m << "videoFrameRate = " << videoFrameRate;
00088   m << ", videoPacketIat = " << videoPacketIat;
00089   MESSAGE_END();
00090 
00091   state = running;
00092 
00093   /* only for probing */
00094   sessionType = wimaxvideotelephony;
00095 
00096   packetFrom = "server.WiMAXVideoTelephony";
00097 }
00098 
00099 
00100 VideoTelephony::~VideoTelephony()
00101 {
00102   if (stateTransitionDistribution != NULL)
00103     delete stateTransitionDistribution;
00104   stateTransitionDistribution = NULL;
00105 
00106   if(iFramePacketSizeDistribution != NULL)
00107     delete iFramePacketSizeDistribution;
00108   iFramePacketSizeDistribution = NULL;
00109 
00110   if(bFramePacketSizeDistribution != NULL)
00111     delete bFramePacketSizeDistribution;
00112   bFramePacketSizeDistribution = NULL;
00113 
00114   if(pFramePacketSizeDistribution != NULL)
00115     delete pFramePacketSizeDistribution;
00116   pFramePacketSizeDistribution = NULL;
00117 }
00118 
00119 void
00120 VideoTelephony::onData(const wns::osi::PDUPtr& _pdu)
00121 {
00122   assureType(_pdu.getPtr(), applications::session::PDU*);
00123 
00124   receivedPacketNumber = static_cast<applications::session::PDU*>(_pdu.getPtr())->getPacketNumber();
00125 
00126   MESSAGE_SINGLE(NORMAL, logger, "APPL: receivedPacketNumber = " << receivedPacketNumber << ".");
00127 
00128   applications::session::Session::incomingProbesCalculation(_pdu);
00129 
00130   if((receivedPacketNumber == 1) && (state != sessionended))
00131     {
00132       MESSAGE_SINGLE(NORMAL, logger, "APPL: Call from client received!");
00133 
00134       firstPacketDelay = packetDelay;
00135 
00136       /* Video */
00137       onTimeout(frametimeout);
00138 
00139       /* Voice */
00140       onTimeout(sendtimeout);
00141       voiceState = inactive;
00142       cnCounter = 1;
00143       setTimeout(statetransitiontimeout, 0.02);
00144     }
00145 }
00146 
00147 void
00148 VideoTelephony::onTimeout(const Timeout& _t)
00149 {
00150   /* Voice */
00151   if(_t == sendtimeout)
00152     {
00153       MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending voicepacket!");
00154 
00155       applications::session::Session::iatProbesCalculation();
00156 
00157       packetSize = voicePacketSize;
00158 
00159       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(voicePacketSize), pyco);
00160       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00161 
00162       ++packetNumber;
00163       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00164       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00165 
00166       wns::osi::PDUPtr pdu(applicationPDU);
00167       applications::session::Session::outgoingProbesCalculation(pdu);
00168 
00169       connection->sendData(pdu);
00170     }
00171   else if(_t == receivetimeout)
00172     {
00173       /* Comfort noise packets will be send to fill the silence */
00174       MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending comfort noise packet!");
00175 
00176       applications::session::Session::iatProbesCalculation();
00177 
00178       packetSize = comfortNoisePacketSize;
00179 
00180       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(comfortNoisePacketSize), pyco);
00181       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00182 
00183       ++packetNumber;
00184       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00185       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00186 
00187       wns::osi::PDUPtr pdu(applicationPDU);
00188       applications::session::Session::outgoingProbesCalculation(pdu);
00189 
00190       connection->sendData(pdu);
00191     }
00192   else if(_t == statetransitiontimeout)
00193     {
00194       /* Transition between active (speech) state and inactive (silent/pause) state. */
00195       double stateTransitionValue = (*stateTransitionDistribution)();
00196 
00197       MESSAGE_SINGLE(NORMAL, logger, "APPL: State transition. TransitionValue = " << stateTransitionValue << ".");
00198 
00199       if(stateTransitionValue <= 0.01 && voiceState == inactive)
00200     {
00201       cnCounter = 0;
00202       onTimeout(sendtimeout);
00203 
00204       voiceState = active;
00205     }
00206       else if(stateTransitionValue > 0.01 && voiceState == inactive)
00207     {
00208       if(cnCounter == 8)
00209         {
00210           cnCounter = 1;
00211           onTimeout(receivetimeout);
00212         }
00213       else
00214         {
00215           cnCounter += 1;
00216         }
00217     }
00218       else if(stateTransitionValue <= 0.01 && voiceState == active)
00219     {
00220       cnCounter = 1;
00221       onTimeout(receivetimeout);
00222 
00223       voiceState = inactive;
00224     }
00225       else if(stateTransitionValue > 0.01 && voiceState == active)
00226     {
00227       onTimeout(sendtimeout);
00228     }
00229 
00230       setTimeout(statetransitiontimeout, 0.02);
00231     }
00232   /* Video */
00233   else if(_t == frametimeout)
00234     {
00235       if(newGOP == false)
00236     {
00237       if(bFrameCounter <= 2)
00238         {
00239           /* B-Frame */
00240           MESSAGE_SINGLE(NORMAL, logger, "APPL: B-FRAME!");
00241 
00242           // applications::session::Session::iatProbesCalculation();
00243 
00244           bFrameCounter += 1;
00245 
00246           packetSize = (*bFramePacketSizeDistribution)();
00247           /* The distributed value is in byte, so it has to be converted to bit. */
00248           packetSize *= 8.0;
00249 
00250           applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00251           applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00252 
00253           ++packetNumber;
00254           applicationPDU->setPacketNumber(packetNumber, packetFrom);
00255           MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00256 
00257           wns::osi::PDUPtr pdu(applicationPDU);
00258           applications::session::Session::outgoingProbesCalculation(pdu);
00259 
00260           connection->sendData(pdu);
00261 
00262           setTimeout(frametimeout, videoPacketIat);
00263 
00264         }
00265       else
00266         {
00267           /* P-Frame */
00268           bFrameCounter = 1;
00269 
00270           if(gopCounter < 4)
00271         {
00272           MESSAGE_SINGLE(NORMAL, logger, "APPL: P-FRAME!");
00273 
00274           // applications::session::Session::iatProbesCalculation();
00275 
00276           packetSize = (*pFramePacketSizeDistribution)();
00277           /* The distributed value is in byte, so it has to be converted to bit. */
00278           packetSize *= 8.0;
00279 
00280           applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00281           applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00282 
00283           ++packetNumber;
00284           applicationPDU->setPacketNumber(packetNumber, packetFrom);
00285           MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00286 
00287           wns::osi::PDUPtr pdu(applicationPDU);
00288           applications::session::Session::outgoingProbesCalculation(pdu);
00289 
00290           connection->sendData(pdu);
00291 
00292 
00293           gopCounter += 1;
00294           setTimeout(frametimeout, videoPacketIat);
00295         }
00296           else
00297         {
00298           gopCounter = 1;
00299           newGOP = true;
00300           onTimeout(frametimeout);
00301         }
00302         }
00303     }
00304       else
00305     {
00306       /* I-Frame */
00307       MESSAGE_SINGLE(NORMAL, logger, "APPL: I-FRAME!");
00308 
00309       // applications::session::Session::iatProbesCalculation();
00310 
00311       packetSize = (*iFramePacketSizeDistribution)();
00312       /* This value has to be shifted. */
00313       packetSize += shiftI;
00314       /* The distributed value is in byte, so it has to be converted to bit. */
00315       packetSize *= 8.0;
00316 
00317       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00318       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00319 
00320       ++packetNumber;
00321       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00322       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00323 
00324       wns::osi::PDUPtr pdu(applicationPDU);
00325       applications::session::Session::outgoingProbesCalculation(pdu);
00326 
00327       connection->sendData(pdu);
00328 
00329       newGOP = false;
00330       setTimeout(frametimeout, videoPacketIat);
00331     }
00332     }
00333   else if(_t == probetimeout)
00334     {
00335       applications::session::Session::onTimeout(_t);
00336     }
00337   else
00338     {
00339       assure(false, "APPL: Unknown timout type =" << _t);
00340     }
00341 }
00342 

Generated on Sun May 27 03:32:16 2012 for openWNS by  doxygen 1.5.5