examples/MultimediaProtocols/RTPExample/src/RTPFileStreamer.cpp

00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include "RTPFileStreamer.h"
00017 #include <commdbconnpref.h>
00018 
00019 _LIT(KGreetingText,"Welcome to RTP source file dump!\n");
00020 
00025 void CRtpFileSender::ConstructL(const TDesC& aSrcFilename)
00026         {
00027                         
00028         User::LeaveIfError(iFile.Replace(iFs,aSrcFilename,EFileWrite|EFileStreamText));
00029         TPtrC8 representation((TUint8*)(&KGreetingText)->Ptr(), (&KGreetingText)->Size());
00030         User::LeaveIfError(iFile.Write(representation));
00031         User::LeaveIfError(iFile.Flush());
00032         iFile.Close(); 
00033         
00034         User::LeaveIfError(iFile.Open(iFs,aSrcFilename,EFileRead|EFileWrite|EFileStreamText));
00035         iSendSrc = iSession.NewSendSourceL();
00036         iSendSrc.SetDefaultPayloadSize(iPacketSize);
00037         
00038         iSendSrc.PrivRegisterEventCallbackL(ERtpSendSucceeded, (TRtpCallbackFunction)CRtpFileSender::PacketSent, this);
00039         iSendSrc.PrivRegisterEventCallbackL(ERtpSendFail, (TRtpCallbackFunction)CRtpFileSender::SendError, this);
00040 
00041         iSendPacket = iSendSrc.NewSendPacketL();
00042 
00043         CActiveScheduler::Add(this);
00044         }
00045 
00050 void CRtpFileSender::PacketSent(CRtpFileSender* aPtr, const TRtpEvent& aEvent)
00051         {
00052         aPtr->DoPacketSent(aEvent);
00053         }
00054         
00060 void CRtpFileSender::DoPacketSent(const TRtpEvent& /*aEvent*/)
00061         {
00062         if (iObserver)
00063                 {
00064                 iObserver->NotifyPacketSent();
00065                 }
00066         iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
00067         SetActive();
00068         }
00069 
00073 void CRtpFileSender::SendError(CRtpFileSender* aPtr, const TRtpEvent& aEvent)
00074         {
00075         aPtr->DoSendError(aEvent);
00076         }
00077         
00082 void CRtpFileSender::DoSendError(const TRtpEvent& /*aEvent*/)
00083         {
00084         if (iObserver)
00085                 {
00086                 iObserver->NotifyError();
00087                 }
00088         }
00089                 
00090 CRtpFileSender::~CRtpFileSender()
00091         {
00092         Cancel();
00093         iFile.Close();
00094         iSendIntervalTimer.Close();
00095         iSendPacket.Close();
00096         iSendSrc.Close();
00097         }
00098 CRtpFileSender* CRtpFileSender::NewL(RRtpSession& aSession,RFs& aFs,const TDesC& aSrcFilename, TInt aPacketSize, TInt aDelayMicroSeconds)
00099         {
00100         if (!aSession.IsOpen())
00101                 {
00102                 User::Leave(KErrArgument);
00103                 }
00104         CRtpFileSender* self = new (ELeave) CRtpFileSender(aSession,aFs,aPacketSize,aDelayMicroSeconds);
00105         CleanupStack::PushL(self);
00106         self->ConstructL(aSrcFilename);
00107         CleanupStack::Pop(self);
00108         return self;
00109         }
00110 CRtpFileSender::CRtpFileSender(RRtpSession& aSession,RFs& aFs,TInt aPacketSize, TInt aDelayMicroSeconds) :
00111         CActive(0), iDelayMicroSecs(aDelayMicroSeconds), iPacketSize(aPacketSize), 
00112         iSession(aSession), iPayloadDesC(NULL,NULL), iFs(aFs)
00113         {
00114         }
00115 void CRtpFileSender::StartL()
00116         {
00117         User::LeaveIfError(iSendIntervalTimer.CreateLocal());
00118         iSendIntervalTimer.After(iStatus,TTimeIntervalMicroSeconds32(iDelayMicroSecs));
00119         SetActive();
00120         }
00121         
00129 void CRtpFileSender::RunL()
00130         {
00131         iSendPacket.SetTimestamp(User::FastCounter());
00132         iPayloadDesC.Set(const_cast<TUint8*>(iSendPacket.WritePayload().Ptr()),iPacketSize,iPacketSize);
00133         User::LeaveIfError(iFile.Read(iPayloadDesC));
00134         if (iPayloadDesC.Length()>0)
00135                 {
00136                 TInt tmp = iPayloadDesC.Length();
00137                 iSendPacket.WritePayload().SetLength(tmp);
00138                 iSendPacket.Send();
00139                 }
00140         else
00141                 {
00142                 if (iObserver)
00143                         {
00144                         iObserver->NotifyComplete();
00145                         }
00146                 }       
00147         }
00148 void CRtpFileSender::DoCancel()
00149         {
00150         iSendIntervalTimer.Cancel();
00151         }
00152 CRtpFileStreamer::CRtpFileStreamer(RSocketServ& aSocketServ, const TInetAddr& aDestAddr, TUint aLocalPort) :
00153       iSocketServ(aSocketServ),
00154       iDestAddr(TInetAddr(aDestAddr)),
00155       iLocalPort(aLocalPort)
00156         {
00157         }
00158 CRtpFileStreamer* CRtpFileStreamer::NewL(RSocketServ& aSocketServ,
00159                                                                                         const TDesC& aSrcFilename,
00160                                                                                         const TDesC& aDestFilename, 
00161                                                                                         TInt aBlockLen,
00162                                                                                         const TInetAddr& aDestAddr,
00163                                                                                         TUint aLocalPort, TInt aConnId)
00164         {
00165         CRtpFileStreamer* self = new (ELeave) CRtpFileStreamer(aSocketServ,aDestAddr,aLocalPort);
00166         CleanupStack::PushL(self);
00167         self->ConstructL(aSrcFilename,aDestFilename,aBlockLen,10000,aConnId);
00168         CleanupStack::Pop(self);
00169         return self;
00170         }
00171 
00177 void CRtpFileStreamer::ConstructL(const TDesC& aSrcFilename, const TDesC& aDestFilename, TInt aPacketSize, TInt aDelayMicroSeconds, TInt aConnId)
00178         {
00179         
00180         User::LeaveIfError(iRFs.Connect());
00181         iRFs.CreatePrivatePath(RFs::GetSystemDrive());
00182         iRFs.SetSessionToPrivate(RFs::GetSystemDrive());
00183         User::LeaveIfError(iDestFile.Replace(iRFs,aDestFilename,EFileWrite));
00184         TInetAddr localAddr;
00185         localAddr.SetPort(iLocalPort);
00186         _LIT8(KCname, "test");
00187                 
00188         if (aConnId!=KErrNotFound)
00189                 {
00190                 User::LeaveIfError(iConnection.Open(iSocketServ));
00191                 TCommDbConnPref prefs;
00192                 prefs.SetIapId(aConnId);
00193                 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00194                 TRequestStatus stat;
00195                 iConnection.Start(prefs, stat);
00196                 User::WaitForRequest( stat );
00197         User::LeaveIfError( stat.Int() );
00198                 iRtpSession.OpenL(iSocketServ, localAddr,iDestAddr,aPacketSize+12,iConnection, EPriorityNormal, KCname);        
00199                 }
00200         else
00201                 {
00202                 User::LeaveIfError(iSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp)); 
00203                 User::LeaveIfError(iSocket.Bind(localAddr));
00204                 localAddr.SetPort(iLocalPort + 1);
00205                 User::LeaveIfError(iRtcpSocket.Open(iSocketServ, KAfInet,KSockDatagram, KProtocolInetUdp));     
00206                 User::LeaveIfError(iRtcpSocket.Bind(localAddr));
00207                 TRequestStatus stat;
00208                 iSocket.Connect(iDestAddr,stat);
00209                 User::WaitForRequest(stat);
00210                 User::LeaveIfError(stat.Int());
00211                 iDestAddr.SetPort(iDestAddr.Port() + 1);
00212                 iRtcpSocket.Connect(iDestAddr,stat);
00213                 User::WaitForRequest(stat);
00214                 User::LeaveIfError(stat.Int());
00215                 iRtpSession.OpenL(iSocket, aPacketSize+12, iRtcpSocket);
00216                 iRtpSession.SetRTPTimeConversion(100, 100);
00217                 }                               
00218         iRtpSession.PrivRegisterEventCallbackL(ERtpNewSource, (TRtpCallbackFunction)CRtpFileStreamer::NewSource, this);
00219         iSender = CRtpFileSender::NewL(iRtpSession,iRFs,aSrcFilename, aPacketSize, aDelayMicroSeconds);
00220         }
00221 CRtpFileStreamer::~CRtpFileStreamer()
00222         {
00223         if (iSender)
00224                 {
00225                 delete iSender; 
00226                 }
00227         iDestFile.Close();
00228         iRFs.Close();
00229         iRecvPacket.Close();
00230         iRtpRecvSrc.Close();
00231         iRtpSession.Close();
00232         iSocket.Close();
00233         iRtcpSocket.Close();
00234         }
00235 
00236 void CRtpFileStreamer::StartL()
00237         {
00238         iSender->StartL();
00239         }
00243 void CRtpFileStreamer::NewSource(CRtpFileStreamer* aPtr, const TRtpEvent& aEvent)
00244         {
00245         if (aPtr->ReceiveSrc().IsOpen())
00246                 {
00247                 aPtr->ReceiveSrc().Close();
00248                 }
00249         TRAPD(err,
00250                 aPtr->ReceiveSrc() = aEvent.Session().NewReceiveSourceL();
00251                 aPtr->ReceiveSrc().PrivRegisterEventCallbackL(ERtpPacketReceived, (TRtpCallbackFunction)CRtpFileStreamer::PacketArrived, aPtr);)
00252         if (err!=KErrNone)
00253                 {
00254                 __DEBUGGER();
00255                 }
00256         }
00257         
00261 void CRtpFileStreamer::PacketArrived(CRtpFileStreamer* aPtr, const TRtpEvent& aEvent)
00262         {       
00263         aPtr->iRecvPacket.Close();
00264         aPtr->iRecvPacket = aEvent.ReceiveSource().Packet();
00265         if (aEvent.ReceiveSource().Packet().IsOpen())
00266                 {
00267                 __DEBUGGER();
00268                 }
00269         TRAPD(err,aPtr->HandleReceivedPacketL());
00270         if (err!=KErrNone)
00271                 {
00272                 __DEBUGGER();
00273                 }
00274         }
00275         
00280 void CRtpFileStreamer::HandleReceivedPacketL()
00281         {
00282         if (iObserver)
00283                 {
00284                 iObserver->NotifyPacketReceived();
00285                 }
00286         TPtrC8 writeDesc(iRecvPacket.Payload().Ptr(),iRecvPacket.Payload().Length());
00287         User::LeaveIfError(iDestFile.Write(writeDesc));
00288         }

Generated by  doxygen 1.6.2