Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 56ebf73a8cc21535297a85df4640d63b > files > 17

libccrtp0.7-0.7.2-1mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta name="robots" content="noindex">
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Example Documentation</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.5 on Fri Oct 5 10:28:25 2001 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; <a class="qindex" href="examples.html">Examples</a> &nbsp; </center>
<hr><h1>audiotx.cpp</h1>
<p>
<div class="fragment"><pre><font class="comment">// audiotx. </font>
<font class="comment">// A simple and amusing program for testing basic features of ccRTP.</font>
<font class="comment">// Copyright (C) 2001  Federico Montesino &lt;p5087@quintero.fie.us.es&gt;</font>
<font class="comment">//  </font>
<font class="comment">// This program is free software; you can redistribute it and/or modify</font>
<font class="comment">// it under the terms of the GNU General Public License as published by</font>
<font class="comment">// the Free Software Foundation; either version 2 of the License, or</font>
<font class="comment">// (at your option) any later version.</font>
<font class="comment">//  </font>
<font class="comment">// This program is distributed in the hope that it will be useful,</font>
<font class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
<font class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</font>
<font class="comment">// GNU General Public License for more details.</font>
<font class="comment">//  </font>
<font class="comment">// You should have received a copy of the GNU General Public License</font>
<font class="comment">// along with this program; if not, write to the Free Software</font>
<font class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</font>


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

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


<font class="comment">// In order to use ccRTP, the RTP stack of CommonC++, you only need to</font>
<font class="comment">// include ...</font>
<font class="preprocessor">#include &lt;cc++/<a class="code" href="rtp_h.html">rtp.h</a>&gt;</font>

<font class="comment">// Some consts common to audiotx and audiorx</font>
<font class="preprocessor">#include &lt;audio.h&gt;</font>

<font class="keyword">class </font>ccRTP_AudioTransmitter: <font class="keyword">public</font> <a name="_a0"></a><a class="code" href="class_Thread.html">Thread</a>, <font class="keyword">public</font> TimerPort
{
<font class="keyword">private</font>:
        
        <font class="comment">// This is the descriptor of the file we will read from</font>
        <font class="comment">// (commonly, /dev/audio or a .au file)</font>
        <font class="keywordtype">int</font> audioinput;

        <font class="comment">// If we are sending a .au file</font>
        <font class="keywordtype">bool</font> sendingfile;

        <font class="comment">// The aforementioned file will be transmitted through this socket</font>
        <a name="_a1"></a><a class="code" href="class_T_RTPSocket.html">RTPSocket</a> *socket;

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

        <font class="comment">// Destructor. </font>
        ~ccRTP_AudioTransmitter()<font class="keyword"></font>{
                Terminate();
                
                <font class="keywordflow">if</font>( socket )
                        <font class="keyword">delete</font> socket;
                
                close(audioinput);
        }
        
        <font class="comment">// This method does almost everything.</font>
        <font class="keywordtype">void</font> Run(<font class="keywordtype">void</font>)<font class="keyword"></font>{    
                <font class="comment">// redefined from Thread.</font>
                
                <font class="comment">// Before using ccRTP you should learn something about other</font>
                <font class="comment">// CommonC++ classes. We need InetHostAddress...</font>
                
                <font class="comment">// Construct loopback address</font>
                InetHostAddress local_ip;
                local_ip = <font class="stringliteral">"127.0.0.1"</font>;
                
                <font class="comment">// Is that correct?</font>
                <font class="keywordflow">if</font>( ! local_ip ){  
                        <font class="comment">// this is equivalent to `! local_ip.isInetAddress()'</font>
                        cerr &lt;&lt; <font class="stringliteral">": IP address is not correct!"</font> &lt;&lt; endl;
                        exit(1);
                }
                
                cout &lt;&lt; local_ip.getHostname() &lt;&lt; 
                        <font class="stringliteral">" is going to transmit audio to perself through "</font> &lt;&lt;
                        local_ip &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;
                
                <font class="comment">// ____Here comes the real RTP stuff____</font>
                
                <font class="comment">// Construct the RTP socket.</font>
                socket = <font class="keyword">new</font> <a name="a2"></a><a class="code" href="rtp_h.html#a3">RTPSocket</a>(local_ip,TRANSMITTER_BASE,0);
                
                <font class="comment">// Set up connection</font>
                socket-&gt;setTimeout(10000);
                socket-&gt;setExpired(1000000);
                <font class="keywordflow">if</font>( socket-&gt;<a name="a3"></a><a class="code" href="class_T_RTPSocket.html#a3">Connect</a>(local_ip,RECEIVER_BASE) &lt; 0 )
                        cerr &lt;&lt; <font class="stringliteral">"I could not connect."</font>;
                
                <font class="comment">// Let's the queue (you should read the documentation</font>
                <font class="comment">// so that you know what the queue is for).</font>
                cout &lt;&lt; <font class="stringliteral">"The RTP queue is "</font>;
                <font class="keywordflow">if</font>( socket-&gt;<a name="a4"></a><a class="code" href="class_RTPQueue.html#a20">RTPQueue::isActive</a>() )
                        cout &lt;&lt; <font class="stringliteral">"active."</font> &lt;&lt; endl;
                <font class="keywordflow">else</font>
                        cerr &lt;&lt; <font class="stringliteral">"not active."</font> &lt;&lt; endl;
                
                cout &lt;&lt; <font class="stringliteral">"Transmitting "</font> &lt;&lt; PACKET_SIZE 
                     &lt;&lt; <font class="stringliteral">" octects long packets "</font>
                     &lt;&lt; <font class="stringliteral">"every "</font> &lt;&lt; PERIOD &lt;&lt; <font class="stringliteral">" milliseconds..."</font> &lt;&lt; endl;
                
                <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> buffer[PACKET_SIZE];
                <font class="keywordtype">int</font> count=PACKET_SIZE;
                
                <font class="comment">// This will be useful for periodic execution</font>
                TimerPort::setTimer(PERIOD);
                
                setCancel(THREAD_CANCEL_IMMEDIATE);
                <font class="comment">// This is the main loop, where packets are transmitted.</font>
                <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i=0 ; (!sendingfile || count &gt; 0) ; i++ ){
                        
                        count=read(audioinput,buffer,PACKET_SIZE);
                        
                        <font class="keywordflow">if</font>( count &gt; 0 ){
                                
                                <font class="comment">// send an RTP packet, providing timestamp,</font>
                                <font class="comment">// payload type and payload.</font>
                                socket-&gt;putPacket(PACKET_SIZE*i,
                                                  RTP_PAYLOAD_PCMU,
                                                  buffer,
                                                  PACKET_SIZE);
                        }

                        cout &lt;&lt; <font class="stringliteral">"."</font>&lt;&lt;flush;

                        <font class="comment">// Let's wait for the next cycle</font>
                        ccxx_sleep(TimerPort::getTimer());
                        TimerPort::incTimer(PERIOD);
                }

                cout &lt;&lt; endl &lt;&lt; <font class="stringliteral">"I have got no more data to send. "</font> &lt;&lt;endl;
                
        } <font class="comment">// end of Run</font>
};



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


        ccRTP_AudioTransmitter *transmitter;

        <font class="comment">// Construct the main thread. It will not run yet.</font>
        <font class="keywordflow">if</font> ( argc == 2 )
                transmitter = <font class="keyword">new</font> ccRTP_AudioTransmitter(argv[1]);
        <font class="keywordflow">else</font>
                transmitter = <font class="keyword">new</font> ccRTP_AudioTransmitter();
        
        <font class="comment">// Start transmission now.</font>
        transmitter-&gt;Start();
        
        cin.get();

        cout &lt;&lt; endl &lt;&lt; <font class="stringliteral">"That's all."</font> &lt;&lt; endl;

        <font class="keyword">delete</font> transmitter;

        exit(0);
}

</div></pre><hr><address><small>Generated at Fri Oct 5 10:28:25 2001 for ccRTP by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.gif" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.5 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>,
 &copy;&nbsp;1997-2001</small></address>
</body>
</html>