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 #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     // This is the file we will write to (/dev/audio)
00048     int audiooutput;
00049     // The aforementioned file will be transmitted through this socket
00050     RTPSession *socket;
00051 
00052 public:
00053     // Constructor
00054     ccRTP_AudioReceiver() {
00055         audiooutput=open("/dev/audio",O_WRONLY/*|O_NDELAY*/);
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     // Destructor.
00068     ~ccRTP_AudioReceiver() {
00069         terminate();
00070         delete socket;
00071         ::close(audiooutput);
00072     }
00073 
00074     // This method does almost everything.
00075     void run(void) {
00076         // redefined from Thread.
00077 
00078         // Before using ccRTP you should learn something about other
00079         // CommonC++ classes. We need InetHostAddress...
00080 
00081         // Construct loopback address
00082         InetHostAddress local_ip;
00083         local_ip = "127.0.0.1";
00084 
00085         // Is that correct?
00086         if( ! local_ip ) {
00087         // this is equivalent to `! local_ip.isInetAddress()'
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         // ____Here comes the real RTP stuff____
00097 
00098         // Construct the RTP socket
00099         socket = new RTPSession(local_ip,RECEIVER_BASE,0);
00100 
00101         // Set up receiver's connection
00102         socket->setSchedulingTimeout(10000);
00103         if( !socket->addDestination(local_ip,TRANSMITTER_BASE) )
00104             cerr << "The receiver could not connect.";
00105 
00106         // Let's check the queue (you should read the documentation
00107         // so that you know what the queue is for).
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         // This will be useful for periodic execution.
00118         TimerPort::setTimer(PERIOD);
00119 
00120         // This is the main loop, where packets are sent and receipt.
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             // This is for buffering some packets at the
00133             // receiver side, since playing smoothly
00134             // without any reception buffer is almost
00135             // impossible.  Try commenting the two lines
00136             // below, or stop transmission and continue
00137             // later: you will probably hear noise or
00138             // cracks.
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             // Let's wait for the next cycle
00148             Thread::sleep(TimerPort::getTimer());
00149             TimerPort::incTimer(PERIOD);
00150         }
00151 
00152     } // end of run
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     // Construct the main thread.
00165     ccRTP_AudioReceiver *receiver = new ccRTP_AudioReceiver();
00166 
00167     // Run it.
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 

Generated on 14 Aug 2013 for ccRTP by  doxygen 1.4.7