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 COMMONCPP_NAMESPACE;
00038 using namespace std;
00039
00044 class ccRTP_AudioReceiver: public Thread, public TimerPort
00045 {
00046 private:
00047
00048 int audiooutput;
00049
00050 RTPSession *socket;
00051
00052 public:
00053
00054 ccRTP_AudioReceiver() {
00055 audiooutput=open("/dev/audio",O_WRONLY);
00056
00057 if( audiooutput > 0 ) {
00058 cout << "Audio device is ready to play." << endl;
00059 }else{
00060 cout << "I could not open /dev/audio " << endl;
00061 exit();
00062 }
00063
00064 socket=NULL;
00065 }
00066
00067
00068 ~ccRTP_AudioReceiver() {
00069 terminate();
00070 delete socket;
00071 ::close(audiooutput);
00072 }
00073
00074
00075 void run(void) {
00076
00077
00078
00079
00080
00081
00082 InetHostAddress local_ip;
00083 local_ip = "127.0.0.1";
00084
00085
00086 if( ! local_ip ) {
00087
00088 cerr << ": IP address is not correct!" << endl;
00089 exit();
00090 }
00091
00092 cout << local_ip.getHostname() <<
00093 " is going to listen to perself through " <<
00094 local_ip << "..." << endl;
00095
00096
00097
00098
00099 socket = new RTPSession(local_ip,RECEIVER_BASE,0);
00100
00101
00102 socket->setSchedulingTimeout(10000);
00103 if( !socket->addDestination(local_ip,TRANSMITTER_BASE) )
00104 cerr << "The receiver could not connect.";
00105
00106
00107
00108 socket->startRunning();
00109 cout << "The RTP queue is ";
00110 if( socket->isActive() )
00111 cout << "active." << endl;
00112 else
00113 cerr << "not active." << endl;
00114
00115 cout << "Waiting for audio packets..." << endl;
00116
00117
00118 TimerPort::setTimer(PERIOD);
00119
00120
00121 socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
00122 for( int i=0 ; true ; i++ ) {
00123 const AppDataUnit* adu;
00124 do {
00125 adu = socket->getData(socket->getFirstTimestamp());
00126 if ( NULL == adu )
00127 Thread::sleep(5);
00128 else cout << ".";
00129 }while ( (NULL == adu) || (adu->getSize() <= 0) );
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 if (i==0)
00140 Thread::sleep(20);
00141
00142 if(::write(audiooutput,adu->getData(),adu->getSize()) < (ssize_t)adu->getSize())
00143 break;
00144
00145 cout << "." << flush;
00146
00147
00148 Thread::sleep(TimerPort::getTimer());
00149 TimerPort::incTimer(PERIOD);
00150 }
00151
00152 }
00153 };
00154
00155
00156 int main(int argc, char *argv[])
00157 {
00158 cout << "This is audiorx, a simple test program for ccRTP." << endl;
00159 cout << "I am waiting for audio packets on port " << RECEIVER_BASE
00160 << "." << endl;
00161 cout << "Do you want to hear something? Run audiotx." << endl;
00162 cout << "Strike [Enter] when you are fed up. Enjoy!." << endl;
00163
00164
00165 ccRTP_AudioReceiver *receiver = new ccRTP_AudioReceiver();
00166
00167
00168 receiver->start();
00169
00170 cin.get();
00171
00172 cout << endl << "That's all." << endl;
00173
00174 delete receiver;
00175
00176 exit(0);
00177 }
00178