Sophie

Sophie

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

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: rtphello.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>rtphello.cpp</h1>A basic example of how to use <a class="el" href="class_r_t_p_packet.html">RTPPacket</a><p>
<div class="fragment"><pre class="fragment"><span class="comment">// rtphello</span>
<span class="comment">// A very simple program for testing and illustrating 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 other classes from</span>
<span class="comment">// CommonC++.</span>

<span class="comment">// I am a typical hello world program. I consist of a sender thread,</span>
<span class="comment">// that sends the salutation message on RTP packets; and a receiver</span>
<span class="comment">// thread, that prints the messages. This is a program with an unsual</span>
<span class="comment">// structure, the receiver just tries to process the first available</span>
<span class="comment">// packet periodically, and both are in the same program. Thus, it</span>
<span class="comment">// should not be seen as an example for typical applications but as a</span>
<span class="comment">// test of some functions of ccRTP.</span>

<span class="preprocessor">#include &lt;cstdio&gt;</span>
<span class="preprocessor">#include &lt;ctime&gt;</span>
<span class="comment">// In order to use ccRTP, the RTP stack of CommonC++, just 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="comment">// base ports</span>
<span class="keyword">const</span> <span class="keywordtype">int</span> RECEIVER_BASE = 33634;
<span class="keyword">const</span> <span class="keywordtype">int</span> TRANSMITTER_BASE = 32522;

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

<span class="keyword">class </span>ccRTP_Hello_Rx: <span class="keyword">public</span> Thread
{

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

                <span class="comment">// create socket for RTP connection and get a random</span>
                <span class="comment">// SSRC identifier</span>
                socket = <span class="keyword">new</span> <a name="a39"></a><a class="code" href="group__sessions.html#ga0">RTPSession</a>(local_ip,RECEIVER_BASE);
                ssrc = socket-&gt;getLocalSSRC();
        }
        
        ~ccRTP_Hello_Rx(){
                cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">"Destroying receiver -ID: "</span> &lt;&lt; hex
                     &lt;&lt; (int)ssrc;
                terminate();
                <span class="keyword">delete</span> socket;
                cout &lt;&lt; <span class="stringliteral">"... "</span> &lt;&lt; <span class="stringliteral">"destroyed."</span>;
        }

        <span class="comment">// This method does almost everything.</span>
        <span class="keywordtype">void</span> run(<span class="keywordtype">void</span>){    

                cout &lt;&lt; <span class="stringliteral">"Hello, "</span> &lt;&lt; <a name="a40"></a><a class="code" href="group__sources.html#ga0">defaultApplication</a>().
                        getSDESItem(<a name="a41"></a><a class="code" href="group__rtcppacket.html#gga2a1">SDESItemTypeCNAME</a>)
                     &lt;&lt; <span class="stringliteral">" ..."</span> &lt;&lt; endl;
                <span class="comment">// redefined from Thread.</span>
                <span class="comment">// Set up connection</span>
                socket-&gt;setSchedulingTimeout(20000);
                socket-&gt;setExpireTimeout(3000000);
                <span class="comment">//socket-&gt;UDPTransmit::setTypeOfService(SOCKET_IPTOS_LOWDELAY);</span>
                <span class="keywordflow">if</span>( !socket-&gt;addDestination(local_ip,TRANSMITTER_BASE) )
                        cerr &lt;&lt; <span class="stringliteral">"Rx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc 
                             &lt;&lt; <span class="stringliteral">"): could not connect to port."</span> 
                             &lt;&lt; TRANSMITTER_BASE;
                
                cout &lt;&lt; <span class="stringliteral">"Rx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc
                     &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; local_ip.getHostname() 
                     &lt;&lt; <span class="stringliteral">" is waiting for salutes in port "</span>
                     &lt;&lt; RECEIVER_BASE &lt;&lt; <span class="stringliteral">"..."</span> &lt;&lt; endl;
                
                socket-&gt;setPayloadFormat(<a name="_a42"></a><a class="code" href="class_static_payload_format.html">StaticPayloadFormat</a>(<a name="a43"></a><a class="code" href="group__payload.html#gga2a26">sptMP2T</a>));
                socket-&gt;startRunning();
                <span class="comment">// Let's check the queues  (you should read the documentation</span>
                <span class="comment">// so that you know what the queues are for).</span>
                cout &lt;&lt; <span class="stringliteral">"Rx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc 
                     &lt;&lt; <span class="stringliteral">"): The queue is "</span> 
                     &lt;&lt; ( socket-&gt;isActive() ? <span class="stringliteral">""</span> : <span class="stringliteral">"in"</span>) 
                     &lt;&lt; <span class="stringliteral">"active."</span> &lt;&lt; endl;              

                <span class="comment">// This is the main loop, where packets are received.</span>
                <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0 ; true ; i++ ){
                        
                        <span class="comment">// Wait for an RTP packet.</span>
                        <span class="keyword">const</span> <a name="_a44"></a><a class="code" href="class_app_data_unit.html">AppDataUnit</a> *adu = NULL;
                        <span class="keywordflow">while</span> ( NULL == adu ) {
                                Thread::sleep(10);
                                adu = socket-&gt;<a name="a45"></a><a class="code" href="class_app_data_unit.html#a5">getData</a>(socket-&gt;getFirstTimestamp());
                        }
                        
                        <span class="comment">// Print content (likely a salute :))</span>
                        <span class="comment">// Note we are sure the data is an asciiz string.</span>
                        time_t receiving_time = time(NULL);
                        <span class="keywordtype">char</span> tmstring[30];
                        strftime(tmstring,30,<span class="stringliteral">"%X"</span>,localtime(&amp;receiving_time));
                        cout &lt;&lt; <span class="stringliteral">"Rx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc 
                             &lt;&lt; <span class="stringliteral">"): [receiving at "</span> &lt;&lt; tmstring &lt;&lt; <span class="stringliteral">"]: "</span> 
                             &lt;&lt; adu-&gt;<a class="code" href="class_app_data_unit.html#a5">getData</a>() &lt;&lt; endl;
                        <span class="keyword">delete</span> adu;
                }
        }
};

<span class="keyword">class </span>ccRTP_Hello_Tx: <span class="keyword">public</span> Thread, <span class="keyword">public</span> TimerPort
{

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

<span class="keyword">public</span>:
        ccRTP_Hello_Tx(){
                <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>
                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">"Tx: IP address is not correct!"</span> &lt;&lt; endl;
                        exit();
                }
                
                socket = <span class="keyword">new</span> <a class="code" href="group__sessions.html#ga0">RTPSession</a>(local_ip,TRANSMITTER_BASE);
                ssrc = socket-&gt;getLocalSSRC();
        }

        ~ccRTP_Hello_Tx(){
                cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">"Destroying transmitter -ID: "</span> &lt;&lt; hex 
                     &lt;&lt; (int)ssrc;
                terminate();
                <span class="keyword">delete</span> socket;
                cout &lt;&lt; <span class="stringliteral">"... "</span> &lt;&lt; <span class="stringliteral">"destroyed."</span>;
        }

        <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>
                cout &lt;&lt; <span class="stringliteral">"Tx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc &lt;&lt; <span class="stringliteral">"): "</span> &lt;&lt; 
                        local_ip.getHostname() 
                     &lt;&lt; <span class="stringliteral">" is going to salute perself through "</span> 
                     &lt;&lt; local_ip &lt;&lt; <span class="stringliteral">"..."</span> &lt;&lt; endl;
                
                <span class="comment">// Set up connection</span>
                socket-&gt;setSchedulingTimeout(20000);
                socket-&gt;setExpireTimeout(3000000);
                <span class="keywordflow">if</span>( !socket-&gt;addDestination(local_ip,RECEIVER_BASE) )
                        cerr &lt;&lt; <span class="stringliteral">"Tx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc 
                             &lt;&lt; <span class="stringliteral">"): could not connect to port."</span> 
                             &lt;&lt; RECEIVER_BASE;
                
                cout &lt;&lt; <span class="stringliteral">"Tx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc &lt;&lt; 
                        <span class="stringliteral">"): Transmitting salutes to port "</span>
                     &lt;&lt; RECEIVER_BASE &lt;&lt; <span class="stringliteral">"..."</span> &lt;&lt; endl;

                uint32 timestamp = 0;
                <span class="comment">// This will be useful for periodic execution</span>
                TimerPort::setTimer(1000);

                <span class="comment">// This is the main loop, where packets are sent.</span>
                socket-&gt;setPayloadFormat(<a class="code" href="class_static_payload_format.html">StaticPayloadFormat</a>(<a class="code" href="group__payload.html#gga2a26">sptMP2T</a>));
                socket-&gt;startRunning();
                <span class="comment">// Let's check the queues  (you should read the documentation</span>
                <span class="comment">// so that you know what the queues are for).</span>
                cout &lt;&lt; <span class="stringliteral">"Tx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc &lt;&lt; <span class="stringliteral">"): The queue is "</span>
                     &lt;&lt; ( socket-&gt;isActive()? <span class="stringliteral">""</span> : <span class="stringliteral">"in"</span>)
                     &lt;&lt; <span class="stringliteral">"active."</span> &lt;&lt; endl;

                <span class="keywordflow">for</span>( <span class="keywordtype">int</span> i = 0 ; true ;i++ ){

                        <span class="comment">// send RTP packets, providing timestamp,</span>
                        <span class="comment">// payload type and payload.  </span>
                        <span class="comment">// construct salute.</span>
                        <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> salute[50];
                        snprintf((<span class="keywordtype">char</span> *)salute,50,
                                 <span class="stringliteral">"Hello, brave gnu world (#%u)!"</span>,i);
                        time_t sending_time = time(NULL);
                        <span class="comment">// get timestamp to send salute</span>
                        <span class="keywordflow">if</span> ( 0 == i ){
                                timestamp = socket-&gt;getCurrentTimestamp();
                                
                        } <span class="keywordflow">else</span> {
                                <span class="comment">// increment for 1 second</span>
                                timestamp += socket-&gt;getCurrentRTPClockRate();
                        }       

                        socket-&gt;putData(timestamp,salute,
                                        strlen((<span class="keywordtype">char</span> *)salute)+1);
                        <span class="comment">// print info</span>
                        <span class="keywordtype">char</span> tmstring[30];
                        strftime(tmstring,30,<span class="stringliteral">"%X"</span>,
                                 localtime(&amp;sending_time));
                        cout &lt;&lt; <span class="stringliteral">"Tx ("</span> &lt;&lt; hex &lt;&lt; (int)ssrc 
                             &lt;&lt; <span class="stringliteral">"): sending salute "</span> &lt;&lt; <span class="stringliteral">"no "</span> &lt;&lt; dec &lt;&lt; i 
                             &lt;&lt; <span class="stringliteral">", at "</span> &lt;&lt; tmstring 
                             &lt;&lt; <span class="stringliteral">"..."</span> &lt;&lt; endl;

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

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

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

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

        cin.get();

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

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