Sophie

Sophie

distrib > PLD > ac > amd64 > media > dist > by-pkgid > 4cd3fba015b84aa8198c12116a5f41ed > files > 23

ccrtp-devel-1.3.6-1.amd64.rpm

<!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&nbsp;Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;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 &lt;fedemp@altern.org&gt;</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 &lt;cstdio&gt;</span>
<span class="preprocessor">#include &lt;cstdlib&gt;</span>
<span class="comment">// Some consts common to audiotx and audiorx</span>
<span class="preprocessor">#include &lt;audio.h&gt;</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 &lt;<a class="code" href="rtp_8h.html">ccrtp/rtp.h</a>&gt;</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 &gt;= 0 ){
                        cout &lt;&lt; <span class="stringliteral">"Ready to transmit "</span> &lt;&lt; filename &lt;&lt; <span class="stringliteral">"."</span> &lt;&lt;endl;
                }<span class="keywordflow">else</span>{
                        cout &lt;&lt; <span class="stringliteral">"I could not open "</span> &lt;&lt; filename &lt;&lt; <span class="stringliteral">"."</span> &lt;&lt; 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 &lt;&lt; <span class="stringliteral">": IP address is not correct!"</span> &lt;&lt; endl;
                        exit();
                }
                
                cout &lt;&lt; local_ip.getHostname() &lt;&lt; 
                        <span class="stringliteral">" is going to transmit audio to perself through "</span> &lt;&lt;
                        local_ip &lt;&lt; <span class="stringliteral">"..."</span> &lt;&lt; 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-&gt;setSchedulingTimeout(10000);
                <span class="keywordflow">if</span>( !socket-&gt;addDestination(local_ip,RECEIVER_BASE) )
                        cerr &lt;&lt; <span class="stringliteral">"I could not connect."</span>;
                
                socket-&gt;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-&gt;startRunning();
                cout &lt;&lt; <span class="stringliteral">"The RTP queue service thread is "</span>;
                <span class="keywordflow">if</span>( socket-&gt;isActive() )
                        cout &lt;&lt; <span class="stringliteral">"active."</span> &lt;&lt; endl;
                <span class="keywordflow">else</span>
                        cerr &lt;&lt; <span class="stringliteral">"not active."</span> &lt;&lt; endl;
                
                cout &lt;&lt; <span class="stringliteral">"Transmitting "</span> &lt;&lt; PACKET_SIZE 
                     &lt;&lt; <span class="stringliteral">" octects long packets "</span>
                     &lt;&lt; <span class="stringliteral">"every "</span> &lt;&lt; PERIOD &lt;&lt; <span class="stringliteral">" milliseconds..."</span> &lt;&lt; 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 &gt; 0) ; i++ ){
                        
                        count = ::read(audioinput,buffer,PACKET_SIZE);
                        <span class="keywordflow">if</span>( count &gt; 0 ){                                
                                <span class="comment">// send an RTP packet, providing timestamp,</span>
                                <span class="comment">// payload type and payload.</span>
                                socket-&gt;putData(PACKET_SIZE*i,buffer,
                                                PACKET_SIZE);
                        }
                        cout &lt;&lt; <span class="stringliteral">"."</span> &lt;&lt; flush;

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



<span class="keywordtype">int</span>
main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]){
        
        cout &lt;&lt; <span class="stringliteral">"This is audiotx, a simple test program for ccRTP."</span> &lt;&lt; endl;
        cout &lt;&lt; <span class="stringliteral">"You should have run audiorx (the server/receiver) before."</span>
             &lt;&lt; endl;
        cout &lt;&lt; <span class="stringliteral">"Strike [Enter] when you are fed up. Enjoy!."</span> &lt;&lt; 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-&gt;start();
        
        cin.get();

        cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">"That's all."</span> &lt;&lt; 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&nbsp;
<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>