Sophie

Sophie

distrib > Fedora > 14 > i386 > by-pkgid > 598246a3099b0106d22dd5249478d3b7 > files > 25

ccrtp-devel-1.7.1-2.fc12.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>ccRTP: audiorx.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.9 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>audiorx.cpp</h1><div class="fragment"><pre class="fragment"><span class="comment">// audiorx. </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">// A very simple mu-law encoded audio player.</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 player of \mu-law encoded RTP audio packets. I </span>
<span class="comment">// do not accept any arguments.</span>

<span class="preprocessor">#include &lt;cstdio&gt;</span>
<span class="preprocessor">#include &lt;cstdlib&gt;</span>
<span class="comment">// Some consts common to audiorx and audiotx</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" title="Generic and audio/video profile specific RTP interface of ccRTP.">ccrtp/rtp.h</a>&gt;</span>

<span class="preprocessor">#ifdef  CCXX_NAMESPACES</span>
<span class="preprocessor"></span><span class="keyword">using namespace </span>ost;
<span class="keyword">using namespace </span>std;
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keyword">class </span>ccRTP_AudioReceiver: <span class="keyword">public</span> Thread, <span class="keyword">public</span> TimerPort
{
<span class="keyword">private</span>:
        <span class="comment">// This is the file we will write to (/dev/audio)</span>
        <span class="keywordtype">int</span> audiooutput;
        <span class="comment">// The aforementioned file will be transmitted through this socket</span>
        <a name="_a0"></a><a class="code" href="class_single_thread_r_t_p_session.html" title="This template class adds the threading aspect to the RTPSessionBase template in one...">RTPSession</a> *socket;

<span class="keyword">public</span>:
        <span class="comment">// Constructor</span>
        ccRTP_AudioReceiver(){
                audiooutput=open(<span class="stringliteral">"/dev/audio"</span>,O_WRONLY<span class="comment">/*|O_NDELAY*/</span>);

                <span class="keywordflow">if</span>( audiooutput &gt; 0 ){
                        cout &lt;&lt; <span class="stringliteral">"Audio device is ready to play."</span> &lt;&lt; endl;
                }<span class="keywordflow">else</span>{
                        cout &lt;&lt; <span class="stringliteral">"I could not open /dev/audio "</span> &lt;&lt; endl;
                        exit();
                }

                socket=NULL;
        }
        
        <span class="comment">// Destructor. </span>
        ~ccRTP_AudioReceiver(){
                terminate();
                <span class="keyword">delete</span> socket;  
                ::close(audiooutput);
        }

        <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 listen 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="a1"></a><a class="code" href="group__sessions.html#g24e16f350c9ae0c1e2a3f5d6a6dad9f9" title="Uses two pairs of sockets for RTP data and RTCP transmission/reception.">RTPSession</a>(local_ip,RECEIVER_BASE,0);
                
                <span class="comment">// Set up receiver's connection</span>
                socket-&gt;setSchedulingTimeout(10000);  
                <span class="keywordflow">if</span>( !socket-&gt;addDestination(local_ip,TRANSMITTER_BASE) )
                        cerr &lt;&lt; <span class="stringliteral">"The receiver could not connect."</span>;
                
                <span class="comment">// Let's check the queue (you should read the documentation</span>
                <span class="comment">// so that you know what the queue is for).</span>
                socket-&gt;startRunning();
                cout &lt;&lt; <span class="stringliteral">"The RTP queue 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">"Waiting for audio packets..."</span> &lt;&lt; endl;
                
                <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 sent and receipt.</span>
                socket-&gt;setPayloadFormat(<a name="_a2"></a><a class="code" href="class_static_payload_format.html" title="Static payload format objects.">StaticPayloadFormat</a>(<a name="a3"></a><a class="code" href="group__payload.html#gg7644623ce29b8ca519d7e3f276b6ddc6095b083465fe4d6597ba586e0116022c" title="ITU-T G.711. mu-law audio 8 Khz (RFC 1890).">sptPCMU</a>));
                <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i=0 ; true ; i++ ){
                        <span class="keyword">const</span> <a name="_a4"></a><a class="code" href="class_app_data_unit.html" title="Interface (envelope) to data received over RTP packets.">AppDataUnit</a>* adu;
                        <span class="keywordflow">do</span>{
                                adu = socket-&gt;<a name="a5"></a><a class="code" href="class_app_data_unit.html#be15787e1cc84dd03b0769a5546d7a38" title="Get data as it is received in RTP packets (i.e.">getData</a>(socket-&gt;getFirstTimestamp());
                                <span class="keywordflow">if</span> ( NULL == adu )
                                        Thread::sleep(5);
                                <span class="keywordflow">else</span> cout &lt;&lt; <span class="stringliteral">"."</span>;
                        }<span class="keywordflow">while</span> ( (NULL == adu) || (adu-&gt;<a name="a6"></a><a class="code" href="class_app_data_unit.html#f41ffb3a12208e4ec9f6aa1b1180eedd">getSize</a>() &lt;= 0) );

                        
                        <span class="comment">// This is for buffering some packets at the</span>
                        <span class="comment">// receiver side, since playing smoothly</span>
                        <span class="comment">// without any reception buffer is almost</span>
                        <span class="comment">// impossible.  Try commenting the two lines</span>
                        <span class="comment">// below, or stop transmission and continue</span>
                        <span class="comment">// later: you will probably hear noise or</span>
                        <span class="comment">// cracks.  </span>
                        <span class="keywordflow">if</span> (i==0)
                                Thread::sleep(20);
                        
                        <span class="keywordflow">if</span>(::write(audiooutput,adu-&gt;<a class="code" href="class_app_data_unit.html#be15787e1cc84dd03b0769a5546d7a38" title="Get data as it is received in RTP packets (i.e.">getData</a>(),adu-&gt;<a class="code" href="class_app_data_unit.html#f41ffb3a12208e4ec9f6aa1b1180eedd">getSize</a>()) &lt; (ssize_t)adu-&gt;<a class="code" href="class_app_data_unit.html#f41ffb3a12208e4ec9f6aa1b1180eedd">getSize</a>())
                                <span class="keywordflow">break</span>;

                        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);
                }

        } <span class="comment">// end of run</span>
};


<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 audiorx, a simple test program for ccRTP."</span> &lt;&lt; endl;
        cout &lt;&lt; <span class="stringliteral">"I am waiting for audio packets on port "</span> &lt;&lt; RECEIVER_BASE
             &lt;&lt; <span class="stringliteral">"."</span> &lt;&lt; endl;
        cout &lt;&lt; <span class="stringliteral">"Do you want to hear something? Run audiotx."</span> &lt;&lt; endl;
        cout &lt;&lt; <span class="stringliteral">"Strike [Enter] when you are fed up. Enjoy!."</span> &lt;&lt; endl; 

        <span class="comment">// Construct the main thread. </span>
        ccRTP_AudioReceiver *receiver = <span class="keyword">new</span> ccRTP_AudioReceiver();
        
        <span class="comment">// Run it.</span>
        receiver-&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> receiver;

        exit(0);
}

</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>Generated on Fri Jul 24 21:42:25 2009 for ccRTP by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>
</body>
</html>