#include <cstdio>
#include <cstdlib>
#include <ccrtp/ext.h>
#ifdef CCXX_NAMESPACES
using namespace ost;
using namespace std;
#endif
class ccRTP_dupHello: public Thread
{
private:
RTPDuplex *duplexA, *duplexB;
public:
~ccRTP_dupHello(){
terminate();
delete duplexA;
delete duplexB;
}
ccRTP_dupHello() : duplexA(NULL), duplexB(NULL)
{ }
void run(void){
InetHostAddress local_ip;
local_ip = "127.0.0.1";
if( ! local_ip ){
cerr << ": IP address is not correct!" << endl;
exit();
}
cout << local_ip.getHostname() <<
" is going to talk to perself through " <<
local_ip << "..." << endl;
const int A_BASE = 22222;
const int B_BASE = 33334;
duplexA = new RTPDuplex(local_ip,A_BASE,B_BASE);
duplexB = new RTPDuplex(local_ip,B_BASE,A_BASE);
duplexA->setSchedulingTimeout(90000);
duplexA->setExpireTimeout(2500000);
if( duplexA->connect(local_ip,B_BASE) < 0 )
cerr << "Duplex A could not connect.";
duplexB->setSchedulingTimeout(160000);
duplexB->setExpireTimeout(3500000);
if( duplexB->connect(local_ip,A_BASE) < 0 )
cerr << "Duplex B could not connect.";
if( duplexA->RTPDataQueue::isActive() )
cout << "The queue A is active." << endl;
else
cerr << "The queue A is not active." << endl;
if( duplexB->RTPDataQueue::isActive() )
cout << "The queue B is active." << endl;
else
cerr << "The queue B is not active." << endl;
cout << "Transmitting..." << endl;
unsigned char helloA[] = "Hello, brave gnu world from A!";
unsigned char helloB[] = "Hello, brave gnu world from B!";
time_t sending_time;
time_t receiving_time;
char tmstring[30];
StaticPayloadFormat pf = sptMP2T;
duplexA->setPayloadFormat(pf);
duplexB->setPayloadFormat(pf);
for( int i = 0 ; true ; i++ ){
sending_time = time(NULL);
duplexA->putData(2*(i)*90000,helloA,
strlen((char *)helloA));
strftime(tmstring,30,"%X",localtime(&sending_time));
cout << "A: sending message at " << tmstring << "..."
<< endl;
receiving_time = time(NULL);
const AppDataUnit* aduA =
duplexA->getData(duplexA->getFirstTimestamp());
if ( aduA ) {
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "A:[receiving at " << tmstring << "]: " <<
aduA->getData() << endl;
}
Thread::sleep(100);
sending_time = time(NULL);
duplexB->putData(2*(i)*90000,helloB,
strlen((char *)helloB));
strftime(tmstring,30,"%X",localtime(&sending_time));
cout << "B: sending message at " << tmstring << "..."
<< endl;
receiving_time = time(NULL);
const AppDataUnit* aduB =
duplexB->getData(duplexB->getFirstTimestamp());
if ( aduB ) {
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "B:[receiving at " << tmstring << "]: " <<
aduB->getData() << endl;
}
Thread::sleep(1900);
}
}
};
int main(int argc, char *argv[])
{
ccRTP_dupHello *hello = new ccRTP_dupHello;
cout << "This is rtpduphello, a very simple test program for ccRTP."
<< endl << "Strike [Enter] when you are fed up." << endl;
hello->start();
cin.get();
cout << endl << "That's all" << endl;
delete hello;
exit(0);
}