<!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 Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related 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 <fedemp@altern.org></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 <cstdio></span> <span class="preprocessor">#include <ctime></span> <span class="comment">// In order to use ccRTP, the RTP stack of CommonC++, just include...</span> <span class="preprocessor">#include <<a class="code" href="rtp_8h.html">ccrtp/rtp.h</a>></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 << <span class="stringliteral">"Rx: IP address is not correct!"</span> << 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->getLocalSSRC(); } ~ccRTP_Hello_Rx(){ cout << endl << <span class="stringliteral">"Destroying receiver -ID: "</span> << hex << (int)ssrc; terminate(); <span class="keyword">delete</span> socket; cout << <span class="stringliteral">"... "</span> << <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 << <span class="stringliteral">"Hello, "</span> << <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>) << <span class="stringliteral">" ..."</span> << endl; <span class="comment">// redefined from Thread.</span> <span class="comment">// Set up connection</span> socket->setSchedulingTimeout(20000); socket->setExpireTimeout(3000000); <span class="comment">//socket->UDPTransmit::setTypeOfService(SOCKET_IPTOS_LOWDELAY);</span> <span class="keywordflow">if</span>( !socket->addDestination(local_ip,TRANSMITTER_BASE) ) cerr << <span class="stringliteral">"Rx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): could not connect to port."</span> << TRANSMITTER_BASE; cout << <span class="stringliteral">"Rx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): "</span> << local_ip.getHostname() << <span class="stringliteral">" is waiting for salutes in port "</span> << RECEIVER_BASE << <span class="stringliteral">"..."</span> << endl; socket->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->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 << <span class="stringliteral">"Rx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): The queue is "</span> << ( socket->isActive() ? <span class="stringliteral">""</span> : <span class="stringliteral">"in"</span>) << <span class="stringliteral">"active."</span> << 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-><a name="a45"></a><a class="code" href="class_app_data_unit.html#a5">getData</a>(socket->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(&receiving_time)); cout << <span class="stringliteral">"Rx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): [receiving at "</span> << tmstring << <span class="stringliteral">"]: "</span> << adu-><a class="code" href="class_app_data_unit.html#a5">getData</a>() << 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 << <span class="stringliteral">"Tx: IP address is not correct!"</span> << endl; exit(); } socket = <span class="keyword">new</span> <a class="code" href="group__sessions.html#ga0">RTPSession</a>(local_ip,TRANSMITTER_BASE); ssrc = socket->getLocalSSRC(); } ~ccRTP_Hello_Tx(){ cout << endl << <span class="stringliteral">"Destroying transmitter -ID: "</span> << hex << (int)ssrc; terminate(); <span class="keyword">delete</span> socket; cout << <span class="stringliteral">"... "</span> << <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 << <span class="stringliteral">"Tx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): "</span> << local_ip.getHostname() << <span class="stringliteral">" is going to salute perself through "</span> << local_ip << <span class="stringliteral">"..."</span> << endl; <span class="comment">// Set up connection</span> socket->setSchedulingTimeout(20000); socket->setExpireTimeout(3000000); <span class="keywordflow">if</span>( !socket->addDestination(local_ip,RECEIVER_BASE) ) cerr << <span class="stringliteral">"Tx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): could not connect to port."</span> << RECEIVER_BASE; cout << <span class="stringliteral">"Tx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): Transmitting salutes to port "</span> << RECEIVER_BASE << <span class="stringliteral">"..."</span> << 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->setPayloadFormat(<a class="code" href="class_static_payload_format.html">StaticPayloadFormat</a>(<a class="code" href="group__payload.html#gga2a26">sptMP2T</a>)); socket->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 << <span class="stringliteral">"Tx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): The queue is "</span> << ( socket->isActive()? <span class="stringliteral">""</span> : <span class="stringliteral">"in"</span>) << <span class="stringliteral">"active."</span> << 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->getCurrentTimestamp(); } <span class="keywordflow">else</span> { <span class="comment">// increment for 1 second</span> timestamp += socket->getCurrentRTPClockRate(); } socket->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(&sending_time)); cout << <span class="stringliteral">"Tx ("</span> << hex << (int)ssrc << <span class="stringliteral">"): sending salute "</span> << <span class="stringliteral">"no "</span> << dec << i << <span class="stringliteral">", at "</span> << tmstring << <span class="stringliteral">"..."</span> << 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 << <span class="stringliteral">"This is rtphello, a very simple test program for ccRTP."</span> << endl << <span class="stringliteral">"Strike [Enter] when you are fed up with it."</span> << endl; <span class="comment">// Start execution of hello now.</span> receiver->start(); transmitter->start(); cin.get(); <span class="keyword">delete</span> transmitter; <span class="keyword">delete</span> receiver; cout << endl << <span class="stringliteral">"That's all."</span> << 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 <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>