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
00036 #ifdef CCXX_NAMESPACES
00037 using namespace ost;
00038 using namespace std;
00039 #endif
00040
00045 class ccRTP_AudioTransmitter: public Thread, public TimerPort
00046 {
00047 private:
00048
00049
00050 int audioinput;
00051
00052
00053 bool sendingfile;
00054
00055
00056 RTPSession *socket;
00057
00058 public:
00059
00060
00061
00062 ccRTP_AudioTransmitter(char *filename=""){
00063
00064 if( !strcmp(filename,"") ){
00065 filename="/dev/audio";
00066 sendingfile = false;
00067 }else{
00068 sendingfile = true;
00069 }
00070
00071 audioinput=open(filename,O_RDONLY|O_NDELAY);
00072
00073 if( audioinput >= 0 ){
00074 cout << "Ready to transmit " << filename << "." <<endl;
00075 }else{
00076 cout << "I could not open " << filename << "." << endl;
00077 exit();
00078 }
00079
00080 socket=NULL;
00081 }
00082
00083
00084 ~ccRTP_AudioTransmitter(){
00085 terminate();
00086 delete socket;
00087 ::close(audioinput);
00088 }
00089
00090
00091 void run(void) {
00092
00093
00094
00095
00096
00097
00098 InetHostAddress local_ip;
00099 local_ip = "127.0.0.1";
00100
00101
00102 if( ! local_ip ){
00103
00104 cerr << ": IP address is not correct!" << endl;
00105 exit();
00106 }
00107
00108 cout << local_ip.getHostname() <<
00109 " is going to transmit audio to perself through " <<
00110 local_ip << "..." << endl;
00111
00112
00113
00114
00115 socket = new RTPSession(local_ip,TRANSMITTER_BASE);
00116
00117
00118 socket->setSchedulingTimeout(10000);
00119 if( !socket->addDestination(local_ip,RECEIVER_BASE) )
00120 cerr << "I could not connect.";
00121
00122 socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
00123
00124 socket->startRunning();
00125 cout << "The RTP queue service thread is ";
00126 if( socket->isActive() )
00127 cout << "active." << endl;
00128 else
00129 cerr << "not active." << endl;
00130
00131 cout << "Transmitting " << PACKET_SIZE
00132 << " octects long packets "
00133 << "every " << PERIOD << " milliseconds..." << endl;
00134
00135 unsigned char buffer[PACKET_SIZE];
00136 int count=PACKET_SIZE;
00137
00138
00139 TimerPort::setTimer(PERIOD);
00140
00141 setCancel(cancelImmediate);
00142
00143 for( int i = 0 ; (!sendingfile || count > 0) ; i++ ){
00144
00145 count = ::read(audioinput,buffer,PACKET_SIZE);
00146 if( count > 0 ){
00147
00148
00149 socket->putData(PACKET_SIZE*i,buffer,
00150 PACKET_SIZE);
00151 }
00152 cout << "." << flush;
00153
00154
00155 Thread::sleep(TimerPort::getTimer());
00156 TimerPort::incTimer(PERIOD);
00157 }
00158 cout << endl << "I have got no more data to send. " <<endl;
00159 }
00160 };
00161
00162
00163
00164 int
00165 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."
00169 << endl;
00170 cout << "Strike [Enter] when you are fed up. Enjoy!." << endl;
00171
00172
00173 ccRTP_AudioTransmitter *transmitter;
00174
00175
00176 if ( argc == 2 )
00177 transmitter = new ccRTP_AudioTransmitter(argv[1]);
00178 else
00179 transmitter = new ccRTP_AudioTransmitter();
00180
00181
00182 transmitter->start();
00183
00184 cin.get();
00185
00186 cout << endl << "That's all." << endl;
00187
00188 delete transmitter;
00189
00190 exit(0);
00191 }
00192