<!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: rtphello.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 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>rtphello.cpp</h1>A basic example of how to use <a class="el" href="class_r_t_p_packet.html" title="A base class for both IncomingRTPPkt and OutgoingRTPPkt.">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" title="Generic and audio/video profile specific RTP interface of ccRTP.">ccrtp/rtp.h</a>></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="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="_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="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="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); 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="a2"></a><a class="code" href="group__sources.html#g206420a8423d0c261e9bfc5a388a9534" title="Get the RTPApplication object for the &quot;default&quot; application (the only one...">defaultApplication</a>(). getSDESItem(<a name="a3"></a><a class="code" href="group__rtcppacket.html#ggf6d38e5ccbbcdf2a24ebbd4fc5b4eeac56818aeaa2d578ab0055a59b8d8d28f9" title="Canonical end-point identifier.">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 << (<span class="keywordtype">int</span>)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="_a4"></a><a class="code" href="class_static_payload_format.html" title="Static payload format objects.">StaticPayloadFormat</a>(<a name="a5"></a><a class="code" href="group__payload.html#gg7644623ce29b8ca519d7e3f276b6ddc63f5b07dca910f599be16c871ebbb29d1" title="MPEG 2 Transport stream (RFCs 1890, 2250).">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="_a6"></a><a class="code" href="class_app_data_unit.html" title="Interface (envelope) to data received over RTP packets.">AppDataUnit</a> *adu = NULL; <span class="keywordflow">while</span> ( NULL == adu ) { Thread::sleep(10); adu = socket-><a name="a7"></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->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#be15787e1cc84dd03b0769a5546d7a38" title="Get data as it is received in RTP packets (i.e.">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" title="This template class adds the threading aspect to the RTPSessionBase template in one...">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#g24e16f350c9ae0c1e2a3f5d6a6dad9f9" title="Uses two pairs of sockets for RTP data and RTCP transmission/reception.">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 << (<span class="keywordtype">int</span>)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" title="Static payload format objects.">StaticPayloadFormat</a>(<a class="code" href="group__payload.html#gg7644623ce29b8ca519d7e3f276b6ddc63f5b07dca910f599be16c871ebbb29d1" title="MPEG 2 Transport stream (RFCs 1890, 2250).">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> </div> <hr size="1"><address style="text-align: right;"><small>Generated on Fri Jul 24 21:42:25 2009 for ccRTP by <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>