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