ccrtptest.cpp

00001 // test ccRTP functionality
00002 // Copyright (C) 2004 Federico Montesino Pouzols <fedemp@altern.org>
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 2 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #include <cstdlib>
00018 #include <ccrtp/rtp.h>
00019 
00020 #ifdef  CCXX_NAMESPACES
00021 using namespace ost;
00022 using namespace std;
00023 #endif
00024 
00025 class PacketsPattern
00026 {
00027 public:
00028     inline const InetHostAddress& getDestinationAddress() const
00029         { return destinationAddress; }
00030 
00031     inline const tpport_t getDestinationPort() const
00032         { return destinationPort; }
00033 
00034     uint32 getPacketsNumber() const
00035         { return packetsNumber; }
00036 
00037     const unsigned char* getPacketData(uint32 i)
00038         { return data; }
00039 
00040     const size_t getPacketSize(uint32 i)
00041         { return packetsSize; }
00042 
00043 private:
00044     static const InetHostAddress destinationAddress;
00045     static const uint16 destinationPort = 34566;
00046     static const uint32 packetsNumber = 100;
00047     static const uint32 packetsSize = 100;
00048     static unsigned char data[65535];
00049 };
00050 
00051 const InetHostAddress PacketsPattern::destinationAddress =
00052     InetHostAddress("localhost");
00053 
00054 unsigned char PacketsPattern::data[65535];
00055 
00056 PacketsPattern pattern;
00057 
00058 class Test
00059 {
00060 public:
00061     virtual int doTest() = 0;
00062 };
00063 
00064 class SendPacketTransmissionTest : public Test, public Thread, public TimerPort
00065 {
00066 public:
00067     void run()
00068     {
00069         doTest();
00070     }
00071 
00072     int doTest()
00073     {
00074         // should be valid?
00075         //RTPSession tx();
00076         RTPSession tx(InetHostAddress("localhost"));
00077         tx.setSchedulingTimeout(10000);
00078         tx.setExpireTimeout(1000000);
00079 
00080         tx.startRunning();
00081 
00082         tx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
00083         if ( !tx.addDestination(pattern.getDestinationAddress(),
00084                     pattern.getDestinationPort()) ) {
00085             return 1;
00086         }
00087 
00088         // 50 packets per second (packet duration of 20ms)
00089         uint32 period = 20;
00090         uint16 inc = tx.getCurrentRTPClockRate()/50;
00091         TimerPort::setTimer(period);
00092         for ( uint32 i = 0; i < pattern.getPacketsNumber(); i++ ) {
00093             tx.putData(i*inc, pattern.getPacketData(i), pattern.getPacketSize(i));
00094             Thread::sleep(TimerPort::getTimer());
00095             TimerPort::incTimer(period);
00096         }
00097         return 0;
00098     }
00099 };
00100 
00101 class RecvPacketTransmissionTest : public Test, public Thread
00102 {
00103 public:
00104     void run()
00105     {
00106         doTest();
00107     }
00108 
00109     int doTest()
00110     {
00111         RTPSession rx(pattern.getDestinationAddress(), pattern.getDestinationPort());
00112 
00113         rx.setSchedulingTimeout(10000);
00114         rx.setExpireTimeout(1000000);
00115 
00116         rx.startRunning();
00117         rx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
00118         // arbitrary number of loops
00119         for ( int i = 0; i < 500 ; i++ ) {
00120             const AppDataUnit* adu;
00121             while ( (adu = rx.getData(rx.getFirstTimestamp())) ) {
00122 
00123                 delete adu;
00124             }
00125             Thread::sleep(7);
00126         }
00127         return 0;
00128     }
00129 };
00130 
00131 class MiscTest : public Test, public Thread, public TimerPort
00132 {
00133     void run()
00134     {
00135         doTest();
00136     }
00137 
00138     int doTest()
00139     {
00140         const uint32 NSESSIONS = 10;
00141         RTPSession rx(pattern.getDestinationAddress(),pattern.getDestinationPort());
00142         RTPSession **tx = new RTPSession* [NSESSIONS];
00143         for ( uint32 i = 0; i < NSESSIONS; i++ ) {
00144             tx[i] = new RTPSession(InetHostAddress("localhost"));
00145         }
00146         for ( uint32 i = 0; i  < NSESSIONS; i++) {
00147             tx[i]->setSchedulingTimeout(10000);
00148             tx[i]->setExpireTimeout(1000000);
00149             tx[i]->setPayloadFormat(StaticPayloadFormat(sptPCMU));
00150             if ( !tx[i]->addDestination(pattern.getDestinationAddress(), pattern.getDestinationPort()) ) {
00151                 return 1;
00152             }
00153         }
00154 
00155         rx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
00156         rx.setSchedulingTimeout(5000);
00157         rx.setExpireTimeout(10000000); // 10 seconds!
00158         rx.startRunning();
00159 
00160         for ( uint32 i = 0; i  < NSESSIONS; i++) {
00161             tx[i]->startRunning();
00162         }
00163         uint32 period = 20;
00164         TimerPort::setTimer(period);
00165         for ( uint32 i = 0; i < pattern.getPacketsNumber(); i++ ) {
00166             if ( i == 70 ) {
00167                 RTPApplication &app = defaultApplication();
00168                 app.setSDESItem(SDESItemTypeCNAME,"foo@bar");
00169             }
00170             for ( uint32 s = 0; s  < NSESSIONS; s++) {
00171             // 50 packets per second (packet duration of 20ms)
00172                 uint16 inc =
00173                     tx[s]->getCurrentRTPClockRate()/50;
00174                 tx[s]->putData(i*inc, pattern.getPacketData(i), pattern.getPacketSize(i));
00175             }
00176             Thread::sleep(TimerPort::getTimer());
00177             TimerPort::incTimer(period);
00178         }
00179 
00180         Thread::sleep(5000);
00181         for ( uint32 i = 0; i < NSESSIONS; i++ ) {
00182             delete tx[i];
00183         }
00184         RTPSession::SyncSourcesIterator it;
00185         cout << "Sources of synchronization:" << endl;
00186         for (it = rx.begin() ; it != rx.end(); it++) {
00187             const SyncSource &s = *it;
00188             cout << s.getID();
00189             if ( s.isSender() )
00190                 cout << " (sender) ";
00191             cout << s.getNetworkAddress() << ":" <<
00192                 s.getControlTransportPort() << "/" <<
00193                 s.getDataTransportPort();
00194             Participant *p = s.getParticipant();
00195             cout << " (" <<
00196                 p->getSDESItem(SDESItemTypeCNAME)
00197                  << ") " << endl;
00198         }
00199         RTPApplication &app = defaultApplication();
00200         RTPApplication::ParticipantsIterator ai;
00201         cout << "Participants:" << endl;
00202         for ( ai = app.begin(); ai != app.end(); ai++ ) {
00203             const Participant &p = *ai;
00204             cout << p.getSDESItem(SDESItemTypeCNAME) << endl;
00205             //cout << p.getPRIVPrefix();
00206         }
00207         delete tx;
00208         return 0;
00209     }
00210 };
00211 
00212 // class TestPacketHeaders { }
00213 // header extension
00214 
00215 // class TestRTCPTransmission { }
00216 
00217 // class TestMiscellaneous { }
00218 
00219 // Things that should be tested:
00220 // extreme values (0 - big) for putData
00221 // segmentation (setMaxSendSegmentSize())
00222 // performance: packets/second (depending on packet size and # of participants)
00223 int main(int argc, char *argv[])
00224 {
00225     int result = 0;
00226     bool send = false;
00227     bool recv = false;
00228 
00229     RecvPacketTransmissionTest *rx;
00230     SendPacketTransmissionTest *tx;
00231 
00232     // accept as parameter if must run as --send or --recv
00233 
00234     // run several tests in parallel threads
00235     if ( send ) {
00236         tx = new SendPacketTransmissionTest();
00237         tx->start();
00238         tx->join();
00239     } else  if ( recv ) {
00240         rx = new RecvPacketTransmissionTest();
00241         rx->start();
00242         rx->join();
00243     } else {
00244         MiscTest m;
00245         m.start();
00246         m.join();
00247     }
00248     exit(result);
00249 }
00250 

Generated on 14 Aug 2013 for ccRTP by  doxygen 1.4.7