examples/MultimediaProtocols/RTPExample/src/RTPFileStreamer.cpp

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

Generated by  doxygen 1.6.2