ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ccrtptest.cpp
Go to the documentation of this file.
1 // test ccRTP functionality
2 // Copyright (C) 2004-2015 Federico Montesino Pouzols <fedemp@altern.org>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <cstdlib>
18 #include <ccrtp/rtp.h>
19 
20 #ifdef CCXX_NAMESPACES
21 using namespace ost;
22 using namespace std;
23 #endif
24 
26 {
27 public:
28  inline const InetHostAddress& getDestinationAddress() const
29  { return destinationAddress; }
30 
31  inline const tpport_t getDestinationPort() const
32  { return destinationPort; }
33 
34  uint32 getPacketsNumber() const
35  { return packetsNumber; }
36 
37  const unsigned char* getPacketData(uint32 i)
38  { return data; }
39 
40  const size_t getPacketSize(uint32 i)
41  { return packetsSize; }
42 
43 private:
44  static const InetHostAddress destinationAddress;
45  static const uint16 destinationPort = 34566;
46  static const uint32 packetsNumber = 100;
47  static const uint32 packetsSize = 100;
48  static unsigned char data[65535];
49 };
50 
51 const InetHostAddress PacketsPattern::destinationAddress =
52  InetHostAddress("localhost");
53 
54 unsigned char PacketsPattern::data[65535];
55 
57 
58 class Test
59 {
60 public:
61  virtual int doTest() = 0;
62 };
63 
64 class SendPacketTransmissionTest : public Test, public Thread, public TimerPort
65 {
66 public:
67  void run()
68  {
69  doTest();
70  }
71 
72  int doTest()
73  {
74  // should be valid?
75  //RTPSession tx();
76  RTPSession tx(InetHostAddress("localhost"));
77  tx.setSchedulingTimeout(10000);
78  tx.setExpireTimeout(1000000);
79 
80  tx.startRunning();
81 
82  tx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
83  if ( !tx.addDestination(pattern.getDestinationAddress(),
85  return 1;
86  }
87 
88  // 50 packets per second (packet duration of 20ms)
89  uint32 period = 20;
90  uint16 inc = tx.getCurrentRTPClockRate()/50;
91  TimerPort::setTimer(period);
92  for ( uint32 i = 0; i < pattern.getPacketsNumber(); i++ ) {
93  tx.putData(i*inc, pattern.getPacketData(i), pattern.getPacketSize(i));
94  Thread::sleep(TimerPort::getTimer());
95  TimerPort::incTimer(period);
96  }
97  return 0;
98  }
99 };
100 
101 class RecvPacketTransmissionTest : public Test, public Thread
102 {
103 public:
104  void run()
105  {
106  doTest();
107  }
108 
109  int doTest()
110  {
112 
113  rx.setSchedulingTimeout(10000);
114  rx.setExpireTimeout(1000000);
115 
116  rx.startRunning();
117  rx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
118  // arbitrary number of loops
119  for ( int i = 0; i < 500 ; i++ ) {
120  const AppDataUnit* adu;
121  while ( (adu = rx.getData(rx.getFirstTimestamp())) ) {
122 
123  delete adu;
124  }
125  Thread::sleep(7);
126  }
127  return 0;
128  }
129 };
130 
131 class MiscTest : public Test, public Thread, public TimerPort
132 {
133  void run()
134  {
135  doTest();
136  }
137 
138  int doTest()
139  {
140  const uint32 NSESSIONS = 10;
142  RTPSession **tx = new RTPSession* [NSESSIONS];
143  for ( uint32 i = 0; i < NSESSIONS; i++ ) {
144  tx[i] = new RTPSession(InetHostAddress("localhost"));
145  }
146  for ( uint32 i = 0; i < NSESSIONS; i++) {
147  tx[i]->setSchedulingTimeout(10000);
148  tx[i]->setExpireTimeout(1000000);
149  tx[i]->setPayloadFormat(StaticPayloadFormat(sptPCMU));
150  if ( !tx[i]->addDestination(pattern.getDestinationAddress(), pattern.getDestinationPort()) ) {
151  return 1;
152  }
153  }
154 
155  rx.setPayloadFormat(StaticPayloadFormat(sptPCMU));
156  rx.setSchedulingTimeout(5000);
157  rx.setExpireTimeout(10000000); // 10 seconds!
158  rx.startRunning();
159 
160  for ( uint32 i = 0; i < NSESSIONS; i++) {
161  tx[i]->startRunning();
162  }
163  uint32 period = 20;
164  TimerPort::setTimer(period);
165  for ( uint32 i = 0; i < pattern.getPacketsNumber(); i++ ) {
166  if ( i == 70 ) {
168  app.setSDESItem(SDESItemTypeCNAME,"foo@bar");
169  }
170  for ( uint32 s = 0; s < NSESSIONS; s++) {
171  // 50 packets per second (packet duration of 20ms)
172  uint16 inc =
173  tx[s]->getCurrentRTPClockRate()/50;
174  tx[s]->putData(i*inc, pattern.getPacketData(i), pattern.getPacketSize(i));
175  }
176  Thread::sleep(TimerPort::getTimer());
177  TimerPort::incTimer(period);
178  }
179 
180  Thread::sleep(5000);
181  for ( uint32 i = 0; i < NSESSIONS; i++ ) {
182  delete tx[i];
183  }
184  RTPSession::SyncSourcesIterator it;
185  cout << "Sources of synchronization:" << endl;
186  for (it = rx.begin() ; it != rx.end(); it++) {
187  const SyncSource &s = *it;
188  cout << s.getID();
189  if ( s.isSender() )
190  cout << " (sender) ";
191  cout << s.getNetworkAddress() << ":" <<
192  s.getControlTransportPort() << "/" <<
194  Participant *p = s.getParticipant();
195  cout << " (" <<
197  << ") " << endl;
198  }
201  cout << "Participants:" << endl;
202  for ( ai = app.begin(); ai != app.end(); ai++ ) {
203  const Participant &p = *ai;
204  cout << p.getSDESItem(SDESItemTypeCNAME) << endl;
205  //cout << p.getPRIVPrefix();
206  }
207  delete[] tx;
208  return 0;
209  }
210 };
211 
212 // class TestPacketHeaders { }
213 // header extension
214 
215 // class TestRTCPTransmission { }
216 
217 // class TestMiscellaneous { }
218 
219 // Things that should be tested:
220 // extreme values (0 - big) for putData
221 // segmentation (setMaxSendSegmentSize())
222 // performance: packets/second (depending on packet size and # of participants)
223 int main(int argc, char *argv[])
224 {
225  int result = 0;
226  bool send = false;
227  bool recv = false;
228 
229  // accept as parameter if must run as --send or --recv
230  for (int i = 1; i < argc; ++i) {
231  send |= !strcmp(argv[i], "-s") or !strcmp(argv[i], "--send");
232  if ( send )
233  break;
234  recv |= !strcmp(argv[i], "-r") or !strcmp(argv[i], "--recv");
235  if ( recv )
236  break;
237  }
238 
239  // run several tests in parallel threads
240  if ( send ) {
242  tx.start();
243  tx.join();
244  } else if ( recv ) {
246  rx.start();
247  rx.join();
248  } else {
249  MiscTest m;
250  m.start();
251  m.join();
252  }
253  exit(result);
254 }
255 
const size_t getPacketSize(uint32 i)
Definition: ccrtptest.cpp:40
ITU-T G.711. mu-law audio 8 Khz (RFC 1890)
Definition: formats.h:75
tpport_t getDataTransportPort() const
Definition: sources.h:271
An RTP application, holding identifying RTCP SDES item values.
Definition: sources.h:364
Synchronization source in an RTP session.
Definition: sources.h:192
Interface (envelope) to data received over RTP packets.
Definition: queuebase.h:68
Canonical end-point identifier.
Definition: rtcppkt.h:67
ParticipantsIterator begin()
Definition: sources.h:445
uint32 getPacketsNumber() const
Definition: ccrtptest.cpp:34
const InetHostAddress & getDestinationAddress() const
Definition: ccrtptest.cpp:28
A class of objects representing remote participants (RTP applications) in a multimedia session...
Definition: sources.h:124
ParticipantsIterator end()
Definition: sources.h:448
PacketsPattern pattern
Definition: ccrtptest.cpp:56
void startRunning()
Activate stack and start service thread.
Definition: rtp.h:508
SingleThreadRTPSession RTPSession
Uses two pairs of sockets for RTP data and RTCP transmission/reception.
Definition: rtp.h:601
Participant * getParticipant() const
Get the participant this synchronization source is asociated to.
Definition: sources.h:268
const tpport_t getDestinationPort() const
Definition: ccrtptest.cpp:31
void setSDESItem(SDESItemType item, const std::string &val)
Definition: sources.h:382
int doTest()
Definition: ccrtptest.cpp:138
const InetAddress & getNetworkAddress() const
Definition: sources.h:277
Iterator through the list of participants in this session.
Definition: sources.h:401
tpport_t getControlTransportPort() const
Definition: sources.h:274
const unsigned char * getPacketData(uint32 i)
Definition: ccrtptest.cpp:37
bool isSender() const
Whether this source sends RTP data packets.
Definition: sources.h:254
This template class adds the threading aspect to the RTPSessionBase template in one of the many possi...
Definition: rtp.h:418
static const InetHostAddress destinationAddress
Definition: ccrtptest.cpp:44
Static payload format objects.
Definition: formats.h:200
void run()
Definition: ccrtptest.cpp:133
const uint8 *const getData() const
Get data as it is received in RTP packets (i.e.
Definition: queuebase.h:105
uint32 getID() const
Definition: sources.h:257
Generic and audio/video profile specific RTP interface of ccRTP.
static unsigned char data[65535]
Definition: ccrtptest.cpp:48
__EXPORT RTPApplication & defaultApplication()
Get the RTPApplication object for the "default" application (the only one used by common applications...
Definition: source.cpp:128
const std::string & getSDESItem(SDESItemType type) const
Get the value of an SDES item.
Definition: sources.h:140
int main(int argc, char *argv[])
Definition: ccrtptest.cpp:223