Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_r_t_p_file_streamer_8cpp_source.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/MultimediaProtocols/RTPExample/src/RTPFileStreamer.cpp Source File</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.6.2 -->
<h1>examples/MultimediaProtocols/RTPExample/src/RTPFileStreamer.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of "Eclipse Public License v1.0"</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL "http://www.eclipse.org/legal/epl-v10.html".</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Description:</span>
<a name="l00014"></a>00014 <span class="comment">//</span>
<a name="l00015"></a>00015
<a name="l00016"></a>00016 <span class="preprocessor">#include "RTPFileStreamer.h"</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include <commdbconnpref.h></span>
<a name="l00018"></a>00018
<a name="l00019"></a>00019 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KGreetingText,<span class="stringliteral">"Welcome to RTP source file dump!\n"</span>);
<a name="l00020"></a>00020
<a name="l00025"></a>00025 <span class="keywordtype">void</span> CRtpFileSender::ConstructL(<span class="keyword">const</span> TDesC& aSrcFilename)
<a name="l00026"></a>00026 {
<a name="l00027"></a>00027
<a name="l00028"></a>00028 User::LeaveIfError(iFile.Replace(iFs,aSrcFilename,EFileWrite|EFileStreamText));
<a name="l00029"></a>00029 TPtrC8 representation((TUint8*)(&KGreetingText)->Ptr(), (&KGreetingText)->Size());
<a name="l00030"></a>00030 User::LeaveIfError(iFile.Write(representation));
<a name="l00031"></a>00031 User::LeaveIfError(iFile.Flush());
<a name="l00032"></a>00032 iFile.Close();
<a name="l00033"></a>00033
<a name="l00034"></a>00034 User::LeaveIfError(iFile.Open(iFs,aSrcFilename,EFileRead|EFileWrite|EFileStreamText));
<a name="l00035"></a>00035 iSendSrc = iSession.NewSendSourceL();
<a name="l00036"></a>00036 iSendSrc.SetDefaultPayloadSize(iPacketSize);
<a name="l00037"></a>00037
<a name="l00038"></a>00038 iSendSrc.PrivRegisterEventCallbackL(ERtpSendSucceeded, (TRtpCallbackFunction)<a class="code" href="class_c_rtp_file_sender.html#acc0e18b5616ed680f92433c2cc023375">CRtpFileSender::PacketSent</a>, <span class="keyword">this</span>);
<a name="l00039"></a>00039 iSendSrc.PrivRegisterEventCallbackL(ERtpSendFail, (TRtpCallbackFunction)<a class="code" href="class_c_rtp_file_sender.html#a2f2435036f8e6ad9e01b9433d8b269b7">CRtpFileSender::SendError</a>, <span class="keyword">this</span>);
<a name="l00040"></a>00040
<a name="l00041"></a>00041 iSendPacket = iSendSrc.NewSendPacketL();
<a name="l00042"></a>00042
<a name="l00043"></a>00043 CActiveScheduler::Add(<span class="keyword">this</span>);
<a name="l00044"></a>00044 }
<a name="l00045"></a>00045
<a name="l00050"></a><a class="code" href="class_c_rtp_file_sender.html#acc0e18b5616ed680f92433c2cc023375">00050</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_sender.html#acc0e18b5616ed680f92433c2cc023375">CRtpFileSender::PacketSent</a>(<a class="code" href="class_c_rtp_file_sender.html">CRtpFileSender</a>* aPtr, <span class="keyword">const</span> TRtpEvent& aEvent)
<a name="l00051"></a>00051 {
<a name="l00052"></a>00052 aPtr-><a class="code" href="class_c_rtp_file_sender.html#aecbbd6e4ff98c225474210edde8f02e5">DoPacketSent</a>(aEvent);
<a name="l00053"></a>00053 }
<a name="l00054"></a>00054
<a name="l00060"></a><a class="code" href="class_c_rtp_file_sender.html#aecbbd6e4ff98c225474210edde8f02e5">00060</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_sender.html#aecbbd6e4ff98c225474210edde8f02e5">CRtpFileSender::DoPacketSent</a>(<span class="keyword">const</span> TRtpEvent& <span class="comment">/*aEvent*/</span>)
<a name="l00061"></a>00061 {
<a name="l00062"></a>00062 <span class="keywordflow">if</span> (iObserver)
<a name="l00063"></a>00063 {
<a name="l00064"></a>00064 iObserver->NotifyPacketSent();
<a name="l00065"></a>00065 }
<a name="l00066"></a>00066 iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
<a name="l00067"></a>00067 SetActive();
<a name="l00068"></a>00068 }
<a name="l00069"></a>00069
<a name="l00073"></a><a class="code" href="class_c_rtp_file_sender.html#a2f2435036f8e6ad9e01b9433d8b269b7">00073</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_sender.html#a2f2435036f8e6ad9e01b9433d8b269b7">CRtpFileSender::SendError</a>(<a class="code" href="class_c_rtp_file_sender.html">CRtpFileSender</a>* aPtr, <span class="keyword">const</span> TRtpEvent& aEvent)
<a name="l00074"></a>00074 {
<a name="l00075"></a>00075 aPtr-><a class="code" href="class_c_rtp_file_sender.html#acd993430262ed8a1909fc47b0ef36c99">DoSendError</a>(aEvent);
<a name="l00076"></a>00076 }
<a name="l00077"></a>00077
<a name="l00082"></a><a class="code" href="class_c_rtp_file_sender.html#acd993430262ed8a1909fc47b0ef36c99">00082</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_sender.html#acd993430262ed8a1909fc47b0ef36c99">CRtpFileSender::DoSendError</a>(<span class="keyword">const</span> TRtpEvent& <span class="comment">/*aEvent*/</span>)
<a name="l00083"></a>00083 {
<a name="l00084"></a>00084 <span class="keywordflow">if</span> (iObserver)
<a name="l00085"></a>00085 {
<a name="l00086"></a>00086 iObserver->NotifyError();
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 }
<a name="l00089"></a>00089
<a name="l00090"></a>00090 CRtpFileSender::~CRtpFileSender()
<a name="l00091"></a>00091 {
<a name="l00092"></a>00092 Cancel();
<a name="l00093"></a>00093 iFile.Close();
<a name="l00094"></a>00094 iSendIntervalTimer.Close();
<a name="l00095"></a>00095 iSendPacket.Close();
<a name="l00096"></a>00096 iSendSrc.Close();
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 <a class="code" href="class_c_rtp_file_sender.html">CRtpFileSender</a>* CRtpFileSender::NewL(RRtpSession& aSession,RFs& aFs,<span class="keyword">const</span> TDesC& aSrcFilename, TInt aPacketSize, TInt aDelayMicroSeconds)
<a name="l00099"></a>00099 {
<a name="l00100"></a>00100 <span class="keywordflow">if</span> (!aSession.IsOpen())
<a name="l00101"></a>00101 {
<a name="l00102"></a>00102 User::Leave(KErrArgument);
<a name="l00103"></a>00103 }
<a name="l00104"></a>00104 <a class="code" href="class_c_rtp_file_sender.html">CRtpFileSender</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_rtp_file_sender.html">CRtpFileSender</a>(aSession,aFs,aPacketSize,aDelayMicroSeconds);
<a name="l00105"></a>00105 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00106"></a>00106 <span class="keyword">self</span>->ConstructL(aSrcFilename);
<a name="l00107"></a>00107 CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00108"></a>00108 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00109"></a>00109 }
<a name="l00110"></a>00110 CRtpFileSender::CRtpFileSender(RRtpSession& aSession,RFs& aFs,TInt aPacketSize, TInt aDelayMicroSeconds) :
<a name="l00111"></a>00111 CActive(0), iDelayMicroSecs(aDelayMicroSeconds), iPacketSize(aPacketSize),
<a name="l00112"></a>00112 iSession(aSession), iPayloadDesC(NULL,NULL), iFs(aFs)
<a name="l00113"></a>00113 {
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 <span class="keywordtype">void</span> CRtpFileSender::StartL()
<a name="l00116"></a>00116 {
<a name="l00117"></a>00117 User::LeaveIfError(iSendIntervalTimer.CreateLocal());
<a name="l00118"></a>00118 iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
<a name="l00119"></a>00119 SetActive();
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121
<a name="l00129"></a>00129 <span class="keywordtype">void</span> CRtpFileSender::RunL()
<a name="l00130"></a>00130 {
<a name="l00131"></a>00131 iSendPacket.SetTimestamp(User::FastCounter());
<a name="l00132"></a>00132 iPayloadDesC.Set(const_cast<TUint8*>(iSendPacket.WritePayload().Ptr()),iPacketSize,iPacketSize);
<a name="l00133"></a>00133 User::LeaveIfError(iFile.Read(iPayloadDesC));
<a name="l00134"></a>00134 <span class="keywordflow">if</span> (iPayloadDesC.Length()>0)
<a name="l00135"></a>00135 {
<a name="l00136"></a>00136 TInt tmp = iPayloadDesC.Length();
<a name="l00137"></a>00137 iSendPacket.WritePayload().SetLength(tmp);
<a name="l00138"></a>00138 iSendPacket.Send();
<a name="l00139"></a>00139 }
<a name="l00140"></a>00140 <span class="keywordflow">else</span>
<a name="l00141"></a>00141 {
<a name="l00142"></a>00142 <span class="keywordflow">if</span> (iObserver)
<a name="l00143"></a>00143 {
<a name="l00144"></a>00144 iObserver->NotifyComplete();
<a name="l00145"></a>00145 }
<a name="l00146"></a>00146 }
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148 <span class="keywordtype">void</span> CRtpFileSender::DoCancel()
<a name="l00149"></a>00149 {
<a name="l00150"></a>00150 iSendIntervalTimer.Cancel();
<a name="l00151"></a>00151 }
<a name="l00152"></a>00152 CRtpFileStreamer::CRtpFileStreamer(RSocketServ& aSocketServ, <span class="keyword">const</span> TInetAddr& aDestAddr, TUint aLocalPort) :
<a name="l00153"></a>00153 iSocketServ(aSocketServ),
<a name="l00154"></a>00154 iDestAddr(TInetAddr(aDestAddr)),
<a name="l00155"></a>00155 iLocalPort(aLocalPort)
<a name="l00156"></a>00156 {
<a name="l00157"></a>00157 }
<a name="l00158"></a>00158 <a class="code" href="class_c_rtp_file_streamer.html">CRtpFileStreamer</a>* CRtpFileStreamer::NewL(RSocketServ& aSocketServ,
<a name="l00159"></a>00159 <span class="keyword">const</span> TDesC& aSrcFilename,
<a name="l00160"></a>00160 <span class="keyword">const</span> TDesC& aDestFilename,
<a name="l00161"></a>00161 TInt aBlockLen,
<a name="l00162"></a>00162 <span class="keyword">const</span> TInetAddr& aDestAddr,
<a name="l00163"></a>00163 TUint aLocalPort, TInt aConnId)
<a name="l00164"></a>00164 {
<a name="l00165"></a>00165 <a class="code" href="class_c_rtp_file_streamer.html">CRtpFileStreamer</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave) <a class="code" href="class_c_rtp_file_streamer.html">CRtpFileStreamer</a>(aSocketServ,aDestAddr,aLocalPort);
<a name="l00166"></a>00166 CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00167"></a>00167 <span class="keyword">self</span>->ConstructL(aSrcFilename,aDestFilename,aBlockLen,10000,aConnId);
<a name="l00168"></a>00168 CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00169"></a>00169 <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00170"></a>00170 }
<a name="l00171"></a>00171
<a name="l00177"></a>00177 <span class="keywordtype">void</span> CRtpFileStreamer::ConstructL(<span class="keyword">const</span> TDesC& aSrcFilename, <span class="keyword">const</span> TDesC& aDestFilename, TInt aPacketSize, TInt aDelayMicroSeconds, TInt aConnId)
<a name="l00178"></a>00178 {
<a name="l00179"></a>00179
<a name="l00180"></a>00180 User::LeaveIfError(iRFs.Connect());
<a name="l00181"></a>00181 iRFs.CreatePrivatePath(RFs::GetSystemDrive());
<a name="l00182"></a>00182 iRFs.SetSessionToPrivate(RFs::GetSystemDrive());
<a name="l00183"></a>00183 User::LeaveIfError(iDestFile.Replace(iRFs,aDestFilename,EFileWrite));
<a name="l00184"></a>00184 TInetAddr localAddr;
<a name="l00185"></a>00185 localAddr.SetPort(iLocalPort);
<a name="l00186"></a>00186 _LIT8(KCname, <span class="stringliteral">"test"</span>);
<a name="l00187"></a>00187
<a name="l00188"></a>00188 <span class="keywordflow">if</span> (aConnId!=KErrNotFound)
<a name="l00189"></a>00189 {
<a name="l00190"></a>00190 User::LeaveIfError(iConnection.Open(iSocketServ));
<a name="l00191"></a>00191 TCommDbConnPref prefs;
<a name="l00192"></a>00192 prefs.SetIapId(aConnId);
<a name="l00193"></a>00193 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
<a name="l00194"></a>00194 TRequestStatus stat;
<a name="l00195"></a>00195 iConnection.Start(prefs, stat);
<a name="l00196"></a>00196 User::WaitForRequest( stat );
<a name="l00197"></a>00197 User::LeaveIfError( stat.Int() );
<a name="l00198"></a>00198 iRtpSession.OpenL(iSocketServ, localAddr,iDestAddr,aPacketSize+12,iConnection, EPriorityNormal, KCname);
<a name="l00199"></a>00199 }
<a name="l00200"></a>00200 <span class="keywordflow">else</span>
<a name="l00201"></a>00201 {
<a name="l00202"></a>00202 User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp));
<a name="l00203"></a>00203 User::LeaveIfError(iSocket.Bind(localAddr));
<a name="l00204"></a>00204 localAddr.SetPort(iLocalPort + 1);
<a name="l00205"></a>00205 User::LeaveIfError(iRtcpSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp));
<a name="l00206"></a>00206 User::LeaveIfError(iRtcpSocket.Bind(localAddr));
<a name="l00207"></a>00207 TRequestStatus stat;
<a name="l00208"></a>00208 iSocket.Connect(iDestAddr,stat);
<a name="l00209"></a>00209 User::WaitForRequest(stat);
<a name="l00210"></a>00210 User::LeaveIfError(stat.Int());
<a name="l00211"></a>00211 iDestAddr.SetPort(iDestAddr.Port() + 1);
<a name="l00212"></a>00212 iRtcpSocket.Connect(iDestAddr,stat);
<a name="l00213"></a>00213 User::WaitForRequest(stat);
<a name="l00214"></a>00214 User::LeaveIfError(stat.Int());
<a name="l00215"></a>00215 iRtpSession.OpenL(iSocket, aPacketSize+12, iRtcpSocket);
<a name="l00216"></a>00216 iRtpSession.SetRTPTimeConversion(100, 100);
<a name="l00217"></a>00217 }
<a name="l00218"></a>00218 iRtpSession.PrivRegisterEventCallbackL(ERtpNewSource, (TRtpCallbackFunction)<a class="code" href="class_c_rtp_file_streamer.html#adffe5f4e61feb928764fcf94a55ded5a">CRtpFileStreamer::NewSource</a>, <span class="keyword">this</span>);
<a name="l00219"></a>00219 iSender = CRtpFileSender::NewL(iRtpSession,iRFs,aSrcFilename, aPacketSize, aDelayMicroSeconds);
<a name="l00220"></a>00220 }
<a name="l00221"></a>00221 CRtpFileStreamer::~CRtpFileStreamer()
<a name="l00222"></a>00222 {
<a name="l00223"></a>00223 <span class="keywordflow">if</span> (iSender)
<a name="l00224"></a>00224 {
<a name="l00225"></a>00225 <span class="keyword">delete</span> iSender;
<a name="l00226"></a>00226 }
<a name="l00227"></a>00227 iDestFile.Close();
<a name="l00228"></a>00228 iRFs.Close();
<a name="l00229"></a>00229 iRecvPacket.Close();
<a name="l00230"></a>00230 iRtpRecvSrc.Close();
<a name="l00231"></a>00231 iRtpSession.Close();
<a name="l00232"></a>00232 iSocket.Close();
<a name="l00233"></a>00233 iRtcpSocket.Close();
<a name="l00234"></a>00234 }
<a name="l00235"></a>00235
<a name="l00236"></a>00236 <span class="keywordtype">void</span> CRtpFileStreamer::StartL()
<a name="l00237"></a>00237 {
<a name="l00238"></a>00238 iSender->StartL();
<a name="l00239"></a>00239 }
<a name="l00243"></a><a class="code" href="class_c_rtp_file_streamer.html#adffe5f4e61feb928764fcf94a55ded5a">00243</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_streamer.html#adffe5f4e61feb928764fcf94a55ded5a">CRtpFileStreamer::NewSource</a>(<a class="code" href="class_c_rtp_file_streamer.html">CRtpFileStreamer</a>* aPtr, <span class="keyword">const</span> TRtpEvent& aEvent)
<a name="l00244"></a>00244 {
<a name="l00245"></a>00245 <span class="keywordflow">if</span> (aPtr->ReceiveSrc().IsOpen())
<a name="l00246"></a>00246 {
<a name="l00247"></a>00247 aPtr->ReceiveSrc().Close();
<a name="l00248"></a>00248 }
<a name="l00249"></a>00249 TRAPD(err,
<a name="l00250"></a>00250 aPtr->ReceiveSrc() = aEvent.Session().NewReceiveSourceL();
<a name="l00251"></a>00251 aPtr->ReceiveSrc().PrivRegisterEventCallbackL(ERtpPacketReceived, (TRtpCallbackFunction)<a class="code" href="class_c_rtp_file_streamer.html#a21ee5f7d8e570650912d84afea2a1e32">CRtpFileStreamer::PacketArrived</a>, aPtr);)
<a name="l00252"></a>00252 <span class="keywordflow">if</span> (err!=KErrNone)
<a name="l00253"></a>00253 {
<a name="l00254"></a>00254 __DEBUGGER();
<a name="l00255"></a>00255 }
<a name="l00256"></a>00256 }
<a name="l00257"></a>00257
<a name="l00261"></a><a class="code" href="class_c_rtp_file_streamer.html#a21ee5f7d8e570650912d84afea2a1e32">00261</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_streamer.html#a21ee5f7d8e570650912d84afea2a1e32">CRtpFileStreamer::PacketArrived</a>(<a class="code" href="class_c_rtp_file_streamer.html">CRtpFileStreamer</a>* aPtr, <span class="keyword">const</span> TRtpEvent& aEvent)
<a name="l00262"></a>00262 {
<a name="l00263"></a>00263 aPtr->iRecvPacket.Close();
<a name="l00264"></a>00264 aPtr->iRecvPacket = aEvent.ReceiveSource().Packet();
<a name="l00265"></a>00265 <span class="keywordflow">if</span> (aEvent.ReceiveSource().Packet().IsOpen())
<a name="l00266"></a>00266 {
<a name="l00267"></a>00267 __DEBUGGER();
<a name="l00268"></a>00268 }
<a name="l00269"></a>00269 TRAPD(err,aPtr-><a class="code" href="class_c_rtp_file_streamer.html#a4a974344fba7f1735ba7aa06a7db87a3">HandleReceivedPacketL</a>());
<a name="l00270"></a>00270 <span class="keywordflow">if</span> (err!=KErrNone)
<a name="l00271"></a>00271 {
<a name="l00272"></a>00272 __DEBUGGER();
<a name="l00273"></a>00273 }
<a name="l00274"></a>00274 }
<a name="l00275"></a>00275
<a name="l00280"></a><a class="code" href="class_c_rtp_file_streamer.html#a4a974344fba7f1735ba7aa06a7db87a3">00280</a> <span class="keywordtype">void</span> <a class="code" href="class_c_rtp_file_streamer.html#a4a974344fba7f1735ba7aa06a7db87a3">CRtpFileStreamer::HandleReceivedPacketL</a>()
<a name="l00281"></a>00281 {
<a name="l00282"></a>00282 <span class="keywordflow">if</span> (iObserver)
<a name="l00283"></a>00283 {
<a name="l00284"></a>00284 iObserver->NotifyPacketReceived();
<a name="l00285"></a>00285 }
<a name="l00286"></a>00286 TPtrC8 writeDesc(iRecvPacket.Payload().Ptr(),iRecvPacket.Payload().Length());
<a name="l00287"></a>00287 User::LeaveIfError(iDestFile.Write(writeDesc));
<a name="l00288"></a>00288 }
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>