audiorx.cpp

00001 // audiorx. 
00002 // A simple and amusing program for testing basic features of ccRTP.
00003 // Copyright (C) 2001,2002  Federico Montesino <fedemp@altern.org>
00004 //  
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //  
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //  
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // A very simple mu-law encoded audio player.
00020 
00021 // This is an introductory example file that illustrates basic usage
00022 // of ccRTP. You will also see a bit on how to use CommonC++ threads and
00023 // TimerPort.
00024 
00025 // I am a player of \mu-law encoded RTP audio packets. I 
00026 // do not accept any arguments.
00027 
00028 #include <cstdio>
00029 #include <cstdlib>
00030 // Some consts common to audiorx and audiotx
00031 #include <audio.h>
00032 // In order to use ccRTP, the RTP stack of CommonC++, you only need to
00033 // include ...
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         // This is the file we will write to (/dev/audio)
00049         int audiooutput;
00050         // The aforementioned file will be transmitted through this socket
00051         RTPSession *socket;
00052 
00053 public:
00054         // Constructor
00055         ccRTP_AudioReceiver(){
00056                 audiooutput=open("/dev/audio",O_WRONLY/*|O_NDELAY*/);
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         // Destructor. 
00069         ~ccRTP_AudioReceiver(){
00070                 terminate();
00071                 delete socket;  
00072                 ::close(audiooutput);
00073         }
00074 
00075         // This method does almost everything.
00076         void run(void){    
00077                 // redefined from Thread.
00078                 
00079                 // Before using ccRTP you should learn something about other
00080                 // CommonC++ classes. We need InetHostAddress...
00081 
00082                 // Construct loopback address
00083                 InetHostAddress local_ip;
00084                 local_ip = "127.0.0.1";
00085                 
00086                 // Is that correct?
00087                 if( ! local_ip ){  
00088                 // this is equivalent to `! local_ip.isInetAddress()'
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                 // ____Here comes the real RTP stuff____
00098                 
00099                 // Construct the RTP socket
00100                 socket = new RTPSession(local_ip,RECEIVER_BASE,0);
00101                 
00102                 // Set up receiver's connection
00103                 socket->setSchedulingTimeout(10000);  
00104                 if( !socket->addDestination(local_ip,TRANSMITTER_BASE) )
00105                         cerr << "The receiver could not connect.";
00106                 
00107                 // Let's check the queue (you should read the documentation
00108                 // so that you know what the queue is for).
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                 // This will be useful for periodic execution.
00119                 TimerPort::setTimer(PERIOD);
00120 
00121                 setCancel(cancelImmediate);
00122                 // This is the main loop, where packets are sent and receipt.
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                         // This is for buffering some packets at the
00135                         // receiver side, since playing smoothly
00136                         // without any reception buffer is almost
00137                         // impossible.  Try commenting the two lines
00138                         // below, or stop transmission and continue
00139                         // later: you will probably hear noise or
00140                         // cracks.  
00141                         if (i==0)
00142                                 Thread::sleep(20);
00143                         
00144                         ::write(audiooutput,adu->getData(),adu->getSize());
00145 
00146                         cout << "." << flush;
00147 
00148                         // Let's wait for the next cycle
00149                         Thread::sleep(TimerPort::getTimer());
00150                         TimerPort::incTimer(PERIOD);
00151                 }
00152 
00153         } // end of run
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         // Construct the main thread. 
00166         ccRTP_AudioReceiver *receiver = new ccRTP_AudioReceiver();
00167         
00168         // Run it.
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 

Generated on Mon Oct 23 06:11:55 2006 for ccRTP by  doxygen 1.4.7