audiotx.cpp
#include <cstdio>
#include <cstdlib>
#include <audio.h>
#include <ccrtp/rtp.h>
#ifdef CCXX_NAMESPACES
using namespace ost;
using namespace std;
#endif
class ccRTP_AudioTransmitter: public Thread, public TimerPort
{
private:
int audioinput;
bool sendingfile;
RTPSession *socket;
public:
ccRTP_AudioTransmitter(char *filename=""){
if( !strcmp(filename,"") ){
filename="/dev/audio";
sendingfile = false;
}else{
sendingfile = true;
}
audioinput=open(filename,O_RDONLY|O_NDELAY);
if( audioinput >= 0 ){
cout << "Ready to transmit " << filename << "." <<endl;
}else{
cout << "I could not open " << filename << "." << endl;
exit();
}
socket=NULL;
}
~ccRTP_AudioTransmitter(){
terminate();
delete socket;
::close(audioinput);
}
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 transmit audio to perself through " <<
local_ip << "..." << endl;
socket = new RTPSession(local_ip,TRANSMITTER_BASE);
socket->setSchedulingTimeout(10000);
if( !socket->addDestination(local_ip,RECEIVER_BASE) )
cerr << "I could not connect.";
socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
socket->startRunning();
cout << "The RTP queue service thread is ";
if( socket->isActive() )
cout << "active." << endl;
else
cerr << "not active." << endl;
cout << "Transmitting " << PACKET_SIZE
<< " octects long packets "
<< "every " << PERIOD << " milliseconds..." << endl;
unsigned char buffer[PACKET_SIZE];
int count=PACKET_SIZE;
TimerPort::setTimer(PERIOD);
setCancel(cancelImmediate);
for( int i = 0 ; (!sendingfile || count > 0) ; i++ ){
count = ::read(audioinput,buffer,PACKET_SIZE);
if( count > 0 ){
socket->putData(PACKET_SIZE*i,buffer,
PACKET_SIZE);
}
cout << "." << flush;
Thread::sleep(TimerPort::getTimer());
TimerPort::incTimer(PERIOD);
}
cout << endl << "I have got no more data to send. " <<endl;
}
};
int
main(int argc, char *argv[]){
cout << "This is audiotx, a simple test program for ccRTP." << endl;
cout << "You should have run audiorx (the server/receiver) before."
<< endl;
cout << "Strike [Enter] when you are fed up. Enjoy!." << endl;
ccRTP_AudioTransmitter *transmitter;
if ( argc == 2 )
transmitter = new ccRTP_AudioTransmitter(argv[1]);
else
transmitter = new ccRTP_AudioTransmitter();
transmitter->start();
cin.get();
cout << endl << "That's all." << endl;
delete transmitter;
exit(0);
}