#include <cstdio>
#include <ctime>
#include <ccrtp/rtp.h>
#ifdef CCXX_NAMESPACES
using namespace ost;
using namespace std;
#endif
const int RECEIVER_BASE = 33634;
const int TRANSMITTER_BASE = 32522;
class ccRTP_Hello_Rx: public Thread
{
private:
RTPSession *socket;
InetHostAddress local_ip;
uint32 ssrc;
public:
ccRTP_Hello_Rx(){
local_ip = "127.0.0.1";
if( ! local_ip ){
cerr << "Rx: IP address is not correct!" << endl;
exit();
}
socket = new RTPSession(local_ip,RECEIVER_BASE);
ssrc = socket->getLocalSSRC();
}
~ccRTP_Hello_Rx(){
cout << endl << "Destroying receiver -ID: " << hex
<< (int)ssrc;
terminate();
delete socket;
cout << "... " << "destroyed.";
}
void run(void){
cout << "Hello, " << defaultApplication().
getSDESItem(SDESItemTypeCNAME)
<< " ..." << endl;
socket->setSchedulingTimeout(20000);
socket->setExpireTimeout(3000000);
if( !socket->addDestination(local_ip,TRANSMITTER_BASE) )
cerr << "Rx (" << hex << (int)ssrc
<< "): could not connect to port."
<< TRANSMITTER_BASE;
cout << "Rx (" << hex << (int)ssrc
<< "): " << local_ip.getHostname()
<< " is waiting for salutes in port "
<< RECEIVER_BASE << "..." << endl;
socket->setPayloadFormat(StaticPayloadFormat(sptMP2T));
socket->startRunning();
cout << "Rx (" << hex << (int)ssrc
<< "): The queue is "
<< ( socket->isActive() ? "" : "in")
<< "active." << endl;
for( int i = 0 ; true ; i++ ){
const AppDataUnit *adu = NULL;
while ( NULL == adu ) {
Thread::sleep(10);
adu = socket->getData(socket->getFirstTimestamp());
}
time_t receiving_time = time(NULL);
char tmstring[30];
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "Rx (" << hex << (int)ssrc
<< "): [receiving at " << tmstring << "]: "
<< adu->getData() << endl;
delete adu;
}
}
};
class ccRTP_Hello_Tx: public Thread, public TimerPort
{
private:
RTPSession *socket;
InetHostAddress local_ip;
uint32 ssrc;
public:
ccRTP_Hello_Tx(){
local_ip = "127.0.0.1";
if( ! local_ip ){
cerr << "Tx: IP address is not correct!" << endl;
exit();
}
socket = new RTPSession(local_ip,TRANSMITTER_BASE);
ssrc = socket->getLocalSSRC();
}
~ccRTP_Hello_Tx(){
cout << endl << "Destroying transmitter -ID: " << hex
<< (int)ssrc;
terminate();
delete socket;
cout << "... " << "destroyed.";
}
void run(void){
cout << "Tx (" << hex << (int)ssrc << "): " <<
local_ip.getHostname()
<< " is going to salute perself through "
<< local_ip << "..." << endl;
socket->setSchedulingTimeout(20000);
socket->setExpireTimeout(3000000);
if( !socket->addDestination(local_ip,RECEIVER_BASE) )
cerr << "Tx (" << hex << (int)ssrc
<< "): could not connect to port."
<< RECEIVER_BASE;
cout << "Tx (" << hex << (int)ssrc <<
"): Transmitting salutes to port "
<< RECEIVER_BASE << "..." << endl;
uint32 timestamp = 0;
TimerPort::setTimer(1000);
socket->setPayloadFormat(StaticPayloadFormat(sptMP2T));
socket->startRunning();
cout << "Tx (" << hex << (int)ssrc << "): The queue is "
<< ( socket->isActive()? "" : "in")
<< "active." << endl;
for( int i = 0 ; true ;i++ ){
unsigned char salute[50];
snprintf((char *)salute,50,
"Hello, brave gnu world (#%u)!",i);
time_t sending_time = time(NULL);
if ( 0 == i ){
timestamp = socket->getCurrentTimestamp();
} else {
timestamp += socket->getCurrentRTPClockRate();
}
socket->putData(timestamp,salute,
strlen((char *)salute)+1);
char tmstring[30];
strftime(tmstring,30,"%X",
localtime(&sending_time));
cout << "Tx (" << hex << (int)ssrc
<< "): sending salute " << "no " << dec << i
<< ", at " << tmstring
<< "..." << endl;
Thread::sleep(TimerPort::getTimer());
TimerPort::incTimer(1000);
}
}
};
int main(int argc, char *argv[])
{
ccRTP_Hello_Rx *receiver = new ccRTP_Hello_Rx;
ccRTP_Hello_Tx *transmitter = new ccRTP_Hello_Tx;
cout << "This is rtphello, a very simple test program for ccRTP." <<
endl << "Strike [Enter] when you are fed up with it." << endl;
receiver->start();
transmitter->start();
cin.get();
delete transmitter;
delete receiver;
cout << endl << "That's all." << endl;
return 0;
}