Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // audiorx. |
| 2 | // A simple and amusing program for testing basic features of ccRTP. |
| 3 | // Copyright (C) 2001,2002 Federico Montesino <fedemp@altern.org> |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | // A very simple mu-law encoded audio player. |
| 20 | |
| 21 | // This is an introductory example file that illustrates basic usage |
| 22 | // of ccRTP. You will also see a bit on how to use CommonC++ threads and |
| 23 | // TimerPort. |
| 24 | |
| 25 | // I am a player of \mu-law encoded RTP audio packets. I |
| 26 | // do not accept any arguments. |
| 27 | |
| 28 | #include <cstdio> |
| 29 | #include <cstdlib> |
| 30 | // Some consts common to audiorx and audiotx |
| 31 | #include <audio.h> |
| 32 | // In order to use ccRTP, the RTP stack of CommonC++, you only need to |
| 33 | // include ... |
| 34 | #include <ccrtp/rtp.h> |
| 35 | |
| 36 | #ifdef CCXX_NAMESPACES |
| 37 | using namespace ost; |
| 38 | using namespace std; |
| 39 | #endif |
| 40 | |
| 41 | /** |
| 42 | * @class ccRTP_AudioReceiver |
| 43 | * This is the class that will do almost everything. |
| 44 | */ |
| 45 | class ccRTP_AudioReceiver: public Thread, public TimerPort |
| 46 | { |
| 47 | private: |
| 48 | // This is the file we will write to (/dev/audio) |
| 49 | int audiooutput; |
| 50 | // The aforementioned file will be transmitted through this socket |
| 51 | RTPSession *socket; |
| 52 | |
| 53 | public: |
| 54 | // Constructor |
| 55 | ccRTP_AudioReceiver() { |
| 56 | audiooutput=open("/dev/audio",O_WRONLY/*|O_NDELAY*/); |
| 57 | |
| 58 | if( audiooutput > 0 ) { |
| 59 | cout << "Audio device is ready to play." << endl; |
| 60 | }else{ |
| 61 | cout << "I could not open /dev/audio " << endl; |
| 62 | exit(); |
| 63 | } |
| 64 | |
| 65 | socket=NULL; |
| 66 | } |
| 67 | |
| 68 | // Destructor. |
| 69 | ~ccRTP_AudioReceiver() { |
| 70 | terminate(); |
| 71 | delete socket; |
| 72 | ::close(audiooutput); |
| 73 | } |
| 74 | |
| 75 | // This method does almost everything. |
| 76 | void run(void) { |
| 77 | // redefined from Thread. |
| 78 | |
| 79 | // Before using ccRTP you should learn something about other |
| 80 | // CommonC++ classes. We need InetHostAddress... |
| 81 | |
| 82 | // Construct loopback address |
| 83 | InetHostAddress local_ip; |
| 84 | local_ip = "127.0.0.1"; |
| 85 | |
| 86 | // Is that correct? |
| 87 | if( ! local_ip ) { |
| 88 | // this is equivalent to `! local_ip.isInetAddress()' |
| 89 | cerr << ": IP address is not correct!" << endl; |
| 90 | exit(); |
| 91 | } |
| 92 | |
| 93 | cout << local_ip.getHostname() << |
| 94 | " is going to listen to perself through " << |
| 95 | local_ip << "..." << endl; |
| 96 | |
| 97 | // ____Here comes the real RTP stuff____ |
| 98 | |
| 99 | // Construct the RTP socket |
| 100 | socket = new RTPSession(local_ip,RECEIVER_BASE,0); |
| 101 | |
| 102 | // Set up receiver's connection |
| 103 | socket->setSchedulingTimeout(10000); |
| 104 | if( !socket->addDestination(local_ip,TRANSMITTER_BASE) ) |
| 105 | cerr << "The receiver could not connect."; |
| 106 | |
| 107 | // Let's check the queue (you should read the documentation |
| 108 | // so that you know what the queue is for). |
| 109 | socket->startRunning(); |
| 110 | cout << "The RTP queue is "; |
| 111 | if( socket->isActive() ) |
| 112 | cout << "active." << endl; |
| 113 | else |
| 114 | cerr << "not active." << endl; |
| 115 | |
| 116 | cout << "Waiting for audio packets..." << endl; |
| 117 | |
| 118 | // This will be useful for periodic execution. |
| 119 | TimerPort::setTimer(PERIOD); |
| 120 | |
| 121 | setCancel(cancelImmediate); |
| 122 | // This is the main loop, where packets are sent and receipt. |
| 123 | socket->setPayloadFormat(StaticPayloadFormat(sptPCMU)); |
| 124 | for( int i=0 ; true ; i++ ) { |
| 125 | const AppDataUnit* adu; |
| 126 | do { |
| 127 | adu = socket->getData(socket->getFirstTimestamp()); |
| 128 | if ( NULL == adu ) |
| 129 | Thread::sleep(5); |
| 130 | else cout << "."; |
| 131 | }while ( (NULL == adu) || (adu->getSize() <= 0) ); |
| 132 | |
| 133 | |
| 134 | // This is for buffering some packets at the |
| 135 | // receiver side, since playing smoothly |
| 136 | // without any reception buffer is almost |
| 137 | // impossible. Try commenting the two lines |
| 138 | // below, or stop transmission and continue |
| 139 | // later: you will probably hear noise or |
| 140 | // cracks. |
| 141 | if (i==0) |
| 142 | Thread::sleep(20); |
| 143 | |
| 144 | if(::write(audiooutput,adu->getData(),adu->getSize()) < (ssize_t)adu->getSize()) |
| 145 | break; |
| 146 | |
| 147 | cout << "." << flush; |
| 148 | |
| 149 | // Let's wait for the next cycle |
| 150 | Thread::sleep(TimerPort::getTimer()); |
| 151 | TimerPort::incTimer(PERIOD); |
| 152 | } |
| 153 | |
| 154 | } // end of run |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | int main(int argc, char *argv[]) |
| 159 | { |
| 160 | cout << "This is audiorx, a simple test program for ccRTP." << endl; |
| 161 | cout << "I am waiting for audio packets on port " << RECEIVER_BASE |
| 162 | << "." << endl; |
| 163 | cout << "Do you want to hear something? Run audiotx." << endl; |
| 164 | cout << "Strike [Enter] when you are fed up. Enjoy!." << endl; |
| 165 | |
| 166 | // Construct the main thread. |
| 167 | ccRTP_AudioReceiver *receiver = new ccRTP_AudioReceiver(); |
| 168 | |
| 169 | // Run it. |
| 170 | receiver->start(); |
| 171 | |
| 172 | cin.get(); |
| 173 | |
| 174 | cout << endl << "That's all." << endl; |
| 175 | |
| 176 | delete receiver; |
| 177 | |
| 178 | exit(0); |
| 179 | } |
| 180 | |
| 181 | /** EMACS ** |
| 182 | * Local variables: |
| 183 | * mode: c++ |
| 184 | * c-basic-offset: 4 |
| 185 | * End: |
| 186 | */ |
| 187 | |
| 188 | |
| 189 | |