#include <cc++/rtp.h>
const int RECEIVER_BASE = 33634;
const int TRANSMITTER_BASE = 32522;
const int TIMESTAMP_RATE = 90000;
class ccRTP_Hello_Rx: public Thread
{
private:
RTPSocket *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(1);
}
socket = new RTPSocket(local_ip,RECEIVER_BASE,0);
ssrc = socket->getLocalInfo().getID();
}
~ccRTP_Hello_Rx(){
cout << endl << "Destroying receiver with ID " << ssrc;
Terminate();
delete socket;
cout << "... " << "destroyed.";
}
void Run(void){
cout << "Hello, " << socket->getLocalInfo().getCNAME()
<< " ..." << endl;
socket->setTimeout(20000);
socket->setExpired(3000000);
if( socket->Connect(local_ip,TRANSMITTER_BASE) < 0 )
cerr << "Rx (" << ssrc
<< "): could not connect to port."
<< TRANSMITTER_BASE;
cout << "Rx (" << ssrc
<< "): The queue is "
<< ( socket->RTPQueue::isActive() ? "" : "in")
<< "active." << endl;
cout << "Rx (" << ssrc
<< "): " << local_ip.getHostname()
<< " is waiting for salutes in port "
<< RECEIVER_BASE << "..." << endl;
for( int i = 0 ; true ; i++ ){
unsigned char buffer[100] = "[empty]";
while ( !socket->getPacket(socket->getFirstTimestamp(),
buffer,100) )
ccxx_sleep(10);
time_t receiving_time = time(NULL);
char tmstring[30];
strftime(tmstring,30,"%X",localtime(&receiving_time));
cout << "Rx (" << ssrc
<< "): [receiving at " << tmstring << "]: "
<< buffer << endl;
}
}
};
class ccRTP_Hello_Tx: public Thread, public TimerPort
{
private:
RTPSocket *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(1);
}
socket = new RTPSocket(local_ip,TRANSMITTER_BASE,0);
ssrc = socket->getLocalInfo().getID();
}
~ccRTP_Hello_Tx(){
cout << endl << "Destroying transmitter with ID " << ssrc;
Terminate();
delete socket;
cout << "... " << "destroyed.";
}
void Run(void){
cout << "Tx (" << ssrc << "): " << local_ip.getHostname()
<< " is going to salute perself through "
<< local_ip << "..." << endl;
socket->setTimeout(20000);
socket->setExpired(3000000);
if( socket->Connect(local_ip,RECEIVER_BASE) < 0 )
cerr << "Tx (" << ssrc
<< "): could not connect to port."
<< RECEIVER_BASE;
cout << "Tx (" << ssrc << "): The queue is "
<< ( socket->RTPQueue::isActive()? "" : "in")
<< "active." << endl;
cout << "Tx (" << ssrc << "): Transmitting salutes to port "
<< RECEIVER_BASE << "..." << endl;
uint32 timestamp = 0;
time_t initial_time = time(NULL);
TimerPort::setTimer(1000);
for( int i = 0 ; true ;){
const uint32 TIMESTAMP_RATE =
socket->getRate(RTP_PAYLOAD_MP2T);
unsigned char salute[50];
snprintf((char *)salute,50,
"Hello, brave gnu world! (no %u)",i);
time_t sending_time = time(NULL);
if ( i++ == 0 ){
timestamp = socket->
getCurrentTimestamp(RTP_PAYLOAD_MP2T);
} else {
timestamp += socket->getRate(RTP_PAYLOAD_MP2T);
}
socket->putPacket(timestamp,
RTP_PAYLOAD_MP2T,
salute,
strlen((char *)salute)+1);
char tmstring[30];
strftime(tmstring,30,"%X",
localtime(&sending_time));
cout << "Tx (" << ssrc
<< "): sending salute " << "no " << i++
<< ", at " << tmstring
<< "..." << endl;
ccxx_sleep(TimerPort::getTimer());
TimerPort::incTimer(1000);
}
}
};
void 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 receiver;
delete transmitter;
cout << endl << "That's all" << endl;
exit(0);
}