examples/MultimediaProtocols/RTPExample/src/RTPFileStreamer.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008 � list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010 � this list of conditions and the following disclaimer in the documentation
00011 � and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013 � may be used to endorse or promote products derived from this software
00014 � without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:� 
00028 */
00029 
00030 
00031 #include "RTPFileStreamer.h"
00032 #include <commdbconnpref.h>
00033 
00034 _LIT(KGreetingText,"Welcome to RTP source file dump!\n");
00035 
00040 void CRtpFileSender::ConstructL(const TDesC& aSrcFilename)
00041         {
00042                         
00043         User::LeaveIfError(iFile.Replace(iFs,aSrcFilename,EFileWrite|EFileStreamText));
00044         TPtrC8 representation((TUint8*)(&KGreetingText)->Ptr(), (&KGreetingText)->Size());
00045         User::LeaveIfError(iFile.Write(representation));
00046         User::LeaveIfError(iFile.Flush());
00047         iFile.Close(); 
00048         
00049         User::LeaveIfError(iFile.Open(iFs,aSrcFilename,EFileRead|EFileWrite|EFileStreamText));
00050         iSendSrc = iSession.NewSendSourceL();
00051         iSendSrc.SetDefaultPayloadSize(iPacketSize);
00052         
00053         iSendSrc.PrivRegisterEventCallbackL(ERtpSendSucceeded, (TRtpCallbackFunction)CRtpFileSender::PacketSent, this);
00054         iSendSrc.PrivRegisterEventCallbackL(ERtpSendFail, (TRtpCallbackFunction)CRtpFileSender::SendError, this);
00055 
00056         iSendPacket = iSendSrc.NewSendPacketL();
00057 
00058         CActiveScheduler::Add(this);
00059         }
00060 
00065 void CRtpFileSender::PacketSent(CRtpFileSender* aPtr, const TRtpEvent& aEvent)
00066         {
00067         aPtr->DoPacketSent(aEvent);
00068         }
00069         
00075 void CRtpFileSender::DoPacketSent(const TRtpEvent& /*aEvent*/)
00076         {
00077         if (iObserver)
00078                 {
00079                 iObserver->NotifyPacketSent();
00080                 }
00081         iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
00082         SetActive();
00083         }
00084 
00088 void CRtpFileSender::SendError(CRtpFileSender* aPtr, const TRtpEvent& aEvent)
00089         {
00090         aPtr->DoSendError(aEvent);
00091         }
00092         
00097 void CRtpFileSender::DoSendError(const TRtpEvent& /*aEvent*/)
00098         {
00099         if (iObserver)
00100                 {
00101                 iObserver->NotifyError();
00102                 }
00103         }
00104                 
00105 CRtpFileSender::~CRtpFileSender()
00106         {
00107         Cancel();
00108         iFile.Close();
00109         iSendIntervalTimer.Close();
00110         iSendPacket.Close();
00111         iSendSrc.Close();
00112         }
00113 CRtpFileSender* CRtpFileSender::NewL(RRtpSession& aSession,RFs& aFs,const TDesC& aSrcFilename, TInt aPacketSize, TInt aDelayMicroSeconds)
00114         {
00115         if (!aSession.IsOpen())
00116                 {
00117                 User::Leave(KErrArgument);
00118                 }
00119         CRtpFileSender* self = new (ELeave) CRtpFileSender(aSession,aFs,aPacketSize,aDelayMicroSeconds);
00120         CleanupStack::PushL(self);
00121         self->ConstructL(aSrcFilename);
00122         CleanupStack::Pop(self);
00123         return self;
00124         }
00125 CRtpFileSender::CRtpFileSender(RRtpSession& aSession,RFs& aFs,TInt aPacketSize, TInt aDelayMicroSeconds) :
00126         CActive(0), iDelayMicroSecs(aDelayMicroSeconds), iPacketSize(aPacketSize), 
00127         iSession(aSession), iPayloadDesC(NULL,NULL), iFs(aFs)
00128         {
00129         }
00130 void CRtpFileSender::StartL()
00131         {
00132         User::LeaveIfError(iSendIntervalTimer.CreateLocal());
00133         iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
00134         SetActive();
00135         }
00136         
00144 void CRtpFileSender::RunL()
00145         {
00146         iSendPacket.SetTimestamp(User::FastCounter());
00147         iPayloadDesC.Set(const_cast<TUint8*>(iSendPacket.WritePayload().Ptr()),iPacketSize,iPacketSize);
00148         User::LeaveIfError(iFile.Read(iPayloadDesC));
00149         if (iPayloadDesC.Length()>0)
00150                 {
00151                 TInt tmp = iPayloadDesC.Length();
00152                 iSendPacket.WritePayload().SetLength(tmp);
00153                 iSendPacket.Send();
00154                 }
00155         else
00156                 {
00157                 if (iObserver)
00158                         {
00159                         iObserver->NotifyComplete();
00160                         }
00161                 }       
00162         }
00163 void CRtpFileSender::DoCancel()
00164         {
00165         iSendIntervalTimer.Cancel();
00166         }
00167 CRtpFileStreamer::CRtpFileStreamer(RSocketServ& aSocketServ, const TInetAddr& aDestAddr, TUint aLocalPort) :
00168       iSocketServ(aSocketServ),
00169       iDestAddr(TInetAddr(aDestAddr)),
00170       iLocalPort(aLocalPort)
00171         {
00172         }
00173 CRtpFileStreamer* CRtpFileStreamer::NewL(RSocketServ& aSocketServ,
00174                                                                                         const TDesC& aSrcFilename,
00175                                                                                         const TDesC& aDestFilename, 
00176                                                                                         TInt aBlockLen,
00177                                                                                         const TInetAddr& aDestAddr,
00178                                                                                         TUint aLocalPort, TInt aConnId)
00179         {
00180         CRtpFileStreamer* self = new (ELeave) CRtpFileStreamer(aSocketServ,aDestAddr,aLocalPort);
00181         CleanupStack::PushL(self);
00182         self->ConstructL(aSrcFilename,aDestFilename,aBlockLen,10000,aConnId);
00183         CleanupStack::Pop(self);
00184         return self;
00185         }
00186 
00192 void CRtpFileStreamer::ConstructL(const TDesC& aSrcFilename, const TDesC& aDestFilename, TInt aPacketSize, TInt aDelayMicroSeconds, TInt aConnId)
00193         {
00194         
00195         User::LeaveIfError(iRFs.Connect());
00196         iRFs.CreatePrivatePath(RFs::GetSystemDrive());
00197         iRFs.SetSessionToPrivate(RFs::GetSystemDrive());
00198         User::LeaveIfError(iDestFile.Replace(iRFs,aDestFilename,EFileWrite));
00199         TInetAddr localAddr;
00200         localAddr.SetPort(iLocalPort);
00201         _LIT8(KCname, "test");
00202                 
00203         if (aConnId!=KErrNotFound)
00204                 {
00205                 User::LeaveIfError(iConnection.Open(iSocketServ));
00206                 TCommDbConnPref prefs;
00207                 prefs.SetIapId(aConnId);
00208                 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00209                 TRequestStatus stat;
00210                 iConnection.Start(prefs, stat);
00211                 User::WaitForRequest( stat );
00212         User::LeaveIfError( stat.Int() );
00213                 iRtpSession.OpenL(iSocketServ, localAddr,iDestAddr,aPacketSize+12,iConnection, EPriorityNormal, KCname);        
00214                 }
00215         else
00216                 {
00217                 User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp)); 
00218                 User::LeaveIfError(iSocket.Bind(localAddr));
00219                 localAddr.SetPort(iLocalPort + 1);
00220                 User::LeaveIfError(iRtcpSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp));     
00221                 User::LeaveIfError(iRtcpSocket.Bind(localAddr));
00222                 TRequestStatus stat;
00223                 iSocket.Connect(iDestAddr,stat);
00224                 User::WaitForRequest(stat);
00225                 User::LeaveIfError(stat.Int());
00226                 iDestAddr.SetPort(iDestAddr.Port() + 1);
00227                 iRtcpSocket.Connect(iDestAddr,stat);
00228                 User::WaitForRequest(stat);
00229                 User::LeaveIfError(stat.Int());
00230                 iRtpSession.OpenL(iSocket, aPacketSize+12, iRtcpSocket);
00231                 iRtpSession.SetRTPTimeConversion(100, 100);
00232                 }                               
00233         iRtpSession.PrivRegisterEventCallbackL(ERtpNewSource, (TRtpCallbackFunction)CRtpFileStreamer::NewSource, this);
00234         iSender = CRtpFileSender::NewL(iRtpSession,iRFs,aSrcFilename, aPacketSize, aDelayMicroSeconds);
00235         }
00236 CRtpFileStreamer::~CRtpFileStreamer()
00237         {
00238         if (iSender)
00239                 {
00240                 delete iSender; 
00241                 }
00242         iDestFile.Close();
00243         iRFs.Close();
00244         iRecvPacket.Close();
00245         iRtpRecvSrc.Close();
00246         iRtpSession.Close();
00247         iSocket.Close();
00248         iRtcpSocket.Close();
00249         }
00250 
00251 void CRtpFileStreamer::StartL()
00252         {
00253         iSender->StartL();
00254         }
00258 void CRtpFileStreamer::NewSource(CRtpFileStreamer* aPtr, const TRtpEvent& aEvent)
00259         {
00260         if (aPtr->ReceiveSrc().IsOpen())
00261                 {
00262                 aPtr->ReceiveSrc().Close();
00263                 }
00264         TRAPD(err,
00265                 aPtr->ReceiveSrc() = aEvent.Session().NewReceiveSourceL();
00266                 aPtr->ReceiveSrc().PrivRegisterEventCallbackL(ERtpPacketReceived, (TRtpCallbackFunction)CRtpFileStreamer::PacketArrived, aPtr);)
00267         if (err!=KErrNone)
00268                 {
00269                 __DEBUGGER();
00270                 }
00271         }
00272         
00276 void CRtpFileStreamer::PacketArrived(CRtpFileStreamer* aPtr, const TRtpEvent& aEvent)
00277         {       
00278         aPtr->iRecvPacket.Close();
00279         aPtr->iRecvPacket = aEvent.ReceiveSource().Packet();
00280         if (aEvent.ReceiveSource().Packet().IsOpen())
00281                 {
00282                 __DEBUGGER();
00283                 }
00284         TRAPD(err,aPtr->HandleReceivedPacketL());
00285         if (err!=KErrNone)
00286                 {
00287                 __DEBUGGER();
00288                 }
00289         }
00290         
00295 void CRtpFileStreamer::HandleReceivedPacketL()
00296         {
00297         if (iObserver)
00298                 {
00299                 iObserver->NotifyPacketReceived();
00300                 }
00301         TPtrC8 writeDesc(iRecvPacket.Payload().Ptr(),iRecvPacket.Payload().Length());
00302         User::LeaveIfError(iDestFile.Write(writeDesc));
00303         }

Generated by  doxygen 1.6.2