00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <cstdio>
00029 #include <cstdlib>
00030
00031 #include <audio.h>
00032
00033
00034 #include <ccrtp/rtp.h>
00035 #include <fcntl.h>
00036
00037 using namespace ost;
00038 using namespace std;
00039
00040
00041 #ifndef O_NDELAY
00042 #define O_NDELAY 0
00043 #endif
00044
00049 class ccRTP_AudioTransmitter: public Thread, public TimerPort
00050 {
00051 private:
00052
00053
00054 int audioinput;
00055
00056
00057 bool sendingfile;
00058
00059
00060 RTPSession *socket;
00061
00062 public:
00063
00064
00065
00066 ccRTP_AudioTransmitter(char *filename=(char *)"") {
00067
00068 if( !strcmp(filename,"") ) {
00069 filename=(char *)"/dev/audio";
00070 sendingfile = false;
00071 }else{
00072 sendingfile = true;
00073 }
00074
00075 audioinput=open(filename,O_RDONLY|O_NDELAY);
00076
00077 if( audioinput >= 0 ) {
00078 cout << "Ready to transmit " << filename << "." <<endl;
00079 }else{
00080 cout << "I could not open " << filename << "." << endl;
00081 exit();
00082 }
00083
00084 socket=NULL;
00085 }
00086
00087
00088 ~ccRTP_AudioTransmitter() {
00089 terminate();
00090 delete socket;
00091 ::close(audioinput);
00092 }
00093
00094
00095 void run(void) {
00096
00097
00098
00099
00100
00101
00102 InetHostAddress local_ip;
00103 local_ip = "127.0.0.1";
00104
00105
00106 if( ! local_ip ){
00107
00108 cerr << ": IP address is not correct!" << endl;
00109 exit();
00110 }
00111
00112 cout << local_ip.getHostname() <<
00113 " is going to transmit audio to perself through " <<
00114 local_ip << "..." << endl;
00115
00116
00117
00118
00119 socket = new RTPSession(local_ip,TRANSMITTER_BASE);
00120
00121
00122 socket->setSchedulingTimeout(10000);
00123 if( !socket->addDestination(local_ip,RECEIVER_BASE) )
00124 cerr << "I could not connect.";
00125
00126 socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
00127
00128 socket->startRunning();
00129 cout << "The RTP queue service thread is ";
00130 if( socket->isActive() )
00131 cout << "active." << endl;
00132 else
00133 cerr << "not active." << endl;
00134
00135 cout << "Transmitting " << PACKET_SIZE
00136 << " octects long packets "
00137 << "every " << PERIOD << " milliseconds..." << endl;
00138
00139 unsigned char buffer[PACKET_SIZE];
00140 int count=PACKET_SIZE;
00141
00142
00143 TimerPort::setTimer(PERIOD);
00144
00145
00146 for( int i = 0 ; (!sendingfile || count > 0) ; i++ ) {
00147
00148 count = ::read(audioinput,buffer,PACKET_SIZE);
00149 if( count > 0 ) {
00150
00151
00152 socket->putData(PACKET_SIZE*i,buffer,
00153 PACKET_SIZE);
00154 }
00155 cout << "." << flush;
00156
00157
00158 Thread::sleep(TimerPort::getTimer());
00159 TimerPort::incTimer(PERIOD);
00160 }
00161 cout << endl << "I have got no more data to send. " <<endl;
00162 }
00163 };
00164
00165 int main(int argc, char *argv[])
00166 {
00167 cout << "This is audiotx, a simple test program for ccRTP." << endl;
00168 cout << "You should have run audiorx (the server/receiver) before." << endl;
00169 cout << "Strike [Enter] when you are fed up. Enjoy!." << endl;
00170
00171 ccRTP_AudioTransmitter *transmitter;
00172
00173
00174 if ( argc == 2 )
00175 transmitter = new ccRTP_AudioTransmitter(argv[1]);
00176 else
00177 transmitter = new ccRTP_AudioTransmitter();
00178
00179
00180 transmitter->start();
00181
00182 cin.get();
00183
00184 cout << endl << "That's all." << endl;
00185
00186 delete transmitter;
00187
00188 exit(0);
00189 }
00190