File: audiotx.cpp

package info (click to toggle)
libccrtp 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,036 kB
  • ctags: 1,567
  • sloc: cpp: 9,159; sh: 8,774; makefile: 120
file content (202 lines) | stat: -rw-r--r-- 5,276 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// audiotx. 
// A simple and amusing program for testing basic features of ccRTP.
// Copyright (C) 2001,2002  Federico Montesino <fedemp@altern.org>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


// This is an introductory example file that illustrates basic usage
// of ccRTP. You will also see a bit on how to use CommonC++ threads and
// TimerPort.

// I am a transmitter of \mu-law encoded RTP audio packets. In order
// to hear what I transmit, you should be running my colleague
// `audiorx'. You can give me the name of a .au file as argument.

#include <cstdio>
#include <cstdlib>
// Some consts common to audiotx and audiorx
#include <audio.h>
// In order to use ccRTP, the RTP stack of CommonC++, you only need to
// include ...
#include <ccrtp/rtp.h>

#ifdef	CCXX_NAMESPACES
using namespace ost;
using namespace std;
#endif

/**
 * @class ccRTP_AudioTransmitter
 * This is the class that will do almost everything.
 */
class ccRTP_AudioTransmitter: public Thread, public TimerPort
{
private:
	// This is the descriptor of the file we will read from
	// (commonly, /dev/audio or a .au file)
	int audioinput;

	// If we are sending a .au file
	bool sendingfile;

	// The aforementioned file will be transmitted through this socket
	RTPSession *socket;

public:
	// Constructor. If it is given a file name, this thread will
	// transmit that file. If it is not, /dev/audio input is
	// transmitted
	ccRTP_AudioTransmitter(char *filename=""){
		
		if( !strcmp(filename,"") ){
			filename="/dev/audio";
			sendingfile = false;
		}else{
			sendingfile = true;
		}
		
		audioinput=open(filename,O_RDONLY|O_NDELAY);
		
		if( audioinput >= 0 ){
			cout << "Ready to transmit " << filename << "." <<endl;
		}else{
			cout << "I could not open " << filename << "." << endl;
			exit();
		}
		
		socket=NULL;
	}

	// Destructor. 
	~ccRTP_AudioTransmitter(){
		terminate();
		delete socket;		
		::close(audioinput);
	}
	
	// This method does almost everything.
	void run(void) {
		// redefined from Thread.
		
		// Before using ccRTP you should learn something about other
		// CommonC++ classes. We need InetHostAddress...
		
		// Construct loopback address
		InetHostAddress local_ip;
		local_ip = "127.0.0.1";
		
		// Is that correct?
		if( ! local_ip ){  
			// this is equivalent to `! local_ip.isInetAddress()'
			cerr << ": IP address is not correct!" << endl;
			exit();
		}
		
		cout << local_ip.getHostname() << 
			" is going to transmit audio to perself through " <<
			local_ip << "..." << endl;
		
		// ____Here comes the real RTP stuff____
		
		// Construct the RTP socket.
		socket = new RTPSession(local_ip,TRANSMITTER_BASE);
		
		// Set up connection
		socket->setSchedulingTimeout(10000);
		if( !socket->addDestination(local_ip,RECEIVER_BASE) )
			cerr << "I could not connect.";
		
		socket->setPayloadFormat(StaticPayloadFormat(sptPCMU));

		socket->startRunning();
		cout << "The RTP queue service thread is ";
		if( socket->isActive() )
			cout << "active." << endl;
		else
			cerr << "not active." << endl;
		
		cout << "Transmitting " << PACKET_SIZE 
		     << " octects long packets "
		     << "every " << PERIOD << " milliseconds..." << endl;
		
		unsigned char buffer[PACKET_SIZE];
		int count=PACKET_SIZE;
		
		// This will be useful for periodic execution
		TimerPort::setTimer(PERIOD);
		
		setCancel(cancelImmediate);
		// This is the main loop, where packets are transmitted.
		for( int i = 0 ; (!sendingfile || count > 0) ; i++ ){
			
			count = ::read(audioinput,buffer,PACKET_SIZE);
			if( count > 0 ){				
				// send an RTP packet, providing timestamp,
				// payload type and payload.
				socket->putData(PACKET_SIZE*i,buffer,
						PACKET_SIZE);
			}
			cout << "." << flush;

			// Let's wait for the next cycle
			Thread::sleep(TimerPort::getTimer());
			TimerPort::incTimer(PERIOD);
		}
		cout << endl << "I have got no more data to send. " <<endl;
	}
};



int
main(int argc, char *argv[]){
	
	cout << "This is audiotx, a simple test program for ccRTP." << endl;
	cout << "You should have run audiorx (the server/receiver) before."
	     << endl;
	cout << "Strike [Enter] when you are fed up. Enjoy!." << endl; 


	ccRTP_AudioTransmitter *transmitter;

	// Construct the main thread. It will not run yet.
	if ( argc == 2 )
		transmitter = new ccRTP_AudioTransmitter(argv[1]);
	else
		transmitter = new ccRTP_AudioTransmitter();
	
	// Start transmitter thread.
	transmitter->start();
	
	cin.get();

	cout << endl << "That's all." << endl;

	delete transmitter;

	exit(0);
}

/** EMACS **
 * Local variables:
 * mode: c++
 * c-basic-offset: 8
 * End:
 */