<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>ccRTP: audiotx.cpp</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.4.4 --> <div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <a class="qindex" href="examples.html">Examples</a></div> <h1>audiotx.cpp</h1><div class="fragment"><pre class="fragment"><span class="comment">// audiotx. </span> <span class="comment">// A simple and amusing program for testing basic features of ccRTP.</span> <span class="comment">// Copyright (C) 2001,2002 Federico Montesino <fedemp@altern.org></span> <span class="comment">// </span> <span class="comment">// This program is free software; you can redistribute it and/or modify</span> <span class="comment">// it under the terms of the GNU General Public License as published by</span> <span class="comment">// the Free Software Foundation; either version 2 of the License, or</span> <span class="comment">// (at your option) any later version.</span> <span class="comment">// </span> <span class="comment">// This program is distributed in the hope that it will be useful,</span> <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <span class="comment">// GNU General Public License for more details.</span> <span class="comment">// </span> <span class="comment">// You should have received a copy of the GNU General Public License</span> <span class="comment">// along with this program; if not, write to the Free Software</span> <span class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span> <span class="comment">// This is an introductory example file that illustrates basic usage</span> <span class="comment">// of ccRTP. You will also see a bit on how to use CommonC++ threads and</span> <span class="comment">// TimerPort.</span> <span class="comment">// I am a transmitter of \mu-law encoded RTP audio packets. In order</span> <span class="comment">// to hear what I transmit, you should be running my colleague</span> <span class="comment">// `audiorx'. You can give me the name of a .au file as argument.</span> <span class="preprocessor">#include <cstdio></span> <span class="preprocessor">#include <cstdlib></span> <span class="comment">// Some consts common to audiotx and audiorx</span> <span class="preprocessor">#include <audio.h></span> <span class="comment">// In order to use ccRTP, the RTP stack of CommonC++, you only need to</span> <span class="comment">// include ...</span> <span class="preprocessor">#include <<a class="code" href="rtp_8h.html">ccrtp/rtp.h</a>></span> <span class="preprocessor">#ifdef CCXX_NAMESPACES</span> <span class="preprocessor"></span><span class="keyword">using</span> <span class="keyword">namespace </span>ost; <span class="keyword">using</span> <span class="keyword">namespace </span>std; <span class="preprocessor">#endif</span> <span class="preprocessor"></span> <span class="keyword">class </span>ccRTP_AudioTransmitter: <span class="keyword">public</span> Thread, <span class="keyword">public</span> TimerPort { <span class="keyword">private</span>: <span class="comment">// This is the descriptor of the file we will read from</span> <span class="comment">// (commonly, /dev/audio or a .au file)</span> <span class="keywordtype">int</span> audioinput; <span class="comment">// If we are sending a .au file</span> <span class="keywordtype">bool</span> sendingfile; <span class="comment">// The aforementioned file will be transmitted through this socket</span> <a name="_a7"></a><a class="code" href="class_single_thread_r_t_p_session.html">RTPSession</a> *socket; <span class="keyword">public</span>: <span class="comment">// Constructor. If it is given a file name, this thread will</span> <span class="comment">// transmit that file. If it is not, /dev/audio input is</span> <span class="comment">// transmitted</span> ccRTP_AudioTransmitter(<span class="keywordtype">char</span> *filename=<span class="stringliteral">""</span>){ <span class="keywordflow">if</span>( !strcmp(filename,<span class="stringliteral">""</span>) ){ filename=<span class="stringliteral">"/dev/audio"</span>; sendingfile = <span class="keyword">false</span>; }<span class="keywordflow">else</span>{ sendingfile = <span class="keyword">true</span>; } audioinput=open(filename,O_RDONLY|O_NDELAY); <span class="keywordflow">if</span>( audioinput >= 0 ){ cout << <span class="stringliteral">"Ready to transmit "</span> << filename << <span class="stringliteral">"."</span> <<endl; }<span class="keywordflow">else</span>{ cout << <span class="stringliteral">"I could not open "</span> << filename << <span class="stringliteral">"."</span> << endl; exit(); } socket=NULL; } <span class="comment">// Destructor. </span> ~ccRTP_AudioTransmitter(){ terminate(); <span class="keyword">delete</span> socket; ::close(audioinput); } <span class="comment">// This method does almost everything.</span> <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>) { <span class="comment">// redefined from Thread.</span> <span class="comment">// Before using ccRTP you should learn something about other</span> <span class="comment">// CommonC++ classes. We need InetHostAddress...</span> <span class="comment">// Construct loopback address</span> InetHostAddress local_ip; local_ip = <span class="stringliteral">"127.0.0.1"</span>; <span class="comment">// Is that correct?</span> <span class="keywordflow">if</span>( ! local_ip ){ <span class="comment">// this is equivalent to `! local_ip.isInetAddress()'</span> cerr << <span class="stringliteral">": IP address is not correct!"</span> << endl; exit(); } cout << local_ip.getHostname() << <span class="stringliteral">" is going to transmit audio to perself through "</span> << local_ip << <span class="stringliteral">"..."</span> << endl; <span class="comment">// ____Here comes the real RTP stuff____</span> <span class="comment">// Construct the RTP socket.</span> socket = <span class="keyword">new</span> <a name="a8"></a><a class="code" href="group__sessions.html#ga0">RTPSession</a>(local_ip,TRANSMITTER_BASE); <span class="comment">// Set up connection</span> socket->setSchedulingTimeout(10000); <span class="keywordflow">if</span>( !socket->addDestination(local_ip,RECEIVER_BASE) ) cerr << <span class="stringliteral">"I could not connect."</span>; socket->setPayloadFormat(<a name="_a9"></a><a class="code" href="class_static_payload_format.html">StaticPayloadFormat</a>(<a name="a10"></a><a class="code" href="group__payload.html#gga2a2">sptPCMU</a>)); socket->startRunning(); cout << <span class="stringliteral">"The RTP queue service thread is "</span>; <span class="keywordflow">if</span>( socket->isActive() ) cout << <span class="stringliteral">"active."</span> << endl; <span class="keywordflow">else</span> cerr << <span class="stringliteral">"not active."</span> << endl; cout << <span class="stringliteral">"Transmitting "</span> << PACKET_SIZE << <span class="stringliteral">" octects long packets "</span> << <span class="stringliteral">"every "</span> << PERIOD << <span class="stringliteral">" milliseconds..."</span> << endl; <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> buffer[PACKET_SIZE]; <span class="keywordtype">int</span> count=PACKET_SIZE; <span class="comment">// This will be useful for periodic execution</span> TimerPort::setTimer(PERIOD); setCancel(cancelImmediate); <span class="comment">// This is the main loop, where packets are transmitted.</span> <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0 ; (!sendingfile || count > 0) ; i++ ){ count = ::read(audioinput,buffer,PACKET_SIZE); <span class="keywordflow">if</span>( count > 0 ){ <span class="comment">// send an RTP packet, providing timestamp,</span> <span class="comment">// payload type and payload.</span> socket->putData(PACKET_SIZE*i,buffer, PACKET_SIZE); } cout << <span class="stringliteral">"."</span> << flush; <span class="comment">// Let's wait for the next cycle</span> Thread::sleep(TimerPort::getTimer()); TimerPort::incTimer(PERIOD); } cout << endl << <span class="stringliteral">"I have got no more data to send. "</span> <<endl; } }; <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]){ cout << <span class="stringliteral">"This is audiotx, a simple test program for ccRTP."</span> << endl; cout << <span class="stringliteral">"You should have run audiorx (the server/receiver) before."</span> << endl; cout << <span class="stringliteral">"Strike [Enter] when you are fed up. Enjoy!."</span> << endl; ccRTP_AudioTransmitter *transmitter; <span class="comment">// Construct the main thread. It will not run yet.</span> <span class="keywordflow">if</span> ( argc == 2 ) transmitter = <span class="keyword">new</span> ccRTP_AudioTransmitter(argv[1]); <span class="keywordflow">else</span> transmitter = <span class="keyword">new</span> ccRTP_AudioTransmitter(); <span class="comment">// Start transmitter thread.</span> transmitter->start(); cin.get(); cout << endl << <span class="stringliteral">"That's all."</span> << endl; <span class="keyword">delete</span> transmitter; exit(0); } </pre></div> <hr size="1"><address style="align: right;"><small>Generated on Fri Dec 9 22:32:03 2005 for ccRTP by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address> </body> </html>