Sophie

Sophie

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

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>rtphello.cpp</h1>
<p>
<div class="fragment"><pre><font class="comment">// rtphello</font>
<font class="comment">// A very simple program for testing and illustrating 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 other classes from</font>
<font class="comment">// CommonC++.</font>

<font class="comment">// I am a typical hello world program. I consist of a sender thread,</font>
<font class="comment">// that sends the salutation message on RTP packets; and a receiver</font>
<font class="comment">// thread, that prints the messages. This is a program with an unsual</font>
<font class="comment">// structure, since the sender has no clear periodicity, the receiver</font>
<font class="comment">// is very unreliable, and both are in the same program. Thus, it</font>
<font class="comment">// should not be seen as an example for typical applications but as a</font>
<font class="comment">// test of some functions of ccRTP.</font>

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

<font class="comment">// base ports</font>
<font class="keyword">const</font> <font class="keywordtype">int</font> RECEIVER_BASE = 33634;
<font class="keyword">const</font> <font class="keywordtype">int</font> TRANSMITTER_BASE = 32522;

<font class="comment">// For this example, this is irrelevant. </font>
<font class="keyword">const</font> <font class="keywordtype">int</font> TIMESTAMP_RATE = 90000;

<font class="keyword">class </font>ccRTP_Hello_Rx: <font class="keyword">public</font> <a name="_a0"></a><a class="code" href="class_Thread.html">Thread</a>
{

<font class="keyword">private</font>:
        <font class="comment">// socket to receive packets</font>
        <a name="_a1"></a><a class="code" href="class_T_RTPSocket.html">RTPSocket</a> *socket;
        <font class="comment">// loopback network address</font>
        InetHostAddress local_ip;
        <font class="comment">// identifier of this sender</font>
        uint32 ssrc;
        
<font class="keyword">public</font>:
        ccRTP_Hello_Rx()<font class="keyword"></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>
                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">"Rx: IP address is not correct!"</font> &lt;&lt; endl;
                        exit(1);
                }

                <font class="comment">// create socket for RTP connection and get a random</font>
                <font class="comment">// SSRC identifier</font>
                socket = <font class="keyword">new</font> <a name="a2"></a><a class="code" href="rtp_h.html#a3">RTPSocket</a>(local_ip,RECEIVER_BASE,0);
                ssrc = socket-&gt;getLocalInfo().getID();
        }
        
        ~ccRTP_Hello_Rx()<font class="keyword"></font>{
                cout &lt;&lt; endl &lt;&lt; <font class="stringliteral">"Destroying receiver with ID "</font> &lt;&lt; ssrc;
                Terminate();
                <font class="keyword">delete</font> socket;
                cout &lt;&lt; <font class="stringliteral">"... "</font> &lt;&lt; <font class="stringliteral">"destroyed."</font>;
        }

        <font class="comment">// This method does almost everything.</font>
        <font class="keywordtype">void</font> Run(<font class="keywordtype">void</font>)<font class="keyword"></font>{    

                cout &lt;&lt; <font class="stringliteral">"Hello, "</font> &lt;&lt; socket-&gt;getLocalInfo().getCNAME() 
                     &lt;&lt; <font class="stringliteral">" ..."</font> &lt;&lt; endl;
                <font class="comment">// redefined from Thread.</font>
                <font class="comment">// Set up connection</font>
                socket-&gt;setTimeout(20000);
                socket-&gt;setExpired(3000000);
                <font class="comment">//socket-&gt;UDPTransmit::setTypeOfService(SOCKET_IPTOS_LOWDELAY);</font>
                <font class="keywordflow">if</font>( socket-&gt;<a name="a3"></a><a class="code" href="class_T_RTPSocket.html#a3">Connect</a>(local_ip,TRANSMITTER_BASE) &lt; 0 )
                        cerr &lt;&lt; <font class="stringliteral">"Rx ("</font> &lt;&lt; ssrc 
                             &lt;&lt; <font class="stringliteral">"): could not connect to port."</font> 
                             &lt;&lt; TRANSMITTER_BASE;
                
                <font class="comment">// Let's check the queues  (you should read the documentation</font>
                <font class="comment">// so that you know what the queues are for).</font>
                cout &lt;&lt; <font class="stringliteral">"Rx ("</font> &lt;&lt; ssrc 
                     &lt;&lt; <font class="stringliteral">"): The queue is "</font> 
                     &lt;&lt; ( socket-&gt;<a name="a4"></a><a class="code" href="class_RTPQueue.html#a20">RTPQueue::isActive</a>() ? <font class="stringliteral">""</font> : <font class="stringliteral">"in"</font>) 
                     &lt;&lt; <font class="stringliteral">"active."</font> &lt;&lt; endl;              

                cout &lt;&lt; <font class="stringliteral">"Rx ("</font> &lt;&lt; ssrc
                     &lt;&lt; <font class="stringliteral">"): "</font> &lt;&lt; local_ip.getHostname() 
                     &lt;&lt; <font class="stringliteral">" is waiting for salutes in port "</font>
                     &lt;&lt; RECEIVER_BASE &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;
                
                <font class="comment">// This is the main loop, where packets are received.</font>
                <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i = 0 ; <font class="keyword">true</font> ; i++ ){
                        <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> buffer[100] = <font class="stringliteral">"[empty]"</font>;
                        
                        <font class="comment">// Wait for an RTP packet. [Absolutely unreliable]</font>
                        <font class="keywordflow">while</font> ( !socket-&gt;getPacket(socket-&gt;getFirstTimestamp(),
                                                   buffer,100) )
                        ccxx_sleep(10);
                        
                        <font class="comment">// Print content (likely a salute)</font>
                        time_t receiving_time = time(NULL);
                        <font class="keywordtype">char</font> tmstring[30];
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,localtime(&amp;receiving_time));
                        cout &lt;&lt; <font class="stringliteral">"Rx ("</font> &lt;&lt; ssrc 
                             &lt;&lt; <font class="stringliteral">"): [receiving at "</font> &lt;&lt; tmstring &lt;&lt; <font class="stringliteral">"]: "</font> 
                             &lt;&lt; buffer &lt;&lt; endl;
                }
        }
};

<font class="keyword">class </font>ccRTP_Hello_Tx: <font class="keyword">public</font> <a class="code" href="class_Thread.html">Thread</a>, <font class="keyword">public</font> TimerPort
{

<font class="keyword">private</font>:
        <font class="comment">// socket to transmit</font>
        <a class="code" href="class_T_RTPSocket.html">RTPSocket</a> *socket;
        <font class="comment">// loopback network address</font>
        InetHostAddress local_ip;
        <font class="comment">// identifier of this sender</font>
        uint32 ssrc;

<font class="keyword">public</font>:
        ccRTP_Hello_Tx()<font class="keyword"></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>
                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">"Tx: IP address is not correct!"</font> &lt;&lt; endl;
                        exit(1);
                }
                
                socket = <font class="keyword">new</font> <a class="code" href="rtp_h.html#a3">RTPSocket</a>(local_ip,TRANSMITTER_BASE,0);
                ssrc = socket-&gt;getLocalInfo().getID();
        }

        ~ccRTP_Hello_Tx()<font class="keyword"></font>{
                cout &lt;&lt; endl &lt;&lt; <font class="stringliteral">"Destroying transmitter with ID "</font> &lt;&lt; ssrc;
                Terminate();
                <font class="keyword">delete</font> socket;
                cout &lt;&lt; <font class="stringliteral">"... "</font> &lt;&lt; <font class="stringliteral">"destroyed."</font>;
        }

        <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>
                cout &lt;&lt; <font class="stringliteral">"Tx ("</font> &lt;&lt; ssrc &lt;&lt; <font class="stringliteral">"): "</font> &lt;&lt; local_ip.getHostname() 
                     &lt;&lt; <font class="stringliteral">" is going to salute perself through "</font> 
                     &lt;&lt; local_ip &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;
                
                <font class="comment">// Set up connection</font>
                socket-&gt;setTimeout(20000);
                socket-&gt;setExpired(3000000);
                <font class="keywordflow">if</font>( socket-&gt;<a class="code" href="class_T_RTPSocket.html#a3">Connect</a>(local_ip,RECEIVER_BASE) &lt; 0 )
                        cerr &lt;&lt; <font class="stringliteral">"Tx ("</font> &lt;&lt; ssrc 
                             &lt;&lt; <font class="stringliteral">"): could not connect to port."</font> 
                             &lt;&lt; RECEIVER_BASE;
                
                <font class="comment">// Let's check the queues  (you should read the documentation</font>
                <font class="comment">// so that you know what the queues are for).</font>
                cout &lt;&lt; <font class="stringliteral">"Tx ("</font> &lt;&lt; ssrc &lt;&lt; <font class="stringliteral">"): The queue is "</font>
                     &lt;&lt; ( socket-&gt;<a class="code" href="class_RTPQueue.html#a20">RTPQueue::isActive</a>()? <font class="stringliteral">""</font> : <font class="stringliteral">"in"</font>)
                     &lt;&lt; <font class="stringliteral">"active."</font> &lt;&lt; endl;

                cout &lt;&lt; <font class="stringliteral">"Tx ("</font> &lt;&lt; ssrc &lt;&lt; <font class="stringliteral">"): Transmitting salutes to port "</font>
                     &lt;&lt; RECEIVER_BASE &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;

                uint32 timestamp = 0;
                <font class="comment">//socket-&gt;getCurrentTimestamp(RTP_PAYLOAD_MP2T);</font>
                time_t initial_time = time(NULL);
                <font class="comment">// This will be useful for periodic execution</font>
                TimerPort::setTimer(1000);

                <font class="comment">// This is the main loop, where packets are sent.</font>
                <font class="keywordflow">for</font>( <font class="keywordtype">int</font> i = 0 ; <font class="keyword">true</font> ;){

                        <font class="keyword">const</font> uint32 TIMESTAMP_RATE = 
                                socket-&gt;getRate(RTP_PAYLOAD_MP2T);
                        
                        <font class="comment">// send RTP packets, providing timestamp,</font>
                        <font class="comment">// payload type and payload.  </font>
                        <font class="comment">// construct salute.</font>
                        <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> salute[50];
                        snprintf((<font class="keywordtype">char</font> *)salute,50,
                                 <font class="stringliteral">"Hello, brave gnu world! (no %u)"</font>,i);
                        time_t sending_time = time(NULL);
                        <font class="comment">// get timestamp to send salute</font>
                        <font class="keywordflow">if</font> ( i++ == 0 ){
                                timestamp = socket-&gt;
                                        getCurrentTimestamp(RTP_PAYLOAD_MP2T);
                                
                        } <font class="keywordflow">else</font> {
                                <font class="comment">// increment for 1 second</font>
                                timestamp += socket-&gt;getRate(RTP_PAYLOAD_MP2T);
                        }       
                        socket-&gt;putPacket(timestamp,
                                          RTP_PAYLOAD_MP2T,
                                          salute,
                                          strlen((<font class="keywordtype">char</font> *)salute)+1);
                        <font class="comment">// print info</font>
                        <font class="keywordtype">char</font> tmstring[30];
                        strftime(tmstring,30,<font class="stringliteral">"%X"</font>,
                                 localtime(&amp;sending_time));
                        cout &lt;&lt; <font class="stringliteral">"Tx ("</font> &lt;&lt; ssrc 
                             &lt;&lt; <font class="stringliteral">"): sending salute "</font> &lt;&lt; <font class="stringliteral">"no "</font> &lt;&lt; i++ 
                             &lt;&lt; <font class="stringliteral">", at "</font> &lt;&lt; tmstring 
                             &lt;&lt; <font class="stringliteral">"..."</font> &lt;&lt; endl;

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

<font class="keywordtype">void</font> main(<font class="keywordtype">int</font> argc, <font class="keywordtype">char</font> *argv[])<font class="keyword"></font>{

        <font class="comment">// Construct the two main threads. they will not run yet.</font>
        ccRTP_Hello_Rx *receiver = <font class="keyword">new</font> ccRTP_Hello_Rx;
        ccRTP_Hello_Tx *transmitter = <font class="keyword">new</font> ccRTP_Hello_Tx;
        
        cout &lt;&lt; <font class="stringliteral">"This is rtphello, a very simple test program for ccRTP."</font> &lt;&lt; 
                endl &lt;&lt; <font class="stringliteral">"Strike [Enter] when you are fed up with it."</font> &lt;&lt; endl;

        <font class="comment">// Start execution of hello now.</font>
        receiver-&gt;Start();
        transmitter-&gt;Start();

        cin.get();

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

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