ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
audiotx.cpp
Go to the documentation of this file.
1 // audiotx.
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 
20 // This is an introductory example file that illustrates basic usage
21 // of ccRTP. You will also see a bit on how to use CommonC++ threads and
22 // TimerPort.
23 
24 // I am a transmitter of \mu-law encoded RTP audio packets. In order
25 // to hear what I transmit, you should be running my colleague
26 // `audiorx'. You can give me the name of a .au file as argument.
27 
28 #include <cstdio>
29 #include <cstdlib>
30 // Some consts common to audiotx and audiorx
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 #include <fcntl.h>
36 
37 using namespace ost;
38 using namespace std;
39 
40 // TODO: a temporary fix....just to allow building on broken platforms...
41 #ifndef O_NDELAY
42 #define O_NDELAY 0
43 #endif
44 
49 class ccRTP_AudioTransmitter: public Thread
50 {
51 private:
52  // Used to time operations
53  TimerPort timerport;
54  // This is the descriptor of the file we will read from
55  // (commonly, /dev/audio or a .au file)
57 
58  // If we are sending a .au file
60 
61  // The aforementioned file will be transmitted through this socket
63 
64 public:
65  // Constructor. If it is given a file name, this thread will
66  // transmit that file. If it is not, /dev/audio input is
67  // transmitted
68  ccRTP_AudioTransmitter(char *filename=(char *)"") {
69 
70  if( !strcmp(filename,"") ) {
71  filename=(char *)"/dev/audio";
72  sendingfile = false;
73  }else{
74  sendingfile = true;
75  }
76 
77  audioinput=open(filename,O_RDONLY|O_NDELAY);
78 
79  if( audioinput >= 0 ) {
80  cout << "Ready to transmit " << filename << "." <<endl;
81  }else{
82  cout << "I could not open " << filename << "." << endl;
83  exit();
84  }
85 
86  socket=NULL;
87  }
88 
89  // Destructor.
91  terminate();
92  delete socket;
93  ::close(audioinput);
94  }
95 
96  // This method does almost everything.
97  void run(void) {
98  // redefined from Thread.
99 
100  // Before using ccRTP you should learn something about other
101  // CommonC++ classes. We need InetHostAddress...
102 
103  // Construct loopback address
104  InetHostAddress local_ip;
105  local_ip = "127.0.0.1";
106 
107  // Is that correct?
108  if( ! local_ip ){
109  // this is equivalent to `! local_ip.isInetAddress()'
110  cerr << ": IP address is not correct!" << endl;
111  exit();
112  }
113 
114  cout << local_ip.getHostname() <<
115  " is going to transmit audio to perself through " <<
116  local_ip << "..." << endl;
117 
118  // ____Here comes the real RTP stuff____
119 
120  // Construct the RTP socket.
121  socket = new RTPSession(local_ip,TRANSMITTER_BASE);
122 
123  // Set up connection
124  socket->setSchedulingTimeout(10000);
125  if( !socket->addDestination(local_ip,RECEIVER_BASE) )
126  cerr << "I could not connect.";
127 
128  socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));
129 
130  socket->startRunning();
131  cout << "The RTP queue service thread is ";
132  if( socket->isActive() )
133  cout << "active." << endl;
134  else
135  cerr << "not active." << endl;
136 
137  cout << "Transmitting " << PACKET_SIZE
138  << " octects long packets "
139  << "every " << PERIOD << " milliseconds..." << endl;
140 
141  unsigned char buffer[PACKET_SIZE];
142  int count=PACKET_SIZE;
143 
144  // This will be useful for periodic execution
145  timerport.setTimer(PERIOD);
146 
147  // This is the main loop, where packets are transmitted.
148  for( int i = 0 ; (!sendingfile || count > 0) ; i++ ) {
149 
150  count = ::read(audioinput,buffer,PACKET_SIZE);
151  if( count > 0 ) {
152  // send an RTP packet, providing timestamp,
153  // payload type and payload.
154  socket->putData(PACKET_SIZE*i,buffer,
155  PACKET_SIZE);
156  }
157  cout << "." << flush;
158 
159  // Let's wait for the next cycle
160  Thread::sleep(timerport.getTimer());
161  timerport.incTimer(PERIOD);
162  }
163  cout << endl << "I have got no more data to send. " <<endl;
164  }
165 };
166 
167 int main(int argc, char *argv[])
168 {
169  cout << "This is audiotx, a simple test program for ccRTP." << endl;
170  cout << "You should have run audiorx (the server/receiver) before." << endl;
171  cout << "Strike [Enter] when you are fed up. Enjoy!." << endl;
172 
173  ccRTP_AudioTransmitter *transmitter;
174 
175  // Construct the main thread. It will not run yet.
176  if ( argc == 2 )
177  transmitter = new ccRTP_AudioTransmitter(argv[1]);
178  else
179  transmitter = new ccRTP_AudioTransmitter();
180 
181  // Start transmitter thread.
182  transmitter->start();
183 
184  cin.get();
185 
186  cout << endl << "That's all." << endl;
187 
188  delete transmitter;
189 
190  exit(0);
191 }
192 
ITU-T G.711. mu-law audio 8 Khz (RFC 1890)
Definition: formats.h:75
const int PERIOD
Definition: audio.h:37
ccRTP_AudioTransmitter(char *filename=(char *)"")
Definition: audiotx.cpp:68
const int RECEIVER_BASE
file audio.h Common header for audiorx and audiotx.
Definition: audio.h:29
RTPSession * socket
Definition: audiotx.cpp:62
int main(int argc, char *argv[])
Definition: audiotx.cpp:167
const int TRANSMITTER_BASE
Definition: audio.h:30
SingleThreadRTPSession RTPSession
Uses two pairs of sockets for RTP data and RTCP transmission/reception.
Definition: rtp.h:601
This is the class that will do almost everything.
Definition: audiotx.cpp:49
This template class adds the threading aspect to the RTPSessionBase template in one of the many possi...
Definition: rtp.h:418
Static payload format objects.
Definition: formats.h:200
Generic and audio/video profile specific RTP interface of ccRTP.
#define O_NDELAY
Definition: audiotx.cpp:42
const int PACKET_SIZE
Definition: audio.h:40